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