CmRenderSystem.cpp 13 KB

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