| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #pragma once
- #include "BsCorePrerequisites.h"
- #include "BsModule.h"
- #include "BsBlendState.h"
- #include "BsRasterizerState.h"
- #include "BsDepthStencilState.h"
- #include "BsSamplerState.h"
- #include "BsGpuPipelineState.h"
- #include "BsGpuPipelineParamInfo.h"
- namespace bs
- {
- /** @addtogroup RenderAPI-Internal
- * @{
- */
- /** Handles creation of various render states. */
- class BS_CORE_EXPORT RenderStateManager : public Module <RenderStateManager>
- {
- public:
- /**
- * Creates and initializes a new SamplerState.
- *
- * @param[in] desc Object describing the sampler state to create.
- */
- SPtr<SamplerState> createSamplerState(const SAMPLER_STATE_DESC& desc) const;
- /** Creates and initializes a new DepthStencilState. */
- SPtr<DepthStencilState> createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const;
- /** Creates and initializes a new RasterizerState. */
- SPtr<RasterizerState> createRasterizerState(const RASTERIZER_STATE_DESC& desc) const;
- /** Creates and initializes a new BlendState. */
- SPtr<BlendState> createBlendState(const BLEND_STATE_DESC& desc) const;
- /**
- * Creates and initializes a new GraphicsPipelineState.
- *
- * @param[in] desc Object describing the pipeline to create.
- */
- SPtr<GraphicsPipelineState> createGraphicsPipelineState(const PIPELINE_STATE_DESC& desc) const;
- /**
- * Creates and initializes a new ComputePipelineState.
- *
- * @param[in] program Compute GPU program to be executed by the pipeline.
- */
- SPtr<ComputePipelineState> createComputePipelineState(const SPtr<GpuProgram>& program) const;
- /** Creates an uninitialized sampler state. Requires manual initialization after creation. */
- SPtr<SamplerState> _createSamplerStatePtr(const SAMPLER_STATE_DESC& desc) const;
- /** Creates an uninitialized depth-stencil state. Requires manual initialization after creation. */
- SPtr<DepthStencilState> _createDepthStencilStatePtr(const DEPTH_STENCIL_STATE_DESC& desc) const;
- /** Creates an uninitialized rasterizer state. Requires manual initialization after creation. */
- SPtr<RasterizerState> _createRasterizerStatePtr(const RASTERIZER_STATE_DESC& desc) const;
- /** Creates an uninitialized blend state. Requires manual initialization after creation. */
- SPtr<BlendState> _createBlendStatePtr(const BLEND_STATE_DESC& desc) const;
- /** Creates an uninitialized GraphicsPipelineState. Requires manual initialization after creation. */
- virtual SPtr<GraphicsPipelineState> _createGraphicsPipelineState(const PIPELINE_STATE_DESC& desc) const;
- /** Creates an uninitialized ComputePipelineState. Requires manual initialization after creation. */
- virtual SPtr<ComputePipelineState> _createComputePipelineState(const SPtr<GpuProgram>& program) const;
- /** Gets a sampler state initialized with default options. */
- const SPtr<SamplerState>& getDefaultSamplerState() const;
- /** Gets a blend state initialized with default options. */
- const SPtr<BlendState>& getDefaultBlendState() const;
- /** Gets a rasterizer state initialized with default options. */
- const SPtr<RasterizerState>& getDefaultRasterizerState() const;
- /** Gets a depth stencil state initialized with default options. */
- const SPtr<DepthStencilState>& getDefaultDepthStencilState() const;
- private:
- friend class SamplerState;
- friend class BlendState;
- friend class RasterizerState;
- friend class DepthStencilState;
- mutable SPtr<SamplerState> mDefaultSamplerState;
- mutable SPtr<BlendState> mDefaultBlendState;
- mutable SPtr<RasterizerState> mDefaultRasterizerState;
- mutable SPtr<DepthStencilState> mDefaultDepthStencilState;
- };
- namespace ct
- {
- /** Handles creation of various render states. */
- class BS_CORE_EXPORT RenderStateManager : public Module<RenderStateManager>
- {
- private:
- /** Contains data about a cached blend state. */
- struct CachedBlendState
- {
- CachedBlendState()
- :id(0)
- { }
- CachedBlendState(UINT32 id)
- :id(id)
- { }
- std::weak_ptr<BlendState> state;
- UINT32 id;
- };
- /** Contains data about a cached rasterizer state. */
- struct CachedRasterizerState
- {
- CachedRasterizerState()
- :id(0)
- { }
- CachedRasterizerState(UINT32 id)
- :id(id)
- { }
- std::weak_ptr<RasterizerState> state;
- UINT32 id;
- };
- /** Contains data about a cached depth stencil state. */
- struct CachedDepthStencilState
- {
- CachedDepthStencilState()
- :id(0)
- { }
- CachedDepthStencilState(UINT32 id)
- :id(id)
- { }
- std::weak_ptr<DepthStencilState> state;
- UINT32 id;
- };
- public:
- RenderStateManager();
- /**
- * @copydoc bs::RenderStateManager::createSamplerState
- * @param[in] deviceMask Mask that determines on which GPU devices should the object be created on.
- */
- SPtr<SamplerState> createSamplerState(const SAMPLER_STATE_DESC& desc,
- GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
- /** @copydoc bs::RenderStateManager::createDepthStencilState */
- SPtr<DepthStencilState> createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const;
- /** @copydoc bs::RenderStateManager::createRasterizerState */
- SPtr<RasterizerState> createRasterizerState(const RASTERIZER_STATE_DESC& desc) const;
- /** @copydoc bs::RenderStateManager::createBlendState */
- SPtr<BlendState> createBlendState(const BLEND_STATE_DESC& desc) const;
- /**
- * @copydoc bs::RenderStateManager::createGraphicsPipelineState
- * @param[in] deviceMask Mask that determines on which GPU devices should the object be created on.
- */
- SPtr<GraphicsPipelineState> createGraphicsPipelineState(const PIPELINE_STATE_DESC& desc,
- GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
- /**
- * @copydoc bs::RenderStateManager::createComputePipelineState
- * @param[in] deviceMask Mask that determines on which GPU devices should the object be created on.
- */
- SPtr<ComputePipelineState> createComputePipelineState(const SPtr<GpuProgram>& program,
- GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
- /** @copydoc GpuPipelineParamInfo::create */
- SPtr<GpuPipelineParamInfo> createPipelineParamInfo(const GPU_PIPELINE_PARAMS_DESC& desc,
- GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
- /** Creates an uninitialized sampler state. Requires manual initialization after creation. */
- SPtr<SamplerState> _createSamplerState(const SAMPLER_STATE_DESC& desc,
- GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
- /** Creates an uninitialized depth-stencil state. Requires manual initialization after creation. */
- SPtr<DepthStencilState> _createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const;
- /** Creates an uninitialized rasterizer state. Requires manual initialization after creation. */
- SPtr<RasterizerState> _createRasterizerState(const RASTERIZER_STATE_DESC& desc) const;
- /** Creates an uninitialized blend state. Requires manual initialization after creation. */
- SPtr<BlendState> _createBlendState(const BLEND_STATE_DESC& desc) const;
- /** Creates an uninitialized GraphicsPipelineState. Requires manual initialization after creation. */
- virtual SPtr<GraphicsPipelineState> _createGraphicsPipelineState(const PIPELINE_STATE_DESC& desc,
- GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
- /** Creates an uninitialized ComputePipelineState. Requires manual initialization after creation. */
- virtual SPtr<ComputePipelineState> _createComputePipelineState(const SPtr<GpuProgram>& program,
- GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
- /** Creates an uninitialized GpuPipelineParamInfo. Requires manual initialization after creation. */
- virtual SPtr<GpuPipelineParamInfo> _createPipelineParamInfo(const GPU_PIPELINE_PARAMS_DESC& desc,
- GpuDeviceFlags deviceMask = GDF_DEFAULT) const;
- /** Gets a sampler state initialized with default options. */
- const SPtr<SamplerState>& getDefaultSamplerState() const;
- /** Gets a blend state initialized with default options. */
- const SPtr<BlendState>& getDefaultBlendState() const;
- /** Gets a rasterizer state initialized with default options. */
- const SPtr<RasterizerState>& getDefaultRasterizerState() const;
- /** Gets a depth stencil state initialized with default options. */
- const SPtr<DepthStencilState>& getDefaultDepthStencilState() const;
- protected:
- friend class bs::SamplerState;
- friend class bs::BlendState;
- friend class bs::RasterizerState;
- friend class bs::DepthStencilState;
- friend class SamplerState;
- friend class BlendState;
- friend class RasterizerState;
- friend class DepthStencilState;
- /** @copydoc Module::onShutDown */
- void onShutDown() override;
- /** @copydoc createSamplerState */
- virtual SPtr<SamplerState> createSamplerStateInternal(const SAMPLER_STATE_DESC& desc, GpuDeviceFlags deviceMask) const;
- /** @copydoc createBlendState */
- virtual SPtr<BlendState> createBlendStateInternal(const BLEND_STATE_DESC& desc, UINT32 id) const;
- /** @copydoc createRasterizerState */
- virtual SPtr<RasterizerState> createRasterizerStateInternal(const RASTERIZER_STATE_DESC& desc, UINT32 id) const;
- /** @copydoc createDepthStencilState */
- virtual SPtr<DepthStencilState> createDepthStencilStateInternal(const DEPTH_STENCIL_STATE_DESC& desc, UINT32 id) const;
- private:
- /** Triggered when a new sampler state is created. */
- void notifySamplerStateCreated(const SAMPLER_STATE_DESC& desc, const SPtr<SamplerState>& state) const;
- /** Triggered when a new sampler state is created. */
- void notifyBlendStateCreated(const BLEND_STATE_DESC& desc, const CachedBlendState& state) const;
- /** Triggered when a new sampler state is created. */
- void notifyRasterizerStateCreated(const RASTERIZER_STATE_DESC& desc, const CachedRasterizerState& state) const;
- /** Triggered when a new sampler state is created. */
- void notifyDepthStencilStateCreated(const DEPTH_STENCIL_STATE_DESC& desc, const CachedDepthStencilState& state) const;
- /**
- * Triggered when the last reference to a specific sampler state is destroyed, which means we must clear our cached
- * version as well.
- */
- void notifySamplerStateDestroyed(const SAMPLER_STATE_DESC& desc) const;
- /**
- * Attempts to find a cached sampler state corresponding to the provided descriptor. Returns null if one doesn't
- * exist.
- */
- SPtr<SamplerState> findCachedState(const SAMPLER_STATE_DESC& desc) const;
- /**
- * Attempts to find a cached blend state corresponding to the provided descriptor. Returns null if one doesn't exist.
- */
- SPtr<BlendState> findCachedState(const BLEND_STATE_DESC& desc, UINT32& id) const;
- /**
- * Attempts to find a cached rasterizer state corresponding to the provided descriptor. Returns null if one doesn't
- * exist.
- */
- SPtr<RasterizerState> findCachedState(const RASTERIZER_STATE_DESC& desc, UINT32& id) const;
- /**
- * Attempts to find a cached depth-stencil state corresponding to the provided descriptor. Returns null if one
- * doesn't exist.
- */
- SPtr<DepthStencilState> findCachedState(const DEPTH_STENCIL_STATE_DESC& desc, UINT32& id) const;
- mutable SPtr<SamplerState> mDefaultSamplerState;
- mutable SPtr<BlendState> mDefaultBlendState;
- mutable SPtr<RasterizerState> mDefaultRasterizerState;
- mutable SPtr<DepthStencilState> mDefaultDepthStencilState;
- mutable UnorderedMap<SAMPLER_STATE_DESC, std::weak_ptr<SamplerState>> mCachedSamplerStates;
- mutable UnorderedMap<BLEND_STATE_DESC, CachedBlendState> mCachedBlendStates;
- mutable UnorderedMap<RASTERIZER_STATE_DESC, CachedRasterizerState> mCachedRasterizerStates;
- mutable UnorderedMap<DEPTH_STENCIL_STATE_DESC, CachedDepthStencilState> mCachedDepthStencilStates;
- mutable UINT32 mNextBlendStateId;
- mutable UINT32 mNextRasterizerStateId;
- mutable UINT32 mNextDepthStencilStateId;
- mutable Mutex mMutex;
- };
- }
- /** @} */
- }
|