Skip to content

キャンバスの初期化

const adapter = await navigator.gpu.requestAdapter()
if (!adapter) {
throw new Error("WebGPU cannot be initialized - Adapter not found")
}
const device = await adapter.requestDevice()
device.lost.then(() => {
throw new Error("WebGPU cannot be initialized - Device has been lost")
})
const canvas = document.querySelector("canvas")
const context = canvas!.getContext("webgpu")
if (!context) {
throw new Error("WebGPU cannot be initialized - Canvas does not support WebGPU")
}
const format = navigator.gpu.getPreferredCanvasFormat()
context.configure({ device, format })
const commandEncoder = device.createCommandEncoder()
const renderPass = commandEncoder.beginRenderPass({
colorAttachments: [
{
view: context.getCurrentTexture().createView(),
loadOp: "clear",
clearValue: { r: 0.749, g: 0.925, b: 1.0, a: 1 },
storeOp: "store"
}
]
})
renderPass.end()
device.queue.submit([commandEncoder.finish()])