Sign in to follow this  

Loading model meshes into device vertex buffers

This topic is 1774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It seems that every sample I've come across so far simply loads models with Content.Load<Model>(etc).

 

Then, when its time to draw them (in the XNA case) you wind up looping through the model.Meshes and drawing them.  This can't be very fast if its has to push all the vertex data of the model back to the card (or graphics device, whatever) each time.

 

So, I presumed that the right thing to do (especially if you have a model that's reused a lot) would be to load each of the meshes into a vertex buffer, keep track of the index, all of that.  Then, when you draw, you could use your card's vertex buffer DrawIndexedPrimitive() instead of mesh.Draw(), and it would use the vertexes already loaded into the card-controlled memory.

 

My questions are

(a) is this happening behind the scenes with Load<Model> and mesh.Draw() and I just don't realize it?

(b) if not, does anyone have an example they could point me at that does it "right" that I can learn from?

 

Thanks!

Dave

Edited by davepl1968

Share this post


Link to post
Share on other sites

The Model vertex data is loaded into vertex buffers when the model is loaded by the ContentManager. You can actually access these vertex buffers using the VertexBuffer property of ModelMeshPart.

Share this post


Link to post
Share on other sites

But aren't those in the user space, and would be drawn with DrawUserPrimitive (etc) as opposed to in card memory so they could be drawn with the DrawPrimitive (etc) calls?

Share this post


Link to post
Share on other sites

Thanks.  So just to be clear, when I call mesh.Draw() it's using a vertex buffer on the graphics card that was loaded back when I loaded the model, is that right?

 

My vocabulary might be wrong, so by "vertex buffer" I mean memory actually on the GPU die without having to go through northbridge/southbridge/whatever.

 

And some magical force takes care of allocating and managing the memory of the vertex buffer?  I notice that Model and ModelMesh don't implement IDisposable, so I'm curious as to when and how it would know to free up the vertex buffer. 

Share this post


Link to post
Share on other sites
Yes. mesh.Draw is essentially just calling DrawIndexedPrimitives under the covers. As MJP mentioned, the vertex and index buffers are accessible as properties on the ModelMesh/ModelMeshPart if you want to draw the mesh with DrawIndexedPrimitives directly.

And some magical force takes care of allocating and managing the memory of the vertex buffer?  I notice that Model and ModelMesh don't implement IDisposable, so I'm curious as to when and how it would know to free up the vertex buffer.

Yeah, that's kind of weird they don't implement IDisposable. I'm guessing that ContentManager must keep track of the vertex and index buffers that were created when the Model was loaded. So they would be freed when ContentManager is Disposed, just like the other resources.

Share this post


Link to post
Share on other sites

The "magical force" you speak of is the content pipeline. The content processor for models understands the concept of vertex and index buffers, so the model processor is able to take the vertex/index data and create vertex buffer and index buffer assets. Then when you load the model at runtime, the model automatically loads its vertex and index buffer assets. These assets create vertex buffers and index buffers on the GPU, and fill them with the data that was created in the content pipeline. A similar process happens with textures.

The way that content loaded from the content pipeline works is that you don't dispose of individual assets. Instead, the ContentManager that you used to load them is considered to have ownership of all assets that it loaded. To dispose of assets loaded with the stock ContentManager, you would call Unload or Dispose on the ContentManager itself. If you needed to dispose of individual assets, I think it should be possible if your derive from ContentManager to make a custom class.

Share this post


Link to post
Share on other sites
Sign in to follow this