lightShadowMap.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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* mShadowMapSizeSC;
  90. GFXShaderConstHandle* mCookieMapSC;
  91. GFXShaderConstHandle* mIesProfileSC;
  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. LightingShaderConstants();
  111. ~LightingShaderConstants();
  112. void init(GFXShader* buffer);
  113. void _onShaderReload();
  114. };
  115. typedef Map<GFXShader*, LightingShaderConstants*> LightConstantMap;
  116. /// This represents everything we need to render
  117. /// the shadowmap for one light.
  118. class LightShadowMap
  119. {
  120. public:
  121. const static GFXFormat ShadowMapFormat;
  122. /// Used to scale the shadow texture size for performance tweaking.
  123. static F32 smShadowTexScalar;
  124. /// Whether to render shadow frustums for lights that have debug
  125. /// rendering enabled.
  126. static bool smDebugRenderFrustums;
  127. public:
  128. LightShadowMap( LightInfo *light );
  129. virtual ~LightShadowMap();
  130. void render( RenderPassManager* renderPass,
  131. const SceneRenderState *diffuseState);
  132. //U32 getLastVisible() const { return mLastVisible; }
  133. bool isViewDependent() const { return mIsViewDependent; }
  134. void updatePriority( const SceneRenderState *state, U32 currTimeMs );
  135. F32 getLastScreenSize() const { return mLastScreenSize; }
  136. F32 getLastPriority() const { return mLastPriority; }
  137. virtual bool hasShadowTex() const { return mShadowMapTex.isValid(); }
  138. virtual bool setTextureStage( U32 currTexFlag, LightingShaderConstants* lsc );
  139. LightInfo* getLightInfo() { return mLight; }
  140. virtual void setShaderParameters(GFXShaderConstBuffer* params, LightingShaderConstants* lsc) = 0;
  141. U32 getTexSize() const { return mTexSize; }
  142. /// Returns the best texture size based on the user
  143. /// texture size, the last light screen size, and
  144. /// global shadow tweak parameters.
  145. U32 getBestTexSize( U32 scale = 1 ) const;
  146. const MatrixF& getWorldToLightProj() const { return mWorldToLightProj; }
  147. static GFXTextureObject* _getDepthTarget( U32 width, U32 height );
  148. virtual ShadowType getShadowType() const = 0;
  149. // Cleanup texture resources
  150. virtual void releaseTextures();
  151. ///
  152. GFXTextureObject* getTexture() const { return mShadowMapTex; }
  153. ///
  154. void setDebugTarget( const String &name );
  155. static void releaseAllTextures();
  156. /// Releases any shadow maps that have not been culled
  157. /// in a while and returns the count of the remaing
  158. /// shadow maps in use.
  159. static U32 releaseUnusedTextures();
  160. ///
  161. static S32 QSORT_CALLBACK cmpPriority( LightShadowMap *const *lsm1, LightShadowMap *const *lsm2 );
  162. /// Returns the correct shadow material this type of light
  163. /// or NULL if no shadow material is possible.
  164. BaseMatInstance* getShadowMaterial( BaseMatInstance *inMat ) const;
  165. protected:
  166. /// All the shadow maps in the system.
  167. static Vector<LightShadowMap*> smShadowMaps;
  168. /// All the shadow maps that have been recently rendered to.
  169. static Vector<LightShadowMap*> smUsedShadowMaps;
  170. virtual void _render( RenderPassManager* renderPass,
  171. const SceneRenderState *diffuseState ) = 0;
  172. /// If there is a LightDebugInfo attached to the light that owns this map,
  173. /// then update its information from the given render state.
  174. ///
  175. /// @note This method only does something in debug builds.
  176. void _debugRender( SceneRenderState* shadowRenderState );
  177. /// Helper for rendering shadow map for debugging.
  178. NamedTexTarget mDebugTarget;
  179. /// If true the shadow is view dependent and cannot
  180. /// be skipped if visible and within active range.
  181. bool mIsViewDependent;
  182. /// The time this shadow was last culled and prioritized.
  183. U32 mLastCull;
  184. F32 mLastScreenSize;
  185. F32 mLastPriority;
  186. MatrixF mWorldToLightProj;
  187. GFXTextureTargetRef mTarget;
  188. U32 mTexSize;
  189. GFXTexHandle mShadowMapTex;
  190. GFXTexHandle mShadowMapDepth;
  191. // The light we are rendering.
  192. LightInfo *mLight;
  193. // Used for blur
  194. GFXShader* mLastShader;
  195. GFXShaderConstHandle* mBlurBoundaries;
  196. // Calculate view matrices and set proper projection with GFX
  197. void calcLightMatrices( MatrixF& outLightMatrix, const Frustum &viewFrustum );
  198. /// The callback used to get texture events.
  199. /// @see GFXTextureManager::addEventDelegate
  200. void _onTextureEvent( GFXTexCallbackCode code );
  201. };
  202. GFX_DeclareTextureProfile( ShadowMapProfile );
  203. GFX_DeclareTextureProfile( ShadowMapZProfile );
  204. class ShadowMapParams : public LightInfoEx
  205. {
  206. public:
  207. ShadowMapParams( LightInfo *light );
  208. virtual ~ShadowMapParams();
  209. /// The LightInfoEx hook type.
  210. static LightInfoExType Type;
  211. // LightInfoEx
  212. void set( const LightInfoEx *ex ) override;
  213. const LightInfoExType& getType() const override { return Type; }
  214. void packUpdate( BitStream *stream ) const override;
  215. void unpackUpdate( BitStream *stream ) override;
  216. LightShadowMap* getShadowMap() const { return mShadowMap; }
  217. LightShadowMap* getOrCreateShadowMap();
  218. bool hasCookieTex() const { return cookie != StringTable->EmptyString(); }
  219. bool hasIesProfile() const { return iesProfile != StringTable->EmptyString(); }
  220. GFXOcclusionQuery* getOcclusionQuery() const { return mQuery; }
  221. GFXTextureObject* getCookieTex();
  222. GFXTextureObject* getIesProfileTex();
  223. GFXCubemap* getCookieCubeTex();
  224. // Validates the parameters after a field is changed.
  225. void _validate();
  226. protected:
  227. void _initShadowMap();
  228. ///
  229. LightShadowMap *mShadowMap;
  230. GFXOcclusionQuery* mQuery;
  231. LightInfo *mLight;
  232. GFXTexHandle mCookieTex;
  233. GFXCubemapHandle mCookieCubeTex;
  234. GFXTexHandle mIesTex;
  235. public:
  236. // We're leaving these public for easy access
  237. // for console protected fields.
  238. /// @name Shadow Map
  239. /// @{
  240. ///
  241. U32 texSize;
  242. ///
  243. StringTableEntry cookie;
  244. StringTableEntry iesProfile;
  245. /// @}
  246. Point3F attenuationRatio;
  247. /// @name Point Lights
  248. /// @{
  249. ///
  250. ShadowType shadowType;
  251. /// @}
  252. /// @name Exponential Shadow Map Parameters
  253. /// @{
  254. Point4F overDarkFactor;
  255. /// @}
  256. /// @name Parallel Split Shadow Map
  257. /// @{
  258. ///
  259. F32 shadowDistance;
  260. ///
  261. F32 shadowSoftness;
  262. /// The number of splits in the shadow map.
  263. U32 numSplits;
  264. ///
  265. F32 logWeight;
  266. /// At what distance do we start fading the shadows out completely.
  267. F32 fadeStartDist;
  268. /// This toggles only terrain being visible in the last
  269. /// split of a PSSM shadow map.
  270. bool lastSplitTerrainOnly;
  271. /// @}
  272. };
  273. #endif // _LIGHTSHADOWMAP_H_