Dynamic Vertex Buffers

Started by
6 comments, last by alirakiyan 13 years ago
hi .

what would happen if I edit some vertex value after Unlock()?

for example what would happen if I fill an array of Pointers To Vertices and change the values any other place in my code?


char *pData;
// Lock the vertex buffer
m_pVB->Lock( 0, 0, (void**)&pData, flags )
....

....
// Unlock vertex buffer
m_pVB->Unlock()

//and then ...

pData[0] = Some Data. // Some changes on some vertex




instead of this:




char *pData;
// Lock the vertex buffer
m_pVB->Lock( 0, 0, (void**)&pData, flags )

// Copy vertices to vertex buffer
memcpy( pData, pVertices, numVertices * m_vertexSize );

// Unlock vertex buffer
m_pVB->Unlock() ;
Advertisement
a Lock does what it implies: gives you exclusive access to the data. After the lock is released, the GPU can read/or write to the vertices and if you attempt to read or write to them, then you could either read junk data (because the gpu was writing to that exact spot when you were trying to read), or visa versa for the write.

So, you could probably (without error), read or write to an address after the lock, but you would likely get errors from time to time. It is the same reasoning why you would use a lock (mutex) on shared data when writing a multi threaded program.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
thanks

so Lock() and Unlock() are to prevent data sharing .right?

how can I create two mesh objects with one vertex buffer so that when I change vertex data, both meshes are affected?
is there any way to change Vertex Data outside Lock() and Unlock()?

thanks

so Lock() and Unlock() are to prevent data sharing .right?
They're to allow you to access the vertex data. If you try to use data after Unlock(), then if you're lucky it'll crash there and then. If you're unlucky, it'll appear to work fine and then crash unexpectedly 6 months down the line or on someone elses machine where you can't debug it and where you'll find it very difficult to find the cause.


how can I create two mesh objects with one vertex buffer so that when I change vertex data, both meshes are affected?
is there any way to change Vertex Data outside Lock() and Unlock()?
If you're using your own mesh class, there's no problem in sharing the vertex buffer. If you're using ID3DXMesh, then there's no way to share a vertex or index buffer between instances that I'm aware of. You could however, just use one ID3DXMesh object, and render it twice.


EDIT: To give a bit more information about why accessing vertex data after Unlock() is bad: If the resource is in D3DPOOL_DEFAULT, then the resource might exist only in VRAM. In that case, when you call Lock(), the driver can create a temporary buffer in system memory, which is returned from the Lock() call. When you call Unlock(), the driver can copy that buffer into VRAM and free it - so if you access the pointer after the Unlock(), you're no longer accessing the actual vertex data, you're prodding around in memory that's been freed up, or is reserved by the driver.
Further, for dynamic resources, the driver allocates a sort of swap chain of buffers, and returns the oldest one, and the driver is allowed to run a frame behind the CPU. So poking around in memory you've released back to the driver may have the effect of changing the vertices for data you've already rendered, which will be difficult to track down/

Also, doing things like this is a very good way of causing a BSOD on older drivers.
tahnks .great explanation.

suppose that you have a mesh object and you wanna divide it to two indipendent mesh objects.(for example due to spatial partitioning or water or an exploding box)

one way is to forget parent mesh and only work with subsets(as independent meshes).
(another way is to simply edit vertex data in parent mesh and it's subsets!!! but we know it may cause low fps)


consider that some times you wanna work with parent mesh , and some times you may want to work with independent subsets.

so if you wanna change the vertex data in the parent mesh ,you should change the vertex data in subsets too(and vice versa).

what is your suggestion?

how should I edit vertex data?

ID3DXMesh class is the standard class for meshes and I can work with it in shadow,effects,collision and explosion,morph and...

does it worth to work with some personal mesh class instead of ID3DXMesh or not?
hello!

any helps?




Please wait 24 hours before bumping your post; remember that everyone is in different timezones and may not be around to help when you post a question.

As for answering the question:
This sounds exactly what subsets are for - a "piece" of a mesh that can be transformed independently from the rest.

ID3DXMesh is a helper class, it's not supposed to be any kind of ultimate solution. If you need to share vertex or index buffers, or anything else that's not supported by ID3DXMesh, then you could consider writing your own mesh class, yes.
in case of static meshes,there are some solutions.

I may change attributes so that any BIG subset is divided.

what do you suggest in case of partitioning dynamic objects?

could you please help me in updating dynamic subsets in a spatial tree (or BVH)?

This topic is closed to new replies.

Advertisement