wgpu

module
v0.8.6 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: MIT

README

wgpu

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

CI codecov Go Reference Go Report Card License Go Version Zero CGO Stars Discussions

Part of the GoGPU ecosystem

Status: Active development. All 5 HAL backends ready (Vulkan, Metal, DX12, GLES, Software).


Vision

A complete WebGPU implementation in pure Go:

  • No wgpu-native dependency — Standalone Go library
  • Direct GPU access — Vulkan, Metal, DX12 backends
  • WebGPU compliant — Following the W3C specification
  • WASM compatible — Run in browsers via WebAssembly

Installation

go get github.com/gogpu/wgpu

Usage (Preview)

import (
    "github.com/gogpu/wgpu/core"
    "github.com/gogpu/wgpu/types"
)

// Create instance for GPU discovery
instance := core.NewInstance(&types.InstanceDescriptor{
    Backends: types.BackendsVulkan | types.BackendsMetal,
})

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

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

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

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

Compute Shaders (Preview)

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

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

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

Architecture

wgpu/
├── types/         # WebGPU type definitions ✓
├── core/          # Validation, state tracking ✓
├── hal/           # Hardware abstraction layer ✓
│   ├── noop/      # No-op backend (testing) ✓
│   ├── software/  # Software backend ✓ (Full rasterizer, ~10K LOC)
│   ├── gles/      # OpenGL ES backend ✓ (Pure Go, ~7500 LOC, Windows + Linux)
│   ├── vulkan/    # Vulkan backend ✓ (Pure Go, ~27K LOC)
│   │   ├── vk/        # Generated Vulkan bindings (~20K LOC)
│   │   └── memory/    # GPU memory allocator (~1.8K LOC)
│   ├── metal/     # Metal backend ✓ (Pure Go, ~3K LOC, macOS)
│   └── dx12/      # DirectX 12 backend ✓ (Pure Go, ~12K LOC, Windows)
└── cmd/
    ├── vk-gen/           # Vulkan bindings generator from vk.xml
    └── vulkan-triangle/  # Vulkan integration test (red triangle) ✓

Roadmap

See ROADMAP.md for detailed development milestones and version history.

Current Focus: Compute shader support across all backends.

Pure Go Approach

All backends implemented without CGO:

Backend Status Approach Platforms
Software Done Pure Go CPU rendering All (headless)
OpenGL ES Done goffi + WGL/EGL Windows, Linux
Vulkan Done goffi + vk-gen from vk.xml Windows, Linux, macOS
Metal Done goffi (Obj-C bridge) macOS, iOS
DX12 Done syscall + COM Windows

Software Backend

Full-featured CPU rasterizer for headless rendering:

# Build with software backend
go build -tags software ./...
import _ "github.com/gogpu/wgpu/hal/software"

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

// Key feature: read rendered pixels
surface.GetFramebuffer() // Returns []byte (RGBA8)

Rasterization Pipeline (hal/software/raster/):

  • Edge function (Pineda) triangle rasterization with top-left fill rule
  • Perspective-correct attribute interpolation
  • Depth buffer with 8 compare functions
  • Stencil buffer with 8 operations
  • 13 blend factors, 5 blend operations (WebGPU spec compliant)
  • 6-plane frustum clipping (Sutherland-Hodgman)
  • Backface culling (CW/CCW)
  • 8x8 tile-based rasterization for cache locality
  • Parallel rasterization with worker pool
  • Incremental edge evaluation (O(1) per pixel)

Shader System (hal/software/shader/):

  • Callback-based vertex/fragment shaders
  • Built-in shaders: SolidColor, VertexColor, Textured
  • Custom shader support via VertexShaderFunc / FragmentShaderFunc

Metrics: ~10K LOC, 100+ tests, 94% coverage

Vulkan Backend Features

  • Auto-generated bindings from official Vulkan XML specification
  • Memory allocator with buddy allocation (O(log n), minimal fragmentation)
  • Vulkan 1.3 dynamic rendering — No render pass objects needed
  • Swapchain management with automatic recreation
  • Semaphore synchronization for frame presentation
  • Complete HAL implementation:
    • Buffer, Texture, TextureView, Sampler
    • ShaderModule, BindGroupLayout, BindGroup
    • PipelineLayout, RenderPipeline, ComputePipeline
    • CommandEncoder, RenderPassEncoder, ComputePassEncoder
    • Fence synchronization, WriteTexture immediate upload
  • Comprehensive unit tests (93 tests, 2200+ LOC):
    • Conversion functions (formats, usage, blend modes)
    • Descriptor allocator logic
    • Resource structures
    • Memory allocator (buddy allocation)

Metal Backend Features

  • Pure Go Objective-C bridge via goffi
  • Metal API access via Objective-C runtime
  • Device and adapter enumeration
  • Command buffer and render encoder
  • Shader compilation (MSL via naga)
  • Texture and buffer management
  • Surface presentation (CAMetalLayer integration)
  • ~3K lines of code

DirectX 12 Backend Features

  • Pure Go COM bindings via syscall (no CGO!)
  • D3D12 API access via COM interface vtables
  • Intel GPU support (fixed COM calling convention)
  • DXGI integration for swapchain and adapter enumeration
  • Descriptor heap management (CBV/SRV/UAV, Sampler, RTV, DSV)
  • Flip model swapchain with tearing support (VRR)
  • Command list recording with resource barriers
  • Root signature and PSO creation
  • ~12K lines of code

Structure:

hal/dx12/
├── d3d12/      # D3D12 COM bindings (~4K LOC)
├── dxgi/       # DXGI bindings (~2K LOC)
├── instance.go # Backend, Instance, Surface
├── adapter.go  # Adapter enumeration
├── device.go   # Device, descriptor heaps
├── queue.go    # Command queue
├── surface.go  # Swapchain management
├── resource.go # Buffer, Texture, TextureView
├── command.go  # CommandEncoder, RenderPassEncoder
├── pipeline.go # RenderPipeline, ComputePipeline
└── convert.go  # Format conversion helpers

References

Project Description Purpose
gogpu/gogpu Graphics framework GPU abstraction, windowing, input
gogpu/naga Shader compiler WGSL → SPIR-V, MSL, GLSL
gogpu/gg 2D graphics Canvas API, scene graph, GPU text
gogpu/ui GUI toolkit Widgets, layouts, themes (planned)
go-webgpu/webgpu FFI bindings wgpu-native integration

Note: Always use the latest versions. Check each repository for current releases.

Contributing

We welcome contributions! See CONTRIBUTING.md for guidelines.

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-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.
hal
Package hal provides the Hardware Abstraction Layer for WebGPU implementations.
Package hal provides the Hardware Abstraction Layer for WebGPU 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/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.
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.
Package types defines WebGPU types that are backend-agnostic.
Package types defines WebGPU types that are backend-agnostic.