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