diff --git a/web/app/src/app/WasmGPUCanvas.tsx b/web/app/src/app/WasmGPUCanvas.tsx index 6cc6c303b..c332fdb45 100644 --- a/web/app/src/app/WasmGPUCanvas.tsx +++ b/web/app/src/app/WasmGPUCanvas.tsx @@ -40,6 +40,7 @@ export function WasmGPUCanvas({ client, active = true, playing = false, onApplie const [error, setError] = useState(""); const [retry, setRetry] = useState(0), [retrying, setRetrying] = useState(false); const retrySurface = useRef<(() => Promise) | null>(null); + const computeRebinds = useRef(0); const [ready, setReady] = useState(false), [nativeAttached, setNativeAttached] = useState(false); const [fileSession, setFileSession] = useState(''); const [filesOpen, setFilesOpen] = useState(false); @@ -274,13 +275,24 @@ export function WasmGPUCanvas({ client, active = true, playing = false, onApplie /* A lost WebGPU device leaves the native draw transaction incomplete; replaying * the frame in place hits a broken GL state. Rebuild the whole surface through * the manual-retry path instead, bounded so a persistent fault still surfaces. */ - let computeRebinds = 0; const failSurface = (reason: unknown) => { const detail = reason && typeof reason === 'object' && 'message' in reason ? String((reason as { message?: unknown }).message) : String(reason); fail(reason); - if (computeRebinds < 2 && detail.includes('COMPUTE_DEVICE_LOST')) { - computeRebinds += 1; + if (computeRebinds.current < 2 && detail.includes('COMPUTE_DEVICE_LOST')) { + computeRebinds.current += 1; + window.setTimeout(() => { if (!disposed) void retrySurface.current?.(); }, 400); + } + }; + /* A failed attach reports the native boolean create result, so a compute + * device loss during attach arrives without its COMPUTE_DEVICE_LOST text. + * Replay the surface through the same bounded path instead of stopping. */ + const failAttach = (reason: unknown) => { + const detail = reason && typeof reason === 'object' && 'message' in reason + ? String((reason as { message?: unknown }).message) : String(reason); + failSurface(reason); + if (computeRebinds.current < 2 && !detail.includes('COMPUTE_DEVICE_LOST')) { + computeRebinds.current += 1; window.setTimeout(() => { if (!disposed) void retrySurface.current?.(); }, 400); } }; @@ -718,7 +730,7 @@ export function WasmGPUCanvas({ client, active = true, playing = false, onApplie const ownerReady = Boolean(result.displayAcknowledged && !ownerChanged && !result.inputDiscarded && !error && attached && !contextLost && !events.length); readyOwnerFrames = ownerReady ? readyOwnerFrames + 1 : 0; canvas.dataset.nativeInputReady = String(Boolean(!disposed && !stopped && ownerReady && readyOwnerFrames >= 2)); - if (!readyPublished && canvas.dataset.nativeInputReady === 'true') { readyPublished = true; computeRebinds = 0; setReady(true); onReady?.(true); } + if (!readyPublished && canvas.dataset.nativeInputReady === 'true') { readyPublished = true; computeRebinds.current = 0; setReady(true); onReady?.(true); } const redrawPending = error?.code === 'CAPABILITY_MISSING' && (error.message.includes('Browser region requires redraw') || error.message.includes('Browser region draw buffer is unavailable')); if (result.inputDiscarded && !ownerChanged) { events = []; @@ -888,7 +900,7 @@ export function WasmGPUCanvas({ client, active = true, playing = false, onApplie if (document.activeElement === canvas) focus(7); else canvas.focus(); frame = requestAnimationFrame(() => void draw()); - })(); void startup.catch(fail); + })(); void startup.catch(failAttach); // Keep worker notifications within this canvas effect's lifecycle. const onWorkerMessage = (event: MessageEvent) => { const type = event.data?.type;