diff --git a/aephysics/hull/module.ae b/aephysics/hull/module.ae index 3aca9e2..c29b29a 100644 --- a/aephysics/hull/module.ae +++ b/aephysics/hull/module.ae @@ -1279,7 +1279,9 @@ find_hull_support_vertex(h: *HullData, direction: Vec3) -> int { dz = lanes.splat4(direction.z as f32) four = lanes.splat4(4.0) index = lanes.f32x4(0.0, 1.0, 2.0, 3.0) - lowest = 0.0 - math.infinity_f32() + // -FLT_MAX as the reference starts: a constant, where infinity was a + // division every call. + lowest = 0.0 - (math.MAX_FLOAT as f32) best_dot = lanes.splat4(lowest) best_lane_index = lanes.splat4(0.0 - 1.0) loop_count = (n + 3) & (0 - 4) @@ -1292,19 +1294,29 @@ find_hull_support_vertex(h: *HullData, direction: Vec3) -> int { index = index + four i = i + 4 } - best_index = NULL_INDEX - best = lowest - lane = 0 - while lane < 4 { - lane_index = lanes.lane4(best_lane_index, lane as long) as int - lane_dot = lanes.lane4(best_dot, lane as long) - if lane_index >= 0 && (lane_dot > best || (lane_dot == best && lane_index < best_index)) { - best_index = lane_index - best = lane_dot - } - lane = lane + 1 + // The lanes read at fixed indices: lanes.lane4 takes the vector by + // value, and a loop's index made each read a spill and a reload. The + // vertex indices stay floats (whole numbers, compared exactly) until + // the one checked conversion at the end. The reference's rule: the + // greatest dot, a tie to the lower vertex. + pick = SupportPick { index: 0.0 - 1.0, dot: lowest } + pick = support_pick(pick, lanes.lane4(best_lane_index, 0), lanes.lane4(best_dot, 0)) + pick = support_pick(pick, lanes.lane4(best_lane_index, 1), lanes.lane4(best_dot, 1)) + pick = support_pick(pick, lanes.lane4(best_lane_index, 2), lanes.lane4(best_dot, 2)) + pick = support_pick(pick, lanes.lane4(best_lane_index, 3), lanes.lane4(best_dot, 3)) + return pick.index as int +} + +struct SupportPick { + index: f32 + dot: f32 +} + +support_pick(pick: SupportPick, lane_index: f32, lane_dot: f32) -> SupportPick { + if lane_index >= 0.0 && (lane_dot > pick.dot || (lane_dot == pick.dot && lane_index < pick.index)) { + return SupportPick { index: lane_index, dot: lane_dot } } - return best_index + return pick } find_hull_support_face(h: *HullData, direction: Vec3) -> int { diff --git a/aephysics/manifold/module.ae b/aephysics/manifold/module.ae index ab1b5ac..3c717f2 100644 --- a/aephysics/manifold/module.ae +++ b/aephysics/manifold/module.ae @@ -957,7 +957,9 @@ find_incident_face_f(h: *HullData, ref_normal: Vec3f, vertex_index: int) -> int normals = hull.soa_normals(h) as f32[] face_stride = hull.soa_stride(h.face_count) min_edge = NULL_INDEX - f32 min_projection = math.infinity_f32() + // FLT_MAX as the reference starts (minEdgeProjection): a constant, where + // infinity was a division every call. + f32 min_projection = math.MAX_FLOAT as f32 start = vertices[vertex_index] edge_origin = row_point(rows, stride, hull.half_edge_origin(words[start])) edge_index = start diff --git a/bench/suite.ae b/bench/suite.ae index 639d863..4114b37 100644 --- a/bench/suite.ae +++ b/bench/suite.ae @@ -159,6 +159,15 @@ run(name: string, world_id: WorldId, scene_steps: int, scene: int) { println("aephysics suite: ${name}: ${steps} steps of ${counters.body_count} bodies ${ms(t1 - t0)} ms, ${ms(t1 - t0) / timed} ms per step (height sum ${height_sum(world_id)}, ${counters.contact_count} contacts, ${counters.joint_count} joints, ${awake} awake)") println("aephysics phases ms/step: pairs ${pairs_ms / timed} collide ${collide_ms / timed}") println("aephysics stages ms/step: solve ${solve_ms / timed} transforms ${transforms_ms / timed} refit ${refit_ms / timed} sleep ${sleep_ms / timed} sensors ${sensors_ms / timed}") + // The graph's colours, each one's contacts and joints (the overflow + // last), as bench/suite_box3d.c prints the reference's. + colors = "aephysics colors:" + ci = 0 + while ci < 24 { + if counters.color_counts[ci] > 0 { colors = "${colors} ${ci}:${counters.color_counts[ci]}" } + ci = ci + 1 + } + println(colors) // The solve's own stages under AEPHYSICS_STAGES=1, beside the // reference's (bench/suite_box3d.c prints the same columns). solver.stage_report() @@ -191,6 +200,29 @@ ground_box(world_id: WorldId, y: float, hx: float, hy: float, hz: float) -> Body return body } +// --- tiny: a step with almost nothing in it ------------------------------------------------------------------ + +// Not the reference's: a ground box and eight spheres resting on it, never +// sleeping, so a step's fixed cost (the stages, the blocks, the passes over +// empty sets) is most of what is timed. bench/suite_box3d.c builds the +// same scene with the reference's calls. +tiny() { + world_id = world_with(1, 8, 1, 8, 64) + ground_box(world_id, 0.0 - 1.0, 20.0, 1.0, 20.0) + bdef = dynamics.default_body_def() + bdef.kind = dynamics.BODY_DYNAMIC + bdef.enable_sleep = false + sdef = dynamics.default_shape_def() + i = 0 + while i < 8 { + bdef.position = math.vec3(2.0 * (i as float) - 7.0, 0.5, 0.0) + body = dynamics.create_body(world_id, &bdef) + dynamics.create_sphere_shape(body, &sdef, manifold.sphere(math.vec3_zero(), 0.5)) + i = i + 1 + } + run("tiny", world_id, 20000, SCENE_NONE) +} + // --- convex pile: a huge pile of PEEL's large convexes ------------------------------------------------------ convex_pile() { @@ -885,6 +917,7 @@ main() { if wanted("trees50") { trees("trees50", 2) } if wanted("trees100") { trees("trees100", 1) } if wanted("washer") { washer() } + if wanted("tiny") { tiny() } if wanted("sat") { sat_runs() } free(g_hulls) free(g_meshes) diff --git a/bench/suite_box3d.c b/bench/suite_box3d.c index 1b746a8..d171d5f 100644 --- a/bench/suite_box3d.c +++ b/bench/suite_box3d.c @@ -127,6 +127,15 @@ static void Run( const Scene* scene ) printf( "box3d phases ms/step: pairs %.6f collide %.6f\n", pairs / timed, collide / timed ); printf( "box3d stages ms/step: solve %.6f transforms %.6f refit %.6f sleep %.6f sensors %.6f\n", solve / timed, transforms / timed, refit / timed, sleep / timed, sensors / timed ); + printf( "box3d colors:" ); + for ( int i = 0; i < 24; ++i ) + { + if ( counters.colorCounts[i] > 0 ) + { + printf( " %d:%d", i, counters.colorCounts[i] ); + } + } + printf( "\n" ); printf( "box3d solve ms/step: prepare %.6f integrate_v %.6f warm %.6f solve %.6f integrate_p %.6f relax %.6f restitution %.6f store %.6f\n", prepare / timed, integrateV / timed, warm / timed, impulses / timed, integrateP / timed, relax / timed, restitution / timed, store / timed ); @@ -143,6 +152,30 @@ static void Run( const Scene* scene ) ResetGroundShapeId(); } +// Not the reference's: a ground box and eight spheres resting on it, never +// sleeping, as bench/suite.ae's tiny builds it, so a step's fixed cost is +// most of what is timed. +static void CreateTiny( b3WorldId worldId ) +{ + b3BodyDef groundDef = b3DefaultBodyDef(); + groundDef.position = (b3Pos){ 0.0f, -1.0f, 0.0f }; + b3BodyId groundId = b3CreateBody( worldId, &groundDef ); + b3BoxHull box = b3MakeBoxHull( 20.0f, 1.0f, 20.0f ); + b3ShapeDef shapeDef = b3DefaultShapeDef(); + b3CreateHullShape( groundId, &shapeDef, &box.base ); + + b3BodyDef bodyDef = b3DefaultBodyDef(); + bodyDef.type = b3_dynamicBody; + bodyDef.enableSleep = false; + b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.5f }; + for ( int i = 0; i < 8; ++i ) + { + bodyDef.position = (b3Pos){ 2.0f * i - 7.0f, 0.5f, 0.0f }; + b3BodyId bodyId = b3CreateBody( worldId, &bodyDef ); + b3CreateSphereShape( bodyId, &shapeDef, &sphere ); + } +} + // benchmark/main.c's RunSatBenchmarks: each hull pair's GJK cold and warm // and SAT with and without the inscribed-sphere bound, the best of four // runs of twenty repeats, in microseconds a query. @@ -193,6 +226,7 @@ int main( void ) { "trees50", NULL, CreateTrees50, DestroyTrees, NULL, 500 }, { "trees100", NULL, CreateTrees100, DestroyTrees, NULL, 500 }, { "washer", GetWasherCapacity, CreateWasher, NULL, NULL, 1000 }, + { "tiny", NULL, CreateTiny, NULL, NULL, 20000 }, }; const char* asked = getenv( "AEPHYSICS_BENCH_SCENE" );