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