-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy patharray.test.ts
More file actions
670 lines (555 loc) · 18.2 KB
/
array.test.ts
File metadata and controls
670 lines (555 loc) · 18.2 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
import { attest } from '@ark/attest';
import { BufferReader, BufferWriter } from 'typed-binary';
import { describe, expect, expectTypeOf } from 'vitest';
import { readData, writeData } from '../src/data/dataIO.ts';
import { d, tgpu } from '../src/index.js';
import { namespace } from '../src/core/resolve/namespace.ts';
import { resolve } from '../src/resolutionCtx.ts';
import type { Infer } from '../src/shared/repr.ts';
import { arrayLength } from '../src/std/array.ts';
import { it } from 'typegpu-testing-utility';
describe('array', () => {
it('produces a visually pleasant type', () => {
const TestArray = d.arrayOf(d.vec3u, 3);
attest(TestArray).type.toString.snap('WgslArray<Vec3u>');
});
it('takes element alignment into account when measuring', () => {
const TestArray = d.arrayOf(d.vec3u, 3);
expect(d.sizeOf(TestArray)).toBe(48);
});
it('aligns array elements when writing', () => {
const TestArray = d.arrayOf(d.vec3u, 3);
const buffer = new ArrayBuffer(d.sizeOf(TestArray));
const writer = new BufferWriter(buffer);
writeData(writer, TestArray, [d.vec3u(1, 2, 3), d.vec3u(4, 5, 6), d.vec3u(7, 8, 9)]);
expect([...new Uint32Array(buffer)]).toStrictEqual([1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9, 0]);
});
it('aligns array elements when reading', () => {
const TestArray = d.arrayOf(d.vec3u, 3);
const buffer = new ArrayBuffer(d.sizeOf(TestArray));
const reader = new BufferReader(buffer);
new Uint32Array(buffer).set([1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9, 0]);
expect(readData(reader, TestArray)).toStrictEqual([
d.vec3u(1, 2, 3),
d.vec3u(4, 5, 6),
d.vec3u(7, 8, 9),
]);
});
it('encodes and decodes arrays properly', () => {
const TestArray = d.arrayOf(d.vec3f, 5);
const buffer = new ArrayBuffer(d.sizeOf(TestArray));
const value: Infer<typeof TestArray> = [
d.vec3f(1.5, 2, 3.5),
d.vec3f(),
d.vec3f(-1.5, 2, 3.5),
d.vec3f(1.5, -2, 3.5),
d.vec3f(1.5, 2, 15),
];
writeData(new BufferWriter(buffer), TestArray, value);
expect(readData(new BufferReader(buffer), TestArray)).toStrictEqual(value);
});
it('throws when trying to read/write a runtime-sized array', () => {
const TestArray = d.arrayOf(d.vec3f, 0);
expect(d.sizeOf(TestArray)).toBeNaN();
expect(() =>
writeData(new BufferWriter(new ArrayBuffer(0)), TestArray, [d.vec3f(), d.vec3f()]),
).toThrow();
expect(() => readData(new BufferReader(new ArrayBuffer(0)), TestArray)).toThrow();
const opts = { namespace: namespace({ names: 'strict' }) };
expect(resolve(TestArray, opts).code).toContain('array<vec3f>');
});
it('throws when trying to nest runtime sized arrays', () => {
expect(() => d.arrayOf(d.arrayOf(d.vec3f, 0), 0)).toThrowErrorMatchingInlineSnapshot(
'[Error: Cannot nest runtime sized arrays.]',
);
});
it('can be called to create an array', () => {
const ArraySchema = d.arrayOf(d.u32, 4);
const obj = ArraySchema([1, 2, 3, 4]);
expect(obj).toStrictEqual([1, 2, 3, 4]);
expectTypeOf(obj).toEqualTypeOf<number[]>();
});
it('cannot be called with invalid elements', () => {
const ArraySchema = d.arrayOf(d.u32, 4);
// @ts-expect-error
() => ArraySchema([1, 2, 3, d.vec3f()]);
// @ts-expect-error
() => ArraySchema([d.vec3f(), d.vec3f(), d.vec3f(), d.vec3f()]);
});
it('can be called to create a deep copy of other array', () => {
const InnerSchema = d.arrayOf(d.vec3f, 2);
const OuterSchema = d.arrayOf(InnerSchema, 3);
const instance = OuterSchema([
InnerSchema([d.vec3f(1, 2, 3), d.vec3f()]),
InnerSchema([d.vec3f(), d.vec3f()]),
InnerSchema([d.vec3f(), d.vec3f()]),
]);
const clone = OuterSchema(instance);
expect(clone).toStrictEqual(instance);
expect(clone).not.toBe(instance);
expect(clone[0]).not.toBe(instance[0]);
expect(clone[0]).not.toBe(clone[1]);
expect(clone[0]?.[0]).not.toBe(instance[0]?.[0]);
expect(clone[0]?.[0]).toStrictEqual(d.vec3f(1, 2, 3));
});
it('throws when invalid number of arguments', () => {
const ArraySchema = d.arrayOf(d.u32, 2);
expect(() => ArraySchema([1])).toThrowErrorMatchingInlineSnapshot(
'[Error: Array schema of 2 elements of type u32 called with 1 argument(s).]',
);
expect(() => ArraySchema([1, 2, 3])).toThrowErrorMatchingInlineSnapshot(
'[Error: Array schema of 2 elements of type u32 called with 3 argument(s).]',
);
});
it('throws when invalid number of arguments during code generation', () => {
const ArraySchema = d.arrayOf(d.u32, 2);
const f = () => {
'use gpu';
// @ts-expect-error
const arr = ArraySchema([1, 1], [6, 7]);
return;
};
expect(() => tgpu.resolve([f])).toThrowErrorMatchingInlineSnapshot(`
[Error: Resolution of the following tree failed:
- <root>
- fn*:f
- fn*:f(): Array schemas should always be called with at most 1 argument]
`);
});
it('can be called to create a default value', () => {
const ArraySchema = d.arrayOf(d.vec3f, 2);
const defaultArray = ArraySchema();
expect(defaultArray).toStrictEqual([d.vec3f(), d.vec3f()]);
});
it('can be called to create a default value with nested struct', () => {
const StructSchema = d.struct({ vec: d.vec3f });
const ArraySchema = d.arrayOf(StructSchema, 2);
const defaultArray = ArraySchema();
expect(defaultArray).toStrictEqual([{ vec: d.vec3f() }, { vec: d.vec3f() }]);
});
it('can be partially called', () => {
const ArrayPartialSchema = d.arrayOf(d.f32);
const array3 = ArrayPartialSchema(3)();
expect(array3).toStrictEqual([d.f32(), d.f32(), d.f32()]);
const array7 = ArrayPartialSchema(7)([1, 2, 1, 9, 2, 9, 7]);
expect(array7).toStrictEqual([1, 2, 1, 9, 2, 9, 7]);
});
it('generates correct code when Array default constructor is used', () => {
const Nested = d.arrayOf(d.f32, 1);
const Outer = d.arrayOf(Nested, 2);
const testFunction = tgpu.fn([])(() => {
const defaultValue = Outer();
});
expect(tgpu.resolve([testFunction])).toMatchInlineSnapshot(`
"fn testFunction() {
var defaultValue = array<array<f32, 1>, 2>();
}"
`);
});
it('generates correct code when array clone is used', () => {
const ArraySchema = d.arrayOf(d.u32, 1);
function f(arr: d.InferGPU<typeof ArraySchema>) {
'use gpu';
const clone = ArraySchema(arr);
}
const external = [3];
function testFn() {
'use gpu';
const myArray = ArraySchema([d.u32(10)]);
const myClone = ArraySchema(myArray);
const myExternal = ArraySchema(external);
f(myArray);
return;
}
expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(`
"fn f(arr: array<u32, 1>) {
var clone = arr;
}
fn testFn() {
var myArray = array<u32, 1>(10u);
var myClone = myArray;
var myExternal = array<u32, 1>(3u);
f(myArray);
return;
}"
`);
});
it('generates correct code when complex array clone is used', () => {
const ArraySchema = d.arrayOf(d.i32, 1);
const testFn = tgpu.fn([])(() => {
const myArrays = [ArraySchema([10])] as const;
const myClone = ArraySchema(myArrays[0]);
return;
});
expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(`
"fn testFn() {
var myArrays = array<array<i32, 1>, 1>(array<i32, 1>(10i));
var myClone = myArrays[0i];
return;
}"
`);
});
it('generates correct code when array expression with ephemeral element type clone is used', () => {
const f = () => {
'use gpu';
const arr = d.arrayOf(d.f32, 2)([6, 7]);
return;
};
expect(tgpu.resolve([f])).toMatchInlineSnapshot(`
"fn f() {
var arr = array<f32, 2>(6f, 7f);
return;
}"
`);
});
it('generates correct code when array expression with reference element type clone is used', () => {
const f = (v: d.v4f) => {
'use gpu';
const v2 = d.vec4f(3);
const v3 = v2;
const arr = d.arrayOf(d.vec4f, 3)([v, v2, v3]);
};
const main = tgpu.fn([])(() => {
const v1 = d.vec4f(7);
f(v1);
return;
});
expect(tgpu.resolve([main])).toMatchInlineSnapshot(`
"fn f(v: vec4f) {
var v2 = vec4f(3);
let v3 = (&v2);
var arr = array<vec4f, 3>(v, v2, (*v3));
}
fn main() {
var v1 = vec4f(7);
f(v1);
return;
}"
`);
});
it('generates correct code when array expression with mixed element types clone is used', () => {
const f = () => {
'use gpu';
const arr = d.arrayOf(d.f32, 3)([5, 6.7, 8.0]);
return;
};
expect(tgpu.resolve([f])).toMatchInlineSnapshot(`
"fn f() {
var arr = array<f32, 3>(5f, 6.7f, 8f);
return;
}"
`);
});
it('can be immediately-invoked in TGSL', () => {
const foo = tgpu.fn([])(() => {
const result = d.arrayOf(d.f32, 4)();
});
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"fn foo() {
var result = array<f32, 4>();
}"
`);
});
it('can be immediately-partially-invoked in TGSL', () => {
const foo = tgpu.fn([])(() => {
const result = d.arrayOf(d.f32)(4)();
});
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"fn foo() {
var result = array<f32, 4>();
}"
`);
});
it('throws when creating schema with runtime-known count', () => {
const foo = tgpu.fn([d.u32])((count) => {
const result = d.arrayOf(d.f32, count)();
});
expect(() => tgpu.resolve([foo])).toThrowErrorMatchingInlineSnapshot(`
[Error: Resolution of the following tree failed:
- <root>
- fn:foo
- fn:arrayOf: Called comptime function with runtime-known values: 'count']
`);
});
it('generates correct code when array is partially called in a layout', () => {
const testLayout = tgpu.bindGroupLayout({
testArray: { storage: d.arrayOf(d.u32) },
});
expect(tgpu.resolve([testLayout])).toMatchInlineSnapshot(
`"@group(0) @binding(0) var<storage, read> testArray: array<u32>;"`,
);
});
it('can be immediately-invoked and initialized in TGSL', () => {
const foo = tgpu.fn([])(() => {
const result = d.arrayOf(d.f32, 4)([1, 2, 3, 4]);
});
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"fn foo() {
var result = array<f32, 4>(1f, 2f, 3f, 4f);
}"
`);
});
it('can be immediately-partially-invoked and initialized in TGSL', () => {
const foo = tgpu.fn([])(() => {
const result = d.arrayOf(d.f32)(4)([4, 3, 2, 1]);
});
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"fn foo() {
var result = array<f32, 4>(4f, 3f, 2f, 1f);
}"
`);
});
it('can be immediately-invoked and initialized in TGSL in combination with slots', () => {
const arraySizeSlot = tgpu.slot(4);
const foo = tgpu.fn([])(() => {
const result = d.arrayOf(d.f32, arraySizeSlot.$)([4, 3, 2, 1]);
});
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"fn foo() {
var result = array<f32, 4>(4f, 3f, 2f, 1f);
}"
`);
});
it('can be immediately-invoked and initialized in TGSL in combination with slots and lazy', () => {
const arraySizeSlot = tgpu.slot(4);
const lazyArraySizeSlot = tgpu.lazy(() => arraySizeSlot.$ * 2);
const lazyInitializer = tgpu.lazy(() => [...Array(lazyArraySizeSlot.$).keys()]);
const foo = tgpu.fn([])(() => {
const result = d.arrayOf(d.f32, lazyArraySizeSlot.$)(lazyInitializer.$);
});
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"fn foo() {
var result = array<f32, 8>(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f);
}"
`);
});
it('throws when using refs in arrays', () => {
const foo = tgpu.fn([])(() => {
const myVec = d.vec2f(1, 2);
const result = [d.vec2f(3, 4), myVec];
});
expect(() => tgpu.resolve([foo])).toThrowErrorMatchingInlineSnapshot(`
[Error: Resolution of the following tree failed:
- <root>
- fn:foo
- ArrayExpression: 'myVec' reference cannot be used in an array constructor.
-----
Try 'vec2f(myVec)' or 'arrayOf(vec2f, count)([...])' to copy the value instead.
-----]
`);
});
it('throws when using argument refs in arrays', () => {
const foo = tgpu.fn([d.vec2f])((myVec) => {
const result = [d.vec2f(3, 4), myVec];
});
expect(() => tgpu.resolve([foo])).toThrowErrorMatchingInlineSnapshot(`
[Error: Resolution of the following tree failed:
- <root>
- fn:foo
- ArrayExpression: 'myVec' reference cannot be used in an array constructor.
-----
Try 'vec2f(myVec)' or 'arrayOf(vec2f, count)([...])' to copy the value instead.
-----]
`);
});
it('allows using ephemeral refs in arrays', () => {
const foo = tgpu.fn([d.u32])((n) => {
const m = d.u32(1);
const result = [1, n, m];
});
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"fn foo(n: u32) {
const m = 1u;
var result = array<u32, 3>(1u, n, m);
}"
`);
});
it('array expressions can be indexed into with a comptime-known index', () => {
function foo() {
'use gpu';
const i = 2;
const a = [i, 2][0];
const b = [i, 2][1];
}
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"fn foo() {
const i = 2;
const a = i;
const b = 2i;
}"
`);
});
it('array expressions can be indexed into with a runtime-known index', () => {
function foo() {
'use gpu';
const i = 0;
const a = [1, 2][i];
}
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"fn foo() {
const i = 0;
let a = array<i32, 2>(1, 2)[i];
}"
`);
});
it('allows picking among references using comptime-known indices', () => {
function foo() {
'use gpu';
const x = d.vec3f(1, 2, 3);
// `const y = [x, d.vec3f()]` would throw, but since
// we're never constructing the array, it's equivalent
// to writing `const y = x;`
const y = [x, d.vec3f()][0];
return y;
}
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"fn foo() -> vec3f {
var x = vec3f(1, 2, 3);
let y = (&x);
return (*y);
}"
`);
});
it('resolves array expression elements when accessed with comptime-known index', () => {
let n = 0;
const next = tgpu.comptime(() => n++);
function foo() {
'use gpu';
const a = [next(), next()][0];
const b = [next(), next()][1];
}
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"fn foo() {
const a = 0;
const b = 3;
}"
`);
});
it('prunes definitions in array expressions accessed with comptime-known index', ({ root }) => {
const u1 = root.createUniform(d.u32, 1);
const u2 = root.createUniform(d.u32, 2);
const u3 = root.createUniform(d.u32, 3);
const u4 = root.createUniform(d.u32, 4);
function foo() {
'use gpu';
const a = [u1.$, u2.$][0];
const b = [u3.$, u4.$][1];
}
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"@group(0) @binding(0) var<uniform> u1: u32;
@group(0) @binding(1) var<uniform> u4: u32;
fn foo() {
let a = u1;
let b = u4;
}"
`);
});
it('throws when trying to resolve an untyped external array', () => {
const arr = [1, 2, 3];
function main() {
'use gpu';
arr;
}
expect(() => tgpu.resolve([main])).toThrowErrorMatchingInlineSnapshot(`
[Error: Resolution of the following tree failed:
- <root>
- fn*:main
- fn*:main(): Value [1, 2, 3] is not resolvable]
`);
});
});
describe('array.length', () => {
it('works for dynamically-sized arrays in TGSL', () => {
const layout = tgpu.bindGroupLayout({
values: {
storage: d.arrayOf(d.f32),
access: 'mutable',
},
});
const foo = tgpu.fn([])(() => {
let acc = d.f32(1);
for (let i = d.u32(0); i < layout.$.values.length; i++) {
layout.$.values[i] = acc;
acc *= 2;
}
});
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"@group(0) @binding(0) var<storage, read_write> values: array<f32>;
fn foo() {
var acc = 1f;
for (var i = 0u; (i < arrayLength(&values)); i++) {
values[i] = acc;
acc *= 2f;
}
}"
`);
});
it('works for statically-sized arrays in TGSL', () => {
const layout = tgpu.bindGroupLayout({
values: {
storage: d.arrayOf(d.f32, 128),
access: 'mutable',
},
});
const foo = tgpu.fn([])(() => {
let acc = d.f32(1);
for (let i = 0; i < layout.$.values.length; i++) {
layout.$.values[i] = acc;
acc *= 2;
}
});
expect(tgpu.resolve([foo])).toMatchInlineSnapshot(`
"@group(0) @binding(0) var<storage, read_write> values: array<f32, 128>;
fn foo() {
var acc = 1f;
for (var i = 0; (i < 128i); i++) {
values[i] = acc;
acc *= 2f;
}
}"
`);
});
describe('arrayLength', () => {
it('returns the length of a static array', () => {
const staticArray = d.arrayOf(d.f32, 5);
const layout = tgpu.bindGroupLayout({
values: {
storage: staticArray,
access: 'mutable',
},
});
const testFn = tgpu.fn(
[],
d.i32,
)(() => {
return arrayLength(layout.$.values);
});
expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(`
"fn testFn() -> i32 {
return 5;
}"
`);
});
it('returns the length of a dynamic array', () => {
const dynamicArray = d.arrayOf(d.f32);
const layout = tgpu.bindGroupLayout({
values: {
storage: dynamicArray,
access: 'mutable',
},
});
const testFn = tgpu.fn(
[],
d.u32,
)(() => {
return arrayLength(layout.$.values);
});
expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(`
"@group(0) @binding(0) var<storage, read_write> values: array<f32>;
fn testFn() -> u32 {
return arrayLength((&values));
}"
`);
});
});
});