Jump to content
  • Advertisement
Sign in to follow this  
jay8ee

GLSL access depth buffer from shader

This topic is 3047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I want to read the depth buffer of my FBO in one of my shaders so I can run some DOF equation. I am having trouble reading the buffer however. I setup the depth texture with GL_DEPTH_STENCIL_EXT so I have the stencil value to play with too, but thats another issue for another day.
glBindTexture(GL_TEXTURE_2D, textures[depth]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8_EXT, wWidth, wHeight, 0, GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT, 0);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );

then..

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, textures[depth], 0);



I don't how I access just the depth component of the texture from a shader (if it is possible at all?). Can I pass the the depth texture as a sampler? If anyone can help, then thanks.

Share this post


Link to post
Share on other sites
Advertisement
For sure you can just bind the depth as a sampler. Just make sure you set your depth comparison to NONE.

Share this post


Link to post
Share on other sites
Huh? I don't understand.. If I set the texture as a sampler, how do i read of the first 24bits and leave out the stencil value?

Share this post


Link to post
Share on other sites
Quote:
Original post by idinev
The texture2D() automatically gives you values in the [0;1] range. The value is (z/w+1)/2
To expand on that answer, you just sample the red component of the depth buffer, and you get the full 24 bit value as a float.
float depth = texture2D( depthBuf, texCoords ).r;

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
Quote:
Original post by idinev
The texture2D() automatically gives you values in the [0;1] range. The value is (z/w+1)/2
To expand on that answer, you just sample the red component of the depth buffer, and you get the full 24 bit value as a float.
float depth = texture2D( depthBuf, texCoords ).r;


Awesome, I'm just on my way to work so i'll give this a blast later. If this is the case as you say, can you access the stencil value with .b?

Share this post


Link to post
Share on other sites
float depth = texture2D(depthImg, gl_TexCoord[0].xy).r;

this doesn't work after all. Always seems to equate to 1 :S. I can't work it out. Here is what I got to try and just output the depth as colour...


uniform sampler2D depthImg;

void main()
{
float depth = texture2D(depthImg, gl_TexCoord[0].xy).r;

gl_FragColor = vec4(depth, depth, depth, 1.0);
}




and in the c++ code...


glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[depth]);
shaderDepthColour.UseProgram();
RenderFullscreenQuad();



seems like I am only getting a white screen though :(


Share this post


Link to post
Share on other sites
Yes, it "seems"

Try this in your shader, to convert to linear:

float LinearizeDepth(float zoverw){
float n = 1.0; // camera z near
float f = 20000.0; // camera z far
return (2.0 * n) / (f + n - zoverw * (f - n));
}


uniform sampler2D depthImg;

void main()
{
float depth = texture2D(depthImg, gl_TexCoord[0].xy).r;
depth = LinearizeDepth(depth)*77;

gl_FragColor = vec4(depth, depth, depth, 1.0);
}




Share this post


Link to post
Share on other sites
Quote:
Original post by idinev
Yes, it "seems"

Try this in your shader, to convert to linear:
*** Source Snippet Removed ***


This doesn't work either... is it potentially because I am using the wrong format for my texture?

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8_EXT, wWidth, wHeight, 0, GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT, 0);

perhaps when using float data type in the shader the value is getting clamped because it is a 24 uint in the texture?

Share this post


Link to post
Share on other sites
Hopefully you simply didn't change the "n" and "f" vars to match your persp-proj values. Also toy with the "77" value .

Or check if really the depth==1.0 :

float depth = texture2D(depthImg, gl_TexCoord[0].xy).r;
depth = depth==1.0 ? 0 : 1;
gl_FragColor = vec4(depth, depth, depth, 1.0);



The other stuff is: glTexParami(gl_texture_compare_mode) - has to be gl_none, otherwise the sampler will be doing shadowmap comparison (which will result in the exact problem you currently describe).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!