st_cb_texture.c revision 58edb0683db45c449b219988a8715cf8fd69e42d
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 01
697   if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
698      ctx->Driver.GenerateMipmap(ctx, target, texObj);
699   }
700#endif
701}
702
703
704static void
705st_TexImage3D(GLcontext * ctx,
706                GLenum target, GLint level,
707                GLint internalFormat,
708                GLint width, GLint height, GLint depth,
709                GLint border,
710                GLenum format, GLenum type, const void *pixels,
711                const struct gl_pixelstore_attrib *unpack,
712                struct gl_texture_object *texObj,
713                struct gl_texture_image *texImage)
714{
715   st_TexImage(ctx, 3, target, level,
716                 internalFormat, width, height, depth, border,
717                 format, type, pixels, unpack, texObj, texImage, 0, 0);
718}
719
720
721static void
722st_TexImage2D(GLcontext * ctx,
723                GLenum target, GLint level,
724                GLint internalFormat,
725                GLint width, GLint height, GLint border,
726                GLenum format, GLenum type, const void *pixels,
727                const struct gl_pixelstore_attrib *unpack,
728                struct gl_texture_object *texObj,
729                struct gl_texture_image *texImage)
730{
731   st_TexImage(ctx, 2, target, level,
732                 internalFormat, width, height, 1, border,
733                 format, type, pixels, unpack, texObj, texImage, 0, 0);
734}
735
736
737static void
738st_TexImage1D(GLcontext * ctx,
739                GLenum target, GLint level,
740                GLint internalFormat,
741                GLint width, GLint border,
742                GLenum format, GLenum type, const void *pixels,
743                const struct gl_pixelstore_attrib *unpack,
744                struct gl_texture_object *texObj,
745                struct gl_texture_image *texImage)
746{
747   st_TexImage(ctx, 1, target, level,
748                 internalFormat, width, 1, 1, border,
749                 format, type, pixels, unpack, texObj, texImage, 0, 0);
750}
751
752
753static void
754st_CompressedTexImage2D( GLcontext *ctx, GLenum target, GLint level,
755				GLint internalFormat,
756				GLint width, GLint height, GLint border,
757				GLsizei imageSize, const GLvoid *data,
758				struct gl_texture_object *texObj,
759				struct gl_texture_image *texImage )
760{
761   st_TexImage(ctx, 2, target, level,
762		 internalFormat, width, height, 1, border,
763		 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, 1);
764}
765
766
767/**
768 * Need to map texture image into memory before copying image data,
769 * then unmap it.
770 */
771static void
772st_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
773                 GLenum format, GLenum type, GLvoid * pixels,
774                 struct gl_texture_object *texObj,
775                 struct gl_texture_image *texImage, int compressed)
776{
777   struct st_texture_image *stImage = st_texture_image(texImage);
778   GLuint dstImageStride = _mesa_image_image_stride(&ctx->Pack, texImage->Width,
779						    texImage->Height, format,
780						    type);
781   GLuint depth;
782   int i;
783   GLubyte *dest;
784
785   /* Map */
786   if (stImage->pt) {
787      /* Image is stored in hardware format in a buffer managed by the
788       * kernel.  Need to explicitly map and unmap it.
789       */
790      texImage->Data = st_texture_image_map(ctx->st, stImage, 0);
791      texImage->RowStride = stImage->surface->pitch;
792   }
793   else {
794      /* Otherwise, the image should actually be stored in
795       * texImage->Data.  This is pretty confusing for
796       * everybody, I'd much prefer to separate the two functions of
797       * texImage->Data - storage for texture images in main memory
798       * and access (ie mappings) of images.  In other words, we'd
799       * create a new texImage->Map field and leave Data simply for
800       * storage.
801       */
802      assert(texImage->Data);
803   }
804
805   depth = texImage->Depth;
806   texImage->Depth = 1;
807
808   dest = (GLubyte *) pixels;
809
810   for (i = 0; i++ < depth;) {
811      if (compressed) {
812	 _mesa_get_compressed_teximage(ctx, target, level, dest,
813				       texObj, texImage);
814      } else {
815	 _mesa_get_teximage(ctx, target, level, format, type, dest,
816			    texObj, texImage);
817      }
818
819      if (stImage->pt && i < depth) {
820	 st_texture_image_unmap(stImage);
821	 texImage->Data = st_texture_image_map(ctx->st, stImage, i);
822	 dest += dstImageStride;
823      }
824   }
825
826   texImage->Depth = depth;
827
828   /* Unmap */
829   if (stImage->pt) {
830      st_texture_image_unmap(stImage);
831      texImage->Data = NULL;
832   }
833}
834
835
836static void
837st_GetTexImage(GLcontext * ctx, GLenum target, GLint level,
838                 GLenum format, GLenum type, GLvoid * pixels,
839                 struct gl_texture_object *texObj,
840                 struct gl_texture_image *texImage)
841{
842   st_get_tex_image(ctx, target, level, format, type, pixels,
843                    texObj, texImage, 0);
844}
845
846
847static void
848st_GetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level,
849			   GLvoid *pixels,
850			   const struct gl_texture_object *texObj,
851			   const struct gl_texture_image *texImage)
852{
853   st_get_tex_image(ctx, target, level, 0, 0, pixels,
854                    (struct gl_texture_object *) texObj,
855                    (struct gl_texture_image *) texImage, 1);
856}
857
858
859
860static void
861st_TexSubimage(GLcontext * ctx,
862                 GLint dims,
863                 GLenum target, GLint level,
864                 GLint xoffset, GLint yoffset, GLint zoffset,
865                 GLint width, GLint height, GLint depth,
866                 GLenum format, GLenum type, const void *pixels,
867                 const struct gl_pixelstore_attrib *packing,
868                 struct gl_texture_object *texObj,
869                 struct gl_texture_image *texImage)
870{
871   struct st_texture_object *stObj = st_texture_object(texObj);
872   struct st_texture_image *stImage = st_texture_image(texImage);
873   GLuint dstRowStride;
874   GLuint srcImageStride = _mesa_image_image_stride(packing, width, height,
875						    format, type);
876   int i;
877   const GLubyte *src;
878
879   DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
880       _mesa_lookup_enum_by_nr(target),
881       level, xoffset, yoffset, width, height);
882
883   pixels =
884      _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format,
885                                  type, pixels, packing, "glTexSubImage2D");
886   if (!pixels)
887      return;
888
889   /* Map buffer if necessary.  Need to lock to prevent other contexts
890    * from uploading the buffer under us.
891    */
892   if (stImage->pt) {
893      texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset);
894      dstRowStride = stImage->surface->pitch * stImage->surface->cpp;
895   }
896
897   src = (const GLubyte *) pixels;
898
899   for (i = 0; i++ < depth;) {
900      if (!texImage->TexFormat->StoreImage(ctx, dims, texImage->_BaseFormat,
901					   texImage->TexFormat,
902					   texImage->Data,
903					   xoffset, yoffset, 0,
904					   dstRowStride,
905					   texImage->ImageOffsets,
906					   width, height, 1,
907					   format, type, src, packing)) {
908	 _mesa_error(ctx, GL_OUT_OF_MEMORY, "st_TexSubImage");
909      }
910
911      if (stImage->pt && i < depth) {
912	 st_texture_image_unmap(stImage);
913	 texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset + i);
914	 src += srcImageStride;
915      }
916   }
917
918#if 0
919   /* GL_SGIS_generate_mipmap */
920   if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
921      _mesa_generate_mipmap(ctx, target,
922                            &ctx->Texture.Unit[ctx->Texture.CurrentUnit],
923                            texObj);
924   }
925#endif
926
927   _mesa_unmap_teximage_pbo(ctx, packing);
928
929   if (stImage->pt) {
930      st_texture_image_unmap(stImage);
931      texImage->Data = NULL;
932   }
933
934   /* flag data as dirty */
935   stObj->dirtyData = GL_TRUE;
936}
937
938
939
940static void
941st_TexSubImage3D(GLcontext * ctx,
942                   GLenum target,
943                   GLint level,
944                   GLint xoffset, GLint yoffset, GLint zoffset,
945                   GLsizei width, GLsizei height, GLsizei depth,
946                   GLenum format, GLenum type,
947                   const GLvoid * pixels,
948                   const struct gl_pixelstore_attrib *packing,
949                   struct gl_texture_object *texObj,
950                   struct gl_texture_image *texImage)
951{
952   st_TexSubimage(ctx, 3, target, level,
953                  xoffset, yoffset, zoffset,
954                  width, height, depth,
955                  format, type, pixels, packing, texObj, texImage);
956}
957
958
959
960static void
961st_TexSubImage2D(GLcontext * ctx,
962                   GLenum target,
963                   GLint level,
964                   GLint xoffset, GLint yoffset,
965                   GLsizei width, GLsizei height,
966                   GLenum format, GLenum type,
967                   const GLvoid * pixels,
968                   const struct gl_pixelstore_attrib *packing,
969                   struct gl_texture_object *texObj,
970                   struct gl_texture_image *texImage)
971{
972   st_TexSubimage(ctx, 2, target, level,
973                  xoffset, yoffset, 0,
974                  width, height, 1,
975                  format, type, pixels, packing, texObj, texImage);
976}
977
978
979static void
980st_TexSubImage1D(GLcontext * ctx,
981                   GLenum target,
982                   GLint level,
983                   GLint xoffset,
984                   GLsizei width,
985                   GLenum format, GLenum type,
986                   const GLvoid * pixels,
987                   const struct gl_pixelstore_attrib *packing,
988                   struct gl_texture_object *texObj,
989                   struct gl_texture_image *texImage)
990{
991   st_TexSubimage(ctx, 1, target, level,
992                  xoffset, 0, 0,
993                  width, 1, 1,
994                  format, type, pixels, packing, texObj, texImage);
995}
996
997
998
999/**
1000 * Return 0 for GL_TEXTURE_CUBE_MAP_POSITIVE_X,
1001 *        1 for GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
1002 *        etc.
1003 * XXX duplicated from main/teximage.c
1004 */
1005static uint
1006texture_face(GLenum target)
1007{
1008   if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1009       target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)
1010      return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
1011   else
1012      return 0;
1013}
1014
1015
1016
1017/**
1018 * Do a CopyTexSubImage operation by mapping the source surface and
1019 * dest surface and using get_tile()/put_tile() to access the pixels/texels.
1020 *
1021 * Note: srcY=0=TOP of renderbuffer
1022 */
1023static void
1024fallback_copy_texsubimage(GLcontext *ctx,
1025                          GLenum target,
1026                          GLint level,
1027                          struct st_renderbuffer *strb,
1028                          struct st_texture_image *stImage,
1029                          GLenum baseFormat,
1030                          GLint destX, GLint destY, GLint destZ,
1031                          GLint srcX, GLint srcY,
1032                          GLsizei width, GLsizei height)
1033{
1034   struct pipe_context *pipe = ctx->st->pipe;
1035   const uint face = texture_face(target);
1036   struct pipe_texture *pt = stImage->pt;
1037   struct pipe_surface *src_surf, *dest_surf;
1038   GLfloat *data;
1039   GLint row, yStep;
1040
1041   /* determine bottom-to-top vs. top-to-bottom order */
1042   if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1043      destY = height - 1 - destY;
1044      yStep = -1;
1045   }
1046   else {
1047      yStep = 1;
1048   }
1049
1050   src_surf = strb->surface;
1051
1052   dest_surf = pipe->get_tex_surface(pipe, pt,
1053                                    face, level, destZ);
1054
1055   /* buffer for one row */
1056   data = (GLfloat *) malloc(width * 4 * sizeof(GLfloat));
1057
1058   /* do copy row by row */
1059   for (row = 0; row < height; row++) {
1060      pipe_get_tile_rgba(pipe, src_surf, srcX, srcY + row, width, 1, data);
1061
1062      /* XXX we're ignoring convolution for now */
1063      if (ctx->_ImageTransferState) {
1064         _mesa_apply_rgba_transfer_ops(ctx,
1065                            ctx->_ImageTransferState & ~IMAGE_CONVOLUTION_BIT,
1066                            width, (GLfloat (*)[4])data);
1067      }
1068
1069      pipe_put_tile_rgba(pipe, dest_surf, destX, destY, width, 1, data);
1070      destY += yStep;
1071   }
1072
1073   free(data);
1074}
1075
1076
1077
1078
1079/**
1080 * Do a CopyTex[Sub]Image using an optimized hardware (blit) path.
1081 * Note that the region to copy has already been clip tested.
1082 *
1083 * Note: srcY=0=Bottom of renderbuffer
1084 *
1085 * \return GL_TRUE if success, GL_FALSE if failure (use a fallback)
1086 */
1087static void
1088do_copy_texsubimage(GLcontext *ctx,
1089                    GLenum target, GLint level,
1090                    GLint destX, GLint destY, GLint destZ,
1091                    GLint srcX, GLint srcY,
1092                    GLsizei width, GLsizei height)
1093{
1094   struct gl_texture_unit *texUnit =
1095      &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1096   struct gl_texture_object *texObj =
1097      _mesa_select_tex_object(ctx, texUnit, target);
1098   struct gl_texture_image *texImage =
1099      _mesa_select_tex_image(ctx, texObj, target, level);
1100   struct st_texture_image *stImage = st_texture_image(texImage);
1101   GLenum baseFormat = texImage->InternalFormat;
1102   struct gl_framebuffer *fb = ctx->ReadBuffer;
1103   struct st_renderbuffer *strb;
1104   struct pipe_context *pipe = ctx->st->pipe;
1105   struct pipe_surface *dest_surface;
1106   uint dest_format, src_format;
1107   uint do_flip = FALSE;
1108
1109   (void) texImage;
1110
1111   /* determine if copying depth or color data */
1112   if (baseFormat == GL_DEPTH_COMPONENT) {
1113      strb = st_renderbuffer(fb->_DepthBuffer);
1114   }
1115   else if (baseFormat == GL_DEPTH_STENCIL_EXT) {
1116      strb = st_renderbuffer(fb->_StencilBuffer);
1117   }
1118   else {
1119      /* baseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1120      strb = st_renderbuffer(fb->_ColorReadBuffer);
1121   }
1122
1123   assert(strb);
1124   assert(strb->surface);
1125   assert(stImage->pt);
1126
1127   if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1128      srcY = strb->Base.Height - srcY - height;
1129      do_flip = TRUE;
1130   }
1131
1132   src_format = strb->surface->format;
1133   dest_format = stImage->pt->format;
1134
1135   dest_surface = pipe->get_tex_surface(pipe, stImage->pt, stImage->face,
1136					stImage->level, destZ);
1137
1138   if (src_format == dest_format &&
1139       ctx->_ImageTransferState == 0x0 &&
1140       strb->surface->buffer &&
1141       dest_surface->buffer &&
1142       strb->surface->cpp == stImage->pt->cpp) {
1143      /* do blit-style copy */
1144
1145      /* XXX may need to invert image depending on window
1146       * vs. user-created FBO
1147       */
1148
1149#if 0
1150      /* A bit of fiddling to get the blitter to work with -ve
1151       * pitches.  But we get a nice inverted blit this way, so it's
1152       * worth it:
1153       */
1154      intelEmitCopyBlit(intel,
1155                        stImage->pt->cpp,
1156                        -src->pitch,
1157                        src->buffer,
1158                        src->height * src->pitch * src->cpp,
1159                        stImage->pt->pitch,
1160                        stImage->pt->region->buffer,
1161                        dest_offset,
1162                        x, y + height, dstx, dsty, width, height,
1163                        GL_COPY); /* ? */
1164#else
1165
1166      pipe->surface_copy(pipe,
1167                         do_flip,
1168			 /* dest */
1169			 dest_surface,
1170			 destX, destY,
1171			 /* src */
1172			 strb->surface,
1173			 srcX, srcY,
1174			 /* size */
1175			 width, height);
1176#endif
1177   }
1178   else {
1179      fallback_copy_texsubimage(ctx, target, level,
1180                                strb, stImage, baseFormat,
1181                                destX, destY, destZ,
1182                                srcX, srcY, width, height);
1183   }
1184
1185   pipe_surface_reference(&dest_surface, NULL);
1186
1187#if 0
1188   /* GL_SGIS_generate_mipmap -- this can be accelerated now.
1189    * XXX Add a ctx->Driver.GenerateMipmaps() function?
1190    */
1191   if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
1192      intel_generate_mipmap(ctx, target,
1193                            &ctx->Texture.Unit[ctx->Texture.CurrentUnit],
1194                            texObj);
1195   }
1196#endif
1197
1198}
1199
1200
1201
1202static void
1203st_CopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
1204                  GLenum internalFormat,
1205                  GLint x, GLint y, GLsizei width, GLint border)
1206{
1207   struct gl_texture_unit *texUnit =
1208      &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1209   struct gl_texture_object *texObj =
1210      _mesa_select_tex_object(ctx, texUnit, target);
1211   struct gl_texture_image *texImage =
1212      _mesa_select_tex_image(ctx, texObj, target, level);
1213
1214#if 0
1215   if (border)
1216      goto fail;
1217#endif
1218
1219   /* Setup or redefine the texture object, texture and texture
1220    * image.  Don't populate yet.
1221    */
1222   ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
1223                          width, border,
1224                          GL_RGBA, CHAN_TYPE, NULL,
1225                          &ctx->DefaultPacking, texObj, texImage);
1226
1227   do_copy_texsubimage(ctx, target, level,
1228                       0, 0, 0,
1229                       x, y, width, 1);
1230}
1231
1232
1233static void
1234st_CopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
1235                  GLenum internalFormat,
1236                  GLint x, GLint y, GLsizei width, GLsizei height,
1237                  GLint border)
1238{
1239   struct gl_texture_unit *texUnit =
1240      &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1241   struct gl_texture_object *texObj =
1242      _mesa_select_tex_object(ctx, texUnit, target);
1243   struct gl_texture_image *texImage =
1244      _mesa_select_tex_image(ctx, texObj, target, level);
1245
1246#if 0
1247   if (border)
1248      goto fail;
1249#endif
1250
1251   /* Setup or redefine the texture object, texture and texture
1252    * image.  Don't populate yet.
1253    */
1254   ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
1255                          width, height, border,
1256                          GL_RGBA, CHAN_TYPE, NULL,
1257                          &ctx->DefaultPacking, texObj, texImage);
1258
1259
1260   do_copy_texsubimage(ctx, target, level,
1261                       0, 0, 0,
1262                       x, y, width, height);
1263}
1264
1265
1266static void
1267st_CopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
1268                     GLint xoffset, GLint x, GLint y, GLsizei width)
1269{
1270   const GLint yoffset = 0, zoffset = 0;
1271   const GLsizei height = 1;
1272   do_copy_texsubimage(ctx, target, level,
1273                       xoffset, yoffset, zoffset,
1274                       x, y, width, height);
1275}
1276
1277
1278static void
1279st_CopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
1280                     GLint xoffset, GLint yoffset,
1281                     GLint x, GLint y, GLsizei width, GLsizei height)
1282{
1283   const GLint zoffset = 0;
1284   do_copy_texsubimage(ctx, target, level,
1285                       xoffset, yoffset, zoffset,
1286                       x, y, width, height);
1287}
1288
1289
1290static void
1291st_CopyTexSubImage3D(GLcontext * ctx, GLenum target, GLint level,
1292                     GLint xoffset, GLint yoffset, GLint zoffset,
1293                     GLint x, GLint y, GLsizei width, GLsizei height)
1294{
1295   do_copy_texsubimage(ctx, target, level,
1296                       xoffset, yoffset, zoffset,
1297                       x, y, width, height);
1298}
1299
1300
1301
1302
1303/**
1304 * Compute which mipmap levels that really need to be sent to the hardware.
1305 * This depends on the base image size, GL_TEXTURE_MIN_LOD,
1306 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
1307 */
1308static void
1309calculate_first_last_level(struct st_texture_object *stObj)
1310{
1311   struct gl_texture_object *tObj = &stObj->base;
1312   const struct gl_texture_image *const baseImage =
1313      tObj->Image[0][tObj->BaseLevel];
1314
1315   /* These must be signed values.  MinLod and MaxLod can be negative numbers,
1316    * and having firstLevel and lastLevel as signed prevents the need for
1317    * extra sign checks.
1318    */
1319   int firstLevel;
1320   int lastLevel;
1321
1322   /* Yes, this looks overly complicated, but it's all needed.
1323    */
1324   switch (tObj->Target) {
1325   case GL_TEXTURE_1D:
1326   case GL_TEXTURE_2D:
1327   case GL_TEXTURE_3D:
1328   case GL_TEXTURE_CUBE_MAP:
1329      if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) {
1330         /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
1331          */
1332         firstLevel = lastLevel = tObj->BaseLevel;
1333      }
1334      else {
1335         firstLevel = 0;
1336         lastLevel = MIN2(tObj->MaxLevel - tObj->BaseLevel, baseImage->MaxLog2);
1337      }
1338      break;
1339   case GL_TEXTURE_RECTANGLE_NV:
1340   case GL_TEXTURE_4D_SGIS:
1341      firstLevel = lastLevel = 0;
1342      break;
1343   default:
1344      return;
1345   }
1346
1347   stObj->lastLevel = lastLevel;
1348}
1349
1350
1351static void
1352copy_image_data_to_texture(struct st_context *st,
1353			   struct st_texture_object *stObj,
1354                           GLuint dstLevel,
1355			   struct st_texture_image *stImage)
1356{
1357   if (stImage->pt) {
1358      /* Copy potentially with the blitter:
1359       */
1360      st_texture_image_copy(st->pipe,
1361                            stObj->pt, dstLevel,  /* dest texture, level */
1362                            stImage->pt, /* src texture */
1363                            stImage->face
1364                            );
1365
1366      st->pipe->texture_release(st->pipe, &stImage->pt);
1367   }
1368   else {
1369      assert(stImage->base.Data != NULL);
1370
1371      /* More straightforward upload.
1372       */
1373      st_texture_image_data(st->pipe,
1374                               stObj->pt,
1375                               stImage->face,
1376                               stImage->level,
1377                               stImage->base.Data,
1378                               stImage->base.RowStride,
1379                               stImage->base.RowStride *
1380                               stImage->base.Height);
1381      _mesa_align_free(stImage->base.Data);
1382      stImage->base.Data = NULL;
1383   }
1384
1385   pipe_texture_reference(st->pipe, &stImage->pt, stObj->pt);
1386}
1387
1388
1389/**
1390 * Called during state validation.  When this function is finished,
1391 * the texture object should be ready for rendering.
1392 * \return GL_FALSE if a texture border is present, GL_TRUE otherwise
1393 */
1394GLboolean
1395st_finalize_texture(GLcontext *ctx,
1396		    struct pipe_context *pipe,
1397		    struct gl_texture_object *tObj,
1398		    GLboolean *needFlush)
1399{
1400   struct st_texture_object *stObj = st_texture_object(tObj);
1401   const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1402   int comp_byte = 0;
1403   int cpp;
1404   GLuint face;
1405   struct st_texture_image *firstImage;
1406
1407   *needFlush = GL_FALSE;
1408
1409   /* We know/require this is true by now:
1410    */
1411   assert(stObj->base._Complete);
1412
1413   /* What levels must the texture include at a minimum?
1414    */
1415   calculate_first_last_level(stObj);
1416   firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1417
1418   /* Fallback case:
1419    */
1420   if (firstImage->base.Border) {
1421      if (stObj->pt) {
1422         ctx->st->pipe->texture_release(ctx->st->pipe, &stObj->pt);
1423      }
1424      return GL_FALSE;
1425   }
1426
1427
1428   /* If both firstImage and stObj point to a texture which can contain
1429    * all active images, favour firstImage.  Note that because of the
1430    * completeness requirement, we know that the image dimensions
1431    * will match.
1432    */
1433   if (firstImage->pt &&
1434       firstImage->pt != stObj->pt &&
1435       firstImage->pt->last_level >= stObj->lastLevel) {
1436
1437      if (stObj->pt)
1438         ctx->st->pipe->texture_release(ctx->st->pipe, &stObj->pt);
1439
1440      pipe_texture_reference(ctx->st->pipe, &stObj->pt, firstImage->pt);
1441   }
1442
1443   if (firstImage->base.IsCompressed) {
1444      comp_byte = compressed_num_bytes(firstImage->base.TexFormat->MesaFormat);
1445      cpp = comp_byte;
1446   }
1447   else {
1448      cpp = firstImage->base.TexFormat->TexelBytes;
1449   }
1450
1451   /* Check texture can hold all active levels.  Check texture matches
1452    * target, imageFormat, etc.
1453    */
1454   if (stObj->pt &&
1455       (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1456	stObj->pt->format !=
1457	st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat) ||
1458	stObj->pt->last_level != stObj->lastLevel ||
1459	stObj->pt->width[0] != firstImage->base.Width ||
1460	stObj->pt->height[0] != firstImage->base.Height ||
1461	stObj->pt->depth[0] != firstImage->base.Depth ||
1462	stObj->pt->cpp != cpp ||
1463	stObj->pt->compressed != firstImage->base.IsCompressed)) {
1464      ctx->st->pipe->texture_release(ctx->st->pipe, &stObj->pt);
1465   }
1466
1467
1468   /* May need to create a new texture:
1469    */
1470   if (!stObj->pt) {
1471      stObj->pt = st_texture_create(ctx->st,
1472                                    gl_target_to_pipe(stObj->base.Target),
1473                                    st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat),
1474                                    stObj->lastLevel,
1475                                    firstImage->base.Width,
1476                                    firstImage->base.Height,
1477                                    firstImage->base.Depth,
1478                                    comp_byte);
1479   }
1480
1481   /* Pull in any images not in the object's texture:
1482    */
1483   for (face = 0; face < nr_faces; face++) {
1484      GLuint level;
1485      for (level = 0; level <= stObj->lastLevel; level++) {
1486         struct st_texture_image *stImage =
1487            st_texture_image(stObj->base.Image[face][stObj->base.BaseLevel + level]);
1488
1489         /* Need to import images in main memory or held in other textures.
1490          */
1491         if (stImage && stObj->pt != stImage->pt) {
1492            copy_image_data_to_texture(ctx->st, stObj, level, stImage);
1493	    *needFlush = GL_TRUE;
1494         }
1495      }
1496   }
1497
1498
1499   return GL_TRUE;
1500}
1501
1502
1503
1504
1505void
1506st_init_texture_functions(struct dd_function_table *functions)
1507{
1508   functions->ChooseTextureFormat = st_ChooseTextureFormat;
1509   functions->TexImage1D = st_TexImage1D;
1510   functions->TexImage2D = st_TexImage2D;
1511   functions->TexImage3D = st_TexImage3D;
1512   functions->TexSubImage1D = st_TexSubImage1D;
1513   functions->TexSubImage2D = st_TexSubImage2D;
1514   functions->TexSubImage3D = st_TexSubImage3D;
1515   functions->CopyTexImage1D = st_CopyTexImage1D;
1516   functions->CopyTexImage2D = st_CopyTexImage2D;
1517   functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
1518   functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
1519   functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
1520   functions->GenerateMipmap = st_generate_mipmap;
1521
1522   functions->GetTexImage = st_GetTexImage;
1523
1524   /* compressed texture functions */
1525   functions->CompressedTexImage2D = st_CompressedTexImage2D;
1526   functions->GetCompressedTexImage = st_GetCompressedTexImage;
1527
1528   functions->NewTextureObject = st_NewTextureObject;
1529   functions->NewTextureImage = st_NewTextureImage;
1530   functions->DeleteTexture = st_DeleteTextureObject;
1531   functions->FreeTexImageData = st_FreeTextureImageData;
1532   functions->UpdateTexturePalette = 0;
1533   functions->IsTextureResident = st_IsTextureResident;
1534
1535   functions->TextureMemCpy = do_memcpy;
1536
1537   /* XXX Temporary until we can query pipe's texture sizes */
1538   functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1539}
1540