lightShadowMap.h 11 KB

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