BCPowers
BCPowers

Reputation: 133

XNA - How can I draw my vertices more efficiently?

Using the code below, 1 maxed out mesh draws at 60 FPS, 2 maxed out meshes draw at 33~ FPS, 3 maxed out meshes draw at 28~ FPS, 4 maxed out meshes draw at 20~ FPS. Am I doing something wrong, or am I reaching some sort of limit? It doesn't seem like I am drawing a lot of polygons but I am still new to programming so I don't know much. Please offer some efficiency advice. Thank you.

class PolygonManager
{
    List<List<VertexPositionColor>> vertices;
    VertexBuffer vertexBuffer;
    List<List<int>> indices;
    IndexBuffer indexBuffer;
    int meshRef;
    int indexRef;
    Random random;

    public PolygonManager()
    {
        vertices = new List<List<VertexPositionColor>>();
        vertices.Add(new List<VertexPositionColor>());
        indices = new List<List<int>>();
        indices.Add(new List<int>());
        meshRef = -1;
        indexRef = 0;
        random = new Random();
    }

    public void CreateMesh(int length, int width, Vector3 position, Color color)
    {
        meshRef = -1;
        indexRef = 0;
        for (int i = 0; i < vertices.Count; i++)
        {
            if (vertices[i].Count <= 65536 - (length * width))
                meshRef = i;
        }

        if (meshRef == -1)
        {
            vertices.Add(new List<VertexPositionColor>());
            indices.Add(new List<int>());
            meshRef = vertices.Count - 1;
        }

        indexRef = vertices[meshRef].Count;

        for (int y = 0; y < length; y++)
        {
            for (int x = 0; x < width; x++)
            {
                vertices[meshRef].Add(new VertexPositionColor(new Vector3(x, 0, y) + position,
                                      new Color(color.R + (random.Next(-10, 10) / 100), color.G + (random.Next(-10, 10) / 100), color.B + (random.Next(-10, 10) / 100))));
            }
        }

        for (int y = 0; y < length - 1; y++)
        {
            for (int x = 0; x < width - 1; x++)
            {
                int topLeft = x + y * width;
                int topRight = (x + 1) + y * width;
                int lowerLeft = x + (y + 1) * width;
                int lowerRight = (x + 1) + (y + 1) * width;

                indices[meshRef].Add(topLeft + indexRef);
                indices[meshRef].Add(lowerRight + indexRef);
                indices[meshRef].Add(lowerLeft + indexRef);

                indices[meshRef].Add(topLeft + indexRef);
                indices[meshRef].Add(topRight + indexRef);
                indices[meshRef].Add(lowerRight + indexRef);
            }
        }
    }

    public void Draw(GraphicsDevice graphicsDevice, BasicEffect basicEffect)
    {
        for (int v = 0; v < vertices.Count; v++)
        {
            vertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionColor), vertices[v].Count, BufferUsage.WriteOnly);
            vertexBuffer.SetData<VertexPositionColor>(vertices[v].ToArray());
            graphicsDevice.SetVertexBuffer(vertexBuffer);
            indexBuffer = new IndexBuffer(graphicsDevice, typeof(int), indices[v].Count, BufferUsage.WriteOnly);
            indexBuffer.SetData<int>(indices[v].ToArray());
            graphicsDevice.Indices = indexBuffer;
            foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes)
            {
                effectPass.Apply();
                for (int i = 0; i < 6; i++)
                {
                    graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices[v].Count, 0, indices[v].Count/3);
                }
            }
        }
    }
}

Upvotes: 3

Views: 418

Answers (1)

Lucius
Lucius

Reputation: 3745

Moving the code where you initialize the buffers and write the data outside of the draw method should increase performance significantly.

Creating vertex and index buffers is an expensive operation. For static meshes (where the vertices don't change) you can reuse the buffers.

If the vertices/indices change often (once per frame) use a dynamic buffer.

Upvotes: 4

Related Questions