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

Performance XNA-如何更有效地绘制顶点?,performance,xna,Performance,Xna,使用下面的代码, 1个最大网格以60 FPS的速度绘制, 以每秒33帧的速度绘制2个最大网格, 以每秒28帧的速度绘制3个最大网格, 4个最大网格以20 FPS的速度绘制。 我是做错了什么,还是达到了某种极限?看起来我画的多边形并不多,但我还是编程新手,所以我知道的不多。请提供一些效率建议。多谢各位 class PolygonManager { List<List<VertexPositionColor>> vertices; VertexBuffer v

使用下面的代码, 1个最大网格以60 FPS的速度绘制, 以每秒33帧的速度绘制2个最大网格, 以每秒28帧的速度绘制3个最大网格, 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);
                }
            }
        }
    }
}
类PolygonManager { 列出顶点; 顶点缓冲区顶点缓冲区; 列出索引; IndexBuffer IndexBuffer; int meshRef; 内部索引外部参照; 随机; 公共多克隆管理器() { 顶点=新列表(); 添加(新列表()); 索引=新列表(); 添加(新列表()); meshRef=-1; indexRef=0; 随机=新随机(); } 公共void CreateMesh(整数长度、整数宽度、矢量3位置、颜色) { meshRef=-1; indexRef=0; 对于(int i=0;i如果(顶点[i].Count将代码移动到初始化缓冲区的位置,并在draw方法之外写入数据,则会显著提高性能

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


如果顶点/索引经常更改(每帧一次)使用动态缓冲区。

我发布了一个关于暂停管道的长答案,但不知何故完全忽略了他在每帧分配全新缓冲区的事实。这无疑是正确的答案。非常感谢,我将尝试它。我很高兴这个世界上有帮助的人。我尝试了它,它很有效。我创建了一个l顶点缓冲区和索引缓冲区列表,以便我创建的网格有自己的,并在我创建网格时定义。FPS恢复到60,有4个最大网格。再次感谢。如果我增加你的声誉,我会的。