st_cb_texture.c revision 3ccbaa977f96eaa849093875dd0944f744ee1e21
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/imports.h"
29#include "main/convolve.h"
30#include "main/enums.h"
31#include "main/image.h"
32#include "main/macros.h"
33#include "main/mipmap.h"
34#include "main/texcompress.h"
35#include "main/texformat.h"
36#include "main/teximage.h"
37#include "main/texobj.h"
38#include "main/texstore.h"
39
40#include "state_tracker/st_context.h"
41#include "state_tracker/st_cb_fbo.h"
42#include "state_tracker/st_cb_texture.h"
43#include "state_tracker/st_format.h"
44#include "state_tracker/st_texture.h"
45#include "state_tracker/st_gen_mipmap.h"
46
47#include "pipe/p_context.h"
48#include "pipe/p_defines.h"
49#include "pipe/p_inlines.h"
50#include "util/p_tile.h"
51
52
53#define DBG if (0) printf
54
55
56static INLINE struct st_texture_image *
57st_texture_image(struct gl_texture_image *img)
58{
59   return (struct st_texture_image *) img;
60}
61
62
63static enum pipe_texture_target
64gl_target_to_pipe(GLenum target)
65{
66   switch (target) {
67   case GL_TEXTURE_1D:
68      return PIPE_TEXTURE_1D;
69
70   case GL_TEXTURE_2D:
71   case GL_TEXTURE_RECTANGLE_NV:
72      return PIPE_TEXTURE_2D;
73
74   case GL_TEXTURE_3D:
75      return PIPE_TEXTURE_3D;
76
77   case GL_TEXTURE_CUBE_MAP_ARB:
78      return PIPE_TEXTURE_CUBE;
79
80   default:
81      assert(0);
82      return 0;
83   }
84}
85
86
87/**
88 * Return nominal bytes per texel for a compressed format, 0 for non-compressed
89 * format.
90 */
91static int
92compressed_num_bytes(GLuint mesaFormat)
93{
94   switch(mesaFormat) {
95   case MESA_FORMAT_RGB_FXT1:
96   case MESA_FORMAT_RGBA_FXT1:
97   case MESA_FORMAT_RGB_DXT1:
98   case MESA_FORMAT_RGBA_DXT1:
99      return 2;
100   case MESA_FORMAT_RGBA_DXT3:
101   case MESA_FORMAT_RGBA_DXT5:
102      return 4;
103   default:
104      return 0;
105   }
106}
107
108
109static GLboolean
110st_IsTextureResident(GLcontext * ctx, struct gl_texture_object *texObj)
111{
112#if 0
113   struct intel_context *intel = intel_context(ctx);
114   struct st_texture_object *stObj = st_texture_object(texObj);
115
116   return
117      stObj->pt &&
118      stObj->pt->region &&
119      intel_is_region_resident(intel, stObj->pt->region);
120#endif
121   return 1;
122}
123
124
125static struct gl_texture_image *
126st_NewTextureImage(GLcontext * ctx)
127{
128   DBG("%s\n", __FUNCTION__);
129   (void) ctx;
130   return (struct gl_texture_image *) CALLOC_STRUCT(st_texture_image);
131}
132
133
134static struct gl_texture_object *
135st_NewTextureObject(GLcontext * ctx, GLuint name, GLenum target)
136{
137   struct st_texture_object *obj = CALLOC_STRUCT(st_texture_object);
138
139   DBG("%s\n", __FUNCTION__);
140   _mesa_initialize_texture_object(&obj->base, name, target);
141
142   return &obj->base;
143}
144
145static void
146st_DeleteTextureObject(GLcontext *ctx,
147			 struct gl_texture_object *texObj)
148{
149   struct st_texture_object *stObj = st_texture_object(texObj);
150
151   if (stObj->pt)
152      ctx->st->pipe->texture_release(ctx->st->pipe, &stObj->pt);
153
154   _mesa_delete_texture_object(ctx, texObj);
155}
156
157
158static void
159st_FreeTextureImageData(GLcontext * ctx, struct gl_texture_image *texImage)
160{
161   struct st_texture_image *stImage = st_texture_image(texImage);
162
163   DBG("%s\n", __FUNCTION__);
164
165   if (stImage->pt) {
166      ctx->st->pipe->texture_release(ctx->st->pipe, &stImage->pt);
167   }
168
169   if (texImage->Data) {
170      free(texImage->Data);
171      texImage->Data = NULL;
172   }
173}
174
175
176/* ================================================================
177 * From linux kernel i386 header files, copes with odd sizes better
178 * than COPY_DWORDS would:
179 * XXX Put this in src/mesa/main/imports.h ???
180 */
181#if defined(i386) || defined(__i386__)
182static INLINE void *
183__memcpy(void *to, const void *from, size_t n)
184{
185   int d0, d1, d2;
186   __asm__ __volatile__("rep ; movsl\n\t"
187                        "testb $2,%b4\n\t"
188                        "je 1f\n\t"
189                        "movsw\n"
190                        "1:\ttestb $1,%b4\n\t"
191                        "je 2f\n\t"
192                        "movsb\n" "2:":"=&c"(d0), "=&D"(d1), "=&S"(d2)
193                        :"0"(n / 4), "q"(n), "1"((long) to), "2"((long) from)
194                        :"memory");
195   return (to);
196}
197#else
198#define __memcpy(a,b,c) memcpy(a,b,c)
199#endif
200
201
202/* The system memcpy (at least on ubuntu 5.10) has problems copying
203 * to agp (writecombined) memory from a source which isn't 64-byte
204 * aligned - there is a 4x performance falloff.
205 *
206 * The x86 __memcpy is immune to this but is slightly slower
207 * (10%-ish) than the system memcpy.
208 *
209 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
210 * isn't much faster than x86_memcpy for agp copies.
211 *
212 * TODO: switch dynamically.
213 */
214static void *
215do_memcpy(void *dest, const void *src, size_t n)
216{
217   if ((((unsigned) src) & 63) || (((unsigned) dest) & 63)) {
218      return __memcpy(dest, src, n);
219   }
220   else
221      return memcpy(dest, src, n);
222}
223
224
225/* Functions to store texture images.  Where possible, textures
226 * will be created or further instantiated with image data, otherwise
227 * images will be stored in malloc'd memory.  A validation step is
228 * required to pull those images into a texture, or otherwise
229 * decide a fallback is required.
230 */
231
232
233static int
234logbase2(int n)
235{
236   GLint i = 1;
237   GLint log2 = 0;
238
239   while (n > i) {
240      i *= 2;
241      log2++;
242   }
243
244   return log2;
245}
246
247
248/**
249 * Allocate a pipe_texture object for the given st_texture_object using
250 * the given st_texture_image to guess the mipmap size/levels.
251 *
252 * [comments...]
253 * Otherwise, store it in memory if (Border != 0) or (any dimension ==
254 * 1).
255 *
256 * Otherwise, if max_level >= level >= min_level, create texture with
257 * space for images from min_level down to max_level.
258 *
259 * Otherwise, create texture with space for images from (level 0)..(1x1).
260 * Consider pruning this texture at a validation if the saving is worth it.
261 */
262static void
263guess_and_alloc_texture(struct st_context *st,
264			struct st_texture_object *stObj,
265			const struct st_texture_image *stImage)
266{
267   GLuint firstLevel;
268   GLuint lastLevel;
269   GLuint width = stImage->base.Width;
270   GLuint height = stImage->base.Height;
271   GLuint depth = stImage->base.Depth;
272   GLuint i, comp_byte = 0;
273
274   DBG("%s\n", __FUNCTION__);
275
276   assert(!stObj->pt);
277
278   if (stImage->base.Border)
279      return;
280
281   if (stImage->level > stObj->base.BaseLevel &&
282       (stImage->base.Width == 1 ||
283        (stObj->base.Target != GL_TEXTURE_1D &&
284         stImage->base.Height == 1) ||
285        (stObj->base.Target == GL_TEXTURE_3D &&
286         stImage->base.Depth == 1)))
287      return;
288
289   /* If this image disrespects BaseLevel, allocate from level zero.
290    * Usually BaseLevel == 0, so it's unlikely to happen.
291    */
292   if (stImage->level < stObj->base.BaseLevel)
293      firstLevel = 0;
294   else
295      firstLevel = stObj->base.BaseLevel;
296
297
298   /* Figure out image dimensions at start level.
299    */
300   for (i = stImage->level; i > firstLevel; i--) {
301      width <<= 1;
302      if (height != 1)
303         height <<= 1;
304      if (depth != 1)
305         depth <<= 1;
306   }
307
308   /* Guess a reasonable value for lastLevel.  This is probably going
309    * to be wrong fairly often and might mean that we have to look at
310    * resizable buffers, or require that buffers implement lazy
311    * pagetable arrangements.
312    */
313   if ((stObj->base.MinFilter == GL_NEAREST ||
314        stObj->base.MinFilter == GL_LINEAR) &&
315       stImage->level == firstLevel) {
316      lastLevel = firstLevel;
317   }
318   else {
319      GLuint l2width = logbase2(width);
320      GLuint l2height = logbase2(height);
321      GLuint l2depth = logbase2(depth);
322      lastLevel = firstLevel + MAX2(MAX2(l2width, l2height), l2depth);
323   }
324
325   if (stImage->base.IsCompressed)
326      comp_byte = compressed_num_bytes(stImage->base.TexFormat->MesaFormat);
327
328   stObj->pt = st_texture_create(st,
329                                 gl_target_to_pipe(stObj->base.Target),
330                                 st_mesa_format_to_pipe_format(stImage->base.TexFormat->MesaFormat),
331                                 lastLevel,
332                                 width,
333                                 height,
334                                 depth,
335                                 comp_byte);
336
337   DBG("%s - success\n", __FUNCTION__);
338}
339
340
341/* There are actually quite a few combinations this will work for,
342 * more than what I've listed here.
343 */
344static GLboolean
345check_pbo_format(GLint internalFormat,
346                 GLenum format, GLenum type,
347                 const struct gl_texture_format *mesa_format)
348{
349   switch (internalFormat) {
350   case 4:
351   case GL_RGBA:
352      return (format == GL_BGRA &&
353              (type == GL_UNSIGNED_BYTE ||
354               type == GL_UNSIGNED_INT_8_8_8_8_REV) &&
355              mesa_format == &_mesa_texformat_argb8888);
356   case 3:
357   case GL_RGB:
358      return (format == GL_RGB &&
359              type == GL_UNSIGNED_SHORT_5_6_5 &&
360              mesa_format == &_mesa_texformat_rgb565);
361   case GL_YCBCR_MESA:
362      return (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE);
363   default:
364      return GL_FALSE;
365   }
366}
367
368
369/* XXX: Do this for TexSubImage also:
370 */
371static GLboolean
372try_pbo_upload(GLcontext *ctx,
373               struct st_texture_image *stImage,
374               const struct gl_pixelstore_attrib *unpack,
375               GLint internalFormat,
376               GLint width, GLint height,
377               GLenum format, GLenum type, const void *pixels)
378{
379   return GL_FALSE;  /* XXX fix flushing/locking/blitting below */
380#if 000
381   struct intel_context *intel = intel_context(ctx);
382   struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
383   GLuint src_offset, src_stride;
384   GLuint dst_offset, dst_stride;
385
386   if (!pbo ||
387       ctx._ImageTransferState ||
388       unpack->SkipPixels || unpack->SkipRows) {
389      _mesa_printf("%s: failure 1\n", __FUNCTION__);
390      return GL_FALSE;
391   }
392
393   src_offset = (GLuint) pixels;
394
395   if (unpack->RowLength > 0)
396      src_stride = unpack->RowLength;
397   else
398      src_stride = width;
399
400   dst_offset = st_texture_image_offset(stImage->pt,
401                                           stImage->face,
402                                           stImage->level);
403
404   dst_stride = stImage->pt->pitch;
405
406   {
407      struct _DriBufferObject *src_buffer =
408         intel_bufferobj_buffer(intel, pbo, INTEL_READ);
409
410      /* Temporary hack: cast to _DriBufferObject:
411       */
412      struct _DriBufferObject *dst_buffer =
413         (struct _DriBufferObject *)stImage->pt->region->buffer;
414
415
416      intelEmitCopyBlit(intel,
417                        stImage->pt->cpp,
418                        src_stride, src_buffer, src_offset,
419                        dst_stride, dst_buffer, dst_offset,
420                        0, 0, 0, 0, width, height,
421			GL_COPY);
422   }
423
424   return GL_TRUE;
425#endif
426}
427
428
429/**
430 * Adjust pixel unpack params and image dimensions to strip off the
431 * texture border.
432 * Gallium doesn't support texture borders.  They've seldem been used
433 * and seldom been implemented correctly anyway.
434 * \param unpackNew  returns the new pixel unpack parameters
435 */
436static void
437strip_texture_border(GLint border,
438                     GLint *width, GLint *height, GLint *depth,
439                     const struct gl_pixelstore_attrib *unpack,
440                     struct gl_pixelstore_attrib *unpackNew)
441{
442   assert(border > 0);  /* sanity check */
443
444   *unpackNew = *unpack;
445
446   if (unpackNew->RowLength == 0)
447      unpackNew->RowLength = *width;
448
449   if (depth && unpackNew->ImageHeight == 0)
450      unpackNew->ImageHeight = *height;
451
452   unpackNew->SkipPixels += border;
453   if (height)
454      unpackNew->SkipRows += border;
455   if (depth)
456      unpackNew->SkipImages += border;
457
458   assert(*width >= 3);
459   *width = *width - 2 * border;
460   if (height && *height >= 3)
461      *height = *height - 2 * border;
462   if (depth && *depth >= 3)
463      *depth = *depth - 2 * border;
464}
465
466
467static void
468st_TexImage(GLcontext * ctx,
469            GLint dims,
470            GLenum target, GLint level,
471            GLint internalFormat,
472            GLint width, GLint height, GLint depth,
473            GLint border,
474            GLenum format, GLenum type, const void *pixels,
475            const struct gl_pixelstore_attrib *unpack,
476            struct gl_texture_object *texObj,
477            struct gl_texture_image *texImage,
478            GLsizei imageSize, int compressed)
479{
480   struct st_texture_object *stObj = st_texture_object(texObj);
481   struct st_texture_image *stImage = st_texture_image(texImage);
482   GLint postConvWidth, postConvHeight;
483   GLint texelBytes, sizeInBytes;
484   GLuint dstRowStride;
485   struct gl_pixelstore_attrib unpackNB;
486
487   DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
488       _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
489
490   /* gallium does not support texture borders, strip it off */
491   if (border) {
492      strip_texture_border(border, &width, &height, &depth,
493                           unpack, &unpackNB);
494      unpack = &unpackNB;
495      border = 0;
496   }
497
498   postConvWidth = width;
499   postConvHeight = height;
500
501   stImage->face = _mesa_tex_target_to_face(target);
502   stImage->level = level;
503
504   if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) {
505      _mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth,
506                                         &postConvHeight);
507   }
508
509   /* choose the texture format */
510   texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat,
511                                                format, type);
512
513   _mesa_set_fetch_functions(texImage, dims);
514
515   if (texImage->TexFormat->TexelBytes == 0) {
516      /* must be a compressed format */
517      texelBytes = 0;
518      texImage->IsCompressed = GL_TRUE;
519      texImage->CompressedSize =
520	 ctx->Driver.CompressedTextureSize(ctx, texImage->Width,
521					   texImage->Height, texImage->Depth,
522					   texImage->TexFormat->MesaFormat);
523   }
524   else {
525      texelBytes = texImage->TexFormat->TexelBytes;
526
527      /* Minimum pitch of 32 bytes */
528      if (postConvWidth * texelBytes < 32) {
529	 postConvWidth = 32 / texelBytes;
530	 texImage->RowStride = postConvWidth;
531      }
532
533      assert(texImage->RowStride == postConvWidth);
534   }
535
536   /* Release the reference to a potentially orphaned buffer.
537    * Release any old malloced memory.
538    */
539   if (stImage->pt) {
540      ctx->st->pipe->texture_release(ctx->st->pipe, &stImage->pt);
541      assert(!texImage->Data);
542   }
543   else if (texImage->Data) {
544      _mesa_align_free(texImage->Data);
545   }
546
547   /* If this is the only mipmap level in the texture, could call
548    * bmBufferData with NULL data to free the old block and avoid
549    * waiting on any outstanding fences.
550    */
551   if (stObj->pt &&
552       /*stObj->pt->first_level == level &&*/
553       stObj->pt->last_level == level &&
554       stObj->pt->target != PIPE_TEXTURE_CUBE &&
555       !st_texture_match_image(stObj->pt, &stImage->base,
556                                  stImage->face, stImage->level)) {
557
558      DBG("release it\n");
559      ctx->st->pipe->texture_release(ctx->st->pipe, &stObj->pt);
560      assert(!stObj->pt);
561   }
562
563   if (!stObj->pt) {
564      guess_and_alloc_texture(ctx->st, stObj, stImage);
565      if (!stObj->pt) {
566	 DBG("guess_and_alloc_texture: failed\n");
567      }
568   }
569
570   assert(!stImage->pt);
571
572   if (stObj->pt &&
573       st_texture_match_image(stObj->pt, &stImage->base,
574                                 stImage->face, stImage->level)) {
575
576      pipe_texture_reference(ctx->st->pipe, &stImage->pt, stObj->pt);
577      assert(stImage->pt);
578   }
579
580   if (!stImage->pt)
581      DBG("XXX: Image did not fit into texture - storing in local memory!\n");
582
583#if 0 /* XXX FIX when st_buffer_objects are in place */
584   /* PBO fastpaths:
585    */
586   if (dims <= 2 &&
587       stImage->pt &&
588       intel_buffer_object(unpack->BufferObj) &&
589       check_pbo_format(internalFormat, format,
590                        type, texImage->TexFormat)) {
591
592      DBG("trying pbo upload\n");
593
594
595
596      /* Otherwise, attempt to use the blitter for PBO image uploads.
597       */
598      if (try_pbo_upload(intel, stImage, unpack,
599                         internalFormat,
600                         width, height, format, type, pixels)) {
601         DBG("pbo upload succeeded\n");
602         return;
603      }
604
605      DBG("pbo upload failed\n");
606   }
607#else
608   (void) try_pbo_upload;
609   (void) check_pbo_format;
610#endif
611
612
613   /* st_CopyTexImage calls this function with pixels == NULL, with
614    * the expectation that the texture will be set up but nothing
615    * more will be done.  This is where those calls return:
616    */
617   if (compressed) {
618      pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
619						      unpack,
620						      "glCompressedTexImage");
621   } else {
622      pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
623					   format, type,
624					   pixels, unpack, "glTexImage");
625   }
626   if (!pixels)
627      return;
628
629   if (stImage->pt) {
630      texImage->Data = st_texture_image_map(ctx->st, stImage, 0);
631      dstRowStride = stImage->surface->pitch * stImage->surface->cpp;
632   }
633   else {
634      /* Allocate regular memory and store the image there temporarily.   */
635      if (texImage->IsCompressed) {
636         sizeInBytes = texImage->CompressedSize;
637         dstRowStride =
638            _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat, width);
639         assert(dims != 3);
640      }
641      else {
642         dstRowStride = postConvWidth * texelBytes;
643         sizeInBytes = depth * dstRowStride * postConvHeight;
644      }
645
646      texImage->Data = malloc(sizeInBytes);
647   }
648
649   DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
650       width, height, depth, width * texelBytes, dstRowStride);
651
652   /* Copy data.  Would like to know when it's ok for us to eg. use
653    * the blitter to copy.  Or, use the hardware to do the format
654    * conversion and copy:
655    */
656   if (compressed) {
657      memcpy(texImage->Data, pixels, imageSize);
658   }
659   else {
660      GLuint srcImageStride = _mesa_image_image_stride(unpack, width, height,
661						       format, type);
662      int i;
663      const GLubyte *src = (const GLubyte *) pixels;
664
665      for (i = 0; i++ < depth;) {
666	 if (!texImage->TexFormat->StoreImage(ctx, dims,
667					      texImage->_BaseFormat,
668					      texImage->TexFormat,
669					      texImage->Data,
670					      0, 0, 0, /* dstX/Y/Zoffset */
671					      dstRowStride,
672					      texImage->ImageOffsets,
673					      width, height, 1,
674					      format, type, src, unpack)) {
675	    _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
676	 }
677
678	 if (stImage->pt && i < depth) {
679	    st_texture_image_unmap(stImage);
680	    texImage->Data = st_texture_image_map(ctx->st, stImage, i);
681	    src += srcImageStride;
682	 }
683      }
684   }
685
686   _mesa_unmap_teximage_pbo(ctx, unpack);
687
688   if (stImage->pt) {
689      st_texture_image_unmap(stImage);
690      texImage->Data = NULL;
691   }
692
693   /* flag data as dirty */
694   stObj->dirtyData = GL_TRUE;
695
696   if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
697      ctx->Driver.GenerateMipmap(ctx, target, texObj);
698   }
699}
700
701
702static void
703st_TexImage3D(GLcontext * ctx,
704                GLenum target, GLint level,
705                GLint internalFormat,
706                GLint width, GLint height, GLint depth,
707                GLint border,
708                GLenum format, GLenum type, const void *pixels,
709                const struct gl_pixelstore_attrib *unpack,
710                struct gl_texture_object *texObj,
711                struct gl_texture_image *texImage)
712{
713   st_TexImage(ctx, 3, target, level,
714                 internalFormat, width, height, depth, border,
715                 format, type, pixels, unpack, texObj, texImage, 0, 0);
716}
717
718
719static void
720st_TexImage2D(GLcontext * ctx,
721                GLenum target, GLint level,
722                GLint internalFormat,
723                GLint width, GLint height, GLint border,
724                GLenum format, GLenum type, const void *pixels,
725                const struct gl_pixelstore_attrib *unpack,
726                struct gl_texture_object *texObj,
727                struct gl_texture_image *texImage)
728{
729   st_TexImage(ctx, 2, target, level,
730                 internalFormat, width, height, 1, border,
731                 format, type, pixels, unpack, texObj, texImage, 0, 0);
732}
733
734
735static void
736st_TexImage1D(GLcontext * ctx,
737                GLenum target, GLint level,
738                GLint internalFormat,
739                GLint width, GLint border,
740                GLenum format, GLenum type, const void *pixels,
741                const struct gl_pixelstore_attrib *unpack,
742                struct gl_texture_object *texObj,
743                struct gl_texture_image *texImage)
744{
745   st_TexImage(ctx, 1, target, level,
746                 internalFormat, width, 1, 1, border,
747                 format, type, pixels, unpack, texObj, texImage, 0, 0);
748}
749
750
751static void
752st_CompressedTexImage2D( GLcontext *ctx, GLenum target, GLint level,
753				GLint internalFormat,
754				GLint width, GLint height, GLint border,
755				GLsizei imageSize, const GLvoid *data,
756				struct gl_texture_object *texObj,
757				struct gl_texture_image *texImage )
758{
759   st_TexImage(ctx, 2, target, level,
760		 internalFormat, width, height, 1, border,
761		 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, 1);
762}
763
764
765/**
766 * Need to map texture image into memory before copying image data,
767 * then unmap it.
768 */
769static void
770st_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
771                 GLenum format, GLenum type, GLvoid * pixels,
772                 struct gl_texture_object *texObj,
773                 struct gl_texture_image *texImage, int compressed)
774{
775   struct st_texture_image *stImage = st_texture_image(texImage);
776   GLuint dstImageStride = _mesa_image_image_stride(&ctx->Pack, texImage->Width,
777						    texImage->Height, format,
778						    type);
779   GLuint depth;
780   int i;
781   GLubyte *dest;
782
783   /* Map */
784   if (stImage->pt) {
785      /* Image is stored in hardware format in a buffer managed by the
786       * kernel.  Need to explicitly map and unmap it.
787       */
788      texImage->Data = st_texture_image_map(ctx->st, stImage, 0);
789      texImage->RowStride = stImage->surface->pitch;
790   }
791   else {
792      /* Otherwise, the image should actually be stored in
793       * texImage->Data.  This is pretty confusing for
794       * everybody, I'd much prefer to separate the two functions of
795       * texImage->Data - storage for texture images in main memory
796       * and access (ie mappings) of images.  In other words, we'd
797       * create a new texImage->Map field and leave Data simply for
798       * storage.
799       */
800      assert(texImage->Data);
801   }
802
803   depth = texImage->Depth;
804   texImage->Depth = 1;
805
806   dest = (GLubyte *) pixels;
807
808   for (i = 0; i++ < depth;) {
809      if (compressed) {
810	 _mesa_get_compressed_teximage(ctx, target, level, dest,
811				       texObj, texImage);
812      } else {
813	 _mesa_get_teximage(ctx, target, level, format, type, dest,
814			    texObj, texImage);
815      }
816
817      if (stImage->pt && i < depth) {
818	 st_texture_image_unmap(stImage);
819	 texImage->Data = st_texture_image_map(ctx->st, stImage, i);
820	 dest += dstImageStride;
821      }
822   }
823
824   texImage->Depth = depth;
825
826   /* Unmap */
827   if (stImage->pt) {
828      st_texture_image_unmap(stImage);
829      texImage->Data = NULL;
830   }
831}
832
833
834static void
835st_GetTexImage(GLcontext * ctx, GLenum target, GLint level,
836                 GLenum format, GLenum type, GLvoid * pixels,
837                 struct gl_texture_object *texObj,
838                 struct gl_texture_image *texImage)
839{
840   st_get_tex_image(ctx, target, level, format, type, pixels,
841                    texObj, texImage, 0);
842}
843
844
845static void
846st_GetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level,
847			   GLvoid *pixels,
848			   const struct gl_texture_object *texObj,
849			   const struct gl_texture_image *texImage)
850{
851   st_get_tex_image(ctx, target, level, 0, 0, pixels,
852                    (struct gl_texture_object *) texObj,
853                    (struct gl_texture_image *) texImage, 1);
854}
855
856
857
858static void
859st_TexSubimage(GLcontext * ctx,
860                 GLint dims,
861                 GLenum target, GLint level,
862                 GLint xoffset, GLint yoffset, GLint zoffset,
863                 GLint width, GLint height, GLint depth,
864                 GLenum format, GLenum type, const void *pixels,
865                 const struct gl_pixelstore_attrib *packing,
866                 struct gl_texture_object *texObj,
867                 struct gl_texture_image *texImage)
868{
869   struct st_texture_object *stObj = st_texture_object(texObj);
870   struct st_texture_image *stImage = st_texture_image(texImage);
871   GLuint dstRowStride;
872   GLuint srcImageStride = _mesa_image_image_stride(packing, width, height,
873						    format, type);
874   int i;
875   const GLubyte *src;
876
877   DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
878       _mesa_lookup_enum_by_nr(target),
879       level, xoffset, yoffset, width, height);
880
881   pixels =
882      _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format,
883                                  type, pixels, packing, "glTexSubImage2D");
884   if (!pixels)
885      return;
886
887   /* Map buffer if necessary.  Need to lock to prevent other contexts
888    * from uploading the buffer under us.
889    */
890   if (stImage->pt) {
891      texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset);
892      dstRowStride = stImage->surface->pitch * stImage->surface->cpp;
893   }
894
895   src = (const GLubyte *) pixels;
896
897   for (i = 0; i++ < depth;) {
898      if (!texImage->TexFormat->StoreImage(ctx, dims, texImage->_BaseFormat,
899					   texImage->TexFormat,
900					   texImage->Data,
901					   xoffset, yoffset, 0,
902					   dstRowStride,
903					   texImage->ImageOffsets,
904					   width, height, 1,
905					   format, type, src, packing)) {
906	 _mesa_error(ctx, GL_OUT_OF_MEMORY, "st_TexSubImage");
907      }
908
909      if (stImage->pt && i < depth) {
910	 st_texture_image_unmap(stImage);
911	 texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset + i);
912	 src += srcImageStride;
913      }
914   }
915
916   if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
917      ctx->Driver.GenerateMipmap(ctx, target, texObj);
918   }
919
920   _mesa_unmap_teximage_pbo(ctx, packing);
921
922   if (stImage->pt) {
923      st_texture_image_unmap(stImage);
924      texImage->Data = NULL;
925   }
926
927   /* flag data as dirty */
928   stObj->dirtyData = GL_TRUE;
929}
930
931
932
933static void
934st_TexSubImage3D(GLcontext * ctx,
935                   GLenum target,
936                   GLint level,
937                   GLint xoffset, GLint yoffset, GLint zoffset,
938                   GLsizei width, GLsizei height, GLsizei depth,
939                   GLenum format, GLenum type,
940                   const GLvoid * pixels,
941                   const struct gl_pixelstore_attrib *packing,
942                   struct gl_texture_object *texObj,
943                   struct gl_texture_image *texImage)
944{
945   st_TexSubimage(ctx, 3, target, level,
946                  xoffset, yoffset, zoffset,
947                  width, height, depth,
948                  format, type, pixels, packing, texObj, texImage);
949}
950
951
952
953static void
954st_TexSubImage2D(GLcontext * ctx,
955                   GLenum target,
956                   GLint level,
957                   GLint xoffset, GLint yoffset,
958                   GLsizei width, GLsizei height,
959                   GLenum format, GLenum type,
960                   const GLvoid * pixels,
961                   const struct gl_pixelstore_attrib *packing,
962                   struct gl_texture_object *texObj,
963                   struct gl_texture_image *texImage)
964{
965   st_TexSubimage(ctx, 2, target, level,
966                  xoffset, yoffset, 0,
967                  width, height, 1,
968                  format, type, pixels, packing, texObj, texImage);
969}
970
971
972static void
973st_TexSubImage1D(GLcontext * ctx,
974                   GLenum target,
975                   GLint level,
976                   GLint xoffset,
977                   GLsizei width,
978                   GLenum format, GLenum type,
979                   const GLvoid * pixels,
980                   const struct gl_pixelstore_attrib *packing,
981                   struct gl_texture_object *texObj,
982                   struct gl_texture_image *texImage)
983{
984   st_TexSubimage(ctx, 1, target, level,
985                  xoffset, 0, 0,
986                  width, 1, 1,
987                  format, type, pixels, packing, texObj, texImage);
988}
989
990
991
992/**
993 * Return 0 for GL_TEXTURE_CUBE_MAP_POSITIVE_X,
994 *        1 for GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
995 *        etc.
996 * XXX duplicated from main/teximage.c
997 */
998static uint
999texture_face(GLenum target)
1000{
1001   if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1002       target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)
1003      return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
1004   else
1005      return 0;
1006}
1007
1008
1009
1010/**
1011 * Do a CopyTexSubImage operation by mapping the source surface and
1012 * dest surface and using get_tile()/put_tile() to access the pixels/texels.
1013 *
1014 * Note: srcY=0=TOP of renderbuffer
1015 */
1016static void
1017fallback_copy_texsubimage(GLcontext *ctx,
1018                          GLenum target,
1019                          GLint level,
1020                          struct st_renderbuffer *strb,
1021                          struct st_texture_image *stImage,
1022                          GLenum baseFormat,
1023                          GLint destX, GLint destY, GLint destZ,
1024                          GLint srcX, GLint srcY,
1025                          GLsizei width, GLsizei height)
1026{
1027   struct pipe_context *pipe = ctx->st->pipe;
1028   const uint face = texture_face(target);
1029   struct pipe_texture *pt = stImage->pt;
1030   struct pipe_surface *src_surf, *dest_surf;
1031   GLfloat *data;
1032   GLint row, yStep;
1033
1034   /* determine bottom-to-top vs. top-to-bottom order */
1035   if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1036      destY = height - 1 - destY;
1037      yStep = -1;
1038   }
1039   else {
1040      yStep = 1;
1041   }
1042
1043   src_surf = strb->surface;
1044
1045   dest_surf = pipe->get_tex_surface(pipe, pt,
1046                                    face, level, destZ);
1047
1048   /* buffer for one row */
1049   data = (GLfloat *) malloc(width * 4 * sizeof(GLfloat));
1050
1051   /* do copy row by row */
1052   for (row = 0; row < height; row++) {
1053      pipe_get_tile_rgba(pipe, src_surf, srcX, srcY + row, width, 1, data);
1054
1055      /* XXX we're ignoring convolution for now */
1056      if (ctx->_ImageTransferState) {
1057         _mesa_apply_rgba_transfer_ops(ctx,
1058                            ctx->_ImageTransferState & ~IMAGE_CONVOLUTION_BIT,
1059                            width, (GLfloat (*)[4])data);
1060      }
1061
1062      pipe_put_tile_rgba(pipe, dest_surf, destX, destY, width, 1, data);
1063      destY += yStep;
1064   }
1065
1066   free(data);
1067}
1068
1069
1070
1071
1072/**
1073 * Do a CopyTex[Sub]Image using an optimized hardware (blit) path.
1074 * Note that the region to copy has already been clip tested.
1075 *
1076 * Note: srcY=0=Bottom of renderbuffer
1077 *
1078 * \return GL_TRUE if success, GL_FALSE if failure (use a fallback)
1079 */
1080static void
1081do_copy_texsubimage(GLcontext *ctx,
1082                    GLenum target, GLint level,
1083                    GLint destX, GLint destY, GLint destZ,
1084                    GLint srcX, GLint srcY,
1085                    GLsizei width, GLsizei height)
1086{
1087   struct gl_texture_unit *texUnit =
1088      &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1089   struct gl_texture_object *texObj =
1090      _mesa_select_tex_object(ctx, texUnit, target);
1091   struct gl_texture_image *texImage =
1092      _mesa_select_tex_image(ctx, texObj, target, level);
1093   struct st_texture_image *stImage = st_texture_image(texImage);
1094   GLenum baseFormat = texImage->InternalFormat;
1095   struct gl_framebuffer *fb = ctx->ReadBuffer;
1096   struct st_renderbuffer *strb;
1097   struct pipe_context *pipe = ctx->st->pipe;
1098   struct pipe_surface *dest_surface;
1099   uint dest_format, src_format;
1100   uint do_flip = FALSE;
1101
1102   (void) texImage;
1103
1104   /* determine if copying depth or color data */
1105   if (baseFormat == GL_DEPTH_COMPONENT) {
1106      strb = st_renderbuffer(fb->_DepthBuffer);
1107   }
1108   else if (baseFormat == GL_DEPTH_STENCIL_EXT) {
1109      strb = st_renderbuffer(fb->_StencilBuffer);
1110   }
1111   else {
1112      /* baseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1113      strb = st_renderbuffer(fb->_ColorReadBuffer);
1114   }
1115
1116   assert(strb);
1117   assert(strb->surface);
1118   assert(stImage->pt);
1119
1120   if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1121      srcY = strb->Base.Height - srcY - height;
1122      do_flip = TRUE;
1123   }
1124
1125   src_format = strb->surface->format;
1126   dest_format = stImage->pt->format;
1127
1128   dest_surface = pipe->get_tex_surface(pipe, stImage->pt, stImage->face,
1129					stImage->level, destZ);
1130
1131   if (src_format == dest_format &&
1132       ctx->_ImageTransferState == 0x0 &&
1133       strb->surface->buffer &&
1134       dest_surface->buffer &&
1135       strb->surface->cpp == stImage->pt->cpp) {
1136      /* do blit-style copy */
1137
1138      /* XXX may need to invert image depending on window
1139       * vs. user-created FBO
1140       */
1141
1142#if 0
1143      /* A bit of fiddling to get the blitter to work with -ve
1144       * pitches.  But we get a nice inverted blit this way, so it's
1145       * worth it:
1146       */
1147      intelEmitCopyBlit(intel,
1148                        stImage->pt->cpp,
1149                        -src->pitch,
1150                        src->buffer,
1151                        src->height * src->pitch * src->cpp,
1152                        stImage->pt->pitch,
1153                        stImage->pt->region->buffer,
1154                        dest_offset,
1155                        x, y + height, dstx, dsty, width, height,
1156                        GL_COPY); /* ? */
1157#else
1158
1159      pipe->surface_copy(pipe,
1160                         do_flip,
1161			 /* dest */
1162			 dest_surface,
1163			 destX, destY,
1164			 /* src */
1165			 strb->surface,
1166			 srcX, srcY,
1167			 /* size */
1168			 width, height);
1169#endif
1170   }
1171   else {
1172      fallback_copy_texsubimage(ctx, target, level,
1173                                strb, stImage, baseFormat,
1174                                destX, destY, destZ,
1175                                srcX, srcY, width, height);
1176   }
1177
1178   pipe_surface_reference(&dest_surface, NULL);
1179
1180   if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
1181      ctx->Driver.GenerateMipmap(ctx, target, texObj);
1182   }
1183}
1184
1185
1186
1187static void
1188st_CopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
1189                  GLenum internalFormat,
1190                  GLint x, GLint y, GLsizei width, GLint border)
1191{
1192   struct gl_texture_unit *texUnit =
1193      &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1194   struct gl_texture_object *texObj =
1195      _mesa_select_tex_object(ctx, texUnit, target);
1196   struct gl_texture_image *texImage =
1197      _mesa_select_tex_image(ctx, texObj, target, level);
1198
1199#if 0
1200   if (border)
1201      goto fail;
1202#endif
1203
1204   /* Setup or redefine the texture object, texture and texture
1205    * image.  Don't populate yet.
1206    */
1207   ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
1208                          width, border,
1209                          GL_RGBA, CHAN_TYPE, NULL,
1210                          &ctx->DefaultPacking, texObj, texImage);
1211
1212   do_copy_texsubimage(ctx, target, level,
1213                       0, 0, 0,
1214                       x, y, width, 1);
1215}
1216
1217
1218static void
1219st_CopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
1220                  GLenum internalFormat,
1221                  GLint x, GLint y, GLsizei width, GLsizei height,
1222                  GLint border)
1223{
1224   struct gl_texture_unit *texUnit =
1225      &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1226   struct gl_texture_object *texObj =
1227      _mesa_select_tex_object(ctx, texUnit, target);
1228   struct gl_texture_image *texImage =
1229      _mesa_select_tex_image(ctx, texObj, target, level);
1230
1231#if 0
1232   if (border)
1233      goto fail;
1234#endif
1235
1236   /* Setup or redefine the texture object, texture and texture
1237    * image.  Don't populate yet.
1238    */
1239   ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
1240                          width, height, border,
1241                          GL_RGBA, CHAN_TYPE, NULL,
1242                          &ctx->DefaultPacking, texObj, texImage);
1243
1244
1245   do_copy_texsubimage(ctx, target, level,
1246                       0, 0, 0,
1247                       x, y, width, height);
1248}
1249
1250
1251static void
1252st_CopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
1253                     GLint xoffset, GLint x, GLint y, GLsizei width)
1254{
1255   const GLint yoffset = 0, zoffset = 0;
1256   const GLsizei height = 1;
1257   do_copy_texsubimage(ctx, target, level,
1258                       xoffset, yoffset, zoffset,
1259                       x, y, width, height);
1260}
1261
1262
1263static void
1264st_CopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
1265                     GLint xoffset, GLint yoffset,
1266                     GLint x, GLint y, GLsizei width, GLsizei height)
1267{
1268   const GLint zoffset = 0;
1269   do_copy_texsubimage(ctx, target, level,
1270                       xoffset, yoffset, zoffset,
1271                       x, y, width, height);
1272}
1273
1274
1275static void
1276st_CopyTexSubImage3D(GLcontext * ctx, GLenum target, GLint level,
1277                     GLint xoffset, GLint yoffset, GLint zoffset,
1278                     GLint x, GLint y, GLsizei width, GLsizei height)
1279{
1280   do_copy_texsubimage(ctx, target, level,
1281                       xoffset, yoffset, zoffset,
1282                       x, y, width, height);
1283}
1284
1285
1286
1287
1288/**
1289 * Compute which mipmap levels that really need to be sent to the hardware.
1290 * This depends on the base image size, GL_TEXTURE_MIN_LOD,
1291 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
1292 */
1293static void
1294calculate_first_last_level(struct st_texture_object *stObj)
1295{
1296   struct gl_texture_object *tObj = &stObj->base;
1297   const struct gl_texture_image *const baseImage =
1298      tObj->Image[0][tObj->BaseLevel];
1299
1300   /* These must be signed values.  MinLod and MaxLod can be negative numbers,
1301    * and having firstLevel and lastLevel as signed prevents the need for
1302    * extra sign checks.
1303    */
1304   int firstLevel;
1305   int lastLevel;
1306
1307   /* Yes, this looks overly complicated, but it's all needed.
1308    */
1309   switch (tObj->Target) {
1310   case GL_TEXTURE_1D:
1311   case GL_TEXTURE_2D:
1312   case GL_TEXTURE_3D:
1313   case GL_TEXTURE_CUBE_MAP:
1314      if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) {
1315         /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
1316          */
1317         firstLevel = lastLevel = tObj->BaseLevel;
1318      }
1319      else {
1320         firstLevel = 0;
1321         lastLevel = MIN2(tObj->MaxLevel - tObj->BaseLevel, baseImage->MaxLog2);
1322      }
1323      break;
1324   case GL_TEXTURE_RECTANGLE_NV:
1325   case GL_TEXTURE_4D_SGIS:
1326      firstLevel = lastLevel = 0;
1327      break;
1328   default:
1329      return;
1330   }
1331
1332   stObj->lastLevel = lastLevel;
1333}
1334
1335
1336static void
1337copy_image_data_to_texture(struct st_context *st,
1338			   struct st_texture_object *stObj,
1339                           GLuint dstLevel,
1340			   struct st_texture_image *stImage)
1341{
1342   if (stImage->pt) {
1343      /* Copy potentially with the blitter:
1344       */
1345      st_texture_image_copy(st->pipe,
1346                            stObj->pt, dstLevel,  /* dest texture, level */
1347                            stImage->pt, /* src texture */
1348                            stImage->face
1349                            );
1350
1351      st->pipe->texture_release(st->pipe, &stImage->pt);
1352   }
1353   else {
1354      assert(stImage->base.Data != NULL);
1355
1356      /* More straightforward upload.
1357       */
1358      st_texture_image_data(st->pipe,
1359                               stObj->pt,
1360                               stImage->face,
1361                               stImage->level,
1362                               stImage->base.Data,
1363                               stImage->base.RowStride,
1364                               stImage->base.RowStride *
1365                               stImage->base.Height);
1366      _mesa_align_free(stImage->base.Data);
1367      stImage->base.Data = NULL;
1368   }
1369
1370   pipe_texture_reference(st->pipe, &stImage->pt, stObj->pt);
1371}
1372
1373
1374/**
1375 * Called during state validation.  When this function is finished,
1376 * the texture object should be ready for rendering.
1377 * \return GL_FALSE if a texture border is present, GL_TRUE otherwise
1378 */
1379GLboolean
1380st_finalize_texture(GLcontext *ctx,
1381		    struct pipe_context *pipe,
1382		    struct gl_texture_object *tObj,
1383		    GLboolean *needFlush)
1384{
1385   struct st_texture_object *stObj = st_texture_object(tObj);
1386   const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1387   int comp_byte = 0;
1388   int cpp;
1389   GLuint face;
1390   struct st_texture_image *firstImage;
1391
1392   *needFlush = GL_FALSE;
1393
1394   /* We know/require this is true by now:
1395    */
1396   assert(stObj->base._Complete);
1397
1398   /* What levels must the texture include at a minimum?
1399    */
1400   calculate_first_last_level(stObj);
1401   firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1402
1403   /* Fallback case:
1404    */
1405   if (firstImage->base.Border) {
1406      if (stObj->pt) {
1407         ctx->st->pipe->texture_release(ctx->st->pipe, &stObj->pt);
1408      }
1409      return GL_FALSE;
1410   }
1411
1412
1413   /* If both firstImage and stObj point to a texture which can contain
1414    * all active images, favour firstImage.  Note that because of the
1415    * completeness requirement, we know that the image dimensions
1416    * will match.
1417    */
1418   if (firstImage->pt &&
1419       firstImage->pt != stObj->pt &&
1420       firstImage->pt->last_level >= stObj->lastLevel) {
1421
1422      if (stObj->pt)
1423         ctx->st->pipe->texture_release(ctx->st->pipe, &stObj->pt);
1424
1425      pipe_texture_reference(ctx->st->pipe, &stObj->pt, firstImage->pt);
1426   }
1427
1428   if (firstImage->base.IsCompressed) {
1429      comp_byte = compressed_num_bytes(firstImage->base.TexFormat->MesaFormat);
1430      cpp = comp_byte;
1431   }
1432   else {
1433      cpp = firstImage->base.TexFormat->TexelBytes;
1434   }
1435
1436   /* Check texture can hold all active levels.  Check texture matches
1437    * target, imageFormat, etc.
1438    */
1439   if (stObj->pt &&
1440       (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1441	stObj->pt->format !=
1442	st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat) ||
1443	stObj->pt->last_level != stObj->lastLevel ||
1444	stObj->pt->width[0] != firstImage->base.Width ||
1445	stObj->pt->height[0] != firstImage->base.Height ||
1446	stObj->pt->depth[0] != firstImage->base.Depth ||
1447	stObj->pt->cpp != cpp ||
1448	stObj->pt->compressed != firstImage->base.IsCompressed)) {
1449      ctx->st->pipe->texture_release(ctx->st->pipe, &stObj->pt);
1450   }
1451
1452
1453   /* May need to create a new texture:
1454    */
1455   if (!stObj->pt) {
1456      stObj->pt = st_texture_create(ctx->st,
1457                                    gl_target_to_pipe(stObj->base.Target),
1458                                    st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat),
1459                                    stObj->lastLevel,
1460                                    firstImage->base.Width,
1461                                    firstImage->base.Height,
1462                                    firstImage->base.Depth,
1463                                    comp_byte);
1464   }
1465
1466   /* Pull in any images not in the object's texture:
1467    */
1468   for (face = 0; face < nr_faces; face++) {
1469      GLuint level;
1470      for (level = 0; level <= stObj->lastLevel; level++) {
1471         struct st_texture_image *stImage =
1472            st_texture_image(stObj->base.Image[face][stObj->base.BaseLevel + level]);
1473
1474         /* Need to import images in main memory or held in other textures.
1475          */
1476         if (stImage && stObj->pt != stImage->pt) {
1477            copy_image_data_to_texture(ctx->st, stObj, level, stImage);
1478	    *needFlush = GL_TRUE;
1479         }
1480      }
1481   }
1482
1483
1484   return GL_TRUE;
1485}
1486
1487
1488
1489
1490void
1491st_init_texture_functions(struct dd_function_table *functions)
1492{
1493   functions->ChooseTextureFormat = st_ChooseTextureFormat;
1494   functions->TexImage1D = st_TexImage1D;
1495   functions->TexImage2D = st_TexImage2D;
1496   functions->TexImage3D = st_TexImage3D;
1497   functions->TexSubImage1D = st_TexSubImage1D;
1498   functions->TexSubImage2D = st_TexSubImage2D;
1499   functions->TexSubImage3D = st_TexSubImage3D;
1500   functions->CopyTexImage1D = st_CopyTexImage1D;
1501   functions->CopyTexImage2D = st_CopyTexImage2D;
1502   functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
1503   functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
1504   functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
1505   functions->GenerateMipmap = st_generate_mipmap;
1506
1507   functions->GetTexImage = st_GetTexImage;
1508
1509   /* compressed texture functions */
1510   functions->CompressedTexImage2D = st_CompressedTexImage2D;
1511   functions->GetCompressedTexImage = st_GetCompressedTexImage;
1512
1513   functions->NewTextureObject = st_NewTextureObject;
1514   functions->NewTextureImage = st_NewTextureImage;
1515   functions->DeleteTexture = st_DeleteTextureObject;
1516   functions->FreeTexImageData = st_FreeTextureImageData;
1517   functions->UpdateTexturePalette = 0;
1518   functions->IsTextureResident = st_IsTextureResident;
1519
1520   functions->TextureMemCpy = do_memcpy;
1521
1522   /* XXX Temporary until we can query pipe's texture sizes */
1523   functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1524}
1525