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