st_cb_texture.c revision 4a448a63adbbece1d9bddacd9428aad7cc68a628
1/**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <stdio.h>
29#include "main/bufferobj.h"
30#include "main/enums.h"
31#include "main/fbobject.h"
32#include "main/formats.h"
33#include "main/format_utils.h"
34#include "main/glformats.h"
35#include "main/image.h"
36#include "main/imports.h"
37#include "main/macros.h"
38#include "main/mipmap.h"
39#include "main/pack.h"
40#include "main/pbo.h"
41#include "main/pixeltransfer.h"
42#include "main/texcompress.h"
43#include "main/texcompress_etc.h"
44#include "main/texgetimage.h"
45#include "main/teximage.h"
46#include "main/texobj.h"
47#include "main/texstore.h"
48
49#include "state_tracker/st_debug.h"
50#include "state_tracker/st_context.h"
51#include "state_tracker/st_cb_fbo.h"
52#include "state_tracker/st_cb_flush.h"
53#include "state_tracker/st_cb_texture.h"
54#include "state_tracker/st_cb_bufferobjects.h"
55#include "state_tracker/st_format.h"
56#include "state_tracker/st_texture.h"
57#include "state_tracker/st_gen_mipmap.h"
58#include "state_tracker/st_atom.h"
59
60#include "pipe/p_context.h"
61#include "pipe/p_defines.h"
62#include "util/u_inlines.h"
63#include "pipe/p_shader_tokens.h"
64#include "util/u_tile.h"
65#include "util/u_format.h"
66#include "util/u_surface.h"
67#include "util/u_sampler.h"
68#include "util/u_math.h"
69#include "util/u_box.h"
70
71#define DBG if (0) printf
72
73
74enum pipe_texture_target
75gl_target_to_pipe(GLenum target)
76{
77   switch (target) {
78   case GL_TEXTURE_1D:
79   case GL_PROXY_TEXTURE_1D:
80      return PIPE_TEXTURE_1D;
81   case GL_TEXTURE_2D:
82   case GL_PROXY_TEXTURE_2D:
83   case GL_TEXTURE_EXTERNAL_OES:
84   case GL_TEXTURE_2D_MULTISAMPLE:
85   case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
86      return PIPE_TEXTURE_2D;
87   case GL_TEXTURE_RECTANGLE_NV:
88   case GL_PROXY_TEXTURE_RECTANGLE_NV:
89      return PIPE_TEXTURE_RECT;
90   case GL_TEXTURE_3D:
91   case GL_PROXY_TEXTURE_3D:
92      return PIPE_TEXTURE_3D;
93   case GL_TEXTURE_CUBE_MAP_ARB:
94   case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
95   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
96   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
97   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
98   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
99   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
100   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
101      return PIPE_TEXTURE_CUBE;
102   case GL_TEXTURE_1D_ARRAY_EXT:
103   case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
104      return PIPE_TEXTURE_1D_ARRAY;
105   case GL_TEXTURE_2D_ARRAY_EXT:
106   case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
107   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
108   case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
109      return PIPE_TEXTURE_2D_ARRAY;
110   case GL_TEXTURE_BUFFER:
111      return PIPE_BUFFER;
112   case GL_TEXTURE_CUBE_MAP_ARRAY:
113   case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
114      return PIPE_TEXTURE_CUBE_ARRAY;
115   default:
116      assert(0);
117      return 0;
118   }
119}
120
121
122/** called via ctx->Driver.NewTextureImage() */
123static struct gl_texture_image *
124st_NewTextureImage(struct gl_context * ctx)
125{
126   DBG("%s\n", __func__);
127   (void) ctx;
128   return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
129}
130
131
132/** called via ctx->Driver.DeleteTextureImage() */
133static void
134st_DeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
135{
136   /* nothing special (yet) for st_texture_image */
137   _mesa_delete_texture_image(ctx, img);
138}
139
140
141/** called via ctx->Driver.NewTextureObject() */
142static struct gl_texture_object *
143st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
144{
145   struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
146
147   DBG("%s\n", __func__);
148   _mesa_initialize_texture_object(ctx, &obj->base, name, target);
149
150   return &obj->base;
151}
152
153/** called via ctx->Driver.DeleteTextureObject() */
154static void
155st_DeleteTextureObject(struct gl_context *ctx,
156                       struct gl_texture_object *texObj)
157{
158   struct st_context *st = st_context(ctx);
159   struct st_texture_object *stObj = st_texture_object(texObj);
160
161   pipe_resource_reference(&stObj->pt, NULL);
162   st_texture_release_all_sampler_views(st, stObj);
163   st_texture_free_sampler_views(stObj);
164   _mesa_delete_texture_object(ctx, texObj);
165}
166
167
168/** called via ctx->Driver.FreeTextureImageBuffer() */
169static void
170st_FreeTextureImageBuffer(struct gl_context *ctx,
171                          struct gl_texture_image *texImage)
172{
173   struct st_texture_image *stImage = st_texture_image(texImage);
174
175   DBG("%s\n", __func__);
176
177   if (stImage->pt) {
178      pipe_resource_reference(&stImage->pt, NULL);
179   }
180
181   free(stImage->transfer);
182   stImage->transfer = NULL;
183   stImage->num_transfers = 0;
184}
185
186
187/** called via ctx->Driver.MapTextureImage() */
188static void
189st_MapTextureImage(struct gl_context *ctx,
190                   struct gl_texture_image *texImage,
191                   GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h,
192                   GLbitfield mode,
193                   GLubyte **mapOut, GLint *rowStrideOut)
194{
195   struct st_context *st = st_context(ctx);
196   struct st_texture_image *stImage = st_texture_image(texImage);
197   unsigned pipeMode;
198   GLubyte *map;
199   struct pipe_transfer *transfer;
200
201   pipeMode = 0x0;
202   if (mode & GL_MAP_READ_BIT)
203      pipeMode |= PIPE_TRANSFER_READ;
204   if (mode & GL_MAP_WRITE_BIT)
205      pipeMode |= PIPE_TRANSFER_WRITE;
206   if (mode & GL_MAP_INVALIDATE_RANGE_BIT)
207      pipeMode |= PIPE_TRANSFER_DISCARD_RANGE;
208
209   map = st_texture_image_map(st, stImage, pipeMode, x, y, slice, w, h, 1,
210                              &transfer);
211   if (map) {
212      if ((_mesa_is_format_etc2(texImage->TexFormat) && !st->has_etc2) ||
213          (texImage->TexFormat == MESA_FORMAT_ETC1_RGB8 && !st->has_etc1)) {
214         /* ETC isn't supported by gallium and it's represented
215          * by uncompressed formats. Only write transfers with precompressed
216          * data are supported by ES3, which makes this really simple.
217          *
218          * Just create a temporary storage where the ETC texture will
219          * be stored. It will be decompressed in the Unmap function.
220          */
221         unsigned z = transfer->box.z;
222         struct st_texture_image_transfer *itransfer = &stImage->transfer[z];
223
224         itransfer->temp_data =
225            malloc(_mesa_format_image_size(texImage->TexFormat, w, h, 1));
226         itransfer->temp_stride =
227            _mesa_format_row_stride(texImage->TexFormat, w);
228         itransfer->map = map;
229
230         *mapOut = itransfer->temp_data;
231         *rowStrideOut = itransfer->temp_stride;
232      }
233      else {
234         /* supported mapping */
235         *mapOut = map;
236         *rowStrideOut = transfer->stride;
237      }
238   }
239   else {
240      *mapOut = NULL;
241      *rowStrideOut = 0;
242   }
243}
244
245
246/** called via ctx->Driver.UnmapTextureImage() */
247static void
248st_UnmapTextureImage(struct gl_context *ctx,
249                     struct gl_texture_image *texImage,
250                     GLuint slice)
251{
252   struct st_context *st = st_context(ctx);
253   struct st_texture_image *stImage  = st_texture_image(texImage);
254
255   if ((_mesa_is_format_etc2(texImage->TexFormat) && !st->has_etc2) ||
256       (texImage->TexFormat == MESA_FORMAT_ETC1_RGB8 && !st->has_etc1)) {
257      /* Decompress the ETC texture to the mapped one. */
258      unsigned z = slice + stImage->base.Face;
259      struct st_texture_image_transfer *itransfer = &stImage->transfer[z];
260      struct pipe_transfer *transfer = itransfer->transfer;
261
262      assert(z == transfer->box.z);
263
264      if (texImage->TexFormat == MESA_FORMAT_ETC1_RGB8) {
265         _mesa_etc1_unpack_rgba8888(itransfer->map, transfer->stride,
266                                    itransfer->temp_data,
267                                    itransfer->temp_stride,
268                                    transfer->box.width, transfer->box.height);
269      }
270      else {
271         _mesa_unpack_etc2_format(itransfer->map, transfer->stride,
272                                  itransfer->temp_data, itransfer->temp_stride,
273                                  transfer->box.width, transfer->box.height,
274                                  texImage->TexFormat);
275      }
276
277      free(itransfer->temp_data);
278      itransfer->temp_data = NULL;
279      itransfer->temp_stride = 0;
280      itransfer->map = 0;
281   }
282
283   st_texture_image_unmap(st, stImage, slice);
284}
285
286
287/**
288 * Return default texture resource binding bitmask for the given format.
289 */
290static GLuint
291default_bindings(struct st_context *st, enum pipe_format format)
292{
293   struct pipe_screen *screen = st->pipe->screen;
294   const unsigned target = PIPE_TEXTURE_2D;
295   unsigned bindings;
296
297   if (util_format_is_depth_or_stencil(format))
298      bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
299   else
300      bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
301
302   if (screen->is_format_supported(screen, format, target, 0, bindings))
303      return bindings;
304   else {
305      /* Try non-sRGB. */
306      format = util_format_linear(format);
307
308      if (screen->is_format_supported(screen, format, target, 0, bindings))
309         return bindings;
310      else
311         return PIPE_BIND_SAMPLER_VIEW;
312   }
313}
314
315
316/**
317 * Given the size of a mipmap image, try to compute the size of the level=0
318 * mipmap image.
319 *
320 * Note that this isn't always accurate for odd-sized, non-POW textures.
321 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
322 *
323 * \return GL_TRUE for success, GL_FALSE for failure
324 */
325static GLboolean
326guess_base_level_size(GLenum target,
327                      GLuint width, GLuint height, GLuint depth, GLuint level,
328                      GLuint *width0, GLuint *height0, GLuint *depth0)
329{
330   assert(width >= 1);
331   assert(height >= 1);
332   assert(depth >= 1);
333
334   if (level > 0) {
335      /* Guess the size of the base level.
336       * Depending on the image's size, we can't always make a guess here.
337       */
338      switch (target) {
339      case GL_TEXTURE_1D:
340      case GL_TEXTURE_1D_ARRAY:
341         width <<= level;
342         break;
343
344      case GL_TEXTURE_2D:
345      case GL_TEXTURE_2D_ARRAY:
346         /* We can't make a good guess here, because the base level dimensions
347          * can be non-square.
348          */
349         if (width == 1 || height == 1) {
350            return GL_FALSE;
351         }
352         width <<= level;
353         height <<= level;
354         break;
355
356      case GL_TEXTURE_CUBE_MAP:
357      case GL_TEXTURE_CUBE_MAP_ARRAY:
358         width <<= level;
359         height <<= level;
360         break;
361
362      case GL_TEXTURE_3D:
363         /* We can't make a good guess here, because the base level dimensions
364          * can be non-cube.
365          */
366         if (width == 1 || height == 1 || depth == 1) {
367            return GL_FALSE;
368         }
369         width <<= level;
370         height <<= level;
371         depth <<= level;
372         break;
373
374      case GL_TEXTURE_RECTANGLE:
375         break;
376
377      default:
378         assert(0);
379      }
380   }
381
382   *width0 = width;
383   *height0 = height;
384   *depth0 = depth;
385
386   return GL_TRUE;
387}
388
389
390/**
391 * Try to determine whether we should allocate memory for a full texture
392 * mipmap.  The problem is when we get a glTexImage(level=0) call, we
393 * can't immediately know if other mipmap levels are coming next.  Here
394 * we try to guess whether to allocate memory for a mipmap or just the
395 * 0th level.
396 *
397 * If we guess incorrectly here we'll later reallocate the right amount of
398 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
399 *
400 * \param stObj  the texture object we're going to allocate memory for.
401 * \param stImage  describes the incoming image which we need to store.
402 */
403static boolean
404allocate_full_mipmap(const struct st_texture_object *stObj,
405                     const struct st_texture_image *stImage)
406{
407   switch (stObj->base.Target) {
408   case GL_TEXTURE_RECTANGLE_NV:
409   case GL_TEXTURE_BUFFER:
410   case GL_TEXTURE_EXTERNAL_OES:
411   case GL_TEXTURE_2D_MULTISAMPLE:
412   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
413      /* these texture types cannot be mipmapped */
414      return FALSE;
415   }
416
417   if (stImage->base.Level > 0 || stObj->base.GenerateMipmap)
418      return TRUE;
419
420   if (stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
421       stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT)
422      /* depth/stencil textures are seldom mipmapped */
423      return FALSE;
424
425   if (stObj->base.BaseLevel == 0 && stObj->base.MaxLevel == 0)
426      return FALSE;
427
428   if (stObj->base.Sampler.MinFilter == GL_NEAREST ||
429       stObj->base.Sampler.MinFilter == GL_LINEAR)
430      /* not a mipmap minification filter */
431      return FALSE;
432
433   if (stObj->base.Target == GL_TEXTURE_3D)
434      /* 3D textures are seldom mipmapped */
435      return FALSE;
436
437   return TRUE;
438}
439
440
441/**
442 * Try to allocate a pipe_resource object for the given st_texture_object.
443 *
444 * We use the given st_texture_image as a clue to determine the size of the
445 * mipmap image at level=0.
446 *
447 * \return GL_TRUE for success, GL_FALSE if out of memory.
448 */
449static GLboolean
450guess_and_alloc_texture(struct st_context *st,
451			struct st_texture_object *stObj,
452			const struct st_texture_image *stImage)
453{
454   GLuint lastLevel, width, height, depth;
455   GLuint bindings;
456   GLuint ptWidth, ptHeight, ptDepth, ptLayers;
457   enum pipe_format fmt;
458
459   DBG("%s\n", __func__);
460
461   assert(!stObj->pt);
462
463   if (!guess_base_level_size(stObj->base.Target,
464                              stImage->base.Width2,
465                              stImage->base.Height2,
466                              stImage->base.Depth2,
467                              stImage->base.Level,
468                              &width, &height, &depth)) {
469      /* we can't determine the image size at level=0 */
470      stObj->width0 = stObj->height0 = stObj->depth0 = 0;
471      /* this is not an out of memory error */
472      return GL_TRUE;
473   }
474
475   /* At this point, (width x height x depth) is the expected size of
476    * the level=0 mipmap image.
477    */
478
479   /* Guess a reasonable value for lastLevel.  With OpenGL we have no
480    * idea how many mipmap levels will be in a texture until we start
481    * to render with it.  Make an educated guess here but be prepared
482    * to re-allocating a texture buffer with space for more (or fewer)
483    * mipmap levels later.
484    */
485   if (allocate_full_mipmap(stObj, stImage)) {
486      /* alloc space for a full mipmap */
487      lastLevel = _mesa_get_tex_max_num_levels(stObj->base.Target,
488                                               width, height, depth) - 1;
489   }
490   else {
491      /* only alloc space for a single mipmap level */
492      lastLevel = 0;
493   }
494
495   /* Save the level=0 dimensions */
496   stObj->width0 = width;
497   stObj->height0 = height;
498   stObj->depth0 = depth;
499
500   fmt = st_mesa_format_to_pipe_format(st, stImage->base.TexFormat);
501
502   bindings = default_bindings(st, fmt);
503
504   st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
505                                   width, height, depth,
506                                   &ptWidth, &ptHeight, &ptDepth, &ptLayers);
507
508   stObj->pt = st_texture_create(st,
509                                 gl_target_to_pipe(stObj->base.Target),
510                                 fmt,
511                                 lastLevel,
512                                 ptWidth,
513                                 ptHeight,
514                                 ptDepth,
515                                 ptLayers, 0,
516                                 bindings);
517
518   stObj->lastLevel = lastLevel;
519
520   DBG("%s returning %d\n", __func__, (stObj->pt != NULL));
521
522   return stObj->pt != NULL;
523}
524
525
526/**
527 * Called via ctx->Driver.AllocTextureImageBuffer().
528 * If the texture object/buffer already has space for the indicated image,
529 * we're done.  Otherwise, allocate memory for the new texture image.
530 */
531static GLboolean
532st_AllocTextureImageBuffer(struct gl_context *ctx,
533                           struct gl_texture_image *texImage)
534{
535   struct st_context *st = st_context(ctx);
536   struct st_texture_image *stImage = st_texture_image(texImage);
537   struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
538   const GLuint level = texImage->Level;
539   GLuint width = texImage->Width;
540   GLuint height = texImage->Height;
541   GLuint depth = texImage->Depth;
542
543   DBG("%s\n", __func__);
544
545   assert(!stImage->pt); /* xxx this might be wrong */
546
547   /* Look if the parent texture object has space for this image */
548   if (stObj->pt &&
549       level <= stObj->pt->last_level &&
550       st_texture_match_image(st, stObj->pt, texImage)) {
551      /* this image will fit in the existing texture object's memory */
552      pipe_resource_reference(&stImage->pt, stObj->pt);
553      return GL_TRUE;
554   }
555
556   /* The parent texture object does not have space for this image */
557
558   pipe_resource_reference(&stObj->pt, NULL);
559   st_texture_release_all_sampler_views(st, stObj);
560
561   if (!guess_and_alloc_texture(st, stObj, stImage)) {
562      /* Probably out of memory.
563       * Try flushing any pending rendering, then retry.
564       */
565      st_finish(st);
566      if (!guess_and_alloc_texture(st, stObj, stImage)) {
567         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
568         return GL_FALSE;
569      }
570   }
571
572   if (stObj->pt &&
573       st_texture_match_image(st, stObj->pt, texImage)) {
574      /* The image will live in the object's mipmap memory */
575      pipe_resource_reference(&stImage->pt, stObj->pt);
576      assert(stImage->pt);
577      return GL_TRUE;
578   }
579   else {
580      /* Create a new, temporary texture/resource/buffer to hold this
581       * one texture image.  Note that when we later access this image
582       * (either for mapping or copying) we'll want to always specify
583       * mipmap level=0, even if the image represents some other mipmap
584       * level.
585       */
586      enum pipe_format format =
587         st_mesa_format_to_pipe_format(st, texImage->TexFormat);
588      GLuint bindings = default_bindings(st, format);
589      GLuint ptWidth, ptHeight, ptDepth, ptLayers;
590
591      st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
592                                      width, height, depth,
593                                      &ptWidth, &ptHeight, &ptDepth, &ptLayers);
594
595      stImage->pt = st_texture_create(st,
596                                      gl_target_to_pipe(stObj->base.Target),
597                                      format,
598                                      0, /* lastLevel */
599                                      ptWidth,
600                                      ptHeight,
601                                      ptDepth,
602                                      ptLayers, 0,
603                                      bindings);
604      return stImage->pt != NULL;
605   }
606}
607
608
609/**
610 * Preparation prior to glTexImage.  Basically check the 'surface_based'
611 * field and switch to a "normal" tex image if necessary.
612 */
613static void
614prep_teximage(struct gl_context *ctx, struct gl_texture_image *texImage,
615              GLenum format, GLenum type)
616{
617   struct gl_texture_object *texObj = texImage->TexObject;
618   struct st_texture_object *stObj = st_texture_object(texObj);
619
620   /* switch to "normal" */
621   if (stObj->surface_based) {
622      const GLenum target = texObj->Target;
623      const GLuint level = texImage->Level;
624      mesa_format texFormat;
625
626      _mesa_clear_texture_object(ctx, texObj);
627      pipe_resource_reference(&stObj->pt, NULL);
628
629      /* oops, need to init this image again */
630      texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
631                                              texImage->InternalFormat, format,
632                                              type);
633
634      _mesa_init_teximage_fields(ctx, texImage,
635                                 texImage->Width, texImage->Height,
636                                 texImage->Depth, texImage->Border,
637                                 texImage->InternalFormat, texFormat);
638
639      stObj->surface_based = GL_FALSE;
640   }
641}
642
643
644/**
645 * Return a writemask for the gallium blit. The parameters can be base
646 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
647 */
648unsigned
649st_get_blit_mask(GLenum srcFormat, GLenum dstFormat)
650{
651   switch (dstFormat) {
652   case GL_DEPTH_STENCIL:
653      switch (srcFormat) {
654      case GL_DEPTH_STENCIL:
655         return PIPE_MASK_ZS;
656      case GL_DEPTH_COMPONENT:
657         return PIPE_MASK_Z;
658      case GL_STENCIL_INDEX:
659         return PIPE_MASK_S;
660      default:
661         assert(0);
662         return 0;
663      }
664
665   case GL_DEPTH_COMPONENT:
666      switch (srcFormat) {
667      case GL_DEPTH_STENCIL:
668      case GL_DEPTH_COMPONENT:
669         return PIPE_MASK_Z;
670      default:
671         assert(0);
672         return 0;
673      }
674
675   case GL_STENCIL_INDEX:
676      switch (srcFormat) {
677      case GL_STENCIL_INDEX:
678         return PIPE_MASK_S;
679      default:
680         assert(0);
681         return 0;
682      }
683
684   default:
685      return PIPE_MASK_RGBA;
686   }
687}
688
689
690static void
691st_TexSubImage(struct gl_context *ctx, GLuint dims,
692               struct gl_texture_image *texImage,
693               GLint xoffset, GLint yoffset, GLint zoffset,
694               GLint width, GLint height, GLint depth,
695               GLenum format, GLenum type, const void *pixels,
696               const struct gl_pixelstore_attrib *unpack)
697{
698   struct st_context *st = st_context(ctx);
699   struct st_texture_image *stImage = st_texture_image(texImage);
700   struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
701   struct pipe_context *pipe = st->pipe;
702   struct pipe_screen *screen = pipe->screen;
703   struct pipe_resource *dst = stImage->pt;
704   struct pipe_resource *src = NULL;
705   struct pipe_resource src_templ;
706   struct pipe_transfer *transfer;
707   struct pipe_blit_info blit;
708   enum pipe_format src_format, dst_format;
709   mesa_format mesa_src_format;
710   GLenum gl_target = texImage->TexObject->Target;
711   unsigned bind;
712   GLubyte *map;
713
714   assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
715          texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
716
717   if (!st->prefer_blit_based_texture_transfer) {
718      goto fallback;
719   }
720
721   if (!dst) {
722      goto fallback;
723   }
724
725   /* XXX Fallback for depth-stencil formats due to an incomplete stencil
726    * blit implementation in some drivers. */
727   if (format == GL_DEPTH_STENCIL) {
728      goto fallback;
729   }
730
731   /* If the base internal format and the texture format don't match,
732    * we can't use blit-based TexSubImage. */
733   if (texImage->_BaseFormat !=
734       _mesa_get_format_base_format(texImage->TexFormat)) {
735      goto fallback;
736   }
737
738   /* See if the texture format already matches the format and type,
739    * in which case the memcpy-based fast path will likely be used and
740    * we don't have to blit. */
741   if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
742                                            type, unpack->SwapBytes, NULL)) {
743      goto fallback;
744   }
745
746   if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
747      bind = PIPE_BIND_DEPTH_STENCIL;
748   else
749      bind = PIPE_BIND_RENDER_TARGET;
750
751   /* See if the destination format is supported.
752    * For luminance and intensity, only the red channel is stored there. */
753   dst_format = util_format_linear(dst->format);
754   dst_format = util_format_luminance_to_red(dst_format);
755   dst_format = util_format_intensity_to_red(dst_format);
756
757   if (!dst_format ||
758       !screen->is_format_supported(screen, dst_format, dst->target,
759                                    dst->nr_samples, bind)) {
760      goto fallback;
761   }
762
763   /* Choose the source format. */
764   src_format = st_choose_matching_format(st, PIPE_BIND_SAMPLER_VIEW,
765                                          format, type, unpack->SwapBytes);
766   if (!src_format) {
767      goto fallback;
768   }
769
770   mesa_src_format = st_pipe_format_to_mesa_format(src_format);
771
772   /* There is no reason to do this if we cannot use memcpy for the temporary
773    * source texture at least. This also takes transfer ops into account,
774    * etc. */
775   if (!_mesa_texstore_can_use_memcpy(ctx,
776                             _mesa_get_format_base_format(mesa_src_format),
777                             mesa_src_format, format, type, unpack)) {
778      goto fallback;
779   }
780
781   /* TexSubImage only sets a single cubemap face. */
782   if (gl_target == GL_TEXTURE_CUBE_MAP) {
783      gl_target = GL_TEXTURE_2D;
784   }
785   /* TexSubImage can specify subsets of cube map array faces
786    * so we need to upload via 2D array instead */
787   if (gl_target == GL_TEXTURE_CUBE_MAP_ARRAY) {
788      gl_target = GL_TEXTURE_2D_ARRAY;
789   }
790
791   /* Initialize the source texture description. */
792   memset(&src_templ, 0, sizeof(src_templ));
793   src_templ.target = gl_target_to_pipe(gl_target);
794   src_templ.format = src_format;
795   src_templ.bind = PIPE_BIND_SAMPLER_VIEW;
796   src_templ.usage = PIPE_USAGE_STAGING;
797
798   st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
799                                   &src_templ.width0, &src_templ.height0,
800                                   &src_templ.depth0, &src_templ.array_size);
801
802   /* Check for NPOT texture support. */
803   if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES) &&
804       (!util_is_power_of_two(src_templ.width0) ||
805        !util_is_power_of_two(src_templ.height0) ||
806        !util_is_power_of_two(src_templ.depth0))) {
807      goto fallback;
808   }
809
810   /* Create the source texture. */
811   src = screen->resource_create(screen, &src_templ);
812   if (!src) {
813      goto fallback;
814   }
815
816   /* Map source pixels. */
817   pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
818                                        format, type, pixels, unpack,
819                                        "glTexSubImage");
820   if (!pixels) {
821      /* This is a GL error. */
822      pipe_resource_reference(&src, NULL);
823      return;
824   }
825
826   /* From now on, we need the gallium representation of dimensions. */
827   if (gl_target == GL_TEXTURE_1D_ARRAY) {
828      zoffset = yoffset;
829      yoffset = 0;
830      depth = height;
831      height = 1;
832   }
833
834   map = pipe_transfer_map_3d(pipe, src, 0, PIPE_TRANSFER_WRITE, 0, 0, 0,
835                              width, height, depth, &transfer);
836   if (!map) {
837      _mesa_unmap_teximage_pbo(ctx, unpack);
838      pipe_resource_reference(&src, NULL);
839      goto fallback;
840   }
841
842   /* Upload pixels (just memcpy). */
843   {
844      const uint bytesPerRow = width * util_format_get_blocksize(src_format);
845      GLuint row, slice;
846
847      for (slice = 0; slice < (unsigned) depth; slice++) {
848         if (gl_target == GL_TEXTURE_1D_ARRAY) {
849            /* 1D array textures.
850             * We need to convert gallium coords to GL coords.
851             */
852            GLvoid *src = _mesa_image_address2d(unpack, pixels,
853                                                width, depth, format,
854                                                type, slice, 0);
855            memcpy(map, src, bytesPerRow);
856         }
857         else {
858            ubyte *slice_map = map;
859
860            for (row = 0; row < (unsigned) height; row++) {
861               GLvoid *src = _mesa_image_address(dims, unpack, pixels,
862                                                 width, height, format,
863                                                 type, slice, row, 0);
864               memcpy(slice_map, src, bytesPerRow);
865               slice_map += transfer->stride;
866            }
867         }
868         map += transfer->layer_stride;
869      }
870   }
871
872   pipe_transfer_unmap(pipe, transfer);
873   _mesa_unmap_teximage_pbo(ctx, unpack);
874
875   /* Blit. */
876   memset(&blit, 0, sizeof(blit));
877   blit.src.resource = src;
878   blit.src.level = 0;
879   blit.src.format = src_format;
880   blit.dst.resource = dst;
881   blit.dst.level = stObj->pt != stImage->pt ? 0 : texImage->TexObject->MinLevel + texImage->Level;
882   blit.dst.format = dst_format;
883   blit.src.box.x = blit.src.box.y = blit.src.box.z = 0;
884   blit.dst.box.x = xoffset;
885   blit.dst.box.y = yoffset;
886   blit.dst.box.z = zoffset + texImage->Face + texImage->TexObject->MinLayer;
887   blit.src.box.width = blit.dst.box.width = width;
888   blit.src.box.height = blit.dst.box.height = height;
889   blit.src.box.depth = blit.dst.box.depth = depth;
890   blit.mask = st_get_blit_mask(format, texImage->_BaseFormat);
891   blit.filter = PIPE_TEX_FILTER_NEAREST;
892   blit.scissor_enable = FALSE;
893
894   st->pipe->blit(st->pipe, &blit);
895
896   pipe_resource_reference(&src, NULL);
897   return;
898
899fallback:
900   _mesa_store_texsubimage(ctx, dims, texImage, xoffset, yoffset, zoffset,
901                           width, height, depth, format, type, pixels,
902                           unpack);
903}
904
905static void
906st_TexImage(struct gl_context * ctx, GLuint dims,
907            struct gl_texture_image *texImage,
908            GLenum format, GLenum type, const void *pixels,
909            const struct gl_pixelstore_attrib *unpack)
910{
911   assert(dims == 1 || dims == 2 || dims == 3);
912
913   prep_teximage(ctx, texImage, format, type);
914
915   if (texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
916      return;
917
918   /* allocate storage for texture data */
919   if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
920      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
921      return;
922   }
923
924   st_TexSubImage(ctx, dims, texImage, 0, 0, 0,
925                  texImage->Width, texImage->Height, texImage->Depth,
926                  format, type, pixels, unpack);
927}
928
929
930static void
931st_CompressedTexImage(struct gl_context *ctx, GLuint dims,
932                      struct gl_texture_image *texImage,
933                      GLsizei imageSize, const GLvoid *data)
934{
935   prep_teximage(ctx, texImage, GL_NONE, GL_NONE);
936   _mesa_store_compressed_teximage(ctx, dims, texImage, imageSize, data);
937}
938
939
940
941
942/**
943 * Called via ctx->Driver.GetTexSubImage()
944 *
945 * This uses a blit to copy the texture to a texture format which matches
946 * the format and type combo and then a fast read-back is done using memcpy.
947 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
948 * a format which matches the swizzling.
949 *
950 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
951 *
952 * NOTE: Drivers usually do a blit to convert between tiled and linear
953 *       texture layouts during texture uploads/downloads, so the blit
954 *       we do here should be free in such cases.
955 */
956static void
957st_GetTexSubImage(struct gl_context * ctx,
958                  GLint xoffset, GLint yoffset, GLint zoffset,
959                  GLsizei width, GLsizei height, GLint depth,
960                  GLenum format, GLenum type, GLvoid * pixels,
961                  struct gl_texture_image *texImage)
962{
963   struct st_context *st = st_context(ctx);
964   struct pipe_context *pipe = st->pipe;
965   struct pipe_screen *screen = pipe->screen;
966   struct st_texture_image *stImage = st_texture_image(texImage);
967   struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
968   struct pipe_resource *src = stObj->pt;
969   struct pipe_resource *dst = NULL;
970   struct pipe_resource dst_templ;
971   enum pipe_format dst_format, src_format;
972   mesa_format mesa_format;
973   GLenum gl_target = texImage->TexObject->Target;
974   enum pipe_texture_target pipe_target;
975   struct pipe_blit_info blit;
976   unsigned bind = PIPE_BIND_TRANSFER_READ;
977   struct pipe_transfer *tex_xfer;
978   ubyte *map = NULL;
979   boolean done = FALSE;
980
981   assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
982          texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
983
984   if (!st->prefer_blit_based_texture_transfer &&
985       !_mesa_is_format_compressed(texImage->TexFormat)) {
986      /* Try to avoid the fallback if we're doing texture decompression here */
987      goto fallback;
988   }
989
990   if (!stImage->pt || !src) {
991      goto fallback;
992   }
993
994   /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
995    * due to an incomplete stencil blit implementation in some drivers. */
996   if (format == GL_DEPTH_STENCIL || format == GL_STENCIL_INDEX) {
997      goto fallback;
998   }
999
1000   /* If the base internal format and the texture format don't match, we have
1001    * to fall back to _mesa_GetTexImage_sw. */
1002   if (texImage->_BaseFormat !=
1003       _mesa_get_format_base_format(texImage->TexFormat)) {
1004      goto fallback;
1005   }
1006
1007   /* See if the texture format already matches the format and type,
1008    * in which case the memcpy-based fast path will be used. */
1009   if (_mesa_format_matches_format_and_type(texImage->TexFormat, format,
1010                                            type, ctx->Pack.SwapBytes, NULL)) {
1011      goto fallback;
1012   }
1013
1014   /* Convert the source format to what is expected by GetTexImage
1015    * and see if it's supported.
1016    *
1017    * This only applies to glGetTexImage:
1018    * - Luminance must be returned as (L,0,0,1).
1019    * - Luminance alpha must be returned as (L,0,0,A).
1020    * - Intensity must be returned as (I,0,0,1)
1021    */
1022   if (stObj->surface_based)
1023      src_format = util_format_linear(stObj->surface_format);
1024   else
1025      src_format = util_format_linear(src->format);
1026   src_format = util_format_luminance_to_red(src_format);
1027   src_format = util_format_intensity_to_red(src_format);
1028
1029   if (!src_format ||
1030       !screen->is_format_supported(screen, src_format, src->target,
1031                                    src->nr_samples,
1032                                    PIPE_BIND_SAMPLER_VIEW)) {
1033      goto fallback;
1034   }
1035
1036   if (format == GL_DEPTH_COMPONENT || format == GL_DEPTH_STENCIL)
1037      bind |= PIPE_BIND_DEPTH_STENCIL;
1038   else
1039      bind |= PIPE_BIND_RENDER_TARGET;
1040
1041   /* GetTexImage only returns a single face for cubemaps. */
1042   if (gl_target == GL_TEXTURE_CUBE_MAP) {
1043      gl_target = GL_TEXTURE_2D;
1044   }
1045   pipe_target = gl_target_to_pipe(gl_target);
1046
1047   /* Choose the destination format by finding the best match
1048    * for the format+type combo. */
1049   dst_format = st_choose_matching_format(st, bind, format, type,
1050					  ctx->Pack.SwapBytes);
1051
1052   if (dst_format == PIPE_FORMAT_NONE) {
1053      GLenum dst_glformat;
1054
1055      /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1056       * where decompression with a blit is always preferred. */
1057      if (!util_format_is_compressed(src->format)) {
1058         goto fallback;
1059      }
1060
1061      /* Set the appropriate format for the decompressed texture.
1062       * Luminance and sRGB formats shouldn't appear here.*/
1063      switch (src_format) {
1064      case PIPE_FORMAT_DXT1_RGB:
1065      case PIPE_FORMAT_DXT1_RGBA:
1066      case PIPE_FORMAT_DXT3_RGBA:
1067      case PIPE_FORMAT_DXT5_RGBA:
1068      case PIPE_FORMAT_RGTC1_UNORM:
1069      case PIPE_FORMAT_RGTC2_UNORM:
1070      case PIPE_FORMAT_ETC1_RGB8:
1071      case PIPE_FORMAT_BPTC_RGBA_UNORM:
1072         dst_glformat = GL_RGBA8;
1073         break;
1074      case PIPE_FORMAT_RGTC1_SNORM:
1075      case PIPE_FORMAT_RGTC2_SNORM:
1076         if (!ctx->Extensions.EXT_texture_snorm)
1077            goto fallback;
1078         dst_glformat = GL_RGBA8_SNORM;
1079         break;
1080      case PIPE_FORMAT_BPTC_RGB_FLOAT:
1081      case PIPE_FORMAT_BPTC_RGB_UFLOAT:
1082         if (!ctx->Extensions.ARB_texture_float)
1083            goto fallback;
1084         dst_glformat = GL_RGBA32F;
1085         break;
1086      default:
1087         assert(0);
1088         goto fallback;
1089      }
1090
1091      dst_format = st_choose_format(st, dst_glformat, format, type,
1092                                    pipe_target, 0, bind, FALSE);
1093
1094      if (dst_format == PIPE_FORMAT_NONE) {
1095         /* unable to get an rgba format!?! */
1096         goto fallback;
1097      }
1098   }
1099
1100   /* create the destination texture of size (width X height X depth) */
1101   memset(&dst_templ, 0, sizeof(dst_templ));
1102   dst_templ.target = pipe_target;
1103   dst_templ.format = dst_format;
1104   dst_templ.bind = bind;
1105   dst_templ.usage = PIPE_USAGE_STAGING;
1106
1107   st_gl_texture_dims_to_pipe_dims(gl_target, width, height, depth,
1108                                   &dst_templ.width0, &dst_templ.height0,
1109                                   &dst_templ.depth0, &dst_templ.array_size);
1110
1111   dst = screen->resource_create(screen, &dst_templ);
1112   if (!dst) {
1113      goto fallback;
1114   }
1115
1116   /* From now on, we need the gallium representation of dimensions. */
1117   if (gl_target == GL_TEXTURE_1D_ARRAY) {
1118      zoffset = yoffset;
1119      yoffset = 0;
1120      depth = height;
1121      height = 1;
1122   }
1123
1124   assert(texImage->Face == 0 ||
1125          texImage->TexObject->MinLayer == 0 ||
1126          zoffset == 0);
1127
1128   memset(&blit, 0, sizeof(blit));
1129   blit.src.resource = src;
1130   blit.src.level = texImage->Level + texImage->TexObject->MinLevel;
1131   blit.src.format = src_format;
1132   blit.dst.resource = dst;
1133   blit.dst.level = 0;
1134   blit.dst.format = dst->format;
1135   blit.src.box.x = xoffset;
1136   blit.dst.box.x = 0;
1137   blit.src.box.y = yoffset;
1138   blit.dst.box.y = 0;
1139   blit.src.box.z = texImage->Face + texImage->TexObject->MinLayer + zoffset;
1140   blit.dst.box.z = 0;
1141   blit.src.box.width = blit.dst.box.width = width;
1142   blit.src.box.height = blit.dst.box.height = height;
1143   blit.src.box.depth = blit.dst.box.depth = depth;
1144   blit.mask = st_get_blit_mask(texImage->_BaseFormat, format);
1145   blit.filter = PIPE_TEX_FILTER_NEAREST;
1146   blit.scissor_enable = FALSE;
1147
1148   /* blit/render/decompress */
1149   st->pipe->blit(st->pipe, &blit);
1150
1151   pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
1152
1153   map = pipe_transfer_map_3d(pipe, dst, 0, PIPE_TRANSFER_READ,
1154                              0, 0, 0, width, height, depth, &tex_xfer);
1155   if (!map) {
1156      goto end;
1157   }
1158
1159   mesa_format = st_pipe_format_to_mesa_format(dst_format);
1160
1161   /* copy/pack data into user buffer */
1162   if (_mesa_format_matches_format_and_type(mesa_format, format, type,
1163                                            ctx->Pack.SwapBytes, NULL)) {
1164      /* memcpy */
1165      const uint bytesPerRow = width * util_format_get_blocksize(dst_format);
1166      GLuint row, slice;
1167
1168      for (slice = 0; slice < depth; slice++) {
1169         if (gl_target == GL_TEXTURE_1D_ARRAY) {
1170            /* 1D array textures.
1171             * We need to convert gallium coords to GL coords.
1172             */
1173            GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
1174                                                 width, depth, format,
1175                                                 type, 0, slice, 0);
1176            memcpy(dest, map, bytesPerRow);
1177         }
1178         else {
1179            ubyte *slice_map = map;
1180
1181            for (row = 0; row < height; row++) {
1182               GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
1183                                                    width, height, format,
1184                                                    type, slice, row, 0);
1185               memcpy(dest, slice_map, bytesPerRow);
1186               slice_map += tex_xfer->stride;
1187            }
1188         }
1189         map += tex_xfer->layer_stride;
1190      }
1191   }
1192   else {
1193      /* format translation via floats */
1194      GLuint row, slice;
1195      GLfloat *rgba;
1196      uint32_t dstMesaFormat;
1197      int dstStride, srcStride;
1198
1199      assert(util_format_is_compressed(src->format));
1200
1201      rgba = malloc(width * 4 * sizeof(GLfloat));
1202      if (!rgba) {
1203         goto end;
1204      }
1205
1206      if (ST_DEBUG & DEBUG_FALLBACK)
1207         debug_printf("%s: fallback format translation\n", __func__);
1208
1209      dstMesaFormat = _mesa_format_from_format_and_type(format, type);
1210      dstStride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
1211      srcStride = 4 * width * sizeof(GLfloat);
1212      for (slice = 0; slice < depth; slice++) {
1213         if (gl_target == GL_TEXTURE_1D_ARRAY) {
1214            /* 1D array textures.
1215             * We need to convert gallium coords to GL coords.
1216             */
1217            GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
1218                                                 width, depth, format,
1219                                                 type, 0, slice, 0);
1220
1221            /* get float[4] rgba row from surface */
1222            pipe_get_tile_rgba_format(tex_xfer, map, 0, 0, width, 1,
1223                                      dst_format, rgba);
1224
1225            _mesa_format_convert(dest, dstMesaFormat, dstStride,
1226                                 rgba, RGBA32_FLOAT, srcStride,
1227                                 width, 1, NULL);
1228         }
1229         else {
1230            for (row = 0; row < height; row++) {
1231               GLvoid *dest = _mesa_image_address3d(&ctx->Pack, pixels,
1232                                                    width, height, format,
1233                                                    type, slice, row, 0);
1234
1235               /* get float[4] rgba row from surface */
1236               pipe_get_tile_rgba_format(tex_xfer, map, 0, row, width, 1,
1237                                         dst_format, rgba);
1238
1239               _mesa_format_convert(dest, dstMesaFormat, dstStride,
1240                                    rgba, RGBA32_FLOAT, srcStride,
1241                                    width, 1, NULL);
1242            }
1243         }
1244         map += tex_xfer->layer_stride;
1245      }
1246
1247      free(rgba);
1248   }
1249   done = TRUE;
1250
1251end:
1252   if (map)
1253      pipe_transfer_unmap(pipe, tex_xfer);
1254
1255   _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
1256   pipe_resource_reference(&dst, NULL);
1257
1258fallback:
1259   if (!done) {
1260      _mesa_GetTexSubImage_sw(ctx, xoffset, yoffset, zoffset,
1261                              width, height, depth,
1262                              format, type, pixels, texImage);
1263   }
1264}
1265
1266
1267/**
1268 * Do a CopyTexSubImage operation using a read transfer from the source,
1269 * a write transfer to the destination and get_tile()/put_tile() to access
1270 * the pixels/texels.
1271 *
1272 * Note: srcY=0=TOP of renderbuffer
1273 */
1274static void
1275fallback_copy_texsubimage(struct gl_context *ctx,
1276                          struct st_renderbuffer *strb,
1277                          struct st_texture_image *stImage,
1278                          GLenum baseFormat,
1279                          GLint destX, GLint destY, GLint slice,
1280                          GLint srcX, GLint srcY,
1281                          GLsizei width, GLsizei height)
1282{
1283   struct st_context *st = st_context(ctx);
1284   struct pipe_context *pipe = st->pipe;
1285   struct pipe_transfer *src_trans;
1286   GLubyte *texDest;
1287   enum pipe_transfer_usage transfer_usage;
1288   void *map;
1289   unsigned dst_width = width;
1290   unsigned dst_height = height;
1291   unsigned dst_depth = 1;
1292   struct pipe_transfer *transfer;
1293
1294   if (ST_DEBUG & DEBUG_FALLBACK)
1295      debug_printf("%s: fallback processing\n", __func__);
1296
1297   if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1298      srcY = strb->Base.Height - srcY - height;
1299   }
1300
1301   map = pipe_transfer_map(pipe,
1302                           strb->texture,
1303                           strb->surface->u.tex.level,
1304                           strb->surface->u.tex.first_layer,
1305                           PIPE_TRANSFER_READ,
1306                           srcX, srcY,
1307                           width, height, &src_trans);
1308
1309   if ((baseFormat == GL_DEPTH_COMPONENT ||
1310        baseFormat == GL_DEPTH_STENCIL) &&
1311       util_format_is_depth_and_stencil(stImage->pt->format))
1312      transfer_usage = PIPE_TRANSFER_READ_WRITE;
1313   else
1314      transfer_usage = PIPE_TRANSFER_WRITE;
1315
1316   texDest = st_texture_image_map(st, stImage, transfer_usage,
1317                                  destX, destY, slice,
1318                                  dst_width, dst_height, dst_depth,
1319                                  &transfer);
1320
1321   if (baseFormat == GL_DEPTH_COMPONENT ||
1322       baseFormat == GL_DEPTH_STENCIL) {
1323      const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
1324                                     ctx->Pixel.DepthBias != 0.0F);
1325      GLint row, yStep;
1326      uint *data;
1327
1328      /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1329      if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1330         srcY = height - 1;
1331         yStep = -1;
1332      }
1333      else {
1334         srcY = 0;
1335         yStep = 1;
1336      }
1337
1338      data = malloc(width * sizeof(uint));
1339
1340      if (data) {
1341         /* To avoid a large temp memory allocation, do copy row by row */
1342         for (row = 0; row < height; row++, srcY += yStep) {
1343            pipe_get_tile_z(src_trans, map, 0, srcY, width, 1, data);
1344            if (scaleOrBias) {
1345               _mesa_scale_and_bias_depth_uint(ctx, width, data);
1346            }
1347
1348            if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
1349               pipe_put_tile_z(transfer, texDest + row*transfer->layer_stride,
1350                               0, 0, width, 1, data);
1351            }
1352            else {
1353               pipe_put_tile_z(transfer, texDest, 0, row, width, 1, data);
1354            }
1355         }
1356      }
1357      else {
1358         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
1359      }
1360
1361      free(data);
1362   }
1363   else {
1364      /* RGBA format */
1365      GLfloat *tempSrc =
1366         malloc(width * height * 4 * sizeof(GLfloat));
1367
1368      if (tempSrc && texDest) {
1369         const GLint dims = 2;
1370         GLint dstRowStride;
1371         struct gl_texture_image *texImage = &stImage->base;
1372         struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
1373
1374         if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1375            unpack.Invert = GL_TRUE;
1376         }
1377
1378         if (stImage->pt->target == PIPE_TEXTURE_1D_ARRAY) {
1379            dstRowStride = transfer->layer_stride;
1380         }
1381         else {
1382            dstRowStride = transfer->stride;
1383         }
1384
1385         /* get float/RGBA image from framebuffer */
1386         /* XXX this usually involves a lot of int/float conversion.
1387          * try to avoid that someday.
1388          */
1389         pipe_get_tile_rgba_format(src_trans, map, 0, 0, width, height,
1390                                   util_format_linear(strb->texture->format),
1391                                   tempSrc);
1392
1393         /* Store into texture memory.
1394          * Note that this does some special things such as pixel transfer
1395          * ops and format conversion.  In particular, if the dest tex format
1396          * is actually RGBA but the user created the texture as GL_RGB we
1397          * need to fill-in/override the alpha channel with 1.0.
1398          */
1399         _mesa_texstore(ctx, dims,
1400                        texImage->_BaseFormat,
1401                        texImage->TexFormat,
1402                        dstRowStride,
1403                        &texDest,
1404                        width, height, 1,
1405                        GL_RGBA, GL_FLOAT, tempSrc, /* src */
1406                        &unpack);
1407      }
1408      else {
1409         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1410      }
1411
1412      free(tempSrc);
1413   }
1414
1415   st_texture_image_unmap(st, stImage, slice);
1416   pipe->transfer_unmap(pipe, src_trans);
1417}
1418
1419
1420/**
1421 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1422 * Note that the region to copy has already been clipped so we know we
1423 * won't read from outside the source renderbuffer's bounds.
1424 *
1425 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1426 */
1427static void
1428st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
1429                   struct gl_texture_image *texImage,
1430                   GLint destX, GLint destY, GLint slice,
1431                   struct gl_renderbuffer *rb,
1432                   GLint srcX, GLint srcY, GLsizei width, GLsizei height)
1433{
1434   struct st_texture_image *stImage = st_texture_image(texImage);
1435   struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
1436   struct st_renderbuffer *strb = st_renderbuffer(rb);
1437   struct st_context *st = st_context(ctx);
1438   struct pipe_context *pipe = st->pipe;
1439   struct pipe_screen *screen = pipe->screen;
1440   struct pipe_blit_info blit;
1441   enum pipe_format dst_format;
1442   GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1443   unsigned bind;
1444   GLint srcY0, srcY1;
1445
1446   assert(!_mesa_is_format_etc2(texImage->TexFormat) &&
1447          texImage->TexFormat != MESA_FORMAT_ETC1_RGB8);
1448
1449   if (!strb || !strb->surface || !stImage->pt) {
1450      debug_printf("%s: null strb or stImage\n", __func__);
1451      return;
1452   }
1453
1454   if (_mesa_texstore_needs_transfer_ops(ctx, texImage->_BaseFormat,
1455                                         texImage->TexFormat)) {
1456      goto fallback;
1457   }
1458
1459   /* The base internal format must match the mesa format, so make sure
1460    * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
1461    */
1462   if (texImage->_BaseFormat !=
1463       _mesa_get_format_base_format(texImage->TexFormat) ||
1464       rb->_BaseFormat != _mesa_get_format_base_format(rb->Format)) {
1465      goto fallback;
1466   }
1467
1468   /* Choose the destination format to match the TexImage behavior. */
1469   dst_format = util_format_linear(stImage->pt->format);
1470   dst_format = util_format_luminance_to_red(dst_format);
1471   dst_format = util_format_intensity_to_red(dst_format);
1472
1473   /* See if the destination format is supported. */
1474   if (texImage->_BaseFormat == GL_DEPTH_STENCIL ||
1475       texImage->_BaseFormat == GL_DEPTH_COMPONENT) {
1476      bind = PIPE_BIND_DEPTH_STENCIL;
1477   }
1478   else {
1479      bind = PIPE_BIND_RENDER_TARGET;
1480   }
1481
1482   if (!dst_format ||
1483       !screen->is_format_supported(screen, dst_format, stImage->pt->target,
1484                                    stImage->pt->nr_samples, bind)) {
1485      goto fallback;
1486   }
1487
1488   /* Y flipping for the main framebuffer. */
1489   if (do_flip) {
1490      srcY1 = strb->Base.Height - srcY - height;
1491      srcY0 = srcY1 + height;
1492   }
1493   else {
1494      srcY0 = srcY;
1495      srcY1 = srcY0 + height;
1496   }
1497
1498   /* Blit the texture.
1499    * This supports flipping, format conversions, and downsampling.
1500    */
1501   memset(&blit, 0, sizeof(blit));
1502   blit.src.resource = strb->texture;
1503   blit.src.format = util_format_linear(strb->surface->format);
1504   blit.src.level = strb->surface->u.tex.level;
1505   blit.src.box.x = srcX;
1506   blit.src.box.y = srcY0;
1507   blit.src.box.z = strb->surface->u.tex.first_layer;
1508   blit.src.box.width = width;
1509   blit.src.box.height = srcY1 - srcY0;
1510   blit.src.box.depth = 1;
1511   blit.dst.resource = stImage->pt;
1512   blit.dst.format = dst_format;
1513   blit.dst.level = stObj->pt != stImage->pt ? 0 : texImage->Level + texImage->TexObject->MinLevel;
1514   blit.dst.box.x = destX;
1515   blit.dst.box.y = destY;
1516   blit.dst.box.z = stImage->base.Face + slice + texImage->TexObject->MinLayer;
1517   blit.dst.box.width = width;
1518   blit.dst.box.height = height;
1519   blit.dst.box.depth = 1;
1520   blit.mask = st_get_blit_mask(rb->_BaseFormat, texImage->_BaseFormat);
1521   blit.filter = PIPE_TEX_FILTER_NEAREST;
1522   pipe->blit(pipe, &blit);
1523   return;
1524
1525fallback:
1526   /* software fallback */
1527   fallback_copy_texsubimage(ctx,
1528                             strb, stImage, texImage->_BaseFormat,
1529                             destX, destY, slice,
1530                             srcX, srcY, width, height);
1531}
1532
1533
1534/**
1535 * Copy image data from stImage into the texture object 'stObj' at level
1536 * 'dstLevel'.
1537 */
1538static void
1539copy_image_data_to_texture(struct st_context *st,
1540			   struct st_texture_object *stObj,
1541                           GLuint dstLevel,
1542			   struct st_texture_image *stImage)
1543{
1544   /* debug checks */
1545   {
1546      const struct gl_texture_image *dstImage =
1547         stObj->base.Image[stImage->base.Face][dstLevel];
1548      assert(dstImage);
1549      assert(dstImage->Width == stImage->base.Width);
1550      assert(dstImage->Height == stImage->base.Height);
1551      assert(dstImage->Depth == stImage->base.Depth);
1552   }
1553
1554   if (stImage->pt) {
1555      /* Copy potentially with the blitter:
1556       */
1557      GLuint src_level;
1558      if (stImage->pt->last_level == 0)
1559         src_level = 0;
1560      else
1561         src_level = stImage->base.Level;
1562
1563      assert(src_level <= stImage->pt->last_level);
1564      assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width);
1565      assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY ||
1566             u_minify(stImage->pt->height0, src_level) == stImage->base.Height);
1567      assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY ||
1568             stImage->pt->target == PIPE_TEXTURE_CUBE_ARRAY ||
1569             u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth);
1570
1571      st_texture_image_copy(st->pipe,
1572                            stObj->pt, dstLevel,  /* dest texture, level */
1573                            stImage->pt, src_level, /* src texture, level */
1574                            stImage->base.Face);
1575
1576      pipe_resource_reference(&stImage->pt, NULL);
1577   }
1578   pipe_resource_reference(&stImage->pt, stObj->pt);
1579}
1580
1581
1582/**
1583 * Called during state validation.  When this function is finished,
1584 * the texture object should be ready for rendering.
1585 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1586 */
1587GLboolean
1588st_finalize_texture(struct gl_context *ctx,
1589		    struct pipe_context *pipe,
1590		    struct gl_texture_object *tObj)
1591{
1592   struct st_context *st = st_context(ctx);
1593   struct st_texture_object *stObj = st_texture_object(tObj);
1594   const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1595   GLuint face;
1596   const struct st_texture_image *firstImage;
1597   enum pipe_format firstImageFormat;
1598   GLuint ptWidth, ptHeight, ptDepth, ptLayers, ptNumSamples;
1599
1600   if (tObj->Immutable)
1601      return GL_TRUE;
1602
1603   if (_mesa_is_texture_complete(tObj, &tObj->Sampler)) {
1604      /* The texture is complete and we know exactly how many mipmap levels
1605       * are present/needed.  This is conditional because we may be called
1606       * from the st_generate_mipmap() function when the texture object is
1607       * incomplete.  In that case, we'll have set stObj->lastLevel before
1608       * we get here.
1609       */
1610      if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
1611          stObj->base.Sampler.MinFilter == GL_NEAREST)
1612         stObj->lastLevel = stObj->base.BaseLevel;
1613      else
1614         stObj->lastLevel = stObj->base._MaxLevel;
1615   }
1616
1617   if (tObj->Target == GL_TEXTURE_BUFFER) {
1618      struct st_buffer_object *st_obj = st_buffer_object(tObj->BufferObject);
1619
1620      if (!st_obj) {
1621         pipe_resource_reference(&stObj->pt, NULL);
1622         st_texture_release_all_sampler_views(st, stObj);
1623         return GL_TRUE;
1624      }
1625
1626      if (st_obj->buffer != stObj->pt) {
1627         pipe_resource_reference(&stObj->pt, st_obj->buffer);
1628         st_texture_release_all_sampler_views(st, stObj);
1629         stObj->width0 = stObj->pt->width0 / _mesa_get_format_bytes(tObj->_BufferObjectFormat);
1630         stObj->height0 = 1;
1631         stObj->depth0 = 1;
1632      }
1633      return GL_TRUE;
1634
1635   }
1636
1637   firstImage = st_texture_image_const(_mesa_base_tex_image(&stObj->base));
1638   assert(firstImage);
1639
1640   /* If both firstImage and stObj point to a texture which can contain
1641    * all active images, favour firstImage.  Note that because of the
1642    * completeness requirement, we know that the image dimensions
1643    * will match.
1644    */
1645   if (firstImage->pt &&
1646       firstImage->pt != stObj->pt &&
1647       (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
1648      pipe_resource_reference(&stObj->pt, firstImage->pt);
1649      st_texture_release_all_sampler_views(st, stObj);
1650   }
1651
1652   /* If this texture comes from a window system, there is nothing else to do. */
1653   if (stObj->surface_based) {
1654      return GL_TRUE;
1655   }
1656
1657   /* Find gallium format for the Mesa texture */
1658   firstImageFormat =
1659      st_mesa_format_to_pipe_format(st, firstImage->base.TexFormat);
1660
1661   /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1662   {
1663      GLuint width, height, depth;
1664      if (!guess_base_level_size(stObj->base.Target,
1665                                 firstImage->base.Width2,
1666                                 firstImage->base.Height2,
1667                                 firstImage->base.Depth2,
1668                                 firstImage->base.Level,
1669                                 &width, &height, &depth)) {
1670         width = stObj->width0;
1671         height = stObj->height0;
1672         depth = stObj->depth0;
1673      } else {
1674         /* The width/height/depth may have been previously reset in
1675          * guess_and_alloc_texture. */
1676         stObj->width0 = width;
1677         stObj->height0 = height;
1678         stObj->depth0 = depth;
1679      }
1680      /* convert GL dims to Gallium dims */
1681      st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
1682                                      &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1683      ptNumSamples = firstImage->base.NumSamples;
1684   }
1685
1686   /* If we already have a gallium texture, check that it matches the texture
1687    * object's format, target, size, num_levels, etc.
1688    */
1689   if (stObj->pt) {
1690      if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1691          stObj->pt->format != firstImageFormat ||
1692          stObj->pt->last_level < stObj->lastLevel ||
1693          stObj->pt->width0 != ptWidth ||
1694          stObj->pt->height0 != ptHeight ||
1695          stObj->pt->depth0 != ptDepth ||
1696          stObj->pt->nr_samples != ptNumSamples ||
1697          stObj->pt->array_size != ptLayers)
1698      {
1699         /* The gallium texture does not match the Mesa texture so delete the
1700          * gallium texture now.  We'll make a new one below.
1701          */
1702         pipe_resource_reference(&stObj->pt, NULL);
1703         st_texture_release_all_sampler_views(st, stObj);
1704         st->dirty.st |= ST_NEW_FRAMEBUFFER;
1705      }
1706   }
1707
1708   /* May need to create a new gallium texture:
1709    */
1710   if (!stObj->pt) {
1711      GLuint bindings = default_bindings(st, firstImageFormat);
1712
1713      stObj->pt = st_texture_create(st,
1714                                    gl_target_to_pipe(stObj->base.Target),
1715                                    firstImageFormat,
1716                                    stObj->lastLevel,
1717                                    ptWidth,
1718                                    ptHeight,
1719                                    ptDepth,
1720                                    ptLayers, ptNumSamples,
1721                                    bindings);
1722
1723      if (!stObj->pt) {
1724         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1725         return GL_FALSE;
1726      }
1727   }
1728
1729   /* Pull in any images not in the object's texture:
1730    */
1731   for (face = 0; face < nr_faces; face++) {
1732      GLuint level;
1733      for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1734         struct st_texture_image *stImage =
1735            st_texture_image(stObj->base.Image[face][level]);
1736
1737         /* Need to import images in main memory or held in other textures.
1738          */
1739         if (stImage && stObj->pt != stImage->pt) {
1740            if (level == 0 ||
1741                (stImage->base.Width == u_minify(stObj->width0, level) &&
1742                 stImage->base.Height == u_minify(stObj->height0, level) &&
1743                 stImage->base.Depth == u_minify(stObj->depth0, level))) {
1744               /* src image fits expected dest mipmap level size */
1745               copy_image_data_to_texture(st, stObj, level, stImage);
1746            }
1747         }
1748      }
1749   }
1750
1751   return GL_TRUE;
1752}
1753
1754
1755/**
1756 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1757 * for a whole mipmap stack.
1758 */
1759static GLboolean
1760st_AllocTextureStorage(struct gl_context *ctx,
1761                       struct gl_texture_object *texObj,
1762                       GLsizei levels, GLsizei width,
1763                       GLsizei height, GLsizei depth)
1764{
1765   const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
1766   struct gl_texture_image *texImage = texObj->Image[0][0];
1767   struct st_context *st = st_context(ctx);
1768   struct st_texture_object *stObj = st_texture_object(texObj);
1769   struct pipe_screen *screen = st->pipe->screen;
1770   GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
1771   enum pipe_format fmt;
1772   GLint level;
1773   GLuint num_samples = texImage->NumSamples;
1774
1775   assert(levels > 0);
1776
1777   /* Save the level=0 dimensions */
1778   stObj->width0 = width;
1779   stObj->height0 = height;
1780   stObj->depth0 = depth;
1781   stObj->lastLevel = levels - 1;
1782
1783   fmt = st_mesa_format_to_pipe_format(st, texImage->TexFormat);
1784
1785   bindings = default_bindings(st, fmt);
1786
1787   /* Raise the sample count if the requested one is unsupported. */
1788   if (num_samples > 1) {
1789      boolean found = FALSE;
1790
1791      for (; num_samples <= ctx->Const.MaxSamples; num_samples++) {
1792         if (screen->is_format_supported(screen, fmt, PIPE_TEXTURE_2D,
1793                                         num_samples,
1794                                         PIPE_BIND_SAMPLER_VIEW)) {
1795            /* Update the sample count in gl_texture_image as well. */
1796            texImage->NumSamples = num_samples;
1797            found = TRUE;
1798            break;
1799         }
1800      }
1801
1802      if (!found) {
1803         return GL_FALSE;
1804      }
1805   }
1806
1807   st_gl_texture_dims_to_pipe_dims(texObj->Target,
1808                                   width, height, depth,
1809                                   &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1810
1811   stObj->pt = st_texture_create(st,
1812                                 gl_target_to_pipe(texObj->Target),
1813                                 fmt,
1814                                 levels - 1,
1815                                 ptWidth,
1816                                 ptHeight,
1817                                 ptDepth,
1818                                 ptLayers, num_samples,
1819                                 bindings);
1820   if (!stObj->pt)
1821      return GL_FALSE;
1822
1823   /* Set image resource pointers */
1824   for (level = 0; level < levels; level++) {
1825      GLuint face;
1826      for (face = 0; face < numFaces; face++) {
1827         struct st_texture_image *stImage =
1828            st_texture_image(texObj->Image[face][level]);
1829         pipe_resource_reference(&stImage->pt, stObj->pt);
1830      }
1831   }
1832
1833   return GL_TRUE;
1834}
1835
1836
1837static GLboolean
1838st_TestProxyTexImage(struct gl_context *ctx, GLenum target,
1839                     GLint level, mesa_format format,
1840                     GLint width, GLint height,
1841                     GLint depth, GLint border)
1842{
1843   struct st_context *st = st_context(ctx);
1844   struct pipe_context *pipe = st->pipe;
1845
1846   if (width == 0 || height == 0 || depth == 0) {
1847      /* zero-sized images are legal, and always fit! */
1848      return GL_TRUE;
1849   }
1850
1851   if (pipe->screen->can_create_resource) {
1852      /* Ask the gallium driver if the texture is too large */
1853      struct gl_texture_object *texObj =
1854         _mesa_get_current_tex_object(ctx, target);
1855      struct pipe_resource pt;
1856
1857      /* Setup the pipe_resource object
1858       */
1859      memset(&pt, 0, sizeof(pt));
1860
1861      pt.target = gl_target_to_pipe(target);
1862      pt.format = st_mesa_format_to_pipe_format(st, format);
1863
1864      st_gl_texture_dims_to_pipe_dims(target,
1865                                      width, height, depth,
1866                                      &pt.width0, &pt.height0,
1867                                      &pt.depth0, &pt.array_size);
1868
1869      if (level == 0 && (texObj->Sampler.MinFilter == GL_LINEAR ||
1870                         texObj->Sampler.MinFilter == GL_NEAREST)) {
1871         /* assume just one mipmap level */
1872         pt.last_level = 0;
1873      }
1874      else {
1875         /* assume a full set of mipmaps */
1876         pt.last_level = _mesa_logbase2(MAX3(width, height, depth));
1877      }
1878
1879      return pipe->screen->can_create_resource(pipe->screen, &pt);
1880   }
1881   else {
1882      /* Use core Mesa fallback */
1883      return _mesa_test_proxy_teximage(ctx, target, level, format,
1884                                       width, height, depth, border);
1885   }
1886}
1887
1888static GLboolean
1889st_TextureView(struct gl_context *ctx,
1890               struct gl_texture_object *texObj,
1891               struct gl_texture_object *origTexObj)
1892{
1893   struct st_texture_object *orig = st_texture_object(origTexObj);
1894   struct st_texture_object *tex = st_texture_object(texObj);
1895   struct gl_texture_image *image = texObj->Image[0][0];
1896
1897   const int numFaces = _mesa_num_tex_faces(texObj->Target);
1898   const int numLevels = texObj->NumLevels;
1899
1900   int face;
1901   int level;
1902
1903   pipe_resource_reference(&tex->pt, orig->pt);
1904
1905   /* Set image resource pointers */
1906   for (level = 0; level < numLevels; level++) {
1907      for (face = 0; face < numFaces; face++) {
1908         struct st_texture_image *stImage =
1909            st_texture_image(texObj->Image[face][level]);
1910         pipe_resource_reference(&stImage->pt, tex->pt);
1911      }
1912   }
1913
1914   tex->surface_based = GL_TRUE;
1915   tex->surface_format =
1916      st_mesa_format_to_pipe_format(st_context(ctx), image->TexFormat);
1917
1918   tex->width0 = image->Width;
1919   tex->height0 = image->Height;
1920   tex->depth0 = image->Depth;
1921   tex->lastLevel = numLevels - 1;
1922
1923   return GL_TRUE;
1924}
1925
1926static void
1927st_ClearTexSubImage(struct gl_context *ctx,
1928                    struct gl_texture_image *texImage,
1929                    GLint xoffset, GLint yoffset, GLint zoffset,
1930                    GLsizei width, GLsizei height, GLsizei depth,
1931                    const GLvoid *clearValue)
1932{
1933   static const char zeros[16] = {0};
1934   struct st_texture_image *stImage = st_texture_image(texImage);
1935   struct pipe_resource *pt = stImage->pt;
1936   struct st_context *st = st_context(ctx);
1937   struct pipe_context *pipe = st->pipe;
1938   unsigned level = texImage->Level;
1939   struct pipe_box box;
1940
1941   if (!pt)
1942      return;
1943
1944   u_box_3d(xoffset, yoffset, zoffset + texImage->Face,
1945            width, height, depth, &box);
1946   if (texImage->TexObject->Immutable) {
1947      level += texImage->TexObject->MinLevel;
1948      box.z += texImage->TexObject->MinLayer;
1949   }
1950
1951   pipe->clear_texture(pipe, pt, level, &box, clearValue ? clearValue : zeros);
1952}
1953
1954void
1955st_init_texture_functions(struct dd_function_table *functions)
1956{
1957   functions->ChooseTextureFormat = st_ChooseTextureFormat;
1958   functions->QuerySamplesForFormat = st_QuerySamplesForFormat;
1959   functions->TexImage = st_TexImage;
1960   functions->TexSubImage = st_TexSubImage;
1961   functions->CompressedTexSubImage = _mesa_store_compressed_texsubimage;
1962   functions->CopyTexSubImage = st_CopyTexSubImage;
1963   functions->GenerateMipmap = st_generate_mipmap;
1964
1965   functions->GetTexSubImage = st_GetTexSubImage;
1966
1967   /* compressed texture functions */
1968   functions->CompressedTexImage = st_CompressedTexImage;
1969   functions->GetCompressedTexSubImage = _mesa_GetCompressedTexSubImage_sw;
1970
1971   functions->NewTextureObject = st_NewTextureObject;
1972   functions->NewTextureImage = st_NewTextureImage;
1973   functions->DeleteTextureImage = st_DeleteTextureImage;
1974   functions->DeleteTexture = st_DeleteTextureObject;
1975   functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
1976   functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
1977   functions->MapTextureImage = st_MapTextureImage;
1978   functions->UnmapTextureImage = st_UnmapTextureImage;
1979
1980   /* XXX Temporary until we can query pipe's texture sizes */
1981   functions->TestProxyTexImage = st_TestProxyTexImage;
1982
1983   functions->AllocTextureStorage = st_AllocTextureStorage;
1984   functions->TextureView = st_TextureView;
1985   functions->ClearTexSubImage = st_ClearTexSubImage;
1986}
1987