Skip to content

Commit

Permalink
Merge pull request bevyengine#7 from robtfm/4-using-imported-consts-i…
Browse files Browse the repository at this point in the history
…n-let-statements-can-fail

rescope imported constants
  • Loading branch information
robtfm authored Oct 26, 2022
2 parents d2814df + 5858856 commit 833c866
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/compose/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,9 +1025,36 @@ impl Composer {
}
}

let module_ir = module_builder.into_module_with_entrypoints();
let mut module_ir = module_builder.into_module_with_entrypoints();
let mut header_ir: naga::Module = header_builder.into();

// rescope any imported constants
let mut renamed_consts = HashMap::new();
for (_, constant) in header_ir.constants.iter_mut() {
if let Some(name) = constant.name.as_mut() {
if name.contains(DECORATION_PRE) && !name.contains(module_decoration) {
let rename = format!("{}{}", module_decoration, name);
debug!(
"{}: header rename {} -> {}",
module_definition.name, name, rename
);
renamed_consts.insert(name.clone(), rename.clone());
*name = rename;
}
}
}
for (_, constant) in module_ir.constants.iter_mut() {
if let Some(name) = constant.name.as_mut() {
if let Some(rename) = renamed_consts.get(name) {
debug!(
"{}: module rename {} -> {}",
module_definition.name, name, rename
);
*name = rename.clone();
}
}
}

let info =
naga::valid::Validator::new(naga::valid::ValidationFlags::all(), self.capabilities)
.validate(&header_ir);
Expand Down
48 changes: 48 additions & 0 deletions src/compose/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,54 @@ mod test {
assert_eq!(test_shader(&mut composer), 2.0);
}

#[test]
fn import_in_decl() {
let mut composer = Composer::default();
composer
.add_composable_module(ComposableModuleDescriptor {
source: include_str!("tests/const_in_decl/consts.wgsl"),
file_path: "tests/const_in_decl/consts.wgsl",
..Default::default()
})
.unwrap();
composer
.add_composable_module(ComposableModuleDescriptor {
source: include_str!("tests/const_in_decl/bind.wgsl"),
file_path: "tests/const_in_decl/bind.wgsl",
..Default::default()
})
.unwrap();
let module = composer
.make_naga_module(NagaModuleDescriptor {
source: include_str!("tests/const_in_decl/top.wgsl"),
file_path: "tests/const_in_decl/top.wgsl",
..Default::default()
})
.unwrap();

println!("{:#?}", module);

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();

// println!("{}", wgsl);
// let mut f = std::fs::File::create("import_in_decl.txt").unwrap();
// f.write_all(wgsl.as_bytes()).unwrap();
// drop(f);

assert_eq!(wgsl, include_str!("tests/expected/import_in_decl.txt"));
}

// actually run a shader and extract the result
// needs the composer to contain a module called "test_module", with a function called "entry_point" returning an f32.
fn test_shader(composer: &mut Composer) -> f32 {
Expand Down
7 changes: 7 additions & 0 deletions src/compose/tests/const_in_decl/bind.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#define_import_path bind

#import consts

let y: u32 = 2u;

var<private> arr: array<u32, consts::X>;
3 changes: 3 additions & 0 deletions src/compose/tests/const_in_decl/consts.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define_import_path consts

let X: u32 = 1u;
6 changes: 6 additions & 0 deletions src/compose/tests/const_in_decl/top.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#import consts
#import bind

fn main() -> f32 {
return f32(bind::arr[0]);
}
13 changes: 13 additions & 0 deletions src/compose/tests/expected/import_in_decl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let _naga_oil__mod__MNXW443UOM__member__X: u32 = 1u;

let _naga_oil__mod__MJUW4ZA__member__y: u32 = 2u;

let _naga_oil__mod__MJUW4ZA__member___naga_oil__mod__MNXW443UOM__member__X: u32 = 1u;

var<private> _naga_oil__mod__MJUW4ZA__member__arr: array<u32,_naga_oil__mod__MJUW4ZA__member___naga_oil__mod__MNXW443UOM__member__X>;

fn main() -> f32 {
let _e6: u32 = _naga_oil__mod__MJUW4ZA__member__arr[0];
return f32(_e6);
}

0 comments on commit 833c866

Please sign in to comment.