glsl

package
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package glsl implements a GLSL ES 1.00 / 3.00 front end (lexer, preprocessor, parser, type checker) and a closure-compiled executor for software rendering.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Float32ToHalf

func Float32ToHalf(f float32) uint16

Float32ToHalf converts to IEEE binary16 (round to nearest even).

func HalfToFloat32

func HalfToFloat32(h uint16) float32

HalfToFloat32 converts IEEE binary16 to float32.

Types

type Block

type Block struct {
	Name     string
	Inst     string
	Binding  int
	Members  []BlockMember
	ByteSize int
	Used     bool
	ArrLen   int
}

Block describes a uniform block.

type BlockDecl

type BlockDecl struct {
	Q      Qual
	Name   string
	Fields []*StructField
	Inst   string
	ArrLen *Expr
	HasArr bool
	Line   int
}

type BlockMember

type BlockMember struct {
	Name   string
	T      Type
	Off    int // register offset
	Byte   int // byte offset in the std140 buffer
	Stride int // array stride in bytes (arrays), else 0
	Mat    int // matrix column stride in bytes
}

BlockMember is one member of a uniform block with its std140 placement.

type Decl

type Decl struct {
	Q      Qual
	T      *TypeSpec
	Name   string
	ArrLen *Expr
	HasArr bool
	Init   *Expr
	Line   int
}

type EK

type EK uint8
const (
	EIdent EK = iota
	EInt
	EUint
	EFloat
	EBool
	EUnary   // S = op (prefix: - + ! ~ ++ --)
	EPostfix // S = ++ or --
	EBinary  // S = op
	EAssign  // S = = += ...
	ETernary
	ECall  // S = function name or constructor type name; Type set for constructors
	EIndex // Args[0][Args[1]]
	EField // Args[0].S
	EComma
)

type Expr

type Expr struct {
	K    EK
	Line int
	S    string
	Args []*Expr
	I    int64
	F    float64
	Type *TypeSpec // constructor of array type or struct
}

type ExtDirective

type ExtDirective struct {
	Name, Behavior string
	Line           int
}

ExtDirective records an #extension line.

type Field

type Field struct {
	Name string
	T    Type
	Off  int // lane offset
}

Field is a struct member.

type Frame

type Frame struct {
	R   []uint32
	Tex []Sampler
	// Quad is called at derivative / implicit-LOD call sites; lanes of a
	// quad are paired by site id.
	Quad      func(site int, in []float32) (dx, dy []float32)
	Discarded bool
	// contains filtered or unexported fields
}

Frame is the execution state of one shader invocation.

type FuncDef

type FuncDef struct {
	Ret    *TypeSpec
	Name   string
	Params []*Param
	Body   []*Stmt
	Proto  bool
	Line   int
}

type IOVar

type IOVar struct {
	Name      string
	T         Type
	Off       int
	Loc       int // layout(location) or -1
	Flat      bool
	Used      bool
	Invariant bool
}

IOVar describes a shader input or output variable.

type Instance

type Instance struct {
	Frame
}

Instance executes a shader. Each goroutine needs its own Instance.

func (*Instance) Run

func (in *Instance) Run() (discarded bool)

Run executes main (after global initializers). It returns true when the invocation was discarded or aborted.

func (*Instance) Shader

func (in *Instance) Shader() *Shader

Shader returns the compiled shader.

type Kind

type Kind uint8

Kind is the base kind of a GLSL type.

const (
	KVoid Kind = iota
	KBool
	KInt
	KUint
	KFloat
	KSampler
	KStruct
)

type Options

type Options struct {
	WebGL2         bool            // allow #version 300 es
	Extensions     map[string]bool // extensions the context exposes (macros defined, behaviours accepted)
	MaxDrawBuffers int
}

Options controls compilation.

type Param

type Param struct {
	Dir    string // in out inout
	T      *TypeSpec
	Name   string
	ArrLen *Expr
	HasArr bool
	Const  bool
}

type Qual

type Qual struct {
	Storage   string // const attribute varying uniform in out inout (""=none)
	Interp    string // flat smooth
	Centroid  bool
	Invariant bool
	Prec      string
	Layout    map[string]int
	Std       string // std140 etc.
}

type SK

type SK uint8
const (
	SExpr SK = iota
	SDecl
	SBlock
	SIf
	SFor
	SWhile
	SDo
	SReturn
	SBreak
	SContinue
	SDiscard
	SEmpty
	SSwitch
	SCase
	SDefault
)

type SampleMode

type SampleMode uint8

SampleMode selects how the level of detail is chosen.

const (
	SampleImplicit SampleMode = iota // derive from DX/DY (or level 0 when zero)
	SampleLod                        // explicit Lod
	SampleGrad                       // explicit DX/DY
)

type SampleReq

type SampleReq struct {
	Coord  [4]float32 // s,t,r and array layer / cube face already in Coord[2] or [3]
	Ref    float32    // shadow comparison value
	DX, DY [3]float32 // derivatives of Coord (normalized texture coordinates)
	Lod    float32
	Bias   float32
	Mode   SampleMode
	Offset [3]int32
}

SampleReq is a texture lookup.

type Sampler

type Sampler interface {
	// Sample returns the filtered RGBA (shadow samplers return the
	// comparison result in all channels).
	Sample(r *SampleReq) [4]float32
	// SampleRaw performs a lookup in an integer texture; the result holds
	// raw int32/uint32 bits.
	SampleRaw(r *SampleReq) [4]uint32
	// Texel fetches without filtering. For float textures the result holds
	// float32 bits.
	Texel(x, y, z, lod int) [4]uint32
	// Size returns width, height, depth (or layers) of a level.
	Size(lod int) [3]int
}

Sampler is the texture side of the executor.

type SamplerKind

type SamplerKind uint8

SamplerKind describes a sampler type.

const (
	Sampler2D SamplerKind = iota + 1
	SamplerCube
	Sampler3D
	Sampler2DArray
	Sampler2DShadow
	SamplerCubeShadow
	Sampler2DArrayShadow
	ISampler2D
	ISampler3D
	ISamplerCube
	ISampler2DArray
	USampler2D
	USampler3D
	USamplerCube
	USampler2DArray
)

func (SamplerKind) Dim

func (k SamplerKind) Dim() int

Dim returns 2, 3 (3D), 6 (cube) or 22 (2D array).

func (SamplerKind) IsShadow

func (k SamplerKind) IsShadow() bool

IsShadow reports whether k is a shadow sampler.

func (SamplerKind) ResultKind

func (k SamplerKind) ResultKind() Kind

ResultKind is the component kind a sampler returns.

type Shader

type Shader struct {
	Stage   Stage
	Version int
	OK      bool
	Log     string
	Exts    map[string]string

	Inputs   []IOVar
	Outputs  []IOVar
	Uniforms []Uniform
	Blocks   []*Block

	// Built-in variable registers (-1 when absent).
	PositionOff, PointSizeOff, VertexIDOff, InstanceIDOff int
	FragCoordOff, FrontFacingOff, PointCoordOff           int
	FragColorOff, FragDataOff, FragDepthOff               int
	DepthRangeOff                                         int
	WritesFragDepth                                       bool
	WritesPointSize                                       bool
	WritesFragColor, WritesFragData                       bool
	UsesDerivatives                                       bool // dFdx/dFdy/fwidth
	UsesImplicitLod                                       bool // texture lookup without explicit lod
	UsesDiscard                                           bool
	UsesFragCoord                                         bool
	// contains filtered or unexported fields
}

Shader is a compiled shader.

func Compile

func Compile(stage Stage, src string, opt Options) *Shader

Compile compiles one shader. The returned Shader is never nil; check OK and Log.

func (*Shader) NewInstance

func (sh *Shader) NewInstance() *Instance

NewInstance creates an execution instance with its own register file.

type Stage

type Stage uint8

Stage is a shader stage.

const (
	Vertex Stage = iota
	Fragment
)

func (Stage) String

func (s Stage) String() string

type Stmt

type Stmt struct {
	K     SK
	Line  int
	E     *Expr
	Decls []*Decl
	Body  []*Stmt
	Then  *Stmt
	Else  *Stmt
	Init  *Stmt
	Post  *Expr
}

type StructField

type StructField struct {
	Name   string
	T      *TypeSpec
	ArrLen *Expr
	Line   int
}

type StructSpec

type StructSpec struct {
	Name   string
	Fields []*StructField
	Line   int
}

type StructT

type StructT struct {
	Name   string
	Fields []Field
	Size   int
}

StructT is a struct definition.

type Tok

type Tok struct {
	K     TokKind
	S     string
	Line  int
	BOL   bool // first token on its line
	Space bool // whitespace precedes the token
	// contains filtered or unexported fields
}

Tok is one lexical token.

type TokKind

type TokKind uint8

TokKind classifies a lexical token.

const (
	TEOF TokKind = iota
	TIdent
	TInt
	TUint
	TFloat
	TPunct
)

type Top

type Top struct {
	K      TopKind
	Decls  []*Decl
	Func   *FuncDef
	Struct *StructSpec
	Block  *BlockDecl
	Line   int
}

type TopKind

type TopKind uint8
const (
	TopDecl TopKind = iota
	TopFunc
	TopStruct
	TopPrecision
	TopBlock
	TopInvariant
)

type Type

type Type struct {
	K   Kind
	N   int // vector size or matrix rows (1 = scalar)
	M   int // matrix columns (1 = not a matrix)
	Arr int
	S   *StructT
	SK  SamplerKind
}

Type is a GLSL type. Arrays are Types with Arr > 0 whose element type is the same Type with Arr == 0.

func (Type) BaseSize

func (t Type) BaseSize() int

BaseSize is the lane count of one element.

func (Type) Elem

func (t Type) Elem() Type

func (Type) IsMat

func (t Type) IsMat() bool

func (Type) IsNumeric

func (t Type) IsNumeric() bool

func (Type) IsScalar

func (t Type) IsScalar() bool

func (Type) IsVec

func (t Type) IsVec() bool

func (Type) ScalarOf

func (t Type) ScalarOf() Type

ScalarOf returns the scalar type with the same kind.

func (Type) Size

func (t Type) Size() int

Size is the lane count of the whole value.

func (Type) String

func (t Type) String() string

type TypeSpec

type TypeSpec struct {
	Name   string      // basic type or struct name
	Struct *StructSpec // inline struct definition
	ArrLen *Expr       // float[3]
	Arr    bool        // true for float[] / float[3]
	Prec   string
	Line   int
}

type Uniform

type Uniform struct {
	Name  string
	T     Type
	Off   int
	Block int // index into Blocks, -1 for default block
	Used  bool
	Prec  string
}

Uniform describes a uniform variable.

type V

type V struct {
	T     Type
	Off   int
	Ops   []op
	C     []uint32
	Place bool
	RO    bool
	Dyn   []dyn
	Lanes []int
	Sym   *sym
	// contains filtered or unexported fields
}

V is a compiled expression.

Jump to

Keyboard shortcuts

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