Merge branch '7.8'
[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_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
166 static const struct dri_extension all_mesa_extensions[] = {
167 { "GL_ARB_draw_buffers", GL_ARB_draw_buffers_functions },
168 { "GL_ARB_multisample", GL_ARB_multisample_functions },
169 { "GL_ARB_texture_compression", GL_ARB_texture_compression_functions },
170 { "GL_ARB_transpose_matrix", GL_ARB_transpose_matrix_functions },
171 { "GL_ARB_vertex_buffer_object", GL_ARB_vertex_buffer_object_functions},
172 { "GL_ARB_window_pos", GL_ARB_window_pos_functions },
173 { "GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions },
174 { "GL_EXT_compiled_vertex_array", GL_EXT_compiled_vertex_array_functions },
175 { "GL_EXT_multi_draw_arrays", GL_EXT_multi_draw_arrays_functions },
176 { "GL_EXT_polygon_offset", GL_EXT_polygon_offset_functions },
177 { "GL_EXT_texture_object", GL_EXT_texture_object_functions },
178 { "GL_EXT_vertex_array", GL_EXT_vertex_array_functions },
179 { "GL_IBM_multimode_draw_arrays", GL_IBM_multimode_draw_arrays_functions },
180 { "GL_MESA_window_pos", GL_MESA_window_pos_functions },
181 { "GL_NV_vertex_program", GL_NV_vertex_program_functions },
182 { NULL, NULL }
183 };
184
185
186 /**
187 * Enable and map extensions supported by the driver.
188 *
189 * When ctx is NULL, extensions are not enabled, but their functions
190 * are still mapped. When extensions_to_enable is NULL, all static
191 * functions known to mesa core are mapped.
192 *
193 * \bug
194 * ARB_imaging isn't handled properly. In Mesa, enabling ARB_imaging also
195 * enables all the sub-extensions that are folded into it. This means that
196 * we need to add entry-points (via \c driInitSingleExtension) for those
197 * new functions here.
198 */
199 void driInitExtensions( GLcontext * ctx,
200 const struct dri_extension * extensions_to_enable,
201 GLboolean enable_imaging )
202 {
203 static int first_time = 1;
204 unsigned i;
205
206 if ( first_time ) {
207 first_time = 0;
208 driInitExtensions( NULL, all_mesa_extensions, GL_FALSE );
209 }
210
211 if ( (ctx != NULL) && enable_imaging ) {
212 _mesa_enable_imaging_extensions( ctx );
213 }
214
215 /* The caller is too lazy to list any extension */
216 if ( extensions_to_enable == NULL ) {
217 /* Map the static functions. Together with those mapped by remap
218 * table, this should cover everything mesa core knows.
219 */
220 _mesa_map_static_functions();
221 return;
222 }
223
224 for ( i = 0 ; extensions_to_enable[i].name != NULL ; i++ ) {
225 driInitSingleExtension( ctx, & extensions_to_enable[i] );
226 }
227 }
228
229
230
231
232 /**
233 * Enable and map functions for a single extension
234 *
235 * \param ctx Context where extension is to be enabled.
236 * \param ext Extension that is to be enabled.
237 *
238 * \sa driInitExtensions, _mesa_enable_extension, _mesa_map_function_array
239 */
240 void driInitSingleExtension( GLcontext * ctx,
241 const struct dri_extension * ext )
242 {
243 if ( ext->functions != NULL ) {
244 _mesa_map_function_array(ext->functions);
245 }
246
247 if ( ctx != NULL ) {
248 _mesa_enable_extension( ctx, ext->name );
249 }
250 }
251
252
253 /**
254 * Utility function used by drivers to test the verions of other components.
255 *
256 * \param driver_name Name of the driver. Used in error messages.
257 * \param driActual Actual DRI version supplied __driCreateNewScreen.
258 * \param driExpected Minimum DRI version required by the driver.
259 * \param ddxActual Actual DDX version supplied __driCreateNewScreen.
260 * \param ddxExpected Minimum DDX minor and range of DDX major version required by the driver.
261 * \param drmActual Actual DRM version supplied __driCreateNewScreen.
262 * \param drmExpected Minimum DRM version required by the driver.
263 *
264 * \returns \c GL_TRUE if all version requirements are met. Otherwise,
265 * \c GL_FALSE is returned.
266 *
267 * \sa __driCreateNewScreen, driCheckDriDdxDrmVersions2
268 *
269 * \todo
270 * Now that the old \c driCheckDriDdxDrmVersions function is gone, this
271 * function and \c driCheckDriDdxDrmVersions2 should be renamed.
272 */
273 GLboolean
274 driCheckDriDdxDrmVersions3(const char * driver_name,
275 const __DRIversion * driActual,
276 const __DRIversion * driExpected,
277 const __DRIversion * ddxActual,
278 const __DRIutilversion2 * ddxExpected,
279 const __DRIversion * drmActual,
280 const __DRIversion * drmExpected)
281 {
282 static const char format[] = "%s DRI driver expected %s version %d.%d.x "
283 "but got version %d.%d.%d\n";
284 static const char format2[] = "%s DRI driver expected %s version %d-%d.%d.x "
285 "but got version %d.%d.%d\n";
286
287
288 /* Check the DRI version */
289 if ( (driActual->major != driExpected->major)
290 || (driActual->minor < driExpected->minor) ) {
291 fprintf(stderr, format, driver_name, "DRI",
292 driExpected->major, driExpected->minor,
293 driActual->major, driActual->minor, driActual->patch);
294 return GL_FALSE;
295 }
296
297 /* Check that the DDX driver version is compatible */
298 if ( (ddxActual->major < ddxExpected->major_min)
299 || (ddxActual->major > ddxExpected->major_max)
300 || (ddxActual->minor < ddxExpected->minor) ) {
301 fprintf(stderr, format2, driver_name, "DDX",
302 ddxExpected->major_min, ddxExpected->major_max, ddxExpected->minor,
303 ddxActual->major, ddxActual->minor, ddxActual->patch);
304 return GL_FALSE;
305 }
306
307 /* Check that the DRM driver version is compatible */
308 if ( (drmActual->major != drmExpected->major)
309 || (drmActual->minor < drmExpected->minor) ) {
310 fprintf(stderr, format, driver_name, "DRM",
311 drmExpected->major, drmExpected->minor,
312 drmActual->major, drmActual->minor, drmActual->patch);
313 return GL_FALSE;
314 }
315
316 return GL_TRUE;
317 }
318
319 GLboolean
320 driCheckDriDdxDrmVersions2(const char * driver_name,
321 const __DRIversion * driActual,
322 const __DRIversion * driExpected,
323 const __DRIversion * ddxActual,
324 const __DRIversion * ddxExpected,
325 const __DRIversion * drmActual,
326 const __DRIversion * drmExpected)
327 {
328 __DRIutilversion2 ddx_expected;
329 ddx_expected.major_min = ddxExpected->major;
330 ddx_expected.major_max = ddxExpected->major;
331 ddx_expected.minor = ddxExpected->minor;
332 ddx_expected.patch = ddxExpected->patch;
333 return driCheckDriDdxDrmVersions3(driver_name, driActual,
334 driExpected, ddxActual, & ddx_expected,
335 drmActual, drmExpected);
336 }
337
338 GLboolean driClipRectToFramebuffer( const GLframebuffer *buffer,
339 GLint *x, GLint *y,
340 GLsizei *width, GLsizei *height )
341 {
342 /* left clipping */
343 if (*x < buffer->_Xmin) {
344 *width -= (buffer->_Xmin - *x);
345 *x = buffer->_Xmin;
346 }
347
348 /* right clipping */
349 if (*x + *width > buffer->_Xmax)
350 *width -= (*x + *width - buffer->_Xmax - 1);
351
352 if (*width <= 0)
353 return GL_FALSE;
354
355 /* bottom clipping */
356 if (*y < buffer->_Ymin) {
357 *height -= (buffer->_Ymin - *y);
358 *y = buffer->_Ymin;
359 }
360
361 /* top clipping */
362 if (*y + *height > buffer->_Ymax)
363 *height -= (*y + *height - buffer->_Ymax - 1);
364
365 if (*height <= 0)
366 return GL_FALSE;
367
368 return GL_TRUE;
369 }
370
371 /**
372 * Creates a set of \c __GLcontextModes that a driver will expose.
373 *
374 * A set of \c __GLcontextModes will be created based on the supplied
375 * parameters. The number of modes processed will be 2 *
376 * \c num_depth_stencil_bits * \c num_db_modes.
377 *
378 * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
379 * \c db_modes, and \c visType into each \c __GLcontextModes element.
380 * However, the meanings of \c fb_format and \c fb_type require further
381 * explanation. The \c fb_format specifies which color components are in
382 * each pixel and what the default order is. For example, \c GL_RGB specifies
383 * that red, green, blue are available and red is in the "most significant"
384 * position and blue is in the "least significant". The \c fb_type specifies
385 * the bit sizes of each component and the actual ordering. For example, if
386 * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
387 * are the blue value, bits [10:5] are the green value, and bits [4:0] are
388 * the red value.
389 *
390 * One sublte issue is the combination of \c GL_RGB or \c GL_BGR and either
391 * of the \c GL_UNSIGNED_INT_8_8_8_8 modes. The resulting mask values in the
392 * \c __GLcontextModes structure is \b identical to the \c GL_RGBA or
393 * \c GL_BGRA case, except the \c alphaMask is zero. This means that, as
394 * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
395 * still uses 32-bits.
396 *
397 * If in doubt, look at the tables used in the function.
398 *
399 * \param ptr_to_modes Pointer to a pointer to a linked list of
400 * \c __GLcontextModes. Upon completion, a pointer to
401 * the next element to be process will be stored here.
402 * If the function fails and returns \c GL_FALSE, this
403 * value will be unmodified, but some elements in the
404 * linked list may be modified.
405 * \param fb_format Format of the framebuffer. Currently only \c GL_RGB,
406 * \c GL_RGBA, \c GL_BGR, and \c GL_BGRA are supported.
407 * \param fb_type Type of the pixels in the framebuffer. Currently only
408 * \c GL_UNSIGNED_SHORT_5_6_5,
409 * \c GL_UNSIGNED_SHORT_5_6_5_REV,
410 * \c GL_UNSIGNED_INT_8_8_8_8, and
411 * \c GL_UNSIGNED_INT_8_8_8_8_REV are supported.
412 * \param depth_bits Array of depth buffer sizes to be exposed.
413 * \param stencil_bits Array of stencil buffer sizes to be exposed.
414 * \param num_depth_stencil_bits Number of entries in both \c depth_bits and
415 * \c stencil_bits.
416 * \param db_modes Array of buffer swap modes. If an element has a
417 * value of \c GLX_NONE, then it represents a
418 * single-buffered mode. Other valid values are
419 * \c GLX_SWAP_EXCHANGE_OML, \c GLX_SWAP_COPY_OML, and
420 * \c GLX_SWAP_UNDEFINED_OML. See the
421 * GLX_OML_swap_method extension spec for more details.
422 * \param num_db_modes Number of entries in \c db_modes.
423 * \param msaa_samples Array of msaa sample count. 0 represents a visual
424 * without a multisample buffer.
425 * \param num_msaa_modes Number of entries in \c msaa_samples.
426 * \param visType GLX visual type. Usually either \c GLX_TRUE_COLOR or
427 * \c GLX_DIRECT_COLOR.
428 *
429 * \returns
430 * \c GL_TRUE on success or \c GL_FALSE on failure. Currently the only
431 * cause of failure is a bad parameter (i.e., unsupported \c fb_format or
432 * \c fb_type).
433 *
434 * \todo
435 * There is currently no way to support packed RGB modes (i.e., modes with
436 * exactly 3 bytes per pixel) or floating-point modes. This could probably
437 * be done by creating some new, private enums with clever names likes
438 * \c GL_UNSIGNED_3BYTE_8_8_8, \c GL_4FLOAT_32_32_32_32,
439 * \c GL_4HALF_16_16_16_16, etc. We can cross that bridge when we come to it.
440 */
441 __DRIconfig **
442 driCreateConfigs(GLenum fb_format, GLenum fb_type,
443 const uint8_t * depth_bits, const uint8_t * stencil_bits,
444 unsigned num_depth_stencil_bits,
445 const GLenum * db_modes, unsigned num_db_modes,
446 const uint8_t * msaa_samples, unsigned num_msaa_modes,
447 GLboolean enable_accum)
448 {
449 static const uint8_t bits_table[4][4] = {
450 /* R G B A */
451 { 3, 3, 2, 0 }, /* Any GL_UNSIGNED_BYTE_3_3_2 */
452 { 5, 6, 5, 0 }, /* Any GL_UNSIGNED_SHORT_5_6_5 */
453 { 8, 8, 8, 0 }, /* Any RGB with any GL_UNSIGNED_INT_8_8_8_8 */
454 { 8, 8, 8, 8 } /* Any RGBA with any GL_UNSIGNED_INT_8_8_8_8 */
455 };
456
457 static const uint32_t masks_table_rgb[6][4] = {
458 { 0x000000E0, 0x0000001C, 0x00000003, 0x00000000 }, /* 3_3_2 */
459 { 0x00000007, 0x00000038, 0x000000C0, 0x00000000 }, /* 2_3_3_REV */
460 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5 */
461 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5_REV */
462 { 0xFF000000, 0x00FF0000, 0x0000FF00, 0x00000000 }, /* 8_8_8_8 */
463 { 0x000000FF, 0x0000FF00, 0x00FF0000, 0x00000000 } /* 8_8_8_8_REV */
464 };
465
466 static const uint32_t masks_table_rgba[6][4] = {
467 { 0x000000E0, 0x0000001C, 0x00000003, 0x00000000 }, /* 3_3_2 */
468 { 0x00000007, 0x00000038, 0x000000C0, 0x00000000 }, /* 2_3_3_REV */
469 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5 */
470 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5_REV */
471 { 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF }, /* 8_8_8_8 */
472 { 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 }, /* 8_8_8_8_REV */
473 };
474
475 static const uint32_t masks_table_bgr[6][4] = {
476 { 0x00000007, 0x00000038, 0x000000C0, 0x00000000 }, /* 3_3_2 */
477 { 0x000000E0, 0x0000001C, 0x00000003, 0x00000000 }, /* 2_3_3_REV */
478 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5 */
479 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5_REV */
480 { 0x0000FF00, 0x00FF0000, 0xFF000000, 0x00000000 }, /* 8_8_8_8 */
481 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 }, /* 8_8_8_8_REV */
482 };
483
484 static const uint32_t masks_table_bgra[6][4] = {
485 { 0x00000007, 0x00000038, 0x000000C0, 0x00000000 }, /* 3_3_2 */
486 { 0x000000E0, 0x0000001C, 0x00000003, 0x00000000 }, /* 2_3_3_REV */
487 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5 */
488 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5_REV */
489 { 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF }, /* 8_8_8_8 */
490 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 }, /* 8_8_8_8_REV */
491 };
492
493 static const uint8_t bytes_per_pixel[6] = {
494 1, /* 3_3_2 */
495 1, /* 2_3_3_REV */
496 2, /* 5_6_5 */
497 2, /* 5_6_5_REV */
498 4, /* 8_8_8_8 */
499 4 /* 8_8_8_8_REV */
500 };
501
502 const uint8_t * bits;
503 const uint32_t * masks;
504 int index;
505 __DRIconfig **configs, **c;
506 __GLcontextModes *modes;
507 unsigned i, j, k, h;
508 unsigned num_modes;
509 unsigned num_accum_bits = (enable_accum) ? 2 : 1;
510
511 switch ( fb_type ) {
512 case GL_UNSIGNED_BYTE_3_3_2:
513 index = 0;
514 break;
515 case GL_UNSIGNED_BYTE_2_3_3_REV:
516 index = 1;
517 break;
518 case GL_UNSIGNED_SHORT_5_6_5:
519 index = 2;
520 break;
521 case GL_UNSIGNED_SHORT_5_6_5_REV:
522 index = 3;
523 break;
524 case GL_UNSIGNED_INT_8_8_8_8:
525 index = 4;
526 break;
527 case GL_UNSIGNED_INT_8_8_8_8_REV:
528 index = 5;
529 break;
530 default:
531 fprintf( stderr, "[%s:%u] Unknown framebuffer type 0x%04x.\n",
532 __FUNCTION__, __LINE__, fb_type );
533 return NULL;
534 }
535
536
537 /* Valid types are GL_UNSIGNED_SHORT_5_6_5 and GL_UNSIGNED_INT_8_8_8_8 and
538 * the _REV versions.
539 *
540 * Valid formats are GL_RGBA, GL_RGB, and GL_BGRA.
541 */
542
543 switch ( fb_format ) {
544 case GL_RGB:
545 masks = masks_table_rgb[ index ];
546 break;
547
548 case GL_RGBA:
549 masks = masks_table_rgba[ index ];
550 break;
551
552 case GL_BGR:
553 masks = masks_table_bgr[ index ];
554 break;
555
556 case GL_BGRA:
557 masks = masks_table_bgra[ index ];
558 break;
559
560 default:
561 fprintf( stderr, "[%s:%u] Unknown framebuffer format 0x%04x.\n",
562 __FUNCTION__, __LINE__, fb_format );
563 return NULL;
564 }
565
566 switch ( bytes_per_pixel[ index ] ) {
567 case 1:
568 bits = bits_table[0];
569 break;
570 case 2:
571 bits = bits_table[1];
572 break;
573 default:
574 bits = ((fb_format == GL_RGB) || (fb_format == GL_BGR))
575 ? bits_table[2]
576 : bits_table[3];
577 break;
578 }
579
580 num_modes = num_depth_stencil_bits * num_db_modes * num_accum_bits * num_msaa_modes;
581 configs = calloc(1, (num_modes + 1) * sizeof *configs);
582 if (configs == NULL)
583 return NULL;
584
585 c = configs;
586 for ( k = 0 ; k < num_depth_stencil_bits ; k++ ) {
587 for ( i = 0 ; i < num_db_modes ; i++ ) {
588 for ( h = 0 ; h < num_msaa_modes; h++ ) {
589 for ( j = 0 ; j < num_accum_bits ; j++ ) {
590 *c = malloc (sizeof **c);
591 modes = &(*c)->modes;
592 c++;
593
594 memset(modes, 0, sizeof *modes);
595 modes->redBits = bits[0];
596 modes->greenBits = bits[1];
597 modes->blueBits = bits[2];
598 modes->alphaBits = bits[3];
599 modes->redMask = masks[0];
600 modes->greenMask = masks[1];
601 modes->blueMask = masks[2];
602 modes->alphaMask = masks[3];
603 modes->rgbBits = modes->redBits + modes->greenBits
604 + modes->blueBits + modes->alphaBits;
605
606 modes->accumRedBits = 16 * j;
607 modes->accumGreenBits = 16 * j;
608 modes->accumBlueBits = 16 * j;
609 modes->accumAlphaBits = (masks[3] != 0) ? 16 * j : 0;
610 modes->visualRating = (j == 0) ? GLX_NONE : GLX_SLOW_CONFIG;
611
612 modes->stencilBits = stencil_bits[k];
613 modes->depthBits = depth_bits[k];
614
615 modes->transparentPixel = GLX_NONE;
616 modes->transparentRed = GLX_DONT_CARE;
617 modes->transparentGreen = GLX_DONT_CARE;
618 modes->transparentBlue = GLX_DONT_CARE;
619 modes->transparentAlpha = GLX_DONT_CARE;
620 modes->transparentIndex = GLX_DONT_CARE;
621 modes->visualType = GLX_DONT_CARE;
622 modes->renderType = GLX_RGBA_BIT;
623 modes->drawableType = GLX_WINDOW_BIT;
624 modes->rgbMode = GL_TRUE;
625
626 if ( db_modes[i] == GLX_NONE ) {
627 modes->doubleBufferMode = GL_FALSE;
628 }
629 else {
630 modes->doubleBufferMode = GL_TRUE;
631 modes->swapMethod = db_modes[i];
632 }
633
634 modes->samples = msaa_samples[h];
635 modes->sampleBuffers = modes->samples ? 1 : 0;
636
637
638 modes->haveAccumBuffer = ((modes->accumRedBits +
639 modes->accumGreenBits +
640 modes->accumBlueBits +
641 modes->accumAlphaBits) > 0);
642 modes->haveDepthBuffer = (modes->depthBits > 0);
643 modes->haveStencilBuffer = (modes->stencilBits > 0);
644
645 modes->bindToTextureRgb = GL_TRUE;
646 modes->bindToTextureRgba = GL_TRUE;
647 modes->bindToMipmapTexture = GL_FALSE;
648 modes->bindToTextureTargets =
649 __DRI_ATTRIB_TEXTURE_1D_BIT |
650 __DRI_ATTRIB_TEXTURE_2D_BIT |
651 __DRI_ATTRIB_TEXTURE_RECTANGLE_BIT;
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(__GLcontextModes, field) }
690
691 static 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
732 /* The struct field doesn't matter here, these are handled by the
733 * switch in driGetConfigAttribIndex. We need them in the array
734 * so the iterator includes them though.*/
735 __ATTRIB(__DRI_ATTRIB_RENDER_TYPE, level),
736 __ATTRIB(__DRI_ATTRIB_CONFIG_CAVEAT, level),
737 __ATTRIB(__DRI_ATTRIB_SWAP_METHOD, level)
738 };
739
740 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
741
742 static int
743 driGetConfigAttribIndex(const __DRIconfig *config,
744 unsigned int index, unsigned int *value)
745 {
746 switch (attribMap[index].attrib) {
747 case __DRI_ATTRIB_RENDER_TYPE:
748 *value = __DRI_ATTRIB_RGBA_BIT;
749 break;
750 case __DRI_ATTRIB_CONFIG_CAVEAT:
751 if (config->modes.visualRating == GLX_NON_CONFORMANT_CONFIG)
752 *value = __DRI_ATTRIB_NON_CONFORMANT_CONFIG;
753 else if (config->modes.visualRating == GLX_SLOW_CONFIG)
754 *value = __DRI_ATTRIB_SLOW_BIT;
755 else
756 *value = 0;
757 break;
758 case __DRI_ATTRIB_SWAP_METHOD:
759 break;
760
761 case __DRI_ATTRIB_FLOAT_MODE:
762 *value = config->modes.floatMode;
763 break;
764
765 default:
766 *value = *(unsigned int *)
767 ((char *) &config->modes + attribMap[index].offset);
768
769 break;
770 }
771
772 return GL_TRUE;
773 }
774
775 int
776 driGetConfigAttrib(const __DRIconfig *config,
777 unsigned int attrib, unsigned int *value)
778 {
779 int i;
780
781 for (i = 0; i < ARRAY_SIZE(attribMap); i++)
782 if (attribMap[i].attrib == attrib)
783 return driGetConfigAttribIndex(config, i, value);
784
785 return GL_FALSE;
786 }
787
788 int
789 driIndexConfigAttrib(const __DRIconfig *config, int index,
790 unsigned int *attrib, unsigned int *value)
791 {
792 if (index >= 0 && index < ARRAY_SIZE(attribMap)) {
793 *attrib = attribMap[index].attrib;
794 return driGetConfigAttribIndex(config, index, value);
795 }
796
797 return GL_FALSE;
798 }