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