Skip to content

Commit b6f0675

Browse files
authored
Fuse the three jacobian dot products into one horizontal reduction. (#2151)
SolveVelocityConstraintGetTotalLambda computed three independent Vec3::Dot calls and summed the scalar results. Since Dot(a,b) = sum_xyz(a*b), the sum of the dots equals a single reduction of the component-wise sum of products, so the two redundant horizontal reductions can be removed from the latency-bound critical path. Accumulate component-wise into a Vec3 and call ReduceSum() once. Performance uplift about 3% for Pyramid test with 300 iterations. Cross-platform determinism is preserved but results changed (fewer roundings), so the determinism check hashes have been regenerated.
1 parent 8c605f1 commit b6f0675

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

‎.github/workflows/determinism_check.yml‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
name: Determinism Check
22

33
env:
4-
CONVEX_VS_MESH_HASH: '0x918f27cacae9752b'
5-
HIGH_SPEED_HASH: '0x58d8d892536f4021'
6-
RAGDOLL_HASH: '0x3be6ea76c07a2ac8'
7-
PYRAMID_HASH: '0x74d0118836ac0892'
8-
CHARACTER_VIRTUAL_HASH: '0xc1d4e05b164b1221'
9-
CONVEX_VS_MESH_HASH_DOUBLE: '0x3729e7890b93a289'
10-
HIGH_SPEED_HASH_DOUBLE: '0x183f48cb94739ce'
11-
RAGDOLL_HASH_DOUBLE: '0x8dd6bc501859a228'
12-
PYRAMID_HASH_DOUBLE: '0xf07f04597a102a82'
13-
CHARACTER_VIRTUAL_HASH_DOUBLE: '0xa0b16727aa16fdb0'
4+
CONVEX_VS_MESH_HASH: '0x30ea0fd16accfad0'
5+
HIGH_SPEED_HASH: '0x64d4f332a3a94503'
6+
RAGDOLL_HASH: '0x82a4ecc9e5424430'
7+
PYRAMID_HASH: '0x7d979c44bb41a1e4'
8+
CHARACTER_VIRTUAL_HASH: '0x37d910bfeae010e8'
9+
CONVEX_VS_MESH_HASH_DOUBLE: '0x725342da41935e02'
10+
HIGH_SPEED_HASH_DOUBLE: '0xd0e6a90aa77edbf1'
11+
RAGDOLL_HASH_DOUBLE: '0x3c5e5cdf1473a471'
12+
PYRAMID_HASH_DOUBLE: '0xcd3653dde7ab6582'
13+
CHARACTER_VIRTUAL_HASH_DOUBLE: '0x9b1c94e2c4b6db97'
1414
EMSCRIPTEN_VERSION: 6.0.8
1515
NODE_VERSION: 24.x
1616
UBUNTU_CLANG_VERSION: clang++-18

‎Jolt/Physics/Constraints/ConstraintPart/ContactConstraintPart.h‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,21 +169,22 @@ class ContactConstraintPart : public ContactConstraintPart1<Type1>, public Conta
169169
JPH_INLINE float SolveVelocityConstraintGetTotalLambda(Vec3Arg inLinearVelocity1, Vec3Arg inAngularVelocity1, Vec3Arg inLinearVelocity2, Vec3Arg inAngularVelocity2, Vec3Arg inWorldSpaceAxis) const
170170
{
171171
// Calculate jacobian multiplied by linear velocity
172-
float jv;
172+
Vec3 acc;
173173
if constexpr (Type1 != EMotionType::Static && Type2 != EMotionType::Static)
174-
jv = inWorldSpaceAxis.Dot(inLinearVelocity1 - inLinearVelocity2);
174+
acc = inWorldSpaceAxis * (inLinearVelocity1 - inLinearVelocity2);
175175
else if constexpr (Type1 != EMotionType::Static)
176-
jv = inWorldSpaceAxis.Dot(inLinearVelocity1);
176+
acc = inWorldSpaceAxis * inLinearVelocity1;
177177
else if constexpr (Type2 != EMotionType::Static)
178-
jv = inWorldSpaceAxis.Dot(-inLinearVelocity2);
178+
acc = inWorldSpaceAxis * (-inLinearVelocity2);
179179
else
180180
JPH_ASSERT(false); // Static vs static is nonsensical!
181181

182-
// Calculate jacobian multiplied by angular velocity
183182
if constexpr (Type1 != EMotionType::Static)
184-
jv += Vec3::sLoadFloat3Unsafe(this->mR1PlusUxAxis).Dot(inAngularVelocity1);
183+
acc += Vec3::sLoadFloat3Unsafe(this->mR1PlusUxAxis) * inAngularVelocity1;
185184
if constexpr (Type2 != EMotionType::Static)
186-
jv -= Vec3::sLoadFloat3Unsafe(this->mR2xAxis).Dot(inAngularVelocity2);
185+
acc -= Vec3::sLoadFloat3Unsafe(this->mR2xAxis) * inAngularVelocity2;
186+
187+
float jv = acc.ReduceSum();
187188

188189
// Lagrange multiplier is:
189190
//

0 commit comments

Comments
 (0)