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