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