Преглед изворни кода

Separated DrawHelper into a template so I can more easily make 2D and 3D versions

Marko Pintera пре 12 година
родитељ
комит
fb8295270d

+ 4 - 2
BansheeEngine/BansheeEngine.vcxproj

@@ -227,8 +227,9 @@
   </ItemDefinitionGroup>
   <ItemGroup>
     <ClInclude Include="Include\BsApplication.h" />
-    <ClInclude Include="Include\BsDrawHelper.h" />
+    <ClInclude Include="Include\BsDrawHelperTemplate.h" />
     <ClInclude Include="Include\BsDragAndDropManager.h" />
+    <ClInclude Include="Include\BsDrawHelper2D.h" />
     <ClInclude Include="Include\BsEngineGUI.h" />
     <ClInclude Include="Include\BsGUIArea.h" />
     <ClInclude Include="Include\BsGUIButton.h" />
@@ -284,7 +285,8 @@
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\BsApplication.cpp" />
-    <ClCompile Include="Source\BsDrawHelper.cpp" />
+    <ClCompile Include="Source\BsDrawHelper2D.cpp" />
+    <ClCompile Include="Source\BsDrawHelperTemplate.cpp" />
     <ClCompile Include="Source\BsDragAndDropManager.cpp" />
     <ClCompile Include="Source\BsEngineGUI.cpp" />
     <ClCompile Include="Source\BsGUIArea.cpp" />

+ 8 - 2
BansheeEngine/BansheeEngine.vcxproj.filters

@@ -198,7 +198,10 @@
     <ClInclude Include="Include\BsRendererSort.h">
       <Filter>Header Files</Filter>
     </ClInclude>
-    <ClInclude Include="Include\BsDrawHelper.h">
+    <ClInclude Include="Include\BsDrawHelper2D.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="Include\BsDrawHelperTemplate.h">
       <Filter>Header Files</Filter>
     </ClInclude>
   </ItemGroup>
@@ -341,7 +344,10 @@
     <ClCompile Include="Source\BsGUIViewport.cpp">
       <Filter>Source Files\GUI</Filter>
     </ClCompile>
-    <ClCompile Include="Source\BsDrawHelper.cpp">
+    <ClCompile Include="Source\BsDrawHelperTemplate.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="Source\BsDrawHelper2D.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
   </ItemGroup>

+ 18 - 65
BansheeEngine/Include/BsDrawHelper.h → BansheeEngine/Include/BsDrawHelper2D.h

@@ -1,45 +1,17 @@
 #pragma once
 
 #include "BsPrerequisites.h"
+#include "BsDrawHelperTemplate.h"
 #include "CmModule.h"
 #include "CmColor.h"
 #include "CmAABox.h"
 
 namespace BansheeEngine
 {
-	enum class CoordType
+	class BS_EXPORT DrawHelper2D : public DrawHelperTemplate<CM::Vector2>, public CM::Module<DrawHelper2D>
 	{
-		Pixel,
-		Normalized
-	};
-
-	enum class DrawStyle
-	{
-		Fill,
-		Border,
-		FillAndBorder
-	};
-
-	class BS_EXPORT DrawHelper : public CM::Module<DrawHelper>
-	{
-		enum class DebugDrawType
-		{
-			ClipSpace,
-			ScreenSpace,
-			WorldSpace
-		};
-
-		struct DebugDrawCommand
-		{
-			CM::HMesh mesh;
-			CM::HMaterial material;
-			DebugDrawType type;
-			CM::Vector3 worldCenter;
-			float endTime;
-		};
-
 	public:
-		DrawHelper();
+		DrawHelper2D();
 
 		/**
 		 * @brief	Fills the mesh data with vertices representing a quad (2 triangles).
@@ -54,7 +26,7 @@ namespace BansheeEngine
 		 * 			  32bit index buffer
 		 * 			  Enough space for 4 vertices and 6 indices
 		 */
-		void quad2D(const CM::FRect& area, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset);
+		void quad(const CM::FRect& area, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset);
 
 		/**
 		 * @brief	Fills the mesh data with vertices representing a per-pixel line.
@@ -72,7 +44,7 @@ namespace BansheeEngine
 		 * 			  32bit index buffer
 		 * 			  Enough space for 2 vertices and 2 indices
 		 */
-		void line2D_Pixel(const CM::Vector2& a, const CM::Vector2& b, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset);
+		void line_Pixel(const CM::Vector2& a, const CM::Vector2& b, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset);
 
 		/**
 		 * @brief	Fills the mesh data with vertices representing an anti-aliased line of specific width. Antialiasing is done using alpha blending.
@@ -92,7 +64,7 @@ namespace BansheeEngine
 		 * 			  32bit index buffer
 		 *			  Enough space for 8 vertices and 30 indices
 		 */
-		void line2D_AA(const CM::Vector2& a, const CM::Vector2& b, float width, float borderWidth, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset);
+		void line_AA(const CM::Vector2& a, const CM::Vector2& b, float width, float borderWidth, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset);
 
 		/**
 		 * @brief	Fills the mesh data with vertices representing per-pixel lines.
@@ -109,7 +81,7 @@ namespace BansheeEngine
 		 * 			  32bit index buffer
 		 * 			  Enough space for (numLines * 2) vertices and (numLines * 2) indices
 		 */
-		void lineList2D_Pixel(const CM::Vector<CM::Vector2>::type& linePoints, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset);
+		void lineList_Pixel(const CM::Vector<CM::Vector2>::type& linePoints, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset);
 
 		/**
 		 * @brief	Fills the mesh data with vertices representing anti-aliased lines of specific width. Antialiasing is done using alpha blending.
@@ -128,40 +100,21 @@ namespace BansheeEngine
 		 * 			  32bit index buffer
 		 *			  Enough space for (numLines * 8) vertices and (numLines * 30) indices
 		 */
-		void lineList2D_AA(const CM::Vector<CM::Vector2>::type& linePoints, float width, float borderWidth, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset);
-
-		void drawQuad2D(const HCamera& camera, const CM::FRect& area, const CM::Color& color = CM::Color::White, CoordType coordType = CoordType::Pixel, float timeout = 0.0f);
-		void drawLine2D_Pixel(const HCamera& camera, const CM::Vector2& a, const CM::Vector2& b, const CM::Color& color = CM::Color::White, 
-			CoordType coordType = CoordType::Pixel, float timeout = 0.0f);
-		void drawLine2D_AA(const HCamera& camera, const CM::Vector2& a, const CM::Vector2& b, float width, float borderWidth, 
-			const CM::Color& color = CM::Color::White, CoordType coordType = CoordType::Pixel, float timeout = 0.0f);
-		void drawLineList2D_Pixel(const HCamera& camera, const CM::Vector<CM::Vector2>::type& linePoints, const CM::Color& color = CM::Color::White, 
-			CoordType coordType = CoordType::Pixel, float timeout = 0.0f);
-		void drawLineList2D_AA(const HCamera& camera, const CM::Vector<CM::Vector2>::type& linePoints, float width, float borderWidth, 
-			const CM::Color& color = CM::Color::White, CoordType coordType = CoordType::Pixel, float timeout = 0.0f);
-
-		void render(const HCamera& camera, CM::RenderQueue& renderQueue);
+		void lineList_AA(const CM::Vector<CM::Vector2>::type& linePoints, float width, float borderWidth, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset);
+
+		void drawQuad(const HCamera& camera, const CM::FRect& area, const CM::Color& color = CM::Color::White, DebugDrawCoordType coordType = DebugDrawCoordType::Pixel, float timeout = 0.0f);
+		void drawLine_Pixel(const HCamera& camera, const CM::Vector2& a, const CM::Vector2& b, const CM::Color& color = CM::Color::White, 
+			DebugDrawCoordType coordType = DebugDrawCoordType::Pixel, float timeout = 0.0f);
+		void drawLine_AA(const HCamera& camera, const CM::Vector2& a, const CM::Vector2& b, float width, float borderWidth, 
+			const CM::Color& color = CM::Color::White, DebugDrawCoordType coordType = DebugDrawCoordType::Pixel, float timeout = 0.0f);
+		void drawLineList_Pixel(const HCamera& camera, const CM::Vector<CM::Vector2>::type& linePoints, const CM::Color& color = CM::Color::White, 
+			DebugDrawCoordType coordType = DebugDrawCoordType::Pixel, float timeout = 0.0f);
+		void drawLineList_AA(const HCamera& camera, const CM::Vector<CM::Vector2>::type& linePoints, float width, float borderWidth, 
+			const CM::Color& color = CM::Color::White, DebugDrawCoordType coordType = DebugDrawCoordType::Pixel, float timeout = 0.0f);
 
 	private:
 		CM::HMaterial mMaterial2DClipSpace;
 
-		CM::UnorderedMap<const CM::Viewport*, CM::Vector<DebugDrawCommand>::type>::type mCommandsPerViewport;
-
-		void line2D_Pixel(const CM::Vector2& a, const CM::Vector2& b, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
-			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset);
-
-		void line2D_AA(const CM::Vector2& a, const CM::Vector2& b, float width, float borderWidth, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
-			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset);
-
-		void polygon2D_AA(const CM::Vector<CM::Vector2>::type& points, float borderWidth, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
-			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset);
-
-		void polygonFill2D_Pixel(const CM::Vector<CM::Vector2>::type& points, CM::UINT8* outVertices, 
-			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset);
-
-		void polygonBorder2D_Pixel(const CM::Vector<CM::Vector2>::type& points, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
-			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset);
-
 		CM::FRect normalizedCoordToClipSpace(const CM::FRect& area) const;
 		CM::Vector2 normalizedCoordToClipSpace(const CM::Vector2& pos) const;
 	};

+ 304 - 0
BansheeEngine/Include/BsDrawHelperTemplate.h

@@ -0,0 +1,304 @@
+#pragma once
+
+#include "BsPrerequisites.h"
+#include "CmColor.h"
+#include "CmAABox.h"
+
+namespace BansheeEngine
+{
+	enum class DebugDrawCoordType
+	{
+		Pixel,
+		Normalized
+	};
+
+	enum class DrawStyle
+	{
+		Fill,
+		Border,
+		FillAndBorder
+	};
+
+	enum class DebugDrawType
+	{
+		ClipSpace,
+		ScreenSpace,
+		WorldSpace
+	};
+
+	struct DebugDrawCommand
+	{
+		CM::HMesh mesh;
+		CM::HMaterial material;
+		DebugDrawType type;
+		CM::Vector3 worldCenter;
+		float endTime;
+	};
+
+	class BS_EXPORT DrawHelperTemplateBase
+	{
+	public:
+		void render(const HCamera& camera, CM::RenderQueue& renderQueue);
+
+	protected:
+		CM::UnorderedMap<const CM::Viewport*, CM::Vector<DebugDrawCommand>::type>::type mCommandsPerViewport;
+	};
+
+	template <class T>
+	class BS_EXPORT DrawHelperTemplate : public DrawHelperTemplateBase
+	{
+	protected:
+		void line_Pixel(const T& a, const T& b, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
+		{
+			UINT32* indexData = meshData->getIndices32();
+			UINT8* positionData = meshData->getElementData(VES_POSITION);
+			UINT8* colorData = meshData->getElementData(VES_COLOR);
+
+			assert((vertexOffset + 2) <= meshData->getNumVertices());
+			assert((indexOffset + 2) <= meshData->getNumIndices());
+
+			line_Pixel(a, b, color, positionData, colorData, vertexOffset, meshData->getVertexStride(), indexData, indexOffset);
+		}
+
+		void line_AA(const T& a, const T& b, float width, float borderWidth, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
+		{
+			UINT32* indexData = meshData->getIndices32();
+			UINT8* positionData = meshData->getElementData(VES_POSITION);
+			UINT8* colorData = meshData->getElementData(VES_COLOR);
+
+			assert((vertexOffset + 8) <= meshData->getNumVertices());
+			assert((indexOffset + 30) <= meshData->getNumIndices());
+
+			line_AA(a, b, width, borderWidth, color, positionData, colorData, vertexOffset, meshData->getVertexStride(), indexData, indexOffset);
+		}
+
+		void lineList_Pixel(const typename CM::Vector<T>::type& linePoints, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
+		{
+			assert(linePoints.size() % 2 == 0);
+
+			assert((vertexOffset + linePoints.size() * 2) <= meshData->getNumVertices());
+			assert((indexOffset + linePoints.size() * 2) <= meshData->getNumIndices());
+
+			UINT32 curVertOffset = vertexOffset;
+			UINT32 curIdxOffset = indexOffset;
+
+			UINT32* indexData = meshData->getIndices32();
+			UINT8* positionData = meshData->getElementData(VES_POSITION);
+			UINT8* colorData = meshData->getElementData(VES_COLOR);
+
+			UINT32 numPoints = (UINT32)linePoints.size();
+			for(UINT32 i = 0; i < numPoints; i += 2)
+			{
+				line_Pixel(linePoints[i], linePoints[i + 1], color, positionData, colorData, curVertOffset, meshData->getVertexStride(), indexData, curIdxOffset);
+
+				curVertOffset += 2;
+				curIdxOffset += 2;
+			}
+		}
+
+		void lineList_AA(const typename CM::Vector<T>::type& linePoints, float width, float borderWidth, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
+		{
+			assert(linePoints.size() % 2 == 0);
+
+			assert((vertexOffset + linePoints.size() * 4) <= meshData->getNumVertices());
+			assert((indexOffset + linePoints.size() * 15) <= meshData->getNumIndices());
+
+			UINT32 curVertOffset = vertexOffset;
+			UINT32 curIdxOffset = indexOffset;
+
+			UINT32* indexData = meshData->getIndices32();
+			UINT8* positionData = meshData->getElementData(VES_POSITION);
+			UINT8* colorData = meshData->getElementData(VES_COLOR);
+
+			UINT32 numPoints = (UINT32)linePoints.size();
+			for(UINT32 i = 0; i < numPoints; i += 2)
+			{
+				line_AA(linePoints[i], linePoints[i + 1], width, borderWidth, color, positionData, colorData, curVertOffset, meshData->getVertexStride(), indexData, curIdxOffset);
+
+				curVertOffset += 8;
+				curIdxOffset += 30;
+			}
+		}
+
+		void line_Pixel(const T& a, const T& b, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
+			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
+		{
+			outVertices += (vertexOffset * vertexStride);
+			outColors += (vertexOffset * vertexStride);
+
+			T* vertices = (T*)outVertices;
+			(*vertices) = a;
+
+			vertices = (T*)(outVertices + vertexStride);
+			(*vertices) = b;
+
+			UINT32* colors = (UINT32*)outColors;
+			(*colors) = color.getAsRGBA();
+
+			colors = (UINT32*)(outColors + vertexStride);
+			(*colors) = color.getAsRGBA();
+
+			outIndices += indexOffset;
+			outIndices[0] = vertexOffset + 0;
+			outIndices[1] = vertexOffset + 1;
+		}
+
+		void line_AA(const T& a, const T& b, float width, float borderWidth, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
+			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
+		{
+			T dir = b - a;
+			dir.normalize();
+
+			T nrm(dir.y, -dir.x); // TODO - Not properly ported
+
+			Vector<T>::type points(4);
+
+			float r = width - 1.0f;
+			r *= 0.5f;
+			if (r < 0.01f) 
+				r = 0.01f;
+
+			dir = dir * r;
+			nrm = nrm * r;
+
+			T v0 = a - dir - nrm;
+			T v1 = a - dir + nrm;
+			T v2 = b + dir + nrm;
+			T v3 = b + dir - nrm;
+
+			points[0] = v0;
+			points[1] = v1;
+			points[2] = v2;
+			points[3] = v3;
+
+			polygon_AA(points, borderWidth, color, outVertices, outColors, vertexOffset, vertexStride, outIndices, indexOffset);
+		}
+
+		void polygon_AA(const typename CM::Vector<T>::type& points, float borderWidth, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
+			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
+		{
+			UINT32 numCoords = (UINT32)points.size();
+
+			outVertices += vertexOffset * vertexStride;
+			Vector<T>::type tempNormals(numCoords);
+
+			for(UINT32 i = 0, j = numCoords - 1; i < numCoords; j = i++)
+			{
+				const T& v0 = points[j];
+				const T& v1 = points[i];
+
+				T d = v1 - v0;
+				d.normalize();
+
+				// Rotate by 90 degrees
+				std::swap(d.x, d.y); // TODO - Not properly ported
+				d.y = -d.y;
+
+				tempNormals[j] = d;
+
+				// Also start populating the vertex array
+				T* vertices = (T*)outVertices;
+				*vertices = v1;
+
+				UINT32* colors = (UINT32*)outColors;
+				*colors = color.getAsRGBA();
+
+				outVertices += vertexStride;
+				outColors += vertexStride;
+			}
+
+			Color transparentColor = color;
+			transparentColor.a = 0.0f;
+
+			for(UINT32 i = 0, j = numCoords - 1; i < numCoords; j = i++)
+			{
+				const T& n0 = tempNormals[j];
+				const T& n1 = tempNormals[i];
+
+				T avgNrm = (n0 + n1) * 0.5f;
+				float magSqrd = avgNrm.squaredLength();
+
+				if (magSqrd > 0.000001f)
+				{
+					float scale = 1.0f / magSqrd;
+					if (scale > 10.0f) 
+						scale = 10.0f;
+
+					avgNrm = avgNrm * scale;
+				}
+
+				T tempCoord = points[i] + avgNrm * borderWidth;
+
+				// Move it to the vertex array
+				T* vertices = (T*)outVertices;
+				*vertices = tempCoord;
+
+				UINT32* colors = (UINT32*)outColors;
+				*colors = transparentColor.getAsRGBA();
+
+				outVertices += vertexStride;
+				outColors += vertexStride;
+			}
+
+			// Populate index buffer
+			outIndices += indexOffset;
+
+			UINT32 idxCnt = 0;
+			for(UINT32 i = 0, j = numCoords - 1; i < numCoords; j = i++)
+			{
+				outIndices[idxCnt++] = i;
+				outIndices[idxCnt++] = j;
+				outIndices[idxCnt++] = numCoords + j;
+
+				outIndices[idxCnt++] = numCoords + j;
+				outIndices[idxCnt++] = numCoords + i;
+				outIndices[idxCnt++] = i;
+			}
+
+			for(UINT32 i = 2; i < numCoords; ++i)
+			{
+				outIndices[idxCnt++] = 0;
+				outIndices[idxCnt++] = i - 1;
+				outIndices[idxCnt++] = i;
+			}
+		}
+
+		void polygonFill_Pixel(const typename CM::Vector<T>::type& points, CM::UINT8* outVertices, 
+			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
+		{
+			outVertices += (vertexOffset * vertexStride);
+
+			for(auto& point : points)
+			{
+				T* vertices = (T*)outVertices;
+				(*vertices) = Vector2(point.x, point.y); // TODO - Not properly ported
+
+				outVertices += vertexStride;
+			}
+
+			outIndices += indexOffset;
+			INT32 numPoints = (INT32)points.size();
+			UINT32 idxCnt = 0;
+			for(int i = 2; i < numPoints; i++)
+			{
+				outIndices[idxCnt++] = vertexOffset;
+				outIndices[idxCnt++] = vertexOffset + i - 1;
+				outIndices[idxCnt++] = vertexOffset + i;
+			}
+		}
+
+		void polygonBorder_Pixel(const typename CM::Vector<T>::type& points, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
+			CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
+		{
+			INT32 numPoints = (INT32)points.size();
+			UINT32 curVertOffset = vertexOffset;
+			UINT32 curIdxOffset = indexOffset;
+			for(INT32 i = 0, j = numPoints - 1; i < numPoints; j = i++)
+			{
+				line_Pixel(points[j], points[i], color, outVertices, outColors, curVertOffset, vertexStride, outIndices, curIdxOffset);
+				curVertOffset += 2;
+				curIdxOffset += 2;
+			}
+		}
+	};
+}

+ 3 - 3
BansheeEngine/Source/BsApplication.cpp

@@ -2,7 +2,7 @@
 #include "BsGUIMaterialManager.h"
 #include "BsGUIManager.h"
 #include "BsOverlayManager.h"
-#include "BsDrawHelper.h"
+#include "BsDrawHelper2D.h"
 #include "BsBuiltinMaterialManager.h"
 #include "BsD3D9BuiltinMaterialFactory.h"
 #include "BsD3D11BuiltinMaterialFactory.h"
@@ -45,7 +45,7 @@ namespace BansheeEngine
 		BuiltinMaterialManager::instance().addFactory(cm_new<GLBuiltinMaterialFactory>());
 		BuiltinMaterialManager::instance().setActive(desc.renderSystem);
 
-		DrawHelper::startUp(cm_new<DrawHelper>());
+		DrawHelper2D::startUp(cm_new<DrawHelper2D>());
 
 		EngineGUI::startUp(new EngineGUI());
 
@@ -63,7 +63,7 @@ namespace BansheeEngine
 
 		EngineGUI::shutDown();
 
-		DrawHelper::shutDown();
+		DrawHelper2D::shutDown();
 
 		GUIMaterialManager::instance().forceReleaseAllMaterials();
 

+ 0 - 628
BansheeEngine/Source/BsDrawHelper.cpp

@@ -1,628 +0,0 @@
-#include "BsDrawHelper.h"
-#include "CmFRect.h"
-#include "CmMesh.h"
-#include "CmTime.h"
-#include "CmVector2.h"
-#include "CmMaterial.h"
-#include "CmPass.h"
-#include "CmApplication.h"
-#include "CmRenderQueue.h"
-#include "BsCamera.h"
-#include "BsBuiltinMaterialManager.h"
-
-using namespace CamelotFramework;
-
-namespace BansheeEngine
-{
-	DrawHelper::DrawHelper()
-	{
-		mMaterial2DClipSpace = BuiltinMaterialManager::instance().createDebugDraw2DClipSpaceMaterial();
-	}
-
-	void DrawHelper::quad2D(const CM::FRect& area, const MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
-	{
-		UINT32* indexData = meshData->getIndices32();
-		UINT8* positionData = meshData->getElementData(VES_POSITION);
-
-		assert((vertexOffset + 4) <= meshData->getNumVertices());
-		assert((indexOffset + 6) <= meshData->getNumIndices());
-
-		Vector<Vector2>::type points;
-		points.push_back(Vector2(area.x, area.y));
-		points.push_back(Vector2(area.x + area.width, area.y));
-		points.push_back(Vector2(area.x + area.width, area.y + area.height));
-		points.push_back(Vector2(area.x, area.y + area.height));	
-		
-		polygonFill2D_Pixel(points, positionData, vertexOffset, meshData->getVertexStride(), indexData, indexOffset);
-	}
-
-	void DrawHelper::line2D_Pixel(const CM::Vector2& a, const CM::Vector2& b, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
-		CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
-	{
-		outVertices += (vertexOffset * vertexStride);
-		outColors += (vertexOffset * vertexStride);
-
-		Vector2* vertices = (Vector2*)outVertices;
-		(*vertices) = a;
-
-		vertices = (Vector2*)(outVertices + vertexStride);
-		(*vertices) = b;
-
-		UINT32* colors = (UINT32*)outColors;
-		(*colors) = color.getAsRGBA();
-
-		colors = (UINT32*)(outColors + vertexStride);
-		(*colors) = color.getAsRGBA();
-
-		outIndices += indexOffset;
-		outIndices[0] = vertexOffset + 0;
-		outIndices[1] = vertexOffset + 1;
-	}
-
-	void DrawHelper::line2D_Pixel(const CM::Vector2& a, const CM::Vector2& b, const CM::Color& color, const MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
-	{
-		UINT32* indexData = meshData->getIndices32();
-		UINT8* positionData = meshData->getElementData(VES_POSITION);
-		UINT8* colorData = meshData->getElementData(VES_COLOR);
-
-		assert((vertexOffset + 2) <= meshData->getNumVertices());
-		assert((indexOffset + 2) <= meshData->getNumIndices());
-
-		line2D_Pixel(a, b, color, positionData, colorData, vertexOffset, meshData->getVertexStride(), indexData, indexOffset);
-	}
-
-	void DrawHelper::line2D_AA(const CM::Vector2& a, const CM::Vector2& b, float width, float borderWidth, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
-		CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
-	{
-		Vector2 dir = b - a;
-		dir.normalize();
-
-		Vector2 nrm(dir.y, -dir.x);
-
-		Vector<Vector2>::type points(4);
-
-		float r = width - 1.0f;
-		r *= 0.5f;
-		if (r < 0.01f) 
-			r = 0.01f;
-
-		dir = dir * r;
-		nrm = nrm * r;
-
-		Vector2 v0 = a - dir - nrm;
-		Vector2 v1 = a - dir + nrm;
-		Vector2 v2 = b + dir + nrm;
-		Vector2 v3 = b + dir - nrm;
-
-		points[0] = v0;
-		points[1] = v1;
-		points[2] = v2;
-		points[3] = v3;
-
-		polygon2D_AA(points, borderWidth, color, outVertices, outColors, vertexOffset, vertexStride, outIndices, indexOffset);
-	}
-
-	void DrawHelper::line2D_AA(const CM::Vector2& a, const CM::Vector2& b, float width, float borderWidth, const CM::Color& color, const MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
-	{
-		UINT32* indexData = meshData->getIndices32();
-		UINT8* positionData = meshData->getElementData(VES_POSITION);
-		UINT8* colorData = meshData->getElementData(VES_COLOR);
-
-		assert((vertexOffset + 8) <= meshData->getNumVertices());
-		assert((indexOffset + 30) <= meshData->getNumIndices());
-
-		line2D_AA(a, b, width, borderWidth, color, positionData, colorData, vertexOffset, meshData->getVertexStride(), indexData, indexOffset);
-	}
-
-	void DrawHelper::lineList2D_Pixel(const Vector<CM::Vector2>::type& linePoints, const Color& color, const MeshDataPtr& meshData, UINT32 vertexOffset, UINT32 indexOffset)
-	{
-		assert(linePoints.size() % 2 == 0);
-
-		assert((vertexOffset + linePoints.size() * 2) <= meshData->getNumVertices());
-		assert((indexOffset + linePoints.size() * 2) <= meshData->getNumIndices());
-
-		UINT32 curVertOffset = vertexOffset;
-		UINT32 curIdxOffset = indexOffset;
-
-		UINT32* indexData = meshData->getIndices32();
-		UINT8* positionData = meshData->getElementData(VES_POSITION);
-		UINT8* colorData = meshData->getElementData(VES_COLOR);
-
-		UINT32 numPoints = (UINT32)linePoints.size();
-		for(UINT32 i = 0; i < numPoints; i += 2)
-		{
-			line2D_Pixel(linePoints[i], linePoints[i + 1], color, positionData, colorData, curVertOffset, meshData->getVertexStride(), indexData, curIdxOffset);
-
-			curVertOffset += 2;
-			curIdxOffset += 2;
-		}
-	}
-
-	void DrawHelper::lineList2D_AA(const Vector<Vector2>::type& linePoints, float width, float borderWidth, const Color& color, const MeshDataPtr& meshData, UINT32 vertexOffset, UINT32 indexOffset)
-	{
-		assert(linePoints.size() % 2 == 0);
-
-		assert((vertexOffset + linePoints.size() * 4) <= meshData->getNumVertices());
-		assert((indexOffset + linePoints.size() * 15) <= meshData->getNumIndices());
-
-		UINT32 curVertOffset = vertexOffset;
-		UINT32 curIdxOffset = indexOffset;
-
-		UINT32* indexData = meshData->getIndices32();
-		UINT8* positionData = meshData->getElementData(VES_POSITION);
-		UINT8* colorData = meshData->getElementData(VES_COLOR);
-
-		UINT32 numPoints = (UINT32)linePoints.size();
-		for(UINT32 i = 0; i < numPoints; i += 2)
-		{
-			line2D_AA(linePoints[i], linePoints[i + 1], width, borderWidth, color, positionData, colorData, curVertOffset, meshData->getVertexStride(), indexData, curIdxOffset);
-
-			curVertOffset += 8;
-			curIdxOffset += 30;
-		}
-	}
-
-	void DrawHelper::polygon2D_AA(const Vector<Vector2>::type& points, float borderWidth, const CM::Color& color, 
-		UINT8* outVertices, CM::UINT8* outColors, UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset)
-	{
-		UINT32 numCoords = (UINT32)points.size();
-
-		outVertices += vertexOffset * vertexStride;
-		Vector<Vector2>::type tempNormals(numCoords);
-
-		for(UINT32 i = 0, j = numCoords - 1; i < numCoords; j = i++)
-		{
-			const Vector2& v0 = points[j];
-			const Vector2& v1 = points[i];
-
-			Vector2 d = v1 - v0;
-			d.normalize();
-
-			// Rotate by 90 degrees
-			std::swap(d.x, d.y);
-			d.y = -d.y;
-
-			tempNormals[j] = d;
-
-			// Also start populating the vertex array
-			Vector2* vertices = (Vector2*)outVertices;
-			*vertices = v1;
-
-			UINT32* colors = (UINT32*)outColors;
-			*colors = color.getAsRGBA();
-
-			outVertices += vertexStride;
-			outColors += vertexStride;
-		}
-
-		Color transparentColor = color;
-		transparentColor.a = 0.0f;
-
-		for(UINT32 i = 0, j = numCoords - 1; i < numCoords; j = i++)
-		{
-			const Vector2& n0 = tempNormals[j];
-			const Vector2& n1 = tempNormals[i];
-
-			Vector2 avgNrm = (n0 + n1) * 0.5f;
-			float magSqrd = avgNrm.squaredLength();
-
-			if (magSqrd > 0.000001f)
-			{
-				float scale = 1.0f / magSqrd;
-				if (scale > 10.0f) 
-					scale = 10.0f;
-
-				avgNrm.x *= scale;
-				avgNrm.y *= scale;
-			}
-
-			Vector2 tempCoord = points[i] + avgNrm * borderWidth;
-
-			// Move it to the vertex array
-			Vector2* vertices = (Vector2*)outVertices;
-			*vertices = tempCoord;
-
-			UINT32* colors = (UINT32*)outColors;
-			*colors = transparentColor.getAsRGBA();
-
-			outVertices += vertexStride;
-			outColors += vertexStride;
-		}
-
-		// Populate index buffer
-		outIndices += indexOffset;
-
-		UINT32 idxCnt = 0;
-		for(UINT32 i = 0, j = numCoords - 1; i < numCoords; j = i++)
-		{
-			outIndices[idxCnt++] = i;
-			outIndices[idxCnt++] = j;
-			outIndices[idxCnt++] = numCoords + j;
-
-			outIndices[idxCnt++] = numCoords + j;
-			outIndices[idxCnt++] = numCoords + i;
-			outIndices[idxCnt++] = i;
-		}
-		
-		for(UINT32 i = 2; i < numCoords; ++i)
-		{
-			outIndices[idxCnt++] = 0;
-			outIndices[idxCnt++] = i - 1;
-			outIndices[idxCnt++] = i;
-		}
-	}
-
-	void DrawHelper::polygonFill2D_Pixel(const CM::Vector<CM::Vector2>::type& points, CM::UINT8* outVertices, 
-		CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
-	{
-		outVertices += (vertexOffset * vertexStride);
-
-		for(auto& point : points)
-		{
-			Vector2* vertices = (Vector2*)outVertices;
-			(*vertices) = Vector2(point.x, point.y);
-
-			outVertices += vertexStride;
-		}
-
-		outIndices += indexOffset;
-		INT32 numPoints = (INT32)points.size();
-		UINT32 idxCnt = 0;
-		for(int i = 2; i < numPoints; i++)
-		{
-			outIndices[idxCnt++] = vertexOffset;
-			outIndices[idxCnt++] = vertexOffset + i - 1;
-			outIndices[idxCnt++] = vertexOffset + i;
-		}
-	}
-
-	void DrawHelper::polygonBorder2D_Pixel(const CM::Vector<CM::Vector2>::type& points, const CM::Color& color, CM::UINT8* outVertices, CM::UINT8* outColors, 
-		CM::UINT32 vertexOffset, CM::UINT32 vertexStride, CM::UINT32* outIndices, CM::UINT32 indexOffset)
-	{
-		INT32 numPoints = (INT32)points.size();
-		UINT32 curVertOffset = vertexOffset;
-		UINT32 curIdxOffset = indexOffset;
-		for(INT32 i = 0, j = numPoints - 1; i < numPoints; j = i++)
-		{
-			line2D_Pixel(points[j], points[i], color, outVertices, outColors, curVertOffset, vertexStride, outIndices, curIdxOffset);
-			curVertOffset += 2;
-			curIdxOffset += 2;
-		}
-	}
-
-	void DrawHelper::drawQuad2D(const HCamera& camera, const CM::FRect& area, const Color& color, CoordType coordType, float timeout)
-	{
-		const Viewport* viewport = camera->getViewport().get();
-
-		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
-
-		commands.push_back(DebugDrawCommand());
-		DebugDrawCommand& dbgCmd = commands.back();
-		dbgCmd.endTime = gTime().getTime() + timeout;
-
-		MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>(4);
-
-		meshData->beginDesc();
-
-		meshData->addSubMesh(6);
-		meshData->addVertElem(VET_FLOAT2, VES_POSITION);
-		meshData->addVertElem(VET_COLOR, VES_COLOR);
-
-		meshData->endDesc();
-
-		FRect actualArea = area;
-		if(coordType == CoordType::Normalized)
-			actualArea = normalizedCoordToClipSpace(area);
-
-		quad2D(actualArea, meshData, 0, 0);
-
-		UINT32 vertexStride = meshData->getVertexStride();
-		UINT8* colorData = meshData->getElementData(VES_COLOR);
-
-		UINT32* colors = (UINT32*)colorData;
-		(*colors) = color.getAsRGBA();
-
-		colors = (UINT32*)(colorData + vertexStride);
-		(*colors) = color.getAsRGBA();
-
-		colors = (UINT32*)(colorData + vertexStride * 2);
-		(*colors) = color.getAsRGBA();
-
-		colors = (UINT32*)(colorData + vertexStride * 3);
-		(*colors) = color.getAsRGBA();
-
-		HMesh mesh = Mesh::create();
-		gMainSyncedCA().writeSubresource(mesh.getInternalPtr(), 0, *meshData);
-		gMainSyncedCA().submitToCoreThread(true);
-
-		dbgCmd.mesh = mesh;
-		dbgCmd.worldCenter = Vector3::ZERO;
-
-		if(coordType == CoordType::Normalized)
-		{
-			dbgCmd.type = DebugDrawType::ClipSpace;
-			dbgCmd.material = mMaterial2DClipSpace;
-		}
-		else
-		{
-			dbgCmd.type = DebugDrawType::ScreenSpace;
-			dbgCmd.material = BuiltinMaterialManager::instance().createDebugDraw2DScreenSpaceMaterial();
-		}
-	}
-
-	void DrawHelper::drawLine2D_Pixel(const HCamera& camera, const Vector2& a, const Vector2& b, const Color& color, CoordType coordType, float timeout)
-	{
-		const Viewport* viewport = camera->getViewport().get();
-
-		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
-
-		commands.push_back(DebugDrawCommand());
-		DebugDrawCommand& dbgCmd = commands.back();
-		dbgCmd.endTime = gTime().getTime() + timeout;
-
-		MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>(2);
-
-		meshData->beginDesc();
-
-		meshData->addSubMesh(2, 0, DOT_LINE_LIST);
-		meshData->addVertElem(VET_FLOAT2, VES_POSITION);
-		meshData->addVertElem(VET_COLOR, VES_COLOR);
-
-		meshData->endDesc();
-
-		Vector2 actualA = a;
-		Vector2 actualB = b;
-		if(coordType == CoordType::Normalized)
-		{
-			actualA = normalizedCoordToClipSpace(a);
-			actualB = normalizedCoordToClipSpace(b);
-		}
-
-		line2D_Pixel(actualA, actualB, color, meshData, 0, 0);
-
-		HMesh mesh = Mesh::create();
-
-		gMainSyncedCA().writeSubresource(mesh.getInternalPtr(), 0, *meshData);
-		gMainSyncedCA().submitToCoreThread(true);
-
-		dbgCmd.mesh = mesh;
-		dbgCmd.worldCenter = Vector3::ZERO;
-
-		if(coordType == CoordType::Normalized)
-		{
-			dbgCmd.type = DebugDrawType::ClipSpace;
-			dbgCmd.material = mMaterial2DClipSpace;
-		}
-		else
-		{
-			dbgCmd.type = DebugDrawType::ScreenSpace;
-			dbgCmd.material = BuiltinMaterialManager::instance().createDebugDraw2DScreenSpaceMaterial();
-		}
-	}
-
-	void DrawHelper::drawLine2D_AA(const HCamera& camera, const Vector2& a, const Vector2& b, float width, float borderWidth, const Color& color, CoordType coordType, float timeout)
-	{
-		const Viewport* viewport = camera->getViewport().get();
-
-		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
-
-		commands.push_back(DebugDrawCommand());
-		DebugDrawCommand& dbgCmd = commands.back();
-		dbgCmd.endTime = gTime().getTime() + timeout;
-
-		MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>(8);
-
-		meshData->beginDesc();
-
-		meshData->addSubMesh(30, 0, DOT_TRIANGLE_LIST);
-		meshData->addVertElem(VET_FLOAT2, VES_POSITION);
-		meshData->addVertElem(VET_COLOR, VES_COLOR);
-
-		meshData->endDesc();
-
-		Vector2 actualA = a;
-		Vector2 actualB = b;
-		if(coordType == CoordType::Normalized)
-		{
-			actualA = normalizedCoordToClipSpace(a);
-			actualB = normalizedCoordToClipSpace(b);
-		}
-
-		line2D_AA(actualA, actualB, width, borderWidth, color, meshData, 0, 0);
-
-		HMesh mesh = Mesh::create();
-
-		gMainSyncedCA().writeSubresource(mesh.getInternalPtr(), 0, *meshData);
-		gMainSyncedCA().submitToCoreThread(true);
-
-		dbgCmd.mesh = mesh;
-		dbgCmd.worldCenter = Vector3::ZERO;
-
-		if(coordType == CoordType::Normalized)
-		{
-			dbgCmd.type = DebugDrawType::ClipSpace;
-			dbgCmd.material = mMaterial2DClipSpace;
-		}
-		else
-		{
-			dbgCmd.type = DebugDrawType::ScreenSpace;
-			dbgCmd.material = BuiltinMaterialManager::instance().createDebugDraw2DScreenSpaceMaterial();
-		}
-	}
-
-	void DrawHelper::drawLineList2D_Pixel(const HCamera& camera, const Vector<Vector2>::type& linePoints, const Color& color, 
-		CoordType coordType, float timeout)
-	{
-		const Viewport* viewport = camera->getViewport().get();
-
-		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
-
-		commands.push_back(DebugDrawCommand());
-		DebugDrawCommand& dbgCmd = commands.back();
-		dbgCmd.endTime = gTime().getTime() + timeout;
-
-		MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>((UINT32)(linePoints.size() * 2));
-
-		meshData->beginDesc();
-
-		meshData->addSubMesh((UINT32)(linePoints.size() * 2), 0, DOT_LINE_LIST);
-		meshData->addVertElem(VET_FLOAT2, VES_POSITION);
-		meshData->addVertElem(VET_COLOR, VES_COLOR);
-
-		meshData->endDesc();
-
-		if(coordType == CoordType::Normalized)
-		{
-			Vector<Vector2>::type points;
-			UINT32 numPoints = (UINT32)linePoints.size();
-			for(UINT32 i = 0; i < numPoints; i++)
-				points.push_back(normalizedCoordToClipSpace(linePoints[i]));
-
-			lineList2D_Pixel(points, color, meshData, 0, 0);
-		}
-		else
-		{
-			lineList2D_Pixel(linePoints, color, meshData, 0, 0);
-		}		
-
-		HMesh mesh = Mesh::create();
-
-		gMainSyncedCA().writeSubresource(mesh.getInternalPtr(), 0, *meshData);
-		gMainSyncedCA().submitToCoreThread(true);
-
-		dbgCmd.mesh = mesh;
-		dbgCmd.worldCenter = Vector3::ZERO;
-
-		if(coordType == CoordType::Normalized)
-		{
-			dbgCmd.type = DebugDrawType::ClipSpace;
-			dbgCmd.material = mMaterial2DClipSpace;
-		}
-		else
-		{
-			dbgCmd.type = DebugDrawType::ScreenSpace;
-			dbgCmd.material = BuiltinMaterialManager::instance().createDebugDraw2DScreenSpaceMaterial();
-		}
-	}
-
-	void DrawHelper::drawLineList2D_AA(const HCamera& camera, const CM::Vector<CM::Vector2>::type& linePoints, float width, float borderWidth, 
-		const CM::Color& color, CoordType coordType, float timeout)
-	{
-		const Viewport* viewport = camera->getViewport().get();
-
-		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
-
-		commands.push_back(DebugDrawCommand());
-		DebugDrawCommand& dbgCmd = commands.back();
-		dbgCmd.endTime = gTime().getTime() + timeout;
-
-		MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>((UINT32)(linePoints.size() * 4));
-
-		meshData->beginDesc();
-
-		meshData->addSubMesh((UINT32)(linePoints.size() * 15), 0, DOT_TRIANGLE_LIST);
-		meshData->addVertElem(VET_FLOAT2, VES_POSITION);
-		meshData->addVertElem(VET_COLOR, VES_COLOR);
-
-		meshData->endDesc();
-
-		if(coordType == CoordType::Normalized)
-		{
-			Vector<Vector2>::type points;
-			UINT32 numPoints = (UINT32)linePoints.size();
-			for(UINT32 i = 0; i < numPoints; i++)
-				points.push_back(normalizedCoordToClipSpace(linePoints[i]));
-
-			lineList2D_AA(points, width, borderWidth, color, meshData, 0, 0);
-		}
-		else
-		{
-			lineList2D_AA(linePoints, width, borderWidth, color, meshData, 0, 0);
-		}		
-
-		HMesh mesh = Mesh::create();
-
-		gMainSyncedCA().writeSubresource(mesh.getInternalPtr(), 0, *meshData);
-		gMainSyncedCA().submitToCoreThread(true);
-
-		dbgCmd.mesh = mesh;
-		dbgCmd.worldCenter = Vector3::ZERO;
-
-		if(coordType == CoordType::Normalized)
-		{
-			dbgCmd.type = DebugDrawType::ClipSpace;
-			dbgCmd.material = mMaterial2DClipSpace;
-		}
-		else
-		{
-			dbgCmd.type = DebugDrawType::ScreenSpace;
-			dbgCmd.material = BuiltinMaterialManager::instance().createDebugDraw2DScreenSpaceMaterial();
-		}
-	}
-
-	void DrawHelper::render(const HCamera& camera, CM::RenderQueue& renderQueue)
-	{
-		const Viewport* viewport = camera->getViewport().get();
-		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
-
-		Matrix4 projMatrixCstm = camera->getProjectionMatrix();
-		Matrix4 viewMatrixCstm = camera->getViewMatrix();
-		Matrix4 viewProjMatrix = projMatrixCstm * viewMatrixCstm;
-
-		float invViewportWidth = 1.0f / (viewport->getWidth() * 0.5f);
-		float invViewportHeight = 1.0f / (viewport->getHeight() * 0.5f);
-
-		for(auto& cmd : commands)
-		{
-			if(cmd.mesh == nullptr || !cmd.mesh.isLoaded() || !cmd.mesh->isInitialized())
-				continue;
-
-			if(cmd.material == nullptr || !cmd.material.isLoaded() || !cmd.material->isInitialized())
-				continue;
-
-			if(cmd.type == DebugDrawType::ClipSpace)
-			{
-				renderQueue.add(cmd.material, cmd.mesh->getSubMeshData(), cmd.worldCenter);
-			}
-			else if(cmd.type == DebugDrawType::ScreenSpace)
-			{
-				cmd.material->setFloat("invViewportWidth", invViewportWidth);
-				cmd.material->setFloat("invViewportHeight", invViewportHeight);
-
-				renderQueue.add(cmd.material, cmd.mesh->getSubMeshData(), cmd.worldCenter);
-			}
-			else if(cmd.type == DebugDrawType::WorldSpace)
-			{
-				cmd.material->setMat4("matViewProj", viewProjMatrix);
-
-				renderQueue.add(cmd.material, cmd.mesh->getSubMeshData(), cmd.worldCenter);
-			}
-		}
-
-		float curTime = gTime().getTime();
-		Vector<DebugDrawCommand>::type newCommands;
-		for(auto& cmd : commands)
-		{
-			if(cmd.endTime > curTime)
-				newCommands.push_back(cmd);
-		}
-
-		commands.swap(newCommands);
-	}
-
-	FRect DrawHelper::normalizedCoordToClipSpace(const FRect& area) const
-	{
-		FRect clipSpaceRect;
-		clipSpaceRect.x = area.x * 2.0f - 1.0f;
-		clipSpaceRect.width = area.width * 2.0f;
-		clipSpaceRect.y = -area.y * 2.0f + 1.0f;
-		clipSpaceRect.height = area.height * -2.0f;
-
-		return clipSpaceRect;
-	}
-
-	Vector2 DrawHelper::normalizedCoordToClipSpace(const Vector2& pos) const
-	{
-		return Vector2(pos.x * 2.0f - 1.0f, -pos.y * 2.0f + 1.0f);
-	}
-}

+ 348 - 0
BansheeEngine/Source/BsDrawHelper2D.cpp

@@ -0,0 +1,348 @@
+#include "BsDrawHelper2D.h"
+#include "CmFRect.h"
+#include "CmMesh.h"
+#include "CmTime.h"
+#include "CmVector2.h"
+#include "CmMaterial.h"
+#include "CmPass.h"
+#include "CmApplication.h"
+#include "CmRenderQueue.h"
+#include "BsCamera.h"
+#include "BsBuiltinMaterialManager.h"
+
+using namespace CamelotFramework;
+
+namespace BansheeEngine
+{
+	DrawHelper2D::DrawHelper2D()
+	{
+		mMaterial2DClipSpace = BuiltinMaterialManager::instance().createDebugDraw2DClipSpaceMaterial();
+	}
+
+	void DrawHelper2D::quad(const CM::FRect& area, const MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
+	{
+		UINT32* indexData = meshData->getIndices32();
+		UINT8* positionData = meshData->getElementData(VES_POSITION);
+
+		assert((vertexOffset + 4) <= meshData->getNumVertices());
+		assert((indexOffset + 6) <= meshData->getNumIndices());
+
+		Vector<Vector2>::type points;
+		points.push_back(Vector2(area.x, area.y));
+		points.push_back(Vector2(area.x + area.width, area.y));
+		points.push_back(Vector2(area.x + area.width, area.y + area.height));
+		points.push_back(Vector2(area.x, area.y + area.height));	
+
+		polygonFill_Pixel(points, positionData, vertexOffset, meshData->getVertexStride(), indexData, indexOffset);
+	}
+
+	void DrawHelper2D::line_Pixel(const Vector2& a, const Vector2& b, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
+	{
+		DrawHelperTemplate<Vector2>::line_Pixel(a, b, color, meshData, vertexOffset, indexOffset);
+	}
+
+	void DrawHelper2D::line_AA(const Vector2& a, const Vector2& b, float width, float borderWidth, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
+	{
+		DrawHelperTemplate<Vector2>::line_AA(a, b, width, borderWidth, color, meshData, vertexOffset, indexOffset);
+	}
+
+	void DrawHelper2D::lineList_Pixel(const CM::Vector<Vector2>::type& linePoints, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
+	{
+		DrawHelperTemplate<Vector2>::lineList_Pixel(linePoints, color, meshData, vertexOffset, indexOffset);
+	}
+
+	void DrawHelper2D::lineList_AA(const CM::Vector<Vector2>::type& linePoints, float width, float borderWidth, const CM::Color& color, const CM::MeshDataPtr& meshData, CM::UINT32 vertexOffset, CM::UINT32 indexOffset)
+	{
+		DrawHelperTemplate<Vector2>::lineList_AA(linePoints, width, borderWidth, color, meshData, vertexOffset, indexOffset);
+	}
+
+	/************************************************************************/
+	/* 								2D - DRAW	                     		*/
+	/************************************************************************/
+
+	void DrawHelper2D::drawQuad(const HCamera& camera, const CM::FRect& area, const Color& color, DebugDrawCoordType coordType, float timeout)
+	{
+		const Viewport* viewport = camera->getViewport().get();
+
+		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
+
+		commands.push_back(DebugDrawCommand());
+		DebugDrawCommand& dbgCmd = commands.back();
+		dbgCmd.endTime = gTime().getTime() + timeout;
+
+		MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>(4);
+
+		meshData->beginDesc();
+
+		meshData->addSubMesh(6);
+		meshData->addVertElem(VET_FLOAT2, VES_POSITION);
+		meshData->addVertElem(VET_COLOR, VES_COLOR);
+
+		meshData->endDesc();
+
+		FRect actualArea = area;
+		if(coordType == DebugDrawCoordType::Normalized)
+			actualArea = normalizedCoordToClipSpace(area);
+
+		quad(actualArea, meshData, 0, 0);
+
+		UINT32 vertexStride = meshData->getVertexStride();
+		UINT8* colorData = meshData->getElementData(VES_COLOR);
+
+		UINT32* colors = (UINT32*)colorData;
+		(*colors) = color.getAsRGBA();
+
+		colors = (UINT32*)(colorData + vertexStride);
+		(*colors) = color.getAsRGBA();
+
+		colors = (UINT32*)(colorData + vertexStride * 2);
+		(*colors) = color.getAsRGBA();
+
+		colors = (UINT32*)(colorData + vertexStride * 3);
+		(*colors) = color.getAsRGBA();
+
+		HMesh mesh = Mesh::create();
+		gMainSyncedCA().writeSubresource(mesh.getInternalPtr(), 0, *meshData);
+		gMainSyncedCA().submitToCoreThread(true);
+
+		dbgCmd.mesh = mesh;
+		dbgCmd.worldCenter = Vector3::ZERO;
+
+		if(coordType == DebugDrawCoordType::Normalized)
+		{
+			dbgCmd.type = DebugDrawType::ClipSpace;
+			dbgCmd.material = mMaterial2DClipSpace;
+		}
+		else
+		{
+			dbgCmd.type = DebugDrawType::ScreenSpace;
+			dbgCmd.material = BuiltinMaterialManager::instance().createDebugDraw2DScreenSpaceMaterial();
+		}
+	}
+
+	void DrawHelper2D::drawLine_Pixel(const HCamera& camera, const Vector2& a, const Vector2& b, const Color& color, DebugDrawCoordType coordType, float timeout)
+	{
+		const Viewport* viewport = camera->getViewport().get();
+
+		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
+
+		commands.push_back(DebugDrawCommand());
+		DebugDrawCommand& dbgCmd = commands.back();
+		dbgCmd.endTime = gTime().getTime() + timeout;
+
+		MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>(2);
+
+		meshData->beginDesc();
+
+		meshData->addSubMesh(2, 0, DOT_LINE_LIST);
+		meshData->addVertElem(VET_FLOAT2, VES_POSITION);
+		meshData->addVertElem(VET_COLOR, VES_COLOR);
+
+		meshData->endDesc();
+
+		Vector2 actualA = a;
+		Vector2 actualB = b;
+		if(coordType == DebugDrawCoordType::Normalized)
+		{
+			actualA = normalizedCoordToClipSpace(a);
+			actualB = normalizedCoordToClipSpace(b);
+		}
+
+		line_Pixel(actualA, actualB, color, meshData, 0, 0);
+
+		HMesh mesh = Mesh::create();
+
+		gMainSyncedCA().writeSubresource(mesh.getInternalPtr(), 0, *meshData);
+		gMainSyncedCA().submitToCoreThread(true);
+
+		dbgCmd.mesh = mesh;
+		dbgCmd.worldCenter = Vector3::ZERO;
+
+		if(coordType == DebugDrawCoordType::Normalized)
+		{
+			dbgCmd.type = DebugDrawType::ClipSpace;
+			dbgCmd.material = mMaterial2DClipSpace;
+		}
+		else
+		{
+			dbgCmd.type = DebugDrawType::ScreenSpace;
+			dbgCmd.material = BuiltinMaterialManager::instance().createDebugDraw2DScreenSpaceMaterial();
+		}
+	}
+
+	void DrawHelper2D::drawLine_AA(const HCamera& camera, const Vector2& a, const Vector2& b, float width, float borderWidth, const Color& color, DebugDrawCoordType coordType, float timeout)
+	{
+		const Viewport* viewport = camera->getViewport().get();
+
+		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
+
+		commands.push_back(DebugDrawCommand());
+		DebugDrawCommand& dbgCmd = commands.back();
+		dbgCmd.endTime = gTime().getTime() + timeout;
+
+		MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>(8);
+
+		meshData->beginDesc();
+
+		meshData->addSubMesh(30, 0, DOT_TRIANGLE_LIST);
+		meshData->addVertElem(VET_FLOAT2, VES_POSITION);
+		meshData->addVertElem(VET_COLOR, VES_COLOR);
+
+		meshData->endDesc();
+
+		Vector2 actualA = a;
+		Vector2 actualB = b;
+		if(coordType == DebugDrawCoordType::Normalized)
+		{
+			actualA = normalizedCoordToClipSpace(a);
+			actualB = normalizedCoordToClipSpace(b);
+		}
+
+		line_AA(actualA, actualB, width, borderWidth, color, meshData, 0, 0);
+
+		HMesh mesh = Mesh::create();
+
+		gMainSyncedCA().writeSubresource(mesh.getInternalPtr(), 0, *meshData);
+		gMainSyncedCA().submitToCoreThread(true);
+
+		dbgCmd.mesh = mesh;
+		dbgCmd.worldCenter = Vector3::ZERO;
+
+		if(coordType == DebugDrawCoordType::Normalized)
+		{
+			dbgCmd.type = DebugDrawType::ClipSpace;
+			dbgCmd.material = mMaterial2DClipSpace;
+		}
+		else
+		{
+			dbgCmd.type = DebugDrawType::ScreenSpace;
+			dbgCmd.material = BuiltinMaterialManager::instance().createDebugDraw2DScreenSpaceMaterial();
+		}
+	}
+
+	void DrawHelper2D::drawLineList_Pixel(const HCamera& camera, const Vector<Vector2>::type& linePoints, const Color& color, 
+		DebugDrawCoordType coordType, float timeout)
+	{
+		const Viewport* viewport = camera->getViewport().get();
+
+		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
+
+		commands.push_back(DebugDrawCommand());
+		DebugDrawCommand& dbgCmd = commands.back();
+		dbgCmd.endTime = gTime().getTime() + timeout;
+
+		MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>((UINT32)(linePoints.size() * 2));
+
+		meshData->beginDesc();
+
+		meshData->addSubMesh((UINT32)(linePoints.size() * 2), 0, DOT_LINE_LIST);
+		meshData->addVertElem(VET_FLOAT2, VES_POSITION);
+		meshData->addVertElem(VET_COLOR, VES_COLOR);
+
+		meshData->endDesc();
+
+		if(coordType == DebugDrawCoordType::Normalized)
+		{
+			Vector<Vector2>::type points;
+			UINT32 numPoints = (UINT32)linePoints.size();
+			for(UINT32 i = 0; i < numPoints; i++)
+				points.push_back(normalizedCoordToClipSpace(linePoints[i]));
+
+			lineList_Pixel(points, color, meshData, 0, 0);
+		}
+		else
+		{
+			lineList_Pixel(linePoints, color, meshData, 0, 0);
+		}		
+
+		HMesh mesh = Mesh::create();
+
+		gMainSyncedCA().writeSubresource(mesh.getInternalPtr(), 0, *meshData);
+		gMainSyncedCA().submitToCoreThread(true);
+
+		dbgCmd.mesh = mesh;
+		dbgCmd.worldCenter = Vector3::ZERO;
+
+		if(coordType == DebugDrawCoordType::Normalized)
+		{
+			dbgCmd.type = DebugDrawType::ClipSpace;
+			dbgCmd.material = mMaterial2DClipSpace;
+		}
+		else
+		{
+			dbgCmd.type = DebugDrawType::ScreenSpace;
+			dbgCmd.material = BuiltinMaterialManager::instance().createDebugDraw2DScreenSpaceMaterial();
+		}
+	}
+
+	void DrawHelper2D::drawLineList_AA(const HCamera& camera, const CM::Vector<CM::Vector2>::type& linePoints, float width, float borderWidth, 
+		const CM::Color& color, DebugDrawCoordType coordType, float timeout)
+	{
+		const Viewport* viewport = camera->getViewport().get();
+
+		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
+
+		commands.push_back(DebugDrawCommand());
+		DebugDrawCommand& dbgCmd = commands.back();
+		dbgCmd.endTime = gTime().getTime() + timeout;
+
+		MeshDataPtr meshData = cm_shared_ptr<MeshData, ScratchAlloc>((UINT32)(linePoints.size() * 4));
+
+		meshData->beginDesc();
+
+		meshData->addSubMesh((UINT32)(linePoints.size() * 15), 0, DOT_TRIANGLE_LIST);
+		meshData->addVertElem(VET_FLOAT2, VES_POSITION);
+		meshData->addVertElem(VET_COLOR, VES_COLOR);
+
+		meshData->endDesc();
+
+		if(coordType == DebugDrawCoordType::Normalized)
+		{
+			Vector<Vector2>::type points;
+			UINT32 numPoints = (UINT32)linePoints.size();
+			for(UINT32 i = 0; i < numPoints; i++)
+				points.push_back(normalizedCoordToClipSpace(linePoints[i]));
+
+			lineList_AA(points, width, borderWidth, color, meshData, 0, 0);
+		}
+		else
+		{
+			lineList_AA(linePoints, width, borderWidth, color, meshData, 0, 0);
+		}		
+
+		HMesh mesh = Mesh::create();
+
+		gMainSyncedCA().writeSubresource(mesh.getInternalPtr(), 0, *meshData);
+		gMainSyncedCA().submitToCoreThread(true);
+
+		dbgCmd.mesh = mesh;
+		dbgCmd.worldCenter = Vector3::ZERO;
+
+		if(coordType == DebugDrawCoordType::Normalized)
+		{
+			dbgCmd.type = DebugDrawType::ClipSpace;
+			dbgCmd.material = mMaterial2DClipSpace;
+		}
+		else
+		{
+			dbgCmd.type = DebugDrawType::ScreenSpace;
+			dbgCmd.material = BuiltinMaterialManager::instance().createDebugDraw2DScreenSpaceMaterial();
+		}
+	}
+
+	FRect DrawHelper2D::normalizedCoordToClipSpace(const FRect& area) const
+	{
+		FRect clipSpaceRect;
+		clipSpaceRect.x = area.x * 2.0f - 1.0f;
+		clipSpaceRect.width = area.width * 2.0f;
+		clipSpaceRect.y = -area.y * 2.0f + 1.0f;
+		clipSpaceRect.height = area.height * -2.0f;
+
+		return clipSpaceRect;
+	}
+
+	Vector2 DrawHelper2D::normalizedCoordToClipSpace(const Vector2& pos) const
+	{
+		return Vector2(pos.x * 2.0f - 1.0f, -pos.y * 2.0f + 1.0f);
+	}
+}

+ 66 - 0
BansheeEngine/Source/BsDrawHelperTemplate.cpp

@@ -0,0 +1,66 @@
+#include "BsDrawHelperTemplate.h"
+#include "CmFRect.h"
+#include "CmMesh.h"
+#include "CmTime.h"
+#include "CmVector2.h"
+#include "CmMaterial.h"
+#include "CmPass.h"
+#include "CmApplication.h"
+#include "CmRenderQueue.h"
+#include "BsCamera.h"
+#include "BsBuiltinMaterialManager.h"
+
+using namespace CamelotFramework;
+
+namespace BansheeEngine
+{
+	void DrawHelperTemplateBase::render(const HCamera& camera, CM::RenderQueue& renderQueue)
+	{
+		const Viewport* viewport = camera->getViewport().get();
+		Vector<DebugDrawCommand>::type& commands = mCommandsPerViewport[viewport];
+
+		Matrix4 projMatrixCstm = camera->getProjectionMatrix();
+		Matrix4 viewMatrixCstm = camera->getViewMatrix();
+		Matrix4 viewProjMatrix = projMatrixCstm * viewMatrixCstm;
+
+		float invViewportWidth = 1.0f / (viewport->getWidth() * 0.5f);
+		float invViewportHeight = 1.0f / (viewport->getHeight() * 0.5f);
+
+		for(auto& cmd : commands)
+		{
+			if(cmd.mesh == nullptr || !cmd.mesh.isLoaded() || !cmd.mesh->isInitialized())
+				continue;
+
+			if(cmd.material == nullptr || !cmd.material.isLoaded() || !cmd.material->isInitialized())
+				continue;
+
+			if(cmd.type == DebugDrawType::ClipSpace)
+			{
+				renderQueue.add(cmd.material, cmd.mesh->getSubMeshData(), cmd.worldCenter);
+			}
+			else if(cmd.type == DebugDrawType::ScreenSpace)
+			{
+				cmd.material->setFloat("invViewportWidth", invViewportWidth);
+				cmd.material->setFloat("invViewportHeight", invViewportHeight);
+
+				renderQueue.add(cmd.material, cmd.mesh->getSubMeshData(), cmd.worldCenter);
+			}
+			else if(cmd.type == DebugDrawType::WorldSpace)
+			{
+				cmd.material->setMat4("matViewProj", viewProjMatrix);
+
+				renderQueue.add(cmd.material, cmd.mesh->getSubMeshData(), cmd.worldCenter);
+			}
+		}
+
+		float curTime = gTime().getTime();
+		Vector<DebugDrawCommand>::type newCommands;
+		for(auto& cmd : commands)
+		{
+			if(cmd.endTime > curTime)
+				newCommands.push_back(cmd);
+		}
+
+		commands.swap(newCommands);
+	}
+}

+ 2 - 2
BansheeForwardRenderer/Source/BsForwardRenderer.cpp

@@ -14,7 +14,7 @@
 #include "CmRenderOperation.h"
 #include "CmDefaultRenderQueue.h"
 #include "BsOverlayManager.h"
-#include "BsDrawHelper.h"
+#include "BsDrawHelper2D.h"
 #include "BsGUIManager.h"
 
 using namespace CamelotFramework;
@@ -156,7 +156,7 @@ namespace BansheeEngine
 		OverlayManager::instance().render(camera->getViewport(), *mRenderQueue);
 
 		// Get debug render operations
-		DrawHelper::instance().render(camera, *mRenderQueue);
+		DrawHelper2D::instance().render(camera, *mRenderQueue);
 
 		// TODO - Material queue is completely ignored
 		mRenderQueue->sort();

+ 5 - 5
CamelotClient/Source/BsMainEditorWindow.cpp

@@ -7,7 +7,7 @@
 // DEBUG ONLY
 #include "CmTestTextSprite.h"
 #include "CmDebugCamera.h"
-#include "BsDrawHelper.h"
+#include "BsDrawHelper2D.h"
 #include "CmFRect.h"
 
 using namespace CamelotFramework;
@@ -39,12 +39,12 @@ namespace BansheeEditor
 
 		textSprite->init(sceneCamera, "Testing in a new row, does this work?", nullptr);
 
-		DrawHelper::instance().drawQuad2D(sceneCamera, FRect(0.0f, 0.2f, 0.75f, 0.5f), Color::White, CoordType::Normalized, 250.0f);
-		DrawHelper::instance().drawQuad2D(sceneCamera, FRect(50.0f, 50.0f, 100.0f, 50.0f), Color::Blue, CoordType::Pixel, 250.0f);
+		DrawHelper2D::instance().drawQuad(sceneCamera, FRect(0.0f, 0.2f, 0.75f, 0.5f), Color::White, DebugDrawCoordType::Normalized, 250.0f);
+		DrawHelper2D::instance().drawQuad(sceneCamera, FRect(50.0f, 50.0f, 100.0f, 50.0f), Color::Blue, DebugDrawCoordType::Pixel, 250.0f);
 
-		DrawHelper::instance().drawLine2D_Pixel(sceneCamera, Vector2(0, 0), Vector2(20, 20), Color::Blue, CoordType::Pixel, 250.0f);
+		DrawHelper2D::instance().drawLine_Pixel(sceneCamera, Vector2(0, 0), Vector2(20, 20), Color::Blue, DebugDrawCoordType::Pixel, 250.0f);
 
-		DrawHelper::instance().drawLine2D_AA(sceneCamera, Vector2(100, 10), Vector2(120, 40), 1.0f, 1.0f, Color::Blue, CoordType::Pixel, 250.0f);
+		DrawHelper2D::instance().drawLine_AA(sceneCamera, Vector2(100, 10), Vector2(120, 40), 1.0f, 1.0f, Color::Blue, DebugDrawCoordType::Pixel, 250.0f);
 	}
 
 	MainEditorWindow::~MainEditorWindow()