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