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
@@ -0,0 +1,83 @@
1
+ import { $internal, $resolve } from "../shared/symbols.js";
2
+ import { getName, setName } from "../shared/meta.js";
3
+ import { isValidProp } from "../nameRegistry.js";
4
+ import { createIoSchema } from "../core/function/ioSchema.js";
5
+
6
+ //#region src/data/autoStruct.ts
7
+ /**
8
+ * A requirement for the generated struct is that non-builtin properties need to
9
+ * have the same name in WGSL as they do in JS. This allows locations to be properly
10
+ * matched between the Vertex output and Fragment input.
11
+ */
12
+ var AutoStruct = class {
13
+ /**
14
+ * js key -> data type
15
+ */
16
+ #validProps;
17
+ /**
18
+ * js key -> { prop: 'wgsl key', type: ... }
19
+ * @example '$position' -> { prop: 'position', type: ... }
20
+ */
21
+ #allocated;
22
+ #usedWgslKeys;
23
+ #locations;
24
+ #cachedStruct;
25
+ #typeForExtraProps;
26
+ constructor(validProps, typeForExtraProps, locations) {
27
+ this.#validProps = validProps;
28
+ this.#typeForExtraProps = typeForExtraProps;
29
+ this.#allocated = {};
30
+ this.#locations = locations;
31
+ this.#usedWgslKeys = /* @__PURE__ */ new Set();
32
+ }
33
+ /**
34
+ * Used for accessing builtins, varying and attributes in code.
35
+ */
36
+ accessProp(key) {
37
+ const dataType = this.#validProps[key] ?? this.#typeForExtraProps;
38
+ if (!dataType) return;
39
+ return this.provideProp(key, dataType);
40
+ }
41
+ /**
42
+ * Used for providing new varyings.
43
+ *
44
+ * @privateRemarks
45
+ * Internally used by `accessProp`.
46
+ */
47
+ provideProp(key, dataType) {
48
+ let alloc = this.#allocated[key];
49
+ if (!alloc) {
50
+ const wgslKey = key.replaceAll("$", "");
51
+ if (this.#usedWgslKeys.has(wgslKey)) throw new Error(`Property name '${wgslKey}' causes naming clashes. Choose a different name.`);
52
+ if (!isValidProp(wgslKey)) throw new Error(`Property key '${key}' is a reserved WGSL word. Choose a different name.`);
53
+ this.#usedWgslKeys.add(wgslKey);
54
+ alloc = {
55
+ prop: wgslKey,
56
+ type: dataType
57
+ };
58
+ this.#allocated[key] = alloc;
59
+ }
60
+ return alloc;
61
+ }
62
+ get completeStruct() {
63
+ if (!this.#cachedStruct) {
64
+ this.#cachedStruct = createIoSchema(Object.fromEntries(Object.values(this.#allocated).map((alloc) => {
65
+ return [alloc.prop, alloc.type];
66
+ })), this.#locations);
67
+ const ownName = getName(this);
68
+ if (ownName) setName(this.#cachedStruct, ownName);
69
+ }
70
+ return this.#cachedStruct;
71
+ }
72
+ [$resolve](ctx) {
73
+ return ctx.resolve(this.completeStruct);
74
+ }
75
+ toString() {
76
+ return `auto-struct:${getName(this) ?? "<unnamed>"}`;
77
+ }
78
+ };
79
+ AutoStruct.prototype[$internal] = {};
80
+ AutoStruct.prototype.type = "auto-struct";
81
+
82
+ //#endregion
83
+ export { AutoStruct };
@@ -0,0 +1,231 @@
1
+ import { isAtomic, isDecorated, isMat, isMat2x2f, isMat3x3f, isVec, isVecBool, isWgslArray, isWgslStruct } from "./wgslTypes.js";
2
+ import { isDisarray, isUnstruct } from "./dataTypes.js";
3
+ import { formatToWGSLType, isPackedData } from "./vertexFormatData.js";
4
+ import { alignmentOf } from "./alignmentOf.js";
5
+ import { roundUp } from "../mathUtils.js";
6
+ import { sizeOf } from "./sizeOf.js";
7
+ import { offsetsForProps } from "./offsets.js";
8
+
9
+ //#region src/data/compiledIO.ts
10
+ const EVAL_ALLOWED_IN_ENV = (() => {
11
+ try {
12
+ new Function("return true");
13
+ return true;
14
+ } catch {
15
+ return false;
16
+ }
17
+ })();
18
+ const compiledWriters = /* @__PURE__ */ new WeakMap();
19
+ const typeToPrimitive = {
20
+ u32: "u32",
21
+ vec2u: "u32",
22
+ vec3u: "u32",
23
+ vec4u: "u32",
24
+ u16: "u16",
25
+ i32: "i32",
26
+ vec2i: "i32",
27
+ vec3i: "i32",
28
+ vec4i: "i32",
29
+ f32: "f32",
30
+ vec2f: "f32",
31
+ vec3f: "f32",
32
+ vec4f: "f32",
33
+ f16: "f16",
34
+ vec2h: "f16",
35
+ vec3h: "f16",
36
+ vec4h: "f16",
37
+ mat2x2f: "f32",
38
+ mat3x3f: "f32",
39
+ mat4x4f: "f32"
40
+ };
41
+ const vertexFormatToPrimitive = {
42
+ uint8: "u8",
43
+ uint8x2: "u8",
44
+ uint8x4: "u8",
45
+ sint8: "i8",
46
+ sint8x2: "i8",
47
+ sint8x4: "i8",
48
+ unorm8: "u8",
49
+ unorm8x2: "u8",
50
+ unorm8x4: "u8",
51
+ snorm8: "i8",
52
+ snorm8x2: "i8",
53
+ snorm8x4: "i8",
54
+ uint16: "u16",
55
+ uint16x2: "u16",
56
+ uint16x4: "u16",
57
+ sint16: "i16",
58
+ sint16x2: "i16",
59
+ sint16x4: "i16",
60
+ unorm16: "u16",
61
+ unorm16x2: "u16",
62
+ unorm16x4: "u16",
63
+ snorm16: "i16",
64
+ snorm16x2: "i16",
65
+ snorm16x4: "i16",
66
+ float16: "f16",
67
+ float16x2: "f16",
68
+ float16x4: "f16",
69
+ float32: "f32",
70
+ float32x2: "f32",
71
+ float32x3: "f32",
72
+ float32x4: "f32",
73
+ uint32: "u32",
74
+ uint32x2: "u32",
75
+ uint32x3: "u32",
76
+ uint32x4: "u32",
77
+ sint32: "i32",
78
+ sint32x2: "i32",
79
+ sint32x3: "i32",
80
+ sint32x4: "i32"
81
+ };
82
+ const primitiveToWriteFunction = {
83
+ u32: "setUint32",
84
+ i32: "setInt32",
85
+ f32: "setFloat32",
86
+ u16: "setUint16",
87
+ i16: "setInt16",
88
+ f16: "setFloat16",
89
+ u8: "setUint8",
90
+ i8: "setInt8"
91
+ };
92
+ /**
93
+ * @privateRemarks
94
+ * based on the `Channel Formats` table https://www.w3.org/TR/WGSL/#texel-formats
95
+ */
96
+ const vertexFormatValueTransform = {
97
+ unorm8: (value) => `Math.round(${value} * 255)`,
98
+ unorm8x2: (value) => `Math.round(${value} * 255)`,
99
+ unorm8x4: (value) => `Math.round(${value} * 255)`,
100
+ snorm8: (value) => `Math.round(${value} * 127)`,
101
+ snorm8x2: (value) => `Math.round(${value} * 127)`,
102
+ snorm8x4: (value) => `Math.round(${value} * 127)`,
103
+ unorm16: (value) => `Math.round(${value} * 65535)`,
104
+ unorm16x2: (value) => `Math.round(${value} * 65535)`,
105
+ unorm16x4: (value) => `Math.round(${value} * 65535)`,
106
+ snorm16: (value) => `Math.round(${value} * 32767)`,
107
+ snorm16x2: (value) => `Math.round(${value} * 32767)`,
108
+ snorm16x4: (value) => `Math.round(${value} * 32767)`
109
+ };
110
+ const specialPackedFormats = {
111
+ "unorm10-10-10-2": {
112
+ writeFunction: "setUint32",
113
+ generator: (offsetExpr, valueExpr) => `output.setUint32(${offsetExpr}, ((${valueExpr}.x*1023&0x3FF)<<22)|((${valueExpr}.y*1023&0x3FF)<<12)|((${valueExpr}.z*1023&0x3FF)<<2)|(${valueExpr}.w*3&3), littleEndian);\n`
114
+ },
115
+ "unorm8x4-bgra": {
116
+ writeFunction: "setUint8",
117
+ generator: (offsetExpr, valueExpr) => {
118
+ const bgraComponents = [
119
+ "z",
120
+ "y",
121
+ "x",
122
+ "w"
123
+ ];
124
+ let code = "";
125
+ for (let idx = 0; idx < 4; idx++) code += `output.setUint8((${offsetExpr} + ${idx}), Math.round(${valueExpr}.${bgraComponents[idx]} * 255), littleEndian);\n`;
126
+ return code;
127
+ }
128
+ }
129
+ };
130
+ function buildWriter(node, offsetExpr, valueExpr, depth = 0) {
131
+ const loopVar = [
132
+ "i",
133
+ "j",
134
+ "k"
135
+ ][depth] || `i${depth}`;
136
+ if (isAtomic(node) || isDecorated(node)) return buildWriter(node.inner, offsetExpr, valueExpr, depth);
137
+ if (isWgslStruct(node) || isUnstruct(node)) {
138
+ const propOffsets = offsetsForProps(node);
139
+ let code = "";
140
+ for (const [key, propOffset] of Object.entries(propOffsets)) {
141
+ const subSchema = node.propTypes[key];
142
+ if (!subSchema) continue;
143
+ code += buildWriter(subSchema, `(${offsetExpr} + ${propOffset.offset})`, `${valueExpr}.${key}`, depth);
144
+ }
145
+ return code;
146
+ }
147
+ if (isWgslArray(node) || isDisarray(node)) {
148
+ const elementSize = roundUp(sizeOf(node.elementType), alignmentOf(node));
149
+ let code = "";
150
+ code += `for (let ${loopVar} = 0; ${loopVar} < ${node.elementCount}; ${loopVar}++) {\n`;
151
+ code += buildWriter(node.elementType, `(${offsetExpr} + ${loopVar} * ${elementSize})`, `${valueExpr}[${loopVar}]`, depth + 1);
152
+ code += "}\n";
153
+ return code;
154
+ }
155
+ if (isVec(node)) {
156
+ if (isVecBool(node)) throw new Error("Compiled writers do not support boolean vectors");
157
+ const primitive = typeToPrimitive[node.type];
158
+ let code = "";
159
+ const writeFunc = primitiveToWriteFunction[primitive];
160
+ const components = [
161
+ "x",
162
+ "y",
163
+ "z",
164
+ "w"
165
+ ];
166
+ for (let i = 0; i < node.componentCount; i++) code += `output.${writeFunc}((${offsetExpr} + ${i * 4}), ${valueExpr}.${components[i]}, littleEndian);\n`;
167
+ return code;
168
+ }
169
+ if (isMat(node)) {
170
+ const writeFunc = primitiveToWriteFunction[typeToPrimitive[node.type]];
171
+ const matSize = isMat2x2f(node) ? 2 : isMat3x3f(node) ? 3 : 4;
172
+ const elementCount = matSize * matSize;
173
+ const rowStride = roundUp(matSize * 4, 8);
174
+ let code = "";
175
+ for (let idx = 0; idx < elementCount; idx++) {
176
+ const colIndex = Math.floor(idx / matSize);
177
+ const rowIndex = idx % matSize;
178
+ const byteOffset = colIndex * rowStride + rowIndex * 4;
179
+ code += `output.${writeFunc}((${offsetExpr} + ${byteOffset}), ${valueExpr}.columns[${colIndex}].${[
180
+ "x",
181
+ "y",
182
+ "z",
183
+ "w"
184
+ ][rowIndex]}, littleEndian);\n`;
185
+ }
186
+ return code;
187
+ }
188
+ if (isPackedData(node)) {
189
+ const formatName = node.type;
190
+ if (formatName in specialPackedFormats) return specialPackedFormats[formatName].generator(offsetExpr, valueExpr);
191
+ const primitive = vertexFormatToPrimitive[formatName];
192
+ const writeFunc = primitiveToWriteFunction[primitive];
193
+ const wgslType = formatToWGSLType[formatName];
194
+ const componentCount = isVec(wgslType) ? wgslType.componentCount : 1;
195
+ const componentSize = primitive === "u8" || primitive === "i8" ? 1 : primitive === "u16" || primitive === "i16" || primitive === "f16" ? 2 : 4;
196
+ const components = [
197
+ "x",
198
+ "y",
199
+ "z",
200
+ "w"
201
+ ];
202
+ const transform = vertexFormatValueTransform[formatName];
203
+ let code = "";
204
+ for (let idx = 0; idx < componentCount; idx++) {
205
+ const accessor = componentCount === 1 ? valueExpr : `${valueExpr}.${components[idx]}`;
206
+ const value = transform ? transform(accessor) : accessor;
207
+ code += `output.${writeFunc}((${offsetExpr} + ${idx * componentSize}), ${value}, littleEndian);\n`;
208
+ }
209
+ return code;
210
+ }
211
+ if (!Object.hasOwn(typeToPrimitive, node.type)) throw new Error(`Primitive ${node.type} is unsupported by compiled writer`);
212
+ return `output.${primitiveToWriteFunction[typeToPrimitive[node.type]]}(${offsetExpr}, ${valueExpr}, littleEndian);\n`;
213
+ }
214
+ function getCompiledWriterForSchema(schema) {
215
+ if (!EVAL_ALLOWED_IN_ENV) {
216
+ console.warn("This environment does not allow eval - using default writer as fallback");
217
+ return;
218
+ }
219
+ if (compiledWriters.has(schema)) return compiledWriters.get(schema);
220
+ try {
221
+ const body = buildWriter(schema, "offset", "value", 0);
222
+ const fn = new Function("output", "offset", "value", "littleEndian=true", body);
223
+ compiledWriters.set(schema, fn);
224
+ return fn;
225
+ } catch (error) {
226
+ console.warn(`Failed to compile writer for schema: ${schema}\nReason: ${error instanceof Error ? error.message : String(error)}\nFalling back to default writer`);
227
+ }
228
+ }
229
+
230
+ //#endregion
231
+ export { getCompiledWriterForSchema };