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