From 24a9d94a9155d5d192750cdf61b3c7dd7fa5401f Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Wed, 18 Oct 2023 18:56:26 -0400 Subject: [PATCH] Add 'glsl' feature to gate naga glsl features (#59) This can be used to reduce the final binary size when a consumer only needs WGSL support. I've enabled the feature by default for backwards-compatibility --------- Co-authored-by: robtfm <50659922+robtfm@users.noreply.github.com> --- .github/workflows/rust.yml | 8 ++++++-- Cargo.toml | 7 ++++--- src/compose/error.rs | 4 ++++ src/compose/mod.rs | 20 ++++++++++++++++++-- src/compose/test.rs | 6 ++++++ 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 11b94c7..ae6997e 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -10,7 +10,9 @@ jobs: - uses: actions/checkout@v2 - uses: dtolnay/rust-toolchain@stable - name: Check - run: cargo check + run: | + cargo check + cargo check --no-default-features check-wasm: name: Check (Wasm) @@ -21,7 +23,9 @@ jobs: with: target: wasm32-unknown-unknown - name: Check wasm - run: cargo check --target wasm32-unknown-unknown + run: | + cargo check --target wasm32-unknown-unknown + cargo check --target wasm32-unknown-unknown --no-default-features test-windows: name: Test Suite (Windows) diff --git a/Cargo.toml b/Cargo.toml index b2a34e2..759529b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,15 +8,16 @@ repository = "https://github.com/bevyengine/naga_oil/" readme = "README.md" [features] -default = ["test_shader"] +default = ["test_shader", "glsl"] # enable tests that need a graphical card test_shader = [] -prune = [] +glsl = ["naga/glsl-in", "naga/glsl-out"] override_any = [] +prune = [] allow_deprecated = [] [dependencies] -naga = { version = "0.13", features = ["wgsl-in", "wgsl-out", "glsl-in", "glsl-out", "clone", "span"] } +naga = { version = "0.13", features = ["wgsl-in", "wgsl-out", "clone", "span"] } tracing = "0.1" regex = "1.8" regex-syntax = "0.7" diff --git a/src/compose/error.rs b/src/compose/error.rs index e6b86d2..2d1fc6e 100644 --- a/src/compose/error.rs +++ b/src/compose/error.rs @@ -76,10 +76,12 @@ pub enum ComposerErrorInner { ImportNotFound(String, usize), #[error("{0}")] WgslParseError(naga::front::wgsl::ParseError), + #[cfg(feature = "glsl")] #[error("{0:?}")] GlslParseError(Vec), #[error("naga_oil bug, please file a report: failed to convert imported module IR back into WGSL for use with WGSL shaders: {0}")] WgslBackError(naga::back::wgsl::Error), + #[cfg(feature = "glsl")] #[error("naga_oil bug, please file a report: failed to convert imported module IR back into GLSL for use with GLSL shaders: {0}")] GlslBackError(naga::back::glsl::Error), #[error("naga_oil bug, please file a report: composer failed to build a valid header: {0}")] @@ -222,6 +224,7 @@ impl ComposerError { .collect(), vec![e.message().to_owned()], ), + #[cfg(feature = "glsl")] ComposerErrorInner::GlslParseError(e) => ( e.iter() .map(|naga::front::glsl::Error { kind, meta }| { @@ -246,6 +249,7 @@ impl ComposerError { ComposerErrorInner::WgslBackError(e) => { return format!("{path}: wgsl back error: {e}"); } + #[cfg(feature = "glsl")] ComposerErrorInner::GlslBackError(e) => { return format!("{path}: glsl back error: {e}"); } diff --git a/src/compose/mod.rs b/src/compose/mod.rs index 36a2070..a6514fd 100644 --- a/src/compose/mod.rs +++ b/src/compose/mod.rs @@ -151,6 +151,7 @@ pub mod util; pub enum ShaderLanguage { #[default] Wgsl, + #[cfg(feature = "glsl")] Glsl, } @@ -158,7 +159,9 @@ pub enum ShaderLanguage { pub enum ShaderType { #[default] Wgsl, + #[cfg(feature = "glsl")] GlslVertex, + #[cfg(feature = "glsl")] GlslFragment, } @@ -166,6 +169,7 @@ impl From for ShaderLanguage { fn from(ty: ShaderType) -> Self { match ty { ShaderType::Wgsl => ShaderLanguage::Wgsl, + #[cfg(feature = "glsl")] ShaderType::GlslVertex | ShaderType::GlslFragment => ShaderLanguage::Glsl, } } @@ -468,7 +472,7 @@ impl Composer { &self, naga_module: &mut naga::Module, language: ShaderLanguage, - header_for: &str, + #[allow(unused)] header_for: &str, // Only used when GLSL is enabled ) -> Result { // TODO: cache headers again let info = @@ -483,6 +487,7 @@ impl Composer { naga::back::wgsl::WriterFlags::EXPLICIT_TYPES, ) .map_err(ComposerErrorInner::WgslBackError), + #[cfg(feature = "glsl")] ShaderLanguage::Glsl => { let vec4 = naga_module.types.insert( naga::Type { @@ -574,6 +579,7 @@ impl Composer { let mut module_string = match language { ShaderLanguage::Wgsl => String::new(), + #[cfg(feature = "glsl")] ShaderLanguage::Glsl => String::from("#version 450\n"), }; @@ -657,6 +663,7 @@ impl Composer { }, } })?, + #[cfg(feature = "glsl")] ShaderLanguage::Glsl => naga::front::glsl::Frontend::default() .parse( &naga::front::glsl::Options { @@ -694,12 +701,14 @@ impl Composer { owned_types: &HashSet, ) -> Result<(), ComposerErrorInner> { // TODO: remove this once glsl front support is complete + #[cfg(feature = "glsl")] if lang == ShaderLanguage::Glsl { return Ok(()); } let recompiled = match lang { ShaderLanguage::Wgsl => naga::front::wgsl::parse_str(header).unwrap(), + #[cfg(feature = "glsl")] ShaderLanguage::Glsl => naga::front::glsl::Frontend::default() .parse( &naga::front::glsl::Options { @@ -1137,7 +1146,12 @@ impl Composer { if self.validate && create_headers { // check that identifiers haven't been renamed - for language in [ShaderLanguage::Wgsl, ShaderLanguage::Glsl] { + #[allow(clippy::single_element_loop)] + for language in [ + ShaderLanguage::Wgsl, + #[cfg(feature = "glsl")] + ShaderLanguage::Glsl, + ] { let header = self .naga_to_string(&mut header_ir, language, &module_definition.name) .map_err(wrap_err)?; @@ -1673,7 +1687,9 @@ impl Composer { Self::add_composable_data(&mut derived, &composable, None, 0, false); let stage = match shader_type { + #[cfg(feature = "glsl")] ShaderType::GlslVertex => Some(naga::ShaderStage::Vertex), + #[cfg(feature = "glsl")] ShaderType::GlslFragment => Some(naga::ShaderStage::Fragment), _ => None, }; diff --git a/src/compose/test.rs b/src/compose/test.rs index a90102e..21908ff 100644 --- a/src/compose/test.rs +++ b/src/compose/test.rs @@ -303,6 +303,7 @@ mod test { output_eq!(text, "tests/expected/missing_import.txt"); } + #[cfg(feature = "glsl")] #[test] fn wgsl_call_glsl() { let mut composer = Composer::default(); @@ -354,6 +355,7 @@ mod test { let _ = wgsl; } + #[cfg(feature = "glsl")] #[test] fn glsl_call_wgsl() { let mut composer = Composer::default(); @@ -394,6 +396,7 @@ mod test { output_eq!(wgsl, "tests/expected/glsl_call_wgsl.txt"); } + #[cfg(feature = "glsl")] #[test] fn basic_glsl() { let mut composer = Composer::default(); @@ -693,6 +696,7 @@ mod test { output_eq!(wgsl, "tests/expected/import_in_decl.txt"); } + #[cfg(feature = "glsl")] #[test] fn glsl_const_import() { let mut composer = Composer::default(); @@ -734,6 +738,7 @@ mod test { output_eq!(wgsl, "tests/expected/glsl_const_import.txt"); } + #[cfg(feature = "glsl")] #[test] fn glsl_wgsl_const_import() { let mut composer = Composer::default(); @@ -773,6 +778,7 @@ mod test { output_eq!(wgsl, "tests/expected/glsl_wgsl_const_import.txt"); } + #[cfg(feature = "glsl")] #[test] fn wgsl_glsl_const_import() { let mut composer = Composer::default();