Merge branch 'mesa_7_7_branch'
[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 unsigned
41 driParseDebugString( 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 */
83 unsigned
84 driGetRendererString( 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 _mesa_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
143 static 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 */
176 void 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 */
217 void 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 */
253 GLboolean
254 driCheckDriDdxDrmVersions3(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 /* for miniglx we pass in -1 so we can ignore the DDX version */
279 if ( (ddxActual->major != -1) && ((ddxActual->major < ddxExpected->major_min)
280 || (ddxActual->major > ddxExpected->major_max)
281 || (ddxActual->minor < ddxExpected->minor)) ) {
282 fprintf(stderr, format2, driver_name, "DDX",
283 ddxExpected->major_min, ddxExpected->major_max, ddxExpected->minor,
284 ddxActual->major, ddxActual->minor, ddxActual->patch);
285 return GL_FALSE;
286 }
287
288 /* Check that the DRM driver version is compatible */
289 if ( (drmActual->major != drmExpected->major)
290 || (drmActual->minor < drmExpected->minor) ) {
291 fprintf(stderr, format, driver_name, "DRM",
292 drmExpected->major, drmExpected->minor,
293 drmActual->major, drmActual->minor, drmActual->patch);
294 return GL_FALSE;
295 }
296
297 return GL_TRUE;
298 }
299
300 GLboolean
301 driCheckDriDdxDrmVersions2(const char * driver_name,
302 const __DRIversion * driActual,
303 const __DRIversion * driExpected,
304 const __DRIversion * ddxActual,
305 const __DRIversion * ddxExpected,
306 const __DRIversion * drmActual,
307 const __DRIversion * drmExpected)
308 {
309 __DRIutilversion2 ddx_expected;
310 ddx_expected.major_min = ddxExpected->major;
311 ddx_expected.major_max = ddxExpected->major;
312 ddx_expected.minor = ddxExpected->minor;
313 ddx_expected.patch = ddxExpected->patch;
314 return driCheckDriDdxDrmVersions3(driver_name, driActual,
315 driExpected, ddxActual, & ddx_expected,
316 drmActual, drmExpected);
317 }
318
319 GLboolean driClipRectToFramebuffer( const GLframebuffer *buffer,
320 GLint *x, GLint *y,
321 GLsizei *width, GLsizei *height )
322 {
323 /* left clipping */
324 if (*x < buffer->_Xmin) {
325 *width -= (buffer->_Xmin - *x);
326 *x = buffer->_Xmin;
327 }
328
329 /* right clipping */
330 if (*x + *width > buffer->_Xmax)
331 *width -= (*x + *width - buffer->_Xmax - 1);
332
333 if (*width <= 0)
334 return GL_FALSE;
335
336 /* bottom clipping */
337 if (*y < buffer->_Ymin) {
338 *height -= (buffer->_Ymin - *y);
339 *y = buffer->_Ymin;
340 }
341
342 /* top clipping */
343 if (*y + *height > buffer->_Ymax)
344 *height -= (*y + *height - buffer->_Ymax - 1);
345
346 if (*height <= 0)
347 return GL_FALSE;
348
349 return GL_TRUE;
350 }
351
352 /**
353 * Creates a set of \c __GLcontextModes that a driver will expose.
354 *
355 * A set of \c __GLcontextModes will be created based on the supplied
356 * parameters. The number of modes processed will be 2 *
357 * \c num_depth_stencil_bits * \c num_db_modes.
358 *
359 * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
360 * \c db_modes, and \c visType into each \c __GLcontextModes element.
361 * However, the meanings of \c fb_format and \c fb_type require further
362 * explanation. The \c fb_format specifies which color components are in
363 * each pixel and what the default order is. For example, \c GL_RGB specifies
364 * that red, green, blue are available and red is in the "most significant"
365 * position and blue is in the "least significant". The \c fb_type specifies
366 * the bit sizes of each component and the actual ordering. For example, if
367 * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
368 * are the blue value, bits [10:5] are the green value, and bits [4:0] are
369 * the red value.
370 *
371 * One sublte issue is the combination of \c GL_RGB or \c GL_BGR and either
372 * of the \c GL_UNSIGNED_INT_8_8_8_8 modes. The resulting mask values in the
373 * \c __GLcontextModes structure is \b identical to the \c GL_RGBA or
374 * \c GL_BGRA case, except the \c alphaMask is zero. This means that, as
375 * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
376 * still uses 32-bits.
377 *
378 * If in doubt, look at the tables used in the function.
379 *
380 * \param ptr_to_modes Pointer to a pointer to a linked list of
381 * \c __GLcontextModes. Upon completion, a pointer to
382 * the next element to be process will be stored here.
383 * If the function fails and returns \c GL_FALSE, this
384 * value will be unmodified, but some elements in the
385 * linked list may be modified.
386 * \param fb_format Format of the framebuffer. Currently only \c GL_RGB,
387 * \c GL_RGBA, \c GL_BGR, and \c GL_BGRA are supported.
388 * \param fb_type Type of the pixels in the framebuffer. Currently only
389 * \c GL_UNSIGNED_SHORT_5_6_5,
390 * \c GL_UNSIGNED_SHORT_5_6_5_REV,
391 * \c GL_UNSIGNED_INT_8_8_8_8, and
392 * \c GL_UNSIGNED_INT_8_8_8_8_REV are supported.
393 * \param depth_bits Array of depth buffer sizes to be exposed.
394 * \param stencil_bits Array of stencil buffer sizes to be exposed.
395 * \param num_depth_stencil_bits Number of entries in both \c depth_bits and
396 * \c stencil_bits.
397 * \param db_modes Array of buffer swap modes. If an element has a
398 * value of \c GLX_NONE, then it represents a
399 * single-buffered mode. Other valid values are
400 * \c GLX_SWAP_EXCHANGE_OML, \c GLX_SWAP_COPY_OML, and
401 * \c GLX_SWAP_UNDEFINED_OML. See the
402 * GLX_OML_swap_method extension spec for more details.
403 * \param num_db_modes Number of entries in \c db_modes.
404 * \param msaa_samples Array of msaa sample count. 0 represents a visual
405 * without a multisample buffer.
406 * \param num_msaa_modes Number of entries in \c msaa_samples.
407 * \param visType GLX visual type. Usually either \c GLX_TRUE_COLOR or
408 * \c GLX_DIRECT_COLOR.
409 *
410 * \returns
411 * \c GL_TRUE on success or \c GL_FALSE on failure. Currently the only
412 * cause of failure is a bad parameter (i.e., unsupported \c fb_format or
413 * \c fb_type).
414 *
415 * \todo
416 * There is currently no way to support packed RGB modes (i.e., modes with
417 * exactly 3 bytes per pixel) or floating-point modes. This could probably
418 * be done by creating some new, private enums with clever names likes
419 * \c GL_UNSIGNED_3BYTE_8_8_8, \c GL_4FLOAT_32_32_32_32,
420 * \c GL_4HALF_16_16_16_16, etc. We can cross that bridge when we come to it.
421 */
422 __DRIconfig **
423 driCreateConfigs(GLenum fb_format, GLenum fb_type,
424 const uint8_t * depth_bits, const uint8_t * stencil_bits,
425 unsigned num_depth_stencil_bits,
426 const GLenum * db_modes, unsigned num_db_modes,
427 const uint8_t * msaa_samples, unsigned num_msaa_modes)
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 = 2;
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 = _mesa_calloc((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 = _mesa_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 = modes->rgbMode ?
629 __DRI_ATTRIB_TEXTURE_1D_BIT |
630 __DRI_ATTRIB_TEXTURE_2D_BIT |
631 __DRI_ATTRIB_TEXTURE_RECTANGLE_BIT :
632 0;
633 }
634 }
635 }
636 }
637 *c = NULL;
638
639 return configs;
640 }
641
642 __DRIconfig **driConcatConfigs(__DRIconfig **a,
643 __DRIconfig **b)
644 {
645 __DRIconfig **all;
646 int i, j, index;
647
648 i = 0;
649 while (a[i] != NULL)
650 i++;
651 j = 0;
652 while (b[j] != NULL)
653 j++;
654
655 all = _mesa_malloc((i + j + 1) * sizeof *all);
656 index = 0;
657 for (i = 0; a[i] != NULL; i++)
658 all[index++] = a[i];
659 for (j = 0; b[j] != NULL; j++)
660 all[index++] = b[j];
661 all[index++] = NULL;
662
663 _mesa_free(a);
664 _mesa_free(b);
665
666 return all;
667 }
668
669 #define __ATTRIB(attrib, field) \
670 { attrib, offsetof(__GLcontextModes, field) }
671
672 static const struct { unsigned int attrib, offset; } attribMap[] = {
673 __ATTRIB(__DRI_ATTRIB_BUFFER_SIZE, rgbBits),
674 __ATTRIB(__DRI_ATTRIB_LEVEL, level),
675 __ATTRIB(__DRI_ATTRIB_RED_SIZE, redBits),
676 __ATTRIB(__DRI_ATTRIB_GREEN_SIZE, greenBits),
677 __ATTRIB(__DRI_ATTRIB_BLUE_SIZE, blueBits),
678 __ATTRIB(__DRI_ATTRIB_ALPHA_SIZE, alphaBits),
679 __ATTRIB(__DRI_ATTRIB_DEPTH_SIZE, depthBits),
680 __ATTRIB(__DRI_ATTRIB_STENCIL_SIZE, stencilBits),
681 __ATTRIB(__DRI_ATTRIB_ACCUM_RED_SIZE, accumRedBits),
682 __ATTRIB(__DRI_ATTRIB_ACCUM_GREEN_SIZE, accumGreenBits),
683 __ATTRIB(__DRI_ATTRIB_ACCUM_BLUE_SIZE, accumBlueBits),
684 __ATTRIB(__DRI_ATTRIB_ACCUM_ALPHA_SIZE, accumAlphaBits),
685 __ATTRIB(__DRI_ATTRIB_SAMPLE_BUFFERS, sampleBuffers),
686 __ATTRIB(__DRI_ATTRIB_SAMPLES, samples),
687 __ATTRIB(__DRI_ATTRIB_DOUBLE_BUFFER, doubleBufferMode),
688 __ATTRIB(__DRI_ATTRIB_STEREO, stereoMode),
689 __ATTRIB(__DRI_ATTRIB_AUX_BUFFERS, numAuxBuffers),
690 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_TYPE, transparentPixel),
691 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_INDEX_VALUE, transparentPixel),
692 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_RED_VALUE, transparentRed),
693 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_GREEN_VALUE, transparentGreen),
694 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_BLUE_VALUE, transparentBlue),
695 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE, transparentAlpha),
696 __ATTRIB(__DRI_ATTRIB_FLOAT_MODE, floatMode),
697 __ATTRIB(__DRI_ATTRIB_RED_MASK, redMask),
698 __ATTRIB(__DRI_ATTRIB_GREEN_MASK, greenMask),
699 __ATTRIB(__DRI_ATTRIB_BLUE_MASK, blueMask),
700 __ATTRIB(__DRI_ATTRIB_ALPHA_MASK, alphaMask),
701 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_WIDTH, maxPbufferWidth),
702 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_HEIGHT, maxPbufferHeight),
703 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_PIXELS, maxPbufferPixels),
704 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH, optimalPbufferWidth),
705 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT, optimalPbufferHeight),
706 __ATTRIB(__DRI_ATTRIB_SWAP_METHOD, swapMethod),
707 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGB, bindToTextureRgb),
708 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGBA, bindToTextureRgba),
709 __ATTRIB(__DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE, bindToMipmapTexture),
710 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS, bindToTextureTargets),
711 __ATTRIB(__DRI_ATTRIB_YINVERTED, yInverted),
712
713 /* The struct field doesn't matter here, these are handled by the
714 * switch in driGetConfigAttribIndex. We need them in the array
715 * so the iterator includes them though.*/
716 __ATTRIB(__DRI_ATTRIB_RENDER_TYPE, level),
717 __ATTRIB(__DRI_ATTRIB_CONFIG_CAVEAT, level),
718 __ATTRIB(__DRI_ATTRIB_SWAP_METHOD, level)
719 };
720
721 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
722
723 static int
724 driGetConfigAttribIndex(const __DRIconfig *config,
725 unsigned int index, unsigned int *value)
726 {
727 switch (attribMap[index].attrib) {
728 case __DRI_ATTRIB_RENDER_TYPE:
729 if (config->modes.rgbMode)
730 *value = __DRI_ATTRIB_RGBA_BIT;
731 else
732 *value = __DRI_ATTRIB_COLOR_INDEX_BIT;
733 break;
734 case __DRI_ATTRIB_CONFIG_CAVEAT:
735 if (config->modes.visualRating == GLX_NON_CONFORMANT_CONFIG)
736 *value = __DRI_ATTRIB_NON_CONFORMANT_CONFIG;
737 else if (config->modes.visualRating == GLX_SLOW_CONFIG)
738 *value = __DRI_ATTRIB_SLOW_BIT;
739 else
740 *value = 0;
741 break;
742 case __DRI_ATTRIB_SWAP_METHOD:
743 break;
744
745 case __DRI_ATTRIB_FLOAT_MODE:
746 *value = config->modes.floatMode;
747 break;
748
749 default:
750 *value = *(unsigned int *)
751 ((char *) &config->modes + attribMap[index].offset);
752
753 break;
754 }
755
756 return GL_TRUE;
757 }
758
759 int
760 driGetConfigAttrib(const __DRIconfig *config,
761 unsigned int attrib, unsigned int *value)
762 {
763 int i;
764
765 for (i = 0; i < ARRAY_SIZE(attribMap); i++)
766 if (attribMap[i].attrib == attrib)
767 return driGetConfigAttribIndex(config, i, value);
768
769 return GL_FALSE;
770 }
771
772 int
773 driIndexConfigAttrib(const __DRIconfig *config, int index,
774 unsigned int *attrib, unsigned int *value)
775 {
776 if (index >= 0 && index < ARRAY_SIZE(attribMap)) {
777 *attrib = attribMap[index].attrib;
778 return driGetConfigAttribIndex(config, index, value);
779 }
780
781 return GL_FALSE;
782 }