st_texture.c revision b43c182f19c6291c88420fa12714f952c2b461fb
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 "st_context.h"
29#include "st_format.h"
30#include "st_public.h"
31#include "st_texture.h"
32#include "st_cb_fbo.h"
33#include "st_inlines.h"
34#include "main/enums.h"
35#include "main/texfetch.h"
36#include "main/teximage.h"
37#include "main/texobj.h"
38
39#undef Elements  /* fix re-defined macro warning */
40
41#include "pipe/p_state.h"
42#include "pipe/p_context.h"
43#include "pipe/p_defines.h"
44#include "util/u_inlines.h"
45#include "util/u_format.h"
46#include "util/u_rect.h"
47#include "util/u_math.h"
48
49
50#define DBG if(0) printf
51
52#if 0
53static GLenum
54target_to_target(GLenum target)
55{
56   switch (target) {
57   case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
58   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
59   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
60   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
61   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
62   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
63      return GL_TEXTURE_CUBE_MAP_ARB;
64   default:
65      return target;
66   }
67}
68#endif
69
70
71/**
72 * Allocate a new pipe_texture object
73 * width0, height0, depth0 are the dimensions of the level 0 image
74 * (the highest resolution).  last_level indicates how many mipmap levels
75 * to allocate storage for.  For non-mipmapped textures, this will be zero.
76 */
77struct pipe_texture *
78st_texture_create(struct st_context *st,
79                  enum pipe_texture_target target,
80		  enum pipe_format format,
81		  GLuint last_level,
82		  GLuint width0,
83		  GLuint height0,
84		  GLuint depth0,
85                  GLuint usage )
86{
87   struct pipe_texture pt, *newtex;
88   struct pipe_screen *screen = st->pipe->screen;
89
90   assert(target <= PIPE_TEXTURE_CUBE);
91
92   DBG("%s target %s format %s last_level %d\n", __FUNCTION__,
93       _mesa_lookup_enum_by_nr(target),
94       _mesa_lookup_enum_by_nr(format), last_level);
95
96   assert(format);
97   assert(screen->is_format_supported(screen, format, target,
98                                      PIPE_TEXTURE_USAGE_SAMPLER, 0));
99
100   memset(&pt, 0, sizeof(pt));
101   pt.target = target;
102   pt.format = format;
103   pt.last_level = last_level;
104   pt.width0 = width0;
105   pt.height0 = height0;
106   pt.depth0 = depth0;
107   pt.tex_usage = usage;
108
109   newtex = screen->texture_create(screen, &pt);
110
111   assert(!newtex || pipe_is_referenced(&newtex->reference));
112
113   return newtex;
114}
115
116
117/**
118 * Check if a texture image can be pulled into a unified mipmap texture.
119 */
120GLboolean
121st_texture_match_image(const struct pipe_texture *pt,
122                       const struct gl_texture_image *image,
123                       GLuint face, GLuint level)
124{
125   /* Images with borders are never pulled into mipmap textures.
126    */
127   if (image->Border)
128      return GL_FALSE;
129
130   /* Check if this image's format matches the established texture's format.
131    */
132   if (st_mesa_format_to_pipe_format(image->TexFormat) != pt->format)
133      return GL_FALSE;
134
135   /* Test if this image's size matches what's expected in the
136    * established texture.
137    */
138   if (image->Width != u_minify(pt->width0, level) ||
139       image->Height != u_minify(pt->height0, level) ||
140       image->Depth != u_minify(pt->depth0, level))
141      return GL_FALSE;
142
143   return GL_TRUE;
144}
145
146
147#if 000
148/* Although we use the image_offset[] array to store relative offsets
149 * to cube faces, Mesa doesn't know anything about this and expects
150 * each cube face to be treated as a separate image.
151 *
152 * These functions present that view to mesa:
153 */
154const GLuint *
155st_texture_depth_offsets(struct pipe_texture *pt, GLuint level)
156{
157   static const GLuint zero = 0;
158
159   if (pt->target != PIPE_TEXTURE_3D || pt->level[level].nr_images == 1)
160      return &zero;
161   else
162      return pt->level[level].image_offset;
163}
164
165
166/**
167 * Return the offset to the given mipmap texture image within the
168 * texture memory buffer, in bytes.
169 */
170GLuint
171st_texture_image_offset(const struct pipe_texture * pt,
172                        GLuint face, GLuint level)
173{
174   if (pt->target == PIPE_TEXTURE_CUBE)
175      return (pt->level[level].level_offset +
176              pt->level[level].image_offset[face] * pt->cpp);
177   else
178      return pt->level[level].level_offset;
179}
180#endif
181
182
183/**
184 * Map a teximage in a mipmap texture.
185 * \param row_stride  returns row stride in bytes
186 * \param image_stride  returns image stride in bytes (for 3D textures).
187 * \return address of mapping
188 */
189GLubyte *
190st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
191		     GLuint zoffset, enum pipe_transfer_usage usage,
192                     GLuint x, GLuint y, GLuint w, GLuint h)
193{
194   struct pipe_context *pipe = st->pipe;
195   struct pipe_texture *pt = stImage->pt;
196
197   DBG("%s \n", __FUNCTION__);
198
199   stImage->transfer = st_no_flush_get_tex_transfer(st, pt, stImage->face,
200						    stImage->level, zoffset,
201						    usage, x, y, w, h);
202
203   if (stImage->transfer)
204      return pipe->transfer_map(pipe, stImage->transfer);
205   else
206      return NULL;
207}
208
209
210void
211st_texture_image_unmap(struct st_context *st,
212                       struct st_texture_image *stImage)
213{
214   struct pipe_context *pipe = st->pipe;
215
216   DBG("%s\n", __FUNCTION__);
217
218   pipe->transfer_unmap(pipe, stImage->transfer);
219
220   pipe->tex_transfer_destroy(stImage->transfer);
221}
222
223
224
225/**
226 * Upload data to a rectangular sub-region.  Lots of choices how to do this:
227 *
228 * - memcpy by span to current destination
229 * - upload data as new buffer and blit
230 *
231 * Currently always memcpy.
232 */
233static void
234st_surface_data(struct pipe_context *pipe,
235		struct pipe_transfer *dst,
236		unsigned dstx, unsigned dsty,
237		const void *src, unsigned src_stride,
238		unsigned srcx, unsigned srcy, unsigned width, unsigned height)
239{
240   void *map = pipe->transfer_map(pipe, dst);
241
242   assert(dst->texture);
243   util_copy_rect(map,
244                  dst->texture->format,
245                  dst->stride,
246                  dstx, dsty,
247                  width, height,
248                  src, src_stride,
249                  srcx, srcy);
250
251   pipe->transfer_unmap(pipe, dst);
252}
253
254
255/* Upload data for a particular image.
256 */
257void
258st_texture_image_data(struct st_context *st,
259                      struct pipe_texture *dst,
260                      GLuint face,
261                      GLuint level,
262                      void *src,
263                      GLuint src_row_stride, GLuint src_image_stride)
264{
265   struct pipe_context *pipe = st->pipe;
266   GLuint depth = u_minify(dst->depth0, level);
267   GLuint i;
268   const GLubyte *srcUB = src;
269   struct pipe_transfer *dst_transfer;
270
271   DBG("%s\n", __FUNCTION__);
272
273   for (i = 0; i < depth; i++) {
274      dst_transfer = st_no_flush_get_tex_transfer(st, dst, face, level, i,
275						  PIPE_TRANSFER_WRITE, 0, 0,
276						  u_minify(dst->width0, level),
277                                                  u_minify(dst->height0, level));
278
279      st_surface_data(pipe, dst_transfer,
280		      0, 0,                             /* dstx, dsty */
281		      srcUB,
282		      src_row_stride,
283		      0, 0,                             /* source x, y */
284		      u_minify(dst->width0, level),
285                      u_minify(dst->height0, level));      /* width, height */
286
287      pipe->tex_transfer_destroy(dst_transfer);
288
289      srcUB += src_image_stride;
290   }
291}
292
293
294/* Copy mipmap image between textures
295 */
296void
297st_texture_image_copy(struct pipe_context *pipe,
298                      struct pipe_texture *dst, GLuint dstLevel,
299                      struct pipe_texture *src,
300                      GLuint face)
301{
302   struct pipe_screen *screen = pipe->screen;
303   GLuint width = u_minify(dst->width0, dstLevel);
304   GLuint height = u_minify(dst->height0, dstLevel);
305   GLuint depth = u_minify(dst->depth0, dstLevel);
306   struct pipe_surface *src_surface;
307   struct pipe_surface *dst_surface;
308   GLuint i;
309
310   for (i = 0; i < depth; i++) {
311      GLuint srcLevel;
312
313      /* find src texture level of needed size */
314      for (srcLevel = 0; srcLevel <= src->last_level; srcLevel++) {
315         if (u_minify(src->width0, srcLevel) == width &&
316             u_minify(src->height0, srcLevel) == height) {
317            break;
318         }
319      }
320      assert(u_minify(src->width0, srcLevel) == width);
321      assert(u_minify(src->height0, srcLevel) == height);
322
323#if 0
324      {
325         src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
326                                               PIPE_BUFFER_USAGE_CPU_READ);
327         ubyte *map = screen->surface_map(screen, src_surface, PIPE_BUFFER_USAGE_CPU_READ);
328         map += src_surface->width * src_surface->height * 4 / 2;
329         printf("%s center pixel: %d %d %d %d (pt %p[%d] -> %p[%d])\n",
330                __FUNCTION__,
331                map[0], map[1], map[2], map[3],
332                src, srcLevel, dst, dstLevel);
333
334         screen->surface_unmap(screen, src_surface);
335         pipe_surface_reference(&src_surface, NULL);
336      }
337#endif
338
339      dst_surface = screen->get_tex_surface(screen, dst, face, dstLevel, i,
340                                            PIPE_BUFFER_USAGE_GPU_WRITE);
341
342      src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
343                                            PIPE_BUFFER_USAGE_GPU_READ);
344
345      if (pipe->surface_copy) {
346         pipe->surface_copy(pipe,
347			    dst_surface,
348			    0, 0, /* destX, Y */
349			    src_surface,
350			    0, 0, /* srcX, Y */
351			    width, height);
352      } else {
353         util_surface_copy(pipe, FALSE,
354			   dst_surface,
355			   0, 0, /* destX, Y */
356			   src_surface,
357			   0, 0, /* srcX, Y */
358			   width, height);
359      }
360
361      pipe_surface_reference(&src_surface, NULL);
362      pipe_surface_reference(&dst_surface, NULL);
363   }
364}
365
366
367/**
368 * Bind a pipe surface to a texture object.  After the call,
369 * the texture object is marked dirty and will be (re-)validated.
370 *
371 * If this is the first surface bound, the texture object is said to
372 * switch from normal to surface based.  It will be cleared first in
373 * this case.
374 *
375 * \param ps      pipe surface to be unbound
376 * \param target  texture target
377 * \param level   image level
378 * \param format  internal format of the texture
379 */
380int
381st_bind_texture_surface(struct pipe_surface *ps, int target, int level,
382                        enum pipe_format format)
383{
384   GET_CURRENT_CONTEXT(ctx);
385   const GLuint unit = ctx->Texture.CurrentUnit;
386   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
387   struct gl_texture_object *texObj;
388   struct gl_texture_image *texImage;
389   struct st_texture_object *stObj;
390   struct st_texture_image *stImage;
391   GLenum internalFormat;
392
393   switch (target) {
394   case ST_TEXTURE_2D:
395      target = GL_TEXTURE_2D;
396      break;
397   case ST_TEXTURE_RECT:
398      target = GL_TEXTURE_RECTANGLE_ARB;
399      break;
400   default:
401      return 0;
402   }
403
404   /* map pipe format to base format for now */
405   if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 3) > 0)
406      internalFormat = GL_RGBA;
407   else
408      internalFormat = GL_RGB;
409
410   texObj = _mesa_select_tex_object(ctx, texUnit, target);
411   _mesa_lock_texture(ctx, texObj);
412
413   stObj = st_texture_object(texObj);
414   /* switch to surface based */
415   if (!stObj->surface_based) {
416      _mesa_clear_texture_object(ctx, texObj);
417      stObj->surface_based = GL_TRUE;
418   }
419
420   texImage = _mesa_get_tex_image(ctx, texObj, target, level);
421   stImage = st_texture_image(texImage);
422
423   _mesa_init_teximage_fields(ctx, target, texImage,
424                              ps->width, ps->height, 1, 0, internalFormat);
425   texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat,
426                                                GL_RGBA, GL_UNSIGNED_BYTE);
427   _mesa_set_fetch_functions(texImage, 2);
428   pipe_texture_reference(&stImage->pt, ps->texture);
429
430   _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
431   _mesa_unlock_texture(ctx, texObj);
432
433   return 1;
434}
435
436
437/**
438 * Unbind a pipe surface from a texture object.  After the call,
439 * the texture object is marked dirty and will be (re-)validated.
440 *
441 * \param ps      pipe surface to be unbound
442 * \param target  texture target
443 * \param level   image level
444 */
445int
446st_unbind_texture_surface(struct pipe_surface *ps, int target, int level)
447{
448   GET_CURRENT_CONTEXT(ctx);
449   const GLuint unit = ctx->Texture.CurrentUnit;
450   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
451   struct gl_texture_object *texObj;
452   struct gl_texture_image *texImage;
453   struct st_texture_object *stObj;
454   struct st_texture_image *stImage;
455
456   switch (target) {
457   case ST_TEXTURE_2D:
458      target = GL_TEXTURE_2D;
459      break;
460   case ST_TEXTURE_RECT:
461      target = GL_TEXTURE_RECTANGLE_ARB;
462      break;
463   default:
464      return 0;
465   }
466
467   texObj = _mesa_select_tex_object(ctx, texUnit, target);
468
469   _mesa_lock_texture(ctx, texObj);
470
471   texImage = _mesa_get_tex_image(ctx, texObj, target, level);
472   stObj = st_texture_object(texObj);
473   stImage = st_texture_image(texImage);
474
475   /* Make sure the pipe surface is still bound.  The texture object is still
476    * considered surface based even if this is the last bound surface. */
477   if (stImage->pt == ps->texture) {
478      pipe_texture_reference(&stImage->pt, NULL);
479      _mesa_clear_texture_image(ctx, texImage);
480
481      _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
482   }
483
484   _mesa_unlock_texture(ctx, texObj);
485
486   return 1;
487}
488
489
490/** Redirect rendering into stfb's surface to a texture image */
491int
492st_bind_teximage(struct st_framebuffer *stfb, uint surfIndex,
493                 int target, int format, int level)
494{
495   GET_CURRENT_CONTEXT(ctx);
496   struct st_context *st = ctx->st;
497   struct pipe_context *pipe = st->pipe;
498   struct pipe_screen *screen = pipe->screen;
499   const GLuint unit = ctx->Texture.CurrentUnit;
500   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
501   struct gl_texture_object *texObj;
502   struct gl_texture_image *texImage;
503   struct st_texture_image *stImage;
504   struct st_renderbuffer *strb;
505   GLint face = 0, slice = 0;
506
507   assert(surfIndex <= ST_SURFACE_DEPTH);
508
509   strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
510
511   if (strb->texture_save || strb->surface_save) {
512      /* Error! */
513      return 0;
514   }
515
516   if (target == ST_TEXTURE_2D) {
517      texObj = texUnit->CurrentTex[TEXTURE_2D_INDEX];
518      texImage = _mesa_get_tex_image(ctx, texObj, GL_TEXTURE_2D, level);
519      stImage = st_texture_image(texImage);
520   }
521   else {
522      /* unsupported target */
523      return 0;
524   }
525
526   st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
527
528   /* save the renderbuffer's surface/texture info */
529   pipe_texture_reference(&strb->texture_save, strb->texture);
530   pipe_surface_reference(&strb->surface_save, strb->surface);
531
532   /* plug in new surface/texture info */
533   pipe_texture_reference(&strb->texture, stImage->pt);
534   strb->surface = screen->get_tex_surface(screen, strb->texture,
535                                           face, level, slice,
536                                           (PIPE_BUFFER_USAGE_GPU_READ |
537                                            PIPE_BUFFER_USAGE_GPU_WRITE));
538
539   st->dirty.st |= ST_NEW_FRAMEBUFFER;
540
541   return 1;
542}
543
544
545/** Undo surface-to-texture binding */
546int
547st_release_teximage(struct st_framebuffer *stfb, uint surfIndex,
548                    int target, int format, int level)
549{
550   GET_CURRENT_CONTEXT(ctx);
551   struct st_context *st = ctx->st;
552   struct st_renderbuffer *strb;
553
554   assert(surfIndex <= ST_SURFACE_DEPTH);
555
556   strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
557
558   if (!strb->texture_save || !strb->surface_save) {
559      /* Error! */
560      return 0;
561   }
562
563   st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
564
565   /* free tex surface, restore original */
566   pipe_surface_reference(&strb->surface, strb->surface_save);
567   pipe_texture_reference(&strb->texture, strb->texture_save);
568
569   pipe_surface_reference(&strb->surface_save, NULL);
570   pipe_texture_reference(&strb->texture_save, NULL);
571
572   st->dirty.st |= ST_NEW_FRAMEBUFFER;
573
574   return 1;
575}
576
577void
578st_teximage_flush_before_map(struct st_context *st,
579			     struct pipe_texture *pt,
580			     unsigned int face,
581			     unsigned int level,
582			     enum pipe_transfer_usage usage)
583{
584   struct pipe_context *pipe = st->pipe;
585   unsigned referenced =
586      pipe->is_texture_referenced(pipe, pt, face, level);
587
588   if (referenced && ((referenced & PIPE_REFERENCED_FOR_WRITE) ||
589		      (usage & PIPE_TRANSFER_WRITE)))
590      st->pipe->flush(st->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
591}
592