typegpu 0.12.8 → 0.12.10

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 (119) hide show
  1. package/common/attachAutoResizer.d.ts +33 -0
  2. package/common/attachAutoResizer.js +83 -0
  3. package/common/index.d.ts +1 -0
  4. package/common/index.js +1 -0
  5. package/common/writeSoA.js +4 -3
  6. package/core/commandEncoder/commandEncoder.d.ts +1 -1
  7. package/core/declare/tgpuDeclare.d.ts +10 -7
  8. package/core/declare/tgpuDeclare.js +25 -10
  9. package/core/function/comptime.d.ts +2 -0
  10. package/core/function/comptime.js +29 -24
  11. package/core/function/createCallableSchema.js +32 -37
  12. package/core/function/dualImpl.js +45 -44
  13. package/core/function/tgpuComputeFn.d.ts +0 -3
  14. package/core/function/tgpuFn.d.ts +1 -0
  15. package/core/function/tgpuFn.js +14 -4
  16. package/core/function/tgpuFragmentFn.d.ts +1 -1
  17. package/core/function/tgpuVertexFn.d.ts +1 -1
  18. package/core/pipeline/computePipeline.js +5 -2
  19. package/core/pipeline/drawState.js +1 -1
  20. package/core/pipeline/renderPipeline.js +5 -2
  21. package/core/pipeline/webgpuLimitations.js +11 -3
  22. package/core/resolve/externals.js +4 -0
  23. package/core/resolve/namespace.d.ts +4 -0
  24. package/core/resolve/namespace.js +1 -2
  25. package/core/resolve/stitch.js +13 -8
  26. package/core/root/init.d.ts +3 -3
  27. package/core/root/init.js +12 -13
  28. package/core/root/rootTypes.d.ts +44 -37
  29. package/core/slot/accessor.js +4 -4
  30. package/core/texture/texture.d.ts +3 -3
  31. package/core/texture/texture.js +60 -44
  32. package/core/texture/textureFormats.d.ts +4 -3
  33. package/core/texture/textureFormats.js +17 -6
  34. package/core/texture/textureWrite.d.ts +9 -0
  35. package/core/texture/textureWrite.js +21 -0
  36. package/core/unroll/tgpuUnroll.js +2 -4
  37. package/core/vertexLayout/vertexAttribute.d.ts +1 -4
  38. package/core/vertexLayout/vertexLayout.js +11 -30
  39. package/data/alignmentOf.d.ts +1 -1
  40. package/data/alignmentOf.js +5 -12
  41. package/data/array.js +34 -2
  42. package/data/attributes.d.ts +16 -3
  43. package/data/attributes.js +11 -2
  44. package/data/compiledIO.js +3 -3
  45. package/data/dataIO.js +77 -111
  46. package/data/dataTypes.d.ts +0 -2
  47. package/data/dataTypes.js +3 -3
  48. package/data/disarray.js +2 -0
  49. package/data/index.d.ts +4 -3
  50. package/data/index.js +3 -2
  51. package/data/offsetUtils.js +23 -13
  52. package/data/offsets.js +1 -2
  53. package/data/partialIO.js +2 -3
  54. package/data/ref.js +35 -37
  55. package/data/schemaCallWrapper.js +2 -2
  56. package/data/schemaMemoryLayout.d.ts +6 -1
  57. package/data/schemaMemoryLayout.js +23 -14
  58. package/data/schemasEqual.d.ts +37 -0
  59. package/data/schemasEqual.js +122 -0
  60. package/data/sizeOf.d.ts +1 -1
  61. package/data/sizeOf.js +2 -3
  62. package/data/snippet.d.ts +1 -1
  63. package/data/struct.js +27 -1
  64. package/data/unstruct.js +2 -0
  65. package/data/vectorImpl.js +45 -21
  66. package/data/vertexFormatData.js +7 -9
  67. package/data/wgslTypes.d.ts +28 -13
  68. package/data/wgslTypes.js +33 -39
  69. package/extension.d.ts +0 -4
  70. package/package.json +2 -2
  71. package/resolutionCtx.d.ts +1 -1
  72. package/resolutionCtx.js +26 -7
  73. package/serial/restore.d.ts +4 -3
  74. package/serial/restore.js +2 -1
  75. package/shared/meta.js +1 -1
  76. package/shared/symbols.d.ts +5 -0
  77. package/shared/symbols.js +6 -1
  78. package/shared/utilityTypes.d.ts +0 -6
  79. package/std/bitcast.d.ts +1 -1
  80. package/std/comptime.d.ts +67 -0
  81. package/std/comptime.js +88 -0
  82. package/std/environment.js +2 -4
  83. package/std/index.d.ts +2 -0
  84. package/std/index.js +2 -0
  85. package/std/matrix.js +5 -5
  86. package/std/numeric.d.ts +2 -1
  87. package/std/numeric.js +47 -10
  88. package/std/typeOf.d.ts +31 -0
  89. package/std/typeOf.js +54 -0
  90. package/tgpu.d.ts +1 -1
  91. package/tgpu.js +1 -1
  92. package/tgpuBindGroupLayout.d.ts +6 -3
  93. package/tgpuBindGroupLayout.js +15 -2
  94. package/tgpuLogger.d.ts +3 -0
  95. package/tgpuLogger.js +3 -0
  96. package/tgpuUnstableNamespace.d.ts +1 -0
  97. package/tgpuUnstableNamespace.js +5 -0
  98. package/tgsl/accessIndex.js +10 -7
  99. package/tgsl/accessProp.js +5 -57
  100. package/tgsl/consoleLog/serializers.d.ts +1 -8
  101. package/tgsl/consoleLog/serializers.js +53 -37
  102. package/tgsl/consoleLog/types.d.ts +1 -1
  103. package/tgsl/conversion.js +21 -0
  104. package/tgsl/forOfUtils.js +1 -1
  105. package/tgsl/generationHelpers.d.ts +0 -1
  106. package/tgsl/generationHelpers.js +1 -4
  107. package/tgsl/shaderGenerator.d.ts +5 -0
  108. package/tgsl/shellless.js +3 -20
  109. package/tgsl/swizzle.d.ts +24 -0
  110. package/tgsl/swizzle.js +112 -0
  111. package/tgsl/wgslGenerator.d.ts +13 -3
  112. package/tgsl/wgslGenerator.js +150 -152
  113. package/types.d.ts +10 -10
  114. package/types.js +1 -1
  115. package/unwrapper.d.ts +2 -2
  116. package/data/deepEqual.d.ts +0 -21
  117. package/data/deepEqual.js +0 -93
  118. package/taskQueue.d.ts +0 -6
  119. package/taskQueue.js +0 -30
package/data/dataIO.js CHANGED
@@ -1,15 +1,64 @@
1
1
  import { BufferReader, BufferWriter, getSystemEndianness, } from 'typed-binary';
2
- import alignIO from "./alignIO.js";
3
- import { alignmentOf, customAlignmentOf } from "./alignmentOf.js";
4
2
  import { mat2x2f, mat3x3f, mat4x4f } from "./matrix.js";
3
+ import { offsetsForProps } from "./offsets.js";
5
4
  import { sizeOf } from "./sizeOf.js";
5
+ import { strideOf } from "./schemaMemoryLayout.js";
6
6
  import { vec2f, vec2h, vec2i, vec2u, vec3f, vec3h, vec3i, vec3u, vec4f, vec4h, vec4i, vec4u, } from "./vector.js";
7
7
  import * as wgsl from "./wgslTypes.js";
8
8
  import { getCompiledWriter } from "./compiledIO.js";
9
9
  import { getName } from "../shared/meta.js";
10
- import { roundUp } from "../mathUtils.js";
11
10
  import { logger } from "../tgpuLogger.js";
12
11
  import { readFloat16, writeFloat16 } from "./float16Conversion.js";
12
+ function writeStructLike(output, schema, value) {
13
+ // The parent has placed us at our offset
14
+ const beginning = output.currentByteOffset;
15
+ for (const [key, { offset }] of Object.entries(offsetsForProps(schema))) {
16
+ const property = schema.propTypes[key];
17
+ output.seekTo(beginning + offset);
18
+ writeData(output, property, value[key]);
19
+ }
20
+ output.seekTo(beginning + sizeOf(schema));
21
+ }
22
+ function writeArrayLike(output, schema, value) {
23
+ if (schema.elementCount === 0) {
24
+ throw new Error('Cannot write using a runtime-sized schema.');
25
+ }
26
+ // The parent has placed us at our offset
27
+ const beginning = output.currentByteOffset;
28
+ const stride = strideOf(schema);
29
+ for (let i = 0; i < Math.min(schema.elementCount, value.length); i++) {
30
+ output.seekTo(beginning + i * stride);
31
+ writeData(output, schema.elementType, value[i]);
32
+ }
33
+ output.seekTo(beginning + sizeOf(schema));
34
+ }
35
+ function readStructLike(input, schema) {
36
+ // The parent has placed us at our offset
37
+ const beginning = input.currentByteOffset;
38
+ const result = {};
39
+ for (const [key, { offset }] of Object.entries(offsetsForProps(schema))) {
40
+ input.seekTo(beginning + offset);
41
+ result[key] = readData(input, schema.propTypes[key]);
42
+ }
43
+ input.seekTo(beginning + sizeOf(schema));
44
+ return result;
45
+ }
46
+ function readArrayLike(input, schema) {
47
+ if (schema.elementCount === 0) {
48
+ throw new Error('Cannot read using a runtime-sized schema.');
49
+ }
50
+ // The parent has placed us at our offset
51
+ const beginning = input.currentByteOffset;
52
+ const stride = strideOf(schema);
53
+ const elements = [];
54
+ for (let i = 0; i < schema.elementCount; i++) {
55
+ input.seekTo(beginning + i * stride);
56
+ const value = readData(input, schema.elementType);
57
+ elements.push(value);
58
+ }
59
+ input.seekTo(beginning + sizeOf(schema));
60
+ return elements;
61
+ }
13
62
  const dataWriters = {
14
63
  bool() {
15
64
  throw new Error('Booleans are not host-shareable');
@@ -123,28 +172,8 @@ const dataWriters = {
123
172
  output.writeFloat32(value[i]);
124
173
  }
125
174
  },
126
- struct(output, schema, value) {
127
- const alignment = alignmentOf(schema);
128
- alignIO(output, alignment);
129
- for (const [key, property] of Object.entries(schema.propTypes)) {
130
- alignIO(output, alignmentOf(property));
131
- writeData(output, property, value[key]);
132
- }
133
- alignIO(output, alignment);
134
- },
135
- array(output, schema, value) {
136
- if (schema.elementCount === 0) {
137
- throw new Error('Cannot write using a runtime-sized schema.');
138
- }
139
- const alignment = alignmentOf(schema);
140
- alignIO(output, alignment);
141
- const beginning = output.currentByteOffset;
142
- for (let i = 0; i < Math.min(schema.elementCount, value.length); i++) {
143
- alignIO(output, alignment);
144
- writeData(output, schema.elementType, value[i]);
145
- }
146
- output.seekTo(beginning + sizeOf(schema));
147
- },
175
+ struct: writeStructLike,
176
+ array: writeArrayLike,
148
177
  ptr() {
149
178
  throw new Error('Pointers are not host-shareable');
150
179
  },
@@ -152,8 +181,6 @@ const dataWriters = {
152
181
  dataWriters[schema.inner.type]?.(output, schema, value);
153
182
  },
154
183
  decorated(output, schema, value) {
155
- const alignment = customAlignmentOf(schema);
156
- alignIO(output, alignment);
157
184
  const beginning = output.currentByteOffset;
158
185
  dataWriters[schema.inner?.type]?.(output, schema.inner, value);
159
186
  output.seekTo(beginning + sizeOf(schema));
@@ -238,17 +265,17 @@ const dataWriters = {
238
265
  output.writeInt16(value.w);
239
266
  },
240
267
  unorm16(output, _, value) {
241
- output.writeUint16(value * 65535);
268
+ output.writeUint16(Math.round(value * 65535));
242
269
  },
243
270
  unorm16x2(output, _, value) {
244
- output.writeUint16(value.x * 65535);
245
- output.writeUint16(value.y * 65535);
271
+ output.writeUint16(Math.round(value.x * 65535));
272
+ output.writeUint16(Math.round(value.y * 65535));
246
273
  },
247
274
  unorm16x4(output, _, value) {
248
- output.writeUint16(value.x * 65535);
249
- output.writeUint16(value.y * 65535);
250
- output.writeUint16(value.z * 65535);
251
- output.writeUint16(value.w * 65535);
275
+ output.writeUint16(Math.round(value.x * 65535));
276
+ output.writeUint16(Math.round(value.y * 65535));
277
+ output.writeUint16(Math.round(value.z * 65535));
278
+ output.writeUint16(Math.round(value.w * 65535));
252
279
  },
253
280
  snorm16(output, _, value) {
254
281
  output.writeInt16(Math.round(value * 32767));
@@ -332,37 +359,21 @@ const dataWriters = {
332
359
  },
333
360
  'unorm10-10-10-2'(output, _, value) {
334
361
  let packed = 0;
335
- packed |= ((value.x * 1023) & 1023) << 22; // r (10 bits)
336
- packed |= ((value.y * 1023) & 1023) << 12; // g (10 bits)
337
- packed |= ((value.z * 1023) & 1023) << 2; // b (10 bits)
338
- packed |= (value.w * 3) & 3; // a (2 bits)
362
+ packed |= (Math.round(value.x * 1023) & 1023) << 22; // r (10 bits)
363
+ packed |= (Math.round(value.y * 1023) & 1023) << 12; // g (10 bits)
364
+ packed |= (Math.round(value.z * 1023) & 1023) << 2; // b (10 bits)
365
+ packed |= Math.round(value.w * 3) & 3; // a (2 bits)
339
366
  output.writeUint32(packed);
340
367
  },
341
368
  'unorm8x4-bgra'(output, _, value) {
342
- output.writeUint8(value.z * 255);
343
- output.writeUint8(value.y * 255);
344
- output.writeUint8(value.x * 255);
345
- output.writeUint8(value.w * 255);
346
- },
347
- disarray(output, schema, value) {
348
- const alignment = alignmentOf(schema);
349
- alignIO(output, alignment);
350
- const beginning = output.currentByteOffset;
351
- for (let i = 0; i < Math.min(schema.elementCount, value.length); i++) {
352
- alignIO(output, alignment);
353
- dataWriters[schema.elementType?.type]?.(output, schema.elementType, value[i]);
354
- }
355
- output.seekTo(beginning + sizeOf(schema));
356
- },
357
- unstruct(output, schema, value) {
358
- const propTypes = schema.propTypes;
359
- for (const [key, property] of Object.entries(propTypes)) {
360
- dataWriters[property.type]?.(output, property, value[key]);
361
- }
369
+ output.writeUint8(Math.round(value.z * 255));
370
+ output.writeUint8(Math.round(value.y * 255));
371
+ output.writeUint8(Math.round(value.x * 255));
372
+ output.writeUint8(Math.round(value.w * 255));
362
373
  },
374
+ disarray: writeArrayLike,
375
+ unstruct: writeStructLike,
363
376
  'loose-decorated'(output, schema, value) {
364
- const alignment = customAlignmentOf(schema);
365
- alignIO(output, alignment);
366
377
  const beginning = output.currentByteOffset;
367
378
  const writer = dataWriters[schema.inner?.type];
368
379
  writer?.(output, schema.inner, value);
@@ -478,33 +489,8 @@ const dataReaders = {
478
489
  //
479
490
  input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32());
480
491
  },
481
- struct(input, schema) {
482
- const alignment = alignmentOf(schema);
483
- alignIO(input, alignment);
484
- const result = {};
485
- const propTypes = schema.propTypes;
486
- for (const [key, property] of Object.entries(propTypes)) {
487
- alignIO(input, alignmentOf(property));
488
- result[key] = readData(input, property);
489
- }
490
- alignIO(input, alignment);
491
- return result;
492
- },
493
- array(input, schema) {
494
- if (schema.elementCount === 0) {
495
- throw new Error('Cannot read using a runtime-sized schema.');
496
- }
497
- const alignment = alignmentOf(schema);
498
- const elements = [];
499
- for (let i = 0; i < schema.elementCount; i++) {
500
- alignIO(input, alignment);
501
- const elementType = schema.elementType;
502
- const value = readData(input, elementType);
503
- elements.push(value);
504
- }
505
- alignIO(input, alignment);
506
- return elements;
507
- },
492
+ struct: readStructLike,
493
+ array: readArrayLike,
508
494
  ptr() {
509
495
  throw new Error('Pointers are not host-shareable');
510
496
  },
@@ -512,8 +498,6 @@ const dataReaders = {
512
498
  return readData(input, schema.inner);
513
499
  },
514
500
  decorated(input, schema) {
515
- const alignment = customAlignmentOf(schema);
516
- alignIO(input, alignment);
517
501
  const beginning = input.currentByteOffset;
518
502
  const value = readData(input, schema.inner);
519
503
  input.seekTo(beginning + sizeOf(schema));
@@ -565,7 +549,7 @@ const dataReaders = {
565
549
  sint32x4: (i) => vec4i(i.readInt32(), i.readInt32(), i.readInt32(), i.readInt32()),
566
550
  'unorm10-10-10-2'(i) {
567
551
  const packed = i.readUint32();
568
- const r = (packed >> 22) / 1023;
552
+ const r = (packed >>> 22) / 1023;
569
553
  const g = ((packed >> 12) & 1023) / 1023;
570
554
  const b = ((packed >> 2) & 1023) / 1023;
571
555
  const a = (packed & 3) / 3;
@@ -578,26 +562,9 @@ const dataReaders = {
578
562
  const a = i.readByte() / 255;
579
563
  return vec4f(r, g, b, a);
580
564
  },
581
- unstruct(input, schema) {
582
- const result = {};
583
- const propTypes = schema.propTypes;
584
- for (const [key, property] of Object.entries(propTypes)) {
585
- result[key] = readData(input, property);
586
- }
587
- return result;
588
- },
589
- disarray(input, schema) {
590
- const alignment = alignmentOf(schema);
591
- const elements = [];
592
- for (let i = 0; i < schema.elementCount; i++) {
593
- alignIO(input, alignment);
594
- elements.push(readData(input, schema.elementType));
595
- }
596
- alignIO(input, alignment);
597
- return elements;
598
- },
565
+ unstruct: readStructLike,
566
+ disarray: readArrayLike,
599
567
  'loose-decorated'(input, schema) {
600
- alignIO(input, customAlignmentOf(schema));
601
568
  const beginning = input.currentByteOffset;
602
569
  const value = readData(input, schema.inner);
603
570
  input.seekTo(beginning + sizeOf(schema));
@@ -619,8 +586,7 @@ export function calculateOffsets(options, schema, data) {
619
586
  let naturalSize = undefined;
620
587
  if (wgsl.isWgslArray(schema) && Array.isArray(data)) {
621
588
  const arrayData = data;
622
- naturalSize =
623
- arrayData.length * roundUp(sizeOf(schema.elementType), alignmentOf(schema.elementType));
589
+ naturalSize = arrayData.length * strideOf(schema);
624
590
  }
625
591
  else if (ArrayBuffer.isView(data) || data instanceof ArrayBuffer) {
626
592
  naturalSize = data.byteLength;
@@ -55,8 +55,6 @@ export interface Unstruct<out TProps extends Record<string, wgsl.BaseData> = Rec
55
55
  }[keyof TProps] extends true ? true : false;
56
56
  readonly [$invalidSchemaReason]: 'Unstructs are not host-shareable, use structs instead';
57
57
  }
58
- /** @deprecated Just use `Unstruct` without any type parameters */
59
- export type AnyUnstruct = Unstruct;
60
58
  export interface LooseDecorated<out TInner extends wgsl.BaseData = wgsl.BaseData, out TAttribs extends unknown[] = unknown[]> extends wgsl.BaseData {
61
59
  readonly type: 'loose-decorated';
62
60
  readonly inner: TInner;
package/data/dataTypes.js CHANGED
@@ -36,7 +36,7 @@ export function isLooseData(data) {
36
36
  * isDisarray(d.vec3f) // false
37
37
  */
38
38
  export function isDisarray(schema) {
39
- return isMarkedInternal(schema) && schema?.type === 'disarray';
39
+ return schema?.type === 'disarray' && isMarkedInternal(schema);
40
40
  }
41
41
  /**
42
42
  * Checks whether passed in value is a unstruct schema,
@@ -52,10 +52,10 @@ export function isDisarray(schema) {
52
52
  * isUnstruct(d.vec3f) // false
53
53
  */
54
54
  export function isUnstruct(schema) {
55
- return isMarkedInternal(schema) && schema?.type === 'unstruct';
55
+ return schema?.type === 'unstruct' && isMarkedInternal(schema);
56
56
  }
57
57
  export function isLooseDecorated(value) {
58
- return isMarkedInternal(value) && value?.type === 'loose-decorated';
58
+ return value?.type === 'loose-decorated' && isMarkedInternal(value);
59
59
  }
60
60
  export function getCustomAlignment(data) {
61
61
  return data.attribs?.find(wgsl.isAlignAttrib)
package/data/disarray.js CHANGED
@@ -56,3 +56,5 @@ const DisarrayImpl = {
56
56
  return `disarrayOf(${this.elementType}, ${this.elementCount})`;
57
57
  },
58
58
  };
59
+ // Schemas are still functions, so they should keep `call`, `apply`, `bind`, etc.
60
+ Object.setPrototypeOf(DisarrayImpl, Function.prototype);
package/data/index.d.ts CHANGED
@@ -19,9 +19,9 @@ export declare const vec4u: import("./wgslTypes.ts").Vec4u;
19
19
  export declare const mat2x2f: import("./wgslTypes.ts").Mat2x2f;
20
20
  export declare const mat3x3f: import("./wgslTypes.ts").Mat3x3f;
21
21
  export declare const mat4x4f: import("./wgslTypes.ts").Mat4x4f;
22
- export { bool, f16, f32, i32, u16, u32 } from './numeric.ts';
22
+ export { abstractFloat, abstractInt, bool, f16, f32, i32, u16, u32 } from './numeric.ts';
23
23
  export { isAlignAttrib, isAtomic, isBuiltinAttrib, isDecorated, isInterpolateAttrib, isInvariantAttrib, isLocationAttrib, isPtr, isSizeAttrib, isWgslArray, isWgslData, isWgslStruct, Void, } from './wgslTypes.ts';
24
- export type { Align, AnyVecInstance, AnyWgslData, AnyWgslStruct, Atomic, atomicI32, atomicU32, BaseData, BaseData as BaseWgslData, Bool, Builtin, Decorated, F16, F32, I32, Interpolate, Invariant, Location, m2x2f, m3x3f, m4x4f, Mat2x2f, Mat3x3f, Mat4x4f, matBase, Ptr, Size, StorableData, U16, U32, v2b, v2f, v2h, v2i, v2u, v3b, v3f, v3h, v3i, v3u, v4b, v4f, v4h, v4i, v4u, Vec2b, Vec2f, Vec2h, Vec2i, Vec2u, Vec3b, Vec3f, Vec3h, Vec3i, Vec3u, Vec4b, Vec4f, Vec4h, Vec4i, Vec4u, vecBase, WgslArray, WgslStruct, } from './wgslTypes.ts';
24
+ export type { AbstractFloat, AbstractInt, Align, AnyVecInstance, AnyWgslData, AnyWgslStruct, Atomic, atomicI32, atomicU32, BaseData, BaseData as BaseWgslData, Bool, Builtin, Decorated, F16, F32, I32, Interpolate, Invariant, Location, m2x2f, m3x3f, m4x4f, Mat2x2f, Mat3x3f, Mat4x4f, matBase, Ptr, Size, StorableData, U16, U32, v2b, v2f, v2h, v2i, v2u, v3b, v3f, v3h, v3i, v3u, v4b, v4f, v4h, v4i, v4u, Vec2b, Vec2f, Vec2h, Vec2i, Vec2u, Vec3b, Vec3f, Vec3h, Vec3i, Vec3u, Vec4b, Vec4f, Vec4h, Vec4i, Vec4u, vecBase, WgslArray, WgslStruct, } from './wgslTypes.ts';
25
25
  export { struct } from './struct.ts';
26
26
  export { arrayOf } from './array.ts';
27
27
  export { ptrFn, ptrHandle, ptrPrivate, ptrStorage, ptrUniform, ptrWorkgroup } from './ptr.ts';
@@ -42,6 +42,7 @@ export { PUBLIC_getLongestContiguousPrefix as getLongestContiguousPrefix } from
42
42
  export { memoryLayoutOf } from './offsetUtils.ts';
43
43
  export { PUBLIC_alignmentOf as alignmentOf } from './alignmentOf.ts';
44
44
  export { builtin } from '../builtin.ts';
45
- export { deepEqual } from './deepEqual.ts';
45
+ export { schemasEqualStructurally, schemasEqualNominally,
46
+ /** @deprecated Use `schemasEqualStructurally` instead */ schemasEqualStructurally as deepEqual, } from './schemasEqual.ts';
46
47
  export type { AnyBuiltin, BuiltinClipDistances, BuiltinFragDepth, BuiltinFrontFacing, BuiltinGlobalInvocationId, BuiltinGlobalInvocationIndex, BuiltinInstanceIndex, BuiltinLocalInvocationId, BuiltinLocalInvocationIndex, BuiltinNumSubgroups, BuiltinNumWorkgroups, BuiltinPosition, BuiltinPrimitiveIndex, BuiltinSampleIndex, BuiltinSampleMask, BuiltinSubgroupId, BuiltinSubgroupInvocationId, BuiltinSubgroupSize, BuiltinVertexIndex, BuiltinWorkgroupId, BuiltinWorkgroupIndex, } from '../builtin.ts';
47
48
  export type { Infer, InferGPU, InferInput, InferPartial, InferPatch, MemIdentity, } from '../shared/repr.ts';
package/data/index.js CHANGED
@@ -23,7 +23,7 @@ import { mat2x2f as _mat2x2f, mat3x3f as _mat3x3f, mat4x4f as _mat4x4f } from ".
23
23
  export const mat2x2f = (() => (assignInfixOperators(), _mat2x2f))();
24
24
  export const mat3x3f = (() => (assignInfixOperators(), _mat3x3f))();
25
25
  export const mat4x4f = (() => (assignInfixOperators(), _mat4x4f))();
26
- export { bool, f16, f32, i32, u16, u32 } from "./numeric.js";
26
+ export { abstractFloat, abstractInt, bool, f16, f32, i32, u16, u32 } from "./numeric.js";
27
27
  export { isAlignAttrib, isAtomic, isBuiltinAttrib, isDecorated, isInterpolateAttrib, isInvariantAttrib, isLocationAttrib, isPtr, isSizeAttrib, isWgslArray, isWgslData, isWgslStruct, Void, } from "./wgslTypes.js";
28
28
  export { struct } from "./struct.js";
29
29
  export { arrayOf } from "./array.js";
@@ -44,4 +44,5 @@ export { PUBLIC_getLongestContiguousPrefix as getLongestContiguousPrefix } from
44
44
  export { memoryLayoutOf } from "./offsetUtils.js";
45
45
  export { PUBLIC_alignmentOf as alignmentOf } from "./alignmentOf.js";
46
46
  export { builtin } from "../builtin.js";
47
- export { deepEqual } from "./deepEqual.js";
47
+ export { schemasEqualStructurally, schemasEqualNominally,
48
+ /** @deprecated Use `schemasEqualStructurally` instead */ schemasEqualStructurally as deepEqual, } from "./schemasEqual.js";
@@ -1,11 +1,11 @@
1
- import { roundUp } from "../mathUtils.js";
2
- import { alignmentOf } from "./alignmentOf.js";
3
1
  import { offsetsForProps } from "./offsets.js";
4
2
  import { sizeOf } from "./sizeOf.js";
3
+ import { strideOf } from "./schemaMemoryLayout.js";
5
4
  import { isContiguous } from "./isContiguous.js";
6
5
  import { getLongestContiguousPrefix } from "./getLongestContiguousPrefix.js";
7
6
  import { isMat, isVec, isWgslArray, isWgslStruct } from "./wgslTypes.js";
8
7
  import { isDisarray, isUnstruct, undecorate } from "./dataTypes.js";
8
+ import { isPackedData } from "./vertexFormatData.js";
9
9
  import { vec2f, vec3f, vec4f } from "./vector.js";
10
10
  /**
11
11
  * The absolute byte offset of the node, counted from the start of the root schema.
@@ -42,26 +42,23 @@ function getMarker(target, prop) {
42
42
  }
43
43
  return undefined;
44
44
  }
45
- function throwIfLoose(schema) {
46
- if (isUnstruct(schema) || isDisarray(schema)) {
47
- throw new Error(`memoryLayoutOf: loose schemas are not supported (encountered '${schema.type}').`);
48
- }
49
- }
50
45
  function makeProxy(schema, baseOffset, contiguous) {
51
46
  const unwrapped = undecorate(schema);
52
- throwIfLoose(unwrapped);
53
47
  if (isVec(unwrapped)) {
54
48
  return makeVecProxy(unwrapped, scalarNode(baseOffset, contiguous));
55
49
  }
56
50
  if (isMat(unwrapped)) {
57
51
  return makeMatProxy(unwrapped, scalarNode(baseOffset, contiguous));
58
52
  }
59
- if (isWgslStruct(unwrapped)) {
53
+ if (isWgslStruct(unwrapped) || isUnstruct(unwrapped)) {
60
54
  return makeStructProxy(unwrapped, scalarNode(baseOffset, contiguous));
61
55
  }
62
- if (isWgslArray(unwrapped)) {
56
+ if (isWgslArray(unwrapped) || isDisarray(unwrapped)) {
63
57
  return makeArrayProxy(unwrapped, scalarNode(baseOffset, contiguous));
64
58
  }
59
+ if (isPackedData(unwrapped)) {
60
+ return makePackedProxy(unwrapped, scalarNode(baseOffset, contiguous));
61
+ }
65
62
  return scalarNode(baseOffset, contiguous);
66
63
  }
67
64
  export function createOffsetProxy(schema, baseOffset = 0) {
@@ -104,6 +101,20 @@ function makeVecProxy(vec, parent) {
104
101
  },
105
102
  });
106
103
  }
104
+ function makePackedProxy(packed, parent) {
105
+ return new Proxy(parent, {
106
+ get(t, prop) {
107
+ const marker = getMarker(t, prop);
108
+ if (marker !== undefined) {
109
+ return marker;
110
+ }
111
+ if (typeof prop === 'string' && vecPropToIndex[prop] !== undefined) {
112
+ throw new Error(`memoryLayoutOf: accessing components of packed vertex format '${packed.type}' is not supported.`);
113
+ }
114
+ return undefined;
115
+ },
116
+ });
117
+ }
107
118
  function makeMatProxy(mat, parent) {
108
119
  const [columnCount, columnSchema] = mat.type === 'mat2x2f' ? [2, vec2f] : mat.type === 'mat3x3f' ? [3, vec3f] : [4, vec4f];
109
120
  const elementSize = 4;
@@ -166,8 +177,8 @@ function makeMatProxy(mat, parent) {
166
177
  }
167
178
  function makeArrayProxy(array, parent) {
168
179
  const elementType = array.elementType;
169
- const elementSize = sizeOf(elementType);
170
- const stride = roundUp(elementSize, alignmentOf(elementType));
180
+ const elementSize = sizeOf(undecorate(elementType));
181
+ const stride = strideOf(array);
171
182
  const hasPadding = stride > elementSize;
172
183
  const elementLCP = getLongestContiguousPrefix(elementType);
173
184
  const remainingFromParent = parent[CONTIGUOUS_MARKER];
@@ -287,7 +298,6 @@ function makeStructProxy(struct, parent) {
287
298
  * @returns An object containing the offset and contiguous byte information.
288
299
  */
289
300
  export function memoryLayoutOf(schema, accessor) {
290
- throwIfLoose(undecorate(schema));
291
301
  if (!accessor) {
292
302
  return {
293
303
  offset: 0,
package/data/offsets.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { Measurer } from 'typed-binary';
2
- import { roundUp } from "../mathUtils.js";
3
2
  import alignIO from "./alignIO.js";
4
3
  import { alignmentOf, customAlignmentOf } from "./alignmentOf.js";
5
4
  import { isUnstruct } from "./dataTypes.js";
@@ -29,7 +28,7 @@ export function offsetsForProps(struct) {
29
28
  measurer.add(propSize);
30
29
  }
31
30
  if (lastEntry) {
32
- lastEntry.padding = roundUp(sizeOf(struct), alignmentOf(struct)) - measurer.size;
31
+ lastEntry.padding = sizeOf(struct) - measurer.size;
33
32
  }
34
33
  cachedOffsets.set(struct, offsets);
35
34
  return offsets;
package/data/partialIO.js CHANGED
@@ -1,11 +1,10 @@
1
1
  import { BufferWriter, getSystemEndianness } from 'typed-binary';
2
- import { roundUp } from "../mathUtils.js";
3
- import { alignmentOf } from "./alignmentOf.js";
4
2
  import { getCompiledWriter } from "./compiledIO.js";
5
3
  import { writeData } from "./dataIO.js";
6
4
  import { isDisarray, isUnstruct } from "./dataTypes.js";
7
5
  import { offsetsForProps } from "./offsets.js";
8
6
  import { sizeOf } from "./sizeOf.js";
7
+ import { strideOf } from "./schemaMemoryLayout.js";
9
8
  import { isWgslArray, isWgslStruct } from "./wgslTypes.js";
10
9
  /**
11
10
  * Converts `{idx, value}[]` sparse arrays into `Record<number, T>` format.
@@ -70,7 +69,7 @@ export function getPatchInstructions(schema, data, targetBuffer, validateRange)
70
69
  }
71
70
  if (isWgslArray(node) || isDisarray(node)) {
72
71
  const arrSchema = node;
73
- const elementSize = roundUp(sizeOf(arrSchema.elementType), alignmentOf(arrSchema.elementType));
72
+ const elementSize = strideOf(arrSchema);
74
73
  const elementPadding = elementSize - sizeOf(arrSchema.elementType);
75
74
  if (ArrayBuffer.isView(value)) {
76
75
  const copyLen = Math.min(value.byteLength, arrSchema.elementCount * elementSize);
package/data/ref.js CHANGED
@@ -13,49 +13,47 @@ export const _ref = (() => {
13
13
  setName(impl, 'ref');
14
14
  impl.toString = () => 'ref';
15
15
  impl[$internal] = true;
16
- impl[$gpuCallable] = {
17
- call(ctx, [value]) {
18
- if (value.origin === 'argument') {
19
- throw new WgslTypeError(`d.ref(${stringifySnippet(value)}) is illegal, cannot take a reference of an argument. Copy the value first, and take a reference of the copy.`);
20
- }
21
- if (value.origin === 'constant-immutable-def' || value.origin === 'runtime-immutable-def') {
22
- const typeStr = ctx.resolve(value.dataType).value;
23
- throw new WgslTypeError(`d.ref(${stringifySnippet(value)}) is illegal, cannot take a reference to a constant.
16
+ impl[$gpuCallable] = (ctx, [value]) => {
17
+ if (value.origin === 'argument') {
18
+ throw new WgslTypeError(`d.ref(${stringifySnippet(value)}) is illegal, cannot take a reference of an argument. Copy the value first, and take a reference of the copy.`);
19
+ }
20
+ if (value.origin === 'constant-immutable-def' || value.origin === 'runtime-immutable-def') {
21
+ const typeStr = ctx.resolve(value.dataType).value;
22
+ throw new WgslTypeError(`d.ref(${stringifySnippet(value)}) is illegal, cannot take a reference to a constant.
24
23
  -----
25
24
  - Try 'd.ref(${typeStr}(${stringifySnippet(value)}));' instead to create a new referencable value.
26
25
  -----`);
27
- }
28
- if (isStoredInMemory(value) && isNaturallyEphemeral(value.dataType)) {
29
- const typeStr = ctx.resolve(value.dataType).value;
30
- throw new WgslTypeError(`d.ref(${stringifySnippet(value)}) is illegal, cannot take a reference to a scalar value.
26
+ }
27
+ if (isStoredInMemory(value) && isNaturallyEphemeral(value.dataType)) {
28
+ const typeStr = ctx.resolve(value.dataType).value;
29
+ throw new WgslTypeError(`d.ref(${stringifySnippet(value)}) is illegal, cannot take a reference to a scalar value.
31
30
  -----
32
31
  - Try 'd.ref(${typeStr}(${stringifySnippet(value)}));' instead to create a new referencable scalar.
33
32
  -----`);
34
- }
35
- if (isPtr(value.dataType)) {
36
- // This can happen if we take a reference of an *implicit* pointer, one
37
- // made by assigning a reference to a `const`.
38
- return withDataType(explicitFrom(value.dataType), value);
39
- }
40
- if (value.dataType === UnknownData || isVoid(value.dataType)) {
41
- throw new WgslTypeError(`d.ref(${stringifySnippet(value)}) is illegal, cannot determine the WGSL type of '${stringifySnippet(value)}'.`);
42
- }
43
- /**
44
- * The value is addressable only if the ref was created from a reference (buttery-butter).
45
- *
46
- * @example
47
- * ```ts
48
- * const life = ref(42); // created from a value, has to be stored in a variable
49
- * const boid = ref(layout.$.boids[0]); // created from a reference
50
- * ```
51
- */
52
- const ptrType = createPtrFromOrigin(value.origin, value.dataType) ??
53
- // Values that are not addressable become addressable once stored in a
54
- // function-scope variable (`const life = d.ref(42)`), so that's the type they get.
55
- ptrFn(concretize(value.dataType));
56
- return snip(new RefOperator(value, ptrType), ptrType,
57
- /* origin */ 'runtime', value.possibleSideEffects);
58
- },
33
+ }
34
+ if (isPtr(value.dataType)) {
35
+ // This can happen if we take a reference of an *implicit* pointer, one
36
+ // made by assigning a reference to a `const`.
37
+ return withDataType(explicitFrom(value.dataType), value);
38
+ }
39
+ if (value.dataType === UnknownData || isVoid(value.dataType)) {
40
+ throw new WgslTypeError(`d.ref(${stringifySnippet(value)}) is illegal, cannot determine the WGSL type of '${stringifySnippet(value)}'.`);
41
+ }
42
+ /**
43
+ * The value is addressable only if the ref was created from a reference (buttery-butter).
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * const life = ref(42); // created from a value, has to be stored in a variable
48
+ * const boid = ref(layout.$.boids[0]); // created from a reference
49
+ * ```
50
+ */
51
+ const ptrType = createPtrFromOrigin(value.origin, value.dataType) ??
52
+ // Values that are not addressable become addressable once stored in a
53
+ // function-scope variable (`const life = d.ref(42)`), so that's the type they get.
54
+ ptrFn(concretize(value.dataType));
55
+ return snip(new RefOperator(value, ptrType), ptrType,
56
+ /* origin */ 'runtime', value.possibleSideEffects);
59
57
  };
60
58
  return impl;
61
59
  })();
@@ -32,6 +32,6 @@ export function schemaCallWrapperGPU(ctx, schema, item) {
32
32
  }
33
33
  const callSchema = schema;
34
34
  return item === undefined || item.value === undefined
35
- ? callSchema[$gpuCallable].call(ctx, [])
36
- : callSchema[$gpuCallable].call(ctx, [item]);
35
+ ? callSchema[$gpuCallable](ctx, [])
36
+ : callSchema[$gpuCallable](ctx, [item]);
37
37
  }
@@ -1,8 +1,13 @@
1
- import type { BaseData } from './wgslTypes.ts';
1
+ import type { Disarray } from './dataTypes.ts';
2
+ import type { BaseData, WgslArray } from './wgslTypes.ts';
2
3
  interface SchemaMemoryLayout {
3
4
  isContiguous: boolean;
4
5
  size: number;
5
6
  longestContiguousPrefix: number;
6
7
  }
8
+ /**
9
+ * Returns the distance (in bytes) between the starts of consecutive elements of an array.
10
+ */
11
+ export declare function strideOf(schema: WgslArray | Disarray): number;
7
12
  export declare function getLayoutInfo<T extends keyof SchemaMemoryLayout>(schema: BaseData, key: T): SchemaMemoryLayout[T];
8
13
  export {};