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