I'm building a voxel world generator with XNA where the voxels are rendered as polygonal cubes. The world is divided into 1024 chunks of 32x32x256 cubes each (as 32 chunks by 32 chunks), and each chunk has its own index and vertex buffer. Since each chunk is different, vertex buffer size is determined on runtime.
To prevent massive stalling on opening the program, only one chunk is re-built per frame until all chunks are built. Occasionally the program crashes with an "insufficient memory" related error that is caused by the line that creates the vertex buffer. Here is how I set them up:
vb = new VertexBuffer(
device,
VertexPositionColorNormal.VertexDeclaration,
nextIndex,
BufferUsage.None
);
ib = new IndexBuffer(
device, IndexElementSize.ThirtyTwoBits,
nextIndex,
BufferUsage.None
);
My vertex structure is not very large, using only a Vector3 for position and Byte4 for color and normal lookup info:
public struct VertexPositionColorNormal : IVertexType
{
public Vector3 Position;
public uint Data;
public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
(
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
new VertexElement(12, VertexElementFormat.Byte4, VertexElementUsage.TextureCoordinate, 0)
);
VertexDeclaration IVertexType.VertexDeclaration { get { return VertexDeclaration; } }
}
NextIndex is the number of vertices and indices allocated to the buffers (always 1 index per vertex).
Am I making too many buffers? It crashes despite the GC showing only about 12 MB of usage so I guess memory fragmentation is a problem when creating lots of buffers in a short time? Should I use a smaller amount of buffers that store more data and have several chunks share them?
EDIT: I estimate roughly 0.75 to 1 GB of memory used in the vertex buffers. My graphics card has 2GB of VRAM. Though I also know sometimes not all the vertex data may be stored in VRAM at one time.