CmRenderSystem.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. namespace CamelotEngine {
  37. static const TexturePtr sNullTexPtr;
  38. //-----------------------------------------------------------------------
  39. RenderSystem::RenderSystem()
  40. : mActiveRenderTarget(0)
  41. , mActiveViewport(0)
  42. // This means CULL clockwise vertices, i.e. front of poly is counter-clockwise
  43. // This makes it the same as OpenGL and other right-handed systems
  44. , mCullingMode(CULL_CLOCKWISE)
  45. , mVSync(true)
  46. , mVSyncInterval(1)
  47. , mInvertVertexWinding(false)
  48. , mDisabledTexUnitsFrom(0)
  49. , mVertexProgramBound(false)
  50. , mGeometryProgramBound(false)
  51. , mFragmentProgramBound(false)
  52. , mClipPlanesDirty(true)
  53. , mRealCapabilities(0)
  54. , mCurrentCapabilities(0)
  55. , mUseCustomCapabilities(false)
  56. {
  57. }
  58. //-----------------------------------------------------------------------
  59. RenderSystem::~RenderSystem()
  60. {
  61. shutdown();
  62. delete mRealCapabilities;
  63. mRealCapabilities = 0;
  64. // Current capabilities managed externally
  65. mCurrentCapabilities = 0;
  66. }
  67. //-----------------------------------------------------------------------
  68. void RenderSystem::swapAllRenderTargetBuffers(bool waitForVSync)
  69. {
  70. // Update all in order of priority
  71. // This ensures render-to-texture targets get updated before render windows
  72. RenderTargetPriorityMap::iterator itarg, itargend;
  73. itargend = mPrioritisedRenderTargets.end();
  74. for( itarg = mPrioritisedRenderTargets.begin(); itarg != itargend; ++itarg )
  75. {
  76. if( itarg->second->isActive())
  77. itarg->second->swapBuffers(waitForVSync);
  78. }
  79. }
  80. //-----------------------------------------------------------------------
  81. RenderWindow* RenderSystem::_initialise(bool autoCreateWindow, const String& windowTitle)
  82. {
  83. // Have I been registered by call to Root::setRenderSystem?
  84. /** Don't do this anymore, just allow via Root
  85. RenderSystem* regPtr = Root::getSingleton().getRenderSystem();
  86. if (!regPtr || regPtr != this)
  87. // Register self - library user has come to me direct
  88. Root::getSingleton().setRenderSystem(this);
  89. */
  90. // Subclasses should take it from here
  91. // They should ALL call this superclass method from
  92. // their own initialise() implementations.
  93. mVertexProgramBound = false;
  94. mGeometryProgramBound = false;
  95. mFragmentProgramBound = false;
  96. return 0;
  97. }
  98. //---------------------------------------------------------------------------------------------
  99. void RenderSystem::useCustomRenderSystemCapabilities(RenderSystemCapabilities* capabilities)
  100. {
  101. if (mRealCapabilities != 0)
  102. {
  103. CM_EXCEPT(InternalErrorException,
  104. "Custom render capabilities must be set before the RenderSystem is initialised.");
  105. }
  106. mCurrentCapabilities = capabilities;
  107. mUseCustomCapabilities = true;
  108. }
  109. //---------------------------------------------------------------------------------------------
  110. bool RenderSystem::_createRenderWindows(const RenderWindowDescriptionList& renderWindowDescriptions,
  111. RenderWindowList& createdWindows)
  112. {
  113. unsigned int fullscreenWindowsCount = 0;
  114. // Grab some information and avoid duplicate render windows.
  115. for (unsigned int nWindow=0; nWindow < renderWindowDescriptions.size(); ++nWindow)
  116. {
  117. const RenderWindowDescription* curDesc = &renderWindowDescriptions[nWindow];
  118. // Count full screen windows.
  119. if (curDesc->useFullScreen)
  120. fullscreenWindowsCount++;
  121. bool renderWindowFound = false;
  122. if (mRenderTargets.find(curDesc->name) != mRenderTargets.end())
  123. renderWindowFound = true;
  124. else
  125. {
  126. for (unsigned int nSecWindow = nWindow + 1 ; nSecWindow < renderWindowDescriptions.size(); ++nSecWindow)
  127. {
  128. if (curDesc->name == renderWindowDescriptions[nSecWindow].name)
  129. {
  130. renderWindowFound = true;
  131. break;
  132. }
  133. }
  134. }
  135. // Make sure we don't already have a render target of the
  136. // same name as the one supplied
  137. if(renderWindowFound)
  138. {
  139. String msg;
  140. msg = "A render target of the same name '" + String(curDesc->name) + "' already "
  141. "exists. You cannot create a new window with this name.";
  142. CM_EXCEPT(InternalErrorException, msg);
  143. }
  144. }
  145. // Case we have to create some full screen rendering windows.
  146. if (fullscreenWindowsCount > 0)
  147. {
  148. // Can not mix full screen and windowed rendering windows.
  149. if (fullscreenWindowsCount != renderWindowDescriptions.size())
  150. {
  151. CM_EXCEPT(InvalidParametersException,
  152. "Can not create mix of full screen and windowed rendering windows");
  153. }
  154. }
  155. return true;
  156. }
  157. //---------------------------------------------------------------------------------------------
  158. void RenderSystem::destroyRenderWindow(const String& name)
  159. {
  160. destroyRenderTarget(name);
  161. }
  162. //---------------------------------------------------------------------------------------------
  163. void RenderSystem::destroyRenderTexture(const String& name)
  164. {
  165. destroyRenderTarget(name);
  166. }
  167. //---------------------------------------------------------------------------------------------
  168. void RenderSystem::destroyRenderTarget(const String& name)
  169. {
  170. RenderTarget* rt = detachRenderTarget(name);
  171. delete rt;
  172. }
  173. //---------------------------------------------------------------------------------------------
  174. void RenderSystem::attachRenderTarget( RenderTarget &target )
  175. {
  176. assert( target.getPriority() < OGRE_NUM_RENDERTARGET_GROUPS );
  177. mRenderTargets.insert( RenderTargetMap::value_type( target.getName(), &target ) );
  178. mPrioritisedRenderTargets.insert(
  179. RenderTargetPriorityMap::value_type(target.getPriority(), &target ));
  180. }
  181. //---------------------------------------------------------------------------------------------
  182. RenderTarget * RenderSystem::getRenderTarget( const String &name )
  183. {
  184. RenderTargetMap::iterator it = mRenderTargets.find( name );
  185. RenderTarget *ret = NULL;
  186. if( it != mRenderTargets.end() )
  187. {
  188. ret = it->second;
  189. }
  190. return ret;
  191. }
  192. //---------------------------------------------------------------------------------------------
  193. RenderTarget * RenderSystem::detachRenderTarget( const String &name )
  194. {
  195. RenderTargetMap::iterator it = mRenderTargets.find( name );
  196. RenderTarget *ret = NULL;
  197. if( it != mRenderTargets.end() )
  198. {
  199. ret = it->second;
  200. /* Remove the render target from the priority groups. */
  201. RenderTargetPriorityMap::iterator itarg, itargend;
  202. itargend = mPrioritisedRenderTargets.end();
  203. for( itarg = mPrioritisedRenderTargets.begin(); itarg != itargend; ++itarg )
  204. {
  205. if( itarg->second == ret ) {
  206. mPrioritisedRenderTargets.erase( itarg );
  207. break;
  208. }
  209. }
  210. mRenderTargets.erase( it );
  211. }
  212. /// If detached render target is the active render target, reset active render target
  213. if(ret == mActiveRenderTarget)
  214. mActiveRenderTarget = 0;
  215. return ret;
  216. }
  217. //-----------------------------------------------------------------------
  218. Viewport* RenderSystem::getViewport(void)
  219. {
  220. return mActiveViewport;
  221. }
  222. //-----------------------------------------------------------------------
  223. void RenderSystem::setTextureUnitSettings(size_t texUnit, const TexturePtr& tex, const SamplerState& tl)
  224. {
  225. // This method is only ever called to set a texture unit to valid details
  226. // The method _disableTextureUnit is called to turn a unit off
  227. // Vertex texture binding?
  228. if (mCurrentCapabilities->hasCapability(RSC_VERTEX_TEXTURE_FETCH) &&
  229. !mCurrentCapabilities->getVertexTextureUnitsShared())
  230. {
  231. if (tl.getBindingType() == SamplerState::BT_VERTEX)
  232. {
  233. // Bind vertex texture
  234. setVertexTexture(texUnit, tex);
  235. // bind nothing to fragment unit (hardware isn't shared but fragment
  236. // unit can't be using the same index
  237. setTexture(texUnit, true, sNullTexPtr);
  238. }
  239. else
  240. {
  241. // vice versa
  242. setVertexTexture(texUnit, sNullTexPtr);
  243. setTexture(texUnit, true, tex);
  244. }
  245. }
  246. else
  247. {
  248. // Shared vertex / fragment textures or no vertex texture support
  249. // Bind texture (may be blank)
  250. setTexture(texUnit, true, tex);
  251. }
  252. // Set texture layer filtering
  253. setTextureFiltering(texUnit,
  254. tl.getTextureFiltering(FT_MIN),
  255. tl.getTextureFiltering(FT_MAG),
  256. tl.getTextureFiltering(FT_MIP));
  257. // Set texture layer filtering
  258. setTextureAnisotropy(texUnit, tl.getTextureAnisotropy());
  259. // Set mipmap biasing
  260. setTextureMipmapBias(texUnit, tl.getTextureMipmapBias());
  261. // Texture addressing mode
  262. const SamplerState::UVWAddressingMode& uvw = tl.getTextureAddressingMode();
  263. setTextureAddressingMode(texUnit, uvw);
  264. }
  265. //-----------------------------------------------------------------------
  266. void RenderSystem::setVertexTexture(size_t unit, const TexturePtr& tex)
  267. {
  268. CM_EXCEPT(NotImplementedException,
  269. "This rendersystem does not support separate vertex texture samplers, "
  270. "you should use the regular texture samplers which are shared between "
  271. "the vertex and fragment units.");
  272. }
  273. //-----------------------------------------------------------------------
  274. void RenderSystem::disableTextureUnit(size_t texUnit)
  275. {
  276. setTexture(texUnit, false, sNullTexPtr);
  277. }
  278. //---------------------------------------------------------------------
  279. void RenderSystem::disableTextureUnitsFrom(size_t texUnit)
  280. {
  281. size_t disableTo = CM_MAX_TEXTURE_LAYERS;
  282. if (disableTo > mDisabledTexUnitsFrom)
  283. disableTo = mDisabledTexUnitsFrom;
  284. mDisabledTexUnitsFrom = texUnit;
  285. for (size_t i = texUnit; i < disableTo; ++i)
  286. {
  287. disableTextureUnit(i);
  288. }
  289. }
  290. //-----------------------------------------------------------------------
  291. void RenderSystem::setTextureFiltering(size_t unit, FilterOptions minFilter,
  292. FilterOptions magFilter, FilterOptions mipFilter)
  293. {
  294. setTextureFiltering(unit, FT_MIN, minFilter);
  295. setTextureFiltering(unit, FT_MAG, magFilter);
  296. setTextureFiltering(unit, FT_MIP, mipFilter);
  297. }
  298. //-----------------------------------------------------------------------
  299. CullingMode RenderSystem::getCullingMode(void) const
  300. {
  301. return mCullingMode;
  302. }
  303. //-----------------------------------------------------------------------
  304. bool RenderSystem::getWaitForVerticalBlank(void) const
  305. {
  306. return mVSync;
  307. }
  308. //-----------------------------------------------------------------------
  309. void RenderSystem::setWaitForVerticalBlank(bool enabled)
  310. {
  311. mVSync = enabled;
  312. }
  313. //-----------------------------------------------------------------------
  314. void RenderSystem::shutdown(void)
  315. {
  316. // Remove all the render targets.
  317. // (destroy primary target last since others may depend on it)
  318. RenderTarget* primary = 0;
  319. for (RenderTargetMap::iterator it = mRenderTargets.begin(); it != mRenderTargets.end(); ++it)
  320. {
  321. if (!primary && it->second->isPrimary())
  322. primary = it->second;
  323. else
  324. delete it->second;
  325. }
  326. delete primary;
  327. mRenderTargets.clear();
  328. mPrioritisedRenderTargets.clear();
  329. }
  330. //-----------------------------------------------------------------------
  331. void RenderSystem::beginGeometryCount(void)
  332. {
  333. mBatchCount = mFaceCount = mVertexCount = 0;
  334. }
  335. //-----------------------------------------------------------------------
  336. unsigned int RenderSystem::getFaceCount(void) const
  337. {
  338. return static_cast< unsigned int >( mFaceCount );
  339. }
  340. //-----------------------------------------------------------------------
  341. unsigned int RenderSystem::getBatchCount(void) const
  342. {
  343. return static_cast< unsigned int >( mBatchCount );
  344. }
  345. //-----------------------------------------------------------------------
  346. unsigned int RenderSystem::getVertexCount(void) const
  347. {
  348. return static_cast< unsigned int >( mVertexCount );
  349. }
  350. //-----------------------------------------------------------------------
  351. void RenderSystem::convertColorValue(const Color& colour, UINT32* pDest)
  352. {
  353. *pDest = VertexElement::convertColourValue(colour, getColorVertexElementType());
  354. }
  355. //-----------------------------------------------------------------------
  356. void RenderSystem::render(const RenderOperation& op)
  357. {
  358. // Update stats
  359. size_t val;
  360. if (op.useIndexes)
  361. val = op.indexData->indexCount;
  362. else
  363. val = op.vertexData->vertexCount;
  364. switch(op.operationType)
  365. {
  366. case RenderOperation::OT_TRIANGLE_LIST:
  367. mFaceCount += val / 3;
  368. break;
  369. case RenderOperation::OT_TRIANGLE_STRIP:
  370. case RenderOperation::OT_TRIANGLE_FAN:
  371. mFaceCount += val - 2;
  372. break;
  373. case RenderOperation::OT_POINT_LIST:
  374. case RenderOperation::OT_LINE_LIST:
  375. case RenderOperation::OT_LINE_STRIP:
  376. break;
  377. }
  378. mVertexCount += op.vertexData->vertexCount;
  379. mBatchCount += 1;
  380. // sort out clip planes
  381. // have to do it here in case of matrix issues
  382. if (mClipPlanesDirty)
  383. {
  384. setClipPlanesImpl(mClipPlanes);
  385. mClipPlanesDirty = false;
  386. }
  387. }
  388. //-----------------------------------------------------------------------
  389. void RenderSystem::setInvertVertexWinding(bool invert)
  390. {
  391. mInvertVertexWinding = invert;
  392. }
  393. //-----------------------------------------------------------------------
  394. bool RenderSystem::getInvertVertexWinding(void) const
  395. {
  396. return mInvertVertexWinding;
  397. }
  398. //---------------------------------------------------------------------
  399. void RenderSystem::addClipPlane (const Plane &p)
  400. {
  401. mClipPlanes.push_back(p);
  402. mClipPlanesDirty = true;
  403. }
  404. //---------------------------------------------------------------------
  405. void RenderSystem::addClipPlane (float A, float B, float C, float D)
  406. {
  407. addClipPlane(Plane(A, B, C, D));
  408. }
  409. //---------------------------------------------------------------------
  410. void RenderSystem::setClipPlanes(const PlaneList& clipPlanes)
  411. {
  412. if (clipPlanes != mClipPlanes)
  413. {
  414. mClipPlanes = clipPlanes;
  415. mClipPlanesDirty = true;
  416. }
  417. }
  418. //---------------------------------------------------------------------
  419. void RenderSystem::resetClipPlanes()
  420. {
  421. if (!mClipPlanes.empty())
  422. {
  423. mClipPlanes.clear();
  424. mClipPlanesDirty = true;
  425. }
  426. }
  427. //-----------------------------------------------------------------------
  428. void RenderSystem::bindGpuProgram(GpuProgram* prg)
  429. {
  430. switch(prg->getType())
  431. {
  432. case GPT_VERTEX_PROGRAM:
  433. // mark clip planes dirty if changed (programmable can change space)
  434. if (!mVertexProgramBound && !mClipPlanes.empty())
  435. mClipPlanesDirty = true;
  436. mVertexProgramBound = true;
  437. break;
  438. case GPT_GEOMETRY_PROGRAM:
  439. mGeometryProgramBound = true;
  440. break;
  441. case GPT_FRAGMENT_PROGRAM:
  442. mFragmentProgramBound = true;
  443. break;
  444. }
  445. }
  446. //-----------------------------------------------------------------------
  447. void RenderSystem::unbindGpuProgram(GpuProgramType gptype)
  448. {
  449. switch(gptype)
  450. {
  451. case GPT_VERTEX_PROGRAM:
  452. // mark clip planes dirty if changed (programmable can change space)
  453. if (mVertexProgramBound && !mClipPlanes.empty())
  454. mClipPlanesDirty = true;
  455. mVertexProgramBound = false;
  456. break;
  457. case GPT_GEOMETRY_PROGRAM:
  458. mGeometryProgramBound = false;
  459. break;
  460. case GPT_FRAGMENT_PROGRAM:
  461. mFragmentProgramBound = false;
  462. break;
  463. }
  464. }
  465. //-----------------------------------------------------------------------
  466. bool RenderSystem::isGpuProgramBound(GpuProgramType gptype)
  467. {
  468. switch(gptype)
  469. {
  470. case GPT_VERTEX_PROGRAM:
  471. return mVertexProgramBound;
  472. case GPT_GEOMETRY_PROGRAM:
  473. return mGeometryProgramBound;
  474. case GPT_FRAGMENT_PROGRAM:
  475. return mFragmentProgramBound;
  476. }
  477. // Make compiler happy
  478. return false;
  479. }
  480. }