TL;DR: I'm sampling the depth values from a depth-stencil texture while at the same time having it bound to an FBO with depth testing enabled but depth writing disabled, and on some cards I'm getting random flickering all over the screen.
I'm currently writing a deferred lighting renderer, and it's working great on my Nvidia Geforce GTX 295. The engine first draws the geometry information to two GL_RGBA16F g-buffers plus a depth-stencil buffer. Then in the lighting pass I pass in those 3 textures, and render my lights as spheres. I render all my lights spheres twice. The first pass marks pixels that cannot be hit by light with the stencil buffer, and the second lights pixels that pass both the depth and stencil test.
//Mark pixels that are too far away from the camera to be affected by the light glCullFace(GL_BACK); glStencilFunc(GL_ALWAYS, 0, 0xFF); glStencilOp(GL_KEEP, GL_KEEP, GL_INCR); glColorMask(false, false, false, false); glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0); //Light pixels that passed the too-far-away-test AND the depth test. //This filters out only the pixels that actually are affected by light. glCullFace(GL_FRONT); glStencilFunc(GL_EQUAL, 0, 0xFF); glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO); glColorMask(true, true, true, true); glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
This works fine on my graphics card and produces perfect results. However, when I test the same program on an AMD Radeon HD 5870, I get randomly flickering squares all over the screen. It looks very much like what happens on my graphics card if I sample a texture while at the same time writing to it with an FBO, causing a race condition between writing and reading during overdraw. It's true that I have the depth-stencil texture both bound as a texture input to the shader so I can reconstruct position from depth while also having it bound to the FBO so I can do stencil and depth testing, but I have disabled depth WRITING so there shouldn't be a problem, right? It shouldn't be a problem since I'm not writing to it.