Explorar o código

Added Blend and Rasterizer states

Marko Pintera %!s(int64=13) %!d(string=hai) anos
pai
achega
b41cfc7313

+ 4 - 0
CamelotRenderer/CamelotRenderer.vcxproj

@@ -214,6 +214,7 @@
     <ClInclude Include="Include\CmPass.h" />
     <ClInclude Include="Include\CmPassRTTI.h" />
     <ClInclude Include="Include\CmPrerequisites.h" />
+    <ClInclude Include="Include\CmRasterizerState.h" />
     <ClInclude Include="Include\CmRenderable.h" />
     <ClInclude Include="Include\CmRenderableRTTI.h" />
     <ClInclude Include="Include\CmRenderer.h" />
@@ -249,6 +250,7 @@
     <ClInclude Include="Include\CmGameObject.h" />
     <ClInclude Include="Include\CmComponent.h" />
     <ClInclude Include="Include\CmShader.h" />
+    <ClInclude Include="Include\CmBlendState.h" />
     <ClInclude Include="Include\stdafx.h" />
     <ClInclude Include="Include\targetver.h" />
     <ClInclude Include="Include\CmVertexDeclarationRTTI.h" />
@@ -259,6 +261,7 @@
   <ItemGroup>
     <ClCompile Include="Source\CamelotRenderer.cpp" />
     <ClCompile Include="Source\CmApplication.cpp" />
+    <ClCompile Include="Source\CmBlendState.cpp" />
     <ClCompile Include="Source\CmCamera.cpp" />
     <ClCompile Include="Source\CmCgProgram.cpp" />
     <ClCompile Include="Source\CmCgProgramFactory.cpp" />
@@ -284,6 +287,7 @@
     <ClCompile Include="Source\CmMesh.cpp" />
     <ClCompile Include="Source\CmMeshData.cpp" />
     <ClCompile Include="Source\CmPass.cpp" />
+    <ClCompile Include="Source\CmRasterizerState.cpp" />
     <ClCompile Include="Source\CmRenderable.cpp" />
     <ClCompile Include="Source\CmRendererManager.cpp" />
     <ClCompile Include="Source\CmRenderSystem.cpp" />

+ 12 - 0
CamelotRenderer/CamelotRenderer.vcxproj.filters

@@ -326,6 +326,12 @@
     <ClInclude Include="Include\CmSamplerStateRTTI.h">
       <Filter>Header Files\RTTI</Filter>
     </ClInclude>
+    <ClInclude Include="Include\CmRasterizerState.h">
+      <Filter>Header Files\RenderSystem</Filter>
+    </ClInclude>
+    <ClInclude Include="Include\CmBlendState.h">
+      <Filter>Header Files\RenderSystem</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\CamelotRenderer.cpp">
@@ -487,5 +493,11 @@
     <ClCompile Include="Source\CmRenderStateManager.cpp">
       <Filter>Source Files\RenderSystem</Filter>
     </ClCompile>
+    <ClCompile Include="Source\CmRasterizerState.cpp">
+      <Filter>Source Files\RenderSystem</Filter>
+    </ClCompile>
+    <ClCompile Include="Source\CmBlendState.cpp">
+      <Filter>Source Files\RenderSystem</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 52 - 0
CamelotRenderer/Include/CmBlendState.h

@@ -0,0 +1,52 @@
+#pragma once
+
+#include "CmPrerequisites.h"
+#include "CmCommon.h"
+
+namespace CamelotEngine
+{
+	struct CM_EXPORT RENDER_TARGET_BLEND_STATE_DESC
+	{
+		bool blendEnable;
+		SceneBlendFactor srcBlend;
+		SceneBlendFactor dstBlend;
+		SceneBlendOperation blendOp;
+		SceneBlendFactor srcBlendAlpha;
+		SceneBlendFactor dstBlendAlpha;
+		SceneBlendOperation blendOpAlpha;
+		UINT8 renderTargetWriteMask;
+	};
+
+	struct CM_EXPORT BLEND_STATE_DESC
+	{
+		bool alphaToCoverageEnable;
+		bool independantBlendEnable;
+		RENDER_TARGET_BLEND_STATE_DESC renderTargetDesc[CM_MAX_MULTIPLE_RENDER_TARGETS];
+	};
+
+	class CM_EXPORT BlendState
+	{
+	public:
+		virtual ~BlendState() {}
+
+		bool getAlphaToCoverageEnabled() const { return mData.alphaToCoverageEnable; }
+		bool getIndependantBlendEnable() const { return mData.independantBlendEnable; }
+
+		bool getBlendEnable(UINT32 renderTargetIdx) const;
+		SceneBlendFactor getSrcBlend(UINT32 renderTargetIdx) const;
+		SceneBlendFactor getDstBlend(UINT32 renderTargetIdx) const;
+		SceneBlendOperation getBlendOperation(UINT32 renderTargetIdx) const;
+		SceneBlendFactor getAlphaSrcBlend(UINT32 renderTargetIdx) const;
+		SceneBlendFactor getAlphaDstBlend(UINT32 renderTargetIdx) const;
+		SceneBlendOperation getAlphaBlendOperation(UINT32 renderTargetIdx) const;
+		UINT8 getRenderTargetWriteMask(UINT32 renderTargetIdx) const;
+
+		static BlendStatePtr create(const BLEND_STATE_DESC& desc);
+
+	private:
+		friend class RenderStateManager;
+
+		virtual void initialize(const BLEND_STATE_DESC& desc);
+		BLEND_STATE_DESC mData;
+	};
+}

+ 3 - 1
CamelotRenderer/Include/CmDepthStencilState.h

@@ -5,7 +5,7 @@
 
 namespace CamelotEngine
 {
-	struct DEPTH_STENCIL_DESC
+	struct CM_EXPORT DEPTH_STENCIL_DESC
 	{
 		DEPTH_STENCIL_DESC()
 			: depthReadEnable(true)
@@ -46,6 +46,8 @@ namespace CamelotEngine
 	class CM_EXPORT DepthStencilState
 	{
 	public:
+		virtual ~DepthStencilState() {}
+
 		bool getDepthReadEnable() const { return mData.depthReadEnable; }
 		bool getDepthWriteEnable() const { return mData.depthWriteEnable; }
 		CompareFunction getDepthComparisonFunc() const { return mData.depthComparisonFunc; }

+ 7 - 0
CamelotRenderer/Include/CmPrerequisites.h

@@ -112,6 +112,8 @@ namespace CamelotEngine {
 	class DeferredRenderContext;
 	class DepthStencilState;
 	class RenderStateManager;
+	class RasterizerState;
+	class BlendState;
 	// Asset import
 	class SpecificImporter;
 	class Importer;
@@ -129,6 +131,9 @@ namespace CamelotEngine {
 	// Desc structs
 	struct SAMPLER_STATE_DESC;
 	struct DEPTH_STENCIL_DESC;
+	struct RASTERIZER_STATE_DESC;
+	struct BLEND_STATE_DESC;
+	struct RENDER_TARGET_BLEND_STATE_DESC;
 }
 
 /* Shared pointer typedefs*/
@@ -158,6 +163,8 @@ namespace CamelotEngine
 	typedef std::shared_ptr<DeferredRenderContext> DeferredRenderContextPtr;
 	typedef std::shared_ptr<SamplerState> SamplerStatePtr;
 	typedef std::shared_ptr<DepthStencilState> DepthStencilStatePtr;
+	typedef std::shared_ptr<RasterizerState> RasterizerStatePtr;
+	typedef std::shared_ptr<BlendState> BlendStatePtr;
 }
 
 // All type IDs

+ 60 - 0
CamelotRenderer/Include/CmRasterizerState.h

@@ -0,0 +1,60 @@
+#pragma once
+
+#include "CmPrerequisites.h"
+#include "CmCommon.h"
+
+namespace CamelotEngine
+{
+	struct CM_EXPORT RASTERIZER_STATE_DESC
+	{
+		RASTERIZER_STATE_DESC()
+			: polygonMode(PM_SOLID)
+			, cullMode(CULL_CLOCKWISE)
+			, depthBias(0)
+			, depthBiasClamp(0.0f)
+			, slopeScaledDepthBias(0.0f)
+			, depthClipEnable(true)
+			, scissorEnable(false)
+			, multisampleEnable(false)
+			, antialiasedLineEnable(false)
+		{ }
+
+		PolygonMode polygonMode;
+		CullingMode cullMode;
+
+		int depthBias;
+		float depthBiasClamp;
+		float slopeScaledDepthBias;
+
+		bool depthClipEnable;
+		bool scissorEnable;
+		bool multisampleEnable;
+		bool antialiasedLineEnable;
+	};
+
+	class CM_EXPORT RasterizerState
+	{
+	public:
+		virtual ~RasterizerState() {}
+
+		PolygonMode getPolygonMode() const { return mData.polygonMode; }
+		CullingMode getCullMode() const { return mData.cullMode; }
+
+		int getDepthBias() const { return mData.depthBias; }
+		float getDepthBiasClamp() const { return mData.depthBiasClamp; }
+		float getSlopeScaledDepthBias() const { return mData.slopeScaledDepthBias; }
+
+		bool getDepthClipEnable() const { return mData.depthClipEnable; }
+		bool getScissorEnable() const { return mData.scissorEnable; }
+		bool getMultisampleEnable() const { return mData.multisampleEnable; }
+		bool getAntialiasedLineEnable() const { return mData.antialiasedLineEnable; }
+
+		static RasterizerStatePtr create(const RASTERIZER_STATE_DESC& desc);
+
+	private:
+		friend class RenderStateManager;
+
+		virtual void initialize(const RASTERIZER_STATE_DESC& desc);
+		RASTERIZER_STATE_DESC mData;
+	};
+}

+ 24 - 0
CamelotRenderer/Include/CmRenderStateManager.h

@@ -18,6 +18,16 @@ namespace CamelotEngine
 		 */
 		virtual DepthStencilStatePtr createDepthStencilState(const DEPTH_STENCIL_DESC& desc) const;
 
+		/**
+		 * @brief	Creates and initializes a new RasterizerState.
+		 */
+		virtual RasterizerStatePtr createRasterizerState(const RASTERIZER_STATE_DESC& desc) const;
+
+		/**
+		 * @brief	Creates and initializes a new BlendState.
+		 */
+		virtual BlendStatePtr createBlendState(const BLEND_STATE_DESC& desc) const;
+
 		/**
 		 * @brief	Creates a completely empty and uninitialized SamplerState.
 		 * 			Should only be used for VERY specific purposes, like deserialization,
@@ -31,5 +41,19 @@ namespace CamelotEngine
 		 * 			as it requires additional manual initialization that is not required normally.
 		 */
 		virtual DepthStencilStatePtr createEmptyDepthStencilState() const;
+
+		/**
+		 * @brief	Creates a completely empty and uninitialized RasterizerState.
+		 * 			Should only be used for VERY specific purposes, like deserialization,
+		 * 			as it requires additional manual initialization that is not required normally.
+		 */
+		virtual RasterizerStatePtr createEmptyRasterizerState() const;
+
+		/**
+		 * @brief	Creates a completely empty and uninitialized BlendState.
+		 * 			Should only be used for VERY specific purposes, like deserialization,
+		 * 			as it requires additional manual initialization that is not required normally.
+		 */
+		virtual BlendStatePtr createEmptyBlendState() const;
 	};
 }

+ 3 - 1
CamelotRenderer/Include/CmSamplerState.h

@@ -11,7 +11,7 @@
 
 namespace CamelotEngine 
 {
-	struct SAMPLER_STATE_DESC
+	struct CM_EXPORT SAMPLER_STATE_DESC
 	{
 		SAMPLER_STATE_DESC()
 			: minFilter(FO_LINEAR)
@@ -52,6 +52,8 @@ namespace CamelotEngine
 	class CM_EXPORT SamplerState : public IReflectable
     {
     public:
+		virtual ~SamplerState() {}
+
 		static SamplerState DEFAULT;
 
         /** Gets the texture addressing mode for a given coordinate, 

+ 71 - 0
CamelotRenderer/Source/CmBlendState.cpp

@@ -0,0 +1,71 @@
+#include "CmBlendState.h"
+#include "CmRenderStateManager.h"
+
+namespace CamelotEngine
+{
+	void BlendState::initialize(const BLEND_STATE_DESC& desc)
+	{
+		mData = desc;
+	}
+
+	bool BlendState::getBlendEnable(UINT32 renderTargetIdx) const
+	{
+		assert(renderTargetIdx >= 0 && renderTargetIdx < CM_MAX_MULTIPLE_RENDER_TARGETS);
+
+		return mData.renderTargetDesc[renderTargetIdx].blendEnable;
+	}
+
+	SceneBlendFactor BlendState::getSrcBlend(UINT32 renderTargetIdx) const
+	{
+		assert(renderTargetIdx >= 0 && renderTargetIdx < CM_MAX_MULTIPLE_RENDER_TARGETS);
+
+		return mData.renderTargetDesc[renderTargetIdx].srcBlend;
+	}
+
+	SceneBlendFactor BlendState::getDstBlend(UINT32 renderTargetIdx) const
+	{
+		assert(renderTargetIdx >= 0 && renderTargetIdx < CM_MAX_MULTIPLE_RENDER_TARGETS);
+
+		return mData.renderTargetDesc[renderTargetIdx].dstBlend;
+	}
+
+	SceneBlendOperation BlendState::getBlendOperation(UINT32 renderTargetIdx) const
+	{
+		assert(renderTargetIdx >= 0 && renderTargetIdx < CM_MAX_MULTIPLE_RENDER_TARGETS);
+
+		return mData.renderTargetDesc[renderTargetIdx].blendOp;
+	}
+
+	SceneBlendFactor BlendState::getAlphaSrcBlend(UINT32 renderTargetIdx) const
+	{
+		assert(renderTargetIdx >= 0 && renderTargetIdx < CM_MAX_MULTIPLE_RENDER_TARGETS);
+
+		return mData.renderTargetDesc[renderTargetIdx].srcBlendAlpha;
+	}
+
+	SceneBlendFactor BlendState::getAlphaDstBlend(UINT32 renderTargetIdx) const
+	{
+		assert(renderTargetIdx >= 0 && renderTargetIdx < CM_MAX_MULTIPLE_RENDER_TARGETS);
+
+		return mData.renderTargetDesc[renderTargetIdx].dstBlendAlpha;
+	}
+
+	SceneBlendOperation BlendState::getAlphaBlendOperation(UINT32 renderTargetIdx) const
+	{
+		assert(renderTargetIdx >= 0 && renderTargetIdx < CM_MAX_MULTIPLE_RENDER_TARGETS);
+
+		return mData.renderTargetDesc[renderTargetIdx].blendOpAlpha;
+	}
+
+	UINT8 BlendState::getRenderTargetWriteMask(UINT32 renderTargetIdx) const
+	{
+		assert(renderTargetIdx >= 0 && renderTargetIdx < CM_MAX_MULTIPLE_RENDER_TARGETS);
+
+		return mData.renderTargetDesc[renderTargetIdx].renderTargetWriteMask;
+	}
+
+	BlendStatePtr BlendState::create(const BLEND_STATE_DESC& desc)
+	{
+		return RenderStateManager::instance().createBlendState(desc);
+	}
+}

+ 15 - 0
CamelotRenderer/Source/CmRasterizerState.cpp

@@ -0,0 +1,15 @@
+#include "CmRasterizerState.h"
+#include "CmRenderStateManager.h"
+
+namespace CamelotEngine
+{
+	void RasterizerState::initialize(const RASTERIZER_STATE_DESC& desc)
+	{
+		mData = desc;
+	}
+
+	RasterizerStatePtr RasterizerState::create(const RASTERIZER_STATE_DESC& desc)
+	{
+		return RenderStateManager::instance().createRasterizerState(desc);
+	}
+}

+ 28 - 0
CamelotRenderer/Source/CmRenderStateManager.cpp

@@ -1,6 +1,8 @@
 #include "CmRenderStateManager.h"
 #include "CmSamplerState.h"
 #include "CmDepthStencilState.h"
+#include "CmRasterizerState.h"
+#include "CmBlendState.h"
 
 namespace CamelotEngine
 {
@@ -20,6 +22,22 @@ namespace CamelotEngine
 		return depthStencilState;
 	}
 
+	RasterizerStatePtr RenderStateManager::createRasterizerState(const RASTERIZER_STATE_DESC& desc) const
+	{
+		RasterizerStatePtr rasterizerState = RasterizerStatePtr(new RasterizerState());
+		rasterizerState->initialize(desc);
+
+		return rasterizerState;
+	}
+
+	BlendStatePtr RenderStateManager::createBlendState(const BLEND_STATE_DESC& desc) const
+	{
+		BlendStatePtr blendState = BlendStatePtr(new BlendState());
+		blendState->initialize(desc);
+
+		return blendState;
+	}
+
 	SamplerStatePtr RenderStateManager::createEmptySamplerState() const
 	{
 		return SamplerStatePtr(new SamplerState());
@@ -29,4 +47,14 @@ namespace CamelotEngine
 	{
 		return DepthStencilStatePtr(new DepthStencilState());
 	}
+
+	RasterizerStatePtr RenderStateManager::createEmptyRasterizerState() const
+	{
+		return RasterizerStatePtr(new RasterizerState());
+	}
+
+	BlendStatePtr RenderStateManager::createEmptyBlendState() const
+	{
+		return BlendStatePtr(new BlendState());
+	}
 }