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