View Full Version : Editing an image (image effects) with a fragment shader

03-29-2015, 04:25 PM
I have various fragment shaders such as outline, sobel, hue rotation, ect. I want to apply these effects on an image, and save it to disk.

Everything is strictly 2d, is there a way that I can just use a fragment shader?

Right now the only way I can think of is:
*change viewport to size of image
*draw screen filling quad
*apply the image as the texture to the screen filling quad
*render to texture
*after the first pass I can do all the editing I like in the fragment shader of the following passes

Is this my only option? Am I able to implement the above with rectangle and non-power of 2 textures?

Alfonse Reinheart
03-29-2015, 04:48 PM
Are you trying to apply a sequence of fragment shader processes to an image, such that each FS operates on the results of the prior FS? If that's the case, then you're effectively talking about doing a series of read/modify/write operations to an image.

If you're rendering to a texture, then you need to be mindful of the fact that you can't read from a texture you're writing to. (https://www.opengl.org/wiki/Memory_Model#Framebuffer_objects) So you'd have to ping-pong between images to make this work. However, if you have NV/ARB_texture_barrier (https://www.opengl.org/wiki/Memory_Model#Texture_barrier), then you just need to issue a barrier between each rendering call. And a single read/modify/write will work even from the same texture.

To use Image Load/Store (https://www.opengl.org/wiki/Image Load Store), you would need to use appropriate synchronization (https://www.opengl.org/wiki/Memory_Model#Incoherent_memory_access) to make read/modify/writes work.

Am I able to implement the above with rectangle and non-power of 2 textures?

All versions of OpenGL 2.0 or better consider NPOT (non-power-of-two) textures (https://www.opengl.org/wiki/NPOT_Texture) to be functionally no different from POT textures.