gallium: consolidate the bitmap->texel conversion code
[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/imports.h"
29 #include "main/convolve.h"
30 #include "main/enums.h"
31 #include "main/image.h"
32 #include "main/macros.h"
33 #include "main/mipmap.h"
34 #include "main/pixel.h"
35 #include "main/texcompress.h"
36 #include "main/texformat.h"
37 #include "main/teximage.h"
38 #include "main/texobj.h"
39 #include "main/texstore.h"
40
41 #include "state_tracker/st_context.h"
42 #include "state_tracker/st_cb_fbo.h"
43 #include "state_tracker/st_cb_texture.h"
44 #include "state_tracker/st_format.h"
45 #include "state_tracker/st_public.h"
46 #include "state_tracker/st_texture.h"
47 #include "state_tracker/st_gen_mipmap.h"
48
49 #include "pipe/p_context.h"
50 #include "pipe/p_defines.h"
51 #include "pipe/p_inlines.h"
52 #include "util/p_tile.h"
53 #include "util/u_blit.h"
54
55
56 #define DBG if (0) printf
57
58
59 static INLINE struct st_texture_image *
60 st_texture_image(struct gl_texture_image *img)
61 {
62 return (struct st_texture_image *) img;
63 }
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 int
95 compressed_num_bytes(GLuint mesaFormat)
96 {
97 switch(mesaFormat) {
98 case MESA_FORMAT_RGB_FXT1:
99 case MESA_FORMAT_RGBA_FXT1:
100 case MESA_FORMAT_RGB_DXT1:
101 case MESA_FORMAT_RGBA_DXT1:
102 return 2;
103 case MESA_FORMAT_RGBA_DXT3:
104 case MESA_FORMAT_RGBA_DXT5:
105 return 4;
106 default:
107 return 0;
108 }
109 }
110
111
112 static GLboolean
113 st_IsTextureResident(GLcontext * ctx, struct gl_texture_object *texObj)
114 {
115 #if 0
116 struct intel_context *intel = intel_context(ctx);
117 struct st_texture_object *stObj = st_texture_object(texObj);
118
119 return
120 stObj->pt &&
121 stObj->pt->region &&
122 intel_is_region_resident(intel, stObj->pt->region);
123 #endif
124 return 1;
125 }
126
127
128 static struct gl_texture_image *
129 st_NewTextureImage(GLcontext * ctx)
130 {
131 DBG("%s\n", __FUNCTION__);
132 (void) ctx;
133 return (struct gl_texture_image *) CALLOC_STRUCT(st_texture_image);
134 }
135
136
137 static struct gl_texture_object *
138 st_NewTextureObject(GLcontext * ctx, GLuint name, GLenum target)
139 {
140 struct st_texture_object *obj = CALLOC_STRUCT(st_texture_object);
141
142 DBG("%s\n", __FUNCTION__);
143 _mesa_initialize_texture_object(&obj->base, name, target);
144
145 return &obj->base;
146 }
147
148 static void
149 st_DeleteTextureObject(GLcontext *ctx,
150 struct gl_texture_object *texObj)
151 {
152 struct st_texture_object *stObj = st_texture_object(texObj);
153 if (stObj->pt)
154 pipe_texture_release(&stObj->pt);
155
156 _mesa_delete_texture_object(ctx, texObj);
157 }
158
159
160 static void
161 st_FreeTextureImageData(GLcontext * ctx, struct gl_texture_image *texImage)
162 {
163 struct st_texture_image *stImage = st_texture_image(texImage);
164
165 DBG("%s\n", __FUNCTION__);
166
167 if (stImage->pt) {
168 pipe_texture_release(&stImage->pt);
169 }
170
171 if (texImage->Data) {
172 free(texImage->Data);
173 texImage->Data = NULL;
174 }
175 }
176
177
178 /* ================================================================
179 * From linux kernel i386 header files, copes with odd sizes better
180 * than COPY_DWORDS would:
181 * XXX Put this in src/mesa/main/imports.h ???
182 */
183 #if defined(i386) || defined(__i386__)
184 static INLINE void *
185 __memcpy(void *to, const void *from, size_t n)
186 {
187 int d0, d1, d2;
188 __asm__ __volatile__("rep ; movsl\n\t"
189 "testb $2,%b4\n\t"
190 "je 1f\n\t"
191 "movsw\n"
192 "1:\ttestb $1,%b4\n\t"
193 "je 2f\n\t"
194 "movsb\n" "2:":"=&c"(d0), "=&D"(d1), "=&S"(d2)
195 :"0"(n / 4), "q"(n), "1"((long) to), "2"((long) from)
196 :"memory");
197 return (to);
198 }
199 #else
200 #define __memcpy(a,b,c) memcpy(a,b,c)
201 #endif
202
203
204 /* The system memcpy (at least on ubuntu 5.10) has problems copying
205 * to agp (writecombined) memory from a source which isn't 64-byte
206 * aligned - there is a 4x performance falloff.
207 *
208 * The x86 __memcpy is immune to this but is slightly slower
209 * (10%-ish) than the system memcpy.
210 *
211 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
212 * isn't much faster than x86_memcpy for agp copies.
213 *
214 * TODO: switch dynamically.
215 */
216 static void *
217 do_memcpy(void *dest, const void *src, size_t n)
218 {
219 if ((((unsigned) src) & 63) || (((unsigned) dest) & 63)) {
220 return __memcpy(dest, src, n);
221 }
222 else
223 return memcpy(dest, src, n);
224 }
225
226
227 /* Functions to store texture images. Where possible, textures
228 * will be created or further instantiated with image data, otherwise
229 * images will be stored in malloc'd memory. A validation step is
230 * required to pull those images into a texture, or otherwise
231 * decide a fallback is required.
232 */
233
234
235 static int
236 logbase2(int n)
237 {
238 GLint i = 1;
239 GLint log2 = 0;
240
241 while (n > i) {
242 i *= 2;
243 log2++;
244 }
245
246 return log2;
247 }
248
249
250 /**
251 * Allocate a pipe_texture object for the given st_texture_object using
252 * the given st_texture_image to guess the mipmap size/levels.
253 *
254 * [comments...]
255 * Otherwise, store it in memory if (Border != 0) or (any dimension ==
256 * 1).
257 *
258 * Otherwise, if max_level >= level >= min_level, create texture with
259 * space for images from min_level down to max_level.
260 *
261 * Otherwise, create texture with space for images from (level 0)..(1x1).
262 * Consider pruning this texture at a validation if the saving is worth it.
263 */
264 static void
265 guess_and_alloc_texture(struct st_context *st,
266 struct st_texture_object *stObj,
267 const struct st_texture_image *stImage)
268 {
269 GLuint firstLevel;
270 GLuint lastLevel;
271 GLuint width = stImage->base.Width2; /* size w/out border */
272 GLuint height = stImage->base.Height2;
273 GLuint depth = stImage->base.Depth2;
274 GLuint i, comp_byte = 0;
275
276 DBG("%s\n", __FUNCTION__);
277
278 assert(!stObj->pt);
279
280 if (stObj->pt &&
281 stImage->level > stObj->base.BaseLevel &&
282 (stImage->base.Width == 1 ||
283 (stObj->base.Target != GL_TEXTURE_1D &&
284 stImage->base.Height == 1) ||
285 (stObj->base.Target == GL_TEXTURE_3D &&
286 stImage->base.Depth == 1)))
287 return;
288
289 /* If this image disrespects BaseLevel, allocate from level zero.
290 * Usually BaseLevel == 0, so it's unlikely to happen.
291 */
292 if (stImage->level < stObj->base.BaseLevel)
293 firstLevel = 0;
294 else
295 firstLevel = stObj->base.BaseLevel;
296
297
298 /* Figure out image dimensions at start level.
299 */
300 for (i = stImage->level; i > firstLevel; i--) {
301 if (width != 1)
302 width <<= 1;
303 if (height != 1)
304 height <<= 1;
305 if (depth != 1)
306 depth <<= 1;
307 }
308
309 /* Guess a reasonable value for lastLevel. This is probably going
310 * to be wrong fairly often and might mean that we have to look at
311 * resizable buffers, or require that buffers implement lazy
312 * pagetable arrangements.
313 */
314 if ((stObj->base.MinFilter == GL_NEAREST ||
315 stObj->base.MinFilter == GL_LINEAR) &&
316 stImage->level == firstLevel) {
317 lastLevel = firstLevel;
318 }
319 else {
320 GLuint l2width = logbase2(width);
321 GLuint l2height = logbase2(height);
322 GLuint l2depth = logbase2(depth);
323 lastLevel = firstLevel + MAX2(MAX2(l2width, l2height), l2depth);
324 }
325
326 if (stImage->base.IsCompressed)
327 comp_byte = compressed_num_bytes(stImage->base.TexFormat->MesaFormat);
328
329 stObj->pt = st_texture_create(st,
330 gl_target_to_pipe(stObj->base.Target),
331 st_mesa_format_to_pipe_format(stImage->base.TexFormat->MesaFormat),
332 lastLevel,
333 width,
334 height,
335 depth,
336 comp_byte);
337
338 DBG("%s - success\n", __FUNCTION__);
339 }
340
341
342 /* There are actually quite a few combinations this will work for,
343 * more than what I've listed here.
344 */
345 static GLboolean
346 check_pbo_format(GLint internalFormat,
347 GLenum format, GLenum type,
348 const struct gl_texture_format *mesa_format)
349 {
350 switch (internalFormat) {
351 case 4:
352 case GL_RGBA:
353 return (format == GL_BGRA &&
354 (type == GL_UNSIGNED_BYTE ||
355 type == GL_UNSIGNED_INT_8_8_8_8_REV) &&
356 mesa_format == &_mesa_texformat_argb8888);
357 case 3:
358 case GL_RGB:
359 return (format == GL_RGB &&
360 type == GL_UNSIGNED_SHORT_5_6_5 &&
361 mesa_format == &_mesa_texformat_rgb565);
362 case GL_YCBCR_MESA:
363 return (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE);
364 default:
365 return GL_FALSE;
366 }
367 }
368
369
370 /* XXX: Do this for TexSubImage also:
371 */
372 static GLboolean
373 try_pbo_upload(GLcontext *ctx,
374 struct st_texture_image *stImage,
375 const struct gl_pixelstore_attrib *unpack,
376 GLint internalFormat,
377 GLint width, GLint height,
378 GLenum format, GLenum type, const void *pixels)
379 {
380 return GL_FALSE; /* XXX fix flushing/locking/blitting below */
381 #if 000
382 struct intel_context *intel = intel_context(ctx);
383 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
384 GLuint src_offset, src_stride;
385 GLuint dst_offset, dst_stride;
386
387 if (!pbo ||
388 ctx._ImageTransferState ||
389 unpack->SkipPixels || unpack->SkipRows) {
390 _mesa_printf("%s: failure 1\n", __FUNCTION__);
391 return GL_FALSE;
392 }
393
394 src_offset = (GLuint) pixels;
395
396 if (unpack->RowLength > 0)
397 src_stride = unpack->RowLength;
398 else
399 src_stride = width;
400
401 dst_offset = st_texture_image_offset(stImage->pt,
402 stImage->face,
403 stImage->level);
404
405 dst_stride = stImage->pt->pitch;
406
407 {
408 struct _DriBufferObject *src_buffer =
409 intel_bufferobj_buffer(intel, pbo, INTEL_READ);
410
411 /* Temporary hack: cast to _DriBufferObject:
412 */
413 struct _DriBufferObject *dst_buffer =
414 (struct _DriBufferObject *)stImage->pt->region->buffer;
415
416
417 intelEmitCopyBlit(intel,
418 stImage->pt->cpp,
419 src_stride, src_buffer, src_offset,
420 dst_stride, dst_buffer, dst_offset,
421 0, 0, 0, 0, width, height,
422 GL_COPY);
423 }
424
425 return GL_TRUE;
426 #endif
427 }
428
429
430 /**
431 * Adjust pixel unpack params and image dimensions to strip off the
432 * texture border.
433 * Gallium doesn't support texture borders. They've seldem been used
434 * and seldom been implemented correctly anyway.
435 * \param unpackNew returns the new pixel unpack parameters
436 */
437 static void
438 strip_texture_border(GLint border,
439 GLint *width, GLint *height, GLint *depth,
440 const struct gl_pixelstore_attrib *unpack,
441 struct gl_pixelstore_attrib *unpackNew)
442 {
443 assert(border > 0); /* sanity check */
444
445 *unpackNew = *unpack;
446
447 if (unpackNew->RowLength == 0)
448 unpackNew->RowLength = *width;
449
450 if (depth && unpackNew->ImageHeight == 0)
451 unpackNew->ImageHeight = *height;
452
453 unpackNew->SkipPixels += border;
454 if (height)
455 unpackNew->SkipRows += border;
456 if (depth)
457 unpackNew->SkipImages += border;
458
459 assert(*width >= 3);
460 *width = *width - 2 * border;
461 if (height && *height >= 3)
462 *height = *height - 2 * border;
463 if (depth && *depth >= 3)
464 *depth = *depth - 2 * border;
465 }
466
467
468 static void
469 st_TexImage(GLcontext * ctx,
470 GLint dims,
471 GLenum target, GLint level,
472 GLint internalFormat,
473 GLint width, GLint height, GLint depth,
474 GLint border,
475 GLenum format, GLenum type, const void *pixels,
476 const struct gl_pixelstore_attrib *unpack,
477 struct gl_texture_object *texObj,
478 struct gl_texture_image *texImage,
479 GLsizei imageSize, int compressed)
480 {
481 struct pipe_context *pipe = ctx->st->pipe;
482 struct st_texture_object *stObj = st_texture_object(texObj);
483 struct st_texture_image *stImage = st_texture_image(texImage);
484 GLint postConvWidth, postConvHeight;
485 GLint texelBytes, sizeInBytes;
486 GLuint dstRowStride;
487 struct gl_pixelstore_attrib unpackNB;
488
489 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
490 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
491
492 /* gallium does not support texture borders, strip it off */
493 if (border) {
494 strip_texture_border(border, &width, &height, &depth,
495 unpack, &unpackNB);
496 unpack = &unpackNB;
497 border = 0;
498 }
499
500 postConvWidth = width;
501 postConvHeight = height;
502
503 stImage->face = _mesa_tex_target_to_face(target);
504 stImage->level = level;
505
506 if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) {
507 _mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth,
508 &postConvHeight);
509 }
510
511 /* choose the texture format */
512 texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat,
513 format, type);
514
515 _mesa_set_fetch_functions(texImage, dims);
516
517 if (texImage->TexFormat->TexelBytes == 0) {
518 /* must be a compressed format */
519 texelBytes = 0;
520 texImage->IsCompressed = GL_TRUE;
521 texImage->CompressedSize =
522 ctx->Driver.CompressedTextureSize(ctx, texImage->Width,
523 texImage->Height, texImage->Depth,
524 texImage->TexFormat->MesaFormat);
525 }
526 else {
527 texelBytes = texImage->TexFormat->TexelBytes;
528
529 /* Minimum pitch of 32 bytes */
530 if (postConvWidth * texelBytes < 32) {
531 postConvWidth = 32 / texelBytes;
532 texImage->RowStride = postConvWidth;
533 }
534
535 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
536 /*assert(texImage->RowStride == postConvWidth);*/
537 }
538
539 /* Release the reference to a potentially orphaned buffer.
540 * Release any old malloced memory.
541 */
542 if (stImage->pt) {
543 pipe_texture_release(&stImage->pt);
544 assert(!texImage->Data);
545 }
546 else if (texImage->Data) {
547 _mesa_align_free(texImage->Data);
548 }
549
550 /* If this is the only mipmap level in the texture, could call
551 * bmBufferData with NULL data to free the old block and avoid
552 * waiting on any outstanding fences.
553 */
554 if (stObj->pt &&
555 /*stObj->pt->first_level == level &&*/
556 stObj->pt->last_level == level &&
557 stObj->pt->target != PIPE_TEXTURE_CUBE &&
558 !st_texture_match_image(stObj->pt, &stImage->base,
559 stImage->face, stImage->level)) {
560
561 DBG("release it\n");
562 pipe_texture_release(&stObj->pt);
563 assert(!stObj->pt);
564 }
565
566 if (!stObj->pt) {
567 guess_and_alloc_texture(ctx->st, stObj, stImage);
568 if (!stObj->pt) {
569 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
570 return;
571 }
572 }
573
574 assert(!stImage->pt);
575
576 if (stObj->pt &&
577 st_texture_match_image(stObj->pt, &stImage->base,
578 stImage->face, stImage->level)) {
579
580 pipe_texture_reference(&stImage->pt, stObj->pt);
581 assert(stImage->pt);
582 }
583
584 if (!stImage->pt)
585 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
586
587 #if 0 /* XXX FIX when st_buffer_objects are in place */
588 /* PBO fastpaths:
589 */
590 if (dims <= 2 &&
591 stImage->pt &&
592 intel_buffer_object(unpack->BufferObj) &&
593 check_pbo_format(internalFormat, format,
594 type, texImage->TexFormat)) {
595
596 DBG("trying pbo upload\n");
597
598
599
600 /* Otherwise, attempt to use the blitter for PBO image uploads.
601 */
602 if (try_pbo_upload(intel, stImage, unpack,
603 internalFormat,
604 width, height, format, type, pixels)) {
605 DBG("pbo upload succeeded\n");
606 return;
607 }
608
609 DBG("pbo upload failed\n");
610 }
611 #else
612 (void) try_pbo_upload;
613 (void) check_pbo_format;
614 #endif
615
616
617 /* st_CopyTexImage calls this function with pixels == NULL, with
618 * the expectation that the texture will be set up but nothing
619 * more will be done. This is where those calls return:
620 */
621 if (compressed) {
622 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
623 unpack,
624 "glCompressedTexImage");
625 } else {
626 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
627 format, type,
628 pixels, unpack, "glTexImage");
629 }
630 if (!pixels)
631 return;
632
633 if (stImage->pt) {
634 texImage->Data = st_texture_image_map(ctx->st, stImage, 0);
635 dstRowStride = stImage->surface->pitch * stImage->surface->cpp;
636 }
637 else {
638 /* Allocate regular memory and store the image there temporarily. */
639 if (texImage->IsCompressed) {
640 sizeInBytes = texImage->CompressedSize;
641 dstRowStride =
642 _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat, width);
643 assert(dims != 3);
644 }
645 else {
646 dstRowStride = postConvWidth * texelBytes;
647 sizeInBytes = depth * dstRowStride * postConvHeight;
648 }
649
650 texImage->Data = malloc(sizeInBytes);
651 }
652
653 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
654 width, height, depth, width * texelBytes, dstRowStride);
655
656 /* Copy data. Would like to know when it's ok for us to eg. use
657 * the blitter to copy. Or, use the hardware to do the format
658 * conversion and copy:
659 */
660 if (compressed) {
661 memcpy(texImage->Data, pixels, imageSize);
662 }
663 else {
664 GLuint srcImageStride = _mesa_image_image_stride(unpack, width, height,
665 format, type);
666 int i;
667 const GLubyte *src = (const GLubyte *) pixels;
668
669 for (i = 0; i++ < depth;) {
670 if (!texImage->TexFormat->StoreImage(ctx, dims,
671 texImage->_BaseFormat,
672 texImage->TexFormat,
673 texImage->Data,
674 0, 0, 0, /* dstX/Y/Zoffset */
675 dstRowStride,
676 texImage->ImageOffsets,
677 width, height, 1,
678 format, type, src, unpack)) {
679 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
680 }
681
682 if (stImage->pt && i < depth) {
683 st_texture_image_unmap(stImage);
684 texImage->Data = st_texture_image_map(ctx->st, stImage, i);
685 src += srcImageStride;
686 }
687 }
688 }
689
690 _mesa_unmap_teximage_pbo(ctx, unpack);
691
692 if (stImage->pt) {
693 st_texture_image_unmap(stImage);
694 texImage->Data = NULL;
695 }
696
697 if (stObj->pt)
698 pipe->texture_update(pipe, stObj->pt, stImage->face, (1 << level));
699
700 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
701 ctx->Driver.GenerateMipmap(ctx, target, texObj);
702 }
703 }
704
705
706 static void
707 st_TexImage3D(GLcontext * ctx,
708 GLenum target, GLint level,
709 GLint internalFormat,
710 GLint width, GLint height, GLint depth,
711 GLint border,
712 GLenum format, GLenum type, const void *pixels,
713 const struct gl_pixelstore_attrib *unpack,
714 struct gl_texture_object *texObj,
715 struct gl_texture_image *texImage)
716 {
717 st_TexImage(ctx, 3, target, level,
718 internalFormat, width, height, depth, border,
719 format, type, pixels, unpack, texObj, texImage, 0, 0);
720 }
721
722
723 static void
724 st_TexImage2D(GLcontext * ctx,
725 GLenum target, GLint level,
726 GLint internalFormat,
727 GLint width, GLint height, GLint border,
728 GLenum format, GLenum type, const void *pixels,
729 const struct gl_pixelstore_attrib *unpack,
730 struct gl_texture_object *texObj,
731 struct gl_texture_image *texImage)
732 {
733 st_TexImage(ctx, 2, target, level,
734 internalFormat, width, height, 1, border,
735 format, type, pixels, unpack, texObj, texImage, 0, 0);
736 }
737
738
739 static void
740 st_TexImage1D(GLcontext * ctx,
741 GLenum target, GLint level,
742 GLint internalFormat,
743 GLint width, GLint border,
744 GLenum format, GLenum type, const void *pixels,
745 const struct gl_pixelstore_attrib *unpack,
746 struct gl_texture_object *texObj,
747 struct gl_texture_image *texImage)
748 {
749 st_TexImage(ctx, 1, target, level,
750 internalFormat, width, 1, 1, border,
751 format, type, pixels, unpack, texObj, texImage, 0, 0);
752 }
753
754
755 static void
756 st_CompressedTexImage2D( GLcontext *ctx, GLenum target, GLint level,
757 GLint internalFormat,
758 GLint width, GLint height, GLint border,
759 GLsizei imageSize, const GLvoid *data,
760 struct gl_texture_object *texObj,
761 struct gl_texture_image *texImage )
762 {
763 st_TexImage(ctx, 2, target, level,
764 internalFormat, width, height, 1, border,
765 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, 1);
766 }
767
768
769 /**
770 * Need to map texture image into memory before copying image data,
771 * then unmap it.
772 */
773 static void
774 st_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
775 GLenum format, GLenum type, GLvoid * pixels,
776 struct gl_texture_object *texObj,
777 struct gl_texture_image *texImage, int compressed)
778 {
779 struct st_texture_image *stImage = st_texture_image(texImage);
780 GLuint dstImageStride = _mesa_image_image_stride(&ctx->Pack, texImage->Width,
781 texImage->Height, format,
782 type);
783 GLuint depth;
784 int i;
785 GLubyte *dest;
786
787 /* Map */
788 if (stImage->pt) {
789 /* Image is stored in hardware format in a buffer managed by the
790 * kernel. Need to explicitly map and unmap it.
791 */
792 texImage->Data = st_texture_image_map(ctx->st, stImage, 0);
793 texImage->RowStride = stImage->surface->pitch;
794 }
795 else {
796 /* Otherwise, the image should actually be stored in
797 * texImage->Data. This is pretty confusing for
798 * everybody, I'd much prefer to separate the two functions of
799 * texImage->Data - storage for texture images in main memory
800 * and access (ie mappings) of images. In other words, we'd
801 * create a new texImage->Map field and leave Data simply for
802 * storage.
803 */
804 assert(texImage->Data);
805 }
806
807 depth = texImage->Depth;
808 texImage->Depth = 1;
809
810 dest = (GLubyte *) pixels;
811
812 for (i = 0; i++ < depth;) {
813 if (compressed) {
814 _mesa_get_compressed_teximage(ctx, target, level, dest,
815 texObj, texImage);
816 } else {
817 _mesa_get_teximage(ctx, target, level, format, type, dest,
818 texObj, texImage);
819 }
820
821 if (stImage->pt && i < depth) {
822 st_texture_image_unmap(stImage);
823 texImage->Data = st_texture_image_map(ctx->st, stImage, i);
824 dest += dstImageStride;
825 }
826 }
827
828 texImage->Depth = depth;
829
830 /* Unmap */
831 if (stImage->pt) {
832 st_texture_image_unmap(stImage);
833 texImage->Data = NULL;
834 }
835 }
836
837
838 static void
839 st_GetTexImage(GLcontext * ctx, GLenum target, GLint level,
840 GLenum format, GLenum type, GLvoid * pixels,
841 struct gl_texture_object *texObj,
842 struct gl_texture_image *texImage)
843 {
844 st_get_tex_image(ctx, target, level, format, type, pixels,
845 texObj, texImage, 0);
846 }
847
848
849 static void
850 st_GetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level,
851 GLvoid *pixels,
852 const struct gl_texture_object *texObj,
853 const struct gl_texture_image *texImage)
854 {
855 st_get_tex_image(ctx, target, level, 0, 0, pixels,
856 (struct gl_texture_object *) texObj,
857 (struct gl_texture_image *) texImage, 1);
858 }
859
860
861
862 static void
863 st_TexSubimage(GLcontext * ctx,
864 GLint dims,
865 GLenum target, GLint level,
866 GLint xoffset, GLint yoffset, GLint zoffset,
867 GLint width, GLint height, GLint depth,
868 GLenum format, GLenum type, const void *pixels,
869 const struct gl_pixelstore_attrib *packing,
870 struct gl_texture_object *texObj,
871 struct gl_texture_image *texImage)
872 {
873 struct pipe_context *pipe = ctx->st->pipe;
874 struct st_texture_object *stObj = st_texture_object(texObj);
875 struct st_texture_image *stImage = st_texture_image(texImage);
876 GLuint dstRowStride;
877 GLuint srcImageStride = _mesa_image_image_stride(packing, width, height,
878 format, type);
879 int i;
880 const GLubyte *src;
881
882 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
883 _mesa_lookup_enum_by_nr(target),
884 level, xoffset, yoffset, width, height);
885
886 pixels =
887 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format,
888 type, pixels, packing, "glTexSubImage2D");
889 if (!pixels)
890 return;
891
892 /* Map buffer if necessary. Need to lock to prevent other contexts
893 * from uploading the buffer under us.
894 */
895 if (stImage->pt) {
896 texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset);
897 dstRowStride = stImage->surface->pitch * stImage->surface->cpp;
898 }
899
900 if (!texImage->Data) {
901 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
902 return;
903 }
904
905 src = (const GLubyte *) pixels;
906
907 for (i = 0; i++ < depth;) {
908 if (!texImage->TexFormat->StoreImage(ctx, dims, texImage->_BaseFormat,
909 texImage->TexFormat,
910 texImage->Data,
911 xoffset, yoffset, 0,
912 dstRowStride,
913 texImage->ImageOffsets,
914 width, height, 1,
915 format, type, src, packing)) {
916 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
917 }
918
919 if (stImage->pt && i < depth) {
920 /* map next slice of 3D texture */
921 st_texture_image_unmap(stImage);
922 texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset + i);
923 src += srcImageStride;
924 }
925 }
926
927 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
928 ctx->Driver.GenerateMipmap(ctx, target, texObj);
929 }
930
931 _mesa_unmap_teximage_pbo(ctx, packing);
932
933 if (stImage->pt) {
934 st_texture_image_unmap(stImage);
935 texImage->Data = NULL;
936 }
937
938 pipe->texture_update(pipe, stObj->pt, stImage->face, (1 << level));
939 }
940
941
942
943 static void
944 st_TexSubImage3D(GLcontext * ctx,
945 GLenum target,
946 GLint level,
947 GLint xoffset, GLint yoffset, GLint zoffset,
948 GLsizei width, GLsizei height, GLsizei depth,
949 GLenum format, GLenum type,
950 const GLvoid * pixels,
951 const struct gl_pixelstore_attrib *packing,
952 struct gl_texture_object *texObj,
953 struct gl_texture_image *texImage)
954 {
955 st_TexSubimage(ctx, 3, target, level,
956 xoffset, yoffset, zoffset,
957 width, height, depth,
958 format, type, pixels, packing, texObj, texImage);
959 }
960
961
962
963 static void
964 st_TexSubImage2D(GLcontext * ctx,
965 GLenum target,
966 GLint level,
967 GLint xoffset, GLint yoffset,
968 GLsizei width, GLsizei height,
969 GLenum format, GLenum type,
970 const GLvoid * pixels,
971 const struct gl_pixelstore_attrib *packing,
972 struct gl_texture_object *texObj,
973 struct gl_texture_image *texImage)
974 {
975 st_TexSubimage(ctx, 2, target, level,
976 xoffset, yoffset, 0,
977 width, height, 1,
978 format, type, pixels, packing, texObj, texImage);
979 }
980
981
982 static void
983 st_TexSubImage1D(GLcontext * ctx,
984 GLenum target,
985 GLint level,
986 GLint xoffset,
987 GLsizei width,
988 GLenum format, GLenum type,
989 const GLvoid * pixels,
990 const struct gl_pixelstore_attrib *packing,
991 struct gl_texture_object *texObj,
992 struct gl_texture_image *texImage)
993 {
994 st_TexSubimage(ctx, 1, target, level,
995 xoffset, 0, 0,
996 width, 1, 1,
997 format, type, pixels, packing, texObj, texImage);
998 }
999
1000
1001
1002 /**
1003 * Return 0 for GL_TEXTURE_CUBE_MAP_POSITIVE_X,
1004 * 1 for GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
1005 * etc.
1006 * XXX duplicated from main/teximage.c
1007 */
1008 static uint
1009 texture_face(GLenum target)
1010 {
1011 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
1012 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)
1013 return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
1014 else
1015 return 0;
1016 }
1017
1018
1019
1020 /**
1021 * Do a CopyTexSubImage operation by mapping the source surface and
1022 * dest surface and using get_tile()/put_tile() to access the pixels/texels.
1023 *
1024 * Note: srcY=0=TOP of renderbuffer
1025 */
1026 static void
1027 fallback_copy_texsubimage(GLcontext *ctx,
1028 GLenum target,
1029 GLint level,
1030 struct st_renderbuffer *strb,
1031 struct st_texture_image *stImage,
1032 GLenum baseFormat,
1033 GLint destX, GLint destY, GLint destZ,
1034 GLint srcX, GLint srcY,
1035 GLsizei width, GLsizei height)
1036 {
1037 struct pipe_context *pipe = ctx->st->pipe;
1038 struct pipe_screen *screen = pipe->screen;
1039 const uint face = texture_face(target);
1040 struct pipe_texture *pt = stImage->pt;
1041 struct pipe_surface *src_surf, *dest_surf;
1042 GLint row, yStep;
1043
1044 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
1045
1046 /* determine bottom-to-top vs. top-to-bottom order */
1047 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1048 destY = height - 1 - destY;
1049 yStep = -1;
1050 }
1051 else {
1052 yStep = 1;
1053 }
1054
1055 src_surf = strb->surface;
1056
1057 dest_surf = screen->get_tex_surface(screen, pt, face, level, destZ);
1058
1059 assert(width <= MAX_WIDTH);
1060
1061 /*
1062 * To avoid a large temp memory allocation, do copy row by row.
1063 */
1064 if (baseFormat == GL_DEPTH_COMPONENT) {
1065 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
1066 ctx->Pixel.DepthBias != 0.0F);
1067
1068 for (row = 0; row < height; row++, srcY++, destY += yStep) {
1069 uint data[MAX_WIDTH];
1070 pipe_get_tile_z(pipe, src_surf, srcX, srcY, width, 1, data);
1071 if (scaleOrBias) {
1072 _mesa_scale_and_bias_depth_uint(ctx, width, data);
1073 }
1074 pipe_put_tile_z(pipe, dest_surf, destX, destY, width, 1, data);
1075 }
1076 }
1077 else {
1078 /* RGBA format */
1079 for (row = 0; row < height; row++, srcY++, destY += yStep) {
1080 float data[4 * MAX_WIDTH];
1081 pipe_get_tile_rgba(pipe, src_surf, srcX, srcY, width, 1, data);
1082 /* XXX we're ignoring convolution for now */
1083 if (ctx->_ImageTransferState) {
1084 _mesa_apply_rgba_transfer_ops(ctx,
1085 ctx->_ImageTransferState & ~IMAGE_CONVOLUTION_BIT,
1086 width, (GLfloat (*)[4]) data);
1087 }
1088 pipe_put_tile_rgba(pipe, dest_surf, destX, destY, width, 1, data);
1089 }
1090 }
1091 }
1092
1093
1094
1095
1096 /**
1097 * Do a CopyTex[Sub]Image using an optimized hardware (blit) path.
1098 * Note that the region to copy has already been clip tested.
1099 *
1100 * Note: srcY=0=Bottom of renderbuffer
1101 *
1102 * \return GL_TRUE if success, GL_FALSE if failure (use a fallback)
1103 */
1104 static void
1105 do_copy_texsubimage(GLcontext *ctx,
1106 GLenum target, GLint level,
1107 GLint destX, GLint destY, GLint destZ,
1108 GLint srcX, GLint srcY,
1109 GLsizei width, GLsizei height)
1110 {
1111 struct gl_texture_unit *texUnit =
1112 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1113 struct gl_texture_object *texObj =
1114 _mesa_select_tex_object(ctx, texUnit, target);
1115 struct gl_texture_image *texImage =
1116 _mesa_select_tex_image(ctx, texObj, target, level);
1117 struct st_texture_image *stImage = st_texture_image(texImage);
1118 struct st_texture_object *stObj = st_texture_object(texObj);
1119 GLenum baseFormat = texImage->InternalFormat;
1120 struct gl_framebuffer *fb = ctx->ReadBuffer;
1121 struct st_renderbuffer *strb;
1122 struct pipe_context *pipe = ctx->st->pipe;
1123 struct pipe_screen *screen = pipe->screen;
1124 struct pipe_surface *dest_surface;
1125 uint dest_format, src_format;
1126 uint do_flip = FALSE;
1127 GLboolean use_fallback = GL_TRUE;
1128
1129 (void) texImage;
1130
1131 /* determine if copying depth or color data */
1132 if (baseFormat == GL_DEPTH_COMPONENT) {
1133 strb = st_renderbuffer(fb->_DepthBuffer);
1134 }
1135 else if (baseFormat == GL_DEPTH_STENCIL_EXT) {
1136 strb = st_renderbuffer(fb->_StencilBuffer);
1137 }
1138 else {
1139 /* baseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1140 strb = st_renderbuffer(fb->_ColorReadBuffer);
1141 }
1142
1143 assert(strb);
1144 assert(strb->surface);
1145 assert(stImage->pt);
1146
1147 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1148 srcY = strb->Base.Height - srcY - height;
1149 do_flip = TRUE;
1150 }
1151
1152 src_format = strb->surface->format;
1153 dest_format = stImage->pt->format;
1154
1155 dest_surface = screen->get_tex_surface(screen, stImage->pt, stImage->face,
1156 stImage->level, destZ);
1157
1158 if (ctx->_ImageTransferState == 0x0 &&
1159 strb->surface->buffer &&
1160 dest_surface->buffer) {
1161 /* do blit-style copy */
1162
1163 /* XXX may need to invert image depending on window
1164 * vs. user-created FBO
1165 */
1166
1167 #if 0
1168 /* A bit of fiddling to get the blitter to work with -ve
1169 * pitches. But we get a nice inverted blit this way, so it's
1170 * worth it:
1171 */
1172 intelEmitCopyBlit(intel,
1173 stImage->pt->cpp,
1174 -src->pitch,
1175 src->buffer,
1176 src->height * src->pitch * src->cpp,
1177 stImage->pt->pitch,
1178 stImage->pt->region->buffer,
1179 dest_offset,
1180 x, y + height, dstx, dsty, width, height,
1181 GL_COPY); /* ? */
1182 #else
1183
1184 if (src_format == dest_format) {
1185 pipe->surface_copy(pipe,
1186 do_flip,
1187 /* dest */
1188 dest_surface,
1189 destX, destY,
1190 /* src */
1191 strb->surface,
1192 srcX, srcY,
1193 /* size */
1194 width, height);
1195 use_fallback = GL_FALSE;
1196 }
1197 else if (screen->is_format_supported(screen, strb->surface->format,
1198 PIPE_TEXTURE) &&
1199 screen->is_format_supported(screen, dest_surface->format,
1200 PIPE_SURFACE)) {
1201 util_blit_pixels(ctx->st->blit,
1202 strb->surface,
1203 srcX, do_flip ? srcY + height : srcY,
1204 srcX + width, do_flip ? srcY : srcY + height,
1205 dest_surface,
1206 destX, destY, destX + width, destY + height,
1207 0.0, PIPE_TEX_MIPFILTER_NEAREST);
1208 use_fallback = GL_FALSE;
1209 }
1210 #endif
1211 }
1212
1213 if (use_fallback) {
1214 fallback_copy_texsubimage(ctx, target, level,
1215 strb, stImage, baseFormat,
1216 destX, destY, destZ,
1217 srcX, srcY, width, height);
1218 }
1219
1220 pipe_surface_reference(&dest_surface, NULL);
1221
1222 pipe->texture_update(pipe, stObj->pt, stImage->face, (1 << level));
1223
1224 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
1225 ctx->Driver.GenerateMipmap(ctx, target, texObj);
1226 }
1227 }
1228
1229
1230
1231 static void
1232 st_CopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
1233 GLenum internalFormat,
1234 GLint x, GLint y, GLsizei width, GLint border)
1235 {
1236 struct gl_texture_unit *texUnit =
1237 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1238 struct gl_texture_object *texObj =
1239 _mesa_select_tex_object(ctx, texUnit, target);
1240 struct gl_texture_image *texImage =
1241 _mesa_select_tex_image(ctx, texObj, target, level);
1242
1243 #if 0
1244 if (border)
1245 goto fail;
1246 #endif
1247
1248 /* Setup or redefine the texture object, texture and texture
1249 * image. Don't populate yet.
1250 */
1251 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
1252 width, border,
1253 GL_RGBA, CHAN_TYPE, NULL,
1254 &ctx->DefaultPacking, texObj, texImage);
1255
1256 do_copy_texsubimage(ctx, target, level,
1257 0, 0, 0,
1258 x, y, width, 1);
1259 }
1260
1261
1262 static void
1263 st_CopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
1264 GLenum internalFormat,
1265 GLint x, GLint y, GLsizei width, GLsizei height,
1266 GLint border)
1267 {
1268 struct gl_texture_unit *texUnit =
1269 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1270 struct gl_texture_object *texObj =
1271 _mesa_select_tex_object(ctx, texUnit, target);
1272 struct gl_texture_image *texImage =
1273 _mesa_select_tex_image(ctx, texObj, target, level);
1274
1275 #if 0
1276 if (border)
1277 goto fail;
1278 #endif
1279
1280 /* Setup or redefine the texture object, texture and texture
1281 * image. Don't populate yet.
1282 */
1283 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
1284 width, height, border,
1285 GL_RGBA, CHAN_TYPE, NULL,
1286 &ctx->DefaultPacking, texObj, texImage);
1287
1288
1289 do_copy_texsubimage(ctx, target, level,
1290 0, 0, 0,
1291 x, y, width, height);
1292 }
1293
1294
1295 static void
1296 st_CopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
1297 GLint xoffset, GLint x, GLint y, GLsizei width)
1298 {
1299 const GLint yoffset = 0, zoffset = 0;
1300 const GLsizei height = 1;
1301 do_copy_texsubimage(ctx, target, level,
1302 xoffset, yoffset, zoffset,
1303 x, y, width, height);
1304 }
1305
1306
1307 static void
1308 st_CopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
1309 GLint xoffset, GLint yoffset,
1310 GLint x, GLint y, GLsizei width, GLsizei height)
1311 {
1312 const GLint zoffset = 0;
1313 do_copy_texsubimage(ctx, target, level,
1314 xoffset, yoffset, zoffset,
1315 x, y, width, height);
1316 }
1317
1318
1319 static void
1320 st_CopyTexSubImage3D(GLcontext * ctx, GLenum target, GLint level,
1321 GLint xoffset, GLint yoffset, GLint zoffset,
1322 GLint x, GLint y, GLsizei width, GLsizei height)
1323 {
1324 do_copy_texsubimage(ctx, target, level,
1325 xoffset, yoffset, zoffset,
1326 x, y, width, height);
1327 }
1328
1329
1330
1331
1332 /**
1333 * Compute which mipmap levels that really need to be sent to the hardware.
1334 * This depends on the base image size, GL_TEXTURE_MIN_LOD,
1335 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
1336 */
1337 static void
1338 calculate_first_last_level(struct st_texture_object *stObj)
1339 {
1340 struct gl_texture_object *tObj = &stObj->base;
1341 const struct gl_texture_image *const baseImage =
1342 tObj->Image[0][tObj->BaseLevel];
1343
1344 /* These must be signed values. MinLod and MaxLod can be negative numbers,
1345 * and having firstLevel and lastLevel as signed prevents the need for
1346 * extra sign checks.
1347 */
1348 int firstLevel;
1349 int lastLevel;
1350
1351 /* Yes, this looks overly complicated, but it's all needed.
1352 */
1353 switch (tObj->Target) {
1354 case GL_TEXTURE_1D:
1355 case GL_TEXTURE_2D:
1356 case GL_TEXTURE_3D:
1357 case GL_TEXTURE_CUBE_MAP:
1358 if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) {
1359 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
1360 */
1361 firstLevel = lastLevel = tObj->BaseLevel;
1362 }
1363 else {
1364 firstLevel = 0;
1365 lastLevel = MIN2(tObj->MaxLevel - tObj->BaseLevel, baseImage->MaxLog2);
1366 }
1367 break;
1368 case GL_TEXTURE_RECTANGLE_NV:
1369 case GL_TEXTURE_4D_SGIS:
1370 firstLevel = lastLevel = 0;
1371 break;
1372 default:
1373 return;
1374 }
1375
1376 stObj->lastLevel = lastLevel;
1377 }
1378
1379
1380 static void
1381 copy_image_data_to_texture(struct st_context *st,
1382 struct st_texture_object *stObj,
1383 GLuint dstLevel,
1384 struct st_texture_image *stImage)
1385 {
1386 if (stImage->pt) {
1387 /* Copy potentially with the blitter:
1388 */
1389 st_texture_image_copy(st->pipe,
1390 stObj->pt, dstLevel, /* dest texture, level */
1391 stImage->pt, /* src texture */
1392 stImage->face
1393 );
1394
1395 pipe_texture_release(&stImage->pt);
1396 }
1397 else if (stImage->base.Data) {
1398 assert(stImage->base.Data != NULL);
1399
1400 /* More straightforward upload.
1401 */
1402 st_texture_image_data(st->pipe,
1403 stObj->pt,
1404 stImage->face,
1405 dstLevel,
1406 stImage->base.Data,
1407 stImage->base.RowStride,
1408 stImage->base.RowStride *
1409 stImage->base.Height);
1410 _mesa_align_free(stImage->base.Data);
1411 stImage->base.Data = NULL;
1412 }
1413
1414 pipe_texture_reference(&stImage->pt, stObj->pt);
1415 }
1416
1417
1418 /**
1419 * Called during state validation. When this function is finished,
1420 * the texture object should be ready for rendering.
1421 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1422 */
1423 GLboolean
1424 st_finalize_texture(GLcontext *ctx,
1425 struct pipe_context *pipe,
1426 struct gl_texture_object *tObj,
1427 GLboolean *needFlush)
1428 {
1429 struct st_texture_object *stObj = st_texture_object(tObj);
1430 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1431 int comp_byte = 0;
1432 int cpp;
1433 GLuint face;
1434 struct st_texture_image *firstImage;
1435
1436 *needFlush = GL_FALSE;
1437
1438 /* We know/require this is true by now:
1439 */
1440 assert(stObj->base._Complete);
1441
1442 /* What levels must the texture include at a minimum?
1443 */
1444 calculate_first_last_level(stObj);
1445 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1446
1447 #if 0
1448 /* Fallback case:
1449 */
1450 if (firstImage->base.Border) {
1451 if (stObj->pt) {
1452 pipe_texture_release(&stObj->pt);
1453 }
1454 return GL_FALSE;
1455 }
1456 #endif
1457
1458 /* If both firstImage and stObj point to a texture which can contain
1459 * all active images, favour firstImage. Note that because of the
1460 * completeness requirement, we know that the image dimensions
1461 * will match.
1462 */
1463 if (firstImage->pt &&
1464 firstImage->pt != stObj->pt &&
1465 firstImage->pt->last_level >= stObj->lastLevel) {
1466
1467 if (stObj->pt)
1468 pipe_texture_release(&stObj->pt);
1469
1470 pipe_texture_reference(&stObj->pt, firstImage->pt);
1471 }
1472
1473 if (firstImage->base.IsCompressed) {
1474 comp_byte = compressed_num_bytes(firstImage->base.TexFormat->MesaFormat);
1475 cpp = comp_byte;
1476 }
1477 else {
1478 cpp = firstImage->base.TexFormat->TexelBytes;
1479 }
1480
1481 /* Check texture can hold all active levels. Check texture matches
1482 * target, imageFormat, etc.
1483 */
1484 if (stObj->pt &&
1485 (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1486 stObj->pt->format !=
1487 st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat) ||
1488 stObj->pt->last_level != stObj->lastLevel ||
1489 stObj->pt->width[0] != firstImage->base.Width2 ||
1490 stObj->pt->height[0] != firstImage->base.Height2 ||
1491 stObj->pt->depth[0] != firstImage->base.Depth2 ||
1492 stObj->pt->cpp != cpp ||
1493 stObj->pt->compressed != firstImage->base.IsCompressed)) {
1494 pipe_texture_release(&stObj->pt);
1495 }
1496
1497
1498 /* May need to create a new texture:
1499 */
1500 if (!stObj->pt) {
1501 stObj->pt = st_texture_create(ctx->st,
1502 gl_target_to_pipe(stObj->base.Target),
1503 st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat),
1504 stObj->lastLevel,
1505 firstImage->base.Width2,
1506 firstImage->base.Height2,
1507 firstImage->base.Depth2,
1508 comp_byte);
1509 if (!stObj->pt) {
1510 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1511 return GL_FALSE;
1512 }
1513 }
1514
1515 /* Pull in any images not in the object's texture:
1516 */
1517 for (face = 0; face < nr_faces; face++) {
1518 GLuint level;
1519 for (level = 0; level <= stObj->lastLevel; level++) {
1520 struct st_texture_image *stImage =
1521 st_texture_image(stObj->base.Image[face][stObj->base.BaseLevel + level]);
1522
1523 /* Need to import images in main memory or held in other textures.
1524 */
1525 if (stImage && stObj->pt != stImage->pt) {
1526 copy_image_data_to_texture(ctx->st, stObj, level, stImage);
1527 *needFlush = GL_TRUE;
1528 pipe->texture_update(pipe, stObj->pt, face, (1 << level));
1529 }
1530 }
1531 }
1532
1533 return GL_TRUE;
1534 }
1535
1536
1537
1538
1539 void
1540 st_init_texture_functions(struct dd_function_table *functions)
1541 {
1542 functions->ChooseTextureFormat = st_ChooseTextureFormat;
1543 functions->TexImage1D = st_TexImage1D;
1544 functions->TexImage2D = st_TexImage2D;
1545 functions->TexImage3D = st_TexImage3D;
1546 functions->TexSubImage1D = st_TexSubImage1D;
1547 functions->TexSubImage2D = st_TexSubImage2D;
1548 functions->TexSubImage3D = st_TexSubImage3D;
1549 functions->CopyTexImage1D = st_CopyTexImage1D;
1550 functions->CopyTexImage2D = st_CopyTexImage2D;
1551 functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
1552 functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
1553 functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
1554 functions->GenerateMipmap = st_generate_mipmap;
1555
1556 functions->GetTexImage = st_GetTexImage;
1557
1558 /* compressed texture functions */
1559 functions->CompressedTexImage2D = st_CompressedTexImage2D;
1560 functions->GetCompressedTexImage = st_GetCompressedTexImage;
1561 functions->CompressedTextureSize = _mesa_compressed_texture_size;
1562
1563 functions->NewTextureObject = st_NewTextureObject;
1564 functions->NewTextureImage = st_NewTextureImage;
1565 functions->DeleteTexture = st_DeleteTextureObject;
1566 functions->FreeTexImageData = st_FreeTextureImageData;
1567 functions->UpdateTexturePalette = 0;
1568 functions->IsTextureResident = st_IsTextureResident;
1569
1570 functions->TextureMemCpy = do_memcpy;
1571
1572 /* XXX Temporary until we can query pipe's texture sizes */
1573 functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1574 }