7

X11 に依存しない OpenGL アプリの作成に興味があります。これまで見てきたように、これは EGL によって可能になるはずです。インターネット上にも例があります。しかし、コンテキストのバージョンを制御するにはどうすればよいでしょうか? 以下のサンプル コードは、バージョン 2.1 で OpenGL コンテキスト (ウェイランド上) を作成しますが、私のコンピューターでは、サポートされている OpenGL の最高バージョンが 3.3 であることを示しています (このようなコンテキストは、X サーバーで GLX と xlib を使用して glXCreateContextAttribsARB で作成できます)。だから私の質問は次のとおりです。どうにかして 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

4

1 に答える 1

13

EGL は当初、OpenGL ES 専用に開発されました。デスクトップ OpenGL のサポートは、EGL 1.4 (2008 年リリース) で追加されました。ただし、その時点では、「レガシー」GL コンテキスト、前方互換コンテキスト、およびコアや互換性などの OpenGL プロファイルの区別がまだ存在していなかったため、EGL 1.4 にはこれらのいずれかを要求する手段が含まれていません。信頼できる唯一のことは、「レガシー」GL 1.x/2.x と互換性のある GL コンテキストを取得することです。ただし、これは、より高いバージョンの互換性プロファイルも作成できないことを意味するものではありません。これに依存することはできず、それを制御する方法がありません。

特定のバージョンと互換性のある GL コンテキストを要求する機能、または特定のプロファイルを実装するEGL_KHR_create_context機能は、2012 年に EGL 拡張機能によって最初に提供されました。これは、従来のデスクトップ GL バインディング APIのWGL / glX対応物と機能的に非常に似ています。この機能は、コアEGL バージョン 1.5にも追加されています。

これは、EGL 1.5 または 1.4 のいずれかをサポートし、その拡張機能を使用して、最新の OpenGL コンテキストを制御された方法で確実に作成できるようにするために、

  • EGL_CONTEXT_MAJOR_VERSIONEGL_CONTEXT_MINOR_VERSION
  • EGL_CONTEXT_FLAGS
  • EGL_CONTEXT_OPENGL_PROFILE_MASK

コンテキスト作成時の属性 (またはサフィックスが付いた対応する拡張機能_KHR。最終的には同じ値になります。後者は で定義されているだけelgext.hですが、コア バージョンはegl.hバージョン 1.5 に含まれています)。

于 2015-12-23T22:58:46.227 に答える