Reputation:
I'm using SDL2 2.0.2 on Debian stable, and I'm trying to acquire an OpenGL ES 3.0 context with it. This works if I request an OpenGL ES 2.0 context, but not if I directly request an OpenGL ES 3.0 context.
Consider the following program:
#include <GLES3/gl3.h>
#include <SDL2/SDL.h>
int main(
int argc,
char **argv
) {
SDL_GLContext context;
int rc;
const GLubyte *version;
SDL_Window *window;
rc = SDL_Init(SDL_INIT_VIDEO);
if (rc < 0) {
return EXIT_FAILURE;
}
atexit(SDL_Quit);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
#ifdef THIS_SHOULD_WORK_TOO
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#endif
window = SDL_CreateWindow("OpenGL", 100, 100, 800, 600, SDL_WINDOW_OPENGL);
context = SDL_GL_CreateContext(window);
version = glGetString(GL_VERSION);
if (version == 0) {
printf(
"Unable to get OpenGL ES version string: %d\n",
glGetError()
);
return EXIT_FAILURE;
}
printf("Version string: %s\n", version);
SDL_GL_DeleteContext(context);
return EXIT_SUCCESS;
}
When I compile this normally, it requests an OpenGL ES 2.0 context, and the program receives an OpenGL ES 3.0 context:
$ c99 example.c -lSDL2 -lGLESv2
$ ./a.out
Version string: OpenGL ES 3.0 Mesa 10.3.2
However, when I request an OpenGL ES 3.0 context, this program fails without a clear error message:
$ c99 -DTHIS_SHOULD_WORK_TOO example.c -lSDL2 -lGLESv2
$ ./a.out
Unable to get OpenGL ES version string: 0
Why is this?
I doubt it is because of -lGLESv2
, mostly because the OpenGL ES context reports that it is version 3.0, and because platforms that do ship a libGLESv3.so
ship it as a symlink it to libGLESv2.so
(Android does this, for example).
Upvotes: 4
Views: 8228
Reputation:
This is a bug in SDL2:
SDL's EGL code (used for OpenGL ES context creation on Windows / Linux / Android / etc.) only seems to properly support OpenGL ES 1 and 2 contexts.
In particular, the current code sets the EGL_RENDERABLE_TYPE config attribute to EGL_OPENGL_ES2_BIT if GLES2 is requested and EGL_OPENGL_ES_BIT in all other cases. It never uses EGL_OPENGL_ES3_BIT / EGL_OPENGL_ES3_BIT_KHR even when GLES3.0+ is requested. It also doesn't use EGL_CONTEXT_MINOR_VERSION_KHR to set the version to 3.1 rather than 3.0 in SDL_EGL_CreateContext, when 3.1 is requested.
This means that a request for OpenGL ES 3.0 is translated into a request for OpenGL ES 1.0. Since OpenGL ES 3.0 is backwards incompatible with OpenGL ES 1.0, the request eventually fails (I think).
The fix has been merged into the master branch, and is scheduled to be released as part of SDL2 version 2.0.4:
- Added EGL_KHR_create_context support to allow OpenGL ES version selection on some platforms
Upvotes: 6