View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Reusing VBOs

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.

9 replies to this topic

### #1voodoodrul  Members

Posted 05 July 2013 - 11:06 AM

I have an odd problem. My render() code does this

render() {
...
if (rebuildMesh == true) {
buildMesh();
}
...
}

buildMesh() {
....
if (this.vboVertexHandle == 0) {
this.vboVertexHandle = glGenBuffers();
}
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, this.vbuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

...
}


It creates a buffer if one doesn't exist, binds it, and buffers the data.

The problem comes later when another render() call decides to update the mesh. It creates the vertex buffer correctly and then tries to upload the data into the existing vboVertexHandle. This seems to result in a corrupted VBO with fragments of the original vertex data somehow "transposed" onto the existing buffer.

I was under the impression that glBufferData(GL_ARRAY_BUFFER, this.vbuffer, GL_STATIC_DRAW) would replace the entire contents of the buffer, but it doesn't appear to. It seems to prepend the new data onto the buffer?

I need to flush and upload all new data to the VBO. The VBO holds a mesh that changes infrequently.

This code works:

buildMesh2() {
....
if (this.vboVertexHandle == 0) {
this.vboVertexHandle = GL15.glGenBuffers();
} else {
GL15.glDeleteBuffers(vboVertexHandle);
this.vboVertexHandle = GL15.glGenBuffers();
}
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboVertexHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, this.vbuffer, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
...
}


But that seems wasteful to delete and gen a whole new buffer..

### #2MarkS  Members

Posted 05 July 2013 - 02:21 PM

When you call glBufferData, you are telling the GPU how much memory to set aside for the buffer. First, use GL_DYNAMIC_DRAW if you are going to be changing the buffer.

I am not too sure about the potential consequences, if any, of calling glBufferData multiple times on the same buffer. I would surmise that changing the buffer size could trash the data stored in the VRAM. I *think* that glBufferData is similar to calling "new" on a pointer. If you need to update part of the buffer, use glBufferSubData.

Edited by MarkS, 05 July 2013 - 02:23 PM.

### #3voodoodrul  Members

Posted 05 July 2013 - 03:24 PM

When you call glBufferData, you are telling the GPU how much memory to set aside for the buffer. First, use GL_DYNAMIC_DRAW if you are going to be changing the buffer.

I am not too sure about the potential consequences, if any, of calling glBufferData multiple times on the same buffer. I would surmise that changing the buffer size could trash the data stored in the VRAM. I *think* that glBufferData is similar to calling "new" on a pointer. If you need to update part of the buffer, use glBufferSubData.

That's why I thought I should be able to reuse it without flushing it..

Each chunk has its own VBO so I should be able to create/destroy them without worry. I don't need to rewrite portions of the buffer, I just need a overwrite with data that might be larger or smaller..

Think this might be a bug in LWJGL's JNI calls? *update* latest version doesn't change anything..

Edited by voodoodrul, 05 July 2013 - 03:43 PM.

### #4BornToCode  Members

Posted 05 July 2013 - 03:29 PM

If the data is smaller there is no need to call glBufferData. As MarkS mention glBufferData reallocate the memory for the active buffer, which in turn will trash whatever data you had in there. The gl drawing routines allows you to render just a section of that buffer, so there is no need to reallocate if you just downsize the buffer.

Edited by BornToCode, 05 July 2013 - 03:30 PM.

### #5mhagain  Members

Posted 05 July 2013 - 05:05 PM

glBufferData will trash the buffer, yes; think of it as being the same as calling glTexImage at runtime (i.e. you most definitely don't want to do it).  It's difficult to recommend a better buffer update strategy without having a clear understanding of exactly what the problem that the OP is trying to solve actually is, so more detail around those lines would be appreciated.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #6voodoodrul  Members

Posted 05 July 2013 - 05:20 PM

glBufferData will trash the buffer, yes; think of it as being the same as calling glTexImage at runtime (i.e. you most definitely don't want to do it).  It's difficult to recommend a better buffer update strategy without having a clear understanding of exactly what the problem that the OP is trying to solve actually is, so more detail around those lines would be appreciated.

It's probably best to refer you to my earlier thread - http://www.gamedev.net/topic/644944-optimization-of-many-small-gldrawelements-calls/

Just focus on the last couple entries there to get a feel for how I am creating vertex arrays.

Sorry for creating another thread, but I guess I had diverged from the initial problem enough to facilitate a new one..

As you can see, that's how I'm drawing my cubes and populating a vertex array. It works well as long as I explicitly call glDeleteBuffers() and glGenBuffers() again before populating the VBO.

I have discovered that these rogue faces occur on the edges of chunks - each chunk is a:

glPushMatrix()

glTranslatef()

//draw VBO

glPopMatrix();

It's as if the degenerate verticies I am using to draw only certain faces are "bleeding over" into the next chunk.render(), but that's a completely unique glBindBuffer() operation and shouldn't impact the previous one..

Is it possible that the glGenBuffers() just slows it down enough to sit inside of some race condition or state issue? Clearly everything in the render() loop should be occurring in a single thread anyway so I doubt it. I do offload work into thread for things like building the vertex arrays (converting the chunk to a mesh) and then flag that chunk as "ready" for the render loop to actually push it in to the VBO if not already..

Edited by voodoodrul, 05 July 2013 - 05:39 PM.

### #7voodoodrul  Members

Posted 05 July 2013 - 05:49 PM

Solved. I blame this on 4 days of less than 3 hours of sleep per night. Ha.

When I draw, I was doing:

glDrawArrays(GL_TRIANGLES, 1, this.numVerts);

I step over the first degenerate vertex as I should. The problem was that, stupid me, this.numVerts was being calculated wrong..

I was doing:

this.numVerts = this.vbuffer.position() / 8;

this.numVerts = this.vbuffer.position() / 12;

You'll see my blocks hold 12 floats per vertex. So chalk this one up to me being stupid.

I suppose it's useful to know that if you tell glDrawArrays(GL_TRIANGLES, 1, x); where x is larger than the currently bound buffer, you can and will overrun it and spill over into surrounding memory.

Lesson learned.

Edited by voodoodrul, 05 July 2013 - 05:56 PM.

### #8MarkS  Members

Posted 05 July 2013 - 10:38 PM

Solved. I blame this on 4 days of less than 3 hours of sleep per night. Ha.

When I draw, I was doing:

glDrawArrays(GL_TRIANGLES, 1, this.numVerts);

I step over the first degenerate vertex as I should. The problem was that, stupid me, this.numVerts was being calculated wrong..

I was doing:

this.numVerts = this.vbuffer.position() / 8;

this.numVerts = this.vbuffer.position() / 12;

You'll see my blocks hold 12 floats per vertex.

That would do it as well! Been there, done that. And quite recently!

I suppose it's useful to know that if you tell glDrawArrays(GL_TRIANGLES, 1, x); where x is larger than the currently bound buffer, you can and will overrun it and spill over into surrounding memory.

Well, yes. This is no different than accessing an array out of bounds on a CPU. You'll successfully access the data outside of the array, but the result will not be what you expect.

I do stand by what I said about using glBufferSubData...

glBufferData

glBufferData creates a new data store for the buffer object currently bound to target. Any pre-existing data store is deleted. The new data store is created with the specified size in bytes and usage.

glBufferSubData

glBufferSubData redefines some or all of the data store for the buffer object currently bound to target. Data starting at byte offset offset and extending for size bytes is copied to the data store from the memory pointed to by data. An error is thrown if offset and size together define a range beyond the bounds of the buffer object's data store.

When replacing the entire data store, consider using glBufferSubData rather than completely recreating the data store with glBufferData. This avoids the cost of reallocating the data store.

You *can* use glBufferData, but you'll see a performance drop as it has to delete the data and then create a new data store. glBufferSubData works within the existing buffer's memory store and doesn't do any expensive memory allocation/management.

Edited by MarkS, 05 July 2013 - 10:49 PM.

### #9voodoodrul  Members

Posted 06 July 2013 - 12:31 PM

Thanks MarkS. The community here is really helpful. I think I've solved most of my current issues and I think I'm getting some really decent performance out of my prototype voxel renderer. I want to share it with the world so I'll post a link to my app here:

https://voodoo.arcanelogic.net/CYDI-latest.jar

If anyone cares, this represents about 4-5 weeks of entirely from-scratch effort to teach myself OpenGL. Despite being a programmer for a few years, I have never worked with graphics programming before and I just wanted to see how long it would take to get something simple off the ground.

My renderer uses perlin noise to generate a seeded, random, and unique heightmap with each app restart and uses chunked rendering to page in and out chunks/tiles.

Controls are Minecraft-like. Space to jump, double space to fly up, shift to fly down. Turn off camera collision to fly outside the chunk boundaries. Move around faster with +/- keys. Then turn up the view distance (F5) and turn off vsync (F8) and take in the view.

Oddly, I get really excellent performance on Intel integrated graphics cards - on an HD 5000 card I'm generally getting 60fps with a million exposed block faces. 250-300fps with view distance 11, which I think is plenty. Still holds 60fps at view distance 33 which I think is nutty, especially for an integrated graphics card...

My GTX 690, though, holds steady at ~2450 fps.

Edited by voodoodrul, 06 July 2013 - 12:42 PM.

### #10mhagain  Members

Posted 06 July 2013 - 12:54 PM

You *can* use glBufferData, but you'll see a performance drop as it has to delete the data and then create a new data store. glBufferSubData works within the existing buffer's memory store and doesn't do any expensive memory allocation/management.

It doesn't always have to; if you specify the same size and usage parameters as before, in theory it can just reuse the previous storage.  In some cases this can actually be a performance optimization as it can also allow for the previous storage to be orphaned; after a few frames things will settle down and the driver will just be cycling through previously allocated blocks of memory (in other words it's classic double-buffering but managed for you by the driver).  The tricky part here is knowing this and tuning your usage of the buffer to allow this to happen, which is why it's generally not recommended.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

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.