dri: Add fp16 formats
[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 <stdio.h>
34 #include <stdlib.h>
35 #include <stdbool.h>
36 #include <stdint.h>
37 #include "main/macros.h"
38 #include "main/mtypes.h"
39 #include "main/cpuinfo.h"
40 #include "main/extensions.h"
41 #include "utils.h"
42 #include "dri_util.h"
43
44 /* WARNING: HACK: Local defines to avoid pulling glx.h.
45 *
46 * Any parts of this file that use the following defines are either partial or
47 * entirely broken wrt EGL.
48 *
49 * For example any getConfigAttrib() or indexConfigAttrib() query from EGL for
50 * SLOW or NON_CONFORMANT_CONFIG will not work as expected since the EGL tokens
51 * are different from the GLX ones.
52 */
53 #define GLX_NONE 0x8000
54 #define GLX_SLOW_CONFIG 0x8001
55 #define GLX_NON_CONFORMANT_CONFIG 0x800D
56 #define GLX_DONT_CARE 0xFFFFFFFF
57
58 /**
59 * Create the \c GL_RENDERER string for DRI drivers.
60 *
61 * Almost all DRI drivers use a \c GL_RENDERER string of the form:
62 *
63 * "Mesa DRI <chip> <driver date> <AGP speed) <CPU information>"
64 *
65 * Using the supplied chip name, driver data, and AGP speed, this function
66 * creates the string.
67 *
68 * \param buffer Buffer to hold the \c GL_RENDERER string.
69 * \param hardware_name Name of the hardware.
70 * \param agp_mode AGP mode (speed).
71 *
72 * \returns
73 * The length of the string stored in \c buffer. This does \b not include
74 * the terminating \c NUL character.
75 */
76 unsigned
77 driGetRendererString( char * buffer, const char * hardware_name,
78 GLuint agp_mode )
79 {
80 unsigned offset;
81 char *cpu;
82
83 offset = sprintf( buffer, "Mesa DRI %s", hardware_name );
84
85 /* Append any AGP-specific information.
86 */
87 switch ( agp_mode ) {
88 case 1:
89 case 2:
90 case 4:
91 case 8:
92 offset += sprintf( & buffer[ offset ], " AGP %ux", agp_mode );
93 break;
94
95 default:
96 break;
97 }
98
99 /* Append any CPU-specific information.
100 */
101 cpu = _mesa_get_cpu_string();
102 if (cpu) {
103 offset += sprintf(buffer + offset, " %s", cpu);
104 free(cpu);
105 }
106
107 return offset;
108 }
109
110
111 /**
112 * Creates a set of \c struct gl_config that a driver will expose.
113 *
114 * A set of \c struct gl_config will be created based on the supplied
115 * parameters. The number of modes processed will be 2 *
116 * \c num_depth_stencil_bits * \c num_db_modes.
117 *
118 * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
119 * \c db_modes, and \c visType into each \c struct gl_config element.
120 * However, the meanings of \c fb_format and \c fb_type require further
121 * explanation. The \c fb_format specifies which color components are in
122 * each pixel and what the default order is. For example, \c GL_RGB specifies
123 * that red, green, blue are available and red is in the "most significant"
124 * position and blue is in the "least significant". The \c fb_type specifies
125 * the bit sizes of each component and the actual ordering. For example, if
126 * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
127 * are the blue value, bits [10:5] are the green value, and bits [4:0] are
128 * the red value.
129 *
130 * One sublte issue is the combination of \c GL_RGB or \c GL_BGR and either
131 * of the \c GL_UNSIGNED_INT_8_8_8_8 modes. The resulting mask values in the
132 * \c struct gl_config structure is \b identical to the \c GL_RGBA or
133 * \c GL_BGRA case, except the \c alphaMask is zero. This means that, as
134 * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
135 * still uses 32-bits.
136 *
137 * If in doubt, look at the tables used in the function.
138 *
139 * \param ptr_to_modes Pointer to a pointer to a linked list of
140 * \c struct gl_config. Upon completion, a pointer to
141 * the next element to be process will be stored here.
142 * If the function fails and returns \c GL_FALSE, this
143 * value will be unmodified, but some elements in the
144 * linked list may be modified.
145 * \param format Mesa mesa_format enum describing the pixel format
146 * \param depth_bits Array of depth buffer sizes to be exposed.
147 * \param stencil_bits Array of stencil buffer sizes to be exposed.
148 * \param num_depth_stencil_bits Number of entries in both \c depth_bits and
149 * \c stencil_bits.
150 * \param db_modes Array of buffer swap modes. If an element has a
151 * value of \c __DRI_ATTRIB_SWAP_NONE, then it
152 * represents a single-buffered mode. Other valid
153 * values are \c __DRI_ATTRIB_SWAP_EXCHANGE,
154 * \c __DRI_ATTRIB_SWAP_COPY, and \c __DRI_ATTRIB_SWAP_UNDEFINED.
155 * They represent the respective GLX values as in
156 * the GLX_OML_swap_method extension spec.
157 * \param num_db_modes Number of entries in \c db_modes.
158 * \param msaa_samples Array of msaa sample count. 0 represents a visual
159 * without a multisample buffer.
160 * \param num_msaa_modes Number of entries in \c msaa_samples.
161 * \param enable_accum Add an accum buffer to the configs
162 * \param color_depth_match Whether the color depth must match the zs depth
163 * This forces 32-bit color to have 24-bit depth, and
164 * 16-bit color to have 16-bit depth.
165 * \param mutable_render_buffer Enable __DRI_ATTRIB_MUTABLE_RENDER_BUFFER,
166 * which translates to
167 * EGL_MUTABLE_RENDER_BUFFER_BIT_KHR.
168 *
169 * \returns
170 * Pointer to any array of pointers to the \c __DRIconfig structures created
171 * for the specified formats. If there is an error, \c NULL is returned.
172 * Currently the only cause of failure is a bad parameter (i.e., unsupported
173 * \c format).
174 */
175 __DRIconfig **
176 driCreateConfigs(mesa_format format,
177 const uint8_t * depth_bits, const uint8_t * stencil_bits,
178 unsigned num_depth_stencil_bits,
179 const GLenum * db_modes, unsigned num_db_modes,
180 const uint8_t * msaa_samples, unsigned num_msaa_modes,
181 GLboolean enable_accum, GLboolean color_depth_match,
182 GLboolean mutable_render_buffer)
183 {
184 static const struct {
185 uint32_t masks[4];
186 int shifts[4];
187 } format_table[] = {
188 /* MESA_FORMAT_B5G6R5_UNORM */
189 {{ 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 },
190 { 11, 5, 0, -1 }},
191 /* MESA_FORMAT_B8G8R8X8_UNORM */
192 {{ 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 },
193 { 16, 8, 0, -1 }},
194 /* MESA_FORMAT_B8G8R8A8_UNORM */
195 {{ 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 },
196 { 16, 8, 0, 24 }},
197 /* MESA_FORMAT_B10G10R10X2_UNORM */
198 {{ 0x3FF00000, 0x000FFC00, 0x000003FF, 0x00000000 },
199 { 20, 10, 0, -1 }},
200 /* MESA_FORMAT_B10G10R10A2_UNORM */
201 {{ 0x3FF00000, 0x000FFC00, 0x000003FF, 0xC0000000 },
202 { 20, 10, 0, 30 }},
203 /* MESA_FORMAT_R8G8B8A8_UNORM */
204 {{ 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 },
205 { 0, 8, 16, 24 }},
206 /* MESA_FORMAT_R8G8B8X8_UNORM */
207 {{ 0x000000FF, 0x0000FF00, 0x00FF0000, 0x00000000 },
208 { 0, 8, 16, -1 }},
209 /* MESA_FORMAT_R10G10B10X2_UNORM */
210 {{ 0x000003FF, 0x000FFC00, 0x3FF00000, 0x00000000 },
211 { 0, 10, 20, -1 }},
212 /* MESA_FORMAT_R10G10B10A2_UNORM */
213 {{ 0x000003FF, 0x000FFC00, 0x3FF00000, 0xC0000000 },
214 { 0, 10, 20, 30 }},
215 /* MESA_FORMAT_RGBX_FLOAT16 */
216 {{ 0, 0, 0, 0},
217 { 0, 16, 32, -1 }},
218 /* MESA_FORMAT_RGBA_FLOAT16 */
219 {{ 0, 0, 0, 0},
220 { 0, 16, 32, 48 }},
221 };
222
223 const uint32_t * masks;
224 const int * shifts;
225 __DRIconfig **configs, **c;
226 struct gl_config *modes;
227 unsigned i, j, k, h;
228 unsigned num_modes;
229 unsigned num_accum_bits = (enable_accum) ? 2 : 1;
230 int red_bits;
231 int green_bits;
232 int blue_bits;
233 int alpha_bits;
234 bool is_srgb;
235 bool is_float;
236
237 switch (format) {
238 case MESA_FORMAT_B5G6R5_UNORM:
239 masks = format_table[0].masks;
240 shifts = format_table[0].shifts;
241 break;
242 case MESA_FORMAT_B8G8R8X8_UNORM:
243 case MESA_FORMAT_B8G8R8X8_SRGB:
244 masks = format_table[1].masks;
245 shifts = format_table[1].shifts;
246 break;
247 case MESA_FORMAT_B8G8R8A8_UNORM:
248 case MESA_FORMAT_B8G8R8A8_SRGB:
249 masks = format_table[2].masks;
250 shifts = format_table[2].shifts;
251 break;
252 case MESA_FORMAT_R8G8B8A8_UNORM:
253 case MESA_FORMAT_R8G8B8A8_SRGB:
254 masks = format_table[5].masks;
255 shifts = format_table[5].shifts;
256 break;
257 case MESA_FORMAT_R8G8B8X8_UNORM:
258 masks = format_table[6].masks;
259 shifts = format_table[6].shifts;
260 break;
261 case MESA_FORMAT_B10G10R10X2_UNORM:
262 masks = format_table[3].masks;
263 shifts = format_table[3].shifts;
264 break;
265 case MESA_FORMAT_B10G10R10A2_UNORM:
266 masks = format_table[4].masks;
267 shifts = format_table[4].shifts;
268 break;
269 case MESA_FORMAT_RGBX_FLOAT16:
270 masks = format_table[9].masks;
271 shifts = format_table[9].shifts;
272 break;
273 case MESA_FORMAT_RGBA_FLOAT16:
274 masks = format_table[10].masks;
275 shifts = format_table[10].shifts;
276 break;
277 case MESA_FORMAT_R10G10B10X2_UNORM:
278 masks = format_table[7].masks;
279 shifts = format_table[7].shifts;
280 break;
281 case MESA_FORMAT_R10G10B10A2_UNORM:
282 masks = format_table[8].masks;
283 shifts = format_table[8].shifts;
284 break;
285 default:
286 fprintf(stderr, "[%s:%u] Unknown framebuffer type %s (%d).\n",
287 __func__, __LINE__,
288 _mesa_get_format_name(format), format);
289 return NULL;
290 }
291
292 red_bits = _mesa_get_format_bits(format, GL_RED_BITS);
293 green_bits = _mesa_get_format_bits(format, GL_GREEN_BITS);
294 blue_bits = _mesa_get_format_bits(format, GL_BLUE_BITS);
295 alpha_bits = _mesa_get_format_bits(format, GL_ALPHA_BITS);
296 is_srgb = _mesa_is_format_srgb(format);
297 is_float = _mesa_get_format_datatype(format) == GL_FLOAT;
298
299 num_modes = num_depth_stencil_bits * num_db_modes * num_accum_bits * num_msaa_modes;
300 configs = calloc(num_modes + 1, sizeof *configs);
301 if (configs == NULL)
302 return NULL;
303
304 c = configs;
305 for ( k = 0 ; k < num_depth_stencil_bits ; k++ ) {
306 for ( i = 0 ; i < num_db_modes ; i++ ) {
307 for ( h = 0 ; h < num_msaa_modes; h++ ) {
308 for ( j = 0 ; j < num_accum_bits ; j++ ) {
309 if (color_depth_match &&
310 (depth_bits[k] || stencil_bits[k])) {
311 /* Depth can really only be 0, 16, 24, or 32. A 32-bit
312 * color format still matches 24-bit depth, as there
313 * is an implicit 8-bit stencil. So really we just
314 * need to make sure that color/depth are both 16 or
315 * both non-16.
316 */
317 if ((depth_bits[k] + stencil_bits[k] == 16) !=
318 (red_bits + green_bits + blue_bits + alpha_bits == 16))
319 continue;
320 }
321
322 *c = malloc (sizeof **c);
323 modes = &(*c)->modes;
324 c++;
325
326 memset(modes, 0, sizeof *modes);
327 modes->floatMode = is_float;
328 modes->redBits = red_bits;
329 modes->greenBits = green_bits;
330 modes->blueBits = blue_bits;
331 modes->alphaBits = alpha_bits;
332 modes->redMask = masks[0];
333 modes->greenMask = masks[1];
334 modes->blueMask = masks[2];
335 modes->alphaMask = masks[3];
336 modes->redShift = shifts[0];
337 modes->greenShift = shifts[1];
338 modes->blueShift = shifts[2];
339 modes->alphaShift = shifts[3];
340 modes->rgbBits = modes->redBits + modes->greenBits
341 + modes->blueBits + modes->alphaBits;
342
343 modes->accumRedBits = 16 * j;
344 modes->accumGreenBits = 16 * j;
345 modes->accumBlueBits = 16 * j;
346 modes->accumAlphaBits = (masks[3] != 0) ? 16 * j : 0;
347 modes->visualRating = (j == 0) ? GLX_NONE : GLX_SLOW_CONFIG;
348
349 modes->stencilBits = stencil_bits[k];
350 modes->depthBits = depth_bits[k];
351
352 modes->transparentPixel = GLX_NONE;
353 modes->transparentRed = GLX_DONT_CARE;
354 modes->transparentGreen = GLX_DONT_CARE;
355 modes->transparentBlue = GLX_DONT_CARE;
356 modes->transparentAlpha = GLX_DONT_CARE;
357 modes->transparentIndex = GLX_DONT_CARE;
358 modes->rgbMode = GL_TRUE;
359
360 if (db_modes[i] == __DRI_ATTRIB_SWAP_NONE) {
361 modes->doubleBufferMode = GL_FALSE;
362 modes->swapMethod = __DRI_ATTRIB_SWAP_UNDEFINED;
363 }
364 else {
365 modes->doubleBufferMode = GL_TRUE;
366 modes->swapMethod = db_modes[i];
367 }
368
369 modes->samples = msaa_samples[h];
370 modes->sampleBuffers = modes->samples ? 1 : 0;
371
372
373 modes->haveAccumBuffer = ((modes->accumRedBits +
374 modes->accumGreenBits +
375 modes->accumBlueBits +
376 modes->accumAlphaBits) > 0);
377 modes->haveDepthBuffer = (modes->depthBits > 0);
378 modes->haveStencilBuffer = (modes->stencilBits > 0);
379
380 modes->bindToTextureRgb = GL_TRUE;
381 modes->bindToTextureRgba = GL_TRUE;
382 modes->bindToMipmapTexture = GL_FALSE;
383 modes->bindToTextureTargets =
384 __DRI_ATTRIB_TEXTURE_1D_BIT |
385 __DRI_ATTRIB_TEXTURE_2D_BIT |
386 __DRI_ATTRIB_TEXTURE_RECTANGLE_BIT;
387
388 modes->yInverted = GL_TRUE;
389 modes->sRGBCapable = is_srgb;
390 modes->mutableRenderBuffer = mutable_render_buffer;
391 }
392 }
393 }
394 }
395 *c = NULL;
396
397 return configs;
398 }
399
400 __DRIconfig **driConcatConfigs(__DRIconfig **a,
401 __DRIconfig **b)
402 {
403 __DRIconfig **all;
404 int i, j, index;
405
406 if (a == NULL || a[0] == NULL)
407 return b;
408 else if (b == NULL || b[0] == NULL)
409 return a;
410
411 i = 0;
412 while (a[i] != NULL)
413 i++;
414 j = 0;
415 while (b[j] != NULL)
416 j++;
417
418 all = malloc((i + j + 1) * sizeof *all);
419 index = 0;
420 for (i = 0; a[i] != NULL; i++)
421 all[index++] = a[i];
422 for (j = 0; b[j] != NULL; j++)
423 all[index++] = b[j];
424 all[index++] = NULL;
425
426 free(a);
427 free(b);
428
429 return all;
430 }
431
432 #define __ATTRIB(attrib, field) \
433 { attrib, offsetof(struct gl_config, field) }
434
435 static const struct { unsigned int attrib, offset; } attribMap[] = {
436 __ATTRIB(__DRI_ATTRIB_BUFFER_SIZE, rgbBits),
437 __ATTRIB(__DRI_ATTRIB_LEVEL, level),
438 __ATTRIB(__DRI_ATTRIB_RED_SIZE, redBits),
439 __ATTRIB(__DRI_ATTRIB_GREEN_SIZE, greenBits),
440 __ATTRIB(__DRI_ATTRIB_BLUE_SIZE, blueBits),
441 __ATTRIB(__DRI_ATTRIB_ALPHA_SIZE, alphaBits),
442 __ATTRIB(__DRI_ATTRIB_DEPTH_SIZE, depthBits),
443 __ATTRIB(__DRI_ATTRIB_STENCIL_SIZE, stencilBits),
444 __ATTRIB(__DRI_ATTRIB_ACCUM_RED_SIZE, accumRedBits),
445 __ATTRIB(__DRI_ATTRIB_ACCUM_GREEN_SIZE, accumGreenBits),
446 __ATTRIB(__DRI_ATTRIB_ACCUM_BLUE_SIZE, accumBlueBits),
447 __ATTRIB(__DRI_ATTRIB_ACCUM_ALPHA_SIZE, accumAlphaBits),
448 __ATTRIB(__DRI_ATTRIB_SAMPLE_BUFFERS, sampleBuffers),
449 __ATTRIB(__DRI_ATTRIB_SAMPLES, samples),
450 __ATTRIB(__DRI_ATTRIB_DOUBLE_BUFFER, doubleBufferMode),
451 __ATTRIB(__DRI_ATTRIB_STEREO, stereoMode),
452 __ATTRIB(__DRI_ATTRIB_AUX_BUFFERS, numAuxBuffers),
453 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_TYPE, transparentPixel),
454 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_INDEX_VALUE, transparentPixel),
455 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_RED_VALUE, transparentRed),
456 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_GREEN_VALUE, transparentGreen),
457 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_BLUE_VALUE, transparentBlue),
458 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE, transparentAlpha),
459 __ATTRIB(__DRI_ATTRIB_RED_MASK, redMask),
460 __ATTRIB(__DRI_ATTRIB_RED_SHIFT, redShift),
461 __ATTRIB(__DRI_ATTRIB_GREEN_MASK, greenMask),
462 __ATTRIB(__DRI_ATTRIB_GREEN_SHIFT, greenShift),
463 __ATTRIB(__DRI_ATTRIB_BLUE_MASK, blueMask),
464 __ATTRIB(__DRI_ATTRIB_BLUE_SHIFT, blueShift),
465 __ATTRIB(__DRI_ATTRIB_ALPHA_MASK, alphaMask),
466 __ATTRIB(__DRI_ATTRIB_ALPHA_SHIFT, alphaShift),
467 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_WIDTH, maxPbufferWidth),
468 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_HEIGHT, maxPbufferHeight),
469 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_PIXELS, maxPbufferPixels),
470 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH, optimalPbufferWidth),
471 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT, optimalPbufferHeight),
472 __ATTRIB(__DRI_ATTRIB_SWAP_METHOD, swapMethod),
473 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGB, bindToTextureRgb),
474 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGBA, bindToTextureRgba),
475 __ATTRIB(__DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE, bindToMipmapTexture),
476 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS, bindToTextureTargets),
477 __ATTRIB(__DRI_ATTRIB_YINVERTED, yInverted),
478 __ATTRIB(__DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE, sRGBCapable),
479 __ATTRIB(__DRI_ATTRIB_MUTABLE_RENDER_BUFFER, mutableRenderBuffer),
480
481 /* The struct field doesn't matter here, these are handled by the
482 * switch in driGetConfigAttribIndex. We need them in the array
483 * so the iterator includes them though.*/
484 __ATTRIB(__DRI_ATTRIB_RENDER_TYPE, level),
485 __ATTRIB(__DRI_ATTRIB_CONFIG_CAVEAT, level),
486 };
487
488
489 /**
490 * Return the value of a configuration attribute. The attribute is
491 * indicated by the index.
492 */
493 static int
494 driGetConfigAttribIndex(const __DRIconfig *config,
495 unsigned int index, unsigned int *value)
496 {
497 switch (attribMap[index].attrib) {
498 case __DRI_ATTRIB_RENDER_TYPE:
499 /* no support for color index mode */
500 *value = __DRI_ATTRIB_RGBA_BIT;
501 if (config->modes.floatMode)
502 *value |= __DRI_ATTRIB_FLOAT_BIT;
503 break;
504 case __DRI_ATTRIB_CONFIG_CAVEAT:
505 if (config->modes.visualRating == GLX_NON_CONFORMANT_CONFIG)
506 *value = __DRI_ATTRIB_NON_CONFORMANT_CONFIG;
507 else if (config->modes.visualRating == GLX_SLOW_CONFIG)
508 *value = __DRI_ATTRIB_SLOW_BIT;
509 else
510 *value = 0;
511 break;
512 default:
513 /* any other int-sized field */
514 *value = *(unsigned int *)
515 ((char *) &config->modes + attribMap[index].offset);
516
517 break;
518 }
519
520 return GL_TRUE;
521 }
522
523
524 /**
525 * Get the value of a configuration attribute.
526 * \param attrib the attribute (one of the _DRI_ATTRIB_x tokens)
527 * \param value returns the attribute's value
528 * \return 1 for success, 0 for failure
529 */
530 int
531 driGetConfigAttrib(const __DRIconfig *config,
532 unsigned int attrib, unsigned int *value)
533 {
534 unsigned i;
535
536 for (i = 0; i < ARRAY_SIZE(attribMap); i++)
537 if (attribMap[i].attrib == attrib)
538 return driGetConfigAttribIndex(config, i, value);
539
540 return GL_FALSE;
541 }
542
543
544 /**
545 * Get a configuration attribute name and value, given an index.
546 * \param index which field of the __DRIconfig to query
547 * \param attrib returns the attribute name (one of the _DRI_ATTRIB_x tokens)
548 * \param value returns the attribute's value
549 * \return 1 for success, 0 for failure
550 */
551 int
552 driIndexConfigAttrib(const __DRIconfig *config, int index,
553 unsigned int *attrib, unsigned int *value)
554 {
555 if (index >= 0 && index < ARRAY_SIZE(attribMap)) {
556 *attrib = attribMap[index].attrib;
557 return driGetConfigAttribIndex(config, index, value);
558 }
559
560 return GL_FALSE;
561 }
562
563 /**
564 * Implement queries for values that are common across all Mesa drivers
565 *
566 * Currently only the following queries are supported by this function:
567 *
568 * - \c __DRI2_RENDERER_VERSION
569 * - \c __DRI2_RENDERER_PREFERRED_PROFILE
570 * - \c __DRI2_RENDERER_OPENGL_CORE_PROFILE_VERSION
571 * - \c __DRI2_RENDERER_OPENGL_COMPATIBLITY_PROFILE_VERSION
572 * - \c __DRI2_RENDERER_ES_PROFILE_VERSION
573 * - \c __DRI2_RENDERER_ES2_PROFILE_VERSION
574 *
575 * \returns
576 * Zero if a recognized value of \c param is supplied, -1 otherwise.
577 */
578 int
579 driQueryRendererIntegerCommon(__DRIscreen *psp, int param, unsigned int *value)
580 {
581 switch (param) {
582 case __DRI2_RENDERER_VERSION: {
583 static const char *const ver = PACKAGE_VERSION;
584 char *endptr;
585 int v[3];
586
587 v[0] = strtol(ver, &endptr, 10);
588 assert(endptr[0] == '.');
589 if (endptr[0] != '.')
590 return -1;
591
592 v[1] = strtol(endptr + 1, &endptr, 10);
593 assert(endptr[0] == '.');
594 if (endptr[0] != '.')
595 return -1;
596
597 v[2] = strtol(endptr + 1, &endptr, 10);
598
599 value[0] = v[0];
600 value[1] = v[1];
601 value[2] = v[2];
602 return 0;
603 }
604 case __DRI2_RENDERER_PREFERRED_PROFILE:
605 value[0] = (psp->max_gl_core_version != 0)
606 ? (1U << __DRI_API_OPENGL_CORE) : (1U << __DRI_API_OPENGL);
607 return 0;
608 case __DRI2_RENDERER_OPENGL_CORE_PROFILE_VERSION:
609 value[0] = psp->max_gl_core_version / 10;
610 value[1] = psp->max_gl_core_version % 10;
611 return 0;
612 case __DRI2_RENDERER_OPENGL_COMPATIBILITY_PROFILE_VERSION:
613 value[0] = psp->max_gl_compat_version / 10;
614 value[1] = psp->max_gl_compat_version % 10;
615 return 0;
616 case __DRI2_RENDERER_OPENGL_ES_PROFILE_VERSION:
617 value[0] = psp->max_gl_es1_version / 10;
618 value[1] = psp->max_gl_es1_version % 10;
619 return 0;
620 case __DRI2_RENDERER_OPENGL_ES2_PROFILE_VERSION:
621 value[0] = psp->max_gl_es2_version / 10;
622 value[1] = psp->max_gl_es2_version % 10;
623 return 0;
624 default:
625 break;
626 }
627
628 return -1;
629 }