typegpu 0.12.7 → 0.12.9

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 (84) hide show
  1. package/core/function/autoIO.js +2 -2
  2. package/core/function/comptime.js +7 -5
  3. package/core/function/createCallableSchema.js +4 -3
  4. package/core/function/dualImpl.js +21 -19
  5. package/core/function/ioSchema.d.ts +10 -0
  6. package/core/function/ioSchema.js +37 -16
  7. package/core/function/tgpuFn.d.ts +12 -1
  8. package/core/function/tgpuFn.js +19 -2
  9. package/core/pipeline/computePipeline.js +5 -2
  10. package/core/pipeline/renderPipeline.d.ts +4 -2
  11. package/core/pipeline/renderPipeline.js +17 -12
  12. package/core/pipeline/timeable.js +15 -2
  13. package/core/pipeline/webgpuLimitations.js +9 -2
  14. package/core/root/configurableImpl.js +1 -2
  15. package/core/root/init.d.ts +3 -3
  16. package/core/root/init.js +12 -13
  17. package/core/root/rootTypes.d.ts +4 -1
  18. package/core/slot/accessor.js +3 -3
  19. package/core/texture/externalTexture.d.ts +1 -1
  20. package/core/texture/externalTexture.js +3 -1
  21. package/core/texture/texture.d.ts +3 -2
  22. package/core/texture/texture.js +27 -25
  23. package/core/unroll/tgpuUnroll.d.ts +4 -3
  24. package/core/unroll/tgpuUnroll.js +7 -1
  25. package/core/valueProxyUtils.d.ts +4 -2
  26. package/core/valueProxyUtils.js +4 -2
  27. package/data/compiledIO.js +13 -10
  28. package/data/dataIO.d.ts +10 -6
  29. package/data/dataIO.js +35 -20
  30. package/data/dataTypes.d.ts +0 -5
  31. package/data/dataTypes.js +0 -6
  32. package/data/index.d.ts +4 -3
  33. package/data/index.js +3 -2
  34. package/data/matrixColumnsAccess.d.ts +8 -0
  35. package/data/matrixColumnsAccess.js +15 -0
  36. package/data/offsetUtils.js +181 -102
  37. package/data/ref.d.ts +12 -5
  38. package/data/ref.js +50 -23
  39. package/data/schemasEqual.d.ts +37 -0
  40. package/data/schemasEqual.js +131 -0
  41. package/data/snippet.d.ts +7 -0
  42. package/data/snippet.js +9 -0
  43. package/data/struct.js +5 -0
  44. package/data/wgslTypes.d.ts +4 -4
  45. package/package.json +2 -2
  46. package/resolutionCtx.js +1 -1
  47. package/serial/restore.d.ts +3 -2
  48. package/serial/restore.js +2 -1
  49. package/shared/meta.js +1 -1
  50. package/shared/symbols.d.ts +5 -0
  51. package/shared/symbols.js +6 -1
  52. package/std/comptime.d.ts +67 -0
  53. package/std/comptime.js +90 -0
  54. package/std/index.d.ts +2 -0
  55. package/std/index.js +2 -0
  56. package/std/numeric.d.ts +1 -1
  57. package/std/numeric.js +13 -13
  58. package/std/packing.js +10 -10
  59. package/std/typeOf.d.ts +31 -0
  60. package/std/typeOf.js +56 -0
  61. package/tgpuLogger.d.ts +4 -1
  62. package/tgpuLogger.js +3 -2
  63. package/tgsl/accessIndex.js +18 -15
  64. package/tgsl/accessProp.js +10 -7
  65. package/tgsl/consoleLog/serializers.d.ts +1 -8
  66. package/tgsl/consoleLog/serializers.js +53 -37
  67. package/tgsl/conversion.js +3 -2
  68. package/tgsl/forOfUtils.js +2 -2
  69. package/tgsl/generationHelpers.d.ts +4 -3
  70. package/tgsl/generationHelpers.js +16 -3
  71. package/tgsl/infixDispatch.d.ts +4 -1
  72. package/tgsl/infixDispatch.js +13 -1
  73. package/tgsl/makeDereferenceable.d.ts +1 -1
  74. package/tgsl/makeDereferenceable.js +3 -1
  75. package/tgsl/shellless.js +8 -29
  76. package/tgsl/stringifySnippet.d.ts +9 -0
  77. package/tgsl/stringifySnippet.js +24 -0
  78. package/tgsl/wgslGenerator.d.ts +1 -0
  79. package/tgsl/wgslGenerator.js +98 -66
  80. package/types.d.ts +23 -2
  81. package/types.js +30 -5
  82. package/unwrapper.d.ts +2 -2
  83. package/data/deepEqual.d.ts +0 -21
  84. package/data/deepEqual.js +0 -93
@@ -1,7 +1,7 @@
1
1
  import { isData, undecorate } from "../../data/dataTypes.js";
2
2
  import { schemaCallWrapper } from "../../data/schemaCallWrapper.js";
3
3
  import { isSnippet, snip } from "../../data/snippet.js";
4
- import { deepEqual } from "../../data/deepEqual.js";
4
+ import { schemasEqualStructurally } from "../../data/schemasEqual.js";
5
5
  import { arrayOf } from "../../data/array.js";
6
6
  import { UnknownData } from "../../data/dataTypes.js";
7
7
  import { isAbstract, isWgslArray } from "../../data/wgslTypes.js";
@@ -54,9 +54,9 @@ function matchesSchema(accessorSchema, providedType) {
54
54
  const expected = undecorate(accessorSchema);
55
55
  const provided = undecorate(providedType);
56
56
  if (isWgslArray(expected) && expected.elementCount === 0 && isWgslArray(provided)) {
57
- return deepEqual(expected.elementType, provided.elementType);
57
+ return schemasEqualStructurally(expected.elementType, provided.elementType);
58
58
  }
59
- return deepEqual(expected, provided);
59
+ return schemasEqualStructurally(expected, provided);
60
60
  }
61
61
  /**
62
62
  * Values with a concrete WGSL type (buffers, variables, GPU functions, ...) are used as-is,
@@ -1,7 +1,7 @@
1
1
  import { $gpuValueOf, $internal, $resolve } from '../../shared/symbols.ts';
2
2
  import type { LayoutMembership } from '../../tgpuBindGroupLayout.ts';
3
3
  import { type WgslExternalTexture } from '../../data/texture.ts';
4
- import type { ResolutionCtx, SelfResolvable } from '../../types.ts';
4
+ import { type ResolutionCtx, type SelfResolvable } from '../../types.ts';
5
5
  import { type ResolvedSnippet } from '../../data/snippet.ts';
6
6
  import type { Infer } from '../../shared/repr.ts';
7
7
  export interface TgpuExternalTexture {
@@ -1,8 +1,9 @@
1
- import { $gpuValueOf, $internal, $ownSnippet, $resolve } from "../../shared/symbols.js";
1
+ import { $comptimeValueOf, $gpuValueOf, $internal, $ownSnippet, $resolve, } from "../../shared/symbols.js";
2
2
  import { getName, setName } from "../../shared/meta.js";
3
3
  import { textureExternal } from "../../data/texture.js";
4
4
  import { valueProxyHandler } from "../valueProxyUtils.js";
5
5
  import { inCodegenMode } from "../../execMode.js";
6
+ import { runtimeOnly } from "../../types.js";
6
7
  import { snip } from "../../data/snippet.js";
7
8
  export function isExternalTexture(value) {
8
9
  return value?.resourceType === 'external-texture';
@@ -33,6 +34,7 @@ export class TgpuExternalTextureImpl {
33
34
  get [$ownSnippet]() {
34
35
  return snip(this, schema, 'handle', false);
35
36
  },
37
+ [$comptimeValueOf]: runtimeOnly,
36
38
  [$resolve]: (ctx) => ctx.resolve(this),
37
39
  toString: () => `textureExternal:${getName(this) ?? '<unnamed>'}.$`,
38
40
  }, valueProxyHandler);
@@ -9,7 +9,7 @@ import type { TgpuDeviceOwningSoul, TgpuSoul } from '../../shared/soul.ts';
9
9
  import { $gpuValueOf, $internal, $repr, $resolve, $soul } from '../../shared/symbols.ts';
10
10
  import type { Default, TypedArray, UnionToIntersection } from '../../shared/utilityTypes.ts';
11
11
  import type { LayoutMembership } from '../../tgpuBindGroupLayout.ts';
12
- import type { ResolutionCtx, SelfResolvable } from '../../types.ts';
12
+ import { type ResolutionCtx, type SelfResolvable } from '../../types.ts';
13
13
  import type { ExperimentalTgpuRoot } from '../root/rootTypes.ts';
14
14
  import type { TextureProps } from './textureProps.ts';
15
15
  import type { AllowedUsages, LiteralToExtensionMap, RenderFlag, SampledFlag } from './usageExtension.ts';
@@ -152,6 +152,7 @@ export interface TgpuTextureRenderView {
152
152
  readonly descriptor: TgpuTextureViewDescriptor;
153
153
  }
154
154
  export declare function INTERNAL_createTexture(props: TextureProps, root: ExperimentalTgpuRoot, rawTexture?: GPUTexture): TgpuTexture<TextureProps>;
155
+ export declare function INTERNAL_restoreTextureView(soul: TgpuTextureViewSoul): TgpuTextureView | TgpuTextureRenderView;
155
156
  export declare function isTexture(value: unknown): value is TgpuTexture;
156
157
  export declare function isTextureView(value: unknown): value is TgpuTextureView;
157
158
  export declare class TgpuLaidOutTextureViewImpl<T extends WgslTexture | WgslStorageTexture> implements TgpuTextureView<T>, SelfResolvable {
@@ -174,6 +175,6 @@ export declare class TgpuTextureRenderViewImpl implements TgpuTextureRenderView
174
175
  readonly [$internal]: TextureViewInternals;
175
176
  readonly [$soul]: TgpuTextureViewSoul<'render'>;
176
177
  readonly resourceType: "texture-view";
177
- constructor(baseTexture: TgpuTexture, descriptor?: TgpuTextureViewDescriptor);
178
+ constructor(soul: TgpuTextureViewSoul<'render'>);
178
179
  get descriptor(): TgpuTextureViewDescriptor;
179
180
  }
@@ -3,7 +3,8 @@ import { inCodegenMode } from "../../execMode.js";
3
3
  import { snip } from "../../data/snippet.js";
4
4
  import { getName, setName } from "../../shared/meta.js";
5
5
  import { getTextureFormatInfo, } from "./textureFormats.js";
6
- import { $gpuValueOf, $internal, $ownSnippet, $repr, $resolve, $soul, } from "../../shared/symbols.js";
6
+ import { $gpuValueOf, $internal, $ownSnippet, $comptimeValueOf, $repr, $resolve, $soul, } from "../../shared/symbols.js";
7
+ import { runtimeOnly } from "../../types.js";
7
8
  import { valueProxyHandler } from "../valueProxyUtils.js";
8
9
  import { clearTextureWithColor, generateTextureMipmaps, writeImages } from "./textureUtils.js";
9
10
  import { mipLevelSize, origin3d, resolveImageWrite, writeRegionSize, } from "./textureWrite.js";
@@ -18,6 +19,13 @@ function getDescriptorForProps(props) {
18
19
  export function INTERNAL_createTexture(props, root, rawTexture) {
19
20
  return new TgpuTextureImpl(props, root, rawTexture);
20
21
  }
22
+ export function INTERNAL_restoreTextureView(soul) {
23
+ const { schema } = soul;
24
+ const descriptor = soul.descriptor;
25
+ return schema === 'render'
26
+ ? new TgpuTextureRenderViewImpl({ ...soul, schema, descriptor })
27
+ : new TgpuFixedTextureViewImpl({ ...soul, schema });
28
+ }
21
29
  export function isTexture(value) {
22
30
  return value?.resourceType === 'texture' && !!value[$internal];
23
31
  }
@@ -123,10 +131,15 @@ class TgpuTextureImpl {
123
131
  return this;
124
132
  }
125
133
  createView(schema, viewDescriptor) {
134
+ const descriptor = viewDescriptor;
135
+ const soul = { type: 'texture-view', texture: this, descriptor };
126
136
  if (schema === 'render') {
127
- return new TgpuTextureRenderViewImpl(this, viewDescriptor);
137
+ return new TgpuTextureRenderViewImpl({ ...soul, schema });
128
138
  }
129
- return new TgpuFixedTextureViewImpl(schema ?? textureDescriptorToSchema(getDescriptorForProps(this.props)), this, viewDescriptor);
139
+ return new TgpuFixedTextureViewImpl({
140
+ ...soul,
141
+ schema: schema ?? textureDescriptorToSchema(getDescriptorForProps(this.props)),
142
+ });
130
143
  }
131
144
  #clearMipLevel(mip = 0) {
132
145
  const [width, height, depth] = mipLevelSize(this.props, mip);
@@ -267,15 +280,9 @@ class TgpuFixedTextureViewImpl {
267
280
  [$internal];
268
281
  [$soul];
269
282
  resourceType = 'texture-view';
270
- constructor(schema, baseTexture, descriptor) {
271
- this[$soul] = {
272
- type: 'texture-view',
273
- texture: baseTexture,
274
- schema,
275
- descriptor,
276
- raw: undefined,
277
- label: undefined,
278
- };
283
+ constructor(soul) {
284
+ this[$soul] = { ...soul };
285
+ const { texture, schema, descriptor } = soul;
279
286
  this[$internal] = {
280
287
  unwrap: () => {
281
288
  const soul = this[$soul];
@@ -291,8 +298,7 @@ class TgpuFixedTextureViewImpl {
291
298
  }
292
299
  return soul.raw;
293
300
  },
294
- format: descriptor?.format ??
295
- (isWgslStorageTexture(schema) ? schema.format : baseTexture.props.format),
301
+ format: descriptor?.format ?? (isWgslStorageTexture(schema) ? schema.format : texture.props.format),
296
302
  aspect: descriptor?.aspect,
297
303
  };
298
304
  }
@@ -313,6 +319,7 @@ class TgpuFixedTextureViewImpl {
313
319
  get [$ownSnippet]() {
314
320
  return snip(this, schema, /* origin */ 'handle', false);
315
321
  },
322
+ [$comptimeValueOf]: runtimeOnly,
316
323
  [$resolve]: (ctx) => ctx.resolve(this),
317
324
  toString: () => `${this.toString()}.$`,
318
325
  }, valueProxyHandler);
@@ -375,6 +382,7 @@ export class TgpuLaidOutTextureViewImpl {
375
382
  get [$ownSnippet]() {
376
383
  return snip(this, schema, /* origin */ 'handle', false);
377
384
  },
385
+ [$comptimeValueOf]: runtimeOnly,
378
386
  [$resolve]: (ctx) => ctx.resolve(this),
379
387
  toString: () => `${this.toString()}.$`,
380
388
  }, valueProxyHandler);
@@ -394,15 +402,9 @@ export class TgpuTextureRenderViewImpl {
394
402
  [$internal];
395
403
  [$soul];
396
404
  resourceType = 'texture-view';
397
- constructor(baseTexture, descriptor = {}) {
398
- this[$soul] = {
399
- type: 'texture-view',
400
- texture: baseTexture,
401
- schema: 'render',
402
- descriptor,
403
- raw: undefined,
404
- label: undefined,
405
- };
405
+ constructor(soul) {
406
+ this[$soul] = { ...soul };
407
+ const { texture, descriptor } = soul;
406
408
  this[$internal] = {
407
409
  unwrap: () => {
408
410
  const soul = this[$soul];
@@ -414,8 +416,8 @@ export class TgpuTextureRenderViewImpl {
414
416
  }
415
417
  return soul.raw;
416
418
  },
417
- format: descriptor.format ?? baseTexture.props.format,
418
- aspect: descriptor.aspect,
419
+ format: descriptor?.format ?? texture.props.format,
420
+ aspect: descriptor?.aspect,
419
421
  };
420
422
  }
421
423
  get descriptor() {
@@ -1,15 +1,16 @@
1
- import { $internal, $resolve } from '../../shared/symbols.ts';
1
+ import { $comptimeValueOf, $internal, $resolve } from '../../shared/symbols.ts';
2
+ import { type ComptimeValue, type ResolutionCtx, type SelfResolvable, type WithComptimeValue } from '../../types.ts';
2
3
  import { type ResolvedSnippet, type Snippet } from '../../data/snippet.ts';
3
- import type { ResolutionCtx, SelfResolvable } from '../../types.ts';
4
4
  /**
5
5
  * The result of calling `tgpu.unroll(...)`. The code responsible for
6
6
  * generating shader code can check if the value of a snippet is
7
7
  * an instance of `UnrollableIterable`, and act accordingly.
8
8
  */
9
- export declare class UnrollableIterable implements SelfResolvable {
9
+ export declare class UnrollableIterable implements SelfResolvable, WithComptimeValue {
10
10
  readonly [$internal] = true;
11
11
  readonly snippet: Snippet;
12
12
  constructor(snippet: Snippet);
13
+ [$comptimeValueOf](): ComptimeValue;
13
14
  [$resolve](ctx: ResolutionCtx): ResolvedSnippet;
14
15
  }
15
16
  /**
@@ -1,5 +1,6 @@
1
- import { $gpuCallable, $internal, $resolve } from "../../shared/symbols.js";
1
+ import { $comptimeValueOf, $gpuCallable, $internal, $resolve } from "../../shared/symbols.js";
2
2
  import { setName } from "../../shared/meta.js";
3
+ import { asComptime, } from "../../types.js";
3
4
  import { withValue } from "../../data/snippet.js";
4
5
  /**
5
6
  * The result of calling `tgpu.unroll(...)`. The code responsible for
@@ -12,6 +13,11 @@ export class UnrollableIterable {
12
13
  constructor(snippet) {
13
14
  this.snippet = snippet;
14
15
  }
16
+ [$comptimeValueOf]() {
17
+ // Outside of for-of loops, `tgpu.unroll(...)` is transparent, just like in JS
18
+ const known = asComptime(this.snippet);
19
+ return known ? { comptime: true, value: known.value } : { comptime: false };
20
+ }
15
21
  [$resolve](ctx) {
16
22
  return ctx.resolveSnippet(this.snippet);
17
23
  }
@@ -1,3 +1,5 @@
1
- import { type SelfResolvable, type WithOwnSnippet } from '../types.ts';
2
- export declare const valueProxyHandler: ProxyHandler<SelfResolvable & WithOwnSnippet>;
1
+ import { type SelfResolvable, type WithComptimeValue, type WithOwnSnippet } from '../types.ts';
2
+ type ValueProxyTarget = SelfResolvable & WithOwnSnippet & WithComptimeValue;
3
+ export declare const valueProxyHandler: ProxyHandler<ValueProxyTarget>;
3
4
  export declare function getGpuValueRecursively<T>(value: unknown): T;
5
+ export {};
@@ -1,8 +1,8 @@
1
1
  import { getGPUValue } from "../getGPUValue.js";
2
- import { $internal, $ownSnippet, $resolve } from "../shared/symbols.js";
2
+ import { $comptimeValueOf, $internal, $ownSnippet, $resolve } from "../shared/symbols.js";
3
3
  import { accessIndex } from "../tgsl/accessIndex.js";
4
4
  import { accessProp } from "../tgsl/accessProp.js";
5
- import { getOwnSnippet } from "../types.js";
5
+ import { getOwnSnippet, runtimeOnly, } from "../types.js";
6
6
  export const valueProxyHandler = {
7
7
  get(target, prop) {
8
8
  if (prop in target) {
@@ -26,6 +26,7 @@ export const valueProxyHandler = {
26
26
  [$internal]: true,
27
27
  [$resolve]: (ctx) => ctx.resolveSnippet(accessed),
28
28
  [$ownSnippet]: accessed,
29
+ [$comptimeValueOf]: runtimeOnly,
29
30
  toString: () => `${String(target)}[${prop}]`,
30
31
  }, valueProxyHandler);
31
32
  }
@@ -38,6 +39,7 @@ export const valueProxyHandler = {
38
39
  [$internal]: true,
39
40
  [$resolve]: (ctx) => ctx.resolveSnippet(accessed),
40
41
  [$ownSnippet]: accessed,
42
+ [$comptimeValueOf]: runtimeOnly,
41
43
  toString: () => `${String(target)}.${prop}`,
42
44
  }, valueProxyHandler);
43
45
  },
@@ -1,7 +1,7 @@
1
1
  import { roundUp } from "../mathUtils.js";
2
2
  import { logger } from "../tgpuLogger.js";
3
3
  import { alignmentOf } from "./alignmentOf.js";
4
- import { isDisarray, isUnstruct } from "./dataTypes.js";
4
+ import { isDisarray, isLooseDecorated, isUnstruct } from "./dataTypes.js";
5
5
  import { offsetsForProps } from "./offsets.js";
6
6
  import { sizeOf } from "./sizeOf.js";
7
7
  import { formatToWGSLType, isPackedData } from "./vertexFormatData.js";
@@ -112,7 +112,7 @@ const vertexFormatValueTransform = {
112
112
  const specialPackedFormats = {
113
113
  'unorm10-10-10-2': {
114
114
  writeFunction: 'setUint32',
115
- generator: (offsetExpr, valueExpr) => `output.setUint32(${offsetExpr}, ((${valueExpr}.x*1023&0x3FF)<<22)|((${valueExpr}.y*1023&0x3FF)<<12)|((${valueExpr}.z*1023&0x3FF)<<2)|(${valueExpr}.w*3&3), littleEndian);\n`,
115
+ generator: (offsetExpr, valueExpr) => `output.setUint32(${offsetExpr}, ((Math.round(${valueExpr}.x*1023)&0x3FF)<<22)|((Math.round(${valueExpr}.y*1023)&0x3FF)<<12)|((Math.round(${valueExpr}.z*1023)&0x3FF)<<2)|(Math.round(${valueExpr}.w*3)&3), littleEndian);\n`,
116
116
  },
117
117
  'unorm8x4-bgra': {
118
118
  writeFunction: 'setUint8',
@@ -131,7 +131,7 @@ export function buildWriter(node, offsetExpr, valueExpr, depth = 0, partial = fa
131
131
  const emitBlock = (addr, block) => partial ? `if (${addr} < endOffset) {\n${block}}\n` : block;
132
132
  function go(node, offsetExpr, valueExpr, depth) {
133
133
  const loopVar = ['i', 'j', 'k'][depth] || `i${depth}`;
134
- if (wgsl.isAtomic(node) || wgsl.isDecorated(node)) {
134
+ if (wgsl.isAtomic(node) || wgsl.isDecorated(node) || isLooseDecorated(node)) {
135
135
  return go(node.inner, offsetExpr, valueExpr, depth);
136
136
  }
137
137
  if (wgsl.isWgslStruct(node) || isUnstruct(node)) {
@@ -178,15 +178,18 @@ export function buildWriter(node, offsetExpr, valueExpr, depth = 0, partial = fa
178
178
  const primitive = typeToPrimitive[node.type];
179
179
  const writeFunc = primitiveToWriteFunction[primitive];
180
180
  const matSize = wgsl.isMat2x2f(node) ? 2 : wgsl.isMat3x3f(node) ? 3 : 4;
181
- const rowStride = roundUp(matSize * 4, 8);
182
181
  const components = ['x', 'y', 'z', 'w'];
183
- const wgslElementStride = rowStride / 4;
184
- return Array.from({ length: matSize * matSize }, (_, idx) => {
185
- const col = Math.floor(idx / matSize);
186
- const row = idx % matSize;
182
+ const wgslElementStride = roundUp(matSize, 2);
183
+ return Array.from({ length: matSize * wgslElementStride }, (_, idx) => {
184
+ const col = Math.floor(idx / wgslElementStride);
185
+ const row = idx % wgslElementStride;
186
+ const addr = `(${offsetExpr} + ${idx * 4})`;
187
+ if (row >= matSize) {
188
+ // mat3x3f padding
189
+ return emitWrite(writeFunc, addr, '0');
190
+ }
187
191
  const packedIndex = col * matSize + row;
188
- const wgslIndex = col * wgslElementStride + row;
189
- return emitWrite(writeFunc, `(${offsetExpr} + ${col * rowStride + row * 4})`, `ArrayBuffer.isView(${valueExpr}) ? ${valueExpr}[${wgslIndex}] : Array.isArray(${valueExpr}) ? ${valueExpr}[${packedIndex}] : ${valueExpr}.columns[${col}].${components[row]}`);
192
+ return emitWrite(writeFunc, addr, `ArrayBuffer.isView(${valueExpr}) ? ${valueExpr}[${idx}] : Array.isArray(${valueExpr}) ? ${valueExpr}[${packedIndex}] : ${valueExpr}.columns[${col}].${components[row]}`);
190
193
  }).join('');
191
194
  }
192
195
  if (isPackedData(node)) {
package/data/dataIO.d.ts CHANGED
@@ -1,13 +1,17 @@
1
1
  import { type ISerialInput, type ISerialOutput } from 'typed-binary';
2
2
  import type { Infer, InferInput } from '../shared/repr.ts';
3
- import type * as wgsl from './wgslTypes.ts';
4
- import { type BaseData } from './wgslTypes.ts';
3
+ import * as wgsl from './wgslTypes.ts';
5
4
  import type { BufferWriteOptions } from '../core/buffer/buffer.ts';
6
- export declare function writeData<TData extends wgsl.BaseData>(output: ISerialOutput, schema: TData, value: Infer<TData>): void;
5
+ export declare function writeData<TData extends wgsl.BaseData>(output: ISerialOutput, schema: TData, value: InferInput<TData>): void;
7
6
  export declare function readData<TData extends wgsl.BaseData>(input: ISerialInput, schema: TData): Infer<TData>;
8
- export declare function calculateOffsets<T extends BaseData>(options: BufferWriteOptions | undefined, schema: T, data: InferInput<T> | ArrayBuffer | ArrayBufferView): {
7
+ export declare function calculateOffsets<T extends wgsl.BaseData>(options: BufferWriteOptions | undefined, schema: T, data: InferInput<T> | ArrayBuffer | ArrayBufferView): {
9
8
  startOffset: number;
10
9
  endOffset: number;
11
10
  };
12
- export declare function writeToArrayBuffer<T extends BaseData>(buffer: ArrayBuffer, schema: T, data: InferInput<T> | ArrayBuffer | ArrayBufferView, options?: BufferWriteOptions): void;
13
- export declare function readFromArrayBuffer<T extends BaseData>(buffer: ArrayBuffer, schema: T): Infer<T>;
11
+ /**
12
+ * A function for filling in buffers with data based on TypeGPU schemas.
13
+ * When schema instances or JS arrays are passed, the padding is applied automatically.
14
+ * ArrayBuffers are expected to be already padded.
15
+ */
16
+ export declare function writeToArrayBuffer<T extends wgsl.BaseData>(buffer: ArrayBuffer, schema: T, data: InferInput<T> | ArrayBuffer | ArrayBufferView, options?: BufferWriteOptions): void;
17
+ export declare function readFromArrayBuffer<T extends wgsl.BaseData>(buffer: ArrayBuffer, schema: T): Infer<T>;
package/data/dataIO.js CHANGED
@@ -4,7 +4,7 @@ import { alignmentOf, customAlignmentOf } from "./alignmentOf.js";
4
4
  import { mat2x2f, mat3x3f, mat4x4f } from "./matrix.js";
5
5
  import { sizeOf } from "./sizeOf.js";
6
6
  import { vec2f, vec2h, vec2i, vec2u, vec3f, vec3h, vec3i, vec3u, vec4f, vec4h, vec4i, vec4u, } from "./vector.js";
7
- import { isWgslArray } from "./wgslTypes.js";
7
+ import * as wgsl from "./wgslTypes.js";
8
8
  import { getCompiledWriter } from "./compiledIO.js";
9
9
  import { getName } from "../shared/meta.js";
10
10
  import { roundUp } from "../mathUtils.js";
@@ -104,8 +104,18 @@ const dataWriters = {
104
104
  }
105
105
  },
106
106
  mat3x3f(output, _, value) {
107
- for (let i = 0; i < value.length; ++i) {
108
- output.writeFloat32(value[i]);
107
+ if (value instanceof Float32Array || wgsl.isMatInstance(value)) {
108
+ for (let i = 0; i < value.length; ++i) {
109
+ output.writeFloat32(value[i]);
110
+ }
111
+ }
112
+ else {
113
+ for (let i = 0; i < 3; ++i) {
114
+ for (let j = 0; j < 3; ++j) {
115
+ output.writeFloat32(value[3 * i + j]);
116
+ }
117
+ output.writeFloat32(0);
118
+ }
109
119
  }
110
120
  },
111
121
  mat4x4f(output, _, value) {
@@ -228,17 +238,17 @@ const dataWriters = {
228
238
  output.writeInt16(value.w);
229
239
  },
230
240
  unorm16(output, _, value) {
231
- output.writeUint16(value * 65535);
241
+ output.writeUint16(Math.round(value * 65535));
232
242
  },
233
243
  unorm16x2(output, _, value) {
234
- output.writeUint16(value.x * 65535);
235
- output.writeUint16(value.y * 65535);
244
+ output.writeUint16(Math.round(value.x * 65535));
245
+ output.writeUint16(Math.round(value.y * 65535));
236
246
  },
237
247
  unorm16x4(output, _, value) {
238
- output.writeUint16(value.x * 65535);
239
- output.writeUint16(value.y * 65535);
240
- output.writeUint16(value.z * 65535);
241
- output.writeUint16(value.w * 65535);
248
+ output.writeUint16(Math.round(value.x * 65535));
249
+ output.writeUint16(Math.round(value.y * 65535));
250
+ output.writeUint16(Math.round(value.z * 65535));
251
+ output.writeUint16(Math.round(value.w * 65535));
242
252
  },
243
253
  snorm16(output, _, value) {
244
254
  output.writeInt16(Math.round(value * 32767));
@@ -322,17 +332,17 @@ const dataWriters = {
322
332
  },
323
333
  'unorm10-10-10-2'(output, _, value) {
324
334
  let packed = 0;
325
- packed |= ((value.x * 1023) & 1023) << 22; // r (10 bits)
326
- packed |= ((value.y * 1023) & 1023) << 12; // g (10 bits)
327
- packed |= ((value.z * 1023) & 1023) << 2; // b (10 bits)
328
- packed |= (value.w * 3) & 3; // a (2 bits)
335
+ packed |= (Math.round(value.x * 1023) & 1023) << 22; // r (10 bits)
336
+ packed |= (Math.round(value.y * 1023) & 1023) << 12; // g (10 bits)
337
+ packed |= (Math.round(value.z * 1023) & 1023) << 2; // b (10 bits)
338
+ packed |= Math.round(value.w * 3) & 3; // a (2 bits)
329
339
  output.writeUint32(packed);
330
340
  },
331
341
  'unorm8x4-bgra'(output, _, value) {
332
- output.writeUint8(value.z * 255);
333
- output.writeUint8(value.y * 255);
334
- output.writeUint8(value.x * 255);
335
- output.writeUint8(value.w * 255);
342
+ output.writeUint8(Math.round(value.z * 255));
343
+ output.writeUint8(Math.round(value.y * 255));
344
+ output.writeUint8(Math.round(value.x * 255));
345
+ output.writeUint8(Math.round(value.w * 255));
336
346
  },
337
347
  disarray(output, schema, value) {
338
348
  const alignment = alignmentOf(schema);
@@ -555,7 +565,7 @@ const dataReaders = {
555
565
  sint32x4: (i) => vec4i(i.readInt32(), i.readInt32(), i.readInt32(), i.readInt32()),
556
566
  'unorm10-10-10-2'(i) {
557
567
  const packed = i.readUint32();
558
- const r = (packed >> 22) / 1023;
568
+ const r = (packed >>> 22) / 1023;
559
569
  const g = ((packed >> 12) & 1023) / 1023;
560
570
  const b = ((packed >> 2) & 1023) / 1023;
561
571
  const a = (packed & 3) / 3;
@@ -607,7 +617,7 @@ export function calculateOffsets(options, schema, data) {
607
617
  const bufferSize = sizeOf(schema);
608
618
  const startOffset = options?.startOffset ?? 0;
609
619
  let naturalSize = undefined;
610
- if (isWgslArray(schema) && Array.isArray(data)) {
620
+ if (wgsl.isWgslArray(schema) && Array.isArray(data)) {
611
621
  const arrayData = data;
612
622
  naturalSize =
613
623
  arrayData.length * roundUp(sizeOf(schema.elementType), alignmentOf(schema.elementType));
@@ -619,6 +629,11 @@ export function calculateOffsets(options, schema, data) {
619
629
  const endOffset = options?.endOffset ?? naturalEndOffset ?? bufferSize;
620
630
  return { startOffset, endOffset };
621
631
  }
632
+ /**
633
+ * A function for filling in buffers with data based on TypeGPU schemas.
634
+ * When schema instances or JS arrays are passed, the padding is applied automatically.
635
+ * ArrayBuffers are expected to be already padded.
636
+ */
622
637
  export function writeToArrayBuffer(buffer, schema, data, options) {
623
638
  const { startOffset, endOffset } = calculateOffsets(options, schema, data);
624
639
  // Fast path: raw byte copy, user guarantees the padded layout
@@ -3,7 +3,6 @@ import type { Infer, InferGPURecord, InferInput, InferInputRecord, InferPatch, I
3
3
  import type { $gpuRepr, $inRepr, $invalidSchemaReason, $memIdent, $repr, $reprPartial, $reprPatch, $validVertexSchema } from '../shared/symbols.ts';
4
4
  import type { Prettify } from '../shared/utilityTypes.ts';
5
5
  import type { WgslExternalTexture, WgslStorageTexture, WgslTexture } from './texture.ts';
6
- import type { Snippet } from './snippet.ts';
7
6
  import type { PackedData } from './vertexFormatData.ts';
8
7
  import * as wgsl from './wgslTypes.ts';
9
8
  import type { WgslComparisonSampler, WgslSampler } from './sampler.ts';
@@ -130,8 +129,4 @@ export type AnyData = wgsl.AnyWgslData | AnyLooseData;
130
129
  export type AnyConcreteData = Exclude<AnyData, wgsl.AbstractInt | wgsl.AbstractFloat | wgsl.Void | WgslTexture | WgslStorageTexture | WgslExternalTexture | WgslSampler | WgslComparisonSampler>;
131
130
  export declare const UnknownData: unique symbol;
132
131
  export type UnknownData = typeof UnknownData;
133
- export declare class MatrixColumnsAccess {
134
- readonly matrix: Snippet;
135
- constructor(matrix: Snippet);
136
- }
137
132
  export {};
package/data/dataTypes.js CHANGED
@@ -73,9 +73,3 @@ export function isData(value) {
73
73
  return wgsl.isWgslData(value) || isLooseData(value);
74
74
  }
75
75
  export const UnknownData = Symbol('UNKNOWN');
76
- export class MatrixColumnsAccess {
77
- matrix;
78
- constructor(matrix) {
79
- this.matrix = matrix;
80
- }
81
- }
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";
@@ -0,0 +1,8 @@
1
+ import { $comptimeValueOf } from '../shared/symbols.ts';
2
+ import { type ComptimeValue, type WithComptimeValue } from '../types.ts';
3
+ import type { Snippet } from './snippet.ts';
4
+ export declare class MatrixColumnsAccess implements WithComptimeValue {
5
+ readonly matrix: Snippet;
6
+ constructor(matrix: Snippet);
7
+ [$comptimeValueOf](): ComptimeValue;
8
+ }
@@ -0,0 +1,15 @@
1
+ import { $comptimeValueOf } from "../shared/symbols.js";
2
+ import { asComptime } from "../types.js";
3
+ export class MatrixColumnsAccess {
4
+ matrix;
5
+ constructor(matrix) {
6
+ this.matrix = matrix;
7
+ }
8
+ [$comptimeValueOf]() {
9
+ // Same as accessing `.columns` on a matrix in JS
10
+ const known = asComptime(this.matrix);
11
+ return known
12
+ ? { comptime: true, value: known.value.columns }
13
+ : { comptime: false };
14
+ }
15
+ }