Merge remote branch 'origin/gallium-st-api'
[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/fbobject.h"
35 #include "main/formats.h"
36 #include "main/image.h"
37 #include "main/imports.h"
38 #include "main/macros.h"
39 #include "main/mipmap.h"
40 #include "main/texcompress.h"
41 #include "main/texfetch.h"
42 #include "main/texgetimage.h"
43 #include "main/teximage.h"
44 #include "main/texobj.h"
45 #include "main/texstore.h"
46
47 #include "state_tracker/st_debug.h"
48 #include "state_tracker/st_context.h"
49 #include "state_tracker/st_cb_fbo.h"
50 #include "state_tracker/st_cb_texture.h"
51 #include "state_tracker/st_format.h"
52 #include "state_tracker/st_public.h"
53 #include "state_tracker/st_texture.h"
54 #include "state_tracker/st_gen_mipmap.h"
55 #include "state_tracker/st_inlines.h"
56 #include "state_tracker/st_atom.h"
57
58 #include "pipe/p_context.h"
59 #include "pipe/p_defines.h"
60 #include "util/u_inlines.h"
61 #include "pipe/p_shader_tokens.h"
62 #include "util/u_tile.h"
63 #include "util/u_blit.h"
64 #include "util/u_format.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 (util_format_is_depth_or_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_context *pipe = ctx->st->pipe;
375 struct pipe_screen *screen = pipe->screen;
376 gl_format mesa_format;
377 struct pipe_texture templ;
378 struct pipe_texture *src_tex;
379 struct pipe_surface *dst_surface;
380 struct pipe_transfer *tex_xfer;
381 void *map;
382
383 if (!stImage->pt) {
384 /* XXX: Can this happen? Should we assert? */
385 return GL_FALSE;
386 }
387
388 /* get destination surface (in the compressed texture) */
389 dst_surface = screen->get_tex_surface(screen, stImage->pt,
390 stImage->face, stImage->level, 0,
391 PIPE_BUFFER_USAGE_GPU_WRITE);
392 if (!dst_surface) {
393 /* can't render into this format (or other problem) */
394 return GL_FALSE;
395 }
396
397 /* Choose format for the temporary RGBA texture image.
398 */
399 mesa_format = st_ChooseTextureFormat(ctx, GL_RGBA, format, type);
400 assert(mesa_format);
401 if (!mesa_format)
402 return GL_FALSE;
403
404 /* Create the temporary source texture
405 */
406 memset(&templ, 0, sizeof(templ));
407 templ.target = PIPE_TEXTURE_2D;
408 templ.format = st_mesa_format_to_pipe_format(mesa_format);
409 templ.width0 = width;
410 templ.height0 = height;
411 templ.depth0 = 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 = pipe->transfer_map(pipe, 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 pipe->transfer_unmap(pipe, tex_xfer);
438 pipe->tex_transfer_destroy(pipe, 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 /*
548 * See if the new image is somehow incompatible with the existing
549 * mipmap. If so, free the old mipmap.
550 */
551 if (stObj->pt) {
552 if (stObj->teximage_realloc ||
553 level > (GLint) stObj->pt->last_level ||
554 !st_texture_match_image(stObj->pt, &stImage->base,
555 stImage->face, stImage->level)) {
556 DBG("release it\n");
557 pipe_texture_reference(&stObj->pt, NULL);
558 assert(!stObj->pt);
559 stObj->teximage_realloc = FALSE;
560 }
561 }
562
563 if (width == 0 || height == 0 || depth == 0) {
564 /* stop after freeing old image */
565 return;
566 }
567
568 if (!stObj->pt) {
569 guess_and_alloc_texture(ctx->st, stObj, stImage);
570 if (!stObj->pt) {
571 /* Probably out of memory.
572 * Try flushing any pending rendering, then retry.
573 */
574 st_finish(ctx->st);
575 guess_and_alloc_texture(ctx->st, stObj, stImage);
576 if (!stObj->pt) {
577 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
578 return;
579 }
580 }
581 }
582
583 assert(!stImage->pt);
584
585 if (stObj->pt &&
586 st_texture_match_image(stObj->pt, &stImage->base,
587 stImage->face, stImage->level)) {
588
589 pipe_texture_reference(&stImage->pt, stObj->pt);
590 assert(stImage->pt);
591 }
592
593 if (!stImage->pt)
594 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
595
596 /* st_CopyTexImage calls this function with pixels == NULL, with
597 * the expectation that the texture will be set up but nothing
598 * more will be done. This is where those calls return:
599 */
600 if (compressed_src) {
601 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
602 unpack,
603 "glCompressedTexImage");
604 }
605 else {
606 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
607 format, type,
608 pixels, unpack, "glTexImage");
609 }
610
611 /* Note: we can't check for pixels==NULL until after we've allocated
612 * memory for the texture.
613 */
614
615 /* See if we can do texture compression with a blit/render.
616 */
617 if (!compressed_src &&
618 !ctx->Mesa_DXTn &&
619 _mesa_is_format_compressed(texImage->TexFormat) &&
620 screen->is_format_supported(screen,
621 stImage->pt->format,
622 stImage->pt->target,
623 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
624 if (!pixels)
625 goto done;
626
627 if (compress_with_blit(ctx, target, level, 0, 0, 0, width, height, depth,
628 format, type, pixels, unpack, texImage)) {
629 goto done;
630 }
631 }
632
633 if (stImage->pt) {
634 if (format == GL_DEPTH_COMPONENT &&
635 util_format_is_depth_and_stencil(stImage->pt->format))
636 transfer_usage = PIPE_TRANSFER_READ_WRITE;
637 else
638 transfer_usage = PIPE_TRANSFER_WRITE;
639
640 texImage->Data = st_texture_image_map(ctx->st, stImage, 0,
641 transfer_usage, 0, 0,
642 stImage->base.Width,
643 stImage->base.Height);
644 if(stImage->transfer)
645 dstRowStride = stImage->transfer->stride;
646 }
647 else {
648 /* Allocate regular memory and store the image there temporarily. */
649 if (_mesa_is_format_compressed(texImage->TexFormat)) {
650 sizeInBytes = _mesa_format_image_size(texImage->TexFormat,
651 texImage->Width,
652 texImage->Height,
653 texImage->Depth);
654 dstRowStride = _mesa_format_row_stride(texImage->TexFormat, width);
655 assert(dims != 3);
656 }
657 else {
658 dstRowStride = postConvWidth * texelBytes;
659 sizeInBytes = depth * dstRowStride * postConvHeight;
660 }
661
662 texImage->Data = _mesa_align_malloc(sizeInBytes, 16);
663 }
664
665 if (!texImage->Data) {
666 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
667 return;
668 }
669
670 if (!pixels)
671 goto done;
672
673 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
674 width, height, depth, width * texelBytes, dstRowStride);
675
676 /* Copy data. Would like to know when it's ok for us to eg. use
677 * the blitter to copy. Or, use the hardware to do the format
678 * conversion and copy:
679 */
680 if (compressed_src) {
681 const GLuint srcImageStride = _mesa_format_row_stride(texImage->TexFormat, width);
682 if(dstRowStride == srcImageStride)
683 memcpy(texImage->Data, pixels, imageSize);
684 else
685 {
686 char *dst = texImage->Data;
687 const char *src = pixels;
688 GLuint i, bw, bh, lines;
689 _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
690 lines = (height + bh - 1) / bh;
691
692 for(i = 0; i < lines; ++i)
693 {
694 memcpy(dst, src, srcImageStride);
695 dst += dstRowStride;
696 src += srcImageStride;
697 }
698 }
699 }
700 else {
701 const GLuint srcImageStride =
702 _mesa_image_image_stride(unpack, width, height, format, type);
703 GLint i;
704 const GLubyte *src = (const GLubyte *) pixels;
705
706 for (i = 0; i < depth; i++) {
707 if (!_mesa_texstore(ctx, dims,
708 texImage->_BaseFormat,
709 texImage->TexFormat,
710 texImage->Data,
711 0, 0, 0, /* dstX/Y/Zoffset */
712 dstRowStride,
713 texImage->ImageOffsets,
714 width, height, 1,
715 format, type, src, unpack)) {
716 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
717 }
718
719 if (stImage->pt && i + 1 < depth) {
720 /* unmap this slice */
721 st_texture_image_unmap(ctx->st, stImage);
722 /* map next slice of 3D texture */
723 texImage->Data = st_texture_image_map(ctx->st, stImage, i + 1,
724 transfer_usage, 0, 0,
725 stImage->base.Width,
726 stImage->base.Height);
727 src += srcImageStride;
728 }
729 }
730 }
731
732 done:
733 _mesa_unmap_teximage_pbo(ctx, unpack);
734
735 if (stImage->pt && texImage->Data) {
736 st_texture_image_unmap(ctx->st, stImage);
737 texImage->Data = NULL;
738 }
739 }
740
741
742 static void
743 st_TexImage3D(GLcontext * ctx,
744 GLenum target, GLint level,
745 GLint internalFormat,
746 GLint width, GLint height, GLint depth,
747 GLint border,
748 GLenum format, GLenum type, const void *pixels,
749 const struct gl_pixelstore_attrib *unpack,
750 struct gl_texture_object *texObj,
751 struct gl_texture_image *texImage)
752 {
753 st_TexImage(ctx, 3, target, level, internalFormat, width, height, depth,
754 border, format, type, pixels, unpack, texObj, texImage,
755 0, GL_FALSE);
756 }
757
758
759 static void
760 st_TexImage2D(GLcontext * ctx,
761 GLenum target, GLint level,
762 GLint internalFormat,
763 GLint width, GLint height, 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, 2, target, level, internalFormat, width, height, 1, border,
770 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
771 }
772
773
774 static void
775 st_TexImage1D(GLcontext * ctx,
776 GLenum target, GLint level,
777 GLint internalFormat,
778 GLint width, GLint border,
779 GLenum format, GLenum type, const void *pixels,
780 const struct gl_pixelstore_attrib *unpack,
781 struct gl_texture_object *texObj,
782 struct gl_texture_image *texImage)
783 {
784 st_TexImage(ctx, 1, target, level, internalFormat, width, 1, 1, border,
785 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
786 }
787
788
789 static void
790 st_CompressedTexImage2D(GLcontext *ctx, GLenum target, GLint level,
791 GLint internalFormat,
792 GLint width, GLint height, GLint border,
793 GLsizei imageSize, const GLvoid *data,
794 struct gl_texture_object *texObj,
795 struct gl_texture_image *texImage)
796 {
797 st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border,
798 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE);
799 }
800
801
802
803 /**
804 * glGetTexImage() helper: decompress a compressed texture by rendering
805 * a textured quad. Store the results in the user's buffer.
806 */
807 static void
808 decompress_with_blit(GLcontext * ctx, GLenum target, GLint level,
809 GLenum format, GLenum type, GLvoid *pixels,
810 struct gl_texture_object *texObj,
811 struct gl_texture_image *texImage)
812 {
813 struct pipe_context *pipe = ctx->st->pipe;
814 struct pipe_screen *screen = pipe->screen;
815 struct st_texture_image *stImage = st_texture_image(texImage);
816 const GLuint width = texImage->Width;
817 const GLuint height = texImage->Height;
818 struct pipe_surface *dst_surface;
819 struct pipe_texture *dst_texture;
820 struct pipe_transfer *tex_xfer;
821
822 /* create temp / dest surface */
823 if (!util_create_rgba_surface(screen, width, height,
824 &dst_texture, &dst_surface)) {
825 _mesa_problem(ctx, "util_create_rgba_surface() failed "
826 "in decompress_with_blit()");
827 return;
828 }
829
830 /* blit/render/decompress */
831 util_blit_pixels_tex(ctx->st->blit,
832 stImage->pt, /* pipe_texture (src) */
833 0, 0, /* src x0, y0 */
834 width, height, /* src x1, y1 */
835 dst_surface, /* pipe_surface (dst) */
836 0, 0, /* dst x0, y0 */
837 width, height, /* dst x1, y1 */
838 0.0, /* z */
839 PIPE_TEX_MIPFILTER_NEAREST);
840
841 /* map the dst_surface so we can read from it */
842 tex_xfer = st_cond_flush_get_tex_transfer(st_context(ctx),
843 dst_texture, 0, 0, 0,
844 PIPE_TRANSFER_READ,
845 0, 0, width, height);
846
847 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
848
849 /* copy/pack data into user buffer */
850 if (st_equal_formats(stImage->pt->format, format, type)) {
851 /* memcpy */
852 const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format);
853 ubyte *map = pipe->transfer_map(pipe, tex_xfer);
854 GLuint row;
855 for (row = 0; row < height; row++) {
856 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
857 height, format, type, row, 0);
858 memcpy(dest, map, bytesPerRow);
859 map += tex_xfer->stride;
860 }
861 pipe->transfer_unmap(pipe, tex_xfer);
862 }
863 else {
864 /* format translation via floats */
865 GLuint row;
866 for (row = 0; row < height; row++) {
867 const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
868 GLfloat rgba[4 * MAX_WIDTH];
869 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
870 height, format, type, row, 0);
871
872 if (ST_DEBUG & DEBUG_FALLBACK)
873 debug_printf("%s: fallback format translation\n", __FUNCTION__);
874
875 /* get float[4] rgba row from surface */
876 pipe_get_tile_rgba(pipe, tex_xfer, 0, row, width, 1, rgba);
877
878 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
879 type, dest, &ctx->Pack, transferOps);
880 }
881 }
882
883 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
884
885 /* destroy the temp / dest surface */
886 util_destroy_rgba_surface(dst_texture, dst_surface);
887 }
888
889
890
891 /**
892 * Need to map texture image into memory before copying image data,
893 * then unmap it.
894 */
895 static void
896 st_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
897 GLenum format, GLenum type, GLvoid * pixels,
898 struct gl_texture_object *texObj,
899 struct gl_texture_image *texImage, GLboolean compressed_dst)
900 {
901 struct st_texture_image *stImage = st_texture_image(texImage);
902 const GLuint dstImageStride =
903 _mesa_image_image_stride(&ctx->Pack, texImage->Width, texImage->Height,
904 format, type);
905 GLuint depth, i;
906 GLubyte *dest;
907
908 if (stImage->pt &&
909 util_format_is_compressed(stImage->pt->format) &&
910 !compressed_dst) {
911 /* Need to decompress the texture.
912 * We'll do this by rendering a textured quad.
913 * Note that we only expect RGBA formats (no Z/depth formats).
914 */
915 decompress_with_blit(ctx, target, level, format, type, pixels,
916 texObj, texImage);
917 return;
918 }
919
920 /* Map */
921 if (stImage->pt) {
922 /* Image is stored in hardware format in a buffer managed by the
923 * kernel. Need to explicitly map and unmap it.
924 */
925 unsigned face = _mesa_tex_target_to_face(target);
926
927 st_teximage_flush_before_map(ctx->st, stImage->pt, face, level,
928 PIPE_TRANSFER_READ);
929
930 texImage->Data = st_texture_image_map(ctx->st, stImage, 0,
931 PIPE_TRANSFER_READ, 0, 0,
932 stImage->base.Width,
933 stImage->base.Height);
934 texImage->RowStride = stImage->transfer->stride / util_format_get_blocksize(stImage->pt->format);
935 }
936 else {
937 /* Otherwise, the image should actually be stored in
938 * texImage->Data. This is pretty confusing for
939 * everybody, I'd much prefer to separate the two functions of
940 * texImage->Data - storage for texture images in main memory
941 * and access (ie mappings) of images. In other words, we'd
942 * create a new texImage->Map field and leave Data simply for
943 * storage.
944 */
945 assert(texImage->Data);
946 }
947
948 depth = texImage->Depth;
949 texImage->Depth = 1;
950
951 dest = (GLubyte *) pixels;
952
953 for (i = 0; i < depth; i++) {
954 if (compressed_dst) {
955 _mesa_get_compressed_teximage(ctx, target, level, dest,
956 texObj, texImage);
957 }
958 else {
959 _mesa_get_teximage(ctx, target, level, format, type, dest,
960 texObj, texImage);
961 }
962
963 if (stImage->pt && i + 1 < depth) {
964 /* unmap this slice */
965 st_texture_image_unmap(ctx->st, stImage);
966 /* map next slice of 3D texture */
967 texImage->Data = st_texture_image_map(ctx->st, stImage, i + 1,
968 PIPE_TRANSFER_READ, 0, 0,
969 stImage->base.Width,
970 stImage->base.Height);
971 dest += dstImageStride;
972 }
973 }
974
975 texImage->Depth = depth;
976
977 /* Unmap */
978 if (stImage->pt) {
979 st_texture_image_unmap(ctx->st, stImage);
980 texImage->Data = NULL;
981 }
982 }
983
984
985 static void
986 st_GetTexImage(GLcontext * ctx, GLenum target, GLint level,
987 GLenum format, GLenum type, GLvoid * pixels,
988 struct gl_texture_object *texObj,
989 struct gl_texture_image *texImage)
990 {
991 st_get_tex_image(ctx, target, level, format, type, pixels, texObj, texImage,
992 GL_FALSE);
993 }
994
995
996 static void
997 st_GetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level,
998 GLvoid *pixels,
999 struct gl_texture_object *texObj,
1000 struct gl_texture_image *texImage)
1001 {
1002 st_get_tex_image(ctx, target, level, 0, 0, pixels, texObj, texImage,
1003 GL_TRUE);
1004 }
1005
1006
1007
1008 static void
1009 st_TexSubimage(GLcontext *ctx, GLint dims, GLenum target, GLint level,
1010 GLint xoffset, GLint yoffset, GLint zoffset,
1011 GLint width, GLint height, GLint depth,
1012 GLenum format, GLenum type, const void *pixels,
1013 const struct gl_pixelstore_attrib *packing,
1014 struct gl_texture_object *texObj,
1015 struct gl_texture_image *texImage)
1016 {
1017 struct pipe_screen *screen = ctx->st->pipe->screen;
1018 struct st_texture_image *stImage = st_texture_image(texImage);
1019 GLuint dstRowStride;
1020 const GLuint srcImageStride =
1021 _mesa_image_image_stride(packing, width, height, format, type);
1022 GLint i;
1023 const GLubyte *src;
1024 /* init to silence warning only: */
1025 enum pipe_transfer_usage transfer_usage = PIPE_TRANSFER_WRITE;
1026
1027 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
1028 _mesa_lookup_enum_by_nr(target),
1029 level, xoffset, yoffset, width, height);
1030
1031 pixels =
1032 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format,
1033 type, pixels, packing, "glTexSubImage2D");
1034 if (!pixels)
1035 return;
1036
1037 /* See if we can do texture compression with a blit/render.
1038 */
1039 if (!ctx->Mesa_DXTn &&
1040 _mesa_is_format_compressed(texImage->TexFormat) &&
1041 screen->is_format_supported(screen,
1042 stImage->pt->format,
1043 stImage->pt->target,
1044 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
1045 if (compress_with_blit(ctx, target, level,
1046 xoffset, yoffset, zoffset,
1047 width, height, depth,
1048 format, type, pixels, packing, texImage)) {
1049 goto done;
1050 }
1051 }
1052
1053 /* Map buffer if necessary. Need to lock to prevent other contexts
1054 * from uploading the buffer under us.
1055 */
1056 if (stImage->pt) {
1057 unsigned face = _mesa_tex_target_to_face(target);
1058
1059 if (format == GL_DEPTH_COMPONENT &&
1060 util_format_is_depth_and_stencil(stImage->pt->format))
1061 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1062 else
1063 transfer_usage = PIPE_TRANSFER_WRITE;
1064
1065 st_teximage_flush_before_map(ctx->st, stImage->pt, face, level,
1066 transfer_usage);
1067 texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset,
1068 transfer_usage,
1069 xoffset, yoffset,
1070 width, height);
1071 }
1072
1073 if (!texImage->Data) {
1074 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1075 goto done;
1076 }
1077
1078 src = (const GLubyte *) pixels;
1079 dstRowStride = stImage->transfer->stride;
1080
1081 for (i = 0; i < depth; i++) {
1082 if (!_mesa_texstore(ctx, dims, texImage->_BaseFormat,
1083 texImage->TexFormat,
1084 texImage->Data,
1085 0, 0, 0,
1086 dstRowStride,
1087 texImage->ImageOffsets,
1088 width, height, 1,
1089 format, type, src, packing)) {
1090 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1091 }
1092
1093 if (stImage->pt && i + 1 < depth) {
1094 /* unmap this slice */
1095 st_texture_image_unmap(ctx->st, stImage);
1096 /* map next slice of 3D texture */
1097 texImage->Data = st_texture_image_map(ctx->st, stImage,
1098 zoffset + i + 1,
1099 transfer_usage,
1100 xoffset, yoffset,
1101 width, height);
1102 src += srcImageStride;
1103 }
1104 }
1105
1106 done:
1107 _mesa_unmap_teximage_pbo(ctx, packing);
1108
1109 if (stImage->pt && texImage->Data) {
1110 st_texture_image_unmap(ctx->st, stImage);
1111 texImage->Data = NULL;
1112 }
1113 }
1114
1115
1116
1117 static void
1118 st_TexSubImage3D(GLcontext *ctx, GLenum target, GLint level,
1119 GLint xoffset, GLint yoffset, GLint zoffset,
1120 GLsizei width, GLsizei height, GLsizei depth,
1121 GLenum format, GLenum type, const GLvoid *pixels,
1122 const struct gl_pixelstore_attrib *packing,
1123 struct gl_texture_object *texObj,
1124 struct gl_texture_image *texImage)
1125 {
1126 st_TexSubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
1127 width, height, depth, format, type,
1128 pixels, packing, texObj, texImage);
1129 }
1130
1131
1132 static void
1133 st_TexSubImage2D(GLcontext *ctx, GLenum target, GLint level,
1134 GLint xoffset, GLint yoffset,
1135 GLsizei width, GLsizei height,
1136 GLenum format, GLenum type, const GLvoid * pixels,
1137 const struct gl_pixelstore_attrib *packing,
1138 struct gl_texture_object *texObj,
1139 struct gl_texture_image *texImage)
1140 {
1141 st_TexSubimage(ctx, 2, target, level, xoffset, yoffset, 0,
1142 width, height, 1, format, type,
1143 pixels, packing, texObj, texImage);
1144 }
1145
1146
1147 static void
1148 st_TexSubImage1D(GLcontext *ctx, GLenum target, GLint level,
1149 GLint xoffset, GLsizei width, GLenum format, GLenum type,
1150 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, 1, target, level, xoffset, 0, 0, width, 1, 1,
1156 format, type, pixels, packing, texObj, texImage);
1157 }
1158
1159
1160 static void
1161 st_CompressedTexSubImage1D(GLcontext *ctx, GLenum target, GLint level,
1162 GLint xoffset, GLsizei width,
1163 GLenum format,
1164 GLsizei imageSize, const GLvoid *data,
1165 struct gl_texture_object *texObj,
1166 struct gl_texture_image *texImage)
1167 {
1168 assert(0);
1169 }
1170
1171
1172 static void
1173 st_CompressedTexSubImage2D(GLcontext *ctx, GLenum target, GLint level,
1174 GLint xoffset, GLint yoffset,
1175 GLsizei width, GLint height,
1176 GLenum format,
1177 GLsizei imageSize, const GLvoid *data,
1178 struct gl_texture_object *texObj,
1179 struct gl_texture_image *texImage)
1180 {
1181 struct st_texture_image *stImage = st_texture_image(texImage);
1182 int srcBlockStride;
1183 int dstBlockStride;
1184 int y;
1185 enum pipe_format pformat= stImage->pt->format;
1186
1187 if (stImage->pt) {
1188 unsigned face = _mesa_tex_target_to_face(target);
1189
1190 st_teximage_flush_before_map(ctx->st, stImage->pt, face, level,
1191 PIPE_TRANSFER_WRITE);
1192 texImage->Data = st_texture_image_map(ctx->st, stImage, 0,
1193 PIPE_TRANSFER_WRITE,
1194 xoffset, yoffset,
1195 width, height);
1196
1197 srcBlockStride = util_format_get_stride(pformat, width);
1198 dstBlockStride = stImage->transfer->stride;
1199 } else {
1200 assert(stImage->pt);
1201 /* TODO find good values for block and strides */
1202 /* TODO also adjust texImage->data for yoffset/xoffset */
1203 return;
1204 }
1205
1206 if (!texImage->Data) {
1207 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexSubImage");
1208 return;
1209 }
1210
1211 assert(xoffset % util_format_get_blockwidth(pformat) == 0);
1212 assert(yoffset % util_format_get_blockheight(pformat) == 0);
1213 assert(width % util_format_get_blockwidth(pformat) == 0);
1214 assert(height % util_format_get_blockheight(pformat) == 0);
1215
1216 for (y = 0; y < height; y += util_format_get_blockheight(pformat)) {
1217 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1218 const char *src = (const char*)data + srcBlockStride * util_format_get_nblocksy(pformat, y);
1219 char *dst = (char*)texImage->Data + dstBlockStride * util_format_get_nblocksy(pformat, y);
1220 memcpy(dst, src, util_format_get_stride(pformat, width));
1221 }
1222
1223 if (stImage->pt) {
1224 st_texture_image_unmap(ctx->st, stImage);
1225 texImage->Data = NULL;
1226 }
1227 }
1228
1229
1230 static void
1231 st_CompressedTexSubImage3D(GLcontext *ctx, GLenum target, GLint level,
1232 GLint xoffset, GLint yoffset, GLint zoffset,
1233 GLsizei width, GLint height, GLint depth,
1234 GLenum format,
1235 GLsizei imageSize, const GLvoid *data,
1236 struct gl_texture_object *texObj,
1237 struct gl_texture_image *texImage)
1238 {
1239 assert(0);
1240 }
1241
1242
1243
1244 /**
1245 * Do a CopyTexSubImage operation using a read transfer from the source,
1246 * a write transfer to the destination and get_tile()/put_tile() to access
1247 * the pixels/texels.
1248 *
1249 * Note: srcY=0=TOP of renderbuffer
1250 */
1251 static void
1252 fallback_copy_texsubimage(GLcontext *ctx, GLenum target, GLint level,
1253 struct st_renderbuffer *strb,
1254 struct st_texture_image *stImage,
1255 GLenum baseFormat,
1256 GLint destX, GLint destY, GLint destZ,
1257 GLint srcX, GLint srcY,
1258 GLsizei width, GLsizei height)
1259 {
1260 struct pipe_context *pipe = ctx->st->pipe;
1261 struct pipe_transfer *src_trans;
1262 GLvoid *texDest;
1263 enum pipe_transfer_usage transfer_usage;
1264
1265 if (ST_DEBUG & DEBUG_FALLBACK)
1266 debug_printf("%s: fallback processing\n", __FUNCTION__);
1267
1268 assert(width <= MAX_WIDTH);
1269
1270 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1271 srcY = strb->Base.Height - srcY - height;
1272 }
1273
1274 src_trans = st_cond_flush_get_tex_transfer( st_context(ctx),
1275 strb->texture,
1276 0, 0, 0,
1277 PIPE_TRANSFER_READ,
1278 srcX, srcY,
1279 width, height);
1280
1281 if ((baseFormat == GL_DEPTH_COMPONENT ||
1282 baseFormat == GL_DEPTH_STENCIL) &&
1283 util_format_is_depth_and_stencil(stImage->pt->format))
1284 transfer_usage = PIPE_TRANSFER_READ_WRITE;
1285 else
1286 transfer_usage = PIPE_TRANSFER_WRITE;
1287
1288 st_teximage_flush_before_map(ctx->st, stImage->pt, 0, 0,
1289 transfer_usage);
1290
1291 texDest = st_texture_image_map(ctx->st, stImage, 0, transfer_usage,
1292 destX, destY, width, height);
1293
1294 if (baseFormat == GL_DEPTH_COMPONENT ||
1295 baseFormat == GL_DEPTH_STENCIL) {
1296 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
1297 ctx->Pixel.DepthBias != 0.0F);
1298 GLint row, yStep;
1299
1300 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1301 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1302 srcY = height - 1;
1303 yStep = -1;
1304 }
1305 else {
1306 srcY = 0;
1307 yStep = 1;
1308 }
1309
1310 /* To avoid a large temp memory allocation, do copy row by row */
1311 for (row = 0; row < height; row++, srcY += yStep) {
1312 uint data[MAX_WIDTH];
1313 pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
1314 if (scaleOrBias) {
1315 _mesa_scale_and_bias_depth_uint(ctx, width, data);
1316 }
1317 pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
1318 }
1319 }
1320 else {
1321 /* RGBA format */
1322 GLfloat *tempSrc =
1323 (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
1324
1325 if (tempSrc && texDest) {
1326 const GLint dims = 2;
1327 const GLint dstRowStride = stImage->transfer->stride;
1328 struct gl_texture_image *texImage = &stImage->base;
1329 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
1330
1331 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1332 unpack.Invert = GL_TRUE;
1333 }
1334
1335 /* get float/RGBA image from framebuffer */
1336 /* XXX this usually involves a lot of int/float conversion.
1337 * try to avoid that someday.
1338 */
1339 pipe_get_tile_rgba(pipe, src_trans, 0, 0, width, height, tempSrc);
1340
1341 /* Store into texture memory.
1342 * Note that this does some special things such as pixel transfer
1343 * ops and format conversion. In particular, if the dest tex format
1344 * is actually RGBA but the user created the texture as GL_RGB we
1345 * need to fill-in/override the alpha channel with 1.0.
1346 */
1347 _mesa_texstore(ctx, dims,
1348 texImage->_BaseFormat,
1349 texImage->TexFormat,
1350 texDest,
1351 0, 0, 0,
1352 dstRowStride,
1353 texImage->ImageOffsets,
1354 width, height, 1,
1355 GL_RGBA, GL_FLOAT, tempSrc, /* src */
1356 &unpack);
1357 }
1358 else {
1359 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1360 }
1361
1362 if (tempSrc)
1363 free(tempSrc);
1364 }
1365
1366 st_texture_image_unmap(ctx->st, stImage);
1367 pipe->tex_transfer_destroy(pipe, src_trans);
1368 }
1369
1370
1371
1372 /**
1373 * If the format of the src renderbuffer and the format of the dest
1374 * texture are compatible (in terms of blitting), return a TGSI writemask
1375 * to be used during the blit.
1376 * If the src/dest are incompatible, return 0.
1377 */
1378 static unsigned
1379 compatible_src_dst_formats(GLcontext *ctx,
1380 const struct gl_renderbuffer *src,
1381 const struct gl_texture_image *dst)
1382 {
1383 /* Get logical base formats for the src and dest.
1384 * That is, use the user-requested formats and not the actual, device-
1385 * chosen formats.
1386 * For example, the user may have requested an A8 texture but the
1387 * driver may actually be using an RGBA texture format. When we
1388 * copy/blit to that texture, we only want to copy the Alpha channel
1389 * and not the RGB channels.
1390 *
1391 * Similarly, when the src FBO was created an RGB format may have been
1392 * requested but the driver actually chose an RGBA format. In that case,
1393 * we don't want to copy the undefined Alpha channel to the dest texture
1394 * (it should be 1.0).
1395 */
1396 const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
1397 const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
1398
1399 /**
1400 * XXX when we have red-only and red/green renderbuffers we'll need
1401 * to add more cases here (or implement a general-purpose routine that
1402 * queries the existance of the R,G,B,A channels in the src and dest).
1403 */
1404 if (srcFormat == dstFormat) {
1405 /* This is the same as matching_base_formats, which should
1406 * always pass, as it did previously.
1407 */
1408 return TGSI_WRITEMASK_XYZW;
1409 }
1410 else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
1411 /* Make sure that A in the dest is 1. The actual src format
1412 * may be RGBA and have undefined A values.
1413 */
1414 return TGSI_WRITEMASK_XYZ;
1415 }
1416 else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
1417 /* Make sure that A in the dest is 1. The actual dst format
1418 * may be RGBA and will need A=1 to provide proper alpha values
1419 * when sampled later.
1420 */
1421 return TGSI_WRITEMASK_XYZ;
1422 }
1423 else {
1424 if (ST_DEBUG & DEBUG_FALLBACK)
1425 debug_printf("%s failed for src %s, dst %s\n",
1426 __FUNCTION__,
1427 _mesa_lookup_enum_by_nr(srcFormat),
1428 _mesa_lookup_enum_by_nr(dstFormat));
1429
1430 /* Otherwise fail.
1431 */
1432 return 0;
1433 }
1434 }
1435
1436
1437
1438 /**
1439 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1440 * Note that the region to copy has already been clipped so we know we
1441 * won't read from outside the source renderbuffer's bounds.
1442 *
1443 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1444 */
1445 static void
1446 st_copy_texsubimage(GLcontext *ctx,
1447 GLenum target, GLint level,
1448 GLint destX, GLint destY, GLint destZ,
1449 GLint srcX, GLint srcY,
1450 GLsizei width, GLsizei height)
1451 {
1452 struct gl_texture_unit *texUnit =
1453 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1454 struct gl_texture_object *texObj =
1455 _mesa_select_tex_object(ctx, texUnit, target);
1456 struct gl_texture_image *texImage =
1457 _mesa_select_tex_image(ctx, texObj, target, level);
1458 struct st_texture_image *stImage = st_texture_image(texImage);
1459 const GLenum texBaseFormat = texImage->_BaseFormat;
1460 struct gl_framebuffer *fb = ctx->ReadBuffer;
1461 struct st_renderbuffer *strb;
1462 struct pipe_context *pipe = ctx->st->pipe;
1463 struct pipe_screen *screen = pipe->screen;
1464 enum pipe_format dest_format, src_format;
1465 GLboolean use_fallback = GL_TRUE;
1466 GLboolean matching_base_formats;
1467 GLuint format_writemask;
1468 struct pipe_surface *dest_surface = NULL;
1469 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1470
1471 /* any rendering in progress must flushed before we grab the fb image */
1472 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
1473
1474 /* make sure finalize_textures has been called?
1475 */
1476 if (0) st_validate_state(ctx->st);
1477
1478 /* determine if copying depth or color data */
1479 if (texBaseFormat == GL_DEPTH_COMPONENT ||
1480 texBaseFormat == GL_DEPTH_STENCIL) {
1481 strb = st_renderbuffer(fb->_DepthBuffer);
1482 }
1483 else {
1484 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1485 strb = st_renderbuffer(fb->_ColorReadBuffer);
1486 }
1487
1488 if (!strb || !strb->surface || !stImage->pt) {
1489 debug_printf("%s: null strb or stImage\n", __FUNCTION__);
1490 return;
1491 }
1492
1493 if (srcX < 0) {
1494 width -= -srcX;
1495 destX += -srcX;
1496 srcX = 0;
1497 }
1498
1499 if (srcY < 0) {
1500 height -= -srcY;
1501 destY += -srcY;
1502 srcY = 0;
1503 }
1504
1505 if (destX < 0) {
1506 width -= -destX;
1507 srcX += -destX;
1508 destX = 0;
1509 }
1510
1511 if (destY < 0) {
1512 height -= -destY;
1513 srcY += -destY;
1514 destY = 0;
1515 }
1516
1517 if (width < 0 || height < 0)
1518 return;
1519
1520
1521 assert(strb);
1522 assert(strb->surface);
1523 assert(stImage->pt);
1524
1525 src_format = strb->surface->format;
1526 dest_format = stImage->pt->format;
1527
1528 /*
1529 * Determine if the src framebuffer and dest texture have the same
1530 * base format. We need this to detect a case such as the framebuffer
1531 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1532 * texture format stores RGBA we need to set A=1 (overriding the
1533 * framebuffer's alpha values). We can't do that with the blit or
1534 * textured-quad paths.
1535 */
1536 matching_base_formats =
1537 (_mesa_get_format_base_format(strb->Base.Format) ==
1538 _mesa_get_format_base_format(texImage->TexFormat));
1539 format_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
1540
1541 if (ctx->_ImageTransferState == 0x0) {
1542
1543 if (pipe->surface_copy &&
1544 matching_base_formats &&
1545 src_format == dest_format &&
1546 !do_flip)
1547 {
1548 /* use surface_copy() / blit */
1549
1550 dest_surface = screen->get_tex_surface(screen, stImage->pt,
1551 stImage->face, stImage->level,
1552 destZ,
1553 PIPE_BUFFER_USAGE_GPU_WRITE);
1554
1555 /* for surface_copy(), y=0=top, always */
1556 pipe->surface_copy(pipe,
1557 /* dest */
1558 dest_surface,
1559 destX, destY,
1560 /* src */
1561 strb->surface,
1562 srcX, srcY,
1563 /* size */
1564 width, height);
1565 use_fallback = GL_FALSE;
1566 }
1567 else if (format_writemask &&
1568 texBaseFormat != GL_DEPTH_COMPONENT &&
1569 texBaseFormat != GL_DEPTH_STENCIL &&
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 pipe_texture_reference(&stImage->pt, NULL);
1724 }
1725 else if (stImage->base.Data) {
1726 /* More straightforward upload.
1727 */
1728 st_teximage_flush_before_map(st, stObj->pt, stImage->face, dstLevel,
1729 PIPE_TRANSFER_WRITE);
1730
1731 st_texture_image_data(st,
1732 stObj->pt,
1733 stImage->face,
1734 dstLevel,
1735 stImage->base.Data,
1736 stImage->base.RowStride *
1737 util_format_get_blocksize(stObj->pt->format),
1738 stImage->base.RowStride *
1739 stImage->base.Height *
1740 util_format_get_blocksize(stObj->pt->format));
1741 _mesa_align_free(stImage->base.Data);
1742 stImage->base.Data = NULL;
1743 }
1744
1745 pipe_texture_reference(&stImage->pt, stObj->pt);
1746 }
1747
1748
1749 /**
1750 * Called during state validation. When this function is finished,
1751 * the texture object should be ready for rendering.
1752 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1753 */
1754 GLboolean
1755 st_finalize_texture(GLcontext *ctx,
1756 struct pipe_context *pipe,
1757 struct gl_texture_object *tObj,
1758 GLboolean *needFlush)
1759 {
1760 struct st_texture_object *stObj = st_texture_object(tObj);
1761 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1762 GLuint blockSize, face;
1763 struct st_texture_image *firstImage;
1764
1765 *needFlush = GL_FALSE;
1766
1767 if (stObj->base._Complete) {
1768 /* The texture is complete and we know exactly how many mipmap levels
1769 * are present/needed. This is conditional because we may be called
1770 * from the st_generate_mipmap() function when the texture object is
1771 * incomplete. In that case, we'll have set stObj->lastLevel before
1772 * we get here.
1773 */
1774 if (stObj->base.MinFilter == GL_LINEAR ||
1775 stObj->base.MinFilter == GL_NEAREST)
1776 stObj->lastLevel = stObj->base.BaseLevel;
1777 else
1778 stObj->lastLevel = stObj->base._MaxLevel - stObj->base.BaseLevel;
1779 }
1780
1781 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1782
1783 /* If both firstImage and stObj point to a texture which can contain
1784 * all active images, favour firstImage. Note that because of the
1785 * completeness requirement, we know that the image dimensions
1786 * will match.
1787 */
1788 if (firstImage->pt &&
1789 firstImage->pt != stObj->pt &&
1790 firstImage->pt->last_level >= stObj->lastLevel) {
1791 pipe_texture_reference(&stObj->pt, firstImage->pt);
1792 }
1793
1794 /* bytes per pixel block (blocks are usually 1x1) */
1795 blockSize = _mesa_get_format_bytes(firstImage->base.TexFormat);
1796
1797 /* If we already have a gallium texture, check that it matches the texture
1798 * object's format, target, size, num_levels, etc.
1799 */
1800 if (stObj->pt) {
1801 const enum pipe_format fmt =
1802 st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1803 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1804 stObj->pt->format != fmt ||
1805 stObj->pt->last_level < stObj->lastLevel ||
1806 stObj->pt->width0 != firstImage->base.Width2 ||
1807 stObj->pt->height0 != firstImage->base.Height2 ||
1808 stObj->pt->depth0 != firstImage->base.Depth2)
1809 {
1810 pipe_texture_reference(&stObj->pt, NULL);
1811 ctx->st->dirty.st |= ST_NEW_FRAMEBUFFER;
1812 }
1813 }
1814
1815 /* May need to create a new gallium texture:
1816 */
1817 if (!stObj->pt) {
1818 const enum pipe_format fmt =
1819 st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1820 GLuint usage = default_usage(fmt);
1821
1822 stObj->pt = st_texture_create(ctx->st,
1823 gl_target_to_pipe(stObj->base.Target),
1824 fmt,
1825 stObj->lastLevel,
1826 firstImage->base.Width2,
1827 firstImage->base.Height2,
1828 firstImage->base.Depth2,
1829 usage);
1830
1831 if (!stObj->pt) {
1832 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1833 return GL_FALSE;
1834 }
1835 }
1836
1837 /* Pull in any images not in the object's texture:
1838 */
1839 for (face = 0; face < nr_faces; face++) {
1840 GLuint level;
1841 for (level = 0; level <= stObj->lastLevel; level++) {
1842 struct st_texture_image *stImage =
1843 st_texture_image(stObj->base.Image[face][stObj->base.BaseLevel + level]);
1844
1845 /* Need to import images in main memory or held in other textures.
1846 */
1847 if (stImage && stObj->pt != stImage->pt) {
1848 copy_image_data_to_texture(ctx->st, stObj, level, stImage);
1849 *needFlush = GL_TRUE;
1850 }
1851 }
1852 }
1853
1854 return GL_TRUE;
1855 }
1856
1857
1858 /**
1859 * Returns pointer to a default/dummy texture.
1860 * This is typically used when the current shader has tex/sample instructions
1861 * but the user has not provided a (any) texture(s).
1862 */
1863 struct gl_texture_object *
1864 st_get_default_texture(struct st_context *st)
1865 {
1866 if (!st->default_texture) {
1867 static const GLenum target = GL_TEXTURE_2D;
1868 GLubyte pixels[16][16][4];
1869 struct gl_texture_object *texObj;
1870 struct gl_texture_image *texImg;
1871 GLuint i, j;
1872
1873 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1874 * when attempting to sample incomplete textures.
1875 */
1876 for (i = 0; i < 16; i++) {
1877 for (j = 0; j < 16; j++) {
1878 pixels[i][j][0] = 0;
1879 pixels[i][j][1] = 0;
1880 pixels[i][j][2] = 0;
1881 pixels[i][j][3] = 255;
1882 }
1883 }
1884
1885 texObj = st->ctx->Driver.NewTextureObject(st->ctx, 0, target);
1886
1887 texImg = _mesa_get_tex_image(st->ctx, texObj, target, 0);
1888
1889 _mesa_init_teximage_fields(st->ctx, target, texImg,
1890 16, 16, 1, 0, /* w, h, d, border */
1891 GL_RGBA);
1892
1893 st_TexImage(st->ctx, 2, target,
1894 0, GL_RGBA, /* level, intformat */
1895 16, 16, 1, 0, /* w, h, d, border */
1896 GL_RGBA, GL_UNSIGNED_BYTE, pixels,
1897 &st->ctx->DefaultPacking,
1898 texObj, texImg,
1899 0, 0);
1900
1901 texObj->MinFilter = GL_NEAREST;
1902 texObj->MagFilter = GL_NEAREST;
1903 texObj->_Complete = GL_TRUE;
1904
1905 st->default_texture = texObj;
1906 }
1907 return st->default_texture;
1908 }
1909
1910
1911 void
1912 st_init_texture_functions(struct dd_function_table *functions)
1913 {
1914 functions->ChooseTextureFormat = st_ChooseTextureFormat;
1915 functions->TexImage1D = st_TexImage1D;
1916 functions->TexImage2D = st_TexImage2D;
1917 functions->TexImage3D = st_TexImage3D;
1918 functions->TexSubImage1D = st_TexSubImage1D;
1919 functions->TexSubImage2D = st_TexSubImage2D;
1920 functions->TexSubImage3D = st_TexSubImage3D;
1921 functions->CompressedTexSubImage1D = st_CompressedTexSubImage1D;
1922 functions->CompressedTexSubImage2D = st_CompressedTexSubImage2D;
1923 functions->CompressedTexSubImage3D = st_CompressedTexSubImage3D;
1924 functions->CopyTexImage1D = st_CopyTexImage1D;
1925 functions->CopyTexImage2D = st_CopyTexImage2D;
1926 functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
1927 functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
1928 functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
1929 functions->GenerateMipmap = st_generate_mipmap;
1930
1931 functions->GetTexImage = st_GetTexImage;
1932
1933 /* compressed texture functions */
1934 functions->CompressedTexImage2D = st_CompressedTexImage2D;
1935 functions->GetCompressedTexImage = st_GetCompressedTexImage;
1936
1937 functions->NewTextureObject = st_NewTextureObject;
1938 functions->NewTextureImage = st_NewTextureImage;
1939 functions->DeleteTexture = st_DeleteTextureObject;
1940 functions->FreeTexImageData = st_FreeTextureImageData;
1941 functions->UpdateTexturePalette = 0;
1942
1943 functions->TextureMemCpy = do_memcpy;
1944
1945 /* XXX Temporary until we can query pipe's texture sizes */
1946 functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1947 }