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