From 29d5a5d0aea48407ef0e74a0a70c129e790f47aa Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sat, 23 Sep 2023 19:28:13 -0400 Subject: [PATCH 1/2] Add 'glsl' feature to gate naga glsl features 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 --- .github/workflows/rust.yml | 8 ++++++-- Cargo.toml | 5 +++-- src/compose/error.rs | 4 ++++ src/compose/mod.rs | 19 +++++++++++++++++-- src/compose/test.rs | 6 ++++++ 5 files changed, 36 insertions(+), 6 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 39d5f21..8b92d25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,14 +8,15 @@ 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 = [] override_any = [] +glsl = ["naga/glsl-in", "naga/glsl-out"] [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 ec06ae7..58c21d6 100644 --- a/src/compose/error.rs +++ b/src/compose/error.rs @@ -81,10 +81,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}")] @@ -223,6 +225,7 @@ impl ComposerError { .collect(), vec![e.message().to_owned()], ), + #[cfg(feature = "glsl")] ComposerErrorInner::GlslParseError(e) => ( e.iter() .map(|naga::front::glsl::Error { kind, meta }| { @@ -247,6 +250,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 b679aff..5f7ffbf 100644 --- a/src/compose/mod.rs +++ b/src/compose/mod.rs @@ -149,6 +149,7 @@ pub mod util; pub enum ShaderLanguage { #[default] Wgsl, + #[cfg(feature = "glsl")] Glsl, } @@ -156,7 +157,9 @@ pub enum ShaderLanguage { pub enum ShaderType { #[default] Wgsl, + #[cfg(feature = "glsl")] GlslVertex, + #[cfg(feature = "glsl")] GlslFragment, } @@ -164,6 +167,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, } } @@ -548,7 +552,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 = @@ -563,6 +567,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 { @@ -654,6 +659,7 @@ impl Composer { let mut module_string = match language { ShaderLanguage::Wgsl => String::new(), + #[cfg(feature = "glsl")] ShaderLanguage::Glsl => String::from("#version 450\n"), }; @@ -737,6 +743,7 @@ impl Composer { }, } })?, + #[cfg(feature = "glsl")] ShaderLanguage::Glsl => naga::front::glsl::Frontend::default() .parse( &naga::front::glsl::Options { @@ -774,12 +781,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 { @@ -1231,7 +1240,11 @@ impl Composer { if self.validate && create_headers { // check that identifiers haven't been renamed - for language in [ShaderLanguage::Wgsl, ShaderLanguage::Glsl] { + 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)?; @@ -1766,7 +1779,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 bed0462..854b85c 100644 --- a/src/compose/test.rs +++ b/src/compose/test.rs @@ -300,6 +300,7 @@ mod test { output_eq!(text, "tests/expected/missing_import.txt"); } + #[cfg(feature = "glsl")] #[test] fn wgsl_call_glsl() { let mut composer = Composer::default(); @@ -351,6 +352,7 @@ mod test { let _ = wgsl; } + #[cfg(feature = "glsl")] #[test] fn glsl_call_wgsl() { let mut composer = Composer::default(); @@ -391,6 +393,7 @@ mod test { output_eq!(wgsl, "tests/expected/glsl_call_wgsl.txt"); } + #[cfg(feature = "glsl")] #[test] fn basic_glsl() { let mut composer = Composer::default(); @@ -679,6 +682,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(); @@ -720,6 +724,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(); @@ -759,6 +764,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(); From b4722a5e3215842cf510de8822d636d528408ed5 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Wed, 18 Oct 2023 23:51:30 +0100 Subject: [PATCH 2/2] allow single element loop --- src/compose/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/compose/mod.rs b/src/compose/mod.rs index 23668a1..a6514fd 100644 --- a/src/compose/mod.rs +++ b/src/compose/mod.rs @@ -1146,6 +1146,7 @@ impl Composer { if self.validate && create_headers { // check that identifiers haven't been renamed + #[allow(clippy::single_element_loop)] for language in [ ShaderLanguage::Wgsl, #[cfg(feature = "glsl")]