How to write to a PBO taking image stride into account?

#1

I have a Pixel Buffer Object for which I need to write an YUV image. The problem is that the YUV image has a stride, and therefore, I cannot simply copy the buffer sequentially, I need to do it line by line. That is, stop when I hit the stride of each line and go to the next line.

As I see in glBufferData, it has no way of doing that, I can only copy from a single buffer to an entire texture.

Is there another function that does what I do or make me able to fill the texture line by line?

#2

If there’s a fixed stride between rows, call glPixelStorei with the first parameter set to GL_UNPACK_ROW_LENGTH before calling glTexImage2D. Remember to set it back to zero afterwards if you have other textures.

If each row is at an arbitrary offset, call glTexImage2D without anything bound to GL_PIXEL_UNPACK_BUFFER and with a null pointer for the data parameter to allocate storage without uploading data, then call glTexSubImage2D to upload each row of data.

With OpenGL 4.2+, you can also use glTexStorage2D to allocate storage without uploading data. That makes the texture an “immutable-format” texture; you can modify the texture data but not the format, dimensions or number of mipmap levels.

Do you need to control the transfer of data between client memory and the PBO, or between the PBO and the texture? For the former, you can call glBufferData with a null data pointer to allocate storage without transferring data, then use glBufferSubData (or glMapBuffer and memcpy) to transfer portions of the data.