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