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