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