1
\$\begingroup\$

I create a packed depth/stencil texture and attach it to a framebuffer like this:

glGenTextures(1,&_depthStencilTexture);
glBindTexture(GL_TEXTURE_2D,_depthStencilTexture);
//set filtering to gl_nearest, not shown cause not important
glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH24_STENCIL8,width,height,0,GL_DEPTH_STENCIL,GL_UNSIGNED_INT_24_8,nullptr);

//... create framebuffer and attach some color attachments
glFramebufferTexture(GL_FRAMEBUFFER,GL_DEPTH_STENCIL_ATTACHMENT,_depthStencilTexture,0);

The creation of the framebuffer causes no OpenGL status errors.

In terms of usage for depth and stencil testing, the texture/attachment seems to work correctly. If I disable depth testing, I get the usual artifacts of objects in the back being drawn over objects in the front if they are drawn in the wrong order. If I disable stencil testing, the effects I'm using related to that don't work anymore (I'm masking out areas that shouldn't be lit by the lighting pass). Also, if I simply don't attach the depth/stencil buffer to the framebuffer, the related tests stop working (as expected).

However, reading from that texture in a shader always returns 1 if I read the depth part, and 0 if I read from the stencil part.

I confirmed this via glReadBuffer(GL_DEPTH_STENCIL_ATTACHMENT) and glReadPixels. All integers read show up as 0xffffff00, directly after clearing them via glClear and a black clearcolor and also after the screen is drawn full of stuff.

Drawing to and reading from the rest of the attached textures (four color buffers for now) works fine.

I have to note that I'm not actually manually drawing anything to the depth-stencil attachment in the fragment shader when rendering - I'm assuming OpenGL automatically draws depth and stencil values to the correct buffer.

What could be the cause of these incorrect values showing up when reading from the texture?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I have the same problem with preview of depth textures, but not from code level, but from CodeXL from AMD. I'm not 100% sure but maybe they try to draw textures in the same way as you.

My solution to see the content of depth attachment is using this simple shader:

// vertex
#version 330
in vec4 vertex;

void main(){
    gl_Position=vertex;
}

// fragment
#version 330
out vec4 frag;
uniform vec2 screen; // screen size

uniform texture2D depth;
void main(){
    frag=texture2D(depth,gl_FragCoord/screen);
}

to 'depth' pass your _depthStencilTexture and use this shader when rendering fullscreen quad. You can also add linearization to see better what the content of depth attachmnet is.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .