Changed around line 1
+ async function initWebGPU() {
+ if (!navigator.gpu) {
+ alert('WebGPU not supported on this browser.');
+ return;
+ }
+
+ const adapter = await navigator.gpu.requestAdapter();
+ const device = await adapter.requestDevice();
+
+ const canvas = document.getElementById('webgpu-canvas');
+ const context = canvas.getContext('webgpu');
+ const format = navigator.gpu.getPreferredCanvasFormat();
+
+ context.configure({
+ device: device,
+ format: format,
+ alphaMode: 'opaque'
+ });
+
+ const shaderModule = device.createShaderModule({
+ code: `
+ @vertex
+ fn vertexMain(@builtin(vertex_index) vertexIndex : u32) -> @builtin(position) vec4f {
+ let pos = array(
+ vec2f( 0.0, 0.5),
+ vec2f(-0.5, -0.5),
+ vec2f( 0.5, -0.5)
+ );
+ return vec4f(pos[vertexIndex], 0.0, 1.0);
+ }
+
+ @fragment
+ fn fragmentMain() -> @location(0) vec4f {
+ return vec4f(1.0, 0.0, 0.0, 1.0);
+ }
+ `
+ });
+
+ const pipeline = device.createRenderPipeline({
+ layout: 'auto',
+ vertex: {
+ module: shaderModule,
+ entryPoint: 'vertexMain'
+ },
+ fragment: {
+ module: shaderModule,
+ entryPoint: 'fragmentMain',
+ targets: [{ format }]
+ },
+ primitive: {
+ topology: 'triangle-list'
+ }
+ });
+
+ const commandEncoder = device.createCommandEncoder();
+ const textureView = context.getCurrentTexture().createView();
+ const renderPass = commandEncoder.beginRenderPass({
+ colorAttachments: [{
+ view: textureView,
+ clearValue: { r: 0.0, g: 0.0, b: 0.0, a: 1.0 },
+ loadOp: 'clear',
+ storeOp: 'store'
+ }]
+ });
+
+ renderPass.setPipeline(pipeline);
+ renderPass.draw(3);
+ renderPass.end();
+
+ device.queue.submit([commandEncoder.finish()]);
+ }
+
+ window.addEventListener('load', initWebGPU);