wgpu

module
v0.17.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2026 License: MIT

README

wgpu

Pure Go WebGPU Implementation
No Rust. No CGO. Just Go.

CI codecov Go Reference Go Report Card License Latest Release Go Version Zero CGO

Part of the GoGPU ecosystem


Overview

wgpu is a complete WebGPU implementation written entirely in Go. It provides direct GPU access through multiple hardware abstraction layer (HAL) backends without requiring Rust, CGO, or any external dependencies.

Key Features

Category Capabilities
Backends Vulkan, Metal, DirectX 12, OpenGL ES, Software
Platforms Windows, Linux, macOS, iOS
API WebGPU-compliant (W3C specification)
Shaders WGSL via gogpu/naga compiler
Compute Full compute shader support, GPU→CPU readback
Debug Leak detection, error scopes, validation layers, structured logging (log/slog)
Build Zero CGO, simple go build

Installation

go get github.com/gogpu/wgpu

Requirements: Go 1.25+

Build:

CGO_ENABLED=0 go build

Note: wgpu uses Pure Go FFI via cgo_import_dynamic, which requires CGO_ENABLED=0. This enables zero C compiler dependency and easy cross-compilation.


Quick Start

package main

import (
    "fmt"

    "github.com/gogpu/wgpu/core"
    types "github.com/gogpu/gputypes"
    _ "github.com/gogpu/wgpu/hal/allbackends" // Auto-register platform backends
)

func main() {
    // Create instance with platform-appropriate backends
    instance := core.NewInstance(&types.InstanceDescriptor{
        Backends: types.BackendsAll,
    })

    // Request high-performance GPU
    adapterID, _ := instance.RequestAdapter(&types.RequestAdapterOptions{
        PowerPreference: types.PowerPreferenceHighPerformance,
    })

    // Get adapter info
    info, _ := core.GetAdapterInfo(adapterID)
    fmt.Printf("GPU: %s (%s)\n", info.Name, info.Backend)

    // Create device
    deviceID, _ := core.RequestDevice(adapterID, &types.DeviceDescriptor{
        Label: "My Device",
    })

    // Get queue for command submission
    queueID, _ := core.GetDeviceQueue(deviceID)
    _ = queueID // Ready for rendering
}

Compute Shaders

// Create compute pipeline
pipelineID, _ := core.DeviceCreateComputePipeline(deviceID, &core.ComputePipelineDescriptor{
    Label:  "Compute Pipeline",
    Layout: layoutID,
    Compute: core.ProgrammableStage{
        Module:     shaderModuleID,
        EntryPoint: "main",
    },
})

// Begin compute pass
encoderID, _ := core.DeviceCreateCommandEncoder(deviceID, "Compute Encoder")
computePass := encoder.BeginComputePass(nil)

// Dispatch workgroups
computePass.SetPipeline(pipelineID)
computePass.SetBindGroup(0, bindGroupID, nil)
computePass.Dispatch(64, 1, 1)
computePass.End()

// Submit commands
cmdBufID, _ := core.CommandEncoderFinish(encoderID)
core.QueueSubmit(queueID, []core.CommandBufferID{cmdBufID})

Guides: Getting Started | Backend Differences

Features: WGSL compute shaders, storage/uniform buffers, indirect dispatch, GPU timestamp queries (Vulkan), GPU-to-CPU readback.


Architecture

wgpu/
├── core/               # Validation, state tracking, resource management
├── hal/                # Hardware Abstraction Layer
│   ├── allbackends/    # Platform-specific backend auto-registration
│   ├── noop/           # No-op backend (testing)
│   ├── software/       # CPU software rasterizer (~11K LOC)
│   ├── gles/           # OpenGL ES 3.0+ (~10K LOC)
│   ├── vulkan/         # Vulkan 1.3 (~38K LOC)
│   ├── metal/          # Metal (~5K LOC)
│   └── dx12/           # DirectX 12 (~14K LOC)
├── examples/
│   ├── compute-copy/   # GPU buffer copy with compute shader
│   └── compute-sum/    # Parallel reduction on GPU
└── cmd/
    ├── vk-gen/         # Vulkan bindings generator
    └── ...             # Backend integration tests

HAL Backend Integration

The HAL Backend Integration layer provides unified multi-backend support:

import _ "github.com/gogpu/wgpu/hal/allbackends"

// Platform-specific backends auto-registered:
// - Windows: Vulkan, DX12, GLES
// - Linux:   Vulkan, GLES
// - macOS:   Metal, Vulkan

Backend Details

Platform Support

Backend Windows Linux macOS iOS Notes
Vulkan Yes Yes Yes - MoltenVK on macOS
Metal - - Yes Yes Native Apple GPU
DX12 Yes - - - Windows 10+
GLES Yes Yes - - OpenGL ES 3.0+
Software Yes Yes Yes Yes CPU fallback

Vulkan Backend

Full Vulkan 1.3 implementation with:

  • Auto-generated bindings from official vk.xml
  • Buddy allocator for GPU memory (O(log n), minimal fragmentation)
  • Dynamic rendering (VK_KHR_dynamic_rendering)
  • Classic render pass fallback for Intel compatibility
  • wgpu-style swapchain synchronization
  • MSAA render pass with automatic resolve
  • Complete resource management (Buffer, Texture, Pipeline, BindGroup)
  • Surface creation: Win32, X11, Wayland, Metal (MoltenVK)
  • Debug messenger for validation layer error capture (VK_EXT_debug_utils)
  • Structured diagnostic logging via log/slog

Metal Backend

Native Apple GPU access via:

  • Pure Go Objective-C bridge (goffi)
  • Metal API via runtime message dispatch
  • CAMetalLayer integration for surface presentation
  • MSL shader compilation via naga

DirectX 12 Backend

Windows GPU access via:

  • Pure Go COM bindings (syscall, no CGO)
  • DXGI integration for swapchain and adapters
  • Flip model with VRR support
  • Descriptor heap management
  • WGSL shader compilation (WGSL → HLSL via naga → DXBC via d3dcompiler_47.dll)
  • Staging buffer GPU data transfer (WriteBuffer, WriteTexture)

OpenGL ES Backend

Cross-platform GPU access via OpenGL ES 3.0+:

  • Pure Go EGL/GL bindings (goffi)
  • Full rendering pipeline: VAO, FBO, MSAA, blend, stencil, depth
  • WGSL shader compilation (WGSL → GLSL via naga)
  • CopyTextureToBuffer readback for GPU → CPU data transfer
  • Platform detection: X11, Wayland, Surfaceless (headless CI)
  • Works with Mesa llvmpipe for software-only environments

Software Backend

Full-featured CPU rasterizer for headless rendering. Always compiled — no build tags or GPU hardware required.

// Software backend auto-registers via init().
// No explicit import needed when using hal/allbackends.
// For standalone usage:
import _ "github.com/gogpu/wgpu/hal/software"

// Use cases:
// - CI/CD testing without GPU
// - Server-side image generation
// - Reference implementation
// - Fallback when GPU unavailable
// - Embedded systems without GPU

Rasterization Features:

  • Edge function triangle rasterization (Pineda algorithm)
  • Perspective-correct interpolation
  • Depth buffer (8 compare functions)
  • Stencil buffer (8 operations)
  • Blending (13 factors, 5 operations)
  • 6-plane frustum clipping (Sutherland-Hodgman)
  • 8x8 tile-based parallel rendering

Ecosystem

wgpu is the foundation of the GoGPU ecosystem.

Project Description
gogpu/gogpu GPU framework with windowing and input
gogpu/wgpu Pure Go WebGPU (this repo)
gogpu/naga Shader compiler (WGSL to SPIR-V, HLSL, MSL, GLSL)
gogpu/gg 2D graphics library
go-webgpu/webgpu wgpu-native FFI bindings
go-webgpu/goffi Pure Go FFI library

Documentation


References


Contributing

Contributions welcome! See CONTRIBUTING.md for guidelines.

Priority areas:

  • Cross-platform testing
  • Performance benchmarks
  • Documentation improvements
  • Bug reports and fixes

License

MIT License — see LICENSE for details.


wgpu — WebGPU in Pure Go

Directories

Path Synopsis
cmd
dx12-test command
Command dx12-test is an integration test for the DX12 backend.
Command dx12-test is an integration test for the DX12 backend.
gles-test command
Command gles-test is an integration test for the Pure Go GLES backend.
Command gles-test is an integration test for the Pure Go GLES backend.
vk-gen command
Command vk-gen generates Pure Go Vulkan bindings from vk.xml specification.
Command vk-gen generates Pure Go Vulkan bindings from vk.xml specification.
vk-test command
Command vk-test is an integration test for the Pure Go Vulkan backend.
Command vk-test is an integration test for the Pure Go Vulkan backend.
vulkan-renderpass-test command
Command vulkan-renderpass-test validates the hypothesis that Intel Iris Xe driver bug is specific to VK_KHR_dynamic_rendering, not vkCreateGraphicsPipelines.
Command vulkan-renderpass-test validates the hypothesis that Intel Iris Xe driver bug is specific to VK_KHR_dynamic_rendering, not vkCreateGraphicsPipelines.
vulkan-triangle command
Command vulkan-triangle is a full integration test for the Pure Go Vulkan backend.
Command vulkan-triangle is a full integration test for the Pure Go Vulkan backend.
Package core provides validation and state management for WebGPU resources.
Package core provides validation and state management for WebGPU resources.
track
Package track provides resource state tracking infrastructure.
Package track provides resource state tracking infrastructure.
examples
compute-copy command
Command compute-copy demonstrates GPU buffer copying via a compute shader.
Command compute-copy demonstrates GPU buffer copying via a compute shader.
compute-sum command
Command compute-sum demonstrates a parallel reduction (sum) using a GPU compute shader.
Command compute-sum demonstrates a parallel reduction (sum) using a GPU compute shader.
hal
Package hal provides the Hardware Abstraction Layer for WebGPU implementations.
Package hal provides the Hardware Abstraction Layer for WebGPU implementations.
allbackends
Package allbackends imports all HAL backend implementations.
Package allbackends imports all HAL backend implementations.
dx12
Package dx12 provides a DirectX 12 backend for the HAL.
Package dx12 provides a DirectX 12 backend for the HAL.
dx12/d3d12
Package d3d12 provides low-level Direct3D 12 COM bindings for Windows.
Package d3d12 provides low-level Direct3D 12 COM bindings for Windows.
dx12/d3dcompile
Package d3dcompile provides Pure Go bindings to d3dcompiler_47.dll.
Package d3dcompile provides Pure Go bindings to d3dcompiler_47.dll.
dx12/dxgi
Package dxgi provides low-level DXGI (DirectX Graphics Infrastructure) COM bindings for Windows.
Package dxgi provides low-level DXGI (DirectX Graphics Infrastructure) COM bindings for Windows.
gles
Package gles implements the HAL backend for OpenGL ES 3.0 / OpenGL 3.3+.
Package gles implements the HAL backend for OpenGL ES 3.0 / OpenGL 3.3+.
gles/egl
Package egl provides EGL (EGL) context management for OpenGL ES on Linux.
Package egl provides EGL (EGL) context management for OpenGL ES on Linux.
gles/gl
Package gl provides OpenGL constants and types for the GLES backend.
Package gl provides OpenGL constants and types for the GLES backend.
gles/wgl
Package wgl provides Windows OpenGL (WGL) context management.
Package wgl provides Windows OpenGL (WGL) context management.
metal
Package metal provides a Metal backend for the HAL.
Package metal provides a Metal backend for the HAL.
noop
Package noop provides a no-operation GPU backend.
Package noop provides a no-operation GPU backend.
software
Package software provides a CPU-based software rendering backend.
Package software provides a CPU-based software rendering backend.
software/raster
Package raster provides CPU-based triangle rasterization for the software backend.
Package raster provides CPU-based triangle rasterization for the software backend.
software/shader
Package shader provides callback-based shader execution for the software backend.
Package shader provides callback-based shader execution for the software backend.
vulkan
Package vulkan provides Pure Go Vulkan backend for the HAL.
Package vulkan provides Pure Go Vulkan backend for the HAL.
vulkan/memory
Package memory provides GPU memory allocation for Vulkan backend.
Package memory provides GPU memory allocation for Vulkan backend.
vulkan/vk
Package vk provides Pure Go Vulkan bindings using goffi for FFI calls.
Package vk provides Pure Go Vulkan bindings using goffi for FFI calls.
internal
thread
Package thread provides thread abstraction for GPU operations.
Package thread provides thread abstraction for GPU operations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL