Skip to content

Commit

Permalink
Merge branch 'master' into sub-after-preprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
robtfm committed Jul 17, 2023
2 parents 6918cf3 + 8875c76 commit eebe5f0
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/compose/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ impl Composer {
let mut item_substituted_source = String::default();
let mut current_word = String::default();
let mut line_is_directive = None;
let mut is_valid_import_substitution_point = true;

for char in substituted_source.chars() {
if !current_word.is_empty() {
Expand All @@ -497,7 +498,7 @@ impl Composer {
let mut output = &current_word;

// substitute current word if we are not writing a directive (e.g. `#import xyz`)
if line_is_directive != Some(true) {
if is_valid_import_substitution_point {
if let Some(replacement) = imported_items.get(&current_word) {
output = replacement;
}
Expand Down Expand Up @@ -526,6 +527,14 @@ impl Composer {
current_word.push(char);
} else {
item_substituted_source.push(char);
// we should only substitute global names
// '.' -> avoid substituting members with name == import item
// '@' -> avoid substituting annotations
if char == '.' || char == '@' {
is_valid_import_substitution_point = false;
} else {
is_valid_import_substitution_point = !line_is_directive.unwrap_or(false);
}
}
}
substituted_source = item_substituted_source;
Expand Down
40 changes: 40 additions & 0 deletions src/compose/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,46 @@ mod test {
output_eq!(wgsl, "tests/expected/dup_struct_import.txt");
}

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

composer
.add_composable_module(ComposableModuleDescriptor {
source: include_str!("tests/item_sub_point/mod.wgsl"),
file_path: "tests/item_sub_point/mod.wgsl",
..Default::default()
})
.unwrap();

let module = composer
.make_naga_module(NagaModuleDescriptor {
source: include_str!("tests/item_sub_point/top.wgsl"),
file_path: "tests/item_sub_point/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("item_sub_point.txt").unwrap();
// f.write_all(wgsl.as_bytes()).unwrap();
// drop(f);

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

#[test]
fn conditional_import() {
let mut composer = Composer::default();
Expand Down
17 changes: 17 additions & 0 deletions src/compose/tests/expected/item_sub_point.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
struct _naga_oil_mod_NVXWI_memberFrag {
fragment: f32,
}

fn _naga_oil_mod_NVXWI_memberfragment(f_1: _naga_oil_mod_NVXWI_memberFrag) -> f32 {
return (f_1.fragment * 2.0);
}

@fragment
fn main() -> @location(0) f32 {
var f: _naga_oil_mod_NVXWI_memberFrag;

f.fragment = 3.0;
let _e3: _naga_oil_mod_NVXWI_memberFrag = f;
let _e4: f32 = _naga_oil_mod_NVXWI_memberfragment(_e3);
return _e4;
}
9 changes: 9 additions & 0 deletions src/compose/tests/item_sub_point/mod.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#define_import_path mod

struct Frag {
fragment: f32,
}

fn fragment(f: Frag) -> f32 {
return f.fragment * 2.0;
}
8 changes: 8 additions & 0 deletions src/compose/tests/item_sub_point/top.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#import mod Frag, fragment

@fragment
fn main() -> @location(0) f32 {
var f: Frag;
f.fragment = 3.0;
return fragment(f);
}

0 comments on commit eebe5f0

Please sign in to comment.