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>
34 #include "dri_screen.h"
35 #include "dri_context.h"
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"
44 #include "util/u_debug.h"
45 #include "util/u_format_s3tc.h"
47 #define MSAA_VISUAL_MAX_SAMPLES 32
51 const __DRIconfigOptionsExtension gallium_config_options
= {
52 .base
= { __DRI_CONFIG_OPTIONS
, 2 },
53 .xml
= gallium_driinfo_xml
,
54 .getXml
= pipe_loader_get_driinfo_xml
60 dri_fill_st_options(struct dri_screen
*screen
)
62 struct st_config_options
*options
= &screen
->options
;
63 const struct driOptionCache
*optionCache
= &screen
->dev
->option_cache
;
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
->allow_glsl_extension_directive_midshader
=
76 driQueryOptionb(optionCache
, "allow_glsl_extension_directive_midshader");
77 options
->allow_glsl_builtin_variable_redeclaration
=
78 driQueryOptionb(optionCache
, "allow_glsl_builtin_variable_redeclaration");
79 options
->allow_higher_compat_version
=
80 driQueryOptionb(optionCache
, "allow_higher_compat_version");
81 options
->glsl_zero_init
= driQueryOptionb(optionCache
, "glsl_zero_init");
82 options
->force_glsl_abs_sqrt
=
83 driQueryOptionb(optionCache
, "force_glsl_abs_sqrt");
84 options
->allow_glsl_cross_stage_interpolation_mismatch
=
85 driQueryOptionb(optionCache
, "allow_glsl_cross_stage_interpolation_mismatch");
87 driComputeOptionsSha1(optionCache
, options
->config_options_sha1
);
91 dri_loader_get_cap(struct dri_screen
*screen
, enum dri_loader_cap cap
)
93 const __DRIdri2LoaderExtension
*dri2_loader
= screen
->sPriv
->dri2
.loader
;
94 const __DRIimageLoaderExtension
*image_loader
= screen
->sPriv
->image
.loader
;
96 if (dri2_loader
&& dri2_loader
->base
.version
>= 4 &&
97 dri2_loader
->getCapability
)
98 return dri2_loader
->getCapability(screen
->sPriv
->loaderPrivate
, cap
);
100 if (image_loader
&& image_loader
->base
.version
>= 2 &&
101 image_loader
->getCapability
)
102 return image_loader
->getCapability(screen
->sPriv
->loaderPrivate
, cap
);
107 static const __DRIconfig
**
108 dri_fill_in_modes(struct dri_screen
*screen
)
110 static const mesa_format mesa_formats
[] = {
111 MESA_FORMAT_B10G10R10A2_UNORM
,
112 MESA_FORMAT_B10G10R10X2_UNORM
,
113 MESA_FORMAT_R10G10B10A2_UNORM
,
114 MESA_FORMAT_R10G10B10X2_UNORM
,
115 MESA_FORMAT_B8G8R8A8_UNORM
,
116 MESA_FORMAT_B8G8R8X8_UNORM
,
117 MESA_FORMAT_B8G8R8A8_SRGB
,
118 MESA_FORMAT_B8G8R8X8_SRGB
,
119 MESA_FORMAT_B5G6R5_UNORM
,
121 /* The 32-bit RGBA format must not precede the 32-bit BGRA format.
122 * Likewise for RGBX and BGRX. Otherwise, the GLX client and the GLX
123 * server may disagree on which format the GLXFBConfig represents,
124 * resulting in swapped color channels.
126 * The problem, as of 2017-05-30:
127 * When matching a GLXFBConfig to a __DRIconfig, GLX ignores the channel
128 * order and chooses the first __DRIconfig with the expected channel
129 * sizes. Specifically, GLX compares the GLXFBConfig's and __DRIconfig's
130 * __DRI_ATTRIB_{CHANNEL}_SIZE but ignores __DRI_ATTRIB_{CHANNEL}_MASK.
132 * EGL does not suffer from this problem. It correctly compares the
133 * channel masks when matching EGLConfig to __DRIconfig.
136 /* Required by Android, for HAL_PIXEL_FORMAT_RGBA_8888. */
137 MESA_FORMAT_R8G8B8A8_UNORM
,
139 /* Required by Android, for HAL_PIXEL_FORMAT_RGBX_8888. */
140 MESA_FORMAT_R8G8B8X8_UNORM
,
142 static const enum pipe_format pipe_formats
[] = {
143 PIPE_FORMAT_B10G10R10A2_UNORM
,
144 PIPE_FORMAT_B10G10R10X2_UNORM
,
145 PIPE_FORMAT_R10G10B10A2_UNORM
,
146 PIPE_FORMAT_R10G10B10X2_UNORM
,
147 PIPE_FORMAT_BGRA8888_UNORM
,
148 PIPE_FORMAT_BGRX8888_UNORM
,
149 PIPE_FORMAT_BGRA8888_SRGB
,
150 PIPE_FORMAT_BGRX8888_SRGB
,
151 PIPE_FORMAT_B5G6R5_UNORM
,
152 PIPE_FORMAT_RGBA8888_UNORM
,
153 PIPE_FORMAT_RGBX8888_UNORM
,
156 __DRIconfig
**configs
= NULL
;
157 uint8_t depth_bits_array
[5];
158 uint8_t stencil_bits_array
[5];
159 unsigned depth_buffer_factor
;
160 unsigned msaa_samples_max
;
162 struct pipe_screen
*p_screen
= screen
->base
.screen
;
163 boolean pf_z16
, pf_x8z24
, pf_z24x8
, pf_s8z24
, pf_z24s8
, pf_z32
;
164 boolean mixed_color_depth
;
167 static const GLenum back_buffer_modes
[] = {
168 __DRI_ATTRIB_SWAP_NONE
, __DRI_ATTRIB_SWAP_UNDEFINED
,
169 __DRI_ATTRIB_SWAP_COPY
172 if (driQueryOptionb(&screen
->dev
->option_cache
, "always_have_depth_buffer")) {
173 /* all visuals will have a depth buffer */
174 depth_buffer_factor
= 0;
177 depth_bits_array
[0] = 0;
178 stencil_bits_array
[0] = 0;
179 depth_buffer_factor
= 1;
182 allow_rgb10
= driQueryOptionb(&screen
->dev
->option_cache
, "allow_rgb10_configs");
184 msaa_samples_max
= (screen
->st_api
->feature_mask
& ST_API_FEATURE_MS_VISUALS_MASK
)
185 ? MSAA_VISUAL_MAX_SAMPLES
: 1;
187 pf_x8z24
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z24X8_UNORM
,
189 PIPE_BIND_DEPTH_STENCIL
);
190 pf_z24x8
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_X8Z24_UNORM
,
192 PIPE_BIND_DEPTH_STENCIL
);
193 pf_s8z24
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z24_UNORM_S8_UINT
,
195 PIPE_BIND_DEPTH_STENCIL
);
196 pf_z24s8
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_S8_UINT_Z24_UNORM
,
198 PIPE_BIND_DEPTH_STENCIL
);
199 pf_z16
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z16_UNORM
,
201 PIPE_BIND_DEPTH_STENCIL
);
202 pf_z32
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z32_UNORM
,
204 PIPE_BIND_DEPTH_STENCIL
);
207 depth_bits_array
[depth_buffer_factor
] = 16;
208 stencil_bits_array
[depth_buffer_factor
++] = 0;
210 if (pf_x8z24
|| pf_z24x8
) {
211 depth_bits_array
[depth_buffer_factor
] = 24;
212 stencil_bits_array
[depth_buffer_factor
++] = 0;
213 screen
->d_depth_bits_last
= pf_x8z24
;
215 if (pf_s8z24
|| pf_z24s8
) {
216 depth_bits_array
[depth_buffer_factor
] = 24;
217 stencil_bits_array
[depth_buffer_factor
++] = 8;
218 screen
->sd_depth_bits_last
= pf_s8z24
;
221 depth_bits_array
[depth_buffer_factor
] = 32;
222 stencil_bits_array
[depth_buffer_factor
++] = 0;
226 p_screen
->get_param(p_screen
, PIPE_CAP_MIXED_COLOR_DEPTH_BITS
);
228 assert(ARRAY_SIZE(mesa_formats
) == ARRAY_SIZE(pipe_formats
));
230 /* Expose only BGRA ordering if the loader doesn't support RGBA ordering. */
231 unsigned num_formats
;
232 if (dri_loader_get_cap(screen
, DRI_LOADER_CAP_RGBA_ORDERING
))
233 num_formats
= ARRAY_SIZE(mesa_formats
);
235 num_formats
= ARRAY_SIZE(mesa_formats
) - 2; /* all - RGBA_ORDERING formats */
238 for (format
= 0; format
< num_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
];
244 (mesa_formats
[format
] == MESA_FORMAT_B10G10R10A2_UNORM
||
245 mesa_formats
[format
] == MESA_FORMAT_B10G10R10X2_UNORM
||
246 mesa_formats
[format
] == MESA_FORMAT_R10G10B10A2_UNORM
||
247 mesa_formats
[format
] == MESA_FORMAT_R10G10B10X2_UNORM
))
250 if (!p_screen
->is_format_supported(p_screen
, pipe_formats
[format
],
252 PIPE_BIND_RENDER_TARGET
|
253 PIPE_BIND_DISPLAY_TARGET
))
256 for (i
= 1; i
<= msaa_samples_max
; i
++) {
257 int samples
= i
> 1 ? i
: 0;
259 if (p_screen
->is_format_supported(p_screen
, pipe_formats
[format
],
260 PIPE_TEXTURE_2D
, samples
,
261 PIPE_BIND_RENDER_TARGET
)) {
262 msaa_modes
[num_msaa_modes
++] = samples
;
266 if (num_msaa_modes
) {
267 /* Single-sample configs with an accumulation buffer. */
268 new_configs
= driCreateConfigs(mesa_formats
[format
],
269 depth_bits_array
, stencil_bits_array
,
270 depth_buffer_factor
, back_buffer_modes
,
271 ARRAY_SIZE(back_buffer_modes
),
273 GL_TRUE
, !mixed_color_depth
);
274 configs
= driConcatConfigs(configs
, new_configs
);
276 /* Multi-sample configs without an accumulation buffer. */
277 if (num_msaa_modes
> 1) {
278 new_configs
= driCreateConfigs(mesa_formats
[format
],
279 depth_bits_array
, stencil_bits_array
,
280 depth_buffer_factor
, back_buffer_modes
,
281 ARRAY_SIZE(back_buffer_modes
),
282 msaa_modes
+1, num_msaa_modes
-1,
283 GL_FALSE
, !mixed_color_depth
);
284 configs
= driConcatConfigs(configs
, new_configs
);
289 if (configs
== NULL
) {
290 debug_printf("%s: driCreateConfigs failed\n", __FUNCTION__
);
294 return (const __DRIconfig
**)configs
;
298 * Roughly the converse of dri_fill_in_modes.
301 dri_fill_st_visual(struct st_visual
*stvis
, struct dri_screen
*screen
,
302 const struct gl_config
*mode
)
304 memset(stvis
, 0, sizeof(*stvis
));
309 /* Deduce the color format. */
310 switch (mode
->redMask
) {
312 if (mode
->alphaMask
) {
313 assert(mode
->alphaMask
== 0xC0000000);
314 stvis
->color_format
= PIPE_FORMAT_B10G10R10A2_UNORM
;
316 stvis
->color_format
= PIPE_FORMAT_B10G10R10X2_UNORM
;
321 if (mode
->alphaMask
) {
322 assert(mode
->alphaMask
== 0xC0000000);
323 stvis
->color_format
= PIPE_FORMAT_R10G10B10A2_UNORM
;
325 stvis
->color_format
= PIPE_FORMAT_R10G10B10X2_UNORM
;
330 if (mode
->alphaMask
) {
331 assert(mode
->alphaMask
== 0xFF000000);
332 stvis
->color_format
= mode
->sRGBCapable
?
333 PIPE_FORMAT_BGRA8888_SRGB
:
334 PIPE_FORMAT_BGRA8888_UNORM
;
336 stvis
->color_format
= mode
->sRGBCapable
?
337 PIPE_FORMAT_BGRX8888_SRGB
:
338 PIPE_FORMAT_BGRX8888_UNORM
;
343 if (mode
->alphaMask
) {
344 assert(mode
->alphaMask
== 0xFF000000);
345 stvis
->color_format
= mode
->sRGBCapable
?
346 PIPE_FORMAT_RGBA8888_SRGB
:
347 PIPE_FORMAT_RGBA8888_UNORM
;
349 stvis
->color_format
= mode
->sRGBCapable
?
350 PIPE_FORMAT_RGBX8888_SRGB
:
351 PIPE_FORMAT_RGBX8888_UNORM
;
356 stvis
->color_format
= PIPE_FORMAT_B5G6R5_UNORM
;
360 assert(!"unsupported visual: invalid red mask");
364 if (mode
->sampleBuffers
) {
365 stvis
->samples
= mode
->samples
;
368 switch (mode
->depthBits
) {
371 stvis
->depth_stencil_format
= PIPE_FORMAT_NONE
;
374 stvis
->depth_stencil_format
= PIPE_FORMAT_Z16_UNORM
;
377 if (mode
->stencilBits
== 0) {
378 stvis
->depth_stencil_format
= (screen
->d_depth_bits_last
) ?
379 PIPE_FORMAT_Z24X8_UNORM
:
380 PIPE_FORMAT_X8Z24_UNORM
;
382 stvis
->depth_stencil_format
= (screen
->sd_depth_bits_last
) ?
383 PIPE_FORMAT_Z24_UNORM_S8_UINT
:
384 PIPE_FORMAT_S8_UINT_Z24_UNORM
;
388 stvis
->depth_stencil_format
= PIPE_FORMAT_Z32_UNORM
;
392 stvis
->accum_format
= (mode
->haveAccumBuffer
) ?
393 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
395 stvis
->buffer_mask
|= ST_ATTACHMENT_FRONT_LEFT_MASK
;
396 stvis
->render_buffer
= ST_ATTACHMENT_FRONT_LEFT
;
397 if (mode
->doubleBufferMode
) {
398 stvis
->buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
399 stvis
->render_buffer
= ST_ATTACHMENT_BACK_LEFT
;
401 if (mode
->stereoMode
) {
402 stvis
->buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
403 if (mode
->doubleBufferMode
)
404 stvis
->buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
407 if (mode
->haveDepthBuffer
|| mode
->haveStencilBuffer
)
408 stvis
->buffer_mask
|= ST_ATTACHMENT_DEPTH_STENCIL_MASK
;
409 /* let the state tracker allocate the accum buffer */
413 dri_get_egl_image(struct st_manager
*smapi
,
415 struct st_egl_image
*stimg
)
417 struct dri_screen
*screen
= (struct dri_screen
*)smapi
;
418 __DRIimage
*img
= NULL
;
420 if (screen
->lookup_egl_image
) {
421 img
= screen
->lookup_egl_image(screen
, egl_image
);
427 stimg
->texture
= NULL
;
428 pipe_resource_reference(&stimg
->texture
, img
->texture
);
429 switch (img
->dri_components
) {
430 case __DRI_IMAGE_COMPONENTS_Y_U_V
:
431 stimg
->format
= PIPE_FORMAT_IYUV
;
433 case __DRI_IMAGE_COMPONENTS_Y_UV
:
434 stimg
->format
= PIPE_FORMAT_NV12
;
437 stimg
->format
= img
->texture
->format
;
440 stimg
->level
= img
->level
;
441 stimg
->layer
= img
->layer
;
447 dri_get_param(struct st_manager
*smapi
,
448 enum st_manager_param param
)
450 struct dri_screen
*screen
= (struct dri_screen
*)smapi
;
453 case ST_MANAGER_BROKEN_INVALIDATE
:
454 return screen
->broken_invalidate
;
461 dri_destroy_screen_helper(struct dri_screen
* screen
)
463 if (screen
->base
.destroy
)
464 screen
->base
.destroy(&screen
->base
);
466 if (screen
->st_api
&& screen
->st_api
->destroy
)
467 screen
->st_api
->destroy(screen
->st_api
);
469 if (screen
->base
.screen
)
470 screen
->base
.screen
->destroy(screen
->base
.screen
);
472 mtx_destroy(&screen
->opencl_func_mutex
);
476 dri_destroy_screen(__DRIscreen
* sPriv
)
478 struct dri_screen
*screen
= dri_screen(sPriv
);
480 dri_destroy_screen_helper(screen
);
482 pipe_loader_release(&screen
->dev
, 1);
485 sPriv
->driverPrivate
= NULL
;
486 sPriv
->extensions
= NULL
;
490 dri_postprocessing_init(struct dri_screen
*screen
)
494 for (i
= 0; i
< PP_FILTERS
; i
++) {
495 screen
->pp_enabled
[i
] = driQueryOptioni(&screen
->dev
->option_cache
,
501 dri_set_background_context(struct st_context_iface
*st
,
502 struct util_queue_monitoring
*queue_info
)
504 struct dri_context
*ctx
= (struct dri_context
*)st
->st_manager_private
;
505 const __DRIbackgroundCallableExtension
*backgroundCallable
=
506 ctx
->sPriv
->dri2
.backgroundCallable
;
508 /* Note: Mesa will only call this function if GL multithreading is enabled
509 * We only do that if the loader exposed the __DRI_BACKGROUND_CALLABLE
510 * extension. So we know that backgroundCallable is not NULL.
512 assert(backgroundCallable
);
513 backgroundCallable
->setBackgroundContext(ctx
->cPriv
->loaderPrivate
);
516 hud_add_queue_for_monitoring(ctx
->hud
, queue_info
);
520 dri_init_options(struct dri_screen
*screen
)
522 pipe_loader_load_options(screen
->dev
);
524 dri_fill_st_options(screen
);
528 dri_init_screen_helper(struct dri_screen
*screen
,
529 struct pipe_screen
*pscreen
)
531 screen
->base
.screen
= pscreen
;
532 screen
->base
.get_egl_image
= dri_get_egl_image
;
533 screen
->base
.get_param
= dri_get_param
;
534 screen
->base
.set_background_context
= dri_set_background_context
;
536 screen
->st_api
= st_gl_api_create();
540 if(pscreen
->get_param(pscreen
, PIPE_CAP_NPOT_TEXTURES
))
541 screen
->target
= PIPE_TEXTURE_2D
;
543 screen
->target
= PIPE_TEXTURE_RECT
;
545 dri_postprocessing_init(screen
);
547 screen
->st_api
->query_versions(screen
->st_api
, &screen
->base
,
549 &screen
->sPriv
->max_gl_core_version
,
550 &screen
->sPriv
->max_gl_compat_version
,
551 &screen
->sPriv
->max_gl_es1_version
,
552 &screen
->sPriv
->max_gl_es2_version
);
554 return dri_fill_in_modes(screen
);
557 /* vim: set sw=3 ts=8 sts=3 expandtab: */