I try to implement DOF effect in my shader. In the shader I have color texture + offset for blurring. I need to add offset for RGB and my own A channel, which I make in the shader before. How can I replace the alpha channel in one step? I can't to find something suitable among GLSL functions. Thank you!
-
This isn't very clear, do you want to sample from the texture and save a new value in its alpha channel, all in a single shader? – pleluron Jan 03 '17 at 15:33
-
**pleluron** Yes, that's! Not difficult for me implement this outside the shader, but interesting how to solve this into. – javierMarquez Jan 03 '17 at 15:38
-
What version of OpenGL are you using? – pleluron Jan 03 '17 at 15:41
-
**pleluron** 330 or above. – javierMarquez Jan 03 '17 at 15:45
-
Is there really a need of having the colors and offsets in the same texture? Using separate textures would be better overall, both for practicality and performance. – pleluron Jan 03 '17 at 15:51
-
**pleluron** It's not so important but it would be to make life better. I confused in textures. :)) Ok, I'll try it for separate textures. Thanks! – javierMarquez Jan 03 '17 at 15:56
1 Answers
Yes, it's possible to use textures (or rather images) as both input and outputs, but you shouldn't do it.
The OpenGL driver needs to know what is an input and what is an output, so it can properly synchronize the operations (e.g. a read on a texture should wait for a write to that same texture to have completed...)
OpenGL 4.2 introduces Image Load/Store, a way of reading and writing to images, but you have to manage synchronization yourself (with barrier()
and *Barrier()
calls). It's meant for data that has to be both read and written to, e.g. atomic counters, transparency values...
In your case, there are two sets of data: colors and offsets. One will only be read from, and the other only written to. It's better to just separate the two and simply treat them as regular inputs/outputs (texture binding/FBO attachment). If you used one texture for the two sets, it would be treated as one entity to synchronize, thus leading to more waits somewhere else.

- 733
- 4
- 12