/* Minimal WebGPU control page: one compute dispatch plus one render pass per * frame. Used by the headed tools as the "environment vs application" control * (see docs/web/HEADED-TOOLS.md). */ const canvas = document.getElementById("canvas"); const adapter = await navigator.gpu?.requestAdapter(); if (!adapter) { document.title = "no-adapter"; throw new Error("no webgpu adapter"); } const device = await adapter.requestDevice(); document.title = `webgpu ${adapter.info?.vendor ?? ""}/${adapter.info?.architecture ?? ""}`; const context = canvas.getContext("webgpu"); const format = navigator.gpu.getPreferredCanvasFormat(); context.configure({ device, format, alphaMode: "opaque" }); const N = 100000; const data = new Float32Array(N).map((_, index) => Math.sin(index)); const buffer = device.createBuffer({ size: data.byteLength, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC }); device.queue.writeBuffer(buffer, 0, data); const module = device.createShaderModule({ code: ` @group(0) @binding(0) var data: array; @compute @workgroup_size(64) fn main(@builtin(global_invocation_id) id: vec3) { let i = id.x; if (i < arrayLength(&data)) { data[i] = sin(data[i] + 0.01); } }`, }); const pipeline = device.createComputePipeline({ layout: "auto", compute: { module, entryPoint: "main" } }); const bind = device.createBindGroup({ layout: pipeline.getBindGroupLayout(0), entries: [{ binding: 0, resource: { buffer } }] }); let frame = 0; function tick() { const encoder = device.createCommandEncoder(); const pass = encoder.beginComputePass(); pass.setPipeline(pipeline); pass.setBindGroup(0, bind); pass.dispatchWorkgroups(Math.ceil(N / 64)); pass.end(); const view = context.getCurrentTexture().createView(); const render = encoder.beginRenderPass({ colorAttachments: [{ view, clearValue: { r: 0.06 + 0.02 * Math.sin(frame / 10), g: 0.2, b: 0.5, a: 1 }, loadOp: "clear", storeOp: "store" }], }); render.end(); device.queue.submit([encoder.finish()]); frame += 1; document.body.dataset.frames = String(frame); requestAnimationFrame(tick); } tick();