How to render color and depth in multisample texture?

时间:2015-05-24 20:22:00

标签: c++ opengl framebuffer antialiasing multisampling

In order to implement "depth-peeling", I render my OpenGL scene in to a series of framebuffers each equipped with a rgba color texture and depth texture. This works fine if I don't care about anti-aliasing. If I do, then it seems the correct thing to do is enable GL_MULTISAMPLING and use a GL_TEXTURE_2D_MULTISAMPLE instead of GL_TEXTURE_2D. But I'm confused about which other calls need to be replaced.

In particular, how should I adapt my framebuffer construction to use glTexImage2DMultisample instead of glTexImage2D?

Do I need to change the calls to glFramebufferTexture2D beyond using GL_TEXTURE_2D_MULTISAMPLE instead of GL_TEXTURE_2D?

If I'm rendering both color and depth into textures, do I need to make a call to glRenderbufferStorageMultisample?

Finally, is there some glBlit* that I need to do in addition to setting up textures for the framebuffer to render into?

There are many related questions on this topic, but none of the solutions I found seem to point to a canonical tutorial or clear example putting all these together.

1 个答案:

答案 0 :(得分:0)

虽然我只使用渲染缓冲区的多重采样FBO渲染,而不是纹理,但以下是我的理解。

  

我是否需要将使用glFramebufferTexture2D而不是GL_TEXTURE_2D_MULTISAMPLE的电话更改为GL_TEXTURE_2D

不,这就是你所需要的。您使用glTexImage2DMultisample()创建纹理,然后使用GL_TEXTURE_2D_MULTISAMPLE作为第3个参数将其附加到glFramebufferTexture2D()。唯一的限制是级别(第五个参数)必须为0。

  

如果我在纹理中渲染颜色和深度,我是否需要拨打glRenderbufferStorageMultisample

是。如果将深度缓冲区附加到同一FBO,则需要使用多重采样渲染缓冲区,其中样本数与颜色缓冲区相同。因此,您使用glRenderbufferStorageMultisample()创建深度渲染缓冲区,并传入用于颜色缓冲区的相同样本计数。

  

最后,除了为帧缓冲区设置纹理之外,我还需要做一些glBlit*吗?

不用于渲染到帧缓冲区。完成渲染后,您有几个选择:

  1. 您可以将多重采样纹理下采样(解析)为常规纹理,然后使用常规纹理进行后续渲染。要解析多重采样纹理,您可以使用glBlitFramebuffer(),其中多重采样纹理附加到GL_READ_FRAMEBUFFER,常规纹理附加到GL_DRAW_FRAMEBUFFER

  2. 您可以将多重采样纹理用于后续渲染。您需要在着色器代码中使用sampler2DMS类型的采样器,并使用相应的采样函数。

  3. 对于选项1,我真的没有理由使用多重采样纹理。您也可以使用多样本渲染缓冲区,它稍微容易使用,并且应该至少同样有效。为此,您为颜色附件创建渲染缓冲区,并使用glRenderbufferStorageMultisample()分配它,非常类似于深度缓冲区所需的内容。