juj/wasm_webgpu
System headers for interfacing WebGPU from C programs compiled via Emscripten to WebAssembly
Overview
wasm_webgpu is a small open‑source library that lets C/C++ programs talk to the WebGPU graphics API when they are compiled to WebAssembly with Emscripten. It provides a thin, 1‑to‑1 mapping of the browser‑side WebGPU JavaScript API so developers can write GPU‑accelerated rendering code in native C/C++ and run it in the browser (or, experimentally, via Dawn outside the browser).
What it does
| Feature | What it means for you |
|---|---|
Header + implementation files (lib_webgpu.*) |
Drop these into any Emscripten project, include lib_webgpu.h, and you get C‑style wrappers such as wgpu_adapter_request_device_sync() that call the underlying WebGPU JS functions. |
| Exact API mapping | Types like GPUAdapter become WGpuAdapter; functions keep the same shape (wgpu_canvas_context_configure). This makes the spec a reliable reference when writing C code. |
| Performance‑focused | The binding layer is deliberately low‑level and avoids extra JavaScript objects. It works with Closure Compiler for aggressive minification and tries to keep runtime overhead and generated code size tiny. |
| Custom helpers | Functions for reading/writing mapped buffers and loading images from URLs simplify common WebGPU tasks that are awkward to marshal between JS and Wasm. |
| JSPI (synchronous API) support | Optional flags (-sJSPI) expose synchronous versions of normally async WebGPU calls, handy for quick prototypes or test harnesses. |
| OffscreenCanvas & workers | APIs to create and control an OffscreenCanvas from a WebWorker or pthread, enabling rendering on background threads. |
| Memory model flexibility | Works with Emscripten’s 2 GB, 4 GB, and Wasm64 memory configurations, so you can target large data sets if needed. |
| Dawn backend (experimental) | A separate source file (lib_webgpu_dawn.cpp) lets you compile against the Dawn implementation of WebGPU when you’re not in a browser, though it’s not the primary focus. |
Who might use it
- Game or graphics engine developers who want to keep their core rendering code in C/C++ and ship it to the web without rewriting everything in JavaScript or WebGL.
- Research codebases that already use GPU compute in native C++ and need a quick path to run in a browser for demos or reproducibility.
- Tooling authors building WebGPU‑based visualizers, editors, or test suites that benefit from the synchronous API and minimal code‑size footprint.
Getting started (quick recap)
- Install Emscripten.
- Clone the repo, copy the
lib/files into your project, and add the library to the Emscripten link step (--js-library …). - Use the provided CMakeLists in
samples/as a template. - Build with
emcmake cmake … && ninja, then serve the generated HTML withemrun ..
Limitations / caveats
- The library mirrors the current WebGPU spec exactly; breaking changes can appear when the spec evolves, and there is no ABI stability guarantee.
- The Dawn backend is outdated and not actively maintained.
- It is a binding layer only—you still need to write the actual rendering logic; the supplied samples are intentionally simple and not flashy.
- Using JSPI can introduce unexpected event‑loop interactions, so profiling is recommended for production‑grade code.
Bottom line
wasm_webgpu is a genuine, low‑level bridge that makes WebGPU accessible from C/C++ compiled to WebAssembly. It is aimed at developers who care about performance, code‑size, and staying close to the official WebGPU specification, rather than those looking for a high‑level graphics engine.
Related
- Project
- Project
- Project
- Project
- Project