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