tags:

views:

31

answers:

1

Looking at the MSDN documentation, I can't find a way to lock VertexBuffers so that I can change their data while on the device. Is this possible in XNA?

+2  A: 

You have a number of options for modifying the contents of vertex buffers in XNA:

  • VertexBuffer has a SetData member. You can only safely use this outside of Draw in any case where you may activate Predicated Tiling (so it's good practice to simply always do it outside of Draw).

  • DynamicVertexBuffer is like VertexBuffer, but faster when settings its contents. However it is susceptible to the graphics device being lost, and this condition must be handled. Also take a look at SetDataOptions.

  • DrawUserPrimitives (and indexed version). This has the advantages of not affecting Predicated Tiling, and not causing the the command buffer to flush for small numbers of primitives.

There is more information on MSDN about Dynamically Updating Vertices. And this thread on the XNA forums may also be worth reading.

Andrew Russell
Thank you very much. This is a lot of good info. One other question: Is it ALWAYS safe to call SetData on a VertexBuffer outside Draw?
blachniet
@blachniet: Yes (actually it's a command-buffer thing, but, yes, outside Draw in the simple case). You sound like you are coming from a DirectX background: if need to be absolutely certain about what XNA is doing behind the scenes, you can always run your game through PIX.
Andrew Russell