cleanup: split stats dump out of stepSimulation

This commit is contained in:
Andrew Meadows 2015-04-14 09:00:36 -07:00
parent f969517ab0
commit abb40ca080
3 changed files with 67 additions and 64 deletions

View file

@ -2226,6 +2226,7 @@ void Application::update(float deltaTime) {
PerformanceTimer perfTimer("physics"); PerformanceTimer perfTimer("physics");
_myAvatar->relayDriveKeysToCharacterController(); _myAvatar->relayDriveKeysToCharacterController();
_physicsEngine.stepSimulation(); _physicsEngine.stepSimulation();
_physicsEngine.dumpStatsIfNecessary();
} }
if (!_aboutToQuit) { if (!_aboutToQuit) {

View file

@ -288,77 +288,73 @@ void PhysicsEngine::init(EntityEditPacketSender* packetSender) {
} }
void PhysicsEngine::stepSimulation() { void PhysicsEngine::stepSimulation() {
{ lock();
lock(); CProfileManager::Reset();
CProfileManager::Reset(); BT_PROFILE("stepSimulation");
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 // (3) synchronize outgoing motion states
// (3) synchronize outgoing motion states // (4) send outgoing packets
// (4) send outgoing packets
// This is step (1) pull incoming changes // This is step (1) pull incoming changes
relayIncomingChangesToSimulation(); relayIncomingChangesToSimulation();
const int MAX_NUM_SUBSTEPS = 4; const int MAX_NUM_SUBSTEPS = 4;
const float MAX_TIMESTEP = (float)MAX_NUM_SUBSTEPS * PHYSICS_ENGINE_FIXED_SUBSTEP; const float MAX_TIMESTEP = (float)MAX_NUM_SUBSTEPS * PHYSICS_ENGINE_FIXED_SUBSTEP;
float dt = 1.0e-6f * (float)(_clock.getTimeMicroseconds()); float dt = 1.0e-6f * (float)(_clock.getTimeMicroseconds());
_clock.reset(); _clock.reset();
float timeStep = btMin(dt, MAX_TIMESTEP); float timeStep = btMin(dt, MAX_TIMESTEP);
// TODO: move character->preSimulation() into relayIncomingChanges // TODO: move character->preSimulation() into relayIncomingChanges
if (_characterController) { if (_characterController) {
if (_characterController->needsRemoval()) { if (_characterController->needsRemoval()) {
_characterController->setDynamicsWorld(NULL); _characterController->setDynamicsWorld(NULL);
}
_characterController->updateShapeIfNecessary();
if (_characterController->needsAddition()) {
_characterController->setDynamicsWorld(_dynamicsWorld);
}
_characterController->preSimulation(timeStep);
} }
_characterController->updateShapeIfNecessary();
// This is step (2) step simulation if (_characterController->needsAddition()) {
int numSubsteps = _dynamicsWorld->stepSimulation(timeStep, MAX_NUM_SUBSTEPS, PHYSICS_ENGINE_FIXED_SUBSTEP); _characterController->setDynamicsWorld(_dynamicsWorld);
_numSubsteps += (uint32_t)numSubsteps;
stepNonPhysicalKinematics(usecTimestampNow());
unlock();
// TODO: make all of this harvest stuff into one function: relayOutgoingChanges()
if (numSubsteps > 0) {
BT_PROFILE("postSimulation");
// 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
// to avoid deadlock -- the _entityTree may try to lock its EntitySimulation (from which this
// PhysicsEngine derives) when updating/adding/deleting entities so we need to wait for our own
// lock on the tree before we re-lock ourselves.
//
// TODO: untangle these lock sequences.
_entityTree->lockForWrite();
lock();
_dynamicsWorld->synchronizeMotionStates();
if (_characterController) {
_characterController->postSimulation();
}
unlock();
_entityTree->unlock();
computeCollisionEvents();
} }
_characterController->preSimulation(timeStep);
} }
if (_dumpNextStats) {
_dumpNextStats = false; // This is step (2) step simulation
CProfileManager::dumpAll(); int numSubsteps = _dynamicsWorld->stepSimulation(timeStep, MAX_NUM_SUBSTEPS, PHYSICS_ENGINE_FIXED_SUBSTEP);
_numSubsteps += (uint32_t)numSubsteps;
stepNonPhysicalKinematics(usecTimestampNow());
unlock();
// TODO: make all of this harvest stuff into one function: relayOutgoingChanges()
if (numSubsteps > 0) {
BT_PROFILE("postSimulation");
// 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
// to avoid deadlock -- the _entityTree may try to lock its EntitySimulation (from which this
// PhysicsEngine derives) when updating/adding/deleting entities so we need to wait for our own
// lock on the tree before we re-lock ourselves.
//
// TODO: untangle these lock sequences.
ObjectMotionState::setSimulationStep(_numSubsteps);
_entityTree->lockForWrite();
lock();
_dynamicsWorld->synchronizeMotionStates();
if (_characterController) {
_characterController->postSimulation();
}
unlock();
_entityTree->unlock();
computeCollisionEvents();
} }
} }
void PhysicsEngine::stepNonPhysicalKinematics(const quint64& now) { void PhysicsEngine::stepNonPhysicalKinematics(const quint64& now) {
BT_PROFILE("nonPhysicalKinematics"); BT_PROFILE("nonPhysicalKinematics");
QSet<ObjectMotionState*>::iterator stateItr = _nonPhysicalKinematicObjects.begin(); QSet<ObjectMotionState*>::iterator stateItr = _nonPhysicalKinematicObjects.begin();
// TODO?: need to occasionally scan for stopped non-physical kinematics objects
while (stateItr != _nonPhysicalKinematicObjects.end()) { while (stateItr != _nonPhysicalKinematicObjects.end()) {
ObjectMotionState* motionState = *stateItr; ObjectMotionState* motionState = *stateItr;
motionState->stepKinematicSimulation(now); motionState->stepKinematicSimulation(now);
@ -366,8 +362,6 @@ void PhysicsEngine::stepNonPhysicalKinematics(const quint64& now) {
} }
} }
// TODO?: need to occasionally scan for stopped non-physical kinematics objects
void PhysicsEngine::computeCollisionEvents() { void PhysicsEngine::computeCollisionEvents() {
BT_PROFILE("computeCollisionEvents"); BT_PROFILE("computeCollisionEvents");
// update all contacts every frame // update all contacts every frame
@ -445,6 +439,13 @@ void PhysicsEngine::computeCollisionEvents() {
} }
} }
void PhysicsEngine::dumpStatsIfNecessary() {
if (_dumpNextStats) {
_dumpNextStats = false;
CProfileManager::dumpAll();
}
}
// Bullet collision flags are as follows: // Bullet collision flags are as follows:
// CF_STATIC_OBJECT= 1, // CF_STATIC_OBJECT= 1,
// CF_KINEMATIC_OBJECT= 2, // CF_KINEMATIC_OBJECT= 2,

View file

@ -68,9 +68,10 @@ public:
void stepSimulation(); void stepSimulation();
void stepNonPhysicalKinematics(const quint64& now); void stepNonPhysicalKinematics(const quint64& now);
void computeCollisionEvents(); void computeCollisionEvents();
void dumpStatsIfNecessary();
/// \param offset position of simulation origin in domain-frame /// \param offset position of simulation origin in domain-frame
void setOriginOffset(const glm::vec3& offset) { _originOffset = offset; } void setOriginOffset(const glm::vec3& offset) { _originOffset = offset; }