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