typegpu 0.10.1 → 0.11.0-rc.1

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