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