| [6819] | 1 | #include "OgreGpuCommandBufferFlush.h" | 
|---|
 | 2 | #include "OgreRoot.h" | 
|---|
 | 3 | #include "OgreRenderSystem.h" | 
|---|
 | 4 | #include "OgreHardwareOcclusionQuery.h" | 
|---|
 | 5 |  | 
|---|
 | 6 | namespace Ogre | 
|---|
 | 7 | { | 
|---|
 | 8 |         //--------------------------------------------------------------------- | 
|---|
 | 9 |         GpuCommandBufferFlush::GpuCommandBufferFlush() | 
|---|
 | 10 |                 : mUseOcclusionQuery(true) | 
|---|
 | 11 |                 , mMaxQueuedFrames(2) | 
|---|
 | 12 |                 , mCurrentFrame(0) | 
|---|
 | 13 |                 , mStartPull(false) | 
|---|
 | 14 |                 , mStarted(false) | 
|---|
 | 15 |         { | 
|---|
 | 16 |  | 
|---|
 | 17 |         } | 
|---|
 | 18 |         //--------------------------------------------------------------------- | 
|---|
 | 19 |         GpuCommandBufferFlush::~GpuCommandBufferFlush() | 
|---|
 | 20 |         { | 
|---|
 | 21 |                 stop(); | 
|---|
 | 22 |         } | 
|---|
 | 23 |         //--------------------------------------------------------------------- | 
|---|
 | 24 |         void GpuCommandBufferFlush::start(size_t maxQueuedFrames) | 
|---|
 | 25 |         { | 
|---|
 | 26 |                 if (!Root::getSingletonPtr() || !Root::getSingletonPtr()->getRenderSystem()) | 
|---|
 | 27 |                         return; | 
|---|
 | 28 |  | 
|---|
 | 29 |                 stop(); | 
|---|
 | 30 |                 mMaxQueuedFrames = maxQueuedFrames; | 
|---|
 | 31 |                 RenderSystem* rsys = Root::getSingleton().getRenderSystem(); | 
|---|
 | 32 |                 mUseOcclusionQuery = rsys->getCapabilities()->hasCapability(RSC_HWOCCLUSION); | 
|---|
 | 33 |  | 
|---|
 | 34 |                 if (mUseOcclusionQuery) | 
|---|
 | 35 |                 { | 
|---|
 | 36 |                         for (size_t i = 0; i < mMaxQueuedFrames; ++i) | 
|---|
 | 37 |                         { | 
|---|
 | 38 |                                 HardwareOcclusionQuery* hoq = rsys->createHardwareOcclusionQuery(); | 
|---|
 | 39 |                                 mHOQList.push_back(hoq); | 
|---|
 | 40 |                         } | 
|---|
 | 41 |                 } | 
|---|
 | 42 |  | 
|---|
 | 43 |                 mCurrentFrame = 0; | 
|---|
 | 44 |                 mStartPull = false; | 
|---|
 | 45 |  | 
|---|
 | 46 |                 Root::getSingleton().addFrameListener(this); | 
|---|
 | 47 |  | 
|---|
 | 48 |                 mStarted = true; | 
|---|
 | 49 |  | 
|---|
 | 50 |         } | 
|---|
 | 51 |         //--------------------------------------------------------------------- | 
|---|
 | 52 |         void GpuCommandBufferFlush::stop() | 
|---|
 | 53 |         { | 
|---|
 | 54 |                 if (!mStarted || !Root::getSingletonPtr() || !Root::getSingletonPtr()->getRenderSystem()) | 
|---|
 | 55 |                         return; | 
|---|
 | 56 |  | 
|---|
 | 57 |                 RenderSystem* rsys = Root::getSingleton().getRenderSystem(); | 
|---|
 | 58 |                 for (HOQList::iterator i = mHOQList.begin(); i != mHOQList.end(); ++i) | 
|---|
 | 59 |                 { | 
|---|
 | 60 |                         rsys->destroyHardwareOcclusionQuery(*i); | 
|---|
 | 61 |                 } | 
|---|
 | 62 |                 mHOQList.clear(); | 
|---|
 | 63 |  | 
|---|
 | 64 |                 Root::getSingleton().removeFrameListener(this); | 
|---|
 | 65 |  | 
|---|
 | 66 |                 mStarted = false; | 
|---|
 | 67 |  | 
|---|
 | 68 |         } | 
|---|
 | 69 |         //--------------------------------------------------------------------- | 
|---|
 | 70 |         bool GpuCommandBufferFlush::frameStarted(const FrameEvent& evt) | 
|---|
 | 71 |         { | 
|---|
 | 72 |                 if (mUseOcclusionQuery) | 
|---|
 | 73 |                 { | 
|---|
 | 74 |  | 
|---|
 | 75 |                         mHOQList[mCurrentFrame]->beginOcclusionQuery(); | 
|---|
 | 76 |  | 
|---|
 | 77 |                 } | 
|---|
 | 78 |  | 
|---|
 | 79 |                 return true; | 
|---|
 | 80 |         } | 
|---|
 | 81 |         //--------------------------------------------------------------------- | 
|---|
 | 82 |         bool GpuCommandBufferFlush::frameEnded(const FrameEvent& evt) | 
|---|
 | 83 |         { | 
|---|
 | 84 |                 if (mUseOcclusionQuery) | 
|---|
 | 85 |                 { | 
|---|
 | 86 |                         mHOQList[mCurrentFrame]->endOcclusionQuery(); | 
|---|
 | 87 |                 } | 
|---|
 | 88 |                 mCurrentFrame = (mCurrentFrame + 1) % mMaxQueuedFrames; | 
|---|
 | 89 |                 // If we've wrapped around, time to start pulling | 
|---|
 | 90 |                 if (mCurrentFrame == 0) | 
|---|
 | 91 |                         mStartPull = true; | 
|---|
 | 92 |  | 
|---|
 | 93 |                 if (mStartPull) | 
|---|
 | 94 |                 { | 
|---|
 | 95 |                         unsigned int dummy; | 
|---|
 | 96 |                         mHOQList[mCurrentFrame]->pullOcclusionQuery(&dummy); | 
|---|
 | 97 |                 } | 
|---|
 | 98 |  | 
|---|
 | 99 |                 return true; | 
|---|
 | 100 |         } | 
|---|
 | 101 |         //--------------------------------------------------------------------- | 
|---|
 | 102 |  | 
|---|
 | 103 |  | 
|---|
 | 104 | } | 
|---|
 | 105 |  | 
|---|