new RGBA_LOGICOP_ENABLED() macro
[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: %s\n", glXIsDirect(dpy, ctx) ? "Yes" : "No");
460 printf("server glx vendor string: %s\n", serverVendor);
461 printf("server glx version string: %s\n", serverVersion);
462 printf("server glx extensions:\n");
463 print_extension_list(serverExtensions);
464 printf("client glx vendor string: %s\n", clientVendor);
465 printf("client glx version string: %s\n", clientVersion);
466 printf("client glx extensions:\n");
467 print_extension_list(clientExtensions);
468 printf("GLX version: %u.%u\n", glxVersionMajor, glxVersionMinor);
469 printf("GLX extensions:\n");
470 print_extension_list(glxExtensions);
471 printf("OpenGL vendor string: %s\n", glVendor);
472 printf("OpenGL renderer string: %s\n", glRenderer);
473 printf("OpenGL version string: %s\n", glVersion);
474 printf("OpenGL extensions:\n");
475 print_extension_list(glExtensions);
476 if (limits)
477 print_limits(glExtensions);
478 }
479 else {
480 fprintf(stderr, "Error: glXMakeCurrent failed\n");
481 }
482
483 glXDestroyContext(dpy, ctx);
484 XFree(visinfo);
485 XDestroyWindow(dpy, win);
486 }
487
488
489 static const char *
490 visual_class_name(int cls)
491 {
492 switch (cls) {
493 case StaticColor:
494 return "StaticColor";
495 case PseudoColor:
496 return "PseudoColor";
497 case StaticGray:
498 return "StaticGray";
499 case GrayScale:
500 return "GrayScale";
501 case TrueColor:
502 return "TrueColor";
503 case DirectColor:
504 return "DirectColor";
505 default:
506 return "";
507 }
508 }
509
510
511 static const char *
512 visual_class_abbrev(int cls)
513 {
514 switch (cls) {
515 case StaticColor:
516 return "sc";
517 case PseudoColor:
518 return "pc";
519 case StaticGray:
520 return "sg";
521 case GrayScale:
522 return "gs";
523 case TrueColor:
524 return "tc";
525 case DirectColor:
526 return "dc";
527 default:
528 return "";
529 }
530 }
531
532
533 static void
534 get_visual_attribs(Display *dpy, XVisualInfo *vInfo,
535 struct visual_attribs *attribs)
536 {
537 const char *ext = glXQueryExtensionsString(dpy, vInfo->screen);
538
539 memset(attribs, 0, sizeof(struct visual_attribs));
540
541 attribs->id = vInfo->visualid;
542 #if defined(__cplusplus) || defined(c_plusplus)
543 attribs->klass = vInfo->c_class;
544 #else
545 attribs->klass = vInfo->class;
546 #endif
547 attribs->depth = vInfo->depth;
548 attribs->redMask = vInfo->red_mask;
549 attribs->greenMask = vInfo->green_mask;
550 attribs->blueMask = vInfo->blue_mask;
551 attribs->colormapSize = vInfo->colormap_size;
552 attribs->bitsPerRGB = vInfo->bits_per_rgb;
553
554 if (glXGetConfig(dpy, vInfo, GLX_USE_GL, &attribs->supportsGL) != 0)
555 return;
556 glXGetConfig(dpy, vInfo, GLX_BUFFER_SIZE, &attribs->bufferSize);
557 glXGetConfig(dpy, vInfo, GLX_LEVEL, &attribs->level);
558 glXGetConfig(dpy, vInfo, GLX_RGBA, &attribs->rgba);
559 glXGetConfig(dpy, vInfo, GLX_DOUBLEBUFFER, &attribs->doubleBuffer);
560 glXGetConfig(dpy, vInfo, GLX_STEREO, &attribs->stereo);
561 glXGetConfig(dpy, vInfo, GLX_AUX_BUFFERS, &attribs->auxBuffers);
562 glXGetConfig(dpy, vInfo, GLX_RED_SIZE, &attribs->redSize);
563 glXGetConfig(dpy, vInfo, GLX_GREEN_SIZE, &attribs->greenSize);
564 glXGetConfig(dpy, vInfo, GLX_BLUE_SIZE, &attribs->blueSize);
565 glXGetConfig(dpy, vInfo, GLX_ALPHA_SIZE, &attribs->alphaSize);
566 glXGetConfig(dpy, vInfo, GLX_DEPTH_SIZE, &attribs->depthSize);
567 glXGetConfig(dpy, vInfo, GLX_STENCIL_SIZE, &attribs->stencilSize);
568 glXGetConfig(dpy, vInfo, GLX_ACCUM_RED_SIZE, &attribs->accumRedSize);
569 glXGetConfig(dpy, vInfo, GLX_ACCUM_GREEN_SIZE, &attribs->accumGreenSize);
570 glXGetConfig(dpy, vInfo, GLX_ACCUM_BLUE_SIZE, &attribs->accumBlueSize);
571 glXGetConfig(dpy, vInfo, GLX_ACCUM_ALPHA_SIZE, &attribs->accumAlphaSize);
572
573 /* get transparent pixel stuff */
574 glXGetConfig(dpy, vInfo,GLX_TRANSPARENT_TYPE, &attribs->transparentType);
575 if (attribs->transparentType == GLX_TRANSPARENT_RGB) {
576 glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_RED_VALUE, &attribs->transparentRedValue);
577 glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_GREEN_VALUE, &attribs->transparentGreenValue);
578 glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_BLUE_VALUE, &attribs->transparentBlueValue);
579 glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_ALPHA_VALUE, &attribs->transparentAlphaValue);
580 }
581 else if (attribs->transparentType == GLX_TRANSPARENT_INDEX) {
582 glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_INDEX_VALUE, &attribs->transparentIndexValue);
583 }
584
585 /* multisample attribs */
586 #ifdef GLX_ARB_multisample
587 if (ext && strstr(ext, "GLX_ARB_multisample") == 0) {
588 glXGetConfig(dpy, vInfo, GLX_SAMPLE_BUFFERS_ARB, &attribs->numMultisample);
589 glXGetConfig(dpy, vInfo, GLX_SAMPLES_ARB, &attribs->numSamples);
590 }
591 #endif
592 else {
593 attribs->numSamples = 0;
594 attribs->numMultisample = 0;
595 }
596
597 #if defined(GLX_EXT_visual_rating)
598 if (ext && strstr(ext, "GLX_EXT_visual_rating")) {
599 glXGetConfig(dpy, vInfo, GLX_VISUAL_CAVEAT_EXT, &attribs->visualCaveat);
600 }
601 else {
602 attribs->visualCaveat = GLX_NONE_EXT;
603 }
604 #else
605 attribs->visualCaveat = 0;
606 #endif
607 }
608
609
610 static void
611 print_visual_attribs_verbose(const struct visual_attribs *attribs)
612 {
613 printf("Visual ID: %x depth=%d class=%s\n",
614 attribs->id, attribs->depth, visual_class_name(attribs->klass));
615 printf(" bufferSize=%d level=%d renderType=%s doubleBuffer=%d stereo=%d\n",
616 attribs->bufferSize, attribs->level, attribs->rgba ? "rgba" : "ci",
617 attribs->doubleBuffer, attribs->stereo);
618 printf(" rgba: redSize=%d greenSize=%d blueSize=%d alphaSize=%d\n",
619 attribs->redSize, attribs->greenSize,
620 attribs->blueSize, attribs->alphaSize);
621 printf(" auxBuffers=%d depthSize=%d stencilSize=%d\n",
622 attribs->auxBuffers, attribs->depthSize, attribs->stencilSize);
623 printf(" accum: redSize=%d greenSize=%d blueSize=%d alphaSize=%d\n",
624 attribs->accumRedSize, attribs->accumGreenSize,
625 attribs->accumBlueSize, attribs->accumAlphaSize);
626 printf(" multiSample=%d multiSampleBuffers=%d\n",
627 attribs->numSamples, attribs->numMultisample);
628 #ifdef GLX_EXT_visual_rating
629 if (attribs->visualCaveat == GLX_NONE_EXT || attribs->visualCaveat == 0)
630 printf(" visualCaveat=None\n");
631 else if (attribs->visualCaveat == GLX_SLOW_VISUAL_EXT)
632 printf(" visualCaveat=Slow\n");
633 else if (attribs->visualCaveat == GLX_NON_CONFORMANT_VISUAL_EXT)
634 printf(" visualCaveat=Nonconformant\n");
635 #endif
636 if (attribs->transparentType == GLX_NONE) {
637 printf(" Opaque.\n");
638 }
639 else if (attribs->transparentType == GLX_TRANSPARENT_RGB) {
640 printf(" Transparent RGB: Red=%d Green=%d Blue=%d Alpha=%d\n",attribs->transparentRedValue,attribs->transparentGreenValue,attribs->transparentBlueValue,attribs->transparentAlphaValue);
641 }
642 else if (attribs->transparentType == GLX_TRANSPARENT_INDEX) {
643 printf(" Transparent index=%d\n",attribs->transparentIndexValue);
644 }
645 }
646
647
648 static void
649 print_visual_attribs_short_header(void)
650 {
651 printf(" visual x bf lv rg d st colorbuffer ax dp st accumbuffer ms cav\n");
652 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");
653 printf("----------------------------------------------------------------------\n");
654 }
655
656
657 static void
658 print_visual_attribs_short(const struct visual_attribs *attribs)
659 {
660 char *caveat = NULL;
661 #ifdef GLX_EXT_visual_rating
662 if (attribs->visualCaveat == GLX_NONE_EXT || attribs->visualCaveat == 0)
663 caveat = "None";
664 else if (attribs->visualCaveat == GLX_SLOW_VISUAL_EXT)
665 caveat = "Slow";
666 else if (attribs->visualCaveat == GLX_NON_CONFORMANT_VISUAL_EXT)
667 caveat = "Ncon";
668 else
669 caveat = "None";
670 #else
671 caveat = "None";
672 #endif
673
674 printf("0x%2x %2d %2s %2d %2d %2d %1s %2s %2s %2d %2d %2d %2d %2d %2d %2d",
675 attribs->id,
676 attribs->depth,
677 visual_class_abbrev(attribs->klass),
678 attribs->transparentType != GLX_NONE,
679 attribs->bufferSize,
680 attribs->level,
681 attribs->rgba ? "r" : "c",
682 attribs->doubleBuffer ? "y" : ".",
683 attribs->stereo ? "y" : ".",
684 attribs->redSize, attribs->greenSize,
685 attribs->blueSize, attribs->alphaSize,
686 attribs->auxBuffers,
687 attribs->depthSize,
688 attribs->stencilSize
689 );
690
691 printf(" %2d %2d %2d %2d %2d %1d %s\n",
692 attribs->accumRedSize, attribs->accumGreenSize,
693 attribs->accumBlueSize, attribs->accumAlphaSize,
694 attribs->numSamples, attribs->numMultisample,
695 caveat
696 );
697 }
698
699
700 static void
701 print_visual_attribs_long_header(void)
702 {
703 printf("Vis Vis Visual Trans buff lev render DB ste r g b a aux dep ste accum buffers MS MS\n");
704 printf(" ID Depth Type parent size el type reo sz sz sz sz buf th ncl r g b a num bufs\n");
705 printf("----------------------------------------------------------------------------------------------------\n");
706 }
707
708
709 static void
710 print_visual_attribs_long(const struct visual_attribs *attribs)
711 {
712 printf("0x%2x %2d %-11s %2d %2d %2d %4s %3d %3d %3d %3d %3d %3d",
713 attribs->id,
714 attribs->depth,
715 visual_class_name(attribs->klass),
716 attribs->transparentType != GLX_NONE,
717 attribs->bufferSize,
718 attribs->level,
719 attribs->rgba ? "rgba" : "ci ",
720 attribs->doubleBuffer,
721 attribs->stereo,
722 attribs->redSize, attribs->greenSize,
723 attribs->blueSize, attribs->alphaSize
724 );
725
726 printf(" %3d %4d %2d %3d %3d %3d %3d %2d %2d\n",
727 attribs->auxBuffers,
728 attribs->depthSize,
729 attribs->stencilSize,
730 attribs->accumRedSize, attribs->accumGreenSize,
731 attribs->accumBlueSize, attribs->accumAlphaSize,
732 attribs->numSamples, attribs->numMultisample
733 );
734 }
735
736
737 static void
738 print_visual_info(Display *dpy, int scrnum, InfoMode mode)
739 {
740 XVisualInfo theTemplate;
741 XVisualInfo *visuals;
742 int numVisuals;
743 long mask;
744 int i;
745
746 /* get list of all visuals on this screen */
747 theTemplate.screen = scrnum;
748 mask = VisualScreenMask;
749 visuals = XGetVisualInfo(dpy, mask, &theTemplate, &numVisuals);
750
751 if (mode == Verbose) {
752 for (i = 0; i < numVisuals; i++) {
753 struct visual_attribs attribs;
754 get_visual_attribs(dpy, &visuals[i], &attribs);
755 print_visual_attribs_verbose(&attribs);
756 }
757 }
758 else if (mode == Normal) {
759 print_visual_attribs_short_header();
760 for (i = 0; i < numVisuals; i++) {
761 struct visual_attribs attribs;
762 get_visual_attribs(dpy, &visuals[i], &attribs);
763 print_visual_attribs_short(&attribs);
764 }
765 }
766 else if (mode == Wide) {
767 print_visual_attribs_long_header();
768 for (i = 0; i < numVisuals; i++) {
769 struct visual_attribs attribs;
770 get_visual_attribs(dpy, &visuals[i], &attribs);
771 print_visual_attribs_long(&attribs);
772 }
773 }
774
775 XFree(visuals);
776 }
777
778
779 /*
780 * Stand-alone Mesa doesn't really implement the GLX protocol so it
781 * doesn't really know the GLX attributes associated with an X visual.
782 * The first time a visual is presented to Mesa's pseudo-GLX it
783 * attaches ancilliary buffers to it (like depth and stencil).
784 * But that usually only works if glXChooseVisual is used.
785 * This function calls glXChooseVisual() to sort of "prime the pump"
786 * for Mesa's GLX so that the visuals that get reported actually
787 * reflect what applications will see.
788 * This has no effect when using true GLX.
789 */
790 static void
791 mesa_hack(Display *dpy, int scrnum)
792 {
793 static int attribs[] = {
794 GLX_RGBA,
795 GLX_RED_SIZE, 1,
796 GLX_GREEN_SIZE, 1,
797 GLX_BLUE_SIZE, 1,
798 GLX_DEPTH_SIZE, 1,
799 GLX_STENCIL_SIZE, 1,
800 GLX_ACCUM_RED_SIZE, 1,
801 GLX_ACCUM_GREEN_SIZE, 1,
802 GLX_ACCUM_BLUE_SIZE, 1,
803 GLX_ACCUM_ALPHA_SIZE, 1,
804 GLX_DOUBLEBUFFER,
805 None
806 };
807 XVisualInfo *visinfo;
808
809 visinfo = glXChooseVisual(dpy, scrnum, attribs);
810 if (visinfo)
811 XFree(visinfo);
812 }
813
814
815 /*
816 * Examine all visuals to find the so-called best one.
817 * We prefer deepest RGBA buffer with depth, stencil and accum
818 * that has no caveats.
819 */
820 static int
821 find_best_visual(Display *dpy, int scrnum)
822 {
823 XVisualInfo theTemplate;
824 XVisualInfo *visuals;
825 int numVisuals;
826 long mask;
827 int i;
828 struct visual_attribs bestVis;
829
830 /* get list of all visuals on this screen */
831 theTemplate.screen = scrnum;
832 mask = VisualScreenMask;
833 visuals = XGetVisualInfo(dpy, mask, &theTemplate, &numVisuals);
834
835 /* init bestVis with first visual info */
836 get_visual_attribs(dpy, &visuals[0], &bestVis);
837
838 /* try to find a "better" visual */
839 for (i = 1; i < numVisuals; i++) {
840 struct visual_attribs vis;
841
842 get_visual_attribs(dpy, &visuals[i], &vis);
843
844 /* always skip visuals with caveats */
845 if (vis.visualCaveat != GLX_NONE_EXT)
846 continue;
847
848 /* see if this vis is better than bestVis */
849 if ((!bestVis.supportsGL && vis.supportsGL) ||
850 (bestVis.visualCaveat != GLX_NONE_EXT) ||
851 (!bestVis.rgba && vis.rgba) ||
852 (!bestVis.doubleBuffer && vis.doubleBuffer) ||
853 (bestVis.redSize < vis.redSize) ||
854 (bestVis.greenSize < vis.greenSize) ||
855 (bestVis.blueSize < vis.blueSize) ||
856 (bestVis.alphaSize < vis.alphaSize) ||
857 (bestVis.depthSize < vis.depthSize) ||
858 (bestVis.stencilSize < vis.stencilSize) ||
859 (bestVis.accumRedSize < vis.accumRedSize)) {
860 /* found a better visual */
861 bestVis = vis;
862 }
863 }
864
865 XFree(visuals);
866
867 return bestVis.id;
868 }
869
870
871 static void
872 usage(void)
873 {
874 printf("Usage: glxinfo [-v] [-t] [-h] [-i] [-b] [-display <dname>]\n");
875 printf("\t-v: Print visuals info in verbose form.\n");
876 printf("\t-t: Print verbose table.\n");
877 printf("\t-display <dname>: Print GLX visuals on specified server.\n");
878 printf("\t-h: This information.\n");
879 printf("\t-i: Force an indirect rendering context.\n");
880 printf("\t-b: Find the 'best' visual and print it's number.\n");
881 printf("\t-l: Print interesting OpenGL limits.\n");
882 }
883
884
885 int
886 main(int argc, char *argv[])
887 {
888 char *displayName = NULL;
889 Display *dpy;
890 int numScreens, scrnum;
891 InfoMode mode = Normal;
892 GLboolean findBest = GL_FALSE;
893 GLboolean limits = GL_FALSE;
894 Bool allowDirect = True;
895 int i;
896
897 for (i = 1; i < argc; i++) {
898 if (strcmp(argv[i], "-display") == 0 && i + 1 < argc) {
899 displayName = argv[i + 1];
900 i++;
901 }
902 else if (strcmp(argv[i], "-t") == 0) {
903 mode = Wide;
904 }
905 else if (strcmp(argv[i], "-v") == 0) {
906 mode = Verbose;
907 }
908 else if (strcmp(argv[i], "-b") == 0) {
909 findBest = GL_TRUE;
910 }
911 else if (strcmp(argv[i], "-i") == 0) {
912 allowDirect = False;
913 }
914 else if (strcmp(argv[i], "-l") == 0) {
915 limits = GL_TRUE;
916 }
917 else if (strcmp(argv[i], "-h") == 0) {
918 usage();
919 return 0;
920 }
921 else {
922 printf("Unknown option `%s'\n", argv[i]);
923 usage();
924 return 0;
925 }
926 }
927
928 dpy = XOpenDisplay(displayName);
929 if (!dpy) {
930 fprintf(stderr, "Error: unable to open display %s\n", displayName);
931 return -1;
932 }
933
934 if (findBest) {
935 int b;
936 mesa_hack(dpy, 0);
937 b = find_best_visual(dpy, 0);
938 printf("%d\n", b);
939 }
940 else {
941 numScreens = ScreenCount(dpy);
942 print_display_info(dpy);
943 for (scrnum = 0; scrnum < numScreens; scrnum++) {
944 mesa_hack(dpy, scrnum);
945 print_screen_info(dpy, scrnum, allowDirect, limits);
946 printf("\n");
947 print_visual_info(dpy, scrnum, mode);
948 if (scrnum + 1 < numScreens)
949 printf("\n\n");
950 }
951 }
952
953 XCloseDisplay(dpy);
954
955 return 0;
956 }