We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3122 - Please allow desktop OpenGL contexts under EGL
Summary: Please allow desktop OpenGL contexts under EGL
Status: RESOLVED WONTFIX
Alias: None
Product: SDL
Classification: Unclassified
Component: video (show other bugs)
Version: 2.0.3
Hardware: x86_64 Linux
: P2 enhancement
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-09-15 11:59 UTC by Steinar H. Gunderson
Modified: 2017-08-12 23:15 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Steinar H. Gunderson 2015-09-15 11:59:50 UTC
Hi,

I'm using SDL2 on regular Linux, with OpenGL. I've enjoyed the new functionality (over 1.2) for core contexts and GLES; they make things a lot easier to test for my program. However, I've recently found out that I can't use GLX anymore; I really need EGL, even for desktop OpenGL contexts. The reason is that EGL allows you to encapsulate images from various places in an API-neutral object (EGLImage), so that you can e.g. decode video from VA-API or OpenMAX, place it in an EGLImage and then make a OpenGL texture from that without having to copy the data. (Similarly for encoding.)

However, from reading the code, EGL in SDL seems to be all about GLES (which is too limited for my use): The EGL attribute is merely an alias for asking for a GLES context, and even if I compile without GLX to force EGL, it detects that I want a core context and then tries to forward to some other functions.

EGL with desktop OpenGL is fully supported since EGL 1.4; you just need to eglBindAPI(EGL_OPENGL_API) in each thread before eglMakeCurrent(). (There are also some issues with multi-context/multithreading; seemingly you can't share EGLSurfaces between multiple threads. The usual workaround is to create a 1x1 PBuffer surface, see e.g. http://malideveloper.arm.com/resources/sample-code/thread-synchronization/.) With EGL_KHR_create_context, you can also ask for core contexts. I've tested that all of this works on my Intel card using Mesa 11 by writing EGL init code manually, but it would really nice just to have it as part of SDL. So please consider lifting these limitations :-)

Thanks!
Comment 1 Alex Szpakowski 2015-09-15 14:43:47 UTC
SDL 2.0.4's EGL code has this capability.
Comment 2 Steinar H. Gunderson 2015-09-20 21:42:27 UTC
I tried this, but SDL_GL_CreateContext returns NULL:

#include <epoxy/egl.h>

#include <SDL2/SDL.h>
#include <SDL2/SDL_error.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_video.h>
#include <assert.h>

int main(int argc, char **argv)
{
        if (SDL_Init(SDL_INIT_VIDEO) == -1) {
                fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
                exit(1);
        }
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
        SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_EGL, 1);

        // SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
        SDL_Window *window = SDL_CreateWindow("OpenGL window",
                SDL_WINDOWPOS_UNDEFINED,
                SDL_WINDOWPOS_UNDEFINED,
                1280, 720,
                SDL_WINDOW_OPENGL);
        SDL_GLContext context = SDL_GL_CreateContext(surface);
        assert(context != NULL);

        printf("%p\n", eglGetCurrentContext());
}

So, the assert fails.
Comment 3 Steinar H. Gunderson 2015-09-20 21:42:43 UTC
This was with SDL from hg, by the way.
Comment 4 Alex Szpakowski 2015-09-20 21:52:14 UTC
Well, your code is requesting an OpenGL ES context (since the deprecated SDL_GL_CONTEXT_EGL flag does that.)

SDL chooses an appropriate context creation API based on the windowing backend and the requested OpenGL version and profile. It's not possible to force it to always choose EGL for desktop OpenGL contexts when the X11 windowing backend is used, for example. (Nor is it possible to force it to always use GLX no matter what.)

Be sure to set the appropriate SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_MAJOR_VERSION, and SDL_GL_MINOR_VERSION flags to what you want.

Also check SDL_GetError after a SDL function call returns NULL or -1, to better understand what caused it to fail.
Comment 5 Steinar H. Gunderson 2015-09-20 21:57:03 UTC
If so, count this bug as a feature request to always force EGL :-) I could always compile without GLX, but a distribution is unlikely to.
Comment 6 Alex Szpakowski 2015-09-20 21:57:41 UTC
It's also worth mentioning that some linux video drivers don't support EGL at all (e.g. older nvidia drivers). EGL is also not available at all in OS X or iOS, and on Windows it's generally not provided with a regular video driver install.
Comment 7 Steinar H. Gunderson 2015-09-20 21:59:23 UTC
Yes, I'm fully aware that getting an EGL context might fail. But it's a good point.
Comment 8 Sam Lantinga 2017-08-12 23:15:42 UTC
I'm going to close this for now, but feel free to reopen it with a patch that adds an SDL hint to force EGL usage.