lightShadowMap.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. #ifndef _LIGHTSHADOWMAP_H_
  23. #define _LIGHTSHADOWMAP_H_
  24. #ifndef _GFXTEXTUREHANDLE_H_
  25. #include "gfx/gfxTextureHandle.h"
  26. #endif
  27. #ifndef _GFXCUBEMAP_H_
  28. #include "gfx/gfxCubemap.h"
  29. #endif
  30. #ifndef _GFXTARGET_H_
  31. #include "gfx/gfxTarget.h"
  32. #endif
  33. #ifndef _LIGHTINFO_H_
  34. #include "lighting/lightInfo.h"
  35. #endif
  36. #ifndef _MATHUTIL_FRUSTUM_H_
  37. #include "math/util/frustum.h"
  38. #endif
  39. #ifndef _MATTEXTURETARGET_H_
  40. #include "materials/matTextureTarget.h"
  41. #endif
  42. #ifndef _SHADOW_COMMON_H_
  43. #include "lighting/shadowMap/shadowCommon.h"
  44. #endif
  45. #ifndef _GFXSHADER_H_
  46. #include "gfx/gfxShader.h"
  47. #endif
  48. #ifndef _PLATFORM_PLATFORMTIMER_H_
  49. #include "platform/platformTimer.h"
  50. #endif
  51. class ShadowMapManager;
  52. class SceneManager;
  53. class SceneRenderState;
  54. class BaseMatInstance;
  55. class MaterialParameters;
  56. class SharedShadowMapObjects;
  57. struct SceneData;
  58. class GFXShaderConstBuffer;
  59. class GFXShaderConstHandle;
  60. class GFXShader;
  61. class GFXOcclusionQuery;
  62. class LightManager;
  63. class RenderPassManager;
  64. // Shader constant handle lookup
  65. // This isn't broken up as much as it could be, we're mixing single light constants
  66. // and pssm constants.
  67. struct LightingShaderConstants
  68. {
  69. bool mInit;
  70. GFXShaderRef mShader;
  71. GFXShaderConstHandle* mLightParamsSC;
  72. GFXShaderConstHandle* mLightSpotParamsSC;
  73. // NOTE: These are the shader constants used for doing
  74. // lighting during the forward pass. Do not confuse
  75. // these for the prepass lighting constants which are
  76. // used from AdvancedLightBinManager.
  77. GFXShaderConstHandle *mLightPositionSC;
  78. GFXShaderConstHandle *mLightDiffuseSC;
  79. GFXShaderConstHandle *mLightAmbientSC;
  80. GFXShaderConstHandle *mLightInvRadiusSqSC;
  81. GFXShaderConstHandle *mLightSpotDirSC;
  82. GFXShaderConstHandle *mLightSpotAngleSC;
  83. GFXShaderConstHandle *mLightSpotFalloffSC;
  84. GFXShaderConstHandle* mShadowMapSC;
  85. GFXShaderConstHandle* mDynamicShadowMapSC;
  86. GFXShaderConstHandle* mShadowMapSizeSC;
  87. GFXShaderConstHandle* mCookieMapSC;
  88. GFXShaderConstHandle* mRandomDirsConst;
  89. GFXShaderConstHandle* mShadowSoftnessConst;
  90. GFXShaderConstHandle* mAtlasXOffsetSC;
  91. GFXShaderConstHandle* mAtlasYOffsetSC;
  92. GFXShaderConstHandle* mAtlasScaleSC;
  93. // fadeStartLength.x = Distance in eye space to start fading shadows
  94. // fadeStartLength.y = 1 / Length of fade
  95. GFXShaderConstHandle* mFadeStartLength;
  96. GFXShaderConstHandle* mOverDarkFactorPSSM;
  97. GFXShaderConstHandle* mTapRotationTexSC;
  98. // Static Specific:
  99. GFXShaderConstHandle* mWorldToLightProjSC;
  100. GFXShaderConstHandle* mViewToLightProjSC;
  101. GFXShaderConstHandle* mScaleXSC;
  102. GFXShaderConstHandle* mScaleYSC;
  103. GFXShaderConstHandle* mOffsetXSC;
  104. GFXShaderConstHandle* mOffsetYSC;
  105. GFXShaderConstHandle* mFarPlaneScalePSSM;
  106. // Dynamic Specific:
  107. GFXShaderConstHandle* mDynamicWorldToLightProjSC;
  108. GFXShaderConstHandle* mDynamicViewToLightProjSC;
  109. GFXShaderConstHandle* mDynamicScaleXSC;
  110. GFXShaderConstHandle* mDynamicScaleYSC;
  111. GFXShaderConstHandle* mDynamicOffsetXSC;
  112. GFXShaderConstHandle* mDynamicOffsetYSC;
  113. GFXShaderConstHandle* mDynamicFarPlaneScalePSSM;
  114. LightingShaderConstants();
  115. ~LightingShaderConstants();
  116. void init(GFXShader* buffer);
  117. void _onShaderReload();
  118. };
  119. typedef Map<GFXShader*, LightingShaderConstants*> LightConstantMap;
  120. /// This represents everything we need to render
  121. /// the shadowmap for one light.
  122. class LightShadowMap
  123. {
  124. public:
  125. const static GFXFormat ShadowMapFormat;
  126. /// Used to scale the shadow texture size for performance tweaking.
  127. static F32 smShadowTexScalar;
  128. /// Whether to render shadow frustums for lights that have debug
  129. /// rendering enabled.
  130. static bool smDebugRenderFrustums;
  131. public:
  132. LightShadowMap( LightInfo *light );
  133. virtual ~LightShadowMap();
  134. void render( RenderPassManager* renderPass,
  135. const SceneRenderState *diffuseState,
  136. bool _dynamic);
  137. U32 getLastUpdate() const { return mLastUpdate; }
  138. //U32 getLastVisible() const { return mLastVisible; }
  139. bool isViewDependent() const { return mIsViewDependent; }
  140. bool wasOccluded() const { return mWasOccluded; }
  141. void preLightRender();
  142. void postLightRender();
  143. void updatePriority( const SceneRenderState *state, U32 currTimeMs );
  144. F32 getLastScreenSize() const { return mLastScreenSize; }
  145. F32 getLastPriority() const { return mLastPriority; }
  146. virtual bool hasShadowTex() const { return mShadowMapTex.isValid(); }
  147. virtual bool setTextureStage( U32 currTexFlag, LightingShaderConstants* lsc );
  148. LightInfo* getLightInfo() { return mLight; }
  149. virtual void setShaderParameters(GFXShaderConstBuffer* params, LightingShaderConstants* lsc) = 0;
  150. U32 getTexSize() const { return mTexSize; }
  151. /// Returns the best texture size based on the user
  152. /// texture size, the last light screen size, and
  153. /// global shadow tweak parameters.
  154. U32 getBestTexSize( U32 scale = 1 ) const;
  155. const MatrixF& getWorldToLightProj() const { return mWorldToLightProj; }
  156. static GFXTextureObject* _getDepthTarget( U32 width, U32 height );
  157. virtual ShadowType getShadowType() const = 0;
  158. // Cleanup texture resources
  159. virtual void releaseTextures();
  160. ///
  161. GFXTextureObject* getTexture() const { return mShadowMapTex; }
  162. ///
  163. void setDebugTarget( const String &name );
  164. static void releaseAllTextures();
  165. /// Releases any shadow maps that have not been culled
  166. /// in a while and returns the count of the remaing
  167. /// shadow maps in use.
  168. static U32 releaseUnusedTextures();
  169. ///
  170. static S32 QSORT_CALLBACK cmpPriority( LightShadowMap *const *lsm1, LightShadowMap *const *lsm2 );
  171. /// Returns the correct shadow material this type of light
  172. /// or NULL if no shadow material is possible.
  173. BaseMatInstance* getShadowMaterial( BaseMatInstance *inMat ) const;
  174. protected:
  175. /// All the shadow maps in the system.
  176. static Vector<LightShadowMap*> smShadowMaps;
  177. /// All the shadow maps that have been recently rendered to.
  178. static Vector<LightShadowMap*> smUsedShadowMaps;
  179. virtual void _render( RenderPassManager* renderPass,
  180. const SceneRenderState *diffuseState ) = 0;
  181. /// If there is a LightDebugInfo attached to the light that owns this map,
  182. /// then update its information from the given render state.
  183. ///
  184. /// @note This method only does something in debug builds.
  185. void _debugRender( SceneRenderState* shadowRenderState );
  186. /// Helper for rendering shadow map for debugging.
  187. NamedTexTarget mDebugTarget;
  188. /// If true the shadow is view dependent and cannot
  189. /// be skipped if visible and within active range.
  190. bool mIsViewDependent;
  191. /// The time this shadow was last updated.
  192. U32 mLastUpdate;
  193. PlatformTimer *mStaticRefreshTimer;
  194. PlatformTimer *mDynamicRefreshTimer;
  195. /// The time this shadow was last culled and prioritized.
  196. U32 mLastCull;
  197. /// The shadow occlusion query used when the light is
  198. /// rendered to determine if any pixel of it is visible.
  199. GFXOcclusionQuery *mVizQuery;
  200. /// If true the light was occluded by geometry the
  201. /// last frame it was updated.
  202. //the last frame.
  203. bool mWasOccluded;
  204. F32 mLastScreenSize;
  205. F32 mLastPriority;
  206. MatrixF mWorldToLightProj;
  207. GFXTextureTargetRef mTarget;
  208. U32 mTexSize;
  209. GFXTexHandle mShadowMapTex;
  210. GFXTexHandle mShadowMapDepth;
  211. // The light we are rendering.
  212. LightInfo *mLight;
  213. // Used for blur
  214. GFXShader* mLastShader;
  215. GFXShaderConstHandle* mBlurBoundaries;
  216. // Calculate view matrices and set proper projection with GFX
  217. void calcLightMatrices( MatrixF& outLightMatrix, const Frustum &viewFrustum );
  218. /// The callback used to get texture events.
  219. /// @see GFXTextureManager::addEventDelegate
  220. void _onTextureEvent( GFXTexCallbackCode code );
  221. bool mIsDynamic;
  222. public:
  223. bool isDynamic() { return mIsDynamic; }
  224. void setDynamic(bool value) { mIsDynamic = value; }
  225. };
  226. GFX_DeclareTextureProfile( ShadowMapProfile );
  227. GFX_DeclareTextureProfile( ShadowMapZProfile );
  228. class ShadowMapParams : public LightInfoEx
  229. {
  230. public:
  231. ShadowMapParams( LightInfo *light );
  232. virtual ~ShadowMapParams();
  233. /// The LightInfoEx hook type.
  234. static LightInfoExType Type;
  235. // LightInfoEx
  236. virtual void set( const LightInfoEx *ex );
  237. virtual const LightInfoExType& getType() const { return Type; }
  238. virtual void packUpdate( BitStream *stream ) const;
  239. virtual void unpackUpdate( BitStream *stream );
  240. LightShadowMap* getShadowMap(bool _isDynamic = false) const { return _isDynamic ? mDynamicShadowMap : mShadowMap; }
  241. LightShadowMap* getOrCreateShadowMap(bool _isDynamic = false);
  242. bool hasCookieTex() const { return cookie.isNotEmpty(); }
  243. GFXTextureObject* getCookieTex();
  244. GFXCubemap* getCookieCubeTex();
  245. // Validates the parameters after a field is changed.
  246. void _validate();
  247. protected:
  248. void _initShadowMap();
  249. ///
  250. LightShadowMap *mShadowMap;
  251. LightShadowMap *mDynamicShadowMap;
  252. LightInfo *mLight;
  253. GFXTexHandle mCookieTex;
  254. GFXCubemapHandle mCookieCubeTex;
  255. public:
  256. // We're leaving these public for easy access
  257. // for console protected fields.
  258. /// @name Shadow Map
  259. /// @{
  260. ///
  261. U32 texSize;
  262. ///
  263. FileName cookie;
  264. /// @}
  265. Point3F attenuationRatio;
  266. /// @name Point Lights
  267. /// @{
  268. ///
  269. ShadowType shadowType;
  270. /// @}
  271. /// @name Exponential Shadow Map Parameters
  272. /// @{
  273. Point4F overDarkFactor;
  274. /// @}
  275. /// @name Parallel Split Shadow Map
  276. /// @{
  277. ///
  278. F32 shadowDistance;
  279. ///
  280. F32 shadowSoftness;
  281. /// The number of splits in the shadow map.
  282. U32 numSplits;
  283. ///
  284. F32 logWeight;
  285. /// At what distance do we start fading the shadows out completely.
  286. F32 fadeStartDist;
  287. /// This toggles only terrain being visible in the last
  288. /// split of a PSSM shadow map.
  289. bool lastSplitTerrainOnly;
  290. /// @}
  291. bool isDynamic;
  292. };
  293. #endif // _LIGHTSHADOWMAP_H_