Skip to content

About

wgpu (WebGPU/wgpu-native) backend for the labelle 2D engine — out-of-tree pluggable backend

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

labelle-wgpu

The wgpu (WebGPU via wgpu-native) rendering backend for the labelle 2D engine, as an out-of-tree pluggable backend (labelle-assembler#386).

On the desktop it has a loop-style lifecycle: GLFW supplies the native window for wgpu-native on macOS (Metal), Windows (Win32), and Linux (X11 or Wayland, whichever GLFW selects).

It also builds for the browser (wasm32-emscripten), rendering through the browser's WebGPU. See Web.

Use it

In a labelle project's project.labelle:

.backend = .wgpu,
.backend_package = .{ .name = "wgpu", .repo = "github.com/labelle-toolkit/labelle-wgpu", .version = "0.7.0" },

Requires Zig 0.17.1 (labelle CLI 5.0.0 or newer, engine 5.x). The Zig 0.16 line stays on the zig-0.16 branch (v0.3.0).

With labelle-assembler 0.130.0 or newer, .backend = .wgpu alone resolves to 0.6.0. Keep the .backend_package line to get 0.7.0 and the browser build.

Renders with 4x MSAA and vsync (fifo). Audio plays through miniaudio (WAV, OGG, MP3), and TTF/OTF fonts load through the font-atlas seam. setVsync(false) switches to immediate or mailbox when the surface offers one, and the project's fps caps the loop.

Gamepads

With .gamepad = .auto gamepads come from the shared windowless-SDL source (labelle-sdl-gamepad), the same one labelle-bgfx uses, so the build links SDL2: install it with brew install sdl2 on macOS or libsdl2-dev on Debian/Ubuntu; on Windows point LABELLE_SDL2_LIB at the directory holding SDL2.dll. .gamepad_hidapi = true also turns on SDL's raw-HID drivers (Switch-mode Nintendo and 8BitDo pads), at the cost of a few seconds' stall when such a pad connects on some platforms.

With .gamepad = .none (what an unset .gamepad means for wgpu from labelle-assembler 0.130.0 on, as for bgfx) no SDL is linked and gamepads are read through GLFW's gamepad mappings; a controller GLFW cannot map (for example a Switch Pro controller in Switch mode) then reads as released.

Building the package directly, the same switches are -Dgamepad_enabled=false and -Dgamepad_hidapi=true.

labelle run --screenshot=<path> [--after=<dur>] writes the frame at exactly <path>: PNG, or TGA/BMP when the path ends in .tga/.bmp. --headless hides the window (a display server is still needed), --uncapped turns vsync and the fps cap off, and --ticks=N exits after N frames. The demo in example/ takes the same LABELLE_SCREENSHOT_PATH / LABELLE_HEADLESS variables.

Layout

  • src/ — the four backend modules: gfx, window, input, audio
  • src/web/ — the browser pieces: wgpu.zig (WebGPU binding for emdawnwebgpu), input.zig (HTML5 input), platform.zig (canvas, clock, fullscreen)
  • backend.manifest.zon + build_fragments/ — drive the assembler's manifest-splice codegen (desktop)
  • backend.manifest.v2.zon + backend.hook.zig — the v2 manifest; its wasm platform entry and the hook's emcc link
  • templates/desktop.txt, templates/wasm.txt — the generated main() (desktop loop, browser frame callback)
  • example/ — a standalone wgpu demo (desktop and browser)

Build

zig build test          # host-target unit tests (WAV parser + module compile)
cd example && zig build  # the demo (macOS: links Metal/QuartzCore/Foundation)

On Linux, build with the GLFW and SDL2 dependencies listed in CI. Run zig build run from example/ in an X11 or Wayland session with a Vulkan driver available.

Web

In a project, build and serve the browser version through the labelle-web provider:

labelle build --platform=wasm
labelle run --platform=wasm

This needs emsdk 4.0.10 or newer, because Emscripten's emdawnwebgpu port first ships in 4.0.10. The web binding is laid out against 4.0.23, the version labelle-assembler pins from 0.130.0. labelle-web still defaults to 4.0.9, so until it moves, set the version in the project's labelle-web settings file (providers/web.json, mapped in project.labelle's .provider_config):

{ "schema_version": 1, "emsdk": { "version": "4.0.23" } }

The demo builds without the CLI:

cd example
EMSDK=/path/to/emsdk zig build -Dtarget=wasm32-emscripten   # zig-out/web/wgpu-demo.{html,js,wasm}
cd zig-out/web && python3 -m http.server 8000                 # open http://localhost:8000/wgpu-demo.html

It needs emcc 4.0.23, from EMSDK or from the lazy emsdk package once emsdk install latest has run in it. src/web/wgpu.zig is laid out against the emdawnwebgpu port that emcc 4.0.23 pins. With another emcc, check the binding before relying on it, because a different port header can link and then misbehave at run time: build the example with that emcc once (the link fetches the port into Emscripten's cache), then run zig build web-abi-check -Dtarget=wasm32-emscripten from the repository root with the same EMSDK (and EM_CACHE, if set). It also needs a browser with WebGPU (verified in Chrome on macOS). The first link downloads Emscripten's emdawnwebgpu port.

How it works:

  • WebGPU comes from Emscripten's emdawnwebgpu port (--use-port=emdawnwebgpu). wgpu_native_zig follows wgpu-native v25's webgpu.h, which has different enum values and struct layouts from the header emdawnwebgpu ships, so the web build binds that header in src/web/wgpu.zig instead. It has the same Zig API as wgpu_native_zig, so the renderer in src/window.zig is shared with the desktop. zig build web-abi-check -Dtarget=wasm32-emscripten (with EMSDK set) checks the binding against the C header.
  • The adapter and device are requested with callbacks, without ASYNCIFY. Frames before the device exists are dropped. The browser drives the frame through emscripten_set_main_loop, and the surface is the #canvas element.
  • Input comes from Emscripten HTML5 callbacks: mouse, wheel, keyboard by physical key, and the first touch point as mouse button 0. This matches labelle-bgfx's web input.
  • The C parts (stb_truetype, miniaudio, labelle-audio's decoders) compile against the emsdk sysroot: a valid EMSDK's, else the lazy emsdk package's (emsdk_source.zig, the same rule as labelle-bgfx).

What the browser build has:

Feature Web
TTF/OTF fonts (.fonts) Yes. stb_truetype is compiled with the sysroot.
Audio, WAV/OGG/MP3 (.audio_ogg) Yes, through miniaudio's Web Audio backend (a ScriptProcessorNode, so no threads or COOP/COEP headers). Browsers start it on the first click, touch or key press.
Screenshots (.screenshots) No. takeScreenshot is a no-op: the readback waits on a buffer map, and a browser frame can't block on that.
Gamepads (.gamepad_polling) No. The web input reports none, and SDL is never resolved for wasm.
Present modes fifo only.
pthread builds Untested.

The manifest's capabilities are one flat list for every platform, as in labelle-bgfx, so they describe the desktop build. The table lists what the browser lacks.

About

wgpu (WebGPU/wgpu-native) backend for the labelle 2D engine — out-of-tree pluggable backend

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages