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