shadowMapPass.cpp 9.1 KB

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