typegpu 0.10.1 → 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (263) hide show
  1. package/{chunk-BYypO7fO.js → _virtual/_rolldown/runtime.js} +1 -1
  2. package/builtin.d.ts +50 -0
  3. package/builtin.js +37 -0
  4. package/common/fullScreenTriangle.d.ts +26 -0
  5. package/common/fullScreenTriangle.js +36 -0
  6. package/common/index.d.ts +2 -3
  7. package/common/index.js +3 -4
  8. package/core/buffer/buffer.d.ts +74 -0
  9. package/core/buffer/buffer.js +197 -0
  10. package/core/buffer/bufferShorthand.d.ts +48 -0
  11. package/core/buffer/bufferShorthand.js +49 -0
  12. package/core/buffer/bufferUsage.d.ts +45 -0
  13. package/core/buffer/bufferUsage.js +163 -0
  14. package/core/constant/tgpuConstant.d.ts +28 -0
  15. package/core/constant/tgpuConstant.js +67 -0
  16. package/core/declare/tgpuDeclare.d.ts +18 -0
  17. package/core/declare/tgpuDeclare.js +40 -0
  18. package/core/function/autoIO.d.ts +37 -0
  19. package/core/function/autoIO.js +87 -0
  20. package/core/function/comptime.d.ts +39 -0
  21. package/core/function/comptime.js +51 -0
  22. package/core/function/createCallableSchema.js +42 -0
  23. package/core/function/dualImpl.js +54 -0
  24. package/core/function/extractArgs.js +204 -0
  25. package/core/function/fnCore.js +79 -0
  26. package/core/function/fnTypes.d.ts +34 -0
  27. package/core/function/ioSchema.d.ts +10 -0
  28. package/core/function/ioSchema.js +30 -0
  29. package/core/function/shelllessImpl.d.ts +28 -0
  30. package/core/function/shelllessImpl.js +23 -0
  31. package/core/function/templateUtils.js +13 -0
  32. package/core/function/tgpuComputeFn.d.ts +49 -0
  33. package/core/function/tgpuComputeFn.js +62 -0
  34. package/core/function/tgpuFn.d.ts +52 -0
  35. package/core/function/tgpuFn.js +170 -0
  36. package/core/function/tgpuFragmentFn.d.ts +68 -0
  37. package/core/function/tgpuFragmentFn.js +68 -0
  38. package/core/function/tgpuVertexFn.d.ts +55 -0
  39. package/core/function/tgpuVertexFn.js +65 -0
  40. package/core/pipeline/applyPipelineState.js +37 -0
  41. package/core/pipeline/computePipeline.d.ts +58 -0
  42. package/core/pipeline/computePipeline.js +226 -0
  43. package/core/pipeline/connectAttachmentToShader.js +26 -0
  44. package/core/pipeline/connectTargetsToShader.js +29 -0
  45. package/core/pipeline/limitsOverflow.js +13 -0
  46. package/core/pipeline/renderPipeline.d.ts +266 -0
  47. package/core/pipeline/renderPipeline.js +471 -0
  48. package/core/pipeline/timeable.d.ts +23 -0
  49. package/core/pipeline/timeable.js +61 -0
  50. package/core/pipeline/typeGuards.js +29 -0
  51. package/core/querySet/querySet.d.ts +22 -0
  52. package/core/querySet/querySet.js +103 -0
  53. package/core/rawCodeSnippet/tgpuRawCodeSnippet.d.ts +59 -0
  54. package/core/rawCodeSnippet/tgpuRawCodeSnippet.js +96 -0
  55. package/core/resolve/externals.d.ts +10 -0
  56. package/core/resolve/externals.js +58 -0
  57. package/core/resolve/namespace.d.ts +38 -0
  58. package/core/resolve/namespace.js +41 -0
  59. package/core/resolve/resolveData.js +146 -0
  60. package/core/resolve/stitch.js +25 -0
  61. package/core/resolve/tgpuResolve.d.ts +151 -0
  62. package/core/resolve/tgpuResolve.js +68 -0
  63. package/core/root/configurableImpl.js +18 -0
  64. package/core/root/init.d.ts +69 -0
  65. package/core/root/init.js +457 -0
  66. package/core/root/rootTypes.d.ts +622 -0
  67. package/core/sampler/sampler.d.ts +35 -0
  68. package/core/sampler/sampler.js +116 -0
  69. package/core/simulate/tgpuSimulate.d.ts +36 -0
  70. package/core/simulate/tgpuSimulate.js +76 -0
  71. package/core/slot/accessor.d.ts +13 -0
  72. package/core/slot/accessor.js +97 -0
  73. package/core/slot/internalSlots.js +7 -0
  74. package/core/slot/lazy.d.ts +6 -0
  75. package/core/slot/lazy.js +42 -0
  76. package/core/slot/slot.d.ts +6 -0
  77. package/core/slot/slot.js +40 -0
  78. package/core/slot/slotTypes.d.ts +92 -0
  79. package/core/slot/slotTypes.js +21 -0
  80. package/core/texture/externalTexture.d.ts +12 -0
  81. package/core/texture/externalTexture.js +48 -0
  82. package/core/texture/texture.d.ts +118 -0
  83. package/core/texture/texture.js +312 -0
  84. package/core/texture/textureFormats.d.ts +29 -0
  85. package/core/texture/textureFormats.js +99 -0
  86. package/core/texture/textureProps.d.ts +11 -0
  87. package/core/texture/textureUtils.js +224 -0
  88. package/core/texture/usageExtension.d.ts +21 -0
  89. package/core/texture/usageExtension.js +21 -0
  90. package/core/unroll/tgpuUnroll.d.ts +13 -0
  91. package/core/unroll/tgpuUnroll.js +36 -0
  92. package/core/valueProxyUtils.js +44 -0
  93. package/core/variable/tgpuVariable.d.ts +38 -0
  94. package/core/variable/tgpuVariable.js +101 -0
  95. package/core/vertexLayout/connectAttributesToShader.js +59 -0
  96. package/core/vertexLayout/vertexAttribute.d.ts +29 -0
  97. package/core/vertexLayout/vertexLayout.d.ts +19 -0
  98. package/core/vertexLayout/vertexLayout.js +103 -0
  99. package/data/alignIO.js +15 -0
  100. package/data/alignmentOf.d.ts +10 -0
  101. package/data/alignmentOf.js +88 -0
  102. package/data/array.d.ts +28 -0
  103. package/data/array.js +48 -0
  104. package/data/atomic.d.ts +15 -0
  105. package/data/atomic.js +25 -0
  106. package/data/attributes.d.ts +121 -0
  107. package/data/attributes.js +145 -0
  108. package/data/autoStruct.d.ts +3 -0
  109. package/data/autoStruct.js +83 -0
  110. package/data/compiledIO.js +231 -0
  111. package/data/dataIO.js +549 -0
  112. package/data/dataTypes.d.ts +115 -0
  113. package/data/dataTypes.js +97 -0
  114. package/data/deepEqual.d.ts +25 -0
  115. package/data/deepEqual.js +58 -0
  116. package/data/disarray.d.ts +34 -0
  117. package/data/disarray.js +52 -0
  118. package/data/getLongestContiguousPrefix.d.ts +10 -0
  119. package/data/getLongestContiguousPrefix.js +15 -0
  120. package/data/index.d.ts +26 -4
  121. package/data/index.js +27 -7
  122. package/data/instanceToSchema.d.ts +33 -0
  123. package/data/isContiguous.d.ts +10 -0
  124. package/data/isContiguous.js +15 -0
  125. package/data/matrix.d.ts +126 -0
  126. package/data/matrix.js +517 -0
  127. package/data/numberOps.js +24 -0
  128. package/data/numeric.d.ts +81 -0
  129. package/data/numeric.js +234 -0
  130. package/data/offsetUtils.d.ts +33 -0
  131. package/data/offsetUtils.js +167 -0
  132. package/data/offsets.js +36 -0
  133. package/data/partialIO.js +68 -0
  134. package/data/ptr.d.ts +12 -0
  135. package/data/ptr.js +46 -0
  136. package/data/ref.d.ts +37 -0
  137. package/data/ref.js +96 -0
  138. package/data/sampler.d.ts +107 -0
  139. package/data/sampler.js +26 -0
  140. package/data/schemaCallWrapper.js +32 -0
  141. package/data/schemaMemoryLayout.js +200 -0
  142. package/data/sizeOf.d.ts +10 -0
  143. package/data/sizeOf.js +15 -0
  144. package/data/snippet.d.ts +26 -0
  145. package/data/snippet.js +61 -0
  146. package/data/struct.d.ts +17 -0
  147. package/data/struct.js +46 -0
  148. package/data/texture.d.ts +292 -0
  149. package/{texture-BagDrrks.js → data/texture.js} +6 -3
  150. package/data/unstruct.d.ts +24 -0
  151. package/data/unstruct.js +43 -0
  152. package/data/vector.d.ts +191 -0
  153. package/data/vector.js +247 -0
  154. package/data/vectorImpl.js +516 -0
  155. package/data/vectorOps.js +664 -0
  156. package/data/vertexFormatData.d.ts +190 -0
  157. package/data/vertexFormatData.js +110 -0
  158. package/data/wgslTypes.d.ts +896 -0
  159. package/data/wgslTypes.js +215 -0
  160. package/errors.d.ts +44 -0
  161. package/errors.js +128 -0
  162. package/execMode.js +51 -0
  163. package/extension.d.ts +11 -0
  164. package/extension.js +18 -0
  165. package/getGPUValue.js +9 -0
  166. package/index.d.ts +40 -243
  167. package/index.js +19 -6318
  168. package/indexNamedExports.d.ts +38 -0
  169. package/mathUtils.js +13 -0
  170. package/memo.js +22 -0
  171. package/nameRegistry.d.ts +30 -0
  172. package/nameRegistry.js +449 -0
  173. package/package.js +5 -0
  174. package/package.json +23 -23
  175. package/resolutionCtx.d.ts +29 -0
  176. package/resolutionCtx.js +546 -0
  177. package/shared/env.js +13 -0
  178. package/shared/generators.js +14 -0
  179. package/shared/meta.d.ts +39 -0
  180. package/shared/meta.js +63 -0
  181. package/shared/repr.d.ts +108 -0
  182. package/shared/stringify.js +22 -0
  183. package/shared/symbols.d.ts +61 -0
  184. package/shared/symbols.js +71 -0
  185. package/shared/utilityTypes.d.ts +29 -0
  186. package/shared/utilityTypes.js +7 -0
  187. package/shared/vertexFormat.d.ts +70 -0
  188. package/shared/vertexFormat.js +64 -0
  189. package/std/array.d.ts +7 -0
  190. package/std/array.js +27 -0
  191. package/std/atomic.d.ts +19 -0
  192. package/std/atomic.js +113 -0
  193. package/std/bitcast.d.ts +10 -0
  194. package/std/bitcast.js +43 -0
  195. package/std/boolean.d.ts +127 -0
  196. package/std/boolean.js +274 -0
  197. package/std/derivative.d.ts +16 -0
  198. package/std/derivative.js +89 -0
  199. package/std/discard.d.ts +6 -0
  200. package/std/discard.js +16 -0
  201. package/std/extensions.d.ts +8 -0
  202. package/std/extensions.js +14 -0
  203. package/std/index.d.ts +15 -3
  204. package/std/index.js +16 -5
  205. package/std/matrix.d.ts +41 -0
  206. package/std/matrix.js +87 -0
  207. package/std/numeric.d.ts +254 -0
  208. package/std/numeric.js +847 -0
  209. package/std/operators.d.ts +48 -0
  210. package/std/operators.js +153 -0
  211. package/std/packing.d.ts +26 -0
  212. package/std/packing.js +86 -0
  213. package/std/subgroup.d.ts +47 -0
  214. package/std/subgroup.js +220 -0
  215. package/std/texture.d.ts +108 -0
  216. package/std/texture.js +197 -0
  217. package/tgpu.js +44 -0
  218. package/tgpuBindGroupLayout.d.ts +161 -0
  219. package/tgpuBindGroupLayout.js +271 -0
  220. package/tgpuUnstable.d.ts +48 -0
  221. package/tgpuUnstable.js +66 -0
  222. package/tgsl/accessIndex.js +45 -0
  223. package/tgsl/accessProp.js +113 -0
  224. package/tgsl/consoleLog/deserializers.js +117 -0
  225. package/tgsl/consoleLog/logGenerator.js +86 -0
  226. package/tgsl/consoleLog/serializers.js +225 -0
  227. package/tgsl/consoleLog/types.d.ts +54 -0
  228. package/tgsl/consoleLog/types.js +12 -0
  229. package/tgsl/conversion.js +200 -0
  230. package/tgsl/forOfUtils.js +45 -0
  231. package/tgsl/generationHelpers.d.ts +37 -0
  232. package/tgsl/generationHelpers.js +67 -0
  233. package/tgsl/math.js +45 -0
  234. package/tgsl/shaderGenerator.d.ts +18 -0
  235. package/tgsl/shellless.d.ts +11 -0
  236. package/tgsl/shellless.js +53 -0
  237. package/tgsl/wgslGenerator.js +585 -0
  238. package/types.d.ts +255 -0
  239. package/types.js +43 -0
  240. package/unwrapper.d.ts +27 -0
  241. package/wgslExtensions.d.ts +5 -0
  242. package/wgslExtensions.js +18 -0
  243. package/builtin-DdtWpk2t.js +0 -818
  244. package/builtin-DdtWpk2t.js.map +0 -1
  245. package/common/index.d.ts.map +0 -1
  246. package/common/index.js.map +0 -1
  247. package/data/index.d.ts.map +0 -1
  248. package/data/index.js.map +0 -1
  249. package/deepEqual-DQxK4vdp.js +0 -413
  250. package/deepEqual-DQxK4vdp.js.map +0 -1
  251. package/extensions-DIVuAfBM.js +0 -2032
  252. package/extensions-DIVuAfBM.js.map +0 -1
  253. package/fullScreenTriangle-CfFyQd_0.js +0 -543
  254. package/fullScreenTriangle-CfFyQd_0.js.map +0 -1
  255. package/index.d.ts.map +0 -1
  256. package/index.js.map +0 -1
  257. package/indexNamedExports-oL6tyaJ9.d.ts +0 -5697
  258. package/indexNamedExports-oL6tyaJ9.d.ts.map +0 -1
  259. package/operators-d-PMVTo7.js +0 -4158
  260. package/operators-d-PMVTo7.js.map +0 -1
  261. package/std/index.d.ts.map +0 -1
  262. package/std/index.js.map +0 -1
  263. package/texture-BagDrrks.js.map +0 -1
@@ -0,0 +1,457 @@
1
+ import { $getNameForward, $internal } from "../../shared/symbols.js";
2
+ import { setName } from "../../shared/meta.js";
3
+ import { invariant } from "../../errors.js";
4
+ import { isAccessor, isMutableAccessor } from "../slot/slotTypes.js";
5
+ import { u32 } from "../../data/numeric.js";
6
+ import { vec3f, vec3u } from "../../data/vector.js";
7
+ import { fn } from "../function/tgpuFn.js";
8
+ import { ConfigurableImpl } from "./configurableImpl.js";
9
+ import { INTERNAL_createBuffer, isBuffer } from "../buffer/buffer.js";
10
+ import { INTERNAL_createComparisonSampler, INTERNAL_createSampler, isComparisonSampler, isSampler } from "../sampler/sampler.js";
11
+ import { clearTextureUtilsCache } from "../texture/textureUtils.js";
12
+ import { INTERNAL_createTexture, isTexture, isTextureView } from "../texture/texture.js";
13
+ import { TgpuBindGroupImpl, isBindGroup, isBindGroupLayout } from "../../tgpuBindGroupLayout.js";
14
+ import { ceil } from "../../std/numeric.js";
15
+ import { isComputePipeline, isRenderPipeline } from "../pipeline/typeGuards.js";
16
+ import { builtin } from "../../builtin.js";
17
+ import { INTERNAL_createQuerySet, isQuerySet } from "../querySet/querySet.js";
18
+ import { WeakMemo } from "../../memo.js";
19
+ import { TgpuBufferShorthandImpl } from "../buffer/bufferShorthand.js";
20
+ import { computeFn } from "../function/tgpuComputeFn.js";
21
+ import { applyBindGroups, applyVertexBuffers } from "../pipeline/applyPipelineState.js";
22
+ import { INTERNAL_createComputePipeline } from "../pipeline/computePipeline.js";
23
+ import { isVertexLayout } from "../vertexLayout/vertexLayout.js";
24
+ import { INTERNAL_createRenderPipeline } from "../pipeline/renderPipeline.js";
25
+ import { allEq } from "../../std/boolean.js";
26
+
27
+ //#region src/core/root/init.ts
28
+ /**
29
+ * Changes the given array to a vec of 3 numbers, filling missing values with 1.
30
+ */
31
+ function toVec3(arr) {
32
+ if (arr.includes(0)) throw new Error("Size and workgroupSize cannot contain zeroes.");
33
+ return vec3u(arr[0] ?? 1, arr[1] ?? 1, arr[2] ?? 1);
34
+ }
35
+ const workgroupSizeConfigs = [
36
+ vec3u(1, 1, 1),
37
+ vec3u(256, 1, 1),
38
+ vec3u(16, 16, 1),
39
+ vec3u(8, 8, 4)
40
+ ];
41
+ var TgpuGuardedComputePipelineImpl = class TgpuGuardedComputePipelineImpl {
42
+ #root;
43
+ #pipeline;
44
+ #sizeUniform;
45
+ #workgroupSize;
46
+ #lastSize;
47
+ constructor(root, pipeline, sizeUniform, workgroupSize) {
48
+ this.#root = root;
49
+ this.#pipeline = pipeline;
50
+ this.#sizeUniform = sizeUniform;
51
+ this.#workgroupSize = workgroupSize;
52
+ this.#lastSize = vec3u();
53
+ }
54
+ with(bindGroup) {
55
+ return new TgpuGuardedComputePipelineImpl(this.#root, this.#pipeline.with(bindGroup), this.#sizeUniform, this.#workgroupSize);
56
+ }
57
+ dispatchThreads(...threads) {
58
+ const sanitizedSize = toVec3(threads);
59
+ const workgroupCount = ceil(vec3f(sanitizedSize).div(vec3f(this.#workgroupSize)));
60
+ if (!allEq(sanitizedSize, this.#lastSize)) {
61
+ this.#lastSize = sanitizedSize;
62
+ this.#sizeUniform.write(sanitizedSize);
63
+ }
64
+ this.#pipeline.dispatchWorkgroups(workgroupCount.x, workgroupCount.y, workgroupCount.z);
65
+ }
66
+ get pipeline() {
67
+ return this.#pipeline;
68
+ }
69
+ get sizeUniform() {
70
+ return this.#sizeUniform;
71
+ }
72
+ [$internal] = true;
73
+ get [$getNameForward]() {
74
+ return this.#pipeline;
75
+ }
76
+ $name(label) {
77
+ setName(this, label);
78
+ return this;
79
+ }
80
+ };
81
+ var WithBindingImpl = class WithBindingImpl {
82
+ constructor(_getRoot, _slotBindings) {
83
+ this._getRoot = _getRoot;
84
+ this._slotBindings = _slotBindings;
85
+ }
86
+ with(slot, value) {
87
+ return new WithBindingImpl(this._getRoot, [...this._slotBindings, [isAccessor(slot) || isMutableAccessor(slot) ? slot.slot : slot, value]]);
88
+ }
89
+ withCompute(entryFn) {
90
+ return new WithComputeImpl(this._getRoot(), this._slotBindings, entryFn);
91
+ }
92
+ createComputePipeline(descriptor) {
93
+ return INTERNAL_createComputePipeline(this._getRoot(), this._slotBindings, descriptor);
94
+ }
95
+ createRenderPipeline(descriptor) {
96
+ return INTERNAL_createRenderPipeline({
97
+ root: this._getRoot(),
98
+ slotBindings: this._slotBindings,
99
+ descriptor
100
+ });
101
+ }
102
+ createGuardedComputePipeline(callback) {
103
+ const root = this._getRoot();
104
+ if (callback.length >= 4) throw new Error("Guarded compute callback only supports up to three dimensions.");
105
+ const workgroupSize = workgroupSizeConfigs[callback.length];
106
+ const wrappedCallback = fn([
107
+ u32,
108
+ u32,
109
+ u32
110
+ ])(callback);
111
+ const sizeUniform = root.createUniform(vec3u);
112
+ const mainCompute = computeFn({
113
+ workgroupSize,
114
+ in: { id: builtin.globalInvocationId }
115
+ })`{
116
+ if (any(in.id >= sizeUniform)) {
117
+ return;
118
+ }
119
+ wrappedCallback(in.id.x, in.id.y, in.id.z);
120
+ }`.$uses({
121
+ sizeUniform,
122
+ wrappedCallback
123
+ });
124
+ return new TgpuGuardedComputePipelineImpl(root, this.createComputePipeline({ compute: mainCompute }), sizeUniform, workgroupSize);
125
+ }
126
+ withVertex(entryFn, attribs) {
127
+ return new WithVertexImpl(this._getRoot(), this._slotBindings, {
128
+ vertex: entryFn,
129
+ attribs
130
+ });
131
+ }
132
+ pipe(transform) {
133
+ const newCfg = transform(new ConfigurableImpl([]));
134
+ return new WithBindingImpl(this._getRoot, [...this._slotBindings, ...newCfg.bindings]);
135
+ }
136
+ };
137
+ var WithComputeImpl = class {
138
+ constructor(_root, _slotBindings, _entryFn) {
139
+ this._root = _root;
140
+ this._slotBindings = _slotBindings;
141
+ this._entryFn = _entryFn;
142
+ }
143
+ createPipeline() {
144
+ return INTERNAL_createComputePipeline(this._root, this._slotBindings, { compute: this._entryFn });
145
+ }
146
+ };
147
+ var WithVertexImpl = class WithVertexImpl {
148
+ #root;
149
+ #slotBindings;
150
+ #partialDescriptor;
151
+ constructor(root, slotBindings, partialDescriptor) {
152
+ this.#root = root;
153
+ this.#slotBindings = slotBindings;
154
+ this.#partialDescriptor = partialDescriptor;
155
+ }
156
+ withFragment(fragmentFn, targets, _mismatch) {
157
+ invariant(typeof fragmentFn !== "string", "Just type mismatch validation");
158
+ invariant(targets === void 0 || typeof targets !== "string", "Just type mismatch validation");
159
+ return new WithFragmentImpl(this.#root, this.#slotBindings, {
160
+ ...this.#partialDescriptor,
161
+ fragment: fragmentFn,
162
+ targets
163
+ });
164
+ }
165
+ withPrimitive(primitive) {
166
+ return new WithVertexImpl(this.#root, this.#slotBindings, {
167
+ ...this.#partialDescriptor,
168
+ primitive
169
+ });
170
+ }
171
+ withDepthStencil(depthStencil) {
172
+ return new WithVertexImpl(this.#root, this.#slotBindings, {
173
+ ...this.#partialDescriptor,
174
+ depthStencil
175
+ });
176
+ }
177
+ withMultisample(multisample) {
178
+ return new WithVertexImpl(this.#root, this.#slotBindings, {
179
+ ...this.#partialDescriptor,
180
+ multisample
181
+ });
182
+ }
183
+ createPipeline() {
184
+ return INTERNAL_createRenderPipeline({
185
+ root: this.#root,
186
+ slotBindings: this.#slotBindings,
187
+ descriptor: this.#partialDescriptor
188
+ });
189
+ }
190
+ };
191
+ var WithFragmentImpl = class WithFragmentImpl {
192
+ #root;
193
+ #slotBindings;
194
+ #descriptor;
195
+ constructor(root, slotBindings, descriptor) {
196
+ this.#root = root;
197
+ this.#slotBindings = slotBindings;
198
+ this.#descriptor = descriptor;
199
+ }
200
+ withPrimitive(primitive) {
201
+ return new WithFragmentImpl(this.#root, this.#slotBindings, {
202
+ ...this.#descriptor,
203
+ primitive
204
+ });
205
+ }
206
+ withDepthStencil(depthStencil) {
207
+ return new WithFragmentImpl(this.#root, this.#slotBindings, {
208
+ ...this.#descriptor,
209
+ depthStencil
210
+ });
211
+ }
212
+ withMultisample(multisample) {
213
+ return new WithFragmentImpl(this.#root, this.#slotBindings, {
214
+ ...this.#descriptor,
215
+ multisample
216
+ });
217
+ }
218
+ createPipeline() {
219
+ return INTERNAL_createRenderPipeline({
220
+ root: this.#root,
221
+ slotBindings: this.#slotBindings,
222
+ descriptor: this.#descriptor
223
+ });
224
+ }
225
+ };
226
+ /**
227
+ * Holds all data that is necessary to facilitate CPU and GPU communication.
228
+ * Programs that share a root can interact via GPU buffers.
229
+ */
230
+ var TgpuRootImpl = class extends WithBindingImpl {
231
+ "~unstable";
232
+ _unwrappedBindGroupLayouts = new WeakMemo((key) => key.unwrap(this));
233
+ _unwrappedBindGroups = new WeakMemo((key) => key.unwrap(this));
234
+ [$internal];
235
+ constructor(device, nameRegistrySetting, _ownDevice, logOptions, shaderGenerator) {
236
+ super(() => this, []);
237
+ this.device = device;
238
+ this.nameRegistrySetting = nameRegistrySetting;
239
+ this._ownDevice = _ownDevice;
240
+ this.shaderGenerator = shaderGenerator;
241
+ this["~unstable"] = this;
242
+ this[$internal] = { logOptions };
243
+ }
244
+ configureContext(options) {
245
+ const context = options.canvas.getContext("webgpu");
246
+ if (!context) throw new Error("Unable to initialize 'webgpu' context on the provided canvas.");
247
+ context.configure({
248
+ ...options,
249
+ device: this.device,
250
+ format: options.format ?? navigator.gpu.getPreferredCanvasFormat()
251
+ });
252
+ return context;
253
+ }
254
+ get enabledFeatures() {
255
+ return new Set(this.device.features);
256
+ }
257
+ createBuffer(typeSchema, initialOrBuffer) {
258
+ return INTERNAL_createBuffer(this, typeSchema, initialOrBuffer);
259
+ }
260
+ createUniform(typeSchema, initialOrBuffer) {
261
+ return new TgpuBufferShorthandImpl("uniform", INTERNAL_createBuffer(this, typeSchema, initialOrBuffer).$usage("uniform"));
262
+ }
263
+ createMutable(typeSchema, initialOrBuffer) {
264
+ return new TgpuBufferShorthandImpl("mutable", INTERNAL_createBuffer(this, typeSchema, initialOrBuffer).$usage("storage"));
265
+ }
266
+ createReadonly(typeSchema, initialOrBuffer) {
267
+ return new TgpuBufferShorthandImpl("readonly", INTERNAL_createBuffer(this, typeSchema, initialOrBuffer).$usage("storage"));
268
+ }
269
+ createQuerySet(type, count, rawQuerySet) {
270
+ return INTERNAL_createQuerySet(this, type, count, rawQuerySet);
271
+ }
272
+ createBindGroup(layout, entries) {
273
+ return new TgpuBindGroupImpl(layout, entries);
274
+ }
275
+ destroy() {
276
+ clearTextureUtilsCache(this.device);
277
+ if (this._ownDevice) this.device.destroy();
278
+ }
279
+ createTexture(props) {
280
+ return INTERNAL_createTexture(props, this);
281
+ }
282
+ createSampler(props) {
283
+ return INTERNAL_createSampler(props, this);
284
+ }
285
+ createComparisonSampler(props) {
286
+ return INTERNAL_createComparisonSampler(props, this);
287
+ }
288
+ unwrap(resource) {
289
+ if (isComputePipeline(resource)) return resource[$internal].rawPipeline;
290
+ if (isRenderPipeline(resource)) return resource[$internal].core.unwrap().pipeline;
291
+ if (isBindGroupLayout(resource)) return this._unwrappedBindGroupLayouts.getOrMake(resource);
292
+ if (isBindGroup(resource)) return this._unwrappedBindGroups.getOrMake(resource);
293
+ if (isBuffer(resource)) return resource.buffer;
294
+ if (isTexture(resource)) return resource[$internal].unwrap();
295
+ if (isTextureView(resource)) {
296
+ if (!resource[$internal].unwrap) throw new Error("Cannot unwrap laid-out texture view as it has no underlying resource.");
297
+ return resource[$internal].unwrap();
298
+ }
299
+ if (isVertexLayout(resource)) return resource.vertexLayout;
300
+ if (isSampler(resource) || isComparisonSampler(resource)) {
301
+ if (resource[$internal].unwrap) return resource[$internal].unwrap();
302
+ throw new Error("Cannot unwrap laid-out sampler.");
303
+ }
304
+ if (isQuerySet(resource)) return resource.querySet;
305
+ throw new Error(`Unknown resource type: ${resource}`);
306
+ }
307
+ createDrawablePassProxy(encoder) {
308
+ const bindGroups = /* @__PURE__ */ new Map();
309
+ const vertexBuffers = /* @__PURE__ */ new Map();
310
+ let currentPipeline;
311
+ let dirty = true;
312
+ const applyPipelineState = () => {
313
+ if (!currentPipeline) throw new Error("Cannot draw without a call to pass.setPipeline");
314
+ if (!dirty) return;
315
+ dirty = false;
316
+ const { core, priors } = currentPipeline[$internal];
317
+ const memo = core.unwrap();
318
+ encoder.setPipeline(memo.pipeline);
319
+ applyBindGroups(encoder, this, memo.usedBindGroupLayouts, memo.catchall, (layout) => priors.bindGroupLayoutMap?.get(layout) ?? bindGroups.get(layout));
320
+ applyVertexBuffers(encoder, this, memo.usedVertexLayouts, (vertexLayout) => {
321
+ const priorBuffer = priors.vertexLayoutMap?.get(vertexLayout);
322
+ return priorBuffer ? {
323
+ buffer: priorBuffer,
324
+ offset: void 0,
325
+ size: void 0
326
+ } : vertexBuffers.get(vertexLayout);
327
+ });
328
+ };
329
+ return {
330
+ setPipeline(pipeline) {
331
+ currentPipeline = pipeline;
332
+ dirty = true;
333
+ },
334
+ setIndexBuffer: (buffer, indexFormat, offset, size) => {
335
+ if (isBuffer(buffer)) encoder.setIndexBuffer(this.unwrap(buffer), indexFormat, offset, size);
336
+ else encoder.setIndexBuffer(buffer, indexFormat, offset, size);
337
+ },
338
+ setVertexBuffer(vertexLayout, buffer, offset, size) {
339
+ vertexBuffers.set(vertexLayout, {
340
+ buffer,
341
+ offset,
342
+ size
343
+ });
344
+ dirty = true;
345
+ },
346
+ setBindGroup(bindGroupLayout, bindGroup) {
347
+ bindGroups.set(bindGroupLayout, bindGroup);
348
+ dirty = true;
349
+ },
350
+ draw(vertexCount, instanceCount, firstVertex, firstInstance) {
351
+ applyPipelineState();
352
+ encoder.draw(vertexCount, instanceCount, firstVertex, firstInstance);
353
+ },
354
+ drawIndexed(...args) {
355
+ applyPipelineState();
356
+ encoder.drawIndexed(...args);
357
+ },
358
+ drawIndirect(...args) {
359
+ applyPipelineState();
360
+ encoder.drawIndirect(...args);
361
+ },
362
+ drawIndexedIndirect(...args) {
363
+ applyPipelineState();
364
+ encoder.drawIndexedIndirect(...args);
365
+ }
366
+ };
367
+ }
368
+ beginRenderPass(descriptor, callback) {
369
+ const commandEncoder = this.device.createCommandEncoder();
370
+ const pass = commandEncoder.beginRenderPass(descriptor);
371
+ callback({
372
+ setViewport(...args) {
373
+ pass.setViewport(...args);
374
+ },
375
+ setScissorRect(...args) {
376
+ pass.setScissorRect(...args);
377
+ },
378
+ setBlendConstant(...args) {
379
+ pass.setBlendConstant(...args);
380
+ },
381
+ setStencilReference(...args) {
382
+ pass.setStencilReference(...args);
383
+ },
384
+ beginOcclusionQuery(...args) {
385
+ pass.beginOcclusionQuery(...args);
386
+ },
387
+ endOcclusionQuery(...args) {
388
+ pass.endOcclusionQuery(...args);
389
+ },
390
+ executeBundles(...args) {
391
+ pass.executeBundles(...args);
392
+ },
393
+ ...this.createDrawablePassProxy(pass)
394
+ });
395
+ pass.end();
396
+ this.device.queue.submit([commandEncoder.finish()]);
397
+ }
398
+ beginRenderBundleEncoder(descriptor, callback) {
399
+ const bundleEncoder = this.device.createRenderBundleEncoder(descriptor);
400
+ callback(this.createDrawablePassProxy(bundleEncoder));
401
+ return bundleEncoder.finish();
402
+ }
403
+ flush() {
404
+ console.warn("flush() has been deprecated, and has no effect.");
405
+ }
406
+ };
407
+ /**
408
+ * Requests a new GPU device and creates a root around it.
409
+ * If a specific device should be used instead, use @see initFromDevice.
410
+ *
411
+ * @example
412
+ * When given no options, the function will ask the browser for a suitable GPU device.
413
+ * ```ts
414
+ * const root = await tgpu.init();
415
+ * ```
416
+ *
417
+ * @example
418
+ * If there are specific options that should be used when requesting a device, you can pass those in.
419
+ * ```ts
420
+ * const adapterOptions: GPURequestAdapterOptions = ...;
421
+ * const deviceDescriptor: GPUDeviceDescriptor = ...;
422
+ * const root = await tgpu.init({ adapter: adapterOptions, device: deviceDescriptor });
423
+ * ```
424
+ */
425
+ async function init(options) {
426
+ const { adapter: adapterOpt, device: deviceOpt, unstable_names: names = "strict", unstable_logOptions } = options ?? {};
427
+ if (!navigator.gpu) throw new Error("WebGPU is not supported by this browser.");
428
+ const adapter = await navigator.gpu.requestAdapter(adapterOpt);
429
+ if (!adapter) throw new Error("Could not find a compatible GPU");
430
+ const availableFeatures = [];
431
+ for (const feature of deviceOpt?.requiredFeatures ?? []) {
432
+ if (!adapter.features.has(feature)) throw new Error(`Requested feature "${feature}" is not supported by the adapter.`);
433
+ availableFeatures.push(feature);
434
+ }
435
+ for (const feature of deviceOpt?.optionalFeatures ?? []) if (adapter.features.has(feature)) availableFeatures.push(feature);
436
+ else console.warn(`Optional feature "${feature}" is not supported by the adapter.`);
437
+ return new TgpuRootImpl(await adapter.requestDevice({
438
+ ...deviceOpt,
439
+ requiredFeatures: availableFeatures
440
+ }), names, true, unstable_logOptions ?? {}, options?.shaderGenerator);
441
+ }
442
+ /**
443
+ * Creates a root from the given device, instead of requesting it like @see init.
444
+ *
445
+ * @example
446
+ * ```ts
447
+ * const device: GPUDevice = ...;
448
+ * const root = tgpu.initFromDevice({ device });
449
+ * ```
450
+ */
451
+ function initFromDevice(options) {
452
+ const { device, unstable_names: names = "strict", unstable_logOptions } = options ?? {};
453
+ return new TgpuRootImpl(device, names, false, unstable_logOptions ?? {}, options?.shaderGenerator);
454
+ }
455
+
456
+ //#endregion
457
+ export { init, initFromDevice };