From 8165375c738943664d26c76826c44d305999df5d Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Fri, 9 Sep 2022 00:19:12 +0100 Subject: [PATCH 1/8] vpc_scripts: Remove jolt_static group Deprecated. --- vpc_scripts/vjolt_groups.vgc | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/vpc_scripts/vjolt_groups.vgc b/vpc_scripts/vjolt_groups.vgc index 5eec5eb..72ce8f3 100644 --- a/vpc_scripts/vjolt_groups.vgc +++ b/vpc_scripts/vjolt_groups.vgc @@ -21,16 +21,3 @@ $Group "jolt" "vphysics_jolt_sse2" "vphysics_wrapper_external" } - -$Group "jolt_static" -{ - "appframework" - "interfaces" - "tier0_static" - "tier1" - "tier2" - "mathlib" - "joltphysics" - "vphysics_jolt_static" - "vstdlib_static" -} From 2931f2e5cfd7307bdfc0028b4df132f412e52a8f Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Fri, 9 Sep 2022 00:07:08 +0000 Subject: [PATCH 2/8] ci: Copy vphysics.so -> vphysics_client.so for GMod --- .drone.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.drone.yml b/.drone.yml index ba62011..19e3477 100644 --- a/.drone.yml +++ b/.drone.yml @@ -65,6 +65,8 @@ steps: image: alpine/git commands: - cd game + - cp bin/linux64/vphysics.so bin/linux64/vphysics_client.so + - cp bin/linux64/vphysics.so.dbg bin/linux64/vphysics_client.so.dbg - git add . - git commit -m "Update binaries as of $DRONE_COMMIT" || true - git pull --rebase From 954d301a2b76604a2dec28af3083357e0906bf81 Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Fri, 9 Sep 2022 01:42:31 +0100 Subject: [PATCH 3/8] constraints: Fix refcountning of constraint pt Closes: #17 --- vphysics_jolt/vjolt_constraints.cpp | 9 ++++----- vphysics_jolt/vjolt_constraints.h | 16 ++++++++-------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/vphysics_jolt/vjolt_constraints.cpp b/vphysics_jolt/vjolt_constraints.cpp index 6e00c16..18ee551 100644 --- a/vphysics_jolt/vjolt_constraints.cpp +++ b/vphysics_jolt/vjolt_constraints.cpp @@ -151,7 +151,7 @@ void JoltPhysicsConstraint::SetLinearMotor( float speed, float maxLinearImpulse { case CONSTRAINT_SLIDING: { - JPH::SliderConstraint *pConstraint = static_cast( m_pConstraint ); + JPH::SliderConstraint *pConstraint = static_cast( m_pConstraint.GetPtr() ); pConstraint->SetMotorState( speed ? JPH::EMotorState::Velocity : JPH::EMotorState::Off ); pConstraint->SetTargetVelocity( speed ); @@ -184,7 +184,7 @@ void JoltPhysicsConstraint::SetAngularMotor( float rotSpeed, float maxAngularImp // :/ VJoltAssert( m_pConstraint->GetSubType() == JPH::EConstraintSubType::SixDOF ); - JPH::SixDOFConstraint *pConstraint = static_cast( m_pConstraint ); + JPH::SixDOFConstraint *pConstraint = static_cast( m_pConstraint.GetPtr() ); pConstraint->SetTargetAngularVelocityCS( JPH::Vec3( rotSpeed, rotSpeed, rotSpeed ) ); pConstraint->SetMaxFriction( JPH::SixDOFConstraint::EAxis::RotationX, maxAngularImpulse ); pConstraint->SetMaxFriction( JPH::SixDOFConstraint::EAxis::RotationY, maxAngularImpulse ); @@ -194,7 +194,7 @@ void JoltPhysicsConstraint::SetAngularMotor( float rotSpeed, float maxAngularImp case CONSTRAINT_HINGE: { - JPH::HingeConstraint *pConstraint = static_cast( m_pConstraint ); + JPH::HingeConstraint *pConstraint = static_cast( m_pConstraint.GetPtr() ); pConstraint->SetMotorState( rotSpeed ? JPH::EMotorState::Velocity : JPH::EMotorState::Off ); pConstraint->SetTargetAngularVelocity( rotSpeed ); @@ -494,7 +494,7 @@ void JoltPhysicsConstraint::InitialiseSliding( IPhysicsConstraintGroup *pGroup, if ( sliding.velocity ) { - JPH::SliderConstraint *pConstraint = static_cast( m_pConstraint ); + JPH::SliderConstraint *pConstraint = static_cast( m_pConstraint.GetPtr() ); pConstraint->SetMotorState( JPH::EMotorState::Velocity ); pConstraint->SetTargetVelocity( SourceToJolt::Distance( sliding.velocity ) ); } @@ -618,7 +618,6 @@ void JoltPhysicsConstraint::DestroyConstraint() if ( m_pConstraint ) { m_pPhysicsSystem->RemoveConstraint( m_pConstraint ); - m_pConstraint->Release(); m_pConstraint = nullptr; } } diff --git a/vphysics_jolt/vjolt_constraints.h b/vphysics_jolt/vjolt_constraints.h index 8821cbc..349b6e0 100644 --- a/vphysics_jolt/vjolt_constraints.h +++ b/vphysics_jolt/vjolt_constraints.h @@ -88,14 +88,14 @@ private: void DestroyConstraint(); - JoltPhysicsObject *m_pObjReference = nullptr; - JoltPhysicsObject *m_pObjAttached = nullptr; - JPH::Constraint *m_pConstraint = nullptr; - constraintType_t m_ConstraintType = CONSTRAINT_UNKNOWN; + JoltPhysicsObject *m_pObjReference = nullptr; + JoltPhysicsObject *m_pObjAttached = nullptr; + JPH::Ref< JPH::Constraint > m_pConstraint; + constraintType_t m_ConstraintType = CONSTRAINT_UNKNOWN; - JoltPhysicsConstraintGroup *m_pGroup = nullptr; + JoltPhysicsConstraintGroup *m_pGroup = nullptr; - void *m_pGameData = nullptr; - JoltPhysicsEnvironment *m_pPhysicsEnvironment = nullptr; - JPH::PhysicsSystem *m_pPhysicsSystem = nullptr; + void *m_pGameData = nullptr; + JoltPhysicsEnvironment *m_pPhysicsEnvironment = nullptr; + JPH::PhysicsSystem *m_pPhysicsSystem = nullptr; }; From 6767ca8f32edc7ebb743a5f8733b7b74a1ae82da Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Fri, 9 Sep 2022 01:59:56 +0100 Subject: [PATCH 4/8] object: Unmark body as dirty when destroyed Fixes some crashes since this was introduced. --- vphysics_jolt/vjolt_object.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vphysics_jolt/vjolt_object.cpp b/vphysics_jolt/vjolt_object.cpp index 91a5f32..a3aaec7 100644 --- a/vphysics_jolt/vjolt_object.cpp +++ b/vphysics_jolt/vjolt_object.cpp @@ -68,6 +68,8 @@ JoltPhysicsObject::~JoltPhysicsObject() for ( int i = m_destroyedListeners.Count() - 1; i >= 0; i-- ) m_destroyedListeners[ i ]->OnJoltPhysicsObjectDestroyed( this ); + m_pEnvironment->RemoveDirtyStaticBody( GetBodyID() ); + JPH::BodyInterface& bodyInterface = m_pPhysicsSystem->GetBodyInterfaceNoLock(); bodyInterface.DestroyBody( GetBodyID() ); } From cfae6a7997858db898d11a86f0ce180132485d61 Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Fri, 9 Sep 2022 02:00:20 +0100 Subject: [PATCH 5/8] constraints: Use JPH CountBits and CountTrailingZeros --- vphysics_jolt/vjolt_constraints.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vphysics_jolt/vjolt_constraints.cpp b/vphysics_jolt/vjolt_constraints.cpp index 18ee551..0dfa01e 100644 --- a/vphysics_jolt/vjolt_constraints.cpp +++ b/vphysics_jolt/vjolt_constraints.cpp @@ -285,12 +285,12 @@ static uint32 GetDegreesOfFreedom( const constraint_ragdollparams_t &ragdoll ) bool JoltPhysicsConstraint::InitialiseHingeFromRagdoll( IPhysicsConstraintGroup* pGroup, const constraint_ragdollparams_t& ragdoll ) { const uint32 uDOFMask = GetDegreesOfFreedom( ragdoll ); - const uint32 uDOFCount = popcnt( uDOFMask ); + const uint32 uDOFCount = JPH::CountBits( uDOFMask ); if ( uDOFCount != 1 ) return false; - const uint32 uDOF = tzcnt( uDOFMask ); + const uint32 uDOF = JPH::CountTrailingZeros( uDOFMask ); const Vector vecNextDOFAxis = DOFToAxis( NextDOF( uDOF ) ); matrix3x4_t refObjToWorld; From 6636409b1065008fcd7dcca9c28cba750e0e5f46 Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Fri, 9 Sep 2022 02:00:33 +0100 Subject: [PATCH 6/8] listener_contact: Use JPH CountTrailingZeros --- vphysics_jolt/vjolt_listener_contact.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vphysics_jolt/vjolt_listener_contact.h b/vphysics_jolt/vjolt_listener_contact.h index 4979d7d..cf4f095 100644 --- a/vphysics_jolt/vjolt_listener_contact.h +++ b/vphysics_jolt/vjolt_listener_contact.h @@ -552,7 +552,7 @@ private: { for ( uint32 thread = m_Mask; thread; thread &= thread - 1 ) { - const uint32 i = tzcnt( thread ); + const uint32 i = JPH::CountTrailingZeros( thread ); for ( auto &event : m_Events[ i ] ) func( event ); From 23bbdea910ae029131b5d2b04f47526dc3b66787 Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Fri, 9 Sep 2022 02:00:43 +0100 Subject: [PATCH 7/8] util: Remove our impls of popcnt and tzcnt --- vphysics_jolt/vjolt_util.h | 50 -------------------------------------- 1 file changed, 50 deletions(-) diff --git a/vphysics_jolt/vjolt_util.h b/vphysics_jolt/vjolt_util.h index 15e8d46..e6427ac 100644 --- a/vphysics_jolt/vjolt_util.h +++ b/vphysics_jolt/vjolt_util.h @@ -312,56 +312,6 @@ void ActOnSubShapes( const JPH::Shape *pShape, Func ShapeFunc ) ShapeFunc( static_cast< const ShapeType * >( UndecorateShape( pShape ) ), JPH::Mat44::sIdentity() ); } -inline uint32 popcntStep( uint32 n, uint32 mask, uint32 shift ) -{ - return ( n & mask ) + ( ( n & ~mask ) >> shift ); -} - -inline uint32 popcnt( uint32 n ) -{ - n = popcntStep(n, 0x55555555, 1); - n = popcntStep(n, 0x33333333, 2); - n = popcntStep(n, 0x0F0F0F0F, 4); - n = popcntStep(n, 0x00FF00FF, 8); - n = popcntStep(n, 0x0000FFFF, 16); - return n; -} - -inline uint32 tzcnt( uint32 n ) -{ -#if defined(_MSC_VER) && !defined(__clang__) - return _tzcnt_u32( n ); -#elif defined(__BMI__) - return __tzcnt_u32( n ); -#elif defined(__GNUC__) || defined(__clang__) - // tzcnt is encoded as rep bsf, so we can use it on all - // processors, but the behaviour of zero inputs differs: - // - bsf: zf = 1, cf = ?, result = ? - // - tzcnt: zf = 0, cf = 1, result = 32 - // We'll have to handle this case manually. - uint32 res; - uint32 tmp; - asm ( - "tzcnt %2, %0;" - "mov $32, %1;" - "test %2, %2;" - "cmovz %1, %0;" - : "=&r" (res), "=&r" (tmp) - : "r" (n) - : "cc"); - return res; -#else - uint32 r = 31; - n &= -n; - r -= ( n & 0x0000FFFF ) ? 16 : 0; - r -= ( n & 0x00FF00FF ) ? 8 : 0; - r -= ( n & 0x0F0F0F0F ) ? 4 : 0; - r -= ( n & 0x33333333 ) ? 2 : 0; - r -= ( n & 0x55555555 ) ? 1 : 0; - return n != 0 ? r : 32; -#endif -} - template< typename T, typename Value > constexpr void Erase( T &c, const Value &value ) { From 6c1ec779807223c775aefe748979466b2dd05ed0 Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Fri, 9 Sep 2022 02:10:17 +0100 Subject: [PATCH 8/8] environment: Add stub for Get/SetPerformanceSettings --- vphysics_jolt/vjolt_environment.cpp | 14 ++++++++++++-- vphysics_jolt/vjolt_environment.h | 2 ++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/vphysics_jolt/vjolt_environment.cpp b/vphysics_jolt/vjolt_environment.cpp index a1f7da3..a61a6ec 100644 --- a/vphysics_jolt/vjolt_environment.cpp +++ b/vphysics_jolt/vjolt_environment.cpp @@ -182,6 +182,8 @@ JoltBPLayerInterfaceImpl JoltPhysicsEnvironment::s_BPLayerInterface; JoltPhysicsEnvironment::JoltPhysicsEnvironment() : m_ContactListener( m_PhysicsSystem ) { + m_PerformanceParams.Defaults(); + m_PhysicsSystem.Init( kMaxBodies, kNumBodyMutexes, kMaxBodyPairs, kMaxContactConstraints, s_BPLayerInterface, JoltBroadPhaseCanCollide, JoltObjectCanCollide ); @@ -1176,12 +1178,20 @@ void JoltPhysicsEnvironment::SweepCollideable( const CPhysCollide *pCollide, con void JoltPhysicsEnvironment::GetPerformanceSettings( physics_performanceparams_t *pOutput ) const { - Log_Stub( LOG_VJolt ); + if ( pOutput ) + *pOutput = m_PerformanceParams; } void JoltPhysicsEnvironment::SetPerformanceSettings( const physics_performanceparams_t *pSettings ) { - Log_Stub( LOG_VJolt ); + if ( pSettings ) + { + m_PerformanceParams = *pSettings; + + // Normalize these values to match VPhysics behaviour. + m_PerformanceParams.minFrictionMass = Clamp( m_PerformanceParams.minFrictionMass, 1.0f, VPHYSICS_MAX_MASS ); + m_PerformanceParams.maxFrictionMass = Clamp( m_PerformanceParams.maxFrictionMass, 1.0f, VPHYSICS_MAX_MASS ); + } } //------------------------------------------------------------------------------------------------- diff --git a/vphysics_jolt/vjolt_environment.h b/vphysics_jolt/vjolt_environment.h index c6c586e..972004e 100644 --- a/vphysics_jolt/vjolt_environment.h +++ b/vphysics_jolt/vjolt_environment.h @@ -225,4 +225,6 @@ private: bool m_EnableConstraintNotify = false; mutable bool m_bActiveObjectCountFirst = true; + + physics_performanceparams_t m_PerformanceParams; };