glBlitFramebuffer 无效操作

glBlitFramebuffer Invalid Operation

本文关键字:操作 无效 glBlitFramebuffer      更新时间:2023-10-16

我一直在弄乱帧缓冲区并渲染到纹理,我遇到了需要将它们块状化。同样,在某些机器上,我在glBlitFramebuffer呼叫后立即收到GL_INVALID_OPERATION。绑定到帧缓冲的每个纹理都以完全相同的方式设置,所有相同的大小和参数。此外,当我尝试将整个纹理(以前成功渲染到)到另一个帧缓冲区时,只有要写入的目标"矩形"小于要读取的矩形(例如,当我想将其块传输到屏幕的四分之一时),它也抛出了一个GL_INVALID_OPERATION

编辑: 实际上,每当要读取和绘制的矩形具有不同的大小时,它总是会抛出错误,所以我无法快速处理不同大小的纹理,或者相同大小但大小不同的"渲染"区域......?

每次我到手动生成的帧缓冲时,状态都会通过glCheckFramebufferStatus进行检查,并且它总是返回GL_FRAMEBUFFER_COMPLETE .

-

有史以来最大的截图-,请参阅下面的较短的"源代码",显然有几个C++错误并且不完整,但它仅适用于GL调用

当我调用视口的最后一个方法(视口::blit)并将屏幕帧缓冲作为目标(通过传递 NULL)时,会发生 OpenGL 错误。它首先设置自己的帧缓冲区的读取缓冲区(已经设置了绘制缓冲区),然后调用 RenderTarget::blit 调用 glBlitFramebuffer 。在 blit 方法中,它绑定了两个缓冲区,您可以看到它在那里调用glCheckFramebufferStatus,这不会返回错误。

我一直在一遍又一遍地阅读这篇文章,但我似乎找不到导致它的错误。当我使用颜色缓冲区时,我使用 GL_LINEAR ,否则我使用 GL_NEAREST 所有颜色缓冲区都使用 GL_RGB32F 作为内部格式,深度缓冲区(我从不 blit)使用GL_DEPTH_COMPONENT32F

EDIT,一个较短的例子,只是获取了所有 GL 调用并填充了我使用的参数

glBindFramebuffer(GL_READ_FRAMEBUFFER, _GL_Framebuffer);
glReadBuffer(GL_COLOR_ATTACHMENT0 + index);
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
// OpenGL error check, does not return an error
glBindFramebuffer(GL_READ_FRAMEBUFFER, _GL_Framebuffer);
GLenum status = glCheckFramebufferStatus(GL_READ_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
{
    // Some error checking, fortunately status always turns out to be complete
}
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(0, 0, screenWidth, screenHeight, 0, 0, screenWidth, screenHeight, target, (target == GL_COLOR_BUFFER_BIT) ? GL_LINEAR : GL_NEAREST);
// If the source/destination read/draw rectangles are different in size, GL_INVALID_OPERATION is cought here

以及帧缓冲/纹理创建:

glGenFramebuffer(1, &_GL_Framebuffer);
glGenTextures(1, &_GL_ZBuffer);
glBindTexture(GL_TEXTURE_2D, _GL_ZBuffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, screenWidth, screenHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, _GL_Framebuffer);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT + 0, _GL_ZBuffer, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
int writeIndices[BUFFER_COUNT];
for(unsigned int i = 0; i < BUFFER_COUNT; ++i)
{
    writeIndices[i] = i;
    glGenTextures(1, &_GL_Texture);
    glBindTexture(GL_TEXTURE_2D, _GL_Texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, screenWidth, screenHeight, 0, GL_RGB, GL_FLOAT, 0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glBindTexture(GL_TEXTURE_2D, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, _GL_Framebuffer);
    glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, _GL_Texture, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    // In the actual code each texture is obviously saved in an object
}
GLenum *enums = new GLenum[BUFFER_COUNT];
for(unsigned int i = 0; i < BUFFER_COUNT; ++i)
{
    // Get index and validate
    int index = *(indices + i); // indices = writeIndices
    if(index < 0 || index >= maxAttachments)
    {
        delete[] enums;
        return false;
    }
    // Set index
    enums[i] = GL_COLOR_ATTACHMENT0 + index;
}
// Set indices
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _GL_Framebuffer);
glDrawBuffers(BUFFER_COUNT, enums);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
delete[] enums;
// OpenGL error check, no errors

经过一番仔细阅读,我发现多重采样的差异是问题所在。"主"FBO 是由 SFML 设置的,因此只需将启动时的抗锯齿级别设置为 0,问题就部分解决了。

如果绘制/读取矩形的大小不相等,它现在会变快,但它在一些应该工作的机器上不断崩溃。