st_cb_texture.c revision 30f46e7b4c9d6c6f8c1c01825b344b90adc93982
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.DeleteTextureObject() */
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
1246   for (y = 0; y < height; y += util_format_get_blockheight(pformat)) {
1247      /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1248      const char *src = (const char*)data + srcBlockStride * util_format_get_nblocksy(pformat, y);
1249      char *dst = (char*)texImage->Data + dstBlockStride * util_format_get_nblocksy(pformat, y);
1250      memcpy(dst, src, util_format_get_stride(pformat, width));
1251   }
1252
1253   if (stImage->pt) {
1254      st_texture_image_unmap(st, stImage);
1255      texImage->Data = NULL;
1256   }
1257}
1258
1259
1260static void
1261st_CompressedTexSubImage3D(GLcontext *ctx, GLenum target, GLint level,
1262                           GLint xoffset, GLint yoffset, GLint zoffset,
1263                           GLsizei width, GLint height, GLint depth,
1264                           GLenum format,
1265                           GLsizei imageSize, const GLvoid *data,
1266                           struct gl_texture_object *texObj,
1267                           struct gl_texture_image *texImage)
1268{
1269   assert(0);
1270}
1271
1272
1273
1274/**
1275 * Do a CopyTexSubImage operation using a read transfer from the source,
1276 * a write transfer to the destination and get_tile()/put_tile() to access
1277 * the pixels/texels.
1278 *
1279 * Note: srcY=0=TOP of renderbuffer
1280 */
1281static void
1282fallback_copy_texsubimage(GLcontext *ctx, GLenum target, GLint level,
1283                          struct st_renderbuffer *strb,
1284                          struct st_texture_image *stImage,
1285                          GLenum baseFormat,
1286                          GLint destX, GLint destY, GLint destZ,
1287                          GLint srcX, GLint srcY,
1288                          GLsizei width, GLsizei height)
1289{
1290   struct st_context *st = st_context(ctx);
1291   struct pipe_context *pipe = st->pipe;
1292   struct pipe_transfer *src_trans;
1293   GLvoid *texDest;
1294   enum pipe_transfer_usage transfer_usage;
1295
1296   if (ST_DEBUG & DEBUG_FALLBACK)
1297      debug_printf("%s: fallback processing\n", __FUNCTION__);
1298
1299   assert(width <= MAX_WIDTH);
1300
1301   if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1302      srcY = strb->Base.Height - srcY - height;
1303   }
1304
1305   src_trans = pipe_get_transfer(st_context(ctx)->pipe,
1306					       strb->texture,
1307					       0, 0, 0,
1308					       PIPE_TRANSFER_READ,
1309					       srcX, srcY,
1310					       width, height);
1311
1312   if ((baseFormat == GL_DEPTH_COMPONENT ||
1313        baseFormat == GL_DEPTH_STENCIL) &&
1314       util_format_is_depth_and_stencil(stImage->pt->format))
1315      transfer_usage = PIPE_TRANSFER_READ_WRITE;
1316   else
1317      transfer_usage = PIPE_TRANSFER_WRITE;
1318
1319   texDest = st_texture_image_map(st, stImage, 0, transfer_usage,
1320                                  destX, destY, width, height);
1321
1322   if (baseFormat == GL_DEPTH_COMPONENT ||
1323       baseFormat == GL_DEPTH_STENCIL) {
1324      const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
1325                                     ctx->Pixel.DepthBias != 0.0F);
1326      GLint row, yStep;
1327
1328      /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1329      if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1330         srcY = height - 1;
1331         yStep = -1;
1332      }
1333      else {
1334         srcY = 0;
1335         yStep = 1;
1336      }
1337
1338      /* To avoid a large temp memory allocation, do copy row by row */
1339      for (row = 0; row < height; row++, srcY += yStep) {
1340         uint data[MAX_WIDTH];
1341         pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
1342         if (scaleOrBias) {
1343            _mesa_scale_and_bias_depth_uint(ctx, width, data);
1344         }
1345         pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
1346      }
1347   }
1348   else {
1349      /* RGBA format */
1350      GLfloat *tempSrc =
1351         (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1352
1353      if (tempSrc && texDest) {
1354         const GLint dims = 2;
1355         const GLint dstRowStride = stImage->transfer->stride;
1356         struct gl_texture_image *texImage = &stImage->base;
1357         struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
1358
1359         if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1360            unpack.Invert = GL_TRUE;
1361         }
1362
1363         /* get float/RGBA image from framebuffer */
1364         /* XXX this usually involves a lot of int/float conversion.
1365          * try to avoid that someday.
1366          */
1367         pipe_get_tile_rgba(pipe, src_trans, 0, 0, width, height, tempSrc);
1368
1369         /* Store into texture memory.
1370          * Note that this does some special things such as pixel transfer
1371          * ops and format conversion.  In particular, if the dest tex format
1372          * is actually RGBA but the user created the texture as GL_RGB we
1373          * need to fill-in/override the alpha channel with 1.0.
1374          */
1375         _mesa_texstore(ctx, dims,
1376                        texImage->_BaseFormat,
1377                        texImage->TexFormat,
1378                        texDest,
1379                        0, 0, 0,
1380                        dstRowStride,
1381                        texImage->ImageOffsets,
1382                        width, height, 1,
1383                        GL_RGBA, GL_FLOAT, tempSrc, /* src */
1384                        &unpack);
1385      }
1386      else {
1387         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1388      }
1389
1390      if (tempSrc)
1391         free(tempSrc);
1392   }
1393
1394   st_texture_image_unmap(st, stImage);
1395   pipe->transfer_destroy(pipe, src_trans);
1396}
1397
1398
1399
1400/**
1401 * If the format of the src renderbuffer and the format of the dest
1402 * texture are compatible (in terms of blitting), return a TGSI writemask
1403 * to be used during the blit.
1404 * If the src/dest are incompatible, return 0.
1405 */
1406static unsigned
1407compatible_src_dst_formats(GLcontext *ctx,
1408                           const struct gl_renderbuffer *src,
1409                           const struct gl_texture_image *dst)
1410{
1411   /* Get logical base formats for the src and dest.
1412    * That is, use the user-requested formats and not the actual, device-
1413    * chosen formats.
1414    * For example, the user may have requested an A8 texture but the
1415    * driver may actually be using an RGBA texture format.  When we
1416    * copy/blit to that texture, we only want to copy the Alpha channel
1417    * and not the RGB channels.
1418    *
1419    * Similarly, when the src FBO was created an RGB format may have been
1420    * requested but the driver actually chose an RGBA format.  In that case,
1421    * we don't want to copy the undefined Alpha channel to the dest texture
1422    * (it should be 1.0).
1423    */
1424   const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
1425   const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
1426
1427   /**
1428    * XXX when we have red-only and red/green renderbuffers we'll need
1429    * to add more cases here (or implement a general-purpose routine that
1430    * queries the existance of the R,G,B,A channels in the src and dest).
1431    */
1432   if (srcFormat == dstFormat) {
1433      /* This is the same as matching_base_formats, which should
1434       * always pass, as it did previously.
1435       */
1436      return TGSI_WRITEMASK_XYZW;
1437   }
1438   else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
1439      /* Make sure that A in the dest is 1.  The actual src format
1440       * may be RGBA and have undefined A values.
1441       */
1442      return TGSI_WRITEMASK_XYZ;
1443   }
1444   else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
1445      /* Make sure that A in the dest is 1.  The actual dst format
1446       * may be RGBA and will need A=1 to provide proper alpha values
1447       * when sampled later.
1448       */
1449      return TGSI_WRITEMASK_XYZ;
1450   }
1451   else {
1452      if (ST_DEBUG & DEBUG_FALLBACK)
1453         debug_printf("%s failed for src %s, dst %s\n",
1454                      __FUNCTION__,
1455                      _mesa_lookup_enum_by_nr(srcFormat),
1456                      _mesa_lookup_enum_by_nr(dstFormat));
1457
1458      /* Otherwise fail.
1459       */
1460      return 0;
1461   }
1462}
1463
1464
1465
1466/**
1467 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1468 * Note that the region to copy has already been clipped so we know we
1469 * won't read from outside the source renderbuffer's bounds.
1470 *
1471 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1472 */
1473static void
1474st_copy_texsubimage(GLcontext *ctx,
1475                    GLenum target, GLint level,
1476                    GLint destX, GLint destY, GLint destZ,
1477                    GLint srcX, GLint srcY,
1478                    GLsizei width, GLsizei height)
1479{
1480   struct gl_texture_unit *texUnit =
1481      &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1482   struct gl_texture_object *texObj =
1483      _mesa_select_tex_object(ctx, texUnit, target);
1484   struct gl_texture_image *texImage =
1485      _mesa_select_tex_image(ctx, texObj, target, level);
1486   struct st_texture_image *stImage = st_texture_image(texImage);
1487   const GLenum texBaseFormat = texImage->_BaseFormat;
1488   struct gl_framebuffer *fb = ctx->ReadBuffer;
1489   struct st_renderbuffer *strb;
1490   struct st_context *st = st_context(ctx);
1491   struct pipe_context *pipe = st->pipe;
1492   struct pipe_screen *screen = pipe->screen;
1493   enum pipe_format dest_format, src_format;
1494   GLboolean use_fallback = GL_TRUE;
1495   GLboolean matching_base_formats;
1496   GLuint format_writemask, sample_count;
1497   struct pipe_surface *dest_surface = NULL;
1498   GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1499
1500   /* make sure finalize_textures has been called?
1501    */
1502   if (0) st_validate_state(st);
1503
1504   /* determine if copying depth or color data */
1505   if (texBaseFormat == GL_DEPTH_COMPONENT ||
1506       texBaseFormat == GL_DEPTH_STENCIL) {
1507      strb = st_renderbuffer(fb->_DepthBuffer);
1508   }
1509   else {
1510      /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1511      strb = st_renderbuffer(fb->_ColorReadBuffer);
1512   }
1513
1514   if (!strb || !strb->surface || !stImage->pt) {
1515      debug_printf("%s: null strb or stImage\n", __FUNCTION__);
1516      return;
1517   }
1518
1519   sample_count = strb->surface->texture->nr_samples;
1520   /* I believe this would be legal, presumably would need to do a resolve
1521      for color, and for depth/stencil spec says to just use one of the
1522      depth/stencil samples per pixel? Need some transfer clarifications. */
1523   assert(sample_count < 2);
1524
1525   if (srcX < 0) {
1526      width -= -srcX;
1527      destX += -srcX;
1528      srcX = 0;
1529   }
1530
1531   if (srcY < 0) {
1532      height -= -srcY;
1533      destY += -srcY;
1534      srcY = 0;
1535   }
1536
1537   if (destX < 0) {
1538      width -= -destX;
1539      srcX += -destX;
1540      destX = 0;
1541   }
1542
1543   if (destY < 0) {
1544      height -= -destY;
1545      srcY += -destY;
1546      destY = 0;
1547   }
1548
1549   if (width < 0 || height < 0)
1550      return;
1551
1552
1553   assert(strb);
1554   assert(strb->surface);
1555   assert(stImage->pt);
1556
1557   src_format = strb->surface->format;
1558   dest_format = stImage->pt->format;
1559
1560   /*
1561    * Determine if the src framebuffer and dest texture have the same
1562    * base format.  We need this to detect a case such as the framebuffer
1563    * being GL_RGBA but the texture being GL_RGB.  If the actual hardware
1564    * texture format stores RGBA we need to set A=1 (overriding the
1565    * framebuffer's alpha values).  We can't do that with the blit or
1566    * textured-quad paths.
1567    */
1568   matching_base_formats =
1569      (_mesa_get_format_base_format(strb->Base.Format) ==
1570       _mesa_get_format_base_format(texImage->TexFormat));
1571   format_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
1572
1573   if (ctx->_ImageTransferState == 0x0) {
1574
1575      if (matching_base_formats &&
1576          src_format == dest_format &&
1577          !do_flip)
1578      {
1579         /* use surface_copy() / blit */
1580         struct pipe_subresource subdst, subsrc;
1581         subdst.face = stImage->face;
1582         subdst.level = stImage->level;
1583         subsrc.face = strb->surface->face;
1584         subsrc.level = strb->surface->level;
1585
1586         /* for resource_copy_region(), y=0=top, always */
1587         pipe->resource_copy_region(pipe,
1588                                    /* dest */
1589                                    stImage->pt,
1590                                    subdst,
1591                                    destX, destY, destZ,
1592                                    /* src */
1593                                    strb->texture,
1594                                    subsrc,
1595                                    srcX, srcY, strb->surface->zslice,
1596                                    /* size */
1597                                    width, height);
1598         use_fallback = GL_FALSE;
1599      }
1600      else if (format_writemask &&
1601               texBaseFormat != GL_DEPTH_COMPONENT &&
1602               texBaseFormat != GL_DEPTH_STENCIL &&
1603               screen->is_format_supported(screen, src_format,
1604                                           PIPE_TEXTURE_2D, sample_count,
1605                                           PIPE_BIND_SAMPLER_VIEW,
1606                                           0) &&
1607               screen->is_format_supported(screen, dest_format,
1608                                           PIPE_TEXTURE_2D, 0,
1609                                           PIPE_BIND_RENDER_TARGET,
1610                                           0)) {
1611         /* draw textured quad to do the copy */
1612         GLint srcY0, srcY1;
1613         struct pipe_subresource subsrc;
1614
1615         dest_surface = screen->get_tex_surface(screen, stImage->pt,
1616                                                stImage->face, stImage->level,
1617                                                destZ,
1618                                                PIPE_BIND_RENDER_TARGET);
1619
1620         if (do_flip) {
1621            srcY1 = strb->Base.Height - srcY - height;
1622            srcY0 = srcY1 + height;
1623         }
1624         else {
1625            srcY0 = srcY;
1626            srcY1 = srcY0 + height;
1627         }
1628         subsrc.face = strb->surface->face;
1629         subsrc.level = strb->surface->level;
1630
1631         util_blit_pixels_writemask(st->blit,
1632                                    strb->texture,
1633                                    subsrc,
1634                                    srcX, srcY0,
1635                                    srcX + width, srcY1,
1636                                    strb->surface->zslice,
1637                                    dest_surface,
1638                                    destX, destY,
1639                                    destX + width, destY + height,
1640                                    0.0, PIPE_TEX_MIPFILTER_NEAREST,
1641                                    format_writemask);
1642         use_fallback = GL_FALSE;
1643      }
1644
1645      if (dest_surface)
1646         pipe_surface_reference(&dest_surface, NULL);
1647   }
1648
1649   if (use_fallback) {
1650      /* software fallback */
1651      fallback_copy_texsubimage(ctx, target, level,
1652                                strb, stImage, texBaseFormat,
1653                                destX, destY, destZ,
1654                                srcX, srcY, width, height);
1655   }
1656}
1657
1658
1659
1660static void
1661st_CopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
1662                  GLenum internalFormat,
1663                  GLint x, GLint y, GLsizei width, GLint border)
1664{
1665   struct gl_texture_unit *texUnit =
1666      &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1667   struct gl_texture_object *texObj =
1668      _mesa_select_tex_object(ctx, texUnit, target);
1669   struct gl_texture_image *texImage =
1670      _mesa_select_tex_image(ctx, texObj, target, level);
1671
1672   /* Setup or redefine the texture object, texture and texture
1673    * image.  Don't populate yet.
1674    */
1675   ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
1676                          width, border,
1677                          GL_RGBA, CHAN_TYPE, NULL,
1678                          &ctx->DefaultPacking, texObj, texImage);
1679
1680   st_copy_texsubimage(ctx, target, level,
1681                       0, 0, 0,  /* destX,Y,Z */
1682                       x, y, width, 1);  /* src X, Y, size */
1683}
1684
1685
1686static void
1687st_CopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
1688                  GLenum internalFormat,
1689                  GLint x, GLint y, GLsizei width, GLsizei height,
1690                  GLint border)
1691{
1692   struct gl_texture_unit *texUnit =
1693      &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1694   struct gl_texture_object *texObj =
1695      _mesa_select_tex_object(ctx, texUnit, target);
1696   struct gl_texture_image *texImage =
1697      _mesa_select_tex_image(ctx, texObj, target, level);
1698
1699   /* Setup or redefine the texture object, texture and texture
1700    * image.  Don't populate yet.
1701    */
1702   ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
1703                          width, height, border,
1704                          GL_RGBA, CHAN_TYPE, NULL,
1705                          &ctx->DefaultPacking, texObj, texImage);
1706
1707   st_copy_texsubimage(ctx, target, level,
1708                       0, 0, 0,  /* destX,Y,Z */
1709                       x, y, width, height);  /* src X, Y, size */
1710}
1711
1712
1713static void
1714st_CopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
1715                     GLint xoffset, GLint x, GLint y, GLsizei width)
1716{
1717   const GLint yoffset = 0, zoffset = 0;
1718   const GLsizei height = 1;
1719   st_copy_texsubimage(ctx, target, level,
1720                       xoffset, yoffset, zoffset,  /* destX,Y,Z */
1721                       x, y, width, height);  /* src X, Y, size */
1722}
1723
1724
1725static void
1726st_CopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
1727                     GLint xoffset, GLint yoffset,
1728                     GLint x, GLint y, GLsizei width, GLsizei height)
1729{
1730   const GLint zoffset = 0;
1731   st_copy_texsubimage(ctx, target, level,
1732                       xoffset, yoffset, zoffset,  /* destX,Y,Z */
1733                       x, y, width, height);  /* src X, Y, size */
1734}
1735
1736
1737static void
1738st_CopyTexSubImage3D(GLcontext * ctx, GLenum target, GLint level,
1739                     GLint xoffset, GLint yoffset, GLint zoffset,
1740                     GLint x, GLint y, GLsizei width, GLsizei height)
1741{
1742   st_copy_texsubimage(ctx, target, level,
1743                       xoffset, yoffset, zoffset,  /* destX,Y,Z */
1744                       x, y, width, height);  /* src X, Y, size */
1745}
1746
1747
1748/**
1749 * Copy image data from stImage into the texture object 'stObj' at level
1750 * 'dstLevel'.
1751 */
1752static void
1753copy_image_data_to_texture(struct st_context *st,
1754			   struct st_texture_object *stObj,
1755                           GLuint dstLevel,
1756			   struct st_texture_image *stImage)
1757{
1758   /* debug checks */
1759   {
1760      const struct gl_texture_image *dstImage =
1761         stObj->base.Image[stImage->face][stImage->level];
1762      assert(dstImage);
1763      assert(dstImage->Width == stImage->base.Width);
1764      assert(dstImage->Height == stImage->base.Height);
1765      assert(dstImage->Depth == stImage->base.Depth);
1766   }
1767
1768   if (stImage->pt) {
1769      /* Copy potentially with the blitter:
1770       */
1771      st_texture_image_copy(st->pipe,
1772                            stObj->pt, dstLevel,  /* dest texture, level */
1773                            stImage->pt, stImage->level, /* src texture, level */
1774                            stImage->face);
1775
1776      pipe_resource_reference(&stImage->pt, NULL);
1777   }
1778   else if (stImage->base.Data) {
1779      st_texture_image_data(st,
1780                            stObj->pt,
1781                            stImage->face,
1782                            dstLevel,
1783                            stImage->base.Data,
1784                            stImage->base.RowStride *
1785                            util_format_get_blocksize(stObj->pt->format),
1786                            stImage->base.RowStride *
1787                            stImage->base.Height *
1788                            util_format_get_blocksize(stObj->pt->format));
1789      _mesa_align_free(stImage->base.Data);
1790      stImage->base.Data = NULL;
1791   }
1792
1793   pipe_resource_reference(&stImage->pt, stObj->pt);
1794}
1795
1796
1797/**
1798 * Called during state validation.  When this function is finished,
1799 * the texture object should be ready for rendering.
1800 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1801 */
1802GLboolean
1803st_finalize_texture(GLcontext *ctx,
1804		    struct pipe_context *pipe,
1805		    struct gl_texture_object *tObj)
1806{
1807   struct st_context *st = st_context(ctx);
1808   struct st_texture_object *stObj = st_texture_object(tObj);
1809   const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1810   GLuint face;
1811   struct st_texture_image *firstImage;
1812   enum pipe_format firstImageFormat;
1813
1814   if (stObj->base._Complete) {
1815      /* The texture is complete and we know exactly how many mipmap levels
1816       * are present/needed.  This is conditional because we may be called
1817       * from the st_generate_mipmap() function when the texture object is
1818       * incomplete.  In that case, we'll have set stObj->lastLevel before
1819       * we get here.
1820       */
1821      if (stObj->base.MinFilter == GL_LINEAR ||
1822          stObj->base.MinFilter == GL_NEAREST)
1823         stObj->lastLevel = stObj->base.BaseLevel;
1824      else
1825         stObj->lastLevel = stObj->base._MaxLevel;
1826   }
1827
1828   firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1829   assert(firstImage);
1830
1831   /* If both firstImage and stObj point to a texture which can contain
1832    * all active images, favour firstImage.  Note that because of the
1833    * completeness requirement, we know that the image dimensions
1834    * will match.
1835    */
1836   if (firstImage->pt &&
1837       firstImage->pt != stObj->pt &&
1838       firstImage->pt->last_level >= stObj->lastLevel) {
1839      pipe_resource_reference(&stObj->pt, firstImage->pt);
1840      pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1841   }
1842
1843   /* Find gallium format for the Mesa texture */
1844   firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1845
1846   /* If we already have a gallium texture, check that it matches the texture
1847    * object's format, target, size, num_levels, etc.
1848    */
1849   if (stObj->pt) {
1850      if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1851          !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
1852          stObj->pt->last_level < stObj->lastLevel ||
1853          stObj->pt->width0 != stObj->width0 ||
1854          stObj->pt->height0 != stObj->height0 ||
1855          stObj->pt->depth0 != stObj->depth0)
1856      {
1857         /* The gallium texture does not match the Mesa texture so delete the
1858          * gallium texture now.  We'll make a new one below.
1859          */
1860         pipe_resource_reference(&stObj->pt, NULL);
1861         pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1862         st->dirty.st |= ST_NEW_FRAMEBUFFER;
1863      }
1864   }
1865
1866   /* May need to create a new gallium texture:
1867    */
1868   if (!stObj->pt) {
1869      GLuint bindings = default_bindings(st, firstImageFormat);
1870
1871      stObj->pt = st_texture_create(st,
1872                                    gl_target_to_pipe(stObj->base.Target),
1873                                    firstImageFormat,
1874                                    stObj->lastLevel,
1875                                    stObj->width0,
1876                                    stObj->height0,
1877                                    stObj->depth0,
1878                                    bindings);
1879
1880      if (!stObj->pt) {
1881         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1882         return GL_FALSE;
1883      }
1884   }
1885
1886   /* Pull in any images not in the object's texture:
1887    */
1888   for (face = 0; face < nr_faces; face++) {
1889      GLuint level;
1890      for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1891         struct st_texture_image *stImage =
1892            st_texture_image(stObj->base.Image[face][level]);
1893
1894         /* Need to import images in main memory or held in other textures.
1895          */
1896         if (stImage && stObj->pt != stImage->pt) {
1897            copy_image_data_to_texture(st, stObj, level, stImage);
1898         }
1899      }
1900   }
1901
1902   return GL_TRUE;
1903}
1904
1905
1906/**
1907 * Returns pointer to a default/dummy texture.
1908 * This is typically used when the current shader has tex/sample instructions
1909 * but the user has not provided a (any) texture(s).
1910 */
1911struct gl_texture_object *
1912st_get_default_texture(struct st_context *st)
1913{
1914   if (!st->default_texture) {
1915      static const GLenum target = GL_TEXTURE_2D;
1916      GLubyte pixels[16][16][4];
1917      struct gl_texture_object *texObj;
1918      struct gl_texture_image *texImg;
1919      GLuint i, j;
1920
1921      /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1922       * when attempting to sample incomplete textures.
1923       */
1924      for (i = 0; i < 16; i++) {
1925         for (j = 0; j < 16; j++) {
1926            pixels[i][j][0] = 0;
1927            pixels[i][j][1] = 0;
1928            pixels[i][j][2] = 0;
1929            pixels[i][j][3] = 255;
1930         }
1931      }
1932
1933      texObj = st->ctx->Driver.NewTextureObject(st->ctx, 0, target);
1934
1935      texImg = _mesa_get_tex_image(st->ctx, texObj, target, 0);
1936
1937      _mesa_init_teximage_fields(st->ctx, target, texImg,
1938                                 16, 16, 1, 0,  /* w, h, d, border */
1939                                 GL_RGBA);
1940
1941      st_TexImage(st->ctx, 2, target,
1942                  0, GL_RGBA,    /* level, intformat */
1943                  16, 16, 1, 0,  /* w, h, d, border */
1944                  GL_RGBA, GL_UNSIGNED_BYTE, pixels,
1945                  &st->ctx->DefaultPacking,
1946                  texObj, texImg,
1947                  0, 0);
1948
1949      texObj->MinFilter = GL_NEAREST;
1950      texObj->MagFilter = GL_NEAREST;
1951      texObj->_Complete = GL_TRUE;
1952
1953      st->default_texture = texObj;
1954   }
1955   return st->default_texture;
1956}
1957
1958
1959void
1960st_init_texture_functions(struct dd_function_table *functions)
1961{
1962   functions->ChooseTextureFormat = st_ChooseTextureFormat;
1963   functions->TexImage1D = st_TexImage1D;
1964   functions->TexImage2D = st_TexImage2D;
1965   functions->TexImage3D = st_TexImage3D;
1966   functions->TexSubImage1D = st_TexSubImage1D;
1967   functions->TexSubImage2D = st_TexSubImage2D;
1968   functions->TexSubImage3D = st_TexSubImage3D;
1969   functions->CompressedTexSubImage1D = st_CompressedTexSubImage1D;
1970   functions->CompressedTexSubImage2D = st_CompressedTexSubImage2D;
1971   functions->CompressedTexSubImage3D = st_CompressedTexSubImage3D;
1972   functions->CopyTexImage1D = st_CopyTexImage1D;
1973   functions->CopyTexImage2D = st_CopyTexImage2D;
1974   functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
1975   functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
1976   functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
1977   functions->GenerateMipmap = st_generate_mipmap;
1978
1979   functions->GetTexImage = st_GetTexImage;
1980
1981   /* compressed texture functions */
1982   functions->CompressedTexImage2D = st_CompressedTexImage2D;
1983   functions->GetCompressedTexImage = st_GetCompressedTexImage;
1984
1985   functions->NewTextureObject = st_NewTextureObject;
1986   functions->NewTextureImage = st_NewTextureImage;
1987   functions->DeleteTexture = st_DeleteTextureObject;
1988   functions->FreeTexImageData = st_FreeTextureImageData;
1989   functions->UpdateTexturePalette = 0;
1990
1991   functions->TextureMemCpy = do_memcpy;
1992
1993   /* XXX Temporary until we can query pipe's texture sizes */
1994   functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1995}
1996