typegpu 0.10.1 → 0.11.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (273) hide show
  1. package/_virtual/_rolldown/runtime.js +13 -0
  2. package/builtin.d.ts +50 -0
  3. package/builtin.js +35 -0
  4. package/common/fullScreenTriangle.d.ts +22 -0
  5. package/common/fullScreenTriangle.js +34 -0
  6. package/common/index.d.ts +4 -4
  7. package/common/index.js +8 -7
  8. package/common/writeSoA.d.ts +16 -0
  9. package/common/writeSoA.js +90 -0
  10. package/core/buffer/buffer.d.ts +79 -0
  11. package/core/buffer/buffer.js +246 -0
  12. package/core/buffer/bufferShorthand.d.ts +48 -0
  13. package/core/buffer/bufferShorthand.js +53 -0
  14. package/core/buffer/bufferUsage.d.ts +43 -0
  15. package/core/buffer/bufferUsage.js +165 -0
  16. package/core/constant/tgpuConstant.d.ts +29 -0
  17. package/core/constant/tgpuConstant.js +68 -0
  18. package/core/declare/tgpuDeclare.d.ts +18 -0
  19. package/core/declare/tgpuDeclare.js +39 -0
  20. package/core/function/autoIO.d.ts +38 -0
  21. package/core/function/autoIO.js +85 -0
  22. package/core/function/comptime.d.ts +39 -0
  23. package/core/function/comptime.js +49 -0
  24. package/core/function/createCallableSchema.js +40 -0
  25. package/core/function/dualImpl.js +52 -0
  26. package/core/function/entryInputRouter.js +39 -0
  27. package/core/function/extractArgs.js +204 -0
  28. package/core/function/fnCore.js +90 -0
  29. package/core/function/fnTypes.d.ts +40 -0
  30. package/core/function/ioSchema.d.ts +10 -0
  31. package/core/function/ioSchema.js +51 -0
  32. package/core/function/shelllessImpl.d.ts +28 -0
  33. package/core/function/shelllessImpl.js +21 -0
  34. package/core/function/templateUtils.js +12 -0
  35. package/core/function/tgpuComputeFn.d.ts +48 -0
  36. package/core/function/tgpuComputeFn.js +55 -0
  37. package/core/function/tgpuFn.d.ts +52 -0
  38. package/core/function/tgpuFn.js +168 -0
  39. package/core/function/tgpuFragmentFn.d.ts +72 -0
  40. package/core/function/tgpuFragmentFn.js +63 -0
  41. package/core/function/tgpuVertexFn.d.ts +59 -0
  42. package/core/function/tgpuVertexFn.js +59 -0
  43. package/core/pipeline/applyPipelineState.js +35 -0
  44. package/core/pipeline/computePipeline.d.ts +54 -0
  45. package/core/pipeline/computePipeline.js +227 -0
  46. package/core/pipeline/connectAttachmentToShader.js +24 -0
  47. package/core/pipeline/connectTargetsToShader.js +27 -0
  48. package/core/pipeline/limitsOverflow.js +12 -0
  49. package/core/pipeline/pipelineUtils.js +29 -0
  50. package/core/pipeline/renderPipeline.d.ts +284 -0
  51. package/core/pipeline/renderPipeline.js +489 -0
  52. package/core/pipeline/timeable.d.ts +20 -0
  53. package/core/pipeline/timeable.js +55 -0
  54. package/core/pipeline/typeGuards.js +27 -0
  55. package/core/querySet/querySet.d.ts +20 -0
  56. package/core/querySet/querySet.js +104 -0
  57. package/core/rawCodeSnippet/tgpuRawCodeSnippet.d.ts +59 -0
  58. package/core/rawCodeSnippet/tgpuRawCodeSnippet.js +94 -0
  59. package/core/resolve/externals.d.ts +8 -0
  60. package/core/resolve/externals.js +56 -0
  61. package/core/resolve/namespace.d.ts +38 -0
  62. package/core/resolve/namespace.js +39 -0
  63. package/core/resolve/resolveData.js +144 -0
  64. package/core/resolve/stitch.js +23 -0
  65. package/core/resolve/tgpuResolve.d.ts +153 -0
  66. package/core/resolve/tgpuResolve.js +66 -0
  67. package/core/root/configurableImpl.js +17 -0
  68. package/core/root/init.d.ts +64 -0
  69. package/core/root/init.js +464 -0
  70. package/core/root/rootTypes.d.ts +642 -0
  71. package/core/sampler/sampler.d.ts +31 -0
  72. package/core/sampler/sampler.js +116 -0
  73. package/core/simulate/tgpuSimulate.d.ts +36 -0
  74. package/core/simulate/tgpuSimulate.js +74 -0
  75. package/core/slot/accessor.d.ts +9 -0
  76. package/core/slot/accessor.js +95 -0
  77. package/core/slot/internalSlots.js +5 -0
  78. package/core/slot/lazy.d.ts +6 -0
  79. package/core/slot/lazy.js +40 -0
  80. package/core/slot/slot.d.ts +6 -0
  81. package/core/slot/slot.js +39 -0
  82. package/core/slot/slotTypes.d.ts +92 -0
  83. package/core/slot/slotTypes.js +19 -0
  84. package/core/texture/externalTexture.d.ts +6 -0
  85. package/core/texture/externalTexture.js +47 -0
  86. package/core/texture/texture.d.ts +114 -0
  87. package/core/texture/texture.js +314 -0
  88. package/core/texture/textureFormats.d.ts +29 -0
  89. package/core/texture/textureFormats.js +97 -0
  90. package/core/texture/textureProps.d.ts +11 -0
  91. package/core/texture/textureUtils.js +222 -0
  92. package/core/texture/usageExtension.d.ts +21 -0
  93. package/core/texture/usageExtension.js +19 -0
  94. package/core/unroll/tgpuUnroll.d.ts +68 -0
  95. package/core/unroll/tgpuUnroll.js +94 -0
  96. package/core/valueProxyUtils.js +42 -0
  97. package/core/variable/tgpuVariable.d.ts +38 -0
  98. package/core/variable/tgpuVariable.js +99 -0
  99. package/core/vertexLayout/connectAttributesToShader.js +57 -0
  100. package/core/vertexLayout/vertexAttribute.d.ts +29 -0
  101. package/core/vertexLayout/vertexLayout.d.ts +19 -0
  102. package/core/vertexLayout/vertexLayout.js +103 -0
  103. package/data/alignIO.js +14 -0
  104. package/data/alignmentOf.d.ts +9 -0
  105. package/data/alignmentOf.js +86 -0
  106. package/data/array.d.ts +26 -0
  107. package/data/array.js +46 -0
  108. package/data/atomic.d.ts +15 -0
  109. package/data/atomic.js +24 -0
  110. package/data/attributes.d.ts +121 -0
  111. package/data/attributes.js +145 -0
  112. package/data/autoStruct.d.ts +1 -0
  113. package/data/autoStruct.js +81 -0
  114. package/data/compiledIO.js +228 -0
  115. package/data/dataIO.js +556 -0
  116. package/data/dataTypes.d.ts +115 -0
  117. package/data/dataTypes.js +100 -0
  118. package/data/deepEqual.d.ts +25 -0
  119. package/data/deepEqual.js +56 -0
  120. package/data/disarray.d.ts +32 -0
  121. package/data/disarray.js +50 -0
  122. package/data/getLongestContiguousPrefix.d.ts +9 -0
  123. package/data/getLongestContiguousPrefix.js +13 -0
  124. package/data/index.d.ts +26 -4
  125. package/data/index.js +36 -9
  126. package/data/instanceToSchema.d.ts +33 -0
  127. package/data/isContiguous.d.ts +9 -0
  128. package/data/isContiguous.js +13 -0
  129. package/data/matrix.d.ts +124 -0
  130. package/data/matrix.js +531 -0
  131. package/data/numberOps.js +23 -0
  132. package/data/numeric.d.ts +81 -0
  133. package/data/numeric.js +221 -0
  134. package/data/offsetUtils.d.ts +33 -0
  135. package/data/offsetUtils.js +165 -0
  136. package/data/offsets.js +34 -0
  137. package/data/partialIO.js +113 -0
  138. package/data/ptr.d.ts +11 -0
  139. package/data/ptr.js +44 -0
  140. package/data/ref.d.ts +34 -0
  141. package/data/ref.js +94 -0
  142. package/data/sampler.d.ts +107 -0
  143. package/data/sampler.js +24 -0
  144. package/data/schemaCallWrapper.js +30 -0
  145. package/data/schemaMemoryLayout.js +198 -0
  146. package/data/sizeOf.d.ts +9 -0
  147. package/data/sizeOf.js +13 -0
  148. package/data/snippet.d.ts +26 -0
  149. package/data/snippet.js +70 -0
  150. package/data/struct.d.ts +17 -0
  151. package/data/struct.js +44 -0
  152. package/data/texture.d.ts +292 -0
  153. package/{texture-BagDrrks.js → data/texture.js} +6 -5
  154. package/data/unstruct.d.ts +24 -0
  155. package/data/unstruct.js +41 -0
  156. package/data/vector.d.ts +191 -0
  157. package/data/vector.js +239 -0
  158. package/data/vectorImpl.js +515 -0
  159. package/data/vectorOps.js +681 -0
  160. package/data/vertexFormatData.d.ts +190 -0
  161. package/data/vertexFormatData.js +109 -0
  162. package/data/wgslTypes.d.ts +924 -0
  163. package/data/wgslTypes.js +222 -0
  164. package/errors.d.ts +44 -0
  165. package/errors.js +131 -0
  166. package/execMode.js +49 -0
  167. package/extension.d.ts +11 -0
  168. package/extension.js +16 -0
  169. package/getGPUValue.js +7 -0
  170. package/index.d.ts +42 -243
  171. package/index.js +21 -6320
  172. package/indexNamedExports.d.ts +40 -0
  173. package/mathUtils.js +12 -0
  174. package/memo.js +22 -0
  175. package/nameRegistry.d.ts +30 -0
  176. package/nameRegistry.js +447 -0
  177. package/package.js +4 -0
  178. package/package.json +26 -26
  179. package/resolutionCtx.d.ts +19 -0
  180. package/resolutionCtx.js +612 -0
  181. package/shared/env.js +12 -0
  182. package/shared/generators.js +13 -0
  183. package/shared/meta.d.ts +39 -0
  184. package/shared/meta.js +61 -0
  185. package/shared/repr.d.ts +138 -0
  186. package/shared/stringify.js +20 -0
  187. package/shared/symbols.d.ts +70 -0
  188. package/shared/symbols.js +48 -0
  189. package/shared/utilityTypes.d.ts +33 -0
  190. package/shared/utilityTypes.js +6 -0
  191. package/shared/vertexFormat.d.ts +70 -0
  192. package/shared/vertexFormat.js +63 -0
  193. package/std/array.d.ts +7 -0
  194. package/std/array.js +25 -0
  195. package/std/atomic.d.ts +19 -0
  196. package/std/atomic.js +111 -0
  197. package/std/bitcast.d.ts +10 -0
  198. package/std/bitcast.js +41 -0
  199. package/std/boolean.d.ts +141 -0
  200. package/std/boolean.js +299 -0
  201. package/std/derivative.d.ts +16 -0
  202. package/std/derivative.js +87 -0
  203. package/std/discard.d.ts +6 -0
  204. package/std/discard.js +14 -0
  205. package/std/extensions.d.ts +6 -0
  206. package/std/extensions.js +12 -0
  207. package/std/index.d.ts +17 -4
  208. package/std/index.js +21 -7
  209. package/std/matrix.d.ts +41 -0
  210. package/std/matrix.js +85 -0
  211. package/std/numeric.d.ts +200 -0
  212. package/std/numeric.js +845 -0
  213. package/std/operators.d.ts +56 -0
  214. package/std/operators.js +227 -0
  215. package/std/packing.d.ts +26 -0
  216. package/std/packing.js +84 -0
  217. package/std/range.d.ts +24 -0
  218. package/std/range.js +38 -0
  219. package/std/subgroup.d.ts +47 -0
  220. package/std/subgroup.js +218 -0
  221. package/std/texture.d.ts +117 -0
  222. package/std/texture.js +207 -0
  223. package/tgpu.js +42 -0
  224. package/tgpuBindGroupLayout.d.ts +161 -0
  225. package/tgpuBindGroupLayout.js +272 -0
  226. package/tgpuUnstable.d.ts +48 -0
  227. package/tgpuUnstable.js +64 -0
  228. package/tgsl/accessIndex.js +43 -0
  229. package/tgsl/accessProp.js +115 -0
  230. package/tgsl/consoleLog/deserializers.js +115 -0
  231. package/tgsl/consoleLog/logGenerator.js +84 -0
  232. package/tgsl/consoleLog/serializers.js +223 -0
  233. package/tgsl/consoleLog/types.d.ts +52 -0
  234. package/tgsl/consoleLog/types.js +11 -0
  235. package/tgsl/conversion.js +198 -0
  236. package/tgsl/forOfUtils.js +71 -0
  237. package/tgsl/generationHelpers.d.ts +37 -0
  238. package/tgsl/generationHelpers.js +67 -0
  239. package/tgsl/math.js +43 -0
  240. package/tgsl/shaderGenerator.d.ts +20 -0
  241. package/tgsl/shaderGenerator_members.d.ts +2 -0
  242. package/tgsl/shaderGenerator_members.js +6 -0
  243. package/tgsl/shellless.d.ts +11 -0
  244. package/tgsl/shellless.js +46 -0
  245. package/tgsl/wgslGenerator.d.ts +36 -0
  246. package/tgsl/wgslGenerator.js +639 -0
  247. package/types.d.ts +265 -0
  248. package/types.js +43 -0
  249. package/unwrapper.d.ts +27 -0
  250. package/wgslExtensions.d.ts +5 -0
  251. package/wgslExtensions.js +17 -0
  252. package/builtin-DdtWpk2t.js +0 -818
  253. package/builtin-DdtWpk2t.js.map +0 -1
  254. package/chunk-BYypO7fO.js +0 -18
  255. package/common/index.d.ts.map +0 -1
  256. package/common/index.js.map +0 -1
  257. package/data/index.d.ts.map +0 -1
  258. package/data/index.js.map +0 -1
  259. package/deepEqual-DQxK4vdp.js +0 -413
  260. package/deepEqual-DQxK4vdp.js.map +0 -1
  261. package/extensions-DIVuAfBM.js +0 -2032
  262. package/extensions-DIVuAfBM.js.map +0 -1
  263. package/fullScreenTriangle-CfFyQd_0.js +0 -543
  264. package/fullScreenTriangle-CfFyQd_0.js.map +0 -1
  265. package/index.d.ts.map +0 -1
  266. package/index.js.map +0 -1
  267. package/indexNamedExports-oL6tyaJ9.d.ts +0 -5697
  268. package/indexNamedExports-oL6tyaJ9.d.ts.map +0 -1
  269. package/operators-d-PMVTo7.js +0 -4158
  270. package/operators-d-PMVTo7.js.map +0 -1
  271. package/std/index.d.ts.map +0 -1
  272. package/std/index.js.map +0 -1
  273. package/texture-BagDrrks.js.map +0 -1
package/data/ref.js ADDED
@@ -0,0 +1,94 @@
1
+ import { $gpuCallable, $internal, $ownSnippet, $resolve } from "../shared/symbols.js";
2
+ import { setName } from "../shared/meta.js";
3
+ import { isNaturallyEphemeral, isPtr } from "./wgslTypes.js";
4
+ import { UnknownData } from "./dataTypes.js";
5
+ import { snip } from "./snippet.js";
6
+ import { WgslTypeError } from "../errors.js";
7
+ import { stitch } from "../core/resolve/stitch.js";
8
+ import { createPtrFromOrigin, explicitFrom } from "./ptr.js";
9
+ //#region src/data/ref.ts
10
+ const _ref = (() => {
11
+ const impl = ((value) => INTERNAL_createRef(value));
12
+ setName(impl, "ref");
13
+ impl.toString = () => "ref";
14
+ impl[$internal] = true;
15
+ impl[$gpuCallable] = { call(_ctx, [value]) {
16
+ if (value.origin === "argument") throw new WgslTypeError(stitch`d.ref(${value}) is illegal, cannot take a reference of an argument. Copy the value locally first, and take a reference of the copy.`);
17
+ if (isPtr(value.dataType)) return snip(value.value, explicitFrom(value.dataType), value.origin);
18
+ /**
19
+ * Pointer type only exists if the ref was created from a reference (buttery-butter).
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * const life = ref(42); // created from a value
24
+ * const boid = ref(layout.$.boids[0]); // created from a reference
25
+ * ```
26
+ */
27
+ const ptrType = createPtrFromOrigin(value.origin, value.dataType);
28
+ return snip(new RefOperator(value, ptrType), ptrType ?? UnknownData, "runtime");
29
+ } };
30
+ return impl;
31
+ })();
32
+ function isRef(value) {
33
+ return value?.[$internal]?.type === "ref";
34
+ }
35
+ function INTERNAL_createRef(value) {
36
+ const target = {
37
+ [$internal]: { type: "ref" },
38
+ get $() {
39
+ return value;
40
+ },
41
+ set $(newValue) {
42
+ if (newValue && typeof newValue === "object") for (const key of Object.keys(newValue)) value[key] = newValue[key];
43
+ else value = newValue;
44
+ }
45
+ };
46
+ if (value === void 0 || value === null) throw new Error("Cannot create a ref from undefined or null");
47
+ if (typeof value === "object") return new Proxy(target, {
48
+ get(target, prop) {
49
+ if (prop in target) return target[prop];
50
+ return value[prop];
51
+ },
52
+ set(_target, prop, propValue) {
53
+ if (prop === $internal) return false;
54
+ if (prop === "$") {
55
+ console.log("Setting ref value:", propValue);
56
+ return Reflect.set(target, prop, propValue);
57
+ }
58
+ return Reflect.set(value, prop, propValue);
59
+ }
60
+ });
61
+ return target;
62
+ }
63
+ /**
64
+ * The result of calling `d.ref(...)`. The code responsible for
65
+ * generating shader code can check if the value of a snippet is
66
+ * an instance of `RefOperator`, and act accordingly.
67
+ */
68
+ var RefOperator = class {
69
+ [$internal];
70
+ snippet;
71
+ #ptrType;
72
+ constructor(snippet, ptrType) {
73
+ this[$internal] = true;
74
+ this.snippet = snippet;
75
+ this.#ptrType = ptrType;
76
+ }
77
+ get [$ownSnippet]() {
78
+ if (!this.#ptrType) throw new Error(stitch`Cannot take a reference of ${this.snippet}`);
79
+ return snip(this, this.#ptrType, this.snippet.origin);
80
+ }
81
+ [$resolve]() {
82
+ if (!this.#ptrType) throw new Error(stitch`Cannot take a reference of ${this.snippet}`);
83
+ return snip(stitch`(&${this.snippet})`, this.#ptrType, this.snippet.origin);
84
+ }
85
+ };
86
+ function derefSnippet(snippet) {
87
+ if (!isPtr(snippet.dataType)) return snippet;
88
+ const innerType = snippet.dataType.inner;
89
+ const origin = isNaturallyEphemeral(innerType) ? "runtime" : snippet.origin;
90
+ if (snippet.value instanceof RefOperator) return snip(stitch`${snippet.value.snippet}`, innerType, origin);
91
+ return snip(stitch`(*${snippet})`, innerType, origin);
92
+ }
93
+ //#endregion
94
+ export { RefOperator, _ref, derefSnippet, isRef };
@@ -0,0 +1,107 @@
1
+ import { $internal, $repr } from "../shared/symbols.js";
2
+ import { BaseData } from "./wgslTypes.js";
3
+
4
+ //#region src/data/sampler.d.ts
5
+ interface WgslSamplerProps {
6
+ addressModeU?: GPUAddressMode;
7
+ addressModeV?: GPUAddressMode;
8
+ /**
9
+ * Specifies the address modes for the texture width, height, and depth
10
+ * coordinates, respectively.
11
+ */
12
+ addressModeW?: GPUAddressMode;
13
+ /**
14
+ * Specifies the sampling behavior when the sample footprint is smaller than or equal to one
15
+ * texel.
16
+ */
17
+ magFilter?: GPUFilterMode;
18
+ /**
19
+ * Specifies the sampling behavior when the sample footprint is larger than one texel.
20
+ */
21
+ minFilter?: GPUFilterMode;
22
+ /**
23
+ * Specifies behavior for sampling between mipmap levels.
24
+ */
25
+ mipmapFilter?: GPUMipmapFilterMode;
26
+ lodMinClamp?: number;
27
+ /**
28
+ * Specifies the minimum and maximum levels of detail, respectively, used internally when
29
+ * sampling a texture.
30
+ */
31
+ lodMaxClamp?: number;
32
+ /**
33
+ * Specifies the maximum anisotropy value clamp used by the sampler. Anisotropic filtering is
34
+ * enabled when {@link GPUSamplerDescriptor.maxAnisotropy} is > 1 and the implementation supports it.
35
+ * Anisotropic filtering improves the image quality of textures sampled at oblique viewing
36
+ * angles. Higher {@link GPUSamplerDescriptor.maxAnisotropy} values indicate the maximum ratio of
37
+ * anisotropy supported when filtering.
38
+ *
39
+ * Most implementations support {@link GPUSamplerDescriptor.maxAnisotropy} values in range
40
+ * between 1 and 16, inclusive. The used value of {@link GPUSamplerDescriptor.maxAnisotropy}
41
+ * will be clamped to the maximum value that the platform supports.
42
+ * The precise filtering behavior is implementation-dependent.
43
+ */
44
+ maxAnisotropy?: number;
45
+ }
46
+ interface WgslComparisonSamplerProps {
47
+ compare: GPUCompareFunction;
48
+ addressModeU?: GPUAddressMode;
49
+ addressModeV?: GPUAddressMode;
50
+ /**
51
+ * Specifies the address modes for the texture width, height, and depth
52
+ * coordinates, respectively.
53
+ */
54
+ addressModeW?: GPUAddressMode;
55
+ /**
56
+ * Specifies the sampling behavior when the sample footprint is smaller than or equal to one
57
+ * texel.
58
+ */
59
+ magFilter?: GPUFilterMode;
60
+ /**
61
+ * Specifies the sampling behavior when the sample footprint is larger than one texel.
62
+ */
63
+ minFilter?: GPUFilterMode;
64
+ /**
65
+ * Specifies behavior for sampling between mipmap levels.
66
+ */
67
+ mipmapFilter?: GPUMipmapFilterMode;
68
+ lodMinClamp?: number;
69
+ /**
70
+ * Specifies the minimum and maximum levels of detail, respectively, used internally when
71
+ * sampling a texture.
72
+ */
73
+ lodMaxClamp?: number;
74
+ /**
75
+ * Specifies the maximum anisotropy value clamp used by the sampler. Anisotropic filtering is
76
+ * enabled when {@link GPUSamplerDescriptor.maxAnisotropy} is > 1 and the implementation supports it.
77
+ * Anisotropic filtering improves the image quality of textures sampled at oblique viewing
78
+ * angles. Higher {@link GPUSamplerDescriptor.maxAnisotropy} values indicate the maximum ratio of
79
+ * anisotropy supported when filtering.
80
+ *
81
+ * Most implementations support {@link GPUSamplerDescriptor.maxAnisotropy} values in range
82
+ * between 1 and 16, inclusive. The used value of {@link GPUSamplerDescriptor.maxAnisotropy}
83
+ * will be clamped to the maximum value that the platform supports.
84
+ * The precise filtering behavior is implementation-dependent.
85
+ */
86
+ maxAnisotropy?: number;
87
+ }
88
+ interface sampler {
89
+ [$internal]: true;
90
+ type: 'sampler';
91
+ }
92
+ declare function sampler(): WgslSampler;
93
+ interface comparisonSampler {
94
+ [$internal]: Record<string, never>;
95
+ type: 'sampler_comparison';
96
+ }
97
+ declare function comparisonSampler(): WgslComparisonSampler;
98
+ interface WgslSampler extends BaseData {
99
+ readonly [$repr]: sampler;
100
+ readonly type: 'sampler';
101
+ }
102
+ interface WgslComparisonSampler extends BaseData {
103
+ readonly [$repr]: comparisonSampler;
104
+ readonly type: 'sampler_comparison';
105
+ }
106
+ //#endregion
107
+ export { WgslComparisonSampler, WgslComparisonSamplerProps, WgslSampler, WgslSamplerProps, comparisonSampler, sampler };
@@ -0,0 +1,24 @@
1
+ import { $internal, $repr } from "../shared/symbols.js";
2
+ //#region src/data/sampler.ts
3
+ function sampler() {
4
+ return {
5
+ [$internal]: {},
6
+ type: "sampler",
7
+ [$repr]: void 0
8
+ };
9
+ }
10
+ function comparisonSampler() {
11
+ return {
12
+ [$internal]: {},
13
+ type: "sampler_comparison",
14
+ [$repr]: void 0
15
+ };
16
+ }
17
+ function isWgslSampler(value) {
18
+ return !!value[$internal] && value.type === "sampler";
19
+ }
20
+ function isWgslComparisonSampler(value) {
21
+ return !!value[$internal] && value.type === "sampler_comparison";
22
+ }
23
+ //#endregion
24
+ export { comparisonSampler, isWgslComparisonSampler, isWgslSampler, sampler };
@@ -0,0 +1,30 @@
1
+ import { $cast, $gpuCallable } from "../shared/symbols.js";
2
+ import { hasCast, isGPUCallable } from "../types.js";
3
+ //#region src/data/schemaCallWrapper.ts
4
+ /**
5
+ * A wrapper for `schema(item)` or `schema()` call on JS side.
6
+ * If the schema is a pointer, returns the value pointed to without copying.
7
+ * If the schema is a TgpuVertexFormatData, calls the corresponding constructible schema instead.
8
+ * If the schema is not callable, throws an error.
9
+ * Otherwise, returns `schema(item)` or `schema()`.
10
+ */
11
+ function schemaCallWrapper(schema, item) {
12
+ const callSchema = schema;
13
+ if (hasCast(callSchema)) return callSchema[$cast](item);
14
+ if (typeof callSchema !== "function") return item;
15
+ return item === void 0 ? callSchema() : callSchema(item);
16
+ }
17
+ /**
18
+ * A wrapper for `schema(item)` or `schema()` call on the GPU side.
19
+ * If the schema is a pointer, returns the value pointed to without copying.
20
+ * If the schema is a TgpuVertexFormatData, calls the corresponding constructible schema instead.
21
+ * If the schema is not callable, throws an error.
22
+ * Otherwise, returns `schema(item)` or `schema()`.
23
+ */
24
+ function schemaCallWrapperGPU(ctx, schema, item) {
25
+ if (!isGPUCallable(schema)) return item;
26
+ const callSchema = schema;
27
+ return item === void 0 || item.value === void 0 ? callSchema[$gpuCallable].call(ctx, []) : callSchema[$gpuCallable].call(ctx, [item]);
28
+ }
29
+ //#endregion
30
+ export { schemaCallWrapper, schemaCallWrapperGPU };
@@ -0,0 +1,198 @@
1
+ import { isDecorated, isWgslArray, isWgslStruct } from "./wgslTypes.js";
2
+ import { getCustomSize, isDisarray, isLooseDecorated, isUnstruct, undecorate } from "./dataTypes.js";
3
+ import { alignmentOf, customAlignmentOf } from "./alignmentOf.js";
4
+ import { roundUp } from "../mathUtils.js";
5
+ //#region src/data/schemaMemoryLayout.ts
6
+ const knownSizesMap = {
7
+ bool: 4,
8
+ f32: 4,
9
+ f16: 2,
10
+ i32: 4,
11
+ u32: 4,
12
+ u16: 2,
13
+ vec2f: 8,
14
+ vec2h: 4,
15
+ vec2i: 8,
16
+ vec2u: 8,
17
+ "vec2<bool>": 8,
18
+ vec3f: 12,
19
+ vec3h: 6,
20
+ vec3i: 12,
21
+ vec3u: 12,
22
+ "vec3<bool>": 12,
23
+ vec4f: 16,
24
+ vec4h: 8,
25
+ vec4i: 16,
26
+ vec4u: 16,
27
+ "vec4<bool>": 16,
28
+ mat2x2f: 16,
29
+ mat3x3f: 48,
30
+ mat4x4f: 64,
31
+ uint8: 1,
32
+ uint8x2: 2,
33
+ uint8x4: 4,
34
+ sint8: 1,
35
+ sint8x2: 2,
36
+ sint8x4: 4,
37
+ unorm8: 1,
38
+ unorm8x2: 2,
39
+ unorm8x4: 4,
40
+ snorm8: 1,
41
+ snorm8x2: 2,
42
+ snorm8x4: 4,
43
+ uint16: 2,
44
+ uint16x2: 4,
45
+ uint16x4: 8,
46
+ sint16: 2,
47
+ sint16x2: 4,
48
+ sint16x4: 8,
49
+ unorm16: 2,
50
+ unorm16x2: 4,
51
+ unorm16x4: 8,
52
+ snorm16: 2,
53
+ snorm16x2: 4,
54
+ snorm16x4: 8,
55
+ float16: 2,
56
+ float16x2: 4,
57
+ float16x4: 8,
58
+ float32: 4,
59
+ float32x2: 8,
60
+ float32x3: 12,
61
+ float32x4: 16,
62
+ uint32: 4,
63
+ uint32x2: 8,
64
+ uint32x3: 12,
65
+ uint32x4: 16,
66
+ sint32: 4,
67
+ sint32x2: 8,
68
+ sint32x3: 12,
69
+ sint32x4: 16,
70
+ "unorm10-10-10-2": 4,
71
+ "unorm8x4-bgra": 4,
72
+ atomic: 4
73
+ };
74
+ function computeMLOfStruct(struct) {
75
+ let size = 0;
76
+ let longestContiguousPrefix = 0;
77
+ let isContiguous = true;
78
+ let prefixEnd = false;
79
+ for (const property of Object.values(struct.propTypes)) {
80
+ if (Number.isNaN(size)) throw new Error("Only the last property of a struct can be unbounded");
81
+ const prevSize = size;
82
+ size = roundUp(size, alignmentOf(property));
83
+ const hasPadding = prevSize !== size;
84
+ const propLayout = computeMemoryLayout(property);
85
+ size += propLayout.size;
86
+ if (Number.isNaN(size) && property.type !== "array") throw new Error("Cannot nest unbounded struct within another struct");
87
+ if (prefixEnd) continue;
88
+ if (!hasPadding && propLayout.isContiguous) longestContiguousPrefix += propLayout.size;
89
+ else {
90
+ prefixEnd = true;
91
+ isContiguous = false;
92
+ if (!hasPadding) longestContiguousPrefix += propLayout.longestContiguousPrefix;
93
+ }
94
+ }
95
+ const trueSize = roundUp(size, alignmentOf(struct));
96
+ return {
97
+ isContiguous: size === trueSize && isContiguous,
98
+ size: trueSize,
99
+ longestContiguousPrefix
100
+ };
101
+ }
102
+ function computeMLOfUnstruct(data) {
103
+ let size = 0;
104
+ let longestContiguousPrefix = 0;
105
+ let isContiguous = true;
106
+ let prefixEnd = false;
107
+ for (const property of Object.values(data.propTypes)) {
108
+ const alignment = customAlignmentOf(property);
109
+ const prevSize = size;
110
+ size = roundUp(size, alignment);
111
+ const hasPadding = prevSize !== size;
112
+ if (hasPadding) isContiguous = false;
113
+ const propLayout = computeMemoryLayout(property);
114
+ size += propLayout.size;
115
+ if (prefixEnd) continue;
116
+ if (!hasPadding && propLayout.isContiguous) longestContiguousPrefix += propLayout.size;
117
+ else {
118
+ prefixEnd = true;
119
+ isContiguous = false;
120
+ if (!hasPadding) longestContiguousPrefix += propLayout.longestContiguousPrefix;
121
+ }
122
+ }
123
+ return {
124
+ isContiguous,
125
+ size,
126
+ longestContiguousPrefix
127
+ };
128
+ }
129
+ function computeMLOfWgslArray(data) {
130
+ const elementType = data.elementType;
131
+ const elementMemoryLayout = computeMemoryLayout(elementType);
132
+ const elementSize = elementMemoryLayout.size;
133
+ const stride = roundUp(elementSize, alignmentOf(elementType));
134
+ const isContiguous = !(stride > elementSize) && elementMemoryLayout.isContiguous;
135
+ const size = data.elementCount === 0 ? NaN : data.elementCount * stride;
136
+ let longestContiguousPrefix;
137
+ if (isContiguous) longestContiguousPrefix = size;
138
+ else longestContiguousPrefix = elementMemoryLayout.longestContiguousPrefix;
139
+ return {
140
+ size,
141
+ isContiguous,
142
+ longestContiguousPrefix
143
+ };
144
+ }
145
+ function computeMLOfDisarray(data) {
146
+ const elementType = data.elementType;
147
+ const elementMemoryLayout = computeMemoryLayout(elementType);
148
+ const elementSize = elementMemoryLayout.size;
149
+ const stride = roundUp(elementSize, customAlignmentOf(elementType));
150
+ const isContiguous = !(stride > elementSize) && elementMemoryLayout.isContiguous;
151
+ const size = data.elementCount * stride;
152
+ let longestContiguousPrefix;
153
+ if (isContiguous) longestContiguousPrefix = size;
154
+ else longestContiguousPrefix = elementMemoryLayout.longestContiguousPrefix;
155
+ return {
156
+ size,
157
+ isContiguous,
158
+ longestContiguousPrefix
159
+ };
160
+ }
161
+ function computeMemoryLayout(data) {
162
+ const knownSize = knownSizesMap[data.type];
163
+ if (knownSize !== void 0) return {
164
+ isContiguous: data.type !== "mat3x3f",
165
+ size: knownSize,
166
+ longestContiguousPrefix: data.type === "mat3x3f" ? 12 : knownSize
167
+ };
168
+ if (isWgslStruct(data)) return computeMLOfStruct(data);
169
+ if (isUnstruct(data)) return computeMLOfUnstruct(data);
170
+ if (isWgslArray(data)) return computeMLOfWgslArray(data);
171
+ if (isDisarray(data)) return computeMLOfDisarray(data);
172
+ if (isDecorated(data) || isLooseDecorated(data)) {
173
+ const size = getCustomSize(data);
174
+ const undecoratedLayout = computeMemoryLayout(undecorate(data));
175
+ if (size) return {
176
+ isContiguous: size === undecoratedLayout.size && undecoratedLayout.isContiguous,
177
+ size,
178
+ longestContiguousPrefix: undecoratedLayout.longestContiguousPrefix
179
+ };
180
+ return computeMemoryLayout(data.inner);
181
+ }
182
+ throw new Error(`Cannot determine memory layout of data: ${data}`);
183
+ }
184
+ /**
185
+ * Since memory layout can be inferred from data types, they are not stored on them.
186
+ * Instead, this weak map acts as an extended property of those data types.
187
+ */
188
+ const cachedLayouts = /* @__PURE__ */ new WeakMap();
189
+ function getLayoutInfo(schema, key) {
190
+ let layout = cachedLayouts.get(schema);
191
+ if (layout === void 0) {
192
+ layout = computeMemoryLayout(schema);
193
+ cachedLayouts.set(schema, layout);
194
+ }
195
+ return layout[key];
196
+ }
197
+ //#endregion
198
+ export { getLayoutInfo };
@@ -0,0 +1,9 @@
1
+ import { AnyData } from "./dataTypes.js";
2
+
3
+ //#region src/data/sizeOf.d.ts
4
+ /**
5
+ * Returns the size (in bytes) of data represented by the `schema`.
6
+ */
7
+ declare function PUBLIC_sizeOf(schema: AnyData): number;
8
+ //#endregion
9
+ export { PUBLIC_sizeOf };
package/data/sizeOf.js ADDED
@@ -0,0 +1,13 @@
1
+ import { getLayoutInfo } from "./schemaMemoryLayout.js";
2
+ //#region src/data/sizeOf.ts
3
+ function sizeOf(schema) {
4
+ return getLayoutInfo(schema, "size");
5
+ }
6
+ /**
7
+ * Returns the size (in bytes) of data represented by the `schema`.
8
+ */
9
+ function PUBLIC_sizeOf(schema) {
10
+ return sizeOf(schema);
11
+ }
12
+ //#endregion
13
+ export { PUBLIC_sizeOf, sizeOf };
@@ -0,0 +1,26 @@
1
+ import { BaseData } from "./wgslTypes.js";
2
+ import { UnknownData } from "./dataTypes.js";
3
+
4
+ //#region src/data/snippet.d.ts
5
+ type Origin = 'uniform' | 'readonly' | 'mutable' | 'workgroup' | 'private' | 'function' | 'this-function' | 'handle' | 'argument' | 'runtime' | 'constant' | 'constant-tgpu-const-ref' | 'runtime-tgpu-const-ref';
6
+ interface Snippet {
7
+ readonly value: unknown;
8
+ /**
9
+ * The type that `value` is assignable to (not necessary exactly inferred as).
10
+ * E.g. `1.1` is assignable to `f32`, but `1.1` itself is an abstract float
11
+ */
12
+ readonly dataType: BaseData | UnknownData;
13
+ readonly origin: Origin;
14
+ }
15
+ interface ResolvedSnippet {
16
+ readonly value: string;
17
+ /**
18
+ * The type that `value` is assignable to (not necessary exactly inferred as).
19
+ * E.g. `1.1` is assignable to `f32`, but `1.1` itself is an abstract float
20
+ */
21
+ readonly dataType: BaseData;
22
+ readonly origin: Origin;
23
+ }
24
+ type MapValueToSnippet<T> = { [K in keyof T]: Snippet };
25
+ //#endregion
26
+ export { MapValueToSnippet, Origin, ResolvedSnippet, Snippet };
@@ -0,0 +1,70 @@
1
+ import { DEV } from "../shared/env.js";
2
+ import { isNumericSchema } from "./wgslTypes.js";
3
+ import { undecorate } from "./dataTypes.js";
4
+ //#region src/data/snippet.ts
5
+ function isEphemeralOrigin(space) {
6
+ return space === "runtime" || space === "constant" || space === "argument";
7
+ }
8
+ /**
9
+ * What happens to a snippet's origin when it's deep copied in JS, and left as is in WGSL?
10
+ * e.g. `vec3f(vec3f(0, 1, 2))`
11
+ */
12
+ function fallthroughCopyOrigin(origin) {
13
+ if (origin === "runtime" || origin === "constant") return origin;
14
+ return "runtime";
15
+ }
16
+ function isEphemeralSnippet(snippet) {
17
+ return isEphemeralOrigin(snippet.origin);
18
+ }
19
+ const originToPtrParams = {
20
+ uniform: {
21
+ space: "uniform",
22
+ access: "read"
23
+ },
24
+ readonly: {
25
+ space: "storage",
26
+ access: "read"
27
+ },
28
+ mutable: {
29
+ space: "storage",
30
+ access: "read-write"
31
+ },
32
+ workgroup: {
33
+ space: "workgroup",
34
+ access: "read-write"
35
+ },
36
+ private: {
37
+ space: "private",
38
+ access: "read-write"
39
+ },
40
+ function: {
41
+ space: "function",
42
+ access: "read-write"
43
+ },
44
+ "this-function": {
45
+ space: "function",
46
+ access: "read-write"
47
+ }
48
+ };
49
+ var SnippetImpl = class {
50
+ value;
51
+ dataType;
52
+ origin;
53
+ constructor(value, dataType, origin) {
54
+ this.value = value;
55
+ this.dataType = dataType;
56
+ this.origin = origin;
57
+ }
58
+ };
59
+ function isSnippet(value) {
60
+ return value instanceof SnippetImpl;
61
+ }
62
+ function isSnippetNumeric(snippet) {
63
+ return isNumericSchema(snippet.dataType);
64
+ }
65
+ function snip(value, dataType, origin) {
66
+ if (DEV && isSnippet(value)) throw new Error("Cannot nest snippets");
67
+ return new SnippetImpl(value, undecorate(dataType), origin);
68
+ }
69
+ //#endregion
70
+ export { fallthroughCopyOrigin, isEphemeralOrigin, isEphemeralSnippet, isSnippet, isSnippetNumeric, originToPtrParams, snip };
@@ -0,0 +1,17 @@
1
+ import { AnyWgslData, WgslStruct } from "./wgslTypes.js";
2
+
3
+ //#region src/data/struct.d.ts
4
+ /**
5
+ * Creates a struct schema that can be used to construct GPU buffers.
6
+ * Ensures proper alignment and padding of properties (as opposed to a `d.unstruct` schema).
7
+ * The order of members matches the passed in properties object.
8
+ *
9
+ * @example
10
+ * const CircleStruct = d.struct({ radius: d.f32, pos: d.vec3f });
11
+ *
12
+ * @param props Record with `string` keys and `TgpuData` values,
13
+ * each entry describing one struct member.
14
+ */
15
+ declare function struct<TProps extends Record<string, AnyWgslData>>(props: TProps): WgslStruct<TProps>;
16
+ //#endregion
17
+ export { struct };
package/data/struct.js ADDED
@@ -0,0 +1,44 @@
1
+ import { $internal } from "../shared/symbols.js";
2
+ import { getName, setName } from "../shared/meta.js";
3
+ import { schemaCallWrapper } from "./schemaCallWrapper.js";
4
+ import { isValidProp } from "../nameRegistry.js";
5
+ //#region src/data/struct.ts
6
+ /**
7
+ * Creates a struct schema that can be used to construct GPU buffers.
8
+ * Ensures proper alignment and padding of properties (as opposed to a `d.unstruct` schema).
9
+ * The order of members matches the passed in properties object.
10
+ *
11
+ * @example
12
+ * const CircleStruct = d.struct({ radius: d.f32, pos: d.vec3f });
13
+ *
14
+ * @param props Record with `string` keys and `TgpuData` values,
15
+ * each entry describing one struct member.
16
+ */
17
+ function struct(props) {
18
+ return INTERNAL_createStruct(props, false);
19
+ }
20
+ function abstruct(props) {
21
+ return INTERNAL_createStruct(props, true);
22
+ }
23
+ function INTERNAL_createStruct(props, isAbstruct) {
24
+ Object.keys(props).forEach((key) => {
25
+ if (!isValidProp(key)) throw new Error(`Property key '${key}' is a reserved WGSL word. Choose a different name.`);
26
+ });
27
+ const structSchema = (instanceProps) => Object.fromEntries(Object.entries(props).map(([key, schema]) => [key, schemaCallWrapper(schema, instanceProps?.[key])]));
28
+ Object.setPrototypeOf(structSchema, WgslStructImpl);
29
+ structSchema.propTypes = props;
30
+ Object.defineProperty(structSchema, $internal, { value: { isAbstruct } });
31
+ return structSchema;
32
+ }
33
+ const WgslStructImpl = {
34
+ type: "struct",
35
+ $name(label) {
36
+ setName(this, label);
37
+ return this;
38
+ },
39
+ toString() {
40
+ return `struct:${getName(this) ?? "<unnamed>"}`;
41
+ }
42
+ };
43
+ //#endregion
44
+ export { INTERNAL_createStruct, abstruct, struct };