2
0

renderTerrainMgr.cpp 6.4 KB

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