typegpu 0.11.1 → 0.11.3

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 (216) hide show
  1. package/_virtual/{_rolldown/runtime.js → rolldown_runtime.js} +4 -4
  2. package/builtin.js +3 -1
  3. package/common/fullScreenTriangle.d.ts +6 -1
  4. package/common/fullScreenTriangle.js +3 -1
  5. package/common/index.js +5 -3
  6. package/common/writeSoA.js +3 -1
  7. package/core/buffer/buffer.d.ts +3 -0
  8. package/core/buffer/buffer.js +5 -3
  9. package/core/buffer/bufferShorthand.d.ts +2 -0
  10. package/core/buffer/bufferShorthand.js +3 -1
  11. package/core/buffer/bufferUsage.d.ts +2 -0
  12. package/core/buffer/bufferUsage.js +3 -1
  13. package/core/constant/tgpuConstant.d.ts +2 -1
  14. package/core/constant/tgpuConstant.js +3 -1
  15. package/core/declare/tgpuDeclare.js +3 -1
  16. package/core/function/autoIO.d.ts +3 -0
  17. package/core/function/autoIO.js +5 -3
  18. package/core/function/comptime.js +3 -1
  19. package/core/function/createCallableSchema.js +3 -1
  20. package/core/function/dualImpl.js +3 -1
  21. package/core/function/entryInputRouter.js +13 -17
  22. package/core/function/extractArgs.js +2 -1
  23. package/core/function/fnCore.js +17 -11
  24. package/core/function/fnTypes.d.ts +2 -0
  25. package/core/function/ioSchema.d.ts +1 -0
  26. package/core/function/ioSchema.js +3 -1
  27. package/core/function/shelllessImpl.d.ts +1 -0
  28. package/core/function/shelllessImpl.js +4 -2
  29. package/core/function/templateUtils.js +2 -1
  30. package/core/function/tgpuComputeFn.d.ts +1 -0
  31. package/core/function/tgpuComputeFn.js +4 -2
  32. package/core/function/tgpuFn.d.ts +1 -0
  33. package/core/function/tgpuFn.js +17 -15
  34. package/core/function/tgpuFragmentFn.js +4 -2
  35. package/core/function/tgpuVertexFn.js +4 -2
  36. package/core/pipeline/applyPipelineState.js +3 -1
  37. package/core/pipeline/computePipeline.d.ts +4 -0
  38. package/core/pipeline/computePipeline.js +8 -6
  39. package/core/pipeline/connectAttachmentToShader.js +3 -1
  40. package/core/pipeline/connectTargetsToShader.js +3 -1
  41. package/core/pipeline/limitsOverflow.js +2 -1
  42. package/core/pipeline/pipelineUtils.js +6 -4
  43. package/core/pipeline/renderPipeline.js +21 -19
  44. package/core/pipeline/timeable.d.ts +2 -0
  45. package/core/pipeline/timeable.js +3 -1
  46. package/core/pipeline/typeGuards.js +3 -1
  47. package/core/querySet/querySet.d.ts +2 -0
  48. package/core/querySet/querySet.js +3 -1
  49. package/core/rawCodeSnippet/tgpuRawCodeSnippet.d.ts +1 -0
  50. package/core/rawCodeSnippet/tgpuRawCodeSnippet.js +3 -1
  51. package/core/resolve/externals.d.ts +3 -0
  52. package/core/resolve/externals.js +7 -5
  53. package/core/resolve/namespace.js +6 -4
  54. package/core/resolve/resolveData.js +3 -1
  55. package/core/resolve/stitch.js +3 -1
  56. package/core/resolve/tgpuResolve.js +7 -5
  57. package/core/root/configurableImpl.js +3 -1
  58. package/core/root/init.d.ts +10 -2
  59. package/core/root/init.js +4 -2
  60. package/core/root/rootTypes.d.ts +11 -11
  61. package/core/sampler/sampler.d.ts +4 -0
  62. package/core/sampler/sampler.js +3 -1
  63. package/core/simulate/tgpuSimulate.js +6 -4
  64. package/core/slot/accessor.d.ts +4 -0
  65. package/core/slot/accessor.js +3 -1
  66. package/core/slot/internalSlots.js +3 -1
  67. package/core/slot/lazy.js +3 -1
  68. package/core/slot/slot.js +3 -1
  69. package/core/slot/slotTypes.js +3 -1
  70. package/core/texture/externalTexture.d.ts +6 -0
  71. package/core/texture/externalTexture.js +3 -1
  72. package/core/texture/texture.d.ts +4 -0
  73. package/core/texture/texture.js +6 -4
  74. package/core/texture/textureFormats.js +3 -1
  75. package/core/texture/textureUtils.js +3 -1
  76. package/core/texture/usageExtension.js +3 -1
  77. package/core/unroll/tgpuUnroll.d.ts +3 -1
  78. package/core/unroll/tgpuUnroll.js +3 -1
  79. package/core/valueProxyUtils.js +7 -5
  80. package/core/variable/tgpuVariable.d.ts +3 -1
  81. package/core/variable/tgpuVariable.js +3 -1
  82. package/core/vertexLayout/connectAttributesToShader.js +3 -1
  83. package/core/vertexLayout/vertexAttribute.d.ts +1 -0
  84. package/core/vertexLayout/vertexLayout.js +7 -5
  85. package/data/alignIO.js +3 -1
  86. package/data/alignmentOf.d.ts +2 -0
  87. package/data/alignmentOf.js +3 -1
  88. package/data/array.js +3 -1
  89. package/data/atomic.d.ts +1 -0
  90. package/data/atomic.js +3 -1
  91. package/data/attributes.js +7 -5
  92. package/data/autoStruct.d.ts +3 -1
  93. package/data/autoStruct.js +3 -1
  94. package/data/compiledIO.js +39 -37
  95. package/data/dataIO.js +22 -20
  96. package/data/dataTypes.d.ts +9 -4
  97. package/data/dataTypes.js +3 -1
  98. package/data/deepEqual.d.ts +1 -0
  99. package/data/deepEqual.js +3 -1
  100. package/data/disarray.d.ts +3 -1
  101. package/data/disarray.js +3 -1
  102. package/data/getLongestContiguousPrefix.d.ts +2 -0
  103. package/data/getLongestContiguousPrefix.js +3 -1
  104. package/data/index.d.ts +3 -3
  105. package/data/index.js +5 -6
  106. package/data/isContiguous.d.ts +2 -0
  107. package/data/isContiguous.js +3 -1
  108. package/data/matrix.d.ts +11 -8
  109. package/data/matrix.js +3 -1
  110. package/data/numberOps.js +2 -1
  111. package/data/numeric.d.ts +1 -0
  112. package/data/numeric.js +19 -15
  113. package/data/offsetUtils.d.ts +1 -0
  114. package/data/offsetUtils.js +3 -1
  115. package/data/offsets.js +5 -3
  116. package/data/partialIO.js +3 -1
  117. package/data/ptr.d.ts +1 -0
  118. package/data/ptr.js +3 -1
  119. package/data/ref.d.ts +3 -0
  120. package/data/ref.js +5 -3
  121. package/data/sampler.js +3 -1
  122. package/data/schemaCallWrapper.js +3 -1
  123. package/data/schemaMemoryLayout.js +3 -1
  124. package/data/sizeOf.d.ts +2 -0
  125. package/data/sizeOf.js +3 -1
  126. package/data/snippet.d.ts +1 -6
  127. package/data/snippet.js +3 -1
  128. package/data/struct.d.ts +1 -0
  129. package/data/struct.js +3 -1
  130. package/data/texture.js +3 -1
  131. package/data/unstruct.d.ts +1 -0
  132. package/data/unstruct.js +3 -1
  133. package/data/vector.d.ts +1 -0
  134. package/data/vector.js +3 -1
  135. package/data/vectorImpl.js +3 -1
  136. package/data/vectorOps.js +3 -1
  137. package/data/vertexFormatData.js +3 -1
  138. package/data/wgslTypes.d.ts +4 -0
  139. package/data/wgslTypes.js +3 -1
  140. package/errors.d.ts +1 -0
  141. package/errors.js +3 -1
  142. package/execMode.js +3 -1
  143. package/extension.d.ts +1 -0
  144. package/extension.js +3 -1
  145. package/getGPUValue.js +3 -1
  146. package/index.js +3 -4
  147. package/indexNamedExports.d.ts +1 -0
  148. package/mathUtils.js +2 -1
  149. package/memo.js +2 -1
  150. package/nameRegistry.js +3 -1
  151. package/package.js +3 -2
  152. package/package.json +1 -1
  153. package/resolutionCtx.d.ts +11 -0
  154. package/resolutionCtx.js +86 -87
  155. package/shared/env.js +2 -1
  156. package/shared/generators.js +2 -1
  157. package/shared/meta.js +3 -1
  158. package/shared/repr.d.ts +1 -0
  159. package/shared/stringify.js +3 -1
  160. package/shared/symbols.js +42 -10
  161. package/shared/utilityTypes.js +2 -1
  162. package/shared/vertexFormat.js +2 -1
  163. package/std/array.d.ts +1 -1
  164. package/std/array.js +3 -1
  165. package/std/atomic.d.ts +12 -12
  166. package/std/atomic.js +3 -1
  167. package/std/bitcast.d.ts +2 -2
  168. package/std/bitcast.js +3 -1
  169. package/std/boolean.d.ts +15 -14
  170. package/std/boolean.js +3 -1
  171. package/std/derivative.d.ts +9 -9
  172. package/std/derivative.js +3 -1
  173. package/std/discard.d.ts +1 -1
  174. package/std/discard.js +3 -1
  175. package/std/extensions.d.ts +3 -1
  176. package/std/extensions.js +3 -1
  177. package/std/index.js +5 -3
  178. package/std/matrix.d.ts +6 -5
  179. package/std/matrix.js +3 -1
  180. package/std/numeric.d.ts +132 -78
  181. package/std/numeric.js +7 -5
  182. package/std/operators.d.ts +8 -8
  183. package/std/operators.js +7 -5
  184. package/std/packing.d.ts +5 -4
  185. package/std/packing.js +3 -1
  186. package/std/range.d.ts +3 -1
  187. package/std/range.js +3 -1
  188. package/std/subgroup.d.ts +21 -21
  189. package/std/subgroup.js +3 -1
  190. package/std/texture.d.ts +11 -11
  191. package/std/texture.js +9 -7
  192. package/tgpu.js +5 -3
  193. package/tgpuBindGroupLayout.d.ts +11 -7
  194. package/tgpuBindGroupLayout.js +3 -1
  195. package/tgpuUnstable.js +5 -3
  196. package/tgsl/accessIndex.js +5 -3
  197. package/tgsl/accessProp.js +10 -3
  198. package/tgsl/consoleLog/deserializers.js +3 -1
  199. package/tgsl/consoleLog/logGenerator.js +18 -9
  200. package/tgsl/consoleLog/serializers.js +3 -1
  201. package/tgsl/consoleLog/types.d.ts +3 -0
  202. package/tgsl/consoleLog/types.js +2 -1
  203. package/tgsl/conversion.js +18 -6
  204. package/tgsl/forOfUtils.js +17 -7
  205. package/tgsl/generationHelpers.d.ts +1 -0
  206. package/tgsl/generationHelpers.js +3 -4
  207. package/tgsl/math.js +3 -1
  208. package/tgsl/shaderGenerator.d.ts +3 -2
  209. package/tgsl/shaderGenerator_members.d.ts +15 -2
  210. package/tgsl/shaderGenerator_members.js +5 -3
  211. package/tgsl/shellless.js +3 -1
  212. package/tgsl/wgslGenerator.d.ts +3 -1
  213. package/tgsl/wgslGenerator.js +54 -44
  214. package/types.d.ts +11 -7
  215. package/types.js +3 -1
  216. package/wgslExtensions.js +2 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typegpu",
3
- "version": "0.11.1",
3
+ "version": "0.11.3",
4
4
  "description": "A thin layer between JS and WebGPU/WGSL that improves development experience and allows for faster iteration.",
5
5
  "keywords": [
6
6
  "compute",
@@ -1,6 +1,17 @@
1
+ import "./data/snippet.js";
2
+ import "./tgsl/shellless.js";
1
3
  import { LogResources } from "./tgsl/consoleLog/types.js";
4
+ import "./core/slot/slotTypes.js";
2
5
  import { TgpuBindGroup, TgpuBindGroupLayout } from "./tgpuBindGroupLayout.js";
6
+ import "./tgsl/shaderGenerator.js";
7
+ import "./core/root/rootTypes.js";
8
+ import "./types.js";
9
+ import "./data/wgslTypes.js";
10
+ import "./data/dataTypes.js";
11
+ import "./core/resolve/namespace.js";
12
+
3
13
  //#region src/resolutionCtx.d.ts
14
+
4
15
  /**
5
16
  * The results of a WGSL resolution.
6
17
  *
package/resolutionCtx.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { $internal, $providing, $resolve, isMarkedInternal } from "./shared/symbols.js";
2
2
  import { getName, hasTinyestMetadata, setName } from "./shared/meta.js";
3
3
  import { Void, isPtr, isWgslArray, isWgslStruct } from "./data/wgslTypes.js";
4
- import { UnknownData, isData, undecorate } from "./data/dataTypes.js";
4
+ import { UnknownData, isData } from "./data/dataTypes.js";
5
5
  import { snip } from "./data/snippet.js";
6
6
  import { MissingSlotValueError, ResolutionError, WgslTypeError, invariant } from "./errors.js";
7
7
  import { isLazy, isProviding, isSlot } from "./core/slot/slotTypes.js";
@@ -12,7 +12,6 @@ import { safeStringify } from "./shared/stringify.js";
12
12
  import { getBestConversion } from "./tgsl/conversion.js";
13
13
  import { bool } from "./data/numeric.js";
14
14
  import { coerceToSnippet, concretize, numericLiteralToSnippet } from "./tgsl/generationHelpers.js";
15
- import { getAttributesString } from "./data/attributes.js";
16
15
  import { createIoSchema } from "./core/function/ioSchema.js";
17
16
  import { AutoStruct } from "./data/autoStruct.js";
18
17
  import { EntryInputRouter } from "./core/function/entryInputRouter.js";
@@ -23,8 +22,9 @@ import { ConfigurableImpl } from "./core/root/configurableImpl.js";
23
22
  import { naturalsExcept } from "./shared/generators.js";
24
23
  import { TgpuBindGroupImpl, bindGroupLayout } from "./tgpuBindGroupLayout.js";
25
24
  import { LogGeneratorImpl, LogGeneratorNullImpl } from "./tgsl/consoleLog/logGenerator.js";
26
- import wgslGenerator from "./tgsl/wgslGenerator.js";
25
+ import wgslGenerator_default from "./tgsl/wgslGenerator.js";
27
26
  import { FuncParameterType } from "tinyest";
27
+
28
28
  //#region src/resolutionCtx.ts
29
29
  /**
30
30
  * Inserted into bind group entry definitions that belong
@@ -63,12 +63,11 @@ var ItemStateStackImpl = class {
63
63
  bindingMap: new WeakMap(pairs)
64
64
  });
65
65
  }
66
- pushFunctionScope(functionType, args, argAliases, returnType, externalMap) {
66
+ pushFunctionScope(functionType, argAccess, returnType, externalMap) {
67
67
  const scope = {
68
68
  type: "functionScope",
69
69
  functionType,
70
- args,
71
- argAliases,
70
+ argAccess,
72
71
  returnType,
73
72
  externalMap,
74
73
  reportedReturnTypes: /* @__PURE__ */ new Set()
@@ -105,9 +104,8 @@ var ItemStateStackImpl = class {
105
104
  for (let i = this._stack.length - 1; i >= 0; --i) {
106
105
  const layer = this._stack[i];
107
106
  if (layer?.type === "functionScope") {
108
- const arg = layer.args.find((a) => a.value === id);
109
- if (arg !== void 0) return arg;
110
- if (layer.argAliases[id]) return layer.argAliases[id];
107
+ const access = layer.argAccess[id];
108
+ if (access) return access();
111
109
  const external = layer.externalMap[id];
112
110
  if (external !== void 0 && external !== null) return coerceToSnippet(external);
113
111
  return;
@@ -188,6 +186,27 @@ var IndentController = class {
188
186
  }
189
187
  }
190
188
  };
189
+ function createArgument(name, type, origin = "argument") {
190
+ let used = false;
191
+ return {
192
+ name,
193
+ access: () => {
194
+ used = true;
195
+ return snip(name, type, origin);
196
+ },
197
+ decoratedType: type,
198
+ get used() {
199
+ return used;
200
+ }
201
+ };
202
+ }
203
+ function createArgumentPropAccess(argAccess, prop) {
204
+ return () => {
205
+ const argSnippet = argAccess();
206
+ if (!argSnippet) return;
207
+ return accessProp(argSnippet, prop);
208
+ };
209
+ }
191
210
  var ResolutionCtxImpl = class {
192
211
  #namespaceInternal;
193
212
  _indentController = new IndentController();
@@ -215,7 +234,7 @@ var ResolutionCtxImpl = class {
215
234
  expectedType;
216
235
  constructor(opts) {
217
236
  this.enableExtensions = opts.enableExtensions;
218
- this.gen = opts.shaderGenerator ?? wgslGenerator;
237
+ this.gen = opts.shaderGenerator ?? wgslGenerator_default;
219
238
  this.#logGenerator = opts.root ? new LogGeneratorImpl(opts.root) : new LogGeneratorNullImpl();
220
239
  this.#namespaceInternal = opts.namespace[$internal];
221
240
  }
@@ -286,107 +305,93 @@ var ResolutionCtxImpl = class {
286
305
  try {
287
306
  this.#namespaceInternal.nameRegistry.pushFunctionScope();
288
307
  const args = [];
289
- const argAliases = [];
290
- const pendingHeaderEntries = [];
308
+ const argAccess = {};
291
309
  if (options.entryInput) {
292
310
  const { dataSchema, positionalArgs } = options.entryInput;
293
311
  const firstParam = options.params[0];
294
- const structArgName = this.makeNameValid("_arg_0");
295
- const structArg = dataSchema ? snip(structArgName, dataSchema, "argument") : void 0;
296
- if (structArg) {
297
- args.push(structArg);
298
- pendingHeaderEntries.push({
299
- argName: structArgName,
300
- header: `${structArgName}: ${this.resolve(dataSchema).value}`
301
- });
302
- }
312
+ const structArg = dataSchema ? createArgument(this.makeNameValid("_arg_0"), dataSchema) : void 0;
313
+ if (structArg) args.push(structArg);
303
314
  if (firstParam?.type === FuncParameterType.destructuredObject) for (const { name, alias } of firstParam.props) {
304
315
  const argInfo = positionalArgs.find((a) => a.schemaKey === name);
305
316
  if (argInfo) {
306
- const argName = this.makeNameValid(alias);
307
- const argSnippet = snip(argName, argInfo.type, "argument");
308
- args.push(argSnippet);
309
- argAliases.push([alias, argSnippet]);
310
- pendingHeaderEntries.push({
311
- argName,
312
- header: `${getAttributesString(argInfo.type)}${argName}: ${this.resolve(undecorate(argInfo.type)).value}`
313
- });
314
- } else if (structArg) {
315
- const propSnippet = accessProp(structArg, name);
316
- if (propSnippet) argAliases.push([alias, propSnippet]);
317
- }
317
+ const arg = createArgument(this.makeNameValid(alias), argInfo.type);
318
+ args.push(arg);
319
+ argAccess[alias] = arg.access;
320
+ } else if (structArg) argAccess[alias] = createArgumentPropAccess(structArg.access, name);
318
321
  }
319
322
  else if (firstParam?.type === FuncParameterType.identifier) {
320
323
  const proxyEntries = [];
321
324
  for (const a of positionalArgs) {
322
- const argName = this.makeNameValid(`_arg_${a.schemaKey}`);
323
- const s = snip(argName, a.type, "argument");
324
- args.push(s);
325
+ const arg = createArgument(this.makeNameValid(`_arg_${a.schemaKey}`), a.type);
326
+ args.push(arg);
325
327
  proxyEntries.push({
326
328
  schemaKey: a.schemaKey,
327
- argName,
328
- type: a.type
329
- });
330
- pendingHeaderEntries.push({
331
- argName,
332
- header: `${getAttributesString(a.type)}${argName}: ${this.resolve(undecorate(a.type)).value}`
329
+ arg: arg.access
333
330
  });
334
331
  }
335
- const router = new EntryInputRouter(structArgName, dataSchema, proxyEntries);
336
- argAliases.push([firstParam.name, snip(firstParam.name, router, "argument")]);
332
+ const router = new EntryInputRouter(structArg?.access, proxyEntries);
333
+ argAccess[firstParam.name] = () => snip("N/A", router, "argument");
337
334
  } else for (const a of positionalArgs) {
338
335
  const argName = this.makeNameValid(`_arg_${a.schemaKey}`);
339
- args.push(snip(argName, a.type, "argument"));
340
- pendingHeaderEntries.push({
341
- argName,
342
- header: `${getAttributesString(a.type)}${argName}: ${this.resolve(undecorate(a.type)).value}`
343
- });
336
+ const arg = createArgument(argName, a.type);
337
+ args.push(arg);
338
+ argAccess[argName] = arg.access;
344
339
  }
345
340
  } else for (const [i, argType] of options.argTypes.entries()) {
346
341
  const astParam = options.params[i];
347
342
  const origin = isPtr(argType) ? argType.addressSpace === "storage" ? argType.access === "read" ? "readonly" : "mutable" : argType.addressSpace : "argument";
348
343
  switch (astParam?.type) {
349
344
  case FuncParameterType.identifier: {
350
- const rawName = astParam.name;
351
- const snippet = snip(this.makeNameValid(rawName), argType, origin);
352
- args.push(snippet);
353
- if (snippet.value !== rawName) argAliases.push([rawName, snippet]);
345
+ const arg = createArgument(this.makeNameValid(astParam.name), argType, origin);
346
+ args.push(arg);
347
+ argAccess[astParam.name] = arg.access;
354
348
  break;
355
349
  }
356
350
  case FuncParameterType.destructuredObject: {
357
- const objSnippet = snip(`_arg_${i}`, argType, origin);
358
- args.push(objSnippet);
359
- argAliases.push(...astParam.props.map(({ name, alias }) => [alias, accessProp(objSnippet, name)]));
351
+ const objArg = createArgument(this.makeNameValid(`_arg_${i}`), argType, origin);
352
+ args.push(objArg);
353
+ for (const { name, alias } of astParam.props) argAccess[alias] = createArgumentPropAccess(objArg.access, name);
360
354
  break;
361
355
  }
362
- case void 0: if (!(argType instanceof AutoStruct)) args.push(snip(`_arg_${i}`, argType, origin));
356
+ case void 0: if (!(argType instanceof AutoStruct)) args.push({
357
+ name: this.makeNameValid(`_arg_${i}`),
358
+ access: () => {
359
+ throw new Error(`Unreachable: Accessing an argument that wasn't named in the function signature`);
360
+ },
361
+ decoratedType: argType,
362
+ used: false
363
+ });
363
364
  }
364
365
  }
365
- const scope = this._itemStateStack.pushFunctionScope(options.functionType, args, Object.fromEntries(argAliases), options.returnType, options.externalMap);
366
+ const scope = this._itemStateStack.pushFunctionScope(options.functionType, argAccess, options.returnType, options.externalMap);
366
367
  fnScopePushed = true;
367
- const body = this.gen.functionDefinition(options.body);
368
- let returnType = options.returnType;
369
- if (returnType instanceof AutoStruct) if (isWgslStruct(scope.reportedReturnTypes.values().next().value)) returnType = returnType.completeStruct;
370
- else returnType = void 0;
371
- if (!returnType) {
372
- const returnTypes = [...scope.reportedReturnTypes];
373
- if (returnTypes.length === 0) returnType = Void;
374
- else {
375
- const conversion = getBestConversion(returnTypes);
376
- if (conversion && !conversion.hasImplicitConversions) returnType = conversion.targetType;
368
+ let returnType;
369
+ const code = this.gen.functionDefinition({
370
+ functionType: options.functionType,
371
+ args,
372
+ body: options.body,
373
+ determineReturnType: () => {
374
+ if (returnType) return returnType;
375
+ returnType = options.returnType;
376
+ if (returnType instanceof AutoStruct) if (isWgslStruct(scope.reportedReturnTypes.values().next().value)) returnType = returnType.completeStruct;
377
+ else returnType = void 0;
378
+ if (!returnType) {
379
+ const returnTypes = [...scope.reportedReturnTypes];
380
+ if (returnTypes.length === 0) returnType = Void;
381
+ else {
382
+ const conversion = getBestConversion(returnTypes);
383
+ if (conversion && !conversion.hasImplicitConversions) returnType = conversion.targetType;
384
+ }
385
+ if (!returnType) throw new Error(`Expected function to have a single return type, got [${returnTypes.join(", ")}]. Cast explicitly to the desired type.`);
386
+ returnType = concretize(returnType);
387
+ if (options.functionType === "vertex" || options.functionType === "fragment") returnType = createIoSchema(returnType);
388
+ }
389
+ return returnType;
377
390
  }
378
- if (!returnType) throw new Error(`Expected function to have a single return type, got [${returnTypes.join(", ")}]. Cast explicitly to the desired type.`);
379
- returnType = concretize(returnType);
380
- if (options.functionType === "vertex" || options.functionType === "fragment") returnType = createIoSchema(returnType);
381
- }
382
- if (options.entryInput) return {
383
- head: `(${pendingHeaderEntries.filter(({ argName }) => isArgUsedInBody(argName, body)).map(({ header }) => header).join(", ")}) ${returnType.type !== "void" ? `-> ${getAttributesString(returnType)}${this.resolve(returnType).value} ` : ""}`,
384
- body,
385
- returnType
386
- };
391
+ });
392
+ if (!returnType) throw new Error(`Failed to determine return type`);
387
393
  return {
388
- head: resolveFunctionHeader(this, args, returnType),
389
- body,
394
+ code,
390
395
  returnType
391
396
  };
392
397
  } finally {
@@ -601,12 +606,6 @@ function resolve(item, options) {
601
606
  logResources: ctx.logResources
602
607
  };
603
608
  }
604
- function isArgUsedInBody(argName, body) {
605
- return new RegExp(`\\b${argName}\\b`).test(body);
606
- }
607
- function resolveFunctionHeader(ctx, args, returnType) {
608
- const argList = args.map((arg) => `${arg.value}: ${ctx.resolve(arg.dataType).value}`).join(", ");
609
- return returnType.type !== "void" ? `(${argList}) -> ${getAttributesString(returnType)}${ctx.resolve(returnType).value} ` : `(${argList}) `;
610
- }
609
+
611
610
  //#endregion
612
- export { ResolutionCtxImpl, resolve };
611
+ export { ResolutionCtxImpl, resolve };
package/shared/env.js CHANGED
@@ -8,5 +8,6 @@
8
8
  */
9
9
  const DEV = process.env.NODE_ENV === "development";
10
10
  const TEST = process.env.NODE_ENV === "test";
11
+
11
12
  //#endregion
12
- export { DEV, TEST };
13
+ export { DEV, TEST };
@@ -9,5 +9,6 @@ function* naturalsExcept(excluded) {
9
9
  next++;
10
10
  }
11
11
  }
12
+
12
13
  //#endregion
13
- export { naturalsExcept };
14
+ export { naturalsExcept };
package/shared/meta.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { version } from "../package.js";
2
2
  import { DEV, TEST } from "./env.js";
3
3
  import { $getNameForward, isMarkedInternal } from "./symbols.js";
4
+
4
5
  //#region src/shared/meta.ts
5
6
  const globalWithMeta = globalThis;
6
7
  if (globalWithMeta.__TYPEGPU_VERSION__ !== void 0) console.warn(`Found duplicate TypeGPU version. First was ${globalWithMeta.__TYPEGPU_VERSION__}, this one is ${version}. This may cause unexpected behavior.`);
@@ -57,5 +58,6 @@ function setMetaData(definition, metaData) {
57
58
  ...metaData
58
59
  });
59
60
  }
61
+
60
62
  //#endregion
61
- export { PERF, getMetaData, getName, hasTinyestMetadata, isNamable, setName };
63
+ export { PERF, getMetaData, getName, hasTinyestMetadata, isNamable, setName };
package/shared/repr.d.ts CHANGED
@@ -7,6 +7,7 @@ import { TgpuTexture } from "../core/texture/texture.js";
7
7
  import { Disarray, Undecorate } from "../data/dataTypes.js";
8
8
 
9
9
  //#region src/shared/repr.d.ts
10
+
10
11
  /**
11
12
  * Extracts the inferred representation of a resource.
12
13
  * For inferring types as seen by the GPU, see {@link InferGPU}
@@ -1,4 +1,5 @@
1
1
  import { isMatInstance, isVecInstance } from "../data/wgslTypes.js";
2
+
2
3
  //#region src/shared/stringify.ts
3
4
  function safeStringify(item) {
4
5
  const asString = String(item);
@@ -16,5 +17,6 @@ function niceStringify(item) {
16
17
  if (item && typeof item === "object") return `{ ${Object.entries(item).map(([key, value]) => `${key}: ${niceStringify(value)}`).join(", ")} }`;
17
18
  return String(item);
18
19
  }
20
+
19
21
  //#endregion
20
- export { niceStringify, safeStringify };
22
+ export { niceStringify, safeStringify };
package/shared/symbols.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { version } from "../package.js";
2
+
2
3
  //#region src/shared/symbols.ts
3
4
  const $internal = Symbol(`typegpu:${version}:$internal`);
4
5
  /**
@@ -32,17 +33,48 @@ const $gpuCallable = Symbol(`typegpu:${version}:$gpuCallable`);
32
33
  * Type token for the inferred (CPU & GPU) representation of a resource
33
34
  */
34
35
  const $repr = Symbol(`typegpu:${version}:$repr`);
35
- Symbol(`typegpu:${version}:$gpuRepr`);
36
- Symbol(`typegpu:${version}:$reprPartial`);
37
- Symbol(`typegpu:${version}:$reprPatch`);
38
- Symbol(`typegpu:${version}:$inRepr`);
39
- Symbol(`typegpu:${version}:$memIdent`);
40
- Symbol(`typegpu:${version}:$invalidStorageSchema`);
41
- Symbol(`typegpu:${version}:$validUniformSchema`);
42
- Symbol(`typegpu:${version}:$validVertexSchema`);
43
- Symbol(`typegpu:${version}:$invalidSchemaReason`);
36
+ /**
37
+ * Type token for the inferred (GPU-side) representation of a resource
38
+ * If present, it shadows the value of `$repr` for GPU-side inference.
39
+ */
40
+ const $gpuRepr = Symbol(`typegpu:${version}:$gpuRepr`);
41
+ /**
42
+ * Type token for the inferred partial representation of a resource.
43
+ * If present, it shadows the value of `$repr` for use in partial IO.
44
+ */
45
+ const $reprPartial = Symbol(`typegpu:${version}:$reprPartial`);
46
+ /**
47
+ * Type token for the inferred patch representation of a resource.
48
+ * Used by the `buffer.patch` API with `Record<number, T>` sparse arrays.
49
+ */
50
+ const $reprPatch = Symbol(`typegpu:${version}:$reprPatch`);
51
+ /**
52
+ * Type token for the write-side (input) representation of a resource.
53
+ */
54
+ const $inRepr = Symbol(`typegpu:${version}:$inRepr`);
55
+ /**
56
+ * Type token holding schemas that are identical in memory layout.
57
+ */
58
+ const $memIdent = Symbol(`typegpu:${version}:$memIdent`);
59
+ /**
60
+ * Type token, signaling that a schema can be used in a storage buffer.
61
+ */
62
+ const $validStorageSchema = Symbol(`typegpu:${version}:$invalidStorageSchema`);
63
+ /**
64
+ * Type token, signaling that a schema can be used in a uniform buffer.
65
+ */
66
+ const $validUniformSchema = Symbol(`typegpu:${version}:$validUniformSchema`);
67
+ /**
68
+ * Type token, signaling that a schema can be used in a vertex buffer.
69
+ */
70
+ const $validVertexSchema = Symbol(`typegpu:${version}:$validVertexSchema`);
71
+ /**
72
+ * Type token, containing a reason for why the schema is invalid (if it is).
73
+ */
74
+ const $invalidSchemaReason = Symbol(`typegpu:${version}:$invalidSchemaReason`);
44
75
  function isMarkedInternal(value) {
45
76
  return !!value?.[$internal];
46
77
  }
78
+
47
79
  //#endregion
48
- export { $cast, $getNameForward, $gpuCallable, $gpuValueOf, $internal, $ownSnippet, $providing, $repr, $resolve, isMarkedInternal };
80
+ export { $cast, $getNameForward, $gpuCallable, $gpuValueOf, $internal, $ownSnippet, $providing, $repr, $resolve, isMarkedInternal };
@@ -2,5 +2,6 @@
2
2
  function assertExhaustive(x, location) {
3
3
  throw new Error(`Failed to handle ${x} at ${location}`);
4
4
  }
5
+
5
6
  //#endregion
6
- export { assertExhaustive };
7
+ export { assertExhaustive };
@@ -59,5 +59,6 @@ const kindToDefaultFormatMap = {
59
59
  vec3i: "sint32x3",
60
60
  vec4i: "sint32x4"
61
61
  };
62
+
62
63
  //#endregion
63
- export { kindToDefaultFormatMap, vertexFormats };
64
+ export { kindToDefaultFormatMap, vertexFormats };
package/std/array.d.ts CHANGED
@@ -2,6 +2,6 @@ import { DualFn } from "../types.js";
2
2
  import { _ref } from "../data/ref.js";
3
3
 
4
4
  //#region src/std/array.d.ts
5
- declare const arrayLength: DualFn;
5
+ declare const arrayLength: DualFn<(a: unknown[] | _ref<unknown[]>) => number>;
6
6
  //#endregion
7
7
  export { arrayLength };
package/std/array.js CHANGED
@@ -4,6 +4,7 @@ import { ptrFn } from "../data/ptr.js";
4
4
  import { isRef } from "../data/ref.js";
5
5
  import { abstractInt, u32 } from "../data/numeric.js";
6
6
  import { dualImpl } from "../core/function/dualImpl.js";
7
+
7
8
  //#region src/std/array.ts
8
9
  const sizeOfPointedToArray = (dataType) => isPtr(dataType) && isWgslArray(dataType.inner) ? dataType.inner.elementCount : 0;
9
10
  const arrayLength = dualImpl({
@@ -21,5 +22,6 @@ const arrayLength = dualImpl({
21
22
  return length > 0 ? `${length}` : stitch`arrayLength(${a})`;
22
23
  }
23
24
  });
25
+
24
26
  //#endregion
25
- export { arrayLength };
27
+ export { arrayLength };
package/std/atomic.d.ts CHANGED
@@ -3,17 +3,17 @@ import { atomicI32, atomicU32 } from "../data/wgslTypes.js";
3
3
 
4
4
  //#region src/std/atomic.d.ts
5
5
  type AnyAtomic = atomicI32 | atomicU32;
6
- declare const workgroupBarrier: DualFn;
7
- declare const storageBarrier: DualFn;
8
- declare const textureBarrier: DualFn;
9
- declare const atomicLoad: DualFn;
10
- declare const atomicStore: DualFn;
11
- declare const atomicAdd: DualFn;
12
- declare const atomicSub: DualFn;
13
- declare const atomicMax: DualFn;
14
- declare const atomicMin: DualFn;
15
- declare const atomicAnd: DualFn;
16
- declare const atomicOr: DualFn;
17
- declare const atomicXor: DualFn;
6
+ declare const workgroupBarrier: DualFn<(...args: never[]) => unknown>;
7
+ declare const storageBarrier: DualFn<(...args: never[]) => unknown>;
8
+ declare const textureBarrier: DualFn<(...args: never[]) => unknown>;
9
+ declare const atomicLoad: DualFn<(<T extends AnyAtomic>(a: T) => number)>;
10
+ declare const atomicStore: DualFn<(<T extends AnyAtomic>(a: T, value: number) => void)>;
11
+ declare const atomicAdd: DualFn<(<T extends AnyAtomic>(a: T, value: number) => number)>;
12
+ declare const atomicSub: DualFn<(<T extends AnyAtomic>(a: T, value: number) => number)>;
13
+ declare const atomicMax: DualFn<(<T extends AnyAtomic>(a: T, value: number) => number)>;
14
+ declare const atomicMin: DualFn<(<T extends AnyAtomic>(a: T, value: number) => number)>;
15
+ declare const atomicAnd: DualFn<(<T extends AnyAtomic>(a: T, value: number) => number)>;
16
+ declare const atomicOr: DualFn<(<T extends AnyAtomic>(a: T, value: number) => number)>;
17
+ declare const atomicXor: DualFn<(<T extends AnyAtomic>(a: T, value: number) => number)>;
18
18
  //#endregion
19
19
  export { atomicAdd, atomicAnd, atomicLoad, atomicMax, atomicMin, atomicOr, atomicStore, atomicSub, atomicXor, storageBarrier, textureBarrier, workgroupBarrier };
package/std/atomic.js CHANGED
@@ -3,6 +3,7 @@ import { stitch } from "../core/resolve/stitch.js";
3
3
  import { safeStringify } from "../shared/stringify.js";
4
4
  import { i32, u32 } from "../data/numeric.js";
5
5
  import { dualImpl } from "../core/function/dualImpl.js";
6
+
6
7
  //#region src/std/atomic.ts
7
8
  const workgroupBarrier = dualImpl({
8
9
  name: "workgroupBarrier",
@@ -107,5 +108,6 @@ const atomicXor = dualImpl({
107
108
  signature: atomicOpSignature,
108
109
  codegenImpl: (_ctx, [a, value]) => stitch`atomicXor(&${a}, ${value})`
109
110
  });
111
+
110
112
  //#endregion
111
- export { atomicAdd, atomicAnd, atomicLoad, atomicMax, atomicMin, atomicOr, atomicStore, atomicSub, atomicXor, storageBarrier, textureBarrier, workgroupBarrier };
113
+ export { atomicAdd, atomicAnd, atomicLoad, atomicMax, atomicMin, atomicOr, atomicStore, atomicSub, atomicXor, storageBarrier, textureBarrier, workgroupBarrier };
package/std/bitcast.d.ts CHANGED
@@ -3,8 +3,8 @@ import { v2f, v2i, v2u, v3f, v3i, v3u, v4f, v4i, v4u } from "../data/wgslTypes.j
3
3
 
4
4
  //#region src/std/bitcast.d.ts
5
5
  type BitcastU32toF32Overload = ((value: number) => number) & ((value: v2u) => v2f) & ((value: v3u) => v3f) & ((value: v4u) => v4f);
6
- declare const bitcastU32toF32: DualFn;
6
+ declare const bitcastU32toF32: DualFn<BitcastU32toF32Overload>;
7
7
  type BitcastU32toI32Overload = ((value: number) => number) & ((value: v2u) => v2i) & ((value: v3u) => v3i) & ((value: v4u) => v4i);
8
- declare const bitcastU32toI32: DualFn;
8
+ declare const bitcastU32toI32: DualFn<BitcastU32toI32Overload>;
9
9
  //#endregion
10
10
  export { bitcastU32toF32, bitcastU32toI32 };
package/std/bitcast.js CHANGED
@@ -6,6 +6,7 @@ import { vec2f, vec2i, vec3f, vec3i, vec4f, vec4i } from "../data/vector.js";
6
6
  import { dualImpl } from "../core/function/dualImpl.js";
7
7
  import { bitcastU32toF32Impl, bitcastU32toI32Impl } from "../data/numberOps.js";
8
8
  import { VectorOps } from "../data/vectorOps.js";
9
+
9
10
  //#region src/std/bitcast.ts
10
11
  const bitcastU32toF32 = dualImpl({
11
12
  name: "bitcastU32toF32",
@@ -37,5 +38,6 @@ const bitcastU32toI32 = dualImpl({
37
38
  };
38
39
  }
39
40
  });
41
+
40
42
  //#endregion
41
- export { bitcastU32toF32, bitcastU32toI32 };
43
+ export { bitcastU32toF32, bitcastU32toI32 };