2 * (C) Copyright IBM Corporation 2002, 2004
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:
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
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.
27 * Utility functions for DRI drivers.
29 * \author Ian Romanick <idr@us.ibm.com>
34 #include "main/mtypes.h"
35 #include "main/cpuinfo.h"
36 #include "main/extensions.h"
41 * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
44 * Is called from the drivers.
46 * \param f \c printf like format string.
49 __driUtilMessage(const char *f
, ...)
53 if (getenv("LIBGL_DEBUG")) {
54 fprintf(stderr
, "libGL: ");
56 vfprintf(stderr
, f
, args
);
58 fprintf(stderr
, "\n");
64 driParseDebugString( const char * debug
,
65 const struct dri_debug_control
* control
)
71 if ( debug
!= NULL
) {
72 while( control
->string
!= NULL
) {
73 if ( !strcmp( debug
, "all" ) ||
74 strstr( debug
, control
->string
) != NULL
) {
75 flag
|= control
->flag
;
88 * Create the \c GL_RENDERER string for DRI drivers.
90 * Almost all DRI drivers use a \c GL_RENDERER string of the form:
92 * "Mesa DRI <chip> <driver date> <AGP speed) <CPU information>"
94 * Using the supplied chip name, driver data, and AGP speed, this function
97 * \param buffer Buffer to hold the \c GL_RENDERER string.
98 * \param hardware_name Name of the hardware.
99 * \param agp_mode AGP mode (speed).
102 * The length of the string stored in \c buffer. This does \b not include
103 * the terminating \c NUL character.
106 driGetRendererString( char * buffer
, const char * hardware_name
,
112 offset
= sprintf( buffer
, "Mesa DRI %s", hardware_name
);
114 /* Append any AGP-specific information.
116 switch ( agp_mode
) {
121 offset
+= sprintf( & buffer
[ offset
], " AGP %ux", agp_mode
);
128 /* Append any CPU-specific information.
130 cpu
= _mesa_get_cpu_string();
132 offset
+= sprintf(buffer
+ offset
, " %s", cpu
);
141 * Creates a set of \c struct gl_config that a driver will expose.
143 * A set of \c struct gl_config will be created based on the supplied
144 * parameters. The number of modes processed will be 2 *
145 * \c num_depth_stencil_bits * \c num_db_modes.
147 * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
148 * \c db_modes, and \c visType into each \c struct gl_config element.
149 * However, the meanings of \c fb_format and \c fb_type require further
150 * explanation. The \c fb_format specifies which color components are in
151 * each pixel and what the default order is. For example, \c GL_RGB specifies
152 * that red, green, blue are available and red is in the "most significant"
153 * position and blue is in the "least significant". The \c fb_type specifies
154 * the bit sizes of each component and the actual ordering. For example, if
155 * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
156 * are the blue value, bits [10:5] are the green value, and bits [4:0] are
159 * One sublte issue is the combination of \c GL_RGB or \c GL_BGR and either
160 * of the \c GL_UNSIGNED_INT_8_8_8_8 modes. The resulting mask values in the
161 * \c struct gl_config structure is \b identical to the \c GL_RGBA or
162 * \c GL_BGRA case, except the \c alphaMask is zero. This means that, as
163 * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
164 * still uses 32-bits.
166 * If in doubt, look at the tables used in the function.
168 * \param ptr_to_modes Pointer to a pointer to a linked list of
169 * \c struct gl_config. Upon completion, a pointer to
170 * the next element to be process will be stored here.
171 * If the function fails and returns \c GL_FALSE, this
172 * value will be unmodified, but some elements in the
173 * linked list may be modified.
174 * \param fb_format Format of the framebuffer. Currently only \c GL_RGB,
175 * \c GL_RGBA, \c GL_BGR, and \c GL_BGRA are supported.
176 * \param fb_type Type of the pixels in the framebuffer. Currently only
177 * \c GL_UNSIGNED_SHORT_5_6_5,
178 * \c GL_UNSIGNED_SHORT_5_6_5_REV,
179 * \c GL_UNSIGNED_INT_8_8_8_8, and
180 * \c GL_UNSIGNED_INT_8_8_8_8_REV are supported.
181 * \param depth_bits Array of depth buffer sizes to be exposed.
182 * \param stencil_bits Array of stencil buffer sizes to be exposed.
183 * \param num_depth_stencil_bits Number of entries in both \c depth_bits and
185 * \param db_modes Array of buffer swap modes. If an element has a
186 * value of \c GLX_NONE, then it represents a
187 * single-buffered mode. Other valid values are
188 * \c GLX_SWAP_EXCHANGE_OML, \c GLX_SWAP_COPY_OML, and
189 * \c GLX_SWAP_UNDEFINED_OML. See the
190 * GLX_OML_swap_method extension spec for more details.
191 * \param num_db_modes Number of entries in \c db_modes.
192 * \param msaa_samples Array of msaa sample count. 0 represents a visual
193 * without a multisample buffer.
194 * \param num_msaa_modes Number of entries in \c msaa_samples.
195 * \param visType GLX visual type. Usually either \c GLX_TRUE_COLOR or
196 * \c GLX_DIRECT_COLOR.
199 * \c GL_TRUE on success or \c GL_FALSE on failure. Currently the only
200 * cause of failure is a bad parameter (i.e., unsupported \c fb_format or
204 * There is currently no way to support packed RGB modes (i.e., modes with
205 * exactly 3 bytes per pixel) or floating-point modes. This could probably
206 * be done by creating some new, private enums with clever names likes
207 * \c GL_UNSIGNED_3BYTE_8_8_8, \c GL_4FLOAT_32_32_32_32,
208 * \c GL_4HALF_16_16_16_16, etc. We can cross that bridge when we come to it.
211 driCreateConfigs(GLenum fb_format
, GLenum fb_type
,
212 const uint8_t * depth_bits
, const uint8_t * stencil_bits
,
213 unsigned num_depth_stencil_bits
,
214 const GLenum
* db_modes
, unsigned num_db_modes
,
215 const uint8_t * msaa_samples
, unsigned num_msaa_modes
,
216 GLboolean enable_accum
)
218 static const uint8_t bits_table
[4][4] = {
220 { 3, 3, 2, 0 }, /* Any GL_UNSIGNED_BYTE_3_3_2 */
221 { 5, 6, 5, 0 }, /* Any GL_UNSIGNED_SHORT_5_6_5 */
222 { 8, 8, 8, 0 }, /* Any RGB with any GL_UNSIGNED_INT_8_8_8_8 */
223 { 8, 8, 8, 8 } /* Any RGBA with any GL_UNSIGNED_INT_8_8_8_8 */
226 static const uint32_t masks_table_rgb
[6][4] = {
227 { 0x000000E0, 0x0000001C, 0x00000003, 0x00000000 }, /* 3_3_2 */
228 { 0x00000007, 0x00000038, 0x000000C0, 0x00000000 }, /* 2_3_3_REV */
229 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5 */
230 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5_REV */
231 { 0xFF000000, 0x00FF0000, 0x0000FF00, 0x00000000 }, /* 8_8_8_8 */
232 { 0x000000FF, 0x0000FF00, 0x00FF0000, 0x00000000 } /* 8_8_8_8_REV */
235 static const uint32_t masks_table_rgba
[6][4] = {
236 { 0x000000E0, 0x0000001C, 0x00000003, 0x00000000 }, /* 3_3_2 */
237 { 0x00000007, 0x00000038, 0x000000C0, 0x00000000 }, /* 2_3_3_REV */
238 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5 */
239 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5_REV */
240 { 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF }, /* 8_8_8_8 */
241 { 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 }, /* 8_8_8_8_REV */
244 static const uint32_t masks_table_bgr
[6][4] = {
245 { 0x00000007, 0x00000038, 0x000000C0, 0x00000000 }, /* 3_3_2 */
246 { 0x000000E0, 0x0000001C, 0x00000003, 0x00000000 }, /* 2_3_3_REV */
247 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5 */
248 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5_REV */
249 { 0x0000FF00, 0x00FF0000, 0xFF000000, 0x00000000 }, /* 8_8_8_8 */
250 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 }, /* 8_8_8_8_REV */
253 static const uint32_t masks_table_bgra
[6][4] = {
254 { 0x00000007, 0x00000038, 0x000000C0, 0x00000000 }, /* 3_3_2 */
255 { 0x000000E0, 0x0000001C, 0x00000003, 0x00000000 }, /* 2_3_3_REV */
256 { 0x0000001F, 0x000007E0, 0x0000F800, 0x00000000 }, /* 5_6_5 */
257 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 }, /* 5_6_5_REV */
258 { 0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF }, /* 8_8_8_8 */
259 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 }, /* 8_8_8_8_REV */
262 static const uint8_t bytes_per_pixel
[6] = {
271 const uint8_t * bits
;
272 const uint32_t * masks
;
274 __DRIconfig
**configs
, **c
;
275 struct gl_config
*modes
;
278 unsigned num_accum_bits
= (enable_accum
) ? 2 : 1;
281 case GL_UNSIGNED_BYTE_3_3_2
:
284 case GL_UNSIGNED_BYTE_2_3_3_REV
:
287 case GL_UNSIGNED_SHORT_5_6_5
:
290 case GL_UNSIGNED_SHORT_5_6_5_REV
:
293 case GL_UNSIGNED_INT_8_8_8_8
:
296 case GL_UNSIGNED_INT_8_8_8_8_REV
:
300 fprintf( stderr
, "[%s:%u] Unknown framebuffer type 0x%04x.\n",
301 __FUNCTION__
, __LINE__
, fb_type
);
306 /* Valid types are GL_UNSIGNED_SHORT_5_6_5 and GL_UNSIGNED_INT_8_8_8_8 and
309 * Valid formats are GL_RGBA, GL_RGB, and GL_BGRA.
312 switch ( fb_format
) {
314 masks
= masks_table_rgb
[ index
];
318 masks
= masks_table_rgba
[ index
];
322 masks
= masks_table_bgr
[ index
];
326 masks
= masks_table_bgra
[ index
];
330 fprintf( stderr
, "[%s:%u] Unknown framebuffer format 0x%04x.\n",
331 __FUNCTION__
, __LINE__
, fb_format
);
335 switch ( bytes_per_pixel
[ index
] ) {
337 bits
= bits_table
[0];
340 bits
= bits_table
[1];
343 bits
= ((fb_format
== GL_RGB
) || (fb_format
== GL_BGR
))
349 num_modes
= num_depth_stencil_bits
* num_db_modes
* num_accum_bits
* num_msaa_modes
;
350 configs
= calloc(1, (num_modes
+ 1) * sizeof *configs
);
355 for ( k
= 0 ; k
< num_depth_stencil_bits
; k
++ ) {
356 for ( i
= 0 ; i
< num_db_modes
; i
++ ) {
357 for ( h
= 0 ; h
< num_msaa_modes
; h
++ ) {
358 for ( j
= 0 ; j
< num_accum_bits
; j
++ ) {
359 *c
= malloc (sizeof **c
);
360 modes
= &(*c
)->modes
;
363 memset(modes
, 0, sizeof *modes
);
364 modes
->redBits
= bits
[0];
365 modes
->greenBits
= bits
[1];
366 modes
->blueBits
= bits
[2];
367 modes
->alphaBits
= bits
[3];
368 modes
->redMask
= masks
[0];
369 modes
->greenMask
= masks
[1];
370 modes
->blueMask
= masks
[2];
371 modes
->alphaMask
= masks
[3];
372 modes
->rgbBits
= modes
->redBits
+ modes
->greenBits
373 + modes
->blueBits
+ modes
->alphaBits
;
375 modes
->accumRedBits
= 16 * j
;
376 modes
->accumGreenBits
= 16 * j
;
377 modes
->accumBlueBits
= 16 * j
;
378 modes
->accumAlphaBits
= (masks
[3] != 0) ? 16 * j
: 0;
379 modes
->visualRating
= (j
== 0) ? GLX_NONE
: GLX_SLOW_CONFIG
;
381 modes
->stencilBits
= stencil_bits
[k
];
382 modes
->depthBits
= depth_bits
[k
];
384 modes
->transparentPixel
= GLX_NONE
;
385 modes
->transparentRed
= GLX_DONT_CARE
;
386 modes
->transparentGreen
= GLX_DONT_CARE
;
387 modes
->transparentBlue
= GLX_DONT_CARE
;
388 modes
->transparentAlpha
= GLX_DONT_CARE
;
389 modes
->transparentIndex
= GLX_DONT_CARE
;
390 modes
->rgbMode
= GL_TRUE
;
392 if ( db_modes
[i
] == GLX_NONE
) {
393 modes
->doubleBufferMode
= GL_FALSE
;
396 modes
->doubleBufferMode
= GL_TRUE
;
397 modes
->swapMethod
= db_modes
[i
];
400 modes
->samples
= msaa_samples
[h
];
401 modes
->sampleBuffers
= modes
->samples
? 1 : 0;
404 modes
->haveAccumBuffer
= ((modes
->accumRedBits
+
405 modes
->accumGreenBits
+
406 modes
->accumBlueBits
+
407 modes
->accumAlphaBits
) > 0);
408 modes
->haveDepthBuffer
= (modes
->depthBits
> 0);
409 modes
->haveStencilBuffer
= (modes
->stencilBits
> 0);
411 modes
->bindToTextureRgb
= GL_TRUE
;
412 modes
->bindToTextureRgba
= GL_TRUE
;
413 modes
->bindToMipmapTexture
= GL_FALSE
;
414 modes
->bindToTextureTargets
=
415 __DRI_ATTRIB_TEXTURE_1D_BIT
|
416 __DRI_ATTRIB_TEXTURE_2D_BIT
|
417 __DRI_ATTRIB_TEXTURE_RECTANGLE_BIT
;
419 modes
->sRGBCapable
= GL_FALSE
;
429 __DRIconfig
**driConcatConfigs(__DRIconfig
**a
,
442 all
= malloc((i
+ j
+ 1) * sizeof *all
);
444 for (i
= 0; a
[i
] != NULL
; i
++)
446 for (j
= 0; b
[j
] != NULL
; j
++)
456 #define __ATTRIB(attrib, field) \
457 { attrib, offsetof(struct gl_config, field) }
459 static const struct { unsigned int attrib
, offset
; } attribMap
[] = {
460 __ATTRIB(__DRI_ATTRIB_BUFFER_SIZE
, rgbBits
),
461 __ATTRIB(__DRI_ATTRIB_LEVEL
, level
),
462 __ATTRIB(__DRI_ATTRIB_RED_SIZE
, redBits
),
463 __ATTRIB(__DRI_ATTRIB_GREEN_SIZE
, greenBits
),
464 __ATTRIB(__DRI_ATTRIB_BLUE_SIZE
, blueBits
),
465 __ATTRIB(__DRI_ATTRIB_ALPHA_SIZE
, alphaBits
),
466 __ATTRIB(__DRI_ATTRIB_DEPTH_SIZE
, depthBits
),
467 __ATTRIB(__DRI_ATTRIB_STENCIL_SIZE
, stencilBits
),
468 __ATTRIB(__DRI_ATTRIB_ACCUM_RED_SIZE
, accumRedBits
),
469 __ATTRIB(__DRI_ATTRIB_ACCUM_GREEN_SIZE
, accumGreenBits
),
470 __ATTRIB(__DRI_ATTRIB_ACCUM_BLUE_SIZE
, accumBlueBits
),
471 __ATTRIB(__DRI_ATTRIB_ACCUM_ALPHA_SIZE
, accumAlphaBits
),
472 __ATTRIB(__DRI_ATTRIB_SAMPLE_BUFFERS
, sampleBuffers
),
473 __ATTRIB(__DRI_ATTRIB_SAMPLES
, samples
),
474 __ATTRIB(__DRI_ATTRIB_DOUBLE_BUFFER
, doubleBufferMode
),
475 __ATTRIB(__DRI_ATTRIB_STEREO
, stereoMode
),
476 __ATTRIB(__DRI_ATTRIB_AUX_BUFFERS
, numAuxBuffers
),
477 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_TYPE
, transparentPixel
),
478 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_INDEX_VALUE
, transparentPixel
),
479 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_RED_VALUE
, transparentRed
),
480 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_GREEN_VALUE
, transparentGreen
),
481 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_BLUE_VALUE
, transparentBlue
),
482 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE
, transparentAlpha
),
483 __ATTRIB(__DRI_ATTRIB_FLOAT_MODE
, floatMode
),
484 __ATTRIB(__DRI_ATTRIB_RED_MASK
, redMask
),
485 __ATTRIB(__DRI_ATTRIB_GREEN_MASK
, greenMask
),
486 __ATTRIB(__DRI_ATTRIB_BLUE_MASK
, blueMask
),
487 __ATTRIB(__DRI_ATTRIB_ALPHA_MASK
, alphaMask
),
488 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_WIDTH
, maxPbufferWidth
),
489 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_HEIGHT
, maxPbufferHeight
),
490 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_PIXELS
, maxPbufferPixels
),
491 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH
, optimalPbufferWidth
),
492 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT
, optimalPbufferHeight
),
493 __ATTRIB(__DRI_ATTRIB_SWAP_METHOD
, swapMethod
),
494 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGB
, bindToTextureRgb
),
495 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
, bindToTextureRgba
),
496 __ATTRIB(__DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE
, bindToMipmapTexture
),
497 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS
, bindToTextureTargets
),
498 __ATTRIB(__DRI_ATTRIB_YINVERTED
, yInverted
),
499 __ATTRIB(__DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
, sRGBCapable
),
501 /* The struct field doesn't matter here, these are handled by the
502 * switch in driGetConfigAttribIndex. We need them in the array
503 * so the iterator includes them though.*/
504 __ATTRIB(__DRI_ATTRIB_RENDER_TYPE
, level
),
505 __ATTRIB(__DRI_ATTRIB_CONFIG_CAVEAT
, level
),
506 __ATTRIB(__DRI_ATTRIB_SWAP_METHOD
, level
)
509 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
513 * Return the value of a configuration attribute. The attribute is
514 * indicated by the index.
517 driGetConfigAttribIndex(const __DRIconfig
*config
,
518 unsigned int index
, unsigned int *value
)
520 switch (attribMap
[index
].attrib
) {
521 case __DRI_ATTRIB_RENDER_TYPE
:
522 /* no support for color index mode */
523 *value
= __DRI_ATTRIB_RGBA_BIT
;
525 case __DRI_ATTRIB_CONFIG_CAVEAT
:
526 if (config
->modes
.visualRating
== GLX_NON_CONFORMANT_CONFIG
)
527 *value
= __DRI_ATTRIB_NON_CONFORMANT_CONFIG
;
528 else if (config
->modes
.visualRating
== GLX_SLOW_CONFIG
)
529 *value
= __DRI_ATTRIB_SLOW_BIT
;
533 case __DRI_ATTRIB_SWAP_METHOD
:
534 /* XXX no return value??? */
537 case __DRI_ATTRIB_FLOAT_MODE
:
538 /* this field is not int-sized */
539 *value
= config
->modes
.floatMode
;
543 /* any other int-sized field */
544 *value
= *(unsigned int *)
545 ((char *) &config
->modes
+ attribMap
[index
].offset
);
555 * Get the value of a configuration attribute.
556 * \param attrib the attribute (one of the _DRI_ATTRIB_x tokens)
557 * \param value returns the attribute's value
558 * \return 1 for success, 0 for failure
561 driGetConfigAttrib(const __DRIconfig
*config
,
562 unsigned int attrib
, unsigned int *value
)
566 for (i
= 0; i
< ARRAY_SIZE(attribMap
); i
++)
567 if (attribMap
[i
].attrib
== attrib
)
568 return driGetConfigAttribIndex(config
, i
, value
);
575 * Get a configuration attribute name and value, given an index.
576 * \param index which field of the __DRIconfig to query
577 * \param attrib returns the attribute name (one of the _DRI_ATTRIB_x tokens)
578 * \param value returns the attribute's value
579 * \return 1 for success, 0 for failure
582 driIndexConfigAttrib(const __DRIconfig
*config
, int index
,
583 unsigned int *attrib
, unsigned int *value
)
585 if (index
>= 0 && index
< ARRAY_SIZE(attribMap
)) {
586 *attrib
= attribMap
[index
].attrib
;
587 return driGetConfigAttribIndex(config
, index
, value
);