CmRenderSystem.cpp 33 KB

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