Commits

Alex Szpakowski  committed 145524a

SpriteBatches are a little more intelligent about how much data they upload to the GPU when they're flushed or drawn, now

  • Participants
  • Parent commits 6b10b37
  • Branches minor

Comments (0)

Files changed (4)

File src/modules/graphics/opengl/SpriteBatch.cpp

 	, color(0)
 	, array_buf(0)
 	, element_buf(0)
+	, buffer_used_offset(0)
+	, buffer_used_size(0)
 {
 	if (size <= 0)
 		throw love::Exception("Invalid SpriteBatch size.");
 void SpriteBatch::flush()
 {
 	VertexBuffer::Bind bind(*array_buf);
-	array_buf->unmap();
+	array_buf->unmap(buffer_used_offset, buffer_used_size);
+
+	buffer_used_offset = buffer_used_size = 0;
 }
 
 void SpriteBatch::setTexture(Texture *newtexture)
 
 	VertexBuffer *new_array_buf = nullptr;
 	VertexIndex *new_element_buf = nullptr;
-	void *new_data = nullptr;
 
 	try
 	{
 		new_array_buf = VertexBuffer::Create(vertex_size, array_buf->getTarget(), array_buf->getUsage());
 		new_element_buf = new VertexIndex(newsize);
-
-		// VBO::map can throw an exception. Also we want to scope the bind.
-		VertexBuffer::Bind bind(*new_array_buf);
-		new_data = new_array_buf->map();
 	}
 	catch (love::Exception &)
 	{
 		delete new_array_buf;
 		delete new_element_buf;
-
-		{
-			VertexBuffer::Bind bind(*array_buf);
-			array_buf->unmap();
-		}
-
 		throw;
 	}
 
 	// Copy as much of the old data into the new VertexBuffer as can fit.
-	memcpy(new_data, old_data, sizeof(Vertex) * 4 * std::min(newsize, size));
+	new_array_buf->fill(0, sizeof(Vertex) * 4 * std::min(newsize, size), old_data);
 
 	// We don't need to unmap the old VertexBuffer since we're deleting it.
 	delete array_buf;
 
 	next = std::min(next, newsize);
 
-	// But we should unmap the new one!
-	{
-		VertexBuffer::Bind bind(*new_array_buf);
-		new_array_buf->unmap();
-	}
+	// The new VertexBuffer isn't mapped, so we should reset these variables.
+	buffer_used_offset = buffer_used_size = 0;
 }
 
 int SpriteBatch::getBufferSize() const
 	VertexBuffer::Bind element_bind(*element_buf->getVertexBuffer());
 
 	// Make sure the VBO isn't mapped when we draw (sends data to GPU if needed.)
-	array_buf->unmap();
+	array_buf->unmap(buffer_used_offset, buffer_used_size);
+	buffer_used_offset = buffer_used_size = 0;
 
 	Color curcolor = gl.getColor();
 
 
 void SpriteBatch::addv(const Vertex *v, int index)
 {
-	static const int sprite_size = 4 * sizeof(Vertex); // bytecount
+	static const size_t sprite_size = 4 * sizeof(Vertex); // bytecount
 
 	VertexBuffer::Bind bind(*array_buf);
 
 	array_buf->map();
 
 	array_buf->fill(index * sprite_size, sprite_size, v);
+
+	buffer_used_offset = std::min(buffer_used_offset, index * sprite_size);
+	buffer_used_size = std::max(buffer_used_size, (index + 1) * sprite_size - buffer_used_offset);
 }
 
 void SpriteBatch::setColorv(Vertex *v, const Color &color)

File src/modules/graphics/opengl/SpriteBatch.h

 	VertexBuffer *array_buf;
 	VertexIndex *element_buf;
 
+	// The portion of the vertex buffer that's been modified while mapped.
+	size_t buffer_used_offset;
+	size_t buffer_used_size;
+
 	static StringMap<UsageHint, USAGE_MAX_ENUM>::Entry usageHintEntries[];
 	static StringMap<UsageHint, USAGE_MAX_ENUM> usageHints;
 

File src/modules/graphics/opengl/VertexBuffer.cpp

 	, is_dirty(true)
 {
 	if (getMemoryBacking() == BACKING_FULL)
-		memory_map = malloc(getSize());
+		memory_map = (char *) malloc(getSize());
 
 	bool ok = load(false);
 
 
 	if (!memory_map)
 	{
-		memory_map = malloc(getSize());
+		memory_map = (char *) malloc(getSize());
 		if (!memory_map)
 			throw love::Exception("Out of memory (oh the humanity!)");
 	}
 	return memory_map;
 }
 
-void VertexBuffer::unmap()
+void VertexBuffer::unmapStatic(size_t offset, size_t size)
+{
+	// Upload the mapped data to the buffer.
+	glBufferSubData(getTarget(), (GLintptr) offset, (GLsizeiptr) size, memory_map + offset);
+}
+
+void VertexBuffer::unmapStream()
+{
+	// "orphan" current buffer to avoid implicit synchronisation on the GPU:
+	// http://www.seas.upenn.edu/~pcozzi/OpenGLInsights/OpenGLInsights-AsynchronousBufferTransfers.pdf
+	glBufferData(getTarget(), (GLsizeiptr) getSize(), nullptr,    getUsage());
+	glBufferData(getTarget(), (GLsizeiptr) getSize(), memory_map, getUsage());
+}
+
+void VertexBuffer::unmap(size_t usedOffset, size_t usedSize)
 {
 	if (!is_mapped)
 		return;
 
+	usedOffset = std::min(usedOffset, getSize());
+	usedSize = std::min(usedSize, getSize() - usedOffset);
+
 	// VBO::bind is a no-op when the VBO is mapped, so we have to make sure it's
 	// bound here.
 	if (!is_bound)
 		is_bound = true;
 	}
 
-	if (getUsage() == GL_STATIC_DRAW)
+	switch (getUsage())
 	{
-		// Upload the mapped data to the buffer.
-		glBufferSubData(getTarget(), 0, (GLsizeiptr) getSize(), memory_map);
-	}
-	else
-	{
-		// "orphan" current buffer to avoid implicit synchronisation on the GPU:
-		// http://www.seas.upenn.edu/~pcozzi/OpenGLInsights/OpenGLInsights-AsynchronousBufferTransfers.pdf
-		glBufferData(getTarget(), (GLsizeiptr) getSize(), NULL,       getUsage());
-		glBufferData(getTarget(), (GLsizeiptr) getSize(), memory_map, getUsage());
+	case GL_STATIC_DRAW:
+		unmapStatic(usedOffset, usedSize);
+		break;
+	case GL_STREAM_DRAW:
+		unmapStream();
+		break;
+	case GL_DYNAMIC_DRAW:
+	default:
+		// It's probably more efficient to treat it like a streaming buffer if
+		// more than a third of its contents have been modified during the map().
+		if (usedSize >= getSize() / 3)
+			unmapStream();
+		else
+			unmapStatic(usedOffset, usedSize);
+		break;
 	}
 
 	is_mapped = false;
 void VertexBuffer::fill(size_t offset, size_t size, const void *data)
 {
 	if (is_mapped || getMemoryBacking() == BACKING_FULL)
-		memcpy(static_cast<char *>(memory_map) + offset, data, size);
+		memcpy(memory_map + offset, data, size);
 
 	if (!is_mapped)
 	{

File src/modules/graphics/opengl/VertexBuffer.h

 // OpenGL
 #include "OpenGL.h"
 
+// C
+#include <stddef.h>
+
 namespace love
 {
 namespace graphics
 	 * when used to draw elements.
 	 *
 	 * The VertexBuffer must be bound to use this function.
+	 *
+	 * @param usedOffset The offset into the mapped buffer indicating the
+	 *                   sub-range of data modified. Optional.
+	 * @param usedSize   The size of the sub-range of modified data. Optional.
 	 */
-	virtual void unmap();
+	virtual void unmap(size_t usedOffset = 0, size_t usedSize = -1);
 
 	/**
 	 * Bind the VertexBuffer to its specified target.
 	 */
 	void unload(bool save);
 
+	void unmapStatic(size_t offset, size_t size);
+	void unmapStream();
 
 	// Whether the buffer is currently bound.
 	bool is_bound;
 
 	// A pointer to mapped memory. Will be inialized on the first
 	// call to map().
-	void *memory_map;
+	char *memory_map;
 
 	// Set if the buffer was modified while operating on gpu memory
 	// and needs to be synchronized.