如何通过 EGL 创建 OpenGL 3.3 或 4.x 上下文

How to create an OpenGL 3.3 or 4.x context through EGL

我有兴趣制作一个不依赖于 X11 的 OpenGL 应用程序。如我所见,这应该可以通过 EGL 实现。网上什至有例子。但是我怎样才能控制上下文版本呢?下面的示例代码创建了一个版本为 2.1 的 OpenGL 上下文(在 wayland 上),但在我的计算机上它显示支持的最高 OpenGL 版本是 3.3(这样的上下文可以使用 glXCreateContextAttribsARB 在 X 服务器中使用 GLX 和 xlib 创建)。所以我的问题是:我可以通过 EGL 以某种方式创建更高版本的 OpenGL 上下文吗?如果可以,如何创建?

示例代码:

#include <wayland-client.h>
#include <wayland-egl.h>
#include <EGL/egl.h>
#include <GL/gl.h>
#include <string.h>
#include <stdio.h>

#define WIDTH 256
#define HEIGHT 256

static struct wl_display *display;
static struct wl_compositor *compositor = NULL;
static struct wl_shell *shell = NULL;
static EGLDisplay egl_display;
static char running = 1;

struct window {
    EGLContext egl_context;
    struct wl_surface *surface;
    struct wl_shell_surface *shell_surface;
    struct wl_egl_window *egl_window;
    EGLSurface egl_surface;
};

// listeners
static void registry_add_object (void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) {
    if (!strcmp(interface,"wl_compositor")) {
        compositor = wl_registry_bind (registry, name, &wl_compositor_interface, 0);
    }
    else if (!strcmp(interface,"wl_shell")) {
        shell = wl_registry_bind (registry, name, &wl_shell_interface, 0);
    }
}
static void registry_remove_object (void *data, struct wl_registry *registry, uint32_t name) {

}
static struct wl_registry_listener registry_listener = {&registry_add_object, &registry_remove_object};

static void shell_surface_ping (void *data, struct wl_shell_surface *shell_surface, uint32_t serial) {
    wl_shell_surface_pong (shell_surface, serial);
}
static void shell_surface_configure (void *data, struct wl_shell_surface *shell_surface, uint32_t edges, int32_t width, int32_t height) {
    struct window *window = data;
    wl_egl_window_resize (window->egl_window, width, height, 0, 0);
}
static void shell_surface_popup_done (void *data, struct wl_shell_surface *shell_surface) {

}
static struct wl_shell_surface_listener shell_surface_listener = {&shell_surface_ping, &shell_surface_configure, &shell_surface_popup_done};

static void create_window (struct window *window, int32_t width, int32_t height) {
    eglBindAPI (EGL_OPENGL_API);
    EGLint attributes[] = {
        EGL_RED_SIZE, 8,
        EGL_GREEN_SIZE, 8,
        EGL_BLUE_SIZE, 8,
    EGL_NONE};
    EGLConfig config;
    EGLint num_config;
    eglChooseConfig (egl_display, attributes, &config, 1, &num_config);
    window->egl_context = eglCreateContext (egl_display, config, EGL_NO_CONTEXT, NULL);

    window->surface = wl_compositor_create_surface (compositor);
    window->shell_surface = wl_shell_get_shell_surface (shell, window->surface);
    wl_shell_surface_add_listener (window->shell_surface, &shell_surface_listener, window);
    wl_shell_surface_set_toplevel (window->shell_surface);
    window->egl_window = wl_egl_window_create (window->surface, width, height);
    window->egl_surface = eglCreateWindowSurface (egl_display, config, window->egl_window, NULL);
    eglMakeCurrent (egl_display, window->egl_surface, window->egl_surface, window->egl_context);
}
static void delete_window (struct window *window) {
    eglDestroySurface (egl_display, window->egl_surface);
    wl_egl_window_destroy (window->egl_window);
    wl_shell_surface_destroy (window->shell_surface);
    wl_surface_destroy (window->surface);
    eglDestroyContext (egl_display, window->egl_context);
}
static void draw_window (struct window *window) {
    glClearColor (0.0, 1.0, 0.0, 1.0);
    glClear (GL_COLOR_BUFFER_BIT);

    printf("%s\n", glGetString(GL_VERSION));

    eglSwapBuffers (egl_display, window->egl_surface);
}

int main () {
    display = wl_display_connect (NULL);
    struct wl_registry *registry = wl_display_get_registry (display);
    wl_registry_add_listener (registry, &registry_listener, NULL);
    wl_display_dispatch (display);

    egl_display = eglGetDisplay (display);
    eglInitialize (egl_display, NULL, NULL);

    struct window window;
    create_window (&window, WIDTH, HEIGHT);

    while (running) {
        wl_display_dispatch_pending (display);
        draw_window (&window);
    }

    delete_window (&window);
    eglTerminate (egl_display);
    wl_display_disconnect (display);
    return 0;
}

来源:https://github.com/eyelash/tutorials

EGL 最初只是为 OpenGL ES 开发的。 EGL 1.4(2008 年发布)添加了对桌面 OpenGL 的支持。然而,那时 "legacy" GL 上下文、前向兼容上下文和 OpenGL 配置文件(如核心和兼容性)之间的区别尚不存在,因此 EGL 1.4 不包括请求其中任何一个的方法。您唯一可以依赖的是您获得了与 "legacy" GL 1.x/2.x 兼容的 GL 上下文。这并不意味着它也不能创建更高版本的兼容性配置文件,但是 - 你不能依赖它,也无法控制它。

请求与特定版本兼容的 GL 上下文或实现特定配置文件的能力首先由 EGL_KHR_create_context EGL extension in 2012, which is functionally quite similiar to the WGL/glX counterparts for the classical desktop GL binding APIs. This functionality also has been added to core EGL version 1.5 提供。

这意味着您需要一个支持带有该扩展的 EGL 1.5 或 1.4 的实现,以便能够通过指定

  • EGL_CONTEXT_MAJOR_VERSION, EGL_CONTEXT_MINOR_VERSION
  • EGL_CONTEXT_FLAGS
  • EGL_CONTEXT_OPENGL_PROFILE_MASK

属性(或带有 _KHR 后缀的扩展对应物,它们最终具有相同的值 - 后者仅在 elgext.h 中定义,而核心版本在 egl.h 对于版本 1.5) 在上下文创建时。