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
package/data/dataIO.js ADDED
@@ -0,0 +1,549 @@
1
+ import { vec2f, vec2h, vec2i, vec2u, vec3f, vec3h, vec3i, vec3u, vec4f, vec4h, vec4i, vec4u } from "./vector.js";
2
+ import { alignmentOf, customAlignmentOf } from "./alignmentOf.js";
3
+ import { sizeOf } from "./sizeOf.js";
4
+ import { mat2x2f, mat3x3f, mat4x4f } from "./matrix.js";
5
+ import alignIO from "./alignIO.js";
6
+
7
+ //#region src/data/dataIO.ts
8
+ const dataWriters = {
9
+ bool() {
10
+ throw new Error("Booleans are not host-shareable");
11
+ },
12
+ f32(output, _schema, value) {
13
+ output.writeFloat32(value);
14
+ },
15
+ f16(output, _schema, value) {
16
+ output.writeFloat16(value);
17
+ },
18
+ i32(output, _schema, value) {
19
+ output.writeInt32(value);
20
+ },
21
+ u32(output, _schema, value) {
22
+ output.writeUint32(value);
23
+ },
24
+ u16(output, _schema, value) {
25
+ output.writeUint16(value);
26
+ },
27
+ vec2f(output, _, value) {
28
+ output.writeFloat32(value.x);
29
+ output.writeFloat32(value.y);
30
+ },
31
+ vec2h(output, _, value) {
32
+ output.writeFloat16(value.x);
33
+ output.writeFloat16(value.y);
34
+ },
35
+ vec2i(output, _, value) {
36
+ output.writeInt32(value.x);
37
+ output.writeInt32(value.y);
38
+ },
39
+ vec2u(output, _, value) {
40
+ output.writeUint32(value.x);
41
+ output.writeUint32(value.y);
42
+ },
43
+ "vec2<bool>"() {
44
+ throw new Error("Booleans are not host-shareable");
45
+ },
46
+ vec3f(output, _, value) {
47
+ output.writeFloat32(value.x);
48
+ output.writeFloat32(value.y);
49
+ output.writeFloat32(value.z);
50
+ },
51
+ vec3h(output, _, value) {
52
+ output.writeFloat16(value.x);
53
+ output.writeFloat16(value.y);
54
+ output.writeFloat16(value.z);
55
+ },
56
+ vec3i(output, _, value) {
57
+ output.writeInt32(value.x);
58
+ output.writeInt32(value.y);
59
+ output.writeInt32(value.z);
60
+ },
61
+ vec3u(output, _, value) {
62
+ output.writeUint32(value.x);
63
+ output.writeUint32(value.y);
64
+ output.writeUint32(value.z);
65
+ },
66
+ "vec3<bool>"() {
67
+ throw new Error("Booleans are not host-shareable");
68
+ },
69
+ vec4f(output, _, value) {
70
+ output.writeFloat32(value.x);
71
+ output.writeFloat32(value.y);
72
+ output.writeFloat32(value.z);
73
+ output.writeFloat32(value.w);
74
+ },
75
+ vec4h(output, _, value) {
76
+ output.writeFloat16(value.x);
77
+ output.writeFloat16(value.y);
78
+ output.writeFloat16(value.z);
79
+ output.writeFloat16(value.w);
80
+ },
81
+ vec4i(output, _, value) {
82
+ output.writeInt32(value.x);
83
+ output.writeInt32(value.y);
84
+ output.writeInt32(value.z);
85
+ output.writeInt32(value.w);
86
+ },
87
+ vec4u(output, _, value) {
88
+ output.writeUint32(value.x);
89
+ output.writeUint32(value.y);
90
+ output.writeUint32(value.z);
91
+ output.writeUint32(value.w);
92
+ },
93
+ "vec4<bool>"() {
94
+ throw new Error("Booleans are not host-shareable");
95
+ },
96
+ mat2x2f(output, _, value) {
97
+ for (let i = 0; i < value.length; ++i) output.writeFloat32(value[i]);
98
+ },
99
+ mat3x3f(output, _, value) {
100
+ for (let i = 0; i < value.length; ++i) output.writeFloat32(value[i]);
101
+ },
102
+ mat4x4f(output, _, value) {
103
+ for (let i = 0; i < value.length; ++i) output.writeFloat32(value[i]);
104
+ },
105
+ struct(output, schema, value) {
106
+ const alignment = alignmentOf(schema);
107
+ alignIO(output, alignment);
108
+ for (const [key, property] of Object.entries(schema.propTypes)) {
109
+ alignIO(output, alignmentOf(property));
110
+ writeData(output, property, value[key]);
111
+ }
112
+ alignIO(output, alignment);
113
+ },
114
+ array(output, schema, value) {
115
+ if (schema.elementCount === 0) throw new Error("Cannot write using a runtime-sized schema.");
116
+ const alignment = alignmentOf(schema);
117
+ alignIO(output, alignment);
118
+ const beginning = output.currentByteOffset;
119
+ for (let i = 0; i < Math.min(schema.elementCount, value.length); i++) {
120
+ alignIO(output, alignment);
121
+ writeData(output, schema.elementType, value[i]);
122
+ }
123
+ output.seekTo(beginning + sizeOf(schema));
124
+ },
125
+ ptr() {
126
+ throw new Error("Pointers are not host-shareable");
127
+ },
128
+ atomic(output, schema, value) {
129
+ dataWriters[schema.inner.type]?.(output, schema, value);
130
+ },
131
+ decorated(output, schema, value) {
132
+ alignIO(output, customAlignmentOf(schema));
133
+ const beginning = output.currentByteOffset;
134
+ dataWriters[schema.inner?.type]?.(output, schema.inner, value);
135
+ output.seekTo(beginning + sizeOf(schema));
136
+ },
137
+ uint8(output, _, value) {
138
+ output.writeUint8(value);
139
+ },
140
+ uint8x2(output, _, value) {
141
+ output.writeUint8(value.x);
142
+ output.writeUint8(value.y);
143
+ },
144
+ uint8x4(output, _, value) {
145
+ output.writeUint8(value.x);
146
+ output.writeUint8(value.y);
147
+ output.writeUint8(value.z);
148
+ output.writeUint8(value.w);
149
+ },
150
+ sint8(output, _, value) {
151
+ output.writeInt8(value);
152
+ },
153
+ sint8x2(output, _, value) {
154
+ output.writeInt8(value.x);
155
+ output.writeInt8(value.y);
156
+ },
157
+ sint8x4(output, _, value) {
158
+ output.writeInt8(value.x);
159
+ output.writeInt8(value.y);
160
+ output.writeInt8(value.z);
161
+ output.writeInt8(value.w);
162
+ },
163
+ unorm8(output, _, value) {
164
+ output.writeUint8(Math.round(value * 255));
165
+ },
166
+ unorm8x2(output, _, value) {
167
+ output.writeUint8(Math.round(value.x * 255));
168
+ output.writeUint8(Math.round(value.y * 255));
169
+ },
170
+ unorm8x4(output, _, value) {
171
+ output.writeUint8(Math.round(value.x * 255));
172
+ output.writeUint8(Math.round(value.y * 255));
173
+ output.writeUint8(Math.round(value.z * 255));
174
+ output.writeUint8(Math.round(value.w * 255));
175
+ },
176
+ snorm8(output, _, value) {
177
+ output.writeInt8(Math.round(value * 127));
178
+ },
179
+ snorm8x2(output, _, value) {
180
+ output.writeInt8(Math.round(value.x * 127));
181
+ output.writeInt8(Math.round(value.y * 127));
182
+ },
183
+ snorm8x4(output, _, value) {
184
+ output.writeInt8(Math.round(value.x * 127));
185
+ output.writeInt8(Math.round(value.y * 127));
186
+ output.writeInt8(Math.round(value.z * 127));
187
+ output.writeInt8(Math.round(value.w * 127));
188
+ },
189
+ uint16(output, _, value) {
190
+ output.writeUint16(value);
191
+ },
192
+ uint16x2(output, _, value) {
193
+ output.writeUint16(value.x);
194
+ output.writeUint16(value.y);
195
+ },
196
+ uint16x4(output, _, value) {
197
+ output.writeUint16(value.x);
198
+ output.writeUint16(value.y);
199
+ output.writeUint16(value.z);
200
+ output.writeUint16(value.w);
201
+ },
202
+ sint16(output, _, value) {
203
+ output.writeInt16(value);
204
+ },
205
+ sint16x2(output, _, value) {
206
+ output.writeInt16(value.x);
207
+ output.writeInt16(value.y);
208
+ },
209
+ sint16x4(output, _, value) {
210
+ output.writeInt16(value.x);
211
+ output.writeInt16(value.y);
212
+ output.writeInt16(value.z);
213
+ output.writeInt16(value.w);
214
+ },
215
+ unorm16(output, _, value) {
216
+ output.writeUint16(value * 65535);
217
+ },
218
+ unorm16x2(output, _, value) {
219
+ output.writeUint16(value.x * 65535);
220
+ output.writeUint16(value.y * 65535);
221
+ },
222
+ unorm16x4(output, _, value) {
223
+ output.writeUint16(value.x * 65535);
224
+ output.writeUint16(value.y * 65535);
225
+ output.writeUint16(value.z * 65535);
226
+ output.writeUint16(value.w * 65535);
227
+ },
228
+ snorm16(output, _, value) {
229
+ output.writeInt16(Math.round(value * 32767));
230
+ },
231
+ snorm16x2(output, _, value) {
232
+ output.writeInt16(Math.round(value.x * 32767));
233
+ output.writeInt16(Math.round(value.y * 32767));
234
+ },
235
+ snorm16x4(output, _, value) {
236
+ output.writeInt16(Math.round(value.x * 32767));
237
+ output.writeInt16(Math.round(value.y * 32767));
238
+ output.writeInt16(Math.round(value.z * 32767));
239
+ output.writeInt16(Math.round(value.w * 32767));
240
+ },
241
+ float16(output, _, value) {
242
+ output.writeFloat16(value);
243
+ },
244
+ float16x2(output, _, value) {
245
+ output.writeFloat16(value.x);
246
+ output.writeFloat16(value.y);
247
+ },
248
+ float16x4(output, _, value) {
249
+ output.writeFloat16(value.x);
250
+ output.writeFloat16(value.y);
251
+ output.writeFloat16(value.z);
252
+ output.writeFloat16(value.w);
253
+ },
254
+ float32(output, _, value) {
255
+ output.writeFloat32(value);
256
+ },
257
+ float32x2(output, _, value) {
258
+ output.writeFloat32(value.x);
259
+ output.writeFloat32(value.y);
260
+ },
261
+ float32x3(output, _, value) {
262
+ output.writeFloat32(value.x);
263
+ output.writeFloat32(value.y);
264
+ output.writeFloat32(value.z);
265
+ },
266
+ float32x4(output, _, value) {
267
+ output.writeFloat32(value.x);
268
+ output.writeFloat32(value.y);
269
+ output.writeFloat32(value.z);
270
+ output.writeFloat32(value.w);
271
+ },
272
+ uint32(output, _, value) {
273
+ output.writeUint32(value);
274
+ },
275
+ uint32x2(output, _, value) {
276
+ output.writeUint32(value.x);
277
+ output.writeUint32(value.y);
278
+ },
279
+ uint32x3(output, _, value) {
280
+ output.writeUint32(value.x);
281
+ output.writeUint32(value.y);
282
+ output.writeUint32(value.z);
283
+ },
284
+ uint32x4(output, _, value) {
285
+ output.writeUint32(value.x);
286
+ output.writeUint32(value.y);
287
+ output.writeUint32(value.z);
288
+ output.writeUint32(value.w);
289
+ },
290
+ sint32(output, _, value) {
291
+ output.writeInt32(value);
292
+ },
293
+ sint32x2(output, _, value) {
294
+ output.writeInt32(value.x);
295
+ output.writeInt32(value.y);
296
+ },
297
+ sint32x3(output, _, value) {
298
+ output.writeInt32(value.x);
299
+ output.writeInt32(value.y);
300
+ output.writeInt32(value.z);
301
+ },
302
+ sint32x4(output, _, value) {
303
+ output.writeInt32(value.x);
304
+ output.writeInt32(value.y);
305
+ output.writeInt32(value.z);
306
+ output.writeInt32(value.w);
307
+ },
308
+ "unorm10-10-10-2"(output, _, value) {
309
+ let packed = 0;
310
+ packed |= (value.x * 1023 & 1023) << 22;
311
+ packed |= (value.y * 1023 & 1023) << 12;
312
+ packed |= (value.z * 1023 & 1023) << 2;
313
+ packed |= value.w * 3 & 3;
314
+ output.writeUint32(packed);
315
+ },
316
+ "unorm8x4-bgra"(output, _, value) {
317
+ output.writeUint8(value.z * 255);
318
+ output.writeUint8(value.y * 255);
319
+ output.writeUint8(value.x * 255);
320
+ output.writeUint8(value.w * 255);
321
+ },
322
+ disarray(output, schema, value) {
323
+ const alignment = alignmentOf(schema);
324
+ alignIO(output, alignment);
325
+ const beginning = output.currentByteOffset;
326
+ for (let i = 0; i < Math.min(schema.elementCount, value.length); i++) {
327
+ alignIO(output, alignment);
328
+ dataWriters[schema.elementType?.type]?.(output, schema.elementType, value[i]);
329
+ }
330
+ output.seekTo(beginning + sizeOf(schema));
331
+ },
332
+ unstruct(output, schema, value) {
333
+ const propTypes = schema.propTypes;
334
+ for (const [key, property] of Object.entries(propTypes)) dataWriters[property.type]?.(output, property, value[key]);
335
+ },
336
+ "loose-decorated"(output, schema, value) {
337
+ alignIO(output, customAlignmentOf(schema));
338
+ const beginning = output.currentByteOffset;
339
+ const writer = dataWriters[schema.inner?.type];
340
+ writer?.(output, schema.inner, value);
341
+ output.seekTo(beginning + sizeOf(schema));
342
+ return value;
343
+ }
344
+ };
345
+ function writeData(output, schema, value) {
346
+ const writer = dataWriters[schema.type];
347
+ if (!writer) throw new Error(`Cannot write data of type '${schema.type}'.`);
348
+ writer(output, schema, value);
349
+ }
350
+ const dataReaders = {
351
+ bool() {
352
+ throw new Error("Booleans are not host-shareable");
353
+ },
354
+ f32(input) {
355
+ return input.readFloat32();
356
+ },
357
+ f16(input) {
358
+ return input.readFloat16();
359
+ },
360
+ i32(input) {
361
+ return input.readInt32();
362
+ },
363
+ u32(input) {
364
+ return input.readUint32();
365
+ },
366
+ u16(input) {
367
+ return input.readUint16();
368
+ },
369
+ vec2f(input) {
370
+ return vec2f(input.readFloat32(), input.readFloat32());
371
+ },
372
+ vec3f(input) {
373
+ return vec3f(input.readFloat32(), input.readFloat32(), input.readFloat32());
374
+ },
375
+ vec4f(input) {
376
+ return vec4f(input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32());
377
+ },
378
+ vec2h(input) {
379
+ return vec2h(input.readFloat16(), input.readFloat16());
380
+ },
381
+ vec3h(input) {
382
+ return vec3h(input.readFloat16(), input.readFloat16(), input.readFloat16());
383
+ },
384
+ vec4h(input) {
385
+ return vec4h(input.readFloat16(), input.readFloat16(), input.readFloat16(), input.readFloat16());
386
+ },
387
+ vec2i(input) {
388
+ return vec2i(input.readInt32(), input.readInt32());
389
+ },
390
+ vec3i(input) {
391
+ return vec3i(input.readInt32(), input.readInt32(), input.readInt32());
392
+ },
393
+ vec4i(input) {
394
+ return vec4i(input.readInt32(), input.readInt32(), input.readInt32(), input.readInt32());
395
+ },
396
+ vec2u(input) {
397
+ return vec2u(input.readUint32(), input.readUint32());
398
+ },
399
+ vec3u(input) {
400
+ return vec3u(input.readUint32(), input.readUint32(), input.readUint32());
401
+ },
402
+ vec4u(input) {
403
+ return vec4u(input.readUint32(), input.readUint32(), input.readUint32(), input.readUint32());
404
+ },
405
+ "vec2<bool>"() {
406
+ throw new Error("Booleans are not host-shareable");
407
+ },
408
+ "vec3<bool>"() {
409
+ throw new Error("Booleans are not host-shareable");
410
+ },
411
+ "vec4<bool>"() {
412
+ throw new Error("Booleans are not host-shareable");
413
+ },
414
+ mat2x2f(input) {
415
+ return mat2x2f(input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32());
416
+ },
417
+ mat3x3f(input) {
418
+ const skipOneAfter = () => {
419
+ const value = input.readFloat32();
420
+ input.readFloat32();
421
+ return value;
422
+ };
423
+ return mat3x3f(input.readFloat32(), input.readFloat32(), skipOneAfter(), input.readFloat32(), input.readFloat32(), skipOneAfter(), input.readFloat32(), input.readFloat32(), skipOneAfter());
424
+ },
425
+ mat4x4f(input) {
426
+ return mat4x4f(input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32(), input.readFloat32());
427
+ },
428
+ struct(input, schema) {
429
+ const alignment = alignmentOf(schema);
430
+ alignIO(input, alignment);
431
+ const result = {};
432
+ const propTypes = schema.propTypes;
433
+ for (const [key, property] of Object.entries(propTypes)) {
434
+ alignIO(input, alignmentOf(property));
435
+ result[key] = readData(input, property);
436
+ }
437
+ alignIO(input, alignment);
438
+ return result;
439
+ },
440
+ array(input, schema) {
441
+ if (schema.elementCount === 0) throw new Error("Cannot read using a runtime-sized schema.");
442
+ const alignment = alignmentOf(schema);
443
+ const elements = [];
444
+ for (let i = 0; i < schema.elementCount; i++) {
445
+ alignIO(input, alignment);
446
+ const elementType = schema.elementType;
447
+ const value = readData(input, elementType);
448
+ elements.push(value);
449
+ }
450
+ alignIO(input, alignment);
451
+ return elements;
452
+ },
453
+ ptr() {
454
+ throw new Error("Pointers are not host-shareable");
455
+ },
456
+ atomic(input, schema) {
457
+ return readData(input, schema.inner);
458
+ },
459
+ decorated(input, schema) {
460
+ alignIO(input, customAlignmentOf(schema));
461
+ const beginning = input.currentByteOffset;
462
+ const value = readData(input, schema.inner);
463
+ input.seekTo(beginning + sizeOf(schema));
464
+ return value;
465
+ },
466
+ uint8: (i) => i.readUint8(),
467
+ uint8x2: (i) => vec2u(i.readUint8(), i.readUint8()),
468
+ uint8x4: (i) => vec4u(i.readUint8(), i.readUint8(), i.readUint8(), i.readUint8()),
469
+ sint8: (i) => i.readInt8(),
470
+ sint8x2: (i) => {
471
+ return vec2i(i.readInt8(), i.readInt8());
472
+ },
473
+ sint8x4: (i) => vec4i(i.readInt8(), i.readInt8(), i.readInt8(), i.readInt8()),
474
+ unorm8: (i) => i.readUint8() / 255,
475
+ unorm8x2: (i) => vec2f(i.readUint8() / 255, i.readUint8() / 255),
476
+ unorm8x4: (i) => vec4f(i.readUint8() / 255, i.readUint8() / 255, i.readUint8() / 255, i.readUint8() / 255),
477
+ snorm8: (i) => i.readInt8() / 127,
478
+ snorm8x2: (i) => vec2f(i.readInt8() / 127, i.readInt8() / 127),
479
+ snorm8x4: (i) => vec4f(i.readInt8() / 127, i.readInt8() / 127, i.readInt8() / 127, i.readInt8() / 127),
480
+ uint16: (i) => i.readUint16(),
481
+ uint16x2: (i) => vec2u(i.readUint16(), i.readUint16()),
482
+ uint16x4: (i) => vec4u(i.readUint16(), i.readUint16(), i.readUint16(), i.readUint16()),
483
+ sint16: (i) => i.readInt16(),
484
+ sint16x2: (i) => vec2i(i.readInt16(), i.readInt16()),
485
+ sint16x4: (i) => vec4i(i.readInt16(), i.readInt16(), i.readInt16(), i.readInt16()),
486
+ unorm16: (i) => i.readUint16() / 65535,
487
+ unorm16x2: (i) => vec2f(i.readUint16() / 65535, i.readUint16() / 65535),
488
+ unorm16x4: (i) => vec4f(i.readUint16() / 65535, i.readUint16() / 65535, i.readUint16() / 65535, i.readUint16() / 65535),
489
+ snorm16: (i) => i.readInt16() / 32767,
490
+ snorm16x2: (i) => vec2f(i.readInt16() / 32767, i.readInt16() / 32767),
491
+ snorm16x4: (i) => vec4f(i.readInt16() / 32767, i.readInt16() / 32767, i.readInt16() / 32767, i.readInt16() / 32767),
492
+ float16(i) {
493
+ return i.readFloat16();
494
+ },
495
+ float16x2: (i) => vec2f(i.readFloat16(), i.readFloat16()),
496
+ float16x4: (i) => vec4f(i.readFloat16(), i.readFloat16(), i.readFloat16(), i.readFloat16()),
497
+ float32: (i) => i.readFloat32(),
498
+ float32x2: (i) => vec2f(i.readFloat32(), i.readFloat32()),
499
+ float32x3: (i) => vec3f(i.readFloat32(), i.readFloat32(), i.readFloat32()),
500
+ float32x4: (i) => vec4f(i.readFloat32(), i.readFloat32(), i.readFloat32(), i.readFloat32()),
501
+ uint32: (i) => i.readUint32(),
502
+ uint32x2: (i) => vec2u(i.readUint32(), i.readUint32()),
503
+ uint32x3: (i) => vec3u(i.readUint32(), i.readUint32(), i.readUint32()),
504
+ uint32x4: (i) => vec4u(i.readUint32(), i.readUint32(), i.readUint32(), i.readUint32()),
505
+ sint32: (i) => i.readInt32(),
506
+ sint32x2: (i) => vec2i(i.readInt32(), i.readInt32()),
507
+ sint32x3: (i) => vec3i(i.readInt32(), i.readInt32(), i.readInt32()),
508
+ sint32x4: (i) => vec4i(i.readInt32(), i.readInt32(), i.readInt32(), i.readInt32()),
509
+ "unorm10-10-10-2"(i) {
510
+ const packed = i.readUint32();
511
+ return vec4f((packed >> 22) / 1023, (packed >> 12 & 1023) / 1023, (packed >> 2 & 1023) / 1023, (packed & 3) / 3);
512
+ },
513
+ "unorm8x4-bgra"(i) {
514
+ const b = i.readByte() / 255;
515
+ const g = i.readByte() / 255;
516
+ return vec4f(i.readByte() / 255, g, b, i.readByte() / 255);
517
+ },
518
+ unstruct(input, schema) {
519
+ const result = {};
520
+ const propTypes = schema.propTypes;
521
+ for (const [key, property] of Object.entries(propTypes)) result[key] = readData(input, property);
522
+ return result;
523
+ },
524
+ disarray(input, schema) {
525
+ const alignment = alignmentOf(schema);
526
+ const elements = [];
527
+ for (let i = 0; i < schema.elementCount; i++) {
528
+ alignIO(input, alignment);
529
+ elements.push(readData(input, schema.elementType));
530
+ }
531
+ alignIO(input, alignment);
532
+ return elements;
533
+ },
534
+ "loose-decorated"(input, schema) {
535
+ alignIO(input, customAlignmentOf(schema));
536
+ const beginning = input.currentByteOffset;
537
+ const value = readData(input, schema.inner);
538
+ input.seekTo(beginning + sizeOf(schema));
539
+ return value;
540
+ }
541
+ };
542
+ function readData(input, schema) {
543
+ const reader = dataReaders[schema.type];
544
+ if (!reader) throw new Error(`Cannot read data of type '${schema.type}'.`);
545
+ return reader(input, schema);
546
+ }
547
+
548
+ //#endregion
549
+ export { readData, writeData };
@@ -0,0 +1,115 @@
1
+ import { TgpuNamable } from "../shared/meta.js";
2
+ import { $gpuRepr, $invalidSchemaReason, $memIdent, $repr, $reprPartial, $validVertexSchema } from "../shared/symbols.js";
3
+ import { Prettify } from "../shared/utilityTypes.js";
4
+ import "./sampler.js";
5
+ import "./snippet.js";
6
+ import { PackedData } from "./vertexFormatData.js";
7
+ import "../types.js";
8
+ import { AnyWgslData, BaseData } from "./wgslTypes.js";
9
+ import "./texture.js";
10
+ import { Infer, InferGPURecord, InferPartial, InferPartialRecord, InferRecord, IsValidVertexSchema, MemIdentityRecord } from "../shared/repr.js";
11
+
12
+ //#region src/data/dataTypes.d.ts
13
+ /**
14
+ * Array schema constructed via `d.disarrayOf` function.
15
+ *
16
+ * Useful for defining vertex buffers.
17
+ * Elements in the schema are not aligned in respect to their `byteAlignment`,
18
+ * unless they are explicitly decorated with the custom align attribute
19
+ * via `d.align` function.
20
+ */
21
+ interface Disarray<out TElement extends BaseData = BaseData> extends BaseData {
22
+ <T extends TElement>(elements: Infer<T>[]): Infer<T>[];
23
+ (): Infer<TElement>[];
24
+ readonly type: 'disarray';
25
+ readonly elementCount: number;
26
+ readonly elementType: TElement;
27
+ readonly [$repr]: Infer<TElement>[];
28
+ readonly [$reprPartial]: {
29
+ idx: number;
30
+ value: InferPartial<TElement>;
31
+ }[] | undefined;
32
+ readonly [$validVertexSchema]: IsValidVertexSchema<TElement>;
33
+ readonly [$invalidSchemaReason]: 'Disarrays are not host-shareable, use arrays instead';
34
+ }
35
+ /**
36
+ * Struct schema constructed via `d.unstruct` function.
37
+ *
38
+ * Useful for defining vertex buffers, as the standard layout restrictions do not apply.
39
+ * Members are not aligned in respect to their `byteAlignment`,
40
+ * unless they are explicitly decorated with the custom align attribute
41
+ * via `d.align` function.
42
+ */
43
+ interface Unstruct<out TProps extends Record<string, BaseData> = Record<string, BaseData>> extends BaseData, TgpuNamable {
44
+ (props: Prettify<InferRecord<TProps>>): Prettify<InferRecord<TProps>>;
45
+ (): Prettify<InferRecord<TProps>>;
46
+ readonly type: 'unstruct';
47
+ readonly propTypes: TProps;
48
+ readonly [$repr]: Prettify<InferRecord<TProps>>;
49
+ readonly [$gpuRepr]: Prettify<InferGPURecord<TProps>>;
50
+ readonly [$memIdent]: Unstruct<Prettify<MemIdentityRecord<TProps>>>;
51
+ readonly [$reprPartial]: Prettify<Partial<InferPartialRecord<TProps>>> | undefined;
52
+ readonly [$validVertexSchema]: { [K in keyof TProps]: IsValidVertexSchema<TProps[K]> }[keyof TProps] extends true ? true : false;
53
+ readonly [$invalidSchemaReason]: 'Unstructs are not host-shareable, use structs instead';
54
+ }
55
+ interface LooseDecorated<out TInner extends BaseData = BaseData, out TAttribs extends unknown[] = unknown[]> extends BaseData {
56
+ readonly type: 'loose-decorated';
57
+ readonly inner: TInner;
58
+ readonly attribs: TAttribs;
59
+ readonly [$repr]: Infer<TInner>;
60
+ readonly [$invalidSchemaReason]: 'Loosely decorated schemas are not host-shareable';
61
+ readonly [$validVertexSchema]: IsValidVertexSchema<TInner>;
62
+ }
63
+ /**
64
+ * Type utility to extract the inner type from decorated types.
65
+ */
66
+ type Undecorate<T> = T extends {
67
+ readonly type: 'decorated' | 'loose-decorated';
68
+ readonly inner: infer TInner;
69
+ } ? TInner : T;
70
+ /**
71
+ * Type utility to undecorate all values in a record.
72
+ */
73
+ type UndecorateRecord<T extends Record<string, unknown>> = { [Key in keyof T]: Undecorate<T[Key]> };
74
+ declare const looseTypeLiterals: readonly ["unstruct", "disarray", "loose-decorated", "uint8", "uint8x2", "uint8x4", "sint8", "sint8x2", "sint8x4", "unorm8", "unorm8x2", "unorm8x4", "snorm8", "snorm8x2", "snorm8x4", "uint16", "uint16x2", "uint16x4", "sint16", "sint16x2", "sint16x4", "unorm16", "unorm16x2", "unorm16x4", "snorm16", "snorm16x2", "snorm16x4", "float16", "float16x2", "float16x4", "float32", "float32x2", "float32x3", "float32x4", "uint32", "uint32x2", "uint32x3", "uint32x4", "sint32", "sint32x2", "sint32x3", "sint32x4", "unorm10-10-10-2", "unorm8x4-bgra"];
75
+ type LooseTypeLiteral = (typeof looseTypeLiterals)[number];
76
+ type IsLooseData<T> = T extends {
77
+ readonly type: LooseTypeLiteral;
78
+ } ? true : false;
79
+ type AnyLooseData = Disarray | Unstruct | LooseDecorated | PackedData;
80
+ declare function isLooseData(data: unknown): data is AnyLooseData;
81
+ /**
82
+ * Checks whether the passed in value is a disarray schema,
83
+ * as opposed to, e.g., a regular array schema.
84
+ *
85
+ * Array schemas can be used to describe uniform and storage buffers,
86
+ * whereas disarray schemas cannot. Disarrays are useful for
87
+ * defining vertex buffers instead.
88
+ *
89
+ * @example
90
+ * isDisarray(d.arrayOf(d.u32, 4)) // false
91
+ * isDisarray(d.disarrayOf(d.u32, 4)) // true
92
+ * isDisarray(d.vec3f) // false
93
+ */
94
+ declare function isDisarray(schema: unknown): schema is Disarray;
95
+ /**
96
+ * Checks whether passed in value is a unstruct schema,
97
+ * as opposed to, e.g., a struct schema.
98
+ *
99
+ * Struct schemas can be used to describe uniform and storage buffers,
100
+ * whereas unstruct schemas cannot. Unstructs are useful for
101
+ * defining vertex buffers instead.
102
+ *
103
+ * @example
104
+ * isUnstruct(d.struct({ a: d.u32 })) // false
105
+ * isUnstruct(d.unstruct({ a: d.u32 })) // true
106
+ * isUnstruct(d.vec3f) // false
107
+ */
108
+ declare function isUnstruct(schema: unknown): schema is Unstruct;
109
+ declare function isLooseDecorated(value: unknown): value is LooseDecorated;
110
+ declare function isData(value: unknown): value is AnyData;
111
+ type AnyData = AnyWgslData | AnyLooseData;
112
+ declare const UnknownData: unique symbol;
113
+ type UnknownData = typeof UnknownData;
114
+ //#endregion
115
+ export { AnyData, AnyLooseData, Disarray, IsLooseData, LooseDecorated, Undecorate, UndecorateRecord, UnknownData, Unstruct, isData, isDisarray, isLooseData, isLooseDecorated, isUnstruct };