()
| 346 | * Clamps to maxTextureDimension2D to avoid WebGPU validation errors on large/zoomed viewports. |
| 347 | */ |
| 348 | const resizeCanvasAndConfigure = (): void => { |
| 349 | const rect = canvas.getBoundingClientRect(); |
| 350 | const dpr = window.devicePixelRatio || 1; |
| 351 | |
| 352 | const maxDimension = gpuContext?.device?.limits.maxTextureDimension2D ?? 8192; |
| 353 | const width = Math.min(maxDimension, Math.max(1, Math.round(rect.width * dpr))); |
| 354 | const height = Math.min(maxDimension, Math.max(1, Math.round(rect.height * dpr))); |
| 355 | |
| 356 | const sizeChanged = canvas.width !== width || canvas.height !== height; |
| 357 | if (sizeChanged) { |
| 358 | canvas.width = width; |
| 359 | canvas.height = height; |
| 360 | } |
| 361 | |
| 362 | const device = gpuContext?.device; |
| 363 | const canvasContext = gpuContext?.canvasContext; |
| 364 | const preferredFormat = gpuContext?.preferredFormat; |
| 365 | |
| 366 | if (!device || !canvasContext || !preferredFormat) return; |
| 367 | |
| 368 | const shouldConfigure = |
| 369 | sizeChanged || |
| 370 | !lastConfigured || |
| 371 | lastConfigured.width !== canvas.width || |
| 372 | lastConfigured.height !== canvas.height || |
| 373 | lastConfigured.format !== preferredFormat; |
| 374 | |
| 375 | if (!shouldConfigure) return; |
| 376 | |
| 377 | canvasContext.configure({ |
| 378 | device, |
| 379 | format: preferredFormat, |
| 380 | alphaMode: 'opaque', |
| 381 | }); |
| 382 | lastConfigured = { width: canvas.width, height: canvas.height, format: preferredFormat }; |
| 383 | |
| 384 | if (coordinator && coordinatorTargetFormat !== preferredFormat) { |
| 385 | // Recreate coordinator through the shared helper so we don't leak subscriptions/slider bindings. |
| 386 | coordinatorTargetFormat = preferredFormat; |
| 387 | recreateCoordinator(); |
| 388 | } |
| 389 | }; |
| 390 | |
| 391 | const recreateCoordinator = (): void => { |
| 392 | if (!gpuContext) return; |
no test coverage detected