4 Replies Latest reply on Jun 6, 2013 5:36 AM by maxis11

    XCloseDisplay and clGetPlatformIDs

    maxis11

      i catch segmentation fault in

      XCloseDisplay(display);
      

      If before it i call

      clGetPlatformIDs(0, NULL, &numPlatforms);
      

      I'm doing something wrong, or it's a bug?

        • Re: XCloseDisplay and clGetPlatformIDs
          himanshu.gautam

          Can you provide a small test-code. I am not very familiar with xcloseDisplay().

          Also provide your GPU, CPU, Driver,OS, and SDK version.

            • Re: XCloseDisplay and clGetPlatformIDs
              maxis11

              Example:

              #include <stdio.h>

              #include <stdlib.h>

              #include <string.h>

              #include <unistd.h>

              #include <X11/X.h>

              #include <X11/Xlib.h>

              #include <X11/Xutil.h>

              #include <GL/gl.h>

              #include <GL/glx.h>

              #include <CL/cl.h>

              #include <CL/cl_gl.h>

               

              #define GLX_CONTEXT_MAJOR_VERSION_ARB       0x2091

              #define GLX_CONTEXT_MINOR_VERSION_ARB       0x2092

              typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);

               

              static bool isExtensionSupported(const char *extList, const char *extension)

              {

                const char *start;

                const char *where, *terminator;

                where = strchr(extension, ' ');

                if ( where || *extension == '\0' )

                  return false;

                for ( start = extList; ; ) {

                  where = strstr( start, extension );

               

                  if ( !where )

                    break;

               

                  terminator = where + strlen( extension );

               

                  if ( where == start || *(where - 1) == ' ' )

                    if ( *terminator == ' ' || *terminator == '\0' )

                      return true;

               

                  start = terminator;

                }

               

                return false;

              }

               

              static bool ctxErrorOccurred = false;

              static int ctxErrorHandler( Display *dpy, XErrorEvent *ev )

              {

                  ctxErrorOccurred = true;

                  return 0;

              }

               

              int main (int argc, char ** argv)

              {

                Display *display = XOpenDisplay(0);

                if ( !display )

                {

                  printf( "Failed to open X display\n" );

                  exit(1);

                }

                static int visual_attribs[] =

                  {

                    GLX_X_RENDERABLE    , True,

                    GLX_DRAWABLE_TYPE   , GLX_WINDOW_BIT,

                    GLX_RENDER_TYPE     , GLX_RGBA_BIT,

                    GLX_X_VISUAL_TYPE   , GLX_TRUE_COLOR,

                    GLX_RED_SIZE        , 8,

                    GLX_GREEN_SIZE      , 8,

                    GLX_BLUE_SIZE       , 8,

                    GLX_ALPHA_SIZE      , 8,

                    GLX_DEPTH_SIZE      , 24,

                    GLX_STENCIL_SIZE    , 8,

                    GLX_DOUBLEBUFFER    , True,

                    None

                  };

                int glx_major, glx_minor;

                if ( !glXQueryVersion( display, &glx_major, &glx_minor ) ||

                     ( ( glx_major == 1 ) && ( glx_minor < 3 ) ) || ( glx_major < 1 ) )

                {

                  printf( "Invalid GLX version" );

                  exit(1);

                }

                printf( "Getting matching framebuffer configs\n" );

                int fbcount;

                GLXFBConfig *fbc = glXChooseFBConfig( display, DefaultScreen( display ), visual_attribs, &fbcount );

                if ( !fbc )

                {

                  printf( "Failed to retrieve a framebuffer config\n" );

                  exit(1);

                }

                printf( "Found %d matching FB configs.\n", fbcount );

                printf( "Getting XVisualInfos\n" );

                int best_fbc = -1, worst_fbc = -1, best_num_samp = -1, worst_num_samp = 999;

                int i;

                for ( i = 0; i < fbcount; i++ )

                {

                  XVisualInfo *vi = glXGetVisualFromFBConfig( display, fbc[i] );

                  if ( vi )

                  {

                    int samp_buf, samples;

                    glXGetFBConfigAttrib( display, fbc[i], GLX_SAMPLE_BUFFERS, &samp_buf );

                    glXGetFBConfigAttrib( display, fbc[i], GLX_SAMPLES       , &samples  );

               

                    if ( best_fbc < 0 || samp_buf && samples > best_num_samp )

                      best_fbc = i, best_num_samp = samples;

                    if ( worst_fbc < 0 || !samp_buf || samples < worst_num_samp )

                      worst_fbc = i, worst_num_samp = samples;

                  }

                  XFree( vi );

                }

                GLXFBConfig bestFbc = fbc[ best_fbc ];

                XFree( fbc );

                XVisualInfo *vi = glXGetVisualFromFBConfig( display, bestFbc );

                printf( "Creating colormap\n" );

                XSetWindowAttributes swa;

                Colormap cmap;

                swa.colormap = cmap = XCreateColormap( display,

                                                       RootWindow( display, vi->screen ),

                                                       vi->visual, AllocNone );

                swa.background_pixmap = None ;

                swa.border_pixel      = 0;

                swa.event_mask        = StructureNotifyMask;

                printf( "Creating window\n" );

                Window win = XCreateWindow( display, RootWindow( display, vi->screen ),

                                            0, 0, 800, 640, 0, vi->depth, InputOutput,

                                            vi->visual,

                                            CWBorderPixel|CWColormap|CWEventMask, &swa );

                if ( !win )

                {

                  printf( "Failed to create window.\n" );

                  exit(1);

                }

                XFree( vi );

                XStoreName( display, win, "GL 4.2 Window" );

                printf( "Mapping window\n" );

                XMapWindow( display, win );

                const char *glxExts = glXQueryExtensionsString( display,DefaultScreen( display ) );

                glXCreateContextAttribsARBProc glXCreateContextAttribsARB = 0;

                glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)glXGetProcAddressARB( (const GLubyte *) "glXCreateContextAttribsARB" );

                GLXContext ctx = 0;

                ctxErrorOccurred = false;

                int (*oldHandler)(Display*, XErrorEvent*) =

                    XSetErrorHandler(&ctxErrorHandler);

                if ( !isExtensionSupported( glxExts, "GLX_ARB_create_context" ) ||

                     !glXCreateContextAttribsARB )

                {

                  printf( "glXCreateContextAttribsARB() not found"

                          " ... using old-style GLX context\n" );

                  ctx = glXCreateNewContext( display, bestFbc, GLX_RGBA_TYPE, 0, True );

                }

                else

                {

                  int context_attribs[] =

                    {

                      GLX_CONTEXT_MAJOR_VERSION_ARB, 4,

                      GLX_CONTEXT_MINOR_VERSION_ARB, 2,

                      None

                    };

                  printf( "Creating context\n" );

                  ctx = glXCreateContextAttribsARB( display, bestFbc, 0, True, context_attribs );

                  XSync( display, False );

                  if ( !ctxErrorOccurred && ctx )

                    printf( "Created GL 4.2 context\n" );

                  else

                  {

                    context_attribs[1] = 1;

                    context_attribs[3] = 0;

                    ctxErrorOccurred = false;

               

                    printf( "Failed to create GL 4.2 context"

                            " ... using old-style GLX context\n" );

                    ctx = glXCreateContextAttribsARB( display, bestFbc, 0,

                                                      True, context_attribs );

                  }

                }

                XSync( display, False );

                XSetErrorHandler( oldHandler );

               

                if ( ctxErrorOccurred || !ctx )

                {

                  printf( "Failed to create an OpenGL context\n" );

                  exit(1);

                }

                if ( ! glXIsDirect ( display, ctx ) )

                {

                  printf( "Indirect GLX rendering context obtained\n" );

                }

                else

                {

                  printf( "Direct GLX rendering context obtained\n" );

                }

                printf( "Making context current\n" );

                glXMakeCurrent( display, win, ctx );

                cl_int err;

                cl_uint numPlatforms;

                err=clGetPlatformIDs(0, NULL, &numPlatforms); //error here

                if (err!=CL_SUCCESS)

                {

                XDestroyWindow( display, win );

                XFreeColormap( display, cmap );

                XCloseDisplay( display );

                exit (1);

                }

                glClearColor ( 0, 0.5, 1, 1 );

                glClear ( GL_COLOR_BUFFER_BIT );

                glXSwapBuffers ( display, win );

                sleep( 1 );

                glClearColor ( 1, 0.5, 0, 1 );

                glClear ( GL_COLOR_BUFFER_BIT );

                glXSwapBuffers ( display, win );

                sleep( 1 );

                glXMakeCurrent( display, 0, 0 );

                glXDestroyContext( display, ctx );

                XDestroyWindow( display, win );

                XFreeColormap( display, cmap );

                XCloseDisplay( display );

                return 0;

              }

              libs : -lX11 -lGL -lOpenCL

               

              CPU: A4

              GPU: HD 6480G + 7470M

              Catalyst : 13.6

              OS: Ubuntu 13.04

              APP SDK: 2.8