Skip to content

Commit

Permalink
migrate to naga 0.13 (#40)
Browse files Browse the repository at this point in the history
fixes #33

this should not merged until bevy 0.11.1 is out. builds on @VitalyAnkh's
partial migration, with additionally :

- use `Rc<RefCell>` for `const_expressions` and `const_expr_map` - this
is basically unavoidable, as `DerivedModule::import_expression` needs
access to both the `const_expressions` and the arena to import into,
which may also be `const_expressions`
- don't emit `Expression::Literal` and `Expression::ZeroValue`s
- enforce `const_expression` uniqueness with a custom impl of
`PartialEq` for `Expression`s, to ensure that the uniqueness test for
globals and consts still passes (else we end up with duplicated items,
they no longer test as equal as they refer to different `init`
expressions). this could be removed if the `PartialEq` derive on
`Expression` in naga is made externally available.
- a basic port of the `prune` module which just leaves all
`const_expressions` present

the tests pass, but i haven't tried integrating into bevy (which will
require bevy to use wgpu 0.17).

---------

Co-authored-by: VitalyR <[email protected]>
  • Loading branch information
robtfm and VitalyAnkh authored Aug 26, 2023
1 parent 9344d35 commit 1854567
Show file tree
Hide file tree
Showing 13 changed files with 464 additions and 76 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "naga_oil"
version = "0.8.2"
version = "0.9.0"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "a crate for combining and manipulating shaders using naga IR"
Expand All @@ -15,7 +15,7 @@ prune = []
override_any = []

[dependencies]
naga = { version = "0.12", features = ["wgsl-in", "wgsl-out", "glsl-in", "glsl-out", "clone", "span"] }
naga = { version = "0.13", features = ["wgsl-in", "wgsl-out", "glsl-in", "glsl-out", "clone", "span"] }
tracing = "0.1"
regex = "1.5"
regex-syntax = "0.6"
Expand All @@ -29,6 +29,6 @@ once_cell = "1.17.0"
indexmap = "1.9.3"

[dev-dependencies]
wgpu = { version = "0.16", features=["naga"] }
wgpu = { version = "0.17", features=["naga"] }
futures-lite = "1"
tracing-subscriber = { version = "0.3", features = ["std", "fmt"] }
1 change: 1 addition & 0 deletions src/compose/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ pub mod comment_strip_iter;
pub mod error;
pub mod preprocess;
mod test;
pub mod util;

#[derive(Hash, PartialEq, Eq, Clone, Copy, Debug, Default)]
pub enum ShaderLanguage {
Expand Down
39 changes: 39 additions & 0 deletions src/compose/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,45 @@ mod test {
output_eq!(wgsl, "tests/expected/conditional_import_b.txt");
}

#[test]
fn use_shared_global() {
let mut composer = Composer::default();

composer
.add_composable_module(ComposableModuleDescriptor {
source: include_str!("tests/use_shared_global/mod.wgsl"),
file_path: "tests/use_shared_global/mod.wgsl",
..Default::default()
})
.unwrap();
let module = composer
.make_naga_module(NagaModuleDescriptor {
source: include_str!("tests/use_shared_global/top.wgsl"),
file_path: "tests/use_shared_global/top.wgsl",
..Default::default()
})
.unwrap();

let info = naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
naga::valid::Capabilities::default(),
)
.validate(&module)
.unwrap();
let wgsl = naga::back::wgsl::write_string(
&module,
&info,
naga::back::wgsl::WriterFlags::EXPLICIT_TYPES,
)
.unwrap();

// let mut f = std::fs::File::create("use_shared_global.txt").unwrap();
// f.write_all(wgsl.as_bytes()).unwrap();
// drop(f);

output_eq!(wgsl, "tests/expected/use_shared_global.txt");
}

#[cfg(feature = "test_shader")]
#[test]
fn effective_defs() {
Expand Down
2 changes: 1 addition & 1 deletion src/compose/tests/expected/dup_import.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _naga_oil_mod_MNXW443UOM_memberPI: f32 = 3.0999999046325684;
const _naga_oil_mod_MNXW443UOM_memberPI: f32 = 3.1;

fn _naga_oil_mod_ME_memberf() -> f32 {
return (_naga_oil_mod_MNXW443UOM_memberPI * 1.0);
Expand Down
3 changes: 1 addition & 2 deletions src/compose/tests/expected/import_in_decl.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const _naga_oil_mod_MNXW443UOM_memberX: u32 = 1u;

const _naga_oil_mod_MJUW4ZA_membery: u32 = 2u;

var<private> _naga_oil_mod_MJUW4ZA_memberarr: array<u32,_naga_oil_mod_MNXW443UOM_memberX>;
var<private> _naga_oil_mod_MJUW4ZA_memberarr: array<u32, 1>;

fn main() -> f32 {
let _e2: u32 = _naga_oil_mod_MJUW4ZA_memberarr[0];
Expand Down
1 change: 0 additions & 1 deletion src/compose/tests/expected/item_import_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@




let _e1: u32 = _naga_oil_mod_MNXW443UOM_memberdouble(_naga_oil_mod_MNXW443UOM_memberX);
let _e1: u32 = _naga_oil_mod_MNXW443UOM_memberdouble(_naga_oil_mod_MNXW443UOM_memberY);
return (in * 2u);
Expand Down
15 changes: 15 additions & 0 deletions src/compose/tests/expected/use_shared_global.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var<private> _naga_oil_mod_NVXWI_membera: f32 = 0.0;

fn add() {
let _e2: f32 = _naga_oil_mod_NVXWI_membera;
_naga_oil_mod_NVXWI_membera = (_e2 + 1.0);
return;
}

fn main() -> f32 {
add();
add();
let _e1: f32 = _naga_oil_mod_NVXWI_membera;
return _e1;
}

3 changes: 3 additions & 0 deletions src/compose/tests/use_shared_global/mod.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define_import_path mod

var<private> a: f32 = 0.0;
11 changes: 11 additions & 0 deletions src/compose/tests/use_shared_global/top.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#import mod

fn add() {
mod::a += 1.0;
}

fn main() -> f32 {
add();
add();
return mod::a;
}
Loading

0 comments on commit 1854567

Please sign in to comment.