Opengl Vbo Errors

Started by
2 comments, last by Marz Tierney 11 years, 4 months ago
Hey, I'm working on a minecraft-like voxel engine. I keep my block type information in chunks, before creating vectors, and then sending that information off into the GPU as a vbo. This was working fine, but after generating too many different vbo objects (well the pointer that you bind, with the buffer), the game kept crashing. I ignored this for a while, but i've fixed all my other bugs on my list, and this one needs to be addressed.
In order to solve it, I tried running the game without drawing the VBOs, and it didn't crash after alot of time. So from this, I figured having too many vbos was the problem, then I read various things, and people seemed to agree on reducing the calls to bindBuffer, or shoving all your objects(Th into one vbo (ideally) before sending it off. The render information is static, so this shouldn't be too difficult.
In order to solve it, I used indexes, and created just one vboPointer, then for every chunk I had, I just generated it in one of the indexes at a time. There can be anywhere from 30 to 1000 chunks loaded depending on the renderDistance.
Also deleting the buffers seemed to cause it to crash too. So after some research, I figured this was because it releases the buffer position for any future use. I think I tried generating it again, and had no luck.
I would like some clarity on vbo useage, do vbo indexes work for large numbers? Has uanyone had the bg where multiple vbo objects don't work? What should be the best approach for this? Minecraft uses display lists, but don't vbos give a performance boost?
Also, I heard the calls to bindBuffer will take a fair amount of processing, so is it better to bind these on a thread to completely reduce loading times?
One possible solution is too use not one vbo per chunk (16x16x16 blocks), or one for all of them, but one per X-Z position or something like that. But it's very hard to debug vbo errors when I can't understand the problem. Feels like i'm just trying anything, not making any progress on this.

class VboData {
int[] quadLength; // Sizes of Arrays
int vboIndex = -1;
int maxIndex = 0; // Stores the total number of vbos in it
int[] vboQuadID;
int[] vboColorID;
int[] vboNormalID;
int[] vboTexID;
boolean hasGenVbo = false;
VboData() {
//maxIndexTotal = maxChunks;
quadLength = new int[maxIndexTotal];
}
VboData(int maxIndex_) {
maxIndex = maxIndex_;
//maxIndexTotal = maxChunks;
quadLength = new int[maxIndexTotal];
}
void dumpMemory() {

}
int getQuadLength(int index) {
if (maxIndexTotal > -1)
return quadLength[index];
else return 0;
}
void genAllVBO() {
if (!hasGenVbo) {
println("Total: " + maxIndexTotal);
vboQuadID = new int[maxIndexTotal];
vboColorID = new int[maxIndexTotal];
vboNormalID = new int[maxIndexTotal];
vboTexID = new int[maxIndexTotal];
gl.glGenBuffers(maxIndexTotal, vboQuadID, 0 );
gl.glGenBuffers(maxIndexTotal, vboNormalID, 0);
gl.glGenBuffers(maxIndexTotal, vboTexID, 0);
gl.glGenBuffers(maxIndexTotal, vboColorID, 0 );
hasGenVbo = true;
}
}
void clearMemory(int vboIndex_) {
/*if (quadLength[vboIndex_] > 0) {
gl.glDeleteBuffers(1, vboQuadID, 0 );
gl.glDeleteBuffers(1, vboNormalID, 0);
gl.glDeleteBuffers(1, vboTexID, 0);
gl.glDeleteBuffers(1, vboColorID, 0 );
}*/
}
void genAllVBOs(Vector vboVertex, Vector vboColor, Vector vboNormal, Vector vboTexture, int vboIndex_) {
if (vboIndex_ < maxIndexTotal)
vboIndex = vboIndex_;
quadLength[vboIndex_] = vboVertex.size();
genAllVBO();
genVBO(vboVertex, vboNormal, vboTexture, vboColor);
}
void genVBO(Vector vboVertex, Vector vboNormal, Vector vboTexture, Vector vboColor) {
int quadLength2 = vboVertex.size();
if (quadLength2 > 0 && vboIndex > -1) {
int normalLength = vboNormal.size();
int texLength = vboTexture.size();
int colorLength = vboColor.size();

gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vboQuadID[vboIndex] );
gl.glBufferData( GL.GL_ARRAY_BUFFER, quadLength2 * 3 * BufferUtil.SIZEOF_FLOAT, myVertex_to_float_buffer( vboVertex ), GL.GL_DYNAMIC_DRAW );
gl.glBindBuffer( GL.GL_ARRAY_BUFFER, 0); // This resets the gl pointer to the start
gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vboNormalID[vboIndex] );
gl.glBufferData( GL.GL_ARRAY_BUFFER, normalLength * 3 * BufferUtil.SIZEOF_FLOAT, myNormal_to_float_buffer( vboNormal ), GL.GL_DYNAMIC_DRAW );
gl.glBindBuffer( GL.GL_ARRAY_BUFFER, 0);
gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vboTexID[vboIndex] );
gl.glBufferData( GL.GL_ARRAY_BUFFER, texLength * 2 * BufferUtil.SIZEOF_FLOAT, myTexture_to_float_buffer( vboTexture ), GL.GL_DYNAMIC_DRAW );
gl.glBindBuffer( GL.GL_ARRAY_BUFFER, 0);

gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vboColorID[vboIndex] );
gl.glBufferData( GL.GL_ARRAY_BUFFER, colorLength * 4 * BufferUtil.SIZEOF_FLOAT, myColor_to_float_buffer( vboColor ), GL.GL_DYNAMIC_DRAW);
gl.glBindBuffer( GL.GL_ARRAY_BUFFER, 0);
}
}
}
class RenderData {
int vboIndex = 0; // It's position in the vbo array
Vector vboVertex;
Vector vboColor;
Vector vboNormal;
Vector vboTexture;
RenderData() {
vboVertex = new Vector();
vboColor = new Vector();
vboNormal = new Vector();
vboTexture = new Vector();
}
void setMax() {
//if (vboVertex.size() != 0)
//quadLength = vboVertex.size();
}
void setIndex(int index) {
if (index < maxIndexTotal && index > -1)
vboIndex = index;
}

void clearMemory() {
clearVectors();
/*
vboVertex = null;
vboNormal = null;
vboTexture = null;
vboColor = null;*/
}
void clearVectors() {
vboVertex = new Vector();
vboNormal = new Vector();
vboTexture = new Vector();
vboColor = new Vector();
}
}
void renderVBO(VboData vbo,Texture tex, int quadLength, int vboIndex) {
//if (vbo.quadLength > 0) {
//println(render.quadLength);
if (isRenderVBO && quadLength > 0 && vboIndex > -1) {
tex.enable();
tex.bind();
gl.glPushMatrix();

if (isShader) glsl.startShader();
// Enables opengl to draw vertex points from memory
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL.GL_NORMAL_ARRAY);
gl.glEnableClientState(GL.GL_COLOR_ARRAY);
gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);

// Points to vertexes in memory
gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vbo.vboTexID[vboIndex]);
gl.glTexCoordPointer(2,GL.GL_FLOAT,0,0);
gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vbo.vboColorID[vboIndex]);
gl.glColorPointer(4,GL.GL_FLOAT,0,0);
gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vbo.vboNormalID[vboIndex]);
gl.glNormalPointer(GL.GL_FLOAT,0,0);
gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vbo.vboQuadID[vboIndex]);
gl.glVertexPointer(3,GL.GL_FLOAT,0,0);
// Draws the vertexes from memory - hence why needed point
gl.glDrawArrays(GL.GL_QUADS, 0, quadLength);
// Resets the binded buffer
gl.glBindBuffer( GL.GL_ARRAY_BUFFER, 0);
// Ends the opengl drawing

// Disables the client state
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL.GL_NORMAL_ARRAY);
gl.glDisableClientState(GL.GL_COLOR_ARRAY);
gl.glDisableClientState(GL.GL_TEXTURE_COORD_ARRAY);

if (isShader) glsl.endShader();
gl.glPopMatrix();
tex.disable();
}
}
Advertisement
you may get better FPS by not using indices, or by super-optimizing indices which may be a waste of time
deleting VBOs is completely safe, and completely necessary to keep from using too much vram
consider using columns of the entire world as a final VBO, then you will have let's say 32x32 VBOs, and you need only bind a few of them for each render
in minecraft 32x32 sectors x 16x16 blocks is the size of the world on "far render distance" setting
remember that the memory requirement for the game increases exponentially with each ring of sectors
you also need to parallellize the pipeline that assembles the world into vbo data
but you can only call gl* functions on the render threadi
that means that it's pointless to try to parallellize anything in the renderer, and instead focusing on optimizing hard on what the renderer has to do
and lift some weight off of it
ideally, the rendering thread should only do rendering, and nothing else =)
you can only call gl* functions from the same thread as the opengl context was created on, otherwise you are in trouble

you can use occlusion query objects to find out what isnt visible 1 frame from when you render them
this will let you draw alot less 1 frame after drawing everything in the frustum
Ahh thank you for your advice! It's very spefic to what I needed. Before reading this I already took the initiative to put the rendering just on one single thread, this seemed to improve it alot, ie it crashes less.
What do you mean by super-optimizing indices?
I put all my chunks into one single vbo, using indexes to the pointer array. Is it necessary to use sectors of vbos rather then just one?
Yeah I read that its no use as opengl doesnt work well with threading 'that means that it's pointless to try to parallellize anything in the renderer, and instead focusing on optimizing hard on what the renderer has to do and lift some weight off of it'!
I'm curious as too the occlusion, but that would require alot of updates to the vbos, is this what minecraft does? How it says chunk updates in its debug gui? Is it because it uses cpu useage and causes opengl to render less things, thus improving fps... It's just the calls of glBindBuffer are very expensive for time, so implementing these things and getting better fps has been a largely trial and error thing for me.
YESS!!! I finished redoing most of my code, and putting only rendering in the main thread. I had a little bit of extra stuff in the rendering thread left, and after removing it it works the same as many other voxel like games i've been trying to make my game as smooth as. Thank you!

This topic is closed to new replies.

Advertisement