CmRenderSystem.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. RenderWindowPtr RenderSystem::initialize(const RENDER_WINDOW_DESC& primaryWindowDesc)
  79. {
  80. mRenderThreadId = CM_THREAD_CURRENT_ID;
  81. mCommandQueue = CM_NEW(CommandQueue, GenAlloc) CommandQueue(CM_THREAD_CURRENT_ID, true);
  82. mPrimaryWindowDesc = primaryWindowDesc;
  83. initRenderThread();
  84. AsyncOp op = queueReturnCommand(boost::bind(&RenderSystem::initialize_internal, this, _1), true);
  85. return op.getReturnValue<RenderWindowPtr>();
  86. }
  87. //-----------------------------------------------------------------------
  88. void RenderSystem::initialize_internal(AsyncOp& asyncOp)
  89. {
  90. THROW_IF_NOT_RENDER_THREAD;
  91. mVertexProgramBound = false;
  92. mGeometryProgramBound = false;
  93. mFragmentProgramBound = false;
  94. }
  95. //-----------------------------------------------------------------------
  96. void RenderSystem::destroy_internal()
  97. {
  98. mActiveRenderTarget = nullptr;
  99. }
  100. //---------------------------------------------------------------------------------------------
  101. void RenderSystem::_notifyWindowCreated(RenderWindow& window)
  102. {
  103. THROW_IF_NOT_RENDER_THREAD;
  104. }
  105. //---------------------------------------------------------------------------------------------
  106. const RenderSystemCapabilities* RenderSystem::getCapabilities(void) const
  107. {
  108. THROW_IF_NOT_RENDER_THREAD;
  109. return mCurrentCapabilities;
  110. }
  111. //---------------------------------------------------------------------------------------------
  112. const DriverVersion& RenderSystem::getDriverVersion(void) const
  113. {
  114. THROW_IF_NOT_RENDER_THREAD;
  115. return mDriverVersion;
  116. }
  117. //-----------------------------------------------------------------------
  118. void RenderSystem::disableTextureUnit(GpuProgramType gptype, UINT16 texUnit)
  119. {
  120. THROW_IF_NOT_RENDER_THREAD;
  121. setTexture(gptype, texUnit, false, sNullTexPtr);
  122. }
  123. //---------------------------------------------------------------------
  124. void RenderSystem::addClipPlane (const Plane &p)
  125. {
  126. THROW_IF_NOT_RENDER_THREAD;
  127. mClipPlanes.push_back(p);
  128. mClipPlanesDirty = true;
  129. }
  130. //---------------------------------------------------------------------
  131. void RenderSystem::addClipPlane (float A, float B, float C, float D)
  132. {
  133. THROW_IF_NOT_RENDER_THREAD;
  134. addClipPlane(Plane(A, B, C, D));
  135. }
  136. //---------------------------------------------------------------------
  137. void RenderSystem::setClipPlanes(const PlaneList& clipPlanes)
  138. {
  139. THROW_IF_NOT_RENDER_THREAD;
  140. if (clipPlanes != mClipPlanes)
  141. {
  142. mClipPlanes = clipPlanes;
  143. mClipPlanesDirty = true;
  144. }
  145. }
  146. //---------------------------------------------------------------------
  147. void RenderSystem::resetClipPlanes()
  148. {
  149. THROW_IF_NOT_RENDER_THREAD;
  150. if (!mClipPlanes.empty())
  151. {
  152. mClipPlanes.clear();
  153. mClipPlanesDirty = true;
  154. }
  155. }
  156. //-----------------------------------------------------------------------
  157. void RenderSystem::bindGpuProgram(HGpuProgram prg)
  158. {
  159. THROW_IF_NOT_RENDER_THREAD;
  160. switch(prg->getBindingDelegate()->getType())
  161. {
  162. case GPT_VERTEX_PROGRAM:
  163. // mark clip planes dirty if changed (programmable can change space)
  164. if (!mVertexProgramBound && !mClipPlanes.empty())
  165. mClipPlanesDirty = true;
  166. mVertexProgramBound = true;
  167. break;
  168. case GPT_GEOMETRY_PROGRAM:
  169. mGeometryProgramBound = true;
  170. break;
  171. case GPT_FRAGMENT_PROGRAM:
  172. mFragmentProgramBound = true;
  173. break;
  174. }
  175. }
  176. //-----------------------------------------------------------------------
  177. void RenderSystem::unbindGpuProgram(GpuProgramType gptype)
  178. {
  179. THROW_IF_NOT_RENDER_THREAD;
  180. switch(gptype)
  181. {
  182. case GPT_VERTEX_PROGRAM:
  183. // mark clip planes dirty if changed (programmable can change space)
  184. if (mVertexProgramBound && !mClipPlanes.empty())
  185. mClipPlanesDirty = true;
  186. mVertexProgramBound = false;
  187. break;
  188. case GPT_GEOMETRY_PROGRAM:
  189. mGeometryProgramBound = false;
  190. break;
  191. case GPT_FRAGMENT_PROGRAM:
  192. mFragmentProgramBound = false;
  193. break;
  194. }
  195. }
  196. //-----------------------------------------------------------------------
  197. bool RenderSystem::isGpuProgramBound(GpuProgramType gptype)
  198. {
  199. THROW_IF_NOT_RENDER_THREAD;
  200. switch(gptype)
  201. {
  202. case GPT_VERTEX_PROGRAM:
  203. return mVertexProgramBound;
  204. case GPT_GEOMETRY_PROGRAM:
  205. return mGeometryProgramBound;
  206. case GPT_FRAGMENT_PROGRAM:
  207. return mFragmentProgramBound;
  208. }
  209. // Make compiler happy
  210. return false;
  211. }
  212. //-----------------------------------------------------------------------
  213. void RenderSystem::render(const RenderOperation& op)
  214. {
  215. THROW_IF_NOT_RENDER_THREAD;
  216. // sort out clip planes
  217. // have to do it here in case of matrix issues
  218. if (mClipPlanesDirty)
  219. {
  220. setClipPlanesImpl(mClipPlanes);
  221. mClipPlanesDirty = false;
  222. }
  223. setVertexDeclaration(op.vertexData->vertexDeclaration);
  224. auto vertexBuffers = op.vertexData->getBuffers();
  225. for(auto iter = vertexBuffers.begin(); iter != vertexBuffers.end() ; ++iter)
  226. setVertexBuffer(iter->first, iter->second);
  227. setDrawOperation(op.operationType);
  228. if (op.useIndexes)
  229. {
  230. setIndexBuffer(op.indexData->indexBuffer);
  231. drawIndexed(op.indexData->indexStart, op.indexData->indexCount, op.vertexData->vertexCount);
  232. }
  233. else
  234. draw(op.vertexData->vertexCount);
  235. }
  236. //-----------------------------------------------------------------------
  237. void RenderSystem::swapBuffers(RenderTargetPtr target)
  238. {
  239. target->swapBuffers();
  240. }
  241. /************************************************************************/
  242. /* PRIVATE */
  243. /************************************************************************/
  244. void RenderSystem::initRenderThread()
  245. {
  246. #if !CM_FORCE_SINGLETHREADED_RENDERING
  247. mRenderThreadFunc = CM_NEW(RenderWorkerFunc, GenAlloc) RenderWorkerFunc(this);
  248. #if CM_THREAD_SUPPORT
  249. CM_THREAD_CREATE(t, *mRenderThreadFunc);
  250. mRenderThread = t;
  251. CM_LOCK_MUTEX_NAMED(mRenderThreadStartMutex, lock);
  252. while(!mRenderThreadStarted)
  253. CM_THREAD_WAIT(mRenderThreadStartCondition, mRenderThreadStartMutex, lock);
  254. #else
  255. CM_EXCEPT(InternalErrorException, "Attempting to start a render thread but Camelot isn't compiled with thread support.");
  256. #endif
  257. #endif
  258. }
  259. void RenderSystem::runRenderThread()
  260. {
  261. #if !CM_FORCE_SINGLETHREADED_RENDERING
  262. mRenderThreadId = CM_THREAD_CURRENT_ID;
  263. {
  264. CM_LOCK_MUTEX(mRenderThreadStartMutex);
  265. mRenderThreadStarted = true;
  266. }
  267. CM_THREAD_NOTIFY_ALL(mRenderThreadStartCondition)
  268. while(true)
  269. {
  270. // Wait until we get some ready commands
  271. std::queue<CommandQueue::Command>* commands = nullptr;
  272. {
  273. CM_LOCK_MUTEX_NAMED(mCommandQueueMutex, lock)
  274. while(mCommandQueue->isEmpty())
  275. {
  276. if(mRenderThreadShutdown)
  277. return;
  278. CM_THREAD_WAIT(mCommandReadyCondition, mCommandQueueMutex, lock);
  279. }
  280. commands = mCommandQueue->flush();
  281. }
  282. // Play commands
  283. mCommandQueue->playback(commands, boost::bind(&RenderSystem::commandCompletedNotify, this, _1));
  284. }
  285. #endif
  286. }
  287. void RenderSystem::shutdownRenderThread()
  288. {
  289. #if !CM_FORCE_SINGLETHREADED_RENDERING
  290. {
  291. CM_LOCK_MUTEX(mCommandQueueMutex);
  292. mRenderThreadShutdown = true;
  293. }
  294. // Wake all threads. They will quit after they see the shutdown flag
  295. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  296. mRenderThread->join();
  297. CM_THREAD_DESTROY(mRenderThread);
  298. mRenderThread = nullptr;
  299. mRenderThreadId = CM_THREAD_CURRENT_ID;
  300. if(mRenderThreadFunc != nullptr)
  301. {
  302. CM_DELETE(mRenderThreadFunc, RenderWorkerFunc, GenAlloc);
  303. mRenderThreadFunc = nullptr;
  304. }
  305. #endif
  306. mRenderThreadStarted = false;
  307. }
  308. DeferredRenderContextPtr RenderSystem::createDeferredContext()
  309. {
  310. return DeferredRenderContextPtr(CM_NEW(DeferredRenderContext, GenAlloc) DeferredRenderContext(this, CM_THREAD_CURRENT_ID),
  311. &MemAllocDeleter<DeferredRenderContext, GenAlloc>::deleter);
  312. }
  313. AsyncOp RenderSystem::queueReturnCommand(boost::function<void(AsyncOp&)> commandCallback, bool blockUntilComplete)
  314. {
  315. AsyncOp op;
  316. if(CM_THREAD_CURRENT_ID == getRenderThreadId())
  317. {
  318. commandCallback(op); // Execute immediately
  319. return op;
  320. }
  321. UINT32 commandId = -1;
  322. {
  323. CM_LOCK_MUTEX(mCommandQueueMutex);
  324. if(blockUntilComplete)
  325. {
  326. commandId = mMaxCommandNotifyId++;
  327. op = mCommandQueue->queueReturn(commandCallback, true, commandId);
  328. }
  329. else
  330. op = mCommandQueue->queueReturn(commandCallback);
  331. }
  332. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  333. if(blockUntilComplete)
  334. blockUntilCommandCompleted(commandId);
  335. return op;
  336. }
  337. void RenderSystem::queueCommand(boost::function<void()> commandCallback, bool blockUntilComplete)
  338. {
  339. if(CM_THREAD_CURRENT_ID == getRenderThreadId())
  340. {
  341. commandCallback(); // Execute immediately
  342. return;
  343. }
  344. UINT32 commandId = -1;
  345. {
  346. CM_LOCK_MUTEX(mCommandQueueMutex);
  347. if(blockUntilComplete)
  348. {
  349. commandId = mMaxCommandNotifyId++;
  350. mCommandQueue->queue(commandCallback, true, commandId);
  351. }
  352. else
  353. mCommandQueue->queue(commandCallback);
  354. }
  355. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  356. if(blockUntilComplete)
  357. blockUntilCommandCompleted(commandId);
  358. }
  359. void RenderSystem::blockUntilCommandCompleted(UINT32 commandId)
  360. {
  361. #if !CM_FORCE_SINGLETHREADED_RENDERING
  362. CM_LOCK_MUTEX_NAMED(mCommandNotifyMutex, lock);
  363. while(true)
  364. {
  365. // Check if our command id is in the completed list
  366. auto iter = mCommandsCompleted.begin();
  367. for(; iter != mCommandsCompleted.end(); ++iter)
  368. {
  369. if(*iter == commandId)
  370. break;
  371. }
  372. if(iter != mCommandsCompleted.end())
  373. {
  374. mCommandsCompleted.erase(iter);
  375. break;
  376. }
  377. CM_THREAD_WAIT(mCommandCompleteCondition, mCommandNotifyMutex, lock);
  378. }
  379. #endif
  380. }
  381. void RenderSystem::commandCompletedNotify(UINT32 commandId)
  382. {
  383. {
  384. CM_LOCK_MUTEX(mCommandNotifyMutex);
  385. mCommandsCompleted.push_back(commandId);
  386. }
  387. CM_THREAD_NOTIFY_ALL(mCommandCompleteCondition);
  388. }
  389. void RenderSystem::throwIfNotRenderThread() const
  390. {
  391. #if !CM_FORCE_SINGLETHREADED_RENDERING
  392. if(CM_THREAD_CURRENT_ID != getRenderThreadId())
  393. CM_EXCEPT(InternalErrorException, "Calling the render system from a non-render thread!");
  394. #endif
  395. }
  396. /************************************************************************/
  397. /* THREAD WORKER */
  398. /************************************************************************/
  399. RenderSystem::RenderWorkerFunc::RenderWorkerFunc(RenderSystem* rs)
  400. :mRS(rs)
  401. {
  402. assert(mRS != nullptr);
  403. }
  404. void RenderSystem::RenderWorkerFunc::operator()()
  405. {
  406. mRS->runRenderThread();
  407. }
  408. }
  409. #undef THROW_IF_NOT_RENDER_THREAD