This ought to be XNA's bug.
I have two 3d models, currentModel and nextModel and I would like to apply special effect on these two models, to say morph one to another.
Code:
void DrawModelMorphing(Model currentModel, Model nextModel)
{
int targetIndex = 0;
foreach(var mesh in currentModel.Meshes.Count)
{
foreach(var mp in mesh.MeshParts)
{
GraphicsDevice.Indices = mp.IndexBuffer;
VertexBufferBinding[] vertexBufferBindings = new VertexBufferBinding[2];
vertexBufferBindings[0] = new VertexBufferBinding(mp.VertexBuffer, mp.VertexOffset);
vertexBufferBindings[1] = new VertexBufferBinding(nextModel.Meshes[targetIndex].MeshParts[0].VertexBuffer, nextModel.Meshes[targetIndex].MeshParts[0].VertexOffset);
GraphicsDevice.SetVertexBuffers(vertexBufferBindings);
mEffect.CurrentTechnique.Passes[0].Apply();
GraphicsDevice.DrawIndexedPrimitives(
PrimitiveType.TriangleList,
0,
0,
mesh.MeshParts[0].NumVertices,
mesh.MeshParts[0].StartIndex,
mesh.MeshParts[0].PrimitiveCount);
//vertexBufferBindings[0].VertexBuffer.Dispose(); //exception
//vertexBufferBindings[1].VertexBuffer.Dispose(); //exception
}
}
targetIndex++;
}
}
GraphicsDevice.SetVertexBuffers
will cause serious memory leaks, 300MB in 30 minutes, then "Out of Memory"
Here is one similar issue on different function call: OutOfMemory Exception when drawing cube
Something I had tested:
1) Event if I define vertexBufferBindings[]
globally, problem remains.
2) If I dispose the the vertex buffer, there will be exception. "A first chance exception of type 'System.ObjectDisposedException' occurred in Microsoft.Xna.Framework.dll"
3) GC and some 3rd party tool like freeMem will not work too.
Can anyone help on this?
I nearly want to give up using XNA in my project.
Thanks.
Edit:
The solution is to cache everything that I needed, eg: VertexBuffer and Textural