Panagiotis Christopoulos Charitos il y a 13 ans
Parent
commit
f7878d47df

+ 9 - 9
include/anki/renderer/DebugDrawer.h

@@ -8,8 +8,8 @@
 #include "anki/collision/CollisionShape.h"
 #include "anki/scene/SceneNode.h"
 #include "anki/util/Flags.h"
-#include <array>
-#include <map>
+#include "anki/util/Array.h"
+#include <unordered_map>
 #include <LinearMath/btIDebugDraw.h>
 
 namespace anki {
@@ -22,8 +22,8 @@ public:
 	~DebugDrawer();
 
 	void drawGrid();
-	void drawSphere(float radius, int complexity = 4);
-	void drawCube(float size = 1.0);
+	void drawSphere(F32 radius, int complexity = 4);
+	void drawCube(F32 size = 1.0);
 	void drawLine(const Vec3& from, const Vec3& to, const Vec4& color);
 
 	/// @name Render functions. Imitate the GL 1.1 immediate mode
@@ -50,12 +50,12 @@ public:
 
 private:
 	ShaderProgramResourcePointer sProg;
-	static const uint MAX_POINTS_PER_DRAW = 256;
-	std::array<Vec3, MAX_POINTS_PER_DRAW> positions;
-	std::array<Vec3, MAX_POINTS_PER_DRAW> colors;
+	static const U MAX_POINTS_PER_DRAW = 256;
+	Array<Vec3, MAX_POINTS_PER_DRAW> positions;
+	Array<Vec3, MAX_POINTS_PER_DRAW> colors;
 	Mat4 modelMat;
 	Mat4 vpMat;
-	uint pointIndex;
+	U pointIndex;
 	Vec3 crntCol;
 	Vbo positionsVbo;
 	Vbo colorsVbo;
@@ -64,7 +64,7 @@ private:
 	/// This is a container of some precalculated spheres. Its a map that
 	/// from sphere complexity it returns a vector of lines (Vec3s in
 	/// pairs)
-	std::map<uint, Vector<Vec3>> complexityToPreCalculatedSphere;
+	std::unordered_map<U32, Vector<Vec3>> complexityToPreCalculatedSphere;
 };
 
 /// Contains methods to render the collision shapes

+ 2 - 0
include/anki/renderer/Drawer.h

@@ -27,6 +27,8 @@ private:
 		const PassLevelKey& key,
 		const Frustumable& fr,
 		Renderable& renderable);
+
+	void setBuildinIds(Renderable& renderable);
 };
 
 } // end namespace anki

+ 40 - 39
src/renderer/DebugDrawer.cpp

@@ -1,4 +1,4 @@
-#include "anki/renderer/Drawer.h"
+#include "anki/renderer/DebugDrawer.h"
 #include "anki/resource/ShaderProgramResource.h"
 #include "anki/physics/Convertors.h"
 #include "anki/collision/Collision.h"
@@ -59,10 +59,10 @@ void DebugDrawer::drawGrid()
 	Vec4 col1(0.0, 0.0, 1.0, 1.0);
 	Vec4 col2(1.0, 0.0, 0.0, 1.0);
 
-	const float SPACE = 1.0; // space between lines
+	const F32 SPACE = 1.0; // space between lines
 	const int NUM = 57;  // lines number. must be odd
 
-	const float GRID_HALF_SIZE = ((NUM - 1) * SPACE / 2);
+	const F32 GRID_HALF_SIZE = ((NUM - 1) * SPACE / 2);
 
 	setColor(col0);
 
@@ -98,13 +98,13 @@ void DebugDrawer::drawGrid()
 }
 
 //==============================================================================
-void DebugDrawer::drawSphere(float radius, int complexity)
+void DebugDrawer::drawSphere(F32 radius, int complexity)
 {
 	Vector<Vec3>* sphereLines;
 
 	// Pre-calculate the sphere points5
 	//
-	std::map<uint, Vector<Vec3> >::iterator it =
+	std::unordered_map<U32, Vector<Vec3>>::iterator it =
 		complexityToPreCalculatedSphere.find(complexity);
 
 	if(it != complexityToPreCalculatedSphere.end()) // Found
@@ -116,14 +116,14 @@ void DebugDrawer::drawSphere(float radius, int complexity)
 		complexityToPreCalculatedSphere[complexity] = Vector<Vec3>();
 		sphereLines = &complexityToPreCalculatedSphere[complexity];
 
-		float fi = getPi<F32>() / complexity;
+		F32 fi = getPi<F32>() / complexity;
 
 		Vec3 prev(1.0, 0.0, 0.0);
-		for(float th = fi; th < getPi<F32>() * 2.0 + fi; th += fi)
+		for(F32 th = fi; th < getPi<F32>() * 2.0 + fi; th += fi)
 		{
 			Vec3 p = Mat3(Euler(0.0, th, 0.0)) * Vec3(1.0, 0.0, 0.0);
 
-			for(float th2 = 0.0; th2 < getPi<F32>(); th2 += fi)
+			for(F32 th2 = 0.0; th2 < getPi<F32>(); th2 += fi)
 			{
 				Mat3 rot(Euler(th2, 0.0, 0.0));
 
@@ -162,12 +162,12 @@ void DebugDrawer::drawSphere(float radius, int complexity)
 }
 
 //==============================================================================
-void DebugDrawer::drawCube(float size)
+void DebugDrawer::drawCube(F32 size)
 {
 	Vec3 maxPos = Vec3(0.5 * size);
 	Vec3 minPos = Vec3(-0.5 * size);
 
-	std::array<Vec3, 8> points = {{
+	Array<Vec3, 8> points = {{
 		Vec3(maxPos.x(), maxPos.y(), maxPos.z()),  // right top front
 		Vec3(minPos.x(), maxPos.y(), maxPos.z()),  // left top front
 		Vec3(minPos.x(), minPos.y(), maxPos.z()),  // left bottom front
@@ -178,7 +178,7 @@ void DebugDrawer::drawCube(float size)
 		Vec3(maxPos.x(), minPos.y(), minPos.z())   // right bottom back
 	}};
 
-	std::array<uint, 24> indeces = {{0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6,
+	Array<uint, 24> indeces = {{0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6,
 		7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7}};
 
 	begin();
@@ -267,7 +267,7 @@ void CollisionDebugDrawer::visit(const Obb& obb)
 void CollisionDebugDrawer::visit(const Plane& plane)
 {
 	const Vec3& n = plane.getNormal();
-	const float& o = plane.getOffset();
+	const F32& o = plane.getOffset();
 	Quat q;
 	q.setFrom2Vec3(Vec3(0.0, 0.0, 1.0), n);
 	Mat3 rot(q);
@@ -307,34 +307,35 @@ void CollisionDebugDrawer::visit(const Frustum& f)
 		visit(static_cast<const OrthographicFrustum&>(f).getObb());
 		break;
 	case Frustum::FT_PERSPECTIVE:
-	{
-		dbg->setColor(Vec4(0.5, 0.0, 0.5, 1.0));
-		const PerspectiveFrustum& pf =
-			static_cast<const PerspectiveFrustum&>(f);
-
-		float camLen = pf.getFar();
-		float tmp0 = camLen / tan((getPi<F32>() - pf.getFovX()) * 0.5) + 0.001;
-		float tmp1 = camLen * tan(pf.getFovY() * 0.5) + 0.001;
-
-		Vec3 points[] = {
-			Vec3(0.0, 0.0, 0.0), // 0: eye point
-			Vec3(-tmp0, tmp1, -camLen), // 1: top left
-			Vec3(-tmp0, -tmp1, -camLen), // 2: bottom left
-			Vec3(tmp0, -tmp1, -camLen), // 3: bottom right
-			Vec3(tmp0, tmp1, -camLen) // 4: top right
-		};
-
-		const uint indeces[] = {0, 1, 0, 2, 0, 3, 0, 4, 1, 2, 2,
-			3, 3, 4, 4, 1};
-
-		dbg->begin();
-		for(uint i = 0; i < sizeof(indeces) / sizeof(uint); i++)
 		{
-			dbg->pushBackVertex(points[indeces[i]]);
+			dbg->setColor(Vec4(0.5, 0.0, 0.5, 1.0));
+			const PerspectiveFrustum& pf =
+				static_cast<const PerspectiveFrustum&>(f);
+
+			F32 camLen = pf.getFar();
+			F32 tmp0 = camLen / tan((getPi<F32>() - pf.getFovX()) * 0.5) 
+				+ 0.001;
+			F32 tmp1 = camLen * tan(pf.getFovY() * 0.5) + 0.001;
+
+			Vec3 points[] = {
+				Vec3(0.0, 0.0, 0.0), // 0: eye point
+				Vec3(-tmp0, tmp1, -camLen), // 1: top left
+				Vec3(-tmp0, -tmp1, -camLen), // 2: bottom left
+				Vec3(tmp0, -tmp1, -camLen), // 3: bottom right
+				Vec3(tmp0, tmp1, -camLen) // 4: top right
+			};
+
+			const uint indeces[] = {0, 1, 0, 2, 0, 3, 0, 4, 1, 2, 2,
+				3, 3, 4, 4, 1};
+
+			dbg->begin();
+			for(uint i = 0; i < sizeof(indeces) / sizeof(uint); i++)
+			{
+				dbg->pushBackVertex(points[indeces[i]]);
+			}
+			dbg->end();
+			break;
 		}
-		dbg->end();
-		break;
-	}
 	}
 }
 
@@ -484,7 +485,7 @@ void SceneDebugDrawer::draw(const OctreeNode& octnode, U32 depth,
 	// Draw if it has spatials
 	if(octnode.getSceneNodesCount() != 0)
 	{
-		//Vec3 color = Vec3(1.0 - float(depth) / float(octree.getMaxDepth()));
+		//Vec3 color = Vec3(1.0 - F32(depth) / F32(octree.getMaxDepth()));
 		Vec3 color(1.0);
 		dbg->setColor(color);
 

+ 83 - 67
src/renderer/Drawer.cpp

@@ -11,7 +11,6 @@
 namespace anki {
 
 //==============================================================================
-
 enum BuildinId
 {
 	BI_UNITIALIZED = 0,
@@ -30,41 +29,47 @@ static Array<const char*, BI_COUNT - 2> buildinNames = {{
 	"blurring"
 }};
 
-static const UNIFORM_BLOCK_MAX_SIZE = 256;
-
-template<typename T>
-static void uniSet(const ShaderProgramUniformVariable& uni, const T& x)
+struct SetBuildinIdVisitor
 {
-	(void)uni;
-	(void)x;
-	ANKI_ASSERT(0);
-}
+	Bool setted;
 
-#define TEMPLATE_SPECIALIZATION(type) \
-	template<> \
-	void uniSet<type>(const ShaderProgramUniformVariable& uni, const type& x) \
-	{ \
-		uni.set(x); \
-	}
+	template<typename TProp>
+	void visit(TProp& x)
+	{
+		MaterialVariableProperty<typename TProp::Value>& mprop =
+			static_cast<MaterialVariableProperty<typename TProp::Value>&>(x);
 
-TEMPLATE_SPECIALIZATION(float)
-TEMPLATE_SPECIALIZATION(Vec2)
-TEMPLATE_SPECIALIZATION(Vec3)
-TEMPLATE_SPECIALIZATION(Vec4)
-TEMPLATE_SPECIALIZATION(Mat3)
-TEMPLATE_SPECIALIZATION(Mat4)
+		const MaterialVariable& mv = mprop.getMaterialVariable();
 
-// Texture specialization
-template<>
-void uniSet<TextureResourcePointer>(
-	const ShaderProgramUniformVariable& uni,
-	const TextureResourcePointer& x)
-{
-	const Texture* tex = x.get();
-	uni.set(*tex);
-}
+		if(mprop.getBuildinId() == BI_UNITIALIZED)
+		{
+			const std::string& name = mv.getName();
 
-/// XXX
+			for(U i = 0; i < buildinNames.size(); i++)
+			{
+				if(name == buildinNames[i])
+				{
+					mprop.setBuildinId(i + 2);
+					break;
+				}
+			}
+
+			if(mprop.getBuildinId() == BI_UNITIALIZED)
+			{
+				mprop.setBuildinId(BT_NO_BUILDIN);
+			}
+
+			setted = true;
+		}
+
+		setted = false
+	}
+};
+
+//==============================================================================
+static const UNIFORM_BLOCK_MAX_SIZE = 256;
+
+/// Visitor that sets a uniform
 struct SetupMaterialVariableVisitor
 {
 	PassLevelKey key;
@@ -73,6 +78,13 @@ struct SetupMaterialVariableVisitor
 	Renderable* renderable = nullptr;
 	Array<U8, UNIFORM_BLOCK_MAX_SIZE> clientBlock;
 
+	/// Set a uniform in a client block
+	template<typename T>
+	static void uniSet(const ShaderProgramUniformVariable& uni, const T& value)
+	{
+		uni.setClientMemory(&clientBlock[0], UNIFORM_BLOCK_MAX_SIZE, &value, 1);
+	}
+
 	template<typename TProp>
 	void visit(TProp& x)
 	{
@@ -89,34 +101,14 @@ struct SetupMaterialVariableVisitor
 			return;
 		}
 
-		// Set buildin id
-		//
-		if(mprop.getBuildinId() == BI_UNITIALIZED)
-		{
-			const std::string name = mv.getName();
-
-			for(uint32_t i = 0; i < buildinNames.size(); i++)
-			{
-				if(name == buildinNames[i])
-				{
-					mprop.setBuildinId(i + 2);
-					break;
-				}
-			}
-
-			if(mprop.getBuildinId() == BI_UNITIALIZED)
-			{
-				mprop.setBuildinId(BT_NO_BUILDIN);
-			}
-		}
-
 		// Sanity check
 		//
-		/*if(!mv.getInitialized() && mprop.getBuildinId() == BT_NO_BUILDIN)
+		ANKI_ASSERT(mprop.getBuildinId() != BI_UNITIALIZED);
+		if(!mv.hasValue() && mprop.getBuildinId() == BT_NO_BUILDIN)
 		{
 			ANKI_LOGW("Material variable no building and not initialized: "
 				<< mv.getName());
-		}*/
+		}
 
 		// Set uniform
 		//
@@ -127,7 +119,7 @@ struct SetupMaterialVariableVisitor
 		Mat4 mvpMat = vpMat * mMat;
 
 		Mat4 mvMat;
-		bool mvMatCalculated = false; // Opt
+		Bool mvMatCalculated = false; // Opt
 
 		switch(mprop.getBuildinId())
 		{
@@ -160,24 +152,46 @@ struct SetupMaterialVariableVisitor
 	}
 };
 
+// Texture specialization
+template<>
+void uniSet<TextureResourcePointer>(
+	const ShaderProgramUniformVariable& uni,
+	const TextureResourcePointer& x,
+	void*)
+{
+	const Texture* tex = x.get();
+	uni.set(*tex);
+}
+
+//==============================================================================
+void RenderableDrawer::setBuildinIds(Renderable& renderable)
+{
+	SetBuildinIdVisitor vis;
+
+	for(auto it = renderable.getPropertiesBegin();
+		it != renderable.getPropertiesEnd(); ++it)
+	{
+		PropertyBase* prop = *it;
+
+		pbase->acceptVisitor(vis);
+
+		if(!vis.setted)
+		{
+			return;
+		}
+	}
+}
+
 //==============================================================================
 void RenderableDrawer::setupShaderProg(
 	const PassLevelKey& key,
 	const Frustumable& fr,
 	Renderable& renderable)
 {
+	setBuildinIds(renderable);
+
 	const Material& mtl = renderable.getMaterial();
 	const ShaderProgram& sprog = mtl.findShaderProgram(key);
-	Array<U8, UNIFORM_BLOCK_MAX_SIZE> clientBlock;
-
-	/*if(mtl.getDepthTestingEnabled())
-	{
-		GlStateSingleton::get().enable(GL_DEPTH_TEST);
-	}
-	else
-	{
-		GlStateSingleton::get().disable(GL_DEPTH_TEST);
-	}*/
 
 	sprog.bind();
 	
@@ -187,7 +201,6 @@ void RenderableDrawer::setupShaderProg(
 	vis.key = key;
 	vis.renderable = &renderable;
 	vis.r = r;
-	vis.clientBlock = &clientBlock;
 
 	for(auto it = renderable.getPropertiesBegin();
 		it != renderable.getPropertiesEnd(); ++it)
@@ -196,8 +209,11 @@ void RenderableDrawer::setupShaderProg(
 		pbase->acceptVisitor(vis);
 	}
 
-	if(mtl->getUniformBlock())
+	const ShaderProgramUniformBlock* block = mtl->getUniformBlock();
+	if(block)
 	{
+		ANKI_ASSERT(block->getSize() <= UNIFORM_BLOCK_MAX_SIZE);
+		renderable.getUbo().write(&vis.clientBlock[0]);
 		renderable.getUbo().setBindingPoint(0);
 	}
 }