My VBO world system. Any thoughts?

Started by
8 comments, last by ic0de 11 years, 4 months ago
In my World structure there are several "chunks" that represent a group of vertices and various vertex attributes, there are three main functions in each chunk: allocate() generates VBOs, upload() sends data to the video card and draw() which obviously draws the chunk, there are also two other functions start3D() and end3D() which sandwich a group of Chunk::draw() calls. I know my structure is pretty solid and it works well. What I'm wondering about is whether my actual OpenGL calls are set up in the best way to maximize performance because gDebugger flagged a bunch of redundant state changes in my draw function and I know that's bad for performance. Anyone know how I could remove redundant state changes and maximize performance assuming I'm targeting GL 2.1

My code is below:


void start3D()
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableVertexAttribArray(attribPointer);
}

void end3D()
{
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableVertexAttribArray(attribPointer);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}



void Chunk::allocate()
{
glGenBuffers(1, &vBuffer); //positions
glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float), NULL, GL_STATIC_DRAW);
glGenBuffers(1, &tBuffer); //texcoords
glBindBuffer(GL_ARRAY_BUFFER, tBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float), NULL, GL_STATIC_DRAW);
glGenBuffers(1, &nBuffer); //normals
glBindBuffer(GL_ARRAY_BUFFER, nBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float), NULL, GL_STATIC_DRAW);
glGenBuffers(1, &tanBuffer); //tangents
glBindBuffer(GL_ARRAY_BUFFER, tanBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float), NULL, GL_STATIC_DRAW);
}



void Chunk::upload()
{
glBindBuffer(GL_ARRAY_BUFFER, tBuffer); //texcoords
glBufferData(GL_ARRAY_BUFFER, numtriangles * 6 * sizeof(float), texcoords, GL_STATIC_DRAW);
glTexCoordPointer(2, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, vBuffer); //positions
glBufferData(GL_ARRAY_BUFFER, numtriangles * 9 * sizeof(float), vertices, GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, nBuffer); //normals
glBufferData(GL_ARRAY_BUFFER, numtriangles * 9 * sizeof(float), normals, GL_STATIC_DRAW);
glNormalPointer(GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, tanBuffer); //tangents
glBufferData(GL_ARRAY_BUFFER, numtriangles * 12 * sizeof(float), tangents, GL_STATIC_DRAW);
glVertexAttribPointer(attribPointer, 4, GL_FLOAT, false, 0, 0);
}



void Chunk::draw()
{
glActiveTexture(GL_TEXTURE0);
glUniform1i(texture_location, 0);
glBindTexture(GL_TEXTURE_2D, bitmap);
glActiveTexture(GL_TEXTURE1);
glUniform1i(normal_texture_location, 1);
glBindTexture(GL_TEXTURE_2D, normap);
glActiveTexture(GL_TEXTURE2);
glUniform1i(height_texture_loc, 2);
glBindTexture(GL_TEXTURE_2D, heightmap);


glBindBuffer(GL_ARRAY_BUFFER, tBuffer); //texcoords
glTexCoordPointer(2, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, vBuffer); //positions
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, nBuffer); //normals
glNormalPointer(GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, tanBuffer); //tangents
glVertexAttribPointer(attribPointer, 4, GL_FLOAT, false, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, numtriangles*3);
}


Sample Usage:


chunk1.allocate();
chunk2.allocate();
chunk3.allocate();

start3D();
chunk1.upload();
chunk2.upload();
chunk3.upload();
end3D();

while(running)
{
start3D();
chunk1.draw();
chunk2.draw();
chunk3.draw();
end3D();

//do other stuff
}
Advertisement
Why do you have allocate and upload as two separate functions if they are supposed to be called together? Are there times where you expect to allocate the buffers but not upload any data?

What is the purpose of allocating empty buffers the size of a single float in the allocate function? If you actually indent to pre-allocate the whole buffer, you should allocate a buffer large enough to store the actual data you put into it later and then use glBufferSubData instead to upload the content to an existing buffer; glBufferData will discard the old buffer and create a new one and your pre-allocated buffer was of no use.

You set the vertex attribute pointers in both the upload and the draw functions. Setting the pointers in the upload function serves no purpose since you're not using the binding points anywhere except in the draw function, and the draw function already set the attribute pointers.

You don't have to enable the buffers to upload data to them. Your calls to start3D and stop3D around the upload calls have no purpose.

I find your setup very limiting because you have generic functions to enable vertex attribute arrays, but they force a specific vertex array layout: any vertex array you draw must have a position, a normal, a texture coordinate and a generic attribute array; no more, no less. Either you intend to use that function for the Chunk class only, in which case it is named way too generically named, or you should use a more flexible vertex array system that doesn't force a specific vertex attribute set, but rather enables what is in use. If you intend to only use the functions for the Chunk class, then put the two helper functions in the chunk class to show that they are for the Chunk class specifically and nothing else.

The redundant state changes I can see are, except for setting the attribute pointers in the upload function, could be the result of the very limited test bed you have in the while loop. The while loop calls end3D and then calls start3D again in the next iteration. Unless "// do other stuff" actually depends on end3D being called, the observable state changes in OpenGL could be that you disable and then re-enable some states immediately after each other.
the bit at the end is just a sample usage not necessarily the best one. And as for start3D and end3D being restrictive I found that in my game it resulted in less opengl calls if I enabled everything right off the bat and then disabled the ones that I did in the rare case that I didn't need all of them not need for example I have a drawSkybox function which disables tangents and normals. As for my draw function do you propose that It looks like this, removing the pointers:

void Chunk::draw()
{
glActiveTexture(GL_TEXTURE0);
glUniform1i(texture_location, 0);
glBindTexture(GL_TEXTURE_2D, bitmap);
glActiveTexture(GL_TEXTURE1);
glUniform1i(normal_texture_location, 1);
glBindTexture(GL_TEXTURE_2D, normap);
glActiveTexture(GL_TEXTURE2);
glUniform1i(height_texture_loc, 2);
glBindTexture(GL_TEXTURE_2D, heightmap);

glBindBuffer(GL_ARRAY_BUFFER, tBuffer); //texcoords
glBindBuffer(GL_ARRAY_BUFFER, vBuffer); //positions
glBindBuffer(GL_ARRAY_BUFFER, nBuffer); //normals
glBindBuffer(GL_ARRAY_BUFFER, tanBuffer); //tangents
glDrawArrays(GL_TRIANGLES, 0, numtriangles*3);
}


because I tried that and it broke the function.

BTW this code is just for the internals of my game not for a library or anything I don't really care if it makes sense but rather that it's as fast as possible.
that draw function isn't going to work well, because you are binding 4 VBOs in a row, which means that only the last one is bound before drawing
you are drawing tangents :)
you should have kept the "pointers" to the data, and even better if you want fast rendering - interleave the data in a single VBO using a struct
http://www.opengl.org/wiki/Vertex_Specification_Best_Practices

unfortunately i have to cut my reply short, i have an exam to go to =)

the bit at the end is just a sample usage not necessarily the best one. And as for start3D and end3D being restrictive I found that in my game it resulted in less opengl calls if I enabled everything right off the bat and then disabled the ones that I did in the rare case that I didn't need all of them not need for example I have a drawSkybox function which disables tangents and normals.

It may work now, but it will definitely bite you later on because it's going to be a nightmare to maintain and get right, and especially get optimal as you aim for. You may very well get to the point where the disabling-and-enabling-when-done approach will cost you just as much as enabling-and-disabling-when-done approach. What you effectively end up with is a global enable state, and then local disable-on-demand calls.

You're aiming for a global optimum, so you're gonna need a global solution. For example, some state manager that doesn't re-enable things that are enabled already, or sets pointers that are already set. You can only achieve local optimum with local calls. For example, consider that you draw your sky box that, as you say, doesn't have tangents and normals, so you disable tangents and normals before drawing it, and then re-enable it again. That may be the optimum way to handle it since you only need to disable a few states.

Then you want to draw a small GUI or something that doesn't have tangents and normals either. So you do the same thing again; you disable them, draw the GUI and enable them before exiting; again, locally the optimum way to do it. But the net effect are four redundant calls; two do enabble two states and then two more to disable them again. Local optimum, but not global optimum.


As for my draw function do you propose that It looks like this, removing the pointers:

void Chunk::draw()
{
glActiveTexture(GL_TEXTURE0);
glUniform1i(texture_location, 0);
glBindTexture(GL_TEXTURE_2D, bitmap);
glActiveTexture(GL_TEXTURE1);
glUniform1i(normal_texture_location, 1);
glBindTexture(GL_TEXTURE_2D, normap);
glActiveTexture(GL_TEXTURE2);
glUniform1i(height_texture_loc, 2);
glBindTexture(GL_TEXTURE_2D, heightmap);

glBindBuffer(GL_ARRAY_BUFFER, tBuffer); //texcoords
glBindBuffer(GL_ARRAY_BUFFER, vBuffer); //positions
glBindBuffer(GL_ARRAY_BUFFER, nBuffer); //normals
glBindBuffer(GL_ARRAY_BUFFER, tanBuffer); //tangents
glDrawArrays(GL_TRIANGLES, 0, numtriangles*3);
}


because I tried that and it broke the function.

Kaptein already addressed it, but I said that the pointers in the update function, not the draw function, served no purpose.


BTW this code is just for the internals of my game not for a library or anything I don't really care if it makes sense but rather that it's as fast as possible.

When your code base grows, you will forget about the details about an increasing amount of your very own code, and it will become unfamiliar to you. Design is not only for other's sake, but for your own as well.
So I put all my arrays into one and here is the resulting code:


void Chunk::upload()
{
glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
glBufferData(GL_ARRAY_BUFFER, totalsize * sizeof(float), vertices, GL_STATIC_DRAW);
}



void Chunk::draw()
{
glActiveTexture(GL_TEXTURE0);
glUniform1i(texture_location, 0);
glBindTexture(GL_TEXTURE_2D, bitmap);
glActiveTexture(GL_TEXTURE1);
glUniform1i(normal_texture_location, 1);
glBindTexture(GL_TEXTURE_2D, normap);
glActiveTexture(GL_TEXTURE2);
glUniform1i(height_texture_loc, 2);
glBindTexture(GL_TEXTURE_2D, heightmap);

glBindBuffer(GL_ARRAY_BUFFER, vBuffer);

glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(v_start * sizeof(float)));
glTexCoordPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(t_start * sizeof(float)));
glNormalPointer(GL_FLOAT, 0, BUFFER_OFFSET(n_start * sizeof(float)));
glVertexAttribPointer(attribPointer, 4, GL_FLOAT, false, 0, BUFFER_OFFSET(tan_start * sizeof(float)));
glDrawArrays(GL_TRIANGLES, 0, numtriangles*3);
}


I works fine but the problem is that I thought I could improve performance by moving the part where I set my pointers into the upload function, however for some reason It doesn't work unless I set the pointers every time I draw. Any way I can avoid setting the pointers every draw call considering they never change?
You can use vertex array objects then. They encapsulate binding points into a single object. The problem with the pointers is that they are global (unless you're using VAO that is, in which case the are local to the VAO), so setting them in the upload function won't do any good as soon as you upload a second chunk; the pointers for the first chunk are overwritten. And not to mention that you cannot have anything other vertex array overwriting the pointers either.

Using VAO does not change the fact that you have to bind something for every single chunk every frame. You just get rid of the calls to the pointer-functions, but replace them with a bind-call instead.
Using VAO's now but for some reason it isn't working here's my code:


void Chunk::allocate()
{
if(GLEW_ARB_vertex_array_object)
{
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
}
glGenBuffers(1, &vBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float), NULL, GL_STATIC_DRAW);
if(GLEW_ARB_vertex_array_object)
{
glBindVertexArray(0);
}
}



void Chunk::upload()
{
if(GLEW_ARB_vertex_array_object)
{
glBindVertexArray(vao);
}
glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
glBufferData(GL_ARRAY_BUFFER, totalsize * sizeof(float), vertices, GL_STATIC_DRAW);
if(GLEW_ARB_vertex_array_object)
{
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, false, 0, BUFFER_OFFSET(v_start * sizeof(float)));
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, false, 0, BUFFER_OFFSET(t_start * sizeof(float)));
glVertexAttribPointer(normAttrib, 3, GL_FLOAT, true, 0, BUFFER_OFFSET(n_start * sizeof(float)));
glVertexAttribPointer(tanAttrib, 4, GL_FLOAT, false, 0, BUFFER_OFFSET(tan_start * sizeof(float)));
glBindVertexArray(0);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
}



void Chunk::draw()
{
glActiveTexture(GL_TEXTURE0);
glUniform1i(texture_location, 0);
glBindTexture(GL_TEXTURE_2D, bitmap);
glActiveTexture(GL_TEXTURE1);
glUniform1i(normal_texture_location, 1);
glBindTexture(GL_TEXTURE_2D, normap);
glActiveTexture(GL_TEXTURE2);
glUniform1i(height_texture_loc, 2);
glBindTexture(GL_TEXTURE_2D, heightmap);

if(GLEW_ARB_vertex_array_object)
{
glBindVertexArray(vao);
}
else
{
glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, false, 0, BUFFER_OFFSET(v_start * sizeof(float)));
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, false, 0, BUFFER_OFFSET(t_start * sizeof(float)));
glVertexAttribPointer(normAttrib, 3, GL_FLOAT, true, 0, BUFFER_OFFSET(n_start * sizeof(float)));
glVertexAttribPointer(tanAttrib, 4, GL_FLOAT, false, 0, BUFFER_OFFSET(tan_start * sizeof(float)));
}
glDrawArrays(GL_TRIANGLES, 0, numtriangles*3);
if(GLEW_ARB_vertex_array_object)
{
glBindVertexArray(0);
}
}


I have it set up so that computers that don't support the VAO extension can still render properly seeing as my target is GL 2.1. Now everything works as expected when I force the check to fail but when the check passes is when it doesn't work.

that draw function isn't going to work well, because you are binding 4 VBOs in a row, which means that only the last one is bound before drawing
you are drawing tangents

That is not correct. The calls to glVertexPointer() and friends work on whatever vertex buffer was active at the time of the call. After the call he is free to bind a different vertex buffer and call, for example, glNormalPointer().

However his code will leave trash bound when other objects are drawn that don’t have all of the same attributes because they are never unbound.


ic0de, you need to call glActiveTexture() before binding textures to different slots (calling glBindTexture()).
Additionally you seem to have switched to shaders but I don’t see any shaders ever being set. This has to be done before setting uniforms etc.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


Additionally you seem to have switched to shaders but I don’t see any shaders ever being set. This has to be done before setting uniforms etc.


There's allot of work being done outside the scope of this code that I posted shading is one of them, I was mainly concerned about my VBO's and that's why I didn't post the other code.

This topic is closed to new replies.

Advertisement