-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathalign.test.ts
More file actions
132 lines (114 loc) · 2.87 KB
/
Copy pathalign.test.ts
File metadata and controls
132 lines (114 loc) · 2.87 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { describe, expect, expectTypeOf, it } from 'vitest';
import { d, tgpu } from 'typegpu';
describe('d.align', () => {
it('adds @align attribute for custom aligned struct members', () => {
const s1 = d.struct({
a: d.u32,
b: d.align(16, d.u32),
c: d.u32,
});
expect(tgpu.resolve([s1])).toContain('@align(16) b: u32,');
});
it('changes alignment of a struct containing aligned member', () => {
expect(
d.alignmentOf(
d.struct({
a: d.u32,
b: d.u32,
c: d.u32,
}),
),
).toBe(4);
expect(
d.alignmentOf(
d.struct({
a: d.u32,
b: d.align(16, d.u32),
c: d.u32,
}),
),
).toBe(16);
});
it('changes size of a struct containing aligned member', () => {
expect(
d.sizeOf(
d.struct({
a: d.u32,
b: d.u32,
c: d.u32,
}),
),
).toBe(12);
expect(
d.sizeOf(
d.struct({
a: d.u32,
b: d.align(16, d.u32),
c: d.u32,
}),
),
).toBe(32);
expect(
d.sizeOf(
d.struct({
a: d.u32,
b: d.align(16, d.u32),
c: d.align(16, d.u32),
}),
),
).toBe(48);
// nested
const FooStruct = d.struct({
a: d.u32,
b: d.struct({
c: d.f32,
d: d.align(16, d.f32),
}),
});
expect(d.sizeOf(FooStruct)).toBe(48);
expect(
d.sizeOf(
d.struct({
a: d.u32,
b: d.align(
32,
d.struct({
c: d.f32,
d: d.align(16, d.f32),
}),
),
}),
),
).toBe(64);
});
it('throws for invalid align values', () => {
expect(() => d.align(11, d.u32)).toThrow();
expect(() => d.align(8, d.vec3f)).toThrow();
expect(() => d.align(-2, d.u32)).toThrow();
});
it('allows aligning loose data without losing the looseness information', () => {
const array = d.arrayOf(d.vec3f, 2);
const alignedArray = d.align(16, array);
const disarray = d.disarrayOf(d.vec3f, 2);
const alignedDisarray = d.align(16, disarray);
expectTypeOf(alignedArray).toEqualTypeOf<d.Decorated<d.WgslArray<d.Vec3f>, [d.Align<16>]>>();
expect(d.isLooseData(alignedArray)).toBe(false);
expectTypeOf(alignedDisarray).toEqualTypeOf<
d.LooseDecorated<d.Disarray<d.Vec3f>, [d.Align<16>]>
>();
expect(d.isLooseData(alignedDisarray)).toBe(true);
});
it('does not allow aligned loose data as non-loose struct members', () => {
const array = d.arrayOf(d.u32, 2);
const alignedArray = d.align(16, array);
const disarray = d.disarrayOf(d.u32, 2);
const alignedDisarray = d.align(16, disarray);
d.struct({
// @ts-expect-error
a: alignedDisarray,
});
d.struct({
a: alignedArray,
});
});
});