typegpu 0.10.1 → 0.11.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (273) hide show
  1. package/_virtual/_rolldown/runtime.js +13 -0
  2. package/builtin.d.ts +50 -0
  3. package/builtin.js +35 -0
  4. package/common/fullScreenTriangle.d.ts +22 -0
  5. package/common/fullScreenTriangle.js +34 -0
  6. package/common/index.d.ts +4 -4
  7. package/common/index.js +8 -7
  8. package/common/writeSoA.d.ts +16 -0
  9. package/common/writeSoA.js +90 -0
  10. package/core/buffer/buffer.d.ts +79 -0
  11. package/core/buffer/buffer.js +246 -0
  12. package/core/buffer/bufferShorthand.d.ts +48 -0
  13. package/core/buffer/bufferShorthand.js +53 -0
  14. package/core/buffer/bufferUsage.d.ts +43 -0
  15. package/core/buffer/bufferUsage.js +165 -0
  16. package/core/constant/tgpuConstant.d.ts +29 -0
  17. package/core/constant/tgpuConstant.js +68 -0
  18. package/core/declare/tgpuDeclare.d.ts +18 -0
  19. package/core/declare/tgpuDeclare.js +39 -0
  20. package/core/function/autoIO.d.ts +38 -0
  21. package/core/function/autoIO.js +85 -0
  22. package/core/function/comptime.d.ts +39 -0
  23. package/core/function/comptime.js +49 -0
  24. package/core/function/createCallableSchema.js +40 -0
  25. package/core/function/dualImpl.js +52 -0
  26. package/core/function/entryInputRouter.js +39 -0
  27. package/core/function/extractArgs.js +204 -0
  28. package/core/function/fnCore.js +90 -0
  29. package/core/function/fnTypes.d.ts +40 -0
  30. package/core/function/ioSchema.d.ts +10 -0
  31. package/core/function/ioSchema.js +51 -0
  32. package/core/function/shelllessImpl.d.ts +28 -0
  33. package/core/function/shelllessImpl.js +21 -0
  34. package/core/function/templateUtils.js +12 -0
  35. package/core/function/tgpuComputeFn.d.ts +48 -0
  36. package/core/function/tgpuComputeFn.js +55 -0
  37. package/core/function/tgpuFn.d.ts +52 -0
  38. package/core/function/tgpuFn.js +168 -0
  39. package/core/function/tgpuFragmentFn.d.ts +72 -0
  40. package/core/function/tgpuFragmentFn.js +63 -0
  41. package/core/function/tgpuVertexFn.d.ts +59 -0
  42. package/core/function/tgpuVertexFn.js +59 -0
  43. package/core/pipeline/applyPipelineState.js +35 -0
  44. package/core/pipeline/computePipeline.d.ts +54 -0
  45. package/core/pipeline/computePipeline.js +227 -0
  46. package/core/pipeline/connectAttachmentToShader.js +24 -0
  47. package/core/pipeline/connectTargetsToShader.js +27 -0
  48. package/core/pipeline/limitsOverflow.js +12 -0
  49. package/core/pipeline/pipelineUtils.js +29 -0
  50. package/core/pipeline/renderPipeline.d.ts +284 -0
  51. package/core/pipeline/renderPipeline.js +489 -0
  52. package/core/pipeline/timeable.d.ts +20 -0
  53. package/core/pipeline/timeable.js +55 -0
  54. package/core/pipeline/typeGuards.js +27 -0
  55. package/core/querySet/querySet.d.ts +20 -0
  56. package/core/querySet/querySet.js +104 -0
  57. package/core/rawCodeSnippet/tgpuRawCodeSnippet.d.ts +59 -0
  58. package/core/rawCodeSnippet/tgpuRawCodeSnippet.js +94 -0
  59. package/core/resolve/externals.d.ts +8 -0
  60. package/core/resolve/externals.js +56 -0
  61. package/core/resolve/namespace.d.ts +38 -0
  62. package/core/resolve/namespace.js +39 -0
  63. package/core/resolve/resolveData.js +144 -0
  64. package/core/resolve/stitch.js +23 -0
  65. package/core/resolve/tgpuResolve.d.ts +153 -0
  66. package/core/resolve/tgpuResolve.js +66 -0
  67. package/core/root/configurableImpl.js +17 -0
  68. package/core/root/init.d.ts +64 -0
  69. package/core/root/init.js +464 -0
  70. package/core/root/rootTypes.d.ts +642 -0
  71. package/core/sampler/sampler.d.ts +31 -0
  72. package/core/sampler/sampler.js +116 -0
  73. package/core/simulate/tgpuSimulate.d.ts +36 -0
  74. package/core/simulate/tgpuSimulate.js +74 -0
  75. package/core/slot/accessor.d.ts +9 -0
  76. package/core/slot/accessor.js +95 -0
  77. package/core/slot/internalSlots.js +5 -0
  78. package/core/slot/lazy.d.ts +6 -0
  79. package/core/slot/lazy.js +40 -0
  80. package/core/slot/slot.d.ts +6 -0
  81. package/core/slot/slot.js +39 -0
  82. package/core/slot/slotTypes.d.ts +92 -0
  83. package/core/slot/slotTypes.js +19 -0
  84. package/core/texture/externalTexture.d.ts +6 -0
  85. package/core/texture/externalTexture.js +47 -0
  86. package/core/texture/texture.d.ts +114 -0
  87. package/core/texture/texture.js +314 -0
  88. package/core/texture/textureFormats.d.ts +29 -0
  89. package/core/texture/textureFormats.js +97 -0
  90. package/core/texture/textureProps.d.ts +11 -0
  91. package/core/texture/textureUtils.js +222 -0
  92. package/core/texture/usageExtension.d.ts +21 -0
  93. package/core/texture/usageExtension.js +19 -0
  94. package/core/unroll/tgpuUnroll.d.ts +68 -0
  95. package/core/unroll/tgpuUnroll.js +94 -0
  96. package/core/valueProxyUtils.js +42 -0
  97. package/core/variable/tgpuVariable.d.ts +38 -0
  98. package/core/variable/tgpuVariable.js +99 -0
  99. package/core/vertexLayout/connectAttributesToShader.js +57 -0
  100. package/core/vertexLayout/vertexAttribute.d.ts +29 -0
  101. package/core/vertexLayout/vertexLayout.d.ts +19 -0
  102. package/core/vertexLayout/vertexLayout.js +103 -0
  103. package/data/alignIO.js +14 -0
  104. package/data/alignmentOf.d.ts +9 -0
  105. package/data/alignmentOf.js +86 -0
  106. package/data/array.d.ts +26 -0
  107. package/data/array.js +46 -0
  108. package/data/atomic.d.ts +15 -0
  109. package/data/atomic.js +24 -0
  110. package/data/attributes.d.ts +121 -0
  111. package/data/attributes.js +145 -0
  112. package/data/autoStruct.d.ts +1 -0
  113. package/data/autoStruct.js +81 -0
  114. package/data/compiledIO.js +228 -0
  115. package/data/dataIO.js +556 -0
  116. package/data/dataTypes.d.ts +115 -0
  117. package/data/dataTypes.js +100 -0
  118. package/data/deepEqual.d.ts +25 -0
  119. package/data/deepEqual.js +56 -0
  120. package/data/disarray.d.ts +32 -0
  121. package/data/disarray.js +50 -0
  122. package/data/getLongestContiguousPrefix.d.ts +9 -0
  123. package/data/getLongestContiguousPrefix.js +13 -0
  124. package/data/index.d.ts +26 -4
  125. package/data/index.js +36 -9
  126. package/data/instanceToSchema.d.ts +33 -0
  127. package/data/isContiguous.d.ts +9 -0
  128. package/data/isContiguous.js +13 -0
  129. package/data/matrix.d.ts +124 -0
  130. package/data/matrix.js +531 -0
  131. package/data/numberOps.js +23 -0
  132. package/data/numeric.d.ts +81 -0
  133. package/data/numeric.js +221 -0
  134. package/data/offsetUtils.d.ts +33 -0
  135. package/data/offsetUtils.js +165 -0
  136. package/data/offsets.js +34 -0
  137. package/data/partialIO.js +113 -0
  138. package/data/ptr.d.ts +11 -0
  139. package/data/ptr.js +44 -0
  140. package/data/ref.d.ts +34 -0
  141. package/data/ref.js +94 -0
  142. package/data/sampler.d.ts +107 -0
  143. package/data/sampler.js +24 -0
  144. package/data/schemaCallWrapper.js +30 -0
  145. package/data/schemaMemoryLayout.js +198 -0
  146. package/data/sizeOf.d.ts +9 -0
  147. package/data/sizeOf.js +13 -0
  148. package/data/snippet.d.ts +26 -0
  149. package/data/snippet.js +70 -0
  150. package/data/struct.d.ts +17 -0
  151. package/data/struct.js +44 -0
  152. package/data/texture.d.ts +292 -0
  153. package/{texture-BagDrrks.js → data/texture.js} +6 -5
  154. package/data/unstruct.d.ts +24 -0
  155. package/data/unstruct.js +41 -0
  156. package/data/vector.d.ts +191 -0
  157. package/data/vector.js +239 -0
  158. package/data/vectorImpl.js +515 -0
  159. package/data/vectorOps.js +681 -0
  160. package/data/vertexFormatData.d.ts +190 -0
  161. package/data/vertexFormatData.js +109 -0
  162. package/data/wgslTypes.d.ts +924 -0
  163. package/data/wgslTypes.js +222 -0
  164. package/errors.d.ts +44 -0
  165. package/errors.js +131 -0
  166. package/execMode.js +49 -0
  167. package/extension.d.ts +11 -0
  168. package/extension.js +16 -0
  169. package/getGPUValue.js +7 -0
  170. package/index.d.ts +42 -243
  171. package/index.js +21 -6320
  172. package/indexNamedExports.d.ts +40 -0
  173. package/mathUtils.js +12 -0
  174. package/memo.js +22 -0
  175. package/nameRegistry.d.ts +30 -0
  176. package/nameRegistry.js +447 -0
  177. package/package.js +4 -0
  178. package/package.json +26 -26
  179. package/resolutionCtx.d.ts +19 -0
  180. package/resolutionCtx.js +612 -0
  181. package/shared/env.js +12 -0
  182. package/shared/generators.js +13 -0
  183. package/shared/meta.d.ts +39 -0
  184. package/shared/meta.js +61 -0
  185. package/shared/repr.d.ts +138 -0
  186. package/shared/stringify.js +20 -0
  187. package/shared/symbols.d.ts +70 -0
  188. package/shared/symbols.js +48 -0
  189. package/shared/utilityTypes.d.ts +33 -0
  190. package/shared/utilityTypes.js +6 -0
  191. package/shared/vertexFormat.d.ts +70 -0
  192. package/shared/vertexFormat.js +63 -0
  193. package/std/array.d.ts +7 -0
  194. package/std/array.js +25 -0
  195. package/std/atomic.d.ts +19 -0
  196. package/std/atomic.js +111 -0
  197. package/std/bitcast.d.ts +10 -0
  198. package/std/bitcast.js +41 -0
  199. package/std/boolean.d.ts +141 -0
  200. package/std/boolean.js +299 -0
  201. package/std/derivative.d.ts +16 -0
  202. package/std/derivative.js +87 -0
  203. package/std/discard.d.ts +6 -0
  204. package/std/discard.js +14 -0
  205. package/std/extensions.d.ts +6 -0
  206. package/std/extensions.js +12 -0
  207. package/std/index.d.ts +17 -4
  208. package/std/index.js +21 -7
  209. package/std/matrix.d.ts +41 -0
  210. package/std/matrix.js +85 -0
  211. package/std/numeric.d.ts +200 -0
  212. package/std/numeric.js +845 -0
  213. package/std/operators.d.ts +56 -0
  214. package/std/operators.js +227 -0
  215. package/std/packing.d.ts +26 -0
  216. package/std/packing.js +84 -0
  217. package/std/range.d.ts +24 -0
  218. package/std/range.js +38 -0
  219. package/std/subgroup.d.ts +47 -0
  220. package/std/subgroup.js +218 -0
  221. package/std/texture.d.ts +117 -0
  222. package/std/texture.js +207 -0
  223. package/tgpu.js +42 -0
  224. package/tgpuBindGroupLayout.d.ts +161 -0
  225. package/tgpuBindGroupLayout.js +272 -0
  226. package/tgpuUnstable.d.ts +48 -0
  227. package/tgpuUnstable.js +64 -0
  228. package/tgsl/accessIndex.js +43 -0
  229. package/tgsl/accessProp.js +115 -0
  230. package/tgsl/consoleLog/deserializers.js +115 -0
  231. package/tgsl/consoleLog/logGenerator.js +84 -0
  232. package/tgsl/consoleLog/serializers.js +223 -0
  233. package/tgsl/consoleLog/types.d.ts +52 -0
  234. package/tgsl/consoleLog/types.js +11 -0
  235. package/tgsl/conversion.js +198 -0
  236. package/tgsl/forOfUtils.js +71 -0
  237. package/tgsl/generationHelpers.d.ts +37 -0
  238. package/tgsl/generationHelpers.js +67 -0
  239. package/tgsl/math.js +43 -0
  240. package/tgsl/shaderGenerator.d.ts +20 -0
  241. package/tgsl/shaderGenerator_members.d.ts +2 -0
  242. package/tgsl/shaderGenerator_members.js +6 -0
  243. package/tgsl/shellless.d.ts +11 -0
  244. package/tgsl/shellless.js +46 -0
  245. package/tgsl/wgslGenerator.d.ts +36 -0
  246. package/tgsl/wgslGenerator.js +639 -0
  247. package/types.d.ts +265 -0
  248. package/types.js +43 -0
  249. package/unwrapper.d.ts +27 -0
  250. package/wgslExtensions.d.ts +5 -0
  251. package/wgslExtensions.js +17 -0
  252. package/builtin-DdtWpk2t.js +0 -818
  253. package/builtin-DdtWpk2t.js.map +0 -1
  254. package/chunk-BYypO7fO.js +0 -18
  255. package/common/index.d.ts.map +0 -1
  256. package/common/index.js.map +0 -1
  257. package/data/index.d.ts.map +0 -1
  258. package/data/index.js.map +0 -1
  259. package/deepEqual-DQxK4vdp.js +0 -413
  260. package/deepEqual-DQxK4vdp.js.map +0 -1
  261. package/extensions-DIVuAfBM.js +0 -2032
  262. package/extensions-DIVuAfBM.js.map +0 -1
  263. package/fullScreenTriangle-CfFyQd_0.js +0 -543
  264. package/fullScreenTriangle-CfFyQd_0.js.map +0 -1
  265. package/index.d.ts.map +0 -1
  266. package/index.js.map +0 -1
  267. package/indexNamedExports-oL6tyaJ9.d.ts +0 -5697
  268. package/indexNamedExports-oL6tyaJ9.d.ts.map +0 -1
  269. package/operators-d-PMVTo7.js +0 -4158
  270. package/operators-d-PMVTo7.js.map +0 -1
  271. package/std/index.d.ts.map +0 -1
  272. package/std/index.js.map +0 -1
  273. package/texture-BagDrrks.js.map +0 -1
@@ -0,0 +1,81 @@
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
+ //#region src/data/autoStruct.ts
6
+ /**
7
+ * A requirement for the generated struct is that non-builtin properties need to
8
+ * have the same name in WGSL as they do in JS. This allows locations to be properly
9
+ * matched between the Vertex output and Fragment input.
10
+ */
11
+ var AutoStruct = class {
12
+ /**
13
+ * js key -> data type
14
+ */
15
+ #validProps;
16
+ /**
17
+ * js key -> { prop: 'wgsl key', type: ... }
18
+ * @example '$position' -> { prop: 'position', type: ... }
19
+ */
20
+ #allocated;
21
+ #usedWgslKeys;
22
+ #locations;
23
+ #cachedStruct;
24
+ #typeForExtraProps;
25
+ constructor(validProps, typeForExtraProps, locations) {
26
+ this.#validProps = validProps;
27
+ this.#typeForExtraProps = typeForExtraProps;
28
+ this.#allocated = {};
29
+ this.#locations = locations;
30
+ this.#usedWgslKeys = /* @__PURE__ */ new Set();
31
+ }
32
+ /**
33
+ * Used for accessing builtins, varying and attributes in code.
34
+ */
35
+ accessProp(key) {
36
+ const dataType = this.#validProps[key] ?? this.#typeForExtraProps;
37
+ if (!dataType) return;
38
+ return this.provideProp(key, dataType);
39
+ }
40
+ /**
41
+ * Used for providing new varyings.
42
+ *
43
+ * @privateRemarks
44
+ * Internally used by `accessProp`.
45
+ */
46
+ provideProp(key, dataType) {
47
+ let alloc = this.#allocated[key];
48
+ if (!alloc) {
49
+ const wgslKey = key.replaceAll("$", "");
50
+ if (this.#usedWgslKeys.has(wgslKey)) throw new Error(`Property name '${wgslKey}' causes naming clashes. Choose a different name.`);
51
+ if (!isValidProp(wgslKey)) throw new Error(`Property key '${key}' is a reserved WGSL word. Choose a different name.`);
52
+ this.#usedWgslKeys.add(wgslKey);
53
+ alloc = {
54
+ prop: wgslKey,
55
+ type: dataType
56
+ };
57
+ this.#allocated[key] = alloc;
58
+ }
59
+ return alloc;
60
+ }
61
+ get completeStruct() {
62
+ if (!this.#cachedStruct) {
63
+ this.#cachedStruct = createIoSchema(Object.fromEntries(Object.values(this.#allocated).map((alloc) => {
64
+ return [alloc.prop, alloc.type];
65
+ })), this.#locations);
66
+ const ownName = getName(this);
67
+ if (ownName) setName(this.#cachedStruct, ownName);
68
+ }
69
+ return this.#cachedStruct;
70
+ }
71
+ [$resolve](ctx) {
72
+ return ctx.resolve(this.completeStruct);
73
+ }
74
+ toString() {
75
+ return `auto-struct:${getName(this) ?? "<unnamed>"}`;
76
+ }
77
+ };
78
+ AutoStruct.prototype[$internal] = {};
79
+ AutoStruct.prototype.type = "auto-struct";
80
+ //#endregion
81
+ export { AutoStruct };
@@ -0,0 +1,228 @@
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
+ //#region src/data/compiledIO.ts
9
+ const EVAL_ALLOWED_IN_ENV = (() => {
10
+ try {
11
+ new Function("return true");
12
+ return true;
13
+ } catch {
14
+ return false;
15
+ }
16
+ })();
17
+ const compiledWriters = /* @__PURE__ */ new WeakMap();
18
+ const typeToPrimitive = {
19
+ u32: "u32",
20
+ vec2u: "u32",
21
+ vec3u: "u32",
22
+ vec4u: "u32",
23
+ u16: "u16",
24
+ i32: "i32",
25
+ vec2i: "i32",
26
+ vec3i: "i32",
27
+ vec4i: "i32",
28
+ f32: "f32",
29
+ vec2f: "f32",
30
+ vec3f: "f32",
31
+ vec4f: "f32",
32
+ f16: "f16",
33
+ vec2h: "f16",
34
+ vec3h: "f16",
35
+ vec4h: "f16",
36
+ mat2x2f: "f32",
37
+ mat3x3f: "f32",
38
+ mat4x4f: "f32"
39
+ };
40
+ const vertexFormatToPrimitive = {
41
+ uint8: "u8",
42
+ uint8x2: "u8",
43
+ uint8x4: "u8",
44
+ sint8: "i8",
45
+ sint8x2: "i8",
46
+ sint8x4: "i8",
47
+ unorm8: "u8",
48
+ unorm8x2: "u8",
49
+ unorm8x4: "u8",
50
+ snorm8: "i8",
51
+ snorm8x2: "i8",
52
+ snorm8x4: "i8",
53
+ uint16: "u16",
54
+ uint16x2: "u16",
55
+ uint16x4: "u16",
56
+ sint16: "i16",
57
+ sint16x2: "i16",
58
+ sint16x4: "i16",
59
+ unorm16: "u16",
60
+ unorm16x2: "u16",
61
+ unorm16x4: "u16",
62
+ snorm16: "i16",
63
+ snorm16x2: "i16",
64
+ snorm16x4: "i16",
65
+ float16: "f16",
66
+ float16x2: "f16",
67
+ float16x4: "f16",
68
+ float32: "f32",
69
+ float32x2: "f32",
70
+ float32x3: "f32",
71
+ float32x4: "f32",
72
+ uint32: "u32",
73
+ uint32x2: "u32",
74
+ uint32x3: "u32",
75
+ uint32x4: "u32",
76
+ sint32: "i32",
77
+ sint32x2: "i32",
78
+ sint32x3: "i32",
79
+ sint32x4: "i32"
80
+ };
81
+ const primitiveToWriteFunction = {
82
+ u32: "setUint32",
83
+ i32: "setInt32",
84
+ f32: "setFloat32",
85
+ u16: "setUint16",
86
+ i16: "setInt16",
87
+ f16: "setFloat16",
88
+ u8: "setUint8",
89
+ i8: "setInt8"
90
+ };
91
+ /**
92
+ * @privateRemarks
93
+ * based on the `Channel Formats` table https://www.w3.org/TR/WGSL/#texel-formats
94
+ */
95
+ const vertexFormatValueTransform = {
96
+ unorm8: (value) => `Math.round(${value} * 255)`,
97
+ unorm8x2: (value) => `Math.round(${value} * 255)`,
98
+ unorm8x4: (value) => `Math.round(${value} * 255)`,
99
+ snorm8: (value) => `Math.round(${value} * 127)`,
100
+ snorm8x2: (value) => `Math.round(${value} * 127)`,
101
+ snorm8x4: (value) => `Math.round(${value} * 127)`,
102
+ unorm16: (value) => `Math.round(${value} * 65535)`,
103
+ unorm16x2: (value) => `Math.round(${value} * 65535)`,
104
+ unorm16x4: (value) => `Math.round(${value} * 65535)`,
105
+ snorm16: (value) => `Math.round(${value} * 32767)`,
106
+ snorm16x2: (value) => `Math.round(${value} * 32767)`,
107
+ snorm16x4: (value) => `Math.round(${value} * 32767)`
108
+ };
109
+ const specialPackedFormats = {
110
+ "unorm10-10-10-2": {
111
+ writeFunction: "setUint32",
112
+ 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`
113
+ },
114
+ "unorm8x4-bgra": {
115
+ writeFunction: "setUint8",
116
+ generator: (offsetExpr, valueExpr) => {
117
+ return [
118
+ "z",
119
+ "y",
120
+ "x",
121
+ "w"
122
+ ].map((c, idx) => `output.setUint8((${offsetExpr} + ${idx}), Math.round(${valueExpr}.${c} * 255), littleEndian);\n`).join("");
123
+ }
124
+ }
125
+ };
126
+ function buildWriter(node, offsetExpr, valueExpr, depth = 0, partial = false) {
127
+ const emitWrite = (writeFunc, addr, value) => partial ? `if (${addr} < endOffset) { output.${writeFunc}(${addr}, ${value}, littleEndian); }\n` : `output.${writeFunc}(${addr}, ${value}, littleEndian);\n`;
128
+ const emitBlock = (addr, block) => partial ? `if (${addr} < endOffset) {\n${block}}\n` : block;
129
+ function go(node, offsetExpr, valueExpr, depth) {
130
+ const loopVar = [
131
+ "i",
132
+ "j",
133
+ "k"
134
+ ][depth] || `i${depth}`;
135
+ if (isAtomic(node) || isDecorated(node)) return go(node.inner, offsetExpr, valueExpr, depth);
136
+ if (isWgslStruct(node) || isUnstruct(node)) return Object.entries(offsetsForProps(node)).map(([key, propOffset]) => {
137
+ const subSchema = node.propTypes[key];
138
+ return subSchema ? go(subSchema, `(${offsetExpr} + ${propOffset.offset})`, `${valueExpr}.${key}`, depth) : "";
139
+ }).join("");
140
+ if (isWgslArray(node) || isDisarray(node)) {
141
+ const elementSize = roundUp(sizeOf(node.elementType), alignmentOf(node));
142
+ const totalSize = node.elementCount * elementSize;
143
+ const copyLen = partial ? `Math.min(${valueExpr}.byteLength, Math.max(0, endOffset - (${offsetExpr})))` : `Math.min(${valueExpr}.byteLength, ${totalSize})`;
144
+ let code = `if (ArrayBuffer.isView(${valueExpr})) {\n`;
145
+ code += ` new Uint8Array(output.buffer).set(new Uint8Array(${valueExpr}.buffer, ${valueExpr}.byteOffset, ${copyLen}), output.byteOffset + (${offsetExpr}));\n`;
146
+ code += `} else {\n`;
147
+ code += `for (let ${loopVar} = 0; ${loopVar} < ${node.elementCount}; ${loopVar}++) {\n`;
148
+ if (partial) code += `if ((${offsetExpr} + ${loopVar} * ${elementSize}) >= endOffset) return;\n`;
149
+ code += go(node.elementType, `(${offsetExpr} + ${loopVar} * ${elementSize})`, `${valueExpr}[${loopVar}]`, depth + 1);
150
+ code += "}\n";
151
+ code += "}\n";
152
+ return code;
153
+ }
154
+ if (isVec(node)) {
155
+ if (isVecBool(node)) throw new Error("Compiled writers do not support boolean vectors");
156
+ const primitive = typeToPrimitive[node.type];
157
+ const componentSize = sizeOf(node.primitive);
158
+ const writeFunc = primitiveToWriteFunction[primitive];
159
+ return Array.from({ length: node.componentCount }, (_, i) => emitWrite(writeFunc, `(${offsetExpr} + ${i * componentSize})`, `${valueExpr}[${i}]`)).join("");
160
+ }
161
+ if (isMat(node)) {
162
+ const writeFunc = primitiveToWriteFunction[typeToPrimitive[node.type]];
163
+ const matSize = isMat2x2f(node) ? 2 : isMat3x3f(node) ? 3 : 4;
164
+ const rowStride = roundUp(matSize * 4, 8);
165
+ const components = [
166
+ "x",
167
+ "y",
168
+ "z",
169
+ "w"
170
+ ];
171
+ const wgslElementStride = rowStride / 4;
172
+ return Array.from({ length: matSize * matSize }, (_, idx) => {
173
+ const col = Math.floor(idx / matSize);
174
+ const row = idx % matSize;
175
+ const packedIndex = col * matSize + row;
176
+ const wgslIndex = col * wgslElementStride + row;
177
+ return emitWrite(writeFunc, `(${offsetExpr} + ${col * rowStride + row * 4})`, `ArrayBuffer.isView(${valueExpr}) ? ${valueExpr}[${wgslIndex}] : Array.isArray(${valueExpr}) ? ${valueExpr}[${packedIndex}] : ${valueExpr}.columns[${col}].${components[row]}`);
178
+ }).join("");
179
+ }
180
+ if (isPackedData(node)) {
181
+ const formatName = node.type;
182
+ if (formatName in specialPackedFormats) {
183
+ const handler = specialPackedFormats[formatName];
184
+ return emitBlock(`(${offsetExpr})`, handler.generator(offsetExpr, valueExpr));
185
+ }
186
+ const primitive = vertexFormatToPrimitive[formatName];
187
+ const writeFunc = primitiveToWriteFunction[primitive];
188
+ const wgslType = formatToWGSLType[formatName];
189
+ const componentCount = isVec(wgslType) ? wgslType.componentCount : 1;
190
+ const componentSize = primitive === "u8" || primitive === "i8" ? 1 : primitive === "u16" || primitive === "i16" || primitive === "f16" ? 2 : 4;
191
+ const components = [
192
+ "x",
193
+ "y",
194
+ "z",
195
+ "w"
196
+ ];
197
+ const transform = vertexFormatValueTransform[formatName];
198
+ return Array.from({ length: componentCount }, (_, idx) => {
199
+ const accessor = componentCount === 1 ? valueExpr : `${valueExpr}.${components[idx]}`;
200
+ const value = transform ? transform(accessor) : accessor;
201
+ return emitWrite(writeFunc, `(${offsetExpr} + ${idx * componentSize})`, value);
202
+ }).join("");
203
+ }
204
+ if (!Object.hasOwn(typeToPrimitive, node.type)) throw new Error(`Primitive ${node.type} is unsupported by compiled writer`);
205
+ const primitive = typeToPrimitive[node.type];
206
+ return emitWrite(primitiveToWriteFunction[primitive], offsetExpr, valueExpr);
207
+ }
208
+ return go(node, offsetExpr, valueExpr, depth);
209
+ }
210
+ function getCompiledWriter(schema) {
211
+ if (!EVAL_ALLOWED_IN_ENV) {
212
+ console.warn("This environment does not allow eval - using default writer as fallback");
213
+ return;
214
+ }
215
+ const cached = compiledWriters.get(schema);
216
+ if (cached) return cached;
217
+ try {
218
+ const fullBody = buildWriter(schema, "offset", "value", 0, false);
219
+ const body = `if (offset > 0 || endOffset < output.byteLength) {\n${buildWriter(schema, "offset", "value", 0, true)}} else {\n${fullBody}}\n`;
220
+ const fn = new Function("output", "offset", "value", "littleEndian=true", "endOffset=output.byteLength", body);
221
+ compiledWriters.set(schema, fn);
222
+ return fn;
223
+ } catch (error) {
224
+ console.warn(`Failed to compile writer for schema: ${schema}\nReason: ${error instanceof Error ? error.message : String(error)}\nFalling back to default writer`);
225
+ }
226
+ }
227
+ //#endregion
228
+ export { getCompiledWriter };