Skip to content

Repository files navigation

go-webgpu

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

GitHub ReleaseGo VersionGo ReferenceGitHub ActionsCodecovGo Report CardLicenseGitHub StarsGitHub Issues

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

Status

Beta — Comprehensive API ready for testing and feedback.

FeatureStatus
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:

# Linuxexport WGPU_NATIVE_PATH=./lib/libwgpu_native.so
# macOSexport 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/macOSset 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 packageconfig.Format=wgpu.TextureFormatBGRA8Unormbuffer.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"
)
funcmain() {
// Initialize libraryiferr:=wgpu.Init(); err!=nil {
log.Fatal(err)
}
// Create WebGPU instanceinstance, err:=wgpu.CreateInstance(nil)
iferr!=nil {
log.Fatal(err)
}
deferinstance.Release()
// Request GPU adapteradapter, err:=instance.RequestAdapter(nil)
iferr!=nil {
log.Fatal(err)
}
deferadapter.Release()
fmt.Printf("Adapter: %#x\n", adapter.Handle())
}

Examples

ExampleDescription
triangleBasic triangle rendering
colored-triangleVertex colors and buffers
textured-quadTextures, samplers, index buffers
rotating-triangleUniform buffers, animation
cube3D rendering with depth buffer
instancedInstanced rendering (25 objects in 1 draw call)
computeCompute shader parallel processing
indirectGPU-driven rendering (DrawIndirect)
render_bundlePre-recorded draw commands
timestamp_queryGPU profiling with timestamps
mrtMultiple Render Targets
error_handlingError 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

ProjectDescription
gogpu/wgpuPure Go WebGPU implementation
gogpu/nagaPure Go shader compiler (WGSL/SPIR-V)
gogpu/gogpuHigh-level GPU compute framework
gogpu/ggPure 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

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages