Skip to content

Commit 5ddf33d

Browse files
committed
Enable simplest-shader and mouse-shader on the ash runner.
mouse-shader doesn't work fully. But it's an example, so partial function is still reasonable.
1 parent 7f5e7aa commit 5ddf33d

File tree

2 files changed

+76
-32
lines changed

2 files changed

+76
-32
lines changed

examples/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ The runners:
2020
[wasm](https://webassembly.org/).
2121
- **ash:** runs the shader code on the GPU using
2222
[ash](https://crates.io/crates/ash), a Vulkan wrapper crate for Rust.
23-
- **CPU:** runs the shader code directly on the CPU, using rayon for
24-
parallelism.
23+
- **CPU:** runs the shader code on the CPU, using rayon for parallelism.
2524

2625
Not all shaders work with all runners. The following combinations are
2726
supported.
@@ -52,6 +51,12 @@ supported.
5251
down arrows to adjust the sun's intensity. Use F5 to recompile the shader
5352
code (but note that the image won't redraw afterwards unless the intensity is
5453
adjusted).
54+
- `cargo run --release -p example-runner-ash -- --shader=sky` also runs the sky shader.
55+
- `cargo run --release -p example-runner-ash -- --shader=simplest` runs the simplest shader.
56+
- `cargo run --release -p example-runner-ash -- --shader=mouse` runs the
57+
mouse shader. The click-and-drag functionality is missing and the image
58+
only animates on mouse or keyboard input, but it still is a useful example.
59+
A pull request to fix these shortcomings would be welcome!
5560

5661
- CPU runner:
5762
- `cargo run --release -p example-runner-cpu` runs the sky shader.

examples/runners/ash/src/main.rs

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,51 @@ use std::{
8484
ffi::{CStr, CString},
8585
fs::File,
8686
os::raw::c_char,
87+
path::PathBuf,
8788
sync::mpsc::{TryRecvError, TrySendError, sync_channel},
8889
thread,
8990
};
9091

91-
use clap::Parser;
92+
use clap::{Parser, ValueEnum};
9293

9394
use spirv_builder::{MetadataPrintout, SpirvBuilder};
9495

9596
use shared::ShaderConstants;
9697

98+
// This runner currently doesn't run the `compute` shader example.
99+
#[derive(Debug, PartialEq, Eq, Copy, Clone, ValueEnum)]
100+
pub enum RustGPUShader {
101+
Simplest,
102+
Sky,
103+
Mouse,
104+
}
105+
106+
impl RustGPUShader {
107+
// The form with dashes, e.g. `sky-shader`.
108+
fn crate_name(&self) -> &'static str {
109+
match self {
110+
RustGPUShader::Simplest => "simplest-shader",
111+
RustGPUShader::Sky => "sky-shader",
112+
RustGPUShader::Mouse => "mouse-shader",
113+
}
114+
}
115+
116+
// The form with underscores, e.g. `sky_shader`.
117+
fn crate_ident(&self) -> &'static str {
118+
match self {
119+
RustGPUShader::Simplest => "simplest_shader",
120+
RustGPUShader::Sky => "sky_shader",
121+
RustGPUShader::Mouse => "mouse_shader",
122+
}
123+
}
124+
}
125+
97126
#[derive(Debug, Parser)]
98127
#[command()]
99128
pub struct Options {
129+
#[arg(short, long, default_value = "sky")]
130+
shader: RustGPUShader,
131+
100132
/// Use Vulkan debug layer (requires Vulkan SDK installed)
101133
#[arg(short, long)]
102134
debug_layer: bool,
@@ -115,7 +147,7 @@ pub fn main() {
115147
}
116148

117149
let options = Options::parse();
118-
let shaders = compile_shaders();
150+
let shaders = compile_shaders(&options.shader);
119151

120152
// runtime setup
121153
let event_loop = EventLoop::new().unwrap();
@@ -138,17 +170,19 @@ pub fn main() {
138170
for SpvFile { name, data } in shaders {
139171
ctx.insert_shader_module(name, &data);
140172
}
173+
174+
let crate_ident = options.shader.crate_ident();
141175
ctx.build_pipelines(
142176
vk::PipelineCache::null(),
143177
vec![(
144-
// HACK(eddyb) used to be `module: "sky_shader"` but we need `multimodule`
145-
// for `debugPrintf` instrumentation to work (see `compile_shaders`).
178+
// HACK(eddyb) we need `multimodule` for `debugPrintf`
179+
// instrumentation to work (see `compile_shaders`).
146180
VertexShaderEntryPoint {
147-
module: "sky_shader::main_vs".into(),
181+
module: format!("{crate_ident}::main_vs"),
148182
entry_point: "main_vs".into(),
149183
},
150184
FragmentShaderEntryPoint {
151-
module: "sky_shader::main_fs".into(),
185+
module: format!("{crate_ident}::main_fs"),
152186
entry_point: "main_fs".into(),
153187
},
154188
)],
@@ -203,7 +237,7 @@ pub fn main() {
203237
let compiler_sender = compiler_sender.clone();
204238
thread::spawn(move || {
205239
if let Err(TrySendError::Disconnected(_)) =
206-
compiler_sender.try_send(compile_shaders())
240+
compiler_sender.try_send(compile_shaders(&options.shader))
207241
{
208242
panic!("compiler sender disconnected unexpectedly");
209243
};
@@ -236,29 +270,33 @@ pub fn main() {
236270
.unwrap();
237271
}
238272

239-
pub fn compile_shaders() -> Vec<SpvFile> {
240-
SpirvBuilder::new(
241-
concat!(env!("CARGO_MANIFEST_DIR"), "/../../shaders/sky-shader"),
242-
"spirv-unknown-vulkan1.1",
243-
)
244-
.print_metadata(MetadataPrintout::None)
245-
.shader_panic_strategy(spirv_builder::ShaderPanicStrategy::DebugPrintfThenExit {
246-
print_inputs: true,
247-
print_backtrace: true,
248-
})
249-
// HACK(eddyb) needed because of `debugPrintf` instrumentation limitations
250-
// (see https://github.com/KhronosGroup/SPIRV-Tools/issues/4892).
251-
.multimodule(true)
252-
.build()
253-
.unwrap()
254-
.module
255-
.unwrap_multi()
256-
.iter()
257-
.map(|(name, path)| SpvFile {
258-
name: format!("sky_shader::{name}"),
259-
data: read_spv(&mut File::open(path).unwrap()).unwrap(),
260-
})
261-
.collect()
273+
pub fn compile_shaders(shader: &RustGPUShader) -> Vec<SpvFile> {
274+
let manifest_dir = env!("CARGO_MANIFEST_DIR");
275+
let crate_path = [manifest_dir, "..", "..", "shaders", shader.crate_name()]
276+
.iter()
277+
.copied()
278+
.collect::<PathBuf>();
279+
let crate_ident = shader.crate_ident();
280+
281+
SpirvBuilder::new(crate_path, "spirv-unknown-vulkan1.1")
282+
.print_metadata(MetadataPrintout::None)
283+
.shader_panic_strategy(spirv_builder::ShaderPanicStrategy::DebugPrintfThenExit {
284+
print_inputs: true,
285+
print_backtrace: true,
286+
})
287+
// HACK(eddyb) needed because of `debugPrintf` instrumentation limitations
288+
// (see https://github.com/KhronosGroup/SPIRV-Tools/issues/4892).
289+
.multimodule(true)
290+
.build()
291+
.unwrap()
292+
.module
293+
.unwrap_multi()
294+
.iter()
295+
.map(|(name, path)| SpvFile {
296+
name: format!("{crate_ident}::{name}"),
297+
data: read_spv(&mut File::open(path).unwrap()).unwrap(),
298+
})
299+
.collect()
262300
}
263301

264302
#[derive(Debug)]
@@ -685,6 +723,7 @@ pub struct RenderCtx {
685723
pub recompiling_shaders: bool,
686724
pub start: std::time::Instant,
687725

726+
// Only used for sky-shader.
688727
// NOTE(eddyb) this acts like an integration test for specialization constants.
689728
pub sky_fs_spec_id_0x5007_sun_intensity_extra_spec_const_factor: u32,
690729
}

0 commit comments

Comments
 (0)