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