GLSL共享深度缓冲似乎不起作用

时间:2014-03-19 09:43:59

标签: opengl buffer glsl depth fbo

当我正在进行平铺前向着色时,我正在尝试为两个FBOS使用共享深度纹理。我正在做的是填充深度缓冲区的prez传递,然后我运行一个计算着色器只是做一些计算器,然后我运行我的前向着色器,我正在尝试渲染到共享深度缓冲区的FBO有一个颜色缓冲区。问题是屏幕变黑了。以下是我对两个FBO的初衷:

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);
}

这是我的Z前传:

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);

前进传球

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);

我尝试在片段着色器中写入颜色缓冲区的方法只是使用

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

更新

这是完整的预通过着色器     #version 430

layout(location = 0)in vec3 position;

uniform mat4 modelViewProjection;

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

#version 430


void main()
{

}

我如何绑定预传递缓冲区

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

ANSWER

问题显然是我的显卡没有14个渲染目标的支持所以当我将纹理从GL_COLOR_ATTACHMENT14更改为GL_COLOR_ATTACHMENT1时,一切都按预期工作

1 个答案:

答案 0 :(得分:0)

首先,你是对的    layout(location = 0)out vec3 finalColor;

0代表drawBuffer []数组中的索引。抱歉错误......

更新

我看不到你在哪里绑定你的附件插槽以进行预传

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

同样在预先通过着色器中,您不会根据DrawBuffer数组绑定所有附件 你的片段着色器是空的吗?

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

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

此外,如果您打算使用深度纹理作为着色器的输入,您应该拥有, 相应的制服,我错了吗?