/
angle_shader_validation.rs
161 lines (145 loc) · 4.18 KB
/
angle_shader_validation.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
extern crate angle;
extern crate webrender;
use angle::hl::{BuiltInResources, Output, ShaderSpec, ShaderValidator};
// from glslang
const FRAGMENT_SHADER: u32 = 0x8B30;
const VERTEX_SHADER: u32 = 0x8B31;
struct Shader {
name: &'static str,
features: &'static [&'static str],
}
const SHADER_PREFIX: &str = "#define WR_MAX_VERTEX_TEXTURE_WIDTH 1024\n";
const CLIP_FEATURES: &[&str] = &["TRANSFORM"];
const CACHE_FEATURES: &[&str] = &[""];
const PRIM_FEATURES: &[&str] = &["", "TRANSFORM"];
const SHADERS: &[Shader] = &[
// Clip mask shaders
Shader {
name: "cs_clip_rectangle",
features: CLIP_FEATURES,
},
Shader {
name: "cs_clip_image",
features: CLIP_FEATURES,
},
Shader {
name: "cs_clip_border",
features: CLIP_FEATURES,
},
// Cache shaders
Shader {
name: "cs_blur",
features: CACHE_FEATURES,
},
Shader {
name: "cs_text_run",
features: CACHE_FEATURES,
},
// Prim shaders
Shader {
name: "ps_border_corner",
features: PRIM_FEATURES,
},
Shader {
name: "ps_border_edge",
features: PRIM_FEATURES,
},
Shader {
name: "ps_hardware_composite",
features: PRIM_FEATURES,
},
Shader {
name: "ps_split_composite",
features: PRIM_FEATURES,
},
Shader {
name: "ps_image",
features: PRIM_FEATURES,
},
Shader {
name: "ps_text_run",
features: PRIM_FEATURES,
},
// Brush shaders
Shader {
name: "brush_yuv_image",
features: &["", "YUV_NV12", "YUV_PLANAR", "YUV_INTERLEAVED", "YUV_NV12,TEXTURE_RECT"],
},
Shader {
name: "brush_mask",
features: &[],
},
Shader {
name: "brush_solid",
features: &[],
},
Shader {
name: "brush_picture",
features: &["COLOR_TARGET", "ALPHA_TARGET"],
},
Shader {
name: "brush_blend",
features: &[],
},
Shader {
name: "brush_composite",
features: &[],
},
Shader {
name: "brush_line",
features: &[],
},
Shader {
name: "brush_radial_gradient",
features: &[ "DITHERING" ],
},
Shader {
name: "brush_linear_gradient",
features: &[],
},
];
const VERSION_STRING: &str = "#version 300 es\n";
#[test]
fn validate_shaders() {
angle::hl::initialize().unwrap();
let resources = BuiltInResources::default();
let vs_validator =
ShaderValidator::new(VERTEX_SHADER, ShaderSpec::Gles3, Output::Essl, &resources).unwrap();
let fs_validator =
ShaderValidator::new(FRAGMENT_SHADER, ShaderSpec::Gles3, Output::Essl, &resources).unwrap();
for shader in SHADERS {
for config in shader.features {
let mut features = String::new();
features.push_str(SHADER_PREFIX);
for feature in config.split(",") {
features.push_str(&format!("#define WR_FEATURE_{}", feature));
}
let (vs, fs) =
webrender::build_shader_strings(VERSION_STRING, &features, shader.name, &None);
validate(&vs_validator, shader.name, vs);
validate(&fs_validator, shader.name, fs);
}
}
}
fn validate(validator: &ShaderValidator, name: &str, source: String) {
// Check for each `switch` to have a `default`, see
// https://github.com/servo/webrender/wiki/Driver-issues#lack-of-default-case-in-a-switch
assert_eq!(source.matches("switch").count(), source.matches("default:").count(),
"Shader '{}' doesn't have all `switch` covered with `default` cases", name);
// Run Angle validator
match validator.compile_and_translate(&[&source]) {
Ok(_) => {
println!("Shader translated succesfully: {}", name);
}
Err(_) => {
panic!(
"Shader compilation failed: {}\n{}",
name,
validator.info_log()
);
}
}
}