尝试在延迟着色之上实现抗锯齿,我尝试使用多采样渲染缓冲区,然后使用缓冲区 blit 传递解析样本。
与传统的延迟着色一样,我使用专用着色器渲染场景,发出 3 种颜色输出:
- 职位
- 法线
- 漫反射和镜面反射
然后将它们用于照明计算通道,从而产生最终的场景纹理
使用简单着色器将场景纹理渲染到全屏四边形屏幕上
正如您可能猜到的那样,屏幕上的 MSAA 在渲染到屏幕时不会应用于场景纹理的内容:为了实现抗锯齿,我因此在步骤 1) 中选择使用多采样渲染缓冲区并引入了一个额外的步骤1.1) 用于分辨率。当然,多重采样仅对彩色图是必要的/有用的,而不是其他 2 个图。
我的问题和问题是,显然,只能为相同类型的附件定义具有多个渲染缓冲区/颜色附件的帧缓冲区;这意味着如果一个附件是多采样的,那么所有其他附件都必须是。
这成为解析期间位置和法线缓冲区的问题,因为抗锯齿会影响几何图形和照明。
- 我对帧缓冲区附件的理解是否有效?
- 有没有办法解决它,以便在 Diffuse&Specular 贴图上仍然进行多重采样,但不影响其他贴图?
// Create the frame buffer for deferred shading: 3 color attachments and a depth buffer
glGenFramebuffers(1, &gBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
{
// - Position color buffer
glGenRenderbuffers(1, &gPosition);
glBindRenderbuffer(GL_RENDERBUFFER, gPosition);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 8, GL_RGBA16F, w, h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, gPosition);
// - Normal color buffer
glGenRenderbuffers(1, &gNormal);
glBindRenderbuffer(GL_RENDERBUFFER, gNormal);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 8, GL_RGBA16F, w, h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_RENDERBUFFER, gNormal);
// - Color + specular color buffer
glGenRenderbuffers(1, &gColorSpec);
glBindRenderbuffer(GL_RENDERBUFFER, gColorSpec);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 8, GL_RGBA, w, h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_RENDERBUFFER, gColorSpec);
unsigned int attachments[3] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
glDrawBuffers(3, attachments);
// - Generate the depth buffer for rendering
glGenRenderbuffers(1, &sceneDepth);
glBindRenderbuffer(GL_RENDERBUFFER, sceneDepth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, w, h);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 8, GL_DEPTH_COMPONENT, w, h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, sceneDepth);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Create a frame buffer with 3 attachments for sample resolution
glGenFramebuffers(1, &gFrameRes);
glBindFramebuffer(GL_FRAMEBUFFER, gFrameRes);
{
glGenTextures(1, &gPositionRes);
glBindTexture(GL_TEXTURE_2D, gPositionRes);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, w, h, 0, GL_RGB, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gPositionRes, 0);
glGenTextures(1, &gNormalRes);
glBindTexture(GL_TEXTURE_2D, gNormalRes);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, w, h, 0, GL_RGBA, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, gNormalRes, 0);
glGenTextures(1, &gColorSpecRes);
glBindTexture(GL_TEXTURE_2D, gColorSpecRes);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, gColorSpecRes, 0);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// ...
//
// Once the scene is rendered, resolve:
glBindFramebuffer(GL_READ_FRAMEBUFFER, gBuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, gFrameRes);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glBlitFramebuffer(0, 0, sw, sh, 0, 0, sw, sh, GL_COLOR_BUFFER_BIT, GL_LINEAR);
glReadBuffer(GL_COLOR_ATTACHMENT1);
glDrawBuffer(GL_COLOR_ATTACHMENT1);
glBlitFramebuffer(0, 0, sw, sh, 0, 0, sw, sh, GL_COLOR_BUFFER_BIT, GL_LINEAR);
glReadBuffer(GL_COLOR_ATTACHMENT2);
glDrawBuffer(GL_COLOR_ATTACHMENT2);
glBlitFramebuffer(0, 0, sw, sh, 0, 0, sw, sh, GL_COLOR_BUFFER_BIT, GL_LINEAR);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
上面代码示例的结果是,被照亮的对象的边缘显示出不适当的暗/黑或亮/白像素伪影,可能是因为它们的位置和/或法线在此过程中已被更改。