angle_shader_validation.rs (3032B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 extern crate mozangle; 6 extern crate webrender; 7 extern crate webrender_build; 8 9 use mozangle::shaders::{BuiltInResources, Output, ShaderSpec, ShaderValidator}; 10 use webrender_build::shader::{ShaderFeatureFlags, ShaderVersion, build_shader_strings, get_shader_features}; 11 12 // from glslang 13 const FRAGMENT_SHADER: u32 = 0x8B30; 14 const VERTEX_SHADER: u32 = 0x8B31; 15 16 #[test] 17 fn validate_shaders() { 18 mozangle::shaders::initialize().unwrap(); 19 20 let resources = BuiltInResources::default(); 21 let vs_validator = 22 ShaderValidator::new(VERTEX_SHADER, ShaderSpec::Gles3, Output::Essl, &resources).unwrap(); 23 24 let fs_validator = 25 ShaderValidator::new(FRAGMENT_SHADER, ShaderSpec::Gles3, Output::Essl, &resources).unwrap(); 26 27 for (shader, configs) in get_shader_features(ShaderFeatureFlags::GLES) { 28 for config in configs { 29 let features = config.split(",").filter(|f| !f.is_empty()).collect::<Vec<_>>(); 30 31 let (vs, fs) = build_shader_strings( 32 ShaderVersion::Gles, 33 &features, 34 shader, 35 &|f| webrender::get_unoptimized_shader_source(f, None) 36 ); 37 38 let full_shader_name = format!("{} {}", shader, config); 39 validate(&vs_validator, &full_shader_name, vs); 40 validate(&fs_validator, &full_shader_name, fs); 41 } 42 } 43 } 44 45 fn validate(validator: &ShaderValidator, name: &str, source: String) { 46 // Check for each `switch` to have a `default`, see 47 // https://github.com/servo/webrender/wiki/Driver-issues#lack-of-default-case-in-a-switch 48 assert_eq!(source.matches("switch").count(), source.matches("default:").count(), 49 "Shader '{}' doesn't have all `switch` covered with `default` cases", name); 50 // Run Angle validator 51 match validator.compile_and_translate(&[&source]) { 52 Ok(_) => { 53 // Ensure that the shader uses at most 16 varying vectors. This counts the number of 54 // vectors assuming that the driver does not perform additional packing. The spec states 55 // that the driver should pack varyings, however, on some Adreno 3xx devices we have 56 // observed that this is not the case. See bug 1695912. 57 let varying_vectors = validator.get_num_unpacked_varying_vectors(); 58 let max_varying_vectors = 16; 59 assert!( 60 varying_vectors <= max_varying_vectors, 61 "Shader {} uses {} varying vectors. Max allowed {}", 62 name, varying_vectors, max_varying_vectors 63 ); 64 65 println!("Shader translated succesfully: {}", name); 66 } 67 Err(_) => { 68 panic!( 69 "Shader compilation failed: {}\n{}", 70 name, 71 validator.info_log() 72 ); 73 } 74 } 75 }