st_cb_texture.c revision 3293bcdc80cdfa20a2381aae2b94505bdf95d857
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_flush.h"
48#include "state_tracker/st_cb_texture.h"
49#include "state_tracker/st_format.h"
50#include "state_tracker/st_texture.h"
51#include "state_tracker/st_gen_mipmap.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, 0, 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                                         0 /* flags */);
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 = pipe_get_transfer(st_context(ctx)->pipe, 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, 0,
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_RENDER_TARGET | /* util_blit may choose to render */
855		    PIPE_BIND_TRANSFER_READ);
856
857   /* create temp / dest surface */
858   if (!util_create_rgba_surface(screen, width, height, bind,
859                                 &dst_texture, &dst_surface)) {
860      _mesa_problem(ctx, "util_create_rgba_surface() failed "
861                    "in decompress_with_blit()");
862      return;
863   }
864
865   /* blit/render/decompress */
866   util_blit_pixels_tex(st->blit,
867                        src_view,      /* pipe_resource (src) */
868                        0, 0,             /* src x0, y0 */
869                        width, height,    /* src x1, y1 */
870                        dst_surface,      /* pipe_surface (dst) */
871                        0, 0,             /* dst x0, y0 */
872                        width, height,    /* dst x1, y1 */
873                        0.0,              /* z */
874                        PIPE_TEX_MIPFILTER_NEAREST);
875
876   /* map the dst_surface so we can read from it */
877   tex_xfer = pipe_get_transfer(st_context(ctx)->pipe,
878					     dst_texture, 0, 0, 0,
879					     PIPE_TRANSFER_READ,
880					     0, 0, width, height);
881
882   pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
883
884   /* copy/pack data into user buffer */
885   if (st_equal_formats(stImage->pt->format, format, type)) {
886      /* memcpy */
887      const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format);
888      ubyte *map = pipe_transfer_map(pipe, tex_xfer);
889      GLuint row;
890      for (row = 0; row < height; row++) {
891         GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
892                                              height, format, type, row, 0);
893         memcpy(dest, map, bytesPerRow);
894         map += tex_xfer->stride;
895      }
896      pipe_transfer_unmap(pipe, tex_xfer);
897   }
898   else {
899      /* format translation via floats */
900      GLuint row;
901      for (row = 0; row < height; row++) {
902         const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
903         GLfloat rgba[4 * MAX_WIDTH];
904         GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
905                                              height, format, type, row, 0);
906
907         if (ST_DEBUG & DEBUG_FALLBACK)
908            debug_printf("%s: fallback format translation\n", __FUNCTION__);
909
910         /* get float[4] rgba row from surface */
911         pipe_get_tile_rgba(pipe, tex_xfer, 0, row, width, 1, rgba);
912
913         _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
914                                    type, dest, &ctx->Pack, transferOps);
915      }
916   }
917
918   _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
919
920   pipe->transfer_destroy(pipe, tex_xfer);
921
922   /* destroy the temp / dest surface */
923   util_destroy_rgba_surface(dst_texture, dst_surface);
924}
925
926
927
928/**
929 * Need to map texture image into memory before copying image data,
930 * then unmap it.
931 */
932static void
933st_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
934                 GLenum format, GLenum type, GLvoid * pixels,
935                 struct gl_texture_object *texObj,
936                 struct gl_texture_image *texImage, GLboolean compressed_dst)
937{
938   struct st_context *st = st_context(ctx);
939   struct st_texture_image *stImage = st_texture_image(texImage);
940   const GLuint dstImageStride =
941      _mesa_image_image_stride(&ctx->Pack, texImage->Width, texImage->Height,
942                               format, type);
943   GLuint depth, i;
944   GLubyte *dest;
945
946   if (stImage->pt &&
947       util_format_is_s3tc(stImage->pt->format) &&
948       !compressed_dst) {
949      /* Need to decompress the texture.
950       * We'll do this by rendering a textured quad.
951       * Note that we only expect RGBA formats (no Z/depth formats).
952       */
953      decompress_with_blit(ctx, target, level, format, type, pixels,
954                           texObj, texImage);
955      return;
956   }
957
958   /* Map */
959   if (stImage->pt) {
960      /* Image is stored in hardware format in a buffer managed by the
961       * kernel.  Need to explicitly map and unmap it.
962       */
963      texImage->Data = st_texture_image_map(st, stImage, 0,
964                                            PIPE_TRANSFER_READ, 0, 0,
965                                            stImage->base.Width,
966                                            stImage->base.Height);
967      /* compute stride in texels from stride in bytes */
968      texImage->RowStride = stImage->transfer->stride
969         * util_format_get_blockwidth(stImage->pt->format)
970         / util_format_get_blocksize(stImage->pt->format);
971   }
972   else {
973      /* Otherwise, the image should actually be stored in
974       * texImage->Data.  This is pretty confusing for
975       * everybody, I'd much prefer to separate the two functions of
976       * texImage->Data - storage for texture images in main memory
977       * and access (ie mappings) of images.  In other words, we'd
978       * create a new texImage->Map field and leave Data simply for
979       * storage.
980       */
981      assert(texImage->Data);
982   }
983
984   depth = texImage->Depth;
985   texImage->Depth = 1;
986
987   dest = (GLubyte *) pixels;
988
989   for (i = 0; i < depth; i++) {
990      if (compressed_dst) {
991	 _mesa_get_compressed_teximage(ctx, target, level, dest,
992				       texObj, texImage);
993      }
994      else {
995	 _mesa_get_teximage(ctx, target, level, format, type, dest,
996			    texObj, texImage);
997      }
998
999      if (stImage->pt && i + 1 < depth) {
1000         /* unmap this slice */
1001	 st_texture_image_unmap(st, stImage);
1002         /* map next slice of 3D texture */
1003	 texImage->Data = st_texture_image_map(st, stImage, i + 1,
1004                                               PIPE_TRANSFER_READ, 0, 0,
1005                                               stImage->base.Width,
1006                                               stImage->base.Height);
1007	 dest += dstImageStride;
1008      }
1009   }
1010
1011   texImage->Depth = depth;
1012
1013   /* Unmap */
1014   if (stImage->pt) {
1015      st_texture_image_unmap(st, stImage);
1016      texImage->Data = NULL;
1017   }
1018}
1019
1020
1021static void
1022st_GetTexImage(GLcontext * ctx, GLenum target, GLint level,
1023               GLenum format, GLenum type, GLvoid * pixels,
1024               struct gl_texture_object *texObj,
1025               struct gl_texture_image *texImage)
1026{
1027   st_get_tex_image(ctx, target, level, format, type, pixels, texObj, texImage,
1028                    GL_FALSE);
1029}
1030
1031
1032static void
1033st_GetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level,
1034                         GLvoid *pixels,
1035                         struct gl_texture_object *texObj,
1036                         struct gl_texture_image *texImage)
1037{
1038   st_get_tex_image(ctx, target, level, 0, 0, pixels, texObj, texImage,
1039                    GL_TRUE);
1040}
1041
1042
1043
1044static void
1045st_TexSubimage(GLcontext *ctx, GLint dims, GLenum target, GLint level,
1046               GLint xoffset, GLint yoffset, GLint zoffset,
1047               GLint width, GLint height, GLint depth,
1048               GLenum format, GLenum type, const void *pixels,
1049               const struct gl_pixelstore_attrib *packing,
1050               struct gl_texture_object *texObj,
1051               struct gl_texture_image *texImage)
1052{
1053   struct st_context *st = st_context(ctx);
1054   struct pipe_screen *screen = st->pipe->screen;
1055   struct st_texture_image *stImage = st_texture_image(texImage);
1056   GLuint dstRowStride;
1057   const GLuint srcImageStride =
1058      _mesa_image_image_stride(packing, width, height, format, type);
1059   GLint i;
1060   const GLubyte *src;
1061   /* init to silence warning only: */
1062   enum pipe_transfer_usage transfer_usage = PIPE_TRANSFER_WRITE;
1063
1064   DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
1065       _mesa_lookup_enum_by_nr(target),
1066       level, xoffset, yoffset, width, height);
1067
1068   pixels =
1069      _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format,
1070                                  type, pixels, packing, "glTexSubImage2D");
1071   if (!pixels)
1072      return;
1073
1074   /* See if we can do texture compression with a blit/render.
1075    */
1076   if (!ctx->Mesa_DXTn &&
1077       _mesa_is_format_compressed(texImage->TexFormat) &&
1078       screen->is_format_supported(screen,
1079                                   stImage->pt->format,
1080                                   stImage->pt->target, 0,
1081                                   PIPE_BIND_RENDER_TARGET, 0)) {
1082      if (compress_with_blit(ctx, target, level,
1083                             xoffset, yoffset, zoffset,
1084                             width, height, depth,
1085                             format, type, pixels, packing, texImage)) {
1086         goto done;
1087      }
1088   }
1089
1090   /* Map buffer if necessary.  Need to lock to prevent other contexts
1091    * from uploading the buffer under us.
1092    */
1093   if (stImage->pt) {
1094      if (format == GL_DEPTH_COMPONENT &&
1095          util_format_is_depth_and_stencil(stImage->pt->format))
1096         transfer_usage = PIPE_TRANSFER_READ_WRITE;
1097      else
1098         transfer_usage = PIPE_TRANSFER_WRITE;
1099
1100      texImage->Data = st_texture_image_map(st, stImage, zoffset,
1101                                            transfer_usage,
1102                                            xoffset, yoffset,
1103                                            width, height);
1104   }
1105
1106   if (!texImage->Data) {
1107      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1108      goto done;
1109   }
1110
1111   src = (const GLubyte *) pixels;
1112   dstRowStride = stImage->transfer->stride;
1113
1114   for (i = 0; i < depth; i++) {
1115      if (!_mesa_texstore(ctx, dims, texImage->_BaseFormat,
1116                          texImage->TexFormat,
1117                          texImage->Data,
1118                          0, 0, 0,
1119                          dstRowStride,
1120                          texImage->ImageOffsets,
1121                          width, height, 1,
1122                          format, type, src, packing)) {
1123	 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1124      }
1125
1126      if (stImage->pt && i + 1 < depth) {
1127         /* unmap this slice */
1128	 st_texture_image_unmap(st, stImage);
1129         /* map next slice of 3D texture */
1130	 texImage->Data = st_texture_image_map(st, stImage,
1131                                               zoffset + i + 1,
1132                                               transfer_usage,
1133                                               xoffset, yoffset,
1134                                               width, height);
1135	 src += srcImageStride;
1136      }
1137   }
1138
1139done:
1140   _mesa_unmap_teximage_pbo(ctx, packing);
1141
1142   if (stImage->pt && texImage->Data) {
1143      st_texture_image_unmap(st, stImage);
1144      texImage->Data = NULL;
1145   }
1146}
1147
1148
1149
1150static void
1151st_TexSubImage3D(GLcontext *ctx, GLenum target, GLint level,
1152                 GLint xoffset, GLint yoffset, GLint zoffset,
1153                 GLsizei width, GLsizei height, GLsizei depth,
1154                 GLenum format, GLenum type, const GLvoid *pixels,
1155                 const struct gl_pixelstore_attrib *packing,
1156                 struct gl_texture_object *texObj,
1157                 struct gl_texture_image *texImage)
1158{
1159   st_TexSubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
1160                  width, height, depth, format, type,
1161                  pixels, packing, texObj, texImage);
1162}
1163
1164
1165static void
1166st_TexSubImage2D(GLcontext *ctx, GLenum target, GLint level,
1167                 GLint xoffset, GLint yoffset,
1168                 GLsizei width, GLsizei height,
1169                 GLenum format, GLenum type, const GLvoid * pixels,
1170                 const struct gl_pixelstore_attrib *packing,
1171                 struct gl_texture_object *texObj,
1172                 struct gl_texture_image *texImage)
1173{
1174   st_TexSubimage(ctx, 2, target, level, xoffset, yoffset, 0,
1175                  width, height, 1, format, type,
1176                  pixels, packing, texObj, texImage);
1177}
1178
1179
1180static void
1181st_TexSubImage1D(GLcontext *ctx, GLenum target, GLint level,
1182                 GLint xoffset, GLsizei width, GLenum format, GLenum type,
1183                 const GLvoid * pixels,
1184                 const struct gl_pixelstore_attrib *packing,
1185                 struct gl_texture_object *texObj,
1186                 struct gl_texture_image *texImage)
1187{
1188   st_TexSubimage(ctx, 1, target, level, xoffset, 0, 0, width, 1, 1,
1189                  format, type, pixels, packing, texObj, texImage);
1190}
1191
1192
1193static void
1194st_CompressedTexSubImage1D(GLcontext *ctx, GLenum target, GLint level,
1195                           GLint xoffset, GLsizei width,
1196                           GLenum format,
1197                           GLsizei imageSize, const GLvoid *data,
1198                           struct gl_texture_object *texObj,
1199                           struct gl_texture_image *texImage)
1200{
1201   assert(0);
1202}
1203
1204
1205static void
1206st_CompressedTexSubImage2D(GLcontext *ctx, GLenum target, GLint level,
1207                           GLint xoffset, GLint yoffset,
1208                           GLsizei width, GLint height,
1209                           GLenum format,
1210                           GLsizei imageSize, const GLvoid *data,
1211                           struct gl_texture_object *texObj,
1212                           struct gl_texture_image *texImage)
1213{
1214   struct st_context *st = st_context(ctx);
1215   struct st_texture_image *stImage = st_texture_image(texImage);
1216   int srcBlockStride;
1217   int dstBlockStride;
1218   int y;
1219   enum pipe_format pformat;
1220
1221   if (stImage->pt) {
1222      pformat = stImage->pt->format;
1223
1224      texImage->Data = st_texture_image_map(st, stImage, 0,
1225                                            PIPE_TRANSFER_WRITE,
1226                                            xoffset, yoffset,
1227                                            width, height);
1228
1229      srcBlockStride = util_format_get_stride(pformat, width);
1230      dstBlockStride = stImage->transfer->stride;
1231   } else {
1232      assert(stImage->pt);
1233      /* TODO find good values for block and strides */
1234      /* TODO also adjust texImage->data for yoffset/xoffset */
1235      return;
1236   }
1237
1238   if (!texImage->Data) {
1239      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexSubImage");
1240      return;
1241   }
1242
1243   assert(xoffset % util_format_get_blockwidth(pformat) == 0);
1244   assert(yoffset % util_format_get_blockheight(pformat) == 0);
1245   assert(width % util_format_get_blockwidth(pformat) == 0);
1246   assert(height % util_format_get_blockheight(pformat) == 0);
1247
1248   for (y = 0; y < height; y += util_format_get_blockheight(pformat)) {
1249      /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1250      const char *src = (const char*)data + srcBlockStride * util_format_get_nblocksy(pformat, y);
1251      char *dst = (char*)texImage->Data + dstBlockStride * util_format_get_nblocksy(pformat, y);
1252      memcpy(dst, src, util_format_get_stride(pformat, width));
1253   }
1254
1255   if (stImage->pt) {
1256      st_texture_image_unmap(st, stImage);
1257      texImage->Data = NULL;
1258   }
1259}
1260
1261
1262static void
1263st_CompressedTexSubImage3D(GLcontext *ctx, GLenum target, GLint level,
1264                           GLint xoffset, GLint yoffset, GLint zoffset,
1265                           GLsizei width, GLint height, GLint depth,
1266                           GLenum format,
1267                           GLsizei imageSize, const GLvoid *data,
1268                           struct gl_texture_object *texObj,
1269                           struct gl_texture_image *texImage)
1270{
1271   assert(0);
1272}
1273
1274
1275
1276/**
1277 * Do a CopyTexSubImage operation using a read transfer from the source,
1278 * a write transfer to the destination and get_tile()/put_tile() to access
1279 * the pixels/texels.
1280 *
1281 * Note: srcY=0=TOP of renderbuffer
1282 */
1283static void
1284fallback_copy_texsubimage(GLcontext *ctx, GLenum target, GLint level,
1285                          struct st_renderbuffer *strb,
1286                          struct st_texture_image *stImage,
1287                          GLenum baseFormat,
1288                          GLint destX, GLint destY, GLint destZ,
1289                          GLint srcX, GLint srcY,
1290                          GLsizei width, GLsizei height)
1291{
1292   struct st_context *st = st_context(ctx);
1293   struct pipe_context *pipe = st->pipe;
1294   struct pipe_transfer *src_trans;
1295   GLvoid *texDest;
1296   enum pipe_transfer_usage transfer_usage;
1297
1298   if (ST_DEBUG & DEBUG_FALLBACK)
1299      debug_printf("%s: fallback processing\n", __FUNCTION__);
1300
1301   assert(width <= MAX_WIDTH);
1302
1303   if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1304      srcY = strb->Base.Height - srcY - height;
1305   }
1306
1307   src_trans = pipe_get_transfer(st_context(ctx)->pipe,
1308					       strb->texture,
1309					       0, 0, 0,
1310					       PIPE_TRANSFER_READ,
1311					       srcX, srcY,
1312					       width, height);
1313
1314   if ((baseFormat == GL_DEPTH_COMPONENT ||
1315        baseFormat == GL_DEPTH_STENCIL) &&
1316       util_format_is_depth_and_stencil(stImage->pt->format))
1317      transfer_usage = PIPE_TRANSFER_READ_WRITE;
1318   else
1319      transfer_usage = PIPE_TRANSFER_WRITE;
1320
1321   texDest = st_texture_image_map(st, stImage, 0, transfer_usage,
1322                                  destX, destY, width, height);
1323
1324   if (baseFormat == GL_DEPTH_COMPONENT ||
1325       baseFormat == GL_DEPTH_STENCIL) {
1326      const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
1327                                     ctx->Pixel.DepthBias != 0.0F);
1328      GLint row, yStep;
1329
1330      /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1331      if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1332         srcY = height - 1;
1333         yStep = -1;
1334      }
1335      else {
1336         srcY = 0;
1337         yStep = 1;
1338      }
1339
1340      /* To avoid a large temp memory allocation, do copy row by row */
1341      for (row = 0; row < height; row++, srcY += yStep) {
1342         uint data[MAX_WIDTH];
1343         pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
1344         if (scaleOrBias) {
1345            _mesa_scale_and_bias_depth_uint(ctx, width, data);
1346         }
1347         pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
1348      }
1349   }
1350   else {
1351      /* RGBA format */
1352      GLfloat *tempSrc =
1353         (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1354
1355      if (tempSrc && texDest) {
1356         const GLint dims = 2;
1357         const GLint dstRowStride = stImage->transfer->stride;
1358         struct gl_texture_image *texImage = &stImage->base;
1359         struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
1360
1361         if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1362            unpack.Invert = GL_TRUE;
1363         }
1364
1365         /* get float/RGBA image from framebuffer */
1366         /* XXX this usually involves a lot of int/float conversion.
1367          * try to avoid that someday.
1368          */
1369         pipe_get_tile_rgba(pipe, src_trans, 0, 0, width, height, tempSrc);
1370
1371         /* Store into texture memory.
1372          * Note that this does some special things such as pixel transfer
1373          * ops and format conversion.  In particular, if the dest tex format
1374          * is actually RGBA but the user created the texture as GL_RGB we
1375          * need to fill-in/override the alpha channel with 1.0.
1376          */
1377         _mesa_texstore(ctx, dims,
1378                        texImage->_BaseFormat,
1379                        texImage->TexFormat,
1380                        texDest,
1381                        0, 0, 0,
1382                        dstRowStride,
1383                        texImage->ImageOffsets,
1384                        width, height, 1,
1385                        GL_RGBA, GL_FLOAT, tempSrc, /* src */
1386                        &unpack);
1387      }
1388      else {
1389         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1390      }
1391
1392      if (tempSrc)
1393         free(tempSrc);
1394   }
1395
1396   st_texture_image_unmap(st, stImage);
1397   pipe->transfer_destroy(pipe, src_trans);
1398}
1399
1400
1401
1402/**
1403 * If the format of the src renderbuffer and the format of the dest
1404 * texture are compatible (in terms of blitting), return a TGSI writemask
1405 * to be used during the blit.
1406 * If the src/dest are incompatible, return 0.
1407 */
1408static unsigned
1409compatible_src_dst_formats(GLcontext *ctx,
1410                           const struct gl_renderbuffer *src,
1411                           const struct gl_texture_image *dst)
1412{
1413   /* Get logical base formats for the src and dest.
1414    * That is, use the user-requested formats and not the actual, device-
1415    * chosen formats.
1416    * For example, the user may have requested an A8 texture but the
1417    * driver may actually be using an RGBA texture format.  When we
1418    * copy/blit to that texture, we only want to copy the Alpha channel
1419    * and not the RGB channels.
1420    *
1421    * Similarly, when the src FBO was created an RGB format may have been
1422    * requested but the driver actually chose an RGBA format.  In that case,
1423    * we don't want to copy the undefined Alpha channel to the dest texture
1424    * (it should be 1.0).
1425    */
1426   const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
1427   const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
1428
1429   /**
1430    * XXX when we have red-only and red/green renderbuffers we'll need
1431    * to add more cases here (or implement a general-purpose routine that
1432    * queries the existance of the R,G,B,A channels in the src and dest).
1433    */
1434   if (srcFormat == dstFormat) {
1435      /* This is the same as matching_base_formats, which should
1436       * always pass, as it did previously.
1437       */
1438      return TGSI_WRITEMASK_XYZW;
1439   }
1440   else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
1441      /* Make sure that A in the dest is 1.  The actual src format
1442       * may be RGBA and have undefined A values.
1443       */
1444      return TGSI_WRITEMASK_XYZ;
1445   }
1446   else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
1447      /* Make sure that A in the dest is 1.  The actual dst format
1448       * may be RGBA and will need A=1 to provide proper alpha values
1449       * when sampled later.
1450       */
1451      return TGSI_WRITEMASK_XYZ;
1452   }
1453   else {
1454      if (ST_DEBUG & DEBUG_FALLBACK)
1455         debug_printf("%s failed for src %s, dst %s\n",
1456                      __FUNCTION__,
1457                      _mesa_lookup_enum_by_nr(srcFormat),
1458                      _mesa_lookup_enum_by_nr(dstFormat));
1459
1460      /* Otherwise fail.
1461       */
1462      return 0;
1463   }
1464}
1465
1466
1467
1468/**
1469 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1470 * Note that the region to copy has already been clipped so we know we
1471 * won't read from outside the source renderbuffer's bounds.
1472 *
1473 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1474 */
1475static void
1476st_copy_texsubimage(GLcontext *ctx,
1477                    GLenum target, GLint level,
1478                    GLint destX, GLint destY, GLint destZ,
1479                    GLint srcX, GLint srcY,
1480                    GLsizei width, GLsizei height)
1481{
1482   struct gl_texture_unit *texUnit =
1483      &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1484   struct gl_texture_object *texObj =
1485      _mesa_select_tex_object(ctx, texUnit, target);
1486   struct gl_texture_image *texImage =
1487      _mesa_select_tex_image(ctx, texObj, target, level);
1488   struct st_texture_image *stImage = st_texture_image(texImage);
1489   const GLenum texBaseFormat = texImage->_BaseFormat;
1490   struct gl_framebuffer *fb = ctx->ReadBuffer;
1491   struct st_renderbuffer *strb;
1492   struct st_context *st = st_context(ctx);
1493   struct pipe_context *pipe = st->pipe;
1494   struct pipe_screen *screen = pipe->screen;
1495   enum pipe_format dest_format, src_format;
1496   GLboolean use_fallback = GL_TRUE;
1497   GLboolean matching_base_formats;
1498   GLuint format_writemask, sample_count;
1499   struct pipe_surface *dest_surface = NULL;
1500   GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1501
1502   /* make sure finalize_textures has been called?
1503    */
1504   if (0) st_validate_state(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   sample_count = strb->surface->texture->nr_samples;
1522   /* I believe this would be legal, presumably would need to do a resolve
1523      for color, and for depth/stencil spec says to just use one of the
1524      depth/stencil samples per pixel? Need some transfer clarifications. */
1525   assert(sample_count < 2);
1526
1527   if (srcX < 0) {
1528      width -= -srcX;
1529      destX += -srcX;
1530      srcX = 0;
1531   }
1532
1533   if (srcY < 0) {
1534      height -= -srcY;
1535      destY += -srcY;
1536      srcY = 0;
1537   }
1538
1539   if (destX < 0) {
1540      width -= -destX;
1541      srcX += -destX;
1542      destX = 0;
1543   }
1544
1545   if (destY < 0) {
1546      height -= -destY;
1547      srcY += -destY;
1548      destY = 0;
1549   }
1550
1551   if (width < 0 || height < 0)
1552      return;
1553
1554
1555   assert(strb);
1556   assert(strb->surface);
1557   assert(stImage->pt);
1558
1559   src_format = strb->surface->format;
1560   dest_format = stImage->pt->format;
1561
1562   /*
1563    * Determine if the src framebuffer and dest texture have the same
1564    * base format.  We need this to detect a case such as the framebuffer
1565    * being GL_RGBA but the texture being GL_RGB.  If the actual hardware
1566    * texture format stores RGBA we need to set A=1 (overriding the
1567    * framebuffer's alpha values).  We can't do that with the blit or
1568    * textured-quad paths.
1569    */
1570   matching_base_formats =
1571      (_mesa_get_format_base_format(strb->Base.Format) ==
1572       _mesa_get_format_base_format(texImage->TexFormat));
1573   format_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
1574
1575   if (ctx->_ImageTransferState == 0x0) {
1576
1577      if (matching_base_formats &&
1578          src_format == dest_format &&
1579          !do_flip)
1580      {
1581         /* use surface_copy() / blit */
1582         struct pipe_subresource subdst, subsrc;
1583         subdst.face = stImage->face;
1584         subdst.level = stImage->level;
1585         subsrc.face = strb->surface->face;
1586         subsrc.level = strb->surface->level;
1587
1588         /* for resource_copy_region(), y=0=top, always */
1589         pipe->resource_copy_region(pipe,
1590                                    /* dest */
1591                                    stImage->pt,
1592                                    subdst,
1593                                    destX, destY, destZ,
1594                                    /* src */
1595                                    strb->texture,
1596                                    subsrc,
1597                                    srcX, srcY, strb->surface->zslice,
1598                                    /* size */
1599                                    width, height);
1600         use_fallback = GL_FALSE;
1601      }
1602      else if (format_writemask &&
1603               texBaseFormat != GL_DEPTH_COMPONENT &&
1604               texBaseFormat != GL_DEPTH_STENCIL &&
1605               screen->is_format_supported(screen, src_format,
1606                                           PIPE_TEXTURE_2D, sample_count,
1607                                           PIPE_BIND_SAMPLER_VIEW,
1608                                           0) &&
1609               screen->is_format_supported(screen, dest_format,
1610                                           PIPE_TEXTURE_2D, 0,
1611                                           PIPE_BIND_RENDER_TARGET,
1612                                           0)) {
1613         /* draw textured quad to do the copy */
1614         GLint srcY0, srcY1;
1615         struct pipe_subresource subsrc;
1616
1617         dest_surface = screen->get_tex_surface(screen, stImage->pt,
1618                                                stImage->face, stImage->level,
1619                                                destZ,
1620                                                PIPE_BIND_RENDER_TARGET);
1621
1622         if (do_flip) {
1623            srcY1 = strb->Base.Height - srcY - height;
1624            srcY0 = srcY1 + height;
1625         }
1626         else {
1627            srcY0 = srcY;
1628            srcY1 = srcY0 + height;
1629         }
1630         subsrc.face = strb->surface->face;
1631         subsrc.level = strb->surface->level;
1632
1633         util_blit_pixels_writemask(st->blit,
1634                                    strb->texture,
1635                                    subsrc,
1636                                    srcX, srcY0,
1637                                    srcX + width, srcY1,
1638                                    strb->surface->zslice,
1639                                    dest_surface,
1640                                    destX, destY,
1641                                    destX + width, destY + height,
1642                                    0.0, PIPE_TEX_MIPFILTER_NEAREST,
1643                                    format_writemask);
1644         use_fallback = GL_FALSE;
1645      }
1646
1647      if (dest_surface)
1648         pipe_surface_reference(&dest_surface, NULL);
1649   }
1650
1651   if (use_fallback) {
1652      /* software fallback */
1653      fallback_copy_texsubimage(ctx, target, level,
1654                                strb, stImage, texBaseFormat,
1655                                destX, destY, destZ,
1656                                srcX, srcY, width, height);
1657   }
1658}
1659
1660
1661
1662static void
1663st_CopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
1664                  GLenum internalFormat,
1665                  GLint x, GLint y, GLsizei width, GLint border)
1666{
1667   struct gl_texture_unit *texUnit =
1668      &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1669   struct gl_texture_object *texObj =
1670      _mesa_select_tex_object(ctx, texUnit, target);
1671   struct gl_texture_image *texImage =
1672      _mesa_select_tex_image(ctx, texObj, target, level);
1673
1674   /* Setup or redefine the texture object, texture and texture
1675    * image.  Don't populate yet.
1676    */
1677   ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
1678                          width, border,
1679                          GL_RGBA, CHAN_TYPE, NULL,
1680                          &ctx->DefaultPacking, texObj, texImage);
1681
1682   st_copy_texsubimage(ctx, target, level,
1683                       0, 0, 0,  /* destX,Y,Z */
1684                       x, y, width, 1);  /* src X, Y, size */
1685}
1686
1687
1688static void
1689st_CopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
1690                  GLenum internalFormat,
1691                  GLint x, GLint y, GLsizei width, GLsizei height,
1692                  GLint border)
1693{
1694   struct gl_texture_unit *texUnit =
1695      &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1696   struct gl_texture_object *texObj =
1697      _mesa_select_tex_object(ctx, texUnit, target);
1698   struct gl_texture_image *texImage =
1699      _mesa_select_tex_image(ctx, texObj, target, level);
1700
1701   /* Setup or redefine the texture object, texture and texture
1702    * image.  Don't populate yet.
1703    */
1704   ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
1705                          width, height, border,
1706                          GL_RGBA, CHAN_TYPE, NULL,
1707                          &ctx->DefaultPacking, texObj, texImage);
1708
1709   st_copy_texsubimage(ctx, target, level,
1710                       0, 0, 0,  /* destX,Y,Z */
1711                       x, y, width, height);  /* src X, Y, size */
1712}
1713
1714
1715static void
1716st_CopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
1717                     GLint xoffset, GLint x, GLint y, GLsizei width)
1718{
1719   const GLint yoffset = 0, zoffset = 0;
1720   const GLsizei height = 1;
1721   st_copy_texsubimage(ctx, target, level,
1722                       xoffset, yoffset, zoffset,  /* destX,Y,Z */
1723                       x, y, width, height);  /* src X, Y, size */
1724}
1725
1726
1727static void
1728st_CopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
1729                     GLint xoffset, GLint yoffset,
1730                     GLint x, GLint y, GLsizei width, GLsizei height)
1731{
1732   const GLint zoffset = 0;
1733   st_copy_texsubimage(ctx, target, level,
1734                       xoffset, yoffset, zoffset,  /* destX,Y,Z */
1735                       x, y, width, height);  /* src X, Y, size */
1736}
1737
1738
1739static void
1740st_CopyTexSubImage3D(GLcontext * ctx, GLenum target, GLint level,
1741                     GLint xoffset, GLint yoffset, GLint zoffset,
1742                     GLint x, GLint y, GLsizei width, GLsizei height)
1743{
1744   st_copy_texsubimage(ctx, target, level,
1745                       xoffset, yoffset, zoffset,  /* destX,Y,Z */
1746                       x, y, width, height);  /* src X, Y, size */
1747}
1748
1749
1750/**
1751 * Copy image data from stImage into the texture object 'stObj' at level
1752 * 'dstLevel'.
1753 */
1754static void
1755copy_image_data_to_texture(struct st_context *st,
1756			   struct st_texture_object *stObj,
1757                           GLuint dstLevel,
1758			   struct st_texture_image *stImage)
1759{
1760   /* debug checks */
1761   {
1762      const struct gl_texture_image *dstImage =
1763         stObj->base.Image[stImage->face][stImage->level];
1764      assert(dstImage);
1765      assert(dstImage->Width == stImage->base.Width);
1766      assert(dstImage->Height == stImage->base.Height);
1767      assert(dstImage->Depth == stImage->base.Depth);
1768   }
1769
1770   if (stImage->pt) {
1771      /* Copy potentially with the blitter:
1772       */
1773      st_texture_image_copy(st->pipe,
1774                            stObj->pt, dstLevel,  /* dest texture, level */
1775                            stImage->pt, stImage->level, /* src texture, level */
1776                            stImage->face);
1777
1778      pipe_resource_reference(&stImage->pt, NULL);
1779   }
1780   else if (stImage->base.Data) {
1781      st_texture_image_data(st,
1782                            stObj->pt,
1783                            stImage->face,
1784                            dstLevel,
1785                            stImage->base.Data,
1786                            stImage->base.RowStride *
1787                            util_format_get_blocksize(stObj->pt->format),
1788                            stImage->base.RowStride *
1789                            stImage->base.Height *
1790                            util_format_get_blocksize(stObj->pt->format));
1791      _mesa_align_free(stImage->base.Data);
1792      stImage->base.Data = NULL;
1793   }
1794
1795   pipe_resource_reference(&stImage->pt, stObj->pt);
1796}
1797
1798
1799/**
1800 * Called during state validation.  When this function is finished,
1801 * the texture object should be ready for rendering.
1802 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1803 */
1804GLboolean
1805st_finalize_texture(GLcontext *ctx,
1806		    struct pipe_context *pipe,
1807		    struct gl_texture_object *tObj)
1808{
1809   struct st_context *st = st_context(ctx);
1810   struct st_texture_object *stObj = st_texture_object(tObj);
1811   const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1812   GLuint face;
1813   struct st_texture_image *firstImage;
1814   enum pipe_format firstImageFormat;
1815
1816   if (stObj->base._Complete) {
1817      /* The texture is complete and we know exactly how many mipmap levels
1818       * are present/needed.  This is conditional because we may be called
1819       * from the st_generate_mipmap() function when the texture object is
1820       * incomplete.  In that case, we'll have set stObj->lastLevel before
1821       * we get here.
1822       */
1823      if (stObj->base.MinFilter == GL_LINEAR ||
1824          stObj->base.MinFilter == GL_NEAREST)
1825         stObj->lastLevel = stObj->base.BaseLevel;
1826      else
1827         stObj->lastLevel = stObj->base._MaxLevel;
1828   }
1829
1830   firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1831   assert(firstImage);
1832
1833   /* If both firstImage and stObj point to a texture which can contain
1834    * all active images, favour firstImage.  Note that because of the
1835    * completeness requirement, we know that the image dimensions
1836    * will match.
1837    */
1838   if (firstImage->pt &&
1839       firstImage->pt != stObj->pt &&
1840       firstImage->pt->last_level >= stObj->lastLevel) {
1841      pipe_resource_reference(&stObj->pt, firstImage->pt);
1842      pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1843   }
1844
1845   /* Find gallium format for the Mesa texture */
1846   firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1847
1848   /* If we already have a gallium texture, check that it matches the texture
1849    * object's format, target, size, num_levels, etc.
1850    */
1851   if (stObj->pt) {
1852      if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1853          stObj->pt->format != firstImageFormat ||
1854          stObj->pt->last_level < stObj->lastLevel ||
1855          stObj->pt->width0 != stObj->width0 ||
1856          stObj->pt->height0 != stObj->height0 ||
1857          stObj->pt->depth0 != stObj->depth0)
1858      {
1859         /* The gallium texture does not match the Mesa texture so delete the
1860          * gallium texture now.  We'll make a new one below.
1861          */
1862         pipe_resource_reference(&stObj->pt, NULL);
1863         pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1864         st->dirty.st |= ST_NEW_FRAMEBUFFER;
1865      }
1866   }
1867
1868   /* May need to create a new gallium texture:
1869    */
1870   if (!stObj->pt) {
1871      GLuint bindings = default_bindings(st, firstImageFormat);
1872
1873      stObj->pt = st_texture_create(st,
1874                                    gl_target_to_pipe(stObj->base.Target),
1875                                    firstImageFormat,
1876                                    stObj->lastLevel,
1877                                    stObj->width0,
1878                                    stObj->height0,
1879                                    stObj->depth0,
1880                                    bindings);
1881
1882      if (!stObj->pt) {
1883         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1884         return GL_FALSE;
1885      }
1886   }
1887
1888   /* Pull in any images not in the object's texture:
1889    */
1890   for (face = 0; face < nr_faces; face++) {
1891      GLuint level;
1892      for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1893         struct st_texture_image *stImage =
1894            st_texture_image(stObj->base.Image[face][level]);
1895
1896         /* Need to import images in main memory or held in other textures.
1897          */
1898         if (stImage && stObj->pt != stImage->pt) {
1899            copy_image_data_to_texture(st, stObj, level, stImage);
1900         }
1901      }
1902   }
1903
1904   return GL_TRUE;
1905}
1906
1907
1908/**
1909 * Returns pointer to a default/dummy texture.
1910 * This is typically used when the current shader has tex/sample instructions
1911 * but the user has not provided a (any) texture(s).
1912 */
1913struct gl_texture_object *
1914st_get_default_texture(struct st_context *st)
1915{
1916   if (!st->default_texture) {
1917      static const GLenum target = GL_TEXTURE_2D;
1918      GLubyte pixels[16][16][4];
1919      struct gl_texture_object *texObj;
1920      struct gl_texture_image *texImg;
1921      GLuint i, j;
1922
1923      /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1924       * when attempting to sample incomplete textures.
1925       */
1926      for (i = 0; i < 16; i++) {
1927         for (j = 0; j < 16; j++) {
1928            pixels[i][j][0] = 0;
1929            pixels[i][j][1] = 0;
1930            pixels[i][j][2] = 0;
1931            pixels[i][j][3] = 255;
1932         }
1933      }
1934
1935      texObj = st->ctx->Driver.NewTextureObject(st->ctx, 0, target);
1936
1937      texImg = _mesa_get_tex_image(st->ctx, texObj, target, 0);
1938
1939      _mesa_init_teximage_fields(st->ctx, target, texImg,
1940                                 16, 16, 1, 0,  /* w, h, d, border */
1941                                 GL_RGBA);
1942
1943      st_TexImage(st->ctx, 2, target,
1944                  0, GL_RGBA,    /* level, intformat */
1945                  16, 16, 1, 0,  /* w, h, d, border */
1946                  GL_RGBA, GL_UNSIGNED_BYTE, pixels,
1947                  &st->ctx->DefaultPacking,
1948                  texObj, texImg,
1949                  0, 0);
1950
1951      texObj->MinFilter = GL_NEAREST;
1952      texObj->MagFilter = GL_NEAREST;
1953      texObj->_Complete = GL_TRUE;
1954
1955      st->default_texture = texObj;
1956   }
1957   return st->default_texture;
1958}
1959
1960
1961void
1962st_init_texture_functions(struct dd_function_table *functions)
1963{
1964   functions->ChooseTextureFormat = st_ChooseTextureFormat;
1965   functions->TexImage1D = st_TexImage1D;
1966   functions->TexImage2D = st_TexImage2D;
1967   functions->TexImage3D = st_TexImage3D;
1968   functions->TexSubImage1D = st_TexSubImage1D;
1969   functions->TexSubImage2D = st_TexSubImage2D;
1970   functions->TexSubImage3D = st_TexSubImage3D;
1971   functions->CompressedTexSubImage1D = st_CompressedTexSubImage1D;
1972   functions->CompressedTexSubImage2D = st_CompressedTexSubImage2D;
1973   functions->CompressedTexSubImage3D = st_CompressedTexSubImage3D;
1974   functions->CopyTexImage1D = st_CopyTexImage1D;
1975   functions->CopyTexImage2D = st_CopyTexImage2D;
1976   functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
1977   functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
1978   functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
1979   functions->GenerateMipmap = st_generate_mipmap;
1980
1981   functions->GetTexImage = st_GetTexImage;
1982
1983   /* compressed texture functions */
1984   functions->CompressedTexImage2D = st_CompressedTexImage2D;
1985   functions->GetCompressedTexImage = st_GetCompressedTexImage;
1986
1987   functions->NewTextureObject = st_NewTextureObject;
1988   functions->NewTextureImage = st_NewTextureImage;
1989   functions->DeleteTexture = st_DeleteTextureObject;
1990   functions->FreeTexImageData = st_FreeTextureImageData;
1991   functions->UpdateTexturePalette = 0;
1992
1993   functions->TextureMemCpy = do_memcpy;
1994
1995   /* XXX Temporary until we can query pipe's texture sizes */
1996   functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1997}
1998