In my rendering pipeline (OpenGL 3.3 core), I have a following cycle (pseudo-code):
for 1..n:
render to texture T
bind texture T
render to back buffer (with texture T sampled in the fragment shader)
When n=1
, everything is OK. However, when n=2
, the first render is not correct.
I suspect render to texture T
to begin the writing to T
before previous render to back buffer
has finished the sampling of T
.
After putting glFlush()
to the end of the cycle, the rendering is correct, however, FPS drops a little. Everywhere on the Internet I keep finding "If you need to use glFlush() you are probably doing something wrong".
Did i identify the problem correctly? Is glFlush()
a correct solution in this case? Would using a different texture for each iteration (I know n
) be better solution?
It happens on GTX580 but not on ATI Mobility Radeon 3470.
My context - details:
I have 2 lights and g-buffer (FBO). In each iteration I am doing a deferred shading with one light and its shadow map (another FBO with texture T
- mutual for all lights), where n
is a number of lights. In render to back buffer
I accumulate the light.
In the following image, I did not accumulate the light. Instead I rendered the first iteration to the left viewport and the second to the right one to demonstrate the problem.