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"
38 #include "main/pixeltransfer.h"
39 #include "main/texcompress.h"
40 #include "main/texfetch.h"
41 #include "main/texgetimage.h"
42 #include "main/teximage.h"
43 #include "main/texobj.h"
44 #include "main/texstore.h"
46 #include "state_tracker/st_debug.h"
47 #include "state_tracker/st_context.h"
48 #include "state_tracker/st_cb_fbo.h"
49 #include "state_tracker/st_cb_flush.h"
50 #include "state_tracker/st_cb_texture.h"
51 #include "state_tracker/st_format.h"
52 #include "state_tracker/st_texture.h"
53 #include "state_tracker/st_gen_mipmap.h"
54 #include "state_tracker/st_atom.h"
56 #include "pipe/p_context.h"
57 #include "pipe/p_defines.h"
58 #include "util/u_inlines.h"
59 #include "pipe/p_shader_tokens.h"
60 #include "util/u_tile.h"
61 #include "util/u_blit.h"
62 #include "util/u_format.h"
63 #include "util/u_surface.h"
64 #include "util/u_sampler.h"
65 #include "util/u_math.h"
66 #include "util/u_box.h"
68 #define DBG if (0) printf
71 static enum pipe_texture_target
72 gl_target_to_pipe(GLenum target
)
76 return PIPE_TEXTURE_1D
;
78 return PIPE_TEXTURE_2D
;
79 case GL_TEXTURE_RECTANGLE_NV
:
80 return PIPE_TEXTURE_RECT
;
82 return PIPE_TEXTURE_3D
;
83 case GL_TEXTURE_CUBE_MAP_ARB
:
84 return PIPE_TEXTURE_CUBE
;
85 case GL_TEXTURE_1D_ARRAY_EXT
:
86 return PIPE_TEXTURE_1D_ARRAY
;
87 case GL_TEXTURE_2D_ARRAY_EXT
:
88 return PIPE_TEXTURE_2D_ARRAY
;
96 /** called via ctx->Driver.NewTextureImage() */
97 static struct gl_texture_image
*
98 st_NewTextureImage(struct gl_context
* ctx
)
100 DBG("%s\n", __FUNCTION__
);
102 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
106 /** called via ctx->Driver.NewTextureObject() */
107 static struct gl_texture_object
*
108 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
110 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
112 DBG("%s\n", __FUNCTION__
);
113 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
118 /** called via ctx->Driver.DeleteTextureObject() */
120 st_DeleteTextureObject(struct gl_context
*ctx
,
121 struct gl_texture_object
*texObj
)
123 struct st_context
*st
= st_context(ctx
);
124 struct st_texture_object
*stObj
= st_texture_object(texObj
);
126 pipe_resource_reference(&stObj
->pt
, NULL
);
127 if (stObj
->sampler_view
) {
128 if (stObj
->sampler_view
->context
!= st
->pipe
) {
129 /* Take "ownership" of this texture sampler view by setting
130 * its context pointer to this context. This avoids potential
131 * crashes when the texture object is shared among contexts
132 * and the original/owner context has already been destroyed.
134 stObj
->sampler_view
->context
= st
->pipe
;
136 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
138 _mesa_delete_texture_object(ctx
, texObj
);
142 /** called via ctx->Driver.FreeTexImageData() */
144 st_FreeTextureImageData(struct gl_context
* ctx
, struct gl_texture_image
*texImage
)
146 struct st_texture_image
*stImage
= st_texture_image(texImage
);
148 DBG("%s\n", __FUNCTION__
);
151 pipe_resource_reference(&stImage
->pt
, NULL
);
154 if (texImage
->Data
) {
155 _mesa_align_free(texImage
->Data
);
156 texImage
->Data
= NULL
;
162 * From linux kernel i386 header files, copes with odd sizes better
163 * than COPY_DWORDS would:
164 * XXX Put this in src/mesa/main/imports.h ???
166 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
168 __memcpy(void *to
, const void *from
, size_t n
)
171 __asm__
__volatile__("rep ; movsl\n\t"
175 "1:\ttestb $1,%b4\n\t"
177 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
178 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
183 #define __memcpy(a,b,c) memcpy(a,b,c)
188 * The system memcpy (at least on ubuntu 5.10) has problems copying
189 * to agp (writecombined) memory from a source which isn't 64-byte
190 * aligned - there is a 4x performance falloff.
192 * The x86 __memcpy is immune to this but is slightly slower
193 * (10%-ish) than the system memcpy.
195 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
196 * isn't much faster than x86_memcpy for agp copies.
198 * TODO: switch dynamically.
201 do_memcpy(void *dest
, const void *src
, size_t n
)
203 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
204 return __memcpy(dest
, src
, n
);
207 return memcpy(dest
, src
, n
);
212 * Return default texture resource binding bitmask for the given format.
215 default_bindings(struct st_context
*st
, enum pipe_format format
)
217 struct pipe_screen
*screen
= st
->pipe
->screen
;
218 const unsigned target
= PIPE_TEXTURE_2D
;
219 const unsigned geom
= 0x0;
222 if (util_format_is_depth_or_stencil(format
))
223 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
225 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
227 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
, geom
))
230 return PIPE_BIND_SAMPLER_VIEW
;
234 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
236 get_texture_dims(GLenum target
)
240 case GL_TEXTURE_1D_ARRAY_EXT
:
243 case GL_TEXTURE_CUBE_MAP_ARB
:
244 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
245 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
246 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
247 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
248 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
249 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
250 case GL_TEXTURE_RECTANGLE_NV
:
251 case GL_TEXTURE_2D_ARRAY_EXT
:
256 assert(0 && "invalid texture target in get_texture_dims()");
263 * Given the size of a mipmap image, try to compute the size of the level=0
266 * Note that this isn't always accurate for odd-sized, non-POW textures.
267 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
269 * \return GL_TRUE for success, GL_FALSE for failure
272 guess_base_level_size(GLenum target
,
273 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
274 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
276 const GLuint dims
= get_texture_dims(target
);
283 /* Depending on the image's size, we can't always make a guess here */
284 if ((dims
>= 1 && width
== 1) ||
285 (dims
>= 2 && height
== 1) ||
286 (dims
>= 3 && depth
== 1)) {
287 /* we can't determine the image size at level=0 */
291 /* grow the image size until we hit level = 0 */
312 * Try to allocate a pipe_resource object for the given st_texture_object.
314 * We use the given st_texture_image as a clue to determine the size of the
315 * mipmap image at level=0.
317 * \return GL_TRUE for success, GL_FALSE if out of memory.
320 guess_and_alloc_texture(struct st_context
*st
,
321 struct st_texture_object
*stObj
,
322 const struct st_texture_image
*stImage
)
324 GLuint lastLevel
, width
, height
, depth
;
326 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
327 enum pipe_format fmt
;
329 DBG("%s\n", __FUNCTION__
);
333 if (!guess_base_level_size(stObj
->base
.Target
,
334 stImage
->base
.Width2
,
335 stImage
->base
.Height2
,
336 stImage
->base
.Depth2
,
338 &width
, &height
, &depth
)) {
339 /* we can't determine the image size at level=0 */
340 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
341 /* this is not an out of memory error */
345 /* At this point, (width x height x depth) is the expected size of
346 * the level=0 mipmap image.
349 /* Guess a reasonable value for lastLevel. With OpenGL we have no
350 * idea how many mipmap levels will be in a texture until we start
351 * to render with it. Make an educated guess here but be prepared
352 * to re-allocating a texture buffer with space for more (or fewer)
353 * mipmap levels later.
355 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
356 stObj
->base
.MinFilter
== GL_LINEAR
||
357 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
358 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
359 !stObj
->base
.GenerateMipmap
&&
360 stImage
->level
== 0) {
361 /* only alloc space for a single mipmap level */
365 /* alloc space for a full mipmap */
366 GLuint l2width
= util_logbase2(width
);
367 GLuint l2height
= util_logbase2(height
);
368 GLuint l2depth
= util_logbase2(depth
);
369 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
372 /* Save the level=0 dimensions */
373 stObj
->width0
= width
;
374 stObj
->height0
= height
;
375 stObj
->depth0
= depth
;
377 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
379 bindings
= default_bindings(st
, fmt
);
381 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
382 width
, height
, depth
,
383 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
385 stObj
->pt
= st_texture_create(st
,
386 gl_target_to_pipe(stObj
->base
.Target
),
395 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
397 return stObj
->pt
!= NULL
;
402 * Adjust pixel unpack params and image dimensions to strip off the
404 * Gallium doesn't support texture borders. They've seldem been used
405 * and seldom been implemented correctly anyway.
406 * \param unpackNew returns the new pixel unpack parameters
409 strip_texture_border(GLint border
,
410 GLint
*width
, GLint
*height
, GLint
*depth
,
411 const struct gl_pixelstore_attrib
*unpack
,
412 struct gl_pixelstore_attrib
*unpackNew
)
414 assert(border
> 0); /* sanity check */
416 *unpackNew
= *unpack
;
418 if (unpackNew
->RowLength
== 0)
419 unpackNew
->RowLength
= *width
;
421 if (depth
&& unpackNew
->ImageHeight
== 0)
422 unpackNew
->ImageHeight
= *height
;
424 unpackNew
->SkipPixels
+= border
;
426 unpackNew
->SkipRows
+= border
;
428 unpackNew
->SkipImages
+= border
;
431 *width
= *width
- 2 * border
;
432 if (height
&& *height
>= 3)
433 *height
= *height
- 2 * border
;
434 if (depth
&& *depth
>= 3)
435 *depth
= *depth
- 2 * border
;
440 * Do glTexImage1/2/3D().
443 st_TexImage(struct gl_context
* ctx
,
445 GLenum target
, GLint level
,
446 GLint internalFormat
,
447 GLint width
, GLint height
, GLint depth
,
449 GLenum format
, GLenum type
, const void *pixels
,
450 const struct gl_pixelstore_attrib
*unpack
,
451 struct gl_texture_object
*texObj
,
452 struct gl_texture_image
*texImage
,
453 GLsizei imageSize
, GLboolean compressed_src
)
455 struct st_context
*st
= st_context(ctx
);
456 struct st_texture_object
*stObj
= st_texture_object(texObj
);
457 struct st_texture_image
*stImage
= st_texture_image(texImage
);
458 GLuint dstRowStride
= 0;
459 struct gl_pixelstore_attrib unpackNB
;
460 enum pipe_transfer_usage transfer_usage
= 0;
462 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
463 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
465 /* switch to "normal" */
466 if (stObj
->surface_based
) {
469 _mesa_clear_texture_object(ctx
, texObj
);
470 pipe_resource_reference(&stObj
->pt
, NULL
);
472 /* oops, need to init this image again */
473 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
474 internalFormat
, format
, type
);
476 _mesa_init_teximage_fields(ctx
, target
, texImage
,
477 width
, height
, depth
, border
,
478 internalFormat
, texFormat
);
480 stObj
->surface_based
= GL_FALSE
;
483 /* gallium does not support texture borders, strip it off */
485 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
487 texImage
->Width
= width
;
488 texImage
->Height
= height
;
489 texImage
->Depth
= depth
;
490 texImage
->Border
= 0;
494 assert(texImage
->Width
== width
);
495 assert(texImage
->Height
== height
);
496 assert(texImage
->Depth
== depth
);
499 stImage
->face
= _mesa_tex_target_to_face(target
);
500 stImage
->level
= level
;
502 _mesa_set_fetch_functions(texImage
, dims
);
504 /* Release the reference to a potentially orphaned buffer.
505 * Release any old malloced memory.
508 pipe_resource_reference(&stImage
->pt
, NULL
);
509 assert(!texImage
->Data
);
511 else if (texImage
->Data
) {
512 _mesa_align_free(texImage
->Data
);
516 * See if the new image is somehow incompatible with the existing
517 * mipmap. If so, free the old mipmap.
520 if (level
> (GLint
) stObj
->pt
->last_level
||
521 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
522 stImage
->face
, stImage
->level
)) {
524 pipe_resource_reference(&stObj
->pt
, NULL
);
526 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
530 if (width
== 0 || height
== 0 || depth
== 0) {
531 /* stop after freeing old image */
536 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
537 /* Probably out of memory.
538 * Try flushing any pending rendering, then retry.
541 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
542 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
548 assert(!stImage
->pt
);
550 /* Check if this texture image can live inside the texture object's buffer.
551 * If so, store the image there. Otherwise the image will temporarily live
555 st_texture_match_image(stObj
->pt
, &stImage
->base
,
556 stImage
->face
, stImage
->level
)) {
558 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
563 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
565 /* Pixel data may come from regular user memory or a PBO. For the later,
566 * do bounds checking and map the PBO to read pixels data from it.
568 * XXX we should try to use a GPU-accelerated path to copy the image data
569 * from the PBO to the texture.
571 if (compressed_src
) {
572 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
574 "glCompressedTexImage");
577 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
579 pixels
, unpack
, "glTexImage");
582 /* for a 1D array upload the image as a series of layer with height = 1 */
583 if (target
== GL_TEXTURE_1D_ARRAY
) {
589 * Prepare to store the texture data. Either map the gallium texture buffer
590 * memory or malloc space for it.
593 /* Store the image in the gallium texture memory buffer */
594 if (format
== GL_DEPTH_COMPONENT
&&
595 util_format_is_depth_and_stencil(stImage
->pt
->format
))
596 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
598 transfer_usage
= PIPE_TRANSFER_WRITE
;
600 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
601 transfer_usage
, 0, 0, width
, height
);
602 if(stImage
->transfer
)
603 dstRowStride
= stImage
->transfer
->stride
;
606 /* Allocate regular memory and store the image there temporarily. */
607 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
608 width
, height
, depth
);
609 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
611 texImage
->Data
= _mesa_align_malloc(imageSize
, 16);
614 if (!texImage
->Data
) {
615 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
620 /* We've allocated texture memory, but have no pixel data - all done. */
624 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
625 width
, height
, depth
, width
, dstRowStride
);
627 /* Copy user texture image into the texture buffer.
629 if (compressed_src
) {
630 const GLuint srcRowStride
=
631 _mesa_format_row_stride(texImage
->TexFormat
, width
);
632 if (dstRowStride
== srcRowStride
) {
633 memcpy(texImage
->Data
, pixels
, imageSize
);
636 char *dst
= texImage
->Data
;
637 const char *src
= pixels
;
638 GLuint i
, bw
, bh
, lines
;
639 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
640 lines
= (height
+ bh
- 1) / bh
;
642 for (i
= 0; i
< lines
; ++i
) {
643 memcpy(dst
, src
, srcRowStride
);
650 const GLuint srcImageStride
=
651 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
653 const GLubyte
*src
= (const GLubyte
*) pixels
;
655 for (i
= 0; i
< depth
; i
++) {
656 if (!_mesa_texstore(ctx
, dims
,
657 texImage
->_BaseFormat
,
660 0, 0, 0, /* dstX/Y/Zoffset */
662 texImage
->ImageOffsets
,
664 format
, type
, src
, unpack
)) {
665 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
668 if (stImage
->pt
&& i
+ 1 < depth
) {
669 /* unmap this slice */
670 st_texture_image_unmap(st
, stImage
);
671 /* map next slice of 3D texture */
672 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
673 transfer_usage
, 0, 0,
675 src
+= srcImageStride
;
681 _mesa_unmap_teximage_pbo(ctx
, unpack
);
683 if (stImage
->pt
&& texImage
->Data
) {
684 st_texture_image_unmap(st
, stImage
);
685 texImage
->Data
= NULL
;
691 st_TexImage3D(struct gl_context
* ctx
,
692 GLenum target
, GLint level
,
693 GLint internalFormat
,
694 GLint width
, GLint height
, GLint depth
,
696 GLenum format
, GLenum type
, const void *pixels
,
697 const struct gl_pixelstore_attrib
*unpack
,
698 struct gl_texture_object
*texObj
,
699 struct gl_texture_image
*texImage
)
701 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
702 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
708 st_TexImage2D(struct gl_context
* ctx
,
709 GLenum target
, GLint level
,
710 GLint internalFormat
,
711 GLint width
, GLint height
, GLint border
,
712 GLenum format
, GLenum type
, const void *pixels
,
713 const struct gl_pixelstore_attrib
*unpack
,
714 struct gl_texture_object
*texObj
,
715 struct gl_texture_image
*texImage
)
717 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
718 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
723 st_TexImage1D(struct gl_context
* ctx
,
724 GLenum target
, GLint level
,
725 GLint internalFormat
,
726 GLint width
, GLint border
,
727 GLenum format
, GLenum type
, const void *pixels
,
728 const struct gl_pixelstore_attrib
*unpack
,
729 struct gl_texture_object
*texObj
,
730 struct gl_texture_image
*texImage
)
732 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
733 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
738 st_CompressedTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
739 GLint internalFormat
,
740 GLint width
, GLint height
, GLint border
,
741 GLsizei imageSize
, const GLvoid
*data
,
742 struct gl_texture_object
*texObj
,
743 struct gl_texture_image
*texImage
)
745 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
746 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
752 * glGetTexImage() helper: decompress a compressed texture by rendering
753 * a textured quad. Store the results in the user's buffer.
756 decompress_with_blit(struct gl_context
* ctx
, GLenum target
, GLint level
,
757 GLenum format
, GLenum type
, GLvoid
*pixels
,
758 struct gl_texture_object
*texObj
,
759 struct gl_texture_image
*texImage
)
761 struct st_context
*st
= st_context(ctx
);
762 struct pipe_context
*pipe
= st
->pipe
;
763 struct st_texture_image
*stImage
= st_texture_image(texImage
);
764 struct st_texture_object
*stObj
= st_texture_object(texObj
);
765 struct pipe_sampler_view
*src_view
=
766 st_get_texture_sampler_view(stObj
, pipe
);
767 const GLuint width
= texImage
->Width
;
768 const GLuint height
= texImage
->Height
;
769 struct pipe_surface
*dst_surface
;
770 struct pipe_resource
*dst_texture
;
771 struct pipe_transfer
*tex_xfer
;
772 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
773 PIPE_BIND_TRANSFER_READ
);
775 /* create temp / dest surface */
776 if (!util_create_rgba_surface(pipe
, width
, height
, bind
,
777 &dst_texture
, &dst_surface
)) {
778 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
779 "in decompress_with_blit()");
783 /* blit/render/decompress */
784 util_blit_pixels_tex(st
->blit
,
785 src_view
, /* pipe_resource (src) */
786 0, 0, /* src x0, y0 */
787 width
, height
, /* src x1, y1 */
788 dst_surface
, /* pipe_surface (dst) */
789 0, 0, /* dst x0, y0 */
790 width
, height
, /* dst x1, y1 */
792 PIPE_TEX_MIPFILTER_NEAREST
);
794 /* map the dst_surface so we can read from it */
795 tex_xfer
= pipe_get_transfer(st_context(ctx
)->pipe
,
798 0, 0, width
, height
);
800 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
802 /* copy/pack data into user buffer */
803 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
805 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
806 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
808 for (row
= 0; row
< height
; row
++) {
809 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
810 height
, format
, type
, row
, 0);
811 memcpy(dest
, map
, bytesPerRow
);
812 map
+= tex_xfer
->stride
;
814 pipe_transfer_unmap(pipe
, tex_xfer
);
817 /* format translation via floats */
819 enum pipe_format format
= util_format_linear(dst_texture
->format
);
820 for (row
= 0; row
< height
; row
++) {
821 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
822 GLfloat rgba
[4 * MAX_WIDTH
];
823 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
824 height
, format
, type
, row
, 0);
826 if (ST_DEBUG
& DEBUG_FALLBACK
)
827 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
829 /* get float[4] rgba row from surface */
830 pipe_get_tile_rgba_format(pipe
, tex_xfer
, 0, row
, width
, 1,
833 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
834 type
, dest
, &ctx
->Pack
, transferOps
);
838 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
840 pipe
->transfer_destroy(pipe
, tex_xfer
);
842 /* destroy the temp / dest surface */
843 util_destroy_rgba_surface(dst_texture
, dst_surface
);
849 * Need to map texture image into memory before copying image data,
853 st_get_tex_image(struct gl_context
* ctx
, GLenum target
, GLint level
,
854 GLenum format
, GLenum type
, GLvoid
* pixels
,
855 struct gl_texture_object
*texObj
,
856 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
858 struct st_context
*st
= st_context(ctx
);
859 struct st_texture_image
*stImage
= st_texture_image(texImage
);
860 const GLuint dstImageStride
=
861 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
867 util_format_is_s3tc(stImage
->pt
->format
) &&
869 /* Need to decompress the texture.
870 * We'll do this by rendering a textured quad.
871 * Note that we only expect RGBA formats (no Z/depth formats).
873 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
880 /* Image is stored in hardware format in a buffer managed by the
881 * kernel. Need to explicitly map and unmap it.
883 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
884 PIPE_TRANSFER_READ
, 0, 0,
886 stImage
->base
.Height
);
887 /* compute stride in texels from stride in bytes */
888 texImage
->RowStride
= stImage
->transfer
->stride
889 * util_format_get_blockwidth(stImage
->pt
->format
)
890 / util_format_get_blocksize(stImage
->pt
->format
);
893 /* Otherwise, the image should actually be stored in
894 * texImage->Data. This is pretty confusing for
895 * everybody, I'd much prefer to separate the two functions of
896 * texImage->Data - storage for texture images in main memory
897 * and access (ie mappings) of images. In other words, we'd
898 * create a new texImage->Map field and leave Data simply for
901 assert(texImage
->Data
);
904 depth
= texImage
->Depth
;
907 dest
= (GLubyte
*) pixels
;
909 _mesa_set_fetch_functions(texImage
, get_texture_dims(target
));
911 for (i
= 0; i
< depth
; i
++) {
912 if (compressed_dst
) {
913 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
917 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
921 if (stImage
->pt
&& i
+ 1 < depth
) {
922 /* unmap this slice */
923 st_texture_image_unmap(st
, stImage
);
924 /* map next slice of 3D texture */
925 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
926 PIPE_TRANSFER_READ
, 0, 0,
928 stImage
->base
.Height
);
929 dest
+= dstImageStride
;
933 texImage
->Depth
= depth
;
937 st_texture_image_unmap(st
, stImage
);
938 texImage
->Data
= NULL
;
944 st_GetTexImage(struct gl_context
* ctx
, GLenum target
, GLint level
,
945 GLenum format
, GLenum type
, GLvoid
* pixels
,
946 struct gl_texture_object
*texObj
,
947 struct gl_texture_image
*texImage
)
949 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
955 st_GetCompressedTexImage(struct gl_context
*ctx
, GLenum target
, GLint level
,
957 struct gl_texture_object
*texObj
,
958 struct gl_texture_image
*texImage
)
960 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
967 st_TexSubimage(struct gl_context
*ctx
, GLint dims
, GLenum target
, GLint level
,
968 GLint xoffset
, GLint yoffset
, GLint zoffset
,
969 GLint width
, GLint height
, GLint depth
,
970 GLenum format
, GLenum type
, const void *pixels
,
971 const struct gl_pixelstore_attrib
*packing
,
972 struct gl_texture_object
*texObj
,
973 struct gl_texture_image
*texImage
)
975 struct st_context
*st
= st_context(ctx
);
976 struct st_texture_image
*stImage
= st_texture_image(texImage
);
978 const GLuint srcImageStride
=
979 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
982 /* init to silence warning only: */
983 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
985 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
986 _mesa_lookup_enum_by_nr(target
),
987 level
, xoffset
, yoffset
, width
, height
);
990 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
991 type
, pixels
, packing
, "glTexSubImage2D");
995 /* for a 1D array upload the image as a series of layer with height = 1 */
996 if (target
== GL_TEXTURE_1D_ARRAY
) {
1001 /* Map buffer if necessary. Need to lock to prevent other contexts
1002 * from uploading the buffer under us.
1005 if (format
== GL_DEPTH_COMPONENT
&&
1006 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1007 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1009 transfer_usage
= PIPE_TRANSFER_WRITE
;
1011 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1017 if (!texImage
->Data
) {
1018 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1022 src
= (const GLubyte
*) pixels
;
1023 dstRowStride
= stImage
->transfer
->stride
;
1025 for (i
= 0; i
< depth
; i
++) {
1026 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1027 texImage
->TexFormat
,
1031 texImage
->ImageOffsets
,
1033 format
, type
, src
, packing
)) {
1034 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1037 if (stImage
->pt
&& i
+ 1 < depth
) {
1038 /* unmap this slice */
1039 st_texture_image_unmap(st
, stImage
);
1040 /* map next slice of 3D texture */
1041 texImage
->Data
= st_texture_image_map(st
, stImage
,
1046 src
+= srcImageStride
;
1051 _mesa_unmap_teximage_pbo(ctx
, packing
);
1053 if (stImage
->pt
&& texImage
->Data
) {
1054 st_texture_image_unmap(st
, stImage
);
1055 texImage
->Data
= NULL
;
1062 st_TexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1063 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1064 GLsizei width
, GLsizei height
, GLsizei depth
,
1065 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1066 const struct gl_pixelstore_attrib
*packing
,
1067 struct gl_texture_object
*texObj
,
1068 struct gl_texture_image
*texImage
)
1070 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1071 width
, height
, depth
, format
, type
,
1072 pixels
, packing
, texObj
, texImage
);
1077 st_TexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1078 GLint xoffset
, GLint yoffset
,
1079 GLsizei width
, GLsizei height
,
1080 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1081 const struct gl_pixelstore_attrib
*packing
,
1082 struct gl_texture_object
*texObj
,
1083 struct gl_texture_image
*texImage
)
1085 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1086 width
, height
, 1, format
, type
,
1087 pixels
, packing
, texObj
, texImage
);
1092 st_TexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1093 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1094 const GLvoid
* pixels
,
1095 const struct gl_pixelstore_attrib
*packing
,
1096 struct gl_texture_object
*texObj
,
1097 struct gl_texture_image
*texImage
)
1099 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1100 format
, type
, pixels
, packing
, texObj
, texImage
);
1105 st_CompressedTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1106 GLint xoffset
, GLsizei width
,
1108 GLsizei imageSize
, const GLvoid
*data
,
1109 struct gl_texture_object
*texObj
,
1110 struct gl_texture_image
*texImage
)
1117 st_CompressedTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1118 GLint xoffset
, GLint yoffset
,
1119 GLsizei width
, GLint height
,
1121 GLsizei imageSize
, const GLvoid
*data
,
1122 struct gl_texture_object
*texObj
,
1123 struct gl_texture_image
*texImage
)
1125 struct st_context
*st
= st_context(ctx
);
1126 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1130 enum pipe_format pformat
;
1133 pformat
= stImage
->pt
->format
;
1135 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1136 PIPE_TRANSFER_WRITE
,
1140 srcBlockStride
= util_format_get_stride(pformat
, width
);
1141 dstBlockStride
= stImage
->transfer
->stride
;
1143 assert(stImage
->pt
);
1144 /* TODO find good values for block and strides */
1145 /* TODO also adjust texImage->data for yoffset/xoffset */
1149 if (!texImage
->Data
) {
1150 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1154 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1155 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1157 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1158 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1159 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1160 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1161 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1165 st_texture_image_unmap(st
, stImage
);
1166 texImage
->Data
= NULL
;
1172 st_CompressedTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1173 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1174 GLsizei width
, GLint height
, GLint depth
,
1176 GLsizei imageSize
, const GLvoid
*data
,
1177 struct gl_texture_object
*texObj
,
1178 struct gl_texture_image
*texImage
)
1186 * Do a CopyTexSubImage operation using a read transfer from the source,
1187 * a write transfer to the destination and get_tile()/put_tile() to access
1188 * the pixels/texels.
1190 * Note: srcY=0=TOP of renderbuffer
1193 fallback_copy_texsubimage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1194 struct st_renderbuffer
*strb
,
1195 struct st_texture_image
*stImage
,
1197 GLint destX
, GLint destY
, GLint destZ
,
1198 GLint srcX
, GLint srcY
,
1199 GLsizei width
, GLsizei height
)
1201 struct st_context
*st
= st_context(ctx
);
1202 struct pipe_context
*pipe
= st
->pipe
;
1203 struct pipe_transfer
*src_trans
;
1205 enum pipe_transfer_usage transfer_usage
;
1207 if (ST_DEBUG
& DEBUG_FALLBACK
)
1208 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1210 assert(width
<= MAX_WIDTH
);
1212 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1213 srcY
= strb
->Base
.Height
- srcY
- height
;
1216 src_trans
= pipe_get_transfer(st_context(ctx
)->pipe
,
1223 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1224 baseFormat
== GL_DEPTH_STENCIL
) &&
1225 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1226 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1228 transfer_usage
= PIPE_TRANSFER_WRITE
;
1230 /* XXX this used to ignore destZ param */
1231 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
1232 destX
, destY
, width
, height
);
1234 if (baseFormat
== GL_DEPTH_COMPONENT
||
1235 baseFormat
== GL_DEPTH_STENCIL
) {
1236 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1237 ctx
->Pixel
.DepthBias
!= 0.0F
);
1240 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1241 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1250 /* To avoid a large temp memory allocation, do copy row by row */
1251 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1252 uint data
[MAX_WIDTH
];
1253 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1255 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1257 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1263 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1265 if (tempSrc
&& texDest
) {
1266 const GLint dims
= 2;
1267 const GLint dstRowStride
= stImage
->transfer
->stride
;
1268 struct gl_texture_image
*texImage
= &stImage
->base
;
1269 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1271 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1272 unpack
.Invert
= GL_TRUE
;
1275 /* get float/RGBA image from framebuffer */
1276 /* XXX this usually involves a lot of int/float conversion.
1277 * try to avoid that someday.
1279 pipe_get_tile_rgba_format(pipe
, src_trans
, 0, 0, width
, height
,
1280 util_format_linear(strb
->texture
->format
),
1283 /* Store into texture memory.
1284 * Note that this does some special things such as pixel transfer
1285 * ops and format conversion. In particular, if the dest tex format
1286 * is actually RGBA but the user created the texture as GL_RGB we
1287 * need to fill-in/override the alpha channel with 1.0.
1289 _mesa_texstore(ctx
, dims
,
1290 texImage
->_BaseFormat
,
1291 texImage
->TexFormat
,
1295 texImage
->ImageOffsets
,
1297 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1301 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1308 st_texture_image_unmap(st
, stImage
);
1309 pipe
->transfer_destroy(pipe
, src_trans
);
1315 * If the format of the src renderbuffer and the format of the dest
1316 * texture are compatible (in terms of blitting), return a TGSI writemask
1317 * to be used during the blit.
1318 * If the src/dest are incompatible, return 0.
1321 compatible_src_dst_formats(struct gl_context
*ctx
,
1322 const struct gl_renderbuffer
*src
,
1323 const struct gl_texture_image
*dst
)
1325 /* Get logical base formats for the src and dest.
1326 * That is, use the user-requested formats and not the actual, device-
1328 * For example, the user may have requested an A8 texture but the
1329 * driver may actually be using an RGBA texture format. When we
1330 * copy/blit to that texture, we only want to copy the Alpha channel
1331 * and not the RGB channels.
1333 * Similarly, when the src FBO was created an RGB format may have been
1334 * requested but the driver actually chose an RGBA format. In that case,
1335 * we don't want to copy the undefined Alpha channel to the dest texture
1336 * (it should be 1.0).
1338 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1339 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1342 * XXX when we have red-only and red/green renderbuffers we'll need
1343 * to add more cases here (or implement a general-purpose routine that
1344 * queries the existance of the R,G,B,A channels in the src and dest).
1346 if (srcFormat
== dstFormat
) {
1347 /* This is the same as matching_base_formats, which should
1348 * always pass, as it did previously.
1350 return TGSI_WRITEMASK_XYZW
;
1352 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1353 /* Make sure that A in the dest is 1. The actual src format
1354 * may be RGBA and have undefined A values.
1356 return TGSI_WRITEMASK_XYZ
;
1358 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1359 /* Make sure that A in the dest is 1. The actual dst format
1360 * may be RGBA and will need A=1 to provide proper alpha values
1361 * when sampled later.
1363 return TGSI_WRITEMASK_XYZ
;
1366 if (ST_DEBUG
& DEBUG_FALLBACK
)
1367 debug_printf("%s failed for src %s, dst %s\n",
1369 _mesa_lookup_enum_by_nr(srcFormat
),
1370 _mesa_lookup_enum_by_nr(dstFormat
));
1381 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1382 * Note that the region to copy has already been clipped so we know we
1383 * won't read from outside the source renderbuffer's bounds.
1385 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1388 st_copy_texsubimage(struct gl_context
*ctx
,
1389 GLenum target
, GLint level
,
1390 GLint destX
, GLint destY
, GLint destZ
,
1391 GLint srcX
, GLint srcY
,
1392 GLsizei width
, GLsizei height
)
1394 struct gl_texture_unit
*texUnit
=
1395 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1396 struct gl_texture_object
*texObj
=
1397 _mesa_select_tex_object(ctx
, texUnit
, target
);
1398 struct gl_texture_image
*texImage
=
1399 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1400 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1401 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1402 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1403 struct st_renderbuffer
*strb
;
1404 struct st_context
*st
= st_context(ctx
);
1405 struct pipe_context
*pipe
= st
->pipe
;
1406 struct pipe_screen
*screen
= pipe
->screen
;
1407 enum pipe_format dest_format
, src_format
;
1408 GLboolean use_fallback
= GL_TRUE
;
1409 GLboolean matching_base_formats
;
1410 GLuint format_writemask
, sample_count
;
1411 struct pipe_surface
*dest_surface
= NULL
;
1412 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1414 /* make sure finalize_textures has been called?
1416 if (0) st_validate_state(st
);
1418 /* determine if copying depth or color data */
1419 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1420 texBaseFormat
== GL_DEPTH_STENCIL
) {
1421 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1422 if (strb
->Base
.Wrapped
) {
1423 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
1427 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1428 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1431 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1432 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1436 sample_count
= strb
->surface
->texture
->nr_samples
;
1437 /* I believe this would be legal, presumably would need to do a resolve
1438 for color, and for depth/stencil spec says to just use one of the
1439 depth/stencil samples per pixel? Need some transfer clarifications. */
1440 assert(sample_count
< 2);
1466 if (width
< 0 || height
< 0)
1471 assert(strb
->surface
);
1472 assert(stImage
->pt
);
1474 src_format
= strb
->surface
->format
;
1475 dest_format
= stImage
->pt
->format
;
1478 * Determine if the src framebuffer and dest texture have the same
1479 * base format. We need this to detect a case such as the framebuffer
1480 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1481 * texture format stores RGBA we need to set A=1 (overriding the
1482 * framebuffer's alpha values). We can't do that with the blit or
1483 * textured-quad paths.
1485 matching_base_formats
=
1486 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1487 _mesa_get_format_base_format(texImage
->TexFormat
));
1488 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1490 if (ctx
->_ImageTransferState
== 0x0) {
1492 if (matching_base_formats
&&
1493 src_format
== dest_format
&&
1496 /* use surface_copy() / blit */
1497 struct pipe_box src_box
;
1498 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
1499 width
, height
, &src_box
);
1501 /* for resource_copy_region(), y=0=top, always */
1502 pipe
->resource_copy_region(pipe
,
1506 destX
, destY
, destZ
+ stImage
->face
,
1509 strb
->surface
->u
.tex
.level
,
1511 use_fallback
= GL_FALSE
;
1513 else if (format_writemask
&&
1514 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1515 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1516 screen
->is_format_supported(screen
, src_format
,
1517 PIPE_TEXTURE_2D
, sample_count
,
1518 PIPE_BIND_SAMPLER_VIEW
,
1520 screen
->is_format_supported(screen
, dest_format
,
1522 PIPE_BIND_RENDER_TARGET
,
1524 /* draw textured quad to do the copy */
1526 struct pipe_surface surf_tmpl
;
1527 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1528 surf_tmpl
.format
= stImage
->pt
->format
;
1529 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
1530 surf_tmpl
.u
.tex
.level
= stImage
->level
;
1531 surf_tmpl
.u
.tex
.first_layer
= stImage
->face
+ destZ
;
1532 surf_tmpl
.u
.tex
.last_layer
= stImage
->face
+ destZ
;
1534 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1538 srcY1
= strb
->Base
.Height
- srcY
- height
;
1539 srcY0
= srcY1
+ height
;
1543 srcY1
= srcY0
+ height
;
1546 util_blit_pixels_writemask(st
->blit
,
1548 strb
->surface
->u
.tex
.level
,
1550 srcX
+ width
, srcY1
,
1551 strb
->surface
->u
.tex
.first_layer
,
1554 destX
+ width
, destY
+ height
,
1555 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1557 use_fallback
= GL_FALSE
;
1561 pipe_surface_reference(&dest_surface
, NULL
);
1565 /* software fallback */
1566 fallback_copy_texsubimage(ctx
, target
, level
,
1567 strb
, stImage
, texBaseFormat
,
1568 destX
, destY
, destZ
,
1569 srcX
, srcY
, width
, height
);
1576 st_CopyTexImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1577 GLenum internalFormat
,
1578 GLint x
, GLint y
, GLsizei width
, GLint border
)
1580 struct gl_texture_unit
*texUnit
=
1581 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1582 struct gl_texture_object
*texObj
=
1583 _mesa_select_tex_object(ctx
, texUnit
, target
);
1584 struct gl_texture_image
*texImage
=
1585 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1587 /* Setup or redefine the texture object, texture and texture
1588 * image. Don't populate yet.
1590 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1592 GL_RGBA
, CHAN_TYPE
, NULL
,
1593 &ctx
->DefaultPacking
, texObj
, texImage
);
1595 st_copy_texsubimage(ctx
, target
, level
,
1596 0, 0, 0, /* destX,Y,Z */
1597 x
, y
, width
, 1); /* src X, Y, size */
1602 st_CopyTexImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1603 GLenum internalFormat
,
1604 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1607 struct gl_texture_unit
*texUnit
=
1608 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1609 struct gl_texture_object
*texObj
=
1610 _mesa_select_tex_object(ctx
, texUnit
, target
);
1611 struct gl_texture_image
*texImage
=
1612 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1614 /* Setup or redefine the texture object, texture and texture
1615 * image. Don't populate yet.
1617 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1618 width
, height
, border
,
1619 GL_RGBA
, CHAN_TYPE
, NULL
,
1620 &ctx
->DefaultPacking
, texObj
, texImage
);
1622 st_copy_texsubimage(ctx
, target
, level
,
1623 0, 0, 0, /* destX,Y,Z */
1624 x
, y
, width
, height
); /* src X, Y, size */
1629 st_CopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1630 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1632 const GLint yoffset
= 0, zoffset
= 0;
1633 const GLsizei height
= 1;
1634 st_copy_texsubimage(ctx
, target
, level
,
1635 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1636 x
, y
, width
, height
); /* src X, Y, size */
1641 st_CopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1642 GLint xoffset
, GLint yoffset
,
1643 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1645 const GLint zoffset
= 0;
1646 st_copy_texsubimage(ctx
, target
, level
,
1647 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1648 x
, y
, width
, height
); /* src X, Y, size */
1653 st_CopyTexSubImage3D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1654 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1655 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1657 st_copy_texsubimage(ctx
, target
, level
,
1658 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1659 x
, y
, width
, height
); /* src X, Y, size */
1664 * Copy image data from stImage into the texture object 'stObj' at level
1668 copy_image_data_to_texture(struct st_context
*st
,
1669 struct st_texture_object
*stObj
,
1671 struct st_texture_image
*stImage
)
1675 const struct gl_texture_image
*dstImage
=
1676 stObj
->base
.Image
[stImage
->face
][stImage
->level
];
1678 assert(dstImage
->Width
== stImage
->base
.Width
);
1679 assert(dstImage
->Height
== stImage
->base
.Height
);
1680 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1684 /* Copy potentially with the blitter:
1686 st_texture_image_copy(st
->pipe
,
1687 stObj
->pt
, dstLevel
, /* dest texture, level */
1688 stImage
->pt
, stImage
->level
, /* src texture, level */
1691 pipe_resource_reference(&stImage
->pt
, NULL
);
1693 else if (stImage
->base
.Data
) {
1694 st_texture_image_data(st
,
1699 stImage
->base
.RowStride
*
1700 util_format_get_blocksize(stObj
->pt
->format
),
1701 stImage
->base
.RowStride
*
1702 stImage
->base
.Height
*
1703 util_format_get_blocksize(stObj
->pt
->format
));
1704 _mesa_align_free(stImage
->base
.Data
);
1705 stImage
->base
.Data
= NULL
;
1708 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1713 * Called during state validation. When this function is finished,
1714 * the texture object should be ready for rendering.
1715 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1718 st_finalize_texture(struct gl_context
*ctx
,
1719 struct pipe_context
*pipe
,
1720 struct gl_texture_object
*tObj
)
1722 struct st_context
*st
= st_context(ctx
);
1723 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1724 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1726 struct st_texture_image
*firstImage
;
1727 enum pipe_format firstImageFormat
;
1728 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1730 if (stObj
->base
._Complete
) {
1731 /* The texture is complete and we know exactly how many mipmap levels
1732 * are present/needed. This is conditional because we may be called
1733 * from the st_generate_mipmap() function when the texture object is
1734 * incomplete. In that case, we'll have set stObj->lastLevel before
1737 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1738 stObj
->base
.MinFilter
== GL_NEAREST
)
1739 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1741 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1744 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1747 /* If both firstImage and stObj point to a texture which can contain
1748 * all active images, favour firstImage. Note that because of the
1749 * completeness requirement, we know that the image dimensions
1752 if (firstImage
->pt
&&
1753 firstImage
->pt
!= stObj
->pt
&&
1754 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1755 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1756 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1759 /* Find gallium format for the Mesa texture */
1760 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1762 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1764 GLuint width
, height
, depth
;
1765 if (!guess_base_level_size(stObj
->base
.Target
,
1766 firstImage
->base
.Width2
,
1767 firstImage
->base
.Height2
,
1768 firstImage
->base
.Depth2
,
1769 stObj
->base
.BaseLevel
,
1770 &width
, &height
, &depth
)) {
1771 width
= stObj
->width0
;
1772 height
= stObj
->height0
;
1773 depth
= stObj
->depth0
;
1775 /* convert GL dims to Gallium dims */
1776 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1777 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1780 /* If we already have a gallium texture, check that it matches the texture
1781 * object's format, target, size, num_levels, etc.
1784 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1785 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1786 stObj
->pt
->last_level
< stObj
->lastLevel
||
1787 stObj
->pt
->width0
!= ptWidth
||
1788 stObj
->pt
->height0
!= ptHeight
||
1789 stObj
->pt
->depth0
!= ptDepth
||
1790 stObj
->pt
->array_size
!= ptLayers
)
1792 /* The gallium texture does not match the Mesa texture so delete the
1793 * gallium texture now. We'll make a new one below.
1795 pipe_resource_reference(&stObj
->pt
, NULL
);
1796 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1797 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1801 /* May need to create a new gallium texture:
1804 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1806 stObj
->pt
= st_texture_create(st
,
1807 gl_target_to_pipe(stObj
->base
.Target
),
1817 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1822 /* Pull in any images not in the object's texture:
1824 for (face
= 0; face
< nr_faces
; face
++) {
1826 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1827 struct st_texture_image
*stImage
=
1828 st_texture_image(stObj
->base
.Image
[face
][level
]);
1830 /* Need to import images in main memory or held in other textures.
1832 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1833 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1843 * Returns pointer to a default/dummy texture.
1844 * This is typically used when the current shader has tex/sample instructions
1845 * but the user has not provided a (any) texture(s).
1847 struct gl_texture_object
*
1848 st_get_default_texture(struct st_context
*st
)
1850 if (!st
->default_texture
) {
1851 static const GLenum target
= GL_TEXTURE_2D
;
1852 GLubyte pixels
[16][16][4];
1853 struct gl_texture_object
*texObj
;
1854 struct gl_texture_image
*texImg
;
1857 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1858 * when attempting to sample incomplete textures.
1860 for (i
= 0; i
< 16; i
++) {
1861 for (j
= 0; j
< 16; j
++) {
1862 pixels
[i
][j
][0] = 0;
1863 pixels
[i
][j
][1] = 0;
1864 pixels
[i
][j
][2] = 0;
1865 pixels
[i
][j
][3] = 255;
1869 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1871 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1873 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1874 16, 16, 1, 0, /* w, h, d, border */
1875 GL_RGBA
, MESA_FORMAT_RGBA8888
);
1877 st_TexImage(st
->ctx
, 2, target
,
1878 0, GL_RGBA
, /* level, intformat */
1879 16, 16, 1, 0, /* w, h, d, border */
1880 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1881 &st
->ctx
->DefaultPacking
,
1885 texObj
->MinFilter
= GL_NEAREST
;
1886 texObj
->MagFilter
= GL_NEAREST
;
1887 texObj
->_Complete
= GL_TRUE
;
1889 st
->default_texture
= texObj
;
1891 return st
->default_texture
;
1896 st_init_texture_functions(struct dd_function_table
*functions
)
1898 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1899 functions
->TexImage1D
= st_TexImage1D
;
1900 functions
->TexImage2D
= st_TexImage2D
;
1901 functions
->TexImage3D
= st_TexImage3D
;
1902 functions
->TexSubImage1D
= st_TexSubImage1D
;
1903 functions
->TexSubImage2D
= st_TexSubImage2D
;
1904 functions
->TexSubImage3D
= st_TexSubImage3D
;
1905 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1906 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1907 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1908 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1909 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1910 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1911 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1912 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1913 functions
->GenerateMipmap
= st_generate_mipmap
;
1915 functions
->GetTexImage
= st_GetTexImage
;
1917 /* compressed texture functions */
1918 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1919 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1921 functions
->NewTextureObject
= st_NewTextureObject
;
1922 functions
->NewTextureImage
= st_NewTextureImage
;
1923 functions
->DeleteTexture
= st_DeleteTextureObject
;
1924 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1926 functions
->TextureMemCpy
= do_memcpy
;
1928 /* XXX Temporary until we can query pipe's texture sizes */
1929 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;