gpt4 book ai didi

c++ - OpenGL 如何使用多重采样 2d 纹理将深度缓冲区附加到帧缓冲区

转载 作者:行者123 更新时间:2023-11-28 05:59:17 26 4
gpt4 key购买 nike

当我使用 GL_TEXTURE_2D_MULTISAMPLE 时,如何将深度缓冲区附加到我的帧缓冲区对象。以下代码中的 glCheckFramebufferStatus(msaa_fbo) 返回 0。从文档来看,这似乎意味着 msaa_fba 不是帧缓冲区,但它是从 glGenFramebuffers(1, &msaa_fbo);

Additionally, if an error occurs, zero is returned. GL_INVALID_ENUM is generated if target is not GL_DRAW_FRAMEBUFFER, GL_READ_FRAMEBUFFER or GL_FRAMEBUFFER.

错误是 1280,我认为这意味着 GL_INVALID_ENUM。

如果我删除深度缓冲区附件,程序将运行并呈现(尽管没有进行深度测试)。然后运行时错误仍然存​​在。包含深度附件后,每帧后都会出现一个错误 (1286),即 INVALID_FRAMEBUFFER。我不知道如何从这里继续。我看过的一些例子有些相同,但似乎有效。

glGenTextures(1, &render_target_texture);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, render_target_texture);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, NUM_SAMPLES, GL_RGBA8, width, height, false);

glGenFramebuffers(1, &msaa_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, msaa_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, render_target_texture, 0);

glGenRenderbuffers(1, &depth_render_buffer);
glBindRenderbuffer(GL_RENDERBUFFER, depth_render_buffer);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, NUM_SAMPLES, GL_DEPTH_COMPONENT24, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_render_buffer);

GLenum status = glCheckFramebufferStatus(msaa_fbo);

大部分代码来自this .

编辑

状态检查错误,应该是 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);。现在当我不包括深度时没有错误。当我包含深度时,我现在收到此错误:GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE

编辑 2

文档声称当 GL_TEXTURE_SAMPLES 和 GL_RENDERBUFFER:SAMPLES 不匹配时会发生这种情况。

GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE is returned if the value of GL_RENDERBUFFER_SAMPLES is not the same for all attached renderbuffers; if the value of GL_TEXTURE_SAMPLES is the not same for all attached textures; or, if the attached images are a mix of renderbuffers and textures, the value of GL_RENDERBUFFER_SAMPLES does not match the value of GL_TEXTURE_SAMPLES.

但他们确实如此!

我已经这样测试过它们:

std::cout << "GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE" << std::endl;
GLsizei gts, grs;
glGetTexLevelParameteriv(GL_TEXTURE_2D_MULTISAMPLE, 0, GL_TEXTURE_SAMPLES, &gts);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &grs);
std::cout << "GL_TEXTURE_SAMPLES: " << gts << std::endl;
std::cout << "GL_RENDERBUFFER_SAMPLES: " << grs << std::endl;

输出是:

GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
GL_TEXTURE_SAMPLES: 8
GL_RENDERBUFFER_SAMPLES: 8

编辑 3

通过使用两个纹理而不是像这样的纹理和渲染缓冲区来解决这个问题:

glGenFramebuffers(1, &msaa_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, msaa_fbo);

glGenTextures(1, &render_texture);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, render_texture);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, NUM_SAMPLES, GL_RGBA8, width, height, false);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, render_texture, 0);

glGenTextures(1, &depth_texture);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, depth_texture);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, NUM_SAMPLES, GL_DEPTH_COMPONENT, width, height, false);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, depth_texture, 0);

我仍然对最初的实现有什么问题很感兴趣,所以问题仍然存在。

最佳答案

如果将纹理与渲染缓冲区混合使用,则需要为纹理使用固定的采样位置。根据规范,在“帧缓冲区完整性”部分:

The value of TEXTURE_FIXED_SAMPLE_LOCATIONS is the same for all attached textures; and, if the attached images are a mix of renderbuffers and textures, the value of TEXTURE_FIXED_SAMPLE_LOCATIONS must be TRUE for all attached textures.

{FRAMEBUFFER_INCOMPLETE_MULTISAMPLE}

为避免这种错误情况,您需要将设置纹理存储的调用更改为:

glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE,
NUM_SAMPLES, GL_RGBA8, width, height, GL_TRUE);

关于c++ - OpenGL 如何使用多重采样 2d 纹理将深度缓冲区附加到帧缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33587682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com