Skip to main content

Type Systems and Conversions

Complete reference for GLRE's type system and automatic conversions.

Scalar Types

Basic Scalar Types

TypeConstructorRangeGLSL/WGSL Equivalent
floatfloat(value)IEEE 754 32-bitfloat / f32
intint(value)-2³¹ to 2³¹-1int / i32
uintuint(value)0 to 2³²-1uint / u32
boolbool(value)true/falsebool / bool

Scalar Construction

// From literal values
const pi = float(3.14159)
const count = int(42)
const flag = bool(true)

// From other nodes
const computed = float(sin(angle))
const index = int(position.x.mul(10))
const condition = bool(value.greaterThan(threshold))

Number System Basics

For beginners: Numbers in programming represent different types of values:

  • Integers (int): Whole numbers like 1, 2, 100, -5
  • Floating point (float): Decimal numbers like 3.14, 0.5, -2.7
  • Boolean (bool): True or false values

Vector Types

Vector Constructors

Vectors are collections of numbers. Think of them as:

  • vec2: A point on a 2D plane (x, y)
  • vec3: A point in 3D space (x, y, z) or RGB color (red, green, blue)
  • vec4: 3D point with transparency (x, y, z, w) or RGBA color
TypeConstructor PatternsComponents
vec2vec2(x, y)x, y
vec2vec2(scalar)scalar, scalar
vec3vec3(x, y, z)x, y, z
vec3vec3(vec2, z)vec2.x, vec2.y, z
vec3vec3(scalar)scalar, scalar, scalar
vec4vec4(x, y, z, w)x, y, z, w
vec4vec4(vec3, w)vec3.x, vec3.y, vec3.z, w

Vector Examples

// Basic construction
const position2D = vec2(100, 200) // Point at (100, 200)
const color = vec3(1, 0.5, 0.2) // Orange color
const colorWithAlpha = vec4(color, 0.8) // Orange with 80% opacity

// Scalar expansion
const white = vec3(1.0) // Same as vec3(1.0, 1.0, 1.0)
const gray = vec4(0.5) // Same as vec4(0.5, 0.5, 0.5, 0.5)

// Combining vectors
const xy = vec2(0.5, 0.3)
const z = float(0.8)
const xyz = vec3(xy, z) // Combines 2D + 1D = 3D

Vector Math Basics

Vectors can be added, subtracted, and scaled:

const a = vec2(1, 2)
const b = vec2(3, 4)

// Addition: add corresponding components
const sum = a.add(b) // vec2(1+3, 2+4) = vec2(4, 6)

// Subtraction: subtract corresponding components
const diff = a.sub(b) // vec2(1-3, 2-4) = vec2(-2, -2)

// Scaling: multiply all components by same number
const scaled = a.mul(2) // vec2(1*2, 2*2) = vec2(2, 4)

Matrix Types

Matrix Basics

Matrices are grids of numbers used for transformations (moving, rotating, scaling objects):

TypeConstructorDimensionsCommon Use
mat2mat2(...)2×22D rotation
mat3mat3(...)3×32D transformation
mat4mat4(...)4×43D transformation

Matrix Construction

// Identity matrix (no transformation)
const identity2 = mat2([1, 0, 0, 1])

const identity3 = mat3([1, 0, 0, 0, 1, 0, 0, 0, 1])

// 2D rotation matrix
const angle = 0.5 // radians
const rotation = mat2([cos(angle), -sin(angle), sin(angle), cos(angle)])

Type Conversions

Explicit Conversions

MethodSource TypeTarget TypeBehavior
.toFloat()anyfloatExtract first component
.toInt()anyintTruncate to integer
.toBool()anyboolNon-zero → true
.toVec2()anyvec2Swizzle or broadcast
.toVec3()anyvec3Swizzle or broadcast
.toVec4()anyvec4Swizzle or broadcast

Conversion Examples

// Scalar to vector (broadcasting)
const brightness = float(0.8)
const gray = brightness.toVec3() // vec3(0.8, 0.8, 0.8)

// Vector to scalar (extract first component)
const position = vec3(1, 2, 3)
const x = position.toFloat() // 1.0

// Vector size changes
const color3 = vec3(1, 0, 0) // Red color
const color4 = color3.toVec4() // vec4(1, 0, 0, 1) - adds alpha
const coord = color4.toVec2() // vec2(1, 0) - takes first two

Automatic Type Promotion

Promotion Rules

When different types are combined, GLRE automatically promotes to the "larger" type:

OperationLeft TypeRight TypeResult TypeExplanation
Arithmeticfloatvec3vec3Float becomes vec3(float, float, float)
ArithmeticintfloatfloatInteger becomes floating point
Arithmeticvec2vec3vec3vec2 becomes vec3(vec2.x, vec2.y, 0)
Comparisonfloatvec3bvec3Compares float with each component

Promotion Examples

// Scalar with vector
const scale = float(2.0)
const position = vec3(1, 0, 0)
const scaled = position.mul(scale) // vec3(2, 0, 0)
// scale becomes vec3(2, 2, 2) automatically

// Different vector sizes
const offset2D = vec2(0.1, 0.2)
const position3D = vec3(1, 2, 3)
const result = position3D.add(offset2D) // vec3(1.1, 2.2, 3)
// offset2D becomes vec3(0.1, 0.2, 0) automatically

Swizzling System

What is Swizzling?

Swizzling lets you rearrange or extract components from vectors using letters:

Swizzling Patterns

PatternCoordinate SystemComponents
xyzwSpatialx, y, z, w
rgbaColorred, green, blue, alpha
stpqTextures, t, p, q

Swizzling Examples

const position = vec4(1, 2, 3, 4)

// Single component access
const x = position.x // float(1)
const green = position.g // float(2) - same as y
const s = position.s // float(1) - same as x

// Multi-component swizzling
const xy = position.xy // vec2(1, 2)
const rgb = position.rgb // vec3(1, 2, 3)
const bgr = position.bgr // vec3(3, 2, 1) - reversed!

// Repeated components
const xx = position.xx // vec2(1, 1)
const xyxy = position.xyxy // vec4(1, 2, 1, 2)

Advanced Swizzling

// Complex reordering
const original = vec4(1, 2, 3, 4)
const reordered = original.wzyx // vec4(4, 3, 2, 1) - completely reversed
const mixed = original.xzyw // vec4(1, 3, 2, 4) - custom order

// Cross-pattern swizzling
const color = vec4(0.8, 0.4, 0.2, 1.0)
const luminance = color.rrr // vec3(0.8, 0.8, 0.8) - all red
const textureCoord = color.st // vec2(0.8, 0.4) - same as xy

Boolean Vector Types

Boolean Vectors

Boolean vectors store true/false values for each component:

TypeConstructorComponent TypeUsage
bvec2bvec2(x, y)bool2D masks
bvec3bvec3(x, y, z)bool3D masks
bvec4bvec4(x, y, z, w)bool4D masks

Boolean Vector Examples

// Create boolean vectors
const mask = bvec3(true, false, true)

// From comparisons
const position = vec3(1, -2, 3)
const positive = position.greaterThan(vec3(0)) // bvec3(true, false, true)

// Logical operations
const all = all(positive) // bool: are ALL components true?
const any = any(positive) // bool: is ANY component true?
const inverted = not(positive) // bvec3(false, true, false)

Type Inference Engine

How Type Inference Works

GLRE automatically figures out what type each operation should return:

  1. Operator Type Rules: Addition of two floats returns float
  2. Function Return Types: sin() of any type returns same type
  3. Comparison Results: Comparisons always return boolean types
  4. Promotion Rules: Mixed operations promote to larger type

Complex Type Inference

// The system tracks types through complex expressions
const a = float(2.0) // float
const b = vec3(1, 0, 0) // vec3
const c = a.add(b) // vec3 (float promoted)
const d = c.lessThan(vec3(1)) // bvec3 (comparison result)
const e = select(d, c, vec3(0)) // vec3 (conditional selection)

Type Safety Features

Compile-Time Checking

GLRE catches type errors before they reach the GPU:

// This would cause a type error:
// const invalid = vec3(1, 2, 3).dot(float(5)) // Error: dot needs two vectors

// Correct version:
const valid = vec3(1, 2, 3).dot(vec3(5, 0, 0)) // OK: dot product of vectors

Runtime Type Information

// Check types at runtime
const checkType = (node) => {
if (node.type === 'vec3') {
console.log('This is a 3D vector')
} else if (node.type === 'float') {
console.log('This is a number')
}
}

Integer Vector Types

Integer Vectors

Vectors can also hold integer values:

TypeConstructorComponent TypeUsage
ivec2ivec2(x, y)intInteger 2D coordinates
ivec3ivec3(x, y, z)intInteger 3D coordinates
ivec4ivec4(x, y, z, w)intInteger 4D coordinates
uvec2uvec2(x, y)uintUnsigned integer coordinates

Integer Vector Examples

// Integer vectors for array indexing
const indices = ivec3(0, 1, 2)
const offset = uvec2(10, 20)

// Useful for grid coordinates
const gridX = int(position.x.mul(10))
const gridY = int(position.y.mul(10))
const gridCoord = ivec2(gridX, gridY)

The GLRE type system provides safety and convenience while maintaining the performance and flexibility needed for GPU programming.