Skip to content
This repository was archived by the owner on Jan 29, 2025. It is now read-only.

Commit d59bc3b

Browse files
committed
bench: backends
1 parent 16a818b commit d59bc3b

File tree

1 file changed

+146
-17
lines changed

1 file changed

+146
-17
lines changed

benches/criterion.rs

Lines changed: 146 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn parse_glsl(stage: naga::ShaderStage, inputs: &[Box<[u8]>]) {
3939

4040
fn frontends(c: &mut Criterion) {
4141
let mut group = c.benchmark_group("front");
42-
#[cfg(all(feature = "serialize", feature = "deserialize"))]
42+
#[cfg(all(feature = "wgsl-in", feature = "serialize", feature = "deserialize"))]
4343
group.bench_function("bin", |b| {
4444
let inputs_wgsl = gather_inputs("tests/in", "wgsl");
4545
let mut parser = naga::front::wgsl::Parser::new();
@@ -93,22 +93,25 @@ fn frontends(c: &mut Criterion) {
9393
});
9494
}
9595

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+
}
111112

113+
fn validation(c: &mut Criterion) {
114+
let inputs = gather_modules();
112115
let mut group = c.benchmark_group("valid");
113116
#[cfg(feature = "validate")]
114117
group.bench_function("safe", |b| {
@@ -136,7 +139,133 @@ fn validation(c: &mut Criterion) {
136139
});
137140
}
138141

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+
}
140269

141270
criterion_group!(criterion, frontends, validation, backends,);
142271
criterion_main!(criterion);

0 commit comments

Comments
 (0)