瀏覽代碼

Refactoring

Panagiotis Christopoulos Charitos 12 年之前
父節點
當前提交
0079f349db

+ 1 - 53
include/anki/gl/BufferObject.h

@@ -89,7 +89,7 @@ public:
 	///		   GL_DYNAMIC_DRAW only!!!!!!!!!
 	/// @param objectCount The number of objects
 	void create(GLenum target, U32 sizeInBytes, const void* dataPtr,
-		GLenum usage, U objectCount = SINGLE_OBJECT);
+		GLenum usage);
 
 	/// Delete the BO
 	void destroy();
@@ -163,58 +163,6 @@ private:
 #endif
 };
 
-/// This is a wrapper for Vertex Buffer Objects to prevent us from making
-/// idiotic errors
-class Vbo: public BufferObject
-{
-public:
-	/// The same as BufferObject::create but it only accepts
-	/// GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER in target
-	/// @see BufferObject::create
-	void create(GLenum target, PtrSize sizeInBytes, const void* dataPtr,
-		GLenum usage, U objectCount = SINGLE_OBJECT)
-	{
-		// unacceptable target_
-		ANKI_ASSERT(target == GL_ARRAY_BUFFER
-			|| target == GL_ELEMENT_ARRAY_BUFFER);
-		BufferObject::create(target, sizeInBytes, dataPtr, usage, objectCount);
-	}
-
-	/// Unbinds all VBOs, meaning both GL_ARRAY_BUFFER and
-	/// GL_ELEMENT_ARRAY_BUFFER targets
-	static void unbindAllTargets()
-	{
-		glBindBuffer(GL_ARRAY_BUFFER, 0);
-		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
-	}
-};
-
-/// Uniform buffer object
-class Ubo: public BufferObject
-{
-public:
-	/// Create a UBO
-	void create(PtrSize size, const void* data, U objectCount = SINGLE_OBJECT);
-};
-
-/// Pixel buffer object
-class Pbo: public BufferObject
-{
-public:
-	/// Create a PBO
-	void create(GLenum target, PtrSize size, const void* data, 
-		U objectCount = SINGLE_OBJECT)
-	{
-		ANKI_ASSERT(target == GL_PIXEL_PACK_BUFFER 
-			|| target == GL_PIXEL_UNPACK_BUFFER);
-
-		GLenum pboUsage = (target == GL_PIXEL_PACK_BUFFER) 
-			? GL_DYNAMIC_READ : GL_DYNAMIC_DRAW;
-
-		BufferObject::create(target, size, data, pboUsage, objectCount);
-	}
-};
-
 /// @}
 
 } // end namespace anki

+ 8 - 58
include/anki/gl/GlObject.h

@@ -22,24 +22,10 @@ namespace anki {
 class GlObject: public NonCopyable
 {
 public:
-	/// Buffering technique
-	enum
-	{
-		SINGLE_OBJECT = 1,
-		DOUBLE_OBJECT = 2,
-		TRIPLE_OBJECT = 3,
-		MAX_OBJECTS = 3
-	};
-
 	/// Default
 	GlObject()
-	{
-		memset(&glIds[0], 0, sizeof(glIds));
-		objectsCount = 1;
-#if ANKI_DEBUG
-		refCount.store(0);
-#endif
-	}
+		: glId(0)
+	{}
 
 	/// Move
 	GlObject(GlObject&& b)
@@ -59,18 +45,8 @@ public:
 	{
 		ANKI_ASSERT(!isCreated());
 		
-		for(U i = 0; i < MAX_OBJECTS; i++)
-		{
-			glIds[i] = b.glIds[i];
-			b.glIds[i] = 0;
-		}
-
-		objectsCount = b.objectsCount;
-		b.objectsCount = 1;
-#if ANKI_DEBUG
-		refCount.store(b.refCount.load());
-		b.refCount.store(0);
-#endif
+		glId = b.glId;
+		b.glId = 0;
 		return *this;
 	}
 
@@ -78,44 +54,18 @@ public:
 	GLuint getGlId() const
 	{
 		ANKI_ASSERT(isCreated());
-		return glIds[getGlobTimestamp() % objectsCount];
+		return glId;
 	}
 
 	/// GL object is created
 	Bool isCreated() const
 	{
-		ANKI_ASSERT(objectsCount > 0);
-#if ANKI_DEBUG
-		U mask = 0;
-		for(U i = 0; i < MAX_OBJECTS; i++)
-		{
-			mask <<= 1;
-			mask |= (glIds[i] != 0);
-		}
-
-		// If the mask is not zero then make sure that objectsCount is sane
-		ANKI_ASSERT(!(mask != 0 && __builtin_popcount(mask) != objectsCount));
-#endif
-
 		return glId != 0;
 	}
 
 protected:
-	/// OpenGL names
-	union
-	{
-		Array<GLuint, MAX_OBJECTS> glIds;
-		GLuint glId;
-	};
-
-	/// The size of the glIds array
-	U8 objectsCount;
-
-#if ANKI_DEBUG
-	/// Textures and buffers can be attached so keep a refcount for sanity
-	/// checks
-	std::atomic<U32> refCount;
-#endif
+	/// OpenGL name
+	GLuint glId;
 };
 
 /// Defines an non sharable GL object. Used to avoid idiotic mistakes and more
@@ -155,7 +105,7 @@ public:
 	void checkNonSharable() const
 	{
 #if ANKI_DEBUG
-		ANKI_ASSERT((!isCreated() 
+		ANKI_ASSERT((!isCreated()
 			|| creationThreadId == std::this_thread::get_id())
 			&& "Object is not context sharable");
 #endif

+ 4 - 4
include/anki/gl/Vao.h

@@ -7,7 +7,7 @@
 namespace anki {
 
 class ShaderProgramAttributeVariable;
-class Vbo;
+class BufferObject;
 
 /// @addtogroup OpenGL
 /// @{
@@ -81,7 +81,7 @@ public:
 	/// @param offset Specifies a offset of the first component of the
 	///        first generic vertex attribute in the array
 	void attachArrayBufferVbo(
-	    const Vbo* vbo,
+	    const BufferObject* vbo,
 	    const ShaderProgramAttributeVariable& attribVar,
 	    const PtrSize size,
 	    const GLenum type,
@@ -104,7 +104,7 @@ public:
 	/// @param pointer Specifies a offset of the first component of the
 	///        first generic vertex attribute in the array
 	void attachArrayBufferVbo(
-	    const Vbo* vbo,
+	    const BufferObject* vbo,
 	    const GLint attribVarLocation,
 	    const PtrSize size,
 	    const GLenum type,
@@ -119,7 +119,7 @@ public:
 	}
 
 	/// Attach an element array buffer VBO
-	void attachElementArrayBufferVbo(const Vbo* vbo);
+	void attachElementArrayBufferVbo(const BufferObject* vbo);
 
 	/// Bind it
 	void bind() const

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

@@ -64,7 +64,7 @@ private:
 
 	Array<Vertex, MAX_POINTS_PER_DRAW> clientVerts;
 
-	Vbo vbo;
+	BufferObject vbo;
 	Vao vao;
 
 	/// This is a container of some precalculated spheres. Its a map that

+ 1 - 1
include/anki/renderer/Hdr.h

@@ -69,7 +69,7 @@ private:
 	Timestamp parameterUpdateTimestamp = getGlobTimestamp();
 	/// When the commonUbo got updated
 	Timestamp commonUboUpdateTimestamp = getGlobTimestamp();
-	Ubo commonUbo;
+	BufferObject commonUbo;
 
 	void initFbo(Fbo& fbo, Texture& fai);
 	void initInternal(const RendererInitializer& initializer);

+ 3 - 3
include/anki/renderer/Is.h

@@ -66,13 +66,13 @@ private:
 	PtrSize uboAlignment = MAX_PTR_SIZE; ///< Cache the value here
 
 	/// Contains common data for all shader programs
-	Ubo commonUbo;
+	BufferObject commonUbo;
 
 	/// Track the updates of commonUbo
 	Timestamp commonUboUpdateTimestamp = getGlobTimestamp();
 
 	/// Contains all the lights
-	Ubo lightsUbo;
+	BufferObject lightsUbo;
 
 	/// Contains the number of lights per tile
 	BufferObject tilesBuffer;
@@ -98,7 +98,7 @@ private:
 
 	/// @name For drawing a quad into the active framebuffer
 	/// @{
-	Vbo quadPositionsVbo; ///< The VBO for quad positions
+	BufferObject quadPositionsVbo; ///< The VBO for quad positions
 	Vao quadVao; ///< This VAO is used everywhere except material stage
 	/// @}
 

+ 1 - 1
include/anki/renderer/Lf.h

@@ -39,7 +39,7 @@ private:
 	TextureResourcePointer lensDirtTex;
 	U8 maxFlaresPerLight;
 	U8 maxLightsWithFlares;
-	Ubo flareDataUbo;
+	BufferObject flareDataUbo;
 	const ShaderProgramUniformBlock* ublock;
 	
 

+ 1 - 1
include/anki/renderer/Renderer.h

@@ -271,7 +271,7 @@ private:
 
 	/// @name For drawing a quad into the active framebuffer
 	/// @{
-	Vbo quadPositionsVbo; ///< The VBO for quad positions
+	BufferObject quadPositionsVbo; ///< The VBO for quad positions
 	Vao quadVao; ///< This VAO is used everywhere except material stage
 	/// @}
 

+ 1 - 1
include/anki/renderer/Ssao.h

@@ -49,7 +49,7 @@ private:
 	ShaderProgramResourcePointer hblurSProg;
 	ShaderProgramResourcePointer vblurSProg;
 	Timestamp commonUboUpdateTimestamp = getGlobTimestamp();
-	Ubo commonUbo;
+	BufferObject commonUbo;
 
 	static void createFbo(Fbo& fbo, Texture& fai, U width, U height);
 	void initInternal(const RendererInitializer& initializer);

+ 1 - 1
include/anki/renderer/Tiler.h

@@ -64,7 +64,7 @@ private:
 	Fbo fbo;
 
 	/// PBO buffer that is used to read the data of fai asynchronously
-	Pbo pbo;
+	BufferObject pbo;
 
 	/// Main shader program
 	ShaderProgramResourcePointer prog;

+ 3 - 3
include/anki/resource/Mesh.h

@@ -85,7 +85,7 @@ public:
 
 	/// Get info on how to attach a VBO to a VAO
 	void getVboInfo(
-		const VertexAttribute attrib, const Vbo*& vbo,
+		const VertexAttribute attrib, const BufferObject*& vbo,
 		U32& size, GLenum& type, U32& stride, U32& offset) const;
 
 	/// Helper function for correct loading
@@ -110,8 +110,8 @@ protected:
 	U8 texChannelsCount;
 	Bool8 weights;
 
-	Vbo vbo;
-	Vbo indicesVbo;
+	BufferObject vbo;
+	BufferObject indicesVbo;
 
 	/// Create the VBOs using the mesh data
 	void createVbos(const MeshLoader& loader);

+ 1 - 1
include/anki/scene/ParticleEmitter.h

@@ -220,7 +220,7 @@ private:
 	U32 aliveParticlesCountDraw = 0;
 
 	Vao vao; ///< Hold the VBO
-	Vbo vbo; ///< Hold the vertex data
+	BufferObject vbo; ///< Hold the vertex data
 	SceneVector<F32> clientBuffer;
 
 	U8 simulationType = UNDEFINED_SIMULATION;

+ 13 - 32
src/gl/BufferObject.cpp

@@ -39,15 +39,14 @@ void BufferObject::destroy()
 	if(isCreated())
 	{
 		unbind();
-		glDeleteBuffers(objectsCount, &glIds[0]);
-		memset(&glIds[0], 0, sizeof(glIds));
-		objectsCount = 1;
+		glDeleteBuffers(1, &glId);
+		glId = 0;
 	}
 }
 
 //==============================================================================
 void BufferObject::create(GLenum target_, U32 sizeInBytes_,
-	const void* dataPtr, GLenum usage_, U objectsCount_)
+	const void* dataPtr, GLenum usage_)
 {
 	ANKI_ASSERT(!isCreated());
 
@@ -70,31 +69,24 @@ void BufferObject::create(GLenum target_, U32 sizeInBytes_,
 	usage = usage_;
 	target = target_;
 	sizeInBytes = sizeInBytes_;
-	objectsCount = objectsCount_;
 
 	ANKI_ASSERT(sizeInBytes > 0 && "Unacceptable sizeInBytes");
 	ANKI_ASSERT(!(target == GL_UNIFORM_BUFFER && usage != GL_DYNAMIC_DRAW)
 		&& "Don't use UBOs like that");
-	ANKI_ASSERT(objectsCount > 0 && objectsCount < MAX_OBJECTS);
-	ANKI_ASSERT(!(objectsCount > 1 && dataPtr != nullptr)
-		&& "Multibuffering with data is not making sence");
 
 	// Create
-	glGenBuffers(objectsCount, &glIds[0]);
+	glGenBuffers(1, &glId);
 
-	for(U i = 0; i < objectsCount; i++)
-	{
-		glBindBuffer(target, glIds[i]);
-		glBufferData(target, sizeInBytes, dataPtr, usage);
+	glBindBuffer(target, glId);
+	glBufferData(target, sizeInBytes, dataPtr, usage);
 
-		// make a check
-		GLint bufferSize = 0;
-		glGetBufferParameteriv(target, GL_BUFFER_SIZE, &bufferSize);
-		if(sizeInBytes != (U32)bufferSize)
-		{
-			destroy();
-			throw ANKI_EXCEPTION("Data size mismatch");
-		}
+	// make a check
+	GLint bufferSize = 0;
+	glGetBufferParameteriv(target, GL_BUFFER_SIZE, &bufferSize);
+	if(sizeInBytes != (U32)bufferSize)
+	{
+		destroy();
+		throw ANKI_EXCEPTION("Data size mismatch");
 	}
 
 	glBindBuffer(target, 0);
@@ -194,15 +186,4 @@ void BufferObject::setBindingRange(
 	ANKI_CHECK_GL_ERROR();
 }
 
-//==============================================================================
-// Ubo                                                                         =
-//==============================================================================
-
-//==============================================================================
-void Ubo::create(PtrSize size, const void* data, U objectCount)
-{
-	BufferObject::create(GL_UNIFORM_BUFFER, size, data, GL_DYNAMIC_DRAW,
-		objectCount);
-}
-
 } // end namespace anki

+ 4 - 3
src/gl/Vao.cpp

@@ -25,7 +25,8 @@ void Vao::destroy()
 }
 
 //==============================================================================
-void Vao::attachArrayBufferVbo(const Vbo* vbo, const GLint attribVarLocation,
+void Vao::attachArrayBufferVbo(const BufferObject* vbo, 
+	const GLint attribVarLocation,
 	const PtrSize size, const GLenum type, const Bool normalized, 
 	const PtrSize stride, const PtrSize offset)
 {
@@ -49,7 +50,7 @@ void Vao::attachArrayBufferVbo(const Vbo* vbo, const GLint attribVarLocation,
 }
 
 //==============================================================================
-void Vao::attachArrayBufferVbo(const Vbo* vbo,
+void Vao::attachArrayBufferVbo(const BufferObject* vbo,
 	const ShaderProgramAttributeVariable& attribVar,
 	const PtrSize size, const GLenum type, const Bool normalized, 
 	const PtrSize stride, const PtrSize offset)
@@ -59,7 +60,7 @@ void Vao::attachArrayBufferVbo(const Vbo* vbo,
 }
 
 //==============================================================================
-void Vao::attachElementArrayBufferVbo(const Vbo* vbo)
+void Vao::attachElementArrayBufferVbo(const BufferObject* vbo)
 {
 	ANKI_ASSERT(isCreated());
 	checkNonSharable();

+ 1 - 1
src/renderer/Hdr.cpp

@@ -49,7 +49,7 @@ void Hdr::initInternal(const RendererInitializer& initializer)
 
 	// init shaders
 	Vec4 block(exposure, 0.0, 0.0, 0.0);
-	commonUbo.create(sizeof(Vec4), &block);
+	commonUbo.create(GL_UNIFORM_BUFFER, sizeof(Vec4), &block, GL_DYNAMIC_DRAW);
 
 	toneSProg.load("shaders/PpsHdr.glsl");
 	toneSProg->findUniformBlock("commonBlock").setBinding(0);

+ 4 - 2
src/renderer/Is.cpp

@@ -524,9 +524,11 @@ void Is::initInternal(const RendererInitializer& initializer)
 
 	uboAlignment = BufferObject::getUniformBufferOffsetAlignment();
 
-	commonUbo.create(sizeof(shader::CommonUniforms), nullptr);
+	commonUbo.create(GL_UNIFORM_BUFFER, sizeof(shader::CommonUniforms), 
+		nullptr, GL_DYNAMIC_DRAW);
 
-	lightsUbo.create(calcLightsUboSize(), nullptr);
+	lightsUbo.create(GL_UNIFORM_BUFFER, calcLightsUboSize(), nullptr,
+		GL_DYNAMIC_DRAW);
 
 	tilesBuffer.create(
 		GL_UNIFORM_BUFFER, 

+ 1 - 1
src/renderer/Lf.cpp

@@ -98,7 +98,7 @@ void Lf::initInternal(const RendererInitializer& initializer)
 	}
 
 	// Init UBO
-	flareDataUbo.create(blockSize, nullptr);
+	flareDataUbo.create(GL_UNIFORM_BUFFER, blockSize, nullptr, GL_DYNAMIC_DRAW);
 
 	// Create the FAI
 	fai.create2dFai(r->getPps().getHdr().getFai().getWidth(), 

+ 2 - 1
src/renderer/Ssao.cpp

@@ -150,7 +150,8 @@ void Ssao::initInternal(const RendererInitializer& initializer)
 	//
 	// Shaders
 	//
-	commonUbo.create(sizeof(ShaderCommonUniforms), nullptr);
+	commonUbo.create(GL_UNIFORM_BUFFER, sizeof(ShaderCommonUniforms), nullptr,
+		GL_DYNAMIC_DRAW);
 
 	std::stringstream pps;
 

+ 1 - 1
src/renderer/Tiler.cpp

@@ -243,7 +243,7 @@ void Tiler::initInternal(Renderer* r_)
 	// Create PBO
 	pbo.create(GL_PIXEL_PACK_BUFFER, 
 		r->getTilesCount().x() * r->getTilesCount().y() * 2 * sizeof(F32), 
-		nullptr);
+		nullptr, GL_DYNAMIC_READ);
 
 	// Init planes
 	U planesCount = 

+ 2 - 2
src/resource/Mesh.cpp

@@ -120,8 +120,8 @@ void Mesh::createVbos(const MeshLoader& loader)
 }
 
 //==============================================================================
-void Mesh::getVboInfo(const VertexAttribute attrib, const Vbo*& v, U32& size,
-	GLenum& type, U32& stride, U32& offset) const
+void Mesh::getVboInfo(const VertexAttribute attrib, const BufferObject*& v, 
+	U32& size, GLenum& type, U32& stride, U32& offset) const
 {
 	stride = calcVertexSize();
 

+ 1 - 1
src/resource/Model.cpp

@@ -36,7 +36,7 @@ void ModelPatchBase::createVao(const ShaderProgram& prog,
 {
 	vao.create();
 
-	const Vbo* vbo;
+	const BufferObject* vbo;
 	U32 size;
 	GLenum type;
 	U32 stride;