CmRenderSystem.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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 "CmHardwarePixelBuffer.h"
  35. #include "CmHardwareOcclusionQuery.h"
  36. #include "CmRenderSystemContext.h"
  37. #include "CmCommandQueue.h"
  38. #include "CmDeferredRenderContext.h"
  39. #include "boost/bind.hpp"
  40. #if CM_DEBUG_MODE
  41. #define THROW_IF_INVALID_CONTEXT throwIfInvalidContextThread();
  42. #define THROW_IF_NOT_RENDER_THREAD throwIfNotRenderThread();
  43. #else
  44. #define THROW_IF_INVALID_CONTEXT
  45. #define THROW_IF_NOT_RENDER_THREAD
  46. #endif
  47. namespace CamelotEngine {
  48. static const TexturePtr sNullTexPtr;
  49. //-----------------------------------------------------------------------
  50. RenderSystem::RenderSystem()
  51. : mActiveRenderTarget(0)
  52. , mCullingMode(CULL_CLOCKWISE)
  53. , mVsync(false)
  54. , mVSyncInterval(1)
  55. , mInvertVertexWinding(false)
  56. , mDisabledTexUnitsFrom(0)
  57. , mVertexProgramBound(false)
  58. , mGeometryProgramBound(false)
  59. , mFragmentProgramBound(false)
  60. , mClipPlanesDirty(true)
  61. , mCurrentCapabilities(nullptr)
  62. , mRenderThreadFunc(nullptr)
  63. , mRenderThreadShutdown(false)
  64. , mCommandQueue(nullptr)
  65. , mMaxCommandNotifyId(0)
  66. {
  67. }
  68. //-----------------------------------------------------------------------
  69. RenderSystem::~RenderSystem()
  70. {
  71. shutdown_internal();
  72. delete mCurrentCapabilities;
  73. mCurrentCapabilities = 0;
  74. }
  75. //-----------------------------------------------------------------------
  76. void RenderSystem::startUp()
  77. {
  78. mRenderThreadId = CM_THREAD_CURRENT_ID;
  79. mCommandQueue = new CommandQueue(CM_THREAD_CURRENT_ID);
  80. initRenderThread();
  81. queueCommand(boost::bind(&RenderSystem::startUp_internal, this));
  82. }
  83. //-----------------------------------------------------------------------
  84. void RenderSystem::startUp_internal()
  85. {
  86. THROW_IF_NOT_RENDER_THREAD;
  87. mVertexProgramBound = false;
  88. mGeometryProgramBound = false;
  89. mFragmentProgramBound = false;
  90. }
  91. //-----------------------------------------------------------------------
  92. void RenderSystem::shutdown(void)
  93. {
  94. queueCommand(boost::bind(&RenderSystem::shutdown_internal, this), true);
  95. // TODO - What if something gets queued between these two calls?
  96. shutdownRenderThread();
  97. }
  98. //-----------------------------------------------------------------------
  99. void RenderSystem::shutdown_internal(void)
  100. {
  101. // TODO - I should probably sync this up to make sure no other threads are doing anything while shutdown is in progress
  102. // Remove all the render targets.
  103. // (destroy primary target last since others may depend on it)
  104. RenderTarget* primary = 0;
  105. for (auto it = mRenderTargets.begin(); it != mRenderTargets.end(); ++it)
  106. {
  107. if (!primary && (*it)->isPrimary())
  108. primary = *it;
  109. else
  110. delete *it;
  111. }
  112. delete primary;
  113. mRenderTargets.clear();
  114. mPrioritisedRenderTargets.clear();
  115. if(mCommandQueue != nullptr)
  116. {
  117. delete mCommandQueue;
  118. mCommandQueue = nullptr;
  119. }
  120. }
  121. //-----------------------------------------------------------------------
  122. void RenderSystem::swapAllRenderTargetBuffers(bool waitForVSync)
  123. {
  124. THROW_IF_INVALID_CONTEXT;
  125. CM_LOCK_MUTEX(mActiveContextMutex);
  126. mActiveContext->queueCommand(boost::bind(&RenderSystem::swapAllRenderTargetBuffers_internal, this, waitForVSync));
  127. }
  128. //-----------------------------------------------------------------------
  129. void RenderSystem::swapAllRenderTargetBuffers_internal(bool waitForVSync)
  130. {
  131. THROW_IF_NOT_RENDER_THREAD;
  132. // Update all in order of priority
  133. // This ensures render-to-texture targets get updated before render windows
  134. RenderTargetPriorityMap::iterator itarg, itargend;
  135. itargend = mPrioritisedRenderTargets.end();
  136. for( itarg = mPrioritisedRenderTargets.begin(); itarg != itargend; ++itarg )
  137. {
  138. if( itarg->second->isActive())
  139. itarg->second->swapBuffers(waitForVSync);
  140. }
  141. }
  142. //---------------------------------------------------------------------------------------------
  143. RenderWindow* RenderSystem::createRenderWindow(const String &name, unsigned int width, unsigned int height,
  144. bool fullScreen, const NameValuePairList *miscParams)
  145. {
  146. AsyncOp op;
  147. if(miscParams != nullptr)
  148. op = queueReturnCommand(boost::bind(&RenderSystem::createRenderWindow_internal, this, name, width, height, fullScreen, *miscParams, _1), true);
  149. else
  150. op = queueReturnCommand(boost::bind(&RenderSystem::createRenderWindow_internal, this, name, width, height, fullScreen, NameValuePairList(), _1), true);
  151. return op.getReturnValue<RenderWindow*>();
  152. }
  153. //---------------------------------------------------------------------------------------------
  154. void RenderSystem::destroyRenderWindow_internal(RenderWindow* renderWindow)
  155. {
  156. THROW_IF_NOT_RENDER_THREAD;
  157. destroyRenderTarget_internal(renderWindow);
  158. }
  159. //---------------------------------------------------------------------------------------------
  160. void RenderSystem::destroyRenderTexture_internal(RenderTexture* renderTexture)
  161. {
  162. THROW_IF_NOT_RENDER_THREAD;
  163. destroyRenderTarget_internal(renderTexture);
  164. }
  165. //---------------------------------------------------------------------------------------------
  166. void RenderSystem::destroyRenderTarget_internal(RenderTarget* renderTarget)
  167. {
  168. THROW_IF_NOT_RENDER_THREAD;
  169. detachRenderTarget_internal(*renderTarget);
  170. delete renderTarget;
  171. }
  172. //---------------------------------------------------------------------------------------------
  173. void RenderSystem::attachRenderTarget_internal( RenderTarget &target )
  174. {
  175. THROW_IF_NOT_RENDER_THREAD;
  176. assert( target.getPriority() < CM_NUM_RENDERTARGET_GROUPS );
  177. mRenderTargets.push_back(&target);
  178. mPrioritisedRenderTargets.insert(
  179. RenderTargetPriorityMap::value_type(target.getPriority(), &target ));
  180. }
  181. //---------------------------------------------------------------------------------------------
  182. void RenderSystem::detachRenderTarget_internal(RenderTarget& renderTarget)
  183. {
  184. THROW_IF_NOT_RENDER_THREAD;
  185. auto it = std::find(mRenderTargets.begin(), mRenderTargets.end(), &renderTarget);
  186. RenderTarget* foundRT = nullptr;
  187. if( it != mRenderTargets.end() )
  188. {
  189. foundRT = *it;
  190. /* Remove the render target from the priority groups. */
  191. RenderTargetPriorityMap::iterator itarg, itargend;
  192. itargend = mPrioritisedRenderTargets.end();
  193. for( itarg = mPrioritisedRenderTargets.begin(); itarg != itargend; ++itarg )
  194. {
  195. if( itarg->second == *it ) {
  196. mPrioritisedRenderTargets.erase( itarg );
  197. break;
  198. }
  199. }
  200. mRenderTargets.erase( it );
  201. }
  202. /// If detached render target is the active render target, reset active render target
  203. if(foundRT == mActiveRenderTarget)
  204. mActiveRenderTarget = 0;
  205. }
  206. //---------------------------------------------------------------------------------------------
  207. const RenderSystemCapabilities* RenderSystem::getCapabilities_internal(void) const
  208. {
  209. THROW_IF_NOT_RENDER_THREAD;
  210. return mCurrentCapabilities;
  211. }
  212. //---------------------------------------------------------------------------------------------
  213. const DriverVersion& RenderSystem::getDriverVersion_internal(void) const
  214. {
  215. THROW_IF_NOT_RENDER_THREAD;
  216. return mDriverVersion;
  217. }
  218. //-----------------------------------------------------------------------
  219. void RenderSystem::setViewport(const Viewport& vp)
  220. {
  221. THROW_IF_INVALID_CONTEXT;
  222. CM_LOCK_MUTEX(mActiveContextMutex);
  223. mActiveContext->queueCommand(boost::bind(&RenderSystem::setViewport_internal, this, vp));
  224. }
  225. //-----------------------------------------------------------------------
  226. Viewport RenderSystem::getViewport_internal(void)
  227. {
  228. THROW_IF_NOT_RENDER_THREAD;
  229. return mActiveViewport;
  230. }
  231. //-----------------------------------------------------------------------
  232. void RenderSystem::setTextureUnitSettings(UINT16 texUnit, const TexturePtr& tex, const SamplerState& samplerState)
  233. {
  234. THROW_IF_INVALID_CONTEXT;
  235. CM_LOCK_MUTEX(mActiveContextMutex);
  236. mActiveContext->queueCommand(boost::bind(&RenderSystem::setTextureUnitSettings_internal, this, texUnit, tex, samplerState));
  237. }
  238. //-----------------------------------------------------------------------
  239. void RenderSystem::setTextureUnitSettings_internal(UINT16 texUnit, const TexturePtr& tex, const SamplerState& tl)
  240. {
  241. THROW_IF_NOT_RENDER_THREAD;
  242. // This method is only ever called to set a texture unit to valid details
  243. // The method _disableTextureUnit is called to turn a unit off
  244. // Vertex texture binding?
  245. if (mCurrentCapabilities->hasCapability(RSC_VERTEX_TEXTURE_FETCH) &&
  246. !mCurrentCapabilities->getVertexTextureUnitsShared())
  247. {
  248. if (tl.getBindingType() == SamplerState::BT_VERTEX)
  249. {
  250. // Bind vertex texture
  251. setVertexTexture_internal(texUnit, tex);
  252. // bind nothing to fragment unit (hardware isn't shared but fragment
  253. // unit can't be using the same index
  254. setTexture_internal(texUnit, true, sNullTexPtr);
  255. }
  256. else
  257. {
  258. // vice versa
  259. setVertexTexture_internal(texUnit, sNullTexPtr);
  260. setTexture_internal(texUnit, true, tex);
  261. }
  262. }
  263. else
  264. {
  265. // Shared vertex / fragment textures or no vertex texture support
  266. // Bind texture (may be blank)
  267. setTexture_internal(texUnit, true, tex);
  268. }
  269. // Set texture layer filtering
  270. setTextureFiltering_internal(texUnit,
  271. tl.getTextureFiltering(FT_MIN),
  272. tl.getTextureFiltering(FT_MAG),
  273. tl.getTextureFiltering(FT_MIP));
  274. // Set texture layer filtering
  275. setTextureAnisotropy_internal(texUnit, tl.getTextureAnisotropy());
  276. // Set mipmap biasing
  277. setTextureMipmapBias_internal(texUnit, tl.getTextureMipmapBias());
  278. // Texture addressing mode
  279. const SamplerState::UVWAddressingMode& uvw = tl.getTextureAddressingMode();
  280. setTextureAddressingMode_internal(texUnit, uvw);
  281. }
  282. //-----------------------------------------------------------------------
  283. void RenderSystem::setTexture(UINT16 unit, bool enabled, const TexturePtr &texPtr)
  284. {
  285. THROW_IF_INVALID_CONTEXT;
  286. CM_LOCK_MUTEX(mActiveContextMutex);
  287. mActiveContext->queueCommand(boost::bind(&RenderSystem::setTexture_internal, this, unit, enabled, texPtr));
  288. }
  289. //-----------------------------------------------------------------------
  290. void RenderSystem::setVertexTexture(UINT16 unit, const TexturePtr& tex)
  291. {
  292. THROW_IF_INVALID_CONTEXT;
  293. CM_LOCK_MUTEX(mActiveContextMutex);
  294. mActiveContext->queueCommand(boost::bind(&RenderSystem::setVertexTexture_internal, this, unit, tex));
  295. }
  296. //-----------------------------------------------------------------------
  297. void RenderSystem::setVertexTexture_internal(UINT16 unit, const TexturePtr& tex)
  298. {
  299. THROW_IF_NOT_RENDER_THREAD;
  300. CM_EXCEPT(NotImplementedException,
  301. "This rendersystem does not support separate vertex texture samplers, "
  302. "you should use the regular texture samplers which are shared between "
  303. "the vertex and fragment units.");
  304. }
  305. //-----------------------------------------------------------------------
  306. void RenderSystem::disableTextureUnit(UINT16 texUnit)
  307. {
  308. THROW_IF_INVALID_CONTEXT;
  309. CM_LOCK_MUTEX(mActiveContextMutex);
  310. mActiveContext->queueCommand(boost::bind(&RenderSystem::disableTextureUnit_internal, this, texUnit));
  311. }
  312. //-----------------------------------------------------------------------
  313. void RenderSystem::disableTextureUnit_internal(UINT16 texUnit)
  314. {
  315. THROW_IF_NOT_RENDER_THREAD;
  316. setTexture_internal(texUnit, false, sNullTexPtr);
  317. }
  318. //---------------------------------------------------------------------
  319. void RenderSystem::disableTextureUnitsFrom(UINT16 texUnit)
  320. {
  321. THROW_IF_INVALID_CONTEXT;
  322. CM_LOCK_MUTEX(mActiveContextMutex);
  323. mActiveContext->queueCommand(boost::bind(&RenderSystem::disableTextureUnitsFrom_internal, this, texUnit));
  324. }
  325. //---------------------------------------------------------------------
  326. void RenderSystem::disableTextureUnitsFrom_internal(UINT16 texUnit)
  327. {
  328. THROW_IF_NOT_RENDER_THREAD;
  329. UINT16 disableTo = CM_MAX_TEXTURE_LAYERS;
  330. if (disableTo > mDisabledTexUnitsFrom)
  331. disableTo = mDisabledTexUnitsFrom;
  332. mDisabledTexUnitsFrom = texUnit;
  333. for (UINT16 i = texUnit; i < disableTo; ++i)
  334. {
  335. disableTextureUnit_internal(i);
  336. }
  337. }
  338. //-----------------------------------------------------------------------
  339. void RenderSystem::setTextureFiltering(UINT16 unit, FilterOptions minFilter,
  340. FilterOptions magFilter, FilterOptions mipFilter)
  341. {
  342. THROW_IF_INVALID_CONTEXT;
  343. CM_LOCK_MUTEX(mActiveContextMutex);
  344. mActiveContext->queueCommand(boost::bind(&RenderSystem::setTextureFiltering_internal, this, unit, minFilter, magFilter, mipFilter));
  345. }
  346. //-----------------------------------------------------------------------
  347. void RenderSystem::setTextureFiltering_internal(UINT16 unit, FilterOptions minFilter,
  348. FilterOptions magFilter, FilterOptions mipFilter)
  349. {
  350. THROW_IF_NOT_RENDER_THREAD;
  351. setTextureFiltering_internal(unit, FT_MIN, minFilter);
  352. setTextureFiltering_internal(unit, FT_MAG, magFilter);
  353. setTextureFiltering_internal(unit, FT_MIP, mipFilter);
  354. }
  355. //-----------------------------------------------------------------------
  356. void RenderSystem::setTextureAnisotropy(UINT16 unit, unsigned int maxAnisotropy)
  357. {
  358. THROW_IF_INVALID_CONTEXT;
  359. CM_LOCK_MUTEX(mActiveContextMutex);
  360. mActiveContext->queueCommand(boost::bind(&RenderSystem::setTextureAnisotropy_internal, this, unit, maxAnisotropy));
  361. }
  362. //-----------------------------------------------------------------------
  363. void RenderSystem::setTextureAddressingMode(UINT16 unit, const SamplerState::UVWAddressingMode& uvw)
  364. {
  365. THROW_IF_INVALID_CONTEXT;
  366. CM_LOCK_MUTEX(mActiveContextMutex);
  367. mActiveContext->queueCommand(boost::bind(&RenderSystem::setTextureAddressingMode_internal, this, unit, uvw));
  368. }
  369. //-----------------------------------------------------------------------
  370. void RenderSystem::setTextureBorderColor(UINT16 unit, const Color& color)
  371. {
  372. THROW_IF_INVALID_CONTEXT;
  373. CM_LOCK_MUTEX(mActiveContextMutex);
  374. mActiveContext->queueCommand(boost::bind(&RenderSystem::setTextureBorderColor_internal, this, unit, color));
  375. }
  376. //-----------------------------------------------------------------------
  377. void RenderSystem::setTextureMipmapBias(UINT16 unit, float bias)
  378. {
  379. THROW_IF_INVALID_CONTEXT;
  380. CM_LOCK_MUTEX(mActiveContextMutex);
  381. mActiveContext->queueCommand(boost::bind(&RenderSystem::setTextureMipmapBias_internal, this, unit, bias));
  382. }
  383. //-----------------------------------------------------------------------
  384. void RenderSystem::setPointParameters(float size, bool attenuationEnabled,
  385. float constant, float linear, float quadratic, float minSize, float maxSize)
  386. {
  387. THROW_IF_INVALID_CONTEXT;
  388. CM_LOCK_MUTEX(mActiveContextMutex);
  389. mActiveContext->queueCommand(boost::bind(&RenderSystem::setPointParameters_internal, this, size, attenuationEnabled, constant, linear, quadratic, minSize, maxSize));
  390. }
  391. //-----------------------------------------------------------------------
  392. void RenderSystem::setSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendOperation op)
  393. {
  394. THROW_IF_INVALID_CONTEXT;
  395. CM_LOCK_MUTEX(mActiveContextMutex);
  396. mActiveContext->queueCommand(boost::bind(&RenderSystem::setSceneBlending_internal, this, sourceFactor, destFactor, op));
  397. }
  398. //-----------------------------------------------------------------------
  399. void RenderSystem::setSeparateSceneBlending(SceneBlendFactor sourceFactor, SceneBlendFactor destFactor, SceneBlendFactor sourceFactorAlpha,
  400. SceneBlendFactor destFactorAlpha, SceneBlendOperation op, SceneBlendOperation alphaOp)
  401. {
  402. THROW_IF_INVALID_CONTEXT;
  403. CM_LOCK_MUTEX(mActiveContextMutex);
  404. mActiveContext->queueCommand(boost::bind(&RenderSystem::setSeparateSceneBlending_internal, this, sourceFactor, destFactor, sourceFactorAlpha, destFactorAlpha, op, alphaOp));
  405. }
  406. //-----------------------------------------------------------------------
  407. void RenderSystem::setAlphaRejectSettings(CompareFunction func, unsigned char value, bool alphaToCoverage)
  408. {
  409. THROW_IF_INVALID_CONTEXT;
  410. CM_LOCK_MUTEX(mActiveContextMutex);
  411. mActiveContext->queueCommand(boost::bind(&RenderSystem::setAlphaRejectSettings_internal, this, func, value, alphaToCoverage));
  412. }
  413. //-----------------------------------------------------------------------
  414. void RenderSystem::setScissorTest(bool enabled, UINT32 left, UINT32 top, UINT32 right, UINT32 bottom)
  415. {
  416. THROW_IF_INVALID_CONTEXT;
  417. CM_LOCK_MUTEX(mActiveContextMutex);
  418. mActiveContext->queueCommand(boost::bind(&RenderSystem::setScissorTest_internal, this, enabled, left, top, right, bottom));
  419. }
  420. //-----------------------------------------------------------------------
  421. bool RenderSystem::getWaitForVerticalBlank(void) const
  422. {
  423. THROW_IF_INVALID_CONTEXT;
  424. CM_LOCK_MUTEX(mActiveContextMutex);
  425. return mActiveContext->waitForVerticalBlank;
  426. }
  427. //-----------------------------------------------------------------------
  428. bool RenderSystem::getWaitForVerticalBlank_internal(void) const
  429. {
  430. THROW_IF_NOT_RENDER_THREAD;
  431. return mVsync;
  432. }
  433. //-----------------------------------------------------------------------
  434. void RenderSystem::setWaitForVerticalBlank(bool enabled)
  435. {
  436. THROW_IF_INVALID_CONTEXT;
  437. CM_LOCK_MUTEX(mActiveContextMutex);
  438. mActiveContext->waitForVerticalBlank = enabled;
  439. mActiveContext->queueCommand(boost::bind(&RenderSystem::setWaitForVerticalBlank_internal, this, enabled));
  440. }
  441. //-----------------------------------------------------------------------
  442. void RenderSystem::setWaitForVerticalBlank_internal(bool enabled)
  443. {
  444. THROW_IF_NOT_RENDER_THREAD;
  445. mVsync = enabled;
  446. }
  447. //-----------------------------------------------------------------------
  448. void RenderSystem::setInvertVertexWinding(bool invert)
  449. {
  450. THROW_IF_INVALID_CONTEXT;
  451. CM_LOCK_MUTEX(mActiveContextMutex);
  452. mActiveContext->invertVertexWinding = invert;
  453. mActiveContext->queueCommand(boost::bind(&RenderSystem::setInvertVertexWinding_internal, this, invert));
  454. }
  455. //-----------------------------------------------------------------------
  456. void RenderSystem::setInvertVertexWinding_internal(bool invert)
  457. {
  458. THROW_IF_NOT_RENDER_THREAD;
  459. mInvertVertexWinding = invert;
  460. }
  461. //-----------------------------------------------------------------------
  462. bool RenderSystem::getInvertVertexWinding(void) const
  463. {
  464. THROW_IF_INVALID_CONTEXT;
  465. CM_LOCK_MUTEX(mActiveContextMutex);
  466. return mActiveContext->invertVertexWinding;
  467. }
  468. //-----------------------------------------------------------------------
  469. bool RenderSystem::getInvertVertexWinding_internal(void) const
  470. {
  471. THROW_IF_NOT_RENDER_THREAD;
  472. return mInvertVertexWinding;
  473. }
  474. //-----------------------------------------------------------------------
  475. void RenderSystem::setDepthBufferParams(bool depthTest, bool depthWrite, CompareFunction depthFunction)
  476. {
  477. THROW_IF_INVALID_CONTEXT;
  478. CM_LOCK_MUTEX(mActiveContextMutex);
  479. mActiveContext->queueCommand(boost::bind(&RenderSystem::setDepthBufferParams_internal, this, depthTest, depthWrite, depthFunction));
  480. }
  481. //-----------------------------------------------------------------------
  482. void RenderSystem::setDepthBufferCheckEnabled(bool enabled)
  483. {
  484. THROW_IF_INVALID_CONTEXT;
  485. CM_LOCK_MUTEX(mActiveContextMutex);
  486. mActiveContext->queueCommand(boost::bind(&RenderSystem::setDepthBufferCheckEnabled_internal, this, enabled));
  487. }
  488. //-----------------------------------------------------------------------
  489. void RenderSystem::setDepthBufferWriteEnabled(bool enabled)
  490. {
  491. THROW_IF_INVALID_CONTEXT;
  492. CM_LOCK_MUTEX(mActiveContextMutex);
  493. mActiveContext->queueCommand(boost::bind(&RenderSystem::setDepthBufferWriteEnabled_internal, this, enabled));
  494. }
  495. //-----------------------------------------------------------------------
  496. void RenderSystem::setDepthBufferFunction(CompareFunction func)
  497. {
  498. THROW_IF_INVALID_CONTEXT;
  499. CM_LOCK_MUTEX(mActiveContextMutex);
  500. mActiveContext->queueCommand(boost::bind(&RenderSystem::setDepthBufferFunction_internal, this, func));
  501. }
  502. //-----------------------------------------------------------------------
  503. void RenderSystem::setColorBufferWriteEnabled(bool red, bool green, bool blue, bool alpha)
  504. {
  505. THROW_IF_INVALID_CONTEXT;
  506. CM_LOCK_MUTEX(mActiveContextMutex);
  507. mActiveContext->queueCommand(boost::bind(&RenderSystem::setColorBufferWriteEnabled_internal, this, red, green, blue, alpha));
  508. }
  509. //---------------------------------------------------------------------
  510. void RenderSystem::setDepthBias(float constantBias, float slopeScaleBias)
  511. {
  512. THROW_IF_INVALID_CONTEXT;
  513. CM_LOCK_MUTEX(mActiveContextMutex);
  514. mActiveContext->queueCommand(boost::bind(&RenderSystem::setDepthBias_internal, this, constantBias, slopeScaleBias));
  515. }
  516. //---------------------------------------------------------------------
  517. void RenderSystem::setPolygonMode(PolygonMode level)
  518. {
  519. THROW_IF_INVALID_CONTEXT;
  520. CM_LOCK_MUTEX(mActiveContextMutex);
  521. mActiveContext->queueCommand(boost::bind(&RenderSystem::setPolygonMode_internal, this, level));
  522. }
  523. //-----------------------------------------------------------------------
  524. void RenderSystem::setStencilCheckEnabled(bool enabled)
  525. {
  526. THROW_IF_INVALID_CONTEXT;
  527. CM_LOCK_MUTEX(mActiveContextMutex);
  528. mActiveContext->queueCommand(boost::bind(&RenderSystem::setStencilCheckEnabled_internal, this, enabled));
  529. }
  530. //-----------------------------------------------------------------------
  531. void RenderSystem::setStencilBufferParams(CompareFunction func, UINT32 refValue, UINT32 mask, StencilOperation stencilFailOp,
  532. StencilOperation depthFailOp, StencilOperation passOp, bool twoSidedOperation)
  533. {
  534. THROW_IF_INVALID_CONTEXT;
  535. CM_LOCK_MUTEX(mActiveContextMutex);
  536. mActiveContext->queueCommand(boost::bind(&RenderSystem::setStencilBufferParams_internal, this, func, refValue, mask, stencilFailOp, depthFailOp, passOp, twoSidedOperation));
  537. }
  538. //-----------------------------------------------------------------------
  539. CullingMode RenderSystem::getCullingMode(void) const
  540. {
  541. THROW_IF_INVALID_CONTEXT;
  542. CM_LOCK_MUTEX(mActiveContextMutex);
  543. return mActiveContext->cullingMode;
  544. }
  545. //-----------------------------------------------------------------------
  546. CullingMode RenderSystem::getCullingMode_internal(void) const
  547. {
  548. THROW_IF_NOT_RENDER_THREAD;
  549. return mCullingMode;
  550. }
  551. //-----------------------------------------------------------------------
  552. void RenderSystem::setCullingMode(CullingMode mode)
  553. {
  554. THROW_IF_INVALID_CONTEXT;
  555. CM_LOCK_MUTEX(mActiveContextMutex);
  556. mActiveContext->cullingMode = mode;
  557. mActiveContext->queueCommand(boost::bind(&RenderSystem::setCullingMode_internal, this, mode));
  558. }
  559. //---------------------------------------------------------------------
  560. void RenderSystem::addClipPlane(const Plane &p)
  561. {
  562. THROW_IF_INVALID_CONTEXT;
  563. CM_LOCK_MUTEX(mActiveContextMutex);
  564. mActiveContext->queueCommand(boost::bind(&RenderSystem::addClipPlane_internal, this, p));
  565. }
  566. //---------------------------------------------------------------------
  567. void RenderSystem::addClipPlane_internal (const Plane &p)
  568. {
  569. THROW_IF_NOT_RENDER_THREAD;
  570. mClipPlanes.push_back(p);
  571. mClipPlanesDirty = true;
  572. }
  573. //---------------------------------------------------------------------
  574. void RenderSystem::addClipPlane(float A, float B, float C, float D)
  575. {
  576. THROW_IF_INVALID_CONTEXT;
  577. CM_LOCK_MUTEX(mActiveContextMutex);
  578. mActiveContext->queueCommand(boost::bind(&RenderSystem::addClipPlane_internal, this, A, B, C, D));
  579. }
  580. //---------------------------------------------------------------------
  581. void RenderSystem::addClipPlane_internal (float A, float B, float C, float D)
  582. {
  583. THROW_IF_NOT_RENDER_THREAD;
  584. addClipPlane_internal(Plane(A, B, C, D));
  585. }
  586. //---------------------------------------------------------------------
  587. void RenderSystem::setClipPlanes(const PlaneList& clipPlanes)
  588. {
  589. THROW_IF_INVALID_CONTEXT;
  590. CM_LOCK_MUTEX(mActiveContextMutex);
  591. mActiveContext->queueCommand(boost::bind(&RenderSystem::setClipPlanes_internal, this, clipPlanes));
  592. }
  593. //---------------------------------------------------------------------
  594. void RenderSystem::setClipPlanes_internal(const PlaneList& clipPlanes)
  595. {
  596. THROW_IF_NOT_RENDER_THREAD;
  597. if (clipPlanes != mClipPlanes)
  598. {
  599. mClipPlanes = clipPlanes;
  600. mClipPlanesDirty = true;
  601. }
  602. }
  603. //---------------------------------------------------------------------
  604. void RenderSystem::resetClipPlanes()
  605. {
  606. THROW_IF_INVALID_CONTEXT;
  607. CM_LOCK_MUTEX(mActiveContextMutex);
  608. mActiveContext->queueCommand(boost::bind(&RenderSystem::resetClipPlanes_internal, this));
  609. }
  610. //---------------------------------------------------------------------
  611. void RenderSystem::resetClipPlanes_internal()
  612. {
  613. THROW_IF_NOT_RENDER_THREAD;
  614. if (!mClipPlanes.empty())
  615. {
  616. mClipPlanes.clear();
  617. mClipPlanesDirty = true;
  618. }
  619. }
  620. //-----------------------------------------------------------------------
  621. void RenderSystem::bindGpuProgram(GpuProgramHandle prg)
  622. {
  623. THROW_IF_INVALID_CONTEXT;
  624. CM_LOCK_MUTEX(mActiveContextMutex);
  625. mActiveContext->queueCommand(boost::bind(&RenderSystem::bindGpuProgram_internal, this, prg));
  626. }
  627. //-----------------------------------------------------------------------
  628. void RenderSystem::bindGpuProgram_internal(GpuProgramHandle prg)
  629. {
  630. THROW_IF_NOT_RENDER_THREAD;
  631. switch(prg->getBindingDelegate_internal()->getType())
  632. {
  633. case GPT_VERTEX_PROGRAM:
  634. // mark clip planes dirty if changed (programmable can change space)
  635. if (!mVertexProgramBound && !mClipPlanes.empty())
  636. mClipPlanesDirty = true;
  637. mVertexProgramBound = true;
  638. break;
  639. case GPT_GEOMETRY_PROGRAM:
  640. mGeometryProgramBound = true;
  641. break;
  642. case GPT_FRAGMENT_PROGRAM:
  643. mFragmentProgramBound = true;
  644. break;
  645. }
  646. }
  647. //-----------------------------------------------------------------------
  648. void RenderSystem::unbindGpuProgram(GpuProgramType gptype)
  649. {
  650. THROW_IF_INVALID_CONTEXT;
  651. CM_LOCK_MUTEX(mActiveContextMutex);
  652. mActiveContext->queueCommand(boost::bind(&RenderSystem::unbindGpuProgram_internal, this, gptype));
  653. }
  654. //-----------------------------------------------------------------------
  655. void RenderSystem::unbindGpuProgram_internal(GpuProgramType gptype)
  656. {
  657. THROW_IF_NOT_RENDER_THREAD;
  658. switch(gptype)
  659. {
  660. case GPT_VERTEX_PROGRAM:
  661. // mark clip planes dirty if changed (programmable can change space)
  662. if (mVertexProgramBound && !mClipPlanes.empty())
  663. mClipPlanesDirty = true;
  664. mVertexProgramBound = false;
  665. break;
  666. case GPT_GEOMETRY_PROGRAM:
  667. mGeometryProgramBound = false;
  668. break;
  669. case GPT_FRAGMENT_PROGRAM:
  670. mFragmentProgramBound = false;
  671. break;
  672. }
  673. }
  674. //-----------------------------------------------------------------------
  675. void RenderSystem::bindGpuProgramParameters(GpuProgramType gptype,
  676. GpuProgramParametersSharedPtr params, UINT16 variabilityMask)
  677. {
  678. THROW_IF_INVALID_CONTEXT;
  679. GpuProgramParametersSharedPtr paramCopy = GpuProgramParametersSharedPtr(new GpuProgramParameters(*params));
  680. CM_LOCK_MUTEX(mActiveContextMutex);
  681. mActiveContext->queueCommand(boost::bind(&RenderSystem::bindGpuProgramParameters_internal, this, gptype, paramCopy, variabilityMask));
  682. }
  683. //-----------------------------------------------------------------------
  684. bool RenderSystem::isGpuProgramBound_internal(GpuProgramType gptype)
  685. {
  686. THROW_IF_NOT_RENDER_THREAD;
  687. switch(gptype)
  688. {
  689. case GPT_VERTEX_PROGRAM:
  690. return mActiveContext->vertexProgramBound;
  691. case GPT_GEOMETRY_PROGRAM:
  692. return mActiveContext->geometryProgramBound;
  693. case GPT_FRAGMENT_PROGRAM:
  694. return mActiveContext->fragmentProgramBound;
  695. }
  696. // Make compiler happy
  697. return false;
  698. }
  699. //-----------------------------------------------------------------------
  700. void RenderSystem::setRenderTarget(RenderTarget *target)
  701. {
  702. THROW_IF_INVALID_CONTEXT;
  703. CM_LOCK_MUTEX(mActiveContextMutex);
  704. mActiveContext->queueCommand(boost::bind(&RenderSystem::setRenderTarget_internal, this, target));
  705. }
  706. //-----------------------------------------------------------------------
  707. void RenderSystem::clearFrameBuffer(unsigned int buffers, const Color& color, float depth, unsigned short stencil)
  708. {
  709. THROW_IF_INVALID_CONTEXT;
  710. CM_LOCK_MUTEX(mActiveContextMutex);
  711. mActiveContext->queueCommand(boost::bind(&RenderSystem::clearFrameBuffer_internal, this, buffers, color, depth, stencil));
  712. }
  713. //-----------------------------------------------------------------------
  714. void RenderSystem::beginFrame()
  715. {
  716. THROW_IF_INVALID_CONTEXT;
  717. CM_LOCK_MUTEX(mActiveContextMutex);
  718. mActiveContext->queueCommand(boost::bind(&RenderSystem::beginFrame_internal, this));
  719. }
  720. //-----------------------------------------------------------------------
  721. void RenderSystem::endFrame()
  722. {
  723. THROW_IF_INVALID_CONTEXT;
  724. CM_LOCK_MUTEX(mActiveContextMutex);
  725. mActiveContext->queueCommand(boost::bind(&RenderSystem::endFrame_internal, this));
  726. }
  727. //-----------------------------------------------------------------------
  728. void RenderSystem::render(const RenderOperation& op)
  729. {
  730. THROW_IF_INVALID_CONTEXT;
  731. CM_LOCK_MUTEX(mActiveContextMutex);
  732. mActiveContext->queueCommand(boost::bind(&RenderSystem::render_internal, this, op));
  733. }
  734. //-----------------------------------------------------------------------
  735. void RenderSystem::render_internal(const RenderOperation& op)
  736. {
  737. THROW_IF_NOT_RENDER_THREAD;
  738. // sort out clip planes
  739. // have to do it here in case of matrix issues
  740. if (mClipPlanesDirty)
  741. {
  742. setClipPlanesImpl(mClipPlanes);
  743. mClipPlanesDirty = false;
  744. }
  745. }
  746. /************************************************************************/
  747. /* PRIVATE */
  748. /************************************************************************/
  749. void RenderSystem::initRenderThread()
  750. {
  751. mRenderThreadFunc = new RenderWorkerFunc(this);
  752. #if CM_THREAD_SUPPORT
  753. CM_THREAD_CREATE(t, *mRenderThreadFunc);
  754. mRenderThread = t;
  755. CM_LOCK_MUTEX_NAMED(mRenderThreadStartMutex, lock)
  756. CM_THREAD_WAIT(mRenderThreadStartCondition, mRenderThreadStartMutex, lock)
  757. #else
  758. CM_EXCEPT(InternalErrorException, "Attempting to start a render thread but Camelot isn't compiled with thread support.");
  759. #endif
  760. }
  761. void RenderSystem::runRenderThread()
  762. {
  763. mRenderThreadId = CM_THREAD_CURRENT_ID;
  764. CM_THREAD_NOTIFY_ALL(mRenderThreadStartCondition)
  765. while(true)
  766. {
  767. if(mRenderThreadShutdown)
  768. return;
  769. // Wait until we get some ready commands
  770. vector<CommandQueue::Command>::type* commands = nullptr;
  771. {
  772. CM_LOCK_MUTEX_NAMED(mCommandQueueMutex, lock)
  773. while(mCommandQueue->isEmpty())
  774. CM_THREAD_WAIT(mCommandReadyCondition, mCommandQueueMutex, lock)
  775. commands = mCommandQueue->flush();
  776. }
  777. // Play commands
  778. mCommandQueue->playback(commands, boost::bind(&RenderSystem::commandCompletedNotify, this, _1));
  779. }
  780. }
  781. void RenderSystem::shutdownRenderThread()
  782. {
  783. mRenderThreadShutdown = true;
  784. // Wake all threads. They will quit after they see the shutdown flag
  785. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition)
  786. mRenderThread->join();
  787. CM_THREAD_DESTROY(mRenderThread);
  788. mRenderThread = nullptr;
  789. mRenderThreadId = CM_THREAD_CURRENT_ID;
  790. }
  791. DeferredRenderContextPtr RenderSystem::createDeferredContext()
  792. {
  793. return DeferredRenderContextPtr(new DeferredRenderContext(this, CM_THREAD_CURRENT_ID));
  794. }
  795. AsyncOp RenderSystem::queueReturnCommand(boost::function<void(AsyncOp&)> commandCallback, bool blockUntilComplete)
  796. {
  797. #ifdef CM_DEBUG_MODE
  798. if(CM_THREAD_CURRENT_ID == getRenderThreadId())
  799. CM_EXCEPT(InternalErrorException, "You are not allowed to call this method on the render thread!");
  800. #endif
  801. AsyncOp op;
  802. UINT32 commandId = -1;
  803. {
  804. CM_LOCK_MUTEX(mCommandQueueMutex);
  805. if(blockUntilComplete)
  806. {
  807. commandId = mMaxCommandNotifyId++;
  808. op = mCommandQueue->queueReturn(commandCallback, true, commandId);
  809. }
  810. else
  811. op = mCommandQueue->queueReturn(commandCallback);
  812. }
  813. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  814. if(blockUntilComplete)
  815. blockUntilCommandCompleted(commandId);
  816. return op;
  817. }
  818. void RenderSystem::queueCommand(boost::function<void()> commandCallback, bool blockUntilComplete)
  819. {
  820. #ifdef CM_DEBUG_MODE
  821. if(CM_THREAD_CURRENT_ID == getRenderThreadId())
  822. CM_EXCEPT(InternalErrorException, "You are not allowed to call this method on the render thread!");
  823. #endif
  824. UINT32 commandId = -1;
  825. {
  826. CM_LOCK_MUTEX(mCommandQueueMutex);
  827. if(blockUntilComplete)
  828. {
  829. commandId = mMaxCommandNotifyId++;
  830. mCommandQueue->queue(commandCallback, true, commandId);
  831. }
  832. else
  833. mCommandQueue->queue(commandCallback);
  834. }
  835. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  836. if(blockUntilComplete)
  837. blockUntilCommandCompleted(commandId);
  838. }
  839. void RenderSystem::blockUntilCommandCompleted(UINT32 commandId)
  840. {
  841. CM_LOCK_MUTEX_NAMED(mCommandNotifyMutex, lock);
  842. while(true)
  843. {
  844. // Check if our command id is in the completed list
  845. auto iter = mCommandsCompleted.begin();
  846. for(; iter != mCommandsCompleted.end(); ++iter)
  847. {
  848. if(*iter == commandId)
  849. break;
  850. }
  851. if(iter != mCommandsCompleted.end())
  852. {
  853. mCommandsCompleted.erase(iter);
  854. break;
  855. }
  856. CM_THREAD_WAIT(mCommandCompleteCondition, mCommandNotifyMutex, lock);
  857. }
  858. }
  859. void RenderSystem::commandCompletedNotify(UINT32 commandId)
  860. {
  861. {
  862. CM_LOCK_MUTEX(mCommandNotifyMutex);
  863. mCommandsCompleted.push_back(commandId);
  864. }
  865. CM_THREAD_NOTIFY_ALL(mCommandCompleteCondition);
  866. }
  867. void RenderSystem::throwIfNotRenderThread() const
  868. {
  869. if(CM_THREAD_CURRENT_ID != getRenderThreadId())
  870. CM_EXCEPT(InternalErrorException, "Calling the render system from a non-render thread!");
  871. }
  872. void RenderSystem::throwIfInvalidContextThread() const
  873. {
  874. if(CM_THREAD_CURRENT_ID != mActiveContext->getThreadId())
  875. CM_EXCEPT(InternalErrorException, "Active context is called from a thread it was not initialized on!");
  876. }
  877. /************************************************************************/
  878. /* THREAD WORKER */
  879. /************************************************************************/
  880. RenderSystem::RenderWorkerFunc::RenderWorkerFunc(RenderSystem* rs)
  881. :mRS(rs)
  882. {
  883. assert(mRS != nullptr);
  884. }
  885. void RenderSystem::RenderWorkerFunc::operator()()
  886. {
  887. mRS->runRenderThread();
  888. }
  889. }
  890. #undef THROW_IF_INVALID_CONTEXT
  891. #undef THROW_IF_NOT_RENDER_THREAD