driconf: add force_glsl_abs_sqrt option
[mesa.git] / src / gallium / state_trackers / dri / dri_screen.c
1 /**************************************************************************
2 *
3 * Copyright 2009, VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27 /*
28 * Author: Keith Whitwell <keithw@vmware.com>
29 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
30 */
31
32 #include "utils.h"
33 #include "xmlpool.h"
34
35 #include "dri_screen.h"
36 #include "dri_context.h"
37
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"
44
45 #include "util/u_debug.h"
46 #include "util/u_format_s3tc.h"
47
48 #define MSAA_VISUAL_MAX_SAMPLES 32
49
50 #undef false
51
52 const __DRIconfigOptionsExtension gallium_config_options = {
53 .base = { __DRI_CONFIG_OPTIONS, 1 },
54 .xml =
55
56 DRI_CONF_BEGIN
57 DRI_CONF_SECTION_PERFORMANCE
58 DRI_CONF_MESA_GLTHREAD("false")
59 DRI_CONF_SECTION_END
60
61 DRI_CONF_SECTION_QUALITY
62 DRI_CONF_FORCE_S3TC_ENABLE("false")
63 DRI_CONF_PP_CELSHADE(0)
64 DRI_CONF_PP_NORED(0)
65 DRI_CONF_PP_NOGREEN(0)
66 DRI_CONF_PP_NOBLUE(0)
67 DRI_CONF_PP_JIMENEZMLAA(0, 0, 32)
68 DRI_CONF_PP_JIMENEZMLAA_COLOR(0, 0, 32)
69 DRI_CONF_SECTION_END
70
71 DRI_CONF_SECTION_DEBUG
72 DRI_CONF_FORCE_GLSL_EXTENSIONS_WARN("false")
73 DRI_CONF_DISABLE_GLSL_LINE_CONTINUATIONS("false")
74 DRI_CONF_DISABLE_BLEND_FUNC_EXTENDED("false")
75 DRI_CONF_DISABLE_SHADER_BIT_ENCODING("false")
76 DRI_CONF_FORCE_GLSL_VERSION(0)
77 DRI_CONF_ALLOW_GLSL_EXTENSION_DIRECTIVE_MIDSHADER("false")
78 DRI_CONF_ALLOW_HIGHER_COMPAT_VERSION("false")
79 DRI_CONF_FORCE_GLSL_ABS_SQRT("false")
80 DRI_CONF_SECTION_END
81
82 DRI_CONF_SECTION_MISCELLANEOUS
83 DRI_CONF_ALWAYS_HAVE_DEPTH_BUFFER("false")
84 DRI_CONF_GLSL_ZERO_INIT("false")
85 DRI_CONF_SECTION_END
86 DRI_CONF_END
87 };
88
89 #define false 0
90
91 static void
92 dri_fill_st_options(struct dri_screen *screen)
93 {
94 struct st_config_options *options = &screen->options;
95 const struct driOptionCache *optionCache = &screen->optionCache;
96
97 options->disable_blend_func_extended =
98 driQueryOptionb(optionCache, "disable_blend_func_extended");
99 options->disable_glsl_line_continuations =
100 driQueryOptionb(optionCache, "disable_glsl_line_continuations");
101 options->disable_shader_bit_encoding =
102 driQueryOptionb(optionCache, "disable_shader_bit_encoding");
103 options->force_glsl_extensions_warn =
104 driQueryOptionb(optionCache, "force_glsl_extensions_warn");
105 options->force_glsl_version =
106 driQueryOptioni(optionCache, "force_glsl_version");
107 options->force_s3tc_enable =
108 driQueryOptionb(optionCache, "force_s3tc_enable");
109 options->allow_glsl_extension_directive_midshader =
110 driQueryOptionb(optionCache, "allow_glsl_extension_directive_midshader");
111 options->allow_higher_compat_version =
112 driQueryOptionb(optionCache, "allow_higher_compat_version");
113 options->glsl_zero_init = driQueryOptionb(optionCache, "glsl_zero_init");
114 options->force_glsl_abs_sqrt =
115 driQueryOptionb(optionCache, "force_glsl_abs_sqrt");
116
117 driComputeOptionsSha1(optionCache, options->config_options_sha1);
118 }
119
120 static const __DRIconfig **
121 dri_fill_in_modes(struct dri_screen *screen)
122 {
123 static const mesa_format mesa_formats[] = {
124 MESA_FORMAT_B8G8R8A8_UNORM,
125 MESA_FORMAT_B8G8R8X8_UNORM,
126 MESA_FORMAT_B8G8R8A8_SRGB,
127 MESA_FORMAT_B8G8R8X8_SRGB,
128 MESA_FORMAT_B5G6R5_UNORM,
129 };
130 static const enum pipe_format pipe_formats[] = {
131 PIPE_FORMAT_BGRA8888_UNORM,
132 PIPE_FORMAT_BGRX8888_UNORM,
133 PIPE_FORMAT_BGRA8888_SRGB,
134 PIPE_FORMAT_BGRX8888_SRGB,
135 PIPE_FORMAT_B5G6R5_UNORM,
136 };
137 mesa_format format;
138 __DRIconfig **configs = NULL;
139 uint8_t depth_bits_array[5];
140 uint8_t stencil_bits_array[5];
141 unsigned depth_buffer_factor;
142 unsigned msaa_samples_max;
143 unsigned i;
144 struct pipe_screen *p_screen = screen->base.screen;
145 boolean pf_z16, pf_x8z24, pf_z24x8, pf_s8z24, pf_z24s8, pf_z32;
146 boolean mixed_color_depth;
147
148 static const GLenum back_buffer_modes[] = {
149 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
150 };
151
152 if (driQueryOptionb(&screen->optionCache, "always_have_depth_buffer")) {
153 /* all visuals will have a depth buffer */
154 depth_buffer_factor = 0;
155 }
156 else {
157 depth_bits_array[0] = 0;
158 stencil_bits_array[0] = 0;
159 depth_buffer_factor = 1;
160 }
161
162 msaa_samples_max = (screen->st_api->feature_mask & ST_API_FEATURE_MS_VISUALS_MASK)
163 ? MSAA_VISUAL_MAX_SAMPLES : 1;
164
165 pf_x8z24 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24X8_UNORM,
166 PIPE_TEXTURE_2D, 0,
167 PIPE_BIND_DEPTH_STENCIL);
168 pf_z24x8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_X8Z24_UNORM,
169 PIPE_TEXTURE_2D, 0,
170 PIPE_BIND_DEPTH_STENCIL);
171 pf_s8z24 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24_UNORM_S8_UINT,
172 PIPE_TEXTURE_2D, 0,
173 PIPE_BIND_DEPTH_STENCIL);
174 pf_z24s8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_S8_UINT_Z24_UNORM,
175 PIPE_TEXTURE_2D, 0,
176 PIPE_BIND_DEPTH_STENCIL);
177 pf_z16 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z16_UNORM,
178 PIPE_TEXTURE_2D, 0,
179 PIPE_BIND_DEPTH_STENCIL);
180 pf_z32 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z32_UNORM,
181 PIPE_TEXTURE_2D, 0,
182 PIPE_BIND_DEPTH_STENCIL);
183
184 if (pf_z16) {
185 depth_bits_array[depth_buffer_factor] = 16;
186 stencil_bits_array[depth_buffer_factor++] = 0;
187 }
188 if (pf_x8z24 || pf_z24x8) {
189 depth_bits_array[depth_buffer_factor] = 24;
190 stencil_bits_array[depth_buffer_factor++] = 0;
191 screen->d_depth_bits_last = pf_x8z24;
192 }
193 if (pf_s8z24 || pf_z24s8) {
194 depth_bits_array[depth_buffer_factor] = 24;
195 stencil_bits_array[depth_buffer_factor++] = 8;
196 screen->sd_depth_bits_last = pf_s8z24;
197 }
198 if (pf_z32) {
199 depth_bits_array[depth_buffer_factor] = 32;
200 stencil_bits_array[depth_buffer_factor++] = 0;
201 }
202
203 mixed_color_depth =
204 p_screen->get_param(p_screen, PIPE_CAP_MIXED_COLOR_DEPTH_BITS);
205
206 assert(ARRAY_SIZE(mesa_formats) == ARRAY_SIZE(pipe_formats));
207
208 /* Add configs. */
209 for (format = 0; format < ARRAY_SIZE(mesa_formats); format++) {
210 __DRIconfig **new_configs = NULL;
211 unsigned num_msaa_modes = 0; /* includes a single-sample mode */
212 uint8_t msaa_modes[MSAA_VISUAL_MAX_SAMPLES];
213
214 if (!p_screen->is_format_supported(p_screen, pipe_formats[format],
215 PIPE_TEXTURE_2D, 0,
216 PIPE_BIND_RENDER_TARGET))
217 continue;
218
219 for (i = 1; i <= msaa_samples_max; i++) {
220 int samples = i > 1 ? i : 0;
221
222 if (p_screen->is_format_supported(p_screen, pipe_formats[format],
223 PIPE_TEXTURE_2D, samples,
224 PIPE_BIND_RENDER_TARGET)) {
225 msaa_modes[num_msaa_modes++] = samples;
226 }
227 }
228
229 if (num_msaa_modes) {
230 /* Single-sample configs with an accumulation buffer. */
231 new_configs = driCreateConfigs(mesa_formats[format],
232 depth_bits_array, stencil_bits_array,
233 depth_buffer_factor, back_buffer_modes,
234 ARRAY_SIZE(back_buffer_modes),
235 msaa_modes, 1,
236 GL_TRUE, !mixed_color_depth);
237 configs = driConcatConfigs(configs, new_configs);
238
239 /* Multi-sample configs without an accumulation buffer. */
240 if (num_msaa_modes > 1) {
241 new_configs = driCreateConfigs(mesa_formats[format],
242 depth_bits_array, stencil_bits_array,
243 depth_buffer_factor, back_buffer_modes,
244 ARRAY_SIZE(back_buffer_modes),
245 msaa_modes+1, num_msaa_modes-1,
246 GL_FALSE, !mixed_color_depth);
247 configs = driConcatConfigs(configs, new_configs);
248 }
249 }
250 }
251
252 if (configs == NULL) {
253 debug_printf("%s: driCreateConfigs failed\n", __FUNCTION__);
254 return NULL;
255 }
256
257 return (const __DRIconfig **)configs;
258 }
259
260 /**
261 * Roughly the converse of dri_fill_in_modes.
262 */
263 void
264 dri_fill_st_visual(struct st_visual *stvis, struct dri_screen *screen,
265 const struct gl_config *mode)
266 {
267 memset(stvis, 0, sizeof(*stvis));
268
269 if (!mode)
270 return;
271
272 if (mode->redBits == 8) {
273 if (mode->alphaBits == 8)
274 if (mode->sRGBCapable)
275 stvis->color_format = PIPE_FORMAT_BGRA8888_SRGB;
276 else
277 stvis->color_format = PIPE_FORMAT_BGRA8888_UNORM;
278 else
279 if (mode->sRGBCapable)
280 stvis->color_format = PIPE_FORMAT_BGRX8888_SRGB;
281 else
282 stvis->color_format = PIPE_FORMAT_BGRX8888_UNORM;
283 } else {
284 stvis->color_format = PIPE_FORMAT_B5G6R5_UNORM;
285 }
286
287 if (mode->sampleBuffers) {
288 stvis->samples = mode->samples;
289 }
290
291 switch (mode->depthBits) {
292 default:
293 case 0:
294 stvis->depth_stencil_format = PIPE_FORMAT_NONE;
295 break;
296 case 16:
297 stvis->depth_stencil_format = PIPE_FORMAT_Z16_UNORM;
298 break;
299 case 24:
300 if (mode->stencilBits == 0) {
301 stvis->depth_stencil_format = (screen->d_depth_bits_last) ?
302 PIPE_FORMAT_Z24X8_UNORM:
303 PIPE_FORMAT_X8Z24_UNORM;
304 } else {
305 stvis->depth_stencil_format = (screen->sd_depth_bits_last) ?
306 PIPE_FORMAT_Z24_UNORM_S8_UINT:
307 PIPE_FORMAT_S8_UINT_Z24_UNORM;
308 }
309 break;
310 case 32:
311 stvis->depth_stencil_format = PIPE_FORMAT_Z32_UNORM;
312 break;
313 }
314
315 stvis->accum_format = (mode->haveAccumBuffer) ?
316 PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
317
318 stvis->buffer_mask |= ST_ATTACHMENT_FRONT_LEFT_MASK;
319 stvis->render_buffer = ST_ATTACHMENT_FRONT_LEFT;
320 if (mode->doubleBufferMode) {
321 stvis->buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
322 stvis->render_buffer = ST_ATTACHMENT_BACK_LEFT;
323 }
324 if (mode->stereoMode) {
325 stvis->buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
326 if (mode->doubleBufferMode)
327 stvis->buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
328 }
329
330 if (mode->haveDepthBuffer || mode->haveStencilBuffer)
331 stvis->buffer_mask |= ST_ATTACHMENT_DEPTH_STENCIL_MASK;
332 /* let the state tracker allocate the accum buffer */
333 }
334
335 static boolean
336 dri_get_egl_image(struct st_manager *smapi,
337 void *egl_image,
338 struct st_egl_image *stimg)
339 {
340 struct dri_screen *screen = (struct dri_screen *)smapi;
341 __DRIimage *img = NULL;
342
343 if (screen->lookup_egl_image) {
344 img = screen->lookup_egl_image(screen, egl_image);
345 }
346
347 if (!img)
348 return FALSE;
349
350 stimg->texture = NULL;
351 pipe_resource_reference(&stimg->texture, img->texture);
352 switch (img->dri_components) {
353 case __DRI_IMAGE_COMPONENTS_Y_U_V:
354 stimg->format = PIPE_FORMAT_IYUV;
355 break;
356 case __DRI_IMAGE_COMPONENTS_Y_UV:
357 stimg->format = PIPE_FORMAT_NV12;
358 break;
359 default:
360 stimg->format = img->texture->format;
361 break;
362 }
363 stimg->level = img->level;
364 stimg->layer = img->layer;
365
366 return TRUE;
367 }
368
369 static int
370 dri_get_param(struct st_manager *smapi,
371 enum st_manager_param param)
372 {
373 struct dri_screen *screen = (struct dri_screen *)smapi;
374
375 switch(param) {
376 case ST_MANAGER_BROKEN_INVALIDATE:
377 return screen->broken_invalidate;
378 default:
379 return 0;
380 }
381 }
382
383 static void
384 dri_destroy_option_cache(struct dri_screen * screen)
385 {
386 int i;
387
388 if (screen->optionCache.info) {
389 for (i = 0; i < (1 << screen->optionCache.tableSize); ++i) {
390 free(screen->optionCache.info[i].name);
391 free(screen->optionCache.info[i].ranges);
392 }
393 free(screen->optionCache.info);
394 }
395
396 free(screen->optionCache.values);
397
398 /* Default values are copied to screen->optionCache->values in
399 * initOptionCache. The info field, however, is a pointer copy, so don't free
400 * that twice.
401 */
402 free(screen->optionCacheDefaults.values);
403 }
404
405 void
406 dri_destroy_screen_helper(struct dri_screen * screen)
407 {
408 if (screen->st_api && screen->st_api->destroy)
409 screen->st_api->destroy(screen->st_api);
410
411 if (screen->base.screen)
412 screen->base.screen->destroy(screen->base.screen);
413
414 dri_destroy_option_cache(screen);
415 mtx_destroy(&screen->opencl_func_mutex);
416 }
417
418 void
419 dri_destroy_screen(__DRIscreen * sPriv)
420 {
421 struct dri_screen *screen = dri_screen(sPriv);
422
423 dri_destroy_screen_helper(screen);
424
425 pipe_loader_release(&screen->dev, 1);
426
427 free(screen);
428 sPriv->driverPrivate = NULL;
429 sPriv->extensions = NULL;
430 }
431
432 static void
433 dri_postprocessing_init(struct dri_screen *screen)
434 {
435 unsigned i;
436
437 for (i = 0; i < PP_FILTERS; i++) {
438 screen->pp_enabled[i] = driQueryOptioni(&screen->optionCache,
439 pp_filters[i].name);
440 }
441 }
442
443 static void
444 dri_set_background_context(struct st_context_iface *st)
445 {
446 struct dri_context *ctx = (struct dri_context *)st->st_manager_private;
447 const __DRIbackgroundCallableExtension *backgroundCallable =
448 ctx->sPriv->dri2.backgroundCallable;
449
450 /* Note: Mesa will only call this function if GL multithreading is enabled
451 * We only do that if the loader exposed the __DRI_BACKGROUND_CALLABLE
452 * extension. So we know that backgroundCallable is not NULL.
453 */
454 assert(backgroundCallable);
455 backgroundCallable->setBackgroundContext(ctx->cPriv->loaderPrivate);
456 }
457
458 const __DRIconfig **
459 dri_init_screen_helper(struct dri_screen *screen,
460 struct pipe_screen *pscreen,
461 const char* driver_name)
462 {
463 screen->base.screen = pscreen;
464 screen->base.get_egl_image = dri_get_egl_image;
465 screen->base.get_param = dri_get_param;
466 screen->base.set_background_context = dri_set_background_context;
467
468 screen->st_api = st_gl_api_create();
469 if (!screen->st_api)
470 return NULL;
471
472 if(pscreen->get_param(pscreen, PIPE_CAP_NPOT_TEXTURES))
473 screen->target = PIPE_TEXTURE_2D;
474 else
475 screen->target = PIPE_TEXTURE_RECT;
476
477 driParseOptionInfo(&screen->optionCacheDefaults, gallium_config_options.xml);
478
479 driParseConfigFiles(&screen->optionCache,
480 &screen->optionCacheDefaults,
481 screen->sPriv->myNum,
482 driver_name);
483
484 dri_fill_st_options(screen);
485
486 /* Handle force_s3tc_enable. */
487 if (!util_format_s3tc_enabled && screen->options.force_s3tc_enable) {
488 /* Ensure libtxc_dxtn has been loaded if available.
489 * Forcing S3TC on before calling this would prevent loading
490 * the library.
491 * This is just a precaution, the driver should have called it
492 * already.
493 */
494 util_format_s3tc_init();
495
496 util_format_s3tc_enabled = TRUE;
497 }
498
499 dri_postprocessing_init(screen);
500
501 screen->st_api->query_versions(screen->st_api, &screen->base,
502 &screen->options,
503 &screen->sPriv->max_gl_core_version,
504 &screen->sPriv->max_gl_compat_version,
505 &screen->sPriv->max_gl_es1_version,
506 &screen->sPriv->max_gl_es2_version);
507
508 return dri_fill_in_modes(screen);
509 }
510
511 /* vim: set sw=3 ts=8 sts=3 expandtab: */