Documentation
¶
Overview ¶
Package naga provides a Pure Go shader compiler.
naga compiles WGSL (WebGPU Shading Language) source code to multiple output formats:
- SPIR-V — Binary format for Vulkan
- MSL — Metal Shading Language for macOS/iOS
- GLSL — OpenGL Shading Language for OpenGL 3.3+, ES 3.0+
- HLSL — High Level Shading Language for DirectX
The package provides a simple, high-level API for shader compilation as well as lower-level access to individual compilation stages.
Example usage (SPIR-V):
source := `
@vertex
fn main(@builtin(vertex_index) idx: u32) -> @builtin(position) vec4<f32> {
return vec4<f32>(0.0, 0.0, 0.0, 1.0);
}
`
spirv, err := naga.Compile(source)
if err != nil {
log.Fatal(err)
}
For MSL output, use the msl package:
module, _ := naga.Lower(ast) mslCode, info, err := msl.Compile(module, msl.DefaultOptions())
For GLSL output, use the glsl package:
module, _ := naga.Lower(ast) glslCode, info, err := glsl.Compile(module, glsl.DefaultOptions())
Index ¶
- func Compile(source string) ([]byte, error)
- func CompileWithOptions(source string, opts CompileOptions) ([]byte, error)
- func GenerateSPIRV(module *ir.Module, opts spirv.Options) ([]byte, error)
- func Lower(ast *wgsl.Module) (*ir.Module, error)
- func LowerWithSource(ast *wgsl.Module, source string) (*ir.Module, error)
- func Parse(source string) (*wgsl.Module, error)
- func Validate(module *ir.Module) ([]ir.ValidationError, error)
- type CompileOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compile ¶
Compile compiles WGSL source code to SPIR-V binary using default options.
This is the simplest way to compile a shader. For more control, use CompileWithOptions or the individual Parse/Lower/Generate functions.
func CompileWithOptions ¶
func CompileWithOptions(source string, opts CompileOptions) ([]byte, error)
CompileWithOptions compiles WGSL source code to SPIR-V binary with custom options.
The compilation pipeline is:
- Parse WGSL source to AST
- Lower AST to IR (intermediate representation)
- Validate IR (if enabled)
- Generate SPIR-V binary
func GenerateSPIRV ¶
GenerateSPIRV generates SPIR-V binary from IR module.
This is the final stage of compilation. The output is a binary blob that can be directly consumed by Vulkan or other SPIR-V consumers.
func Lower ¶
Lower converts WGSL AST to IR (Intermediate Representation).
The IR is a lower-level representation that includes type information, resolved identifiers, and a simpler structure suitable for code generation.
func LowerWithSource ¶ added in v0.4.0
LowerWithSource converts WGSL AST to IR, keeping source for error messages.
When source is provided, errors will include line:column information and can show source context using ErrorList.FormatAll().
func Parse ¶
Parse parses WGSL source code to AST (Abstract Syntax Tree).
This is the first stage of compilation. The AST represents the syntactic structure of the shader but does not include semantic information like types.
func Validate ¶
func Validate(module *ir.Module) ([]ir.ValidationError, error)
Validate validates an IR module for correctness.
Validation checks include:
- Type consistency
- Reference validity (all handles point to valid objects)
- Control flow validity (structured control flow rules)
- Binding uniqueness (no duplicate @group/@binding)
Returns a slice of validation errors. If the slice is empty, validation passed.
Types ¶
type CompileOptions ¶
type CompileOptions struct {
// SPIRVVersion is the target SPIR-V version (default: 1.3)
SPIRVVersion spirv.Version
// Debug enables debug info in output (OpName, OpLine, etc.)
Debug bool
// Validate enables IR validation before code generation
Validate bool
}
CompileOptions configures shader compilation.
func DefaultOptions ¶
func DefaultOptions() CompileOptions
DefaultOptions returns sensible default options.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
dxilval
command
Command dxilval validates DXIL containers using Microsoft's IDxcValidator.
|
Command dxilval validates DXIL containers using Microsoft's IDxcValidator. |
|
dxilval/internal/run
Package run implements the dxilval CLI logic.
|
Package run implements the dxilval CLI logic. |
|
nagac
command
Command nagac is the naga shader compiler CLI.
|
Command nagac is the naga shader compiler CLI. |
|
spvdis
command
spvdis - SPIR-V disassembler Generates valid .spvasm text format
|
spvdis - SPIR-V disassembler Generates valid .spvasm text format |
|
texture_compile
command
|
|
|
Package dxil implements a DXIL (DirectX Intermediate Language) backend for the naga shader compiler.
|
Package dxil implements a DXIL (DirectX Intermediate Language) backend for the naga shader compiler. |
|
internal/bitcode
Package bitcode implements a bit-level writer for LLVM 3.7 bitcode format.
|
Package bitcode implements a bit-level writer for LLVM 3.7 bitcode format. |
|
internal/container
Package container implements the DXBC container format used to wrap DXIL shader bitcode.
|
Package container implements the DXBC container format used to wrap DXIL shader bitcode. |
|
internal/emit
Package emit implements naga IR to DXIL module lowering.
|
Package emit implements naga IR to DXIL module lowering. |
|
internal/module
Package module provides an in-memory representation of a DXIL module.
|
Package module provides an in-memory representation of a DXIL module. |
|
internal/passes/dce
Package dce implements dead code elimination for DXIL shader functions.
|
Package dce implements dead code elimination for DXIL shader functions. |
|
internal/passes/mem2reg
Package mem2reg promotes function-scope local variables of scalar type to SSA values, eliminating the corresponding alloca / load / store triples that the DXIL emitter would otherwise produce.
|
Package mem2reg promotes function-scope local variables of scalar type to SSA values, eliminating the corresponding alloca / load / store triples that the DXIL emitter would otherwise produce. |
|
internal/passes/sroa
Package sroa implements Scalar Replacement of Aggregates for DXIL emit.
|
Package sroa implements Scalar Replacement of Aggregates for DXIL emit. |
|
internal/viewid
Package viewid computes input→output dataflow dependencies for graphics entry points, producing the data that populates DXIL's `dx.viewIdState` metadata (ComputeViewIdState.cpp format) and the PSV0 dependency table (PSVDependencyTable layout).
|
Package viewid computes input→output dataflow dependencies for graphics entry points, producing the data that populates DXIL's `dx.viewIdState` metadata (ComputeViewIdState.cpp format) and the PSV0 dependency table (PSVDependencyTable layout). |
|
Package glsl provides a GLSL (OpenGL Shading Language) backend for naga.
|
Package glsl provides a GLSL (OpenGL Shading Language) backend for naga. |
|
Package hlsl provides HLSL (High-Level Shading Language) code generation from the naga intermediate representation.
|
Package hlsl provides HLSL (High-Level Shading Language) code generation from the naga intermediate representation. |
|
internal/codegen
Package hlsl implements HLSL expression generation for all IR expression types.
|
Package hlsl implements HLSL expression generation for all IR expression types. |
|
internal
|
|
|
backend
Package backend holds cross-backend constants and helpers shared between naga's text backends (HLSL) and binary backends (DXIL) that both target the D3D ecosystem.
|
Package backend holds cross-backend constants and helpers shared between naga's text backends (HLSL) and binary backends (DXIL) that both target the D3D ecosystem. |
|
dxcvalidator
Package dxcvalidator wraps Microsoft's IDxcValidator (dxil.dll) so naga can run real DXIL validation against generated containers from Pure Go.
|
Package dxcvalidator wraps Microsoft's IDxcValidator (dxil.dll) so naga can run real DXIL validation against generated containers from Pure Go. |
|
dxcvalidator/bitcheck
Package bitcheck implements a minimal LLVM 3.7 bitstream reader that walks a DXIL container far enough to verify the `!dx.entryPoints` named metadata is well-formed — specifically that every entry-point tuple has a non-null function reference in operand 0.
|
Package bitcheck implements a minimal LLVM 3.7 bitstream reader that walks a DXIL container far enough to verify the `!dx.entryPoints` named metadata is well-formed — specifically that every entry-point tuple has a non-null function reference in operand 0. |
|
registry
Package registry provides type deduplication for the naga IR.
|
Package registry provides type deduplication for the naga IR. |
|
textutil
Package textutil provides shared text writing utilities for naga codegen backends.
|
Package textutil provides shared text writing utilities for naga codegen backends. |
|
Package ir defines the intermediate representation for naga.
|
Package ir defines the intermediate representation for naga. |
|
Package msl implements Metal Shading Language (MSL) code generation for naga.
|
Package msl implements Metal Shading Language (MSL) code generation for naga. |
|
Package spirv provides SPIR-V code generation from naga IR.
|
Package spirv provides SPIR-V code generation from naga IR. |
|
internal/codegen
Package codegen implements SPIR-V code generation from naga IR.
|
Package codegen implements SPIR-V code generation from naga IR. |
|
Package wgsl provides WGSL (WebGPU Shading Language) parsing and lowering.
|
Package wgsl provides WGSL (WebGPU Shading Language) parsing and lowering. |