Accessing FBO during deferred light pass with combined stencil/depth buffer


I’ve hit a problem during my attempt to write a deferred light shader using stencil light volumes. The combined depth/dtencilbuffer setup looks like this:

    glGenTextures(1, &ids[depthbuffer]);
    glBindTexture(GL_TEXTURE_2D, ids[depthbuffer]);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, dim[0], dim[1], 0,
            GL_TEXTURE_2D, ids[depthbuffer], 0);

During the lighting pass when the stencil buffer is being set it seems that I cannot access the attached depth buffer and write the final color to the screen at the same time. The problem is being discussed in some tutorials and the solution would be to write the lighting pass into a separate color buffer and then blit the color buffer to the FBOs final color buffer (screen). Is there a smarter solution without the need of creating another buffer and copy the color data around?

Thanks & Regards

You cannot read back from a output buffer in a fragment shader. You always need 2 passes. One to write to a buffer and another to read. Put antohter way you cannot attach the same buffer both as an input and an output to the same shader.

Hey i am trying to implement depth stencil textures with FBO but i get a black o/p. Can you post your code or tell me algorithm to implement stencil textures, it would help me. thanks…

hi! take a look at my last thread: