Monday, 28 January 2013

Reading the OpenGL backbuffer to system memory

Sometimes you are in the need to read back the OpenGL backbuffer, or other framebuffers. In my case the question was how to read back a downsampled framebuffer resp. its texture to system memory. There are different methods for this and I wrote a small benchmark to test them on various systems.

UPDATE #1: A thing I've totally overlooked is that you can actually blit the backbuffer to the downsampled framebuffer directly, saving you the FBO overhead (memory, setup, rendering time). I've updated the page and the code reflecting that.
UPDATE #2: The code now works on Windows and Linux (via GLX). Maybe I'll port it to GLES2 for the Raspberry Pi too... :)
UPDATE #3: The code works on the Raspberry Pi now using OpenGL ES 2.0. Reading the framebuffer is quite slow though even with an overclocked device. It's not the actual reading that is slow, but that we have to render to another framebuffer first and back to the screen. Atm all buffers and textures use RGBA8888, which might slow things down. I'll try RGB565 in the future. Also there are no PBOs and no glGetTexImage(), so only the glReadPixels method is working... If you have any ideas on how to make the code faster, I'd love to hear them.
UPDATE #4: Tinkered around with color formats a bit and updated the Pi's firmware and MESA implementation. I've tried changing the FBOs color format to 16bit, but that didn't change much. The secret seems to lie in using a RGB565 EGL display/framebuffer format with no alpha, no depth, no stencil and to remove the glDiscardFramebuffer calls. The FBO backbuffer and the downsampled FBO still have RGBA8888. Together with using a screen size of 640x480 this nearly quadrupled the frame rate. Blitting the FBO to the screen now seems to be much, much faster... I updated the code accordingly and also did some cleanup and found a safer way to get function addresses on Linux systems.


I needed to downsample the framebuffer. This can be done by blitting to a smaller framebuffer that will be read back. For setting up framebuffers, see here. In your rendering loop, do:

// set viewport to window size
glViewport(0, 0, width, height);

// draw stuff here

// blit backbuffer to downsampled buffer
context->glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
context->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, smallId);
context->glBlitFramebuffer(0, 0, width, height, 0, 0, smallWidth, smallHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR);

// read back buffer content here (Method #1, #2, #3)
// unbind again
glBindFramebuffer(GL_FRAMEBUFFER, 0);

Blitting the framebuffer using glBlitFramebuffer isn't much faster than binding a texture and rendering a quad to another framebuffer, but it has much less setup needed and you don't find yourself setting up projection or modelview matrices and trashing the state.

Method #1 - glReadPixels

The first method is to use a standard glReadPixels. This is the common way to read the backbuffer since ancient OpenGL times. Do (depending on you framebuffer format):

// bind downsampled buffer for reading
glBindFramebuffer(GL_FRAMEBUFFER, smallId);

glReadPixels(0, 0, smallWidth, smallHeight, GL_RGBA, GL_UNSIGNED_BYTE, downsampleData);
// unbind buffer again
glBindFramebuffer(GL_FRAMEBUFFER, 0);

The data will end up in downsampleData in system memory. Note that you need to allocate space for that data before!

Method #2 - glGetTexImage

The second method does not read the actual buffer, but the texture attached to it. That can be done using glGetTexImage. Do (depending on you framebuffer format):

// bind downsampled texture
glBindTexture(GL_TEXTURE_2D, downsampleTextureId);

// read from bound texture to CPU
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, downsampleData);
// unbind texture again
glBindTexture(GL_TEXTURE_2D, 0);

The data will end up in downsampleData in system memory again. Note that you need to allocate space for that data before!

Method #3 - PixelBufferObjects

This is the most complicated, but a slightly faster method than the previous two. You read the downsampled framebuffer using glReadPixels again, but now you read it to a PixelBuffer object asynchronously. For that you set up two PBOs and alternate between them so the GPU can go on rendering to the other while you process the first. The download of the data to system memory happens using DMA and does not block the CPU. To do this, first start up creating two PBOs:

int readIndex = 0;
int writeIndex = 1;
GLuint pbo[2];

// create PBOs to hold the data. this allocates memory for them too
glGenBuffers(2, pbo);
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo[0]);
glBufferData(GL_PIXEL_PACK_BUFFER, smallWidth, smallHeight * smallDepth, 0, GL_STREAM_READ);

glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo[1]);
glBufferData(GL_PIXEL_PACK_BUFFER, smallWidth, smallHeight * smallDepth, 0, GL_STREAM_READ);
// unbind buffers for now
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);

Now that you have created the PBOs, go to your rendering loop and there you read back data to the PBOs there.

// bind downsampled fbo
// swap PBOs each frame
writeIndex = (writeIndex + 1) % 2;
readIndex = (writeIndex + 1) % 2;
// bind PBO to read pixels. This buffer is being copied from GPU to CPU memory
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo[writeIndex]);
// copy from framebuffer to PBO asynchronously. it will be ready in the NEXT frame
glReadPixels(0, 0, downsample->getWidth(), downsample->getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
// now read other PBO which should be already in CPU memory
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo[readIndex]);
// map buffer so we can access it
downsampleData = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
if (downsampleData) {
    // ok. we have the data now available and could process it or copy it somewhere
    // ...
    // unmap the buffer again
    downsampleData = nullptr;

// back to conventional pixel operation
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
// unbind downsampled fbo
// now in the next readIndex and writeIndex will be swapped and we'll read from what is currently writeIndex...

The data will end up in downsampleData in system memory. This method seems to be slightly faster on some systems, but gain is highly dependent on the frame rate.


The results depend on the system, method used and read color format. Here's a table showing my benchmarking results. The first column is regular rendering to the backbuffer, no FBOs involved. Then follow the tests, every test first without actually reading the buffer, just rendering to the downsampled FBO. Then two tests reading the downsampled buffer to system memory using different formats:
SystemNo FBOs#1 no reading#1 BGRA#1 RGBA#2 no reading#2 BGRA#2 RGBA#3 no reading#3 BGRA#3 RGBA
Intel Core i7 960
Nvidia Quadro 600, Driver 310.90
OpenGL 4.3,
Intel Core i5 480M
AMD Radeon 5650M, Driver Catalyst 13.1
OpenGL 4.2,
Intel Core i5 480M
Integrated HD Graphics, Driver
OpenGL 2.1,
Windows 7 x64, frame time in ms, vsync off, data size 160x90x32bit

SystemNo FBOs#1 no reading#1 BGRA#1 RGBA#2 no reading#2 BGRA#2 RGBA#3 no reading#3 BGRA#3 RGBA
Intel Core i5 480M
Integrated HD Graphics, Driver Mesa DRI Intel Ironlake Mobile
OpenGL 2.1, GLX 1.4, MESA 9.0.2,
Intel Core i3-2120T
Integrated HD Graphics 2000, Driver Mesa DRI Intel Sandybridge Desktop x86/MMX/SSE2
OpenGL 3.0, GLX 1.4, MESA 9.0.2,
  Ubuntu 12.10 x64, frame time in ms, vsync off ("export vblank_mode=0"), data size 160x90x32bit

SystemNo FBOs#1 no reading#1 BGRA#1 RGBA#2 no reading#2 BGRA#2 RGBA#3 no reading#3 BGRA#3 RGBA
Intel Core i5 480M
Integrated HD Graphics, Driver Mesa DRI Intel Ironlake Mobile
OpenGL 2.1, GLX 1.4, MESA 11.2.0,
  Ubuntu 16.04 x64, frame time in ms, vsync off ("export vblank_mode=0"), data size 160x90x32bit

SystemNo FBOs#1 no reading#1 BGRA#1 RGBA#2 no reading#2 BGRA#2 RGBA#3 no reading#3 BGRA#3 RGBA
Raspberry Pi
OpenGL ES 2.0, EGL 1.4,
  Raspian (Updates: March 25th, 2013), frame time in ms, vsync off (eglSwapInterval(0)), data size 160x90x32bit

Source code

The source code, a CMake build file and the benchmark results can be found on GitHub and building should work at least on Windows 7, Ubuntu 12.10 (via GLX on X11) and on a current Raspian for the Raspberry Pi (via EGL). The beef is in the Test_... classes. To do a benchmark, run the application from the command line and wait for it to finish. It'll spit out some frame time values for the different methods and color formats.
I'd love to get your feedback on how else to read to system memory or maybe speed up the methods. Just drop me a comment.


jdobmeier said...

With some SoCs, in particular the RPi, you can get true DMA as opposed to mere memory mapped I/O. A call to glMapBuffer returns a pointer to the actual buffer in GPU address space (since all memory is, in principal, mutually accessible via the front side bus) so you can avoid even the latency associated with transfer over the PCI bus. This "issue" on Github demonstrates one method: There is also a lot of info on the bare metal section of the RPi forum. Also here:

If you do end up porting what you have above I would be willing to help test. I am also working toward getting this same functionality implemented for the Pi to support some GPGPU experiments.

Kim said...

Sounds nice. Thanks for the info!
There's a reason I'm actually trying this is (as you can grasp from the blog posts) - to get XBMC to render all GL-rendered stuff to my ambilight via boblight (not only videos). I threw together those examples to convince the XBMC people (and myself) that performance is not an issue.
I'm currently porting to the PI, but wasn't familiar with EGL before, there's no glBlitFrameBuffer, eglGetProcAdress doesn't work properly, I'm trying out new things and so on... :) So it takes time...
You should be able to drop me an Email now if you're interested. I can send you the code package when it's done. Or just check the page from time to time. No deadlines though ;)

Kim said...

@jdobmeier: The test code is working, but the problem is that glMapBuffer only supports vertex or index buffers, no PBOs or other stuff AND only supports writes, so even rendering to a vertex buffer is of no use. All the examples you mentioned show that reading the actual framebuffer (what is on screen) is possible, but not just any FBO. The trick in my code is supposed to be that the more powerful GPU does the downsampling and frees the CPU from that task... So your approach actually doesn't make sense in my case...
If you have any idea on how to get the address of an FBO attachment that would be great though.

Kim said...

Btw. The documentation of the GL_OES_mapbuffer extension is here:

Gregory Tetard said...


Nadnerb from raspberry pi forum made a guide on setting up boblight on a Raspberry Pi with, hopefully, every step you need to take to get it up and running.
This includes the hardware and software elements and uses 50 WS2801 LEDs.

here after is the link:

Bim said...

Thanks for the info, but it does not seem like the boblight forks or the omxplayer thingie capture from OpenGL, which is what I wanted...
I wanted boblight not only when playing videos, but also in the menu, visualization etc.

D. Cerisano said...

Not sure why, but following this thread just reduced my GL to H264 transcoder to almost nothing. Time for bed.

Anton said...

Why not do glReadPixels after glUnmapBuffer? Same speed, minus one buffer.

Bim said...

Not sure what you mean. The idea is to hide the latency of copying to CPU memory, and not stall the CPU and GPU. Therefore 2 buffers are used, one that is used to copy from GPU to CPU memory asynchronously (writeIndex) and the other that is already copied and can be read from (readIndex). These buffers are swapped every frame.
With only one buffer you'd stall the GPU.
You are right, whether this is a real gain depends on your GPU + driver + system and probably on how long your rendering takes...

Post a Comment