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