#! Contains how a space solves its contacts eight at a time, as Box2D's solver #! does: 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. import std::containers; import std::math; import malunal::linear; module malunal::physics2d; # 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; # 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, and the # impulses pushing them apart and along. A lane of a contact of one point has # a second of no mass, which pushes nothing. struct wide_point { var rax: lanes; var ray: lanes; var rbx: lanes; var rby: lanes; var base: lanes; var normal: lanes; var tangent: lanes; var most: lanes; var normal_mass: lanes; var tangent_mass: lanes; } # Eight contacts as lanes: where each body's state begins among the states' # numbers, the normal, friction, each body's inverse mass and inertia, and how # soft each is. A lane of no contact is of the state of what doesn't move, and # pushes nothing. struct wide_contact { var a: lane_places; var b: lane_places; var nx: lanes; var ny: lanes; var friction: lanes; var ma: lanes; var ia: lanes; var mb: lanes; var ib: lanes; var bias_rate: lanes; var mass_scale: lanes; var impulse_scale: lanes; var points: wide_point[2]; var of: u32[8]; # Each lane's constraint, or none. } # The states' numbers, eight to a state, as `wide_contact`'s lanes gather them. def numbers_of(s: &mut solver_scratch): slice { return slice::::over(s.states.data as *mut f32, s.states.length * 8); } # 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.ia > 0 ? c.a as u64 : 0u; val b := c.mb > 0 || c.ib > 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); } } # Each color's lanes, a group of eight after another. 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]++; pack(&mut s.wide[(first[color as u64] as u64 + at as u64 / width)], at as u64 % width, &s.constraints[k], k as u32); } if experiment { s.overflow.clear(); for g in 0..s.wide.length { for lane in 0..width { if s.wide[g].of[lane] != none { s.overflow.append(s.wide[g].of[lane]); } } } s.wide.clear(); } } const experiment: bool = false; # Puts a contact into a lane. def pack(w: &mut wide_contact, lane: u64, c: &contact_constraint, k: u32): void { w.of[lane] = k; w.a[lane] = (c.a * 8) as i32; w.b[lane] = (c.b * 8) as i32; w.nx[lane] = c.normal.x; w.ny[lane] = c.normal.y; w.friction[lane] = c.friction; w.ma[lane] = c.ma; w.ia[lane] = c.ia; w.mb[lane] = c.mb; w.ib[lane] = c.ib; 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]; w.points[j].rax[lane] = p.anchor_a.x; w.points[j].ray[lane] = p.anchor_a.y; w.points[j].rbx[lane] = p.anchor_b.x; w.points[j].rby[lane] = p.anchor_b.y; w.points[j].base[lane] = p.base_separation; w.points[j].normal[lane] = p.normal_impulse; w.points[j].tangent[lane] = p.tangent_impulse; w.points[j].normal_mass[lane] = p.normal_mass; w.points[j].tangent_mass[lane] = p.tangent_mass; } } # 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 { s.constraints[k as u64].points[j].normal_impulse = s.wide[g].points[j].normal[lane]; s.constraints[k as u64].points[j].tangent_impulse = s.wide[g].points[j].tangent[lane]; s.constraints[k as u64].points[j].max_impulse = math::max(s.constraints[k as u64].points[j].max_impulse, s.wide[g].points[j].most[lane]); } } } } # 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); } } # Which lanes push a body, by its inverse mass and inertia: 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, i: lanes): bool[8] { val ms := m.lanes(); val turns := i.lanes(); var made: bool[8]; for k in 0..width { made[k] = ms[k] > 0 || turns[k] > 0; } return made; } # Writes each pushed lane's number back to where it was gathered from. def put(v: lanes, into: slice, 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 groups := s.wide.slice; for g in from..to { val w := &groups[g]; val keep_a := pushed(w.ma, w.ia); val keep_b := pushed(w.mb, w.ib); val a1 := w.a + one; val a2 := w.a + two; val b1 := w.b + one; val b2 := w.b + two; var vax := lanes::gather(view, w.a); var vay := lanes::gather(view, a1); var wa := lanes::gather(view, a2); var vbx := lanes::gather(view, w.b); var vby := lanes::gather(view, b1); var wb := lanes::gather(view, b2); val tx := w.ny; val ty := -w.nx; for j in 0..2u { val p := &w.points[j]; val px := w.nx * p.normal + tx * p.tangent; val py := w.ny * p.normal + ty * p.tangent; wa = wa - w.ia * (p.rax * py - p.ray * px); vax = vax - px * w.ma; vay = vay - py * w.ma; wb = wb + w.ib * (p.rbx * py - p.rby * px); vbx = vbx + px * w.mb; vby = vby + py * w.mb; } put(vax, numbers, w.a, &keep_a); put(vay, numbers, a1, &keep_a); put(wa, numbers, a2, &keep_a); put(vbx, numbers, w.b, &keep_b); put(vby, numbers, b1, &keep_b); put(wb, numbers, b2, &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 seven := lane_places::splat(7); val zero := lanes::splat(0); val unit := lanes::splat(1); 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, w.ia); val keep_b := pushed(w.mb, w.ib); val a1 := w.a + one; val a2 := w.a + two; val b1 := w.b + one; val b2 := w.b + two; var vax := lanes::gather(view, w.a); var vay := lanes::gather(view, a1); var wa := lanes::gather(view, a2); var vbx := lanes::gather(view, w.b); var vby := lanes::gather(view, b1); var wb := lanes::gather(view, b2); val dpx := lanes::gather(view, w.b + four) - lanes::gather(view, w.a + four); val dpy := lanes::gather(view, w.b + five) - lanes::gather(view, w.a + five); val qac := lanes::gather(view, w.a + six); val qas := lanes::gather(view, w.a + seven); val qbc := lanes::gather(view, w.b + six); val qbs := lanes::gather(view, w.b + seven); val nx := w.nx; val ny := w.ny; val ma := w.ma; val ia := w.ia; val mb := w.mb; val ib := w.ib; for j in 0..2u { val p := &mut w.points[j]; val rax := p.rax; val ray := p.ray; val rbx := p.rbx; val rby := p.rby; val dx := dpx + (qbc * rbx - qbs * rby) - (qac * rax - qas * ray); val dy := dpy + (qbs * rbx + qbc * rby) - (qas * rax + qac * ray); val separation := dx * nx + dy * ny + p.base; # Apart, only what closes the gap this substep; else, pushing softly. 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); } val vn := (vbx - wb * rby - vax + wa * ray) * nx + (vby + wb * rbx - vay - wa * rax) * ny; val before := p.normal; val sum := (before - p.normal_mass * mass_scale * (vn + bias) - impulse_scale * before).max(&zero); val impulse := sum - before; p.normal = sum; p.most = p.most.max(&impulse); val px := nx * impulse; val py := ny * impulse; vax = vax - px * ma; vay = vay - py * ma; wa = wa - ia * (rax * py - ray * px); vbx = vbx + px * mb; vby = vby + py * mb; wb = wb + ib * (rbx * py - rby * px); } val tx := ny; val ty := -nx; for j in 0..2u { val p := &mut w.points[j]; val rax := p.rax; val ray := p.ray; val rbx := p.rbx; val rby := p.rby; val vt := (vbx - wb * rby - vax + wa * ray) * tx + (vby + wb * rbx - vay - wa * rax) * ty; val most := w.friction * p.normal; val before := p.tangent; val sum := (before - p.tangent_mass * vt).min(&most).max(&-most); val impulse := sum - before; p.tangent = sum; val px := tx * impulse; val py := ty * impulse; vax = vax - px * ma; vay = vay - py * ma; wa = wa - ia * (rax * py - ray * px); vbx = vbx + px * mb; vby = vby + py * mb; wb = wb + ib * (rbx * py - rby * px); } put(vax, numbers, w.a, &keep_a); put(vay, numbers, a1, &keep_a); put(wa, numbers, a2, &keep_a); put(vbx, numbers, w.b, &keep_b); put(vby, numbers, b1, &keep_b); put(wb, numbers, b2, &keep_b); } }