st_cb_texture.c revision f4a93e0665881dd58a95abb6525676bd1cc2e6af
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   DBG("%s returning %d\n", __FUNCTION__, (stObj->pt != NULL));
404
405   return stObj->pt != NULL;
406}
407
408
409/**
410 * Called via ctx->Driver.AllocTextureImageBuffer().
411 * If the texture object/buffer already has space for the indicated image,
412 * we're done.  Otherwise, allocate memory for the new texture image.
413 */
414static GLboolean
415st_AllocTextureImageBuffer(struct gl_context *ctx,
416                           struct gl_texture_image *texImage,
417                           gl_format format, GLsizei width,
418                           GLsizei height, GLsizei depth)
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
425   DBG("%s\n", __FUNCTION__);
426
427   assert(width > 0);
428   assert(height > 0);
429   assert(depth > 0);
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              GLint internalFormat,
502              GLint width, GLint height, GLint depth, GLint border,
503              GLenum format, GLenum type)
504{
505   struct gl_texture_object *texObj = texImage->TexObject;
506   struct st_texture_object *stObj = st_texture_object(texObj);
507
508   /* switch to "normal" */
509   if (stObj->surface_based) {
510      const GLenum target = texObj->Target;
511      const GLuint level = texImage->Level;
512      gl_format texFormat;
513
514      _mesa_clear_texture_object(ctx, texObj);
515      pipe_resource_reference(&stObj->pt, NULL);
516
517      /* oops, need to init this image again */
518      texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
519                                              internalFormat, format, type);
520
521      _mesa_init_teximage_fields(ctx, texImage,
522                                 width, height, depth, border,
523                                 internalFormat, texFormat);
524
525      stObj->surface_based = GL_FALSE;
526   }
527}
528
529
530static void
531st_TexImage3D(struct gl_context * ctx,
532              struct gl_texture_image *texImage,
533              GLint internalFormat,
534              GLint width, GLint height, GLint depth,
535              GLint border,
536              GLenum format, GLenum type, const void *pixels,
537              const struct gl_pixelstore_attrib *unpack)
538{
539   prep_teximage(ctx, texImage, internalFormat, width, height, depth, border,
540                 format, type);
541   _mesa_store_teximage3d(ctx, texImage, internalFormat, width, height, depth,
542                          border, format, type, pixels, unpack);
543}
544
545
546static void
547st_TexImage2D(struct gl_context * ctx,
548              struct gl_texture_image *texImage,
549              GLint internalFormat,
550              GLint width, GLint height, GLint border,
551              GLenum format, GLenum type, const void *pixels,
552              const struct gl_pixelstore_attrib *unpack)
553{
554   prep_teximage(ctx, texImage, internalFormat, width, height, 1, border,
555                 format, type);
556   _mesa_store_teximage2d(ctx, texImage, internalFormat, width, height,
557                          border, format, type, pixels, unpack);
558}
559
560
561static void
562st_TexImage1D(struct gl_context * ctx,
563              struct gl_texture_image *texImage,
564              GLint internalFormat,
565              GLint width, GLint border,
566              GLenum format, GLenum type, const void *pixels,
567              const struct gl_pixelstore_attrib *unpack)
568{
569   prep_teximage(ctx, texImage, internalFormat, width, 1, 1, border,
570                 format, type);
571   _mesa_store_teximage1d(ctx, texImage, internalFormat, width,
572                          border, format, type, pixels, unpack);
573}
574
575
576static void
577st_CompressedTexImage2D(struct gl_context *ctx,
578                        struct gl_texture_image *texImage,
579                        GLint internalFormat,
580                        GLint width, GLint height, GLint border,
581                        GLsizei imageSize, const GLvoid *data)
582{
583   prep_teximage(ctx, texImage, internalFormat, width, 1, 1, border,
584                 GL_NONE, GL_NONE);
585   _mesa_store_compressed_teximage2d(ctx, texImage, internalFormat, width,
586                                     height, border, imageSize, data);
587}
588
589
590
591/**
592 * glGetTexImage() helper: decompress a compressed texture by rendering
593 * a textured quad.  Store the results in the user's buffer.
594 */
595static void
596decompress_with_blit(struct gl_context * ctx,
597                     GLenum format, GLenum type, GLvoid *pixels,
598                     struct gl_texture_image *texImage)
599{
600   struct st_context *st = st_context(ctx);
601   struct pipe_context *pipe = st->pipe;
602   struct st_texture_image *stImage = st_texture_image(texImage);
603   struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
604   struct pipe_sampler_view *src_view;
605   const GLuint width = texImage->Width;
606   const GLuint height = texImage->Height;
607   struct pipe_surface *dst_surface;
608   struct pipe_resource *dst_texture;
609   struct pipe_transfer *tex_xfer;
610   unsigned bind = (PIPE_BIND_RENDER_TARGET | /* util_blit may choose to render */
611		    PIPE_BIND_TRANSFER_READ);
612
613   /* create temp / dest surface */
614   if (!util_create_rgba_surface(pipe, width, height, bind,
615                                 &dst_texture, &dst_surface)) {
616      _mesa_problem(ctx, "util_create_rgba_surface() failed "
617                    "in decompress_with_blit()");
618      return;
619   }
620
621   /* Disable conditional rendering. */
622   if (st->render_condition) {
623      pipe->render_condition(pipe, NULL, 0);
624   }
625
626   /* Create sampler view that limits fetches to the source mipmap level */
627   {
628      struct pipe_sampler_view sv_temp;
629
630      u_sampler_view_default_template(&sv_temp, stObj->pt, stObj->pt->format);
631
632      sv_temp.u.tex.first_level =
633      sv_temp.u.tex.last_level = texImage->Level;
634
635      src_view = pipe->create_sampler_view(pipe, stObj->pt, &sv_temp);
636      if (!src_view) {
637         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
638         return;
639      }
640   }
641
642   /* blit/render/decompress */
643   util_blit_pixels_tex(st->blit,
644                        src_view,      /* pipe_resource (src) */
645                        0, 0,             /* src x0, y0 */
646                        width, height,    /* src x1, y1 */
647                        dst_surface,      /* pipe_surface (dst) */
648                        0, 0,             /* dst x0, y0 */
649                        width, height,    /* dst x1, y1 */
650                        0.0,              /* z */
651                        PIPE_TEX_MIPFILTER_NEAREST);
652
653   /* Restore conditional rendering state. */
654   if (st->render_condition) {
655      pipe->render_condition(pipe, st->render_condition,
656                             st->condition_mode);
657   }
658
659   /* map the dst_surface so we can read from it */
660   tex_xfer = pipe_get_transfer(pipe,
661                                dst_texture, 0, 0,
662                                PIPE_TRANSFER_READ,
663                                0, 0, width, height);
664
665   pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
666
667   /* copy/pack data into user buffer */
668   if (_mesa_format_matches_format_and_type(stImage->base.TexFormat,
669                                            format, type,
670                                            ctx->Pack.SwapBytes)) {
671      /* memcpy */
672      const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format);
673      ubyte *map = pipe_transfer_map(pipe, tex_xfer);
674      GLuint row;
675      for (row = 0; row < height; row++) {
676         GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
677                                              height, format, type, row, 0);
678         memcpy(dest, map, bytesPerRow);
679         map += tex_xfer->stride;
680      }
681      pipe_transfer_unmap(pipe, tex_xfer);
682   }
683   else {
684      /* format translation via floats */
685      GLuint row;
686      enum pipe_format pformat = util_format_linear(dst_texture->format);
687      GLfloat *rgba;
688
689      rgba = (GLfloat *) malloc(width * 4 * sizeof(GLfloat));
690      if (!rgba) {
691         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
692         goto end;
693      }
694
695      for (row = 0; row < height; row++) {
696         const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
697         GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
698                                              height, format, type, row, 0);
699
700         if (ST_DEBUG & DEBUG_FALLBACK)
701            debug_printf("%s: fallback format translation\n", __FUNCTION__);
702
703         /* get float[4] rgba row from surface */
704         pipe_get_tile_rgba_format(pipe, tex_xfer, 0, row, width, 1,
705                                   pformat, rgba);
706
707         _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
708                                    type, dest, &ctx->Pack, transferOps);
709      }
710
711      free(rgba);
712   }
713
714end:
715   _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
716
717   pipe->transfer_destroy(pipe, tex_xfer);
718
719   /* destroy the temp / dest surface */
720   util_destroy_rgba_surface(dst_texture, dst_surface);
721
722   pipe_sampler_view_release(pipe, &src_view);
723}
724
725
726
727/**
728 * Called via ctx->Driver.GetTexImage()
729 */
730static void
731st_GetTexImage(struct gl_context * ctx,
732               GLenum format, GLenum type, GLvoid * pixels,
733               struct gl_texture_image *texImage)
734{
735   struct st_texture_image *stImage = st_texture_image(texImage);
736
737   if (stImage->pt && util_format_is_s3tc(stImage->pt->format)) {
738      /* Need to decompress the texture.
739       * We'll do this by rendering a textured quad (which is hopefully
740       * faster than using the fallback code in texcompress.c).
741       * Note that we only expect RGBA formats (no Z/depth formats).
742       */
743      decompress_with_blit(ctx, format, type, pixels, texImage);
744   }
745   else {
746      _mesa_get_teximage(ctx, format, type, pixels, texImage);
747   }
748}
749
750
751/**
752 * Do a CopyTexSubImage operation using a read transfer from the source,
753 * a write transfer to the destination and get_tile()/put_tile() to access
754 * the pixels/texels.
755 *
756 * Note: srcY=0=TOP of renderbuffer
757 */
758static void
759fallback_copy_texsubimage(struct gl_context *ctx,
760                          struct st_renderbuffer *strb,
761                          struct st_texture_image *stImage,
762                          GLenum baseFormat,
763                          GLint destX, GLint destY, GLint destZ,
764                          GLint srcX, GLint srcY,
765                          GLsizei width, GLsizei height)
766{
767   struct st_context *st = st_context(ctx);
768   struct pipe_context *pipe = st->pipe;
769   struct pipe_transfer *src_trans;
770   GLvoid *texDest;
771   enum pipe_transfer_usage transfer_usage;
772
773   if (ST_DEBUG & DEBUG_FALLBACK)
774      debug_printf("%s: fallback processing\n", __FUNCTION__);
775
776   if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
777      srcY = strb->Base.Height - srcY - height;
778   }
779
780   src_trans = pipe_get_transfer(pipe,
781                                 strb->texture,
782                                 strb->rtt_level,
783                                 strb->rtt_face + strb->rtt_slice,
784                                 PIPE_TRANSFER_READ,
785                                 srcX, srcY,
786                                 width, height);
787
788   if ((baseFormat == GL_DEPTH_COMPONENT ||
789        baseFormat == GL_DEPTH_STENCIL) &&
790       util_format_is_depth_and_stencil(stImage->pt->format))
791      transfer_usage = PIPE_TRANSFER_READ_WRITE;
792   else
793      transfer_usage = PIPE_TRANSFER_WRITE;
794
795   /* XXX this used to ignore destZ param */
796   texDest = st_texture_image_map(st, stImage, destZ, transfer_usage,
797                                  destX, destY, width, height);
798
799   if (baseFormat == GL_DEPTH_COMPONENT ||
800       baseFormat == GL_DEPTH_STENCIL) {
801      const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
802                                     ctx->Pixel.DepthBias != 0.0F);
803      GLint row, yStep;
804      uint *data;
805
806      /* determine bottom-to-top vs. top-to-bottom order for src buffer */
807      if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
808         srcY = height - 1;
809         yStep = -1;
810      }
811      else {
812         srcY = 0;
813         yStep = 1;
814      }
815
816      data = (uint *) malloc(width * sizeof(uint));
817
818      if (data) {
819         /* To avoid a large temp memory allocation, do copy row by row */
820         for (row = 0; row < height; row++, srcY += yStep) {
821            pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
822            if (scaleOrBias) {
823               _mesa_scale_and_bias_depth_uint(ctx, width, data);
824            }
825            pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
826         }
827      }
828      else {
829         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
830      }
831
832      free(data);
833   }
834   else {
835      /* RGBA format */
836      GLfloat *tempSrc =
837         (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
838
839      if (tempSrc && texDest) {
840         const GLint dims = 2;
841         const GLint dstRowStride = stImage->transfer->stride;
842         struct gl_texture_image *texImage = &stImage->base;
843         struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
844
845         if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
846            unpack.Invert = GL_TRUE;
847         }
848
849         /* get float/RGBA image from framebuffer */
850         /* XXX this usually involves a lot of int/float conversion.
851          * try to avoid that someday.
852          */
853         pipe_get_tile_rgba_format(pipe, src_trans, 0, 0, width, height,
854                                   util_format_linear(strb->texture->format),
855                                   tempSrc);
856
857         /* Store into texture memory.
858          * Note that this does some special things such as pixel transfer
859          * ops and format conversion.  In particular, if the dest tex format
860          * is actually RGBA but the user created the texture as GL_RGB we
861          * need to fill-in/override the alpha channel with 1.0.
862          */
863         _mesa_texstore(ctx, dims,
864                        texImage->_BaseFormat,
865                        texImage->TexFormat,
866                        dstRowStride,
867                        (GLubyte **) &texDest,
868                        width, height, 1,
869                        GL_RGBA, GL_FLOAT, tempSrc, /* src */
870                        &unpack);
871      }
872      else {
873         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
874      }
875
876      if (tempSrc)
877         free(tempSrc);
878   }
879
880   st_texture_image_unmap(st, stImage);
881   pipe->transfer_destroy(pipe, src_trans);
882}
883
884
885
886/**
887 * If the format of the src renderbuffer and the format of the dest
888 * texture are compatible (in terms of blitting), return a TGSI writemask
889 * to be used during the blit.
890 * If the src/dest are incompatible, return 0.
891 */
892static unsigned
893compatible_src_dst_formats(struct gl_context *ctx,
894                           const struct gl_renderbuffer *src,
895                           const struct gl_texture_image *dst)
896{
897   /* Get logical base formats for the src and dest.
898    * That is, use the user-requested formats and not the actual, device-
899    * chosen formats.
900    * For example, the user may have requested an A8 texture but the
901    * driver may actually be using an RGBA texture format.  When we
902    * copy/blit to that texture, we only want to copy the Alpha channel
903    * and not the RGB channels.
904    *
905    * Similarly, when the src FBO was created an RGB format may have been
906    * requested but the driver actually chose an RGBA format.  In that case,
907    * we don't want to copy the undefined Alpha channel to the dest texture
908    * (it should be 1.0).
909    */
910   const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
911   const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
912
913   /**
914    * XXX when we have red-only and red/green renderbuffers we'll need
915    * to add more cases here (or implement a general-purpose routine that
916    * queries the existance of the R,G,B,A channels in the src and dest).
917    */
918   if (srcFormat == dstFormat) {
919      /* This is the same as matching_base_formats, which should
920       * always pass, as it did previously.
921       */
922      return TGSI_WRITEMASK_XYZW;
923   }
924   else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
925      /* Make sure that A in the dest is 1.  The actual src format
926       * may be RGBA and have undefined A values.
927       */
928      return TGSI_WRITEMASK_XYZ;
929   }
930   else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
931      /* Make sure that A in the dest is 1.  The actual dst format
932       * may be RGBA and will need A=1 to provide proper alpha values
933       * when sampled later.
934       */
935      return TGSI_WRITEMASK_XYZ;
936   }
937   else {
938      if (ST_DEBUG & DEBUG_FALLBACK)
939         debug_printf("%s failed for src %s, dst %s\n",
940                      __FUNCTION__,
941                      _mesa_lookup_enum_by_nr(srcFormat),
942                      _mesa_lookup_enum_by_nr(dstFormat));
943
944      /* Otherwise fail.
945       */
946      return 0;
947   }
948}
949
950
951
952/**
953 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
954 * Note that the region to copy has already been clipped so we know we
955 * won't read from outside the source renderbuffer's bounds.
956 *
957 * Note: srcY=0=Bottom of renderbuffer (GL convention)
958 */
959static void
960st_copy_texsubimage(struct gl_context *ctx,
961                    struct gl_texture_image *texImage,
962                    GLint destX, GLint destY, GLint destZ,
963                    struct gl_renderbuffer *rb,
964                    GLint srcX, GLint srcY,
965                    GLsizei width, GLsizei height)
966{
967   struct st_texture_image *stImage = st_texture_image(texImage);
968   const GLenum texBaseFormat = texImage->_BaseFormat;
969   struct gl_framebuffer *fb = ctx->ReadBuffer;
970   struct st_renderbuffer *strb;
971   struct st_context *st = st_context(ctx);
972   struct pipe_context *pipe = st->pipe;
973   struct pipe_screen *screen = pipe->screen;
974   enum pipe_format dest_format, src_format;
975   GLboolean matching_base_formats;
976   GLuint format_writemask, sample_count;
977   struct pipe_surface *dest_surface = NULL;
978   GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
979   struct pipe_surface surf_tmpl;
980   unsigned int dst_usage;
981   GLint srcY0, srcY1;
982
983   /* make sure finalize_textures has been called?
984    */
985   if (0) st_validate_state(st);
986
987   /* determine if copying depth or color data */
988   if (texBaseFormat == GL_DEPTH_COMPONENT ||
989       texBaseFormat == GL_DEPTH_STENCIL) {
990      strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
991   }
992   else {
993      /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
994      strb = st_renderbuffer(fb->_ColorReadBuffer);
995   }
996
997   if (!strb || !strb->surface || !stImage->pt) {
998      debug_printf("%s: null strb or stImage\n", __FUNCTION__);
999      return;
1000   }
1001
1002   sample_count = strb->surface->texture->nr_samples;
1003   /* I believe this would be legal, presumably would need to do a resolve
1004      for color, and for depth/stencil spec says to just use one of the
1005      depth/stencil samples per pixel? Need some transfer clarifications. */
1006   assert(sample_count < 2);
1007
1008   assert(strb);
1009   assert(strb->surface);
1010   assert(stImage->pt);
1011
1012   src_format = strb->surface->format;
1013   dest_format = stImage->pt->format;
1014
1015   /*
1016    * Determine if the src framebuffer and dest texture have the same
1017    * base format.  We need this to detect a case such as the framebuffer
1018    * being GL_RGBA but the texture being GL_RGB.  If the actual hardware
1019    * texture format stores RGBA we need to set A=1 (overriding the
1020    * framebuffer's alpha values).  We can't do that with the blit or
1021    * textured-quad paths.
1022    */
1023   matching_base_formats =
1024      (_mesa_get_format_base_format(strb->Base.Format) ==
1025       _mesa_get_format_base_format(texImage->TexFormat));
1026
1027   if (ctx->_ImageTransferState) {
1028      goto fallback;
1029   }
1030
1031   if (matching_base_formats &&
1032       src_format == dest_format &&
1033       !do_flip) {
1034      /* use surface_copy() / blit */
1035      struct pipe_box src_box;
1036      u_box_2d_zslice(srcX, srcY, strb->surface->u.tex.first_layer,
1037                      width, height, &src_box);
1038
1039      /* for resource_copy_region(), y=0=top, always */
1040      pipe->resource_copy_region(pipe,
1041                                 /* dest */
1042                                 stImage->pt,
1043                                 stImage->base.Level,
1044                                 destX, destY, destZ + stImage->base.Face,
1045                                 /* src */
1046                                 strb->texture,
1047                                 strb->surface->u.tex.level,
1048                                 &src_box);
1049      return;
1050   }
1051
1052   if (texBaseFormat == GL_DEPTH_STENCIL) {
1053      goto fallback;
1054   }
1055
1056   if (texBaseFormat == GL_DEPTH_COMPONENT) {
1057      format_writemask = TGSI_WRITEMASK_XYZW;
1058      dst_usage = PIPE_BIND_DEPTH_STENCIL;
1059   }
1060   else {
1061      format_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
1062      dst_usage = PIPE_BIND_RENDER_TARGET;
1063   }
1064
1065   if (!format_writemask ||
1066       !screen->is_format_supported(screen, src_format,
1067                                    PIPE_TEXTURE_2D, sample_count,
1068                                    PIPE_BIND_SAMPLER_VIEW) ||
1069       !screen->is_format_supported(screen, dest_format,
1070                                    PIPE_TEXTURE_2D, 0,
1071                                    dst_usage)) {
1072      goto fallback;
1073   }
1074
1075   if (do_flip) {
1076      srcY1 = strb->Base.Height - srcY - height;
1077      srcY0 = srcY1 + height;
1078   }
1079   else {
1080      srcY0 = srcY;
1081      srcY1 = srcY0 + height;
1082   }
1083
1084   /* Disable conditional rendering. */
1085   if (st->render_condition) {
1086      pipe->render_condition(pipe, NULL, 0);
1087   }
1088
1089   memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1090   surf_tmpl.format = util_format_linear(stImage->pt->format);
1091   surf_tmpl.usage = dst_usage;
1092   surf_tmpl.u.tex.level = stImage->base.Level;
1093   surf_tmpl.u.tex.first_layer = stImage->base.Face + destZ;
1094   surf_tmpl.u.tex.last_layer = stImage->base.Face + destZ;
1095
1096   dest_surface = pipe->create_surface(pipe, stImage->pt,
1097                                       &surf_tmpl);
1098   util_blit_pixels_writemask(st->blit,
1099                              strb->texture,
1100                              strb->surface->u.tex.level,
1101                              srcX, srcY0,
1102                              srcX + width, srcY1,
1103                              strb->surface->u.tex.first_layer,
1104                              dest_surface,
1105                              destX, destY,
1106                              destX + width, destY + height,
1107                              0.0, PIPE_TEX_MIPFILTER_NEAREST,
1108                              format_writemask);
1109   pipe_surface_reference(&dest_surface, NULL);
1110
1111   /* Restore conditional rendering state. */
1112   if (st->render_condition) {
1113      pipe->render_condition(pipe, st->render_condition,
1114                             st->condition_mode);
1115   }
1116
1117   return;
1118
1119fallback:
1120   /* software fallback */
1121   fallback_copy_texsubimage(ctx,
1122                             strb, stImage, texBaseFormat,
1123                             destX, destY, destZ,
1124                             srcX, srcY, width, height);
1125}
1126
1127
1128
1129static void
1130st_CopyTexSubImage1D(struct gl_context *ctx,
1131                     struct gl_texture_image *texImage,
1132                     GLint xoffset,
1133                     struct gl_renderbuffer *rb,
1134                     GLint x, GLint y, GLsizei width)
1135{
1136   const GLint yoffset = 0, zoffset = 0;
1137   const GLsizei height = 1;
1138   st_copy_texsubimage(ctx, texImage,
1139                       xoffset, yoffset, zoffset,  /* destX,Y,Z */
1140                       rb, x, y, width, height);  /* src X, Y, size */
1141}
1142
1143
1144static void
1145st_CopyTexSubImage2D(struct gl_context *ctx,
1146                     struct gl_texture_image *texImage,
1147                     GLint xoffset, GLint yoffset,
1148                     struct gl_renderbuffer *rb,
1149                     GLint x, GLint y, GLsizei width, GLsizei height)
1150{
1151   const GLint zoffset = 0;
1152   st_copy_texsubimage(ctx, texImage,
1153                       xoffset, yoffset, zoffset,  /* destX,Y,Z */
1154                       rb, x, y, width, height);  /* src X, Y, size */
1155}
1156
1157
1158static void
1159st_CopyTexSubImage3D(struct gl_context *ctx,
1160                     struct gl_texture_image *texImage,
1161                     GLint xoffset, GLint yoffset, GLint zoffset,
1162                     struct gl_renderbuffer *rb,
1163                     GLint x, GLint y, GLsizei width, GLsizei height)
1164{
1165   st_copy_texsubimage(ctx, texImage,
1166                       xoffset, yoffset, zoffset,  /* destX,Y,Z */
1167                       rb, x, y, width, height);  /* src X, Y, size */
1168}
1169
1170
1171/**
1172 * Copy image data from stImage into the texture object 'stObj' at level
1173 * 'dstLevel'.
1174 */
1175static void
1176copy_image_data_to_texture(struct st_context *st,
1177			   struct st_texture_object *stObj,
1178                           GLuint dstLevel,
1179			   struct st_texture_image *stImage)
1180{
1181   /* debug checks */
1182   {
1183      const struct gl_texture_image *dstImage =
1184         stObj->base.Image[stImage->base.Face][dstLevel];
1185      assert(dstImage);
1186      assert(dstImage->Width == stImage->base.Width);
1187      assert(dstImage->Height == stImage->base.Height);
1188      assert(dstImage->Depth == stImage->base.Depth);
1189   }
1190
1191   if (stImage->pt) {
1192      /* Copy potentially with the blitter:
1193       */
1194      GLuint src_level;
1195      if (stImage->pt != stObj->pt)
1196         src_level = 0;
1197      else
1198         src_level = stImage->base.Level;
1199
1200      st_texture_image_copy(st->pipe,
1201                            stObj->pt, dstLevel,  /* dest texture, level */
1202                            stImage->pt, src_level, /* src texture, level */
1203                            stImage->base.Face);
1204
1205      pipe_resource_reference(&stImage->pt, NULL);
1206   }
1207   else if (stImage->TexData) {
1208      /* Copy from malloc'd memory */
1209      /* XXX this should be re-examined/tested with a compressed format */
1210      GLuint blockSize = util_format_get_blocksize(stObj->pt->format);
1211      GLuint srcRowStride = stImage->base.Width * blockSize;
1212      GLuint srcSliceStride = stImage->base.Height * srcRowStride;
1213      st_texture_image_data(st,
1214                            stObj->pt,
1215                            stImage->base.Face,
1216                            dstLevel,
1217                            stImage->TexData,
1218                            srcRowStride,
1219                            srcSliceStride);
1220      _mesa_align_free(stImage->TexData);
1221      stImage->TexData = NULL;
1222   }
1223
1224   pipe_resource_reference(&stImage->pt, stObj->pt);
1225}
1226
1227
1228/**
1229 * Called during state validation.  When this function is finished,
1230 * the texture object should be ready for rendering.
1231 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1232 */
1233GLboolean
1234st_finalize_texture(struct gl_context *ctx,
1235		    struct pipe_context *pipe,
1236		    struct gl_texture_object *tObj)
1237{
1238   struct st_context *st = st_context(ctx);
1239   struct st_texture_object *stObj = st_texture_object(tObj);
1240   const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1241   GLuint face;
1242   struct st_texture_image *firstImage;
1243   enum pipe_format firstImageFormat;
1244   GLuint ptWidth, ptHeight, ptDepth, ptLayers;
1245
1246   if (_mesa_is_texture_complete(tObj, &tObj->Sampler)) {
1247      /* The texture is complete and we know exactly how many mipmap levels
1248       * are present/needed.  This is conditional because we may be called
1249       * from the st_generate_mipmap() function when the texture object is
1250       * incomplete.  In that case, we'll have set stObj->lastLevel before
1251       * we get here.
1252       */
1253      if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
1254          stObj->base.Sampler.MinFilter == GL_NEAREST)
1255         stObj->lastLevel = stObj->base.BaseLevel;
1256      else
1257         stObj->lastLevel = stObj->base._MaxLevel;
1258   }
1259
1260   firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1261   assert(firstImage);
1262
1263   /* If both firstImage and stObj point to a texture which can contain
1264    * all active images, favour firstImage.  Note that because of the
1265    * completeness requirement, we know that the image dimensions
1266    * will match.
1267    */
1268   if (firstImage->pt &&
1269       firstImage->pt != stObj->pt &&
1270       (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
1271      pipe_resource_reference(&stObj->pt, firstImage->pt);
1272      pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
1273   }
1274
1275   /* Find gallium format for the Mesa texture */
1276   firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1277
1278   /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1279   {
1280      GLuint width, height, depth;
1281      if (!guess_base_level_size(stObj->base.Target,
1282                                 firstImage->base.Width2,
1283                                 firstImage->base.Height2,
1284                                 firstImage->base.Depth2,
1285                                 firstImage->base.Level,
1286                                 &width, &height, &depth)) {
1287         width = stObj->width0;
1288         height = stObj->height0;
1289         depth = stObj->depth0;
1290      }
1291      /* convert GL dims to Gallium dims */
1292      st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
1293                                      &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1294   }
1295
1296   /* If we already have a gallium texture, check that it matches the texture
1297    * object's format, target, size, num_levels, etc.
1298    */
1299   if (stObj->pt) {
1300      if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1301          !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
1302          stObj->pt->last_level < stObj->lastLevel ||
1303          stObj->pt->width0 != ptWidth ||
1304          stObj->pt->height0 != ptHeight ||
1305          stObj->pt->depth0 != ptDepth ||
1306          stObj->pt->array_size != ptLayers)
1307      {
1308         /* The gallium texture does not match the Mesa texture so delete the
1309          * gallium texture now.  We'll make a new one below.
1310          */
1311         pipe_resource_reference(&stObj->pt, NULL);
1312         pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
1313         st->dirty.st |= ST_NEW_FRAMEBUFFER;
1314      }
1315   }
1316
1317   /* May need to create a new gallium texture:
1318    */
1319   if (!stObj->pt) {
1320      GLuint bindings = default_bindings(st, firstImageFormat);
1321
1322      stObj->pt = st_texture_create(st,
1323                                    gl_target_to_pipe(stObj->base.Target),
1324                                    firstImageFormat,
1325                                    stObj->lastLevel,
1326                                    ptWidth,
1327                                    ptHeight,
1328                                    ptDepth,
1329                                    ptLayers,
1330                                    bindings);
1331
1332      if (!stObj->pt) {
1333         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1334         return GL_FALSE;
1335      }
1336   }
1337
1338   /* Pull in any images not in the object's texture:
1339    */
1340   for (face = 0; face < nr_faces; face++) {
1341      GLuint level;
1342      for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1343         struct st_texture_image *stImage =
1344            st_texture_image(stObj->base.Image[face][level]);
1345
1346         /* Need to import images in main memory or held in other textures.
1347          */
1348         if (stImage && stObj->pt != stImage->pt) {
1349            if (level == 0 ||
1350                (stImage->base.Width == u_minify(stObj->width0, level) &&
1351                 stImage->base.Height == u_minify(stObj->height0, level) &&
1352                 stImage->base.Depth == u_minify(stObj->depth0, level))) {
1353               /* src image fits expected dest mipmap level size */
1354               copy_image_data_to_texture(st, stObj, level, stImage);
1355            }
1356         }
1357      }
1358   }
1359
1360   return GL_TRUE;
1361}
1362
1363
1364/**
1365 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1366 * for a whole mipmap stack.
1367 */
1368static GLboolean
1369st_AllocTextureStorage(struct gl_context *ctx,
1370                       struct gl_texture_object *texObj,
1371                       GLsizei levels, GLsizei width,
1372                       GLsizei height, GLsizei depth)
1373{
1374   const GLuint numFaces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1375   struct st_context *st = st_context(ctx);
1376   struct st_texture_object *stObj = st_texture_object(texObj);
1377   GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
1378   enum pipe_format fmt;
1379   GLint level;
1380
1381   assert(levels > 0);
1382
1383   /* Save the level=0 dimensions */
1384   stObj->width0 = width;
1385   stObj->height0 = height;
1386   stObj->depth0 = depth;
1387   stObj->lastLevel = levels - 1;
1388
1389   fmt = st_mesa_format_to_pipe_format(texObj->Image[0][0]->TexFormat);
1390
1391   bindings = default_bindings(st, fmt);
1392
1393   st_gl_texture_dims_to_pipe_dims(texObj->Target,
1394                                   width, height, depth,
1395                                   &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1396
1397   stObj->pt = st_texture_create(st,
1398                                 gl_target_to_pipe(texObj->Target),
1399                                 fmt,
1400                                 levels,
1401                                 ptWidth,
1402                                 ptHeight,
1403                                 ptDepth,
1404                                 ptLayers,
1405                                 bindings);
1406   if (!stObj->pt)
1407      return GL_FALSE;
1408
1409   /* Set image resource pointers */
1410   for (level = 0; level < levels; level++) {
1411      GLuint face;
1412      for (face = 0; face < numFaces; face++) {
1413         struct st_texture_image *stImage =
1414            st_texture_image(texObj->Image[face][level]);
1415         pipe_resource_reference(&stImage->pt, stObj->pt);
1416      }
1417   }
1418
1419   return GL_TRUE;
1420}
1421
1422
1423
1424void
1425st_init_texture_functions(struct dd_function_table *functions)
1426{
1427   functions->ChooseTextureFormat = st_ChooseTextureFormat;
1428   functions->TexImage1D = st_TexImage1D;
1429   functions->TexImage2D = st_TexImage2D;
1430   functions->TexImage3D = st_TexImage3D;
1431   functions->TexSubImage1D = _mesa_store_texsubimage1d;
1432   functions->TexSubImage2D = _mesa_store_texsubimage2d;
1433   functions->TexSubImage3D = _mesa_store_texsubimage3d;
1434   functions->CompressedTexSubImage1D = _mesa_store_compressed_texsubimage1d;
1435   functions->CompressedTexSubImage2D = _mesa_store_compressed_texsubimage2d;
1436   functions->CompressedTexSubImage3D = _mesa_store_compressed_texsubimage3d;
1437   functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
1438   functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
1439   functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
1440   functions->GenerateMipmap = st_generate_mipmap;
1441
1442   functions->GetTexImage = st_GetTexImage;
1443
1444   /* compressed texture functions */
1445   functions->CompressedTexImage2D = st_CompressedTexImage2D;
1446   functions->GetCompressedTexImage = _mesa_get_compressed_teximage;
1447
1448   functions->NewTextureObject = st_NewTextureObject;
1449   functions->NewTextureImage = st_NewTextureImage;
1450   functions->DeleteTextureImage = st_DeleteTextureImage;
1451   functions->DeleteTexture = st_DeleteTextureObject;
1452   functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
1453   functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
1454   functions->MapTextureImage = st_MapTextureImage;
1455   functions->UnmapTextureImage = st_UnmapTextureImage;
1456
1457   /* XXX Temporary until we can query pipe's texture sizes */
1458   functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1459
1460   functions->AllocTextureStorage = st_AllocTextureStorage;
1461}
1462