//********************************** Banshee Engine (www.banshee3d.com) **************************************************// //**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************// #pragma once #include "BsCorePrerequisites.h" #include "BsModule.h" #include "BsBlendState.h" #include "BsRasterizerState.h" #include "BsDepthStencilState.h" #include "BsSamplerState.h" namespace BansheeEngine { /** @addtogroup RenderAPI-Internal * @{ */ /** Handles creation of various render states. */ class BS_CORE_EXPORT RenderStateManager : public Module { public: /** Creates and initializes a new SamplerState. */ SPtr createSamplerState(const SAMPLER_STATE_DESC& desc) const; /** Creates and initializes a new DepthStencilState. */ SPtr createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const; /** Creates and initializes a new RasterizerState. */ SPtr createRasterizerState(const RASTERIZER_STATE_DESC& desc) const; /** Creates and initializes a new BlendState. */ SPtr createBlendState(const BLEND_STATE_DESC& desc) const; /** Creates an uninitialized sampler state. Requires manual initialization after creation. */ SPtr _createSamplerStatePtr(const SAMPLER_STATE_DESC& desc) const; /** Creates an uninitialized depth-stencil state. Requires manual initialization after creation. */ SPtr _createDepthStencilStatePtr(const DEPTH_STENCIL_STATE_DESC& desc) const; /** Creates an uninitialized rasterizer state. Requires manual initialization after creation. */ SPtr _createRasterizerStatePtr(const RASTERIZER_STATE_DESC& desc) const; /** Creates an uninitialized blend state. Requires manual initialization after creation. */ SPtr _createBlendStatePtr(const BLEND_STATE_DESC& desc) const; /** Gets a sampler state initialized with default options. */ const SPtr& getDefaultSamplerState() const; /** Gets a blend state initialized with default options. */ const SPtr& getDefaultBlendState() const; /** Gets a rasterizer state initialized with default options. */ const SPtr& getDefaultRasterizerState() const; /** Gets a depth stencil state initialized with default options. */ const SPtr& getDefaultDepthStencilState() const; private: friend class SamplerState; friend class BlendState; friend class RasterizerState; friend class DepthStencilState; mutable SPtr mDefaultSamplerState; mutable SPtr mDefaultBlendState; mutable SPtr mDefaultRasterizerState; mutable SPtr mDefaultDepthStencilState; }; /** Handles creation of various render states. */ class BS_CORE_EXPORT RenderStateCoreManager : public Module { private: /** Contains data about a cached blend state. */ struct CachedBlendState { CachedBlendState() :id(0) { } CachedBlendState(UINT32 id) :id(id) { } std::weak_ptr state; UINT32 id; }; /** Contains data about a cached rasterizer state. */ struct CachedRasterizerState { CachedRasterizerState() :id(0) { } CachedRasterizerState(UINT32 id) :id(id) { } std::weak_ptr state; UINT32 id; }; /** Contains data about a cached depth stencil state. */ struct CachedDepthStencilState { CachedDepthStencilState() :id(0) { } CachedDepthStencilState(UINT32 id) :id(id) { } std::weak_ptr state; UINT32 id; }; public: RenderStateCoreManager(); /** @copydoc RenderStateManager::createSamplerState */ SPtr createSamplerState(const SAMPLER_STATE_DESC& desc) const; /** @copydoc RenderStateManager::createDepthStencilState */ SPtr createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const; /** @copydoc RenderStateManager::createRasterizerState */ SPtr createRasterizerState(const RASTERIZER_STATE_DESC& desc) const; /** @copydoc RenderStateManager::createBlendState */ SPtr createBlendState(const BLEND_STATE_DESC& desc) const; /** Creates an uninitialized sampler state. Requires manual initialization after creation. */ SPtr _createSamplerState(const SAMPLER_STATE_DESC& desc) const; /** Creates an uninitialized depth-stencil state. Requires manual initialization after creation. */ SPtr _createDepthStencilState(const DEPTH_STENCIL_STATE_DESC& desc) const; /** Creates an uninitialized rasterizer state. Requires manual initialization after creation. */ SPtr _createRasterizerState(const RASTERIZER_STATE_DESC& desc) const; /** Creates an uninitialized blend state. Requires manual initialization after creation. */ SPtr _createBlendState(const BLEND_STATE_DESC& desc) const; /** Gets a sampler state initialized with default options. */ const SPtr& getDefaultSamplerState() const; /** Gets a blend state initialized with default options. */ const SPtr& getDefaultBlendState() const; /** Gets a rasterizer state initialized with default options. */ const SPtr& getDefaultRasterizerState() const; /** Gets a depth stencil state initialized with default options. */ const SPtr& getDefaultDepthStencilState() const; protected: friend class SamplerState; friend class BlendState; friend class RasterizerState; friend class DepthStencilState; friend class SamplerStateCore; friend class BlendStateCore; friend class RasterizerStateCore; friend class DepthStencilStateCore; /** @copydoc Module::onShutDown */ void onShutDown() override; /** @copydoc createSamplerState */ virtual SPtr createSamplerStateInternal(const SAMPLER_STATE_DESC& desc) const; /** @copydoc createBlendState */ virtual SPtr createBlendStateInternal(const BLEND_STATE_DESC& desc, UINT32 id) const; /** @copydoc createRasterizerState */ virtual SPtr createRasterizerStateInternal(const RASTERIZER_STATE_DESC& desc, UINT32 id) const; /** @copydoc createDepthStencilState */ virtual SPtr 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& 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 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 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 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 findCachedState(const DEPTH_STENCIL_STATE_DESC& desc, UINT32& id) const; mutable SPtr mDefaultSamplerState; mutable SPtr mDefaultBlendState; mutable SPtr mDefaultRasterizerState; mutable SPtr mDefaultDepthStencilState; mutable UnorderedMap> mCachedSamplerStates; mutable UnorderedMap mCachedBlendStates; mutable UnorderedMap mCachedRasterizerStates; mutable UnorderedMap mCachedDepthStencilStates; mutable UINT32 mNextBlendStateId; mutable UINT32 mNextRasterizerStateId; mutable UINT32 mNextDepthStencilStateId; mutable Mutex mMutex; }; /** @} */ }