Install oneshader for Go, compile an HLSL compute shader to SPIR-V, Metal Shading Language and WGSL from a Go program, read a compile error, generate Go bindings with go generate, and compile from the command line.
oneshader for Go is an HLSL compiler written in Go: modern HLSL (Shader Model 6 style) vertex, pixel and compute shaders in; SPIR-V, Metal Shading Language, WGSL and DXIL out, each with reflection. Every sample on this page was compiled and run with Go 1.26.8 against recursiveai.org/go/oneshader v1.0.0 from the Go module proxy; the output under each one is what it printed.
Install
oneshader needs Go 1.24 or newer.
mkdir double && cd double go mod init example.com/double go get recursiveai.org/go/oneshader@v1.0.0
The module has no dependencies (its go.mod requires nothing) and no cgo, so it builds with CGO_ENABLED=0. The license is MIT or Apache-2.0, at your choice.
Compile a shader
A compute shader that doubles every value in a buffer, double.hlsl:
RWStructuredBuffer<float> Values : register(u0); [shader("compute")] [numthreads(64, 1, 1)] void main(uint3 id : SV_DispatchThreadID) { Values[id.x] *= 2.0; }
main.go compiles it for three targets and prints the WGSL:
package main import ( "fmt" "log" "os" "recursiveai.org/go/oneshader" ) func main() { src, err := os.ReadFile("double.hlsl") if err != nil { log.Fatal(err) } res, err := oneshader.Compile(src, oneshader.WithName("double.hlsl"), oneshader.WithTargets( oneshader.TargetSPIRV, oneshader.TargetMSL, oneshader.TargetWGSL)) if err != nil { for _, d := range res.Errors() { fmt.Println(d) } os.Exit(1) } for _, b := range res.Blobs { fmt.Println(b.Entry, b.Stage, b.Target, len(b.Data), "bytes") } wgsl, _ := res.Find("main", oneshader.TargetWGSL) fmt.Print(string(wgsl.Data)) }
go run .main Compute spirv 644 bytes main Compute msl 635 bytes main Compute wgsl 336 bytes @group(0) @binding(0) var<storage, read_write> Values: array<u32>; @compute @workgroup_size(64, 1, 1) fn main(@builtin(global_invocation_id) a_main_id: vec3<u32>) { let v0 = a_main_id; let v1 = v0[0]; let v2 = bitcast<f32>(Values[(v1 * 4u) >> 2u]); let v3 = v2 * 2.0f; Values[(v1 * 4u) >> 2u] = bitcast<u32>(v3); return; }
The entry point is found by its [shader("compute")] attribute; WithEntryPoints names entry points instead. Each Blob holds one entry point for one target: a SPIR-V module, or MSL or WGSL source text. res.Reflection describes the shader’s resources, push constants, stage variables and byte layouts. TargetDXIL adds DXIL containers for Direct3D 12. The same source and options always give byte-identical output.
Read a compile error
Change the multiplier to a name the shader doesn’t declare, Values[id.x] *= scale;, and run again. When the source has errors, Compile returns a *DiagnosticsError and res.Errors() lists them:
double.hlsl(7,21): error OSH0300: 'scale' is not declared; declare it before this useEach diagnostic has a location, a stable OSHnnnn code and what to do about it. A limit on untrusted source (include depth, macro expansion, nesting, program size) is a diagnostic too; errors.Is(err, oneshader.ErrLimit) matches it, and WithLimits sets the bounds.
Go bindings with go generate
The oneshader gen command compiles *.hlsl at build time and writes Go next to it: structs laid out to the shader’s buffers, where each resource binds, and the compiled code embedded with //go:embed. In a new module (go mod init example.com/particles and the same go get), particles.hlsl:
struct Particle { float3 Position; float Life; float3 Velocity; }; cbuffer Params : register(b0) { float Dt; uint Count; }; RWStructuredBuffer<Particle> Particles : register(u1); [shader("compute")] [numthreads(64, 1, 1)] void Step(uint3 id : SV_DispatchThreadID) { if (id.x >= Count) return; Particle p = Particles[id.x]; p.Position += p.Velocity * Dt; p.Life -= Dt; Particles[id.x] = p; }
main.go runs the generator from //go:generate and prints what it wrote:
package main //go:generate go run recursiveai.org/go/oneshader/cmd/oneshader gen -pkg main -targets spirv,msl,wgsl particles.hlsl import ( "fmt" "recursiveai.org/go/oneshader/shader" ) func main() { fmt.Println("Params:", ParticlesParamsSize, "bytes") fmt.Println("Particle:", ParticlesParticleSize, "bytes") fmt.Println("Particles: Vulkan set", ParticlesParticlesBinding.Slots[shader.SPIRV].Set, "binding", ParticlesParticlesBinding.Slots[shader.SPIRV].Binding) fmt.Println("Step workgroup:", ParticlesStep.Workgroup) for t, blob := range ParticlesStep.Blobs { if len(blob) == 0 { continue // a target not passed to -targets } fmt.Printf("%v: %d bytes\n", shader.Target(t), len(blob)) } }
go generate ./... go run .
Params: 16 bytes Particle: 32 bytes Particles: Vulkan set 0 binding 1 Step workgroup: [64 1 1] spirv: 1868 bytes msl: 2379 bytes wgsl: 1590 bytes
go generate wrote particles_shader.go, particles_shader_layout.go, particles_reflection.json and one file per target: particles_Step.spv, particles_Step.metal and particles_Step.wgsl. The structs in particles_shader.go carry the padding the shader’s layout has (doc comments left out here):
type ParticlesParams struct { Dt float32 `hlsl:"Dt"` Count uint32 `hlsl:"Count"` _ [8]byte } type ParticlesParticle struct { Position [3]float32 `hlsl:"Position"` Life float32 `hlsl:"Life"` Velocity [3]float32 `hlsl:"Velocity"` _ [4]byte }
particles_shader_layout.go holds constant checks on every struct’s size and field offsets, so a Go type that doesn’t match the shader’s layout fails go build. They use unsafe.Sizeof and unsafe.Offsetof and cost nothing at run time. With -layout-checks test the generator writes the same checks as a test through reflect instead, and the package imports no unsafe.
An error in the shader stops go generate with the file, line and column:
particles.hlsl:24:20: OSH0300: 'Drag' is not declared; declare it before this use main.go:3: running "go": exit status 1
oneshader gen -h lists every flag, among them -D and -I for macros and include folders, -axes and -variant for shader variants, -debug for debug information in SPIR-V and MSL, and -o for the output folder. A file argument may be a pattern such as shaders/**/*.hlsl.
The command line
Without gen, oneshader is “a DXC-compatible compiler”:
go install recursiveai.org/go/oneshader/cmd/oneshader@v1.0.0 oneshader -T cs_6_6 -E main -spirv -Fo double.spv double.hlsl oneshader -T cs_6_6 -E main --target msl double.hlsl
The first command writes the 644-byte SPIR-V module to double.spv. The second prints the Metal Shading Language:
// OneShader: Metal Shading Language 2.4 or newer #include <metal_stdlib> using namespace metal; struct Values_buffer_mem { float data[1]; }; #ifdef ONESHADER_CHECK_LAYOUT static_assert(__builtin_offsetof(Values_buffer_mem, data) == 0, "Values_buffer_mem.data offset"); #endif kernel void main0( device Values_buffer_mem& Values [[buffer(0)]], uint3 main_id_in [[thread_position_in_grid]]) { thread uint3 main_id = uint3(main_id_in); uint3 v; uint v_1; float v_2; float v_3; v = main_id; v_1 = v.x; v_2 = Values.data[v_1]; v_3 = v_2 * 2.0f; Values.data[v_1] = v_3; return; }
--target takes spirv, msl, wgsl, dxil or ir, and --reflect file.json writes the reflection.
Next
- Overview: the module README, with the two API layers, targets and compile time.
- Changelog: what is in 1.0.0.
- Package reference on pkg.go.dev.