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