Reputation: 5255
Can I render to the same texture, which I pass to my shader?
gl.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, currentTex, 0);
gl.glActiveTexture(GL_TEXTURE0);
gl.glBindTexture(GL_TEXTURE_2D, currentTex);
gl.glUniform1i(texGlId, 0);
// ...
// drawCall
Upvotes: 0
Views: 467
Reputation: 54572
No, you're not supposed to do that. The OpenGL specs call this a "rendering feedback loop". There are cases where you can use the same texture, for example if you render to a mipmap level that is not used for texturing. But if you use a level that is included in your texturing as a render target , the behavior is undefined.
From page 80 of the ES 2.0 spec, "Rendering Feedback Loops":
A rendering feedback loop can occur when a texture is attached to an attachment point of the currently bound framebuffer object. In this case rendering results are undefined. The exact conditions are detailed in section 4.4.4.
Upvotes: 1
Reputation: 13999
We should avoid it. The rendering result will be undefined, it might depend on GPU.
https://www.khronos.org/opengles/sdk/docs/man/xhtml/glFramebufferTexture2D.xml
Notes
Special precautions need to be taken to avoid attaching a texture image to the currently bound framebuffer while the texture object is currently bound and potentially sampled by the current vertex or fragment shader. Doing so could lead to the creation of a "feedback loop" between the writing of pixels by rendering operations and the simultaneous reading of those same pixels when used as texels in the currently bound texture. In this scenario, the framebuffer will be considered framebuffer complete, but the values of fragments rendered while in this state will be undefined. The values of texture samples may be undefined as well.
Upvotes: 0