BsShadowRendering.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsRenderBeastPrerequisites.h"
  5. #include "BsModule.h"
  6. #include "BsMatrix4.h"
  7. #include "BsConvexVolume.h"
  8. #include "BsParamBlocks.h"
  9. #include "BsRendererMaterial.h"
  10. #include "BsTextureAtlasLayout.h"
  11. #include "BsLight.h"
  12. #include "BsLightRendering.h"
  13. namespace bs { namespace ct
  14. {
  15. struct FrameInfo;
  16. class RendererLight;
  17. class RendererScene;
  18. struct ShadowInfo;
  19. /** @addtogroup RenderBeast
  20. * @{
  21. */
  22. /** Number of frustum splits when rendering cascaded shadow maps. */
  23. const UINT32 NUM_CASCADE_SPLITS = 4;
  24. BS_PARAM_BLOCK_BEGIN(ShadowParamsDef)
  25. BS_PARAM_BLOCK_ENTRY(Matrix4, gMatViewProj)
  26. BS_PARAM_BLOCK_ENTRY(Vector2, gNDCZToDeviceZ)
  27. BS_PARAM_BLOCK_ENTRY(float, gDepthBias)
  28. BS_PARAM_BLOCK_ENTRY(float, gInvDepthRange)
  29. BS_PARAM_BLOCK_END
  30. extern ShadowParamsDef gShadowParamsDef;
  31. /** Material used for rendering a single face of a shadow map. */
  32. class ShadowDepthNormalMat : public RendererMaterial<ShadowDepthNormalMat>
  33. {
  34. RMAT_DEF("ShadowDepthNormal.bsl");
  35. public:
  36. ShadowDepthNormalMat();
  37. /** Binds the material to the pipeline, ready to be used on subsequent draw calls. */
  38. void bind(const SPtr<GpuParamBlockBuffer>& shadowParams);
  39. /** Sets a new buffer that determines per-object properties. */
  40. void setPerObjectBuffer(const SPtr<GpuParamBlockBuffer>& perObjectParams);
  41. };
  42. /** Material used for rendering a single face of a shadow map, for a directional light. */
  43. class ShadowDepthDirectionalMat : public RendererMaterial<ShadowDepthDirectionalMat>
  44. {
  45. RMAT_DEF("ShadowDepthDirectional.bsl");
  46. public:
  47. ShadowDepthDirectionalMat();
  48. /** Binds the material to the pipeline, ready to be used on subsequent draw calls. */
  49. void bind(const SPtr<GpuParamBlockBuffer>& shadowParams);
  50. /** Sets a new buffer that determines per-object properties. */
  51. void setPerObjectBuffer(const SPtr<GpuParamBlockBuffer>& perObjectParams);
  52. };
  53. BS_PARAM_BLOCK_BEGIN(ShadowCubeMatricesDef)
  54. BS_PARAM_BLOCK_ENTRY_ARRAY(Matrix4, gFaceVPMatrices, 6)
  55. BS_PARAM_BLOCK_END
  56. extern ShadowCubeMatricesDef gShadowCubeMatricesDef;
  57. BS_PARAM_BLOCK_BEGIN(ShadowCubeMasksDef)
  58. BS_PARAM_BLOCK_ENTRY_ARRAY(int, gFaceMasks, 6)
  59. BS_PARAM_BLOCK_END
  60. extern ShadowCubeMasksDef gShadowCubeMasksDef;
  61. /** Material used for rendering an omni directional cube shadow map. */
  62. class ShadowDepthCubeMat : public RendererMaterial<ShadowDepthCubeMat>
  63. {
  64. RMAT_DEF("ShadowDepthCube.bsl");
  65. public:
  66. ShadowDepthCubeMat();
  67. /** Binds the material to the pipeline, ready to be used on subsequent draw calls. */
  68. void bind(const SPtr<GpuParamBlockBuffer>& shadowParams, const SPtr<GpuParamBlockBuffer>& shadowCubeParams);
  69. /** Sets a new buffer that determines per-object properties. */
  70. void setPerObjectBuffer(const SPtr<GpuParamBlockBuffer>& perObjectParams,
  71. const SPtr<GpuParamBlockBuffer>& shadowCubeMasks);
  72. };
  73. BS_PARAM_BLOCK_BEGIN(ShadowProjectVertParamsDef)
  74. BS_PARAM_BLOCK_ENTRY(Vector4, gPositionAndScale)
  75. BS_PARAM_BLOCK_END
  76. extern ShadowProjectVertParamsDef gShadowProjectVertParamsDef;
  77. /** Material used for populating the stencil buffer when projecting non-omnidirectional shadows. */
  78. template<bool Directional, bool ZFailStencil>
  79. class ShadowProjectStencilMat : public RendererMaterial<ShadowProjectStencilMat<Directional, ZFailStencil>>
  80. {
  81. RMAT_DEF("ShadowProjectStencil.bsl");
  82. public:
  83. ShadowProjectStencilMat();
  84. /** Binds the material and its parameters to the pipeline. */
  85. void bind(const SPtr<GpuParamBlockBuffer>& perCamera);
  86. private:
  87. SPtr<GpuParamBlockBuffer> mVertParams;
  88. };
  89. /** Contains all variations of the ShadowProjectStencilMat material. */
  90. class ShadowProjectStencilMaterials
  91. {
  92. public:
  93. /**
  94. * Binds a shader that can be used for populating the stencil buffer during non-omnidirectional shadow rendering.
  95. *
  96. * @param[in] directional Set to true if shadows from a directional light are being rendered.
  97. * @param[in] useZFailStencil If true the material will use z-fail operation to modify the stencil buffer. If
  98. * false z-pass will be used instead. Z-pass is a more performant alternative as it
  99. * doesn't disable hi-z optimization, but it cannot handle the case when the viewer is
  100. * inside the drawn geometry.
  101. * @param[in] perCamera Buffer containing information about the current view.
  102. */
  103. void bind(bool directional, bool useZFailStencil, const SPtr<GpuParamBlockBuffer>& perCamera);
  104. private:
  105. ShadowProjectStencilMat<true, true> mTT;
  106. ShadowProjectStencilMat<false, true> mFT;
  107. ShadowProjectStencilMat<false, false> mFF;
  108. };
  109. /** Common parameters used by the shadow projection materials. */
  110. struct ShadowProjectParams
  111. {
  112. ShadowProjectParams(const Light& light, const SPtr<Texture>& shadowMap, UINT32 shadowMapFace,
  113. const SPtr<GpuParamBlockBuffer>& shadowParams, const SPtr<GpuParamBlockBuffer>& perCameraParams,
  114. const RenderTargets& renderTargets)
  115. : light(light), shadowMap(shadowMap), shadowMapFace(shadowMapFace), shadowParams(shadowParams)
  116. , perCamera(perCameraParams), renderTargets(renderTargets)
  117. { }
  118. /** Light which is casting the shadow. */
  119. const Light& light;
  120. /** Texture containing the shadow map. */
  121. const SPtr<Texture>& shadowMap;
  122. /** Face of the shadow map to bind, if it has multiple faces. */
  123. UINT32 shadowMapFace;
  124. /** Parameter block containing parameters specific for shadow projection. */
  125. const SPtr<GpuParamBlockBuffer> shadowParams;
  126. /** Parameter block containing parameters specific to this view. */
  127. const SPtr<GpuParamBlockBuffer>& perCamera;
  128. /** Contains the GBuffer textures. */
  129. const RenderTargets& renderTargets;
  130. };
  131. BS_PARAM_BLOCK_BEGIN(ShadowProjectParamsDef)
  132. BS_PARAM_BLOCK_ENTRY(Matrix4, gMixedToShadowSpace)
  133. BS_PARAM_BLOCK_ENTRY(Vector2, gShadowMapSize)
  134. BS_PARAM_BLOCK_ENTRY(Vector2, gShadowMapSizeInv)
  135. BS_PARAM_BLOCK_ENTRY(float, gSoftTransitionScale)
  136. BS_PARAM_BLOCK_ENTRY(float, gFadePercent)
  137. BS_PARAM_BLOCK_ENTRY(float, gFadePlaneDepth)
  138. BS_PARAM_BLOCK_ENTRY(float, gInvFadePlaneRange)
  139. BS_PARAM_BLOCK_END
  140. extern ShadowProjectParamsDef gShadowProjectParamsDef;
  141. /** Material used for projecting depth into a shadow accumulation buffer for non-omnidirectional shadow maps. */
  142. template<int ShadowQuality, bool Directional, bool MSAA>
  143. class ShadowProjectMat : public RendererMaterial<ShadowProjectMat<ShadowQuality, Directional, MSAA>>
  144. {
  145. RMAT_DEF("ShadowProject.bsl");
  146. public:
  147. ShadowProjectMat();
  148. /** Binds the material and its parameters to the pipeline. */
  149. void bind(const ShadowProjectParams& params);
  150. private:
  151. SPtr<SamplerState> mSamplerState;
  152. SPtr<GpuParamBlockBuffer> mVertParams;
  153. GBufferParams mGBufferParams;
  154. GpuParamTexture mShadowMapParam;
  155. GpuParamSampState mShadowSamplerParam;
  156. };
  157. /** Contains all variations of the ShadowProjectMat material. */
  158. class ShadowProjectMaterials
  159. {
  160. public:
  161. /**
  162. * Binds the appropriate variation of the ShadowProjectMat based on the provided parameters.
  163. *
  164. * @param[in] quality Quality of the shadow filtering to use. In range [1, 4].
  165. * @param[in] directional True if rendering a shadow from a directional light.
  166. * @param[in] MSAA True if the GBuffer contains per-sample data.
  167. * @param[in] params Parameters to be passed along to the material.
  168. */
  169. void bind(UINT32 quality, bool directional, bool MSAA, const ShadowProjectParams& params);
  170. private:
  171. #define MAT_MEMBERS(QUALITY) \
  172. ShadowProjectMat<QUALITY, true, true> mMat##QUALITY##TT; \
  173. ShadowProjectMat<QUALITY, true, false> mMat##QUALITY##TF; \
  174. ShadowProjectMat<QUALITY, false, true> mMat##QUALITY##FT; \
  175. ShadowProjectMat<QUALITY, false, false> mMat##QUALITY##FF;
  176. MAT_MEMBERS(1)
  177. MAT_MEMBERS(2)
  178. MAT_MEMBERS(3)
  179. MAT_MEMBERS(4)
  180. #undef MAT_MEMBERS
  181. };
  182. BS_PARAM_BLOCK_BEGIN(ShadowProjectOmniParamsDef)
  183. BS_PARAM_BLOCK_ENTRY_ARRAY(Matrix4, gFaceVPMatrices, 6)
  184. BS_PARAM_BLOCK_ENTRY(Vector4, gLightPosAndRadius)
  185. BS_PARAM_BLOCK_ENTRY(float, gInvResolution)
  186. BS_PARAM_BLOCK_ENTRY(float, gFadePercent)
  187. BS_PARAM_BLOCK_ENTRY(float, gDepthBias)
  188. BS_PARAM_BLOCK_END
  189. extern ShadowProjectOmniParamsDef gShadowProjectOmniParamsDef;
  190. /** Material used for projecting depth into a shadow accumulation buffer for omnidirectional shadow maps. */
  191. template<int ShadowQuality, bool Inside, bool MSAA>
  192. class ShadowProjectOmniMat : public RendererMaterial<ShadowProjectOmniMat<ShadowQuality, Inside, MSAA>>
  193. {
  194. RMAT_DEF("ShadowProjectOmni.bsl");
  195. public:
  196. ShadowProjectOmniMat();
  197. /** Binds the material and its parameters to the pipeline. */
  198. void bind(const ShadowProjectParams& params);
  199. private:
  200. SPtr<SamplerState> mSamplerState;
  201. SPtr<GpuParamBlockBuffer> mVertParams;
  202. GBufferParams mGBufferParams;
  203. GpuParamTexture mShadowMapParam;
  204. GpuParamSampState mShadowSamplerParam;
  205. };
  206. /** Contains all variations of the ShadowProjectOmniMat material. */
  207. class ShadowProjectOmniMaterials
  208. {
  209. public:
  210. /**
  211. * Binds the appropriate variation of the ShadowProjectOmniMat based on the provided parameters.
  212. *
  213. * @param[in] quality Quality of the shadow filtering to use. In range [1, 4].
  214. * @param[in] inside True if the viewer is inside the light volume.
  215. * @param[in] MSAA True if the GBuffer contains per-sample data.
  216. * @param[in] params Parameters to be passed along to the material.
  217. */
  218. void bind(UINT32 quality, bool inside, bool MSAA, const ShadowProjectParams& params);
  219. private:
  220. #define MAT_MEMBERS(QUALITY) \
  221. ShadowProjectOmniMat<QUALITY, true, true> mMat##QUALITY##TT; \
  222. ShadowProjectOmniMat<QUALITY, true, false> mMat##QUALITY##TF; \
  223. ShadowProjectOmniMat<QUALITY, false, true> mMat##QUALITY##FT; \
  224. ShadowProjectOmniMat<QUALITY, false, false> mMat##QUALITY##FF;
  225. MAT_MEMBERS(1)
  226. MAT_MEMBERS(2)
  227. MAT_MEMBERS(3)
  228. MAT_MEMBERS(4)
  229. #undef MAT_MEMBERS
  230. };
  231. /** Pixel format used for rendering and storing shadow maps. */
  232. const PixelFormat SHADOW_MAP_FORMAT = PF_D16;
  233. /** Information about a shadow cast from a single light. */
  234. struct ShadowInfo
  235. {
  236. /** Updates normalized area coordinates based on the non-normalized ones and the provided atlas size. */
  237. void updateNormArea(UINT32 atlasSize);
  238. UINT32 lightIdx; /**< Index of the light casting this shadow. */
  239. Rect2I area; /**< Area of the shadow map in pixels, relative to its source texture. */
  240. Rect2 normArea; /**< Normalized shadow map area in [0, 1] range. */
  241. UINT32 textureIdx; /**< Index of the texture the shadow map is stored in. */
  242. float depthNear; /**< Distance to the near plane. */
  243. float depthFar; /**< Distance to the far plane. */
  244. float depthFade; /**< Distance to the plane at which to start fading out the shadows (only for CSM). */
  245. float fadeRange; /**< Distance from the fade plane to the far plane (only for CSM). */
  246. float depthBias; /**< Bias used to reduce shadow acne. */
  247. float depthRange; /**< Length of the range covered by the shadow caster volume. */
  248. UINT32 cascadeIdx; /**< Index of a cascade. Only relevant for CSM. */
  249. /** View-projection matrix from the shadow casters point of view. */
  250. Matrix4 shadowVPTransform;
  251. /** View-projection matrix for each cubemap face, used for omni-directional shadows. */
  252. Matrix4 shadowVPTransforms[6];
  253. /** Bounds of the geometry the shadow is being applied on. */
  254. Sphere subjectBounds;
  255. /** Determines the fade amount of the shadow, for each view in the scene. */
  256. SmallVector<float, 4> fadePerView;
  257. };
  258. /**
  259. * Contains a texture that serves as an atlas for one or multiple shadow maps. Provides methods for inserting new maps
  260. * in the atlas.
  261. */
  262. class ShadowMapAtlas
  263. {
  264. public:
  265. ShadowMapAtlas(UINT32 size);
  266. ~ShadowMapAtlas();
  267. /**
  268. * Registers a new map in the shadow map atlas. Returns true if the map fits in the atlas, or false otherwise.
  269. * Resets the last used counter to zero.
  270. */
  271. bool addMap(UINT32 size, Rect2I& area, UINT32 border = 4);
  272. /** Clears all shadow maps from the atlas. Increments the last used counter.*/
  273. void clear();
  274. /** Checks have any maps been added to the atlas. */
  275. bool isEmpty() const;
  276. /**
  277. * Returns the value of the last used counter. See addMap() and clear() for information on how the counter is
  278. * incremented/decremented.
  279. */
  280. UINT32 getLastUsedCounter() const { return mLastUsedCounter; }
  281. /** Returns the bindable atlas texture. */
  282. SPtr<Texture> getTexture() const;
  283. /** Returns the render target that allows you to render into the atlas. */
  284. SPtr<RenderTexture> getTarget() const;
  285. private:
  286. SPtr<PooledRenderTexture> mAtlas;
  287. TextureAtlasLayout mLayout;
  288. UINT32 mLastUsedCounter;
  289. };
  290. /** Contains common code for different shadow map types. */
  291. class ShadowMapBase
  292. {
  293. public:
  294. ShadowMapBase(UINT32 size);
  295. virtual ~ShadowMapBase() {}
  296. /** Returns the bindable shadow map texture. */
  297. SPtr<Texture> getTexture() const;
  298. /** Returns the size of a single face of the shadow map texture, in pixels. */
  299. UINT32 getSize() const { return mSize; }
  300. /** Makes the shadow map available for re-use and increments the counter returned by getLastUsedCounter(). */
  301. void clear() { mIsUsed = false; mLastUsedCounter++; }
  302. /** Marks the shadow map as used and resets the last used counter to zero. */
  303. void markAsUsed() { mIsUsed = true; mLastUsedCounter = 0; }
  304. /** Returns true if the object is storing a valid shadow map. */
  305. bool isUsed() const { return mIsUsed; }
  306. /**
  307. * Returns the value of the last used counter. See incrementUseCounter() and markAsUsed() for information on how is
  308. * the counter incremented/decremented.
  309. */
  310. UINT32 getLastUsedCounter() const { return mLastUsedCounter; }
  311. protected:
  312. SPtr<PooledRenderTexture> mShadowMap;
  313. UINT32 mSize;
  314. bool mIsUsed;
  315. UINT32 mLastUsedCounter;
  316. };
  317. /** Contains a cubemap for storing an omnidirectional cubemap. */
  318. class ShadowCubemap : public ShadowMapBase
  319. {
  320. public:
  321. ShadowCubemap(UINT32 size);
  322. ~ShadowCubemap();
  323. /** Returns a render target encompassing all six faces of the shadow cubemap. */
  324. SPtr<RenderTexture> getTarget() const;
  325. };
  326. /** Contains a texture required for rendering cascaded shadow maps. */
  327. class ShadowCascadedMap : public ShadowMapBase
  328. {
  329. public:
  330. ShadowCascadedMap(UINT32 size);
  331. ~ShadowCascadedMap();
  332. /** Returns a render target that allows rendering into a specific cascade of the cascaded shadow map. */
  333. SPtr<RenderTexture> getTarget(UINT32 cascadeIdx) const;
  334. /** Provides information about a shadow for the specified cascade. */
  335. void setShadowInfo(UINT32 cascadeIdx, const ShadowInfo& info) { mShadowInfos[cascadeIdx] = info; }
  336. /** @copydoc setShadowInfo */
  337. const ShadowInfo& getShadowInfo(UINT32 cascadeIdx) const { return mShadowInfos[cascadeIdx]; }
  338. private:
  339. SPtr<RenderTexture> mTargets[NUM_CASCADE_SPLITS];
  340. ShadowInfo mShadowInfos[NUM_CASCADE_SPLITS];
  341. };
  342. /** Provides functionality for rendering shadow maps. */
  343. class ShadowRendering : public Module<ShadowRendering>
  344. {
  345. /** Contains information required for generating a shadow map for a specific light. */
  346. struct ShadowMapOptions
  347. {
  348. UINT32 lightIdx;
  349. UINT32 mapSize;
  350. SmallVector<float, 4> fadePercents;
  351. };
  352. /** Contains references to all shadows cast by a specific light. */
  353. struct LightShadows
  354. {
  355. UINT32 startIdx;
  356. UINT32 numShadows;
  357. };
  358. public:
  359. ShadowRendering(UINT32 shadowMapSize);
  360. /** For each visible shadow casting light, renders a shadow map from its point of view. */
  361. void renderShadowMaps(RendererScene& scene, const RendererViewGroup& viewGroup, const FrameInfo& frameInfo);
  362. /**
  363. * Renders shadow occlusion values for the specified light, into the currently bound render target.
  364. * The system uses shadow maps rendered by renderShadowMaps().
  365. */
  366. void renderShadowOcclusion(const RendererScene& scene, UINT32 shadowQuality, const RendererLight& light,
  367. UINT32 viewIdx);
  368. /** Changes the default shadow map size. Will cause all shadow maps to be rebuilt. */
  369. void setShadowMapSize(UINT32 size);
  370. private:
  371. /** Renders cascaded shadow maps for the provided directional light viewed from the provided view. */
  372. void renderCascadedShadowMaps(UINT32 viewIdx, UINT32 lightIdx, RendererScene& scene, const FrameInfo& frameInfo);
  373. /** Renders shadow maps for the provided spot light. */
  374. void renderSpotShadowMap(const RendererLight& light, const ShadowMapOptions& options, RendererScene& scene,
  375. const FrameInfo& frameInfo);
  376. /** Renders shadow maps for the provided radial light. */
  377. void renderRadialShadowMap(const RendererLight& light, const ShadowMapOptions& options, RendererScene& scene,
  378. const FrameInfo& frameInfo);
  379. /**
  380. * Calculates optimal shadow map size, taking into account all views in the scene. Also calculates a fade value
  381. * that can be used for fading out small shadow maps.
  382. *
  383. * @param[in] light Light for which to calculate the shadow map properties. Cannot be a directional light.
  384. * @param[in] scene Scene information containing all the views the light can be seen through.
  385. * @param[in] border Border to reduce the shadow map size by, in pixels.
  386. * @param[out] size Optimal size of the shadow map, in pixels.
  387. * @param[out] fadePercents Value in range [0, 1] determining how much should the shadow map be faded out. Each
  388. * entry corresponds to a single view.
  389. * @param[out] maxFadePercent Maximum value in the @p fadePercents array.
  390. */
  391. void calcShadowMapProperties(const RendererLight& light, RendererScene& scene, UINT32 border, UINT32& size,
  392. SmallVector<float, 4>& fadePercents, float& maxFadePercent) const;
  393. /**
  394. * Draws a mesh representing near and far planes at the provided coordinates. The mesh is constructed using
  395. * normalized device coordinates and requires no perspective transform. Near plane will be drawn using front facing
  396. * triangles, and the far plane will be drawn using back facing triangles.
  397. *
  398. * @param[in] near Location of the near plane, in NDC.
  399. * @param[in] far Location of the far plane, in NDC.
  400. * @param[in] drawNear If disabled, only the far plane will be drawn.
  401. */
  402. void drawNearFarPlanes(float near, float far, bool drawNear = true);
  403. /**
  404. * Draws a frustum mesh using the provided vertices as its corners. Corners should be in the order specified
  405. * by AABox::Corner enum.
  406. */
  407. void drawFrustum(const std::array<Vector3, 8>& corners);
  408. /**
  409. * Calculates optimal shadow quality based on the quality set in the options and the actual shadow map resolution.
  410. */
  411. static UINT32 getShadowQuality(UINT32 requestedQuality, UINT32 shadowMapResolution, UINT32 minAllowedQuality);
  412. /**
  413. * Generates a frustum for a single cascade of a cascaded shadow map. Also outputs spherical bounds of the
  414. * split view frustum.
  415. *
  416. * @param[in] view View whose frustum to split.
  417. * @param[in] lightDir Direction of the light for which we're generating the shadow map.
  418. * @param[in] cascade Index of the cascade to generate the frustum for.
  419. * @param[in] numCascades Maximum number of cascades in the cascaded shadow map. Must be greater than zero.
  420. * @param[out] outBounds Spherical bounds of the split view frustum.
  421. * @return Convex volume covering the area of the split view frustum visible from the light.
  422. */
  423. static ConvexVolume getCSMSplitFrustum(const RendererView& view, const Vector3& lightDir, UINT32 cascade,
  424. UINT32 numCascades, Sphere& outBounds);
  425. /**
  426. * Finds the distance (along the view direction) of the frustum split for the specified index. Used for cascaded
  427. * shadow maps.
  428. *
  429. * @param[in] view View whose frustum to split.
  430. * @param[in] index Index of the split. 0 = near plane.
  431. * @param[in] numCascades Maximum number of cascades in the cascaded shadow map. Must be greater than zero
  432. * and greater or equal to @p index.
  433. * @return Distance to the split position along the view direction.
  434. */
  435. static float getCSMSplitDistance(const RendererView& view, UINT32 index, UINT32 numCascades);
  436. /**
  437. * Calculates a bias that can be applied when rendering shadow maps, in order to reduce shadow artifacts.
  438. *
  439. * @param[in] light Light to calculate the depth bias for.
  440. * @param[in] radius Radius of the light bounds.
  441. * @param[in] depthRange Range of depths (distance between near and far planes) covered by the shadow.
  442. * @param[in] mapSize Size of the shadow map, in pixels.
  443. * @return Depth bias that can be passed to shadow depth rendering shader.
  444. */
  445. static float getDepthBias(const Light& light, float radius, float depthRange, UINT32 mapSize);
  446. /**
  447. * Calculates a fade transition value that can be used for slowly fading-in the shadow, in order to avoid or reduce
  448. * shadow acne.
  449. *
  450. * @param[in] light Light to calculate the fade transition size for.
  451. * @param[in] radius Radius of the light bounds.
  452. * @param[in] depthRange Range of depths (distance between near and far planes) covered by the shadow.
  453. * @param[in] mapSize Size of the shadow map, in pixels.
  454. * @return Value that determines the size of the fade transition region.
  455. */
  456. static float getFadeTransition(const Light& light, float radius, float depthRange, UINT32 mapSize);
  457. /** Size of a single shadow map atlas, in pixels. */
  458. static const UINT32 MAX_ATLAS_SIZE;
  459. /** Determines how long will an unused shadow map atlas stay allocated, in frames. */
  460. static const UINT32 MAX_UNUSED_FRAMES;
  461. /** Determines the minimal resolution of a shadow map. */
  462. static const UINT32 MIN_SHADOW_MAP_SIZE;
  463. /** Determines the resolution at which shadow maps begin fading out. */
  464. static const UINT32 SHADOW_MAP_FADE_SIZE;
  465. /** Size of the border of a shadow map in a shadow map atlas, in pixels. */
  466. static const UINT32 SHADOW_MAP_BORDER;
  467. /** Percent of the length of a single cascade in a CSM, in which to fade out the cascade. */
  468. static const float CASCADE_FRACTION_FADE;
  469. ShadowDepthNormalMat mDepthNormalMat;
  470. ShadowDepthCubeMat mDepthCubeMat;
  471. ShadowDepthDirectionalMat mDepthDirectionalMat;
  472. ShadowProjectStencilMaterials mProjectStencilMaterials;
  473. ShadowProjectMaterials mProjectMaterials;
  474. ShadowProjectOmniMaterials mProjectOmniMaterials;
  475. UINT32 mShadowMapSize;
  476. Vector<ShadowMapAtlas> mDynamicShadowMaps;
  477. Vector<ShadowCascadedMap> mCascadedShadowMaps;
  478. Vector<ShadowCubemap> mShadowCubemaps;
  479. Vector<ShadowInfo> mShadowInfos;
  480. Vector<LightShadows> mSpotLightShadows;
  481. Vector<LightShadows> mRadialLightShadows;
  482. Vector<UINT32> mDirectionalLightShadows;
  483. SPtr<VertexDeclaration> mPositionOnlyVD;
  484. // Mesh information used for drawing near & far planes
  485. SPtr<IndexBuffer> mPlaneIB;
  486. SPtr<VertexBuffer> mPlaneVB;
  487. // Mesh information used for drawing a shadow frustum
  488. SPtr<IndexBuffer> mFrustumIB;
  489. SPtr<VertexBuffer> mFrustumVB;
  490. Vector<bool> mRenderableVisibility; // Transient
  491. Vector<ShadowMapOptions> mSpotLightShadowOptions; // Transient
  492. Vector<ShadowMapOptions> mRadialLightShadowOptions; // Transient
  493. };
  494. /* @} */
  495. }}