Home | History | Annotate | Download | only in state_tracker
      1 /**************************************************************************
      2  *
      3  * Copyright 2007 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 #include <stdio.h>
     29 #include "main/bufferobj.h"
     30 #include "main/enums.h"
     31 #include "main/fbobject.h"
     32 #include "main/formats.h"
     33 #include "main/format_utils.h"
     34 #include "main/glformats.h"
     35 #include "main/image.h"
     36 #include "main/imports.h"
     37 #include "main/macros.h"
     38 #include "main/mipmap.h"
     39 #include "main/pack.h"
     40 #include "main/pbo.h"
     41 #include "main/pixeltransfer.h"
     42 #include "main/texcompress.h"
     43 #include "main/texcompress_etc.h"
     44 #include "main/texgetimage.h"
     45 #include "main/teximage.h"
     46 #include "main/texobj.h"
     47 #include "main/texstore.h"
     48 
     49 #include "state_tracker/st_debug.h"
     50 #include "state_tracker/st_context.h"
     51 #include "state_tracker/st_cb_bitmap.h"
     52 #include "state_tracker/st_cb_fbo.h"
     53 #include "state_tracker/st_cb_flush.h"
     54 #include "state_tracker/st_cb_texture.h"
     55 #include "state_tracker/st_cb_bufferobjects.h"
     56 #include "state_tracker/st_cb_memoryobjects.h"
     57 #include "state_tracker/st_format.h"
     58 #include "state_tracker/st_pbo.h"
     59 #include "state_tracker/st_texture.h"
     60 #include "state_tracker/st_gen_mipmap.h"
     61 #include "state_tracker/st_atom.h"
     62 #include "state_tracker/st_sampler_view.h"
     63 
     64 #include "pipe/p_context.h"
     65 #include "pipe/p_defines.h"
     66 #include "util/u_inlines.h"
     67 #include "util/u_upload_mgr.h"
     68 #include "pipe/p_shader_tokens.h"
     69 #include "util/u_tile.h"
     70 #include "util/u_format.h"
     71 #include "util/u_surface.h"
     72 #include "util/u_sampler.h"
     73 #include "util/u_math.h"
     74 #include "util/u_box.h"
     75 #include "util/u_simple_shaders.h"
     76 #include "cso_cache/cso_context.h"
     77 #include "tgsi/tgsi_ureg.h"
     78 
     79 #define DBG if (0) printf
     80 
     81 
     82 enum pipe_texture_target
     83 gl_target_to_pipe(GLenum target)
     84 {
     85    switch (target) {
     86    case GL_TEXTURE_1D:
     87    case GL_PROXY_TEXTURE_1D:
     88       return PIPE_TEXTURE_1D;
     89    case GL_TEXTURE_2D:
     90    case GL_PROXY_TEXTURE_2D:
     91    case GL_TEXTURE_EXTERNAL_OES:
     92    case GL_TEXTURE_2D_MULTISAMPLE:
     93    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
     94       return PIPE_TEXTURE_2D;
     95    case GL_TEXTURE_RECTANGLE_NV:
     96    case GL_PROXY_TEXTURE_RECTANGLE_NV:
     97       return PIPE_TEXTURE_RECT;
     98    case GL_TEXTURE_3D:
     99    case GL_PROXY_TEXTURE_3D:
    100       return PIPE_TEXTURE_3D;
    101    case GL_TEXTURE_CUBE_MAP_ARB:
    102    case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
    103    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
    104    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
    105    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
    106    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
    107    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
    108    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
    109       return PIPE_TEXTURE_CUBE;
    110    case GL_TEXTURE_1D_ARRAY_EXT:
    111    case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
    112       return PIPE_TEXTURE_1D_ARRAY;
    113    case GL_TEXTURE_2D_ARRAY_EXT:
    114    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
    115    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
    116    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
    117       return PIPE_TEXTURE_2D_ARRAY;
    118    case GL_TEXTURE_BUFFER:
    119       return PIPE_BUFFER;
    120    case GL_TEXTURE_CUBE_MAP_ARRAY:
    121    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
    122       return PIPE_TEXTURE_CUBE_ARRAY;
    123    default:
    124       assert(0);
    125       return 0;
    126    }
    127 }
    128 
    129 
    130 /** called via ctx->Driver.NewTextureImage() */
    131 static struct gl_texture_image *
    132 st_NewTextureImage(struct gl_context * ctx)
    133 {
    134    DBG("%s\n", __func__);
    135    (void) ctx;
    136    return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
    137 }
    138 
    139 
    140 /** called via ctx->Driver.DeleteTextureImage() */
    141 static void
    142 st_DeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
    143 {
    144    /* nothing special (yet) for st_texture_image */
    145    _mesa_delete_texture_image(ctx, img);
    146 }
    147 
    148 
    149 /** called via ctx->Driver.NewTextureObject() */
    150 static struct gl_texture_object *
    151 st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
    152 {
    153    struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
    154    if (!obj)
    155       return NULL;
    156 
    157    /* Pre-allocate a sampler views container to save a branch in the fast path. */
    158    obj->sampler_views = calloc(1, sizeof(struct st_sampler_views) + sizeof(struct st_sampler_view));
    159    if (!obj->sampler_views) {
    160       free(obj);
    161       return NULL;
    162    }
    163    obj->sampler_views->max = 1;
    164 
    165    DBG("%s\n", __func__);
    166    _mesa_initialize_texture_object(ctx, &obj->base, name, target);
    167 
    168    simple_mtx_init(&obj->validate_mutex, mtx_plain);
    169    obj->needs_validation = true;
    170 
    171    return &obj->base;
    172 }
    173 
    174 /** called via ctx->Driver.DeleteTextureObject() */
    175 static void
    176 st_DeleteTextureObject(struct gl_context *ctx,
    177                        struct gl_texture_object *texObj)
    178 {
    179    struct st_context *st = st_context(ctx);
    180    struct st_texture_object *stObj = st_texture_object(texObj);
    181 
    182    pipe_resource_reference(&stObj->pt, NULL);
    183    st_texture_release_all_sampler_views(st, stObj);
    184    st_texture_free_sampler_views(stObj);
    185    simple_mtx_destroy(&stObj->validate_mutex);
    186    _mesa_delete_texture_object(ctx, texObj);
    187 }
    188 
    189 
    190 /** called via ctx->Driver.FreeTextureImageBuffer() */
    191 static void
    192 st_FreeTextureImageBuffer(struct gl_context *ctx,
    193                           struct gl_texture_image *texImage)
    194 {
    195    struct st_context *st = st_context(ctx);
    196    struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
    197    struct st_texture_image *stImage = st_texture_image(texImage);
    198 
    199    DBG("%s\n", __func__);
    200 
    201    if (stImage->pt) {
    202       pipe_resource_reference(&stImage->pt, NULL);
    203    }
    204 
    205    free(stImage->transfer);
    206    stImage->transfer = NULL;
    207    stImage->num_transfers = 0;
    208 
    209    if (stImage->etc_data) {
    210       free(stImage->etc_data);
    211       stImage->etc_data = NULL;
    212    }
    213 
    214    /* if the texture image is being deallocated, the structure of the
    215     * texture is changing so we'll likely need a new sampler view.
    216     */
    217    st_texture_release_all_sampler_views(st, stObj);
    218 }
    219 
    220 bool
    221 st_etc_fallback(struct st_context *st, struct gl_texture_image *texImage)
    222 {
    223    return (_mesa_is_format_etc2(texImage->TexFormat) && !st->has_etc2) ||
    224           (texImage->TexFormat == MESA_FORMAT_ETC1_RGB8 && !st->has_etc1);
    225 }
    226 
    227 static void
    228 etc_fallback_allocate(struct st_context *st, struct st_texture_image *stImage)
    229 {
    230    struct gl_texture_image *texImage = &stImage->base;
    231 
    232    if (!st_etc_fallback(st, texImage))
    233       return;
    234 
    235    if (stImage->etc_data)
    236       free(stImage->etc_data);
    237 
    238    unsigned data_size = _mesa_format_image_size(texImage->TexFormat,
    239                                                 texImage->Width2,
    240                                                 texImage->Height2,
    241                                                 texImage->Depth2);
    242 
    243    stImage->etc_data =
    244       malloc(data_size * _mesa_num_tex_faces(texImage->TexObject->Target));
    245 }
    246 
    247 /** called via ctx->Driver.MapTextureImage() */
    248 static void
    249 st_MapTextureImage(struct gl_context *ctx,
    250                    struct gl_texture_image *texImage,
    251                    GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h,
    252                    GLbitfield mode,
    253                    GLubyte **mapOut, GLint *rowStrideOut)
    254 {
    255    struct st_context *st = st_context(ctx);
    256    struct st_texture_image *stImage = st_texture_image(texImage);
    257    unsigned pipeMode;
    258    GLubyte *map;
    259    struct pipe_transfer *transfer;
    260 
    261    pipeMode = 0x0;
    262    if (mode & GL_MAP_READ_BIT)
    263       pipeMode |= PIPE_TRANSFER_READ;
    264    if (mode & GL_MAP_WRITE_BIT)
    265       pipeMode |= PIPE_TRANSFER_WRITE;
    266    if (mode & GL_MAP_INVALIDATE_RANGE_BIT)
    267       pipeMode |= PIPE_TRANSFER_DISCARD_RANGE;
    268 
    269    map = st_texture_image_map(st, stImage, pipeMode, x, y, slice, w, h, 1,
    270                               &transfer);
    271    if (map) {
    272       if (st_etc_fallback(st, texImage)) {
    273          /* ETC isn't supported by all gallium drivers, where it's represented
    274           * by uncompressed formats. We store the compressed data (as it's
    275           * needed for image copies in OES_copy_image), and decompress as
    276           * necessary in Unmap.
    277           *
    278           * Note: all ETC1/ETC2 formats have 4x4 block sizes.
    279           */
    280          unsigned z = transfer->box.z;
    281          struct st_texture_image_transfer *itransfer = &stImage->transfer[z];
    282 
    283          unsigned bytes = _mesa_get_format_bytes(texImage->TexFormat);
    284          unsigned stride = *rowStrideOut = itransfer->temp_stride =
    285             _mesa_format_row_stride(texImage->TexFormat, texImage->Width2);
    286          *mapOut = itransfer->temp_data =
    287             stImage->etc_data + ((x / 4) * bytes + (y / 4) * stride) +
    288             z * stride * texImage->Height2 / 4;
    289          itransfer->map = map;
    290       }
    291       else {
    292          /* supported mapping */
    293          *mapOut = map;
    294          *rowStrideOut = transfer->stride;
    295       }
    296    }
    297    else {
    298       *mapOut = NULL;
    299       *rowStrideOut = 0;
    300    }
    301 }
    302 
    303 
    304 /** called via ctx->Driver.UnmapTextureImage() */
    305 static void
    306 st_UnmapTextureImage(struct gl_context *ctx,
    307                      struct gl_texture_image *texImage,
    308                      GLuint slice)
    309 {
    310    struct st_context *st = st_context(ctx);
    311    struct st_texture_image *stImage  = st_texture_image(texImage);
    312 
    313    if (st_etc_fallback(st, texImage)) {
    314       /* Decompress the ETC texture to the mapped one. */
    315       unsigned z = slice + stImage->base.Face;
    316       struct st_texture_image_transfer *itransfer = &stImage->transfer[z];
    317       struct pipe_transfer *transfer = itransfer->transfer;
    318 
    319       assert(z == transfer->box.z);
    320 
    321       if (transfer->usage & PIPE_TRANSFER_WRITE) {
    322          if (texImage->TexFormat == MESA_FORMAT_ETC1_RGB8) {
    323             _mesa_etc1_unpack_rgba8888(itransfer->map, transfer->stride,
    324                                        itransfer->temp_data,
    325                                        itransfer->temp_stride,
    326                                        transfer->box.width, transfer->box.height);
    327          }
    328          else {
    329             _mesa_unpack_etc2_format(itransfer->map, transfer->stride,
    330                                      itransfer->temp_data, itransfer->temp_stride,
    331                                      transfer->box.width, transfer->box.height,
    332                                      texImage->TexFormat);
    333          }
    334       }
    335 
    336       itransfer->temp_data = NULL;
    337       itransfer->temp_stride = 0;
    338       itransfer->map = 0;
    339    }
    340 
    341    st_texture_image_unmap(st, stImage, slice);
    342 }
    343 
    344 
    345 /**
    346  * Return default texture resource binding bitmask for the given format.
    347  */
    348 static GLuint
    349 default_bindings(struct st_context *st, enum pipe_format format)
    350 {
    351    struct pipe_screen *screen = st->pipe->screen;
    352    const unsigned target = PIPE_TEXTURE_2D;
    353    unsigned bindings;
    354 
    355    if (util_format_is_depth_or_stencil(format))
    356       bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
    357    else
    358       bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
    359 
    360    if (screen->is_format_supported(screen, format, target, 0, bindings))
    361       return bindings;
    362    else {
    363       /* Try non-sRGB. */
    364       format = util_format_linear(format);
    365 
    366       if (screen->is_format_supported(screen, format, target, 0, bindings))
    367          return bindings;
    368       else
    369          return PIPE_BIND_SAMPLER_VIEW;
    370    }
    371 }
    372 
    373 
    374 /**
    375  * Given the size of a mipmap image, try to compute the size of the level=0
    376  * mipmap image.
    377  *
    378  * Note that this isn't always accurate for odd-sized, non-POW textures.
    379  * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
    380  *
    381  * \return GL_TRUE for success, GL_FALSE for failure
    382  */
    383 static GLboolean
    384 guess_base_level_size(GLenum target,
    385                       GLuint width, GLuint height, GLuint depth, GLuint level,
    386                       GLuint *width0, GLuint *height0, GLuint *depth0)
    387 {
    388    assert(width >= 1);
    389    assert(height >= 1);
    390    assert(depth >= 1);
    391 
    392    if (level > 0) {
    393       /* Guess the size of the base level.
    394        * Depending on the image's size, we can't always make a guess here.
    395        */
    396       switch (target) {
    397       case GL_TEXTURE_1D:
    398       case GL_TEXTURE_1D_ARRAY:
    399          width <<= level;
    400          break;
    401 
    402       case GL_TEXTURE_2D:
    403       case GL_TEXTURE_2D_ARRAY:
    404          /* We can't make a good guess here, because the base level dimensions
    405           * can be non-square.
    406           */
    407          if (width == 1 || height == 1) {
    408             return GL_FALSE;
    409          }
    410          width <<= level;
    411          height <<= level;
    412          break;
    413 
    414       case GL_TEXTURE_CUBE_MAP:
    415       case GL_TEXTURE_CUBE_MAP_ARRAY:
    416          width <<= level;
    417          height <<= level;
    418          break;
    419 
    420       case GL_TEXTURE_3D:
    421          /* We can't make a good guess here, because the base level dimensions
    422           * can be non-cube.
    423           */
    424          if (width == 1 || height == 1 || depth == 1) {
    425             return GL_FALSE;
    426          }
    427          width <<= level;
    428          height <<= level;
    429          depth <<= level;
    430          break;
    431 
    432       case GL_TEXTURE_RECTANGLE:
    433          break;
    434 
    435       default:
    436          assert(0);
    437       }
    438    }
    439 
    440    *width0 = width;
    441    *height0 = height;
    442    *depth0 = depth;
    443 
    444    return GL_TRUE;
    445 }
    446 
    447 
    448 /**
    449  * Try to determine whether we should allocate memory for a full texture
    450  * mipmap.  The problem is when we get a glTexImage(level=0) call, we
    451  * can't immediately know if other mipmap levels are coming next.  Here
    452  * we try to guess whether to allocate memory for a mipmap or just the
    453  * 0th level.
    454  *
    455  * If we guess incorrectly here we'll later reallocate the right amount of
    456  * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
    457  *
    458  * \param stObj  the texture object we're going to allocate memory for.
    459  * \param stImage  describes the incoming image which we need to store.
    460  */
    461 static boolean
    462 allocate_full_mipmap(const struct st_texture_object *stObj,
    463                      const struct st_texture_image *stImage)
    464 {
    465    switch (stObj->base.Target) {
    466    case GL_TEXTURE_RECTANGLE_NV:
    467    case GL_TEXTURE_BUFFER:
    468    case GL_TEXTURE_EXTERNAL_OES:
    469    case GL_TEXTURE_2D_MULTISAMPLE:
    470    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
    471       /* these texture types cannot be mipmapped */
    472       return FALSE;
    473    }
    474 
    475    if (stImage->base.Level > 0 || stObj->base.GenerateMipmap)
    476       return TRUE;
    477 
    478    if (stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
    479        stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT)
    480       /* depth/stencil textures are seldom mipmapped */
    481       return FALSE;
    482 
    483    if (stObj->base.BaseLevel == 0 && stObj->base.MaxLevel == 0)
    484       return FALSE;
    485 
    486    if (stObj->base.Sampler.MinFilter == GL_NEAREST ||
    487        stObj->base.Sampler.MinFilter == GL_LINEAR)
    488       /* not a mipmap minification filter */
    489       return FALSE;
    490 
    491    if (stObj->base.Target == GL_TEXTURE_3D)
    492       /* 3D textures are seldom mipmapped */
    493       return FALSE;
    494 
    495    return TRUE;
    496 }
    497 
    498 
    499 /**
    500  * Try to allocate a pipe_resource object for the given st_texture_object.
    501  *
    502  * We use the given st_texture_image as a clue to determine the size of the
    503  * mipmap image at level=0.
    504  *
    505  * \return GL_TRUE for success, GL_FALSE if out of memory.
    506  */
    507 static GLboolean
    508 guess_and_alloc_texture(struct st_context *st,
    509 			struct st_texture_object *stObj,
    510 			const struct st_texture_image *stImage)
    511 {
    512    const struct gl_texture_image *firstImage;
    513    GLuint lastLevel, width, height, depth;
    514    GLuint bindings;
    515    unsigned ptWidth;
    516    uint16_t ptHeight, ptDepth, ptLayers;
    517    enum pipe_format fmt;
    518    bool guessed_box = false;
    519 
    520    DBG("%s\n", __func__);
    521 
    522    assert(!stObj->pt);
    523 
    524    /* If a base level image with compatible size exists, use that as our guess.
    525     */
    526    firstImage = _mesa_base_tex_image(&stObj->base);
    527    if (firstImage &&
    528        firstImage->Width2 > 0 &&
    529        firstImage->Height2 > 0 &&
    530        firstImage->Depth2 > 0 &&
    531        guess_base_level_size(stObj->base.Target,
    532                              firstImage->Width2,
    533                              firstImage->Height2,
    534                              firstImage->Depth2,
    535                              firstImage->Level,
    536                              &width, &height, &depth)) {
    537       if (stImage->base.Width2 == u_minify(width, stImage->base.Level) &&
    538           stImage->base.Height2 == u_minify(height, stImage->base.Level) &&
    539           stImage->base.Depth2 == u_minify(depth, stImage->base.Level))
    540          guessed_box = true;
    541    }
    542 
    543    if (!guessed_box)
    544       guessed_box = guess_base_level_size(stObj->base.Target,
    545                                           stImage->base.Width2,
    546                                           stImage->base.Height2,
    547                                           stImage->base.Depth2,
    548                                           stImage->base.Level,
    549                                           &width, &height, &depth);
    550 
    551    if (!guessed_box) {
    552       /* we can't determine the image size at level=0 */
    553       /* this is not an out of memory error */
    554       return GL_TRUE;
    555    }
    556 
    557    /* At this point, (width x height x depth) is the expected size of
    558     * the level=0 mipmap image.
    559     */
    560 
    561    /* Guess a reasonable value for lastLevel.  With OpenGL we have no
    562     * idea how many mipmap levels will be in a texture until we start
    563     * to render with it.  Make an educated guess here but be prepared
    564     * to re-allocating a texture buffer with space for more (or fewer)
    565     * mipmap levels later.
    566     */
    567    if (allocate_full_mipmap(stObj, stImage)) {
    568       /* alloc space for a full mipmap */
    569       lastLevel = _mesa_get_tex_max_num_levels(stObj->base.Target,
    570                                                width, height, depth) - 1;
    571    }
    572    else {
    573       /* only alloc space for a single mipmap level */
    574       lastLevel = 0;
    575    }
    576 
    577    fmt = st_mesa_format_to_pipe_format(st, stImage->base.TexFormat);
    578 
    579    bindings = default_bindings(st, fmt);
    580 
    581    st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
    582                                    width, height, depth,
    583                                    &ptWidth, &ptHeight, &ptDepth, &ptLayers);
    584 
    585    stObj->pt = st_texture_create(st,
    586                                  gl_target_to_pipe(stObj->base.Target),
    587                                  fmt,
    588                                  lastLevel,
    589                                  ptWidth,
    590                                  ptHeight,
    591                                  ptDepth,
    592                                  ptLayers, 0,
    593                                  bindings);
    594 
    595    stObj->lastLevel = lastLevel;
    596 
    597    DBG("%s returning %d\n", __func__, (stObj->pt != NULL));
    598 
    599    return stObj->pt != NULL;
    600 }
    601 
    602 
    603 /**
    604  * Called via ctx->Driver.AllocTextureImageBuffer().
    605  * If the texture object/buffer already has space for the indicated image,
    606  * we're done.  Otherwise, allocate memory for the new texture image.
    607  */
    608 static GLboolean
    609 st_AllocTextureImageBuffer(struct gl_context *ctx,
    610                            struct gl_texture_image *texImage)
    611 {
    612    struct st_context *st = st_context(ctx);
    613    struct st_texture_image *stImage = st_texture_image(texImage);
    614    struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
    615    const GLuint level = texImage->Level;
    616    GLuint width = texImage->Width;
    617    GLuint height = texImage->Height;
    618    GLuint depth = texImage->Depth;
    619 
    620    DBG("%s\n", __func__);
    621 
    622    assert(!stImage->pt); /* xxx this might be wrong */
    623 
    624    stObj->needs_validation = true;
    625 
    626    etc_fallback_allocate(st, stImage);
    627 
    628    /* Look if the parent texture object has space for this image */
    629    if (stObj->pt &&
    630        level <= stObj->pt->last_level &&
    631        st_texture_match_image(st, stObj->pt, texImage)) {
    632       /* this image will fit in the existing texture object's memory */
    633       pipe_resource_reference(&stImage->pt, stObj->pt);
    634       return GL_TRUE;
    635    }
    636 
    637    /* The parent texture object does not have space for this image */
    638 
    639    pipe_resource_reference(&stObj->pt, NULL);
    640    st_texture_release_all_sampler_views(st, stObj);
    641 
    642    if (!guess_and_alloc_texture(st, stObj, stImage)) {
    643       /* Probably out of memory.
    644        * Try flushing any pending rendering, then retry.
    645        */
    646       st_finish(st);
    647       if (!guess_and_alloc_texture(st, stObj, stImage)) {
    648          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
    649          return GL_FALSE;
    650       }
    651    }
    652 
    653    if (stObj->pt &&
    654        st_texture_match_image(st, stObj->pt, texImage)) {
    655       /* The image will live in the object's mipmap memory */
    656       pipe_resource_reference(&stImage->pt, stObj->pt);
    657       assert(stImage->pt);
    658       return GL_TRUE;
    659    }
    660    else {
    661       /* Create a new, temporary texture/resource/buffer to hold this
    662        * one texture image.  Note that when we later access this image
    663        * (either for mapping or copying) we'll want to always specify
    664        * mipmap level=0, even if the image represents some other mipmap
    665        * level.
    666        */
    667       enum pipe_format format =
    668          st_mesa_format_to_pipe_format(st, texImage->TexFormat);
    669       GLuint bindings = default_bindings(st, format);
    670       unsigned ptWidth;
    671       uint16_t ptHeight, ptDepth, ptLayers;
    672 
    673       st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
    674                                       width, height, depth,
    675                                       &ptWidth, &ptHeight, &ptDepth, &ptLayers);
    676 
    677       stImage->pt = st_texture_create(st,
    678                                       gl_target_to_pipe(stObj->base.Target),
    679                                       format,
    680                                       0, /* lastLevel */
    681                                       ptWidth,
    682                                       ptHeight,
    683                                       ptDepth,
    684                                       ptLayers, 0,
    685                                       bindings);
    686       return stImage->pt != NULL;
    687    }
    688 }
    689 
    690 
    691 /**
    692  * Preparation prior to glTexImage.  Basically check the 'surface_based'
    693  * field and switch to a "normal" tex image if necessary.
    694  */
    695 static void
    696 prep_teximage(struct gl_context *ctx, struct gl_texture_image *texImage,
    697               GLenum format, GLenum type)
    698 {
    699    struct gl_texture_object *texObj = texImage->TexObject;
    700    struct st_texture_object *stObj = st_texture_object(texObj);
    701 
    702    /* switch to "normal" */
    703    if (stObj->surface_based) {
    704       const GLenum target = texObj->Target;
    705       const GLuint level = texImage->Level;
    706       mesa_format texFormat;
    707 
    708       assert(!st_texture_image(texImage)->pt);
    709       _mesa_clear_texture_object(ctx, texObj, texImage);
    710       stObj->layer_override = 0;
    711       stObj->level_override = 0;
    712       pipe_resource_reference(&stObj->pt, NULL);
    713 
    714       /* oops, need to init this image again */
    715       texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
    716                                               texImage->InternalFormat, format,
    717                                               type);
    718 
    719       _mesa_init_teximage_fields(ctx, texImage,
    720                                  texImage->Width, texImage->Height,
    721                                  texImage->Depth, texImage->Border,
    722                                  texImage->InternalFormat, texFormat);
    723 
    724       stObj->surface_based = GL_FALSE;
    725    }
    726 }
    727 
    728 
    729 /**
    730  * Return a writemask for the gallium blit. The parameters can be base
    731  * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
    732  */
    733 unsigned
    734 st_get_blit_mask(GLenum srcFormat, GLenum dstFormat)
    735 {
    736    switch (dstFormat) {
    737    case GL_DEPTH_STENCIL:
    738       switch (srcFormat) {
    739       case GL_DEPTH_STENCIL:
    740          return PIPE_MASK_ZS;
    741       case GL_DEPTH_COMPONENT:
    742          return PIPE_MASK_Z;
    743       case GL_STENCIL_INDEX:
    744          return PIPE_MASK_S;
    745       default:
    746          assert(0);
    747          return 0;
    748       }
    749 
    750    case GL_DEPTH_COMPONENT:
    751       switch (srcFormat) {
    752       case GL_DEPTH_STENCIL:
    753       case GL_DEPTH_COMPONENT:
    754          return PIPE_MASK_Z;
    755       default:
    756          assert(0);
    757          return 0;
    758       }
    759 
    760    case GL_STENCIL_INDEX:
    761       switch (srcFormat) {
    762       case GL_STENCIL_INDEX:
    763          return PIPE_MASK_S;
    764       default:
    765          assert(0);
    766          return 0;
    767       }
    768 
    769    default:
    770       return PIPE_MASK_RGBA;
    771    }
    772 }
    773 
    774 /**
    775  * Converts format to a format with the same components, types
    776  * and sizes, but with the components in RGBA order.
    777  */
    778 static enum pipe_format
    779 unswizzle_format(enum pipe_format format)
    780 {
    781    switch (format)
    782    {
    783    case PIPE_FORMAT_B8G8R8A8_UNORM:
    784    case PIPE_FORMAT_A8R8G8B8_UNORM:
    785    case PIPE_FORMAT_A8B8G8R8_UNORM:
    786       return PIPE_FORMAT_R8G8B8A8_UNORM;
    787 
    788    case PIPE_FORMAT_B10G10R10A2_UNORM:
    789       return PIPE_FORMAT_R10G10B10A2_UNORM;
    790 
    791    case PIPE_FORMAT_B10G10R10A2_SNORM:
    792       return PIPE_FORMAT_R10G10B10A2_SNORM;
    793 
    794    case PIPE_FORMAT_B10G10R10A2_UINT:
    795       return PIPE_FORMAT_R10G10B10A2_UINT;
    796 
    797    default:
    798       return format;
    799    }
    800 }
    801 
    802 /**
    803  * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
    804  */
    805 static enum pipe_format
    806 alpha_to_red(enum pipe_format format)
    807 {
    808    switch (format)
    809    {
    810    case PIPE_FORMAT_A8_UNORM:
    811       return PIPE_FORMAT_R8_UNORM;
    812    case PIPE_FORMAT_A8_SNORM:
    813       return PIPE_FORMAT_R8_SNORM;
    814    case PIPE_FORMAT_A8_UINT:
    815       return PIPE_FORMAT_R8_UINT;
    816    case PIPE_FORMAT_A8_SINT:
    817       return PIPE_FORMAT_R8_SINT;
    818 
    819    case PIPE_FORMAT_A16_UNORM:
    820       return PIPE_FORMAT_R16_UNORM;
    821    case PIPE_FORMAT_A16_SNORM:
    822       return PIPE_FORMAT_R16_SNORM;
    823    case PIPE_FORMAT_A16_UINT:
    824       return PIPE_FORMAT_R16_UINT;
    825    case PIPE_FORMAT_A16_SINT:
    826       return PIPE_FORMAT_R16_SINT;
    827    case PIPE_FORMAT_A16_FLOAT:
    828       return PIPE_FORMAT_R16_FLOAT;
    829 
    830    case PIPE_FORMAT_A32_UINT:
    831       return PIPE_FORMAT_R32_UINT;
    832    case PIPE_FORMAT_A32_SINT:
    833       return PIPE_FORMAT_R32_SINT;
    834    case PIPE_FORMAT_A32_FLOAT:
    835       return PIPE_FORMAT_R32_FLOAT;
    836 
    837    default:
    838       return format;
    839    }
    840 }
    841 
    842 /**
    843  * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
    844  */
    845 static enum pipe_format
    846 red_alpha_to_red_green(enum pipe_format format)
    847 {
    848    switch (format)
    849    {
    850    case PIPE_FORMAT_R8A8_UNORM:
    851       return PIPE_FORMAT_R8G8_UNORM;
    852    case PIPE_FORMAT_R8A8_SNORM:
    853       return PIPE_FORMAT_R8G8_SNORM;
    854    case PIPE_FORMAT_R8A8_UINT:
    855       return PIPE_FORMAT_R8G8_UINT;
    856    case PIPE_FORMAT_R8A8_SINT:
    857       return PIPE_FORMAT_R8G8_SINT;
    858 
    859    case PIPE_FORMAT_R16A16_UNORM:
    860       return PIPE_FORMAT_R16G16_UNORM;
    861    case PIPE_FORMAT_R16A16_SNORM:
    862       return PIPE_FORMAT_R16G16_SNORM;
    863    case PIPE_FORMAT_R16A16_UINT:
    864       return PIPE_FORMAT_R16G16_UINT;
    865    case PIPE_FORMAT_R16A16_SINT:
    866       return PIPE_FORMAT_R16G16_SINT;
    867    case PIPE_FORMAT_R16A16_FLOAT:
    868       return PIPE_FORMAT_R16G16_FLOAT;
    869 
    870    case PIPE_FORMAT_R32A32_UINT:
    871       return PIPE_FORMAT_R32G32_UINT;
    872    case PIPE_FORMAT_R32A32_SINT:
    873       return PIPE_FORMAT_R32G32_SINT;
    874    case PIPE_FORMAT_R32A32_FLOAT:
    875       return PIPE_FORMAT_R32G32_FLOAT;
    876 
    877    default:
    878        return format;
    879    }
    880 }
    881 
    882 /**
    883  * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
    884  */
    885 static enum pipe_format
    886 luminance_alpha_to_red_green(enum pipe_format format)
    887 {
    888    switch (format)
    889    {
    890    case PIPE_FORMAT_L8A8_UNORM:
    891       return PIPE_FORMAT_R8G8_UNORM;
    892    case PIPE_FORMAT_L8A8_SNORM:
    893       return PIPE_FORMAT_R8G8_SNORM;
    894    case PIPE_FORMAT_L8A8_UINT:
    895       return PIPE_FORMAT_R8G8_UINT;
    896    case PIPE_FORMAT_L8A8_SINT:
    897       return PIPE_FORMAT_R8G8_SINT;
    898 
    899    case PIPE_FORMAT_L16A16_UNORM:
    900       return PIPE_FORMAT_R16G16_UNORM;
    901    case PIPE_FORMAT_L16A16_SNORM:
    902       return PIPE_FORMAT_R16G16_SNORM;
    903    case PIPE_FORMAT_L16A16_UINT:
    904       return PIPE_FORMAT_R16G16_UINT;
    905    case PIPE_FORMAT_L16A16_SINT:
    906       return PIPE_FORMAT_R16G16_SINT;
    907    case PIPE_FORMAT_L16A16_FLOAT:
    908       return PIPE_FORMAT_R16G16_FLOAT;
    909 
    910    case PIPE_FORMAT_L32A32_UINT:
    911       return PIPE_FORMAT_R32G32_UINT;
    912    case PIPE_FORMAT_L32A32_SINT:
    913       return PIPE_FORMAT_R32G32_SINT;
    914    case PIPE_FORMAT_L32A32_FLOAT:
    915       return PIPE_FORMAT_R32G32_FLOAT;
    916 
    917    default:
    918        return format;
    919    }
    920 }
    921 
    922 /**
    923  * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
    924  */
    925 static bool
    926 format_is_alpha(enum pipe_format format)
    927 {
    928    const struct util_format_description *desc = util_format_description(format);
    929 
    930    if (desc->nr_channels == 1 &&
    931        desc->swizzle[0] == PIPE_SWIZZLE_0 &&
    932        desc->swizzle[1] == PIPE_SWIZZLE_0 &&
    933        desc->swizzle[2] == PIPE_SWIZZLE_0 &&
    934        desc->swizzle[3] == PIPE_SWIZZLE_X)
    935       return true;
    936 
    937    return false;
    938 }
    939 
    940 /**
    941  * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
    942  */
    943 static bool
    944 format_is_red(enum pipe_format format)
    945 {
    946    const struct util_format_description *desc = util_format_description(format);
    947 
    948    if (desc->nr_channels == 1 &&
    949        desc->swizzle[0] == PIPE_SWIZZLE_X &&
    950        desc->swizzle[1] == PIPE_SWIZZLE_0 &&
    951        desc->swizzle[2] == PIPE_SWIZZLE_0 &&
    952        desc->swizzle[3] == PIPE_SWIZZLE_1)
    953       return true;
    954 
    955    return false;
    956 }
    957 
    958 
    959 /**
    960  * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
    961  */
    962 static bool
    963 format_is_luminance(enum pipe_format format)
    964 {
    965    const struct util_format_description *desc = util_format_description(format);
    966 
    967    if (desc->nr_channels == 1 &&
    968        desc->swizzle[0] == PIPE_SWIZZLE_X &&
    969        desc->swizzle[1] == PIPE_SWIZZLE_X &&
    970        desc->swizzle[2] == PIPE_SWIZZLE_X &&
    971        desc->swizzle[3] == PIPE_SWIZZLE_1)
    972       return true;
    973 
    974    return false;
    975 }
    976 
    977 /**
    978  * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
    979  */
    980 static bool
    981 format_is_red_alpha(enum pipe_format format)
    982 {
    983    const struct util_format_description *desc = util_format_description(format);
    984 
    985    if (desc->nr_channels == 2 &&
    986        desc->swizzle[0] == PIPE_SWIZZLE_X &&
    987        desc->swizzle[1] == PIPE_SWIZZLE_0 &&
    988        desc->swizzle[2] == PIPE_SWIZZLE_0 &&
    989        desc->swizzle[3] == PIPE_SWIZZLE_Y)
    990       return true;
    991 
    992    return false;
    993 }
    994 
    995 static bool
    996 format_is_swizzled_rgba(enum pipe_format format)
    997 {
    998     const struct util_format_description *desc = util_format_description(format);
    999 
   1000     if ((desc->swizzle[0] == TGSI_SWIZZLE_X || desc->swizzle[0] == PIPE_SWIZZLE_0) &&
   1001         (desc->swizzle[1] == TGSI_SWIZZLE_Y || desc->swizzle[1] == PIPE_SWIZZLE_0) &&
   1002         (desc->swizzle[2] == TGSI_SWIZZLE_Z || desc->swizzle[2] == PIPE_SWIZZLE_0) &&
   1003         (desc->swizzle[3] == TGSI_SWIZZLE_W || desc->swizzle[3] == PIPE_SWIZZLE_1))
   1004        return false;
   1005 
   1006     return true;
   1007 }
   1008 
   1009 struct format_table
   1010 {
   1011    unsigned char swizzle[4];
   1012    enum pipe_format format;
   1013 };
   1014 
   1015 static const struct format_table table_8888_unorm[] = {
   1016    { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM },
   1017    { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM },
   1018    { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM },
   1019    { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM }
   1020 };
   1021 
   1022 static const struct format_table table_1010102_unorm[] = {
   1023    { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM },
   1024    { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM }
   1025 };
   1026 
   1027 static const struct format_table table_1010102_snorm[] = {
   1028    { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM },
   1029    { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM }
   1030 };
   1031 
   1032 static const struct format_table table_1010102_uint[] = {
   1033    { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT },
   1034    { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT }
   1035 };
   1036 
   1037 static enum pipe_format
   1038 swizzle_format(enum pipe_format format, const int * const swizzle)
   1039 {
   1040    unsigned i;
   1041 
   1042    switch (format) {
   1043    case PIPE_FORMAT_R8G8B8A8_UNORM:
   1044    case PIPE_FORMAT_B8G8R8A8_UNORM:
   1045    case PIPE_FORMAT_A8R8G8B8_UNORM:
   1046    case PIPE_FORMAT_A8B8G8R8_UNORM:
   1047       for (i = 0; i < ARRAY_SIZE(table_8888_unorm); i++) {
   1048          if (swizzle[0] == table_8888_unorm[i].swizzle[0] &&
   1049              swizzle[1] == table_8888_unorm[i].swizzle[1] &&
   1050              swizzle[2] == table_8888_unorm[i].swizzle[2] &&
   1051              swizzle[3] == table_8888_unorm[i].swizzle[3])
   1052             return table_8888_unorm[i].format;
   1053       }
   1054       break;
   1055 
   1056    case PIPE_FORMAT_R10G10B10A2_UNORM:
   1057    case PIPE_FORMAT_B10G10R10A2_UNORM:
   1058       for (i = 0; i < ARRAY_SIZE(table_1010102_unorm); i++) {
   1059          if (swizzle[0] == table_1010102_unorm[i].swizzle[0] &&
   1060              swizzle[1] == table_1010102_unorm[i].swizzle[1] &&
   1061              swizzle[2] == table_1010102_unorm[i].swizzle[2] &&
   1062              swizzle[3] == table_1010102_unorm[i].swizzle[3])
   1063             return table_1010102_unorm[i].format;
   1064       }
   1065       break;
   1066 
   1067    case PIPE_FORMAT_R10G10B10A2_SNORM:
   1068    case PIPE_FORMAT_B10G10R10A2_SNORM:
   1069       for (i = 0; i < ARRAY_SIZE(table_1010102_snorm); i++) {
   1070          if (swizzle[0] == table_1010102_snorm[i].swizzle[0] &&
   1071              swizzle[1] == table_1010102_snorm[i].swizzle[1] &&
   1072              swizzle[2] == table_1010102_snorm[i].swizzle[2] &&
   1073              swizzle[3] == table_1010102_snorm[i].swizzle[3])
   1074             return table_1010102_snorm[i].format;
   1075       }
   1076       break;
   1077 
   1078    case PIPE_FORMAT_R10G10B10A2_UINT:
   1079    case PIPE_FORMAT_B10G10R10A2_UINT:
   1080       for (i = 0; i < ARRAY_SIZE(table_1010102_uint); i++) {
   1081          if (swizzle[0] == table_1010102_uint[i].swizzle[0] &&
   1082              swizzle[1] == table_1010102_uint[i].swizzle[1] &&
   1083              swizzle[2] == table_1010102_uint[i].swizzle[2] &&
   1084              swizzle[3] == table_1010102_uint[i].swizzle[3])
   1085             return table_1010102_uint[i].format;
   1086       }
   1087       break;
   1088 
   1089    default:
   1090       break;
   1091    }
   1092 
   1093    return PIPE_FORMAT_NONE;
   1094 }
   1095 
   1096 static bool
   1097 reinterpret_formats(enum pipe_format *src_format, enum pipe_format *dst_format)
   1098 {
   1099    enum pipe_format src = *src_format;
   1100    enum pipe_format dst = *dst_format;
   1101 
   1102    /* Note: dst_format has already been transformed from luminance/intensity
   1103     *       to red when this function is called.  The source format will never
   1104     *       be an intensity format, because GL_INTENSITY is not a legal value
   1105     *       for the format parameter in glTex(Sub)Image(). */
   1106 
   1107    if (format_is_alpha(src)) {
   1108       if (!format_is_alpha(dst))
   1109          return false;
   1110 
   1111       src = alpha_to_red(src);
   1112       dst = alpha_to_red(dst);
   1113    } else if (format_is_luminance(src)) {
   1114       if (!format_is_red(dst) && !format_is_red_alpha(dst))
   1115          return false;
   1116 
   1117       src = util_format_luminance_to_red(src);
   1118    } else if (util_format_is_luminance_alpha(src)) {
   1119       src = luminance_alpha_to_red_green(src);
   1120 
   1121       if (format_is_red_alpha(dst)) {
   1122          dst = red_alpha_to_red_green(dst);
   1123       } else if (!format_is_red(dst))
   1124          return false;
   1125    } else if (format_is_swizzled_rgba(src)) {
   1126       const struct util_format_description *src_desc = util_format_description(src);
   1127       const struct util_format_description *dst_desc = util_format_description(dst);
   1128       int swizzle[4];
   1129       unsigned i;
   1130 
   1131       /* Make sure the format is an RGBA and not an RGBX format */
   1132       if (src_desc->nr_channels != 4 || src_desc->swizzle[3] == PIPE_SWIZZLE_1)
   1133          return false;
   1134 
   1135       if (dst_desc->nr_channels != 4 || dst_desc->swizzle[3] == PIPE_SWIZZLE_1)
   1136          return false;
   1137 
   1138       for (i = 0; i < 4; i++)
   1139          swizzle[i] = dst_desc->swizzle[src_desc->swizzle[i]];
   1140 
   1141       dst = swizzle_format(dst, swizzle);
   1142       if (dst == PIPE_FORMAT_NONE)
   1143          return false;
   1144 
   1145       src = unswizzle_format(src);
   1146    }
   1147 
   1148    *src_format = src;
   1149    *dst_format = dst;
   1150    return true;
   1151 }
   1152 
   1153 static bool
   1154 try_pbo_upload_common(struct gl_context *ctx,
   1155                       struct pipe_surface *surface,
   1156                       const struct st_pbo_addresses *addr,
   1157                       enum pipe_format src_format)
   1158 {
   1159    struct st_context *st = st_context(ctx);
   1160    struct cso_context *cso = st->cso_context;
   1161    struct pipe_context *pipe = st->pipe;
   1162    bool success = false;
   1163    void *fs;
   1164 
   1165    fs = st_pbo_get_upload_fs(st, src_format, surface->format);
   1166    if (!fs)
   1167       return false;
   1168 
   1169    cso_save_state(cso, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS |
   1170                         CSO_BIT_FRAGMENT_SAMPLERS |
   1171                         CSO_BIT_VERTEX_ELEMENTS |
   1172                         CSO_BIT_AUX_VERTEX_BUFFER_SLOT |
   1173                         CSO_BIT_FRAMEBUFFER |
   1174                         CSO_BIT_VIEWPORT |
   1175                         CSO_BIT_BLEND |
   1176                         CSO_BIT_DEPTH_STENCIL_ALPHA |
   1177                         CSO_BIT_RASTERIZER |
   1178                         CSO_BIT_STREAM_OUTPUTS |
   1179                         CSO_BIT_PAUSE_QUERIES |
   1180                         CSO_BIT_SAMPLE_MASK |
   1181                         CSO_BIT_MIN_SAMPLES |
   1182                         CSO_BIT_RENDER_CONDITION |
   1183                         CSO_BITS_ALL_SHADERS));
   1184    cso_save_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
   1185 
   1186    cso_set_sample_mask(cso, ~0);
   1187    cso_set_min_samples(cso, 1);
   1188    cso_set_render_condition(cso, NULL, FALSE, 0);
   1189 
   1190    /* Set up the sampler_view */
   1191    {
   1192       struct pipe_sampler_view templ;
   1193       struct pipe_sampler_view *sampler_view;
   1194       struct pipe_sampler_state sampler = {0};
   1195       const struct pipe_sampler_state *samplers[1] = {&sampler};
   1196 
   1197       memset(&templ, 0, sizeof(templ));
   1198       templ.target = PIPE_BUFFER;
   1199       templ.format = src_format;
   1200       templ.u.buf.offset = addr->first_element * addr->bytes_per_pixel;
   1201       templ.u.buf.size = (addr->last_element - addr->first_element + 1) *
   1202                          addr->bytes_per_pixel;
   1203       templ.swizzle_r = PIPE_SWIZZLE_X;
   1204       templ.swizzle_g = PIPE_SWIZZLE_Y;
   1205       templ.swizzle_b = PIPE_SWIZZLE_Z;
   1206       templ.swizzle_a = PIPE_SWIZZLE_W;
   1207 
   1208       sampler_view = pipe->create_sampler_view(pipe, addr->buffer, &templ);
   1209       if (sampler_view == NULL)
   1210          goto fail;
   1211 
   1212       cso_set_sampler_views(cso, PIPE_SHADER_FRAGMENT, 1, &sampler_view);
   1213 
   1214       pipe_sampler_view_reference(&sampler_view, NULL);
   1215 
   1216       cso_set_samplers(cso, PIPE_SHADER_FRAGMENT, 1, samplers);
   1217    }
   1218 
   1219    /* Framebuffer_state */
   1220    {
   1221       struct pipe_framebuffer_state fb;
   1222       memset(&fb, 0, sizeof(fb));
   1223       fb.width = surface->width;
   1224       fb.height = surface->height;
   1225       fb.nr_cbufs = 1;
   1226       pipe_surface_reference(&fb.cbufs[0], surface);
   1227 
   1228       cso_set_framebuffer(cso, &fb);
   1229 
   1230       pipe_surface_reference(&fb.cbufs[0], NULL);
   1231    }
   1232 
   1233    cso_set_viewport_dims(cso, surface->width, surface->height, FALSE);
   1234 
   1235    /* Blend state */
   1236    cso_set_blend(cso, &st->pbo.upload_blend);
   1237 
   1238    /* Depth/stencil/alpha state */
   1239    {
   1240       struct pipe_depth_stencil_alpha_state dsa;
   1241       memset(&dsa, 0, sizeof(dsa));
   1242       cso_set_depth_stencil_alpha(cso, &dsa);
   1243    }
   1244 
   1245    /* Set up the fragment shader */
   1246    cso_set_fragment_shader_handle(cso, fs);
   1247 
   1248    success = st_pbo_draw(st, addr, surface->width, surface->height);
   1249 
   1250 fail:
   1251    cso_restore_state(cso);
   1252    cso_restore_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
   1253 
   1254    return success;
   1255 }
   1256 
   1257 static bool
   1258 try_pbo_upload(struct gl_context *ctx, GLuint dims,
   1259                struct gl_texture_image *texImage,
   1260                GLenum format, GLenum type,
   1261                enum pipe_format dst_format,
   1262                GLint xoffset, GLint yoffset, GLint zoffset,
   1263                GLint width, GLint height, GLint depth,
   1264                const void *pixels,
   1265                const struct gl_pixelstore_attrib *unpack)
   1266 {
   1267    struct st_context *st = st_context(ctx);
   1268    struct st_texture_image *stImage = st_texture_image(texImage);
   1269    struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
   1270    struct pipe_resource *texture = stImage->pt;
   1271    struct pipe_context *pipe = st->pipe;
   1272    struct pipe_screen *screen = pipe->screen;
   1273    struct pipe_surface *surface = NULL;
   1274    struct st_pbo_addresses addr;
   1275    enum pipe_format src_format;
   1276    const struct util_format_description *desc;
   1277    GLenum gl_target = texImage->TexObject->Target;
   1278    bool success;
   1279 
   1280    if (!st->pbo.upload_enabled)
   1281       return false;
   1282 
   1283    /* From now on, we need the gallium representation of dimensions. */
   1284    if (gl_target == GL_TEXTURE_1D_ARRAY) {
   1285       depth = height;
   1286       height = 1;
   1287       zoffset = yoffset;
   1288       yoffset = 0;
   1289    }
   1290 
   1291    if (depth != 1 && !st->pbo.layers)
   1292       return false;
   1293 
   1294    /* Choose the source format. Initially, we do so without checking driver
   1295     * support at all because of the remapping we later perform and because
   1296     * at least the Radeon driver actually supports some formats for texture
   1297     * buffers which it doesn't support for regular textures. */
   1298    src_format = st_choose_matching_format(st, 0, format, type, unpack->SwapBytes);
   1299    if (!src_format) {
   1300       return false;
   1301    }
   1302 
   1303    src_format = util_format_linear(src_format);
   1304    desc = util_format_description(src_format);
   1305 
   1306    if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
   1307       return false;
   1308 
   1309    if (desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB)
   1310       return false;
   1311 
   1312    if (st->pbo.rgba_only) {
   1313       enum pipe_format orig_dst_format = dst_format;
   1314 
   1315       if (!reinterpret_formats(&src_format, &dst_format)) {
   1316          return false;
   1317       }
   1318 
   1319       if (dst_format != orig_dst_format &&
   1320           !screen->is_format_supported(screen, dst_format, PIPE_TEXTURE_2D, 0,
   1321                                        PIPE_BIND_RENDER_TARGET)) {
   1322          return false;
   1323       }
   1324    }
   1325 
   1326    if (!src_format ||
   1327        !screen->is_format_supported(screen, src_format, PIPE_BUFFER, 0,
   1328                                     PIPE_BIND_SAMPLER_VIEW)) {
   1329       return false;
   1330    }
   1331 
   1332    /* Compute buffer addresses */
   1333    addr.xoffset = xoffset;
   1334    addr.yoffset = yoffset;
   1335    addr.width = width;
   1336    addr.height = height;
   1337    addr.depth = depth;
   1338    addr.bytes_per_pixel = desc->block.bits / 8;
   1339 
   1340    if (!st_pbo_addresses_pixelstore(st, gl_target, dims == 3, unpack, pixels,
   1341                                     &addr))
   1342       return false;
   1343 
   1344    /* Set up the surface */
   1345    {
   1346       unsigned level = stObj->pt != stImage->pt ? 0 : texImage->TexObject->MinLevel + texImage->Level;
   1347       unsigned max_layer = util_max_layer(texture, level);
   1348 
   1349       zoffset += texImage->Face + texImage->TexObject->MinLayer;
   1350 
   1351       struct pipe_surface templ;
   1352       memset(&templ, 0, sizeof(templ));
   1353       templ.format = dst_format;
   1354       templ.u.tex.level = level;
   1355       templ.u.tex.first_layer = MIN2(zoffset, max_layer);
   1356       templ.u.tex.last_layer = MIN2(zoffset + depth - 1, max_layer);
   1357 
   1358       surface = pipe->create_surface(pipe, texture, &templ);
   1359       if (!surface)
   1360          return false;
   1361    }
   1362 
   1363    success = try_pbo_upload_common(ctx, surface, &addr, src_format);
   1364 
   1365    pipe_surface_reference(&surface, NULL);
   1366 
   1367    return success;
   1368 }
   1369 
   1370 static void
   1371 st_TexSubImage(struct gl_context *ctx, GLuint dims,
   1372                struct gl_texture_image *texImage,
   1373                GLint xoffset, GLint yoffset, GLint zoffset,
   1374                GLint width, GLint height, GLint depth,
   1375                GLenum format, GLenum type, const void *pixels,
   1376                const struct gl_pixelstore_attrib *unpack)
   1377 {
   1378    struct st_context *st = st_context(ctx);
   1379    struct st_texture_image *stImage = st_texture_image(texImage);
   1380    struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
   1381    struct pipe_context *pipe = st->pipe;
   1382    struct pipe_screen *screen = pipe->screen;
   1383    struct pipe_resource *dst = stImage->pt;
   1384    struct pipe_resource *src = NULL;
   1385    struct pipe_resource src_templ;
   1386    struct pipe_transfer *transfer;
   1387    struct pipe_blit_info blit;
   1388    enum pipe_format src_format, dst_format;
   1389    mesa_format mesa_src_format;
   1390    GLenum gl_target = texImage->TexObject->Target;
   1391    unsigned bind;
   1392    GLubyte *map;
   1393    unsigned dstz = texImage->Face + texImage->TexObject->MinLayer;
   1394    unsigned dst_level = 0;
   1395 
   1396    st_flush_bitmap_cache(st);
   1397    st_invalidate_readpix_cache(st);
   1398 
   1399    if (stObj->pt == stImage->pt)
   1400       dst_level = texImage->TexObject->MinLevel + texImage->Level;
   1401 
   1402    assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
   1403           texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
   1404 
   1405    if (!dst)
   1406       goto fallback;
   1407 
   1408    /* Try texture_subdata, which should be the fastest memcpy path. */
   1409    if (pixels &&
   1410        !_mesa_is_bufferobj(unpack->BufferObj) &&
   1411        _mesa_texstore_can_use_memcpy(ctx, texImage->_BaseFormat,
   1412                                      texImage->TexFormat, format, type,
   1413                                      unpack)) {
   1414       struct pipe_box box;
   1415       unsigned stride, layer_stride;
   1416       void *data;
   1417 
   1418       stride = _mesa_image_row_stride(unpack, width, format, type);
   1419       layer_stride = _mesa_image_image_stride(unpack, width, height, format,
   1420                                               type);
   1421       data = _mesa_image_address(dims, unpack, pixels, width, height, format,
   1422                                  type, 0, 0, 0);
   1423 
   1424       /* Convert to Gallium coordinates. */
   1425       if (gl_target == GL_TEXTURE_1D_ARRAY) {
   1426          zoffset = yoffset;
   1427          yoffset = 0;
   1428          depth = height;
   1429          height = 1;
   1430          layer_stride = stride;
   1431       }
   1432 
   1433       u_box_3d(xoffset, yoffset, zoffset + dstz, width, height, depth, &box);
   1434       pipe->texture_subdata(pipe, dst, dst_level, 0,
   1435                             &box, data, stride, layer_stride);
   1436       return;
   1437    }
   1438 
   1439    if (!st->prefer_blit_based_texture_transfer) {
   1440       goto fallback;
   1441    }
   1442 
   1443    /* XXX Fallback for depth-stencil formats due to an incomplete stencil
   1444     * blit implementation in some drivers. */
   1445    if (format == GL_DEPTH_STENCIL) {
   1446       goto fallback;
   1447    }
   1448 
   1449    /* If the base internal format and the texture format don't match,
   1450     * we can't use blit-based TexSubImage. */
   1451    if (texImage->_BaseFormat !=
   1452        _mesa_get_format_base_format(texImage->TexFormat)) {
   1453       goto fallback;
   1454    }
   1455 
   1456 
   1457    /* See if the destination format is supported. */
   1458    if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
   1459       bind = PIPE_BIND_DEPTH_STENCIL;
   1460    else
   1461       bind = PIPE_BIND_RENDER_TARGET;
   1462 
   1463    /* For luminance and intensity, only the red channel is stored
   1464     * in the destination. */
   1465    dst_format = util_format_linear(dst->format);
   1466    dst_format = util_format_luminance_to_red(dst_format);
   1467    dst_format = util_format_intensity_to_red(dst_format);
   1468 
   1469    if (!dst_format ||
   1470        !screen->is_format_supported(screen, dst_format, dst->target,
   1471                                     dst->nr_samples, bind)) {
   1472       goto fallback;
   1473    }
   1474 
   1475    if (_mesa_is_bufferobj(unpack->BufferObj)) {
   1476       if (try_pbo_upload(ctx, dims, texImage, format, type, dst_format,
   1477                          xoffset, yoffset, zoffset,
   1478                          width, height, depth, pixels, unpack))
   1479          return;
   1480    }
   1481 
   1482    /* See if the texture format already matches the format and type,
   1483     * in which case the memcpy-based fast path will likely be used and
   1484     * we don't have to blit. */
   1485    if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
   1486                                             type, unpack->SwapBytes, NULL)) {
   1487       goto fallback;
   1488    }
   1489 
   1490    /* Choose the source format. */
   1491    src_format = st_choose_matching_format(st, PIPE_BIND_SAMPLER_VIEW,
   1492                                           format, type, unpack->SwapBytes);
   1493    if (!src_format) {
   1494       goto fallback;
   1495    }
   1496 
   1497    mesa_src_format = st_pipe_format_to_mesa_format(src_format);
   1498 
   1499    /* There is no reason to do this if we cannot use memcpy for the temporary
   1500     * source texture at least. This also takes transfer ops into account,
   1501     * etc. */
   1502    if (!_mesa_texstore_can_use_memcpy(ctx,
   1503                              _mesa_get_format_base_format(mesa_src_format),
   1504                              mesa_src_format, format, type, unpack)) {
   1505       goto fallback;
   1506    }
   1507 
   1508    /* TexSubImage only sets a single cubemap face. */
   1509    if (gl_target == GL_TEXTURE_CUBE_MAP) {
   1510       gl_target = GL_TEXTURE_2D;
   1511    }
   1512    /* TexSubImage can specify subsets of cube map array faces
   1513     * so we need to upload via 2D array instead */
   1514    if (gl_target == GL_TEXTURE_CUBE_MAP_ARRAY) {
   1515       gl_target = GL_TEXTURE_2D_ARRAY;
   1516    }
   1517 
   1518    /* Initialize the source texture description. */
   1519    memset(&src_templ, 0, sizeof(src_templ));
   1520    src_templ.target = gl_target_to_pipe(gl_target);
   1521    src_templ.format = src_format;
   1522    src_templ.bind = PIPE_BIND_SAMPLER_VIEW;
   1523    src_templ.usage = PIPE_USAGE_STAGING;
   1524 
   1525    st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
   1526                                    &src_templ.width0, &src_templ.height0,
   1527                                    &src_templ.depth0, &src_templ.array_size);
   1528 
   1529    /* Check for NPOT texture support. */
   1530    if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES) &&
   1531        (!util_is_power_of_two(src_templ.width0) ||
   1532         !util_is_power_of_two(src_templ.height0) ||
   1533         !util_is_power_of_two(src_templ.depth0))) {
   1534       goto fallback;
   1535    }
   1536 
   1537    /* Create the source texture. */
   1538    src = screen->resource_create(screen, &src_templ);
   1539    if (!src) {
   1540       goto fallback;
   1541    }
   1542 
   1543    /* Map source pixels. */
   1544    pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
   1545                                         format, type, pixels, unpack,
   1546                                         "glTexSubImage");
   1547    if (!pixels) {
   1548       /* This is a GL error. */
   1549       pipe_resource_reference(&src, NULL);
   1550       return;
   1551    }
   1552 
   1553    /* From now on, we need the gallium representation of dimensions. */
   1554    if (gl_target == GL_TEXTURE_1D_ARRAY) {
   1555       zoffset = yoffset;
   1556       yoffset = 0;
   1557       depth = height;
   1558       height = 1;
   1559    }
   1560 
   1561    map = pipe_transfer_map_3d(pipe, src, 0, PIPE_TRANSFER_WRITE, 0, 0, 0,
   1562                               width, height, depth, &transfer);
   1563    if (!map) {
   1564       _mesa_unmap_teximage_pbo(ctx, unpack);
   1565       pipe_resource_reference(&src, NULL);
   1566       goto fallback;
   1567    }
   1568 
   1569    /* Upload pixels (just memcpy). */
   1570    {
   1571       const uint bytesPerRow = width * util_format_get_blocksize(src_format);
   1572       GLuint row, slice;
   1573 
   1574       for (slice = 0; slice < (unsigned) depth; slice++) {
   1575          if (gl_target == GL_TEXTURE_1D_ARRAY) {
   1576             /* 1D array textures.
   1577              * We need to convert gallium coords to GL coords.
   1578              */
   1579             void *src = _mesa_image_address2d(unpack, pixels,
   1580                                                 width, depth, format,
   1581                                                 type, slice, 0);
   1582             memcpy(map, src, bytesPerRow);
   1583          }
   1584          else {
   1585             ubyte *slice_map = map;
   1586 
   1587             for (row = 0; row < (unsigned) height; row++) {
   1588                void *src = _mesa_image_address(dims, unpack, pixels,
   1589                                                  width, height, format,
   1590                                                  type, slice, row, 0);
   1591                memcpy(slice_map, src, bytesPerRow);
   1592                slice_map += transfer->stride;
   1593             }
   1594          }
   1595          map += transfer->layer_stride;
   1596       }
   1597    }
   1598 
   1599    pipe_transfer_unmap(pipe, transfer);
   1600    _mesa_unmap_teximage_pbo(ctx, unpack);
   1601 
   1602    /* Blit. */
   1603    memset(&blit, 0, sizeof(blit));
   1604    blit.src.resource = src;
   1605    blit.src.level = 0;
   1606    blit.src.format = src_format;
   1607    blit.dst.resource = dst;
   1608    blit.dst.level = dst_level;
   1609    blit.dst.format = dst_format;
   1610    blit.src.box.x = blit.src.box.y = blit.src.box.z = 0;
   1611    blit.dst.box.x = xoffset;
   1612    blit.dst.box.y = yoffset;
   1613    blit.dst.box.z = zoffset + dstz;
   1614    blit.src.box.width = blit.dst.box.width = width;
   1615    blit.src.box.height = blit.dst.box.height = height;
   1616    blit.src.box.depth = blit.dst.box.depth = depth;
   1617    blit.mask = st_get_blit_mask(format, texImage->_BaseFormat);
   1618    blit.filter = PIPE_TEX_FILTER_NEAREST;
   1619    blit.scissor_enable = FALSE;
   1620 
   1621    st->pipe->blit(st->pipe, &blit);
   1622 
   1623    pipe_resource_reference(&src, NULL);
   1624    return;
   1625 
   1626 fallback:
   1627    _mesa_store_texsubimage(ctx, dims, texImage, xoffset, yoffset, zoffset,
   1628                            width, height, depth, format, type, pixels,
   1629                            unpack);
   1630 }
   1631 
   1632 static void
   1633 st_TexImage(struct gl_context * ctx, GLuint dims,
   1634             struct gl_texture_image *texImage,
   1635             GLenum format, GLenum type, const void *pixels,
   1636             const struct gl_pixelstore_attrib *unpack)
   1637 {
   1638    assert(dims == 1 || dims == 2 || dims == 3);
   1639 
   1640    prep_teximage(ctx, texImage, format, type);
   1641 
   1642    if (texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
   1643       return;
   1644 
   1645    /* allocate storage for texture data */
   1646    if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
   1647       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
   1648       return;
   1649    }
   1650 
   1651    st_TexSubImage(ctx, dims, texImage, 0, 0, 0,
   1652                   texImage->Width, texImage->Height, texImage->Depth,
   1653                   format, type, pixels, unpack);
   1654 }
   1655 
   1656 
   1657 static void
   1658 st_CompressedTexSubImage(struct gl_context *ctx, GLuint dims,
   1659                          struct gl_texture_image *texImage,
   1660                          GLint x, GLint y, GLint z,
   1661                          GLsizei w, GLsizei h, GLsizei d,
   1662                          GLenum format, GLsizei imageSize, const void *data)
   1663 {
   1664    struct st_context *st = st_context(ctx);
   1665    struct st_texture_image *stImage = st_texture_image(texImage);
   1666    struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
   1667    struct pipe_resource *texture = stImage->pt;
   1668    struct pipe_context *pipe = st->pipe;
   1669    struct pipe_screen *screen = pipe->screen;
   1670    struct pipe_resource *dst = stImage->pt;
   1671    struct pipe_surface *surface = NULL;
   1672    struct compressed_pixelstore store;
   1673    struct st_pbo_addresses addr;
   1674    enum pipe_format copy_format;
   1675    unsigned bw, bh;
   1676    intptr_t buf_offset;
   1677    bool success = false;
   1678 
   1679    /* Check basic pre-conditions for PBO upload */
   1680    if (!st->prefer_blit_based_texture_transfer) {
   1681       goto fallback;
   1682    }
   1683 
   1684    if (!_mesa_is_bufferobj(ctx->Unpack.BufferObj))
   1685       goto fallback;
   1686 
   1687    if (st_etc_fallback(st, texImage)) {
   1688       /* ETC isn't supported and is represented by uncompressed formats. */
   1689       goto fallback;
   1690    }
   1691 
   1692    if (!dst) {
   1693       goto fallback;
   1694    }
   1695 
   1696    if (!st->pbo.upload_enabled ||
   1697        !screen->get_param(screen, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS)) {
   1698       goto fallback;
   1699    }
   1700 
   1701    /* Choose the pipe format for the upload. */
   1702    addr.bytes_per_pixel = util_format_get_blocksize(dst->format);
   1703    bw = util_format_get_blockwidth(dst->format);
   1704    bh = util_format_get_blockheight(dst->format);
   1705 
   1706    switch (addr.bytes_per_pixel) {
   1707    case 8:
   1708       copy_format = PIPE_FORMAT_R16G16B16A16_UINT;
   1709       break;
   1710    case 16:
   1711       copy_format = PIPE_FORMAT_R32G32B32A32_UINT;
   1712       break;
   1713    default:
   1714       goto fallback;
   1715    }
   1716 
   1717    if (!screen->is_format_supported(screen, copy_format, PIPE_BUFFER, 0,
   1718                                     PIPE_BIND_SAMPLER_VIEW)) {
   1719       goto fallback;
   1720    }
   1721 
   1722    if (!screen->is_format_supported(screen, copy_format, dst->target,
   1723                                     dst->nr_samples, PIPE_BIND_RENDER_TARGET)) {
   1724       goto fallback;
   1725    }
   1726 
   1727    /* Interpret the pixelstore settings. */
   1728    _mesa_compute_compressed_pixelstore(dims, texImage->TexFormat, w, h, d,
   1729                                        &ctx->Unpack, &store);
   1730    assert(store.CopyBytesPerRow % addr.bytes_per_pixel == 0);
   1731    assert(store.SkipBytes % addr.bytes_per_pixel == 0);
   1732 
   1733    /* Compute the offset into the buffer */
   1734    buf_offset = (intptr_t)data + store.SkipBytes;
   1735 
   1736    if (buf_offset % addr.bytes_per_pixel) {
   1737       goto fallback;
   1738    }
   1739 
   1740    buf_offset = buf_offset / addr.bytes_per_pixel;
   1741 
   1742    addr.xoffset = x / bw;
   1743    addr.yoffset = y / bh;
   1744    addr.width = store.CopyBytesPerRow / addr.bytes_per_pixel;
   1745    addr.height = store.CopyRowsPerSlice;
   1746    addr.depth = d;
   1747    addr.pixels_per_row = store.TotalBytesPerRow / addr.bytes_per_pixel;
   1748    addr.image_height = store.TotalRowsPerSlice;
   1749 
   1750    if (!st_pbo_addresses_setup(st, st_buffer_object(ctx->Unpack.BufferObj)->buffer,
   1751                                buf_offset, &addr))
   1752       goto fallback;
   1753 
   1754    /* Set up the surface. */
   1755    {
   1756       unsigned level = stObj->pt != stImage->pt ? 0 : texImage->TexObject->MinLevel + texImage->Level;
   1757       unsigned max_layer = util_max_layer(texture, level);
   1758 
   1759       z += texImage->Face + texImage->TexObject->MinLayer;
   1760 
   1761       struct pipe_surface templ;
   1762       memset(&templ, 0, sizeof(templ));
   1763       templ.format = copy_format;
   1764       templ.u.tex.level = level;
   1765       templ.u.tex.first_layer = MIN2(z, max_layer);
   1766       templ.u.tex.last_layer = MIN2(z + d - 1, max_layer);
   1767 
   1768       surface = pipe->create_surface(pipe, texture, &templ);
   1769       if (!surface)
   1770          goto fallback;
   1771    }
   1772 
   1773    success = try_pbo_upload_common(ctx, surface, &addr, copy_format);
   1774 
   1775    pipe_surface_reference(&surface, NULL);
   1776 
   1777    if (success)
   1778       return;
   1779 
   1780 fallback:
   1781    _mesa_store_compressed_texsubimage(ctx, dims, texImage,
   1782                                       x, y, z, w, h, d,
   1783                                       format, imageSize, data);
   1784 }
   1785 
   1786 static void
   1787 st_CompressedTexImage(struct gl_context *ctx, GLuint dims,
   1788                       struct gl_texture_image *texImage,
   1789                       GLsizei imageSize, const void *data)
   1790 {
   1791    prep_teximage(ctx, texImage, GL_NONE, GL_NONE);
   1792 
   1793    /* only 2D and 3D compressed images are supported at this time */
   1794    if (dims == 1) {
   1795       _mesa_problem(ctx, "Unexpected glCompressedTexImage1D call");
   1796       return;
   1797    }
   1798 
   1799    /* This is pretty simple, because unlike the general texstore path we don't
   1800     * have to worry about the usual image unpacking or image transfer
   1801     * operations.
   1802     */
   1803    assert(texImage);
   1804    assert(texImage->Width > 0);
   1805    assert(texImage->Height > 0);
   1806    assert(texImage->Depth > 0);
   1807 
   1808    /* allocate storage for texture data */
   1809    if (!st_AllocTextureImageBuffer(ctx, texImage)) {
   1810       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage%uD", dims);
   1811       return;
   1812    }
   1813 
   1814    st_CompressedTexSubImage(ctx, dims, texImage,
   1815                             0, 0, 0,
   1816                             texImage->Width, texImage->Height, texImage->Depth,
   1817                             texImage->TexFormat,
   1818                             imageSize, data);
   1819 }
   1820 
   1821 
   1822 
   1823 
   1824 /**
   1825  * Called via ctx->Driver.GetTexSubImage()
   1826  *
   1827  * This uses a blit to copy the texture to a texture format which matches
   1828  * the format and type combo and then a fast read-back is done using memcpy.
   1829  * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
   1830  * a format which matches the swizzling.
   1831  *
   1832  * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
   1833  *
   1834  * NOTE: Drivers usually do a blit to convert between tiled and linear
   1835  *       texture layouts during texture uploads/downloads, so the blit
   1836  *       we do here should be free in such cases.
   1837  */
   1838 static void
   1839 st_GetTexSubImage(struct gl_context * ctx,
   1840                   GLint xoffset, GLint yoffset, GLint zoffset,
   1841                   GLsizei width, GLsizei height, GLint depth,
   1842                   GLenum format, GLenum type, void * pixels,
   1843                   struct gl_texture_image *texImage)
   1844 {
   1845    struct st_context *st = st_context(ctx);
   1846    struct pipe_context *pipe = st->pipe;
   1847    struct pipe_screen *screen = pipe->screen;
   1848    struct st_texture_image *stImage = st_texture_image(texImage);
   1849    struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
   1850    struct pipe_resource *src = stObj->pt;
   1851    struct pipe_resource *dst = NULL;
   1852    struct pipe_resource dst_templ;
   1853    enum pipe_format dst_format, src_format;
   1854    mesa_format mesa_format;
   1855    GLenum gl_target = texImage->TexObject->Target;
   1856    enum pipe_texture_target pipe_target;
   1857    unsigned dims;
   1858    struct pipe_blit_info blit;
   1859    unsigned bind;
   1860    struct pipe_transfer *tex_xfer;
   1861    ubyte *map = NULL;
   1862    boolean done = FALSE;
   1863 
   1864    assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
   1865           texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
   1866 
   1867    st_flush_bitmap_cache(st);
   1868 
   1869    if (!st->prefer_blit_based_texture_transfer &&
   1870        !_mesa_is_format_compressed(texImage->TexFormat)) {
   1871       /* Try to avoid the fallback if we're doing texture decompression here */
   1872       goto fallback;
   1873    }
   1874 
   1875    /* Handle non-finalized textures. */
   1876    if (!stImage->pt || stImage->pt != stObj->pt || !src) {
   1877       goto fallback;
   1878    }
   1879 
   1880    /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
   1881     * due to an incomplete stencil blit implementation in some drivers. */
   1882    if (format == GL_DEPTH_STENCIL || format == GL_STENCIL_INDEX) {
   1883       goto fallback;
   1884    }
   1885 
   1886    /* If the base internal format and the texture format don't match, we have
   1887     * to fall back to _mesa_GetTexImage_sw. */
   1888    if (texImage->_BaseFormat !=
   1889        _mesa_get_format_base_format(texImage->TexFormat)) {
   1890       goto fallback;
   1891    }
   1892 
   1893    /* See if the texture format already matches the format and type,
   1894     * in which case the memcpy-based fast path will be used. */
   1895    if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
   1896                                             type, ctx->Pack.SwapBytes, NULL)) {
   1897       goto fallback;
   1898    }
   1899 
   1900    /* Convert the source format to what is expected by GetTexImage
   1901     * and see if it's supported.
   1902     *
   1903     * This only applies to glGetTexImage:
   1904     * - Luminance must be returned as (L,0,0,1).
   1905     * - Luminance alpha must be returned as (L,0,0,A).
   1906     * - Intensity must be returned as (I,0,0,1)
   1907     */
   1908    if (stObj->surface_based)
   1909       src_format = util_format_linear(stObj->surface_format);
   1910    else
   1911       src_format = util_format_linear(src->format);
   1912    src_format = util_format_luminance_to_red(src_format);
   1913    src_format = util_format_intensity_to_red(src_format);
   1914 
   1915    if (!src_format ||
   1916        !screen->is_format_supported(screen, src_format, src->target,
   1917                                     src->nr_samples,
   1918                                     PIPE_BIND_SAMPLER_VIEW)) {
   1919       goto fallback;
   1920    }
   1921 
   1922    if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
   1923       bind = PIPE_BIND_DEPTH_STENCIL;
   1924    else
   1925       bind = PIPE_BIND_RENDER_TARGET;
   1926 
   1927    /* GetTexImage only returns a single face for cubemaps. */
   1928    if (gl_target == GL_TEXTURE_CUBE_MAP) {
   1929       gl_target = GL_TEXTURE_2D;
   1930    }
   1931    pipe_target = gl_target_to_pipe(gl_target);
   1932 
   1933    /* Choose the destination format by finding the best match
   1934     * for the format+type combo. */
   1935    dst_format = st_choose_matching_format(st, bind, format, type,
   1936 					  ctx->Pack.SwapBytes);
   1937 
   1938    if (dst_format == PIPE_FORMAT_NONE) {
   1939       GLenum dst_glformat;
   1940 
   1941       /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
   1942        * where decompression with a blit is always preferred. */
   1943       if (!util_format_is_compressed(src->format)) {
   1944          goto fallback;
   1945       }
   1946 
   1947       /* Set the appropriate format for the decompressed texture.
   1948        * Luminance and sRGB formats shouldn't appear here.*/
   1949       switch (src_format) {
   1950       case PIPE_FORMAT_DXT1_RGB:
   1951       case PIPE_FORMAT_DXT1_RGBA:
   1952       case PIPE_FORMAT_DXT3_RGBA:
   1953       case PIPE_FORMAT_DXT5_RGBA:
   1954       case PIPE_FORMAT_RGTC1_UNORM:
   1955       case PIPE_FORMAT_RGTC2_UNORM:
   1956       case PIPE_FORMAT_ETC1_RGB8:
   1957       case PIPE_FORMAT_BPTC_RGBA_UNORM:
   1958          dst_glformat = GL_RGBA8;
   1959          break;
   1960       case PIPE_FORMAT_RGTC1_SNORM:
   1961       case PIPE_FORMAT_RGTC2_SNORM:
   1962          if (!ctx->Extensions.EXT_texture_snorm)
   1963             goto fallback;
   1964          dst_glformat = GL_RGBA8_SNORM;
   1965          break;
   1966       case PIPE_FORMAT_BPTC_RGB_FLOAT:
   1967       case PIPE_FORMAT_BPTC_RGB_UFLOAT:
   1968          if (!ctx->Extensions.ARB_texture_float)
   1969             goto fallback;
   1970          dst_glformat = GL_RGBA32F;
   1971          break;
   1972       default:
   1973          assert(0);
   1974          goto fallback;
   1975       }
   1976 
   1977       dst_format = st_choose_format(st, dst_glformat, format, type,
   1978                                     pipe_target, 0, bind, FALSE);
   1979 
   1980       if (dst_format == PIPE_FORMAT_NONE) {
   1981          /* unable to get an rgba format!?! */
   1982          goto fallback;
   1983       }
   1984    }
   1985 
   1986    /* create the destination texture of size (width X height X depth) */
   1987    memset(&dst_templ, 0, sizeof(dst_templ));
   1988    dst_templ.target = pipe_target;
   1989    dst_templ.format = dst_format;
   1990    dst_templ.bind = bind;
   1991    dst_templ.usage = PIPE_USAGE_STAGING;
   1992 
   1993    st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
   1994                                    &dst_templ.width0, &dst_templ.height0,
   1995                                    &dst_templ.depth0, &dst_templ.array_size);
   1996 
   1997    dst = screen->resource_create(screen, &dst_templ);
   1998    if (!dst) {
   1999       goto fallback;
   2000    }
   2001 
   2002    /* From now on, we need the gallium representation of dimensions. */
   2003    if (gl_target == GL_TEXTURE_1D_ARRAY) {
   2004       zoffset = yoffset;
   2005       yoffset = 0;
   2006       depth = height;
   2007       height = 1;
   2008    }
   2009 
   2010    assert(texImage->Face == 0 ||
   2011           texImage->TexObject->MinLayer == 0 ||
   2012           zoffset == 0);
   2013 
   2014    memset(&blit, 0, sizeof(blit));
   2015    blit.src.resource = src;
   2016    blit.src.level = texImage->Level + texImage->TexObject->MinLevel;
   2017    blit.src.format = src_format;
   2018    blit.dst.resource = dst;
   2019    blit.dst.level = 0;
   2020    blit.dst.format = dst->format;
   2021    blit.src.box.x = xoffset;
   2022    blit.dst.box.x = 0;
   2023    blit.src.box.y = yoffset;
   2024    blit.dst.box.y = 0;
   2025    blit.src.box.z = texImage->Face + texImage->TexObject->MinLayer + zoffset;
   2026    blit.dst.box.z = 0;
   2027    blit.src.box.width = blit.dst.box.width = width;
   2028    blit.src.box.height = blit.dst.box.height = height;
   2029    blit.src.box.depth = blit.dst.box.depth = depth;
   2030    blit.mask = st_get_blit_mask(texImage->_BaseFormat, format);
   2031    blit.filter = PIPE_TEX_FILTER_NEAREST;
   2032    blit.scissor_enable = FALSE;
   2033 
   2034    /* blit/render/decompress */
   2035    st->pipe->blit(st->pipe, &blit);
   2036 
   2037    pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
   2038 
   2039    map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
   2040                               0, 0, 0, width, height, depth, &tex_xfer);
   2041    if (!map) {
   2042       goto end;
   2043    }
   2044 
   2045    mesa_format = st_pipe_format_to_mesa_format(dst_format);
   2046    dims = _mesa_get_texture_dimensions(gl_target);
   2047 
   2048    /* copy/pack data into user buffer */
   2049    if (_mesa_format_matches_format_and_type(mesa_format, format, type,
   2050                                             ctx->Pack.SwapBytes, NULL)) {
   2051       /* memcpy */
   2052       const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
   2053       GLuint row, slice;
   2054 
   2055       for (slice = 0; slice < depth; slice++) {
   2056          ubyte *slice_map = map;
   2057 
   2058          for (row = 0; row < height; row++) {
   2059             void *dest = _mesa_image_address(dims, &ctx->Pack, pixels,
   2060                                              width, height, format, type,
   2061                                              slice, row, 0);
   2062 
   2063             memcpy(dest, slice_map, bytesPerRow);
   2064 
   2065             slice_map += tex_xfer->stride;
   2066          }
   2067 
   2068          map += tex_xfer->layer_stride;
   2069       }
   2070    }
   2071    else {
   2072       /* format translation via floats */
   2073       GLuint slice;
   2074       GLfloat *rgba;
   2075       uint32_t dstMesaFormat;
   2076       int dstStride, srcStride;
   2077 
   2078       assert(util_format_is_compressed(src->format));
   2079 
   2080       rgba = malloc(width * height * 4 * sizeof(GLfloat));
   2081       if (!rgba) {
   2082          goto end;
   2083       }
   2084 
   2085       if (ST_DEBUG & DEBUG_FALLBACK)
   2086          debug_printf("%s: fallback format translation\n", __func__);
   2087 
   2088       dstMesaFormat = _mesa_format_from_format_and_type(format, type);
   2089       dstStride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
   2090       srcStride = 4 * width * sizeof(GLfloat);
   2091       for (slice = 0; slice < depth; slice++) {
   2092          void *dest = _mesa_image_address(dims, &ctx->Pack, pixels,
   2093                                           width, height, format, type,
   2094                                           slice, 0, 0);
   2095 
   2096          /* get float[4] rgba row from surface */
   2097          pipe_get_tile_rgba_format(tex_xfer, map, 0, 0, width, height,
   2098                                    dst_format, rgba);
   2099 
   2100          _mesa_format_convert(dest, dstMesaFormat, dstStride,
   2101                               rgba, RGBA32_FLOAT, srcStride,
   2102                               width, height, NULL);
   2103 
   2104          /* Handle byte swapping if required */
   2105          if (ctx->Pack.SwapBytes) {
   2106             _mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
   2107                                       width, height, dest, dest);
   2108          }
   2109 
   2110          map += tex_xfer->layer_stride;
   2111       }
   2112 
   2113       free(rgba);
   2114    }
   2115    done = TRUE;
   2116 
   2117 end:
   2118    if (map)
   2119       pipe_transfer_unmap(pipe, tex_xfer);
   2120 
   2121    _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
   2122    pipe_resource_reference(&dst, NULL);
   2123 
   2124 fallback:
   2125    if (!done) {
   2126       _mesa_GetTexSubImage_sw(ctx, xoffset, yoffset, zoffset,
   2127                               width, height, depth,
   2128                               format, type, pixels, texImage);
   2129    }
   2130 }
   2131 
   2132 
   2133 /**
   2134  * Do a CopyTexSubImage operation using a read transfer from the source,
   2135  * a write transfer to the destination and get_tile()/put_tile() to access
   2136  * the pixels/texels.
   2137  *
   2138  * Note: srcY=0=TOP of renderbuffer
   2139  */
   2140 static void
   2141 fallback_copy_texsubimage(struct gl_context *ctx,
   2142                           struct st_renderbuffer *strb,
   2143                           struct st_texture_image *stImage,
   2144                           GLenum baseFormat,
   2145                           GLint destX, GLint destY, GLint slice,
   2146                           GLint srcX, GLint srcY,
   2147                           GLsizei width, GLsizei height)
   2148 {
   2149    struct st_context *st = st_context(ctx);
   2150    struct pipe_context *pipe = st->pipe;
   2151    struct pipe_transfer *src_trans;
   2152    GLubyte *texDest;
   2153    enum pipe_transfer_usage transfer_usage;
   2154    void *map;
   2155    unsigned dst_width = width;
   2156    unsigned dst_height = height;
   2157    unsigned dst_depth = 1;
   2158    struct pipe_transfer *transfer;
   2159 
   2160    if (ST_DEBUG & DEBUG_FALLBACK)
   2161       debug_printf("%s: fallback processing\n", __func__);
   2162 
   2163    if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
   2164       srcY = strb->Base.Height - srcY - height;
   2165    }
   2166 
   2167    map = pipe_transfer_map(pipe,
   2168                            strb->texture,
   2169                            strb->surface->u.tex.level,
   2170                            strb->surface->u.tex.first_layer,
   2171                            PIPE_TRANSFER_READ,
   2172                            srcX, srcY,
   2173                            width, height, &src_trans);
   2174 
   2175    if ((baseFormat == GL_DEPTH_COMPONENT ||
   2176         baseFormat == GL_DEPTH_STENCIL) &&
   2177        util_format_is_depth_and_stencil(stImage->pt->format))
   2178       transfer_usage = PIPE_TRANSFER_READ_WRITE;
   2179    else
   2180       transfer_usage = PIPE_TRANSFER_WRITE;
   2181 
   2182    texDest = st_texture_image_map(st, stImage, transfer_usage,
   2183                                   destX, destY, slice,
   2184                                   dst_width, dst_height, dst_depth,
   2185                                   &transfer);
   2186 
   2187    if (baseFormat == GL_DEPTH_COMPONENT ||
   2188        baseFormat == GL_DEPTH_STENCIL) {
   2189       const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
   2190                                      ctx->Pixel.DepthBias != 0.0F);
   2191       GLint row, yStep;
   2192       uint *data;
   2193 
   2194       /* determine bottom-to-top vs. top-to-bottom order for src buffer */
   2195       if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
   2196          srcY = height - 1;
   2197          yStep = -1;
   2198       }
   2199       else {
   2200          srcY = 0;
   2201          yStep = 1;
   2202       }
   2203 
   2204       data = malloc(width * sizeof(uint));
   2205 
   2206       if (data) {
   2207          /* To avoid a large temp memory allocation, do copy row by row */
   2208          for (row = 0; row < height; row++, srcY += yStep) {
   2209             pipe_get_tile_z(src_trans, map, 0, srcY, width, 1, data);
   2210             if (scaleOrBias) {
   2211                _mesa_scale_and_bias_depth_uint(ctx, width, data);
   2212             }
   2213 
   2214             if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
   2215                pipe_put_tile_z(transfer, texDest + row*transfer->layer_stride,
   2216                                0, 0, width, 1, data);
   2217             }
   2218             else {
   2219                pipe_put_tile_z(transfer, texDest, 0, row, width, 1, data);
   2220             }
   2221          }
   2222       }
   2223       else {
   2224          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
   2225       }
   2226 
   2227       free(data);
   2228    }
   2229    else {
   2230       /* RGBA format */
   2231       GLfloat *tempSrc =
   2232          malloc(width * height * 4 * sizeof(GLfloat));
   2233 
   2234       if (tempSrc && texDest) {
   2235          const GLint dims = 2;
   2236          GLint dstRowStride;
   2237          struct gl_texture_image *texImage = &stImage->base;
   2238          struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
   2239 
   2240          if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
   2241             unpack.Invert = GL_TRUE;
   2242          }
   2243 
   2244          if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
   2245             dstRowStride = transfer->layer_stride;
   2246          }
   2247          else {
   2248             dstRowStride = transfer->stride;
   2249          }
   2250 
   2251          /* get float/RGBA image from framebuffer */
   2252          /* XXX this usually involves a lot of int/float conversion.
   2253           * try to avoid that someday.
   2254           */
   2255          pipe_get_tile_rgba_format(src_trans, map, 0, 0, width, height,
   2256                                    util_format_linear(strb->texture->format),
   2257                                    tempSrc);
   2258 
   2259          /* Store into texture memory.
   2260           * Note that this does some special things such as pixel transfer
   2261           * ops and format conversion.  In particular, if the dest tex format
   2262           * is actually RGBA but the user created the texture as GL_RGB we
   2263           * need to fill-in/override the alpha channel with 1.0.
   2264           */
   2265          _mesa_texstore(ctx, dims,
   2266                         texImage->_BaseFormat,
   2267                         texImage->TexFormat,
   2268                         dstRowStride,
   2269                         &texDest,
   2270                         width, height, 1,
   2271                         GL_RGBA, GL_FLOAT, tempSrc, /* src */
   2272                         &unpack);
   2273       }
   2274       else {
   2275          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
   2276       }
   2277 
   2278       free(tempSrc);
   2279    }
   2280 
   2281    st_texture_image_unmap(st, stImage, slice);
   2282    pipe->transfer_unmap(pipe, src_trans);
   2283 }
   2284 
   2285 
   2286 /**
   2287  * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
   2288  * Note that the region to copy has already been clipped so we know we
   2289  * won't read from outside the source renderbuffer's bounds.
   2290  *
   2291  * Note: srcY=0=Bottom of renderbuffer (GL convention)
   2292  */
   2293 static void
   2294 st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
   2295                    struct gl_texture_image *texImage,
   2296                    GLint destX, GLint destY, GLint slice,
   2297                    struct gl_renderbuffer *rb,
   2298                    GLint srcX, GLint srcY, GLsizei width, GLsizei height)
   2299 {
   2300    struct st_texture_image *stImage = st_texture_image(texImage);
   2301    struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
   2302    struct st_renderbuffer *strb = st_renderbuffer(rb);
   2303    struct st_context *st = st_context(ctx);
   2304    struct pipe_context *pipe = st->pipe;
   2305    struct pipe_screen *screen = pipe->screen;
   2306    struct pipe_blit_info blit;
   2307    enum pipe_format dst_format;
   2308    GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
   2309    unsigned bind;
   2310    GLint srcY0, srcY1;
   2311 
   2312    st_flush_bitmap_cache(st);
   2313    st_invalidate_readpix_cache(st);
   2314 
   2315    assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
   2316           texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
   2317 
   2318    if (!strb || !strb->surface || !stImage->pt) {
   2319       debug_printf("%s: null strb or stImage\n", __func__);
   2320       return;
   2321    }
   2322 
   2323    if (_mesa_texstore_needs_transfer_ops(ctx, texImage->_BaseFormat,
   2324                                          texImage->TexFormat)) {
   2325       goto fallback;
   2326    }
   2327 
   2328    /* The base internal format must match the mesa format, so make sure
   2329     * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
   2330     */
   2331    if (texImage->_BaseFormat !=
   2332        _mesa_get_format_base_format(texImage->TexFormat) ||
   2333        rb->_BaseFormat != _mesa_get_format_base_format(rb->Format)) {
   2334       goto fallback;
   2335    }
   2336 
   2337    /* Choose the destination format to match the TexImage behavior. */
   2338    dst_format = util_format_linear(stImage->pt->format);
   2339    dst_format = util_format_luminance_to_red(dst_format);
   2340    dst_format = util_format_intensity_to_red(dst_format);
   2341 
   2342    /* See if the destination format is supported. */
   2343    if (texImage->_BaseFormat == GL_DEPTH_STENCIL ||
   2344        texImage->_BaseFormat == GL_DEPTH_COMPONENT) {
   2345       bind = PIPE_BIND_DEPTH_STENCIL;
   2346    }
   2347    else {
   2348       bind = PIPE_BIND_RENDER_TARGET;
   2349    }
   2350 
   2351    if (!dst_format ||
   2352        !screen->is_format_supported(screen, dst_format, stImage->pt->target,
   2353                                     stImage->pt->nr_samples, bind)) {
   2354       goto fallback;
   2355    }
   2356 
   2357    /* Y flipping for the main framebuffer. */
   2358    if (do_flip) {
   2359       srcY1 = strb->Base.Height - srcY - height;
   2360       srcY0 = srcY1 + height;
   2361    }
   2362    else {
   2363       srcY0 = srcY;
   2364       srcY1 = srcY0 + height;
   2365    }
   2366 
   2367    /* Blit the texture.
   2368     * This supports flipping, format conversions, and downsampling.
   2369     */
   2370    memset(&blit, 0, sizeof(blit));
   2371    blit.src.resource = strb->texture;
   2372    blit.src.format = util_format_linear(strb->surface->format);
   2373    blit.src.level = strb->surface->u.tex.level;
   2374    blit.src.box.x = srcX;
   2375    blit.src.box.y = srcY0;
   2376    blit.src.box.z = strb->surface->u.tex.first_layer;
   2377    blit.src.box.width = width;
   2378    blit.src.box.height = srcY1 - srcY0;
   2379    blit.src.box.depth = 1;
   2380    blit.dst.resource = stImage->pt;
   2381    blit.dst.format = dst_format;
   2382    blit.dst.level = stObj->pt != stImage->pt ? 0 : texImage->Level + texImage->TexObject->MinLevel;
   2383    blit.dst.box.x = destX;
   2384    blit.dst.box.y = destY;
   2385    blit.dst.box.z = stImage->base.Face + slice + texImage->TexObject->MinLayer;
   2386    blit.dst.box.width = width;
   2387    blit.dst.box.height = height;
   2388    blit.dst.box.depth = 1;
   2389    blit.mask = st_get_blit_mask(rb->_BaseFormat, texImage->_BaseFormat);
   2390    blit.filter = PIPE_TEX_FILTER_NEAREST;
   2391    pipe->blit(pipe, &blit);
   2392    return;
   2393 
   2394 fallback:
   2395    /* software fallback */
   2396    fallback_copy_texsubimage(ctx,
   2397                              strb, stImage, texImage->_BaseFormat,
   2398                              destX, destY, slice,
   2399                              srcX, srcY, width, height);
   2400 }
   2401 
   2402 
   2403 /**
   2404  * Copy image data from stImage into the texture object 'stObj' at level
   2405  * 'dstLevel'.
   2406  */
   2407 static void
   2408 copy_image_data_to_texture(struct st_context *st,
   2409 			   struct st_texture_object *stObj,
   2410                            GLuint dstLevel,
   2411 			   struct st_texture_image *stImage)
   2412 {
   2413    /* debug checks */
   2414    {
   2415       const struct gl_texture_image MAYBE_UNUSED *dstImage =
   2416          stObj->base.Image[stImage->base.Face][dstLevel];
   2417       assert(dstImage);
   2418       assert(dstImage->Width == stImage->base.Width);
   2419       assert(dstImage->Height == stImage->base.Height);
   2420       assert(dstImage->Depth == stImage->base.Depth);
   2421    }
   2422 
   2423    if (stImage->pt) {
   2424       /* Copy potentially with the blitter:
   2425        */
   2426       GLuint src_level;
   2427       if (stImage->pt->last_level == 0)
   2428          src_level = 0;
   2429       else
   2430          src_level = stImage->base.Level;
   2431 
   2432       assert(src_level <= stImage->pt->last_level);
   2433       assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width);
   2434       assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY ||
   2435              u_minify(stImage->pt->height0, src_level) == stImage->base.Height);
   2436       assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY ||
   2437              stImage->pt->target == PIPE_TEXTURE_CUBE_ARRAY ||
   2438              u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth);
   2439 
   2440       st_texture_image_copy(st->pipe,
   2441                             stObj->pt, dstLevel,  /* dest texture, level */
   2442                             stImage->pt, src_level, /* src texture, level */
   2443                             stImage->base.Face);
   2444 
   2445       pipe_resource_reference(&stImage->pt, NULL);
   2446    }
   2447    pipe_resource_reference(&stImage->pt, stObj->pt);
   2448 }
   2449 
   2450 
   2451 /**
   2452  * Called during state validation.  When this function is finished,
   2453  * the texture object should be ready for rendering.
   2454  * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
   2455  */
   2456 GLboolean
   2457 st_finalize_texture(struct gl_context *ctx,
   2458 		    struct pipe_context *pipe,
   2459 		    struct gl_texture_object *tObj,
   2460 		    GLuint cubeMapFace)
   2461 {
   2462    struct st_context *st = st_context(ctx);
   2463    struct st_texture_object *stObj = st_texture_object(tObj);
   2464    const GLuint nr_faces = _mesa_num_tex_faces(stObj->base.Target);
   2465    GLuint face;
   2466    const struct st_texture_image *firstImage;
   2467    enum pipe_format firstImageFormat;
   2468    unsigned ptWidth;
   2469    uint16_t ptHeight, ptDepth, ptLayers, ptNumSamples;
   2470 
   2471    if (tObj->Immutable)
   2472       return GL_TRUE;
   2473 
   2474    if (_mesa_is_texture_complete(tObj, &tObj->Sampler)) {
   2475       /* The texture is complete and we know exactly how many mipmap levels
   2476        * are present/needed.  This is conditional because we may be called
   2477        * from the st_generate_mipmap() function when the texture object is
   2478        * incomplete.  In that case, we'll have set stObj->lastLevel before
   2479        * we get here.
   2480        */
   2481       if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
   2482           stObj->base.Sampler.MinFilter == GL_NEAREST)
   2483          stObj->lastLevel = stObj->base.BaseLevel;
   2484       else
   2485          stObj->lastLevel = stObj->base._MaxLevel;
   2486    }
   2487 
   2488    /* Skip the loop over images in the common case of no images having
   2489     * changed.  But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
   2490     * haven't looked at, then we do need to look at those new images.
   2491     */
   2492    if (!stObj->needs_validation &&
   2493        stObj->base.BaseLevel >= stObj->validated_first_level &&
   2494        stObj->lastLevel <= stObj->validated_last_level) {
   2495       return GL_TRUE;
   2496    }
   2497 
   2498    /* If this texture comes from a window system, there is nothing else to do. */
   2499    if (stObj->surface_based) {
   2500       return GL_TRUE;
   2501    }
   2502 
   2503    firstImage = st_texture_image_const(stObj->base.Image[cubeMapFace][stObj->base.BaseLevel]);
   2504    assert(firstImage);
   2505 
   2506    /* If both firstImage and stObj point to a texture which can contain
   2507     * all active images, favour firstImage.  Note that because of the
   2508     * completeness requirement, we know that the image dimensions
   2509     * will match.
   2510     */
   2511    if (firstImage->pt &&
   2512        firstImage->pt != stObj->pt &&
   2513        (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
   2514       pipe_resource_reference(&stObj->pt, firstImage->pt);
   2515       st_texture_release_all_sampler_views(st, stObj);
   2516    }
   2517 
   2518    /* Find gallium format for the Mesa texture */
   2519    firstImageFormat =
   2520       st_mesa_format_to_pipe_format(st, firstImage->base.TexFormat);
   2521 
   2522    /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
   2523    {
   2524       unsigned width;
   2525       uint16_t height, depth;
   2526 
   2527       st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
   2528                                       firstImage->base.Width2,
   2529                                       firstImage->base.Height2,
   2530                                       firstImage->base.Depth2,
   2531                                       &width, &height, &depth, &ptLayers);
   2532 
   2533       /* If we previously allocated a pipe texture and its sizes are
   2534        * compatible, use them.
   2535        */
   2536       if (stObj->pt &&
   2537           u_minify(stObj->pt->width0, firstImage->base.Level) == width &&
   2538           u_minify(stObj->pt->height0, firstImage->base.Level) == height &&
   2539           u_minify(stObj->pt->depth0, firstImage->base.Level) == depth) {
   2540          ptWidth = stObj->pt->width0;
   2541          ptHeight = stObj->pt->height0;
   2542          ptDepth = stObj->pt->depth0;
   2543       } else {
   2544          /* Otherwise, compute a new level=0 size that is compatible with the
   2545           * base level image.
   2546           */
   2547          ptWidth = width > 1 ? width << firstImage->base.Level : 1;
   2548          ptHeight = height > 1 ? height << firstImage->base.Level : 1;
   2549          ptDepth = depth > 1 ? depth << firstImage->base.Level : 1;
   2550 
   2551          /* If the base level image is 1x1x1, we still need to ensure that the
   2552           * resulting pipe texture ends up with the required number of levels
   2553           * in total.
   2554           */
   2555          if (ptWidth == 1 && ptHeight == 1 && ptDepth == 1) {
   2556             ptWidth <<= firstImage->base.Level;
   2557 
   2558             if (stObj->base.Target == GL_TEXTURE_CUBE_MAP ||
   2559                 stObj->base.Target == GL_TEXTURE_CUBE_MAP_ARRAY)
   2560                ptHeight = ptWidth;
   2561          }
   2562 
   2563          /* At this point, the texture may be incomplete (mismatched cube
   2564           * face sizes, for example).  If that's the case, give up, but
   2565           * don't return GL_FALSE as that would raise an incorrect
   2566           * GL_OUT_OF_MEMORY error.  See Piglit fbo-incomplete-texture-03 test.
   2567           */
   2568          if (!stObj->base._BaseComplete) {
   2569             _mesa_test_texobj_completeness(ctx, &stObj->base);
   2570             if (!stObj->base._BaseComplete) {
   2571                return TRUE;
   2572             }
   2573          }
   2574       }
   2575 
   2576       ptNumSamples = firstImage->base.NumSamples;
   2577    }
   2578 
   2579    /* If we already have a gallium texture, check that it matches the texture
   2580     * object's format, target, size, num_levels, etc.
   2581     */
   2582    if (stObj->pt) {
   2583       if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
   2584           stObj->pt->format != firstImageFormat ||
   2585           stObj->pt->last_level < stObj->lastLevel ||
   2586           stObj->pt->width0 != ptWidth ||
   2587           stObj->pt->height0 != ptHeight ||
   2588           stObj->pt->depth0 != ptDepth ||
   2589           stObj->pt->nr_samples != ptNumSamples ||
   2590           stObj->pt->array_size != ptLayers)
   2591       {
   2592          /* The gallium texture does not match the Mesa texture so delete the
   2593           * gallium texture now.  We'll make a new one below.
   2594           */
   2595          pipe_resource_reference(&stObj->pt, NULL);
   2596          st_texture_release_all_sampler_views(st, stObj);
   2597          st->dirty |= ST_NEW_FRAMEBUFFER;
   2598       }
   2599    }
   2600 
   2601    /* May need to create a new gallium texture:
   2602     */
   2603    if (!stObj->pt) {
   2604       GLuint bindings = default_bindings(st, firstImageFormat);
   2605 
   2606       stObj->pt = st_texture_create(st,
   2607                                     gl_target_to_pipe(stObj->base.Target),
   2608                                     firstImageFormat,
   2609                                     stObj->lastLevel,
   2610                                     ptWidth,
   2611                                     ptHeight,
   2612                                     ptDepth,
   2613                                     ptLayers, ptNumSamples,
   2614                                     bindings);
   2615 
   2616       if (!stObj->pt) {
   2617          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
   2618          return GL_FALSE;
   2619       }
   2620    }
   2621 
   2622    /* Pull in any images not in the object's texture:
   2623     */
   2624    for (face = 0; face < nr_faces; face++) {
   2625       GLuint level;
   2626       for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
   2627          struct st_texture_image *stImage =
   2628             st_texture_image(stObj->base.Image[face][level]);
   2629 
   2630          /* Need to import images in main memory or held in other textures.
   2631           */
   2632          if (stImage && stObj->pt != stImage->pt) {
   2633             GLuint height;
   2634             GLuint depth;
   2635 
   2636             if (stObj->base.Target != GL_TEXTURE_1D_ARRAY)
   2637                height = u_minify(ptHeight, level);
   2638             else
   2639                height = ptLayers;
   2640 
   2641             if (stObj->base.Target == GL_TEXTURE_3D)
   2642                depth = u_minify(ptDepth, level);
   2643             else if (stObj->base.Target == GL_TEXTURE_CUBE_MAP)
   2644                depth = 1;
   2645             else
   2646                depth = ptLayers;
   2647 
   2648             if (level == 0 ||
   2649                 (stImage->base.Width == u_minify(ptWidth, level) &&
   2650                  stImage->base.Height == height &&
   2651                  stImage->base.Depth == depth)) {
   2652                /* src image fits expected dest mipmap level size */
   2653                copy_image_data_to_texture(st, stObj, level, stImage);
   2654             }
   2655          }
   2656       }
   2657    }
   2658 
   2659    stObj->validated_first_level = stObj->base.BaseLevel;
   2660    stObj->validated_last_level = stObj->lastLevel;
   2661    stObj->needs_validation = false;
   2662 
   2663    return GL_TRUE;
   2664 }
   2665 
   2666 /**
   2667  * Allocate a new pipe_resource object
   2668  * width0, height0, depth0 are the dimensions of the level 0 image
   2669  * (the highest resolution).  last_level indicates how many mipmap levels
   2670  * to allocate storage for.  For non-mipmapped textures, this will be zero.
   2671  */
   2672 static struct pipe_resource *
   2673 st_texture_create_from_memory(struct st_context *st,
   2674                               struct st_memory_object *memObj,
   2675                               GLuint64 offset,
   2676                               enum pipe_texture_target target,
   2677                               enum pipe_format format,
   2678                               GLuint last_level,
   2679                               GLuint width0,
   2680                               GLuint height0,
   2681                               GLuint depth0,
   2682                               GLuint layers,
   2683                               GLuint nr_samples,
   2684                               GLuint bind )
   2685 {
   2686    struct pipe_resource pt, *newtex;
   2687    struct pipe_screen *screen = st->pipe->screen;
   2688 
   2689    assert(target < PIPE_MAX_TEXTURE_TYPES);
   2690    assert(width0 > 0);
   2691    assert(height0 > 0);
   2692    assert(depth0 > 0);
   2693    if (target == PIPE_TEXTURE_CUBE)
   2694       assert(layers == 6);
   2695 
   2696    DBG("%s target %d format %s last_level %d\n", __func__,
   2697        (int) target, util_format_name(format), last_level);
   2698 
   2699    assert(format);
   2700    assert(screen->is_format_supported(screen, format, target, 0,
   2701                                       PIPE_BIND_SAMPLER_VIEW));
   2702 
   2703    memset(&pt, 0, sizeof(pt));
   2704    pt.target = target;
   2705    pt.format = format;
   2706    pt.last_level = last_level;
   2707    pt.width0 = width0;
   2708    pt.height0 = height0;
   2709    pt.depth0 = depth0;
   2710    pt.array_size = layers;
   2711    pt.usage = PIPE_USAGE_DEFAULT;
   2712    pt.bind = bind;
   2713    /* only set this for OpenGL textures, not renderbuffers */
   2714    pt.flags = PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY;
   2715    pt.nr_samples = nr_samples;
   2716 
   2717    newtex = screen->resource_from_memobj(screen, &pt, memObj->memory, offset);
   2718 
   2719    assert(!newtex || pipe_is_referenced(&newtex->reference));
   2720 
   2721    return newtex;
   2722 }
   2723 
   2724 /**
   2725  * Allocate texture memory for a whole mipmap stack.
   2726  * Note: for multisample textures if the requested sample count is not
   2727  * supported, we search for the next higher supported sample count.
   2728  */
   2729 static GLboolean
   2730 st_texture_storage(struct gl_context *ctx,
   2731                    struct gl_texture_object *texObj,
   2732                    GLsizei levels, GLsizei width,
   2733                    GLsizei height, GLsizei depth,
   2734                    struct gl_memory_object *memObj,
   2735                    GLuint64 offset)
   2736 {
   2737    const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
   2738    struct gl_texture_image *texImage = texObj->Image[0][0];
   2739    struct st_context *st = st_context(ctx);
   2740    struct st_texture_object *stObj = st_texture_object(texObj);
   2741    struct st_memory_object *smObj = st_memory_object(memObj);
   2742    struct pipe_screen *screen = st->pipe->screen;
   2743    unsigned ptWidth, bindings;
   2744    uint16_t ptHeight, ptDepth, ptLayers;
   2745    enum pipe_format fmt;
   2746    GLint level;
   2747    GLuint num_samples = texImage->NumSamples;
   2748 
   2749    assert(levels > 0);
   2750 
   2751    stObj->lastLevel = levels - 1;
   2752 
   2753    fmt = st_mesa_format_to_pipe_format(st, texImage->TexFormat);
   2754 
   2755    bindings = default_bindings(st, fmt);
   2756 
   2757    if (num_samples > 0) {
   2758       /* Find msaa sample count which is actually supported.  For example,
   2759        * if the user requests 1x but only 4x or 8x msaa is supported, we'll
   2760        * choose 4x here.
   2761        */
   2762       enum pipe_texture_target ptarget = gl_target_to_pipe(texObj->Target);
   2763       boolean found = FALSE;
   2764 
   2765       if (ctx->Const.MaxSamples > 1 && num_samples == 1) {
   2766          /* don't try num_samples = 1 with drivers that support real msaa */
   2767          num_samples = 2;
   2768       }
   2769 
   2770       for (; num_samples <= ctx->Const.MaxSamples; num_samples++) {
   2771          if (screen->is_format_supported(screen, fmt, ptarget,
   2772                                          num_samples,
   2773                                          PIPE_BIND_SAMPLER_VIEW)) {
   2774             /* Update the sample count in gl_texture_image as well. */
   2775             texImage->NumSamples = num_samples;
   2776             found = TRUE;
   2777             break;
   2778          }
   2779       }
   2780 
   2781       if (!found) {
   2782          return GL_FALSE;
   2783       }
   2784    }
   2785 
   2786    st_gl_texture_dims_to_pipe_dims(texObj->Target,
   2787                                    width, height, depth,
   2788                                    &ptWidth, &ptHeight, &ptDepth, &ptLayers);
   2789 
   2790    if (smObj) {
   2791       stObj->pt = st_texture_create_from_memory(st,
   2792                                                 smObj,
   2793                                                 offset,
   2794                                                 gl_target_to_pipe(texObj->Target),
   2795                                                 fmt,
   2796                                                 levels - 1,
   2797                                                 ptWidth,
   2798                                                 ptHeight,
   2799                                                 ptDepth,
   2800                                                 ptLayers, num_samples,
   2801                                                 bindings);
   2802    }
   2803    else {
   2804       stObj->pt = st_texture_create(st,
   2805                                     gl_target_to_pipe(texObj->Target),
   2806                                     fmt,
   2807                                     levels - 1,
   2808                                     ptWidth,
   2809                                     ptHeight,
   2810                                     ptDepth,
   2811                                     ptLayers, num_samples,
   2812                                     bindings);
   2813    }
   2814 
   2815    if (!stObj->pt)
   2816       return GL_FALSE;
   2817 
   2818    /* Set image resource pointers */
   2819    for (level = 0; level < levels; level++) {
   2820       GLuint face;
   2821       for (face = 0; face < numFaces; face++) {
   2822          struct st_texture_image *stImage =
   2823             st_texture_image(texObj->Image[face][level]);
   2824          pipe_resource_reference(&stImage->pt, stObj->pt);
   2825 
   2826          etc_fallback_allocate(st, stImage);
   2827       }
   2828    }
   2829 
   2830    /* The texture is in a validated state, so no need to check later. */
   2831    stObj->needs_validation = false;
   2832    stObj->validated_first_level = 0;
   2833    stObj->validated_last_level = levels - 1;
   2834 
   2835    return GL_TRUE;
   2836 }
   2837 
   2838 /**
   2839  * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
   2840  * for a whole mipmap stack.
   2841  */
   2842 static GLboolean
   2843 st_AllocTextureStorage(struct gl_context *ctx,
   2844                        struct gl_texture_object *texObj,
   2845                        GLsizei levels, GLsizei width,
   2846                        GLsizei height, GLsizei depth)
   2847 {
   2848    return st_texture_storage(ctx, texObj, levels,
   2849                              width, height, depth,
   2850                              NULL, 0);
   2851 }
   2852 
   2853 
   2854 static GLboolean
   2855 st_TestProxyTexImage(struct gl_context *ctx, GLenum target,
   2856                      GLuint numLevels, GLint level,
   2857                      mesa_format format, GLuint numSamples,
   2858                      GLint width, GLint height, GLint depth)
   2859 {
   2860    struct st_context *st = st_context(ctx);
   2861    struct pipe_context *pipe = st->pipe;
   2862 
   2863    if (width == 0 || height == 0 || depth == 0) {
   2864       /* zero-sized images are legal, and always fit! */
   2865       return GL_TRUE;
   2866    }
   2867 
   2868    if (pipe->screen->can_create_resource) {
   2869       /* Ask the gallium driver if the texture is too large */
   2870       struct gl_texture_object *texObj =
   2871          _mesa_get_current_tex_object(ctx, target);
   2872       struct pipe_resource pt;
   2873 
   2874       /* Setup the pipe_resource object
   2875        */
   2876       memset(&pt, 0, sizeof(pt));
   2877 
   2878       pt.target = gl_target_to_pipe(target);
   2879       pt.format = st_mesa_format_to_pipe_format(st, format);
   2880       pt.nr_samples = numSamples;
   2881 
   2882       st_gl_texture_dims_to_pipe_dims(target,
   2883                                       width, height, depth,
   2884                                       &pt.width0, &pt.height0,
   2885                                       &pt.depth0, &pt.array_size);
   2886 
   2887       if (numLevels > 0) {
   2888          /* For immutable textures we know the final number of mip levels */
   2889          pt.last_level = numLevels - 1;
   2890       }
   2891       else if (level == 0 && (texObj->Sampler.MinFilter == GL_LINEAR ||
   2892                               texObj->Sampler.MinFilter == GL_NEAREST)) {
   2893          /* assume just one mipmap level */
   2894          pt.last_level = 0;
   2895       }
   2896       else {
   2897          /* assume a full set of mipmaps */
   2898          pt.last_level = _mesa_logbase2(MAX3(width, height, depth));
   2899       }
   2900 
   2901       return pipe->screen->can_create_resource(pipe->screen, &pt);
   2902    }
   2903    else {
   2904       /* Use core Mesa fallback */
   2905       return _mesa_test_proxy_teximage(ctx, target, numLevels, level, format,
   2906                                        numSamples, width, height, depth);
   2907    }
   2908 }
   2909 
   2910 static GLboolean
   2911 st_TextureView(struct gl_context *ctx,
   2912                struct gl_texture_object *texObj,
   2913                struct gl_texture_object *origTexObj)
   2914 {
   2915    struct st_context *st = st_context(ctx);
   2916    struct st_texture_object *orig = st_texture_object(origTexObj);
   2917    struct st_texture_object *tex = st_texture_object(texObj);
   2918    struct gl_texture_image *image = texObj->Image[0][0];
   2919 
   2920    const int numFaces = _mesa_num_tex_faces(texObj->Target);
   2921    const int numLevels = texObj->NumLevels;
   2922 
   2923    int face;
   2924    int level;
   2925 
   2926    pipe_resource_reference(&tex->pt, orig->pt);
   2927 
   2928    /* Set image resource pointers */
   2929    for (level = 0; level < numLevels; level++) {
   2930       for (face = 0; face < numFaces; face++) {
   2931          struct st_texture_image *stImage =
   2932             st_texture_image(texObj->Image[face][level]);
   2933          pipe_resource_reference(&stImage->pt, tex->pt);
   2934       }
   2935    }
   2936 
   2937    tex->surface_based = GL_TRUE;
   2938    tex->surface_format =
   2939       st_mesa_format_to_pipe_format(st_context(ctx), image->TexFormat);
   2940 
   2941    tex->lastLevel = numLevels - 1;
   2942 
   2943    /* free texture sampler views.  They need to be recreated when we
   2944     * change the texture view parameters.
   2945     */
   2946    st_texture_release_all_sampler_views(st, tex);
   2947 
   2948    /* The texture is in a validated state, so no need to check later. */
   2949    tex->needs_validation = false;
   2950    tex->validated_first_level = 0;
   2951    tex->validated_last_level = numLevels - 1;
   2952 
   2953    return GL_TRUE;
   2954 }
   2955 
   2956 
   2957 /**
   2958  * Find the mipmap level in 'pt' which matches the level described by
   2959  * 'texImage'.
   2960  */
   2961 static unsigned
   2962 find_mipmap_level(const struct gl_texture_image *texImage,
   2963                   const struct pipe_resource *pt)
   2964 {
   2965    const GLenum target = texImage->TexObject->Target;
   2966    GLint texWidth = texImage->Width;
   2967    GLint texHeight = texImage->Height;
   2968    GLint texDepth = texImage->Depth;
   2969    unsigned level, w;
   2970    uint16_t h, d, layers;
   2971 
   2972    st_gl_texture_dims_to_pipe_dims(target, texWidth, texHeight, texDepth,
   2973                                    &w, &h, &d, &layers);
   2974 
   2975    for (level = 0; level <= pt->last_level; level++) {
   2976       if (u_minify(pt->width0, level) == w &&
   2977           u_minify(pt->height0, level) == h &&
   2978           u_minify(pt->depth0, level) == d) {
   2979          return level;
   2980       }
   2981    }
   2982 
   2983    /* If we get here, there must be some sort of inconsistency between
   2984     * the Mesa texture object/images and the gallium resource.
   2985     */
   2986    debug_printf("Inconsistent textures in find_mipmap_level()\n");
   2987 
   2988    return texImage->Level;
   2989 }
   2990 
   2991 
   2992 static void
   2993 st_ClearTexSubImage(struct gl_context *ctx,
   2994                     struct gl_texture_image *texImage,
   2995                     GLint xoffset, GLint yoffset, GLint zoffset,
   2996                     GLsizei width, GLsizei height, GLsizei depth,
   2997                     const void *clearValue)
   2998 {
   2999    static const char zeros[16] = {0};
   3000    struct gl_texture_object *texObj = texImage->TexObject;
   3001    struct st_texture_image *stImage = st_texture_image(texImage);
   3002    struct pipe_resource *pt = stImage->pt;
   3003    struct st_context *st = st_context(ctx);
   3004    struct pipe_context *pipe = st->pipe;
   3005    unsigned level;
   3006    struct pipe_box box;
   3007 
   3008    if (!pt)
   3009       return;
   3010 
   3011    st_flush_bitmap_cache(st);
   3012    st_invalidate_readpix_cache(st);
   3013 
   3014    u_box_3d(xoffset, yoffset, zoffset + texImage->Face,
   3015             width, height, depth, &box);
   3016    if (texObj->Immutable) {
   3017       /* The texture object has to be consistent (no "loose", per-image
   3018        * gallium resources).  If this texture is a view into another
   3019        * texture, we have to apply the MinLevel/Layer offsets.  If this is
   3020        * not a texture view, the offsets will be zero.
   3021        */
   3022       assert(stImage->pt == st_texture_object(texObj)->pt);
   3023       level = texImage->Level + texObj->MinLevel;
   3024       box.z += texObj->MinLayer;
   3025    }
   3026    else {
   3027       /* Texture level sizes may be inconsistent.  We my have "loose",
   3028        * per-image gallium resources.  The texImage->Level may not match
   3029        * the gallium resource texture level.
   3030        */
   3031       level = find_mipmap_level(texImage, pt);
   3032    }
   3033 
   3034    assert(level <= pt->last_level);
   3035 
   3036    pipe->clear_texture(pipe, pt, level, &box, clearValue ? clearValue : zeros);
   3037 }
   3038 
   3039 
   3040 /**
   3041  * Called via the glTexParam*() function, but only when some texture object
   3042  * state has actually changed.
   3043  */
   3044 static void
   3045 st_TexParameter(struct gl_context *ctx,
   3046                 struct gl_texture_object *texObj, GLenum pname)
   3047 {
   3048    struct st_context *st = st_context(ctx);
   3049    struct st_texture_object *stObj = st_texture_object(texObj);
   3050 
   3051    switch (pname) {
   3052    case GL_TEXTURE_BASE_LEVEL:
   3053    case GL_TEXTURE_MAX_LEVEL:
   3054    case GL_DEPTH_TEXTURE_MODE:
   3055    case GL_DEPTH_STENCIL_TEXTURE_MODE:
   3056    case GL_TEXTURE_SRGB_DECODE_EXT:
   3057    case GL_TEXTURE_SWIZZLE_R:
   3058    case GL_TEXTURE_SWIZZLE_G:
   3059    case GL_TEXTURE_SWIZZLE_B:
   3060    case GL_TEXTURE_SWIZZLE_A:
   3061    case GL_TEXTURE_SWIZZLE_RGBA:
   3062    case GL_TEXTURE_BUFFER_SIZE:
   3063    case GL_TEXTURE_BUFFER_OFFSET:
   3064       /* changing any of these texture parameters means we must create
   3065        * new sampler views.
   3066        */
   3067       st_texture_release_all_sampler_views(st, stObj);
   3068       break;
   3069    default:
   3070       ; /* nothing */
   3071    }
   3072 }
   3073 
   3074 static GLboolean
   3075 st_SetTextureStorageForMemoryObject(struct gl_context *ctx,
   3076                                     struct gl_texture_object *texObj,
   3077                                     struct gl_memory_object *memObj,
   3078                                     GLsizei levels, GLsizei width,
   3079                                     GLsizei height, GLsizei depth,
   3080                                     GLuint64 offset)
   3081 {
   3082    return st_texture_storage(ctx, texObj, levels,
   3083                              width, height, depth,
   3084                              memObj, offset);
   3085 }
   3086 
   3087 static GLuint64
   3088 st_NewTextureHandle(struct gl_context *ctx, struct gl_texture_object *texObj,
   3089                     struct gl_sampler_object *sampObj)
   3090 {
   3091    struct st_context *st = st_context(ctx);
   3092    struct st_texture_object *stObj = st_texture_object(texObj);
   3093    struct pipe_context *pipe = st->pipe;
   3094    struct pipe_sampler_view *view;
   3095    struct pipe_sampler_state sampler = {0};
   3096 
   3097    if (texObj->Target != GL_TEXTURE_BUFFER) {
   3098       if (!st_finalize_texture(ctx, pipe, texObj, 0))
   3099          return 0;
   3100 
   3101       st_convert_sampler(st, texObj, sampObj, 0, &sampler);
   3102 
   3103       /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
   3104       view = st_get_texture_sampler_view_from_stobj(st, stObj, sampObj, 0, true);
   3105    } else {
   3106       view = st_get_buffer_sampler_view_from_stobj(st, stObj);
   3107    }
   3108 
   3109    return pipe->create_texture_handle(pipe, view, &sampler);
   3110 }
   3111 
   3112 
   3113 static void
   3114 st_DeleteTextureHandle(struct gl_context *ctx, GLuint64 handle)
   3115 {
   3116    struct st_context *st = st_context(ctx);
   3117    struct pipe_context *pipe = st->pipe;
   3118 
   3119    pipe->delete_texture_handle(pipe, handle);
   3120 }
   3121 
   3122 
   3123 static void
   3124 st_MakeTextureHandleResident(struct gl_context *ctx, GLuint64 handle,
   3125                              bool resident)
   3126 {
   3127    struct st_context *st = st_context(ctx);
   3128    struct pipe_context *pipe = st->pipe;
   3129 
   3130    pipe->make_texture_handle_resident(pipe, handle, resident);
   3131 }
   3132 
   3133 
   3134 static GLuint64
   3135 st_NewImageHandle(struct gl_context *ctx, struct gl_image_unit *imgObj)
   3136 {
   3137    struct st_context *st = st_context(ctx);
   3138    struct pipe_context *pipe = st->pipe;
   3139    struct pipe_image_view image;
   3140 
   3141    st_convert_image(st, imgObj, &image);
   3142 
   3143    return pipe->create_image_handle(pipe, &image);
   3144 }
   3145 
   3146 
   3147 static void
   3148 st_DeleteImageHandle(struct gl_context *ctx, GLuint64 handle)
   3149 {
   3150    struct st_context *st = st_context(ctx);
   3151    struct pipe_context *pipe = st->pipe;
   3152 
   3153    pipe->delete_image_handle(pipe, handle);
   3154 }
   3155 
   3156 
   3157 static void
   3158 st_MakeImageHandleResident(struct gl_context *ctx, GLuint64 handle,
   3159                            GLenum access, bool resident)
   3160 {
   3161    struct st_context *st = st_context(ctx);
   3162    struct pipe_context *pipe = st->pipe;
   3163 
   3164    pipe->make_image_handle_resident(pipe, handle, access, resident);
   3165 }
   3166 
   3167 
   3168 void
   3169 st_init_texture_functions(struct dd_function_table *functions)
   3170 {
   3171    functions->ChooseTextureFormat = st_ChooseTextureFormat;
   3172    functions->QueryInternalFormat = st_QueryInternalFormat;
   3173    functions->TexImage = st_TexImage;
   3174    functions->TexSubImage = st_TexSubImage;
   3175    functions->CompressedTexSubImage = st_CompressedTexSubImage;
   3176    functions->CopyTexSubImage = st_CopyTexSubImage;
   3177    functions->GenerateMipmap = st_generate_mipmap;
   3178 
   3179    functions->GetTexSubImage = st_GetTexSubImage;
   3180 
   3181    /* compressed texture functions */
   3182    functions->CompressedTexImage = st_CompressedTexImage;
   3183 
   3184    functions->NewTextureObject = st_NewTextureObject;
   3185    functions->NewTextureImage = st_NewTextureImage;
   3186    functions->DeleteTextureImage = st_DeleteTextureImage;
   3187    functions->DeleteTexture = st_DeleteTextureObject;
   3188    functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
   3189    functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
   3190    functions->MapTextureImage = st_MapTextureImage;
   3191    functions->UnmapTextureImage = st_UnmapTextureImage;
   3192 
   3193    /* XXX Temporary until we can query pipe's texture sizes */
   3194    functions->TestProxyTexImage = st_TestProxyTexImage;
   3195 
   3196    functions->AllocTextureStorage = st_AllocTextureStorage;
   3197    functions->TextureView = st_TextureView;
   3198    functions->ClearTexSubImage = st_ClearTexSubImage;
   3199 
   3200    functions->TexParameter = st_TexParameter;
   3201 
   3202    /* bindless functions */
   3203    functions->NewTextureHandle = st_NewTextureHandle;
   3204    functions->DeleteTextureHandle = st_DeleteTextureHandle;
   3205    functions->MakeTextureHandleResident = st_MakeTextureHandleResident;
   3206    functions->NewImageHandle = st_NewImageHandle;
   3207    functions->DeleteImageHandle = st_DeleteImageHandle;
   3208    functions->MakeImageHandleResident = st_MakeImageHandleResident;
   3209 
   3210    /* external object functions */
   3211    functions->SetTextureStorageForMemoryObject = st_SetTextureStorageForMemoryObject;
   3212 }
   3213