1 /**************************************************************************
3 * Copyright 2009, VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 * Author: Keith Whitwell <keithw@vmware.com>
29 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
33 #include "util/xmlpool.h"
35 #include "dri_screen.h"
36 #include "dri_context.h"
38 #include "util/u_inlines.h"
39 #include "pipe/p_screen.h"
40 #include "pipe/p_format.h"
41 #include "pipe-loader/pipe_loader.h"
42 #include "state_tracker/st_gl_api.h" /* for st_gl_api_create */
43 #include "state_tracker/drm_driver.h"
45 #include "util/u_debug.h"
46 #include "util/u_format_s3tc.h"
48 #define MSAA_VISUAL_MAX_SAMPLES 32
52 const __DRIconfigOptionsExtension gallium_config_options
= {
53 .base
= { __DRI_CONFIG_OPTIONS
, 1 },
57 DRI_CONF_SECTION_PERFORMANCE
58 DRI_CONF_MESA_GLTHREAD("false")
59 DRI_CONF_MESA_NO_ERROR("false")
60 DRI_CONF_DISABLE_EXT_BUFFER_AGE("false")
61 DRI_CONF_DISABLE_OML_SYNC_CONTROL("false")
64 DRI_CONF_SECTION_QUALITY
65 DRI_CONF_FORCE_S3TC_ENABLE("false")
66 DRI_CONF_PP_CELSHADE(0)
68 DRI_CONF_PP_NOGREEN(0)
70 DRI_CONF_PP_JIMENEZMLAA(0, 0, 32)
71 DRI_CONF_PP_JIMENEZMLAA_COLOR(0, 0, 32)
74 DRI_CONF_SECTION_DEBUG
75 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
76 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
77 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
78 DRI_CONF_DISABLE_SHADER_BIT_ENCODING("false")
79 DRI_CONF_FORCE_GLSL_VERSION(0)
80 DRI_CONF_ALLOW_GLSL_EXTENSION_DIRECTIVE_MIDSHADER("false")
81 DRI_CONF_ALLOW_GLSL_BUILTIN_VARIABLE_REDECLARATION("false")
82 DRI_CONF_ALLOW_HIGHER_COMPAT_VERSION("false")
83 DRI_CONF_FORCE_GLSL_ABS_SQRT("false")
84 DRI_CONF_GLSL_CORRECT_DERIVATIVES_AFTER_DISCARD("false")
87 DRI_CONF_SECTION_MISCELLANEOUS
88 DRI_CONF_ALWAYS_HAVE_DEPTH_BUFFER("false")
89 DRI_CONF_GLSL_ZERO_INIT("false")
97 dri_fill_st_options(struct dri_screen
*screen
)
99 struct st_config_options
*options
= &screen
->options
;
100 const struct driOptionCache
*optionCache
= &screen
->optionCache
;
102 options
->disable_blend_func_extended
=
103 driQueryOptionb(optionCache
, "disable_blend_func_extended");
104 options
->disable_glsl_line_continuations
=
105 driQueryOptionb(optionCache
, "disable_glsl_line_continuations");
106 options
->disable_shader_bit_encoding
=
107 driQueryOptionb(optionCache
, "disable_shader_bit_encoding");
108 options
->force_glsl_extensions_warn
=
109 driQueryOptionb(optionCache
, "force_glsl_extensions_warn");
110 options
->force_glsl_version
=
111 driQueryOptioni(optionCache
, "force_glsl_version");
112 options
->force_s3tc_enable
=
113 driQueryOptionb(optionCache
, "force_s3tc_enable");
114 options
->allow_glsl_extension_directive_midshader
=
115 driQueryOptionb(optionCache
, "allow_glsl_extension_directive_midshader");
116 options
->allow_glsl_builtin_variable_redeclaration
=
117 driQueryOptionb(optionCache
, "allow_glsl_builtin_variable_redeclaration");
118 options
->allow_higher_compat_version
=
119 driQueryOptionb(optionCache
, "allow_higher_compat_version");
120 options
->glsl_zero_init
= driQueryOptionb(optionCache
, "glsl_zero_init");
121 options
->force_glsl_abs_sqrt
=
122 driQueryOptionb(optionCache
, "force_glsl_abs_sqrt");
124 driComputeOptionsSha1(optionCache
, options
->config_options_sha1
);
128 dri_loader_get_cap(struct dri_screen
*screen
, enum dri_loader_cap cap
)
130 const __DRIdri2LoaderExtension
*dri2_loader
= screen
->sPriv
->dri2
.loader
;
131 const __DRIimageLoaderExtension
*image_loader
= screen
->sPriv
->image
.loader
;
133 if (dri2_loader
&& dri2_loader
->base
.version
>= 4)
134 return dri2_loader
->getCapability(screen
->sPriv
->loaderPrivate
, cap
);
136 if (image_loader
&& image_loader
->base
.version
>= 2)
137 return image_loader
->getCapability(screen
->sPriv
->loaderPrivate
, cap
);
142 static const __DRIconfig
**
143 dri_fill_in_modes(struct dri_screen
*screen
)
145 static const mesa_format mesa_formats
[] = {
146 MESA_FORMAT_B8G8R8A8_UNORM
,
147 MESA_FORMAT_B8G8R8X8_UNORM
,
148 MESA_FORMAT_B8G8R8A8_SRGB
,
149 MESA_FORMAT_B8G8R8X8_SRGB
,
150 MESA_FORMAT_B5G6R5_UNORM
,
152 /* The 32-bit RGBA format must not precede the 32-bit BGRA format.
153 * Likewise for RGBX and BGRX. Otherwise, the GLX client and the GLX
154 * server may disagree on which format the GLXFBConfig represents,
155 * resulting in swapped color channels.
157 * The problem, as of 2017-05-30:
158 * When matching a GLXFBConfig to a __DRIconfig, GLX ignores the channel
159 * order and chooses the first __DRIconfig with the expected channel
160 * sizes. Specifically, GLX compares the GLXFBConfig's and __DRIconfig's
161 * __DRI_ATTRIB_{CHANNEL}_SIZE but ignores __DRI_ATTRIB_{CHANNEL}_MASK.
163 * EGL does not suffer from this problem. It correctly compares the
164 * channel masks when matching EGLConfig to __DRIconfig.
167 /* Required by Android, for HAL_PIXEL_FORMAT_RGBA_8888. */
168 MESA_FORMAT_R8G8B8A8_UNORM
,
170 /* Required by Android, for HAL_PIXEL_FORMAT_RGBX_8888. */
171 MESA_FORMAT_R8G8B8X8_UNORM
,
173 static const enum pipe_format pipe_formats
[] = {
174 PIPE_FORMAT_BGRA8888_UNORM
,
175 PIPE_FORMAT_BGRX8888_UNORM
,
176 PIPE_FORMAT_BGRA8888_SRGB
,
177 PIPE_FORMAT_BGRX8888_SRGB
,
178 PIPE_FORMAT_B5G6R5_UNORM
,
179 PIPE_FORMAT_RGBA8888_UNORM
,
180 PIPE_FORMAT_RGBX8888_UNORM
,
183 __DRIconfig
**configs
= NULL
;
184 uint8_t depth_bits_array
[5];
185 uint8_t stencil_bits_array
[5];
186 unsigned depth_buffer_factor
;
187 unsigned msaa_samples_max
;
189 struct pipe_screen
*p_screen
= screen
->base
.screen
;
190 boolean pf_z16
, pf_x8z24
, pf_z24x8
, pf_s8z24
, pf_z24s8
, pf_z32
;
191 boolean mixed_color_depth
;
193 static const GLenum back_buffer_modes
[] = {
194 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
, GLX_SWAP_COPY_OML
197 if (driQueryOptionb(&screen
->optionCache
, "always_have_depth_buffer")) {
198 /* all visuals will have a depth buffer */
199 depth_buffer_factor
= 0;
202 depth_bits_array
[0] = 0;
203 stencil_bits_array
[0] = 0;
204 depth_buffer_factor
= 1;
207 msaa_samples_max
= (screen
->st_api
->feature_mask
& ST_API_FEATURE_MS_VISUALS_MASK
)
208 ? MSAA_VISUAL_MAX_SAMPLES
: 1;
210 pf_x8z24
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z24X8_UNORM
,
212 PIPE_BIND_DEPTH_STENCIL
);
213 pf_z24x8
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_X8Z24_UNORM
,
215 PIPE_BIND_DEPTH_STENCIL
);
216 pf_s8z24
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z24_UNORM_S8_UINT
,
218 PIPE_BIND_DEPTH_STENCIL
);
219 pf_z24s8
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_S8_UINT_Z24_UNORM
,
221 PIPE_BIND_DEPTH_STENCIL
);
222 pf_z16
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z16_UNORM
,
224 PIPE_BIND_DEPTH_STENCIL
);
225 pf_z32
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z32_UNORM
,
227 PIPE_BIND_DEPTH_STENCIL
);
230 depth_bits_array
[depth_buffer_factor
] = 16;
231 stencil_bits_array
[depth_buffer_factor
++] = 0;
233 if (pf_x8z24
|| pf_z24x8
) {
234 depth_bits_array
[depth_buffer_factor
] = 24;
235 stencil_bits_array
[depth_buffer_factor
++] = 0;
236 screen
->d_depth_bits_last
= pf_x8z24
;
238 if (pf_s8z24
|| pf_z24s8
) {
239 depth_bits_array
[depth_buffer_factor
] = 24;
240 stencil_bits_array
[depth_buffer_factor
++] = 8;
241 screen
->sd_depth_bits_last
= pf_s8z24
;
244 depth_bits_array
[depth_buffer_factor
] = 32;
245 stencil_bits_array
[depth_buffer_factor
++] = 0;
249 p_screen
->get_param(p_screen
, PIPE_CAP_MIXED_COLOR_DEPTH_BITS
);
251 assert(ARRAY_SIZE(mesa_formats
) == ARRAY_SIZE(pipe_formats
));
253 /* Expose only BGRA ordering if the loader doesn't support RGBA ordering. */
254 unsigned num_formats
;
255 if (dri_loader_get_cap(screen
, DRI_LOADER_CAP_RGBA_ORDERING
))
256 num_formats
= ARRAY_SIZE(mesa_formats
);
261 for (format
= 0; format
< num_formats
; format
++) {
262 __DRIconfig
**new_configs
= NULL
;
263 unsigned num_msaa_modes
= 0; /* includes a single-sample mode */
264 uint8_t msaa_modes
[MSAA_VISUAL_MAX_SAMPLES
];
266 if (!p_screen
->is_format_supported(p_screen
, pipe_formats
[format
],
268 PIPE_BIND_RENDER_TARGET
))
271 for (i
= 1; i
<= msaa_samples_max
; i
++) {
272 int samples
= i
> 1 ? i
: 0;
274 if (p_screen
->is_format_supported(p_screen
, pipe_formats
[format
],
275 PIPE_TEXTURE_2D
, samples
,
276 PIPE_BIND_RENDER_TARGET
)) {
277 msaa_modes
[num_msaa_modes
++] = samples
;
281 if (num_msaa_modes
) {
282 /* Single-sample configs with an accumulation buffer. */
283 new_configs
= driCreateConfigs(mesa_formats
[format
],
284 depth_bits_array
, stencil_bits_array
,
285 depth_buffer_factor
, back_buffer_modes
,
286 ARRAY_SIZE(back_buffer_modes
),
288 GL_TRUE
, !mixed_color_depth
);
289 configs
= driConcatConfigs(configs
, new_configs
);
291 /* Multi-sample configs without an accumulation buffer. */
292 if (num_msaa_modes
> 1) {
293 new_configs
= driCreateConfigs(mesa_formats
[format
],
294 depth_bits_array
, stencil_bits_array
,
295 depth_buffer_factor
, back_buffer_modes
,
296 ARRAY_SIZE(back_buffer_modes
),
297 msaa_modes
+1, num_msaa_modes
-1,
298 GL_FALSE
, !mixed_color_depth
);
299 configs
= driConcatConfigs(configs
, new_configs
);
304 if (configs
== NULL
) {
305 debug_printf("%s: driCreateConfigs failed\n", __FUNCTION__
);
309 return (const __DRIconfig
**)configs
;
313 * Roughly the converse of dri_fill_in_modes.
316 dri_fill_st_visual(struct st_visual
*stvis
, struct dri_screen
*screen
,
317 const struct gl_config
*mode
)
319 memset(stvis
, 0, sizeof(*stvis
));
324 /* Deduce the color format. */
325 switch (mode
->redMask
) {
327 if (mode
->alphaMask
) {
328 assert(mode
->alphaMask
== 0xFF000000);
329 stvis
->color_format
= mode
->sRGBCapable
?
330 PIPE_FORMAT_BGRA8888_SRGB
:
331 PIPE_FORMAT_BGRA8888_UNORM
;
333 stvis
->color_format
= mode
->sRGBCapable
?
334 PIPE_FORMAT_BGRX8888_SRGB
:
335 PIPE_FORMAT_BGRX8888_UNORM
;
340 if (mode
->alphaMask
) {
341 assert(mode
->alphaMask
== 0xFF000000);
342 stvis
->color_format
= mode
->sRGBCapable
?
343 PIPE_FORMAT_RGBA8888_SRGB
:
344 PIPE_FORMAT_RGBA8888_UNORM
;
346 stvis
->color_format
= mode
->sRGBCapable
?
347 PIPE_FORMAT_RGBX8888_SRGB
:
348 PIPE_FORMAT_RGBX8888_UNORM
;
353 stvis
->color_format
= PIPE_FORMAT_B5G6R5_UNORM
;
357 assert(!"unsupported visual: invalid red mask");
361 if (mode
->sampleBuffers
) {
362 stvis
->samples
= mode
->samples
;
365 switch (mode
->depthBits
) {
368 stvis
->depth_stencil_format
= PIPE_FORMAT_NONE
;
371 stvis
->depth_stencil_format
= PIPE_FORMAT_Z16_UNORM
;
374 if (mode
->stencilBits
== 0) {
375 stvis
->depth_stencil_format
= (screen
->d_depth_bits_last
) ?
376 PIPE_FORMAT_Z24X8_UNORM
:
377 PIPE_FORMAT_X8Z24_UNORM
;
379 stvis
->depth_stencil_format
= (screen
->sd_depth_bits_last
) ?
380 PIPE_FORMAT_Z24_UNORM_S8_UINT
:
381 PIPE_FORMAT_S8_UINT_Z24_UNORM
;
385 stvis
->depth_stencil_format
= PIPE_FORMAT_Z32_UNORM
;
389 stvis
->accum_format
= (mode
->haveAccumBuffer
) ?
390 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
392 stvis
->buffer_mask
|= ST_ATTACHMENT_FRONT_LEFT_MASK
;
393 stvis
->render_buffer
= ST_ATTACHMENT_FRONT_LEFT
;
394 if (mode
->doubleBufferMode
) {
395 stvis
->buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
396 stvis
->render_buffer
= ST_ATTACHMENT_BACK_LEFT
;
398 if (mode
->stereoMode
) {
399 stvis
->buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
400 if (mode
->doubleBufferMode
)
401 stvis
->buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
404 if (mode
->haveDepthBuffer
|| mode
->haveStencilBuffer
)
405 stvis
->buffer_mask
|= ST_ATTACHMENT_DEPTH_STENCIL_MASK
;
406 /* let the state tracker allocate the accum buffer */
410 dri_get_egl_image(struct st_manager
*smapi
,
412 struct st_egl_image
*stimg
)
414 struct dri_screen
*screen
= (struct dri_screen
*)smapi
;
415 __DRIimage
*img
= NULL
;
417 if (screen
->lookup_egl_image
) {
418 img
= screen
->lookup_egl_image(screen
, egl_image
);
424 stimg
->texture
= NULL
;
425 pipe_resource_reference(&stimg
->texture
, img
->texture
);
426 switch (img
->dri_components
) {
427 case __DRI_IMAGE_COMPONENTS_Y_U_V
:
428 stimg
->format
= PIPE_FORMAT_IYUV
;
430 case __DRI_IMAGE_COMPONENTS_Y_UV
:
431 stimg
->format
= PIPE_FORMAT_NV12
;
434 stimg
->format
= img
->texture
->format
;
437 stimg
->level
= img
->level
;
438 stimg
->layer
= img
->layer
;
444 dri_get_param(struct st_manager
*smapi
,
445 enum st_manager_param param
)
447 struct dri_screen
*screen
= (struct dri_screen
*)smapi
;
450 case ST_MANAGER_BROKEN_INVALIDATE
:
451 return screen
->broken_invalidate
;
458 dri_destroy_option_cache(struct dri_screen
* screen
)
462 if (screen
->optionCache
.info
) {
463 for (i
= 0; i
< (1 << screen
->optionCache
.tableSize
); ++i
) {
464 free(screen
->optionCache
.info
[i
].name
);
465 free(screen
->optionCache
.info
[i
].ranges
);
467 free(screen
->optionCache
.info
);
470 free(screen
->optionCache
.values
);
472 /* Default values are copied to screen->optionCache->values in
473 * initOptionCache. The info field, however, is a pointer copy, so don't free
476 free(screen
->optionCacheDefaults
.values
);
480 dri_destroy_screen_helper(struct dri_screen
* screen
)
482 if (screen
->base
.destroy
)
483 screen
->base
.destroy(&screen
->base
);
485 if (screen
->st_api
&& screen
->st_api
->destroy
)
486 screen
->st_api
->destroy(screen
->st_api
);
488 if (screen
->base
.screen
)
489 screen
->base
.screen
->destroy(screen
->base
.screen
);
491 dri_destroy_option_cache(screen
);
492 mtx_destroy(&screen
->opencl_func_mutex
);
496 dri_destroy_screen(__DRIscreen
* sPriv
)
498 struct dri_screen
*screen
= dri_screen(sPriv
);
500 dri_destroy_screen_helper(screen
);
502 pipe_loader_release(&screen
->dev
, 1);
505 sPriv
->driverPrivate
= NULL
;
506 sPriv
->extensions
= NULL
;
510 dri_postprocessing_init(struct dri_screen
*screen
)
514 for (i
= 0; i
< PP_FILTERS
; i
++) {
515 screen
->pp_enabled
[i
] = driQueryOptioni(&screen
->optionCache
,
521 dri_set_background_context(struct st_context_iface
*st
,
522 struct util_queue_monitoring
*queue_info
)
524 struct dri_context
*ctx
= (struct dri_context
*)st
->st_manager_private
;
525 const __DRIbackgroundCallableExtension
*backgroundCallable
=
526 ctx
->sPriv
->dri2
.backgroundCallable
;
528 /* Note: Mesa will only call this function if GL multithreading is enabled
529 * We only do that if the loader exposed the __DRI_BACKGROUND_CALLABLE
530 * extension. So we know that backgroundCallable is not NULL.
532 assert(backgroundCallable
);
533 backgroundCallable
->setBackgroundContext(ctx
->cPriv
->loaderPrivate
);
536 hud_add_queue_for_monitoring(ctx
->hud
, queue_info
);
540 dri_init_options_get_screen_flags(struct dri_screen
*screen
,
541 const char* driver_name
)
545 driParseOptionInfo(&screen
->optionCacheDefaults
, gallium_config_options
.xml
);
546 driParseConfigFiles(&screen
->optionCache
,
547 &screen
->optionCacheDefaults
,
548 screen
->sPriv
->myNum
,
550 dri_fill_st_options(screen
);
552 if (driQueryOptionb(&screen
->optionCache
,
553 "glsl_correct_derivatives_after_discard"))
554 flags
|= PIPE_SCREEN_ENABLE_CORRECT_TGSI_DERIVATIVES_AFTER_KILL
;
560 dri_init_screen_helper(struct dri_screen
*screen
,
561 struct pipe_screen
*pscreen
)
563 screen
->base
.screen
= pscreen
;
564 screen
->base
.get_egl_image
= dri_get_egl_image
;
565 screen
->base
.get_param
= dri_get_param
;
566 screen
->base
.set_background_context
= dri_set_background_context
;
568 screen
->st_api
= st_gl_api_create();
572 if(pscreen
->get_param(pscreen
, PIPE_CAP_NPOT_TEXTURES
))
573 screen
->target
= PIPE_TEXTURE_2D
;
575 screen
->target
= PIPE_TEXTURE_RECT
;
577 /* Handle force_s3tc_enable. */
578 if (!util_format_s3tc_enabled
&& screen
->options
.force_s3tc_enable
) {
579 /* Ensure libtxc_dxtn has been loaded if available.
580 * Forcing S3TC on before calling this would prevent loading
582 * This is just a precaution, the driver should have called it
585 util_format_s3tc_init();
587 util_format_s3tc_enabled
= TRUE
;
590 dri_postprocessing_init(screen
);
592 screen
->st_api
->query_versions(screen
->st_api
, &screen
->base
,
594 &screen
->sPriv
->max_gl_core_version
,
595 &screen
->sPriv
->max_gl_compat_version
,
596 &screen
->sPriv
->max_gl_es1_version
,
597 &screen
->sPriv
->max_gl_es2_version
);
599 return dri_fill_in_modes(screen
);
602 /* vim: set sw=3 ts=8 sts=3 expandtab: */