Hi,
As an exercise I have put togther a really basic SpriteBatchRenderer class, but what I've found is that far from improving performance when rendering, I have actually gone backwards. Oh no!
I am basing it on the following principles:
Constructor(Direct3D11.Device graphicsIn, Direct3D11.DeviceContext contextIn):
- Create empty indices and vertices data streams
- Create Index Buffer
- Set standard texture positions (I'm drawing simple textures for now on all sprites)
- Create Vertex Buffer
- Create otho matrix
- Create an array of textures for each sprite
Begin()
- Rewind data stream positions to 0
Add(vector3[] positionIn, string texturePath)
- Transform vectors passed in
- Write vectors to vertices data stream
- Write indices array to indices data stream
- Store texture for this sprite
- noOfSprites++;
End()
- Rewind data stream positions to 0
Draw()
- Set index buffer binding
- Set vertex buffer binding
- Draw each sprite in sprite array using corresponding texture in texture array
ResetSpriteCount()
- noOfSprites = 0;
Dispose()
- Does nothing at present
When I use this class, calling Begin(), a series of Add() calls, then End(), Draw(), and then ResetSpriteCount(), nothing appears at all. I've debugged this to see if even one frame is drawn, and it is not.
As an experiement I created another version of the SpriteBatchRenderer() class, which doesn't create the index or vertex buffers in the Constructor. Instead it creates them, and sets the bindings, in the End() method. This WORKS, but is apallingly slow, as each frame new vertex buffers are created - which I think is what is slowing things down.
What I am trying to do is just create the index and vertex data streams ONCE, and the same for the index and vertex buffers, as I thought that it is the declaration of these structures that causes a performance hit. I want to work with the same vertex buffer for the entire lifetime of the game.
Clearly I'm doing something very wrong indeed.