5

我正在使用 OpenGL ES 2.0 和 Android NDK r8b。我有一个用于工作线程的共享上下文。当我尝试使用 eglMakeCurrent 将共享上下文绑定到工作线程时,我收到错误 EGL_BAD_ALLOC。

现在让我感到困惑的是,这段代码之前运行良好。我不确定我做了什么来破坏它。EGL 文档说这个错误与资源不可用有关,但我运行的是同一个应用程序它曾经在这个完全相同的设备上完美地工作,并且所有纹理都可以从主线程很好地加载。


那么是什么导致了这个错误呢?

这是我的 egl 初始化:

bool Initialize(void *displaySurface)
{
    assert(displaySurface);
    ANativeWindow *window = (ANativeWindow*)displaySurface;

    EGLint dummy, format;

    display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

    eglInitialize(display, 0, 0);

    const EGLint configAttribs[] =
    {
        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
        EGL_BLUE_SIZE, 8,
        EGL_GREEN_SIZE, 8,
        EGL_RED_SIZE, 8,
        EGL_ALPHA_SIZE, 8,
        EGL_BUFFER_SIZE, 32,
        EGL_DEPTH_SIZE, 24,
        EGL_NONE
    };

    const EGLint auxConfigAttribs[] =
    {
        EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
        EGL_BLUE_SIZE, 8,
        EGL_GREEN_SIZE, 8,
        EGL_RED_SIZE, 8,
        EGL_ALPHA_SIZE, 8,
        EGL_BUFFER_SIZE, 32,
        EGL_DEPTH_SIZE, 24,
        EGL_NONE
    };

    EGLint contextAttribs[] =
    {
        EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE
    };

    EGLint numConfigs;
    EGLConfig config;

//// create main context
    eglChooseConfig(display, configAttribs, &config, 1, &numConfigs);
    Trace("eglChooseConfig: " + GetEglError());

    eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
    Trace("eglGetConfigAttrib: " + GetEglError());

    ANativeWindow_setBuffersGeometry(window, 0, 0, format);
    Trace("ANativeWindow_setBuffersGeometry: " + GetEglError());

    surface = eglCreateWindowSurface(display, config, window, NULL);
    Trace("eglCreateWindowSurface: " + GetEglError());

    context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
    Trace("eglCreateContext: " + GetEglError());

//// create auxiliary context
    eglChooseConfig(display, auxConfigAttribs, &config, 1, &numConfigs);
    Trace("AUX eglChooseConfig: " + GetEglError());

    auxSurface = eglCreatePbufferSurface(display, config, NULL);
    Trace("AUX eglCreatePbufferSurface: " + GetEglError());

    auxContext = eglCreateContext(display, config, context, contextAttribs);
    Trace("AUX eglCreateContext: " + GetEglError());

//// make main context current
    eglMakeCurrent(display, surface, surface, context);
    Trace("eglMakeCurrent: " + GetError());

    eglQuerySurface(display, surface, EGL_WIDTH, &width);
    eglQuerySurface(display, surface, EGL_HEIGHT, &height);

    SetViewport(width, height);

    EnableDepthBuffer(enableDepthTest);
    EnableBackfaceCulling(enableBackfaceCull);
    SetBackgroundColor(backgroundColor);

    glDisable(GL_DITHER);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    Trace("finished" + GetEglError());

    alive = true;

    return true;
}

编辑:我可能是错的,但这个错误似乎是在更新 ndk 工具链的同时出现的......但我无法测试这个理论,因为我没有任何旧的 ndk 副本了......所以如果有人有我可以获得 NDK 7c 的链接,那也会很有帮助。

4

1 回答 1

10

好吧,问题似乎在于 PBuffer 需要至少为 1x1 像素才能工作,而 EGL_WIDTH 和 EGL_HEIGHT 的默认值为 0。

获奖配置:

bool Initialize(void *displaySurface)
{
    assert(displaySurface);
    ANativeWindow *window = (ANativeWindow*)displaySurface;

    EGLint dummy, format;

    display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

    eglInitialize(display, 0, 0);

    EGLint contextAttribs[] =
    {
        EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE
    };

//// create main context
    const EGLint configAttribs[] =
    {
        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
        EGL_BLUE_SIZE, 8,
        EGL_GREEN_SIZE, 8,
        EGL_RED_SIZE, 8,
        EGL_ALPHA_SIZE, 8,
        EGL_BUFFER_SIZE, 32,
        EGL_DEPTH_SIZE, 24,
        EGL_NONE
    };

    EGLint numConfigs;
    EGLConfig config;

    eglChooseConfig(display, configAttribs, &config, 1, &numConfigs);
    Trace("eglChooseConfig: " + GetEglError());

    eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
    Trace("eglGetConfigAttrib: " + GetEglError());

    ANativeWindow_setBuffersGeometry(window, 0, 0, format);
    Trace("ANativeWindow_setBuffersGeometry: " + GetEglError());

    surface = eglCreateWindowSurface(display, config, window, NULL);
    Trace("eglCreateWindowSurface: " + GetEglError());

    context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
    Trace("eglCreateContext: " + GetEglError());

//// create auxiliary context

    const EGLint auxConfigAttribs[] =
    {
        EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
        EGL_BLUE_SIZE, 8,
        EGL_GREEN_SIZE, 8,
        EGL_RED_SIZE, 8,
        EGL_ALPHA_SIZE, 0,
        EGL_DEPTH_SIZE, 0,
        EGL_STENCIL_SIZE, 0,
        EGL_NONE
    };

    EGLint pbufferAttribs[] =
    {
        EGL_WIDTH, 1,
        EGL_HEIGHT, 1,
        EGL_TEXTURE_TARGET, EGL_NO_TEXTURE,
        EGL_TEXTURE_FORMAT, EGL_NO_TEXTURE,
        EGL_NONE
    };

    EGLint auxNumConfigs;
    EGLConfig auxConfig;

    eglChooseConfig(display, auxConfigAttribs, &auxConfig, 1, &auxNumConfigs);
    Trace("AUX eglChooseConfig: " + GetEglError());

    auxSurface = eglCreatePbufferSurface(display, auxConfig, pbufferAttribs);
    Trace("AUX eglCreatePbufferSurface: " + GetEglError());

    auxContext = eglCreateContext(display, auxConfig, context, contextAttribs);
    Trace("AUX eglCreateContext: " + GetEglError());

//// make main context current
    eglMakeCurrent(display, surface, surface, context);
    Trace("eglMakeCurrent main: " + GetError());

    eglQuerySurface(display, surface, EGL_WIDTH, &width);
    eglQuerySurface(display, surface, EGL_HEIGHT, &height);

    SetViewport(width, height);

    EnableDepthBuffer(enableDepthTest);
    EnableBackfaceCulling(enableBackfaceCull);
    SetBackgroundColor(backgroundColor);

    glDisable(GL_DITHER);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    Trace("finished" + GetEglError());

    alive = true;

    return true;
}
于 2012-09-16T20:11:14.713 回答