@@ -39,7 +39,7 @@ fn parse_glsl(stage: naga::ShaderStage, inputs: &[Box<[u8]>]) {
39
39
40
40
fn frontends ( c : & mut Criterion ) {
41
41
let mut group = c. benchmark_group ( "front" ) ;
42
- #[ cfg( all( feature = "serialize" , feature = "deserialize" ) ) ]
42
+ #[ cfg( all( feature = "wgsl-in" , feature = " serialize", feature = "deserialize" ) ) ]
43
43
group. bench_function ( "bin" , |b| {
44
44
let inputs_wgsl = gather_inputs ( "tests/in" , "wgsl" ) ;
45
45
let mut parser = naga:: front:: wgsl:: Parser :: new ( ) ;
@@ -93,22 +93,25 @@ fn frontends(c: &mut Criterion) {
93
93
} ) ;
94
94
}
95
95
96
- fn validation ( c : & mut Criterion ) {
97
- #[ cfg( feature = "wgsl-in" ) ]
98
- let inputs = {
99
- let inputs_wgsl = gather_inputs ( "tests/in" , "wgsl" ) ;
100
- let mut parser = naga:: front:: wgsl:: Parser :: new ( ) ;
101
- inputs_wgsl
102
- . iter ( )
103
- . map ( |input| {
104
- let string = std:: str:: from_utf8 ( input) . unwrap ( ) ;
105
- parser. parse ( string) . unwrap ( )
106
- } )
107
- . collect :: < Vec < _ > > ( )
108
- } ;
109
- #[ cfg( not( feature = "wgsl-in" ) ) ]
110
- let inputs: Vec < Box < [ u8 ] > > = Vec :: new ( ) ;
96
+ #[ cfg( feature = "wgsl-in" ) ]
97
+ fn gather_modules ( ) -> Vec < naga:: Module > {
98
+ let inputs = gather_inputs ( "tests/in" , "wgsl" ) ;
99
+ let mut parser = naga:: front:: wgsl:: Parser :: new ( ) ;
100
+ inputs
101
+ . iter ( )
102
+ . map ( |input| {
103
+ let string = std:: str:: from_utf8 ( input) . unwrap ( ) ;
104
+ parser. parse ( string) . unwrap ( )
105
+ } )
106
+ . collect ( )
107
+ }
108
+ #[ cfg( not( feature = "wgsl-in" ) ) ]
109
+ fn gather_modules ( ) -> Vec < naga:: Module > {
110
+ Vec :: new ( )
111
+ }
111
112
113
+ fn validation ( c : & mut Criterion ) {
114
+ let inputs = gather_modules ( ) ;
112
115
let mut group = c. benchmark_group ( "valid" ) ;
113
116
#[ cfg( feature = "validate" ) ]
114
117
group. bench_function ( "safe" , |b| {
@@ -136,7 +139,133 @@ fn validation(c: &mut Criterion) {
136
139
} ) ;
137
140
}
138
141
139
- fn backends ( _c : & mut Criterion ) { }
142
+ fn backends ( c : & mut Criterion ) {
143
+ #[ cfg( feature = "validate" ) ]
144
+ let inputs = {
145
+ let mut validator = naga:: valid:: Validator :: new (
146
+ naga:: valid:: ValidationFlags :: empty ( ) ,
147
+ naga:: valid:: Capabilities :: empty ( ) ,
148
+ ) ;
149
+ let input_modules = gather_modules ( ) ;
150
+ input_modules
151
+ . into_iter ( )
152
+ . flat_map ( |module| validator. validate ( & module) . ok ( ) . map ( |info| ( module, info) ) )
153
+ . collect :: < Vec < _ > > ( )
154
+ } ;
155
+ #[ cfg( not( feature = "validate" ) ) ]
156
+ let inputs = Vec :: < ( naga:: Module , naga:: valid:: ModuleInfo ) > :: new ( ) ;
157
+
158
+ let mut group = c. benchmark_group ( "back" ) ;
159
+ #[ cfg( feature = "wgsl-out" ) ]
160
+ group. bench_function ( "wgsl" , |b| {
161
+ b. iter ( || {
162
+ let mut string = String :: new ( ) ;
163
+ let flags = naga:: back:: wgsl:: WriterFlags :: empty ( ) ;
164
+ for & ( ref module, ref info) in inputs. iter ( ) {
165
+ let mut writer = naga:: back:: wgsl:: Writer :: new ( & mut string, flags) ;
166
+ writer. write ( module, info) . unwrap ( ) ;
167
+ string. clear ( ) ;
168
+ }
169
+ } ) ;
170
+ } ) ;
171
+
172
+ #[ cfg( feature = "spv-out" ) ]
173
+ group. bench_function ( "spv" , |b| {
174
+ b. iter ( || {
175
+ let mut data = Vec :: new ( ) ;
176
+ let options = naga:: back:: spv:: Options :: default ( ) ;
177
+ for & ( ref module, ref info) in inputs. iter ( ) {
178
+ let mut writer = naga:: back:: spv:: Writer :: new ( & options) . unwrap ( ) ;
179
+ writer. write ( module, info, None , & mut data) . unwrap ( ) ;
180
+ data. clear ( ) ;
181
+ }
182
+ } ) ;
183
+ } ) ;
184
+ #[ cfg( feature = "spv-out" ) ]
185
+ group. bench_function ( "spv-separate" , |b| {
186
+ b. iter ( || {
187
+ let mut data = Vec :: new ( ) ;
188
+ let options = naga:: back:: spv:: Options :: default ( ) ;
189
+ for & ( ref module, ref info) in inputs. iter ( ) {
190
+ let mut writer = naga:: back:: spv:: Writer :: new ( & options) . unwrap ( ) ;
191
+ for ep in module. entry_points . iter ( ) {
192
+ let pipeline_options = naga:: back:: spv:: PipelineOptions {
193
+ shader_stage : ep. stage ,
194
+ entry_point : ep. name . clone ( ) ,
195
+ } ;
196
+ writer
197
+ . write ( module, info, Some ( & pipeline_options) , & mut data)
198
+ . unwrap ( ) ;
199
+ data. clear ( ) ;
200
+ }
201
+ }
202
+ } ) ;
203
+ } ) ;
204
+
205
+ #[ cfg( feature = "msl-out" ) ]
206
+ group. bench_function ( "msl" , |b| {
207
+ b. iter ( || {
208
+ let mut string = String :: new ( ) ;
209
+ let options = naga:: back:: msl:: Options :: default ( ) ;
210
+ for & ( ref module, ref info) in inputs. iter ( ) {
211
+ let pipeline_options = naga:: back:: msl:: PipelineOptions :: default ( ) ;
212
+ let mut writer = naga:: back:: msl:: Writer :: new ( & mut string) ;
213
+ writer
214
+ . write ( module, info, & options, & pipeline_options)
215
+ . unwrap ( ) ;
216
+ string. clear ( ) ;
217
+ }
218
+ } ) ;
219
+ } ) ;
220
+
221
+ #[ cfg( feature = "hlsl-out" ) ]
222
+ group. bench_function ( "hlsl" , |b| {
223
+ b. iter ( || {
224
+ let options = naga:: back:: hlsl:: Options :: default ( ) ;
225
+ let mut string = String :: new ( ) ;
226
+ for & ( ref module, ref info) in inputs. iter ( ) {
227
+ let mut writer = naga:: back:: hlsl:: Writer :: new ( & mut string, & options) ;
228
+ let _ = writer. write ( module, info) ; // may fail on unimplemented things
229
+ string. clear ( ) ;
230
+ }
231
+ } ) ;
232
+ } ) ;
233
+
234
+ #[ cfg( feature = "glsl-out" ) ]
235
+ group. bench_function ( "glsl-separate" , |b| {
236
+ b. iter ( || {
237
+ let mut string = String :: new ( ) ;
238
+ let options = naga:: back:: glsl:: Options {
239
+ version : naga:: back:: glsl:: Version :: Embedded ( 320 ) ,
240
+ writer_flags : naga:: back:: glsl:: WriterFlags :: empty ( ) ,
241
+ binding_map : Default :: default ( ) ,
242
+ } ;
243
+ for & ( ref module, ref info) in inputs. iter ( ) {
244
+ for ep in module. entry_points . iter ( ) {
245
+ let pipeline_options = naga:: back:: glsl:: PipelineOptions {
246
+ shader_stage : ep. stage ,
247
+ entry_point : ep. name . clone ( ) ,
248
+ } ;
249
+ match naga:: back:: glsl:: Writer :: new (
250
+ & mut string,
251
+ module,
252
+ info,
253
+ & options,
254
+ & pipeline_options,
255
+ ) {
256
+ Ok ( mut writer) => {
257
+ let _ = writer. write ( ) ; // can error if unsupported
258
+ }
259
+ Err ( _) => {
260
+ // missing features
261
+ }
262
+ } ;
263
+ string. clear ( ) ;
264
+ }
265
+ }
266
+ } ) ;
267
+ } ) ;
268
+ }
140
269
141
270
criterion_group ! ( criterion, frontends, validation, backends, ) ;
142
271
criterion_main ! ( criterion) ;
0 commit comments