Sign in to follow this  

my program crashes out when "using multi vertex buffers"

This topic is 3491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this