Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-31 07:24:40 -05:00
commit c36061ea3e
64 files changed
+13419

No files matched your search

+236
View File
@@ -0,0 +1,236 @@
#! Times what `geometry` does most, each over a thousand values two thousand
#! times, against the same worked a number at a time on a `vec3` of 12 bytes:
#! a camera's frustum culling boxes and spheres, rays meeting boxes and
#! spheres, and boxes moved by matrices.
#!
#! Each pass is a function of its own, as a system is, and what it times is
#! called from elsewhere too, so nothing is inlined only for being called once:
#! the code an executable makes of a loop hangs on what is around it.
#!
#! `blossom build`, then run `malunal.geometry.bench`, as an executable
#! optimized with `-O2`: the VM works numbers one at a time either way.
import std::io;
import std::math;
import std::time;
import std::containers;
import malunal::linear;
import malunal::geometry;
# The shapes as they'd be written a number at a time, of vectors of 12 bytes.
struct old3 { var x: f32; var y: f32; var z: f32; }
struct oldp { var x: f32; var y: f32; var z: f32; var d: f32; }
struct oldb { var min: old3; var max: old3; }
struct olds { var center: old3; var radius: f32; }
struct oldr { var origin: old3; var direction: old3; }
struct oldm { var m: f32[16]; }
# Whether a box is in front of each of six planes, of the corner furthest in
# front of each, a plane at a time.
def oldb::seen(&this, planes: &oldp[6]): bool {
for i in 0..6u {
val p := planes[i];
val x := p.x < 0 ? this.min.x : this.max.x;
val y := p.y < 0 ? this.min.y : this.max.y;
val z := p.z < 0 ? this.min.z : this.max.z;
return false if p.x * x + p.y * y + p.z * z - p.d < 0;
}
return true;
}
def olds::seen(&this, planes: &oldp[6]): bool {
for i in 0..6u {
val p := planes[i];
return false if p.x * this.center.x + p.y * this.center.y + p.z * this.center.z - p.d + this.radius < 0;
}
return true;
}
# Where a ray meets a box, or -1, between the box's faces along each axis.
def oldr::hit(&this, b: &oldb): f32 {
val ix := 1 / this.direction.x;
val iy := 1 / this.direction.y;
val iz := 1 / this.direction.z;
val x0 := (b.min.x - this.origin.x) * ix;
val x1 := (b.max.x - this.origin.x) * ix;
val y0 := (b.min.y - this.origin.y) * iy;
val y1 := (b.max.y - this.origin.y) * iy;
val z0 := (b.min.z - this.origin.z) * iz;
val z1 := (b.max.z - this.origin.z) * iz;
val enter := math::max(math::max(math::min(x0, x1), math::min(y0, y1)), math::max(math::min(z0, z1), 0));
val leave := math::min(math::min(math::max(x0, x1), math::max(y0, y1)), math::max(z0, z1));
return enter <= leave ? enter : -1;
}
# Where a ray meets a sphere, or -1.
def oldr::hit(&this, s: &olds): f32 {
val mx := this.origin.x - s.center.x;
val my := this.origin.y - s.center.y;
val mz := this.origin.z - s.center.z;
val c := mx * mx + my * my + mz * mz - s.radius * s.radius;
return 0 if c <= 0;
val b := mx * this.direction.x + my * this.direction.y + mz * this.direction.z;
return -1 if b >= 0;
val a := this.direction.x * this.direction.x + this.direction.y * this.direction.y + this.direction.z * this.direction.z;
val d := b * b - a * c;
return d < 0 ? -1 : (-b - math::sqrt(d)) / a;
}
# The box holding a box a matrix moves.
def oldb::moved(&this, by: &oldm): oldb {
val m := &by.m;
val cx := (this.min.x + this.max.x) * 0.5;
val cy := (this.min.y + this.max.y) * 0.5;
val cz := (this.min.z + this.max.z) * 0.5;
val hx := (this.max.x - this.min.x) * 0.5;
val hy := (this.max.y - this.min.y) * 0.5;
val hz := (this.max.z - this.min.z) * 0.5;
val x := m[0] * cx + m[4] * cy + m[8] * cz + m[12];
val y := m[1] * cx + m[5] * cy + m[9] * cz + m[13];
val z := m[2] * cx + m[6] * cy + m[10] * cz + m[14];
val rx := math::abs(m[0]) * hx + math::abs(m[4]) * hy + math::abs(m[8]) * hz;
val ry := math::abs(m[1]) * hx + math::abs(m[5]) * hy + math::abs(m[9]) * hz;
val rz := math::abs(m[2]) * hx + math::abs(m[6]) * hy + math::abs(m[10]) * hz;
return oldb{ old3{ x - rx, y - ry, z - rz }, old3{ x + rx, y + ry, z + rz } };
}
const count: u64 = 1024;
const rounds: u64 = 2000;
# What each pass works over.
struct data {
var boxes: vector<bounds>;
var spheres: vector<sphere>;
var rays: vector<ray>;
var ms: vector<mat4>;
var moved: vector<bounds>;
var oboxes: vector<oldb>;
var ospheres: vector<olds>;
var orays: vector<oldr>;
var oms: vector<oldm>;
var omoved: vector<oldb>;
var seen: u64;
var sum: f32;
}
# How long a pass took, for each value, beside what it took before.
def report(what: strview, started: &instant, before: f64): f64 {
val ns := started.elapsed().in_nanoseconds as f64 / (count * rounds) as f64;
if before == 0 {
println($"{what}: {ns} ns");
} else {
println($"{what}: {ns} ns, before {before} ns, {before / ns}x");
}
return ns;
}
def culled_boxes_before(d: &mut data, planes: &oldp[6]): f64 {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.seen += d.oboxes[i].seen(planes) ? 1 : 0; } }
return report("frustum overlaps bounds, before", &at, 0);
}
def culled_boxes(d: &mut data, f: &frustum, before: f64): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.seen += f.overlaps(d.boxes[i]) ? 1 : 0; } }
_ = report("frustum overlaps bounds", &at, before);
}
def culled_spheres_before(d: &mut data, planes: &oldp[6]): f64 {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.seen += d.ospheres[i].seen(planes) ? 1 : 0; } }
return report("frustum overlaps sphere, before", &at, 0);
}
def culled_spheres(d: &mut data, f: &frustum, before: f64): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.seen += f.overlaps(d.spheres[i]) ? 1 : 0; } }
_ = report("frustum overlaps sphere", &at, before);
}
def boxes_hit_before(d: &mut data): f64 {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.sum += d.orays[i].hit(&d.oboxes[(i + 1) % count]); } }
return report("ray hit bounds, before", &at, 0);
}
def boxes_hit(d: &mut data, before: f64): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.sum += d.rays[i].hit(d.boxes[(i + 1) % count]) is some(t) ? t : -1; } }
_ = report("ray hit bounds", &at, before);
}
def spheres_hit_before(d: &mut data): f64 {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.sum += d.orays[i].hit(&d.ospheres[(i + 1) % count]); } }
return report("ray hit sphere, before", &at, 0);
}
def spheres_hit(d: &mut data, before: f64): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.sum += d.rays[i].hit(d.spheres[(i + 1) % count]) is some(t) ? t : -1; } }
_ = report("ray hit sphere", &at, before);
}
def moves_before(d: &mut data): f64 {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.omoved[i] = d.oboxes[i].moved(&d.oms[i]); } }
return report("bounds transformed, before", &at, 0);
}
def moves(d: &mut data, before: f64): void {
val at := instant::now();
for _ in 0..rounds { for i in 0..count { d.moved[i] = d.boxes[i].transformed(d.ms[i]); } }
_ = report("bounds transformed", &at, before);
}
# Each of what's timed, called once more, so none is called from one place.
def elsewhere(d: &mut data, f: &frustum, planes: &oldp[6]): void {
d.seen += (f.overlaps(d.boxes[1]) ? 1 : 0) + (d.oboxes[1].seen(planes) ? 1 : 0);
d.seen += (f.overlaps(d.spheres[1]) ? 1 : 0) + (d.ospheres[1].seen(planes) ? 1 : 0);
d.sum += d.orays[1].hit(&d.oboxes[2]) + d.orays[1].hit(&d.ospheres[2]);
d.sum += (d.rays[1].hit(d.boxes[2]) is some(t) ? t : -1) + (d.rays[1].hit(d.spheres[2]) is some(u) ? u : -1);
d.moved[0] = d.boxes[1].transformed(d.ms[1]);
d.omoved[0] = d.oboxes[1].moved(&d.oms[1]);
}
def old3_of(v: vec3): old3 {
return old3{ v.x, v.y, v.z };
}
def main(): void {
println($"lanes held: {lanes_held()}");
var d := data{
boxes = vector::<bounds>::new(), spheres = vector::<sphere>::new(), rays = vector::<ray>::new(), ms = vector::<mat4>::new(),
moved = vector::<bounds>::new(), oboxes = vector::<oldb>::new(), ospheres = vector::<olds>::new(),
orays = vector::<oldr>::new(), oms = vector::<oldm>::new(), omoved = vector::<oldb>::new(),
};
# A camera at the middle of a field of boxes, seeing about half of them.
val f := frustum::of(mat4::perspective(math::pi as f32 / 3, 16.0 / 9.0, 0.1, 400) * mat4::look_at(vec3{ 0, 5, 0 }, vec3{ 0, 0, -10 }));
var planes: oldp[6];
for i in 0..6u {
val p := f.planes[i];
planes[i] = oldp{ p.normal.x, p.normal.y, p.normal.z, p.distance };
}
for i in 0..count {
val at := vec3{ (i % 32) as f32 * 8 - 128, (i % 3) as f32, (i / 32) as f32 * 8 - 128 };
val b := bounds::around(at, vec3{ 1, 2, 1.5 });
d.boxes.append(b);
d.oboxes.append(oldb{ old3_of(b.min), old3_of(b.max) });
d.spheres.append(sphere{ at, 2 });
d.ospheres.append(olds{ old3_of(at), 2 });
val r := ray::toward(vec3{ 0, 5, 0 }, at);
d.rays.append(r);
d.orays.append(oldr{ old3_of(r.origin), old3_of(r.direction) });
val m := mat4::compose(at, quat::axis_angle(vec3{ 1, 2, i as f32 }, i as f32 * 0.01), vec3{ 1, 2, 1 });
d.ms.append(m);
d.oms.append(oldm{ m = m.m });
}
d.moved.resize(count);
d.omoved.resize(count);
elsewhere(&mut d, &f, &planes);
culled_boxes(&mut d, &f, culled_boxes_before(&mut d, &planes));
culled_spheres(&mut d, &f, culled_spheres_before(&mut d, &planes));
boxes_hit(&mut d, boxes_hit_before(&mut d));
spheres_hit(&mut d, spheres_hit_before(&mut d));
moves(&mut d, moves_before(&mut d));
# What was worked, so none of it is left out.
println($"checks: {d.seen} {d.sum} {d.moved[3].max.y} {d.omoved[3].max.y}");
}