Browse Source

Fixed culling so it is consistent across all platforms (counterclockwise by default)
Added d3d9 builtin materials

Marko Pintera 12 years ago
parent
commit
acc4d601eb

+ 115 - 2
BansheeEngine/Source/BsD3D9BuiltinMaterialFactory.cpp

@@ -5,6 +5,7 @@
 #include "CmPass.h"
 #include "CmMaterial.h"
 #include "CmBlendState.h"
+#include "CmRendererManager.h"
 
 using namespace CamelotFramework;
 
@@ -42,11 +43,123 @@ namespace BansheeEngine
 
 	void D3D9BuiltinMaterialFactory::initSpriteTextShader()
 	{
-		// TODO
+		String vsCode = "										\
+			float halfViewportWidth;							\
+			float halfViewportHeight;							\
+			float4x4 worldTransform;							\
+																\
+			void vs_main(										\
+			in float3 inPos : POSITION,							\
+			in float2 uv : TEXCOORD0,							\
+			out float4 oPosition : POSITION,					\
+			out float2 oUv : TEXCOORD0)							\
+			{													\
+				float4 tfrmdPos = mul(worldTransform, float4(inPos.xy, 0, 1));	\
+																\
+				float tfrmdX = (tfrmdPos.x / halfViewportWidth) - 1.0f;			\
+				float tfrmdY = (tfrmdPos.y / halfViewportHeight) + 1.0f;		\
+																\
+				oPosition = float4(tfrmdX, tfrmdY, 0, 1);		\
+				oUv = uv;										\
+			}													\
+			";
+
+		String psCode = "																\
+			sampler2D mainTexture;														\
+																						\
+			float4 ps_main(float2 uv : TEXCOORD0) : COLOR0								\
+			{																			\
+				float4 color = float4(1.0f, 1.0f, 1.0f, tex2D(mainTexture, uv).r);		\
+				return color;															\
+			}";
+
+		HHighLevelGpuProgram vsProgram = HighLevelGpuProgram::create(vsCode, "vs_main", "hlsl", GPT_VERTEX_PROGRAM, GPP_VS_3_0);
+		HHighLevelGpuProgram psProgram = HighLevelGpuProgram::create(psCode, "ps_main", "hlsl", GPT_FRAGMENT_PROGRAM, GPP_PS_3_0);
+
+		vsProgram.waitUntilLoaded();
+		psProgram.waitUntilLoaded();
+
+		mSpriteTextShader = Shader::create("TextSpriteShader");
+
+		mSpriteTextShader->addParameter("worldTransform", "worldTransform", GPDT_MATRIX_4X4);
+		mSpriteTextShader->addParameter("halfViewportWidth", "halfViewportWidth", GPDT_FLOAT1);
+		mSpriteTextShader->addParameter("halfViewportHeight", "halfViewportHeight", GPDT_FLOAT1);
+		mSpriteTextShader->addParameter("mainTexSamp", "mainTexture", GPOT_SAMPLER2D);
+		mSpriteTextShader->addParameter("mainTexture", "mainTexture", GPOT_TEXTURE2D);
+
+		TechniquePtr newTechnique = mSpriteTextShader->addTechnique("D3D9RenderSystem", RendererManager::getCoreRendererName()); 
+		PassPtr newPass = newTechnique->addPass();
+		newPass->setVertexProgram(vsProgram);
+		newPass->setFragmentProgram(psProgram);
+
+		BLEND_STATE_DESC desc;
+		desc.renderTargetDesc[0].blendEnable = true;
+		desc.renderTargetDesc[0].srcBlend = BF_SOURCE_ALPHA;
+		desc.renderTargetDesc[0].dstBlend = BF_INV_SOURCE_ALPHA;
+		desc.renderTargetDesc[0].blendOp = BO_ADD;
+
+		HBlendState blendState = BlendState::create(desc);
+		newPass->setBlendState(blendState);
 	}
 
 	void D3D9BuiltinMaterialFactory::initSpriteImageShader()
 	{
-		// TODO
+		String vsCode = "										\
+						float halfViewportWidth;							\
+						float halfViewportHeight;							\
+						float4x4 worldTransform;							\
+						\
+						void vs_main(										\
+						in float3 inPos : POSITION,							\
+						in float2 uv : TEXCOORD0,							\
+						out float4 oPosition : POSITION,					\
+						out float2 oUv : TEXCOORD0)							\
+						{													\
+						float4 tfrmdPos = mul(worldTransform, float4(inPos.xy, 0, 1));	\
+						\
+						float tfrmdX = (tfrmdPos.x / halfViewportWidth) - 1.0f;			\
+						float tfrmdY = (tfrmdPos.y / halfViewportHeight) + 1.0f;		\
+						\
+						oPosition = float4(tfrmdX, tfrmdY, 0, 1);		\
+						oUv = uv;										\
+						}													\
+						";
+
+		String psCode = "												\
+						sampler2D mainTexture;							\
+						\
+						float4 ps_main(float2 uv : TEXCOORD0) : COLOR0	\
+						{												\
+						float4 color = tex2D(mainTexture, uv);			\
+						return color;									\
+						}";
+
+		HHighLevelGpuProgram vsProgram = HighLevelGpuProgram::create(vsCode, "vs_main", "hlsl", GPT_VERTEX_PROGRAM, GPP_VS_2_0);
+		HHighLevelGpuProgram psProgram = HighLevelGpuProgram::create(psCode, "ps_main", "hlsl", GPT_FRAGMENT_PROGRAM, GPP_PS_2_0);
+
+		vsProgram.waitUntilLoaded();
+		psProgram.waitUntilLoaded();
+
+		mSpriteImageShader = Shader::create("ImageSpriteShader");
+
+		mSpriteImageShader->addParameter("worldTransform", "worldTransform", GPDT_MATRIX_4X4);
+		mSpriteImageShader->addParameter("halfViewportWidth", "halfViewportWidth", GPDT_FLOAT1);
+		mSpriteImageShader->addParameter("halfViewportHeight", "halfViewportHeight", GPDT_FLOAT1);
+		mSpriteImageShader->addParameter("mainTexSamp", "mainTexture", GPOT_SAMPLER2D);
+		mSpriteImageShader->addParameter("mainTexture", "mainTexture", GPOT_TEXTURE2D);
+
+		TechniquePtr newTechnique = mSpriteImageShader->addTechnique("D3D9RenderSystem", RendererManager::getCoreRendererName()); 
+		PassPtr newPass = newTechnique->addPass();
+		newPass->setVertexProgram(vsProgram);
+		newPass->setFragmentProgram(psProgram);
+
+		BLEND_STATE_DESC desc;
+		desc.renderTargetDesc[0].blendEnable = true;
+		desc.renderTargetDesc[0].srcBlend = BF_SOURCE_ALPHA;
+		desc.renderTargetDesc[0].dstBlend = BF_INV_SOURCE_ALPHA;
+		desc.renderTargetDesc[0].blendOp = BO_ADD;
+
+		HBlendState blendState = BlendState::create(desc);
+		newPass->setBlendState(blendState);
 	}
 }

+ 2 - 2
CamelotClient/CamelotClient.cpp

@@ -28,8 +28,8 @@
 #include "CmTestTextSprite.h"
 #include "CmEditorWindow.h"
 
-#define DX11
-//#define DX9
+//#define DX11
+#define DX9
 //#define GL
 
 using namespace CamelotFramework;

+ 1 - 1
CamelotCore/Include/CmRasterizerState.h

@@ -10,7 +10,7 @@ namespace CamelotFramework
 	{
 		RASTERIZER_STATE_DESC()
 			: polygonMode(PM_SOLID)
-			, cullMode(CULL_CLOCKWISE)
+			, cullMode(CULL_COUNTERCLOCKWISE)
 			, depthBias(0)
 			, depthBiasClamp(0.0f)
 			, slopeScaledDepthBias(0.0f)

+ 1 - 1
CamelotCore/Source/CmRenderSystem.cpp

@@ -55,7 +55,7 @@ namespace CamelotFramework {
 
     //-----------------------------------------------------------------------
     RenderSystem::RenderSystem()
-        : mCullingMode(CULL_CLOCKWISE)
+        : mCullingMode(CULL_COUNTERCLOCKWISE)
         , mInvertVertexWinding(false)
         , mDisabledTexUnitsFrom(0)
         , mVertexProgramBound(false)

+ 1 - 1
CamelotD3D11RenderSystem/Include/CmD3D11Mappings.h

@@ -36,7 +36,7 @@ namespace CamelotFramework
 		/// return a D3D11 equivalent for an engine CompareFunction value
 		static D3D11_COMPARISON_FUNC get(CompareFunction cf);
 		/// return a D3D11 equivalent for an engine CillingMode value
-		static D3D11_CULL_MODE get(CullingMode cm, bool flip = false);
+		static D3D11_CULL_MODE get(CullingMode cm);
 		/// return a D3D11 equivalent for an engine PolygonMode value
 		static D3D11_FILL_MODE get(PolygonMode level);
 		/// return a D3D11 equivalent for an engine StencilOperation value

+ 3 - 9
CamelotD3D11RenderSystem/Source/CmD3D11Mappings.cpp

@@ -90,22 +90,16 @@ namespace CamelotFramework
 		return D3D11_COMPARISON_ALWAYS;
 	}
 
-	D3D11_CULL_MODE D3D11Mappings::get(CullingMode cm, bool flip)
+	D3D11_CULL_MODE D3D11Mappings::get(CullingMode cm)
 	{
 		switch( cm )
 		{
 		case CULL_NONE:
 			return D3D11_CULL_NONE;
 		case CULL_CLOCKWISE:
-			if( flip )
-				return D3D11_CULL_FRONT;
-			else
-				return D3D11_CULL_BACK;
+			return D3D11_CULL_FRONT;
 		case CULL_COUNTERCLOCKWISE:
-			if( flip )
-				return D3D11_CULL_BACK;
-			else
-				return D3D11_CULL_FRONT;
+			return D3D11_CULL_BACK;
 		}
 		return D3D11_CULL_NONE;
 	}

+ 1 - 0
CamelotD3D11RenderSystem/Source/CmD3D11RasterizerState.cpp

@@ -27,6 +27,7 @@ namespace CamelotFramework
 		rasterizerStateDesc.MultisampleEnable = mData.multisampleEnable;
 		rasterizerStateDesc.ScissorEnable = mData.scissorEnable;
 		rasterizerStateDesc.SlopeScaledDepthBias = mData.slopeScaledDepthBias;
+		rasterizerStateDesc.FrontCounterClockwise = false;
 
 		D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
 		D3D11Device& device = rs->getPrimaryDevice();

+ 6 - 0
CamelotGLRenderer/Source/CmGLPixelFormat.cpp

@@ -52,7 +52,9 @@ namespace CamelotFramework  {
             case PF_A8B8G8R8:
                 return GL_RGBA;
             case PF_B8G8R8A8:
+			case PF_B8G8R8X8:
                 return GL_BGRA;
+			case PF_R8G8B8X8:
 			case PF_R8G8B8A8:
 				return GL_RGBA;
 			case PF_FLOAT16_R:
@@ -98,8 +100,10 @@ namespace CamelotFramework  {
             case PF_A8R8G8B8:
 				return GL_UNSIGNED_BYTE;
             case PF_B8G8R8A8:
+			case PF_B8G8R8X8:
                 return GL_UNSIGNED_INT_8_8_8_8;
 			case PF_R8G8B8A8:
+			case PF_R8G8B8X8:
 				return GL_UNSIGNED_INT_8_8_8_8;
 			case PF_FLOAT16_R:
 			case PF_FLOAT16_RG:
@@ -152,6 +156,8 @@ namespace CamelotFramework  {
             case PF_B8G8R8:
 			case PF_X8B8G8R8:
 			case PF_X8R8G8B8:
+			case PF_B8G8R8X8:
+			case PF_R8G8B8X8:
 				if (hwGamma)
 					return GL_SRGB8;
 				else