intel / DRI2: When available, use DRI2GetBuffersWithFormat
[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 GLuint face = _mesa_tex_target_to_face(target);
422 const struct gl_texture_format *mesa_format;
423 struct pipe_texture templ;
424 struct pipe_texture *src_tex;
425 struct pipe_surface *dst_surface;
426 struct pipe_transfer *tex_xfer;
427 void *map;
428
429
430 if (!stImage->pt) {
431 /* XXX: Can this happen? Should we assert? */
432 return GL_FALSE;
433 }
434
435 /* get destination surface (in the compressed texture) */
436 dst_surface = screen->get_tex_surface(screen, stImage->pt,
437 stImage->face, stImage->level, 0,
438 PIPE_BUFFER_USAGE_GPU_WRITE);
439 if (!dst_surface) {
440 /* can't render into this format (or other problem) */
441 return GL_FALSE;
442 }
443
444 /* Choose format for the temporary RGBA texture image.
445 */
446 mesa_format = st_ChooseTextureFormat(ctx, GL_RGBA, format, type);
447 assert(mesa_format);
448 if (!mesa_format)
449 return GL_FALSE;
450
451 /* Create the temporary source texture
452 */
453 memset(&templ, 0, sizeof(templ));
454 templ.target = PIPE_TEXTURE_2D;
455 templ.format = st_mesa_format_to_pipe_format(mesa_format->MesaFormat);
456 pf_get_block(templ.format, &templ.block);
457 templ.width[0] = width;
458 templ.height[0] = height;
459 templ.depth[0] = 1;
460 templ.last_level = 0;
461 templ.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER;
462 src_tex = screen->texture_create(screen, &templ);
463
464 if (!src_tex)
465 return GL_FALSE;
466
467 /* Put user's tex data into the temporary texture
468 */
469 tex_xfer = st_cond_flush_get_tex_transfer(st_context(ctx), src_tex,
470 face, level, 0,
471 PIPE_TRANSFER_WRITE,
472 0, 0, width, height); /* x, y, w, h */
473 map = screen->transfer_map(screen, tex_xfer);
474
475 mesa_format->StoreImage(ctx, 2, GL_RGBA, mesa_format,
476 map, /* dest ptr */
477 0, 0, 0, /* dest x/y/z offset */
478 tex_xfer->stride, /* dest row stride (bytes) */
479 dstImageOffsets, /* image offsets (for 3D only) */
480 width, height, 1, /* size */
481 format, type, /* source format/type */
482 pixels, /* source data */
483 unpack); /* source data packing */
484
485 screen->transfer_unmap(screen, tex_xfer);
486 screen->tex_transfer_destroy(tex_xfer);
487
488 /* copy / compress image */
489 util_blit_pixels_tex(ctx->st->blit,
490 src_tex, /* pipe_texture (src) */
491 0, 0, /* src x0, y0 */
492 width, height, /* src x1, y1 */
493 dst_surface, /* pipe_surface (dst) */
494 xoffset, yoffset, /* dst x0, y0 */
495 xoffset + width, /* dst x1 */
496 yoffset + height, /* dst y1 */
497 0.0, /* z */
498 PIPE_TEX_MIPFILTER_NEAREST);
499
500 pipe_surface_reference(&dst_surface, NULL);
501 pipe_texture_reference(&src_tex, NULL);
502
503 return GL_TRUE;
504 }
505
506
507 /**
508 * Do glTexImage1/2/3D().
509 */
510 static void
511 st_TexImage(GLcontext * ctx,
512 GLint dims,
513 GLenum target, GLint level,
514 GLint internalFormat,
515 GLint width, GLint height, GLint depth,
516 GLint border,
517 GLenum format, GLenum type, const void *pixels,
518 const struct gl_pixelstore_attrib *unpack,
519 struct gl_texture_object *texObj,
520 struct gl_texture_image *texImage,
521 GLsizei imageSize, GLboolean compressed_src)
522 {
523 struct pipe_screen *screen = ctx->st->pipe->screen;
524 struct st_texture_object *stObj = st_texture_object(texObj);
525 struct st_texture_image *stImage = st_texture_image(texImage);
526 GLint postConvWidth, postConvHeight;
527 GLint texelBytes, sizeInBytes;
528 GLuint dstRowStride;
529 struct gl_pixelstore_attrib unpackNB;
530
531 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
532 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
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 /* choose the texture format */
559 texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat,
560 format, type);
561
562 _mesa_set_fetch_functions(texImage, dims);
563
564 if (texImage->TexFormat->TexelBytes == 0) {
565 /* must be a compressed format */
566 texelBytes = 0;
567 texImage->IsCompressed = GL_TRUE;
568 texImage->CompressedSize =
569 ctx->Driver.CompressedTextureSize(ctx, texImage->Width,
570 texImage->Height, texImage->Depth,
571 texImage->TexFormat->MesaFormat);
572 }
573 else {
574 texelBytes = texImage->TexFormat->TexelBytes;
575
576 /* Minimum pitch of 32 bytes */
577 if (postConvWidth * texelBytes < 32) {
578 postConvWidth = 32 / texelBytes;
579 texImage->RowStride = postConvWidth;
580 }
581
582 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
583 /*assert(texImage->RowStride == postConvWidth);*/
584 }
585
586 /* Release the reference to a potentially orphaned buffer.
587 * Release any old malloced memory.
588 */
589 if (stImage->pt) {
590 pipe_texture_reference(&stImage->pt, NULL);
591 assert(!texImage->Data);
592 }
593 else if (texImage->Data) {
594 _mesa_align_free(texImage->Data);
595 }
596
597 if (width == 0 || height == 0 || depth == 0) {
598 /* stop after freeing old image */
599 return;
600 }
601
602 /* If this is the only mipmap level in the texture, could call
603 * bmBufferData with NULL data to free the old block and avoid
604 * waiting on any outstanding fences.
605 */
606 if (stObj->pt) {
607 if (stObj->teximage_realloc ||
608 level > (GLint) stObj->pt->last_level ||
609 (stObj->pt->last_level == level &&
610 stObj->pt->target != PIPE_TEXTURE_CUBE &&
611 !st_texture_match_image(stObj->pt, &stImage->base,
612 stImage->face, stImage->level))) {
613 DBG("release it\n");
614 pipe_texture_reference(&stObj->pt, NULL);
615 assert(!stObj->pt);
616 stObj->teximage_realloc = FALSE;
617 }
618 }
619
620 if (!stObj->pt) {
621 guess_and_alloc_texture(ctx->st, stObj, stImage);
622 if (!stObj->pt) {
623 /* Probably out of memory.
624 * Try flushing any pending rendering, then retry.
625 */
626 st_finish(ctx->st);
627 guess_and_alloc_texture(ctx->st, stObj, stImage);
628 if (!stObj->pt) {
629 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
630 return;
631 }
632 }
633 }
634
635 assert(!stImage->pt);
636
637 if (stObj->pt &&
638 st_texture_match_image(stObj->pt, &stImage->base,
639 stImage->face, stImage->level)) {
640
641 pipe_texture_reference(&stImage->pt, stObj->pt);
642 assert(stImage->pt);
643 }
644
645 if (!stImage->pt)
646 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
647
648 /* st_CopyTexImage calls this function with pixels == NULL, with
649 * the expectation that the texture will be set up but nothing
650 * more will be done. This is where those calls return:
651 */
652 if (compressed_src) {
653 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
654 unpack,
655 "glCompressedTexImage");
656 }
657 else {
658 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
659 format, type,
660 pixels, unpack, "glTexImage");
661 }
662 if (!pixels)
663 return;
664
665 /* See if we can do texture compression with a blit/render.
666 */
667 if (!compressed_src &&
668 !ctx->Mesa_DXTn &&
669 is_compressed_mesa_format(texImage->TexFormat) &&
670 screen->is_format_supported(screen,
671 stImage->pt->format,
672 stImage->pt->target,
673 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
674 if (compress_with_blit(ctx, target, level, 0, 0, 0, width, height, depth,
675 format, type, pixels, unpack, texImage)) {
676 return;
677 }
678 }
679
680 if (stImage->pt) {
681 texImage->Data = st_texture_image_map(ctx->st, stImage, 0,
682 PIPE_TRANSFER_WRITE, 0, 0,
683 stImage->base.Width,
684 stImage->base.Height);
685 if(stImage->transfer)
686 dstRowStride = stImage->transfer->stride;
687 }
688 else {
689 /* Allocate regular memory and store the image there temporarily. */
690 if (texImage->IsCompressed) {
691 sizeInBytes = texImage->CompressedSize;
692 dstRowStride =
693 _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat, width);
694 assert(dims != 3);
695 }
696 else {
697 dstRowStride = postConvWidth * texelBytes;
698 sizeInBytes = depth * dstRowStride * postConvHeight;
699 }
700
701 texImage->Data = _mesa_align_malloc(sizeInBytes, 16);
702 }
703
704 if (!texImage->Data) {
705 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
706 return;
707 }
708
709 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
710 width, height, depth, width * texelBytes, dstRowStride);
711
712 /* Copy data. Would like to know when it's ok for us to eg. use
713 * the blitter to copy. Or, use the hardware to do the format
714 * conversion and copy:
715 */
716 if (compressed_src) {
717 memcpy(texImage->Data, pixels, imageSize);
718 }
719 else {
720 const GLuint srcImageStride =
721 _mesa_image_image_stride(unpack, width, height, format, type);
722 GLint i;
723 const GLubyte *src = (const GLubyte *) pixels;
724
725 for (i = 0; i < depth; i++) {
726 if (!texImage->TexFormat->StoreImage(ctx, dims,
727 texImage->_BaseFormat,
728 texImage->TexFormat,
729 texImage->Data,
730 0, 0, 0, /* dstX/Y/Zoffset */
731 dstRowStride,
732 texImage->ImageOffsets,
733 width, height, 1,
734 format, type, src, unpack)) {
735 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
736 }
737
738 if (stImage->pt && i + 1 < depth) {
739 /* unmap this slice */
740 st_texture_image_unmap(ctx->st, stImage);
741 /* map next slice of 3D texture */
742 texImage->Data = st_texture_image_map(ctx->st, stImage, i + 1,
743 PIPE_TRANSFER_WRITE, 0, 0,
744 stImage->base.Width,
745 stImage->base.Height);
746 src += srcImageStride;
747 }
748 }
749 }
750
751 _mesa_unmap_teximage_pbo(ctx, unpack);
752
753 if (stImage->pt && texImage->Data) {
754 st_texture_image_unmap(ctx->st, stImage);
755 texImage->Data = NULL;
756 }
757
758 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
759 ctx->Driver.GenerateMipmap(ctx, target, texObj);
760 }
761 }
762
763
764 static void
765 st_TexImage3D(GLcontext * ctx,
766 GLenum target, GLint level,
767 GLint internalFormat,
768 GLint width, GLint height, GLint depth,
769 GLint border,
770 GLenum format, GLenum type, const void *pixels,
771 const struct gl_pixelstore_attrib *unpack,
772 struct gl_texture_object *texObj,
773 struct gl_texture_image *texImage)
774 {
775 st_TexImage(ctx, 3, target, level, internalFormat, width, height, depth,
776 border, format, type, pixels, unpack, texObj, texImage,
777 0, GL_FALSE);
778 }
779
780
781 static void
782 st_TexImage2D(GLcontext * ctx,
783 GLenum target, GLint level,
784 GLint internalFormat,
785 GLint width, GLint height, GLint border,
786 GLenum format, GLenum type, const void *pixels,
787 const struct gl_pixelstore_attrib *unpack,
788 struct gl_texture_object *texObj,
789 struct gl_texture_image *texImage)
790 {
791 st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border,
792 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
793 }
794
795
796 static void
797 st_TexImage1D(GLcontext * ctx,
798 GLenum target, GLint level,
799 GLint internalFormat,
800 GLint width, GLint border,
801 GLenum format, GLenum type, const void *pixels,
802 const struct gl_pixelstore_attrib *unpack,
803 struct gl_texture_object *texObj,
804 struct gl_texture_image *texImage)
805 {
806 st_TexImage(ctx, 1, target, level, internalFormat, width, 1, 1, border,
807 format, type, pixels, unpack, texObj, texImage, 0, GL_FALSE);
808 }
809
810
811 static void
812 st_CompressedTexImage2D(GLcontext *ctx, GLenum target, GLint level,
813 GLint internalFormat,
814 GLint width, GLint height, GLint border,
815 GLsizei imageSize, const GLvoid *data,
816 struct gl_texture_object *texObj,
817 struct gl_texture_image *texImage)
818 {
819 st_TexImage(ctx, 2, target, level, internalFormat, width, height, 1, border,
820 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, GL_TRUE);
821 }
822
823
824
825 /**
826 * glGetTexImage() helper: decompress a compressed texture by rendering
827 * a textured quad. Store the results in the user's buffer.
828 */
829 static void
830 decompress_with_blit(GLcontext * ctx, GLenum target, GLint level,
831 GLenum format, GLenum type, GLvoid *pixels,
832 struct gl_texture_object *texObj,
833 struct gl_texture_image *texImage)
834 {
835 struct pipe_screen *screen = ctx->st->pipe->screen;
836 struct st_texture_image *stImage = st_texture_image(texImage);
837 const GLuint width = texImage->Width;
838 const GLuint height = texImage->Height;
839 struct pipe_surface *dst_surface;
840 struct pipe_texture *dst_texture;
841 struct pipe_transfer *tex_xfer;
842
843 /* create temp / dest surface */
844 if (!util_create_rgba_surface(screen, width, height,
845 &dst_texture, &dst_surface)) {
846 _mesa_problem(ctx, "util_create_rgba_surface() failed "
847 "in decompress_with_blit()");
848 return;
849 }
850
851 /* blit/render/decompress */
852 util_blit_pixels_tex(ctx->st->blit,
853 stImage->pt, /* pipe_texture (src) */
854 0, 0, /* src x0, y0 */
855 width, height, /* src x1, y1 */
856 dst_surface, /* pipe_surface (dst) */
857 0, 0, /* dst x0, y0 */
858 width, height, /* dst x1, y1 */
859 0.0, /* z */
860 PIPE_TEX_MIPFILTER_NEAREST);
861
862 /* map the dst_surface so we can read from it */
863 tex_xfer = st_cond_flush_get_tex_transfer(st_context(ctx),
864 dst_texture, 0, 0, 0,
865 PIPE_TRANSFER_READ,
866 0, 0, width, height);
867
868 pixels = _mesa_map_readpix_pbo(ctx, &ctx->Pack, pixels);
869
870 /* copy/pack data into user buffer */
871 if (st_equal_formats(stImage->pt->format, format, type)) {
872 /* memcpy */
873 const uint bytesPerRow = width * pf_get_size(stImage->pt->format);
874 ubyte *map = screen->transfer_map(screen, tex_xfer);
875 GLuint row;
876 for (row = 0; row < height; row++) {
877 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
878 height, format, type, row, 0);
879 memcpy(dest, map, bytesPerRow);
880 map += tex_xfer->stride;
881 }
882 screen->transfer_unmap(screen, tex_xfer);
883 }
884 else {
885 /* format translation via floats */
886 GLuint row;
887 for (row = 0; row < height; row++) {
888 const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
889 GLfloat rgba[4 * MAX_WIDTH];
890 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
891 height, format, type, row, 0);
892
893 /* get float[4] rgba row from surface */
894 pipe_get_tile_rgba(tex_xfer, 0, row, width, 1, rgba);
895
896 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
897 type, dest, &ctx->Pack, transferOps);
898 }
899 }
900
901 _mesa_unmap_readpix_pbo(ctx, &ctx->Pack);
902
903 /* destroy the temp / dest surface */
904 util_destroy_rgba_surface(dst_texture, dst_surface);
905 }
906
907
908
909 /**
910 * Need to map texture image into memory before copying image data,
911 * then unmap it.
912 */
913 static void
914 st_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
915 GLenum format, GLenum type, GLvoid * pixels,
916 struct gl_texture_object *texObj,
917 struct gl_texture_image *texImage, GLboolean compressed_dst)
918 {
919 struct st_texture_image *stImage = st_texture_image(texImage);
920 const GLuint dstImageStride =
921 _mesa_image_image_stride(&ctx->Pack, texImage->Width, texImage->Height,
922 format, type);
923 GLuint depth, i;
924 GLubyte *dest;
925
926 if (stImage->pt &&
927 pf_is_compressed(stImage->pt->format) &&
928 !compressed_dst) {
929 /* Need to decompress the texture.
930 * We'll do this by rendering a textured quad.
931 * Note that we only expect RGBA formats (no Z/depth formats).
932 */
933 decompress_with_blit(ctx, target, level, format, type, pixels,
934 texObj, texImage);
935 return;
936 }
937
938 /* Map */
939 if (stImage->pt) {
940 /* Image is stored in hardware format in a buffer managed by the
941 * kernel. Need to explicitly map and unmap it.
942 */
943
944 st_teximage_flush_before_map(ctx->st, stImage->pt, 0, level,
945 PIPE_TRANSFER_READ);
946
947 texImage->Data = st_texture_image_map(ctx->st, stImage, 0,
948 PIPE_TRANSFER_READ, 0, 0,
949 stImage->base.Width,
950 stImage->base.Height);
951 texImage->RowStride = stImage->transfer->stride / stImage->pt->block.size;
952 }
953 else {
954 /* Otherwise, the image should actually be stored in
955 * texImage->Data. This is pretty confusing for
956 * everybody, I'd much prefer to separate the two functions of
957 * texImage->Data - storage for texture images in main memory
958 * and access (ie mappings) of images. In other words, we'd
959 * create a new texImage->Map field and leave Data simply for
960 * storage.
961 */
962 assert(texImage->Data);
963 }
964
965 depth = texImage->Depth;
966 texImage->Depth = 1;
967
968 dest = (GLubyte *) pixels;
969
970 for (i = 0; i < depth; i++) {
971 if (compressed_dst) {
972 _mesa_get_compressed_teximage(ctx, target, level, dest,
973 texObj, texImage);
974 }
975 else {
976 _mesa_get_teximage(ctx, target, level, format, type, dest,
977 texObj, texImage);
978 }
979
980 if (stImage->pt && i + 1 < depth) {
981 /* unmap this slice */
982 st_texture_image_unmap(ctx->st, stImage);
983 /* map next slice of 3D texture */
984 texImage->Data = st_texture_image_map(ctx->st, stImage, i + 1,
985 PIPE_TRANSFER_READ, 0, 0,
986 stImage->base.Width,
987 stImage->base.Height);
988 dest += dstImageStride;
989 }
990 }
991
992 texImage->Depth = depth;
993
994 /* Unmap */
995 if (stImage->pt) {
996 st_texture_image_unmap(ctx->st, stImage);
997 texImage->Data = NULL;
998 }
999 }
1000
1001
1002 static void
1003 st_GetTexImage(GLcontext * ctx, GLenum target, GLint level,
1004 GLenum format, GLenum type, GLvoid * pixels,
1005 struct gl_texture_object *texObj,
1006 struct gl_texture_image *texImage)
1007 {
1008 st_get_tex_image(ctx, target, level, format, type, pixels, texObj, texImage,
1009 GL_FALSE);
1010 }
1011
1012
1013 static void
1014 st_GetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level,
1015 GLvoid *pixels,
1016 struct gl_texture_object *texObj,
1017 struct gl_texture_image *texImage)
1018 {
1019 st_get_tex_image(ctx, target, level, 0, 0, pixels, texObj, texImage,
1020 GL_TRUE);
1021 }
1022
1023
1024
1025 static void
1026 st_TexSubimage(GLcontext *ctx, GLint dims, GLenum target, GLint level,
1027 GLint xoffset, GLint yoffset, GLint zoffset,
1028 GLint width, GLint height, GLint depth,
1029 GLenum format, GLenum type, const void *pixels,
1030 const struct gl_pixelstore_attrib *packing,
1031 struct gl_texture_object *texObj,
1032 struct gl_texture_image *texImage)
1033 {
1034 struct pipe_screen *screen = ctx->st->pipe->screen;
1035 struct st_texture_image *stImage = st_texture_image(texImage);
1036 GLuint dstRowStride;
1037 const GLuint srcImageStride =
1038 _mesa_image_image_stride(packing, width, height, format, type);
1039 GLint i;
1040 const GLubyte *src;
1041
1042 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
1043 _mesa_lookup_enum_by_nr(target),
1044 level, xoffset, yoffset, width, height);
1045
1046 pixels =
1047 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format,
1048 type, pixels, packing, "glTexSubImage2D");
1049 if (!pixels)
1050 return;
1051
1052 /* See if we can do texture compression with a blit/render.
1053 */
1054 if (!ctx->Mesa_DXTn &&
1055 is_compressed_mesa_format(texImage->TexFormat) &&
1056 screen->is_format_supported(screen,
1057 stImage->pt->format,
1058 stImage->pt->target,
1059 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
1060 if (compress_with_blit(ctx, target, level,
1061 xoffset, yoffset, zoffset,
1062 width, height, depth,
1063 format, type, pixels, packing, texImage)) {
1064 return;
1065 }
1066 }
1067
1068 /* Map buffer if necessary. Need to lock to prevent other contexts
1069 * from uploading the buffer under us.
1070 */
1071 if (stImage->pt) {
1072 st_teximage_flush_before_map(ctx->st, stImage->pt, 0, level,
1073 PIPE_TRANSFER_WRITE);
1074 texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset,
1075 PIPE_TRANSFER_WRITE,
1076 xoffset, yoffset,
1077 width, height);
1078 }
1079
1080 if (!texImage->Data) {
1081 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1082 return;
1083 }
1084
1085 src = (const GLubyte *) pixels;
1086 dstRowStride = stImage->transfer->stride;
1087
1088 for (i = 0; i < depth; i++) {
1089 if (!texImage->TexFormat->StoreImage(ctx, dims, texImage->_BaseFormat,
1090 texImage->TexFormat,
1091 texImage->Data,
1092 0, 0, 0,
1093 dstRowStride,
1094 texImage->ImageOffsets,
1095 width, height, 1,
1096 format, type, src, packing)) {
1097 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1098 }
1099
1100 if (stImage->pt && i + 1 < depth) {
1101 /* unmap this slice */
1102 st_texture_image_unmap(ctx->st, stImage);
1103 /* map next slice of 3D texture */
1104 texImage->Data = st_texture_image_map(ctx->st, stImage,
1105 zoffset + i + 1,
1106 PIPE_TRANSFER_WRITE,
1107 xoffset, yoffset,
1108 width, height);
1109 src += srcImageStride;
1110 }
1111 }
1112
1113 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
1114 ctx->Driver.GenerateMipmap(ctx, target, texObj);
1115 }
1116
1117 _mesa_unmap_teximage_pbo(ctx, packing);
1118
1119 if (stImage->pt) {
1120 st_texture_image_unmap(ctx->st, stImage);
1121 texImage->Data = NULL;
1122 }
1123 }
1124
1125
1126
1127 static void
1128 st_TexSubImage3D(GLcontext *ctx, GLenum target, GLint level,
1129 GLint xoffset, GLint yoffset, GLint zoffset,
1130 GLsizei width, GLsizei height, GLsizei depth,
1131 GLenum format, GLenum type, const GLvoid *pixels,
1132 const struct gl_pixelstore_attrib *packing,
1133 struct gl_texture_object *texObj,
1134 struct gl_texture_image *texImage)
1135 {
1136 st_TexSubimage(ctx, 3, target, level, xoffset, yoffset, zoffset,
1137 width, height, depth, format, type,
1138 pixels, packing, texObj, texImage);
1139 }
1140
1141
1142 static void
1143 st_TexSubImage2D(GLcontext *ctx, GLenum target, GLint level,
1144 GLint xoffset, GLint yoffset,
1145 GLsizei width, GLsizei height,
1146 GLenum format, GLenum type, const GLvoid * pixels,
1147 const struct gl_pixelstore_attrib *packing,
1148 struct gl_texture_object *texObj,
1149 struct gl_texture_image *texImage)
1150 {
1151 st_TexSubimage(ctx, 2, target, level, xoffset, yoffset, 0,
1152 width, height, 1, format, type,
1153 pixels, packing, texObj, texImage);
1154 }
1155
1156
1157 static void
1158 st_TexSubImage1D(GLcontext *ctx, GLenum target, GLint level,
1159 GLint xoffset, GLsizei width, GLenum format, GLenum type,
1160 const GLvoid * pixels,
1161 const struct gl_pixelstore_attrib *packing,
1162 struct gl_texture_object *texObj,
1163 struct gl_texture_image *texImage)
1164 {
1165 st_TexSubimage(ctx, 1, target, level, xoffset, 0, 0, width, 1, 1,
1166 format, type, pixels, packing, texObj, texImage);
1167 }
1168
1169
1170
1171 /**
1172 * Do a CopyTexSubImage operation using a read transfer from the source,
1173 * a write transfer to the destination and get_tile()/put_tile() to access
1174 * the pixels/texels.
1175 *
1176 * Note: srcY=0=TOP of renderbuffer
1177 */
1178 static void
1179 fallback_copy_texsubimage(GLcontext *ctx, GLenum target, GLint level,
1180 struct st_renderbuffer *strb,
1181 struct st_texture_image *stImage,
1182 GLenum baseFormat,
1183 GLint destX, GLint destY, GLint destZ,
1184 GLint srcX, GLint srcY,
1185 GLsizei width, GLsizei height)
1186 {
1187 struct pipe_context *pipe = ctx->st->pipe;
1188 struct pipe_screen *screen = pipe->screen;
1189 struct pipe_transfer *src_trans;
1190 GLvoid *texDest;
1191
1192 assert(width <= MAX_WIDTH);
1193
1194 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1195 srcY = strb->Base.Height - srcY - height;
1196 }
1197
1198 src_trans = st_cond_flush_get_tex_transfer( st_context(ctx),
1199 strb->texture,
1200 0, 0, 0,
1201 PIPE_TRANSFER_READ,
1202 srcX, srcY,
1203 width, height);
1204
1205 st_teximage_flush_before_map(ctx->st, stImage->pt, 0, 0,
1206 PIPE_TRANSFER_WRITE);
1207
1208 texDest = st_texture_image_map(ctx->st, stImage, 0, PIPE_TRANSFER_WRITE,
1209 destX, destY, width, height);
1210
1211 if (baseFormat == GL_DEPTH_COMPONENT ||
1212 baseFormat == GL_DEPTH24_STENCIL8) {
1213 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
1214 ctx->Pixel.DepthBias != 0.0F);
1215 GLint row, yStep;
1216
1217 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1218 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1219 srcY = height - 1;
1220 yStep = -1;
1221 }
1222 else {
1223 srcY = 0;
1224 yStep = 1;
1225 }
1226
1227 /* To avoid a large temp memory allocation, do copy row by row */
1228 for (row = 0; row < height; row++, srcY += yStep) {
1229 uint data[MAX_WIDTH];
1230 pipe_get_tile_z(src_trans, 0, srcY, width, 1, data);
1231 if (scaleOrBias) {
1232 _mesa_scale_and_bias_depth_uint(ctx, width, data);
1233 }
1234 pipe_put_tile_z(stImage->transfer, 0, row, width, 1, data);
1235 }
1236 }
1237 else {
1238 /* RGBA format */
1239 GLfloat *tempSrc =
1240 (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
1241
1242 if (tempSrc && texDest) {
1243 const GLint dims = 2;
1244 const GLint dstRowStride = stImage->transfer->stride;
1245 struct gl_texture_image *texImage = &stImage->base;
1246 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
1247
1248 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1249 unpack.Invert = GL_TRUE;
1250 }
1251
1252 /* get float/RGBA image from framebuffer */
1253 /* XXX this usually involves a lot of int/float conversion.
1254 * try to avoid that someday.
1255 */
1256 pipe_get_tile_rgba(src_trans, 0, 0, width, height, tempSrc);
1257
1258 /* Store into texture memory.
1259 * Note that this does some special things such as pixel transfer
1260 * ops and format conversion. In particular, if the dest tex format
1261 * is actually RGBA but the user created the texture as GL_RGB we
1262 * need to fill-in/override the alpha channel with 1.0.
1263 */
1264 texImage->TexFormat->StoreImage(ctx, dims,
1265 texImage->_BaseFormat,
1266 texImage->TexFormat,
1267 texDest,
1268 0, 0, 0,
1269 dstRowStride,
1270 texImage->ImageOffsets,
1271 width, height, 1,
1272 GL_RGBA, GL_FLOAT, tempSrc, /* src */
1273 &unpack);
1274 }
1275 else {
1276 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1277 }
1278
1279 if (tempSrc)
1280 _mesa_free(tempSrc);
1281 }
1282
1283 st_texture_image_unmap(ctx->st, stImage);
1284 screen->tex_transfer_destroy(src_trans);
1285 }
1286
1287
1288 /**
1289 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1290 * Note that the region to copy has already been clipped so we know we
1291 * won't read from outside the source renderbuffer's bounds.
1292 *
1293 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1294 */
1295 static void
1296 st_copy_texsubimage(GLcontext *ctx,
1297 GLenum target, GLint level,
1298 GLint destX, GLint destY, GLint destZ,
1299 GLint srcX, GLint srcY,
1300 GLsizei width, GLsizei height)
1301 {
1302 struct gl_texture_unit *texUnit =
1303 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1304 struct gl_texture_object *texObj =
1305 _mesa_select_tex_object(ctx, texUnit, target);
1306 struct gl_texture_image *texImage =
1307 _mesa_select_tex_image(ctx, texObj, target, level);
1308 struct st_texture_image *stImage = st_texture_image(texImage);
1309 const GLenum texBaseFormat = texImage->InternalFormat;
1310 struct gl_framebuffer *fb = ctx->ReadBuffer;
1311 struct st_renderbuffer *strb;
1312 struct pipe_context *pipe = ctx->st->pipe;
1313 struct pipe_screen *screen = pipe->screen;
1314 enum pipe_format dest_format, src_format;
1315 GLboolean use_fallback = GL_TRUE;
1316 GLboolean matching_base_formats;
1317
1318 /* any rendering in progress must complete before we grab the fb image */
1319 st_finish(ctx->st);
1320
1321 /* determine if copying depth or color data */
1322 if (texBaseFormat == GL_DEPTH_COMPONENT ||
1323 texBaseFormat == GL_DEPTH24_STENCIL8) {
1324 strb = st_renderbuffer(fb->_DepthBuffer);
1325 }
1326 else if (texBaseFormat == GL_DEPTH_STENCIL_EXT) {
1327 strb = st_renderbuffer(fb->_StencilBuffer);
1328 }
1329 else {
1330 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1331 strb = st_renderbuffer(fb->_ColorReadBuffer);
1332 }
1333
1334 assert(strb);
1335 assert(strb->surface);
1336 assert(stImage->pt);
1337
1338 src_format = strb->surface->format;
1339 dest_format = stImage->pt->format;
1340
1341 /*
1342 * Determine if the src framebuffer and dest texture have the same
1343 * base format. We need this to detect a case such as the framebuffer
1344 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1345 * texture format stores RGBA we need to set A=1 (overriding the
1346 * framebuffer's alpha values). We can't do that with the blit or
1347 * textured-quad paths.
1348 */
1349 matching_base_formats = (strb->Base._BaseFormat == texImage->_BaseFormat);
1350
1351 if (matching_base_formats && ctx->_ImageTransferState == 0x0) {
1352 /* try potential hardware path */
1353 struct pipe_surface *dest_surface = NULL;
1354 boolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1355
1356 if (src_format == dest_format && !do_flip) {
1357 /* use surface_copy() / blit */
1358
1359 dest_surface = screen->get_tex_surface(screen, stImage->pt,
1360 stImage->face, stImage->level,
1361 destZ,
1362 PIPE_BUFFER_USAGE_GPU_WRITE);
1363
1364 /* for surface_copy(), y=0=top, always */
1365 pipe->surface_copy(pipe,
1366 /* dest */
1367 dest_surface,
1368 destX, destY,
1369 /* src */
1370 strb->surface,
1371 srcX, srcY,
1372 /* size */
1373 width, height);
1374 use_fallback = GL_FALSE;
1375 }
1376 else if (screen->is_format_supported(screen, src_format,
1377 PIPE_TEXTURE_2D,
1378 PIPE_TEXTURE_USAGE_SAMPLER,
1379 0) &&
1380 screen->is_format_supported(screen, dest_format,
1381 PIPE_TEXTURE_2D,
1382 PIPE_TEXTURE_USAGE_RENDER_TARGET,
1383 0)) {
1384 /* draw textured quad to do the copy */
1385 GLint srcY0, srcY1;
1386
1387 dest_surface = screen->get_tex_surface(screen, stImage->pt,
1388 stImage->face, stImage->level,
1389 destZ,
1390 PIPE_BUFFER_USAGE_GPU_WRITE);
1391
1392 if (do_flip) {
1393 srcY1 = strb->Base.Height - srcY - height;
1394 srcY0 = srcY1 + height;
1395 }
1396 else {
1397 srcY0 = srcY;
1398 srcY1 = srcY0 + height;
1399 }
1400 util_blit_pixels(ctx->st->blit,
1401 strb->surface,
1402 srcX, srcY0,
1403 srcX + width, srcY1,
1404 dest_surface,
1405 destX, destY,
1406 destX + width, destY + height,
1407 0.0, PIPE_TEX_MIPFILTER_NEAREST);
1408 use_fallback = GL_FALSE;
1409 }
1410
1411 if (dest_surface)
1412 pipe_surface_reference(&dest_surface, NULL);
1413 }
1414
1415 if (use_fallback) {
1416 /* software fallback */
1417 fallback_copy_texsubimage(ctx, target, level,
1418 strb, stImage, texBaseFormat,
1419 destX, destY, destZ,
1420 srcX, srcY, width, height);
1421 }
1422
1423 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
1424 ctx->Driver.GenerateMipmap(ctx, target, texObj);
1425 }
1426 }
1427
1428
1429
1430 static void
1431 st_CopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
1432 GLenum internalFormat,
1433 GLint x, GLint y, GLsizei width, GLint border)
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
1442 /* Setup or redefine the texture object, texture and texture
1443 * image. Don't populate yet.
1444 */
1445 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
1446 width, border,
1447 GL_RGBA, CHAN_TYPE, NULL,
1448 &ctx->DefaultPacking, texObj, texImage);
1449
1450 st_copy_texsubimage(ctx, target, level,
1451 0, 0, 0, /* destX,Y,Z */
1452 x, y, width, 1); /* src X, Y, size */
1453 }
1454
1455
1456 static void
1457 st_CopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
1458 GLenum internalFormat,
1459 GLint x, GLint y, GLsizei width, GLsizei height,
1460 GLint border)
1461 {
1462 struct gl_texture_unit *texUnit =
1463 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1464 struct gl_texture_object *texObj =
1465 _mesa_select_tex_object(ctx, texUnit, target);
1466 struct gl_texture_image *texImage =
1467 _mesa_select_tex_image(ctx, texObj, target, level);
1468
1469 /* Setup or redefine the texture object, texture and texture
1470 * image. Don't populate yet.
1471 */
1472 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
1473 width, height, border,
1474 GL_RGBA, CHAN_TYPE, NULL,
1475 &ctx->DefaultPacking, texObj, texImage);
1476
1477 st_copy_texsubimage(ctx, target, level,
1478 0, 0, 0, /* destX,Y,Z */
1479 x, y, width, height); /* src X, Y, size */
1480 }
1481
1482
1483 static void
1484 st_CopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
1485 GLint xoffset, GLint x, GLint y, GLsizei width)
1486 {
1487 const GLint yoffset = 0, zoffset = 0;
1488 const GLsizei height = 1;
1489 st_copy_texsubimage(ctx, target, level,
1490 xoffset, yoffset, zoffset, /* destX,Y,Z */
1491 x, y, width, height); /* src X, Y, size */
1492 }
1493
1494
1495 static void
1496 st_CopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
1497 GLint xoffset, GLint yoffset,
1498 GLint x, GLint y, GLsizei width, GLsizei height)
1499 {
1500 const GLint zoffset = 0;
1501 st_copy_texsubimage(ctx, target, level,
1502 xoffset, yoffset, zoffset, /* destX,Y,Z */
1503 x, y, width, height); /* src X, Y, size */
1504 }
1505
1506
1507 static void
1508 st_CopyTexSubImage3D(GLcontext * ctx, GLenum target, GLint level,
1509 GLint xoffset, GLint yoffset, GLint zoffset,
1510 GLint x, GLint y, GLsizei width, GLsizei height)
1511 {
1512 st_copy_texsubimage(ctx, target, level,
1513 xoffset, yoffset, zoffset, /* destX,Y,Z */
1514 x, y, width, height); /* src X, Y, size */
1515 }
1516
1517
1518 /**
1519 * Compute which mipmap levels that really need to be sent to the hardware.
1520 * This depends on the base image size, GL_TEXTURE_MIN_LOD,
1521 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
1522 */
1523 static void
1524 calculate_first_last_level(struct st_texture_object *stObj)
1525 {
1526 struct gl_texture_object *tObj = &stObj->base;
1527
1528 /* These must be signed values. MinLod and MaxLod can be negative numbers,
1529 * and having firstLevel and lastLevel as signed prevents the need for
1530 * extra sign checks.
1531 */
1532 GLint firstLevel;
1533 GLint lastLevel;
1534
1535 /* Yes, this looks overly complicated, but it's all needed.
1536 */
1537 switch (tObj->Target) {
1538 case GL_TEXTURE_1D:
1539 case GL_TEXTURE_2D:
1540 case GL_TEXTURE_3D:
1541 case GL_TEXTURE_CUBE_MAP:
1542 if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) {
1543 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
1544 */
1545 firstLevel = lastLevel = tObj->BaseLevel;
1546 }
1547 else {
1548 firstLevel = 0;
1549 lastLevel = MIN2(tObj->MaxLevel,
1550 (int) tObj->Image[0][tObj->BaseLevel]->WidthLog2);
1551 }
1552 break;
1553 case GL_TEXTURE_RECTANGLE_NV:
1554 case GL_TEXTURE_4D_SGIS:
1555 firstLevel = lastLevel = 0;
1556 break;
1557 default:
1558 return;
1559 }
1560
1561 stObj->lastLevel = lastLevel;
1562 }
1563
1564
1565 static void
1566 copy_image_data_to_texture(struct st_context *st,
1567 struct st_texture_object *stObj,
1568 GLuint dstLevel,
1569 struct st_texture_image *stImage)
1570 {
1571 if (stImage->pt) {
1572 /* Copy potentially with the blitter:
1573 */
1574 st_texture_image_copy(st->pipe,
1575 stObj->pt, dstLevel, /* dest texture, level */
1576 stImage->pt, /* src texture */
1577 stImage->face
1578 );
1579
1580 pipe_texture_reference(&stImage->pt, NULL);
1581 }
1582 else if (stImage->base.Data) {
1583 assert(stImage->base.Data != NULL);
1584
1585 /* More straightforward upload.
1586 */
1587
1588 st_teximage_flush_before_map(st, stObj->pt, stImage->face, dstLevel,
1589 PIPE_TRANSFER_WRITE);
1590
1591
1592 st_texture_image_data(st,
1593 stObj->pt,
1594 stImage->face,
1595 dstLevel,
1596 stImage->base.Data,
1597 stImage->base.RowStride *
1598 stObj->pt->block.size,
1599 stImage->base.RowStride *
1600 stImage->base.Height *
1601 stObj->pt->block.size);
1602 _mesa_align_free(stImage->base.Data);
1603 stImage->base.Data = NULL;
1604 }
1605
1606 pipe_texture_reference(&stImage->pt, stObj->pt);
1607 }
1608
1609
1610 /**
1611 * Called during state validation. When this function is finished,
1612 * the texture object should be ready for rendering.
1613 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1614 */
1615 GLboolean
1616 st_finalize_texture(GLcontext *ctx,
1617 struct pipe_context *pipe,
1618 struct gl_texture_object *tObj,
1619 GLboolean *needFlush)
1620 {
1621 struct st_texture_object *stObj = st_texture_object(tObj);
1622 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1623 GLuint cpp, face;
1624 struct st_texture_image *firstImage;
1625
1626 *needFlush = GL_FALSE;
1627
1628 /* We know/require this is true by now:
1629 */
1630 assert(stObj->base._Complete);
1631
1632 /* What levels must the texture include at a minimum?
1633 */
1634 calculate_first_last_level(stObj);
1635 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1636
1637 /* If both firstImage and stObj point to a texture which can contain
1638 * all active images, favour firstImage. Note that because of the
1639 * completeness requirement, we know that the image dimensions
1640 * will match.
1641 */
1642 if (firstImage->pt &&
1643 firstImage->pt != stObj->pt &&
1644 firstImage->pt->last_level >= stObj->lastLevel) {
1645 pipe_texture_reference(&stObj->pt, firstImage->pt);
1646 }
1647
1648 /* FIXME: determine format block instead of cpp */
1649 if (firstImage->base.IsCompressed) {
1650 cpp = compressed_num_bytes(firstImage->base.TexFormat->MesaFormat);
1651 }
1652 else {
1653 cpp = firstImage->base.TexFormat->TexelBytes;
1654 }
1655
1656 /* If we already have a gallium texture, check that it matches the texture
1657 * object's format, target, size, num_levels, etc.
1658 */
1659 if (stObj->pt) {
1660 const enum pipe_format fmt =
1661 st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat);
1662 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1663 stObj->pt->format != fmt ||
1664 stObj->pt->last_level < stObj->lastLevel ||
1665 stObj->pt->width[0] != firstImage->base.Width2 ||
1666 stObj->pt->height[0] != firstImage->base.Height2 ||
1667 stObj->pt->depth[0] != firstImage->base.Depth2 ||
1668 /* Nominal bytes per pixel: */
1669 stObj->pt->block.size / stObj->pt->block.width != cpp)
1670 {
1671 pipe_texture_reference(&stObj->pt, NULL);
1672 ctx->st->dirty.st |= ST_NEW_FRAMEBUFFER;
1673 }
1674 }
1675
1676 /* May need to create a new gallium texture:
1677 */
1678 if (!stObj->pt) {
1679 const enum pipe_format fmt =
1680 st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat);
1681 GLuint usage = default_usage(fmt);
1682
1683 stObj->pt = st_texture_create(ctx->st,
1684 gl_target_to_pipe(stObj->base.Target),
1685 fmt,
1686 stObj->lastLevel,
1687 firstImage->base.Width2,
1688 firstImage->base.Height2,
1689 firstImage->base.Depth2,
1690 usage);
1691
1692 if (!stObj->pt) {
1693 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1694 return GL_FALSE;
1695 }
1696 }
1697
1698 /* Pull in any images not in the object's texture:
1699 */
1700 for (face = 0; face < nr_faces; face++) {
1701 GLuint level;
1702 for (level = 0; level <= stObj->lastLevel; level++) {
1703 struct st_texture_image *stImage =
1704 st_texture_image(stObj->base.Image[face][stObj->base.BaseLevel + level]);
1705
1706 /* Need to import images in main memory or held in other textures.
1707 */
1708 if (stImage && stObj->pt != stImage->pt) {
1709 copy_image_data_to_texture(ctx->st, stObj, level, stImage);
1710 *needFlush = GL_TRUE;
1711 }
1712 }
1713 }
1714
1715 return GL_TRUE;
1716 }
1717
1718
1719 /**
1720 * Returns pointer to a default/dummy texture.
1721 * This is typically used when the current shader has tex/sample instructions
1722 * but the user has not provided a (any) texture(s).
1723 */
1724 struct gl_texture_object *
1725 st_get_default_texture(struct st_context *st)
1726 {
1727 if (!st->default_texture) {
1728 static const GLenum target = GL_TEXTURE_2D;
1729 GLubyte pixels[16][16][4];
1730 struct gl_texture_object *texObj;
1731 struct gl_texture_image *texImg;
1732 GLuint i, j;
1733
1734 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1735 * when attempting to sample incomplete textures.
1736 */
1737 for (i = 0; i < 16; i++) {
1738 for (j = 0; j < 16; j++) {
1739 pixels[i][j][0] = 0;
1740 pixels[i][j][1] = 0;
1741 pixels[i][j][2] = 0;
1742 pixels[i][j][3] = 255;
1743 }
1744 }
1745
1746 texObj = st->ctx->Driver.NewTextureObject(st->ctx, 0, target);
1747
1748 texImg = _mesa_get_tex_image(st->ctx, texObj, target, 0);
1749
1750 _mesa_init_teximage_fields(st->ctx, target, texImg,
1751 16, 16, 1, 0, /* w, h, d, border */
1752 GL_RGBA);
1753
1754 st_TexImage(st->ctx, 2, target,
1755 0, GL_RGBA, /* level, intformat */
1756 16, 16, 1, 0, /* w, h, d, border */
1757 GL_RGBA, GL_UNSIGNED_BYTE, pixels,
1758 &st->ctx->DefaultPacking,
1759 texObj, texImg,
1760 0, 0);
1761
1762 texObj->MinFilter = GL_NEAREST;
1763 texObj->MagFilter = GL_NEAREST;
1764 texObj->_Complete = GL_TRUE;
1765
1766 st->default_texture = texObj;
1767 }
1768 return st->default_texture;
1769 }
1770
1771
1772 void
1773 st_init_texture_functions(struct dd_function_table *functions)
1774 {
1775 functions->ChooseTextureFormat = st_ChooseTextureFormat;
1776 functions->TexImage1D = st_TexImage1D;
1777 functions->TexImage2D = st_TexImage2D;
1778 functions->TexImage3D = st_TexImage3D;
1779 functions->TexSubImage1D = st_TexSubImage1D;
1780 functions->TexSubImage2D = st_TexSubImage2D;
1781 functions->TexSubImage3D = st_TexSubImage3D;
1782 functions->CopyTexImage1D = st_CopyTexImage1D;
1783 functions->CopyTexImage2D = st_CopyTexImage2D;
1784 functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
1785 functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
1786 functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
1787 functions->GenerateMipmap = st_generate_mipmap;
1788
1789 functions->GetTexImage = st_GetTexImage;
1790
1791 /* compressed texture functions */
1792 functions->CompressedTexImage2D = st_CompressedTexImage2D;
1793 functions->GetCompressedTexImage = st_GetCompressedTexImage;
1794 functions->CompressedTextureSize = _mesa_compressed_texture_size;
1795
1796 functions->NewTextureObject = st_NewTextureObject;
1797 functions->NewTextureImage = st_NewTextureImage;
1798 functions->DeleteTexture = st_DeleteTextureObject;
1799 functions->FreeTexImageData = st_FreeTextureImageData;
1800 functions->UpdateTexturePalette = 0;
1801
1802 functions->TextureMemCpy = do_memcpy;
1803
1804 /* XXX Temporary until we can query pipe's texture sizes */
1805 functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1806 }