typegpu 0.12.5 → 0.12.7

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 (99) hide show
  1. package/common/index.d.ts +1 -0
  2. package/common/index.js +1 -0
  3. package/common/writeChannels.d.ts +31 -0
  4. package/common/writeChannels.js +32 -0
  5. package/common/writeSoA.js +16 -0
  6. package/core/buffer/buffer.js +47 -11
  7. package/core/commandEncoder/attachments.js +5 -2
  8. package/core/commandEncoder/computePass.d.ts +13 -0
  9. package/core/commandEncoder/computePass.js +5 -2
  10. package/core/commandEncoder/renderPass.d.ts +18 -4
  11. package/core/commandEncoder/renderPass.js +6 -2
  12. package/core/declare/tgpuDeclare.d.ts +11 -0
  13. package/core/declare/tgpuDeclare.js +1 -1
  14. package/core/function/autoIO.js +4 -2
  15. package/core/function/ioSchema.d.ts +7 -2
  16. package/core/function/ioSchema.js +41 -15
  17. package/core/function/tgpuFragmentFn.d.ts +1 -1
  18. package/core/function/tgpuFragmentFn.js +2 -1
  19. package/core/function/tgpuVertexFn.js +2 -1
  20. package/core/immediate/immediateVar.d.ts +52 -0
  21. package/core/immediate/immediateVar.js +149 -0
  22. package/core/pipeline/computePipeline.d.ts +17 -0
  23. package/core/pipeline/computePipeline.js +23 -9
  24. package/core/pipeline/connectAttachmentToShader.d.ts +2 -2
  25. package/core/pipeline/connectAttachmentToShader.js +16 -21
  26. package/core/pipeline/connectTargetsToShader.d.ts +5 -1
  27. package/core/pipeline/connectTargetsToShader.js +23 -18
  28. package/core/pipeline/drawState.d.ts +8 -4
  29. package/core/pipeline/drawState.js +41 -51
  30. package/core/pipeline/priors.js +1 -0
  31. package/core/pipeline/renderPipeline.d.ts +22 -4
  32. package/core/pipeline/renderPipeline.js +25 -13
  33. package/core/resolve/namespace.js +2 -2
  34. package/core/root/init.js +3 -0
  35. package/core/root/rootTypes.d.ts +17 -2
  36. package/core/slot/accessor.d.ts +1 -1
  37. package/core/slot/accessor.js +72 -5
  38. package/core/slot/slotTypes.d.ts +2 -1
  39. package/core/texture/texture.d.ts +20 -14
  40. package/core/texture/texture.js +78 -69
  41. package/core/texture/textureUtils.d.ts +3 -6
  42. package/core/texture/textureUtils.js +142 -165
  43. package/core/texture/textureWrite.d.ts +61 -0
  44. package/core/texture/textureWrite.js +59 -0
  45. package/data/autoStruct.d.ts +1 -1
  46. package/data/autoStruct.js +8 -4
  47. package/data/dataIO.d.ts +2 -2
  48. package/data/deepEqual.js +19 -9
  49. package/data/matrix.js +10 -3
  50. package/data/partialIO.d.ts +8 -1
  51. package/data/partialIO.js +36 -19
  52. package/data/ref.js +2 -2
  53. package/data/snippet.d.ts +11 -5
  54. package/data/snippet.js +9 -4
  55. package/data/wgslTypes.d.ts +4 -1
  56. package/errors.d.ts +7 -0
  57. package/errors.js +15 -0
  58. package/indexNamedExports.d.ts +5 -2
  59. package/indexNamedExports.js +2 -1
  60. package/internal.d.ts +2 -1
  61. package/internal.js +1 -1
  62. package/nameUtils.d.ts +7 -0
  63. package/nameUtils.js +60 -0
  64. package/package.json +2 -2
  65. package/resolutionCtx.d.ts +5 -0
  66. package/resolutionCtx.js +11 -0
  67. package/serial/restore.d.ts +3 -1
  68. package/serial/restore.js +2 -0
  69. package/shared/meta.js +1 -1
  70. package/shared/symbols.js +1 -1
  71. package/shared/tseynit.d.ts +1 -0
  72. package/shared/tseynit.js +80 -44
  73. package/std/atomic.d.ts +13 -0
  74. package/std/atomic.js +63 -29
  75. package/std/boolean.js +9 -10
  76. package/std/derivative.js +14 -9
  77. package/std/index.d.ts +4 -4
  78. package/std/index.js +5 -5
  79. package/std/numeric.d.ts +5 -1
  80. package/std/numeric.js +3 -3
  81. package/std/operators.js +9 -4
  82. package/std/packing.d.ts +61 -1
  83. package/std/packing.js +214 -25
  84. package/std/subgroup.d.ts +4 -0
  85. package/std/subgroup.js +34 -0
  86. package/std/texture.d.ts +15 -2
  87. package/std/texture.js +48 -0
  88. package/tgpuUnstable.d.ts +1 -0
  89. package/tgpuUnstable.js +1 -0
  90. package/tgsl/accessIndex.js +19 -23
  91. package/tgsl/conversion.d.ts +2 -0
  92. package/tgsl/conversion.js +3 -0
  93. package/tgsl/forOfUtils.js +6 -6
  94. package/tgsl/generationHelpers.js +2 -2
  95. package/tgsl/shaderGenerator.d.ts +1 -1
  96. package/tgsl/wgslGenerator.d.ts +5 -0
  97. package/tgsl/wgslGenerator.js +288 -58
  98. package/types.d.ts +9 -1
  99. package/types.js +5 -0
package/std/boolean.js CHANGED
@@ -9,6 +9,7 @@ import { isBool, isVecBool, isVecInstance, } from "../data/wgslTypes.js";
9
9
  import { SignatureNotSupportedError, WgslTypeError } from "../errors.js";
10
10
  import { unify } from "../tgsl/conversion.js";
11
11
  import { cpuCopy } from "./copy.js";
12
+ import { coerceToSnippet } from "../tgsl/generationHelpers.js";
12
13
  function correspondingBooleanVectorSchema(dataType) {
13
14
  if (dataType.type.includes('2')) {
14
15
  return vec2b;
@@ -30,7 +31,7 @@ export const allEq = dualImpl({
30
31
  name: 'allEq',
31
32
  signature: (...argTypes) => ({ argTypes, returnType: bool }),
32
33
  normalImpl: (lhs, rhs) => cpuAll(cpuEq(lhs, rhs)),
33
- codegenImpl: (_ctx, [lhs, rhs]) => stitch `all(${lhs} == ${rhs})`,
34
+ codegenImpl: (ctx, [lhs, rhs]) => stitch `all(${ctx.gen.emitBinaryOp(lhs, '==', rhs)})`,
34
35
  sideEffects: false,
35
36
  });
36
37
  const cpuEq = (lhs, rhs) => {
@@ -54,7 +55,7 @@ export const eq = dualImpl({
54
55
  returnType: correspondingBooleanVectorSchema(argTypes[0]),
55
56
  }),
56
57
  normalImpl: cpuEq,
57
- codegenImpl: (_ctx, [lhs, rhs]) => stitch `(${lhs} == ${rhs})`,
58
+ codegenImpl: (ctx, [lhs, rhs]) => ctx.gen.emitBinaryOp(lhs, '==', rhs),
58
59
  sideEffects: false,
59
60
  });
60
61
  /**
@@ -72,7 +73,7 @@ export const ne = dualImpl({
72
73
  returnType: correspondingBooleanVectorSchema(argTypes[0]),
73
74
  }),
74
75
  normalImpl: (lhs, rhs) => cpuNot(cpuEq(lhs, rhs)),
75
- codegenImpl: (_ctx, [lhs, rhs]) => stitch `(${lhs} != ${rhs})`,
76
+ codegenImpl: (ctx, [lhs, rhs]) => ctx.gen.emitBinaryOp(lhs, '!=', rhs),
76
77
  sideEffects: false,
77
78
  });
78
79
  const cpuLt = (lhs, rhs) => {
@@ -95,7 +96,7 @@ export const lt = dualImpl({
95
96
  returnType: correspondingBooleanVectorSchema(argTypes[0]),
96
97
  }),
97
98
  normalImpl: cpuLt,
98
- codegenImpl: (_ctx, [lhs, rhs]) => stitch `(${lhs} < ${rhs})`,
99
+ codegenImpl: (ctx, [lhs, rhs]) => ctx.gen.emitBinaryOp(lhs, '<', rhs),
99
100
  sideEffects: false,
100
101
  });
101
102
  /**
@@ -113,7 +114,7 @@ export const le = dualImpl({
113
114
  returnType: correspondingBooleanVectorSchema(argTypes[0]),
114
115
  }),
115
116
  normalImpl: (lhs, rhs) => cpuOr(cpuLt(lhs, rhs), cpuEq(lhs, rhs)),
116
- codegenImpl: (_ctx, [lhs, rhs]) => stitch `(${lhs} <= ${rhs})`,
117
+ codegenImpl: (ctx, [lhs, rhs]) => ctx.gen.emitBinaryOp(lhs, '<=', rhs),
117
118
  sideEffects: false,
118
119
  });
119
120
  /**
@@ -131,7 +132,7 @@ export const gt = dualImpl({
131
132
  returnType: correspondingBooleanVectorSchema(argTypes[0]),
132
133
  }),
133
134
  normalImpl: (lhs, rhs) => cpuAnd(cpuNot(cpuLt(lhs, rhs)), cpuNot(cpuEq(lhs, rhs))),
134
- codegenImpl: (_ctx, [lhs, rhs]) => stitch `(${lhs} > ${rhs})`,
135
+ codegenImpl: (ctx, [lhs, rhs]) => ctx.gen.emitBinaryOp(lhs, '>', rhs),
135
136
  sideEffects: false,
136
137
  });
137
138
  /**
@@ -149,7 +150,7 @@ export const ge = dualImpl({
149
150
  returnType: correspondingBooleanVectorSchema(argTypes[0]),
150
151
  }),
151
152
  normalImpl: (lhs, rhs) => cpuNot(cpuLt(lhs, rhs)),
152
- codegenImpl: (_ctx, [lhs, rhs]) => stitch `(${lhs} >= ${rhs})`,
153
+ codegenImpl: (ctx, [lhs, rhs]) => ctx.gen.emitBinaryOp(lhs, '>=', rhs),
153
154
  sideEffects: false,
154
155
  });
155
156
  function cpuNot(value) {
@@ -274,9 +275,7 @@ export const isCloseTo = dualImpl({
274
275
  return stitch `(abs(f32(${lhs}) - f32(${rhs})) <= ${precision})`;
275
276
  }
276
277
  if (!isSnippetNumeric(lhs) && !isSnippetNumeric(rhs)) {
277
- // https://www.w3.org/TR/WGSL/#vector-multi-component:~:text=Binary%20arithmetic%20expressions%20with%20mixed%20scalar%20and%20vector%20operands
278
- // (a-a)+prec creates a vector of a.length elements, all equal to prec
279
- return stitch `all(abs(${lhs} - ${rhs}) <= (${lhs} - ${lhs}) + ${precision})`;
278
+ return stitch `all(abs(${lhs} - ${rhs}) <= ${_ctx.gen.typeInstantiation(lhs.dataType, [coerceToSnippet(precision)])})`;
280
279
  }
281
280
  return 'false';
282
281
  },
package/std/derivative.js CHANGED
@@ -1,66 +1,71 @@
1
1
  import { dualImpl } from "../core/function/dualImpl.js";
2
2
  import { stitch } from "../core/resolve/stitch.js";
3
+ import { f32 } from "../data/numeric.js";
4
+ import { vec2f, vec3f, vec4f } from "../data/vector.js";
5
+ import { unifyRestrictedSignature } from "./numeric.js";
3
6
  const derivativeNormalError = 'Derivative builtins are not allowed on the CPU';
7
+ // WGSL derivative builtins only accept f32 and vecN<f32>
8
+ const derivativeSignature = unifyRestrictedSignature([f32, vec2f, vec3f, vec4f]);
4
9
  export const dpdx = dualImpl({
5
10
  name: 'dpdx',
6
11
  normalImpl: derivativeNormalError,
7
- signature: (value) => ({ argTypes: [value], returnType: value }),
12
+ signature: derivativeSignature,
8
13
  codegenImpl: (ctx, [value]) => ctx.gen.emitCall('dpdx', [], [value]),
9
14
  sideEffects: false,
10
15
  });
11
16
  export const dpdxCoarse = dualImpl({
12
17
  name: 'dpdxCoarse',
13
18
  normalImpl: derivativeNormalError,
14
- signature: (value) => ({ argTypes: [value], returnType: value }),
19
+ signature: derivativeSignature,
15
20
  codegenImpl: (_ctx, [value]) => stitch `dpdxCoarse(${value})`,
16
21
  sideEffects: false,
17
22
  });
18
23
  export const dpdxFine = dualImpl({
19
24
  name: 'dpdxFine',
20
25
  normalImpl: derivativeNormalError,
21
- signature: (value) => ({ argTypes: [value], returnType: value }),
26
+ signature: derivativeSignature,
22
27
  codegenImpl: (_ctx, [value]) => stitch `dpdxFine(${value})`,
23
28
  sideEffects: false,
24
29
  });
25
30
  export const dpdy = dualImpl({
26
31
  name: 'dpdy',
27
32
  normalImpl: derivativeNormalError,
28
- signature: (value) => ({ argTypes: [value], returnType: value }),
33
+ signature: derivativeSignature,
29
34
  codegenImpl: (ctx, [value]) => ctx.gen.emitCall('dpdy', [], [value]),
30
35
  sideEffects: false,
31
36
  });
32
37
  export const dpdyCoarse = dualImpl({
33
38
  name: 'dpdyCoarse',
34
39
  normalImpl: derivativeNormalError,
35
- signature: (value) => ({ argTypes: [value], returnType: value }),
40
+ signature: derivativeSignature,
36
41
  codegenImpl: (_ctx, [value]) => stitch `dpdyCoarse(${value})`,
37
42
  sideEffects: false,
38
43
  });
39
44
  export const dpdyFine = dualImpl({
40
45
  name: 'dpdyFine',
41
46
  normalImpl: derivativeNormalError,
42
- signature: (value) => ({ argTypes: [value], returnType: value }),
47
+ signature: derivativeSignature,
43
48
  codegenImpl: (_ctx, [value]) => stitch `dpdyFine(${value})`,
44
49
  sideEffects: false,
45
50
  });
46
51
  export const fwidth = dualImpl({
47
52
  name: 'fwidth',
48
53
  normalImpl: derivativeNormalError,
49
- signature: (value) => ({ argTypes: [value], returnType: value }),
54
+ signature: derivativeSignature,
50
55
  codegenImpl: (ctx, [value]) => ctx.gen.emitCall('fwidth', [], [value]),
51
56
  sideEffects: false,
52
57
  });
53
58
  export const fwidthCoarse = dualImpl({
54
59
  name: 'fwidthCoarse',
55
60
  normalImpl: derivativeNormalError,
56
- signature: (value) => ({ argTypes: [value], returnType: value }),
61
+ signature: derivativeSignature,
57
62
  codegenImpl: (_ctx, [value]) => stitch `fwidthCoarse(${value})`,
58
63
  sideEffects: false,
59
64
  });
60
65
  export const fwidthFine = dualImpl({
61
66
  name: 'fwidthFine',
62
67
  normalImpl: derivativeNormalError,
63
- signature: (value) => ({ argTypes: [value], returnType: value }),
68
+ signature: derivativeSignature,
64
69
  codegenImpl: (_ctx, [value]) => stitch `fwidthFine(${value})`,
65
70
  sideEffects: false,
66
71
  });
package/std/index.d.ts CHANGED
@@ -8,12 +8,12 @@ export { add, bitShiftLeft, bitShiftRight, div, mod, mul, neg, sub } from './ope
8
8
  export { rotateX4, rotateY4, rotateZ4, scale4, translate4 } from './matrix.ts';
9
9
  export { identity2, identity3, identity4, rotationX4, rotationY4, rotationZ4, scaling4, translation4, } from '../data/matrix.ts';
10
10
  export { allEq, eq, ne, lt, le, gt, ge, not, or, and, all, any, isCloseTo, select, } from './boolean.ts';
11
- export { atomicAdd, atomicAnd, atomicLoad, atomicMax, atomicMin, atomicOr, atomicStore, atomicSub, atomicXor, workgroupBarrier, storageBarrier, textureBarrier, } from './atomic.ts';
11
+ export { atomicAdd, atomicAnd, atomicCompareExchangeWeak, atomicExchange, atomicLoad, atomicMax, atomicMin, atomicOr, atomicStore, atomicSub, atomicXor, workgroupBarrier, storageBarrier, textureBarrier, workgroupUniformLoad, } from './atomic.ts';
12
12
  export { dpdx, dpdxCoarse, dpdxFine, dpdy, dpdyCoarse, dpdyFine, fwidth, fwidthCoarse, fwidthFine, } from './derivative.ts';
13
13
  export { arrayLength } from './array.ts';
14
- export { pack4x8unorm, pack2x16float, unpack4x8unorm, unpack2x16float, } from './packing.ts';
15
- export { textureDimensions, textureGather, textureLoad, textureSample, textureSampleBaseClampToEdge, textureSampleBias, textureSampleCompare, textureSampleCompareLevel, textureSampleGrad, textureSampleLevel, textureStore, } from './texture.ts';
16
- export { subgroupAdd, subgroupAll, subgroupAnd, subgroupAny, subgroupBallot, subgroupBroadcast, subgroupBroadcastFirst, subgroupElect, subgroupExclusiveAdd, subgroupExclusiveMul, subgroupInclusiveAdd, subgroupInclusiveMul, subgroupMax, subgroupMin, subgroupMul, subgroupOr, subgroupShuffle, subgroupShuffleDown, subgroupShuffleUp, subgroupShuffleXor, subgroupXor, } from './subgroup.ts';
14
+ export { pack2x16float, pack2x16snorm, pack2x16unorm, pack4x8snorm, pack4x8unorm, pack4xI8, pack4xI8Clamp, pack4xU8, pack4xU8Clamp, unpack2x16float, unpack2x16snorm, unpack2x16unorm, unpack4x8snorm, unpack4x8unorm, unpack4xI8, unpack4xU8, } from './packing.ts';
15
+ export { textureDimensions, textureGather, textureGatherCompare, textureLoad, textureNumLayers, textureNumLevels, textureNumSamples, textureSample, textureSampleBaseClampToEdge, textureSampleBias, textureSampleCompare, textureSampleCompareLevel, textureSampleGrad, textureSampleLevel, textureStore, } from './texture.ts';
16
+ export { subgroupAdd, subgroupAll, subgroupAnd, subgroupAny, subgroupBallot, subgroupBroadcast, subgroupBroadcastFirst, subgroupElect, subgroupExclusiveAdd, subgroupExclusiveMul, subgroupInclusiveAdd, subgroupInclusiveMul, subgroupMax, subgroupMin, subgroupMul, subgroupOr, subgroupShuffle, subgroupShuffleDown, subgroupShuffleUp, subgroupShuffleXor, subgroupXor, quadBroadcast, quadSwapDiagonal, quadSwapX, quadSwapY, } from './subgroup.ts';
17
17
  export { extensionEnabled } from './extensions.ts';
18
18
  export { bitcastU32toF32, bitcastU32toI32, bitcastF32toU32, bitcast } from './bitcast.ts';
19
19
  export { range } from './range.ts';
package/std/index.js CHANGED
@@ -17,15 +17,15 @@ not, or, and,
17
17
  all, any,
18
18
  // other
19
19
  isCloseTo, select, } from "./boolean.js";
20
- export { atomicAdd, atomicAnd, atomicLoad, atomicMax, atomicMin, atomicOr, atomicStore, atomicSub, atomicXor,
20
+ export { atomicAdd, atomicAnd, atomicCompareExchangeWeak, atomicExchange, atomicLoad, atomicMax, atomicMin, atomicOr, atomicStore, atomicSub, atomicXor,
21
21
  // synchronization
22
- workgroupBarrier, storageBarrier, textureBarrier, } from "./atomic.js";
22
+ workgroupBarrier, storageBarrier, textureBarrier, workgroupUniformLoad, } from "./atomic.js";
23
23
  export { dpdx, dpdxCoarse, dpdxFine, dpdy, dpdyCoarse, dpdyFine, fwidth, fwidthCoarse, fwidthFine, } from "./derivative.js";
24
24
  export { arrayLength } from "./array.js";
25
25
  // oxfmt-ignore
26
- export { pack4x8unorm, pack2x16float, unpack4x8unorm, unpack2x16float, } from "./packing.js";
27
- export { textureDimensions, textureGather, textureLoad, textureSample, textureSampleBaseClampToEdge, textureSampleBias, textureSampleCompare, textureSampleCompareLevel, textureSampleGrad, textureSampleLevel, textureStore, } from "./texture.js";
28
- export { subgroupAdd, subgroupAll, subgroupAnd, subgroupAny, subgroupBallot, subgroupBroadcast, subgroupBroadcastFirst, subgroupElect, subgroupExclusiveAdd, subgroupExclusiveMul, subgroupInclusiveAdd, subgroupInclusiveMul, subgroupMax, subgroupMin, subgroupMul, subgroupOr, subgroupShuffle, subgroupShuffleDown, subgroupShuffleUp, subgroupShuffleXor, subgroupXor, } from "./subgroup.js";
26
+ export { pack2x16float, pack2x16snorm, pack2x16unorm, pack4x8snorm, pack4x8unorm, pack4xI8, pack4xI8Clamp, pack4xU8, pack4xU8Clamp, unpack2x16float, unpack2x16snorm, unpack2x16unorm, unpack4x8snorm, unpack4x8unorm, unpack4xI8, unpack4xU8, } from "./packing.js";
27
+ export { textureDimensions, textureGather, textureGatherCompare, textureLoad, textureNumLayers, textureNumLevels, textureNumSamples, textureSample, textureSampleBaseClampToEdge, textureSampleBias, textureSampleCompare, textureSampleCompareLevel, textureSampleGrad, textureSampleLevel, textureStore, } from "./texture.js";
28
+ export { subgroupAdd, subgroupAll, subgroupAnd, subgroupAny, subgroupBallot, subgroupBroadcast, subgroupBroadcastFirst, subgroupElect, subgroupExclusiveAdd, subgroupExclusiveMul, subgroupInclusiveAdd, subgroupInclusiveMul, subgroupMax, subgroupMin, subgroupMul, subgroupOr, subgroupShuffle, subgroupShuffleDown, subgroupShuffleUp, subgroupShuffleXor, subgroupXor, quadBroadcast, quadSwapDiagonal, quadSwapX, quadSwapY, } from "./subgroup.js";
29
29
  export { extensionEnabled } from "./extensions.js";
30
30
  export { bitcastU32toF32, bitcastU32toI32, bitcastF32toU32, bitcast } from "./bitcast.js";
31
31
  export { range } from "./range.js";
package/std/numeric.d.ts CHANGED
@@ -1,6 +1,10 @@
1
- import { type AnyFloat32VecInstance, type AnyFloatVecInstance, type AnyIntegerVecInstance, type AnyMatInstance, type AnyNumericVecInstance, type AnySignedVecInstance, type v2f, type v2h, type v2i, type v3f, type v3h, type v3i, type v4f, type v4h, type v4i, type Vec2f } from '../data/wgslTypes.ts';
1
+ import { type AnyFloat32VecInstance, type AnyFloatVecInstance, type AnyIntegerVecInstance, type AnyMatInstance, type AnyNumericVecInstance, type AnySignedVecInstance, type BaseData, type v2f, type v2h, type v2i, type v3f, type v3h, type v3i, type v4f, type v4h, type v4i, type Vec2f } from '../data/wgslTypes.ts';
2
2
  import type { Infer } from '../shared/repr.ts';
3
3
  type NumVec = AnyNumericVecInstance;
4
+ export declare const unifyRestrictedSignature: (restrict: BaseData[]) => (...args: BaseData[]) => {
5
+ argTypes: BaseData[];
6
+ returnType: BaseData;
7
+ };
4
8
  declare function cpuAbs(value: number): number;
5
9
  declare function cpuAbs<T extends NumVec | number>(value: T): T;
6
10
  export declare const abs: import("../types.ts").DualFn<typeof cpuAbs>;
package/std/numeric.js CHANGED
@@ -26,7 +26,7 @@ const variadicUnifySignature = (...args) => {
26
26
  returnType: uargs[0],
27
27
  };
28
28
  };
29
- const unifyRestrictedSignature = (restrict) => (...args) => {
29
+ export const unifyRestrictedSignature = (restrict) => (...args) => {
30
30
  const uargs = unify(args, restrict);
31
31
  if (!uargs) {
32
32
  throw new SignatureNotSupportedError(args, restrict);
@@ -946,9 +946,9 @@ function cpuIntdiv(lhs, rhs) {
946
946
  export const intdiv = dualImpl({
947
947
  name: 'intdiv',
948
948
  signature: (lhs, rhs) => {
949
- const unified = unify([lhs, rhs], [u32, i32]);
949
+ const unified = unify([lhs, rhs], [i32, u32]);
950
950
  if (!unified) {
951
- throw new SignatureNotSupportedError([lhs, rhs], [u32, i32, abstractInt]);
951
+ throw new SignatureNotSupportedError([lhs, rhs], [i32, u32, abstractInt]);
952
952
  }
953
953
  return { argTypes: unified, returnType: unified[0] };
954
954
  },
package/std/operators.js CHANGED
@@ -160,10 +160,15 @@ function cpuNeg(value) {
160
160
  }
161
161
  export const neg = dualImpl({
162
162
  name: 'neg',
163
- signature: (arg) => ({
164
- argTypes: [arg],
165
- returnType: arg,
166
- }),
163
+ signature: (arg) => {
164
+ if (getPrimitive(arg) === u32) {
165
+ throw new WgslTypeError(`Unsupported signature. Unary operator - cannot be applied to argument of kind '${arg.type}'.`);
166
+ }
167
+ return {
168
+ argTypes: [arg],
169
+ returnType: arg,
170
+ };
171
+ },
167
172
  normalImpl: cpuNeg,
168
173
  codegenImpl: (_ctx, [arg]) => stitch `-(${arg})`,
169
174
  sideEffects: false,
package/std/packing.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { v2f, v4f } from '../data/wgslTypes.ts';
1
+ import type { v2f, v4f, v4i, v4u } from '../data/wgslTypes.ts';
2
2
  /**
3
3
  * @privateRemarks
4
4
  * https://gpuweb.github.io/gpuweb/wgsl/#unpack2x16float-builtin
@@ -9,13 +9,73 @@ export declare const unpack2x16float: import("../types.ts").DualFn<(e: number) =
9
9
  * https://gpuweb.github.io/gpuweb/wgsl/#pack2x16float-builtin
10
10
  */
11
11
  export declare const pack2x16float: import("../types.ts").DualFn<(e: v2f) => number>;
12
+ /**
13
+ * @privateRemarks
14
+ * https://www.w3.org/TR/WGSL/#pack2x16snorm-builtin
15
+ */
16
+ export declare const pack2x16snorm: import("../types.ts").DualFn<(e: v2f) => number>;
17
+ /**
18
+ * @privateRemarks
19
+ * https://www.w3.org/TR/WGSL/#pack2x16unorm-builtin
20
+ */
21
+ export declare const pack2x16unorm: import("../types.ts").DualFn<(e: v2f) => number>;
12
22
  /**
13
23
  * @privateRemarks
14
24
  * https://gpuweb.github.io/gpuweb/wgsl/#unpack4x8unorm-builtin
15
25
  */
16
26
  export declare const unpack4x8unorm: import("../types.ts").DualFn<(e: number) => v4f>;
27
+ /**
28
+ * @privateRemarks
29
+ * https://www.w3.org/TR/WGSL/#unpack4x8snorm-builtin
30
+ */
31
+ export declare const unpack4x8snorm: import("../types.ts").DualFn<(e: number) => v4f>;
32
+ /**
33
+ * @privateRemarks
34
+ * https://www.w3.org/TR/WGSL/#unpack4xi8-builtin
35
+ */
36
+ export declare const unpack4xI8: import("../types.ts").DualFn<(e: number) => v4i>;
37
+ /**
38
+ * @privateRemarks
39
+ * https://www.w3.org/TR/WGSL/#unpack4xu8-builtin
40
+ */
41
+ export declare const unpack4xU8: import("../types.ts").DualFn<(e: number) => v4u>;
42
+ /**
43
+ * @privateRemarks
44
+ * https://www.w3.org/TR/WGSL/#unpack2x16snorm-builtin
45
+ */
46
+ export declare const unpack2x16snorm: import("../types.ts").DualFn<(e: number) => v2f>;
47
+ /**
48
+ * @privateRemarks
49
+ * https://www.w3.org/TR/WGSL/#unpack2x16unorm-builtin
50
+ */
51
+ export declare const unpack2x16unorm: import("../types.ts").DualFn<(e: number) => v2f>;
17
52
  /**
18
53
  * @privateRemarks
19
54
  * https://gpuweb.github.io/gpuweb/wgsl/#pack4x8unorm-builtin
20
55
  */
21
56
  export declare const pack4x8unorm: import("../types.ts").DualFn<(e: v4f) => number>;
57
+ /**
58
+ * @privateRemarks
59
+ * https://www.w3.org/TR/WGSL/#pack4x8snorm-builtin
60
+ */
61
+ export declare const pack4x8snorm: import("../types.ts").DualFn<(e: v4f) => number>;
62
+ /**
63
+ * @privateRemarks
64
+ * https://www.w3.org/TR/WGSL/#pack4xi8-builtin
65
+ */
66
+ export declare const pack4xI8: import("../types.ts").DualFn<(e: v4i) => number>;
67
+ /**
68
+ * @privateRemarks
69
+ * https://www.w3.org/TR/WGSL/#pack4xu8-builtin
70
+ */
71
+ export declare const pack4xU8: import("../types.ts").DualFn<(e: v4u) => number>;
72
+ /**
73
+ * @privateRemarks
74
+ * https://www.w3.org/TR/WGSL/#pack4xi8clamp-builtin
75
+ */
76
+ export declare const pack4xI8Clamp: import("../types.ts").DualFn<(e: v4i) => number>;
77
+ /**
78
+ * @privateRemarks
79
+ * https://www.w3.org/TR/WGSL/#pack4xu8clamp-builtin
80
+ */
81
+ export declare const pack4xU8Clamp: import("../types.ts").DualFn<(e: v4u) => number>;
package/std/packing.js CHANGED
@@ -2,8 +2,25 @@ import * as TB from 'typed-binary';
2
2
  import { dualImpl } from "../core/function/dualImpl.js";
3
3
  import { stitch } from "../core/resolve/stitch.js";
4
4
  import { u32 } from "../data/numeric.js";
5
- import { vec2f, vec4f } from "../data/vector.js";
5
+ import { clampScalar } from "../data/numberOps.js";
6
+ import { vec2f, vec4f, vec4i, vec4u } from "../data/vector.js";
6
7
  import { readFloat16, writeFloat16 } from "../data/float16Conversion.js";
8
+ const littleEndian = { endianness: 'little' };
9
+ const packedBuffer = new ArrayBuffer(4);
10
+ const packedWriter = new TB.BufferWriter(packedBuffer, littleEndian);
11
+ const packedReader = new TB.BufferReader(packedBuffer, littleEndian);
12
+ function readPacked(value) {
13
+ packedWriter.seekTo(0);
14
+ packedWriter.writeUint32(value);
15
+ packedReader.seekTo(0);
16
+ return packedReader;
17
+ }
18
+ function writePacked(write) {
19
+ packedWriter.seekTo(0);
20
+ write(packedWriter);
21
+ packedReader.seekTo(0);
22
+ return u32(packedReader.readUint32());
23
+ }
7
24
  /**
8
25
  * @privateRemarks
9
26
  * https://gpuweb.github.io/gpuweb/wgsl/#unpack2x16float-builtin
@@ -11,10 +28,7 @@ import { readFloat16, writeFloat16 } from "../data/float16Conversion.js";
11
28
  export const unpack2x16float = dualImpl({
12
29
  name: 'unpack2x16float',
13
30
  normalImpl: (e) => {
14
- const buffer = new ArrayBuffer(4);
15
- const writer = new TB.BufferWriter(buffer);
16
- writer.writeUint32(e);
17
- const reader = new TB.BufferReader(buffer);
31
+ const reader = readPacked(e);
18
32
  return vec2f(readFloat16(reader), readFloat16(reader));
19
33
  },
20
34
  signature: { argTypes: [u32], returnType: vec2f },
@@ -27,18 +41,50 @@ export const unpack2x16float = dualImpl({
27
41
  */
28
42
  export const pack2x16float = dualImpl({
29
43
  name: 'pack2x16float',
30
- normalImpl: (e) => {
31
- const buffer = new ArrayBuffer(4);
32
- const writer = new TB.BufferWriter(buffer);
44
+ normalImpl: (e) => writePacked((writer) => {
33
45
  writeFloat16(writer, e.x);
34
46
  writeFloat16(writer, e.y);
35
- const reader = new TB.BufferReader(buffer);
36
- return u32(reader.readUint32());
37
- },
47
+ }),
38
48
  signature: { argTypes: [vec2f], returnType: u32 },
39
49
  codegenImpl: (_ctx, [e]) => stitch `pack2x16float(${e})`,
40
50
  sideEffects: false,
41
51
  });
52
+ /**
53
+ * @privateRemarks
54
+ * https://www.w3.org/TR/WGSL/#pack2x16snorm-builtin
55
+ */
56
+ export const pack2x16snorm = dualImpl({
57
+ name: 'pack2x16snorm',
58
+ normalImpl: (e) => {
59
+ const x = Math.floor(0.5 + 32767 * clampScalar(e.x, -1, 1));
60
+ const y = Math.floor(0.5 + 32767 * clampScalar(e.y, -1, 1));
61
+ return writePacked((writer) => {
62
+ writer.writeInt16(x);
63
+ writer.writeInt16(y);
64
+ });
65
+ },
66
+ signature: { argTypes: [vec2f], returnType: u32 },
67
+ codegenImpl: (_ctx, [e]) => stitch `pack2x16snorm(${e})`,
68
+ sideEffects: false,
69
+ });
70
+ /**
71
+ * @privateRemarks
72
+ * https://www.w3.org/TR/WGSL/#pack2x16unorm-builtin
73
+ */
74
+ export const pack2x16unorm = dualImpl({
75
+ name: 'pack2x16unorm',
76
+ normalImpl: (e) => {
77
+ const x = Math.floor(0.5 + 65535 * clampScalar(e.x, 0, 1));
78
+ const y = Math.floor(0.5 + 65535 * clampScalar(e.y, 0, 1));
79
+ return writePacked((writer) => {
80
+ writer.writeUint16(x);
81
+ writer.writeUint16(y);
82
+ });
83
+ },
84
+ signature: { argTypes: [vec2f], returnType: u32 },
85
+ codegenImpl: (_ctx, [e]) => stitch `pack2x16unorm(${e})`,
86
+ sideEffects: false,
87
+ });
42
88
  /**
43
89
  * @privateRemarks
44
90
  * https://gpuweb.github.io/gpuweb/wgsl/#unpack4x8unorm-builtin
@@ -46,33 +92,176 @@ export const pack2x16float = dualImpl({
46
92
  export const unpack4x8unorm = dualImpl({
47
93
  name: 'unpack4x8unorm',
48
94
  normalImpl: (e) => {
49
- const buffer = new ArrayBuffer(4);
50
- const writer = new TB.BufferWriter(buffer);
51
- writer.writeUint32(e);
52
- const reader = new TB.BufferReader(buffer);
95
+ const reader = readPacked(e);
53
96
  return vec4f(reader.readUint8() / 255, reader.readUint8() / 255, reader.readUint8() / 255, reader.readUint8() / 255);
54
97
  },
55
98
  signature: { argTypes: [u32], returnType: vec4f },
56
99
  codegenImpl: (_ctx, [e]) => stitch `unpack4x8unorm(${e})`,
57
100
  sideEffects: false,
58
101
  });
102
+ /**
103
+ * @privateRemarks
104
+ * https://www.w3.org/TR/WGSL/#unpack4x8snorm-builtin
105
+ */
106
+ export const unpack4x8snorm = dualImpl({
107
+ name: 'unpack4x8snorm',
108
+ normalImpl: (e) => {
109
+ const reader = readPacked(e);
110
+ return vec4f(Math.max(reader.readInt8() / 127, -1), Math.max(reader.readInt8() / 127, -1), Math.max(reader.readInt8() / 127, -1), Math.max(reader.readInt8() / 127, -1));
111
+ },
112
+ signature: { argTypes: [u32], returnType: vec4f },
113
+ codegenImpl: (_ctx, [e]) => stitch `unpack4x8snorm(${e})`,
114
+ sideEffects: false,
115
+ });
116
+ /**
117
+ * @privateRemarks
118
+ * https://www.w3.org/TR/WGSL/#unpack4xi8-builtin
119
+ */
120
+ export const unpack4xI8 = dualImpl({
121
+ name: 'unpack4xI8',
122
+ normalImpl: (e) => {
123
+ const reader = readPacked(e);
124
+ return vec4i(reader.readInt8(), reader.readInt8(), reader.readInt8(), reader.readInt8());
125
+ },
126
+ signature: { argTypes: [u32], returnType: vec4i },
127
+ codegenImpl: (_ctx, [e]) => stitch `unpack4xI8(${e})`,
128
+ sideEffects: false,
129
+ });
130
+ /**
131
+ * @privateRemarks
132
+ * https://www.w3.org/TR/WGSL/#unpack4xu8-builtin
133
+ */
134
+ export const unpack4xU8 = dualImpl({
135
+ name: 'unpack4xU8',
136
+ normalImpl: (e) => {
137
+ const reader = readPacked(e);
138
+ return vec4u(reader.readUint8(), reader.readUint8(), reader.readUint8(), reader.readUint8());
139
+ },
140
+ signature: { argTypes: [u32], returnType: vec4u },
141
+ codegenImpl: (_ctx, [e]) => stitch `unpack4xU8(${e})`,
142
+ sideEffects: false,
143
+ });
144
+ /**
145
+ * @privateRemarks
146
+ * https://www.w3.org/TR/WGSL/#unpack2x16snorm-builtin
147
+ */
148
+ export const unpack2x16snorm = dualImpl({
149
+ name: 'unpack2x16snorm',
150
+ normalImpl: (e) => {
151
+ const reader = readPacked(e);
152
+ return vec2f(Math.max(reader.readInt16() / 32767, -1), Math.max(reader.readInt16() / 32767, -1));
153
+ },
154
+ signature: { argTypes: [u32], returnType: vec2f },
155
+ codegenImpl: (_ctx, [e]) => stitch `unpack2x16snorm(${e})`,
156
+ sideEffects: false,
157
+ });
158
+ /**
159
+ * @privateRemarks
160
+ * https://www.w3.org/TR/WGSL/#unpack2x16unorm-builtin
161
+ */
162
+ export const unpack2x16unorm = dualImpl({
163
+ name: 'unpack2x16unorm',
164
+ normalImpl: (e) => {
165
+ const reader = readPacked(e);
166
+ return vec2f(reader.readUint16() / 65535, reader.readUint16() / 65535);
167
+ },
168
+ signature: { argTypes: [u32], returnType: vec2f },
169
+ codegenImpl: (_ctx, [e]) => stitch `unpack2x16unorm(${e})`,
170
+ sideEffects: false,
171
+ });
59
172
  /**
60
173
  * @privateRemarks
61
174
  * https://gpuweb.github.io/gpuweb/wgsl/#pack4x8unorm-builtin
62
175
  */
63
176
  export const pack4x8unorm = dualImpl({
64
177
  name: 'pack4x8unorm',
65
- normalImpl: (e) => {
66
- const buffer = new ArrayBuffer(4);
67
- const writer = new TB.BufferWriter(buffer);
68
- writer.writeUint8(e.x * 255);
69
- writer.writeUint8(e.y * 255);
70
- writer.writeUint8(e.z * 255);
71
- writer.writeUint8(e.w * 255);
72
- const reader = new TB.BufferReader(buffer);
73
- return u32(reader.readUint32());
74
- },
178
+ normalImpl: (e) => writePacked((writer) => {
179
+ writer.writeUint8(Math.floor(0.5 + 255 * clampScalar(e.x, 0, 1)));
180
+ writer.writeUint8(Math.floor(0.5 + 255 * clampScalar(e.y, 0, 1)));
181
+ writer.writeUint8(Math.floor(0.5 + 255 * clampScalar(e.z, 0, 1)));
182
+ writer.writeUint8(Math.floor(0.5 + 255 * clampScalar(e.w, 0, 1)));
183
+ }),
75
184
  signature: { argTypes: [vec4f], returnType: u32 },
76
185
  codegenImpl: (_ctx, [e]) => stitch `pack4x8unorm(${e})`,
77
186
  sideEffects: false,
78
187
  });
188
+ /**
189
+ * @privateRemarks
190
+ * https://www.w3.org/TR/WGSL/#pack4x8snorm-builtin
191
+ */
192
+ export const pack4x8snorm = dualImpl({
193
+ name: 'pack4x8snorm',
194
+ normalImpl: (e) => writePacked((writer) => {
195
+ writer.writeInt8(Math.floor(0.5 + 127 * clampScalar(e.x, -1, 1)));
196
+ writer.writeInt8(Math.floor(0.5 + 127 * clampScalar(e.y, -1, 1)));
197
+ writer.writeInt8(Math.floor(0.5 + 127 * clampScalar(e.z, -1, 1)));
198
+ writer.writeInt8(Math.floor(0.5 + 127 * clampScalar(e.w, -1, 1)));
199
+ }),
200
+ signature: { argTypes: [vec4f], returnType: u32 },
201
+ codegenImpl: (_ctx, [e]) => stitch `pack4x8snorm(${e})`,
202
+ sideEffects: false,
203
+ });
204
+ /**
205
+ * @privateRemarks
206
+ * https://www.w3.org/TR/WGSL/#pack4xi8-builtin
207
+ */
208
+ export const pack4xI8 = dualImpl({
209
+ name: 'pack4xI8',
210
+ normalImpl: (e) => writePacked((writer) => {
211
+ writer.writeInt8(e.x);
212
+ writer.writeInt8(e.y);
213
+ writer.writeInt8(e.z);
214
+ writer.writeInt8(e.w);
215
+ }),
216
+ signature: { argTypes: [vec4i], returnType: u32 },
217
+ codegenImpl: (_ctx, [e]) => stitch `pack4xI8(${e})`,
218
+ sideEffects: false,
219
+ });
220
+ /**
221
+ * @privateRemarks
222
+ * https://www.w3.org/TR/WGSL/#pack4xu8-builtin
223
+ */
224
+ export const pack4xU8 = dualImpl({
225
+ name: 'pack4xU8',
226
+ normalImpl: (e) => writePacked((writer) => {
227
+ writer.writeUint8(e.x);
228
+ writer.writeUint8(e.y);
229
+ writer.writeUint8(e.z);
230
+ writer.writeUint8(e.w);
231
+ }),
232
+ signature: { argTypes: [vec4u], returnType: u32 },
233
+ codegenImpl: (_ctx, [e]) => stitch `pack4xU8(${e})`,
234
+ sideEffects: false,
235
+ });
236
+ /**
237
+ * @privateRemarks
238
+ * https://www.w3.org/TR/WGSL/#pack4xi8clamp-builtin
239
+ */
240
+ export const pack4xI8Clamp = dualImpl({
241
+ name: 'pack4xI8Clamp',
242
+ normalImpl: (e) => writePacked((writer) => {
243
+ writer.writeInt8(clampScalar(e.x, -128, 127));
244
+ writer.writeInt8(clampScalar(e.y, -128, 127));
245
+ writer.writeInt8(clampScalar(e.z, -128, 127));
246
+ writer.writeInt8(clampScalar(e.w, -128, 127));
247
+ }),
248
+ signature: { argTypes: [vec4i], returnType: u32 },
249
+ codegenImpl: (_ctx, [e]) => stitch `pack4xI8Clamp(${e})`,
250
+ sideEffects: false,
251
+ });
252
+ /**
253
+ * @privateRemarks
254
+ * https://www.w3.org/TR/WGSL/#pack4xu8clamp-builtin
255
+ */
256
+ export const pack4xU8Clamp = dualImpl({
257
+ name: 'pack4xU8Clamp',
258
+ normalImpl: (e) => writePacked((writer) => {
259
+ writer.writeUint8(clampScalar(e.x, 0, 255));
260
+ writer.writeUint8(clampScalar(e.y, 0, 255));
261
+ writer.writeUint8(clampScalar(e.z, 0, 255));
262
+ writer.writeUint8(clampScalar(e.w, 0, 255));
263
+ }),
264
+ signature: { argTypes: [vec4u], returnType: u32 },
265
+ codegenImpl: (_ctx, [e]) => stitch `pack4xU8Clamp(${e})`,
266
+ sideEffects: false,
267
+ });
package/std/subgroup.d.ts CHANGED
@@ -40,4 +40,8 @@ export declare const subgroupShuffleDown: import("../types.ts").DualFn<IdentityN
40
40
  export declare const subgroupShuffleUp: import("../types.ts").DualFn<IdentityNumOrVecWithDelta>;
41
41
  export declare const subgroupShuffleXor: import("../types.ts").DualFn<IdentityNumOrVecWithMask>;
42
42
  export declare const subgroupXor: import("../types.ts").DualFn<IdentityIntNumOrVec>;
43
+ export declare const quadBroadcast: import("../types.ts").DualFn<IdentityNumOrVecWithIdx>;
44
+ export declare const quadSwapDiagonal: import("../types.ts").DualFn<IdentityNumOrVec>;
45
+ export declare const quadSwapX: import("../types.ts").DualFn<IdentityNumOrVec>;
46
+ export declare const quadSwapY: import("../types.ts").DualFn<IdentityNumOrVec>;
43
47
  export {};