Skip to content

Commit

Permalink
Add 'glsl' feature to gate naga glsl features (#59)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Aaron1011 and robtfm authored Oct 18, 2023
1 parent ac56a00 commit 24a9d94
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions src/compose/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ pub enum ComposerErrorInner {
ImportNotFound(String, usize),
#[error("{0}")]
WgslParseError(naga::front::wgsl::ParseError),
#[cfg(feature = "glsl")]
#[error("{0:?}")]
GlslParseError(Vec<naga::front::glsl::Error>),
#[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}")]
Expand Down Expand Up @@ -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 }| {
Expand All @@ -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}");
}
Expand Down
20 changes: 18 additions & 2 deletions src/compose/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,25 @@ pub mod util;
pub enum ShaderLanguage {
#[default]
Wgsl,
#[cfg(feature = "glsl")]
Glsl,
}

#[derive(Hash, PartialEq, Eq, Clone, Copy, Debug, Default)]
pub enum ShaderType {
#[default]
Wgsl,
#[cfg(feature = "glsl")]
GlslVertex,
#[cfg(feature = "glsl")]
GlslFragment,
}

impl From<ShaderType> for ShaderLanguage {
fn from(ty: ShaderType) -> Self {
match ty {
ShaderType::Wgsl => ShaderLanguage::Wgsl,
#[cfg(feature = "glsl")]
ShaderType::GlslVertex | ShaderType::GlslFragment => ShaderLanguage::Glsl,
}
}
Expand Down Expand Up @@ -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<String, ComposerErrorInner> {
// TODO: cache headers again
let info =
Expand All @@ -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 {
Expand Down Expand Up @@ -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"),
};

Expand Down Expand Up @@ -657,6 +663,7 @@ impl Composer {
},
}
})?,
#[cfg(feature = "glsl")]
ShaderLanguage::Glsl => naga::front::glsl::Frontend::default()
.parse(
&naga::front::glsl::Options {
Expand Down Expand Up @@ -694,12 +701,14 @@ impl Composer {
owned_types: &HashSet<String>,
) -> 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 {
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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,
};
Expand Down
6 changes: 6 additions & 0 deletions src/compose/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -354,6 +355,7 @@ mod test {
let _ = wgsl;
}

#[cfg(feature = "glsl")]
#[test]
fn glsl_call_wgsl() {
let mut composer = Composer::default();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 24a9d94

Please sign in to comment.