TheCherno / Sparky

Cross-Platform High Performance 2D/3D game engine for people like me who like to write code.
Apache License 2.0
1.1k stars 222 forks source link

Batch renderer submit optimization #40

Open LPeter1997 opened 9 years ago

LPeter1997 commented 9 years ago

So the batch renderer's submit(...) function takes a lot of time, mainly because of the matrix multiplications. With a little trick I was able to cut down the time (60% to 50%). Here's what I did:

Maths::vec3f _tpos = m_TransformationBack \ position;

m_Buffer->Position = _tpos; m_Buffer->TexCoord = texCoords[0]; m_Buffer->TexID = ts; m_Buffer->Color = c; m_Buffer++;

_tpos.y += size.y;

m_Buffer->Position = _tpos; m_Buffer->TexCoord = texCoords[1]; m_Buffer->TexID = ts; m_Buffer->Color = c; m_Buffer++;

_tpos.x += size.x;

m_Buffer->Position = _tpos; m_Buffer->TexCoord = texCoords[2]; m_Buffer->TexID = ts; m_Buffer->Color = c; m_Buffer++;

_tpos.y -= size.y;

m_Buffer->Position = _tpos; m_Buffer->TexCoord = texCoords[3]; m_Buffer->TexID = ts; m_Buffer->Color = c; m_Buffer++;

So I'm saving the position, multyply it only once with the matrix, and then keep working with that. Because I don't have a very good computer, without this optimization I got around 3025 FPS. With this I've got almost 3300.

PedDavid commented 9 years ago

Nice one! Just got 200 fps's for free :+1: I think you should make a pull request

Luminiscental commented 8 years ago

This breaks rotation and scale for the transformation though...

eatplayhate commented 8 years ago

Yeah this doesn't produce the same results. A better optimization is to write a SIMD multiply function and process the positions as a batch.