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>
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_DISABLE_EXT_BUFFER_AGE("false")
60 DRI_CONF_DISABLE_OML_SYNC_CONTROL("false")
63 DRI_CONF_SECTION_QUALITY
64 DRI_CONF_FORCE_S3TC_ENABLE("false")
65 DRI_CONF_PP_CELSHADE(0)
67 DRI_CONF_PP_NOGREEN(0)
69 DRI_CONF_PP_JIMENEZMLAA(0, 0, 32)
70 DRI_CONF_PP_JIMENEZMLAA_COLOR(0, 0, 32)
73 DRI_CONF_SECTION_DEBUG
74 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
75 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
76 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
77 DRI_CONF_DISABLE_SHADER_BIT_ENCODING("false")
78 DRI_CONF_FORCE_GLSL_VERSION(0)
79 DRI_CONF_ALLOW_GLSL_EXTENSION_DIRECTIVE_MIDSHADER("false")
80 DRI_CONF_ALLOW_GLSL_BUILTIN_VARIABLE_REDECLARATION("false")
81 DRI_CONF_ALLOW_HIGHER_COMPAT_VERSION("false")
82 DRI_CONF_FORCE_GLSL_ABS_SQRT("false")
83 DRI_CONF_GLSL_CORRECT_DERIVATIVES_AFTER_DISCARD("false")
86 DRI_CONF_SECTION_MISCELLANEOUS
87 DRI_CONF_ALWAYS_HAVE_DEPTH_BUFFER("false")
88 DRI_CONF_GLSL_ZERO_INIT("false")
96 dri_fill_st_options(struct dri_screen
*screen
)
98 struct st_config_options
*options
= &screen
->options
;
99 const struct driOptionCache
*optionCache
= &screen
->optionCache
;
101 options
->disable_blend_func_extended
=
102 driQueryOptionb(optionCache
, "disable_blend_func_extended");
103 options
->disable_glsl_line_continuations
=
104 driQueryOptionb(optionCache
, "disable_glsl_line_continuations");
105 options
->disable_shader_bit_encoding
=
106 driQueryOptionb(optionCache
, "disable_shader_bit_encoding");
107 options
->force_glsl_extensions_warn
=
108 driQueryOptionb(optionCache
, "force_glsl_extensions_warn");
109 options
->force_glsl_version
=
110 driQueryOptioni(optionCache
, "force_glsl_version");
111 options
->force_s3tc_enable
=
112 driQueryOptionb(optionCache
, "force_s3tc_enable");
113 options
->allow_glsl_extension_directive_midshader
=
114 driQueryOptionb(optionCache
, "allow_glsl_extension_directive_midshader");
115 options
->allow_glsl_builtin_variable_redeclaration
=
116 driQueryOptionb(optionCache
, "allow_glsl_builtin_variable_redeclaration");
117 options
->allow_higher_compat_version
=
118 driQueryOptionb(optionCache
, "allow_higher_compat_version");
119 options
->glsl_zero_init
= driQueryOptionb(optionCache
, "glsl_zero_init");
120 options
->force_glsl_abs_sqrt
=
121 driQueryOptionb(optionCache
, "force_glsl_abs_sqrt");
123 driComputeOptionsSha1(optionCache
, options
->config_options_sha1
);
126 static const __DRIconfig
**
127 dri_fill_in_modes(struct dri_screen
*screen
)
129 static const mesa_format mesa_formats
[] = {
130 MESA_FORMAT_B8G8R8A8_UNORM
,
131 MESA_FORMAT_B8G8R8X8_UNORM
,
132 MESA_FORMAT_B8G8R8A8_SRGB
,
133 MESA_FORMAT_B8G8R8X8_SRGB
,
134 MESA_FORMAT_B5G6R5_UNORM
,
136 /* The 32-bit RGBA format must not precede the 32-bit BGRA format.
137 * Likewise for RGBX and BGRX. Otherwise, the GLX client and the GLX
138 * server may disagree on which format the GLXFBConfig represents,
139 * resulting in swapped color channels.
141 * The problem, as of 2017-05-30:
142 * When matching a GLXFBConfig to a __DRIconfig, GLX ignores the channel
143 * order and chooses the first __DRIconfig with the expected channel
144 * sizes. Specifically, GLX compares the GLXFBConfig's and __DRIconfig's
145 * __DRI_ATTRIB_{CHANNEL}_SIZE but ignores __DRI_ATTRIB_{CHANNEL}_MASK.
147 * EGL does not suffer from this problem. It correctly compares the
148 * channel masks when matching EGLConfig to __DRIconfig.
151 /* Required by Android, for HAL_PIXEL_FORMAT_RGBA_8888. */
152 MESA_FORMAT_R8G8B8A8_UNORM
,
154 /* Required by Android, for HAL_PIXEL_FORMAT_RGBX_8888. */
155 MESA_FORMAT_R8G8B8X8_UNORM
,
157 static const enum pipe_format pipe_formats
[] = {
158 PIPE_FORMAT_BGRA8888_UNORM
,
159 PIPE_FORMAT_BGRX8888_UNORM
,
160 PIPE_FORMAT_BGRA8888_SRGB
,
161 PIPE_FORMAT_BGRX8888_SRGB
,
162 PIPE_FORMAT_B5G6R5_UNORM
,
163 PIPE_FORMAT_RGBA8888_UNORM
,
164 PIPE_FORMAT_RGBX8888_UNORM
,
167 __DRIconfig
**configs
= NULL
;
168 uint8_t depth_bits_array
[5];
169 uint8_t stencil_bits_array
[5];
170 unsigned depth_buffer_factor
;
171 unsigned msaa_samples_max
;
173 struct pipe_screen
*p_screen
= screen
->base
.screen
;
174 boolean pf_z16
, pf_x8z24
, pf_z24x8
, pf_s8z24
, pf_z24s8
, pf_z32
;
175 boolean mixed_color_depth
;
177 static const GLenum back_buffer_modes
[] = {
178 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
, GLX_SWAP_COPY_OML
181 if (driQueryOptionb(&screen
->optionCache
, "always_have_depth_buffer")) {
182 /* all visuals will have a depth buffer */
183 depth_buffer_factor
= 0;
186 depth_bits_array
[0] = 0;
187 stencil_bits_array
[0] = 0;
188 depth_buffer_factor
= 1;
191 msaa_samples_max
= (screen
->st_api
->feature_mask
& ST_API_FEATURE_MS_VISUALS_MASK
)
192 ? MSAA_VISUAL_MAX_SAMPLES
: 1;
194 pf_x8z24
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z24X8_UNORM
,
196 PIPE_BIND_DEPTH_STENCIL
);
197 pf_z24x8
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_X8Z24_UNORM
,
199 PIPE_BIND_DEPTH_STENCIL
);
200 pf_s8z24
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z24_UNORM_S8_UINT
,
202 PIPE_BIND_DEPTH_STENCIL
);
203 pf_z24s8
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_S8_UINT_Z24_UNORM
,
205 PIPE_BIND_DEPTH_STENCIL
);
206 pf_z16
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z16_UNORM
,
208 PIPE_BIND_DEPTH_STENCIL
);
209 pf_z32
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z32_UNORM
,
211 PIPE_BIND_DEPTH_STENCIL
);
214 depth_bits_array
[depth_buffer_factor
] = 16;
215 stencil_bits_array
[depth_buffer_factor
++] = 0;
217 if (pf_x8z24
|| pf_z24x8
) {
218 depth_bits_array
[depth_buffer_factor
] = 24;
219 stencil_bits_array
[depth_buffer_factor
++] = 0;
220 screen
->d_depth_bits_last
= pf_x8z24
;
222 if (pf_s8z24
|| pf_z24s8
) {
223 depth_bits_array
[depth_buffer_factor
] = 24;
224 stencil_bits_array
[depth_buffer_factor
++] = 8;
225 screen
->sd_depth_bits_last
= pf_s8z24
;
228 depth_bits_array
[depth_buffer_factor
] = 32;
229 stencil_bits_array
[depth_buffer_factor
++] = 0;
233 p_screen
->get_param(p_screen
, PIPE_CAP_MIXED_COLOR_DEPTH_BITS
);
235 assert(ARRAY_SIZE(mesa_formats
) == ARRAY_SIZE(pipe_formats
));
238 for (format
= 0; format
< ARRAY_SIZE(mesa_formats
); format
++) {
239 __DRIconfig
**new_configs
= NULL
;
240 unsigned num_msaa_modes
= 0; /* includes a single-sample mode */
241 uint8_t msaa_modes
[MSAA_VISUAL_MAX_SAMPLES
];
243 if (!p_screen
->is_format_supported(p_screen
, pipe_formats
[format
],
245 PIPE_BIND_RENDER_TARGET
))
248 for (i
= 1; i
<= msaa_samples_max
; i
++) {
249 int samples
= i
> 1 ? i
: 0;
251 if (p_screen
->is_format_supported(p_screen
, pipe_formats
[format
],
252 PIPE_TEXTURE_2D
, samples
,
253 PIPE_BIND_RENDER_TARGET
)) {
254 msaa_modes
[num_msaa_modes
++] = samples
;
258 if (num_msaa_modes
) {
259 /* Single-sample configs with an accumulation buffer. */
260 new_configs
= driCreateConfigs(mesa_formats
[format
],
261 depth_bits_array
, stencil_bits_array
,
262 depth_buffer_factor
, back_buffer_modes
,
263 ARRAY_SIZE(back_buffer_modes
),
265 GL_TRUE
, !mixed_color_depth
);
266 configs
= driConcatConfigs(configs
, new_configs
);
268 /* Multi-sample configs without an accumulation buffer. */
269 if (num_msaa_modes
> 1) {
270 new_configs
= driCreateConfigs(mesa_formats
[format
],
271 depth_bits_array
, stencil_bits_array
,
272 depth_buffer_factor
, back_buffer_modes
,
273 ARRAY_SIZE(back_buffer_modes
),
274 msaa_modes
+1, num_msaa_modes
-1,
275 GL_FALSE
, !mixed_color_depth
);
276 configs
= driConcatConfigs(configs
, new_configs
);
281 if (configs
== NULL
) {
282 debug_printf("%s: driCreateConfigs failed\n", __FUNCTION__
);
286 return (const __DRIconfig
**)configs
;
290 * Roughly the converse of dri_fill_in_modes.
293 dri_fill_st_visual(struct st_visual
*stvis
, struct dri_screen
*screen
,
294 const struct gl_config
*mode
)
296 memset(stvis
, 0, sizeof(*stvis
));
301 /* Deduce the color format. */
302 switch (mode
->redMask
) {
304 if (mode
->alphaMask
) {
305 assert(mode
->alphaMask
== 0xFF000000);
306 stvis
->color_format
= mode
->sRGBCapable
?
307 PIPE_FORMAT_BGRA8888_SRGB
:
308 PIPE_FORMAT_BGRA8888_UNORM
;
310 stvis
->color_format
= mode
->sRGBCapable
?
311 PIPE_FORMAT_BGRX8888_SRGB
:
312 PIPE_FORMAT_BGRX8888_UNORM
;
317 if (mode
->alphaMask
) {
318 assert(mode
->alphaMask
== 0xFF000000);
319 stvis
->color_format
= mode
->sRGBCapable
?
320 PIPE_FORMAT_RGBA8888_SRGB
:
321 PIPE_FORMAT_RGBA8888_UNORM
;
323 stvis
->color_format
= mode
->sRGBCapable
?
324 PIPE_FORMAT_RGBX8888_SRGB
:
325 PIPE_FORMAT_RGBX8888_UNORM
;
330 stvis
->color_format
= PIPE_FORMAT_B5G6R5_UNORM
;
334 assert(!"unsupported visual: invalid red mask");
338 if (mode
->sampleBuffers
) {
339 stvis
->samples
= mode
->samples
;
342 switch (mode
->depthBits
) {
345 stvis
->depth_stencil_format
= PIPE_FORMAT_NONE
;
348 stvis
->depth_stencil_format
= PIPE_FORMAT_Z16_UNORM
;
351 if (mode
->stencilBits
== 0) {
352 stvis
->depth_stencil_format
= (screen
->d_depth_bits_last
) ?
353 PIPE_FORMAT_Z24X8_UNORM
:
354 PIPE_FORMAT_X8Z24_UNORM
;
356 stvis
->depth_stencil_format
= (screen
->sd_depth_bits_last
) ?
357 PIPE_FORMAT_Z24_UNORM_S8_UINT
:
358 PIPE_FORMAT_S8_UINT_Z24_UNORM
;
362 stvis
->depth_stencil_format
= PIPE_FORMAT_Z32_UNORM
;
366 stvis
->accum_format
= (mode
->haveAccumBuffer
) ?
367 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
369 stvis
->buffer_mask
|= ST_ATTACHMENT_FRONT_LEFT_MASK
;
370 stvis
->render_buffer
= ST_ATTACHMENT_FRONT_LEFT
;
371 if (mode
->doubleBufferMode
) {
372 stvis
->buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
373 stvis
->render_buffer
= ST_ATTACHMENT_BACK_LEFT
;
375 if (mode
->stereoMode
) {
376 stvis
->buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
377 if (mode
->doubleBufferMode
)
378 stvis
->buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
381 if (mode
->haveDepthBuffer
|| mode
->haveStencilBuffer
)
382 stvis
->buffer_mask
|= ST_ATTACHMENT_DEPTH_STENCIL_MASK
;
383 /* let the state tracker allocate the accum buffer */
387 dri_get_egl_image(struct st_manager
*smapi
,
389 struct st_egl_image
*stimg
)
391 struct dri_screen
*screen
= (struct dri_screen
*)smapi
;
392 __DRIimage
*img
= NULL
;
394 if (screen
->lookup_egl_image
) {
395 img
= screen
->lookup_egl_image(screen
, egl_image
);
401 stimg
->texture
= NULL
;
402 pipe_resource_reference(&stimg
->texture
, img
->texture
);
403 switch (img
->dri_components
) {
404 case __DRI_IMAGE_COMPONENTS_Y_U_V
:
405 stimg
->format
= PIPE_FORMAT_IYUV
;
407 case __DRI_IMAGE_COMPONENTS_Y_UV
:
408 stimg
->format
= PIPE_FORMAT_NV12
;
411 stimg
->format
= img
->texture
->format
;
414 stimg
->level
= img
->level
;
415 stimg
->layer
= img
->layer
;
421 dri_get_param(struct st_manager
*smapi
,
422 enum st_manager_param param
)
424 struct dri_screen
*screen
= (struct dri_screen
*)smapi
;
427 case ST_MANAGER_BROKEN_INVALIDATE
:
428 return screen
->broken_invalidate
;
435 dri_destroy_option_cache(struct dri_screen
* screen
)
439 if (screen
->optionCache
.info
) {
440 for (i
= 0; i
< (1 << screen
->optionCache
.tableSize
); ++i
) {
441 free(screen
->optionCache
.info
[i
].name
);
442 free(screen
->optionCache
.info
[i
].ranges
);
444 free(screen
->optionCache
.info
);
447 free(screen
->optionCache
.values
);
449 /* Default values are copied to screen->optionCache->values in
450 * initOptionCache. The info field, however, is a pointer copy, so don't free
453 free(screen
->optionCacheDefaults
.values
);
457 dri_destroy_screen_helper(struct dri_screen
* screen
)
459 if (screen
->st_api
&& screen
->st_api
->destroy
)
460 screen
->st_api
->destroy(screen
->st_api
);
462 if (screen
->base
.screen
)
463 screen
->base
.screen
->destroy(screen
->base
.screen
);
465 dri_destroy_option_cache(screen
);
466 mtx_destroy(&screen
->opencl_func_mutex
);
470 dri_destroy_screen(__DRIscreen
* sPriv
)
472 struct dri_screen
*screen
= dri_screen(sPriv
);
474 dri_destroy_screen_helper(screen
);
476 pipe_loader_release(&screen
->dev
, 1);
479 sPriv
->driverPrivate
= NULL
;
480 sPriv
->extensions
= NULL
;
484 dri_postprocessing_init(struct dri_screen
*screen
)
488 for (i
= 0; i
< PP_FILTERS
; i
++) {
489 screen
->pp_enabled
[i
] = driQueryOptioni(&screen
->optionCache
,
495 dri_set_background_context(struct st_context_iface
*st
,
496 struct util_queue_monitoring
*queue_info
)
498 struct dri_context
*ctx
= (struct dri_context
*)st
->st_manager_private
;
499 const __DRIbackgroundCallableExtension
*backgroundCallable
=
500 ctx
->sPriv
->dri2
.backgroundCallable
;
502 /* Note: Mesa will only call this function if GL multithreading is enabled
503 * We only do that if the loader exposed the __DRI_BACKGROUND_CALLABLE
504 * extension. So we know that backgroundCallable is not NULL.
506 assert(backgroundCallable
);
507 backgroundCallable
->setBackgroundContext(ctx
->cPriv
->loaderPrivate
);
510 hud_add_queue_for_monitoring(ctx
->hud
, queue_info
);
514 dri_init_options_get_screen_flags(struct dri_screen
*screen
,
515 const char* driver_name
)
519 driParseOptionInfo(&screen
->optionCacheDefaults
, gallium_config_options
.xml
);
520 driParseConfigFiles(&screen
->optionCache
,
521 &screen
->optionCacheDefaults
,
522 screen
->sPriv
->myNum
,
524 dri_fill_st_options(screen
);
526 if (driQueryOptionb(&screen
->optionCache
,
527 "glsl_correct_derivatives_after_discard"))
528 flags
|= PIPE_SCREEN_ENABLE_CORRECT_TGSI_DERIVATIVES_AFTER_KILL
;
534 dri_init_screen_helper(struct dri_screen
*screen
,
535 struct pipe_screen
*pscreen
)
537 screen
->base
.screen
= pscreen
;
538 screen
->base
.get_egl_image
= dri_get_egl_image
;
539 screen
->base
.get_param
= dri_get_param
;
540 screen
->base
.set_background_context
= dri_set_background_context
;
542 screen
->st_api
= st_gl_api_create();
546 if(pscreen
->get_param(pscreen
, PIPE_CAP_NPOT_TEXTURES
))
547 screen
->target
= PIPE_TEXTURE_2D
;
549 screen
->target
= PIPE_TEXTURE_RECT
;
551 /* Handle force_s3tc_enable. */
552 if (!util_format_s3tc_enabled
&& screen
->options
.force_s3tc_enable
) {
553 /* Ensure libtxc_dxtn has been loaded if available.
554 * Forcing S3TC on before calling this would prevent loading
556 * This is just a precaution, the driver should have called it
559 util_format_s3tc_init();
561 util_format_s3tc_enabled
= TRUE
;
564 dri_postprocessing_init(screen
);
566 screen
->st_api
->query_versions(screen
->st_api
, &screen
->base
,
568 &screen
->sPriv
->max_gl_core_version
,
569 &screen
->sPriv
->max_gl_compat_version
,
570 &screen
->sPriv
->max_gl_es1_version
,
571 &screen
->sPriv
->max_gl_es2_version
);
573 return dri_fill_in_modes(screen
);
576 /* vim: set sw=3 ts=8 sts=3 expandtab: */