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