WebGPU

The modern GPU API for the web (and, via Dawn/wgpu, native apps). Compute shaders in WGSL run on NVIDIA, AMD, Intel and Apple GPUs from the same code, no install — the easiest way to ship GPU compute to everyone. Now shipping in Chrome, Edge, Firefox and Safari.

W3CJavaScript · WGSLbrowsercross-vendoropen standard

Official docs ↗ ← All libraries

Install

<!-- no install: served over https, opened in a WebGPU-capable browser -->
<script type="module" src="add.js"></script>

Hello, GPU

add.js — double an array in a compute shader

const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();

const shader = device.createShaderModule({ code: `
  @group(0) @binding(0) var<storage, read_write> data: array<f32>;
  @compute @workgroup_size(64)
  fn main(@builtin(global_invocation_id) id: vec3<u32>) {
    data[id.x] = data[id.x] * 2.0;
  }`
});

const input = new Float32Array([1, 2, 3, 4]);
const buf = device.createBuffer({ size: input.byteLength,
  usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST });
device.queue.writeBuffer(buf, 0, input);

const pipeline = device.createComputePipeline({ layout: "auto",
  compute: { module: shader, entryPoint: "main" } });
const bind = device.createBindGroup({ layout: pipeline.getBindGroupLayout(0),
  entries: [{ binding: 0, resource: { buffer: buf } }] });

const enc = device.createCommandEncoder();
const pass = enc.beginComputePass();
pass.setPipeline(pipeline); pass.setBindGroup(0, bind);
pass.dispatchWorkgroups(1); pass.end();
device.queue.submit([enc.finish()]);   // data is now [2, 4, 6, 8]

Run it:

# serve over https/localhost and open in Chrome 113+, Edge, Firefox or Safari 26+

Learn more