Native WebGPU

Native WebGPU with Dawn and wgpu-native

Both engines of Architecture are also native libraries sharing one C header, webgpu.h (webgpu-native/webgpu-headers (https://github.com/webgpu-native/webgpu-headers 588 )), "a multi-vendor standard between Dawn/Emdawnwebgpu and wgpu-native". A C++ renderer written against it links either library, and compiled with Emscripten 175,027 's emdawnwebgpu port it runs in the browser. Dawn 1,149 is built from source with CMake; wgpu-native (gfx-rs/wgpu-native (https://github.com/gfx-rs/wgpu-native 1,393 ), MIT or Apache-2.0, v29.0.1.1) ships prebuilt libraries with wrappers for Python, .NET, Go, Java, Julia, Zig and more; Rust code uses wgpu directly.

pip 21,050 install wgpu (wgpu-py 0.32.0, BSD 2-Clause) bundles wgpu-native. A Python translation of WebGPU in Node.js and Deno's script (demos/ch04/discount.py, same WGSL) printed NVIDIA GeForce GTX 1650 | Vulkan and the same six prices: the shader ran unchanged under a second engine and a second native API. Native code also gets features browsers withhold, but code that depends on them no longer ports to the web.

One C header, two engines: native apps and language wrappers on webgpu.h, implemented by Dawn and by wgpu-nativeHTMLLive
<!doctype html>
<style>
  body { margin: 0; background: #f7f4ee; font: 14px system-ui, sans-serif; }
  .stage { position: relative; width: 100%; max-width: 600px; }
  .stage canvas { display: block; width: 100%; }
  .stage canvas + canvas { position: absolute; inset: 0; pointer-events: none; }
</style>
<div class="stage">
  <canvas id="view" width="600" height="370"></canvas>
  <canvas id="labels" width="600" height="370"></canvas>
</div>
<script>
const canvas = document.getElementById('view');
const ink = document.getElementById('labels').getContext('2d');

function showMessage(text) {                     // 2D fallback when WebGPU is missing
  const ctx = canvas.getContext('2d');
  ctx.fillStyle = '#fbeaea'; ctx.fillRect(0, 0, canvas.width, canvas.height);
  ctx.fillStyle = '#8a2b2b'; ctx.font = '18px system-ui, sans-serif'; ctx.textAlign = 'center';
  ctx.fillText(text, canvas.width / 2, canvas.height / 2);
}
function label(text, x, y, size = 12, color = '#2b2b2b', align = 'center', weight = '') {
  ink.font = `${weight} ${size}px system-ui, sans-serif`; ink.fillStyle = color; ink.textAlign = align; ink.fillText(text, x, y);
}

const APP = [0.86, 0.91, 0.97], HDR = [0.98, 0.90, 0.72], ENG = [0.93, 0.88, 0.96], API = [0.88, 0.88, 0.88];
const blocks = [  // text, sub, x, y, w, h, colour
  ['C++ renderer', '', 14, 20, 130, 40, APP], ['Python', 'pip install wgpu', 154, 20, 130, 40, APP],
  ['.NET, Go, Java, Zig...', 'wgpu-native wrappers', 294, 20, 150, 40, APP], ['Rust apps', 'use wgpu directly', 454, 20, 132, 40, APP],
  ['webgpu.h', 'webgpu-native/webgpu-headers: one C API', 14, 96, 430, 44, HDR],
  ['Dawn', 'C++, built with CMake', 14, 176, 205, 50, ENG], ['wgpu-native', 'Rust core, prebuilt libraries', 239, 176, 205, 50, ENG],
  ['wgpu (crate)', 'Rust API', 454, 176, 132, 50, ENG],
  ['Direct3D 12', '', 14, 262, 136, 34, API], ['Vulkan', '', 162, 262, 136, 34, API], ['Metal', '', 310, 262, 136, 34, API], ['OpenGL (ES)', '', 458, 262, 128, 34, API],
  ['Emscripten + emdawnwebgpu: the same C++ in the browser', '', 14, 312, 572, 32, [0.86, 0.93, 0.87]]];
const links = [[79, 60, 79, 96], [219, 60, 219, 96], [369, 60, 369, 96], [520, 60, 520, 176], [116, 140, 116, 176], [341, 140, 341, 176],
  [80, 226, 80, 262], [230, 226, 230, 262], [380, 226, 380, 262], [520, 226, 520, 262]];

async function main() {
  const adapter = await navigator.gpu?.requestAdapter();
  if (!adapter) return showMessage('WebGPU is not available in this browser');
  const device = await adapter.requestDevice();
  const context = canvas.getContext('webgpu');
  const format = navigator.gpu.getPreferredCanvasFormat();
  context.configure({ device, format });

  const boxes = [];
  for (const [x1, y1, x2, y2] of links) boxes.push([Math.min(x1, x2) - 1, y1, 2, y2 - y1, 0.45, 0.45, 0.45, 1], [x2 - 4, y2 - 5, 8, 8, 0.45, 0.45, 0.45, 4]);
  for (const [name, sub, x, y, w, h, c] of blocks) {
    boxes.push([x, y, w, h, ...c, 9]);
    label(name, x + w / 2, y + (sub ? h / 2 - 2 : h / 2 + 4), 12.5, '#222', 'center', 'bold');
    if (sub) label(sub, x + w / 2, y + h / 2 + 13, 10.5, '#555');
  }
  label('The same discount shader gave the same six prices under Dawn/D3D12 (Node.js) and wgpu/Vulkan (Python).', 14, 364, 11.5, '#1f4f8a', 'left');

  const module = device.createShaderModule({ code: `
    struct Box { rect: vec4f, style: vec4f }          // style: r, g, b, corner radius
    @group(0) @binding(0) var<storage> boxes: array<Box>;
    struct Out { @builtin(position) pos: vec4f, @location(0) local: vec2f,
                 @location(1) @interpolate(flat) i: u32 }
    @vertex fn vs(@builtin(vertex_index) v: u32, @builtin(instance_index) i: u32) -> Out {
      let corner = vec2f(f32(v & 1), f32(v >> 1));   // triangle-strip corners
      let r = boxes[i].rect;
      let px = r.xy + corner * r.zw;
      return Out(vec4f(px.x / 300 - 1, 1 - px.y / 185, 0, 1), corner * r.zw, i);
    }
    @fragment fn fs(in: Out) -> @location(0) vec4f {
      let b = boxes[in.i];
      let half = b.rect.zw / 2;
      let q = abs(in.local - half) - half + b.style.w; // signed distance to a rounded box
      let d = length(max(q, vec2f(0))) + min(max(q.x, q.y), 0) - b.style.w;
      let a = clamp(0.5 - d, 0, 1);
      return vec4f(b.style.rgb * a, a);               // premultiplied alpha
    }` });
  const blend = { srcFactor: 'one', dstFactor: 'one-minus-src-alpha' };
  const pipeline = device.createRenderPipeline({ layout: 'auto',
    vertex: { module }, primitive: { topology: 'triangle-strip' },
    fragment: { module, targets: [{ format, blend: { color: blend, alpha: blend } }] } });
  const data = new Float32Array(boxes.flat());
  const buffer = device.createBuffer({ size: data.byteLength, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST });
  device.queue.writeBuffer(buffer, 0, data);
  const encoder = device.createCommandEncoder();
  const pass = encoder.beginRenderPass({ colorAttachments: [{ view: context.getCurrentTexture().createView(),
    clearValue: [0.97, 0.96, 0.93, 1], loadOp: 'clear', storeOp: 'store' }] });
  pass.setPipeline(pipeline);
  pass.setBindGroup(0, device.createBindGroup({ layout: pipeline.getBindGroupLayout(0), entries: [{ binding: 0, resource: { buffer } }] }));
  pass.draw(4, boxes.length);
  pass.end();
  device.queue.submit([encoder.finish()]);
}
main();
</script>