typegpu 0.10.0 → 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-Dg5ybJro.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-ClEnM-Ye.js +0 -818
  244. package/builtin-ClEnM-Ye.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-yZXvaV2C.js +0 -413
  250. package/deepEqual-yZXvaV2C.js.map +0 -1
  251. package/extensions-0SFbU9FH.js +0 -2032
  252. package/extensions-0SFbU9FH.js.map +0 -1
  253. package/fullScreenTriangle-MdLGaAMR.js +0 -543
  254. package/fullScreenTriangle-MdLGaAMR.js.map +0 -1
  255. package/index.d.ts.map +0 -1
  256. package/index.js.map +0 -1
  257. package/indexNamedExports-Cdy7USiY.d.ts +0 -5696
  258. package/indexNamedExports-Cdy7USiY.d.ts.map +0 -1
  259. package/operators-HTxa_0k9.js +0 -4156
  260. package/operators-HTxa_0k9.js.map +0 -1
  261. package/std/index.d.ts.map +0 -1
  262. package/std/index.js.map +0 -1
  263. package/texture-Dg5ybJro.js.map +0 -1
package/data/ref.js ADDED
@@ -0,0 +1,96 @@
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
+
10
+ //#region src/data/ref.ts
11
+ const _ref = (() => {
12
+ const impl = ((value) => INTERNAL_createRef(value));
13
+ setName(impl, "ref");
14
+ impl.toString = () => "ref";
15
+ impl[$internal] = true;
16
+ impl[$gpuCallable] = { call(_ctx, [value]) {
17
+ 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.`);
18
+ if (isPtr(value.dataType)) return snip(value.value, explicitFrom(value.dataType), value.origin);
19
+ /**
20
+ * Pointer type only exists if the ref was created from a reference (buttery-butter).
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * const life = ref(42); // created from a value
25
+ * const boid = ref(layout.$.boids[0]); // created from a reference
26
+ * ```
27
+ */
28
+ const ptrType = createPtrFromOrigin(value.origin, value.dataType);
29
+ return snip(new RefOperator(value, ptrType), ptrType ?? UnknownData, "runtime");
30
+ } };
31
+ return impl;
32
+ })();
33
+ function isRef(value) {
34
+ return value?.[$internal]?.type === "ref";
35
+ }
36
+ function INTERNAL_createRef(value) {
37
+ const target = {
38
+ [$internal]: { type: "ref" },
39
+ get $() {
40
+ return value;
41
+ },
42
+ set $(newValue) {
43
+ if (newValue && typeof newValue === "object") for (const key of Object.keys(newValue)) value[key] = newValue[key];
44
+ else value = newValue;
45
+ }
46
+ };
47
+ if (value === void 0 || value === null) throw new Error("Cannot create a ref from undefined or null");
48
+ if (typeof value === "object") return new Proxy(target, {
49
+ get(target, prop) {
50
+ if (prop in target) return target[prop];
51
+ return value[prop];
52
+ },
53
+ set(_target, prop, propValue) {
54
+ if (prop === $internal) return false;
55
+ if (prop === "$") {
56
+ console.log("Setting ref value:", propValue);
57
+ return Reflect.set(target, prop, propValue);
58
+ }
59
+ return Reflect.set(value, prop, propValue);
60
+ }
61
+ });
62
+ return target;
63
+ }
64
+ /**
65
+ * The result of calling `d.ref(...)`. The code responsible for
66
+ * generating shader code can check if the value of a snippet is
67
+ * an instance of `RefOperator`, and act accordingly.
68
+ */
69
+ var RefOperator = class {
70
+ [$internal];
71
+ snippet;
72
+ #ptrType;
73
+ constructor(snippet, ptrType) {
74
+ this[$internal] = true;
75
+ this.snippet = snippet;
76
+ this.#ptrType = ptrType;
77
+ }
78
+ get [$ownSnippet]() {
79
+ if (!this.#ptrType) throw new Error(stitch`Cannot take a reference of ${this.snippet}`);
80
+ return snip(this, this.#ptrType, this.snippet.origin);
81
+ }
82
+ [$resolve]() {
83
+ if (!this.#ptrType) throw new Error(stitch`Cannot take a reference of ${this.snippet}`);
84
+ return snip(stitch`(&${this.snippet})`, this.#ptrType, this.snippet.origin);
85
+ }
86
+ };
87
+ function derefSnippet(snippet) {
88
+ if (!isPtr(snippet.dataType)) return snippet;
89
+ const innerType = snippet.dataType.inner;
90
+ const origin = isNaturallyEphemeral(innerType) ? "runtime" : snippet.origin;
91
+ if (snippet.value instanceof RefOperator) return snip(stitch`${snippet.value.snippet}`, innerType, origin);
92
+ return snip(stitch`(*${snippet})`, innerType, origin);
93
+ }
94
+
95
+ //#endregion
96
+ 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,26 @@
1
+ import { $internal, $repr } from "../shared/symbols.js";
2
+
3
+ //#region src/data/sampler.ts
4
+ function sampler() {
5
+ return {
6
+ [$internal]: {},
7
+ type: "sampler",
8
+ [$repr]: void 0
9
+ };
10
+ }
11
+ function comparisonSampler() {
12
+ return {
13
+ [$internal]: {},
14
+ type: "sampler_comparison",
15
+ [$repr]: void 0
16
+ };
17
+ }
18
+ function isWgslSampler(value) {
19
+ return !!value[$internal] && value.type === "sampler";
20
+ }
21
+ function isWgslComparisonSampler(value) {
22
+ return !!value[$internal] && value.type === "sampler_comparison";
23
+ }
24
+
25
+ //#endregion
26
+ export { comparisonSampler, isWgslComparisonSampler, isWgslSampler, sampler };
@@ -0,0 +1,32 @@
1
+ import { $cast, $gpuCallable } from "../shared/symbols.js";
2
+ import { hasCast, isGPUCallable } from "../types.js";
3
+
4
+ //#region src/data/schemaCallWrapper.ts
5
+ /**
6
+ * A wrapper for `schema(item)` or `schema()` call on JS side.
7
+ * If the schema is a pointer, returns the value pointed to without copying.
8
+ * If the schema is a TgpuVertexFormatData, calls the corresponding constructible schema instead.
9
+ * If the schema is not callable, throws an error.
10
+ * Otherwise, returns `schema(item)` or `schema()`.
11
+ */
12
+ function schemaCallWrapper(schema, item) {
13
+ const callSchema = schema;
14
+ if (hasCast(callSchema)) return callSchema[$cast](item);
15
+ if (typeof callSchema !== "function") return item;
16
+ return item === void 0 ? callSchema() : callSchema(item);
17
+ }
18
+ /**
19
+ * A wrapper for `schema(item)` or `schema()` call on the GPU side.
20
+ * If the schema is a pointer, returns the value pointed to without copying.
21
+ * If the schema is a TgpuVertexFormatData, calls the corresponding constructible schema instead.
22
+ * If the schema is not callable, throws an error.
23
+ * Otherwise, returns `schema(item)` or `schema()`.
24
+ */
25
+ function schemaCallWrapperGPU(ctx, schema, item) {
26
+ if (!isGPUCallable(schema)) return item;
27
+ const callSchema = schema;
28
+ return item === void 0 || item.value === void 0 ? callSchema[$gpuCallable].call(ctx, []) : callSchema[$gpuCallable].call(ctx, [item]);
29
+ }
30
+
31
+ //#endregion
32
+ export { schemaCallWrapper, schemaCallWrapperGPU };
@@ -0,0 +1,200 @@
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
+
6
+ //#region src/data/schemaMemoryLayout.ts
7
+ const knownSizesMap = {
8
+ bool: 4,
9
+ f32: 4,
10
+ f16: 2,
11
+ i32: 4,
12
+ u32: 4,
13
+ u16: 2,
14
+ vec2f: 8,
15
+ vec2h: 4,
16
+ vec2i: 8,
17
+ vec2u: 8,
18
+ "vec2<bool>": 8,
19
+ vec3f: 12,
20
+ vec3h: 6,
21
+ vec3i: 12,
22
+ vec3u: 12,
23
+ "vec3<bool>": 12,
24
+ vec4f: 16,
25
+ vec4h: 8,
26
+ vec4i: 16,
27
+ vec4u: 16,
28
+ "vec4<bool>": 16,
29
+ mat2x2f: 16,
30
+ mat3x3f: 48,
31
+ mat4x4f: 64,
32
+ uint8: 1,
33
+ uint8x2: 2,
34
+ uint8x4: 4,
35
+ sint8: 1,
36
+ sint8x2: 2,
37
+ sint8x4: 4,
38
+ unorm8: 1,
39
+ unorm8x2: 2,
40
+ unorm8x4: 4,
41
+ snorm8: 1,
42
+ snorm8x2: 2,
43
+ snorm8x4: 4,
44
+ uint16: 2,
45
+ uint16x2: 4,
46
+ uint16x4: 8,
47
+ sint16: 2,
48
+ sint16x2: 4,
49
+ sint16x4: 8,
50
+ unorm16: 2,
51
+ unorm16x2: 4,
52
+ unorm16x4: 8,
53
+ snorm16: 2,
54
+ snorm16x2: 4,
55
+ snorm16x4: 8,
56
+ float16: 2,
57
+ float16x2: 4,
58
+ float16x4: 8,
59
+ float32: 4,
60
+ float32x2: 8,
61
+ float32x3: 12,
62
+ float32x4: 16,
63
+ uint32: 4,
64
+ uint32x2: 8,
65
+ uint32x3: 12,
66
+ uint32x4: 16,
67
+ sint32: 4,
68
+ sint32x2: 8,
69
+ sint32x3: 12,
70
+ sint32x4: 16,
71
+ "unorm10-10-10-2": 4,
72
+ "unorm8x4-bgra": 4,
73
+ atomic: 4
74
+ };
75
+ function computeMLOfStruct(struct) {
76
+ let size = 0;
77
+ let longestContiguousPrefix = 0;
78
+ let isContiguous = true;
79
+ let prefixEnd = false;
80
+ for (const property of Object.values(struct.propTypes)) {
81
+ if (Number.isNaN(size)) throw new Error("Only the last property of a struct can be unbounded");
82
+ const prevSize = size;
83
+ size = roundUp(size, alignmentOf(property));
84
+ const hasPadding = prevSize !== size;
85
+ const propLayout = computeMemoryLayout(property);
86
+ size += propLayout.size;
87
+ if (Number.isNaN(size) && property.type !== "array") throw new Error("Cannot nest unbounded struct within another struct");
88
+ if (prefixEnd) continue;
89
+ if (!hasPadding && propLayout.isContiguous) longestContiguousPrefix += propLayout.size;
90
+ else {
91
+ prefixEnd = true;
92
+ isContiguous = false;
93
+ if (!hasPadding) longestContiguousPrefix += propLayout.longestContiguousPrefix;
94
+ }
95
+ }
96
+ const trueSize = roundUp(size, alignmentOf(struct));
97
+ return {
98
+ isContiguous: size === trueSize && isContiguous,
99
+ size: trueSize,
100
+ longestContiguousPrefix
101
+ };
102
+ }
103
+ function computeMLOfUnstruct(data) {
104
+ let size = 0;
105
+ let longestContiguousPrefix = 0;
106
+ let isContiguous = true;
107
+ let prefixEnd = false;
108
+ for (const property of Object.values(data.propTypes)) {
109
+ const alignment = customAlignmentOf(property);
110
+ const prevSize = size;
111
+ size = roundUp(size, alignment);
112
+ const hasPadding = prevSize !== size;
113
+ if (hasPadding) isContiguous = false;
114
+ const propLayout = computeMemoryLayout(property);
115
+ size += propLayout.size;
116
+ if (prefixEnd) continue;
117
+ if (!hasPadding && propLayout.isContiguous) longestContiguousPrefix += propLayout.size;
118
+ else {
119
+ prefixEnd = true;
120
+ isContiguous = false;
121
+ if (!hasPadding) longestContiguousPrefix += propLayout.longestContiguousPrefix;
122
+ }
123
+ }
124
+ return {
125
+ isContiguous,
126
+ size,
127
+ longestContiguousPrefix
128
+ };
129
+ }
130
+ function computeMLOfWgslArray(data) {
131
+ const elementType = data.elementType;
132
+ const elementMemoryLayout = computeMemoryLayout(elementType);
133
+ const elementSize = elementMemoryLayout.size;
134
+ const stride = roundUp(elementSize, alignmentOf(elementType));
135
+ const isContiguous = !(stride > elementSize) && elementMemoryLayout.isContiguous;
136
+ const size = data.elementCount === 0 ? NaN : data.elementCount * stride;
137
+ let longestContiguousPrefix;
138
+ if (isContiguous) longestContiguousPrefix = size;
139
+ else longestContiguousPrefix = elementMemoryLayout.longestContiguousPrefix;
140
+ return {
141
+ size,
142
+ isContiguous,
143
+ longestContiguousPrefix
144
+ };
145
+ }
146
+ function computeMLOfDisarray(data) {
147
+ const elementType = data.elementType;
148
+ const elementMemoryLayout = computeMemoryLayout(elementType);
149
+ const elementSize = elementMemoryLayout.size;
150
+ const stride = roundUp(elementSize, customAlignmentOf(elementType));
151
+ const isContiguous = !(stride > elementSize) && elementMemoryLayout.isContiguous;
152
+ const size = data.elementCount * stride;
153
+ let longestContiguousPrefix;
154
+ if (isContiguous) longestContiguousPrefix = size;
155
+ else longestContiguousPrefix = elementMemoryLayout.longestContiguousPrefix;
156
+ return {
157
+ size,
158
+ isContiguous,
159
+ longestContiguousPrefix
160
+ };
161
+ }
162
+ function computeMemoryLayout(data) {
163
+ const knownSize = knownSizesMap[data.type];
164
+ if (knownSize !== void 0) return {
165
+ isContiguous: data.type !== "mat3x3f",
166
+ size: knownSize,
167
+ longestContiguousPrefix: data.type === "mat3x3f" ? 12 : knownSize
168
+ };
169
+ if (isWgslStruct(data)) return computeMLOfStruct(data);
170
+ if (isUnstruct(data)) return computeMLOfUnstruct(data);
171
+ if (isWgslArray(data)) return computeMLOfWgslArray(data);
172
+ if (isDisarray(data)) return computeMLOfDisarray(data);
173
+ if (isDecorated(data) || isLooseDecorated(data)) {
174
+ const size = getCustomSize(data);
175
+ const undecoratedLayout = computeMemoryLayout(undecorate(data));
176
+ if (size) return {
177
+ isContiguous: size === undecoratedLayout.size && undecoratedLayout.isContiguous,
178
+ size,
179
+ longestContiguousPrefix: undecoratedLayout.longestContiguousPrefix
180
+ };
181
+ return computeMemoryLayout(data.inner);
182
+ }
183
+ throw new Error(`Cannot determine memory layout of data: ${data}`);
184
+ }
185
+ /**
186
+ * Since memory layout can be inferred from data types, they are not stored on them.
187
+ * Instead, this weak map acts as an extended property of those data types.
188
+ */
189
+ const cachedLayouts = /* @__PURE__ */ new WeakMap();
190
+ function getLayoutInfo(schema, key) {
191
+ let layout = cachedLayouts.get(schema);
192
+ if (layout === void 0) {
193
+ layout = computeMemoryLayout(schema);
194
+ cachedLayouts.set(schema, layout);
195
+ }
196
+ return layout[key];
197
+ }
198
+
199
+ //#endregion
200
+ export { getLayoutInfo };
@@ -0,0 +1,10 @@
1
+ import "./wgslTypes.js";
2
+ import { AnyData } from "./dataTypes.js";
3
+
4
+ //#region src/data/sizeOf.d.ts
5
+ /**
6
+ * Returns the size (in bytes) of data represented by the `schema`.
7
+ */
8
+ declare function PUBLIC_sizeOf(schema: AnyData): number;
9
+ //#endregion
10
+ export { PUBLIC_sizeOf };
package/data/sizeOf.js ADDED
@@ -0,0 +1,15 @@
1
+ import { getLayoutInfo } from "./schemaMemoryLayout.js";
2
+
3
+ //#region src/data/sizeOf.ts
4
+ function sizeOf(schema) {
5
+ return getLayoutInfo(schema, "size");
6
+ }
7
+ /**
8
+ * Returns the size (in bytes) of data represented by the `schema`.
9
+ */
10
+ function PUBLIC_sizeOf(schema) {
11
+ return sizeOf(schema);
12
+ }
13
+
14
+ //#endregion
15
+ 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,61 @@
1
+ import { DEV } from "../shared/env.js";
2
+ import { isNumericSchema } from "./wgslTypes.js";
3
+ import { undecorate } from "./dataTypes.js";
4
+
5
+ //#region src/data/snippet.ts
6
+ function isEphemeralOrigin(space) {
7
+ return space === "runtime" || space === "constant" || space === "argument";
8
+ }
9
+ function isEphemeralSnippet(snippet) {
10
+ return isEphemeralOrigin(snippet.origin);
11
+ }
12
+ const originToPtrParams = {
13
+ uniform: {
14
+ space: "uniform",
15
+ access: "read"
16
+ },
17
+ readonly: {
18
+ space: "storage",
19
+ access: "read"
20
+ },
21
+ mutable: {
22
+ space: "storage",
23
+ access: "read-write"
24
+ },
25
+ workgroup: {
26
+ space: "workgroup",
27
+ access: "read-write"
28
+ },
29
+ private: {
30
+ space: "private",
31
+ access: "read-write"
32
+ },
33
+ function: {
34
+ space: "function",
35
+ access: "read-write"
36
+ },
37
+ "this-function": {
38
+ space: "function",
39
+ access: "read-write"
40
+ }
41
+ };
42
+ var SnippetImpl = class {
43
+ constructor(value, dataType, origin) {
44
+ this.value = value;
45
+ this.dataType = dataType;
46
+ this.origin = origin;
47
+ }
48
+ };
49
+ function isSnippet(value) {
50
+ return value instanceof SnippetImpl;
51
+ }
52
+ function isSnippetNumeric(snippet) {
53
+ return isNumericSchema(snippet.dataType);
54
+ }
55
+ function snip(value, dataType, origin) {
56
+ if (DEV && isSnippet(value)) throw new Error("Cannot nest snippets");
57
+ return new SnippetImpl(value, undecorate(dataType), origin);
58
+ }
59
+
60
+ //#endregion
61
+ export { 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,46 @@
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
+
6
+ //#region src/data/struct.ts
7
+ /**
8
+ * Creates a struct schema that can be used to construct GPU buffers.
9
+ * Ensures proper alignment and padding of properties (as opposed to a `d.unstruct` schema).
10
+ * The order of members matches the passed in properties object.
11
+ *
12
+ * @example
13
+ * const CircleStruct = d.struct({ radius: d.f32, pos: d.vec3f });
14
+ *
15
+ * @param props Record with `string` keys and `TgpuData` values,
16
+ * each entry describing one struct member.
17
+ */
18
+ function struct(props) {
19
+ return INTERNAL_createStruct(props, false);
20
+ }
21
+ function abstruct(props) {
22
+ return INTERNAL_createStruct(props, true);
23
+ }
24
+ function INTERNAL_createStruct(props, isAbstruct) {
25
+ Object.keys(props).forEach((key) => {
26
+ if (!isValidProp(key)) throw new Error(`Property key '${key}' is a reserved WGSL word. Choose a different name.`);
27
+ });
28
+ const structSchema = (instanceProps) => Object.fromEntries(Object.entries(props).map(([key, schema]) => [key, schemaCallWrapper(schema, instanceProps?.[key])]));
29
+ Object.setPrototypeOf(structSchema, WgslStructImpl);
30
+ structSchema.propTypes = props;
31
+ Object.defineProperty(structSchema, $internal, { value: { isAbstruct } });
32
+ return structSchema;
33
+ }
34
+ const WgslStructImpl = {
35
+ type: "struct",
36
+ $name(label) {
37
+ setName(this, label);
38
+ return this;
39
+ },
40
+ toString() {
41
+ return `struct:${getName(this) ?? "<unnamed>"}`;
42
+ }
43
+ };
44
+
45
+ //#endregion
46
+ export { INTERNAL_createStruct, abstruct, struct };