| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- /*
- -----------------------------------------------------------------------------
- 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 "CmDeferredRenderContext.h"
- #include "CmViewport.h"
- #include "CmException.h"
- #include "CmRenderTarget.h"
- #include "CmRenderWindow.h"
- #include "CmPixelBuffer.h"
- #include "CmOcclusionQuery.h"
- #include "CmGpuResource.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 CamelotFramework {
- static const TexturePtr sNullTexPtr;
- RenderSystem::RenderSystem()
- : mCullingMode(CULL_COUNTERCLOCKWISE)
- , mInvertVertexWinding(false)
- , mDisabledTexUnitsFrom(0)
- , mVertexProgramBound(false)
- , mGeometryProgramBound(false)
- , mFragmentProgramBound(false)
- , mClipPlanesDirty(true)
- , mCurrentCapabilities(nullptr)
- , mRenderThreadFunc(nullptr)
- , mRenderThreadStarted(false)
- , mRenderThreadShutdown(false)
- , mCommandQueue(nullptr)
- , mMaxCommandNotifyId(0)
- , mSyncedRenderContext(nullptr)
- {
- }
- RenderSystem::~RenderSystem()
- {
- // Base classes need to call virtual destroy_internal method (queue it on render thread)
- // TODO - What if something gets queued between the queued call to destroy_internal and this!?
- shutdownRenderThread();
- if(mCommandQueue != nullptr)
- {
- cm_delete(mCommandQueue);
- mCommandQueue = nullptr;
- }
- cm_delete(mCurrentCapabilities);
- mCurrentCapabilities = nullptr;
- }
- RenderWindowPtr RenderSystem::initialize(const RENDER_WINDOW_DESC& primaryWindowDesc)
- {
- mRenderThreadId = CM_THREAD_CURRENT_ID;
- mCommandQueue = cm_new<CommandQueue<CommandQueueSync>>(CM_THREAD_CURRENT_ID, true);
- mPrimaryWindowDesc = primaryWindowDesc;
- MemStack::setupHeap(HID_Render);
- initRenderThread();
- AsyncOp op = queueReturnCommand(boost::bind(&RenderSystem::initialize_internal, this, _1), true);
- return op.getReturnValue<RenderWindowPtr>();
- }
- void RenderSystem::initialize_internal(AsyncOp& asyncOp)
- {
- THROW_IF_NOT_RENDER_THREAD;
- mVertexProgramBound = false;
- mGeometryProgramBound = false;
- mFragmentProgramBound = false;
- mSyncedRenderContext = cm_new<DeferredRenderContext<CommandQueueSync>>(this, CM_THREAD_CURRENT_ID);
- }
- void RenderSystem::destroy_internal()
- {
- mActiveRenderTarget = nullptr;
- if(mSyncedRenderContext != nullptr)
- {
- cm_delete(mSyncedRenderContext);
- }
- }
- 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;
- }
- void RenderSystem::disableTextureUnit(GpuProgramType gptype, UINT16 texUnit)
- {
- THROW_IF_NOT_RENDER_THREAD;
- setTexture(gptype, texUnit, false, sNullTexPtr);
- }
- 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(HGpuProgram prg)
- {
- THROW_IF_NOT_RENDER_THREAD;
- switch(prg->getBindingDelegate()->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;
- }
- setVertexDeclaration(op.vertexData->vertexDeclaration);
- auto vertexBuffers = op.vertexData->getBuffers();
- for(auto iter = vertexBuffers.begin(); iter != vertexBuffers.end() ; ++iter)
- setVertexBuffer(iter->first, iter->second);
- setDrawOperation(op.operationType);
- if (op.useIndexes)
- {
- setIndexBuffer(op.indexData->indexBuffer);
- drawIndexed(op.indexData->indexStart, op.indexData->indexCount, op.vertexData->vertexCount);
- }
- else
- draw(op.vertexData->vertexCount);
- }
- void RenderSystem::swapBuffers(RenderTargetPtr target)
- {
- THROW_IF_NOT_RENDER_THREAD;
- target->swapBuffers();
- }
- void RenderSystem::writeSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, const GpuResourceData& data, AsyncOp& asyncOp)
- {
- THROW_IF_NOT_RENDER_THREAD;
- resource->writeSubresource(subresourceIdx, data);
- data.unlock();
- asyncOp.completeOperation();
- }
- void RenderSystem::readSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, GpuResourceData& data, AsyncOp& asyncOp)
- {
- THROW_IF_NOT_RENDER_THREAD;
- resource->readSubresource(subresourceIdx, data);
- data.unlock();
- asyncOp.completeOperation();
- }
- /************************************************************************/
- /* PRIVATE */
- /************************************************************************/
- void RenderSystem::initRenderThread()
- {
- #if !CM_FORCE_SINGLETHREADED_RENDERING
- mRenderThreadFunc = cm_new<RenderWorkerFunc>(this);
- #if CM_THREAD_SUPPORT
- CM_THREAD_CREATE(t, *mRenderThreadFunc);
- mRenderThread = t;
- CM_LOCK_MUTEX_NAMED(mRenderThreadStartMutex, lock);
- while(!mRenderThreadStarted)
- 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
- #endif
- }
- void RenderSystem::runRenderThread()
- {
- #if !CM_FORCE_SINGLETHREADED_RENDERING
- mRenderThreadId = CM_THREAD_CURRENT_ID;
- {
- CM_LOCK_MUTEX(mRenderThreadStartMutex);
- mRenderThreadStarted = true;
- }
- CM_THREAD_NOTIFY_ALL(mRenderThreadStartCondition)
- while(true)
- {
- // Wait until we get some ready commands
- queue<QueuedCommand>::type* commands = nullptr;
- {
- CM_LOCK_MUTEX_NAMED(mCommandQueueMutex, lock)
- while(mCommandQueue->isEmpty())
- {
- if(mRenderThreadShutdown)
- return;
- CM_THREAD_WAIT(mCommandReadyCondition, mCommandQueueMutex, lock);
- }
- commands = mCommandQueue->flush();
- }
- // Play commands
- mCommandQueue->playback(commands, boost::bind(&RenderSystem::commandCompletedNotify, this, _1));
- }
- #endif
- }
- void RenderSystem::shutdownRenderThread()
- {
- #if !CM_FORCE_SINGLETHREADED_RENDERING
- {
- CM_LOCK_MUTEX(mCommandQueueMutex);
- 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;
- if(mRenderThreadFunc != nullptr)
- {
- cm_delete(mRenderThreadFunc);
- mRenderThreadFunc = nullptr;
- }
- #endif
- mRenderThreadStarted = false;
- }
- RenderContextPtr RenderSystem::createDeferredContext()
- {
- return cm_shared_ptr<DeferredRenderContext<CommandQueueNoSync>>(this, CM_THREAD_CURRENT_ID);
- }
- SyncedRenderContext& RenderSystem::getSyncedDeferredContext()
- {
- return *mSyncedRenderContext;
- }
-
- AsyncOp RenderSystem::queueReturnCommand(boost::function<void(AsyncOp&)> commandCallback, bool blockUntilComplete)
- {
- AsyncOp op;
- if(CM_THREAD_CURRENT_ID == getRenderThreadId())
- {
- commandCallback(op); // Execute immediately
- return 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)
- {
- if(CM_THREAD_CURRENT_ID == getRenderThreadId())
- {
- commandCallback(); // Execute immediately
- return;
- }
- 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)
- {
- #if !CM_FORCE_SINGLETHREADED_RENDERING
- 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);
- }
- #endif
- }
- void RenderSystem::commandCompletedNotify(UINT32 commandId)
- {
- {
- CM_LOCK_MUTEX(mCommandNotifyMutex);
- mCommandsCompleted.push_back(commandId);
- }
- CM_THREAD_NOTIFY_ALL(mCommandCompleteCondition);
- }
- void RenderSystem::throwIfNotRenderThread() const
- {
- #if !CM_FORCE_SINGLETHREADED_RENDERING
- if(CM_THREAD_CURRENT_ID != getRenderThreadId())
- CM_EXCEPT(InternalErrorException, "Calling the render system from a non-render thread!");
- #endif
- }
- /************************************************************************/
- /* THREAD WORKER */
- /************************************************************************/
- RenderSystem::RenderWorkerFunc::RenderWorkerFunc(RenderSystem* rs)
- :mRS(rs)
- {
- assert(mRS != nullptr);
- }
- void RenderSystem::RenderWorkerFunc::operator()()
- {
- mRS->runRenderThread();
- }
- }
- #undef THROW_IF_NOT_RENDER_THREAD
|