Files
physics/README.md
T

77 lines
6.7 KiB
Markdown
Raw Normal View History

2026-10-27 22:08:25 -05:00
# 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. It's Sakura's, built with [blossom](https://git.malunal.studio/sakura/blossom), and reads only std's `math`. An executable optimized with `-O2` works what it does to whole vectors as SIMD lanes, which the virtual machine works a number at a time, giving the same bits.
```sakura
import linear;
val up := vec3::up;
var at := vec3{ 1, 2, 3 };
at = at + vec3{ x = 0.5 } * 2; # vec3{ 2, 2, 3 }
val ahead := at.cross(up).normalized();
val far := at.dot(ahead) + at.length;
val turn := quat::axis_angle(vec3::back, math::pi as f32 / 2);
val turned := turn * vec3::right; # vec3::up, as near as f32 gets
val both := quat::euler(0.3, -1.1, 0.7) * turn; # turn, then the other
val place := mat4::compose(vec3{ 5, 0, -2 }, turn, vec3::one);
val moved := place.transform_point(vec3::zero); # vec3{ 5, 0, -2 }
val clip := mat4::perspective(math::pi as f32 / 3, 16.0 / 9.0, 0.1, 100) * mat4::look_at(vec3{ 0, 2, 10 }, vec3::zero);
val inertia := mat3::rotation(turn) * mat3::scaling(vec3{ 2, 1, 1 }) * mat3::rotation(turn).transposed();
```
`tests/linear.sakura` drives all of it, and `tools/bench.sakura` times it.
A project reads it by requiring this folder, or its remote, and naming `linear` among a target's `deps`:
```yaml
requires:
- ../linear
targets:
- name: game
type: program
deps: [linear]
```
## Layout
| Path | Holds |
|---|---|
| `linear/linear.sakura` | The conventions, and reading a value of four `f32`s as one `f32x4` where it lies |
| `linear/vec2.sakura` | `vec2`: a place or direction in a plane |
| `linear/vec3.sakura` | `vec3`: a place or direction in space, and the axes' directions |
| `linear/vec4.sakura` | `vec4`: a place of space with its `w`, as a matrix projects one |
| `linear/quat.sakura` | `quat`: rotations by an axis and angle, by angles about each axis, by where they look, and of a basis; turning vectors, following, undoing, and `slerp` |
| `linear/mat3.sakura` | `mat3`: rotations, scales, and what turns directions, as an inertia tensor; transposes, determinants, and inverses |
| `linear/mat4.sakura` | `mat4`: moving, turning, scaling, and composing them; perspective and orthographic projections; `look_at`; points and directions moved; inverses |
## How it works
- **Space** is right-handed: x to the right, y up, z toward the viewer, so a camera looks down its -z. Matrices are column-major, as GLSL reads them, and multiply a vector on their right, so `a * b` does `b`, then `a`, as quaternions' products do. 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 with its y flipped. Angles are radians.
- **Plain fields**: each type is of `f32` fields, aligned as an `f32`, so a struct holding one lays it out wherever it falls, as C and the GPU do, and the virtual machine reads a field as quickly as any. `vec3` holds a fourth `f32` no one names, so it's 16 bytes, an `f32x4`'s, as `vec4` and `quat` are. It's 0 as a literal leaves it, as `vec3{ 1, 2, 3 }` and `vec3{ y = 1 }` do, and printing a `vec3` shows it as `_w`. A `mat3`'s columns are `vec3`s, so it's laid out as std140 lays out a GLSL `mat3`; a `mat4` is its 16 numbers, column after column.
- **Lanes**: an executable optimized with `-O2` holds `simd` vectors in registers, so it reads a `vec3`, `vec4`, or `quat` as one `f32x4` from where it is, aligned or not, and works lanes at once: adding, subtracting, scaling, and dividing vectors, their least, greatest, and absolute numbers, `lerp`, dots and lengths, `distance`, the products of `mat4`s, and a `quat` turning a vector, by its matrix's columns. What gathers one vector's numbers into other lanes, as a cross product or a quaternions' product would, or splats each into lanes, as one vector a matrix turns, it works faster a number at a time, as the bench found, so those are. `lanes_held()` says which way while compiling, so only that way's code is kept. `lanes()` and `of_lanes` give a `vec3`'s, `vec4`'s, or `quat`'s numbers as an `f32x4`, for code of one's own to work.
- **The same bits**: the virtual machine, which works lanes one at a time, and constants work everything a number at a time, in the order the lanes do, so either way gives the same bits; `tests/linear.sakura` folds the bits of a long chain of operations, and the machine and an executable each check they're those.
## Speed
`blossom run linear.bench` works each operation over 1,024 values 2,000 times, each pass a function of its own, as a system is, its operations called from elsewhere too, built with `-O2`, on a Ryzen 9 9950X3D, against the same worked a number at a time on a `vec3` of 12 bytes, as hearth's scenes worked them before `linear`:
| What | Before | `linear` | |
|---|---|---|---|
| `p + v * dt` of `vec3`s | 3.51 ns | 0.87 ns | 4.0x |
| `dot` of `vec3`s | 5.80 ns | 3.19 ns | 1.8x |
| `quat * vec3` | 33.1 ns | 16.6 ns | 2.0x |
| `mat4 * mat4` | 33.7 ns | 20.6 ns | 1.6x |
| `quat * quat` | 7.87 ns | 7.87 ns | 1.0x |
| `vec3::normalized` | 4.75 ns | 6.67 ns | 0.71x |
| `vec3::cross` | 4.20 ns | 6.91 ns | 0.61x |
| `mat4::transform_point` | 5.05 ns | 7.52 ns | 0.67x |
| `mat3 * mat3` | 14.9 ns | 22.6 ns | 0.66x |
| `mat3 * vec3` | | 7.01 ns | |
| `mat4 * vec4` | | 8.27 ns | |
| `mat4::inverse` | | 60.4 ns | |
What works whole vectors gains most: integrating places by velocities, as physics does for every body, is four times as fast. What builds a `vec3` a number at a time costs about 2 ns more than it did of 12 bytes, which the same code built of 12 bytes doesn't, and lanes don't recover there. How fast the same operation runs hangs on the code around it, by as much as three times either way, as code inlined into a large function slows; these are of small functions, as a game's systems are.
## Testing
`blossom test` runs `tests/linear.sakura` twice: `linear.tests` on the machine, a number at a time, with every read and write checked, and `linear.lanes.tests` as an executable built with `-O2`, as lanes. Each checks the types' sizes; vectors' arithmetic, dots, crosses, lengths, and the rest; rotations turning, following one another, undone, and between; projections; matrices of three's rotations, scales, transposes, determinants, and inverses; values read where a struct puts them, unaligned; and the bits of a long chain of all of it.
## Not yet
Vectors of several values at once, as eight `vec3`s in three `f32x8`s, which physics passes over its bodies with, and which the bench's own tries found ten to twenty times as fast as one at a time; `f64` vectors; and `vec2`s worked as lanes, which hold half an `f32x4`.