expose wgsl source code
diff --git a/sparse_strips/vello_hybrid/Cargo.toml b/sparse_strips/vello_hybrid/Cargo.toml index d2d6836..65ce9dc 100644 --- a/sparse_strips/vello_hybrid/Cargo.toml +++ b/sparse_strips/vello_hybrid/Cargo.toml
@@ -49,5 +49,5 @@ [features] default = ["wgpu"] -wgpu = ["dep:wgpu"] -webgl = ["dep:js-sys", "dep:web-sys", "dep:vello_sparse_shaders"] +wgpu = ["dep:wgpu", "dep:vello_sparse_shaders"] +webgl = ["dep:js-sys", "dep:web-sys", "dep:vello_sparse_shaders", "vello_sparse_shaders/glsl"]
diff --git a/sparse_strips/vello_hybrid/src/render/wgpu.rs b/sparse_strips/vello_hybrid/src/render/wgpu.rs index aaa5ebe..d3db90c 100644 --- a/sparse_strips/vello_hybrid/src/render/wgpu.rs +++ b/sparse_strips/vello_hybrid/src/render/wgpu.rs
@@ -227,16 +227,12 @@ let strip_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("Strip Shader"), - source: wgpu::ShaderSource::Wgsl( - include_str!("../../../vello_sparse_shaders/shaders/render_strips.wgsl").into(), - ), + source: wgpu::ShaderSource::Wgsl(vello_sparse_shaders::wgsl::RENDER_STRIPS.into()), }); let clear_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("Clear Slots Shader"), - source: wgpu::ShaderSource::Wgsl( - include_str!("../../../vello_sparse_shaders/shaders/clear_slots.wgsl").into(), - ), + source: wgpu::ShaderSource::Wgsl(vello_sparse_shaders::wgsl::CLEAR_SLOTS.into()), }); let strip_pipeline_layout =
diff --git a/sparse_strips/vello_sparse_shaders/Cargo.toml b/sparse_strips/vello_sparse_shaders/Cargo.toml index 50a54d6..6123c54 100644 --- a/sparse_strips/vello_sparse_shaders/Cargo.toml +++ b/sparse_strips/vello_sparse_shaders/Cargo.toml
@@ -17,10 +17,13 @@ targets = [] [dependencies] -naga = { version = "24.0.0", features = ["wgsl-in", "glsl-out"] } +naga = { version = "24.0.0", features = ["wgsl-in", "glsl-out"], optional = true } [build-dependencies] -naga = { version = "24.0.0", features = ["wgsl-in", "glsl-out"] } +naga = { version = "24.0.0", features = ["wgsl-in", "glsl-out"], optional = true } + +[features] +glsl = ["dep:naga"] [lints] workspace = true
diff --git a/sparse_strips/vello_sparse_shaders/build.rs b/sparse_strips/vello_sparse_shaders/build.rs index fda04fd..90218ef 100644 --- a/sparse_strips/vello_sparse_shaders/build.rs +++ b/sparse_strips/vello_sparse_shaders/build.rs
@@ -9,12 +9,15 @@ use std::path::{Path, PathBuf}; #[allow(warnings)] +#[cfg(feature = "glsl")] #[path = "src/compile.rs"] mod compile; #[allow(warnings)] +#[cfg(feature = "glsl")] #[path = "src/types.rs"] mod types; +#[cfg(feature = "glsl")] use compile::compile_wgsl_shader; // TODO: Format the generated code via `rustfmt`. @@ -22,7 +25,6 @@ fn main() { // Rerun build if the shaders directory changes println!("cargo:rerun-if-changed=shaders"); - let out_dir = env::var_os("OUT_DIR").unwrap(); // Build outputs a `compiled_shaders.rs` module containing the GLSL source and reflection // metadata. @@ -63,18 +65,45 @@ "// Generated code by `vello_sparse_shaders` - DO NOT EDIT" ) .unwrap(); - writeln!( - buf, - "/// Build time GLSL shaders derived from wgsl shaders." - ) - .unwrap(); + + writeln!(buf, "/// Re-exporting wgsl shader source code.").unwrap(); + + writeln!(buf, "pub mod wgsl {{").unwrap(); + for (shader_name, shader_source) in shader_infos { + generate_wgsl_shader_module(buf, shader_name, shader_source).unwrap(); + } + writeln!(buf, "}}").unwrap(); // Implementation for creating a CompiledGlsl struct per shader assuming the standard entry // names of `vs_main` and `fs_main`. - for (shader_name, shader_source) in shader_infos { - let compiled = compile_wgsl_shader(shader_source, "vs_main", "fs_main"); + #[cfg(feature = "glsl")] + { + writeln!( + buf, + "/// Build time GLSL shaders derived from wgsl shaders." + ) + .unwrap(); - let generated_code = compiled.to_generated_code(shader_name); - writeln!(buf, "{generated_code}").unwrap(); + for (shader_name, shader_source) in shader_infos { + let compiled = compile_wgsl_shader(shader_source, "vs_main", "fs_main"); + + let generated_code = compiled.to_generated_code(shader_name); + writeln!(buf, "{generated_code}").unwrap(); + } } } + +fn generate_wgsl_shader_module<T: Write>( + buf: &mut T, + shader_name: &str, + shader_source: &str, +) -> std::fmt::Result { + let const_name = shader_name.to_uppercase(); + writeln!(buf, " /// Source for `{shader_name}.wgsl`")?; + writeln!( + buf, + " pub const {const_name}: &str = r###\"{shader_source}\"###;" + )?; + + Ok(()) +}
diff --git a/sparse_strips/vello_sparse_shaders/src/lib.rs b/sparse_strips/vello_sparse_shaders/src/lib.rs index 75e9ac2..6932063 100644 --- a/sparse_strips/vello_sparse_shaders/src/lib.rs +++ b/sparse_strips/vello_sparse_shaders/src/lib.rs
@@ -3,7 +3,9 @@ //! This is a utility library to help integrate `vello_hybrid` WebGPU wgsl shaders into glsl. +#[cfg(feature = "glsl")] mod compile; +#[cfg(feature = "glsl")] mod types; include!(concat!(env!("OUT_DIR"), "/compiled_shaders.rs"));
diff --git a/sparse_strips/vello_sparse_shaders/src/types.rs b/sparse_strips/vello_sparse_shaders/src/types.rs index 06b3ee0..03fa15c 100644 --- a/sparse_strips/vello_sparse_shaders/src/types.rs +++ b/sparse_strips/vello_sparse_shaders/src/types.rs
@@ -79,7 +79,7 @@ pub(crate) fn to_generated_code(&self, shader_name: &str) -> String { let mut code = format!("/// Compiled glsl for `{shader_name}.wgsl`\n"); code.push_str(&format!("pub mod {shader_name} {{\n")); - code.push_str(r#" #![allow(missing_docs, reason="No metadata to generate precise documentation forgenerated code.")]"#); + code.push_str(r#" #![allow(missing_docs, reason="No metadata to generate precise documentation for generated code.")]"#); code.push_str("\n\n"); code.push_str(" pub const VERTEX_SOURCE: &str = r###\"");