wgsl

package
v0.18.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 3 Imported by: 0

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

func Lower

func Lower(ast *Module) (*ir.Module, error)

Lower converts a WGSL AST module to Naga IR.

func LowerWithSource

func LowerWithSource(ast *Module, source string) (*ir.Module, error)

LowerWithSource converts a WGSL AST module to Naga IR, keeping source for error messages.

Types

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer tokenizes WGSL source code into tokens.

func NewLexer

func NewLexer(source string) *Lexer

NewLexer creates a new lexer for the given source.

func (*Lexer) Tokenize

func (l *Lexer) Tokenize() (*Tokens, error)

Tokenize returns all tokens from the source.

type LowerResult

type LowerResult struct {
	Module   *ir.Module
	Warnings []Warning
}

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

type ParseError struct {
	Message string
	Line    int
	Column  int
}

ParseError represents a parsing error with location information.

func (ParseError) Error

func (e ParseError) Error() string

Error implements the error interface.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses WGSL tokens into an AST.

func NewParser

func NewParser(tokens *Tokens) *Parser

NewParser creates a new parser for the given tokens.

func (*Parser) Parse

func (p *Parser) Parse() (*Module, error)

Parse parses the tokens and returns a Module AST.

type Position

type Position struct {
	Line   int
	Column int
	Offset int
}

Position represents a position in source code.

type Span

type Span struct {
	Start  Position
	End    Position
	Source string
}

Span represents a source code location span.

type Tokens

type Tokens struct {
	// contains filtered or unexported fields
}

Tokens holds the result of lexical analysis. Pass it to NewParser.

type Warning

type Warning struct {
	Message string
	Span    Span
}

Warning represents a compiler warning (not an error).

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL