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_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
);
127 static const __DRIconfig
**
128 dri_fill_in_modes(struct dri_screen
*screen
)
130 static const mesa_format mesa_formats
[] = {
131 MESA_FORMAT_B8G8R8A8_UNORM
,
132 MESA_FORMAT_B8G8R8X8_UNORM
,
133 MESA_FORMAT_B8G8R8A8_SRGB
,
134 MESA_FORMAT_B8G8R8X8_SRGB
,
135 MESA_FORMAT_B5G6R5_UNORM
,
137 /* The 32-bit RGBA format must not precede the 32-bit BGRA format.
138 * Likewise for RGBX and BGRX. Otherwise, the GLX client and the GLX
139 * server may disagree on which format the GLXFBConfig represents,
140 * resulting in swapped color channels.
142 * The problem, as of 2017-05-30:
143 * When matching a GLXFBConfig to a __DRIconfig, GLX ignores the channel
144 * order and chooses the first __DRIconfig with the expected channel
145 * sizes. Specifically, GLX compares the GLXFBConfig's and __DRIconfig's
146 * __DRI_ATTRIB_{CHANNEL}_SIZE but ignores __DRI_ATTRIB_{CHANNEL}_MASK.
148 * EGL does not suffer from this problem. It correctly compares the
149 * channel masks when matching EGLConfig to __DRIconfig.
152 /* Required by Android, for HAL_PIXEL_FORMAT_RGBA_8888. */
153 MESA_FORMAT_R8G8B8A8_UNORM
,
155 /* Required by Android, for HAL_PIXEL_FORMAT_RGBX_8888. */
156 MESA_FORMAT_R8G8B8X8_UNORM
,
158 static const enum pipe_format pipe_formats
[] = {
159 PIPE_FORMAT_BGRA8888_UNORM
,
160 PIPE_FORMAT_BGRX8888_UNORM
,
161 PIPE_FORMAT_BGRA8888_SRGB
,
162 PIPE_FORMAT_BGRX8888_SRGB
,
163 PIPE_FORMAT_B5G6R5_UNORM
,
164 PIPE_FORMAT_RGBA8888_UNORM
,
165 PIPE_FORMAT_RGBX8888_UNORM
,
168 __DRIconfig
**configs
= NULL
;
169 uint8_t depth_bits_array
[5];
170 uint8_t stencil_bits_array
[5];
171 unsigned depth_buffer_factor
;
172 unsigned msaa_samples_max
;
174 struct pipe_screen
*p_screen
= screen
->base
.screen
;
175 boolean pf_z16
, pf_x8z24
, pf_z24x8
, pf_s8z24
, pf_z24s8
, pf_z32
;
176 boolean mixed_color_depth
;
178 static const GLenum back_buffer_modes
[] = {
179 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
, GLX_SWAP_COPY_OML
182 if (driQueryOptionb(&screen
->optionCache
, "always_have_depth_buffer")) {
183 /* all visuals will have a depth buffer */
184 depth_buffer_factor
= 0;
187 depth_bits_array
[0] = 0;
188 stencil_bits_array
[0] = 0;
189 depth_buffer_factor
= 1;
192 msaa_samples_max
= (screen
->st_api
->feature_mask
& ST_API_FEATURE_MS_VISUALS_MASK
)
193 ? MSAA_VISUAL_MAX_SAMPLES
: 1;
195 pf_x8z24
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z24X8_UNORM
,
197 PIPE_BIND_DEPTH_STENCIL
);
198 pf_z24x8
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_X8Z24_UNORM
,
200 PIPE_BIND_DEPTH_STENCIL
);
201 pf_s8z24
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z24_UNORM_S8_UINT
,
203 PIPE_BIND_DEPTH_STENCIL
);
204 pf_z24s8
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_S8_UINT_Z24_UNORM
,
206 PIPE_BIND_DEPTH_STENCIL
);
207 pf_z16
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z16_UNORM
,
209 PIPE_BIND_DEPTH_STENCIL
);
210 pf_z32
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z32_UNORM
,
212 PIPE_BIND_DEPTH_STENCIL
);
215 depth_bits_array
[depth_buffer_factor
] = 16;
216 stencil_bits_array
[depth_buffer_factor
++] = 0;
218 if (pf_x8z24
|| pf_z24x8
) {
219 depth_bits_array
[depth_buffer_factor
] = 24;
220 stencil_bits_array
[depth_buffer_factor
++] = 0;
221 screen
->d_depth_bits_last
= pf_x8z24
;
223 if (pf_s8z24
|| pf_z24s8
) {
224 depth_bits_array
[depth_buffer_factor
] = 24;
225 stencil_bits_array
[depth_buffer_factor
++] = 8;
226 screen
->sd_depth_bits_last
= pf_s8z24
;
229 depth_bits_array
[depth_buffer_factor
] = 32;
230 stencil_bits_array
[depth_buffer_factor
++] = 0;
234 p_screen
->get_param(p_screen
, PIPE_CAP_MIXED_COLOR_DEPTH_BITS
);
236 assert(ARRAY_SIZE(mesa_formats
) == ARRAY_SIZE(pipe_formats
));
239 for (format
= 0; format
< ARRAY_SIZE(mesa_formats
); format
++) {
240 __DRIconfig
**new_configs
= NULL
;
241 unsigned num_msaa_modes
= 0; /* includes a single-sample mode */
242 uint8_t msaa_modes
[MSAA_VISUAL_MAX_SAMPLES
];
244 if (!p_screen
->is_format_supported(p_screen
, pipe_formats
[format
],
246 PIPE_BIND_RENDER_TARGET
))
249 for (i
= 1; i
<= msaa_samples_max
; i
++) {
250 int samples
= i
> 1 ? i
: 0;
252 if (p_screen
->is_format_supported(p_screen
, pipe_formats
[format
],
253 PIPE_TEXTURE_2D
, samples
,
254 PIPE_BIND_RENDER_TARGET
)) {
255 msaa_modes
[num_msaa_modes
++] = samples
;
259 if (num_msaa_modes
) {
260 /* Single-sample configs with an accumulation buffer. */
261 new_configs
= driCreateConfigs(mesa_formats
[format
],
262 depth_bits_array
, stencil_bits_array
,
263 depth_buffer_factor
, back_buffer_modes
,
264 ARRAY_SIZE(back_buffer_modes
),
266 GL_TRUE
, !mixed_color_depth
);
267 configs
= driConcatConfigs(configs
, new_configs
);
269 /* Multi-sample configs without an accumulation buffer. */
270 if (num_msaa_modes
> 1) {
271 new_configs
= driCreateConfigs(mesa_formats
[format
],
272 depth_bits_array
, stencil_bits_array
,
273 depth_buffer_factor
, back_buffer_modes
,
274 ARRAY_SIZE(back_buffer_modes
),
275 msaa_modes
+1, num_msaa_modes
-1,
276 GL_FALSE
, !mixed_color_depth
);
277 configs
= driConcatConfigs(configs
, new_configs
);
282 if (configs
== NULL
) {
283 debug_printf("%s: driCreateConfigs failed\n", __FUNCTION__
);
287 return (const __DRIconfig
**)configs
;
291 * Roughly the converse of dri_fill_in_modes.
294 dri_fill_st_visual(struct st_visual
*stvis
, struct dri_screen
*screen
,
295 const struct gl_config
*mode
)
297 memset(stvis
, 0, sizeof(*stvis
));
302 /* Deduce the color format. */
303 switch (mode
->redMask
) {
305 if (mode
->alphaMask
) {
306 assert(mode
->alphaMask
== 0xFF000000);
307 stvis
->color_format
= mode
->sRGBCapable
?
308 PIPE_FORMAT_BGRA8888_SRGB
:
309 PIPE_FORMAT_BGRA8888_UNORM
;
311 stvis
->color_format
= mode
->sRGBCapable
?
312 PIPE_FORMAT_BGRX8888_SRGB
:
313 PIPE_FORMAT_BGRX8888_UNORM
;
318 if (mode
->alphaMask
) {
319 assert(mode
->alphaMask
== 0xFF000000);
320 stvis
->color_format
= mode
->sRGBCapable
?
321 PIPE_FORMAT_RGBA8888_SRGB
:
322 PIPE_FORMAT_RGBA8888_UNORM
;
324 stvis
->color_format
= mode
->sRGBCapable
?
325 PIPE_FORMAT_RGBX8888_SRGB
:
326 PIPE_FORMAT_RGBX8888_UNORM
;
331 stvis
->color_format
= PIPE_FORMAT_B5G6R5_UNORM
;
335 assert(!"unsupported visual: invalid red mask");
339 if (mode
->sampleBuffers
) {
340 stvis
->samples
= mode
->samples
;
343 switch (mode
->depthBits
) {
346 stvis
->depth_stencil_format
= PIPE_FORMAT_NONE
;
349 stvis
->depth_stencil_format
= PIPE_FORMAT_Z16_UNORM
;
352 if (mode
->stencilBits
== 0) {
353 stvis
->depth_stencil_format
= (screen
->d_depth_bits_last
) ?
354 PIPE_FORMAT_Z24X8_UNORM
:
355 PIPE_FORMAT_X8Z24_UNORM
;
357 stvis
->depth_stencil_format
= (screen
->sd_depth_bits_last
) ?
358 PIPE_FORMAT_Z24_UNORM_S8_UINT
:
359 PIPE_FORMAT_S8_UINT_Z24_UNORM
;
363 stvis
->depth_stencil_format
= PIPE_FORMAT_Z32_UNORM
;
367 stvis
->accum_format
= (mode
->haveAccumBuffer
) ?
368 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
370 stvis
->buffer_mask
|= ST_ATTACHMENT_FRONT_LEFT_MASK
;
371 stvis
->render_buffer
= ST_ATTACHMENT_FRONT_LEFT
;
372 if (mode
->doubleBufferMode
) {
373 stvis
->buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
374 stvis
->render_buffer
= ST_ATTACHMENT_BACK_LEFT
;
376 if (mode
->stereoMode
) {
377 stvis
->buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
378 if (mode
->doubleBufferMode
)
379 stvis
->buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
382 if (mode
->haveDepthBuffer
|| mode
->haveStencilBuffer
)
383 stvis
->buffer_mask
|= ST_ATTACHMENT_DEPTH_STENCIL_MASK
;
384 /* let the state tracker allocate the accum buffer */
388 dri_get_egl_image(struct st_manager
*smapi
,
390 struct st_egl_image
*stimg
)
392 struct dri_screen
*screen
= (struct dri_screen
*)smapi
;
393 __DRIimage
*img
= NULL
;
395 if (screen
->lookup_egl_image
) {
396 img
= screen
->lookup_egl_image(screen
, egl_image
);
402 stimg
->texture
= NULL
;
403 pipe_resource_reference(&stimg
->texture
, img
->texture
);
404 switch (img
->dri_components
) {
405 case __DRI_IMAGE_COMPONENTS_Y_U_V
:
406 stimg
->format
= PIPE_FORMAT_IYUV
;
408 case __DRI_IMAGE_COMPONENTS_Y_UV
:
409 stimg
->format
= PIPE_FORMAT_NV12
;
412 stimg
->format
= img
->texture
->format
;
415 stimg
->level
= img
->level
;
416 stimg
->layer
= img
->layer
;
422 dri_get_param(struct st_manager
*smapi
,
423 enum st_manager_param param
)
425 struct dri_screen
*screen
= (struct dri_screen
*)smapi
;
428 case ST_MANAGER_BROKEN_INVALIDATE
:
429 return screen
->broken_invalidate
;
436 dri_destroy_option_cache(struct dri_screen
* screen
)
440 if (screen
->optionCache
.info
) {
441 for (i
= 0; i
< (1 << screen
->optionCache
.tableSize
); ++i
) {
442 free(screen
->optionCache
.info
[i
].name
);
443 free(screen
->optionCache
.info
[i
].ranges
);
445 free(screen
->optionCache
.info
);
448 free(screen
->optionCache
.values
);
450 /* Default values are copied to screen->optionCache->values in
451 * initOptionCache. The info field, however, is a pointer copy, so don't free
454 free(screen
->optionCacheDefaults
.values
);
458 dri_destroy_screen_helper(struct dri_screen
* screen
)
460 if (screen
->st_api
&& screen
->st_api
->destroy
)
461 screen
->st_api
->destroy(screen
->st_api
);
463 if (screen
->base
.screen
)
464 screen
->base
.screen
->destroy(screen
->base
.screen
);
466 dri_destroy_option_cache(screen
);
467 mtx_destroy(&screen
->opencl_func_mutex
);
471 dri_destroy_screen(__DRIscreen
* sPriv
)
473 struct dri_screen
*screen
= dri_screen(sPriv
);
475 dri_destroy_screen_helper(screen
);
477 pipe_loader_release(&screen
->dev
, 1);
480 sPriv
->driverPrivate
= NULL
;
481 sPriv
->extensions
= NULL
;
485 dri_postprocessing_init(struct dri_screen
*screen
)
489 for (i
= 0; i
< PP_FILTERS
; i
++) {
490 screen
->pp_enabled
[i
] = driQueryOptioni(&screen
->optionCache
,
496 dri_set_background_context(struct st_context_iface
*st
,
497 struct util_queue_monitoring
*queue_info
)
499 struct dri_context
*ctx
= (struct dri_context
*)st
->st_manager_private
;
500 const __DRIbackgroundCallableExtension
*backgroundCallable
=
501 ctx
->sPriv
->dri2
.backgroundCallable
;
503 /* Note: Mesa will only call this function if GL multithreading is enabled
504 * We only do that if the loader exposed the __DRI_BACKGROUND_CALLABLE
505 * extension. So we know that backgroundCallable is not NULL.
507 assert(backgroundCallable
);
508 backgroundCallable
->setBackgroundContext(ctx
->cPriv
->loaderPrivate
);
511 hud_add_queue_for_monitoring(ctx
->hud
, queue_info
);
515 dri_init_options_get_screen_flags(struct dri_screen
*screen
,
516 const char* driver_name
)
520 driParseOptionInfo(&screen
->optionCacheDefaults
, gallium_config_options
.xml
);
521 driParseConfigFiles(&screen
->optionCache
,
522 &screen
->optionCacheDefaults
,
523 screen
->sPriv
->myNum
,
525 dri_fill_st_options(screen
);
527 if (driQueryOptionb(&screen
->optionCache
,
528 "glsl_correct_derivatives_after_discard"))
529 flags
|= PIPE_SCREEN_ENABLE_CORRECT_TGSI_DERIVATIVES_AFTER_KILL
;
535 dri_init_screen_helper(struct dri_screen
*screen
,
536 struct pipe_screen
*pscreen
)
538 screen
->base
.screen
= pscreen
;
539 screen
->base
.get_egl_image
= dri_get_egl_image
;
540 screen
->base
.get_param
= dri_get_param
;
541 screen
->base
.set_background_context
= dri_set_background_context
;
543 screen
->st_api
= st_gl_api_create();
547 if(pscreen
->get_param(pscreen
, PIPE_CAP_NPOT_TEXTURES
))
548 screen
->target
= PIPE_TEXTURE_2D
;
550 screen
->target
= PIPE_TEXTURE_RECT
;
552 /* Handle force_s3tc_enable. */
553 if (!util_format_s3tc_enabled
&& screen
->options
.force_s3tc_enable
) {
554 /* Ensure libtxc_dxtn has been loaded if available.
555 * Forcing S3TC on before calling this would prevent loading
557 * This is just a precaution, the driver should have called it
560 util_format_s3tc_init();
562 util_format_s3tc_enabled
= TRUE
;
565 dri_postprocessing_init(screen
);
567 screen
->st_api
->query_versions(screen
->st_api
, &screen
->base
,
569 &screen
->sPriv
->max_gl_core_version
,
570 &screen
->sPriv
->max_gl_compat_version
,
571 &screen
->sPriv
->max_gl_es1_version
,
572 &screen
->sPriv
->max_gl_es2_version
);
574 return dri_fill_in_modes(screen
);
577 /* vim: set sw=3 ts=8 sts=3 expandtab: */