Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Texture Image processing on the GPU?

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?

like image 499
shoosh Avatar asked Jan 08 '09 09:01

shoosh


2 Answers

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).

like image 83
NeARAZ Avatar answered Nov 15 '22 11:11

NeARAZ


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).

like image 39
Doug Avatar answered Nov 15 '22 09:11

Doug