Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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_fbo.h"
  52. #include "state_tracker/st_cb_flush.h"
  53. #include "state_tracker/st_cb_texture.h"
  54. #include "state_tracker/st_cb_bufferobjects.h"
  55. #include "state_tracker/st_format.h"
  56. #include "state_tracker/st_texture.h"
  57. #include "state_tracker/st_gen_mipmap.h"
  58. #include "state_tracker/st_atom.h"
  59.  
  60. #include "pipe/p_context.h"
  61. #include "pipe/p_defines.h"
  62. #include "util/u_inlines.h"
  63. #include "pipe/p_shader_tokens.h"
  64. #include "util/u_tile.h"
  65. #include "util/u_format.h"
  66. #include "util/u_surface.h"
  67. #include "util/u_sampler.h"
  68. #include "util/u_math.h"
  69. #include "util/u_box.h"
  70.  
  71. #define DBG if (0) printf
  72.  
  73.  
  74. enum pipe_texture_target
  75. gl_target_to_pipe(GLenum target)
  76. {
  77.    switch (target) {
  78.    case GL_TEXTURE_1D:
  79.    case GL_PROXY_TEXTURE_1D:
  80.       return PIPE_TEXTURE_1D;
  81.    case GL_TEXTURE_2D:
  82.    case GL_PROXY_TEXTURE_2D:
  83.    case GL_TEXTURE_EXTERNAL_OES:
  84.    case GL_TEXTURE_2D_MULTISAMPLE:
  85.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
  86.       return PIPE_TEXTURE_2D;
  87.    case GL_TEXTURE_RECTANGLE_NV:
  88.    case GL_PROXY_TEXTURE_RECTANGLE_NV:
  89.       return PIPE_TEXTURE_RECT;
  90.    case GL_TEXTURE_3D:
  91.    case GL_PROXY_TEXTURE_3D:
  92.       return PIPE_TEXTURE_3D;
  93.    case GL_TEXTURE_CUBE_MAP_ARB:
  94.    case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
  95.    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
  96.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
  97.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
  98.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
  99.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
  100.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
  101.       return PIPE_TEXTURE_CUBE;
  102.    case GL_TEXTURE_1D_ARRAY_EXT:
  103.    case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
  104.       return PIPE_TEXTURE_1D_ARRAY;
  105.    case GL_TEXTURE_2D_ARRAY_EXT:
  106.    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
  107.    case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
  108.    case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
  109.       return PIPE_TEXTURE_2D_ARRAY;
  110.    case GL_TEXTURE_BUFFER:
  111.       return PIPE_BUFFER;
  112.    case GL_TEXTURE_CUBE_MAP_ARRAY:
  113.    case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
  114.       return PIPE_TEXTURE_CUBE_ARRAY;
  115.    default:
  116.       assert(0);
  117.       return 0;
  118.    }
  119. }
  120.  
  121.  
  122. /** called via ctx->Driver.NewTextureImage() */
  123. static struct gl_texture_image *
  124. st_NewTextureImage(struct gl_context * ctx)
  125. {
  126.    DBG("%s\n", __func__);
  127.    (void) ctx;
  128.    return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
  129. }
  130.  
  131.  
  132. /** called via ctx->Driver.DeleteTextureImage() */
  133. static void
  134. st_DeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
  135. {
  136.    /* nothing special (yet) for st_texture_image */
  137.    _mesa_delete_texture_image(ctx, img);
  138. }
  139.  
  140.  
  141. /** called via ctx->Driver.NewTextureObject() */
  142. static struct gl_texture_object *
  143. st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
  144. {
  145.    struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
  146.  
  147.    DBG("%s\n", __func__);
  148.    _mesa_initialize_texture_object(ctx, &obj->base, name, target);
  149.  
  150.    return &obj->base;
  151. }
  152.  
  153. /** called via ctx->Driver.DeleteTextureObject() */
  154. static void
  155. st_DeleteTextureObject(struct gl_context *ctx,
  156.                        struct gl_texture_object *texObj)
  157. {
  158.    struct st_context *st = st_context(ctx);
  159.    struct st_texture_object *stObj = st_texture_object(texObj);
  160.  
  161.    pipe_resource_reference(&stObj->pt, NULL);
  162.    st_texture_release_all_sampler_views(st, stObj);
  163.    st_texture_free_sampler_views(stObj);
  164.    _mesa_delete_texture_object(ctx, texObj);
  165. }
  166.  
  167.  
  168. /** called via ctx->Driver.FreeTextureImageBuffer() */
  169. static void
  170. st_FreeTextureImageBuffer(struct gl_context *ctx,
  171.                           struct gl_texture_image *texImage)
  172. {
  173.    struct st_texture_image *stImage = st_texture_image(texImage);
  174.  
  175.    DBG("%s\n", __func__);
  176.  
  177.    if (stImage->pt) {
  178.       pipe_resource_reference(&stImage->pt, NULL);
  179.    }
  180.  
  181.    free(stImage->transfer);
  182.    stImage->transfer = NULL;
  183.    stImage->num_transfers = 0;
  184. }
  185.  
  186.  
  187. /** called via ctx->Driver.MapTextureImage() */
  188. static void
  189. st_MapTextureImage(struct gl_context *ctx,
  190.                    struct gl_texture_image *texImage,
  191.                    GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h,
  192.                    GLbitfield mode,
  193.                    GLubyte **mapOut, GLint *rowStrideOut)
  194. {
  195.    struct st_context *st = st_context(ctx);
  196.    struct st_texture_image *stImage = st_texture_image(texImage);
  197.    unsigned pipeMode;
  198.    GLubyte *map;
  199.    struct pipe_transfer *transfer;
  200.  
  201.    pipeMode = 0x0;
  202.    if (mode & GL_MAP_READ_BIT)
  203.       pipeMode |= PIPE_TRANSFER_READ;
  204.    if (mode & GL_MAP_WRITE_BIT)
  205.       pipeMode |= PIPE_TRANSFER_WRITE;
  206.    if (mode & GL_MAP_INVALIDATE_RANGE_BIT)
  207.       pipeMode |= PIPE_TRANSFER_DISCARD_RANGE;
  208.  
  209.    map = st_texture_image_map(st, stImage, pipeMode, x, y, slice, w, h, 1,
  210.                               &transfer);
  211.    if (map) {
  212.       if ((_mesa_is_format_etc2(texImage->TexFormat) && !st->has_etc2) ||
  213.           (texImage->TexFormat == MESA_FORMAT_ETC1_RGB8 && !st->has_etc1)) {
  214.          /* ETC isn't supported by gallium and it's represented
  215.           * by uncompressed formats. Only write transfers with precompressed
  216.           * data are supported by ES3, which makes this really simple.
  217.           *
  218.           * Just create a temporary storage where the ETC texture will
  219.           * be stored. It will be decompressed in the Unmap function.
  220.           */
  221.          unsigned z = transfer->box.z;
  222.          struct st_texture_image_transfer *itransfer = &stImage->transfer[z];
  223.  
  224.          itransfer->temp_data =
  225.             malloc(_mesa_format_image_size(texImage->TexFormat, w, h, 1));
  226.          itransfer->temp_stride =
  227.             _mesa_format_row_stride(texImage->TexFormat, w);
  228.          itransfer->map = map;
  229.  
  230.          *mapOut = itransfer->temp_data;
  231.          *rowStrideOut = itransfer->temp_stride;
  232.       }
  233.       else {
  234.          /* supported mapping */
  235.          *mapOut = map;
  236.          *rowStrideOut = transfer->stride;
  237.       }
  238.    }
  239.    else {
  240.       *mapOut = NULL;
  241.       *rowStrideOut = 0;
  242.    }
  243. }
  244.  
  245.  
  246. /** called via ctx->Driver.UnmapTextureImage() */
  247. static void
  248. st_UnmapTextureImage(struct gl_context *ctx,
  249.                      struct gl_texture_image *texImage,
  250.                      GLuint slice)
  251. {
  252.    struct st_context *st = st_context(ctx);
  253.    struct st_texture_image *stImage  = st_texture_image(texImage);
  254.  
  255.    if ((_mesa_is_format_etc2(texImage->TexFormat) && !st->has_etc2) ||
  256.        (texImage->TexFormat == MESA_FORMAT_ETC1_RGB8 && !st->has_etc1)) {
  257.       /* Decompress the ETC texture to the mapped one. */
  258.       unsigned z = slice + stImage->base.Face;
  259.       struct st_texture_image_transfer *itransfer = &stImage->transfer[z];
  260.       struct pipe_transfer *transfer = itransfer->transfer;
  261.  
  262.       assert(z == transfer->box.z);
  263.  
  264.       if (texImage->TexFormat == MESA_FORMAT_ETC1_RGB8) {
  265.          _mesa_etc1_unpack_rgba8888(itransfer->map, transfer->stride,
  266.                                     itransfer->temp_data,
  267.                                     itransfer->temp_stride,
  268.                                     transfer->box.width, transfer->box.height);
  269.       }
  270.       else {
  271.          _mesa_unpack_etc2_format(itransfer->map, transfer->stride,
  272.                                   itransfer->temp_data, itransfer->temp_stride,
  273.                                   transfer->box.width, transfer->box.height,
  274.                                   texImage->TexFormat);
  275.       }
  276.  
  277.       free(itransfer->temp_data);
  278.       itransfer->temp_data = NULL;
  279.       itransfer->temp_stride = 0;
  280.       itransfer->map = 0;
  281.    }
  282.  
  283.    st_texture_image_unmap(st, stImage, slice);
  284. }
  285.  
  286.  
  287. /**
  288.  * Return default texture resource binding bitmask for the given format.
  289.  */
  290. static GLuint
  291. default_bindings(struct st_context *st, enum pipe_format format)
  292. {
  293.    struct pipe_screen *screen = st->pipe->screen;
  294.    const unsigned target = PIPE_TEXTURE_2D;
  295.    unsigned bindings;
  296.  
  297.    if (util_format_is_depth_or_stencil(format))
  298.       bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
  299.    else
  300.       bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
  301.  
  302.    if (screen->is_format_supported(screen, format, target, 0, bindings))
  303.       return bindings;
  304.    else {
  305.       /* Try non-sRGB. */
  306.       format = util_format_linear(format);
  307.  
  308.       if (screen->is_format_supported(screen, format, target, 0, bindings))
  309.          return bindings;
  310.       else
  311.          return PIPE_BIND_SAMPLER_VIEW;
  312.    }
  313. }
  314.  
  315.  
  316. /**
  317.  * Given the size of a mipmap image, try to compute the size of the level=0
  318.  * mipmap image.
  319.  *
  320.  * Note that this isn't always accurate for odd-sized, non-POW textures.
  321.  * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
  322.  *
  323.  * \return GL_TRUE for success, GL_FALSE for failure
  324.  */
  325. static GLboolean
  326. guess_base_level_size(GLenum target,
  327.                       GLuint width, GLuint height, GLuint depth, GLuint level,
  328.                       GLuint *width0, GLuint *height0, GLuint *depth0)
  329. {
  330.    assert(width >= 1);
  331.    assert(height >= 1);
  332.    assert(depth >= 1);
  333.  
  334.    if (level > 0) {
  335.       /* Guess the size of the base level.
  336.        * Depending on the image's size, we can't always make a guess here.
  337.        */
  338.       switch (target) {
  339.       case GL_TEXTURE_1D:
  340.       case GL_TEXTURE_1D_ARRAY:
  341.          width <<= level;
  342.          break;
  343.  
  344.       case GL_TEXTURE_2D:
  345.       case GL_TEXTURE_2D_ARRAY:
  346.          /* We can't make a good guess here, because the base level dimensions
  347.           * can be non-square.
  348.           */
  349.          if (width == 1 || height == 1) {
  350.             return GL_FALSE;
  351.          }
  352.          width <<= level;
  353.          height <<= level;
  354.          break;
  355.  
  356.       case GL_TEXTURE_CUBE_MAP:
  357.       case GL_TEXTURE_CUBE_MAP_ARRAY:
  358.          width <<= level;
  359.          height <<= level;
  360.          break;
  361.  
  362.       case GL_TEXTURE_3D:
  363.          /* We can't make a good guess here, because the base level dimensions
  364.           * can be non-cube.
  365.           */
  366.          if (width == 1 || height == 1 || depth == 1) {
  367.             return GL_FALSE;
  368.          }
  369.          width <<= level;
  370.          height <<= level;
  371.          depth <<= level;
  372.          break;
  373.  
  374.       case GL_TEXTURE_RECTANGLE:
  375.          break;
  376.  
  377.       default:
  378.          assert(0);
  379.       }
  380.    }
  381.  
  382.    *width0 = width;
  383.    *height0 = height;
  384.    *depth0 = depth;
  385.  
  386.    return GL_TRUE;
  387. }
  388.  
  389.  
  390. /**
  391.  * Try to allocate a pipe_resource object for the given st_texture_object.
  392.  *
  393.  * We use the given st_texture_image as a clue to determine the size of the
  394.  * mipmap image at level=0.
  395.  *
  396.  * \return GL_TRUE for success, GL_FALSE if out of memory.
  397.  */
  398. static GLboolean
  399. guess_and_alloc_texture(struct st_context *st,
  400.                         struct st_texture_object *stObj,
  401.                         const struct st_texture_image *stImage)
  402. {
  403.    GLuint lastLevel, width, height, depth;
  404.    GLuint bindings;
  405.    GLuint ptWidth, ptHeight, ptDepth, ptLayers;
  406.    enum pipe_format fmt;
  407.  
  408.    DBG("%s\n", __func__);
  409.  
  410.    assert(!stObj->pt);
  411.  
  412.    if (!guess_base_level_size(stObj->base.Target,
  413.                               stImage->base.Width2,
  414.                               stImage->base.Height2,
  415.                               stImage->base.Depth2,
  416.                               stImage->base.Level,
  417.                               &width, &height, &depth)) {
  418.       /* we can't determine the image size at level=0 */
  419.       stObj->width0 = stObj->height0 = stObj->depth0 = 0;
  420.       /* this is not an out of memory error */
  421.       return GL_TRUE;
  422.    }
  423.  
  424.    /* At this point, (width x height x depth) is the expected size of
  425.     * the level=0 mipmap image.
  426.     */
  427.  
  428.    /* Guess a reasonable value for lastLevel.  With OpenGL we have no
  429.     * idea how many mipmap levels will be in a texture until we start
  430.     * to render with it.  Make an educated guess here but be prepared
  431.     * to re-allocating a texture buffer with space for more (or fewer)
  432.     * mipmap levels later.
  433.     */
  434.    if ((stObj->base.Sampler.MinFilter == GL_NEAREST ||
  435.         stObj->base.Sampler.MinFilter == GL_LINEAR ||
  436.         (stObj->base.BaseLevel == 0 &&
  437.          stObj->base.MaxLevel == 0) ||
  438.         stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
  439.         stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) &&
  440.        !stObj->base.GenerateMipmap &&
  441.        stImage->base.Level == 0) {
  442.       /* only alloc space for a single mipmap level */
  443.       lastLevel = 0;
  444.    }
  445.    else {
  446.       /* alloc space for a full mipmap */
  447.       lastLevel = _mesa_get_tex_max_num_levels(stObj->base.Target,
  448.                                                width, height, depth) - 1;
  449.    }
  450.  
  451.    /* Save the level=0 dimensions */
  452.    stObj->width0 = width;
  453.    stObj->height0 = height;
  454.    stObj->depth0 = depth;
  455.  
  456.    fmt = st_mesa_format_to_pipe_format(st, stImage->base.TexFormat);
  457.  
  458.    bindings = default_bindings(st, fmt);
  459.  
  460.    st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
  461.                                    width, height, depth,
  462.                                    &ptWidth, &ptHeight, &ptDepth, &ptLayers);
  463.  
  464.    stObj->pt = st_texture_create(st,
  465.                                  gl_target_to_pipe(stObj->base.Target),
  466.                                  fmt,
  467.                                  lastLevel,
  468.                                  ptWidth,
  469.                                  ptHeight,
  470.                                  ptDepth,
  471.                                  ptLayers, 0,
  472.                                  bindings);
  473.  
  474.    stObj->lastLevel = lastLevel;
  475.  
  476.    DBG("%s returning %d\n", __func__, (stObj->pt != NULL));
  477.  
  478.    return stObj->pt != NULL;
  479. }
  480.  
  481.  
  482. /**
  483.  * Called via ctx->Driver.AllocTextureImageBuffer().
  484.  * If the texture object/buffer already has space for the indicated image,
  485.  * we're done.  Otherwise, allocate memory for the new texture image.
  486.  */
  487. static GLboolean
  488. st_AllocTextureImageBuffer(struct gl_context *ctx,
  489.                            struct gl_texture_image *texImage)
  490. {
  491.    struct st_context *st = st_context(ctx);
  492.    struct st_texture_image *stImage = st_texture_image(texImage);
  493.    struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
  494.    const GLuint level = texImage->Level;
  495.    GLuint width = texImage->Width;
  496.    GLuint height = texImage->Height;
  497.    GLuint depth = texImage->Depth;
  498.  
  499.    DBG("%s\n", __func__);
  500.  
  501.    assert(!stImage->pt); /* xxx this might be wrong */
  502.  
  503.    /* Look if the parent texture object has space for this image */
  504.    if (stObj->pt &&
  505.        level <= stObj->pt->last_level &&
  506.        st_texture_match_image(st, stObj->pt, texImage)) {
  507.       /* this image will fit in the existing texture object's memory */
  508.       pipe_resource_reference(&stImage->pt, stObj->pt);
  509.       return GL_TRUE;
  510.    }
  511.  
  512.    /* The parent texture object does not have space for this image */
  513.  
  514.    pipe_resource_reference(&stObj->pt, NULL);
  515.    st_texture_release_all_sampler_views(st, stObj);
  516.  
  517.    if (!guess_and_alloc_texture(st, stObj, stImage)) {
  518.       /* Probably out of memory.
  519.        * Try flushing any pending rendering, then retry.
  520.        */
  521.       st_finish(st);
  522.       if (!guess_and_alloc_texture(st, stObj, stImage)) {
  523.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
  524.          return GL_FALSE;
  525.       }
  526.    }
  527.  
  528.    if (stObj->pt &&
  529.        st_texture_match_image(st, stObj->pt, texImage)) {
  530.       /* The image will live in the object's mipmap memory */
  531.       pipe_resource_reference(&stImage->pt, stObj->pt);
  532.       assert(stImage->pt);
  533.       return GL_TRUE;
  534.    }
  535.    else {
  536.       /* Create a new, temporary texture/resource/buffer to hold this
  537.        * one texture image.  Note that when we later access this image
  538.        * (either for mapping or copying) we'll want to always specify
  539.        * mipmap level=0, even if the image represents some other mipmap
  540.        * level.
  541.        */
  542.       enum pipe_format format =
  543.          st_mesa_format_to_pipe_format(st, texImage->TexFormat);
  544.       GLuint bindings = default_bindings(st, format);
  545.       GLuint ptWidth, ptHeight, ptDepth, ptLayers;
  546.  
  547.       st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
  548.                                       width, height, depth,
  549.                                       &ptWidth, &ptHeight, &ptDepth, &ptLayers);
  550.  
  551.       stImage->pt = st_texture_create(st,
  552.                                       gl_target_to_pipe(stObj->base.Target),
  553.                                       format,
  554.                                       0, /* lastLevel */
  555.                                       ptWidth,
  556.                                       ptHeight,
  557.                                       ptDepth,
  558.                                       ptLayers, 0,
  559.                                       bindings);
  560.       return stImage->pt != NULL;
  561.    }
  562. }
  563.  
  564.  
  565. /**
  566.  * Preparation prior to glTexImage.  Basically check the 'surface_based'
  567.  * field and switch to a "normal" tex image if necessary.
  568.  */
  569. static void
  570. prep_teximage(struct gl_context *ctx, struct gl_texture_image *texImage,
  571.               GLenum format, GLenum type)
  572. {
  573.    struct gl_texture_object *texObj = texImage->TexObject;
  574.    struct st_texture_object *stObj = st_texture_object(texObj);
  575.  
  576.    /* switch to "normal" */
  577.    if (stObj->surface_based) {
  578.       const GLenum target = texObj->Target;
  579.       const GLuint level = texImage->Level;
  580.       mesa_format texFormat;
  581.  
  582.       _mesa_clear_texture_object(ctx, texObj);
  583.       pipe_resource_reference(&stObj->pt, NULL);
  584.  
  585.       /* oops, need to init this image again */
  586.       texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
  587.                                               texImage->InternalFormat, format,
  588.                                               type);
  589.  
  590.       _mesa_init_teximage_fields(ctx, texImage,
  591.                                  texImage->Width, texImage->Height,
  592.                                  texImage->Depth, texImage->Border,
  593.                                  texImage->InternalFormat, texFormat);
  594.  
  595.       stObj->surface_based = GL_FALSE;
  596.    }
  597. }
  598.  
  599.  
  600. /**
  601.  * Return a writemask for the gallium blit. The parameters can be base
  602.  * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
  603.  */
  604. unsigned
  605. st_get_blit_mask(GLenum srcFormat, GLenum dstFormat)
  606. {
  607.    switch (dstFormat) {
  608.    case GL_DEPTH_STENCIL:
  609.       switch (srcFormat) {
  610.       case GL_DEPTH_STENCIL:
  611.          return PIPE_MASK_ZS;
  612.       case GL_DEPTH_COMPONENT:
  613.          return PIPE_MASK_Z;
  614.       case GL_STENCIL_INDEX:
  615.          return PIPE_MASK_S;
  616.       default:
  617.          assert(0);
  618.          return 0;
  619.       }
  620.  
  621.    case GL_DEPTH_COMPONENT:
  622.       switch (srcFormat) {
  623.       case GL_DEPTH_STENCIL:
  624.       case GL_DEPTH_COMPONENT:
  625.          return PIPE_MASK_Z;
  626.       default:
  627.          assert(0);
  628.          return 0;
  629.       }
  630.  
  631.    case GL_STENCIL_INDEX:
  632.       switch (srcFormat) {
  633.       case GL_STENCIL_INDEX:
  634.          return PIPE_MASK_S;
  635.       default:
  636.          assert(0);
  637.          return 0;
  638.       }
  639.  
  640.    default:
  641.       return PIPE_MASK_RGBA;
  642.    }
  643. }
  644.  
  645.  
  646. static void
  647. st_TexSubImage(struct gl_context *ctx, GLuint dims,
  648.                struct gl_texture_image *texImage,
  649.                GLint xoffset, GLint yoffset, GLint zoffset,
  650.                GLint width, GLint height, GLint depth,
  651.                GLenum format, GLenum type, const void *pixels,
  652.                const struct gl_pixelstore_attrib *unpack)
  653. {
  654.    struct st_context *st = st_context(ctx);
  655.    struct st_texture_image *stImage = st_texture_image(texImage);
  656.    struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
  657.    struct pipe_context *pipe = st->pipe;
  658.    struct pipe_screen *screen = pipe->screen;
  659.    struct pipe_resource *dst = stImage->pt;
  660.    struct pipe_resource *src = NULL;
  661.    struct pipe_resource src_templ;
  662.    struct pipe_transfer *transfer;
  663.    struct pipe_blit_info blit;
  664.    enum pipe_format src_format, dst_format;
  665.    mesa_format mesa_src_format;
  666.    GLenum gl_target = texImage->TexObject->Target;
  667.    unsigned bind;
  668.    GLubyte *map;
  669.  
  670.    assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
  671.           texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
  672.  
  673.    if (!st->prefer_blit_based_texture_transfer) {
  674.       goto fallback;
  675.    }
  676.  
  677.    if (!dst) {
  678.       goto fallback;
  679.    }
  680.  
  681.    /* XXX Fallback for depth-stencil formats due to an incomplete stencil
  682.     * blit implementation in some drivers. */
  683.    if (format == GL_DEPTH_STENCIL) {
  684.       goto fallback;
  685.    }
  686.  
  687.    /* If the base internal format and the texture format don't match,
  688.     * we can't use blit-based TexSubImage. */
  689.    if (texImage->_BaseFormat !=
  690.        _mesa_get_format_base_format(texImage->TexFormat)) {
  691.       goto fallback;
  692.    }
  693.  
  694.    /* See if the texture format already matches the format and type,
  695.     * in which case the memcpy-based fast path will likely be used and
  696.     * we don't have to blit. */
  697.    if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
  698.                                             type, unpack->SwapBytes)) {
  699.       goto fallback;
  700.    }
  701.  
  702.    if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
  703.       bind = PIPE_BIND_DEPTH_STENCIL;
  704.    else
  705.       bind = PIPE_BIND_RENDER_TARGET;
  706.  
  707.    /* See if the destination format is supported.
  708.     * For luminance and intensity, only the red channel is stored there. */
  709.    dst_format = util_format_linear(dst->format);
  710.    dst_format = util_format_luminance_to_red(dst_format);
  711.    dst_format = util_format_intensity_to_red(dst_format);
  712.  
  713.    if (!dst_format ||
  714.        !screen->is_format_supported(screen, dst_format, dst->target,
  715.                                     dst->nr_samples, bind)) {
  716.       goto fallback;
  717.    }
  718.  
  719.    /* Choose the source format. */
  720.    src_format = st_choose_matching_format(st, PIPE_BIND_SAMPLER_VIEW,
  721.                                           format, type, unpack->SwapBytes);
  722.    if (!src_format) {
  723.       goto fallback;
  724.    }
  725.  
  726.    mesa_src_format = st_pipe_format_to_mesa_format(src_format);
  727.  
  728.    /* There is no reason to do this if we cannot use memcpy for the temporary
  729.     * source texture at least. This also takes transfer ops into account,
  730.     * etc. */
  731.    if (!_mesa_texstore_can_use_memcpy(ctx,
  732.                              _mesa_get_format_base_format(mesa_src_format),
  733.                              mesa_src_format, format, type, unpack)) {
  734.       goto fallback;
  735.    }
  736.  
  737.    /* TexSubImage only sets a single cubemap face. */
  738.    if (gl_target == GL_TEXTURE_CUBE_MAP) {
  739.       gl_target = GL_TEXTURE_2D;
  740.    }
  741.    /* TexSubImage can specify subsets of cube map array faces
  742.     * so we need to upload via 2D array instead */
  743.    if (gl_target == GL_TEXTURE_CUBE_MAP_ARRAY) {
  744.       gl_target = GL_TEXTURE_2D_ARRAY;
  745.    }
  746.  
  747.    /* Initialize the source texture description. */
  748.    memset(&src_templ, 0, sizeof(src_templ));
  749.    src_templ.target = gl_target_to_pipe(gl_target);
  750.    src_templ.format = src_format;
  751.    src_templ.bind = PIPE_BIND_SAMPLER_VIEW;
  752.    src_templ.usage = PIPE_USAGE_STAGING;
  753.  
  754.    st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
  755.                                    &src_templ.width0, &src_templ.height0,
  756.                                    &src_templ.depth0, &src_templ.array_size);
  757.  
  758.    /* Check for NPOT texture support. */
  759.    if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES) &&
  760.        (!util_is_power_of_two(src_templ.width0) ||
  761.         !util_is_power_of_two(src_templ.height0) ||
  762.         !util_is_power_of_two(src_templ.depth0))) {
  763.       goto fallback;
  764.    }
  765.  
  766.    /* Create the source texture. */
  767.    src = screen->resource_create(screen, &src_templ);
  768.    if (!src) {
  769.       goto fallback;
  770.    }
  771.  
  772.    /* Map source pixels. */
  773.    pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
  774.                                         format, type, pixels, unpack,
  775.                                         "glTexSubImage");
  776.    if (!pixels) {
  777.       /* This is a GL error. */
  778.       pipe_resource_reference(&src, NULL);
  779.       return;
  780.    }
  781.  
  782.    /* From now on, we need the gallium representation of dimensions. */
  783.    if (gl_target == GL_TEXTURE_1D_ARRAY) {
  784.       zoffset = yoffset;
  785.       yoffset = 0;
  786.       depth = height;
  787.       height = 1;
  788.    }
  789.  
  790.    map = pipe_transfer_map_3d(pipe, src, 0, PIPE_TRANSFER_WRITE, 0, 0, 0,
  791.                               width, height, depth, &transfer);
  792.    if (!map) {
  793.       _mesa_unmap_teximage_pbo(ctx, unpack);
  794.       pipe_resource_reference(&src, NULL);
  795.       goto fallback;
  796.    }
  797.  
  798.    /* Upload pixels (just memcpy). */
  799.    {
  800.       const uint bytesPerRow = width * util_format_get_blocksize(src_format);
  801.       GLuint row, slice;
  802.  
  803.       for (slice = 0; slice < (unsigned) depth; slice++) {
  804.          if (gl_target == GL_TEXTURE_1D_ARRAY) {
  805.             /* 1D array textures.
  806.              * We need to convert gallium coords to GL coords.
  807.              */
  808.             GLvoid *src = _mesa_image_address3d(unpack, pixels,
  809.                                                 width, depth, format,
  810.                                                 type, 0, slice, 0);
  811.             memcpy(map, src, bytesPerRow);
  812.          }
  813.          else {
  814.             ubyte *slice_map = map;
  815.  
  816.             for (row = 0; row < (unsigned) height; row++) {
  817.                GLvoid *src = _mesa_image_address3d(unpack, pixels,
  818.                                                    width, height, format,
  819.                                                    type, slice, row, 0);
  820.                memcpy(slice_map, src, bytesPerRow);
  821.                slice_map += transfer->stride;
  822.             }
  823.          }
  824.          map += transfer->layer_stride;
  825.       }
  826.    }
  827.  
  828.    pipe_transfer_unmap(pipe, transfer);
  829.    _mesa_unmap_teximage_pbo(ctx, unpack);
  830.  
  831.    /* Blit. */
  832.    memset(&blit, 0, sizeof(blit));
  833.    blit.src.resource = src;
  834.    blit.src.level = 0;
  835.    blit.src.format = src_format;
  836.    blit.dst.resource = dst;
  837.    blit.dst.level = stObj->pt != stImage->pt ? 0 : texImage->TexObject->MinLevel + texImage->Level;
  838.    blit.dst.format = dst_format;
  839.    blit.src.box.x = blit.src.box.y = blit.src.box.z = 0;
  840.    blit.dst.box.x = xoffset;
  841.    blit.dst.box.y = yoffset;
  842.    blit.dst.box.z = zoffset + texImage->Face + texImage->TexObject->MinLayer;
  843.    blit.src.box.width = blit.dst.box.width = width;
  844.    blit.src.box.height = blit.dst.box.height = height;
  845.    blit.src.box.depth = blit.dst.box.depth = depth;
  846.    blit.mask = st_get_blit_mask(format, texImage->_BaseFormat);
  847.    blit.filter = PIPE_TEX_FILTER_NEAREST;
  848.    blit.scissor_enable = FALSE;
  849.  
  850.    st->pipe->blit(st->pipe, &blit);
  851.  
  852.    pipe_resource_reference(&src, NULL);
  853.    return;
  854.  
  855. fallback:
  856.    _mesa_store_texsubimage(ctx, dims, texImage, xoffset, yoffset, zoffset,
  857.                            width, height, depth, format, type, pixels,
  858.                            unpack);
  859. }
  860.  
  861. static void
  862. st_TexImage(struct gl_context * ctx, GLuint dims,
  863.             struct gl_texture_image *texImage,
  864.             GLenum format, GLenum type, const void *pixels,
  865.             const struct gl_pixelstore_attrib *unpack)
  866. {
  867.    assert(dims == 1 || dims == 2 || dims == 3);
  868.  
  869.    prep_teximage(ctx, texImage, format, type);
  870.  
  871.    if (texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
  872.       return;
  873.  
  874.    /* allocate storage for texture data */
  875.    if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
  876.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
  877.       return;
  878.    }
  879.  
  880.    st_TexSubImage(ctx, dims, texImage, 0, 0, 0,
  881.                   texImage->Width, texImage->Height, texImage->Depth,
  882.                   format, type, pixels, unpack);
  883. }
  884.  
  885.  
  886. static void
  887. st_CompressedTexImage(struct gl_context *ctx, GLuint dims,
  888.                       struct gl_texture_image *texImage,
  889.                       GLsizei imageSize, const GLvoid *data)
  890. {
  891.    prep_teximage(ctx, texImage, GL_NONE, GL_NONE);
  892.    _mesa_store_compressed_teximage(ctx, dims, texImage, imageSize, data);
  893. }
  894.  
  895.  
  896.  
  897.  
  898. /**
  899.  * Called via ctx->Driver.GetTexImage()
  900.  *
  901.  * This uses a blit to copy the texture to a texture format which matches
  902.  * the format and type combo and then a fast read-back is done using memcpy.
  903.  * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
  904.  * a format which matches the swizzling.
  905.  *
  906.  * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
  907.  *
  908.  * NOTE: Drivers usually do a blit to convert between tiled and linear
  909.  *       texture layouts during texture uploads/downloads, so the blit
  910.  *       we do here should be free in such cases.
  911.  */
  912. static void
  913. st_GetTexImage(struct gl_context * ctx,
  914.                GLenum format, GLenum type, GLvoid * pixels,
  915.                struct gl_texture_image *texImage)
  916. {
  917.    struct st_context *st = st_context(ctx);
  918.    struct pipe_context *pipe = st->pipe;
  919.    struct pipe_screen *screen = pipe->screen;
  920.    GLuint width = texImage->Width;
  921.    GLuint height = texImage->Height;
  922.    GLuint depth = texImage->Depth;
  923.    struct st_texture_image *stImage = st_texture_image(texImage);
  924.    struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
  925.    struct pipe_resource *src = stObj->pt;
  926.    struct pipe_resource *dst = NULL;
  927.    struct pipe_resource dst_templ;
  928.    enum pipe_format dst_format, src_format;
  929.    mesa_format mesa_format;
  930.    GLenum gl_target = texImage->TexObject->Target;
  931.    enum pipe_texture_target pipe_target;
  932.    struct pipe_blit_info blit;
  933.    unsigned bind = PIPE_BIND_TRANSFER_READ;
  934.    struct pipe_transfer *tex_xfer;
  935.    ubyte *map = NULL;
  936.    boolean done = FALSE;
  937.  
  938.    assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
  939.           texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
  940.  
  941.    if (!st->prefer_blit_based_texture_transfer &&
  942.        !_mesa_is_format_compressed(texImage->TexFormat)) {
  943.       /* Try to avoid the fallback if we're doing texture decompression here */
  944.       goto fallback;
  945.    }
  946.  
  947.    if (!stImage->pt || !src) {
  948.       goto fallback;
  949.    }
  950.  
  951.    /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
  952.     * due to an incomplete stencil blit implementation in some drivers. */
  953.    if (format == GL_DEPTH_STENCIL || format == GL_STENCIL_INDEX) {
  954.       goto fallback;
  955.    }
  956.  
  957.    /* If the base internal format and the texture format don't match, we have
  958.     * to fall back to _mesa_GetTexImage_sw. */
  959.    if (texImage->_BaseFormat !=
  960.        _mesa_get_format_base_format(texImage->TexFormat)) {
  961.       goto fallback;
  962.    }
  963.  
  964.    /* See if the texture format already matches the format and type,
  965.     * in which case the memcpy-based fast path will be used. */
  966.    if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
  967.                                             type, ctx->Pack.SwapBytes)) {
  968.       goto fallback;
  969.    }
  970.  
  971.    /* Convert the source format to what is expected by GetTexImage
  972.     * and see if it's supported.
  973.     *
  974.     * This only applies to glGetTexImage:
  975.     * - Luminance must be returned as (L,0,0,1).
  976.     * - Luminance alpha must be returned as (L,0,0,A).
  977.     * - Intensity must be returned as (I,0,0,1)
  978.     */
  979.    if (stObj->surface_based)
  980.       src_format = util_format_linear(stObj->surface_format);
  981.    else
  982.       src_format = util_format_linear(src->format);
  983.    src_format = util_format_luminance_to_red(src_format);
  984.    src_format = util_format_intensity_to_red(src_format);
  985.  
  986.    if (!src_format ||
  987.        !screen->is_format_supported(screen, src_format, src->target,
  988.                                     src->nr_samples,
  989.                                     PIPE_BIND_SAMPLER_VIEW)) {
  990.       goto fallback;
  991.    }
  992.  
  993.    if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
  994.       bind |= PIPE_BIND_DEPTH_STENCIL;
  995.    else
  996.       bind |= PIPE_BIND_RENDER_TARGET;
  997.  
  998.    /* GetTexImage only returns a single face for cubemaps. */
  999.    if (gl_target == GL_TEXTURE_CUBE_MAP) {
  1000.       gl_target = GL_TEXTURE_2D;
  1001.    }
  1002.    pipe_target = gl_target_to_pipe(gl_target);
  1003.  
  1004.    /* Choose the destination format by finding the best match
  1005.     * for the format+type combo. */
  1006.    dst_format = st_choose_matching_format(st, bind, format, type,
  1007.                                           ctx->Pack.SwapBytes);
  1008.  
  1009.    if (dst_format == PIPE_FORMAT_NONE) {
  1010.       GLenum dst_glformat;
  1011.  
  1012.       /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
  1013.        * where decompression with a blit is always preferred. */
  1014.       if (!util_format_is_compressed(src->format)) {
  1015.          goto fallback;
  1016.       }
  1017.  
  1018.       /* Set the appropriate format for the decompressed texture.
  1019.        * Luminance and sRGB formats shouldn't appear here.*/
  1020.       switch (src_format) {
  1021.       case PIPE_FORMAT_DXT1_RGB:
  1022.       case PIPE_FORMAT_DXT1_RGBA:
  1023.       case PIPE_FORMAT_DXT3_RGBA:
  1024.       case PIPE_FORMAT_DXT5_RGBA:
  1025.       case PIPE_FORMAT_RGTC1_UNORM:
  1026.       case PIPE_FORMAT_RGTC2_UNORM:
  1027.       case PIPE_FORMAT_ETC1_RGB8:
  1028.       case PIPE_FORMAT_BPTC_RGBA_UNORM:
  1029.          dst_glformat = GL_RGBA8;
  1030.          break;
  1031.       case PIPE_FORMAT_RGTC1_SNORM:
  1032.       case PIPE_FORMAT_RGTC2_SNORM:
  1033.          if (!ctx->Extensions.EXT_texture_snorm)
  1034.             goto fallback;
  1035.          dst_glformat = GL_RGBA8_SNORM;
  1036.          break;
  1037.       case PIPE_FORMAT_BPTC_RGB_FLOAT:
  1038.       case PIPE_FORMAT_BPTC_RGB_UFLOAT:
  1039.          if (!ctx->Extensions.ARB_texture_float)
  1040.             goto fallback;
  1041.          dst_glformat = GL_RGBA32F;
  1042.          break;
  1043.       default:
  1044.          assert(0);
  1045.          goto fallback;
  1046.       }
  1047.  
  1048.       dst_format = st_choose_format(st, dst_glformat, format, type,
  1049.                                     pipe_target, 0, bind, FALSE);
  1050.  
  1051.       if (dst_format == PIPE_FORMAT_NONE) {
  1052.          /* unable to get an rgba format!?! */
  1053.          goto fallback;
  1054.       }
  1055.    }
  1056.  
  1057.    /* create the destination texture */
  1058.    memset(&dst_templ, 0, sizeof(dst_templ));
  1059.    dst_templ.target = pipe_target;
  1060.    dst_templ.format = dst_format;
  1061.    dst_templ.bind = bind;
  1062.    dst_templ.usage = PIPE_USAGE_STAGING;
  1063.  
  1064.    st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
  1065.                                    &dst_templ.width0, &dst_templ.height0,
  1066.                                    &dst_templ.depth0, &dst_templ.array_size);
  1067.  
  1068.    dst = screen->resource_create(screen, &dst_templ);
  1069.    if (!dst) {
  1070.       goto fallback;
  1071.    }
  1072.  
  1073.    /* From now on, we need the gallium representation of dimensions. */
  1074.    if (gl_target == GL_TEXTURE_1D_ARRAY) {
  1075.       depth = height;
  1076.       height = 1;
  1077.    }
  1078.  
  1079.    memset(&blit, 0, sizeof(blit));
  1080.    blit.src.resource = src;
  1081.    blit.src.level = texImage->Level + texImage->TexObject->MinLevel;
  1082.    blit.src.format = src_format;
  1083.    blit.dst.resource = dst;
  1084.    blit.dst.level = 0;
  1085.    blit.dst.format = dst->format;
  1086.    blit.src.box.x = blit.dst.box.x = 0;
  1087.    blit.src.box.y = blit.dst.box.y = 0;
  1088.    blit.src.box.z = texImage->Face + texImage->TexObject->MinLayer;
  1089.    blit.dst.box.z = 0;
  1090.    blit.src.box.width = blit.dst.box.width = width;
  1091.    blit.src.box.height = blit.dst.box.height = height;
  1092.    blit.src.box.depth = blit.dst.box.depth = depth;
  1093.    blit.mask = st_get_blit_mask(texImage->_BaseFormat, format);
  1094.    blit.filter = PIPE_TEX_FILTER_NEAREST;
  1095.    blit.scissor_enable = FALSE;
  1096.  
  1097.    /* blit/render/decompress */
  1098.    st->pipe->blit(st->pipe, &blit);
  1099.  
  1100.    pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
  1101.  
  1102.    map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
  1103.                               0, 0, 0, width, height, depth, &tex_xfer);
  1104.    if (!map) {
  1105.       goto end;
  1106.    }
  1107.  
  1108.    mesa_format = st_pipe_format_to_mesa_format(dst_format);
  1109.  
  1110.    /* copy/pack data into user buffer */
  1111.    if (_mesa_format_matches_format_and_type(mesa_format, format, type,
  1112.                                             ctx->Pack.SwapBytes)) {
  1113.       /* memcpy */
  1114.       const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
  1115.       GLuint row, slice;
  1116.  
  1117.       for (slice = 0; slice < depth; slice++) {
  1118.          if (gl_target == GL_TEXTURE_1D_ARRAY) {
  1119.             /* 1D array textures.
  1120.              * We need to convert gallium coords to GL coords.
  1121.              */
  1122.             GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
  1123.                                                  width, depth, format,
  1124.                                                  type, 0, slice, 0);
  1125.             memcpy(dest, map, bytesPerRow);
  1126.          }
  1127.          else {
  1128.             ubyte *slice_map = map;
  1129.  
  1130.             for (row = 0; row < height; row++) {
  1131.                GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
  1132.                                                     width, height, format,
  1133.                                                     type, slice, row, 0);
  1134.                memcpy(dest, slice_map, bytesPerRow);
  1135.                slice_map += tex_xfer->stride;
  1136.             }
  1137.          }
  1138.          map += tex_xfer->layer_stride;
  1139.       }
  1140.    }
  1141.    else {
  1142.       /* format translation via floats */
  1143.       GLuint row, slice;
  1144.       GLfloat *rgba;
  1145.       uint32_t dstMesaFormat;
  1146.       int dstStride, srcStride;
  1147.  
  1148.       assert(util_format_is_compressed(src->format));
  1149.  
  1150.       rgba = malloc(width * 4 * sizeof(GLfloat));
  1151.       if (!rgba) {
  1152.          goto end;
  1153.       }
  1154.  
  1155.       if (ST_DEBUG & DEBUG_FALLBACK)
  1156.          debug_printf("%s: fallback format translation\n", __func__);
  1157.  
  1158.       dstMesaFormat = _mesa_format_from_format_and_type(format, type);
  1159.       dstStride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
  1160.       srcStride = 4 * width * sizeof(GLfloat);
  1161.       for (slice = 0; slice < depth; slice++) {
  1162.          if (gl_target == GL_TEXTURE_1D_ARRAY) {
  1163.             /* 1D array textures.
  1164.              * We need to convert gallium coords to GL coords.
  1165.              */
  1166.             GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
  1167.                                                  width, depth, format,
  1168.                                                  type, 0, slice, 0);
  1169.  
  1170.             /* get float[4] rgba row from surface */
  1171.             pipe_get_tile_rgba_format(tex_xfer, map, 0, 0, width, 1,
  1172.                                       dst_format, rgba);
  1173.  
  1174.             _mesa_format_convert(dest, dstMesaFormat, dstStride,
  1175.                                  rgba, RGBA32_FLOAT, srcStride,
  1176.                                  width, 1, NULL);
  1177.          }
  1178.          else {
  1179.             for (row = 0; row < height; row++) {
  1180.                GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
  1181.                                                     width, height, format,
  1182.                                                     type, slice, row, 0);
  1183.  
  1184.                /* get float[4] rgba row from surface */
  1185.                pipe_get_tile_rgba_format(tex_xfer, map, 0, row, width, 1,
  1186.                                          dst_format, rgba);
  1187.  
  1188.                _mesa_format_convert(dest, dstMesaFormat, dstStride,
  1189.                                     rgba, RGBA32_FLOAT, srcStride,
  1190.                                     width, 1, NULL);
  1191.             }
  1192.          }
  1193.          map += tex_xfer->layer_stride;
  1194.       }
  1195.  
  1196.       free(rgba);
  1197.    }
  1198.    done = TRUE;
  1199.  
  1200. end:
  1201.    if (map)
  1202.       pipe_transfer_unmap(pipe, tex_xfer);
  1203.  
  1204.    _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
  1205.    pipe_resource_reference(&dst, NULL);
  1206.  
  1207. fallback:
  1208.    if (!done) {
  1209.       _mesa_GetTexImage_sw(ctx, format, type, pixels, texImage);
  1210.    }
  1211. }
  1212.  
  1213.  
  1214. /**
  1215.  * Do a CopyTexSubImage operation using a read transfer from the source,
  1216.  * a write transfer to the destination and get_tile()/put_tile() to access
  1217.  * the pixels/texels.
  1218.  *
  1219.  * Note: srcY=0=TOP of renderbuffer
  1220.  */
  1221. static void
  1222. fallback_copy_texsubimage(struct gl_context *ctx,
  1223.                           struct st_renderbuffer *strb,
  1224.                           struct st_texture_image *stImage,
  1225.                           GLenum baseFormat,
  1226.                           GLint destX, GLint destY, GLint slice,
  1227.                           GLint srcX, GLint srcY,
  1228.                           GLsizei width, GLsizei height)
  1229. {
  1230.    struct st_context *st = st_context(ctx);
  1231.    struct pipe_context *pipe = st->pipe;
  1232.    struct pipe_transfer *src_trans;
  1233.    GLubyte *texDest;
  1234.    enum pipe_transfer_usage transfer_usage;
  1235.    void *map;
  1236.    unsigned dst_width = width;
  1237.    unsigned dst_height = height;
  1238.    unsigned dst_depth = 1;
  1239.    struct pipe_transfer *transfer;
  1240.  
  1241.    if (ST_DEBUG & DEBUG_FALLBACK)
  1242.       debug_printf("%s: fallback processing\n", __func__);
  1243.  
  1244.    if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
  1245.       srcY = strb->Base.Height - srcY - height;
  1246.    }
  1247.  
  1248.    map = pipe_transfer_map(pipe,
  1249.                            strb->texture,
  1250.                            strb->surface->u.tex.level,
  1251.                            strb->surface->u.tex.first_layer,
  1252.                            PIPE_TRANSFER_READ,
  1253.                            srcX, srcY,
  1254.                            width, height, &src_trans);
  1255.  
  1256.    if ((baseFormat == GL_DEPTH_COMPONENT ||
  1257.         baseFormat == GL_DEPTH_STENCIL) &&
  1258.        util_format_is_depth_and_stencil(stImage->pt->format))
  1259.       transfer_usage = PIPE_TRANSFER_READ_WRITE;
  1260.    else
  1261.       transfer_usage = PIPE_TRANSFER_WRITE;
  1262.  
  1263.    texDest = st_texture_image_map(st, stImage, transfer_usage,
  1264.                                   destX, destY, slice,
  1265.                                   dst_width, dst_height, dst_depth,
  1266.                                   &transfer);
  1267.  
  1268.    if (baseFormat == GL_DEPTH_COMPONENT ||
  1269.        baseFormat == GL_DEPTH_STENCIL) {
  1270.       const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
  1271.                                      ctx->Pixel.DepthBias != 0.0F);
  1272.       GLint row, yStep;
  1273.       uint *data;
  1274.  
  1275.       /* determine bottom-to-top vs. top-to-bottom order for src buffer */
  1276.       if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
  1277.          srcY = height - 1;
  1278.          yStep = -1;
  1279.       }
  1280.       else {
  1281.          srcY = 0;
  1282.          yStep = 1;
  1283.       }
  1284.  
  1285.       data = malloc(width * sizeof(uint));
  1286.  
  1287.       if (data) {
  1288.          /* To avoid a large temp memory allocation, do copy row by row */
  1289.          for (row = 0; row < height; row++, srcY += yStep) {
  1290.             pipe_get_tile_z(src_trans, map, 0, srcY, width, 1, data);
  1291.             if (scaleOrBias) {
  1292.                _mesa_scale_and_bias_depth_uint(ctx, width, data);
  1293.             }
  1294.  
  1295.             if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
  1296.                pipe_put_tile_z(transfer, texDest + row*transfer->layer_stride,
  1297.                                0, 0, width, 1, data);
  1298.             }
  1299.             else {
  1300.                pipe_put_tile_z(transfer, texDest, 0, row, width, 1, data);
  1301.             }
  1302.          }
  1303.       }
  1304.       else {
  1305.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
  1306.       }
  1307.  
  1308.       free(data);
  1309.    }
  1310.    else {
  1311.       /* RGBA format */
  1312.       GLfloat *tempSrc =
  1313.          malloc(width * height * 4 * sizeof(GLfloat));
  1314.  
  1315.       if (tempSrc && texDest) {
  1316.          const GLint dims = 2;
  1317.          GLint dstRowStride;
  1318.          struct gl_texture_image *texImage = &stImage->base;
  1319.          struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
  1320.  
  1321.          if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
  1322.             unpack.Invert = GL_TRUE;
  1323.          }
  1324.  
  1325.          if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
  1326.             dstRowStride = transfer->layer_stride;
  1327.          }
  1328.          else {
  1329.             dstRowStride = transfer->stride;
  1330.          }
  1331.  
  1332.          /* get float/RGBA image from framebuffer */
  1333.          /* XXX this usually involves a lot of int/float conversion.
  1334.           * try to avoid that someday.
  1335.           */
  1336.          pipe_get_tile_rgba_format(src_trans, map, 0, 0, width, height,
  1337.                                    util_format_linear(strb->texture->format),
  1338.                                    tempSrc);
  1339.  
  1340.          /* Store into texture memory.
  1341.           * Note that this does some special things such as pixel transfer
  1342.           * ops and format conversion.  In particular, if the dest tex format
  1343.           * is actually RGBA but the user created the texture as GL_RGB we
  1344.           * need to fill-in/override the alpha channel with 1.0.
  1345.           */
  1346.          _mesa_texstore(ctx, dims,
  1347.                         texImage->_BaseFormat,
  1348.                         texImage->TexFormat,
  1349.                         dstRowStride,
  1350.                         &texDest,
  1351.                         width, height, 1,
  1352.                         GL_RGBA, GL_FLOAT, tempSrc, /* src */
  1353.                         &unpack);
  1354.       }
  1355.       else {
  1356.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
  1357.       }
  1358.  
  1359.       free(tempSrc);
  1360.    }
  1361.  
  1362.    st_texture_image_unmap(st, stImage, slice);
  1363.    pipe->transfer_unmap(pipe, src_trans);
  1364. }
  1365.  
  1366.  
  1367. /**
  1368.  * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
  1369.  * Note that the region to copy has already been clipped so we know we
  1370.  * won't read from outside the source renderbuffer's bounds.
  1371.  *
  1372.  * Note: srcY=0=Bottom of renderbuffer (GL convention)
  1373.  */
  1374. static void
  1375. st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
  1376.                    struct gl_texture_image *texImage,
  1377.                    GLint destX, GLint destY, GLint slice,
  1378.                    struct gl_renderbuffer *rb,
  1379.                    GLint srcX, GLint srcY, GLsizei width, GLsizei height)
  1380. {
  1381.    struct st_texture_image *stImage = st_texture_image(texImage);
  1382.    struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
  1383.    struct st_renderbuffer *strb = st_renderbuffer(rb);
  1384.    struct st_context *st = st_context(ctx);
  1385.    struct pipe_context *pipe = st->pipe;
  1386.    struct pipe_screen *screen = pipe->screen;
  1387.    struct pipe_blit_info blit;
  1388.    enum pipe_format dst_format;
  1389.    GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
  1390.    unsigned bind;
  1391.    GLint srcY0, srcY1;
  1392.  
  1393.    assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
  1394.           texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
  1395.  
  1396.    if (!strb || !strb->surface || !stImage->pt) {
  1397.       debug_printf("%s: null strb or stImage\n", __func__);
  1398.       return;
  1399.    }
  1400.  
  1401.    if (_mesa_texstore_needs_transfer_ops(ctx, texImage->_BaseFormat,
  1402.                                          texImage->TexFormat)) {
  1403.       goto fallback;
  1404.    }
  1405.  
  1406.    /* The base internal format must match the mesa format, so make sure
  1407.     * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
  1408.     */
  1409.    if (texImage->_BaseFormat !=
  1410.        _mesa_get_format_base_format(texImage->TexFormat) ||
  1411.        rb->_BaseFormat != _mesa_get_format_base_format(rb->Format)) {
  1412.       goto fallback;
  1413.    }
  1414.  
  1415.    /* Choose the destination format to match the TexImage behavior. */
  1416.    dst_format = util_format_linear(stImage->pt->format);
  1417.    dst_format = util_format_luminance_to_red(dst_format);
  1418.    dst_format = util_format_intensity_to_red(dst_format);
  1419.  
  1420.    /* See if the destination format is supported. */
  1421.    if (texImage->_BaseFormat == GL_DEPTH_STENCIL ||
  1422.        texImage->_BaseFormat == GL_DEPTH_COMPONENT) {
  1423.       bind = PIPE_BIND_DEPTH_STENCIL;
  1424.    }
  1425.    else {
  1426.       bind = PIPE_BIND_RENDER_TARGET;
  1427.    }
  1428.  
  1429.    if (!dst_format ||
  1430.        !screen->is_format_supported(screen, dst_format, stImage->pt->target,
  1431.                                     stImage->pt->nr_samples, bind)) {
  1432.       goto fallback;
  1433.    }
  1434.  
  1435.    /* Y flipping for the main framebuffer. */
  1436.    if (do_flip) {
  1437.       srcY1 = strb->Base.Height - srcY - height;
  1438.       srcY0 = srcY1 + height;
  1439.    }
  1440.    else {
  1441.       srcY0 = srcY;
  1442.       srcY1 = srcY0 + height;
  1443.    }
  1444.  
  1445.    /* Blit the texture.
  1446.     * This supports flipping, format conversions, and downsampling.
  1447.     */
  1448.    memset(&blit, 0, sizeof(blit));
  1449.    blit.src.resource = strb->texture;
  1450.    blit.src.format = util_format_linear(strb->surface->format);
  1451.    blit.src.level = strb->surface->u.tex.level;
  1452.    blit.src.box.x = srcX;
  1453.    blit.src.box.y = srcY0;
  1454.    blit.src.box.z = strb->surface->u.tex.first_layer;
  1455.    blit.src.box.width = width;
  1456.    blit.src.box.height = srcY1 - srcY0;
  1457.    blit.src.box.depth = 1;
  1458.    blit.dst.resource = stImage->pt;
  1459.    blit.dst.format = dst_format;
  1460.    blit.dst.level = stObj->pt != stImage->pt ? 0 : texImage->Level + texImage->TexObject->MinLevel;
  1461.    blit.dst.box.x = destX;
  1462.    blit.dst.box.y = destY;
  1463.    blit.dst.box.z = stImage->base.Face + slice + texImage->TexObject->MinLayer;
  1464.    blit.dst.box.width = width;
  1465.    blit.dst.box.height = height;
  1466.    blit.dst.box.depth = 1;
  1467.    blit.mask = st_get_blit_mask(rb->_BaseFormat, texImage->_BaseFormat);
  1468.    blit.filter = PIPE_TEX_FILTER_NEAREST;
  1469.    pipe->blit(pipe, &blit);
  1470.    return;
  1471.  
  1472. fallback:
  1473.    /* software fallback */
  1474.    fallback_copy_texsubimage(ctx,
  1475.                              strb, stImage, texImage->_BaseFormat,
  1476.                              destX, destY, slice,
  1477.                              srcX, srcY, width, height);
  1478. }
  1479.  
  1480.  
  1481. /**
  1482.  * Copy image data from stImage into the texture object 'stObj' at level
  1483.  * 'dstLevel'.
  1484.  */
  1485. static void
  1486. copy_image_data_to_texture(struct st_context *st,
  1487.                            struct st_texture_object *stObj,
  1488.                            GLuint dstLevel,
  1489.                            struct st_texture_image *stImage)
  1490. {
  1491.    /* debug checks */
  1492.    {
  1493.       const struct gl_texture_image *dstImage =
  1494.          stObj->base.Image[stImage->base.Face][dstLevel];
  1495.       assert(dstImage);
  1496.       assert(dstImage->Width == stImage->base.Width);
  1497.       assert(dstImage->Height == stImage->base.Height);
  1498.       assert(dstImage->Depth == stImage->base.Depth);
  1499.    }
  1500.  
  1501.    if (stImage->pt) {
  1502.       /* Copy potentially with the blitter:
  1503.        */
  1504.       GLuint src_level;
  1505.       if (stImage->pt->last_level == 0)
  1506.          src_level = 0;
  1507.       else
  1508.          src_level = stImage->base.Level;
  1509.  
  1510.       assert(src_level <= stImage->pt->last_level);
  1511.       assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width);
  1512.       assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY ||
  1513.              u_minify(stImage->pt->height0, src_level) == stImage->base.Height);
  1514.       assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY ||
  1515.              stImage->pt->target == PIPE_TEXTURE_CUBE_ARRAY ||
  1516.              u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth);
  1517.  
  1518.       st_texture_image_copy(st->pipe,
  1519.                             stObj->pt, dstLevel,  /* dest texture, level */
  1520.                             stImage->pt, src_level, /* src texture, level */
  1521.                             stImage->base.Face);
  1522.  
  1523.       pipe_resource_reference(&stImage->pt, NULL);
  1524.    }
  1525.    pipe_resource_reference(&stImage->pt, stObj->pt);
  1526. }
  1527.  
  1528.  
  1529. /**
  1530.  * Called during state validation.  When this function is finished,
  1531.  * the texture object should be ready for rendering.
  1532.  * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
  1533.  */
  1534. GLboolean
  1535. st_finalize_texture(struct gl_context *ctx,
  1536.                     struct pipe_context *pipe,
  1537.                     struct gl_texture_object *tObj)
  1538. {
  1539.    struct st_context *st = st_context(ctx);
  1540.    struct st_texture_object *stObj = st_texture_object(tObj);
  1541.    const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
  1542.    GLuint face;
  1543.    const struct st_texture_image *firstImage;
  1544.    enum pipe_format firstImageFormat;
  1545.    GLuint ptWidth, ptHeight, ptDepth, ptLayers, ptNumSamples;
  1546.  
  1547.    if (tObj->Immutable)
  1548.       return GL_TRUE;
  1549.  
  1550.    if (_mesa_is_texture_complete(tObj, &tObj->Sampler)) {
  1551.       /* The texture is complete and we know exactly how many mipmap levels
  1552.        * are present/needed.  This is conditional because we may be called
  1553.        * from the st_generate_mipmap() function when the texture object is
  1554.        * incomplete.  In that case, we'll have set stObj->lastLevel before
  1555.        * we get here.
  1556.        */
  1557.       if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
  1558.           stObj->base.Sampler.MinFilter == GL_NEAREST)
  1559.          stObj->lastLevel = stObj->base.BaseLevel;
  1560.       else
  1561.          stObj->lastLevel = stObj->base._MaxLevel;
  1562.    }
  1563.  
  1564.    if (tObj->Target == GL_TEXTURE_BUFFER) {
  1565.       struct st_buffer_object *st_obj = st_buffer_object(tObj->BufferObject);
  1566.  
  1567.       if (!st_obj) {
  1568.          pipe_resource_reference(&stObj->pt, NULL);
  1569.          st_texture_release_all_sampler_views(st, stObj);
  1570.          return GL_TRUE;
  1571.       }
  1572.  
  1573.       if (st_obj->buffer != stObj->pt) {
  1574.          pipe_resource_reference(&stObj->pt, st_obj->buffer);
  1575.          st_texture_release_all_sampler_views(st, stObj);
  1576.          stObj->width0 = stObj->pt->width0 / _mesa_get_format_bytes(tObj->_BufferObjectFormat);
  1577.          stObj->height0 = 1;
  1578.          stObj->depth0 = 1;
  1579.       }
  1580.       return GL_TRUE;
  1581.  
  1582.    }
  1583.  
  1584.    firstImage = st_texture_image_const(_mesa_base_tex_image(&stObj->base));
  1585.    assert(firstImage);
  1586.  
  1587.    /* If both firstImage and stObj point to a texture which can contain
  1588.     * all active images, favour firstImage.  Note that because of the
  1589.     * completeness requirement, we know that the image dimensions
  1590.     * will match.
  1591.     */
  1592.    if (firstImage->pt &&
  1593.        firstImage->pt != stObj->pt &&
  1594.        (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
  1595.       pipe_resource_reference(&stObj->pt, firstImage->pt);
  1596.       st_texture_release_all_sampler_views(st, stObj);
  1597.    }
  1598.  
  1599.    /* If this texture comes from a window system, there is nothing else to do. */
  1600.    if (stObj->surface_based) {
  1601.       return GL_TRUE;
  1602.    }
  1603.  
  1604.    /* Find gallium format for the Mesa texture */
  1605.    firstImageFormat =
  1606.       st_mesa_format_to_pipe_format(st, firstImage->base.TexFormat);
  1607.  
  1608.    /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
  1609.    {
  1610.       GLuint width, height, depth;
  1611.       if (!guess_base_level_size(stObj->base.Target,
  1612.                                  firstImage->base.Width2,
  1613.                                  firstImage->base.Height2,
  1614.                                  firstImage->base.Depth2,
  1615.                                  firstImage->base.Level,
  1616.                                  &width, &height, &depth)) {
  1617.          width = stObj->width0;
  1618.          height = stObj->height0;
  1619.          depth = stObj->depth0;
  1620.       }
  1621.       /* convert GL dims to Gallium dims */
  1622.       st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
  1623.                                       &ptWidth, &ptHeight, &ptDepth, &ptLayers);
  1624.       ptNumSamples = firstImage->base.NumSamples;
  1625.    }
  1626.  
  1627.    /* If we already have a gallium texture, check that it matches the texture
  1628.     * object's format, target, size, num_levels, etc.
  1629.     */
  1630.    if (stObj->pt) {
  1631.       if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
  1632.           stObj->pt->format != firstImageFormat ||
  1633.           stObj->pt->last_level < stObj->lastLevel ||
  1634.           stObj->pt->width0 != ptWidth ||
  1635.           stObj->pt->height0 != ptHeight ||
  1636.           stObj->pt->depth0 != ptDepth ||
  1637.           stObj->pt->nr_samples != ptNumSamples ||
  1638.           stObj->pt->array_size != ptLayers)
  1639.       {
  1640.          /* The gallium texture does not match the Mesa texture so delete the
  1641.           * gallium texture now.  We'll make a new one below.
  1642.           */
  1643.          pipe_resource_reference(&stObj->pt, NULL);
  1644.          st_texture_release_all_sampler_views(st, stObj);
  1645.          st->dirty.st |= ST_NEW_FRAMEBUFFER;
  1646.       }
  1647.    }
  1648.  
  1649.    /* May need to create a new gallium texture:
  1650.     */
  1651.    if (!stObj->pt) {
  1652.       GLuint bindings = default_bindings(st, firstImageFormat);
  1653.  
  1654.       stObj->pt = st_texture_create(st,
  1655.                                     gl_target_to_pipe(stObj->base.Target),
  1656.                                     firstImageFormat,
  1657.                                     stObj->lastLevel,
  1658.                                     ptWidth,
  1659.                                     ptHeight,
  1660.                                     ptDepth,
  1661.                                     ptLayers, ptNumSamples,
  1662.                                     bindings);
  1663.  
  1664.       if (!stObj->pt) {
  1665.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
  1666.          return GL_FALSE;
  1667.       }
  1668.    }
  1669.  
  1670.    /* Pull in any images not in the object's texture:
  1671.     */
  1672.    for (face = 0; face < nr_faces; face++) {
  1673.       GLuint level;
  1674.       for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
  1675.          struct st_texture_image *stImage =
  1676.             st_texture_image(stObj->base.Image[face][level]);
  1677.  
  1678.          /* Need to import images in main memory or held in other textures.
  1679.           */
  1680.          if (stImage && stObj->pt != stImage->pt) {
  1681.             if (level == 0 ||
  1682.                 (stImage->base.Width == u_minify(stObj->width0, level) &&
  1683.                  stImage->base.Height == u_minify(stObj->height0, level) &&
  1684.                  stImage->base.Depth == u_minify(stObj->depth0, level))) {
  1685.                /* src image fits expected dest mipmap level size */
  1686.                copy_image_data_to_texture(st, stObj, level, stImage);
  1687.             }
  1688.          }
  1689.       }
  1690.    }
  1691.  
  1692.    return GL_TRUE;
  1693. }
  1694.  
  1695.  
  1696. /**
  1697.  * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
  1698.  * for a whole mipmap stack.
  1699.  */
  1700. static GLboolean
  1701. st_AllocTextureStorage(struct gl_context *ctx,
  1702.                        struct gl_texture_object *texObj,
  1703.                        GLsizei levels, GLsizei width,
  1704.                        GLsizei height, GLsizei depth)
  1705. {
  1706.    const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
  1707.    struct gl_texture_image *texImage = texObj->Image[0][0];
  1708.    struct st_context *st = st_context(ctx);
  1709.    struct st_texture_object *stObj = st_texture_object(texObj);
  1710.    struct pipe_screen *screen = st->pipe->screen;
  1711.    GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
  1712.    enum pipe_format fmt;
  1713.    GLint level;
  1714.    GLuint num_samples = texImage->NumSamples;
  1715.  
  1716.    assert(levels > 0);
  1717.  
  1718.    /* Save the level=0 dimensions */
  1719.    stObj->width0 = width;
  1720.    stObj->height0 = height;
  1721.    stObj->depth0 = depth;
  1722.    stObj->lastLevel = levels - 1;
  1723.  
  1724.    fmt = st_mesa_format_to_pipe_format(st, texImage->TexFormat);
  1725.  
  1726.    bindings = default_bindings(st, fmt);
  1727.  
  1728.    /* Raise the sample count if the requested one is unsupported. */
  1729.    if (num_samples > 1) {
  1730.       boolean found = FALSE;
  1731.  
  1732.       for (; num_samples <= ctx->Const.MaxSamples; num_samples++) {
  1733.          if (screen->is_format_supported(screen, fmt, PIPE_TEXTURE_2D,
  1734.                                          num_samples,
  1735.                                          PIPE_BIND_SAMPLER_VIEW)) {
  1736.             /* Update the sample count in gl_texture_image as well. */
  1737.             texImage->NumSamples = num_samples;
  1738.             found = TRUE;
  1739.             break;
  1740.          }
  1741.       }
  1742.  
  1743.       if (!found) {
  1744.          return GL_FALSE;
  1745.       }
  1746.    }
  1747.  
  1748.    st_gl_texture_dims_to_pipe_dims(texObj->Target,
  1749.                                    width, height, depth,
  1750.                                    &ptWidth, &ptHeight, &ptDepth, &ptLayers);
  1751.  
  1752.    stObj->pt = st_texture_create(st,
  1753.                                  gl_target_to_pipe(texObj->Target),
  1754.                                  fmt,
  1755.                                  levels - 1,
  1756.                                  ptWidth,
  1757.                                  ptHeight,
  1758.                                  ptDepth,
  1759.                                  ptLayers, num_samples,
  1760.                                  bindings);
  1761.    if (!stObj->pt)
  1762.       return GL_FALSE;
  1763.  
  1764.    /* Set image resource pointers */
  1765.    for (level = 0; level < levels; level++) {
  1766.       GLuint face;
  1767.       for (face = 0; face < numFaces; face++) {
  1768.          struct st_texture_image *stImage =
  1769.             st_texture_image(texObj->Image[face][level]);
  1770.          pipe_resource_reference(&stImage->pt, stObj->pt);
  1771.       }
  1772.    }
  1773.  
  1774.    return GL_TRUE;
  1775. }
  1776.  
  1777.  
  1778. static GLboolean
  1779. st_TestProxyTexImage(struct gl_context *ctx, GLenum target,
  1780.                      GLint level, mesa_format format,
  1781.                      GLint width, GLint height,
  1782.                      GLint depth, GLint border)
  1783. {
  1784.    struct st_context *st = st_context(ctx);
  1785.    struct pipe_context *pipe = st->pipe;
  1786.  
  1787.    if (width == 0 || height == 0 || depth == 0) {
  1788.       /* zero-sized images are legal, and always fit! */
  1789.       return GL_TRUE;
  1790.    }
  1791.  
  1792.    if (pipe->screen->can_create_resource) {
  1793.       /* Ask the gallium driver if the texture is too large */
  1794.       struct gl_texture_object *texObj =
  1795.          _mesa_get_current_tex_object(ctx, target);
  1796.       struct pipe_resource pt;
  1797.  
  1798.       /* Setup the pipe_resource object
  1799.        */
  1800.       memset(&pt, 0, sizeof(pt));
  1801.  
  1802.       pt.target = gl_target_to_pipe(target);
  1803.       pt.format = st_mesa_format_to_pipe_format(st, format);
  1804.  
  1805.       st_gl_texture_dims_to_pipe_dims(target,
  1806.                                       width, height, depth,
  1807.                                       &pt.width0, &pt.height0,
  1808.                                       &pt.depth0, &pt.array_size);
  1809.  
  1810.       if (level == 0 && (texObj->Sampler.MinFilter == GL_LINEAR ||
  1811.                          texObj->Sampler.MinFilter == GL_NEAREST)) {
  1812.          /* assume just one mipmap level */
  1813.          pt.last_level = 0;
  1814.       }
  1815.       else {
  1816.          /* assume a full set of mipmaps */
  1817.          pt.last_level = _mesa_logbase2(MAX3(width, height, depth));
  1818.       }
  1819.  
  1820.       return pipe->screen->can_create_resource(pipe->screen, &pt);
  1821.    }
  1822.    else {
  1823.       /* Use core Mesa fallback */
  1824.       return _mesa_test_proxy_teximage(ctx, target, level, format,
  1825.                                        width, height, depth, border);
  1826.    }
  1827. }
  1828.  
  1829. static GLboolean
  1830. st_TextureView(struct gl_context *ctx,
  1831.                struct gl_texture_object *texObj,
  1832.                struct gl_texture_object *origTexObj)
  1833. {
  1834.    struct st_texture_object *orig = st_texture_object(origTexObj);
  1835.    struct st_texture_object *tex = st_texture_object(texObj);
  1836.    struct gl_texture_image *image = texObj->Image[0][0];
  1837.  
  1838.    const int numFaces = _mesa_num_tex_faces(texObj->Target);
  1839.    const int numLevels = texObj->NumLevels;
  1840.  
  1841.    int face;
  1842.    int level;
  1843.  
  1844.    pipe_resource_reference(&tex->pt, orig->pt);
  1845.  
  1846.    /* Set image resource pointers */
  1847.    for (level = 0; level < numLevels; level++) {
  1848.       for (face = 0; face < numFaces; face++) {
  1849.          struct st_texture_image *stImage =
  1850.             st_texture_image(texObj->Image[face][level]);
  1851.          pipe_resource_reference(&stImage->pt, tex->pt);
  1852.       }
  1853.    }
  1854.  
  1855.    tex->surface_based = GL_TRUE;
  1856.    tex->surface_format =
  1857.       st_mesa_format_to_pipe_format(st_context(ctx), image->TexFormat);
  1858.  
  1859.    tex->width0 = image->Width;
  1860.    tex->height0 = image->Height;
  1861.    tex->depth0 = image->Depth;
  1862.    tex->lastLevel = numLevels - 1;
  1863.  
  1864.    return GL_TRUE;
  1865. }
  1866.  
  1867.  
  1868. void
  1869. st_init_texture_functions(struct dd_function_table *functions)
  1870. {
  1871.    functions->ChooseTextureFormat = st_ChooseTextureFormat;
  1872.    functions->QuerySamplesForFormat = st_QuerySamplesForFormat;
  1873.    functions->TexImage = st_TexImage;
  1874.    functions->TexSubImage = st_TexSubImage;
  1875.    functions->CompressedTexSubImage = _mesa_store_compressed_texsubimage;
  1876.    functions->CopyTexSubImage = st_CopyTexSubImage;
  1877.    functions->GenerateMipmap = st_generate_mipmap;
  1878.  
  1879.    functions->GetTexImage = st_GetTexImage;
  1880.  
  1881.    /* compressed texture functions */
  1882.    functions->CompressedTexImage = st_CompressedTexImage;
  1883.    functions->GetCompressedTexImage = _mesa_GetCompressedTexImage_sw;
  1884.  
  1885.    functions->NewTextureObject = st_NewTextureObject;
  1886.    functions->NewTextureImage = st_NewTextureImage;
  1887.    functions->DeleteTextureImage = st_DeleteTextureImage;
  1888.    functions->DeleteTexture = st_DeleteTextureObject;
  1889.    functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
  1890.    functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
  1891.    functions->MapTextureImage = st_MapTextureImage;
  1892.    functions->UnmapTextureImage = st_UnmapTextureImage;
  1893.  
  1894.    /* XXX Temporary until we can query pipe's texture sizes */
  1895.    functions->TestProxyTexImage = st_TestProxyTexImage;
  1896.  
  1897.    functions->AllocTextureStorage = st_AllocTextureStorage;
  1898.    functions->TextureView = st_TextureView;
  1899. }
  1900.