XNA - 如何更有效地绘制顶点?

时间:2013-02-26 20:30:00

标签: performance xna

使用以下代码, 1个最大网格绘制为60 FPS, 2个最大网格绘制在33~FPS, 3个最大网格绘制在28~FPS, 4个最大网格绘制在20~FPS。 我做错了什么,还是达到某种限制?我似乎并没有绘制很多多边形,但是我还是编程新手所以我不太了解。请提供一些有效的建议。谢谢。

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);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:4)

移动初始化缓冲区的代码并在draw方法之外写入数据应该会显着提高性能。

创建顶点和索引缓冲区是一项昂贵的操作。对于静态网格物体(顶点不会改变),您可以重复使用缓冲区。

如果顶点/索引经常变化(每帧一次),请使用动态缓冲区。