Skip to content

About

WebGPU for MoonBit, on a native GPU backend and in the browser, with one typed API.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

opraying/webgpu

WebGPU for MoonBit, on native and js. The typed API is the same object model on a native GPU backend and in the browser: a device, a buffer, a texture, and a pass.

Tests and headless tools use an in-process fake device. They do not need a GPU.

Scripts

./scripts/webgpu-build.sh is the entry point: generate, check, and test. ./scripts/ci.sh is the full gate. ./scripts/new-app.sh scaffolds an example. Commands and invariants are in AGENTS.md.

Design

The API follows the W3C WebGPU specification. Native execution links wgpu-native through a generated C layer. The browser execution uses navigator.gpu through opraying/js. Header pins live in WebGPUWgpuFfi/spec/PIN.json.

Example

examples/hello creates a buffer on the fake device, writes a byte, and reads it back:

fn main {
  let fake = @WebGPU.FakeExecutor::new()
  let ctx = @WebGPU.GpuContext::new(fake)
  let device = ctx.device()
  let buf = device.create_buffer(
    size=8UL,
    usage=@WebGPU.BUFFER_USAGE_STORAGE | @WebGPU.BUFFER_USAGE_COPY_SRC | @WebGPU.BUFFER_USAGE_COPY_DST,
    label="hello-buf",
  ) catch { @WebGPU.GpuError(msg) => abort(msg) }
  buf.write(b"\x2a\x00\x00\x00\x00\x00\x00\x00") catch {
    @WebGPU.GpuError(msg) => abort(msg)
  }
  let bytes = buf.read() catch { @WebGPU.GpuError(msg) => abort(msg) }
  println(bytes[0].to_int())
}

Replace FakeExecutor with the native or browser executor when a real device is present. Run the headless smoke example with moon run examples/hello, launch the visible Apple live GPU showcase windows (macOS, iOS, visionOS, tvOS, watchOS Simulator) with ./examples/apple_window/run.sh --all-apple, or launch the Android Vulkan showcase with ./examples/android_window/run.sh --run.

Backends

WebGPU is the shared IR and the typed API. WebGPUWgpuFfi and WebGPUNative are the native backend, over Metal, Vulkan, or Direct3D. WebGPUBrowser is js only.

License

Apache-2.0. Copyright the opraying contributors.

About

WebGPU for MoonBit, on a native GPU backend and in the browser, with one typed API.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages