Skip to content

Commit 2ed23d0

Browse files
committed
example-runner-wgpu: add --force-spirv-passthru for testing.
1 parent 4252427 commit 2ed23d0

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

examples/runners/wgpu/src/compute.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pub fn start(options: &Options) {
1212
}
1313

1414
pub async fn start_internal(
15-
_options: &Options,
16-
shader_binary: wgpu::ShaderModuleDescriptor<'static>,
15+
options: &Options,
16+
shader_binary: wgpu::ShaderModuleDescriptorSpirV<'static>,
1717
) {
1818
let backends = wgpu::util::backend_bits_from_env().unwrap_or(wgpu::Backends::PRIMARY);
1919
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
@@ -24,11 +24,16 @@ pub async fn start_internal(
2424
.await
2525
.expect("Failed to find an appropriate adapter");
2626

27+
let mut features = wgpu::Features::TIMESTAMP_QUERY;
28+
if options.force_spirv_passthru {
29+
features |= wgpu::Features::SPIRV_SHADER_PASSTHROUGH;
30+
}
31+
2732
let (device, queue) = adapter
2833
.request_device(
2934
&wgpu::DeviceDescriptor {
3035
label: None,
31-
features: wgpu::Features::TIMESTAMP_QUERY,
36+
features,
3237
limits: wgpu::Limits::default(),
3338
},
3439
None,
@@ -40,8 +45,16 @@ pub async fn start_internal(
4045

4146
let timestamp_period = queue.get_timestamp_period();
4247

43-
// Load the shaders from disk
44-
let module = device.create_shader_module(shader_binary);
48+
// FIXME(eddyb) automate this decision by default.
49+
let module = if options.force_spirv_passthru {
50+
unsafe { device.create_shader_module_spirv(&shader_binary) }
51+
} else {
52+
let wgpu::ShaderModuleDescriptorSpirV { label, source } = shader_binary;
53+
device.create_shader_module(wgpu::ShaderModuleDescriptor {
54+
label,
55+
source: wgpu::ShaderSource::SpirV(source),
56+
})
57+
};
4558

4659
let top = 2u32.pow(20);
4760
let src_range = 1..top;

examples/runners/wgpu/src/graphics.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ fn mouse_button_index(button: MouseButton) -> usize {
3333
}
3434

3535
async fn run(
36-
event_loop: EventLoop<wgpu::ShaderModuleDescriptor<'static>>,
36+
options: Options,
37+
event_loop: EventLoop<wgpu::ShaderModuleDescriptorSpirV<'static>>,
3738
window: Window,
38-
shader_binary: wgpu::ShaderModuleDescriptor<'static>,
39+
shader_binary: wgpu::ShaderModuleDescriptorSpirV<'static>,
3940
) {
4041
let backends = wgpu::util::backend_bits_from_env()
4142
.unwrap_or(wgpu::Backends::VULKAN | wgpu::Backends::METAL);
@@ -69,7 +70,10 @@ async fn run(
6970
.await
7071
.expect("Failed to find an appropriate adapter");
7172

72-
let features = wgpu::Features::PUSH_CONSTANTS;
73+
let mut features = wgpu::Features::PUSH_CONSTANTS;
74+
if options.force_spirv_passthru {
75+
features |= wgpu::Features::SPIRV_SHADER_PASSTHROUGH;
76+
}
7377
let limits = wgpu::Limits {
7478
max_push_constant_size: 128,
7579
..Default::default()
@@ -124,6 +128,7 @@ async fn run(
124128
});
125129

126130
let mut render_pipeline = create_pipeline(
131+
&options,
127132
&device,
128133
&pipeline_layout,
129134
surface_with_config.as_ref().map_or_else(
@@ -309,6 +314,7 @@ async fn run(
309314
}
310315
Event::UserEvent(new_module) => {
311316
*render_pipeline = create_pipeline(
317+
&options,
312318
&device,
313319
&pipeline_layout,
314320
surface_with_config.as_ref().map_or_else(
@@ -326,12 +332,22 @@ async fn run(
326332
}
327333

328334
fn create_pipeline(
335+
options: &Options,
329336
device: &wgpu::Device,
330337
pipeline_layout: &wgpu::PipelineLayout,
331338
surface_format: wgpu::TextureFormat,
332-
shader_binary: wgpu::ShaderModuleDescriptor<'_>,
339+
shader_binary: wgpu::ShaderModuleDescriptorSpirV<'_>,
333340
) -> wgpu::RenderPipeline {
334-
let module = device.create_shader_module(shader_binary);
341+
// FIXME(eddyb) automate this decision by default.
342+
let module = if options.force_spirv_passthru {
343+
unsafe { device.create_shader_module_spirv(&shader_binary) }
344+
} else {
345+
let wgpu::ShaderModuleDescriptorSpirV { label, source } = shader_binary;
346+
device.create_shader_module(wgpu::ShaderModuleDescriptor {
347+
label,
348+
source: wgpu::ShaderSource::SpirV(source),
349+
})
350+
};
335351
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
336352
label: None,
337353
layout: Some(pipeline_layout),
@@ -425,16 +441,17 @@ pub fn start(
425441
})
426442
.expect("couldn't append canvas to document body");
427443
wasm_bindgen_futures::spawn_local(run(
444+
options.clone(),
428445
event_loop,
429446
window,
430447
initial_shader,
431448
));
432449
} else {
433450
futures::executor::block_on(run(
451+
options.clone(),
434452
event_loop,
435453
window,
436454
initial_shader,
437-
438455
));
439456
}
440457
}

examples/runners/wgpu/src/lib.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ pub enum RustGPUShader {
9090
fn maybe_watch(
9191
shader: RustGPUShader,
9292
#[cfg(not(any(target_os = "android", target_arch = "wasm32")))] on_watch: Option<
93-
Box<dyn FnMut(wgpu::ShaderModuleDescriptor<'static>) + Send + 'static>,
93+
Box<dyn FnMut(wgpu::ShaderModuleDescriptorSpirV<'static>) + Send + 'static>,
9494
>,
95-
) -> wgpu::ShaderModuleDescriptor<'static> {
95+
) -> wgpu::ShaderModuleDescriptorSpirV<'static> {
9696
#[cfg(not(any(target_os = "android", target_arch = "wasm32")))]
9797
{
9898
use spirv_builder::{CompileResult, MetadataPrintout, SpirvBuilder};
@@ -128,33 +128,38 @@ fn maybe_watch(
128128
};
129129
fn handle_compile_result(
130130
compile_result: CompileResult,
131-
) -> wgpu::ShaderModuleDescriptor<'static> {
131+
) -> wgpu::ShaderModuleDescriptorSpirV<'static> {
132132
let module_path = compile_result.module.unwrap_single();
133133
let data = std::fs::read(module_path).unwrap();
134+
// FIXME(eddyb) this reallocates all the data pointlessly, there is
135+
// not a good reason to use `ShaderModuleDescriptorSpirV` specifically.
134136
let spirv = Cow::Owned(wgpu::util::make_spirv_raw(&data).into_owned());
135-
wgpu::ShaderModuleDescriptor {
137+
wgpu::ShaderModuleDescriptorSpirV {
136138
label: None,
137-
source: wgpu::ShaderSource::SpirV(spirv),
139+
source: spirv,
138140
}
139141
}
140142
handle_compile_result(initial_result)
141143
}
142144
#[cfg(any(target_os = "android", target_arch = "wasm32"))]
143145
{
144146
match shader {
145-
RustGPUShader::Simplest => wgpu::include_spirv!(env!("simplest_shader.spv")),
146-
RustGPUShader::Sky => wgpu::include_spirv!(env!("sky_shader.spv")),
147-
RustGPUShader::Compute => wgpu::include_spirv!(env!("compute_shader.spv")),
148-
RustGPUShader::Mouse => wgpu::include_spirv!(env!("mouse_shader.spv")),
147+
RustGPUShader::Simplest => wgpu::include_spirv_raw!(env!("simplest_shader.spv")),
148+
RustGPUShader::Sky => wgpu::include_spirv_raw!(env!("sky_shader.spv")),
149+
RustGPUShader::Compute => wgpu::include_spirv_raw!(env!("compute_shader.spv")),
150+
RustGPUShader::Mouse => wgpu::include_spirv_raw!(env!("mouse_shader.spv")),
149151
}
150152
}
151153
}
152154

153-
#[derive(StructOpt)]
155+
#[derive(StructOpt, Clone)]
154156
#[structopt(name = "example-runner-wgpu")]
155157
pub struct Options {
156158
#[structopt(short, long, default_value = "Sky")]
157159
shader: RustGPUShader,
160+
161+
#[structopt(long)]
162+
force_spirv_passthru: bool,
158163
}
159164

160165
#[cfg_attr(target_os = "android", export_name = "android_main")]

0 commit comments

Comments
 (0)