renderTerrainMgr.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "renderInstance/renderTerrainMgr.h"
  24. #include "platform/profiler.h"
  25. #include "scene/sceneManager.h"
  26. #include "gfx/gfxTransformSaver.h"
  27. #include "gfx/gfxDrawUtil.h"
  28. #include "gfx/gfxDebugEvent.h"
  29. #include "materials/shaderData.h"
  30. #include "materials/matInstance.h"
  31. #include "scene/sceneRenderState.h"
  32. #include "console/consoleTypes.h"
  33. #include "terrain/terrCell.h"
  34. #include "terrain/terrCellMaterial.h"
  35. #include "math/util/matrixSet.h"
  36. bool RenderTerrainMgr::smRenderWireframe = false;
  37. S32 RenderTerrainMgr::smCellsRendered = 0;
  38. S32 RenderTerrainMgr::smOverrideCells = 0;
  39. S32 RenderTerrainMgr::smDrawCalls = 0;
  40. IMPLEMENT_CONOBJECT(RenderTerrainMgr);
  41. ConsoleDocClass( RenderTerrainMgr,
  42. "@brief A render bin for terrain mesh rendering.\n\n"
  43. "This bin renders terrain render instances from a TerrainBlock. "
  44. "Normally a mesh would render via the RenderMeshMgr, but terrain uses "
  45. "a TerrainMaterial designed for multi-layered surfaces which this "
  46. "bin can processs.\n\n"
  47. "@ingroup RenderBin\n" );
  48. RenderTerrainMgr::RenderTerrainMgr()
  49. : RenderBinManager( RenderPassManager::RIT_Terrain, 1.0f, 1.0f )
  50. {
  51. }
  52. RenderTerrainMgr::RenderTerrainMgr( F32 renderOrder, F32 processAddOrder )
  53. : RenderBinManager( RenderPassManager::RIT_Terrain, renderOrder, processAddOrder )
  54. {
  55. }
  56. RenderTerrainMgr::~RenderTerrainMgr()
  57. {
  58. }
  59. void RenderTerrainMgr::initPersistFields()
  60. {
  61. Con::addVariable( "RenderTerrainMgr::renderWireframe", TypeBool, &smRenderWireframe,
  62. "Used to enable wireframe rendering on terrain for debugging.\n"
  63. "@ingroup RenderBin\n" );
  64. // For stats.
  65. GFXDevice::getDeviceEventSignal().notify( &RenderTerrainMgr::_clearStats );
  66. Con::addVariable( "$TerrainBlock::cellsRendered", TypeS32, &smCellsRendered, "@internal" );
  67. Con::addVariable( "$TerrainBlock::overrideCells", TypeS32, &smOverrideCells, "@internal" );
  68. Con::addVariable( "$TerrainBlock::drawCalls", TypeS32, &smDrawCalls, "@internal" );
  69. Parent::initPersistFields();
  70. }
  71. bool RenderTerrainMgr::_clearStats( GFXDevice::GFXDeviceEventType type )
  72. {
  73. if ( type == GFXDevice::deStartOfFrame )
  74. {
  75. smCellsRendered = 0;
  76. smOverrideCells = 0;
  77. smDrawCalls = 0;
  78. }
  79. return true;
  80. }
  81. void RenderTerrainMgr::internalAddElement( RenderInst *inst_ )
  82. {
  83. TerrainRenderInst *inst = static_cast<TerrainRenderInst*>( inst_ );
  84. mInstVector.push_back( inst );
  85. }
  86. void RenderTerrainMgr::sort()
  87. {
  88. // TODO: We could probably sort this in some
  89. // manner to improve terrain rendering perf.
  90. }
  91. void RenderTerrainMgr::clear()
  92. {
  93. mInstVector.clear();
  94. }
  95. void RenderTerrainMgr::render( SceneRenderState *state )
  96. {
  97. if ( mInstVector.empty() )
  98. return;
  99. PROFILE_SCOPE( RenderTerrainMgr_Render );
  100. GFXTransformSaver saver;
  101. // Prepare the common scene graph data.
  102. SceneData sgData;
  103. sgData.init( state );
  104. sgData.wireframe |= smRenderWireframe;
  105. // Restore transforms
  106. MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
  107. matrixSet.restoreSceneViewProjection();
  108. GFXDEBUGEVENT_SCOPE( RenderTerrainMgr_Render, ColorI::GREEN );
  109. const MatrixF worldViewXfm = matrixSet.getWorldToCamera();
  110. const MatrixF &projXfm = matrixSet.getCameraToScreen();
  111. // HACKTASTIC!
  112. //
  113. // If this is a shadow pass then render the terrain
  114. // with the first material we get.
  115. //
  116. // In the near future terrains will operate using regular
  117. // MatInstance materials like any other mesh in which case
  118. // this will all be replaced by a normal mesh render bin.
  119. //
  120. if ( state->isShadowPass() )
  121. {
  122. PROFILE_SCOPE( RenderTerrainMgr_Render_Shadow );
  123. Vector<TerrainRenderInst*>::iterator inst = mInstVector.begin();
  124. BaseMatInstance *overideMat = (*inst)->mat;
  125. while ( overideMat && overideMat->setupPass( state, sgData ) )
  126. {
  127. for ( ; inst != mInstVector.end(); inst++ )
  128. {
  129. smOverrideCells++;
  130. GFX->setPrimitiveBuffer( (*inst)->primBuff );
  131. GFX->setVertexBuffer( (*inst)->vertBuff );
  132. matrixSet.setWorld(*(*inst)->objectToWorldXfm);
  133. overideMat->setSceneInfo( state, sgData );
  134. overideMat->setTransforms( matrixSet, state );
  135. GFX->drawPrimitive( (*inst)->prim );
  136. }
  137. }
  138. return;
  139. }
  140. // Do the detail map passes.
  141. Vector<TerrainRenderInst*>::iterator inst = mInstVector.begin();
  142. for ( ; inst != mInstVector.end(); inst++ )
  143. {
  144. TerrainCellMaterial *mat = (*inst)->cellMat;
  145. GFX->setPrimitiveBuffer( (*inst)->primBuff );
  146. GFX->setVertexBuffer( (*inst)->vertBuff );
  147. ++smCellsRendered;
  148. mat->setTransformAndEye( *(*inst)->objectToWorldXfm,
  149. worldViewXfm,
  150. projXfm,
  151. state->getFarPlane() );
  152. sgData.objTrans = (*inst)->objectToWorldXfm;
  153. dMemcpy( sgData.lights, (*inst)->lights, sizeof( sgData.lights ) );
  154. while ( mat->setupPass( state, sgData ) )
  155. {
  156. ++smDrawCalls;
  157. GFX->drawPrimitive( (*inst)->prim );
  158. }
  159. }
  160. }