views:

663

answers:

2

I'm rendering a certain scene into a texture and then I need to process that image in some simple way. How I'm doing this now is to read the texture using glReadPixels() and then process it on the CPU. This is however too slow so I was thinking about moving the processing to the GPU.

The simplest setup to do this I could think of is to display a simple white quad that takes up the entire viewport in an orthogonal projection and then write the image processing bit as a fragment shader. This will allow many instances of the processing to run in parallel as well as to access any pixel of the texture it requires for the processing.

Is this a viable course of action? is it common to do things this way? Is there maybe a better way to do it?

+2  A: 

Yes, this is the usual way of doing things.

  1. Render something into a texture.
  2. Draw a fullscreen quad with a shader that reads that texture and does some operations.

Simple effects (e.g. grayscale, color correction, etc.) can be done by reading one pixel and outputting one pixel in the fragment shader. More complex operations (e.g. swirling patterns) can be done by reading one pixel from offset location and outputting one pixel. Even more complex operations can be done by reading multiple pixels.

In some cases multiple temporary textures would be needed. E.g. blur with high radius is often done this way:

  1. Render into a texture.
  2. Render into another (smaller) texture, with a shader that computes each output pixel as average of multiple source pixels.
  3. Use this smaller texture to render into another small texture, with a shader that does proper Gaussian blur or something.
  4. ... repeat

In all of the above cases though, each output pixel should be independent of other output pixels. It can use one more more input pixels just fine.

An example of processing operation that does not map well is Summed Area Table, where each output pixel is dependent on input pixel and the value of adjacent output pixel. Still, it is possible to do those kinds on the GPU (example pdf).

NeARAZ
+1  A: 

Yes, it's the normal way to do image processing. The color of the quad doesn't really matter if you'll be setting the color for every pixel. Depending on your application, you might need to careful about pixel sampling issues (i.e. ensuring that you sample from exactly the correct pixel on the source texture, rather than halfway between two pixels).

Doug