43 lines
2.2 KiB
Plaintext
43 lines
2.2 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, and works it as lanes
|
||
|
|
#! at once: adding, scaling, dots, lengths, and `normalized`, and what a matrix
|
||
|
|
#! makes of a vector or another matrix, each of its columns scaled by a number
|
||
|
|
#! splat into every lane, and added. What gathers one vector's numbers into
|
||
|
|
#! other lanes, as a cross product and a quaternions' product would, it works a
|
||
|
|
#! number at a time, as `simd::shuffle` gathers them a lane at a time. The
|
||
|
|
#! virtual machine, which works lanes one at a time, works everything a number
|
||
|
|
#! at a time, in the order the lanes do, so both ways give the same bits.
|
||
|
|
#!
|
||
|
|
#! A product is rounded before it's added, not fused with the sum as `fma` does:
|
||
|
|
#! a column waiting on the fused sum before it is slower than one adding
|
||
|
|
#! products worked at once, and the virtual machine would work each fused sum
|
||
|
|
#! as `std::math` does without FMA3, many instructions for one.
|
||
|
|
|
||
|
|
module malunal::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;
|
||
|
|
}
|