close
Skip to content

go-webgpu/webgpu

go-webgpu

Zero-CGO WebGPU bindings for Go — GPU-accelerated graphics and compute in pure Go

GitHub Release Go Version Go Reference GitHub Actions Codecov Go Report Card License GitHub Stars GitHub Issues

Pure Go WebGPU bindings using goffi + wgpu-native. No CGO required.

Status

Beta — Comprehensive API ready for testing and feedback.

Feature Status
Instance, Adapter, Device
Buffers (vertex, index, uniform, storage)
Buffer Mapping (Map with context, async MapPending, type-safe MappedRange)
Queue Submission Index Tracking
Textures, Samplers, Storage Textures
Region-Based Copy Operations (CopyTextureToBuffer)
Render Pipelines
Compute Pipelines
Depth Buffer
MRT (Multiple Render Targets)
Instanced Rendering
Indirect Drawing (GPU-driven)
RenderBundle (pre-recorded commands)
Cross-Platform Surface (Win/Linux/macOS)
Error Handling (error scopes)
QuerySet (GPU timestamps)
BindGroupLayout (explicit)
3D Math (Mat4, Vec3)

Requirements

  • Go 1.25+
  • wgpu-native v29.0.0.0 (download)

Installation

go get github.com/go-webgpu/webgpu

wgpu-native Setup (auto)

go run github.com/go-webgpu/webgpu/cmd/setup@latest

This downloads the correct wgpu-native v29 binary for your platform (Windows/macOS/Linux, amd64/arm64) into ./lib/.

The library is found automatically from ./lib/ — no environment variable needed. To override the search path explicitly:

# Linux
export WGPU_NATIVE_PATH=./lib/libwgpu_native.so

# macOS
export WGPU_NATIVE_PATH=./lib/libwgpu_native.dylib

# Windows (PowerShell)
$env:WGPU_NATIVE_PATH = "lib\wgpu_native.dll"

# Windows (cmd)
set WGPU_NATIVE_PATH=lib\wgpu_native.dll

Or copy the library to your project root — it will also be found automatically.

wgpu-native Setup (manual)

Download from gfx-rs/wgpu-native releases and place in your project directory or system PATH.

Custom library location:

export WGPU_NATIVE_PATH=/path/to/libwgpu_native.so      # Linux/macOS
set WGPU_NATIVE_PATH=C:\path\to\wgpu_native.dll          # Windows

gogpu/wgpu Integration

This library is the Rust FFI backend for gogpu/wgpu — the unified Go WebGPU package. Build with -tags rust to use wgpu-native instead of the Pure Go implementation:

go build -tags rust ./myapp

Same API, same types, same user code — build tag selects the implementation.

Type System

WebGPU types from gputypes are re-exported as type aliases in the wgpu package. A single import is sufficient:

import "github.com/go-webgpu/webgpu/wgpu"

// gputypes constants available directly via wgpu package
config.Format = wgpu.TextureFormatBGRA8Unorm
buffer.Usage = wgpu.BufferUsageVertex | wgpu.BufferUsageCopyDst

If you use multiple gogpu ecosystem packages, importing gputypes directly also works and is fully compatible:

import (
    "github.com/go-webgpu/webgpu/wgpu"
    "github.com/gogpu/gputypes" // optional — for ecosystem interop
)

config.Format = gputypes.TextureFormatBGRA8Unorm // same underlying type

go-webgpu API is designed to be compatible with gogpu/wgpu, enabling future backend switching within the gogpu ecosystem.

Quick Start

package main

import (
    "fmt"
    "log"

    "github.com/go-webgpu/webgpu/wgpu"
)

func main() {
    // Initialize library
    if err := wgpu.Init(); err != nil {
        log.Fatal(err)
    }

    // Create WebGPU instance
    instance, err := wgpu.CreateInstance(nil)
    if err != nil {
        log.Fatal(err)
    }
    defer instance.Release()

    // Request GPU adapter
    adapter, err := instance.RequestAdapter(nil)
    if err != nil {
        log.Fatal(err)
    }
    defer adapter.Release()

    fmt.Printf("Adapter: %#x\n", adapter.Handle())
}

Examples

Example Description
triangle Basic triangle rendering
colored-triangle Vertex colors and buffers
textured-quad Textures, samplers, index buffers
rotating-triangle Uniform buffers, animation
cube 3D rendering with depth buffer
instanced Instanced rendering (25 objects in 1 draw call)
compute Compute shader parallel processing
indirect GPU-driven rendering (DrawIndirect)
render_bundle Pre-recorded draw commands
timestamp_query GPU profiling with timestamps
mrt Multiple Render Targets
error_handling Error scopes API

Run examples:

cd examples/triangle && go run .

Architecture

┌─────────────────────────────────────────┐
│            Your Go Application          │
│    (uses wgpu.TextureFormatBGRA8Unorm)  │
├─────────────────────────────────────────┤
│     go-webgpu/wgpu (this package)       │
│     - Go-idiomatic API                  │
│     - gputypes type aliases             │
│     - Error returns on all operations   │
├─────────────────────────────────────────┤
│     Internal FFI layer                  │
│     - Wire structs (C-layout)           │
│     - convert.go (enum translation)     │
│     - goffi (Pure Go FFI)               │
├─────────────────────────────────────────┤
│     wgpu-native v29 (Rust WebGPU)       │
├─────────────────────────────────────────┤
│     Vulkan / Metal / DX12 / OpenGL      │
└─────────────────────────────────────────┘

For a detailed explanation of the architecture, including why convert.go exists and the FFI wire struct contract, see docs/ARCHITECTURE.md.

Looking for Pure Go WebGPU?

This project uses FFI bindings to wgpu-native. If you're looking for a 100% Pure Go WebGPU implementation (no native dependencies), check out:

👉 github.com/gogpu — Pure Go GPU ecosystem

Project Description
gogpu/wgpu Pure Go WebGPU implementation
gogpu/naga Pure Go shader compiler (WGSL/SPIR-V)
gogpu/gogpu High-level GPU compute framework
gogpu/gg Pure Go graphics library

Dependencies

  • goffi — Pure Go FFI (callbacks, cross-platform loading)
  • gputypes — Shared WebGPU type definitions for the gogpu ecosystem
  • wgpu-native — Rust WebGPU implementation (runtime binary, not a Go dependency)
  • golang.org/x/sys — Platform-specific syscalls

License

MIT

Contributing

Contributions welcome! Please open an issue or PR.

About

Zero-CGO WebGPU bindings for Go — GPU-accelerated graphics and compute in pure Go

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors