From 63f07850ac9eeb3b6071e59d677dca50b4a4afb3 Mon Sep 17 00:00:00 2001 From: Robin Southern Date: Wed, 30 Sep 2009 14:46:03 +0100 Subject: [PATCH] Backport of test timing code from Detritus --- build/msvc/NxOgre.VC9.vcproj | 8 +++ build/source/NxOgreAccumulativeSceneTimer.cpp | 58 +++++++++++++--- build/source/NxOgreAccumulativeSceneTimer.h | 6 +- build/source/NxOgreCommon.h | 7 ++ build/source/NxOgreFixedSceneTimer.cpp | 47 ++++++++++++- build/source/NxOgreFixedSceneTimer.h | 4 +- build/source/NxOgreScene.cpp | 25 ++++--- build/source/NxOgreSceneTimer.cpp | 9 ++- build/source/NxOgreSceneTimer.h | 10 ++- build/source/NxOgreTime.cpp | 69 +++++++++++++++++++ build/source/NxOgreTime.h | 51 ++++++++++++++ rendersystems/ogre/OGRE3DBody.cpp | 20 ++---- 12 files changed, 268 insertions(+), 46 deletions(-) create mode 100644 build/source/NxOgreTime.cpp create mode 100644 build/source/NxOgreTime.h diff --git a/build/msvc/NxOgre.VC9.vcproj b/build/msvc/NxOgre.VC9.vcproj index 080fa6c..fa32ad5 100644 --- a/build/msvc/NxOgre.VC9.vcproj +++ b/build/msvc/NxOgre.VC9.vcproj @@ -1592,6 +1592,14 @@ RelativePath="..\source\NxOgreString.h" > + + + + diff --git a/build/source/NxOgreAccumulativeSceneTimer.cpp b/build/source/NxOgreAccumulativeSceneTimer.cpp index 6939555..1d78fe3 100644 --- a/build/source/NxOgreAccumulativeSceneTimer.cpp +++ b/build/source/NxOgreAccumulativeSceneTimer.cpp @@ -26,6 +26,7 @@ #include "NxOgreStable.h" #include "NxOgreAccumulativeSceneTimer.h" #include "NxOgreScene.h" +#include "NxOgreTime.h" #include "NxPhysics.h" @@ -37,7 +38,7 @@ namespace NxOgre AccumulativeSceneTimer::AccumulativeSceneTimer(Scene* scene, Real maxTime, Real expectedTime) -: SceneTimer(scene, maxTime, expectedTime), mAccumulator(0) +: SceneTimer(scene, maxTime, expectedTime), mOldTime(0.0f), mAccumulator(0.0f) { } @@ -45,12 +46,50 @@ AccumulativeSceneTimer::~AccumulativeSceneTimer(void) { } -void AccumulativeSceneTimer::simulate(float user_deltaTime) +void AccumulativeSceneTimer::simulate(float userDeltaTime) { if (mScene->isWritable() == false) return; + const float now = Functions::time(); + float deltaTime = now - mOldTime; + mOldTime = now; + + if (deltaTime > mMaxTime) + deltaTime = mMaxTime; + + mAccumulator += deltaTime; + + TimeStep& ts = mParent->getTimeStep(); + ts.mSubSteps = 0; + ts.mActual = userDeltaTime; + + float sim_time = 0.0f; + while (mAccumulator >= mExpectedTime) + { + ts.mSubSteps++; + mScene->simulate(mExpectedTime); + mScene->flushStream(); + mScene->fetchResults(NX_RIGID_BODY_FINISHED, true); + mAccumulator -= mExpectedTime; + } + + ts.mAlpha = mAccumulator / mExpectedTime; + ts.mModified = mExpectedTime; + + if (ts.mSubSteps) + { + mTimerMode = Enums::TimerMode_Simulating; + } + else + { + mTimerMode = Enums::TimerMode_Miss; + } + +} + +#if 0 if (user_deltaTime > mMaxTime) user_deltaTime = mMaxTime; @@ -59,6 +98,7 @@ void AccumulativeSceneTimer::simulate(float user_deltaTime) TimeStep& ts = mParent->getTimeStep(); ts.mActual = user_deltaTime; ts.mSubSteps = 0; + while (mAccumulator >= mExpectedTime) { ts.mSubSteps++; @@ -70,20 +110,22 @@ void AccumulativeSceneTimer::simulate(float user_deltaTime) ts.mAlpha = mAccumulator / mExpectedTime; ts.mModified = mExpectedTime; - -} +#endif bool AccumulativeSceneTimer::hasResults(void) const { - return true;// mScene->checkResults(NX_RIGID_BODY_FINISHED); + return mScene->checkResults(NX_RIGID_BODY_FINISHED); } -void AccumulativeSceneTimer::fetchResults(void) const +void AccumulativeSceneTimer::fetchResults(void) { +/* + mScene->flushStream(); + mScene->fetchResults(NX_RIGID_BODY_FINISHED, true); +*/ + mTimerMode = Enums::TimerMode_FetchedResults; } - - } // namespace NxOgre diff --git a/build/source/NxOgreAccumulativeSceneTimer.h b/build/source/NxOgreAccumulativeSceneTimer.h index 0223921..3de2fc0 100644 --- a/build/source/NxOgreAccumulativeSceneTimer.h +++ b/build/source/NxOgreAccumulativeSceneTimer.h @@ -69,13 +69,11 @@ class NxOgrePublicClass AccumulativeSceneTimer : public PointerClasssetTiming(mMaxTimeStep / 8.0f, 8, NX_TIMESTEP_FIXED); + TimeStep& ts = mParent->getTimeStep(); + ts.mAlpha = 1.0f; + ts.mModified = mMaxTimeStep; + ts.mSubSteps = 8; } FixedSceneTimer::~FixedSceneTimer(void) @@ -44,15 +56,44 @@ FixedSceneTimer::~FixedSceneTimer(void) void FixedSceneTimer::simulate(float user_deltaTimer) { + + float now = Functions::time(); + float deltaTime = now - mOldTime; + mOldTime = now; + + // Go ahead PhysX. + mScene->simulate(deltaTime); + mScene->flushStream(); + + // Calculate how much time PhysX did process. + mAccumulator += deltaTime; + + TimeStep& ts = mParent->getTimeStep(); + + for (unsigned int i=0; i < ts.mSubSteps;i++) + if (mAccumulator <= mMaxTimeStep) + break; + else + mAccumulator -= mExpectedTime; + + ts.mAlpha = mAccumulator / mMaxTimeStep; + ts.mModified = mExpectedTime; + + // We probably simulated so in goes. + mTimerMode = Enums::TimerMode_Simulating; + ts.mActual = deltaTime; + } bool FixedSceneTimer::hasResults(void) const { - return true; + return mScene->checkResults(NX_RIGID_BODY_FINISHED, false); } -void FixedSceneTimer::fetchResults(void) const +void FixedSceneTimer::fetchResults(void) { + mScene->fetchResults(NX_RIGID_BODY_FINISHED, true); + mTimerMode = Enums::TimerMode_FetchedResults; } diff --git a/build/source/NxOgreFixedSceneTimer.h b/build/source/NxOgreFixedSceneTimer.h index 381987a..931e08d 100644 --- a/build/source/NxOgreFixedSceneTimer.h +++ b/build/source/NxOgreFixedSceneTimer.h @@ -72,11 +72,13 @@ class NxOgrePublicClass FixedSceneTimer : public PointerClasscreateScene(scene_description); @@ -116,12 +118,10 @@ Scene::Scene(ScenePrototype* prototype, NxPhysicsSDK* sdk) mMaterials.insert(material); TimeController::getSingleton()->mListeners[mProcessingPriority].insert(this); + TimeController::getSingleton()->mListeners[mFetchingPriority].insert(this); - if (mFetchingPriority != mProcessingPriority) - TimeController::getSingleton()->mListeners[mFetchingPriority].insert(this); - - mSceneTimer = new AccumulativeSceneTimer(this); // temp. + mSceneTimer = new FixedSceneTimer(this); // temp. mMachineIterator = mMachines.getIterator(); @@ -223,23 +223,26 @@ SceneGeometry* Scene::createSceneGeometry(Shape* shape, const Matrix44& pose, co bool Scene::advance(float user_deltaTime, const Enums::Priority& p) { - if (p == mProcessingPriority) + if (p == mProcessingPriority && mSceneTimer->getTimerMode() > 0 ) { - for (Machine* machine = mMachineIterator.begin();machine = mMachineIterator.next();) machine->simulate(user_deltaTime); mCanRender = false; + mSceneTimer->simulate(user_deltaTime); + return true; } - if (p == mFetchingPriority) + if (p == mFetchingPriority && mSceneTimer->getTimerMode() == Enums::TimerMode_Simulating) { - while(!mSceneTimer->hasResults()) - mSceneTimer->fetchResults(); + + mSceneTimer->fetchResults(); mCanRender = true; + + return true; } return true; diff --git a/build/source/NxOgreSceneTimer.cpp b/build/source/NxOgreSceneTimer.cpp index 4ac4324..152686f 100644 --- a/build/source/NxOgreSceneTimer.cpp +++ b/build/source/NxOgreSceneTimer.cpp @@ -36,7 +36,7 @@ namespace NxOgre SceneTimer::SceneTimer(Scene* scene, Real maxTime, Real expectedTime) -: mParent(scene), mMaxTime(maxTime), mExpectedTime(expectedTime) +: mParent(scene), mMaxTime(maxTime), mExpectedTime(expectedTime), mTimerMode(Enums::TimerMode_Miss) { mScene = mParent->getScene(); } @@ -54,10 +54,15 @@ bool SceneTimer::hasResults(void) const return true; } -void SceneTimer::fetchResults(void) const +void SceneTimer::fetchResults(void) { } +Enums::TimerMode SceneTimer::getTimerMode() const +{ + return mTimerMode; +} + } // namespace NxOgre diff --git a/build/source/NxOgreSceneTimer.h b/build/source/NxOgreSceneTimer.h index f659ba4..66d3c81 100644 --- a/build/source/NxOgreSceneTimer.h +++ b/build/source/NxOgreSceneTimer.h @@ -49,6 +49,7 @@ class NxOgrePublicClass SceneTimer Real mMaxTime; }; + /** \brief Text */ SceneTimer(Scene*, Real maxTime, Real expectedTime); @@ -72,8 +73,12 @@ class NxOgrePublicClass SceneTimer /** \brief */ - virtual void fetchResults(void) const; + virtual void fetchResults(void); + + + virtual Enums::TimerMode getTimerMode() const; + protected: /** \brief Maximum time that can be injected. @@ -92,6 +97,9 @@ class NxOgrePublicClass SceneTimer */ NxScene* mScene; + + Enums::TimerMode mTimerMode; + }; // class ClassName diff --git a/build/source/NxOgreTime.cpp b/build/source/NxOgreTime.cpp new file mode 100644 index 0000000..ef5e7e8 --- /dev/null +++ b/build/source/NxOgreTime.cpp @@ -0,0 +1,69 @@ +/** File: NxOgreTime.cpp + Created on: 30-Sep-09 + Author: Robin Southern "betajaen" + + + © Copyright, 2008-2009 by Robin Southern, http://www.nxogre.org + + This file is part of NxOgre. + + NxOgre is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + NxOgre is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with NxOgre. If not, see . +*/ + + + +#include "NxOgreStable.h" +#include "NxOgreTime.h" + +#if NxOgrePlatform == NxOgrePlatformWindows +#define WIN32_LEAN_AND_MEAN +#include +#endif + + + +namespace NxOgre +{ + + + +namespace Functions +{ + +float time() +{ + static __int64 s = 0; + static __int64 f = 0; + + if (s == 0) + { + QueryPerformanceCounter( (LARGE_INTEGER*) &s); + QueryPerformanceFrequency( (LARGE_INTEGER*) &f); + return 0.0f; + } + + __int64 c = 0; + QueryPerformanceCounter( (LARGE_INTEGER*) &c); + return (float) ( (c - s) / double(f) ); +} + + + +} // namespace Functions + + + +} // namespace NxOgre + + diff --git a/build/source/NxOgreTime.h b/build/source/NxOgreTime.h new file mode 100644 index 0000000..01a3235 --- /dev/null +++ b/build/source/NxOgreTime.h @@ -0,0 +1,51 @@ +/** File: NxOgreTime.h + Created on: 30-Sep-09 + Author: Robin Southern "betajaen" + + + © Copyright, 2008-2009 by Robin Southern, http://www.nxogre.org + + This file is part of NxOgre. + + NxOgre is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + NxOgre is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with NxOgre. If not, see . +*/ + +#ifndef NXOGRE_TIME_H +#define NXOGRE_TIME_H + + + +#include "NxOgreStable.h" + + + +namespace NxOgre +{ + + + +namespace Functions +{ + +float time(); + +} // namespace Functions + + + +} // namespace NxOgre + + + +#endif diff --git a/rendersystems/ogre/OGRE3DBody.cpp b/rendersystems/ogre/OGRE3DBody.cpp index 89b336b..c4e035f 100644 --- a/rendersystems/ogre/OGRE3DBody.cpp +++ b/rendersystems/ogre/OGRE3DBody.cpp @@ -120,22 +120,10 @@ Ogre::Entity* OGRE3DBody::getEntity() bool OGRE3DBody::advance(float, const NxOgre::Enums::Priority&) { -#if 1 - NxOgre::Matrix44 m = getGlobalPose(); - NxOgre::Vec3 pos = m; - NxOgre::Quat quat = m; - mNode->setPosition(pos.x, pos.y, pos.z); - mNode->setOrientation(quat.w, quat.x, quat.y, quat.z); -#else - Ogre::Matrix4 om = toMatrix44(getGlobalPose()); - mNode->setPosition(om.getTrans()); - mNode->setOrientation(om.extractQuaternion()); -#endif - -#if 0 - mNode->setPosition(NxOgre::Functions::XYZ(getGlobalPosition())); - mNode->setOrientation(NxOgre::Functions::WXYZ(getGlobalOrientationQuat())); -#endif + + mNode->setPosition(NxOgre::Vec3(getGlobalPose()).as()); + mNode->setOrientation(NxOgre::Quat(getGlobalPose()).as()); + return true; }