types

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: CC0-1.0 Imports: 2 Imported by: 0

Documentation

Overview

Package types provides the WGSL type system for semantic validation.

This implements the type system as defined in WGSL spec section 6, supporting type inference, type checking, and overload resolution.

Index

Constants

This section is empty.

Variables

View Source
var (
	Bool          = &Scalar{Kind: ScalarBool}
	I32           = &Scalar{Kind: ScalarI32}
	U32           = &Scalar{Kind: ScalarU32}
	F32           = &Scalar{Kind: ScalarF32}
	F16           = &Scalar{Kind: ScalarF16}
	AbstractInt   = &Scalar{Kind: ScalarAbstractInt}
	AbstractFloat = &Scalar{Kind: ScalarAbstractFloat}
	VoidType      = &Void{}
)

Functions

func CanConvertTo

func CanConvertTo(src, dst Type) bool

CanConvertTo returns true if src can be implicitly converted to dst.

func IsArray

func IsArray(t Type) bool

IsArray returns true if t is an array type.

func IsFloat

func IsFloat(t Type) bool

IsFloat returns true if t is a floating-point type.

func IsInteger

func IsInteger(t Type) bool

IsInteger returns true if t is an integer type (scalar or vector of integer).

func IsMatrix

func IsMatrix(t Type) bool

IsMatrix returns true if t is a matrix type.

func IsNumeric

func IsNumeric(t Type) bool

IsNumeric returns true if t is a numeric type (scalar or vector of numeric).

func IsPointer

func IsPointer(t Type) bool

IsPointer returns true if t is a pointer type.

func IsReference

func IsReference(t Type) bool

IsReference returns true if t is a reference type.

func IsSampler

func IsSampler(t Type) bool

IsSampler returns true if t is a sampler type.

func IsScalar

func IsScalar(t Type) bool

IsScalar returns true if t is a scalar type.

func IsStruct

func IsStruct(t Type) bool

IsStruct returns true if t is a struct type.

func IsTexture

func IsTexture(t Type) bool

IsTexture returns true if t is a texture type.

func IsVector

func IsVector(t Type) bool

IsVector returns true if t is a vector type.

Types

type AccessMode

type AccessMode uint8

AccessMode represents WGSL access modes.

const (
	AccessModeNone AccessMode = iota
	AccessModeRead
	AccessModeWrite
	AccessModeReadWrite
)

func (AccessMode) String

func (a AccessMode) String() string

type AddressSpace

type AddressSpace uint8

AddressSpace represents WGSL address spaces.

const (
	AddressSpaceNone AddressSpace = iota
	AddressSpaceFunction
	AddressSpacePrivate
	AddressSpaceWorkgroup
	AddressSpaceUniform
	AddressSpaceStorage
	AddressSpaceHandle
)

func (AddressSpace) String

func (a AddressSpace) String() string

type Array

type Array struct {
	Element Type
	Count   int // 0 for runtime-sized arrays
}

Array represents array<T, N> or array<T> (runtime-sized).

func Arr

func Arr(elem Type, count int) *Array

Arr creates an array type.

func RuntimeArray

func RuntimeArray(elem Type) *Array

RuntimeArray creates a runtime-sized array type.

func (*Array) Align

func (a *Array) Align() int

func (*Array) Equals

func (a *Array) Equals(other Type) bool

func (*Array) IsConcrete

func (a *Array) IsConcrete() bool

func (*Array) IsConstructible

func (a *Array) IsConstructible() bool

func (*Array) IsHostShareable

func (a *Array) IsHostShareable() bool

func (*Array) IsRuntimeSized

func (a *Array) IsRuntimeSized() bool

IsRuntimeSized returns true if this is a runtime-sized array.

func (*Array) IsStorable

func (a *Array) IsStorable() bool

func (*Array) Size

func (a *Array) Size() int

func (*Array) String

func (a *Array) String() string

type Atomic

type Atomic struct {
	Element *Scalar // Must be i32 or u32
}

Atomic represents atomic<T>.

func AtomicType

func AtomicType(elem *Scalar) *Atomic

AtomicType creates an atomic type.

func (*Atomic) Align

func (a *Atomic) Align() int

func (*Atomic) Equals

func (a *Atomic) Equals(other Type) bool

func (*Atomic) IsConcrete

func (a *Atomic) IsConcrete() bool

func (*Atomic) IsConstructible

func (a *Atomic) IsConstructible() bool

func (*Atomic) IsHostShareable

func (a *Atomic) IsHostShareable() bool

func (*Atomic) IsStorable

func (a *Atomic) IsStorable() bool

func (*Atomic) Size

func (a *Atomic) Size() int

func (*Atomic) String

func (a *Atomic) String() string

type Function

type Function struct {
	Parameters []Type
	ReturnType Type // nil for void
}

Function represents a function type signature.

func (*Function) Align

func (f *Function) Align() int

func (*Function) Equals

func (f *Function) Equals(other Type) bool

func (*Function) IsConcrete

func (f *Function) IsConcrete() bool

func (*Function) IsConstructible

func (f *Function) IsConstructible() bool

func (*Function) IsHostShareable

func (f *Function) IsHostShareable() bool

func (*Function) IsStorable

func (f *Function) IsStorable() bool

func (*Function) Size

func (f *Function) Size() int

func (*Function) String

func (f *Function) String() string

type Matrix

type Matrix struct {
	Cols    int // 2, 3, or 4
	Rows    int // 2, 3, or 4
	Element *Scalar
}

Matrix represents matCxR<T>.

func Mat

func Mat(cols, rows int, elem *Scalar) *Matrix

Mat creates a matrix type.

func (*Matrix) Align

func (m *Matrix) Align() int

func (*Matrix) Equals

func (m *Matrix) Equals(other Type) bool

func (*Matrix) IsConcrete

func (m *Matrix) IsConcrete() bool

func (*Matrix) IsConstructible

func (m *Matrix) IsConstructible() bool

func (*Matrix) IsHostShareable

func (m *Matrix) IsHostShareable() bool

func (*Matrix) IsStorable

func (m *Matrix) IsStorable() bool

func (*Matrix) Size

func (m *Matrix) Size() int

func (*Matrix) String

func (m *Matrix) String() string

type Pointer

type Pointer struct {
	AddressSpace AddressSpace
	Element      Type
	AccessMode   AccessMode
}

Pointer represents ptr<space, T, access>.

func Ptr

func Ptr(space AddressSpace, elem Type, access AccessMode) *Pointer

Ptr creates a pointer type.

func (*Pointer) Align

func (p *Pointer) Align() int

func (*Pointer) Equals

func (p *Pointer) Equals(other Type) bool

func (*Pointer) IsConcrete

func (p *Pointer) IsConcrete() bool

func (*Pointer) IsConstructible

func (p *Pointer) IsConstructible() bool

func (*Pointer) IsHostShareable

func (p *Pointer) IsHostShareable() bool

func (*Pointer) IsStorable

func (p *Pointer) IsStorable() bool

func (*Pointer) Size

func (p *Pointer) Size() int

func (*Pointer) String

func (p *Pointer) String() string

type Reference

type Reference struct {
	AddressSpace AddressSpace
	Element      Type
	AccessMode   AccessMode
}

Reference represents a reference type (implicit pointer).

func Ref

func Ref(space AddressSpace, elem Type, access AccessMode) *Reference

Ref creates a reference type.

func (*Reference) Align

func (r *Reference) Align() int

func (*Reference) Equals

func (r *Reference) Equals(other Type) bool

func (*Reference) IsConcrete

func (r *Reference) IsConcrete() bool

func (*Reference) IsConstructible

func (r *Reference) IsConstructible() bool

func (*Reference) IsHostShareable

func (r *Reference) IsHostShareable() bool

func (*Reference) IsStorable

func (r *Reference) IsStorable() bool

func (*Reference) Size

func (r *Reference) Size() int

func (*Reference) String

func (r *Reference) String() string

type Sampler

type Sampler struct {
	Comparison bool
}

Sampler represents sampler or sampler_comparison.

func SamplerType

func SamplerType(comparison bool) *Sampler

SamplerType creates a sampler type.

func (*Sampler) Align

func (s *Sampler) Align() int

func (*Sampler) Equals

func (s *Sampler) Equals(other Type) bool

func (*Sampler) IsConcrete

func (s *Sampler) IsConcrete() bool

func (*Sampler) IsConstructible

func (s *Sampler) IsConstructible() bool

func (*Sampler) IsHostShareable

func (s *Sampler) IsHostShareable() bool

func (*Sampler) IsStorable

func (s *Sampler) IsStorable() bool

func (*Sampler) Size

func (s *Sampler) Size() int

func (*Sampler) String

func (s *Sampler) String() string

type Scalar

type Scalar struct {
	Kind ScalarKind
}

Scalar represents a scalar type (bool, i32, u32, f32, f16).

func (*Scalar) Align

func (s *Scalar) Align() int

func (*Scalar) Equals

func (s *Scalar) Equals(other Type) bool

func (*Scalar) IsConcrete

func (s *Scalar) IsConcrete() bool

func (*Scalar) IsConstructible

func (s *Scalar) IsConstructible() bool

func (*Scalar) IsFloat

func (s *Scalar) IsFloat() bool

IsFloat returns true if this is a floating-point type.

func (*Scalar) IsHostShareable

func (s *Scalar) IsHostShareable() bool

func (*Scalar) IsInteger

func (s *Scalar) IsInteger() bool

IsInteger returns true if this is an integer type.

func (*Scalar) IsNumeric

func (s *Scalar) IsNumeric() bool

IsNumeric returns true if this is a numeric scalar type.

func (*Scalar) IsStorable

func (s *Scalar) IsStorable() bool

func (*Scalar) Size

func (s *Scalar) Size() int

func (*Scalar) String

func (s *Scalar) String() string

type ScalarKind

type ScalarKind uint8

ScalarKind represents the kind of scalar type.

const (
	ScalarBool ScalarKind = iota
	ScalarI32
	ScalarU32
	ScalarF32
	ScalarF16
	ScalarAbstractInt
	ScalarAbstractFloat
)

type Struct

type Struct struct {
	Name   string
	Fields []StructField
	// contains filtered or unexported fields
}

Struct represents a user-defined struct type.

func (*Struct) Align

func (s *Struct) Align() int

func (*Struct) ComputeLayout

func (s *Struct) ComputeLayout()

ComputeLayout computes field offsets, struct size, and alignment.

func (*Struct) Equals

func (s *Struct) Equals(other Type) bool

func (*Struct) GetField

func (s *Struct) GetField(name string) *StructField

GetField returns the field with the given name, or nil.

func (*Struct) IsConcrete

func (s *Struct) IsConcrete() bool

func (*Struct) IsConstructible

func (s *Struct) IsConstructible() bool

func (*Struct) IsHostShareable

func (s *Struct) IsHostShareable() bool

func (*Struct) IsStorable

func (s *Struct) IsStorable() bool

func (*Struct) Size

func (s *Struct) Size() int

func (*Struct) String

func (s *Struct) String() string

type StructField

type StructField struct {
	Name   string
	Type   Type
	Offset int // Computed during type resolution
}

StructField represents a struct member.

type Texture

type Texture struct {
	Kind        TextureKind
	Dimension   TextureDimension
	SampledType *Scalar // For sampled textures
	TexelFormat string  // For storage textures
	AccessMode  AccessMode
}

Texture represents texture types.

func (*Texture) Align

func (t *Texture) Align() int

func (*Texture) Equals

func (t *Texture) Equals(other Type) bool

func (*Texture) IsConcrete

func (t *Texture) IsConcrete() bool

func (*Texture) IsConstructible

func (t *Texture) IsConstructible() bool

func (*Texture) IsHostShareable

func (t *Texture) IsHostShareable() bool

func (*Texture) IsStorable

func (t *Texture) IsStorable() bool

func (*Texture) Size

func (t *Texture) Size() int

func (*Texture) String

func (t *Texture) String() string

type TextureDimension

type TextureDimension uint8

TextureDimension indicates texture dimensionality.

const (
	Texture1D TextureDimension = iota
	Texture2D
	Texture2DArray
	Texture3D
	TextureCube
	TextureCubeArray
)

func (TextureDimension) String

func (d TextureDimension) String() string

type TextureKind

type TextureKind uint8

TextureKind indicates the texture category.

const (
	TextureSampled TextureKind = iota
	TextureMultisampled
	TextureStorage
	TextureDepth
	TextureDepthMultisampled
	TextureExternal
)

type Type

type Type interface {
	// String returns the WGSL syntax for this type.
	String() string
	// Equals returns true if this type equals another type.
	Equals(Type) bool
	// IsConstructible returns true if values of this type can be constructed.
	IsConstructible() bool
	// IsConcrete returns true if this is not an abstract type.
	IsConcrete() bool
	// IsStorable returns true if values can be stored in memory.
	IsStorable() bool
	// IsHostShareable returns true if this type can cross the CPU/GPU boundary.
	IsHostShareable() bool
	// Size returns the size in bytes (0 for unsized types).
	Size() int
	// Align returns the alignment in bytes.
	Align() int
	// contains filtered or unexported methods
}

Type represents a WGSL type.

func AddSubResultType

func AddSubResultType(left, right Type) Type

AddSubResultType returns the result type of a +/- b, or nil if invalid. Addition and subtraction require matching types or vector/matrix operations.

func CommonType

func CommonType(a, b Type) Type

CommonType returns the common type of two types for binary operations.

func ConcreteType

func ConcreteType(t Type) Type

ConcreteType returns the concrete version of an abstract type. For abstract-float returns f32, for abstract-int returns i32. For vectors/matrices with abstract elements, returns concrete element versions. For already concrete types, returns the type unchanged.

func DivResultType

func DivResultType(left, right Type) Type

DivResultType returns the result type of a / b. Division supports:

  • scalar / scalar → scalar
  • vector / vector → vector (component-wise)
  • vector / scalar → vector
  • scalar / vector → vector (broadcasts scalar)

func ElementType

func ElementType(t Type) Type

ElementType returns the element type of composite types, or nil.

func MultiplyResultType

func MultiplyResultType(left, right Type) Type

MultiplyResultType returns the result type of a * b, or nil if invalid. This handles all WGSL multiplication cases:

  • scalar * scalar → scalar
  • vector * vector → vector (component-wise)
  • matrix * matrix → matrix
  • scalar * vector → vector
  • vector * scalar → vector
  • scalar * matrix → matrix
  • matrix * scalar → matrix
  • matrix * vector → vector (matrix-vector multiplication)
  • vector * matrix → vector (vector-matrix multiplication)

type Vector

type Vector struct {
	Width   int // 2, 3, or 4
	Element *Scalar
}

Vector represents vec2<T>, vec3<T>, vec4<T>.

func Vec

func Vec(size int, elem *Scalar) *Vector

Vec creates a vector type.

func (*Vector) Align

func (v *Vector) Align() int

func (*Vector) Equals

func (v *Vector) Equals(other Type) bool

func (*Vector) IsConcrete

func (v *Vector) IsConcrete() bool

func (*Vector) IsConstructible

func (v *Vector) IsConstructible() bool

func (*Vector) IsHostShareable

func (v *Vector) IsHostShareable() bool

func (*Vector) IsStorable

func (v *Vector) IsStorable() bool

func (*Vector) Size

func (v *Vector) Size() int

func (*Vector) String

func (v *Vector) String() string

type Void

type Void struct{}

Void represents the absence of a return type.

func (*Void) Align

func (v *Void) Align() int

func (*Void) Equals

func (v *Void) Equals(other Type) bool

func (*Void) IsConcrete

func (v *Void) IsConcrete() bool

func (*Void) IsConstructible

func (v *Void) IsConstructible() bool

func (*Void) IsHostShareable

func (v *Void) IsHostShareable() bool

func (*Void) IsStorable

func (v *Void) IsStorable() bool

func (*Void) Size

func (v *Void) Size() int

func (*Void) String

func (v *Void) String() string

Jump to

Keyboard shortcuts

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