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")
85 DRI_CONF_SECTION_MISCELLANEOUS
86 DRI_CONF_ALWAYS_HAVE_DEPTH_BUFFER("false")
87 DRI_CONF_GLSL_ZERO_INIT("false")
95 dri_fill_st_options(struct dri_screen
*screen
)
97 struct st_config_options
*options
= &screen
->options
;
98 const struct driOptionCache
*optionCache
= &screen
->optionCache
;
100 options
->disable_blend_func_extended
=
101 driQueryOptionb(optionCache
, "disable_blend_func_extended");
102 options
->disable_glsl_line_continuations
=
103 driQueryOptionb(optionCache
, "disable_glsl_line_continuations");
104 options
->disable_shader_bit_encoding
=
105 driQueryOptionb(optionCache
, "disable_shader_bit_encoding");
106 options
->force_glsl_extensions_warn
=
107 driQueryOptionb(optionCache
, "force_glsl_extensions_warn");
108 options
->force_glsl_version
=
109 driQueryOptioni(optionCache
, "force_glsl_version");
110 options
->force_s3tc_enable
=
111 driQueryOptionb(optionCache
, "force_s3tc_enable");
112 options
->allow_glsl_extension_directive_midshader
=
113 driQueryOptionb(optionCache
, "allow_glsl_extension_directive_midshader");
114 options
->allow_glsl_builtin_variable_redeclaration
=
115 driQueryOptionb(optionCache
, "allow_glsl_builtin_variable_redeclaration");
116 options
->allow_higher_compat_version
=
117 driQueryOptionb(optionCache
, "allow_higher_compat_version");
118 options
->glsl_zero_init
= driQueryOptionb(optionCache
, "glsl_zero_init");
119 options
->force_glsl_abs_sqrt
=
120 driQueryOptionb(optionCache
, "force_glsl_abs_sqrt");
122 driComputeOptionsSha1(optionCache
, options
->config_options_sha1
);
125 static const __DRIconfig
**
126 dri_fill_in_modes(struct dri_screen
*screen
)
128 static const mesa_format mesa_formats
[] = {
129 MESA_FORMAT_B8G8R8A8_UNORM
,
130 MESA_FORMAT_B8G8R8X8_UNORM
,
131 MESA_FORMAT_B8G8R8A8_SRGB
,
132 MESA_FORMAT_B8G8R8X8_SRGB
,
133 MESA_FORMAT_B5G6R5_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
,
143 __DRIconfig
**configs
= NULL
;
144 uint8_t depth_bits_array
[5];
145 uint8_t stencil_bits_array
[5];
146 unsigned depth_buffer_factor
;
147 unsigned msaa_samples_max
;
149 struct pipe_screen
*p_screen
= screen
->base
.screen
;
150 boolean pf_z16
, pf_x8z24
, pf_z24x8
, pf_s8z24
, pf_z24s8
, pf_z32
;
151 boolean mixed_color_depth
;
153 static const GLenum back_buffer_modes
[] = {
154 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
, GLX_SWAP_COPY_OML
157 if (driQueryOptionb(&screen
->optionCache
, "always_have_depth_buffer")) {
158 /* all visuals will have a depth buffer */
159 depth_buffer_factor
= 0;
162 depth_bits_array
[0] = 0;
163 stencil_bits_array
[0] = 0;
164 depth_buffer_factor
= 1;
167 msaa_samples_max
= (screen
->st_api
->feature_mask
& ST_API_FEATURE_MS_VISUALS_MASK
)
168 ? MSAA_VISUAL_MAX_SAMPLES
: 1;
170 pf_x8z24
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z24X8_UNORM
,
172 PIPE_BIND_DEPTH_STENCIL
);
173 pf_z24x8
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_X8Z24_UNORM
,
175 PIPE_BIND_DEPTH_STENCIL
);
176 pf_s8z24
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z24_UNORM_S8_UINT
,
178 PIPE_BIND_DEPTH_STENCIL
);
179 pf_z24s8
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_S8_UINT_Z24_UNORM
,
181 PIPE_BIND_DEPTH_STENCIL
);
182 pf_z16
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z16_UNORM
,
184 PIPE_BIND_DEPTH_STENCIL
);
185 pf_z32
= p_screen
->is_format_supported(p_screen
, PIPE_FORMAT_Z32_UNORM
,
187 PIPE_BIND_DEPTH_STENCIL
);
190 depth_bits_array
[depth_buffer_factor
] = 16;
191 stencil_bits_array
[depth_buffer_factor
++] = 0;
193 if (pf_x8z24
|| pf_z24x8
) {
194 depth_bits_array
[depth_buffer_factor
] = 24;
195 stencil_bits_array
[depth_buffer_factor
++] = 0;
196 screen
->d_depth_bits_last
= pf_x8z24
;
198 if (pf_s8z24
|| pf_z24s8
) {
199 depth_bits_array
[depth_buffer_factor
] = 24;
200 stencil_bits_array
[depth_buffer_factor
++] = 8;
201 screen
->sd_depth_bits_last
= pf_s8z24
;
204 depth_bits_array
[depth_buffer_factor
] = 32;
205 stencil_bits_array
[depth_buffer_factor
++] = 0;
209 p_screen
->get_param(p_screen
, PIPE_CAP_MIXED_COLOR_DEPTH_BITS
);
211 assert(ARRAY_SIZE(mesa_formats
) == ARRAY_SIZE(pipe_formats
));
214 for (format
= 0; format
< ARRAY_SIZE(mesa_formats
); format
++) {
215 __DRIconfig
**new_configs
= NULL
;
216 unsigned num_msaa_modes
= 0; /* includes a single-sample mode */
217 uint8_t msaa_modes
[MSAA_VISUAL_MAX_SAMPLES
];
219 if (!p_screen
->is_format_supported(p_screen
, pipe_formats
[format
],
221 PIPE_BIND_RENDER_TARGET
))
224 for (i
= 1; i
<= msaa_samples_max
; i
++) {
225 int samples
= i
> 1 ? i
: 0;
227 if (p_screen
->is_format_supported(p_screen
, pipe_formats
[format
],
228 PIPE_TEXTURE_2D
, samples
,
229 PIPE_BIND_RENDER_TARGET
)) {
230 msaa_modes
[num_msaa_modes
++] = samples
;
234 if (num_msaa_modes
) {
235 /* Single-sample configs with an accumulation buffer. */
236 new_configs
= driCreateConfigs(mesa_formats
[format
],
237 depth_bits_array
, stencil_bits_array
,
238 depth_buffer_factor
, back_buffer_modes
,
239 ARRAY_SIZE(back_buffer_modes
),
241 GL_TRUE
, !mixed_color_depth
);
242 configs
= driConcatConfigs(configs
, new_configs
);
244 /* Multi-sample configs without an accumulation buffer. */
245 if (num_msaa_modes
> 1) {
246 new_configs
= driCreateConfigs(mesa_formats
[format
],
247 depth_bits_array
, stencil_bits_array
,
248 depth_buffer_factor
, back_buffer_modes
,
249 ARRAY_SIZE(back_buffer_modes
),
250 msaa_modes
+1, num_msaa_modes
-1,
251 GL_FALSE
, !mixed_color_depth
);
252 configs
= driConcatConfigs(configs
, new_configs
);
257 if (configs
== NULL
) {
258 debug_printf("%s: driCreateConfigs failed\n", __FUNCTION__
);
262 return (const __DRIconfig
**)configs
;
266 * Roughly the converse of dri_fill_in_modes.
269 dri_fill_st_visual(struct st_visual
*stvis
, struct dri_screen
*screen
,
270 const struct gl_config
*mode
)
272 memset(stvis
, 0, sizeof(*stvis
));
277 if (mode
->redBits
== 8) {
278 if (mode
->alphaBits
== 8)
279 if (mode
->sRGBCapable
)
280 stvis
->color_format
= PIPE_FORMAT_BGRA8888_SRGB
;
282 stvis
->color_format
= PIPE_FORMAT_BGRA8888_UNORM
;
284 if (mode
->sRGBCapable
)
285 stvis
->color_format
= PIPE_FORMAT_BGRX8888_SRGB
;
287 stvis
->color_format
= PIPE_FORMAT_BGRX8888_UNORM
;
289 stvis
->color_format
= PIPE_FORMAT_B5G6R5_UNORM
;
292 if (mode
->sampleBuffers
) {
293 stvis
->samples
= mode
->samples
;
296 switch (mode
->depthBits
) {
299 stvis
->depth_stencil_format
= PIPE_FORMAT_NONE
;
302 stvis
->depth_stencil_format
= PIPE_FORMAT_Z16_UNORM
;
305 if (mode
->stencilBits
== 0) {
306 stvis
->depth_stencil_format
= (screen
->d_depth_bits_last
) ?
307 PIPE_FORMAT_Z24X8_UNORM
:
308 PIPE_FORMAT_X8Z24_UNORM
;
310 stvis
->depth_stencil_format
= (screen
->sd_depth_bits_last
) ?
311 PIPE_FORMAT_Z24_UNORM_S8_UINT
:
312 PIPE_FORMAT_S8_UINT_Z24_UNORM
;
316 stvis
->depth_stencil_format
= PIPE_FORMAT_Z32_UNORM
;
320 stvis
->accum_format
= (mode
->haveAccumBuffer
) ?
321 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
323 stvis
->buffer_mask
|= ST_ATTACHMENT_FRONT_LEFT_MASK
;
324 stvis
->render_buffer
= ST_ATTACHMENT_FRONT_LEFT
;
325 if (mode
->doubleBufferMode
) {
326 stvis
->buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
327 stvis
->render_buffer
= ST_ATTACHMENT_BACK_LEFT
;
329 if (mode
->stereoMode
) {
330 stvis
->buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
331 if (mode
->doubleBufferMode
)
332 stvis
->buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
335 if (mode
->haveDepthBuffer
|| mode
->haveStencilBuffer
)
336 stvis
->buffer_mask
|= ST_ATTACHMENT_DEPTH_STENCIL_MASK
;
337 /* let the state tracker allocate the accum buffer */
341 dri_get_egl_image(struct st_manager
*smapi
,
343 struct st_egl_image
*stimg
)
345 struct dri_screen
*screen
= (struct dri_screen
*)smapi
;
346 __DRIimage
*img
= NULL
;
348 if (screen
->lookup_egl_image
) {
349 img
= screen
->lookup_egl_image(screen
, egl_image
);
355 stimg
->texture
= NULL
;
356 pipe_resource_reference(&stimg
->texture
, img
->texture
);
357 switch (img
->dri_components
) {
358 case __DRI_IMAGE_COMPONENTS_Y_U_V
:
359 stimg
->format
= PIPE_FORMAT_IYUV
;
361 case __DRI_IMAGE_COMPONENTS_Y_UV
:
362 stimg
->format
= PIPE_FORMAT_NV12
;
365 stimg
->format
= img
->texture
->format
;
368 stimg
->level
= img
->level
;
369 stimg
->layer
= img
->layer
;
375 dri_get_param(struct st_manager
*smapi
,
376 enum st_manager_param param
)
378 struct dri_screen
*screen
= (struct dri_screen
*)smapi
;
381 case ST_MANAGER_BROKEN_INVALIDATE
:
382 return screen
->broken_invalidate
;
389 dri_destroy_option_cache(struct dri_screen
* screen
)
393 if (screen
->optionCache
.info
) {
394 for (i
= 0; i
< (1 << screen
->optionCache
.tableSize
); ++i
) {
395 free(screen
->optionCache
.info
[i
].name
);
396 free(screen
->optionCache
.info
[i
].ranges
);
398 free(screen
->optionCache
.info
);
401 free(screen
->optionCache
.values
);
403 /* Default values are copied to screen->optionCache->values in
404 * initOptionCache. The info field, however, is a pointer copy, so don't free
407 free(screen
->optionCacheDefaults
.values
);
411 dri_destroy_screen_helper(struct dri_screen
* screen
)
413 if (screen
->st_api
&& screen
->st_api
->destroy
)
414 screen
->st_api
->destroy(screen
->st_api
);
416 if (screen
->base
.screen
)
417 screen
->base
.screen
->destroy(screen
->base
.screen
);
419 dri_destroy_option_cache(screen
);
420 mtx_destroy(&screen
->opencl_func_mutex
);
424 dri_destroy_screen(__DRIscreen
* sPriv
)
426 struct dri_screen
*screen
= dri_screen(sPriv
);
428 dri_destroy_screen_helper(screen
);
430 pipe_loader_release(&screen
->dev
, 1);
433 sPriv
->driverPrivate
= NULL
;
434 sPriv
->extensions
= NULL
;
438 dri_postprocessing_init(struct dri_screen
*screen
)
442 for (i
= 0; i
< PP_FILTERS
; i
++) {
443 screen
->pp_enabled
[i
] = driQueryOptioni(&screen
->optionCache
,
449 dri_set_background_context(struct st_context_iface
*st
)
451 struct dri_context
*ctx
= (struct dri_context
*)st
->st_manager_private
;
452 const __DRIbackgroundCallableExtension
*backgroundCallable
=
453 ctx
->sPriv
->dri2
.backgroundCallable
;
455 /* Note: Mesa will only call this function if GL multithreading is enabled
456 * We only do that if the loader exposed the __DRI_BACKGROUND_CALLABLE
457 * extension. So we know that backgroundCallable is not NULL.
459 assert(backgroundCallable
);
460 backgroundCallable
->setBackgroundContext(ctx
->cPriv
->loaderPrivate
);
464 dri_init_screen_helper(struct dri_screen
*screen
,
465 struct pipe_screen
*pscreen
,
466 const char* driver_name
)
468 screen
->base
.screen
= pscreen
;
469 screen
->base
.get_egl_image
= dri_get_egl_image
;
470 screen
->base
.get_param
= dri_get_param
;
471 screen
->base
.set_background_context
= dri_set_background_context
;
473 screen
->st_api
= st_gl_api_create();
477 if(pscreen
->get_param(pscreen
, PIPE_CAP_NPOT_TEXTURES
))
478 screen
->target
= PIPE_TEXTURE_2D
;
480 screen
->target
= PIPE_TEXTURE_RECT
;
482 driParseOptionInfo(&screen
->optionCacheDefaults
, gallium_config_options
.xml
);
484 driParseConfigFiles(&screen
->optionCache
,
485 &screen
->optionCacheDefaults
,
486 screen
->sPriv
->myNum
,
489 dri_fill_st_options(screen
);
491 /* Handle force_s3tc_enable. */
492 if (!util_format_s3tc_enabled
&& screen
->options
.force_s3tc_enable
) {
493 /* Ensure libtxc_dxtn has been loaded if available.
494 * Forcing S3TC on before calling this would prevent loading
496 * This is just a precaution, the driver should have called it
499 util_format_s3tc_init();
501 util_format_s3tc_enabled
= TRUE
;
504 dri_postprocessing_init(screen
);
506 screen
->st_api
->query_versions(screen
->st_api
, &screen
->base
,
508 &screen
->sPriv
->max_gl_core_version
,
509 &screen
->sPriv
->max_gl_compat_version
,
510 &screen
->sPriv
->max_gl_es1_version
,
511 &screen
->sPriv
->max_gl_es2_version
);
513 return dri_fill_in_modes(screen
);
516 /* vim: set sw=3 ts=8 sts=3 expandtab: */