nigels-com / glew

The OpenGL Extension Wrangler Library
Other
2.62k stars 614 forks source link

how to use glew with EGL when using desktop OpenGL? #164

Open lugui2009 opened 6 years ago

lugui2009 commented 6 years ago

Hello everybody:

I have an issue that I want use EGL to create context for desktop OpenGL .But I get segfault when executing my demo code. My graphic card is intel integrated card ,and my os is ubuntn.For more details about my pc ,take a look at image bellow. os

I downloaded glew2.0.0 source code ,because I saw "OSMesa and EGL support". However, I encountered some warnings and an error (maybe not fatal error) when building . Following codes were used(I followed the README): build

when executing "make SYSTEM=linux-egl" there were some warnings as follows,I were not so sure these warnings are fatal or not; first

when executing "sudo make install" It seemed to be OK second

when executing "make clean" I came out an error,I don't know why. I am an Admin user. third

I were not sure that whether I builded the glew2.0 successfully or not successfully when using "SYSTEM=linux-egl",so someone can give me some help at this point.

And then I tested my code(I think maybe the build was successful) ,my test code as follows . I used X11+EGL+glew+desktop OpenGL.

include

include <X11/Xlib.h>

include <X11/Xatom.h>

include <X11/Xutil.h>

include <EGL/egl.h>

include <EGL/eglext.h>

include <GL/glew.h>

include <GL/gl.h>

Display *x_display; Window win;

EGLDisplay egl_display; EGLContext egl_context; EGLSurface egl_surface;

int main(int argc, char *argv[]) {

x_display = XOpenDisplay(NULL);
if(x_display ==NULL)
{
    std::cerr<<"cannot connect to X server"<<std::endl;
    return 1;
}

Window root = DefaultRootWindow(x_display);

XSetWindowAttributes  swa;
swa.event_mask = ExposureMask|PointerMotionMask|KeyPressMask;
win = XCreateWindow(x_display,root,0,0,800,480,0,CopyFromParent,InputOutput,CopyFromParent,CWEventMask,&swa);
XSetWindowAttributes xattr;

Atom atom;
int one =1;
xattr.override_redirect = False;
XChangeProperty(x_display,win,XInternAtom(x_display,"_NET_WM_STATE",True),XA_INTEGER,  32,  PropModeReplace,(unsigned char*) &one,  1);

XWMHints hints; hints.input = True; hints.flags = InputHint; XSetWMHints(x_display, win, &hints); XMapWindow ( x_display , win ); // make the window visible on the screen XStoreName ( x_display , win , "GL test" ); Atom wm_state = XInternAtom ( x_display, "_NET_WM_STATE", False ); Atom fullscreen = XInternAtom ( x_display, "_NET_WM_STATE_FULLSCREEN", False );

XEvent xev; memset ( &xev, 0, sizeof(xev) );

xev.type = ClientMessage; xev.xclient.window = win; xev.xclient.message_type = wm_state; xev.xclient.format = 32; xev.xclient.data.l[0] = 1; xev.xclient.data.l[1] = fullscreen; XSendEvent ( // send an event mask to the X-server x_display, DefaultRootWindow ( x_display ), False, SubstructureNotifyMask, &xev );

//SET UP EGL

egl_display  =  eglGetDisplay( (EGLNativeDisplayType) x_display );  

if ( egl_display == EGL_NO_DISPLAY ) { std::cerr << "Got no EGL display." << std::endl; return 1; }

EGLint major, minor; if ( !eglInitialize( egl_display, &major, &minor ) ) { std::cerr << "Unable to initialize EGL" << std::endl; return 1; }
std::cout<<"The major vesion is "<<major<<std::endl; std::cout<<"The minor vesion is "<<minor<<std::endl;

EGLint attr[] = { EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_NONE };

EGLConfig ecfg; EGLint num_config; if ( !eglChooseConfig( egl_display, attr, &ecfg, 1, &num_config ) ) { std::cerr << "Failed to choose config (eglError: " << eglGetError() << ")" << std ::endl; return 1; }

if ( num_config != 1 ) { std::cerr << "Didn't get exactly one config, but " << num_config << std::endl; return 1; } if(!eglBindAPI (EGL_OPENGL_API)) { std::cerr<<"Bind EGL_OPENGL_API failed ,crying!"<<std::endl; return 1; } egl_surface = eglCreateWindowSurface ( egl_display, ecfg, win, NULL ); if ( egl_surface == EGL_NO_SURFACE ) { std::cerr << "Unable to create EGL surface (eglError: " << eglGetError() << ")" << std::endl; return 1; }

EGLint ctxattr[] = { EGL_CONTEXT_MAJOR_VERSION, 4, EGL_CONTEXT_MINOR_VERSION, 5, EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR, EGL_NONE };

egl_context = eglCreateContext ( egl_display, ecfg, EGL_NO_CONTEXT, ctxattr ); if ( egl_context == EGL_NO_CONTEXT ) { std::cerr << "Unable to create EGL context (eglError: " << eglGetError() << ")" << std::endl; return 1; }

eglMakeCurrent( egl_display, egl_surface, egl_surface, egl_context );

std::cout<<"I am above glew!!"<<std::endl; glewExperimental = GL_TRUE; std::cout<<glGetError()<<std::endl; std::cout<<"I am between glewExperimental and glewInit()"<<std::endl; glewInit(); std::cout<<glGetError()<<std::endl; std::cout<<"I am below glew!!"<<std::endl; std::cout<<"The OpenGL version is :"<<glGetString(GL_VERSION)<<std::endl;

bool quit=false; while(!quit) { while(XPending(x_display)) { XEvent xev; XNextEvent(x_display,&xev); if(xev.type == KeyPress) { quit = true; } }

   static XWindowAttributes gwa;
   XGetWindowAttributes(x_display,win,&gwa);
   glClearColor(0.8f, 0.5f, 0.4f, 1.0f);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glViewport(0,0,gwa.width,gwa.height);

   eglSwapBuffers(egl_display,egl_surface);

}

eglDestroyContext ( egl_display, egl_context ); eglDestroySurface ( egl_display, egl_surface ); eglTerminate ( egl_display ); XDestroyWindow ( x_display, win ); XCloseDisplay ( x_display );

return 0;

}

build g++ X11eglGLewSimple.cpp -o X11eglGLewSimple.out -lGLEW -lGL -lX11 -lEGL

But I got the segment bellows when entering glewInit(),I don't know how to deal with it . sg

I found a strange thingthat my code could be executed successfully when I used Nvidia even though I used glew 1.13. But I encountered segment fault when using integrated card.Can someone help me?give me some tips or documents or something else ,or should I give more information.thank you very much.

nigels-com commented 6 years ago

There were some EGL fixes for GLEW 2.1.0. Could you give that a try?

glew.sourceforge.net

lugui2009 commented 6 years ago

@nigels-com I have also tried 2.1.0. However I got the same error.

nigels-com commented 6 years ago

Did you have any luck running glewinfo on that GPU/driver?

This is what I get on an Intel GPU:

$ LD_LIBRARY_PATH=`pwd`/lib DISPLAY=:0 bin/glewinfo
---------------------------
    GLEW Extension Info
---------------------------

GLEW version 2.1.0
Running on a Mesa DRI Intel(R) Iris Pro Graphics 580 (Skylake GT4e)  from Intel Open Source Technology Center
OpenGL version 3.0 Mesa 17.0.7 is supported

GL_VERSION_1_1:                                                OK
---------------

GL_VERSION_1_2:                                                OK
---------------
  glCopyTexSubImage3D:                                         OK
  glDrawRangeElements:                                         OK
  glTexImage3D:                                                OK
  glTexSubImage3D:                                             OK
...
nigels-com commented 6 years ago

For the attached test code, I get a context creation failure. I tried xfce and Gnome on Ubuntu 17.10

The major vesion is 1
The minor vesion is 4
Unable to create EGL context (eglError: 12297)
nigels-com commented 6 years ago

The test program worked for me with a small change to request OpenGL 2.1:

EGL_CONTEXT_MAJOR_VERSION, 2,
EGL_CONTEXT_MINOR_VERSION, 1,