@motion-core/motion-gpu 0.4.1 → 0.5.0
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.
- package/README.md +99 -0
- package/dist/advanced.d.ts +1 -0
- package/dist/advanced.d.ts.map +1 -0
- package/dist/advanced.js +14 -6
- package/dist/core/advanced.d.ts +1 -0
- package/dist/core/advanced.d.ts.map +1 -0
- package/dist/core/advanced.js +14 -5
- package/dist/core/compute-shader.d.ts +87 -0
- package/dist/core/compute-shader.d.ts.map +1 -0
- package/dist/core/compute-shader.js +205 -0
- package/dist/core/compute-shader.js.map +1 -0
- package/dist/core/current-value.d.ts +1 -0
- package/dist/core/current-value.d.ts.map +1 -0
- package/dist/core/current-value.js +35 -34
- package/dist/core/current-value.js.map +1 -0
- package/dist/core/error-diagnostics.d.ts +1 -0
- package/dist/core/error-diagnostics.d.ts.map +1 -0
- package/dist/core/error-diagnostics.js +70 -137
- package/dist/core/error-diagnostics.js.map +1 -0
- package/dist/core/error-report.d.ts +2 -1
- package/dist/core/error-report.d.ts.map +1 -0
- package/dist/core/error-report.js +247 -233
- package/dist/core/error-report.js.map +1 -0
- package/dist/core/frame-registry.d.ts +1 -0
- package/dist/core/frame-registry.d.ts.map +1 -0
- package/dist/core/frame-registry.js +546 -662
- package/dist/core/frame-registry.js.map +1 -0
- package/dist/core/index.d.ts +6 -2
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +13 -12
- package/dist/core/material-preprocess.d.ts +1 -0
- package/dist/core/material-preprocess.d.ts.map +1 -0
- package/dist/core/material-preprocess.js +131 -152
- package/dist/core/material-preprocess.js.map +1 -0
- package/dist/core/material.d.ts +23 -6
- package/dist/core/material.d.ts.map +1 -0
- package/dist/core/material.js +290 -317
- package/dist/core/material.js.map +1 -0
- package/dist/core/recompile-policy.d.ts +1 -0
- package/dist/core/recompile-policy.d.ts.map +1 -0
- package/dist/core/recompile-policy.js +18 -13
- package/dist/core/recompile-policy.js.map +1 -0
- package/dist/core/render-graph.d.ts +8 -3
- package/dist/core/render-graph.d.ts.map +1 -0
- package/dist/core/render-graph.js +77 -68
- package/dist/core/render-graph.js.map +1 -0
- package/dist/core/render-targets.d.ts +1 -0
- package/dist/core/render-targets.d.ts.map +1 -0
- package/dist/core/render-targets.js +52 -53
- package/dist/core/render-targets.js.map +1 -0
- package/dist/core/renderer.d.ts +1 -0
- package/dist/core/renderer.d.ts.map +1 -0
- package/dist/core/renderer.js +1337 -1081
- package/dist/core/renderer.js.map +1 -0
- package/dist/core/runtime-loop.d.ts +3 -2
- package/dist/core/runtime-loop.d.ts.map +1 -0
- package/dist/core/runtime-loop.js +353 -362
- package/dist/core/runtime-loop.js.map +1 -0
- package/dist/core/scheduler-helpers.d.ts +1 -0
- package/dist/core/scheduler-helpers.d.ts.map +1 -0
- package/dist/core/scheduler-helpers.js +52 -51
- package/dist/core/scheduler-helpers.js.map +1 -0
- package/dist/core/shader.d.ts +10 -1
- package/dist/core/shader.d.ts.map +1 -0
- package/dist/core/shader.js +109 -115
- package/dist/core/shader.js.map +1 -0
- package/dist/core/storage-buffers.d.ts +37 -0
- package/dist/core/storage-buffers.d.ts.map +1 -0
- package/dist/core/storage-buffers.js +95 -0
- package/dist/core/storage-buffers.js.map +1 -0
- package/dist/core/texture-loader.d.ts +1 -0
- package/dist/core/texture-loader.d.ts.map +1 -0
- package/dist/core/texture-loader.js +209 -273
- package/dist/core/texture-loader.js.map +1 -0
- package/dist/core/textures.d.ts +13 -0
- package/dist/core/textures.d.ts.map +1 -0
- package/dist/core/textures.js +111 -116
- package/dist/core/textures.js.map +1 -0
- package/dist/core/types.d.ts +147 -4
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +0 -4
- package/dist/core/uniforms.d.ts +1 -0
- package/dist/core/uniforms.d.ts.map +1 -0
- package/dist/core/uniforms.js +170 -191
- package/dist/core/uniforms.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -6
- package/dist/passes/BlitPass.d.ts +1 -0
- package/dist/passes/BlitPass.d.ts.map +1 -0
- package/dist/passes/BlitPass.js +23 -18
- package/dist/passes/BlitPass.js.map +1 -0
- package/dist/passes/ComputePass.d.ts +83 -0
- package/dist/passes/ComputePass.d.ts.map +1 -0
- package/dist/passes/ComputePass.js +92 -0
- package/dist/passes/ComputePass.js.map +1 -0
- package/dist/passes/CopyPass.d.ts +1 -0
- package/dist/passes/CopyPass.d.ts.map +1 -0
- package/dist/passes/CopyPass.js +58 -52
- package/dist/passes/CopyPass.js.map +1 -0
- package/dist/passes/FullscreenPass.d.ts +1 -0
- package/dist/passes/FullscreenPass.d.ts.map +1 -0
- package/dist/passes/FullscreenPass.js +127 -130
- package/dist/passes/FullscreenPass.js.map +1 -0
- package/dist/passes/PingPongComputePass.d.ts +104 -0
- package/dist/passes/PingPongComputePass.d.ts.map +1 -0
- package/dist/passes/PingPongComputePass.js +132 -0
- package/dist/passes/PingPongComputePass.js.map +1 -0
- package/dist/passes/ShaderPass.d.ts +1 -0
- package/dist/passes/ShaderPass.d.ts.map +1 -0
- package/dist/passes/ShaderPass.js +41 -37
- package/dist/passes/ShaderPass.js.map +1 -0
- package/dist/passes/index.d.ts +3 -0
- package/dist/passes/index.d.ts.map +1 -0
- package/dist/passes/index.js +6 -3
- package/dist/react/FragCanvas.d.ts +3 -2
- package/dist/react/FragCanvas.d.ts.map +1 -0
- package/dist/react/FragCanvas.js +234 -211
- package/dist/react/FragCanvas.js.map +1 -0
- package/dist/react/MotionGPUErrorOverlay.d.ts +1 -0
- package/dist/react/MotionGPUErrorOverlay.d.ts.map +1 -0
- package/dist/react/MotionGPUErrorOverlay.js +200 -14
- package/dist/react/MotionGPUErrorOverlay.js.map +1 -0
- package/dist/react/Portal.d.ts +1 -0
- package/dist/react/Portal.d.ts.map +1 -0
- package/dist/react/Portal.js +18 -21
- package/dist/react/Portal.js.map +1 -0
- package/dist/react/advanced.d.ts +1 -0
- package/dist/react/advanced.d.ts.map +1 -0
- package/dist/react/advanced.js +14 -6
- package/dist/react/frame-context.d.ts +1 -0
- package/dist/react/frame-context.d.ts.map +1 -0
- package/dist/react/frame-context.js +88 -94
- package/dist/react/frame-context.js.map +1 -0
- package/dist/react/index.d.ts +6 -2
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +12 -9
- package/dist/react/motiongpu-context.d.ts +1 -0
- package/dist/react/motiongpu-context.d.ts.map +1 -0
- package/dist/react/motiongpu-context.js +18 -15
- package/dist/react/motiongpu-context.js.map +1 -0
- package/dist/react/use-motiongpu-user-context.d.ts +1 -0
- package/dist/react/use-motiongpu-user-context.d.ts.map +1 -0
- package/dist/react/use-motiongpu-user-context.js +83 -82
- package/dist/react/use-motiongpu-user-context.js.map +1 -0
- package/dist/react/use-texture.d.ts +1 -0
- package/dist/react/use-texture.d.ts.map +1 -0
- package/dist/react/use-texture.js +132 -152
- package/dist/react/use-texture.js.map +1 -0
- package/dist/svelte/FragCanvas.svelte +2 -2
- package/dist/svelte/FragCanvas.svelte.d.ts +3 -2
- package/dist/svelte/FragCanvas.svelte.d.ts.map +1 -0
- package/dist/svelte/MotionGPUErrorOverlay.svelte +137 -7
- package/dist/svelte/MotionGPUErrorOverlay.svelte.d.ts +1 -0
- package/dist/svelte/MotionGPUErrorOverlay.svelte.d.ts.map +1 -0
- package/dist/svelte/Portal.svelte.d.ts +1 -0
- package/dist/svelte/Portal.svelte.d.ts.map +1 -0
- package/dist/svelte/advanced.d.ts +1 -0
- package/dist/svelte/advanced.d.ts.map +1 -0
- package/dist/svelte/advanced.js +13 -6
- package/dist/svelte/frame-context.d.ts +1 -0
- package/dist/svelte/frame-context.d.ts.map +1 -0
- package/dist/svelte/frame-context.js +27 -27
- package/dist/svelte/frame-context.js.map +1 -0
- package/dist/svelte/index.d.ts +6 -2
- package/dist/svelte/index.d.ts.map +1 -0
- package/dist/svelte/index.js +12 -9
- package/dist/svelte/motiongpu-context.d.ts +1 -0
- package/dist/svelte/motiongpu-context.d.ts.map +1 -0
- package/dist/svelte/motiongpu-context.js +24 -21
- package/dist/svelte/motiongpu-context.js.map +1 -0
- package/dist/svelte/use-motiongpu-user-context.d.ts +1 -0
- package/dist/svelte/use-motiongpu-user-context.d.ts.map +1 -0
- package/dist/svelte/use-motiongpu-user-context.js +69 -70
- package/dist/svelte/use-motiongpu-user-context.js.map +1 -0
- package/dist/svelte/use-texture.d.ts +1 -0
- package/dist/svelte/use-texture.d.ts.map +1 -0
- package/dist/svelte/use-texture.js +125 -147
- package/dist/svelte/use-texture.js.map +1 -0
- package/package.json +12 -7
- package/src/lib/advanced.ts +6 -0
- package/src/lib/core/advanced.ts +12 -0
- package/src/lib/core/compute-shader.ts +326 -0
- package/src/lib/core/current-value.ts +64 -0
- package/src/lib/core/error-diagnostics.ts +236 -0
- package/src/lib/core/error-report.ts +535 -0
- package/src/lib/core/frame-registry.ts +1190 -0
- package/src/lib/core/index.ts +94 -0
- package/src/lib/core/material-preprocess.ts +295 -0
- package/src/lib/core/material.ts +748 -0
- package/src/lib/core/recompile-policy.ts +31 -0
- package/src/lib/core/render-graph.ts +173 -0
- package/src/lib/core/render-targets.ts +107 -0
- package/src/lib/core/renderer.ts +2161 -0
- package/src/lib/core/runtime-loop.ts +537 -0
- package/src/lib/core/scheduler-helpers.ts +136 -0
- package/src/lib/core/shader.ts +301 -0
- package/src/lib/core/storage-buffers.ts +142 -0
- package/src/lib/core/texture-loader.ts +482 -0
- package/src/lib/core/textures.ts +257 -0
- package/src/lib/core/types.ts +743 -0
- package/src/lib/core/uniforms.ts +282 -0
- package/src/lib/index.ts +6 -0
- package/src/lib/passes/BlitPass.ts +54 -0
- package/src/lib/passes/ComputePass.ts +136 -0
- package/src/lib/passes/CopyPass.ts +80 -0
- package/src/lib/passes/FullscreenPass.ts +173 -0
- package/src/lib/passes/PingPongComputePass.ts +180 -0
- package/src/lib/passes/ShaderPass.ts +89 -0
- package/src/lib/passes/index.ts +9 -0
- package/src/lib/react/FragCanvas.tsx +345 -0
- package/src/lib/react/MotionGPUErrorOverlay.tsx +524 -0
- package/src/lib/react/Portal.tsx +34 -0
- package/src/lib/react/advanced.ts +36 -0
- package/src/lib/react/frame-context.ts +169 -0
- package/src/lib/react/index.ts +68 -0
- package/src/lib/react/motiongpu-context.ts +88 -0
- package/src/lib/react/use-motiongpu-user-context.ts +186 -0
- package/src/lib/react/use-texture.ts +233 -0
- package/src/lib/svelte/FragCanvas.svelte +249 -0
- package/src/lib/svelte/MotionGPUErrorOverlay.svelte +512 -0
- package/src/lib/svelte/Portal.svelte +31 -0
- package/src/lib/svelte/advanced.ts +32 -0
- package/src/lib/svelte/frame-context.ts +87 -0
- package/src/lib/svelte/index.ts +68 -0
- package/src/lib/svelte/motiongpu-context.ts +97 -0
- package/src/lib/svelte/use-motiongpu-user-context.ts +145 -0
- package/src/lib/svelte/use-texture.ts +232 -0
package/dist/core/renderer.js
CHANGED
|
@@ -1,284 +1,301 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { attachShaderCompilationDiagnostics } from
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { packUniformsInto } from "./uniforms.js";
|
|
2
|
+
import { getTextureMipLevelCount, normalizeTextureDefinitions, resolveTextureSize, resolveTextureUpdateMode, toTextureData } from "./textures.js";
|
|
3
|
+
import { normalizeStorageBufferDefinition } from "./storage-buffers.js";
|
|
4
|
+
import { attachShaderCompilationDiagnostics } from "./error-diagnostics.js";
|
|
5
|
+
import { buildShaderSourceWithMap, formatShaderSourceLocation } from "./shader.js";
|
|
6
|
+
import { buildRenderTargetSignature, resolveRenderTargetDefinitions } from "./render-targets.js";
|
|
7
|
+
import { planRenderGraph } from "./render-graph.js";
|
|
8
|
+
import { buildComputeShaderSource, buildPingPongComputeShaderSource, extractWorkgroupSize, storageTextureSampleScalarType } from "./compute-shader.js";
|
|
9
|
+
//#region src/lib/core/renderer.ts
|
|
7
10
|
/**
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
* Binding index for frame uniforms (`time`, `delta`, `resolution`).
|
|
12
|
+
*/
|
|
13
|
+
var FRAME_BINDING = 0;
|
|
11
14
|
/**
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
* Binding index for material uniform buffer.
|
|
16
|
+
*/
|
|
17
|
+
var UNIFORM_BINDING = 1;
|
|
15
18
|
/**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
* First binding index used for texture sampler/texture pairs.
|
|
20
|
+
*/
|
|
21
|
+
var FIRST_TEXTURE_BINDING = 2;
|
|
19
22
|
/**
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
* Returns sampler/texture binding slots for a texture index.
|
|
24
|
+
*/
|
|
22
25
|
function getTextureBindings(index) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
const samplerBinding = FIRST_TEXTURE_BINDING + index * 2;
|
|
27
|
+
return {
|
|
28
|
+
samplerBinding,
|
|
29
|
+
textureBinding: samplerBinding + 1
|
|
30
|
+
};
|
|
28
31
|
}
|
|
29
32
|
/**
|
|
30
|
-
|
|
31
|
-
|
|
33
|
+
* Maps WGSL scalar texture type to WebGPU sampled texture bind-group sample type.
|
|
34
|
+
*/
|
|
35
|
+
function toGpuTextureSampleType(type) {
|
|
36
|
+
if (type === "u32") return "uint";
|
|
37
|
+
if (type === "i32") return "sint";
|
|
38
|
+
return "float";
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Resizes canvas backing store to match client size and DPR.
|
|
42
|
+
*/
|
|
32
43
|
function resizeCanvas(canvas, dprInput, cssSize) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
const dpr = Number.isFinite(dprInput) && dprInput > 0 ? dprInput : 1;
|
|
45
|
+
const rect = cssSize ? null : canvas.getBoundingClientRect();
|
|
46
|
+
const cssWidth = Math.max(0, cssSize?.width ?? rect?.width ?? 0);
|
|
47
|
+
const cssHeight = Math.max(0, cssSize?.height ?? rect?.height ?? 0);
|
|
48
|
+
const width = Math.max(1, Math.floor((cssWidth || 1) * dpr));
|
|
49
|
+
const height = Math.max(1, Math.floor((cssHeight || 1) * dpr));
|
|
50
|
+
if (canvas.width !== width || canvas.height !== height) {
|
|
51
|
+
canvas.width = width;
|
|
52
|
+
canvas.height = height;
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
width,
|
|
56
|
+
height
|
|
57
|
+
};
|
|
44
58
|
}
|
|
45
59
|
/**
|
|
46
|
-
|
|
47
|
-
|
|
60
|
+
* Throws when a shader module contains WGSL compilation errors.
|
|
61
|
+
*/
|
|
48
62
|
async function assertCompilation(module, options) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const error = new Error(`WGSL compilation failed:\n${summary}`);
|
|
73
|
-
throw attachShaderCompilationDiagnostics(error, {
|
|
74
|
-
kind: 'shader-compilation',
|
|
75
|
-
diagnostics,
|
|
76
|
-
fragmentSource: options?.fragmentSource ?? '',
|
|
77
|
-
includeSources: options?.includeSources ?? {},
|
|
78
|
-
...(options?.defineBlockSource !== undefined
|
|
79
|
-
? { defineBlockSource: options.defineBlockSource }
|
|
80
|
-
: {}),
|
|
81
|
-
materialSource: options?.materialSource ?? null,
|
|
82
|
-
...(options?.runtimeContext !== undefined ? { runtimeContext: options.runtimeContext } : {})
|
|
83
|
-
});
|
|
63
|
+
const errors = (await module.getCompilationInfo()).messages.filter((message) => message.type === "error");
|
|
64
|
+
if (errors.length === 0) return;
|
|
65
|
+
const diagnostics = errors.map((message) => ({
|
|
66
|
+
generatedLine: message.lineNum,
|
|
67
|
+
message: message.message,
|
|
68
|
+
linePos: message.linePos,
|
|
69
|
+
lineLength: message.length,
|
|
70
|
+
sourceLocation: options?.lineMap?.[message.lineNum] ?? null
|
|
71
|
+
}));
|
|
72
|
+
const summary = diagnostics.map((diagnostic) => {
|
|
73
|
+
const contextLabel = [formatShaderSourceLocation(diagnostic.sourceLocation), diagnostic.generatedLine > 0 ? `generated WGSL line ${diagnostic.generatedLine}` : null].filter((value) => Boolean(value));
|
|
74
|
+
if (contextLabel.length === 0) return diagnostic.message;
|
|
75
|
+
return `[${contextLabel.join(" | ")}] ${diagnostic.message}`;
|
|
76
|
+
}).join("\n");
|
|
77
|
+
throw attachShaderCompilationDiagnostics(/* @__PURE__ */ new Error(`WGSL compilation failed:\n${summary}`), {
|
|
78
|
+
kind: "shader-compilation",
|
|
79
|
+
diagnostics,
|
|
80
|
+
fragmentSource: options?.fragmentSource ?? "",
|
|
81
|
+
includeSources: options?.includeSources ?? {},
|
|
82
|
+
...options?.defineBlockSource !== void 0 ? { defineBlockSource: options.defineBlockSource } : {},
|
|
83
|
+
materialSource: options?.materialSource ?? null,
|
|
84
|
+
...options?.runtimeContext !== void 0 ? { runtimeContext: options.runtimeContext } : {}
|
|
85
|
+
});
|
|
84
86
|
}
|
|
85
87
|
function toSortedUniqueStrings(values) {
|
|
86
|
-
|
|
88
|
+
return Array.from(new Set(values)).sort((a, b) => a.localeCompare(b));
|
|
87
89
|
}
|
|
88
90
|
function buildPassGraphSnapshot(passes) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
91
|
+
const declaredPasses = passes ?? [];
|
|
92
|
+
let enabledPassCount = 0;
|
|
93
|
+
const inputs = [];
|
|
94
|
+
const outputs = [];
|
|
95
|
+
for (const pass of declaredPasses) {
|
|
96
|
+
if (pass.enabled === false) continue;
|
|
97
|
+
enabledPassCount += 1;
|
|
98
|
+
if ("isCompute" in pass && pass.isCompute === true) continue;
|
|
99
|
+
const rp = pass;
|
|
100
|
+
const needsSwap = rp.needsSwap ?? true;
|
|
101
|
+
const input = rp.input ?? "source";
|
|
102
|
+
const output = rp.output ?? (needsSwap ? "target" : "source");
|
|
103
|
+
inputs.push(input);
|
|
104
|
+
outputs.push(output);
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
passCount: declaredPasses.length,
|
|
108
|
+
enabledPassCount,
|
|
109
|
+
inputs: toSortedUniqueStrings(inputs),
|
|
110
|
+
outputs: toSortedUniqueStrings(outputs)
|
|
111
|
+
};
|
|
110
112
|
}
|
|
111
113
|
function buildShaderCompilationRuntimeContext(options) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
114
|
+
const passList = options.getPasses?.() ?? options.passes;
|
|
115
|
+
const renderTargetMap = options.getRenderTargets?.() ?? options.renderTargets;
|
|
116
|
+
return {
|
|
117
|
+
...options.materialSignature ? { materialSignature: options.materialSignature } : {},
|
|
118
|
+
passGraph: buildPassGraphSnapshot(passList),
|
|
119
|
+
activeRenderTargets: Object.keys(renderTargetMap ?? {}).sort((a, b) => a.localeCompare(b))
|
|
120
|
+
};
|
|
119
121
|
}
|
|
120
122
|
/**
|
|
121
|
-
|
|
122
|
-
|
|
123
|
+
* Creates a 1x1 white fallback texture used before user textures become available.
|
|
124
|
+
*/
|
|
123
125
|
function createFallbackTexture(device, format) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
126
|
+
const texture = device.createTexture({
|
|
127
|
+
size: {
|
|
128
|
+
width: 1,
|
|
129
|
+
height: 1,
|
|
130
|
+
depthOrArrayLayers: 1
|
|
131
|
+
},
|
|
132
|
+
format,
|
|
133
|
+
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT
|
|
134
|
+
});
|
|
135
|
+
const pixel = new Uint8Array([
|
|
136
|
+
255,
|
|
137
|
+
255,
|
|
138
|
+
255,
|
|
139
|
+
255
|
|
140
|
+
]);
|
|
141
|
+
device.queue.writeTexture({ texture }, pixel, {
|
|
142
|
+
offset: 0,
|
|
143
|
+
bytesPerRow: 4,
|
|
144
|
+
rowsPerImage: 1
|
|
145
|
+
}, {
|
|
146
|
+
width: 1,
|
|
147
|
+
height: 1,
|
|
148
|
+
depthOrArrayLayers: 1
|
|
149
|
+
});
|
|
150
|
+
return texture;
|
|
132
151
|
}
|
|
133
152
|
/**
|
|
134
|
-
|
|
135
|
-
|
|
153
|
+
* Creates an offscreen canvas used for CPU mipmap generation.
|
|
154
|
+
*/
|
|
136
155
|
function createMipmapCanvas(width, height) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
canvas.height = height;
|
|
143
|
-
return canvas;
|
|
156
|
+
if (typeof OffscreenCanvas !== "undefined") return new OffscreenCanvas(width, height);
|
|
157
|
+
const canvas = document.createElement("canvas");
|
|
158
|
+
canvas.width = width;
|
|
159
|
+
canvas.height = height;
|
|
160
|
+
return canvas;
|
|
144
161
|
}
|
|
145
162
|
/**
|
|
146
|
-
|
|
147
|
-
|
|
163
|
+
* Creates typed descriptor for `copyExternalImageToTexture`.
|
|
164
|
+
*/
|
|
148
165
|
function createExternalCopySource(source, options) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
return descriptor;
|
|
166
|
+
return {
|
|
167
|
+
source,
|
|
168
|
+
...options.flipY ? { flipY: true } : {},
|
|
169
|
+
...options.premultipliedAlpha ? { premultipliedAlpha: true } : {}
|
|
170
|
+
};
|
|
155
171
|
}
|
|
156
172
|
/**
|
|
157
|
-
|
|
158
|
-
|
|
173
|
+
* Uploads source content to a GPU texture and optionally generates mip chain on CPU.
|
|
174
|
+
*/
|
|
159
175
|
function uploadTexture(device, texture, binding, source, width, height, mipLevelCount) {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
176
|
+
device.queue.copyExternalImageToTexture(createExternalCopySource(source, {
|
|
177
|
+
flipY: binding.flipY,
|
|
178
|
+
premultipliedAlpha: binding.premultipliedAlpha
|
|
179
|
+
}), {
|
|
180
|
+
texture,
|
|
181
|
+
mipLevel: 0
|
|
182
|
+
}, {
|
|
183
|
+
width,
|
|
184
|
+
height,
|
|
185
|
+
depthOrArrayLayers: 1
|
|
186
|
+
});
|
|
187
|
+
if (!binding.generateMipmaps || mipLevelCount <= 1) return;
|
|
188
|
+
let previousSource = source;
|
|
189
|
+
let previousWidth = width;
|
|
190
|
+
let previousHeight = height;
|
|
191
|
+
for (let level = 1; level < mipLevelCount; level += 1) {
|
|
192
|
+
const nextWidth = Math.max(1, Math.floor(previousWidth / 2));
|
|
193
|
+
const nextHeight = Math.max(1, Math.floor(previousHeight / 2));
|
|
194
|
+
const canvas = createMipmapCanvas(nextWidth, nextHeight);
|
|
195
|
+
const context = canvas.getContext("2d");
|
|
196
|
+
if (!context) throw new Error("Unable to create 2D context for mipmap generation");
|
|
197
|
+
context.drawImage(previousSource, 0, 0, previousWidth, previousHeight, 0, 0, nextWidth, nextHeight);
|
|
198
|
+
device.queue.copyExternalImageToTexture(createExternalCopySource(canvas, { premultipliedAlpha: binding.premultipliedAlpha }), {
|
|
199
|
+
texture,
|
|
200
|
+
mipLevel: level
|
|
201
|
+
}, {
|
|
202
|
+
width: nextWidth,
|
|
203
|
+
height: nextHeight,
|
|
204
|
+
depthOrArrayLayers: 1
|
|
205
|
+
});
|
|
206
|
+
previousSource = canvas;
|
|
207
|
+
previousWidth = nextWidth;
|
|
208
|
+
previousHeight = nextHeight;
|
|
209
|
+
}
|
|
186
210
|
}
|
|
187
211
|
/**
|
|
188
|
-
|
|
189
|
-
|
|
212
|
+
* Creates bind group layout entries for frame/uniform buffers plus texture bindings.
|
|
213
|
+
*/
|
|
190
214
|
function createBindGroupLayoutEntries(textureBindings) {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
215
|
+
const entries = [{
|
|
216
|
+
binding: FRAME_BINDING,
|
|
217
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
218
|
+
buffer: {
|
|
219
|
+
type: "uniform",
|
|
220
|
+
minBindingSize: 16
|
|
221
|
+
}
|
|
222
|
+
}, {
|
|
223
|
+
binding: UNIFORM_BINDING,
|
|
224
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
225
|
+
buffer: { type: "uniform" }
|
|
226
|
+
}];
|
|
227
|
+
for (const binding of textureBindings) {
|
|
228
|
+
entries.push({
|
|
229
|
+
binding: binding.samplerBinding,
|
|
230
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
231
|
+
sampler: { type: "filtering" }
|
|
232
|
+
});
|
|
233
|
+
entries.push({
|
|
234
|
+
binding: binding.textureBinding,
|
|
235
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
236
|
+
texture: {
|
|
237
|
+
sampleType: "float",
|
|
238
|
+
viewDimension: "2d",
|
|
239
|
+
multisampled: false
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
return entries;
|
|
220
244
|
}
|
|
221
245
|
/**
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
246
|
+
* Maximum gap (in floats) between two dirty ranges that triggers merge.
|
|
247
|
+
*
|
|
248
|
+
* Set to 4 (16 bytes) which covers one vec4f alignment slot.
|
|
249
|
+
*/
|
|
250
|
+
var DIRTY_RANGE_MERGE_GAP = 4;
|
|
227
251
|
/**
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
else {
|
|
263
|
-
merged.push(curr);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
return merged;
|
|
252
|
+
* Computes dirty float ranges between two uniform snapshots.
|
|
253
|
+
*
|
|
254
|
+
* Adjacent dirty ranges separated by a gap smaller than or equal to
|
|
255
|
+
* {@link DIRTY_RANGE_MERGE_GAP} are merged to reduce `writeBuffer` calls.
|
|
256
|
+
*/
|
|
257
|
+
function findDirtyFloatRanges(previous, next, mergeGapThreshold = DIRTY_RANGE_MERGE_GAP) {
|
|
258
|
+
const ranges = [];
|
|
259
|
+
let start = -1;
|
|
260
|
+
for (let index = 0; index < next.length; index += 1) {
|
|
261
|
+
if (previous[index] !== next[index]) {
|
|
262
|
+
if (start === -1) start = index;
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
if (start !== -1) {
|
|
266
|
+
ranges.push({
|
|
267
|
+
start,
|
|
268
|
+
count: index - start
|
|
269
|
+
});
|
|
270
|
+
start = -1;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (start !== -1) ranges.push({
|
|
274
|
+
start,
|
|
275
|
+
count: next.length - start
|
|
276
|
+
});
|
|
277
|
+
if (ranges.length <= 1) return ranges;
|
|
278
|
+
const merged = [ranges[0]];
|
|
279
|
+
for (let index = 1; index < ranges.length; index += 1) {
|
|
280
|
+
const prev = merged[merged.length - 1];
|
|
281
|
+
const curr = ranges[index];
|
|
282
|
+
if (curr.start - (prev.start + prev.count) <= mergeGapThreshold) prev.count = curr.start + curr.count - prev.start;
|
|
283
|
+
else merged.push(curr);
|
|
284
|
+
}
|
|
285
|
+
return merged;
|
|
267
286
|
}
|
|
268
287
|
/**
|
|
269
|
-
|
|
270
|
-
|
|
288
|
+
* Determines whether shader output should perform linear-to-sRGB conversion.
|
|
289
|
+
*/
|
|
271
290
|
function shouldConvertLinearToSrgb(outputColorSpace, canvasFormat) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
}
|
|
275
|
-
return !canvasFormat.endsWith('-srgb');
|
|
291
|
+
if (outputColorSpace !== "srgb") return false;
|
|
292
|
+
return !canvasFormat.endsWith("-srgb");
|
|
276
293
|
}
|
|
277
294
|
/**
|
|
278
|
-
|
|
279
|
-
|
|
295
|
+
* WGSL shader used to blit an offscreen texture to the canvas.
|
|
296
|
+
*/
|
|
280
297
|
function createFullscreenBlitShader() {
|
|
281
|
-
|
|
298
|
+
return `
|
|
282
299
|
struct MotionGPUVertexOut {
|
|
283
300
|
@builtin(position) position: vec4f,
|
|
284
301
|
@location(0) uv: vec2f,
|
|
@@ -309,851 +326,1090 @@ fn motiongpuBlitFragment(in: MotionGPUVertexOut) -> @location(0) vec4f {
|
|
|
309
326
|
`;
|
|
310
327
|
}
|
|
311
328
|
/**
|
|
312
|
-
|
|
313
|
-
|
|
329
|
+
* Allocates a render target texture with usage flags suitable for passes/blits.
|
|
330
|
+
*/
|
|
314
331
|
function createRenderTexture(device, width, height, format) {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
332
|
+
const texture = device.createTexture({
|
|
333
|
+
size: {
|
|
334
|
+
width,
|
|
335
|
+
height,
|
|
336
|
+
depthOrArrayLayers: 1
|
|
337
|
+
},
|
|
338
|
+
format,
|
|
339
|
+
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC
|
|
340
|
+
});
|
|
341
|
+
return {
|
|
342
|
+
texture,
|
|
343
|
+
view: texture.createView(),
|
|
344
|
+
width,
|
|
345
|
+
height,
|
|
346
|
+
format
|
|
347
|
+
};
|
|
330
348
|
}
|
|
331
349
|
/**
|
|
332
|
-
|
|
333
|
-
|
|
350
|
+
* Destroys a render target texture if present.
|
|
351
|
+
*/
|
|
334
352
|
function destroyRenderTexture(target) {
|
|
335
|
-
|
|
353
|
+
target?.texture.destroy();
|
|
336
354
|
}
|
|
337
355
|
/**
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
356
|
+
* Creates the WebGPU renderer used by `FragCanvas`.
|
|
357
|
+
*
|
|
358
|
+
* @param options - Renderer creation options resolved from material/context state.
|
|
359
|
+
* @returns Renderer instance with `render` and `destroy`.
|
|
360
|
+
* @throws {Error} On WebGPU unavailability, shader compilation issues, or runtime setup failures.
|
|
361
|
+
*/
|
|
362
|
+
async function createRenderer(options) {
|
|
363
|
+
if (!navigator.gpu) throw new Error("WebGPU is not available in this browser");
|
|
364
|
+
const context = options.canvas.getContext("webgpu");
|
|
365
|
+
if (!context) throw new Error("Canvas does not support webgpu context");
|
|
366
|
+
const format = navigator.gpu.getPreferredCanvasFormat();
|
|
367
|
+
const adapter = await navigator.gpu.requestAdapter(options.adapterOptions);
|
|
368
|
+
if (!adapter) throw new Error("Unable to acquire WebGPU adapter");
|
|
369
|
+
const device = await adapter.requestDevice(options.deviceDescriptor);
|
|
370
|
+
let isDestroyed = false;
|
|
371
|
+
let deviceLostMessage = null;
|
|
372
|
+
let uncapturedErrorMessage = null;
|
|
373
|
+
const initializationCleanups = [];
|
|
374
|
+
let acceptInitializationCleanups = true;
|
|
375
|
+
const registerInitializationCleanup = (cleanup) => {
|
|
376
|
+
if (!acceptInitializationCleanups) return;
|
|
377
|
+
options.__onInitializationCleanupRegistered?.();
|
|
378
|
+
initializationCleanups.push(cleanup);
|
|
379
|
+
};
|
|
380
|
+
const runInitializationCleanups = () => {
|
|
381
|
+
for (let index = initializationCleanups.length - 1; index >= 0; index -= 1) try {
|
|
382
|
+
initializationCleanups[index]?.();
|
|
383
|
+
} catch {}
|
|
384
|
+
initializationCleanups.length = 0;
|
|
385
|
+
};
|
|
386
|
+
device.lost.then((info) => {
|
|
387
|
+
if (isDestroyed) return;
|
|
388
|
+
const reason = info.reason ? ` (${info.reason})` : "";
|
|
389
|
+
const details = info.message?.trim();
|
|
390
|
+
deviceLostMessage = details ? `WebGPU device lost: ${details}${reason}` : `WebGPU device lost${reason}`;
|
|
391
|
+
});
|
|
392
|
+
const handleUncapturedError = (event) => {
|
|
393
|
+
if (isDestroyed) return;
|
|
394
|
+
uncapturedErrorMessage = `WebGPU uncaptured error: ${event.error instanceof Error ? event.error.message : String(event.error?.message ?? event.error)}`;
|
|
395
|
+
};
|
|
396
|
+
device.addEventListener("uncapturederror", handleUncapturedError);
|
|
397
|
+
try {
|
|
398
|
+
const runtimeContext = buildShaderCompilationRuntimeContext(options);
|
|
399
|
+
const convertLinearToSrgb = shouldConvertLinearToSrgb(options.outputColorSpace, format);
|
|
400
|
+
const builtShader = buildShaderSourceWithMap(options.fragmentWgsl, options.uniformLayout, options.textureKeys, {
|
|
401
|
+
convertLinearToSrgb,
|
|
402
|
+
fragmentLineMap: options.fragmentLineMap,
|
|
403
|
+
...options.storageBufferKeys !== void 0 ? { storageBufferKeys: options.storageBufferKeys } : {},
|
|
404
|
+
...options.storageBufferDefinitions !== void 0 ? { storageBufferDefinitions: options.storageBufferDefinitions } : {}
|
|
405
|
+
});
|
|
406
|
+
const shaderModule = device.createShaderModule({ code: builtShader.code });
|
|
407
|
+
await assertCompilation(shaderModule, {
|
|
408
|
+
lineMap: builtShader.lineMap,
|
|
409
|
+
fragmentSource: options.fragmentSource,
|
|
410
|
+
includeSources: options.includeSources,
|
|
411
|
+
...options.defineBlockSource !== void 0 ? { defineBlockSource: options.defineBlockSource } : {},
|
|
412
|
+
materialSource: options.materialSource ?? null,
|
|
413
|
+
runtimeContext
|
|
414
|
+
});
|
|
415
|
+
const normalizedTextureDefinitions = normalizeTextureDefinitions(options.textureDefinitions, options.textureKeys);
|
|
416
|
+
const storageBufferKeys = options.storageBufferKeys ?? [];
|
|
417
|
+
const storageBufferDefinitions = options.storageBufferDefinitions ?? {};
|
|
418
|
+
const storageTextureKeys = options.storageTextureKeys ?? [];
|
|
419
|
+
const storageTextureKeySet = new Set(storageTextureKeys);
|
|
420
|
+
const textureBindings = options.textureKeys.map((key, index) => {
|
|
421
|
+
const config = normalizedTextureDefinitions[key];
|
|
422
|
+
if (!config) throw new Error(`Missing texture definition for "${key}"`);
|
|
423
|
+
const { samplerBinding, textureBinding } = getTextureBindings(index);
|
|
424
|
+
const sampler = device.createSampler({
|
|
425
|
+
magFilter: config.filter,
|
|
426
|
+
minFilter: config.filter,
|
|
427
|
+
mipmapFilter: config.generateMipmaps ? config.filter : "nearest",
|
|
428
|
+
addressModeU: config.addressModeU,
|
|
429
|
+
addressModeV: config.addressModeV,
|
|
430
|
+
maxAnisotropy: config.filter === "linear" ? config.anisotropy : 1
|
|
431
|
+
});
|
|
432
|
+
const fallbackTexture = createFallbackTexture(device, config.storage ? "rgba8unorm" : config.format);
|
|
433
|
+
registerInitializationCleanup(() => {
|
|
434
|
+
fallbackTexture.destroy();
|
|
435
|
+
});
|
|
436
|
+
const fallbackView = fallbackTexture.createView();
|
|
437
|
+
const runtimeBinding = {
|
|
438
|
+
key,
|
|
439
|
+
samplerBinding,
|
|
440
|
+
textureBinding,
|
|
441
|
+
sampler,
|
|
442
|
+
fallbackTexture,
|
|
443
|
+
fallbackView,
|
|
444
|
+
texture: null,
|
|
445
|
+
view: fallbackView,
|
|
446
|
+
source: null,
|
|
447
|
+
width: void 0,
|
|
448
|
+
height: void 0,
|
|
449
|
+
mipLevelCount: 1,
|
|
450
|
+
format: config.format,
|
|
451
|
+
colorSpace: config.colorSpace,
|
|
452
|
+
defaultColorSpace: config.colorSpace,
|
|
453
|
+
flipY: config.flipY,
|
|
454
|
+
defaultFlipY: config.flipY,
|
|
455
|
+
generateMipmaps: config.generateMipmaps,
|
|
456
|
+
defaultGenerateMipmaps: config.generateMipmaps,
|
|
457
|
+
premultipliedAlpha: config.premultipliedAlpha,
|
|
458
|
+
defaultPremultipliedAlpha: config.premultipliedAlpha,
|
|
459
|
+
update: config.update ?? "once",
|
|
460
|
+
lastToken: null
|
|
461
|
+
};
|
|
462
|
+
if (config.update !== void 0) runtimeBinding.defaultUpdate = config.update;
|
|
463
|
+
if (config.storage && config.width && config.height) {
|
|
464
|
+
const storageUsage = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.COPY_DST;
|
|
465
|
+
const storageTexture = device.createTexture({
|
|
466
|
+
size: {
|
|
467
|
+
width: config.width,
|
|
468
|
+
height: config.height,
|
|
469
|
+
depthOrArrayLayers: 1
|
|
470
|
+
},
|
|
471
|
+
format: config.format,
|
|
472
|
+
usage: storageUsage
|
|
473
|
+
});
|
|
474
|
+
registerInitializationCleanup(() => {
|
|
475
|
+
storageTexture.destroy();
|
|
476
|
+
});
|
|
477
|
+
runtimeBinding.texture = storageTexture;
|
|
478
|
+
runtimeBinding.view = storageTexture.createView();
|
|
479
|
+
runtimeBinding.width = config.width;
|
|
480
|
+
runtimeBinding.height = config.height;
|
|
481
|
+
}
|
|
482
|
+
return runtimeBinding;
|
|
483
|
+
});
|
|
484
|
+
const bindGroupLayout = device.createBindGroupLayout({ entries: createBindGroupLayoutEntries(textureBindings) });
|
|
485
|
+
const fragmentStorageBindGroupLayout = storageBufferKeys.length > 0 ? device.createBindGroupLayout({ entries: storageBufferKeys.map((_, index) => ({
|
|
486
|
+
binding: index,
|
|
487
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
488
|
+
buffer: { type: "read-only-storage" }
|
|
489
|
+
})) }) : null;
|
|
490
|
+
const pipelineLayout = device.createPipelineLayout({ bindGroupLayouts: fragmentStorageBindGroupLayout ? [bindGroupLayout, fragmentStorageBindGroupLayout] : [bindGroupLayout] });
|
|
491
|
+
const pipeline = device.createRenderPipeline({
|
|
492
|
+
layout: pipelineLayout,
|
|
493
|
+
vertex: {
|
|
494
|
+
module: shaderModule,
|
|
495
|
+
entryPoint: "motiongpuVertex"
|
|
496
|
+
},
|
|
497
|
+
fragment: {
|
|
498
|
+
module: shaderModule,
|
|
499
|
+
entryPoint: "motiongpuFragment",
|
|
500
|
+
targets: [{ format }]
|
|
501
|
+
},
|
|
502
|
+
primitive: { topology: "triangle-list" }
|
|
503
|
+
});
|
|
504
|
+
const blitShaderModule = device.createShaderModule({ code: createFullscreenBlitShader() });
|
|
505
|
+
await assertCompilation(blitShaderModule);
|
|
506
|
+
const blitBindGroupLayout = device.createBindGroupLayout({ entries: [{
|
|
507
|
+
binding: 0,
|
|
508
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
509
|
+
sampler: { type: "filtering" }
|
|
510
|
+
}, {
|
|
511
|
+
binding: 1,
|
|
512
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
513
|
+
texture: {
|
|
514
|
+
sampleType: "float",
|
|
515
|
+
viewDimension: "2d",
|
|
516
|
+
multisampled: false
|
|
517
|
+
}
|
|
518
|
+
}] });
|
|
519
|
+
const blitPipelineLayout = device.createPipelineLayout({ bindGroupLayouts: [blitBindGroupLayout] });
|
|
520
|
+
const blitPipeline = device.createRenderPipeline({
|
|
521
|
+
layout: blitPipelineLayout,
|
|
522
|
+
vertex: {
|
|
523
|
+
module: blitShaderModule,
|
|
524
|
+
entryPoint: "motiongpuBlitVertex"
|
|
525
|
+
},
|
|
526
|
+
fragment: {
|
|
527
|
+
module: blitShaderModule,
|
|
528
|
+
entryPoint: "motiongpuBlitFragment",
|
|
529
|
+
targets: [{ format }]
|
|
530
|
+
},
|
|
531
|
+
primitive: { topology: "triangle-list" }
|
|
532
|
+
});
|
|
533
|
+
const blitSampler = device.createSampler({
|
|
534
|
+
magFilter: "linear",
|
|
535
|
+
minFilter: "linear",
|
|
536
|
+
addressModeU: "clamp-to-edge",
|
|
537
|
+
addressModeV: "clamp-to-edge"
|
|
538
|
+
});
|
|
539
|
+
let blitBindGroupByView = /* @__PURE__ */ new WeakMap();
|
|
540
|
+
const storageBufferMap = /* @__PURE__ */ new Map();
|
|
541
|
+
const pingPongTexturePairs = /* @__PURE__ */ new Map();
|
|
542
|
+
for (const key of storageBufferKeys) {
|
|
543
|
+
const definition = storageBufferDefinitions[key];
|
|
544
|
+
if (!definition) continue;
|
|
545
|
+
const normalized = normalizeStorageBufferDefinition(definition);
|
|
546
|
+
const buffer = device.createBuffer({
|
|
547
|
+
size: normalized.size,
|
|
548
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC
|
|
549
|
+
});
|
|
550
|
+
registerInitializationCleanup(() => {
|
|
551
|
+
buffer.destroy();
|
|
552
|
+
});
|
|
553
|
+
if (definition.initialData) {
|
|
554
|
+
const data = definition.initialData;
|
|
555
|
+
device.queue.writeBuffer(buffer, 0, data.buffer, data.byteOffset, data.byteLength);
|
|
556
|
+
}
|
|
557
|
+
storageBufferMap.set(key, buffer);
|
|
558
|
+
}
|
|
559
|
+
const fragmentStorageBindGroup = fragmentStorageBindGroupLayout && storageBufferKeys.length > 0 ? device.createBindGroup({
|
|
560
|
+
layout: fragmentStorageBindGroupLayout,
|
|
561
|
+
entries: storageBufferKeys.map((key, index) => {
|
|
562
|
+
const buffer = storageBufferMap.get(key);
|
|
563
|
+
if (!buffer) throw new Error(`Storage buffer "${key}" not allocated.`);
|
|
564
|
+
return {
|
|
565
|
+
binding: index,
|
|
566
|
+
resource: { buffer }
|
|
567
|
+
};
|
|
568
|
+
})
|
|
569
|
+
}) : null;
|
|
570
|
+
const ensurePingPongTexturePair = (target) => {
|
|
571
|
+
const existing = pingPongTexturePairs.get(target);
|
|
572
|
+
if (existing) return existing;
|
|
573
|
+
const config = normalizedTextureDefinitions[target];
|
|
574
|
+
if (!config || !config.storage) throw new Error(`PingPongComputePass target "${target}" must reference a texture declared with storage:true.`);
|
|
575
|
+
if (!config.width || !config.height) throw new Error(`PingPongComputePass target "${target}" requires explicit texture width and height.`);
|
|
576
|
+
const usage = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.STORAGE_BINDING | GPUTextureUsage.COPY_DST;
|
|
577
|
+
const textureA = device.createTexture({
|
|
578
|
+
size: {
|
|
579
|
+
width: config.width,
|
|
580
|
+
height: config.height,
|
|
581
|
+
depthOrArrayLayers: 1
|
|
582
|
+
},
|
|
583
|
+
format: config.format,
|
|
584
|
+
usage
|
|
585
|
+
});
|
|
586
|
+
const textureB = device.createTexture({
|
|
587
|
+
size: {
|
|
588
|
+
width: config.width,
|
|
589
|
+
height: config.height,
|
|
590
|
+
depthOrArrayLayers: 1
|
|
591
|
+
},
|
|
592
|
+
format: config.format,
|
|
593
|
+
usage
|
|
594
|
+
});
|
|
595
|
+
registerInitializationCleanup(() => {
|
|
596
|
+
textureA.destroy();
|
|
597
|
+
});
|
|
598
|
+
registerInitializationCleanup(() => {
|
|
599
|
+
textureB.destroy();
|
|
600
|
+
});
|
|
601
|
+
const sampleType = toGpuTextureSampleType(storageTextureSampleScalarType(config.format));
|
|
602
|
+
const bindGroupLayout = device.createBindGroupLayout({ entries: [{
|
|
603
|
+
binding: 0,
|
|
604
|
+
visibility: GPUShaderStage.COMPUTE,
|
|
605
|
+
texture: {
|
|
606
|
+
sampleType,
|
|
607
|
+
viewDimension: "2d",
|
|
608
|
+
multisampled: false
|
|
609
|
+
}
|
|
610
|
+
}, {
|
|
611
|
+
binding: 1,
|
|
612
|
+
visibility: GPUShaderStage.COMPUTE,
|
|
613
|
+
storageTexture: {
|
|
614
|
+
access: "write-only",
|
|
615
|
+
format: config.format,
|
|
616
|
+
viewDimension: "2d"
|
|
617
|
+
}
|
|
618
|
+
}] });
|
|
619
|
+
const pair = {
|
|
620
|
+
target,
|
|
621
|
+
format: config.format,
|
|
622
|
+
width: config.width,
|
|
623
|
+
height: config.height,
|
|
624
|
+
textureA,
|
|
625
|
+
viewA: textureA.createView(),
|
|
626
|
+
textureB,
|
|
627
|
+
viewB: textureB.createView(),
|
|
628
|
+
bindGroupLayout
|
|
629
|
+
};
|
|
630
|
+
pingPongTexturePairs.set(target, pair);
|
|
631
|
+
return pair;
|
|
632
|
+
};
|
|
633
|
+
const computePipelineCache = /* @__PURE__ */ new Map();
|
|
634
|
+
const buildComputePipelineEntry = (buildOptions) => {
|
|
635
|
+
const cacheKey = buildOptions.pingPongTarget && buildOptions.pingPongFormat ? `pingpong:${buildOptions.pingPongTarget}:${buildOptions.pingPongFormat}:${buildOptions.computeSource}` : `compute:${buildOptions.computeSource}`;
|
|
636
|
+
const cached = computePipelineCache.get(cacheKey);
|
|
637
|
+
if (cached) return cached;
|
|
638
|
+
const storageBufferDefs = {};
|
|
639
|
+
for (const key of storageBufferKeys) {
|
|
640
|
+
const def = storageBufferDefinitions[key];
|
|
641
|
+
if (def) {
|
|
642
|
+
const norm = normalizeStorageBufferDefinition(def);
|
|
643
|
+
storageBufferDefs[key] = {
|
|
644
|
+
type: norm.type,
|
|
645
|
+
access: norm.access
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
const storageTextureDefs = {};
|
|
650
|
+
for (const key of storageTextureKeys) {
|
|
651
|
+
const texDef = options.textureDefinitions[key];
|
|
652
|
+
if (texDef?.format) storageTextureDefs[key] = { format: texDef.format };
|
|
653
|
+
}
|
|
654
|
+
const isPingPongPipeline = Boolean(buildOptions.pingPongTarget && buildOptions.pingPongFormat);
|
|
655
|
+
const shaderCode = isPingPongPipeline ? buildPingPongComputeShaderSource({
|
|
656
|
+
compute: buildOptions.computeSource,
|
|
657
|
+
uniformLayout: options.uniformLayout,
|
|
658
|
+
storageBufferKeys,
|
|
659
|
+
storageBufferDefinitions: storageBufferDefs,
|
|
660
|
+
target: buildOptions.pingPongTarget,
|
|
661
|
+
targetFormat: buildOptions.pingPongFormat
|
|
662
|
+
}) : buildComputeShaderSource({
|
|
663
|
+
compute: buildOptions.computeSource,
|
|
664
|
+
uniformLayout: options.uniformLayout,
|
|
665
|
+
storageBufferKeys,
|
|
666
|
+
storageBufferDefinitions: storageBufferDefs,
|
|
667
|
+
storageTextureKeys,
|
|
668
|
+
storageTextureDefinitions: storageTextureDefs
|
|
669
|
+
});
|
|
670
|
+
const computeShaderModule = device.createShaderModule({ code: shaderCode });
|
|
671
|
+
const workgroupSize = extractWorkgroupSize(buildOptions.computeSource);
|
|
672
|
+
const computeUniformBGL = device.createBindGroupLayout({ entries: [{
|
|
673
|
+
binding: FRAME_BINDING,
|
|
674
|
+
visibility: GPUShaderStage.COMPUTE,
|
|
675
|
+
buffer: {
|
|
676
|
+
type: "uniform",
|
|
677
|
+
minBindingSize: 16
|
|
678
|
+
}
|
|
679
|
+
}, {
|
|
680
|
+
binding: UNIFORM_BINDING,
|
|
681
|
+
visibility: GPUShaderStage.COMPUTE,
|
|
682
|
+
buffer: { type: "uniform" }
|
|
683
|
+
}] });
|
|
684
|
+
const storageBGLEntries = storageBufferKeys.map((key, index) => {
|
|
685
|
+
const bufferType = (storageBufferDefinitions[key]?.access ?? "read-write") === "read" ? "read-only-storage" : "storage";
|
|
686
|
+
return {
|
|
687
|
+
binding: index,
|
|
688
|
+
visibility: GPUShaderStage.COMPUTE,
|
|
689
|
+
buffer: { type: bufferType }
|
|
690
|
+
};
|
|
691
|
+
});
|
|
692
|
+
const storageBGL = storageBGLEntries.length > 0 ? device.createBindGroupLayout({ entries: storageBGLEntries }) : null;
|
|
693
|
+
const storageTextureBGLEntries = isPingPongPipeline ? [{
|
|
694
|
+
binding: 0,
|
|
695
|
+
visibility: GPUShaderStage.COMPUTE,
|
|
696
|
+
texture: {
|
|
697
|
+
sampleType: toGpuTextureSampleType(storageTextureSampleScalarType(buildOptions.pingPongFormat)),
|
|
698
|
+
viewDimension: "2d",
|
|
699
|
+
multisampled: false
|
|
700
|
+
}
|
|
701
|
+
}, {
|
|
702
|
+
binding: 1,
|
|
703
|
+
visibility: GPUShaderStage.COMPUTE,
|
|
704
|
+
storageTexture: {
|
|
705
|
+
access: "write-only",
|
|
706
|
+
format: buildOptions.pingPongFormat,
|
|
707
|
+
viewDimension: "2d"
|
|
708
|
+
}
|
|
709
|
+
}] : storageTextureKeys.map((key, index) => {
|
|
710
|
+
const texDef = options.textureDefinitions[key];
|
|
711
|
+
return {
|
|
712
|
+
binding: index,
|
|
713
|
+
visibility: GPUShaderStage.COMPUTE,
|
|
714
|
+
storageTexture: {
|
|
715
|
+
access: "write-only",
|
|
716
|
+
format: texDef?.format ?? "rgba8unorm",
|
|
717
|
+
viewDimension: "2d"
|
|
718
|
+
}
|
|
719
|
+
};
|
|
720
|
+
});
|
|
721
|
+
const storageTextureBGL = storageTextureBGLEntries.length > 0 ? device.createBindGroupLayout({ entries: storageTextureBGLEntries }) : null;
|
|
722
|
+
const bindGroupLayouts = [computeUniformBGL];
|
|
723
|
+
if (storageBGL || storageTextureBGL) bindGroupLayouts.push(storageBGL ?? device.createBindGroupLayout({ entries: [] }));
|
|
724
|
+
if (storageTextureBGL) bindGroupLayouts.push(storageTextureBGL);
|
|
725
|
+
const computePipelineLayout = device.createPipelineLayout({ bindGroupLayouts });
|
|
726
|
+
const entry = {
|
|
727
|
+
pipeline: device.createComputePipeline({
|
|
728
|
+
layout: computePipelineLayout,
|
|
729
|
+
compute: {
|
|
730
|
+
module: computeShaderModule,
|
|
731
|
+
entryPoint: "compute"
|
|
732
|
+
}
|
|
733
|
+
}),
|
|
734
|
+
bindGroup: device.createBindGroup({
|
|
735
|
+
layout: computeUniformBGL,
|
|
736
|
+
entries: [{
|
|
737
|
+
binding: FRAME_BINDING,
|
|
738
|
+
resource: { buffer: frameBuffer }
|
|
739
|
+
}, {
|
|
740
|
+
binding: UNIFORM_BINDING,
|
|
741
|
+
resource: { buffer: uniformBuffer }
|
|
742
|
+
}]
|
|
743
|
+
}),
|
|
744
|
+
workgroupSize,
|
|
745
|
+
computeSource: buildOptions.computeSource
|
|
746
|
+
};
|
|
747
|
+
computePipelineCache.set(cacheKey, entry);
|
|
748
|
+
return entry;
|
|
749
|
+
};
|
|
750
|
+
const getComputeStorageBindGroup = () => {
|
|
751
|
+
if (storageBufferKeys.length === 0) return null;
|
|
752
|
+
const storageBGLEntries = storageBufferKeys.map((key, index) => {
|
|
753
|
+
const bufferType = (storageBufferDefinitions[key]?.access ?? "read-write") === "read" ? "read-only-storage" : "storage";
|
|
754
|
+
return {
|
|
755
|
+
binding: index,
|
|
756
|
+
visibility: GPUShaderStage.COMPUTE,
|
|
757
|
+
buffer: { type: bufferType }
|
|
758
|
+
};
|
|
759
|
+
});
|
|
760
|
+
const storageBGL = device.createBindGroupLayout({ entries: storageBGLEntries });
|
|
761
|
+
const storageEntries = storageBufferKeys.map((key, index) => {
|
|
762
|
+
const buffer = storageBufferMap.get(key);
|
|
763
|
+
if (!buffer) throw new Error(`Storage buffer "${key}" not allocated.`);
|
|
764
|
+
return {
|
|
765
|
+
binding: index,
|
|
766
|
+
resource: { buffer }
|
|
767
|
+
};
|
|
768
|
+
});
|
|
769
|
+
return device.createBindGroup({
|
|
770
|
+
layout: storageBGL,
|
|
771
|
+
entries: storageEntries
|
|
772
|
+
});
|
|
773
|
+
};
|
|
774
|
+
const getComputeStorageTextureBindGroup = () => {
|
|
775
|
+
if (storageTextureKeys.length === 0) return null;
|
|
776
|
+
const entries = storageTextureKeys.map((key, index) => {
|
|
777
|
+
const texDef = options.textureDefinitions[key];
|
|
778
|
+
return {
|
|
779
|
+
binding: index,
|
|
780
|
+
visibility: GPUShaderStage.COMPUTE,
|
|
781
|
+
storageTexture: {
|
|
782
|
+
access: "write-only",
|
|
783
|
+
format: texDef?.format ?? "rgba8unorm",
|
|
784
|
+
viewDimension: "2d"
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
});
|
|
788
|
+
const bgl = device.createBindGroupLayout({ entries });
|
|
789
|
+
const bgEntries = storageTextureKeys.map((key, index) => {
|
|
790
|
+
const binding = textureBindings.find((b) => b.key === key);
|
|
791
|
+
if (!binding || !binding.texture) throw new Error(`Storage texture "${key}" not allocated.`);
|
|
792
|
+
return {
|
|
793
|
+
binding: index,
|
|
794
|
+
resource: binding.view
|
|
795
|
+
};
|
|
796
|
+
});
|
|
797
|
+
return device.createBindGroup({
|
|
798
|
+
layout: bgl,
|
|
799
|
+
entries: bgEntries
|
|
800
|
+
});
|
|
801
|
+
};
|
|
802
|
+
const getPingPongStorageTextureBindGroup = (target, readFromA) => {
|
|
803
|
+
const pair = ensurePingPongTexturePair(target);
|
|
804
|
+
const readView = readFromA ? pair.viewA : pair.viewB;
|
|
805
|
+
const writeView = readFromA ? pair.viewB : pair.viewA;
|
|
806
|
+
return device.createBindGroup({
|
|
807
|
+
layout: pair.bindGroupLayout,
|
|
808
|
+
entries: [{
|
|
809
|
+
binding: 0,
|
|
810
|
+
resource: readView
|
|
811
|
+
}, {
|
|
812
|
+
binding: 1,
|
|
813
|
+
resource: writeView
|
|
814
|
+
}]
|
|
815
|
+
});
|
|
816
|
+
};
|
|
817
|
+
const frameBuffer = device.createBuffer({
|
|
818
|
+
size: 16,
|
|
819
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
820
|
+
});
|
|
821
|
+
registerInitializationCleanup(() => {
|
|
822
|
+
frameBuffer.destroy();
|
|
823
|
+
});
|
|
824
|
+
const uniformBuffer = device.createBuffer({
|
|
825
|
+
size: options.uniformLayout.byteLength,
|
|
826
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
827
|
+
});
|
|
828
|
+
registerInitializationCleanup(() => {
|
|
829
|
+
uniformBuffer.destroy();
|
|
830
|
+
});
|
|
831
|
+
const frameScratch = new Float32Array(4);
|
|
832
|
+
const uniformScratch = new Float32Array(options.uniformLayout.byteLength / 4);
|
|
833
|
+
const uniformPrevious = new Float32Array(options.uniformLayout.byteLength / 4);
|
|
834
|
+
let hasUniformSnapshot = false;
|
|
835
|
+
/**
|
|
836
|
+
* Rebuilds bind group using current texture views.
|
|
837
|
+
*/
|
|
838
|
+
const createBindGroup = () => {
|
|
839
|
+
const entries = [{
|
|
840
|
+
binding: FRAME_BINDING,
|
|
841
|
+
resource: { buffer: frameBuffer }
|
|
842
|
+
}, {
|
|
843
|
+
binding: UNIFORM_BINDING,
|
|
844
|
+
resource: { buffer: uniformBuffer }
|
|
845
|
+
}];
|
|
846
|
+
for (const binding of textureBindings) {
|
|
847
|
+
entries.push({
|
|
848
|
+
binding: binding.samplerBinding,
|
|
849
|
+
resource: binding.sampler
|
|
850
|
+
});
|
|
851
|
+
entries.push({
|
|
852
|
+
binding: binding.textureBinding,
|
|
853
|
+
resource: binding.view
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
return device.createBindGroup({
|
|
857
|
+
layout: bindGroupLayout,
|
|
858
|
+
entries
|
|
859
|
+
});
|
|
860
|
+
};
|
|
861
|
+
/**
|
|
862
|
+
* Synchronizes one runtime texture binding with incoming texture value.
|
|
863
|
+
*
|
|
864
|
+
* @returns `true` when bind group must be rebuilt.
|
|
865
|
+
*/
|
|
866
|
+
const updateTextureBinding = (binding, value, renderMode) => {
|
|
867
|
+
const nextData = toTextureData(value);
|
|
868
|
+
if (!nextData) {
|
|
869
|
+
if (binding.source === null && binding.texture === null) return false;
|
|
870
|
+
binding.texture?.destroy();
|
|
871
|
+
binding.texture = null;
|
|
872
|
+
binding.view = binding.fallbackView;
|
|
873
|
+
binding.source = null;
|
|
874
|
+
binding.width = void 0;
|
|
875
|
+
binding.height = void 0;
|
|
876
|
+
binding.lastToken = null;
|
|
877
|
+
return true;
|
|
878
|
+
}
|
|
879
|
+
const source = nextData.source;
|
|
880
|
+
const colorSpace = nextData.colorSpace ?? binding.defaultColorSpace;
|
|
881
|
+
const format = colorSpace === "linear" ? "rgba8unorm" : "rgba8unorm-srgb";
|
|
882
|
+
const flipY = nextData.flipY ?? binding.defaultFlipY;
|
|
883
|
+
const premultipliedAlpha = nextData.premultipliedAlpha ?? binding.defaultPremultipliedAlpha;
|
|
884
|
+
const generateMipmaps = nextData.generateMipmaps ?? binding.defaultGenerateMipmaps;
|
|
885
|
+
const update = resolveTextureUpdateMode({
|
|
886
|
+
source,
|
|
887
|
+
...nextData.update !== void 0 ? { override: nextData.update } : {},
|
|
888
|
+
...binding.defaultUpdate !== void 0 ? { defaultMode: binding.defaultUpdate } : {}
|
|
889
|
+
});
|
|
890
|
+
const { width, height } = resolveTextureSize(nextData);
|
|
891
|
+
const mipLevelCount = generateMipmaps ? getTextureMipLevelCount(width, height) : 1;
|
|
892
|
+
const sourceChanged = binding.source !== source;
|
|
893
|
+
const tokenChanged = binding.lastToken !== value;
|
|
894
|
+
if (!(binding.texture === null || binding.width !== width || binding.height !== height || binding.mipLevelCount !== mipLevelCount || binding.format !== format)) {
|
|
895
|
+
if ((sourceChanged || update === "perFrame" || update === "onInvalidate" && (renderMode !== "always" || tokenChanged)) && binding.texture) {
|
|
896
|
+
binding.flipY = flipY;
|
|
897
|
+
binding.generateMipmaps = generateMipmaps;
|
|
898
|
+
binding.premultipliedAlpha = premultipliedAlpha;
|
|
899
|
+
binding.colorSpace = colorSpace;
|
|
900
|
+
uploadTexture(device, binding.texture, binding, source, width, height, mipLevelCount);
|
|
901
|
+
}
|
|
902
|
+
binding.source = source;
|
|
903
|
+
binding.width = width;
|
|
904
|
+
binding.height = height;
|
|
905
|
+
binding.mipLevelCount = mipLevelCount;
|
|
906
|
+
binding.update = update;
|
|
907
|
+
binding.lastToken = value;
|
|
908
|
+
return false;
|
|
909
|
+
}
|
|
910
|
+
let textureUsage = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT;
|
|
911
|
+
if (storageTextureKeySet.has(binding.key)) textureUsage |= GPUTextureUsage.STORAGE_BINDING;
|
|
912
|
+
const texture = device.createTexture({
|
|
913
|
+
size: {
|
|
914
|
+
width,
|
|
915
|
+
height,
|
|
916
|
+
depthOrArrayLayers: 1
|
|
917
|
+
},
|
|
918
|
+
format,
|
|
919
|
+
mipLevelCount,
|
|
920
|
+
usage: textureUsage
|
|
921
|
+
});
|
|
922
|
+
registerInitializationCleanup(() => {
|
|
923
|
+
texture.destroy();
|
|
924
|
+
});
|
|
925
|
+
binding.flipY = flipY;
|
|
926
|
+
binding.generateMipmaps = generateMipmaps;
|
|
927
|
+
binding.premultipliedAlpha = premultipliedAlpha;
|
|
928
|
+
binding.colorSpace = colorSpace;
|
|
929
|
+
binding.format = format;
|
|
930
|
+
uploadTexture(device, texture, binding, source, width, height, mipLevelCount);
|
|
931
|
+
binding.texture?.destroy();
|
|
932
|
+
binding.texture = texture;
|
|
933
|
+
binding.view = texture.createView();
|
|
934
|
+
binding.source = source;
|
|
935
|
+
binding.width = width;
|
|
936
|
+
binding.height = height;
|
|
937
|
+
binding.mipLevelCount = mipLevelCount;
|
|
938
|
+
binding.update = update;
|
|
939
|
+
binding.lastToken = value;
|
|
940
|
+
return true;
|
|
941
|
+
};
|
|
942
|
+
for (const binding of textureBindings) {
|
|
943
|
+
if (storageTextureKeySet.has(binding.key)) continue;
|
|
944
|
+
updateTextureBinding(binding, normalizedTextureDefinitions[binding.key]?.source ?? null, "always");
|
|
945
|
+
}
|
|
946
|
+
let bindGroup = createBindGroup();
|
|
947
|
+
let sourceSlotTarget = null;
|
|
948
|
+
let targetSlotTarget = null;
|
|
949
|
+
let renderTargetSignature = "";
|
|
950
|
+
let renderTargetSnapshot = {};
|
|
951
|
+
let renderTargetKeys = [];
|
|
952
|
+
let cachedGraphPlan = null;
|
|
953
|
+
let cachedGraphRenderTargetSignature = "";
|
|
954
|
+
const cachedGraphClearColor = [
|
|
955
|
+
NaN,
|
|
956
|
+
NaN,
|
|
957
|
+
NaN,
|
|
958
|
+
NaN
|
|
959
|
+
];
|
|
960
|
+
const cachedGraphPasses = [];
|
|
961
|
+
let contextConfigured = false;
|
|
962
|
+
let configuredWidth = 0;
|
|
963
|
+
let configuredHeight = 0;
|
|
964
|
+
const runtimeRenderTargets = /* @__PURE__ */ new Map();
|
|
965
|
+
const activePasses = [];
|
|
966
|
+
const lifecyclePreviousSet = /* @__PURE__ */ new Set();
|
|
967
|
+
const lifecycleNextSet = /* @__PURE__ */ new Set();
|
|
968
|
+
const lifecycleUniquePasses = [];
|
|
969
|
+
let lifecyclePassesRef = null;
|
|
970
|
+
let passWidth = 0;
|
|
971
|
+
let passHeight = 0;
|
|
972
|
+
/**
|
|
973
|
+
* Resolves active render pass list for current frame.
|
|
974
|
+
*/
|
|
975
|
+
const resolvePasses = () => {
|
|
976
|
+
return options.getPasses?.() ?? options.passes ?? [];
|
|
977
|
+
};
|
|
978
|
+
/**
|
|
979
|
+
* Resolves active render target declarations for current frame.
|
|
980
|
+
*/
|
|
981
|
+
const resolveRenderTargets = () => {
|
|
982
|
+
return options.getRenderTargets?.() ?? options.renderTargets;
|
|
983
|
+
};
|
|
984
|
+
/**
|
|
985
|
+
* Checks whether cached render-graph plan can be reused for this frame.
|
|
986
|
+
*/
|
|
987
|
+
const isGraphPlanCacheValid = (passes, clearColor) => {
|
|
988
|
+
if (!cachedGraphPlan) return false;
|
|
989
|
+
if (cachedGraphRenderTargetSignature !== renderTargetSignature) return false;
|
|
990
|
+
if (cachedGraphClearColor[0] !== clearColor[0] || cachedGraphClearColor[1] !== clearColor[1] || cachedGraphClearColor[2] !== clearColor[2] || cachedGraphClearColor[3] !== clearColor[3]) return false;
|
|
991
|
+
if (cachedGraphPasses.length !== passes.length) return false;
|
|
992
|
+
for (let index = 0; index < passes.length; index += 1) {
|
|
993
|
+
const pass = passes[index];
|
|
994
|
+
const rp = pass;
|
|
995
|
+
const snapshot = cachedGraphPasses[index];
|
|
996
|
+
if (!pass || !snapshot || snapshot.pass !== pass) return false;
|
|
997
|
+
if (snapshot.enabled !== pass.enabled || snapshot.needsSwap !== rp.needsSwap || snapshot.input !== rp.input || snapshot.output !== rp.output || snapshot.clear !== rp.clear || snapshot.preserve !== rp.preserve) return false;
|
|
998
|
+
const passClearColor = rp.clearColor;
|
|
999
|
+
const hasPassClearColor = passClearColor !== void 0;
|
|
1000
|
+
if (snapshot.hasClearColor !== hasPassClearColor) return false;
|
|
1001
|
+
if (passClearColor) {
|
|
1002
|
+
if (snapshot.clearColor0 !== passClearColor[0] || snapshot.clearColor1 !== passClearColor[1] || snapshot.clearColor2 !== passClearColor[2] || snapshot.clearColor3 !== passClearColor[3]) return false;
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
return true;
|
|
1006
|
+
};
|
|
1007
|
+
/**
|
|
1008
|
+
* Updates render-graph cache with current pass set.
|
|
1009
|
+
*/
|
|
1010
|
+
const updateGraphPlanCache = (passes, clearColor, graphPlan) => {
|
|
1011
|
+
cachedGraphPlan = graphPlan;
|
|
1012
|
+
cachedGraphRenderTargetSignature = renderTargetSignature;
|
|
1013
|
+
cachedGraphClearColor[0] = clearColor[0];
|
|
1014
|
+
cachedGraphClearColor[1] = clearColor[1];
|
|
1015
|
+
cachedGraphClearColor[2] = clearColor[2];
|
|
1016
|
+
cachedGraphClearColor[3] = clearColor[3];
|
|
1017
|
+
cachedGraphPasses.length = passes.length;
|
|
1018
|
+
let index = 0;
|
|
1019
|
+
for (const pass of passes) {
|
|
1020
|
+
const rp = pass;
|
|
1021
|
+
const passClearColor = rp.clearColor;
|
|
1022
|
+
const hasPassClearColor = passClearColor !== void 0;
|
|
1023
|
+
const snapshot = cachedGraphPasses[index];
|
|
1024
|
+
if (!snapshot) {
|
|
1025
|
+
cachedGraphPasses[index] = {
|
|
1026
|
+
pass,
|
|
1027
|
+
enabled: pass.enabled,
|
|
1028
|
+
needsSwap: rp.needsSwap,
|
|
1029
|
+
input: rp.input,
|
|
1030
|
+
output: rp.output,
|
|
1031
|
+
clear: rp.clear,
|
|
1032
|
+
preserve: rp.preserve,
|
|
1033
|
+
hasClearColor: hasPassClearColor,
|
|
1034
|
+
clearColor0: passClearColor?.[0] ?? 0,
|
|
1035
|
+
clearColor1: passClearColor?.[1] ?? 0,
|
|
1036
|
+
clearColor2: passClearColor?.[2] ?? 0,
|
|
1037
|
+
clearColor3: passClearColor?.[3] ?? 0
|
|
1038
|
+
};
|
|
1039
|
+
index += 1;
|
|
1040
|
+
continue;
|
|
1041
|
+
}
|
|
1042
|
+
snapshot.pass = pass;
|
|
1043
|
+
snapshot.enabled = pass.enabled;
|
|
1044
|
+
snapshot.needsSwap = rp.needsSwap;
|
|
1045
|
+
snapshot.input = rp.input;
|
|
1046
|
+
snapshot.output = rp.output;
|
|
1047
|
+
snapshot.clear = rp.clear;
|
|
1048
|
+
snapshot.preserve = rp.preserve;
|
|
1049
|
+
snapshot.hasClearColor = hasPassClearColor;
|
|
1050
|
+
snapshot.clearColor0 = passClearColor?.[0] ?? 0;
|
|
1051
|
+
snapshot.clearColor1 = passClearColor?.[1] ?? 0;
|
|
1052
|
+
snapshot.clearColor2 = passClearColor?.[2] ?? 0;
|
|
1053
|
+
snapshot.clearColor3 = passClearColor?.[3] ?? 0;
|
|
1054
|
+
index += 1;
|
|
1055
|
+
}
|
|
1056
|
+
};
|
|
1057
|
+
/**
|
|
1058
|
+
* Synchronizes pass lifecycle callbacks and resize notifications.
|
|
1059
|
+
*/
|
|
1060
|
+
const syncPassLifecycle = (passes, width, height) => {
|
|
1061
|
+
const resized = passWidth !== width || passHeight !== height;
|
|
1062
|
+
if (!resized && lifecyclePassesRef === passes && passes.length === activePasses.length) {
|
|
1063
|
+
let isSameOrder = true;
|
|
1064
|
+
for (let index = 0; index < passes.length; index += 1) if (activePasses[index] !== passes[index]) {
|
|
1065
|
+
isSameOrder = false;
|
|
1066
|
+
break;
|
|
1067
|
+
}
|
|
1068
|
+
if (isSameOrder) return;
|
|
1069
|
+
}
|
|
1070
|
+
lifecycleNextSet.clear();
|
|
1071
|
+
lifecycleUniquePasses.length = 0;
|
|
1072
|
+
for (const pass of passes) {
|
|
1073
|
+
if (lifecycleNextSet.has(pass)) continue;
|
|
1074
|
+
lifecycleNextSet.add(pass);
|
|
1075
|
+
lifecycleUniquePasses.push(pass);
|
|
1076
|
+
}
|
|
1077
|
+
lifecyclePreviousSet.clear();
|
|
1078
|
+
for (const pass of activePasses) lifecyclePreviousSet.add(pass);
|
|
1079
|
+
for (const pass of activePasses) if (!lifecycleNextSet.has(pass)) pass.dispose?.();
|
|
1080
|
+
for (const pass of lifecycleUniquePasses) if (resized || !lifecyclePreviousSet.has(pass)) pass.setSize?.(width, height);
|
|
1081
|
+
activePasses.length = 0;
|
|
1082
|
+
for (const pass of lifecycleUniquePasses) activePasses.push(pass);
|
|
1083
|
+
lifecyclePassesRef = passes;
|
|
1084
|
+
passWidth = width;
|
|
1085
|
+
passHeight = height;
|
|
1086
|
+
};
|
|
1087
|
+
/**
|
|
1088
|
+
* Ensures internal ping-pong slot texture matches current canvas size/format.
|
|
1089
|
+
*/
|
|
1090
|
+
const ensureSlotTarget = (slot, width, height) => {
|
|
1091
|
+
const current = slot === "source" ? sourceSlotTarget : targetSlotTarget;
|
|
1092
|
+
if (current && current.width === width && current.height === height && current.format === format) return current;
|
|
1093
|
+
destroyRenderTexture(current);
|
|
1094
|
+
const next = createRenderTexture(device, width, height, format);
|
|
1095
|
+
if (slot === "source") sourceSlotTarget = next;
|
|
1096
|
+
else targetSlotTarget = next;
|
|
1097
|
+
return next;
|
|
1098
|
+
};
|
|
1099
|
+
/**
|
|
1100
|
+
* Creates/updates runtime render targets and returns immutable pass snapshot.
|
|
1101
|
+
*/
|
|
1102
|
+
const syncRenderTargets = (canvasWidth, canvasHeight) => {
|
|
1103
|
+
const resolvedDefinitions = resolveRenderTargetDefinitions(resolveRenderTargets(), canvasWidth, canvasHeight, format);
|
|
1104
|
+
const nextSignature = buildRenderTargetSignature(resolvedDefinitions);
|
|
1105
|
+
if (nextSignature !== renderTargetSignature) {
|
|
1106
|
+
const activeKeys = new Set(resolvedDefinitions.map((definition) => definition.key));
|
|
1107
|
+
for (const [key, target] of runtimeRenderTargets.entries()) if (!activeKeys.has(key)) {
|
|
1108
|
+
target.texture.destroy();
|
|
1109
|
+
runtimeRenderTargets.delete(key);
|
|
1110
|
+
}
|
|
1111
|
+
for (const definition of resolvedDefinitions) {
|
|
1112
|
+
const current = runtimeRenderTargets.get(definition.key);
|
|
1113
|
+
if (current && current.width === definition.width && current.height === definition.height && current.format === definition.format) continue;
|
|
1114
|
+
current?.texture.destroy();
|
|
1115
|
+
runtimeRenderTargets.set(definition.key, createRenderTexture(device, definition.width, definition.height, definition.format));
|
|
1116
|
+
}
|
|
1117
|
+
renderTargetSignature = nextSignature;
|
|
1118
|
+
const nextSnapshot = {};
|
|
1119
|
+
const nextKeys = [];
|
|
1120
|
+
for (const definition of resolvedDefinitions) {
|
|
1121
|
+
const target = runtimeRenderTargets.get(definition.key);
|
|
1122
|
+
if (!target) continue;
|
|
1123
|
+
nextKeys.push(definition.key);
|
|
1124
|
+
nextSnapshot[definition.key] = {
|
|
1125
|
+
texture: target.texture,
|
|
1126
|
+
view: target.view,
|
|
1127
|
+
width: target.width,
|
|
1128
|
+
height: target.height,
|
|
1129
|
+
format: target.format
|
|
1130
|
+
};
|
|
1131
|
+
}
|
|
1132
|
+
renderTargetSnapshot = nextSnapshot;
|
|
1133
|
+
renderTargetKeys = nextKeys;
|
|
1134
|
+
}
|
|
1135
|
+
return renderTargetSnapshot;
|
|
1136
|
+
};
|
|
1137
|
+
/**
|
|
1138
|
+
* Blits a texture view to the current canvas texture.
|
|
1139
|
+
*/
|
|
1140
|
+
const blitToCanvas = (commandEncoder, sourceView, canvasView, clearColor) => {
|
|
1141
|
+
let bindGroup = blitBindGroupByView.get(sourceView);
|
|
1142
|
+
if (!bindGroup) {
|
|
1143
|
+
bindGroup = device.createBindGroup({
|
|
1144
|
+
layout: blitBindGroupLayout,
|
|
1145
|
+
entries: [{
|
|
1146
|
+
binding: 0,
|
|
1147
|
+
resource: blitSampler
|
|
1148
|
+
}, {
|
|
1149
|
+
binding: 1,
|
|
1150
|
+
resource: sourceView
|
|
1151
|
+
}]
|
|
1152
|
+
});
|
|
1153
|
+
blitBindGroupByView.set(sourceView, bindGroup);
|
|
1154
|
+
}
|
|
1155
|
+
const pass = commandEncoder.beginRenderPass({ colorAttachments: [{
|
|
1156
|
+
view: canvasView,
|
|
1157
|
+
clearValue: {
|
|
1158
|
+
r: clearColor[0],
|
|
1159
|
+
g: clearColor[1],
|
|
1160
|
+
b: clearColor[2],
|
|
1161
|
+
a: clearColor[3]
|
|
1162
|
+
},
|
|
1163
|
+
loadOp: "clear",
|
|
1164
|
+
storeOp: "store"
|
|
1165
|
+
}] });
|
|
1166
|
+
pass.setPipeline(blitPipeline);
|
|
1167
|
+
pass.setBindGroup(0, bindGroup);
|
|
1168
|
+
pass.draw(3);
|
|
1169
|
+
pass.end();
|
|
1170
|
+
};
|
|
1171
|
+
/**
|
|
1172
|
+
* Executes a full frame render.
|
|
1173
|
+
*/
|
|
1174
|
+
const render = ({ time, delta, renderMode, uniforms, textures, canvasSize, pendingStorageWrites }) => {
|
|
1175
|
+
if (deviceLostMessage) throw new Error(deviceLostMessage);
|
|
1176
|
+
if (uncapturedErrorMessage) {
|
|
1177
|
+
const message = uncapturedErrorMessage;
|
|
1178
|
+
uncapturedErrorMessage = null;
|
|
1179
|
+
throw new Error(message);
|
|
1180
|
+
}
|
|
1181
|
+
const { width, height } = resizeCanvas(options.canvas, options.getDpr(), canvasSize);
|
|
1182
|
+
if (!contextConfigured || configuredWidth !== width || configuredHeight !== height) {
|
|
1183
|
+
context.configure({
|
|
1184
|
+
device,
|
|
1185
|
+
format,
|
|
1186
|
+
alphaMode: "premultiplied"
|
|
1187
|
+
});
|
|
1188
|
+
contextConfigured = true;
|
|
1189
|
+
configuredWidth = width;
|
|
1190
|
+
configuredHeight = height;
|
|
1191
|
+
}
|
|
1192
|
+
frameScratch[0] = time;
|
|
1193
|
+
frameScratch[1] = delta;
|
|
1194
|
+
frameScratch[2] = width;
|
|
1195
|
+
frameScratch[3] = height;
|
|
1196
|
+
device.queue.writeBuffer(frameBuffer, 0, frameScratch.buffer, frameScratch.byteOffset, frameScratch.byteLength);
|
|
1197
|
+
packUniformsInto(uniforms, options.uniformLayout, uniformScratch);
|
|
1198
|
+
if (!hasUniformSnapshot) {
|
|
1199
|
+
device.queue.writeBuffer(uniformBuffer, 0, uniformScratch.buffer, uniformScratch.byteOffset, uniformScratch.byteLength);
|
|
1200
|
+
uniformPrevious.set(uniformScratch);
|
|
1201
|
+
hasUniformSnapshot = true;
|
|
1202
|
+
} else {
|
|
1203
|
+
const dirtyRanges = findDirtyFloatRanges(uniformPrevious, uniformScratch);
|
|
1204
|
+
for (const range of dirtyRanges) {
|
|
1205
|
+
const byteOffset = range.start * 4;
|
|
1206
|
+
const byteLength = range.count * 4;
|
|
1207
|
+
device.queue.writeBuffer(uniformBuffer, byteOffset, uniformScratch.buffer, uniformScratch.byteOffset + byteOffset, byteLength);
|
|
1208
|
+
}
|
|
1209
|
+
if (dirtyRanges.length > 0) uniformPrevious.set(uniformScratch);
|
|
1210
|
+
}
|
|
1211
|
+
let bindGroupDirty = false;
|
|
1212
|
+
for (const binding of textureBindings) {
|
|
1213
|
+
if (storageTextureKeySet.has(binding.key)) continue;
|
|
1214
|
+
if (updateTextureBinding(binding, textures[binding.key] ?? normalizedTextureDefinitions[binding.key]?.source ?? null, renderMode)) bindGroupDirty = true;
|
|
1215
|
+
}
|
|
1216
|
+
if (bindGroupDirty) bindGroup = createBindGroup();
|
|
1217
|
+
if (pendingStorageWrites) for (const write of pendingStorageWrites) {
|
|
1218
|
+
const buffer = storageBufferMap.get(write.name);
|
|
1219
|
+
if (buffer) {
|
|
1220
|
+
const data = write.data;
|
|
1221
|
+
device.queue.writeBuffer(buffer, write.offset, data.buffer, data.byteOffset, data.byteLength);
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
const commandEncoder = device.createCommandEncoder();
|
|
1225
|
+
const passes = resolvePasses();
|
|
1226
|
+
const clearColor = options.getClearColor();
|
|
1227
|
+
syncPassLifecycle(passes, width, height);
|
|
1228
|
+
const runtimeTargets = syncRenderTargets(width, height);
|
|
1229
|
+
const graphPlan = isGraphPlanCacheValid(passes, clearColor) ? cachedGraphPlan : (() => {
|
|
1230
|
+
const nextPlan = planRenderGraph(passes, clearColor, renderTargetKeys);
|
|
1231
|
+
updateGraphPlanCache(passes, clearColor, nextPlan);
|
|
1232
|
+
return nextPlan;
|
|
1233
|
+
})();
|
|
1234
|
+
const canvasTexture = context.getCurrentTexture();
|
|
1235
|
+
const canvasSurface = {
|
|
1236
|
+
texture: canvasTexture,
|
|
1237
|
+
view: canvasTexture.createView(),
|
|
1238
|
+
width,
|
|
1239
|
+
height,
|
|
1240
|
+
format
|
|
1241
|
+
};
|
|
1242
|
+
const slots = graphPlan.steps.length > 0 ? {
|
|
1243
|
+
source: ensureSlotTarget("source", width, height),
|
|
1244
|
+
target: ensureSlotTarget("target", width, height),
|
|
1245
|
+
canvas: canvasSurface
|
|
1246
|
+
} : null;
|
|
1247
|
+
const sceneOutput = slots ? slots.source : canvasSurface;
|
|
1248
|
+
if (slots) for (const step of graphPlan.steps) {
|
|
1249
|
+
if (step.kind !== "compute") continue;
|
|
1250
|
+
const computePass = step.pass;
|
|
1251
|
+
if (computePass.getCompute && computePass.resolveDispatch && computePass.getWorkgroupSize) {
|
|
1252
|
+
const computeSource = computePass.getCompute();
|
|
1253
|
+
const pingPongTarget = computePass.isPingPong && computePass.getTarget ? computePass.getTarget() : void 0;
|
|
1254
|
+
if (computePass.isPingPong && !pingPongTarget) throw new Error("PingPongComputePass must provide a target texture key.");
|
|
1255
|
+
const pingPongPair = pingPongTarget ? ensurePingPongTexturePair(pingPongTarget) : null;
|
|
1256
|
+
const pipelineEntry = buildComputePipelineEntry({
|
|
1257
|
+
computeSource,
|
|
1258
|
+
...pingPongPair ? {
|
|
1259
|
+
pingPongTarget: pingPongPair.target,
|
|
1260
|
+
pingPongFormat: pingPongPair.format
|
|
1261
|
+
} : {}
|
|
1262
|
+
});
|
|
1263
|
+
const workgroupSize = computePass.getWorkgroupSize();
|
|
1264
|
+
const storageBindGroup = getComputeStorageBindGroup();
|
|
1265
|
+
const storageTextureBindGroup = getComputeStorageTextureBindGroup();
|
|
1266
|
+
const iterations = computePass.isPingPong && computePass.getIterations ? computePass.getIterations() : 1;
|
|
1267
|
+
const currentOutput = computePass.isPingPong && computePass.getCurrentOutput ? computePass.getCurrentOutput() : null;
|
|
1268
|
+
const readFromAAtIterationZero = pingPongPair && currentOutput ? currentOutput !== `${pingPongPair.target}B` : true;
|
|
1269
|
+
for (let iter = 0; iter < iterations; iter += 1) {
|
|
1270
|
+
const dispatch = computePass.resolveDispatch({
|
|
1271
|
+
width,
|
|
1272
|
+
height,
|
|
1273
|
+
time,
|
|
1274
|
+
delta,
|
|
1275
|
+
workgroupSize
|
|
1276
|
+
});
|
|
1277
|
+
const cPass = commandEncoder.beginComputePass();
|
|
1278
|
+
cPass.setPipeline(pipelineEntry.pipeline);
|
|
1279
|
+
cPass.setBindGroup(0, pipelineEntry.bindGroup);
|
|
1280
|
+
if (storageBindGroup) cPass.setBindGroup(1, storageBindGroup);
|
|
1281
|
+
if (pingPongPair) {
|
|
1282
|
+
const readFromA = iter % 2 === 0 ? readFromAAtIterationZero : !readFromAAtIterationZero;
|
|
1283
|
+
cPass.setBindGroup(2, getPingPongStorageTextureBindGroup(pingPongPair.target, readFromA));
|
|
1284
|
+
} else if (storageTextureBindGroup) cPass.setBindGroup(2, storageTextureBindGroup);
|
|
1285
|
+
cPass.dispatchWorkgroups(dispatch[0], dispatch[1], dispatch[2]);
|
|
1286
|
+
cPass.end();
|
|
1287
|
+
}
|
|
1288
|
+
if (computePass.isPingPong && computePass.advanceFrame) computePass.advanceFrame();
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
const scenePass = commandEncoder.beginRenderPass({ colorAttachments: [{
|
|
1292
|
+
view: sceneOutput.view,
|
|
1293
|
+
clearValue: {
|
|
1294
|
+
r: clearColor[0],
|
|
1295
|
+
g: clearColor[1],
|
|
1296
|
+
b: clearColor[2],
|
|
1297
|
+
a: clearColor[3]
|
|
1298
|
+
},
|
|
1299
|
+
loadOp: "clear",
|
|
1300
|
+
storeOp: "store"
|
|
1301
|
+
}] });
|
|
1302
|
+
scenePass.setPipeline(pipeline);
|
|
1303
|
+
scenePass.setBindGroup(0, bindGroup);
|
|
1304
|
+
if (fragmentStorageBindGroup) scenePass.setBindGroup(1, fragmentStorageBindGroup);
|
|
1305
|
+
scenePass.draw(3);
|
|
1306
|
+
scenePass.end();
|
|
1307
|
+
if (slots) {
|
|
1308
|
+
const resolveStepSurface = (slot) => {
|
|
1309
|
+
if (slot === "source") return slots.source;
|
|
1310
|
+
if (slot === "target") return slots.target;
|
|
1311
|
+
if (slot === "canvas") return slots.canvas;
|
|
1312
|
+
const named = runtimeTargets[slot];
|
|
1313
|
+
if (!named) throw new Error(`Render graph references unknown runtime target "${slot}".`);
|
|
1314
|
+
return named;
|
|
1315
|
+
};
|
|
1316
|
+
for (const step of graphPlan.steps) {
|
|
1317
|
+
if (step.kind === "compute") continue;
|
|
1318
|
+
const input = resolveStepSurface(step.input);
|
|
1319
|
+
const output = resolveStepSurface(step.output);
|
|
1320
|
+
step.pass.render({
|
|
1321
|
+
device,
|
|
1322
|
+
commandEncoder,
|
|
1323
|
+
source: slots.source,
|
|
1324
|
+
target: slots.target,
|
|
1325
|
+
canvas: slots.canvas,
|
|
1326
|
+
input,
|
|
1327
|
+
output,
|
|
1328
|
+
targets: runtimeTargets,
|
|
1329
|
+
time,
|
|
1330
|
+
delta,
|
|
1331
|
+
width,
|
|
1332
|
+
height,
|
|
1333
|
+
clear: step.clear,
|
|
1334
|
+
clearColor: step.clearColor,
|
|
1335
|
+
preserve: step.preserve,
|
|
1336
|
+
beginRenderPass: (passOptions) => {
|
|
1337
|
+
const clear = passOptions?.clear ?? step.clear;
|
|
1338
|
+
const clearColor = passOptions?.clearColor ?? step.clearColor;
|
|
1339
|
+
const preserve = passOptions?.preserve ?? step.preserve;
|
|
1340
|
+
return commandEncoder.beginRenderPass({ colorAttachments: [{
|
|
1341
|
+
view: passOptions?.view ?? output.view,
|
|
1342
|
+
clearValue: {
|
|
1343
|
+
r: clearColor[0],
|
|
1344
|
+
g: clearColor[1],
|
|
1345
|
+
b: clearColor[2],
|
|
1346
|
+
a: clearColor[3]
|
|
1347
|
+
},
|
|
1348
|
+
loadOp: clear ? "clear" : "load",
|
|
1349
|
+
storeOp: preserve ? "store" : "discard"
|
|
1350
|
+
}] });
|
|
1351
|
+
}
|
|
1352
|
+
});
|
|
1353
|
+
if (step.needsSwap) {
|
|
1354
|
+
const previousSource = slots.source;
|
|
1355
|
+
slots.source = slots.target;
|
|
1356
|
+
slots.target = previousSource;
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
if (graphPlan.finalOutput !== "canvas") blitToCanvas(commandEncoder, resolveStepSurface(graphPlan.finalOutput).view, slots.canvas.view, clearColor);
|
|
1360
|
+
}
|
|
1361
|
+
device.queue.submit([commandEncoder.finish()]);
|
|
1362
|
+
};
|
|
1363
|
+
acceptInitializationCleanups = false;
|
|
1364
|
+
initializationCleanups.length = 0;
|
|
1365
|
+
return {
|
|
1366
|
+
render,
|
|
1367
|
+
getStorageBuffer: (name) => {
|
|
1368
|
+
return storageBufferMap.get(name);
|
|
1369
|
+
},
|
|
1370
|
+
getDevice: () => {
|
|
1371
|
+
return device;
|
|
1372
|
+
},
|
|
1373
|
+
destroy: () => {
|
|
1374
|
+
isDestroyed = true;
|
|
1375
|
+
device.removeEventListener("uncapturederror", handleUncapturedError);
|
|
1376
|
+
frameBuffer.destroy();
|
|
1377
|
+
uniformBuffer.destroy();
|
|
1378
|
+
for (const buffer of storageBufferMap.values()) buffer.destroy();
|
|
1379
|
+
storageBufferMap.clear();
|
|
1380
|
+
for (const pair of pingPongTexturePairs.values()) {
|
|
1381
|
+
pair.textureA.destroy();
|
|
1382
|
+
pair.textureB.destroy();
|
|
1383
|
+
}
|
|
1384
|
+
pingPongTexturePairs.clear();
|
|
1385
|
+
computePipelineCache.clear();
|
|
1386
|
+
destroyRenderTexture(sourceSlotTarget);
|
|
1387
|
+
destroyRenderTexture(targetSlotTarget);
|
|
1388
|
+
for (const target of runtimeRenderTargets.values()) target.texture.destroy();
|
|
1389
|
+
runtimeRenderTargets.clear();
|
|
1390
|
+
for (const pass of activePasses) pass.dispose?.();
|
|
1391
|
+
activePasses.length = 0;
|
|
1392
|
+
lifecyclePassesRef = null;
|
|
1393
|
+
for (const binding of textureBindings) {
|
|
1394
|
+
binding.texture?.destroy();
|
|
1395
|
+
binding.fallbackTexture.destroy();
|
|
1396
|
+
}
|
|
1397
|
+
blitBindGroupByView = /* @__PURE__ */ new WeakMap();
|
|
1398
|
+
cachedGraphPlan = null;
|
|
1399
|
+
cachedGraphPasses.length = 0;
|
|
1400
|
+
renderTargetSnapshot = {};
|
|
1401
|
+
renderTargetKeys = [];
|
|
1402
|
+
}
|
|
1403
|
+
};
|
|
1404
|
+
} catch (error) {
|
|
1405
|
+
isDestroyed = true;
|
|
1406
|
+
acceptInitializationCleanups = false;
|
|
1407
|
+
device.removeEventListener("uncapturederror", handleUncapturedError);
|
|
1408
|
+
runInitializationCleanups();
|
|
1409
|
+
throw error;
|
|
1410
|
+
}
|
|
1159
1411
|
}
|
|
1412
|
+
//#endregion
|
|
1413
|
+
export { createRenderer, findDirtyFloatRanges };
|
|
1414
|
+
|
|
1415
|
+
//# sourceMappingURL=renderer.js.map
|