shadowMapPass.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "lighting/shadowMap/shadowMapPass.h"
  24. #include "lighting/shadowMap/lightShadowMap.h"
  25. #include "lighting/shadowMap/shadowMapManager.h"
  26. #include "lighting/lightManager.h"
  27. #include "scene/sceneManager.h"
  28. #include "scene/sceneRenderState.h"
  29. #include "renderInstance/renderPassManager.h"
  30. #include "renderInstance/renderObjectMgr.h"
  31. #include "renderInstance/renderMeshMgr.h"
  32. #include "renderInstance/renderTerrainMgr.h"
  33. #include "renderInstance/renderImposterMgr.h"
  34. #include "core/util/safeDelete.h"
  35. #include "console/consoleTypes.h"
  36. #include "gfx/gfxTransformSaver.h"
  37. #include "gfx/gfxDebugEvent.h"
  38. #include "platform/platformTimer.h"
  39. const String ShadowMapPass::PassTypeName("ShadowMap");
  40. U32 ShadowMapPass::smActiveShadowMaps = 0;
  41. U32 ShadowMapPass::smUpdatedShadowMaps = 0;
  42. U32 ShadowMapPass::smNearShadowMaps = 0;
  43. U32 ShadowMapPass::smShadowMapsDrawCalls = 0;
  44. U32 ShadowMapPass::smShadowMapPolyCount = 0;
  45. U32 ShadowMapPass::smRenderTargetChanges = 0;
  46. U32 ShadowMapPass::smShadowPoolTexturesCount = 0.;
  47. F32 ShadowMapPass::smShadowPoolMemory = 0.0f;
  48. bool ShadowMapPass::smDisableShadows = false;
  49. bool ShadowMapPass::smDisableShadowsEditor = false;
  50. bool ShadowMapPass::smDisableShadowsPref = false;
  51. /// We have a default 8ms render budget for shadow rendering.
  52. U32 ShadowMapPass::smRenderBudgetMs = 8;
  53. ShadowMapPass::ShadowMapPass(LightManager* lightManager, ShadowMapManager* shadowManager)
  54. {
  55. mLightManager = lightManager;
  56. mShadowManager = shadowManager;
  57. mShadowRPM = new ShadowRenderPassManager();
  58. mShadowRPM->assignName( "ShadowRenderPassManager" );
  59. mShadowRPM->registerObject();
  60. Sim::getRootGroup()->addObject( mShadowRPM );
  61. // Setup our render pass manager
  62. mShadowRPM->addManager( new RenderMeshMgr(RenderPassManager::RIT_Mesh, 0.3f, 0.3f) );
  63. //mShadowRPM->addManager( new RenderObjectMgr() );
  64. mShadowRPM->addManager( new RenderTerrainMgr( 0.5f, 0.5f ) );
  65. mShadowRPM->addManager( new RenderImposterMgr( 0.6f, 0.6f ) );
  66. mActiveLights = 0;
  67. mTimer = PlatformTimer::create();
  68. Con::addVariable( "$ShadowStats::activeMaps", TypeS32, &smActiveShadowMaps,
  69. "The shadow stats showing the active number of shadow maps.\n"
  70. "@ingroup AdvancedLighting\n" );
  71. Con::addVariable( "$ShadowStats::updatedMaps", TypeS32, &smUpdatedShadowMaps,
  72. "The shadow stats showing the number of shadow maps updated this frame.\n"
  73. "@ingroup AdvancedLighting\n" );
  74. Con::addVariable( "$ShadowStats::nearMaps", TypeS32, &smNearShadowMaps,
  75. "The shadow stats showing the number of shadow maps that are close enough to be updated very frame.\n"
  76. "@ingroup AdvancedLighting\n" );
  77. Con::addVariable( "$ShadowStats::drawCalls", TypeS32, &smShadowMapsDrawCalls,
  78. "The shadow stats showing the number of draw calls in shadow map renders for this frame.\n"
  79. "@ingroup AdvancedLighting\n" );
  80. Con::addVariable( "$ShadowStats::polyCount", TypeS32, &smShadowMapPolyCount,
  81. "The shadow stats showing the number of triangles in shadow map renders for this frame.\n"
  82. "@ingroup AdvancedLighting\n" );
  83. Con::addVariable( "$ShadowStats::rtChanges", TypeS32, &smRenderTargetChanges,
  84. "The shadow stats showing the number of render target changes for shadow maps in this frame.\n"
  85. "@ingroup AdvancedLighting\n" );
  86. Con::addVariable( "$ShadowStats::poolTexCount", TypeS32, &smShadowPoolTexturesCount,
  87. "The shadow stats showing the number of shadow textures in the shadow texture pool.\n"
  88. "@ingroup AdvancedLighting\n" );
  89. Con::addVariable( "$ShadowStats::poolTexMemory", TypeF32, &smShadowPoolMemory,
  90. "The shadow stats showing the approximate texture memory usage of the shadow map texture pool.\n"
  91. "@ingroup AdvancedLighting\n" );
  92. }
  93. ShadowMapPass::~ShadowMapPass()
  94. {
  95. SAFE_DELETE( mTimer );
  96. if ( mShadowRPM )
  97. mShadowRPM->deleteObject();
  98. }
  99. void ShadowMapPass::render( SceneManager *sceneManager,
  100. const SceneRenderState *diffuseState,
  101. U32 objectMask )
  102. {
  103. PROFILE_SCOPE( ShadowMapPass_Render );
  104. // Prep some shadow rendering stats.
  105. smActiveShadowMaps = 0;
  106. smUpdatedShadowMaps = 0;
  107. smNearShadowMaps = 0;
  108. GFXDeviceStatistics stats;
  109. stats.start( GFX->getDeviceStatistics() );
  110. // NOTE: The lights were already registered by SceneManager.
  111. // Update mLights
  112. mLights.clear();
  113. mLightManager->getAllUnsortedLights( &mLights );
  114. mActiveLights = mLights.size();
  115. // Use the per-frame incremented time for
  116. // priority updates and to track when the
  117. // shadow was last updated.
  118. const U32 currTime = Sim::getCurrentTime();
  119. // First do a loop thru the lights setting up the shadow
  120. // info array for this pass.
  121. Vector<LightShadowMap*> shadowMaps;
  122. shadowMaps.reserve( mActiveLights );
  123. for ( U32 i = 0; i < mActiveLights; i++ )
  124. {
  125. ShadowMapParams *params = mLights[i]->getExtended<ShadowMapParams>();
  126. // Before we do anything... skip lights without shadows.
  127. if ( !mLights[i]->getCastShadows() || smDisableShadows )
  128. continue;
  129. LightShadowMap *lsm = params->getOrCreateShadowMap();
  130. // First check the visiblity query... if it wasn't
  131. // visible skip it.
  132. if ( lsm->wasOccluded() )
  133. continue;
  134. // Any shadow that is visible is counted as being
  135. // active regardless if we update it or not.
  136. ++smActiveShadowMaps;
  137. // Do a priority update for this shadow.
  138. lsm->updatePriority( diffuseState, currTime );
  139. shadowMaps.push_back( lsm );
  140. }
  141. // Now sort the shadow info by priority.
  142. shadowMaps.sort( LightShadowMap::cmpPriority );
  143. GFXDEBUGEVENT_SCOPE( ShadowMapPass_Render, ColorI::RED );
  144. // Use a timer for tracking our shadow rendering
  145. // budget to ensure a high precision results.
  146. mTimer->getElapsedMs();
  147. mTimer->reset();
  148. for ( U32 i = 0; i < shadowMaps.size(); i++ )
  149. {
  150. LightShadowMap *lsm = shadowMaps[i];
  151. {
  152. GFXDEBUGEVENT_SCOPE( ShadowMapPass_Render_Shadow, ColorI::RED );
  153. mShadowManager->setLightShadowMap( lsm );
  154. lsm->render( mShadowRPM, diffuseState );
  155. ++smUpdatedShadowMaps;
  156. }
  157. // View dependent shadows or ones that are covering the entire
  158. // screen are updated every frame no matter the time left in
  159. // our shadow rendering budget.
  160. if ( lsm->isViewDependent() || lsm->getLastScreenSize() >= 1.0f )
  161. {
  162. ++smNearShadowMaps;
  163. continue;
  164. }
  165. // See if we're over our frame budget for shadow
  166. // updates... give up completely in that case.
  167. if ( mTimer->getElapsedMs() > smRenderBudgetMs )
  168. break;
  169. }
  170. // Cleanup old unused textures.
  171. LightShadowMap::releaseUnusedTextures();
  172. // Update the stats.
  173. stats.end( GFX->getDeviceStatistics() );
  174. smShadowMapsDrawCalls = stats.mDrawCalls;
  175. smShadowMapPolyCount = stats.mPolyCount;
  176. smRenderTargetChanges = stats.mRenderTargetChanges;
  177. smShadowPoolTexturesCount = ShadowMapProfile.getStats().activeCount;
  178. smShadowPoolMemory = ( ShadowMapProfile.getStats().activeBytes / 1024.0f ) / 1024.0f;
  179. // The NULL here is importaint as having it around
  180. // will cause extra work in AdvancedLightManager::setLightInfo().
  181. mShadowManager->setLightShadowMap( NULL );
  182. }
  183. void ShadowRenderPassManager::addInst( RenderInst *inst )
  184. {
  185. PROFILE_SCOPE(ShadowRenderPassManager_addInst);
  186. if ( inst->type == RIT_Mesh )
  187. {
  188. MeshRenderInst *meshRI = static_cast<MeshRenderInst*>( inst );
  189. if ( !meshRI->matInst )
  190. return;
  191. const BaseMaterialDefinition *mat = meshRI->matInst->getMaterial();
  192. if ( !mat->castsShadows() || mat->isTranslucent() )
  193. {
  194. // Do not add this instance, return here and avoid the default behavior
  195. // of calling up to Parent::addInst()
  196. return;
  197. }
  198. }
  199. Parent::addInst(inst);
  200. }