Archived

This topic is now archived and is closed to further replies.

creative1

vertexbuffer lock/unlock issue

Recommended Posts

Hi I am trying to do something pretty simple. Update the fifth triangle in a vertex buffer. What i do is as simple as this CustomVertex.PositionColored[] ver = (CustomVertex.PositionColored[])this.vertexbuffer.Lock(0,0); ver[5*3].x=x; ver[5*3].y=y; ver[5*3].z=z; ver[5*3].color=whatevercolor; vertexbuffer.unlock(); everything looks fine but when i render this, i only get the fifth triangle and all the triangles from 1-4 seems destroyed. Why is this? Is the whole vertexbuffer deleted with I lock it like that? Also, would it be possible to lock only that fifth triangle and nothing else? how could that be done? Using directx 9

Share this post


Link to post
Share on other sites
if you call the lock function with D3DLOCK_DISCARD, then anything inside the buffer is lost. Doesn''t seem like you did that from your code (which i don''t really understand, is that C#?). Also make sure you didn''t create the vertex buffer with D3DUSAGE_WRITEONLY initally.

Share this post


Link to post
Share on other sites
well, i lock it with value 0, which i don''t know if it is discard but i think it is ''none''

and why i shouldn''t use write only ?

I am only writing to it, not reading right?
what flag should i use instead of write only?

Share this post


Link to post
Share on other sites
yes, index 4
sorry i typed it here fast, but the problem still continues
everything on that buffer gets erased.


Anyway, the problem persists.

Anyway knows why the write only flag is a bad idea?

Share this post


Link to post
Share on other sites
If you create it with D3DUSAGE_WRITEONLY, it will expect you to fill in the data in between for every frame. For instance, it just returns a pointer to some empty memory that you will fill up, and it will then overwrite the memory on the video card with your new data. This way, you only write to the video card, instead of reading and writing. Reading is MUCH slower.

If you don't use D3DUSAGE_WRITEONLY, then you will be telling DirectX to return a pointer to some memory that contains data that was in the video card. That way, you can just adjust one piece at a time. Then when you unlock the vertex buffer, all of the original data is still intact, plus any modifications you made. It's slower though.

One way to solve your problem would be to only lock the memory used for the 5th triangle. I think you would still want to use the D3DUSAGE_WRITEONLY flag.

Another solution is to keep all of the triangles stored in system memory, use the D3DUSAGE_WRITEONLY flag, and copy all of the triangles to the buffer every frame, which is what I would suggest doing. It's probably still faster that way than locking only one small part of memory. Plus, you may want to change the 5th and the 8th triangle at some point in the future, then you'll have to lock 2 separate times. Not good...most of the time, I mean if you had 10 triangles. Now, if you had 50k triangles and you needed to update 1 triangle...by all means only lock that one.

Good luck,
Chris

[edited by - Supernat02 on May 26, 2004 12:44:54 AM]

Share this post


Link to post
Share on other sites