oneshader for Go
A free, open-source HLSL shader compiler for Go, MIT or Apache-2.0. It compiles modern HLSL vertex, pixel and compute shaders to SPIR-V, Metal Shading Language, WGSL and DXIL, each with reflection.
Pure Go: no cgo and no dependencies. go generate turns your .hlsl files into Go structs laid out like the shader’s buffers, binding values and embedded compiled code.
Compile a shader
The README’s example as a whole program, run with Go 1.26.8 against v1.0.0 from the module proxy. One Compile call gives one blob per entry point and target.
RWStructuredBuffer<float> Values : register(u0); [shader("compute")] [numthreads(64, 1, 1)] void main(uint3 id : SV_DispatchThreadID) { Values[id.x] *= 2.0; }
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 mod init example.com/double go get recursiveai.org/go/oneshader@v1.0.0 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; }
Compile time
From the README at v1.0.0: one entry point to SPIR-V per process, median of 15 rounds, on an AMD Ryzen 9 7950X3D with Go 1.26.8 on Linux, one run on December 4, 2026.
Method and machine Every row“Compile time against DXC 1.9.2609, the reference compiler, run as a native process, on a corpus of 106 entry points (105 compile; see below): 29 from a 2D renderer, 26 from a 3D PBR set, 24 compute kernels, 26 language-feature and type probes. Both tools compile one entry point to SPIR-V for Vulkan 1.2 at their default optimization, one process per entry point, with the blob written to disk. Every output passes
spirv-val --target-env vulkan1.2.”
Group Time vs DXC (geometric mean) 2D renderer 3.02× faster 3D (PBR set) 2.94× faster compute 2.89× faster language features and types 2.81× faster All 2.92× faster “‘In process’ is
Compilecalled from Go (go test -bench CorpusSPIRV -benchmem, median of 5 runs of 100 iterations), the cost a build tool pays per shader without a process start: the median is 0.47 ms and 3,774 allocations.”
What it does
- Version
oneshader v1.0.0. Go 1.24 or newer.
- HLSL
Modern HLSL (Shader Model 6 style) for vertex, pixel and compute stages: structs, functions and overloads,
cbufferandConstantBuffer<T>, textures and samplers, structured and byte-address buffers,groupsharedand barriers, wave and quad intrinsics, cooperative matrices, templates as HLSL 2021 allows, inheritance, bindless resources, 16-bit, 64-bit andhalftypes, bit-fields and the preprocessor. - Targets
SPIR-V for Vulkan 1.2 or 1.3 (the default); Metal Shading Language 2.4 and later; WGSL, where what WebGPU can’t express is a diagnostic or a warning; DXIL containers for shader model 6.0 to 6.8, one per entry point. All four come from one IR with a verifier and an optimizer.
- Reflection
Every compile reports entry points, stage variables, resource slots per target, buffer layouts with byte offsets, push constants, specialization constants and required device features, with a JSON form and a reader.
Compatiblechecks a recompiled shader against a pipeline layout. - Go bindings
oneshader gen, run from//go:generate, writes Go structs laid out to the shader’s byte layout with compile-time size and offset checks, per-resource binding values, shader variants and the compiled blobs embedded with//go:embed. A shader error fails it withfile:line:col. - Command line
Without
gen,oneshaderis “a DXC-compatible compiler”:oneshader -T cs_6_6 -E CSMain -spirv -Fo out.spv file.hlsl, with--target spirv|msl|wgsl|dxil|irand--reflect file.json. - Packages
Compileis built on packages a tool can use on their own:preprocessandsyntax,layout,irandir/opt, thespirv,msl,wgslanddxilbackends, andpipelineandshaderfor a program that only loads compiled shaders. - Diagnostics
Errors read
file(line,col): error OSHnnnn: message, with a stable code and what to do about it. - Untrusted source
Include depth and count, macro expansion, nesting, output tokens, kept errors and lowered IR size are bounded, each with its own diagnostic and set with
WithLimits.CompileContextends a compile early. No library function panics on input. - Output
The same input and options give byte-identical blobs, reflection and generated code.
Compilekeeps no state between calls and may run on many goroutines at once; aCachekeeps compiles for hot reload. - Dependencies
None, and no cgo: every package builds with
CGO_ENABLED=0. The module’sgo.modrequires nothing. - License
MIT or Apache-2.0, at your choice.