Browse Source

added DX9 and GL debug draw shaders

Marko Pintera 12 years ago
parent
commit
8e6c86a166

+ 4 - 2
BansheeEngine/Include/BsBuiltinMaterialManager.h

@@ -17,7 +17,8 @@ namespace BansheeEngine
 
 		virtual CM::HMaterial createSpriteTextMaterial() const = 0;
 		virtual CM::HMaterial createSpriteImageMaterial() const = 0;
-		virtual CM::HMaterial createDebugDrawMaterial() const = 0;
+		virtual CM::HMaterial createDebugDraw2DMaterial() const = 0;
+		virtual CM::HMaterial createDebugDraw3DMaterial() const = 0;
 	};
 
 	/**
@@ -33,7 +34,8 @@ namespace BansheeEngine
 
 		CM::HMaterial createSpriteTextMaterial() const;
 		CM::HMaterial createSpriteImageMaterial() const;
-		CM::HMaterial createDebugDrawMaterial() const;
+		CM::HMaterial createDebugDraw2DMaterial() const;
+		CM::HMaterial createDebugDraw3DMaterial() const;
 
 		void addFactory(BuiltinMaterialFactory* factory);
 		void setActive(const CM::String& renderSystemName);

+ 6 - 3
BansheeEngine/Include/BsD3D11BuiltinMaterialFactory.h

@@ -17,17 +17,20 @@ namespace BansheeEngine
 		
 		CM::HMaterial createSpriteTextMaterial() const;
 		CM::HMaterial createSpriteImageMaterial() const;
-		CM::HMaterial createDebugDrawMaterial() const;
+		CM::HMaterial createDebugDraw2DMaterial() const;
+		CM::HMaterial createDebugDraw3DMaterial() const;
 
 	protected:
 		CM::ShaderPtr mSpriteTextShader;
 		CM::ShaderPtr mSpriteImageShader;
-		CM::ShaderPtr mDebugDrawShader;
+		CM::ShaderPtr mDebugDraw2DShader;
+		CM::ShaderPtr mDebugDraw3DShader;
 
 		CM::HSamplerState mGUISamplerState;
 
 		void initSpriteTextShader();
 		void initSpriteImageShader();
-		void initDebugDrawShader();
+		void initDebugDraw2DShader();
+		void initDebugDraw3DShader();
 	};
 }

+ 6 - 3
BansheeEngine/Include/BsD3D9BuiltinMaterialFactory.h

@@ -17,17 +17,20 @@ namespace BansheeEngine
 
 		CM::HMaterial createSpriteTextMaterial() const;
 		CM::HMaterial createSpriteImageMaterial() const;
-		CM::HMaterial createDebugDrawMaterial() const;
+		CM::HMaterial createDebugDraw2DMaterial() const;
+		CM::HMaterial createDebugDraw3DMaterial() const;
 
 	protected:
 		CM::ShaderPtr mSpriteTextShader;
 		CM::ShaderPtr mSpriteImageShader;
-		CM::ShaderPtr mDebugDrawShader;
+		CM::ShaderPtr mDebugDraw2DShader;
+		CM::ShaderPtr mDebugDraw3DShader;
 
 		CM::HSamplerState mGUISamplerState;
 
 		void initSpriteTextShader();
 		void initSpriteImageShader();
-		void initDebugDrawShader();
+		void initDebugDraw2DShader();
+		void initDebugDraw3DShader();
 	};
 }

+ 6 - 3
BansheeEngine/Include/BsGLBuiltinMaterialFactory.h

@@ -17,17 +17,20 @@ namespace BansheeEngine
 
 		CM::HMaterial createSpriteTextMaterial() const;
 		CM::HMaterial createSpriteImageMaterial() const;
-		CM::HMaterial createDebugDrawMaterial() const;
+		CM::HMaterial createDebugDraw2DMaterial() const;
+		CM::HMaterial createDebugDraw3DMaterial() const;
 
 	protected:
 		CM::ShaderPtr mSpriteTextShader;
 		CM::ShaderPtr mSpriteImageShader;
-		CM::ShaderPtr mDebugDrawShader;
+		CM::ShaderPtr mDebugDraw2DShader;
+		CM::ShaderPtr mDebugDraw3DShader;
 
 		CM::HSamplerState mGUISamplerState;
 
 		void initSpriteTextShader();
 		void initSpriteImageShader();
-		void initDebugDrawShader();
+		void initDebugDraw2DShader();
+		void initDebugDraw3DShader();
 	};
 }

+ 9 - 2
BansheeEngine/Source/BsBuiltinMaterialManager.cpp

@@ -33,11 +33,18 @@ namespace BansheeEngine
 		return mActiveFactory->createSpriteImageMaterial();
 	}
 
-	HMaterial BuiltinMaterialManager::createDebugDrawMaterial() const
+	HMaterial BuiltinMaterialManager::createDebugDraw2DMaterial() const
 	{
 		assert(mActiveFactory != nullptr);
 
-		return mActiveFactory->createDebugDrawMaterial();
+		return mActiveFactory->createDebugDraw2DMaterial();
+	}
+
+	HMaterial BuiltinMaterialManager::createDebugDraw3DMaterial() const
+	{
+		assert(mActiveFactory != nullptr);
+
+		return mActiveFactory->createDebugDraw3DMaterial();
 	}
 
 	void BuiltinMaterialManager::addFactory(BuiltinMaterialFactory* factory)

+ 59 - 7
BansheeEngine/Source/BsD3D11BuiltinMaterialFactory.cpp

@@ -16,7 +16,8 @@ namespace BansheeEngine
 	{
 		initSpriteTextShader();
 		initSpriteImageShader();
-		initDebugDrawShader();
+		initDebugDraw2DShader();
+		initDebugDraw3DShader();
 
 		SAMPLER_STATE_DESC ssDesc;
 		ssDesc.magFilter = FO_POINT;
@@ -30,6 +31,8 @@ namespace BansheeEngine
 	{
 		mSpriteTextShader = nullptr;
 		mSpriteImageShader = nullptr;
+		mDebugDraw2DShader = nullptr;
+		mDebugDraw3DShader = nullptr;
 	}
 
 	const CM::String& D3D11BuiltinMaterialFactory::getSupportedRenderSystem() const
@@ -55,9 +58,14 @@ namespace BansheeEngine
 		return newMaterial;
 	}
 
-	HMaterial D3D11BuiltinMaterialFactory::createDebugDrawMaterial() const
+	HMaterial D3D11BuiltinMaterialFactory::createDebugDraw2DMaterial() const
 	{
-		return Material::create(mDebugDrawShader);
+		return Material::create(mDebugDraw2DShader);
+	}
+
+	HMaterial D3D11BuiltinMaterialFactory::createDebugDraw3DMaterial() const
+	{
+		return Material::create(mDebugDraw3DShader);
 	}
 
 	void D3D11BuiltinMaterialFactory::initSpriteTextShader()
@@ -198,11 +206,11 @@ namespace BansheeEngine
 		newPass->setDepthStencilState(depthState);
 	}
 
-	void D3D11BuiltinMaterialFactory::initDebugDrawShader()
+	void D3D11BuiltinMaterialFactory::initDebugDraw2DShader()
 	{
 		String vsCode = "						\
 		void vs_main(							\
-			in float3 inPos : POSITION,			\
+			in float2 inPos : POSITION,			\
 			in float4 color : COLOR0,			\
 			out float4 oPosition : SV_Position, \
 			out float4 oColor : COLOR0)			\
@@ -224,9 +232,53 @@ namespace BansheeEngine
 		vsProgram.synchronize();
 		psProgram.synchronize();
 
-		mDebugDrawShader = Shader::create("DebugDrawShader");
+		mDebugDraw2DShader = Shader::create("DebugDraw2DShader");
+
+		TechniquePtr newTechnique = mDebugDraw2DShader->addTechnique("D3D11RenderSystem", RendererManager::getCoreRendererName()); 
+		PassPtr newPass = newTechnique->addPass();
+		newPass->setVertexProgram(vsProgram);
+		newPass->setFragmentProgram(psProgram);
+
+		DEPTH_STENCIL_STATE_DESC depthStateDesc;
+		depthStateDesc.depthReadEnable = false;
+		depthStateDesc.depthWriteEnable = false;
+
+		HDepthStencilState depthState = DepthStencilState::create(depthStateDesc);
+		newPass->setDepthStencilState(depthState);
+	}
+
+	void D3D11BuiltinMaterialFactory::initDebugDraw3DShader()
+	{
+		String vsCode = "float4x4 viewTfrm;					\
+															\
+						void vs_main(						\
+						in float3 inPos : POSITION,			\
+						in float4 color : COLOR0,			\
+						out float4 oPosition : SV_Position, \
+						out float4 oColor : COLOR0)			\
+						{										\
+						oPosition = mul(viewTfrm, float4(inPos.xyz, 1)); \
+						oColor = color;						\
+						}";
+
+		String psCode = "																		\
+						float4 ps_main(in float4 inPos : SV_Position, in float4 color : COLOR0) : SV_Target		\
+						{																						\
+						return color;																		\
+						}																						\
+						";	
+
+		HHighLevelGpuProgram vsProgram = HighLevelGpuProgram::create(vsCode, "vs_main", "hlsl", GPT_VERTEX_PROGRAM, GPP_VS_4_0);
+		HHighLevelGpuProgram psProgram = HighLevelGpuProgram::create(psCode, "ps_main", "hlsl", GPT_FRAGMENT_PROGRAM, GPP_PS_4_0);
+
+		vsProgram.synchronize();
+		psProgram.synchronize();
+
+		mDebugDraw3DShader = Shader::create("DebugDraw3DShader");
+
+		mDebugDraw3DShader->addParameter("viewTfrm", "viewTfrm", GPDT_MATRIX_4X4);
 
-		TechniquePtr newTechnique = mDebugDrawShader->addTechnique("D3D11RenderSystem", RendererManager::getCoreRendererName()); 
+		TechniquePtr newTechnique = mDebugDraw3DShader->addTechnique("D3D11RenderSystem", RendererManager::getCoreRendererName()); 
 		PassPtr newPass = newTechnique->addPass();
 		newPass->setVertexProgram(vsProgram);
 		newPass->setFragmentProgram(psProgram);

+ 93 - 5
BansheeEngine/Source/BsD3D9BuiltinMaterialFactory.cpp

@@ -16,7 +16,8 @@ namespace BansheeEngine
 	{
 		initSpriteTextShader();
 		initSpriteImageShader();
-		initDebugDrawShader();
+		initDebugDraw2DShader();
+		initDebugDraw3DShader();
 
 		SAMPLER_STATE_DESC ssDesc;
 		ssDesc.magFilter = FO_POINT;
@@ -30,6 +31,8 @@ namespace BansheeEngine
 	{
 		mSpriteTextShader = nullptr;
 		mSpriteImageShader = nullptr;
+		mDebugDraw2DShader = nullptr;
+		mDebugDraw3DShader = nullptr;
 	}
 
 	const CM::String& D3D9BuiltinMaterialFactory::getSupportedRenderSystem() const
@@ -55,9 +58,14 @@ namespace BansheeEngine
 		return newMaterial;
 	}
 
-	HMaterial D3D9BuiltinMaterialFactory::createDebugDrawMaterial() const
+	HMaterial D3D9BuiltinMaterialFactory::createDebugDraw2DMaterial() const
 	{
-		return Material::create(mDebugDrawShader);
+		return Material::create(mDebugDraw2DShader);
+	}
+
+	HMaterial D3D9BuiltinMaterialFactory::createDebugDraw3DMaterial() const
+	{
+		return Material::create(mDebugDraw3DShader);
 	}
 
 	void D3D9BuiltinMaterialFactory::initSpriteTextShader()
@@ -196,8 +204,88 @@ namespace BansheeEngine
 		newPass->setDepthStencilState(depthState);
 	}
 
-	void D3D9BuiltinMaterialFactory::initDebugDrawShader()
+	void D3D9BuiltinMaterialFactory::initDebugDraw2DShader()
 	{
-		// TODO - Not implemented
+		String vsCode = "										\
+						void vs_main(							\
+						in float2 inPos : POSITION,				\
+						in float4 inColor : COLOR0,				\
+						out float4 oPosition : POSITION,		\
+						out float4 oColor : COLOR0)				\
+						{										\
+						oPosition = float4(inPos.xy, 0, 1);		\
+						oColor = inColor;						\
+						}										\
+						";
+
+		String psCode = "												\
+						float4 ps_main(float4 color : COLOR0) : COLOR0	\
+						{												\
+						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.synchronize();
+		psProgram.synchronize();
+
+		mDebugDraw2DShader = Shader::create("DebugDraw2DShader");
+
+		TechniquePtr newTechnique = mDebugDraw2DShader->addTechnique("D3D9RenderSystem", RendererManager::getCoreRendererName()); 
+		PassPtr newPass = newTechnique->addPass();
+		newPass->setVertexProgram(vsProgram);
+		newPass->setFragmentProgram(psProgram);
+
+		DEPTH_STENCIL_STATE_DESC depthStateDesc;
+		depthStateDesc.depthReadEnable = false;
+		depthStateDesc.depthWriteEnable = false;
+
+		HDepthStencilState depthState = DepthStencilState::create(depthStateDesc);
+		newPass->setDepthStencilState(depthState);
+	}
+
+	void D3D9BuiltinMaterialFactory::initDebugDraw3DShader()
+	{
+		String vsCode = "float4x4 viewTfrm;						\
+																\
+						void vs_main(							\
+						in float3 inPos : POSITION,				\
+						in float4 inColor : COLOR0,				\
+						out float4 oPosition : POSITION,		\
+						out float4 oColor : COLOR0)				\
+						{										\
+						oPosition = mul(viewTfrm, float4(inPos.xyz, 1));		\
+						oColor = inColor;						\
+						}										\
+						";
+
+		String psCode = "												\
+						float4 ps_main(float4 color : COLOR0) : COLOR0	\
+						{												\
+						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.synchronize();
+		psProgram.synchronize();
+
+		mDebugDraw3DShader = Shader::create("DebugDraw3DShader");
+
+		mDebugDraw3DShader->addParameter("viewTfrm", "viewTfrm", GPDT_MATRIX_4X4);
+
+		TechniquePtr newTechnique = mDebugDraw3DShader->addTechnique("D3D9RenderSystem", RendererManager::getCoreRendererName()); 
+		PassPtr newPass = newTechnique->addPass();
+		newPass->setVertexProgram(vsProgram);
+		newPass->setFragmentProgram(psProgram);
+
+		DEPTH_STENCIL_STATE_DESC depthStateDesc;
+		depthStateDesc.depthReadEnable = false;
+		depthStateDesc.depthWriteEnable = false;
+
+		HDepthStencilState depthState = DepthStencilState::create(depthStateDesc);
+		newPass->setDepthStencilState(depthState);
 	}
 }

+ 1 - 1
BansheeEngine/Source/BsDebugDraw.cpp

@@ -14,7 +14,7 @@ namespace BansheeEngine
 {
 	DebugDraw::DebugDraw()
 	{
-		mMaterial = BuiltinMaterialManager::instance().createDebugDrawMaterial();
+		mMaterial = BuiltinMaterialManager::instance().createDebugDraw2DMaterial();
 	}
 
 	void DebugDraw::quad2D(const Vector2& pos, const Vector2& size, UINT8* outVertices, UINT8* outColors, 

+ 102 - 5
BansheeEngine/Source/BsGLBuiltinMaterialFactory.cpp

@@ -16,7 +16,8 @@ namespace BansheeEngine
 	{
 		initSpriteTextShader();
 		initSpriteImageShader();
-		initDebugDrawShader();
+		initDebugDraw2DShader();
+		initDebugDraw3DShader();
 
 		SAMPLER_STATE_DESC ssDesc;
 		ssDesc.magFilter = FO_POINT;
@@ -30,6 +31,8 @@ namespace BansheeEngine
 	{
 		mSpriteTextShader = nullptr;
 		mSpriteImageShader = nullptr;
+		mDebugDraw2DShader = nullptr;
+		mDebugDraw3DShader = nullptr;
 	}
 
 	const CM::String& GLBuiltinMaterialFactory::getSupportedRenderSystem() const
@@ -55,9 +58,14 @@ namespace BansheeEngine
 		return newMaterial;
 	}
 
-	HMaterial GLBuiltinMaterialFactory::createDebugDrawMaterial() const
+	HMaterial GLBuiltinMaterialFactory::createDebugDraw2DMaterial() const
 	{
-		return Material::create(mDebugDrawShader);
+		return Material::create(mDebugDraw2DShader);
+	}
+
+	HMaterial GLBuiltinMaterialFactory::createDebugDraw3DMaterial() const
+	{
+		return Material::create(mDebugDraw3DShader);
 	}
 
 	void GLBuiltinMaterialFactory::initSpriteTextShader()
@@ -202,8 +210,97 @@ namespace BansheeEngine
 		newPass->setDepthStencilState(depthState);
 	}
 
-	void GLBuiltinMaterialFactory::initDebugDrawShader()
+	void GLBuiltinMaterialFactory::initDebugDraw2DShader()
 	{
-		// TODO - Not implemented
+		String vsCode = "#version 400\n								\
+																	\
+						in vec2 cm_position;						\
+						in vec4 cm_color0;							\
+						out vec4 color0;							\
+																	\
+						void main()									\
+						{											\
+						gl_Position = vec4(cm_position.xy, 0, 1);	\
+						color0 = cm_color0;							\
+						}";
+
+		String psCode = "#version 400\n						\
+															\
+						in vec4 color0;						\
+						out vec4 fragColor;					\
+															\
+						void main()							\
+						{									\
+						fragColor = color0;					\
+						}";
+
+		HHighLevelGpuProgram vsProgram = HighLevelGpuProgram::create(vsCode, "vs_main", "glsl", GPT_VERTEX_PROGRAM, GPP_VS_4_0);
+		HHighLevelGpuProgram psProgram = HighLevelGpuProgram::create(psCode, "ps_main", "glsl", GPT_FRAGMENT_PROGRAM, GPP_PS_4_0);
+
+		vsProgram.synchronize();
+		psProgram.synchronize();
+
+		mDebugDraw2DShader = Shader::create("DebugDraw2DShader");
+
+		TechniquePtr newTechnique = mDebugDraw2DShader->addTechnique("GLRenderSystem", RendererManager::getCoreRendererName()); 
+		PassPtr newPass = newTechnique->addPass();
+		newPass->setVertexProgram(vsProgram);
+		newPass->setFragmentProgram(psProgram);
+
+		DEPTH_STENCIL_STATE_DESC depthStateDesc;
+		depthStateDesc.depthReadEnable = false;
+		depthStateDesc.depthWriteEnable = false;
+
+		HDepthStencilState depthState = DepthStencilState::create(depthStateDesc);
+		newPass->setDepthStencilState(depthState);
+	}
+
+	void GLBuiltinMaterialFactory::initDebugDraw3DShader()
+	{
+		String vsCode = "#version 400\n								\
+																	\
+						uniform mat4 viewTfrm;						\
+																	\
+						in vec3 cm_position;						\
+						in vec4 cm_color0;							\
+						out vec4 color0;							\
+																	\
+						void main()									\
+						{											\
+						gl_Position = viewTfrm * vec4(cm_position.xyz, 1);		\
+						color0 = cm_color0;							\
+						}";
+
+		String psCode = "#version 400\n						\
+															\
+						in vec4 color0;						\
+						out vec4 fragColor;					\
+															\
+						void main()							\
+						{									\
+						fragColor = color;					\
+						}";
+
+		HHighLevelGpuProgram vsProgram = HighLevelGpuProgram::create(vsCode, "vs_main", "glsl", GPT_VERTEX_PROGRAM, GPP_VS_4_0);
+		HHighLevelGpuProgram psProgram = HighLevelGpuProgram::create(psCode, "ps_main", "glsl", GPT_FRAGMENT_PROGRAM, GPP_PS_4_0);
+
+		vsProgram.synchronize();
+		psProgram.synchronize();
+
+		mDebugDraw3DShader = Shader::create("DebugDraw3DShader");
+
+		mDebugDraw3DShader->addParameter("viewTfrm", "viewTfrm", GPDT_MATRIX_4X4);
+
+		TechniquePtr newTechnique = mDebugDraw3DShader->addTechnique("GLRenderSystem", RendererManager::getCoreRendererName()); 
+		PassPtr newPass = newTechnique->addPass();
+		newPass->setVertexProgram(vsProgram);
+		newPass->setFragmentProgram(psProgram);
+
+		DEPTH_STENCIL_STATE_DESC depthStateDesc;
+		depthStateDesc.depthReadEnable = false;
+		depthStateDesc.depthWriteEnable = false;
+
+		HDepthStencilState depthState = DepthStencilState::create(depthStateDesc);
+		newPass->setDepthStencilState(depthState);
 	}
 }

+ 2 - 2
CamelotClient/CamelotClient.cpp

@@ -37,9 +37,9 @@
 #include "CmRTTIType.h"
 #include "CmCursor.h"
 
-#define DX11
+//#define DX11
 //#define DX9
-//#define GL
+#define GL
 
 using namespace CamelotFramework;
 using namespace BansheeEditor;

+ 3 - 0
DrawHelper.txt

@@ -19,3 +19,6 @@ Add class DrawHelper:
    - all elements should be grouped into a common vertex/index buffer (as much as possible)
    - draw methods should combine methods above to make drawing easier. e.g. a transparent box with AA line borders should be easily drawable
 
+   IMMEDIATE:
+    Add debug draw materials for DX9 and GL
+    I need to apply view transform on non-2D elements (probably will need another shader and material)