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