my program crashes out when "using multi vertex buffers"

Started by
7 comments, last by ahmetay 15 years, 10 months ago
There is no problem when rendering 'only one' vertex buffer. It is straightforward. But when I have 2 vertex buffers and rendering them, my program crashes. How can I render two or more vertex buffers sequentially? My code is here : #define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1) struct CUSTOMVERTEX {FLOAT X; FLOAT Y; FLOAT Z; DWORD COLOR; FLOAT U, V; }; // global to everywhere struct CUSTOMVERTEX vb1[4]; struct CUSTOMVERTEX vb2[8]; LPDIRECT3DVERTEXBUFFER9 vb1_buffer = NULL; LPDIRECT3DVERTEXBUFFER9 vb2_buffer = NULL; // this is the initilazition step void init_vb1(LPDIRECT3DDEVICE9 & device) { vb1[0].assign(-12,-12,49); vb1[1].assign(-24,-12,49); vb1[2].assign(-12,-12,56); vb1[3].assign(-24,-12,56); // create a vertex buffer interface device->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &vb1_buffer, NULL); VOID* pVoid; // a void pointer // lock vb1_buffer and load the vertices into it vb1_buffer->Lock(0, 0, (void**)&pVoid, 0); memcpy(pVoid, vb1, sizeof(vb1)); vb1_buffer->Unlock(); } void init_vb2(LPDIRECT3DDEVICE9 & device) { vb2[0].assign(20, 12, 83); vb2[1].assign(18.5, 12, 83); vb2[2].assign(20, -12, 83); vb2[3].assign(18.5, -12, 83); vb2[4].assign(20, 12, 93); vb2[5].assign(20, 12, 83); vb2[6].assign(20, -12, 93); vb2[7].assign(20, -12, 83); // create a vertex buffer interface device->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &vb2_buffer, NULL); VOID* pVoid; // a void pointer // lock vb2_buffer and load the vertices into it vb2_buffer->Lock(0, 0, (void**)&pVoid, 0); memcpy(pVoid, vb2, sizeof(vb2)); vb2_buffer->Unlock(); } // this is the rendering step void render(LPDIRECT3DDEVICE9 & device) { // select the vertex buffer to display device->SetStreamSource(0, vb1_buffer, 0, sizeof(CUSTOMVERTEX)); // select which vertex format we are using device->SetFVF(CUSTOMFVF); device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); // select the vertex buffer to display device->SetStreamSource(0, vb2_buffer, 0, sizeof(CUSTOMVERTEX)); // select which vertex format we are using device->SetFVF(CUSTOMFVF); device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2); } In the main.cpp file, I call the above 2 init functions and in while loop in main, I call the render function and my program crashes. I did not understand why this does not run. I searched the web but did not get any useful information.
Advertisement
Your second vertex buffer only has space for 4 of your verts.
Quote:// create a vertex buffer interface
device->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&vb2_buffer,
NULL);
You probably want that to be an 8 [smile]

Regards,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
sorry, it crashes again.

I just started using 2 vertices. Before I have been using just one vertex buffer at a time.

device->SetStreamSource(0, vb1_buffer, 0, sizeof(CUSTOMVERTEX));

device->SetStreamSource(0, vb2_buffer, 0, sizeof(CUSTOMVERTEX));

when doing this, do device's vertex buffers shift?

I am not sure about that.

I have made this ,

device->SetStreamSource(0, vb1_buffer, 0, sizeof(CUSTOMVERTEX));

device->SetStreamSource(1, vb2_buffer, 0, sizeof(CUSTOMVERTEX));

But at this time, It DOES NOT CRASH, RUNS. BUT it draws the first shape, not the second vertex buffer's data.
But I want to draw two vertex buffer's data. How?
At what line does it "crash," and what is the error message?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

The error message when "debugging" it is "Unhandled exception at 0x00403416 in Proje2.exe: 0xC0000005: Access violation reading location 0xffffffff."

I think it is a general pointer error.
(In fact I dont know where the error line is.)

But I found a way to solve this problem. In my program there was 4 vertex buffers, each containing 800 vertex, totally 3200 vertex.
I decided to bind these four vertex buffers to 'one' big vertex buffer(length is 3200 of course)

Can the last one be faster than the first one?
(No need to switch vertex buffers, but it is longer.)

Thanks.
If you are going to process all the vertices anyway, switching buffers only adds time.

The total "length" of the vertices processed is the same whether they're all in one buffer or split among several, right?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

yes, you are right. Switching between vertex buffers is an OVERHEAD. So putting all vertex buffers into one vertex buffer is a good idea.
1. You're not checking any return values. If anything like Lock() fails for any reason, you ignore it and write to an invalid pointer anyway.
2. Any output from the Debug Runtimes?
3. Are you using software or hardware vertex processing?
4. That access violation looks like you're accessing a null pointer or something similar. What line does it crash on?
Right Steve, I am not checking any return values. Yesterday in my program there was 4 vertex initialization functions. And in each function, I did not check the function's return values.

For example, I have 4 similar functions like the below.

// create a vertex buffer interface called koridor_buffer
device->CreateVertexBuffer(804*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&koridor_buffer,
NULL);

VOID* pVoid; // a void pointer

// lock koridor_buffer and load the vertices into it
koridor_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, koridor_vert, sizeof(koridor_vert));
koridor_buffer->Unlock();


I use D3DCREATE_SOFTWARE_VERTEXPROCESSING.

I thougt the reason for crash is at initializing the vertex buffers.
Because in render function, I swap vertex buffers sequentially. Maybe memory leak, or without checking the return values of functions, I would get a NULL pointer or pointer in different part of memory.

I solved this problem this morning ;) I made a vertex buffer that has length = 4*804. And while program is running, I enqueued the objects to this vertex buffer, like a queue. I did not used 4 different vbs.

Before, in render I would made these function calls.

device->SetStreamSource(0, koridor_buffer1, 0, sizeof(CUSTOMVERTEX));
device->SetStreamSource(0, koridor_buffer2, 0, sizeof(CUSTOMVERTEX));
device->SetStreamSource(0, koridor_buffer3, 0, sizeof(CUSTOMVERTEX));
device->SetStreamSource(0, koridor_buffer4, 0, sizeof(CUSTOMVERTEX));

But now I make just one function call,

device->SetStreamSource(0, koridor_buffer, 0, sizeof(CUSTOMVERTEX));

And draw these vertices sequentially.

I advice those who use multi vertex buffers, using just one vertex buffer is good at performance. Combining them is easy.

This topic is closed to new replies.

Advertisement