tycho_webgpu

    Pure-MoonBit WebGPU bindings over the standard webgpu.h C ABI (runtime-loaded wgpu-native / Dawn, zero link flags)

    webgpu
    wgpu
    wgsl
    gpu
    tycho
    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    1 hour ago
    Downloads
    3

    No dependencies

    #laplaceliu/tycho_webgpu

    Pure-MoonBit WebGPU bindings over the standard webgpu.h C ABI, loaded at run time from wgpu-native or Dawn. No import library, no cc-link-flags and no build-time SDK: the same invariant tycho_vulkan and tycho_dx12 keep.

    This is the third peer backend module of the Tycho RHI. It targets native first (preferred_target = "native") and deliberately sticks to the standard WebGPU subset, so the same shader artifacts and pipeline shapes can later be driven by navigator.gpu from MoonBit js / wasm-gc builds.

    #Scope (M0)

    Shipped and exercised by smoke/:

    • device creation (instance → adapter → device → queue), validation_errors()
    • WGSL shader modules and one graphics pipeline (auto bind group layout)
    • render targets, one clear-and-draw render pass, queue.submit
    • texture readback through a MAP_READ staging buffer with WebGPU's mandatory 256-byte row pitch, unpadded on the way out

    Not in M0 (the RHI integration milestone adds them): surfaces/windows, buffers, bind groups, depth/stencil, MSAA, MRT, compute, queries, indirect draws.

    #Requirements

    A native WebGPU implementation must be loadable at run time:

    # Debian/Ubuntu layout from a wgpu-native release export TYCHO_WGPU_LIB=$PWD/libwgpu_native.so moon run laplaceliu/tycho_webgpu/smoke

    Without TYCHO_WGPU_LIB the stub looks for libwgpu_native.so / libwgpu_native.dylib / wgpu_native.dll, then the Dawn equivalents. When nothing is found, available() returns false and smoke prints SKIP instead of failing — CI hosts without an implementation stay green.

    The vendored headers under include/webgpu/ are pinned to wgpu-native v29.0.1.1; see include/README.md before upgrading either file.

    #Synchronous shape

    WebGPU's only asynchronous boundaries are adapter/device requests and buffer mapping. wgpu_stub.c turns each into a blocking call: the request is issued with WGPUCallbackMode_AllowProcessEvents and the stub pumps wgpuInstanceProcessEvents until the callback has run, so the MoonBit surface is plain return values — the shape the RHI's synchronous command API needs.

    wgpuInstanceWaitAny would be the tidier primitive, but wgpu-native v29 does not implement it (src/unimplemented.rs panics), while wgpuInstanceProcessEvents is implemented by both wgpu-native and Dawn. The pump loop is therefore the portable choice rather than a workaround.

    #Layout

    PathRole
    types/target-agnostic WebGPU enums, usage bitflags, descriptors, and WGSL word helpers (native + js)
    native/native webgpu.h C ABI bindings (native.mbt + wgpu_stub.c, supported_targets = "native")
    js/typed browser navigator.gpu bindings (supported_targets = "js")
    webgpu.mbtbackward-compatible native facade re-exporting types/ and native/
    include/webgpu/vendored webgpu.h + wgpu.h
    smoke/headless triangle: pixel gate + validation_errors() == 0

    AddressMode

    WebGPU has no border address mode; callers decide whether to reject ClampToBorder or clamp to edge.

    BufferUsage

    Composable buffer usage flags; bits() is the contract with wgpu_stub.c, while js_bits() produces standard WebGPU GPUBufferUsage bitmasks.

    IndexFormat

    Index buffer element width.

    TextureFormat

    TextureFormat ordinals. The integer code() values match the encoding wgpu_stub.c maps to WGPUTextureFormat; wgpu_name() yields the exact WebGPU string token used by browser navigator.gpu.

    TextureUsage

    Composable texture usage flags; bits() is the contract with wgpu_stub.c, while js_bits() produces standard WebGPU GPUTextureUsage bitmasks.

    VertexFormat

    Vertex attribute formats. The ordinals match wgpu_stub.c; wgpu_name() matches the WebGPU JS specification.

    WgpuError

    Errors reported by the WebGPU binding layer.

    available

    fn available() -> Bool

    is_spirv_words

    fn is_spirv_words(words : FixedArray[UInt]) -> Bool

    Returns true when words starts with the SPIR-V magic number 0x07230203.

    last_error

    fn last_error() -> String

    wgsl_from_words

    fn wgsl_from_words(words : FixedArray[UInt]) -> String

    Unpacks a little-endian zero-padded FixedArray[UInt32] WGSL blob back to UTF-8 text. Works identically on both native and js targets.

    wgsl_words

    fn wgsl_words(source : String) -> FixedArray[UInt]

    Packs WGSL UTF-8 source text into a FixedArray[UInt32] blob (little-endian words, zero-padded to a 4-byte boundary) so WGSL rides the same shader word channel as SPIR-V and DXIL.

    Source Files