Skip to main content
GameDev.net gamedev.net
🔒 Locked

Write into multi depth buffer

Started by zenux May 6, 2012 at 2:24 PM 10 replies 2.2k views
Original Post
zenux
zenux
Hello,

I need to create several depth buffer (for each vertex emitted by a geometry shader) on my FBO.
Here the source code:

glBindFramebuffer(GL_FRAMEBUFFER, fboID);
//depth buffer 1
glTexImage2D( GL_TEXTURE_2D, 0, GL_R32F, shadowMapResolution, shadowMapResolution, 0, GL_RED, GL_FLOAT, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);

//depth buffer2
//....


In fragment shader, I have:

layout (location = 0) out float fragDepth;
void main()
{
fragDepth = gl_FragCoord.z / gl_FragCoord.w;
}


Problem: the depth value is well written in my texture but the value is not right. Indeed, it seems that the value has not been affected by the depth test function: glDepthFunc(GL_LESS).

What can I do to solve this problem ?
Thank you in advance.
zenux
zenux
Thanks for your reply.
I have tried to execute "glDepthMask(GL_TRUE);" and "glEnable(GL_DEPTH_TEST);" just before draw objects to be sure but I still have the problem.
FXACE
FXACE
When I worked with geometry shaders (with classic GLSL) I had a problem with gl_FragCoord (which is carrying nothing on fragment shader, always returns vec4(0.0)). I had used built-in varying variable (gl_TexCoord[]) to store the same value which gl_FragCoord usually has ("gl_Position * 0.5 + 0.5",ignoring x and y screen coordinates in pixels), and this solved my problem. Maybe this is point on modern GLSL too.

Best wishes, FXACE.
dpadam450
dpadam450
You don't need to write gl_FragDepth if you are going to write it exactly how you have it. GL will write it for you before the shader so it can perform early depth test rejection and not run the pixel shader.
NBA2K, Madden, Maneater, Killing Floor, Sims 
zenux
zenux
@FXACE: sorry but I don't understand your solution. First, I think the built-in variable "gl_TexCoord" doesn't exist anymore since OpenGL 3.0, no ? Then, the variable gl_Position doesn't contain the right value because OpenGL has not done the depth test yet in vertex/geometry shader, no ?

@dpadam450: I don't write gl_FragDepth. I read the value.
FXACE
FXACE
gl_TexCoord and some other stuff are deprecated since GLSL v1.5...

I mean this:

[geometry shader]
uniform mat4 proj; // your projection matrix
uniform mat4 mv; // your modelview matrix
out vec4 fragCoord;
void main()
{
....
// for each incoming vertex (from vertex shader)
vec4 glposition = proj * (mv * vs_vertex); // this can be done in vertex shader
// vs_vertex - vec4
glposition /= glposition.w;
fragCoord = glposition * 0.5 + 0.5; // move from [-1..1] to [0..1]
gl_Position = glposition;
EmitVertex();
...
EndPrimitive();
}
[fragment shader]
in vec4 fragCoord;
....
void main()
{
// TODO with fragCoord
}

Well, as I said, I working with classic GLSL because of my old gpu, so, this code is need to be checked by someone who is working with modern gpus.
My point was "do not use gl_FragCoord with geometry shaders"...
Can you show us the screen-shot?
Best wishes, FXACE.
zenux
zenux
Thanks FXACE for your help.
I have try your source code but it's not working as I expected.

I get exactly the same value that gl_FragCoord.z: no depth test (GL_LESS) are applied on this value
FXACE
FXACE
Upload the screen of your shadow map (maybe that would be easier to determine the kind of bug).

Another thing:
What object did you attached to your FBO as GL_DEPTH_ATTACHMENT? If nothing, so your FBO doesn't have depth buffer.

Best wishes, FXACE.
zenux
zenux
Sorry, I haven't good screenshot of my shadow map. I have seen after debug session that the last object drawn is always visible on my shadow maps no matter his depth value.
If I sort my objects by depth value (from the further to the nearest): I haven't problem anymore.

No, I haven't GL_DEPTH_ATTACHMENT on my FBO for two reasons:
* I have already several depth buffers (see my first post).
* What OpenGL will write in GL_DEPTH_ATTACHMENT ? A common depth buffer will be wrote for all vertex emitted by the geometry shader ? I need different depth buffer: http://http.developer.nvidia.com/GPUGems3/elementLinks/10fig11.jpg
FXACE
FXACE

* I have already several depth buffers (see my first post).

Anything you had attached to GL_COLOR_ATTACHMENTi are used for your purposes (but for OpenGL they are notified as color buffer) and they are not reacting with depth buffer...

* What OpenGL will write in GL_DEPTH_ATTACHMENT ? A common depth buffer will be wrote for all vertex emitted by the geometry shader ?

If your FBO doesn't have something attached into GL_DEPTH_ATTACHMENT OpenGL can't do any operations with depth buffer because it doesn't exist. A common (screen) depth buffer will not be touched till you are working with your FBO.

So, you need to attach something into GL_DEPTH_ATTACHMENT (texture or render buffer) and do it (what you want) in multi-pass or...
you can attach large depth texture (internal format: GL_DEPTH_COMPONENT) which would contain a couple of textures (which you planned to make them with separated "depth" textures):
+---+
|012|
|345|
+---+
each number - area of depth buffer which is used for something, for your purposes (in fragment shader you need to do custom clipping).

Best wishes, FXACE.
zenux
zenux
Ok, thank you.
I've just seen that it's maybe possible to have something better with texture 2d array and gl_Layer. I will try it.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.