gallium: move driinfo XML to pipe_loader
[mesa.git] / src / gallium / state_trackers / dri / dri_screen.c
1 /**************************************************************************
2 *
3 * Copyright 2009, VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27 /*
28 * Author: Keith Whitwell <keithw@vmware.com>
29 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
30 */
31
32 #include "utils.h"
33
34 #include "dri_screen.h"
35 #include "dri_context.h"
36
37 #include "util/u_inlines.h"
38 #include "pipe/p_screen.h"
39 #include "pipe/p_format.h"
40 #include "pipe-loader/pipe_loader.h"
41 #include "state_tracker/st_gl_api.h" /* for st_gl_api_create */
42 #include "state_tracker/drm_driver.h"
43
44 #include "util/u_debug.h"
45 #include "util/u_format_s3tc.h"
46
47 #define MSAA_VISUAL_MAX_SAMPLES 32
48
49 #undef false
50
51 const __DRIconfigOptionsExtension gallium_config_options = {
52 .base = { __DRI_CONFIG_OPTIONS, 1 },
53 .xml = gallium_driinfo_xml
54 };
55
56 #define false 0
57
58 static void
59 dri_fill_st_options(struct dri_screen *screen)
60 {
61 struct st_config_options *options = &screen->options;
62 const struct driOptionCache *optionCache = &screen->optionCache;
63
64 options->disable_blend_func_extended =
65 driQueryOptionb(optionCache, "disable_blend_func_extended");
66 options->disable_glsl_line_continuations =
67 driQueryOptionb(optionCache, "disable_glsl_line_continuations");
68 options->disable_shader_bit_encoding =
69 driQueryOptionb(optionCache, "disable_shader_bit_encoding");
70 options->force_glsl_extensions_warn =
71 driQueryOptionb(optionCache, "force_glsl_extensions_warn");
72 options->force_glsl_version =
73 driQueryOptioni(optionCache, "force_glsl_version");
74 options->force_s3tc_enable =
75 driQueryOptionb(optionCache, "force_s3tc_enable");
76 options->allow_glsl_extension_directive_midshader =
77 driQueryOptionb(optionCache, "allow_glsl_extension_directive_midshader");
78 options->allow_glsl_builtin_variable_redeclaration =
79 driQueryOptionb(optionCache, "allow_glsl_builtin_variable_redeclaration");
80 options->allow_higher_compat_version =
81 driQueryOptionb(optionCache, "allow_higher_compat_version");
82 options->glsl_zero_init = driQueryOptionb(optionCache, "glsl_zero_init");
83 options->force_glsl_abs_sqrt =
84 driQueryOptionb(optionCache, "force_glsl_abs_sqrt");
85
86 driComputeOptionsSha1(optionCache, options->config_options_sha1);
87 }
88
89 static unsigned
90 dri_loader_get_cap(struct dri_screen *screen, enum dri_loader_cap cap)
91 {
92 const __DRIdri2LoaderExtension *dri2_loader = screen->sPriv->dri2.loader;
93 const __DRIimageLoaderExtension *image_loader = screen->sPriv->image.loader;
94
95 if (dri2_loader && dri2_loader->base.version >= 4)
96 return dri2_loader->getCapability(screen->sPriv->loaderPrivate, cap);
97
98 if (image_loader && image_loader->base.version >= 2)
99 return image_loader->getCapability(screen->sPriv->loaderPrivate, cap);
100
101 return 0;
102 }
103
104 static const __DRIconfig **
105 dri_fill_in_modes(struct dri_screen *screen)
106 {
107 static const mesa_format mesa_formats[] = {
108 MESA_FORMAT_B8G8R8A8_UNORM,
109 MESA_FORMAT_B8G8R8X8_UNORM,
110 MESA_FORMAT_B8G8R8A8_SRGB,
111 MESA_FORMAT_B8G8R8X8_SRGB,
112 MESA_FORMAT_B5G6R5_UNORM,
113
114 /* The 32-bit RGBA format must not precede the 32-bit BGRA format.
115 * Likewise for RGBX and BGRX. Otherwise, the GLX client and the GLX
116 * server may disagree on which format the GLXFBConfig represents,
117 * resulting in swapped color channels.
118 *
119 * The problem, as of 2017-05-30:
120 * When matching a GLXFBConfig to a __DRIconfig, GLX ignores the channel
121 * order and chooses the first __DRIconfig with the expected channel
122 * sizes. Specifically, GLX compares the GLXFBConfig's and __DRIconfig's
123 * __DRI_ATTRIB_{CHANNEL}_SIZE but ignores __DRI_ATTRIB_{CHANNEL}_MASK.
124 *
125 * EGL does not suffer from this problem. It correctly compares the
126 * channel masks when matching EGLConfig to __DRIconfig.
127 */
128
129 /* Required by Android, for HAL_PIXEL_FORMAT_RGBA_8888. */
130 MESA_FORMAT_R8G8B8A8_UNORM,
131
132 /* Required by Android, for HAL_PIXEL_FORMAT_RGBX_8888. */
133 MESA_FORMAT_R8G8B8X8_UNORM,
134 };
135 static const enum pipe_format pipe_formats[] = {
136 PIPE_FORMAT_BGRA8888_UNORM,
137 PIPE_FORMAT_BGRX8888_UNORM,
138 PIPE_FORMAT_BGRA8888_SRGB,
139 PIPE_FORMAT_BGRX8888_SRGB,
140 PIPE_FORMAT_B5G6R5_UNORM,
141 PIPE_FORMAT_RGBA8888_UNORM,
142 PIPE_FORMAT_RGBX8888_UNORM,
143 };
144 mesa_format format;
145 __DRIconfig **configs = NULL;
146 uint8_t depth_bits_array[5];
147 uint8_t stencil_bits_array[5];
148 unsigned depth_buffer_factor;
149 unsigned msaa_samples_max;
150 unsigned i;
151 struct pipe_screen *p_screen = screen->base.screen;
152 boolean pf_z16, pf_x8z24, pf_z24x8, pf_s8z24, pf_z24s8, pf_z32;
153 boolean mixed_color_depth;
154
155 static const GLenum back_buffer_modes[] = {
156 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
157 };
158
159 if (driQueryOptionb(&screen->optionCache, "always_have_depth_buffer")) {
160 /* all visuals will have a depth buffer */
161 depth_buffer_factor = 0;
162 }
163 else {
164 depth_bits_array[0] = 0;
165 stencil_bits_array[0] = 0;
166 depth_buffer_factor = 1;
167 }
168
169 msaa_samples_max = (screen->st_api->feature_mask & ST_API_FEATURE_MS_VISUALS_MASK)
170 ? MSAA_VISUAL_MAX_SAMPLES : 1;
171
172 pf_x8z24 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24X8_UNORM,
173 PIPE_TEXTURE_2D, 0,
174 PIPE_BIND_DEPTH_STENCIL);
175 pf_z24x8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_X8Z24_UNORM,
176 PIPE_TEXTURE_2D, 0,
177 PIPE_BIND_DEPTH_STENCIL);
178 pf_s8z24 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24_UNORM_S8_UINT,
179 PIPE_TEXTURE_2D, 0,
180 PIPE_BIND_DEPTH_STENCIL);
181 pf_z24s8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_S8_UINT_Z24_UNORM,
182 PIPE_TEXTURE_2D, 0,
183 PIPE_BIND_DEPTH_STENCIL);
184 pf_z16 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z16_UNORM,
185 PIPE_TEXTURE_2D, 0,
186 PIPE_BIND_DEPTH_STENCIL);
187 pf_z32 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z32_UNORM,
188 PIPE_TEXTURE_2D, 0,
189 PIPE_BIND_DEPTH_STENCIL);
190
191 if (pf_z16) {
192 depth_bits_array[depth_buffer_factor] = 16;
193 stencil_bits_array[depth_buffer_factor++] = 0;
194 }
195 if (pf_x8z24 || pf_z24x8) {
196 depth_bits_array[depth_buffer_factor] = 24;
197 stencil_bits_array[depth_buffer_factor++] = 0;
198 screen->d_depth_bits_last = pf_x8z24;
199 }
200 if (pf_s8z24 || pf_z24s8) {
201 depth_bits_array[depth_buffer_factor] = 24;
202 stencil_bits_array[depth_buffer_factor++] = 8;
203 screen->sd_depth_bits_last = pf_s8z24;
204 }
205 if (pf_z32) {
206 depth_bits_array[depth_buffer_factor] = 32;
207 stencil_bits_array[depth_buffer_factor++] = 0;
208 }
209
210 mixed_color_depth =
211 p_screen->get_param(p_screen, PIPE_CAP_MIXED_COLOR_DEPTH_BITS);
212
213 assert(ARRAY_SIZE(mesa_formats) == ARRAY_SIZE(pipe_formats));
214
215 /* Expose only BGRA ordering if the loader doesn't support RGBA ordering. */
216 unsigned num_formats;
217 if (dri_loader_get_cap(screen, DRI_LOADER_CAP_RGBA_ORDERING))
218 num_formats = ARRAY_SIZE(mesa_formats);
219 else
220 num_formats = 5;
221
222 /* Add configs. */
223 for (format = 0; format < num_formats; format++) {
224 __DRIconfig **new_configs = NULL;
225 unsigned num_msaa_modes = 0; /* includes a single-sample mode */
226 uint8_t msaa_modes[MSAA_VISUAL_MAX_SAMPLES];
227
228 if (!p_screen->is_format_supported(p_screen, pipe_formats[format],
229 PIPE_TEXTURE_2D, 0,
230 PIPE_BIND_RENDER_TARGET))
231 continue;
232
233 for (i = 1; i <= msaa_samples_max; i++) {
234 int samples = i > 1 ? i : 0;
235
236 if (p_screen->is_format_supported(p_screen, pipe_formats[format],
237 PIPE_TEXTURE_2D, samples,
238 PIPE_BIND_RENDER_TARGET)) {
239 msaa_modes[num_msaa_modes++] = samples;
240 }
241 }
242
243 if (num_msaa_modes) {
244 /* Single-sample configs with an accumulation buffer. */
245 new_configs = driCreateConfigs(mesa_formats[format],
246 depth_bits_array, stencil_bits_array,
247 depth_buffer_factor, back_buffer_modes,
248 ARRAY_SIZE(back_buffer_modes),
249 msaa_modes, 1,
250 GL_TRUE, !mixed_color_depth);
251 configs = driConcatConfigs(configs, new_configs);
252
253 /* Multi-sample configs without an accumulation buffer. */
254 if (num_msaa_modes > 1) {
255 new_configs = driCreateConfigs(mesa_formats[format],
256 depth_bits_array, stencil_bits_array,
257 depth_buffer_factor, back_buffer_modes,
258 ARRAY_SIZE(back_buffer_modes),
259 msaa_modes+1, num_msaa_modes-1,
260 GL_FALSE, !mixed_color_depth);
261 configs = driConcatConfigs(configs, new_configs);
262 }
263 }
264 }
265
266 if (configs == NULL) {
267 debug_printf("%s: driCreateConfigs failed\n", __FUNCTION__);
268 return NULL;
269 }
270
271 return (const __DRIconfig **)configs;
272 }
273
274 /**
275 * Roughly the converse of dri_fill_in_modes.
276 */
277 void
278 dri_fill_st_visual(struct st_visual *stvis, struct dri_screen *screen,
279 const struct gl_config *mode)
280 {
281 memset(stvis, 0, sizeof(*stvis));
282
283 if (!mode)
284 return;
285
286 /* Deduce the color format. */
287 switch (mode->redMask) {
288 case 0x00FF0000:
289 if (mode->alphaMask) {
290 assert(mode->alphaMask == 0xFF000000);
291 stvis->color_format = mode->sRGBCapable ?
292 PIPE_FORMAT_BGRA8888_SRGB :
293 PIPE_FORMAT_BGRA8888_UNORM;
294 } else {
295 stvis->color_format = mode->sRGBCapable ?
296 PIPE_FORMAT_BGRX8888_SRGB :
297 PIPE_FORMAT_BGRX8888_UNORM;
298 }
299 break;
300
301 case 0x000000FF:
302 if (mode->alphaMask) {
303 assert(mode->alphaMask == 0xFF000000);
304 stvis->color_format = mode->sRGBCapable ?
305 PIPE_FORMAT_RGBA8888_SRGB :
306 PIPE_FORMAT_RGBA8888_UNORM;
307 } else {
308 stvis->color_format = mode->sRGBCapable ?
309 PIPE_FORMAT_RGBX8888_SRGB :
310 PIPE_FORMAT_RGBX8888_UNORM;
311 }
312 break;
313
314 case 0x0000F800:
315 stvis->color_format = PIPE_FORMAT_B5G6R5_UNORM;
316 break;
317
318 default:
319 assert(!"unsupported visual: invalid red mask");
320 return;
321 }
322
323 if (mode->sampleBuffers) {
324 stvis->samples = mode->samples;
325 }
326
327 switch (mode->depthBits) {
328 default:
329 case 0:
330 stvis->depth_stencil_format = PIPE_FORMAT_NONE;
331 break;
332 case 16:
333 stvis->depth_stencil_format = PIPE_FORMAT_Z16_UNORM;
334 break;
335 case 24:
336 if (mode->stencilBits == 0) {
337 stvis->depth_stencil_format = (screen->d_depth_bits_last) ?
338 PIPE_FORMAT_Z24X8_UNORM:
339 PIPE_FORMAT_X8Z24_UNORM;
340 } else {
341 stvis->depth_stencil_format = (screen->sd_depth_bits_last) ?
342 PIPE_FORMAT_Z24_UNORM_S8_UINT:
343 PIPE_FORMAT_S8_UINT_Z24_UNORM;
344 }
345 break;
346 case 32:
347 stvis->depth_stencil_format = PIPE_FORMAT_Z32_UNORM;
348 break;
349 }
350
351 stvis->accum_format = (mode->haveAccumBuffer) ?
352 PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
353
354 stvis->buffer_mask |= ST_ATTACHMENT_FRONT_LEFT_MASK;
355 stvis->render_buffer = ST_ATTACHMENT_FRONT_LEFT;
356 if (mode->doubleBufferMode) {
357 stvis->buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
358 stvis->render_buffer = ST_ATTACHMENT_BACK_LEFT;
359 }
360 if (mode->stereoMode) {
361 stvis->buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
362 if (mode->doubleBufferMode)
363 stvis->buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
364 }
365
366 if (mode->haveDepthBuffer || mode->haveStencilBuffer)
367 stvis->buffer_mask |= ST_ATTACHMENT_DEPTH_STENCIL_MASK;
368 /* let the state tracker allocate the accum buffer */
369 }
370
371 static boolean
372 dri_get_egl_image(struct st_manager *smapi,
373 void *egl_image,
374 struct st_egl_image *stimg)
375 {
376 struct dri_screen *screen = (struct dri_screen *)smapi;
377 __DRIimage *img = NULL;
378
379 if (screen->lookup_egl_image) {
380 img = screen->lookup_egl_image(screen, egl_image);
381 }
382
383 if (!img)
384 return FALSE;
385
386 stimg->texture = NULL;
387 pipe_resource_reference(&stimg->texture, img->texture);
388 switch (img->dri_components) {
389 case __DRI_IMAGE_COMPONENTS_Y_U_V:
390 stimg->format = PIPE_FORMAT_IYUV;
391 break;
392 case __DRI_IMAGE_COMPONENTS_Y_UV:
393 stimg->format = PIPE_FORMAT_NV12;
394 break;
395 default:
396 stimg->format = img->texture->format;
397 break;
398 }
399 stimg->level = img->level;
400 stimg->layer = img->layer;
401
402 return TRUE;
403 }
404
405 static int
406 dri_get_param(struct st_manager *smapi,
407 enum st_manager_param param)
408 {
409 struct dri_screen *screen = (struct dri_screen *)smapi;
410
411 switch(param) {
412 case ST_MANAGER_BROKEN_INVALIDATE:
413 return screen->broken_invalidate;
414 default:
415 return 0;
416 }
417 }
418
419 static void
420 dri_destroy_option_cache(struct dri_screen * screen)
421 {
422 int i;
423
424 if (screen->optionCache.info) {
425 for (i = 0; i < (1 << screen->optionCache.tableSize); ++i) {
426 free(screen->optionCache.info[i].name);
427 free(screen->optionCache.info[i].ranges);
428 }
429 free(screen->optionCache.info);
430 }
431
432 free(screen->optionCache.values);
433
434 /* Default values are copied to screen->optionCache->values in
435 * initOptionCache. The info field, however, is a pointer copy, so don't free
436 * that twice.
437 */
438 free(screen->optionCacheDefaults.values);
439 }
440
441 void
442 dri_destroy_screen_helper(struct dri_screen * screen)
443 {
444 if (screen->base.destroy)
445 screen->base.destroy(&screen->base);
446
447 if (screen->st_api && screen->st_api->destroy)
448 screen->st_api->destroy(screen->st_api);
449
450 if (screen->base.screen)
451 screen->base.screen->destroy(screen->base.screen);
452
453 dri_destroy_option_cache(screen);
454 mtx_destroy(&screen->opencl_func_mutex);
455 }
456
457 void
458 dri_destroy_screen(__DRIscreen * sPriv)
459 {
460 struct dri_screen *screen = dri_screen(sPriv);
461
462 dri_destroy_screen_helper(screen);
463
464 pipe_loader_release(&screen->dev, 1);
465
466 free(screen);
467 sPriv->driverPrivate = NULL;
468 sPriv->extensions = NULL;
469 }
470
471 static void
472 dri_postprocessing_init(struct dri_screen *screen)
473 {
474 unsigned i;
475
476 for (i = 0; i < PP_FILTERS; i++) {
477 screen->pp_enabled[i] = driQueryOptioni(&screen->optionCache,
478 pp_filters[i].name);
479 }
480 }
481
482 static void
483 dri_set_background_context(struct st_context_iface *st,
484 struct util_queue_monitoring *queue_info)
485 {
486 struct dri_context *ctx = (struct dri_context *)st->st_manager_private;
487 const __DRIbackgroundCallableExtension *backgroundCallable =
488 ctx->sPriv->dri2.backgroundCallable;
489
490 /* Note: Mesa will only call this function if GL multithreading is enabled
491 * We only do that if the loader exposed the __DRI_BACKGROUND_CALLABLE
492 * extension. So we know that backgroundCallable is not NULL.
493 */
494 assert(backgroundCallable);
495 backgroundCallable->setBackgroundContext(ctx->cPriv->loaderPrivate);
496
497 if (ctx->hud)
498 hud_add_queue_for_monitoring(ctx->hud, queue_info);
499 }
500
501 unsigned
502 dri_init_options_get_screen_flags(struct dri_screen *screen,
503 const char* driver_name)
504 {
505 unsigned flags = 0;
506
507 driParseOptionInfo(&screen->optionCacheDefaults, gallium_config_options.xml);
508 driParseConfigFiles(&screen->optionCache,
509 &screen->optionCacheDefaults,
510 screen->sPriv->myNum,
511 driver_name);
512 dri_fill_st_options(screen);
513
514 if (driQueryOptionb(&screen->optionCache,
515 "glsl_correct_derivatives_after_discard"))
516 flags |= PIPE_SCREEN_ENABLE_CORRECT_TGSI_DERIVATIVES_AFTER_KILL;
517
518 return flags;
519 }
520
521 const __DRIconfig **
522 dri_init_screen_helper(struct dri_screen *screen,
523 struct pipe_screen *pscreen)
524 {
525 screen->base.screen = pscreen;
526 screen->base.get_egl_image = dri_get_egl_image;
527 screen->base.get_param = dri_get_param;
528 screen->base.set_background_context = dri_set_background_context;
529
530 screen->st_api = st_gl_api_create();
531 if (!screen->st_api)
532 return NULL;
533
534 if(pscreen->get_param(pscreen, PIPE_CAP_NPOT_TEXTURES))
535 screen->target = PIPE_TEXTURE_2D;
536 else
537 screen->target = PIPE_TEXTURE_RECT;
538
539 /* Handle force_s3tc_enable. */
540 if (!util_format_s3tc_enabled && screen->options.force_s3tc_enable) {
541 /* Ensure libtxc_dxtn has been loaded if available.
542 * Forcing S3TC on before calling this would prevent loading
543 * the library.
544 * This is just a precaution, the driver should have called it
545 * already.
546 */
547 util_format_s3tc_init();
548
549 util_format_s3tc_enabled = TRUE;
550 }
551
552 dri_postprocessing_init(screen);
553
554 screen->st_api->query_versions(screen->st_api, &screen->base,
555 &screen->options,
556 &screen->sPriv->max_gl_core_version,
557 &screen->sPriv->max_gl_compat_version,
558 &screen->sPriv->max_gl_es1_version,
559 &screen->sPriv->max_gl_es2_version);
560
561 return dri_fill_in_modes(screen);
562 }
563
564 /* vim: set sw=3 ts=8 sts=3 expandtab: */