多重采样渲染到纹理

时间:2012-06-01 11:46:25

标签: ios opengl-es textures fbo msaa

我正在使用以下架构:

  • iOS上的OpenGL ES 2
  • 具有相同ShareGroup的两个EAGL上下文
  • 两个线程(服务器,客户端=主线程);服务器将东西渲染为纹理,客户端使用简单的纹理四边形显示纹理。

服务器线程的其他详细信息(工作代码)

初始化期间创建了一个fbo:

void init(void) {
    glGenFramebuffer(1, &fbo);
}

服务器的渲染循环看起来大致如下:

GLuint loop(void) {
    glBindFrameBuffer(GL_FRAMEBUFFER, fbo);
    glViewport(0,0,width,height);

    GLuint tex;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);

    // Framebuffer completeness check omitted

    glClear(GL_COLOR_BUFFER_BIT);

    // actual drawing code omitted

    // the drawing code bound other textures, so..
    glBindTexture(GL_TEXTURE_2D, tex);
    glGenerateMipmap(GL_TEXTURE_2D);

    glBindTexture(GL_TEXTURE_2D, GL_NONE);
    glFlush();

    return tex;
}

到目前为止,这一切都很好。

新(错误)代码

现在我想将多重采样添加到服务器线程,使用GL_APPLE_framebuffer_multisample扩展并修改初始化代码,如下所示:

void init(void) {
    glGenFramebuffer(1, &resolve_fbo);
    glGenFramebuffers(1, &sample_fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, sample_fbo);
    glGenRenderbuffers(1, &sample_colorRenderbuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, sample_colorRenderbuffer);
    glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, 4, GL_RGBA8_OES, width, height);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, sample_colorRenderbuffer);

    // Framebuffer completeness check (sample_fbo) omitted

    glBindRenderbuffer(GL_RENDERBUFFER, GL_NONE);
    glBindFramebuffer(GL_FRAMEBUFFER, GL_NONE);
}

主循环已更改为:

GLuint loop(void) {
    glBindFrameBuffer(GL_FRAMEBUFFER, sample_fbo);
    glViewport(0,0,width,height);

    GLuint tex;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);

    glClear(GL_COLOR_BUFFER_BIT);

    // actual drawing code omitted

    glBindFramebuffer(GL_FRAMEBUFFER, resolve_fbo);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);

    // Framebuffer completeness check (resolve_fbo) omitted

    // resolve multisampling
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER_APPLE, resolve_fbo);
    glBindFramebuffer(GL_READ_FRAMEBUFFER_APPLE, sample_fbo);
    glResolveMultisampleFramebufferAPPLE();

    // the drawing code bound other textures, so..
    glBindTexture(GL_TEXTURE_2D, tex);
    glGenerateMipmap(GL_TEXTURE_2D);

    glBindTexture(GL_TEXTURE_2D, GL_NONE);
    glFlush();

    return tex;
}

我现在看到的是纹理包含来自多个loop()调用的数据,这些数据混合在一起。我想我要么错过了某种类型的'unbind',或者可能是glFinish()调用(我以前在不同的点上遇到过这样的问题,我用glTexImage2D()设置纹理数据并正确使用它之后 - 需要glFinish()调用才能强制更新纹理。

但是,在绘图代码之后插入glFinish()并没有改变任何内容..

1 个答案:

答案 0 :(得分:1)

哦,没关系,这么愚蠢的错误。我省略了loop()方法实际上包含for循环并呈现多个纹理的细节,错误是我只在此循环之前绑定了样本fbo,所以在第一次运行之后,解析fbo被绑定了..

在循环内移动fbo绑定可以解决问题。

无论如何,感谢@所有读者并抱歉浪费你的时间:)