Vertexbuffer manipulation

Started by
3 comments, last by Gruik 18 years, 10 months ago
Hello, [i've posted this on "Beginners" forum a few days ago, but no answer] I'm a DirectX newbie that made some test handling vertices and a few questions went to me : 1) If i want to move a vertex, do i really need to repopulate entirely the vertexbuffer? Do i really need to recreate a vertex array and putting this array in the vertexbuffer doing this (c#) : GraphicStream stm = vb.Lock (0, 0, 0); stm.Write (myArrayOfVertices); vb.UnLock(); ? Can't i get one vertex in the vertexbuffer and change only one proprety (a coordinate, for exemple) ? 2) Should i use 1 vertexbuffer for each of my primitive? And then print them like this : device.SetStreamSource(0, vb1, 0); device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 3); device.SetStreamSource(0, vb2, 0); device.DrawPrimitives(PrimitiveType.TriangleList, 0, 7); .. ? Or should i put all my vertices in a single VB and deal with index/offset/number of primitives (or things like that) ? 3) I've noticed a device can be "reset", making the vertexbuffer recreated. This happen when the client area is resized. Does this happen in other situations? Which other events should i take care? Thanks
Advertisement
hi,

1) of course you can only update 1 vertex. You seem to be using Managed DirectX, and I only know standard DirectX, but it's something like that :

VERTEX *vertex; // custom vertex format
vb->Lock(0, (void **)&vertex, 0, 0);
vertex[10].position = D3DXVECTOR3(0.0f, 0.0f, 0.0f); // update the 10th vertex
vb->Unlock();

2) No fixed rule here. If each primitive is pretty small, you better batch them into 1 bigger primitive. Usually, it's said that the optimial size of a batch (vb / ib) is around 1k (in memory size) ... you can do some tests to find that out, but usually, the rule is to avoid small batches (under 100 faces)

3) Hmm I don't really concern myself with that, I let the SDK framework handle that for me ^^
Quote:Original post by Gruik
Hello,

[i've posted this on "Beginners" forum a few days ago, but no answer]
I'm a DirectX newbie that made some test handling vertices and a few questions went to me :

1) If i want to move a vertex, do i really need to repopulate entirely the vertexbuffer? Do i really need to recreate a vertex array and putting this array in the vertexbuffer doing this (c#) :
GraphicStream stm = vb.Lock (0, 0, 0);
stm.Write (myArrayOfVertices);
vb.UnLock();
?
Can't i get one vertex in the vertexbuffer and change only one proprety (a coordinate, for exemple) ?
You can. Look at those parameters you're passing to vb.Lock() - they let you specify a region of the buffer to lock. So you can lock vertex 10, and write the updated vertex data into the graphicsstream.

However, bear in mind that doing so can be slow. Vertex buffers generally don't like being read from, so you want to avoid doing things like "vertex.position += offset" because the += requires reading back the current value of vertex.position.

Quote:
2) Should i use 1 vertexbuffer for each of my primitive? And then print them like this :
device.SetStreamSource(0, vb1, 0);
device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 3);
device.SetStreamSource(0, vb2, 0);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 7);
..
?
Or should i put all my vertices in a single VB and deal with index/offset/number of primitives (or things like that) ?
You generally don't want to use a seperate VB for each primitive. Each buffer adds a certain amount of overhead (using up more memory) and each SetStreamSource call does too. In my experience it's been better to group vertices by vertex format, so you put all the vertices with TBN data in one buffer, all the vertices with 2 sets of texcoords in another, etc, because a single system only tends to use one vertex type. Then, yes, setting up an index buffer to go with the vertex buffer, and setting up each of your primitives as start+count references to the index buffer, will be efficient.

Quote:
3) I've noticed a device can be "reset", making the vertexbuffer recreated. This happen when the client area is resized. Does this happen in other situations? Which other events should i take care?

I think there are certain things like the app losing focus (e.g. the screensaver coming on), going into hibernation, etc... that cause it too.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Thank you
Quote:3) I've noticed a device can be "reset", making the vertexbuffer recreated. This happen when the client area is resized. Does this happen in other situations? Which other events should i take care?


Actually, the device is not reset when the client area is resized. That only happens in the DirectX samples because Microsoft put code in to do that.

Secondly, if you put your resources into the Managed memory pool when creating the, you don't have to worry about recreating the vertex buffer when the device is reset. However, if you created the vertex buffer using the Dynamic mesh flag (which you're probably doing), you can't place it in Managed memory, and you must recreate the buffer yourself.
_______________________________________________________________________Hoo-rah.

This topic is closed to new replies.

Advertisement