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