CmRenderSystem.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. // RenderSystem implementation
  25. // Note that most of this class is abstract since
  26. // we cannot know how to implement the behaviour without
  27. // being aware of the 3D API. However there are a few
  28. // simple functions which can have a base implementation
  29. #include "CmRenderSystem.h"
  30. #include "CmViewport.h"
  31. #include "CmException.h"
  32. #include "CmRenderTarget.h"
  33. #include "CmRenderWindow.h"
  34. #include "CmPixelBuffer.h"
  35. #include "CmOcclusionQuery.h"
  36. #include "CmCommandQueue.h"
  37. #include "CmDeferredRenderContext.h"
  38. #include "boost/bind.hpp"
  39. #if CM_DEBUG_MODE
  40. #define THROW_IF_NOT_RENDER_THREAD throwIfNotRenderThread();
  41. #else
  42. #define THROW_IF_NOT_RENDER_THREAD
  43. #endif
  44. namespace CamelotFramework {
  45. static const TexturePtr sNullTexPtr;
  46. //-----------------------------------------------------------------------
  47. RenderSystem::RenderSystem()
  48. : mCullingMode(CULL_COUNTERCLOCKWISE)
  49. , mInvertVertexWinding(false)
  50. , mDisabledTexUnitsFrom(0)
  51. , mVertexProgramBound(false)
  52. , mGeometryProgramBound(false)
  53. , mFragmentProgramBound(false)
  54. , mClipPlanesDirty(true)
  55. , mCurrentCapabilities(nullptr)
  56. , mRenderThreadFunc(nullptr)
  57. , mRenderThreadStarted(false)
  58. , mRenderThreadShutdown(false)
  59. , mCommandQueue(nullptr)
  60. , mMaxCommandNotifyId(0)
  61. {
  62. }
  63. //-----------------------------------------------------------------------
  64. RenderSystem::~RenderSystem()
  65. {
  66. // Base classes need to call virtual destroy_internal method (queue it on render thread)
  67. // TODO - What if something gets queued between the queued call to destroy_internal and this!?
  68. shutdownRenderThread();
  69. if(mCommandQueue != nullptr)
  70. {
  71. CM_DELETE(mCommandQueue, CommandQueue, GenAlloc);
  72. mCommandQueue = nullptr;
  73. }
  74. CM_DELETE(mCurrentCapabilities, RenderSystemCapabilities, GenAlloc);
  75. mCurrentCapabilities = nullptr;
  76. }
  77. //-----------------------------------------------------------------------
  78. void RenderSystem::initialize()
  79. {
  80. mRenderThreadId = CM_THREAD_CURRENT_ID;
  81. mCommandQueue = CM_NEW(CommandQueue, GenAlloc) CommandQueue(CM_THREAD_CURRENT_ID, true);
  82. initRenderThread();
  83. queueCommand(boost::bind(&RenderSystem::initialize_internal, this), true);
  84. }
  85. //-----------------------------------------------------------------------
  86. void RenderSystem::initialize_internal()
  87. {
  88. THROW_IF_NOT_RENDER_THREAD;
  89. mVertexProgramBound = false;
  90. mGeometryProgramBound = false;
  91. mFragmentProgramBound = false;
  92. }
  93. //-----------------------------------------------------------------------
  94. void RenderSystem::destroy_internal()
  95. {
  96. mActiveRenderTarget = nullptr;
  97. }
  98. //---------------------------------------------------------------------------------------------
  99. void RenderSystem::_notifyWindowCreated(RenderWindow& window)
  100. {
  101. THROW_IF_NOT_RENDER_THREAD;
  102. }
  103. //---------------------------------------------------------------------------------------------
  104. const RenderSystemCapabilities* RenderSystem::getCapabilities(void) const
  105. {
  106. THROW_IF_NOT_RENDER_THREAD;
  107. return mCurrentCapabilities;
  108. }
  109. //---------------------------------------------------------------------------------------------
  110. const DriverVersion& RenderSystem::getDriverVersion(void) const
  111. {
  112. THROW_IF_NOT_RENDER_THREAD;
  113. return mDriverVersion;
  114. }
  115. //-----------------------------------------------------------------------
  116. void RenderSystem::disableTextureUnit(GpuProgramType gptype, UINT16 texUnit)
  117. {
  118. THROW_IF_NOT_RENDER_THREAD;
  119. setTexture(gptype, texUnit, false, sNullTexPtr);
  120. }
  121. //---------------------------------------------------------------------
  122. void RenderSystem::addClipPlane (const Plane &p)
  123. {
  124. THROW_IF_NOT_RENDER_THREAD;
  125. mClipPlanes.push_back(p);
  126. mClipPlanesDirty = true;
  127. }
  128. //---------------------------------------------------------------------
  129. void RenderSystem::addClipPlane (float A, float B, float C, float D)
  130. {
  131. THROW_IF_NOT_RENDER_THREAD;
  132. addClipPlane(Plane(A, B, C, D));
  133. }
  134. //---------------------------------------------------------------------
  135. void RenderSystem::setClipPlanes(const PlaneList& clipPlanes)
  136. {
  137. THROW_IF_NOT_RENDER_THREAD;
  138. if (clipPlanes != mClipPlanes)
  139. {
  140. mClipPlanes = clipPlanes;
  141. mClipPlanesDirty = true;
  142. }
  143. }
  144. //---------------------------------------------------------------------
  145. void RenderSystem::resetClipPlanes()
  146. {
  147. THROW_IF_NOT_RENDER_THREAD;
  148. if (!mClipPlanes.empty())
  149. {
  150. mClipPlanes.clear();
  151. mClipPlanesDirty = true;
  152. }
  153. }
  154. //-----------------------------------------------------------------------
  155. void RenderSystem::bindGpuProgram(HGpuProgram prg)
  156. {
  157. THROW_IF_NOT_RENDER_THREAD;
  158. switch(prg->getBindingDelegate()->getType())
  159. {
  160. case GPT_VERTEX_PROGRAM:
  161. // mark clip planes dirty if changed (programmable can change space)
  162. if (!mVertexProgramBound && !mClipPlanes.empty())
  163. mClipPlanesDirty = true;
  164. mVertexProgramBound = true;
  165. break;
  166. case GPT_GEOMETRY_PROGRAM:
  167. mGeometryProgramBound = true;
  168. break;
  169. case GPT_FRAGMENT_PROGRAM:
  170. mFragmentProgramBound = true;
  171. break;
  172. }
  173. }
  174. //-----------------------------------------------------------------------
  175. void RenderSystem::unbindGpuProgram(GpuProgramType gptype)
  176. {
  177. THROW_IF_NOT_RENDER_THREAD;
  178. switch(gptype)
  179. {
  180. case GPT_VERTEX_PROGRAM:
  181. // mark clip planes dirty if changed (programmable can change space)
  182. if (mVertexProgramBound && !mClipPlanes.empty())
  183. mClipPlanesDirty = true;
  184. mVertexProgramBound = false;
  185. break;
  186. case GPT_GEOMETRY_PROGRAM:
  187. mGeometryProgramBound = false;
  188. break;
  189. case GPT_FRAGMENT_PROGRAM:
  190. mFragmentProgramBound = false;
  191. break;
  192. }
  193. }
  194. //-----------------------------------------------------------------------
  195. bool RenderSystem::isGpuProgramBound(GpuProgramType gptype)
  196. {
  197. THROW_IF_NOT_RENDER_THREAD;
  198. switch(gptype)
  199. {
  200. case GPT_VERTEX_PROGRAM:
  201. return mVertexProgramBound;
  202. case GPT_GEOMETRY_PROGRAM:
  203. return mGeometryProgramBound;
  204. case GPT_FRAGMENT_PROGRAM:
  205. return mFragmentProgramBound;
  206. }
  207. // Make compiler happy
  208. return false;
  209. }
  210. //-----------------------------------------------------------------------
  211. void RenderSystem::render(const RenderOperation& op)
  212. {
  213. THROW_IF_NOT_RENDER_THREAD;
  214. // sort out clip planes
  215. // have to do it here in case of matrix issues
  216. if (mClipPlanesDirty)
  217. {
  218. setClipPlanesImpl(mClipPlanes);
  219. mClipPlanesDirty = false;
  220. }
  221. setVertexDeclaration(op.vertexData->vertexDeclaration);
  222. auto vertexBuffers = op.vertexData->getBuffers();
  223. for(auto iter = vertexBuffers.begin(); iter != vertexBuffers.end() ; ++iter)
  224. setVertexBuffer(iter->first, iter->second);
  225. setDrawOperation(op.operationType);
  226. if (op.useIndexes)
  227. {
  228. setIndexBuffer(op.indexData->indexBuffer);
  229. drawIndexed(op.indexData->indexStart, op.indexData->indexCount, op.vertexData->vertexCount);
  230. }
  231. else
  232. draw(op.vertexData->vertexCount);
  233. }
  234. /************************************************************************/
  235. /* PRIVATE */
  236. /************************************************************************/
  237. void RenderSystem::initRenderThread()
  238. {
  239. #if !CM_FORCE_SINGLETHREADED_RENDERING
  240. mRenderThreadFunc = CM_NEW(RenderWorkerFunc, GenAlloc) RenderWorkerFunc(this);
  241. #if CM_THREAD_SUPPORT
  242. CM_THREAD_CREATE(t, *mRenderThreadFunc);
  243. mRenderThread = t;
  244. CM_LOCK_MUTEX_NAMED(mRenderThreadStartMutex, lock);
  245. while(!mRenderThreadStarted)
  246. CM_THREAD_WAIT(mRenderThreadStartCondition, mRenderThreadStartMutex, lock);
  247. #else
  248. CM_EXCEPT(InternalErrorException, "Attempting to start a render thread but Camelot isn't compiled with thread support.");
  249. #endif
  250. #endif
  251. }
  252. void RenderSystem::runRenderThread()
  253. {
  254. #if !CM_FORCE_SINGLETHREADED_RENDERING
  255. mRenderThreadId = CM_THREAD_CURRENT_ID;
  256. {
  257. CM_LOCK_MUTEX(mRenderThreadStartMutex);
  258. mRenderThreadStarted = true;
  259. }
  260. CM_THREAD_NOTIFY_ALL(mRenderThreadStartCondition)
  261. while(true)
  262. {
  263. // Wait until we get some ready commands
  264. std::queue<CommandQueue::Command>* commands = nullptr;
  265. {
  266. CM_LOCK_MUTEX_NAMED(mCommandQueueMutex, lock)
  267. while(mCommandQueue->isEmpty())
  268. {
  269. if(mRenderThreadShutdown)
  270. return;
  271. CM_THREAD_WAIT(mCommandReadyCondition, mCommandQueueMutex, lock);
  272. }
  273. commands = mCommandQueue->flush();
  274. }
  275. // Play commands
  276. mCommandQueue->playback(commands, boost::bind(&RenderSystem::commandCompletedNotify, this, _1));
  277. }
  278. #endif
  279. }
  280. void RenderSystem::shutdownRenderThread()
  281. {
  282. #if !CM_FORCE_SINGLETHREADED_RENDERING
  283. {
  284. CM_LOCK_MUTEX(mCommandQueueMutex);
  285. mRenderThreadShutdown = true;
  286. }
  287. // Wake all threads. They will quit after they see the shutdown flag
  288. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  289. mRenderThread->join();
  290. CM_THREAD_DESTROY(mRenderThread);
  291. mRenderThread = nullptr;
  292. mRenderThreadId = CM_THREAD_CURRENT_ID;
  293. if(mRenderThreadFunc != nullptr)
  294. {
  295. CM_DELETE(mRenderThreadFunc, RenderWorkerFunc, GenAlloc);
  296. mRenderThreadFunc = nullptr;
  297. }
  298. #endif
  299. mRenderThreadStarted = false;
  300. }
  301. DeferredRenderContextPtr RenderSystem::createDeferredContext()
  302. {
  303. return DeferredRenderContextPtr(CM_NEW(DeferredRenderContext, GenAlloc) DeferredRenderContext(this, CM_THREAD_CURRENT_ID),
  304. &MemAllocDeleter<DeferredRenderContext, GenAlloc>::deleter);
  305. }
  306. AsyncOp RenderSystem::queueReturnCommand(boost::function<void(AsyncOp&)> commandCallback, bool blockUntilComplete)
  307. {
  308. AsyncOp op;
  309. if(CM_THREAD_CURRENT_ID == getRenderThreadId())
  310. {
  311. commandCallback(op); // Execute immediately
  312. return op;
  313. }
  314. UINT32 commandId = -1;
  315. {
  316. CM_LOCK_MUTEX(mCommandQueueMutex);
  317. if(blockUntilComplete)
  318. {
  319. commandId = mMaxCommandNotifyId++;
  320. op = mCommandQueue->queueReturn(commandCallback, true, commandId);
  321. }
  322. else
  323. op = mCommandQueue->queueReturn(commandCallback);
  324. }
  325. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  326. if(blockUntilComplete)
  327. blockUntilCommandCompleted(commandId);
  328. return op;
  329. }
  330. void RenderSystem::queueCommand(boost::function<void()> commandCallback, bool blockUntilComplete)
  331. {
  332. if(CM_THREAD_CURRENT_ID == getRenderThreadId())
  333. {
  334. commandCallback(); // Execute immediately
  335. return;
  336. }
  337. UINT32 commandId = -1;
  338. {
  339. CM_LOCK_MUTEX(mCommandQueueMutex);
  340. if(blockUntilComplete)
  341. {
  342. commandId = mMaxCommandNotifyId++;
  343. mCommandQueue->queue(commandCallback, true, commandId);
  344. }
  345. else
  346. mCommandQueue->queue(commandCallback);
  347. }
  348. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  349. if(blockUntilComplete)
  350. blockUntilCommandCompleted(commandId);
  351. }
  352. void RenderSystem::blockUntilCommandCompleted(UINT32 commandId)
  353. {
  354. #if !CM_FORCE_SINGLETHREADED_RENDERING
  355. CM_LOCK_MUTEX_NAMED(mCommandNotifyMutex, lock);
  356. while(true)
  357. {
  358. // Check if our command id is in the completed list
  359. auto iter = mCommandsCompleted.begin();
  360. for(; iter != mCommandsCompleted.end(); ++iter)
  361. {
  362. if(*iter == commandId)
  363. break;
  364. }
  365. if(iter != mCommandsCompleted.end())
  366. {
  367. mCommandsCompleted.erase(iter);
  368. break;
  369. }
  370. CM_THREAD_WAIT(mCommandCompleteCondition, mCommandNotifyMutex, lock);
  371. }
  372. #endif
  373. }
  374. void RenderSystem::commandCompletedNotify(UINT32 commandId)
  375. {
  376. {
  377. CM_LOCK_MUTEX(mCommandNotifyMutex);
  378. mCommandsCompleted.push_back(commandId);
  379. }
  380. CM_THREAD_NOTIFY_ALL(mCommandCompleteCondition);
  381. }
  382. void RenderSystem::throwIfNotRenderThread() const
  383. {
  384. #if !CM_FORCE_SINGLETHREADED_RENDERING
  385. if(CM_THREAD_CURRENT_ID != getRenderThreadId())
  386. CM_EXCEPT(InternalErrorException, "Calling the render system from a non-render thread!");
  387. #endif
  388. }
  389. /************************************************************************/
  390. /* THREAD WORKER */
  391. /************************************************************************/
  392. RenderSystem::RenderWorkerFunc::RenderWorkerFunc(RenderSystem* rs)
  393. :mRS(rs)
  394. {
  395. assert(mRS != nullptr);
  396. }
  397. void RenderSystem::RenderWorkerFunc::operator()()
  398. {
  399. mRS->runRenderThread();
  400. }
  401. }
  402. #undef THROW_IF_NOT_RENDER_THREAD