Skip to content

Commit

Permalink
Add 'glsl' feature to gate naga glsl features
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
  • Loading branch information
Aaron1011 committed Sep 23, 2023
1 parent 58e2272 commit 29d5a5d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 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
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions src/compose/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,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 @@ -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 }| {
Expand All @@ -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}");
}
Expand Down
19 changes: 17 additions & 2 deletions src/compose/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,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 @@ -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<String, ComposerErrorInner> {
// TODO: cache headers again
let info =
Expand All @@ -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 {
Expand Down Expand Up @@ -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"),
};

Expand Down Expand Up @@ -737,6 +743,7 @@ impl Composer {
},
}
})?,
#[cfg(feature = "glsl")]
ShaderLanguage::Glsl => naga::front::glsl::Frontend::default()
.parse(
&naga::front::glsl::Options {
Expand Down Expand Up @@ -774,12 +781,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 @@ -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)?;
Expand Down Expand Up @@ -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,
};
Expand Down
6 changes: 6 additions & 0 deletions src/compose/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -351,6 +352,7 @@ mod test {
let _ = wgsl;
}

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

0 comments on commit 29d5a5d

Please sign in to comment.