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