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