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