I'm looking the youtube videos therefore I'm a little behind. So delete this comment if it's out of date.
I saw that your batch renderer is calculating vertex postions every submit. This is quite inefficient.
My suggestion is a rectangle class/struct that precalculate all vertecies on the constructor call of a sprite. This works as long as the size of a sprite doesn't change. I was able to achieve about a 8-10% higher fps rate then before.
struct Rect
{
vec3 topLeftPositon;
vec3 topRightPositon;
vec3 bottomLeftPositon;
vec3 bottomRightPositon;
enum Alignment
{
BottomLeft,
BottomRight,
TopLeft,
TopRight,
CenteredLeft,
CenteredRight,
BottomCentered,
TopCentered,
Centered
};
Rect(Alignment alignment = Alignment::BottomLeft);
Rect(float x, float y, float width, float height, Alignment alignment = Alignment::BottomLeft);
inline const vec3& GetTopLeftPositon() const { return topLeftPositon; }
inline const vec3& GetTopRightPositon() const { return topRightPositon; }
inline const vec3& GetBottomLeftPositon() const { return bottomLeftPositon; }
inline const vec3& GetBottomRightPositon() const { return bottomRightPositon; }
};
Rect::Rect(Alignment alignment)
: Rect(1, 1, 1, 1, alignment)
{
}
Rect::Rect(float x, float y, float width, float height, Alignment alignment)
{
float halfWidth = 0.5f + width;
float halfHeight = 0.5f * height;
switch (alignment)
{
case Alignment::BottomLeft:
topLeftPositon = vec3(x, y + height, 0);
topRightPositon = vec3(x + width, y + height, 0);
bottomLeftPositon = vec3(x, y, 0);
bottomRightPositon = vec3(x + width, y, 0);
break;
case Alignment::BottomCentered:
topLeftPositon = vec3(x - halfWidth, y + height, 0);
topRightPositon = vec3(x + halfWidth, y + height, 0);
bottomLeftPositon = vec3(x - halfWidth, y, 0);
bottomRightPositon = vec3(x + halfWidth, y, 0);
break;
case Alignment::BottomRight:
topLeftPositon = vec3(x - width, y + height, 0);
topRightPositon = vec3(x, y + height, 0);
bottomLeftPositon = vec3(x - width, y, 0);
bottomRightPositon = vec3(x, y, 0);
break;
case Alignment::CenteredLeft:
topLeftPositon = vec3(x, y + halfHeight, 0);
topRightPositon = vec3(x + width, y + halfHeight, 0);
bottomLeftPositon = vec3(x, y - halfHeight, 0);
bottomRightPositon = vec3(x + width, y - halfHeight, 0);
break;
case Alignment::Centered:
topLeftPositon = vec3(x - halfWidth, y + halfHeight, 0);
topRightPositon = vec3(x + halfWidth, y + halfHeight, 0);
bottomLeftPositon = vec3(x - halfWidth, y - halfHeight, 0);
bottomRightPositon = vec3(x + halfWidth, y - halfHeight, 0);
break;
case Alignment::CenteredRight:
topLeftPositon = vec3(x - width, y + halfHeight, 0);
topRightPositon = vec3(x, y + halfHeight, 0);
bottomLeftPositon = vec3(x - width, y - halfHeight, 0);
bottomRightPositon = vec3(x, y - halfHeight, 0);
break;
case Alignment::TopLeft:
topLeftPositon = vec3(x, y, 0);
topRightPositon = vec3(x + width, y, 0);
bottomLeftPositon = vec3(x, y - height, 0);
bottomRightPositon = vec3(x + width, y - height, 0);
break;
case Alignment::TopCentered:
topLeftPositon = vec3(x - halfWidth, y, 0);
topRightPositon = vec3(x + halfWidth, y, 0);
bottomLeftPositon = vec3(x - halfWidth, y - height, 0);
bottomRightPositon = vec3(x + halfWidth, y - height, 0);
break;
case Alignment::TopRight:
topLeftPositon = vec3(x - width, y, 0);
topRightPositon = vec3(x, y, 0);
bottomLeftPositon = vec3(x - width, y - height, 0);
bottomRightPositon = vec3(x, y - height, 0);
break;
default:
topLeftPositon = vec3(x, y + height, 0);
topRightPositon = vec3(x + width, y + height, 0);
bottomLeftPositon = vec3(x, y, 0);
bottomRightPositon = vec3(x + width, y, 0);
break;
}
}
A sprite has a rect member and in the constructor this rect is generated.
So the batch renderer submit function looks like this (I commented out the old code):
void BatchRenderer2D::Submit(const Renderable2D* renderable)
{
//const vec3& position = renderable->GetPosition();
//const vec2& size = renderable->GetSize();
const vec4& color = renderable->GetColor();
int r = color.x * 255.0f;
int g = color.y * 255.0f;
int b = color.z * 255.0f;
int a = color.w * 255.0f;
unsigned int col = a << 24 | b << 16 | g << 8 | r;
const Rect* rect = ((Sprite*)renderable)->GetRect();
//buffer->vertex = position;
buffer->vertex = rect->GetBottomLeftPositon();
buffer->color = col;
buffer++;
//buffer->vertex = vec3(position.x, position.y + size.y, position.z);
buffer->vertex = rect->GetTopLeftPositon();
buffer->color = col;
buffer++;
//buffer->vertex = vec3(position.x + size.x, position.y + size.y, position.z);
buffer->vertex = rect->GetTopRightPositon();
buffer->color = col;
buffer++;
//buffer->vertex = vec3(position.x + size.x, position.y, position.z);
buffer->vertex = rect->GetBottomRightPositon();
buffer->color = col;
buffer++;
indexCount += 6;
}
I hope this is useful for you.