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,489 @@
1
+ import { $getNameForward, $internal, $resolve } from "../../shared/symbols.js";
2
+ import { PERF, getName, setName } from "../../shared/meta.js";
3
+ import { Void, isWgslData } from "../../data/wgslTypes.js";
4
+ import { getCustomLocation } from "../../data/dataTypes.js";
5
+ import { snip } from "../../data/snippet.js";
6
+ import { isGPUBuffer } from "../../types.js";
7
+ import { formatToWGSLType } from "../../data/vertexFormatData.js";
8
+ import { sizeOf } from "../../data/sizeOf.js";
9
+ import { isBuiltin } from "../../data/attributes.js";
10
+ import { namespace } from "../resolve/namespace.js";
11
+ import { isTexture, isTextureView } from "../texture/texture.js";
12
+ import { isBindGroup, isBindGroupLayout } from "../../tgpuBindGroupLayout.js";
13
+ import { connectAttributesToShader } from "../vertexLayout/connectAttributesToShader.js";
14
+ import { resolve } from "../../resolutionCtx.js";
15
+ import { isGPUCommandEncoder, isGPURenderBundleEncoder, isGPURenderPassEncoder } from "./typeGuards.js";
16
+ import { applyBindGroups, applyVertexBuffers } from "./applyPipelineState.js";
17
+ import { logDataFromGPU } from "../../tgsl/consoleLog/deserializers.js";
18
+ import { wgslExtensionToFeatureName, wgslExtensions } from "../../wgslExtensions.js";
19
+ import { resolveIndirectOffset } from "./pipelineUtils.js";
20
+ import { createWithPerformanceCallback, createWithTimestampWrites, setupTimestampWrites, triggerPerformanceCallback } from "./timeable.js";
21
+ import { AutoFragmentFn, AutoVertexFn } from "../function/autoIO.js";
22
+ import { isVertexLayout } from "../vertexLayout/vertexLayout.js";
23
+ import { connectAttachmentToShader } from "./connectAttachmentToShader.js";
24
+ import { connectTargetsToShader } from "./connectTargetsToShader.js";
25
+ //#region src/core/pipeline/renderPipeline.ts
26
+ const DRAW_INDIRECT_SIZE = 16;
27
+ const DRAW_INDEXED_INDIRECT_SIZE = 20;
28
+ function INTERNAL_createRenderPipeline(options) {
29
+ return new TgpuRenderPipelineImpl(new RenderPipelineCore(options), {});
30
+ }
31
+ const _lastAppliedRender = /* @__PURE__ */ new WeakMap();
32
+ var TgpuRenderPipelineImpl = class TgpuRenderPipelineImpl {
33
+ [$internal];
34
+ resourceType = "render-pipeline";
35
+ [$getNameForward];
36
+ hasIndexBuffer = false;
37
+ constructor(core, priors) {
38
+ this[$internal] = {
39
+ core,
40
+ priors,
41
+ root: core.options.root
42
+ };
43
+ this[$getNameForward] = core;
44
+ }
45
+ [$resolve](ctx) {
46
+ return ctx.resolve(this[$internal].core);
47
+ }
48
+ toString() {
49
+ return `renderPipeline:${getName(this) ?? "<unnamed>"}`;
50
+ }
51
+ $name(label) {
52
+ setName(this, label);
53
+ return this;
54
+ }
55
+ with(first, resource) {
56
+ const internals = this[$internal];
57
+ if (isGPURenderPassEncoder(first) || isGPURenderBundleEncoder(first)) return new TgpuRenderPipelineImpl(internals.core, {
58
+ ...internals.priors,
59
+ externalRenderEncoder: first,
60
+ externalEncoder: void 0
61
+ });
62
+ if (isGPUCommandEncoder(first)) return new TgpuRenderPipelineImpl(internals.core, {
63
+ ...internals.priors,
64
+ externalEncoder: first,
65
+ externalRenderEncoder: void 0
66
+ });
67
+ if (isBindGroup(first)) return new TgpuRenderPipelineImpl(internals.core, {
68
+ ...internals.priors,
69
+ bindGroupLayoutMap: new Map([...internals.priors.bindGroupLayoutMap ?? [], [first.layout, first]])
70
+ });
71
+ if (isBindGroupLayout(first)) return new TgpuRenderPipelineImpl(internals.core, {
72
+ ...internals.priors,
73
+ bindGroupLayoutMap: new Map([...internals.priors.bindGroupLayoutMap ?? [], [first, resource]])
74
+ });
75
+ if (isVertexLayout(first)) return new TgpuRenderPipelineImpl(internals.core, {
76
+ ...internals.priors,
77
+ vertexLayoutMap: new Map([...internals.priors.vertexLayoutMap ?? [], [first, resource]])
78
+ });
79
+ throw new Error("Unsupported value passed into .with()");
80
+ }
81
+ withPerformanceCallback(callback) {
82
+ const internals = this[$internal];
83
+ if (internals.priors.timestampWrites) return new TgpuRenderPipelineImpl(internals.core, {
84
+ ...internals.priors,
85
+ performanceCallback: callback
86
+ });
87
+ const querySet = internals.core.performanceCallbackQuerySet;
88
+ if (!querySet) {
89
+ console.warn("Performance callback cannot be used because the timestamp-query feature is not enabled on the root.");
90
+ return this;
91
+ }
92
+ const newPriors = createWithPerformanceCallback(internals.priors, callback, querySet);
93
+ return new TgpuRenderPipelineImpl(internals.core, newPriors);
94
+ }
95
+ withTimestampWrites(options) {
96
+ const internals = this[$internal];
97
+ const newPriors = createWithTimestampWrites(internals.priors, options, internals.core.options.root);
98
+ return new TgpuRenderPipelineImpl(internals.core, newPriors);
99
+ }
100
+ withColorAttachment(attachment) {
101
+ const internals = this[$internal];
102
+ return new TgpuRenderPipelineImpl(internals.core, {
103
+ ...internals.priors,
104
+ colorAttachment: attachment
105
+ });
106
+ }
107
+ withDepthStencilAttachment(attachment) {
108
+ const internals = this[$internal];
109
+ return new TgpuRenderPipelineImpl(internals.core, {
110
+ ...internals.priors,
111
+ depthStencilAttachment: attachment
112
+ });
113
+ }
114
+ withStencilReference(reference) {
115
+ const internals = this[$internal];
116
+ return new TgpuRenderPipelineImpl(internals.core, {
117
+ ...internals.priors,
118
+ stencilReference: reference
119
+ });
120
+ }
121
+ withIndexBuffer(buffer, indexFormatOrOffset, offsetElementsOrSizeBytes, sizeElementsOrUndefined) {
122
+ const internals = this[$internal];
123
+ if (isGPUBuffer(buffer)) {
124
+ if (typeof indexFormatOrOffset !== "string") throw new Error("If a GPUBuffer is passed, indexFormat must be provided.");
125
+ return new TgpuRenderPipelineImpl(internals.core, {
126
+ ...internals.priors,
127
+ indexBuffer: {
128
+ buffer,
129
+ indexFormat: indexFormatOrOffset,
130
+ offsetBytes: offsetElementsOrSizeBytes,
131
+ sizeBytes: sizeElementsOrUndefined
132
+ }
133
+ });
134
+ }
135
+ const dataTypeToIndexFormat = {
136
+ u32: "uint32",
137
+ u16: "uint16"
138
+ };
139
+ const elementType = buffer.dataType.elementType;
140
+ return new TgpuRenderPipelineImpl(internals.core, {
141
+ ...internals.priors,
142
+ indexBuffer: {
143
+ buffer,
144
+ indexFormat: dataTypeToIndexFormat[elementType.type],
145
+ offsetBytes: indexFormatOrOffset !== void 0 ? indexFormatOrOffset * sizeOf(elementType) : void 0,
146
+ sizeBytes: sizeElementsOrUndefined !== void 0 ? sizeElementsOrUndefined * sizeOf(elementType) : void 0
147
+ }
148
+ });
149
+ }
150
+ _createRenderPass(encoder) {
151
+ const internals = this[$internal];
152
+ const { root, descriptor } = internals.core.options;
153
+ const memo = internals.core.unwrap();
154
+ const colorAttachments = descriptor.fragment ? connectAttachmentToShader(descriptor.fragment?.shell?.returnType ?? memo.fragmentOut, internals.priors.colorAttachment ?? {}).map((_attachment) => {
155
+ const attachment = {
156
+ loadOp: "clear",
157
+ storeOp: "store",
158
+ ..._attachment
159
+ };
160
+ if (isTexture(attachment.view)) attachment.view = root.unwrap(attachment.view).createView();
161
+ else if (isTextureView(attachment.view)) attachment.view = root.unwrap(attachment.view);
162
+ else if (isGPUCanvasContext(attachment.view)) attachment.view = attachment.view.getCurrentTexture().createView();
163
+ if (isTexture(attachment.resolveTarget)) attachment.resolveTarget = root.unwrap(attachment.resolveTarget).createView();
164
+ else if (isTextureView(attachment.resolveTarget)) attachment.resolveTarget = root.unwrap(attachment.resolveTarget);
165
+ else if (isGPUCanvasContext(attachment.resolveTarget)) attachment.resolveTarget = attachment.resolveTarget.getCurrentTexture().createView();
166
+ return attachment;
167
+ }) : [];
168
+ const renderPassDescriptor = {
169
+ label: getName(internals.core) ?? "<unnamed>",
170
+ colorAttachments,
171
+ ...setupTimestampWrites(internals.priors, root)
172
+ };
173
+ const depthStencil = internals.priors.depthStencilAttachment;
174
+ if (depthStencil !== void 0) {
175
+ const view = isTexture(depthStencil.view) ? root.unwrap(depthStencil.view).createView() : isTextureView(depthStencil.view) ? root.unwrap(depthStencil.view) : depthStencil.view;
176
+ renderPassDescriptor.depthStencilAttachment = {
177
+ ...depthStencil,
178
+ view
179
+ };
180
+ }
181
+ return encoder.beginRenderPass(renderPassDescriptor);
182
+ }
183
+ _applyRenderState(encoder) {
184
+ const internals = this[$internal];
185
+ const memo = internals.core.unwrap();
186
+ const { root } = internals.core.options;
187
+ encoder.setPipeline(memo.pipeline);
188
+ applyBindGroups(encoder, root, memo.usedBindGroupLayouts, memo.catchall, (layout) => internals.priors.bindGroupLayoutMap?.get(layout));
189
+ applyVertexBuffers(encoder, root, memo.usedVertexLayouts, (layout) => {
190
+ const buffer = internals.priors.vertexLayoutMap?.get(layout);
191
+ return buffer ? { buffer } : void 0;
192
+ });
193
+ if (internals.priors.stencilReference !== void 0 && "setStencilReference" in encoder) encoder.setStencilReference(internals.priors.stencilReference);
194
+ }
195
+ _setIndexBuffer(encoder) {
196
+ const internals = this[$internal];
197
+ const { root } = internals.core.options;
198
+ if (!internals.priors.indexBuffer) throw new Error("No index buffer set for this render pipeline.");
199
+ const { buffer, indexFormat, offsetBytes, sizeBytes } = internals.priors.indexBuffer;
200
+ if (isGPUBuffer(buffer)) encoder.setIndexBuffer(buffer, indexFormat, offsetBytes, sizeBytes);
201
+ else encoder.setIndexBuffer(root.unwrap(buffer), indexFormat, offsetBytes, sizeBytes);
202
+ }
203
+ draw(vertexCount, instanceCount, firstVertex, firstInstance) {
204
+ const internals = this[$internal];
205
+ const { root } = internals.core.options;
206
+ if (internals.priors.externalRenderEncoder) {
207
+ if (_lastAppliedRender.get(internals.priors.externalRenderEncoder) !== this) {
208
+ this._applyRenderState(internals.priors.externalRenderEncoder);
209
+ _lastAppliedRender.set(internals.priors.externalRenderEncoder, this);
210
+ }
211
+ internals.priors.externalRenderEncoder.draw(vertexCount, instanceCount, firstVertex, firstInstance);
212
+ return;
213
+ }
214
+ if (internals.priors.externalEncoder) {
215
+ const pass = this._createRenderPass(internals.priors.externalEncoder);
216
+ this._applyRenderState(pass);
217
+ pass.draw(vertexCount, instanceCount, firstVertex, firstInstance);
218
+ pass.end();
219
+ return;
220
+ }
221
+ const { logResources } = internals.core.unwrap();
222
+ const commandEncoder = root.device.createCommandEncoder();
223
+ const pass = this._createRenderPass(commandEncoder);
224
+ this._applyRenderState(pass);
225
+ pass.draw(vertexCount, instanceCount, firstVertex, firstInstance);
226
+ pass.end();
227
+ root.device.queue.submit([commandEncoder.finish()]);
228
+ if (logResources) logDataFromGPU(logResources);
229
+ if (internals.priors.performanceCallback) triggerPerformanceCallback({
230
+ root,
231
+ priors: internals.priors
232
+ });
233
+ }
234
+ drawIndexed(indexCount, instanceCount, firstIndex, baseVertex, firstInstance) {
235
+ const internals = this[$internal];
236
+ const { root } = internals.core.options;
237
+ if (internals.priors.externalRenderEncoder) {
238
+ if (_lastAppliedRender.get(internals.priors.externalRenderEncoder) !== this) {
239
+ this._applyRenderState(internals.priors.externalRenderEncoder);
240
+ this._setIndexBuffer(internals.priors.externalRenderEncoder);
241
+ _lastAppliedRender.set(internals.priors.externalRenderEncoder, this);
242
+ }
243
+ internals.priors.externalRenderEncoder.drawIndexed(indexCount, instanceCount, firstIndex, baseVertex, firstInstance);
244
+ return;
245
+ }
246
+ if (internals.priors.externalEncoder) {
247
+ const pass = this._createRenderPass(internals.priors.externalEncoder);
248
+ this._applyRenderState(pass);
249
+ this._setIndexBuffer(pass);
250
+ pass.drawIndexed(indexCount, instanceCount, firstIndex, baseVertex, firstInstance);
251
+ pass.end();
252
+ return;
253
+ }
254
+ const { logResources } = internals.core.unwrap();
255
+ const commandEncoder = root.device.createCommandEncoder();
256
+ const pass = this._createRenderPass(commandEncoder);
257
+ this._applyRenderState(pass);
258
+ this._setIndexBuffer(pass);
259
+ pass.drawIndexed(indexCount, instanceCount, firstIndex, baseVertex, firstInstance);
260
+ pass.end();
261
+ root.device.queue.submit([commandEncoder.finish()]);
262
+ if (logResources) logDataFromGPU(logResources);
263
+ if (internals.priors.performanceCallback) triggerPerformanceCallback({
264
+ root,
265
+ priors: internals.priors
266
+ });
267
+ }
268
+ drawIndirect(indirectBuffer, indirectOffset) {
269
+ const internals = this[$internal];
270
+ const { root } = internals.core.options;
271
+ const rawBuffer = isGPUBuffer(indirectBuffer) ? indirectBuffer : indirectBuffer.buffer;
272
+ const offset = resolveIndirectOffset(indirectBuffer, indirectOffset, DRAW_INDIRECT_SIZE, "drawIndirect");
273
+ if (internals.priors.externalRenderEncoder) {
274
+ if (_lastAppliedRender.get(internals.priors.externalRenderEncoder) !== this) {
275
+ this._applyRenderState(internals.priors.externalRenderEncoder);
276
+ _lastAppliedRender.set(internals.priors.externalRenderEncoder, this);
277
+ }
278
+ internals.priors.externalRenderEncoder.drawIndirect(rawBuffer, offset);
279
+ return;
280
+ }
281
+ if (internals.priors.externalEncoder) {
282
+ const pass = this._createRenderPass(internals.priors.externalEncoder);
283
+ this._applyRenderState(pass);
284
+ pass.drawIndirect(rawBuffer, offset);
285
+ pass.end();
286
+ return;
287
+ }
288
+ const { logResources } = internals.core.unwrap();
289
+ const commandEncoder = root.device.createCommandEncoder();
290
+ const pass = this._createRenderPass(commandEncoder);
291
+ this._applyRenderState(pass);
292
+ pass.drawIndirect(rawBuffer, offset);
293
+ pass.end();
294
+ root.device.queue.submit([commandEncoder.finish()]);
295
+ if (logResources) logDataFromGPU(logResources);
296
+ if (internals.priors.performanceCallback) triggerPerformanceCallback({
297
+ root,
298
+ priors: internals.priors
299
+ });
300
+ }
301
+ drawIndexedIndirect(indirectBuffer, indirectOffset) {
302
+ const internals = this[$internal];
303
+ const { root } = internals.core.options;
304
+ const rawBuffer = isGPUBuffer(indirectBuffer) ? indirectBuffer : root.unwrap(indirectBuffer);
305
+ const offset = resolveIndirectOffset(indirectBuffer, indirectOffset, DRAW_INDEXED_INDIRECT_SIZE, "drawIndexedIndirect");
306
+ if (internals.priors.externalRenderEncoder) {
307
+ if (_lastAppliedRender.get(internals.priors.externalRenderEncoder) !== this) {
308
+ this._applyRenderState(internals.priors.externalRenderEncoder);
309
+ this._setIndexBuffer(internals.priors.externalRenderEncoder);
310
+ _lastAppliedRender.set(internals.priors.externalRenderEncoder, this);
311
+ }
312
+ internals.priors.externalRenderEncoder.drawIndexedIndirect(rawBuffer, offset);
313
+ return;
314
+ }
315
+ if (internals.priors.externalEncoder) {
316
+ const pass = this._createRenderPass(internals.priors.externalEncoder);
317
+ this._applyRenderState(pass);
318
+ this._setIndexBuffer(pass);
319
+ pass.drawIndexedIndirect(rawBuffer, offset);
320
+ pass.end();
321
+ return;
322
+ }
323
+ const { logResources } = internals.core.unwrap();
324
+ const commandEncoder = root.device.createCommandEncoder();
325
+ const pass = this._createRenderPass(commandEncoder);
326
+ this._applyRenderState(pass);
327
+ this._setIndexBuffer(pass);
328
+ pass.drawIndexedIndirect(rawBuffer, offset);
329
+ pass.end();
330
+ root.device.queue.submit([commandEncoder.finish()]);
331
+ if (logResources) logDataFromGPU(logResources);
332
+ if (internals.priors.performanceCallback) triggerPerformanceCallback({
333
+ root,
334
+ priors: internals.priors
335
+ });
336
+ }
337
+ };
338
+ var RenderPipelineCore = class {
339
+ [$internal] = true;
340
+ options;
341
+ _memo;
342
+ #latestAutoVertexIn;
343
+ #latestAutoFragmentOut;
344
+ #performanceCallbackQuerySet;
345
+ constructor(options) {
346
+ this.options = options;
347
+ }
348
+ [$resolve](ctx) {
349
+ const { slotBindings } = this.options;
350
+ const { vertex, fragment, attribs = {} } = this.options.descriptor;
351
+ this.#latestAutoVertexIn = void 0;
352
+ this.#latestAutoFragmentOut = void 0;
353
+ const locations = matchUpVaryingLocations(vertex?.shell?.out, fragment?.shell?.in, getName(vertex) ?? "<unnamed>", getName(fragment) ?? "<unnamed>");
354
+ return ctx.withVaryingLocations(locations, () => ctx.withSlots(slotBindings, () => {
355
+ let vertexOut;
356
+ if (typeof vertex === "function") {
357
+ const autoFn = new AutoVertexFn(vertex, Object.fromEntries(Object.entries(attribs).map(([key, value]) => [key, formatToWGSLType[value.format]])), locations);
358
+ ctx.resolve(autoFn);
359
+ this.#latestAutoVertexIn = autoFn.autoIn.completeStruct.propTypes;
360
+ vertexOut = autoFn.autoOut.completeStruct;
361
+ } else vertexOut = ctx.resolve(vertex).dataType;
362
+ if (fragment) if (typeof fragment === "function") {
363
+ const varyings = Object.fromEntries(Object.entries(vertexOut.propTypes).filter(([, dataType]) => !isBuiltin(dataType)));
364
+ this.#latestAutoFragmentOut = ctx.resolve(new AutoFragmentFn(fragment, varyings, locations)).dataType;
365
+ } else ctx.resolve(fragment);
366
+ return snip("", Void, "runtime");
367
+ }));
368
+ }
369
+ toString() {
370
+ return "renderPipelineCore";
371
+ }
372
+ get performanceCallbackQuerySet() {
373
+ if (!this.options.root.enabledFeatures.has("timestamp-query")) return;
374
+ return this.#performanceCallbackQuerySet ??= this.options.root.createQuerySet("timestamp", 2);
375
+ }
376
+ unwrap() {
377
+ if (this._memo !== void 0) return this._memo;
378
+ const { root, descriptor: tgpuDescriptor } = this.options;
379
+ const device = root.device;
380
+ const enableExtensions = wgslExtensions.filter((extension) => root.enabledFeatures.has(wgslExtensionToFeatureName[extension]));
381
+ let resolutionResult;
382
+ let resolveMeasure;
383
+ const ns = namespace({ names: root.nameRegistrySetting });
384
+ if (PERF?.enabled) {
385
+ const resolveStart = performance.mark("typegpu:resolution:start");
386
+ resolutionResult = resolve(this, {
387
+ namespace: ns,
388
+ enableExtensions,
389
+ shaderGenerator: root.shaderGenerator,
390
+ root
391
+ });
392
+ resolveMeasure = performance.measure("typegpu:resolution", { start: resolveStart.name });
393
+ } else resolutionResult = resolve(this, {
394
+ namespace: ns,
395
+ enableExtensions,
396
+ shaderGenerator: root.shaderGenerator,
397
+ root
398
+ });
399
+ const { code, usedBindGroupLayouts, catchall, logResources } = resolutionResult;
400
+ if (catchall !== void 0) usedBindGroupLayouts[catchall[0]]?.$name(`${getName(this) ?? "<unnamed>"} - Automatic Bind Group & Layout`);
401
+ const module = device.createShaderModule({
402
+ label: `${getName(this) ?? "<unnamed>"} - Shader`,
403
+ code
404
+ });
405
+ const { vertex, fragment, attribs = {}, targets } = this.options.descriptor;
406
+ const connectedAttribs = connectAttributesToShader(vertex?.shell?.in ?? this.#latestAutoVertexIn ?? {}, attribs);
407
+ const fragmentOut = fragment?.shell?.returnType ?? this.#latestAutoFragmentOut;
408
+ const connectedTargets = fragmentOut ? connectTargetsToShader(fragmentOut, targets) : [null];
409
+ const descriptor = {
410
+ layout: device.createPipelineLayout({
411
+ label: `${getName(this) ?? "<unnamed>"} - Pipeline Layout`,
412
+ bindGroupLayouts: usedBindGroupLayouts.map((l) => root.unwrap(l))
413
+ }),
414
+ vertex: {
415
+ module,
416
+ buffers: connectedAttribs.bufferDefinitions
417
+ }
418
+ };
419
+ const label = getName(this);
420
+ if (label !== void 0) descriptor.label = label;
421
+ if (tgpuDescriptor.fragment) descriptor.fragment = {
422
+ module,
423
+ targets: connectedTargets
424
+ };
425
+ if (tgpuDescriptor.primitive) if (isWgslData(tgpuDescriptor.primitive.stripIndexFormat)) descriptor.primitive = {
426
+ ...tgpuDescriptor.primitive,
427
+ stripIndexFormat: {
428
+ u32: "uint32",
429
+ u16: "uint16"
430
+ }[tgpuDescriptor.primitive.stripIndexFormat.type]
431
+ };
432
+ else descriptor.primitive = tgpuDescriptor.primitive;
433
+ if (tgpuDescriptor.depthStencil) descriptor.depthStencil = tgpuDescriptor.depthStencil;
434
+ if (tgpuDescriptor.multisample) descriptor.multisample = tgpuDescriptor.multisample;
435
+ this._memo = {
436
+ pipeline: device.createRenderPipeline(descriptor),
437
+ usedBindGroupLayouts,
438
+ catchall,
439
+ logResources,
440
+ usedVertexLayouts: connectedAttribs.usedVertexLayouts,
441
+ fragmentOut: this.#latestAutoFragmentOut
442
+ };
443
+ if (PERF?.enabled) (async () => {
444
+ const start = performance.mark("typegpu:compile-start");
445
+ await device.queue.onSubmittedWorkDone();
446
+ const compileMeasure = performance.measure("typegpu:compiled", { start: start.name });
447
+ PERF?.record("resolution", {
448
+ resolveDuration: resolveMeasure?.duration,
449
+ compileDuration: compileMeasure.duration,
450
+ wgslSize: code.length
451
+ });
452
+ })();
453
+ return this._memo;
454
+ }
455
+ };
456
+ /**
457
+ * Assumes vertexOut and fragmentIn are matching when it comes to the keys, that is fragmentIn's keyset is a subset of vertexOut's
458
+ * Logs a warning, when they don't match in terms of custom locations
459
+ */
460
+ function matchUpVaryingLocations(vertexOut = {}, fragmentIn = {}, vertexFnName, fragmentFnName) {
461
+ const locations = {};
462
+ const usedLocations = /* @__PURE__ */ new Set();
463
+ function saveLocation(key, location) {
464
+ locations[key] = location;
465
+ usedLocations.add(location);
466
+ }
467
+ for (const [key, value] of Object.entries(vertexOut)) {
468
+ const customLocation = getCustomLocation(value);
469
+ if (customLocation !== void 0) saveLocation(key, customLocation);
470
+ }
471
+ for (const [key, value] of Object.entries(fragmentIn)) {
472
+ const customLocation = getCustomLocation(value);
473
+ if (customLocation === void 0) continue;
474
+ if (locations[key] === void 0) saveLocation(key, customLocation);
475
+ else if (locations[key] !== customLocation) console.warn(`Mismatched location between vertexFn (${vertexFnName}) output (${locations[key]}) and fragmentFn (${fragmentFnName}) input (${customLocation}) for the key "${key}", using the location set on vertex output.`);
476
+ }
477
+ let nextLocation = 0;
478
+ for (const key of Object.keys(vertexOut ?? {})) {
479
+ if (isBuiltin(vertexOut[key]) || locations[key] !== void 0) continue;
480
+ while (usedLocations.has(nextLocation)) nextLocation++;
481
+ saveLocation(key, nextLocation);
482
+ }
483
+ return locations;
484
+ }
485
+ function isGPUCanvasContext(value) {
486
+ return typeof value?.getCurrentTexture === "function";
487
+ }
488
+ //#endregion
489
+ export { INTERNAL_createRenderPipeline };
@@ -0,0 +1,20 @@
1
+ import { TgpuQuerySet } from "../querySet/querySet.js";
2
+ //#region src/core/pipeline/timeable.d.ts
3
+ interface Timeable {
4
+ withPerformanceCallback(callback: (start: bigint, end: bigint) => void | Promise<void>): this;
5
+ withTimestampWrites(options: {
6
+ querySet: TgpuQuerySet<'timestamp'> | GPUQuerySet;
7
+ beginningOfPassWriteIndex?: number;
8
+ endOfPassWriteIndex?: number;
9
+ }): this;
10
+ }
11
+ type TimestampWritesPriors = {
12
+ readonly timestampWrites?: {
13
+ querySet: TgpuQuerySet<'timestamp'> | GPUQuerySet;
14
+ beginningOfPassWriteIndex?: number;
15
+ endOfPassWriteIndex?: number;
16
+ };
17
+ readonly performanceCallback?: (start: bigint, end: bigint) => void | Promise<void>;
18
+ };
19
+ //#endregion
20
+ export { Timeable, TimestampWritesPriors };
@@ -0,0 +1,55 @@
1
+ import { $internal } from "../../shared/symbols.js";
2
+ import { isQuerySet } from "../querySet/querySet.js";
3
+ //#region src/core/pipeline/timeable.ts
4
+ function createWithPerformanceCallback(currentPriors, callback, querySet) {
5
+ if (!currentPriors.timestampWrites) return {
6
+ ...currentPriors,
7
+ performanceCallback: callback,
8
+ timestampWrites: {
9
+ querySet,
10
+ beginningOfPassWriteIndex: 0,
11
+ endOfPassWriteIndex: 1
12
+ }
13
+ };
14
+ return {
15
+ ...currentPriors,
16
+ performanceCallback: callback
17
+ };
18
+ }
19
+ function createWithTimestampWrites(currentPriors, options, root) {
20
+ if (!root.enabledFeatures.has("timestamp-query")) throw new Error("Timestamp writes require the \"timestamp-query\" feature to be enabled on GPU device.");
21
+ const timestampWrites = { querySet: options.querySet };
22
+ if (options.beginningOfPassWriteIndex !== void 0) timestampWrites.beginningOfPassWriteIndex = options.beginningOfPassWriteIndex;
23
+ if (options.endOfPassWriteIndex !== void 0) timestampWrites.endOfPassWriteIndex = options.endOfPassWriteIndex;
24
+ return {
25
+ ...currentPriors,
26
+ timestampWrites
27
+ };
28
+ }
29
+ function setupTimestampWrites(priors, root) {
30
+ if (!priors.timestampWrites) return {};
31
+ const { querySet, beginningOfPassWriteIndex, endOfPassWriteIndex } = priors.timestampWrites;
32
+ const timestampWrites = { querySet: isQuerySet(querySet) ? root.unwrap(querySet) : querySet };
33
+ if (beginningOfPassWriteIndex !== void 0) timestampWrites.beginningOfPassWriteIndex = beginningOfPassWriteIndex;
34
+ if (endOfPassWriteIndex !== void 0) timestampWrites.endOfPassWriteIndex = endOfPassWriteIndex;
35
+ return { timestampWrites };
36
+ }
37
+ function triggerPerformanceCallback({ root, priors }) {
38
+ const querySet = priors.timestampWrites?.querySet;
39
+ const callback = priors.performanceCallback;
40
+ if (!querySet) throw new Error("Cannot dispatch workgroups with performance callback without a query set.");
41
+ if (!isQuerySet(querySet)) throw new Error("Performance callback with raw GPUQuerySet is not supported. Use TgpuQuerySet instead.");
42
+ const commandEncoder = root.device.createCommandEncoder();
43
+ commandEncoder.resolveQuerySet(root.unwrap(querySet), 0, querySet.count, querySet[$internal].resolveBuffer, 0);
44
+ root.device.queue.submit([commandEncoder.finish()]);
45
+ root.device.queue.onSubmittedWorkDone().then(async () => {
46
+ if (!querySet.available) return;
47
+ const result = await querySet.read();
48
+ const start = result[priors.timestampWrites?.beginningOfPassWriteIndex ?? 0];
49
+ const end = result[priors.timestampWrites?.endOfPassWriteIndex ?? 1];
50
+ if (start === void 0 || end === void 0) throw new Error("QuerySet did not return valid timestamps.");
51
+ await callback(start, end);
52
+ });
53
+ }
54
+ //#endregion
55
+ export { createWithPerformanceCallback, createWithTimestampWrites, setupTimestampWrites, triggerPerformanceCallback };
@@ -0,0 +1,27 @@
1
+ import { $internal } from "../../shared/symbols.js";
2
+ //#region src/core/pipeline/typeGuards.ts
3
+ function isComputePipeline(value) {
4
+ const maybe = value;
5
+ return maybe?.resourceType === "compute-pipeline" && !!maybe[$internal];
6
+ }
7
+ function isRenderPipeline(value) {
8
+ const maybe = value;
9
+ return maybe?.resourceType === "render-pipeline" && !!maybe[$internal];
10
+ }
11
+ function isPipeline(value) {
12
+ return isRenderPipeline(value) || isComputePipeline(value);
13
+ }
14
+ function isGPUCommandEncoder(value) {
15
+ return !!value && typeof value === "object" && "beginRenderPass" in value && "beginComputePass" in value;
16
+ }
17
+ function isGPUComputePassEncoder(value) {
18
+ return !!value && typeof value === "object" && "dispatchWorkgroups" in value && !("beginRenderPass" in value);
19
+ }
20
+ function isGPURenderPassEncoder(value) {
21
+ return !!value && typeof value === "object" && "executeBundles" in value && "draw" in value;
22
+ }
23
+ function isGPURenderBundleEncoder(value) {
24
+ return !!value && typeof value === "object" && "draw" in value && "finish" in value && !("executeBundles" in value) && !("beginRenderPass" in value) && !("dispatchWorkgroups" in value);
25
+ }
26
+ //#endregion
27
+ export { isComputePipeline, isGPUCommandEncoder, isGPUComputePassEncoder, isGPURenderBundleEncoder, isGPURenderPassEncoder, isPipeline, isRenderPipeline };
@@ -0,0 +1,20 @@
1
+ import { TgpuNamable } from "../../shared/meta.js";
2
+ import { $internal } from "../../shared/symbols.js";
3
+ //#region src/core/querySet/querySet.d.ts
4
+ interface TgpuQuerySet<T extends GPUQueryType> extends TgpuNamable {
5
+ readonly resourceType: 'query-set';
6
+ readonly type: T;
7
+ readonly count: number;
8
+ readonly querySet: GPUQuerySet;
9
+ readonly destroyed: boolean;
10
+ readonly available: boolean;
11
+ readonly [$internal]: {
12
+ readonly readBuffer: GPUBuffer;
13
+ readonly resolveBuffer: GPUBuffer;
14
+ };
15
+ resolve(): void;
16
+ read(): Promise<bigint[]>;
17
+ destroy(): void;
18
+ }
19
+ //#endregion
20
+ export { TgpuQuerySet };