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
, 1 },
53 .xml
= gallium_driinfo_xml
59 dri_fill_st_options(struct dri_screen
*screen
)
61 struct st_config_options
*options
= &screen
->options
;
62 const struct driOptionCache
*optionCache
= &screen
->optionCache
;
64 options
->disable_blend_func_extended
=
65 driQueryOptionb(optionCache
, "disable_blend_func_extended");
66 options
->disable_glsl_line_continuations
=
67 driQueryOptionb(optionCache
, "disable_glsl_line_continuations");
68 options
->disable_shader_bit_encoding
=
69 driQueryOptionb(optionCache
, "disable_shader_bit_encoding");
70 options
->force_glsl_extensions_warn
=
71 driQueryOptionb(optionCache
, "force_glsl_extensions_warn");
72 options
->force_glsl_version
=
73 driQueryOptioni(optionCache
, "force_glsl_version");
74 options
->force_s3tc_enable
=
75 driQueryOptionb(optionCache
, "force_s3tc_enable");
76 options
->allow_glsl_extension_directive_midshader
=
77 driQueryOptionb(optionCache
, "allow_glsl_extension_directive_midshader");
78 options
->allow_glsl_builtin_variable_redeclaration
=
79 driQueryOptionb(optionCache
, "allow_glsl_builtin_variable_redeclaration");
80 options
->allow_higher_compat_version
=
81 driQueryOptionb(optionCache
, "allow_higher_compat_version");
82 options
->glsl_zero_init
= driQueryOptionb(optionCache
, "glsl_zero_init");
83 options
->force_glsl_abs_sqrt
=
84 driQueryOptionb(optionCache
, "force_glsl_abs_sqrt");
86 driComputeOptionsSha1(optionCache
, options
->config_options_sha1
);
90 dri_loader_get_cap(struct dri_screen
*screen
, enum dri_loader_cap cap
)
92 const __DRIdri2LoaderExtension
*dri2_loader
= screen
->sPriv
->dri2
.loader
;
93 const __DRIimageLoaderExtension
*image_loader
= screen
->sPriv
->image
.loader
;
95 if (dri2_loader
&& dri2_loader
->base
.version
>= 4)
96 return dri2_loader
->getCapability(screen
->sPriv
->loaderPrivate
, cap
);
98 if (image_loader
&& image_loader
->base
.version
>= 2)
99 return image_loader
->getCapability(screen
->sPriv
->loaderPrivate
, cap
);
104 static const __DRIconfig
**
105 dri_fill_in_modes(struct dri_screen
*screen
)
107 static const mesa_format mesa_formats
[] = {
108 MESA_FORMAT_B8G8R8A8_UNORM
,
109 MESA_FORMAT_B8G8R8X8_UNORM
,
110 MESA_FORMAT_B8G8R8A8_SRGB
,
111 MESA_FORMAT_B8G8R8X8_SRGB
,
112 MESA_FORMAT_B5G6R5_UNORM
,
114 /* The 32-bit RGBA format must not precede the 32-bit BGRA format.
115 * Likewise for RGBX and BGRX. Otherwise, the GLX client and the GLX
116 * server may disagree on which format the GLXFBConfig represents,
117 * resulting in swapped color channels.
119 * The problem, as of 2017-05-30:
120 * When matching a GLXFBConfig to a __DRIconfig, GLX ignores the channel
121 * order and chooses the first __DRIconfig with the expected channel
122 * sizes. Specifically, GLX compares the GLXFBConfig's and __DRIconfig's
123 * __DRI_ATTRIB_{CHANNEL}_SIZE but ignores __DRI_ATTRIB_{CHANNEL}_MASK.
125 * EGL does not suffer from this problem. It correctly compares the
126 * channel masks when matching EGLConfig to __DRIconfig.
129 /* Required by Android, for HAL_PIXEL_FORMAT_RGBA_8888. */
130 MESA_FORMAT_R8G8B8A8_UNORM
,
132 /* Required by Android, for HAL_PIXEL_FORMAT_RGBX_8888. */
133 MESA_FORMAT_R8G8B8X8_UNORM
,
135 static const enum pipe_format pipe_formats
[] = {
136 PIPE_FORMAT_BGRA8888_UNORM
,
137 PIPE_FORMAT_BGRX8888_UNORM
,
138 PIPE_FORMAT_BGRA8888_SRGB
,
139 PIPE_FORMAT_BGRX8888_SRGB
,
140 PIPE_FORMAT_B5G6R5_UNORM
,
141 PIPE_FORMAT_RGBA8888_UNORM
,
142 PIPE_FORMAT_RGBX8888_UNORM
,
145 __DRIconfig
**configs
= NULL
;
146 uint8_t depth_bits_array
[5];
147 uint8_t stencil_bits_array
[5];
148 unsigned depth_buffer_factor
;
149 unsigned msaa_samples_max
;
151 struct pipe_screen
*p_screen
= screen
->base
.screen
;
152 boolean pf_z16
, pf_x8z24
, pf_z24x8
, pf_s8z24
, pf_z24s8
, pf_z32
;
153 boolean mixed_color_depth
;
155 static const GLenum back_buffer_modes
[] = {
156 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
, GLX_SWAP_COPY_OML
159 if (driQueryOptionb(&screen
->optionCache
, "always_have_depth_buffer")) {
160 /* all visuals will have a depth buffer */
161 depth_buffer_factor
= 0;
164 depth_bits_array
[0] = 0;
165 stencil_bits_array
[0] = 0;
166 depth_buffer_factor
= 1;
169 msaa_samples_max
= (screen
->st_api
->feature_mask
& ST_API_FEATURE_MS_VISUALS_MASK
)
170 ? MSAA_VISUAL_MAX_SAMPLES
: 1;
172 pf_x8z24
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z24X8_UNORM
,
174 PIPE_BIND_DEPTH_STENCIL
);
175 pf_z24x8
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_X8Z24_UNORM
,
177 PIPE_BIND_DEPTH_STENCIL
);
178 pf_s8z24
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z24_UNORM_S8_UINT
,
180 PIPE_BIND_DEPTH_STENCIL
);
181 pf_z24s8
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_S8_UINT_Z24_UNORM
,
183 PIPE_BIND_DEPTH_STENCIL
);
184 pf_z16
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z16_UNORM
,
186 PIPE_BIND_DEPTH_STENCIL
);
187 pf_z32
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z32_UNORM
,
189 PIPE_BIND_DEPTH_STENCIL
);
192 depth_bits_array
[depth_buffer_factor
] = 16;
193 stencil_bits_array
[depth_buffer_factor
++] = 0;
195 if (pf_x8z24
|| pf_z24x8
) {
196 depth_bits_array
[depth_buffer_factor
] = 24;
197 stencil_bits_array
[depth_buffer_factor
++] = 0;
198 screen
->d_depth_bits_last
= pf_x8z24
;
200 if (pf_s8z24
|| pf_z24s8
) {
201 depth_bits_array
[depth_buffer_factor
] = 24;
202 stencil_bits_array
[depth_buffer_factor
++] = 8;
203 screen
->sd_depth_bits_last
= pf_s8z24
;
206 depth_bits_array
[depth_buffer_factor
] = 32;
207 stencil_bits_array
[depth_buffer_factor
++] = 0;
211 p_screen
->get_param(p_screen
, PIPE_CAP_MIXED_COLOR_DEPTH_BITS
);
213 assert(ARRAY_SIZE(mesa_formats
) == ARRAY_SIZE(pipe_formats
));
215 /* Expose only BGRA ordering if the loader doesn't support RGBA ordering. */
216 unsigned num_formats
;
217 if (dri_loader_get_cap(screen
, DRI_LOADER_CAP_RGBA_ORDERING
))
218 num_formats
= ARRAY_SIZE(mesa_formats
);
223 for (format
= 0; format
< num_formats
; format
++) {
224 __DRIconfig
**new_configs
= NULL
;
225 unsigned num_msaa_modes
= 0; /* includes a single-sample mode */
226 uint8_t msaa_modes
[MSAA_VISUAL_MAX_SAMPLES
];
228 if (!p_screen
->is_format_supported(p_screen
, pipe_formats
[format
],
230 PIPE_BIND_RENDER_TARGET
))
233 for (i
= 1; i
<= msaa_samples_max
; i
++) {
234 int samples
= i
> 1 ? i
: 0;
236 if (p_screen
->is_format_supported(p_screen
, pipe_formats
[format
],
237 PIPE_TEXTURE_2D
, samples
,
238 PIPE_BIND_RENDER_TARGET
)) {
239 msaa_modes
[num_msaa_modes
++] = samples
;
243 if (num_msaa_modes
) {
244 /* Single-sample configs with an accumulation buffer. */
245 new_configs
= driCreateConfigs(mesa_formats
[format
],
246 depth_bits_array
, stencil_bits_array
,
247 depth_buffer_factor
, back_buffer_modes
,
248 ARRAY_SIZE(back_buffer_modes
),
250 GL_TRUE
, !mixed_color_depth
);
251 configs
= driConcatConfigs(configs
, new_configs
);
253 /* Multi-sample configs without an accumulation buffer. */
254 if (num_msaa_modes
> 1) {
255 new_configs
= driCreateConfigs(mesa_formats
[format
],
256 depth_bits_array
, stencil_bits_array
,
257 depth_buffer_factor
, back_buffer_modes
,
258 ARRAY_SIZE(back_buffer_modes
),
259 msaa_modes
+1, num_msaa_modes
-1,
260 GL_FALSE
, !mixed_color_depth
);
261 configs
= driConcatConfigs(configs
, new_configs
);
266 if (configs
== NULL
) {
267 debug_printf("%s: driCreateConfigs failed\n", __FUNCTION__
);
271 return (const __DRIconfig
**)configs
;
275 * Roughly the converse of dri_fill_in_modes.
278 dri_fill_st_visual(struct st_visual
*stvis
, struct dri_screen
*screen
,
279 const struct gl_config
*mode
)
281 memset(stvis
, 0, sizeof(*stvis
));
286 /* Deduce the color format. */
287 switch (mode
->redMask
) {
289 if (mode
->alphaMask
) {
290 assert(mode
->alphaMask
== 0xFF000000);
291 stvis
->color_format
= mode
->sRGBCapable
?
292 PIPE_FORMAT_BGRA8888_SRGB
:
293 PIPE_FORMAT_BGRA8888_UNORM
;
295 stvis
->color_format
= mode
->sRGBCapable
?
296 PIPE_FORMAT_BGRX8888_SRGB
:
297 PIPE_FORMAT_BGRX8888_UNORM
;
302 if (mode
->alphaMask
) {
303 assert(mode
->alphaMask
== 0xFF000000);
304 stvis
->color_format
= mode
->sRGBCapable
?
305 PIPE_FORMAT_RGBA8888_SRGB
:
306 PIPE_FORMAT_RGBA8888_UNORM
;
308 stvis
->color_format
= mode
->sRGBCapable
?
309 PIPE_FORMAT_RGBX8888_SRGB
:
310 PIPE_FORMAT_RGBX8888_UNORM
;
315 stvis
->color_format
= PIPE_FORMAT_B5G6R5_UNORM
;
319 assert(!"unsupported visual: invalid red mask");
323 if (mode
->sampleBuffers
) {
324 stvis
->samples
= mode
->samples
;
327 switch (mode
->depthBits
) {
330 stvis
->depth_stencil_format
= PIPE_FORMAT_NONE
;
333 stvis
->depth_stencil_format
= PIPE_FORMAT_Z16_UNORM
;
336 if (mode
->stencilBits
== 0) {
337 stvis
->depth_stencil_format
= (screen
->d_depth_bits_last
) ?
338 PIPE_FORMAT_Z24X8_UNORM
:
339 PIPE_FORMAT_X8Z24_UNORM
;
341 stvis
->depth_stencil_format
= (screen
->sd_depth_bits_last
) ?
342 PIPE_FORMAT_Z24_UNORM_S8_UINT
:
343 PIPE_FORMAT_S8_UINT_Z24_UNORM
;
347 stvis
->depth_stencil_format
= PIPE_FORMAT_Z32_UNORM
;
351 stvis
->accum_format
= (mode
->haveAccumBuffer
) ?
352 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
354 stvis
->buffer_mask
|= ST_ATTACHMENT_FRONT_LEFT_MASK
;
355 stvis
->render_buffer
= ST_ATTACHMENT_FRONT_LEFT
;
356 if (mode
->doubleBufferMode
) {
357 stvis
->buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
358 stvis
->render_buffer
= ST_ATTACHMENT_BACK_LEFT
;
360 if (mode
->stereoMode
) {
361 stvis
->buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
362 if (mode
->doubleBufferMode
)
363 stvis
->buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
366 if (mode
->haveDepthBuffer
|| mode
->haveStencilBuffer
)
367 stvis
->buffer_mask
|= ST_ATTACHMENT_DEPTH_STENCIL_MASK
;
368 /* let the state tracker allocate the accum buffer */
372 dri_get_egl_image(struct st_manager
*smapi
,
374 struct st_egl_image
*stimg
)
376 struct dri_screen
*screen
= (struct dri_screen
*)smapi
;
377 __DRIimage
*img
= NULL
;
379 if (screen
->lookup_egl_image
) {
380 img
= screen
->lookup_egl_image(screen
, egl_image
);
386 stimg
->texture
= NULL
;
387 pipe_resource_reference(&stimg
->texture
, img
->texture
);
388 switch (img
->dri_components
) {
389 case __DRI_IMAGE_COMPONENTS_Y_U_V
:
390 stimg
->format
= PIPE_FORMAT_IYUV
;
392 case __DRI_IMAGE_COMPONENTS_Y_UV
:
393 stimg
->format
= PIPE_FORMAT_NV12
;
396 stimg
->format
= img
->texture
->format
;
399 stimg
->level
= img
->level
;
400 stimg
->layer
= img
->layer
;
406 dri_get_param(struct st_manager
*smapi
,
407 enum st_manager_param param
)
409 struct dri_screen
*screen
= (struct dri_screen
*)smapi
;
412 case ST_MANAGER_BROKEN_INVALIDATE
:
413 return screen
->broken_invalidate
;
420 dri_destroy_option_cache(struct dri_screen
* screen
)
424 if (screen
->optionCache
.info
) {
425 for (i
= 0; i
< (1 << screen
->optionCache
.tableSize
); ++i
) {
426 free(screen
->optionCache
.info
[i
].name
);
427 free(screen
->optionCache
.info
[i
].ranges
);
429 free(screen
->optionCache
.info
);
432 free(screen
->optionCache
.values
);
434 /* Default values are copied to screen->optionCache->values in
435 * initOptionCache. The info field, however, is a pointer copy, so don't free
438 free(screen
->optionCacheDefaults
.values
);
442 dri_destroy_screen_helper(struct dri_screen
* screen
)
444 if (screen
->base
.destroy
)
445 screen
->base
.destroy(&screen
->base
);
447 if (screen
->st_api
&& screen
->st_api
->destroy
)
448 screen
->st_api
->destroy(screen
->st_api
);
450 if (screen
->base
.screen
)
451 screen
->base
.screen
->destroy(screen
->base
.screen
);
453 dri_destroy_option_cache(screen
);
454 mtx_destroy(&screen
->opencl_func_mutex
);
458 dri_destroy_screen(__DRIscreen
* sPriv
)
460 struct dri_screen
*screen
= dri_screen(sPriv
);
462 dri_destroy_screen_helper(screen
);
464 pipe_loader_release(&screen
->dev
, 1);
467 sPriv
->driverPrivate
= NULL
;
468 sPriv
->extensions
= NULL
;
472 dri_postprocessing_init(struct dri_screen
*screen
)
476 for (i
= 0; i
< PP_FILTERS
; i
++) {
477 screen
->pp_enabled
[i
] = driQueryOptioni(&screen
->optionCache
,
483 dri_set_background_context(struct st_context_iface
*st
,
484 struct util_queue_monitoring
*queue_info
)
486 struct dri_context
*ctx
= (struct dri_context
*)st
->st_manager_private
;
487 const __DRIbackgroundCallableExtension
*backgroundCallable
=
488 ctx
->sPriv
->dri2
.backgroundCallable
;
490 /* Note: Mesa will only call this function if GL multithreading is enabled
491 * We only do that if the loader exposed the __DRI_BACKGROUND_CALLABLE
492 * extension. So we know that backgroundCallable is not NULL.
494 assert(backgroundCallable
);
495 backgroundCallable
->setBackgroundContext(ctx
->cPriv
->loaderPrivate
);
498 hud_add_queue_for_monitoring(ctx
->hud
, queue_info
);
502 dri_init_options_get_screen_flags(struct dri_screen
*screen
,
503 const char* driver_name
)
507 driParseOptionInfo(&screen
->optionCacheDefaults
, gallium_config_options
.xml
);
508 driParseConfigFiles(&screen
->optionCache
,
509 &screen
->optionCacheDefaults
,
510 screen
->sPriv
->myNum
,
512 dri_fill_st_options(screen
);
514 if (driQueryOptionb(&screen
->optionCache
,
515 "glsl_correct_derivatives_after_discard"))
516 flags
|= PIPE_SCREEN_ENABLE_CORRECT_TGSI_DERIVATIVES_AFTER_KILL
;
522 dri_init_screen_helper(struct dri_screen
*screen
,
523 struct pipe_screen
*pscreen
)
525 screen
->base
.screen
= pscreen
;
526 screen
->base
.get_egl_image
= dri_get_egl_image
;
527 screen
->base
.get_param
= dri_get_param
;
528 screen
->base
.set_background_context
= dri_set_background_context
;
530 screen
->st_api
= st_gl_api_create();
534 if(pscreen
->get_param(pscreen
, PIPE_CAP_NPOT_TEXTURES
))
535 screen
->target
= PIPE_TEXTURE_2D
;
537 screen
->target
= PIPE_TEXTURE_RECT
;
539 /* Handle force_s3tc_enable. */
540 if (!util_format_s3tc_enabled
&& screen
->options
.force_s3tc_enable
) {
541 /* Ensure libtxc_dxtn has been loaded if available.
542 * Forcing S3TC on before calling this would prevent loading
544 * This is just a precaution, the driver should have called it
547 util_format_s3tc_init();
549 util_format_s3tc_enabled
= TRUE
;
552 dri_postprocessing_init(screen
);
554 screen
->st_api
->query_versions(screen
->st_api
, &screen
->base
,
556 &screen
->sPriv
->max_gl_core_version
,
557 &screen
->sPriv
->max_gl_compat_version
,
558 &screen
->sPriv
->max_gl_es1_version
,
559 &screen
->sPriv
->max_gl_es2_version
);
561 return dri_fill_in_modes(screen
);
564 /* vim: set sw=3 ts=8 sts=3 expandtab: */