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