ARB_pbo question

Hi!

The new ARB_pbo extension (perhaps the older EXT_pbo, too?) suggests the following code for an iterated update of texture data inside the pbo-buffer:

 
// Reset the contents of the texSize-sized buffer object
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, texSize, NULL,
             GL_STREAM_DRAW);

// Map the texture image buffer (the contents of which
// are undefined due to the previous glBufferData)
pboMemory = glMapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB,
                        GL_WRITE_ONLY);

// Modify (sub-)buffer data
memcpy(pboMemory, texData, texsize);

// Unmap the texture image buffer
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB);
 

Why should I not use this simpler code for the update: :confused:

 
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, texSize, texData,
             GL_STREAM_DRAW);
 

Bad example - in the case of memcpy you’re better off using glBufferData, but if you are generating data on the fly it may be better to write it into a mapped buffer.

It’s an example. It as lots of redundant stuff… :wink:

But I still cannot see, why one copy of the data can be avoid:

Streaming texture updates: If the application uses glMapBuffer/glUnmapBuffer to write its data for glTexSubImage into a buffer object, at least one of the data copies usually required to download a texture can be eliminated, significantly increasing texture download performance.

But I still cannot see, why one copy of the data can be avoid:
Here’s why.

When you call “glBufferData”, it copies stuff from your “texData” variable into the internal VBO. When you use mapping, you’re writing (theoretically, as drivers could still force a copy operation) directly to the VBO server-side memory.

This eliminates a copy on your side. If you have an array of data, and you only update pieces of it, you don’t need to keep that array of data yourself. You can let the server keep it, and update it via mapping. If you use glBufferData, you must keep an array and you force the driver to copy your array into VBO memory.

Now, their example is rather silly, becuase they use a “memcpy” (effectively manually doing what “glBufferData” does). However, if you were only updating a portion of the memory, it’d be better to use mapping and just update what you need to than to use glBufferData.