st_cb_texture.c revision 9b56a2cb626b254bcb7b7202e6babd1b5570208f
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                                 bindings);
363
364   DBG("%s returning %d\n", __FUNCTION__, (stObj->pt != NULL));
365
366   return stObj->pt != NULL;
367}
368
369
370/**
371 * Adjust pixel unpack params and image dimensions to strip off the
372 * texture border.
373 * Gallium doesn't support texture borders.  They've seldem been used
374 * and seldom been implemented correctly anyway.
375 * \param unpackNew  returns the new pixel unpack parameters
376 */
377static void
378strip_texture_border(GLint border,
379                     GLint *width, GLint *height, GLint *depth,
380                     const struct gl_pixelstore_attrib *unpack,
381                     struct gl_pixelstore_attrib *unpackNew)
382{
383   assert(border > 0);  /* sanity check */
384
385   *unpackNew = *unpack;
386
387   if (unpackNew->RowLength == 0)
388      unpackNew->RowLength = *width;
389
390   if (depth && unpackNew->ImageHeight == 0)
391      unpackNew->ImageHeight = *height;
392
393   unpackNew->SkipPixels += border;
394   if (height)
395      unpackNew->SkipRows += border;
396   if (depth)
397      unpackNew->SkipImages += border;
398
399   assert(*width >= 3);
400   *width = *width - 2 * border;
401   if (height && *height >= 3)
402      *height = *height - 2 * border;
403   if (depth && *depth >= 3)
404      *depth = *depth - 2 * border;
405}
406
407
408/**
409 * Try to do texture compression via rendering.  If the Gallium driver
410 * can render into a compressed surface this will allow us to do texture
411 * compression.
412 * \return GL_TRUE for success, GL_FALSE for failure
413 */
414static GLboolean
415compress_with_blit(struct gl_context * ctx,
416                   GLenum target, GLint level,
417                   GLint xoffset, GLint yoffset, GLint zoffset,
418                   GLint width, GLint height, GLint depth,
419                   GLenum format, GLenum type, const void *pixels,
420                   const struct gl_pixelstore_attrib *unpack,
421                   struct gl_texture_image *texImage)
422{
423   const GLuint dstImageOffsets[1] = {0};
424   struct st_texture_image *stImage = st_texture_image(texImage);
425   struct st_context *st = st_context(ctx);
426   struct pipe_context *pipe = st->pipe;
427   struct pipe_screen *screen = pipe->screen;
428   gl_format mesa_format;
429   struct pipe_resource templ;
430   struct pipe_resource *src_tex;
431   struct pipe_sampler_view view_templ;
432   struct pipe_sampler_view *src_view;
433   struct pipe_surface *dst_surface, surf_tmpl;
434   struct pipe_transfer *tex_xfer;
435   void *map;
436
437   if (!stImage->pt) {
438      /* XXX: Can this happen? Should we assert? */
439      return GL_FALSE;
440   }
441
442   /* get destination surface (in the compressed texture) */
443   memset(&surf_tmpl, 0, sizeof(surf_tmpl));
444   surf_tmpl.format = stImage->pt->format;
445   surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
446   surf_tmpl.u.tex.level = stImage->level;
447   surf_tmpl.u.tex.first_layer = stImage->face;
448   surf_tmpl.u.tex.last_layer = stImage->face;
449   dst_surface = pipe->create_surface(pipe, stImage->pt, &surf_tmpl);
450   if (!dst_surface) {
451      /* can't render into this format (or other problem) */
452      return GL_FALSE;
453   }
454
455   /* Choose format for the temporary RGBA texture image.
456    */
457   mesa_format = st_ChooseTextureFormat(ctx, GL_RGBA, format, type);
458   assert(mesa_format);
459   if (!mesa_format)
460      return GL_FALSE;
461
462   /* Create the temporary source texture
463    */
464   memset(&templ, 0, sizeof(templ));
465   templ.target = st->internal_target;
466   templ.format = st_mesa_format_to_pipe_format(mesa_format);
467   templ.width0 = width;
468   templ.height0 = height;
469   templ.depth0 = 1;
470   templ.array_size = 1;
471   templ.last_level = 0;
472   templ.usage = PIPE_USAGE_DEFAULT;
473   templ.bind = PIPE_BIND_SAMPLER_VIEW;
474   src_tex = screen->resource_create(screen, &templ);
475
476   if (!src_tex)
477      return GL_FALSE;
478
479   /* Put user's tex data into the temporary texture
480    */
481   tex_xfer = pipe_get_transfer(st_context(ctx)->pipe, src_tex,
482                                0, 0, /* layer, level are zero */
483                                PIPE_TRANSFER_WRITE,
484                                0, 0, width, height); /* x, y, w, h */
485   map = pipe_transfer_map(pipe, tex_xfer);
486
487   _mesa_texstore(ctx, 2, GL_RGBA, mesa_format,
488                  map,              /* dest ptr */
489                  0, 0, 0,          /* dest x/y/z offset */
490                  tex_xfer->stride, /* dest row stride (bytes) */
491                  dstImageOffsets,  /* image offsets (for 3D only) */
492                  width, height, 1, /* size */
493                  format, type,     /* source format/type */
494                  pixels,           /* source data */
495                  unpack);          /* source data packing */
496
497   pipe_transfer_unmap(pipe, tex_xfer);
498   pipe->transfer_destroy(pipe, tex_xfer);
499
500   /* Create temporary sampler view */
501   u_sampler_view_default_template(&view_templ,
502                                   src_tex,
503                                   src_tex->format);
504   src_view = pipe->create_sampler_view(pipe, src_tex, &view_templ);
505
506
507   /* copy / compress image */
508   util_blit_pixels_tex(st->blit,
509                        src_view,         /* sampler view (src) */
510                        0, 0,             /* src x0, y0 */
511                        width, height,    /* src x1, y1 */
512                        dst_surface,      /* pipe_surface (dst) */
513                        xoffset, yoffset, /* dst x0, y0 */
514                        xoffset + width,  /* dst x1 */
515                        yoffset + height, /* dst y1 */
516                        0.0,              /* z */
517                        PIPE_TEX_MIPFILTER_NEAREST);
518
519   pipe_surface_reference(&dst_surface, NULL);
520   pipe_resource_reference(&src_tex, NULL);
521   pipe_sampler_view_reference(&src_view, NULL);
522
523   return GL_TRUE;
524}
525
526
527/**
528 * Do glTexImage1/2/3D().
529 */
530static void
531st_TexImage(struct gl_context * ctx,
532            GLint dims,
533            GLenum target, GLint level,
534            GLint internalFormat,
535            GLint width, GLint height, GLint depth,
536            GLint border,
537            GLenum format, GLenum type, const void *pixels,
538            const struct gl_pixelstore_attrib *unpack,
539            struct gl_texture_object *texObj,
540            struct gl_texture_image *texImage,
541            GLsizei imageSize, GLboolean compressed_src)
542{
543   struct st_context *st = st_context(ctx);
544   struct pipe_screen *screen = st->pipe->screen;
545   struct st_texture_object *stObj = st_texture_object(texObj);
546   struct st_texture_image *stImage = st_texture_image(texImage);
547   GLuint dstRowStride = 0;
548   struct gl_pixelstore_attrib unpackNB;
549   enum pipe_transfer_usage transfer_usage = 0;
550
551   DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
552       _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
553
554   /* switch to "normal" */
555   if (stObj->surface_based) {
556      gl_format texFormat;
557
558      _mesa_clear_texture_object(ctx, texObj);
559      pipe_resource_reference(&stObj->pt, NULL);
560
561      /* oops, need to init this image again */
562      texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
563                                              internalFormat, format, type);
564
565      _mesa_init_teximage_fields(ctx, target, texImage,
566                                 width, height, depth, border,
567                                 internalFormat, texFormat);
568
569      stObj->surface_based = GL_FALSE;
570   }
571
572   /* gallium does not support texture borders, strip it off */
573   if (border) {
574      strip_texture_border(border, &width, &height, &depth, unpack, &unpackNB);
575      unpack = &unpackNB;
576      texImage->Width = width;
577      texImage->Height = height;
578      texImage->Depth = depth;
579      texImage->Border = 0;
580      border = 0;
581   }
582   else {
583      assert(texImage->Width == width);
584      assert(texImage->Height == height);
585      assert(texImage->Depth == depth);
586   }
587
588   stImage->face = _mesa_tex_target_to_face(target);
589   stImage->level = level;
590
591   _mesa_set_fetch_functions(texImage, dims);
592
593   /* Release the reference to a potentially orphaned buffer.
594    * Release any old malloced memory.
595    */
596   if (stImage->pt) {
597      pipe_resource_reference(&stImage->pt, NULL);
598      assert(!texImage->Data);
599   }
600   else if (texImage->Data) {
601      _mesa_align_free(texImage->Data);
602   }
603
604   /*
605    * See if the new image is somehow incompatible with the existing
606    * mipmap.  If so, free the old mipmap.
607    */
608   if (stObj->pt) {
609      if (level > (GLint) stObj->pt->last_level ||
610          !st_texture_match_image(stObj->pt, &stImage->base,
611                                  stImage->face, stImage->level)) {
612         DBG("release it\n");
613         pipe_resource_reference(&stObj->pt, NULL);
614         assert(!stObj->pt);
615         pipe_sampler_view_reference(&stObj->sampler_view, NULL);
616      }
617   }
618
619   if (width == 0 || height == 0 || depth == 0) {
620      /* stop after freeing old image */
621      return;
622   }
623
624   if (!stObj->pt) {
625      if (!guess_and_alloc_texture(st, stObj, stImage)) {
626         /* Probably out of memory.
627          * Try flushing any pending rendering, then retry.
628          */
629         st_finish(st);
630         if (!guess_and_alloc_texture(st, stObj, stImage)) {
631            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
632            return;
633         }
634      }
635   }
636
637   assert(!stImage->pt);
638
639   /* Check if this texture image can live inside the texture object's buffer.
640    * If so, store the image there.  Otherwise the image will temporarily live
641    * in its own buffer.
642    */
643   if (stObj->pt &&
644       st_texture_match_image(stObj->pt, &stImage->base,
645                              stImage->face, stImage->level)) {
646
647      pipe_resource_reference(&stImage->pt, stObj->pt);
648      assert(stImage->pt);
649   }
650
651   if (!stImage->pt)
652      DBG("XXX: Image did not fit into texture - storing in local memory!\n");
653
654   /* Pixel data may come from regular user memory or a PBO.  For the later,
655    * do bounds checking and map the PBO to read pixels data from it.
656    *
657    * XXX we should try to use a GPU-accelerated path to copy the image data
658    * from the PBO to the texture.
659    */
660   if (compressed_src) {
661      pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
662						      unpack,
663						      "glCompressedTexImage");
664   }
665   else {
666      pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
667					   format, type,
668					   pixels, unpack, "glTexImage");
669   }
670
671   /* See if we can do texture compression with a blit/render.
672    */
673   if (!compressed_src &&
674       !ctx->Mesa_DXTn &&
675       _mesa_is_format_compressed(texImage->TexFormat) &&
676       screen->is_format_supported(screen,
677                                   stImage->pt->format,
678                                   stImage->pt->target, 0,
679                                   PIPE_BIND_RENDER_TARGET, 0)) {
680      if (!pixels)
681         goto done;
682
683      if (compress_with_blit(ctx, target, level, 0, 0, 0, width, height, depth,
684                             format, type, pixels, unpack, texImage)) {
685         goto done;
686      }
687   }
688
689   /*
690    * Prepare to store the texture data.  Either map the gallium texture buffer
691    * memory or malloc space for it.
692    */
693   if (stImage->pt) {
694      /* Store the image in the gallium texture memory buffer */
695      if (format == GL_DEPTH_COMPONENT &&
696          util_format_is_depth_and_stencil(stImage->pt->format))
697         transfer_usage = PIPE_TRANSFER_READ_WRITE;
698      else
699         transfer_usage = PIPE_TRANSFER_WRITE;
700
701      texImage->Data = st_texture_image_map(st, stImage, 0,
702                                            transfer_usage, 0, 0, width, height);
703      if(stImage->transfer)
704         dstRowStride = stImage->transfer->stride;
705   }
706   else {
707      /* Allocate regular memory and store the image there temporarily.   */
708      GLuint imageSize = _mesa_format_image_size(texImage->TexFormat,
709                                                 width, height, depth);
710      dstRowStride = _mesa_format_row_stride(texImage->TexFormat, width);
711
712      texImage->Data = _mesa_align_malloc(imageSize, 16);
713   }
714
715   if (!texImage->Data) {
716      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
717      return;
718   }
719
720   if (!pixels) {
721      /* We've allocated texture memory, but have no pixel data - all done. */
722      goto done;
723   }
724
725   DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
726       width, height, depth, width, dstRowStride);
727
728   /* Copy user texture image into the texture buffer.
729    */
730   if (compressed_src) {
731      const GLuint srcRowStride =
732         _mesa_format_row_stride(texImage->TexFormat, width);
733      if (dstRowStride == srcRowStride) {
734         memcpy(texImage->Data, pixels, imageSize);
735      }
736      else {
737         char *dst = texImage->Data;
738         const char *src = pixels;
739         GLuint i, bw, bh, lines;
740         _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
741         lines = (height + bh - 1) / bh;
742
743         for (i = 0; i < lines; ++i) {
744            memcpy(dst, src, srcRowStride);
745            dst += dstRowStride;
746            src += srcRowStride;
747         }
748      }
749   }
750   else {
751      const GLuint srcImageStride =
752         _mesa_image_image_stride(unpack, width, height, format, type);
753      GLint i;
754      const GLubyte *src = (const GLubyte *) pixels;
755
756      for (i = 0; i < depth; i++) {
757	 if (!_mesa_texstore(ctx, dims,
758                             texImage->_BaseFormat,
759                             texImage->TexFormat,
760                             texImage->Data,
761                             0, 0, 0, /* dstX/Y/Zoffset */
762                             dstRowStride,
763                             texImage->ImageOffsets,
764                             width, height, 1,
765                             format, type, src, unpack)) {
766	    _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
767	 }
768
769	 if (stImage->pt && i + 1 < depth) {
770            /* unmap this slice */
771	    st_texture_image_unmap(st, stImage);
772            /* map next slice of 3D texture */
773	    texImage->Data = st_texture_image_map(st, stImage, i + 1,
774                                                  transfer_usage, 0, 0,
775                                                  width, height);
776	    src += srcImageStride;
777	 }
778      }
779   }
780
781done:
782   _mesa_unmap_teximage_pbo(ctx, unpack);
783
784   if (stImage->pt && texImage->Data) {
785      st_texture_image_unmap(st, stImage);
786      texImage->Data = NULL;
787   }
788}
789
790
791static void
792st_TexImage3D(struct gl_context * ctx,
793              GLenum target, GLint level,
794              GLint internalFormat,
795              GLint width, GLint height, GLint depth,
796              GLint border,
797              GLenum format, GLenum type, const void *pixels,
798              const struct gl_pixelstore_attrib *unpack,
799              struct gl_texture_object *texObj,
800              struct gl_texture_image *texImage)
801{
802   st_TexImage(ctx, 3, target, level, internalFormat, width, height, depth,
803               border, format, type, pixels, unpack, texObj, texImage,
804               0, GL_FALSE);
805}
806
807
808static void
809st_TexImage2D(struct gl_context * ctx,
810              GLenum target, GLint level,
811              GLint internalFormat,
812              GLint width, GLint height, GLint border,
813              GLenum format, GLenum type, const void *pixels,
814              const struct gl_pixelstore_attrib *unpack,
815              struct gl_texture_object *texObj,
816              struct gl_texture_image *texImage)
817{
818   st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border,
819               format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
820}
821
822
823static void
824st_TexImage1D(struct gl_context * ctx,
825              GLenum target, GLint level,
826              GLint internalFormat,
827              GLint width, GLint border,
828              GLenum format, GLenum type, const void *pixels,
829              const struct gl_pixelstore_attrib *unpack,
830              struct gl_texture_object *texObj,
831              struct gl_texture_image *texImage)
832{
833   st_TexImage(ctx, 1, target, level, internalFormat, width, 1, 1, border,
834               format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
835}
836
837
838static void
839st_CompressedTexImage2D(struct gl_context *ctx, GLenum target, GLint level,
840                        GLint internalFormat,
841                        GLint width, GLint height, GLint border,
842                        GLsizei imageSize, const GLvoid *data,
843                        struct gl_texture_object *texObj,
844                        struct gl_texture_image *texImage)
845{
846   st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border,
847               0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE);
848}
849
850
851
852/**
853 * glGetTexImage() helper: decompress a compressed texture by rendering
854 * a textured quad.  Store the results in the user's buffer.
855 */
856static void
857decompress_with_blit(struct gl_context * ctx, GLenum target, GLint level,
858                     GLenum format, GLenum type, GLvoid *pixels,
859                     struct gl_texture_object *texObj,
860                     struct gl_texture_image *texImage)
861{
862   struct st_context *st = st_context(ctx);
863   struct pipe_context *pipe = st->pipe;
864   struct st_texture_image *stImage = st_texture_image(texImage);
865   struct st_texture_object *stObj = st_texture_object(texObj);
866   struct pipe_sampler_view *src_view =
867      st_get_texture_sampler_view(stObj, pipe);
868   const GLuint width = texImage->Width;
869   const GLuint height = texImage->Height;
870   struct pipe_surface *dst_surface;
871   struct pipe_resource *dst_texture;
872   struct pipe_transfer *tex_xfer;
873   unsigned bind = (PIPE_BIND_RENDER_TARGET | /* util_blit may choose to render */
874		    PIPE_BIND_TRANSFER_READ);
875
876   /* create temp / dest surface */
877   if (!util_create_rgba_surface(pipe, width, height, bind,
878                                 &dst_texture, &dst_surface)) {
879      _mesa_problem(ctx, "util_create_rgba_surface() failed "
880                    "in decompress_with_blit()");
881      return;
882   }
883
884   /* blit/render/decompress */
885   util_blit_pixels_tex(st->blit,
886                        src_view,      /* pipe_resource (src) */
887                        0, 0,             /* src x0, y0 */
888                        width, height,    /* src x1, y1 */
889                        dst_surface,      /* pipe_surface (dst) */
890                        0, 0,             /* dst x0, y0 */
891                        width, height,    /* dst x1, y1 */
892                        0.0,              /* z */
893                        PIPE_TEX_MIPFILTER_NEAREST);
894
895   /* map the dst_surface so we can read from it */
896   tex_xfer = pipe_get_transfer(st_context(ctx)->pipe,
897                                dst_texture, 0, 0,
898                                PIPE_TRANSFER_READ,
899                                0, 0, width, height);
900
901   pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
902
903   /* copy/pack data into user buffer */
904   if (st_equal_formats(stImage->pt->format, format, type)) {
905      /* memcpy */
906      const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format);
907      ubyte *map = pipe_transfer_map(pipe, tex_xfer);
908      GLuint row;
909      for (row = 0; row < height; row++) {
910         GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
911                                              height, format, type, row, 0);
912         memcpy(dest, map, bytesPerRow);
913         map += tex_xfer->stride;
914      }
915      pipe_transfer_unmap(pipe, tex_xfer);
916   }
917   else {
918      /* format translation via floats */
919      GLuint row;
920      enum pipe_format format = util_format_linear(dst_texture->format);
921      for (row = 0; row < height; row++) {
922         const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
923         GLfloat rgba[4 * MAX_WIDTH];
924         GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
925                                              height, format, type, row, 0);
926
927         if (ST_DEBUG & DEBUG_FALLBACK)
928            debug_printf("%s: fallback format translation\n", __FUNCTION__);
929
930         /* get float[4] rgba row from surface */
931         pipe_get_tile_rgba_format(pipe, tex_xfer, 0, row, width, 1,
932                                   format, rgba);
933
934         _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
935                                    type, dest, &ctx->Pack, transferOps);
936      }
937   }
938
939   _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
940
941   pipe->transfer_destroy(pipe, tex_xfer);
942
943   /* destroy the temp / dest surface */
944   util_destroy_rgba_surface(dst_texture, dst_surface);
945}
946
947
948
949/**
950 * Need to map texture image into memory before copying image data,
951 * then unmap it.
952 */
953static void
954st_get_tex_image(struct gl_context * ctx, GLenum target, GLint level,
955                 GLenum format, GLenum type, GLvoid * pixels,
956                 struct gl_texture_object *texObj,
957                 struct gl_texture_image *texImage, GLboolean compressed_dst)
958{
959   struct st_context *st = st_context(ctx);
960   struct st_texture_image *stImage = st_texture_image(texImage);
961   const GLuint dstImageStride =
962      _mesa_image_image_stride(&ctx->Pack, texImage->Width, texImage->Height,
963                               format, type);
964   GLuint depth, i;
965   GLubyte *dest;
966
967   if (stImage->pt &&
968       util_format_is_s3tc(stImage->pt->format) &&
969       !compressed_dst) {
970      /* Need to decompress the texture.
971       * We'll do this by rendering a textured quad.
972       * Note that we only expect RGBA formats (no Z/depth formats).
973       */
974      decompress_with_blit(ctx, target, level, format, type, pixels,
975                           texObj, texImage);
976      return;
977   }
978
979   /* Map */
980   if (stImage->pt) {
981      /* Image is stored in hardware format in a buffer managed by the
982       * kernel.  Need to explicitly map and unmap it.
983       */
984      texImage->Data = st_texture_image_map(st, stImage, 0,
985                                            PIPE_TRANSFER_READ, 0, 0,
986                                            stImage->base.Width,
987                                            stImage->base.Height);
988      /* compute stride in texels from stride in bytes */
989      texImage->RowStride = stImage->transfer->stride
990         * util_format_get_blockwidth(stImage->pt->format)
991         / util_format_get_blocksize(stImage->pt->format);
992   }
993   else {
994      /* Otherwise, the image should actually be stored in
995       * texImage->Data.  This is pretty confusing for
996       * everybody, I'd much prefer to separate the two functions of
997       * texImage->Data - storage for texture images in main memory
998       * and access (ie mappings) of images.  In other words, we'd
999       * create a new texImage->Map field and leave Data simply for
1000       * storage.
1001       */
1002      assert(texImage->Data);
1003   }
1004
1005   depth = texImage->Depth;
1006   texImage->Depth = 1;
1007
1008   dest = (GLubyte *) pixels;
1009
1010   _mesa_set_fetch_functions(texImage, get_texture_dims(target));
1011
1012   for (i = 0; i < depth; i++) {
1013      if (compressed_dst) {
1014	 _mesa_get_compressed_teximage(ctx, target, level, dest,
1015				       texObj, texImage);
1016      }
1017      else {
1018	 _mesa_get_teximage(ctx, target, level, format, type, dest,
1019			    texObj, texImage);
1020      }
1021
1022      if (stImage->pt && i + 1 < depth) {
1023         /* unmap this slice */
1024	 st_texture_image_unmap(st, stImage);
1025         /* map next slice of 3D texture */
1026	 texImage->Data = st_texture_image_map(st, stImage, i + 1,
1027                                               PIPE_TRANSFER_READ, 0, 0,
1028                                               stImage->base.Width,
1029                                               stImage->base.Height);
1030	 dest += dstImageStride;
1031      }
1032   }
1033
1034   texImage->Depth = depth;
1035
1036   /* Unmap */
1037   if (stImage->pt) {
1038      st_texture_image_unmap(st, stImage);
1039      texImage->Data = NULL;
1040   }
1041}
1042
1043
1044static void
1045st_GetTexImage(struct gl_context * ctx, GLenum target, GLint level,
1046               GLenum format, GLenum type, GLvoid * pixels,
1047               struct gl_texture_object *texObj,
1048               struct gl_texture_image *texImage)
1049{
1050   st_get_tex_image(ctx, target, level, format, type, pixels, texObj, texImage,
1051                    GL_FALSE);
1052}
1053
1054
1055static void
1056st_GetCompressedTexImage(struct gl_context *ctx, GLenum target, GLint level,
1057                         GLvoid *pixels,
1058                         struct gl_texture_object *texObj,
1059                         struct gl_texture_image *texImage)
1060{
1061   st_get_tex_image(ctx, target, level, 0, 0, pixels, texObj, texImage,
1062                    GL_TRUE);
1063}
1064
1065
1066
1067static void
1068st_TexSubimage(struct gl_context *ctx, GLint dims, GLenum target, GLint level,
1069               GLint xoffset, GLint yoffset, GLint zoffset,
1070               GLint width, GLint height, GLint depth,
1071               GLenum format, GLenum type, const void *pixels,
1072               const struct gl_pixelstore_attrib *packing,
1073               struct gl_texture_object *texObj,
1074               struct gl_texture_image *texImage)
1075{
1076   struct st_context *st = st_context(ctx);
1077   struct pipe_screen *screen = st->pipe->screen;
1078   struct st_texture_image *stImage = st_texture_image(texImage);
1079   GLuint dstRowStride;
1080   const GLuint srcImageStride =
1081      _mesa_image_image_stride(packing, width, height, format, type);
1082   GLint i;
1083   const GLubyte *src;
1084   /* init to silence warning only: */
1085   enum pipe_transfer_usage transfer_usage = PIPE_TRANSFER_WRITE;
1086
1087   DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
1088       _mesa_lookup_enum_by_nr(target),
1089       level, xoffset, yoffset, width, height);
1090
1091   pixels =
1092      _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format,
1093                                  type, pixels, packing, "glTexSubImage2D");
1094   if (!pixels)
1095      return;
1096
1097   /* See if we can do texture compression with a blit/render.
1098    */
1099   if (!ctx->Mesa_DXTn &&
1100       _mesa_is_format_compressed(texImage->TexFormat) &&
1101       screen->is_format_supported(screen,
1102                                   stImage->pt->format,
1103                                   stImage->pt->target, 0,
1104                                   PIPE_BIND_RENDER_TARGET, 0)) {
1105      if (compress_with_blit(ctx, target, level,
1106                             xoffset, yoffset, zoffset,
1107                             width, height, depth,
1108                             format, type, pixels, packing, texImage)) {
1109         goto done;
1110      }
1111   }
1112
1113   /* Map buffer if necessary.  Need to lock to prevent other contexts
1114    * from uploading the buffer under us.
1115    */
1116   if (stImage->pt) {
1117      if (format == GL_DEPTH_COMPONENT &&
1118          util_format_is_depth_and_stencil(stImage->pt->format))
1119         transfer_usage = PIPE_TRANSFER_READ_WRITE;
1120      else
1121         transfer_usage = PIPE_TRANSFER_WRITE;
1122
1123      texImage->Data = st_texture_image_map(st, stImage, zoffset,
1124                                            transfer_usage,
1125                                            xoffset, yoffset,
1126                                            width, height);
1127   }
1128
1129   if (!texImage->Data) {
1130      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1131      goto done;
1132   }
1133
1134   src = (const GLubyte *) pixels;
1135   dstRowStride = stImage->transfer->stride;
1136
1137   for (i = 0; i < depth; i++) {
1138      if (!_mesa_texstore(ctx, dims, texImage->_BaseFormat,
1139                          texImage->TexFormat,
1140                          texImage->Data,
1141                          0, 0, 0,
1142                          dstRowStride,
1143                          texImage->ImageOffsets,
1144                          width, height, 1,
1145                          format, type, src, packing)) {
1146	 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1147      }
1148
1149      if (stImage->pt && i + 1 < depth) {
1150         /* unmap this slice */
1151	 st_texture_image_unmap(st, stImage);
1152         /* map next slice of 3D texture */
1153	 texImage->Data = st_texture_image_map(st, stImage,
1154                                               zoffset + i + 1,
1155                                               transfer_usage,
1156                                               xoffset, yoffset,
1157                                               width, height);
1158	 src += srcImageStride;
1159      }
1160   }
1161
1162done:
1163   _mesa_unmap_teximage_pbo(ctx, packing);
1164
1165   if (stImage->pt && texImage->Data) {
1166      st_texture_image_unmap(st, stImage);
1167      texImage->Data = NULL;
1168   }
1169}
1170
1171
1172
1173static void
1174st_TexSubImage3D(struct gl_context *ctx, GLenum target, GLint level,
1175                 GLint xoffset, GLint yoffset, GLint zoffset,
1176                 GLsizei width, GLsizei height, GLsizei depth,
1177                 GLenum format, GLenum type, const GLvoid *pixels,
1178                 const struct gl_pixelstore_attrib *packing,
1179                 struct gl_texture_object *texObj,
1180                 struct gl_texture_image *texImage)
1181{
1182   st_TexSubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
1183                  width, height, depth, format, type,
1184                  pixels, packing, texObj, texImage);
1185}
1186
1187
1188static void
1189st_TexSubImage2D(struct gl_context *ctx, GLenum target, GLint level,
1190                 GLint xoffset, GLint yoffset,
1191                 GLsizei width, GLsizei height,
1192                 GLenum format, GLenum type, const GLvoid * pixels,
1193                 const struct gl_pixelstore_attrib *packing,
1194                 struct gl_texture_object *texObj,
1195                 struct gl_texture_image *texImage)
1196{
1197   st_TexSubimage(ctx, 2, target, level, xoffset, yoffset, 0,
1198                  width, height, 1, format, type,
1199                  pixels, packing, texObj, texImage);
1200}
1201
1202
1203static void
1204st_TexSubImage1D(struct gl_context *ctx, GLenum target, GLint level,
1205                 GLint xoffset, GLsizei width, GLenum format, GLenum type,
1206                 const GLvoid * pixels,
1207                 const struct gl_pixelstore_attrib *packing,
1208                 struct gl_texture_object *texObj,
1209                 struct gl_texture_image *texImage)
1210{
1211   st_TexSubimage(ctx, 1, target, level, xoffset, 0, 0, width, 1, 1,
1212                  format, type, pixels, packing, texObj, texImage);
1213}
1214
1215
1216static void
1217st_CompressedTexSubImage1D(struct gl_context *ctx, GLenum target, GLint level,
1218                           GLint xoffset, GLsizei width,
1219                           GLenum format,
1220                           GLsizei imageSize, const GLvoid *data,
1221                           struct gl_texture_object *texObj,
1222                           struct gl_texture_image *texImage)
1223{
1224   assert(0);
1225}
1226
1227
1228static void
1229st_CompressedTexSubImage2D(struct gl_context *ctx, GLenum target, GLint level,
1230                           GLint xoffset, GLint yoffset,
1231                           GLsizei width, GLint height,
1232                           GLenum format,
1233                           GLsizei imageSize, const GLvoid *data,
1234                           struct gl_texture_object *texObj,
1235                           struct gl_texture_image *texImage)
1236{
1237   struct st_context *st = st_context(ctx);
1238   struct st_texture_image *stImage = st_texture_image(texImage);
1239   int srcBlockStride;
1240   int dstBlockStride;
1241   int y;
1242   enum pipe_format pformat;
1243
1244   if (stImage->pt) {
1245      pformat = stImage->pt->format;
1246
1247      texImage->Data = st_texture_image_map(st, stImage, 0,
1248                                            PIPE_TRANSFER_WRITE,
1249                                            xoffset, yoffset,
1250                                            width, height);
1251
1252      srcBlockStride = util_format_get_stride(pformat, width);
1253      dstBlockStride = stImage->transfer->stride;
1254   } else {
1255      assert(stImage->pt);
1256      /* TODO find good values for block and strides */
1257      /* TODO also adjust texImage->data for yoffset/xoffset */
1258      return;
1259   }
1260
1261   if (!texImage->Data) {
1262      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexSubImage");
1263      return;
1264   }
1265
1266   assert(xoffset % util_format_get_blockwidth(pformat) == 0);
1267   assert(yoffset % util_format_get_blockheight(pformat) == 0);
1268
1269   for (y = 0; y < height; y += util_format_get_blockheight(pformat)) {
1270      /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1271      const char *src = (const char*)data + srcBlockStride * util_format_get_nblocksy(pformat, y);
1272      char *dst = (char*)texImage->Data + dstBlockStride * util_format_get_nblocksy(pformat, y);
1273      memcpy(dst, src, util_format_get_stride(pformat, width));
1274   }
1275
1276   if (stImage->pt) {
1277      st_texture_image_unmap(st, stImage);
1278      texImage->Data = NULL;
1279   }
1280}
1281
1282
1283static void
1284st_CompressedTexSubImage3D(struct gl_context *ctx, GLenum target, GLint level,
1285                           GLint xoffset, GLint yoffset, GLint zoffset,
1286                           GLsizei width, GLint height, GLint depth,
1287                           GLenum format,
1288                           GLsizei imageSize, const GLvoid *data,
1289                           struct gl_texture_object *texObj,
1290                           struct gl_texture_image *texImage)
1291{
1292   assert(0);
1293}
1294
1295
1296
1297/**
1298 * Do a CopyTexSubImage operation using a read transfer from the source,
1299 * a write transfer to the destination and get_tile()/put_tile() to access
1300 * the pixels/texels.
1301 *
1302 * Note: srcY=0=TOP of renderbuffer
1303 */
1304static void
1305fallback_copy_texsubimage(struct gl_context *ctx, GLenum target, GLint level,
1306                          struct st_renderbuffer *strb,
1307                          struct st_texture_image *stImage,
1308                          GLenum baseFormat,
1309                          GLint destX, GLint destY, GLint destZ,
1310                          GLint srcX, GLint srcY,
1311                          GLsizei width, GLsizei height)
1312{
1313   struct st_context *st = st_context(ctx);
1314   struct pipe_context *pipe = st->pipe;
1315   struct pipe_transfer *src_trans;
1316   GLvoid *texDest;
1317   enum pipe_transfer_usage transfer_usage;
1318
1319   if (ST_DEBUG & DEBUG_FALLBACK)
1320      debug_printf("%s: fallback processing\n", __FUNCTION__);
1321
1322   assert(width <= MAX_WIDTH);
1323
1324   if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1325      srcY = strb->Base.Height - srcY - height;
1326   }
1327
1328   src_trans = pipe_get_transfer(st_context(ctx)->pipe,
1329                                 strb->texture,
1330                                 0, 0,
1331                                 PIPE_TRANSFER_READ,
1332                                 srcX, srcY,
1333                                 width, height);
1334
1335   if ((baseFormat == GL_DEPTH_COMPONENT ||
1336        baseFormat == GL_DEPTH_STENCIL) &&
1337       util_format_is_depth_and_stencil(stImage->pt->format))
1338      transfer_usage = PIPE_TRANSFER_READ_WRITE;
1339   else
1340      transfer_usage = PIPE_TRANSFER_WRITE;
1341
1342   /* XXX this used to ignore destZ param */
1343   texDest = st_texture_image_map(st, stImage, destZ, transfer_usage,
1344                                  destX, destY, width, height);
1345
1346   if (baseFormat == GL_DEPTH_COMPONENT ||
1347       baseFormat == GL_DEPTH_STENCIL) {
1348      const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
1349                                     ctx->Pixel.DepthBias != 0.0F);
1350      GLint row, yStep;
1351
1352      /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1353      if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1354         srcY = height - 1;
1355         yStep = -1;
1356      }
1357      else {
1358         srcY = 0;
1359         yStep = 1;
1360      }
1361
1362      /* To avoid a large temp memory allocation, do copy row by row */
1363      for (row = 0; row < height; row++, srcY += yStep) {
1364         uint data[MAX_WIDTH];
1365         pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
1366         if (scaleOrBias) {
1367            _mesa_scale_and_bias_depth_uint(ctx, width, data);
1368         }
1369         pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
1370      }
1371   }
1372   else {
1373      /* RGBA format */
1374      GLfloat *tempSrc =
1375         (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1376
1377      if (tempSrc && texDest) {
1378         const GLint dims = 2;
1379         const GLint dstRowStride = stImage->transfer->stride;
1380         struct gl_texture_image *texImage = &stImage->base;
1381         struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
1382
1383         if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1384            unpack.Invert = GL_TRUE;
1385         }
1386
1387         /* get float/RGBA image from framebuffer */
1388         /* XXX this usually involves a lot of int/float conversion.
1389          * try to avoid that someday.
1390          */
1391         pipe_get_tile_rgba_format(pipe, src_trans, 0, 0, width, height,
1392                                   util_format_linear(strb->texture->format),
1393                                   tempSrc);
1394
1395         /* Store into texture memory.
1396          * Note that this does some special things such as pixel transfer
1397          * ops and format conversion.  In particular, if the dest tex format
1398          * is actually RGBA but the user created the texture as GL_RGB we
1399          * need to fill-in/override the alpha channel with 1.0.
1400          */
1401         _mesa_texstore(ctx, dims,
1402                        texImage->_BaseFormat,
1403                        texImage->TexFormat,
1404                        texDest,
1405                        0, 0, 0,
1406                        dstRowStride,
1407                        texImage->ImageOffsets,
1408                        width, height, 1,
1409                        GL_RGBA, GL_FLOAT, tempSrc, /* src */
1410                        &unpack);
1411      }
1412      else {
1413         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1414      }
1415
1416      if (tempSrc)
1417         free(tempSrc);
1418   }
1419
1420   st_texture_image_unmap(st, stImage);
1421   pipe->transfer_destroy(pipe, src_trans);
1422}
1423
1424
1425
1426/**
1427 * If the format of the src renderbuffer and the format of the dest
1428 * texture are compatible (in terms of blitting), return a TGSI writemask
1429 * to be used during the blit.
1430 * If the src/dest are incompatible, return 0.
1431 */
1432static unsigned
1433compatible_src_dst_formats(struct gl_context *ctx,
1434                           const struct gl_renderbuffer *src,
1435                           const struct gl_texture_image *dst)
1436{
1437   /* Get logical base formats for the src and dest.
1438    * That is, use the user-requested formats and not the actual, device-
1439    * chosen formats.
1440    * For example, the user may have requested an A8 texture but the
1441    * driver may actually be using an RGBA texture format.  When we
1442    * copy/blit to that texture, we only want to copy the Alpha channel
1443    * and not the RGB channels.
1444    *
1445    * Similarly, when the src FBO was created an RGB format may have been
1446    * requested but the driver actually chose an RGBA format.  In that case,
1447    * we don't want to copy the undefined Alpha channel to the dest texture
1448    * (it should be 1.0).
1449    */
1450   const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
1451   const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
1452
1453   /**
1454    * XXX when we have red-only and red/green renderbuffers we'll need
1455    * to add more cases here (or implement a general-purpose routine that
1456    * queries the existance of the R,G,B,A channels in the src and dest).
1457    */
1458   if (srcFormat == dstFormat) {
1459      /* This is the same as matching_base_formats, which should
1460       * always pass, as it did previously.
1461       */
1462      return TGSI_WRITEMASK_XYZW;
1463   }
1464   else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
1465      /* Make sure that A in the dest is 1.  The actual src format
1466       * may be RGBA and have undefined A values.
1467       */
1468      return TGSI_WRITEMASK_XYZ;
1469   }
1470   else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
1471      /* Make sure that A in the dest is 1.  The actual dst format
1472       * may be RGBA and will need A=1 to provide proper alpha values
1473       * when sampled later.
1474       */
1475      return TGSI_WRITEMASK_XYZ;
1476   }
1477   else {
1478      if (ST_DEBUG & DEBUG_FALLBACK)
1479         debug_printf("%s failed for src %s, dst %s\n",
1480                      __FUNCTION__,
1481                      _mesa_lookup_enum_by_nr(srcFormat),
1482                      _mesa_lookup_enum_by_nr(dstFormat));
1483
1484      /* Otherwise fail.
1485       */
1486      return 0;
1487   }
1488}
1489
1490
1491
1492/**
1493 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1494 * Note that the region to copy has already been clipped so we know we
1495 * won't read from outside the source renderbuffer's bounds.
1496 *
1497 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1498 */
1499static void
1500st_copy_texsubimage(struct gl_context *ctx,
1501                    GLenum target, GLint level,
1502                    GLint destX, GLint destY, GLint destZ,
1503                    GLint srcX, GLint srcY,
1504                    GLsizei width, GLsizei height)
1505{
1506   struct gl_texture_unit *texUnit =
1507      &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1508   struct gl_texture_object *texObj =
1509      _mesa_select_tex_object(ctx, texUnit, target);
1510   struct gl_texture_image *texImage =
1511      _mesa_select_tex_image(ctx, texObj, target, level);
1512   struct st_texture_image *stImage = st_texture_image(texImage);
1513   const GLenum texBaseFormat = texImage->_BaseFormat;
1514   struct gl_framebuffer *fb = ctx->ReadBuffer;
1515   struct st_renderbuffer *strb;
1516   struct st_context *st = st_context(ctx);
1517   struct pipe_context *pipe = st->pipe;
1518   struct pipe_screen *screen = pipe->screen;
1519   enum pipe_format dest_format, src_format;
1520   GLboolean use_fallback = GL_TRUE;
1521   GLboolean matching_base_formats;
1522   GLuint format_writemask, sample_count;
1523   struct pipe_surface *dest_surface = NULL;
1524   GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1525
1526   /* make sure finalize_textures has been called?
1527    */
1528   if (0) st_validate_state(st);
1529
1530   /* determine if copying depth or color data */
1531   if (texBaseFormat == GL_DEPTH_COMPONENT ||
1532       texBaseFormat == GL_DEPTH_STENCIL) {
1533      strb = st_renderbuffer(fb->_DepthBuffer);
1534      if (strb->Base.Wrapped) {
1535         strb = st_renderbuffer(strb->Base.Wrapped);
1536      }
1537   }
1538   else {
1539      /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1540      strb = st_renderbuffer(fb->_ColorReadBuffer);
1541   }
1542
1543   if (!strb || !strb->surface || !stImage->pt) {
1544      debug_printf("%s: null strb or stImage\n", __FUNCTION__);
1545      return;
1546   }
1547
1548   sample_count = strb->surface->texture->nr_samples;
1549   /* I believe this would be legal, presumably would need to do a resolve
1550      for color, and for depth/stencil spec says to just use one of the
1551      depth/stencil samples per pixel? Need some transfer clarifications. */
1552   assert(sample_count < 2);
1553
1554   if (srcX < 0) {
1555      width -= -srcX;
1556      destX += -srcX;
1557      srcX = 0;
1558   }
1559
1560   if (srcY < 0) {
1561      height -= -srcY;
1562      destY += -srcY;
1563      srcY = 0;
1564   }
1565
1566   if (destX < 0) {
1567      width -= -destX;
1568      srcX += -destX;
1569      destX = 0;
1570   }
1571
1572   if (destY < 0) {
1573      height -= -destY;
1574      srcY += -destY;
1575      destY = 0;
1576   }
1577
1578   if (width < 0 || height < 0)
1579      return;
1580
1581
1582   assert(strb);
1583   assert(strb->surface);
1584   assert(stImage->pt);
1585
1586   src_format = strb->surface->format;
1587   dest_format = stImage->pt->format;
1588
1589   /*
1590    * Determine if the src framebuffer and dest texture have the same
1591    * base format.  We need this to detect a case such as the framebuffer
1592    * being GL_RGBA but the texture being GL_RGB.  If the actual hardware
1593    * texture format stores RGBA we need to set A=1 (overriding the
1594    * framebuffer's alpha values).  We can't do that with the blit or
1595    * textured-quad paths.
1596    */
1597   matching_base_formats =
1598      (_mesa_get_format_base_format(strb->Base.Format) ==
1599       _mesa_get_format_base_format(texImage->TexFormat));
1600   format_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
1601
1602   if (ctx->_ImageTransferState == 0x0) {
1603
1604      if (matching_base_formats &&
1605          src_format == dest_format &&
1606          !do_flip)
1607      {
1608         /* use surface_copy() / blit */
1609         struct pipe_box src_box;
1610         u_box_2d_zslice(srcX, srcY, strb->surface->u.tex.first_layer,
1611                         width, height, &src_box);
1612
1613         /* for resource_copy_region(), y=0=top, always */
1614         pipe->resource_copy_region(pipe,
1615                                    /* dest */
1616                                    stImage->pt,
1617                                    stImage->level,
1618                                    destX, destY, destZ + stImage->face,
1619                                    /* src */
1620                                    strb->texture,
1621                                    strb->surface->u.tex.level,
1622                                    &src_box);
1623         use_fallback = GL_FALSE;
1624      }
1625      else if (format_writemask &&
1626               texBaseFormat != GL_DEPTH_COMPONENT &&
1627               texBaseFormat != GL_DEPTH_STENCIL &&
1628               screen->is_format_supported(screen, src_format,
1629                                           PIPE_TEXTURE_2D, sample_count,
1630                                           PIPE_BIND_SAMPLER_VIEW,
1631                                           0) &&
1632               screen->is_format_supported(screen, dest_format,
1633                                           PIPE_TEXTURE_2D, 0,
1634                                           PIPE_BIND_RENDER_TARGET,
1635                                           0)) {
1636         /* draw textured quad to do the copy */
1637         GLint srcY0, srcY1;
1638         struct pipe_surface surf_tmpl;
1639         memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1640         surf_tmpl.format = stImage->pt->format;
1641         surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
1642         surf_tmpl.u.tex.level = stImage->level;
1643         surf_tmpl.u.tex.first_layer = stImage->face + destZ;
1644         surf_tmpl.u.tex.last_layer = stImage->face + destZ;
1645
1646         dest_surface = pipe->create_surface(pipe, stImage->pt,
1647                                             &surf_tmpl);
1648
1649         if (do_flip) {
1650            srcY1 = strb->Base.Height - srcY - height;
1651            srcY0 = srcY1 + height;
1652         }
1653         else {
1654            srcY0 = srcY;
1655            srcY1 = srcY0 + height;
1656         }
1657
1658         util_blit_pixels_writemask(st->blit,
1659                                    strb->texture,
1660                                    strb->surface->u.tex.level,
1661                                    srcX, srcY0,
1662                                    srcX + width, srcY1,
1663                                    strb->surface->u.tex.first_layer,
1664                                    dest_surface,
1665                                    destX, destY,
1666                                    destX + width, destY + height,
1667                                    0.0, PIPE_TEX_MIPFILTER_NEAREST,
1668                                    format_writemask);
1669         use_fallback = GL_FALSE;
1670      }
1671
1672      if (dest_surface)
1673         pipe_surface_reference(&dest_surface, NULL);
1674   }
1675
1676   if (use_fallback) {
1677      /* software fallback */
1678      fallback_copy_texsubimage(ctx, target, level,
1679                                strb, stImage, texBaseFormat,
1680                                destX, destY, destZ,
1681                                srcX, srcY, width, height);
1682   }
1683}
1684
1685
1686
1687static void
1688st_CopyTexImage1D(struct gl_context * ctx, GLenum target, GLint level,
1689                  GLenum internalFormat,
1690                  GLint x, GLint y, GLsizei width, GLint border)
1691{
1692   struct gl_texture_unit *texUnit =
1693      &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1694   struct gl_texture_object *texObj =
1695      _mesa_select_tex_object(ctx, texUnit, target);
1696   struct gl_texture_image *texImage =
1697      _mesa_select_tex_image(ctx, texObj, target, level);
1698
1699   /* Setup or redefine the texture object, texture and texture
1700    * image.  Don't populate yet.
1701    */
1702   ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
1703                          width, border,
1704                          GL_RGBA, CHAN_TYPE, NULL,
1705                          &ctx->DefaultPacking, texObj, texImage);
1706
1707   st_copy_texsubimage(ctx, target, level,
1708                       0, 0, 0,  /* destX,Y,Z */
1709                       x, y, width, 1);  /* src X, Y, size */
1710}
1711
1712
1713static void
1714st_CopyTexImage2D(struct gl_context * ctx, GLenum target, GLint level,
1715                  GLenum internalFormat,
1716                  GLint x, GLint y, GLsizei width, GLsizei height,
1717                  GLint border)
1718{
1719   struct gl_texture_unit *texUnit =
1720      &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1721   struct gl_texture_object *texObj =
1722      _mesa_select_tex_object(ctx, texUnit, target);
1723   struct gl_texture_image *texImage =
1724      _mesa_select_tex_image(ctx, texObj, target, level);
1725
1726   /* Setup or redefine the texture object, texture and texture
1727    * image.  Don't populate yet.
1728    */
1729   ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
1730                          width, height, border,
1731                          GL_RGBA, CHAN_TYPE, NULL,
1732                          &ctx->DefaultPacking, texObj, texImage);
1733
1734   st_copy_texsubimage(ctx, target, level,
1735                       0, 0, 0,  /* destX,Y,Z */
1736                       x, y, width, height);  /* src X, Y, size */
1737}
1738
1739
1740static void
1741st_CopyTexSubImage1D(struct gl_context * ctx, GLenum target, GLint level,
1742                     GLint xoffset, GLint x, GLint y, GLsizei width)
1743{
1744   const GLint yoffset = 0, zoffset = 0;
1745   const GLsizei height = 1;
1746   st_copy_texsubimage(ctx, target, level,
1747                       xoffset, yoffset, zoffset,  /* destX,Y,Z */
1748                       x, y, width, height);  /* src X, Y, size */
1749}
1750
1751
1752static void
1753st_CopyTexSubImage2D(struct gl_context * ctx, GLenum target, GLint level,
1754                     GLint xoffset, GLint yoffset,
1755                     GLint x, GLint y, GLsizei width, GLsizei height)
1756{
1757   const GLint zoffset = 0;
1758   st_copy_texsubimage(ctx, target, level,
1759                       xoffset, yoffset, zoffset,  /* destX,Y,Z */
1760                       x, y, width, height);  /* src X, Y, size */
1761}
1762
1763
1764static void
1765st_CopyTexSubImage3D(struct gl_context * ctx, GLenum target, GLint level,
1766                     GLint xoffset, GLint yoffset, GLint zoffset,
1767                     GLint x, GLint y, GLsizei width, GLsizei height)
1768{
1769   st_copy_texsubimage(ctx, target, level,
1770                       xoffset, yoffset, zoffset,  /* destX,Y,Z */
1771                       x, y, width, height);  /* src X, Y, size */
1772}
1773
1774
1775/**
1776 * Copy image data from stImage into the texture object 'stObj' at level
1777 * 'dstLevel'.
1778 */
1779static void
1780copy_image_data_to_texture(struct st_context *st,
1781			   struct st_texture_object *stObj,
1782                           GLuint dstLevel,
1783			   struct st_texture_image *stImage)
1784{
1785   /* debug checks */
1786   {
1787      const struct gl_texture_image *dstImage =
1788         stObj->base.Image[stImage->face][stImage->level];
1789      assert(dstImage);
1790      assert(dstImage->Width == stImage->base.Width);
1791      assert(dstImage->Height == stImage->base.Height);
1792      assert(dstImage->Depth == stImage->base.Depth);
1793   }
1794
1795   if (stImage->pt) {
1796      /* Copy potentially with the blitter:
1797       */
1798      st_texture_image_copy(st->pipe,
1799                            stObj->pt, dstLevel,  /* dest texture, level */
1800                            stImage->pt, stImage->level, /* src texture, level */
1801                            stImage->face);
1802
1803      pipe_resource_reference(&stImage->pt, NULL);
1804   }
1805   else if (stImage->base.Data) {
1806      st_texture_image_data(st,
1807                            stObj->pt,
1808                            stImage->face,
1809                            dstLevel,
1810                            stImage->base.Data,
1811                            stImage->base.RowStride *
1812                            util_format_get_blocksize(stObj->pt->format),
1813                            stImage->base.RowStride *
1814                            stImage->base.Height *
1815                            util_format_get_blocksize(stObj->pt->format));
1816      _mesa_align_free(stImage->base.Data);
1817      stImage->base.Data = NULL;
1818   }
1819
1820   pipe_resource_reference(&stImage->pt, stObj->pt);
1821}
1822
1823
1824/**
1825 * Called during state validation.  When this function is finished,
1826 * the texture object should be ready for rendering.
1827 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1828 */
1829GLboolean
1830st_finalize_texture(struct gl_context *ctx,
1831		    struct pipe_context *pipe,
1832		    struct gl_texture_object *tObj)
1833{
1834   struct st_context *st = st_context(ctx);
1835   struct st_texture_object *stObj = st_texture_object(tObj);
1836   const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1837   GLuint face;
1838   struct st_texture_image *firstImage;
1839   enum pipe_format firstImageFormat;
1840
1841   if (stObj->base._Complete) {
1842      /* The texture is complete and we know exactly how many mipmap levels
1843       * are present/needed.  This is conditional because we may be called
1844       * from the st_generate_mipmap() function when the texture object is
1845       * incomplete.  In that case, we'll have set stObj->lastLevel before
1846       * we get here.
1847       */
1848      if (stObj->base.MinFilter == GL_LINEAR ||
1849          stObj->base.MinFilter == GL_NEAREST)
1850         stObj->lastLevel = stObj->base.BaseLevel;
1851      else
1852         stObj->lastLevel = stObj->base._MaxLevel;
1853   }
1854
1855   firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1856   assert(firstImage);
1857
1858   /* If both firstImage and stObj point to a texture which can contain
1859    * all active images, favour firstImage.  Note that because of the
1860    * completeness requirement, we know that the image dimensions
1861    * will match.
1862    */
1863   if (firstImage->pt &&
1864       firstImage->pt != stObj->pt &&
1865       (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
1866      pipe_resource_reference(&stObj->pt, firstImage->pt);
1867      pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1868   }
1869
1870   /* Find gallium format for the Mesa texture */
1871   firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1872
1873   /* If we already have a gallium texture, check that it matches the texture
1874    * object's format, target, size, num_levels, etc.
1875    */
1876   if (stObj->pt) {
1877      if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1878          !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
1879          stObj->pt->last_level < stObj->lastLevel ||
1880          stObj->pt->width0 != stObj->width0 ||
1881          stObj->pt->height0 != stObj->height0 ||
1882          stObj->pt->depth0 != stObj->depth0)
1883      {
1884         /* The gallium texture does not match the Mesa texture so delete the
1885          * gallium texture now.  We'll make a new one below.
1886          */
1887         pipe_resource_reference(&stObj->pt, NULL);
1888         pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1889         st->dirty.st |= ST_NEW_FRAMEBUFFER;
1890      }
1891   }
1892
1893   /* May need to create a new gallium texture:
1894    */
1895   if (!stObj->pt) {
1896      GLuint bindings = default_bindings(st, firstImageFormat);
1897
1898      stObj->pt = st_texture_create(st,
1899                                    gl_target_to_pipe(stObj->base.Target),
1900                                    firstImageFormat,
1901                                    stObj->lastLevel,
1902                                    stObj->width0,
1903                                    stObj->height0,
1904                                    stObj->depth0,
1905                                    bindings);
1906
1907      if (!stObj->pt) {
1908         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1909         return GL_FALSE;
1910      }
1911   }
1912
1913   /* Pull in any images not in the object's texture:
1914    */
1915   for (face = 0; face < nr_faces; face++) {
1916      GLuint level;
1917      for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1918         struct st_texture_image *stImage =
1919            st_texture_image(stObj->base.Image[face][level]);
1920
1921         /* Need to import images in main memory or held in other textures.
1922          */
1923         if (stImage && stObj->pt != stImage->pt) {
1924            copy_image_data_to_texture(st, stObj, level, stImage);
1925         }
1926      }
1927   }
1928
1929   return GL_TRUE;
1930}
1931
1932
1933/**
1934 * Returns pointer to a default/dummy texture.
1935 * This is typically used when the current shader has tex/sample instructions
1936 * but the user has not provided a (any) texture(s).
1937 */
1938struct gl_texture_object *
1939st_get_default_texture(struct st_context *st)
1940{
1941   if (!st->default_texture) {
1942      static const GLenum target = GL_TEXTURE_2D;
1943      GLubyte pixels[16][16][4];
1944      struct gl_texture_object *texObj;
1945      struct gl_texture_image *texImg;
1946      GLuint i, j;
1947
1948      /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1949       * when attempting to sample incomplete textures.
1950       */
1951      for (i = 0; i < 16; i++) {
1952         for (j = 0; j < 16; j++) {
1953            pixels[i][j][0] = 0;
1954            pixels[i][j][1] = 0;
1955            pixels[i][j][2] = 0;
1956            pixels[i][j][3] = 255;
1957         }
1958      }
1959
1960      texObj = st->ctx->Driver.NewTextureObject(st->ctx, 0, target);
1961
1962      texImg = _mesa_get_tex_image(st->ctx, texObj, target, 0);
1963
1964      _mesa_init_teximage_fields(st->ctx, target, texImg,
1965                                 16, 16, 1, 0,  /* w, h, d, border */
1966                                 GL_RGBA, MESA_FORMAT_RGBA8888);
1967
1968      st_TexImage(st->ctx, 2, target,
1969                  0, GL_RGBA,    /* level, intformat */
1970                  16, 16, 1, 0,  /* w, h, d, border */
1971                  GL_RGBA, GL_UNSIGNED_BYTE, pixels,
1972                  &st->ctx->DefaultPacking,
1973                  texObj, texImg,
1974                  0, 0);
1975
1976      texObj->MinFilter = GL_NEAREST;
1977      texObj->MagFilter = GL_NEAREST;
1978      texObj->_Complete = GL_TRUE;
1979
1980      st->default_texture = texObj;
1981   }
1982   return st->default_texture;
1983}
1984
1985
1986void
1987st_init_texture_functions(struct dd_function_table *functions)
1988{
1989   functions->ChooseTextureFormat = st_ChooseTextureFormat;
1990   functions->TexImage1D = st_TexImage1D;
1991   functions->TexImage2D = st_TexImage2D;
1992   functions->TexImage3D = st_TexImage3D;
1993   functions->TexSubImage1D = st_TexSubImage1D;
1994   functions->TexSubImage2D = st_TexSubImage2D;
1995   functions->TexSubImage3D = st_TexSubImage3D;
1996   functions->CompressedTexSubImage1D = st_CompressedTexSubImage1D;
1997   functions->CompressedTexSubImage2D = st_CompressedTexSubImage2D;
1998   functions->CompressedTexSubImage3D = st_CompressedTexSubImage3D;
1999   functions->CopyTexImage1D = st_CopyTexImage1D;
2000   functions->CopyTexImage2D = st_CopyTexImage2D;
2001   functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
2002   functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
2003   functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
2004   functions->GenerateMipmap = st_generate_mipmap;
2005
2006   functions->GetTexImage = st_GetTexImage;
2007
2008   /* compressed texture functions */
2009   functions->CompressedTexImage2D = st_CompressedTexImage2D;
2010   functions->GetCompressedTexImage = st_GetCompressedTexImage;
2011
2012   functions->NewTextureObject = st_NewTextureObject;
2013   functions->NewTextureImage = st_NewTextureImage;
2014   functions->DeleteTexture = st_DeleteTextureObject;
2015   functions->FreeTexImageData = st_FreeTextureImageData;
2016
2017   functions->TextureMemCpy = do_memcpy;
2018
2019   /* XXX Temporary until we can query pipe's texture sizes */
2020   functions->TestProxyTexImage = _mesa_test_proxy_teximage;
2021}
2022