Files
physics/linear/linear.sakura
T
2026-10-27 22:20:40 -05:00

39 lines
1.8 KiB
Plaintext

#! Contains `linear`: vectors of two, three, and four `f32`s, rotations as
#! quaternions, and matrices of three by three and four by four, the numbers of
#! space as games, their renderers, and their physics work it.
#!
#! Space is right-handed: x to the right, y up, and z toward the viewer, so a
#! camera looks down its own -z. A matrix is column-major, as GLSL reads one,
#! and multiplies a vector on its right: `m * v`. Projections give a clip space
#! of y up and depth from 0 at the near plane to 1 at the far one, as Vulkan's
#! is with its y flipped. Angles are radians.
#!
#! Each type is of plain `f32` fields, so it's laid out as C and the GPU lay
#! out its numbers, and the virtual machine reads a field as quickly as any.
#! `vec3`, `vec4`, and `quat` are 16 bytes, `vec3` holding a fourth number no one
#! names, so an executable optimized with `-O2`, which holds `simd` vectors in
#! registers, reads each as one `f32x4` from where it is: what works whole
#! vectors, as adding, scaling, and dots do, the products of 4 by 4 matrices,
#! and a rotation turning a vector by its matrix's columns, it works as lanes
#! at once. What gathers one vector's numbers into
#! lanes, as a cross product, or splats them, as one vector a matrix turns, it
#! outruns lanes with a number at a time, as the bench shows, so that's how
#! those are worked. The virtual machine, which works lanes one at a time,
#! works everything a number at a time. Both ways give the same bits.
module linear;
# The lanes of a value of four `f32`s, as `vec3`, `vec4`, and `quat` are, read
# from where it is, which needs no alignment.
def lanes_of<T>(v: &T): f32x4 {
return ((v as *T) as *f32x4)[0];
}
# The value of four `f32`s whose numbers are lanes.
def of_lanes<T>(lanes: f32x4): T where T: zeroable {
var made: T;
((&mut made) as *mut f32x4)[0] = lanes;
return made;
}