add Bullet profiling hooks for physics simulation

This commit is contained in:
Andrew Meadows 2015-04-02 15:20:16 -07:00
parent a824403132
commit 27d6d9f720
4 changed files with 81 additions and 53 deletions

View file

@ -257,6 +257,7 @@ btPairCachingGhostObject* CharacterController::getGhostObject() {
} }
bool CharacterController::recoverFromPenetration(btCollisionWorld* collisionWorld) { bool CharacterController::recoverFromPenetration(btCollisionWorld* collisionWorld) {
BT_PROFILE("recoverFromPenetration");
// Here we must refresh the overlapping paircache as the penetrating movement itself or the // Here we must refresh the overlapping paircache as the penetrating movement itself or the
// previous recovery iteration might have used setWorldTransform and pushed us into an object // previous recovery iteration might have used setWorldTransform and pushed us into an object
// that is not in the previous cache contents from the last timestep, as will happen if we // that is not in the previous cache contents from the last timestep, as will happen if we
@ -355,6 +356,7 @@ bool CharacterController::recoverFromPenetration(btCollisionWorld* collisionWorl
void CharacterController::scanDown(btCollisionWorld* world) { void CharacterController::scanDown(btCollisionWorld* world) {
BT_PROFILE("scanDown");
// we test with downward raycast and if we don't find floor close enough then turn on "hover" // we test with downward raycast and if we don't find floor close enough then turn on "hover"
btKinematicClosestNotMeRayResultCallback callback(_ghostObject); btKinematicClosestNotMeRayResultCallback callback(_ghostObject);
callback.m_collisionFilterGroup = getGhostObject()->getBroadphaseHandle()->m_collisionFilterGroup; callback.m_collisionFilterGroup = getGhostObject()->getBroadphaseHandle()->m_collisionFilterGroup;
@ -374,6 +376,7 @@ void CharacterController::scanDown(btCollisionWorld* world) {
} }
void CharacterController::stepUp(btCollisionWorld* world) { void CharacterController::stepUp(btCollisionWorld* world) {
BT_PROFILE("stepUp");
// phase 1: up // phase 1: up
// compute start and end // compute start and end
@ -440,6 +443,7 @@ void CharacterController::updateTargetPositionBasedOnCollision(const btVector3&
} }
void CharacterController::stepForward(btCollisionWorld* collisionWorld, const btVector3& movement) { void CharacterController::stepForward(btCollisionWorld* collisionWorld, const btVector3& movement) {
BT_PROFILE("stepForward");
// phase 2: forward // phase 2: forward
_targetPosition = _currentPosition + movement; _targetPosition = _currentPosition + movement;
@ -496,6 +500,7 @@ void CharacterController::stepForward(btCollisionWorld* collisionWorld, const bt
} }
void CharacterController::stepDown(btCollisionWorld* collisionWorld, btScalar dt) { void CharacterController::stepDown(btCollisionWorld* collisionWorld, btScalar dt) {
BT_PROFILE("stepDown");
// phase 3: down // phase 3: down
// //
// The "stepDown" phase first makes a normal sweep down that cancels the lift from the "stepUp" phase. // The "stepDown" phase first makes a normal sweep down that cancels the lift from the "stepUp" phase.
@ -607,6 +612,7 @@ void CharacterController::warp(const btVector3& origin) {
void CharacterController::preStep(btCollisionWorld* collisionWorld) { void CharacterController::preStep(btCollisionWorld* collisionWorld) {
BT_PROFILE("preStep");
if (!_enabled) { if (!_enabled) {
return; return;
} }
@ -627,6 +633,7 @@ void CharacterController::preStep(btCollisionWorld* collisionWorld) {
} }
void CharacterController::playerStep(btCollisionWorld* collisionWorld, btScalar dt) { void CharacterController::playerStep(btCollisionWorld* collisionWorld, btScalar dt) {
BT_PROFILE("playerStep");
if (!_enabled) { if (!_enabled) {
return; // no motion return; // no motion
} }
@ -875,6 +882,7 @@ void CharacterController::updateShapeIfNecessary() {
} }
void CharacterController::preSimulation(btScalar timeStep) { void CharacterController::preSimulation(btScalar timeStep) {
BT_PROFILE("preSimulation");
if (_enabled && _dynamicsWorld) { if (_enabled && _dynamicsWorld) {
glm::quat rotation = _avatarData->getOrientation(); glm::quat rotation = _avatarData->getOrientation();
_currentUp = quatRotate(glmToBullet(rotation), LOCAL_UP_AXIS); _currentUp = quatRotate(glmToBullet(rotation), LOCAL_UP_AXIS);
@ -897,6 +905,7 @@ void CharacterController::preSimulation(btScalar timeStep) {
} }
void CharacterController::postSimulation() { void CharacterController::postSimulation() {
BT_PROFILE("postSimulation");
if (_enabled && _ghostObject) { if (_enabled && _ghostObject) {
const btTransform& avatarTransform = _ghostObject->getWorldTransform(); const btTransform& avatarTransform = _ghostObject->getWorldTransform();
glm::quat rotation = bulletToGLM(avatarTransform.getRotation()); glm::quat rotation = bulletToGLM(avatarTransform.getRotation());

View file

@ -155,6 +155,7 @@ void PhysicsEngine::clearEntitiesInternal() {
// end EntitySimulation overrides // end EntitySimulation overrides
void PhysicsEngine::relayIncomingChangesToSimulation() { void PhysicsEngine::relayIncomingChangesToSimulation() {
BT_PROFILE("incomingChanges");
// process incoming changes // process incoming changes
QSet<ObjectMotionState*>::iterator stateItr = _incomingChanges.begin(); QSet<ObjectMotionState*>::iterator stateItr = _incomingChanges.begin();
while (stateItr != _incomingChanges.end()) { while (stateItr != _incomingChanges.end()) {
@ -287,7 +288,10 @@ void PhysicsEngine::init(EntityEditPacketSender* packetSender) {
} }
void PhysicsEngine::stepSimulation() { void PhysicsEngine::stepSimulation() {
{
lock(); lock();
CProfileManager::Reset();
BT_PROFILE("stepSimulation");
// NOTE: the grand order of operations is: // NOTE: the grand order of operations is:
// (1) pull incoming changes // (1) pull incoming changes
// (2) step simulation // (2) step simulation
@ -323,6 +327,7 @@ void PhysicsEngine::stepSimulation() {
// TODO: make all of this harvest stuff into one function: relayOutgoingChanges() // TODO: make all of this harvest stuff into one function: relayOutgoingChanges()
if (numSubsteps > 0) { if (numSubsteps > 0) {
BT_PROFILE("postSimulation");
// This is step (3) which is done outside of stepSimulation() so we can lock _entityTree. // This is step (3) which is done outside of stepSimulation() so we can lock _entityTree.
// //
// Unfortunately we have to unlock the simulation (above) before we try to lock the _entityTree // Unfortunately we have to unlock the simulation (above) before we try to lock the _entityTree
@ -345,8 +350,14 @@ void PhysicsEngine::stepSimulation() {
computeCollisionEvents(); computeCollisionEvents();
} }
} }
if (_dumpNextStats) {
_dumpNextStats = false;
CProfileManager::dumpAll();
}
}
void PhysicsEngine::stepNonPhysicalKinematics(const quint64& now) { void PhysicsEngine::stepNonPhysicalKinematics(const quint64& now) {
BT_PROFILE("nonPhysicalKinematics");
QSet<ObjectMotionState*>::iterator stateItr = _nonPhysicalKinematicObjects.begin(); QSet<ObjectMotionState*>::iterator stateItr = _nonPhysicalKinematicObjects.begin();
while (stateItr != _nonPhysicalKinematicObjects.end()) { while (stateItr != _nonPhysicalKinematicObjects.end()) {
ObjectMotionState* motionState = *stateItr; ObjectMotionState* motionState = *stateItr;
@ -358,6 +369,7 @@ void PhysicsEngine::stepNonPhysicalKinematics(const quint64& now) {
// TODO?: need to occasionally scan for stopped non-physical kinematics objects // TODO?: need to occasionally scan for stopped non-physical kinematics objects
void PhysicsEngine::computeCollisionEvents() { void PhysicsEngine::computeCollisionEvents() {
BT_PROFILE("computeCollisionEvents");
// update all contacts every frame // update all contacts every frame
int numManifolds = _collisionDispatcher->getNumManifolds(); int numManifolds = _collisionDispatcher->getNumManifolds();
for (int i = 0; i < numManifolds; ++i) { for (int i = 0; i < numManifolds; ++i) {

View file

@ -86,6 +86,8 @@ public:
void setCharacterController(CharacterController* character); void setCharacterController(CharacterController* character);
void dumpNextStats() { _dumpNextStats = true; }
private: private:
/// \param motionState pointer to Object's MotionState /// \param motionState pointer to Object's MotionState
void removeObjectFromBullet(ObjectMotionState* motionState); void removeObjectFromBullet(ObjectMotionState* motionState);
@ -121,6 +123,8 @@ private:
/// character collisions /// character collisions
CharacterController* _characterController = NULL; CharacterController* _characterController = NULL;
bool _dumpNextStats = false;
}; };
#endif // hifi_PhysicsEngine_h #endif // hifi_PhysicsEngine_h

View file

@ -15,6 +15,8 @@
* Copied and modified from btDiscreteDynamicsWorld.cpp by AndrewMeadows on 2014.11.12. * Copied and modified from btDiscreteDynamicsWorld.cpp by AndrewMeadows on 2014.11.12.
* */ * */
#include <LinearMath/btQuickprof.h>
#include "ThreadSafeDynamicsWorld.h" #include "ThreadSafeDynamicsWorld.h"
ThreadSafeDynamicsWorld::ThreadSafeDynamicsWorld( ThreadSafeDynamicsWorld::ThreadSafeDynamicsWorld(
@ -26,6 +28,7 @@ ThreadSafeDynamicsWorld::ThreadSafeDynamicsWorld(
} }
int ThreadSafeDynamicsWorld::stepSimulation( btScalar timeStep, int maxSubSteps, btScalar fixedTimeStep) { int ThreadSafeDynamicsWorld::stepSimulation( btScalar timeStep, int maxSubSteps, btScalar fixedTimeStep) {
BT_PROFILE("stepSimulation");
int subSteps = 0; int subSteps = 0;
if (maxSubSteps) { if (maxSubSteps) {
//fixed timestep with interpolation //fixed timestep with interpolation