@luma.gl/engine 9.3.6 → 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
@@ -2,28 +2,88 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
 
5
- import {Device, RenderPass, Texture} from '@luma.gl/core';
6
- import type {ShaderPass} from '@luma.gl/shadertools';
5
+ import type {Binding, CommandEncoder, TextureFormatColor} from '@luma.gl/core';
6
+ import {Device, Framebuffer, RenderPass, Texture} from '@luma.gl/core';
7
+ import type {
8
+ ShaderPass,
9
+ ShaderPassInputSource,
10
+ ShaderPassPipeline,
11
+ ShaderPassRenderTarget
12
+ } from '@luma.gl/shadertools';
7
13
  import {initializeShaderModule} from '@luma.gl/shadertools';
8
14
  import {ShaderInputs} from '../shader-inputs';
9
15
  import {DynamicTexture} from '../dynamic-texture/dynamic-texture';
16
+ import type {TextureBindingSource} from '../dynamic-texture/texture-binding-source';
10
17
  import {ClipSpace} from '../models/clip-space';
11
18
  import {SwapFramebuffers} from '../compute/swap';
12
19
  import {BackgroundTextureModel} from '../models/billboard-texture-model';
13
20
 
14
21
  import {getFragmentShaderForRenderPass} from './get-fragment-shader';
22
+ import {textureTransform} from './texture-transform-module';
15
23
 
24
+ type ShaderPassLike = ShaderPass | ShaderPassPipeline;
16
25
  type ShaderSubPass = NonNullable<ShaderPass['passes']>[0];
17
26
 
18
- /** Props for ShaderPassRenderer */
27
+ type EffectiveSubPass = {
28
+ ownerName: string;
29
+ shaderPass: ShaderPass;
30
+ subPassRenderer: SubPassRenderer;
31
+ inputs?: Partial<Record<string, ShaderPassInputSource<string>>>;
32
+ output?: 'previous' | string;
33
+ uniforms?: Record<string, unknown>;
34
+ };
35
+
36
+ type ManagedRenderTarget = {
37
+ name: string;
38
+ spec: ShaderPassRenderTarget;
39
+ texture: Texture;
40
+ framebuffer: Framebuffer;
41
+ historyTexture?: Texture;
42
+ historyFramebuffer?: Framebuffer;
43
+ historyInitialized: boolean;
44
+ writtenThisFrame: boolean;
45
+ };
46
+
47
+ const RESERVED_RENDER_TARGET_NAMES = new Set(['original', 'previous']);
48
+
49
+ /** Construction props for {@link ShaderPassRenderer}. */
19
50
  export type ShaderPassRendererProps = {
20
- /** List of ShaderPasses to apply to the sourceTexture */
21
- shaderPasses: ShaderPass[];
51
+ /** List of ShaderPasses or ShaderPassPipelines to apply to the sourceTexture */
52
+ shaderPasses: ShaderPassLike[];
22
53
  /** Optional typed ShaderInputs object for setting uniforms */
23
54
  shaderInputs?: ShaderInputs;
55
+ /** Format of the shared previous-chain textures. Defaults to the canvas-preferred format. */
56
+ colorFormat?: TextureFormatColor;
57
+ /** Flip source and presentation sampling vertically. Defaults to true on WebGPU. */
58
+ flipY?: boolean;
59
+ };
60
+
61
+ /** Source texture accepted by {@link ShaderPassRenderer}. */
62
+ export type ShaderPassSourceTexture = DynamicTexture | Texture;
63
+ /** Binding accepted by shader pass inputs, including deferred texture binding sources. */
64
+ export type ShaderPassBinding = Binding | TextureBindingSource;
65
+
66
+ /** Per-frame inputs accepted by {@link ShaderPassRenderer}. */
67
+ export type ShaderPassRendererRenderOptions = {
68
+ sourceTexture: ShaderPassSourceTexture;
69
+ uniforms?: Record<string, Record<string, unknown>>;
70
+ bindings?: Record<string, ShaderPassBinding>;
71
+ /** Invalidates all persistent render targets before rendering this frame. */
72
+ resetHistory?: boolean;
24
73
  };
25
74
 
26
- /** A pass that renders a given texture into screen space */
75
+ /**
76
+ * Runs one or more shader passes against a source texture.
77
+ *
78
+ * The renderer owns:
79
+ * - a shared ping-pong pair for the logical `previous` chain
80
+ * - any named render targets declared by `ShaderPassPipeline`
81
+ * - a fullscreen model used both to seed the chain and to present to the screen
82
+ *
83
+ * Per-draw `uniforms` and `bindings` passed to `renderToTexture()` or `renderToScreen()` are
84
+ * merged on top of the renderer's `shaderInputs`, which lets callers reuse one renderer instance
85
+ * while varying pass inputs frame to frame.
86
+ */
27
87
  export class ShaderPassRenderer {
28
88
  device: Device;
29
89
  shaderInputs: ShaderInputs;
@@ -34,9 +94,10 @@ export class ShaderPassRenderer {
34
94
  constructor(device: Device, props: ShaderPassRendererProps) {
35
95
  this.device = device;
36
96
 
37
- props.shaderPasses.map(shaderPass => initializeShaderModule(shaderPass));
97
+ const executableShaderPasses = getExecutableShaderPasses(props.shaderPasses);
98
+ executableShaderPasses.map(shaderPass => initializeShaderModule(shaderPass));
38
99
 
39
- const modules = props.shaderPasses.reduce(
100
+ const modules = executableShaderPasses.reduce(
40
101
  (object, shaderPass) => ({...object, [shaderPass.name]: shaderPass}),
41
102
  {}
42
103
  );
@@ -44,22 +105,26 @@ export class ShaderPassRenderer {
44
105
 
45
106
  const size = device.getCanvasContext().getDrawingBufferSize();
46
107
  this.swapFramebuffers = new SwapFramebuffers(device, {
47
- colorAttachments: [device.preferredColorFormat],
108
+ colorAttachments: [props.colorFormat || device.preferredColorFormat],
48
109
  width: size[0],
49
110
  height: size[1]
50
111
  });
51
112
 
52
113
  this.textureModel = new BackgroundTextureModel(device, {
53
- backgroundTexture: this.swapFramebuffers.current.colorAttachments[0].texture
114
+ backgroundTexture: this.swapFramebuffers.current.colorAttachments[0].texture,
115
+ flipY: props.flipY ?? device.type === 'webgpu'
54
116
  });
55
117
 
56
- this.passRenderers = props.shaderPasses.map(shaderPass => new PassRenderer(device, shaderPass));
118
+ const flipY = props.flipY ?? device.type === 'webgpu';
119
+ this.passRenderers = props.shaderPasses.map(
120
+ shaderPass => new PassRenderer(device, shaderPass, this.shaderInputs, flipY)
121
+ );
57
122
  }
58
123
 
59
124
  /** Destroys resources created by this ShaderPassRenderer */
60
125
  destroy() {
61
- for (const subPassRenderer of this.passRenderers) {
62
- subPassRenderer.destroy();
126
+ for (const passRenderer of this.passRenderers) {
127
+ passRenderer.destroy();
63
128
  }
64
129
  this.swapFramebuffers.destroy();
65
130
  this.textureModel.destroy();
@@ -68,30 +133,36 @@ export class ShaderPassRenderer {
68
133
  resize(size?: [width: number, height: number]): void {
69
134
  size ||= this.device.getCanvasContext().getDrawingBufferSize();
70
135
  this.swapFramebuffers.resize({width: size[0], height: size[1]});
136
+ for (const passRenderer of this.passRenderers) {
137
+ passRenderer.resize(size);
138
+ }
71
139
  }
72
140
 
73
- renderToScreen(options: {
74
- sourceTexture: DynamicTexture;
75
- uniforms?: any;
76
- bindings?: any;
77
- }): boolean {
78
- // Run the shader passes and generate an output texture
141
+ /** Invalidates persistent pipeline targets without destroying their allocations. */
142
+ resetHistory(): void {
143
+ for (const passRenderer of this.passRenderers) {
144
+ passRenderer.resetHistory();
145
+ }
146
+ }
147
+
148
+ renderToScreen(options: ShaderPassRendererRenderOptions): boolean {
79
149
  const outputTexture = this.renderToTexture(options);
80
150
  if (!outputTexture) {
81
- // source texture not yet loaded
82
151
  return false;
83
152
  }
84
153
 
85
154
  const framebuffer = this.device
86
155
  .getDefaultCanvasContext()
87
156
  .getCurrentFramebuffer({depthStencilFormat: false});
157
+ // Prepare fullscreen presentation bindings before opening the render pass so WebGPU
158
+ // uploads stay on the parent command encoder rather than the active render pass encoder.
159
+ this.textureModel.setProps({backgroundTexture: outputTexture});
160
+ this.textureModel.predraw(this.device.commandEncoder);
88
161
  const renderPass = this.device.beginRenderPass({
89
162
  id: 'shader-pass-renderer-to-screen',
90
163
  framebuffer,
91
- // clearColor: [1, 1, 0, 1],
92
164
  clearDepth: false
93
165
  });
94
- this.textureModel.setProps({backgroundTexture: outputTexture});
95
166
  this.textureModel.draw(renderPass);
96
167
  renderPass.end();
97
168
  return true;
@@ -100,106 +171,363 @@ export class ShaderPassRenderer {
100
171
  /** Runs the shaderPasses in sequence on the sourceTexture and returns a texture with the results.
101
172
  * @returns null if the the sourceTexture has not yet been loaded
102
173
  */
103
- renderToTexture(options: {
104
- sourceTexture: DynamicTexture;
105
- uniforms?: any;
106
- bindings?: any;
107
- }): Texture | null {
174
+ renderToTexture(options: ShaderPassRendererRenderOptions): Texture | null {
108
175
  const {sourceTexture} = options;
109
- if (!sourceTexture.isReady) {
176
+ if (sourceTexture instanceof DynamicTexture && !sourceTexture.isReady) {
110
177
  return null;
111
178
  }
179
+ const originalTexture =
180
+ sourceTexture instanceof DynamicTexture ? sourceTexture.texture : sourceTexture;
112
181
 
113
- // If no shader passes are provided, just return the original texture
114
182
  if (this.passRenderers.length === 0) {
115
- return sourceTexture.texture;
183
+ return originalTexture;
116
184
  }
117
185
 
118
- this.textureModel.setProps({backgroundTexture: sourceTexture});
186
+ if (options.resetHistory) {
187
+ this.resetHistory();
188
+ }
119
189
 
120
- // Clear the current texture before we begin
121
- const clearTexturePass = this.device.beginRenderPass({
122
- id: 'shader-pass-renderer-clear-texture',
123
- framebuffer: this.swapFramebuffers.current,
124
- clearColor: [1, 0, 0, 1]
190
+ // Seed the first shared framebuffer with the original source. This normalizes the starting
191
+ // point for later passes so `previous` always refers to a drawing-buffer-sized texture.
192
+ this.textureModel.setProps({backgroundTexture: originalTexture});
193
+ // The seed draw updates fullscreen bind groups, so it must be prepared before the
194
+ // render pass begins to avoid command-encoder locking errors on WebGPU.
195
+ this.textureModel.predraw(this.device.commandEncoder);
196
+
197
+ const sourceFramebuffer = getSeedFramebuffer(this.swapFramebuffers, originalTexture);
198
+ const seedRenderPass = this.device.beginRenderPass({
199
+ id: 'shader-pass-renderer-seed-source',
200
+ framebuffer: sourceFramebuffer,
201
+ clearColor: [0, 0, 0, 1],
202
+ clearDepth: false
125
203
  });
126
- this.textureModel.draw(clearTexturePass);
127
- clearTexturePass.end();
128
-
129
- // Copy the texture contents
130
- // const commandEncoder = this.device.createCommandEncoder();
131
- // commandEncoder.copyTextureToTexture({
132
- // sourceTexture: sourceTexture.texture,
133
- // destinationTexture: this.swapFramebuffers.current.colorAttachments[0].texture
134
- // });
135
- // commandEncoder.finish();
136
-
137
- let first = true;
138
- for (const passRenderer of this.passRenderers) {
139
- for (const subPassRenderer of passRenderer.subPassRenderers) {
140
- if (!first) {
141
- this.swapFramebuffers.swap();
204
+ this.textureModel.draw(seedRenderPass);
205
+ seedRenderPass.end();
206
+
207
+ let previousFramebuffer: Framebuffer | null = sourceFramebuffer;
208
+ let previousTexture: Texture = getFramebufferTexture(sourceFramebuffer);
209
+ let frameSucceeded = false;
210
+ try {
211
+ for (const passRenderer of this.passRenderers) {
212
+ passRenderer.initializeHistoryTargets(originalTexture, this.textureModel);
213
+ for (const execution of passRenderer.subPassExecutions) {
214
+ const outputName = execution.output || 'previous';
215
+ const outputFramebuffer: Framebuffer =
216
+ outputName === 'previous'
217
+ ? getNextPreviousFramebuffer(this.swapFramebuffers, previousFramebuffer)
218
+ : passRenderer.getOutputFramebuffer(outputName);
219
+ const outputTexture = getFramebufferTexture(outputFramebuffer);
220
+ // Pipeline routing resolves logical input names like `original`, `previous`, or a named
221
+ // render target into concrete textures for this draw.
222
+ const resolvedBindings = passRenderer.resolveBindings({
223
+ execution,
224
+ originalTexture,
225
+ previousTexture,
226
+ outputTexture,
227
+ externalBindings: options.bindings || {}
228
+ });
229
+ // Runtime uniforms are layered last so callers can update a renderer-owned pass without
230
+ // reconstructing it every frame.
231
+ const resolvedUniforms = resolveExecutionUniforms(
232
+ this.shaderInputs,
233
+ execution,
234
+ options.uniforms || {}
235
+ );
236
+
237
+ execution.subPassRenderer.prepare({
238
+ commandEncoder: this.device.commandEncoder,
239
+ bindings: resolvedBindings,
240
+ textureScale: calculateTextureScale(
241
+ (resolvedBindings['sourceTexture'] as Texture) || previousTexture,
242
+ outputTexture
243
+ ),
244
+ uniforms: resolvedUniforms
245
+ });
246
+ const renderPass = this.device.beginRenderPass({
247
+ id: 'shader-pass-renderer-run-pass',
248
+ framebuffer: outputFramebuffer,
249
+ clearColor: [0, 0, 0, 1],
250
+ clearDepth: 1
251
+ });
252
+ execution.subPassRenderer.draw(renderPass);
253
+ renderPass.end();
254
+
255
+ if (outputName === 'previous') {
256
+ previousTexture = outputTexture;
257
+ previousFramebuffer = outputFramebuffer;
258
+ } else {
259
+ passRenderer.markTargetWritten(outputName);
260
+ }
142
261
  }
143
- first = false;
262
+ }
263
+ frameSucceeded = true;
264
+ } finally {
265
+ for (const passRenderer of this.passRenderers) {
266
+ passRenderer.finishFrame(frameSucceeded);
267
+ }
268
+ }
269
+
270
+ return previousTexture;
271
+ }
272
+ }
273
+
274
+ /** Renders one `ShaderPass` or `ShaderPassPipeline` entry in the chain. */
275
+ class PassRenderer {
276
+ device: Device;
277
+ shaderInputs: ShaderInputs;
278
+ passDefinition: ShaderPassLike;
279
+ renderTargets: Record<string, ManagedRenderTarget>;
280
+ subPassExecutions: EffectiveSubPass[];
281
+
282
+ constructor(
283
+ device: Device,
284
+ passDefinition: ShaderPassLike,
285
+ shaderInputs: ShaderInputs,
286
+ flipY: boolean
287
+ ) {
288
+ this.device = device;
289
+ this.shaderInputs = shaderInputs;
290
+ this.passDefinition = passDefinition;
291
+
292
+ if (isShaderPassPipeline(passDefinition)) {
293
+ validateRenderTargetNames(passDefinition.name, passDefinition.renderTargets || {});
294
+ this.renderTargets = createManagedRenderTargets(device, passDefinition.renderTargets || {});
295
+ this.subPassExecutions = passDefinition.steps.flatMap(step =>
296
+ this.createStepExecutions(passDefinition, step, flipY)
297
+ );
298
+ return;
299
+ }
300
+
301
+ validateShaderPassDoesNotOwnRenderTargets(passDefinition, passDefinition.name);
302
+ this.renderTargets = {};
303
+ this.subPassExecutions = this.createPassExecutions(passDefinition, {
304
+ ownerName: passDefinition.name,
305
+ flipY
306
+ });
307
+ }
308
+
309
+ destroy() {
310
+ for (const execution of this.subPassExecutions) {
311
+ execution.subPassRenderer.destroy();
312
+ }
313
+ destroyManagedRenderTargets(this.renderTargets);
314
+ }
144
315
 
145
- const swapBufferTexture = this.swapFramebuffers.current.colorAttachments[0].texture;
316
+ resize(size: [width: number, height: number]) {
317
+ resizeManagedRenderTargets(this.device, this.renderTargets, size);
318
+ }
146
319
 
147
- const bindings = {
148
- sourceTexture: swapBufferTexture
149
- // texSize: [sourceTextures.width, sourceTextures.height]
150
- };
320
+ resetHistory(): void {
321
+ for (const renderTarget of Object.values(this.renderTargets)) {
322
+ renderTarget.historyInitialized = false;
323
+ renderTarget.writtenThisFrame = false;
324
+ }
325
+ }
151
326
 
327
+ initializeHistoryTargets(originalTexture: Texture, textureModel: BackgroundTextureModel): void {
328
+ for (const renderTarget of Object.values(this.renderTargets)) {
329
+ if (renderTarget.spec.lifetime !== 'history' || renderTarget.historyInitialized) {
330
+ continue;
331
+ }
332
+ const historyFramebuffer = getHistoryFramebuffer(renderTarget);
333
+ const initialize = renderTarget.spec.initialize || {
334
+ clearColor: [0, 0, 0, 0] as [number, number, number, number]
335
+ };
336
+ if (initialize === 'original') {
337
+ textureModel.setProps({backgroundTexture: originalTexture});
338
+ textureModel.predraw(this.device.commandEncoder);
339
+ const renderPass = this.device.beginRenderPass({
340
+ id: `${renderTarget.name}-initialize-history`,
341
+ framebuffer: historyFramebuffer,
342
+ clearColor: [0, 0, 0, 0],
343
+ clearDepth: false
344
+ });
345
+ textureModel.draw(renderPass);
346
+ renderPass.end();
347
+ } else {
152
348
  const renderPass = this.device.beginRenderPass({
153
- id: 'shader-pass-renderer-run-pass',
154
- framebuffer: this.swapFramebuffers.next,
155
- clearColor: [0, 0, 0, 1],
156
- clearDepth: 1
349
+ id: `${renderTarget.name}-clear-history`,
350
+ framebuffer: historyFramebuffer,
351
+ clearColor: initialize.clearColor,
352
+ clearDepth: false
157
353
  });
158
- subPassRenderer.render({renderPass, bindings});
159
354
  renderPass.end();
160
355
  }
356
+ renderTarget.historyInitialized = true;
161
357
  }
358
+ }
162
359
 
163
- this.swapFramebuffers.swap();
164
- const outputTexture = this.swapFramebuffers.current.colorAttachments[0].texture;
165
- return outputTexture;
360
+ getOutputFramebuffer(name: string): Framebuffer {
361
+ return this.getRenderTarget(name).framebuffer;
166
362
  }
167
- }
168
363
 
169
- /** renders one ShaderPass */
170
- class PassRenderer {
171
- shaderPass: ShaderPass;
172
- subPassRenderers: SubPassRenderer[];
364
+ markTargetWritten(name: string): void {
365
+ this.getRenderTarget(name).writtenThisFrame = true;
366
+ }
173
367
 
174
- constructor(device: Device, shaderPass: ShaderPass, props = {}) {
175
- this.shaderPass = shaderPass;
176
- // const id = `${shaderPass.name}-pass`;
368
+ finishFrame(succeeded: boolean): void {
369
+ for (const renderTarget of Object.values(this.renderTargets)) {
370
+ if (succeeded && renderTarget.spec.lifetime === 'history' && renderTarget.writtenThisFrame) {
371
+ const historyTexture = getHistoryTexture(renderTarget);
372
+ const historyFramebuffer = getHistoryFramebuffer(renderTarget);
373
+ renderTarget.historyTexture = renderTarget.texture;
374
+ renderTarget.historyFramebuffer = renderTarget.framebuffer;
375
+ renderTarget.texture = historyTexture;
376
+ renderTarget.framebuffer = historyFramebuffer;
377
+ renderTarget.historyInitialized = true;
378
+ }
379
+ renderTarget.writtenThisFrame = false;
380
+ }
381
+ }
177
382
 
178
- const subPasses = shaderPass.passes || [];
179
- // normalizePasses(gl, module, id, props);
383
+ getRenderTarget(name: string): ManagedRenderTarget {
384
+ const renderTarget = this.renderTargets[name];
385
+ if (!renderTarget) {
386
+ throw new Error(`${this.getOwnerName()}: unknown render target "${name}"`);
387
+ }
388
+ return renderTarget;
389
+ }
390
+
391
+ resolveBindings(options: {
392
+ execution: EffectiveSubPass;
393
+ originalTexture: Texture;
394
+ previousTexture: Texture;
395
+ outputTexture: Texture;
396
+ externalBindings: Record<string, ShaderPassBinding>;
397
+ }): Record<string, ShaderPassBinding> {
398
+ const {execution, originalTexture, previousTexture, outputTexture, externalBindings} = options;
399
+ const inputMap = execution.inputs || {sourceTexture: 'previous'};
400
+ const shaderInputBindings = this.shaderInputs.getModuleBindingValues(execution.shaderPass.name);
401
+ const externalBindingsForPass = Object.fromEntries(
402
+ Object.entries(externalBindings).filter(([name]) =>
403
+ execution.shaderPass.bindingLayout?.some(binding => binding.name === name)
404
+ )
405
+ );
406
+ // Shader-pass bindings stored in `shaderInputs` act as renderer-owned defaults. Per-draw
407
+ // external bindings can override those defaults for frame-specific resources, while routed
408
+ // logical inputs such as `sourceTexture` remain authoritative below.
409
+ const resolvedBindings: Record<string, ShaderPassBinding> = {
410
+ ...shaderInputBindings,
411
+ ...externalBindingsForPass
412
+ };
413
+ const outputName = execution.output || 'previous';
414
+
415
+ for (const [bindingName, inputSource] of Object.entries(inputMap)) {
416
+ if (!inputSource) {
417
+ continue;
418
+ }
419
+ const texture = this.resolveInputTexture(inputSource, originalTexture, previousTexture);
420
+ const target = inputSource in this.renderTargets ? this.renderTargets[inputSource] : null;
421
+ if (
422
+ outputName !== 'previous' &&
423
+ inputSource === outputName &&
424
+ target?.spec.lifetime !== 'history'
425
+ ) {
426
+ throw new Error(
427
+ `${execution.ownerName}: subpass cannot read and write render target "${outputName}" in the same draw`
428
+ );
429
+ }
430
+ if (bindingName === 'sourceTexture' && texture === outputTexture) {
431
+ throw new Error(
432
+ `${execution.ownerName}: subpass cannot sample from the render target it is writing to`
433
+ );
434
+ }
435
+ resolvedBindings[bindingName] = texture;
436
+ }
180
437
 
181
- this.subPassRenderers = subPasses.map(subPass => {
182
- // const idn = `${id}-${subPasses.length + 1}`;
183
- return new SubPassRenderer(device, shaderPass, subPass);
438
+ if (!('sourceTexture' in resolvedBindings)) {
439
+ resolvedBindings['sourceTexture'] = previousTexture;
440
+ }
441
+
442
+ return resolvedBindings;
443
+ }
444
+
445
+ private createStepExecutions(
446
+ pipeline: ShaderPassPipeline,
447
+ step: ShaderPassPipeline['steps'][number],
448
+ flipY: boolean
449
+ ): EffectiveSubPass[] {
450
+ validateShaderPassDoesNotOwnRenderTargets(
451
+ step.shaderPass,
452
+ `${pipeline.name}/${step.shaderPass.name}`
453
+ );
454
+
455
+ return this.createPassExecutions(step.shaderPass, {
456
+ ownerName: `${pipeline.name}/${step.shaderPass.name}`,
457
+ firstInputs: step.inputs,
458
+ lastOutput: step.output,
459
+ uniformOverrides: step.uniforms,
460
+ flipY
184
461
  });
185
462
  }
186
463
 
187
- destroy() {
188
- for (const subPassRenderer of this.subPassRenderers) {
189
- subPassRenderer.destroy();
464
+ private createPassExecutions(
465
+ shaderPass: ShaderPass,
466
+ options: {
467
+ ownerName: string;
468
+ firstInputs?: Partial<Record<string, ShaderPassInputSource<string>>>;
469
+ lastOutput?: 'previous' | string;
470
+ uniformOverrides?: Record<string, unknown>;
471
+ flipY: boolean;
472
+ }
473
+ ): EffectiveSubPass[] {
474
+ const subPasses = shaderPass.passes || [];
475
+ return subPasses.map((subPass, index) => {
476
+ const isFirstSubPass = index === 0;
477
+ const isLastSubPass = index === subPasses.length - 1;
478
+ const inputs =
479
+ isFirstSubPass && options.firstInputs !== undefined ? options.firstInputs : subPass.inputs;
480
+ const output =
481
+ isLastSubPass && options.lastOutput !== undefined ? options.lastOutput : subPass.output;
482
+ validateSubPassRouting(options.ownerName, inputs, output, this.renderTargets);
483
+
484
+ return {
485
+ ownerName: options.ownerName,
486
+ shaderPass,
487
+ subPassRenderer: new SubPassRenderer(this.device, shaderPass, subPass, options.flipY),
488
+ inputs,
489
+ output,
490
+ uniforms: mergeUniforms(options.uniformOverrides, subPass.uniforms)
491
+ };
492
+ });
493
+ }
494
+
495
+ private resolveInputTexture(
496
+ inputSource: string,
497
+ originalTexture: Texture,
498
+ previousTexture: Texture
499
+ ): Texture {
500
+ switch (inputSource) {
501
+ case 'original':
502
+ return originalTexture;
503
+ case 'previous':
504
+ return previousTexture;
505
+ default: {
506
+ const renderTarget = this.getRenderTarget(inputSource);
507
+ if (renderTarget.spec.lifetime === 'history' && !renderTarget.writtenThisFrame) {
508
+ return getHistoryTexture(renderTarget);
509
+ }
510
+ return renderTarget.texture;
511
+ }
190
512
  }
191
513
  }
514
+
515
+ private getOwnerName(): string {
516
+ return this.passDefinition.name;
517
+ }
192
518
  }
193
519
 
194
- /** Renders one subpass of a ShaderPass */
520
+ /** Renders a single subpass of a `ShaderPass`. */
195
521
  class SubPassRenderer {
196
522
  model: ClipSpace;
197
523
  shaderPass: ShaderPass;
198
524
  subPass: ShaderSubPass;
525
+ flipY: boolean;
199
526
 
200
- constructor(device: Device, shaderPass: ShaderPass, subPass: ShaderSubPass) {
527
+ constructor(device: Device, shaderPass: ShaderPass, subPass: ShaderSubPass, flipY: boolean) {
201
528
  this.shaderPass = shaderPass;
202
529
  this.subPass = subPass;
530
+ this.flipY = flipY;
203
531
  const action =
204
532
  subPass.action || (subPass.filter && 'filter') || (subPass.sampler && 'sample') || 'filter';
205
533
  const fs = getFragmentShaderForRenderPass({
@@ -212,7 +540,7 @@ class SubPassRenderer {
212
540
  id: `${shaderPass.name}-subpass`,
213
541
  source: fs,
214
542
  fs,
215
- modules: [shaderPass],
543
+ modules: [textureTransform, shaderPass],
216
544
  parameters: {
217
545
  depthWriteEnabled: false
218
546
  }
@@ -223,17 +551,291 @@ class SubPassRenderer {
223
551
  this.model.destroy();
224
552
  }
225
553
 
226
- render(options: {renderPass: RenderPass; bindings: any}): void {
227
- const {renderPass, bindings} = options;
554
+ prepare(options: {
555
+ commandEncoder: CommandEncoder;
556
+ bindings: Record<string, ShaderPassBinding>;
557
+ textureScale: [number, number];
558
+ uniforms?: Record<string, unknown>;
559
+ }): void {
560
+ const {commandEncoder, bindings, textureScale, uniforms} = options;
228
561
 
562
+ this.model.shaderInputs.setProps({
563
+ textureTransform: {scale: textureScale, flipY: this.flipY ? 1 : 0}
564
+ });
229
565
  this.model.shaderInputs.setProps({
230
566
  [this.shaderPass.name]: this.shaderPass.uniforms || {}
231
567
  });
232
568
  this.model.shaderInputs.setProps({
233
- [this.shaderPass.name]: this.subPass.uniforms || {}
569
+ [this.shaderPass.name]: uniforms || {}
234
570
  });
235
- // this.model.setBindings(this.subPass.bindings || {});
236
571
  this.model.setBindings(bindings || {});
572
+ this.model.predraw(commandEncoder);
573
+ }
574
+
575
+ draw(renderPass: RenderPass): void {
237
576
  this.model.draw(renderPass);
238
577
  }
239
578
  }
579
+
580
+ function getExecutableShaderPasses(shaderPasses: ShaderPassLike[]): ShaderPass[] {
581
+ return shaderPasses.flatMap(shaderPass =>
582
+ isShaderPassPipeline(shaderPass) ? shaderPass.steps.map(step => step.shaderPass) : [shaderPass]
583
+ );
584
+ }
585
+
586
+ function isShaderPassPipeline(shaderPass: ShaderPassLike): shaderPass is ShaderPassPipeline {
587
+ return 'steps' in shaderPass;
588
+ }
589
+
590
+ function validateShaderPassDoesNotOwnRenderTargets(
591
+ shaderPass: ShaderPass,
592
+ ownerName: string
593
+ ): void {
594
+ const renderTargets = (shaderPass as ShaderPass & {renderTargets?: Record<string, unknown>})
595
+ .renderTargets;
596
+ if (renderTargets && Object.keys(renderTargets).length > 0) {
597
+ throw new Error(
598
+ `${ownerName}: ShaderPass.renderTargets is not supported; use ShaderPassPipeline.renderTargets instead`
599
+ );
600
+ }
601
+ }
602
+
603
+ function validateRenderTargetNames(
604
+ ownerName: string,
605
+ renderTargets: Record<string, ShaderPassRenderTarget>
606
+ ): void {
607
+ for (const name of Object.keys(renderTargets)) {
608
+ if (RESERVED_RENDER_TARGET_NAMES.has(name)) {
609
+ throw new Error(`${ownerName}: render target name "${name}" is reserved`);
610
+ }
611
+ }
612
+ }
613
+
614
+ function validateSubPassRouting(
615
+ ownerName: string,
616
+ inputs: Partial<Record<string, ShaderPassInputSource<string>>> | undefined,
617
+ output: 'previous' | string | undefined,
618
+ renderTargets: Record<string, ManagedRenderTarget>
619
+ ): void {
620
+ const inputMap = inputs || {sourceTexture: 'previous'};
621
+ for (const inputSource of Object.values(inputMap)) {
622
+ if (
623
+ inputSource &&
624
+ inputSource !== 'original' &&
625
+ inputSource !== 'previous' &&
626
+ !(inputSource in renderTargets)
627
+ ) {
628
+ throw new Error(`${ownerName}: unknown input source "${inputSource}"`);
629
+ }
630
+ }
631
+
632
+ if (output && output !== 'previous' && !(output in renderTargets)) {
633
+ throw new Error(`${ownerName}: unknown output target "${output}"`);
634
+ }
635
+ }
636
+
637
+ function mergeUniforms(
638
+ baseUniforms?: Record<string, unknown>,
639
+ subPassUniforms?: Record<string, unknown>
640
+ ): Record<string, unknown> | undefined {
641
+ if (!baseUniforms && !subPassUniforms) {
642
+ return undefined;
643
+ }
644
+
645
+ return {
646
+ ...(baseUniforms || {}),
647
+ ...(subPassUniforms || {})
648
+ };
649
+ }
650
+
651
+ /**
652
+ * Resolves the effective uniform block for one execution.
653
+ *
654
+ * Merge order is:
655
+ * 1. persisted renderer-level shader inputs
656
+ * 2. uniforms baked into the pipeline or subpass definition
657
+ * 3. per-draw runtime overrides
658
+ */
659
+ function resolveExecutionUniforms(
660
+ shaderInputs: ShaderInputs,
661
+ execution: EffectiveSubPass,
662
+ runtimeUniforms: Record<string, Record<string, unknown>>
663
+ ): Record<string, unknown> | undefined {
664
+ return mergeUniforms(
665
+ mergeUniforms(
666
+ shaderInputs.getUniformValues()[execution.shaderPass.name] as Record<string, unknown>,
667
+ execution.uniforms
668
+ ),
669
+ runtimeUniforms[execution.shaderPass.name]
670
+ );
671
+ }
672
+
673
+ function createManagedRenderTargets(
674
+ device: Device,
675
+ renderTargets: Record<string, ShaderPassRenderTarget>
676
+ ): Record<string, ManagedRenderTarget> {
677
+ const size = device.getCanvasContext().getDrawingBufferSize();
678
+ return Object.entries(renderTargets).reduce(
679
+ (managedRenderTargets, [name, spec]) => ({
680
+ ...managedRenderTargets,
681
+ [name]: createManagedRenderTarget(device, name, spec, size)
682
+ }),
683
+ {} as Record<string, ManagedRenderTarget>
684
+ );
685
+ }
686
+
687
+ function destroyManagedRenderTargets(renderTargets: Record<string, ManagedRenderTarget>): void {
688
+ for (const renderTarget of Object.values(renderTargets)) {
689
+ renderTarget.framebuffer.destroy();
690
+ renderTarget.texture.destroy();
691
+ renderTarget.historyFramebuffer?.destroy();
692
+ renderTarget.historyTexture?.destroy();
693
+ }
694
+ }
695
+
696
+ function resizeManagedRenderTargets(
697
+ device: Device,
698
+ renderTargets: Record<string, ManagedRenderTarget>,
699
+ size: [width: number, height: number]
700
+ ): void {
701
+ for (const renderTarget of Object.values(renderTargets)) {
702
+ const targetSize = getRenderTargetSize(size, renderTarget.spec.scale);
703
+ if (
704
+ renderTarget.texture.width === targetSize[0] &&
705
+ renderTarget.texture.height === targetSize[1]
706
+ ) {
707
+ continue;
708
+ }
709
+
710
+ renderTarget.framebuffer.destroy();
711
+ renderTarget.texture.destroy();
712
+ renderTarget.historyFramebuffer?.destroy();
713
+ renderTarget.historyTexture?.destroy();
714
+ const replacement = createManagedRenderTarget(
715
+ device,
716
+ renderTarget.name,
717
+ renderTarget.spec,
718
+ size
719
+ );
720
+ renderTarget.texture = replacement.texture;
721
+ renderTarget.framebuffer = replacement.framebuffer;
722
+ renderTarget.historyTexture = replacement.historyTexture;
723
+ renderTarget.historyFramebuffer = replacement.historyFramebuffer;
724
+ renderTarget.historyInitialized = false;
725
+ renderTarget.writtenThisFrame = false;
726
+ }
727
+ }
728
+
729
+ function createManagedRenderTarget(
730
+ device: Device,
731
+ name: string,
732
+ spec: ShaderPassRenderTarget,
733
+ size: [number, number]
734
+ ): ManagedRenderTarget {
735
+ const targetSize = getRenderTargetSize(size, spec.scale);
736
+ const {texture, framebuffer} = createTargetResources(device, name, spec, targetSize);
737
+ let historyTexture: Texture | undefined;
738
+ let historyFramebuffer: Framebuffer | undefined;
739
+ if (spec.lifetime === 'history') {
740
+ const historyResources = createTargetResources(device, `${name}-history`, spec, targetSize);
741
+ historyTexture = historyResources.texture;
742
+ historyFramebuffer = historyResources.framebuffer;
743
+ }
744
+
745
+ return {
746
+ name,
747
+ spec,
748
+ texture,
749
+ framebuffer,
750
+ historyTexture,
751
+ historyFramebuffer,
752
+ historyInitialized: false,
753
+ writtenThisFrame: false
754
+ };
755
+ }
756
+
757
+ function createTargetResources(
758
+ device: Device,
759
+ name: string,
760
+ spec: ShaderPassRenderTarget,
761
+ targetSize: [number, number]
762
+ ): {texture: Texture; framebuffer: Framebuffer} {
763
+ const texture = device.createTexture({
764
+ id: `${name}-texture`,
765
+ width: targetSize[0],
766
+ height: targetSize[1],
767
+ format: spec.format || device.preferredColorFormat,
768
+ usage: Texture.SAMPLE | Texture.RENDER | Texture.COPY_SRC | Texture.COPY_DST
769
+ });
770
+ const framebuffer = device.createFramebuffer({
771
+ id: `${name}-framebuffer`,
772
+ width: targetSize[0],
773
+ height: targetSize[1],
774
+ colorAttachments: [texture]
775
+ });
776
+
777
+ return {texture, framebuffer};
778
+ }
779
+
780
+ function getHistoryTexture(renderTarget: ManagedRenderTarget): Texture {
781
+ if (!renderTarget.historyTexture) {
782
+ throw new Error(`${renderTarget.name}: transient render target has no history texture`);
783
+ }
784
+ return renderTarget.historyTexture;
785
+ }
786
+
787
+ function getHistoryFramebuffer(renderTarget: ManagedRenderTarget): Framebuffer {
788
+ if (!renderTarget.historyFramebuffer) {
789
+ throw new Error(`${renderTarget.name}: transient render target has no history framebuffer`);
790
+ }
791
+ return renderTarget.historyFramebuffer;
792
+ }
793
+
794
+ function getRenderTargetSize(
795
+ size: [width: number, height: number],
796
+ scale: [number, number] = [1, 1]
797
+ ): [width: number, height: number] {
798
+ return [Math.max(1, Math.round(size[0] * scale[0])), Math.max(1, Math.round(size[1] * scale[1]))];
799
+ }
800
+
801
+ function getFramebufferTexture(framebuffer: Framebuffer): Texture {
802
+ const texture = framebuffer.colorAttachments[0]?.texture;
803
+ if (!texture) {
804
+ throw new Error('ShaderPassRenderer: framebuffer is missing a color attachment texture');
805
+ }
806
+ return texture;
807
+ }
808
+
809
+ function getNextPreviousFramebuffer(
810
+ swapFramebuffers: SwapFramebuffers,
811
+ previousFramebuffer: Framebuffer | null
812
+ ): Framebuffer {
813
+ if (previousFramebuffer === swapFramebuffers.current) {
814
+ return swapFramebuffers.next;
815
+ }
816
+ return swapFramebuffers.current;
817
+ }
818
+
819
+ function getSeedFramebuffer(
820
+ swapFramebuffers: SwapFramebuffers,
821
+ originalTexture: Texture
822
+ ): Framebuffer {
823
+ if (getFramebufferTexture(swapFramebuffers.current) === originalTexture) {
824
+ return swapFramebuffers.next;
825
+ }
826
+ return swapFramebuffers.current;
827
+ }
828
+
829
+ function calculateTextureScale(
830
+ sourceTexture: Texture,
831
+ outputTexture: Texture
832
+ ): [scaleX: number, scaleY: number] {
833
+ const sourceAspect = sourceTexture.width / sourceTexture.height;
834
+ const outputAspect = outputTexture.width / outputTexture.height;
835
+
836
+ if (outputAspect > sourceAspect) {
837
+ return [1, outputAspect / sourceAspect];
838
+ }
839
+
840
+ return [sourceAspect / outputAspect, 1];
841
+ }