992723afba872f0c697ddc3405898d888c715deb
[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 /* There are actually quite a few combinations this will work for,
383 * more than what I've listed here.
384 */
385 static GLboolean
386 check_pbo_format(GLint internalFormat,
387 GLenum format, GLenum type,
388 const struct gl_texture_format *mesa_format)
389 {
390 switch (internalFormat) {
391 case 4:
392 case GL_RGBA:
393 return (format == GL_BGRA &&
394 (type == GL_UNSIGNED_BYTE ||
395 type == GL_UNSIGNED_INT_8_8_8_8_REV) &&
396 mesa_format == &_mesa_texformat_argb8888);
397 case 3:
398 case GL_RGB:
399 return (format == GL_RGB &&
400 type == GL_UNSIGNED_SHORT_5_6_5 &&
401 mesa_format == &_mesa_texformat_rgb565);
402 case GL_YCBCR_MESA:
403 return (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE);
404 default:
405 return GL_FALSE;
406 }
407 }
408
409
410 /* XXX: Do this for TexSubImage also:
411 */
412 static GLboolean
413 try_pbo_upload(GLcontext *ctx,
414 struct st_texture_image *stImage,
415 const struct gl_pixelstore_attrib *unpack,
416 GLint internalFormat,
417 GLint width, GLint height,
418 GLenum format, GLenum type, const void *pixels)
419 {
420 return GL_FALSE; /* XXX fix flushing/locking/blitting below */
421 #if 000
422 struct intel_context *intel = intel_context(ctx);
423 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
424 GLuint src_offset, src_stride;
425 GLuint dst_offset, dst_stride;
426
427 if (!pbo ||
428 ctx._ImageTransferState ||
429 unpack->SkipPixels || unpack->SkipRows) {
430 _mesa_printf("%s: failure 1\n", __FUNCTION__);
431 return GL_FALSE;
432 }
433
434 src_offset = (GLuint) pixels;
435
436 if (unpack->RowLength > 0)
437 src_stride = unpack->RowLength;
438 else
439 src_stride = width;
440
441 dst_offset = st_texture_image_offset(stImage->pt,
442 stImage->face,
443 stImage->level);
444
445 dst_stride = stImage->pt->pitch;
446
447 {
448 struct _DriBufferObject *src_buffer =
449 intel_bufferobj_buffer(intel, pbo, INTEL_READ);
450
451 /* Temporary hack: cast to _DriBufferObject:
452 */
453 struct _DriBufferObject *dst_buffer =
454 (struct _DriBufferObject *)stImage->pt->region->buffer;
455
456
457 intelEmitCopyBlit(intel,
458 stImage->pt->cpp,
459 src_stride, src_buffer, src_offset,
460 dst_stride, dst_buffer, dst_offset,
461 0, 0, 0, 0, width, height,
462 GL_COPY);
463 }
464
465 return GL_TRUE;
466 #endif
467 }
468
469
470
471 static void
472 st_TexImage(GLcontext * ctx,
473 GLint dims,
474 GLenum target, GLint level,
475 GLint internalFormat,
476 GLint width, GLint height, GLint depth,
477 GLint border,
478 GLenum format, GLenum type, const void *pixels,
479 const struct gl_pixelstore_attrib *unpack,
480 struct gl_texture_object *texObj,
481 struct gl_texture_image *texImage,
482 GLsizei imageSize, int compressed)
483 {
484 struct st_texture_object *stObj = st_texture_object(texObj);
485 struct st_texture_image *stImage = st_texture_image(texImage);
486 GLint postConvWidth = width;
487 GLint postConvHeight = height;
488 GLint texelBytes, sizeInBytes;
489 GLuint dstRowStride;
490
491
492 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
493 _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
494
495 stImage->face = _mesa_tex_target_to_face(target);
496 stImage->level = level;
497
498 if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) {
499 _mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth,
500 &postConvHeight);
501 }
502
503 /* choose the texture format */
504 texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat,
505 format, type);
506
507 _mesa_set_fetch_functions(texImage, dims);
508
509 if (texImage->TexFormat->TexelBytes == 0) {
510 /* must be a compressed format */
511 texelBytes = 0;
512 texImage->IsCompressed = GL_TRUE;
513 texImage->CompressedSize =
514 ctx->Driver.CompressedTextureSize(ctx, texImage->Width,
515 texImage->Height, texImage->Depth,
516 texImage->TexFormat->MesaFormat);
517 }
518 else {
519 texelBytes = texImage->TexFormat->TexelBytes;
520
521 /* Minimum pitch of 32 bytes */
522 if (postConvWidth * texelBytes < 32) {
523 postConvWidth = 32 / texelBytes;
524 texImage->RowStride = postConvWidth;
525 }
526
527 assert(texImage->RowStride == postConvWidth);
528 }
529
530 /* Release the reference to a potentially orphaned buffer.
531 * Release any old malloced memory.
532 */
533 if (stImage->pt) {
534 ctx->st->pipe->texture_release(ctx->st->pipe, &stImage->pt);
535 assert(!texImage->Data);
536 }
537 else if (texImage->Data) {
538 _mesa_align_free(texImage->Data);
539 }
540
541 /* If this is the only texture image in the texture, could call
542 * bmBufferData with NULL data to free the old block and avoid
543 * waiting on any outstanding fences.
544 */
545 if (stObj->pt &&
546 stObj->pt->first_level == level &&
547 stObj->pt->last_level == level &&
548 stObj->pt->target != PIPE_TEXTURE_CUBE &&
549 !st_texture_match_image(stObj->pt, &stImage->base,
550 stImage->face, stImage->level)) {
551
552 DBG("release it\n");
553 ctx->st->pipe->texture_release(ctx->st->pipe, &stObj->pt);
554 assert(!stObj->pt);
555 }
556
557 if (!stObj->pt) {
558 guess_and_alloc_texture(ctx->st, stObj, stImage);
559 if (!stObj->pt) {
560 DBG("guess_and_alloc_texture: failed\n");
561 }
562 }
563
564 assert(!stImage->pt);
565
566 if (stObj->pt &&
567 st_texture_match_image(stObj->pt, &stImage->base,
568 stImage->face, stImage->level)) {
569
570 pipe_texture_reference(ctx->st->pipe, &stImage->pt, stObj->pt);
571 assert(stImage->pt);
572 }
573
574 if (!stImage->pt)
575 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
576
577 #if 0 /* XXX FIX when st_buffer_objects are in place */
578 /* PBO fastpaths:
579 */
580 if (dims <= 2 &&
581 stImage->pt &&
582 intel_buffer_object(unpack->BufferObj) &&
583 check_pbo_format(internalFormat, format,
584 type, texImage->TexFormat)) {
585
586 DBG("trying pbo upload\n");
587
588
589
590 /* Otherwise, attempt to use the blitter for PBO image uploads.
591 */
592 if (try_pbo_upload(intel, stImage, unpack,
593 internalFormat,
594 width, height, format, type, pixels)) {
595 DBG("pbo upload succeeded\n");
596 return;
597 }
598
599 DBG("pbo upload failed\n");
600 }
601 #else
602 (void) try_pbo_upload;
603 (void) check_pbo_format;
604 #endif
605
606
607 /* st_CopyTexImage calls this function with pixels == NULL, with
608 * the expectation that the texture will be set up but nothing
609 * more will be done. This is where those calls return:
610 */
611 if (compressed) {
612 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
613 unpack,
614 "glCompressedTexImage");
615 } else {
616 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
617 format, type,
618 pixels, unpack, "glTexImage");
619 }
620 if (!pixels)
621 return;
622
623 if (stImage->pt) {
624 texImage->Data = st_texture_image_map(ctx->st, stImage, 0);
625 dstRowStride = stImage->surface->pitch * stImage->surface->cpp;
626 }
627 else {
628 /* Allocate regular memory and store the image there temporarily. */
629 if (texImage->IsCompressed) {
630 sizeInBytes = texImage->CompressedSize;
631 dstRowStride =
632 _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat, width);
633 assert(dims != 3);
634 }
635 else {
636 dstRowStride = postConvWidth * texelBytes;
637 sizeInBytes = depth * dstRowStride * postConvHeight;
638 }
639
640 texImage->Data = malloc(sizeInBytes);
641 }
642
643 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
644 width, height, depth, width * texelBytes, dstRowStride);
645
646 /* Copy data. Would like to know when it's ok for us to eg. use
647 * the blitter to copy. Or, use the hardware to do the format
648 * conversion and copy:
649 */
650 if (compressed) {
651 memcpy(texImage->Data, pixels, imageSize);
652 }
653 else {
654 GLuint srcImageStride = _mesa_image_image_stride(unpack, width, height,
655 format, type);
656 int i;
657 const GLubyte *src = (const GLubyte *) pixels;
658
659 for (i = 0; i++ < depth;) {
660 if (!texImage->TexFormat->StoreImage(ctx, dims,
661 texImage->_BaseFormat,
662 texImage->TexFormat,
663 texImage->Data,
664 0, 0, 0, /* dstX/Y/Zoffset */
665 dstRowStride,
666 texImage->ImageOffsets,
667 width, height, 1,
668 format, type, src, unpack)) {
669 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
670 }
671
672 if (stImage->pt && i < depth) {
673 st_texture_image_unmap(stImage);
674 texImage->Data = st_texture_image_map(ctx->st, stImage, i);
675 src += srcImageStride;
676 }
677 }
678 }
679
680 _mesa_unmap_teximage_pbo(ctx, unpack);
681
682 if (stImage->pt) {
683 st_texture_image_unmap(stImage);
684 texImage->Data = NULL;
685 }
686
687 #if 01
688 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
689 ctx->Driver.GenerateMipmap(ctx, target, texObj);
690 }
691 #endif
692 }
693
694
695 static void
696 st_TexImage3D(GLcontext * ctx,
697 GLenum target, GLint level,
698 GLint internalFormat,
699 GLint width, GLint height, GLint depth,
700 GLint border,
701 GLenum format, GLenum type, const void *pixels,
702 const struct gl_pixelstore_attrib *unpack,
703 struct gl_texture_object *texObj,
704 struct gl_texture_image *texImage)
705 {
706 st_TexImage(ctx, 3, target, level,
707 internalFormat, width, height, depth, border,
708 format, type, pixels, unpack, texObj, texImage, 0, 0);
709 }
710
711
712 static void
713 st_TexImage2D(GLcontext * ctx,
714 GLenum target, GLint level,
715 GLint internalFormat,
716 GLint width, GLint height, GLint border,
717 GLenum format, GLenum type, const void *pixels,
718 const struct gl_pixelstore_attrib *unpack,
719 struct gl_texture_object *texObj,
720 struct gl_texture_image *texImage)
721 {
722 st_TexImage(ctx, 2, target, level,
723 internalFormat, width, height, 1, border,
724 format, type, pixels, unpack, texObj, texImage, 0, 0);
725 }
726
727
728 static void
729 st_TexImage1D(GLcontext * ctx,
730 GLenum target, GLint level,
731 GLint internalFormat,
732 GLint width, GLint border,
733 GLenum format, GLenum type, const void *pixels,
734 const struct gl_pixelstore_attrib *unpack,
735 struct gl_texture_object *texObj,
736 struct gl_texture_image *texImage)
737 {
738 st_TexImage(ctx, 1, target, level,
739 internalFormat, width, 1, 1, border,
740 format, type, pixels, unpack, texObj, texImage, 0, 0);
741 }
742
743
744 static void
745 st_CompressedTexImage2D( GLcontext *ctx, GLenum target, GLint level,
746 GLint internalFormat,
747 GLint width, GLint height, GLint border,
748 GLsizei imageSize, const GLvoid *data,
749 struct gl_texture_object *texObj,
750 struct gl_texture_image *texImage )
751 {
752 st_TexImage(ctx, 2, target, level,
753 internalFormat, width, height, 1, border,
754 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, 1);
755 }
756
757
758 /**
759 * Need to map texture image into memory before copying image data,
760 * then unmap it.
761 */
762 static void
763 st_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
764 GLenum format, GLenum type, GLvoid * pixels,
765 struct gl_texture_object *texObj,
766 struct gl_texture_image *texImage, int compressed)
767 {
768 struct st_texture_image *stImage = st_texture_image(texImage);
769 GLuint dstImageStride = _mesa_image_image_stride(&ctx->Pack, texImage->Width,
770 texImage->Height, format,
771 type);
772 GLuint depth;
773 int i;
774 GLubyte *dest;
775
776 /* Map */
777 if (stImage->pt) {
778 /* Image is stored in hardware format in a buffer managed by the
779 * kernel. Need to explicitly map and unmap it.
780 */
781 texImage->Data = st_texture_image_map(ctx->st, stImage, 0);
782 texImage->RowStride = stImage->surface->pitch;
783 }
784 else {
785 /* Otherwise, the image should actually be stored in
786 * texImage->Data. This is pretty confusing for
787 * everybody, I'd much prefer to separate the two functions of
788 * texImage->Data - storage for texture images in main memory
789 * and access (ie mappings) of images. In other words, we'd
790 * create a new texImage->Map field and leave Data simply for
791 * storage.
792 */
793 assert(texImage->Data);
794 }
795
796 depth = texImage->Depth;
797 texImage->Depth = 1;
798
799 dest = (GLubyte *) pixels;
800
801 for (i = 0; i++ < depth;) {
802 if (compressed) {
803 _mesa_get_compressed_teximage(ctx, target, level, dest,
804 texObj, texImage);
805 } else {
806 _mesa_get_teximage(ctx, target, level, format, type, dest,
807 texObj, texImage);
808 }
809
810 if (stImage->pt && i < depth) {
811 st_texture_image_unmap(stImage);
812 texImage->Data = st_texture_image_map(ctx->st, stImage, i);
813 dest += dstImageStride;
814 }
815 }
816
817 texImage->Depth = depth;
818
819 /* Unmap */
820 if (stImage->pt) {
821 st_texture_image_unmap(stImage);
822 texImage->Data = NULL;
823 }
824 }
825
826
827 static void
828 st_GetTexImage(GLcontext * ctx, GLenum target, GLint level,
829 GLenum format, GLenum type, GLvoid * pixels,
830 struct gl_texture_object *texObj,
831 struct gl_texture_image *texImage)
832 {
833 st_get_tex_image(ctx, target, level, format, type, pixels,
834 texObj, texImage, 0);
835 }
836
837
838 static void
839 st_GetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level,
840 GLvoid *pixels,
841 const struct gl_texture_object *texObj,
842 const struct gl_texture_image *texImage)
843 {
844 st_get_tex_image(ctx, target, level, 0, 0, pixels,
845 (struct gl_texture_object *) texObj,
846 (struct gl_texture_image *) texImage, 1);
847 }
848
849
850
851 static void
852 st_TexSubimage(GLcontext * ctx,
853 GLint dims,
854 GLenum target, GLint level,
855 GLint xoffset, GLint yoffset, GLint zoffset,
856 GLint width, GLint height, GLint depth,
857 GLenum format, GLenum type, const void *pixels,
858 const struct gl_pixelstore_attrib *packing,
859 struct gl_texture_object *texObj,
860 struct gl_texture_image *texImage)
861 {
862 struct st_texture_image *stImage = st_texture_image(texImage);
863 GLuint dstRowStride;
864 GLuint srcImageStride = _mesa_image_image_stride(packing, width, height,
865 format, type);
866 int i;
867 const GLubyte *src;
868
869 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
870 _mesa_lookup_enum_by_nr(target),
871 level, xoffset, yoffset, width, height);
872
873 pixels =
874 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format,
875 type, pixels, packing, "glTexSubImage2D");
876 if (!pixels)
877 return;
878
879 /* Map buffer if necessary. Need to lock to prevent other contexts
880 * from uploading the buffer under us.
881 */
882 if (stImage->pt) {
883 texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset);
884 dstRowStride = stImage->surface->pitch * stImage->surface->cpp;
885 }
886
887 src = (const GLubyte *) pixels;
888
889 for (i = 0; i++ < depth;) {
890 if (!texImage->TexFormat->StoreImage(ctx, dims, texImage->_BaseFormat,
891 texImage->TexFormat,
892 texImage->Data,
893 xoffset, yoffset, 0,
894 dstRowStride,
895 texImage->ImageOffsets,
896 width, height, 1,
897 format, type, src, packing)) {
898 _mesa_error(ctx, GL_OUT_OF_MEMORY, "st_TexSubImage");
899 }
900
901 if (stImage->pt && i < depth) {
902 st_texture_image_unmap(stImage);
903 texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset + i);
904 src += srcImageStride;
905 }
906 }
907
908 #if 0
909 /* GL_SGIS_generate_mipmap */
910 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
911 _mesa_generate_mipmap(ctx, target,
912 &ctx->Texture.Unit[ctx->Texture.CurrentUnit],
913 texObj);
914 }
915 #endif
916
917 _mesa_unmap_teximage_pbo(ctx, packing);
918
919 if (stImage->pt) {
920 st_texture_image_unmap(stImage);
921 texImage->Data = NULL;
922 }
923 }
924
925
926
927 static void
928 st_TexSubImage3D(GLcontext * ctx,
929 GLenum target,
930 GLint level,
931 GLint xoffset, GLint yoffset, GLint zoffset,
932 GLsizei width, GLsizei height, GLsizei depth,
933 GLenum format, GLenum type,
934 const GLvoid * pixels,
935 const struct gl_pixelstore_attrib *packing,
936 struct gl_texture_object *texObj,
937 struct gl_texture_image *texImage)
938 {
939 st_TexSubimage(ctx, 3, target, level,
940 xoffset, yoffset, zoffset,
941 width, height, depth,
942 format, type, pixels, packing, texObj, texImage);
943 }
944
945
946
947 static void
948 st_TexSubImage2D(GLcontext * ctx,
949 GLenum target,
950 GLint level,
951 GLint xoffset, GLint yoffset,
952 GLsizei width, GLsizei height,
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, 2, target, level,
960 xoffset, yoffset, 0,
961 width, height, 1,
962 format, type, pixels, packing, texObj, texImage);
963 }
964
965
966 static void
967 st_TexSubImage1D(GLcontext * ctx,
968 GLenum target,
969 GLint level,
970 GLint xoffset,
971 GLsizei width,
972 GLenum format, GLenum type,
973 const GLvoid * pixels,
974 const struct gl_pixelstore_attrib *packing,
975 struct gl_texture_object *texObj,
976 struct gl_texture_image *texImage)
977 {
978 st_TexSubimage(ctx, 1, target, level,
979 xoffset, 0, 0,
980 width, 1, 1,
981 format, type, pixels, packing, texObj, texImage);
982 }
983
984
985
986 /**
987 * Return 0 for GL_TEXTURE_CUBE_MAP_POSITIVE_X,
988 * 1 for GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
989 * etc.
990 * XXX duplicated from main/teximage.c
991 */
992 static uint
993 texture_face(GLenum target)
994 {
995 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
996 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)
997 return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
998 else
999 return 0;
1000 }
1001
1002
1003
1004 /**
1005 * Do a CopyTexSubImage operation by mapping the source surface and
1006 * dest surface and using get_tile()/put_tile() to access the pixels/texels.
1007 *
1008 * Note: srcY=0=TOP of renderbuffer
1009 */
1010 static void
1011 fallback_copy_texsubimage(GLcontext *ctx,
1012 GLenum target,
1013 GLint level,
1014 struct st_renderbuffer *strb,
1015 struct st_texture_image *stImage,
1016 GLenum baseFormat,
1017 GLint destX, GLint destY, GLint destZ,
1018 GLint srcX, GLint srcY,
1019 GLsizei width, GLsizei height)
1020 {
1021 struct pipe_context *pipe = ctx->st->pipe;
1022 const uint face = texture_face(target);
1023 struct pipe_texture *pt = stImage->pt;
1024 struct pipe_surface *src_surf, *dest_surf;
1025 GLfloat *data;
1026 GLint row, yStep;
1027
1028 /* determine bottom-to-top vs. top-to-bottom order */
1029 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1030 destY = height - 1 - destY;
1031 yStep = -1;
1032 }
1033 else {
1034 yStep = 1;
1035 }
1036
1037 src_surf = strb->surface;
1038
1039 dest_surf = pipe->get_tex_surface(pipe, pt,
1040 face, level, destZ);
1041
1042 /* buffer for one row */
1043 data = (GLfloat *) malloc(width * 4 * sizeof(GLfloat));
1044
1045 /* do copy row by row */
1046 for (row = 0; row < height; row++) {
1047 pipe_get_tile_rgba(pipe, src_surf, srcX, srcY + row, width, 1, data);
1048
1049 /* XXX we're ignoring convolution for now */
1050 if (ctx->_ImageTransferState) {
1051 _mesa_apply_rgba_transfer_ops(ctx,
1052 ctx->_ImageTransferState & ~IMAGE_CONVOLUTION_BIT,
1053 width, (GLfloat (*)[4])data);
1054 }
1055
1056 pipe_put_tile_rgba(pipe, dest_surf, destX, destY, width, 1, data);
1057 destY += yStep;
1058 }
1059
1060 free(data);
1061 }
1062
1063
1064
1065
1066 /**
1067 * Do a CopyTex[Sub]Image using an optimized hardware (blit) path.
1068 * Note that the region to copy has already been clip tested.
1069 *
1070 * Note: srcY=0=Bottom of renderbuffer
1071 *
1072 * \return GL_TRUE if success, GL_FALSE if failure (use a fallback)
1073 */
1074 static void
1075 do_copy_texsubimage(GLcontext *ctx,
1076 GLenum target, GLint level,
1077 GLint destX, GLint destY, GLint destZ,
1078 GLint srcX, GLint srcY,
1079 GLsizei width, GLsizei height)
1080 {
1081 struct gl_texture_unit *texUnit =
1082 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1083 struct gl_texture_object *texObj =
1084 _mesa_select_tex_object(ctx, texUnit, target);
1085 struct gl_texture_image *texImage =
1086 _mesa_select_tex_image(ctx, texObj, target, level);
1087 struct st_texture_image *stImage = st_texture_image(texImage);
1088 GLenum baseFormat = texImage->InternalFormat;
1089 struct gl_framebuffer *fb = ctx->ReadBuffer;
1090 struct st_renderbuffer *strb;
1091 struct pipe_context *pipe = ctx->st->pipe;
1092 struct pipe_surface *dest_surface;
1093 uint dest_format, src_format;
1094
1095 (void) texImage;
1096
1097 /* determine if copying depth or color data */
1098 if (baseFormat == GL_DEPTH_COMPONENT) {
1099 strb = st_renderbuffer(fb->_DepthBuffer);
1100 }
1101 else if (baseFormat == GL_DEPTH_STENCIL_EXT) {
1102 strb = st_renderbuffer(fb->_StencilBuffer);
1103 }
1104 else {
1105 /* baseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1106 strb = st_renderbuffer(fb->_ColorReadBuffer);
1107 }
1108
1109 assert(strb);
1110 assert(strb->surface);
1111 assert(stImage->pt);
1112
1113 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
1114 srcY = strb->Base.Height - srcY - height;
1115 }
1116
1117 src_format = strb->surface->format;
1118 dest_format = stImage->pt->format;
1119
1120 dest_surface = pipe->get_tex_surface(pipe, stImage->pt, stImage->face,
1121 stImage->level, destZ);
1122
1123 if (src_format == dest_format &&
1124 ctx->_ImageTransferState == 0x0 &&
1125 strb->surface->buffer &&
1126 dest_surface->buffer &&
1127 strb->surface->cpp == stImage->pt->cpp) {
1128 /* do blit-style copy */
1129
1130 /* XXX may need to invert image depending on window
1131 * vs. user-created FBO
1132 */
1133
1134 #if 0
1135 /* A bit of fiddling to get the blitter to work with -ve
1136 * pitches. But we get a nice inverted blit this way, so it's
1137 * worth it:
1138 */
1139 intelEmitCopyBlit(intel,
1140 stImage->pt->cpp,
1141 -src->pitch,
1142 src->buffer,
1143 src->height * src->pitch * src->cpp,
1144 stImage->pt->pitch,
1145 stImage->pt->region->buffer,
1146 dest_offset,
1147 x, y + height, dstx, dsty, width, height,
1148 GL_COPY); /* ? */
1149 #else
1150
1151 pipe->surface_copy(pipe,
1152 /* dest */
1153 dest_surface,
1154 destX, destY,
1155 /* src */
1156 strb->surface,
1157 srcX, srcY,
1158 /* size */
1159 width, height);
1160 #endif
1161 }
1162 else {
1163 fallback_copy_texsubimage(ctx, target, level,
1164 strb, stImage, baseFormat,
1165 destX, destY, destZ,
1166 srcX, srcY, width, height);
1167 }
1168
1169 pipe_surface_reference(&dest_surface, NULL);
1170
1171 #if 0
1172 /* GL_SGIS_generate_mipmap -- this can be accelerated now.
1173 * XXX Add a ctx->Driver.GenerateMipmaps() function?
1174 */
1175 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
1176 intel_generate_mipmap(ctx, target,
1177 &ctx->Texture.Unit[ctx->Texture.CurrentUnit],
1178 texObj);
1179 }
1180 #endif
1181
1182 }
1183
1184
1185
1186 static void
1187 st_CopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
1188 GLenum internalFormat,
1189 GLint x, GLint y, GLsizei width, GLint border)
1190 {
1191 struct gl_texture_unit *texUnit =
1192 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1193 struct gl_texture_object *texObj =
1194 _mesa_select_tex_object(ctx, texUnit, target);
1195 struct gl_texture_image *texImage =
1196 _mesa_select_tex_image(ctx, texObj, target, level);
1197
1198 #if 0
1199 if (border)
1200 goto fail;
1201 #endif
1202
1203 /* Setup or redefine the texture object, texture and texture
1204 * image. Don't populate yet.
1205 */
1206 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
1207 width, border,
1208 GL_RGBA, CHAN_TYPE, NULL,
1209 &ctx->DefaultPacking, texObj, texImage);
1210
1211 do_copy_texsubimage(ctx, target, level,
1212 0, 0, 0,
1213 x, y, width, 1);
1214 }
1215
1216
1217 static void
1218 st_CopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
1219 GLenum internalFormat,
1220 GLint x, GLint y, GLsizei width, GLsizei height,
1221 GLint border)
1222 {
1223 struct gl_texture_unit *texUnit =
1224 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1225 struct gl_texture_object *texObj =
1226 _mesa_select_tex_object(ctx, texUnit, target);
1227 struct gl_texture_image *texImage =
1228 _mesa_select_tex_image(ctx, texObj, target, level);
1229
1230 #if 0
1231 if (border)
1232 goto fail;
1233 #endif
1234
1235 /* Setup or redefine the texture object, texture and texture
1236 * image. Don't populate yet.
1237 */
1238 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
1239 width, height, border,
1240 GL_RGBA, CHAN_TYPE, NULL,
1241 &ctx->DefaultPacking, texObj, texImage);
1242
1243
1244 do_copy_texsubimage(ctx, target, level,
1245 0, 0, 0,
1246 x, y, width, height);
1247 }
1248
1249
1250 static void
1251 st_CopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
1252 GLint xoffset, GLint x, GLint y, GLsizei width)
1253 {
1254 const GLint yoffset = 0, zoffset = 0;
1255 const GLsizei height = 1;
1256 do_copy_texsubimage(ctx, target, level,
1257 xoffset, yoffset, zoffset,
1258 x, y, width, height);
1259 }
1260
1261
1262 static void
1263 st_CopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
1264 GLint xoffset, GLint yoffset,
1265 GLint x, GLint y, GLsizei width, GLsizei height)
1266 {
1267 const GLint zoffset = 0;
1268 do_copy_texsubimage(ctx, target, level,
1269 xoffset, yoffset, zoffset,
1270 x, y, width, height);
1271 }
1272
1273
1274 static void
1275 st_CopyTexSubImage3D(GLcontext * ctx, GLenum target, GLint level,
1276 GLint xoffset, GLint yoffset, GLint zoffset,
1277 GLint x, GLint y, GLsizei width, GLsizei height)
1278 {
1279 do_copy_texsubimage(ctx, target, level,
1280 xoffset, yoffset, zoffset,
1281 x, y, width, height);
1282 }
1283
1284
1285
1286
1287 /**
1288 * Compute which mipmap levels that really need to be sent to the hardware.
1289 * This depends on the base image size, GL_TEXTURE_MIN_LOD,
1290 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
1291 */
1292 static void
1293 calculate_first_last_level(struct st_texture_object *stObj)
1294 {
1295 struct gl_texture_object *tObj = &stObj->base;
1296 const struct gl_texture_image *const baseImage =
1297 tObj->Image[0][tObj->BaseLevel];
1298
1299 /* These must be signed values. MinLod and MaxLod can be negative numbers,
1300 * and having firstLevel and lastLevel as signed prevents the need for
1301 * extra sign checks.
1302 */
1303 int firstLevel;
1304 int lastLevel;
1305
1306 /* Yes, this looks overly complicated, but it's all needed.
1307 */
1308 switch (tObj->Target) {
1309 case GL_TEXTURE_1D:
1310 case GL_TEXTURE_2D:
1311 case GL_TEXTURE_3D:
1312 case GL_TEXTURE_CUBE_MAP:
1313 if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) {
1314 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
1315 */
1316 firstLevel = lastLevel = tObj->BaseLevel;
1317 }
1318 else {
1319 firstLevel = tObj->BaseLevel + (GLint) (tObj->MinLod + 0.5);
1320 firstLevel = MAX2(firstLevel, tObj->BaseLevel);
1321 lastLevel = tObj->BaseLevel + (GLint) (tObj->MaxLod + 0.5);
1322 lastLevel = MAX2(lastLevel, tObj->BaseLevel);
1323 lastLevel = MIN2(lastLevel, tObj->BaseLevel + baseImage->MaxLog2);
1324 lastLevel = MIN2(lastLevel, tObj->MaxLevel);
1325 lastLevel = MAX2(firstLevel, lastLevel); /* need at least one level */
1326 }
1327 break;
1328 case GL_TEXTURE_RECTANGLE_NV:
1329 case GL_TEXTURE_4D_SGIS:
1330 firstLevel = lastLevel = 0;
1331 break;
1332 default:
1333 return;
1334 }
1335
1336 /* save these values */
1337 stObj->firstLevel = firstLevel;
1338 stObj->lastLevel = lastLevel;
1339 }
1340
1341
1342 static void
1343 copy_image_data_to_texture(struct st_context *st,
1344 struct st_texture_object *stObj,
1345 struct st_texture_image *stImage)
1346 {
1347 if (stImage->pt) {
1348 /* Copy potentially with the blitter:
1349 */
1350 st_texture_image_copy(st->pipe,
1351 stObj->pt, /* dest texture */
1352 stImage->face, stImage->level,
1353 stImage->pt /* src texture */
1354 );
1355
1356 st->pipe->texture_release(st->pipe, &stImage->pt);
1357 }
1358 else {
1359 assert(stImage->base.Data != NULL);
1360
1361 /* More straightforward upload.
1362 */
1363 st_texture_image_data(st->pipe,
1364 stObj->pt,
1365 stImage->face,
1366 stImage->level,
1367 stImage->base.Data,
1368 stImage->base.RowStride,
1369 stImage->base.RowStride *
1370 stImage->base.Height);
1371 _mesa_align_free(stImage->base.Data);
1372 stImage->base.Data = NULL;
1373 }
1374
1375 pipe_texture_reference(st->pipe, &stImage->pt, stObj->pt);
1376 }
1377
1378
1379 /**
1380 * Called during state validation. When this function is finished,
1381 * the texture object should be ready for rendering.
1382 * \return GL_FALSE if a texture border is present, GL_TRUE otherwise
1383 */
1384 GLboolean
1385 st_finalize_texture(GLcontext *ctx,
1386 struct pipe_context *pipe,
1387 struct gl_texture_object *tObj,
1388 GLboolean *needFlush)
1389 {
1390 struct st_texture_object *stObj = st_texture_object(tObj);
1391 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1392 int comp_byte = 0;
1393 int cpp;
1394 GLuint face, i;
1395 struct st_texture_image *firstImage;
1396
1397 *needFlush = GL_FALSE;
1398
1399 /* We know/require this is true by now:
1400 */
1401 assert(stObj->base._Complete);
1402
1403 /* What levels must the texture include at a minimum?
1404 */
1405 calculate_first_last_level(stObj);
1406 firstImage = st_texture_image(stObj->base.Image[0][stObj->firstLevel]);
1407
1408 /* Fallback case:
1409 */
1410 if (firstImage->base.Border) {
1411 if (stObj->pt) {
1412 ctx->st->pipe->texture_release(ctx->st->pipe, &stObj->pt);
1413 }
1414 return GL_FALSE;
1415 }
1416
1417
1418 /* If both firstImage and stObj point to a texture which can contain
1419 * all active images, favour firstImage. Note that because of the
1420 * completeness requirement, we know that the image dimensions
1421 * will match.
1422 */
1423 if (firstImage->pt &&
1424 firstImage->pt != stObj->pt &&
1425 firstImage->pt->first_level <= stObj->firstLevel &&
1426 firstImage->pt->last_level >= stObj->lastLevel) {
1427
1428 if (stObj->pt)
1429 ctx->st->pipe->texture_release(ctx->st->pipe, &stObj->pt);
1430
1431 pipe_texture_reference(ctx->st->pipe, &stObj->pt, firstImage->pt);
1432 }
1433
1434 if (firstImage->base.IsCompressed) {
1435 comp_byte = compressed_num_bytes(firstImage->base.TexFormat->MesaFormat);
1436 cpp = comp_byte;
1437 }
1438 else {
1439 cpp = firstImage->base.TexFormat->TexelBytes;
1440 }
1441
1442 /* Check texture can hold all active levels. Check texture matches
1443 * target, imageFormat, etc.
1444 *
1445 * XXX: For some layouts (eg i945?), the test might have to be
1446 * first_level == firstLevel, as the texture isn't valid except at the
1447 * original start level. Hope to get around this by
1448 * programming minLod, maxLod, baseLevel into the hardware and
1449 * leaving the texture alone.
1450 */
1451 if (stObj->pt &&
1452 (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1453 stObj->pt->format !=
1454 st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat) ||
1455 stObj->pt->first_level != stObj->firstLevel ||
1456 stObj->pt->last_level != stObj->lastLevel ||
1457 stObj->pt->width[0] != firstImage->base.Width ||
1458 stObj->pt->height[0] != firstImage->base.Height ||
1459 stObj->pt->depth[0] != firstImage->base.Depth ||
1460 stObj->pt->cpp != cpp ||
1461 stObj->pt->compressed != firstImage->base.IsCompressed)) {
1462 ctx->st->pipe->texture_release(ctx->st->pipe, &stObj->pt);
1463 }
1464
1465
1466 /* May need to create a new texture:
1467 */
1468 if (!stObj->pt) {
1469 stObj->pt = st_texture_create(ctx->st,
1470 gl_target_to_pipe(stObj->base.Target),
1471 st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat),
1472 stObj->firstLevel,
1473 stObj->lastLevel,
1474 firstImage->base.Width,
1475 firstImage->base.Height,
1476 firstImage->base.Depth,
1477 comp_byte);
1478 }
1479
1480 /* Pull in any images not in the object's texture:
1481 */
1482 for (face = 0; face < nr_faces; face++) {
1483 for (i = stObj->firstLevel; i <= stObj->lastLevel; i++) {
1484 struct st_texture_image *stImage =
1485 st_texture_image(stObj->base.Image[face][i]);
1486
1487 /* Need to import images in main memory or held in other textures.
1488 */
1489 if (stObj->pt != stImage->pt) {
1490 copy_image_data_to_texture(ctx->st, stObj, stImage);
1491 *needFlush = GL_TRUE;
1492 }
1493 }
1494 }
1495
1496
1497 return GL_TRUE;
1498 }
1499
1500
1501
1502
1503 void
1504 st_init_texture_functions(struct dd_function_table *functions)
1505 {
1506 functions->ChooseTextureFormat = st_ChooseTextureFormat;
1507 functions->TexImage1D = st_TexImage1D;
1508 functions->TexImage2D = st_TexImage2D;
1509 functions->TexImage3D = st_TexImage3D;
1510 functions->TexSubImage1D = st_TexSubImage1D;
1511 functions->TexSubImage2D = st_TexSubImage2D;
1512 functions->TexSubImage3D = st_TexSubImage3D;
1513 functions->CopyTexImage1D = st_CopyTexImage1D;
1514 functions->CopyTexImage2D = st_CopyTexImage2D;
1515 functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
1516 functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
1517 functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
1518 functions->GenerateMipmap = st_generate_mipmap;
1519
1520 functions->GetTexImage = st_GetTexImage;
1521
1522 /* compressed texture functions */
1523 functions->CompressedTexImage2D = st_CompressedTexImage2D;
1524 functions->GetCompressedTexImage = st_GetCompressedTexImage;
1525
1526 functions->NewTextureObject = st_NewTextureObject;
1527 functions->NewTextureImage = st_NewTextureImage;
1528 functions->DeleteTexture = st_DeleteTextureObject;
1529 functions->FreeTexImageData = st_FreeTextureImageData;
1530 functions->UpdateTexturePalette = 0;
1531 functions->IsTextureResident = st_IsTextureResident;
1532
1533 functions->TextureMemCpy = do_memcpy;
1534
1535 /* XXX Temporary until we can query pipe's texture sizes */
1536 functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1537 }