renderTerrainMgr.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include "materials/materialManager.h"
  37. bool RenderTerrainMgr::smRenderWireframe = false;
  38. S32 RenderTerrainMgr::smCellsRendered = 0;
  39. S32 RenderTerrainMgr::smOverrideCells = 0;
  40. S32 RenderTerrainMgr::smDrawCalls = 0;
  41. IMPLEMENT_CONOBJECT(RenderTerrainMgr);
  42. ConsoleDocClass( RenderTerrainMgr,
  43. "@brief A render bin for terrain mesh rendering.\n\n"
  44. "This bin renders terrain render instances from a TerrainBlock. "
  45. "Normally a mesh would render via the RenderMeshMgr, but terrain uses "
  46. "a TerrainMaterial designed for multi-layered surfaces which this "
  47. "bin can processs.\n\n"
  48. "@ingroup RenderBin\n" );
  49. RenderTerrainMgr::RenderTerrainMgr()
  50. : RenderBinManager( RenderPassManager::RIT_Terrain, 1.0f, 1.0f )
  51. {
  52. }
  53. RenderTerrainMgr::RenderTerrainMgr( F32 renderOrder, F32 processAddOrder )
  54. : RenderBinManager( RenderPassManager::RIT_Terrain, renderOrder, processAddOrder )
  55. {
  56. }
  57. RenderTerrainMgr::~RenderTerrainMgr()
  58. {
  59. }
  60. void RenderTerrainMgr::initPersistFields()
  61. {
  62. Con::addVariable( "RenderTerrainMgr::renderWireframe", TypeBool, &smRenderWireframe,
  63. "Used to enable wireframe rendering on terrain for debugging.\n"
  64. "@ingroup RenderBin\n" );
  65. // For stats.
  66. GFXDevice::getDeviceEventSignal().notify( &RenderTerrainMgr::_clearStats );
  67. Con::addVariable( "$TerrainBlock::cellsRendered", TypeS32, &smCellsRendered, "@internal" );
  68. Con::addVariable( "$TerrainBlock::overrideCells", TypeS32, &smOverrideCells, "@internal" );
  69. Con::addVariable( "$TerrainBlock::drawCalls", TypeS32, &smDrawCalls, "@internal" );
  70. Parent::initPersistFields();
  71. }
  72. bool RenderTerrainMgr::_clearStats( GFXDevice::GFXDeviceEventType type )
  73. {
  74. if ( type == GFXDevice::deStartOfFrame )
  75. {
  76. smCellsRendered = 0;
  77. smOverrideCells = 0;
  78. smDrawCalls = 0;
  79. }
  80. return true;
  81. }
  82. void RenderTerrainMgr::internalAddElement( RenderInst *inst_ )
  83. {
  84. TerrainRenderInst *inst = static_cast<TerrainRenderInst*>( inst_ );
  85. mInstVector.push_back( inst );
  86. }
  87. void RenderTerrainMgr::sort()
  88. {
  89. // TODO: We could probably sort this in some
  90. // manner to improve terrain rendering perf.
  91. }
  92. void RenderTerrainMgr::clear()
  93. {
  94. mInstVector.clear();
  95. }
  96. void RenderTerrainMgr::render( SceneRenderState *state )
  97. {
  98. if ( mInstVector.empty() )
  99. return;
  100. // Check if bin is disabled in advanced lighting.
  101. if ( MATMGR->getDeferredEnabled() && mBasicOnly )
  102. return;
  103. PROFILE_SCOPE( RenderTerrainMgr_Render );
  104. GFXTransformSaver saver;
  105. // Prepare the common scene graph data.
  106. SceneData sgData;
  107. sgData.init( state );
  108. sgData.wireframe |= smRenderWireframe;
  109. // Restore transforms
  110. MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
  111. matrixSet.restoreSceneViewProjection();
  112. GFXDEBUGEVENT_SCOPE( RenderTerrainMgr_Render, ColorI::GREEN );
  113. const MatrixF worldViewXfm = matrixSet.getWorldToCamera();
  114. const MatrixF &projXfm = matrixSet.getCameraToScreen();
  115. // HACKTASTIC!
  116. //
  117. // If this is a shadow pass then render the terrain
  118. // with the first material we get.
  119. //
  120. // In the near future terrains will operate using regular
  121. // MatInstance materials like any other mesh in which case
  122. // this will all be replaced by a normal mesh render bin.
  123. //
  124. if ( state->isShadowPass() )
  125. {
  126. PROFILE_SCOPE( RenderTerrainMgr_Render_Shadow );
  127. Vector<TerrainRenderInst*>::iterator inst = mInstVector.begin();
  128. BaseMatInstance *overideMat = (*inst)->mat;
  129. while ( overideMat && overideMat->setupPass( state, sgData ) )
  130. {
  131. for ( ; inst != mInstVector.end(); inst++ )
  132. {
  133. smOverrideCells++;
  134. GFX->setPrimitiveBuffer( (*inst)->primBuff );
  135. GFX->setVertexBuffer( (*inst)->vertBuff );
  136. matrixSet.setWorld(*(*inst)->objectToWorldXfm);
  137. overideMat->setSceneInfo( state, sgData );
  138. overideMat->setTransforms( matrixSet, state );
  139. GFX->drawPrimitive( (*inst)->prim );
  140. }
  141. }
  142. return;
  143. }
  144. // Do the detail map passes.
  145. Vector<TerrainRenderInst*>::iterator inst = mInstVector.begin();
  146. for ( ; inst != mInstVector.end(); inst++ )
  147. {
  148. TerrainCellMaterial *mat = (*inst)->cellMat;
  149. GFX->setPrimitiveBuffer( (*inst)->primBuff );
  150. GFX->setVertexBuffer( (*inst)->vertBuff );
  151. ++smCellsRendered;
  152. mat->setTransformAndEye( *(*inst)->objectToWorldXfm,
  153. worldViewXfm,
  154. projXfm,
  155. state->getFarPlane() );
  156. sgData.objTrans = (*inst)->objectToWorldXfm;
  157. dMemcpy( sgData.lights, (*inst)->lights, sizeof( sgData.lights ) );
  158. while ( mat->setupPass( state, sgData ) )
  159. {
  160. ++smDrawCalls;
  161. GFX->drawPrimitive( (*inst)->prim );
  162. }
  163. }
  164. }