Skip to content

Commit

Permalink
Upgrade to WGPU 22 (#98)
Browse files Browse the repository at this point in the history
This currently doesn't pass the tests, I think because `naga` removed
the `PartialEq` implementation on a bunch of types
(gfx-rs/wgpu#5818)

---------

Co-authored-by: Elabajaba <[email protected]>
  • Loading branch information
callym and Elabajaba authored Aug 3, 2024
1 parent 7031ca1 commit 17a076f
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 86 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ prune = []
allow_deprecated = []

[dependencies]
naga = { version = "0.20", features = ["wgsl-in", "wgsl-out"] }
naga = { version = "22.1", features = ["wgsl-in", "wgsl-out"] }
tracing = "0.1"
regex = "1.8"
regex-syntax = "0.8"
Expand All @@ -31,6 +31,6 @@ once_cell = "1.17.0"
indexmap = "2"

[dev-dependencies]
wgpu = { version = "0.20", features = ["naga-ir"] }
wgpu = { version = "22", features = ["naga-ir"] }
futures-lite = "1"
tracing-subscriber = { version = "0.3", features = ["std", "fmt"] }
6 changes: 2 additions & 4 deletions src/compose/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ impl ErrSource {
let Ok(PreprocessOutput {
preprocessed_source: source,
..
}) = composer
.preprocessor
.preprocess(raw_source, defs, composer.validate)
}) = composer.preprocessor.preprocess(raw_source, defs)
else {
return Default::default();
};
Expand Down Expand Up @@ -78,7 +76,7 @@ pub enum ComposerErrorInner {
WgslParseError(naga::front::wgsl::ParseError),
#[cfg(feature = "glsl")]
#[error("{0:?}")]
GlslParseError(naga::front::glsl::ParseError),
GlslParseError(naga::front::glsl::ParseErrors),
#[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")]
Expand Down
37 changes: 7 additions & 30 deletions src/compose/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ use indexmap::IndexMap;
/// - a module `a` containing a function `f`,
/// - a module `b` that imports `a`, and containing an `override a::f` function,
/// - a module `c` that imports `a` and `b`, and containing an `override a::f` function,
///
/// then b and c both specify an override for `a::f`.
/// the `override fn a::f` declared in module `b` may call to `a::f` within its body.
/// the `override fn a::f` declared in module 'c' may call to `a::f` within its body, but the call will be redirected to `b::f`.
Expand Down Expand Up @@ -126,10 +127,7 @@ use indexmap::IndexMap;
///
/// codespan reporting for errors is available using the error `emit_to_string` method. this requires validation to be enabled, which is true by default. `Composer::non_validating()` produces a non-validating composer that is not able to give accurate error reporting.
///
use naga::{
valid::{Capabilities, ShaderStages},
EntryPoint,
};
use naga::EntryPoint;
use regex::Regex;
use std::collections::{hash_map::Entry, BTreeMap, HashMap, HashSet};
use tracing::{debug, trace};
Expand Down Expand Up @@ -321,11 +319,6 @@ pub struct Composer {
pub module_sets: HashMap<String, ComposableModuleDefinition>,
pub module_index: HashMap<usize, String>,
pub capabilities: naga::valid::Capabilities,
/// The shader stages that the subgroup operations are valid for.
/// Used when creating a validator for the module.
/// See https://github.com/gfx-rs/wgpu/blob/d9c054c645af0ea9ef81617c3e762fbf0f3fecda/wgpu-core/src/device/mod.rs#L515
/// for how to set this for proper subgroup ops support.
pub subgroup_stages: ShaderStages,
preprocessor: Preprocessor,
check_decoration_regex: Regex,
undecorate_regex: Regex,
Expand All @@ -347,7 +340,6 @@ impl Default for Composer {
Self {
validate: true,
capabilities: Default::default(),
subgroup_stages: ShaderStages::empty(),
module_sets: Default::default(),
module_index: Default::default(),
preprocessor: Preprocessor::default(),
Expand Down Expand Up @@ -426,19 +418,9 @@ impl Composer {
String::from_utf8(data_encoding::BASE32_NOPAD.decode(from.as_bytes()).unwrap()).unwrap()
}

/// This creates a validator that properly detects subgroup support.
/// Shorthand for creating a naga validator.
fn create_validator(&self) -> naga::valid::Validator {
let subgroup_operations = if self.capabilities.contains(Capabilities::SUBGROUP) {
use naga::valid::SubgroupOperationSet as S;
S::BASIC | S::VOTE | S::ARITHMETIC | S::BALLOT | S::SHUFFLE | S::SHUFFLE_RELATIVE
} else {
naga::valid::SubgroupOperationSet::empty()
};
let mut validator =
naga::valid::Validator::new(naga::valid::ValidationFlags::all(), self.capabilities);
validator.subgroup_stages(self.subgroup_stages);
validator.subgroup_operations(subgroup_operations);
validator
naga::valid::Validator::new(naga::valid::ValidationFlags::all(), self.capabilities)
}

fn undecorate(&self, string: &str) -> String {
Expand Down Expand Up @@ -1342,7 +1324,7 @@ impl Composer {
imports,
} = self
.preprocessor
.preprocess(&module_set.sanitized_source, shader_defs, self.validate)
.preprocess(&module_set.sanitized_source, shader_defs)
.map_err(|inner| ComposerError {
inner,
source: ErrSource::Module {
Expand Down Expand Up @@ -1435,15 +1417,10 @@ impl Composer {
/// purges any existing modules
/// See https://github.com/gfx-rs/wgpu/blob/d9c054c645af0ea9ef81617c3e762fbf0f3fecda/wgpu-core/src/device/mod.rs#L515
/// for how to set the subgroup_stages value.
pub fn with_capabilities(
self,
capabilities: naga::valid::Capabilities,
subgroup_stages: naga::valid::ShaderStages,
) -> Self {
pub fn with_capabilities(self, capabilities: naga::valid::Capabilities) -> Self {
Self {
capabilities,
validate: self.validate,
subgroup_stages,
..Default::default()
}
}
Expand Down Expand Up @@ -1697,7 +1674,7 @@ impl Composer {
imports,
} = self
.preprocessor
.preprocess(&sanitized_source, &shader_defs, self.validate)
.preprocess(&sanitized_source, &shader_defs)
.map_err(|inner| ComposerError {
inner,
source: ErrSource::Constructing {
Expand Down
49 changes: 8 additions & 41 deletions src/compose/preprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,13 @@ impl Preprocessor {
&self,
shader_str: &str,
shader_defs: &HashMap<String, ShaderDefValue>,
validate_len: bool,
) -> Result<PreprocessOutput, ComposerErrorInner> {
let mut declared_imports = IndexMap::new();
let mut used_imports = IndexMap::new();
let mut scope = Scope::new();
let mut final_string = String::new();
let mut offset = 0;

#[cfg(debug)]
let len = shader_str.len();

// this code broadly stolen from bevy_render::ShaderProcessor
let mut lines = shader_str.lines();
let mut lines = lines.replace_comments().zip(shader_str.lines()).peekable();
Expand Down Expand Up @@ -371,14 +367,6 @@ impl Preprocessor {

scope.finish(offset)?;

#[cfg(debug)]
if validate_len {
let revised_len = final_string.len();
assert_eq!(len, revised_len);
}
#[cfg(not(debug))]
let _ = validate_len;

Ok(PreprocessOutput {
preprocessed_source: final_string,
imports: used_imports.into_values().collect(),
Expand Down Expand Up @@ -576,7 +564,6 @@ fn vertex(
let result_missing = processor.preprocess(
WGSL,
&[("TEXTURE".to_owned(), ShaderDefValue::Bool(true))].into(),
true,
);

let expected: Result<Preprocessor, ComposerErrorInner> =
Expand Down Expand Up @@ -677,7 +664,6 @@ fn vertex(
.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Int(3))].into(),
true,
)
.unwrap();
assert_eq!(result_eq.preprocessed_source, EXPECTED_EQ);
Expand All @@ -686,12 +672,11 @@ fn vertex(
.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Int(7))].into(),
true,
)
.unwrap();
assert_eq!(result_neq.preprocessed_source, EXPECTED_NEQ);

let result_missing = processor.preprocess(WGSL, &Default::default(), true);
let result_missing = processor.preprocess(WGSL, &Default::default());

let expected_err: Result<
(Option<String>, String, Vec<ImportDefWithOffset>),
Expand All @@ -705,7 +690,6 @@ fn vertex(
let result_wrong_type = processor.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Bool(true))].into(),
true,
);

let expected_err: Result<
Expand Down Expand Up @@ -814,7 +798,6 @@ fn vertex(
.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Bool(true))].into(),
true,
)
.unwrap();
assert_eq!(result_eq.preprocessed_source, EXPECTED_EQ);
Expand All @@ -823,7 +806,6 @@ fn vertex(
.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Bool(false))].into(),
true,
)
.unwrap();
assert_eq!(result_neq.preprocessed_source, EXPECTED_NEQ);
Expand Down Expand Up @@ -919,7 +901,6 @@ fn vertex(
.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Bool(true))].into(),
true,
)
.unwrap();
assert_eq!(result_eq.preprocessed_source, EXPECTED_EQ);
Expand All @@ -928,12 +909,11 @@ fn vertex(
.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Bool(false))].into(),
true,
)
.unwrap();
assert_eq!(result_neq.preprocessed_source, EXPECTED_NEQ);

let result_missing = processor.preprocess(WGSL, &[].into(), true);
let result_missing = processor.preprocess(WGSL, &[].into());
let expected_err: Result<
(Option<String>, String, Vec<ImportDefWithOffset>),
ComposerErrorInner,
Expand All @@ -946,7 +926,6 @@ fn vertex(
let result_wrong_type = processor.preprocess(
WGSL,
&[("TEXTURE".to_string(), ShaderDefValue::Int(7))].into(),
true,
);

let expected_err: Result<
Expand Down Expand Up @@ -1031,7 +1010,6 @@ fn vertex(
("SECOND_VALUE".to_string(), ShaderDefValue::Int(3)),
]
.into(),
true,
)
.unwrap();
assert_eq!(result.preprocessed_source, EXPECTED_REPLACED);
Expand Down Expand Up @@ -1060,7 +1038,7 @@ defined
..
} = processor.get_preprocessor_metadata(&WGSL, true).unwrap();
println!("defines: {:?}", shader_defs);
let result = processor.preprocess(&WGSL, &shader_defs, true).unwrap();
let result = processor.preprocess(&WGSL, &shader_defs).unwrap();
assert_eq!(result.preprocessed_source, EXPECTED);
}

Expand Down Expand Up @@ -1103,7 +1081,7 @@ bool: false
..
} = processor.get_preprocessor_metadata(&WGSL, true).unwrap();
println!("defines: {:?}", shader_defs);
let result = processor.preprocess(&WGSL, &shader_defs, true).unwrap();
let result = processor.preprocess(&WGSL, &shader_defs).unwrap();
assert_eq!(result.preprocessed_source, EXPECTED);
}

Expand Down Expand Up @@ -1135,9 +1113,7 @@ fn vertex(
}
";
let processor = Preprocessor::default();
let result = processor
.preprocess(&WGSL_ELSE_IFDEF, &[].into(), true)
.unwrap();
let result = processor.preprocess(&WGSL_ELSE_IFDEF, &[].into()).unwrap();
assert_eq!(
result
.preprocessed_source
Expand Down Expand Up @@ -1214,7 +1190,7 @@ fn vertex(
";
let processor = Preprocessor::default();
let result = processor
.preprocess(&WGSL_ELSE_IFDEF_NO_ELSE_FALLBACK, &[].into(), true)
.preprocess(&WGSL_ELSE_IFDEF_NO_ELSE_FALLBACK, &[].into())
.unwrap();
assert_eq!(
result
Expand Down Expand Up @@ -1265,7 +1241,6 @@ fn vertex(
.preprocess(
&WGSL_ELSE_IFDEF,
&[("TEXTURE".to_string(), ShaderDefValue::Bool(true))].into(),
true,
)
.unwrap();
assert_eq!(
Expand Down Expand Up @@ -1314,7 +1289,6 @@ fn vertex(
.preprocess(
&WGSL_ELSE_IFDEF,
&[("SECOND_TEXTURE".to_string(), ShaderDefValue::Bool(true))].into(),
true,
)
.unwrap();
assert_eq!(
Expand Down Expand Up @@ -1363,7 +1337,6 @@ fn vertex(
.preprocess(
&WGSL_ELSE_IFDEF,
&[("THIRD_TEXTURE".to_string(), ShaderDefValue::Bool(true))].into(),
true,
)
.unwrap();
assert_eq!(
Expand Down Expand Up @@ -1416,7 +1389,6 @@ fn vertex(
("THIRD_TEXTURE".to_string(), ShaderDefValue::Bool(true)),
]
.into(),
true,
)
.unwrap();
assert_eq!(
Expand Down Expand Up @@ -1471,7 +1443,6 @@ fn vertex(
.preprocess(
&WGSL_COMPLICATED_ELSE_IFDEF,
&[("IS_DEFINED".to_string(), ShaderDefValue::Bool(true))].into(),
true,
)
.unwrap();
assert_eq!(
Expand Down Expand Up @@ -1504,7 +1475,7 @@ fail 3

const EXPECTED: &str = r"ok";
let processor = Preprocessor::default();
let result = processor.preprocess(&INPUT, &[].into(), true).unwrap();
let result = processor.preprocess(&INPUT, &[].into()).unwrap();
assert_eq!(
result
.preprocessed_source
Expand Down Expand Up @@ -1536,11 +1507,7 @@ fail 3
const EXPECTED: &str = r"ok";
let processor = Preprocessor::default();
let result = processor
.preprocess(
&INPUT,
&[("x".to_owned(), ShaderDefValue::Int(2))].into(),
true,
)
.preprocess(&INPUT, &[("x".to_owned(), ShaderDefValue::Int(2))].into())
.unwrap();
assert_eq!(
result
Expand Down
1 change: 1 addition & 0 deletions src/compose/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,7 @@ mod test {
module: &shader_module,
entry_point: "run_test",
compilation_options: Default::default(),
cache: None,
});

let bindgroup = device.create_bind_group(&BindGroupDescriptor {
Expand Down
8 changes: 4 additions & 4 deletions src/compose/tests/expected/err_validation_1.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: failed to build a valid final module: Function [2] 'func' is invalid
error: failed to build a valid final module: Function [1] 'func' is invalid
┌─ tests/error_test/wgsl_valid_err.wgsl:7:1
7 │ ╭ fn func() -> f32 {
8 │ │ return 1u;
│ │ ^^ naga::Expression [1]
│ ╰──────────────^ naga::Function [2]
│ │ ^^ naga::Expression [0]
│ ╰──────────────^ naga::Function [1]
= The `return` value Some([1]) does not match the function return value
= The `return` value Some([0]) does not match the function return value

8 changes: 4 additions & 4 deletions src/compose/tests/expected/err_validation_2.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: failed to build a valid final module: Function [1] 'valid_inc::func' is invalid
error: failed to build a valid final module: Function [0] 'valid_inc::func' is invalid
┌─ tests/error_test/wgsl_valid_err.wgsl:7:1
7 │ ╭ fn func() -> f32 {
8 │ │ return 1u;
│ │ ^^ naga::Expression [1]
│ ╰──────────────^ naga::Function [1]
│ │ ^^ naga::Expression [0]
│ ╰──────────────^ naga::Function [0]
= The `return` value Some([1]) does not match the function return value
= The `return` value Some([0]) does not match the function return value

Loading

0 comments on commit 17a076f

Please sign in to comment.