Jump to content

  • Log In with Google      Sign In   
  • Create Account


My VBO world system. Any thoughts?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 ic0de   Members   -  Reputation: 832

Like
0Likes
Like

Posted 09 December 2012 - 06:41 PM

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
}

you know you program too much when you start ending sentences with semicolons;


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8049

Like
2Likes
Like

Posted 09 December 2012 - 07:06 PM

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.

#3 ic0de   Members   -  Reputation: 832

Like
0Likes
Like

Posted 09 December 2012 - 08:52 PM

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.

you know you program too much when you start ending sentences with semicolons;


#4 Kaptein   Prime Members   -  Reputation: 2061

Like
2Likes
Like

Posted 10 December 2012 - 12:22 AM

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 =)

#5 Brother Bob   Moderators   -  Reputation: 8049

Like
1Likes
Like

Posted 10 December 2012 - 05:12 AM

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.

#6 ic0de   Members   -  Reputation: 832

Like
0Likes
Like

Posted 10 December 2012 - 05:01 PM

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?

Edited by ic0de, 10 December 2012 - 05:01 PM.

you know you program too much when you start ending sentences with semicolons;


#7 Brother Bob   Moderators   -  Reputation: 8049

Like
0Likes
Like

Posted 10 December 2012 - 05:24 PM

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.

Edited by Brother Bob, 10 December 2012 - 05:26 PM.


#8 ic0de   Members   -  Reputation: 832

Like
0Likes
Like

Posted 10 December 2012 - 10:18 PM

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.

Edited by ic0de, 10 December 2012 - 10:19 PM.

you know you program too much when you start ending sentences with semicolons;


#9 L. Spiro   Crossbones+   -  Reputation: 13306

Like
0Likes
Like

Posted 10 December 2012 - 11:31 PM

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

Edited by L. Spiro, 10 December 2012 - 11:32 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#10 ic0de   Members   -  Reputation: 832

Like
0Likes
Like

Posted 11 December 2012 - 07:16 AM

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.

you know you program too much when you start ending sentences with semicolons;





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS