-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathdeclare.test.ts
More file actions
108 lines (80 loc) · 2.89 KB
/
Copy pathdeclare.test.ts
File metadata and controls
108 lines (80 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { describe, expect, it } from 'vitest';
import { d, tgpu } from 'typegpu';
describe('tgpu.declare', () => {
it('should inject provided declaration when resolving a function', () => {
const declaration = tgpu['~unstable'].declare('@group(0) @binding(0) var<uniform> val: f32;');
const empty = tgpu.fn([])`() { /* do nothing */ }`.$uses({ declaration });
expect(tgpu.resolve([empty])).toMatchInlineSnapshot(`
"@group(0) @binding(0) var<uniform> val: f32;
fn empty() { /* do nothing */ }"
`);
});
it('should replace declaration statement in raw wgsl', () => {
const declaration = tgpu['~unstable'].declare('@group(0) @binding(0) var<uniform> val: f32;');
const empty = tgpu.fn([])`() { declaration }`.$uses({ declaration });
expect(tgpu.resolve([empty])).toMatchInlineSnapshot(`
"@group(0) @binding(0) var<uniform> val: f32;
fn empty() { }"
`);
});
it('should inject all provided declarations', () => {
const decl1 = tgpu['~unstable'].declare('@group(0) @binding(0) var<uniform> val: f32;');
const decl2 = tgpu['~unstable'].declare(`\
struct Output {
x: u32,
}`);
const empty = tgpu.fn([])`() { /* do nothing */ }`.$uses({ decl1, decl2 });
expect(tgpu.resolve([empty])).toMatchInlineSnapshot(`
"@group(0) @binding(0) var<uniform> val: f32;
struct Output {
x: u32,
}
fn empty() { /* do nothing */ }"
`);
});
it('should replace nested declarations', () => {
const declaration = tgpu['~unstable']
.declare('@group(0) @binding(0) var<uniform> val: f32;')
.$uses({
nested: tgpu['~unstable'].declare('struct Output { x: u32 }'),
});
const empty = tgpu.fn([])`() { /* do nothing */ }`.$uses({ declaration });
expect(tgpu.resolve([empty])).toMatchInlineSnapshot(`
"struct Output { x: u32 }
@group(0) @binding(0) var<uniform> val: f32;
fn empty() { /* do nothing */ }"
`);
});
it('should resolve declaration with its own externals', () => {
const Output = d.struct({
x: d.u32,
});
const declaration = tgpu['~unstable']
.declare('@group(0) @binding(0) var<uniform> val: Output;')
.$uses({ Output });
const empty = tgpu.fn([])`() { /* do nothing */ }`.$uses({ declaration });
expect(tgpu.resolve([empty])).toMatchInlineSnapshot(`
"struct Output {
x: u32,
}
@group(0) @binding(0) var<uniform> val: Output;
fn empty() { /* do nothing */ }"
`);
});
it('works with TGSL functions', () => {
const declaration = tgpu['~unstable'].declare('@group(0) @binding(0) var<uniform> val: f32;');
const main = tgpu.fn(
[],
d.f32,
)(() => {
declaration;
return 2;
});
expect(tgpu.resolve([main])).toMatchInlineSnapshot(`
"@group(0) @binding(0) var<uniform> val: f32;
fn main() -> f32 {
return 2f;
}"
`);
});
});