I have been working on 2d tile map loading and rendering and im noticing very bad performance with my simple approach.
Basically i have a 2d array which specify the tiles in the map using a number to represent each type of tile (water, rock etc.) Each of these are a sprite of its own with a vertex buffer and a texture. All tiles are 32x32.
This is the render function of the map:
XMMATRIX VPMatrix = g_Game->GetViewProjectionMatrix();
ID3D11Buffer* MVPBufferHandle = g_Game->GetMVPBuffer();
ID3D11DeviceContext* D3DContext = g_Game->GetContext();
XMMATRIX world = GetWorldMatrix();
for(int i = 0; i < m_TileList.size(); i++)
{
for(int j = 0; j < m_TileList.size(); j++)
{
XMMATRIX offset = XMMatrixMultiply(world, XMMatrixTranslation(j * m_TileSize, i * m_TileSize, 0.0f));
XMMATRIX mvp = XMMatrixMultiply(offset, VPMatrix);
mvp = XMMatrixTranspose(mvp);
D3DContext->UpdateSubresource(MVPBufferHandle, 0, 0, &mvp, 0, 0);
D3DContext->VSSetConstantBuffers(0, 1, &MVPBufferHandle);
m_SpriteList[m_TileList[j]].Render();
}
}
Simply put i get the pointer for my constant buffer to send my final matrix to the shader, I multiply the location of each tile by the location of the map and the projectionview matrix and send it off for rendering. The render function of a sprite just binds the vertex buffer and the texture resource and renders a quad(using triangle list).
As an example i tried to render 100 tiles with only 2 types of sprites.
without map = 500 fps;
with map = ~250 fps;
This looks to me like very bad performance probably due to my approach but i have no idea how i can couple things together to save draw calls or texture binds
Can anyone guide me on how i can increase the performance?
Thanks