Home | History | Annotate | Download | only in state_tracker
      1 /**************************************************************************
      2  *
      3  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
      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 TUNGSTEN GRAPHICS 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  /*
     29   * Authors:
     30   *   Keith Whitwell <keith (at) tungstengraphics.com>
     31   *   Brian Paul
     32   */
     33 
     34 
     35 #include "main/macros.h"
     36 #include "main/mtypes.h"
     37 #include "main/glformats.h"
     38 #include "main/samplerobj.h"
     39 #include "main/texobj.h"
     40 
     41 #include "st_context.h"
     42 #include "st_cb_texture.h"
     43 #include "st_format.h"
     44 #include "st_atom.h"
     45 #include "st_texture.h"
     46 #include "pipe/p_context.h"
     47 #include "pipe/p_defines.h"
     48 
     49 #include "cso_cache/cso_context.h"
     50 
     51 
     52 /**
     53  * Convert GLenum texcoord wrap tokens to pipe tokens.
     54  */
     55 static GLuint
     56 gl_wrap_xlate(GLenum wrap)
     57 {
     58    switch (wrap) {
     59    case GL_REPEAT:
     60       return PIPE_TEX_WRAP_REPEAT;
     61    case GL_CLAMP:
     62       return PIPE_TEX_WRAP_CLAMP;
     63    case GL_CLAMP_TO_EDGE:
     64       return PIPE_TEX_WRAP_CLAMP_TO_EDGE;
     65    case GL_CLAMP_TO_BORDER:
     66       return PIPE_TEX_WRAP_CLAMP_TO_BORDER;
     67    case GL_MIRRORED_REPEAT:
     68       return PIPE_TEX_WRAP_MIRROR_REPEAT;
     69    case GL_MIRROR_CLAMP_EXT:
     70       return PIPE_TEX_WRAP_MIRROR_CLAMP;
     71    case GL_MIRROR_CLAMP_TO_EDGE_EXT:
     72       return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE;
     73    case GL_MIRROR_CLAMP_TO_BORDER_EXT:
     74       return PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER;
     75    default:
     76       assert(0);
     77       return 0;
     78    }
     79 }
     80 
     81 
     82 static GLuint
     83 gl_filter_to_mip_filter(GLenum filter)
     84 {
     85    switch (filter) {
     86    case GL_NEAREST:
     87    case GL_LINEAR:
     88       return PIPE_TEX_MIPFILTER_NONE;
     89 
     90    case GL_NEAREST_MIPMAP_NEAREST:
     91    case GL_LINEAR_MIPMAP_NEAREST:
     92       return PIPE_TEX_MIPFILTER_NEAREST;
     93 
     94    case GL_NEAREST_MIPMAP_LINEAR:
     95    case GL_LINEAR_MIPMAP_LINEAR:
     96       return PIPE_TEX_MIPFILTER_LINEAR;
     97 
     98    default:
     99       assert(0);
    100       return PIPE_TEX_MIPFILTER_NONE;
    101    }
    102 }
    103 
    104 
    105 static GLuint
    106 gl_filter_to_img_filter(GLenum filter)
    107 {
    108    switch (filter) {
    109    case GL_NEAREST:
    110    case GL_NEAREST_MIPMAP_NEAREST:
    111    case GL_NEAREST_MIPMAP_LINEAR:
    112       return PIPE_TEX_FILTER_NEAREST;
    113 
    114    case GL_LINEAR:
    115    case GL_LINEAR_MIPMAP_NEAREST:
    116    case GL_LINEAR_MIPMAP_LINEAR:
    117       return PIPE_TEX_FILTER_LINEAR;
    118 
    119    default:
    120       assert(0);
    121       return PIPE_TEX_FILTER_NEAREST;
    122    }
    123 }
    124 
    125 
    126 static void
    127 convert_sampler(struct st_context *st,
    128                 struct pipe_sampler_state *sampler,
    129                 GLuint texUnit)
    130 {
    131    struct gl_texture_object *texobj;
    132    struct gl_context *ctx = st->ctx;
    133    struct gl_sampler_object *msamp;
    134 
    135    texobj = ctx->Texture.Unit[texUnit]._Current;
    136    if (!texobj) {
    137       texobj = _mesa_get_fallback_texture(ctx, TEXTURE_2D_INDEX);
    138    }
    139 
    140    msamp = _mesa_get_samplerobj(ctx, texUnit);
    141 
    142    memset(sampler, 0, sizeof(*sampler));
    143    sampler->wrap_s = gl_wrap_xlate(msamp->WrapS);
    144    sampler->wrap_t = gl_wrap_xlate(msamp->WrapT);
    145    sampler->wrap_r = gl_wrap_xlate(msamp->WrapR);
    146 
    147    sampler->min_img_filter = gl_filter_to_img_filter(msamp->MinFilter);
    148    sampler->min_mip_filter = gl_filter_to_mip_filter(msamp->MinFilter);
    149    sampler->mag_img_filter = gl_filter_to_img_filter(msamp->MagFilter);
    150 
    151    if (texobj->Target != GL_TEXTURE_RECTANGLE_ARB)
    152       sampler->normalized_coords = 1;
    153 
    154    sampler->lod_bias = ctx->Texture.Unit[texUnit].LodBias + msamp->LodBias;
    155 
    156    sampler->min_lod = CLAMP(msamp->MinLod,
    157                             0.0f,
    158                             (GLfloat) texobj->MaxLevel - texobj->BaseLevel);
    159    sampler->max_lod = MIN2((GLfloat) texobj->MaxLevel - texobj->BaseLevel,
    160                            msamp->MaxLod);
    161    if (sampler->max_lod < sampler->min_lod) {
    162       /* The GL spec doesn't seem to specify what to do in this case.
    163        * Swap the values.
    164        */
    165       float tmp = sampler->max_lod;
    166       sampler->max_lod = sampler->min_lod;
    167       sampler->min_lod = tmp;
    168       assert(sampler->min_lod <= sampler->max_lod);
    169    }
    170 
    171    if (msamp->BorderColor.ui[0] ||
    172        msamp->BorderColor.ui[1] ||
    173        msamp->BorderColor.ui[2] ||
    174        msamp->BorderColor.ui[3]) {
    175       struct gl_texture_image *teximg;
    176       GLboolean is_integer = GL_FALSE;
    177 
    178       teximg = texobj->Image[0][texobj->BaseLevel];
    179 
    180       if (teximg) {
    181          is_integer = _mesa_is_enum_format_integer(teximg->InternalFormat);
    182       }
    183 
    184       st_translate_color(&msamp->BorderColor,
    185                          &sampler->border_color,
    186                          teximg ? teximg->_BaseFormat : GL_RGBA, is_integer);
    187    }
    188 
    189    sampler->max_anisotropy = (msamp->MaxAnisotropy == 1.0 ?
    190                               0 : (GLuint) msamp->MaxAnisotropy);
    191 
    192    /* only care about ARB_shadow, not SGI shadow */
    193    if (msamp->CompareMode == GL_COMPARE_R_TO_TEXTURE) {
    194       sampler->compare_mode = PIPE_TEX_COMPARE_R_TO_TEXTURE;
    195       sampler->compare_func
    196          = st_compare_func_to_pipe(msamp->CompareFunc);
    197    }
    198 
    199    sampler->seamless_cube_map =
    200       ctx->Texture.CubeMapSeamless || msamp->CubeMapSeamless;
    201 }
    202 
    203 
    204 /**
    205  * Update the gallium driver's sampler state for fragment, vertex or
    206  * geometry shader stage.
    207  */
    208 static void
    209 update_shader_samplers(struct st_context *st,
    210                        unsigned shader_stage,
    211                        const struct gl_program *prog,
    212                        unsigned max_units,
    213                        struct pipe_sampler_state *samplers,
    214                        unsigned *num_samplers)
    215 {
    216    GLuint unit;
    217    GLbitfield samplers_used;
    218    const GLuint old_max = *num_samplers;
    219 
    220    samplers_used = prog->SamplersUsed;
    221 
    222    if (*num_samplers == 0 && samplers_used == 0x0)
    223        return;
    224 
    225    *num_samplers = 0;
    226 
    227    /* loop over sampler units (aka tex image units) */
    228    for (unit = 0; unit < max_units; unit++, samplers_used >>= 1) {
    229       struct pipe_sampler_state *sampler = samplers + unit;
    230 
    231       if (samplers_used & 1) {
    232          const GLuint texUnit = prog->SamplerUnits[unit];
    233 
    234          convert_sampler(st, sampler, texUnit);
    235 
    236          *num_samplers = unit + 1;
    237 
    238          cso_single_sampler(st->cso_context, shader_stage, unit, sampler);
    239       }
    240       else if (samplers_used != 0 || unit < old_max) {
    241          cso_single_sampler(st->cso_context, shader_stage, unit, NULL);
    242       }
    243       else {
    244          /* if we've reset all the old samplers and we have no more new ones */
    245          break;
    246       }
    247    }
    248 
    249    cso_single_sampler_done(st->cso_context, shader_stage);
    250 }
    251 
    252 
    253 static void
    254 update_samplers(struct st_context *st)
    255 {
    256    const struct gl_context *ctx = st->ctx;
    257 
    258    update_shader_samplers(st,
    259                           PIPE_SHADER_FRAGMENT,
    260                           &ctx->FragmentProgram._Current->Base,
    261                           ctx->Const.MaxTextureImageUnits,
    262                           st->state.samplers[PIPE_SHADER_FRAGMENT],
    263                           &st->state.num_samplers[PIPE_SHADER_FRAGMENT]);
    264 
    265    update_shader_samplers(st,
    266                           PIPE_SHADER_VERTEX,
    267                           &ctx->VertexProgram._Current->Base,
    268                           ctx->Const.MaxVertexTextureImageUnits,
    269                           st->state.samplers[PIPE_SHADER_VERTEX],
    270                           &st->state.num_samplers[PIPE_SHADER_VERTEX]);
    271 
    272    if (ctx->GeometryProgram._Current) {
    273       update_shader_samplers(st,
    274                              PIPE_SHADER_GEOMETRY,
    275                              &ctx->GeometryProgram._Current->Base,
    276                              ctx->Const.MaxGeometryTextureImageUnits,
    277                              st->state.samplers[PIPE_SHADER_GEOMETRY],
    278                              &st->state.num_samplers[PIPE_SHADER_GEOMETRY]);
    279    }
    280 }
    281 
    282 
    283 const struct st_tracked_state st_update_sampler = {
    284    "st_update_sampler",					/* name */
    285    {							/* dirty */
    286       _NEW_TEXTURE,					/* mesa */
    287       0,						/* st */
    288    },
    289    update_samplers					/* update */
    290 };
    291