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