shadowMapPass.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. /// milliseconds before static redraw
  52. S32 ShadowMapPass::smStaticShadowUpdateFreq = 32;
  53. /// milliseconds before dynamic redraw
  54. S32 ShadowMapPass::smDynamicShadowUpdateFreq = 16;
  55. /// We have a default 8ms render budget for shadow rendering.
  56. U32 ShadowMapPass::smRenderBudgetMs = 8;
  57. ShadowMapPass::ShadowMapPass(LightManager* lightManager, ShadowMapManager* shadowManager)
  58. {
  59. mLightManager = lightManager;
  60. mShadowManager = shadowManager;
  61. // Setup our render pass managers
  62. // Static
  63. mShadowRPM = new ShadowRenderPassManager();
  64. mShadowRPM->assignName( "ShadowRenderPassManager" );
  65. mShadowRPM->registerObject();
  66. Sim::getRootGroup()->addObject( mShadowRPM );
  67. mShadowRPM->addManager( new RenderMeshMgr(RenderPassManager::RIT_Mesh, 0.3f, 0.3f) );
  68. mShadowRPM->addManager( new RenderTerrainMgr( 0.5f, 0.5f ) );
  69. mShadowRPM->addManager( new RenderImposterMgr( 0.6f, 0.6f ) );
  70. // Dynamic
  71. mDynamicShadowRPM = new DynamicShadowRenderPassManager();
  72. mDynamicShadowRPM->assignName( "DynamicShadowRenderPassManager" );
  73. mDynamicShadowRPM->registerObject();
  74. Sim::getRootGroup()->addObject( mDynamicShadowRPM );
  75. mDynamicShadowRPM->addManager( new RenderMeshMgr(RenderPassManager::RIT_Mesh, 0.3f, 0.3f) );
  76. mDynamicShadowRPM->addManager( new RenderTerrainMgr( 0.5f, 0.5f ) );
  77. mDynamicShadowRPM->addManager( new RenderImposterMgr( 0.6f, 0.6f ) );
  78. mActiveLights = 0;
  79. mTimer = PlatformTimer::create();
  80. Con::addVariable( "$ShadowStats::activeMaps", TypeS32, &smActiveShadowMaps,
  81. "The shadow stats showing the active number of shadow maps.\n"
  82. "@ingroup AdvancedLighting\n" );
  83. Con::addVariable( "$ShadowStats::updatedMaps", TypeS32, &smUpdatedShadowMaps,
  84. "The shadow stats showing the number of shadow maps updated this frame.\n"
  85. "@ingroup AdvancedLighting\n" );
  86. Con::addVariable( "$ShadowStats::nearMaps", TypeS32, &smNearShadowMaps,
  87. "The shadow stats showing the number of shadow maps that are close enough to be updated very frame.\n"
  88. "@ingroup AdvancedLighting\n" );
  89. Con::addVariable( "$ShadowStats::drawCalls", TypeS32, &smShadowMapsDrawCalls,
  90. "The shadow stats showing the number of draw calls in shadow map renders for this frame.\n"
  91. "@ingroup AdvancedLighting\n" );
  92. Con::addVariable( "$ShadowStats::polyCount", TypeS32, &smShadowMapPolyCount,
  93. "The shadow stats showing the number of triangles in shadow map renders for this frame.\n"
  94. "@ingroup AdvancedLighting\n" );
  95. Con::addVariable( "$ShadowStats::rtChanges", TypeS32, &smRenderTargetChanges,
  96. "The shadow stats showing the number of render target changes for shadow maps in this frame.\n"
  97. "@ingroup AdvancedLighting\n" );
  98. Con::addVariable( "$ShadowStats::poolTexCount", TypeS32, &smShadowPoolTexturesCount,
  99. "The shadow stats showing the number of shadow textures in the shadow texture pool.\n"
  100. "@ingroup AdvancedLighting\n" );
  101. Con::addVariable( "$ShadowStats::poolTexMemory", TypeF32, &smShadowPoolMemory,
  102. "The shadow stats showing the approximate texture memory usage of the shadow map texture pool.\n"
  103. "@ingroup AdvancedLighting\n" );
  104. }
  105. ShadowMapPass::~ShadowMapPass()
  106. {
  107. SAFE_DELETE( mTimer );
  108. if ( mShadowRPM )
  109. mShadowRPM->deleteObject();
  110. if ( mDynamicShadowRPM )
  111. mDynamicShadowRPM->deleteObject();
  112. }
  113. void ShadowMapPass::render( SceneManager *sceneManager,
  114. const SceneRenderState *diffuseState,
  115. U32 objectMask )
  116. {
  117. PROFILE_SCOPE( ShadowMapPass_Render );
  118. // Prep some shadow rendering stats.
  119. smActiveShadowMaps = 0;
  120. smUpdatedShadowMaps = 0;
  121. smNearShadowMaps = 0;
  122. GFXDeviceStatistics stats;
  123. stats.start( GFX->getDeviceStatistics() );
  124. // NOTE: The lights were already registered by SceneManager.
  125. // Update mLights
  126. mLights.clear();
  127. mLightManager->getAllUnsortedLights( &mLights );
  128. mActiveLights = mLights.size();
  129. // Use the per-frame incremented time for
  130. // priority updates and to track when the
  131. // shadow was last updated.
  132. const U32 currTime = Sim::getCurrentTime();
  133. // First do a loop thru the lights setting up the shadow
  134. // info array for this pass.
  135. Vector<LightShadowMap*> shadowMaps;
  136. shadowMaps.reserve( mActiveLights * 2 );
  137. for ( U32 i = 0; i < mActiveLights; i++ )
  138. {
  139. ShadowMapParams *params = mLights[i]->getExtended<ShadowMapParams>();
  140. // Before we do anything... skip lights without shadows.
  141. if ( !mLights[i]->getCastShadows() || smDisableShadows )
  142. continue;
  143. // --- Static Shadow Map ---
  144. LightShadowMap *lsm = params->getOrCreateShadowMap();
  145. LightShadowMap *dlsm = params->getOrCreateShadowMap(true);
  146. // First check the visiblity query... if it wasn't
  147. // visible skip it.
  148. if(params->getOcclusionQuery()->getStatus(true) == GFXOcclusionQuery::Occluded)
  149. continue;
  150. // Any shadow that is visible is counted as being
  151. // active regardless if we update it or not.
  152. ++smActiveShadowMaps;
  153. // Do a priority update for this shadow.
  154. lsm->updatePriority(diffuseState, currTime);
  155. shadowMaps.push_back(lsm);
  156. // --- Dynamic Shadow Map ---
  157. // Any shadow that is visible is counted as being
  158. // active regardless if we update it or not.
  159. ++smActiveShadowMaps;
  160. // Do a priority update for this shadow.
  161. dlsm->updatePriority(diffuseState, currTime);
  162. shadowMaps.push_back( dlsm );
  163. }
  164. // Now sort the shadow info by priority.
  165. // andrewmac: tempoarily disabled until I find a better solution.
  166. //shadowMaps.sort( LightShadowMap::cmpPriority );
  167. GFXDEBUGEVENT_SCOPE( ShadowMapPass_Render, ColorI::RED );
  168. // Use a timer for tracking our shadow rendering
  169. // budget to ensure a high precision results.
  170. mTimer->getElapsedMs();
  171. mTimer->reset();
  172. // 2 Shadow Maps per Light. This may fail.
  173. for ( U32 i = 0; i < shadowMaps.size(); i += 2 )
  174. {
  175. LightShadowMap *lsm = shadowMaps[i];
  176. LightShadowMap *dlsm = shadowMaps[i + 1];
  177. {
  178. GFXDEBUGEVENT_SCOPE( ShadowMapPass_Render_Shadow, ColorI::RED );
  179. mShadowManager->setLightShadowMap(lsm);
  180. mShadowManager->setLightDynamicShadowMap( dlsm );
  181. lsm->render(mShadowRPM, diffuseState, false);
  182. dlsm->render(mDynamicShadowRPM, diffuseState, true);
  183. ++smUpdatedShadowMaps;
  184. }
  185. // View dependent shadows or ones that are covering the entire
  186. // screen are updated every frame no matter the time left in
  187. // our shadow rendering budget.
  188. if ( dlsm->isViewDependent() || dlsm->getLastScreenSize() >= 1.0f )
  189. {
  190. ++smNearShadowMaps;
  191. continue;
  192. }
  193. // See if we're over our frame budget for shadow
  194. // updates... give up completely in that case.
  195. if ( mTimer->getElapsedMs() > smRenderBudgetMs )
  196. break;
  197. }
  198. // Cleanup old unused textures.
  199. LightShadowMap::releaseUnusedTextures();
  200. // Update the stats.
  201. stats.end( GFX->getDeviceStatistics() );
  202. smShadowMapsDrawCalls = stats.mDrawCalls;
  203. smShadowMapPolyCount = stats.mPolyCount;
  204. smRenderTargetChanges = stats.mRenderTargetChanges;
  205. smShadowPoolTexturesCount = ShadowMapProfile.getStats().activeCount;
  206. smShadowPoolMemory = ( ShadowMapProfile.getStats().activeBytes / 1024.0f ) / 1024.0f;
  207. // The NULL here is importaint as having it around
  208. // will cause extra work in AdvancedLightManager::setLightInfo().
  209. mShadowManager->setLightShadowMap( NULL );
  210. mShadowManager->setLightDynamicShadowMap( NULL );
  211. }
  212. void ShadowRenderPassManager::addInst( RenderInst *inst )
  213. {
  214. PROFILE_SCOPE(ShadowRenderPassManager_addInst);
  215. if ( inst->type == RIT_Mesh )
  216. {
  217. MeshRenderInst *meshRI = static_cast<MeshRenderInst*>( inst );
  218. if ( !meshRI->matInst )
  219. return;
  220. const BaseMaterialDefinition *mat = meshRI->matInst->getMaterial();
  221. if ( !mat->castsShadows() || mat->castsDynamicShadows() || mat->isTranslucent() )
  222. {
  223. // Do not add this instance, return here and avoid the default behavior
  224. // of calling up to Parent::addInst()
  225. return;
  226. }
  227. }
  228. Parent::addInst(inst);
  229. }
  230. void DynamicShadowRenderPassManager::addInst( RenderInst *inst )
  231. {
  232. PROFILE_SCOPE(DynamicShadowRenderPassManager_addInst);
  233. if ( inst->type == RIT_Mesh )
  234. {
  235. MeshRenderInst *meshRI = static_cast<MeshRenderInst*>( inst );
  236. if ( !meshRI->matInst )
  237. return;
  238. const BaseMaterialDefinition *mat = meshRI->matInst->getMaterial();
  239. if ( !mat->castsShadows() || !mat->castsDynamicShadows() || mat->isTranslucent() )
  240. {
  241. // Do not add this instance, return here and avoid the default behavior
  242. // of calling up to Parent::addInst()
  243. return;
  244. }
  245. }
  246. Parent::addInst(inst);
  247. }