typegpu 0.10.2 → 0.11.0-rc.1

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 (211) hide show
  1. package/_virtual/_rolldown/runtime.js +6 -11
  2. package/builtin.js +1 -3
  3. package/common/fullScreenTriangle.d.ts +1 -5
  4. package/common/fullScreenTriangle.js +1 -3
  5. package/common/index.d.ts +3 -2
  6. package/common/index.js +6 -4
  7. package/common/writeSoA.d.ts +16 -0
  8. package/common/writeSoA.js +90 -0
  9. package/core/buffer/buffer.d.ts +12 -7
  10. package/core/buffer/buffer.js +102 -53
  11. package/core/buffer/bufferShorthand.d.ts +5 -5
  12. package/core/buffer/bufferShorthand.js +9 -5
  13. package/core/buffer/bufferUsage.d.ts +0 -2
  14. package/core/buffer/bufferUsage.js +6 -4
  15. package/core/constant/tgpuConstant.d.ts +2 -1
  16. package/core/constant/tgpuConstant.js +8 -7
  17. package/core/declare/tgpuDeclare.js +8 -9
  18. package/core/function/autoIO.d.ts +7 -6
  19. package/core/function/autoIO.js +1 -3
  20. package/core/function/comptime.js +1 -3
  21. package/core/function/createCallableSchema.js +4 -6
  22. package/core/function/dualImpl.js +1 -3
  23. package/core/function/entryInputRouter.js +39 -0
  24. package/core/function/extractArgs.js +2 -2
  25. package/core/function/fnCore.js +19 -8
  26. package/core/function/fnTypes.d.ts +14 -8
  27. package/core/function/ioSchema.js +24 -3
  28. package/core/function/shelllessImpl.js +1 -3
  29. package/core/function/templateUtils.js +1 -2
  30. package/core/function/tgpuComputeFn.d.ts +2 -3
  31. package/core/function/tgpuComputeFn.js +9 -16
  32. package/core/function/tgpuFn.d.ts +2 -2
  33. package/core/function/tgpuFn.js +1 -3
  34. package/core/function/tgpuFragmentFn.d.ts +5 -1
  35. package/core/function/tgpuFragmentFn.js +5 -10
  36. package/core/function/tgpuVertexFn.d.ts +4 -0
  37. package/core/function/tgpuVertexFn.js +6 -12
  38. package/core/pipeline/applyPipelineState.js +1 -3
  39. package/core/pipeline/computePipeline.d.ts +2 -6
  40. package/core/pipeline/computePipeline.js +64 -63
  41. package/core/pipeline/connectAttachmentToShader.js +1 -3
  42. package/core/pipeline/connectTargetsToShader.js +1 -3
  43. package/core/pipeline/limitsOverflow.js +1 -2
  44. package/core/pipeline/pipelineUtils.js +29 -0
  45. package/core/pipeline/renderPipeline.d.ts +23 -5
  46. package/core/pipeline/renderPipeline.js +32 -14
  47. package/core/pipeline/timeable.d.ts +0 -3
  48. package/core/pipeline/timeable.js +3 -9
  49. package/core/pipeline/typeGuards.js +1 -3
  50. package/core/querySet/querySet.d.ts +0 -2
  51. package/core/querySet/querySet.js +37 -36
  52. package/core/rawCodeSnippet/tgpuRawCodeSnippet.js +1 -3
  53. package/core/resolve/externals.d.ts +0 -2
  54. package/core/resolve/externals.js +2 -4
  55. package/core/resolve/namespace.js +1 -3
  56. package/core/resolve/resolveData.js +1 -3
  57. package/core/resolve/stitch.js +1 -3
  58. package/core/resolve/tgpuResolve.d.ts +3 -1
  59. package/core/resolve/tgpuResolve.js +3 -5
  60. package/core/root/configurableImpl.js +2 -3
  61. package/core/root/init.d.ts +0 -5
  62. package/core/root/init.js +35 -28
  63. package/core/root/rootTypes.d.ts +25 -5
  64. package/core/sampler/sampler.d.ts +0 -4
  65. package/core/sampler/sampler.js +3 -3
  66. package/core/simulate/tgpuSimulate.js +1 -3
  67. package/core/slot/accessor.d.ts +0 -4
  68. package/core/slot/accessor.js +1 -3
  69. package/core/slot/internalSlots.js +1 -3
  70. package/core/slot/lazy.js +1 -3
  71. package/core/slot/slot.js +2 -3
  72. package/core/slot/slotTypes.js +1 -3
  73. package/core/texture/externalTexture.d.ts +0 -6
  74. package/core/texture/externalTexture.js +2 -3
  75. package/core/texture/texture.d.ts +0 -4
  76. package/core/texture/texture.js +5 -3
  77. package/core/texture/textureFormats.js +1 -3
  78. package/core/texture/textureUtils.js +1 -3
  79. package/core/texture/usageExtension.js +1 -3
  80. package/core/unroll/tgpuUnroll.d.ts +58 -3
  81. package/core/unroll/tgpuUnroll.js +63 -5
  82. package/core/valueProxyUtils.js +1 -3
  83. package/core/variable/tgpuVariable.js +1 -3
  84. package/core/vertexLayout/connectAttributesToShader.js +1 -3
  85. package/core/vertexLayout/vertexLayout.js +9 -9
  86. package/data/alignIO.js +1 -2
  87. package/data/alignmentOf.d.ts +0 -1
  88. package/data/alignmentOf.js +1 -3
  89. package/data/array.d.ts +1 -3
  90. package/data/array.js +2 -4
  91. package/data/atomic.js +2 -3
  92. package/data/attributes.js +3 -3
  93. package/data/autoStruct.d.ts +1 -3
  94. package/data/autoStruct.js +1 -3
  95. package/data/compiledIO.js +83 -86
  96. package/data/dataIO.js +46 -39
  97. package/data/dataTypes.d.ts +7 -7
  98. package/data/dataTypes.js +6 -3
  99. package/data/deepEqual.js +1 -3
  100. package/data/disarray.d.ts +1 -3
  101. package/data/disarray.js +1 -3
  102. package/data/getLongestContiguousPrefix.d.ts +0 -1
  103. package/data/getLongestContiguousPrefix.js +1 -3
  104. package/data/index.d.ts +3 -3
  105. package/data/index.js +10 -3
  106. package/data/isContiguous.d.ts +0 -1
  107. package/data/isContiguous.js +1 -3
  108. package/data/matrix.d.ts +8 -10
  109. package/data/matrix.js +32 -18
  110. package/data/numberOps.js +1 -2
  111. package/data/numeric.js +16 -29
  112. package/data/offsetUtils.d.ts +2 -2
  113. package/data/offsetUtils.js +3 -5
  114. package/data/offsets.js +1 -3
  115. package/data/partialIO.js +84 -39
  116. package/data/ptr.d.ts +0 -1
  117. package/data/ptr.js +1 -3
  118. package/data/ref.d.ts +0 -3
  119. package/data/ref.js +1 -3
  120. package/data/sampler.js +1 -3
  121. package/data/schemaCallWrapper.js +1 -3
  122. package/data/schemaMemoryLayout.js +1 -3
  123. package/data/sizeOf.d.ts +0 -1
  124. package/data/sizeOf.js +1 -3
  125. package/data/snippet.js +12 -3
  126. package/data/struct.js +1 -3
  127. package/data/texture.js +1 -3
  128. package/data/unstruct.js +1 -3
  129. package/data/vector.js +4 -12
  130. package/data/vectorImpl.js +27 -28
  131. package/data/vectorOps.js +20 -3
  132. package/data/vertexFormatData.js +2 -3
  133. package/data/wgslTypes.d.ts +39 -11
  134. package/data/wgslTypes.js +10 -3
  135. package/errors.js +6 -3
  136. package/execMode.js +1 -3
  137. package/extension.js +1 -3
  138. package/getGPUValue.js +1 -3
  139. package/index.d.ts +4 -2
  140. package/index.js +3 -3
  141. package/indexNamedExports.d.ts +3 -1
  142. package/mathUtils.js +1 -2
  143. package/memo.js +8 -8
  144. package/nameRegistry.js +1 -3
  145. package/package.js +2 -3
  146. package/package.json +5 -5
  147. package/resolutionCtx.d.ts +0 -10
  148. package/resolutionCtx.js +84 -18
  149. package/shared/env.js +1 -2
  150. package/shared/generators.js +1 -2
  151. package/shared/meta.js +1 -3
  152. package/shared/repr.d.ts +32 -2
  153. package/shared/stringify.js +1 -3
  154. package/shared/symbols.d.ts +10 -1
  155. package/shared/symbols.js +10 -33
  156. package/shared/utilityTypes.d.ts +6 -2
  157. package/shared/utilityTypes.js +1 -2
  158. package/shared/vertexFormat.js +1 -2
  159. package/std/array.d.ts +1 -1
  160. package/std/array.js +1 -3
  161. package/std/atomic.d.ts +12 -12
  162. package/std/atomic.js +1 -3
  163. package/std/bitcast.d.ts +2 -2
  164. package/std/bitcast.js +1 -3
  165. package/std/boolean.d.ts +30 -16
  166. package/std/boolean.js +37 -12
  167. package/std/derivative.d.ts +9 -9
  168. package/std/derivative.js +1 -3
  169. package/std/discard.d.ts +1 -1
  170. package/std/discard.js +1 -3
  171. package/std/extensions.d.ts +1 -3
  172. package/std/extensions.js +1 -3
  173. package/std/index.d.ts +5 -4
  174. package/std/index.js +8 -5
  175. package/std/matrix.d.ts +5 -5
  176. package/std/matrix.js +1 -3
  177. package/std/numeric.d.ts +78 -132
  178. package/std/numeric.js +1 -3
  179. package/std/operators.d.ts +16 -8
  180. package/std/operators.js +80 -6
  181. package/std/packing.d.ts +4 -4
  182. package/std/packing.js +1 -3
  183. package/std/range.d.ts +24 -0
  184. package/std/range.js +38 -0
  185. package/std/subgroup.d.ts +21 -21
  186. package/std/subgroup.js +1 -3
  187. package/std/texture.d.ts +20 -11
  188. package/std/texture.js +13 -3
  189. package/tgpu.js +1 -3
  190. package/tgpuBindGroupLayout.js +9 -8
  191. package/tgpuUnstable.js +1 -3
  192. package/tgsl/accessIndex.js +2 -4
  193. package/tgsl/accessProp.js +8 -6
  194. package/tgsl/consoleLog/deserializers.js +1 -3
  195. package/tgsl/consoleLog/logGenerator.js +2 -4
  196. package/tgsl/consoleLog/serializers.js +24 -26
  197. package/tgsl/consoleLog/types.d.ts +0 -2
  198. package/tgsl/consoleLog/types.js +1 -2
  199. package/tgsl/conversion.js +1 -3
  200. package/tgsl/forOfUtils.js +35 -9
  201. package/tgsl/generationHelpers.js +3 -3
  202. package/tgsl/math.js +1 -3
  203. package/tgsl/shaderGenerator.d.ts +10 -8
  204. package/tgsl/shaderGenerator_members.d.ts +2 -0
  205. package/tgsl/shaderGenerator_members.js +6 -0
  206. package/tgsl/shellless.js +1 -8
  207. package/tgsl/wgslGenerator.d.ts +36 -0
  208. package/tgsl/wgslGenerator.js +135 -81
  209. package/types.d.ts +14 -4
  210. package/types.js +3 -3
  211. package/wgslExtensions.js +1 -2
package/data/matrix.d.ts CHANGED
@@ -1,54 +1,52 @@
1
1
  import { DualFn } from "../types.js";
2
2
  import { Mat2x2f, Mat3x3f, Mat4x4f, m2x2f, m3x3f, m4x4f, v3f } from "./wgslTypes.js";
3
3
  import { TgpuComptime } from "../core/function/comptime.js";
4
- import "../indexNamedExports.js";
5
-
6
4
  //#region src/data/matrix.d.ts
7
5
  /**
8
6
  * Returns a 2-by-2 identity matrix.
9
7
  * @returns {m2x2f} The result matrix.
10
8
  */
11
- declare const identity2: TgpuComptime<() => m2x2f>;
9
+ declare const identity2: TgpuComptime;
12
10
  /**
13
11
  * Returns a 3-by-3 identity matrix.
14
12
  * @returns {m3x3f} The result matrix.
15
13
  */
16
- declare const identity3: TgpuComptime<() => m3x3f>;
14
+ declare const identity3: TgpuComptime;
17
15
  /**
18
16
  * Returns a 4-by-4 identity matrix.
19
17
  * @returns {m4x4f} The result matrix.
20
18
  */
21
- declare const identity4: TgpuComptime<() => m4x4f>;
19
+ declare const identity4: TgpuComptime;
22
20
  /**
23
21
  * Creates a 4-by-4 matrix which translates by the given vector v.
24
22
  * @param {v3f} vector - The vector by which to translate.
25
23
  * @returns {m4x4f} The translation matrix.
26
24
  */
27
- declare const translation4: DualFn<(vector: v3f) => m4x4f>;
25
+ declare const translation4: DualFn;
28
26
  /**
29
27
  * Creates a 4-by-4 matrix which scales in each dimension by an amount given by the corresponding entry in the given vector.
30
28
  * @param {v3f} vector - A vector of three entries specifying the factor by which to scale in each dimension.
31
29
  * @returns {m4x4f} The scaling matrix.
32
30
  */
33
- declare const scaling4: DualFn<(vector: v3f) => m4x4f>;
31
+ declare const scaling4: DualFn;
34
32
  /**
35
33
  * Creates a 4-by-4 matrix which rotates around the x-axis by the given angle.
36
34
  * @param {number} angle - The angle by which to rotate (in radians).
37
35
  * @returns {m4x4f} The rotation matrix.
38
36
  */
39
- declare const rotationX4: DualFn<(a: number) => m4x4f>;
37
+ declare const rotationX4: DualFn;
40
38
  /**
41
39
  * Creates a 4-by-4 matrix which rotates around the y-axis by the given angle.
42
40
  * @param {number} angle - The angle by which to rotate (in radians).
43
41
  * @returns {m4x4f} The rotation matrix.
44
42
  */
45
- declare const rotationY4: DualFn<(a: number) => m4x4f>;
43
+ declare const rotationY4: DualFn;
46
44
  /**
47
45
  * Creates a 4-by-4 matrix which rotates around the z-axis by the given angle.
48
46
  * @param {number} angle - The angle by which to rotate (in radians).
49
47
  * @returns {m4x4f} The rotation matrix.
50
48
  */
51
- declare const rotationZ4: DualFn<(a: number) => m4x4f>;
49
+ declare const rotationZ4: DualFn;
52
50
  /**
53
51
  * Schema representing mat2x2f - a matrix with 2 rows and 2 columns, with elements of type f32.
54
52
  * Also a constructor function for this matrix type.
package/data/matrix.js CHANGED
@@ -1,18 +1,18 @@
1
1
  import { $internal, $resolve } from "../shared/symbols.js";
2
2
  import { isVec } from "./wgslTypes.js";
3
- import { snip } from "./snippet.js";
4
3
  import { stitch } from "../core/resolve/stitch.js";
5
4
  import { callableSchema } from "../core/function/createCallableSchema.js";
6
5
  import { f32 } from "./numeric.js";
6
+ import { numericLiteralToSnippet } from "../tgsl/generationHelpers.js";
7
7
  import { vec2f, vec3f, vec4f } from "./vector.js";
8
8
  import { dualImpl } from "../core/function/dualImpl.js";
9
9
  import { comptime } from "../core/function/comptime.js";
10
-
11
10
  //#region src/data/matrix.ts
12
11
  var MatBase = class {};
13
12
  function createMatSchema(options) {
14
13
  const construct = callableSchema({
15
14
  name: options.type,
15
+ schema: () => schema,
16
16
  normalImpl: (...args) => {
17
17
  const elements = [];
18
18
  for (const arg of args) if (typeof arg === "number") elements.push(arg);
@@ -21,11 +21,8 @@ function createMatSchema(options) {
21
21
  for (let i = elements.length; i < options.columns * options.rows; ++i) elements.push(0);
22
22
  return new options.MatImpl(...elements);
23
23
  },
24
- signature: (...args) => ({
25
- argTypes: args.map((arg) => isVec(arg) ? arg : f32),
26
- returnType: schema
27
- }),
28
- codegenImpl: (_ctx, args) => stitch`${options.type}(${args})`
24
+ argTypes: (...args) => args.map((arg) => isVec(arg) ? arg : f32),
25
+ codegenImpl: (ctx, args) => ctx.gen.typeInstantiation(schema, args)
29
26
  });
30
27
  const schema = Object.assign(construct, {
31
28
  [$internal]: {},
@@ -41,6 +38,12 @@ function createMatSchema(options) {
41
38
  options.MatImpl.prototype.schema = schema;
42
39
  return schema;
43
40
  }
41
+ const VALID_MAT2x2_ELEMENTS = [
42
+ 0,
43
+ 1,
44
+ 2,
45
+ 3
46
+ ];
44
47
  var mat2x2Impl = class extends MatBase {
45
48
  [$internal] = true;
46
49
  columns;
@@ -79,11 +82,11 @@ var mat2x2Impl = class extends MatBase {
79
82
  yield this[2];
80
83
  yield this[3];
81
84
  }
82
- [$resolve]() {
83
- return snip(`${this.kind}(${Array.from({ length: this.length }).map((_, i) => this[i]).join(", ")})`, mat2x2f, "runtime");
85
+ [$resolve](ctx) {
86
+ return ctx.gen.typeInstantiation(mat2x2f, VALID_MAT2x2_ELEMENTS.map((i) => numericLiteralToSnippet(this[i])));
84
87
  }
85
88
  toString() {
86
- return this[$resolve]().value;
89
+ return `${this.kind}(${VALID_MAT2x2_ELEMENTS.map((i) => this[i]).join(", ")})`;
87
90
  }
88
91
  };
89
92
  var mat2x2fImpl = class extends mat2x2Impl {
@@ -92,6 +95,17 @@ var mat2x2fImpl = class extends mat2x2Impl {
92
95
  return vec2f(e0, e1);
93
96
  }
94
97
  };
98
+ const VALID_MAT3x3_ELEMENTS = [
99
+ 0,
100
+ 1,
101
+ 2,
102
+ 4,
103
+ 5,
104
+ 6,
105
+ 8,
106
+ 9,
107
+ 10
108
+ ];
95
109
  var mat3x3Impl = class extends MatBase {
96
110
  [$internal] = true;
97
111
  columns;
@@ -173,11 +187,11 @@ var mat3x3Impl = class extends MatBase {
173
187
  *[Symbol.iterator]() {
174
188
  for (let i = 0; i < 12; i++) yield this[i];
175
189
  }
176
- [$resolve]() {
177
- return snip(`${this.kind}(${this[0]}, ${this[1]}, ${this[2]}, ${this[4]}, ${this[5]}, ${this[6]}, ${this[8]}, ${this[9]}, ${this[10]})`, mat3x3f, "runtime");
190
+ [$resolve](ctx) {
191
+ return ctx.gen.typeInstantiation(mat3x3f, VALID_MAT3x3_ELEMENTS.map((i) => numericLiteralToSnippet(this[i])));
178
192
  }
179
193
  toString() {
180
- return this[$resolve]().value;
194
+ return `${this.kind}(${VALID_MAT3x3_ELEMENTS.map((i) => this[i]).join(", ")})`;
181
195
  }
182
196
  };
183
197
  var mat3x3fImpl = class extends mat3x3Impl {
@@ -186,6 +200,7 @@ var mat3x3fImpl = class extends mat3x3Impl {
186
200
  return vec3f(x, y, z);
187
201
  }
188
202
  };
203
+ const VALID_MAT4x4_ELEMENTS = Array.from({ length: 16 }, (_, i) => i);
189
204
  var mat4x4Impl = class extends MatBase {
190
205
  [$internal] = true;
191
206
  columns;
@@ -298,11 +313,11 @@ var mat4x4Impl = class extends MatBase {
298
313
  *[Symbol.iterator]() {
299
314
  for (let i = 0; i < 16; i++) yield this[i];
300
315
  }
301
- [$resolve]() {
302
- return snip(`${this.kind}(${Array.from({ length: this.length }).map((_, i) => this[i]).join(", ")})`, mat4x4f, "runtime");
316
+ [$resolve](ctx) {
317
+ return ctx.gen.typeInstantiation(mat4x4f, VALID_MAT4x4_ELEMENTS.map((i) => numericLiteralToSnippet(this[i])));
303
318
  }
304
319
  toString() {
305
- return this[$resolve]().value;
320
+ return `${this.kind}(${VALID_MAT4x4_ELEMENTS.map((i) => this[i]).join(", ")})`;
306
321
  }
307
322
  };
308
323
  var mat4x4fImpl = class extends mat4x4Impl {
@@ -512,6 +527,5 @@ function matToArray(mat) {
512
527
  ];
513
528
  return Array.from({ length: mat.length }).map((_, idx) => mat[idx]);
514
529
  }
515
-
516
530
  //#endregion
517
- export { MatBase, identity2, identity3, identity4, mat2x2f, mat3x3f, mat4x4f, matToArray, rotationX4, rotationY4, rotationZ4, scaling4, translation4 };
531
+ export { MatBase, identity2, identity3, identity4, mat2x2f, mat3x3f, mat4x4f, matToArray, rotationX4, rotationY4, rotationZ4, scaling4, translation4 };
package/data/numberOps.js CHANGED
@@ -19,6 +19,5 @@ function bitcastU32toI32Impl(n) {
19
19
  dataView.setUint32(0, n, true);
20
20
  return dataView.getInt32(0, true);
21
21
  }
22
-
23
22
  //#endregion
24
- export { bitcastU32toF32Impl, bitcastU32toI32Impl, clamp, divInteger, smoothstepScalar };
23
+ export { bitcastU32toF32Impl, bitcastU32toI32Impl, clamp, divInteger, smoothstepScalar };
package/data/numeric.js CHANGED
@@ -1,7 +1,5 @@
1
1
  import { $internal } from "../shared/symbols.js";
2
- import { stitch } from "../core/resolve/stitch.js";
3
2
  import { callableSchema } from "../core/function/createCallableSchema.js";
4
-
5
3
  //#region src/data/numeric.ts
6
4
  const abstractInt = {
7
5
  [$internal]: {},
@@ -19,16 +17,14 @@ const abstractFloat = {
19
17
  };
20
18
  const boolCast = callableSchema({
21
19
  name: "bool",
22
- signature: (arg) => ({
23
- argTypes: arg ? [arg] : [],
24
- returnType: bool
25
- }),
20
+ schema: () => bool,
21
+ argTypes: (arg) => arg ? [arg] : [],
26
22
  normalImpl(v) {
27
23
  if (v === void 0) return false;
28
24
  if (typeof v === "boolean") return v;
29
25
  return !!v;
30
26
  },
31
- codegenImpl: (_ctx, [arg]) => arg?.dataType === bool ? stitch`${arg}` : stitch`bool(${arg})`
27
+ codegenImpl: (ctx, args) => ctx.gen.typeInstantiation(bool, args)
32
28
  });
33
29
  /**
34
30
  * A schema that represents a boolean value. (equivalent to `bool` in WGSL)
@@ -50,10 +46,8 @@ const bool = Object.assign(boolCast, {
50
46
  });
51
47
  const u32Cast = callableSchema({
52
48
  name: "u32",
53
- signature: (arg) => ({
54
- argTypes: arg ? [arg] : [],
55
- returnType: u32
56
- }),
49
+ schema: () => u32,
50
+ argTypes: (arg) => arg ? [arg] : [],
57
51
  normalImpl(v) {
58
52
  if (v === void 0) return 0;
59
53
  if (typeof v === "boolean") return v ? 1 : 0;
@@ -65,7 +59,7 @@ const u32Cast = callableSchema({
65
59
  }
66
60
  return (v & 4294967295) >>> 0;
67
61
  },
68
- codegenImpl: (_ctx, [arg]) => arg?.dataType === u32 ? stitch`${arg}` : stitch`u32(${arg})`
62
+ codegenImpl: (ctx, args) => ctx.gen.typeInstantiation(u32, args)
69
63
  });
70
64
  /**
71
65
  * A schema that represents an unsigned 32-bit integer value. (equivalent to `u32` in WGSL)
@@ -89,16 +83,14 @@ const u32 = Object.assign(u32Cast, {
89
83
  });
90
84
  const i32Cast = callableSchema({
91
85
  name: "i32",
92
- signature: (arg) => ({
93
- argTypes: arg ? [arg] : [],
94
- returnType: i32
95
- }),
86
+ schema: () => i32,
87
+ argTypes: (arg) => arg ? [arg] : [],
96
88
  normalImpl(v) {
97
89
  if (v === void 0) return 0;
98
90
  if (typeof v === "boolean") return v ? 1 : 0;
99
91
  return v | 0;
100
92
  },
101
- codegenImpl: (_ctx, [arg]) => arg?.dataType === i32 ? stitch`${arg}` : stitch`i32(${arg})`
93
+ codegenImpl: (ctx, args) => ctx.gen.typeInstantiation(i32, args)
102
94
  });
103
95
  const u16 = {
104
96
  [$internal]: {},
@@ -124,16 +116,14 @@ const i32 = Object.assign(i32Cast, {
124
116
  });
125
117
  const f32Cast = callableSchema({
126
118
  name: "f32",
127
- signature: (arg) => ({
128
- argTypes: arg ? [arg] : [],
129
- returnType: f32
130
- }),
119
+ schema: () => f32,
120
+ argTypes: (arg) => arg ? [arg] : [],
131
121
  normalImpl(v) {
132
122
  if (v === void 0) return 0;
133
123
  if (typeof v === "boolean") return v ? 1 : 0;
134
124
  return Math.fround(v);
135
125
  },
136
- codegenImpl: (_ctx, [arg]) => arg?.dataType === f32 ? stitch`${arg}` : stitch`f32(${arg})`
126
+ codegenImpl: (ctx, args) => ctx.gen.typeInstantiation(f32, args)
137
127
  });
138
128
  /**
139
129
  * A schema that represents a 32-bit float value. (equivalent to `f32` in WGSL)
@@ -200,16 +190,14 @@ function roundToF16(x) {
200
190
  }
201
191
  const f16Cast = callableSchema({
202
192
  name: "f16",
203
- signature: (arg) => ({
204
- argTypes: arg ? [arg] : [],
205
- returnType: f16
206
- }),
193
+ schema: () => f16,
194
+ argTypes: (arg) => arg ? [arg] : [],
207
195
  normalImpl(v) {
208
196
  if (v === void 0) return 0;
209
197
  if (typeof v === "boolean") return v ? 1 : 0;
210
198
  return roundToF16(v);
211
199
  },
212
- codegenImpl: (_ctx, [arg]) => arg?.dataType === f16 ? stitch`${arg}` : stitch`f16(${arg})`
200
+ codegenImpl: (ctx, args) => ctx.gen.typeInstantiation(f16, args)
213
201
  });
214
202
  /**
215
203
  * A schema that represents a 16-bit float value. (equivalent to `f16` in WGSL)
@@ -229,6 +217,5 @@ const f16 = Object.assign(f16Cast, {
229
217
  [$internal]: {},
230
218
  type: "f16"
231
219
  });
232
-
233
220
  //#endregion
234
- export { abstractFloat, abstractInt, bool, f16, f32, i32, u16, u32 };
221
+ export { abstractFloat, abstractInt, bool, f16, f32, i32, u16, u32 };
@@ -25,9 +25,9 @@ interface PrimitiveOffsetInfo {
25
25
  * ```
26
26
  *
27
27
  * @param schema - The data schema to analyze.
28
- * @param accessor - Optional function that accesses a specific primitive within the schema. If omitted, uses the root offset (0).
28
+ * @param accessor - Optional function that accesses a specific element within the schema. If omitted, uses the root offset (0).
29
29
  * @returns An object containing the offset and contiguous byte information.
30
30
  */
31
- declare function memoryLayoutOf<T extends BaseData>(schema: T, accessor?: (proxy: Infer<T>) => number): PrimitiveOffsetInfo;
31
+ declare function memoryLayoutOf<T extends BaseData>(schema: T, accessor?: (proxy: Infer<T>) => unknown): PrimitiveOffsetInfo;
32
32
  //#endregion
33
33
  export { PrimitiveOffsetInfo, memoryLayoutOf };
@@ -6,7 +6,6 @@ import { sizeOf } from "./sizeOf.js";
6
6
  import { offsetsForProps } from "./offsets.js";
7
7
  import { isContiguous } from "./isContiguous.js";
8
8
  import { getLongestContiguousPrefix } from "./getLongestContiguousPrefix.js";
9
-
10
9
  //#region src/data/offsetUtils.ts
11
10
  const OFFSET_MARKER = Symbol("indirectOffset");
12
11
  const CONTIGUOUS_MARKER = Symbol("indirectContiguous");
@@ -147,7 +146,7 @@ function getRootContiguous(schema) {
147
146
  * ```
148
147
  *
149
148
  * @param schema - The data schema to analyze.
150
- * @param accessor - Optional function that accesses a specific primitive within the schema. If omitted, uses the root offset (0).
149
+ * @param accessor - Optional function that accesses a specific element within the schema. If omitted, uses the root offset (0).
151
150
  * @returns An object containing the offset and contiguous byte information.
152
151
  */
153
152
  function memoryLayoutOf(schema, accessor) {
@@ -160,8 +159,7 @@ function memoryLayoutOf(schema, accessor) {
160
159
  offset: result[OFFSET_MARKER],
161
160
  contiguous: result[CONTIGUOUS_MARKER]
162
161
  };
163
- throw new Error("Invalid accessor result. Expected an offset proxy with markers.");
162
+ throw new Error("memoryLayoutOf: accessor did not return a schema element. Make sure the accessor navigates to a field or element of the schema (e.g. `(s) => s.position.x`).");
164
163
  }
165
-
166
164
  //#endregion
167
- export { memoryLayoutOf };
165
+ export { memoryLayoutOf };
package/data/offsets.js CHANGED
@@ -4,7 +4,6 @@ import { roundUp } from "../mathUtils.js";
4
4
  import { sizeOf } from "./sizeOf.js";
5
5
  import alignIO from "./alignIO.js";
6
6
  import { Measurer } from "typed-binary";
7
-
8
7
  //#region src/data/offsets.ts
9
8
  const cachedOffsets = /* @__PURE__ */ new WeakMap();
10
9
  function offsetsForProps(struct) {
@@ -31,6 +30,5 @@ function offsetsForProps(struct) {
31
30
  cachedOffsets.set(struct, offsets);
32
31
  return offsets;
33
32
  }
34
-
35
33
  //#endregion
36
- export { offsetsForProps };
34
+ export { offsetsForProps };
package/data/partialIO.js CHANGED
@@ -4,65 +4,110 @@ import { alignmentOf } from "./alignmentOf.js";
4
4
  import { roundUp } from "../mathUtils.js";
5
5
  import { sizeOf } from "./sizeOf.js";
6
6
  import { offsetsForProps } from "./offsets.js";
7
+ import { getCompiledWriter } from "./compiledIO.js";
7
8
  import { writeData } from "./dataIO.js";
8
- import { BufferWriter } from "typed-binary";
9
-
9
+ import { BufferWriter, getSystemEndianness } from "typed-binary";
10
10
  //#region src/data/partialIO.ts
11
- function getWriteInstructions(schema, data) {
11
+ /**
12
+ * Converts `{idx, value}[]` sparse arrays into `Record<number, T>` format.
13
+ */
14
+ function convertPartialToPatch(schema, data) {
15
+ if (data === void 0 || data === null) return data;
16
+ if (isWgslStruct(schema) || isUnstruct(schema)) {
17
+ const result = {};
18
+ const record = data;
19
+ for (const key of Object.keys(schema.propTypes)) {
20
+ const subSchema = schema.propTypes[key];
21
+ const value = record[key];
22
+ if (value !== void 0 && subSchema) result[key] = convertPartialToPatch(subSchema, value);
23
+ }
24
+ return result;
25
+ }
26
+ if (isWgslArray(schema) || isDisarray(schema)) {
27
+ const arrSchema = schema;
28
+ const result = {};
29
+ for (const { idx, value } of data) result[idx] = convertPartialToPatch(arrSchema.elementType, value);
30
+ return result;
31
+ }
32
+ return data;
33
+ }
34
+ const isLittleEndian = getSystemEndianness() === "little";
35
+ function getPatchInstructions(schema, data, targetBuffer) {
12
36
  const totalSize = sizeOf(schema);
13
37
  if (totalSize === 0 || data === void 0 || data === null) return [];
14
- const bigBuffer = new ArrayBuffer(totalSize);
15
- const writer = new BufferWriter(bigBuffer);
38
+ const buf = targetBuffer ?? new ArrayBuffer(totalSize);
39
+ const writer = new BufferWriter(buf);
40
+ const compiledView = new DataView(buf);
16
41
  const segments = [];
17
- function gatherAndWrite(node, partialValue, offset, padding) {
18
- if (partialValue === void 0 || partialValue === null) return;
42
+ function collect(node, value, offset, padding) {
43
+ if (value === void 0 || value === null) return;
19
44
  if (isWgslStruct(node) || isUnstruct(node)) {
20
45
  const propOffsets = offsetsForProps(node);
21
46
  for (const [key, propOffset] of Object.entries(propOffsets)) {
47
+ const childValue = value[key];
22
48
  const subSchema = node.propTypes[key];
23
- if (!subSchema) continue;
24
- const childValue = partialValue[key];
25
- if (childValue !== void 0) gatherAndWrite(subSchema, childValue, offset + propOffset.offset, propOffset.padding ?? padding);
49
+ if (childValue !== void 0 && subSchema) collect(subSchema, childValue, offset + propOffset.offset, propOffset.padding ?? padding);
26
50
  }
27
51
  return;
28
52
  }
29
53
  if (isWgslArray(node) || isDisarray(node)) {
30
54
  const arrSchema = node;
31
55
  const elementSize = roundUp(sizeOf(arrSchema.elementType), alignmentOf(arrSchema.elementType));
32
- if (!Array.isArray(partialValue)) throw new Error("Partial value for array must be an array");
33
- const arrayPartialValue = partialValue ?? [];
34
- arrayPartialValue.sort((a, b) => a.idx - b.idx);
35
- for (const { idx, value } of arrayPartialValue) gatherAndWrite(arrSchema.elementType, value, offset + idx * elementSize, elementSize - sizeOf(arrSchema.elementType));
36
- } else {
37
- const leafSize = sizeOf(node);
56
+ const elementPadding = elementSize - sizeOf(arrSchema.elementType);
57
+ if (ArrayBuffer.isView(value)) {
58
+ const copyLen = Math.min(value.byteLength, arrSchema.elementCount * elementSize);
59
+ new Uint8Array(buf, offset, copyLen).set(new Uint8Array(value.buffer, value.byteOffset, copyLen));
60
+ segments.push({
61
+ start: offset,
62
+ end: offset + copyLen,
63
+ padding
64
+ });
65
+ return;
66
+ }
67
+ if (Array.isArray(value)) {
68
+ for (let i = 0; i < Math.min(arrSchema.elementCount, value.length); i++) collect(arrSchema.elementType, value[i], offset + i * elementSize, elementPadding);
69
+ return;
70
+ }
71
+ const sparse = value;
72
+ for (const key of Object.keys(sparse)) {
73
+ const idx = Number(key);
74
+ if (!Number.isNaN(idx)) collect(arrSchema.elementType, sparse[key], offset + idx * elementSize, elementPadding);
75
+ }
76
+ return;
77
+ }
78
+ const leafSize = sizeOf(node);
79
+ const compiledWriter = getCompiledWriter(node);
80
+ if (compiledWriter) compiledWriter(compiledView, offset, value, isLittleEndian, offset + leafSize);
81
+ else {
38
82
  writer.seekTo(offset);
39
- writeData(writer, node, partialValue);
40
- segments.push({
41
- start: offset,
42
- end: offset + leafSize,
43
- padding
44
- });
83
+ writeData(writer, node, value);
45
84
  }
85
+ segments.push({
86
+ start: offset,
87
+ end: offset + leafSize,
88
+ padding
89
+ });
46
90
  }
47
- gatherAndWrite(schema, data, 0);
48
- if (segments.length === 0) return [];
91
+ collect(schema, data, 0);
49
92
  const instructions = [];
50
- let current = segments[0];
51
- for (let i = 1; i < segments.length; i++) {
52
- const next = segments[i];
53
- if (!next || !current) throw new Error("Internal error: missing segment");
54
- if (next.start === current.end + (current.padding ?? 0)) {
55
- current.end = next.end;
56
- current.padding = next.padding;
57
- } else {
58
- instructions.push({ data: new Uint8Array(bigBuffer, current.start, current.end - current.start) });
59
- current = next;
60
- }
93
+ let run = null;
94
+ for (const seg of segments) if (run && seg.start === run.end + (run.padding ?? 0)) run = {
95
+ start: run.start,
96
+ end: seg.end,
97
+ padding: seg.padding
98
+ };
99
+ else {
100
+ if (run) instructions.push({
101
+ gpuOffset: run.start,
102
+ data: new Uint8Array(buf, run.start, run.end - run.start).slice()
103
+ });
104
+ run = seg;
61
105
  }
62
- if (!current) throw new Error("Internal error: missing segment");
63
- instructions.push({ data: new Uint8Array(bigBuffer, current.start, current.end - current.start) });
106
+ if (run) instructions.push({
107
+ gpuOffset: run.start,
108
+ data: new Uint8Array(buf, run.start, run.end - run.start).slice()
109
+ });
64
110
  return instructions;
65
111
  }
66
-
67
112
  //#endregion
68
- export { getWriteInstructions };
113
+ export { convertPartialToPatch, getPatchInstructions };
package/data/ptr.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import "./snippet.js";
2
1
  import { Ptr, StorableData } from "./wgslTypes.js";
3
2
 
4
3
  //#region src/data/ptr.d.ts
package/data/ptr.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { $internal } from "../shared/symbols.js";
2
2
  import { originToPtrParams } from "./snippet.js";
3
-
4
3
  //#region src/data/ptr.ts
5
4
  function ptrFn(inner) {
6
5
  return INTERNAL_createPtr("function", inner, "read-write");
@@ -41,6 +40,5 @@ function implicitFrom(ptr) {
41
40
  function explicitFrom(ptr) {
42
41
  return INTERNAL_createPtr(ptr.addressSpace, ptr.inner, ptr.access, false);
43
42
  }
44
-
45
43
  //#endregion
46
- export { createPtrFromOrigin, explicitFrom, implicitFrom, ptrFn, ptrHandle, ptrPrivate, ptrStorage, ptrUniform, ptrWorkgroup };
44
+ export { createPtrFromOrigin, explicitFrom, implicitFrom, ptrFn, ptrHandle, ptrPrivate, ptrStorage, ptrUniform, ptrWorkgroup };
package/data/ref.d.ts CHANGED
@@ -1,8 +1,5 @@
1
1
  import { $internal } from "../shared/symbols.js";
2
- import "./snippet.js";
3
2
  import { DualFn } from "../types.js";
4
- import "./wgslTypes.js";
5
-
6
3
  //#region src/data/ref.d.ts
7
4
  interface ref<T> {
8
5
  readonly [$internal]: {
package/data/ref.js CHANGED
@@ -6,7 +6,6 @@ import { snip } from "./snippet.js";
6
6
  import { WgslTypeError } from "../errors.js";
7
7
  import { stitch } from "../core/resolve/stitch.js";
8
8
  import { createPtrFromOrigin, explicitFrom } from "./ptr.js";
9
-
10
9
  //#region src/data/ref.ts
11
10
  const _ref = (() => {
12
11
  const impl = ((value) => INTERNAL_createRef(value));
@@ -91,6 +90,5 @@ function derefSnippet(snippet) {
91
90
  if (snippet.value instanceof RefOperator) return snip(stitch`${snippet.value.snippet}`, innerType, origin);
92
91
  return snip(stitch`(*${snippet})`, innerType, origin);
93
92
  }
94
-
95
93
  //#endregion
96
- export { RefOperator, _ref, derefSnippet, isRef };
94
+ export { RefOperator, _ref, derefSnippet, isRef };
package/data/sampler.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { $internal, $repr } from "../shared/symbols.js";
2
-
3
2
  //#region src/data/sampler.ts
4
3
  function sampler() {
5
4
  return {
@@ -21,6 +20,5 @@ function isWgslSampler(value) {
21
20
  function isWgslComparisonSampler(value) {
22
21
  return !!value[$internal] && value.type === "sampler_comparison";
23
22
  }
24
-
25
23
  //#endregion
26
- export { comparisonSampler, isWgslComparisonSampler, isWgslSampler, sampler };
24
+ export { comparisonSampler, isWgslComparisonSampler, isWgslSampler, sampler };
@@ -1,6 +1,5 @@
1
1
  import { $cast, $gpuCallable } from "../shared/symbols.js";
2
2
  import { hasCast, isGPUCallable } from "../types.js";
3
-
4
3
  //#region src/data/schemaCallWrapper.ts
5
4
  /**
6
5
  * A wrapper for `schema(item)` or `schema()` call on JS side.
@@ -27,6 +26,5 @@ function schemaCallWrapperGPU(ctx, schema, item) {
27
26
  const callSchema = schema;
28
27
  return item === void 0 || item.value === void 0 ? callSchema[$gpuCallable].call(ctx, []) : callSchema[$gpuCallable].call(ctx, [item]);
29
28
  }
30
-
31
29
  //#endregion
32
- export { schemaCallWrapper, schemaCallWrapperGPU };
30
+ export { schemaCallWrapper, schemaCallWrapperGPU };
@@ -2,7 +2,6 @@ import { isDecorated, isWgslArray, isWgslStruct } from "./wgslTypes.js";
2
2
  import { getCustomSize, isDisarray, isLooseDecorated, isUnstruct, undecorate } from "./dataTypes.js";
3
3
  import { alignmentOf, customAlignmentOf } from "./alignmentOf.js";
4
4
  import { roundUp } from "../mathUtils.js";
5
-
6
5
  //#region src/data/schemaMemoryLayout.ts
7
6
  const knownSizesMap = {
8
7
  bool: 4,
@@ -195,6 +194,5 @@ function getLayoutInfo(schema, key) {
195
194
  }
196
195
  return layout[key];
197
196
  }
198
-
199
197
  //#endregion
200
- export { getLayoutInfo };
198
+ export { getLayoutInfo };
package/data/sizeOf.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import "./wgslTypes.js";
2
1
  import { AnyData } from "./dataTypes.js";
3
2
 
4
3
  //#region src/data/sizeOf.d.ts
package/data/sizeOf.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { getLayoutInfo } from "./schemaMemoryLayout.js";
2
-
3
2
  //#region src/data/sizeOf.ts
4
3
  function sizeOf(schema) {
5
4
  return getLayoutInfo(schema, "size");
@@ -10,6 +9,5 @@ function sizeOf(schema) {
10
9
  function PUBLIC_sizeOf(schema) {
11
10
  return sizeOf(schema);
12
11
  }
13
-
14
12
  //#endregion
15
- export { PUBLIC_sizeOf, sizeOf };
13
+ export { PUBLIC_sizeOf, sizeOf };