| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592 |
- /*
- -----------------------------------------------------------------------------
- This source file is part of OGRE
- (Object-oriented Graphics Rendering Engine)
- For the latest info, see http://www.ogre3d.org/
- Copyright (c) 2000-2011 Torus Knot Software Ltd
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- -----------------------------------------------------------------------------
- */
- // RenderSystem implementation
- // Note that most of this class is abstract since
- // we cannot know how to implement the behaviour without
- // being aware of the 3D API. However there are a few
- // simple functions which can have a base implementation
- #include "CmRenderSystem.h"
- #include "CmViewport.h"
- #include "CmException.h"
- #include "CmRenderTarget.h"
- #include "CmRenderWindow.h"
- #include "CmHardwarePixelBuffer.h"
- #include "CmHardwareOcclusionQuery.h"
- #include "CmCommandQueue.h"
- #include "CmDeferredRenderContext.h"
- #include "boost/bind.hpp"
- #if CM_DEBUG_MODE
- #define THROW_IF_NOT_RENDER_THREAD throwIfNotRenderThread();
- #else
- #define THROW_IF_NOT_RENDER_THREAD
- #endif
- namespace CamelotEngine {
- static const TexturePtr sNullTexPtr;
- //-----------------------------------------------------------------------
- RenderSystem::RenderSystem()
- : mActiveRenderTarget(0)
- , mCullingMode(CULL_CLOCKWISE)
- , mVsync(false)
- , mVSyncInterval(1)
- , mInvertVertexWinding(false)
- , mDisabledTexUnitsFrom(0)
- , mVertexProgramBound(false)
- , mGeometryProgramBound(false)
- , mFragmentProgramBound(false)
- , mClipPlanesDirty(true)
- , mCurrentCapabilities(nullptr)
- , mRenderThreadFunc(nullptr)
- , mRenderThreadShutdown(false)
- , mCommandQueue(nullptr)
- , mMaxCommandNotifyId(0)
- {
- }
- //-----------------------------------------------------------------------
- RenderSystem::~RenderSystem()
- {
- shutdown_internal();
- delete mCurrentCapabilities;
- mCurrentCapabilities = 0;
- }
- //-----------------------------------------------------------------------
- void RenderSystem::startUp()
- {
- mRenderThreadId = CM_THREAD_CURRENT_ID;
- mCommandQueue = new CommandQueue(CM_THREAD_CURRENT_ID);
- initRenderThread();
- queueCommand(boost::bind(&RenderSystem::startUp_internal, this));
- }
- //-----------------------------------------------------------------------
- void RenderSystem::startUp_internal()
- {
- THROW_IF_NOT_RENDER_THREAD;
- mVertexProgramBound = false;
- mGeometryProgramBound = false;
- mFragmentProgramBound = false;
- }
- //-----------------------------------------------------------------------
- void RenderSystem::shutdown(void)
- {
- queueCommand(boost::bind(&RenderSystem::shutdown_internal, this), true);
- // TODO - What if something gets queued between these two calls?
- shutdownRenderThread();
- }
- //-----------------------------------------------------------------------
- void RenderSystem::shutdown_internal(void)
- {
- // TODO - I should probably sync this up to make sure no other threads are doing anything while shutdown is in progress
- // Remove all the render targets.
- // (destroy primary target last since others may depend on it)
- RenderTarget* primary = 0;
- for (auto it = mRenderTargets.begin(); it != mRenderTargets.end(); ++it)
- {
- if (!primary && (*it)->isPrimary())
- primary = *it;
- else
- delete *it;
- }
- delete primary;
- mRenderTargets.clear();
- mPrioritisedRenderTargets.clear();
- if(mCommandQueue != nullptr)
- {
- delete mCommandQueue;
- mCommandQueue = nullptr;
- }
- }
- //-----------------------------------------------------------------------
- void RenderSystem::swapAllRenderTargetBuffers(bool waitForVSync)
- {
- THROW_IF_NOT_RENDER_THREAD;
- // Update all in order of priority
- // This ensures render-to-texture targets get updated before render windows
- RenderTargetPriorityMap::iterator itarg, itargend;
- itargend = mPrioritisedRenderTargets.end();
- for( itarg = mPrioritisedRenderTargets.begin(); itarg != itargend; ++itarg )
- {
- if( itarg->second->isActive())
- itarg->second->swapBuffers(waitForVSync);
- }
- }
- //---------------------------------------------------------------------------------------------
- RenderWindow* RenderSystem::createRenderWindow(const String &name, unsigned int width, unsigned int height,
- bool fullScreen, const NameValuePairList *miscParams)
- {
- AsyncOp op;
- if(miscParams != nullptr)
- op = queueReturnCommand(boost::bind(&RenderSystem::createRenderWindow_internal, this, name, width, height, fullScreen, *miscParams, _1), true);
- else
- op = queueReturnCommand(boost::bind(&RenderSystem::createRenderWindow_internal, this, name, width, height, fullScreen, NameValuePairList(), _1), true);
- return op.getReturnValue<RenderWindow*>();
- }
- //---------------------------------------------------------------------------------------------
- void RenderSystem::destroyRenderWindow(RenderWindow* renderWindow)
- {
- THROW_IF_NOT_RENDER_THREAD;
- destroyRenderTarget(renderWindow);
- }
- //---------------------------------------------------------------------------------------------
- void RenderSystem::destroyRenderTexture(RenderTexture* renderTexture)
- {
- THROW_IF_NOT_RENDER_THREAD;
- destroyRenderTarget(renderTexture);
- }
- //---------------------------------------------------------------------------------------------
- void RenderSystem::destroyRenderTarget(RenderTarget* renderTarget)
- {
- THROW_IF_NOT_RENDER_THREAD;
- detachRenderTarget(*renderTarget);
- delete renderTarget;
- }
- //---------------------------------------------------------------------------------------------
- void RenderSystem::attachRenderTarget( RenderTarget &target )
- {
- THROW_IF_NOT_RENDER_THREAD;
- assert( target.getPriority() < CM_NUM_RENDERTARGET_GROUPS );
- mRenderTargets.push_back(&target);
- mPrioritisedRenderTargets.insert(
- RenderTargetPriorityMap::value_type(target.getPriority(), &target ));
- }
- //---------------------------------------------------------------------------------------------
- void RenderSystem::detachRenderTarget(RenderTarget& renderTarget)
- {
- THROW_IF_NOT_RENDER_THREAD;
- auto it = std::find(mRenderTargets.begin(), mRenderTargets.end(), &renderTarget);
- RenderTarget* foundRT = nullptr;
- if( it != mRenderTargets.end() )
- {
- foundRT = *it;
- /* Remove the render target from the priority groups. */
- RenderTargetPriorityMap::iterator itarg, itargend;
- itargend = mPrioritisedRenderTargets.end();
- for( itarg = mPrioritisedRenderTargets.begin(); itarg != itargend; ++itarg )
- {
- if( itarg->second == *it ) {
- mPrioritisedRenderTargets.erase( itarg );
- break;
- }
- }
- mRenderTargets.erase( it );
- }
- /// If detached render target is the active render target, reset active render target
- if(foundRT == mActiveRenderTarget)
- mActiveRenderTarget = 0;
- }
- //---------------------------------------------------------------------------------------------
- const RenderSystemCapabilities* RenderSystem::getCapabilities(void) const
- {
- THROW_IF_NOT_RENDER_THREAD;
- return mCurrentCapabilities;
- }
- //---------------------------------------------------------------------------------------------
- const DriverVersion& RenderSystem::getDriverVersion(void) const
- {
- THROW_IF_NOT_RENDER_THREAD;
- return mDriverVersion;
- }
- //-----------------------------------------------------------------------
- Viewport RenderSystem::getViewport(void)
- {
- THROW_IF_NOT_RENDER_THREAD;
- return mActiveViewport;
- }
- //-----------------------------------------------------------------------
- void RenderSystem::disableTextureUnit(UINT16 texUnit)
- {
- THROW_IF_NOT_RENDER_THREAD;
- setTexture(texUnit, false, sNullTexPtr);
- }
- //---------------------------------------------------------------------
- void RenderSystem::disableTextureUnitsFrom(UINT16 texUnit)
- {
- THROW_IF_NOT_RENDER_THREAD;
- UINT16 disableTo = CM_MAX_TEXTURE_LAYERS;
- if (disableTo > mDisabledTexUnitsFrom)
- disableTo = mDisabledTexUnitsFrom;
- mDisabledTexUnitsFrom = texUnit;
- for (UINT16 i = texUnit; i < disableTo; ++i)
- {
- disableTextureUnit(i);
- }
- }
- //-----------------------------------------------------------------------
- bool RenderSystem::getWaitForVerticalBlank(void) const
- {
- THROW_IF_NOT_RENDER_THREAD;
- return mVsync;
- }
- //-----------------------------------------------------------------------
- void RenderSystem::setWaitForVerticalBlank(bool enabled)
- {
- THROW_IF_NOT_RENDER_THREAD;
- mVsync = enabled;
- }
- //-----------------------------------------------------------------------
- void RenderSystem::setInvertVertexWinding(bool invert)
- {
- THROW_IF_NOT_RENDER_THREAD;
- mInvertVertexWinding = invert;
- }
- //-----------------------------------------------------------------------
- bool RenderSystem::getInvertVertexWinding(void) const
- {
- THROW_IF_NOT_RENDER_THREAD;
- return mInvertVertexWinding;
- }
- //-----------------------------------------------------------------------
- CullingMode RenderSystem::getCullingMode(void) const
- {
- THROW_IF_NOT_RENDER_THREAD;
- return mCullingMode;
- }
- //---------------------------------------------------------------------
- void RenderSystem::addClipPlane (const Plane &p)
- {
- THROW_IF_NOT_RENDER_THREAD;
- mClipPlanes.push_back(p);
- mClipPlanesDirty = true;
- }
- //---------------------------------------------------------------------
- void RenderSystem::addClipPlane (float A, float B, float C, float D)
- {
- THROW_IF_NOT_RENDER_THREAD;
- addClipPlane(Plane(A, B, C, D));
- }
- //---------------------------------------------------------------------
- void RenderSystem::setClipPlanes(const PlaneList& clipPlanes)
- {
- THROW_IF_NOT_RENDER_THREAD;
- if (clipPlanes != mClipPlanes)
- {
- mClipPlanes = clipPlanes;
- mClipPlanesDirty = true;
- }
- }
- //---------------------------------------------------------------------
- void RenderSystem::resetClipPlanes()
- {
- THROW_IF_NOT_RENDER_THREAD;
- if (!mClipPlanes.empty())
- {
- mClipPlanes.clear();
- mClipPlanesDirty = true;
- }
- }
- //-----------------------------------------------------------------------
- void RenderSystem::bindGpuProgram(GpuProgramHandle prg)
- {
- THROW_IF_NOT_RENDER_THREAD;
- switch(prg->getBindingDelegate_internal()->getType())
- {
- case GPT_VERTEX_PROGRAM:
- // mark clip planes dirty if changed (programmable can change space)
- if (!mVertexProgramBound && !mClipPlanes.empty())
- mClipPlanesDirty = true;
- mVertexProgramBound = true;
- break;
- case GPT_GEOMETRY_PROGRAM:
- mGeometryProgramBound = true;
- break;
- case GPT_FRAGMENT_PROGRAM:
- mFragmentProgramBound = true;
- break;
- }
- }
- //-----------------------------------------------------------------------
- void RenderSystem::unbindGpuProgram(GpuProgramType gptype)
- {
- THROW_IF_NOT_RENDER_THREAD;
- switch(gptype)
- {
- case GPT_VERTEX_PROGRAM:
- // mark clip planes dirty if changed (programmable can change space)
- if (mVertexProgramBound && !mClipPlanes.empty())
- mClipPlanesDirty = true;
- mVertexProgramBound = false;
- break;
- case GPT_GEOMETRY_PROGRAM:
- mGeometryProgramBound = false;
- break;
- case GPT_FRAGMENT_PROGRAM:
- mFragmentProgramBound = false;
- break;
- }
- }
- //-----------------------------------------------------------------------
- bool RenderSystem::isGpuProgramBound(GpuProgramType gptype)
- {
- THROW_IF_NOT_RENDER_THREAD;
- switch(gptype)
- {
- case GPT_VERTEX_PROGRAM:
- return mVertexProgramBound;
- case GPT_GEOMETRY_PROGRAM:
- return mGeometryProgramBound;
- case GPT_FRAGMENT_PROGRAM:
- return mFragmentProgramBound;
- }
- // Make compiler happy
- return false;
- }
- //-----------------------------------------------------------------------
- void RenderSystem::render(const RenderOperation& op)
- {
- THROW_IF_NOT_RENDER_THREAD;
- // sort out clip planes
- // have to do it here in case of matrix issues
- if (mClipPlanesDirty)
- {
- setClipPlanesImpl(mClipPlanes);
- mClipPlanesDirty = false;
- }
- }
- /************************************************************************/
- /* PRIVATE */
- /************************************************************************/
- void RenderSystem::initRenderThread()
- {
- mRenderThreadFunc = new RenderWorkerFunc(this);
- #if CM_THREAD_SUPPORT
- CM_THREAD_CREATE(t, *mRenderThreadFunc);
- mRenderThread = t;
- CM_LOCK_MUTEX_NAMED(mRenderThreadStartMutex, lock)
- CM_THREAD_WAIT(mRenderThreadStartCondition, mRenderThreadStartMutex, lock)
- #else
- CM_EXCEPT(InternalErrorException, "Attempting to start a render thread but Camelot isn't compiled with thread support.");
- #endif
- }
- void RenderSystem::runRenderThread()
- {
- mRenderThreadId = CM_THREAD_CURRENT_ID;
- CM_THREAD_NOTIFY_ALL(mRenderThreadStartCondition)
- while(true)
- {
- if(mRenderThreadShutdown)
- return;
- // Wait until we get some ready commands
- vector<CommandQueue::Command>::type* commands = nullptr;
- {
- CM_LOCK_MUTEX_NAMED(mCommandQueueMutex, lock)
- while(mCommandQueue->isEmpty())
- CM_THREAD_WAIT(mCommandReadyCondition, mCommandQueueMutex, lock)
- commands = mCommandQueue->flush();
- }
- // Play commands
- mCommandQueue->playback(commands, boost::bind(&RenderSystem::commandCompletedNotify, this, _1));
- }
- }
- void RenderSystem::shutdownRenderThread()
- {
- mRenderThreadShutdown = true;
- // Wake all threads. They will quit after they see the shutdown flag
- CM_THREAD_NOTIFY_ALL(mCommandReadyCondition)
- mRenderThread->join();
- CM_THREAD_DESTROY(mRenderThread);
- mRenderThread = nullptr;
- mRenderThreadId = CM_THREAD_CURRENT_ID;
- }
- DeferredRenderContextPtr RenderSystem::createDeferredContext()
- {
- return DeferredRenderContextPtr(new DeferredRenderContext(this, CM_THREAD_CURRENT_ID));
- }
-
- AsyncOp RenderSystem::queueReturnCommand(boost::function<void(AsyncOp&)> commandCallback, bool blockUntilComplete)
- {
- #ifdef CM_DEBUG_MODE
- if(CM_THREAD_CURRENT_ID == getRenderThreadId())
- CM_EXCEPT(InternalErrorException, "You are not allowed to call this method on the render thread!");
- #endif
- AsyncOp op;
- UINT32 commandId = -1;
- {
- CM_LOCK_MUTEX(mCommandQueueMutex);
- if(blockUntilComplete)
- {
- commandId = mMaxCommandNotifyId++;
- op = mCommandQueue->queueReturn(commandCallback, true, commandId);
- }
- else
- op = mCommandQueue->queueReturn(commandCallback);
- }
- CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
- if(blockUntilComplete)
- blockUntilCommandCompleted(commandId);
- return op;
- }
- void RenderSystem::queueCommand(boost::function<void()> commandCallback, bool blockUntilComplete)
- {
- #ifdef CM_DEBUG_MODE
- if(CM_THREAD_CURRENT_ID == getRenderThreadId())
- CM_EXCEPT(InternalErrorException, "You are not allowed to call this method on the render thread!");
- #endif
- UINT32 commandId = -1;
- {
- CM_LOCK_MUTEX(mCommandQueueMutex);
- if(blockUntilComplete)
- {
- commandId = mMaxCommandNotifyId++;
- mCommandQueue->queue(commandCallback, true, commandId);
- }
- else
- mCommandQueue->queue(commandCallback);
- }
- CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
- if(blockUntilComplete)
- blockUntilCommandCompleted(commandId);
- }
- void RenderSystem::blockUntilCommandCompleted(UINT32 commandId)
- {
- CM_LOCK_MUTEX_NAMED(mCommandNotifyMutex, lock);
- while(true)
- {
- // Check if our command id is in the completed list
- auto iter = mCommandsCompleted.begin();
- for(; iter != mCommandsCompleted.end(); ++iter)
- {
- if(*iter == commandId)
- break;
- }
- if(iter != mCommandsCompleted.end())
- {
- mCommandsCompleted.erase(iter);
- break;
- }
- CM_THREAD_WAIT(mCommandCompleteCondition, mCommandNotifyMutex, lock);
- }
- }
- void RenderSystem::commandCompletedNotify(UINT32 commandId)
- {
- {
- CM_LOCK_MUTEX(mCommandNotifyMutex);
- mCommandsCompleted.push_back(commandId);
- }
- CM_THREAD_NOTIFY_ALL(mCommandCompleteCondition);
- }
- void RenderSystem::throwIfNotRenderThread() const
- {
- if(CM_THREAD_CURRENT_ID != getRenderThreadId())
- CM_EXCEPT(InternalErrorException, "Calling the render system from a non-render thread!");
- }
- /************************************************************************/
- /* THREAD WORKER */
- /************************************************************************/
- RenderSystem::RenderWorkerFunc::RenderWorkerFunc(RenderSystem* rs)
- :mRS(rs)
- {
- assert(mRS != nullptr);
- }
- void RenderSystem::RenderWorkerFunc::operator()()
- {
- mRS->runRenderThread();
- }
- }
- #undef THROW_IF_NOT_RENDER_THREAD
|