Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL read pixels faster than glReadPixels

Tags:

opengl

Is there a way to increase the speed of glReadPixels? Currently I do:

Gdx.gl.glReadPixels(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE, pixels);

The problem is that it blocks the rendering and is slow.

I have heard of Pixel Buffer Objects, but I am quite unsure on how to wire it up and whether it is faster or not.

Also is there any other solutation than glReadPixels?

Basically, I want to take a screenshot as fast as possible, without blocking the drawing of the next scene.

like image 521
Niklas Avatar asked Aug 04 '14 21:08

Niklas


1 Answers

Is there a way to increase the speed of glReadPixels?

Well, the speed of that operation is actually not the main issue. It has to transfer a certain amount of bytes from the framebuffer to your system memory. In your typical desktop system with a discrete GPU, that involves sending the data over PCI-Express, and there is no way around that.

But as you already stated, the implicit synchronization is a big issue. If you need that pixel data as soon as possible, you can't really do much better than that synchronous readback. But if you can live with getting that data later, asynchronous readback via pixel buffer objects (PBOs) is the way to go.

The pseudo code for that is:

  1. create PBO
  2. bind PBO as GL_PIXEL_PACK_BUFFER
  3. do the glReadPixels
  4. do something else. Both work on the CPU and issuing new commands for the GPU is ideal.
  5. Read back the data from PBO by either using glGetBufferSubData or by mapping the PBO for reading.

The crucial point is the timing of step 5. I you do that to early, you still blocking the client side, as it will wait for the data to become available. For some screenshots, It should not be hard to delay that step for even one or two frames. That way, it will have only a slight impact on the overall render performance, and it will neither stall the GPU nor the CPU.

like image 138
derhass Avatar answered Nov 12 '22 04:11

derhass