PDA

View Full Version : question related to buffer object..



epsxk82
04-20-2010, 05:51 AM
Hi.
In rendering an animation, I'll use a buffer object for better rendering performance.
There is 2 ways to make it.
1. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * numVertices
* 3, VertexArray, GL_STREAM_DRAW);
2. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * numVertices
* 3, NULL, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) *
numVertices * 3, VertexArray);
How is 1 different from 2?
It is said that 2 shows better rendering performance than 1. why is so?

Dark Photon
04-20-2010, 06:57 AM
In rendering an animation, I'll use a buffer object for better rendering performance. There is 2 ways to make it.


1. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * numVertices * 3, VertexArray, GL_STREAM_DRAW);
2. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * numVertices * 3, NULL, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * numVertices * 3, VertexArray);
How is 1 different from 2?
It is said that 2 shows better rendering performance than 1. why is so?

#1 allocates the buffer "and" provides contents for it (since the data pointer is not NULL).

#2 allocates the buffer in the first statement (but doesn't provide contents as the pointer is NULL), and then provides the contents for it in the separate, second statement.

The advantage to the second approach for a buffer where you need to update it again and again is that you only call the glBufferData call to allocate the buffer once, then to update the buffer use glSubBufferData. This allows you to not consume the overhead of needlessly reallocating the buffer every time you need to upload new contents for it. You just provide the new contents for the "existing" buffer. If you repeatedly call glBufferData, you are repeatedly reallocating the buffer, which is a waste.

Also, there are other ways besides these two to upload data to a buffer object, such as glMapBuffer and glMapBufferRange. And these may net you even higher upload performance.

Read this post for more details, paying particular note to Rob Barris's posts: VBOs strangely slow? (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=272672)

pjcozzi
04-20-2010, 06:51 PM
Also check out this post (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Main=51749&Number=2673 73#Post267373).

Patrick