import shaderCode from "./shader.wgsl?raw"
const adapter = await navigator.gpu.requestAdapter()
throw new Error("WebGPU cannot be initialized - Adapter not found")
const device = await adapter.requestDevice()
throw new Error("WebGPU cannot be initialized - Device has been lost")
const canvas = document.querySelector("canvas")
const context = canvas!.getContext("webgpu")
throw new Error("WebGPU cannot be initialized - Canvas does not support WebGPU")
const canvasFormat = navigator.gpu.getPreferredCanvasFormat()
context.configure({ device, format: canvasFormat })
const vertices = new Float32Array([0.0, 0.5, -0.5, -0.5, 0.5, -0.5])
const vertexBuffer = device.createBuffer({
size: vertices.byteLength,
usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST
device.queue.writeBuffer(vertexBuffer, 0, vertices)
const shaderModule = device.createShaderModule({ code: shaderCode })
const renderPipeline = device.createRenderPipeline({
arrayStride: vertices.BYTES_PER_ELEMENT * 2,
const commandEncoder = device.createCommandEncoder()
const renderPass = commandEncoder.beginRenderPass({
view: context.getCurrentTexture().createView(),
clearValue: { r: 0.749, g: 0.925, b: 1.0, a: 1 },
renderPass.setPipeline(renderPipeline)
renderPass.setVertexBuffer(0, vertexBuffer)
device.queue.submit([commandEncoder.finish()])