---
title: "oneshader-go"
url: https://recursiveai.org/oneshader/go/docs/overview/
description: "The module README: what oneshader for Go is, compiling a shader, the two API layers, generated Go bindings, targets, limits on untrusted source and compile time."
version: "1.0.0"
source: https://github.com/recursive-ai-oss/oneshader-go/blob/d03e376789b814cdbbb7c5c2fbde431e763156db/README.md
---

# oneshader-go

A pure-Go compiler for modern HLSL (Shader Model 6 style): vertex, pixel and compute shaders in; SPIR-V, Metal Shading Language, WGSL and DXIL out, each with reflection, and Go bindings generated from the shaders. Module `recursiveai.org/go/oneshader`. No cgo and no dependencies: every package builds with `CGO_ENABLED=0` on Go 1.24 or later.

Status: 1.0.0.

## Install

```
go get recursiveai.org/go/oneshader
go install recursiveai.org/go/oneshader/cmd/oneshader@latest
```

## Compile a shader

```go
res, err := oneshader.Compile(src,
	oneshader.WithName("sprite.hlsl"),
	oneshader.WithTargets(oneshader.TargetSPIRV, oneshader.TargetMSL))
if err != nil {
	for _, d := range res.Errors() {
		fmt.Println(d) // sprite.hlsl(12,5): error OSH0301: ... what to do about it
	}
	return err // a *DiagnosticsError when the source has errors
}
for _, b := range res.Blobs { // one per entry point and target, in source order
	use(b.Entry, b.Stage, b.Target, b.Data)
}
refl := res.Reflection // resources, push constants, stage variables, byte layouts
```

Entry points are found by `[shader("vertex"|"pixel"|"compute")]`, or named with `WithEntryPoints`. `CompileFile` and `CompileFileContext` read a file and resolve `#include` from its folder; `WithIncludeFS`, `WithIncludeDirs` and `WithIncludeResolver` say where includes come from. A `Cache` keeps compiles for hot reload.

## Two API layers

**The happy path** is `Compile` with its options, `Result` and `Reflection`, and the `oneshader` command below. It is built on the low level, not beside it.

**The low level** is every stage as its own package, so a tool can start or stop anywhere:

- `preprocess`, `syntax`: the preprocessor with its include resolvers, the lexer, parser and syntax tree.
- `layout`: buffer layouts, DXC's Vulkan defaults, one byte layout per buffer on every target.
- `ir`: the shader IR (builder, verifier, text and binary forms); `ir/opt`: its optimizer.
- `spirv`, `msl`, `wgsl`, `dxil`: the backends, each taking an `ir.Module`.
- `pipeline`, `shader`: compiled shaders as a GPU layer consumes them: descriptor set layouts, Metal argument indices, bind group layouts, vertex input. A program that only loads compiled shaders imports these and never the compiler.
- `diag`: diagnostics and their stable `OSHnnnn` codes. `gen`, `cmd/oneshader`: binding generation and the command.

## Generated Go bindings

The `oneshader` command, run from `//go:generate`, compiles `*.hlsl` files at build time. It fails with `file:line:col` diagnostics, and writes Go structs laid out to the shaders' byte layout (with compile-time size and offset checks), per-resource binding values, and the compiled blobs embedded with `//go:embed`. Output is deterministic.

```go
//go:generate go run recursiveai.org/go/oneshader/cmd/oneshader gen -targets spirv,msl,wgsl sprite.hlsl
```

`oneshader gen -h` lists the flags: `-pkg`, `-targets`, `-entries`, `-D`, `-I`, `-16bit`, `-optimize`, `-layout-checks compile|test|none` (`test` keeps `unsafe` out of the generated package), `-debug`, `-axes` and `-variant` for shader variants, `-o`. A file argument may be a pattern such as `shaders/**/*.hlsl`. `examples/particles` shows the generated package.

Layout checks: by default (`-layout-checks compile`) `gen` writes `<name>_shader_layout.go`, a file of constant assertions on `unsafe.Sizeof` and `unsafe.Offsetof` for every generated struct and field. They use no pointers and cost nothing at run time; a Go type whose size or field offsets differ from the shader's buffer layout fails `go build`. That file is why the generated package imports `unsafe`. With `-layout-checks test`, `gen` writes `<name>_shader_test.go` instead, a test that checks the same sizes and offsets through `reflect`, so the package imports no `unsafe` and a mismatch is caught by `go test`; `-layout-checks none` writes no checks.

Without `gen`, the command is a DXC-compatible compiler: `oneshader -T cs_6_6 -E CSMain -spirv -Fo out.spv file.hlsl`, with `--target spirv|msl|wgsl|dxil|ir` and `--reflect file.json`.

## Targets

| Target | Output | Notes |
|---|---|---|
| `TargetSPIRV` | SPIR-V for Vulkan 1.2 or 1.3 | the default |
| `TargetMSL` | Metal Shading Language 2.4 and later (3.1 for bfloat and some atomics) | |
| `TargetWGSL` | WGSL | what WebGPU cannot express (binding arrays, `precise`, clip distances) is a diagnostic or a warning |
| `TargetDXIL` | DXIL containers, shader model 6.0 to 6.8 | one container per entry point; the DXC validator (`dxv`) accepts them |

Every blob, and the reflection beside it, is byte-identical for the same input and options.

## Limits

HLSL source is untrusted. `WithLimits` bounds include depth (default 64) and count (4096), macro expansion (5,000,000 steps, nesting 128), output tokens (2,000,000), parser nesting (256), kept errors (100) and lowered IR size (500,000 instructions); each limit has its own diagnostic and no input can recurse or grow without one. `CompileContext` ends a compile early. No library function panics on input. Scope: vertex, pixel and compute stages; geometry, tessellation, mesh and ray-tracing stages, GLSL and Direct3D 9 syntax are not supported.

## Performance

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 | Entry points | Process: DXC median | Process: ours median | Time vs DXC (geometric mean) | Fastest | Slowest | Slower than DXC | In process: median time | Allocations (median) |
|---|--:|--:|--:|---|---|---|--:|--:|--:|
| 2D renderer | 29 | 6.14 ms | 1.99 ms | 3.02× faster | 3.50× faster | 2.42× faster | 0 | 0.40 ms | 2,798 |
| 3D (PBR set) | 26 | 6.72 ms | 2.22 ms | 2.94× faster | 3.50× faster | 2.51× faster | 0 | 0.53 ms | 4,029 |
| compute | 24 | 5.99 ms | 2.09 ms | 2.89× faster | 3.32× faster | 2.72× faster | 0 | 0.39 ms | 3,888 |
| language features and types | 26 | 7.00 ms | 2.31 ms | 2.81× faster | 3.50× faster | 2.14× faster | 0 | 0.65 ms | 5,738 |
| All | 105 | 6.49 ms | 2.17 ms | 2.92× faster | 3.50× faster | 2.14× faster | 0 | 0.47 ms | 3,774 |

"Process" is wall-clock time from starting the process to its exit, the median of 15 rounds per entry point; each round runs DXC and `oneshader` back to back for every row, so drift in machine speed affects both alike. The ratio is DXC's time over ours per entry point, summarised by the geometric mean; a time within 5% of DXC's counts as a tie, and no row is slower. "In process" is `Compile` called 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. DXC's allocations are not measured. Per-row figures are in [`bench/RESULTS.md`](https://github.com/recursive-ai-oss/oneshader-go/blob/d03e376789b814cdbbb7c5c2fbde431e763156db/bench/RESULTS.md).

Where we lose or do not compete:

- **Output size.** The SPIR-V is 8.5% larger than DXC's in total (78,048 words against 71,946; larger on 97 of 105 rows), because names are kept by default; `WithKeepNames(false)` or `--no-names` drops them.
- **One row was left out.** `types/byte_address_wide.hlsl` (a struct through `ByteAddressBuffer.Load<T>`) was refused when this run was taken. It compiles now, on every target but WGSL (no 64-bit integers there); the table does not include it.
- Only SPIR-V is timed: MSL, WGSL and DXIL have no counterpart in DXC's SPIR-V path. DXIL against DXC's own `-T` output is not measured. There is no pure-Go shader compiler to compare with.

Method and machine. One publishable run on 2026-12-04, Go 1.26.8 on Linux 7, an AMD Ryzen 9 7950X3D (16 cores, 32 threads, frequency scaling on) with the machine to itself: the whole machine was reserved for the run and nothing else was started on it. Load average (1 minute) was 1.46 before the run, between 1.39 and 1.86 over the 10 samples taken every 5 seconds during it, and 1.81 afterwards; the 15-minute average was still falling from 7.4 as the machine had just been drained. The corpus is `bench/corpus` (our shaders, `MIT OR Apache-2.0`); the rows are `bench/entries.tsv`, the stage and entry point of each shader the corpus' execution cases compile. One run: treat the numbers as local measurements. To reproduce: `bench/node.sh` after putting `dxc`, its libraries and `spirv-val` under `.benchtools/` (see `bench/README.md`).

## Licence

MIT OR Apache-2.0, © Recursive AI, LLC.
