# How to update “dynamicVertexBuffer” correctly with “setdata” on XNA?

I developed a small 3d games xna and uses a "DynamicVertexBuffer" and "DynamicIndexBuffer" to store and draw my vertices. Everything works fine, but my problem is the "Update" function where I update my buffer. Each apel update I have to reset my buffer using the keyword "new DynamicVertexBuffer (...." It goes down the performance and reduce the frames per second of my games.

Do I have to initialize my buffer each time in my function "Update ()"? Is it okay to reset every time my buffer to each call / frame

I can not find a solution to update correctly my Dynamicbuffer with "setData ()" function without having to initialize whenever my buffer just before. Am I obliged to Reset-"DynamicVertexBuffer" and "DynamicIndexBuffer" before the update? If I call "setData ()" before reset it, I throw an exception:

Works well but I have initialized every time "dynamicIndexBuffer" and "dynamicVertexBuffer" before update! (Consumes more resources?)

    public override void Update(GameTime gameTime)
{
foreach (Octree.Node node in regions[regionIndex].Nodes.Where(n => n.ItemGroup == a).Where(m => arcadia.camera.Frustum.Contains(m.boundingBox) != ContainmentType.Disjoint))
{
//build indices
node.Cube.SetUpIndices(indexIndiceBuild);
foreach (int indice in node.Cube.Indices)
{
}
foreach (VertexPositionTexture posText in node.Cube.Vertices)
{
}
indexIndiceBuild += 24;
}
//Declare and initialize new buffers
//VertexBuffer Initialize again
vertexBuffer_cube = new DynamicVertexBuffer(
VertexPositionTexture.VertexDeclaration,
vertexPositionStructureCube.Count,
BufferUsage.WriteOnly);

//VertexBuffer SetData (update)
vertexBuffer_cube.SetData<VertexPositionTexture>(vertexPositionStructureCube.ToArray());
//IndexBuffer Initialize again
indexBuffer_cube = new DynamicIndexBuffer(Game.GraphicsDevice, IndexElementSize.ThirtyTwoBits, list_structure_indices.Count, BufferUsage.WriteOnly);
//IndexBuffer SetData (update)
indexBuffer_cube.SetData(list_structure_indices.ToArray());
.........
}


I'm just trying to update with "SetData" with new vertices and indices but i throws the exception: "The array is not the proper size for the Amount of data requested."

    public override void Update(GameTime gameTime)
{
.........
//Only "setdata()" without initialize buffer again ??
//VertexBuffer SetData (update)
vertexBuffer_cube.SetData<VertexPositionTexture>(vertexPositionStructureCube.ToArray());

//IndexBuffer SetData (update)
indexBuffer_cube.SetData(list_structure_indices.ToArray());

.........
}

• Dynamic Vertex Buffers are a pain. Tried something similar to what you are attempting, then dropped them completely. They dont really improve performance in large scale voxel (cube) terrain engines. – Jason Coombes Apr 9 '13 at 12:00