Index buffer help

Started by
4 comments, last by kauna 10 years, 6 months ago

Hi All

I am having a little trouble with my index buffer and I'm not really sure why

I currently have declared 2 buffer lock holders in a class header file, one for my vertices and one for my indices:


vertex *vertices;
short *indices;

And they are set to NULL in my class constructor


vertices = NULL;
indices = NULL;

But what baffles me is when I write to my vertex buffer lock using:


vertices[currentVertexBufferPosition] = (*i).verts[0];
vertices[currentVertexBufferPosition + 1] = (*i).verts[1];
vertices[currentVertexBufferPosition + 2] = (*i).verts[2];
vertices[currentVertexBufferPosition + 3] = (*i).verts[3];

Everything works and I can get all my expected values back using


//12 because we can hold at most 12 vertices (Max buffer size of 3 quads for testing)
for(int i = 0; i < 12; i++)
	{
		std::cout<<"I["<<i<<"]: X "<<vertices[i].x<<std::endl;
	}

BUT using the same method for my index buffer lock


indices[currentIndexBufferPosition] = currentVertexBufferPosition;
indices[currentIndexBufferPosition + 1] = currentVertexBufferPosition + 1;
indices[currentIndexBufferPosition + 2] = currentVertexBufferPosition + 2;
indices[currentIndexBufferPosition + 3] = currentVertexBufferPosition + 3;
indices[currentIndexBufferPosition + 4] = currentVertexBufferPosition;
indices[currentIndexBufferPosition + 5] = currentVertexBufferPosition + 2;

And same method to get my data


//18 because we can hold at most 18 vertices (Max buffer size of 3 quads for testing)
for(int i = 0; i < 18; i++)
	{
		std::cout<<"I["<<i<<"]: Indices = "<<indices[i]<<std::endl;
	}

I always get 0 back

when I step through, it seems like all of my indices are getting the value of 0.

Like the program is only accepting the first value assigned as the value to use all the time.

I know it is not a position issue as I always increase there values at the end of my run


currentIndexBufferPosition += 6;
currentVertexBufferPosition += 4;

Also just in case this is how I am locking, where the bufferLockFlag is set to NOOVERWRITE by default


vBuffer->Lock(0, 0, (void**) &vertices, bufferLockFlag);
iBuffer->Lock(0, 0, (void**) &indices, bufferLockFlag);

/* Code for drawing all loading the vertices / indices in the buffers. If the vertex buffer is full, unlock the buffers and change
the lock flag to DISCARD. Lock the buffers again and change the lokc flag back to NOOVERWRITE*/

vBuffer->Unlock();
iBuffer->Unlock();

Anyone have any ideas why this is happening?

Advertisement

According to your code, you are reading from the vertex and index buffer. Have you specified READ flag in the creation? The memory used for those buffers may not be readable by default.

Otherwise, reading data from GPU resources is typically not a good idea.

Cheers!

According to your code, you are reading from the vertex and index buffer. Have you specified READ flag in the creation? The memory used for those buffers may not be readable by default.

Otherwise, reading data from GPU resources is typically not a good idea.

Cheers!


The only 2 flags I have when I create my buffers are the D3DUSAGE_WRITEONLY and D3DUSAGE_DYNAMIC flags

My buffers are created like so


batDevice->CreateVertexBuffer(vertexBufferSize, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, CUSTOMFVF2, D3DPOOL_DEFAULT, &vBuffer, NULL);

batDevice->CreateIndexBuffer(indexBufferSize, D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &iBuffer, NULL);

But the thing I don't understand is that even if I step through my code it says my buffer is only storing 0 or whatever the first value was set. I don't think it would matter if I have the read flag and I'm stepping through my code. Right?


But the thing I don't understand is that even if I step through my code it says my buffer is only storing 0 or whatever the first value was set. I don't think it would matter if I have the read flag and I'm stepping through my code. Right?

Oh yes it matters, having set D3DUSAGE_WRITEONLY especially ensures that card that you never ever will attempt to read form the buffer, and therefore have it store that data so that it won't ever need to be read back to RAM. That way, when locking the buffer, that handle will contain any data other than what you filled it in previously. The memory of the GPU is seperated from the system RAM, so even if you step through in the debugger, you'll get the same results.


Oh yes it matters, having set D3DUSAGE_WRITEONLY especially ensures that card that you never ever will attempt to read form the buffer, and therefore have it store that data so that it won't ever need to be read back to RAM. That way, when locking the buffer, that handle will contain any data other than what you filled it in previously. The memory of the GPU is seperated from the system RAM, so even if you step through in the debugger, you'll get the same results.


I find this interesting and confusing.

Its interest that saying

indices[currentIndexBufferPosition] = currentVertexBufferPosition;
indices[currentIndexBufferPosition + 1] = currentVertexBufferPosition + 1;
indices[currentIndexBufferPosition + 2] = currentVertexBufferPosition + 2;
indices[currentIndexBufferPosition + 3] = currentVertexBufferPosition + 3;
indices[currentIndexBufferPosition + 4] = currentVertexBufferPosition;
indices[currentIndexBufferPosition + 5] = currentVertexBufferPosition + 2;
Is only a link to the GPU and that it is the only thing that knows what is going on

I find it confusing that using the exact same setup code for my vertex buffer, method of filling, and reading works just fine.

Does this mean that its safe to assume I'm not actually having all zeros in my index buffer?

You should use PIX or some other software the verify the contents of your buffers. That is an easy and good way to handle the situation.

If your draw code works fine ie. the output is what you are expecting, then your buffers contain the correct information.

Cheers!

This topic is closed to new replies.

Advertisement