BsShadowRendering.h 22 KB

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