In a 2D tile-based platformer I tried drawing all of my tiles using a vertex and index buffer with DrawIndexedPrimitives instead of a spritebatch because I heard it was MUCH faster, but it’s not faster than just using a spritebatch (it has the same CPU usage). First the TileAtlasBuffer is initialized, then all the tiles are added with AddTile (each tile is represented as a textured quad) and SetData is called. After that only the Draw method is called again. Specifically DrawIndexedPrimitives is no faster than just drawing all the tiles one by one with spritebatch. What am I doing wrong?
`
private class TileAtlasBuffer
{
public Texture2D texture;
public VertexBuffer vertexBuffer;
public IndexBuffer indexBuffer;
private VertexPositionTexture[] vertices;
private short[] indices;
private int tileCount;
private GraphicsDevice GraphicsDevice
{
get { return Core.Instance.GraphicsDevice; }
}
public TileAtlasBuffer(Texture2D texture, int tiles)
{
this.texture = texture;
vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), tiles * 4,
BufferUsage.WriteOnly);
indexBuffer = new IndexBuffer(GraphicsDevice, typeof(short), tiles * 6, BufferUsage.WriteOnly);
vertices = new VertexPositionTexture[tiles * 4];
indices = new short[tiles * 6];
tileCount = 0;
}
public void SetData()
{
vertexBuffer.SetData(vertices);
indexBuffer.SetData(indices);
}
public void Draw(BasicEffect basicEffect)
{
if (tileCount > 0)
{
GraphicsDevice.SetVertexBuffer(vertexBuffer);
GraphicsDevice.Indices = indexBuffer;
basicEffect.Texture = texture;
foreach (var pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, tileCount * 2);
}
}
}
public void AddTile(int tx, int ty, Rectangle rect, bool flippedHorizontally, bool flippedVertically)
{
float textureSizeX = 1f / texture.Width;
float textureSizeY = 1f / texture.Height;
float left = rect.Left * textureSizeX;
float right = rect.Right * textureSizeX;
float bottom = rect.Bottom * textureSizeY;
float top = rect.Top * textureSizeY;
if (flippedHorizontally)
{
float temp = left;
left = right;
right = temp;
}
if (flippedVertically)
{
float temp = top;
top = bottom;
bottom = temp;
}
int vertexCount = tileCount * 4;
vertices[vertexCount] = new VertexPositionTexture(new Vector3(tx, ty + rect.Height, 0), new Vector2(left, bottom));
vertices[vertexCount + 1] = new VertexPositionTexture(new Vector3(tx, ty, 0), new Vector2(left, top));
vertices[vertexCount + 2] = new VertexPositionTexture(new Vector3(tx + rect.Width, ty + rect.Height, 0), new Vector2(right, bottom));
vertices[vertexCount + 3] = new VertexPositionTexture(new Vector3(tx + rect.Width, ty, 0), new Vector2(right, top));
int indexCount = tileCount * 6;
indices[indexCount] = (short)vertexCount;
indices[indexCount + 1] = (short)(vertexCount + 1);
indices[indexCount + 2] = (short)(vertexCount + 2);
indices[indexCount + 3] = (short)(vertexCount + 2);
indices[indexCount + 4] = (short)(vertexCount + 1);
indices[indexCount + 5] = (short)(vertexCount + 3);
tileCount++;
}
}`