GLTexStorage2D crash the program

#1

Hi, I want to use opengl function in the main function to debug my code but when I use those functions in the main function, it crash, I’ve also the same problem with the glBindVertexArray function.

Here is the source code :

Blockquote
#include <GL/glew.h>
#include <SFML/OpenGL.hpp>
#include “odfaeg/Graphics/glCheck.h”
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/gl.h>
#include <GL/glx.h>

#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
typedef GLXContext (glXCreateContextAttribsARBProc)(Display, GLXFBConfig, GLXContext, Bool, const int*);

// Helper to check for extension string presence. Adapted from:
// http://www.opengl.org/resources/features/OGLextensions/
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;:wink: {
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;
}
Display *display = XOpenDisplay(NULL);

if (!display)
{
printf(“Failed to open X display\n”);
exit(1);
}

// Get a matching FB config
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,
//GLX_SAMPLE_BUFFERS , 1,
//GLX_SAMPLES , 4,
None
};

int glx_major, glx_minor;

// FBConfigs were added in GLX version 1.3.
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 );

// Pick the FB config/visual with the most samples per pixel
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 );

  printf( "  Matching fbconfig %d, visual ID 0x%2x: SAMPLE_BUFFERS = %d,"
          " SAMPLES = %d\n",
          i, vi -> visualid, samp_buf, 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 ];

// Be sure to free the FBConfig list allocated by glXChooseFBConfig()
XFree( fbc );

// Get a visual
XVisualInfo *vi = glXGetVisualFromFBConfig( display, bestFbc );
printf( “Chosen visual ID = 0x%x\n”, vi->visualid );

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, 100, 100, 0, vi->depth, InputOutput,
vi->visual,
CWBorderPixel|CWColormap|CWEventMask, &swa );
if ( !win )
{
printf( “Failed to create window.\n” );
exit(1);
}

// Done with the visual info data
XFree( vi );

XStoreName( display, win, “GL 3.0 Window” );

printf( “Mapping window\n” );
XMapWindow( display, win );

// Get the default screen’s GLX extension list
const char *glxExts = glXQueryExtensionsString( display,
DefaultScreen( display ) );

// NOTE: It is not necessary to create or make current to a context before
// calling glXGetProcAddressARB
glXCreateContextAttribsARBProc glXCreateContextAttribsARB = 0;
glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)
glXGetProcAddressARB( (const GLubyte *) “glXCreateContextAttribsARB” );

GLXContext ctx = 0;

// Install an X error handler so the application won’t exit if GL 3.0
// context allocation fails.
//
// Note this error handler is global. All display connections in all threads
// of a process use the same error handler, so be sure to guard against other
// threads issuing X commands while this code is running.
ctxErrorOccurred = false;
int (oldHandler)(Display, XErrorEvent*) =
XSetErrorHandler(&ctxErrorHandler);

// Check for the GLX_ARB_create_context extension string and the function.
// If either is not present, use GLX 1.3 context creation method.
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 );
}

// If it does, try to get a GL 3.0 context!
else
{
int context_attribs[] =
{
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
GLX_CONTEXT_MINOR_VERSION_ARB, 0,
//GLX_CONTEXT_FLAGS_ARB , GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
None
};

printf( "Creating context\n" );
ctx = glXCreateContextAttribsARB( display, bestFbc, 0,
                                  True, context_attribs );

// Sync to ensure any errors generated are processed.
XSync( display, False );
if ( !ctxErrorOccurred && ctx )
  printf( "Created GL 3.0 context\n" );
else
{
  // Couldn't create GL 3.0 context.  Fall back to old-style 2.x context.
  // When a context version below 3.0 is requested, implementations will
  // return the newest context version compatible with OpenGL versions less
  // than version 3.0.
  // GLX_CONTEXT_MAJOR_VERSION_ARB = 1
  context_attribs[1] = 1;
  // GLX_CONTEXT_MINOR_VERSION_ARB = 0
  context_attribs[3] = 0;

  ctxErrorOccurred = false;

  printf( "Failed to create GL 3.0 context"
          " ... using old-style GLX context\n" );
  ctx = glXCreateContextAttribsARB( display, bestFbc, 0,
                                    True, context_attribs );
}

}

// Sync to ensure any errors generated are processed.
XSync( display, False );

// Restore the original error handler
XSetErrorHandler( oldHandler );

if ( ctxErrorOccurred || !ctx )
{
printf( “Failed to create an OpenGL context\n” );
exit(1);
}

// Verifying that context is a direct context
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 );
GLuint maxNodes = 20 * 100 * 100;
GLint nodeSize = 5 * sizeof(GLfloat) + sizeof(GLuint);
GLuint headPtrTex, atomicBuffer, linkedListBuffer, clearBuf, pass1Index, pass2Index;
glCheck(glGenTextures(1, &headPtrTex));
glCheck(glBindTexture(GL_TEXTURE_2D, headPtrTex));
glCheck(glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32UI, 100, 100));

return 0;
}
I don’t go further because it crash at this line, I followed this tutorial for context creation :
https://www.khronos.org/opengl/wiki/Tutorial:OpenGL_3.0_Context_Creation(GLX)

#2

Hi again @laurent7601 Please be sure to format your code using the proper code tags. This will make it a LOT easier for others to read and respond to your code. Also, you mention in your post “it crash at this line” but didn’t include which line. Thank you for understanding.

1 Like
#3

How can I format the code ? I tried bloquote but it format only a part of the code…

#4

As mentioned by @Dark_Photon you will see how to format the code and questions in the Forum Posting Guidelines and I have given you a short how to in your last post:

#5

Try:

printf( "glTexStorage2D = %p", glTexStorage2D );

Is it NULL?

Part of your problem may be that you’re not actually using GLEW (though you’re including the GLEW header).

Using GLEW is easy, and it takes care of all of the setup of OpenGL function pointers and querying for supported GL extensions, giving you a nice, simple interface. See this page for an example:

The alternative to using GLEW is that, depending on your GL driver, you may have to manually query GL function pointers from the driver using glXGetProcAddress().

Just use GLEW. Then you don’t need to do this. It does it all for you.

That works. But personally, I would not recommend creating your own context and window manually, unless you you know that you need to do something unusual. For test programs, I would just use GLUT or GLFW and skip all that needless complexity.

Here’s a simple little test program to show you how easy it is to use GLEW and GLUT:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glut.h>

//-----------------------------------------------------------------------

void checkGLError( const char hdr[] )
{
  int err = glGetError();
  if( err )
  {
    fprintf(stderr, "ERROR %s: %s\n", hdr, gluErrorString(err));
    exit(1);
  }
}

//-----------------------------------------------------------------------

void reshape( int width, int height )
{
  glViewport(0, 0, width, height);
}

//-----------------------------------------------------------------------

void display()
{
  // Clear screen
  glClearColor( 0,0,1,1 );
  glClear( GL_COLOR_BUFFER_BIT );

  // Load up PROJECTION and MODELVIEW
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-2,2,-2,2,-2,2);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  // FIXME: Add GL draw commands here

  // Swap
  glutSwapBuffers();
  glutPostRedisplay();
  checkGLError( "End of display()" );
}

//-----------------------------------------------------------------------

void keyboard( unsigned char key, int x, int y )
{
   switch (key) {
      case 27:
         exit(0);
         break;
   }
}

int main( int argc, char** argv )
{
  // Init GLUT
  glutInit( &argc, argv );
  glutInitDisplayMode(  GLUT_RGBA | GLUT_DOUBLE  );
  glutCreateWindow( argv[0] );

  glutKeyboardFunc( keyboard );
  glutDisplayFunc( display );
  glutReshapeFunc( reshape );

  glutReshapeWindow( 400,400 );

  // Init GLEW
  GLenum err = glewInit();
  if ( err != GLEW_OK )
  {
    // Problem: glewInit failed, something is seriously wrong.
    fprintf( stderr, "Error: %s\n", glewGetErrorString(err) );
    exit(1);
  }

  printf( "GL_RENDERER = %s\n", glGetString( GL_RENDERER) );

  glutMainLoop();
  return 0;
}
#6

Blockquote
Part of your problem may be that you’re not actually using GLEW (though you’re including the GLEW header).
Using GLEW is easy, and it takes care of all of the setup of OpenGL function pointers and querying for supported GL extensions, giving you a nice, simple interface. See this page for an example:

Ha yes I frogot to initialize glew it’s certainly the cause of the crash.

Blockquote
That works. But personally, I would not recommend creating your own context and window manually, unless you you know that you need to do something unusual. For test programs, I would just use GLUT or GLFW and skip all that needless complexity.
Here’s a simple little test program to show you how easy it is to use GLEW and GLUT:

Urmf…, yes previously I used SFML to not manage the opengl context manually and I have even written an interface to switch between libraries without having to change the source code to test multiple libraries. But I had a crash while creating the context when using multiple windows so I encoded my own module and it works.

I have a better understanding when I do things by myself and I often do this if the lib I use doesn’t work, and it’s not the single library I have reinvented.

Once time I even tried to implement my own driver with openCL because mesa source code is complex but opencl context creation fails with my GPU device.