typegpu 0.10.1 → 0.10.2

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 (263) hide show
  1. package/{chunk-BYypO7fO.js → _virtual/_rolldown/runtime.js} +1 -1
  2. package/builtin.d.ts +50 -0
  3. package/builtin.js +37 -0
  4. package/common/fullScreenTriangle.d.ts +26 -0
  5. package/common/fullScreenTriangle.js +36 -0
  6. package/common/index.d.ts +2 -3
  7. package/common/index.js +3 -4
  8. package/core/buffer/buffer.d.ts +74 -0
  9. package/core/buffer/buffer.js +197 -0
  10. package/core/buffer/bufferShorthand.d.ts +48 -0
  11. package/core/buffer/bufferShorthand.js +49 -0
  12. package/core/buffer/bufferUsage.d.ts +45 -0
  13. package/core/buffer/bufferUsage.js +163 -0
  14. package/core/constant/tgpuConstant.d.ts +28 -0
  15. package/core/constant/tgpuConstant.js +67 -0
  16. package/core/declare/tgpuDeclare.d.ts +18 -0
  17. package/core/declare/tgpuDeclare.js +40 -0
  18. package/core/function/autoIO.d.ts +37 -0
  19. package/core/function/autoIO.js +87 -0
  20. package/core/function/comptime.d.ts +39 -0
  21. package/core/function/comptime.js +51 -0
  22. package/core/function/createCallableSchema.js +42 -0
  23. package/core/function/dualImpl.js +54 -0
  24. package/core/function/extractArgs.js +204 -0
  25. package/core/function/fnCore.js +79 -0
  26. package/core/function/fnTypes.d.ts +34 -0
  27. package/core/function/ioSchema.d.ts +10 -0
  28. package/core/function/ioSchema.js +30 -0
  29. package/core/function/shelllessImpl.d.ts +28 -0
  30. package/core/function/shelllessImpl.js +23 -0
  31. package/core/function/templateUtils.js +13 -0
  32. package/core/function/tgpuComputeFn.d.ts +49 -0
  33. package/core/function/tgpuComputeFn.js +62 -0
  34. package/core/function/tgpuFn.d.ts +52 -0
  35. package/core/function/tgpuFn.js +170 -0
  36. package/core/function/tgpuFragmentFn.d.ts +68 -0
  37. package/core/function/tgpuFragmentFn.js +68 -0
  38. package/core/function/tgpuVertexFn.d.ts +55 -0
  39. package/core/function/tgpuVertexFn.js +65 -0
  40. package/core/pipeline/applyPipelineState.js +37 -0
  41. package/core/pipeline/computePipeline.d.ts +58 -0
  42. package/core/pipeline/computePipeline.js +226 -0
  43. package/core/pipeline/connectAttachmentToShader.js +26 -0
  44. package/core/pipeline/connectTargetsToShader.js +29 -0
  45. package/core/pipeline/limitsOverflow.js +13 -0
  46. package/core/pipeline/renderPipeline.d.ts +266 -0
  47. package/core/pipeline/renderPipeline.js +471 -0
  48. package/core/pipeline/timeable.d.ts +23 -0
  49. package/core/pipeline/timeable.js +61 -0
  50. package/core/pipeline/typeGuards.js +29 -0
  51. package/core/querySet/querySet.d.ts +22 -0
  52. package/core/querySet/querySet.js +103 -0
  53. package/core/rawCodeSnippet/tgpuRawCodeSnippet.d.ts +59 -0
  54. package/core/rawCodeSnippet/tgpuRawCodeSnippet.js +96 -0
  55. package/core/resolve/externals.d.ts +10 -0
  56. package/core/resolve/externals.js +58 -0
  57. package/core/resolve/namespace.d.ts +38 -0
  58. package/core/resolve/namespace.js +41 -0
  59. package/core/resolve/resolveData.js +146 -0
  60. package/core/resolve/stitch.js +25 -0
  61. package/core/resolve/tgpuResolve.d.ts +151 -0
  62. package/core/resolve/tgpuResolve.js +68 -0
  63. package/core/root/configurableImpl.js +18 -0
  64. package/core/root/init.d.ts +69 -0
  65. package/core/root/init.js +457 -0
  66. package/core/root/rootTypes.d.ts +622 -0
  67. package/core/sampler/sampler.d.ts +35 -0
  68. package/core/sampler/sampler.js +116 -0
  69. package/core/simulate/tgpuSimulate.d.ts +36 -0
  70. package/core/simulate/tgpuSimulate.js +76 -0
  71. package/core/slot/accessor.d.ts +13 -0
  72. package/core/slot/accessor.js +97 -0
  73. package/core/slot/internalSlots.js +7 -0
  74. package/core/slot/lazy.d.ts +6 -0
  75. package/core/slot/lazy.js +42 -0
  76. package/core/slot/slot.d.ts +6 -0
  77. package/core/slot/slot.js +40 -0
  78. package/core/slot/slotTypes.d.ts +92 -0
  79. package/core/slot/slotTypes.js +21 -0
  80. package/core/texture/externalTexture.d.ts +12 -0
  81. package/core/texture/externalTexture.js +48 -0
  82. package/core/texture/texture.d.ts +118 -0
  83. package/core/texture/texture.js +312 -0
  84. package/core/texture/textureFormats.d.ts +29 -0
  85. package/core/texture/textureFormats.js +99 -0
  86. package/core/texture/textureProps.d.ts +11 -0
  87. package/core/texture/textureUtils.js +224 -0
  88. package/core/texture/usageExtension.d.ts +21 -0
  89. package/core/texture/usageExtension.js +21 -0
  90. package/core/unroll/tgpuUnroll.d.ts +13 -0
  91. package/core/unroll/tgpuUnroll.js +36 -0
  92. package/core/valueProxyUtils.js +44 -0
  93. package/core/variable/tgpuVariable.d.ts +38 -0
  94. package/core/variable/tgpuVariable.js +101 -0
  95. package/core/vertexLayout/connectAttributesToShader.js +59 -0
  96. package/core/vertexLayout/vertexAttribute.d.ts +29 -0
  97. package/core/vertexLayout/vertexLayout.d.ts +19 -0
  98. package/core/vertexLayout/vertexLayout.js +103 -0
  99. package/data/alignIO.js +15 -0
  100. package/data/alignmentOf.d.ts +10 -0
  101. package/data/alignmentOf.js +88 -0
  102. package/data/array.d.ts +28 -0
  103. package/data/array.js +48 -0
  104. package/data/atomic.d.ts +15 -0
  105. package/data/atomic.js +25 -0
  106. package/data/attributes.d.ts +121 -0
  107. package/data/attributes.js +145 -0
  108. package/data/autoStruct.d.ts +3 -0
  109. package/data/autoStruct.js +83 -0
  110. package/data/compiledIO.js +231 -0
  111. package/data/dataIO.js +549 -0
  112. package/data/dataTypes.d.ts +115 -0
  113. package/data/dataTypes.js +97 -0
  114. package/data/deepEqual.d.ts +25 -0
  115. package/data/deepEqual.js +58 -0
  116. package/data/disarray.d.ts +34 -0
  117. package/data/disarray.js +52 -0
  118. package/data/getLongestContiguousPrefix.d.ts +10 -0
  119. package/data/getLongestContiguousPrefix.js +15 -0
  120. package/data/index.d.ts +26 -4
  121. package/data/index.js +27 -7
  122. package/data/instanceToSchema.d.ts +33 -0
  123. package/data/isContiguous.d.ts +10 -0
  124. package/data/isContiguous.js +15 -0
  125. package/data/matrix.d.ts +126 -0
  126. package/data/matrix.js +517 -0
  127. package/data/numberOps.js +24 -0
  128. package/data/numeric.d.ts +81 -0
  129. package/data/numeric.js +234 -0
  130. package/data/offsetUtils.d.ts +33 -0
  131. package/data/offsetUtils.js +167 -0
  132. package/data/offsets.js +36 -0
  133. package/data/partialIO.js +68 -0
  134. package/data/ptr.d.ts +12 -0
  135. package/data/ptr.js +46 -0
  136. package/data/ref.d.ts +37 -0
  137. package/data/ref.js +96 -0
  138. package/data/sampler.d.ts +107 -0
  139. package/data/sampler.js +26 -0
  140. package/data/schemaCallWrapper.js +32 -0
  141. package/data/schemaMemoryLayout.js +200 -0
  142. package/data/sizeOf.d.ts +10 -0
  143. package/data/sizeOf.js +15 -0
  144. package/data/snippet.d.ts +26 -0
  145. package/data/snippet.js +61 -0
  146. package/data/struct.d.ts +17 -0
  147. package/data/struct.js +46 -0
  148. package/data/texture.d.ts +292 -0
  149. package/{texture-BagDrrks.js → data/texture.js} +6 -3
  150. package/data/unstruct.d.ts +24 -0
  151. package/data/unstruct.js +43 -0
  152. package/data/vector.d.ts +191 -0
  153. package/data/vector.js +247 -0
  154. package/data/vectorImpl.js +516 -0
  155. package/data/vectorOps.js +664 -0
  156. package/data/vertexFormatData.d.ts +190 -0
  157. package/data/vertexFormatData.js +110 -0
  158. package/data/wgslTypes.d.ts +896 -0
  159. package/data/wgslTypes.js +215 -0
  160. package/errors.d.ts +44 -0
  161. package/errors.js +128 -0
  162. package/execMode.js +51 -0
  163. package/extension.d.ts +11 -0
  164. package/extension.js +18 -0
  165. package/getGPUValue.js +9 -0
  166. package/index.d.ts +40 -243
  167. package/index.js +19 -6318
  168. package/indexNamedExports.d.ts +38 -0
  169. package/mathUtils.js +13 -0
  170. package/memo.js +22 -0
  171. package/nameRegistry.d.ts +30 -0
  172. package/nameRegistry.js +449 -0
  173. package/package.js +5 -0
  174. package/package.json +23 -23
  175. package/resolutionCtx.d.ts +29 -0
  176. package/resolutionCtx.js +546 -0
  177. package/shared/env.js +13 -0
  178. package/shared/generators.js +14 -0
  179. package/shared/meta.d.ts +39 -0
  180. package/shared/meta.js +63 -0
  181. package/shared/repr.d.ts +108 -0
  182. package/shared/stringify.js +22 -0
  183. package/shared/symbols.d.ts +61 -0
  184. package/shared/symbols.js +71 -0
  185. package/shared/utilityTypes.d.ts +29 -0
  186. package/shared/utilityTypes.js +7 -0
  187. package/shared/vertexFormat.d.ts +70 -0
  188. package/shared/vertexFormat.js +64 -0
  189. package/std/array.d.ts +7 -0
  190. package/std/array.js +27 -0
  191. package/std/atomic.d.ts +19 -0
  192. package/std/atomic.js +113 -0
  193. package/std/bitcast.d.ts +10 -0
  194. package/std/bitcast.js +43 -0
  195. package/std/boolean.d.ts +127 -0
  196. package/std/boolean.js +274 -0
  197. package/std/derivative.d.ts +16 -0
  198. package/std/derivative.js +89 -0
  199. package/std/discard.d.ts +6 -0
  200. package/std/discard.js +16 -0
  201. package/std/extensions.d.ts +8 -0
  202. package/std/extensions.js +14 -0
  203. package/std/index.d.ts +15 -3
  204. package/std/index.js +16 -5
  205. package/std/matrix.d.ts +41 -0
  206. package/std/matrix.js +87 -0
  207. package/std/numeric.d.ts +254 -0
  208. package/std/numeric.js +847 -0
  209. package/std/operators.d.ts +48 -0
  210. package/std/operators.js +153 -0
  211. package/std/packing.d.ts +26 -0
  212. package/std/packing.js +86 -0
  213. package/std/subgroup.d.ts +47 -0
  214. package/std/subgroup.js +220 -0
  215. package/std/texture.d.ts +108 -0
  216. package/std/texture.js +197 -0
  217. package/tgpu.js +44 -0
  218. package/tgpuBindGroupLayout.d.ts +161 -0
  219. package/tgpuBindGroupLayout.js +271 -0
  220. package/tgpuUnstable.d.ts +48 -0
  221. package/tgpuUnstable.js +66 -0
  222. package/tgsl/accessIndex.js +45 -0
  223. package/tgsl/accessProp.js +113 -0
  224. package/tgsl/consoleLog/deserializers.js +117 -0
  225. package/tgsl/consoleLog/logGenerator.js +86 -0
  226. package/tgsl/consoleLog/serializers.js +225 -0
  227. package/tgsl/consoleLog/types.d.ts +54 -0
  228. package/tgsl/consoleLog/types.js +12 -0
  229. package/tgsl/conversion.js +200 -0
  230. package/tgsl/forOfUtils.js +45 -0
  231. package/tgsl/generationHelpers.d.ts +37 -0
  232. package/tgsl/generationHelpers.js +67 -0
  233. package/tgsl/math.js +45 -0
  234. package/tgsl/shaderGenerator.d.ts +18 -0
  235. package/tgsl/shellless.d.ts +11 -0
  236. package/tgsl/shellless.js +53 -0
  237. package/tgsl/wgslGenerator.js +585 -0
  238. package/types.d.ts +255 -0
  239. package/types.js +43 -0
  240. package/unwrapper.d.ts +27 -0
  241. package/wgslExtensions.d.ts +5 -0
  242. package/wgslExtensions.js +18 -0
  243. package/builtin-DdtWpk2t.js +0 -818
  244. package/builtin-DdtWpk2t.js.map +0 -1
  245. package/common/index.d.ts.map +0 -1
  246. package/common/index.js.map +0 -1
  247. package/data/index.d.ts.map +0 -1
  248. package/data/index.js.map +0 -1
  249. package/deepEqual-DQxK4vdp.js +0 -413
  250. package/deepEqual-DQxK4vdp.js.map +0 -1
  251. package/extensions-DIVuAfBM.js +0 -2032
  252. package/extensions-DIVuAfBM.js.map +0 -1
  253. package/fullScreenTriangle-CfFyQd_0.js +0 -543
  254. package/fullScreenTriangle-CfFyQd_0.js.map +0 -1
  255. package/index.d.ts.map +0 -1
  256. package/index.js.map +0 -1
  257. package/indexNamedExports-oL6tyaJ9.d.ts +0 -5697
  258. package/indexNamedExports-oL6tyaJ9.d.ts.map +0 -1
  259. package/operators-d-PMVTo7.js +0 -4158
  260. package/operators-d-PMVTo7.js.map +0 -1
  261. package/std/index.d.ts.map +0 -1
  262. package/std/index.js.map +0 -1
  263. package/texture-BagDrrks.js.map +0 -1
@@ -0,0 +1,48 @@
1
+ import { DualFn } from "../types.js";
2
+ import { AnyMatInstance, AnyNumericVecInstance, mBaseForVec, vBaseForMat } from "../data/wgslTypes.js";
3
+
4
+ //#region src/std/operators.d.ts
5
+ type NumVec = AnyNumericVecInstance;
6
+ type Mat = AnyMatInstance;
7
+ declare function cpuAdd(lhs: number, rhs: number): number;
8
+ declare function cpuAdd<T extends NumVec>(lhs: number, rhs: T): T;
9
+ declare function cpuAdd<T extends NumVec>(lhs: T, rhs: number): T;
10
+ declare function cpuAdd<T extends NumVec | Mat>(lhs: T, rhs: T): T;
11
+ declare function cpuAdd<Lhs extends number | NumVec | Mat, Rhs extends (Lhs extends number ? number | NumVec : Lhs extends NumVec ? number | Lhs : Lhs extends Mat ? Lhs : never)>(lhs: Lhs, rhs: Rhs): Lhs | Rhs;
12
+ declare const add: DualFn<typeof cpuAdd>;
13
+ declare function cpuSub(lhs: number, rhs: number): number;
14
+ declare function cpuSub<T extends NumVec>(lhs: number, rhs: T): T;
15
+ declare function cpuSub<T extends NumVec>(lhs: T, rhs: number): T;
16
+ declare function cpuSub<T extends NumVec | Mat>(lhs: T, rhs: T): T;
17
+ declare function cpuSub<Lhs extends number | NumVec | Mat, Rhs extends (Lhs extends number ? number | NumVec : Lhs extends NumVec ? number | Lhs : Lhs extends Mat ? Lhs : never)>(lhs: Lhs, rhs: Rhs): Lhs | Rhs;
18
+ declare const sub: DualFn<typeof cpuSub>;
19
+ declare function cpuMul(lhs: number, rhs: number): number;
20
+ declare function cpuMul<MV extends NumVec | Mat>(lhs: number, rhs: MV): MV;
21
+ declare function cpuMul<MV extends NumVec | Mat>(lhs: MV, rhs: number): MV;
22
+ declare function cpuMul<V extends NumVec>(lhs: V, rhs: V): V;
23
+ declare function cpuMul<M extends Mat, V extends vBaseForMat<M>>(lhs: V, rhs: M): V;
24
+ declare function cpuMul<M extends Mat, V extends vBaseForMat<M>>(lhs: M, rhs: V): V;
25
+ declare function cpuMul<M extends Mat>(lhs: M, rhs: M): M;
26
+ declare function cpuMul<Lhs extends number | NumVec | Mat, Rhs extends (Lhs extends number ? number | NumVec | Mat : Lhs extends NumVec ? number | Lhs | mBaseForVec<Lhs> : Lhs extends Mat ? number | vBaseForMat<Lhs> | Lhs : never)>(lhs: Lhs, rhs: Rhs): Lhs | Rhs;
27
+ declare const mul: DualFn<typeof cpuMul>;
28
+ declare function cpuDiv(lhs: number, rhs: number): number;
29
+ declare function cpuDiv<T extends NumVec>(lhs: T, rhs: T): T;
30
+ declare function cpuDiv<T extends NumVec>(lhs: number, rhs: T): T;
31
+ declare function cpuDiv<T extends NumVec>(lhs: T, rhs: number): T;
32
+ declare const div: DualFn<typeof cpuDiv>;
33
+ type ModOverload = {
34
+ (a: number, b: number): number;
35
+ <T extends NumVec>(a: T, b: T): T;
36
+ <T extends NumVec>(a: number, b: T): T;
37
+ <T extends NumVec>(a: T, b: number): T;
38
+ };
39
+ /**
40
+ * @privateRemarks
41
+ * Both JS and WGSL implementations use truncated definition of modulo
42
+ */
43
+ declare const mod: DualFn<ModOverload>;
44
+ declare function cpuNeg(value: number): number;
45
+ declare function cpuNeg<T extends NumVec>(value: T): T;
46
+ declare const neg: DualFn<typeof cpuNeg>;
47
+ //#endregion
48
+ export { add, div, mod, mul, neg, sub };
@@ -0,0 +1,153 @@
1
+ import { isFloat32VecInstance, isMat, isMatInstance, isVec, isVecInstance } from "../data/wgslTypes.js";
2
+ import { SignatureNotSupportedError } from "../errors.js";
3
+ import { stitch } from "../core/resolve/stitch.js";
4
+ import { unify } from "../tgsl/conversion.js";
5
+ import { abstractFloat, f16, f32 } from "../data/numeric.js";
6
+ import { vecTypeToConstructor } from "../data/vector.js";
7
+ import { dualImpl } from "../core/function/dualImpl.js";
8
+ import { VectorOps } from "../data/vectorOps.js";
9
+
10
+ //#region src/std/operators.ts
11
+ const getPrimitive = (t) => "primitive" in t ? t.primitive : t;
12
+ const makeBinarySignature = (opts) => (lhs, rhs) => {
13
+ const { restrict } = opts ?? {};
14
+ const fail = (msg) => {
15
+ if (restrict) throw new SignatureNotSupportedError([lhs, rhs], restrict);
16
+ throw new Error(`Cannot apply operator to ${lhs.type} and ${rhs.type}: ${msg}`);
17
+ };
18
+ if (opts?.noMat && (isMat(lhs) || isMat(rhs))) return fail("matrices not supported");
19
+ const lhsC = isVec(lhs) || isMat(lhs);
20
+ const rhsC = isVec(rhs) || isMat(rhs);
21
+ if (!lhsC && !rhsC) {
22
+ const unified = unify([lhs, rhs], restrict);
23
+ if (!unified) return fail("incompatible scalar types");
24
+ return {
25
+ argTypes: unified,
26
+ returnType: unified[0]
27
+ };
28
+ }
29
+ if (lhsC && rhsC) {
30
+ if (opts?.matVecProduct && isVec(lhs) !== isVec(rhs)) return {
31
+ argTypes: [lhs, rhs],
32
+ returnType: isVec(lhs) ? lhs : rhs
33
+ };
34
+ if (lhs.type !== rhs.type) return fail("operands must have the same type");
35
+ return {
36
+ argTypes: [lhs, rhs],
37
+ returnType: lhs
38
+ };
39
+ }
40
+ const [scalar, composite] = lhsC ? [rhs, lhs] : [lhs, rhs];
41
+ const unified = unify([scalar], [getPrimitive(composite)]);
42
+ if (!unified) return fail(`scalar not convertible to ${getPrimitive(composite).type}`);
43
+ return {
44
+ argTypes: lhsC ? [lhs, unified[0]] : [unified[0], rhs],
45
+ returnType: composite
46
+ };
47
+ };
48
+ const binaryArithmeticSignature = makeBinarySignature();
49
+ const binaryMulSignature = makeBinarySignature({ matVecProduct: true });
50
+ const binaryDivSignature = makeBinarySignature({
51
+ noMat: true,
52
+ restrict: [
53
+ f32,
54
+ f16,
55
+ abstractFloat
56
+ ]
57
+ });
58
+ function cpuAdd(lhs, rhs) {
59
+ if (typeof lhs === "number" && typeof rhs === "number") return lhs + rhs;
60
+ if (typeof lhs === "number" && isVecInstance(rhs)) return VectorOps.addMixed[rhs.kind](rhs, lhs);
61
+ if (isVecInstance(lhs) && typeof rhs === "number") return VectorOps.addMixed[lhs.kind](lhs, rhs);
62
+ if (isVecInstance(lhs) && isVecInstance(rhs) || isMatInstance(lhs) && isMatInstance(rhs)) return VectorOps.add[lhs.kind](lhs, rhs);
63
+ throw new Error("Add/Sub called with invalid arguments.");
64
+ }
65
+ const add = dualImpl({
66
+ name: "add",
67
+ signature: binaryArithmeticSignature,
68
+ normalImpl: cpuAdd,
69
+ codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} + ${rhs})`
70
+ });
71
+ function cpuSub(lhs, rhs) {
72
+ return cpuAdd(lhs, cpuMul(-1, rhs));
73
+ }
74
+ const sub = dualImpl({
75
+ name: "sub",
76
+ signature: binaryArithmeticSignature,
77
+ normalImpl: cpuSub,
78
+ codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} - ${rhs})`
79
+ });
80
+ function cpuMul(lhs, rhs) {
81
+ if (typeof lhs === "number" && typeof rhs === "number") return lhs * rhs;
82
+ if (typeof lhs === "number" && (isVecInstance(rhs) || isMatInstance(rhs))) return VectorOps.mulSxV[rhs.kind](lhs, rhs);
83
+ if ((isVecInstance(lhs) || isMatInstance(lhs)) && typeof rhs === "number") return VectorOps.mulSxV[lhs.kind](rhs, lhs);
84
+ if (isVecInstance(lhs) && isVecInstance(rhs)) return VectorOps.mulVxV[lhs.kind](lhs, rhs);
85
+ if (isFloat32VecInstance(lhs) && isMatInstance(rhs)) return VectorOps.mulVxM[rhs.kind](lhs, rhs);
86
+ if (isMatInstance(lhs) && isFloat32VecInstance(rhs)) return VectorOps.mulMxV[lhs.kind](lhs, rhs);
87
+ if (isMatInstance(lhs) && isMatInstance(rhs)) return VectorOps.mulVxV[lhs.kind](lhs, rhs);
88
+ throw new Error("Mul called with invalid arguments.");
89
+ }
90
+ const mul = dualImpl({
91
+ name: "mul",
92
+ signature: binaryMulSignature,
93
+ normalImpl: cpuMul,
94
+ codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} * ${rhs})`
95
+ });
96
+ function cpuDiv(lhs, rhs) {
97
+ if (typeof lhs === "number" && typeof rhs === "number") return lhs / rhs;
98
+ if (typeof lhs === "number" && isVecInstance(rhs)) {
99
+ const schema = vecTypeToConstructor[rhs.kind];
100
+ return VectorOps.div[rhs.kind](schema(lhs), rhs);
101
+ }
102
+ if (isVecInstance(lhs) && typeof rhs === "number") {
103
+ const schema = vecTypeToConstructor[lhs.kind];
104
+ return VectorOps.div[lhs.kind](lhs, schema(rhs));
105
+ }
106
+ if (isVecInstance(lhs) && isVecInstance(rhs)) return VectorOps.div[lhs.kind](lhs, rhs);
107
+ throw new Error("Div called with invalid arguments.");
108
+ }
109
+ const div = dualImpl({
110
+ name: "div",
111
+ signature: binaryDivSignature,
112
+ normalImpl: cpuDiv,
113
+ codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} / ${rhs})`,
114
+ ignoreImplicitCastWarning: true
115
+ });
116
+ /**
117
+ * @privateRemarks
118
+ * Both JS and WGSL implementations use truncated definition of modulo
119
+ */
120
+ const mod = dualImpl({
121
+ name: "mod",
122
+ signature: binaryDivSignature,
123
+ normalImpl: ((a, b) => {
124
+ if (typeof a === "number" && typeof b === "number") return a % b;
125
+ if (typeof a === "number" && isVecInstance(b)) {
126
+ const schema = vecTypeToConstructor[b.kind];
127
+ return VectorOps.mod[b.kind](schema(a), b);
128
+ }
129
+ if (isVecInstance(a) && typeof b === "number") {
130
+ const schema = vecTypeToConstructor[a.kind];
131
+ return VectorOps.mod[a.kind](a, schema(b));
132
+ }
133
+ if (isVecInstance(a) && isVecInstance(b)) return VectorOps.mod[a.kind](a, b);
134
+ throw new Error("Mod called with invalid arguments, expected types: number or vector.");
135
+ }),
136
+ codegenImpl: (_ctx, [lhs, rhs]) => stitch`(${lhs} % ${rhs})`
137
+ });
138
+ function cpuNeg(value) {
139
+ if (typeof value === "number") return -value;
140
+ return VectorOps.neg[value.kind](value);
141
+ }
142
+ const neg = dualImpl({
143
+ name: "neg",
144
+ signature: (arg) => ({
145
+ argTypes: [arg],
146
+ returnType: arg
147
+ }),
148
+ normalImpl: cpuNeg,
149
+ codegenImpl: (_ctx, [arg]) => stitch`-(${arg})`
150
+ });
151
+
152
+ //#endregion
153
+ export { add, div, mod, mul, neg, sub };
@@ -0,0 +1,26 @@
1
+ import { DualFn } from "../types.js";
2
+ import { v2f, v4f } from "../data/wgslTypes.js";
3
+
4
+ //#region src/std/packing.d.ts
5
+ /**
6
+ * @privateRemarks
7
+ * https://gpuweb.github.io/gpuweb/wgsl/#unpack2x16float-builtin
8
+ */
9
+ declare const unpack2x16float: DualFn<(e: number) => v2f>;
10
+ /**
11
+ * @privateRemarks
12
+ * https://gpuweb.github.io/gpuweb/wgsl/#pack2x16float-builtin
13
+ */
14
+ declare const pack2x16float: DualFn<(e: v2f) => number>;
15
+ /**
16
+ * @privateRemarks
17
+ * https://gpuweb.github.io/gpuweb/wgsl/#unpack4x8unorm-builtin
18
+ */
19
+ declare const unpack4x8unorm: DualFn<(e: number) => v4f>;
20
+ /**
21
+ * @privateRemarks
22
+ * https://gpuweb.github.io/gpuweb/wgsl/#pack4x8unorm-builtin
23
+ */
24
+ declare const pack4x8unorm: DualFn<(e: v4f) => number>;
25
+ //#endregion
26
+ export { pack2x16float, pack4x8unorm, unpack2x16float, unpack4x8unorm };
package/std/packing.js ADDED
@@ -0,0 +1,86 @@
1
+ import { stitch } from "../core/resolve/stitch.js";
2
+ import { u32 } from "../data/numeric.js";
3
+ import { vec2f, vec4f } from "../data/vector.js";
4
+ import { dualImpl } from "../core/function/dualImpl.js";
5
+ import * as TB from "typed-binary";
6
+
7
+ //#region src/std/packing.ts
8
+ /**
9
+ * @privateRemarks
10
+ * https://gpuweb.github.io/gpuweb/wgsl/#unpack2x16float-builtin
11
+ */
12
+ const unpack2x16float = dualImpl({
13
+ name: "unpack2x16float",
14
+ normalImpl: (e) => {
15
+ const buffer = /* @__PURE__ */ new ArrayBuffer(4);
16
+ new TB.BufferWriter(buffer).writeUint32(e);
17
+ const reader = new TB.BufferReader(buffer);
18
+ return vec2f(reader.readFloat16(), reader.readFloat16());
19
+ },
20
+ signature: {
21
+ argTypes: [u32],
22
+ returnType: vec2f
23
+ },
24
+ codegenImpl: (_ctx, [e]) => stitch`unpack2x16float(${e})`
25
+ });
26
+ /**
27
+ * @privateRemarks
28
+ * https://gpuweb.github.io/gpuweb/wgsl/#pack2x16float-builtin
29
+ */
30
+ const pack2x16float = dualImpl({
31
+ name: "pack2x16float",
32
+ normalImpl: (e) => {
33
+ const buffer = /* @__PURE__ */ new ArrayBuffer(4);
34
+ const writer = new TB.BufferWriter(buffer);
35
+ writer.writeFloat16(e.x);
36
+ writer.writeFloat16(e.y);
37
+ return u32(new TB.BufferReader(buffer).readUint32());
38
+ },
39
+ signature: {
40
+ argTypes: [vec2f],
41
+ returnType: u32
42
+ },
43
+ codegenImpl: (_ctx, [e]) => stitch`pack2x16float(${e})`
44
+ });
45
+ /**
46
+ * @privateRemarks
47
+ * https://gpuweb.github.io/gpuweb/wgsl/#unpack4x8unorm-builtin
48
+ */
49
+ const unpack4x8unorm = dualImpl({
50
+ name: "unpack4x8unorm",
51
+ normalImpl: (e) => {
52
+ const buffer = /* @__PURE__ */ new ArrayBuffer(4);
53
+ new TB.BufferWriter(buffer).writeUint32(e);
54
+ const reader = new TB.BufferReader(buffer);
55
+ return vec4f(reader.readUint8() / 255, reader.readUint8() / 255, reader.readUint8() / 255, reader.readUint8() / 255);
56
+ },
57
+ signature: {
58
+ argTypes: [u32],
59
+ returnType: vec4f
60
+ },
61
+ codegenImpl: (_ctx, [e]) => stitch`unpack4x8unorm(${e})`
62
+ });
63
+ /**
64
+ * @privateRemarks
65
+ * https://gpuweb.github.io/gpuweb/wgsl/#pack4x8unorm-builtin
66
+ */
67
+ const pack4x8unorm = dualImpl({
68
+ name: "pack4x8unorm",
69
+ normalImpl: (e) => {
70
+ const buffer = /* @__PURE__ */ new ArrayBuffer(4);
71
+ const writer = new TB.BufferWriter(buffer);
72
+ writer.writeUint8(e.x * 255);
73
+ writer.writeUint8(e.y * 255);
74
+ writer.writeUint8(e.z * 255);
75
+ writer.writeUint8(e.w * 255);
76
+ return u32(new TB.BufferReader(buffer).readUint32());
77
+ },
78
+ signature: {
79
+ argTypes: [vec4f],
80
+ returnType: u32
81
+ },
82
+ codegenImpl: (_ctx, [e]) => stitch`pack4x8unorm(${e})`
83
+ });
84
+
85
+ //#endregion
86
+ export { pack2x16float, pack4x8unorm, unpack2x16float, unpack4x8unorm };
@@ -0,0 +1,47 @@
1
+ import { DualFn } from "../types.js";
2
+ import { AnyIntegerVecInstance, AnyNumericVecInstance, v4u } from "../data/wgslTypes.js";
3
+
4
+ //#region src/std/subgroup.d.ts
5
+ interface IdentityNumOrVec {
6
+ (e: number): number;
7
+ <T extends AnyNumericVecInstance>(e: T): T;
8
+ }
9
+ interface IdentityIntNumOrVec {
10
+ (e: number): number;
11
+ <T extends AnyIntegerVecInstance>(e: T): T;
12
+ }
13
+ interface IdentityNumOrVecWithIdx {
14
+ (e: number, index: number): number;
15
+ <T extends AnyNumericVecInstance>(e: T, index: number): T;
16
+ }
17
+ interface IdentityNumOrVecWithDelta {
18
+ (e: number, delta: number): number;
19
+ <T extends AnyNumericVecInstance>(e: T, delta: number): T;
20
+ }
21
+ interface IdentityNumOrVecWithMask {
22
+ (e: number, mask: number): number;
23
+ <T extends AnyNumericVecInstance>(e: T, mask: number): T;
24
+ }
25
+ declare const subgroupAdd: DualFn<IdentityNumOrVec>;
26
+ declare const subgroupExclusiveAdd: DualFn<IdentityNumOrVec>;
27
+ declare const subgroupInclusiveAdd: DualFn<IdentityNumOrVec>;
28
+ declare const subgroupAll: DualFn<(e: boolean) => boolean>;
29
+ declare const subgroupAnd: DualFn<IdentityIntNumOrVec>;
30
+ declare const subgroupAny: DualFn<(e: boolean) => boolean>;
31
+ declare const subgroupBallot: DualFn<(e: boolean) => v4u>;
32
+ declare const subgroupBroadcast: DualFn<IdentityNumOrVecWithIdx>;
33
+ declare const subgroupBroadcastFirst: DualFn<IdentityNumOrVec>;
34
+ declare const subgroupElect: DualFn<() => boolean>;
35
+ declare const subgroupMax: DualFn<IdentityNumOrVec>;
36
+ declare const subgroupMin: DualFn<IdentityNumOrVec>;
37
+ declare const subgroupMul: DualFn<IdentityNumOrVec>;
38
+ declare const subgroupExclusiveMul: DualFn<IdentityNumOrVec>;
39
+ declare const subgroupInclusiveMul: DualFn<IdentityNumOrVec>;
40
+ declare const subgroupOr: DualFn<IdentityIntNumOrVec>;
41
+ declare const subgroupShuffle: DualFn<IdentityNumOrVecWithIdx>;
42
+ declare const subgroupShuffleDown: DualFn<IdentityNumOrVecWithDelta>;
43
+ declare const subgroupShuffleUp: DualFn<IdentityNumOrVecWithDelta>;
44
+ declare const subgroupShuffleXor: DualFn<IdentityNumOrVecWithMask>;
45
+ declare const subgroupXor: DualFn<IdentityIntNumOrVec>;
46
+ //#endregion
47
+ export { subgroupAdd, subgroupAll, subgroupAnd, subgroupAny, subgroupBallot, subgroupBroadcast, subgroupBroadcastFirst, subgroupElect, subgroupExclusiveAdd, subgroupExclusiveMul, subgroupInclusiveAdd, subgroupInclusiveMul, subgroupMax, subgroupMin, subgroupMul, subgroupOr, subgroupShuffle, subgroupShuffleDown, subgroupShuffleUp, subgroupShuffleXor, subgroupXor };
@@ -0,0 +1,220 @@
1
+ import { stitch } from "../core/resolve/stitch.js";
2
+ import { unify } from "../tgsl/conversion.js";
3
+ import { bool, i32, u32 } from "../data/numeric.js";
4
+ import { vec4u } from "../data/vector.js";
5
+ import { dualImpl } from "../core/function/dualImpl.js";
6
+
7
+ //#region src/std/subgroup.ts
8
+ const errorMessage = "Subgroup operations can only be used in the GPU context.";
9
+ const subgroupAdd = dualImpl({
10
+ name: "subgroupAdd",
11
+ signature: (arg) => ({
12
+ argTypes: [arg],
13
+ returnType: arg
14
+ }),
15
+ normalImpl: errorMessage,
16
+ codegenImpl: (_ctx, [arg]) => stitch`subgroupAdd(${arg})`
17
+ });
18
+ const subgroupExclusiveAdd = dualImpl({
19
+ name: "subgroupExclusiveAdd",
20
+ signature: (arg) => ({
21
+ argTypes: [arg],
22
+ returnType: arg
23
+ }),
24
+ normalImpl: errorMessage,
25
+ codegenImpl: (_ctx, [arg]) => stitch`subgroupExclusiveAdd(${arg})`
26
+ });
27
+ const subgroupInclusiveAdd = dualImpl({
28
+ name: "subgroupInclusiveAdd",
29
+ signature: (arg) => ({
30
+ argTypes: [arg],
31
+ returnType: arg
32
+ }),
33
+ normalImpl: errorMessage,
34
+ codegenImpl: (_ctx, [arg]) => stitch`subgroupInclusiveAdd(${arg})`
35
+ });
36
+ const subgroupAll = dualImpl({
37
+ name: "subgroupAll",
38
+ signature: {
39
+ argTypes: [bool],
40
+ returnType: bool
41
+ },
42
+ normalImpl: errorMessage,
43
+ codegenImpl: (_ctx, [e]) => stitch`subgroupAll(${e})`
44
+ });
45
+ const subgroupAnd = dualImpl({
46
+ name: "subgroupAnd",
47
+ signature: (arg) => ({
48
+ argTypes: [arg],
49
+ returnType: arg
50
+ }),
51
+ normalImpl: errorMessage,
52
+ codegenImpl: (_ctx, [e]) => stitch`subgroupAnd(${e})`
53
+ });
54
+ const subgroupAny = dualImpl({
55
+ name: "subgroupAny",
56
+ signature: {
57
+ argTypes: [bool],
58
+ returnType: bool
59
+ },
60
+ normalImpl: errorMessage,
61
+ codegenImpl: (_ctx, [e]) => stitch`subgroupAny(${e})`
62
+ });
63
+ const subgroupBallot = dualImpl({
64
+ name: "subgroupBallot",
65
+ signature: {
66
+ argTypes: [bool],
67
+ returnType: vec4u
68
+ },
69
+ normalImpl: errorMessage,
70
+ codegenImpl: (_ctx, [e]) => stitch`subgroupBallot(${e})`
71
+ });
72
+ const subgroupBroadcast = dualImpl({
73
+ name: "subgroupBroadcast",
74
+ signature: (...args) => {
75
+ const id = unify([args[1]], [i32, u32]);
76
+ if (!id) throw new Error(`subgroupBroadcast's second argument has to be compatible with i32 or u32. Got: ${args[1].type}`);
77
+ return {
78
+ argTypes: [args[0], id[0]],
79
+ returnType: args[0]
80
+ };
81
+ },
82
+ normalImpl: errorMessage,
83
+ codegenImpl: (_ctx, [e, index]) => stitch`subgroupBroadcast(${e}, ${index})`
84
+ });
85
+ const subgroupBroadcastFirst = dualImpl({
86
+ name: "subgroupBroadcastFirst",
87
+ signature: (arg) => ({
88
+ argTypes: [arg],
89
+ returnType: arg
90
+ }),
91
+ normalImpl: errorMessage,
92
+ codegenImpl: (_ctx, [e]) => stitch`subgroupBroadcastFirst(${e})`
93
+ });
94
+ const subgroupElect = dualImpl({
95
+ name: "subgroupElect",
96
+ signature: {
97
+ argTypes: [],
98
+ returnType: bool
99
+ },
100
+ normalImpl: errorMessage,
101
+ codegenImpl: () => stitch`subgroupElect()`
102
+ });
103
+ const subgroupMax = dualImpl({
104
+ name: "subgroupMax",
105
+ signature: (arg) => ({
106
+ argTypes: [arg],
107
+ returnType: arg
108
+ }),
109
+ normalImpl: errorMessage,
110
+ codegenImpl: (_ctx, [arg]) => stitch`subgroupMax(${arg})`
111
+ });
112
+ const subgroupMin = dualImpl({
113
+ name: "subgroupMin",
114
+ signature: (arg) => ({
115
+ argTypes: [arg],
116
+ returnType: arg
117
+ }),
118
+ normalImpl: errorMessage,
119
+ codegenImpl: (_ctx, [arg]) => stitch`subgroupMin(${arg})`
120
+ });
121
+ const subgroupMul = dualImpl({
122
+ name: "subgroupMul",
123
+ signature: (arg) => ({
124
+ argTypes: [arg],
125
+ returnType: arg
126
+ }),
127
+ normalImpl: errorMessage,
128
+ codegenImpl: (_ctx, [arg]) => stitch`subgroupMul(${arg})`
129
+ });
130
+ const subgroupExclusiveMul = dualImpl({
131
+ name: "subgroupExclusiveMul",
132
+ signature: (arg) => ({
133
+ argTypes: [arg],
134
+ returnType: arg
135
+ }),
136
+ normalImpl: errorMessage,
137
+ codegenImpl: (_ctx, [arg]) => stitch`subgroupExclusiveMul(${arg})`
138
+ });
139
+ const subgroupInclusiveMul = dualImpl({
140
+ name: "subgroupInclusiveMul",
141
+ signature: (arg) => ({
142
+ argTypes: [arg],
143
+ returnType: arg
144
+ }),
145
+ normalImpl: errorMessage,
146
+ codegenImpl: (_ctx, [arg]) => stitch`subgroupInclusiveMul(${arg})`
147
+ });
148
+ const subgroupOr = dualImpl({
149
+ name: "subgroupOr",
150
+ signature: (arg) => ({
151
+ argTypes: [arg],
152
+ returnType: arg
153
+ }),
154
+ normalImpl: errorMessage,
155
+ codegenImpl: (_ctx, [e]) => stitch`subgroupOr(${e})`
156
+ });
157
+ const subgroupShuffle = dualImpl({
158
+ name: "subgroupShuffle",
159
+ signature: (...args) => {
160
+ const id = unify([args[1]], [i32, u32]);
161
+ if (!id) throw new Error(`subgroupShuffle's second argument has to be compatible with i32 or u32. Got: ${args[1].type}`);
162
+ return {
163
+ argTypes: [args[0], id[0]],
164
+ returnType: args[0]
165
+ };
166
+ },
167
+ normalImpl: errorMessage,
168
+ codegenImpl: (_ctx, [e, index]) => stitch`subgroupShuffle(${e}, ${index})`
169
+ });
170
+ const subgroupShuffleDown = dualImpl({
171
+ name: "subgroupShuffleDown",
172
+ signature: (...args) => {
173
+ const delta = unify([args[1]], [u32]);
174
+ if (!delta) throw new Error(`subgroupShuffleDown's second argument has to be compatible with u32. Got: ${args[1].type}`);
175
+ return {
176
+ argTypes: [args[0], delta[0]],
177
+ returnType: args[0]
178
+ };
179
+ },
180
+ normalImpl: errorMessage,
181
+ codegenImpl: (_ctx, [e, delta]) => stitch`subgroupShuffleDown(${e}, ${delta})`
182
+ });
183
+ const subgroupShuffleUp = dualImpl({
184
+ name: "subgroupShuffleUp",
185
+ signature: (...args) => {
186
+ const delta = unify([args[1]], [u32]);
187
+ if (!delta) throw new Error(`subgroupShuffleUp's second argument has to be compatible with u32. Got: ${args[1].type}`);
188
+ return {
189
+ argTypes: [args[0], delta[0]],
190
+ returnType: args[0]
191
+ };
192
+ },
193
+ normalImpl: errorMessage,
194
+ codegenImpl: (_ctx, [e, delta]) => stitch`subgroupShuffleUp(${e}, ${delta})`
195
+ });
196
+ const subgroupShuffleXor = dualImpl({
197
+ name: "subgroupShuffleXor",
198
+ signature: (...args) => {
199
+ const mask = unify([args[1]], [u32]);
200
+ if (!mask) throw new Error(`subgroupShuffleXor's second argument has to be compatible with u32. Got: ${args[1].type}`);
201
+ return {
202
+ argTypes: [args[0], mask[0]],
203
+ returnType: args[0]
204
+ };
205
+ },
206
+ normalImpl: errorMessage,
207
+ codegenImpl: (_ctx, [e, mask]) => stitch`subgroupShuffleXor(${e}, ${mask})`
208
+ });
209
+ const subgroupXor = dualImpl({
210
+ name: "subgroupXor",
211
+ signature: (arg) => ({
212
+ argTypes: [arg],
213
+ returnType: arg
214
+ }),
215
+ normalImpl: errorMessage,
216
+ codegenImpl: (_ctx, [e]) => stitch`subgroupXor(${e})`
217
+ });
218
+
219
+ //#endregion
220
+ export { subgroupAdd, subgroupAll, subgroupAnd, subgroupAny, subgroupBallot, subgroupBroadcast, subgroupBroadcastFirst, subgroupElect, subgroupExclusiveAdd, subgroupExclusiveMul, subgroupInclusiveAdd, subgroupInclusiveMul, subgroupMax, subgroupMin, subgroupMul, subgroupOr, subgroupShuffle, subgroupShuffleDown, subgroupShuffleUp, subgroupShuffleXor, subgroupXor };