CmRenderSystem.cpp 38 KB

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