Prevent redundant glBindBuffer calls.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2016-10-20 21:50:48 -03:00
parent 4c122e6581
commit d1c5edd792
14 changed files with 128 additions and 160 deletions
+16 -29
View File
@@ -57,7 +57,7 @@ SpriteBatch::SpriteBatch(Texture *texture, int size, Mesh::Usage usage)
GLenum gl_usage = Mesh::getGLBufferUsage(usage);
size_t vertex_size = sizeof(Vertex) * 4 * size;
array_buf = new GLBuffer(vertex_size, nullptr, GL_ARRAY_BUFFER, gl_usage, GLBuffer::MAP_EXPLICIT_RANGE_MODIFY);
array_buf = new GLBuffer(vertex_size, nullptr, BUFFER_VERTEX, gl_usage, GLBuffer::MAP_EXPLICIT_RANGE_MODIFY);
}
SpriteBatch::~SpriteBatch()
@@ -108,7 +108,6 @@ void SpriteBatch::clear()
void SpriteBatch::flush()
{
GLBuffer::Bind bind(*array_buf);
array_buf->unmap();
}
@@ -155,11 +154,7 @@ void SpriteBatch::setBufferSize(int newsize)
return;
// Map the old GLBuffer to get a pointer to its data.
void *old_data = nullptr;
{
GLBuffer::Bind bind(*array_buf);
old_data = array_buf->map();
}
void *old_data = array_buf->map();
size_t vertex_size = sizeof(Vertex) * 4 * newsize;
GLBuffer *new_array_buf = nullptr;
@@ -168,9 +163,7 @@ void SpriteBatch::setBufferSize(int newsize)
try
{
new_array_buf = new GLBuffer(vertex_size, nullptr, array_buf->getTarget(), array_buf->getUsage(), array_buf->getMapFlags());
GLBuffer::Bind bind(*new_array_buf);
new_array_buf = new GLBuffer(vertex_size, nullptr, array_buf->getType(), array_buf->getUsage(), array_buf->getMapFlags());
// Copy as much of the old data into the new GLBuffer as can fit.
size_t copy_size = sizeof(Vertex) * 4 * new_next;
@@ -263,25 +256,21 @@ void SpriteBatch::draw(const Matrix4 &m)
uint32 enabledattribs = ATTRIBFLAG_POS | ATTRIBFLAG_TEXCOORD;
// Make sure the VBO isn't mapped when we draw (sends data to GPU if needed.)
array_buf->unmap();
array_buf->bind();
// Apply per-sprite color, if a color is set.
if (color)
{
// Scope this bind so it doesn't interfere with the
// Mesh::bindAttributeToShaderInput calls below.
GLBuffer::Bind array_bind(*array_buf);
// Make sure the VBO isn't mapped when we draw (sends data to GPU if needed.)
array_buf->unmap();
// Apply per-sprite color, if a color is set.
if (color)
{
enabledattribs |= ATTRIBFLAG_COLOR;
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), array_buf->getPointer(color_offset));
}
glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), array_buf->getPointer(pos_offset));
glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), array_buf->getPointer(texel_offset));
enabledattribs |= ATTRIBFLAG_COLOR;
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), array_buf->getPointer(color_offset));
}
glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), array_buf->getPointer(pos_offset));
glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), array_buf->getPointer(texel_offset));
for (const auto &it : attached_attributes)
{
Mesh *mesh = it.second.mesh.get();
@@ -309,7 +298,7 @@ void SpriteBatch::draw(const Matrix4 &m)
count = std::min(count, next - start);
GLBuffer::Bind element_bind(*quad_indices.getBuffer());
quad_indices.getBuffer()->bind();
const void *indices = quad_indices.getPointer(start * quad_indices.getElementSize());
if (count > 0)
@@ -327,8 +316,6 @@ void SpriteBatch::addv(const Vertex *v, const Matrix4 &m, int index)
if (color)
setColorv(sprite, *color);
GLBuffer::Bind bind(*array_buf);
// Always keep the VBO mapped when adding data for now (it'll be unmapped
// on draw.)
array_buf->map();