Sign in to follow this  
Marz Tierney

OpenGL Opengl Vbo Errors

Recommended Posts

Marz Tierney    125
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,[b] do vbo indexes work for large numbers[/b]? Has uanyone had the bg where multiple vbo objects don't work? What should be the best approach for this? Minecraft [b]uses display lists, but don't vbos give a performance boost[/b]?
Also, I heard the calls to bindBuffer will take a fair amount of processing, so [b]is it better to bind these on a thread to completely reduce loading times[/b]?
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();
}
[b]void setIndex(int index) {
if (index < maxIndexTotal && index > -1)
vboIndex = index;
}[/b]
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
[b] gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vbo.vboTexID[vboIndex]);[/b]
gl.glTexCoordPointer(2,GL.GL_FLOAT,0,0);
[b] gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vbo.vboColorID[vboIndex]);[/b]
gl.glColorPointer(4,GL.GL_FLOAT,0,0);
[b] gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vbo.vboNormalID[vboIndex]);[/b]
gl.glNormalPointer(GL.GL_FLOAT,0,0);
[b] gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vbo.vboQuadID[vboIndex]);[/b]
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();
}
}

Share this post


Link to post
Share on other sites
Kaptein    2224
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
[b]but you can only call gl* functions on the render threadi[/b]
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
Edited by Kaptein

Share this post


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

Share this post


Link to post
Share on other sites
Marz Tierney    125
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!

Share this post


Link to post
Share on other sites

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  

  • Similar Content

    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
    • By Picpenguin
      Hi
      I'm new to learning OpenGL and still learning C. I'm using SDL2, glew, OpenGL 3.3, linmath and stb_image.
      I started following through learnopengl.com and got through it until I had to load models. The problem is, it uses Assimp for loading models. Assimp is C++ and uses things I don't want in my program (boost for example) and C support doesn't seem that good.
      Things like glVertexAttribPointer and shaders are still confusing to me, but I have to start somewhere right?
      I can't seem to find any good loading/rendering tutorials or source code that is simple to use and easy to understand.
      I have tried this for over a week by myself, searching for solutions but so far no luck. With tinyobjloader-c and project that uses it, FantasyGolfSimulator, I was able to actually load the model with plain color (always the same color no matter what I do) on screen and move it around, but cannot figure out how to use textures or use its multiple textures with it.
      I don't ask much: I just want to load models with textures in them, maybe have lights affect them (directional spotlight etc). Also, some models have multiple parts and multiple textures in them, how can I handle those?
      Are there solutions anywhere?
      Thank you for your time. Sorry if this is a bit confusing, English isn't my native language
  • Popular Now