Skip to main content
GameDev.net gamedev.net
🔒 Locked

DX9 vertex buffer stack overflow

Started by rkennedy9064 Sep 20, 2010 at 9:48 AM 7 replies 1k views
Original Post
rkennedy9064
rkennedy9064
I'm trying to use an array of points to simulate pixels on the screen and seem to run into a problem. If I use an array of 50,000 it creates the vertex buffer, but if i go much higher then that it throws a stack overflow exeption. Is there a limit as to how large a vertex buffer can get. This is my code so far.

void initGraphics(){	PixelVertex test[SCREEN_LENGTH];	for(int i = 0; i < SCREEN_LENGTH; i++)	{		PixelVertex vertex = {i % 640, i / 640, 1.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 0)};		test = vertex;	}    // create a vertex buffer interface called v_buffer    d3ddev->CreateVertexBuffer(SCREEN_LENGTH*sizeof(PixelVertex),                               0,                               CUSTOMFVF,                               D3DPOOL_MANAGED,                               &vertexBuffer,                               NULL);    VOID* pVoid;    // a void pointer    // lock v_buffer and load the vertices into it    vertexBuffer->Lock(0, 0, (void**)&pVoid, 0);    memcpy(pVoid, test, sizeof(test));    vertexBuffer->Unlock();}
Evil Steve
Evil Steve
Quote:
Original post by rkennedy9064
I'm trying to use an array of points to simulate pixels on the screen and seem to run into a problem. If I use an array of 50,000 it creates the vertex buffer, but if i go much higher then that it throws a stack overflow exeption. Is there a limit as to how large a vertex buffer can get. This is my code so far.

*** Source Snippet Removed ***
PixelVertex test[SCREEN_LENGTH];
That's an array you're declaring on the stack, which is causing your overflow. You should either declare it on the heap (With new[]), or Lock() the vertex buffer and write directly into it instead of using an intermediate buffer.
rkennedy9064
rkennedy9064
I changed the PixelVertex array to a new array and it creates it but now it doesnt seem to draw anything. Did I forget to change something else?

PixelVertex *test = new PixelVertex[SCREEN_LENGTH];	for(int i = 0; i < SCREEN_LENGTH; i++)	{		PixelVertex vertex = {i % 640, i / 640, 1.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 0)};		test = vertex;	}    // create a vertex buffer interface called v_buffer    d3ddev->CreateVertexBuffer(SCREEN_LENGTH*sizeof(PixelVertex),                               0,                               CUSTOMFVF,                               D3DPOOL_MANAGED,                               &vertexBuffer,                               NULL);    VOID* pVoid;    // a void pointer    // lock v_buffer and load the vertices into it    vertexBuffer->Lock(0, 0, (void**)&pVoid, 0);    memcpy(pVoid, test, sizeof(test));    vertexBuffer->Unlock();
Evil Steve
Evil Steve
memcpy(pVoid, test, sizeof(test));
Because test is now a pointer instead of an array, sizeof(test) will evaluate to 4 (on a 32-bit build). You want:
memcpy(pVoid, test, sizeof(PixelVertex)*SCREEN_LENGTH);

EDIT:
Also, you're not checking if Lock() SUCCEEDED() or FAILED(). If it fails, you'll end up crashing your program.
One of the several reasons I particularly dislike directxtutorial.com...
rkennedy9064
rkennedy9064
I understand, sometimes i just don't think clearly lol. Thats because test is just pointing to a memory location and that only takes up 4 bytes right?
Evil Steve
Evil Steve
Quote:
Original post by rkennedy9064
I understand, sometimes i just don't think clearly lol. Thats because test is just pointing to a memory location and that only takes up 4 bytes right?
Yes, test is a pointer to a memory location, like 0x020d3649, which only needs 4 bytes (on a 32-bit build, or 8 bytes on a 64-bit build) to hold it. Using sizeof() on an array on the stack will give the size in bytes of the entire array.
rkennedy9064
rkennedy9064
One other quick question. If I make the array global and copy the data into the vertex buffer, will i need to recopy the array into the vertex buffer every time i update points or will it change the values automatically since it is a pointer?
Evil Steve
Evil Steve
Quote:
Original post by rkennedy9064
One other quick question. If I make the array global and copy the data into the vertex buffer, will i need to recopy the array into the vertex buffer every time i update points or will it change the values automatically since it is a pointer?
When you memcpy() into the vertex buffer, you're making a copy of the data. If the copy of the data in your array changes, you'll need to re-copy it to the vertex buffer.
rkennedy9064
rkennedy9064
Ok that makes sense I just wasnt sure if it was a direct copy into the vertex buffer or if the buffer used pointers.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.