1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "main/mfeatures.h"
29 #include "main/bufferobj.h"
30 #include "main/enums.h"
31 #include "main/fbobject.h"
32 #include "main/formats.h"
33 #include "main/image.h"
34 #include "main/imports.h"
35 #include "main/macros.h"
36 #include "main/mipmap.h"
37 #include "main/pack.h"
39 #include "main/pixeltransfer.h"
40 #include "main/texcompress.h"
41 #include "main/texfetch.h"
42 #include "main/texgetimage.h"
43 #include "main/teximage.h"
44 #include "main/texobj.h"
45 #include "main/texstore.h"
47 #include "state_tracker/st_debug.h"
48 #include "state_tracker/st_context.h"
49 #include "state_tracker/st_cb_fbo.h"
50 #include "state_tracker/st_cb_flush.h"
51 #include "state_tracker/st_cb_texture.h"
52 #include "state_tracker/st_format.h"
53 #include "state_tracker/st_texture.h"
54 #include "state_tracker/st_gen_mipmap.h"
55 #include "state_tracker/st_atom.h"
57 #include "pipe/p_context.h"
58 #include "pipe/p_defines.h"
59 #include "util/u_inlines.h"
60 #include "pipe/p_shader_tokens.h"
61 #include "util/u_tile.h"
62 #include "util/u_blit.h"
63 #include "util/u_format.h"
64 #include "util/u_surface.h"
65 #include "util/u_sampler.h"
66 #include "util/u_math.h"
67 #include "util/u_box.h"
69 #define DBG if (0) printf
72 static enum pipe_texture_target
73 gl_target_to_pipe(GLenum target
)
77 return PIPE_TEXTURE_1D
;
79 return PIPE_TEXTURE_2D
;
80 case GL_TEXTURE_RECTANGLE_NV
:
81 return PIPE_TEXTURE_RECT
;
83 return PIPE_TEXTURE_3D
;
84 case GL_TEXTURE_CUBE_MAP_ARB
:
85 return PIPE_TEXTURE_CUBE
;
86 case GL_TEXTURE_1D_ARRAY_EXT
:
87 return PIPE_TEXTURE_1D_ARRAY
;
88 case GL_TEXTURE_2D_ARRAY_EXT
:
89 return PIPE_TEXTURE_2D_ARRAY
;
97 /** called via ctx->Driver.NewTextureImage() */
98 static struct gl_texture_image
*
99 st_NewTextureImage(struct gl_context
* ctx
)
101 DBG("%s\n", __FUNCTION__
);
103 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
107 /** called via ctx->Driver.NewTextureObject() */
108 static struct gl_texture_object
*
109 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
111 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
113 DBG("%s\n", __FUNCTION__
);
114 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
119 /** called via ctx->Driver.DeleteTextureObject() */
121 st_DeleteTextureObject(struct gl_context
*ctx
,
122 struct gl_texture_object
*texObj
)
124 struct st_context
*st
= st_context(ctx
);
125 struct st_texture_object
*stObj
= st_texture_object(texObj
);
127 pipe_resource_reference(&stObj
->pt
, NULL
);
128 if (stObj
->sampler_view
) {
129 if (stObj
->sampler_view
->context
!= st
->pipe
) {
130 /* Take "ownership" of this texture sampler view by setting
131 * its context pointer to this context. This avoids potential
132 * crashes when the texture object is shared among contexts
133 * and the original/owner context has already been destroyed.
135 stObj
->sampler_view
->context
= st
->pipe
;
137 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
139 _mesa_delete_texture_object(ctx
, texObj
);
143 /** called via ctx->Driver.FreeTexImageData() */
145 st_FreeTextureImageData(struct gl_context
* ctx
, struct gl_texture_image
*texImage
)
147 struct st_texture_image
*stImage
= st_texture_image(texImage
);
149 DBG("%s\n", __FUNCTION__
);
152 pipe_resource_reference(&stImage
->pt
, NULL
);
155 if (texImage
->Data
) {
156 _mesa_align_free(texImage
->Data
);
157 texImage
->Data
= NULL
;
163 * From linux kernel i386 header files, copes with odd sizes better
164 * than COPY_DWORDS would:
165 * XXX Put this in src/mesa/main/imports.h ???
167 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
169 __memcpy(void *to
, const void *from
, size_t n
)
172 __asm__
__volatile__("rep ; movsl\n\t"
176 "1:\ttestb $1,%b4\n\t"
178 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
179 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
184 #define __memcpy(a,b,c) memcpy(a,b,c)
189 * The system memcpy (at least on ubuntu 5.10) has problems copying
190 * to agp (writecombined) memory from a source which isn't 64-byte
191 * aligned - there is a 4x performance falloff.
193 * The x86 __memcpy is immune to this but is slightly slower
194 * (10%-ish) than the system memcpy.
196 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
197 * isn't much faster than x86_memcpy for agp copies.
199 * TODO: switch dynamically.
202 do_memcpy(void *dest
, const void *src
, size_t n
)
204 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
205 return __memcpy(dest
, src
, n
);
208 return memcpy(dest
, src
, n
);
213 * Return default texture resource binding bitmask for the given format.
216 default_bindings(struct st_context
*st
, enum pipe_format format
)
218 struct pipe_screen
*screen
= st
->pipe
->screen
;
219 const unsigned target
= PIPE_TEXTURE_2D
;
220 const unsigned geom
= 0x0;
223 if (util_format_is_depth_or_stencil(format
))
224 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
226 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
228 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
, geom
))
231 return PIPE_BIND_SAMPLER_VIEW
;
235 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
237 get_texture_dims(GLenum target
)
241 case GL_TEXTURE_1D_ARRAY_EXT
:
244 case GL_TEXTURE_CUBE_MAP_ARB
:
245 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
246 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
247 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
248 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
249 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
250 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
251 case GL_TEXTURE_RECTANGLE_NV
:
252 case GL_TEXTURE_2D_ARRAY_EXT
:
257 assert(0 && "invalid texture target in get_texture_dims()");
264 * Given the size of a mipmap image, try to compute the size of the level=0
267 * Note that this isn't always accurate for odd-sized, non-POW textures.
268 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
270 * \return GL_TRUE for success, GL_FALSE for failure
273 guess_base_level_size(GLenum target
,
274 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
275 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
277 const GLuint dims
= get_texture_dims(target
);
284 /* Depending on the image's size, we can't always make a guess here */
285 if ((dims
>= 1 && width
== 1) ||
286 (dims
>= 2 && height
== 1) ||
287 (dims
>= 3 && depth
== 1)) {
288 /* we can't determine the image size at level=0 */
292 /* grow the image size until we hit level = 0 */
313 * Try to allocate a pipe_resource object for the given st_texture_object.
315 * We use the given st_texture_image as a clue to determine the size of the
316 * mipmap image at level=0.
318 * \return GL_TRUE for success, GL_FALSE if out of memory.
321 guess_and_alloc_texture(struct st_context
*st
,
322 struct st_texture_object
*stObj
,
323 const struct st_texture_image
*stImage
)
325 GLuint lastLevel
, width
, height
, depth
;
327 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
328 enum pipe_format fmt
;
330 DBG("%s\n", __FUNCTION__
);
334 if (!guess_base_level_size(stObj
->base
.Target
,
335 stImage
->base
.Width2
,
336 stImage
->base
.Height2
,
337 stImage
->base
.Depth2
,
339 &width
, &height
, &depth
)) {
340 /* we can't determine the image size at level=0 */
341 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
342 /* this is not an out of memory error */
346 /* At this point, (width x height x depth) is the expected size of
347 * the level=0 mipmap image.
350 /* Guess a reasonable value for lastLevel. With OpenGL we have no
351 * idea how many mipmap levels will be in a texture until we start
352 * to render with it. Make an educated guess here but be prepared
353 * to re-allocating a texture buffer with space for more (or fewer)
354 * mipmap levels later.
356 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
357 stObj
->base
.MinFilter
== GL_LINEAR
||
358 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
359 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
360 !stObj
->base
.GenerateMipmap
&&
361 stImage
->level
== 0) {
362 /* only alloc space for a single mipmap level */
366 /* alloc space for a full mipmap */
367 GLuint l2width
= util_logbase2(width
);
368 GLuint l2height
= util_logbase2(height
);
369 GLuint l2depth
= util_logbase2(depth
);
370 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
373 /* Save the level=0 dimensions */
374 stObj
->width0
= width
;
375 stObj
->height0
= height
;
376 stObj
->depth0
= depth
;
378 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
380 bindings
= default_bindings(st
, fmt
);
382 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
383 width
, height
, depth
,
384 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
386 stObj
->pt
= st_texture_create(st
,
387 gl_target_to_pipe(stObj
->base
.Target
),
396 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
398 return stObj
->pt
!= NULL
;
403 * Adjust pixel unpack params and image dimensions to strip off the
405 * Gallium doesn't support texture borders. They've seldem been used
406 * and seldom been implemented correctly anyway.
407 * \param unpackNew returns the new pixel unpack parameters
410 strip_texture_border(GLint border
,
411 GLint
*width
, GLint
*height
, GLint
*depth
,
412 const struct gl_pixelstore_attrib
*unpack
,
413 struct gl_pixelstore_attrib
*unpackNew
)
415 assert(border
> 0); /* sanity check */
417 *unpackNew
= *unpack
;
419 if (unpackNew
->RowLength
== 0)
420 unpackNew
->RowLength
= *width
;
422 if (depth
&& unpackNew
->ImageHeight
== 0)
423 unpackNew
->ImageHeight
= *height
;
425 unpackNew
->SkipPixels
+= border
;
427 unpackNew
->SkipRows
+= border
;
429 unpackNew
->SkipImages
+= border
;
432 *width
= *width
- 2 * border
;
433 if (height
&& *height
>= 3)
434 *height
= *height
- 2 * border
;
435 if (depth
&& *depth
>= 3)
436 *depth
= *depth
- 2 * border
;
441 * Do glTexImage1/2/3D().
444 st_TexImage(struct gl_context
* ctx
,
446 GLenum target
, GLint level
,
447 GLint internalFormat
,
448 GLint width
, GLint height
, GLint depth
,
450 GLenum format
, GLenum type
, const void *pixels
,
451 const struct gl_pixelstore_attrib
*unpack
,
452 struct gl_texture_object
*texObj
,
453 struct gl_texture_image
*texImage
,
454 GLsizei imageSize
, GLboolean compressed_src
)
456 struct st_context
*st
= st_context(ctx
);
457 struct st_texture_object
*stObj
= st_texture_object(texObj
);
458 struct st_texture_image
*stImage
= st_texture_image(texImage
);
459 GLuint dstRowStride
= 0;
460 struct gl_pixelstore_attrib unpackNB
;
461 enum pipe_transfer_usage transfer_usage
= 0;
463 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
464 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
466 /* switch to "normal" */
467 if (stObj
->surface_based
) {
470 _mesa_clear_texture_object(ctx
, texObj
);
471 pipe_resource_reference(&stObj
->pt
, NULL
);
473 /* oops, need to init this image again */
474 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
475 internalFormat
, format
, type
);
477 _mesa_init_teximage_fields(ctx
, target
, texImage
,
478 width
, height
, depth
, border
,
479 internalFormat
, texFormat
);
481 stObj
->surface_based
= GL_FALSE
;
484 /* gallium does not support texture borders, strip it off */
486 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
488 texImage
->Width
= width
;
489 texImage
->Height
= height
;
490 texImage
->Depth
= depth
;
491 texImage
->Border
= 0;
495 assert(texImage
->Width
== width
);
496 assert(texImage
->Height
== height
);
497 assert(texImage
->Depth
== depth
);
500 stImage
->face
= _mesa_tex_target_to_face(target
);
501 stImage
->level
= level
;
503 _mesa_set_fetch_functions(texImage
, dims
);
505 /* Release the reference to a potentially orphaned buffer.
506 * Release any old malloced memory.
509 pipe_resource_reference(&stImage
->pt
, NULL
);
510 assert(!texImage
->Data
);
512 else if (texImage
->Data
) {
513 _mesa_align_free(texImage
->Data
);
517 * See if the new image is somehow incompatible with the existing
518 * mipmap. If so, free the old mipmap.
521 if (level
> (GLint
) stObj
->pt
->last_level
||
522 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
523 stImage
->face
, stImage
->level
)) {
525 pipe_resource_reference(&stObj
->pt
, NULL
);
527 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
531 if (width
== 0 || height
== 0 || depth
== 0) {
532 /* stop after freeing old image */
537 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
538 /* Probably out of memory.
539 * Try flushing any pending rendering, then retry.
542 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
543 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
549 assert(!stImage
->pt
);
551 /* Check if this texture image can live inside the texture object's buffer.
552 * If so, store the image there. Otherwise the image will temporarily live
556 st_texture_match_image(stObj
->pt
, &stImage
->base
,
557 stImage
->face
, stImage
->level
)) {
559 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
564 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
566 /* Pixel data may come from regular user memory or a PBO. For the later,
567 * do bounds checking and map the PBO to read pixels data from it.
569 * XXX we should try to use a GPU-accelerated path to copy the image data
570 * from the PBO to the texture.
572 if (compressed_src
) {
573 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
575 "glCompressedTexImage");
578 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
580 pixels
, unpack
, "glTexImage");
583 /* for a 1D array upload the image as a series of layer with height = 1 */
584 if (target
== GL_TEXTURE_1D_ARRAY
) {
590 * Prepare to store the texture data. Either map the gallium texture buffer
591 * memory or malloc space for it.
594 /* Store the image in the gallium texture memory buffer */
595 if (format
== GL_DEPTH_COMPONENT
&&
596 util_format_is_depth_and_stencil(stImage
->pt
->format
))
597 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
599 transfer_usage
= PIPE_TRANSFER_WRITE
;
601 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
602 transfer_usage
, 0, 0, width
, height
);
603 if(stImage
->transfer
)
604 dstRowStride
= stImage
->transfer
->stride
;
607 /* Allocate regular memory and store the image there temporarily. */
608 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
609 width
, height
, depth
);
610 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
612 texImage
->Data
= _mesa_align_malloc(imageSize
, 16);
615 if (!texImage
->Data
) {
616 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
621 /* We've allocated texture memory, but have no pixel data - all done. */
625 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
626 width
, height
, depth
, width
, dstRowStride
);
628 /* Copy user texture image into the texture buffer.
630 if (compressed_src
) {
631 const GLuint srcRowStride
=
632 _mesa_format_row_stride(texImage
->TexFormat
, width
);
633 if (dstRowStride
== srcRowStride
) {
634 memcpy(texImage
->Data
, pixels
, imageSize
);
637 char *dst
= texImage
->Data
;
638 const char *src
= pixels
;
639 GLuint i
, bw
, bh
, lines
;
640 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
641 lines
= (height
+ bh
- 1) / bh
;
643 for (i
= 0; i
< lines
; ++i
) {
644 memcpy(dst
, src
, srcRowStride
);
651 const GLuint srcImageStride
=
652 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
654 const GLubyte
*src
= (const GLubyte
*) pixels
;
656 for (i
= 0; i
< depth
; i
++) {
657 if (!_mesa_texstore(ctx
, dims
,
658 texImage
->_BaseFormat
,
661 0, 0, 0, /* dstX/Y/Zoffset */
663 texImage
->ImageOffsets
,
665 format
, type
, src
, unpack
)) {
666 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
669 if (stImage
->pt
&& i
+ 1 < depth
) {
670 /* unmap this slice */
671 st_texture_image_unmap(st
, stImage
);
672 /* map next slice of 3D texture */
673 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
674 transfer_usage
, 0, 0,
676 src
+= srcImageStride
;
682 _mesa_unmap_teximage_pbo(ctx
, unpack
);
684 if (stImage
->pt
&& texImage
->Data
) {
685 st_texture_image_unmap(st
, stImage
);
686 texImage
->Data
= NULL
;
692 st_TexImage3D(struct gl_context
* ctx
,
693 GLenum target
, GLint level
,
694 GLint internalFormat
,
695 GLint width
, GLint height
, GLint depth
,
697 GLenum format
, GLenum type
, const void *pixels
,
698 const struct gl_pixelstore_attrib
*unpack
,
699 struct gl_texture_object
*texObj
,
700 struct gl_texture_image
*texImage
)
702 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
703 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
709 st_TexImage2D(struct gl_context
* ctx
,
710 GLenum target
, GLint level
,
711 GLint internalFormat
,
712 GLint width
, GLint height
, GLint border
,
713 GLenum format
, GLenum type
, const void *pixels
,
714 const struct gl_pixelstore_attrib
*unpack
,
715 struct gl_texture_object
*texObj
,
716 struct gl_texture_image
*texImage
)
718 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
719 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
724 st_TexImage1D(struct gl_context
* ctx
,
725 GLenum target
, GLint level
,
726 GLint internalFormat
,
727 GLint width
, GLint border
,
728 GLenum format
, GLenum type
, const void *pixels
,
729 const struct gl_pixelstore_attrib
*unpack
,
730 struct gl_texture_object
*texObj
,
731 struct gl_texture_image
*texImage
)
733 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
734 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
739 st_CompressedTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
740 GLint internalFormat
,
741 GLint width
, GLint height
, GLint border
,
742 GLsizei imageSize
, const GLvoid
*data
,
743 struct gl_texture_object
*texObj
,
744 struct gl_texture_image
*texImage
)
746 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
747 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
753 * glGetTexImage() helper: decompress a compressed texture by rendering
754 * a textured quad. Store the results in the user's buffer.
757 decompress_with_blit(struct gl_context
* ctx
, GLenum target
, GLint level
,
758 GLenum format
, GLenum type
, GLvoid
*pixels
,
759 struct gl_texture_object
*texObj
,
760 struct gl_texture_image
*texImage
)
762 struct st_context
*st
= st_context(ctx
);
763 struct pipe_context
*pipe
= st
->pipe
;
764 struct st_texture_image
*stImage
= st_texture_image(texImage
);
765 struct st_texture_object
*stObj
= st_texture_object(texObj
);
766 struct pipe_sampler_view
*src_view
=
767 st_get_texture_sampler_view(stObj
, pipe
);
768 const GLuint width
= texImage
->Width
;
769 const GLuint height
= texImage
->Height
;
770 struct pipe_surface
*dst_surface
;
771 struct pipe_resource
*dst_texture
;
772 struct pipe_transfer
*tex_xfer
;
773 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
774 PIPE_BIND_TRANSFER_READ
);
776 /* create temp / dest surface */
777 if (!util_create_rgba_surface(pipe
, width
, height
, bind
,
778 &dst_texture
, &dst_surface
)) {
779 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
780 "in decompress_with_blit()");
784 /* blit/render/decompress */
785 util_blit_pixels_tex(st
->blit
,
786 src_view
, /* pipe_resource (src) */
787 0, 0, /* src x0, y0 */
788 width
, height
, /* src x1, y1 */
789 dst_surface
, /* pipe_surface (dst) */
790 0, 0, /* dst x0, y0 */
791 width
, height
, /* dst x1, y1 */
793 PIPE_TEX_MIPFILTER_NEAREST
);
795 /* map the dst_surface so we can read from it */
796 tex_xfer
= pipe_get_transfer(st_context(ctx
)->pipe
,
799 0, 0, width
, height
);
801 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
803 /* copy/pack data into user buffer */
804 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
806 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
807 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
809 for (row
= 0; row
< height
; row
++) {
810 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
811 height
, format
, type
, row
, 0);
812 memcpy(dest
, map
, bytesPerRow
);
813 map
+= tex_xfer
->stride
;
815 pipe_transfer_unmap(pipe
, tex_xfer
);
818 /* format translation via floats */
820 enum pipe_format format
= util_format_linear(dst_texture
->format
);
821 for (row
= 0; row
< height
; row
++) {
822 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
823 GLfloat rgba
[4 * MAX_WIDTH
];
824 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
825 height
, format
, type
, row
, 0);
827 if (ST_DEBUG
& DEBUG_FALLBACK
)
828 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
830 /* get float[4] rgba row from surface */
831 pipe_get_tile_rgba_format(pipe
, tex_xfer
, 0, row
, width
, 1,
834 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
835 type
, dest
, &ctx
->Pack
, transferOps
);
839 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
841 pipe
->transfer_destroy(pipe
, tex_xfer
);
843 /* destroy the temp / dest surface */
844 util_destroy_rgba_surface(dst_texture
, dst_surface
);
850 * Need to map texture image into memory before copying image data,
854 st_get_tex_image(struct gl_context
* ctx
, GLenum target
, GLint level
,
855 GLenum format
, GLenum type
, GLvoid
* pixels
,
856 struct gl_texture_object
*texObj
,
857 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
859 struct st_context
*st
= st_context(ctx
);
860 struct st_texture_image
*stImage
= st_texture_image(texImage
);
861 const GLuint dstImageStride
=
862 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
868 util_format_is_s3tc(stImage
->pt
->format
) &&
870 /* Need to decompress the texture.
871 * We'll do this by rendering a textured quad.
872 * Note that we only expect RGBA formats (no Z/depth formats).
874 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
881 /* Image is stored in hardware format in a buffer managed by the
882 * kernel. Need to explicitly map and unmap it.
884 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
885 PIPE_TRANSFER_READ
, 0, 0,
887 stImage
->base
.Height
);
888 /* compute stride in texels from stride in bytes */
889 texImage
->RowStride
= stImage
->transfer
->stride
890 * util_format_get_blockwidth(stImage
->pt
->format
)
891 / util_format_get_blocksize(stImage
->pt
->format
);
894 /* Otherwise, the image should actually be stored in
895 * texImage->Data. This is pretty confusing for
896 * everybody, I'd much prefer to separate the two functions of
897 * texImage->Data - storage for texture images in main memory
898 * and access (ie mappings) of images. In other words, we'd
899 * create a new texImage->Map field and leave Data simply for
902 assert(texImage
->Data
);
905 depth
= texImage
->Depth
;
908 dest
= (GLubyte
*) pixels
;
910 _mesa_set_fetch_functions(texImage
, get_texture_dims(target
));
912 for (i
= 0; i
< depth
; i
++) {
913 if (compressed_dst
) {
914 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
918 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
922 if (stImage
->pt
&& i
+ 1 < depth
) {
923 /* unmap this slice */
924 st_texture_image_unmap(st
, stImage
);
925 /* map next slice of 3D texture */
926 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
927 PIPE_TRANSFER_READ
, 0, 0,
929 stImage
->base
.Height
);
930 dest
+= dstImageStride
;
934 texImage
->Depth
= depth
;
938 st_texture_image_unmap(st
, stImage
);
939 texImage
->Data
= NULL
;
945 st_GetTexImage(struct gl_context
* ctx
, GLenum target
, GLint level
,
946 GLenum format
, GLenum type
, GLvoid
* pixels
,
947 struct gl_texture_object
*texObj
,
948 struct gl_texture_image
*texImage
)
950 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
956 st_GetCompressedTexImage(struct gl_context
*ctx
, GLenum target
, GLint level
,
958 struct gl_texture_object
*texObj
,
959 struct gl_texture_image
*texImage
)
961 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
968 st_TexSubimage(struct gl_context
*ctx
, GLint dims
, GLenum target
, GLint level
,
969 GLint xoffset
, GLint yoffset
, GLint zoffset
,
970 GLint width
, GLint height
, GLint depth
,
971 GLenum format
, GLenum type
, const void *pixels
,
972 const struct gl_pixelstore_attrib
*packing
,
973 struct gl_texture_object
*texObj
,
974 struct gl_texture_image
*texImage
)
976 struct st_context
*st
= st_context(ctx
);
977 struct st_texture_image
*stImage
= st_texture_image(texImage
);
979 const GLuint srcImageStride
=
980 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
983 /* init to silence warning only: */
984 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
986 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
987 _mesa_lookup_enum_by_nr(target
),
988 level
, xoffset
, yoffset
, width
, height
);
991 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
992 type
, pixels
, packing
, "glTexSubImage2D");
996 /* for a 1D array upload the image as a series of layer with height = 1 */
997 if (target
== GL_TEXTURE_1D_ARRAY
) {
1002 /* Map buffer if necessary. Need to lock to prevent other contexts
1003 * from uploading the buffer under us.
1006 if (format
== GL_DEPTH_COMPONENT
&&
1007 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1008 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1010 transfer_usage
= PIPE_TRANSFER_WRITE
;
1012 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1018 if (!texImage
->Data
) {
1019 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1023 src
= (const GLubyte
*) pixels
;
1024 dstRowStride
= stImage
->transfer
->stride
;
1026 for (i
= 0; i
< depth
; i
++) {
1027 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1028 texImage
->TexFormat
,
1032 texImage
->ImageOffsets
,
1034 format
, type
, src
, packing
)) {
1035 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1038 if (stImage
->pt
&& i
+ 1 < depth
) {
1039 /* unmap this slice */
1040 st_texture_image_unmap(st
, stImage
);
1041 /* map next slice of 3D texture */
1042 texImage
->Data
= st_texture_image_map(st
, stImage
,
1047 src
+= srcImageStride
;
1052 _mesa_unmap_teximage_pbo(ctx
, packing
);
1054 if (stImage
->pt
&& texImage
->Data
) {
1055 st_texture_image_unmap(st
, stImage
);
1056 texImage
->Data
= NULL
;
1063 st_TexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1064 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1065 GLsizei width
, GLsizei height
, GLsizei depth
,
1066 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1067 const struct gl_pixelstore_attrib
*packing
,
1068 struct gl_texture_object
*texObj
,
1069 struct gl_texture_image
*texImage
)
1071 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1072 width
, height
, depth
, format
, type
,
1073 pixels
, packing
, texObj
, texImage
);
1078 st_TexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1079 GLint xoffset
, GLint yoffset
,
1080 GLsizei width
, GLsizei height
,
1081 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1082 const struct gl_pixelstore_attrib
*packing
,
1083 struct gl_texture_object
*texObj
,
1084 struct gl_texture_image
*texImage
)
1086 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1087 width
, height
, 1, format
, type
,
1088 pixels
, packing
, texObj
, texImage
);
1093 st_TexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1094 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1095 const GLvoid
* pixels
,
1096 const struct gl_pixelstore_attrib
*packing
,
1097 struct gl_texture_object
*texObj
,
1098 struct gl_texture_image
*texImage
)
1100 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1101 format
, type
, pixels
, packing
, texObj
, texImage
);
1106 st_CompressedTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1107 GLint xoffset
, GLsizei width
,
1109 GLsizei imageSize
, const GLvoid
*data
,
1110 struct gl_texture_object
*texObj
,
1111 struct gl_texture_image
*texImage
)
1118 st_CompressedTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1119 GLint xoffset
, GLint yoffset
,
1120 GLsizei width
, GLint height
,
1122 GLsizei imageSize
, const GLvoid
*data
,
1123 struct gl_texture_object
*texObj
,
1124 struct gl_texture_image
*texImage
)
1126 struct st_context
*st
= st_context(ctx
);
1127 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1131 enum pipe_format pformat
;
1134 pformat
= stImage
->pt
->format
;
1136 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1137 PIPE_TRANSFER_WRITE
,
1141 srcBlockStride
= util_format_get_stride(pformat
, width
);
1142 dstBlockStride
= stImage
->transfer
->stride
;
1144 assert(stImage
->pt
);
1145 /* TODO find good values for block and strides */
1146 /* TODO also adjust texImage->data for yoffset/xoffset */
1150 if (!texImage
->Data
) {
1151 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1155 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1156 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1158 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1159 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1160 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1161 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1162 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1166 st_texture_image_unmap(st
, stImage
);
1167 texImage
->Data
= NULL
;
1173 st_CompressedTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1174 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1175 GLsizei width
, GLint height
, GLint depth
,
1177 GLsizei imageSize
, const GLvoid
*data
,
1178 struct gl_texture_object
*texObj
,
1179 struct gl_texture_image
*texImage
)
1187 * Do a CopyTexSubImage operation using a read transfer from the source,
1188 * a write transfer to the destination and get_tile()/put_tile() to access
1189 * the pixels/texels.
1191 * Note: srcY=0=TOP of renderbuffer
1194 fallback_copy_texsubimage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1195 struct st_renderbuffer
*strb
,
1196 struct st_texture_image
*stImage
,
1198 GLint destX
, GLint destY
, GLint destZ
,
1199 GLint srcX
, GLint srcY
,
1200 GLsizei width
, GLsizei height
)
1202 struct st_context
*st
= st_context(ctx
);
1203 struct pipe_context
*pipe
= st
->pipe
;
1204 struct pipe_transfer
*src_trans
;
1206 enum pipe_transfer_usage transfer_usage
;
1208 if (ST_DEBUG
& DEBUG_FALLBACK
)
1209 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1211 assert(width
<= MAX_WIDTH
);
1213 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1214 srcY
= strb
->Base
.Height
- srcY
- height
;
1217 src_trans
= pipe_get_transfer(st_context(ctx
)->pipe
,
1224 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1225 baseFormat
== GL_DEPTH_STENCIL
) &&
1226 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1227 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1229 transfer_usage
= PIPE_TRANSFER_WRITE
;
1231 /* XXX this used to ignore destZ param */
1232 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
1233 destX
, destY
, width
, height
);
1235 if (baseFormat
== GL_DEPTH_COMPONENT
||
1236 baseFormat
== GL_DEPTH_STENCIL
) {
1237 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1238 ctx
->Pixel
.DepthBias
!= 0.0F
);
1241 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1242 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1251 /* To avoid a large temp memory allocation, do copy row by row */
1252 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1253 uint data
[MAX_WIDTH
];
1254 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1256 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1258 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1264 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1266 if (tempSrc
&& texDest
) {
1267 const GLint dims
= 2;
1268 const GLint dstRowStride
= stImage
->transfer
->stride
;
1269 struct gl_texture_image
*texImage
= &stImage
->base
;
1270 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1272 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1273 unpack
.Invert
= GL_TRUE
;
1276 /* get float/RGBA image from framebuffer */
1277 /* XXX this usually involves a lot of int/float conversion.
1278 * try to avoid that someday.
1280 pipe_get_tile_rgba_format(pipe
, src_trans
, 0, 0, width
, height
,
1281 util_format_linear(strb
->texture
->format
),
1284 /* Store into texture memory.
1285 * Note that this does some special things such as pixel transfer
1286 * ops and format conversion. In particular, if the dest tex format
1287 * is actually RGBA but the user created the texture as GL_RGB we
1288 * need to fill-in/override the alpha channel with 1.0.
1290 _mesa_texstore(ctx
, dims
,
1291 texImage
->_BaseFormat
,
1292 texImage
->TexFormat
,
1296 texImage
->ImageOffsets
,
1298 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1302 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1309 st_texture_image_unmap(st
, stImage
);
1310 pipe
->transfer_destroy(pipe
, src_trans
);
1316 * If the format of the src renderbuffer and the format of the dest
1317 * texture are compatible (in terms of blitting), return a TGSI writemask
1318 * to be used during the blit.
1319 * If the src/dest are incompatible, return 0.
1322 compatible_src_dst_formats(struct gl_context
*ctx
,
1323 const struct gl_renderbuffer
*src
,
1324 const struct gl_texture_image
*dst
)
1326 /* Get logical base formats for the src and dest.
1327 * That is, use the user-requested formats and not the actual, device-
1329 * For example, the user may have requested an A8 texture but the
1330 * driver may actually be using an RGBA texture format. When we
1331 * copy/blit to that texture, we only want to copy the Alpha channel
1332 * and not the RGB channels.
1334 * Similarly, when the src FBO was created an RGB format may have been
1335 * requested but the driver actually chose an RGBA format. In that case,
1336 * we don't want to copy the undefined Alpha channel to the dest texture
1337 * (it should be 1.0).
1339 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1340 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1343 * XXX when we have red-only and red/green renderbuffers we'll need
1344 * to add more cases here (or implement a general-purpose routine that
1345 * queries the existance of the R,G,B,A channels in the src and dest).
1347 if (srcFormat
== dstFormat
) {
1348 /* This is the same as matching_base_formats, which should
1349 * always pass, as it did previously.
1351 return TGSI_WRITEMASK_XYZW
;
1353 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1354 /* Make sure that A in the dest is 1. The actual src format
1355 * may be RGBA and have undefined A values.
1357 return TGSI_WRITEMASK_XYZ
;
1359 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1360 /* Make sure that A in the dest is 1. The actual dst format
1361 * may be RGBA and will need A=1 to provide proper alpha values
1362 * when sampled later.
1364 return TGSI_WRITEMASK_XYZ
;
1367 if (ST_DEBUG
& DEBUG_FALLBACK
)
1368 debug_printf("%s failed for src %s, dst %s\n",
1370 _mesa_lookup_enum_by_nr(srcFormat
),
1371 _mesa_lookup_enum_by_nr(dstFormat
));
1382 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1383 * Note that the region to copy has already been clipped so we know we
1384 * won't read from outside the source renderbuffer's bounds.
1386 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1389 st_copy_texsubimage(struct gl_context
*ctx
,
1390 GLenum target
, GLint level
,
1391 GLint destX
, GLint destY
, GLint destZ
,
1392 GLint srcX
, GLint srcY
,
1393 GLsizei width
, GLsizei height
)
1395 struct gl_texture_unit
*texUnit
=
1396 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1397 struct gl_texture_object
*texObj
=
1398 _mesa_select_tex_object(ctx
, texUnit
, target
);
1399 struct gl_texture_image
*texImage
=
1400 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1401 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1402 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1403 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1404 struct st_renderbuffer
*strb
;
1405 struct st_context
*st
= st_context(ctx
);
1406 struct pipe_context
*pipe
= st
->pipe
;
1407 struct pipe_screen
*screen
= pipe
->screen
;
1408 enum pipe_format dest_format
, src_format
;
1409 GLboolean use_fallback
= GL_TRUE
;
1410 GLboolean matching_base_formats
;
1411 GLuint format_writemask
, sample_count
;
1412 struct pipe_surface
*dest_surface
= NULL
;
1413 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1415 /* make sure finalize_textures has been called?
1417 if (0) st_validate_state(st
);
1419 /* determine if copying depth or color data */
1420 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1421 texBaseFormat
== GL_DEPTH_STENCIL
) {
1422 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1423 if (strb
->Base
.Wrapped
) {
1424 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
1428 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1429 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1432 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1433 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1437 sample_count
= strb
->surface
->texture
->nr_samples
;
1438 /* I believe this would be legal, presumably would need to do a resolve
1439 for color, and for depth/stencil spec says to just use one of the
1440 depth/stencil samples per pixel? Need some transfer clarifications. */
1441 assert(sample_count
< 2);
1467 if (width
< 0 || height
< 0)
1472 assert(strb
->surface
);
1473 assert(stImage
->pt
);
1475 src_format
= strb
->surface
->format
;
1476 dest_format
= stImage
->pt
->format
;
1479 * Determine if the src framebuffer and dest texture have the same
1480 * base format. We need this to detect a case such as the framebuffer
1481 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1482 * texture format stores RGBA we need to set A=1 (overriding the
1483 * framebuffer's alpha values). We can't do that with the blit or
1484 * textured-quad paths.
1486 matching_base_formats
=
1487 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1488 _mesa_get_format_base_format(texImage
->TexFormat
));
1489 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1491 if (ctx
->_ImageTransferState
== 0x0) {
1493 if (matching_base_formats
&&
1494 src_format
== dest_format
&&
1497 /* use surface_copy() / blit */
1498 struct pipe_box src_box
;
1499 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
1500 width
, height
, &src_box
);
1502 /* for resource_copy_region(), y=0=top, always */
1503 pipe
->resource_copy_region(pipe
,
1507 destX
, destY
, destZ
+ stImage
->face
,
1510 strb
->surface
->u
.tex
.level
,
1512 use_fallback
= GL_FALSE
;
1514 else if (format_writemask
&&
1515 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1516 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1517 screen
->is_format_supported(screen
, src_format
,
1518 PIPE_TEXTURE_2D
, sample_count
,
1519 PIPE_BIND_SAMPLER_VIEW
,
1521 screen
->is_format_supported(screen
, dest_format
,
1523 PIPE_BIND_RENDER_TARGET
,
1525 /* draw textured quad to do the copy */
1527 struct pipe_surface surf_tmpl
;
1528 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1529 surf_tmpl
.format
= stImage
->pt
->format
;
1530 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
1531 surf_tmpl
.u
.tex
.level
= stImage
->level
;
1532 surf_tmpl
.u
.tex
.first_layer
= stImage
->face
+ destZ
;
1533 surf_tmpl
.u
.tex
.last_layer
= stImage
->face
+ destZ
;
1535 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1539 srcY1
= strb
->Base
.Height
- srcY
- height
;
1540 srcY0
= srcY1
+ height
;
1544 srcY1
= srcY0
+ height
;
1547 util_blit_pixels_writemask(st
->blit
,
1549 strb
->surface
->u
.tex
.level
,
1551 srcX
+ width
, srcY1
,
1552 strb
->surface
->u
.tex
.first_layer
,
1555 destX
+ width
, destY
+ height
,
1556 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1558 use_fallback
= GL_FALSE
;
1562 pipe_surface_reference(&dest_surface
, NULL
);
1566 /* software fallback */
1567 fallback_copy_texsubimage(ctx
, target
, level
,
1568 strb
, stImage
, texBaseFormat
,
1569 destX
, destY
, destZ
,
1570 srcX
, srcY
, width
, height
);
1577 st_CopyTexImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1578 GLenum internalFormat
,
1579 GLint x
, GLint y
, GLsizei width
, GLint border
)
1581 struct gl_texture_unit
*texUnit
=
1582 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1583 struct gl_texture_object
*texObj
=
1584 _mesa_select_tex_object(ctx
, texUnit
, target
);
1585 struct gl_texture_image
*texImage
=
1586 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1588 /* Setup or redefine the texture object, texture and texture
1589 * image. Don't populate yet.
1591 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1593 GL_RGBA
, CHAN_TYPE
, NULL
,
1594 &ctx
->DefaultPacking
, texObj
, texImage
);
1596 st_copy_texsubimage(ctx
, target
, level
,
1597 0, 0, 0, /* destX,Y,Z */
1598 x
, y
, width
, 1); /* src X, Y, size */
1603 st_CopyTexImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1604 GLenum internalFormat
,
1605 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1608 struct gl_texture_unit
*texUnit
=
1609 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1610 struct gl_texture_object
*texObj
=
1611 _mesa_select_tex_object(ctx
, texUnit
, target
);
1612 struct gl_texture_image
*texImage
=
1613 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1615 /* Setup or redefine the texture object, texture and texture
1616 * image. Don't populate yet.
1618 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1619 width
, height
, border
,
1620 GL_RGBA
, CHAN_TYPE
, NULL
,
1621 &ctx
->DefaultPacking
, texObj
, texImage
);
1623 st_copy_texsubimage(ctx
, target
, level
,
1624 0, 0, 0, /* destX,Y,Z */
1625 x
, y
, width
, height
); /* src X, Y, size */
1630 st_CopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1631 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1633 const GLint yoffset
= 0, zoffset
= 0;
1634 const GLsizei height
= 1;
1635 st_copy_texsubimage(ctx
, target
, level
,
1636 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1637 x
, y
, width
, height
); /* src X, Y, size */
1642 st_CopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1643 GLint xoffset
, GLint yoffset
,
1644 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1646 const GLint zoffset
= 0;
1647 st_copy_texsubimage(ctx
, target
, level
,
1648 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1649 x
, y
, width
, height
); /* src X, Y, size */
1654 st_CopyTexSubImage3D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1655 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1656 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1658 st_copy_texsubimage(ctx
, target
, level
,
1659 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1660 x
, y
, width
, height
); /* src X, Y, size */
1665 * Copy image data from stImage into the texture object 'stObj' at level
1669 copy_image_data_to_texture(struct st_context
*st
,
1670 struct st_texture_object
*stObj
,
1672 struct st_texture_image
*stImage
)
1676 const struct gl_texture_image
*dstImage
=
1677 stObj
->base
.Image
[stImage
->face
][stImage
->level
];
1679 assert(dstImage
->Width
== stImage
->base
.Width
);
1680 assert(dstImage
->Height
== stImage
->base
.Height
);
1681 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1685 /* Copy potentially with the blitter:
1687 st_texture_image_copy(st
->pipe
,
1688 stObj
->pt
, dstLevel
, /* dest texture, level */
1689 stImage
->pt
, stImage
->level
, /* src texture, level */
1692 pipe_resource_reference(&stImage
->pt
, NULL
);
1694 else if (stImage
->base
.Data
) {
1695 st_texture_image_data(st
,
1700 stImage
->base
.RowStride
*
1701 util_format_get_blocksize(stObj
->pt
->format
),
1702 stImage
->base
.RowStride
*
1703 stImage
->base
.Height
*
1704 util_format_get_blocksize(stObj
->pt
->format
));
1705 _mesa_align_free(stImage
->base
.Data
);
1706 stImage
->base
.Data
= NULL
;
1709 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1714 * Called during state validation. When this function is finished,
1715 * the texture object should be ready for rendering.
1716 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1719 st_finalize_texture(struct gl_context
*ctx
,
1720 struct pipe_context
*pipe
,
1721 struct gl_texture_object
*tObj
)
1723 struct st_context
*st
= st_context(ctx
);
1724 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1725 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1727 struct st_texture_image
*firstImage
;
1728 enum pipe_format firstImageFormat
;
1729 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1731 if (stObj
->base
._Complete
) {
1732 /* The texture is complete and we know exactly how many mipmap levels
1733 * are present/needed. This is conditional because we may be called
1734 * from the st_generate_mipmap() function when the texture object is
1735 * incomplete. In that case, we'll have set stObj->lastLevel before
1738 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1739 stObj
->base
.MinFilter
== GL_NEAREST
)
1740 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1742 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1745 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1748 /* If both firstImage and stObj point to a texture which can contain
1749 * all active images, favour firstImage. Note that because of the
1750 * completeness requirement, we know that the image dimensions
1753 if (firstImage
->pt
&&
1754 firstImage
->pt
!= stObj
->pt
&&
1755 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1756 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1757 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1760 /* Find gallium format for the Mesa texture */
1761 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1763 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1765 GLuint width
, height
, depth
;
1766 if (!guess_base_level_size(stObj
->base
.Target
,
1767 firstImage
->base
.Width2
,
1768 firstImage
->base
.Height2
,
1769 firstImage
->base
.Depth2
,
1770 stObj
->base
.BaseLevel
,
1771 &width
, &height
, &depth
)) {
1772 width
= stObj
->width0
;
1773 height
= stObj
->height0
;
1774 depth
= stObj
->depth0
;
1776 /* convert GL dims to Gallium dims */
1777 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1778 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1781 /* If we already have a gallium texture, check that it matches the texture
1782 * object's format, target, size, num_levels, etc.
1785 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1786 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1787 stObj
->pt
->last_level
< stObj
->lastLevel
||
1788 stObj
->pt
->width0
!= ptWidth
||
1789 stObj
->pt
->height0
!= ptHeight
||
1790 stObj
->pt
->depth0
!= ptDepth
||
1791 stObj
->pt
->array_size
!= ptLayers
)
1793 /* The gallium texture does not match the Mesa texture so delete the
1794 * gallium texture now. We'll make a new one below.
1796 pipe_resource_reference(&stObj
->pt
, NULL
);
1797 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1798 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1802 /* May need to create a new gallium texture:
1805 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1807 stObj
->pt
= st_texture_create(st
,
1808 gl_target_to_pipe(stObj
->base
.Target
),
1818 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1823 /* Pull in any images not in the object's texture:
1825 for (face
= 0; face
< nr_faces
; face
++) {
1827 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1828 struct st_texture_image
*stImage
=
1829 st_texture_image(stObj
->base
.Image
[face
][level
]);
1831 /* Need to import images in main memory or held in other textures.
1833 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1834 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1844 * Returns pointer to a default/dummy texture.
1845 * This is typically used when the current shader has tex/sample instructions
1846 * but the user has not provided a (any) texture(s).
1848 struct gl_texture_object
*
1849 st_get_default_texture(struct st_context
*st
)
1851 if (!st
->default_texture
) {
1852 static const GLenum target
= GL_TEXTURE_2D
;
1853 GLubyte pixels
[16][16][4];
1854 struct gl_texture_object
*texObj
;
1855 struct gl_texture_image
*texImg
;
1858 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1859 * when attempting to sample incomplete textures.
1861 for (i
= 0; i
< 16; i
++) {
1862 for (j
= 0; j
< 16; j
++) {
1863 pixels
[i
][j
][0] = 0;
1864 pixels
[i
][j
][1] = 0;
1865 pixels
[i
][j
][2] = 0;
1866 pixels
[i
][j
][3] = 255;
1870 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1872 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1874 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1875 16, 16, 1, 0, /* w, h, d, border */
1876 GL_RGBA
, MESA_FORMAT_RGBA8888
);
1878 st_TexImage(st
->ctx
, 2, target
,
1879 0, GL_RGBA
, /* level, intformat */
1880 16, 16, 1, 0, /* w, h, d, border */
1881 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1882 &st
->ctx
->DefaultPacking
,
1886 texObj
->MinFilter
= GL_NEAREST
;
1887 texObj
->MagFilter
= GL_NEAREST
;
1888 texObj
->_Complete
= GL_TRUE
;
1890 st
->default_texture
= texObj
;
1892 return st
->default_texture
;
1897 st_init_texture_functions(struct dd_function_table
*functions
)
1899 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1900 functions
->TexImage1D
= st_TexImage1D
;
1901 functions
->TexImage2D
= st_TexImage2D
;
1902 functions
->TexImage3D
= st_TexImage3D
;
1903 functions
->TexSubImage1D
= st_TexSubImage1D
;
1904 functions
->TexSubImage2D
= st_TexSubImage2D
;
1905 functions
->TexSubImage3D
= st_TexSubImage3D
;
1906 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1907 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1908 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1909 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1910 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1911 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1912 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1913 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1914 functions
->GenerateMipmap
= st_generate_mipmap
;
1916 functions
->GetTexImage
= st_GetTexImage
;
1918 /* compressed texture functions */
1919 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1920 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1922 functions
->NewTextureObject
= st_NewTextureObject
;
1923 functions
->NewTextureImage
= st_NewTextureImage
;
1924 functions
->DeleteTexture
= st_DeleteTextureObject
;
1925 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1927 functions
->TextureMemCpy
= do_memcpy
;
1929 /* XXX Temporary until we can query pipe's texture sizes */
1930 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;