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>
37 #include "main/macros.h"
38 #include "main/mtypes.h"
39 #include "main/cpuinfo.h"
40 #include "main/extensions.h"
45 * Create the \c GL_RENDERER string for DRI drivers.
47 * Almost all DRI drivers use a \c GL_RENDERER string of the form:
49 * "Mesa DRI <chip> <driver date> <AGP speed) <CPU information>"
51 * Using the supplied chip name, driver data, and AGP speed, this function
54 * \param buffer Buffer to hold the \c GL_RENDERER string.
55 * \param hardware_name Name of the hardware.
56 * \param agp_mode AGP mode (speed).
59 * The length of the string stored in \c buffer. This does \b not include
60 * the terminating \c NUL character.
63 driGetRendererString( char * buffer
, const char * hardware_name
,
69 offset
= sprintf( buffer
, "Mesa DRI %s", hardware_name
);
71 /* Append any AGP-specific information.
78 offset
+= sprintf( & buffer
[ offset
], " AGP %ux", agp_mode
);
85 /* Append any CPU-specific information.
87 cpu
= _mesa_get_cpu_string();
89 offset
+= sprintf(buffer
+ offset
, " %s", cpu
);
98 * Creates a set of \c struct gl_config that a driver will expose.
100 * A set of \c struct gl_config will be created based on the supplied
101 * parameters. The number of modes processed will be 2 *
102 * \c num_depth_stencil_bits * \c num_db_modes.
104 * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
105 * \c db_modes, and \c visType into each \c struct gl_config element.
106 * However, the meanings of \c fb_format and \c fb_type require further
107 * explanation. The \c fb_format specifies which color components are in
108 * each pixel and what the default order is. For example, \c GL_RGB specifies
109 * that red, green, blue are available and red is in the "most significant"
110 * position and blue is in the "least significant". The \c fb_type specifies
111 * the bit sizes of each component and the actual ordering. For example, if
112 * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
113 * are the blue value, bits [10:5] are the green value, and bits [4:0] are
116 * One sublte issue is the combination of \c GL_RGB or \c GL_BGR and either
117 * of the \c GL_UNSIGNED_INT_8_8_8_8 modes. The resulting mask values in the
118 * \c struct gl_config structure is \b identical to the \c GL_RGBA or
119 * \c GL_BGRA case, except the \c alphaMask is zero. This means that, as
120 * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
121 * still uses 32-bits.
123 * If in doubt, look at the tables used in the function.
125 * \param ptr_to_modes Pointer to a pointer to a linked list of
126 * \c struct gl_config. Upon completion, a pointer to
127 * the next element to be process will be stored here.
128 * If the function fails and returns \c GL_FALSE, this
129 * value will be unmodified, but some elements in the
130 * linked list may be modified.
131 * \param format Mesa mesa_format enum describing the pixel format
132 * \param depth_bits Array of depth buffer sizes to be exposed.
133 * \param stencil_bits Array of stencil buffer sizes to be exposed.
134 * \param num_depth_stencil_bits Number of entries in both \c depth_bits and
136 * \param db_modes Array of buffer swap modes. If an element has a
137 * value of \c __DRI_ATTRIB_SWAP_NONE, then it
138 * represents a single-buffered mode. Other valid
139 * values are \c __DRI_ATTRIB_SWAP_EXCHANGE,
140 * \c __DRI_ATTRIB_SWAP_COPY, and \c __DRI_ATTRIB_SWAP_UNDEFINED.
141 * They represent the respective GLX values as in
142 * the GLX_OML_swap_method extension spec.
143 * \param num_db_modes Number of entries in \c db_modes.
144 * \param msaa_samples Array of msaa sample count. 0 represents a visual
145 * without a multisample buffer.
146 * \param num_msaa_modes Number of entries in \c msaa_samples.
147 * \param enable_accum Add an accum buffer to the configs
148 * \param color_depth_match Whether the color depth must match the zs depth
149 * This forces 32-bit color to have 24-bit depth, and
150 * 16-bit color to have 16-bit depth.
151 * \param mutable_render_buffer Enable __DRI_ATTRIB_MUTABLE_RENDER_BUFFER,
152 * which translates to
153 * EGL_MUTABLE_RENDER_BUFFER_BIT_KHR.
156 * Pointer to any array of pointers to the \c __DRIconfig structures created
157 * for the specified formats. If there is an error, \c NULL is returned.
158 * Currently the only cause of failure is a bad parameter (i.e., unsupported
162 driCreateConfigs(mesa_format format
,
163 const uint8_t * depth_bits
, const uint8_t * stencil_bits
,
164 unsigned num_depth_stencil_bits
,
165 const GLenum
* db_modes
, unsigned num_db_modes
,
166 const uint8_t * msaa_samples
, unsigned num_msaa_modes
,
167 GLboolean enable_accum
, GLboolean color_depth_match
,
168 GLboolean mutable_render_buffer
)
170 static const uint32_t masks_table
[][4] = {
171 /* MESA_FORMAT_B5G6R5_UNORM */
172 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 },
173 /* MESA_FORMAT_B8G8R8X8_UNORM */
174 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 },
175 /* MESA_FORMAT_B8G8R8A8_UNORM */
176 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 },
177 /* MESA_FORMAT_B10G10R10X2_UNORM */
178 { 0x3FF00000, 0x000FFC00, 0x000003FF, 0x00000000 },
179 /* MESA_FORMAT_B10G10R10A2_UNORM */
180 { 0x3FF00000, 0x000FFC00, 0x000003FF, 0xC0000000 },
181 /* MESA_FORMAT_R8G8B8A8_UNORM */
182 { 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 },
183 /* MESA_FORMAT_R8G8B8X8_UNORM */
184 { 0x000000FF, 0x0000FF00, 0x00FF0000, 0x00000000 },
185 /* MESA_FORMAT_R10G10B10X2_UNORM */
186 { 0x000003FF, 0x000FFC00, 0x3FF00000, 0x00000000 },
187 /* MESA_FORMAT_R10G10B10A2_UNORM */
188 { 0x000003FF, 0x000FFC00, 0x3FF00000, 0xC0000000 },
191 const uint32_t * masks
;
192 __DRIconfig
**configs
, **c
;
193 struct gl_config
*modes
;
196 unsigned num_accum_bits
= (enable_accum
) ? 2 : 1;
204 case MESA_FORMAT_B5G6R5_UNORM
:
205 masks
= masks_table
[0];
207 case MESA_FORMAT_B8G8R8X8_UNORM
:
208 case MESA_FORMAT_B8G8R8X8_SRGB
:
209 masks
= masks_table
[1];
211 case MESA_FORMAT_B8G8R8A8_UNORM
:
212 case MESA_FORMAT_B8G8R8A8_SRGB
:
213 masks
= masks_table
[2];
215 case MESA_FORMAT_R8G8B8A8_UNORM
:
216 case MESA_FORMAT_R8G8B8A8_SRGB
:
217 masks
= masks_table
[5];
219 case MESA_FORMAT_R8G8B8X8_UNORM
:
220 masks
= masks_table
[6];
222 case MESA_FORMAT_B10G10R10X2_UNORM
:
223 masks
= masks_table
[3];
225 case MESA_FORMAT_B10G10R10A2_UNORM
:
226 masks
= masks_table
[4];
228 case MESA_FORMAT_R10G10B10X2_UNORM
:
229 masks
= masks_table
[7];
231 case MESA_FORMAT_R10G10B10A2_UNORM
:
232 masks
= masks_table
[8];
235 fprintf(stderr
, "[%s:%u] Unknown framebuffer type %s (%d).\n",
237 _mesa_get_format_name(format
), format
);
241 red_bits
= _mesa_get_format_bits(format
, GL_RED_BITS
);
242 green_bits
= _mesa_get_format_bits(format
, GL_GREEN_BITS
);
243 blue_bits
= _mesa_get_format_bits(format
, GL_BLUE_BITS
);
244 alpha_bits
= _mesa_get_format_bits(format
, GL_ALPHA_BITS
);
245 is_srgb
= _mesa_get_format_color_encoding(format
) == GL_SRGB
;
247 num_modes
= num_depth_stencil_bits
* num_db_modes
* num_accum_bits
* num_msaa_modes
;
248 configs
= calloc(num_modes
+ 1, sizeof *configs
);
253 for ( k
= 0 ; k
< num_depth_stencil_bits
; k
++ ) {
254 for ( i
= 0 ; i
< num_db_modes
; i
++ ) {
255 for ( h
= 0 ; h
< num_msaa_modes
; h
++ ) {
256 for ( j
= 0 ; j
< num_accum_bits
; j
++ ) {
257 if (color_depth_match
&&
258 (depth_bits
[k
] || stencil_bits
[k
])) {
259 /* Depth can really only be 0, 16, 24, or 32. A 32-bit
260 * color format still matches 24-bit depth, as there
261 * is an implicit 8-bit stencil. So really we just
262 * need to make sure that color/depth are both 16 or
265 if ((depth_bits
[k
] + stencil_bits
[k
] == 16) !=
266 (red_bits
+ green_bits
+ blue_bits
+ alpha_bits
== 16))
270 *c
= malloc (sizeof **c
);
271 modes
= &(*c
)->modes
;
274 memset(modes
, 0, sizeof *modes
);
275 modes
->redBits
= red_bits
;
276 modes
->greenBits
= green_bits
;
277 modes
->blueBits
= blue_bits
;
278 modes
->alphaBits
= alpha_bits
;
279 modes
->redMask
= masks
[0];
280 modes
->greenMask
= masks
[1];
281 modes
->blueMask
= masks
[2];
282 modes
->alphaMask
= masks
[3];
283 modes
->rgbBits
= modes
->redBits
+ modes
->greenBits
284 + modes
->blueBits
+ modes
->alphaBits
;
286 modes
->accumRedBits
= 16 * j
;
287 modes
->accumGreenBits
= 16 * j
;
288 modes
->accumBlueBits
= 16 * j
;
289 modes
->accumAlphaBits
= (masks
[3] != 0) ? 16 * j
: 0;
290 modes
->visualRating
= (j
== 0) ? GLX_NONE
: GLX_SLOW_CONFIG
;
292 modes
->stencilBits
= stencil_bits
[k
];
293 modes
->depthBits
= depth_bits
[k
];
295 modes
->transparentPixel
= GLX_NONE
;
296 modes
->transparentRed
= GLX_DONT_CARE
;
297 modes
->transparentGreen
= GLX_DONT_CARE
;
298 modes
->transparentBlue
= GLX_DONT_CARE
;
299 modes
->transparentAlpha
= GLX_DONT_CARE
;
300 modes
->transparentIndex
= GLX_DONT_CARE
;
301 modes
->rgbMode
= GL_TRUE
;
303 if (db_modes
[i
] == __DRI_ATTRIB_SWAP_NONE
) {
304 modes
->doubleBufferMode
= GL_FALSE
;
305 modes
->swapMethod
= __DRI_ATTRIB_SWAP_UNDEFINED
;
308 modes
->doubleBufferMode
= GL_TRUE
;
309 modes
->swapMethod
= db_modes
[i
];
312 modes
->samples
= msaa_samples
[h
];
313 modes
->sampleBuffers
= modes
->samples
? 1 : 0;
316 modes
->haveAccumBuffer
= ((modes
->accumRedBits
+
317 modes
->accumGreenBits
+
318 modes
->accumBlueBits
+
319 modes
->accumAlphaBits
) > 0);
320 modes
->haveDepthBuffer
= (modes
->depthBits
> 0);
321 modes
->haveStencilBuffer
= (modes
->stencilBits
> 0);
323 modes
->bindToTextureRgb
= GL_TRUE
;
324 modes
->bindToTextureRgba
= GL_TRUE
;
325 modes
->bindToMipmapTexture
= GL_FALSE
;
326 modes
->bindToTextureTargets
=
327 __DRI_ATTRIB_TEXTURE_1D_BIT
|
328 __DRI_ATTRIB_TEXTURE_2D_BIT
|
329 __DRI_ATTRIB_TEXTURE_RECTANGLE_BIT
;
331 modes
->yInverted
= GL_TRUE
;
332 modes
->sRGBCapable
= is_srgb
;
333 modes
->mutableRenderBuffer
= mutable_render_buffer
;
343 __DRIconfig
**driConcatConfigs(__DRIconfig
**a
,
349 if (a
== NULL
|| a
[0] == NULL
)
351 else if (b
== NULL
|| b
[0] == NULL
)
361 all
= malloc((i
+ j
+ 1) * sizeof *all
);
363 for (i
= 0; a
[i
] != NULL
; i
++)
365 for (j
= 0; b
[j
] != NULL
; j
++)
375 #define __ATTRIB(attrib, field) \
376 { attrib, offsetof(struct gl_config, field) }
378 static const struct { unsigned int attrib
, offset
; } attribMap
[] = {
379 __ATTRIB(__DRI_ATTRIB_BUFFER_SIZE
, rgbBits
),
380 __ATTRIB(__DRI_ATTRIB_LEVEL
, level
),
381 __ATTRIB(__DRI_ATTRIB_RED_SIZE
, redBits
),
382 __ATTRIB(__DRI_ATTRIB_GREEN_SIZE
, greenBits
),
383 __ATTRIB(__DRI_ATTRIB_BLUE_SIZE
, blueBits
),
384 __ATTRIB(__DRI_ATTRIB_ALPHA_SIZE
, alphaBits
),
385 __ATTRIB(__DRI_ATTRIB_DEPTH_SIZE
, depthBits
),
386 __ATTRIB(__DRI_ATTRIB_STENCIL_SIZE
, stencilBits
),
387 __ATTRIB(__DRI_ATTRIB_ACCUM_RED_SIZE
, accumRedBits
),
388 __ATTRIB(__DRI_ATTRIB_ACCUM_GREEN_SIZE
, accumGreenBits
),
389 __ATTRIB(__DRI_ATTRIB_ACCUM_BLUE_SIZE
, accumBlueBits
),
390 __ATTRIB(__DRI_ATTRIB_ACCUM_ALPHA_SIZE
, accumAlphaBits
),
391 __ATTRIB(__DRI_ATTRIB_SAMPLE_BUFFERS
, sampleBuffers
),
392 __ATTRIB(__DRI_ATTRIB_SAMPLES
, samples
),
393 __ATTRIB(__DRI_ATTRIB_DOUBLE_BUFFER
, doubleBufferMode
),
394 __ATTRIB(__DRI_ATTRIB_STEREO
, stereoMode
),
395 __ATTRIB(__DRI_ATTRIB_AUX_BUFFERS
, numAuxBuffers
),
396 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_TYPE
, transparentPixel
),
397 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_INDEX_VALUE
, transparentPixel
),
398 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_RED_VALUE
, transparentRed
),
399 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_GREEN_VALUE
, transparentGreen
),
400 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_BLUE_VALUE
, transparentBlue
),
401 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE
, transparentAlpha
),
402 __ATTRIB(__DRI_ATTRIB_RED_MASK
, redMask
),
403 __ATTRIB(__DRI_ATTRIB_GREEN_MASK
, greenMask
),
404 __ATTRIB(__DRI_ATTRIB_BLUE_MASK
, blueMask
),
405 __ATTRIB(__DRI_ATTRIB_ALPHA_MASK
, alphaMask
),
406 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_WIDTH
, maxPbufferWidth
),
407 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_HEIGHT
, maxPbufferHeight
),
408 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_PIXELS
, maxPbufferPixels
),
409 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH
, optimalPbufferWidth
),
410 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT
, optimalPbufferHeight
),
411 __ATTRIB(__DRI_ATTRIB_SWAP_METHOD
, swapMethod
),
412 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGB
, bindToTextureRgb
),
413 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
, bindToTextureRgba
),
414 __ATTRIB(__DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE
, bindToMipmapTexture
),
415 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS
, bindToTextureTargets
),
416 __ATTRIB(__DRI_ATTRIB_YINVERTED
, yInverted
),
417 __ATTRIB(__DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
, sRGBCapable
),
418 __ATTRIB(__DRI_ATTRIB_MUTABLE_RENDER_BUFFER
, mutableRenderBuffer
),
420 /* The struct field doesn't matter here, these are handled by the
421 * switch in driGetConfigAttribIndex. We need them in the array
422 * so the iterator includes them though.*/
423 __ATTRIB(__DRI_ATTRIB_RENDER_TYPE
, level
),
424 __ATTRIB(__DRI_ATTRIB_CONFIG_CAVEAT
, level
),
429 * Return the value of a configuration attribute. The attribute is
430 * indicated by the index.
433 driGetConfigAttribIndex(const __DRIconfig
*config
,
434 unsigned int index
, unsigned int *value
)
436 switch (attribMap
[index
].attrib
) {
437 case __DRI_ATTRIB_RENDER_TYPE
:
438 /* no support for color index mode */
439 *value
= __DRI_ATTRIB_RGBA_BIT
;
441 case __DRI_ATTRIB_CONFIG_CAVEAT
:
442 if (config
->modes
.visualRating
== GLX_NON_CONFORMANT_CONFIG
)
443 *value
= __DRI_ATTRIB_NON_CONFORMANT_CONFIG
;
444 else if (config
->modes
.visualRating
== GLX_SLOW_CONFIG
)
445 *value
= __DRI_ATTRIB_SLOW_BIT
;
450 /* any other int-sized field */
451 *value
= *(unsigned int *)
452 ((char *) &config
->modes
+ attribMap
[index
].offset
);
462 * Get the value of a configuration attribute.
463 * \param attrib the attribute (one of the _DRI_ATTRIB_x tokens)
464 * \param value returns the attribute's value
465 * \return 1 for success, 0 for failure
468 driGetConfigAttrib(const __DRIconfig
*config
,
469 unsigned int attrib
, unsigned int *value
)
473 for (i
= 0; i
< ARRAY_SIZE(attribMap
); i
++)
474 if (attribMap
[i
].attrib
== attrib
)
475 return driGetConfigAttribIndex(config
, i
, value
);
482 * Get a configuration attribute name and value, given an index.
483 * \param index which field of the __DRIconfig to query
484 * \param attrib returns the attribute name (one of the _DRI_ATTRIB_x tokens)
485 * \param value returns the attribute's value
486 * \return 1 for success, 0 for failure
489 driIndexConfigAttrib(const __DRIconfig
*config
, int index
,
490 unsigned int *attrib
, unsigned int *value
)
492 if (index
>= 0 && index
< ARRAY_SIZE(attribMap
)) {
493 *attrib
= attribMap
[index
].attrib
;
494 return driGetConfigAttribIndex(config
, index
, value
);
501 * Implement queries for values that are common across all Mesa drivers
503 * Currently only the following queries are supported by this function:
505 * - \c __DRI2_RENDERER_VERSION
506 * - \c __DRI2_RENDERER_PREFERRED_PROFILE
507 * - \c __DRI2_RENDERER_OPENGL_CORE_PROFILE_VERSION
508 * - \c __DRI2_RENDERER_OPENGL_COMPATIBLITY_PROFILE_VERSION
509 * - \c __DRI2_RENDERER_ES_PROFILE_VERSION
510 * - \c __DRI2_RENDERER_ES2_PROFILE_VERSION
513 * Zero if a recognized value of \c param is supplied, -1 otherwise.
516 driQueryRendererIntegerCommon(__DRIscreen
*psp
, int param
, unsigned int *value
)
519 case __DRI2_RENDERER_VERSION
: {
520 static const char *const ver
= PACKAGE_VERSION
;
524 v
[0] = strtol(ver
, &endptr
, 10);
525 assert(endptr
[0] == '.');
526 if (endptr
[0] != '.')
529 v
[1] = strtol(endptr
+ 1, &endptr
, 10);
530 assert(endptr
[0] == '.');
531 if (endptr
[0] != '.')
534 v
[2] = strtol(endptr
+ 1, &endptr
, 10);
541 case __DRI2_RENDERER_PREFERRED_PROFILE
:
542 value
[0] = (psp
->max_gl_core_version
!= 0)
543 ? (1U << __DRI_API_OPENGL_CORE
) : (1U << __DRI_API_OPENGL
);
545 case __DRI2_RENDERER_OPENGL_CORE_PROFILE_VERSION
:
546 value
[0] = psp
->max_gl_core_version
/ 10;
547 value
[1] = psp
->max_gl_core_version
% 10;
549 case __DRI2_RENDERER_OPENGL_COMPATIBILITY_PROFILE_VERSION
:
550 value
[0] = psp
->max_gl_compat_version
/ 10;
551 value
[1] = psp
->max_gl_compat_version
% 10;
553 case __DRI2_RENDERER_OPENGL_ES_PROFILE_VERSION
:
554 value
[0] = psp
->max_gl_es1_version
/ 10;
555 value
[1] = psp
->max_gl_es1_version
% 10;
557 case __DRI2_RENDERER_OPENGL_ES2_PROFILE_VERSION
:
558 value
[0] = psp
->max_gl_es2_version
/ 10;
559 value
[1] = psp
->max_gl_es2_version
% 10;