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