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