utils.c revision 0fe34b7bbc9a8e089bbb4d0fe401b09095a571eb
1/*
2 * (C) Copyright IBM Corporation 2002, 2004
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25/**
26 * \file utils.c
27 * Utility functions for DRI drivers.
28 *
29 * \author Ian Romanick <idr@us.ibm.com>
30 */
31
32#include <string.h>
33#include <stdlib.h>
34#include "main/mtypes.h"
35#include "main/cpuinfo.h"
36#include "main/extensions.h"
37#include "utils.h"
38
39
40/**
41 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
42 * is set.
43 *
44 * Is called from the drivers.
45 *
46 * \param f \c printf like format string.
47 */
48void
49__driUtilMessage(const char *f, ...)
50{
51    va_list args;
52
53    if (getenv("LIBGL_DEBUG")) {
54        fprintf(stderr, "libGL: ");
55        va_start(args, f);
56        vfprintf(stderr, f, args);
57        va_end(args);
58        fprintf(stderr, "\n");
59    }
60}
61
62
63unsigned
64driParseDebugString( const char * debug,
65		     const struct dri_debug_control * control  )
66{
67   unsigned   flag;
68
69
70   flag = 0;
71   if ( debug != NULL ) {
72      while( control->string != NULL ) {
73	 if ( !strcmp( debug, "all" ) ||
74	      strstr( debug, control->string ) != NULL ) {
75	    flag |= control->flag;
76	 }
77
78	 control++;
79      }
80   }
81
82   return flag;
83}
84
85
86
87/**
88 * Create the \c GL_RENDERER string for DRI drivers.
89 *
90 * Almost all DRI drivers use a \c GL_RENDERER string of the form:
91 *
92 *    "Mesa DRI <chip> <driver date> <AGP speed) <CPU information>"
93 *
94 * Using the supplied chip name, driver data, and AGP speed, this function
95 * creates the string.
96 *
97 * \param buffer         Buffer to hold the \c GL_RENDERER string.
98 * \param hardware_name  Name of the hardware.
99 * \param agp_mode       AGP mode (speed).
100 *
101 * \returns
102 * The length of the string stored in \c buffer.  This does \b not include
103 * the terminating \c NUL character.
104 */
105unsigned
106driGetRendererString( char * buffer, const char * hardware_name,
107		      GLuint agp_mode )
108{
109   unsigned offset;
110   char *cpu;
111
112   offset = sprintf( buffer, "Mesa DRI %s", hardware_name );
113
114   /* Append any AGP-specific information.
115    */
116   switch ( agp_mode ) {
117   case 1:
118   case 2:
119   case 4:
120   case 8:
121      offset += sprintf( & buffer[ offset ], " AGP %ux", agp_mode );
122      break;
123
124   default:
125      break;
126   }
127
128   /* Append any CPU-specific information.
129    */
130   cpu = _mesa_get_cpu_string();
131   if (cpu) {
132      offset += sprintf(buffer + offset, " %s", cpu);
133      free(cpu);
134   }
135
136   return offset;
137}
138
139
140
141
142#define need_GL_ARB_copy_buffer
143#define need_GL_ARB_draw_buffers
144#define need_GL_ARB_multisample
145#define need_GL_ARB_texture_compression
146#define need_GL_ARB_transpose_matrix
147#define need_GL_ARB_vertex_buffer_object
148#define need_GL_ARB_window_pos
149#define need_GL_EXT_compiled_vertex_array
150#define need_GL_EXT_multi_draw_arrays
151#define need_GL_EXT_polygon_offset
152#define need_GL_EXT_texture_object
153#define need_GL_EXT_vertex_array
154#define need_GL_IBM_multimode_draw_arrays
155#define need_GL_MESA_window_pos
156
157/* These are needed in *all* drivers because Mesa internally implements
158 * certain functionality in terms of functions provided by these extensions.
159 * For example, glBlendFunc is implemented by calling glBlendFuncSeparateEXT.
160 */
161#define need_GL_EXT_blend_func_separate
162#define need_GL_NV_vertex_program
163
164#include "main/remap_helper.h"
165
166static const struct dri_extension all_mesa_extensions[] = {
167   { "GL_ARB_copy_buffer",           GL_ARB_copy_buffer_functions },
168   { "GL_ARB_draw_buffers",          GL_ARB_draw_buffers_functions },
169   { "GL_ARB_multisample",           GL_ARB_multisample_functions },
170   { "GL_ARB_texture_compression",   GL_ARB_texture_compression_functions },
171   { "GL_ARB_transpose_matrix",      GL_ARB_transpose_matrix_functions },
172   { "GL_ARB_vertex_buffer_object",  GL_ARB_vertex_buffer_object_functions},
173   { "GL_ARB_window_pos",            GL_ARB_window_pos_functions },
174   { "GL_EXT_blend_func_separate",   GL_EXT_blend_func_separate_functions },
175   { "GL_EXT_compiled_vertex_array", GL_EXT_compiled_vertex_array_functions },
176   { "GL_EXT_multi_draw_arrays",     GL_EXT_multi_draw_arrays_functions },
177   { "GL_EXT_polygon_offset",        GL_EXT_polygon_offset_functions },
178   { "GL_EXT_texture_object",        GL_EXT_texture_object_functions },
179   { "GL_EXT_vertex_array",          GL_EXT_vertex_array_functions },
180   { "GL_IBM_multimode_draw_arrays", GL_IBM_multimode_draw_arrays_functions },
181   { "GL_MESA_window_pos",           GL_MESA_window_pos_functions },
182   { "GL_NV_vertex_program",         GL_NV_vertex_program_functions },
183   { NULL,                           NULL }
184};
185
186
187/**
188 * Enable and map extensions supported by the driver.
189 *
190 * When ctx is NULL, extensions are not enabled, but their functions
191 * are still mapped.  When extensions_to_enable is NULL, all static
192 * functions known to mesa core are mapped.
193 *
194 * \bug
195 * ARB_imaging isn't handled properly.  In Mesa, enabling ARB_imaging also
196 * enables all the sub-extensions that are folded into it.  This means that
197 * we need to add entry-points (via \c driInitSingleExtension) for those
198 * new functions here.
199 */
200void driInitExtensions( struct gl_context * ctx,
201			const struct dri_extension * extensions_to_enable,
202			GLboolean enable_imaging )
203{
204   static int first_time = 1;
205   unsigned   i;
206
207   if ( first_time ) {
208      first_time = 0;
209      driInitExtensions( NULL, all_mesa_extensions, GL_FALSE );
210   }
211
212   if ( (ctx != NULL) && enable_imaging ) {
213      _mesa_enable_imaging_extensions( ctx );
214   }
215
216   /* The caller is too lazy to list any extension */
217   if ( extensions_to_enable == NULL ) {
218      /* Map the static functions.  Together with those mapped by remap
219       * table, this should cover everything mesa core knows.
220       */
221      _mesa_map_static_functions();
222      return;
223   }
224
225   for ( i = 0 ; extensions_to_enable[i].name != NULL ; i++ ) {
226       driInitSingleExtension( ctx, & extensions_to_enable[i] );
227   }
228}
229
230
231
232
233/**
234 * Enable and map functions for a single extension
235 *
236 * \param ctx  Context where extension is to be enabled.
237 * \param ext  Extension that is to be enabled.
238 *
239 * \sa driInitExtensions, _mesa_enable_extension, _mesa_map_function_array
240 */
241void driInitSingleExtension( struct gl_context * ctx,
242			     const struct dri_extension * ext )
243{
244    if ( ext->functions != NULL ) {
245       _mesa_map_function_array(ext->functions);
246    }
247
248    if ( ctx != NULL ) {
249	_mesa_enable_extension( ctx, ext->name );
250    }
251}
252
253
254/**
255 * Utility function used by drivers to test the verions of other components.
256 *
257 * \param driver_name  Name of the driver.  Used in error messages.
258 * \param driActual    Actual DRI version supplied __driCreateNewScreen.
259 * \param driExpected  Minimum DRI version required by the driver.
260 * \param ddxActual    Actual DDX version supplied __driCreateNewScreen.
261 * \param ddxExpected  Minimum DDX minor and range of DDX major version required by the driver.
262 * \param drmActual    Actual DRM version supplied __driCreateNewScreen.
263 * \param drmExpected  Minimum DRM version required by the driver.
264 *
265 * \returns \c GL_TRUE if all version requirements are met.  Otherwise,
266 *          \c GL_FALSE is returned.
267 *
268 * \sa __driCreateNewScreen, driCheckDriDdxDrmVersions2
269 *
270 * \todo
271 * Now that the old \c driCheckDriDdxDrmVersions function is gone, this
272 * function and \c driCheckDriDdxDrmVersions2 should be renamed.
273 */
274GLboolean
275driCheckDriDdxDrmVersions3(const char * driver_name,
276			   const __DRIversion * driActual,
277			   const __DRIversion * driExpected,
278			   const __DRIversion * ddxActual,
279			   const __DRIutilversion2 * ddxExpected,
280			   const __DRIversion * drmActual,
281			   const __DRIversion * drmExpected)
282{
283   static const char format[] = "%s DRI driver expected %s version %d.%d.x "
284       "but got version %d.%d.%d\n";
285   static const char format2[] = "%s DRI driver expected %s version %d-%d.%d.x "
286       "but got version %d.%d.%d\n";
287
288
289   /* Check the DRI version */
290   if ( (driActual->major != driExpected->major)
291	|| (driActual->minor < driExpected->minor) ) {
292      fprintf(stderr, format, driver_name, "DRI",
293		       driExpected->major, driExpected->minor,
294		       driActual->major, driActual->minor, driActual->patch);
295      return GL_FALSE;
296   }
297
298   /* Check that the DDX driver version is compatible */
299   if ( (ddxActual->major < ddxExpected->major_min)
300	|| (ddxActual->major > ddxExpected->major_max)
301	|| (ddxActual->minor < ddxExpected->minor) ) {
302      fprintf(stderr, format2, driver_name, "DDX",
303		       ddxExpected->major_min, ddxExpected->major_max, ddxExpected->minor,
304		       ddxActual->major, ddxActual->minor, ddxActual->patch);
305      return GL_FALSE;
306   }
307
308   /* Check that the DRM driver version is compatible */
309   if ( (drmActual->major != drmExpected->major)
310	|| (drmActual->minor < drmExpected->minor) ) {
311      fprintf(stderr, format, driver_name, "DRM",
312		       drmExpected->major, drmExpected->minor,
313		       drmActual->major, drmActual->minor, drmActual->patch);
314      return GL_FALSE;
315   }
316
317   return GL_TRUE;
318}
319
320GLboolean
321driCheckDriDdxDrmVersions2(const char * driver_name,
322			   const __DRIversion * driActual,
323			   const __DRIversion * driExpected,
324			   const __DRIversion * ddxActual,
325			   const __DRIversion * ddxExpected,
326			   const __DRIversion * drmActual,
327			   const __DRIversion * drmExpected)
328{
329   __DRIutilversion2 ddx_expected;
330   ddx_expected.major_min = ddxExpected->major;
331   ddx_expected.major_max = ddxExpected->major;
332   ddx_expected.minor = ddxExpected->minor;
333   ddx_expected.patch = ddxExpected->patch;
334   return driCheckDriDdxDrmVersions3(driver_name, driActual,
335				driExpected, ddxActual, & ddx_expected,
336				drmActual, drmExpected);
337}
338
339GLboolean driClipRectToFramebuffer( const struct gl_framebuffer *buffer,
340				    GLint *x, GLint *y,
341				    GLsizei *width, GLsizei *height )
342{
343   /* left clipping */
344   if (*x < buffer->_Xmin) {
345      *width -= (buffer->_Xmin - *x);
346      *x = buffer->_Xmin;
347   }
348
349   /* right clipping */
350   if (*x + *width > buffer->_Xmax)
351      *width -= (*x + *width - buffer->_Xmax - 1);
352
353   if (*width <= 0)
354      return GL_FALSE;
355
356   /* bottom clipping */
357   if (*y < buffer->_Ymin) {
358      *height -= (buffer->_Ymin - *y);
359      *y = buffer->_Ymin;
360   }
361
362   /* top clipping */
363   if (*y + *height > buffer->_Ymax)
364      *height -= (*y + *height - buffer->_Ymax - 1);
365
366   if (*height <= 0)
367      return GL_FALSE;
368
369   return GL_TRUE;
370}
371
372/**
373 * Creates a set of \c struct gl_config that a driver will expose.
374 *
375 * A set of \c struct gl_config will be created based on the supplied
376 * parameters.  The number of modes processed will be 2 *
377 * \c num_depth_stencil_bits * \c num_db_modes.
378 *
379 * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
380 * \c db_modes, and \c visType into each \c struct gl_config element.
381 * However, the meanings of \c fb_format and \c fb_type require further
382 * explanation.  The \c fb_format specifies which color components are in
383 * each pixel and what the default order is.  For example, \c GL_RGB specifies
384 * that red, green, blue are available and red is in the "most significant"
385 * position and blue is in the "least significant".  The \c fb_type specifies
386 * the bit sizes of each component and the actual ordering.  For example, if
387 * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
388 * are the blue value, bits [10:5] are the green value, and bits [4:0] are
389 * the red value.
390 *
391 * One sublte issue is the combination of \c GL_RGB  or \c GL_BGR and either
392 * of the \c GL_UNSIGNED_INT_8_8_8_8 modes.  The resulting mask values in the
393 * \c struct gl_config structure is \b identical to the \c GL_RGBA or
394 * \c GL_BGRA case, except the \c alphaMask is zero.  This means that, as
395 * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
396 * still uses 32-bits.
397 *
398 * If in doubt, look at the tables used in the function.
399 *
400 * \param ptr_to_modes  Pointer to a pointer to a linked list of
401 *                      \c struct gl_config.  Upon completion, a pointer to
402 *                      the next element to be process will be stored here.
403 *                      If the function fails and returns \c GL_FALSE, this
404 *                      value will be unmodified, but some elements in the
405 *                      linked list may be modified.
406 * \param fb_format     Format of the framebuffer.  Currently only \c GL_RGB,
407 *                      \c GL_RGBA, \c GL_BGR, and \c GL_BGRA are supported.
408 * \param fb_type       Type of the pixels in the framebuffer.  Currently only
409 *                      \c GL_UNSIGNED_SHORT_5_6_5,
410 *                      \c GL_UNSIGNED_SHORT_5_6_5_REV,
411 *                      \c GL_UNSIGNED_INT_8_8_8_8, and
412 *                      \c GL_UNSIGNED_INT_8_8_8_8_REV are supported.
413 * \param depth_bits    Array of depth buffer sizes to be exposed.
414 * \param stencil_bits  Array of stencil buffer sizes to be exposed.
415 * \param num_depth_stencil_bits  Number of entries in both \c depth_bits and
416 *                      \c stencil_bits.
417 * \param db_modes      Array of buffer swap modes.  If an element has a
418 *                      value of \c GLX_NONE, then it represents a
419 *                      single-buffered mode.  Other valid values are
420 *                      \c GLX_SWAP_EXCHANGE_OML, \c GLX_SWAP_COPY_OML, and
421 *                      \c GLX_SWAP_UNDEFINED_OML.  See the
422 *                      GLX_OML_swap_method extension spec for more details.
423 * \param num_db_modes  Number of entries in \c db_modes.
424 * \param msaa_samples  Array of msaa sample count. 0 represents a visual
425 *                      without a multisample buffer.
426 * \param num_msaa_modes Number of entries in \c msaa_samples.
427 * \param visType       GLX visual type.  Usually either \c GLX_TRUE_COLOR or
428 *                      \c GLX_DIRECT_COLOR.
429 *
430 * \returns
431 * \c GL_TRUE on success or \c GL_FALSE on failure.  Currently the only
432 * cause of failure is a bad parameter (i.e., unsupported \c fb_format or
433 * \c fb_type).
434 *
435 * \todo
436 * There is currently no way to support packed RGB modes (i.e., modes with
437 * exactly 3 bytes per pixel) or floating-point modes.  This could probably
438 * be done by creating some new, private enums with clever names likes
439 * \c GL_UNSIGNED_3BYTE_8_8_8, \c GL_4FLOAT_32_32_32_32,
440 * \c GL_4HALF_16_16_16_16, etc.  We can cross that bridge when we come to it.
441 */
442__DRIconfig **
443driCreateConfigs(GLenum fb_format, GLenum fb_type,
444		 const uint8_t * depth_bits, const uint8_t * stencil_bits,
445		 unsigned num_depth_stencil_bits,
446		 const GLenum * db_modes, unsigned num_db_modes,
447		 const uint8_t * msaa_samples, unsigned num_msaa_modes,
448		 GLboolean enable_accum)
449{
450   static const uint8_t bits_table[4][4] = {
451     /* R  G  B  A */
452      { 3, 3, 2, 0 }, /* Any GL_UNSIGNED_BYTE_3_3_2 */
453      { 5, 6, 5, 0 }, /* Any GL_UNSIGNED_SHORT_5_6_5 */
454      { 8, 8, 8, 0 }, /* Any RGB with any GL_UNSIGNED_INT_8_8_8_8 */
455      { 8, 8, 8, 8 }  /* Any RGBA with any GL_UNSIGNED_INT_8_8_8_8 */
456   };
457
458   static const uint32_t masks_table_rgb[6][4] = {
459      { 0x000000E0, 0x0000001C, 0x00000003, 0x00000000 }, /* 3_3_2       */
460      { 0x00000007, 0x00000038, 0x000000C0, 0x00000000 }, /* 2_3_3_REV   */
461      { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5       */
462      { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5_REV   */
463      { 0xFF000000, 0x00FF0000, 0x0000FF00, 0x00000000 }, /* 8_8_8_8     */
464      { 0x000000FF, 0x0000FF00, 0x00FF0000, 0x00000000 }  /* 8_8_8_8_REV */
465   };
466
467   static const uint32_t masks_table_rgba[6][4] = {
468      { 0x000000E0, 0x0000001C, 0x00000003, 0x00000000 }, /* 3_3_2       */
469      { 0x00000007, 0x00000038, 0x000000C0, 0x00000000 }, /* 2_3_3_REV   */
470      { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5       */
471      { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5_REV   */
472      { 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF }, /* 8_8_8_8     */
473      { 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 }, /* 8_8_8_8_REV */
474   };
475
476   static const uint32_t masks_table_bgr[6][4] = {
477      { 0x00000007, 0x00000038, 0x000000C0, 0x00000000 }, /* 3_3_2       */
478      { 0x000000E0, 0x0000001C, 0x00000003, 0x00000000 }, /* 2_3_3_REV   */
479      { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5       */
480      { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5_REV   */
481      { 0x0000FF00, 0x00FF0000, 0xFF000000, 0x00000000 }, /* 8_8_8_8     */
482      { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 }, /* 8_8_8_8_REV */
483   };
484
485   static const uint32_t masks_table_bgra[6][4] = {
486      { 0x00000007, 0x00000038, 0x000000C0, 0x00000000 }, /* 3_3_2       */
487      { 0x000000E0, 0x0000001C, 0x00000003, 0x00000000 }, /* 2_3_3_REV   */
488      { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5       */
489      { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5_REV   */
490      { 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF }, /* 8_8_8_8     */
491      { 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 }, /* 8_8_8_8_REV */
492   };
493
494   static const uint8_t bytes_per_pixel[6] = {
495      1, /* 3_3_2       */
496      1, /* 2_3_3_REV   */
497      2, /* 5_6_5       */
498      2, /* 5_6_5_REV   */
499      4, /* 8_8_8_8     */
500      4  /* 8_8_8_8_REV */
501   };
502
503   const uint8_t  * bits;
504   const uint32_t * masks;
505   int index;
506   __DRIconfig **configs, **c;
507   struct gl_config *modes;
508   unsigned i, j, k, h;
509   unsigned num_modes;
510   unsigned num_accum_bits = (enable_accum) ? 2 : 1;
511
512   switch ( fb_type ) {
513      case GL_UNSIGNED_BYTE_3_3_2:
514	 index = 0;
515	 break;
516      case GL_UNSIGNED_BYTE_2_3_3_REV:
517	 index = 1;
518	 break;
519      case GL_UNSIGNED_SHORT_5_6_5:
520	 index = 2;
521	 break;
522      case GL_UNSIGNED_SHORT_5_6_5_REV:
523	 index = 3;
524	 break;
525      case GL_UNSIGNED_INT_8_8_8_8:
526	 index = 4;
527	 break;
528      case GL_UNSIGNED_INT_8_8_8_8_REV:
529	 index = 5;
530	 break;
531      default:
532	 fprintf( stderr, "[%s:%u] Unknown framebuffer type 0x%04x.\n",
533               __FUNCTION__, __LINE__, fb_type );
534	 return NULL;
535   }
536
537
538   /* Valid types are GL_UNSIGNED_SHORT_5_6_5 and GL_UNSIGNED_INT_8_8_8_8 and
539    * the _REV versions.
540    *
541    * Valid formats are GL_RGBA, GL_RGB, and GL_BGRA.
542    */
543
544   switch ( fb_format ) {
545      case GL_RGB:
546         masks = masks_table_rgb[ index ];
547         break;
548
549      case GL_RGBA:
550         masks = masks_table_rgba[ index ];
551         break;
552
553      case GL_BGR:
554         masks = masks_table_bgr[ index ];
555         break;
556
557      case GL_BGRA:
558         masks = masks_table_bgra[ index ];
559         break;
560
561      default:
562         fprintf( stderr, "[%s:%u] Unknown framebuffer format 0x%04x.\n",
563               __FUNCTION__, __LINE__, fb_format );
564         return NULL;
565   }
566
567   switch ( bytes_per_pixel[ index ] ) {
568      case 1:
569	 bits = bits_table[0];
570	 break;
571      case 2:
572	 bits = bits_table[1];
573	 break;
574      default:
575	 bits = ((fb_format == GL_RGB) || (fb_format == GL_BGR))
576	    ? bits_table[2]
577	    : bits_table[3];
578	 break;
579   }
580
581   num_modes = num_depth_stencil_bits * num_db_modes * num_accum_bits * num_msaa_modes;
582   configs = calloc(1, (num_modes + 1) * sizeof *configs);
583   if (configs == NULL)
584       return NULL;
585
586    c = configs;
587    for ( k = 0 ; k < num_depth_stencil_bits ; k++ ) {
588	for ( i = 0 ; i < num_db_modes ; i++ ) {
589	    for ( h = 0 ; h < num_msaa_modes; h++ ) {
590	    	for ( j = 0 ; j < num_accum_bits ; j++ ) {
591		    *c = malloc (sizeof **c);
592		    modes = &(*c)->modes;
593		    c++;
594
595		    memset(modes, 0, sizeof *modes);
596		    modes->redBits   = bits[0];
597		    modes->greenBits = bits[1];
598		    modes->blueBits  = bits[2];
599		    modes->alphaBits = bits[3];
600		    modes->redMask   = masks[0];
601		    modes->greenMask = masks[1];
602		    modes->blueMask  = masks[2];
603		    modes->alphaMask = masks[3];
604		    modes->rgbBits   = modes->redBits + modes->greenBits
605		    	+ modes->blueBits + modes->alphaBits;
606
607		    modes->accumRedBits   = 16 * j;
608		    modes->accumGreenBits = 16 * j;
609		    modes->accumBlueBits  = 16 * j;
610		    modes->accumAlphaBits = (masks[3] != 0) ? 16 * j : 0;
611		    modes->visualRating = (j == 0) ? GLX_NONE : GLX_SLOW_CONFIG;
612
613		    modes->stencilBits = stencil_bits[k];
614		    modes->depthBits = depth_bits[k];
615
616		    modes->transparentPixel = GLX_NONE;
617		    modes->transparentRed = GLX_DONT_CARE;
618		    modes->transparentGreen = GLX_DONT_CARE;
619		    modes->transparentBlue = GLX_DONT_CARE;
620		    modes->transparentAlpha = GLX_DONT_CARE;
621		    modes->transparentIndex = GLX_DONT_CARE;
622		    modes->rgbMode = GL_TRUE;
623
624		    if ( db_modes[i] == GLX_NONE ) {
625		    	modes->doubleBufferMode = GL_FALSE;
626		    }
627		    else {
628		    	modes->doubleBufferMode = GL_TRUE;
629		    	modes->swapMethod = db_modes[i];
630		    }
631
632		    modes->samples = msaa_samples[h];
633		    modes->sampleBuffers = modes->samples ? 1 : 0;
634
635
636		    modes->haveAccumBuffer = ((modes->accumRedBits +
637					   modes->accumGreenBits +
638					   modes->accumBlueBits +
639					   modes->accumAlphaBits) > 0);
640		    modes->haveDepthBuffer = (modes->depthBits > 0);
641		    modes->haveStencilBuffer = (modes->stencilBits > 0);
642
643		    modes->bindToTextureRgb = GL_TRUE;
644		    modes->bindToTextureRgba = GL_TRUE;
645		    modes->bindToMipmapTexture = GL_FALSE;
646		    modes->bindToTextureTargets =
647			__DRI_ATTRIB_TEXTURE_1D_BIT |
648			__DRI_ATTRIB_TEXTURE_2D_BIT |
649			__DRI_ATTRIB_TEXTURE_RECTANGLE_BIT;
650
651		    modes->sRGBCapable = GL_FALSE;
652		}
653	    }
654	}
655    }
656    *c = NULL;
657
658    return configs;
659}
660
661__DRIconfig **driConcatConfigs(__DRIconfig **a,
662			       __DRIconfig **b)
663{
664    __DRIconfig **all;
665    int i, j, index;
666
667    i = 0;
668    while (a[i] != NULL)
669	i++;
670    j = 0;
671    while (b[j] != NULL)
672	j++;
673
674    all = malloc((i + j + 1) * sizeof *all);
675    index = 0;
676    for (i = 0; a[i] != NULL; i++)
677	all[index++] = a[i];
678    for (j = 0; b[j] != NULL; j++)
679	all[index++] = b[j];
680    all[index++] = NULL;
681
682    free(a);
683    free(b);
684
685    return all;
686}
687
688#define __ATTRIB(attrib, field) \
689    { attrib, offsetof(struct gl_config, field) }
690
691static const struct { unsigned int attrib, offset; } attribMap[] = {
692    __ATTRIB(__DRI_ATTRIB_BUFFER_SIZE,			rgbBits),
693    __ATTRIB(__DRI_ATTRIB_LEVEL,			level),
694    __ATTRIB(__DRI_ATTRIB_RED_SIZE,			redBits),
695    __ATTRIB(__DRI_ATTRIB_GREEN_SIZE,			greenBits),
696    __ATTRIB(__DRI_ATTRIB_BLUE_SIZE,			blueBits),
697    __ATTRIB(__DRI_ATTRIB_ALPHA_SIZE,			alphaBits),
698    __ATTRIB(__DRI_ATTRIB_DEPTH_SIZE,			depthBits),
699    __ATTRIB(__DRI_ATTRIB_STENCIL_SIZE,			stencilBits),
700    __ATTRIB(__DRI_ATTRIB_ACCUM_RED_SIZE,		accumRedBits),
701    __ATTRIB(__DRI_ATTRIB_ACCUM_GREEN_SIZE,		accumGreenBits),
702    __ATTRIB(__DRI_ATTRIB_ACCUM_BLUE_SIZE,		accumBlueBits),
703    __ATTRIB(__DRI_ATTRIB_ACCUM_ALPHA_SIZE,		accumAlphaBits),
704    __ATTRIB(__DRI_ATTRIB_SAMPLE_BUFFERS,		sampleBuffers),
705    __ATTRIB(__DRI_ATTRIB_SAMPLES,			samples),
706    __ATTRIB(__DRI_ATTRIB_DOUBLE_BUFFER,		doubleBufferMode),
707    __ATTRIB(__DRI_ATTRIB_STEREO,			stereoMode),
708    __ATTRIB(__DRI_ATTRIB_AUX_BUFFERS,			numAuxBuffers),
709    __ATTRIB(__DRI_ATTRIB_TRANSPARENT_TYPE,		transparentPixel),
710    __ATTRIB(__DRI_ATTRIB_TRANSPARENT_INDEX_VALUE,	transparentPixel),
711    __ATTRIB(__DRI_ATTRIB_TRANSPARENT_RED_VALUE,	transparentRed),
712    __ATTRIB(__DRI_ATTRIB_TRANSPARENT_GREEN_VALUE,	transparentGreen),
713    __ATTRIB(__DRI_ATTRIB_TRANSPARENT_BLUE_VALUE,	transparentBlue),
714    __ATTRIB(__DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE,	transparentAlpha),
715    __ATTRIB(__DRI_ATTRIB_FLOAT_MODE,			floatMode),
716    __ATTRIB(__DRI_ATTRIB_RED_MASK,			redMask),
717    __ATTRIB(__DRI_ATTRIB_GREEN_MASK,			greenMask),
718    __ATTRIB(__DRI_ATTRIB_BLUE_MASK,			blueMask),
719    __ATTRIB(__DRI_ATTRIB_ALPHA_MASK,			alphaMask),
720    __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_WIDTH,		maxPbufferWidth),
721    __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_HEIGHT,		maxPbufferHeight),
722    __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_PIXELS,		maxPbufferPixels),
723    __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH,	optimalPbufferWidth),
724    __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT,	optimalPbufferHeight),
725    __ATTRIB(__DRI_ATTRIB_SWAP_METHOD,			swapMethod),
726    __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGB,		bindToTextureRgb),
727    __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGBA,		bindToTextureRgba),
728    __ATTRIB(__DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE,	bindToMipmapTexture),
729    __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS,	bindToTextureTargets),
730    __ATTRIB(__DRI_ATTRIB_YINVERTED,			yInverted),
731    __ATTRIB(__DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE,	sRGBCapable),
732
733    /* The struct field doesn't matter here, these are handled by the
734     * switch in driGetConfigAttribIndex.  We need them in the array
735     * so the iterator includes them though.*/
736    __ATTRIB(__DRI_ATTRIB_RENDER_TYPE,			level),
737    __ATTRIB(__DRI_ATTRIB_CONFIG_CAVEAT,		level),
738    __ATTRIB(__DRI_ATTRIB_SWAP_METHOD,			level)
739};
740
741#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
742
743
744/**
745 * Return the value of a configuration attribute.  The attribute is
746 * indicated by the index.
747 */
748static int
749driGetConfigAttribIndex(const __DRIconfig *config,
750			unsigned int index, unsigned int *value)
751{
752    switch (attribMap[index].attrib) {
753    case __DRI_ATTRIB_RENDER_TYPE:
754        /* no support for color index mode */
755	*value = __DRI_ATTRIB_RGBA_BIT;
756	break;
757    case __DRI_ATTRIB_CONFIG_CAVEAT:
758	if (config->modes.visualRating == GLX_NON_CONFORMANT_CONFIG)
759	    *value = __DRI_ATTRIB_NON_CONFORMANT_CONFIG;
760	else if (config->modes.visualRating == GLX_SLOW_CONFIG)
761	    *value = __DRI_ATTRIB_SLOW_BIT;
762	else
763	    *value = 0;
764	break;
765    case __DRI_ATTRIB_SWAP_METHOD:
766        /* XXX no return value??? */
767	break;
768
769    case __DRI_ATTRIB_FLOAT_MODE:
770        /* this field is not int-sized */
771        *value = config->modes.floatMode;
772        break;
773
774    default:
775        /* any other int-sized field */
776	*value = *(unsigned int *)
777	    ((char *) &config->modes + attribMap[index].offset);
778
779	break;
780    }
781
782    return GL_TRUE;
783}
784
785
786/**
787 * Get the value of a configuration attribute.
788 * \param attrib  the attribute (one of the _DRI_ATTRIB_x tokens)
789 * \param value  returns the attribute's value
790 * \return 1 for success, 0 for failure
791 */
792int
793driGetConfigAttrib(const __DRIconfig *config,
794		   unsigned int attrib, unsigned int *value)
795{
796    int i;
797
798    for (i = 0; i < ARRAY_SIZE(attribMap); i++)
799	if (attribMap[i].attrib == attrib)
800	    return driGetConfigAttribIndex(config, i, value);
801
802    return GL_FALSE;
803}
804
805
806/**
807 * Get a configuration attribute name and value, given an index.
808 * \param index  which field of the __DRIconfig to query
809 * \param attrib  returns the attribute name (one of the _DRI_ATTRIB_x tokens)
810 * \param value  returns the attribute's value
811 * \return 1 for success, 0 for failure
812 */
813int
814driIndexConfigAttrib(const __DRIconfig *config, int index,
815		     unsigned int *attrib, unsigned int *value)
816{
817    if (index >= 0 && index < ARRAY_SIZE(attribMap)) {
818	*attrib = attribMap[index].attrib;
819	return driGetConfigAttribIndex(config, index, value);
820    }
821
822    return GL_FALSE;
823}
824