st_cb_texture.c revision 8e019506257b720a1a6ff668aef57bff445a1725
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/pbo.h"
39#include "main/pixeltransfer.h"
40#include "main/texcompress.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   case GL_TEXTURE_EXTERNAL_OES:
79      return PIPE_TEXTURE_2D;
80   case GL_TEXTURE_RECTANGLE_NV:
81      return PIPE_TEXTURE_RECT;
82   case GL_TEXTURE_3D:
83      return PIPE_TEXTURE_3D;
84   case GL_TEXTURE_CUBE_MAP_ARB:
85      return PIPE_TEXTURE_CUBE;
86   case GL_TEXTURE_1D_ARRAY_EXT:
87      return PIPE_TEXTURE_1D_ARRAY;
88   case GL_TEXTURE_2D_ARRAY_EXT:
89      return PIPE_TEXTURE_2D_ARRAY;
90   case GL_TEXTURE_BUFFER:
91      return PIPE_BUFFER;
92   default:
93      assert(0);
94      return 0;
95   }
96}
97
98
99/** called via ctx->Driver.NewTextureImage() */
100static struct gl_texture_image *
101st_NewTextureImage(struct gl_context * ctx)
102{
103   DBG("%s\n", __FUNCTION__);
104   (void) ctx;
105   return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
106}
107
108
109/** called via ctx->Driver.DeleteTextureImage() */
110static void
111st_DeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
112{
113   /* nothing special (yet) for st_texture_image */
114   _mesa_delete_texture_image(ctx, img);
115}
116
117
118/** called via ctx->Driver.NewTextureObject() */
119static struct gl_texture_object *
120st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
121{
122   struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
123
124   DBG("%s\n", __FUNCTION__);
125   _mesa_initialize_texture_object(&obj->base, name, target);
126
127   return &obj->base;
128}
129
130/** called via ctx->Driver.DeleteTextureObject() */
131static void
132st_DeleteTextureObject(struct gl_context *ctx,
133                       struct gl_texture_object *texObj)
134{
135   struct st_context *st = st_context(ctx);
136   struct st_texture_object *stObj = st_texture_object(texObj);
137   if (stObj->pt)
138      pipe_resource_reference(&stObj->pt, NULL);
139   if (stObj->sampler_view) {
140      pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
141   }
142   _mesa_delete_texture_object(ctx, texObj);
143}
144
145
146/** called via ctx->Driver.FreeTextureImageBuffer() */
147static void
148st_FreeTextureImageBuffer(struct gl_context *ctx,
149                          struct gl_texture_image *texImage)
150{
151   struct st_texture_image *stImage = st_texture_image(texImage);
152
153   DBG("%s\n", __FUNCTION__);
154
155   if (stImage->pt) {
156      pipe_resource_reference(&stImage->pt, NULL);
157   }
158
159   if (stImage->TexData) {
160      _mesa_align_free(stImage->TexData);
161      stImage->TexData = NULL;
162   }
163}
164
165
166/** called via ctx->Driver.MapTextureImage() */
167static void
168st_MapTextureImage(struct gl_context *ctx,
169                   struct gl_texture_image *texImage,
170                   GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h,
171                   GLbitfield mode,
172                   GLubyte **mapOut, GLint *rowStrideOut)
173{
174   struct st_context *st = st_context(ctx);
175   struct st_texture_image *stImage = st_texture_image(texImage);
176   unsigned pipeMode;
177   GLubyte *map;
178
179   pipeMode = 0x0;
180   if (mode & GL_MAP_READ_BIT)
181      pipeMode |= PIPE_TRANSFER_READ;
182   if (mode & GL_MAP_WRITE_BIT)
183      pipeMode |= PIPE_TRANSFER_WRITE;
184   if (mode & GL_MAP_INVALIDATE_RANGE_BIT)
185      pipeMode |= PIPE_TRANSFER_DISCARD_RANGE;
186
187   map = st_texture_image_map(st, stImage, slice, pipeMode, x, y, w, h);
188   if (map) {
189      *mapOut = map;
190      *rowStrideOut = stImage->transfer->stride;
191   }
192   else {
193      *mapOut = NULL;
194      *rowStrideOut = 0;
195   }
196}
197
198
199/** called via ctx->Driver.UnmapTextureImage() */
200static void
201st_UnmapTextureImage(struct gl_context *ctx,
202                     struct gl_texture_image *texImage,
203                     GLuint slice)
204{
205   struct st_context *st = st_context(ctx);
206   struct st_texture_image *stImage  = st_texture_image(texImage);
207   st_texture_image_unmap(st, stImage);
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   unsigned bindings;
220
221   if (util_format_is_depth_or_stencil(format))
222      bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
223   else
224      bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
225
226   if (screen->is_format_supported(screen, format, target, 0, bindings))
227      return bindings;
228   else {
229      /* Try non-sRGB. */
230      format = util_format_linear(format);
231
232      if (screen->is_format_supported(screen, format, target, 0, bindings))
233         return bindings;
234      else
235         return PIPE_BIND_SAMPLER_VIEW;
236   }
237}
238
239
240/** Return number of image dimensions (1, 2 or 3) for a texture target. */
241static GLuint
242get_texture_dims(GLenum target)
243{
244   switch (target) {
245   case GL_TEXTURE_1D:
246   case GL_TEXTURE_1D_ARRAY_EXT:
247   case GL_TEXTURE_BUFFER:
248      return 1;
249   case GL_TEXTURE_2D:
250   case GL_TEXTURE_CUBE_MAP_ARB:
251   case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
252   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
253   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
254   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
255   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
256   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
257   case GL_TEXTURE_RECTANGLE_NV:
258   case GL_TEXTURE_2D_ARRAY_EXT:
259   case GL_TEXTURE_EXTERNAL_OES:
260      return 2;
261   case GL_TEXTURE_3D:
262      return 3;
263   default:
264      assert(0 && "invalid texture target in get_texture_dims()");
265      return 1;
266   }
267}
268
269
270/**
271 * Given the size of a mipmap image, try to compute the size of the level=0
272 * mipmap image.
273 *
274 * Note that this isn't always accurate for odd-sized, non-POW textures.
275 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
276 *
277 * \return GL_TRUE for success, GL_FALSE for failure
278 */
279static GLboolean
280guess_base_level_size(GLenum target,
281                      GLuint width, GLuint height, GLuint depth, GLuint level,
282                      GLuint *width0, GLuint *height0, GLuint *depth0)
283{
284   const GLuint dims = get_texture_dims(target);
285
286   assert(width >= 1);
287   assert(height >= 1);
288   assert(depth >= 1);
289
290   if (level > 0) {
291      /* Depending on the image's size, we can't always make a guess here */
292      if ((dims >= 1 && width == 1) ||
293          (dims >= 2 && height == 1) ||
294          (dims >= 3 && depth == 1)) {
295         /* we can't determine the image size at level=0 */
296         return GL_FALSE;
297      }
298
299      /* grow the image size until we hit level = 0 */
300      while (level > 0) {
301         if (width > 1)
302            width <<= 1;
303         if (height > 1)
304            height <<= 1;
305         if (depth > 1)
306            depth <<= 1;
307         level--;
308      }
309   }
310
311   *width0 = width;
312   *height0 = height;
313   *depth0 = depth;
314
315   return GL_TRUE;
316}
317
318
319/**
320 * Try to allocate a pipe_resource object for the given st_texture_object.
321 *
322 * We use the given st_texture_image as a clue to determine the size of the
323 * mipmap image at level=0.
324 *
325 * \return GL_TRUE for success, GL_FALSE if out of memory.
326 */
327static GLboolean
328guess_and_alloc_texture(struct st_context *st,
329			struct st_texture_object *stObj,
330			const struct st_texture_image *stImage)
331{
332   GLuint lastLevel, width, height, depth;
333   GLuint bindings;
334   GLuint ptWidth, ptHeight, ptDepth, ptLayers;
335   enum pipe_format fmt;
336
337   DBG("%s\n", __FUNCTION__);
338
339   assert(!stObj->pt);
340
341   if (!guess_base_level_size(stObj->base.Target,
342                              stImage->base.Width2,
343                              stImage->base.Height2,
344                              stImage->base.Depth2,
345                              stImage->base.Level,
346                              &width, &height, &depth)) {
347      /* we can't determine the image size at level=0 */
348      stObj->width0 = stObj->height0 = stObj->depth0 = 0;
349      /* this is not an out of memory error */
350      return GL_TRUE;
351   }
352
353   /* At this point, (width x height x depth) is the expected size of
354    * the level=0 mipmap image.
355    */
356
357   /* Guess a reasonable value for lastLevel.  With OpenGL we have no
358    * idea how many mipmap levels will be in a texture until we start
359    * to render with it.  Make an educated guess here but be prepared
360    * to re-allocating a texture buffer with space for more (or fewer)
361    * mipmap levels later.
362    */
363   if ((stObj->base.Sampler.MinFilter == GL_NEAREST ||
364        stObj->base.Sampler.MinFilter == GL_LINEAR ||
365        stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
366        stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) &&
367       !stObj->base.GenerateMipmap &&
368       stImage->base.Level == 0) {
369      /* only alloc space for a single mipmap level */
370      lastLevel = 0;
371   }
372   else {
373      /* alloc space for a full mipmap */
374      GLuint l2width = util_logbase2(width);
375      GLuint l2height = util_logbase2(height);
376      GLuint l2depth = util_logbase2(depth);
377      lastLevel = MAX2(MAX2(l2width, l2height), l2depth);
378   }
379
380   /* Save the level=0 dimensions */
381   stObj->width0 = width;
382   stObj->height0 = height;
383   stObj->depth0 = depth;
384
385   fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat);
386
387   bindings = default_bindings(st, fmt);
388
389   st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
390                                   width, height, depth,
391                                   &ptWidth, &ptHeight, &ptDepth, &ptLayers);
392
393   stObj->pt = st_texture_create(st,
394                                 gl_target_to_pipe(stObj->base.Target),
395                                 fmt,
396                                 lastLevel,
397                                 ptWidth,
398                                 ptHeight,
399                                 ptDepth,
400                                 ptLayers,
401                                 bindings);
402
403   stObj->lastLevel = lastLevel;
404
405   DBG("%s returning %d\n", __FUNCTION__, (stObj->pt != NULL));
406
407   return stObj->pt != NULL;
408}
409
410
411/**
412 * Called via ctx->Driver.AllocTextureImageBuffer().
413 * If the texture object/buffer already has space for the indicated image,
414 * we're done.  Otherwise, allocate memory for the new texture image.
415 */
416static GLboolean
417st_AllocTextureImageBuffer(struct gl_context *ctx,
418                           struct gl_texture_image *texImage)
419{
420   struct st_context *st = st_context(ctx);
421   struct st_texture_image *stImage = st_texture_image(texImage);
422   struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
423   const GLuint level = texImage->Level;
424   GLuint width = texImage->Width;
425   GLuint height = texImage->Height;
426   GLuint depth = texImage->Depth;
427
428   DBG("%s\n", __FUNCTION__);
429
430   assert(!stImage->TexData);
431   assert(!stImage->pt); /* xxx this might be wrong */
432
433   /* Look if the parent texture object has space for this image */
434   if (stObj->pt &&
435       level <= stObj->pt->last_level &&
436       st_texture_match_image(stObj->pt, texImage)) {
437      /* this image will fit in the existing texture object's memory */
438      pipe_resource_reference(&stImage->pt, stObj->pt);
439      return GL_TRUE;
440   }
441
442   /* The parent texture object does not have space for this image */
443
444   pipe_resource_reference(&stObj->pt, NULL);
445   pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
446
447   if (!guess_and_alloc_texture(st, stObj, stImage)) {
448      /* Probably out of memory.
449       * Try flushing any pending rendering, then retry.
450       */
451      st_finish(st);
452      if (!guess_and_alloc_texture(st, stObj, stImage)) {
453         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
454         return GL_FALSE;
455      }
456   }
457
458   if (stObj->pt &&
459       st_texture_match_image(stObj->pt, texImage)) {
460      /* The image will live in the object's mipmap memory */
461      pipe_resource_reference(&stImage->pt, stObj->pt);
462      assert(stImage->pt);
463      return GL_TRUE;
464   }
465   else {
466      /* Create a new, temporary texture/resource/buffer to hold this
467       * one texture image.  Note that when we later access this image
468       * (either for mapping or copying) we'll want to always specify
469       * mipmap level=0, even if the image represents some other mipmap
470       * level.
471       */
472      enum pipe_format format =
473         st_mesa_format_to_pipe_format(texImage->TexFormat);
474      GLuint bindings = default_bindings(st, format);
475      GLuint ptWidth, ptHeight, ptDepth, ptLayers;
476
477      st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
478                                      width, height, depth,
479                                      &ptWidth, &ptHeight, &ptDepth, &ptLayers);
480
481      stImage->pt = st_texture_create(st,
482                                      gl_target_to_pipe(stObj->base.Target),
483                                      format,
484                                      0, /* lastLevel */
485                                      ptWidth,
486                                      ptHeight,
487                                      ptDepth,
488                                      ptLayers,
489                                      bindings);
490      return stImage->pt != NULL;
491   }
492}
493
494
495/**
496 * Preparation prior to glTexImage.  Basically check the 'surface_based'
497 * field and switch to a "normal" tex image if necessary.
498 */
499static void
500prep_teximage(struct gl_context *ctx, struct gl_texture_image *texImage,
501              GLenum format, GLenum type)
502{
503   struct gl_texture_object *texObj = texImage->TexObject;
504   struct st_texture_object *stObj = st_texture_object(texObj);
505
506   /* switch to "normal" */
507   if (stObj->surface_based) {
508      const GLenum target = texObj->Target;
509      const GLuint level = texImage->Level;
510      gl_format texFormat;
511
512      _mesa_clear_texture_object(ctx, texObj);
513      pipe_resource_reference(&stObj->pt, NULL);
514
515      /* oops, need to init this image again */
516      texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
517                                              texImage->InternalFormat, format,
518                                              type);
519
520      _mesa_init_teximage_fields(ctx, texImage,
521                                 texImage->Width, texImage->Height,
522                                 texImage->Depth, texImage->Border,
523                                 texImage->InternalFormat, texFormat);
524
525      stObj->surface_based = GL_FALSE;
526   }
527}
528
529
530static void
531st_TexImage(struct gl_context * ctx, GLuint dims,
532            struct gl_texture_image *texImage,
533            GLenum format, GLenum type, const void *pixels,
534            const struct gl_pixelstore_attrib *unpack)
535{
536   prep_teximage(ctx, texImage, format, type);
537   _mesa_store_teximage(ctx, dims, texImage, format, type, pixels, unpack);
538}
539
540
541static void
542st_CompressedTexImage(struct gl_context *ctx, GLuint dims,
543                      struct gl_texture_image *texImage,
544                      GLsizei imageSize, const GLvoid *data)
545{
546   prep_teximage(ctx, texImage, GL_NONE, GL_NONE);
547   _mesa_store_compressed_teximage(ctx, dims, texImage, imageSize, data);
548}
549
550
551
552/**
553 * glGetTexImage() helper: decompress a compressed texture by rendering
554 * a textured quad.  Store the results in the user's buffer.
555 */
556static void
557decompress_with_blit(struct gl_context * ctx,
558                     GLenum format, GLenum type, GLvoid *pixels,
559                     struct gl_texture_image *texImage)
560{
561   struct st_context *st = st_context(ctx);
562   struct pipe_context *pipe = st->pipe;
563   struct st_texture_image *stImage = st_texture_image(texImage);
564   struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
565   struct pipe_sampler_view *src_view;
566   const GLuint width = texImage->Width;
567   const GLuint height = texImage->Height;
568   struct pipe_surface *dst_surface;
569   struct pipe_resource *dst_texture;
570   struct pipe_transfer *tex_xfer;
571   unsigned bind = (PIPE_BIND_RENDER_TARGET | /* util_blit may choose to render */
572		    PIPE_BIND_TRANSFER_READ);
573
574   /* create temp / dest surface */
575   if (!util_create_rgba_surface(pipe, width, height, bind,
576                                 &dst_texture, &dst_surface)) {
577      _mesa_problem(ctx, "util_create_rgba_surface() failed "
578                    "in decompress_with_blit()");
579      return;
580   }
581
582   /* Disable conditional rendering. */
583   if (st->render_condition) {
584      pipe->render_condition(pipe, NULL, 0);
585   }
586
587   /* Create sampler view that limits fetches to the source mipmap level */
588   {
589      struct pipe_sampler_view sv_temp;
590
591      u_sampler_view_default_template(&sv_temp, stObj->pt, stObj->pt->format);
592
593      sv_temp.format = util_format_linear(sv_temp.format);
594      sv_temp.u.tex.first_level =
595      sv_temp.u.tex.last_level = texImage->Level;
596
597      src_view = pipe->create_sampler_view(pipe, stObj->pt, &sv_temp);
598      if (!src_view) {
599         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
600         return;
601      }
602   }
603
604   /* blit/render/decompress */
605   util_blit_pixels_tex(st->blit,
606                        src_view,      /* pipe_resource (src) */
607                        0, 0,             /* src x0, y0 */
608                        width, height,    /* src x1, y1 */
609                        dst_surface,      /* pipe_surface (dst) */
610                        0, 0,             /* dst x0, y0 */
611                        width, height,    /* dst x1, y1 */
612                        0.0,              /* z */
613                        PIPE_TEX_MIPFILTER_NEAREST);
614
615   /* Restore conditional rendering state. */
616   if (st->render_condition) {
617      pipe->render_condition(pipe, st->render_condition,
618                             st->condition_mode);
619   }
620
621   /* map the dst_surface so we can read from it */
622   tex_xfer = pipe_get_transfer(pipe,
623                                dst_texture, 0, 0,
624                                PIPE_TRANSFER_READ,
625                                0, 0, width, height);
626
627   pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
628
629   /* copy/pack data into user buffer */
630   if (_mesa_format_matches_format_and_type(stImage->base.TexFormat,
631                                            format, type,
632                                            ctx->Pack.SwapBytes)) {
633      /* memcpy */
634      const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format);
635      ubyte *map = pipe_transfer_map(pipe, tex_xfer);
636      GLuint row;
637      for (row = 0; row < height; row++) {
638         GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
639                                              height, format, type, row, 0);
640         memcpy(dest, map, bytesPerRow);
641         map += tex_xfer->stride;
642      }
643      pipe_transfer_unmap(pipe, tex_xfer);
644   }
645   else {
646      /* format translation via floats */
647      GLuint row;
648      enum pipe_format pformat = util_format_linear(dst_texture->format);
649      GLfloat *rgba;
650
651      rgba = (GLfloat *) malloc(width * 4 * sizeof(GLfloat));
652      if (!rgba) {
653         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
654         goto end;
655      }
656
657      for (row = 0; row < height; row++) {
658         const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
659         GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
660                                              height, format, type, row, 0);
661
662         if (ST_DEBUG & DEBUG_FALLBACK)
663            debug_printf("%s: fallback format translation\n", __FUNCTION__);
664
665         /* get float[4] rgba row from surface */
666         pipe_get_tile_rgba_format(pipe, tex_xfer, 0, row, width, 1,
667                                   pformat, rgba);
668
669         _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
670                                    type, dest, &ctx->Pack, transferOps);
671      }
672
673      free(rgba);
674   }
675
676end:
677   _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
678
679   pipe->transfer_destroy(pipe, tex_xfer);
680
681   /* destroy the temp / dest surface */
682   util_destroy_rgba_surface(dst_texture, dst_surface);
683
684   pipe_sampler_view_release(pipe, &src_view);
685}
686
687
688
689/**
690 * Called via ctx->Driver.GetTexImage()
691 */
692static void
693st_GetTexImage(struct gl_context * ctx,
694               GLenum format, GLenum type, GLvoid * pixels,
695               struct gl_texture_image *texImage)
696{
697   struct st_texture_image *stImage = st_texture_image(texImage);
698
699   if (stImage->pt && util_format_is_s3tc(stImage->pt->format)) {
700      /* Need to decompress the texture.
701       * We'll do this by rendering a textured quad (which is hopefully
702       * faster than using the fallback code in texcompress.c).
703       * Note that we only expect RGBA formats (no Z/depth formats).
704       */
705      decompress_with_blit(ctx, format, type, pixels, texImage);
706   }
707   else {
708      _mesa_get_teximage(ctx, format, type, pixels, texImage);
709   }
710}
711
712
713/**
714 * Do a CopyTexSubImage operation using a read transfer from the source,
715 * a write transfer to the destination and get_tile()/put_tile() to access
716 * the pixels/texels.
717 *
718 * Note: srcY=0=TOP of renderbuffer
719 */
720static void
721fallback_copy_texsubimage(struct gl_context *ctx,
722                          struct st_renderbuffer *strb,
723                          struct st_texture_image *stImage,
724                          GLenum baseFormat,
725                          GLint destX, GLint destY, GLint destZ,
726                          GLint srcX, GLint srcY,
727                          GLsizei width, GLsizei height)
728{
729   struct st_context *st = st_context(ctx);
730   struct pipe_context *pipe = st->pipe;
731   struct pipe_transfer *src_trans;
732   GLvoid *texDest;
733   enum pipe_transfer_usage transfer_usage;
734
735   if (ST_DEBUG & DEBUG_FALLBACK)
736      debug_printf("%s: fallback processing\n", __FUNCTION__);
737
738   if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
739      srcY = strb->Base.Height - srcY - height;
740   }
741
742   src_trans = pipe_get_transfer(pipe,
743                                 strb->texture,
744                                 strb->rtt_level,
745                                 strb->rtt_face + strb->rtt_slice,
746                                 PIPE_TRANSFER_READ,
747                                 srcX, srcY,
748                                 width, height);
749
750   if ((baseFormat == GL_DEPTH_COMPONENT ||
751        baseFormat == GL_DEPTH_STENCIL) &&
752       util_format_is_depth_and_stencil(stImage->pt->format))
753      transfer_usage = PIPE_TRANSFER_READ_WRITE;
754   else
755      transfer_usage = PIPE_TRANSFER_WRITE;
756
757   /* XXX this used to ignore destZ param */
758   texDest = st_texture_image_map(st, stImage, destZ, transfer_usage,
759                                  destX, destY, width, height);
760
761   if (baseFormat == GL_DEPTH_COMPONENT ||
762       baseFormat == GL_DEPTH_STENCIL) {
763      const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
764                                     ctx->Pixel.DepthBias != 0.0F);
765      GLint row, yStep;
766      uint *data;
767
768      /* determine bottom-to-top vs. top-to-bottom order for src buffer */
769      if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
770         srcY = height - 1;
771         yStep = -1;
772      }
773      else {
774         srcY = 0;
775         yStep = 1;
776      }
777
778      data = (uint *) malloc(width * sizeof(uint));
779
780      if (data) {
781         /* To avoid a large temp memory allocation, do copy row by row */
782         for (row = 0; row < height; row++, srcY += yStep) {
783            pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
784            if (scaleOrBias) {
785               _mesa_scale_and_bias_depth_uint(ctx, width, data);
786            }
787            pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
788         }
789      }
790      else {
791         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
792      }
793
794      free(data);
795   }
796   else {
797      /* RGBA format */
798      GLfloat *tempSrc =
799         (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
800
801      if (tempSrc && texDest) {
802         const GLint dims = 2;
803         const GLint dstRowStride = stImage->transfer->stride;
804         struct gl_texture_image *texImage = &stImage->base;
805         struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
806
807         if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
808            unpack.Invert = GL_TRUE;
809         }
810
811         /* get float/RGBA image from framebuffer */
812         /* XXX this usually involves a lot of int/float conversion.
813          * try to avoid that someday.
814          */
815         pipe_get_tile_rgba_format(pipe, src_trans, 0, 0, width, height,
816                                   util_format_linear(strb->texture->format),
817                                   tempSrc);
818
819         /* Store into texture memory.
820          * Note that this does some special things such as pixel transfer
821          * ops and format conversion.  In particular, if the dest tex format
822          * is actually RGBA but the user created the texture as GL_RGB we
823          * need to fill-in/override the alpha channel with 1.0.
824          */
825         _mesa_texstore(ctx, dims,
826                        texImage->_BaseFormat,
827                        texImage->TexFormat,
828                        dstRowStride,
829                        (GLubyte **) &texDest,
830                        width, height, 1,
831                        GL_RGBA, GL_FLOAT, tempSrc, /* src */
832                        &unpack);
833      }
834      else {
835         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
836      }
837
838      if (tempSrc)
839         free(tempSrc);
840   }
841
842   st_texture_image_unmap(st, stImage);
843   pipe->transfer_destroy(pipe, src_trans);
844}
845
846
847
848/**
849 * If the format of the src renderbuffer and the format of the dest
850 * texture are compatible (in terms of blitting), return a TGSI writemask
851 * to be used during the blit.
852 * If the src/dest are incompatible, return 0.
853 */
854static unsigned
855compatible_src_dst_formats(struct gl_context *ctx,
856                           const struct gl_renderbuffer *src,
857                           const struct gl_texture_image *dst)
858{
859   /* Get logical base formats for the src and dest.
860    * That is, use the user-requested formats and not the actual, device-
861    * chosen formats.
862    * For example, the user may have requested an A8 texture but the
863    * driver may actually be using an RGBA texture format.  When we
864    * copy/blit to that texture, we only want to copy the Alpha channel
865    * and not the RGB channels.
866    *
867    * Similarly, when the src FBO was created an RGB format may have been
868    * requested but the driver actually chose an RGBA format.  In that case,
869    * we don't want to copy the undefined Alpha channel to the dest texture
870    * (it should be 1.0).
871    */
872   const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
873   const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
874
875   /**
876    * XXX when we have red-only and red/green renderbuffers we'll need
877    * to add more cases here (or implement a general-purpose routine that
878    * queries the existance of the R,G,B,A channels in the src and dest).
879    */
880   if (srcFormat == dstFormat) {
881      /* This is the same as matching_base_formats, which should
882       * always pass, as it did previously.
883       */
884      return TGSI_WRITEMASK_XYZW;
885   }
886   else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
887      /* Make sure that A in the dest is 1.  The actual src format
888       * may be RGBA and have undefined A values.
889       */
890      return TGSI_WRITEMASK_XYZ;
891   }
892   else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
893      /* Make sure that A in the dest is 1.  The actual dst format
894       * may be RGBA and will need A=1 to provide proper alpha values
895       * when sampled later.
896       */
897      return TGSI_WRITEMASK_XYZ;
898   }
899   else {
900      if (ST_DEBUG & DEBUG_FALLBACK)
901         debug_printf("%s failed for src %s, dst %s\n",
902                      __FUNCTION__,
903                      _mesa_lookup_enum_by_nr(srcFormat),
904                      _mesa_lookup_enum_by_nr(dstFormat));
905
906      /* Otherwise fail.
907       */
908      return 0;
909   }
910}
911
912
913
914/**
915 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
916 * Note that the region to copy has already been clipped so we know we
917 * won't read from outside the source renderbuffer's bounds.
918 *
919 * Note: srcY=0=Bottom of renderbuffer (GL convention)
920 */
921static void
922st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
923                   struct gl_texture_image *texImage,
924                   GLint destX, GLint destY, GLint destZ,
925                   struct gl_renderbuffer *rb,
926                   GLint srcX, GLint srcY, GLsizei width, GLsizei height)
927{
928   struct st_texture_image *stImage = st_texture_image(texImage);
929   const GLenum texBaseFormat = texImage->_BaseFormat;
930   struct gl_framebuffer *fb = ctx->ReadBuffer;
931   struct st_renderbuffer *strb;
932   struct st_context *st = st_context(ctx);
933   struct pipe_context *pipe = st->pipe;
934   struct pipe_screen *screen = pipe->screen;
935   enum pipe_format dest_format, src_format;
936   GLboolean matching_base_formats;
937   GLuint color_writemask, zs_writemask, sample_count;
938   struct pipe_surface *dest_surface = NULL;
939   GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
940   struct pipe_surface surf_tmpl;
941   unsigned int dst_usage;
942   GLint srcY0, srcY1;
943
944   /* make sure finalize_textures has been called?
945    */
946   if (0) st_validate_state(st);
947
948   /* determine if copying depth or color data */
949   if (texBaseFormat == GL_DEPTH_COMPONENT ||
950       texBaseFormat == GL_DEPTH_STENCIL) {
951      strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
952   }
953   else {
954      /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
955      strb = st_renderbuffer(fb->_ColorReadBuffer);
956   }
957
958   if (!strb || !strb->surface || !stImage->pt) {
959      debug_printf("%s: null strb or stImage\n", __FUNCTION__);
960      return;
961   }
962
963   sample_count = strb->surface->texture->nr_samples;
964   /* I believe this would be legal, presumably would need to do a resolve
965      for color, and for depth/stencil spec says to just use one of the
966      depth/stencil samples per pixel? Need some transfer clarifications. */
967   assert(sample_count < 2);
968
969   assert(strb);
970   assert(strb->surface);
971   assert(stImage->pt);
972
973   src_format = strb->surface->format;
974   dest_format = stImage->pt->format;
975
976   /*
977    * Determine if the src framebuffer and dest texture have the same
978    * base format.  We need this to detect a case such as the framebuffer
979    * being GL_RGBA but the texture being GL_RGB.  If the actual hardware
980    * texture format stores RGBA we need to set A=1 (overriding the
981    * framebuffer's alpha values).  We can't do that with the blit or
982    * textured-quad paths.
983    */
984   matching_base_formats =
985      (_mesa_get_format_base_format(strb->Base.Format) ==
986       _mesa_get_format_base_format(texImage->TexFormat));
987
988   if (ctx->_ImageTransferState) {
989      goto fallback;
990   }
991
992   if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
993      /* 1D arrays might be thought of as 2D images but the actual layout
994       * might not be that way.  At some points, we convert OpenGL's 1D
995       * array 'height' into gallium 'layers' and that prevents the blit
996       * utility code from doing the right thing.  Simpy use the memcpy-based
997       * fallback.
998       */
999      goto fallback;
1000   }
1001
1002   if (matching_base_formats &&
1003       src_format == dest_format &&
1004       !do_flip) {
1005      /* use surface_copy() / blit */
1006      struct pipe_box src_box;
1007      unsigned dstLevel;
1008
1009      u_box_2d_zslice(srcX, srcY, strb->surface->u.tex.first_layer,
1010                      width, height, &src_box);
1011
1012      /* If stImage->pt is an independent image (not a pointer into a full
1013       * mipmap) stImage->pt.last_level will be zero and we need to use that
1014       * as the dest level.
1015       */
1016      dstLevel = MIN2(stImage->base.Level, stImage->pt->last_level);
1017
1018      /* for resource_copy_region(), y=0=top, always */
1019      pipe->resource_copy_region(pipe,
1020                                 /* dest */
1021                                 stImage->pt,
1022                                 dstLevel,
1023                                 destX, destY, destZ + stImage->base.Face,
1024                                 /* src */
1025                                 strb->texture,
1026                                 strb->surface->u.tex.level,
1027                                 &src_box);
1028      return;
1029   }
1030
1031   if (texBaseFormat == GL_DEPTH_STENCIL) {
1032      goto fallback;
1033   }
1034
1035   if (texBaseFormat == GL_DEPTH_COMPONENT) {
1036      color_writemask = 0;
1037      zs_writemask = BLIT_WRITEMASK_Z;
1038      dst_usage = PIPE_BIND_DEPTH_STENCIL;
1039   }
1040   else {
1041      color_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
1042      zs_writemask = 0;
1043      dst_usage = PIPE_BIND_RENDER_TARGET;
1044   }
1045
1046   if ((!color_writemask && !zs_writemask) ||
1047       !screen->is_format_supported(screen, src_format,
1048                                    PIPE_TEXTURE_2D, sample_count,
1049                                    PIPE_BIND_SAMPLER_VIEW) ||
1050       !screen->is_format_supported(screen, dest_format,
1051                                    PIPE_TEXTURE_2D, 0,
1052                                    dst_usage)) {
1053      goto fallback;
1054   }
1055
1056   if (do_flip) {
1057      srcY1 = strb->Base.Height - srcY - height;
1058      srcY0 = srcY1 + height;
1059   }
1060   else {
1061      srcY0 = srcY;
1062      srcY1 = srcY0 + height;
1063   }
1064
1065   /* Disable conditional rendering. */
1066   if (st->render_condition) {
1067      pipe->render_condition(pipe, NULL, 0);
1068   }
1069
1070   memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1071   surf_tmpl.format = util_format_linear(stImage->pt->format);
1072   surf_tmpl.usage = dst_usage;
1073   surf_tmpl.u.tex.level = stImage->base.Level;
1074   surf_tmpl.u.tex.first_layer = stImage->base.Face + destZ;
1075   surf_tmpl.u.tex.last_layer = stImage->base.Face + destZ;
1076
1077   dest_surface = pipe->create_surface(pipe, stImage->pt,
1078                                       &surf_tmpl);
1079   util_blit_pixels(st->blit,
1080                    strb->texture,
1081                    strb->surface->u.tex.level,
1082                    srcX, srcY0,
1083                    srcX + width, srcY1,
1084                    strb->surface->u.tex.first_layer,
1085                    dest_surface,
1086                    destX, destY,
1087                    destX + width, destY + height,
1088                    0.0, PIPE_TEX_MIPFILTER_NEAREST,
1089                    color_writemask, zs_writemask);
1090   pipe_surface_reference(&dest_surface, NULL);
1091
1092   /* Restore conditional rendering state. */
1093   if (st->render_condition) {
1094      pipe->render_condition(pipe, st->render_condition,
1095                             st->condition_mode);
1096   }
1097
1098   return;
1099
1100fallback:
1101   /* software fallback */
1102   fallback_copy_texsubimage(ctx,
1103                             strb, stImage, texBaseFormat,
1104                             destX, destY, destZ,
1105                             srcX, srcY, width, height);
1106}
1107
1108
1109/**
1110 * Copy image data from stImage into the texture object 'stObj' at level
1111 * 'dstLevel'.
1112 */
1113static void
1114copy_image_data_to_texture(struct st_context *st,
1115			   struct st_texture_object *stObj,
1116                           GLuint dstLevel,
1117			   struct st_texture_image *stImage)
1118{
1119   /* debug checks */
1120   {
1121      const struct gl_texture_image *dstImage =
1122         stObj->base.Image[stImage->base.Face][dstLevel];
1123      assert(dstImage);
1124      assert(dstImage->Width == stImage->base.Width);
1125      assert(dstImage->Height == stImage->base.Height);
1126      assert(dstImage->Depth == stImage->base.Depth);
1127   }
1128
1129   if (stImage->pt) {
1130      /* Copy potentially with the blitter:
1131       */
1132      GLuint src_level;
1133      if (stImage->pt->last_level == 0)
1134         src_level = 0;
1135      else
1136         src_level = stImage->base.Level;
1137
1138      assert(src_level <= stImage->pt->last_level);
1139      assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width);
1140      assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY ||
1141             u_minify(stImage->pt->height0, src_level) == stImage->base.Height);
1142      assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY ||
1143             u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth);
1144
1145      st_texture_image_copy(st->pipe,
1146                            stObj->pt, dstLevel,  /* dest texture, level */
1147                            stImage->pt, src_level, /* src texture, level */
1148                            stImage->base.Face);
1149
1150      pipe_resource_reference(&stImage->pt, NULL);
1151   }
1152   else if (stImage->TexData) {
1153      /* Copy from malloc'd memory */
1154      /* XXX this should be re-examined/tested with a compressed format */
1155      GLuint blockSize = util_format_get_blocksize(stObj->pt->format);
1156      GLuint srcRowStride = stImage->base.Width * blockSize;
1157      GLuint srcSliceStride = stImage->base.Height * srcRowStride;
1158      st_texture_image_data(st,
1159                            stObj->pt,
1160                            stImage->base.Face,
1161                            dstLevel,
1162                            stImage->TexData,
1163                            srcRowStride,
1164                            srcSliceStride);
1165      _mesa_align_free(stImage->TexData);
1166      stImage->TexData = NULL;
1167   }
1168
1169   pipe_resource_reference(&stImage->pt, stObj->pt);
1170}
1171
1172
1173/**
1174 * Called during state validation.  When this function is finished,
1175 * the texture object should be ready for rendering.
1176 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1177 */
1178GLboolean
1179st_finalize_texture(struct gl_context *ctx,
1180		    struct pipe_context *pipe,
1181		    struct gl_texture_object *tObj)
1182{
1183   struct st_context *st = st_context(ctx);
1184   struct st_texture_object *stObj = st_texture_object(tObj);
1185   const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1186   GLuint face;
1187   struct st_texture_image *firstImage;
1188   enum pipe_format firstImageFormat;
1189   GLuint ptWidth, ptHeight, ptDepth, ptLayers;
1190
1191   if (_mesa_is_texture_complete(tObj, &tObj->Sampler)) {
1192      /* The texture is complete and we know exactly how many mipmap levels
1193       * are present/needed.  This is conditional because we may be called
1194       * from the st_generate_mipmap() function when the texture object is
1195       * incomplete.  In that case, we'll have set stObj->lastLevel before
1196       * we get here.
1197       */
1198      if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
1199          stObj->base.Sampler.MinFilter == GL_NEAREST)
1200         stObj->lastLevel = stObj->base.BaseLevel;
1201      else
1202         stObj->lastLevel = stObj->base._MaxLevel;
1203   }
1204
1205   firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1206   assert(firstImage);
1207
1208   /* If both firstImage and stObj point to a texture which can contain
1209    * all active images, favour firstImage.  Note that because of the
1210    * completeness requirement, we know that the image dimensions
1211    * will match.
1212    */
1213   if (firstImage->pt &&
1214       firstImage->pt != stObj->pt &&
1215       (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
1216      pipe_resource_reference(&stObj->pt, firstImage->pt);
1217      pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
1218   }
1219
1220   /* Find gallium format for the Mesa texture */
1221   firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1222
1223   /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1224   {
1225      GLuint width, height, depth;
1226      if (!guess_base_level_size(stObj->base.Target,
1227                                 firstImage->base.Width2,
1228                                 firstImage->base.Height2,
1229                                 firstImage->base.Depth2,
1230                                 firstImage->base.Level,
1231                                 &width, &height, &depth)) {
1232         width = stObj->width0;
1233         height = stObj->height0;
1234         depth = stObj->depth0;
1235      }
1236      /* convert GL dims to Gallium dims */
1237      st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
1238                                      &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1239   }
1240
1241   /* If we already have a gallium texture, check that it matches the texture
1242    * object's format, target, size, num_levels, etc.
1243    */
1244   if (stObj->pt) {
1245      if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1246          !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
1247          stObj->pt->last_level < stObj->lastLevel ||
1248          stObj->pt->width0 != ptWidth ||
1249          stObj->pt->height0 != ptHeight ||
1250          stObj->pt->depth0 != ptDepth ||
1251          stObj->pt->array_size != ptLayers)
1252      {
1253         /* The gallium texture does not match the Mesa texture so delete the
1254          * gallium texture now.  We'll make a new one below.
1255          */
1256         pipe_resource_reference(&stObj->pt, NULL);
1257         pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
1258         st->dirty.st |= ST_NEW_FRAMEBUFFER;
1259      }
1260   }
1261
1262   /* May need to create a new gallium texture:
1263    */
1264   if (!stObj->pt) {
1265      GLuint bindings = default_bindings(st, firstImageFormat);
1266
1267      stObj->pt = st_texture_create(st,
1268                                    gl_target_to_pipe(stObj->base.Target),
1269                                    firstImageFormat,
1270                                    stObj->lastLevel,
1271                                    ptWidth,
1272                                    ptHeight,
1273                                    ptDepth,
1274                                    ptLayers,
1275                                    bindings);
1276
1277      if (!stObj->pt) {
1278         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1279         return GL_FALSE;
1280      }
1281   }
1282
1283   /* Pull in any images not in the object's texture:
1284    */
1285   for (face = 0; face < nr_faces; face++) {
1286      GLuint level;
1287      for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1288         struct st_texture_image *stImage =
1289            st_texture_image(stObj->base.Image[face][level]);
1290
1291         /* Need to import images in main memory or held in other textures.
1292          */
1293         if (stImage && stObj->pt != stImage->pt) {
1294            if (level == 0 ||
1295                (stImage->base.Width == u_minify(stObj->width0, level) &&
1296                 stImage->base.Height == u_minify(stObj->height0, level) &&
1297                 stImage->base.Depth == u_minify(stObj->depth0, level))) {
1298               /* src image fits expected dest mipmap level size */
1299               copy_image_data_to_texture(st, stObj, level, stImage);
1300            }
1301         }
1302      }
1303   }
1304
1305   return GL_TRUE;
1306}
1307
1308
1309/**
1310 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1311 * for a whole mipmap stack.
1312 */
1313static GLboolean
1314st_AllocTextureStorage(struct gl_context *ctx,
1315                       struct gl_texture_object *texObj,
1316                       GLsizei levels, GLsizei width,
1317                       GLsizei height, GLsizei depth)
1318{
1319   const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
1320   struct st_context *st = st_context(ctx);
1321   struct st_texture_object *stObj = st_texture_object(texObj);
1322   GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
1323   enum pipe_format fmt;
1324   GLint level;
1325
1326   assert(levels > 0);
1327
1328   /* Save the level=0 dimensions */
1329   stObj->width0 = width;
1330   stObj->height0 = height;
1331   stObj->depth0 = depth;
1332   stObj->lastLevel = levels - 1;
1333
1334   fmt = st_mesa_format_to_pipe_format(texObj->Image[0][0]->TexFormat);
1335
1336   bindings = default_bindings(st, fmt);
1337
1338   st_gl_texture_dims_to_pipe_dims(texObj->Target,
1339                                   width, height, depth,
1340                                   &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1341
1342   stObj->pt = st_texture_create(st,
1343                                 gl_target_to_pipe(texObj->Target),
1344                                 fmt,
1345                                 levels,
1346                                 ptWidth,
1347                                 ptHeight,
1348                                 ptDepth,
1349                                 ptLayers,
1350                                 bindings);
1351   if (!stObj->pt)
1352      return GL_FALSE;
1353
1354   /* Set image resource pointers */
1355   for (level = 0; level < levels; level++) {
1356      GLuint face;
1357      for (face = 0; face < numFaces; face++) {
1358         struct st_texture_image *stImage =
1359            st_texture_image(texObj->Image[face][level]);
1360         pipe_resource_reference(&stImage->pt, stObj->pt);
1361      }
1362   }
1363
1364   return GL_TRUE;
1365}
1366
1367
1368
1369void
1370st_init_texture_functions(struct dd_function_table *functions)
1371{
1372   functions->ChooseTextureFormat = st_ChooseTextureFormat;
1373   functions->TexImage = st_TexImage;
1374   functions->TexSubImage = _mesa_store_texsubimage;
1375   functions->CompressedTexSubImage = _mesa_store_compressed_texsubimage;
1376   functions->CopyTexSubImage = st_CopyTexSubImage;
1377   functions->GenerateMipmap = st_generate_mipmap;
1378
1379   functions->GetTexImage = st_GetTexImage;
1380
1381   /* compressed texture functions */
1382   functions->CompressedTexImage = st_CompressedTexImage;
1383   functions->GetCompressedTexImage = _mesa_get_compressed_teximage;
1384
1385   functions->NewTextureObject = st_NewTextureObject;
1386   functions->NewTextureImage = st_NewTextureImage;
1387   functions->DeleteTextureImage = st_DeleteTextureImage;
1388   functions->DeleteTexture = st_DeleteTextureObject;
1389   functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
1390   functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
1391   functions->MapTextureImage = st_MapTextureImage;
1392   functions->UnmapTextureImage = st_UnmapTextureImage;
1393
1394   /* XXX Temporary until we can query pipe's texture sizes */
1395   functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1396
1397   functions->AllocTextureStorage = st_AllocTextureStorage;
1398}
1399