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