Browse Source

Tiler more async & optimize Dbg

Panagiotis Christopoulos Charitos 13 years ago
parent
commit
46ae57ec96

+ 1 - 0
include/anki/gl/Gl.h

@@ -6,6 +6,7 @@
 
 
 #include "anki/gl/BufferObject.h"
 #include "anki/gl/BufferObject.h"
 #include "anki/gl/Ubo.h"
 #include "anki/gl/Ubo.h"
+#include "anki/gl/Pbo.h"
 #include "anki/gl/Fbo.h"
 #include "anki/gl/Fbo.h"
 #include "anki/gl/GlException.h"
 #include "anki/gl/GlException.h"
 #include "anki/gl/GlState.h"
 #include "anki/gl/GlState.h"

+ 30 - 0
include/anki/gl/Pbo.h

@@ -0,0 +1,30 @@
+#ifndef ANKI_GL_PBO_H
+#define ANKI_GL_PBO_H
+
+#include "anki/gl/BufferObject.h"
+
+namespace anki {
+
+/// @addtogroup OpenGL
+/// @{
+
+/// Pixel buffer object
+class Pbo: public BufferObject
+{
+public:
+	void create(GLenum target, PtrSize size, void* data)
+	{
+		ANKI_ASSERT(target == GL_PIXEL_PACK_BUFFER 
+			|| target == GL_PIXEL_UNPACK_BUFFER);
+
+		GLenum pboUsage = (target == GL_PIXEL_PACK_BUFFER) 
+			? GL_STREAM_READ : GL_STREAM_DRAW;
+
+		BufferObject::create(target, size, data, pboUsage);
+	}
+};
+/// @}
+
+} // end namespace anki
+
+#endif

+ 1 - 2
include/anki/renderer/DebugDrawer.h

@@ -51,8 +51,7 @@ public:
 private:
 private:
 	struct Vertex
 	struct Vertex
 	{
 	{
-		Vec4 position;
-		Vec4 color;
+		Vec4 positionAndColor;
 		Mat4 matrix;
 		Mat4 matrix;
 	};
 	};
 
 

+ 10 - 2
include/anki/renderer/Tiler.h

@@ -3,8 +3,7 @@
 
 
 #include "anki/util/StdTypes.h"
 #include "anki/util/StdTypes.h"
 #include "anki/collision/Collision.h"
 #include "anki/collision/Collision.h"
-#include "anki/gl/Fbo.h"
-#include "anki/gl/Texture.h"
+#include "anki/gl/Gl.h"
 #include "anki/resource/Resource.h"
 #include "anki/resource/Resource.h"
 #include "anki/core/Timestamp.h"
 #include "anki/core/Timestamp.h"
 
 
@@ -12,6 +11,7 @@ namespace anki {
 
 
 class Renderer;
 class Renderer;
 class Camera;
 class Camera;
+class ShaderProgramUniformVariable;
 
 
 /// Tiler used for visibility tests
 /// Tiler used for visibility tests
 class Tiler
 class Tiler
@@ -30,6 +30,9 @@ public:
 
 
 	void init(Renderer* r);
 	void init(Renderer* r);
 
 
+	/// Issue the GPU job
+	void runMinMax(const Texture& depthMap);
+
 	/// Update the tiles before doing visibility tests
 	/// Update the tiles before doing visibility tests
 	void updateTiles(Camera& cam, const Texture& depthMap);
 	void updateTiles(Camera& cam, const Texture& depthMap);
 
 
@@ -71,9 +74,14 @@ private:
 	/// Main FBO
 	/// Main FBO
 	Fbo fbo;
 	Fbo fbo;
 
 
+	/// PBO
+	Pbo pbo;
+
 	/// Main shader program
 	/// Main shader program
 	ShaderProgramResourcePointer prog;
 	ShaderProgramResourcePointer prog;
 
 
+	const ShaderProgramUniformVariable* depthMapUniform;
+
 	Renderer* r;
 	Renderer* r;
 	const Camera* prevCam;
 	const Camera* prevCam;
 
 

+ 2 - 2
shaders/Dbg.glsl

@@ -1,14 +1,14 @@
 #pragma anki start vertexShader
 #pragma anki start vertexShader
 
 
 layout(location = 0) in vec3 position;
 layout(location = 0) in vec3 position;
-layout(location = 1) in vec3 color;
+layout(location = 1) in uint color;
 layout(location = 2) in mat4 modelViewProjectionMat;
 layout(location = 2) in mat4 modelViewProjectionMat;
 
 
 out vec3 vColor;
 out vec3 vColor;
 
 
 void main()
 void main()
 {
 {
-	vColor = color;
+	vColor = unpackSnorm4x8(color).rgb;
 	gl_Position = modelViewProjectionMat * vec4(position, 1.0);
 	gl_Position = modelViewProjectionMat * vec4(position, 1.0);
 }
 }
 
 

+ 17 - 5
src/renderer/DebugDrawer.cpp

@@ -32,8 +32,8 @@ DebugDrawer::DebugDrawer()
 		false, sizeof(Vertex), 0);
 		false, sizeof(Vertex), 0);
 
 
 	vao.attachArrayBufferVbo(
 	vao.attachArrayBufferVbo(
-		&vbo, prog->findAttributeVariable("color"), 3, GL_FLOAT,
-		false, sizeof(Vertex), sizeof(Vec4));
+		&vbo, prog->findAttributeVariable("color"), 1, GL_FLOAT,
+		false, sizeof(Vertex), sizeof(U32));
 
 
 	GLint loc =
 	GLint loc =
 		prog->findAttributeVariable("modelViewProjectionMat").getLocation();
 		prog->findAttributeVariable("modelViewProjectionMat").getLocation();
@@ -88,7 +88,7 @@ void DebugDrawer::end()
 	if(vertexPointer % 2 != 0)
 	if(vertexPointer % 2 != 0)
 	{
 	{
 		// push back the previous vertex to close the loop
 		// push back the previous vertex to close the loop
-		pushBackVertex(clientVerts[vertexPointer].position.xyz());
+		pushBackVertex(clientVerts[vertexPointer].positionAndColor.xyz());
 	}
 	}
 }
 }
 
 
@@ -112,8 +112,20 @@ void DebugDrawer::flush()
 //==============================================================================
 //==============================================================================
 void DebugDrawer::pushBackVertex(const Vec3& pos)
 void DebugDrawer::pushBackVertex(const Vec3& pos)
 {
 {
-	clientVerts[vertexPointer].position = Vec4(pos, 1.0);
-	clientVerts[vertexPointer].color = Vec4(crntCol, 1.0);
+	U32 color = (U8)(crntCol.x() * 255.0);
+	color = (color << 8) | (U8)(crntCol.y() * 255.0);
+	color = (color << 8) | (U8)(crntCol.z() * 255.0);
+	color = (color << 8) | (U8)(1.0 * 255.0);
+
+	union
+	{
+		F32 f;
+		U32 u;
+	} uni;
+
+	uni.u = color;
+
+	clientVerts[vertexPointer].positionAndColor = Vec4(pos, uni.f);
 	clientVerts[vertexPointer].matrix = mvpMat.getTransposed();
 	clientVerts[vertexPointer].matrix = mvpMat.getTransposed();
 
 
 	++vertexPointer;
 	++vertexPointer;

+ 25 - 9
src/renderer/Tiler.cpp

@@ -175,6 +175,8 @@ void Tiler::initInternal(Renderer* r_)
 	prog.load(ShaderProgramResource::createSrcCodeToCache(
 	prog.load(ShaderProgramResource::createSrcCodeToCache(
 		"shaders/TilerMinMax.glsl", pps.c_str()).c_str());
 		"shaders/TilerMinMax.glsl", pps.c_str()).c_str());
 
 
+	depthMapUniform = &(prog->findUniformVariable("depthMap"));
+
 	// Create FBO
 	// Create FBO
 	Renderer::createFai(TILES_X_COUNT, TILES_Y_COUNT, GL_RG32UI,
 	Renderer::createFai(TILES_X_COUNT, TILES_Y_COUNT, GL_RG32UI,
 		GL_RG_INTEGER, GL_UNSIGNED_INT, fai);
 		GL_RG_INTEGER, GL_UNSIGNED_INT, fai);
@@ -186,6 +188,29 @@ void Tiler::initInternal(Renderer* r_)
 	{
 	{
 		throw ANKI_EXCEPTION("FBO not complete");
 		throw ANKI_EXCEPTION("FBO not complete");
 	}
 	}
+
+	// Create PBO
+	pbo.create(GL_PIXEL_PACK_BUFFER, 
+		TILES_X_COUNT * TILES_Y_COUNT * 2 * sizeof(F32), nullptr);
+}
+
+//==============================================================================
+void Tiler::runMinMax(const Texture& depthMap)
+{
+	// Issue the min/max job
+	fbo.bind();
+	GlStateSingleton::get().setViewport(0, 0, TILES_X_COUNT, TILES_Y_COUNT);
+	r->clearAfterBindingFbo(GL_COLOR_BUFFER_BIT);
+	prog->bind();
+	depthMapUniform->set(depthMap);
+
+	r->drawQuad();
+
+	// Issue the async pixel read
+	pbo.bind();
+	glReadPixels(0, 0, TILES_X_COUNT, TILES_Y_COUNT, GL_RG_INTEGER,
+		GL_UNSIGNED_INT, nullptr);
+	pbo.unbind();
 }
 }
 
 
 //==============================================================================
 //==============================================================================
@@ -229,15 +254,6 @@ void Tiler::updateTiles(Camera& cam, const Texture& depthMap)
 	update2Planes(cam, pixels);
 	update2Planes(cam, pixels);
 
 
 	prevCam = &cam;
 	prevCam = &cam;
-
-	/*for(U i = 0; i < TILES_Y_COUNT; i++)
-		for(U j = 0; j < TILES_X_COUNT; j++)
-		{
-			std::cout << tiles[j][i].planesWSpace[0].getNormal() << " "
-				<< tiles[j][i].planesWSpace[0].getOffset() << std::endl;
-		}
-
-	std::cout << std::endl;*/
 #else
 #else
 	Vector<F32> allpixels;
 	Vector<F32> allpixels;
 
 

+ 6 - 0
src/util/Memory.cpp

@@ -3,6 +3,7 @@
 #include "anki/util/Exception.h"
 #include "anki/util/Exception.h"
 #include <limits>
 #include <limits>
 #include <cstdlib>
 #include <cstdlib>
+#include <cstring>
 
 
 namespace anki {
 namespace anki {
 
 
@@ -130,6 +131,11 @@ void StackMemoryPool::reset()
 	// memory is nullptr if moved
 	// memory is nullptr if moved
 	ANKI_ASSERT(memory != nullptr);
 	ANKI_ASSERT(memory != nullptr);
 
 
+#if ANKI_DEBUG
+	// Invalidate the memory
+	memset(memory, 0xCC, memsize);
+#endif
+
 	top = memory;
 	top = memory;
 }
 }