0

I'm trying to use a shared depth texture for two FBOS when I'm doing tiled forward shading. What I'm doing is a prez pass that fills a depth buffer, then I run a compute shader just to do some calculatons and then I run my forward shader where I'm trying to render to an FBO that shares that depth buffer and also has a color buffer. Problem is the screen goes black. Here's how I initalize the two FBOs:

void MainWindow::initalizeTiledForwardPrePassBuffer(int width, int height)
{
glGenFramebuffers(1,&m_prePassBuffer);
glBindFramebuffer(GL_FRAMEBUFFER,m_prePassBuffer);

glGenTextures(1,&m_depthTexture);
glGenTextures(1,&m_finalTexture);

// depth
glBindTexture(GL_TEXTURE_2D, m_depthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthTexture, 0);


glBindTexture(GL_TEXTURE_2D, m_otherTexture);
///Used to be GL_RGBA only
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGB, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT5, GL_TEXTURE_2D, m_otherTexture, 0);

// final
glBindTexture(GL_TEXTURE_2D, m_finalTexture);
///Used to be GL_RGBA only
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGB, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT4, GL_TEXTURE_2D, m_finalTexture, 0);

GLenum Status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

if (Status != GL_FRAMEBUFFER_COMPLETE) {
    printf("FB error, status: 0x%x\n", Status);
}

glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
    void MainWindow::initializeTiledForwardColorBuffer(int width, int height)
    {
    glGenFramebuffers(1,&m_forwardColorBuffer);
    glBindFramebuffer(GL_FRAMEBUFFER,m_forwardColorBuffer);

glGenTextures(1,&m_forwardColorTexture);

glBindTexture(GL_TEXTURE_2D,m_forwardColorTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGB, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT14, GL_TEXTURE_2D, m_forwardColorTexture, 0);

// depth
glBindTexture(GL_TEXTURE_2D, m_depthTexture);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthTexture, 0);

GLenum Status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

if (Status != GL_FRAMEBUFFER_COMPLETE) {
    printf("FB error, status: 0x%x\n", Status);
}

glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

Here's my pre Z pass:

ShaderMan.useTiledForwardPrePass();

///bind the prepasbuffer for drawing
bindTiledForwardPrePassBuffer();

///enable depth test and clear the buffers and then render the scene
glDepthMask(GL_TRUE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);

//renderSceneToDepth();
renderOtherSceneToDepth();

glDepthMask(GL_FALSE);
glDisable(GL_DEPTH_TEST);

And the forward pass

ShaderMan.useForwardShader();
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_forwardColorBuffer);

    //glBindImageTexture(4, m_forwardColorTexture, 0, GL_FALSE, 0, GL_WRITE_ONLY,     GL_RGBA32F);
GLenum DrawBuffers[] = { 
    GL_COLOR_ATTACHMENT14
};
glDrawBuffers(ARRAY_SIZE_IN_ELEMENTS(DrawBuffers), DrawBuffers);

glDepthMask(GL_FALSE);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
//bindMSAABufferForWriting();
bindForForwardPass();
//renderSceneForward();
renderOtherSceneForward();

glDepthMask(GL_FALSE);
glDisable(GL_DEPTH_TEST);

glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindFramebuffer(GL_READ_FRAMEBUFFER,m_forwardColorBuffer);

glReadBuffer(GL_COLOR_ATTACHMENT14);
glBlitFramebuffer(0, 0, window_width, window_height,
                  0, 0, window_width, window_height, GL_COLOR_BUFFER_BIT, GL_LINEAR);

The way I try to write into the color buffer in the fragment shader is simply with

//Other code.....
layout(location = 0)out vec3 finalColor;
void main()
{
    //Other code...
    finalColor = vec3(color).xyz;
}

UPDATE

Here's the full pre pass shader #version 430

layout(location = 0)in vec3 position;

uniform mat4 modelViewProjection;

void main()
{
    gl_Position = modelViewProjection*vec4(position,1.0f);
}

#version 430


void main()
{

}

And how I bind the pre pass buffer

void MainWindow::bindTiledForwardPrePassBuffer()
{
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER,m_prePassBuffer);
}

ANSWER

The problem apparently was that my graphics card didn't have support for 14 render targets so when I changed the texture from GL_COLOR_ATTACHMENT14 to GL_COLOR_ATTACHMENT1 everything worked as expected

Johan
  • 121
  • 1
  • 11
  • In your forward pass are you binding the depth buffer as a texture whilst it is also attached to the FBO? – GuyRT Mar 19 '14 at 10:53
  • The only time I'm binding my depth texture is when I need to read from it during my compute shader pass which is in between the prepass and the forward pass. Then I use glActivateTextuer(GL_TEXTURE3) and glBindTexture(GL_TEXTURE_2D,m_depthTexture) but right after the compute shader is done I use glBindTexture(GL_TEXTURE_2D,0) which is right before the forward shader pass starts – Johan Mar 19 '14 at 11:49
  • By the way, you don't need to bind the depth texture again before you attach it to `m_forwardColorBuffer`, but as you say the depth buffer looks to be filled correctly, I don't think it's the problem. – GuyRT Mar 19 '14 at 13:07

1 Answers1

0

first, you were right with layout(location = 0)out vec3 finalColor;

0 stands for the index in the drawBuffer[] array. Sorry for the mistake...

Update:

I dont see where you bind your Attachment slots for the pre-pass

GLenum DrawBuffers[] = { 
    GL_COLOR_ATTACHMENT4 , GL_COLOR_ATTACHMENT5, GL_DEPTH_ATTACHMENT
};
glDrawBuffers(ARRAY_SIZE_IN_ELEMENTS(DrawBuffers), DrawBuffers);

Also in the pre pass shader, you dont bind all the Attachment according to DrawBuffer Array and is your fragment shader empty?

layout(location = 0)out vec3 color4;
layout(location = 1)out vec3 color5;
layout(location = 2)out float fragmentdepth;

void main()
{
    //output something per fragment!!
}

Also if you intend to use your depth texture as an input to your shader, you should have, a corresponding uniform, Am I wrong?

j-p
  • 1,622
  • 10
  • 18
  • Ah, thanks for the correction, I fixed it but it still doesn't seem to be working, so I must be doing something else wrong too. – Johan Mar 19 '14 at 10:51
  • could you show the TiledForwardPrePass shader and the bindTiledForwardPrePassBuffer please? – j-p Mar 19 '14 at 11:01
  • I added the code in the new update, I'm only binding the prepassbuffer as a drawbuffer and the prepass shader only writes everything to the depth – Johan Mar 19 '14 at 12:04
  • I tried running my prepass and then writing out the depth to an image in the compute shader which shows the depth buffer working so I think the prepass is doing what it's supposed to do. I think I explained a bit poorly, I don't intend to use the texture as an input to my shader, rather I want to run my prepass which is attached to one FBO and fill the depth buffer (just checked its working) and then in my normal forward shader pass I just want to render to another FBO which has the same depth texture attached and a color buffer. The forward pass doesnt write to the texture unfortunately – Johan Mar 19 '14 at 12:27
  • I'm not sure if it's relevent but output vec3 to a 4 component texture will set alpha to 1? try maybe to output a vec4 in your fragment shader – j-p Mar 19 '14 at 12:51
  • Is GL_DEPTH_ATTACHMENT even allowed in glDrawBuffers? I see nothing of it in the specs... – Bim Sep 22 '17 at 16:06
  • @Bim : you're right, there's only one depth attachment allowed, so it would be redundant. – j-p Oct 17 '17 at 14:09