@motion-core/motion-gpu 0.4.0 → 0.4.2
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/dist/advanced.d.ts +1 -0
- package/dist/advanced.d.ts.map +1 -0
- package/dist/advanced.js +12 -6
- package/dist/core/advanced.d.ts +1 -0
- package/dist/core/advanced.d.ts.map +1 -0
- package/dist/core/advanced.js +12 -5
- 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 +1 -0
- package/dist/core/error-report.d.ts.map +1 -0
- package/dist/core/error-report.js +184 -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 +1 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +11 -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 +128 -151
- package/dist/core/material-preprocess.js.map +1 -0
- package/dist/core/material.d.ts +1 -0
- package/dist/core/material.d.ts.map +1 -0
- package/dist/core/material.js +263 -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 +1 -0
- package/dist/core/render-graph.d.ts.map +1 -0
- package/dist/core/render-graph.js +61 -68
- package/dist/core/render-graph.js.map +1 -0
- package/dist/core/render-targets.d.ts +2 -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 +942 -1081
- package/dist/core/renderer.js.map +1 -0
- package/dist/core/runtime-loop.d.ts +2 -0
- package/dist/core/runtime-loop.d.ts.map +1 -0
- package/dist/core/runtime-loop.js +305 -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 +1 -0
- package/dist/core/shader.d.ts.map +1 -0
- package/dist/core/shader.js +92 -117
- package/dist/core/shader.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 +205 -273
- package/dist/core/texture-loader.js.map +1 -0
- package/dist/core/textures.d.ts +2 -0
- package/dist/core/textures.d.ts.map +1 -0
- package/dist/core/textures.js +106 -116
- package/dist/core/textures.js.map +1 -0
- package/dist/core/types.d.ts +2 -0
- 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 +11 -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/CopyPass.d.ts +2 -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 +2 -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/ShaderPass.d.ts +1 -0
- package/dist/passes/ShaderPass.d.ts.map +1 -0
- package/dist/passes/ShaderPass.js +40 -37
- package/dist/passes/ShaderPass.js.map +1 -0
- package/dist/passes/index.d.ts +1 -0
- package/dist/passes/index.d.ts.map +1 -0
- package/dist/passes/index.js +4 -3
- package/dist/react/FragCanvas.d.ts +2 -0
- 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 +384 -48
- 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 +12 -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 +1 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +10 -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.d.ts +2 -0
- package/dist/svelte/FragCanvas.svelte.d.ts.map +1 -0
- package/dist/svelte/MotionGPUErrorOverlay.svelte +17 -20
- 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 +11 -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 +1 -0
- package/dist/svelte/index.d.ts.map +1 -0
- package/dist/svelte/index.js +10 -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 +15 -7
- package/src/lib/advanced.ts +6 -0
- package/src/lib/core/advanced.ts +12 -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 +406 -0
- package/src/lib/core/frame-registry.ts +1189 -0
- package/src/lib/core/index.ts +77 -0
- package/src/lib/core/material-preprocess.ts +284 -0
- package/src/lib/core/material.ts +667 -0
- package/src/lib/core/recompile-policy.ts +31 -0
- package/src/lib/core/render-graph.ts +143 -0
- package/src/lib/core/render-targets.ts +107 -0
- package/src/lib/core/renderer.ts +1547 -0
- package/src/lib/core/runtime-loop.ts +458 -0
- package/src/lib/core/scheduler-helpers.ts +136 -0
- package/src/lib/core/shader.ts +258 -0
- package/src/lib/core/texture-loader.ts +476 -0
- package/src/lib/core/textures.ts +235 -0
- package/src/lib/core/types.ts +582 -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/CopyPass.ts +80 -0
- package/src/lib/passes/FullscreenPass.ts +173 -0
- package/src/lib/passes/ShaderPass.ts +88 -0
- package/src/lib/passes/index.ts +3 -0
- package/src/lib/react/MotionGPUErrorOverlay.tsx +392 -0
- package/src/lib/react/advanced.ts +36 -0
- package/src/lib/react/frame-context.ts +169 -0
- package/src/lib/react/index.ts +51 -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 +382 -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 +51 -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/react/MotionGPUErrorOverlay.tsx +0 -129
- /package/{dist → src/lib}/react/FragCanvas.tsx +0 -0
- /package/{dist → src/lib}/react/Portal.tsx +0 -0
|
@@ -1,365 +1,308 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { assertUniformValueForType } from "./uniforms.js";
|
|
2
|
+
import { resolveMaterial } from "./material.js";
|
|
3
|
+
import { toMotionGPUErrorReport } from "./error-report.js";
|
|
4
|
+
import { createRenderer } from "./renderer.js";
|
|
5
|
+
import { buildRendererPipelineSignature } from "./recompile-policy.js";
|
|
6
|
+
//#region src/lib/core/runtime-loop.ts
|
|
6
7
|
function getRendererRetryDelayMs(attempt) {
|
|
7
|
-
|
|
8
|
+
return Math.min(8e3, 250 * 2 ** Math.max(0, attempt - 1));
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
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
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
registry.getRenderMode() === 'always' ||
|
|
304
|
-
(registry.getRenderMode() === 'on-demand' && shouldRenderFrame);
|
|
305
|
-
if (shouldRenderFrame) {
|
|
306
|
-
for (const key of uniformKeys) {
|
|
307
|
-
const runtimeValue = runtimeUniforms[key];
|
|
308
|
-
renderUniforms[key] =
|
|
309
|
-
runtimeValue === undefined ? activeUniforms[key] : runtimeValue;
|
|
310
|
-
}
|
|
311
|
-
for (const key of textureKeys) {
|
|
312
|
-
const runtimeValue = runtimeTextures[key];
|
|
313
|
-
renderTextures[key] =
|
|
314
|
-
runtimeValue === undefined ? (activeTextures[key]?.source ?? null) : runtimeValue;
|
|
315
|
-
}
|
|
316
|
-
canvasSize.width = width;
|
|
317
|
-
canvasSize.height = height;
|
|
318
|
-
renderer.render({
|
|
319
|
-
time,
|
|
320
|
-
delta,
|
|
321
|
-
renderMode: registry.getRenderMode(),
|
|
322
|
-
uniforms: renderUniforms,
|
|
323
|
-
textures: renderTextures,
|
|
324
|
-
canvasSize
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
clearError();
|
|
328
|
-
}
|
|
329
|
-
catch (error) {
|
|
330
|
-
setError(error, 'render');
|
|
331
|
-
}
|
|
332
|
-
finally {
|
|
333
|
-
registry.endFrame();
|
|
334
|
-
}
|
|
335
|
-
if (shouldContinueAfterFrame) {
|
|
336
|
-
scheduleFrame();
|
|
337
|
-
}
|
|
338
|
-
};
|
|
339
|
-
(async () => {
|
|
340
|
-
try {
|
|
341
|
-
const initialMaterial = resolveActiveMaterial();
|
|
342
|
-
syncMaterialRuntimeState(initialMaterial);
|
|
343
|
-
activeRendererSignature = '';
|
|
344
|
-
scheduleFrame();
|
|
345
|
-
}
|
|
346
|
-
catch (error) {
|
|
347
|
-
setError(error, 'initialization');
|
|
348
|
-
scheduleFrame();
|
|
349
|
-
}
|
|
350
|
-
})();
|
|
351
|
-
return {
|
|
352
|
-
requestFrame,
|
|
353
|
-
invalidate,
|
|
354
|
-
advance,
|
|
355
|
-
destroy: () => {
|
|
356
|
-
isDisposed = true;
|
|
357
|
-
if (frameId !== null) {
|
|
358
|
-
cancelAnimationFrame(frameId);
|
|
359
|
-
frameId = null;
|
|
360
|
-
}
|
|
361
|
-
renderer?.destroy();
|
|
362
|
-
registry.clear();
|
|
363
|
-
}
|
|
364
|
-
};
|
|
10
|
+
function createMotionGPURuntimeLoop(options) {
|
|
11
|
+
const { canvas: canvasElement, registry, size } = options;
|
|
12
|
+
let frameId = null;
|
|
13
|
+
let renderer = null;
|
|
14
|
+
let isDisposed = false;
|
|
15
|
+
let previousTime = performance.now() / 1e3;
|
|
16
|
+
let activeRendererSignature = "";
|
|
17
|
+
let failedRendererSignature = null;
|
|
18
|
+
let failedRendererAttempts = 0;
|
|
19
|
+
let nextRendererRetryAt = 0;
|
|
20
|
+
let rendererRebuildPromise = null;
|
|
21
|
+
const runtimeUniforms = {};
|
|
22
|
+
const runtimeTextures = {};
|
|
23
|
+
let activeUniforms = {};
|
|
24
|
+
let activeTextures = {};
|
|
25
|
+
let uniformKeys = [];
|
|
26
|
+
let uniformKeySet = /* @__PURE__ */ new Set();
|
|
27
|
+
let uniformTypes = /* @__PURE__ */ new Map();
|
|
28
|
+
let textureKeys = [];
|
|
29
|
+
let textureKeySet = /* @__PURE__ */ new Set();
|
|
30
|
+
let activeMaterialSignature = "";
|
|
31
|
+
let currentCssWidth = -1;
|
|
32
|
+
let currentCssHeight = -1;
|
|
33
|
+
const renderUniforms = {};
|
|
34
|
+
const renderTextures = {};
|
|
35
|
+
const canvasSize = {
|
|
36
|
+
width: 0,
|
|
37
|
+
height: 0
|
|
38
|
+
};
|
|
39
|
+
let shouldContinueAfterFrame = false;
|
|
40
|
+
let activeErrorKey = null;
|
|
41
|
+
let errorHistory = [];
|
|
42
|
+
const getHistoryLimit = () => {
|
|
43
|
+
const value = options.getErrorHistoryLimit?.() ?? 0;
|
|
44
|
+
if (!Number.isFinite(value) || value <= 0) return 0;
|
|
45
|
+
return Math.floor(value);
|
|
46
|
+
};
|
|
47
|
+
const publishErrorHistory = () => {
|
|
48
|
+
options.reportErrorHistory?.(errorHistory);
|
|
49
|
+
const onErrorHistory = options.getOnErrorHistory?.();
|
|
50
|
+
if (!onErrorHistory) return;
|
|
51
|
+
try {
|
|
52
|
+
onErrorHistory(errorHistory);
|
|
53
|
+
} catch {}
|
|
54
|
+
};
|
|
55
|
+
const syncErrorHistory = () => {
|
|
56
|
+
const limit = getHistoryLimit();
|
|
57
|
+
if (limit <= 0) {
|
|
58
|
+
if (errorHistory.length === 0) return;
|
|
59
|
+
errorHistory = [];
|
|
60
|
+
publishErrorHistory();
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (errorHistory.length <= limit) return;
|
|
64
|
+
errorHistory = errorHistory.slice(errorHistory.length - limit);
|
|
65
|
+
publishErrorHistory();
|
|
66
|
+
};
|
|
67
|
+
const setError = (error, phase) => {
|
|
68
|
+
const report = toMotionGPUErrorReport(error, phase);
|
|
69
|
+
const reportKey = JSON.stringify({
|
|
70
|
+
phase: report.phase,
|
|
71
|
+
title: report.title,
|
|
72
|
+
message: report.message,
|
|
73
|
+
rawMessage: report.rawMessage
|
|
74
|
+
});
|
|
75
|
+
if (activeErrorKey === reportKey) return;
|
|
76
|
+
activeErrorKey = reportKey;
|
|
77
|
+
const historyLimit = getHistoryLimit();
|
|
78
|
+
if (historyLimit > 0) {
|
|
79
|
+
errorHistory = [...errorHistory, report];
|
|
80
|
+
if (errorHistory.length > historyLimit) errorHistory = errorHistory.slice(errorHistory.length - historyLimit);
|
|
81
|
+
publishErrorHistory();
|
|
82
|
+
}
|
|
83
|
+
options.reportError(report);
|
|
84
|
+
const onError = options.getOnError();
|
|
85
|
+
if (!onError) return;
|
|
86
|
+
try {
|
|
87
|
+
onError(report);
|
|
88
|
+
} catch {}
|
|
89
|
+
};
|
|
90
|
+
const clearError = () => {
|
|
91
|
+
if (activeErrorKey === null) return;
|
|
92
|
+
activeErrorKey = null;
|
|
93
|
+
options.reportError(null);
|
|
94
|
+
};
|
|
95
|
+
const scheduleFrame = () => {
|
|
96
|
+
if (isDisposed || frameId !== null) return;
|
|
97
|
+
frameId = requestAnimationFrame(renderFrame);
|
|
98
|
+
};
|
|
99
|
+
const requestFrame = () => {
|
|
100
|
+
scheduleFrame();
|
|
101
|
+
};
|
|
102
|
+
const invalidate = (token) => {
|
|
103
|
+
registry.invalidate(token);
|
|
104
|
+
requestFrame();
|
|
105
|
+
};
|
|
106
|
+
const advance = () => {
|
|
107
|
+
registry.advance();
|
|
108
|
+
requestFrame();
|
|
109
|
+
};
|
|
110
|
+
const resetRuntimeMaps = () => {
|
|
111
|
+
for (const key of Object.keys(runtimeUniforms)) if (!uniformKeySet.has(key)) Reflect.deleteProperty(runtimeUniforms, key);
|
|
112
|
+
for (const key of Object.keys(runtimeTextures)) if (!textureKeySet.has(key)) Reflect.deleteProperty(runtimeTextures, key);
|
|
113
|
+
};
|
|
114
|
+
const resetRenderPayloadMaps = () => {
|
|
115
|
+
for (const key of Object.keys(renderUniforms)) if (!uniformKeySet.has(key)) Reflect.deleteProperty(renderUniforms, key);
|
|
116
|
+
for (const key of Object.keys(renderTextures)) if (!textureKeySet.has(key)) Reflect.deleteProperty(renderTextures, key);
|
|
117
|
+
};
|
|
118
|
+
const syncMaterialRuntimeState = (materialState) => {
|
|
119
|
+
const signatureChanged = activeMaterialSignature !== materialState.signature;
|
|
120
|
+
const defaultsChanged = activeUniforms !== materialState.uniforms || activeTextures !== materialState.textures;
|
|
121
|
+
if (!signatureChanged && !defaultsChanged) return;
|
|
122
|
+
activeUniforms = materialState.uniforms;
|
|
123
|
+
activeTextures = materialState.textures;
|
|
124
|
+
if (!signatureChanged) return;
|
|
125
|
+
uniformKeys = materialState.uniformLayout.entries.map((entry) => entry.name);
|
|
126
|
+
uniformTypes = new Map(materialState.uniformLayout.entries.map((entry) => [entry.name, entry.type]));
|
|
127
|
+
textureKeys = materialState.textureKeys;
|
|
128
|
+
uniformKeySet = new Set(uniformKeys);
|
|
129
|
+
textureKeySet = new Set(textureKeys);
|
|
130
|
+
resetRuntimeMaps();
|
|
131
|
+
resetRenderPayloadMaps();
|
|
132
|
+
activeMaterialSignature = materialState.signature;
|
|
133
|
+
};
|
|
134
|
+
const resolveActiveMaterial = () => {
|
|
135
|
+
return resolveMaterial(options.getMaterial());
|
|
136
|
+
};
|
|
137
|
+
const setUniform = (name, value) => {
|
|
138
|
+
if (!uniformKeySet.has(name)) throw new Error(`Unknown uniform "${name}". Declare it in material.uniforms first.`);
|
|
139
|
+
const expectedType = uniformTypes.get(name);
|
|
140
|
+
if (!expectedType) throw new Error(`Unknown uniform type for "${name}"`);
|
|
141
|
+
assertUniformValueForType(expectedType, value);
|
|
142
|
+
runtimeUniforms[name] = value;
|
|
143
|
+
};
|
|
144
|
+
const setTexture = (name, value) => {
|
|
145
|
+
if (!textureKeySet.has(name)) throw new Error(`Unknown texture "${name}". Declare it in material.textures first.`);
|
|
146
|
+
runtimeTextures[name] = value;
|
|
147
|
+
};
|
|
148
|
+
const renderFrame = (timestamp) => {
|
|
149
|
+
frameId = null;
|
|
150
|
+
if (isDisposed) return;
|
|
151
|
+
syncErrorHistory();
|
|
152
|
+
let materialState;
|
|
153
|
+
try {
|
|
154
|
+
materialState = resolveActiveMaterial();
|
|
155
|
+
} catch (error) {
|
|
156
|
+
setError(error, "initialization");
|
|
157
|
+
scheduleFrame();
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
shouldContinueAfterFrame = false;
|
|
161
|
+
const outputColorSpace = options.getOutputColorSpace();
|
|
162
|
+
const rendererSignature = buildRendererPipelineSignature({
|
|
163
|
+
materialSignature: materialState.signature,
|
|
164
|
+
outputColorSpace
|
|
165
|
+
});
|
|
166
|
+
syncMaterialRuntimeState(materialState);
|
|
167
|
+
if (failedRendererSignature && failedRendererSignature !== rendererSignature) {
|
|
168
|
+
failedRendererSignature = null;
|
|
169
|
+
failedRendererAttempts = 0;
|
|
170
|
+
nextRendererRetryAt = 0;
|
|
171
|
+
}
|
|
172
|
+
if (!renderer || activeRendererSignature !== rendererSignature) {
|
|
173
|
+
if (failedRendererSignature === rendererSignature && performance.now() < nextRendererRetryAt) {
|
|
174
|
+
scheduleFrame();
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (!rendererRebuildPromise) rendererRebuildPromise = (async () => {
|
|
178
|
+
try {
|
|
179
|
+
const nextRenderer = await createRenderer({
|
|
180
|
+
canvas: canvasElement,
|
|
181
|
+
fragmentWgsl: materialState.fragmentWgsl,
|
|
182
|
+
fragmentLineMap: materialState.fragmentLineMap,
|
|
183
|
+
fragmentSource: materialState.fragmentSource,
|
|
184
|
+
includeSources: materialState.includeSources,
|
|
185
|
+
defineBlockSource: materialState.defineBlockSource,
|
|
186
|
+
materialSource: materialState.source,
|
|
187
|
+
materialSignature: materialState.signature,
|
|
188
|
+
uniformLayout: materialState.uniformLayout,
|
|
189
|
+
textureKeys: materialState.textureKeys,
|
|
190
|
+
textureDefinitions: materialState.textures,
|
|
191
|
+
getRenderTargets: options.getRenderTargets,
|
|
192
|
+
getPasses: options.getPasses,
|
|
193
|
+
outputColorSpace,
|
|
194
|
+
getClearColor: options.getClearColor,
|
|
195
|
+
getDpr: () => options.dpr.current,
|
|
196
|
+
adapterOptions: options.getAdapterOptions(),
|
|
197
|
+
deviceDescriptor: options.getDeviceDescriptor()
|
|
198
|
+
});
|
|
199
|
+
if (isDisposed) {
|
|
200
|
+
nextRenderer.destroy();
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
renderer?.destroy();
|
|
204
|
+
renderer = nextRenderer;
|
|
205
|
+
activeRendererSignature = rendererSignature;
|
|
206
|
+
failedRendererSignature = null;
|
|
207
|
+
failedRendererAttempts = 0;
|
|
208
|
+
nextRendererRetryAt = 0;
|
|
209
|
+
clearError();
|
|
210
|
+
} catch (error) {
|
|
211
|
+
failedRendererSignature = rendererSignature;
|
|
212
|
+
failedRendererAttempts += 1;
|
|
213
|
+
const retryDelayMs = getRendererRetryDelayMs(failedRendererAttempts);
|
|
214
|
+
nextRendererRetryAt = performance.now() + retryDelayMs;
|
|
215
|
+
setError(error, "initialization");
|
|
216
|
+
} finally {
|
|
217
|
+
rendererRebuildPromise = null;
|
|
218
|
+
scheduleFrame();
|
|
219
|
+
}
|
|
220
|
+
})();
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const time = timestamp / 1e3;
|
|
224
|
+
const rawDelta = Math.max(0, time - previousTime);
|
|
225
|
+
const delta = Math.min(rawDelta, options.maxDelta.current);
|
|
226
|
+
previousTime = time;
|
|
227
|
+
const rect = canvasElement.getBoundingClientRect();
|
|
228
|
+
const width = Math.max(0, Math.floor(rect.width));
|
|
229
|
+
const height = Math.max(0, Math.floor(rect.height));
|
|
230
|
+
if (width !== currentCssWidth || height !== currentCssHeight) {
|
|
231
|
+
currentCssWidth = width;
|
|
232
|
+
currentCssHeight = height;
|
|
233
|
+
size.set({
|
|
234
|
+
width,
|
|
235
|
+
height
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
try {
|
|
239
|
+
registry.run({
|
|
240
|
+
time,
|
|
241
|
+
delta,
|
|
242
|
+
setUniform,
|
|
243
|
+
setTexture,
|
|
244
|
+
invalidate,
|
|
245
|
+
advance,
|
|
246
|
+
renderMode: registry.getRenderMode(),
|
|
247
|
+
autoRender: registry.getAutoRender(),
|
|
248
|
+
canvas: canvasElement
|
|
249
|
+
});
|
|
250
|
+
const shouldRenderFrame = registry.shouldRender();
|
|
251
|
+
shouldContinueAfterFrame = registry.getRenderMode() === "always" || registry.getRenderMode() === "on-demand" && shouldRenderFrame;
|
|
252
|
+
if (shouldRenderFrame) {
|
|
253
|
+
for (const key of uniformKeys) {
|
|
254
|
+
const runtimeValue = runtimeUniforms[key];
|
|
255
|
+
renderUniforms[key] = runtimeValue === void 0 ? activeUniforms[key] : runtimeValue;
|
|
256
|
+
}
|
|
257
|
+
for (const key of textureKeys) {
|
|
258
|
+
const runtimeValue = runtimeTextures[key];
|
|
259
|
+
renderTextures[key] = runtimeValue === void 0 ? activeTextures[key]?.source ?? null : runtimeValue;
|
|
260
|
+
}
|
|
261
|
+
canvasSize.width = width;
|
|
262
|
+
canvasSize.height = height;
|
|
263
|
+
renderer.render({
|
|
264
|
+
time,
|
|
265
|
+
delta,
|
|
266
|
+
renderMode: registry.getRenderMode(),
|
|
267
|
+
uniforms: renderUniforms,
|
|
268
|
+
textures: renderTextures,
|
|
269
|
+
canvasSize
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
clearError();
|
|
273
|
+
} catch (error) {
|
|
274
|
+
setError(error, "render");
|
|
275
|
+
} finally {
|
|
276
|
+
registry.endFrame();
|
|
277
|
+
}
|
|
278
|
+
if (shouldContinueAfterFrame) scheduleFrame();
|
|
279
|
+
};
|
|
280
|
+
(async () => {
|
|
281
|
+
try {
|
|
282
|
+
syncMaterialRuntimeState(resolveActiveMaterial());
|
|
283
|
+
activeRendererSignature = "";
|
|
284
|
+
scheduleFrame();
|
|
285
|
+
} catch (error) {
|
|
286
|
+
setError(error, "initialization");
|
|
287
|
+
scheduleFrame();
|
|
288
|
+
}
|
|
289
|
+
})();
|
|
290
|
+
return {
|
|
291
|
+
requestFrame,
|
|
292
|
+
invalidate,
|
|
293
|
+
advance,
|
|
294
|
+
destroy: () => {
|
|
295
|
+
isDisposed = true;
|
|
296
|
+
if (frameId !== null) {
|
|
297
|
+
cancelAnimationFrame(frameId);
|
|
298
|
+
frameId = null;
|
|
299
|
+
}
|
|
300
|
+
renderer?.destroy();
|
|
301
|
+
registry.clear();
|
|
302
|
+
}
|
|
303
|
+
};
|
|
365
304
|
}
|
|
305
|
+
//#endregion
|
|
306
|
+
export { createMotionGPURuntimeLoop };
|
|
307
|
+
|
|
308
|
+
//# sourceMappingURL=runtime-loop.js.map
|