Documentation
¶
Overview ¶
Package wgsl provides WGSL (WebGPU Shading Language) parsing and lowering.
WGSL is the shader language for WebGPU, designed to be portable and map well to modern GPU APIs like Vulkan, Metal, and DX12.
Components ¶
The wgsl package consists of several components:
- Lexer: Tokenizes WGSL source code into tokens
- Parser: Parses tokens into an AST (Abstract Syntax Tree)
- Lowerer: Converts AST to IR (Intermediate Representation)
Usage ¶
To parse and lower a WGSL shader:
source := `
@vertex
fn main() -> @builtin(position) vec4<f32> {
return vec4<f32>(0.0, 0.0, 0.0, 1.0);
}
`
lexer := wgsl.NewLexer(source)
tokens, err := lexer.Tokenize()
if err != nil {
log.Fatal(err)
}
parser := wgsl.NewParser(tokens)
module, err := parser.Parse()
if err != nil {
log.Fatal(err)
}
irModule, err := wgsl.LowerWithSource(module, source)
if err != nil {
log.Fatal(err)
}
WGSL Specification ¶
This implementation follows the WGSL specification: https://www.w3.org/TR/WGSL/
Supported Features ¶
- Full lexical analysis
- Type declarations (struct, alias)
- Function declarations
- Variable declarations (var, let, const)
- All standard types (scalars, vectors, matrices)
- Attributes (@vertex, @fragment, @compute, etc.)
- Control flow (if, for, while, loop, switch)
- All operators and expressions
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Lexer ¶
type Lexer struct {
// contains filtered or unexported fields
}
Lexer tokenizes WGSL source code into tokens.
type LowerResult ¶
LowerResult contains the result of lowering, including any warnings.
func LowerWithWarnings ¶
func LowerWithWarnings(ast *Module, source string) (*LowerResult, error)
LowerWithWarnings converts a WGSL AST module to Naga IR, returning warnings alongside the module.
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module represents a parsed WGSL module (abstract syntax tree). It is produced by Parser.Parse and consumed by Lower/LowerWithSource.
type ParseError ¶
ParseError represents a parsing error with location information.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser parses WGSL tokens into an AST.