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