Merge branch 'gallium-0.1' into gallium-tex-surfaces
[mesa.git] / progs / xdemos / glxinfo.c
index 86959de19c6d8087fe22ec8f3c64869cb9534c5f..3e8e0be52031c67ab88a63ae2bf51f923d635e58 100644 (file)
@@ -1,7 +1,5 @@
-/* $Id: glxinfo.c,v 1.8 2000/04/22 20:31:23 brianp Exp $ */
-
 /*
- * Copyright (C) 1999  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  *  -t                     print wide table
  *  -v                     print verbose information
  *  -display DisplayName   specify the X display to interogate
+ *  -b                     only print ID of "best" visual on screen 0
+ *  -i                     use indirect rendering connection only
+ *  -l                     print interesting OpenGL limits (added 5 Sep 2002)
  *
  * Brian Paul  26 January 2000
  */
 
+#define GLX_GLXEXT_PROTOTYPES
 
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
 #include <GL/gl.h>
-#include <GL/glu.h>
 #include <GL/glx.h>
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
+
+
+#ifndef GLX_NONE_EXT
+#define GLX_NONE_EXT  0x8000
+#endif
+
+#ifndef GLX_TRANSPARENT_RGB
+#define GLX_TRANSPARENT_RGB 0x8008
+#endif
 
 
 typedef enum
@@ -62,7 +73,12 @@ struct visual_attribs
 
    /* GL visual attribs */
    int supportsGL;
-   int transparent;
+   int transparentType;
+   int transparentRedValue;
+   int transparentGreenValue;
+   int transparentBlueValue;
+   int transparentAlphaValue;
+   int transparentIndexValue;
    int bufferSize;
    int level;
    int rgba;
@@ -131,7 +147,228 @@ print_extension_list(const char *ext)
 
 
 static void
-print_screen_info(Display *dpy, int scrnum)
+print_display_info(Display *dpy)
+{
+   printf("name of display: %s\n", DisplayString(dpy));
+}
+
+
+/**
+ * Print interesting limits for vertex/fragment programs.
+ */
+static void
+print_program_limits(GLenum target)
+{
+#if defined(GL_ARB_vertex_program) || defined(GL_ARB_fragment_program)
+   struct token_name {
+      GLenum token;
+      const char *name;
+   };
+   static const struct token_name limits[] = {
+      { GL_MAX_PROGRAM_INSTRUCTIONS_ARB, "GL_MAX_PROGRAM_INSTRUCTIONS_ARB" },
+      { GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, "GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB" },
+      { GL_MAX_PROGRAM_TEMPORARIES_ARB, "GL_MAX_PROGRAM_TEMPORARIES_ARB" },
+      { GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB, "GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB" },
+      { GL_MAX_PROGRAM_PARAMETERS_ARB, "GL_MAX_PROGRAM_PARAMETERS_ARB" },
+      { GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB, "GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB" },
+      { GL_MAX_PROGRAM_ATTRIBS_ARB, "GL_MAX_PROGRAM_ATTRIBS_ARB" },
+      { GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB, "GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB" },
+      { GL_MAX_PROGRAM_ADDRESS_REGISTERS_ARB, "GL_MAX_PROGRAM_ADDRESS_REGISTERS_ARB" },
+      { GL_MAX_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB, "GL_MAX_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB" },
+      { GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, "GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB" },
+      { GL_MAX_PROGRAM_ENV_PARAMETERS_ARB, "GL_MAX_PROGRAM_ENV_PARAMETERS_ARB" },
+      { GL_MAX_PROGRAM_ALU_INSTRUCTIONS_ARB, "GL_MAX_PROGRAM_ALU_INSTRUCTIONS_ARB" },
+      { GL_MAX_PROGRAM_TEX_INSTRUCTIONS_ARB, "GL_MAX_PROGRAM_TEX_INSTRUCTIONS_ARB" },
+      { GL_MAX_PROGRAM_TEX_INDIRECTIONS_ARB, "GL_MAX_PROGRAM_TEX_INDIRECTIONS_ARB" },
+      { GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, "GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB" },
+      { GL_MAX_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB, "GL_MAX_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB" },
+      { GL_MAX_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB, "GL_MAX_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB" },
+      { (GLenum) 0, NULL }
+   };
+   PFNGLGETPROGRAMIVARBPROC GetProgramivARB_func = (PFNGLGETPROGRAMIVARBPROC)
+      glXGetProcAddressARB((GLubyte *) "glGetProgramivARB");
+   GLint max[1];
+   int i;
+
+   if (target == GL_VERTEX_PROGRAM_ARB) {
+      printf("    GL_VERTEX_PROGRAM_ARB:\n");
+   }
+   else if (target == GL_FRAGMENT_PROGRAM_ARB) {
+      printf("    GL_FRAGMENT_PROGRAM_ARB:\n");
+   }
+   else {
+      return; /* something's wrong */
+   }
+
+   for (i = 0; limits[i].token; i++) {
+      GetProgramivARB_func(target, limits[i].token, max);
+      if (glGetError() == GL_NO_ERROR) {
+         printf("        %s = %d\n", limits[i].name, max[0]);
+      }
+   }
+#endif /* GL_ARB_vertex_program / GL_ARB_fragment_program */
+}
+
+
+/**
+ * Print interesting limits for vertex/fragment shaders.
+ */
+static void
+print_shader_limits(GLenum target)
+{
+   struct token_name {
+      GLenum token;
+      const char *name;
+   };
+#if defined(GL_ARB_vertex_shader)
+   static const struct token_name vertex_limits[] = {
+      { GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB, "GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB" },
+      { GL_MAX_VARYING_FLOATS_ARB, "GL_MAX_VARYING_FLOATS_ARB" },
+      { GL_MAX_VERTEX_ATTRIBS_ARB, "GL_MAX_VERTEX_ATTRIBS_ARB" },
+      { GL_MAX_TEXTURE_IMAGE_UNITS_ARB, "GL_MAX_TEXTURE_IMAGE_UNITS_ARB" },
+      { GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB, "GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB" },
+      { GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS_ARB, "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS_ARB" },
+      { GL_MAX_TEXTURE_COORDS_ARB, "GL_MAX_TEXTURE_COORDS_ARB" },
+      { (GLenum) 0, NULL }
+   };
+#endif
+#if defined(GL_ARB_fragment_shader)
+   static const struct token_name fragment_limits[] = {
+      { GL_MAX_FRAGMENT_UNIFORM_COMPONENTS_ARB, "GL_MAX_FRAGMENT_UNIFORM_COMPONENTS_ARB" },
+      { GL_MAX_TEXTURE_COORDS_ARB, "GL_MAX_TEXTURE_COORDS_ARB" },
+      { GL_MAX_TEXTURE_IMAGE_UNITS_ARB, "GL_MAX_TEXTURE_IMAGE_UNITS_ARB" },
+      { (GLenum) 0, NULL }
+   };
+#endif
+   GLint max[1];
+   int i;
+
+#if defined(GL_ARB_vertex_shader)
+   if (target == GL_VERTEX_SHADER_ARB) {
+      printf("    GL_VERTEX_SHADER_ARB:\n");
+      for (i = 0; vertex_limits[i].token; i++) {
+         glGetIntegerv(vertex_limits[i].token, max);
+         if (glGetError() == GL_NO_ERROR) {
+            printf("        %s = %d\n", vertex_limits[i].name, max[0]);
+         }
+      }
+   }
+#endif
+#if defined(GL_ARB_fragment_shader)
+   if (target == GL_FRAGMENT_SHADER_ARB) {
+      printf("    GL_FRAGMENT_SHADER_ARB:\n");
+      for (i = 0; fragment_limits[i].token; i++) {
+         glGetIntegerv(fragment_limits[i].token, max);
+         if (glGetError() == GL_NO_ERROR) {
+            printf("        %s = %d\n", fragment_limits[i].name, max[0]);
+         }
+      }
+   }
+#endif
+}
+
+
+/**
+ * Print interesting OpenGL implementation limits.
+ */
+static void
+print_limits(const char *extensions)
+{
+   struct token_name {
+      GLuint count;
+      GLenum token;
+      const char *name;
+   };
+   static const struct token_name limits[] = {
+      { 1, GL_MAX_ATTRIB_STACK_DEPTH, "GL_MAX_ATTRIB_STACK_DEPTH" },
+      { 1, GL_MAX_CLIENT_ATTRIB_STACK_DEPTH, "GL_MAX_CLIENT_ATTRIB_STACK_DEPTH" },
+      { 1, GL_MAX_CLIP_PLANES, "GL_MAX_CLIP_PLANES" },
+      { 1, GL_MAX_COLOR_MATRIX_STACK_DEPTH, "GL_MAX_COLOR_MATRIX_STACK_DEPTH" },
+      { 1, GL_MAX_ELEMENTS_VERTICES, "GL_MAX_ELEMENTS_VERTICES" },
+      { 1, GL_MAX_ELEMENTS_INDICES, "GL_MAX_ELEMENTS_INDICES" },
+      { 1, GL_MAX_EVAL_ORDER, "GL_MAX_EVAL_ORDER" },
+      { 1, GL_MAX_LIGHTS, "GL_MAX_LIGHTS" },
+      { 1, GL_MAX_LIST_NESTING, "GL_MAX_LIST_NESTING" },
+      { 1, GL_MAX_MODELVIEW_STACK_DEPTH, "GL_MAX_MODELVIEW_STACK_DEPTH" },
+      { 1, GL_MAX_NAME_STACK_DEPTH, "GL_MAX_NAME_STACK_DEPTH" },
+      { 1, GL_MAX_PIXEL_MAP_TABLE, "GL_MAX_PIXEL_MAP_TABLE" },
+      { 1, GL_MAX_PROJECTION_STACK_DEPTH, "GL_MAX_PROJECTION_STACK_DEPTH" },
+      { 1, GL_MAX_TEXTURE_STACK_DEPTH, "GL_MAX_TEXTURE_STACK_DEPTH" },
+      { 1, GL_MAX_TEXTURE_SIZE, "GL_MAX_TEXTURE_SIZE" },
+      { 1, GL_MAX_3D_TEXTURE_SIZE, "GL_MAX_3D_TEXTURE_SIZE" },
+      { 2, GL_MAX_VIEWPORT_DIMS, "GL_MAX_VIEWPORT_DIMS" },
+      { 2, GL_ALIASED_LINE_WIDTH_RANGE, "GL_ALIASED_LINE_WIDTH_RANGE" },
+      { 2, GL_SMOOTH_LINE_WIDTH_RANGE, "GL_SMOOTH_LINE_WIDTH_RANGE" },
+      { 2, GL_ALIASED_POINT_SIZE_RANGE, "GL_ALIASED_POINT_SIZE_RANGE" },
+      { 2, GL_SMOOTH_POINT_SIZE_RANGE, "GL_SMOOTH_POINT_SIZE_RANGE" },
+#if defined(GL_ARB_texture_cube_map)
+      { 1, GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB, "GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB" },
+#endif
+#if defined(GLX_NV_texture_rectangle)
+      { 1, GL_MAX_RECTANGLE_TEXTURE_SIZE_NV, "GL_MAX_RECTANGLE_TEXTURE_SIZE_NV" },
+#endif
+#if defined(GL_ARB_texture_compression)
+      { 1, GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB, "GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB" },
+#endif
+#if defined(GL_ARB_multitexture)
+      { 1, GL_MAX_TEXTURE_UNITS_ARB, "GL_MAX_TEXTURE_UNITS_ARB" },
+#endif
+#if defined(GL_EXT_texture_lod_bias)
+      { 1, GL_MAX_TEXTURE_LOD_BIAS_EXT, "GL_MAX_TEXTURE_LOD_BIAS_EXT" },
+#endif
+#if defined(GL_EXT_texture_filter_anisotropic)
+      { 1, GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, "GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT" },
+#endif
+#if defined(GL_ARB_draw_buffers)
+      { 1, GL_MAX_DRAW_BUFFERS_ARB, "GL_MAX_DRAW_BUFFERS_ARB" },
+#endif
+      { 0, (GLenum) 0, NULL }
+   };
+   GLint i, max[2];
+
+   printf("OpenGL limits:\n");
+   for (i = 0; limits[i].count; i++) {
+      glGetIntegerv(limits[i].token, max);
+      if (glGetError() == GL_NO_ERROR) {
+         if (limits[i].count == 1)
+            printf("    %s = %d\n", limits[i].name, max[0]);
+         else /* XXX fix if we ever query something with more than 2 values */
+            printf("    %s = %d, %d\n", limits[i].name, max[0], max[1]);
+      }
+   }
+
+   /* these don't fit into the above mechanism, unfortunately */
+   glGetConvolutionParameteriv(GL_CONVOLUTION_2D, GL_MAX_CONVOLUTION_WIDTH, max);
+   glGetConvolutionParameteriv(GL_CONVOLUTION_2D, GL_MAX_CONVOLUTION_HEIGHT, max+1);
+   if (glGetError() == GL_NONE) {
+      printf("    GL_MAX_CONVOLUTION_WIDTH/HEIGHT = %d, %d\n", max[0], max[1]);
+   }
+
+#if defined(GL_ARB_vertex_program)
+   if (strstr(extensions, "GL_ARB_vertex_program")) {
+      print_program_limits(GL_VERTEX_PROGRAM_ARB);
+   }
+#endif
+#if defined(GL_ARB_fragment_program)
+   if (strstr(extensions, "GL_ARB_fragment_program")) {
+      print_program_limits(GL_FRAGMENT_PROGRAM_ARB);
+   }
+#endif
+#if defined(GL_ARB_vertex_shader)
+   if (strstr(extensions, "GL_ARB_vertex_shader")) {
+      print_shader_limits(GL_VERTEX_SHADER_ARB);
+   }
+#endif
+#if defined(GL_ARB_fragment_shader)
+   if (strstr(extensions, "GL_ARB_fragment_shader")) {
+      print_shader_limits(GL_FRAGMENT_SHADER_ARB);
+   }
+#endif
+}
+
+
+static void
+print_screen_info(Display *dpy, int scrnum, Bool allowDirect, GLboolean limits)
 {
    Window win;
    int attribSingle[] = {
@@ -161,27 +398,29 @@ print_screen_info(Display *dpy, int scrnum)
    if (!visinfo) {
       visinfo = glXChooseVisual(dpy, scrnum, attribDouble);
       if (!visinfo) {
-         fprintf(stderr, "Error: couldn't find RGB GLX visual!\n");
+         fprintf(stderr, "Error: couldn't find RGB GLX visual\n");
          return;
       }
    }
 
    attr.background_pixel = 0;
    attr.border_pixel = 0;
-   attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
+   attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
    attr.event_mask = StructureNotifyMask | ExposureMask;
    mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
    win = XCreateWindow(dpy, root, 0, 0, width, height,
                       0, visinfo->depth, InputOutput,
                       visinfo->visual, mask, &attr);
 
-   ctx = glXCreateContext( dpy, visinfo, NULL, True );
+   ctx = glXCreateContext( dpy, visinfo, NULL, allowDirect );
    if (!ctx) {
+      fprintf(stderr, "Error: glXCreateContext failed\n");
+      XFree(visinfo);
       XDestroyWindow(dpy, win);
       return;
    }
 
-   if (glXMakeCurrent( dpy, win, ctx )) {
+   if (glXMakeCurrent(dpy, win, ctx)) {
       const char *serverVendor = glXQueryServerString(dpy, scrnum, GLX_VENDOR);
       const char *serverVersion = glXQueryServerString(dpy, scrnum, GLX_VERSION);
       const char *serverExtensions = glXQueryServerString(dpy, scrnum, GLX_EXTENSIONS);
@@ -193,9 +432,43 @@ print_screen_info(Display *dpy, int scrnum)
       const char *glRenderer = (const char *) glGetString(GL_RENDERER);
       const char *glVersion = (const char *) glGetString(GL_VERSION);
       const char *glExtensions = (const char *) glGetString(GL_EXTENSIONS);
-      const char *gluVersion = (const char *) gluGetString(GLU_VERSION);
-      const char *gluExtensions = (const char *) gluGetString(GLU_EXTENSIONS);
-      printf("display: %s  screen:%d\n", DisplayString(dpy), scrnum);
+      int glxVersionMajor;
+      int glxVersionMinor;
+      char *displayName = NULL;
+      char *colon = NULL, *period = NULL;
+      
+      if (! glXQueryVersion( dpy, & glxVersionMajor, & glxVersionMinor )) {
+         fprintf(stderr, "Error: glXQueryVersion failed\n");
+         exit(1);
+      }
+
+      /* Strip the screen number from the display name, if present. */
+      if (!(displayName = (char *) malloc(strlen(DisplayString(dpy)) + 1))) {
+         fprintf(stderr, "Error: malloc() failed\n");
+         exit(1);
+      }
+      strcpy(displayName, DisplayString(dpy));
+      colon = strrchr(displayName, ':');
+      if (colon) {
+         period = strchr(colon, '.');
+         if (period)
+            *period = '\0';
+      }
+      printf("display: %s  screen: %d\n", displayName, scrnum);
+      free(displayName);
+      printf("direct rendering: ");
+      if (glXIsDirect(dpy, ctx)) {
+         printf("Yes\n");
+      } else {
+         if (!allowDirect) {
+            printf("No (-i specified)\n");
+         } else if (getenv("LIBGL_ALWAYS_INDIRECT")) {
+            printf("No (LIBGL_ALWAYS_INDIRECT set)\n");
+         } else {
+            printf("No (If you want to find out why, try setting "
+                   "LIBGL_DEBUG=verbose)\n");
+         }
+      }
       printf("server glx vendor string: %s\n", serverVendor);
       printf("server glx version string: %s\n", serverVersion);
       printf("server glx extensions:\n");
@@ -204,6 +477,7 @@ print_screen_info(Display *dpy, int scrnum)
       printf("client glx version string: %s\n", clientVersion);
       printf("client glx extensions:\n");
       print_extension_list(clientExtensions);
+      printf("GLX version: %u.%u\n", glxVersionMajor, glxVersionMinor);
       printf("GLX extensions:\n");
       print_extension_list(glxExtensions);
       printf("OpenGL vendor string: %s\n", glVendor);
@@ -211,12 +485,15 @@ print_screen_info(Display *dpy, int scrnum)
       printf("OpenGL version string: %s\n", glVersion);
       printf("OpenGL extensions:\n");
       print_extension_list(glExtensions);
-      printf("glu version: %s\n", gluVersion);
-      printf("glu extensions:\n");
-      print_extension_list(gluExtensions);
+      if (limits)
+         print_limits(glExtensions);
+   }
+   else {
+      fprintf(stderr, "Error: glXMakeCurrent failed\n");
    }
 
    glXDestroyContext(dpy, ctx);
+   XFree(visinfo);
    XDestroyWindow(dpy, win);
 }
 
@@ -305,12 +582,29 @@ get_visual_attribs(Display *dpy, XVisualInfo *vInfo,
    glXGetConfig(dpy, vInfo, GLX_ACCUM_BLUE_SIZE, &attribs->accumBlueSize);
    glXGetConfig(dpy, vInfo, GLX_ACCUM_ALPHA_SIZE, &attribs->accumAlphaSize);
 
-   /* transparent pixel value not implemented yet */
-   attribs->transparent = 0;
+   /* get transparent pixel stuff */
+   glXGetConfig(dpy, vInfo,GLX_TRANSPARENT_TYPE, &attribs->transparentType);
+   if (attribs->transparentType == GLX_TRANSPARENT_RGB) {
+     glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_RED_VALUE, &attribs->transparentRedValue);
+     glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_GREEN_VALUE, &attribs->transparentGreenValue);
+     glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_BLUE_VALUE, &attribs->transparentBlueValue);
+     glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_ALPHA_VALUE, &attribs->transparentAlphaValue);
+   }
+   else if (attribs->transparentType == GLX_TRANSPARENT_INDEX) {
+     glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_INDEX_VALUE, &attribs->transparentIndexValue);
+   }
 
-   /* multisample tests not implemented yet */
-   attribs->numSamples = 0;
-   attribs->numMultisample = 0;
+   /* multisample attribs */
+#ifdef GLX_ARB_multisample
+   if (ext && strstr(ext, "GLX_ARB_multisample") == 0) {
+      glXGetConfig(dpy, vInfo, GLX_SAMPLE_BUFFERS_ARB, &attribs->numMultisample);
+      glXGetConfig(dpy, vInfo, GLX_SAMPLES_ARB, &attribs->numSamples);
+   }
+#endif
+   else {
+      attribs->numSamples = 0;
+      attribs->numMultisample = 0;
+   }
 
 #if defined(GLX_EXT_visual_rating)
    if (ext && strstr(ext, "GLX_EXT_visual_rating")) {
@@ -351,7 +645,15 @@ print_visual_attribs_verbose(const struct visual_attribs *attribs)
    else if (attribs->visualCaveat == GLX_NON_CONFORMANT_VISUAL_EXT)
       printf("    visualCaveat=Nonconformant\n");
 #endif
-   printf("    %s\n", attribs->transparent ? "Transparent." : "Opaque.");
+   if (attribs->transparentType == GLX_NONE) {
+     printf("    Opaque.\n");
+   }
+   else if (attribs->transparentType == GLX_TRANSPARENT_RGB) {
+     printf("    Transparent RGB: Red=%d Green=%d Blue=%d Alpha=%d\n",attribs->transparentRedValue,attribs->transparentGreenValue,attribs->transparentBlueValue,attribs->transparentAlphaValue);
+   }
+   else if (attribs->transparentType == GLX_TRANSPARENT_INDEX) {
+     printf("    Transparent index=%d\n",attribs->transparentIndexValue);
+   }
 }
 
 
@@ -367,7 +669,7 @@ print_visual_attribs_short_header(void)
 static void
 print_visual_attribs_short(const struct visual_attribs *attribs)
 {
-   char *caveat;
+   char *caveat = NULL;
 #ifdef GLX_EXT_visual_rating
    if (attribs->visualCaveat == GLX_NONE_EXT || attribs->visualCaveat == 0)
       caveat = "None";
@@ -375,6 +677,8 @@ print_visual_attribs_short(const struct visual_attribs *attribs)
       caveat = "Slow";
    else if (attribs->visualCaveat == GLX_NON_CONFORMANT_VISUAL_EXT)
       caveat = "Ncon";
+   else
+      caveat = "None";
 #else
    caveat = "None";
 #endif 
@@ -383,7 +687,7 @@ print_visual_attribs_short(const struct visual_attribs *attribs)
           attribs->id,
           attribs->depth,
           visual_class_abbrev(attribs->klass),
-          attribs->transparent,
+          attribs->transparentType != GLX_NONE,
           attribs->bufferSize,
           attribs->level,
           attribs->rgba ? "r" : "c",
@@ -421,7 +725,7 @@ print_visual_attribs_long(const struct visual_attribs *attribs)
           attribs->id,
           attribs->depth,
           visual_class_name(attribs->klass),
-          attribs->transparent,
+          attribs->transparentType != GLX_NONE,
           attribs->bufferSize,
           attribs->level,
           attribs->rgba ? "rgba" : "ci  ",
@@ -445,16 +749,16 @@ print_visual_attribs_long(const struct visual_attribs *attribs)
 static void
 print_visual_info(Display *dpy, int scrnum, InfoMode mode)
 {
-   XVisualInfo template;
+   XVisualInfo theTemplate;
    XVisualInfo *visuals;
    int numVisuals;
    long mask;
    int i;
 
    /* get list of all visuals on this screen */
-   template.screen = scrnum;
+   theTemplate.screen = scrnum;
    mask = VisualScreenMask;
-   visuals = XGetVisualInfo(dpy, mask, &template, &numVisuals);
+   visuals = XGetVisualInfo(dpy, mask, &theTemplate, &numVisuals);
 
    if (mode == Verbose) {
       for (i = 0; i < numVisuals; i++) {
@@ -484,13 +788,122 @@ print_visual_info(Display *dpy, int scrnum, InfoMode mode)
 }
 
 
+/*
+ * Stand-alone Mesa doesn't really implement the GLX protocol so it
+ * doesn't really know the GLX attributes associated with an X visual.
+ * The first time a visual is presented to Mesa's pseudo-GLX it
+ * attaches ancilliary buffers to it (like depth and stencil).
+ * But that usually only works if glXChooseVisual is used.
+ * This function calls glXChooseVisual() to sort of "prime the pump"
+ * for Mesa's GLX so that the visuals that get reported actually
+ * reflect what applications will see.
+ * This has no effect when using true GLX.
+ */
+static void
+mesa_hack(Display *dpy, int scrnum)
+{
+   static int attribs[] = {
+      GLX_RGBA,
+      GLX_RED_SIZE, 1,
+      GLX_GREEN_SIZE, 1,
+      GLX_BLUE_SIZE, 1,
+      GLX_DEPTH_SIZE, 1,
+      GLX_STENCIL_SIZE, 1,
+      GLX_ACCUM_RED_SIZE, 1,
+      GLX_ACCUM_GREEN_SIZE, 1,
+      GLX_ACCUM_BLUE_SIZE, 1,
+      GLX_ACCUM_ALPHA_SIZE, 1,
+      GLX_DOUBLEBUFFER,
+      None
+   };
+   XVisualInfo *visinfo;
+
+   visinfo = glXChooseVisual(dpy, scrnum, attribs);
+   if (visinfo)
+      XFree(visinfo);
+}
+
+
+/*
+ * Examine all visuals to find the so-called best one.
+ * We prefer deepest RGBA buffer with depth, stencil and accum
+ * that has no caveats.
+ */
+static int
+find_best_visual(Display *dpy, int scrnum)
+{
+   XVisualInfo theTemplate;
+   XVisualInfo *visuals;
+   int numVisuals;
+   long mask;
+   int i;
+   struct visual_attribs bestVis;
+
+   /* get list of all visuals on this screen */
+   theTemplate.screen = scrnum;
+   mask = VisualScreenMask;
+   visuals = XGetVisualInfo(dpy, mask, &theTemplate, &numVisuals);
+
+   /* init bestVis with first visual info */
+   get_visual_attribs(dpy, &visuals[0], &bestVis);
+
+   /* try to find a "better" visual */
+   for (i = 1; i < numVisuals; i++) {
+      struct visual_attribs vis;
+
+      get_visual_attribs(dpy, &visuals[i], &vis);
+
+      /* always skip visuals with caveats */
+      if (vis.visualCaveat != GLX_NONE_EXT)
+         continue;
+
+      /* see if this vis is better than bestVis */
+      if ((!bestVis.supportsGL && vis.supportsGL) ||
+          (bestVis.visualCaveat != GLX_NONE_EXT) ||
+          (!bestVis.rgba && vis.rgba) ||
+          (!bestVis.doubleBuffer && vis.doubleBuffer) ||
+          (bestVis.redSize < vis.redSize) ||
+          (bestVis.greenSize < vis.greenSize) ||
+          (bestVis.blueSize < vis.blueSize) ||
+          (bestVis.alphaSize < vis.alphaSize) ||
+          (bestVis.depthSize < vis.depthSize) ||
+          (bestVis.stencilSize < vis.stencilSize) ||
+          (bestVis.accumRedSize < vis.accumRedSize)) {
+         /* found a better visual */
+         bestVis = vis;
+      }
+   }
+
+   XFree(visuals);
+
+   return bestVis.id;
+}
+
+
+static void
+usage(void)
+{
+   printf("Usage: glxinfo [-v] [-t] [-h] [-i] [-b] [-display <dname>]\n");
+   printf("\t-v: Print visuals info in verbose form.\n");
+   printf("\t-t: Print verbose table.\n");
+   printf("\t-display <dname>: Print GLX visuals on specified server.\n");
+   printf("\t-h: This information.\n");
+   printf("\t-i: Force an indirect rendering context.\n");
+   printf("\t-b: Find the 'best' visual and print it's number.\n");
+   printf("\t-l: Print interesting OpenGL limits.\n");
+}
+
+
 int
 main(int argc, char *argv[])
 {
-   char *displayName = ":0";
+   char *displayName = NULL;
    Display *dpy;
    int numScreens, scrnum;
    InfoMode mode = Normal;
+   GLboolean findBest = GL_FALSE;
+   GLboolean limits = GL_FALSE;
+   Bool allowDirect = True;
    int i;
 
    for (i = 1; i < argc; i++) {
@@ -504,21 +917,49 @@ main(int argc, char *argv[])
       else if (strcmp(argv[i], "-v") == 0) {
          mode = Verbose;
       }
+      else if (strcmp(argv[i], "-b") == 0) {
+         findBest = GL_TRUE;
+      }
+      else if (strcmp(argv[i], "-i") == 0) {
+         allowDirect = False;
+      }
+      else if (strcmp(argv[i], "-l") == 0) {
+         limits = GL_TRUE;
+      }
+      else if (strcmp(argv[i], "-h") == 0) {
+         usage();
+         return 0;
+      }
+      else {
+         printf("Unknown option `%s'\n", argv[i]);
+         usage();
+         return 0;
+      }
    }
 
    dpy = XOpenDisplay(displayName);
    if (!dpy) {
-      fprintf(stderr, "Error: unable to open display %s\n", displayName);
+      fprintf(stderr, "Error: unable to open display %s\n", XDisplayName(displayName));
       return -1;
    }
 
-   numScreens = ScreenCount(dpy);
-   for (scrnum = 0; scrnum < numScreens; scrnum++) {
-      print_screen_info(dpy, 0);
-      printf("\n");
-      print_visual_info(dpy, 0, mode);
-      if (scrnum + 1 < numScreens)
-         printf("\n\n");
+   if (findBest) {
+      int b;
+      mesa_hack(dpy, 0);
+      b = find_best_visual(dpy, 0);
+      printf("%d\n", b);
+   }
+   else {
+      numScreens = ScreenCount(dpy);
+      print_display_info(dpy);
+      for (scrnum = 0; scrnum < numScreens; scrnum++) {
+         mesa_hack(dpy, scrnum);
+         print_screen_info(dpy, scrnum, allowDirect, limits);
+         printf("\n");
+         print_visual_info(dpy, scrnum, mode);
+         if (scrnum + 1 < numScreens)
+            printf("\n\n");
+      }
    }
 
    XCloseDisplay(dpy);