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