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