Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
  4.  * All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the
  8.  * "Software"), to deal in the Software without restriction, including
  9.  * without limitation the rights to use, copy, modify, merge, publish,
  10.  * distribute, sub license, and/or sell copies of the Software, and to
  11.  * permit persons to whom the Software is furnished to do so, subject to
  12.  * the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice (including the
  15.  * next paragraph) shall be included in all copies or substantial portions
  16.  * of the Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21.  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
  22.  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  *
  26.  **************************************************************************/
  27.  
  28. #include "main/mfeatures.h"
  29. #include "main/bufferobj.h"
  30. #include "main/enums.h"
  31. #include "main/fbobject.h"
  32. #include "main/formats.h"
  33. #include "main/image.h"
  34. #include "main/imports.h"
  35. #include "main/macros.h"
  36. #include "main/mipmap.h"
  37. #include "main/pack.h"
  38. #include "main/pixeltransfer.h"
  39. #include "main/texcompress.h"
  40. #include "main/texfetch.h"
  41. #include "main/texgetimage.h"
  42. #include "main/teximage.h"
  43. #include "main/texobj.h"
  44. #include "main/texstore.h"
  45.  
  46. #include "state_tracker/st_debug.h"
  47. #include "state_tracker/st_context.h"
  48. #include "state_tracker/st_cb_fbo.h"
  49. #include "state_tracker/st_cb_flush.h"
  50. #include "state_tracker/st_cb_texture.h"
  51. #include "state_tracker/st_format.h"
  52. #include "state_tracker/st_texture.h"
  53. #include "state_tracker/st_gen_mipmap.h"
  54. #include "state_tracker/st_atom.h"
  55.  
  56. #include "pipe/p_context.h"
  57. #include "pipe/p_defines.h"
  58. #include "util/u_inlines.h"
  59. #include "pipe/p_shader_tokens.h"
  60. #include "util/u_tile.h"
  61. #include "util/u_blit.h"
  62. #include "util/u_format.h"
  63. #include "util/u_surface.h"
  64. #include "util/u_sampler.h"
  65. #include "util/u_math.h"
  66. #include "util/u_box.h"
  67.  
  68. #define DBG if (0) printf
  69.  
  70.  
  71. static enum pipe_texture_target
  72. gl_target_to_pipe(GLenum target)
  73. {
  74.    switch (target) {
  75.    case GL_TEXTURE_1D:
  76.       return PIPE_TEXTURE_1D;
  77.  
  78.    case GL_TEXTURE_2D:
  79.       return PIPE_TEXTURE_2D;
  80.  
  81.    case GL_TEXTURE_RECTANGLE_NV:
  82.       return PIPE_TEXTURE_RECT;
  83.  
  84.    case GL_TEXTURE_3D:
  85.       return PIPE_TEXTURE_3D;
  86.  
  87.    case GL_TEXTURE_CUBE_MAP_ARB:
  88.       return PIPE_TEXTURE_CUBE;
  89.  
  90.    default:
  91.       assert(0);
  92.       return 0;
  93.    }
  94. }
  95.  
  96.  
  97. /** called via ctx->Driver.NewTextureImage() */
  98. static struct gl_texture_image *
  99. st_NewTextureImage(struct gl_context * ctx)
  100. {
  101.    DBG("%s\n", __FUNCTION__);
  102.    (void) ctx;
  103.    return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
  104. }
  105.  
  106.  
  107. /** called via ctx->Driver.NewTextureObject() */
  108. static struct gl_texture_object *
  109. st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
  110. {
  111.    struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
  112.  
  113.    DBG("%s\n", __FUNCTION__);
  114.    _mesa_initialize_texture_object(&obj->base, name, target);
  115.  
  116.    return &obj->base;
  117. }
  118.  
  119. /** called via ctx->Driver.DeleteTextureObject() */
  120. static void
  121. st_DeleteTextureObject(struct gl_context *ctx,
  122.                        struct gl_texture_object *texObj)
  123. {
  124.    struct st_context *st = st_context(ctx);
  125.    struct st_texture_object *stObj = st_texture_object(texObj);
  126.    if (stObj->pt)
  127.       pipe_resource_reference(&stObj->pt, NULL);
  128.    if (stObj->sampler_view) {
  129.       if (stObj->sampler_view->context != st->pipe) {
  130.          /* Take "ownership" of this texture sampler view by setting
  131.           * its context pointer to this context.  This avoids potential
  132.           * crashes when the texture object is shared among contexts
  133.           * and the original/owner context has already been destroyed.
  134.           */
  135.          stObj->sampler_view->context = st->pipe;
  136.       }
  137.       pipe_sampler_view_reference(&stObj->sampler_view, NULL);
  138.    }
  139.    _mesa_delete_texture_object(ctx, texObj);
  140. }
  141.  
  142.  
  143. /** called via ctx->Driver.FreeTexImageData() */
  144. static void
  145. st_FreeTextureImageData(struct gl_context * ctx, struct gl_texture_image *texImage)
  146. {
  147.    struct st_texture_image *stImage = st_texture_image(texImage);
  148.  
  149.    DBG("%s\n", __FUNCTION__);
  150.  
  151.    if (stImage->pt) {
  152.       pipe_resource_reference(&stImage->pt, NULL);
  153.    }
  154.  
  155.    if (texImage->Data) {
  156.       _mesa_align_free(texImage->Data);
  157.       texImage->Data = NULL;
  158.    }
  159. }
  160.  
  161.  
  162. /**
  163.  * From linux kernel i386 header files, copes with odd sizes better
  164.  * than COPY_DWORDS would:
  165.  * XXX Put this in src/mesa/main/imports.h ???
  166.  */
  167. #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
  168. static INLINE void *
  169. __memcpy(void *to, const void *from, size_t n)
  170. {
  171.    int d0, d1, d2;
  172.    __asm__ __volatile__("rep ; movsl\n\t"
  173.                         "testb $2,%b4\n\t"
  174.                         "je 1f\n\t"
  175.                         "movsw\n"
  176.                         "1:\ttestb $1,%b4\n\t"
  177.                         "je 2f\n\t"
  178.                         "movsb\n" "2:":"=&c"(d0), "=&D"(d1), "=&S"(d2)
  179.                         :"0"(n / 4), "q"(n), "1"((long) to), "2"((long) from)
  180.                         :"memory");
  181.    return (to);
  182. }
  183. #else
  184. #define __memcpy(a,b,c) memcpy(a,b,c)
  185. #endif
  186.  
  187.  
  188. /**
  189.  * The system memcpy (at least on ubuntu 5.10) has problems copying
  190.  * to agp (writecombined) memory from a source which isn't 64-byte
  191.  * aligned - there is a 4x performance falloff.
  192.  *
  193.  * The x86 __memcpy is immune to this but is slightly slower
  194.  * (10%-ish) than the system memcpy.
  195.  *
  196.  * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
  197.  * isn't much faster than x86_memcpy for agp copies.
  198.  *
  199.  * TODO: switch dynamically.
  200.  */
  201. static void *
  202. do_memcpy(void *dest, const void *src, size_t n)
  203. {
  204.    if ((((unsigned long) src) & 63) || (((unsigned long) dest) & 63)) {
  205.       return __memcpy(dest, src, n);
  206.    }
  207.    else
  208.       return memcpy(dest, src, n);
  209. }
  210.  
  211.  
  212. /**
  213.  * Return default texture resource binding bitmask for the given format.
  214.  */
  215. static GLuint
  216. default_bindings(struct st_context *st, enum pipe_format format)
  217. {
  218.    struct pipe_screen *screen = st->pipe->screen;
  219.    const unsigned target = PIPE_TEXTURE_2D;
  220.    const unsigned geom = 0x0;
  221.    unsigned bindings;
  222.  
  223.    if (util_format_is_depth_or_stencil(format))
  224.       bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
  225.    else
  226.       bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
  227.  
  228.    if (screen->is_format_supported(screen, format, target, 0, bindings, geom))
  229.       return bindings;
  230.    else
  231.       return PIPE_BIND_SAMPLER_VIEW;
  232. }
  233.  
  234.  
  235. /** Return number of image dimensions (1, 2 or 3) for a texture target. */
  236. static GLuint
  237. get_texture_dims(GLenum target)
  238. {
  239.    switch (target) {
  240.    case GL_TEXTURE_1D:
  241.    case GL_TEXTURE_1D_ARRAY_EXT:
  242.       return 1;
  243.    case GL_TEXTURE_2D:
  244.    case GL_TEXTURE_CUBE_MAP_ARB:
  245.    case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
  246.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
  247.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
  248.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
  249.    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
  250.    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
  251.    case GL_TEXTURE_RECTANGLE_NV:
  252.    case GL_TEXTURE_2D_ARRAY_EXT:
  253.       return 2;
  254.    case GL_TEXTURE_3D:
  255.       return 3;
  256.    default:
  257.       assert(0 && "invalid texture target in get_texture_dims()");
  258.       return 1;
  259.    }
  260. }
  261.  
  262.  
  263. /**
  264.  * Try to allocate a pipe_resource object for the given st_texture_object.
  265.  *
  266.  * We use the given st_texture_image as a clue to determine the size of the
  267.  * mipmap image at level=0.
  268.  *
  269.  * \return GL_TRUE for success, GL_FALSE if out of memory.
  270.  */
  271. static GLboolean
  272. guess_and_alloc_texture(struct st_context *st,
  273.                         struct st_texture_object *stObj,
  274.                         const struct st_texture_image *stImage)
  275. {
  276.    const GLuint dims = get_texture_dims(stObj->base.Target);
  277.    GLuint level, lastLevel, width, height, depth;
  278.    GLuint bindings;
  279.    enum pipe_format fmt;
  280.  
  281.    DBG("%s\n", __FUNCTION__);
  282.  
  283.    assert(!stObj->pt);
  284.  
  285.    level = stImage->level;
  286.    width = stImage->base.Width2;  /* size w/out border */
  287.    height = stImage->base.Height2;
  288.    depth = stImage->base.Depth2;
  289.  
  290.    assert(width > 0);
  291.    assert(height > 0);
  292.    assert(depth > 0);
  293.  
  294.    /* Depending on the image's size, we can't always make a guess here.
  295.     */
  296.    if (level > 0) {
  297.       if ( (dims >= 1 && width == 1) ||
  298.            (dims >= 2 && height == 1) ||
  299.            (dims >= 3 && depth == 1) ) {
  300.          /* we can't determine the image size at level=0 */
  301.          stObj->width0 = stObj->height0 = stObj->depth0 = 0;
  302.          /* this is not an out of memory error */
  303.          return GL_TRUE;
  304.       }
  305.    }
  306.  
  307.    /* grow the image size until we hit level = 0 */
  308.    while (level > 0) {
  309.       if (width != 1)
  310.          width <<= 1;
  311.       if (height != 1)
  312.          height <<= 1;
  313.       if (depth != 1)
  314.          depth <<= 1;
  315.       level--;
  316.    }      
  317.  
  318.    assert(level == 0);
  319.  
  320.    /* At this point, (width x height x depth) is the expected size of
  321.     * the level=0 mipmap image.
  322.     */
  323.  
  324.    /* Guess a reasonable value for lastLevel.  With OpenGL we have no
  325.     * idea how many mipmap levels will be in a texture until we start
  326.     * to render with it.  Make an educated guess here but be prepared
  327.     * to re-allocating a texture buffer with space for more (or fewer)
  328.     * mipmap levels later.
  329.     */
  330.    if ((stObj->base.MinFilter == GL_NEAREST ||
  331.         stObj->base.MinFilter == GL_LINEAR ||
  332.         stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
  333.         stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) &&
  334.        !stObj->base.GenerateMipmap &&
  335.        stImage->level == 0) {
  336.       /* only alloc space for a single mipmap level */
  337.       lastLevel = 0;
  338.    }
  339.    else {
  340.       /* alloc space for a full mipmap */
  341.       GLuint l2width = util_logbase2(width);
  342.       GLuint l2height = util_logbase2(height);
  343.       GLuint l2depth = util_logbase2(depth);
  344.       lastLevel = MAX2(MAX2(l2width, l2height), l2depth);
  345.    }
  346.  
  347.    /* Save the level=0 dimensions */
  348.    stObj->width0 = width;
  349.    stObj->height0 = height;
  350.    stObj->depth0 = depth;
  351.  
  352.    fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat);
  353.  
  354.    bindings = default_bindings(st, fmt);
  355.  
  356.    stObj->pt = st_texture_create(st,
  357.                                  gl_target_to_pipe(stObj->base.Target),
  358.                                  fmt,
  359.                                  lastLevel,
  360.                                  width,
  361.                                  height,
  362.                                  depth,
  363.                                  bindings);
  364.  
  365.    DBG("%s returning %d\n", __FUNCTION__, (stObj->pt != NULL));
  366.  
  367.    return stObj->pt != NULL;
  368. }
  369.  
  370.  
  371. /**
  372.  * Adjust pixel unpack params and image dimensions to strip off the
  373.  * texture border.
  374.  * Gallium doesn't support texture borders.  They've seldem been used
  375.  * and seldom been implemented correctly anyway.
  376.  * \param unpackNew  returns the new pixel unpack parameters
  377.  */
  378. static void
  379. strip_texture_border(GLint border,
  380.                      GLint *width, GLint *height, GLint *depth,
  381.                      const struct gl_pixelstore_attrib *unpack,
  382.                      struct gl_pixelstore_attrib *unpackNew)
  383. {
  384.    assert(border > 0);  /* sanity check */
  385.  
  386.    *unpackNew = *unpack;
  387.  
  388.    if (unpackNew->RowLength == 0)
  389.       unpackNew->RowLength = *width;
  390.  
  391.    if (depth && unpackNew->ImageHeight == 0)
  392.       unpackNew->ImageHeight = *height;
  393.  
  394.    unpackNew->SkipPixels += border;
  395.    if (height)
  396.       unpackNew->SkipRows += border;
  397.    if (depth)
  398.       unpackNew->SkipImages += border;
  399.  
  400.    assert(*width >= 3);
  401.    *width = *width - 2 * border;
  402.    if (height && *height >= 3)
  403.       *height = *height - 2 * border;
  404.    if (depth && *depth >= 3)
  405.       *depth = *depth - 2 * border;
  406. }
  407.  
  408.  
  409. /**
  410.  * Try to do texture compression via rendering.  If the Gallium driver
  411.  * can render into a compressed surface this will allow us to do texture
  412.  * compression.
  413.  * \return GL_TRUE for success, GL_FALSE for failure
  414.  */
  415. static GLboolean
  416. compress_with_blit(struct gl_context * ctx,
  417.                    GLenum target, GLint level,
  418.                    GLint xoffset, GLint yoffset, GLint zoffset,
  419.                    GLint width, GLint height, GLint depth,
  420.                    GLenum format, GLenum type, const void *pixels,
  421.                    const struct gl_pixelstore_attrib *unpack,
  422.                    struct gl_texture_image *texImage)
  423. {
  424.    const GLuint dstImageOffsets[1] = {0};
  425.    struct st_texture_image *stImage = st_texture_image(texImage);
  426.    struct st_context *st = st_context(ctx);
  427.    struct pipe_context *pipe = st->pipe;
  428.    struct pipe_screen *screen = pipe->screen;
  429.    gl_format mesa_format;
  430.    struct pipe_resource templ;
  431.    struct pipe_resource *src_tex;
  432.    struct pipe_sampler_view view_templ;
  433.    struct pipe_sampler_view *src_view;
  434.    struct pipe_surface *dst_surface, surf_tmpl;
  435.    struct pipe_transfer *tex_xfer;
  436.    void *map;
  437.  
  438.    if (!stImage->pt) {
  439.       /* XXX: Can this happen? Should we assert? */
  440.       return GL_FALSE;
  441.    }
  442.  
  443.    /* get destination surface (in the compressed texture) */
  444.    memset(&surf_tmpl, 0, sizeof(surf_tmpl));
  445.    surf_tmpl.format = stImage->pt->format;
  446.    surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
  447.    surf_tmpl.u.tex.level = stImage->level;
  448.    surf_tmpl.u.tex.first_layer = stImage->face;
  449.    surf_tmpl.u.tex.last_layer = stImage->face;
  450.    dst_surface = pipe->create_surface(pipe, stImage->pt, &surf_tmpl);
  451.    if (!dst_surface) {
  452.       /* can't render into this format (or other problem) */
  453.       return GL_FALSE;
  454.    }
  455.  
  456.    /* Choose format for the temporary RGBA texture image.
  457.     */
  458.    mesa_format = st_ChooseTextureFormat(ctx, GL_RGBA, format, type);
  459.    assert(mesa_format);
  460.    if (!mesa_format)
  461.       return GL_FALSE;
  462.  
  463.    /* Create the temporary source texture
  464.     */
  465.    memset(&templ, 0, sizeof(templ));
  466.    templ.target = st->internal_target;
  467.    templ.format = st_mesa_format_to_pipe_format(mesa_format);
  468.    templ.width0 = width;
  469.    templ.height0 = height;
  470.    templ.depth0 = 1;
  471.    templ.array_size = 1;
  472.    templ.last_level = 0;
  473.    templ.usage = PIPE_USAGE_DEFAULT;
  474.    templ.bind = PIPE_BIND_SAMPLER_VIEW;
  475.    src_tex = screen->resource_create(screen, &templ);
  476.  
  477.    if (!src_tex)
  478.       return GL_FALSE;
  479.  
  480.    /* Put user's tex data into the temporary texture
  481.     */
  482.    tex_xfer = pipe_get_transfer(st_context(ctx)->pipe, src_tex,
  483.                                 0, 0, /* layer, level are zero */
  484.                                 PIPE_TRANSFER_WRITE,
  485.                                 0, 0, width, height); /* x, y, w, h */
  486.    map = pipe_transfer_map(pipe, tex_xfer);
  487.  
  488.    _mesa_texstore(ctx, 2, GL_RGBA, mesa_format,
  489.                   map,              /* dest ptr */
  490.                   0, 0, 0,          /* dest x/y/z offset */
  491.                   tex_xfer->stride, /* dest row stride (bytes) */
  492.                   dstImageOffsets,  /* image offsets (for 3D only) */
  493.                   width, height, 1, /* size */
  494.                   format, type,     /* source format/type */
  495.                   pixels,           /* source data */
  496.                   unpack);          /* source data packing */
  497.  
  498.    pipe_transfer_unmap(pipe, tex_xfer);
  499.    pipe->transfer_destroy(pipe, tex_xfer);
  500.  
  501.    /* Create temporary sampler view */
  502.    u_sampler_view_default_template(&view_templ,
  503.                                    src_tex,
  504.                                    src_tex->format);
  505.    src_view = pipe->create_sampler_view(pipe, src_tex, &view_templ);
  506.  
  507.  
  508.    /* copy / compress image */
  509.    util_blit_pixels_tex(st->blit,
  510.                         src_view,         /* sampler view (src) */
  511.                         0, 0,             /* src x0, y0 */
  512.                         width, height,    /* src x1, y1 */
  513.                         dst_surface,      /* pipe_surface (dst) */
  514.                         xoffset, yoffset, /* dst x0, y0 */
  515.                         xoffset + width,  /* dst x1 */
  516.                         yoffset + height, /* dst y1 */
  517.                         0.0,              /* z */
  518.                         PIPE_TEX_MIPFILTER_NEAREST);
  519.  
  520.    pipe_surface_reference(&dst_surface, NULL);
  521.    pipe_resource_reference(&src_tex, NULL);
  522.    pipe_sampler_view_reference(&src_view, NULL);
  523.  
  524.    return GL_TRUE;
  525. }
  526.  
  527.  
  528. /**
  529.  * Do glTexImage1/2/3D().
  530.  */
  531. static void
  532. st_TexImage(struct gl_context * ctx,
  533.             GLint dims,
  534.             GLenum target, GLint level,
  535.             GLint internalFormat,
  536.             GLint width, GLint height, GLint depth,
  537.             GLint border,
  538.             GLenum format, GLenum type, const void *pixels,
  539.             const struct gl_pixelstore_attrib *unpack,
  540.             struct gl_texture_object *texObj,
  541.             struct gl_texture_image *texImage,
  542.             GLsizei imageSize, GLboolean compressed_src)
  543. {
  544.    struct st_context *st = st_context(ctx);
  545.    struct pipe_screen *screen = st->pipe->screen;
  546.    struct st_texture_object *stObj = st_texture_object(texObj);
  547.    struct st_texture_image *stImage = st_texture_image(texImage);
  548.    GLuint dstRowStride = 0;
  549.    struct gl_pixelstore_attrib unpackNB;
  550.    enum pipe_transfer_usage transfer_usage = 0;
  551.  
  552.    DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
  553.        _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
  554.  
  555.    /* switch to "normal" */
  556.    if (stObj->surface_based) {
  557.       gl_format texFormat;
  558.  
  559.       _mesa_clear_texture_object(ctx, texObj);
  560.       pipe_resource_reference(&stObj->pt, NULL);
  561.  
  562.       /* oops, need to init this image again */
  563.       texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
  564.                                               internalFormat, format, type);
  565.  
  566.       _mesa_init_teximage_fields(ctx, target, texImage,
  567.                                  width, height, depth, border,
  568.                                  internalFormat, texFormat);
  569.  
  570.       stObj->surface_based = GL_FALSE;
  571.    }
  572.  
  573.    /* gallium does not support texture borders, strip it off */
  574.    if (border) {
  575.       strip_texture_border(border, &width, &height, &depth, unpack, &unpackNB);
  576.       unpack = &unpackNB;
  577.       texImage->Width = width;
  578.       texImage->Height = height;
  579.       texImage->Depth = depth;
  580.       texImage->Border = 0;
  581.       border = 0;
  582.    }
  583.    else {
  584.       assert(texImage->Width == width);
  585.       assert(texImage->Height == height);
  586.       assert(texImage->Depth == depth);
  587.    }
  588.  
  589.    stImage->face = _mesa_tex_target_to_face(target);
  590.    stImage->level = level;
  591.  
  592.    _mesa_set_fetch_functions(texImage, dims);
  593.  
  594.    /* Release the reference to a potentially orphaned buffer.  
  595.     * Release any old malloced memory.
  596.     */
  597.    if (stImage->pt) {
  598.       pipe_resource_reference(&stImage->pt, NULL);
  599.       assert(!texImage->Data);
  600.    }
  601.    else if (texImage->Data) {
  602.       _mesa_align_free(texImage->Data);
  603.    }
  604.  
  605.    /*
  606.     * See if the new image is somehow incompatible with the existing
  607.     * mipmap.  If so, free the old mipmap.
  608.     */
  609.    if (stObj->pt) {
  610.       if (level > (GLint) stObj->pt->last_level ||
  611.           !st_texture_match_image(stObj->pt, &stImage->base,
  612.                                   stImage->face, stImage->level)) {
  613.          DBG("release it\n");
  614.          pipe_resource_reference(&stObj->pt, NULL);
  615.          assert(!stObj->pt);
  616.          pipe_sampler_view_reference(&stObj->sampler_view, NULL);
  617.       }
  618.    }
  619.  
  620.    if (width == 0 || height == 0 || depth == 0) {
  621.       /* stop after freeing old image */
  622.       return;
  623.    }
  624.  
  625.    if (!stObj->pt) {
  626.       if (!guess_and_alloc_texture(st, stObj, stImage)) {
  627.          /* Probably out of memory.
  628.           * Try flushing any pending rendering, then retry.
  629.           */
  630.          st_finish(st);
  631.          if (!guess_and_alloc_texture(st, stObj, stImage)) {
  632.             _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
  633.             return;
  634.          }
  635.       }
  636.    }
  637.  
  638.    assert(!stImage->pt);
  639.  
  640.    /* Check if this texture image can live inside the texture object's buffer.
  641.     * If so, store the image there.  Otherwise the image will temporarily live
  642.     * in its own buffer.
  643.     */
  644.    if (stObj->pt &&
  645.        st_texture_match_image(stObj->pt, &stImage->base,
  646.                               stImage->face, stImage->level)) {
  647.  
  648.       pipe_resource_reference(&stImage->pt, stObj->pt);
  649.       assert(stImage->pt);
  650.    }
  651.  
  652.    if (!stImage->pt)
  653.       DBG("XXX: Image did not fit into texture - storing in local memory!\n");
  654.  
  655.    /* Pixel data may come from regular user memory or a PBO.  For the later,
  656.     * do bounds checking and map the PBO to read pixels data from it.
  657.     *
  658.     * XXX we should try to use a GPU-accelerated path to copy the image data
  659.     * from the PBO to the texture.
  660.     */
  661.    if (compressed_src) {
  662.       pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
  663.                                                       unpack,
  664.                                                       "glCompressedTexImage");
  665.    }
  666.    else {
  667.       pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
  668.                                            format, type,
  669.                                            pixels, unpack, "glTexImage");
  670.    }
  671.  
  672.    /* See if we can do texture compression with a blit/render.
  673.     */
  674.    if (!compressed_src &&
  675.        !ctx->Mesa_DXTn &&
  676.        _mesa_is_format_compressed(texImage->TexFormat) &&
  677.        screen->is_format_supported(screen,
  678.                                    stImage->pt->format,
  679.                                    stImage->pt->target, 0,
  680.                                    PIPE_BIND_RENDER_TARGET, 0)) {
  681.       if (!pixels)
  682.          goto done;
  683.  
  684.       if (compress_with_blit(ctx, target, level, 0, 0, 0, width, height, depth,
  685.                              format, type, pixels, unpack, texImage)) {
  686.          goto done;
  687.       }
  688.    }
  689.  
  690.    /*
  691.     * Prepare to store the texture data.  Either map the gallium texture buffer
  692.     * memory or malloc space for it.
  693.     */
  694.    if (stImage->pt) {
  695.       /* Store the image in the gallium texture memory buffer */
  696.       if (format == GL_DEPTH_COMPONENT &&
  697.           util_format_is_depth_and_stencil(stImage->pt->format))
  698.          transfer_usage = PIPE_TRANSFER_READ_WRITE;
  699.       else
  700.          transfer_usage = PIPE_TRANSFER_WRITE;
  701.  
  702.       texImage->Data = st_texture_image_map(st, stImage, 0,
  703.                                             transfer_usage, 0, 0, width, height);
  704.       if(stImage->transfer)
  705.          dstRowStride = stImage->transfer->stride;
  706.    }
  707.    else {
  708.       /* Allocate regular memory and store the image there temporarily.   */
  709.       GLuint imageSize = _mesa_format_image_size(texImage->TexFormat,
  710.                                                  width, height, depth);
  711.       dstRowStride = _mesa_format_row_stride(texImage->TexFormat, width);
  712.  
  713.       texImage->Data = _mesa_align_malloc(imageSize, 16);
  714.    }
  715.  
  716.    if (!texImage->Data) {
  717.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
  718.       return;
  719.    }
  720.  
  721.    if (!pixels) {
  722.       /* We've allocated texture memory, but have no pixel data - all done. */
  723.       goto done;
  724.    }
  725.  
  726.    DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
  727.        width, height, depth, width, dstRowStride);
  728.  
  729.    /* Copy user texture image into the texture buffer.
  730.     */
  731.    if (compressed_src) {
  732.       const GLuint srcRowStride =
  733.          _mesa_format_row_stride(texImage->TexFormat, width);
  734.       if (dstRowStride == srcRowStride) {
  735.          memcpy(texImage->Data, pixels, imageSize);
  736.       }
  737.       else {
  738.          char *dst = texImage->Data;
  739.          const char *src = pixels;
  740.          GLuint i, bw, bh, lines;
  741.          _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
  742.          lines = (height + bh - 1) / bh;
  743.  
  744.          for (i = 0; i < lines; ++i) {
  745.             memcpy(dst, src, srcRowStride);
  746.             dst += dstRowStride;
  747.             src += srcRowStride;
  748.          }
  749.       }
  750.    }
  751.    else {
  752.       const GLuint srcImageStride =
  753.          _mesa_image_image_stride(unpack, width, height, format, type);
  754.       GLint i;
  755.       const GLubyte *src = (const GLubyte *) pixels;
  756.  
  757.       for (i = 0; i < depth; i++) {
  758.          if (!_mesa_texstore(ctx, dims,
  759.                              texImage->_BaseFormat,
  760.                              texImage->TexFormat,
  761.                              texImage->Data,
  762.                              0, 0, 0, /* dstX/Y/Zoffset */
  763.                              dstRowStride,
  764.                              texImage->ImageOffsets,
  765.                              width, height, 1,
  766.                              format, type, src, unpack)) {
  767.             _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
  768.          }
  769.  
  770.          if (stImage->pt && i + 1 < depth) {
  771.             /* unmap this slice */
  772.             st_texture_image_unmap(st, stImage);
  773.             /* map next slice of 3D texture */
  774.             texImage->Data = st_texture_image_map(st, stImage, i + 1,
  775.                                                   transfer_usage, 0, 0,
  776.                                                   width, height);
  777.             src += srcImageStride;
  778.          }
  779.       }
  780.    }
  781.  
  782. done:
  783.    _mesa_unmap_teximage_pbo(ctx, unpack);
  784.  
  785.    if (stImage->pt && texImage->Data) {
  786.       st_texture_image_unmap(st, stImage);
  787.       texImage->Data = NULL;
  788.    }
  789. }
  790.  
  791.  
  792. static void
  793. st_TexImage3D(struct gl_context * ctx,
  794.               GLenum target, GLint level,
  795.               GLint internalFormat,
  796.               GLint width, GLint height, GLint depth,
  797.               GLint border,
  798.               GLenum format, GLenum type, const void *pixels,
  799.               const struct gl_pixelstore_attrib *unpack,
  800.               struct gl_texture_object *texObj,
  801.               struct gl_texture_image *texImage)
  802. {
  803.    st_TexImage(ctx, 3, target, level, internalFormat, width, height, depth,
  804.                border, format, type, pixels, unpack, texObj, texImage,
  805.                0, GL_FALSE);
  806. }
  807.  
  808.  
  809. static void
  810. st_TexImage2D(struct gl_context * ctx,
  811.               GLenum target, GLint level,
  812.               GLint internalFormat,
  813.               GLint width, GLint height, GLint border,
  814.               GLenum format, GLenum type, const void *pixels,
  815.               const struct gl_pixelstore_attrib *unpack,
  816.               struct gl_texture_object *texObj,
  817.               struct gl_texture_image *texImage)
  818. {
  819.    st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border,
  820.                format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
  821. }
  822.  
  823.  
  824. static void
  825. st_TexImage1D(struct gl_context * ctx,
  826.               GLenum target, GLint level,
  827.               GLint internalFormat,
  828.               GLint width, GLint border,
  829.               GLenum format, GLenum type, const void *pixels,
  830.               const struct gl_pixelstore_attrib *unpack,
  831.               struct gl_texture_object *texObj,
  832.               struct gl_texture_image *texImage)
  833. {
  834.    st_TexImage(ctx, 1, target, level, internalFormat, width, 1, 1, border,
  835.                format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
  836. }
  837.  
  838.  
  839. static void
  840. st_CompressedTexImage2D(struct gl_context *ctx, GLenum target, GLint level,
  841.                         GLint internalFormat,
  842.                         GLint width, GLint height, GLint border,
  843.                         GLsizei imageSize, const GLvoid *data,
  844.                         struct gl_texture_object *texObj,
  845.                         struct gl_texture_image *texImage)
  846. {
  847.    st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border,
  848.                0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE);
  849. }
  850.  
  851.  
  852.  
  853. /**
  854.  * glGetTexImage() helper: decompress a compressed texture by rendering
  855.  * a textured quad.  Store the results in the user's buffer.
  856.  */
  857. static void
  858. decompress_with_blit(struct gl_context * ctx, GLenum target, GLint level,
  859.                      GLenum format, GLenum type, GLvoid *pixels,
  860.                      struct gl_texture_object *texObj,
  861.                      struct gl_texture_image *texImage)
  862. {
  863.    struct st_context *st = st_context(ctx);
  864.    struct pipe_context *pipe = st->pipe;
  865.    struct st_texture_image *stImage = st_texture_image(texImage);
  866.    struct st_texture_object *stObj = st_texture_object(texObj);
  867.    struct pipe_sampler_view *src_view =
  868.       st_get_texture_sampler_view(stObj, pipe);
  869.    const GLuint width = texImage->Width;
  870.    const GLuint height = texImage->Height;
  871.    struct pipe_surface *dst_surface;
  872.    struct pipe_resource *dst_texture;
  873.    struct pipe_transfer *tex_xfer;
  874.    unsigned bind = (PIPE_BIND_RENDER_TARGET | /* util_blit may choose to render */
  875.                     PIPE_BIND_TRANSFER_READ);
  876.  
  877.    /* create temp / dest surface */
  878.    if (!util_create_rgba_surface(pipe, width, height, bind,
  879.                                  &dst_texture, &dst_surface)) {
  880.       _mesa_problem(ctx, "util_create_rgba_surface() failed "
  881.                     "in decompress_with_blit()");
  882.       return;
  883.    }
  884.  
  885.    /* blit/render/decompress */
  886.    util_blit_pixels_tex(st->blit,
  887.                         src_view,      /* pipe_resource (src) */
  888.                         0, 0,             /* src x0, y0 */
  889.                         width, height,    /* src x1, y1 */
  890.                         dst_surface,      /* pipe_surface (dst) */
  891.                         0, 0,             /* dst x0, y0 */
  892.                         width, height,    /* dst x1, y1 */
  893.                         0.0,              /* z */
  894.                         PIPE_TEX_MIPFILTER_NEAREST);
  895.  
  896.    /* map the dst_surface so we can read from it */
  897.    tex_xfer = pipe_get_transfer(st_context(ctx)->pipe,
  898.                                 dst_texture, 0, 0,
  899.                                 PIPE_TRANSFER_READ,
  900.                                 0, 0, width, height);
  901.  
  902.    pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
  903.  
  904.    /* copy/pack data into user buffer */
  905.    if (st_equal_formats(stImage->pt->format, format, type)) {
  906.       /* memcpy */
  907.       const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format);
  908.       ubyte *map = pipe_transfer_map(pipe, tex_xfer);
  909.       GLuint row;
  910.       for (row = 0; row < height; row++) {
  911.          GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
  912.                                               height, format, type, row, 0);
  913.          memcpy(dest, map, bytesPerRow);
  914.          map += tex_xfer->stride;
  915.       }
  916.       pipe_transfer_unmap(pipe, tex_xfer);
  917.    }
  918.    else {
  919.       /* format translation via floats */
  920.       GLuint row;
  921.       for (row = 0; row < height; row++) {
  922.          const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
  923.          GLfloat rgba[4 * MAX_WIDTH];
  924.          GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
  925.                                               height, format, type, row, 0);
  926.  
  927.          if (ST_DEBUG & DEBUG_FALLBACK)
  928.             debug_printf("%s: fallback format translation\n", __FUNCTION__);
  929.  
  930.          /* get float[4] rgba row from surface */
  931.          pipe_get_tile_rgba(pipe, tex_xfer, 0, row, width, 1, rgba);
  932.  
  933.          _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
  934.                                     type, dest, &ctx->Pack, transferOps);
  935.       }
  936.    }
  937.  
  938.    _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
  939.  
  940.    pipe->transfer_destroy(pipe, tex_xfer);
  941.  
  942.    /* destroy the temp / dest surface */
  943.    util_destroy_rgba_surface(dst_texture, dst_surface);
  944. }
  945.  
  946.  
  947.  
  948. /**
  949.  * Need to map texture image into memory before copying image data,
  950.  * then unmap it.
  951.  */
  952. static void
  953. st_get_tex_image(struct gl_context * ctx, GLenum target, GLint level,
  954.                  GLenum format, GLenum type, GLvoid * pixels,
  955.                  struct gl_texture_object *texObj,
  956.                  struct gl_texture_image *texImage, GLboolean compressed_dst)
  957. {
  958.    struct st_context *st = st_context(ctx);
  959.    struct st_texture_image *stImage = st_texture_image(texImage);
  960.    const GLuint dstImageStride =
  961.       _mesa_image_image_stride(&ctx->Pack, texImage->Width, texImage->Height,
  962.                                format, type);
  963.    GLuint depth, i;
  964.    GLubyte *dest;
  965.  
  966.    if (stImage->pt &&
  967.        util_format_is_s3tc(stImage->pt->format) &&
  968.        !compressed_dst) {
  969.       /* Need to decompress the texture.
  970.        * We'll do this by rendering a textured quad.
  971.        * Note that we only expect RGBA formats (no Z/depth formats).
  972.        */
  973.       decompress_with_blit(ctx, target, level, format, type, pixels,
  974.                            texObj, texImage);
  975.       return;
  976.    }
  977.  
  978.    /* Map */
  979.    if (stImage->pt) {
  980.       /* Image is stored in hardware format in a buffer managed by the
  981.        * kernel.  Need to explicitly map and unmap it.
  982.        */
  983.       texImage->Data = st_texture_image_map(st, stImage, 0,
  984.                                             PIPE_TRANSFER_READ, 0, 0,
  985.                                             stImage->base.Width,
  986.                                             stImage->base.Height);
  987.       /* compute stride in texels from stride in bytes */
  988.       texImage->RowStride = stImage->transfer->stride
  989.          * util_format_get_blockwidth(stImage->pt->format)
  990.          / util_format_get_blocksize(stImage->pt->format);
  991.    }
  992.    else {
  993.       /* Otherwise, the image should actually be stored in
  994.        * texImage->Data.  This is pretty confusing for
  995.        * everybody, I'd much prefer to separate the two functions of
  996.        * texImage->Data - storage for texture images in main memory
  997.        * and access (ie mappings) of images.  In other words, we'd
  998.        * create a new texImage->Map field and leave Data simply for
  999.        * storage.
  1000.        */
  1001.       assert(texImage->Data);
  1002.    }
  1003.  
  1004.    depth = texImage->Depth;
  1005.    texImage->Depth = 1;
  1006.  
  1007.    dest = (GLubyte *) pixels;
  1008.  
  1009.    _mesa_set_fetch_functions(texImage, get_texture_dims(target));
  1010.  
  1011.    for (i = 0; i < depth; i++) {
  1012.       if (compressed_dst) {
  1013.          _mesa_get_compressed_teximage(ctx, target, level, dest,
  1014.                                        texObj, texImage);
  1015.       }
  1016.       else {
  1017.          _mesa_get_teximage(ctx, target, level, format, type, dest,
  1018.                             texObj, texImage);
  1019.       }
  1020.  
  1021.       if (stImage->pt && i + 1 < depth) {
  1022.          /* unmap this slice */
  1023.          st_texture_image_unmap(st, stImage);
  1024.          /* map next slice of 3D texture */
  1025.          texImage->Data = st_texture_image_map(st, stImage, i + 1,
  1026.                                                PIPE_TRANSFER_READ, 0, 0,
  1027.                                                stImage->base.Width,
  1028.                                                stImage->base.Height);
  1029.          dest += dstImageStride;
  1030.       }
  1031.    }
  1032.  
  1033.    texImage->Depth = depth;
  1034.  
  1035.    /* Unmap */
  1036.    if (stImage->pt) {
  1037.       st_texture_image_unmap(st, stImage);
  1038.       texImage->Data = NULL;
  1039.    }
  1040. }
  1041.  
  1042.  
  1043. static void
  1044. st_GetTexImage(struct gl_context * ctx, GLenum target, GLint level,
  1045.                GLenum format, GLenum type, GLvoid * pixels,
  1046.                struct gl_texture_object *texObj,
  1047.                struct gl_texture_image *texImage)
  1048. {
  1049.    st_get_tex_image(ctx, target, level, format, type, pixels, texObj, texImage,
  1050.                     GL_FALSE);
  1051. }
  1052.  
  1053.  
  1054. static void
  1055. st_GetCompressedTexImage(struct gl_context *ctx, GLenum target, GLint level,
  1056.                          GLvoid *pixels,
  1057.                          struct gl_texture_object *texObj,
  1058.                          struct gl_texture_image *texImage)
  1059. {
  1060.    st_get_tex_image(ctx, target, level, 0, 0, pixels, texObj, texImage,
  1061.                     GL_TRUE);
  1062. }
  1063.  
  1064.  
  1065.  
  1066. static void
  1067. st_TexSubimage(struct gl_context *ctx, GLint dims, GLenum target, GLint level,
  1068.                GLint xoffset, GLint yoffset, GLint zoffset,
  1069.                GLint width, GLint height, GLint depth,
  1070.                GLenum format, GLenum type, const void *pixels,
  1071.                const struct gl_pixelstore_attrib *packing,
  1072.                struct gl_texture_object *texObj,
  1073.                struct gl_texture_image *texImage)
  1074. {
  1075.    struct st_context *st = st_context(ctx);
  1076.    struct pipe_screen *screen = st->pipe->screen;
  1077.    struct st_texture_image *stImage = st_texture_image(texImage);
  1078.    GLuint dstRowStride;
  1079.    const GLuint srcImageStride =
  1080.       _mesa_image_image_stride(packing, width, height, format, type);
  1081.    GLint i;
  1082.    const GLubyte *src;
  1083.    /* init to silence warning only: */
  1084.    enum pipe_transfer_usage transfer_usage = PIPE_TRANSFER_WRITE;
  1085.  
  1086.    DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
  1087.        _mesa_lookup_enum_by_nr(target),
  1088.        level, xoffset, yoffset, width, height);
  1089.  
  1090.    pixels =
  1091.       _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format,
  1092.                                   type, pixels, packing, "glTexSubImage2D");
  1093.    if (!pixels)
  1094.       return;
  1095.  
  1096.    /* See if we can do texture compression with a blit/render.
  1097.     */
  1098.    if (!ctx->Mesa_DXTn &&
  1099.        _mesa_is_format_compressed(texImage->TexFormat) &&
  1100.        screen->is_format_supported(screen,
  1101.                                    stImage->pt->format,
  1102.                                    stImage->pt->target, 0,
  1103.                                    PIPE_BIND_RENDER_TARGET, 0)) {
  1104.       if (compress_with_blit(ctx, target, level,
  1105.                              xoffset, yoffset, zoffset,
  1106.                              width, height, depth,
  1107.                              format, type, pixels, packing, texImage)) {
  1108.          goto done;
  1109.       }
  1110.    }
  1111.  
  1112.    /* Map buffer if necessary.  Need to lock to prevent other contexts
  1113.     * from uploading the buffer under us.
  1114.     */
  1115.    if (stImage->pt) {
  1116.       if (format == GL_DEPTH_COMPONENT &&
  1117.           util_format_is_depth_and_stencil(stImage->pt->format))
  1118.          transfer_usage = PIPE_TRANSFER_READ_WRITE;
  1119.       else
  1120.          transfer_usage = PIPE_TRANSFER_WRITE;
  1121.  
  1122.       texImage->Data = st_texture_image_map(st, stImage, zoffset,
  1123.                                             transfer_usage,
  1124.                                             xoffset, yoffset,
  1125.                                             width, height);
  1126.    }
  1127.  
  1128.    if (!texImage->Data) {
  1129.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
  1130.       goto done;
  1131.    }
  1132.  
  1133.    src = (const GLubyte *) pixels;
  1134.    dstRowStride = stImage->transfer->stride;
  1135.  
  1136.    for (i = 0; i < depth; i++) {
  1137.       if (!_mesa_texstore(ctx, dims, texImage->_BaseFormat,
  1138.                           texImage->TexFormat,
  1139.                           texImage->Data,
  1140.                           0, 0, 0,
  1141.                           dstRowStride,
  1142.                           texImage->ImageOffsets,
  1143.                           width, height, 1,
  1144.                           format, type, src, packing)) {
  1145.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
  1146.       }
  1147.  
  1148.       if (stImage->pt && i + 1 < depth) {
  1149.          /* unmap this slice */
  1150.          st_texture_image_unmap(st, stImage);
  1151.          /* map next slice of 3D texture */
  1152.          texImage->Data = st_texture_image_map(st, stImage,
  1153.                                                zoffset + i + 1,
  1154.                                                transfer_usage,
  1155.                                                xoffset, yoffset,
  1156.                                                width, height);
  1157.          src += srcImageStride;
  1158.       }
  1159.    }
  1160.  
  1161. done:
  1162.    _mesa_unmap_teximage_pbo(ctx, packing);
  1163.  
  1164.    if (stImage->pt && texImage->Data) {
  1165.       st_texture_image_unmap(st, stImage);
  1166.       texImage->Data = NULL;
  1167.    }
  1168. }
  1169.  
  1170.  
  1171.  
  1172. static void
  1173. st_TexSubImage3D(struct gl_context *ctx, GLenum target, GLint level,
  1174.                  GLint xoffset, GLint yoffset, GLint zoffset,
  1175.                  GLsizei width, GLsizei height, GLsizei depth,
  1176.                  GLenum format, GLenum type, const GLvoid *pixels,
  1177.                  const struct gl_pixelstore_attrib *packing,
  1178.                  struct gl_texture_object *texObj,
  1179.                  struct gl_texture_image *texImage)
  1180. {
  1181.    st_TexSubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
  1182.                   width, height, depth, format, type,
  1183.                   pixels, packing, texObj, texImage);
  1184. }
  1185.  
  1186.  
  1187. static void
  1188. st_TexSubImage2D(struct gl_context *ctx, GLenum target, GLint level,
  1189.                  GLint xoffset, GLint yoffset,
  1190.                  GLsizei width, GLsizei height,
  1191.                  GLenum format, GLenum type, const GLvoid * pixels,
  1192.                  const struct gl_pixelstore_attrib *packing,
  1193.                  struct gl_texture_object *texObj,
  1194.                  struct gl_texture_image *texImage)
  1195. {
  1196.    st_TexSubimage(ctx, 2, target, level, xoffset, yoffset, 0,
  1197.                   width, height, 1, format, type,
  1198.                   pixels, packing, texObj, texImage);
  1199. }
  1200.  
  1201.  
  1202. static void
  1203. st_TexSubImage1D(struct gl_context *ctx, GLenum target, GLint level,
  1204.                  GLint xoffset, GLsizei width, GLenum format, GLenum type,
  1205.                  const GLvoid * pixels,
  1206.                  const struct gl_pixelstore_attrib *packing,
  1207.                  struct gl_texture_object *texObj,
  1208.                  struct gl_texture_image *texImage)
  1209. {
  1210.    st_TexSubimage(ctx, 1, target, level, xoffset, 0, 0, width, 1, 1,
  1211.                   format, type, pixels, packing, texObj, texImage);
  1212. }
  1213.  
  1214.  
  1215. static void
  1216. st_CompressedTexSubImage1D(struct gl_context *ctx, GLenum target, GLint level,
  1217.                            GLint xoffset, GLsizei width,
  1218.                            GLenum format,
  1219.                            GLsizei imageSize, const GLvoid *data,
  1220.                            struct gl_texture_object *texObj,
  1221.                            struct gl_texture_image *texImage)
  1222. {
  1223.    assert(0);
  1224. }
  1225.  
  1226.  
  1227. static void
  1228. st_CompressedTexSubImage2D(struct gl_context *ctx, GLenum target, GLint level,
  1229.                            GLint xoffset, GLint yoffset,
  1230.                            GLsizei width, GLint height,
  1231.                            GLenum format,
  1232.                            GLsizei imageSize, const GLvoid *data,
  1233.                            struct gl_texture_object *texObj,
  1234.                            struct gl_texture_image *texImage)
  1235. {
  1236.    struct st_context *st = st_context(ctx);
  1237.    struct st_texture_image *stImage = st_texture_image(texImage);
  1238.    int srcBlockStride;
  1239.    int dstBlockStride;
  1240.    int y;
  1241.    enum pipe_format pformat;
  1242.  
  1243.    if (stImage->pt) {
  1244.       pformat = stImage->pt->format;
  1245.  
  1246.       texImage->Data = st_texture_image_map(st, stImage, 0,
  1247.                                             PIPE_TRANSFER_WRITE,
  1248.                                             xoffset, yoffset,
  1249.                                             width, height);
  1250.      
  1251.       srcBlockStride = util_format_get_stride(pformat, width);
  1252.       dstBlockStride = stImage->transfer->stride;
  1253.    } else {
  1254.       assert(stImage->pt);
  1255.       /* TODO find good values for block and strides */
  1256.       /* TODO also adjust texImage->data for yoffset/xoffset */
  1257.       return;
  1258.    }
  1259.  
  1260.    if (!texImage->Data) {
  1261.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexSubImage");
  1262.       return;
  1263.    }
  1264.  
  1265.    assert(xoffset % util_format_get_blockwidth(pformat) == 0);
  1266.    assert(yoffset % util_format_get_blockheight(pformat) == 0);
  1267.  
  1268.    for (y = 0; y < height; y += util_format_get_blockheight(pformat)) {
  1269.       /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
  1270.       const char *src = (const char*)data + srcBlockStride * util_format_get_nblocksy(pformat, y);
  1271.       char *dst = (char*)texImage->Data + dstBlockStride * util_format_get_nblocksy(pformat, y);
  1272.       memcpy(dst, src, util_format_get_stride(pformat, width));
  1273.    }
  1274.  
  1275.    if (stImage->pt) {
  1276.       st_texture_image_unmap(st, stImage);
  1277.       texImage->Data = NULL;
  1278.    }
  1279. }
  1280.  
  1281.  
  1282. static void
  1283. st_CompressedTexSubImage3D(struct gl_context *ctx, GLenum target, GLint level,
  1284.                            GLint xoffset, GLint yoffset, GLint zoffset,
  1285.                            GLsizei width, GLint height, GLint depth,
  1286.                            GLenum format,
  1287.                            GLsizei imageSize, const GLvoid *data,
  1288.                            struct gl_texture_object *texObj,
  1289.                            struct gl_texture_image *texImage)
  1290. {
  1291.    assert(0);
  1292. }
  1293.  
  1294.  
  1295.  
  1296. /**
  1297.  * Do a CopyTexSubImage operation using a read transfer from the source,
  1298.  * a write transfer to the destination and get_tile()/put_tile() to access
  1299.  * the pixels/texels.
  1300.  *
  1301.  * Note: srcY=0=TOP of renderbuffer
  1302.  */
  1303. static void
  1304. fallback_copy_texsubimage(struct gl_context *ctx, GLenum target, GLint level,
  1305.                           struct st_renderbuffer *strb,
  1306.                           struct st_texture_image *stImage,
  1307.                           GLenum baseFormat,
  1308.                           GLint destX, GLint destY, GLint destZ,
  1309.                           GLint srcX, GLint srcY,
  1310.                           GLsizei width, GLsizei height)
  1311. {
  1312.    struct st_context *st = st_context(ctx);
  1313.    struct pipe_context *pipe = st->pipe;
  1314.    struct pipe_transfer *src_trans;
  1315.    GLvoid *texDest;
  1316.    enum pipe_transfer_usage transfer_usage;
  1317.  
  1318.    if (ST_DEBUG & DEBUG_FALLBACK)
  1319.       debug_printf("%s: fallback processing\n", __FUNCTION__);
  1320.  
  1321.    assert(width <= MAX_WIDTH);
  1322.  
  1323.    if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
  1324.       srcY = strb->Base.Height - srcY - height;
  1325.    }
  1326.  
  1327.    src_trans = pipe_get_transfer(st_context(ctx)->pipe,
  1328.                                  strb->texture,
  1329.                                  0, 0,
  1330.                                  PIPE_TRANSFER_READ,
  1331.                                  srcX, srcY,
  1332.                                  width, height);
  1333.  
  1334.    if ((baseFormat == GL_DEPTH_COMPONENT ||
  1335.         baseFormat == GL_DEPTH_STENCIL) &&
  1336.        util_format_is_depth_and_stencil(stImage->pt->format))
  1337.       transfer_usage = PIPE_TRANSFER_READ_WRITE;
  1338.    else
  1339.       transfer_usage = PIPE_TRANSFER_WRITE;
  1340.  
  1341.    /* XXX this used to ignore destZ param */
  1342.    texDest = st_texture_image_map(st, stImage, destZ, transfer_usage,
  1343.                                   destX, destY, width, height);
  1344.  
  1345.    if (baseFormat == GL_DEPTH_COMPONENT ||
  1346.        baseFormat == GL_DEPTH_STENCIL) {
  1347.       const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
  1348.                                      ctx->Pixel.DepthBias != 0.0F);
  1349.       GLint row, yStep;
  1350.  
  1351.       /* determine bottom-to-top vs. top-to-bottom order for src buffer */
  1352.       if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
  1353.          srcY = height - 1;
  1354.          yStep = -1;
  1355.       }
  1356.       else {
  1357.          srcY = 0;
  1358.          yStep = 1;
  1359.       }
  1360.  
  1361.       /* To avoid a large temp memory allocation, do copy row by row */
  1362.       for (row = 0; row < height; row++, srcY += yStep) {
  1363.          uint data[MAX_WIDTH];
  1364.          pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
  1365.          if (scaleOrBias) {
  1366.             _mesa_scale_and_bias_depth_uint(ctx, width, data);
  1367.          }
  1368.          pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
  1369.       }
  1370.    }
  1371.    else {
  1372.       /* RGBA format */
  1373.       GLfloat *tempSrc =
  1374.          (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
  1375.  
  1376.       if (tempSrc && texDest) {
  1377.          const GLint dims = 2;
  1378.          const GLint dstRowStride = stImage->transfer->stride;
  1379.          struct gl_texture_image *texImage = &stImage->base;
  1380.          struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
  1381.  
  1382.          if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
  1383.             unpack.Invert = GL_TRUE;
  1384.          }
  1385.  
  1386.          /* get float/RGBA image from framebuffer */
  1387.          /* XXX this usually involves a lot of int/float conversion.
  1388.           * try to avoid that someday.
  1389.           */
  1390.          pipe_get_tile_rgba(pipe, src_trans, 0, 0, width, height, tempSrc);
  1391.  
  1392.          /* Store into texture memory.
  1393.           * Note that this does some special things such as pixel transfer
  1394.           * ops and format conversion.  In particular, if the dest tex format
  1395.           * is actually RGBA but the user created the texture as GL_RGB we
  1396.           * need to fill-in/override the alpha channel with 1.0.
  1397.           */
  1398.          _mesa_texstore(ctx, dims,
  1399.                         texImage->_BaseFormat,
  1400.                         texImage->TexFormat,
  1401.                         texDest,
  1402.                         0, 0, 0,
  1403.                         dstRowStride,
  1404.                         texImage->ImageOffsets,
  1405.                         width, height, 1,
  1406.                         GL_RGBA, GL_FLOAT, tempSrc, /* src */
  1407.                         &unpack);
  1408.       }
  1409.       else {
  1410.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
  1411.       }
  1412.  
  1413.       if (tempSrc)
  1414.          free(tempSrc);
  1415.    }
  1416.  
  1417.    st_texture_image_unmap(st, stImage);
  1418.    pipe->transfer_destroy(pipe, src_trans);
  1419. }
  1420.  
  1421.  
  1422.  
  1423. /**
  1424.  * If the format of the src renderbuffer and the format of the dest
  1425.  * texture are compatible (in terms of blitting), return a TGSI writemask
  1426.  * to be used during the blit.
  1427.  * If the src/dest are incompatible, return 0.
  1428.  */
  1429. static unsigned
  1430. compatible_src_dst_formats(struct gl_context *ctx,
  1431.                            const struct gl_renderbuffer *src,
  1432.                            const struct gl_texture_image *dst)
  1433. {
  1434.    /* Get logical base formats for the src and dest.
  1435.     * That is, use the user-requested formats and not the actual, device-
  1436.     * chosen formats.
  1437.     * For example, the user may have requested an A8 texture but the
  1438.     * driver may actually be using an RGBA texture format.  When we
  1439.     * copy/blit to that texture, we only want to copy the Alpha channel
  1440.     * and not the RGB channels.
  1441.     *
  1442.     * Similarly, when the src FBO was created an RGB format may have been
  1443.     * requested but the driver actually chose an RGBA format.  In that case,
  1444.     * we don't want to copy the undefined Alpha channel to the dest texture
  1445.     * (it should be 1.0).
  1446.     */
  1447.    const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
  1448.    const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
  1449.  
  1450.    /**
  1451.     * XXX when we have red-only and red/green renderbuffers we'll need
  1452.     * to add more cases here (or implement a general-purpose routine that
  1453.     * queries the existance of the R,G,B,A channels in the src and dest).
  1454.     */
  1455.    if (srcFormat == dstFormat) {
  1456.       /* This is the same as matching_base_formats, which should
  1457.        * always pass, as it did previously.
  1458.        */
  1459.       return TGSI_WRITEMASK_XYZW;
  1460.    }
  1461.    else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
  1462.       /* Make sure that A in the dest is 1.  The actual src format
  1463.        * may be RGBA and have undefined A values.
  1464.        */
  1465.       return TGSI_WRITEMASK_XYZ;
  1466.    }
  1467.    else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
  1468.       /* Make sure that A in the dest is 1.  The actual dst format
  1469.        * may be RGBA and will need A=1 to provide proper alpha values
  1470.        * when sampled later.
  1471.        */
  1472.       return TGSI_WRITEMASK_XYZ;
  1473.    }
  1474.    else {
  1475.       if (ST_DEBUG & DEBUG_FALLBACK)
  1476.          debug_printf("%s failed for src %s, dst %s\n",
  1477.                       __FUNCTION__,
  1478.                       _mesa_lookup_enum_by_nr(srcFormat),
  1479.                       _mesa_lookup_enum_by_nr(dstFormat));
  1480.  
  1481.       /* Otherwise fail.
  1482.        */
  1483.       return 0;
  1484.    }
  1485. }
  1486.  
  1487.  
  1488.  
  1489. /**
  1490.  * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
  1491.  * Note that the region to copy has already been clipped so we know we
  1492.  * won't read from outside the source renderbuffer's bounds.
  1493.  *
  1494.  * Note: srcY=0=Bottom of renderbuffer (GL convention)
  1495.  */
  1496. static void
  1497. st_copy_texsubimage(struct gl_context *ctx,
  1498.                     GLenum target, GLint level,
  1499.                     GLint destX, GLint destY, GLint destZ,
  1500.                     GLint srcX, GLint srcY,
  1501.                     GLsizei width, GLsizei height)
  1502. {
  1503.    struct gl_texture_unit *texUnit =
  1504.       &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
  1505.    struct gl_texture_object *texObj =
  1506.       _mesa_select_tex_object(ctx, texUnit, target);
  1507.    struct gl_texture_image *texImage =
  1508.       _mesa_select_tex_image(ctx, texObj, target, level);
  1509.    struct st_texture_image *stImage = st_texture_image(texImage);
  1510.    const GLenum texBaseFormat = texImage->_BaseFormat;
  1511.    struct gl_framebuffer *fb = ctx->ReadBuffer;
  1512.    struct st_renderbuffer *strb;
  1513.    struct st_context *st = st_context(ctx);
  1514.    struct pipe_context *pipe = st->pipe;
  1515.    struct pipe_screen *screen = pipe->screen;
  1516.    enum pipe_format dest_format, src_format;
  1517.    GLboolean use_fallback = GL_TRUE;
  1518.    GLboolean matching_base_formats;
  1519.    GLuint format_writemask, sample_count;
  1520.    struct pipe_surface *dest_surface = NULL;
  1521.    GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
  1522.  
  1523.    /* make sure finalize_textures has been called?
  1524.     */
  1525.    if (0) st_validate_state(st);
  1526.  
  1527.    /* determine if copying depth or color data */
  1528.    if (texBaseFormat == GL_DEPTH_COMPONENT ||
  1529.        texBaseFormat == GL_DEPTH_STENCIL) {
  1530.       strb = st_renderbuffer(fb->_DepthBuffer);
  1531.       if (strb->Base.Wrapped) {
  1532.          strb = st_renderbuffer(strb->Base.Wrapped);
  1533.       }
  1534.    }
  1535.    else {
  1536.       /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
  1537.       strb = st_renderbuffer(fb->_ColorReadBuffer);
  1538.    }
  1539.  
  1540.    if (!strb || !strb->surface || !stImage->pt) {
  1541.       debug_printf("%s: null strb or stImage\n", __FUNCTION__);
  1542.       return;
  1543.    }
  1544.  
  1545.    sample_count = strb->surface->texture->nr_samples;
  1546.    /* I believe this would be legal, presumably would need to do a resolve
  1547.       for color, and for depth/stencil spec says to just use one of the
  1548.       depth/stencil samples per pixel? Need some transfer clarifications. */
  1549.    assert(sample_count < 2);
  1550.  
  1551.    if (srcX < 0) {
  1552.       width -= -srcX;
  1553.       destX += -srcX;
  1554.       srcX = 0;
  1555.    }
  1556.  
  1557.    if (srcY < 0) {
  1558.       height -= -srcY;
  1559.       destY += -srcY;
  1560.       srcY = 0;
  1561.    }
  1562.  
  1563.    if (destX < 0) {
  1564.       width -= -destX;
  1565.       srcX += -destX;
  1566.       destX = 0;
  1567.    }
  1568.  
  1569.    if (destY < 0) {
  1570.       height -= -destY;
  1571.       srcY += -destY;
  1572.       destY = 0;
  1573.    }
  1574.  
  1575.    if (width < 0 || height < 0)
  1576.       return;
  1577.  
  1578.  
  1579.    assert(strb);
  1580.    assert(strb->surface);
  1581.    assert(stImage->pt);
  1582.  
  1583.    src_format = strb->surface->format;
  1584.    dest_format = stImage->pt->format;
  1585.  
  1586.    /*
  1587.     * Determine if the src framebuffer and dest texture have the same
  1588.     * base format.  We need this to detect a case such as the framebuffer
  1589.     * being GL_RGBA but the texture being GL_RGB.  If the actual hardware
  1590.     * texture format stores RGBA we need to set A=1 (overriding the
  1591.     * framebuffer's alpha values).  We can't do that with the blit or
  1592.     * textured-quad paths.
  1593.     */
  1594.    matching_base_formats =
  1595.       (_mesa_get_format_base_format(strb->Base.Format) ==
  1596.        _mesa_get_format_base_format(texImage->TexFormat));
  1597.    format_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
  1598.  
  1599.    if (ctx->_ImageTransferState == 0x0) {
  1600.  
  1601.       if (matching_base_formats &&
  1602.           src_format == dest_format &&
  1603.           !do_flip)
  1604.       {
  1605.          /* use surface_copy() / blit */
  1606.          struct pipe_box src_box;
  1607.          u_box_2d_zslice(srcX, srcY, strb->surface->u.tex.first_layer,
  1608.                          width, height, &src_box);
  1609.  
  1610.          /* for resource_copy_region(), y=0=top, always */
  1611.          pipe->resource_copy_region(pipe,
  1612.                                     /* dest */
  1613.                                     stImage->pt,
  1614.                                     stImage->level,
  1615.                                     destX, destY, destZ + stImage->face,
  1616.                                     /* src */
  1617.                                     strb->texture,
  1618.                                     strb->surface->u.tex.level,
  1619.                                     &src_box);
  1620.          use_fallback = GL_FALSE;
  1621.       }
  1622.       else if (format_writemask &&
  1623.                texBaseFormat != GL_DEPTH_COMPONENT &&
  1624.                texBaseFormat != GL_DEPTH_STENCIL &&
  1625.                screen->is_format_supported(screen, src_format,
  1626.                                            PIPE_TEXTURE_2D, sample_count,
  1627.                                            PIPE_BIND_SAMPLER_VIEW,
  1628.                                            0) &&
  1629.                screen->is_format_supported(screen, dest_format,
  1630.                                            PIPE_TEXTURE_2D, 0,
  1631.                                            PIPE_BIND_RENDER_TARGET,
  1632.                                            0)) {
  1633.          /* draw textured quad to do the copy */
  1634.          GLint srcY0, srcY1;
  1635.          struct pipe_surface surf_tmpl;
  1636.          memset(&surf_tmpl, 0, sizeof(surf_tmpl));
  1637.          surf_tmpl.format = stImage->pt->format;
  1638.          surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
  1639.          surf_tmpl.u.tex.level = stImage->level;
  1640.          surf_tmpl.u.tex.first_layer = stImage->face + destZ;
  1641.          surf_tmpl.u.tex.last_layer = stImage->face + destZ;
  1642.  
  1643.          dest_surface = pipe->create_surface(pipe, stImage->pt,
  1644.                                              &surf_tmpl);
  1645.  
  1646.          if (do_flip) {
  1647.             srcY1 = strb->Base.Height - srcY - height;
  1648.             srcY0 = srcY1 + height;
  1649.          }
  1650.          else {
  1651.             srcY0 = srcY;
  1652.             srcY1 = srcY0 + height;
  1653.          }
  1654.  
  1655.          util_blit_pixels_writemask(st->blit,
  1656.                                     strb->texture,
  1657.                                     strb->surface->u.tex.level,
  1658.                                     srcX, srcY0,
  1659.                                     srcX + width, srcY1,
  1660.                                     strb->surface->u.tex.first_layer,
  1661.                                     dest_surface,
  1662.                                     destX, destY,
  1663.                                     destX + width, destY + height,
  1664.                                     0.0, PIPE_TEX_MIPFILTER_NEAREST,
  1665.                                     format_writemask);
  1666.          use_fallback = GL_FALSE;
  1667.       }
  1668.  
  1669.       if (dest_surface)
  1670.          pipe_surface_reference(&dest_surface, NULL);
  1671.    }
  1672.  
  1673.    if (use_fallback) {
  1674.       /* software fallback */
  1675.       fallback_copy_texsubimage(ctx, target, level,
  1676.                                 strb, stImage, texBaseFormat,
  1677.                                 destX, destY, destZ,
  1678.                                 srcX, srcY, width, height);
  1679.    }
  1680. }
  1681.  
  1682.  
  1683.  
  1684. static void
  1685. st_CopyTexImage1D(struct gl_context * ctx, GLenum target, GLint level,
  1686.                   GLenum internalFormat,
  1687.                   GLint x, GLint y, GLsizei width, GLint border)
  1688. {
  1689.    struct gl_texture_unit *texUnit =
  1690.       &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
  1691.    struct gl_texture_object *texObj =
  1692.       _mesa_select_tex_object(ctx, texUnit, target);
  1693.    struct gl_texture_image *texImage =
  1694.       _mesa_select_tex_image(ctx, texObj, target, level);
  1695.  
  1696.    /* Setup or redefine the texture object, texture and texture
  1697.     * image.  Don't populate yet.  
  1698.     */
  1699.    ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
  1700.                           width, border,
  1701.                           GL_RGBA, CHAN_TYPE, NULL,
  1702.                           &ctx->DefaultPacking, texObj, texImage);
  1703.  
  1704.    st_copy_texsubimage(ctx, target, level,
  1705.                        0, 0, 0,  /* destX,Y,Z */
  1706.                        x, y, width, 1);  /* src X, Y, size */
  1707. }
  1708.  
  1709.  
  1710. static void
  1711. st_CopyTexImage2D(struct gl_context * ctx, GLenum target, GLint level,
  1712.                   GLenum internalFormat,
  1713.                   GLint x, GLint y, GLsizei width, GLsizei height,
  1714.                   GLint border)
  1715. {
  1716.    struct gl_texture_unit *texUnit =
  1717.       &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
  1718.    struct gl_texture_object *texObj =
  1719.       _mesa_select_tex_object(ctx, texUnit, target);
  1720.    struct gl_texture_image *texImage =
  1721.       _mesa_select_tex_image(ctx, texObj, target, level);
  1722.  
  1723.    /* Setup or redefine the texture object, texture and texture
  1724.     * image.  Don't populate yet.  
  1725.     */
  1726.    ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
  1727.                           width, height, border,
  1728.                           GL_RGBA, CHAN_TYPE, NULL,
  1729.                           &ctx->DefaultPacking, texObj, texImage);
  1730.  
  1731.    st_copy_texsubimage(ctx, target, level,
  1732.                        0, 0, 0,  /* destX,Y,Z */
  1733.                        x, y, width, height);  /* src X, Y, size */
  1734. }
  1735.  
  1736.  
  1737. static void
  1738. st_CopyTexSubImage1D(struct gl_context * ctx, GLenum target, GLint level,
  1739.                      GLint xoffset, GLint x, GLint y, GLsizei width)
  1740. {
  1741.    const GLint yoffset = 0, zoffset = 0;
  1742.    const GLsizei height = 1;
  1743.    st_copy_texsubimage(ctx, target, level,
  1744.                        xoffset, yoffset, zoffset,  /* destX,Y,Z */
  1745.                        x, y, width, height);  /* src X, Y, size */
  1746. }
  1747.  
  1748.  
  1749. static void
  1750. st_CopyTexSubImage2D(struct gl_context * ctx, GLenum target, GLint level,
  1751.                      GLint xoffset, GLint yoffset,
  1752.                      GLint x, GLint y, GLsizei width, GLsizei height)
  1753. {
  1754.    const GLint zoffset = 0;
  1755.    st_copy_texsubimage(ctx, target, level,
  1756.                        xoffset, yoffset, zoffset,  /* destX,Y,Z */
  1757.                        x, y, width, height);  /* src X, Y, size */
  1758. }
  1759.  
  1760.  
  1761. static void
  1762. st_CopyTexSubImage3D(struct gl_context * ctx, GLenum target, GLint level,
  1763.                      GLint xoffset, GLint yoffset, GLint zoffset,
  1764.                      GLint x, GLint y, GLsizei width, GLsizei height)
  1765. {
  1766.    st_copy_texsubimage(ctx, target, level,
  1767.                        xoffset, yoffset, zoffset,  /* destX,Y,Z */
  1768.                        x, y, width, height);  /* src X, Y, size */
  1769. }
  1770.  
  1771.  
  1772. /**
  1773.  * Copy image data from stImage into the texture object 'stObj' at level
  1774.  * 'dstLevel'.
  1775.  */
  1776. static void
  1777. copy_image_data_to_texture(struct st_context *st,
  1778.                            struct st_texture_object *stObj,
  1779.                            GLuint dstLevel,
  1780.                            struct st_texture_image *stImage)
  1781. {
  1782.    /* debug checks */
  1783.    {
  1784.       const struct gl_texture_image *dstImage =
  1785.          stObj->base.Image[stImage->face][stImage->level];
  1786.       assert(dstImage);
  1787.       assert(dstImage->Width == stImage->base.Width);
  1788.       assert(dstImage->Height == stImage->base.Height);
  1789.       assert(dstImage->Depth == stImage->base.Depth);
  1790.    }
  1791.  
  1792.    if (stImage->pt) {
  1793.       /* Copy potentially with the blitter:
  1794.        */
  1795.       st_texture_image_copy(st->pipe,
  1796.                             stObj->pt, dstLevel,  /* dest texture, level */
  1797.                             stImage->pt, stImage->level, /* src texture, level */
  1798.                             stImage->face);
  1799.  
  1800.       pipe_resource_reference(&stImage->pt, NULL);
  1801.    }
  1802.    else if (stImage->base.Data) {
  1803.       st_texture_image_data(st,
  1804.                             stObj->pt,
  1805.                             stImage->face,
  1806.                             dstLevel,
  1807.                             stImage->base.Data,
  1808.                             stImage->base.RowStride *
  1809.                             util_format_get_blocksize(stObj->pt->format),
  1810.                             stImage->base.RowStride *
  1811.                             stImage->base.Height *
  1812.                             util_format_get_blocksize(stObj->pt->format));
  1813.       _mesa_align_free(stImage->base.Data);
  1814.       stImage->base.Data = NULL;
  1815.    }
  1816.  
  1817.    pipe_resource_reference(&stImage->pt, stObj->pt);
  1818. }
  1819.  
  1820.  
  1821. /**
  1822.  * Called during state validation.  When this function is finished,
  1823.  * the texture object should be ready for rendering.
  1824.  * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
  1825.  */
  1826. GLboolean
  1827. st_finalize_texture(struct gl_context *ctx,
  1828.                     struct pipe_context *pipe,
  1829.                     struct gl_texture_object *tObj)
  1830. {
  1831.    struct st_context *st = st_context(ctx);
  1832.    struct st_texture_object *stObj = st_texture_object(tObj);
  1833.    const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
  1834.    GLuint face;
  1835.    struct st_texture_image *firstImage;
  1836.    enum pipe_format firstImageFormat;
  1837.  
  1838.    if (stObj->base._Complete) {
  1839.       /* The texture is complete and we know exactly how many mipmap levels
  1840.        * are present/needed.  This is conditional because we may be called
  1841.        * from the st_generate_mipmap() function when the texture object is
  1842.        * incomplete.  In that case, we'll have set stObj->lastLevel before
  1843.        * we get here.
  1844.        */
  1845.       if (stObj->base.MinFilter == GL_LINEAR ||
  1846.           stObj->base.MinFilter == GL_NEAREST)
  1847.          stObj->lastLevel = stObj->base.BaseLevel;
  1848.       else
  1849.          stObj->lastLevel = stObj->base._MaxLevel;
  1850.    }
  1851.  
  1852.    firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
  1853.    assert(firstImage);
  1854.  
  1855.    /* If both firstImage and stObj point to a texture which can contain
  1856.     * all active images, favour firstImage.  Note that because of the
  1857.     * completeness requirement, we know that the image dimensions
  1858.     * will match.
  1859.     */
  1860.    if (firstImage->pt &&
  1861.        firstImage->pt != stObj->pt &&
  1862.        (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
  1863.       pipe_resource_reference(&stObj->pt, firstImage->pt);
  1864.       pipe_sampler_view_reference(&stObj->sampler_view, NULL);
  1865.    }
  1866.  
  1867.    /* Find gallium format for the Mesa texture */
  1868.    firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
  1869.  
  1870.    /* If we already have a gallium texture, check that it matches the texture
  1871.     * object's format, target, size, num_levels, etc.
  1872.     */
  1873.    if (stObj->pt) {
  1874.       if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
  1875.           !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
  1876.           stObj->pt->last_level < stObj->lastLevel ||
  1877.           stObj->pt->width0 != stObj->width0 ||
  1878.           stObj->pt->height0 != stObj->height0 ||
  1879.           stObj->pt->depth0 != stObj->depth0)
  1880.       {
  1881.          /* The gallium texture does not match the Mesa texture so delete the
  1882.           * gallium texture now.  We'll make a new one below.
  1883.           */
  1884.          pipe_resource_reference(&stObj->pt, NULL);
  1885.          pipe_sampler_view_reference(&stObj->sampler_view, NULL);
  1886.          st->dirty.st |= ST_NEW_FRAMEBUFFER;
  1887.       }
  1888.    }
  1889.  
  1890.    /* May need to create a new gallium texture:
  1891.     */
  1892.    if (!stObj->pt) {
  1893.       GLuint bindings = default_bindings(st, firstImageFormat);
  1894.  
  1895.       stObj->pt = st_texture_create(st,
  1896.                                     gl_target_to_pipe(stObj->base.Target),
  1897.                                     firstImageFormat,
  1898.                                     stObj->lastLevel,
  1899.                                     stObj->width0,
  1900.                                     stObj->height0,
  1901.                                     stObj->depth0,
  1902.                                     bindings);
  1903.  
  1904.       if (!stObj->pt) {
  1905.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
  1906.          return GL_FALSE;
  1907.       }
  1908.    }
  1909.  
  1910.    /* Pull in any images not in the object's texture:
  1911.     */
  1912.    for (face = 0; face < nr_faces; face++) {
  1913.       GLuint level;
  1914.       for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
  1915.          struct st_texture_image *stImage =
  1916.             st_texture_image(stObj->base.Image[face][level]);
  1917.  
  1918.          /* Need to import images in main memory or held in other textures.
  1919.           */
  1920.          if (stImage && stObj->pt != stImage->pt) {
  1921.             copy_image_data_to_texture(st, stObj, level, stImage);
  1922.          }
  1923.       }
  1924.    }
  1925.  
  1926.    return GL_TRUE;
  1927. }
  1928.  
  1929.  
  1930. /**
  1931.  * Returns pointer to a default/dummy texture.
  1932.  * This is typically used when the current shader has tex/sample instructions
  1933.  * but the user has not provided a (any) texture(s).
  1934.  */
  1935. struct gl_texture_object *
  1936. st_get_default_texture(struct st_context *st)
  1937. {
  1938.    if (!st->default_texture) {
  1939.       static const GLenum target = GL_TEXTURE_2D;
  1940.       GLubyte pixels[16][16][4];
  1941.       struct gl_texture_object *texObj;
  1942.       struct gl_texture_image *texImg;
  1943.       GLuint i, j;
  1944.  
  1945.       /* The ARB_fragment_program spec says (0,0,0,1) should be returned
  1946.        * when attempting to sample incomplete textures.
  1947.        */
  1948.       for (i = 0; i < 16; i++) {
  1949.          for (j = 0; j < 16; j++) {
  1950.             pixels[i][j][0] = 0;
  1951.             pixels[i][j][1] = 0;
  1952.             pixels[i][j][2] = 0;
  1953.             pixels[i][j][3] = 255;
  1954.          }
  1955.       }
  1956.  
  1957.       texObj = st->ctx->Driver.NewTextureObject(st->ctx, 0, target);
  1958.  
  1959.       texImg = _mesa_get_tex_image(st->ctx, texObj, target, 0);
  1960.  
  1961.       _mesa_init_teximage_fields(st->ctx, target, texImg,
  1962.                                  16, 16, 1, 0,  /* w, h, d, border */
  1963.                                  GL_RGBA, MESA_FORMAT_RGBA8888);
  1964.  
  1965.       st_TexImage(st->ctx, 2, target,
  1966.                   0, GL_RGBA,    /* level, intformat */
  1967.                   16, 16, 1, 0,  /* w, h, d, border */
  1968.                   GL_RGBA, GL_UNSIGNED_BYTE, pixels,
  1969.                   &st->ctx->DefaultPacking,
  1970.                   texObj, texImg,
  1971.                   0, 0);
  1972.  
  1973.       texObj->MinFilter = GL_NEAREST;
  1974.       texObj->MagFilter = GL_NEAREST;
  1975.       texObj->_Complete = GL_TRUE;
  1976.  
  1977.       st->default_texture = texObj;
  1978.    }
  1979.    return st->default_texture;
  1980. }
  1981.  
  1982.  
  1983. void
  1984. st_init_texture_functions(struct dd_function_table *functions)
  1985. {
  1986.    functions->ChooseTextureFormat = st_ChooseTextureFormat;
  1987.    functions->TexImage1D = st_TexImage1D;
  1988.    functions->TexImage2D = st_TexImage2D;
  1989.    functions->TexImage3D = st_TexImage3D;
  1990.    functions->TexSubImage1D = st_TexSubImage1D;
  1991.    functions->TexSubImage2D = st_TexSubImage2D;
  1992.    functions->TexSubImage3D = st_TexSubImage3D;
  1993.    functions->CompressedTexSubImage1D = st_CompressedTexSubImage1D;
  1994.    functions->CompressedTexSubImage2D = st_CompressedTexSubImage2D;
  1995.    functions->CompressedTexSubImage3D = st_CompressedTexSubImage3D;
  1996.    functions->CopyTexImage1D = st_CopyTexImage1D;
  1997.    functions->CopyTexImage2D = st_CopyTexImage2D;
  1998.    functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
  1999.    functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
  2000.    functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
  2001.    functions->GenerateMipmap = st_generate_mipmap;
  2002.  
  2003.    functions->GetTexImage = st_GetTexImage;
  2004.  
  2005.    /* compressed texture functions */
  2006.    functions->CompressedTexImage2D = st_CompressedTexImage2D;
  2007.    functions->GetCompressedTexImage = st_GetCompressedTexImage;
  2008.  
  2009.    functions->NewTextureObject = st_NewTextureObject;
  2010.    functions->NewTextureImage = st_NewTextureImage;
  2011.    functions->DeleteTexture = st_DeleteTextureObject;
  2012.    functions->FreeTexImageData = st_FreeTextureImageData;
  2013.  
  2014.    functions->TextureMemCpy = do_memcpy;
  2015.  
  2016.    /* XXX Temporary until we can query pipe's texture sizes */
  2017.    functions->TestProxyTexImage = _mesa_test_proxy_teximage;
  2018. }
  2019.