()
| 496 | } |
| 497 | |
| 498 | const initializeWebGPU = async () => { |
| 499 | if (!containerRef) { |
| 500 | return |
| 501 | } |
| 502 | |
| 503 | await new Promise((resolve) => setTimeout(resolve, 10)) |
| 504 | |
| 505 | if (!containerRef) { |
| 506 | return |
| 507 | } |
| 508 | |
| 509 | if (!navigator.gpu) { |
| 510 | console.warn("WebGPU is not supported in this browser") |
| 511 | return |
| 512 | } |
| 513 | |
| 514 | const adapter = await navigator.gpu.requestAdapter({ |
| 515 | powerPreference: "high-performance", |
| 516 | }) |
| 517 | if (!adapter) { |
| 518 | console.warn("Failed to get WebGPU adapter") |
| 519 | return |
| 520 | } |
| 521 | |
| 522 | const device = await adapter.requestDevice() |
| 523 | deviceRef = device |
| 524 | |
| 525 | const canvas = document.createElement("canvas") |
| 526 | canvas.style.width = "100%" |
| 527 | canvas.style.height = "100%" |
| 528 | canvasRef = canvas |
| 529 | |
| 530 | while (containerRef.firstChild) { |
| 531 | containerRef.removeChild(containerRef.firstChild) |
| 532 | } |
| 533 | containerRef.appendChild(canvas) |
| 534 | |
| 535 | const context = canvas.getContext("webgpu") |
| 536 | if (!context) { |
| 537 | console.warn("Failed to get WebGPU context") |
| 538 | return |
| 539 | } |
| 540 | contextRef = context |
| 541 | |
| 542 | const presentationFormat = navigator.gpu.getPreferredCanvasFormat() |
| 543 | context.configure({ |
| 544 | device, |
| 545 | format: presentationFormat, |
| 546 | alphaMode: "premultiplied", |
| 547 | }) |
| 548 | |
| 549 | const shaderModule = device.createShaderModule({ |
| 550 | code: WGSL_SHADER, |
| 551 | }) |
| 552 | |
| 553 | const uniformBuffer = device.createBuffer({ |
| 554 | size: UNIFORM_BUFFER_SIZE, |
| 555 | usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST, |
no test coverage detected