在framebuffer

时间:2016-05-25 09:49:35

标签: c++ opengl

我正在尝试同时渲染多重采样和单采样纹理(drawcall)。 我为什么要这样?因为多重采样纹理使用抗锯齿,而单采样纹理用于拾色。 两种纹理(nRenderTargetTexture和nColorPickingTexture)具有相同的大小。 如果我运行下面的代码,它在GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE情况下失败。 是否可以同时渲染为多重采样和单采样纹理?

顺便说一下:我也在使用glGetError(),但它返回GL_NO_ERROR

// Enable multisampling
glEnable(GL_MULTISAMPLE);

glGenFramebuffers(1, pFramebufferID);
glBindFramebuffer(GL_FRAMEBUFFER, *pFramebufferID);

// Create color render buffer
glGenRenderbuffers(1, &nColorBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, nColorBuffer);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_RGBA8, nWidth, nHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, nColorBuffer);

// Create depth render buffer
glGenRenderbuffers(1, &nDepthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, nDepthBuffer);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8, nWidth, nHeight);

//Attach the multisampled textures
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, nDepthBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, nDepthBuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, nRenderTargetTexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, nColorPickingTexture, 0);
const GLenum draw_buffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};

glDrawBuffers(2, draw_buffers);

GLenum eFramebufferOk = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);

bool bResult = false;
switch (eFramebufferOk)
{
    case GL_FRAMEBUFFER_UNDEFINED:
    {
        DebugAssertOnce(CREATE_FRAME_BUFFER_FAILED);
    }
    break;
    case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
    {
        DebugAssertOnce(CREATE_FRAME_BUFFER_FAILED);
    }
    break;
    case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
    {
        DebugAssertOnce(CREATE_FRAME_BUFFER_FAILED);
    }
    break;
    case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
    {
        DebugAssertOnce(CREATE_FRAME_BUFFER_FAILED);
    }
    break;
    case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
    {
        DebugAssertOnce(CREATE_FRAME_BUFFER_FAILED);
    }
    break;
    case GL_FRAMEBUFFER_UNSUPPORTED:
    {
        DebugAssertOnce(CREATE_FRAME_BUFFER_FAILED);
    }
    break;
    case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
    {
        DebugAssertOnce(CREATE_FRAME_BUFFER_FAILED);
    }
    break;
    case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS:
    {
        DebugAssertOnce(CREATE_FRAME_BUFFER_FAILED);
    }
    break;
    case GL_FRAMEBUFFER_COMPLETE:
    {
        bResult = true;
    }
    break;
    default:
    {
        DebugAssertOnce(CREATE_FRAME_BUFFER_FAILED);
    }
    break;
}

非常感谢!

2 个答案:

答案 0 :(得分:1)

  

是否可以同时渲染为多重采样和单采样纹理?

没有。附加到帧缓冲区的所有图像必须具有相同的样本计数。否则,您获得GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE

答案 1 :(得分:0)

btw:使用抗锯齿并使用相同的着色器调用进行拾取,我使用多重采样纹理。 在我渲染场景后,我做了第二个渲染(屏幕上有两个三角形)并在我使用的碎片整理器内部:

texelFetch(pickingTexture,ivec2(position),0); //0 sample
它给了我一个不反抗的挑选纹理。 我写这篇文章是因为也许有人在尝试同样的事情。