shadowMapPass.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include "T3D/gameBase/gameConnection.h"
  40. const String ShadowMapPass::PassTypeName("ShadowMap");
  41. U32 ShadowMapPass::smActiveShadowMaps = 0;
  42. U32 ShadowMapPass::smUpdatedShadowMaps = 0;
  43. U32 ShadowMapPass::smNearShadowMaps = 0;
  44. U32 ShadowMapPass::smShadowMapsDrawCalls = 0;
  45. U32 ShadowMapPass::smShadowMapPolyCount = 0;
  46. U32 ShadowMapPass::smRenderTargetChanges = 0;
  47. U32 ShadowMapPass::smShadowPoolTexturesCount = 0.;
  48. F32 ShadowMapPass::smShadowPoolMemory = 0.0f;
  49. bool ShadowMapPass::smDisableShadows = false;
  50. bool ShadowMapPass::smDisableShadowsEditor = false;
  51. bool ShadowMapPass::smDisableShadowsPref = false;
  52. /// distance moved per frame before forcing a shadow update
  53. F32 ShadowMapPass::smShadowsTeleportDist = 4;
  54. /// angle turned per frame before forcing a shadow update
  55. F32 ShadowMapPass::smShadowsTurnRate = 1;
  56. /// We have a default 8ms render budget for shadow rendering.
  57. U32 ShadowMapPass::smRenderBudgetMs = 8;
  58. ShadowMapPass::ShadowMapPass(LightManager* lightManager, ShadowMapManager* shadowManager)
  59. {
  60. mLightManager = lightManager;
  61. mShadowManager = shadowManager;
  62. // Setup our render pass managers
  63. // Static
  64. mShadowRPM = new ShadowRenderPassManager();
  65. mShadowRPM->assignName( "ShadowRenderPassManager" );
  66. mShadowRPM->registerObject();
  67. Sim::getRootGroup()->addObject( mShadowRPM );
  68. mShadowRPM->addManager( new RenderMeshMgr(RenderPassManager::RIT_Mesh, 0.3f, 0.3f) );
  69. mShadowRPM->addManager( new RenderTerrainMgr( 0.5f, 0.5f ) );
  70. mShadowRPM->addManager( new RenderImposterMgr( 0.6f, 0.6f ) );
  71. mActiveLights = 0;
  72. mPrevCamPos = Point3F::Zero;
  73. mPrevCamRot = Point3F::Zero;
  74. mTimer = PlatformTimer::create();
  75. Con::addVariable( "$ShadowStats::activeMaps", TypeS32, &smActiveShadowMaps,
  76. "The shadow stats showing the active number of shadow maps.\n"
  77. "@ingroup AdvancedLighting\n" );
  78. Con::addVariable( "$ShadowStats::updatedMaps", TypeS32, &smUpdatedShadowMaps,
  79. "The shadow stats showing the number of shadow maps updated this frame.\n"
  80. "@ingroup AdvancedLighting\n" );
  81. Con::addVariable( "$ShadowStats::nearMaps", TypeS32, &smNearShadowMaps,
  82. "The shadow stats showing the number of shadow maps that are close enough to be updated very frame.\n"
  83. "@ingroup AdvancedLighting\n" );
  84. Con::addVariable( "$ShadowStats::drawCalls", TypeS32, &smShadowMapsDrawCalls,
  85. "The shadow stats showing the number of draw calls in shadow map renders for this frame.\n"
  86. "@ingroup AdvancedLighting\n" );
  87. Con::addVariable( "$ShadowStats::polyCount", TypeS32, &smShadowMapPolyCount,
  88. "The shadow stats showing the number of triangles in shadow map renders for this frame.\n"
  89. "@ingroup AdvancedLighting\n" );
  90. Con::addVariable( "$ShadowStats::rtChanges", TypeS32, &smRenderTargetChanges,
  91. "The shadow stats showing the number of render target changes for shadow maps in this frame.\n"
  92. "@ingroup AdvancedLighting\n" );
  93. Con::addVariable( "$ShadowStats::poolTexCount", TypeS32, &smShadowPoolTexturesCount,
  94. "The shadow stats showing the number of shadow textures in the shadow texture pool.\n"
  95. "@ingroup AdvancedLighting\n" );
  96. Con::addVariable( "$ShadowStats::poolTexMemory", TypeF32, &smShadowPoolMemory,
  97. "The shadow stats showing the approximate texture memory usage of the shadow map texture pool.\n"
  98. "@ingroup AdvancedLighting\n" );
  99. }
  100. ShadowMapPass::~ShadowMapPass()
  101. {
  102. SAFE_DELETE( mTimer );
  103. if ( mShadowRPM )
  104. mShadowRPM->deleteObject();
  105. }
  106. void ShadowMapPass::render( SceneManager *sceneManager,
  107. const SceneRenderState *diffuseState,
  108. U32 objectMask )
  109. {
  110. PROFILE_SCOPE( ShadowMapPass_Render );
  111. // Prep some shadow rendering stats.
  112. smActiveShadowMaps = 0;
  113. smUpdatedShadowMaps = 0;
  114. smNearShadowMaps = 0;
  115. GFXDeviceStatistics stats;
  116. stats.start( GFX->getDeviceStatistics() );
  117. // NOTE: The lights were already registered by SceneManager.
  118. // Update mLights
  119. mLights.clear();
  120. mLightManager->getAllUnsortedLights( &mLights );
  121. mActiveLights = mLights.size();
  122. // Use the per-frame incremented time for
  123. // priority updates and to track when the
  124. // shadow was last updated.
  125. const U32 currTime = Sim::getCurrentTime();
  126. // First do a loop thru the lights setting up the shadow
  127. // info array for this pass.
  128. Vector<LightShadowMap*> shadowMaps;
  129. shadowMaps.reserve( mActiveLights );
  130. for ( U32 i = 0; i < mActiveLights; i++ )
  131. {
  132. ShadowMapParams *params = mLights[i]->getExtended<ShadowMapParams>();
  133. // Before we do anything... skip lights without shadows.
  134. if ( !mLights[i]->getCastShadows() || smDisableShadows )
  135. continue;
  136. // --- Static Shadow Map ---
  137. LightShadowMap *lsm = params->getOrCreateShadowMap();
  138. // First check the visiblity query... if it wasn't
  139. // visible skip it.
  140. if(params->getOcclusionQuery()->getStatus(true) == GFXOcclusionQuery::Occluded)
  141. continue;
  142. // Any shadow that is visible is counted as being
  143. // active regardless if we update it or not.
  144. ++smActiveShadowMaps;
  145. // Do a priority update for this shadow.
  146. lsm->updatePriority(diffuseState, currTime);
  147. shadowMaps.push_back(lsm);
  148. }
  149. // Now sort the shadow info by priority.
  150. // andrewmac: tempoarily disabled until I find a better solution.
  151. //shadowMaps.sort( LightShadowMap::cmpPriority );
  152. GFXDEBUGEVENT_SCOPE( ShadowMapPass_Render, ColorI::RED );
  153. // Use a timer for tracking our shadow rendering
  154. // budget to ensure a high precision results.
  155. mTimer->getElapsedMs();
  156. mTimer->reset();
  157. // Must have a connection and control object
  158. GameConnection* conn = GameConnection::getConnectionToServer();
  159. if (!conn)
  160. return;
  161. GameBase * control = dynamic_cast<GameBase*>(conn->getControlObject());
  162. if (!control)
  163. return;
  164. bool forceUpdate = false;
  165. //force an update if we're jumping around (respawning, ect)
  166. MatrixF curCamMatrix = control->getTransform();
  167. if (((curCamMatrix.getPosition() - mPrevCamPos).lenSquared() > mPow(smShadowsTeleportDist, 2)) || //update if we're teleporting
  168. ((curCamMatrix.getForwardVector() - mPrevCamRot).lenSquared() > mPow(smShadowsTurnRate*M_PI_F / 180, 2)) || //update if we're turning too fast
  169. (control->getCameraFov()) != mPrevCamFov) //update if we're zooming or unzooming
  170. forceUpdate = true;
  171. mPrevCamRot = curCamMatrix.getForwardVector();
  172. mPrevCamPos = curCamMatrix.getPosition();
  173. mPrevCamFov = control->getCameraFov();
  174. // 2 Shadow Maps per Light. This may fail.
  175. for ( U32 i = 0; i < shadowMaps.size(); i++ )
  176. {
  177. LightShadowMap *lsm = shadowMaps[i];
  178. {
  179. GFXDEBUGEVENT_SCOPE( ShadowMapPass_Render_Shadow, ColorI::RED );
  180. mShadowManager->setLightShadowMap(lsm);
  181. lsm->render(mShadowRPM, diffuseState);
  182. ++smUpdatedShadowMaps;
  183. }
  184. // See if we're over our frame budget for shadow
  185. // updates... give up completely in that case.
  186. if ( mTimer->getElapsedMs() > smRenderBudgetMs )
  187. break;
  188. }
  189. // Cleanup old unused textures.
  190. LightShadowMap::releaseUnusedTextures();
  191. // Update the stats.
  192. stats.end( GFX->getDeviceStatistics() );
  193. smShadowMapsDrawCalls = stats.mDrawCalls;
  194. smShadowMapPolyCount = stats.mPolyCount;
  195. smRenderTargetChanges = stats.mRenderTargetChanges;
  196. smShadowPoolTexturesCount = ShadowMapProfile.getStats().activeCount;
  197. smShadowPoolMemory = ( ShadowMapProfile.getStats().activeBytes / 1024.0f ) / 1024.0f;
  198. // The NULL here is importaint as having it around
  199. // will cause extra work in AdvancedLightManager::setLightInfo().
  200. mShadowManager->setLightShadowMap( NULL );
  201. }
  202. void ShadowRenderPassManager::addInst( RenderInst *inst )
  203. {
  204. PROFILE_SCOPE(ShadowRenderPassManager_addInst);
  205. if ( inst->type == RIT_Mesh )
  206. {
  207. MeshRenderInst *meshRI = static_cast<MeshRenderInst*>( inst );
  208. if ( !meshRI->matInst )
  209. return;
  210. const BaseMaterialDefinition *mat = meshRI->matInst->getMaterial();
  211. if ( !mat->castsShadows() || (mat->isTranslucent() && !mat->isAlphatest()))
  212. {
  213. // Do not add this instance, return here and avoid the default behavior
  214. // of calling up to Parent::addInst()
  215. return;
  216. }
  217. }
  218. Parent::addInst(inst);
  219. }