minor updates to sync with XFree86
[mesa.git] / progs / xdemos / glxinfo.c
1 /* $Id: glxinfo.c,v 1.18 2002/09/06 03:47:34 brianp Exp $ */
2
3 /*
4 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24
25 /*
26 * This program is a work-alike of the IRIX glxinfo program.
27 * Command line options:
28 * -t print wide table
29 * -v print verbose information
30 * -display DisplayName specify the X display to interogate
31 * -b only print ID of "best" visual on screen 0
32 * -i use indirect rendering connection only
33 * -l print interesting OpenGL limits (added 5 Sep 2002)
34 *
35 * Brian Paul 26 January 2000
36 */
37
38 #define DO_GLU /* may want to remove this for easier XFree86 building? */
39
40 #include <X11/Xlib.h>
41 #include <X11/Xutil.h>
42 #include <GL/gl.h>
43 #ifdef DO_GLU
44 #include <GL/glu.h>
45 #endif
46 #include <GL/glx.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <stdlib.h>
50
51
52 #ifndef GLX_NONE_EXT
53 #define GLX_NONE_EXT 0x8000
54 #endif
55
56
57 typedef enum
58 {
59 Normal,
60 Wide,
61 Verbose
62 } InfoMode;
63
64
65 struct visual_attribs
66 {
67 /* X visual attribs */
68 int id;
69 int klass;
70 int depth;
71 int redMask, greenMask, blueMask;
72 int colormapSize;
73 int bitsPerRGB;
74
75 /* GL visual attribs */
76 int supportsGL;
77 int transparent;
78 int bufferSize;
79 int level;
80 int rgba;
81 int doubleBuffer;
82 int stereo;
83 int auxBuffers;
84 int redSize, greenSize, blueSize, alphaSize;
85 int depthSize;
86 int stencilSize;
87 int accumRedSize, accumGreenSize, accumBlueSize, accumAlphaSize;
88 int numSamples, numMultisample;
89 int visualCaveat;
90 };
91
92
93 /*
94 * Print a list of extensions, with word-wrapping.
95 */
96 static void
97 print_extension_list(const char *ext)
98 {
99 const char *indentString = " ";
100 const int indent = 4;
101 const int max = 79;
102 int width, i, j;
103
104 if (!ext || !ext[0])
105 return;
106
107 width = indent;
108 printf(indentString);
109 i = j = 0;
110 while (1) {
111 if (ext[j] == ' ' || ext[j] == 0) {
112 /* found end of an extension name */
113 const int len = j - i;
114 if (width + len > max) {
115 /* start a new line */
116 printf("\n");
117 width = indent;
118 printf(indentString);
119 }
120 /* print the extension name between ext[i] and ext[j] */
121 while (i < j) {
122 printf("%c", ext[i]);
123 i++;
124 }
125 /* either we're all done, or we'll continue with next extension */
126 width += len + 1;
127 if (ext[j] == 0) {
128 break;
129 }
130 else {
131 i++;
132 j++;
133 if (ext[j] == 0)
134 break;
135 printf(", ");
136 width += 2;
137 }
138 }
139 j++;
140 }
141 printf("\n");
142 }
143
144
145 static void
146 print_display_info(Display *dpy)
147 {
148 printf("name of display: %s\n", DisplayString(dpy));
149 }
150
151
152 static void
153 print_limits(void)
154 {
155 struct token_name {
156 GLuint count;
157 GLenum token;
158 const char *name;
159 };
160 static const struct token_name limits[] = {
161 { 1, GL_MAX_ATTRIB_STACK_DEPTH, "GL_MAX_ATTRIB_STACK_DEPTH" },
162 { 1, GL_MAX_CLIENT_ATTRIB_STACK_DEPTH, "GL_MAX_CLIENT_ATTRIB_STACK_DEPTH" },
163 { 1, GL_MAX_CLIP_PLANES, "GL_MAX_CLIP_PLANES" },
164 { 1, GL_MAX_COLOR_MATRIX_STACK_DEPTH, "GL_MAX_COLOR_MATRIX_STACK_DEPTH" },
165 { 1, GL_MAX_CONVOLUTION_WIDTH_EXT, "GL_MAX_CONVOLUTION_WIDTH_EXT" },
166 { 1, GL_MAX_CONVOLUTION_HEIGHT_EXT, "GL_MAX_CONVOLUTION_HEIGHT_EXT" },
167 { 1, GL_MAX_ELEMENTS_VERTICES, "GL_MAX_ELEMENTS_VERTICES" },
168 { 1, GL_MAX_ELEMENTS_INDICES, "GL_MAX_ELEMENTS_INDICES" },
169 { 1, GL_MAX_EVAL_ORDER, "GL_MAX_EVAL_ORDER" },
170 { 1, GL_MAX_LIGHTS, "GL_MAX_LIGHTS" },
171 { 1, GL_MAX_LIST_NESTING, "GL_MAX_LIST_NESTING" },
172 { 1, GL_MAX_MODELVIEW_STACK_DEPTH, "GL_MAX_MODELVIEW_STACK_DEPTH" },
173 { 1, GL_MAX_NAME_STACK_DEPTH, "GL_MAX_NAME_STACK_DEPTH" },
174 { 1, GL_MAX_PIXEL_MAP_TABLE, "GL_MAX_PIXEL_MAP_TABLE" },
175 { 1, GL_MAX_PROJECTION_STACK_DEPTH, "GL_MAX_PROJECTION_STACK_DEPTH" },
176 { 1, GL_MAX_TEXTURE_STACK_DEPTH, "GL_MAX_TEXTURE_STACK_DEPTH" },
177 { 1, GL_MAX_TEXTURE_SIZE, "GL_MAX_TEXTURE_SIZE" },
178 { 1, GL_MAX_3D_TEXTURE_SIZE, "GL_MAX_3D_TEXTURE_SIZE" },
179 { 1, GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB, "GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB" },
180 { 1, GL_MAX_RECTANGLE_TEXTURE_SIZE_NV, "GL_MAX_RECTANGLE_TEXTURE_SIZE_NV" },
181 { 1, GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB, "GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB" },
182 { 1, GL_MAX_TEXTURE_UNITS_ARB, "GL_MAX_TEXTURE_UNITS_ARB" },
183 { 1, GL_MAX_TEXTURE_LOD_BIAS_EXT, "GL_MAX_TEXTURE_LOD_BIAS_EXT" },
184 { 1, GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, "GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT" },
185 { 2, GL_MAX_VIEWPORT_DIMS, "GL_MAX_VIEWPORT_DIMS" },
186 { 2, GL_ALIASED_LINE_WIDTH_RANGE, "GL_ALIASED_LINE_WIDTH_RANGE" },
187 { 2, GL_SMOOTH_LINE_WIDTH_RANGE, "GL_SMOOTH_LINE_WIDTH_RANGE" },
188 { 2, GL_ALIASED_POINT_SIZE_RANGE, "GL_ALIASED_POINT_SIZE_RANGE" },
189 { 2, GL_SMOOTH_POINT_SIZE_RANGE, "GL_SMOOTH_POINT_SIZE_RANGE" },
190 { 0, (GLenum) 0, NULL }
191 };
192 GLint i, max[2];
193 printf("OpenGL limits:\n");
194 for (i = 0; limits[i].count; i++) {
195 glGetIntegerv(limits[i].token, max);
196 if (glGetError() == GL_NONE) {
197 if (limits[i].count == 1)
198 printf(" %s = %d\n", limits[i].name, max[0]);
199 else /* XXX fix if we ever query something with more than 2 values */
200 printf(" %s = %d, %d\n", limits[i].name, max[0], max[1]);
201 }
202 }
203 }
204
205
206 static void
207 print_screen_info(Display *dpy, int scrnum, Bool allowDirect, GLboolean limits)
208 {
209 Window win;
210 int attribSingle[] = {
211 GLX_RGBA,
212 GLX_RED_SIZE, 1,
213 GLX_GREEN_SIZE, 1,
214 GLX_BLUE_SIZE, 1,
215 None };
216 int attribDouble[] = {
217 GLX_RGBA,
218 GLX_RED_SIZE, 1,
219 GLX_GREEN_SIZE, 1,
220 GLX_BLUE_SIZE, 1,
221 GLX_DOUBLEBUFFER,
222 None };
223
224 XSetWindowAttributes attr;
225 unsigned long mask;
226 Window root;
227 GLXContext ctx;
228 XVisualInfo *visinfo;
229 int width = 100, height = 100;
230
231 root = RootWindow(dpy, scrnum);
232
233 visinfo = glXChooseVisual(dpy, scrnum, attribSingle);
234 if (!visinfo) {
235 visinfo = glXChooseVisual(dpy, scrnum, attribDouble);
236 if (!visinfo) {
237 fprintf(stderr, "Error: couldn't find RGB GLX visual\n");
238 return;
239 }
240 }
241
242 attr.background_pixel = 0;
243 attr.border_pixel = 0;
244 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
245 attr.event_mask = StructureNotifyMask | ExposureMask;
246 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
247 win = XCreateWindow(dpy, root, 0, 0, width, height,
248 0, visinfo->depth, InputOutput,
249 visinfo->visual, mask, &attr);
250
251 ctx = glXCreateContext( dpy, visinfo, NULL, allowDirect );
252 if (!ctx) {
253 fprintf(stderr, "Error: glXCreateContext failed\n");
254 XDestroyWindow(dpy, win);
255 return;
256 }
257
258 if (glXMakeCurrent(dpy, win, ctx)) {
259 const char *serverVendor = glXQueryServerString(dpy, scrnum, GLX_VENDOR);
260 const char *serverVersion = glXQueryServerString(dpy, scrnum, GLX_VERSION);
261 const char *serverExtensions = glXQueryServerString(dpy, scrnum, GLX_EXTENSIONS);
262 const char *clientVendor = glXGetClientString(dpy, GLX_VENDOR);
263 const char *clientVersion = glXGetClientString(dpy, GLX_VERSION);
264 const char *clientExtensions = glXGetClientString(dpy, GLX_EXTENSIONS);
265 const char *glxExtensions = glXQueryExtensionsString(dpy, scrnum);
266 const char *glVendor = (const char *) glGetString(GL_VENDOR);
267 const char *glRenderer = (const char *) glGetString(GL_RENDERER);
268 const char *glVersion = (const char *) glGetString(GL_VERSION);
269 const char *glExtensions = (const char *) glGetString(GL_EXTENSIONS);
270 char *displayName = NULL;
271 char *colon = NULL, *period = NULL;
272 #ifdef DO_GLU
273 const char *gluVersion = (const char *) gluGetString(GLU_VERSION);
274 const char *gluExtensions = (const char *) gluGetString(GLU_EXTENSIONS);
275 #endif
276 /* Strip the screen number from the display name, if present. */
277 if (!(displayName = (char *) malloc(strlen(DisplayString(dpy)) + 1))) {
278 fprintf(stderr, "Error: malloc() failed\n");
279 exit(1);
280 }
281 strcpy(displayName, DisplayString(dpy));
282 colon = strrchr(displayName, ':');
283 if (colon) {
284 period = strchr(colon, '.');
285 if (period)
286 *period = '\0';
287 }
288 printf("display: %s screen: %d\n", displayName, scrnum);
289 free(displayName);
290 printf("direct rendering: %s\n", glXIsDirect(dpy, ctx) ? "Yes" : "No");
291 printf("server glx vendor string: %s\n", serverVendor);
292 printf("server glx version string: %s\n", serverVersion);
293 printf("server glx extensions:\n");
294 print_extension_list(serverExtensions);
295 printf("client glx vendor string: %s\n", clientVendor);
296 printf("client glx version string: %s\n", clientVersion);
297 printf("client glx extensions:\n");
298 print_extension_list(clientExtensions);
299 printf("GLX extensions:\n");
300 print_extension_list(glxExtensions);
301 printf("OpenGL vendor string: %s\n", glVendor);
302 printf("OpenGL renderer string: %s\n", glRenderer);
303 printf("OpenGL version string: %s\n", glVersion);
304 printf("OpenGL extensions:\n");
305 print_extension_list(glExtensions);
306 if (limits)
307 print_limits();
308 #ifdef DO_GLU
309 printf("glu version: %s\n", gluVersion);
310 printf("glu extensions:\n");
311 print_extension_list(gluExtensions);
312 #endif
313 }
314 else {
315 fprintf(stderr, "Error: glXMakeCurrent failed\n");
316 }
317
318 glXDestroyContext(dpy, ctx);
319 XDestroyWindow(dpy, win);
320 }
321
322
323 static const char *
324 visual_class_name(int cls)
325 {
326 switch (cls) {
327 case StaticColor:
328 return "StaticColor";
329 case PseudoColor:
330 return "PseudoColor";
331 case StaticGray:
332 return "StaticGray";
333 case GrayScale:
334 return "GrayScale";
335 case TrueColor:
336 return "TrueColor";
337 case DirectColor:
338 return "DirectColor";
339 default:
340 return "";
341 }
342 }
343
344
345 static const char *
346 visual_class_abbrev(int cls)
347 {
348 switch (cls) {
349 case StaticColor:
350 return "sc";
351 case PseudoColor:
352 return "pc";
353 case StaticGray:
354 return "sg";
355 case GrayScale:
356 return "gs";
357 case TrueColor:
358 return "tc";
359 case DirectColor:
360 return "dc";
361 default:
362 return "";
363 }
364 }
365
366
367 static void
368 get_visual_attribs(Display *dpy, XVisualInfo *vInfo,
369 struct visual_attribs *attribs)
370 {
371 const char *ext = glXQueryExtensionsString(dpy, vInfo->screen);
372
373 memset(attribs, 0, sizeof(struct visual_attribs));
374
375 attribs->id = vInfo->visualid;
376 #if defined(__cplusplus) || defined(c_plusplus)
377 attribs->klass = vInfo->c_class;
378 #else
379 attribs->klass = vInfo->class;
380 #endif
381 attribs->depth = vInfo->depth;
382 attribs->redMask = vInfo->red_mask;
383 attribs->greenMask = vInfo->green_mask;
384 attribs->blueMask = vInfo->blue_mask;
385 attribs->colormapSize = vInfo->colormap_size;
386 attribs->bitsPerRGB = vInfo->bits_per_rgb;
387
388 if (glXGetConfig(dpy, vInfo, GLX_USE_GL, &attribs->supportsGL) != 0)
389 return;
390 glXGetConfig(dpy, vInfo, GLX_BUFFER_SIZE, &attribs->bufferSize);
391 glXGetConfig(dpy, vInfo, GLX_LEVEL, &attribs->level);
392 glXGetConfig(dpy, vInfo, GLX_RGBA, &attribs->rgba);
393 glXGetConfig(dpy, vInfo, GLX_DOUBLEBUFFER, &attribs->doubleBuffer);
394 glXGetConfig(dpy, vInfo, GLX_STEREO, &attribs->stereo);
395 glXGetConfig(dpy, vInfo, GLX_AUX_BUFFERS, &attribs->auxBuffers);
396 glXGetConfig(dpy, vInfo, GLX_RED_SIZE, &attribs->redSize);
397 glXGetConfig(dpy, vInfo, GLX_GREEN_SIZE, &attribs->greenSize);
398 glXGetConfig(dpy, vInfo, GLX_BLUE_SIZE, &attribs->blueSize);
399 glXGetConfig(dpy, vInfo, GLX_ALPHA_SIZE, &attribs->alphaSize);
400 glXGetConfig(dpy, vInfo, GLX_DEPTH_SIZE, &attribs->depthSize);
401 glXGetConfig(dpy, vInfo, GLX_STENCIL_SIZE, &attribs->stencilSize);
402 glXGetConfig(dpy, vInfo, GLX_ACCUM_RED_SIZE, &attribs->accumRedSize);
403 glXGetConfig(dpy, vInfo, GLX_ACCUM_GREEN_SIZE, &attribs->accumGreenSize);
404 glXGetConfig(dpy, vInfo, GLX_ACCUM_BLUE_SIZE, &attribs->accumBlueSize);
405 glXGetConfig(dpy, vInfo, GLX_ACCUM_ALPHA_SIZE, &attribs->accumAlphaSize);
406
407 /* transparent pixel value not implemented yet */
408 attribs->transparent = 0;
409
410 /* multisample tests not implemented yet */
411 attribs->numSamples = 0;
412 attribs->numMultisample = 0;
413
414 #if defined(GLX_EXT_visual_rating)
415 if (ext && strstr(ext, "GLX_EXT_visual_rating")) {
416 glXGetConfig(dpy, vInfo, GLX_VISUAL_CAVEAT_EXT, &attribs->visualCaveat);
417 }
418 else {
419 attribs->visualCaveat = GLX_NONE_EXT;
420 }
421 #else
422 attribs->visualCaveat = 0;
423 #endif
424 }
425
426
427 static void
428 print_visual_attribs_verbose(const struct visual_attribs *attribs)
429 {
430 printf("Visual ID: %x depth=%d class=%s\n",
431 attribs->id, attribs->depth, visual_class_name(attribs->klass));
432 printf(" bufferSize=%d level=%d renderType=%s doubleBuffer=%d stereo=%d\n",
433 attribs->bufferSize, attribs->level, attribs->rgba ? "rgba" : "ci",
434 attribs->doubleBuffer, attribs->stereo);
435 printf(" rgba: redSize=%d greenSize=%d blueSize=%d alphaSize=%d\n",
436 attribs->redSize, attribs->greenSize,
437 attribs->blueSize, attribs->alphaSize);
438 printf(" auxBuffers=%d depthSize=%d stencilSize=%d\n",
439 attribs->auxBuffers, attribs->depthSize, attribs->stencilSize);
440 printf(" accum: redSize=%d greenSize=%d blueSize=%d alphaSize=%d\n",
441 attribs->accumRedSize, attribs->accumGreenSize,
442 attribs->accumBlueSize, attribs->accumAlphaSize);
443 printf(" multiSample=%d multiSampleBuffers=%d\n",
444 attribs->numSamples, attribs->numMultisample);
445 #ifdef GLX_EXT_visual_rating
446 if (attribs->visualCaveat == GLX_NONE_EXT || attribs->visualCaveat == 0)
447 printf(" visualCaveat=None\n");
448 else if (attribs->visualCaveat == GLX_SLOW_VISUAL_EXT)
449 printf(" visualCaveat=Slow\n");
450 else if (attribs->visualCaveat == GLX_NON_CONFORMANT_VISUAL_EXT)
451 printf(" visualCaveat=Nonconformant\n");
452 #endif
453 printf(" %s\n", attribs->transparent ? "Transparent." : "Opaque.");
454 }
455
456
457 static void
458 print_visual_attribs_short_header(void)
459 {
460 printf(" visual x bf lv rg d st colorbuffer ax dp st accumbuffer ms cav\n");
461 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");
462 printf("----------------------------------------------------------------------\n");
463 }
464
465
466 static void
467 print_visual_attribs_short(const struct visual_attribs *attribs)
468 {
469 char *caveat = NULL;
470 #ifdef GLX_EXT_visual_rating
471 if (attribs->visualCaveat == GLX_NONE_EXT || attribs->visualCaveat == 0)
472 caveat = "None";
473 else if (attribs->visualCaveat == GLX_SLOW_VISUAL_EXT)
474 caveat = "Slow";
475 else if (attribs->visualCaveat == GLX_NON_CONFORMANT_VISUAL_EXT)
476 caveat = "Ncon";
477 else
478 caveat = "None";
479 #else
480 caveat = "None";
481 #endif
482
483 printf("0x%2x %2d %2s %2d %2d %2d %1s %2s %2s %2d %2d %2d %2d %2d %2d %2d",
484 attribs->id,
485 attribs->depth,
486 visual_class_abbrev(attribs->klass),
487 attribs->transparent,
488 attribs->bufferSize,
489 attribs->level,
490 attribs->rgba ? "r" : "c",
491 attribs->doubleBuffer ? "y" : ".",
492 attribs->stereo ? "y" : ".",
493 attribs->redSize, attribs->greenSize,
494 attribs->blueSize, attribs->alphaSize,
495 attribs->auxBuffers,
496 attribs->depthSize,
497 attribs->stencilSize
498 );
499
500 printf(" %2d %2d %2d %2d %2d %1d %s\n",
501 attribs->accumRedSize, attribs->accumGreenSize,
502 attribs->accumBlueSize, attribs->accumAlphaSize,
503 attribs->numSamples, attribs->numMultisample,
504 caveat
505 );
506 }
507
508
509 static void
510 print_visual_attribs_long_header(void)
511 {
512 printf("Vis Vis Visual Trans buff lev render DB ste r g b a aux dep ste accum buffers MS MS\n");
513 printf(" ID Depth Type parent size el type reo sz sz sz sz buf th ncl r g b a num bufs\n");
514 printf("----------------------------------------------------------------------------------------------------\n");
515 }
516
517
518 static void
519 print_visual_attribs_long(const struct visual_attribs *attribs)
520 {
521 printf("0x%2x %2d %-11s %2d %2d %2d %4s %3d %3d %3d %3d %3d %3d",
522 attribs->id,
523 attribs->depth,
524 visual_class_name(attribs->klass),
525 attribs->transparent,
526 attribs->bufferSize,
527 attribs->level,
528 attribs->rgba ? "rgba" : "ci ",
529 attribs->doubleBuffer,
530 attribs->stereo,
531 attribs->redSize, attribs->greenSize,
532 attribs->blueSize, attribs->alphaSize
533 );
534
535 printf(" %3d %4d %2d %3d %3d %3d %3d %2d %2d\n",
536 attribs->auxBuffers,
537 attribs->depthSize,
538 attribs->stencilSize,
539 attribs->accumRedSize, attribs->accumGreenSize,
540 attribs->accumBlueSize, attribs->accumAlphaSize,
541 attribs->numSamples, attribs->numMultisample
542 );
543 }
544
545
546 static void
547 print_visual_info(Display *dpy, int scrnum, InfoMode mode)
548 {
549 XVisualInfo theTemplate;
550 XVisualInfo *visuals;
551 int numVisuals;
552 long mask;
553 int i;
554
555 /* get list of all visuals on this screen */
556 theTemplate.screen = scrnum;
557 mask = VisualScreenMask;
558 visuals = XGetVisualInfo(dpy, mask, &theTemplate, &numVisuals);
559
560 if (mode == Verbose) {
561 for (i = 0; i < numVisuals; i++) {
562 struct visual_attribs attribs;
563 get_visual_attribs(dpy, &visuals[i], &attribs);
564 print_visual_attribs_verbose(&attribs);
565 }
566 }
567 else if (mode == Normal) {
568 print_visual_attribs_short_header();
569 for (i = 0; i < numVisuals; i++) {
570 struct visual_attribs attribs;
571 get_visual_attribs(dpy, &visuals[i], &attribs);
572 print_visual_attribs_short(&attribs);
573 }
574 }
575 else if (mode == Wide) {
576 print_visual_attribs_long_header();
577 for (i = 0; i < numVisuals; i++) {
578 struct visual_attribs attribs;
579 get_visual_attribs(dpy, &visuals[i], &attribs);
580 print_visual_attribs_long(&attribs);
581 }
582 }
583
584 XFree(visuals);
585 }
586
587
588 /*
589 * Stand-alone Mesa doesn't really implement the GLX protocol so it
590 * doesn't really know the GLX attributes associated with an X visual.
591 * The first time a visual is presented to Mesa's pseudo-GLX it
592 * attaches ancilliary buffers to it (like depth and stencil).
593 * But that usually only works if glXChooseVisual is used.
594 * This function calls glXChooseVisual() to sort of "prime the pump"
595 * for Mesa's GLX so that the visuals that get reported actually
596 * reflect what applications will see.
597 * This has no effect when using true GLX.
598 */
599 static void
600 mesa_hack(Display *dpy, int scrnum)
601 {
602 static int attribs[] = {
603 GLX_RGBA,
604 GLX_RED_SIZE, 1,
605 GLX_GREEN_SIZE, 1,
606 GLX_BLUE_SIZE, 1,
607 GLX_DEPTH_SIZE, 1,
608 GLX_STENCIL_SIZE, 1,
609 GLX_ACCUM_RED_SIZE, 1,
610 GLX_ACCUM_GREEN_SIZE, 1,
611 GLX_ACCUM_BLUE_SIZE, 1,
612 GLX_ACCUM_ALPHA_SIZE, 1,
613 GLX_DOUBLEBUFFER,
614 None
615 };
616 XVisualInfo *visinfo;
617
618 visinfo = glXChooseVisual(dpy, scrnum, attribs);
619 if (visinfo)
620 XFree(visinfo);
621 }
622
623
624 /*
625 * Examine all visuals to find the so-called best one.
626 * We prefer deepest RGBA buffer with depth, stencil and accum
627 * that has no caveats.
628 */
629 static int
630 find_best_visual(Display *dpy, int scrnum)
631 {
632 XVisualInfo theTemplate;
633 XVisualInfo *visuals;
634 int numVisuals;
635 long mask;
636 int i;
637 struct visual_attribs bestVis;
638
639 /* get list of all visuals on this screen */
640 theTemplate.screen = scrnum;
641 mask = VisualScreenMask;
642 visuals = XGetVisualInfo(dpy, mask, &theTemplate, &numVisuals);
643
644 /* init bestVis with first visual info */
645 get_visual_attribs(dpy, &visuals[0], &bestVis);
646
647 /* try to find a "better" visual */
648 for (i = 1; i < numVisuals; i++) {
649 struct visual_attribs vis;
650
651 get_visual_attribs(dpy, &visuals[i], &vis);
652
653 /* always skip visuals with caveats */
654 if (vis.visualCaveat != GLX_NONE_EXT)
655 continue;
656
657 /* see if this vis is better than bestVis */
658 if ((!bestVis.supportsGL && vis.supportsGL) ||
659 (bestVis.visualCaveat != GLX_NONE_EXT) ||
660 (!bestVis.rgba && vis.rgba) ||
661 (!bestVis.doubleBuffer && vis.doubleBuffer) ||
662 (bestVis.redSize < vis.redSize) ||
663 (bestVis.greenSize < vis.greenSize) ||
664 (bestVis.blueSize < vis.blueSize) ||
665 (bestVis.alphaSize < vis.alphaSize) ||
666 (bestVis.depthSize < vis.depthSize) ||
667 (bestVis.stencilSize < vis.stencilSize) ||
668 (bestVis.accumRedSize < vis.accumRedSize)) {
669 /* found a better visual */
670 bestVis = vis;
671 }
672 }
673
674 XFree(visuals);
675
676 return bestVis.id;
677 }
678
679
680 static void
681 usage(void)
682 {
683 printf("Usage: glxinfo [-v] [-t] [-h] [-i] [-b] [-display <dname>]\n");
684 printf("\t-v: Print visuals info in verbose form.\n");
685 printf("\t-t: Print verbose table.\n");
686 printf("\t-display <dname>: Print GLX visuals on specified server.\n");
687 printf("\t-h: This information.\n");
688 printf("\t-i: Force an indirect rendering context.\n");
689 printf("\t-b: Find the 'best' visual and print it's number.\n");
690 printf("\t-l: Print interesting OpenGLl imits.\n");
691 }
692
693
694 int
695 main(int argc, char *argv[])
696 {
697 char *displayName = NULL;
698 Display *dpy;
699 int numScreens, scrnum;
700 InfoMode mode = Normal;
701 GLboolean findBest = GL_FALSE;
702 GLboolean limits = GL_FALSE;
703 Bool allowDirect = True;
704 int i;
705
706 for (i = 1; i < argc; i++) {
707 if (strcmp(argv[i], "-display") == 0 && i + 1 < argc) {
708 displayName = argv[i + 1];
709 i++;
710 }
711 else if (strcmp(argv[i], "-t") == 0) {
712 mode = Wide;
713 }
714 else if (strcmp(argv[i], "-v") == 0) {
715 mode = Verbose;
716 }
717 else if (strcmp(argv[i], "-b") == 0) {
718 findBest = GL_TRUE;
719 }
720 else if (strcmp(argv[i], "-i") == 0) {
721 allowDirect = False;
722 }
723 else if (strcmp(argv[i], "-l") == 0) {
724 limits = GL_TRUE;
725 }
726 else if (strcmp(argv[i], "-h") == 0) {
727 usage();
728 return 0;
729 }
730 else {
731 printf("Unknown option `%s'\n", argv[i]);
732 usage();
733 return 0;
734 }
735 }
736
737 dpy = XOpenDisplay(displayName);
738 if (!dpy) {
739 fprintf(stderr, "Error: unable to open display %s\n", displayName);
740 return -1;
741 }
742
743 if (findBest) {
744 int b;
745 mesa_hack(dpy, 0);
746 b = find_best_visual(dpy, 0);
747 printf("%d\n", b);
748 }
749 else {
750 numScreens = ScreenCount(dpy);
751 print_display_info(dpy);
752 for (scrnum = 0; scrnum < numScreens; scrnum++) {
753 mesa_hack(dpy, scrnum);
754 print_screen_info(dpy, scrnum, allowDirect, limits);
755 printf("\n");
756 print_visual_info(dpy, scrnum, mode);
757 if (scrnum + 1 < numScreens)
758 printf("\n\n");
759 }
760 }
761
762 XCloseDisplay(dpy);
763
764 return 0;
765 }