egl离屏渲染中的pbuffer vs fbo

Posted

技术标签:

【中文标题】egl离屏渲染中的pbuffer vs fbo【英文标题】:pbuffer vs fbo in egl offscreen rendering 【发布时间】:2015-03-02 19:20:18 【问题描述】:

我对 egl pbuffer 表面感到非常困惑。在我看来,pbuffer 表面是一个独立于平台的表面,就像 windows 表面或像素图表面一样。绘制到该表面的东西虽然不可见,但应该能够被读回。

这个问题的答案似乎证实了我的理解:

Difference from eglCreatePbufferSurface and eglCreatePixmapSurface with OpenGL ES(EGL)

但是,我的实验表明,除了使用 pbuffer 表面之外,我还需要创建一个 fbo 缓冲区。

这段代码似乎对我有用,它创建了一个 pbuffer 表面,然后是一个 fbo。

#include <GLES2/gl2.h>
#include <EGL/egl.h>

int main(int argc, char *argv[])

    EGLint ai32ContextAttribs[] =  EGL_CONTEXT_CLIENT_VERSION, 2,
                                                     EGL_NONE ;
    // Step 1 - Get the default display.
    EGLDisplay eglDisplay = eglGetDisplay((EGLNativeDisplayType)0);
    // Step 2 - Initialize EGL.
    eglInitialize(eglDisplay, 0, 0);
    // Step 3 - Make OpenGL ES the current API.
    eglBindAPI(EGL_OPENGL_ES_API);
    // Step 4 - Specify the required configuration attributes.
    EGLint pi32ConfigAttribs[5];
    pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;
    pi32ConfigAttribs[1] = EGL_WINDOW_BIT;
    pi32ConfigAttribs[2] = EGL_RENDERABLE_TYPE;
    pi32ConfigAttribs[3] = EGL_OPENGL_ES2_BIT;
    pi32ConfigAttribs[4] = EGL_NONE;
    // Step 5 - Find a config that matches all requirements.
    int iConfigs;
    EGLConfig eglConfig;
    eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs);

    if (iConfigs != 1) 
    
        printf("Error: eglChooseConfig(): config not found.\n");
        exit(-1);
    

    // Step 6 - Create a surface to draw to.
    EGLSurface eglSurface;
    eglSurface = eglCreatePbufferSurface(eglDisplay, eglConfig, NULL);

    // Step 7 - Create a context.
    EGLContext eglContext;
    eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, ai32ContextAttribs);

    // Step 8 - Bind the context to the current thread
    eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);

    GLuint fboId = 0;
    GLuint renderBufferWidth = 1920;
    GLuint renderBufferHeight = 1080;

    // Step 9 - create a framebuffer object
    glGenFramebuffers(1, &fboId);
    glBindFramebuffer(GL_FRAMEBUFFER, fboId);

    GLuint renderBuffer;
    glGenRenderbuffers(1, &renderBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);

    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB565, renderBufferWidth, renderBufferHeight);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderBuffer);

    GLuint depthRenderbuffer;
    glGenRenderbuffers(1, &depthRenderbuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, depthRenderbuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, renderBufferWidth, renderBufferHeight);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);

    // Step 10 - check FBO status
    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if(status != GL_FRAMEBUFFER_COMPLETE) 
    
        printf("Problem with OpenGL framebuffer after specifying color render buffer: \n%x\n", status);
     
    else 
    
        printf("FBO creation succedded\n");
    

    int size = 4 * renderBufferHeight * renderBufferWidth;
    unsigned char *data2 = new unsigned char[size];
    // Step 11 - clear the screen in Red and read it back
    glClearColor(1.0,0.0,0.0,1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    eglSwapBuffers( eglDisplay, eglSurface);
    glReadPixels(0,0,renderBufferWidth,renderBufferHeight,GL_RGBA, GL_UNSIGNED_BYTE, data2);

   ... save data2 to image ...

但是,如果我删除 fbo,并尝试直接绘制到 pbuffer,我会在调用 glClear() 函数后立即看到分段错误:

#include <GLES2/gl2.h>
#include <EGL/egl.h>

int main(int argc, char *argv[])

    EGLint ai32ContextAttribs[] =  EGL_CONTEXT_CLIENT_VERSION, 2,
                                                     EGL_NONE ;
    // Step 1 - Get the default display.
    EGLDisplay eglDisplay = eglGetDisplay((EGLNativeDisplayType)0);
    // Step 2 - Initialize EGL.
    eglInitialize(eglDisplay, 0, 0);
    // Step 3 - Make OpenGL ES the current API.
    eglBindAPI(EGL_OPENGL_ES_API);
    // Step 4 - Specify the required configuration attributes.
    EGLint pi32ConfigAttribs[5];
    pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;
    pi32ConfigAttribs[1] = EGL_WINDOW_BIT;
    pi32ConfigAttribs[2] = EGL_RENDERABLE_TYPE;
    pi32ConfigAttribs[3] = EGL_OPENGL_ES2_BIT;
    pi32ConfigAttribs[4] = EGL_NONE;
    // Step 5 - Find a config that matches all requirements.
    int iConfigs;
    EGLConfig eglConfig;
    eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs);

    if (iConfigs != 1) 
    
        printf("Error: eglChooseConfig(): config not found.\n");
        exit(-1);
    

     // Step 6 - Create a surface to draw to.
     EGLSurface eglSurface;
     eglSurface = eglCreatePbufferSurface(eglDisplay, eglConfig, NULL);

     // Step 7 - Create a context.
     EGLContext eglContext;
     eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, ai32ContextAttribs);

     // Step 8 - Bind the context to the current thread
     eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);

     int size = 4 * renderBufferHeight * renderBufferWidth;
     unsigned char *data2 = new unsigned char[size];
     // Step 11 - clear the screen in Red and read it back
     glClearColor(1.0,0.0,0.0,1.0);
     glClear(GL_COLOR_BUFFER_BIT);
     eglSwapBuffers(    eglDisplay, eglSurface);
     glReadPixels(0,0,renderBufferWidth,renderBufferHeight,GL_RGBA, GL_UNSIGNED_BYTE, data2);

     ... save data2 to image ...

我的环境是带有英特尔图形/台面的 ubuntu 14。

你知道我为什么看到分段错误吗? (我检查了eglcontext,似乎创建成功了。)你能确认pbuffer表面需要fbo吗?

编辑:正如 Reto 所指出的,我的问题是因为缺少属性。

在设置了这些属性之后,我能够让事情在 opengl es 2 上下文中工作。但是,我的桌面 opengl 上下文仍然存在问题。

使用桌面 opengl 上下文,我只能读取透明图像,而不是读取红色图像。这是我当前的代码:

#include <QCoreApplication>
#include <QDebug>
#include <QImage>
#include <GL/gl.h>
#include <EGL/egl.h>
#include <QElapsedTimer>

int main(int argc, char *argv[])

    // Step 1 - Get the default display.
    EGLDisplay eglDisplay = eglGetDisplay((EGLNativeDisplayType)0);

    // Step 2 - Initialize EGL.
    eglInitialize(eglDisplay, 0, 0);

    // Step 3 - Make OpenGL ES the current API.
    eglBindAPI(EGL_OPENGL_API);

    // Step 4 - Specify the required configuration attributes.
    EGLint pi32ConfigAttribs[5];
    pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;
    pi32ConfigAttribs[1] = EGL_PBUFFER_BIT;
    pi32ConfigAttribs[2] = EGL_RENDERABLE_TYPE;
    pi32ConfigAttribs[3] = EGL_OPENGL_BIT;
    pi32ConfigAttribs[4] = EGL_CONFORMANT;
    pi32ConfigAttribs[5] = EGL_OPENGL_BIT;
    pi32ConfigAttribs[6] = EGL_COLOR_BUFFER_TYPE;
    pi32ConfigAttribs[7] = EGL_RGB_BUFFER;
    pi32ConfigAttribs[8] = EGL_LUMINANCE_SIZE;
    pi32ConfigAttribs[9] = 0;
    pi32ConfigAttribs[10] = EGL_RED_SIZE;
    pi32ConfigAttribs[11] = 8;
    pi32ConfigAttribs[12] = EGL_GREEN_SIZE;
    pi32ConfigAttribs[13] = 8;
    pi32ConfigAttribs[14] = EGL_BLUE_SIZE;
    pi32ConfigAttribs[15] = 8;
    pi32ConfigAttribs[16] = EGL_ALPHA_SIZE;
    pi32ConfigAttribs[17] = 8;
    pi32ConfigAttribs[18] = EGL_DEPTH_SIZE;
    pi32ConfigAttribs[19] = 8;
    pi32ConfigAttribs[20] = EGL_LEVEL;
    pi32ConfigAttribs[21] = 0;
    pi32ConfigAttribs[22] = EGL_BUFFER_SIZE;
    pi32ConfigAttribs[23] = 24;
    pi32ConfigAttribs[24] = EGL_NONE;
    // Step 5 - Find a config that matches all requirements.
    int iConfigs;
    EGLConfig eglConfig;
    eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, &iConfigs);
    qDebug() << "egl error" << eglGetError();

    if (iConfigs != 1)
    
        printf("Error: eglChooseConfig(): config not found.\n");
        exit(-1);
    

    EGLint pbufferAttribs[5];
    pbufferAttribs[0] = EGL_WIDTH;
    pbufferAttribs[1] = 1920;
    pbufferAttribs[2] = EGL_HEIGHT;
    pbufferAttribs[3] = 1080;
    pbufferAttribs[4] = EGL_NONE;

    // Step 6 - Create a surface to draw to.
    EGLSurface eglSurface;
    eglSurface = eglCreatePbufferSurface(eglDisplay, eglConfig, pbufferAttribs);
    qDebug() << "egl error" << eglGetError();

    if (eglSurface == EGL_NO_SURFACE)
    
        qDebug() << "surface issue";
    

    // Step 7 - Create a context.
    EGLContext eglContext;
    eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, NULL);
    qDebug() << "egl error" << eglGetError();

    if (eglContext == EGL_NO_CONTEXT)
    
         qDebug() << "context issue";
    

    // Step 8 - Bind the context to the current thread
    bool result = eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);

    if (!result)
    
        qDebug() << "make current error" << eglGetError();
    
    qDebug() << "egl error" << eglGetError();

    GLuint renderBufferWidth = 1920;
    GLuint renderBufferHeight = 1080;

    QElapsedTimer benchmarkTimer;
    int size = 4 * renderBufferHeight * renderBufferWidth;
    unsigned char *data2 = new unsigned char[size];
    int i = 0;
    benchmarkTimer.start();
    while(i<1000)
    
        glClearColor(1.0,0.0,0.0,1.0);
        glClear(GL_COLOR_BUFFER_BIT);
        eglSwapBuffers( eglDisplay, eglSurface);
        glReadPixels(0,0,renderBufferWidth,renderBufferHeight,GL_RGBA, GL_UNSIGNED_BYTE, data2);
        ++i;
    
    qDebug() << "fps" << 1000.0*1000.0/benchmarkTimer.elapsed();

    QImage saveImage(data2, renderBufferWidth, renderBufferHeight, QImage::Format_RGBA8888_Premultiplied);
    saveImage.save("haha.png");
    QCoreApplication a(argc, argv);
    qDebug() << "done";
    return a.exec();

【问题讨论】:

你为什么不检查eglMakeCurrent()的返回值? 我检查了,也检查了 eglGetError(),一切看起来都很好。为了简洁,我没有将它们粘贴在这里。 【参考方案1】:

这段代码有几个问题:

    config属性中指定的EGL_SURFACE_TYPE错误:

    pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;
    pi32ConfigAttribs[1] = EGL_WINDOW_BIT;
    

    要渲染到 PBuffer,这需要使用匹配值:

    pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;
    pi32ConfigAttribs[1] = EGL_PBUFFER_BIT;
    

    没有为 PBuffer 指定大小。虽然man page 建议在不指定大小的情况下创建 PBuffer 是合法的,但宽度和高度的默认值为 0。我无法想象在尝试渲染到大小为 0 乘以 0 的表面时会发生什么好事。指定大小:

    EGLint pbufferAttribs[5];
    pbufferAttribs[0] = EGL_WIDTH;
    pbufferAttribs[1] = DesiredWidthOfPBuffer;
    pbufferAttribs[2] = EGL_HEIGHT;
    pbufferAttribs[3] = DesiredHeightOfPBuffer;
    pbufferAttribs[4] = EGL_NONE;
    eglSurface = eglCreatePbufferSurface(eglDisplay, eglConfig, pbufferAttribs);
    

我对这个问题的回答包含为两个不同版本的 EGL 创建上下文和 PBuffer 表面的完整代码:GLES10.glGetIntegerv returns 0 in Lollipop only。代码使用 Java 绑定,但应该很容易适应。

【讨论】:

非常感谢,在根据您的建议编辑了我的代码后,我设法让它在 opengl es 2 上下文中工作。我读回的图像确实是红色的。但是,一旦我将 opengl es 更改为桌面 opengl,我仍然遇到问题。我读回的图像现在完全透明了。我尝试设置缓冲区大小、深度大小和我认为必要的所有内容,但我仍然无法解决问题。 :( 我的代码在问题中更新了。【参考方案2】:

为什么你在 glReadPixels 之前做 eglSwapBuffers? 大概 eglSwapBuffers 对 PBufferSurface 没有影响(因为它不是双缓冲表面),但如果是,你会尝试从未定义的缓冲区读取像素,结果未定义..

【讨论】:

以上是关于egl离屏渲染中的pbuffer vs fbo的主要内容,如果未能解决你的问题,请参考以下文章

Android OpenGLES2.0——FBO离屏渲染

FBO 离屏渲染慢

EGL:创建 pBuffer 表面时是不是会创建 FBO?

WebGL—实现使用FBO离屏渲染(亦同拷贝纹理)off-screen rendering的两种方式

WebGL—实现使用FBO离屏渲染(亦同拷贝纹理)off-screen rendering的两种方式

离屏渲染,OpenGL的三种渲染方式