Files
physics/physics3d/wide.sakura
T
2026-11-14 15:41:09 -06:00

545 lines
19 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#! Contains how a space solves its contacts eight at a time, as in a plane:
#! each step, every contact is given the first of 24 colors none of its dynamic
#! bodies has yet, so no two contacts of a color push the same body, and the
#! contacts of each color are solved as the lanes of `f32x8`s, eight at once,
#! each body's velocities gathered from where its state is and scattered back.
#! What fits no color is solved one at a time after.
#!
#! A body's inverse inertia, as it's turned as the step begins, is worked into
#! each point's levers as the contacts are packed, so the lanes turn a body by
#! what's packed, never a matrix of their own.
import std::containers;
import std::math;
import malunal::linear;
module malunal::physics3d;
# How many colors contacts are sorted into.
const colors: u64 = 24;
# How many contacts are solved at once, and the vectors of their numbers.
const width: u64 = 8;
type lanes = f32x8;
type lane_places = i32x8;
# How many numbers a body's state is: its velocities, how far it's moved, and
# how it's turned, each of four.
const state_numbers: u64 = 16;
# A point of eight contacts, a lane each: where it is from each body's
# center, how far apart they are there but for how far they've moved, the
# impulses pushing them apart and along each tangent, how hard each is to push,
# and how each body turns by an impulse along each, its inverse inertia times
# its lever. A lane of a contact of fewer points has the rest of no mass, which
# push nothing.
struct wide_point {
var rax: lanes;
var ray: lanes;
var raz: lanes;
var rbx: lanes;
var rby: lanes;
var rbz: lanes;
var base: lanes;
var normal: lanes;
var tangent1: lanes;
var tangent2: lanes;
var most: lanes;
var mass_n: lanes;
var mass_1: lanes;
var mass_2: lanes;
var nax: lanes;
var nay: lanes;
var naz: lanes;
var nbx: lanes;
var nby: lanes;
var nbz: lanes;
var sax: lanes; # Along the first tangent.
var say: lanes;
var saz: lanes;
var sbx: lanes;
var sby: lanes;
var sbz: lanes;
var tax: lanes; # Along the second.
var tay: lanes;
var taz: lanes;
var tbx: lanes;
var tby: lanes;
var tbz: lanes;
}
# Eight contacts as lanes: where each body's state begins among the states'
# numbers, the normal and tangents, friction, each body's inverse mass, and how
# soft each is. A lane of no contact is of the state of what doesn't move.
struct wide_contact {
var a: lane_places;
var b: lane_places;
var nx: lanes;
var ny: lanes;
var nz: lanes;
var t1x: lanes;
var t1y: lanes;
var t1z: lanes;
var t2x: lanes;
var t2y: lanes;
var t2z: lanes;
var friction: lanes;
var ma: lanes;
var mb: lanes;
var bias_rate: lanes;
var mass_scale: lanes;
var impulse_scale: lanes;
var points: wide_point[4];
var of: u32[8]; # Each lane's constraint, or none.
}
# The states' numbers, sixteen to a state, as `wide_contact`'s lanes gather
# them.
def numbers_of(s: &mut solver_scratch): slice<f32> {
return slice::<f32>::over(s.states.data as *mut f32, s.states.length * state_numbers);
}
# Sorts the step's contacts into colors, and packs each color's into lanes.
def color_contacts(s: &mut solver_scratch): void {
s.wide.clear();
s.overflow.clear();
val words := (s.states.length + 63) / 64;
s.colored.clear();
s.colored.resize(colors * words, 0);
s.color_of.clear();
s.color_of.resize(s.constraints.length, none);
var counts: u32[24];
for k in 0..s.constraints.length {
val c := &s.constraints[k];
# Only a body that's pushed is a body two lanes mustn't share.
val a := c.ma > 0 ? c.a as u64 : 0u;
val b := c.mb > 0 ? c.b as u64 : 0u;
for color in 0..colors {
val row := color * words;
continue if a != 0 && ((s.colored[row + a / 64] >> (a % 64)) & 1) != 0;
continue if b != 0 && ((s.colored[row + b / 64] >> (b % 64)) & 1) != 0;
if a != 0 {
s.colored[row + a / 64] = s.colored[row + a / 64] | (1u << (a % 64));
}
if b != 0 {
s.colored[row + b / 64] = s.colored[row + b / 64] | (1u << (b % 64));
}
s.color_of[k] = color as u32;
counts[color]++;
break;
}
if s.color_of[k] == none {
s.overflow.append(k as u32);
}
}
var first: u32[24];
var groups: u64 = 0;
for color in 0..colors {
first[color] = groups as u32;
s.color_groups[color] = groups as u32;
groups += (counts[color] as u64 + width - 1) / width;
}
s.color_groups[colors] = groups as u32;
s.wide.resize(groups, wide_contact{});
for g in 0..groups {
for lane in 0..width {
s.wide[g].of[lane] = none;
}
}
var filled: u32[24];
for k in 0..s.constraints.length {
val color := s.color_of[k];
continue if color == none;
val at := filled[color as u64];
filled[color as u64]++;
val c := &s.constraints[k];
pack(&mut s.wide[(first[color as u64] as u64 + at as u64 / width)], at as u64 % width, c, k as u32, s.inertias[c.a as u64], s.inertias[c.b as u64]);
}
}
# Puts a contact into a lane.
def pack(w: &mut wide_contact, lane: u64, c: &contact_constraint, k: u32, ia: mat3, ib: mat3): void {
w.of[lane] = k;
w.a[lane] = (c.a as u64 * state_numbers) as i32;
w.b[lane] = (c.b as u64 * state_numbers) as i32;
w.nx[lane] = c.normal.x;
w.ny[lane] = c.normal.y;
w.nz[lane] = c.normal.z;
w.t1x[lane] = c.t1.x;
w.t1y[lane] = c.t1.y;
w.t1z[lane] = c.t1.z;
w.t2x[lane] = c.t2.x;
w.t2y[lane] = c.t2.y;
w.t2z[lane] = c.t2.z;
w.friction[lane] = c.friction;
w.ma[lane] = c.ma;
w.mb[lane] = c.mb;
w.bias_rate[lane] = c.soft.bias_rate;
w.mass_scale[lane] = c.soft.mass_scale;
w.impulse_scale[lane] = c.soft.impulse_scale;
for j in 0..c.count {
val p := &c.points[j];
val q := &mut w.points[j];
val ra := p.anchor_a;
val rb := p.anchor_b;
q.rax[lane] = ra.x;
q.ray[lane] = ra.y;
q.raz[lane] = ra.z;
q.rbx[lane] = rb.x;
q.rby[lane] = rb.y;
q.rbz[lane] = rb.z;
q.base[lane] = p.base_separation;
q.normal[lane] = p.normal_impulse;
q.tangent1[lane] = p.tangent_impulse.x;
q.tangent2[lane] = p.tangent_impulse.y;
q.mass_n[lane] = p.normal_mass;
q.mass_1[lane] = p.tangent_mass.x;
q.mass_2[lane] = p.tangent_mass.y;
val na := ia * ra.cross(c.normal);
val nb := ib * rb.cross(c.normal);
val sa := ia * ra.cross(c.t1);
val sb := ib * rb.cross(c.t1);
val ta := ia * ra.cross(c.t2);
val tb := ib * rb.cross(c.t2);
q.nax[lane] = na.x;
q.nay[lane] = na.y;
q.naz[lane] = na.z;
q.nbx[lane] = nb.x;
q.nby[lane] = nb.y;
q.nbz[lane] = nb.z;
q.sax[lane] = sa.x;
q.say[lane] = sa.y;
q.saz[lane] = sa.z;
q.sbx[lane] = sb.x;
q.sby[lane] = sb.y;
q.sbz[lane] = sb.z;
q.tax[lane] = ta.x;
q.tay[lane] = ta.y;
q.taz[lane] = ta.z;
q.tbx[lane] = tb.x;
q.tby[lane] = tb.y;
q.tbz[lane] = tb.z;
}
}
# Gives each lane's impulses back to its contact, to bounce and keep.
def unpack_contacts(s: &mut solver_scratch): void {
for g in 0..s.wide.length {
for lane in 0..width {
val k := s.wide[g].of[lane];
continue if k == none;
for j in 0..s.constraints[k as u64].count {
val q := &s.wide[g].points[j];
val p := &mut s.constraints[k as u64].points[j];
p.normal_impulse = q.normal[lane];
p.tangent_impulse = vec3{ q.tangent1[lane], q.tangent2[lane], 0 };
p.max_impulse = math::max(p.max_impulse, q.most[lane]);
}
}
}
}
# Which lanes push a body, by its inverse mass: those of what doesn't move, or
# isn't pushed, as a kinematic body, aren't written back, as lanes on other
# threads read its state.
def pushed(m: lanes): bool[8] {
val ms := m.lanes();
var made: bool[8];
for k in 0..width {
made[k] = ms[k] > 0;
}
return made;
}
# Each lane's square root, a lane at a time.
def root_of(x: lanes): lanes {
var each := x.lanes();
for i in 0..width {
each[i] = math::sqrt(each[i]);
}
return lanes::of(each);
}
# A stage of a step's lanes, run on the space's crew a color at a time: the
# lanes of each color split between its threads, as no two lanes of a color
# push one body.
struct wide_job {
var s: *mut solver_scratch;
var first: u64;
var warming: bool;
var inv_h: f32;
var push: f32;
var use_bias: bool;
}
def run_wide(context: *mut u8, begin: u64, end: u64): void {
val job := &through(context as *mut wide_job);
val s := &mut through(job.s);
if job.warming {
warm_groups(s, job.first + begin, job.first + end);
} else {
solve_groups(s, job.first + begin, job.first + end, job.inv_h, job.push, job.use_bias);
}
}
# Warms or solves the lanes, on the crew where the space has one, a color at a
# time.
def space::wide_stage(&mut this, s: &mut solver_scratch, warming: bool, inv_h: f32, push: f32, use_bias: bool): void {
if this.threads <= 1 {
if warming {
warm_groups(s, 0, s.wide.length);
} else {
solve_groups(s, 0, s.wide.length, inv_h, push, use_bias);
}
return;
}
var job := wide_job{ s = address(s), warming = warming, inv_h = inv_h, push = push, use_bias = use_bias };
for color in 0..colors {
val from := s.color_groups[color] as u64;
val to := s.color_groups[color + 1] as u64;
continue if to == from;
job.first = from;
val block := math::max((to - from) / (this.threads as u64 * 4), 1u);
this.crew.run(run_wide, address(&mut job) as *mut u8, to - from, block);
}
}
# Writes each pushed lane's number back to where it was gathered from.
def put(v: lanes, into: slice<f32>, at: lane_places, keep: &bool[8]): void {
val each := v.lanes();
val places := at.lanes();
for k in 0..width {
if keep[k] {
into[places[k] as u64] = each[k];
}
}
}
def warm_groups(s: &mut solver_scratch, from: u64, to: u64): void {
val numbers := numbers_of(s);
val view := numbers.span;
val one := lane_places::splat(1);
val two := lane_places::splat(2);
val four := lane_places::splat(4);
val five := lane_places::splat(5);
val six := lane_places::splat(6);
val groups := s.wide.slice;
for g in from..to {
val w := &groups[g];
val keep_a := pushed(w.ma);
val keep_b := pushed(w.mb);
var vax := lanes::gather(view, w.a);
var vay := lanes::gather(view, w.a + one);
var vaz := lanes::gather(view, w.a + two);
var wax := lanes::gather(view, w.a + four);
var way := lanes::gather(view, w.a + five);
var waz := lanes::gather(view, w.a + six);
var vbx := lanes::gather(view, w.b);
var vby := lanes::gather(view, w.b + one);
var vbz := lanes::gather(view, w.b + two);
var wbx := lanes::gather(view, w.b + four);
var wby := lanes::gather(view, w.b + five);
var wbz := lanes::gather(view, w.b + six);
for j in 0..4u {
val p := &w.points[j];
val n := p.normal;
val t1 := p.tangent1;
val t2 := p.tangent2;
val px := w.nx * n + w.t1x * t1 + w.t2x * t2;
val py := w.ny * n + w.t1y * t1 + w.t2y * t2;
val pz := w.nz * n + w.t1z * t1 + w.t2z * t2;
vax = vax - px * w.ma;
vay = vay - py * w.ma;
vaz = vaz - pz * w.ma;
vbx = vbx + px * w.mb;
vby = vby + py * w.mb;
vbz = vbz + pz * w.mb;
wax = wax - (p.nax * n + p.sax * t1 + p.tax * t2);
way = way - (p.nay * n + p.say * t1 + p.tay * t2);
waz = waz - (p.naz * n + p.saz * t1 + p.taz * t2);
wbx = wbx + (p.nbx * n + p.sbx * t1 + p.tbx * t2);
wby = wby + (p.nby * n + p.sby * t1 + p.tby * t2);
wbz = wbz + (p.nbz * n + p.sbz * t1 + p.tbz * t2);
}
put(vax, numbers, w.a, &keep_a);
put(vay, numbers, w.a + one, &keep_a);
put(vaz, numbers, w.a + two, &keep_a);
put(wax, numbers, w.a + four, &keep_a);
put(way, numbers, w.a + five, &keep_a);
put(waz, numbers, w.a + six, &keep_a);
put(vbx, numbers, w.b, &keep_b);
put(vby, numbers, w.b + one, &keep_b);
put(vbz, numbers, w.b + two, &keep_b);
put(wbx, numbers, w.b + four, &keep_b);
put(wby, numbers, w.b + five, &keep_b);
put(wbz, numbers, w.b + six, &keep_b);
}
}
def solve_groups(s: &mut solver_scratch, from: u64, to: u64, inv_h: f32, push: f32, use_bias: bool): void {
val numbers := numbers_of(s);
val view := numbers.span;
val one := lane_places::splat(1);
val two := lane_places::splat(2);
val four := lane_places::splat(4);
val five := lane_places::splat(5);
val six := lane_places::splat(6);
val eight := lane_places::splat(8);
val nine := lane_places::splat(9);
val ten := lane_places::splat(10);
val twelve := lane_places::splat(12);
val thirteen := lane_places::splat(13);
val fourteen := lane_places::splat(14);
val fifteen := lane_places::splat(15);
val zero := lanes::splat(0);
val unit := lanes::splat(1);
val doubled := lanes::splat(2);
val per_h := lanes::splat(inv_h);
val least := lanes::splat(-push);
val groups := s.wide.slice;
for g in from..to {
val w := &mut groups[g];
val keep_a := pushed(w.ma);
val keep_b := pushed(w.mb);
var vax := lanes::gather(view, w.a);
var vay := lanes::gather(view, w.a + one);
var vaz := lanes::gather(view, w.a + two);
var wax := lanes::gather(view, w.a + four);
var way := lanes::gather(view, w.a + five);
var waz := lanes::gather(view, w.a + six);
var vbx := lanes::gather(view, w.b);
var vby := lanes::gather(view, w.b + one);
var vbz := lanes::gather(view, w.b + two);
var wbx := lanes::gather(view, w.b + four);
var wby := lanes::gather(view, w.b + five);
var wbz := lanes::gather(view, w.b + six);
val dpx := lanes::gather(view, w.b + eight) - lanes::gather(view, w.a + eight);
val dpy := lanes::gather(view, w.b + nine) - lanes::gather(view, w.a + nine);
val dpz := lanes::gather(view, w.b + ten) - lanes::gather(view, w.a + ten);
val qax := lanes::gather(view, w.a + twelve);
val qay := lanes::gather(view, w.a + thirteen);
val qaz := lanes::gather(view, w.a + fourteen);
val qaw := lanes::gather(view, w.a + fifteen);
val qbx := lanes::gather(view, w.b + twelve);
val qby := lanes::gather(view, w.b + thirteen);
val qbz := lanes::gather(view, w.b + fourteen);
val qbw := lanes::gather(view, w.b + fifteen);
val nx := w.nx;
val ny := w.ny;
val nz := w.nz;
val ma := w.ma;
val mb := w.mb;
for j in 0..4u {
val p := &mut w.points[j];
val rax := p.rax;
val ray := p.ray;
val raz := p.raz;
val rbx := p.rbx;
val rby := p.rby;
val rbz := p.rbz;
# Each anchor as its body has turned since the step began: r + w·t +
# q × t, of t = 2 q × r.
val tax := doubled * (qay * raz - qaz * ray);
val tay := doubled * (qaz * rax - qax * raz);
val taz := doubled * (qax * ray - qay * rax);
val tbx := doubled * (qby * rbz - qbz * rby);
val tby := doubled * (qbz * rbx - qbx * rbz);
val tbz := doubled * (qbx * rby - qby * rbx);
val dx := dpx + (rbx + qbw * tbx + (qby * tbz - qbz * tby)) - (rax + qaw * tax + (qay * taz - qaz * tay));
val dy := dpy + (rby + qbw * tby + (qbz * tbx - qbx * tbz)) - (ray + qaw * tay + (qaz * tax - qax * taz));
val dz := dpz + (rbz + qbw * tbz + (qbx * tby - qby * tbx)) - (raz + qaw * taz + (qax * tay - qay * tax));
val separation := dx * nx + dy * ny + dz * nz + p.base;
val apart := separation.greater(&zero);
var bias := select(apart, separation * per_h, zero);
var mass_scale := unit;
var impulse_scale := zero;
if use_bias {
bias = select(apart, bias, (w.bias_rate * separation).max(&least));
mass_scale = select(apart, unit, w.mass_scale);
impulse_scale = select(apart, zero, w.impulse_scale);
}
# How fast they close: along the normal, and as each turns about its
# lever across it.
val vn := (vbx - vax) * nx + (vby - vay) * ny + (vbz - vaz) * nz
+ wbx * (rby * nz - rbz * ny) + wby * (rbz * nx - rbx * nz) + wbz * (rbx * ny - rby * nx)
- wax * (ray * nz - raz * ny) - way * (raz * nx - rax * nz) - waz * (rax * ny - ray * nx);
val before := p.normal;
val sum := (before - p.mass_n * mass_scale * (vn + bias) - impulse_scale * before).max(&zero);
val impulse := sum - before;
p.normal = sum;
p.most = p.most.max(&impulse);
vax = vax - nx * impulse * ma;
vay = vay - ny * impulse * ma;
vaz = vaz - nz * impulse * ma;
vbx = vbx + nx * impulse * mb;
vby = vby + ny * impulse * mb;
vbz = vbz + nz * impulse * mb;
wax = wax - p.nax * impulse;
way = way - p.nay * impulse;
waz = waz - p.naz * impulse;
wbx = wbx + p.nbx * impulse;
wby = wby + p.nby * impulse;
wbz = wbz + p.nbz * impulse;
}
# Friction along both tangents, of no more in all than its share of what
# pushes them apart.
val t1x := w.t1x;
val t1y := w.t1y;
val t1z := w.t1z;
val t2x := w.t2x;
val t2y := w.t2y;
val t2z := w.t2z;
for j in 0..4u {
val p := &mut w.points[j];
val rax := p.rax;
val ray := p.ray;
val raz := p.raz;
val rbx := p.rbx;
val rby := p.rby;
val rbz := p.rbz;
val dvx := vbx + (wby * rbz - wbz * rby) - vax - (way * raz - waz * ray);
val dvy := vby + (wbz * rbx - wbx * rbz) - vay - (waz * rax - wax * raz);
val dvz := vbz + (wbx * rby - wby * rbx) - vaz - (wax * ray - way * rax);
val before1 := p.tangent1;
val before2 := p.tangent2;
var sum1 := before1 - p.mass_1 * (dvx * t1x + dvy * t1y + dvz * t1z);
var sum2 := before2 - p.mass_2 * (dvx * t2x + dvy * t2y + dvz * t2z);
val most := w.friction * p.normal;
val squared := sum1 * sum1 + sum2 * sum2;
val most_squared := most * most;
val over := squared.greater(&most_squared);
val scale := select(over, most / root_of(select(over, squared, unit)), unit);
sum1 = sum1 * scale;
sum2 = sum2 * scale;
p.tangent1 = sum1;
p.tangent2 = sum2;
val d1 := sum1 - before1;
val d2 := sum2 - before2;
val px := t1x * d1 + t2x * d2;
val py := t1y * d1 + t2y * d2;
val pz := t1z * d1 + t2z * d2;
vax = vax - px * ma;
vay = vay - py * ma;
vaz = vaz - pz * ma;
vbx = vbx + px * mb;
vby = vby + py * mb;
vbz = vbz + pz * mb;
wax = wax - (p.sax * d1 + p.tax * d2);
way = way - (p.say * d1 + p.tay * d2);
waz = waz - (p.saz * d1 + p.taz * d2);
wbx = wbx + (p.sbx * d1 + p.tbx * d2);
wby = wby + (p.sby * d1 + p.tby * d2);
wbz = wbz + (p.sbz * d1 + p.tbz * d2);
}
put(vax, numbers, w.a, &keep_a);
put(vay, numbers, w.a + one, &keep_a);
put(vaz, numbers, w.a + two, &keep_a);
put(wax, numbers, w.a + four, &keep_a);
put(way, numbers, w.a + five, &keep_a);
put(waz, numbers, w.a + six, &keep_a);
put(vbx, numbers, w.b, &keep_b);
put(vby, numbers, w.b + one, &keep_b);
put(vbz, numbers, w.b + two, &keep_b);
put(wbx, numbers, w.b + four, &keep_b);
put(wby, numbers, w.b + five, &keep_b);
put(wbz, numbers, w.b + six, &keep_b);
}
}