lightShadowMap.h 11 KB

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