question about the IDirect3DVertexBuffer9::Lock(...)method

Started by
7 comments, last by digitalfreak 20 years, 1 month ago
if I create a vertex buffer with

IDirect3DVertexBuffer9* _vb;
 
then use

_vb->Lock(0, 0, (void**)&v, 0);
 
where v is a pointer to a user defined vertex type. and then is the code to fill in v with vertices data. I don''t understand the relationship between _vb and v. to be precise, why don''t we fill in the vertices data directly to _vb instead of using another piece of memory created at v? we''ve alread set aside memory when creating _vb with the IDirect3DDevice9::CreateVertexBuffer method, didn''t we?
Advertisement
I''m not sure, but my guess is that you can''t make the CPU write to video memory that easily. Locking, copying to system memory, and then having DX copy that to video memory probably saves you from doing a lot of extra coding to get it into video memory directly.
Look at it this way: a vertex buffer in D3D isn''t just an array of vertices. It is essentially an extended COM object, and as such, it has a whole bunch of member variables and internal data that it keeps track of. That data is used by D3D internally when you want to render your geometry.

Think of the Lock call as returning a pointer to the chunk of memory inside the D3D Vertex Buffer where all the vertices will actually be stored. With this pointer, you can store specific vertices inside the Vertex Buffer object (which is usually in video memory).

Although you don''t *really* have a pointer to video memory, because video memory cannot be addressed directly by the CPU, the driver takes care of all this and to the programmer, it seems as though they''re writing directly to video memory.

neneboricua
Hi, I''ve been thinking about almost the same thing, the &v would have been initialised as a void pointer;
VOID *v; //

my question is what does (void++)&v mean, (void**) is a cast, so you are casting the address of v,- &v to a pointer to a pointer, I don''t want to try and get thinhgs confused but that''s is what it seems to me.

Using dx8 it look like this,

VOID* pVertices;
g_pVB->Lock( 0, sizeof(g_Vertices), (BYTE**)&pVertices, 0 )
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
g_pVB->Unlock();

I''m really trying to get at what happens under the surface, because this is used alot in dx, xof files use it as well to access all the data objects ect.

Thanks.
quote:Original post by Stevieboy
...what does (void++)&v mean, (void**) is a cast, so you are casting the address of v,- &v to a pointer to a pointer,


Pretty much, a pointer to a pointer to untyped data (void) to be a bit more specific.

The thing is what is the data in a vertex buffer? It is a list of vertices right, but your vertices are not necessarily of any particular format. They might just be a position (3 floats) they might be a position and a texture coordinate (3+2 floats). Positions, normals, up to 8 texture coordinates, colors, blah, blah ,blah: they have a wide range of formats and sizes. There are several possible ways to deal with this. DirectX usually uses what I think is the C way of doing it. They store the type (FVF) the size in bytes (which given the fvf gives you vertices as well) and store the data as an untyped block of memory (void). Then you have to somehow figure out what the type is before you can use the data (maybe it has a member that describes the fvf? can't remember).

Edit: yea it does have an fvf member.


[edited by - JeffF on March 11, 2004 1:19:17 PM]
Hi, I've been thinking about almost the same thing, the &v would have been initialised as a void pointer;
VOID *v; //

my question is what does (void**)&v mean, (void**) is a cast, so you are casting &v to a pointer to a pointer, I don't want to try and get thinhgs confused but that's is what it seems to me.

Using dx8 it look like this,

VOID* pVertices;
g_pVB->Lock( 0, sizeof(g_Vertices), (BYTE**)&pVertices, 0 )
memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
g_pVB->Unlock();

I'm really trying to get at what happens under the surface, because this is used alot in dx, xof files use it as well to access all the data objects ect.
Thanks.



sorry about posting twice my browser closed down just as I posted the first time, sorry




sorry about posing twice, my

[edited by - Stevieboy on March 11, 2004 1:22:26 PM]
quote:Original post by Stevieboy
my question is what does (void**)&v mean, (void**) is a cast, so you are casting &v to a pointer to a pointer, I don't want to try and get thinhgs confused but that's is what it seems to me.


Yes you are casting &v to a pointer to a pointer (to untyped data).
So clearly v should also be a pointer (usually to the data type you want and know it to be, your vertex type).

Now if you happened to have defined v as void* &v is already a void**, so the cast is redundant.

Typically you want v to be a more usefull type, like your vertex type so you can do the cast just once when getting the data, rather than each time you try to use it.

so it would be:
MyVertex* pvbdata; // somehow I know that _vb is of type MyVertex. Maybe I checked the vertex buffer desc structure, maybe some other way
_vb->Lock(0, 0, (void**)&pvbdata, 0);



Edit: heh ok, well I answered twice already

[edited by - JeffF on March 11, 2004 1:36:44 PM]
Jeff thanks for your reply, it gets a bit hard
The vertexbuffer hold a chunk of memory, but hides it from you where it actually is. When yuo lock it, especially if you lock it with D3DLOCK_DISCARD it will give you a pointer where you can work with the data. But this can be another location than the last time you used it. You don''t know what it will do, it might transfer the data to the video card, or keep a copy in local memory. When you use D3DLOCK_DISCARD, it will give you a new piece of memory you can fill while holding on to the old data to render while you work with it, then when you unlock it it will discard the old data and work with the new data.

This topic is closed to new replies.

Advertisement