CreateVertexBuffer() memory allocation

Started by
6 comments, last by quant 20 years, 5 months ago
When i create and lock a vertex buffer, how would i assign values to the memory allocated? Are the vertex objects already contructed? or would i have to use placement new to construct the objects. For example

pdevice->CreateVertexBuffer(8*sizeof(vertextype), D3DUSAGE_SOFTWAREPROCESSING, CUSTOM_VERTEX, D3DPOOL_SYSTEMMEM, &pbuf);

pbuf->Lock(0, sizeof(vertextype), &pmem, 0);

for( int j = 0; j < 8; ++j )
pmem[j].x = 1;

   
is that valid? or would i have to use placement new

vertextype* pvert = new(pmem) vertextype;
pvert->x = 1;
    
[edited by - quant on November 15, 2003 10:14:57 AM]
-keyboard
Advertisement
Creating the vertex buffer only allocates memory. Usually, you''ll just want to keep your vertex structures/classes simple enough that it doesn''t matter whether the constructor''s been called or not (so you could just use the first method and overwrite what was in the memory before). Remember, the destructors for the vertices will never be called, so you''ll have to keep things pretty simple anyways. The placement new will probably work, though, if that''s what you want to use.
What you''d normally do (well, I would anyway) is one of two things. Either:

1) Have an array of vertex data set up already and just memcpy the data into the buffer after it''s been locked, e.g.:

pdevice->CreateVertexBuffer(8*sizeof(vertextype), D3DUSAGE_SOFTWAREPROCESSING, CUSTOM_VERTEX, D3DPOOL_SYSTEMMEM, &pbuf);
pbuf->Lock(0, sizeof(vertextype), &pmem, 0);
memcpy(pmem, vertArray, 8 * sizeof(vertextype);

OR

2) Cast your pmem pointer e.g.:

vertextype * pmem;
pdevice->CreateVertexBuffer(8*sizeof(vertextype), D3DUSAGE_SOFTWAREPROCESSING, CUSTOM_VERTEX, D3DPOOL_SYSTEMMEM, &pbuf);
pbuf->Lock(0, sizeof(vertextype), (void**)&pmem, 0);
for(int j = 0; j < 8; j++)
{
pmem[j].x = 1;
etc....
}

---------------------------------------

Let''s struggle for our dream of Game!

http://andrewporritt.4t.com
You don''t have to use new. The CreateVertexBuffer function returns to you the address of some allocated memory, so you should use that memory. Then you just go through your vertex structure and fill in the data. I''m not sure why you would use new in this situation...

Thanks for the replys.

quote:
Original post by hoopjones
I'm not sure why you would use new in this situation...


C++ normally deals with objects that have been constructed using a constructor, and the only way to construct an object in a particular area of memory is by using placement new.

But i guess that because the vertex structure only contains basic data types, a constructor isnt needed.



[edited by - quant on November 15, 2003 3:26:03 PM]
-keyboard
Yes, but new is giving you memory from the heap, not the AGP accessible memory that CreateVertexBuffer gives you, which is why you would want to use CreateVertexBuffer in the first place.
quote:Original post by hoopjones
Yes, but new is giving you memory from the heap, not the AGP accessible memory that CreateVertexBuffer gives you, which is why you would want to use CreateVertexBuffer in the first place.



Placement new doesnt give you memory anywhere, it simply constructs an object in the memory location you pass to it.
quote:Original post by Anonymous Poster
quote:Original post by hoopjones
Yes, but new is giving you memory from the heap, not the AGP accessible memory that CreateVertexBuffer gives you, which is why you would want to use CreateVertexBuffer in the first place.



Placement new doesnt give you memory anywhere, it simply constructs an object in the memory location you pass to it.


I never knew about "placement new", thanks for mentioning it!

This topic is closed to new replies.

Advertisement