• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
voodoodrul

Reusing VBOs

9 posts in this topic

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.. 

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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;

 

instead of 

 

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
0

Share this post


Link to post
Share on other sites

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;
 
instead of 
 
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! dry.png 
 

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
0

Share this post


Link to post
Share on other sites

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. smile.png

Edited by voodoodrul
0

Share this post


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

1

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  
Followers 0