2
0

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