Skip to content

Commit 211268a

Browse files
committed
use BitSet for ModuleKey
1 parent 66f25d5 commit 211268a

File tree

6 files changed

+215
-7
lines changed

6 files changed

+215
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ regex-syntax = "0.6"
1818
thiserror = "1.0"
1919
codespan-reporting = "0.11"
2020
data-encoding = "2.3.2"
21+
bit-set = "0.5"
2122

2223
# required for prune only
2324
serde = { version = "1", optional = true }

src/compose/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
///
9797
/// 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.
9898
///
99+
use bit_set::BitSet;
99100
use codespan_reporting::{
100101
diagnostic::{Diagnostic, Label},
101102
files::SimpleFile,
@@ -142,18 +143,18 @@ impl From<ShaderType> for ShaderLanguage {
142143
}
143144
}
144145

145-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
146-
struct ModuleKey(usize);
146+
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
147+
struct ModuleKey(BitSet);
147148

148149
impl ModuleKey {
149150
fn from_members(key: &HashSet<String>, universe: &[String]) -> Self {
150-
ModuleKey(universe.iter().enumerate().fold(0, |acc, (index, item)| {
151+
let mut acc = BitSet::new();
152+
for (index, item) in universe.iter().enumerate() {
151153
if key.contains(item) {
152-
acc + (1 << index)
153-
} else {
154-
acc
154+
acc.insert(index);
155155
}
156-
}))
156+
}
157+
ModuleKey(acc)
157158
}
158159
}
159160

src/compose/test.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,50 @@ mod test {
5454
assert_eq!(wgsl, include_str!("tests/expected/simple_compose.txt"));
5555
}
5656

57+
#[test]
58+
fn big_shaderdefs() {
59+
let mut composer = Composer::default();
60+
61+
composer
62+
.add_composable_module(ComposableModuleDescriptor {
63+
source: include_str!("tests/big_shaderdefs/mod.wgsl"),
64+
file_path: "tests/big_shaderdefs/mod.wgsl",
65+
..Default::default()
66+
})
67+
.unwrap();
68+
69+
let defs = (1..=67).map(|i| format!("a{}", i)).collect::<Vec<String>>();
70+
71+
let module = composer
72+
.make_naga_module(NagaModuleDescriptor {
73+
source: include_str!("tests/big_shaderdefs/top.wgsl"),
74+
file_path: "tests/big_shaderdefs/top.wgsl",
75+
shader_defs: &defs,
76+
..Default::default()
77+
})
78+
.unwrap();
79+
80+
let info = naga::valid::Validator::new(
81+
naga::valid::ValidationFlags::all(),
82+
naga::valid::Capabilities::default(),
83+
)
84+
.validate(&module)
85+
.unwrap();
86+
let wgsl = naga::back::wgsl::write_string(
87+
&module,
88+
&info,
89+
naga::back::wgsl::WriterFlags::EXPLICIT_TYPES,
90+
)
91+
.unwrap();
92+
93+
// println!("{}", wgsl);
94+
// let mut f = std::fs::File::create("big_shaderdefs.txt").unwrap();
95+
// f.write_all(wgsl.as_bytes()).unwrap();
96+
// drop(f);
97+
98+
assert_eq!(wgsl, include_str!("tests/expected/big_shaderdefs.txt"));
99+
}
100+
57101
#[test]
58102
fn duplicate_import() {
59103
let mut composer = Composer::default();
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#define_import_path mod
2+
3+
fn f() -> f32 {
4+
var x = 0.0;
5+
#ifdef a1
6+
#ifdef a2
7+
#ifdef a3
8+
#ifdef a4
9+
#ifdef a5
10+
#ifdef a6
11+
#ifdef a7
12+
#ifdef a8
13+
#ifdef a9
14+
#ifdef a10
15+
#ifdef a11
16+
#ifdef a12
17+
#ifdef a13
18+
#ifdef a14
19+
#ifdef a15
20+
#ifdef a16
21+
#ifdef a17
22+
#ifdef a18
23+
#ifdef a19
24+
#ifdef a20
25+
#ifdef a21
26+
#ifdef a22
27+
#ifdef a23
28+
#ifdef a24
29+
#ifdef a25
30+
#ifdef a26
31+
#ifdef a27
32+
#ifdef a28
33+
#ifdef a29
34+
#ifdef a30
35+
#ifdef a31
36+
#ifdef a32
37+
#ifdef a33
38+
#ifdef a34
39+
#ifdef a35
40+
#ifdef a36
41+
#ifdef a37
42+
#ifdef a38
43+
#ifdef a39
44+
#ifdef a40
45+
#ifdef a41
46+
#ifdef a42
47+
#ifdef a43
48+
#ifdef a44
49+
#ifdef a45
50+
#ifdef a46
51+
#ifdef a47
52+
#ifdef a48
53+
#ifdef a49
54+
#ifdef a50
55+
#ifdef a51
56+
#ifdef a52
57+
#ifdef a53
58+
#ifdef a54
59+
#ifdef a55
60+
#ifdef a56
61+
#ifdef a57
62+
#ifdef a58
63+
#ifdef a59
64+
#ifdef a60
65+
#ifdef a61
66+
#ifdef a62
67+
#ifdef a63
68+
#ifdef a64
69+
#ifdef a65
70+
#ifdef a66
71+
#ifdef a66
72+
#ifdef a67
73+
x = 1.0;
74+
#endif
75+
#endif
76+
#endif
77+
#endif
78+
#endif
79+
#endif
80+
#endif
81+
#endif
82+
#endif
83+
#endif
84+
#endif
85+
#endif
86+
#endif
87+
#endif
88+
#endif
89+
#endif
90+
#endif
91+
#endif
92+
#endif
93+
#endif
94+
#endif
95+
#endif
96+
#endif
97+
#endif
98+
#endif
99+
#endif
100+
#endif
101+
#endif
102+
#endif
103+
#endif
104+
#endif
105+
#endif
106+
#endif
107+
#endif
108+
#endif
109+
#endif
110+
#endif
111+
#endif
112+
#endif
113+
#endif
114+
#endif
115+
#endif
116+
#endif
117+
#endif
118+
#endif
119+
#endif
120+
#endif
121+
#endif
122+
#endif
123+
#endif
124+
#endif
125+
#endif
126+
#endif
127+
#endif
128+
#endif
129+
#endif
130+
#endif
131+
#endif
132+
#endif
133+
#endif
134+
#endif
135+
#endif
136+
#endif
137+
#endif
138+
#endif
139+
#endif
140+
#endif
141+
#endif
142+
return x;
143+
}
144+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#import mod
2+
3+
fn main() -> f32 {
4+
return mod::f();
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn _naga_oil__mod__NVXWI__member__f() -> f32 {
2+
var x: f32 = 0.0;
3+
4+
x = 1.0;
5+
let _e3: f32 = x;
6+
return _e3;
7+
}
8+
9+
fn main() -> f32 {
10+
let _e0: f32 = _naga_oil__mod__NVXWI__member__f();
11+
return _e0;
12+
}
13+

0 commit comments

Comments
 (0)