I have created an FBO and have a texture bound as its color attachment, and I have multiple shader programs that do some post processing on the texture, everything works great, but it does not make sense to me that the texture can be used as the input(sampler2D) as well as the output of the shaders at the same time.
Following are the steps I have taken:
- Create an FBO
fboA
. - Create a texture
textureA
, and bind it as color attachment offboA
. - Call
glBindFrameBuffer
to bindfboA
to the framebuffer target. - Call
glUseProgram
to use shader programshaderA
. - Call
glDrawArrays
to draw something (eventually drawn ontextureA
becausefboA
is currently bound). - Call
glUseProgram
to use shader programshaderB
which has asampler2D
uniform in the fragment shader. - Bind
textureA
assampler2D
uniform of shader programshaderB
. - In the fragment shader,
textureA
is used to set thefragColor
.
What I am confused about is the last two steps, where textureA
is used as the input of the fragment shader, still it is bound to the current framebuffer. This appears to me that the fragment shader is reading from and writing to the same piece of memory, isn't this some kind of undefined behaviors, why it still works correctly?