From 2cea9e2719f97dcdc81f11f4643c23b9cfc0ef8a Mon Sep 17 00:00:00 2001 From: Nigel Stewart Date: Sun, 6 Dec 2015 12:58:46 +1000 Subject: [PATCH] GLEW_EGL mode for loading functions via eglGetProcAddress --- auto/Makefile | 6 ++- auto/src/glew_head.c | 11 ++++-- auto/src/glew_init_tail.c | 2 +- auto/src/glew_str_glx.c | 2 +- auto/src/glew_str_wgl.c | 2 +- auto/src/glewinfo_glx.c | 2 +- auto/src/glewinfo_head.c | 7 +++- auto/src/glewinfo_tail.c | 82 +++++++++++++++++++++++++++++---------- auto/src/glewinfo_wgl.c | 2 +- 9 files changed, 84 insertions(+), 32 deletions(-) diff --git a/auto/Makefile b/auto/Makefile index fb2663c..5862050 100644 --- a/auto/Makefile +++ b/auto/Makefile @@ -200,14 +200,14 @@ $(S.DEST)/glew.c: $(EXT)/.dummy $(BIN)/make_list.pl $(GL_EXT_SPEC) >> $@ $(BIN)/make_list2.pl $(GL_EXT_SPEC) >> $@ echo -e "\n return GLEW_OK;\n}\n" >> $@ - echo -e "\n#if defined(_WIN32) && ! defined(GLEW_OSMESA)" >> $@ + echo -e "\n#if defined(_WIN32) && !defined(GLEW_EGL) && !defined(GLEW_OSMESA)" >> $@ $(BIN)/make_def_fun.pl WGL $(WGL_EXT_SPEC) >> $@ $(BIN)/make_def_var.pl WGL $(WGL_EXT_SPEC) >> $@ $(BIN)/make_init.pl WGL $(WGL_EXT_SPEC) >> $@ cat $(SRC)/glew_init_wgl.c >> $@ $(BIN)/make_list.pl $(WGL_EXT_SPEC) >> $@ echo -e "\n return GLEW_OK;\n}" >> $@; - echo -e "\n#elif !defined(GLEW_OSMESA) && !defined(__ANDROID__) && !defined(__native_client__) && !defined(__HAIKU__) && (!defined(__APPLE__) || defined(GLEW_APPLE_GLX))" >> $@ + echo -e "\n#elif !defined(GLEW_EGL) && !defined(GLEW_OSMESA) && !defined(__ANDROID__) && !defined(__native_client__) && !defined(__HAIKU__) && (!defined(__APPLE__) || defined(GLEW_APPLE_GLX))" >> $@ $(BIN)/make_def_fun.pl GLX $(GLX_CORE_SPEC) >> $@ $(BIN)/make_def_fun.pl GLX $(GLX_EXT_SPEC) >> $@ echo -e "\nGLboolean __GLXEW_VERSION_1_0 = GL_FALSE;" >> $@ @@ -245,12 +245,14 @@ $(S.DEST)/glewinfo.c: $(EXT)/.dummy cat $(SRC)/glewinfo_head.c >> $@ $(BIN)/make_info.pl $(GL_CORE_SPEC) >> $@ $(BIN)/make_info.pl $(GL_EXT_SPEC) >> $@ + echo -e "#if !defined(GLEW_EGL)\n" >> $@ echo -e "#ifdef _WIN32\n" >> $@ $(BIN)/make_info.pl $(WGL_EXT_SPEC) >> $@ echo -e "#else /* _UNIX */\n" >> $@ $(BIN)/make_info.pl $(GLX_CORE_SPEC) >> $@ $(BIN)/make_info.pl $(GLX_EXT_SPEC) >> $@ echo -e "#endif /* _WIN32 */\n" >> $@ + echo -e "#endif /* !defined(GLEW_EGL) */\n" >> $@ cat $(SRC)/glewinfo_gl.c >> $@ $(BIN)/make_info_list.pl $(GL_CORE_SPEC) >> $@ diff --git a/auto/src/glew_head.c b/auto/src/glew_head.c index 99947de..0d052be 100644 --- a/auto/src/glew_head.c +++ b/auto/src/glew_head.c @@ -1,6 +1,7 @@ #include -#if defined(GLEW_OSMESA) +#if defined(GLEW_EGL) +#elif defined(GLEW_OSMESA) # define GLAPI extern # include #elif defined(_WIN32) @@ -13,7 +14,9 @@ #include /* For bsearch */ #include /* For memset */ -#if defined(GLEW_REGAL) +#if defined(GLEW_EGL) +extern void * eglGetProcAddress (const char *procname); +#elif defined(GLEW_REGAL) /* In GLEW_REGAL mode we call direcly into the linked libRegal.so glGetProcAddressREGAL for looking up @@ -113,7 +116,9 @@ void* NSGLGetProcAddress (const GLubyte *name) /* * Define glewGetProcAddress. */ -#if defined(GLEW_REGAL) +#if defined(GLEW_EGL) +# define glewGetProcAddress(name) eglGetProcAddress((const char *)name) +#elif defined(GLEW_REGAL) # define glewGetProcAddress(name) regalGetProcAddress((const GLchar *)name) #elif defined(GLEW_OSMESA) # define glewGetProcAddress(name) OSMesaGetProcAddress((const char *)name) diff --git a/auto/src/glew_init_tail.c b/auto/src/glew_init_tail.c index 17735dc..cc1c4ad 100644 --- a/auto/src/glew_init_tail.c +++ b/auto/src/glew_init_tail.c @@ -37,7 +37,7 @@ GLenum GLEWAPIENTRY glewInit (void) GLenum r; r = glewContextInit(); if ( r != 0 ) return r; -#if defined(GLEW_OSMESA) || defined(__ANDROID__) || defined(__native_client__) || defined(__HAIKU__) +#if defined(GLEW_EGL) || defined(GLEW_OSMESA) || defined(__ANDROID__) || defined(__native_client__) || defined(__HAIKU__) return r; #elif defined(_WIN32) return wglewInit(); diff --git a/auto/src/glew_str_glx.c b/auto/src/glew_str_glx.c index af4f8c6..885db43 100644 --- a/auto/src/glew_str_glx.c +++ b/auto/src/glew_str_glx.c @@ -4,7 +4,7 @@ return ret; } -#elif !defined(GLEW_OSMESA) && !defined(__ANDROID__) && !defined(__native_client__) && !defined(__HAIKU__) && !defined(__APPLE__) || defined(GLEW_APPLE_GLX) +#elif !defined(GLEW_EGL) && !defined(GLEW_OSMESA) && !defined(__ANDROID__) && !defined(__native_client__) && !defined(__HAIKU__) && !defined(__APPLE__) || defined(GLEW_APPLE_GLX) GLboolean glxewIsSupported (const char* name) { diff --git a/auto/src/glew_str_wgl.c b/auto/src/glew_str_wgl.c index b204386..db67b51 100644 --- a/auto/src/glew_str_wgl.c +++ b/auto/src/glew_str_wgl.c @@ -4,7 +4,7 @@ return ret; } -#if defined(_WIN32) && !defined(GLEW_OSMESA) +#if defined(_WIN32) && !defined(GLEW_EGL) && !defined(GLEW_OSMESA) GLboolean GLEWAPIENTRY wglewIsSupported (const char* name) { diff --git a/auto/src/glewinfo_glx.c b/auto/src/glewinfo_glx.c index ead3051..e60b549 100644 --- a/auto/src/glewinfo_glx.c +++ b/auto/src/glewinfo_glx.c @@ -1,6 +1,6 @@ } -#elif !defined(GLEW_OSMESA) /* _UNIX */ +#elif !defined(GLEW_EGL) && !defined(GLEW_OSMESA) /* _UNIX */ static void glxewInfo () { diff --git a/auto/src/glewinfo_head.c b/auto/src/glewinfo_head.c index 6a8347b..ff743f0 100644 --- a/auto/src/glewinfo_head.c +++ b/auto/src/glewinfo_head.c @@ -2,7 +2,9 @@ #include #include #include -#if defined(GLEW_OSMESA) +#if defined(GLEW_EGL) +#include +#elif defined(GLEW_OSMESA) #define GLAPI extern #include #elif defined(_WIN32) @@ -25,7 +27,8 @@ static FILE* f; struct createParams { -#if defined(GLEW_OSMESA) +#if defined(GLEW_EGL) +#elif defined(GLEW_OSMESA) #elif defined(_WIN32) int pixelformat; #elif !defined(__APPLE__) && !defined(__HAIKU__) || defined(GLEW_APPLE_GLX) diff --git a/auto/src/glewinfo_tail.c b/auto/src/glewinfo_tail.c index e69f96d..e4075c0 100644 --- a/auto/src/glewinfo_tail.c +++ b/auto/src/glewinfo_tail.c @@ -7,9 +7,10 @@ int main (int argc, char** argv) { GLuint err; - struct createParams params = + struct createParams params = { -#if defined(GLEW_OSMESA) +#if defined(GLEW_EGL) +#elif defined(GLEW_OSMESA) #elif defined(_WIN32) -1, /* pixelformat */ #elif !defined(__HAIKU__) && !defined(__APPLE__) || defined(GLEW_APPLE_GLX) @@ -25,17 +26,18 @@ int main (int argc, char** argv) if (glewParseArgs(argc-1, argv+1, ¶ms)) { fprintf(stderr, "Usage: glewinfo " -#if defined(GLEW_OSMESA) +#if defined(GLEW_EGL) +#elif defined(GLEW_OSMESA) #elif defined(_WIN32) - "[-pf ] " + "[-pf ] " #elif !defined(__HAIKU__) && !defined(__APPLE__) || defined(GLEW_APPLE_GLX) - "[-display ] " - "[-visual ] " + "[-display ] " + "[-visual ] " #endif - "[-version ] " - "[-profile core|compatibility] " - "[-flag debug|forward]" - "\n"); + "[-version ] " + "[-profile core|compatibility] " + "[-flag debug|forward]" + "\n"); return 1; } @@ -68,18 +70,20 @@ int main (int argc, char** argv) fprintf(f, " GLEW Extension Info\n"); fprintf(f, "---------------------------\n\n"); fprintf(f, "GLEW version %s\n", glewGetString(GLEW_VERSION)); -#if defined(GLEW_OSMESA) +#if defined(GLEW_EGL) +#elif defined(GLEW_OSMESA) #elif defined(_WIN32) fprintf(f, "Reporting capabilities of pixelformat %d\n", params.pixelformat); #elif !defined(__APPLE__) || defined(GLEW_APPLE_GLX) - fprintf(f, "Reporting capabilities of display %s, visual 0x%x\n", + fprintf(f, "Reporting capabilities of display %s, visual 0x%x\n", params.display == NULL ? getenv("DISPLAY") : params.display, params.visual); #endif - fprintf(f, "Running on a %s from %s\n", - glGetString(GL_RENDERER), glGetString(GL_VENDOR)); + fprintf(f, "Running on a %s from %s\n", + glGetString(GL_RENDERER), glGetString(GL_VENDOR)); fprintf(f, "OpenGL version %s is supported\n", glGetString(GL_VERSION)); glewInfo(); -#if defined(GLEW_OSMESA) +#if defined(GLEW_EGL) +#elif defined(GLEW_OSMESA) #elif defined(_WIN32) wglewInfo(); #else @@ -118,7 +122,8 @@ GLboolean glewParseArgs (int argc, char** argv, struct createParams *params) else return GL_TRUE; ++p; } -#if defined(GLEW_OSMESA) +#if defined(GLEW_EGL) +#elif defined(GLEW_OSMESA) #elif defined(_WIN32) else if (!strcmp(argv[p], "-pf") || !strcmp(argv[p], "-pixelformat")) { @@ -145,7 +150,44 @@ GLboolean glewParseArgs (int argc, char** argv, struct createParams *params) /* ------------------------------------------------------------------------ */ -#if defined(GLEW_OSMESA) +#if defined(GLEW_EGL) +EGLDisplay display; +EGLContext ctx; + +/* See: http://stackoverflow.com/questions/12662227/opengl-es2-0-offscreen-context-for-fbo-rendering */ + +GLboolean glewCreateContext (struct createParams *params) +{ + EGLSurface surface; + EGLint majorVersion, minorVersion; + const EGLint attr[] = { + EGL_BUFFER_SIZE, 32, + EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER, + EGL_CONFORMANT, EGL_OPENGL_BIT, + EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, + EGL_NONE + }; + EGLConfig config; + EGLint numConfig; + display = eglGetDisplay((EGLNativeDisplayType) 0); + if (!eglInitialize(display, &majorVersion, &minorVersion)) + return GL_TRUE; + eglBindAPI(EGL_OPENGL_API); + if (!eglChooseConfig(display, attr, &config, 1, &numConfig) || (numConfig != 1)) + return GL_TRUE; + surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType) NULL, NULL); + ctx = eglCreateContext(display, config, NULL, NULL); + if (NULL == ctx) return GL_TRUE; + eglMakeCurrent(display, surface, surface, ctx); + return GL_FALSE; +} + +void glewDestroyContext () +{ + if (NULL != ctx) eglDestroyContext(display, ctx); +} + +#elif defined(GLEW_OSMESA) OSMesaContext ctx; static const GLint osmFormat = GL_UNSIGNED_BYTE; @@ -190,7 +232,7 @@ GLboolean glewCreateContext (struct createParams* params) wc.lpszClassName = "GLEW"; if (0 == RegisterClass(&wc)) return GL_TRUE; /* create window */ - wnd = CreateWindow("GLEW", "GLEW", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, + wnd = CreateWindow("GLEW", "GLEW", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, GetModuleHandle(NULL), NULL); if (NULL == wnd) return GL_TRUE; /* get the device context */ @@ -378,8 +420,8 @@ GLboolean glewCreateContext (struct createParams *params) cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone); swa.border_pixel = 0; swa.colormap = cmap; - wnd = XCreateWindow(dpy, RootWindow(dpy, vi->screen), - 0, 0, 1, 1, 0, vi->depth, InputOutput, vi->visual, + wnd = XCreateWindow(dpy, RootWindow(dpy, vi->screen), + 0, 0, 1, 1, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel | CWColormap, &swa); /* make context current */ if (!glXMakeCurrent(dpy, wnd, ctx)) return GL_TRUE; diff --git a/auto/src/glewinfo_wgl.c b/auto/src/glewinfo_wgl.c index c80264e..9405667 100644 --- a/auto/src/glewinfo_wgl.c +++ b/auto/src/glewinfo_wgl.c @@ -2,7 +2,7 @@ /* ------------------------------------------------------------------------ */ -#if defined(_WIN32) && !defined(GLEW_OSMESA) +#if defined(_WIN32) && !defined(GLEW_EGL) && !defined(GLEW_OSMESA) static void wglewInfo () {