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,103 @@
1
+ import { $internal } from "../../shared/symbols.js";
2
+ import { setName } from "../../shared/meta.js";
3
+ import { kindToDefaultFormatMap, vertexFormats } from "../../shared/vertexFormat.js";
4
+ import { isDecorated, isWgslStruct } from "../../data/wgslTypes.js";
5
+ import { getCustomLocation, isLooseDecorated, isUnstruct } from "../../data/dataTypes.js";
6
+ import { alignmentOf, customAlignmentOf } from "../../data/alignmentOf.js";
7
+ import { roundUp } from "../../mathUtils.js";
8
+ import { sizeOf } from "../../data/sizeOf.js";
9
+
10
+ //#region src/core/vertexLayout/vertexLayout.ts
11
+ function vertexLayout(schemaForCount, stepMode = "vertex") {
12
+ return new TgpuVertexLayoutImpl(schemaForCount, stepMode);
13
+ }
14
+ function isVertexLayout(value) {
15
+ return value?.resourceType === "vertex-layout";
16
+ }
17
+ const defaultAttribEntry = Symbol("defaultAttribEntry");
18
+ function dataToContainedAttribs(layout, data, offset, customLocationMap, key) {
19
+ if (isDecorated(data) || isLooseDecorated(data)) {
20
+ const customLocation = getCustomLocation(data);
21
+ if (customLocation !== void 0) customLocationMap[key ?? defaultAttribEntry] = customLocation;
22
+ return dataToContainedAttribs(layout, data.inner, roundUp(offset, customAlignmentOf(data)), customLocationMap);
23
+ }
24
+ if (isWgslStruct(data)) {
25
+ let memberOffset = offset;
26
+ const propTypes = data.propTypes;
27
+ return Object.fromEntries(Object.entries(propTypes).map(([key, value]) => {
28
+ memberOffset = roundUp(memberOffset, alignmentOf(value));
29
+ const attrib = [key, dataToContainedAttribs(layout, value, memberOffset, customLocationMap, key)];
30
+ memberOffset += sizeOf(value);
31
+ return attrib;
32
+ }));
33
+ }
34
+ if (isUnstruct(data)) {
35
+ let memberOffset = offset;
36
+ const propTypes = data.propTypes;
37
+ return Object.fromEntries(Object.entries(propTypes).map(([key, value]) => {
38
+ memberOffset = roundUp(memberOffset, customAlignmentOf(value));
39
+ const attrib = [key, dataToContainedAttribs(layout, value, memberOffset, customLocationMap, key)];
40
+ memberOffset += sizeOf(value);
41
+ return attrib;
42
+ }));
43
+ }
44
+ if ("type" in data && typeof data.type === "string") {
45
+ if (vertexFormats.includes(data.type)) return {
46
+ _layout: layout,
47
+ format: data.type,
48
+ offset
49
+ };
50
+ const format = kindToDefaultFormatMap[data.type];
51
+ if (format) return {
52
+ _layout: layout,
53
+ format,
54
+ offset
55
+ };
56
+ }
57
+ throw new Error(`Unsupported data used in vertex layout: ${String(data)}`);
58
+ }
59
+ var TgpuVertexLayoutImpl = class {
60
+ [$internal] = true;
61
+ resourceType = "vertex-layout";
62
+ stride;
63
+ attrib;
64
+ _customLocationMap = {};
65
+ constructor(schemaForCount, stepMode) {
66
+ this.schemaForCount = schemaForCount;
67
+ this.stepMode = stepMode;
68
+ const arraySchema = schemaForCount(0);
69
+ this.stride = roundUp(sizeOf(arraySchema.elementType), alignmentOf(arraySchema));
70
+ this.attrib = dataToContainedAttribs(this, arraySchema.elementType, 0, this._customLocationMap);
71
+ }
72
+ get vertexLayout() {
73
+ if (this._customLocationMap[defaultAttribEntry] !== void 0) {
74
+ if (typeof this.attrib.format !== "string" || typeof this.attrib.offset !== "number") throw new Error("Single attribute vertex layouts must have a format and offset.");
75
+ return {
76
+ arrayStride: this.stride,
77
+ stepMode: this.stepMode,
78
+ attributes: [{
79
+ format: this.attrib.format,
80
+ offset: this.attrib.offset,
81
+ shaderLocation: this._customLocationMap[defaultAttribEntry]
82
+ }]
83
+ };
84
+ }
85
+ if (!Object.keys(this.attrib).every((key) => this._customLocationMap[key] !== void 0)) throw new Error("All attributes must have custom locations in order to unwrap a vertex layout.");
86
+ return {
87
+ arrayStride: this.stride,
88
+ stepMode: this.stepMode,
89
+ attributes: Object.entries(this.attrib).map(([key, attrib]) => ({
90
+ format: attrib.format,
91
+ offset: attrib.offset,
92
+ shaderLocation: this._customLocationMap[key]
93
+ }))
94
+ };
95
+ }
96
+ $name(label) {
97
+ setName(this, label);
98
+ return this;
99
+ }
100
+ };
101
+
102
+ //#endregion
103
+ export { isVertexLayout, vertexLayout };
@@ -0,0 +1,15 @@
1
+ //#region src/data/alignIO.ts
2
+ /**
3
+ * @param io the IO to align
4
+ * @param baseAlignment must be power of 2
5
+ */
6
+ function alignIO(io, baseAlignment) {
7
+ const currentPos = "size" in io ? io.size : io.currentByteOffset;
8
+ const bitMask = baseAlignment - 1;
9
+ const offset = currentPos & bitMask;
10
+ if ("skipBytes" in io) io.skipBytes(baseAlignment - offset & bitMask);
11
+ else io.add(baseAlignment - offset & bitMask);
12
+ }
13
+
14
+ //#endregion
15
+ export { alignIO as default };
@@ -0,0 +1,10 @@
1
+ import "./wgslTypes.js";
2
+ import { AnyData } from "./dataTypes.js";
3
+
4
+ //#region src/data/alignmentOf.d.ts
5
+ /**
6
+ * Returns the alignment (in bytes) of data represented by the `schema`.
7
+ */
8
+ declare function PUBLIC_alignmentOf(schema: AnyData): number;
9
+ //#endregion
10
+ export { PUBLIC_alignmentOf };
@@ -0,0 +1,88 @@
1
+ import { isDecorated, isWgslArray, isWgslStruct } from "./wgslTypes.js";
2
+ import { getCustomAlignment, isDisarray, isLooseDecorated, isUnstruct } from "./dataTypes.js";
3
+ import { safeStringify } from "../shared/stringify.js";
4
+ import { packedFormats } from "./vertexFormatData.js";
5
+
6
+ //#region src/data/alignmentOf.ts
7
+ const knownAlignmentMap = {
8
+ f32: 4,
9
+ f16: 2,
10
+ i32: 4,
11
+ u32: 4,
12
+ bool: 4,
13
+ u16: 2,
14
+ vec2f: 8,
15
+ vec2h: 4,
16
+ vec2i: 8,
17
+ vec2u: 8,
18
+ "vec2<bool>": 8,
19
+ vec3f: 16,
20
+ vec3h: 8,
21
+ vec3i: 16,
22
+ vec3u: 16,
23
+ "vec3<bool>": 16,
24
+ vec4f: 16,
25
+ vec4h: 8,
26
+ vec4i: 16,
27
+ vec4u: 16,
28
+ "vec4<bool>": 16,
29
+ mat2x2f: 8,
30
+ mat3x3f: 16,
31
+ mat4x4f: 16,
32
+ atomic: 4
33
+ };
34
+ function computeAlignment(data) {
35
+ const dataType = data?.type;
36
+ const knownAlignment = knownAlignmentMap[dataType];
37
+ if (knownAlignment !== void 0) return knownAlignment;
38
+ if (isWgslStruct(data)) return Object.values(data.propTypes).map(alignmentOf).reduce((a, b) => a > b ? a : b);
39
+ if (isWgslArray(data)) return alignmentOf(data.elementType);
40
+ if (isUnstruct(data)) {
41
+ const firstProp = Object.values(data.propTypes)[0];
42
+ return firstProp ? getCustomAlignment(firstProp) ?? 1 : 1;
43
+ }
44
+ if (isDisarray(data)) return getCustomAlignment(data.elementType) ?? 1;
45
+ if (isDecorated(data) || isLooseDecorated(data)) return getCustomAlignment(data) ?? alignmentOf(data.inner);
46
+ if (packedFormats.has(dataType)) return 1;
47
+ throw new Error(`Cannot determine alignment of data: ${safeStringify(data)}`);
48
+ }
49
+ function computeCustomAlignment(data) {
50
+ if (isUnstruct(data)) {
51
+ const firstProp = Object.values(data.propTypes)[0];
52
+ return firstProp ? customAlignmentOf(firstProp) : 1;
53
+ }
54
+ if (isDisarray(data)) return customAlignmentOf(data.elementType);
55
+ if (isLooseDecorated(data)) return getCustomAlignment(data) ?? customAlignmentOf(data.inner);
56
+ return getCustomAlignment(data) ?? 1;
57
+ }
58
+ /**
59
+ * Since alignments can be inferred from data types, they are not stored on them.
60
+ * Instead, this weak map acts as an extended property of those data types.
61
+ */
62
+ const cachedAlignments = /* @__PURE__ */ new WeakMap();
63
+ const cachedCustomAlignments = /* @__PURE__ */ new WeakMap();
64
+ function alignmentOf(data) {
65
+ let alignment = cachedAlignments.get(data);
66
+ if (alignment === void 0) {
67
+ alignment = computeAlignment(data);
68
+ cachedAlignments.set(data, alignment);
69
+ }
70
+ return alignment;
71
+ }
72
+ function customAlignmentOf(data) {
73
+ let alignment = cachedCustomAlignments.get(data);
74
+ if (alignment === void 0) {
75
+ alignment = computeCustomAlignment(data);
76
+ cachedCustomAlignments.set(data, alignment);
77
+ }
78
+ return alignment;
79
+ }
80
+ /**
81
+ * Returns the alignment (in bytes) of data represented by the `schema`.
82
+ */
83
+ function PUBLIC_alignmentOf(schema) {
84
+ return alignmentOf(schema);
85
+ }
86
+
87
+ //#endregion
88
+ export { PUBLIC_alignmentOf, alignmentOf, customAlignmentOf };
@@ -0,0 +1,28 @@
1
+ import { AnyWgslData, WgslArray } from "./wgslTypes.js";
2
+ import { TgpuComptime } from "../core/function/comptime.js";
3
+ import "../indexNamedExports.js";
4
+
5
+ //#region src/data/array.d.ts
6
+ interface WgslArrayConstructor {
7
+ <TElement extends AnyWgslData>(elementType: TElement): (elementCount: number) => WgslArray<TElement>;
8
+ <TElement extends AnyWgslData>(elementType: TElement, elementCount: number): WgslArray<TElement>;
9
+ }
10
+ /**
11
+ * Creates an array schema that can be used to construct gpu buffers.
12
+ * Describes arrays with fixed-size length, storing elements of the same type.
13
+ *
14
+ * @example
15
+ * const LENGTH = 3;
16
+ * const array = d.arrayOf(d.u32, LENGTH);
17
+ *
18
+ * If `elementCount` is not specified, a partially applied function is returned.
19
+ * @example
20
+ * const array = d.arrayOf(d.vec3f);
21
+ * // ^? (n: number) => WgslArray<d.Vec3f>
22
+ *
23
+ * @param elementType The type of elements in the array.
24
+ * @param elementCount The number of elements in the array.
25
+ */
26
+ declare const arrayOf: TgpuComptime<WgslArrayConstructor>;
27
+ //#endregion
28
+ export { arrayOf };
package/data/array.js ADDED
@@ -0,0 +1,48 @@
1
+ import { $internal } from "../shared/symbols.js";
2
+ import { schemaCallWrapper } from "./schemaCallWrapper.js";
3
+ import { sizeOf } from "./sizeOf.js";
4
+ import { comptime } from "../core/function/comptime.js";
5
+
6
+ //#region src/data/array.ts
7
+ /**
8
+ * Creates an array schema that can be used to construct gpu buffers.
9
+ * Describes arrays with fixed-size length, storing elements of the same type.
10
+ *
11
+ * @example
12
+ * const LENGTH = 3;
13
+ * const array = d.arrayOf(d.u32, LENGTH);
14
+ *
15
+ * If `elementCount` is not specified, a partially applied function is returned.
16
+ * @example
17
+ * const array = d.arrayOf(d.vec3f);
18
+ * // ^? (n: number) => WgslArray<d.Vec3f>
19
+ *
20
+ * @param elementType The type of elements in the array.
21
+ * @param elementCount The number of elements in the array.
22
+ */
23
+ const arrayOf = comptime(((elementType, elementCount) => {
24
+ if (elementCount === void 0) return comptime((count) => cpu_arrayOf(elementType, count));
25
+ return cpu_arrayOf(elementType, elementCount);
26
+ })).$name("arrayOf");
27
+ function cpu_arrayOf(elementType, elementCount) {
28
+ const arraySchema = (elements) => {
29
+ if (elements && elements.length !== elementCount) throw new Error(`Array schema of ${elementCount} elements of type ${elementType.type} called with ${elements.length} argument(s).`);
30
+ return Array.from({ length: elementCount }, (_, i) => schemaCallWrapper(elementType, elements?.[i]));
31
+ };
32
+ Object.setPrototypeOf(arraySchema, WgslArrayImpl);
33
+ if (Number.isNaN(sizeOf(elementType))) throw new Error("Cannot nest runtime sized arrays.");
34
+ arraySchema.elementType = elementType;
35
+ if (!Number.isInteger(elementCount) || elementCount < 0) throw new Error(`Cannot create array schema with invalid element count: ${elementCount}.`);
36
+ arraySchema.elementCount = elementCount;
37
+ return arraySchema;
38
+ }
39
+ const WgslArrayImpl = {
40
+ [$internal]: true,
41
+ type: "array",
42
+ toString() {
43
+ return `arrayOf(${this.elementType}, ${this.elementCount})`;
44
+ }
45
+ };
46
+
47
+ //#endregion
48
+ export { arrayOf };
@@ -0,0 +1,15 @@
1
+ import { Atomic, I32, U32 } from "./wgslTypes.js";
2
+
3
+ //#region src/data/atomic.d.ts
4
+ /**
5
+ * Marks a concrete integer scalar type schema (u32 or i32) as a WGSL atomic.
6
+ *
7
+ * @example
8
+ * const atomicU32 = d.atomic(d.u32);
9
+ * const atomicI32 = d.atomic(d.i32);
10
+ *
11
+ * @param data Underlying type schema.
12
+ */
13
+ declare function atomic<TSchema extends U32 | I32>(data: TSchema): Atomic<TSchema>;
14
+ //#endregion
15
+ export { atomic };
package/data/atomic.js ADDED
@@ -0,0 +1,25 @@
1
+ import { $internal } from "../shared/symbols.js";
2
+
3
+ //#region src/data/atomic.ts
4
+ /**
5
+ * Marks a concrete integer scalar type schema (u32 or i32) as a WGSL atomic.
6
+ *
7
+ * @example
8
+ * const atomicU32 = d.atomic(d.u32);
9
+ * const atomicI32 = d.atomic(d.i32);
10
+ *
11
+ * @param data Underlying type schema.
12
+ */
13
+ function atomic(data) {
14
+ return new AtomicImpl(data);
15
+ }
16
+ var AtomicImpl = class {
17
+ [$internal] = {};
18
+ type = "atomic";
19
+ constructor(inner) {
20
+ this.inner = inner;
21
+ }
22
+ };
23
+
24
+ //#endregion
25
+ export { atomic };
@@ -0,0 +1,121 @@
1
+ import { Align, AnyWgslData, BaseData, Builtin, Decorated, FlatInterpolatableData, FlatInterpolationType, Interpolate, Invariant, IsWgslData, Location, PerspectiveOrLinearInterpolatableData, PerspectiveOrLinearInterpolationType, Size, Vec4f } from "./wgslTypes.js";
2
+ import { AnyData, AnyLooseData, IsLooseData, LooseDecorated, Undecorate } from "./dataTypes.js";
3
+
4
+ //#region src/data/attributes.d.ts
5
+ declare const builtinNames: readonly ["vertex_index", "instance_index", "clip_distances", "position", "front_facing", "frag_depth", "primitive_index", "sample_index", "sample_mask", "fragment", "local_invocation_id", "local_invocation_index", "global_invocation_id", "workgroup_id", "num_workgroups", "subgroup_invocation_id", "subgroup_size", "subgroup_id", "num_subgroups"];
6
+ type BuiltinName = (typeof builtinNames)[number];
7
+ type AnyAttribute<AllowedBuiltins extends Builtin<BuiltinName> = Builtin<BuiltinName>> = Align<number> | Size<number> | Location | Interpolate | Invariant | AllowedBuiltins;
8
+ type ExtractAttributes<T> = T extends {
9
+ readonly attribs: unknown[];
10
+ } ? T['attribs'] : [];
11
+ /**
12
+ * Decorates a data-type `TData` with an attribute `TAttrib`.
13
+ *
14
+ * - if `TData` is loose
15
+ * - if `TData` is already `LooseDecorated`
16
+ * - Prepend `TAttrib` to the existing attribute tuple.
17
+ * - else
18
+ * - Wrap `TData` with `LooseDecorated` and a single attribute `[TAttrib]`
19
+ * - else
20
+ * - if `TData` is already `Decorated`
21
+ * - Prepend `TAttrib` to the existing attribute tuple.
22
+ * - else
23
+ * - Wrap `TData` with `Decorated` and a single attribute `[TAttrib]`
24
+ */
25
+ type Decorate<TData extends BaseData, TAttrib extends AnyAttribute> = IsWgslData<TData> extends true ? Decorated<Undecorate<TData>, [TAttrib, ...ExtractAttributes<TData>]> : IsLooseData<TData> extends true ? LooseDecorated<Undecorate<TData>, [TAttrib, ...ExtractAttributes<TData>]> : never;
26
+ type IsBuiltin<T> = ExtractAttributes<T>[number] extends [] ? false : ExtractAttributes<T>[number] extends Builtin<BuiltinName> ? true : false;
27
+ type HasCustomLocation<T> = ExtractAttributes<T>[number] extends [] ? false : ExtractAttributes<T>[number] extends Location ? true : false;
28
+ /**
29
+ * Gives the wrapped data-type a custom byte alignment. Useful in order to
30
+ * fulfill uniform alignment requirements.
31
+ *
32
+ * @example
33
+ * const Data = d.struct({
34
+ * a: u32, // takes up 4 bytes
35
+ * // 12 bytes of padding, because `b` is custom aligned to multiples of 16 bytes
36
+ * b: d.align(16, u32),
37
+ * });
38
+ *
39
+ * @param alignment The multiple of bytes this data should align itself to.
40
+ * @param data The data-type to align.
41
+ */
42
+ declare function align<TAlign extends number, TData extends AnyData>(alignment: TAlign, data: TData): Decorate<TData, Align<TAlign>>;
43
+ /**
44
+ * Adds padding bytes after the wrapped data-type, until the whole value takes up `size` bytes.
45
+ *
46
+ * @example
47
+ * const Data = d.struct({
48
+ * a: d.size(16, u32), // takes up 16 bytes, instead of 4
49
+ * b: u32, // starts at byte 16, because `a` has a custom size
50
+ * });
51
+ *
52
+ * @param size The amount of bytes that should be reserved for this data-type.
53
+ * @param data The data-type to wrap.
54
+ */
55
+ declare function size<TSize extends number, TData extends AnyData>(size: TSize, data: TData): Decorate<TData, Size<TSize>>;
56
+ /**
57
+ * Assigns an explicit numeric location to a struct member or a parameter that has this type.
58
+ *
59
+ * @example
60
+ * const VertexOutput = {
61
+ * a: d.u32, // has implicit location 0
62
+ * b: d.location(5, d.u32),
63
+ * c: d.u32, // has implicit location 6
64
+ * };
65
+ *
66
+ * @param location The explicit numeric location.
67
+ * @param data The data-type to wrap.
68
+ */
69
+ declare function location<TLocation extends number, TData extends BaseData>(location: TLocation, data: TData): Decorate<TData, Location<TLocation>>;
70
+ /**
71
+ * Specifies how user-defined vertex shader output (fragment shader input)
72
+ * must be interpolated.
73
+ *
74
+ * Tip: Integer outputs cannot be interpolated.
75
+ *
76
+ * @example
77
+ * const VertexOutput = {
78
+ * a: d.f32, // has implicit 'perspective, center' interpolation
79
+ * b: d.interpolate('linear, sample', d.f32),
80
+ * };
81
+ *
82
+ * @param interpolationType How data should be interpolated.
83
+ * @param data The data-type to wrap.
84
+ */
85
+ declare function interpolate<TInterpolation extends PerspectiveOrLinearInterpolationType, TData extends PerspectiveOrLinearInterpolatableData>(interpolationType: TInterpolation, data: TData): Decorate<TData, Interpolate<TInterpolation>>;
86
+ /**
87
+ * Specifies how user-defined vertex shader output (fragment shader input)
88
+ * must be interpolated.
89
+ *
90
+ * Tip: Default sampling method of `flat` is `first`. Unless you specifically
91
+ * need deterministic behavior provided by `'flat, first'`, prefer explicit
92
+ * `'flat, either'` as it could be slightly faster in hardware.
93
+ *
94
+ * @example
95
+ * const VertexOutput = {
96
+ * a: d.f32, // has implicit 'perspective, center' interpolation
97
+ * b: d.interpolate('flat, either', d.u32), // integer outputs cannot interpolate
98
+ * };
99
+ *
100
+ * @param interpolationType How data should be interpolated.
101
+ * @param data The data-type to wrap.
102
+ */
103
+ declare function interpolate<TInterpolation extends FlatInterpolationType, TData extends FlatInterpolatableData>(interpolationType: TInterpolation, data: TData): Decorate<TData, Interpolate<TInterpolation>>;
104
+ /**
105
+ * Marks a position built-in output value as invariant in vertex shaders.
106
+ * If the data and control flow match for two position outputs in different
107
+ * entry points, then the result values are guaranteed to be the same.
108
+ *
109
+ * Must only be applied to the position built-in value.
110
+ *
111
+ * @example
112
+ * const VertexOutput = {
113
+ * pos: d.invariant(d.builtin.position),
114
+ * };
115
+ *
116
+ * @param data The position built-in data-type to mark as invariant.
117
+ */
118
+ declare function invariant(data: Decorated<Vec4f, [Builtin<'position'>]>): Decorated<Vec4f, [Builtin<'position'>, Invariant]>;
119
+ declare function isBuiltin(value: unknown): value is Decorated<AnyWgslData, AnyAttribute[]> | LooseDecorated<AnyLooseData, AnyAttribute[]>;
120
+ //#endregion
121
+ export { AnyAttribute, Decorate, HasCustomLocation, IsBuiltin, align, interpolate, invariant, isBuiltin, location, size };
@@ -0,0 +1,145 @@
1
+ import { $internal } from "../shared/symbols.js";
2
+ import { isAlignAttrib, isBuiltinAttrib, isDecorated, isSizeAttrib, isWgslData } from "./wgslTypes.js";
3
+ import { isLooseData, isLooseDecorated } from "./dataTypes.js";
4
+ import { alignmentOf } from "./alignmentOf.js";
5
+ import { sizeOf } from "./sizeOf.js";
6
+
7
+ //#region src/data/attributes.ts
8
+ function attribute(data, attrib) {
9
+ if (isDecorated(data)) return new DecoratedImpl(data.inner, [attrib, ...data.attribs]);
10
+ if (isLooseDecorated(data)) return new LooseDecoratedImpl(data.inner, [attrib, ...data.attribs]);
11
+ if (isLooseData(data)) return new LooseDecoratedImpl(data, [attrib]);
12
+ return new DecoratedImpl(data, [attrib]);
13
+ }
14
+ /**
15
+ * Gives the wrapped data-type a custom byte alignment. Useful in order to
16
+ * fulfill uniform alignment requirements.
17
+ *
18
+ * @example
19
+ * const Data = d.struct({
20
+ * a: u32, // takes up 4 bytes
21
+ * // 12 bytes of padding, because `b` is custom aligned to multiples of 16 bytes
22
+ * b: d.align(16, u32),
23
+ * });
24
+ *
25
+ * @param alignment The multiple of bytes this data should align itself to.
26
+ * @param data The data-type to align.
27
+ */
28
+ function align(alignment, data) {
29
+ return attribute(data, {
30
+ [$internal]: true,
31
+ type: "@align",
32
+ params: [alignment]
33
+ });
34
+ }
35
+ /**
36
+ * Adds padding bytes after the wrapped data-type, until the whole value takes up `size` bytes.
37
+ *
38
+ * @example
39
+ * const Data = d.struct({
40
+ * a: d.size(16, u32), // takes up 16 bytes, instead of 4
41
+ * b: u32, // starts at byte 16, because `a` has a custom size
42
+ * });
43
+ *
44
+ * @param size The amount of bytes that should be reserved for this data-type.
45
+ * @param data The data-type to wrap.
46
+ */
47
+ function size(size, data) {
48
+ return attribute(data, {
49
+ [$internal]: true,
50
+ type: "@size",
51
+ params: [size]
52
+ });
53
+ }
54
+ /**
55
+ * Assigns an explicit numeric location to a struct member or a parameter that has this type.
56
+ *
57
+ * @example
58
+ * const VertexOutput = {
59
+ * a: d.u32, // has implicit location 0
60
+ * b: d.location(5, d.u32),
61
+ * c: d.u32, // has implicit location 6
62
+ * };
63
+ *
64
+ * @param location The explicit numeric location.
65
+ * @param data The data-type to wrap.
66
+ */
67
+ function location(location, data) {
68
+ return attribute(data, {
69
+ [$internal]: true,
70
+ type: "@location",
71
+ params: [location]
72
+ });
73
+ }
74
+ function interpolate(interpolationType, data) {
75
+ return attribute(data, {
76
+ [$internal]: true,
77
+ type: "@interpolate",
78
+ params: [interpolationType]
79
+ });
80
+ }
81
+ /**
82
+ * Marks a position built-in output value as invariant in vertex shaders.
83
+ * If the data and control flow match for two position outputs in different
84
+ * entry points, then the result values are guaranteed to be the same.
85
+ *
86
+ * Must only be applied to the position built-in value.
87
+ *
88
+ * @example
89
+ * const VertexOutput = {
90
+ * pos: d.invariant(d.builtin.position),
91
+ * };
92
+ *
93
+ * @param data The position built-in data-type to mark as invariant.
94
+ */
95
+ function invariant(data) {
96
+ if (!isBuiltin(data)) throw new Error("The @invariant attribute must only be applied to the position built-in value.");
97
+ const builtinAttrib = isDecorated(data) || isLooseDecorated(data) ? data.attribs.find(isBuiltinAttrib) : void 0;
98
+ if (!builtinAttrib || builtinAttrib.params[0] !== "position") throw new Error("The @invariant attribute must only be applied to the position built-in value.");
99
+ return attribute(data, {
100
+ [$internal]: true,
101
+ type: "@invariant",
102
+ params: []
103
+ });
104
+ }
105
+ function isBuiltin(value) {
106
+ return (isDecorated(value) || isLooseDecorated(value)) && value.attribs.find(isBuiltinAttrib) !== void 0;
107
+ }
108
+ function getAttributesString(field) {
109
+ if (!isDecorated(field) && !isLooseDecorated(field)) return "";
110
+ return field.attribs.map((attrib) => {
111
+ if (attrib.params.length === 0) return `${attrib.type} `;
112
+ return `${attrib.type}(${attrib.params.join(", ")}) `;
113
+ }).join("");
114
+ }
115
+ var BaseDecoratedImpl = class {
116
+ [$internal] = {};
117
+ constructor(inner, attribs) {
118
+ this.inner = inner;
119
+ this.attribs = attribs;
120
+ const alignAttrib = attribs.find(isAlignAttrib)?.params[0];
121
+ const sizeAttrib = attribs.find(isSizeAttrib)?.params[0];
122
+ if (alignAttrib !== void 0) {
123
+ if (alignAttrib <= 0) throw new Error(`Custom data alignment must be a positive number, got: ${alignAttrib}.`);
124
+ if (Math.log2(alignAttrib) % 1 !== 0) throw new Error(`Alignment has to be a power of 2, got: ${alignAttrib}.`);
125
+ if (isWgslData(this.inner)) {
126
+ if (alignAttrib % alignmentOf(this.inner) !== 0) throw new Error(`Custom alignment has to be a multiple of the standard data alignment. Got: ${alignAttrib}, expected multiple of: ${alignmentOf(this.inner)}.`);
127
+ }
128
+ }
129
+ if (sizeAttrib !== void 0) {
130
+ if (sizeAttrib < sizeOf(this.inner)) throw new Error(`Custom data size cannot be smaller then the standard data size. Got: ${sizeAttrib}, expected at least: ${sizeOf(this.inner)}.`);
131
+ if (sizeAttrib <= 0) throw new Error(`Custom data size must be a positive number. Got: ${sizeAttrib}.`);
132
+ }
133
+ }
134
+ };
135
+ var DecoratedImpl = class extends BaseDecoratedImpl {
136
+ [$internal] = {};
137
+ type = "decorated";
138
+ };
139
+ var LooseDecoratedImpl = class extends BaseDecoratedImpl {
140
+ [$internal] = {};
141
+ type = "loose-decorated";
142
+ };
143
+
144
+ //#endregion
145
+ export { align, attribute, getAttributesString, interpolate, invariant, isBuiltin, location, size };
@@ -0,0 +1,3 @@
1
+ import "./snippet.js";
2
+ import "../types.js";
3
+ import "./wgslTypes.js";