diff --git a/aephysics/dynamics/module.ae b/aephysics/dynamics/module.ae index 9561292..098e681 100644 --- a/aephysics/dynamics/module.ae +++ b/aephysics/dynamics/module.ae @@ -213,7 +213,7 @@ const JOINT_SPHERICAL = 6 const JOINT_WELD = 7 const JOINT_WHEEL = 8 const TIME_TO_SLEEP = 0.5 -const JOINT_DATA_BYTES = 704 // JointSim.data: room for the largest kind's data (the spherical joint's 704) +const JOINT_DATA_BYTES = 744 // JointSim.data: room for the largest kind's data (the spherical joint's 744) // The shape kinds (the shape module's, restated). const KIND_SPHERE = 0 const KIND_CAPSULE = 1 @@ -1078,6 +1078,13 @@ struct SphericalJoint { twist_limit_hertz: float twist_limit_damping_ratio: float twist_limit_softness: Softness // made each prepare, as the joint's limit_softness + // The point's anchors (frame A's and B's origins, world rotation, + // relative to the centres of mass) and the centres' offset, single + // precision as the reference keeps them: a plain ball joint's point + // is solved in single precision from these (spherical_point_f). + anchor_a: Vec3f + anchor_b: Vec3f + delta_center_f: Vec3f } struct WeldJoint { @@ -1155,7 +1162,7 @@ struct JointSim { limit_softness: Softness // the limits' rows': their own (#226) or the constraint softness, sized to the joint's load (#213) force_threshold: float // the solve's joint event test torque_threshold: float - data: long[88] // JOINT_DATA_BYTES + data: long[93] // JOINT_DATA_BYTES joint_id: int body_id_a: int body_id_b: int diff --git a/aephysics/joint_solver/module.ae b/aephysics/joint_solver/module.ae index e7d9aa6..faf9d4b 100644 --- a/aephysics/joint_solver/module.ae +++ b/aephysics/joint_solver/module.ae @@ -178,6 +178,39 @@ skew_times(r: Vec3, c: Vec3) -> Vec3 { return math.vec3((0.0 - r.z) * c.y + r.y * c.z, r.z * c.x + (0.0 - r.x) * c.z, (0.0 - r.y) * c.x + r.x * c.y) } +// The same in single precision, for the plain ball joint's point. +skew_sandwich_f(i: Matrix3f, r: Vec3f) -> Matrix3f { + f32 zero = 0.0 + mx = math.vec3f(i.cy.x * r.z + i.cz.x * (zero - r.y), i.cy.y * r.z + i.cz.y * (zero - r.y), i.cy.z * r.z + i.cz.z * (zero - r.y)) + my = math.vec3f(i.cx.x * (zero - r.z) + i.cz.x * r.x, i.cx.y * (zero - r.z) + i.cz.y * r.x, i.cx.z * (zero - r.z) + i.cz.z * r.x) + mz = math.vec3f(i.cx.x * r.y + i.cy.x * (zero - r.x), i.cx.y * r.y + i.cy.y * (zero - r.x), i.cx.z * r.y + i.cy.z * (zero - r.x)) + return Matrix3f { cx: skew_times_f(r, mx), cy: skew_times_f(r, my), cz: skew_times_f(r, mz) } +} +skew_times_f(r: Vec3f, c: Vec3f) -> Vec3f { + f32 zero = 0.0 + return math.vec3f((zero - r.z) * c.y + r.y * c.z, r.z * c.x + (zero - r.x) * c.z, (zero - r.y) * c.x + r.x * c.y) +} +point_mass_matrix_f(m_a: f32, i_a: Matrix3f, r_a: Vec3f, m_b: f32, i_b: Matrix3f, r_b: Vec3f) -> Matrix3f { + k_a = skew_sandwich_f(i_a, r_a) + k_b = skew_sandwich_f(i_b, r_b) + k = Matrix3f { cx: math.negf(math.addf(k_a.cx, k_b.cx)), cy: math.negf(math.addf(k_a.cy, k_b.cy)), cz: math.negf(math.addf(k_a.cz, k_b.cz)) } + m = m_a + m_b + k.cx.x = k.cx.x + m + k.cy.y = k.cy.y + m + k.cz.z = k.cz.z + m + return k +} +// m^-1 a, zero for a singular m (b3Solve3). +solve3_f(m: Matrix3f, a: Vec3f) -> Vec3f { + d = math.detf(m) + if math.abs_f32(d) > (1000.0 * math.TINY) as f32 { + f32 one = 1.0 + inv = one / d + return math.vec3f(inv * math.dotf(math.crossf(m.cy, m.cz), a), inv * math.dotf(math.crossf(m.cz, m.cx), a), inv * math.dotf(math.crossf(m.cx, m.cy), a)) + } + return math.vec3f(0.0, 0.0, 0.0) +} + // The point constraint's mass matrix: (mA + mB) I - skew(rA) iA skew(rA) - skew(rB) iB skew(rB). point_mass_matrix(m_a: float, i_a: Matrix3, r_a: Vec3, m_b: float, i_b: Matrix3, r_b: Vec3) -> Matrix3 { k_a = skew_sandwich(&i_a, r_a) @@ -1710,6 +1743,9 @@ prepare_spherical_joint(base: *JointSim, ctx: *StepContext) { j.frame_a = world_frame(bodies.sim_a, base.local_frame_a) j.frame_b = world_frame(bodies.sim_b, base.local_frame_b) j.delta_center = math.sub(bodies.sim_b.center, bodies.sim_a.center) + j.anchor_a = math.narrow3(j.frame_a.p) + j.anchor_b = math.narrow3(j.frame_b.p) + j.delta_center_f = math.narrow3(j.delta_center) inertia_sum = math.add_mm(math.widen_mat3(base.inv_inertia_a), math.widen_mat3(base.inv_inertia_b)) spherical_angular_rows(j, j.frame_a.q, j.frame_b.q, inertia_sum) // The rotation's mass and the spring's softness only for the rows that @@ -1812,8 +1848,77 @@ spherical_rows_where_now(j: *SphericalJoint, quat_a: Quat, quat_b: Quat, cone_ga if j.lock_cone_minor { spherical_lock_row(j, quat_a, quat_b, inertia_sum) } } +// A ball joint with nothing but its point: no spring, motor, limits or lock. +spherical_is_plain(j: *SphericalJoint) -> bool { + return (j.enable_spring || j.enable_motor || j.enable_cone_limit || j.enable_twist_limit || j.lock_cone_minor) == false +} + +// The plain ball joint's warm start and solve in single precision, as the +// reference's b3WarmStartSphericalJoint and b3SolveSphericalJoint do the +// point: the anchors and the centres' offset made in the prepare, the +// inertias as the body sims keep them, the impulse kept in double but +// always a single's value. +warm_start_spherical_point_f(base: *JointSim, ctx: *StepContext, j: *SphericalJoint) { + state_a = state_of(ctx, j.index_a) + state_b = state_of(ctx, j.index_b) + r_a = math.rotate_vectorf(state_a.dq, j.anchor_a) + r_b = math.rotate_vectorf(state_b.dq, j.anchor_b) + p = math.narrow3(j.linear_impulse) + f32 m_a = base.inv_mass_a as f32 + f32 m_b = base.inv_mass_b as f32 + if (state_a.flags & dynamics.DYNAMIC_FLAG) != 0 { + state_a.v = math.mul_subf(state_a.v, m_a, p) + state_a.w = math.subf(state_a.w, math.mul_mvf(base.inv_inertia_a, math.crossf(r_a, p))) + } + if (state_b.flags & dynamics.DYNAMIC_FLAG) != 0 { + state_b.v = math.mul_addf(state_b.v, m_b, p) + state_b.w = math.addf(state_b.w, math.mul_mvf(base.inv_inertia_b, math.crossf(r_b, p))) + } +} + +solve_spherical_point_f(base: *JointSim, ctx: *StepContext, j: *SphericalJoint, use_bias: bool) { + state_a = state_of(ctx, j.index_a) + state_b = state_of(ctx, j.index_b) + f32 m_a = base.inv_mass_a as f32 + f32 m_b = base.inv_mass_b as f32 + v_a = state_a.v + w_a = state_a.w + v_b = state_b.v + w_b = state_b.w + r_a = math.rotate_vectorf(state_a.dq, j.anchor_a) + r_b = math.rotate_vectorf(state_b.dq, j.anchor_b) + cdot = math.subf(math.subf(math.addf(v_b, math.crossf(w_b, r_b)), v_a), math.crossf(w_a, r_a)) + bias = math.vec3f(0.0, 0.0, 0.0) + f32 mass_scale = 1.0 + f32 impulse_scale = 0.0 + if use_bias { + separation = math.addf(math.addf(math.subf(state_b.dp, state_a.dp), math.subf(r_b, r_a)), j.delta_center_f) + bias = math.mul_svf(base.constraint_softness.bias_rate as f32, separation) + mass_scale = base.constraint_softness.mass_scale as f32 + impulse_scale = base.constraint_softness.impulse_scale as f32 + } + k = point_mass_matrix_f(m_a, base.inv_inertia_a, r_a, m_b, base.inv_inertia_b, r_b) + b = solve3_f(k, math.addf(cdot, bias)) + old = math.narrow3(j.linear_impulse) + f32 zero = 0.0 + impulse = math.mul_subf(math.mul_svf(zero - mass_scale, b), impulse_scale, old) + j.linear_impulse = math.widen3(math.addf(old, impulse)) + if (state_a.flags & dynamics.DYNAMIC_FLAG) != 0 { + state_a.v = math.mul_subf(v_a, m_a, impulse) + state_a.w = math.subf(w_a, math.mul_mvf(base.inv_inertia_a, math.crossf(r_a, impulse))) + } + if (state_b.flags & dynamics.DYNAMIC_FLAG) != 0 { + state_b.v = math.mul_addf(v_b, m_b, impulse) + state_b.w = math.addf(w_b, math.mul_mvf(base.inv_inertia_b, math.crossf(r_b, impulse))) + } +} + warm_start_spherical_joint(base: *JointSim, ctx: *StepContext) { j = dynamics.spherical_joint_of(base) + if spherical_is_plain(j) { + warm_start_spherical_point_f(base, ctx, j) + return + } state_a = state_of(ctx, j.index_a) state_b = state_of(ctx, j.index_b) r_a = math.rotate_vector(math.widen_quat(state_a.dq), j.frame_a.p) @@ -1833,6 +1938,11 @@ warm_start_spherical_joint(base: *JointSim, ctx: *StepContext) { // target angular velocity, the twist limits about frame B's z axis, // the cone limit about frame A's, and the point-to-point. solve_spherical_joint(base: *JointSim, ctx: *StepContext, use_bias: bool) { + plain = dynamics.spherical_joint_of(base) + if spherical_is_plain(plain) { + solve_spherical_point_f(base, ctx, plain, use_bias) + return + } m_a = base.inv_mass_a m_b = base.inv_mass_b i_a = math.widen_mat3(base.inv_inertia_a) diff --git a/aephysics/snapshot/module.ae b/aephysics/snapshot/module.ae index 0f861b0..38d5fcf 100644 --- a/aephysics/snapshot/module.ae +++ b/aephysics/snapshot/module.ae @@ -44,7 +44,7 @@ extern memcpy(dst: ptr, src: ptr, size: int) -> ptr extern memset(block: ptr, value: int, size: int) -> ptr const SNAPSHOT_MAGIC = 0x4E535041 // "APSN", little-endian -const SNAPSHOT_VERSION = 11 +const SNAPSHOT_VERSION = 12 const NULL_INDEX = 0 - 1 // --- the bytes written ------------------------------------------------------------------------------- diff --git a/bench/physics_world_box3d.c b/bench/physics_world_box3d.c index 72f8303..5ab3ba3 100644 --- a/bench/physics_world_box3d.c +++ b/bench/physics_world_box3d.c @@ -115,7 +115,7 @@ static void run( const char* name, b3WorldId worldId, int steps ) } double t1 = now_ms(); b3Counters counters = b3World_GetCounters( worldId ); - printf( "box3d physics_world: %s: %d steps of %d bodies %.1f ms, %.3f ms per step (height sum %.3f, %d contacts, %d joints)\n", name, + printf( "box3d physics_world: %s: %d steps of %d bodies %.3f ms, %.6f ms per step (height sum %.3f, %d contacts, %d joints)\n", name, steps, counters.bodyCount, t1 - t0, ( t1 - t0 ) / steps, heightSum( worldId, g_ids, g_idCount ), counters.contactCount, counters.jointCount ); print_profile( name, sum, steps ); diff --git a/bench/suite.ae b/bench/suite.ae index dc8da64..4898a9b 100644 --- a/bench/suite.ae +++ b/bench/suite.ae @@ -109,11 +109,29 @@ scene_step(scene: int, world_id: WorldId, step: int) { else if scene == SCENE_SLEEP { step_sleep(world_id) } } -run(name: string, world_id: WorldId, steps: int, scene: int) { +// AEPHYSICS_BENCH_STEPS=n runs every scene n steps instead of its own +// count: a profile of a scene whose setup outweighs its steps (large +// world's million static bodies) then samples the steps. +run_steps(steps: int) -> int { + asked = os.getenv("AEPHYSICS_BENCH_STEPS") + if asked != null { + n, err = string.to_int(asked) + if err == "" && n > 1 { return n } + } + return steps +} + +run(name: string, world_id: WorldId, scene_steps: int, scene: int) { + steps = run_steps(scene_steps) scene_step(scene, world_id, 0) physics_world.step(world_id, TIME_STEP, SUB_STEPS) pairs_ms = 0.0 collide_ms = 0.0 + solve_ms = 0.0 + transforms_ms = 0.0 + refit_ms = 0.0 + sleep_ms = 0.0 + sensors_ms = 0.0 t0 = clock() s = 1 while s < steps { @@ -122,6 +140,11 @@ run(name: string, world_id: WorldId, steps: int, scene: int) { seen = physics_world.get_profile(world_id) pairs_ms = pairs_ms + seen.pairs collide_ms = collide_ms + seen.collide + solve_ms = solve_ms + seen.solve + transforms_ms = transforms_ms + seen.transforms + refit_ms = refit_ms + seen.refit + sleep_ms = sleep_ms + seen.sleep_islands + sensors_ms = sensors_ms + seen.sensors s = s + 1 } t1 = clock() @@ -130,6 +153,7 @@ run(name: string, world_id: WorldId, steps: int, scene: int) { awake = physics_world.get_awake_body_count(world_id) 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}") spinner_joint = g_spinner as JointId[] if spinner_joint[0].index1 != 0 { println("aephysics spinner angle ${joint_solver.revolute_joint_get_angle(spinner_joint[0])}") diff --git a/bench/suite_box3d.c b/bench/suite_box3d.c index 0c9dd37..45c2e9a 100644 --- a/bench/suite_box3d.c +++ b/bench/suite_box3d.c @@ -74,9 +74,17 @@ static void Run( const Scene* scene ) } b3World_Step( worldId, timeStep, subStepCount ); - double pairs = 0.0, collide = 0.0; + // AEPHYSICS_BENCH_STEPS=n: every scene n steps (bench/suite.ae's run_steps). + int steps = scene->steps; + const char* askedSteps = getenv( "AEPHYSICS_BENCH_STEPS" ); + if ( askedSteps != NULL && atoi( askedSteps ) > 1 ) + { + steps = atoi( askedSteps ); + } + + double pairs = 0.0, collide = 0.0, solve = 0.0, transforms = 0.0, refit = 0.0, sleep = 0.0, sensors = 0.0; double t0 = now_ms(); - for ( int stepIndex = 1; stepIndex < scene->steps; ++stepIndex ) + for ( int stepIndex = 1; stepIndex < steps; ++stepIndex ) { if ( scene->step != NULL ) { @@ -86,15 +94,22 @@ static void Run( const Scene* scene ) b3Profile p = b3World_GetProfile( worldId ); pairs += p.pairs; collide += p.collide; + solve += p.solve; + transforms += p.transforms; + refit += p.refit; + sleep += p.sleepIslands; + sensors += p.sensors; } double t1 = now_ms(); - double timed = scene->steps - 1; + double timed = steps - 1; b3Counters counters = b3World_GetCounters( worldId ); - printf( "box3d suite: %s: %d steps of %d bodies %.1f ms, %.3f ms per step (height sum %.3f, %d contacts, %d joints, %d awake)\n", - scene->name, scene->steps, counters.bodyCount, t1 - t0, ( t1 - t0 ) / timed, HeightSum( worldId ), counters.contactCount, + printf( "box3d suite: %s: %d steps of %d bodies %.3f ms, %.6f ms per step (height sum %.3f, %d contacts, %d joints, %d awake)\n", + scene->name, steps, counters.bodyCount, t1 - t0, ( t1 - t0 ) / timed, HeightSum( worldId ), counters.contactCount, counters.jointCount, b3World_GetAwakeBodyCount( worldId ) ); 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 ); if ( strcmp( scene->name, "spinner" ) == 0 ) { printf( "box3d spinner angle %.6f\n", GetSpinnerAngle() ); diff --git a/scripts/profile.sh b/scripts/profile.sh index 1d89c65..a94fd92 100755 --- a/scripts/profile.sh +++ b/scripts/profile.sh @@ -22,6 +22,15 @@ out="target/profile" cflags="$(sed -n 's/^cflags = "\(.*\)"/\1/p' aether.toml)" # aether.toml's ${AETHER_AEPHYSICS_CFLAGS}, expanded as ae expands it (empty when unset). cflags="${cflags//'${AETHER_AEPHYSICS_CFLAGS}'/${AETHER_AEPHYSICS_CFLAGS:-}}" +# The inlining budget ae build gives an optimising build since Aether +# 0.937.0 (aether#3779) when the project names none: without it this +# build kept the small maths (mul_quat, make_matrix_from_quat) out of +# line where ae build's own inlines them, and its profile showed calls +# the shipped program never makes. +case "$cflags" in + *inline-unit-growth*) ;; + *) cflags="$cflags --param inline-unit-growth=100" ;; +esac # The compile flags ae gives a C build, whole: since Aether 0.795 the std and # runtime directories come as `-idirafter