docs / GPU Programming / Vector Types

Vector types — Vec2 / Vec3 / Vec4

Vec2<T>, Vec3<T>, and Vec4<T> (use system.gpu.vector) are value types with .x/.y/.z/.w component fields. Inside kernels they compile to native WGSL vectors, and the vector builtins — dot, length, normalize, cross, reflect, mix — plus scalar broadcast (v * 2.0) compile to the hardware's vector instructions:

use system.gpu
use system.gpu.vector
use system.math
use system.collections.array

fn main()
    gpu let ax = [1.0]
    gpu let ay = [0.0]
    gpu let az = [0.0]
    gpu let bx = [2.0]
    gpu let by = [3.0]
    gpu let bz = [4.0]
    gpu var result = [0.0]
    forall i in 0..1
        let a = Vec3<f32>(ax[i], ay[i], az[i])
        let b = Vec3<f32>(bx[i], by[i], bz[i])
        result[i] = dot(a, b)
    let host = result
    println(f'{host[0]}')

Output: 2.0. Arrays of vectors store their elements inline with std430 layout (Array<Vec3<f32>, N> has 16-byte stride), so a buffer of vectors round-trips host → GPU → host bit-exactly for f32, i32, and u32 elements. 64-bit vector components are rejected — WGSL has no portable 64-bit vectors. The type checker enforces dimensions and element types: cross is Vec3-only, integer normalize is an error.

The Raymarch, Black Hole, and Wormhole demos are built on Vec3 math.