Unable to get depth texture in a compute shader generated using fragment shader

67 Views Asked by At

I have an FBO with two textures attached to it. One of them is color buffer attachment and the other is depth buffer attachment. After rendering the scene with the frame buffer attached I properly get the color texture of the screen perfectly but when I try to render my depth buffer, its just black when I try to use it in my compute shader. Why?

Here is how I generate my Frame Buffer:

glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

glGenTextures(1, &ColorTexture);
glBindTexture(GL_TEXTURE_2D, ColorTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, WINDOW_WIDTH, WINDOW_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ColorTexture, 0);
glGenTextures(1, &DepthBuffer);
glBindTexture(GL_TEXTURE_2D, DepthBuffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, WINDOW_WIDTH, WINDOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, DepthBuffer, 0);

const GLenum buffers[]{ GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, buffers);

if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
    printf("FrameBuffer is incomplete!\n");
    exit(1);
}

glBindFramebuffer(GL_FRAMEBUFFER, 0);

Here is my compute shader:

#version 450

layout (local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
layout(rgba8, binding = 0) uniform readonly image2D ColorImage;
layout(r8, binding = 1) uniform readonly image2D DepthImage;
// layout(binding = 1) uniform smapler2D DepthImage;
layout(rgba8, binding = 2) uniform writeonly image2D OutputImage;

vec4 color;
ivec2 position;

void main()
{
    position = ivec2(gl_GlobalInvocationID.xy);
    color = imageLoad(DepthImage, position.xy);
    imageStore(OutputImage, position, vec4(vec3(color.r), 1.0));
}

My fragment shader:

#version 330

in vec4 Color;
in vec2 TexCoord;

uniform sampler2D uSampler;
uniform int textured;

layout (location = 0) out vec4 ColorTexture;
layout (location = 1) out float DepthTexture;

vec4 texColor;

void main()
{
    if(textured == 1)
    {
        ColorTexture = Color * texture2D(uSampler, TexCoord);
    } else {
        ColorTexture = Color;
    }
    DepthTexture = 0.5;//gl_FragCoord.z;
}

Here is how I read my output texture:

glGenTextures(1, &outputImageID);
glBindTexture(GL_TEXTURE_2D, outputImageID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, WINDOW_WIDTH, WINDOW_HEIGHT, 0, GL_RGBA, GL_FLOAT, nullptr);
glBindImageTexture(2, outputImageID, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
glUniform1i(glGetUniformLocation(ComputeShader->data.program, "OutputImage"), 2);

My rendering loop:

glBindFramebuffer(GL_FRAMEBUFFER, scene -> framebuffer);
// rendering each object here
glBindFramebuffer(GL_FRAMEBUFFER, 0);

sh = scene -> ComputeShader;
glUseProgram(sh -> data.program);
glEnable(GL_DEPTH_TEST);
glBindImageTexture(0,ColorTexture, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
glUniform1i(glGetUniformLocation(ComputeShader "ColorImage"), 0);
glBindImageTexture(1, DepthBuffer, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8);
glUniform1i(glGetUniformLocation(ComputeShader "DepthImage"), 1);
glDispatchCompute(800/16, 600/16, 1);
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
// then I bind my normal shader for object rendering and render a quad.

If I try to use my ColorImage in the compute shader it just outputs the same color of every corresponding pixels as expected but, for DepthImage its just black screen. Since I have put DepthTexture = 0.5, I was expecting a grey image. Before all of this I tried to do some shadow mapping and I had similar problems and I couldn't visualize my depth map.

this person also has similar problem but he fixed it

And when I followed that approach I still get black screen and not a grey image. So maybe there is some errors using the opengl functions or fbo?

Edit: I got my application working and I was not aware that glActivateTexture activates that corresponding samplers and we should set the sampler using glUniform1i("uSampler", unit);

0

There are 0 best solutions below