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
package/shared/meta.js ADDED
@@ -0,0 +1,61 @@
1
+ import { version } from "../package.js";
2
+ import { DEV, TEST } from "./env.js";
3
+ import { $getNameForward, isMarkedInternal } from "./symbols.js";
4
+ //#region src/shared/meta.ts
5
+ const globalWithMeta = globalThis;
6
+ if (globalWithMeta.__TYPEGPU_VERSION__ !== void 0) console.warn(`Found duplicate TypeGPU version. First was ${globalWithMeta.__TYPEGPU_VERSION__}, this one is ${version}. This may cause unexpected behavior.`);
7
+ globalWithMeta.__TYPEGPU_VERSION__ = version;
8
+ globalWithMeta.__TYPEGPU_AUTONAME__ = (exp, label) => isNamable(exp) && isMarkedInternal(exp) && !getName(exp) ? exp.$name(label) : exp;
9
+ /**
10
+ * Performance measurements are only enabled in dev & test environments for now
11
+ */
12
+ const PERF = (DEV || TEST) && {
13
+ get enabled() {
14
+ return !!globalWithMeta.__TYPEGPU_MEASURE_PERF__;
15
+ },
16
+ record(name, data) {
17
+ const records = globalWithMeta.__TYPEGPU_PERF_RECORDS__ ??= /* @__PURE__ */ new Map();
18
+ let entries = records.get(name);
19
+ if (!entries) {
20
+ entries = [];
21
+ records.set(name, entries);
22
+ }
23
+ entries.push(data);
24
+ }
25
+ } || void 0;
26
+ function isForwarded(value) {
27
+ return !!value?.[$getNameForward];
28
+ }
29
+ function getName(definition) {
30
+ if (isForwarded(definition)) return getName(definition[$getNameForward]);
31
+ return getMetaData(definition)?.name;
32
+ }
33
+ function setName(definition, name) {
34
+ if (isForwarded(definition)) {
35
+ setName(definition[$getNameForward], name);
36
+ return;
37
+ }
38
+ setMetaData(definition, { name });
39
+ }
40
+ function isNamable(value) {
41
+ return !!value?.$name;
42
+ }
43
+ /**
44
+ * AST's are given to functions with a 'use gpu' directive, which this function checks for.
45
+ */
46
+ function hasTinyestMetadata(value) {
47
+ return !!getMetaData(value)?.ast;
48
+ }
49
+ function getMetaData(definition) {
50
+ return globalWithMeta.__TYPEGPU_META__.get(definition);
51
+ }
52
+ function setMetaData(definition, metaData) {
53
+ globalWithMeta.__TYPEGPU_META__ ??= /* @__PURE__ */ new WeakMap();
54
+ const map = globalWithMeta.__TYPEGPU_META__;
55
+ map.set(definition, {
56
+ ...map.get(definition),
57
+ ...metaData
58
+ });
59
+ }
60
+ //#endregion
61
+ export { PERF, getMetaData, getName, hasTinyestMetadata, isNamable, setName };
@@ -0,0 +1,138 @@
1
+ import { $gpuRepr, $gpuValueOf, $inRepr, $invalidSchemaReason, $memIdent, $repr, $reprPartial, $reprPatch, $validStorageSchema, $validUniformSchema, $validVertexSchema } from "./symbols.js";
2
+ import { Default } from "./utilityTypes.js";
3
+ import { U16, U32, WgslArray } from "../data/wgslTypes.js";
4
+ import { ViewDimensionToDimension } from "../core/texture/textureFormats.js";
5
+ import { WgslStorageTexture, WgslTexture } from "../data/texture.js";
6
+ import { TgpuTexture } from "../core/texture/texture.js";
7
+ import { Disarray, Undecorate } from "../data/dataTypes.js";
8
+
9
+ //#region src/shared/repr.d.ts
10
+ /**
11
+ * Extracts the inferred representation of a resource.
12
+ * For inferring types as seen by the GPU, see {@link InferGPU}
13
+ *
14
+ * @example
15
+ * type A = Infer<F32> // => number
16
+ * type B = Infer<WgslArray<F32>> // => number[]
17
+ * type C = Infer<Atomic<U32>> // => number
18
+ */
19
+ type Infer<T> = T extends {
20
+ readonly [$repr]: infer TRepr;
21
+ } ? TRepr : T;
22
+ /**
23
+ * Extracts the inferred input (write-side) representation of a resource.
24
+ *
25
+ * @example
26
+ * type A = InferInput<Vec3f> // => v3f | [number, number, number] | Float32Array
27
+ * type B = InferInput<WgslArray<Vec3f>> // => (v3f | [number, number, number] | Float32Array)[] | Float32Array
28
+ * type C = InferInput<F32> // => number (same as Infer<F32>)
29
+ * const arrayOfStructs = d.arrayOf(d.struct({ pos: d.vec3f, id: d.f32 }), 4);
30
+ * type D = d.InferInput<typeof arrayOfStructs>; // { pos: d.v3f | Float32Array<ArrayBufferLike> | [number, number, number]; id: number; }[]
31
+ */
32
+ type InferInput<T> = Infer<T> | (T extends {
33
+ readonly [$inRepr]: infer TRepr;
34
+ } ? TRepr : never);
35
+ /**
36
+ * Extracts a sparse/partial inferred representation of a resource.
37
+ * Used by the `buffer.writePartial` API.
38
+ *
39
+ * @deprecated
40
+ *
41
+ * @example
42
+ * type A = InferPartial<F32> // => number | undefined
43
+ * type B = InferPartial<WgslStruct<{ a: F32 }>> // => { a?: number | undefined }
44
+ * type C = InferPartial<WgslArray<F32>> // => { idx: number; value: number | undefined }[] | undefined
45
+ */
46
+ type InferPartial<T> = T extends {
47
+ readonly [$reprPartial]: infer TRepr;
48
+ } ? TRepr : T extends {
49
+ readonly [$repr]: infer TRepr;
50
+ } ? TRepr | undefined : T;
51
+ /**
52
+ * Extracts the inferred representation of a resource (as seen by the GPU).
53
+ *
54
+ * @example
55
+ * type A = InferGPU<F32> // => number
56
+ * type B = InferGPU<WgslArray<F32>> // => number[]
57
+ * type C = InferGPU<Atomic<U32>> // => atomicU32
58
+ */
59
+ type InferGPU<T> = T extends {
60
+ readonly [$gpuRepr]: infer TRepr;
61
+ } ? TRepr : Infer<T>;
62
+ type InferRecord<T extends Record<string | number | symbol, unknown>> = { [Key in keyof T]: Infer<T[Key]> };
63
+ type InferInputRecord<T extends Record<string | number | symbol, unknown>> = { [Key in keyof T]: InferInput<T[Key]> };
64
+ /** @deprecated */
65
+ type InferPartialRecord<T extends Record<string | number | symbol, unknown>> = { [Key in keyof T]?: InferPartial<T[Key]> };
66
+ /**
67
+ * Extracts the patch representation of a resource.
68
+ * Used by the `buffer.patch` API.
69
+ *
70
+ * @example
71
+ * type A = InferPatch<F32> // => number | undefined
72
+ * type B = InferPatch<WgslStruct<{ a: F32 }>> // => { a?: number | undefined }
73
+ * type C = InferPatch<WgslArray<F32>> // => Record<number, number | undefined> | number[] | undefined
74
+ */
75
+ type InferPatch<T> = T extends {
76
+ readonly [$reprPatch]: infer TRepr;
77
+ } ? TRepr : InferInput<T> | undefined;
78
+ type InferPatchRecord<T extends Record<string | number | symbol, unknown>> = { [Key in keyof T]?: InferPatch<T[Key]> };
79
+ type InferGPURecord<T extends Record<string | number | symbol, unknown>> = { [Key in keyof T]: InferGPU<T[Key]> };
80
+ type GPUValueOf<T> = T extends {
81
+ readonly [$gpuValueOf]: infer TValue;
82
+ } ? TValue : T;
83
+ type MemIdentity<T> = T extends {
84
+ readonly [$memIdent]: infer TMemIdent;
85
+ } ? TMemIdent : T;
86
+ type MemIdentityRecord<T extends Record<string | number | symbol, unknown>> = { [Key in keyof T]: MemIdentity<T[Key]> };
87
+ type IsValidStorageSchema<T> = (T extends {
88
+ readonly [$validStorageSchema]: true;
89
+ } ? true : false) extends false ? false : true;
90
+ type IsValidUniformSchema<T> = (T extends {
91
+ readonly [$validUniformSchema]: true;
92
+ } ? true : false) extends false ? false : true;
93
+ type IsValidVertexSchema<T> = (T extends {
94
+ readonly [$validVertexSchema]: true;
95
+ } ? true : false) extends false ? false : true;
96
+ /**
97
+ * Accepts only arrays (or disarrays) of u32 or u16.
98
+ */
99
+ type IsValidIndexSchema<T> = Undecorate<T> extends WgslArray | Disarray ? Undecorate<Undecorate<T>['elementType']> extends U32 | U16 ? true : false : false;
100
+ /**
101
+ * Checks if a schema can be used in a buffer at all
102
+ */
103
+ type IsValidBufferSchema<T> = IsValidStorageSchema<T> | IsValidUniformSchema<T> | IsValidVertexSchema<T> | IsValidIndexSchema<T> extends false ? false : true;
104
+ /**
105
+ * Validates if a texture can be used as sampled texture
106
+ */
107
+ type IsValidSampledTextureUsage<TTexture extends TgpuTexture> = TTexture['usableAsSampled'] extends true ? true : {
108
+ readonly invalidSampled: "Texture not usable as sampled, call $usage('sampled') first";
109
+ };
110
+ /**
111
+ * Validates if a texture can be used as storage texture
112
+ */
113
+ type IsValidStorageTextureUsage<TTexture extends TgpuTexture> = TTexture['usableAsStorage'] extends true ? true : {
114
+ readonly invalidStorage: "Texture not usable as storage, call $usage('storage') first";
115
+ };
116
+ /**
117
+ * Validates if a texture view dimension is compatible with the texture dimension
118
+ */
119
+ type IsValidSubdimension<TTexture extends TgpuTexture, TSchema extends WgslTexture | WgslStorageTexture> = ViewDimensionToDimension[TSchema['dimension']] extends infer TVDim ? TVDim extends Default<TTexture['props']['dimension'], '2d'> ? true : {
120
+ readonly invalidViewDim: `Texture dimension '${Default<TTexture['props']['dimension'], '2d'>}' incompatible with view dimension '${TSchema['dimension']}'`;
121
+ } : never;
122
+ type IsValidStorageFormat<TTexture extends TgpuTexture, TSchema extends WgslStorageTexture> = TSchema['format'] extends TTexture['props']['format'] ? true : TTexture['props']['viewFormats'] extends readonly unknown[] ? TSchema['format'] extends TTexture['props']['viewFormats'][number] ? true : FormatError<TSchema, TTexture> : FormatError<TSchema, TTexture>;
123
+ type FormatError<TSchema extends WgslStorageTexture, TTexture extends TgpuTexture> = {
124
+ readonly invalidFormat: `Storage texture format '${TSchema['format']}' incompatible with texture format '${TTexture['props']['format']}'`;
125
+ };
126
+ type IsExactly<T, U> = [T] extends [U] ? ([U] extends [T] ? true : false) : false;
127
+ type SelfOrErrors<TSelf, T> = IsExactly<T, true> extends true ? TSelf : `(Error) ${T[Extract<keyof T, `invalid${string}`>] & string}`;
128
+ type ValidStorageUsage<TTexture extends TgpuTexture, TSchema extends WgslStorageTexture> = IsValidStorageTextureUsage<TTexture> & IsValidSubdimension<TTexture, TSchema> & IsValidStorageFormat<TTexture, TSchema>;
129
+ type ValidSampledUsage<TTexture extends TgpuTexture, TSchema extends WgslTexture> = IsValidSampledTextureUsage<TTexture> & IsValidSubdimension<TTexture, TSchema>;
130
+ /**
131
+ * Validates texture view schema against texture usage
132
+ */
133
+ type ValidateTextureViewSchema<TTexture extends TgpuTexture, TSchema extends WgslTexture | WgslStorageTexture> = TSchema extends WgslStorageTexture ? SelfOrErrors<TSchema, ValidStorageUsage<TTexture, TSchema>> : TSchema extends WgslTexture ? SelfOrErrors<TSchema, ValidSampledUsage<TTexture, TSchema>> : never;
134
+ type ExtractInvalidSchemaError<T, TPrefix extends string = ''> = [T] extends [{
135
+ readonly [$invalidSchemaReason]: string;
136
+ }] ? `${TPrefix}${T[typeof $invalidSchemaReason]}` : never;
137
+ //#endregion
138
+ export { ExtractInvalidSchemaError, GPUValueOf, Infer, InferGPU, InferGPURecord, InferInput, InferInputRecord, InferPartial, InferPartialRecord, InferPatch, InferPatchRecord, InferRecord, IsValidBufferSchema, IsValidIndexSchema, IsValidStorageSchema, IsValidUniformSchema, IsValidVertexSchema, MemIdentity, MemIdentityRecord, ValidateTextureViewSchema };
@@ -0,0 +1,20 @@
1
+ import { isMatInstance, isVecInstance } from "../data/wgslTypes.js";
2
+ //#region src/shared/stringify.ts
3
+ function safeStringify(item) {
4
+ const asString = String(item);
5
+ if (asString !== "[object Object]") return asString;
6
+ try {
7
+ return JSON.stringify(item);
8
+ } catch (error) {
9
+ console.error("Error parsing JSON:", error);
10
+ return "<invalid json>";
11
+ }
12
+ }
13
+ function niceStringify(item) {
14
+ if (isVecInstance(item) || isMatInstance(item)) return item.toString();
15
+ if (Array.isArray(item)) return `[${item.map(niceStringify).join(", ")}]`;
16
+ if (item && typeof item === "object") return `{ ${Object.entries(item).map(([key, value]) => `${key}: ${niceStringify(value)}`).join(", ")} }`;
17
+ return String(item);
18
+ }
19
+ //#endregion
20
+ export { niceStringify, safeStringify };
@@ -0,0 +1,70 @@
1
+ //#region src/shared/symbols.d.ts
2
+ declare const $internal: unique symbol;
3
+ /**
4
+ * The getter to the value of this resource, accessible on the GPU
5
+ */
6
+ declare const $gpuValueOf: unique symbol;
7
+ /**
8
+ * If this symbol is present, this means that getName and setName
9
+ * will refer to object behind this property instead.
10
+ */
11
+ declare const $getNameForward: unique symbol;
12
+ /**
13
+ * Marks an object with slot-value bindings
14
+ */
15
+ declare const $providing: unique symbol;
16
+ declare const $resolve: unique symbol;
17
+ /**
18
+ * A way for a schema to provide casting behavior, without the need to be explicitly
19
+ * callable by the end-user (e.g. vertex formats)
20
+ */
21
+ declare const $cast: unique symbol;
22
+ /**
23
+ * Can be called on the GPU
24
+ */
25
+ declare const $gpuCallable: unique symbol;
26
+ /**
27
+ * Type token for the inferred (CPU & GPU) representation of a resource
28
+ */
29
+ declare const $repr: unique symbol;
30
+ /**
31
+ * Type token for the inferred (GPU-side) representation of a resource
32
+ * If present, it shadows the value of `$repr` for GPU-side inference.
33
+ */
34
+ declare const $gpuRepr: unique symbol;
35
+ /**
36
+ * Type token for the inferred partial representation of a resource.
37
+ * If present, it shadows the value of `$repr` for use in partial IO.
38
+ */
39
+ declare const $reprPartial: unique symbol;
40
+ /**
41
+ * Type token for the inferred patch representation of a resource.
42
+ * Used by the `buffer.patch` API with `Record<number, T>` sparse arrays.
43
+ */
44
+ declare const $reprPatch: unique symbol;
45
+ /**
46
+ * Type token for the write-side (input) representation of a resource.
47
+ */
48
+ declare const $inRepr: unique symbol;
49
+ /**
50
+ * Type token holding schemas that are identical in memory layout.
51
+ */
52
+ declare const $memIdent: unique symbol;
53
+ /**
54
+ * Type token, signaling that a schema can be used in a storage buffer.
55
+ */
56
+ declare const $validStorageSchema: unique symbol;
57
+ /**
58
+ * Type token, signaling that a schema can be used in a uniform buffer.
59
+ */
60
+ declare const $validUniformSchema: unique symbol;
61
+ /**
62
+ * Type token, signaling that a schema can be used in a vertex buffer.
63
+ */
64
+ declare const $validVertexSchema: unique symbol;
65
+ /**
66
+ * Type token, containing a reason for why the schema is invalid (if it is).
67
+ */
68
+ declare const $invalidSchemaReason: unique symbol;
69
+ //#endregion
70
+ export { $cast, $getNameForward, $gpuCallable, $gpuRepr, $gpuValueOf, $inRepr, $internal, $invalidSchemaReason, $memIdent, $providing, $repr, $reprPartial, $reprPatch, $resolve, $validStorageSchema, $validUniformSchema, $validVertexSchema };
@@ -0,0 +1,48 @@
1
+ import { version } from "../package.js";
2
+ //#region src/shared/symbols.ts
3
+ const $internal = Symbol(`typegpu:${version}:$internal`);
4
+ /**
5
+ * The getter to the value of this resource, accessible on the GPU
6
+ */
7
+ const $gpuValueOf = Symbol(`typegpu:${version}:$gpuValueOf`);
8
+ /**
9
+ * If this symbol is present, this means that getName and setName
10
+ * will refer to object behind this property instead.
11
+ */
12
+ const $getNameForward = Symbol(`typegpu:${version}:$getNameForward`);
13
+ /**
14
+ * Marks an object with slot-value bindings
15
+ */
16
+ const $providing = Symbol(`typegpu:${version}:$providing`);
17
+ /**
18
+ * Objects can provide the snippet that represents them.
19
+ */
20
+ const $ownSnippet = Symbol(`typegpu:${version}:$ownSnippet`);
21
+ const $resolve = Symbol(`typegpu:${version}:$resolve`);
22
+ /**
23
+ * A way for a schema to provide casting behavior, without the need to be explicitly
24
+ * callable by the end-user (e.g. vertex formats)
25
+ */
26
+ const $cast = Symbol(`typegpu:${version}:$cast`);
27
+ /**
28
+ * Can be called on the GPU
29
+ */
30
+ const $gpuCallable = Symbol(`typegpu:${version}:$gpuCallable`);
31
+ /**
32
+ * Type token for the inferred (CPU & GPU) representation of a resource
33
+ */
34
+ const $repr = Symbol(`typegpu:${version}:$repr`);
35
+ Symbol(`typegpu:${version}:$gpuRepr`);
36
+ Symbol(`typegpu:${version}:$reprPartial`);
37
+ Symbol(`typegpu:${version}:$reprPatch`);
38
+ Symbol(`typegpu:${version}:$inRepr`);
39
+ Symbol(`typegpu:${version}:$memIdent`);
40
+ Symbol(`typegpu:${version}:$invalidStorageSchema`);
41
+ Symbol(`typegpu:${version}:$validUniformSchema`);
42
+ Symbol(`typegpu:${version}:$validVertexSchema`);
43
+ Symbol(`typegpu:${version}:$invalidSchemaReason`);
44
+ function isMarkedInternal(value) {
45
+ return !!value?.[$internal];
46
+ }
47
+ //#endregion
48
+ export { $cast, $getNameForward, $gpuCallable, $gpuValueOf, $internal, $ownSnippet, $providing, $repr, $resolve, isMarkedInternal };
@@ -0,0 +1,33 @@
1
+ //#region src/shared/utilityTypes.d.ts
2
+ type Default<T, TDefault> = unknown extends T ? TDefault : T extends undefined ? TDefault : T;
3
+ type SwapNever<T, Replacement> = [T] extends [never] ? Replacement : T;
4
+ type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends ((x: infer I) => void) ? I : never;
5
+ type Prettify<T> = { [K in keyof T]: T[K] } & {};
6
+ /**
7
+ * Utility type that merges a partial type with defaults, where defaults are used
8
+ * for properties not present in the partial type.
9
+ */
10
+ type WithDefaults<TPartial, TDefaults> = Omit<TDefaults, keyof TPartial> & TPartial;
11
+ /**
12
+ * Removes properties from record type that extend `Prop`
13
+ */
14
+ type OmitProps<T extends Record<string, unknown>, Prop> = Pick<T, { [Key in keyof T]: T[Key] extends Prop ? never : Key }[keyof T]>;
15
+ type NullableToOptional<T> = { [K in keyof T as T[K] extends null ? K : never]?: T[K] } & { [K in keyof T as T[K] extends null ? never : K]: T[K] };
16
+ /**
17
+ * The opposite of Readonly<T>
18
+ */
19
+ type Mutable<T> = { -readonly [P in keyof T]: T[P] };
20
+ /**
21
+ * Source: https://code.lol/post/programming/higher-kinded-types
22
+ */
23
+ type Assume<T, U> = T extends U ? T : U;
24
+ /**
25
+ * Any typed array
26
+ */
27
+ type TypedArray = Uint8Array | Uint16Array | Uint32Array | Int32Array | Float32Array | Float64Array;
28
+ /**
29
+ * Source: https://futurestud.io/tutorials/typescript-how-to-remove-index-signature-from-a-type
30
+ */
31
+ type RemoveIndexSignature<T> = { [K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: T[K] };
32
+ //#endregion
33
+ export { Assume, Default, Mutable, NullableToOptional, OmitProps, Prettify, RemoveIndexSignature, SwapNever, TypedArray, UnionToIntersection, WithDefaults };
@@ -0,0 +1,6 @@
1
+ //#region src/shared/utilityTypes.ts
2
+ function assertExhaustive(x, location) {
3
+ throw new Error(`Failed to handle ${x} at ${location}`);
4
+ }
5
+ //#endregion
6
+ export { assertExhaustive };
@@ -0,0 +1,70 @@
1
+ //#region src/shared/vertexFormat.d.ts
2
+ declare const vertexFormats: readonly ["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"];
3
+ type VertexFormat = (typeof vertexFormats)[number];
4
+ declare const kindToDefaultFormatMap: {
5
+ readonly f32: "float32";
6
+ readonly vec2f: "float32x2";
7
+ readonly vec3f: "float32x3";
8
+ readonly vec4f: "float32x4";
9
+ readonly f16: "float16";
10
+ readonly vec2h: "float16x2";
11
+ readonly vec4h: "float16x4";
12
+ readonly u32: "uint32";
13
+ readonly vec2u: "uint32x2";
14
+ readonly vec3u: "uint32x3";
15
+ readonly vec4u: "uint32x4";
16
+ readonly i32: "sint32";
17
+ readonly vec2i: "sint32x2";
18
+ readonly vec3i: "sint32x3";
19
+ readonly vec4i: "sint32x4";
20
+ };
21
+ type KindToDefaultFormatMap = typeof kindToDefaultFormatMap;
22
+ interface TgpuVertexAttrib<TFormat extends VertexFormat = VertexFormat> {
23
+ readonly format: TFormat;
24
+ readonly offset: number;
25
+ }
26
+ type AnyVertexAttribs = Record<string, TgpuVertexAttrib> | TgpuVertexAttrib;
27
+ /**
28
+ * All vertex attribute formats that can be interpreted as
29
+ * an single or multi component u32 in a shader.
30
+ * https://www.w3.org/TR/webgpu/#vertex-formats
31
+ */
32
+ type U32CompatibleFormats = TgpuVertexAttrib<'uint8'> | TgpuVertexAttrib<'uint8x2'> | TgpuVertexAttrib<'uint8x4'> | TgpuVertexAttrib<'uint16'> | TgpuVertexAttrib<'uint16x2'> | TgpuVertexAttrib<'uint16x4'> | TgpuVertexAttrib<'uint32'> | TgpuVertexAttrib<'uint32x2'> | TgpuVertexAttrib<'uint32x3'> | TgpuVertexAttrib<'uint32x4'>;
33
+ /**
34
+ * All vertex attribute formats that can be interpreted as
35
+ * an single or multi component i32 in a shader.
36
+ * https://www.w3.org/TR/webgpu/#vertex-formats
37
+ */
38
+ type I32CompatibleFormats = TgpuVertexAttrib<'sint8'> | TgpuVertexAttrib<'sint8x2'> | TgpuVertexAttrib<'sint8x4'> | TgpuVertexAttrib<'sint16'> | TgpuVertexAttrib<'sint16x2'> | TgpuVertexAttrib<'sint16x4'> | TgpuVertexAttrib<'sint32'> | TgpuVertexAttrib<'sint32x2'> | TgpuVertexAttrib<'sint32x3'> | TgpuVertexAttrib<'sint32x4'>;
39
+ /**
40
+ * All vertex attribute formats that can be interpreted as
41
+ * an single or multi component f32 in a shader.
42
+ * https://www.w3.org/TR/webgpu/#vertex-formats
43
+ */
44
+ type F32CompatibleFormats = TgpuVertexAttrib<'unorm8'> | TgpuVertexAttrib<'unorm8x2'> | TgpuVertexAttrib<'unorm8x4'> | TgpuVertexAttrib<'snorm8'> | TgpuVertexAttrib<'snorm8x2'> | TgpuVertexAttrib<'snorm8x4'> | TgpuVertexAttrib<'unorm16'> | TgpuVertexAttrib<'unorm16x2'> | TgpuVertexAttrib<'unorm16x4'> | TgpuVertexAttrib<'snorm16'> | TgpuVertexAttrib<'snorm16x2'> | TgpuVertexAttrib<'snorm16x4'> | TgpuVertexAttrib<'float16'> | TgpuVertexAttrib<'float16x2'> | TgpuVertexAttrib<'float16x4'> | TgpuVertexAttrib<'float32'> | TgpuVertexAttrib<'float32x2'> | TgpuVertexAttrib<'float32x3'> | TgpuVertexAttrib<'float32x4'> | TgpuVertexAttrib<'unorm10-10-10-2'> | TgpuVertexAttrib<'unorm8x4-bgra'>;
45
+ /**
46
+ * All vertex attribute formats that can be interpreted as
47
+ * a single or multi component f16 in a shader. (same as f32 on the shader side)
48
+ * https://www.w3.org/TR/webgpu/#vertex-formats
49
+ */
50
+ type F16CompatibleFormats = F32CompatibleFormats;
51
+ type KindToAcceptedAttribMap = {
52
+ u32: U32CompatibleFormats;
53
+ vec2u: U32CompatibleFormats;
54
+ vec3u: U32CompatibleFormats;
55
+ vec4u: U32CompatibleFormats;
56
+ i32: I32CompatibleFormats;
57
+ vec2i: I32CompatibleFormats;
58
+ vec3i: I32CompatibleFormats;
59
+ vec4i: I32CompatibleFormats;
60
+ f16: F16CompatibleFormats;
61
+ vec2h: F16CompatibleFormats;
62
+ vec3h: F16CompatibleFormats;
63
+ vec4h: F16CompatibleFormats;
64
+ f32: F32CompatibleFormats;
65
+ vec2f: F32CompatibleFormats;
66
+ vec3f: F32CompatibleFormats;
67
+ vec4f: F32CompatibleFormats;
68
+ };
69
+ //#endregion
70
+ export { AnyVertexAttribs, KindToAcceptedAttribMap, KindToDefaultFormatMap, TgpuVertexAttrib, VertexFormat };
@@ -0,0 +1,63 @@
1
+ //#region src/shared/vertexFormat.ts
2
+ const vertexFormats = [
3
+ "uint8",
4
+ "uint8x2",
5
+ "uint8x4",
6
+ "sint8",
7
+ "sint8x2",
8
+ "sint8x4",
9
+ "unorm8",
10
+ "unorm8x2",
11
+ "unorm8x4",
12
+ "snorm8",
13
+ "snorm8x2",
14
+ "snorm8x4",
15
+ "uint16",
16
+ "uint16x2",
17
+ "uint16x4",
18
+ "sint16",
19
+ "sint16x2",
20
+ "sint16x4",
21
+ "unorm16",
22
+ "unorm16x2",
23
+ "unorm16x4",
24
+ "snorm16",
25
+ "snorm16x2",
26
+ "snorm16x4",
27
+ "float16",
28
+ "float16x2",
29
+ "float16x4",
30
+ "float32",
31
+ "float32x2",
32
+ "float32x3",
33
+ "float32x4",
34
+ "uint32",
35
+ "uint32x2",
36
+ "uint32x3",
37
+ "uint32x4",
38
+ "sint32",
39
+ "sint32x2",
40
+ "sint32x3",
41
+ "sint32x4",
42
+ "unorm10-10-10-2",
43
+ "unorm8x4-bgra"
44
+ ];
45
+ const kindToDefaultFormatMap = {
46
+ f32: "float32",
47
+ vec2f: "float32x2",
48
+ vec3f: "float32x3",
49
+ vec4f: "float32x4",
50
+ f16: "float16",
51
+ vec2h: "float16x2",
52
+ vec4h: "float16x4",
53
+ u32: "uint32",
54
+ vec2u: "uint32x2",
55
+ vec3u: "uint32x3",
56
+ vec4u: "uint32x4",
57
+ i32: "sint32",
58
+ vec2i: "sint32x2",
59
+ vec3i: "sint32x3",
60
+ vec4i: "sint32x4"
61
+ };
62
+ //#endregion
63
+ export { kindToDefaultFormatMap, vertexFormats };
package/std/array.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { DualFn } from "../types.js";
2
+ import { _ref } from "../data/ref.js";
3
+
4
+ //#region src/std/array.d.ts
5
+ declare const arrayLength: DualFn;
6
+ //#endregion
7
+ export { arrayLength };
package/std/array.js ADDED
@@ -0,0 +1,25 @@
1
+ import { isPtr, isWgslArray } from "../data/wgslTypes.js";
2
+ import { stitch } from "../core/resolve/stitch.js";
3
+ import { ptrFn } from "../data/ptr.js";
4
+ import { isRef } from "../data/ref.js";
5
+ import { abstractInt, u32 } from "../data/numeric.js";
6
+ import { dualImpl } from "../core/function/dualImpl.js";
7
+ //#region src/std/array.ts
8
+ const sizeOfPointedToArray = (dataType) => isPtr(dataType) && isWgslArray(dataType.inner) ? dataType.inner.elementCount : 0;
9
+ const arrayLength = dualImpl({
10
+ name: "arrayLength",
11
+ signature: (arg) => {
12
+ const ptrArg = isPtr(arg) ? arg : ptrFn(arg);
13
+ return {
14
+ argTypes: [ptrArg],
15
+ returnType: sizeOfPointedToArray(ptrArg) > 0 ? abstractInt : u32
16
+ };
17
+ },
18
+ normalImpl: (a) => isRef(a) ? a.$.length : a.length,
19
+ codegenImpl(_ctx, [a]) {
20
+ const length = sizeOfPointedToArray(a.dataType);
21
+ return length > 0 ? `${length}` : stitch`arrayLength(${a})`;
22
+ }
23
+ });
24
+ //#endregion
25
+ export { arrayLength };
@@ -0,0 +1,19 @@
1
+ import { DualFn } from "../types.js";
2
+ import { atomicI32, atomicU32 } from "../data/wgslTypes.js";
3
+
4
+ //#region src/std/atomic.d.ts
5
+ type AnyAtomic = atomicI32 | atomicU32;
6
+ declare const workgroupBarrier: DualFn;
7
+ declare const storageBarrier: DualFn;
8
+ declare const textureBarrier: DualFn;
9
+ declare const atomicLoad: DualFn;
10
+ declare const atomicStore: DualFn;
11
+ declare const atomicAdd: DualFn;
12
+ declare const atomicSub: DualFn;
13
+ declare const atomicMax: DualFn;
14
+ declare const atomicMin: DualFn;
15
+ declare const atomicAnd: DualFn;
16
+ declare const atomicOr: DualFn;
17
+ declare const atomicXor: DualFn;
18
+ //#endregion
19
+ export { atomicAdd, atomicAnd, atomicLoad, atomicMax, atomicMin, atomicOr, atomicStore, atomicSub, atomicXor, storageBarrier, textureBarrier, workgroupBarrier };