Jump to content

  • Log In with Google      Sign In   
  • Create Account

Loading model meshes into device vertex buffers


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 davepl1968   Members   -  Reputation: 166

Like
0Likes
Like

Posted 08 March 2013 - 05:27 PM

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, 08 March 2013 - 05:28 PM.


Sponsor:

#2 MJP   Moderators   -  Reputation: 11751

Like
2Likes
Like

Posted 08 March 2013 - 07:40 PM

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.



#3 davepl1968   Members   -  Reputation: 166

Like
0Likes
Like

Posted 09 March 2013 - 01:20 PM

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?



#4 MJP   Moderators   -  Reputation: 11751

Like
0Likes
Like

Posted 09 March 2013 - 02:30 PM

No. DrawUserPrimitive only works with arrays of vertex data, not with vertex buffers.



#5 davepl1968   Members   -  Reputation: 166

Like
0Likes
Like

Posted 09 March 2013 - 03:41 PM

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. 



#6 phil_t   Crossbones+   -  Reputation: 4094

Like
1Likes
Like

Posted 09 March 2013 - 04:32 PM

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.

#7 MJP   Moderators   -  Reputation: 11751

Like
1Likes
Like

Posted 09 March 2013 - 07:20 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS