typegpu 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/README.md +1 -1
  2. package/builtin-ClEnM-Ye.js +818 -0
  3. package/builtin-ClEnM-Ye.js.map +1 -0
  4. package/chunk-BYypO7fO.js +18 -0
  5. package/common/index.d.ts +8 -23
  6. package/common/index.d.ts.map +1 -0
  7. package/common/index.js +7 -5
  8. package/common/index.js.map +1 -1
  9. package/data/index.d.ts +7 -401
  10. package/data/index.d.ts.map +1 -0
  11. package/data/index.js +164 -1
  12. package/data/index.js.map +1 -1
  13. package/deepEqual-yZXvaV2C.js +413 -0
  14. package/deepEqual-yZXvaV2C.js.map +1 -0
  15. package/extensions-0SFbU9FH.js +2032 -0
  16. package/extensions-0SFbU9FH.js.map +1 -0
  17. package/fullScreenTriangle-MdLGaAMR.js +543 -0
  18. package/fullScreenTriangle-MdLGaAMR.js.map +1 -0
  19. package/index.d.ts +124 -310
  20. package/index.d.ts.map +1 -0
  21. package/index.js +6282 -153
  22. package/index.js.map +1 -1
  23. package/indexNamedExports-Cdy7USiY.d.ts +5696 -0
  24. package/indexNamedExports-Cdy7USiY.d.ts.map +1 -0
  25. package/operators-HTxa_0k9.js +4156 -0
  26. package/operators-HTxa_0k9.js.map +1 -0
  27. package/package.json +3 -2
  28. package/std/index.d.ts +7 -621
  29. package/std/index.d.ts.map +1 -0
  30. package/std/index.js +165 -1
  31. package/std/index.js.map +1 -1
  32. package/texture-Dg5ybJro.js +205 -0
  33. package/texture-Dg5ybJro.js.map +1 -0
  34. package/chunk-5ABKYSJD.js +0 -2
  35. package/chunk-5ABKYSJD.js.map +0 -1
  36. package/chunk-D5UYO3OX.js +0 -3
  37. package/chunk-D5UYO3OX.js.map +0 -1
  38. package/chunk-EHLRP4V2.js +0 -2
  39. package/chunk-EHLRP4V2.js.map +0 -1
  40. package/chunk-LMPPDGRD.js +0 -2
  41. package/chunk-LMPPDGRD.js.map +0 -1
  42. package/chunk-MBB2XFH6.js +0 -2
  43. package/chunk-MBB2XFH6.js.map +0 -1
  44. package/chunk-SHSILTWI.js +0 -10
  45. package/chunk-SHSILTWI.js.map +0 -1
  46. package/comptime-DKpw1IVu.d.ts +0 -28
  47. package/matrix-C4IFKU1R.d.ts +0 -123
  48. package/tgpuConstant-BOn7U_lv.d.ts +0 -4031
@@ -0,0 +1,818 @@
1
+ import { An as isAlignAttrib, At as stitch, B as vec2h, Bn as isNaturallyEphemeral, Cn as isDisarray, Dn as undecorate, En as isUnstruct, G as vec3h, H as vec2u, Hn as isSizeAttrib, J as vec4b, Jn as isWgslData, K as vec3i, Kn as isVoid, L as isValidProp, Ln as isMat, M as INTERNAL_createStruct, Mn as isBuiltinAttrib, Nn as isDecorated, O as coerceToSnippet, Q as vec4u, R as vec2b, Sn as isData, Tn as isLooseDecorated, U as vec3b, Un as isVec, V as vec2i, Vn as isPtr, W as vec3f, Wt as isKnownAtComptime, X as vec4h, Y as vec4f, Yn as isWgslStruct, Z as vec4i, _n as MatrixColumnsAccess, _t as schemaCallWrapperGPU, at as i32, bn as getCustomLocation, bt as derefSnippet, cr as $internal, dn as isEphemeralSnippet, er as getName, fr as $resolve, gn as InfixDispatch, gt as schemaCallWrapper, ht as safeStringify, i as mul, ir as $cast, it as f32, mn as snip, n as div, nt as bool, o as sub, or as $gpuCallable, pr as isMarkedInternal, q as vec3u, qn as isWgslArray, r as mod, rr as setName, rt as f16, st as u32, t as add, tt as abstractInt, vn as UnknownData, w as comptime, wn as isLooseData, xn as getCustomSize, yn as getCustomAlignment, z as vec2f } from "./operators-HTxa_0k9.js";
2
+
3
+ //#region src/data/vertexFormatData.ts
4
+ var TgpuVertexFormatDataImpl = class {
5
+ [$internal] = {};
6
+ [$gpuCallable];
7
+ constructor(type) {
8
+ this.type = type;
9
+ this[$gpuCallable] = { call: (ctx, [v]) => {
10
+ return schemaCallWrapperGPU(ctx, formatToWGSLType[this.type], v);
11
+ } };
12
+ }
13
+ [$cast](v) {
14
+ return schemaCallWrapper(formatToWGSLType[this.type], v);
15
+ }
16
+ };
17
+ const formatToWGSLType = {
18
+ uint8: u32,
19
+ uint8x2: vec2u,
20
+ uint8x4: vec4u,
21
+ sint8: i32,
22
+ sint8x2: vec2i,
23
+ sint8x4: vec4i,
24
+ unorm8: f32,
25
+ unorm8x2: vec2f,
26
+ unorm8x4: vec4f,
27
+ snorm8: f32,
28
+ snorm8x2: vec2f,
29
+ snorm8x4: vec4f,
30
+ uint16: u32,
31
+ uint16x2: vec2u,
32
+ uint16x4: vec4u,
33
+ sint16: i32,
34
+ sint16x2: vec2i,
35
+ sint16x4: vec4i,
36
+ unorm16: f32,
37
+ unorm16x2: vec2f,
38
+ unorm16x4: vec4f,
39
+ snorm16: f32,
40
+ snorm16x2: vec2f,
41
+ snorm16x4: vec4f,
42
+ float16: f32,
43
+ float16x2: vec2f,
44
+ float16x4: vec4f,
45
+ float32: f32,
46
+ float32x2: vec2f,
47
+ float32x3: vec3f,
48
+ float32x4: vec4f,
49
+ uint32: u32,
50
+ uint32x2: vec2u,
51
+ uint32x3: vec3u,
52
+ uint32x4: vec4u,
53
+ sint32: i32,
54
+ sint32x2: vec2i,
55
+ sint32x3: vec3i,
56
+ sint32x4: vec4i,
57
+ "unorm10-10-10-2": vec4f,
58
+ "unorm8x4-bgra": vec4f
59
+ };
60
+ const packedFormats = new Set(Object.keys(formatToWGSLType));
61
+ const uint8 = new TgpuVertexFormatDataImpl("uint8");
62
+ const uint8x2 = new TgpuVertexFormatDataImpl("uint8x2");
63
+ const uint8x4 = new TgpuVertexFormatDataImpl("uint8x4");
64
+ const sint8 = new TgpuVertexFormatDataImpl("sint8");
65
+ const sint8x2 = new TgpuVertexFormatDataImpl("sint8x2");
66
+ const sint8x4 = new TgpuVertexFormatDataImpl("sint8x4");
67
+ const unorm8 = new TgpuVertexFormatDataImpl("unorm8");
68
+ const unorm8x2 = new TgpuVertexFormatDataImpl("unorm8x2");
69
+ const unorm8x4 = new TgpuVertexFormatDataImpl("unorm8x4");
70
+ const snorm8 = new TgpuVertexFormatDataImpl("snorm8");
71
+ const snorm8x2 = new TgpuVertexFormatDataImpl("snorm8x2");
72
+ const snorm8x4 = new TgpuVertexFormatDataImpl("snorm8x4");
73
+ const uint16 = new TgpuVertexFormatDataImpl("uint16");
74
+ const uint16x2 = new TgpuVertexFormatDataImpl("uint16x2");
75
+ const uint16x4 = new TgpuVertexFormatDataImpl("uint16x4");
76
+ const sint16 = new TgpuVertexFormatDataImpl("sint16");
77
+ const sint16x2 = new TgpuVertexFormatDataImpl("sint16x2");
78
+ const sint16x4 = new TgpuVertexFormatDataImpl("sint16x4");
79
+ const unorm16 = new TgpuVertexFormatDataImpl("unorm16");
80
+ const unorm16x2 = new TgpuVertexFormatDataImpl("unorm16x2");
81
+ const unorm16x4 = new TgpuVertexFormatDataImpl("unorm16x4");
82
+ const snorm16 = new TgpuVertexFormatDataImpl("snorm16");
83
+ const snorm16x2 = new TgpuVertexFormatDataImpl("snorm16x2");
84
+ const snorm16x4 = new TgpuVertexFormatDataImpl("snorm16x4");
85
+ const float16 = new TgpuVertexFormatDataImpl("float16");
86
+ const float16x2 = new TgpuVertexFormatDataImpl("float16x2");
87
+ const float16x4 = new TgpuVertexFormatDataImpl("float16x4");
88
+ const float32 = new TgpuVertexFormatDataImpl("float32");
89
+ const float32x2 = new TgpuVertexFormatDataImpl("float32x2");
90
+ const float32x3 = new TgpuVertexFormatDataImpl("float32x3");
91
+ const float32x4 = new TgpuVertexFormatDataImpl("float32x4");
92
+ const uint32 = new TgpuVertexFormatDataImpl("uint32");
93
+ const uint32x2 = new TgpuVertexFormatDataImpl("uint32x2");
94
+ const uint32x3 = new TgpuVertexFormatDataImpl("uint32x3");
95
+ const uint32x4 = new TgpuVertexFormatDataImpl("uint32x4");
96
+ const sint32 = new TgpuVertexFormatDataImpl("sint32");
97
+ const sint32x2 = new TgpuVertexFormatDataImpl("sint32x2");
98
+ const sint32x3 = new TgpuVertexFormatDataImpl("sint32x3");
99
+ const sint32x4 = new TgpuVertexFormatDataImpl("sint32x4");
100
+ const unorm10_10_10_2 = new TgpuVertexFormatDataImpl("unorm10-10-10-2");
101
+ const unorm8x4_bgra = new TgpuVertexFormatDataImpl("unorm8x4-bgra");
102
+ function isPackedData(value) {
103
+ return isMarkedInternal(value) && packedFormats.has(value?.type);
104
+ }
105
+
106
+ //#endregion
107
+ //#region src/data/alignmentOf.ts
108
+ const knownAlignmentMap = {
109
+ f32: 4,
110
+ f16: 2,
111
+ i32: 4,
112
+ u32: 4,
113
+ bool: 4,
114
+ u16: 2,
115
+ vec2f: 8,
116
+ vec2h: 4,
117
+ vec2i: 8,
118
+ vec2u: 8,
119
+ "vec2<bool>": 8,
120
+ vec3f: 16,
121
+ vec3h: 8,
122
+ vec3i: 16,
123
+ vec3u: 16,
124
+ "vec3<bool>": 16,
125
+ vec4f: 16,
126
+ vec4h: 8,
127
+ vec4i: 16,
128
+ vec4u: 16,
129
+ "vec4<bool>": 16,
130
+ mat2x2f: 8,
131
+ mat3x3f: 16,
132
+ mat4x4f: 16,
133
+ atomic: 4
134
+ };
135
+ function computeAlignment(data) {
136
+ const dataType = data?.type;
137
+ const knownAlignment = knownAlignmentMap[dataType];
138
+ if (knownAlignment !== void 0) return knownAlignment;
139
+ if (isWgslStruct(data)) return Object.values(data.propTypes).map(alignmentOf).reduce((a, b) => a > b ? a : b);
140
+ if (isWgslArray(data)) return alignmentOf(data.elementType);
141
+ if (isUnstruct(data)) {
142
+ const firstProp = Object.values(data.propTypes)[0];
143
+ return firstProp ? getCustomAlignment(firstProp) ?? 1 : 1;
144
+ }
145
+ if (isDisarray(data)) return getCustomAlignment(data.elementType) ?? 1;
146
+ if (isDecorated(data) || isLooseDecorated(data)) return getCustomAlignment(data) ?? alignmentOf(data.inner);
147
+ if (packedFormats.has(dataType)) return 1;
148
+ throw new Error(`Cannot determine alignment of data: ${safeStringify(data)}`);
149
+ }
150
+ function computeCustomAlignment(data) {
151
+ if (isUnstruct(data)) {
152
+ const firstProp = Object.values(data.propTypes)[0];
153
+ return firstProp ? customAlignmentOf(firstProp) : 1;
154
+ }
155
+ if (isDisarray(data)) return customAlignmentOf(data.elementType);
156
+ if (isLooseDecorated(data)) return getCustomAlignment(data) ?? customAlignmentOf(data.inner);
157
+ return getCustomAlignment(data) ?? 1;
158
+ }
159
+ /**
160
+ * Since alignments can be inferred from data types, they are not stored on them.
161
+ * Instead, this weak map acts as an extended property of those data types.
162
+ */
163
+ const cachedAlignments = /* @__PURE__ */ new WeakMap();
164
+ const cachedCustomAlignments = /* @__PURE__ */ new WeakMap();
165
+ function alignmentOf(data) {
166
+ let alignment = cachedAlignments.get(data);
167
+ if (alignment === void 0) {
168
+ alignment = computeAlignment(data);
169
+ cachedAlignments.set(data, alignment);
170
+ }
171
+ return alignment;
172
+ }
173
+ function customAlignmentOf(data) {
174
+ let alignment = cachedCustomAlignments.get(data);
175
+ if (alignment === void 0) {
176
+ alignment = computeCustomAlignment(data);
177
+ cachedCustomAlignments.set(data, alignment);
178
+ }
179
+ return alignment;
180
+ }
181
+ /**
182
+ * Returns the alignment (in bytes) of data represented by the `schema`.
183
+ */
184
+ function PUBLIC_alignmentOf(schema) {
185
+ return alignmentOf(schema);
186
+ }
187
+
188
+ //#endregion
189
+ //#region src/mathUtils.ts
190
+ /**
191
+ * @param value
192
+ * @param modulo has to be power of 2
193
+ */
194
+ const roundUp = (value, modulo) => {
195
+ const bitMask = modulo - 1;
196
+ const invBitMask = ~bitMask;
197
+ return (value & bitMask) === 0 ? value : (value & invBitMask) + modulo;
198
+ };
199
+
200
+ //#endregion
201
+ //#region src/data/schemaMemoryLayout.ts
202
+ const knownSizesMap = {
203
+ bool: 4,
204
+ f32: 4,
205
+ f16: 2,
206
+ i32: 4,
207
+ u32: 4,
208
+ u16: 2,
209
+ vec2f: 8,
210
+ vec2h: 4,
211
+ vec2i: 8,
212
+ vec2u: 8,
213
+ "vec2<bool>": 8,
214
+ vec3f: 12,
215
+ vec3h: 6,
216
+ vec3i: 12,
217
+ vec3u: 12,
218
+ "vec3<bool>": 12,
219
+ vec4f: 16,
220
+ vec4h: 8,
221
+ vec4i: 16,
222
+ vec4u: 16,
223
+ "vec4<bool>": 16,
224
+ mat2x2f: 16,
225
+ mat3x3f: 48,
226
+ mat4x4f: 64,
227
+ uint8: 1,
228
+ uint8x2: 2,
229
+ uint8x4: 4,
230
+ sint8: 1,
231
+ sint8x2: 2,
232
+ sint8x4: 4,
233
+ unorm8: 1,
234
+ unorm8x2: 2,
235
+ unorm8x4: 4,
236
+ snorm8: 1,
237
+ snorm8x2: 2,
238
+ snorm8x4: 4,
239
+ uint16: 2,
240
+ uint16x2: 4,
241
+ uint16x4: 8,
242
+ sint16: 2,
243
+ sint16x2: 4,
244
+ sint16x4: 8,
245
+ unorm16: 2,
246
+ unorm16x2: 4,
247
+ unorm16x4: 8,
248
+ snorm16: 2,
249
+ snorm16x2: 4,
250
+ snorm16x4: 8,
251
+ float16: 2,
252
+ float16x2: 4,
253
+ float16x4: 8,
254
+ float32: 4,
255
+ float32x2: 8,
256
+ float32x3: 12,
257
+ float32x4: 16,
258
+ uint32: 4,
259
+ uint32x2: 8,
260
+ uint32x3: 12,
261
+ uint32x4: 16,
262
+ sint32: 4,
263
+ sint32x2: 8,
264
+ sint32x3: 12,
265
+ sint32x4: 16,
266
+ "unorm10-10-10-2": 4,
267
+ "unorm8x4-bgra": 4,
268
+ atomic: 4
269
+ };
270
+ function computeMLOfStruct(struct) {
271
+ let size = 0;
272
+ let longestContiguousPrefix = 0;
273
+ let isContiguous = true;
274
+ let prefixEnd = false;
275
+ for (const property of Object.values(struct.propTypes)) {
276
+ if (Number.isNaN(size)) throw new Error("Only the last property of a struct can be unbounded");
277
+ const prevSize = size;
278
+ size = roundUp(size, alignmentOf(property));
279
+ const hasPadding = prevSize !== size;
280
+ const propLayout = computeMemoryLayout(property);
281
+ size += propLayout.size;
282
+ if (Number.isNaN(size) && property.type !== "array") throw new Error("Cannot nest unbounded struct within another struct");
283
+ if (prefixEnd) continue;
284
+ if (!hasPadding && propLayout.isContiguous) longestContiguousPrefix += propLayout.size;
285
+ else {
286
+ prefixEnd = true;
287
+ isContiguous = false;
288
+ if (!hasPadding) longestContiguousPrefix += propLayout.longestContiguousPrefix;
289
+ }
290
+ }
291
+ const trueSize = roundUp(size, alignmentOf(struct));
292
+ return {
293
+ isContiguous: size === trueSize && isContiguous,
294
+ size: trueSize,
295
+ longestContiguousPrefix
296
+ };
297
+ }
298
+ function computeMLOfUnstruct(data) {
299
+ let size = 0;
300
+ let longestContiguousPrefix = 0;
301
+ let isContiguous = true;
302
+ let prefixEnd = false;
303
+ for (const property of Object.values(data.propTypes)) {
304
+ const alignment = customAlignmentOf(property);
305
+ const prevSize = size;
306
+ size = roundUp(size, alignment);
307
+ const hasPadding = prevSize !== size;
308
+ if (hasPadding) isContiguous = false;
309
+ const propLayout = computeMemoryLayout(property);
310
+ size += propLayout.size;
311
+ if (prefixEnd) continue;
312
+ if (!hasPadding && propLayout.isContiguous) longestContiguousPrefix += propLayout.size;
313
+ else {
314
+ prefixEnd = true;
315
+ isContiguous = false;
316
+ if (!hasPadding) longestContiguousPrefix += propLayout.longestContiguousPrefix;
317
+ }
318
+ }
319
+ return {
320
+ isContiguous,
321
+ size,
322
+ longestContiguousPrefix
323
+ };
324
+ }
325
+ function computeMLOfWgslArray(data) {
326
+ const elementType = data.elementType;
327
+ const elementMemoryLayout = computeMemoryLayout(elementType);
328
+ const elementSize = elementMemoryLayout.size;
329
+ const stride = roundUp(elementSize, alignmentOf(elementType));
330
+ const isContiguous = !(stride > elementSize) && elementMemoryLayout.isContiguous;
331
+ const size = data.elementCount === 0 ? NaN : data.elementCount * stride;
332
+ let longestContiguousPrefix;
333
+ if (isContiguous) longestContiguousPrefix = size;
334
+ else longestContiguousPrefix = elementMemoryLayout.longestContiguousPrefix;
335
+ return {
336
+ size,
337
+ isContiguous,
338
+ longestContiguousPrefix
339
+ };
340
+ }
341
+ function computeMLOfDisarray(data) {
342
+ const elementType = data.elementType;
343
+ const elementMemoryLayout = computeMemoryLayout(elementType);
344
+ const elementSize = elementMemoryLayout.size;
345
+ const stride = roundUp(elementSize, customAlignmentOf(elementType));
346
+ const isContiguous = !(stride > elementSize) && elementMemoryLayout.isContiguous;
347
+ const size = data.elementCount * stride;
348
+ let longestContiguousPrefix;
349
+ if (isContiguous) longestContiguousPrefix = size;
350
+ else longestContiguousPrefix = elementMemoryLayout.longestContiguousPrefix;
351
+ return {
352
+ size,
353
+ isContiguous,
354
+ longestContiguousPrefix
355
+ };
356
+ }
357
+ function computeMemoryLayout(data) {
358
+ const knownSize = knownSizesMap[data.type];
359
+ if (knownSize !== void 0) return {
360
+ isContiguous: data.type !== "mat3x3f",
361
+ size: knownSize,
362
+ longestContiguousPrefix: data.type === "mat3x3f" ? 12 : knownSize
363
+ };
364
+ if (isWgslStruct(data)) return computeMLOfStruct(data);
365
+ if (isUnstruct(data)) return computeMLOfUnstruct(data);
366
+ if (isWgslArray(data)) return computeMLOfWgslArray(data);
367
+ if (isDisarray(data)) return computeMLOfDisarray(data);
368
+ if (isDecorated(data) || isLooseDecorated(data)) {
369
+ const size = getCustomSize(data);
370
+ const undecoratedLayout = computeMemoryLayout(undecorate(data));
371
+ if (size) return {
372
+ isContiguous: size === undecoratedLayout.size && undecoratedLayout.isContiguous,
373
+ size,
374
+ longestContiguousPrefix: undecoratedLayout.longestContiguousPrefix
375
+ };
376
+ return computeMemoryLayout(data.inner);
377
+ }
378
+ throw new Error(`Cannot determine memory layout of data: ${data}`);
379
+ }
380
+ /**
381
+ * Since memory layout can be inferred from data types, they are not stored on them.
382
+ * Instead, this weak map acts as an extended property of those data types.
383
+ */
384
+ const cachedLayouts = /* @__PURE__ */ new WeakMap();
385
+ function getLayoutInfo(schema, key) {
386
+ let layout = cachedLayouts.get(schema);
387
+ if (layout === void 0) {
388
+ layout = computeMemoryLayout(schema);
389
+ cachedLayouts.set(schema, layout);
390
+ }
391
+ return layout[key];
392
+ }
393
+
394
+ //#endregion
395
+ //#region src/data/sizeOf.ts
396
+ function sizeOf(schema) {
397
+ return getLayoutInfo(schema, "size");
398
+ }
399
+ /**
400
+ * Returns the size (in bytes) of data represented by the `schema`.
401
+ */
402
+ function PUBLIC_sizeOf(schema) {
403
+ return sizeOf(schema);
404
+ }
405
+
406
+ //#endregion
407
+ //#region src/data/attributes.ts
408
+ function attribute(data, attrib) {
409
+ if (isDecorated(data)) return new DecoratedImpl(data.inner, [attrib, ...data.attribs]);
410
+ if (isLooseDecorated(data)) return new LooseDecoratedImpl(data.inner, [attrib, ...data.attribs]);
411
+ if (isLooseData(data)) return new LooseDecoratedImpl(data, [attrib]);
412
+ return new DecoratedImpl(data, [attrib]);
413
+ }
414
+ /**
415
+ * Gives the wrapped data-type a custom byte alignment. Useful in order to
416
+ * fulfill uniform alignment requirements.
417
+ *
418
+ * @example
419
+ * const Data = d.struct({
420
+ * a: u32, // takes up 4 bytes
421
+ * // 12 bytes of padding, because `b` is custom aligned to multiples of 16 bytes
422
+ * b: d.align(16, u32),
423
+ * });
424
+ *
425
+ * @param alignment The multiple of bytes this data should align itself to.
426
+ * @param data The data-type to align.
427
+ */
428
+ function align(alignment, data) {
429
+ return attribute(data, {
430
+ [$internal]: true,
431
+ type: "@align",
432
+ params: [alignment]
433
+ });
434
+ }
435
+ /**
436
+ * Adds padding bytes after the wrapped data-type, until the whole value takes up `size` bytes.
437
+ *
438
+ * @example
439
+ * const Data = d.struct({
440
+ * a: d.size(16, u32), // takes up 16 bytes, instead of 4
441
+ * b: u32, // starts at byte 16, because `a` has a custom size
442
+ * });
443
+ *
444
+ * @param size The amount of bytes that should be reserved for this data-type.
445
+ * @param data The data-type to wrap.
446
+ */
447
+ function size(size, data) {
448
+ return attribute(data, {
449
+ [$internal]: true,
450
+ type: "@size",
451
+ params: [size]
452
+ });
453
+ }
454
+ /**
455
+ * Assigns an explicit numeric location to a struct member or a parameter that has this type.
456
+ *
457
+ * @example
458
+ * const VertexOutput = {
459
+ * a: d.u32, // has implicit location 0
460
+ * b: d.location(5, d.u32),
461
+ * c: d.u32, // has implicit location 6
462
+ * };
463
+ *
464
+ * @param location The explicit numeric location.
465
+ * @param data The data-type to wrap.
466
+ */
467
+ function location(location, data) {
468
+ return attribute(data, {
469
+ [$internal]: true,
470
+ type: "@location",
471
+ params: [location]
472
+ });
473
+ }
474
+ function interpolate(interpolationType, data) {
475
+ return attribute(data, {
476
+ [$internal]: true,
477
+ type: "@interpolate",
478
+ params: [interpolationType]
479
+ });
480
+ }
481
+ /**
482
+ * Marks a position built-in output value as invariant in vertex shaders.
483
+ * If the data and control flow match for two position outputs in different
484
+ * entry points, then the result values are guaranteed to be the same.
485
+ *
486
+ * Must only be applied to the position built-in value.
487
+ *
488
+ * @example
489
+ * const VertexOutput = {
490
+ * pos: d.invariant(d.builtin.position),
491
+ * };
492
+ *
493
+ * @param data The position built-in data-type to mark as invariant.
494
+ */
495
+ function invariant(data) {
496
+ if (!isBuiltin(data)) throw new Error("The @invariant attribute must only be applied to the position built-in value.");
497
+ const builtinAttrib = isDecorated(data) || isLooseDecorated(data) ? data.attribs.find(isBuiltinAttrib) : void 0;
498
+ if (!builtinAttrib || builtinAttrib.params[0] !== "position") throw new Error("The @invariant attribute must only be applied to the position built-in value.");
499
+ return attribute(data, {
500
+ [$internal]: true,
501
+ type: "@invariant",
502
+ params: []
503
+ });
504
+ }
505
+ function isBuiltin(value) {
506
+ return (isDecorated(value) || isLooseDecorated(value)) && value.attribs.find(isBuiltinAttrib) !== void 0;
507
+ }
508
+ function getAttributesString(field) {
509
+ if (!isDecorated(field) && !isLooseDecorated(field)) return "";
510
+ return field.attribs.map((attrib) => {
511
+ if (attrib.params.length === 0) return `${attrib.type} `;
512
+ return `${attrib.type}(${attrib.params.join(", ")}) `;
513
+ }).join("");
514
+ }
515
+ var BaseDecoratedImpl = class {
516
+ [$internal] = {};
517
+ constructor(inner, attribs) {
518
+ this.inner = inner;
519
+ this.attribs = attribs;
520
+ const alignAttrib = attribs.find(isAlignAttrib)?.params[0];
521
+ const sizeAttrib = attribs.find(isSizeAttrib)?.params[0];
522
+ if (alignAttrib !== void 0) {
523
+ if (alignAttrib <= 0) throw new Error(`Custom data alignment must be a positive number, got: ${alignAttrib}.`);
524
+ if (Math.log2(alignAttrib) % 1 !== 0) throw new Error(`Alignment has to be a power of 2, got: ${alignAttrib}.`);
525
+ if (isWgslData(this.inner)) {
526
+ if (alignAttrib % alignmentOf(this.inner) !== 0) throw new Error(`Custom alignment has to be a multiple of the standard data alignment. Got: ${alignAttrib}, expected multiple of: ${alignmentOf(this.inner)}.`);
527
+ }
528
+ }
529
+ if (sizeAttrib !== void 0) {
530
+ if (sizeAttrib < sizeOf(this.inner)) throw new Error(`Custom data size cannot be smaller then the standard data size. Got: ${sizeAttrib}, expected at least: ${sizeOf(this.inner)}.`);
531
+ if (sizeAttrib <= 0) throw new Error(`Custom data size must be a positive number. Got: ${sizeAttrib}.`);
532
+ }
533
+ }
534
+ };
535
+ var DecoratedImpl = class extends BaseDecoratedImpl {
536
+ [$internal] = {};
537
+ type = "decorated";
538
+ };
539
+ var LooseDecoratedImpl = class extends BaseDecoratedImpl {
540
+ [$internal] = {};
541
+ type = "loose-decorated";
542
+ };
543
+
544
+ //#endregion
545
+ //#region src/core/function/ioSchema.ts
546
+ function withLocations(members, locations = {}) {
547
+ let nextLocation = 0;
548
+ const usedCustomLocations = /* @__PURE__ */ new Set();
549
+ return Object.fromEntries(Object.entries(members ?? {}).map(([key, member]) => {
550
+ const customLocation = getCustomLocation(member);
551
+ if (customLocation !== void 0) {
552
+ if (usedCustomLocations.has(customLocation)) throw new Error("Duplicate custom location attributes found");
553
+ usedCustomLocations.add(customLocation);
554
+ }
555
+ return [key, member];
556
+ }).map(([key, member]) => {
557
+ if (isBuiltin(member)) return [key, member];
558
+ if (getCustomLocation(member) !== void 0) return [key, member];
559
+ if (locations[key]) return [key, location(locations[key], member)];
560
+ while (usedCustomLocations.has(nextLocation)) nextLocation++;
561
+ return [key, location(nextLocation++, member)];
562
+ }));
563
+ }
564
+ function createIoSchema(layout, locations = {}) {
565
+ return isData(layout) ? isVoid(layout) ? layout : isBuiltin(layout) ? layout : getCustomLocation(layout) !== void 0 ? layout : location(0, layout) : INTERNAL_createStruct(withLocations(layout, locations), false);
566
+ }
567
+
568
+ //#endregion
569
+ //#region src/data/autoStruct.ts
570
+ /**
571
+ * A requirement for the generated struct is that non-builtin properties need to
572
+ * have the same name in WGSL as they do in JS. This allows locations to be properly
573
+ * matched between the Vertex output and Fragment input.
574
+ */
575
+ var AutoStruct = class {
576
+ /**
577
+ * js key -> data type
578
+ */
579
+ #validProps;
580
+ /**
581
+ * js key -> { prop: 'wgsl key', type: ... }
582
+ * @example '$position' -> { prop: 'position', type: ... }
583
+ */
584
+ #allocated;
585
+ #usedWgslKeys;
586
+ #locations;
587
+ #cachedStruct;
588
+ #typeForExtraProps;
589
+ constructor(validProps, typeForExtraProps, locations) {
590
+ this.#validProps = validProps;
591
+ this.#typeForExtraProps = typeForExtraProps;
592
+ this.#allocated = {};
593
+ this.#locations = locations;
594
+ this.#usedWgslKeys = /* @__PURE__ */ new Set();
595
+ }
596
+ /**
597
+ * Used for accessing builtins, varying and attributes in code.
598
+ */
599
+ accessProp(key) {
600
+ const dataType = this.#validProps[key] ?? this.#typeForExtraProps;
601
+ if (!dataType) return;
602
+ return this.provideProp(key, dataType);
603
+ }
604
+ /**
605
+ * Used for providing new varyings.
606
+ *
607
+ * @privateRemarks
608
+ * Internally used by `accessProp`.
609
+ */
610
+ provideProp(key, dataType) {
611
+ let alloc = this.#allocated[key];
612
+ if (!alloc) {
613
+ const wgslKey = key.replaceAll("$", "");
614
+ if (this.#usedWgslKeys.has(wgslKey)) throw new Error(`Property name '${wgslKey}' causes naming clashes. Choose a different name.`);
615
+ if (!isValidProp(wgslKey)) throw new Error(`Property key '${key}' is a reserved WGSL word. Choose a different name.`);
616
+ this.#usedWgslKeys.add(wgslKey);
617
+ alloc = {
618
+ prop: wgslKey,
619
+ type: dataType
620
+ };
621
+ this.#allocated[key] = alloc;
622
+ }
623
+ return alloc;
624
+ }
625
+ get completeStruct() {
626
+ if (!this.#cachedStruct) {
627
+ this.#cachedStruct = createIoSchema(Object.fromEntries(Object.values(this.#allocated).map((alloc) => {
628
+ return [alloc.prop, alloc.type];
629
+ })), this.#locations);
630
+ const ownName = getName(this);
631
+ if (ownName) setName(this.#cachedStruct, ownName);
632
+ }
633
+ return this.#cachedStruct;
634
+ }
635
+ [$resolve](ctx) {
636
+ return ctx.resolve(this.completeStruct);
637
+ }
638
+ toString() {
639
+ return `auto-struct:${getName(this) ?? "<unnamed>"}`;
640
+ }
641
+ };
642
+ AutoStruct.prototype[$internal] = {};
643
+ AutoStruct.prototype.type = "auto-struct";
644
+
645
+ //#endregion
646
+ //#region src/tgsl/accessProp.ts
647
+ const infixKinds = [
648
+ "vec2f",
649
+ "vec3f",
650
+ "vec4f",
651
+ "vec2h",
652
+ "vec3h",
653
+ "vec4h",
654
+ "vec2i",
655
+ "vec3i",
656
+ "vec4i",
657
+ "vec2u",
658
+ "vec3u",
659
+ "vec4u",
660
+ "mat2x2f",
661
+ "mat3x3f",
662
+ "mat4x4f"
663
+ ];
664
+ const infixOperators = {
665
+ add,
666
+ sub,
667
+ mul,
668
+ div,
669
+ mod
670
+ };
671
+ const swizzleLenToType = {
672
+ f: {
673
+ 1: f32,
674
+ 2: vec2f,
675
+ 3: vec3f,
676
+ 4: vec4f
677
+ },
678
+ h: {
679
+ 1: f16,
680
+ 2: vec2h,
681
+ 3: vec3h,
682
+ 4: vec4h
683
+ },
684
+ i: {
685
+ 1: i32,
686
+ 2: vec2i,
687
+ 3: vec3i,
688
+ 4: vec4i
689
+ },
690
+ u: {
691
+ 1: u32,
692
+ 2: vec2u,
693
+ 3: vec3u,
694
+ 4: vec4u
695
+ },
696
+ b: {
697
+ 1: bool,
698
+ 2: vec2b,
699
+ 3: vec3b,
700
+ 4: vec4b
701
+ }
702
+ };
703
+ function accessProp(target, propName) {
704
+ if (infixKinds.includes(target.dataType.type) && propName in infixOperators) {
705
+ const operator = infixOperators[propName];
706
+ return snip(new InfixDispatch(propName, target, operator[$gpuCallable].call.bind(operator)), UnknownData, target.origin);
707
+ }
708
+ if (isWgslArray(target.dataType) && propName === "length") {
709
+ if (target.dataType.elementCount === 0) return snip(stitch`arrayLength(&${target})`, u32, "runtime");
710
+ return snip(target.dataType.elementCount, abstractInt, "constant");
711
+ }
712
+ if (isMat(target.dataType) && propName === "columns") return snip(new MatrixColumnsAccess(target), UnknownData, target.origin);
713
+ if (isWgslStruct(target.dataType) || isUnstruct(target.dataType)) {
714
+ let propType = target.dataType.propTypes[propName];
715
+ if (!propType) return;
716
+ propType = undecorate(propType);
717
+ return snip(stitch`${target}.${propName}`, propType, target.origin === "argument" ? "argument" : !isEphemeralSnippet(target) && !isNaturallyEphemeral(propType) ? target.origin : target.origin === "constant" || target.origin === "constant-tgpu-const-ref" ? "constant" : "runtime");
718
+ }
719
+ if (target.dataType instanceof AutoStruct) {
720
+ const result = target.dataType.accessProp(propName);
721
+ if (!result) return;
722
+ return snip(stitch`${target}.${result.prop}`, result.type, "argument");
723
+ }
724
+ if (isPtr(target.dataType)) {
725
+ const derefed = derefSnippet(target);
726
+ if (propName === "$") return derefed;
727
+ return accessProp(derefed, propName);
728
+ }
729
+ if (isVec(target.dataType)) {
730
+ if (propName === "kind") return snip(target.dataType.type, UnknownData, "constant");
731
+ }
732
+ const propLength = propName.length;
733
+ if (isVec(target.dataType) && propLength >= 1 && propLength <= 4) {
734
+ const isXYZW = /^[xyzw]+$/.test(propName);
735
+ const isRGBA = /^[rgba]+$/.test(propName);
736
+ if (!isXYZW && !isRGBA) return;
737
+ const swizzleType = swizzleLenToType[target.dataType.type.includes("bool") ? "b" : target.dataType.type[4]][propLength];
738
+ if (!swizzleType) return;
739
+ return snip(isKnownAtComptime(target) ? target.value[propName] : stitch`${target}.${propName}`, swizzleType, target.origin === "argument" && propLength === 1 ? "argument" : target.origin === "constant" || target.origin === "constant-tgpu-const-ref" ? "constant" : "runtime");
740
+ }
741
+ if (isKnownAtComptime(target) || target.dataType === UnknownData) return coerceToSnippet(target.value[propName]);
742
+ }
743
+
744
+ //#endregion
745
+ //#region src/data/array.ts
746
+ /**
747
+ * Creates an array schema that can be used to construct gpu buffers.
748
+ * Describes arrays with fixed-size length, storing elements of the same type.
749
+ *
750
+ * @example
751
+ * const LENGTH = 3;
752
+ * const array = d.arrayOf(d.u32, LENGTH);
753
+ *
754
+ * If `elementCount` is not specified, a partially applied function is returned.
755
+ * @example
756
+ * const array = d.arrayOf(d.vec3f);
757
+ * // ^? (n: number) => WgslArray<d.Vec3f>
758
+ *
759
+ * @param elementType The type of elements in the array.
760
+ * @param elementCount The number of elements in the array.
761
+ */
762
+ const arrayOf = comptime(((elementType, elementCount) => {
763
+ if (elementCount === void 0) return comptime((count) => cpu_arrayOf(elementType, count));
764
+ return cpu_arrayOf(elementType, elementCount);
765
+ })).$name("arrayOf");
766
+ function cpu_arrayOf(elementType, elementCount) {
767
+ const arraySchema = (elements) => {
768
+ if (elements && elements.length !== elementCount) throw new Error(`Array schema of ${elementCount} elements of type ${elementType.type} called with ${elements.length} argument(s).`);
769
+ return Array.from({ length: elementCount }, (_, i) => schemaCallWrapper(elementType, elements?.[i]));
770
+ };
771
+ Object.setPrototypeOf(arraySchema, WgslArrayImpl);
772
+ if (Number.isNaN(sizeOf(elementType))) throw new Error("Cannot nest runtime sized arrays.");
773
+ arraySchema.elementType = elementType;
774
+ if (!Number.isInteger(elementCount) || elementCount < 0) throw new Error(`Cannot create array schema with invalid element count: ${elementCount}.`);
775
+ arraySchema.elementCount = elementCount;
776
+ return arraySchema;
777
+ }
778
+ const WgslArrayImpl = {
779
+ [$internal]: true,
780
+ type: "array",
781
+ toString() {
782
+ return `arrayOf(${this.elementType}, ${this.elementCount})`;
783
+ }
784
+ };
785
+
786
+ //#endregion
787
+ //#region src/builtin.ts
788
+ function defineBuiltin(dataType, value) {
789
+ return attribute(dataType, {
790
+ [$internal]: true,
791
+ type: "@builtin",
792
+ params: [value]
793
+ });
794
+ }
795
+ const builtin = {
796
+ vertexIndex: defineBuiltin(u32, "vertex_index"),
797
+ instanceIndex: defineBuiltin(u32, "instance_index"),
798
+ clipDistances: defineBuiltin(arrayOf(u32, 8), "clip_distances"),
799
+ position: defineBuiltin(vec4f, "position"),
800
+ frontFacing: defineBuiltin(bool, "front_facing"),
801
+ fragDepth: defineBuiltin(f32, "frag_depth"),
802
+ primitiveIndex: defineBuiltin(u32, "primitive_index"),
803
+ sampleIndex: defineBuiltin(u32, "sample_index"),
804
+ sampleMask: defineBuiltin(u32, "sample_mask"),
805
+ localInvocationId: defineBuiltin(vec3u, "local_invocation_id"),
806
+ localInvocationIndex: defineBuiltin(u32, "local_invocation_index"),
807
+ globalInvocationId: defineBuiltin(vec3u, "global_invocation_id"),
808
+ workgroupId: defineBuiltin(vec3u, "workgroup_id"),
809
+ numWorkgroups: defineBuiltin(vec3u, "num_workgroups"),
810
+ subgroupInvocationId: defineBuiltin(u32, "subgroup_invocation_id"),
811
+ subgroupSize: defineBuiltin(u32, "subgroup_size"),
812
+ subgroupId: defineBuiltin(u32, "subgroup_id"),
813
+ numSubgroups: defineBuiltin(u32, "num_subgroups")
814
+ };
815
+
816
+ //#endregion
817
+ export { uint32x4 as $, packedFormats as A, sint8x4 as B, float16x4 as C, float32x4 as D, float32x3 as E, sint32x2 as F, snorm8x2 as G, snorm16x2 as H, sint32x3 as I, uint16x2 as J, snorm8x4 as K, sint32x4 as L, sint16x2 as M, sint16x4 as N, formatToWGSLType as O, sint32 as P, uint32x3 as Q, sint8 as R, float16x2 as S, float32x2 as T, snorm16x4 as U, snorm16 as V, snorm8 as W, uint32 as X, uint16x4 as Y, uint32x2 as Z, roundUp as _, AutoStruct as a, unorm16x2 as at, customAlignmentOf as b, getAttributesString as c, unorm8x2 as ct, isBuiltin as d, uint8 as et, location as f, getLayoutInfo as g, sizeOf as h, infixOperators as i, unorm16 as it, sint16 as j, isPackedData as k, interpolate as l, unorm8x4 as lt, PUBLIC_sizeOf as m, arrayOf as n, uint8x4 as nt, createIoSchema as o, unorm16x4 as ot, size as p, uint16 as q, accessProp as r, unorm10_10_10_2 as rt, align as s, unorm8 as st, builtin as t, uint8x2 as tt, invariant as u, unorm8x4_bgra as ut, PUBLIC_alignmentOf as v, float32 as w, float16 as x, alignmentOf as y, sint8x2 as z };
818
+ //# sourceMappingURL=builtin-ClEnM-Ye.js.map