This topic is a bit linked to the other one I created earlier.
Currently, my 2D game scene is composed of several objects (around 500), each having it's own geometry (from 4 to 1000 vertices). I have a total of 30000 vertices in my game World. These objects can change in position, size, color, depth. There is no lighting done, so I'm only using BasicEffect with VertexPositionColor (or eventually VertexPositionColorTexture if needed).
The basic question is : how can I efficiently draw these vertices ?
Currently, I have one big DynamicVertexBuffer and one big DynamicIndexBuffer for the whole game, each geometry using a defined part of them. When one of the object parameters changes, I update its vertices and set them in the big buffer using SetData with SetDataOptions.NoOverwrite.
I also have only one BasicEffect for the whole game.
When Draw() is called on an object, I store that call in a DrawBatch. When every draw calls has been batched, I sort them by object's depth in ascending order (I need alpha blending), and then I call an ApplyDraw method on each object, something similar to this code :
Device.Indices = commonIndexBuffer;
foreach (EffectPass pass in commonEffect.CurrentTechnique.Passes)
Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, objectVertexBufferOffset, objectIndexBufferOffset,
objectIndexCount / 3);
Why I'm not satisfied ? Because :
- I can't find a proper way to allocate a part of a DynamicVertexBuffer to a specific object without some side effects (mad flickering when using SetDataOptions.Discard, some weird behavior like stated in the other topic when using SetDataOptions.NoOverwrite)
- My game sometimes lags at 60 fps on Windows and lags badly on Android. I did a CPU profiling, and it's pretty clear that anything that is eating time on each tick is drawing-related. Moreover, on Windows, the Task Manager shows around 50% GPU usage for the game when it's running, which is probably an alert that something might be wrong with the way I'm drawing everything
What I already tried :
- One BasicEffect per object : obviously performance was really bad
- A common BasicEffect for all objects, but one VertexBuffer/IndexBuffer per object : performance was not great either
Thanks for your help !