lightShadowMap.h 10 KB

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