Skip to content

Commit 2a38e63

Browse files
export WGSL shader source code directly from vello_sparse_shaders (#1016)
# Context Addresses comment #1011 (comment) from @DJMcNab . Replaces `include_str!` with a direct import from `vello_sparse_shaders` to get access to the wgsl source code. ### Changes - Adds a `wgsl` module to the build-time module generated by `vello_sparse_shaders`. This allows `render_strips.wgsl` to be accessed via: `vello_sparse_shaders::wgsl::RENDER_STRIPS` instead of a cross-package `include_str!`. - Adds a `glsl` feature flag to `vello_sparse_shaders` so the default compiled module does not include `glsl` unless the `glsl` feature is used. This makes builds much leaner for wgsl without glsl. ### Test plan Reviewed generated code, and tested both examples manually: - `cargo run_wasm -p wgpu_webgl --release --port 8001` - `cargo run_wasm -p native_webgl --release --port 8000` Finally, CI is very thorough.
1 parent 3085e09 commit 2a38e63

File tree

6 files changed

+51
-21
lines changed

6 files changed

+51
-21
lines changed

sparse_strips/vello_hybrid/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ roxmltree = "0.20.0"
4949

5050
[features]
5151
default = ["wgpu"]
52-
wgpu = ["dep:wgpu"]
53-
webgl = ["dep:js-sys", "dep:web-sys", "dep:vello_sparse_shaders"]
52+
wgpu = ["dep:wgpu", "dep:vello_sparse_shaders"]
53+
webgl = ["dep:js-sys", "dep:web-sys", "dep:vello_sparse_shaders", "vello_sparse_shaders/glsl"]

sparse_strips/vello_hybrid/src/render/wgpu.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,12 @@ impl Programs {
227227

228228
let strip_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
229229
label: Some("Strip Shader"),
230-
source: wgpu::ShaderSource::Wgsl(
231-
include_str!("../../../vello_sparse_shaders/shaders/render_strips.wgsl").into(),
232-
),
230+
source: wgpu::ShaderSource::Wgsl(vello_sparse_shaders::wgsl::RENDER_STRIPS.into()),
233231
});
234232

235233
let clear_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
236234
label: Some("Clear Slots Shader"),
237-
source: wgpu::ShaderSource::Wgsl(
238-
include_str!("../../../vello_sparse_shaders/shaders/clear_slots.wgsl").into(),
239-
),
235+
source: wgpu::ShaderSource::Wgsl(vello_sparse_shaders::wgsl::CLEAR_SLOTS.into()),
240236
});
241237

242238
let strip_pipeline_layout =

sparse_strips/vello_sparse_shaders/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ default-target = "x86_64-unknown-linux-gnu"
1717
targets = []
1818

1919
[dependencies]
20-
naga = { version = "24.0.0", features = ["wgsl-in", "glsl-out"] }
20+
naga = { version = "24.0.0", features = ["wgsl-in", "glsl-out"], optional = true }
2121

2222
[build-dependencies]
23-
naga = { version = "24.0.0", features = ["wgsl-in", "glsl-out"] }
23+
naga = { version = "24.0.0", features = ["wgsl-in", "glsl-out"], optional = true }
24+
25+
[features]
26+
glsl = ["dep:naga"]
2427

2528
[lints]
2629
workspace = true

sparse_strips/vello_sparse_shaders/build.rs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@ use std::fs;
99
use std::path::{Path, PathBuf};
1010

1111
#[allow(warnings)]
12+
#[cfg(feature = "glsl")]
1213
#[path = "src/compile.rs"]
1314
mod compile;
1415
#[allow(warnings)]
16+
#[cfg(feature = "glsl")]
1517
#[path = "src/types.rs"]
1618
mod types;
1719

20+
#[cfg(feature = "glsl")]
1821
use compile::compile_wgsl_shader;
1922

2023
// TODO: Format the generated code via `rustfmt`.
2124
// TODO: Use `quote` instead of string concatenation to generate code.
2225
fn main() {
2326
// Rerun build if the shaders directory changes
2427
println!("cargo:rerun-if-changed=shaders");
25-
2628
let out_dir = env::var_os("OUT_DIR").unwrap();
2729
// Build outputs a `compiled_shaders.rs` module containing the GLSL source and reflection
2830
// metadata.
@@ -63,18 +65,45 @@ fn generate_compiled_shaders_module(buf: &mut String, shader_infos: &[(String, S
6365
"// Generated code by `vello_sparse_shaders` - DO NOT EDIT"
6466
)
6567
.unwrap();
66-
writeln!(
67-
buf,
68-
"/// Build time GLSL shaders derived from wgsl shaders."
69-
)
70-
.unwrap();
68+
69+
writeln!(buf, "/// Re-exporting wgsl shader source code.").unwrap();
70+
71+
writeln!(buf, "pub mod wgsl {{").unwrap();
72+
for (shader_name, shader_source) in shader_infos {
73+
generate_wgsl_shader_module(buf, shader_name, shader_source).unwrap();
74+
}
75+
writeln!(buf, "}}").unwrap();
7176

7277
// Implementation for creating a CompiledGlsl struct per shader assuming the standard entry
7378
// names of `vs_main` and `fs_main`.
74-
for (shader_name, shader_source) in shader_infos {
75-
let compiled = compile_wgsl_shader(shader_source, "vs_main", "fs_main");
79+
#[cfg(feature = "glsl")]
80+
{
81+
writeln!(
82+
buf,
83+
"/// Build time GLSL shaders derived from wgsl shaders."
84+
)
85+
.unwrap();
7686

77-
let generated_code = compiled.to_generated_code(shader_name);
78-
writeln!(buf, "{generated_code}").unwrap();
87+
for (shader_name, shader_source) in shader_infos {
88+
let compiled = compile_wgsl_shader(shader_source, "vs_main", "fs_main");
89+
90+
let generated_code = compiled.to_generated_code(shader_name);
91+
writeln!(buf, "{generated_code}").unwrap();
92+
}
7993
}
8094
}
95+
96+
fn generate_wgsl_shader_module<T: Write>(
97+
buf: &mut T,
98+
shader_name: &str,
99+
shader_source: &str,
100+
) -> std::fmt::Result {
101+
let const_name = shader_name.to_uppercase();
102+
writeln!(buf, " /// Source for `{shader_name}.wgsl`")?;
103+
writeln!(
104+
buf,
105+
" pub const {const_name}: &str = r###\"{shader_source}\"###;"
106+
)?;
107+
108+
Ok(())
109+
}

sparse_strips/vello_sparse_shaders/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
//! This is a utility library to help integrate `vello_hybrid` WebGPU wgsl shaders into glsl.
55
6+
#[cfg(feature = "glsl")]
67
mod compile;
8+
#[cfg(feature = "glsl")]
79
mod types;
810

911
include!(concat!(env!("OUT_DIR"), "/compiled_shaders.rs"));

sparse_strips/vello_sparse_shaders/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl CompiledGlsl {
7979
pub(crate) fn to_generated_code(&self, shader_name: &str) -> String {
8080
let mut code = format!("/// Compiled glsl for `{shader_name}.wgsl`\n");
8181
code.push_str(&format!("pub mod {shader_name} {{\n"));
82-
code.push_str(r#" #![allow(missing_docs, reason="No metadata to generate precise documentation forgenerated code.")]"#);
82+
code.push_str(r#" #![allow(missing_docs, reason="No metadata to generate precise documentation for generated code.")]"#);
8383
code.push_str("\n\n");
8484

8585
code.push_str(" pub const VERTEX_SOURCE: &str = r###\"");

0 commit comments

Comments
 (0)