gallium: get rid of pipe_screen_config::flags
[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, 2 },
53 .xml = gallium_driinfo_xml,
54 .getXml = pipe_loader_get_driinfo_xml
55 };
56
57 #define false 0
58
59 static void
60 dri_fill_st_options(struct dri_screen *screen)
61 {
62 struct st_config_options *options = &screen->options;
63 const struct driOptionCache *optionCache = &screen->dev->option_cache;
64
65 options->disable_blend_func_extended =
66 driQueryOptionb(optionCache, "disable_blend_func_extended");
67 options->disable_glsl_line_continuations =
68 driQueryOptionb(optionCache, "disable_glsl_line_continuations");
69 options->disable_shader_bit_encoding =
70 driQueryOptionb(optionCache, "disable_shader_bit_encoding");
71 options->force_glsl_extensions_warn =
72 driQueryOptionb(optionCache, "force_glsl_extensions_warn");
73 options->force_glsl_version =
74 driQueryOptioni(optionCache, "force_glsl_version");
75 options->force_s3tc_enable =
76 driQueryOptionb(optionCache, "force_s3tc_enable");
77 options->allow_glsl_extension_directive_midshader =
78 driQueryOptionb(optionCache, "allow_glsl_extension_directive_midshader");
79 options->allow_glsl_builtin_variable_redeclaration =
80 driQueryOptionb(optionCache, "allow_glsl_builtin_variable_redeclaration");
81 options->allow_higher_compat_version =
82 driQueryOptionb(optionCache, "allow_higher_compat_version");
83 options->glsl_zero_init = driQueryOptionb(optionCache, "glsl_zero_init");
84 options->force_glsl_abs_sqrt =
85 driQueryOptionb(optionCache, "force_glsl_abs_sqrt");
86
87 driComputeOptionsSha1(optionCache, options->config_options_sha1);
88 }
89
90 static unsigned
91 dri_loader_get_cap(struct dri_screen *screen, enum dri_loader_cap cap)
92 {
93 const __DRIdri2LoaderExtension *dri2_loader = screen->sPriv->dri2.loader;
94 const __DRIimageLoaderExtension *image_loader = screen->sPriv->image.loader;
95
96 if (dri2_loader && dri2_loader->base.version >= 4 &&
97 dri2_loader->getCapability)
98 return dri2_loader->getCapability(screen->sPriv->loaderPrivate, cap);
99
100 if (image_loader && image_loader->base.version >= 2 &&
101 image_loader->getCapability)
102 return image_loader->getCapability(screen->sPriv->loaderPrivate, cap);
103
104 return 0;
105 }
106
107 static const __DRIconfig **
108 dri_fill_in_modes(struct dri_screen *screen)
109 {
110 static const mesa_format mesa_formats[] = {
111 MESA_FORMAT_B8G8R8A8_UNORM,
112 MESA_FORMAT_B8G8R8X8_UNORM,
113 MESA_FORMAT_B8G8R8A8_SRGB,
114 MESA_FORMAT_B8G8R8X8_SRGB,
115 MESA_FORMAT_B5G6R5_UNORM,
116
117 /* The 32-bit RGBA format must not precede the 32-bit BGRA format.
118 * Likewise for RGBX and BGRX. Otherwise, the GLX client and the GLX
119 * server may disagree on which format the GLXFBConfig represents,
120 * resulting in swapped color channels.
121 *
122 * The problem, as of 2017-05-30:
123 * When matching a GLXFBConfig to a __DRIconfig, GLX ignores the channel
124 * order and chooses the first __DRIconfig with the expected channel
125 * sizes. Specifically, GLX compares the GLXFBConfig's and __DRIconfig's
126 * __DRI_ATTRIB_{CHANNEL}_SIZE but ignores __DRI_ATTRIB_{CHANNEL}_MASK.
127 *
128 * EGL does not suffer from this problem. It correctly compares the
129 * channel masks when matching EGLConfig to __DRIconfig.
130 */
131
132 /* Required by Android, for HAL_PIXEL_FORMAT_RGBA_8888. */
133 MESA_FORMAT_R8G8B8A8_UNORM,
134
135 /* Required by Android, for HAL_PIXEL_FORMAT_RGBX_8888. */
136 MESA_FORMAT_R8G8B8X8_UNORM,
137 };
138 static const enum pipe_format pipe_formats[] = {
139 PIPE_FORMAT_BGRA8888_UNORM,
140 PIPE_FORMAT_BGRX8888_UNORM,
141 PIPE_FORMAT_BGRA8888_SRGB,
142 PIPE_FORMAT_BGRX8888_SRGB,
143 PIPE_FORMAT_B5G6R5_UNORM,
144 PIPE_FORMAT_RGBA8888_UNORM,
145 PIPE_FORMAT_RGBX8888_UNORM,
146 };
147 mesa_format format;
148 __DRIconfig **configs = NULL;
149 uint8_t depth_bits_array[5];
150 uint8_t stencil_bits_array[5];
151 unsigned depth_buffer_factor;
152 unsigned msaa_samples_max;
153 unsigned i;
154 struct pipe_screen *p_screen = screen->base.screen;
155 boolean pf_z16, pf_x8z24, pf_z24x8, pf_s8z24, pf_z24s8, pf_z32;
156 boolean mixed_color_depth;
157
158 static const GLenum back_buffer_modes[] = {
159 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
160 };
161
162 if (driQueryOptionb(&screen->dev->option_cache, "always_have_depth_buffer")) {
163 /* all visuals will have a depth buffer */
164 depth_buffer_factor = 0;
165 }
166 else {
167 depth_bits_array[0] = 0;
168 stencil_bits_array[0] = 0;
169 depth_buffer_factor = 1;
170 }
171
172 msaa_samples_max = (screen->st_api->feature_mask & ST_API_FEATURE_MS_VISUALS_MASK)
173 ? MSAA_VISUAL_MAX_SAMPLES : 1;
174
175 pf_x8z24 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24X8_UNORM,
176 PIPE_TEXTURE_2D, 0,
177 PIPE_BIND_DEPTH_STENCIL);
178 pf_z24x8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_X8Z24_UNORM,
179 PIPE_TEXTURE_2D, 0,
180 PIPE_BIND_DEPTH_STENCIL);
181 pf_s8z24 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24_UNORM_S8_UINT,
182 PIPE_TEXTURE_2D, 0,
183 PIPE_BIND_DEPTH_STENCIL);
184 pf_z24s8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_S8_UINT_Z24_UNORM,
185 PIPE_TEXTURE_2D, 0,
186 PIPE_BIND_DEPTH_STENCIL);
187 pf_z16 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z16_UNORM,
188 PIPE_TEXTURE_2D, 0,
189 PIPE_BIND_DEPTH_STENCIL);
190 pf_z32 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z32_UNORM,
191 PIPE_TEXTURE_2D, 0,
192 PIPE_BIND_DEPTH_STENCIL);
193
194 if (pf_z16) {
195 depth_bits_array[depth_buffer_factor] = 16;
196 stencil_bits_array[depth_buffer_factor++] = 0;
197 }
198 if (pf_x8z24 || pf_z24x8) {
199 depth_bits_array[depth_buffer_factor] = 24;
200 stencil_bits_array[depth_buffer_factor++] = 0;
201 screen->d_depth_bits_last = pf_x8z24;
202 }
203 if (pf_s8z24 || pf_z24s8) {
204 depth_bits_array[depth_buffer_factor] = 24;
205 stencil_bits_array[depth_buffer_factor++] = 8;
206 screen->sd_depth_bits_last = pf_s8z24;
207 }
208 if (pf_z32) {
209 depth_bits_array[depth_buffer_factor] = 32;
210 stencil_bits_array[depth_buffer_factor++] = 0;
211 }
212
213 mixed_color_depth =
214 p_screen->get_param(p_screen, PIPE_CAP_MIXED_COLOR_DEPTH_BITS);
215
216 assert(ARRAY_SIZE(mesa_formats) == ARRAY_SIZE(pipe_formats));
217
218 /* Expose only BGRA ordering if the loader doesn't support RGBA ordering. */
219 unsigned num_formats;
220 if (dri_loader_get_cap(screen, DRI_LOADER_CAP_RGBA_ORDERING))
221 num_formats = ARRAY_SIZE(mesa_formats);
222 else
223 num_formats = 5;
224
225 /* Add configs. */
226 for (format = 0; format < num_formats; format++) {
227 __DRIconfig **new_configs = NULL;
228 unsigned num_msaa_modes = 0; /* includes a single-sample mode */
229 uint8_t msaa_modes[MSAA_VISUAL_MAX_SAMPLES];
230
231 if (!p_screen->is_format_supported(p_screen, pipe_formats[format],
232 PIPE_TEXTURE_2D, 0,
233 PIPE_BIND_RENDER_TARGET))
234 continue;
235
236 for (i = 1; i <= msaa_samples_max; i++) {
237 int samples = i > 1 ? i : 0;
238
239 if (p_screen->is_format_supported(p_screen, pipe_formats[format],
240 PIPE_TEXTURE_2D, samples,
241 PIPE_BIND_RENDER_TARGET)) {
242 msaa_modes[num_msaa_modes++] = samples;
243 }
244 }
245
246 if (num_msaa_modes) {
247 /* Single-sample configs with an accumulation buffer. */
248 new_configs = driCreateConfigs(mesa_formats[format],
249 depth_bits_array, stencil_bits_array,
250 depth_buffer_factor, back_buffer_modes,
251 ARRAY_SIZE(back_buffer_modes),
252 msaa_modes, 1,
253 GL_TRUE, !mixed_color_depth);
254 configs = driConcatConfigs(configs, new_configs);
255
256 /* Multi-sample configs without an accumulation buffer. */
257 if (num_msaa_modes > 1) {
258 new_configs = driCreateConfigs(mesa_formats[format],
259 depth_bits_array, stencil_bits_array,
260 depth_buffer_factor, back_buffer_modes,
261 ARRAY_SIZE(back_buffer_modes),
262 msaa_modes+1, num_msaa_modes-1,
263 GL_FALSE, !mixed_color_depth);
264 configs = driConcatConfigs(configs, new_configs);
265 }
266 }
267 }
268
269 if (configs == NULL) {
270 debug_printf("%s: driCreateConfigs failed\n", __FUNCTION__);
271 return NULL;
272 }
273
274 return (const __DRIconfig **)configs;
275 }
276
277 /**
278 * Roughly the converse of dri_fill_in_modes.
279 */
280 void
281 dri_fill_st_visual(struct st_visual *stvis, struct dri_screen *screen,
282 const struct gl_config *mode)
283 {
284 memset(stvis, 0, sizeof(*stvis));
285
286 if (!mode)
287 return;
288
289 /* Deduce the color format. */
290 switch (mode->redMask) {
291 case 0x00FF0000:
292 if (mode->alphaMask) {
293 assert(mode->alphaMask == 0xFF000000);
294 stvis->color_format = mode->sRGBCapable ?
295 PIPE_FORMAT_BGRA8888_SRGB :
296 PIPE_FORMAT_BGRA8888_UNORM;
297 } else {
298 stvis->color_format = mode->sRGBCapable ?
299 PIPE_FORMAT_BGRX8888_SRGB :
300 PIPE_FORMAT_BGRX8888_UNORM;
301 }
302 break;
303
304 case 0x000000FF:
305 if (mode->alphaMask) {
306 assert(mode->alphaMask == 0xFF000000);
307 stvis->color_format = mode->sRGBCapable ?
308 PIPE_FORMAT_RGBA8888_SRGB :
309 PIPE_FORMAT_RGBA8888_UNORM;
310 } else {
311 stvis->color_format = mode->sRGBCapable ?
312 PIPE_FORMAT_RGBX8888_SRGB :
313 PIPE_FORMAT_RGBX8888_UNORM;
314 }
315 break;
316
317 case 0x0000F800:
318 stvis->color_format = PIPE_FORMAT_B5G6R5_UNORM;
319 break;
320
321 default:
322 assert(!"unsupported visual: invalid red mask");
323 return;
324 }
325
326 if (mode->sampleBuffers) {
327 stvis->samples = mode->samples;
328 }
329
330 switch (mode->depthBits) {
331 default:
332 case 0:
333 stvis->depth_stencil_format = PIPE_FORMAT_NONE;
334 break;
335 case 16:
336 stvis->depth_stencil_format = PIPE_FORMAT_Z16_UNORM;
337 break;
338 case 24:
339 if (mode->stencilBits == 0) {
340 stvis->depth_stencil_format = (screen->d_depth_bits_last) ?
341 PIPE_FORMAT_Z24X8_UNORM:
342 PIPE_FORMAT_X8Z24_UNORM;
343 } else {
344 stvis->depth_stencil_format = (screen->sd_depth_bits_last) ?
345 PIPE_FORMAT_Z24_UNORM_S8_UINT:
346 PIPE_FORMAT_S8_UINT_Z24_UNORM;
347 }
348 break;
349 case 32:
350 stvis->depth_stencil_format = PIPE_FORMAT_Z32_UNORM;
351 break;
352 }
353
354 stvis->accum_format = (mode->haveAccumBuffer) ?
355 PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
356
357 stvis->buffer_mask |= ST_ATTACHMENT_FRONT_LEFT_MASK;
358 stvis->render_buffer = ST_ATTACHMENT_FRONT_LEFT;
359 if (mode->doubleBufferMode) {
360 stvis->buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
361 stvis->render_buffer = ST_ATTACHMENT_BACK_LEFT;
362 }
363 if (mode->stereoMode) {
364 stvis->buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
365 if (mode->doubleBufferMode)
366 stvis->buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
367 }
368
369 if (mode->haveDepthBuffer || mode->haveStencilBuffer)
370 stvis->buffer_mask |= ST_ATTACHMENT_DEPTH_STENCIL_MASK;
371 /* let the state tracker allocate the accum buffer */
372 }
373
374 static boolean
375 dri_get_egl_image(struct st_manager *smapi,
376 void *egl_image,
377 struct st_egl_image *stimg)
378 {
379 struct dri_screen *screen = (struct dri_screen *)smapi;
380 __DRIimage *img = NULL;
381
382 if (screen->lookup_egl_image) {
383 img = screen->lookup_egl_image(screen, egl_image);
384 }
385
386 if (!img)
387 return FALSE;
388
389 stimg->texture = NULL;
390 pipe_resource_reference(&stimg->texture, img->texture);
391 switch (img->dri_components) {
392 case __DRI_IMAGE_COMPONENTS_Y_U_V:
393 stimg->format = PIPE_FORMAT_IYUV;
394 break;
395 case __DRI_IMAGE_COMPONENTS_Y_UV:
396 stimg->format = PIPE_FORMAT_NV12;
397 break;
398 default:
399 stimg->format = img->texture->format;
400 break;
401 }
402 stimg->level = img->level;
403 stimg->layer = img->layer;
404
405 return TRUE;
406 }
407
408 static int
409 dri_get_param(struct st_manager *smapi,
410 enum st_manager_param param)
411 {
412 struct dri_screen *screen = (struct dri_screen *)smapi;
413
414 switch(param) {
415 case ST_MANAGER_BROKEN_INVALIDATE:
416 return screen->broken_invalidate;
417 default:
418 return 0;
419 }
420 }
421
422 void
423 dri_destroy_screen_helper(struct dri_screen * screen)
424 {
425 if (screen->base.destroy)
426 screen->base.destroy(&screen->base);
427
428 if (screen->st_api && screen->st_api->destroy)
429 screen->st_api->destroy(screen->st_api);
430
431 if (screen->base.screen)
432 screen->base.screen->destroy(screen->base.screen);
433
434 mtx_destroy(&screen->opencl_func_mutex);
435 }
436
437 void
438 dri_destroy_screen(__DRIscreen * sPriv)
439 {
440 struct dri_screen *screen = dri_screen(sPriv);
441
442 dri_destroy_screen_helper(screen);
443
444 pipe_loader_release(&screen->dev, 1);
445
446 free(screen);
447 sPriv->driverPrivate = NULL;
448 sPriv->extensions = NULL;
449 }
450
451 static void
452 dri_postprocessing_init(struct dri_screen *screen)
453 {
454 unsigned i;
455
456 for (i = 0; i < PP_FILTERS; i++) {
457 screen->pp_enabled[i] = driQueryOptioni(&screen->dev->option_cache,
458 pp_filters[i].name);
459 }
460 }
461
462 static void
463 dri_set_background_context(struct st_context_iface *st,
464 struct util_queue_monitoring *queue_info)
465 {
466 struct dri_context *ctx = (struct dri_context *)st->st_manager_private;
467 const __DRIbackgroundCallableExtension *backgroundCallable =
468 ctx->sPriv->dri2.backgroundCallable;
469
470 /* Note: Mesa will only call this function if GL multithreading is enabled
471 * We only do that if the loader exposed the __DRI_BACKGROUND_CALLABLE
472 * extension. So we know that backgroundCallable is not NULL.
473 */
474 assert(backgroundCallable);
475 backgroundCallable->setBackgroundContext(ctx->cPriv->loaderPrivate);
476
477 if (ctx->hud)
478 hud_add_queue_for_monitoring(ctx->hud, queue_info);
479 }
480
481 void
482 dri_init_options(struct dri_screen *screen)
483 {
484 pipe_loader_load_options(screen->dev);
485
486 dri_fill_st_options(screen);
487 }
488
489 const __DRIconfig **
490 dri_init_screen_helper(struct dri_screen *screen,
491 struct pipe_screen *pscreen)
492 {
493 screen->base.screen = pscreen;
494 screen->base.get_egl_image = dri_get_egl_image;
495 screen->base.get_param = dri_get_param;
496 screen->base.set_background_context = dri_set_background_context;
497
498 screen->st_api = st_gl_api_create();
499 if (!screen->st_api)
500 return NULL;
501
502 if(pscreen->get_param(pscreen, PIPE_CAP_NPOT_TEXTURES))
503 screen->target = PIPE_TEXTURE_2D;
504 else
505 screen->target = PIPE_TEXTURE_RECT;
506
507 /* Handle force_s3tc_enable. */
508 if (!util_format_s3tc_enabled && screen->options.force_s3tc_enable) {
509 /* Ensure libtxc_dxtn has been loaded if available.
510 * Forcing S3TC on before calling this would prevent loading
511 * the library.
512 * This is just a precaution, the driver should have called it
513 * already.
514 */
515 util_format_s3tc_init();
516
517 util_format_s3tc_enabled = TRUE;
518 }
519
520 dri_postprocessing_init(screen);
521
522 screen->st_api->query_versions(screen->st_api, &screen->base,
523 &screen->options,
524 &screen->sPriv->max_gl_core_version,
525 &screen->sPriv->max_gl_compat_version,
526 &screen->sPriv->max_gl_es1_version,
527 &screen->sPriv->max_gl_es2_version);
528
529 return dri_fill_in_modes(screen);
530 }
531
532 /* vim: set sw=3 ts=8 sts=3 expandtab: */