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+
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
|
|
|
nagac
command
Command nagac is the naga shader compiler CLI.
|
Command nagac is the naga shader compiler CLI. |
|
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. |
|
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. |
|
Package wgsl provides WGSL (WebGPU Shading Language) parsing.
|
Package wgsl provides WGSL (WebGPU Shading Language) parsing. |