lightShadowMap.h 10 KB

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