Merge remote branch 'origin/master' into pipe-video
[mesa.git] / src / mesa / state_tracker / st_cb_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 "main/mfeatures.h"
29 #include "main/bufferobj.h"
30 #include "main/enums.h"
31 #include "main/fbobject.h"
32 #include "main/formats.h"
33 #include "main/image.h"
34 #include "main/imports.h"
35 #include "main/macros.h"
36 #include "main/mipmap.h"
37 #include "main/pack.h"
38 #include "main/pbo.h"
39 #include "main/pixeltransfer.h"
40 #include "main/texcompress.h"
41 #include "main/texfetch.h"
42 #include "main/texgetimage.h"
43 #include "main/teximage.h"
44 #include "main/texobj.h"
45 #include "main/texstore.h"
46
47 #include "state_tracker/st_debug.h"
48 #include "state_tracker/st_context.h"
49 #include "state_tracker/st_cb_fbo.h"
50 #include "state_tracker/st_cb_flush.h"
51 #include "state_tracker/st_cb_texture.h"
52 #include "state_tracker/st_format.h"
53 #include "state_tracker/st_texture.h"
54 #include "state_tracker/st_gen_mipmap.h"
55 #include "state_tracker/st_atom.h"
56
57 #include "pipe/p_context.h"
58 #include "pipe/p_defines.h"
59 #include "util/u_inlines.h"
60 #include "pipe/p_shader_tokens.h"
61 #include "util/u_tile.h"
62 #include "util/u_blit.h"
63 #include "util/u_format.h"
64 #include "util/u_surface.h"
65 #include "util/u_sampler.h"
66 #include "util/u_math.h"
67 #include "util/u_box.h"
68
69 #define DBG if (0) printf
70
71
72 static enum pipe_texture_target
73 gl_target_to_pipe(GLenum target)
74 {
75 switch (target) {
76 case GL_TEXTURE_1D:
77 return PIPE_TEXTURE_1D;
78 case GL_TEXTURE_2D:
79 return PIPE_TEXTURE_2D;
80 case GL_TEXTURE_RECTANGLE_NV:
81 return PIPE_TEXTURE_RECT;
82 case GL_TEXTURE_3D:
83 return PIPE_TEXTURE_3D;
84 case GL_TEXTURE_CUBE_MAP_ARB:
85 return PIPE_TEXTURE_CUBE;
86 case GL_TEXTURE_1D_ARRAY_EXT:
87 return PIPE_TEXTURE_1D_ARRAY;
88 case GL_TEXTURE_2D_ARRAY_EXT:
89 return PIPE_TEXTURE_2D_ARRAY;
90 default:
91 assert(0);
92 return 0;
93 }
94 }
95
96
97 /** called via ctx->Driver.NewTextureImage() */
98 static struct gl_texture_image *
99 st_NewTextureImage(struct gl_context * ctx)
100 {
101 DBG("%s\n", __FUNCTION__);
102 (void) ctx;
103 return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
104 }
105
106
107 /** called via ctx->Driver.NewTextureObject() */
108 static struct gl_texture_object *
109 st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
110 {
111 struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
112
113 DBG("%s\n", __FUNCTION__);
114 _mesa_initialize_texture_object(&obj->base, name, target);
115
116 return &obj->base;
117 }
118
119 /** called via ctx->Driver.DeleteTextureObject() */
120 static void
121 st_DeleteTextureObject(struct gl_context *ctx,
122 struct gl_texture_object *texObj)
123 {
124 struct st_context *st = st_context(ctx);
125 struct st_texture_object *stObj = st_texture_object(texObj);
126 if (stObj->pt)
127 pipe_resource_reference(&stObj->pt, NULL);
128 if (stObj->sampler_view) {
129 if (stObj->sampler_view->context != st->pipe) {
130 /* Take "ownership" of this texture sampler view by setting
131 * its context pointer to this context. This avoids potential
132 * crashes when the texture object is shared among contexts
133 * and the original/owner context has already been destroyed.
134 */
135 stObj->sampler_view->context = st->pipe;
136 }
137 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
138 }
139 _mesa_delete_texture_object(ctx, texObj);
140 }
141
142
143 /** called via ctx->Driver.FreeTexImageData() */
144 static void
145 st_FreeTextureImageData(struct gl_context * ctx, struct gl_texture_image *texImage)
146 {
147 struct st_texture_image *stImage = st_texture_image(texImage);
148
149 DBG("%s\n", __FUNCTION__);
150
151 if (stImage->pt) {
152 pipe_resource_reference(&stImage->pt, NULL);
153 }
154
155 if (texImage->Data) {
156 _mesa_align_free(texImage->Data);
157 texImage->Data = NULL;
158 }
159 }
160
161
162 /**
163 * From linux kernel i386 header files, copes with odd sizes better
164 * than COPY_DWORDS would:
165 * XXX Put this in src/mesa/main/imports.h ???
166 */
167 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
168 static INLINE void *
169 __memcpy(void *to, const void *from, size_t n)
170 {
171 int d0, d1, d2;
172 __asm__ __volatile__("rep ; movsl\n\t"
173 "testb $2,%b4\n\t"
174 "je 1f\n\t"
175 "movsw\n"
176 "1:\ttestb $1,%b4\n\t"
177 "je 2f\n\t"
178 "movsb\n" "2:":"=&c"(d0), "=&D"(d1), "=&S"(d2)
179 :"0"(n / 4), "q"(n), "1"((long) to), "2"((long) from)
180 :"memory");
181 return (to);
182 }
183 #else
184 #define __memcpy(a,b,c) memcpy(a,b,c)
185 #endif
186
187
188 /**
189 * The system memcpy (at least on ubuntu 5.10) has problems copying
190 * to agp (writecombined) memory from a source which isn't 64-byte
191 * aligned - there is a 4x performance falloff.
192 *
193 * The x86 __memcpy is immune to this but is slightly slower
194 * (10%-ish) than the system memcpy.
195 *
196 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
197 * isn't much faster than x86_memcpy for agp copies.
198 *
199 * TODO: switch dynamically.
200 */
201 static void *
202 do_memcpy(void *dest, const void *src, size_t n)
203 {
204 if ((((unsigned long) src) & 63) || (((unsigned long) dest) & 63)) {
205 return __memcpy(dest, src, n);
206 }
207 else
208 return memcpy(dest, src, n);
209 }
210
211
212 /**
213 * Return default texture resource binding bitmask for the given format.
214 */
215 static GLuint
216 default_bindings(struct st_context *st, enum pipe_format format)
217 {
218 struct pipe_screen *screen = st->pipe->screen;
219 const unsigned target = PIPE_TEXTURE_2D;
220 const unsigned geom = 0x0;
221 unsigned bindings;
222
223 if (util_format_is_depth_or_stencil(format))
224 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
225 else
226 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
227
228 if (screen->is_format_supported(screen, format, target, 0, bindings, geom))
229 return bindings;
230 else {
231 /* Try non-sRGB. */
232 format = util_format_linear(format);
233
234 if (screen->is_format_supported(screen, format, target, 0, bindings, geom))
235 return bindings;
236 else
237 return PIPE_BIND_SAMPLER_VIEW;
238 }
239 }
240
241
242 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
243 static GLuint
244 get_texture_dims(GLenum target)
245 {
246 switch (target) {
247 case GL_TEXTURE_1D:
248 case GL_TEXTURE_1D_ARRAY_EXT:
249 return 1;
250 case GL_TEXTURE_2D:
251 case GL_TEXTURE_CUBE_MAP_ARB:
252 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
253 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
254 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
255 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
256 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
257 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
258 case GL_TEXTURE_RECTANGLE_NV:
259 case GL_TEXTURE_2D_ARRAY_EXT:
260 return 2;
261 case GL_TEXTURE_3D:
262 return 3;
263 default:
264 assert(0 && "invalid texture target in get_texture_dims()");
265 return 1;
266 }
267 }
268
269
270 /**
271 * Given the size of a mipmap image, try to compute the size of the level=0
272 * mipmap image.
273 *
274 * Note that this isn't always accurate for odd-sized, non-POW textures.
275 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
276 *
277 * \return GL_TRUE for success, GL_FALSE for failure
278 */
279 static GLboolean
280 guess_base_level_size(GLenum target,
281 GLuint width, GLuint height, GLuint depth, GLuint level,
282 GLuint *width0, GLuint *height0, GLuint *depth0)
283 {
284 const GLuint dims = get_texture_dims(target);
285
286 assert(width >= 1);
287 assert(height >= 1);
288 assert(depth >= 1);
289
290 if (level > 0) {
291 /* Depending on the image's size, we can't always make a guess here */
292 if ((dims >= 1 && width == 1) ||
293 (dims >= 2 && height == 1) ||
294 (dims >= 3 && depth == 1)) {
295 /* we can't determine the image size at level=0 */
296 return GL_FALSE;
297 }
298
299 /* grow the image size until we hit level = 0 */
300 while (level > 0) {
301 if (width > 1)
302 width <<= 1;
303 if (height > 1)
304 height <<= 1;
305 if (depth > 1)
306 depth <<= 1;
307 level--;
308 }
309 }
310
311 *width0 = width;
312 *height0 = height;
313 *depth0 = depth;
314
315 return GL_TRUE;
316 }
317
318
319 /**
320 * Try to allocate a pipe_resource object for the given st_texture_object.
321 *
322 * We use the given st_texture_image as a clue to determine the size of the
323 * mipmap image at level=0.
324 *
325 * \return GL_TRUE for success, GL_FALSE if out of memory.
326 */
327 static GLboolean
328 guess_and_alloc_texture(struct st_context *st,
329 struct st_texture_object *stObj,
330 const struct st_texture_image *stImage)
331 {
332 GLuint lastLevel, width, height, depth;
333 GLuint bindings;
334 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
335 enum pipe_format fmt;
336
337 DBG("%s\n", __FUNCTION__);
338
339 assert(!stObj->pt);
340
341 if (!guess_base_level_size(stObj->base.Target,
342 stImage->base.Width2,
343 stImage->base.Height2,
344 stImage->base.Depth2,
345 stImage->level,
346 &width, &height, &depth)) {
347 /* we can't determine the image size at level=0 */
348 stObj->width0 = stObj->height0 = stObj->depth0 = 0;
349 /* this is not an out of memory error */
350 return GL_TRUE;
351 }
352
353 /* At this point, (width x height x depth) is the expected size of
354 * the level=0 mipmap image.
355 */
356
357 /* Guess a reasonable value for lastLevel. With OpenGL we have no
358 * idea how many mipmap levels will be in a texture until we start
359 * to render with it. Make an educated guess here but be prepared
360 * to re-allocating a texture buffer with space for more (or fewer)
361 * mipmap levels later.
362 */
363 if ((stObj->base.MinFilter == GL_NEAREST ||
364 stObj->base.MinFilter == GL_LINEAR ||
365 stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
366 stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) &&
367 !stObj->base.GenerateMipmap &&
368 stImage->level == 0) {
369 /* only alloc space for a single mipmap level */
370 lastLevel = 0;
371 }
372 else {
373 /* alloc space for a full mipmap */
374 GLuint l2width = util_logbase2(width);
375 GLuint l2height = util_logbase2(height);
376 GLuint l2depth = util_logbase2(depth);
377 lastLevel = MAX2(MAX2(l2width, l2height), l2depth);
378 }
379
380 /* Save the level=0 dimensions */
381 stObj->width0 = width;
382 stObj->height0 = height;
383 stObj->depth0 = depth;
384
385 fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat);
386
387 bindings = default_bindings(st, fmt);
388
389 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
390 width, height, depth,
391 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
392
393 stObj->pt = st_texture_create(st,
394 gl_target_to_pipe(stObj->base.Target),
395 fmt,
396 lastLevel,
397 ptWidth,
398 ptHeight,
399 ptDepth,
400 ptLayers,
401 bindings);
402
403 DBG("%s returning %d\n", __FUNCTION__, (stObj->pt != NULL));
404
405 return stObj->pt != NULL;
406 }
407
408
409 /**
410 * Adjust pixel unpack params and image dimensions to strip off the
411 * texture border.
412 * Gallium doesn't support texture borders. They've seldem been used
413 * and seldom been implemented correctly anyway.
414 * \param unpackNew returns the new pixel unpack parameters
415 */
416 static void
417 strip_texture_border(GLint border,
418 GLint *width, GLint *height, GLint *depth,
419 const struct gl_pixelstore_attrib *unpack,
420 struct gl_pixelstore_attrib *unpackNew)
421 {
422 assert(border > 0); /* sanity check */
423
424 *unpackNew = *unpack;
425
426 if (unpackNew->RowLength == 0)
427 unpackNew->RowLength = *width;
428
429 if (depth && unpackNew->ImageHeight == 0)
430 unpackNew->ImageHeight = *height;
431
432 unpackNew->SkipPixels += border;
433 if (height)
434 unpackNew->SkipRows += border;
435 if (depth)
436 unpackNew->SkipImages += border;
437
438 assert(*width >= 3);
439 *width = *width - 2 * border;
440 if (height && *height >= 3)
441 *height = *height - 2 * border;
442 if (depth && *depth >= 3)
443 *depth = *depth - 2 * border;
444 }
445
446
447 /**
448 * Do glTexImage1/2/3D().
449 */
450 static void
451 st_TexImage(struct gl_context * ctx,
452 GLint dims,
453 GLenum target, GLint level,
454 GLint internalFormat,
455 GLint width, GLint height, GLint depth,
456 GLint border,
457 GLenum format, GLenum type, const void *pixels,
458 const struct gl_pixelstore_attrib *unpack,
459 struct gl_texture_object *texObj,
460 struct gl_texture_image *texImage,
461 GLsizei imageSize, GLboolean compressed_src)
462 {
463 struct st_context *st = st_context(ctx);
464 struct st_texture_object *stObj = st_texture_object(texObj);
465 struct st_texture_image *stImage = st_texture_image(texImage);
466 GLuint dstRowStride = 0;
467 struct gl_pixelstore_attrib unpackNB;
468 enum pipe_transfer_usage transfer_usage = 0;
469
470 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
471 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
472
473 /* switch to "normal" */
474 if (stObj->surface_based) {
475 gl_format texFormat;
476
477 _mesa_clear_texture_object(ctx, texObj);
478 pipe_resource_reference(&stObj->pt, NULL);
479
480 /* oops, need to init this image again */
481 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
482 internalFormat, format, type);
483
484 _mesa_init_teximage_fields(ctx, target, texImage,
485 width, height, depth, border,
486 internalFormat, texFormat);
487
488 stObj->surface_based = GL_FALSE;
489 }
490
491 /* gallium does not support texture borders, strip it off */
492 if (border) {
493 strip_texture_border(border, &width, &height, &depth, unpack, &unpackNB);
494 unpack = &unpackNB;
495 texImage->Width = width;
496 texImage->Height = height;
497 texImage->Depth = depth;
498 texImage->Border = 0;
499 border = 0;
500 }
501 else {
502 assert(texImage->Width == width);
503 assert(texImage->Height == height);
504 assert(texImage->Depth == depth);
505 }
506
507 stImage->face = _mesa_tex_target_to_face(target);
508 stImage->level = level;
509
510 _mesa_set_fetch_functions(texImage, dims);
511
512 /* Release the reference to a potentially orphaned buffer.
513 * Release any old malloced memory.
514 */
515 if (stImage->pt) {
516 pipe_resource_reference(&stImage->pt, NULL);
517 assert(!texImage->Data);
518 }
519 else if (texImage->Data) {
520 _mesa_align_free(texImage->Data);
521 }
522
523 /*
524 * See if the new image is somehow incompatible with the existing
525 * mipmap. If so, free the old mipmap.
526 */
527 if (stObj->pt) {
528 if (level > (GLint) stObj->pt->last_level ||
529 !st_texture_match_image(stObj->pt, &stImage->base,
530 stImage->face, stImage->level)) {
531 DBG("release it\n");
532 pipe_resource_reference(&stObj->pt, NULL);
533 assert(!stObj->pt);
534 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
535 }
536 }
537
538 if (width == 0 || height == 0 || depth == 0) {
539 /* stop after freeing old image */
540 return;
541 }
542
543 if (!stObj->pt) {
544 if (!guess_and_alloc_texture(st, stObj, stImage)) {
545 /* Probably out of memory.
546 * Try flushing any pending rendering, then retry.
547 */
548 st_finish(st);
549 if (!guess_and_alloc_texture(st, stObj, stImage)) {
550 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
551 return;
552 }
553 }
554 }
555
556 assert(!stImage->pt);
557
558 /* Check if this texture image can live inside the texture object's buffer.
559 * If so, store the image there. Otherwise the image will temporarily live
560 * in its own buffer.
561 */
562 if (stObj->pt &&
563 st_texture_match_image(stObj->pt, &stImage->base,
564 stImage->face, stImage->level)) {
565
566 pipe_resource_reference(&stImage->pt, stObj->pt);
567 assert(stImage->pt);
568 }
569
570 if (!stImage->pt)
571 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
572
573 /* Pixel data may come from regular user memory or a PBO. For the later,
574 * do bounds checking and map the PBO to read pixels data from it.
575 *
576 * XXX we should try to use a GPU-accelerated path to copy the image data
577 * from the PBO to the texture.
578 */
579 if (compressed_src) {
580 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
581 unpack,
582 "glCompressedTexImage");
583 }
584 else {
585 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
586 format, type,
587 pixels, unpack, "glTexImage");
588 }
589
590 /* for a 1D array upload the image as a series of layer with height = 1 */
591 if (target == GL_TEXTURE_1D_ARRAY) {
592 depth = height;
593 height = 1;
594 }
595
596 /*
597 * Prepare to store the texture data. Either map the gallium texture buffer
598 * memory or malloc space for it.
599 */
600 if (stImage->pt) {
601 /* Store the image in the gallium texture memory buffer */
602 if (format == GL_DEPTH_COMPONENT &&
603 util_format_is_depth_and_stencil(stImage->pt->format))
604 transfer_usage = PIPE_TRANSFER_READ_WRITE;
605 else
606 transfer_usage = PIPE_TRANSFER_WRITE;
607
608 texImage->Data = st_texture_image_map(st, stImage, 0,
609 transfer_usage, 0, 0, width, height);
610 if(stImage->transfer)
611 dstRowStride = stImage->transfer->stride;
612 }
613 else {
614 /* Allocate regular memory and store the image there temporarily. */
615 GLuint imageSize = _mesa_format_image_size(texImage->TexFormat,
616 width, height, depth);
617 dstRowStride = _mesa_format_row_stride(texImage->TexFormat, width);
618
619 texImage->Data = _mesa_align_malloc(imageSize, 16);
620 }
621
622 if (!texImage->Data) {
623 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
624 return;
625 }
626
627 if (!pixels) {
628 /* We've allocated texture memory, but have no pixel data - all done. */
629 goto done;
630 }
631
632 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
633 width, height, depth, width, dstRowStride);
634
635 /* Copy user texture image into the texture buffer.
636 */
637 if (compressed_src) {
638 const GLuint srcRowStride =
639 _mesa_format_row_stride(texImage->TexFormat, width);
640 if (dstRowStride == srcRowStride) {
641 memcpy(texImage->Data, pixels, imageSize);
642 }
643 else {
644 char *dst = texImage->Data;
645 const char *src = pixels;
646 GLuint i, bw, bh, lines;
647 _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
648 lines = (height + bh - 1) / bh;
649
650 for (i = 0; i < lines; ++i) {
651 memcpy(dst, src, srcRowStride);
652 dst += dstRowStride;
653 src += srcRowStride;
654 }
655 }
656 }
657 else {
658 const GLuint srcImageStride =
659 _mesa_image_image_stride(unpack, width, height, format, type);
660 GLint i;
661 const GLubyte *src = (const GLubyte *) pixels;
662
663 for (i = 0; i < depth; i++) {
664 if (!_mesa_texstore(ctx, dims,
665 texImage->_BaseFormat,
666 texImage->TexFormat,
667 texImage->Data,
668 0, 0, 0, /* dstX/Y/Zoffset */
669 dstRowStride,
670 texImage->ImageOffsets,
671 width, height, 1,
672 format, type, src, unpack)) {
673 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
674 }
675
676 if (stImage->pt && i + 1 < depth) {
677 /* unmap this slice */
678 st_texture_image_unmap(st, stImage);
679 /* map next slice of 3D texture */
680 texImage->Data = st_texture_image_map(st, stImage, i + 1,
681 transfer_usage, 0, 0,
682 width, height);
683 src += srcImageStride;
684 }
685 }
686 }
687
688 done:
689 _mesa_unmap_teximage_pbo(ctx, unpack);
690
691 if (stImage->pt && texImage->Data) {
692 st_texture_image_unmap(st, stImage);
693 texImage->Data = NULL;
694 }
695 }
696
697
698 static void
699 st_TexImage3D(struct gl_context * ctx,
700 GLenum target, GLint level,
701 GLint internalFormat,
702 GLint width, GLint height, GLint depth,
703 GLint border,
704 GLenum format, GLenum type, const void *pixels,
705 const struct gl_pixelstore_attrib *unpack,
706 struct gl_texture_object *texObj,
707 struct gl_texture_image *texImage)
708 {
709 st_TexImage(ctx, 3, target, level, internalFormat, width, height, depth,
710 border, format, type, pixels, unpack, texObj, texImage,
711 0, GL_FALSE);
712 }
713
714
715 static void
716 st_TexImage2D(struct gl_context * ctx,
717 GLenum target, GLint level,
718 GLint internalFormat,
719 GLint width, GLint height, GLint border,
720 GLenum format, GLenum type, const void *pixels,
721 const struct gl_pixelstore_attrib *unpack,
722 struct gl_texture_object *texObj,
723 struct gl_texture_image *texImage)
724 {
725 st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border,
726 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
727 }
728
729
730 static void
731 st_TexImage1D(struct gl_context * ctx,
732 GLenum target, GLint level,
733 GLint internalFormat,
734 GLint width, GLint border,
735 GLenum format, GLenum type, const void *pixels,
736 const struct gl_pixelstore_attrib *unpack,
737 struct gl_texture_object *texObj,
738 struct gl_texture_image *texImage)
739 {
740 st_TexImage(ctx, 1, target, level, internalFormat, width, 1, 1, border,
741 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
742 }
743
744
745 static void
746 st_CompressedTexImage2D(struct gl_context *ctx, GLenum target, GLint level,
747 GLint internalFormat,
748 GLint width, GLint height, GLint border,
749 GLsizei imageSize, const GLvoid *data,
750 struct gl_texture_object *texObj,
751 struct gl_texture_image *texImage)
752 {
753 st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border,
754 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE);
755 }
756
757
758
759 /**
760 * glGetTexImage() helper: decompress a compressed texture by rendering
761 * a textured quad. Store the results in the user's buffer.
762 */
763 static void
764 decompress_with_blit(struct gl_context * ctx, GLenum target, GLint level,
765 GLenum format, GLenum type, GLvoid *pixels,
766 struct gl_texture_object *texObj,
767 struct gl_texture_image *texImage)
768 {
769 struct st_context *st = st_context(ctx);
770 struct pipe_context *pipe = st->pipe;
771 struct st_texture_image *stImage = st_texture_image(texImage);
772 struct st_texture_object *stObj = st_texture_object(texObj);
773 struct pipe_sampler_view *src_view =
774 st_get_texture_sampler_view(stObj, pipe);
775 const GLuint width = texImage->Width;
776 const GLuint height = texImage->Height;
777 struct pipe_surface *dst_surface;
778 struct pipe_resource *dst_texture;
779 struct pipe_transfer *tex_xfer;
780 unsigned bind = (PIPE_BIND_RENDER_TARGET | /* util_blit may choose to render */
781 PIPE_BIND_TRANSFER_READ);
782
783 /* create temp / dest surface */
784 if (!util_create_rgba_surface(pipe, width, height, bind,
785 &dst_texture, &dst_surface)) {
786 _mesa_problem(ctx, "util_create_rgba_surface() failed "
787 "in decompress_with_blit()");
788 return;
789 }
790
791 /* blit/render/decompress */
792 util_blit_pixels_tex(st->blit,
793 src_view, /* pipe_resource (src) */
794 0, 0, /* src x0, y0 */
795 width, height, /* src x1, y1 */
796 dst_surface, /* pipe_surface (dst) */
797 0, 0, /* dst x0, y0 */
798 width, height, /* dst x1, y1 */
799 0.0, /* z */
800 PIPE_TEX_MIPFILTER_NEAREST);
801
802 /* map the dst_surface so we can read from it */
803 tex_xfer = pipe_get_transfer(st_context(ctx)->pipe,
804 dst_texture, 0, 0,
805 PIPE_TRANSFER_READ,
806 0, 0, width, height);
807
808 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
809
810 /* copy/pack data into user buffer */
811 if (st_equal_formats(stImage->pt->format, format, type)) {
812 /* memcpy */
813 const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format);
814 ubyte *map = pipe_transfer_map(pipe, tex_xfer);
815 GLuint row;
816 for (row = 0; row < height; row++) {
817 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
818 height, format, type, row, 0);
819 memcpy(dest, map, bytesPerRow);
820 map += tex_xfer->stride;
821 }
822 pipe_transfer_unmap(pipe, tex_xfer);
823 }
824 else {
825 /* format translation via floats */
826 GLuint row;
827 enum pipe_format format = util_format_linear(dst_texture->format);
828 for (row = 0; row < height; row++) {
829 const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
830 GLfloat rgba[4 * MAX_WIDTH];
831 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
832 height, format, type, row, 0);
833
834 if (ST_DEBUG & DEBUG_FALLBACK)
835 debug_printf("%s: fallback format translation\n", __FUNCTION__);
836
837 /* get float[4] rgba row from surface */
838 pipe_get_tile_rgba_format(pipe, tex_xfer, 0, row, width, 1,
839 format, rgba);
840
841 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
842 type, dest, &ctx->Pack, transferOps);
843 }
844 }
845
846 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
847
848 pipe->transfer_destroy(pipe, tex_xfer);
849
850 /* destroy the temp / dest surface */
851 util_destroy_rgba_surface(dst_texture, dst_surface);
852 }
853
854
855
856 /**
857 * Need to map texture image into memory before copying image data,
858 * then unmap it.
859 */
860 static void
861 st_get_tex_image(struct gl_context * ctx, GLenum target, GLint level,
862 GLenum format, GLenum type, GLvoid * pixels,
863 struct gl_texture_object *texObj,
864 struct gl_texture_image *texImage, GLboolean compressed_dst)
865 {
866 struct st_context *st = st_context(ctx);
867 struct st_texture_image *stImage = st_texture_image(texImage);
868 const GLuint dstImageStride =
869 _mesa_image_image_stride(&ctx->Pack, texImage->Width, texImage->Height,
870 format, type);
871 GLuint depth, i;
872 GLubyte *dest;
873
874 if (stImage->pt &&
875 util_format_is_s3tc(stImage->pt->format) &&
876 !compressed_dst) {
877 /* Need to decompress the texture.
878 * We'll do this by rendering a textured quad.
879 * Note that we only expect RGBA formats (no Z/depth formats).
880 */
881 decompress_with_blit(ctx, target, level, format, type, pixels,
882 texObj, texImage);
883 return;
884 }
885
886 /* Map */
887 if (stImage->pt) {
888 /* Image is stored in hardware format in a buffer managed by the
889 * kernel. Need to explicitly map and unmap it.
890 */
891 texImage->Data = st_texture_image_map(st, stImage, 0,
892 PIPE_TRANSFER_READ, 0, 0,
893 stImage->base.Width,
894 stImage->base.Height);
895 /* compute stride in texels from stride in bytes */
896 texImage->RowStride = stImage->transfer->stride
897 * util_format_get_blockwidth(stImage->pt->format)
898 / util_format_get_blocksize(stImage->pt->format);
899 }
900 else {
901 /* Otherwise, the image should actually be stored in
902 * texImage->Data. This is pretty confusing for
903 * everybody, I'd much prefer to separate the two functions of
904 * texImage->Data - storage for texture images in main memory
905 * and access (ie mappings) of images. In other words, we'd
906 * create a new texImage->Map field and leave Data simply for
907 * storage.
908 */
909 assert(texImage->Data);
910 }
911
912 depth = texImage->Depth;
913 texImage->Depth = 1;
914
915 dest = (GLubyte *) pixels;
916
917 _mesa_set_fetch_functions(texImage, get_texture_dims(target));
918
919 for (i = 0; i < depth; i++) {
920 if (compressed_dst) {
921 _mesa_get_compressed_teximage(ctx, target, level, dest,
922 texObj, texImage);
923 }
924 else {
925 _mesa_get_teximage(ctx, target, level, format, type, dest,
926 texObj, texImage);
927 }
928
929 if (stImage->pt && i + 1 < depth) {
930 /* unmap this slice */
931 st_texture_image_unmap(st, stImage);
932 /* map next slice of 3D texture */
933 texImage->Data = st_texture_image_map(st, stImage, i + 1,
934 PIPE_TRANSFER_READ, 0, 0,
935 stImage->base.Width,
936 stImage->base.Height);
937 dest += dstImageStride;
938 }
939 }
940
941 texImage->Depth = depth;
942
943 /* Unmap */
944 if (stImage->pt) {
945 st_texture_image_unmap(st, stImage);
946 texImage->Data = NULL;
947 }
948 }
949
950
951 static void
952 st_GetTexImage(struct gl_context * ctx, GLenum target, GLint level,
953 GLenum format, GLenum type, GLvoid * pixels,
954 struct gl_texture_object *texObj,
955 struct gl_texture_image *texImage)
956 {
957 st_get_tex_image(ctx, target, level, format, type, pixels, texObj, texImage,
958 GL_FALSE);
959 }
960
961
962 static void
963 st_GetCompressedTexImage(struct gl_context *ctx, GLenum target, GLint level,
964 GLvoid *pixels,
965 struct gl_texture_object *texObj,
966 struct gl_texture_image *texImage)
967 {
968 st_get_tex_image(ctx, target, level, 0, 0, pixels, texObj, texImage,
969 GL_TRUE);
970 }
971
972
973
974 static void
975 st_TexSubimage(struct gl_context *ctx, GLint dims, GLenum target, GLint level,
976 GLint xoffset, GLint yoffset, GLint zoffset,
977 GLint width, GLint height, GLint depth,
978 GLenum format, GLenum type, const void *pixels,
979 const struct gl_pixelstore_attrib *packing,
980 struct gl_texture_object *texObj,
981 struct gl_texture_image *texImage)
982 {
983 struct st_context *st = st_context(ctx);
984 struct st_texture_image *stImage = st_texture_image(texImage);
985 GLuint dstRowStride;
986 const GLuint srcImageStride =
987 _mesa_image_image_stride(packing, width, height, format, type);
988 GLint i;
989 const GLubyte *src;
990 /* init to silence warning only: */
991 enum pipe_transfer_usage transfer_usage = PIPE_TRANSFER_WRITE;
992
993 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
994 _mesa_lookup_enum_by_nr(target),
995 level, xoffset, yoffset, width, height);
996
997 pixels =
998 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format,
999 type, pixels, packing, "glTexSubImage2D");
1000 if (!pixels)
1001 return;
1002
1003 /* for a 1D array upload the image as a series of layer with height = 1 */
1004 if (target == GL_TEXTURE_1D_ARRAY) {
1005 depth = height;
1006 height = 1;
1007 }
1008
1009 /* Map buffer if necessary. Need to lock to prevent other contexts
1010 * from uploading the buffer under us.
1011 */
1012 if (stImage->pt) {
1013 if (format == GL_DEPTH_COMPONENT &&
1014 util_format_is_depth_and_stencil(stImage->pt->format))
1015 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1016 else
1017 transfer_usage = PIPE_TRANSFER_WRITE;
1018
1019 texImage->Data = st_texture_image_map(st, stImage, zoffset,
1020 transfer_usage,
1021 xoffset, yoffset,
1022 width, height);
1023 }
1024
1025 if (!texImage->Data) {
1026 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1027 goto done;
1028 }
1029
1030 src = (const GLubyte *) pixels;
1031 dstRowStride = stImage->transfer->stride;
1032
1033 for (i = 0; i < depth; i++) {
1034 if (!_mesa_texstore(ctx, dims, texImage->_BaseFormat,
1035 texImage->TexFormat,
1036 texImage->Data,
1037 0, 0, 0,
1038 dstRowStride,
1039 texImage->ImageOffsets,
1040 width, height, 1,
1041 format, type, src, packing)) {
1042 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1043 }
1044
1045 if (stImage->pt && i + 1 < depth) {
1046 /* unmap this slice */
1047 st_texture_image_unmap(st, stImage);
1048 /* map next slice of 3D texture */
1049 texImage->Data = st_texture_image_map(st, stImage,
1050 zoffset + i + 1,
1051 transfer_usage,
1052 xoffset, yoffset,
1053 width, height);
1054 src += srcImageStride;
1055 }
1056 }
1057
1058 done:
1059 _mesa_unmap_teximage_pbo(ctx, packing);
1060
1061 if (stImage->pt && texImage->Data) {
1062 st_texture_image_unmap(st, stImage);
1063 texImage->Data = NULL;
1064 }
1065 }
1066
1067
1068
1069 static void
1070 st_TexSubImage3D(struct gl_context *ctx, GLenum target, GLint level,
1071 GLint xoffset, GLint yoffset, GLint zoffset,
1072 GLsizei width, GLsizei height, GLsizei depth,
1073 GLenum format, GLenum type, const GLvoid *pixels,
1074 const struct gl_pixelstore_attrib *packing,
1075 struct gl_texture_object *texObj,
1076 struct gl_texture_image *texImage)
1077 {
1078 st_TexSubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
1079 width, height, depth, format, type,
1080 pixels, packing, texObj, texImage);
1081 }
1082
1083
1084 static void
1085 st_TexSubImage2D(struct gl_context *ctx, GLenum target, GLint level,
1086 GLint xoffset, GLint yoffset,
1087 GLsizei width, GLsizei height,
1088 GLenum format, GLenum type, const GLvoid * pixels,
1089 const struct gl_pixelstore_attrib *packing,
1090 struct gl_texture_object *texObj,
1091 struct gl_texture_image *texImage)
1092 {
1093 st_TexSubimage(ctx, 2, target, level, xoffset, yoffset, 0,
1094 width, height, 1, format, type,
1095 pixels, packing, texObj, texImage);
1096 }
1097
1098
1099 static void
1100 st_TexSubImage1D(struct gl_context *ctx, GLenum target, GLint level,
1101 GLint xoffset, GLsizei width, GLenum format, GLenum type,
1102 const GLvoid * pixels,
1103 const struct gl_pixelstore_attrib *packing,
1104 struct gl_texture_object *texObj,
1105 struct gl_texture_image *texImage)
1106 {
1107 st_TexSubimage(ctx, 1, target, level, xoffset, 0, 0, width, 1, 1,
1108 format, type, pixels, packing, texObj, texImage);
1109 }
1110
1111
1112 static void
1113 st_CompressedTexSubImage1D(struct gl_context *ctx, GLenum target, GLint level,
1114 GLint xoffset, GLsizei width,
1115 GLenum format,
1116 GLsizei imageSize, const GLvoid *data,
1117 struct gl_texture_object *texObj,
1118 struct gl_texture_image *texImage)
1119 {
1120 assert(0);
1121 }
1122
1123
1124 static void
1125 st_CompressedTexSubImage2D(struct gl_context *ctx, GLenum target, GLint level,
1126 GLint xoffset, GLint yoffset,
1127 GLsizei width, GLint height,
1128 GLenum format,
1129 GLsizei imageSize, const GLvoid *data,
1130 struct gl_texture_object *texObj,
1131 struct gl_texture_image *texImage)
1132 {
1133 struct st_context *st = st_context(ctx);
1134 struct st_texture_image *stImage = st_texture_image(texImage);
1135 int srcBlockStride;
1136 int dstBlockStride;
1137 int y;
1138 enum pipe_format pformat;
1139
1140 if (stImage->pt) {
1141 pformat = stImage->pt->format;
1142
1143 texImage->Data = st_texture_image_map(st, stImage, 0,
1144 PIPE_TRANSFER_WRITE,
1145 xoffset, yoffset,
1146 width, height);
1147
1148 srcBlockStride = util_format_get_stride(pformat, width);
1149 dstBlockStride = stImage->transfer->stride;
1150 } else {
1151 assert(stImage->pt);
1152 /* TODO find good values for block and strides */
1153 /* TODO also adjust texImage->data for yoffset/xoffset */
1154 return;
1155 }
1156
1157 if (!texImage->Data) {
1158 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexSubImage");
1159 return;
1160 }
1161
1162 assert(xoffset % util_format_get_blockwidth(pformat) == 0);
1163 assert(yoffset % util_format_get_blockheight(pformat) == 0);
1164
1165 for (y = 0; y < height; y += util_format_get_blockheight(pformat)) {
1166 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1167 const char *src = (const char*)data + srcBlockStride * util_format_get_nblocksy(pformat, y);
1168 char *dst = (char*)texImage->Data + dstBlockStride * util_format_get_nblocksy(pformat, y);
1169 memcpy(dst, src, util_format_get_stride(pformat, width));
1170 }
1171
1172 if (stImage->pt) {
1173 st_texture_image_unmap(st, stImage);
1174 texImage->Data = NULL;
1175 }
1176 }
1177
1178
1179 static void
1180 st_CompressedTexSubImage3D(struct gl_context *ctx, GLenum target, GLint level,
1181 GLint xoffset, GLint yoffset, GLint zoffset,
1182 GLsizei width, GLint height, GLint depth,
1183 GLenum format,
1184 GLsizei imageSize, const GLvoid *data,
1185 struct gl_texture_object *texObj,
1186 struct gl_texture_image *texImage)
1187 {
1188 assert(0);
1189 }
1190
1191
1192
1193 /**
1194 * Do a CopyTexSubImage operation using a read transfer from the source,
1195 * a write transfer to the destination and get_tile()/put_tile() to access
1196 * the pixels/texels.
1197 *
1198 * Note: srcY=0=TOP of renderbuffer
1199 */
1200 static void
1201 fallback_copy_texsubimage(struct gl_context *ctx, GLenum target, GLint level,
1202 struct st_renderbuffer *strb,
1203 struct st_texture_image *stImage,
1204 GLenum baseFormat,
1205 GLint destX, GLint destY, GLint destZ,
1206 GLint srcX, GLint srcY,
1207 GLsizei width, GLsizei height)
1208 {
1209 struct st_context *st = st_context(ctx);
1210 struct pipe_context *pipe = st->pipe;
1211 struct pipe_transfer *src_trans;
1212 GLvoid *texDest;
1213 enum pipe_transfer_usage transfer_usage;
1214
1215 if (ST_DEBUG & DEBUG_FALLBACK)
1216 debug_printf("%s: fallback processing\n", __FUNCTION__);
1217
1218 assert(width <= MAX_WIDTH);
1219
1220 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1221 srcY = strb->Base.Height - srcY - height;
1222 }
1223
1224 src_trans = pipe_get_transfer(st_context(ctx)->pipe,
1225 strb->texture,
1226 0, 0,
1227 PIPE_TRANSFER_READ,
1228 srcX, srcY,
1229 width, height);
1230
1231 if ((baseFormat == GL_DEPTH_COMPONENT ||
1232 baseFormat == GL_DEPTH_STENCIL) &&
1233 util_format_is_depth_and_stencil(stImage->pt->format))
1234 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1235 else
1236 transfer_usage = PIPE_TRANSFER_WRITE;
1237
1238 /* XXX this used to ignore destZ param */
1239 texDest = st_texture_image_map(st, stImage, destZ, transfer_usage,
1240 destX, destY, width, height);
1241
1242 if (baseFormat == GL_DEPTH_COMPONENT ||
1243 baseFormat == GL_DEPTH_STENCIL) {
1244 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
1245 ctx->Pixel.DepthBias != 0.0F);
1246 GLint row, yStep;
1247
1248 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1249 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1250 srcY = height - 1;
1251 yStep = -1;
1252 }
1253 else {
1254 srcY = 0;
1255 yStep = 1;
1256 }
1257
1258 /* To avoid a large temp memory allocation, do copy row by row */
1259 for (row = 0; row < height; row++, srcY += yStep) {
1260 uint data[MAX_WIDTH];
1261 pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
1262 if (scaleOrBias) {
1263 _mesa_scale_and_bias_depth_uint(ctx, width, data);
1264 }
1265 pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
1266 }
1267 }
1268 else {
1269 /* RGBA format */
1270 GLfloat *tempSrc =
1271 (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1272
1273 if (tempSrc && texDest) {
1274 const GLint dims = 2;
1275 const GLint dstRowStride = stImage->transfer->stride;
1276 struct gl_texture_image *texImage = &stImage->base;
1277 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
1278
1279 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1280 unpack.Invert = GL_TRUE;
1281 }
1282
1283 /* get float/RGBA image from framebuffer */
1284 /* XXX this usually involves a lot of int/float conversion.
1285 * try to avoid that someday.
1286 */
1287 pipe_get_tile_rgba_format(pipe, src_trans, 0, 0, width, height,
1288 util_format_linear(strb->texture->format),
1289 tempSrc);
1290
1291 /* Store into texture memory.
1292 * Note that this does some special things such as pixel transfer
1293 * ops and format conversion. In particular, if the dest tex format
1294 * is actually RGBA but the user created the texture as GL_RGB we
1295 * need to fill-in/override the alpha channel with 1.0.
1296 */
1297 _mesa_texstore(ctx, dims,
1298 texImage->_BaseFormat,
1299 texImage->TexFormat,
1300 texDest,
1301 0, 0, 0,
1302 dstRowStride,
1303 texImage->ImageOffsets,
1304 width, height, 1,
1305 GL_RGBA, GL_FLOAT, tempSrc, /* src */
1306 &unpack);
1307 }
1308 else {
1309 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1310 }
1311
1312 if (tempSrc)
1313 free(tempSrc);
1314 }
1315
1316 st_texture_image_unmap(st, stImage);
1317 pipe->transfer_destroy(pipe, src_trans);
1318 }
1319
1320
1321
1322 /**
1323 * If the format of the src renderbuffer and the format of the dest
1324 * texture are compatible (in terms of blitting), return a TGSI writemask
1325 * to be used during the blit.
1326 * If the src/dest are incompatible, return 0.
1327 */
1328 static unsigned
1329 compatible_src_dst_formats(struct gl_context *ctx,
1330 const struct gl_renderbuffer *src,
1331 const struct gl_texture_image *dst)
1332 {
1333 /* Get logical base formats for the src and dest.
1334 * That is, use the user-requested formats and not the actual, device-
1335 * chosen formats.
1336 * For example, the user may have requested an A8 texture but the
1337 * driver may actually be using an RGBA texture format. When we
1338 * copy/blit to that texture, we only want to copy the Alpha channel
1339 * and not the RGB channels.
1340 *
1341 * Similarly, when the src FBO was created an RGB format may have been
1342 * requested but the driver actually chose an RGBA format. In that case,
1343 * we don't want to copy the undefined Alpha channel to the dest texture
1344 * (it should be 1.0).
1345 */
1346 const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
1347 const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
1348
1349 /**
1350 * XXX when we have red-only and red/green renderbuffers we'll need
1351 * to add more cases here (or implement a general-purpose routine that
1352 * queries the existance of the R,G,B,A channels in the src and dest).
1353 */
1354 if (srcFormat == dstFormat) {
1355 /* This is the same as matching_base_formats, which should
1356 * always pass, as it did previously.
1357 */
1358 return TGSI_WRITEMASK_XYZW;
1359 }
1360 else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
1361 /* Make sure that A in the dest is 1. The actual src format
1362 * may be RGBA and have undefined A values.
1363 */
1364 return TGSI_WRITEMASK_XYZ;
1365 }
1366 else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
1367 /* Make sure that A in the dest is 1. The actual dst format
1368 * may be RGBA and will need A=1 to provide proper alpha values
1369 * when sampled later.
1370 */
1371 return TGSI_WRITEMASK_XYZ;
1372 }
1373 else {
1374 if (ST_DEBUG & DEBUG_FALLBACK)
1375 debug_printf("%s failed for src %s, dst %s\n",
1376 __FUNCTION__,
1377 _mesa_lookup_enum_by_nr(srcFormat),
1378 _mesa_lookup_enum_by_nr(dstFormat));
1379
1380 /* Otherwise fail.
1381 */
1382 return 0;
1383 }
1384 }
1385
1386
1387
1388 /**
1389 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1390 * Note that the region to copy has already been clipped so we know we
1391 * won't read from outside the source renderbuffer's bounds.
1392 *
1393 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1394 */
1395 static void
1396 st_copy_texsubimage(struct gl_context *ctx,
1397 GLenum target, GLint level,
1398 GLint destX, GLint destY, GLint destZ,
1399 GLint srcX, GLint srcY,
1400 GLsizei width, GLsizei height)
1401 {
1402 struct gl_texture_unit *texUnit =
1403 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1404 struct gl_texture_object *texObj =
1405 _mesa_select_tex_object(ctx, texUnit, target);
1406 struct gl_texture_image *texImage =
1407 _mesa_select_tex_image(ctx, texObj, target, level);
1408 struct st_texture_image *stImage = st_texture_image(texImage);
1409 const GLenum texBaseFormat = texImage->_BaseFormat;
1410 struct gl_framebuffer *fb = ctx->ReadBuffer;
1411 struct st_renderbuffer *strb;
1412 struct st_context *st = st_context(ctx);
1413 struct pipe_context *pipe = st->pipe;
1414 struct pipe_screen *screen = pipe->screen;
1415 enum pipe_format dest_format, src_format;
1416 GLboolean use_fallback = GL_TRUE;
1417 GLboolean matching_base_formats;
1418 GLuint format_writemask, sample_count;
1419 struct pipe_surface *dest_surface = NULL;
1420 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1421
1422 /* make sure finalize_textures has been called?
1423 */
1424 if (0) st_validate_state(st);
1425
1426 /* determine if copying depth or color data */
1427 if (texBaseFormat == GL_DEPTH_COMPONENT ||
1428 texBaseFormat == GL_DEPTH_STENCIL) {
1429 strb = st_renderbuffer(fb->_DepthBuffer);
1430 if (strb->Base.Wrapped) {
1431 strb = st_renderbuffer(strb->Base.Wrapped);
1432 }
1433 }
1434 else {
1435 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1436 strb = st_renderbuffer(fb->_ColorReadBuffer);
1437 }
1438
1439 if (!strb || !strb->surface || !stImage->pt) {
1440 debug_printf("%s: null strb or stImage\n", __FUNCTION__);
1441 return;
1442 }
1443
1444 sample_count = strb->surface->texture->nr_samples;
1445 /* I believe this would be legal, presumably would need to do a resolve
1446 for color, and for depth/stencil spec says to just use one of the
1447 depth/stencil samples per pixel? Need some transfer clarifications. */
1448 assert(sample_count < 2);
1449
1450 if (srcX < 0) {
1451 width -= -srcX;
1452 destX += -srcX;
1453 srcX = 0;
1454 }
1455
1456 if (srcY < 0) {
1457 height -= -srcY;
1458 destY += -srcY;
1459 srcY = 0;
1460 }
1461
1462 if (destX < 0) {
1463 width -= -destX;
1464 srcX += -destX;
1465 destX = 0;
1466 }
1467
1468 if (destY < 0) {
1469 height -= -destY;
1470 srcY += -destY;
1471 destY = 0;
1472 }
1473
1474 if (width < 0 || height < 0)
1475 return;
1476
1477
1478 assert(strb);
1479 assert(strb->surface);
1480 assert(stImage->pt);
1481
1482 src_format = strb->surface->format;
1483 dest_format = stImage->pt->format;
1484
1485 /*
1486 * Determine if the src framebuffer and dest texture have the same
1487 * base format. We need this to detect a case such as the framebuffer
1488 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1489 * texture format stores RGBA we need to set A=1 (overriding the
1490 * framebuffer's alpha values). We can't do that with the blit or
1491 * textured-quad paths.
1492 */
1493 matching_base_formats =
1494 (_mesa_get_format_base_format(strb->Base.Format) ==
1495 _mesa_get_format_base_format(texImage->TexFormat));
1496 format_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
1497
1498 if (ctx->_ImageTransferState == 0x0) {
1499
1500 if (matching_base_formats &&
1501 src_format == dest_format &&
1502 !do_flip)
1503 {
1504 /* use surface_copy() / blit */
1505 struct pipe_box src_box;
1506 u_box_2d_zslice(srcX, srcY, strb->surface->u.tex.first_layer,
1507 width, height, &src_box);
1508
1509 /* for resource_copy_region(), y=0=top, always */
1510 pipe->resource_copy_region(pipe,
1511 /* dest */
1512 stImage->pt,
1513 stImage->level,
1514 destX, destY, destZ + stImage->face,
1515 /* src */
1516 strb->texture,
1517 strb->surface->u.tex.level,
1518 &src_box);
1519 use_fallback = GL_FALSE;
1520 }
1521 else if (format_writemask &&
1522 texBaseFormat != GL_DEPTH_COMPONENT &&
1523 texBaseFormat != GL_DEPTH_STENCIL &&
1524 screen->is_format_supported(screen, src_format,
1525 PIPE_TEXTURE_2D, sample_count,
1526 PIPE_BIND_SAMPLER_VIEW,
1527 0) &&
1528 screen->is_format_supported(screen, dest_format,
1529 PIPE_TEXTURE_2D, 0,
1530 PIPE_BIND_RENDER_TARGET,
1531 0)) {
1532 /* draw textured quad to do the copy */
1533 GLint srcY0, srcY1;
1534 struct pipe_surface surf_tmpl;
1535 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1536 surf_tmpl.format = stImage->pt->format;
1537 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
1538 surf_tmpl.u.tex.level = stImage->level;
1539 surf_tmpl.u.tex.first_layer = stImage->face + destZ;
1540 surf_tmpl.u.tex.last_layer = stImage->face + destZ;
1541
1542 dest_surface = pipe->create_surface(pipe, stImage->pt,
1543 &surf_tmpl);
1544
1545 if (do_flip) {
1546 srcY1 = strb->Base.Height - srcY - height;
1547 srcY0 = srcY1 + height;
1548 }
1549 else {
1550 srcY0 = srcY;
1551 srcY1 = srcY0 + height;
1552 }
1553
1554 util_blit_pixels_writemask(st->blit,
1555 strb->texture,
1556 strb->surface->u.tex.level,
1557 srcX, srcY0,
1558 srcX + width, srcY1,
1559 strb->surface->u.tex.first_layer,
1560 dest_surface,
1561 destX, destY,
1562 destX + width, destY + height,
1563 0.0, PIPE_TEX_MIPFILTER_NEAREST,
1564 format_writemask);
1565 use_fallback = GL_FALSE;
1566 }
1567
1568 if (dest_surface)
1569 pipe_surface_reference(&dest_surface, NULL);
1570 }
1571
1572 if (use_fallback) {
1573 /* software fallback */
1574 fallback_copy_texsubimage(ctx, target, level,
1575 strb, stImage, texBaseFormat,
1576 destX, destY, destZ,
1577 srcX, srcY, width, height);
1578 }
1579 }
1580
1581
1582
1583 static void
1584 st_CopyTexImage1D(struct gl_context * ctx, GLenum target, GLint level,
1585 GLenum internalFormat,
1586 GLint x, GLint y, GLsizei width, GLint border)
1587 {
1588 struct gl_texture_unit *texUnit =
1589 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1590 struct gl_texture_object *texObj =
1591 _mesa_select_tex_object(ctx, texUnit, target);
1592 struct gl_texture_image *texImage =
1593 _mesa_select_tex_image(ctx, texObj, target, level);
1594
1595 /* Setup or redefine the texture object, texture and texture
1596 * image. Don't populate yet.
1597 */
1598 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
1599 width, border,
1600 GL_RGBA, CHAN_TYPE, NULL,
1601 &ctx->DefaultPacking, texObj, texImage);
1602
1603 st_copy_texsubimage(ctx, target, level,
1604 0, 0, 0, /* destX,Y,Z */
1605 x, y, width, 1); /* src X, Y, size */
1606 }
1607
1608
1609 static void
1610 st_CopyTexImage2D(struct gl_context * ctx, GLenum target, GLint level,
1611 GLenum internalFormat,
1612 GLint x, GLint y, GLsizei width, GLsizei height,
1613 GLint border)
1614 {
1615 struct gl_texture_unit *texUnit =
1616 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1617 struct gl_texture_object *texObj =
1618 _mesa_select_tex_object(ctx, texUnit, target);
1619 struct gl_texture_image *texImage =
1620 _mesa_select_tex_image(ctx, texObj, target, level);
1621
1622 /* Setup or redefine the texture object, texture and texture
1623 * image. Don't populate yet.
1624 */
1625 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
1626 width, height, border,
1627 GL_RGBA, CHAN_TYPE, NULL,
1628 &ctx->DefaultPacking, texObj, texImage);
1629
1630 st_copy_texsubimage(ctx, target, level,
1631 0, 0, 0, /* destX,Y,Z */
1632 x, y, width, height); /* src X, Y, size */
1633 }
1634
1635
1636 static void
1637 st_CopyTexSubImage1D(struct gl_context * ctx, GLenum target, GLint level,
1638 GLint xoffset, GLint x, GLint y, GLsizei width)
1639 {
1640 const GLint yoffset = 0, zoffset = 0;
1641 const GLsizei height = 1;
1642 st_copy_texsubimage(ctx, target, level,
1643 xoffset, yoffset, zoffset, /* destX,Y,Z */
1644 x, y, width, height); /* src X, Y, size */
1645 }
1646
1647
1648 static void
1649 st_CopyTexSubImage2D(struct gl_context * ctx, GLenum target, GLint level,
1650 GLint xoffset, GLint yoffset,
1651 GLint x, GLint y, GLsizei width, GLsizei height)
1652 {
1653 const GLint zoffset = 0;
1654 st_copy_texsubimage(ctx, target, level,
1655 xoffset, yoffset, zoffset, /* destX,Y,Z */
1656 x, y, width, height); /* src X, Y, size */
1657 }
1658
1659
1660 static void
1661 st_CopyTexSubImage3D(struct gl_context * ctx, GLenum target, GLint level,
1662 GLint xoffset, GLint yoffset, GLint zoffset,
1663 GLint x, GLint y, GLsizei width, GLsizei height)
1664 {
1665 st_copy_texsubimage(ctx, target, level,
1666 xoffset, yoffset, zoffset, /* destX,Y,Z */
1667 x, y, width, height); /* src X, Y, size */
1668 }
1669
1670
1671 /**
1672 * Copy image data from stImage into the texture object 'stObj' at level
1673 * 'dstLevel'.
1674 */
1675 static void
1676 copy_image_data_to_texture(struct st_context *st,
1677 struct st_texture_object *stObj,
1678 GLuint dstLevel,
1679 struct st_texture_image *stImage)
1680 {
1681 /* debug checks */
1682 {
1683 const struct gl_texture_image *dstImage =
1684 stObj->base.Image[stImage->face][stImage->level];
1685 assert(dstImage);
1686 assert(dstImage->Width == stImage->base.Width);
1687 assert(dstImage->Height == stImage->base.Height);
1688 assert(dstImage->Depth == stImage->base.Depth);
1689 }
1690
1691 if (stImage->pt) {
1692 /* Copy potentially with the blitter:
1693 */
1694 st_texture_image_copy(st->pipe,
1695 stObj->pt, dstLevel, /* dest texture, level */
1696 stImage->pt, stImage->level, /* src texture, level */
1697 stImage->face);
1698
1699 pipe_resource_reference(&stImage->pt, NULL);
1700 }
1701 else if (stImage->base.Data) {
1702 st_texture_image_data(st,
1703 stObj->pt,
1704 stImage->face,
1705 dstLevel,
1706 stImage->base.Data,
1707 stImage->base.RowStride *
1708 util_format_get_blocksize(stObj->pt->format),
1709 stImage->base.RowStride *
1710 stImage->base.Height *
1711 util_format_get_blocksize(stObj->pt->format));
1712 _mesa_align_free(stImage->base.Data);
1713 stImage->base.Data = NULL;
1714 }
1715
1716 pipe_resource_reference(&stImage->pt, stObj->pt);
1717 }
1718
1719
1720 /**
1721 * Called during state validation. When this function is finished,
1722 * the texture object should be ready for rendering.
1723 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1724 */
1725 GLboolean
1726 st_finalize_texture(struct gl_context *ctx,
1727 struct pipe_context *pipe,
1728 struct gl_texture_object *tObj)
1729 {
1730 struct st_context *st = st_context(ctx);
1731 struct st_texture_object *stObj = st_texture_object(tObj);
1732 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1733 GLuint face;
1734 struct st_texture_image *firstImage;
1735 enum pipe_format firstImageFormat;
1736 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
1737
1738 if (stObj->base._Complete) {
1739 /* The texture is complete and we know exactly how many mipmap levels
1740 * are present/needed. This is conditional because we may be called
1741 * from the st_generate_mipmap() function when the texture object is
1742 * incomplete. In that case, we'll have set stObj->lastLevel before
1743 * we get here.
1744 */
1745 if (stObj->base.MinFilter == GL_LINEAR ||
1746 stObj->base.MinFilter == GL_NEAREST)
1747 stObj->lastLevel = stObj->base.BaseLevel;
1748 else
1749 stObj->lastLevel = stObj->base._MaxLevel;
1750 }
1751
1752 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1753 assert(firstImage);
1754
1755 /* If both firstImage and stObj point to a texture which can contain
1756 * all active images, favour firstImage. Note that because of the
1757 * completeness requirement, we know that the image dimensions
1758 * will match.
1759 */
1760 if (firstImage->pt &&
1761 firstImage->pt != stObj->pt &&
1762 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
1763 pipe_resource_reference(&stObj->pt, firstImage->pt);
1764 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1765 }
1766
1767 /* Find gallium format for the Mesa texture */
1768 firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1769
1770 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1771 {
1772 GLuint width, height, depth;
1773 if (!guess_base_level_size(stObj->base.Target,
1774 firstImage->base.Width2,
1775 firstImage->base.Height2,
1776 firstImage->base.Depth2,
1777 stObj->base.BaseLevel,
1778 &width, &height, &depth)) {
1779 width = stObj->width0;
1780 height = stObj->height0;
1781 depth = stObj->depth0;
1782 }
1783 /* convert GL dims to Gallium dims */
1784 st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
1785 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1786 }
1787
1788 /* If we already have a gallium texture, check that it matches the texture
1789 * object's format, target, size, num_levels, etc.
1790 */
1791 if (stObj->pt) {
1792 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1793 !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
1794 stObj->pt->last_level < stObj->lastLevel ||
1795 stObj->pt->width0 != ptWidth ||
1796 stObj->pt->height0 != ptHeight ||
1797 stObj->pt->depth0 != ptDepth ||
1798 stObj->pt->array_size != ptLayers)
1799 {
1800 /* The gallium texture does not match the Mesa texture so delete the
1801 * gallium texture now. We'll make a new one below.
1802 */
1803 pipe_resource_reference(&stObj->pt, NULL);
1804 pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1805 st->dirty.st |= ST_NEW_FRAMEBUFFER;
1806 }
1807 }
1808
1809 /* May need to create a new gallium texture:
1810 */
1811 if (!stObj->pt) {
1812 GLuint bindings = default_bindings(st, firstImageFormat);
1813
1814 stObj->pt = st_texture_create(st,
1815 gl_target_to_pipe(stObj->base.Target),
1816 firstImageFormat,
1817 stObj->lastLevel,
1818 ptWidth,
1819 ptHeight,
1820 ptDepth,
1821 ptLayers,
1822 bindings);
1823
1824 if (!stObj->pt) {
1825 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1826 return GL_FALSE;
1827 }
1828 }
1829
1830 /* Pull in any images not in the object's texture:
1831 */
1832 for (face = 0; face < nr_faces; face++) {
1833 GLuint level;
1834 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1835 struct st_texture_image *stImage =
1836 st_texture_image(stObj->base.Image[face][level]);
1837
1838 /* Need to import images in main memory or held in other textures.
1839 */
1840 if (stImage && stObj->pt != stImage->pt) {
1841 copy_image_data_to_texture(st, stObj, level, stImage);
1842 }
1843 }
1844 }
1845
1846 return GL_TRUE;
1847 }
1848
1849
1850 /**
1851 * Returns pointer to a default/dummy texture.
1852 * This is typically used when the current shader has tex/sample instructions
1853 * but the user has not provided a (any) texture(s).
1854 */
1855 struct gl_texture_object *
1856 st_get_default_texture(struct st_context *st)
1857 {
1858 if (!st->default_texture) {
1859 static const GLenum target = GL_TEXTURE_2D;
1860 GLubyte pixels[16][16][4];
1861 struct gl_texture_object *texObj;
1862 struct gl_texture_image *texImg;
1863 GLuint i, j;
1864
1865 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1866 * when attempting to sample incomplete textures.
1867 */
1868 for (i = 0; i < 16; i++) {
1869 for (j = 0; j < 16; j++) {
1870 pixels[i][j][0] = 0;
1871 pixels[i][j][1] = 0;
1872 pixels[i][j][2] = 0;
1873 pixels[i][j][3] = 255;
1874 }
1875 }
1876
1877 texObj = st->ctx->Driver.NewTextureObject(st->ctx, 0, target);
1878
1879 texImg = _mesa_get_tex_image(st->ctx, texObj, target, 0);
1880
1881 _mesa_init_teximage_fields(st->ctx, target, texImg,
1882 16, 16, 1, 0, /* w, h, d, border */
1883 GL_RGBA, MESA_FORMAT_RGBA8888);
1884
1885 st_TexImage(st->ctx, 2, target,
1886 0, GL_RGBA, /* level, intformat */
1887 16, 16, 1, 0, /* w, h, d, border */
1888 GL_RGBA, GL_UNSIGNED_BYTE, pixels,
1889 &st->ctx->DefaultPacking,
1890 texObj, texImg,
1891 0, 0);
1892
1893 texObj->MinFilter = GL_NEAREST;
1894 texObj->MagFilter = GL_NEAREST;
1895 texObj->_Complete = GL_TRUE;
1896
1897 st->default_texture = texObj;
1898 }
1899 return st->default_texture;
1900 }
1901
1902
1903 void
1904 st_init_texture_functions(struct dd_function_table *functions)
1905 {
1906 functions->ChooseTextureFormat = st_ChooseTextureFormat;
1907 functions->TexImage1D = st_TexImage1D;
1908 functions->TexImage2D = st_TexImage2D;
1909 functions->TexImage3D = st_TexImage3D;
1910 functions->TexSubImage1D = st_TexSubImage1D;
1911 functions->TexSubImage2D = st_TexSubImage2D;
1912 functions->TexSubImage3D = st_TexSubImage3D;
1913 functions->CompressedTexSubImage1D = st_CompressedTexSubImage1D;
1914 functions->CompressedTexSubImage2D = st_CompressedTexSubImage2D;
1915 functions->CompressedTexSubImage3D = st_CompressedTexSubImage3D;
1916 functions->CopyTexImage1D = st_CopyTexImage1D;
1917 functions->CopyTexImage2D = st_CopyTexImage2D;
1918 functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
1919 functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
1920 functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
1921 functions->GenerateMipmap = st_generate_mipmap;
1922
1923 functions->GetTexImage = st_GetTexImage;
1924
1925 /* compressed texture functions */
1926 functions->CompressedTexImage2D = st_CompressedTexImage2D;
1927 functions->GetCompressedTexImage = st_GetCompressedTexImage;
1928
1929 functions->NewTextureObject = st_NewTextureObject;
1930 functions->NewTextureImage = st_NewTextureImage;
1931 functions->DeleteTexture = st_DeleteTextureObject;
1932 functions->FreeTexImageData = st_FreeTextureImageData;
1933
1934 functions->TextureMemCpy = do_memcpy;
1935
1936 /* XXX Temporary until we can query pipe's texture sizes */
1937 functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1938 }