Merge git://proxy01.pd.intel.com:9419/git/mesa/mesa into crestline
[mesa.git] / progs / xdemos / glxinfo.c
1 /*
2 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22
23 /*
24 * This program is a work-alike of the IRIX glxinfo program.
25 * Command line options:
26 * -t print wide table
27 * -v print verbose information
28 * -display DisplayName specify the X display to interogate
29 * -b only print ID of "best" visual on screen 0
30 * -i use indirect rendering connection only
31 * -l print interesting OpenGL limits (added 5 Sep 2002)
32 *
33 * Brian Paul 26 January 2000
34 */
35
36 #define GLX_GLXEXT_PROTOTYPES
37
38 #include <X11/Xlib.h>
39 #include <X11/Xutil.h>
40 #include <GL/gl.h>
41 #include <GL/glx.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
45
46
47 #ifndef GLX_NONE_EXT
48 #define GLX_NONE_EXT 0x8000
49 #endif
50
51 #ifndef GLX_TRANSPARENT_RGB
52 #define GLX_TRANSPARENT_RGB 0x8008
53 #endif
54
55
56 typedef enum
57 {
58 Normal,
59 Wide,
60 Verbose
61 } InfoMode;
62
63
64 struct visual_attribs
65 {
66 /* X visual attribs */
67 int id;
68 int klass;
69 int depth;
70 int redMask, greenMask, blueMask;
71 int colormapSize;
72 int bitsPerRGB;
73
74 /* GL visual attribs */
75 int supportsGL;
76 int transparentType;
77 int transparentRedValue;
78 int transparentGreenValue;
79 int transparentBlueValue;
80 int transparentAlphaValue;
81 int transparentIndexValue;
82 int bufferSize;
83 int level;
84 int rgba;
85 int doubleBuffer;
86 int stereo;
87 int auxBuffers;
88 int redSize, greenSize, blueSize, alphaSize;
89 int depthSize;
90 int stencilSize;
91 int accumRedSize, accumGreenSize, accumBlueSize, accumAlphaSize;
92 int numSamples, numMultisample;
93 int visualCaveat;
94 };
95
96
97 /*
98 * Print a list of extensions, with word-wrapping.
99 */
100 static void
101 print_extension_list(const char *ext)
102 {
103 const char *indentString = " ";
104 const int indent = 4;
105 const int max = 79;
106 int width, i, j;
107
108 if (!ext || !ext[0])
109 return;
110
111 width = indent;
112 printf(indentString);
113 i = j = 0;
114 while (1) {
115 if (ext[j] == ' ' || ext[j] == 0) {
116 /* found end of an extension name */
117 const int len = j - i;
118 if (width + len > max) {
119 /* start a new line */
120 printf("\n");
121 width = indent;
122 printf(indentString);
123 }
124 /* print the extension name between ext[i] and ext[j] */
125 while (i < j) {
126 printf("%c", ext[i]);
127 i++;
128 }
129 /* either we're all done, or we'll continue with next extension */
130 width += len + 1;
131 if (ext[j] == 0) {
132 break;
133 }
134 else {
135 i++;
136 j++;
137 if (ext[j] == 0)
138 break;
139 printf(", ");
140 width += 2;
141 }
142 }
143 j++;
144 }
145 printf("\n");
146 }
147
148
149 static void
150 print_display_info(Display *dpy)
151 {
152 printf("name of display: %s\n", DisplayString(dpy));
153 }
154
155
156 /**
157 * Print interesting limits for vertex/fragment programs.
158 */
159 static void
160 print_program_limits(GLenum target)
161 {
162 #if defined(GL_ARB_vertex_program) || defined(GL_ARB_fragment_program)
163 struct token_name {
164 GLenum token;
165 const char *name;
166 };
167 static const struct token_name limits[] = {
168 { GL_MAX_PROGRAM_INSTRUCTIONS_ARB, "GL_MAX_PROGRAM_INSTRUCTIONS_ARB" },
169 { GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, "GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB" },
170 { GL_MAX_PROGRAM_TEMPORARIES_ARB, "GL_MAX_PROGRAM_TEMPORARIES_ARB" },
171 { GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB, "GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB" },
172 { GL_MAX_PROGRAM_PARAMETERS_ARB, "GL_MAX_PROGRAM_PARAMETERS_ARB" },
173 { GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB, "GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB" },
174 { GL_MAX_PROGRAM_ATTRIBS_ARB, "GL_MAX_PROGRAM_ATTRIBS_ARB" },
175 { GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB, "GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB" },
176 { GL_MAX_PROGRAM_ADDRESS_REGISTERS_ARB, "GL_MAX_PROGRAM_ADDRESS_REGISTERS_ARB" },
177 { GL_MAX_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB, "GL_MAX_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB" },
178 { GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB, "GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB" },
179 { GL_MAX_PROGRAM_ENV_PARAMETERS_ARB, "GL_MAX_PROGRAM_ENV_PARAMETERS_ARB" },
180 { GL_MAX_PROGRAM_ALU_INSTRUCTIONS_ARB, "GL_MAX_PROGRAM_ALU_INSTRUCTIONS_ARB" },
181 { GL_MAX_PROGRAM_TEX_INSTRUCTIONS_ARB, "GL_MAX_PROGRAM_TEX_INSTRUCTIONS_ARB" },
182 { GL_MAX_PROGRAM_TEX_INDIRECTIONS_ARB, "GL_MAX_PROGRAM_TEX_INDIRECTIONS_ARB" },
183 { GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, "GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB" },
184 { GL_MAX_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB, "GL_MAX_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB" },
185 { GL_MAX_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB, "GL_MAX_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB" },
186 { (GLenum) 0, NULL }
187 };
188 PFNGLGETPROGRAMIVARBPROC GetProgramivARB_func = (PFNGLGETPROGRAMIVARBPROC)
189 glXGetProcAddressARB((GLubyte *) "glGetProgramivARB");
190 GLint max[1];
191 int i;
192
193 if (target == GL_VERTEX_PROGRAM_ARB) {
194 printf(" GL_VERTEX_PROGRAM_ARB:\n");
195 }
196 else if (target == GL_FRAGMENT_PROGRAM_ARB) {
197 printf(" GL_FRAGMENT_PROGRAM_ARB:\n");
198 }
199 else {
200 return; /* something's wrong */
201 }
202
203 for (i = 0; limits[i].token; i++) {
204 GetProgramivARB_func(target, limits[i].token, max);
205 if (glGetError() == GL_NO_ERROR) {
206 printf(" %s = %d\n", limits[i].name, max[0]);
207 }
208 }
209 #endif /* GL_ARB_vertex_program / GL_ARB_fragment_program */
210 }
211
212
213 /**
214 * Print interesting limits for vertex/fragment shaders.
215 */
216 static void
217 print_shader_limits(GLenum target)
218 {
219 struct token_name {
220 GLenum token;
221 const char *name;
222 };
223 #if defined(GL_ARB_vertex_shader)
224 static const struct token_name vertex_limits[] = {
225 { GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB, "GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB" },
226 { GL_MAX_VARYING_FLOATS_ARB, "GL_MAX_VARYING_FLOATS_ARB" },
227 { GL_MAX_VERTEX_ATTRIBS_ARB, "GL_MAX_VERTEX_ATTRIBS_ARB" },
228 { GL_MAX_TEXTURE_IMAGE_UNITS_ARB, "GL_MAX_TEXTURE_IMAGE_UNITS_ARB" },
229 { GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB, "GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB" },
230 { GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS_ARB, "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS_ARB" },
231 { GL_MAX_TEXTURE_COORDS_ARB, "GL_MAX_TEXTURE_COORDS_ARB" },
232 { (GLenum) 0, NULL }
233 };
234 #endif
235 #if defined(GL_ARB_fragment_shader)
236 static const struct token_name fragment_limits[] = {
237 { GL_MAX_FRAGMENT_UNIFORM_COMPONENTS_ARB, "GL_MAX_FRAGMENT_UNIFORM_COMPONENTS_ARB" },
238 { GL_MAX_TEXTURE_COORDS_ARB, "GL_MAX_TEXTURE_COORDS_ARB" },
239 { GL_MAX_TEXTURE_IMAGE_UNITS_ARB, "GL_MAX_TEXTURE_IMAGE_UNITS_ARB" },
240 { (GLenum) 0, NULL }
241 };
242 #endif
243 GLint max[1];
244 int i;
245
246 #if defined(GL_ARB_vertex_shader)
247 if (target == GL_VERTEX_SHADER_ARB) {
248 printf(" GL_VERTEX_SHADER_ARB:\n");
249 for (i = 0; vertex_limits[i].token; i++) {
250 glGetIntegerv(vertex_limits[i].token, max);
251 if (glGetError() == GL_NO_ERROR) {
252 printf(" %s = %d\n", vertex_limits[i].name, max[0]);
253 }
254 }
255 }
256 #endif
257 #if defined(GL_ARB_fragment_shader)
258 if (target == GL_FRAGMENT_SHADER_ARB) {
259 printf(" GL_FRAGMENT_SHADER_ARB:\n");
260 for (i = 0; fragment_limits[i].token; i++) {
261 glGetIntegerv(fragment_limits[i].token, max);
262 if (glGetError() == GL_NO_ERROR) {
263 printf(" %s = %d\n", fragment_limits[i].name, max[0]);
264 }
265 }
266 }
267 #endif
268 }
269
270
271 /**
272 * Print interesting OpenGL implementation limits.
273 */
274 static void
275 print_limits(const char *extensions)
276 {
277 struct token_name {
278 GLuint count;
279 GLenum token;
280 const char *name;
281 };
282 static const struct token_name limits[] = {
283 { 1, GL_MAX_ATTRIB_STACK_DEPTH, "GL_MAX_ATTRIB_STACK_DEPTH" },
284 { 1, GL_MAX_CLIENT_ATTRIB_STACK_DEPTH, "GL_MAX_CLIENT_ATTRIB_STACK_DEPTH" },
285 { 1, GL_MAX_CLIP_PLANES, "GL_MAX_CLIP_PLANES" },
286 { 1, GL_MAX_COLOR_MATRIX_STACK_DEPTH, "GL_MAX_COLOR_MATRIX_STACK_DEPTH" },
287 { 1, GL_MAX_ELEMENTS_VERTICES, "GL_MAX_ELEMENTS_VERTICES" },
288 { 1, GL_MAX_ELEMENTS_INDICES, "GL_MAX_ELEMENTS_INDICES" },
289 { 1, GL_MAX_EVAL_ORDER, "GL_MAX_EVAL_ORDER" },
290 { 1, GL_MAX_LIGHTS, "GL_MAX_LIGHTS" },
291 { 1, GL_MAX_LIST_NESTING, "GL_MAX_LIST_NESTING" },
292 { 1, GL_MAX_MODELVIEW_STACK_DEPTH, "GL_MAX_MODELVIEW_STACK_DEPTH" },
293 { 1, GL_MAX_NAME_STACK_DEPTH, "GL_MAX_NAME_STACK_DEPTH" },
294 { 1, GL_MAX_PIXEL_MAP_TABLE, "GL_MAX_PIXEL_MAP_TABLE" },
295 { 1, GL_MAX_PROJECTION_STACK_DEPTH, "GL_MAX_PROJECTION_STACK_DEPTH" },
296 { 1, GL_MAX_TEXTURE_STACK_DEPTH, "GL_MAX_TEXTURE_STACK_DEPTH" },
297 { 1, GL_MAX_TEXTURE_SIZE, "GL_MAX_TEXTURE_SIZE" },
298 { 1, GL_MAX_3D_TEXTURE_SIZE, "GL_MAX_3D_TEXTURE_SIZE" },
299 { 2, GL_MAX_VIEWPORT_DIMS, "GL_MAX_VIEWPORT_DIMS" },
300 { 2, GL_ALIASED_LINE_WIDTH_RANGE, "GL_ALIASED_LINE_WIDTH_RANGE" },
301 { 2, GL_SMOOTH_LINE_WIDTH_RANGE, "GL_SMOOTH_LINE_WIDTH_RANGE" },
302 { 2, GL_ALIASED_POINT_SIZE_RANGE, "GL_ALIASED_POINT_SIZE_RANGE" },
303 { 2, GL_SMOOTH_POINT_SIZE_RANGE, "GL_SMOOTH_POINT_SIZE_RANGE" },
304 #if defined(GL_ARB_texture_cube_map)
305 { 1, GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB, "GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB" },
306 #endif
307 #if defined(GLX_NV_texture_rectangle)
308 { 1, GL_MAX_RECTANGLE_TEXTURE_SIZE_NV, "GL_MAX_RECTANGLE_TEXTURE_SIZE_NV" },
309 #endif
310 #if defined(GL_ARB_texture_compression)
311 { 1, GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB, "GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB" },
312 #endif
313 #if defined(GL_ARB_multitexture)
314 { 1, GL_MAX_TEXTURE_UNITS_ARB, "GL_MAX_TEXTURE_UNITS_ARB" },
315 #endif
316 #if defined(GL_EXT_texture_lod_bias)
317 { 1, GL_MAX_TEXTURE_LOD_BIAS_EXT, "GL_MAX_TEXTURE_LOD_BIAS_EXT" },
318 #endif
319 #if defined(GL_EXT_texture_filter_anisotropic)
320 { 1, GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, "GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT" },
321 #endif
322 #if defined(GL_ARB_draw_buffers)
323 { 1, GL_MAX_DRAW_BUFFERS_ARB, "GL_MAX_DRAW_BUFFERS_ARB" },
324 #endif
325 { 0, (GLenum) 0, NULL }
326 };
327 GLint i, max[2];
328
329 printf("OpenGL limits:\n");
330 for (i = 0; limits[i].count; i++) {
331 glGetIntegerv(limits[i].token, max);
332 if (glGetError() == GL_NO_ERROR) {
333 if (limits[i].count == 1)
334 printf(" %s = %d\n", limits[i].name, max[0]);
335 else /* XXX fix if we ever query something with more than 2 values */
336 printf(" %s = %d, %d\n", limits[i].name, max[0], max[1]);
337 }
338 }
339
340 /* these don't fit into the above mechanism, unfortunately */
341 glGetConvolutionParameteriv(GL_CONVOLUTION_2D, GL_MAX_CONVOLUTION_WIDTH, max);
342 glGetConvolutionParameteriv(GL_CONVOLUTION_2D, GL_MAX_CONVOLUTION_HEIGHT, max+1);
343 if (glGetError() == GL_NONE) {
344 printf(" GL_MAX_CONVOLUTION_WIDTH/HEIGHT = %d, %d\n", max[0], max[1]);
345 }
346
347 #if defined(GL_ARB_vertex_program)
348 if (strstr(extensions, "GL_ARB_vertex_program")) {
349 print_program_limits(GL_VERTEX_PROGRAM_ARB);
350 }
351 #endif
352 #if defined(GL_ARB_fragment_program)
353 if (strstr(extensions, "GL_ARB_fragment_program")) {
354 print_program_limits(GL_FRAGMENT_PROGRAM_ARB);
355 }
356 #endif
357 #if defined(GL_ARB_vertex_shader)
358 if (strstr(extensions, "GL_ARB_vertex_shader")) {
359 print_shader_limits(GL_VERTEX_SHADER_ARB);
360 }
361 #endif
362 #if defined(GL_ARB_fragment_shader)
363 if (strstr(extensions, "GL_ARB_fragment_shader")) {
364 print_shader_limits(GL_FRAGMENT_SHADER_ARB);
365 }
366 #endif
367 }
368
369
370 static void
371 print_screen_info(Display *dpy, int scrnum, Bool allowDirect, GLboolean limits)
372 {
373 Window win;
374 int attribSingle[] = {
375 GLX_RGBA,
376 GLX_RED_SIZE, 1,
377 GLX_GREEN_SIZE, 1,
378 GLX_BLUE_SIZE, 1,
379 None };
380 int attribDouble[] = {
381 GLX_RGBA,
382 GLX_RED_SIZE, 1,
383 GLX_GREEN_SIZE, 1,
384 GLX_BLUE_SIZE, 1,
385 GLX_DOUBLEBUFFER,
386 None };
387
388 XSetWindowAttributes attr;
389 unsigned long mask;
390 Window root;
391 GLXContext ctx;
392 XVisualInfo *visinfo;
393 int width = 100, height = 100;
394
395 root = RootWindow(dpy, scrnum);
396
397 visinfo = glXChooseVisual(dpy, scrnum, attribSingle);
398 if (!visinfo) {
399 visinfo = glXChooseVisual(dpy, scrnum, attribDouble);
400 if (!visinfo) {
401 fprintf(stderr, "Error: couldn't find RGB GLX visual\n");
402 return;
403 }
404 }
405
406 attr.background_pixel = 0;
407 attr.border_pixel = 0;
408 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
409 attr.event_mask = StructureNotifyMask | ExposureMask;
410 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
411 win = XCreateWindow(dpy, root, 0, 0, width, height,
412 0, visinfo->depth, InputOutput,
413 visinfo->visual, mask, &attr);
414
415 ctx = glXCreateContext( dpy, visinfo, NULL, allowDirect );
416 if (!ctx) {
417 fprintf(stderr, "Error: glXCreateContext failed\n");
418 XFree(visinfo);
419 XDestroyWindow(dpy, win);
420 return;
421 }
422
423 if (glXMakeCurrent(dpy, win, ctx)) {
424 const char *serverVendor = glXQueryServerString(dpy, scrnum, GLX_VENDOR);
425 const char *serverVersion = glXQueryServerString(dpy, scrnum, GLX_VERSION);
426 const char *serverExtensions = glXQueryServerString(dpy, scrnum, GLX_EXTENSIONS);
427 const char *clientVendor = glXGetClientString(dpy, GLX_VENDOR);
428 const char *clientVersion = glXGetClientString(dpy, GLX_VERSION);
429 const char *clientExtensions = glXGetClientString(dpy, GLX_EXTENSIONS);
430 const char *glxExtensions = glXQueryExtensionsString(dpy, scrnum);
431 const char *glVendor = (const char *) glGetString(GL_VENDOR);
432 const char *glRenderer = (const char *) glGetString(GL_RENDERER);
433 const char *glVersion = (const char *) glGetString(GL_VERSION);
434 const char *glExtensions = (const char *) glGetString(GL_EXTENSIONS);
435 int glxVersionMajor;
436 int glxVersionMinor;
437 char *displayName = NULL;
438 char *colon = NULL, *period = NULL;
439
440 if (! glXQueryVersion( dpy, & glxVersionMajor, & glxVersionMinor )) {
441 fprintf(stderr, "Error: glXQueryVersion failed\n");
442 exit(1);
443 }
444
445 /* Strip the screen number from the display name, if present. */
446 if (!(displayName = (char *) malloc(strlen(DisplayString(dpy)) + 1))) {
447 fprintf(stderr, "Error: malloc() failed\n");
448 exit(1);
449 }
450 strcpy(displayName, DisplayString(dpy));
451 colon = strrchr(displayName, ':');
452 if (colon) {
453 period = strchr(colon, '.');
454 if (period)
455 *period = '\0';
456 }
457 printf("display: %s screen: %d\n", displayName, scrnum);
458 free(displayName);
459 printf("direct rendering: ");
460 if (glXIsDirect(dpy, ctx)) {
461 printf("Yes\n");
462 } else {
463 if (!allowDirect) {
464 printf("No (-i specified)\n");
465 } else if (getenv("LIBGL_ALWAYS_INDIRECT")) {
466 printf("No (LIBGL_ALWAYS_INDIRECT set)\n");
467 } else {
468 printf("No (If you want to find out why, try setting "
469 "LIBGL_DEBUG=verbose)\n");
470 }
471 }
472 printf("server glx vendor string: %s\n", serverVendor);
473 printf("server glx version string: %s\n", serverVersion);
474 printf("server glx extensions:\n");
475 print_extension_list(serverExtensions);
476 printf("client glx vendor string: %s\n", clientVendor);
477 printf("client glx version string: %s\n", clientVersion);
478 printf("client glx extensions:\n");
479 print_extension_list(clientExtensions);
480 printf("GLX version: %u.%u\n", glxVersionMajor, glxVersionMinor);
481 printf("GLX extensions:\n");
482 print_extension_list(glxExtensions);
483 printf("OpenGL vendor string: %s\n", glVendor);
484 printf("OpenGL renderer string: %s\n", glRenderer);
485 printf("OpenGL version string: %s\n", glVersion);
486 printf("OpenGL extensions:\n");
487 print_extension_list(glExtensions);
488 if (limits)
489 print_limits(glExtensions);
490 }
491 else {
492 fprintf(stderr, "Error: glXMakeCurrent failed\n");
493 }
494
495 glXDestroyContext(dpy, ctx);
496 XFree(visinfo);
497 XDestroyWindow(dpy, win);
498 }
499
500
501 static const char *
502 visual_class_name(int cls)
503 {
504 switch (cls) {
505 case StaticColor:
506 return "StaticColor";
507 case PseudoColor:
508 return "PseudoColor";
509 case StaticGray:
510 return "StaticGray";
511 case GrayScale:
512 return "GrayScale";
513 case TrueColor:
514 return "TrueColor";
515 case DirectColor:
516 return "DirectColor";
517 default:
518 return "";
519 }
520 }
521
522
523 static const char *
524 visual_class_abbrev(int cls)
525 {
526 switch (cls) {
527 case StaticColor:
528 return "sc";
529 case PseudoColor:
530 return "pc";
531 case StaticGray:
532 return "sg";
533 case GrayScale:
534 return "gs";
535 case TrueColor:
536 return "tc";
537 case DirectColor:
538 return "dc";
539 default:
540 return "";
541 }
542 }
543
544
545 static void
546 get_visual_attribs(Display *dpy, XVisualInfo *vInfo,
547 struct visual_attribs *attribs)
548 {
549 const char *ext = glXQueryExtensionsString(dpy, vInfo->screen);
550
551 memset(attribs, 0, sizeof(struct visual_attribs));
552
553 attribs->id = vInfo->visualid;
554 #if defined(__cplusplus) || defined(c_plusplus)
555 attribs->klass = vInfo->c_class;
556 #else
557 attribs->klass = vInfo->class;
558 #endif
559 attribs->depth = vInfo->depth;
560 attribs->redMask = vInfo->red_mask;
561 attribs->greenMask = vInfo->green_mask;
562 attribs->blueMask = vInfo->blue_mask;
563 attribs->colormapSize = vInfo->colormap_size;
564 attribs->bitsPerRGB = vInfo->bits_per_rgb;
565
566 if (glXGetConfig(dpy, vInfo, GLX_USE_GL, &attribs->supportsGL) != 0)
567 return;
568 glXGetConfig(dpy, vInfo, GLX_BUFFER_SIZE, &attribs->bufferSize);
569 glXGetConfig(dpy, vInfo, GLX_LEVEL, &attribs->level);
570 glXGetConfig(dpy, vInfo, GLX_RGBA, &attribs->rgba);
571 glXGetConfig(dpy, vInfo, GLX_DOUBLEBUFFER, &attribs->doubleBuffer);
572 glXGetConfig(dpy, vInfo, GLX_STEREO, &attribs->stereo);
573 glXGetConfig(dpy, vInfo, GLX_AUX_BUFFERS, &attribs->auxBuffers);
574 glXGetConfig(dpy, vInfo, GLX_RED_SIZE, &attribs->redSize);
575 glXGetConfig(dpy, vInfo, GLX_GREEN_SIZE, &attribs->greenSize);
576 glXGetConfig(dpy, vInfo, GLX_BLUE_SIZE, &attribs->blueSize);
577 glXGetConfig(dpy, vInfo, GLX_ALPHA_SIZE, &attribs->alphaSize);
578 glXGetConfig(dpy, vInfo, GLX_DEPTH_SIZE, &attribs->depthSize);
579 glXGetConfig(dpy, vInfo, GLX_STENCIL_SIZE, &attribs->stencilSize);
580 glXGetConfig(dpy, vInfo, GLX_ACCUM_RED_SIZE, &attribs->accumRedSize);
581 glXGetConfig(dpy, vInfo, GLX_ACCUM_GREEN_SIZE, &attribs->accumGreenSize);
582 glXGetConfig(dpy, vInfo, GLX_ACCUM_BLUE_SIZE, &attribs->accumBlueSize);
583 glXGetConfig(dpy, vInfo, GLX_ACCUM_ALPHA_SIZE, &attribs->accumAlphaSize);
584
585 /* get transparent pixel stuff */
586 glXGetConfig(dpy, vInfo,GLX_TRANSPARENT_TYPE, &attribs->transparentType);
587 if (attribs->transparentType == GLX_TRANSPARENT_RGB) {
588 glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_RED_VALUE, &attribs->transparentRedValue);
589 glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_GREEN_VALUE, &attribs->transparentGreenValue);
590 glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_BLUE_VALUE, &attribs->transparentBlueValue);
591 glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_ALPHA_VALUE, &attribs->transparentAlphaValue);
592 }
593 else if (attribs->transparentType == GLX_TRANSPARENT_INDEX) {
594 glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_INDEX_VALUE, &attribs->transparentIndexValue);
595 }
596
597 /* multisample attribs */
598 #ifdef GLX_ARB_multisample
599 if (ext && strstr(ext, "GLX_ARB_multisample") == 0) {
600 glXGetConfig(dpy, vInfo, GLX_SAMPLE_BUFFERS_ARB, &attribs->numMultisample);
601 glXGetConfig(dpy, vInfo, GLX_SAMPLES_ARB, &attribs->numSamples);
602 }
603 #endif
604 else {
605 attribs->numSamples = 0;
606 attribs->numMultisample = 0;
607 }
608
609 #if defined(GLX_EXT_visual_rating)
610 if (ext && strstr(ext, "GLX_EXT_visual_rating")) {
611 glXGetConfig(dpy, vInfo, GLX_VISUAL_CAVEAT_EXT, &attribs->visualCaveat);
612 }
613 else {
614 attribs->visualCaveat = GLX_NONE_EXT;
615 }
616 #else
617 attribs->visualCaveat = 0;
618 #endif
619 }
620
621
622 static void
623 print_visual_attribs_verbose(const struct visual_attribs *attribs)
624 {
625 printf("Visual ID: %x depth=%d class=%s\n",
626 attribs->id, attribs->depth, visual_class_name(attribs->klass));
627 printf(" bufferSize=%d level=%d renderType=%s doubleBuffer=%d stereo=%d\n",
628 attribs->bufferSize, attribs->level, attribs->rgba ? "rgba" : "ci",
629 attribs->doubleBuffer, attribs->stereo);
630 printf(" rgba: redSize=%d greenSize=%d blueSize=%d alphaSize=%d\n",
631 attribs->redSize, attribs->greenSize,
632 attribs->blueSize, attribs->alphaSize);
633 printf(" auxBuffers=%d depthSize=%d stencilSize=%d\n",
634 attribs->auxBuffers, attribs->depthSize, attribs->stencilSize);
635 printf(" accum: redSize=%d greenSize=%d blueSize=%d alphaSize=%d\n",
636 attribs->accumRedSize, attribs->accumGreenSize,
637 attribs->accumBlueSize, attribs->accumAlphaSize);
638 printf(" multiSample=%d multiSampleBuffers=%d\n",
639 attribs->numSamples, attribs->numMultisample);
640 #ifdef GLX_EXT_visual_rating
641 if (attribs->visualCaveat == GLX_NONE_EXT || attribs->visualCaveat == 0)
642 printf(" visualCaveat=None\n");
643 else if (attribs->visualCaveat == GLX_SLOW_VISUAL_EXT)
644 printf(" visualCaveat=Slow\n");
645 else if (attribs->visualCaveat == GLX_NON_CONFORMANT_VISUAL_EXT)
646 printf(" visualCaveat=Nonconformant\n");
647 #endif
648 if (attribs->transparentType == GLX_NONE) {
649 printf(" Opaque.\n");
650 }
651 else if (attribs->transparentType == GLX_TRANSPARENT_RGB) {
652 printf(" Transparent RGB: Red=%d Green=%d Blue=%d Alpha=%d\n",attribs->transparentRedValue,attribs->transparentGreenValue,attribs->transparentBlueValue,attribs->transparentAlphaValue);
653 }
654 else if (attribs->transparentType == GLX_TRANSPARENT_INDEX) {
655 printf(" Transparent index=%d\n",attribs->transparentIndexValue);
656 }
657 }
658
659
660 static void
661 print_visual_attribs_short_header(void)
662 {
663 printf(" visual x bf lv rg d st colorbuffer ax dp st accumbuffer ms cav\n");
664 printf(" id dep cl sp sz l ci b ro r g b a bf th cl r g b a ns b eat\n");
665 printf("----------------------------------------------------------------------\n");
666 }
667
668
669 static void
670 print_visual_attribs_short(const struct visual_attribs *attribs)
671 {
672 char *caveat = NULL;
673 #ifdef GLX_EXT_visual_rating
674 if (attribs->visualCaveat == GLX_NONE_EXT || attribs->visualCaveat == 0)
675 caveat = "None";
676 else if (attribs->visualCaveat == GLX_SLOW_VISUAL_EXT)
677 caveat = "Slow";
678 else if (attribs->visualCaveat == GLX_NON_CONFORMANT_VISUAL_EXT)
679 caveat = "Ncon";
680 else
681 caveat = "None";
682 #else
683 caveat = "None";
684 #endif
685
686 printf("0x%2x %2d %2s %2d %2d %2d %1s %2s %2s %2d %2d %2d %2d %2d %2d %2d",
687 attribs->id,
688 attribs->depth,
689 visual_class_abbrev(attribs->klass),
690 attribs->transparentType != GLX_NONE,
691 attribs->bufferSize,
692 attribs->level,
693 attribs->rgba ? "r" : "c",
694 attribs->doubleBuffer ? "y" : ".",
695 attribs->stereo ? "y" : ".",
696 attribs->redSize, attribs->greenSize,
697 attribs->blueSize, attribs->alphaSize,
698 attribs->auxBuffers,
699 attribs->depthSize,
700 attribs->stencilSize
701 );
702
703 printf(" %2d %2d %2d %2d %2d %1d %s\n",
704 attribs->accumRedSize, attribs->accumGreenSize,
705 attribs->accumBlueSize, attribs->accumAlphaSize,
706 attribs->numSamples, attribs->numMultisample,
707 caveat
708 );
709 }
710
711
712 static void
713 print_visual_attribs_long_header(void)
714 {
715 printf("Vis Vis Visual Trans buff lev render DB ste r g b a aux dep ste accum buffers MS MS\n");
716 printf(" ID Depth Type parent size el type reo sz sz sz sz buf th ncl r g b a num bufs\n");
717 printf("----------------------------------------------------------------------------------------------------\n");
718 }
719
720
721 static void
722 print_visual_attribs_long(const struct visual_attribs *attribs)
723 {
724 printf("0x%2x %2d %-11s %2d %2d %2d %4s %3d %3d %3d %3d %3d %3d",
725 attribs->id,
726 attribs->depth,
727 visual_class_name(attribs->klass),
728 attribs->transparentType != GLX_NONE,
729 attribs->bufferSize,
730 attribs->level,
731 attribs->rgba ? "rgba" : "ci ",
732 attribs->doubleBuffer,
733 attribs->stereo,
734 attribs->redSize, attribs->greenSize,
735 attribs->blueSize, attribs->alphaSize
736 );
737
738 printf(" %3d %4d %2d %3d %3d %3d %3d %2d %2d\n",
739 attribs->auxBuffers,
740 attribs->depthSize,
741 attribs->stencilSize,
742 attribs->accumRedSize, attribs->accumGreenSize,
743 attribs->accumBlueSize, attribs->accumAlphaSize,
744 attribs->numSamples, attribs->numMultisample
745 );
746 }
747
748
749 static void
750 print_visual_info(Display *dpy, int scrnum, InfoMode mode)
751 {
752 XVisualInfo theTemplate;
753 XVisualInfo *visuals;
754 int numVisuals;
755 long mask;
756 int i;
757
758 /* get list of all visuals on this screen */
759 theTemplate.screen = scrnum;
760 mask = VisualScreenMask;
761 visuals = XGetVisualInfo(dpy, mask, &theTemplate, &numVisuals);
762
763 if (mode == Verbose) {
764 for (i = 0; i < numVisuals; i++) {
765 struct visual_attribs attribs;
766 get_visual_attribs(dpy, &visuals[i], &attribs);
767 print_visual_attribs_verbose(&attribs);
768 }
769 }
770 else if (mode == Normal) {
771 print_visual_attribs_short_header();
772 for (i = 0; i < numVisuals; i++) {
773 struct visual_attribs attribs;
774 get_visual_attribs(dpy, &visuals[i], &attribs);
775 print_visual_attribs_short(&attribs);
776 }
777 }
778 else if (mode == Wide) {
779 print_visual_attribs_long_header();
780 for (i = 0; i < numVisuals; i++) {
781 struct visual_attribs attribs;
782 get_visual_attribs(dpy, &visuals[i], &attribs);
783 print_visual_attribs_long(&attribs);
784 }
785 }
786
787 XFree(visuals);
788 }
789
790
791 /*
792 * Stand-alone Mesa doesn't really implement the GLX protocol so it
793 * doesn't really know the GLX attributes associated with an X visual.
794 * The first time a visual is presented to Mesa's pseudo-GLX it
795 * attaches ancilliary buffers to it (like depth and stencil).
796 * But that usually only works if glXChooseVisual is used.
797 * This function calls glXChooseVisual() to sort of "prime the pump"
798 * for Mesa's GLX so that the visuals that get reported actually
799 * reflect what applications will see.
800 * This has no effect when using true GLX.
801 */
802 static void
803 mesa_hack(Display *dpy, int scrnum)
804 {
805 static int attribs[] = {
806 GLX_RGBA,
807 GLX_RED_SIZE, 1,
808 GLX_GREEN_SIZE, 1,
809 GLX_BLUE_SIZE, 1,
810 GLX_DEPTH_SIZE, 1,
811 GLX_STENCIL_SIZE, 1,
812 GLX_ACCUM_RED_SIZE, 1,
813 GLX_ACCUM_GREEN_SIZE, 1,
814 GLX_ACCUM_BLUE_SIZE, 1,
815 GLX_ACCUM_ALPHA_SIZE, 1,
816 GLX_DOUBLEBUFFER,
817 None
818 };
819 XVisualInfo *visinfo;
820
821 visinfo = glXChooseVisual(dpy, scrnum, attribs);
822 if (visinfo)
823 XFree(visinfo);
824 }
825
826
827 /*
828 * Examine all visuals to find the so-called best one.
829 * We prefer deepest RGBA buffer with depth, stencil and accum
830 * that has no caveats.
831 */
832 static int
833 find_best_visual(Display *dpy, int scrnum)
834 {
835 XVisualInfo theTemplate;
836 XVisualInfo *visuals;
837 int numVisuals;
838 long mask;
839 int i;
840 struct visual_attribs bestVis;
841
842 /* get list of all visuals on this screen */
843 theTemplate.screen = scrnum;
844 mask = VisualScreenMask;
845 visuals = XGetVisualInfo(dpy, mask, &theTemplate, &numVisuals);
846
847 /* init bestVis with first visual info */
848 get_visual_attribs(dpy, &visuals[0], &bestVis);
849
850 /* try to find a "better" visual */
851 for (i = 1; i < numVisuals; i++) {
852 struct visual_attribs vis;
853
854 get_visual_attribs(dpy, &visuals[i], &vis);
855
856 /* always skip visuals with caveats */
857 if (vis.visualCaveat != GLX_NONE_EXT)
858 continue;
859
860 /* see if this vis is better than bestVis */
861 if ((!bestVis.supportsGL && vis.supportsGL) ||
862 (bestVis.visualCaveat != GLX_NONE_EXT) ||
863 (!bestVis.rgba && vis.rgba) ||
864 (!bestVis.doubleBuffer && vis.doubleBuffer) ||
865 (bestVis.redSize < vis.redSize) ||
866 (bestVis.greenSize < vis.greenSize) ||
867 (bestVis.blueSize < vis.blueSize) ||
868 (bestVis.alphaSize < vis.alphaSize) ||
869 (bestVis.depthSize < vis.depthSize) ||
870 (bestVis.stencilSize < vis.stencilSize) ||
871 (bestVis.accumRedSize < vis.accumRedSize)) {
872 /* found a better visual */
873 bestVis = vis;
874 }
875 }
876
877 XFree(visuals);
878
879 return bestVis.id;
880 }
881
882
883 static void
884 usage(void)
885 {
886 printf("Usage: glxinfo [-v] [-t] [-h] [-i] [-b] [-display <dname>]\n");
887 printf("\t-v: Print visuals info in verbose form.\n");
888 printf("\t-t: Print verbose table.\n");
889 printf("\t-display <dname>: Print GLX visuals on specified server.\n");
890 printf("\t-h: This information.\n");
891 printf("\t-i: Force an indirect rendering context.\n");
892 printf("\t-b: Find the 'best' visual and print it's number.\n");
893 printf("\t-l: Print interesting OpenGL limits.\n");
894 }
895
896
897 int
898 main(int argc, char *argv[])
899 {
900 char *displayName = NULL;
901 Display *dpy;
902 int numScreens, scrnum;
903 InfoMode mode = Normal;
904 GLboolean findBest = GL_FALSE;
905 GLboolean limits = GL_FALSE;
906 Bool allowDirect = True;
907 int i;
908
909 for (i = 1; i < argc; i++) {
910 if (strcmp(argv[i], "-display") == 0 && i + 1 < argc) {
911 displayName = argv[i + 1];
912 i++;
913 }
914 else if (strcmp(argv[i], "-t") == 0) {
915 mode = Wide;
916 }
917 else if (strcmp(argv[i], "-v") == 0) {
918 mode = Verbose;
919 }
920 else if (strcmp(argv[i], "-b") == 0) {
921 findBest = GL_TRUE;
922 }
923 else if (strcmp(argv[i], "-i") == 0) {
924 allowDirect = False;
925 }
926 else if (strcmp(argv[i], "-l") == 0) {
927 limits = GL_TRUE;
928 }
929 else if (strcmp(argv[i], "-h") == 0) {
930 usage();
931 return 0;
932 }
933 else {
934 printf("Unknown option `%s'\n", argv[i]);
935 usage();
936 return 0;
937 }
938 }
939
940 dpy = XOpenDisplay(displayName);
941 if (!dpy) {
942 fprintf(stderr, "Error: unable to open display %s\n", XDisplayName(displayName));
943 return -1;
944 }
945
946 if (findBest) {
947 int b;
948 mesa_hack(dpy, 0);
949 b = find_best_visual(dpy, 0);
950 printf("%d\n", b);
951 }
952 else {
953 numScreens = ScreenCount(dpy);
954 print_display_info(dpy);
955 for (scrnum = 0; scrnum < numScreens; scrnum++) {
956 mesa_hack(dpy, scrnum);
957 print_screen_info(dpy, scrnum, allowDirect, limits);
958 printf("\n");
959 print_visual_info(dpy, scrnum, mode);
960 if (scrnum + 1 < numScreens)
961 printf("\n\n");
962 }
963 }
964
965 XCloseDisplay(dpy);
966
967 return 0;
968 }