Merge commit 'origin/gallium-0.1' 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_convolution
30 #include "main/convolve.h"
31 #endif
32 #include "main/enums.h"
33 #include "main/image.h"
34 #include "main/macros.h"
35 #include "main/mipmap.h"
36 #include "main/pixel.h"
37 #include "main/texcompress.h"
38 #include "main/texformat.h"
39 #include "main/teximage.h"
40 #include "main/texobj.h"
41 #include "main/texstore.h"
42
43 #include "state_tracker/st_context.h"
44 #include "state_tracker/st_cb_fbo.h"
45 #include "state_tracker/st_cb_texture.h"
46 #include "state_tracker/st_format.h"
47 #include "state_tracker/st_public.h"
48 #include "state_tracker/st_texture.h"
49 #include "state_tracker/st_gen_mipmap.h"
50
51 #include "pipe/p_context.h"
52 #include "pipe/p_defines.h"
53 #include "pipe/p_inlines.h"
54 #include "util/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 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_convolution
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 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
485 return;
486 }
487 }
488
489 assert(!stImage->pt);
490
491 if (stObj->pt &&
492 st_texture_match_image(stObj->pt, &stImage->base,
493 stImage->face, stImage->level)) {
494
495 pipe_texture_reference(&stImage->pt, stObj->pt);
496 assert(stImage->pt);
497 }
498
499 if (!stImage->pt)
500 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
501
502 /* st_CopyTexImage calls this function with pixels == NULL, with
503 * the expectation that the texture will be set up but nothing
504 * more will be done. This is where those calls return:
505 */
506 if (compressed) {
507 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
508 unpack,
509 "glCompressedTexImage");
510 } else {
511 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, 1,
512 format, type,
513 pixels, unpack, "glTexImage");
514 }
515 if (!pixels)
516 return;
517
518 if (stImage->pt) {
519 texImage->Data = st_texture_image_map(ctx->st, stImage, 0,
520 PIPE_BUFFER_USAGE_CPU_WRITE);
521 if (stImage->surface)
522 dstRowStride = stImage->surface->stride;
523 }
524 else {
525 /* Allocate regular memory and store the image there temporarily. */
526 if (texImage->IsCompressed) {
527 sizeInBytes = texImage->CompressedSize;
528 dstRowStride =
529 _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat, width);
530 assert(dims != 3);
531 }
532 else {
533 dstRowStride = postConvWidth * texelBytes;
534 sizeInBytes = depth * dstRowStride * postConvHeight;
535 }
536
537 texImage->Data = malloc(sizeInBytes);
538 }
539
540 if (!texImage->Data) {
541 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
542 return;
543 }
544
545 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
546 width, height, depth, width * texelBytes, dstRowStride);
547
548 /* Copy data. Would like to know when it's ok for us to eg. use
549 * the blitter to copy. Or, use the hardware to do the format
550 * conversion and copy:
551 */
552 if (compressed) {
553 memcpy(texImage->Data, pixels, imageSize);
554 }
555 else {
556 GLuint srcImageStride = _mesa_image_image_stride(unpack, width, height,
557 format, type);
558 int i;
559 const GLubyte *src = (const GLubyte *) pixels;
560
561 for (i = 0; i++ < depth;) {
562 if (!texImage->TexFormat->StoreImage(ctx, dims,
563 texImage->_BaseFormat,
564 texImage->TexFormat,
565 texImage->Data,
566 0, 0, 0, /* dstX/Y/Zoffset */
567 dstRowStride,
568 texImage->ImageOffsets,
569 width, height, 1,
570 format, type, src, unpack)) {
571 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
572 }
573
574 if (stImage->pt && i < depth) {
575 st_texture_image_unmap(ctx->st, stImage);
576 texImage->Data = st_texture_image_map(ctx->st, stImage, i,
577 PIPE_BUFFER_USAGE_CPU_WRITE);
578 src += srcImageStride;
579 }
580 }
581 }
582
583 _mesa_unmap_teximage_pbo(ctx, unpack);
584
585 if (stImage->pt) {
586 st_texture_image_unmap(ctx->st, stImage);
587 texImage->Data = NULL;
588 }
589
590 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
591 ctx->Driver.GenerateMipmap(ctx, target, texObj);
592 }
593 }
594
595
596 static void
597 st_TexImage3D(GLcontext * ctx,
598 GLenum target, GLint level,
599 GLint internalFormat,
600 GLint width, GLint height, GLint depth,
601 GLint border,
602 GLenum format, GLenum type, const void *pixels,
603 const struct gl_pixelstore_attrib *unpack,
604 struct gl_texture_object *texObj,
605 struct gl_texture_image *texImage)
606 {
607 st_TexImage(ctx, 3, target, level,
608 internalFormat, width, height, depth, border,
609 format, type, pixels, unpack, texObj, texImage, 0, 0);
610 }
611
612
613 static void
614 st_TexImage2D(GLcontext * ctx,
615 GLenum target, GLint level,
616 GLint internalFormat,
617 GLint width, GLint height, GLint border,
618 GLenum format, GLenum type, const void *pixels,
619 const struct gl_pixelstore_attrib *unpack,
620 struct gl_texture_object *texObj,
621 struct gl_texture_image *texImage)
622 {
623 st_TexImage(ctx, 2, target, level,
624 internalFormat, width, height, 1, border,
625 format, type, pixels, unpack, texObj, texImage, 0, 0);
626 }
627
628
629 static void
630 st_TexImage1D(GLcontext * ctx,
631 GLenum target, GLint level,
632 GLint internalFormat,
633 GLint width, GLint border,
634 GLenum format, GLenum type, const void *pixels,
635 const struct gl_pixelstore_attrib *unpack,
636 struct gl_texture_object *texObj,
637 struct gl_texture_image *texImage)
638 {
639 st_TexImage(ctx, 1, target, level,
640 internalFormat, width, 1, 1, border,
641 format, type, pixels, unpack, texObj, texImage, 0, 0);
642 }
643
644
645 static void
646 st_CompressedTexImage2D(GLcontext *ctx, GLenum target, GLint level,
647 GLint internalFormat,
648 GLint width, GLint height, GLint border,
649 GLsizei imageSize, const GLvoid *data,
650 struct gl_texture_object *texObj,
651 struct gl_texture_image *texImage)
652 {
653 st_TexImage(ctx, 2, target, level,
654 internalFormat, width, height, 1, border,
655 0, 0, data, &ctx->Unpack, texObj, texImage, imageSize, 1);
656 }
657
658
659 /**
660 * Need to map texture image into memory before copying image data,
661 * then unmap it.
662 */
663 static void
664 st_get_tex_image(GLcontext * ctx, GLenum target, GLint level,
665 GLenum format, GLenum type, GLvoid * pixels,
666 struct gl_texture_object *texObj,
667 struct gl_texture_image *texImage, int compressed)
668 {
669 struct st_texture_image *stImage = st_texture_image(texImage);
670 GLuint dstImageStride = _mesa_image_image_stride(&ctx->Pack,
671 texImage->Width,
672 texImage->Height,
673 format, type);
674 GLuint depth;
675 GLuint i;
676 GLubyte *dest;
677
678 /* Map */
679 if (stImage->pt) {
680 /* Image is stored in hardware format in a buffer managed by the
681 * kernel. Need to explicitly map and unmap it.
682 */
683 texImage->Data = st_texture_image_map(ctx->st, stImage, 0,
684 PIPE_BUFFER_USAGE_CPU_READ);
685 texImage->RowStride = stImage->surface->stride / stImage->pt->block.size;
686 }
687 else {
688 /* Otherwise, the image should actually be stored in
689 * texImage->Data. This is pretty confusing for
690 * everybody, I'd much prefer to separate the two functions of
691 * texImage->Data - storage for texture images in main memory
692 * and access (ie mappings) of images. In other words, we'd
693 * create a new texImage->Map field and leave Data simply for
694 * storage.
695 */
696 assert(texImage->Data);
697 }
698
699 depth = texImage->Depth;
700 texImage->Depth = 1;
701
702 dest = (GLubyte *) pixels;
703
704 for (i = 0; i++ < depth;) {
705 if (compressed) {
706 _mesa_get_compressed_teximage(ctx, target, level, dest,
707 texObj, texImage);
708 } else {
709 _mesa_get_teximage(ctx, target, level, format, type, dest,
710 texObj, texImage);
711 }
712
713 if (stImage->pt && i < depth) {
714 st_texture_image_unmap(ctx->st, stImage);
715 texImage->Data = st_texture_image_map(ctx->st, stImage, i,
716 PIPE_BUFFER_USAGE_CPU_READ);
717 dest += dstImageStride;
718 }
719 }
720
721 texImage->Depth = depth;
722
723 /* Unmap */
724 if (stImage->pt) {
725 st_texture_image_unmap(ctx->st, stImage);
726 texImage->Data = NULL;
727 }
728 }
729
730
731 static void
732 st_GetTexImage(GLcontext * ctx, GLenum target, GLint level,
733 GLenum format, GLenum type, GLvoid * pixels,
734 struct gl_texture_object *texObj,
735 struct gl_texture_image *texImage)
736 {
737 st_get_tex_image(ctx, target, level, format, type, pixels,
738 texObj, texImage, 0);
739 }
740
741
742 static void
743 st_GetCompressedTexImage(GLcontext *ctx, GLenum target, GLint level,
744 GLvoid *pixels,
745 const struct gl_texture_object *texObj,
746 const struct gl_texture_image *texImage)
747 {
748 st_get_tex_image(ctx, target, level, 0, 0, pixels,
749 (struct gl_texture_object *) texObj,
750 (struct gl_texture_image *) texImage, 1);
751 }
752
753
754
755 static void
756 st_TexSubimage(GLcontext * ctx,
757 GLint dims,
758 GLenum target, GLint level,
759 GLint xoffset, GLint yoffset, GLint zoffset,
760 GLint width, GLint height, GLint depth,
761 GLenum format, GLenum type, const void *pixels,
762 const struct gl_pixelstore_attrib *packing,
763 struct gl_texture_object *texObj,
764 struct gl_texture_image *texImage)
765 {
766 struct st_texture_image *stImage = st_texture_image(texImage);
767 GLuint dstRowStride;
768 GLuint srcImageStride = _mesa_image_image_stride(packing, width, height,
769 format, type);
770 int i;
771 const GLubyte *src;
772
773 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
774 _mesa_lookup_enum_by_nr(target),
775 level, xoffset, yoffset, width, height);
776
777 pixels =
778 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth, format,
779 type, pixels, packing, "glTexSubImage2D");
780 if (!pixels)
781 return;
782
783 /* Map buffer if necessary. Need to lock to prevent other contexts
784 * from uploading the buffer under us.
785 */
786 if (stImage->pt) {
787 texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset,
788 PIPE_BUFFER_USAGE_CPU_WRITE);
789 if (stImage->surface)
790 dstRowStride = stImage->surface->stride;
791 }
792
793 if (!texImage->Data) {
794 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
795 return;
796 }
797
798 src = (const GLubyte *) pixels;
799
800 for (i = 0; i++ < depth;) {
801 if (!texImage->TexFormat->StoreImage(ctx, dims, texImage->_BaseFormat,
802 texImage->TexFormat,
803 texImage->Data,
804 xoffset, yoffset, 0,
805 dstRowStride,
806 texImage->ImageOffsets,
807 width, height, 1,
808 format, type, src, packing)) {
809 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
810 }
811
812 if (stImage->pt && i < depth) {
813 /* map next slice of 3D texture */
814 st_texture_image_unmap(ctx->st, stImage);
815 texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset + i,
816 PIPE_BUFFER_USAGE_CPU_WRITE);
817 src += srcImageStride;
818 }
819 }
820
821 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
822 ctx->Driver.GenerateMipmap(ctx, target, texObj);
823 }
824
825 _mesa_unmap_teximage_pbo(ctx, packing);
826
827 if (stImage->pt) {
828 st_texture_image_unmap(ctx->st, stImage);
829 texImage->Data = NULL;
830 }
831 }
832
833
834
835 static void
836 st_TexSubImage3D(GLcontext * ctx,
837 GLenum target,
838 GLint level,
839 GLint xoffset, GLint yoffset, GLint zoffset,
840 GLsizei width, GLsizei height, GLsizei depth,
841 GLenum format, GLenum type,
842 const GLvoid * pixels,
843 const struct gl_pixelstore_attrib *packing,
844 struct gl_texture_object *texObj,
845 struct gl_texture_image *texImage)
846 {
847 st_TexSubimage(ctx, 3, target, level,
848 xoffset, yoffset, zoffset,
849 width, height, depth,
850 format, type, pixels, packing, texObj, texImage);
851 }
852
853
854 static void
855 st_TexSubImage2D(GLcontext * ctx,
856 GLenum target,
857 GLint level,
858 GLint xoffset, GLint yoffset,
859 GLsizei width, GLsizei height,
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, 2, target, level,
867 xoffset, yoffset, 0,
868 width, height, 1,
869 format, type, pixels, packing, texObj, texImage);
870 }
871
872
873 static void
874 st_TexSubImage1D(GLcontext * ctx,
875 GLenum target,
876 GLint level,
877 GLint xoffset,
878 GLsizei width,
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, 1, target, level,
886 xoffset, 0, 0,
887 width, 1, 1,
888 format, type, pixels, packing, texObj, texImage);
889 }
890
891
892
893 /**
894 * Return 0 for GL_TEXTURE_CUBE_MAP_POSITIVE_X,
895 * 1 for GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
896 * etc.
897 * XXX duplicated from main/teximage.c
898 */
899 static uint
900 texture_face(GLenum target)
901 {
902 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
903 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)
904 return (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
905 else
906 return 0;
907 }
908
909
910
911 /**
912 * Do a CopyTexSubImage operation by mapping the source surface and
913 * dest surface and using get_tile()/put_tile() to access the pixels/texels.
914 *
915 * Note: srcY=0=TOP of renderbuffer
916 */
917 static void
918 fallback_copy_texsubimage(GLcontext *ctx,
919 GLenum target,
920 GLint level,
921 struct st_renderbuffer *strb,
922 struct st_texture_image *stImage,
923 GLenum baseFormat,
924 GLint destX, GLint destY, GLint destZ,
925 GLint srcX, GLint srcY,
926 GLsizei width, GLsizei height)
927 {
928 struct pipe_context *pipe = ctx->st->pipe;
929 struct pipe_screen *screen = pipe->screen;
930 const uint face = texture_face(target);
931 struct pipe_texture *pt = stImage->pt;
932 struct pipe_surface *src_surf, *dest_surf;
933
934 /* We'd use strb->surface, here but it's created for GPU read/write only */
935 src_surf = pipe->screen->get_tex_surface( pipe->screen,
936 strb->texture,
937 0, 0, 0,
938 PIPE_BUFFER_USAGE_CPU_READ);
939
940 dest_surf = screen->get_tex_surface(screen, pt, face, level, destZ,
941 PIPE_BUFFER_USAGE_CPU_WRITE);
942
943 assert(width <= MAX_WIDTH);
944
945 if (baseFormat == GL_DEPTH_COMPONENT) {
946 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
947 ctx->Pixel.DepthBias != 0.0F);
948 GLint row, yStep;
949
950 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
951 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
952 srcY = strb->Base.Height - 1 - srcY;
953 yStep = -1;
954 }
955 else {
956 yStep = 1;
957 }
958
959 /* To avoid a large temp memory allocation, do copy row by row */
960 for (row = 0; row < height; row++, srcY += yStep, destY++) {
961 uint data[MAX_WIDTH];
962 pipe_get_tile_z(src_surf, srcX, srcY, width, 1, data);
963 if (scaleOrBias) {
964 _mesa_scale_and_bias_depth_uint(ctx, width, data);
965 }
966 pipe_put_tile_z(dest_surf, destX, destY, width, 1, data);
967 }
968 }
969 else {
970 /* RGBA format */
971 GLfloat *tempSrc =
972 (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
973 GLvoid *texDest =
974 st_texture_image_map(ctx->st, stImage, 0,PIPE_BUFFER_USAGE_CPU_WRITE);
975
976 if (tempSrc && texDest) {
977 const GLint dims = 2;
978 struct gl_texture_image *texImage = &stImage->base;
979 GLint dstRowStride = stImage->surface->stride;
980 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
981
982 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
983 /* need to invert src */
984 srcY = strb->Base.Height - srcY - height;
985 unpack.Invert = GL_TRUE;
986 }
987
988 /* get float/RGBA image from framebuffer */
989 /* XXX this usually involves a lot of int/float conversion.
990 * try to avoid that someday.
991 */
992 pipe_get_tile_rgba(src_surf, srcX, srcY, width, height, tempSrc);
993
994 /* Store into texture memory.
995 * Note that this does some special things such as pixel transfer
996 * ops and format conversion. In particular, if the dest tex format
997 * is actually RGBA but the user created the texture as GL_RGB we
998 * need to fill-in/override the alpha channel with 1.0.
999 */
1000 texImage->TexFormat->StoreImage(ctx, dims,
1001 texImage->_BaseFormat,
1002 texImage->TexFormat,
1003 texDest,
1004 destX, destY, destZ,
1005 dstRowStride,
1006 texImage->ImageOffsets,
1007 width, height, 1,
1008 GL_RGBA, GL_FLOAT, tempSrc, /* src */
1009 &unpack);
1010 }
1011 else {
1012 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
1013 }
1014
1015 if (tempSrc)
1016 _mesa_free(tempSrc);
1017 if (texDest)
1018 st_texture_image_unmap(ctx->st, stImage);
1019 }
1020
1021 screen->tex_surface_release(screen, &dest_surf);
1022 screen->tex_surface_release(screen, &src_surf);
1023 }
1024
1025
1026 /**
1027 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1028 * Note that the region to copy has already been clipped so we know we
1029 * won't read from outside the source renderbuffer's bounds.
1030 *
1031 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1032 */
1033 static void
1034 st_copy_texsubimage(GLcontext *ctx,
1035 GLenum target, GLint level,
1036 GLint destX, GLint destY, GLint destZ,
1037 GLint srcX, GLint srcY,
1038 GLsizei width, GLsizei height)
1039 {
1040 struct gl_texture_unit *texUnit =
1041 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1042 struct gl_texture_object *texObj =
1043 _mesa_select_tex_object(ctx, texUnit, target);
1044 struct gl_texture_image *texImage =
1045 _mesa_select_tex_image(ctx, texObj, target, level);
1046 struct st_texture_image *stImage = st_texture_image(texImage);
1047 const GLenum texBaseFormat = texImage->InternalFormat;
1048 struct gl_framebuffer *fb = ctx->ReadBuffer;
1049 struct st_renderbuffer *strb;
1050 struct pipe_context *pipe = ctx->st->pipe;
1051 struct pipe_screen *screen = pipe->screen;
1052 enum pipe_format dest_format, src_format;
1053 GLboolean use_fallback = GL_TRUE;
1054 GLboolean matching_base_formats;
1055
1056 /* any rendering in progress must complete before we grab the fb image */
1057 st_finish(ctx->st);
1058
1059 /* determine if copying depth or color data */
1060 if (texBaseFormat == GL_DEPTH_COMPONENT) {
1061 strb = st_renderbuffer(fb->_DepthBuffer);
1062 }
1063 else if (texBaseFormat == GL_DEPTH_STENCIL_EXT) {
1064 strb = st_renderbuffer(fb->_StencilBuffer);
1065 }
1066 else {
1067 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1068 strb = st_renderbuffer(fb->_ColorReadBuffer);
1069 }
1070
1071 assert(strb);
1072 assert(strb->surface);
1073 assert(stImage->pt);
1074
1075 src_format = strb->surface->format;
1076 dest_format = stImage->pt->format;
1077
1078 /*
1079 * Determine if the src framebuffer and dest texture have the same
1080 * base format. We need this to detect a case such as the framebuffer
1081 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1082 * texture format stores RGBA we need to set A=1 (overriding the
1083 * framebuffer's alpha values). We can't do that with the blit or
1084 * textured-quad paths.
1085 */
1086 matching_base_formats = (strb->Base._BaseFormat == texImage->_BaseFormat);
1087
1088 if (matching_base_formats && ctx->_ImageTransferState == 0x0) {
1089 /* try potential hardware path */
1090 struct pipe_surface *dest_surface = NULL;
1091
1092 if (src_format == dest_format) {
1093 /* use surface_copy() / blit */
1094 boolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1095
1096 dest_surface = screen->get_tex_surface(screen, stImage->pt,
1097 stImage->face, stImage->level,
1098 destZ,
1099 PIPE_BUFFER_USAGE_GPU_WRITE);
1100 if (do_flip)
1101 srcY = strb->surface->height - srcY - height;
1102
1103 /* for surface_copy(), y=0=top, always */
1104 pipe->surface_copy(pipe,
1105 do_flip,
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 boolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1126 int srcY0, srcY1;
1127
1128 dest_surface = screen->get_tex_surface(screen, stImage->pt,
1129 stImage->face, stImage->level,
1130 destZ,
1131 PIPE_BUFFER_USAGE_GPU_WRITE);
1132
1133 if (do_flip) {
1134 srcY1 = strb->Base.Height - srcY - height;
1135 srcY0 = srcY1 + height;
1136 }
1137 else {
1138 srcY0 = srcY;
1139 srcY1 = srcY0 + height;
1140 }
1141 util_blit_pixels(ctx->st->blit,
1142 strb->surface,
1143 srcX, srcY0,
1144 srcX + width, srcY1,
1145 dest_surface,
1146 destX, destY,
1147 destX + width, destY + height,
1148 0.0, PIPE_TEX_MIPFILTER_NEAREST);
1149 use_fallback = GL_FALSE;
1150 }
1151
1152 if (dest_surface)
1153 pipe_surface_reference(&dest_surface, NULL);
1154 }
1155
1156 if (use_fallback) {
1157 /* software fallback */
1158 fallback_copy_texsubimage(ctx, target, level,
1159 strb, stImage, texBaseFormat,
1160 destX, destY, destZ,
1161 srcX, srcY, width, height);
1162 }
1163
1164 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
1165 ctx->Driver.GenerateMipmap(ctx, target, texObj);
1166 }
1167 }
1168
1169
1170
1171 static void
1172 st_CopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
1173 GLenum internalFormat,
1174 GLint x, GLint y, GLsizei width, GLint border)
1175 {
1176 struct gl_texture_unit *texUnit =
1177 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1178 struct gl_texture_object *texObj =
1179 _mesa_select_tex_object(ctx, texUnit, target);
1180 struct gl_texture_image *texImage =
1181 _mesa_select_tex_image(ctx, texObj, target, level);
1182
1183 #if 0
1184 if (border)
1185 goto fail;
1186 #endif
1187
1188 /* Setup or redefine the texture object, texture and texture
1189 * image. Don't populate yet.
1190 */
1191 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
1192 width, border,
1193 GL_RGBA, CHAN_TYPE, NULL,
1194 &ctx->DefaultPacking, texObj, texImage);
1195
1196 st_copy_texsubimage(ctx, target, level,
1197 0, 0, 0, /* destX,Y,Z */
1198 x, y, width, 1); /* src X, Y, size */
1199 }
1200
1201
1202 static void
1203 st_CopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
1204 GLenum internalFormat,
1205 GLint x, GLint y, GLsizei width, GLsizei height,
1206 GLint border)
1207 {
1208 struct gl_texture_unit *texUnit =
1209 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1210 struct gl_texture_object *texObj =
1211 _mesa_select_tex_object(ctx, texUnit, target);
1212 struct gl_texture_image *texImage =
1213 _mesa_select_tex_image(ctx, texObj, target, level);
1214
1215 /* Setup or redefine the texture object, texture and texture
1216 * image. Don't populate yet.
1217 */
1218 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
1219 width, height, border,
1220 GL_RGBA, CHAN_TYPE, NULL,
1221 &ctx->DefaultPacking, texObj, texImage);
1222
1223 st_copy_texsubimage(ctx, target, level,
1224 0, 0, 0, /* destX,Y,Z */
1225 x, y, width, height); /* src X, Y, size */
1226 }
1227
1228
1229 static void
1230 st_CopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
1231 GLint xoffset, GLint x, GLint y, GLsizei width)
1232 {
1233 const GLint yoffset = 0, zoffset = 0;
1234 const GLsizei height = 1;
1235 st_copy_texsubimage(ctx, target, level,
1236 xoffset, yoffset, zoffset, /* destX,Y,Z */
1237 x, y, width, height); /* src X, Y, size */
1238 }
1239
1240
1241 static void
1242 st_CopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
1243 GLint xoffset, GLint yoffset,
1244 GLint x, GLint y, GLsizei width, GLsizei height)
1245 {
1246 const GLint zoffset = 0;
1247 st_copy_texsubimage(ctx, target, level,
1248 xoffset, yoffset, zoffset, /* destX,Y,Z */
1249 x, y, width, height); /* src X, Y, size */
1250 }
1251
1252
1253 static void
1254 st_CopyTexSubImage3D(GLcontext * ctx, GLenum target, GLint level,
1255 GLint xoffset, GLint yoffset, GLint zoffset,
1256 GLint x, GLint y, GLsizei width, GLsizei height)
1257 {
1258 st_copy_texsubimage(ctx, target, level,
1259 xoffset, yoffset, zoffset, /* destX,Y,Z */
1260 x, y, width, height); /* src X, Y, size */
1261 }
1262
1263
1264 /**
1265 * Compute which mipmap levels that really need to be sent to the hardware.
1266 * This depends on the base image size, GL_TEXTURE_MIN_LOD,
1267 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
1268 */
1269 static void
1270 calculate_first_last_level(struct st_texture_object *stObj)
1271 {
1272 struct gl_texture_object *tObj = &stObj->base;
1273
1274 /* These must be signed values. MinLod and MaxLod can be negative numbers,
1275 * and having firstLevel and lastLevel as signed prevents the need for
1276 * extra sign checks.
1277 */
1278 int firstLevel;
1279 int lastLevel;
1280
1281 /* Yes, this looks overly complicated, but it's all needed.
1282 */
1283 switch (tObj->Target) {
1284 case GL_TEXTURE_1D:
1285 case GL_TEXTURE_2D:
1286 case GL_TEXTURE_3D:
1287 case GL_TEXTURE_CUBE_MAP:
1288 if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) {
1289 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
1290 */
1291 firstLevel = lastLevel = tObj->BaseLevel;
1292 }
1293 else {
1294 firstLevel = 0;
1295 lastLevel = MIN2(tObj->MaxLevel,
1296 (int) tObj->Image[0][tObj->BaseLevel]->WidthLog2);
1297 }
1298 break;
1299 case GL_TEXTURE_RECTANGLE_NV:
1300 case GL_TEXTURE_4D_SGIS:
1301 firstLevel = lastLevel = 0;
1302 break;
1303 default:
1304 return;
1305 }
1306
1307 stObj->lastLevel = lastLevel;
1308 }
1309
1310
1311 static void
1312 copy_image_data_to_texture(struct st_context *st,
1313 struct st_texture_object *stObj,
1314 GLuint dstLevel,
1315 struct st_texture_image *stImage)
1316 {
1317 if (stImage->pt) {
1318 /* Copy potentially with the blitter:
1319 */
1320 st_texture_image_copy(st->pipe,
1321 stObj->pt, dstLevel, /* dest texture, level */
1322 stImage->pt, /* src texture */
1323 stImage->face
1324 );
1325
1326 pipe_texture_reference(&stImage->pt, NULL);
1327 }
1328 else if (stImage->base.Data) {
1329 assert(stImage->base.Data != NULL);
1330
1331 /* More straightforward upload.
1332 */
1333 st_texture_image_data(st->pipe,
1334 stObj->pt,
1335 stImage->face,
1336 dstLevel,
1337 stImage->base.Data,
1338 stImage->base.RowStride *
1339 stObj->pt->block.size,
1340 stImage->base.RowStride *
1341 stImage->base.Height *
1342 stObj->pt->block.size);
1343 _mesa_align_free(stImage->base.Data);
1344 stImage->base.Data = NULL;
1345 }
1346
1347 pipe_texture_reference(&stImage->pt, stObj->pt);
1348 }
1349
1350
1351 /**
1352 * Called during state validation. When this function is finished,
1353 * the texture object should be ready for rendering.
1354 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1355 */
1356 GLboolean
1357 st_finalize_texture(GLcontext *ctx,
1358 struct pipe_context *pipe,
1359 struct gl_texture_object *tObj,
1360 GLboolean *needFlush)
1361 {
1362 struct st_texture_object *stObj = st_texture_object(tObj);
1363 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1364 int comp_byte = 0;
1365 int cpp;
1366 GLuint face;
1367 struct st_texture_image *firstImage;
1368
1369 *needFlush = GL_FALSE;
1370
1371 /* We know/require this is true by now:
1372 */
1373 assert(stObj->base._Complete);
1374
1375 /* What levels must the texture include at a minimum?
1376 */
1377 calculate_first_last_level(stObj);
1378 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1379
1380 /* If both firstImage and stObj point to a texture which can contain
1381 * all active images, favour firstImage. Note that because of the
1382 * completeness requirement, we know that the image dimensions
1383 * will match.
1384 */
1385 if (firstImage->pt &&
1386 firstImage->pt != stObj->pt &&
1387 firstImage->pt->last_level >= stObj->lastLevel) {
1388
1389 pipe_texture_reference(&stObj->pt, firstImage->pt);
1390 }
1391
1392 /* FIXME: determine format block instead of cpp */
1393 if (firstImage->base.IsCompressed) {
1394 comp_byte = compressed_num_bytes(firstImage->base.TexFormat->MesaFormat);
1395 cpp = comp_byte;
1396 }
1397 else {
1398 cpp = firstImage->base.TexFormat->TexelBytes;
1399 }
1400
1401 /* If we already have a gallium texture, check that it matches the texture
1402 * object's format, target, size, num_levels, etc.
1403 */
1404 if (stObj->pt) {
1405 const enum pipe_format fmt =
1406 st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat);
1407 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1408 stObj->pt->format != fmt ||
1409 stObj->pt->last_level < stObj->lastLevel ||
1410 stObj->pt->width[0] != firstImage->base.Width2 ||
1411 stObj->pt->height[0] != firstImage->base.Height2 ||
1412 stObj->pt->depth[0] != firstImage->base.Depth2 ||
1413 stObj->pt->block.size != cpp ||
1414 stObj->pt->block.width != 1 ||
1415 stObj->pt->block.height != 1 ||
1416 stObj->pt->compressed != firstImage->base.IsCompressed) {
1417 pipe_texture_release(&stObj->pt);
1418 ctx->st->dirty.st |= ST_NEW_FRAMEBUFFER;
1419 }
1420 }
1421
1422 /* May need to create a new gallium texture:
1423 */
1424 if (!stObj->pt) {
1425 const enum pipe_format fmt =
1426 st_mesa_format_to_pipe_format(firstImage->base.TexFormat->MesaFormat);
1427 stObj->pt = st_texture_create(ctx->st,
1428 gl_target_to_pipe(stObj->base.Target),
1429 fmt,
1430 stObj->lastLevel,
1431 firstImage->base.Width2,
1432 firstImage->base.Height2,
1433 firstImage->base.Depth2,
1434 comp_byte,
1435 ( (pf_is_depth_stencil(fmt) ?
1436 PIPE_TEXTURE_USAGE_DEPTH_STENCIL :
1437 PIPE_TEXTURE_USAGE_RENDER_TARGET) |
1438 PIPE_TEXTURE_USAGE_SAMPLER ));
1439
1440 if (!stObj->pt) {
1441 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1442 return GL_FALSE;
1443 }
1444 }
1445
1446 /* Pull in any images not in the object's texture:
1447 */
1448 for (face = 0; face < nr_faces; face++) {
1449 GLuint level;
1450 for (level = 0; level <= stObj->lastLevel; level++) {
1451 struct st_texture_image *stImage =
1452 st_texture_image(stObj->base.Image[face][stObj->base.BaseLevel + level]);
1453
1454 /* Need to import images in main memory or held in other textures.
1455 */
1456 if (stImage && stObj->pt != stImage->pt) {
1457 copy_image_data_to_texture(ctx->st, stObj, level, stImage);
1458 *needFlush = GL_TRUE;
1459 }
1460 }
1461 }
1462
1463 return GL_TRUE;
1464 }
1465
1466
1467 /**
1468 * Returns pointer to a default/dummy texture.
1469 * This is typically used when the current shader has tex/sample instructions
1470 * but the user has not provided a (any) texture(s).
1471 */
1472 struct gl_texture_object *
1473 st_get_default_texture(struct st_context *st)
1474 {
1475 if (!st->default_texture) {
1476 static const GLenum target = GL_TEXTURE_2D;
1477 GLubyte pixels[16][16][4];
1478 struct gl_texture_object *texObj;
1479 struct gl_texture_image *texImg;
1480
1481 /* init image to gray */
1482 memset(pixels, 127, sizeof(pixels));
1483
1484 texObj = st->ctx->Driver.NewTextureObject(st->ctx, 0, target);
1485
1486 texImg = _mesa_get_tex_image(st->ctx, texObj, target, 0);
1487
1488 _mesa_init_teximage_fields(st->ctx, target, texImg,
1489 16, 16, 1, 0, /* w, h, d, border */
1490 GL_RGBA);
1491
1492 st_TexImage(st->ctx, 2, target,
1493 0, GL_RGBA, /* level, intformat */
1494 16, 16, 1, 0, /* w, h, d, border */
1495 GL_RGBA, GL_UNSIGNED_BYTE, pixels,
1496 &st->ctx->DefaultPacking,
1497 texObj, texImg,
1498 0, 0);
1499
1500 texObj->MinFilter = GL_NEAREST;
1501 texObj->MagFilter = GL_NEAREST;
1502 texObj->_Complete = GL_TRUE;
1503
1504 st->default_texture = texObj;
1505 }
1506 return st->default_texture;
1507 }
1508
1509
1510 void
1511 st_init_texture_functions(struct dd_function_table *functions)
1512 {
1513 functions->ChooseTextureFormat = st_ChooseTextureFormat;
1514 functions->TexImage1D = st_TexImage1D;
1515 functions->TexImage2D = st_TexImage2D;
1516 functions->TexImage3D = st_TexImage3D;
1517 functions->TexSubImage1D = st_TexSubImage1D;
1518 functions->TexSubImage2D = st_TexSubImage2D;
1519 functions->TexSubImage3D = st_TexSubImage3D;
1520 functions->CopyTexImage1D = st_CopyTexImage1D;
1521 functions->CopyTexImage2D = st_CopyTexImage2D;
1522 functions->CopyTexSubImage1D = st_CopyTexSubImage1D;
1523 functions->CopyTexSubImage2D = st_CopyTexSubImage2D;
1524 functions->CopyTexSubImage3D = st_CopyTexSubImage3D;
1525 functions->GenerateMipmap = st_generate_mipmap;
1526
1527 functions->GetTexImage = st_GetTexImage;
1528
1529 /* compressed texture functions */
1530 functions->CompressedTexImage2D = st_CompressedTexImage2D;
1531 functions->GetCompressedTexImage = st_GetCompressedTexImage;
1532 functions->CompressedTextureSize = _mesa_compressed_texture_size;
1533
1534 functions->NewTextureObject = st_NewTextureObject;
1535 functions->NewTextureImage = st_NewTextureImage;
1536 functions->DeleteTexture = st_DeleteTextureObject;
1537 functions->FreeTexImageData = st_FreeTextureImageData;
1538 functions->UpdateTexturePalette = 0;
1539
1540 functions->TextureMemCpy = do_memcpy;
1541
1542 /* XXX Temporary until we can query pipe's texture sizes */
1543 functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1544 }