vertexBuffer = new VertexBuffer(Device, VertexPositionNormalTextureColorTangent.SizeInBytes * vertexCount, Usage.WriteOnly, SlimDX.Direct3D9.VertexFormat.None, Pool.Default);
Can I delete/clear the vertexbuffer before each call? I have about 2GB of video memory according to DXdiag. I have DXdiag debugging turned on and break on memory leaks but I dont see any messages in the output window.
Also in the code below, I am trying to avoid creating arrays on the large object heap so as to avoid fragmentation. But I am having second thoughts on my approach. I calculate the maxloh array size based on the LOH limit of 85k, im using 80 to be safe.
I calculate the capacity of each array and then create new ones when the max size is reached.
int sizeNmTxTn = maxloh / VertexPositionNormalTextureColorTangent.SizeInBytes;
I check if size of the list is greater than or equal to the maximum size allowed to avoid arrays being sent to LOH and add new ones as necessary.
if (verticesNmTxTn.Count >= sizeNmTxTn)
{
verticesNmTxTn = new List<VertexPositionNormalTextureColorTangent>(sizeNmTxTn);
verticesNmTxTxlst.Add(verticesNmTxTn);
}
What do you guys think of this approach? For managed d3d apps (slimdx), It is a pain to have to break down arrays into smaller ones to avoid them being created on LOH.
Are there better approaches to creating large vertexbuffers and not create arrays on LOH?
public void LoadVerticesNmTxTn()
{
Geometry geometry = mesh.Geometry;
Matrix meshRootTransform = getModelTransformationMatrix();
List<Vertex> vertexlst = geometry.Vertices;
vertexCount = vertexlst.Count;
int maxloh = 80000;
int sizeNmTxTn = maxloh / VertexPositionNormalTextureColorTangent.SizeInBytes;
List<List<VertexPositionNormalTextureColorTangent>> verticesNmTxTxlst = new List<List<VertexPositionNormalTextureColorTangent>>();
verticesNmTxTn = new List<VertexPositionNormalTextureColorTangent>(sizeNmTxTn);
verticesNmTxTxlst.Add(verticesNmTxTn);
foreach (Vertex vertexData in vertexlst)
{
float x = vertexData.Position.X;
float y = vertexData.Position.Y;
float z = vertexData.Position.Z;
Vector3 position = new Vector3(x, y, z);
position = Vector3.TransformCoordinate(position, meshRootTransform);
float nX = vertexData.Normal.X;
float nY = vertexData.Normal.Y;
float nZ = vertexData.Normal.Z;
Vector3 normalPosition = new Vector3(nX, nY, nZ);
//normalPosition = Vector3.TransformCoordinate(normalPosition, meshRootTransform);
float u = vertexData.TextureCoordinate.U;
float v = -(vertexData.TextureCoordinate.V - 1);
float w = vertexData.TextureCoordinate.W;
Vector2 textureCoordinate = new Vector2(u, v);
float tX = vertexData.Tangent.X;
float tY = vertexData.Tangent.Y;
float tZ = vertexData.Tangent.Z;
Vector3 tangentPosition = new Vector3(tX, tY, tZ);
float R = diffuseColor.X;
float G = diffuseColor.Y;
float B = diffuseColor.Z;
float A = diffuseColor.W;
Color4 diffuse = new Color4(A, R, G, B);
if (verticesNmTxTn.Count >= sizeNmTxTn)
{
verticesNmTxTn = new List<VertexPositionNormalTextureColorTangent>(sizeNmTxTn);
verticesNmTxTxlst.Add(verticesNmTxTn);
}
verticesNmTxTn.Add(new VertexPositionNormalTextureColorTangent(position, normalPosition, textureCoordinate, diffuse, tangentPosition));
}
if (VertexPositionNormalTextureColorTangent.SizeInBytes * vertexCount > Device.AvailableTextureMemory)
{
MessageBox.Show("Not enough texture memory available!");
}
vertexBuffer = new VertexBuffer(Device, VertexPositionNormalTextureColorTangent.SizeInBytes * vertexCount, Usage.WriteOnly, SlimDX.Direct3D9.VertexFormat.None, Pool.Default);
DataStream dataStream = vertexBuffer.Lock(0, 0, LockFlags.None);
foreach (List<VertexPositionNormalTextureColorTangent> vlist in verticesNmTxTxlst)
{
foreach (VertexPositionNormalTextureColorTangent vertex in vlist)
{
dataStream.Write<VertexPositionNormalTextureColorTangent>(vertex);
}
}
vertexBuffer.Unlock();
}