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