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