Jump to content
  • Advertisement
Sign in to follow this  
alirakiyan

Dynamic Vertex Buffers

This topic is 2782 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

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() ;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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()?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)?

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!