| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #pragma once
- #include "BsRenderBeastPrerequisites.h"
- #include "BsModule.h"
- #include "BsMatrix4.h"
- #include "BsConvexVolume.h"
- #include "BsParamBlocks.h"
- #include "BsRendererMaterial.h"
- #include "BsTextureAtlasLayout.h"
- #include "BsLight.h"
- #include "BsLightRendering.h"
- namespace bs { namespace ct
- {
- struct FrameInfo;
- class RendererLight;
- class RendererScene;
- /** @addtogroup RenderBeast
- * @{
- */
- /** Number of frustum splits when rendering cascaded shadow maps. */
- const UINT32 NUM_CASCADE_SPLITS = 4;
- BS_PARAM_BLOCK_BEGIN(ShadowParamsDef)
- BS_PARAM_BLOCK_ENTRY(Matrix4, gMatViewProj)
- BS_PARAM_BLOCK_ENTRY(float, gDepthBias)
- BS_PARAM_BLOCK_ENTRY(float, gDepthRange)
- BS_PARAM_BLOCK_END
- extern ShadowParamsDef gShadowParamsDef;
-
- /** Material used for rendering a single face of a shadow map. */
- class ShadowDepthNormalMat : public RendererMaterial<ShadowDepthNormalMat>
- {
- RMAT_DEF("ShadowDepthNormal.bsl");
- public:
- ShadowDepthNormalMat();
- /** Binds the material to the pipeline, ready to be used on subsequent draw calls. */
- void bind(const SPtr<GpuParamBlockBuffer>& shadowParams);
- /** Sets a new buffer that determines per-object properties. */
- void setPerObjectBuffer(const SPtr<GpuParamBlockBuffer>& perObjectParams);
- };
- /** Material used for rendering a single face of a shadow map, for a directional light. */
- class ShadowDepthDirectionalMat : public RendererMaterial<ShadowDepthDirectionalMat>
- {
- RMAT_DEF("ShadowDepthDirectional.bsl");
- public:
- ShadowDepthDirectionalMat();
- /** Binds the material to the pipeline, ready to be used on subsequent draw calls. */
- void bind(const SPtr<GpuParamBlockBuffer>& shadowParams);
- /** Sets a new buffer that determines per-object properties. */
- void setPerObjectBuffer(const SPtr<GpuParamBlockBuffer>& perObjectParams);
- };
- BS_PARAM_BLOCK_BEGIN(ShadowCubeMatricesDef)
- BS_PARAM_BLOCK_ENTRY_ARRAY(Matrix4, gFaceVPMatrices, 6)
- BS_PARAM_BLOCK_END
- extern ShadowCubeMatricesDef gShadowCubeMatricesDef;
- BS_PARAM_BLOCK_BEGIN(ShadowCubeMasksDef)
- BS_PARAM_BLOCK_ENTRY_ARRAY(int, gFaceMasks, 6)
- BS_PARAM_BLOCK_END
- extern ShadowCubeMasksDef gShadowCubeMasksDef;
- /** Material used for rendering an omni directional cube shadow map. */
- class ShadowDepthCubeMat : public RendererMaterial<ShadowDepthCubeMat>
- {
- RMAT_DEF("ShadowDepthCube.bsl");
- public:
- ShadowDepthCubeMat();
- /** Binds the material to the pipeline, ready to be used on subsequent draw calls. */
- void bind(const SPtr<GpuParamBlockBuffer>& shadowParams, const SPtr<GpuParamBlockBuffer>& shadowCubeParams);
- /** Sets a new buffer that determines per-object properties. */
- void setPerObjectBuffer(const SPtr<GpuParamBlockBuffer>& perObjectParams,
- const SPtr<GpuParamBlockBuffer>& shadowCubeMasks);
- };
- BS_PARAM_BLOCK_BEGIN(ShadowProjectParamsDef)
- BS_PARAM_BLOCK_ENTRY(Matrix4, gMixedToShadowSpace)
- BS_PARAM_BLOCK_ENTRY(Vector2, gShadowMapSize)
- BS_PARAM_BLOCK_ENTRY(Vector2, gShadowMapSizeInv)
- BS_PARAM_BLOCK_ENTRY(float, gSoftTransitionScale)
- BS_PARAM_BLOCK_ENTRY(float, gFadePercent)
- BS_PARAM_BLOCK_ENTRY(float, gFadePlaneDepth)
- BS_PARAM_BLOCK_ENTRY(float, gInvFadePlaneRange)
- BS_PARAM_BLOCK_END
- extern ShadowProjectParamsDef gShadowProjectParamsDef;
- /** Material used for projecting depth into a shadow accumulation buffer for non-omnidirectional shadow maps. */
- template<int ShadowQuality, bool Directional, bool MSAA>
- class ShadowProjectMat : public RendererMaterial<ShadowProjectMat<ShadowQuality, Directional, MSAA>>
- {
- RMAT_DEF("ShadowProject.bsl");
- public:
- ShadowProjectMat();
- /** Binds the material to the pipeline, ready to be used on subsequent draw calls. */
- void bind(const SPtr<Texture>& shadowMap, const SPtr<GpuParamBlockBuffer>& shadowParams,
- const SPtr<GpuParamBlockBuffer>& perCamera);
- private:
- SPtr<SamplerState> mSamplerState;
- GBufferParams mGBufferParams;
- GpuParamTexture mShadowMapParam;
- GpuParamSampState mShadowSamplerParam;
- };
- BS_PARAM_BLOCK_BEGIN(ShadowProjectOmniParamsDef)
- BS_PARAM_BLOCK_ENTRY_ARRAY(Matrix4, gFaceVPMatrices, 6)
- BS_PARAM_BLOCK_ENTRY(Vector4, gLightPosAndRadius)
- BS_PARAM_BLOCK_ENTRY(float, gInvResolution)
- BS_PARAM_BLOCK_ENTRY(float, gFadePercent)
- BS_PARAM_BLOCK_ENTRY(float, gDepthBias)
- BS_PARAM_BLOCK_END
- extern ShadowProjectOmniParamsDef gShadowProjectOmniParamsDef;
- /** Material used for projecting depth into a shadow accumulation buffer for omnidirectional shadow maps. */
- template<int ShadowQuality, bool MSAA>
- class ShadowProjectOmniMat : public RendererMaterial<ShadowProjectOmniMat<ShadowQuality, MSAA>>
- {
- RMAT_DEF("ShadowProjectOmni.bsl");
- public:
- ShadowProjectOmniMat();
- /** Binds the material to the pipeline, ready to be used on subsequent draw calls. */
- void bind(const SPtr<Texture>& shadowMap, const SPtr<GpuParamBlockBuffer>& shadowParams,
- const SPtr<GpuParamBlockBuffer>& perCamera);
- private:
- SPtr<SamplerState> mSamplerState;
- GBufferParams mGBufferParams;
- GpuParamTexture mShadowMapParam;
- GpuParamSampState mShadowSamplerParam;
- };
- /** Information about a shadow cast from a single light. */
- struct ShadowInfo
- {
- /** Updates normalized area coordinates based on the non-normalized ones and the provided atlas size. */
- void updateNormArea(UINT32 atlasSize);
- UINT32 lightIdx; /**< Index of the light casting this shadow. */
- Rect2I area; /**< Area of the shadow map in pixels, relative to its source texture. */
- Rect2 normArea; /**< Normalized shadow map area in [0, 1] range. */
- UINT32 textureIdx; /**< Index of the texture the shadow map is stored in. */
- /** View-projection matrix from the shadow casters point of view. */
- Matrix4 shadowVPTransform;
- /** View-projection matrix for each cubemap face, used for omni-directional shadows. */
- Matrix4 shadowVPTransforms[6];
- /** Determines the fade amount of the shadow, for each view in the scene. */
- SmallVector<float, 4> fadePerView;
- };
- /**
- * Contains a texture that serves as an atlas for one or multiple shadow maps. Provides methods for inserting new maps
- * in the atlas.
- */
- class ShadowMapAtlas
- {
- public:
- ShadowMapAtlas(UINT32 size);
- ~ShadowMapAtlas();
- /**
- * Registers a new map in the shadow map atlas. Returns true if the map fits in the atlas, or false otherwise.
- * Resets the last used counter to zero.
- */
- bool addMap(UINT32 size, Rect2I& area, UINT32 border = 4);
- /** Clears all shadow maps from the atlas. Increments the last used counter.*/
- void clear();
- /** Checks have any maps been added to the atlas. */
- bool isEmpty() const;
- /**
- * Returns the value of the last used counter. See addMap() and clear() for information on how the counter is
- * incremented/decremented.
- */
- UINT32 getLastUsedCounter() const { return mLastUsedCounter; }
- /** Returns the bindable atlas texture. */
- SPtr<Texture> getTexture() const;
- /** Returns the render target that allows you to render into the atlas. */
- SPtr<RenderTexture> getTarget() const;
- private:
- SPtr<PooledRenderTexture> mAtlas;
- TextureAtlasLayout mLayout;
- UINT32 mLastUsedCounter;
- };
- /** Contains common code for different shadow map types. */
- class ShadowMapBase
- {
- public:
- ShadowMapBase(UINT32 size);
- virtual ~ShadowMapBase() {}
- /** Returns the bindable shadow map texture. */
- SPtr<Texture> getTexture() const;
- /** Returns the size of a single face of the shadow map texture, in pixels. */
- UINT32 getSize() const { return mSize; }
- /** Makes the shadow map available for re-use and increments the counter returned by getLastUsedCounter(). */
- void clear() { mIsUsed = false; mLastUsedCounter++; }
- /** Marks the shadow map as used and resets the last used counter to zero. */
- void markAsUsed() { mIsUsed = true; mLastUsedCounter = 0; }
- /** Returns true if the object is storing a valid shadow map. */
- bool isUsed() const { return mIsUsed; }
- /**
- * Returns the value of the last used counter. See incrementUseCounter() and markAsUsed() for information on how is
- * the counter incremented/decremented.
- */
- UINT32 getLastUsedCounter() const { return mLastUsedCounter; }
- protected:
- SPtr<PooledRenderTexture> mShadowMap;
- UINT32 mSize;
- bool mIsUsed;
- UINT32 mLastUsedCounter;
- };
- /** Contains a cubemap for storing an omnidirectional cubemap. */
- class ShadowCubemap : public ShadowMapBase
- {
- public:
- ShadowCubemap(UINT32 size);
- ~ShadowCubemap();
- /** Returns a render target encompassing all six faces of the shadow cubemap. */
- SPtr<RenderTexture> getTarget() const;
- };
- /** Contains a texture required for rendering cascaded shadow maps. */
- class ShadowCascadedMap : public ShadowMapBase
- {
- public:
- ShadowCascadedMap(UINT32 size);
- ~ShadowCascadedMap();
- /** Returns a render target that allows rendering into a specific cascade of the cascaded shadow map. */
- SPtr<RenderTexture> getTarget(UINT32 cascadeIdx) const;
- /** Provides information about a shadow for the specified cascade. */
- void setShadowInfo(UINT32 cascadeIdx, const ShadowInfo& info) { mShadowInfos[cascadeIdx] = info; }
- /** @copydoc setShadowInfo */
- const ShadowInfo& getShadowInfo(UINT32 cascadeIdx) const { return mShadowInfos[cascadeIdx]; }
- private:
- SPtr<RenderTexture> mTargets[NUM_CASCADE_SPLITS];
- ShadowInfo mShadowInfos[NUM_CASCADE_SPLITS];
- };
- /** Provides functionality for rendering shadow maps. */
- class ShadowRendering : public Module<ShadowRendering>
- {
- /** Contains information required for generating a shadow map for a specific light. */
- struct ShadowMapOptions
- {
- UINT32 lightIdx;
- UINT32 mapSize;
- SmallVector<float, 4> fadePercents;
- };
- /** Contains references to all shadows cast by a specific light. */
- struct LightShadows
- {
- UINT32 startIdx;
- UINT32 numShadows;
- };
- public:
- ShadowRendering(UINT32 shadowMapSize);
- /** For each visibile shadow casting light, renders a shadow map from its point of view. */
- void renderShadowMaps(RendererScene& scene, const FrameInfo& frameInfo);
- /**
- * Renders shadow occlusion values for the specified light, into the currently bound render target.
- * The system uses shadow maps rendered by renderShadowMaps().
- */
- void renderShadowOcclusion(const RendererScene& scene, const RendererLight& light, UINT32 viewIdx);
- /** Changes the default shadow map size. Will cause all shadow maps to be rebuilt. */
- void setShadowMapSize(UINT32 size);
- private:
- /** Renders cascaded shadow maps for the provided directional light viewed from the provided view. */
- void renderCascadedShadowMaps(UINT32 viewIdx, UINT32 lightIdx, RendererScene& scene, const FrameInfo& frameInfo);
- /** Renders shadow maps for the provided spot light. */
- void renderSpotShadowMap(const RendererLight& light, const ShadowMapOptions& options, RendererScene& scene,
- const FrameInfo& frameInfo);
- /** Renders shadow maps for the provided radial light. */
- void renderRadialShadowMap(const RendererLight& light, const ShadowMapOptions& options, RendererScene& scene,
- const FrameInfo& frameInfo);
- /**
- * Calculates optimal shadow map size, taking into account all views in the scene. Also calculates a fade value
- * that can be used for fading out small shadow maps.
- *
- * @param[in] light Light for which to calculate the shadow map properties. Cannot be a directional light.
- * @param[in] scene Scene information containing all the views the light can be seen through.
- * @param[out] size Optimal size of the shadow map, in pixels.
- * @param[out] fadePercents Value in range [0, 1] determining how much should the shadow map be faded out. Each
- * entry corresponds to a single view.
- * @param[out] maxFadePercent Maximum value in the @p fadePercents array.
- */
- void calcShadowMapProperties(const RendererLight& light, RendererScene& scene, UINT32& size,
- SmallVector<float, 4>& fadePercents, float& maxFadePercent) const;
- /**
- * Generates a frustum for a single cascade of a cascaded shadow map. Also outputs spherical bounds of the
- * split view frustum.
- *
- * @param[in] view View whose frustum to split.
- * @param[in] lightDir Direction of the light for which we're generating the shadow map.
- * @param[in] cascade Index of the cascade to generate the frustum for.
- * @param[in] numCascades Maximum number of cascades in the cascaded shadow map. Must be greater than zero.
- * @param[out] outBounds Spherical bounds of the split view frustum.
- * @return Convex volume covering the area of the split view frustum visible from the light.
- */
- static ConvexVolume getCSMSplitFrustum(const RendererView& view, const Vector3& lightDir, UINT32 cascade,
- UINT32 numCascades, Sphere& outBounds);
- /**
- * Finds the distance (along the view direction) of the frustum split for the specified index. Used for cascaded
- * shadow maps.
- *
- * @param[in] view View whose frustum to split.
- * @param[in] index Index of the split. 0 = near plane.
- * @param[in] numCascades Maximum number of cascades in the cascaded shadow map. Must be greater than zero
- * and greater or equal to @p index.
- * @return Distance to the split position along the view direction.
- */
- static float getCSMSplitDistance(const RendererView& view, UINT32 index, UINT32 numCascades);
- /**
- * Calculates a bias that can be applied when rendering shadow maps, in order to reduce shadow artifacts.
- *
- * @param[in] light Light to calculate the depth bias for.
- * @param[in] depthRange Range of depths (distance between near and far planes) covered by the shadow.
- * @param[in] mapSize Size of the shadow map, in pixels.
- * @return Depth bias that can be passed to shadow depth rendering shader.
- */
- static float getDepthBias(const Light& light, float depthRange, UINT32 mapSize);
- /** Size of a single shadow map atlas, in pixels. */
- static const UINT32 MAX_ATLAS_SIZE;
- /** Determines how long will an unused shadow map atlas stay allocated, in frames. */
- static const UINT32 MAX_UNUSED_FRAMES;
- /** Determines the minimal resolution of a shadow map. */
- static const UINT32 MIN_SHADOW_MAP_SIZE;
- /** Determines the resolution at which shadow maps begin fading out. */
- static const UINT32 SHADOW_MAP_FADE_SIZE;
- /** Size of the border of a shadow map in a shadow map atlas, in pixels. */
- static const UINT32 SHADOW_MAP_BORDER;
- ShadowDepthNormalMat mDepthNormalMat;
- ShadowDepthCubeMat mDepthCubeMat;
- ShadowDepthDirectionalMat mDepthDirectionalMat;
- UINT32 mShadowMapSize;
- Vector<ShadowMapAtlas> mDynamicShadowMaps;
- Vector<ShadowCascadedMap> mCascadedShadowMaps;
- Vector<ShadowCubemap> mShadowCubemaps;
- Vector<ShadowInfo> mShadowInfos;
- Vector<LightShadows> mSpotLightShadows;
- Vector<LightShadows> mRadialLightShadows;
- Vector<UINT32> mDirectionalLightShadows;
- Vector<bool> mRenderableVisibility; // Transient
- Vector<ShadowMapOptions> mSpotLightShadowOptions; // Transient
- Vector<ShadowMapOptions> mRadialLightShadowOptions; // Transient
- };
- /* @} */
- }}
|