Implement automatic batching for points, lines, shapes, and images/quads.

The above are batched together into a single draw call when called without other drawing calls in between, as long as the following criteria are met:

- The texture is the same.
- The primitive type is the same (points cannot be batched with non-points).
- The love.graphics state is the same (aside from the current color - i.e. setColor - and the transform state).
- The active shader’s uniform values are the same.

You can examine the ‘drawcalls’ field of love.graphics.getStats() to help determine if your code is allowing for optimum batching.

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2016-12-27 21:02:58 -04:00
parent f98b7ce1ed
commit 0d2e08baff
43 changed files with 1245 additions and 511 deletions
+12 -18
View File
@@ -27,6 +27,7 @@
// LOVE
#include "GLBuffer.h"
#include "graphics/Texture.h"
#include "graphics/Graphics.h"
// C++
#include <algorithm>
@@ -240,21 +241,22 @@ bool SpriteBatch::getDrawRange(int &start, int &count) const
return true;
}
void SpriteBatch::draw(const Matrix4 &m)
void SpriteBatch::draw(Graphics *gfx, const Matrix4 &m)
{
const size_t pos_offset = offsetof(Vertex, x);
const size_t texel_offset = offsetof(Vertex, s);
const size_t color_offset = offsetof(Vertex, r);
const size_t color_offset = offsetof(Vertex, color.r);
if (next == 0)
return;
gfx->flushStreamDraws();
OpenGL::TempDebugGroup debuggroup("SpriteBatch draw");
OpenGL::TempTransform transform(gl);
transform.get() *= m;
Graphics::TempTransform transform(gfx, m);
gl.bindTextureToUnit(*(GLuint *) texture->getHandle(), 0, false);
gl.bindTextureToUnit(texture, 0, false);
uint32 enabledattribs = ATTRIBFLAG_POS | ATTRIBFLAG_TEXCOORD;
@@ -315,8 +317,11 @@ void SpriteBatch::addv(const Vertex *v, const Matrix4 &m, int index)
m.transform(sprite, sprite, 4);
if (color)
setColorv(sprite, *color);
if (color != nullptr)
{
for (int i = 0; i < 4; i++)
sprite[i].color = *color;
}
// Always keep the VBO mapped when adding data for now (it'll be unmapped
// on draw.)
@@ -325,17 +330,6 @@ void SpriteBatch::addv(const Vertex *v, const Matrix4 &m, int index)
array_buf->fill(index * sprite_size, sprite_size, sprite);
}
void SpriteBatch::setColorv(Vertex *v, const Color &color)
{
for (size_t i = 0; i < 4; ++i)
{
v[i].r = color.r;
v[i].g = color.g;
v[i].b = color.b;
v[i].a = color.a;
}
}
} // opengl
} // graphics
} // love