i965: Stop passing num_samples to intel_miptree_alloc_hiz().
[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 <stdbool.h>
35 #include "main/macros.h"
36 #include "main/mtypes.h"
37 #include "main/cpuinfo.h"
38 #include "main/extensions.h"
39 #include "utils.h"
40
41
42 unsigned
43 driParseDebugString( const char * debug,
44 const struct dri_debug_control * control )
45 {
46 unsigned flag;
47
48
49 flag = 0;
50 if ( debug != NULL ) {
51 while( control->string != NULL ) {
52 if ( !strcmp( debug, "all" ) ||
53 strstr( debug, control->string ) != NULL ) {
54 flag |= control->flag;
55 }
56
57 control++;
58 }
59 }
60
61 return flag;
62 }
63
64
65
66 /**
67 * Create the \c GL_RENDERER string for DRI drivers.
68 *
69 * Almost all DRI drivers use a \c GL_RENDERER string of the form:
70 *
71 * "Mesa DRI <chip> <driver date> <AGP speed) <CPU information>"
72 *
73 * Using the supplied chip name, driver data, and AGP speed, this function
74 * creates the string.
75 *
76 * \param buffer Buffer to hold the \c GL_RENDERER string.
77 * \param hardware_name Name of the hardware.
78 * \param agp_mode AGP mode (speed).
79 *
80 * \returns
81 * The length of the string stored in \c buffer. This does \b not include
82 * the terminating \c NUL character.
83 */
84 unsigned
85 driGetRendererString( char * buffer, const char * hardware_name,
86 GLuint agp_mode )
87 {
88 unsigned offset;
89 char *cpu;
90
91 offset = sprintf( buffer, "Mesa DRI %s", hardware_name );
92
93 /* Append any AGP-specific information.
94 */
95 switch ( agp_mode ) {
96 case 1:
97 case 2:
98 case 4:
99 case 8:
100 offset += sprintf( & buffer[ offset ], " AGP %ux", agp_mode );
101 break;
102
103 default:
104 break;
105 }
106
107 /* Append any CPU-specific information.
108 */
109 cpu = _mesa_get_cpu_string();
110 if (cpu) {
111 offset += sprintf(buffer + offset, " %s", cpu);
112 free(cpu);
113 }
114
115 return offset;
116 }
117
118
119 /**
120 * Creates a set of \c struct gl_config that a driver will expose.
121 *
122 * A set of \c struct gl_config will be created based on the supplied
123 * parameters. The number of modes processed will be 2 *
124 * \c num_depth_stencil_bits * \c num_db_modes.
125 *
126 * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
127 * \c db_modes, and \c visType into each \c struct gl_config element.
128 * However, the meanings of \c fb_format and \c fb_type require further
129 * explanation. The \c fb_format specifies which color components are in
130 * each pixel and what the default order is. For example, \c GL_RGB specifies
131 * that red, green, blue are available and red is in the "most significant"
132 * position and blue is in the "least significant". The \c fb_type specifies
133 * the bit sizes of each component and the actual ordering. For example, if
134 * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
135 * are the blue value, bits [10:5] are the green value, and bits [4:0] are
136 * the red value.
137 *
138 * One sublte issue is the combination of \c GL_RGB or \c GL_BGR and either
139 * of the \c GL_UNSIGNED_INT_8_8_8_8 modes. The resulting mask values in the
140 * \c struct gl_config structure is \b identical to the \c GL_RGBA or
141 * \c GL_BGRA case, except the \c alphaMask is zero. This means that, as
142 * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
143 * still uses 32-bits.
144 *
145 * If in doubt, look at the tables used in the function.
146 *
147 * \param ptr_to_modes Pointer to a pointer to a linked list of
148 * \c struct gl_config. Upon completion, a pointer to
149 * the next element to be process will be stored here.
150 * If the function fails and returns \c GL_FALSE, this
151 * value will be unmodified, but some elements in the
152 * linked list may be modified.
153 * \param format Mesa gl_format enum describing the pixel format
154 * \param depth_bits Array of depth buffer sizes to be exposed.
155 * \param stencil_bits Array of stencil buffer sizes to be exposed.
156 * \param num_depth_stencil_bits Number of entries in both \c depth_bits and
157 * \c stencil_bits.
158 * \param db_modes Array of buffer swap modes. If an element has a
159 * value of \c GLX_NONE, then it represents a
160 * single-buffered mode. Other valid values are
161 * \c GLX_SWAP_EXCHANGE_OML, \c GLX_SWAP_COPY_OML, and
162 * \c GLX_SWAP_UNDEFINED_OML. See the
163 * GLX_OML_swap_method extension spec for more details.
164 * \param num_db_modes Number of entries in \c db_modes.
165 * \param msaa_samples Array of msaa sample count. 0 represents a visual
166 * without a multisample buffer.
167 * \param num_msaa_modes Number of entries in \c msaa_samples.
168 * \param visType GLX visual type. Usually either \c GLX_TRUE_COLOR or
169 * \c GLX_DIRECT_COLOR.
170 *
171 * \returns
172 * Pointer to any array of pointers to the \c __DRIconfig structures created
173 * for the specified formats. If there is an error, \c NULL is returned.
174 * Currently the only cause of failure is a bad parameter (i.e., unsupported
175 * \c format).
176 */
177 __DRIconfig **
178 driCreateConfigs(gl_format format,
179 const uint8_t * depth_bits, const uint8_t * stencil_bits,
180 unsigned num_depth_stencil_bits,
181 const GLenum * db_modes, unsigned num_db_modes,
182 const uint8_t * msaa_samples, unsigned num_msaa_modes,
183 GLboolean enable_accum)
184 {
185 static const uint32_t masks_table[][4] = {
186 /* MESA_FORMAT_RGB565 */
187 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 },
188 /* MESA_FORMAT_XRGB8888 */
189 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 },
190 /* MESA_FORMAT_ARGB8888 */
191 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 },
192 };
193
194 const uint32_t * masks;
195 __DRIconfig **configs, **c;
196 struct gl_config *modes;
197 unsigned i, j, k, h;
198 unsigned num_modes;
199 unsigned num_accum_bits = (enable_accum) ? 2 : 1;
200 int red_bits;
201 int green_bits;
202 int blue_bits;
203 int alpha_bits;
204 bool is_srgb;
205
206 switch (format) {
207 case MESA_FORMAT_RGB565:
208 masks = masks_table[0];
209 break;
210 case MESA_FORMAT_XRGB8888:
211 masks = masks_table[1];
212 break;
213 case MESA_FORMAT_ARGB8888:
214 case MESA_FORMAT_SARGB8:
215 masks = masks_table[2];
216 break;
217 default:
218 fprintf(stderr, "[%s:%u] Unknown framebuffer type %s (%d).\n",
219 __FUNCTION__, __LINE__,
220 _mesa_get_format_name(format), format);
221 return NULL;
222 }
223
224 red_bits = _mesa_get_format_bits(format, GL_RED_BITS);
225 green_bits = _mesa_get_format_bits(format, GL_GREEN_BITS);
226 blue_bits = _mesa_get_format_bits(format, GL_BLUE_BITS);
227 alpha_bits = _mesa_get_format_bits(format, GL_ALPHA_BITS);
228 is_srgb = _mesa_get_format_color_encoding(format) == GL_SRGB;
229
230 num_modes = num_depth_stencil_bits * num_db_modes * num_accum_bits * num_msaa_modes;
231 configs = calloc(1, (num_modes + 1) * sizeof *configs);
232 if (configs == NULL)
233 return NULL;
234
235 c = configs;
236 for ( k = 0 ; k < num_depth_stencil_bits ; k++ ) {
237 for ( i = 0 ; i < num_db_modes ; i++ ) {
238 for ( h = 0 ; h < num_msaa_modes; h++ ) {
239 for ( j = 0 ; j < num_accum_bits ; j++ ) {
240 *c = malloc (sizeof **c);
241 modes = &(*c)->modes;
242 c++;
243
244 memset(modes, 0, sizeof *modes);
245 modes->redBits = red_bits;
246 modes->greenBits = green_bits;
247 modes->blueBits = blue_bits;
248 modes->alphaBits = alpha_bits;
249 modes->redMask = masks[0];
250 modes->greenMask = masks[1];
251 modes->blueMask = masks[2];
252 modes->alphaMask = masks[3];
253 modes->rgbBits = modes->redBits + modes->greenBits
254 + modes->blueBits + modes->alphaBits;
255
256 modes->accumRedBits = 16 * j;
257 modes->accumGreenBits = 16 * j;
258 modes->accumBlueBits = 16 * j;
259 modes->accumAlphaBits = (masks[3] != 0) ? 16 * j : 0;
260 modes->visualRating = (j == 0) ? GLX_NONE : GLX_SLOW_CONFIG;
261
262 modes->stencilBits = stencil_bits[k];
263 modes->depthBits = depth_bits[k];
264
265 modes->transparentPixel = GLX_NONE;
266 modes->transparentRed = GLX_DONT_CARE;
267 modes->transparentGreen = GLX_DONT_CARE;
268 modes->transparentBlue = GLX_DONT_CARE;
269 modes->transparentAlpha = GLX_DONT_CARE;
270 modes->transparentIndex = GLX_DONT_CARE;
271 modes->rgbMode = GL_TRUE;
272
273 if ( db_modes[i] == GLX_NONE ) {
274 modes->doubleBufferMode = GL_FALSE;
275 }
276 else {
277 modes->doubleBufferMode = GL_TRUE;
278 modes->swapMethod = db_modes[i];
279 }
280
281 modes->samples = msaa_samples[h];
282 modes->sampleBuffers = modes->samples ? 1 : 0;
283
284
285 modes->haveAccumBuffer = ((modes->accumRedBits +
286 modes->accumGreenBits +
287 modes->accumBlueBits +
288 modes->accumAlphaBits) > 0);
289 modes->haveDepthBuffer = (modes->depthBits > 0);
290 modes->haveStencilBuffer = (modes->stencilBits > 0);
291
292 modes->bindToTextureRgb = GL_TRUE;
293 modes->bindToTextureRgba = GL_TRUE;
294 modes->bindToMipmapTexture = GL_FALSE;
295 modes->bindToTextureTargets =
296 __DRI_ATTRIB_TEXTURE_1D_BIT |
297 __DRI_ATTRIB_TEXTURE_2D_BIT |
298 __DRI_ATTRIB_TEXTURE_RECTANGLE_BIT;
299
300 modes->sRGBCapable = is_srgb;
301 }
302 }
303 }
304 }
305 *c = NULL;
306
307 return configs;
308 }
309
310 __DRIconfig **driConcatConfigs(__DRIconfig **a,
311 __DRIconfig **b)
312 {
313 __DRIconfig **all;
314 int i, j, index;
315
316 if (a == NULL || a[0] == NULL)
317 return b;
318 else if (b == NULL || b[0] == NULL)
319 return a;
320
321 i = 0;
322 while (a[i] != NULL)
323 i++;
324 j = 0;
325 while (b[j] != NULL)
326 j++;
327
328 all = malloc((i + j + 1) * sizeof *all);
329 index = 0;
330 for (i = 0; a[i] != NULL; i++)
331 all[index++] = a[i];
332 for (j = 0; b[j] != NULL; j++)
333 all[index++] = b[j];
334 all[index++] = NULL;
335
336 free(a);
337 free(b);
338
339 return all;
340 }
341
342 #define __ATTRIB(attrib, field) \
343 { attrib, offsetof(struct gl_config, field) }
344
345 static const struct { unsigned int attrib, offset; } attribMap[] = {
346 __ATTRIB(__DRI_ATTRIB_BUFFER_SIZE, rgbBits),
347 __ATTRIB(__DRI_ATTRIB_LEVEL, level),
348 __ATTRIB(__DRI_ATTRIB_RED_SIZE, redBits),
349 __ATTRIB(__DRI_ATTRIB_GREEN_SIZE, greenBits),
350 __ATTRIB(__DRI_ATTRIB_BLUE_SIZE, blueBits),
351 __ATTRIB(__DRI_ATTRIB_ALPHA_SIZE, alphaBits),
352 __ATTRIB(__DRI_ATTRIB_DEPTH_SIZE, depthBits),
353 __ATTRIB(__DRI_ATTRIB_STENCIL_SIZE, stencilBits),
354 __ATTRIB(__DRI_ATTRIB_ACCUM_RED_SIZE, accumRedBits),
355 __ATTRIB(__DRI_ATTRIB_ACCUM_GREEN_SIZE, accumGreenBits),
356 __ATTRIB(__DRI_ATTRIB_ACCUM_BLUE_SIZE, accumBlueBits),
357 __ATTRIB(__DRI_ATTRIB_ACCUM_ALPHA_SIZE, accumAlphaBits),
358 __ATTRIB(__DRI_ATTRIB_SAMPLE_BUFFERS, sampleBuffers),
359 __ATTRIB(__DRI_ATTRIB_SAMPLES, samples),
360 __ATTRIB(__DRI_ATTRIB_DOUBLE_BUFFER, doubleBufferMode),
361 __ATTRIB(__DRI_ATTRIB_STEREO, stereoMode),
362 __ATTRIB(__DRI_ATTRIB_AUX_BUFFERS, numAuxBuffers),
363 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_TYPE, transparentPixel),
364 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_INDEX_VALUE, transparentPixel),
365 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_RED_VALUE, transparentRed),
366 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_GREEN_VALUE, transparentGreen),
367 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_BLUE_VALUE, transparentBlue),
368 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE, transparentAlpha),
369 __ATTRIB(__DRI_ATTRIB_FLOAT_MODE, floatMode),
370 __ATTRIB(__DRI_ATTRIB_RED_MASK, redMask),
371 __ATTRIB(__DRI_ATTRIB_GREEN_MASK, greenMask),
372 __ATTRIB(__DRI_ATTRIB_BLUE_MASK, blueMask),
373 __ATTRIB(__DRI_ATTRIB_ALPHA_MASK, alphaMask),
374 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_WIDTH, maxPbufferWidth),
375 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_HEIGHT, maxPbufferHeight),
376 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_PIXELS, maxPbufferPixels),
377 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH, optimalPbufferWidth),
378 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT, optimalPbufferHeight),
379 __ATTRIB(__DRI_ATTRIB_SWAP_METHOD, swapMethod),
380 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGB, bindToTextureRgb),
381 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGBA, bindToTextureRgba),
382 __ATTRIB(__DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE, bindToMipmapTexture),
383 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS, bindToTextureTargets),
384 __ATTRIB(__DRI_ATTRIB_YINVERTED, yInverted),
385 __ATTRIB(__DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE, sRGBCapable),
386
387 /* The struct field doesn't matter here, these are handled by the
388 * switch in driGetConfigAttribIndex. We need them in the array
389 * so the iterator includes them though.*/
390 __ATTRIB(__DRI_ATTRIB_RENDER_TYPE, level),
391 __ATTRIB(__DRI_ATTRIB_CONFIG_CAVEAT, level),
392 __ATTRIB(__DRI_ATTRIB_SWAP_METHOD, level)
393 };
394
395
396 /**
397 * Return the value of a configuration attribute. The attribute is
398 * indicated by the index.
399 */
400 static int
401 driGetConfigAttribIndex(const __DRIconfig *config,
402 unsigned int index, unsigned int *value)
403 {
404 switch (attribMap[index].attrib) {
405 case __DRI_ATTRIB_RENDER_TYPE:
406 /* no support for color index mode */
407 *value = __DRI_ATTRIB_RGBA_BIT;
408 break;
409 case __DRI_ATTRIB_CONFIG_CAVEAT:
410 if (config->modes.visualRating == GLX_NON_CONFORMANT_CONFIG)
411 *value = __DRI_ATTRIB_NON_CONFORMANT_CONFIG;
412 else if (config->modes.visualRating == GLX_SLOW_CONFIG)
413 *value = __DRI_ATTRIB_SLOW_BIT;
414 else
415 *value = 0;
416 break;
417 case __DRI_ATTRIB_SWAP_METHOD:
418 /* XXX no return value??? */
419 break;
420
421 case __DRI_ATTRIB_FLOAT_MODE:
422 /* this field is not int-sized */
423 *value = config->modes.floatMode;
424 break;
425
426 default:
427 /* any other int-sized field */
428 *value = *(unsigned int *)
429 ((char *) &config->modes + attribMap[index].offset);
430
431 break;
432 }
433
434 return GL_TRUE;
435 }
436
437
438 /**
439 * Get the value of a configuration attribute.
440 * \param attrib the attribute (one of the _DRI_ATTRIB_x tokens)
441 * \param value returns the attribute's value
442 * \return 1 for success, 0 for failure
443 */
444 int
445 driGetConfigAttrib(const __DRIconfig *config,
446 unsigned int attrib, unsigned int *value)
447 {
448 int i;
449
450 for (i = 0; i < ARRAY_SIZE(attribMap); i++)
451 if (attribMap[i].attrib == attrib)
452 return driGetConfigAttribIndex(config, i, value);
453
454 return GL_FALSE;
455 }
456
457
458 /**
459 * Get a configuration attribute name and value, given an index.
460 * \param index which field of the __DRIconfig to query
461 * \param attrib returns the attribute name (one of the _DRI_ATTRIB_x tokens)
462 * \param value returns the attribute's value
463 * \return 1 for success, 0 for failure
464 */
465 int
466 driIndexConfigAttrib(const __DRIconfig *config, int index,
467 unsigned int *attrib, unsigned int *value)
468 {
469 if (index >= 0 && index < ARRAY_SIZE(attribMap)) {
470 *attrib = attribMap[index].attrib;
471 return driGetConfigAttribIndex(config, index, value);
472 }
473
474 return GL_FALSE;
475 }