@luma.gl/engine 9.3.5 → 9.4.0-alpha.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 (181) hide show
  1. package/dist/animation/animator.d.ts +42 -0
  2. package/dist/animation/animator.d.ts.map +1 -0
  3. package/dist/animation/animator.js +53 -0
  4. package/dist/animation/animator.js.map +1 -0
  5. package/dist/animation-loop/animation-loop.d.ts +14 -2
  6. package/dist/animation-loop/animation-loop.d.ts.map +1 -1
  7. package/dist/animation-loop/animation-loop.js +40 -22
  8. package/dist/animation-loop/animation-loop.js.map +1 -1
  9. package/dist/animation-loop/animation-props.d.ts +2 -0
  10. package/dist/animation-loop/animation-props.d.ts.map +1 -1
  11. package/dist/animation-loop/make-animation-loop.d.ts +1 -0
  12. package/dist/animation-loop/make-animation-loop.d.ts.map +1 -1
  13. package/dist/animation-loop/make-animation-loop.js +3 -0
  14. package/dist/animation-loop/make-animation-loop.js.map +1 -1
  15. package/dist/application-utils/load-file.d.ts +2 -0
  16. package/dist/application-utils/load-file.d.ts.map +1 -1
  17. package/dist/application-utils/load-file.js +31 -5
  18. package/dist/application-utils/load-file.js.map +1 -1
  19. package/dist/compute/buffer-transform.d.ts +5 -1
  20. package/dist/compute/buffer-transform.d.ts.map +1 -1
  21. package/dist/compute/buffer-transform.js +7 -3
  22. package/dist/compute/buffer-transform.js.map +1 -1
  23. package/dist/compute/computation.d.ts +20 -8
  24. package/dist/compute/computation.d.ts.map +1 -1
  25. package/dist/compute/computation.js +34 -13
  26. package/dist/compute/computation.js.map +1 -1
  27. package/dist/compute/swap.d.ts.map +1 -1
  28. package/dist/compute/swap.js +17 -0
  29. package/dist/compute/swap.js.map +1 -1
  30. package/dist/dist.dev.js +4100 -1979
  31. package/dist/dist.min.js +288 -42
  32. package/dist/dynamic-buffer/dynamic-buffer.d.ts +171 -0
  33. package/dist/dynamic-buffer/dynamic-buffer.d.ts.map +1 -0
  34. package/dist/dynamic-buffer/dynamic-buffer.js +316 -0
  35. package/dist/dynamic-buffer/dynamic-buffer.js.map +1 -0
  36. package/dist/dynamic-texture/dynamic-texture.d.ts +14 -1
  37. package/dist/dynamic-texture/dynamic-texture.d.ts.map +1 -1
  38. package/dist/dynamic-texture/dynamic-texture.js +31 -2
  39. package/dist/dynamic-texture/dynamic-texture.js.map +1 -1
  40. package/dist/dynamic-texture/texture-binding-source.d.ts +55 -0
  41. package/dist/dynamic-texture/texture-binding-source.d.ts.map +1 -0
  42. package/dist/dynamic-texture/texture-binding-source.js +55 -0
  43. package/dist/dynamic-texture/texture-binding-source.js.map +1 -0
  44. package/dist/dynamic-texture/texture-data.d.ts +5 -3
  45. package/dist/dynamic-texture/texture-data.d.ts.map +1 -1
  46. package/dist/dynamic-texture/texture-data.js +5 -0
  47. package/dist/dynamic-texture/texture-data.js.map +1 -1
  48. package/dist/dynamic-texture/video-texture.d.ts +101 -0
  49. package/dist/dynamic-texture/video-texture.d.ts.map +1 -0
  50. package/dist/dynamic-texture/video-texture.js +272 -0
  51. package/dist/dynamic-texture/video-texture.js.map +1 -0
  52. package/dist/geometries/cube-geometry.d.ts +1 -0
  53. package/dist/geometries/cube-geometry.d.ts.map +1 -1
  54. package/dist/geometries/cube-geometry.js +22 -2
  55. package/dist/geometries/cube-geometry.js.map +1 -1
  56. package/dist/geometries/plane-geometry.d.ts +10 -0
  57. package/dist/geometries/plane-geometry.d.ts.map +1 -1
  58. package/dist/geometries/plane-geometry.js.map +1 -1
  59. package/dist/geometry/geometry-utils.d.ts +36 -1
  60. package/dist/geometry/geometry-utils.d.ts.map +1 -1
  61. package/dist/geometry/geometry-utils.js +137 -2
  62. package/dist/geometry/geometry-utils.js.map +1 -1
  63. package/dist/geometry/geometry.d.ts +52 -33
  64. package/dist/geometry/geometry.d.ts.map +1 -1
  65. package/dist/geometry/geometry.js +70 -19
  66. package/dist/geometry/geometry.js.map +1 -1
  67. package/dist/geometry/gpu-geometry.d.ts +37 -4
  68. package/dist/geometry/gpu-geometry.d.ts.map +1 -1
  69. package/dist/geometry/gpu-geometry.js +44 -36
  70. package/dist/geometry/gpu-geometry.js.map +1 -1
  71. package/dist/index.cjs +3990 -1961
  72. package/dist/index.cjs.map +4 -4
  73. package/dist/index.d.ts +20 -9
  74. package/dist/index.d.ts.map +1 -1
  75. package/dist/index.js +8 -5
  76. package/dist/index.js.map +1 -1
  77. package/dist/material/material-factory.d.ts +2 -1
  78. package/dist/material/material-factory.d.ts.map +1 -1
  79. package/dist/material/material-factory.js.map +1 -1
  80. package/dist/material/material.d.ts +29 -10
  81. package/dist/material/material.d.ts.map +1 -1
  82. package/dist/material/material.js +84 -21
  83. package/dist/material/material.js.map +1 -1
  84. package/dist/model/model.d.ts +81 -23
  85. package/dist/model/model.d.ts.map +1 -1
  86. package/dist/model/model.js +312 -78
  87. package/dist/model/model.js.map +1 -1
  88. package/dist/model/split-uniforms-and-bindings.d.ts +5 -2
  89. package/dist/model/split-uniforms-and-bindings.d.ts.map +1 -1
  90. package/dist/model/split-uniforms-and-bindings.js.map +1 -1
  91. package/dist/models/billboard-texture-model.d.ts +5 -2
  92. package/dist/models/billboard-texture-model.d.ts.map +1 -1
  93. package/dist/models/billboard-texture-model.js +28 -10
  94. package/dist/models/billboard-texture-model.js.map +1 -1
  95. package/dist/modules/picking/index-color-picking.d.ts +24 -0
  96. package/dist/modules/picking/index-color-picking.d.ts.map +1 -0
  97. package/dist/modules/picking/index-color-picking.js +205 -0
  98. package/dist/modules/picking/index-color-picking.js.map +1 -0
  99. package/dist/modules/picking/index-picking.d.ts +3 -3
  100. package/dist/modules/picking/index-picking.d.ts.map +1 -1
  101. package/dist/modules/picking/index-picking.js +23 -6
  102. package/dist/modules/picking/index-picking.js.map +1 -1
  103. package/dist/modules/picking/picking-manager.d.ts +21 -0
  104. package/dist/modules/picking/picking-manager.d.ts.map +1 -1
  105. package/dist/modules/picking/picking-manager.js +100 -0
  106. package/dist/modules/picking/picking-manager.js.map +1 -1
  107. package/dist/modules/picking/picking-uniforms.d.ts +4 -4
  108. package/dist/modules/picking/picking-uniforms.d.ts.map +1 -1
  109. package/dist/modules/picking/picking-uniforms.js +3 -0
  110. package/dist/modules/picking/picking-uniforms.js.map +1 -1
  111. package/dist/modules/picking/picking.d.ts +5 -1
  112. package/dist/modules/picking/picking.d.ts.map +1 -1
  113. package/dist/modules/picking/picking.js +6 -0
  114. package/dist/modules/picking/picking.js.map +1 -1
  115. package/dist/passes/get-fragment-shader.js +4 -4
  116. package/dist/passes/shader-pass-renderer.d.ts +92 -25
  117. package/dist/passes/shader-pass-renderer.d.ts.map +1 -1
  118. package/dist/passes/shader-pass-renderer.js +466 -72
  119. package/dist/passes/shader-pass-renderer.js.map +1 -1
  120. package/dist/passes/texture-transform-module.d.ts +10 -0
  121. package/dist/passes/texture-transform-module.d.ts.map +1 -0
  122. package/dist/passes/texture-transform-module.js +50 -0
  123. package/dist/passes/texture-transform-module.js.map +1 -0
  124. package/dist/shader-inputs.d.ts +33 -16
  125. package/dist/shader-inputs.d.ts.map +1 -1
  126. package/dist/shader-inputs.js +42 -12
  127. package/dist/shader-inputs.js.map +1 -1
  128. package/dist/utils/buffer-layout-helper.d.ts +0 -1
  129. package/dist/utils/buffer-layout-helper.d.ts.map +1 -1
  130. package/dist/utils/buffer-layout-helper.js +2 -11
  131. package/dist/utils/buffer-layout-helper.js.map +1 -1
  132. package/dist/utils/buffer-layout-order.d.ts.map +1 -1
  133. package/dist/utils/buffer-layout-order.js +4 -15
  134. package/dist/utils/buffer-layout-order.js.map +1 -1
  135. package/dist/utils/shader-module-utils.d.ts +2 -0
  136. package/dist/utils/shader-module-utils.d.ts.map +1 -1
  137. package/dist/utils/shader-module-utils.js +64 -3
  138. package/dist/utils/shader-module-utils.js.map +1 -1
  139. package/package.json +2 -2
  140. package/src/animation/animator.ts +79 -0
  141. package/src/animation-loop/animation-loop.ts +56 -22
  142. package/src/animation-loop/animation-props.ts +2 -0
  143. package/src/animation-loop/make-animation-loop.ts +4 -0
  144. package/src/application-utils/load-file.ts +38 -5
  145. package/src/compute/buffer-transform.ts +14 -5
  146. package/src/compute/computation.ts +53 -21
  147. package/src/compute/swap.ts +19 -0
  148. package/src/dynamic-buffer/dynamic-buffer.ts +453 -0
  149. package/src/dynamic-texture/dynamic-texture.ts +36 -3
  150. package/src/dynamic-texture/texture-binding-source.ts +94 -0
  151. package/src/dynamic-texture/texture-data.ts +12 -4
  152. package/src/dynamic-texture/video-texture.ts +334 -0
  153. package/src/geometries/cube-geometry.ts +24 -2
  154. package/src/geometries/plane-geometry.ts +10 -0
  155. package/src/geometry/geometry-utils.ts +209 -4
  156. package/src/geometry/geometry.ts +113 -36
  157. package/src/geometry/gpu-geometry.ts +61 -41
  158. package/src/index.ts +50 -14
  159. package/src/material/material-factory.ts +5 -1
  160. package/src/material/material.ts +123 -27
  161. package/src/model/model.ts +441 -102
  162. package/src/model/split-uniforms-and-bindings.ts +7 -3
  163. package/src/models/billboard-texture-model.ts +33 -12
  164. package/src/modules/picking/index-color-picking.ts +212 -0
  165. package/src/modules/picking/index-picking.ts +23 -6
  166. package/src/modules/picking/picking-manager.ts +141 -0
  167. package/src/modules/picking/picking-uniforms.ts +7 -4
  168. package/src/modules/picking/picking.ts +8 -0
  169. package/src/passes/get-fragment-shader.ts +4 -4
  170. package/src/passes/shader-pass-renderer.ts +691 -89
  171. package/src/passes/texture-transform-module.ts +52 -0
  172. package/src/shader-inputs.ts +65 -17
  173. package/src/utils/buffer-layout-helper.ts +2 -14
  174. package/src/utils/buffer-layout-order.ts +14 -24
  175. package/src/utils/shader-module-utils.ts +109 -3
  176. package/dist/geometry/gpu-table.d.ts +0 -1
  177. package/dist/geometry/gpu-table.d.ts.map +0 -1
  178. package/dist/geometry/gpu-table.js +0 -46
  179. package/dist/geometry/gpu-table.js.map +0 -1
  180. package/src/dynamic-texture/texture-data.ts.disabled +0 -296
  181. package/src/geometry/gpu-table.ts +0 -45
@@ -1,13 +1,28 @@
1
1
  // luma.gl
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
+ import { Texture } from '@luma.gl/core';
4
5
  import { initializeShaderModule } from '@luma.gl/shadertools';
5
6
  import { ShaderInputs } from "../shader-inputs.js";
7
+ import { DynamicTexture } from "../dynamic-texture/dynamic-texture.js";
6
8
  import { ClipSpace } from "../models/clip-space.js";
7
9
  import { SwapFramebuffers } from "../compute/swap.js";
8
10
  import { BackgroundTextureModel } from "../models/billboard-texture-model.js";
9
11
  import { getFragmentShaderForRenderPass } from "./get-fragment-shader.js";
10
- /** A pass that renders a given texture into screen space */
12
+ import { textureTransform } from "./texture-transform-module.js";
13
+ const RESERVED_RENDER_TARGET_NAMES = new Set(['original', 'previous']);
14
+ /**
15
+ * Runs one or more shader passes against a source texture.
16
+ *
17
+ * The renderer owns:
18
+ * - a shared ping-pong pair for the logical `previous` chain
19
+ * - any named render targets declared by `ShaderPassPipeline`
20
+ * - a fullscreen model used both to seed the chain and to present to the screen
21
+ *
22
+ * Per-draw `uniforms` and `bindings` passed to `renderToTexture()` or `renderToScreen()` are
23
+ * merged on top of the renderer's `shaderInputs`, which lets callers reuse one renderer instance
24
+ * while varying pass inputs frame to frame.
25
+ */
11
26
  export class ShaderPassRenderer {
12
27
  device;
13
28
  shaderInputs;
@@ -16,24 +31,27 @@ export class ShaderPassRenderer {
16
31
  textureModel;
17
32
  constructor(device, props) {
18
33
  this.device = device;
19
- props.shaderPasses.map(shaderPass => initializeShaderModule(shaderPass));
20
- const modules = props.shaderPasses.reduce((object, shaderPass) => ({ ...object, [shaderPass.name]: shaderPass }), {});
34
+ const executableShaderPasses = getExecutableShaderPasses(props.shaderPasses);
35
+ executableShaderPasses.map(shaderPass => initializeShaderModule(shaderPass));
36
+ const modules = executableShaderPasses.reduce((object, shaderPass) => ({ ...object, [shaderPass.name]: shaderPass }), {});
21
37
  this.shaderInputs = props.shaderInputs || new ShaderInputs(modules);
22
38
  const size = device.getCanvasContext().getDrawingBufferSize();
23
39
  this.swapFramebuffers = new SwapFramebuffers(device, {
24
- colorAttachments: [device.preferredColorFormat],
40
+ colorAttachments: [props.colorFormat || device.preferredColorFormat],
25
41
  width: size[0],
26
42
  height: size[1]
27
43
  });
28
44
  this.textureModel = new BackgroundTextureModel(device, {
29
- backgroundTexture: this.swapFramebuffers.current.colorAttachments[0].texture
45
+ backgroundTexture: this.swapFramebuffers.current.colorAttachments[0].texture,
46
+ flipY: props.flipY ?? device.type === 'webgpu'
30
47
  });
31
- this.passRenderers = props.shaderPasses.map(shaderPass => new PassRenderer(device, shaderPass));
48
+ const flipY = props.flipY ?? device.type === 'webgpu';
49
+ this.passRenderers = props.shaderPasses.map(shaderPass => new PassRenderer(device, shaderPass, this.shaderInputs, flipY));
32
50
  }
33
51
  /** Destroys resources created by this ShaderPassRenderer */
34
52
  destroy() {
35
- for (const subPassRenderer of this.passRenderers) {
36
- subPassRenderer.destroy();
53
+ for (const passRenderer of this.passRenderers) {
54
+ passRenderer.destroy();
37
55
  }
38
56
  this.swapFramebuffers.destroy();
39
57
  this.textureModel.destroy();
@@ -41,24 +59,33 @@ export class ShaderPassRenderer {
41
59
  resize(size) {
42
60
  size ||= this.device.getCanvasContext().getDrawingBufferSize();
43
61
  this.swapFramebuffers.resize({ width: size[0], height: size[1] });
62
+ for (const passRenderer of this.passRenderers) {
63
+ passRenderer.resize(size);
64
+ }
65
+ }
66
+ /** Invalidates persistent pipeline targets without destroying their allocations. */
67
+ resetHistory() {
68
+ for (const passRenderer of this.passRenderers) {
69
+ passRenderer.resetHistory();
70
+ }
44
71
  }
45
72
  renderToScreen(options) {
46
- // Run the shader passes and generate an output texture
47
73
  const outputTexture = this.renderToTexture(options);
48
74
  if (!outputTexture) {
49
- // source texture not yet loaded
50
75
  return false;
51
76
  }
52
77
  const framebuffer = this.device
53
78
  .getDefaultCanvasContext()
54
79
  .getCurrentFramebuffer({ depthStencilFormat: false });
80
+ // Prepare fullscreen presentation bindings before opening the render pass so WebGPU
81
+ // uploads stay on the parent command encoder rather than the active render pass encoder.
82
+ this.textureModel.setProps({ backgroundTexture: outputTexture });
83
+ this.textureModel.predraw(this.device.commandEncoder);
55
84
  const renderPass = this.device.beginRenderPass({
56
85
  id: 'shader-pass-renderer-to-screen',
57
86
  framebuffer,
58
- // clearColor: [1, 1, 0, 1],
59
87
  clearDepth: false
60
88
  });
61
- this.textureModel.setProps({ backgroundTexture: outputTexture });
62
89
  this.textureModel.draw(renderPass);
63
90
  renderPass.end();
64
91
  return true;
@@ -68,84 +95,278 @@ export class ShaderPassRenderer {
68
95
  */
69
96
  renderToTexture(options) {
70
97
  const { sourceTexture } = options;
71
- if (!sourceTexture.isReady) {
98
+ if (sourceTexture instanceof DynamicTexture && !sourceTexture.isReady) {
72
99
  return null;
73
100
  }
74
- // If no shader passes are provided, just return the original texture
101
+ const originalTexture = sourceTexture instanceof DynamicTexture ? sourceTexture.texture : sourceTexture;
75
102
  if (this.passRenderers.length === 0) {
76
- return sourceTexture.texture;
77
- }
78
- this.textureModel.setProps({ backgroundTexture: sourceTexture });
79
- // Clear the current texture before we begin
80
- const clearTexturePass = this.device.beginRenderPass({
81
- id: 'shader-pass-renderer-clear-texture',
82
- framebuffer: this.swapFramebuffers.current,
83
- clearColor: [1, 0, 0, 1]
103
+ return originalTexture;
104
+ }
105
+ if (options.resetHistory) {
106
+ this.resetHistory();
107
+ }
108
+ // Seed the first shared framebuffer with the original source. This normalizes the starting
109
+ // point for later passes so `previous` always refers to a drawing-buffer-sized texture.
110
+ this.textureModel.setProps({ backgroundTexture: originalTexture });
111
+ // The seed draw updates fullscreen bind groups, so it must be prepared before the
112
+ // render pass begins to avoid command-encoder locking errors on WebGPU.
113
+ this.textureModel.predraw(this.device.commandEncoder);
114
+ const sourceFramebuffer = getSeedFramebuffer(this.swapFramebuffers, originalTexture);
115
+ const seedRenderPass = this.device.beginRenderPass({
116
+ id: 'shader-pass-renderer-seed-source',
117
+ framebuffer: sourceFramebuffer,
118
+ clearColor: [0, 0, 0, 1],
119
+ clearDepth: false
84
120
  });
85
- this.textureModel.draw(clearTexturePass);
86
- clearTexturePass.end();
87
- // Copy the texture contents
88
- // const commandEncoder = this.device.createCommandEncoder();
89
- // commandEncoder.copyTextureToTexture({
90
- // sourceTexture: sourceTexture.texture,
91
- // destinationTexture: this.swapFramebuffers.current.colorAttachments[0].texture
92
- // });
93
- // commandEncoder.finish();
94
- let first = true;
95
- for (const passRenderer of this.passRenderers) {
96
- for (const subPassRenderer of passRenderer.subPassRenderers) {
97
- if (!first) {
98
- this.swapFramebuffers.swap();
121
+ this.textureModel.draw(seedRenderPass);
122
+ seedRenderPass.end();
123
+ let previousFramebuffer = sourceFramebuffer;
124
+ let previousTexture = getFramebufferTexture(sourceFramebuffer);
125
+ let frameSucceeded = false;
126
+ try {
127
+ for (const passRenderer of this.passRenderers) {
128
+ passRenderer.initializeHistoryTargets(originalTexture, this.textureModel);
129
+ for (const execution of passRenderer.subPassExecutions) {
130
+ const outputName = execution.output || 'previous';
131
+ const outputFramebuffer = outputName === 'previous'
132
+ ? getNextPreviousFramebuffer(this.swapFramebuffers, previousFramebuffer)
133
+ : passRenderer.getOutputFramebuffer(outputName);
134
+ const outputTexture = getFramebufferTexture(outputFramebuffer);
135
+ // Pipeline routing resolves logical input names like `original`, `previous`, or a named
136
+ // render target into concrete textures for this draw.
137
+ const resolvedBindings = passRenderer.resolveBindings({
138
+ execution,
139
+ originalTexture,
140
+ previousTexture,
141
+ outputTexture,
142
+ externalBindings: options.bindings || {}
143
+ });
144
+ // Runtime uniforms are layered last so callers can update a renderer-owned pass without
145
+ // reconstructing it every frame.
146
+ const resolvedUniforms = resolveExecutionUniforms(this.shaderInputs, execution, options.uniforms || {});
147
+ execution.subPassRenderer.prepare({
148
+ commandEncoder: this.device.commandEncoder,
149
+ bindings: resolvedBindings,
150
+ textureScale: calculateTextureScale(resolvedBindings['sourceTexture'] || previousTexture, outputTexture),
151
+ uniforms: resolvedUniforms
152
+ });
153
+ const renderPass = this.device.beginRenderPass({
154
+ id: 'shader-pass-renderer-run-pass',
155
+ framebuffer: outputFramebuffer,
156
+ clearColor: [0, 0, 0, 1],
157
+ clearDepth: 1
158
+ });
159
+ execution.subPassRenderer.draw(renderPass);
160
+ renderPass.end();
161
+ if (outputName === 'previous') {
162
+ previousTexture = outputTexture;
163
+ previousFramebuffer = outputFramebuffer;
164
+ }
165
+ else {
166
+ passRenderer.markTargetWritten(outputName);
167
+ }
99
168
  }
100
- first = false;
101
- const swapBufferTexture = this.swapFramebuffers.current.colorAttachments[0].texture;
102
- const bindings = {
103
- sourceTexture: swapBufferTexture
104
- // texSize: [sourceTextures.width, sourceTextures.height]
105
- };
169
+ }
170
+ frameSucceeded = true;
171
+ }
172
+ finally {
173
+ for (const passRenderer of this.passRenderers) {
174
+ passRenderer.finishFrame(frameSucceeded);
175
+ }
176
+ }
177
+ return previousTexture;
178
+ }
179
+ }
180
+ /** Renders one `ShaderPass` or `ShaderPassPipeline` entry in the chain. */
181
+ class PassRenderer {
182
+ device;
183
+ shaderInputs;
184
+ passDefinition;
185
+ renderTargets;
186
+ subPassExecutions;
187
+ constructor(device, passDefinition, shaderInputs, flipY) {
188
+ this.device = device;
189
+ this.shaderInputs = shaderInputs;
190
+ this.passDefinition = passDefinition;
191
+ if (isShaderPassPipeline(passDefinition)) {
192
+ validateRenderTargetNames(passDefinition.name, passDefinition.renderTargets || {});
193
+ this.renderTargets = createManagedRenderTargets(device, passDefinition.renderTargets || {});
194
+ this.subPassExecutions = passDefinition.steps.flatMap(step => this.createStepExecutions(passDefinition, step, flipY));
195
+ return;
196
+ }
197
+ validateShaderPassDoesNotOwnRenderTargets(passDefinition, passDefinition.name);
198
+ this.renderTargets = {};
199
+ this.subPassExecutions = this.createPassExecutions(passDefinition, {
200
+ ownerName: passDefinition.name,
201
+ flipY
202
+ });
203
+ }
204
+ destroy() {
205
+ for (const execution of this.subPassExecutions) {
206
+ execution.subPassRenderer.destroy();
207
+ }
208
+ destroyManagedRenderTargets(this.renderTargets);
209
+ }
210
+ resize(size) {
211
+ resizeManagedRenderTargets(this.device, this.renderTargets, size);
212
+ }
213
+ resetHistory() {
214
+ for (const renderTarget of Object.values(this.renderTargets)) {
215
+ renderTarget.historyInitialized = false;
216
+ renderTarget.writtenThisFrame = false;
217
+ }
218
+ }
219
+ initializeHistoryTargets(originalTexture, textureModel) {
220
+ for (const renderTarget of Object.values(this.renderTargets)) {
221
+ if (renderTarget.spec.lifetime !== 'history' || renderTarget.historyInitialized) {
222
+ continue;
223
+ }
224
+ const historyFramebuffer = getHistoryFramebuffer(renderTarget);
225
+ const initialize = renderTarget.spec.initialize || {
226
+ clearColor: [0, 0, 0, 0]
227
+ };
228
+ if (initialize === 'original') {
229
+ textureModel.setProps({ backgroundTexture: originalTexture });
230
+ textureModel.predraw(this.device.commandEncoder);
231
+ const renderPass = this.device.beginRenderPass({
232
+ id: `${renderTarget.name}-initialize-history`,
233
+ framebuffer: historyFramebuffer,
234
+ clearColor: [0, 0, 0, 0],
235
+ clearDepth: false
236
+ });
237
+ textureModel.draw(renderPass);
238
+ renderPass.end();
239
+ }
240
+ else {
106
241
  const renderPass = this.device.beginRenderPass({
107
- id: 'shader-pass-renderer-run-pass',
108
- framebuffer: this.swapFramebuffers.next,
109
- clearColor: [0, 0, 0, 1],
110
- clearDepth: 1
242
+ id: `${renderTarget.name}-clear-history`,
243
+ framebuffer: historyFramebuffer,
244
+ clearColor: initialize.clearColor,
245
+ clearDepth: false
111
246
  });
112
- subPassRenderer.render({ renderPass, bindings });
113
247
  renderPass.end();
114
248
  }
249
+ renderTarget.historyInitialized = true;
115
250
  }
116
- this.swapFramebuffers.swap();
117
- const outputTexture = this.swapFramebuffers.current.colorAttachments[0].texture;
118
- return outputTexture;
119
251
  }
120
- }
121
- /** renders one ShaderPass */
122
- class PassRenderer {
123
- shaderPass;
124
- subPassRenderers;
125
- constructor(device, shaderPass, props = {}) {
126
- this.shaderPass = shaderPass;
127
- // const id = `${shaderPass.name}-pass`;
252
+ getOutputFramebuffer(name) {
253
+ return this.getRenderTarget(name).framebuffer;
254
+ }
255
+ markTargetWritten(name) {
256
+ this.getRenderTarget(name).writtenThisFrame = true;
257
+ }
258
+ finishFrame(succeeded) {
259
+ for (const renderTarget of Object.values(this.renderTargets)) {
260
+ if (succeeded && renderTarget.spec.lifetime === 'history' && renderTarget.writtenThisFrame) {
261
+ const historyTexture = getHistoryTexture(renderTarget);
262
+ const historyFramebuffer = getHistoryFramebuffer(renderTarget);
263
+ renderTarget.historyTexture = renderTarget.texture;
264
+ renderTarget.historyFramebuffer = renderTarget.framebuffer;
265
+ renderTarget.texture = historyTexture;
266
+ renderTarget.framebuffer = historyFramebuffer;
267
+ renderTarget.historyInitialized = true;
268
+ }
269
+ renderTarget.writtenThisFrame = false;
270
+ }
271
+ }
272
+ getRenderTarget(name) {
273
+ const renderTarget = this.renderTargets[name];
274
+ if (!renderTarget) {
275
+ throw new Error(`${this.getOwnerName()}: unknown render target "${name}"`);
276
+ }
277
+ return renderTarget;
278
+ }
279
+ resolveBindings(options) {
280
+ const { execution, originalTexture, previousTexture, outputTexture, externalBindings } = options;
281
+ const inputMap = execution.inputs || { sourceTexture: 'previous' };
282
+ const shaderInputBindings = this.shaderInputs.getModuleBindingValues(execution.shaderPass.name);
283
+ const externalBindingsForPass = Object.fromEntries(Object.entries(externalBindings).filter(([name]) => execution.shaderPass.bindingLayout?.some(binding => binding.name === name)));
284
+ // Shader-pass bindings stored in `shaderInputs` act as renderer-owned defaults. Per-draw
285
+ // external bindings can override those defaults for frame-specific resources, while routed
286
+ // logical inputs such as `sourceTexture` remain authoritative below.
287
+ const resolvedBindings = {
288
+ ...shaderInputBindings,
289
+ ...externalBindingsForPass
290
+ };
291
+ const outputName = execution.output || 'previous';
292
+ for (const [bindingName, inputSource] of Object.entries(inputMap)) {
293
+ if (!inputSource) {
294
+ continue;
295
+ }
296
+ const texture = this.resolveInputTexture(inputSource, originalTexture, previousTexture);
297
+ const target = inputSource in this.renderTargets ? this.renderTargets[inputSource] : null;
298
+ if (outputName !== 'previous' &&
299
+ inputSource === outputName &&
300
+ target?.spec.lifetime !== 'history') {
301
+ throw new Error(`${execution.ownerName}: subpass cannot read and write render target "${outputName}" in the same draw`);
302
+ }
303
+ if (bindingName === 'sourceTexture' && texture === outputTexture) {
304
+ throw new Error(`${execution.ownerName}: subpass cannot sample from the render target it is writing to`);
305
+ }
306
+ resolvedBindings[bindingName] = texture;
307
+ }
308
+ if (!('sourceTexture' in resolvedBindings)) {
309
+ resolvedBindings['sourceTexture'] = previousTexture;
310
+ }
311
+ return resolvedBindings;
312
+ }
313
+ createStepExecutions(pipeline, step, flipY) {
314
+ validateShaderPassDoesNotOwnRenderTargets(step.shaderPass, `${pipeline.name}/${step.shaderPass.name}`);
315
+ return this.createPassExecutions(step.shaderPass, {
316
+ ownerName: `${pipeline.name}/${step.shaderPass.name}`,
317
+ firstInputs: step.inputs,
318
+ lastOutput: step.output,
319
+ uniformOverrides: step.uniforms,
320
+ flipY
321
+ });
322
+ }
323
+ createPassExecutions(shaderPass, options) {
128
324
  const subPasses = shaderPass.passes || [];
129
- // normalizePasses(gl, module, id, props);
130
- this.subPassRenderers = subPasses.map(subPass => {
131
- // const idn = `${id}-${subPasses.length + 1}`;
132
- return new SubPassRenderer(device, shaderPass, subPass);
325
+ return subPasses.map((subPass, index) => {
326
+ const isFirstSubPass = index === 0;
327
+ const isLastSubPass = index === subPasses.length - 1;
328
+ const inputs = isFirstSubPass && options.firstInputs !== undefined ? options.firstInputs : subPass.inputs;
329
+ const output = isLastSubPass && options.lastOutput !== undefined ? options.lastOutput : subPass.output;
330
+ validateSubPassRouting(options.ownerName, inputs, output, this.renderTargets);
331
+ return {
332
+ ownerName: options.ownerName,
333
+ shaderPass,
334
+ subPassRenderer: new SubPassRenderer(this.device, shaderPass, subPass, options.flipY),
335
+ inputs,
336
+ output,
337
+ uniforms: mergeUniforms(options.uniformOverrides, subPass.uniforms)
338
+ };
133
339
  });
134
340
  }
135
- destroy() {
136
- for (const subPassRenderer of this.subPassRenderers) {
137
- subPassRenderer.destroy();
341
+ resolveInputTexture(inputSource, originalTexture, previousTexture) {
342
+ switch (inputSource) {
343
+ case 'original':
344
+ return originalTexture;
345
+ case 'previous':
346
+ return previousTexture;
347
+ default: {
348
+ const renderTarget = this.getRenderTarget(inputSource);
349
+ if (renderTarget.spec.lifetime === 'history' && !renderTarget.writtenThisFrame) {
350
+ return getHistoryTexture(renderTarget);
351
+ }
352
+ return renderTarget.texture;
353
+ }
138
354
  }
139
355
  }
356
+ getOwnerName() {
357
+ return this.passDefinition.name;
358
+ }
140
359
  }
141
- /** Renders one subpass of a ShaderPass */
360
+ /** Renders a single subpass of a `ShaderPass`. */
142
361
  class SubPassRenderer {
143
362
  model;
144
363
  shaderPass;
145
364
  subPass;
146
- constructor(device, shaderPass, subPass) {
365
+ flipY;
366
+ constructor(device, shaderPass, subPass, flipY) {
147
367
  this.shaderPass = shaderPass;
148
368
  this.subPass = subPass;
369
+ this.flipY = flipY;
149
370
  const action = subPass.action || (subPass.filter && 'filter') || (subPass.sampler && 'sample') || 'filter';
150
371
  const fs = getFragmentShaderForRenderPass({
151
372
  shaderPass,
@@ -156,7 +377,7 @@ class SubPassRenderer {
156
377
  id: `${shaderPass.name}-subpass`,
157
378
  source: fs,
158
379
  fs,
159
- modules: [shaderPass],
380
+ modules: [textureTransform, shaderPass],
160
381
  parameters: {
161
382
  depthWriteEnabled: false
162
383
  }
@@ -165,17 +386,190 @@ class SubPassRenderer {
165
386
  destroy() {
166
387
  this.model.destroy();
167
388
  }
168
- render(options) {
169
- const { renderPass, bindings } = options;
389
+ prepare(options) {
390
+ const { commandEncoder, bindings, textureScale, uniforms } = options;
391
+ this.model.shaderInputs.setProps({
392
+ textureTransform: { scale: textureScale, flipY: this.flipY ? 1 : 0 }
393
+ });
170
394
  this.model.shaderInputs.setProps({
171
395
  [this.shaderPass.name]: this.shaderPass.uniforms || {}
172
396
  });
173
397
  this.model.shaderInputs.setProps({
174
- [this.shaderPass.name]: this.subPass.uniforms || {}
398
+ [this.shaderPass.name]: uniforms || {}
175
399
  });
176
- // this.model.setBindings(this.subPass.bindings || {});
177
400
  this.model.setBindings(bindings || {});
401
+ this.model.predraw(commandEncoder);
402
+ }
403
+ draw(renderPass) {
178
404
  this.model.draw(renderPass);
179
405
  }
180
406
  }
407
+ function getExecutableShaderPasses(shaderPasses) {
408
+ return shaderPasses.flatMap(shaderPass => isShaderPassPipeline(shaderPass) ? shaderPass.steps.map(step => step.shaderPass) : [shaderPass]);
409
+ }
410
+ function isShaderPassPipeline(shaderPass) {
411
+ return 'steps' in shaderPass;
412
+ }
413
+ function validateShaderPassDoesNotOwnRenderTargets(shaderPass, ownerName) {
414
+ const renderTargets = shaderPass
415
+ .renderTargets;
416
+ if (renderTargets && Object.keys(renderTargets).length > 0) {
417
+ throw new Error(`${ownerName}: ShaderPass.renderTargets is not supported; use ShaderPassPipeline.renderTargets instead`);
418
+ }
419
+ }
420
+ function validateRenderTargetNames(ownerName, renderTargets) {
421
+ for (const name of Object.keys(renderTargets)) {
422
+ if (RESERVED_RENDER_TARGET_NAMES.has(name)) {
423
+ throw new Error(`${ownerName}: render target name "${name}" is reserved`);
424
+ }
425
+ }
426
+ }
427
+ function validateSubPassRouting(ownerName, inputs, output, renderTargets) {
428
+ const inputMap = inputs || { sourceTexture: 'previous' };
429
+ for (const inputSource of Object.values(inputMap)) {
430
+ if (inputSource &&
431
+ inputSource !== 'original' &&
432
+ inputSource !== 'previous' &&
433
+ !(inputSource in renderTargets)) {
434
+ throw new Error(`${ownerName}: unknown input source "${inputSource}"`);
435
+ }
436
+ }
437
+ if (output && output !== 'previous' && !(output in renderTargets)) {
438
+ throw new Error(`${ownerName}: unknown output target "${output}"`);
439
+ }
440
+ }
441
+ function mergeUniforms(baseUniforms, subPassUniforms) {
442
+ if (!baseUniforms && !subPassUniforms) {
443
+ return undefined;
444
+ }
445
+ return {
446
+ ...(baseUniforms || {}),
447
+ ...(subPassUniforms || {})
448
+ };
449
+ }
450
+ /**
451
+ * Resolves the effective uniform block for one execution.
452
+ *
453
+ * Merge order is:
454
+ * 1. persisted renderer-level shader inputs
455
+ * 2. uniforms baked into the pipeline or subpass definition
456
+ * 3. per-draw runtime overrides
457
+ */
458
+ function resolveExecutionUniforms(shaderInputs, execution, runtimeUniforms) {
459
+ return mergeUniforms(mergeUniforms(shaderInputs.getUniformValues()[execution.shaderPass.name], execution.uniforms), runtimeUniforms[execution.shaderPass.name]);
460
+ }
461
+ function createManagedRenderTargets(device, renderTargets) {
462
+ const size = device.getCanvasContext().getDrawingBufferSize();
463
+ return Object.entries(renderTargets).reduce((managedRenderTargets, [name, spec]) => ({
464
+ ...managedRenderTargets,
465
+ [name]: createManagedRenderTarget(device, name, spec, size)
466
+ }), {});
467
+ }
468
+ function destroyManagedRenderTargets(renderTargets) {
469
+ for (const renderTarget of Object.values(renderTargets)) {
470
+ renderTarget.framebuffer.destroy();
471
+ renderTarget.texture.destroy();
472
+ renderTarget.historyFramebuffer?.destroy();
473
+ renderTarget.historyTexture?.destroy();
474
+ }
475
+ }
476
+ function resizeManagedRenderTargets(device, renderTargets, size) {
477
+ for (const renderTarget of Object.values(renderTargets)) {
478
+ const targetSize = getRenderTargetSize(size, renderTarget.spec.scale);
479
+ if (renderTarget.texture.width === targetSize[0] &&
480
+ renderTarget.texture.height === targetSize[1]) {
481
+ continue;
482
+ }
483
+ renderTarget.framebuffer.destroy();
484
+ renderTarget.texture.destroy();
485
+ renderTarget.historyFramebuffer?.destroy();
486
+ renderTarget.historyTexture?.destroy();
487
+ const replacement = createManagedRenderTarget(device, renderTarget.name, renderTarget.spec, size);
488
+ renderTarget.texture = replacement.texture;
489
+ renderTarget.framebuffer = replacement.framebuffer;
490
+ renderTarget.historyTexture = replacement.historyTexture;
491
+ renderTarget.historyFramebuffer = replacement.historyFramebuffer;
492
+ renderTarget.historyInitialized = false;
493
+ renderTarget.writtenThisFrame = false;
494
+ }
495
+ }
496
+ function createManagedRenderTarget(device, name, spec, size) {
497
+ const targetSize = getRenderTargetSize(size, spec.scale);
498
+ const { texture, framebuffer } = createTargetResources(device, name, spec, targetSize);
499
+ let historyTexture;
500
+ let historyFramebuffer;
501
+ if (spec.lifetime === 'history') {
502
+ const historyResources = createTargetResources(device, `${name}-history`, spec, targetSize);
503
+ historyTexture = historyResources.texture;
504
+ historyFramebuffer = historyResources.framebuffer;
505
+ }
506
+ return {
507
+ name,
508
+ spec,
509
+ texture,
510
+ framebuffer,
511
+ historyTexture,
512
+ historyFramebuffer,
513
+ historyInitialized: false,
514
+ writtenThisFrame: false
515
+ };
516
+ }
517
+ function createTargetResources(device, name, spec, targetSize) {
518
+ const texture = device.createTexture({
519
+ id: `${name}-texture`,
520
+ width: targetSize[0],
521
+ height: targetSize[1],
522
+ format: spec.format || device.preferredColorFormat,
523
+ usage: Texture.SAMPLE | Texture.RENDER | Texture.COPY_SRC | Texture.COPY_DST
524
+ });
525
+ const framebuffer = device.createFramebuffer({
526
+ id: `${name}-framebuffer`,
527
+ width: targetSize[0],
528
+ height: targetSize[1],
529
+ colorAttachments: [texture]
530
+ });
531
+ return { texture, framebuffer };
532
+ }
533
+ function getHistoryTexture(renderTarget) {
534
+ if (!renderTarget.historyTexture) {
535
+ throw new Error(`${renderTarget.name}: transient render target has no history texture`);
536
+ }
537
+ return renderTarget.historyTexture;
538
+ }
539
+ function getHistoryFramebuffer(renderTarget) {
540
+ if (!renderTarget.historyFramebuffer) {
541
+ throw new Error(`${renderTarget.name}: transient render target has no history framebuffer`);
542
+ }
543
+ return renderTarget.historyFramebuffer;
544
+ }
545
+ function getRenderTargetSize(size, scale = [1, 1]) {
546
+ return [Math.max(1, Math.round(size[0] * scale[0])), Math.max(1, Math.round(size[1] * scale[1]))];
547
+ }
548
+ function getFramebufferTexture(framebuffer) {
549
+ const texture = framebuffer.colorAttachments[0]?.texture;
550
+ if (!texture) {
551
+ throw new Error('ShaderPassRenderer: framebuffer is missing a color attachment texture');
552
+ }
553
+ return texture;
554
+ }
555
+ function getNextPreviousFramebuffer(swapFramebuffers, previousFramebuffer) {
556
+ if (previousFramebuffer === swapFramebuffers.current) {
557
+ return swapFramebuffers.next;
558
+ }
559
+ return swapFramebuffers.current;
560
+ }
561
+ function getSeedFramebuffer(swapFramebuffers, originalTexture) {
562
+ if (getFramebufferTexture(swapFramebuffers.current) === originalTexture) {
563
+ return swapFramebuffers.next;
564
+ }
565
+ return swapFramebuffers.current;
566
+ }
567
+ function calculateTextureScale(sourceTexture, outputTexture) {
568
+ const sourceAspect = sourceTexture.width / sourceTexture.height;
569
+ const outputAspect = outputTexture.width / outputTexture.height;
570
+ if (outputAspect > sourceAspect) {
571
+ return [1, outputAspect / sourceAspect];
572
+ }
573
+ return [sourceAspect / outputAspect, 1];
574
+ }
181
575
  //# sourceMappingURL=shader-pass-renderer.js.map