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
;
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
))
231 format
= util_format_linear(format
);
233 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
236 return PIPE_BIND_SAMPLER_VIEW
;
241 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
243 get_texture_dims(GLenum target
)
247 case GL_TEXTURE_1D_ARRAY_EXT
:
250 case GL_TEXTURE_CUBE_MAP_ARB
:
251 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
252 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
253 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
254 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
255 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
256 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
257 case GL_TEXTURE_RECTANGLE_NV
:
258 case GL_TEXTURE_2D_ARRAY_EXT
:
263 assert(0 && "invalid texture target in get_texture_dims()");
270 * Given the size of a mipmap image, try to compute the size of the level=0
273 * Note that this isn't always accurate for odd-sized, non-POW textures.
274 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
276 * \return GL_TRUE for success, GL_FALSE for failure
279 guess_base_level_size(GLenum target
,
280 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
281 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
283 const GLuint dims
= get_texture_dims(target
);
290 /* Depending on the image's size, we can't always make a guess here */
291 if ((dims
>= 1 && width
== 1) ||
292 (dims
>= 2 && height
== 1) ||
293 (dims
>= 3 && depth
== 1)) {
294 /* we can't determine the image size at level=0 */
298 /* grow the image size until we hit level = 0 */
319 * Try to allocate a pipe_resource object for the given st_texture_object.
321 * We use the given st_texture_image as a clue to determine the size of the
322 * mipmap image at level=0.
324 * \return GL_TRUE for success, GL_FALSE if out of memory.
327 guess_and_alloc_texture(struct st_context
*st
,
328 struct st_texture_object
*stObj
,
329 const struct st_texture_image
*stImage
)
331 GLuint lastLevel
, width
, height
, depth
;
333 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
334 enum pipe_format fmt
;
336 DBG("%s\n", __FUNCTION__
);
340 if (!guess_base_level_size(stObj
->base
.Target
,
341 stImage
->base
.Width2
,
342 stImage
->base
.Height2
,
343 stImage
->base
.Depth2
,
345 &width
, &height
, &depth
)) {
346 /* we can't determine the image size at level=0 */
347 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
348 /* this is not an out of memory error */
352 /* At this point, (width x height x depth) is the expected size of
353 * the level=0 mipmap image.
356 /* Guess a reasonable value for lastLevel. With OpenGL we have no
357 * idea how many mipmap levels will be in a texture until we start
358 * to render with it. Make an educated guess here but be prepared
359 * to re-allocating a texture buffer with space for more (or fewer)
360 * mipmap levels later.
362 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
363 stObj
->base
.MinFilter
== GL_LINEAR
||
364 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
365 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
366 !stObj
->base
.GenerateMipmap
&&
367 stImage
->level
== 0) {
368 /* only alloc space for a single mipmap level */
372 /* alloc space for a full mipmap */
373 GLuint l2width
= util_logbase2(width
);
374 GLuint l2height
= util_logbase2(height
);
375 GLuint l2depth
= util_logbase2(depth
);
376 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
379 /* Save the level=0 dimensions */
380 stObj
->width0
= width
;
381 stObj
->height0
= height
;
382 stObj
->depth0
= depth
;
384 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
386 bindings
= default_bindings(st
, fmt
);
388 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
389 width
, height
, depth
,
390 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
392 stObj
->pt
= st_texture_create(st
,
393 gl_target_to_pipe(stObj
->base
.Target
),
402 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
404 return stObj
->pt
!= NULL
;
409 * Adjust pixel unpack params and image dimensions to strip off the
411 * Gallium doesn't support texture borders. They've seldem been used
412 * and seldom been implemented correctly anyway.
413 * \param unpackNew returns the new pixel unpack parameters
416 strip_texture_border(GLint border
,
417 GLint
*width
, GLint
*height
, GLint
*depth
,
418 const struct gl_pixelstore_attrib
*unpack
,
419 struct gl_pixelstore_attrib
*unpackNew
)
421 assert(border
> 0); /* sanity check */
423 *unpackNew
= *unpack
;
425 if (unpackNew
->RowLength
== 0)
426 unpackNew
->RowLength
= *width
;
428 if (depth
&& unpackNew
->ImageHeight
== 0)
429 unpackNew
->ImageHeight
= *height
;
431 unpackNew
->SkipPixels
+= border
;
433 unpackNew
->SkipRows
+= border
;
435 unpackNew
->SkipImages
+= border
;
438 *width
= *width
- 2 * border
;
439 if (height
&& *height
>= 3)
440 *height
= *height
- 2 * border
;
441 if (depth
&& *depth
>= 3)
442 *depth
= *depth
- 2 * border
;
447 * Do glTexImage1/2/3D().
450 st_TexImage(struct gl_context
* ctx
,
452 GLenum target
, GLint level
,
453 GLint internalFormat
,
454 GLint width
, GLint height
, GLint depth
,
456 GLenum format
, GLenum type
, const void *pixels
,
457 const struct gl_pixelstore_attrib
*unpack
,
458 struct gl_texture_object
*texObj
,
459 struct gl_texture_image
*texImage
,
460 GLsizei imageSize
, GLboolean compressed_src
)
462 struct st_context
*st
= st_context(ctx
);
463 struct st_texture_object
*stObj
= st_texture_object(texObj
);
464 struct st_texture_image
*stImage
= st_texture_image(texImage
);
465 GLuint dstRowStride
= 0;
466 struct gl_pixelstore_attrib unpackNB
;
467 enum pipe_transfer_usage transfer_usage
= 0;
469 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
470 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
472 /* switch to "normal" */
473 if (stObj
->surface_based
) {
476 _mesa_clear_texture_object(ctx
, texObj
);
477 pipe_resource_reference(&stObj
->pt
, NULL
);
479 /* oops, need to init this image again */
480 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
481 internalFormat
, format
, type
);
483 _mesa_init_teximage_fields(ctx
, target
, texImage
,
484 width
, height
, depth
, border
,
485 internalFormat
, texFormat
);
487 stObj
->surface_based
= GL_FALSE
;
490 /* gallium does not support texture borders, strip it off */
492 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
494 texImage
->Width
= width
;
495 texImage
->Height
= height
;
496 texImage
->Depth
= depth
;
497 texImage
->Border
= 0;
501 assert(texImage
->Width
== width
);
502 assert(texImage
->Height
== height
);
503 assert(texImage
->Depth
== depth
);
506 stImage
->face
= _mesa_tex_target_to_face(target
);
507 stImage
->level
= level
;
509 _mesa_set_fetch_functions(texImage
, dims
);
511 /* Release the reference to a potentially orphaned buffer.
512 * Release any old malloced memory.
515 pipe_resource_reference(&stImage
->pt
, NULL
);
516 assert(!texImage
->Data
);
518 else if (texImage
->Data
) {
519 _mesa_align_free(texImage
->Data
);
523 * See if the new image is somehow incompatible with the existing
524 * mipmap. If so, free the old mipmap.
527 if (level
> (GLint
) stObj
->pt
->last_level
||
528 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
529 stImage
->face
, stImage
->level
)) {
531 pipe_resource_reference(&stObj
->pt
, NULL
);
533 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
537 if (width
== 0 || height
== 0 || depth
== 0) {
538 /* stop after freeing old image */
543 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
544 /* Probably out of memory.
545 * Try flushing any pending rendering, then retry.
548 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
549 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
555 assert(!stImage
->pt
);
557 /* Check if this texture image can live inside the texture object's buffer.
558 * If so, store the image there. Otherwise the image will temporarily live
562 st_texture_match_image(stObj
->pt
, &stImage
->base
,
563 stImage
->face
, stImage
->level
)) {
565 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
570 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
572 /* Pixel data may come from regular user memory or a PBO. For the later,
573 * do bounds checking and map the PBO to read pixels data from it.
575 * XXX we should try to use a GPU-accelerated path to copy the image data
576 * from the PBO to the texture.
578 if (compressed_src
) {
579 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
581 "glCompressedTexImage");
584 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
586 pixels
, unpack
, "glTexImage");
589 /* for a 1D array upload the image as a series of layer with height = 1 */
590 if (target
== GL_TEXTURE_1D_ARRAY
) {
596 * Prepare to store the texture data. Either map the gallium texture buffer
597 * memory or malloc space for it.
600 /* Store the image in the gallium texture memory buffer */
601 if (format
== GL_DEPTH_COMPONENT
&&
602 util_format_is_depth_and_stencil(stImage
->pt
->format
))
603 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
605 transfer_usage
= PIPE_TRANSFER_WRITE
;
607 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
608 transfer_usage
, 0, 0, width
, height
);
609 if(stImage
->transfer
)
610 dstRowStride
= stImage
->transfer
->stride
;
613 /* Allocate regular memory and store the image there temporarily. */
614 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
615 width
, height
, depth
);
616 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
618 texImage
->Data
= _mesa_align_malloc(imageSize
, 16);
621 if (!texImage
->Data
) {
622 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
627 /* We've allocated texture memory, but have no pixel data - all done. */
631 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
632 width
, height
, depth
, width
, dstRowStride
);
634 /* Copy user texture image into the texture buffer.
636 if (compressed_src
) {
637 const GLuint srcRowStride
=
638 _mesa_format_row_stride(texImage
->TexFormat
, width
);
639 if (dstRowStride
== srcRowStride
) {
640 memcpy(texImage
->Data
, pixels
, imageSize
);
643 char *dst
= texImage
->Data
;
644 const char *src
= pixels
;
645 GLuint i
, bw
, bh
, lines
;
646 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
647 lines
= (height
+ bh
- 1) / bh
;
649 for (i
= 0; i
< lines
; ++i
) {
650 memcpy(dst
, src
, srcRowStride
);
657 const GLuint srcImageStride
=
658 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
660 const GLubyte
*src
= (const GLubyte
*) pixels
;
662 for (i
= 0; i
< depth
; i
++) {
663 if (!_mesa_texstore(ctx
, dims
,
664 texImage
->_BaseFormat
,
667 0, 0, 0, /* dstX/Y/Zoffset */
669 texImage
->ImageOffsets
,
671 format
, type
, src
, unpack
)) {
672 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
675 if (stImage
->pt
&& i
+ 1 < depth
) {
676 /* unmap this slice */
677 st_texture_image_unmap(st
, stImage
);
678 /* map next slice of 3D texture */
679 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
680 transfer_usage
, 0, 0,
682 src
+= srcImageStride
;
688 _mesa_unmap_teximage_pbo(ctx
, unpack
);
690 if (stImage
->pt
&& texImage
->Data
) {
691 st_texture_image_unmap(st
, stImage
);
692 texImage
->Data
= NULL
;
698 st_TexImage3D(struct gl_context
* ctx
,
699 GLenum target
, GLint level
,
700 GLint internalFormat
,
701 GLint width
, GLint height
, GLint depth
,
703 GLenum format
, GLenum type
, const void *pixels
,
704 const struct gl_pixelstore_attrib
*unpack
,
705 struct gl_texture_object
*texObj
,
706 struct gl_texture_image
*texImage
)
708 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
709 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
715 st_TexImage2D(struct gl_context
* ctx
,
716 GLenum target
, GLint level
,
717 GLint internalFormat
,
718 GLint width
, GLint height
, GLint border
,
719 GLenum format
, GLenum type
, const void *pixels
,
720 const struct gl_pixelstore_attrib
*unpack
,
721 struct gl_texture_object
*texObj
,
722 struct gl_texture_image
*texImage
)
724 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
725 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
730 st_TexImage1D(struct gl_context
* ctx
,
731 GLenum target
, GLint level
,
732 GLint internalFormat
,
733 GLint width
, GLint border
,
734 GLenum format
, GLenum type
, const void *pixels
,
735 const struct gl_pixelstore_attrib
*unpack
,
736 struct gl_texture_object
*texObj
,
737 struct gl_texture_image
*texImage
)
739 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
740 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
745 st_CompressedTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
746 GLint internalFormat
,
747 GLint width
, GLint height
, GLint border
,
748 GLsizei imageSize
, const GLvoid
*data
,
749 struct gl_texture_object
*texObj
,
750 struct gl_texture_image
*texImage
)
752 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
753 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
759 * glGetTexImage() helper: decompress a compressed texture by rendering
760 * a textured quad. Store the results in the user's buffer.
763 decompress_with_blit(struct gl_context
* ctx
, GLenum target
, GLint level
,
764 GLenum format
, GLenum type
, GLvoid
*pixels
,
765 struct gl_texture_object
*texObj
,
766 struct gl_texture_image
*texImage
)
768 struct st_context
*st
= st_context(ctx
);
769 struct pipe_context
*pipe
= st
->pipe
;
770 struct st_texture_image
*stImage
= st_texture_image(texImage
);
771 struct st_texture_object
*stObj
= st_texture_object(texObj
);
772 struct pipe_sampler_view
*src_view
=
773 st_get_texture_sampler_view(stObj
, pipe
);
774 const GLuint width
= texImage
->Width
;
775 const GLuint height
= texImage
->Height
;
776 struct pipe_surface
*dst_surface
;
777 struct pipe_resource
*dst_texture
;
778 struct pipe_transfer
*tex_xfer
;
779 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
780 PIPE_BIND_TRANSFER_READ
);
782 /* create temp / dest surface */
783 if (!util_create_rgba_surface(pipe
, width
, height
, bind
,
784 &dst_texture
, &dst_surface
)) {
785 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
786 "in decompress_with_blit()");
790 /* blit/render/decompress */
791 util_blit_pixels_tex(st
->blit
,
792 src_view
, /* pipe_resource (src) */
793 0, 0, /* src x0, y0 */
794 width
, height
, /* src x1, y1 */
795 dst_surface
, /* pipe_surface (dst) */
796 0, 0, /* dst x0, y0 */
797 width
, height
, /* dst x1, y1 */
799 PIPE_TEX_MIPFILTER_NEAREST
);
801 /* map the dst_surface so we can read from it */
802 tex_xfer
= pipe_get_transfer(st_context(ctx
)->pipe
,
805 0, 0, width
, height
);
807 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
809 /* copy/pack data into user buffer */
810 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
812 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
813 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
815 for (row
= 0; row
< height
; row
++) {
816 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
817 height
, format
, type
, row
, 0);
818 memcpy(dest
, map
, bytesPerRow
);
819 map
+= tex_xfer
->stride
;
821 pipe_transfer_unmap(pipe
, tex_xfer
);
824 /* format translation via floats */
826 enum pipe_format format
= util_format_linear(dst_texture
->format
);
827 for (row
= 0; row
< height
; row
++) {
828 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
829 GLfloat rgba
[4 * MAX_WIDTH
];
830 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
831 height
, format
, type
, row
, 0);
833 if (ST_DEBUG
& DEBUG_FALLBACK
)
834 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
836 /* get float[4] rgba row from surface */
837 pipe_get_tile_rgba_format(pipe
, tex_xfer
, 0, row
, width
, 1,
840 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
841 type
, dest
, &ctx
->Pack
, transferOps
);
845 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
847 pipe
->transfer_destroy(pipe
, tex_xfer
);
849 /* destroy the temp / dest surface */
850 util_destroy_rgba_surface(dst_texture
, dst_surface
);
856 * Need to map texture image into memory before copying image data,
860 st_get_tex_image(struct gl_context
* ctx
, GLenum target
, GLint level
,
861 GLenum format
, GLenum type
, GLvoid
* pixels
,
862 struct gl_texture_object
*texObj
,
863 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
865 struct st_context
*st
= st_context(ctx
);
866 struct st_texture_image
*stImage
= st_texture_image(texImage
);
867 const GLuint dstImageStride
=
868 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
874 util_format_is_s3tc(stImage
->pt
->format
) &&
876 /* Need to decompress the texture.
877 * We'll do this by rendering a textured quad.
878 * Note that we only expect RGBA formats (no Z/depth formats).
880 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
887 /* Image is stored in hardware format in a buffer managed by the
888 * kernel. Need to explicitly map and unmap it.
890 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
891 PIPE_TRANSFER_READ
, 0, 0,
893 stImage
->base
.Height
);
894 /* compute stride in texels from stride in bytes */
895 texImage
->RowStride
= stImage
->transfer
->stride
896 * util_format_get_blockwidth(stImage
->pt
->format
)
897 / util_format_get_blocksize(stImage
->pt
->format
);
900 /* Otherwise, the image should actually be stored in
901 * texImage->Data. This is pretty confusing for
902 * everybody, I'd much prefer to separate the two functions of
903 * texImage->Data - storage for texture images in main memory
904 * and access (ie mappings) of images. In other words, we'd
905 * create a new texImage->Map field and leave Data simply for
908 assert(texImage
->Data
);
911 depth
= texImage
->Depth
;
914 dest
= (GLubyte
*) pixels
;
916 _mesa_set_fetch_functions(texImage
, get_texture_dims(target
));
918 for (i
= 0; i
< depth
; i
++) {
919 if (compressed_dst
) {
920 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
924 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
928 if (stImage
->pt
&& i
+ 1 < depth
) {
929 /* unmap this slice */
930 st_texture_image_unmap(st
, stImage
);
931 /* map next slice of 3D texture */
932 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
933 PIPE_TRANSFER_READ
, 0, 0,
935 stImage
->base
.Height
);
936 dest
+= dstImageStride
;
940 texImage
->Depth
= depth
;
944 st_texture_image_unmap(st
, stImage
);
945 texImage
->Data
= NULL
;
951 st_GetTexImage(struct gl_context
* ctx
, GLenum target
, GLint level
,
952 GLenum format
, GLenum type
, GLvoid
* pixels
,
953 struct gl_texture_object
*texObj
,
954 struct gl_texture_image
*texImage
)
956 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
962 st_GetCompressedTexImage(struct gl_context
*ctx
, GLenum target
, GLint level
,
964 struct gl_texture_object
*texObj
,
965 struct gl_texture_image
*texImage
)
967 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
974 st_TexSubimage(struct gl_context
*ctx
, GLint dims
, GLenum target
, GLint level
,
975 GLint xoffset
, GLint yoffset
, GLint zoffset
,
976 GLint width
, GLint height
, GLint depth
,
977 GLenum format
, GLenum type
, const void *pixels
,
978 const struct gl_pixelstore_attrib
*packing
,
979 struct gl_texture_object
*texObj
,
980 struct gl_texture_image
*texImage
)
982 struct st_context
*st
= st_context(ctx
);
983 struct st_texture_image
*stImage
= st_texture_image(texImage
);
985 const GLuint srcImageStride
=
986 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
989 /* init to silence warning only: */
990 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
992 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
993 _mesa_lookup_enum_by_nr(target
),
994 level
, xoffset
, yoffset
, width
, height
);
997 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
998 type
, pixels
, packing
, "glTexSubImage2D");
1002 /* for a 1D array upload the image as a series of layer with height = 1 */
1003 if (target
== GL_TEXTURE_1D_ARRAY
) {
1008 /* Map buffer if necessary. Need to lock to prevent other contexts
1009 * from uploading the buffer under us.
1012 if (format
== GL_DEPTH_COMPONENT
&&
1013 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1014 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1016 transfer_usage
= PIPE_TRANSFER_WRITE
;
1018 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1024 if (!texImage
->Data
) {
1025 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1029 src
= (const GLubyte
*) pixels
;
1030 dstRowStride
= stImage
->transfer
->stride
;
1032 for (i
= 0; i
< depth
; i
++) {
1033 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1034 texImage
->TexFormat
,
1038 texImage
->ImageOffsets
,
1040 format
, type
, src
, packing
)) {
1041 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1044 if (stImage
->pt
&& i
+ 1 < depth
) {
1045 /* unmap this slice */
1046 st_texture_image_unmap(st
, stImage
);
1047 /* map next slice of 3D texture */
1048 texImage
->Data
= st_texture_image_map(st
, stImage
,
1053 src
+= srcImageStride
;
1058 _mesa_unmap_teximage_pbo(ctx
, packing
);
1060 if (stImage
->pt
&& texImage
->Data
) {
1061 st_texture_image_unmap(st
, stImage
);
1062 texImage
->Data
= NULL
;
1069 st_TexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1070 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1071 GLsizei width
, GLsizei height
, GLsizei depth
,
1072 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1073 const struct gl_pixelstore_attrib
*packing
,
1074 struct gl_texture_object
*texObj
,
1075 struct gl_texture_image
*texImage
)
1077 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1078 width
, height
, depth
, format
, type
,
1079 pixels
, packing
, texObj
, texImage
);
1084 st_TexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1085 GLint xoffset
, GLint yoffset
,
1086 GLsizei width
, GLsizei height
,
1087 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1088 const struct gl_pixelstore_attrib
*packing
,
1089 struct gl_texture_object
*texObj
,
1090 struct gl_texture_image
*texImage
)
1092 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1093 width
, height
, 1, format
, type
,
1094 pixels
, packing
, texObj
, texImage
);
1099 st_TexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1100 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1101 const GLvoid
* pixels
,
1102 const struct gl_pixelstore_attrib
*packing
,
1103 struct gl_texture_object
*texObj
,
1104 struct gl_texture_image
*texImage
)
1106 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1107 format
, type
, pixels
, packing
, texObj
, texImage
);
1112 st_CompressedTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1113 GLint xoffset
, GLsizei width
,
1115 GLsizei imageSize
, const GLvoid
*data
,
1116 struct gl_texture_object
*texObj
,
1117 struct gl_texture_image
*texImage
)
1124 st_CompressedTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1125 GLint xoffset
, GLint yoffset
,
1126 GLsizei width
, GLint height
,
1128 GLsizei imageSize
, const GLvoid
*data
,
1129 struct gl_texture_object
*texObj
,
1130 struct gl_texture_image
*texImage
)
1132 struct st_context
*st
= st_context(ctx
);
1133 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1137 enum pipe_format pformat
;
1140 pformat
= stImage
->pt
->format
;
1142 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1143 PIPE_TRANSFER_WRITE
,
1147 srcBlockStride
= util_format_get_stride(pformat
, width
);
1148 dstBlockStride
= stImage
->transfer
->stride
;
1150 assert(stImage
->pt
);
1151 /* TODO find good values for block and strides */
1152 /* TODO also adjust texImage->data for yoffset/xoffset */
1156 if (!texImage
->Data
) {
1157 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1161 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1162 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1164 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1165 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1166 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1167 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1168 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1172 st_texture_image_unmap(st
, stImage
);
1173 texImage
->Data
= NULL
;
1179 st_CompressedTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1180 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1181 GLsizei width
, GLint height
, GLint depth
,
1183 GLsizei imageSize
, const GLvoid
*data
,
1184 struct gl_texture_object
*texObj
,
1185 struct gl_texture_image
*texImage
)
1193 * Do a CopyTexSubImage operation using a read transfer from the source,
1194 * a write transfer to the destination and get_tile()/put_tile() to access
1195 * the pixels/texels.
1197 * Note: srcY=0=TOP of renderbuffer
1200 fallback_copy_texsubimage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1201 struct st_renderbuffer
*strb
,
1202 struct st_texture_image
*stImage
,
1204 GLint destX
, GLint destY
, GLint destZ
,
1205 GLint srcX
, GLint srcY
,
1206 GLsizei width
, GLsizei height
)
1208 struct st_context
*st
= st_context(ctx
);
1209 struct pipe_context
*pipe
= st
->pipe
;
1210 struct pipe_transfer
*src_trans
;
1212 enum pipe_transfer_usage transfer_usage
;
1214 if (ST_DEBUG
& DEBUG_FALLBACK
)
1215 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1217 assert(width
<= MAX_WIDTH
);
1219 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1220 srcY
= strb
->Base
.Height
- srcY
- height
;
1223 src_trans
= pipe_get_transfer(st_context(ctx
)->pipe
,
1230 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1231 baseFormat
== GL_DEPTH_STENCIL
) &&
1232 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1233 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1235 transfer_usage
= PIPE_TRANSFER_WRITE
;
1237 /* XXX this used to ignore destZ param */
1238 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
1239 destX
, destY
, width
, height
);
1241 if (baseFormat
== GL_DEPTH_COMPONENT
||
1242 baseFormat
== GL_DEPTH_STENCIL
) {
1243 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1244 ctx
->Pixel
.DepthBias
!= 0.0F
);
1247 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1248 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1257 /* To avoid a large temp memory allocation, do copy row by row */
1258 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1259 uint data
[MAX_WIDTH
];
1260 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1262 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1264 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1270 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1272 if (tempSrc
&& texDest
) {
1273 const GLint dims
= 2;
1274 const GLint dstRowStride
= stImage
->transfer
->stride
;
1275 struct gl_texture_image
*texImage
= &stImage
->base
;
1276 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1278 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1279 unpack
.Invert
= GL_TRUE
;
1282 /* get float/RGBA image from framebuffer */
1283 /* XXX this usually involves a lot of int/float conversion.
1284 * try to avoid that someday.
1286 pipe_get_tile_rgba_format(pipe
, src_trans
, 0, 0, width
, height
,
1287 util_format_linear(strb
->texture
->format
),
1290 /* Store into texture memory.
1291 * Note that this does some special things such as pixel transfer
1292 * ops and format conversion. In particular, if the dest tex format
1293 * is actually RGBA but the user created the texture as GL_RGB we
1294 * need to fill-in/override the alpha channel with 1.0.
1296 _mesa_texstore(ctx
, dims
,
1297 texImage
->_BaseFormat
,
1298 texImage
->TexFormat
,
1302 texImage
->ImageOffsets
,
1304 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1308 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1315 st_texture_image_unmap(st
, stImage
);
1316 pipe
->transfer_destroy(pipe
, src_trans
);
1322 * If the format of the src renderbuffer and the format of the dest
1323 * texture are compatible (in terms of blitting), return a TGSI writemask
1324 * to be used during the blit.
1325 * If the src/dest are incompatible, return 0.
1328 compatible_src_dst_formats(struct gl_context
*ctx
,
1329 const struct gl_renderbuffer
*src
,
1330 const struct gl_texture_image
*dst
)
1332 /* Get logical base formats for the src and dest.
1333 * That is, use the user-requested formats and not the actual, device-
1335 * For example, the user may have requested an A8 texture but the
1336 * driver may actually be using an RGBA texture format. When we
1337 * copy/blit to that texture, we only want to copy the Alpha channel
1338 * and not the RGB channels.
1340 * Similarly, when the src FBO was created an RGB format may have been
1341 * requested but the driver actually chose an RGBA format. In that case,
1342 * we don't want to copy the undefined Alpha channel to the dest texture
1343 * (it should be 1.0).
1345 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1346 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1349 * XXX when we have red-only and red/green renderbuffers we'll need
1350 * to add more cases here (or implement a general-purpose routine that
1351 * queries the existance of the R,G,B,A channels in the src and dest).
1353 if (srcFormat
== dstFormat
) {
1354 /* This is the same as matching_base_formats, which should
1355 * always pass, as it did previously.
1357 return TGSI_WRITEMASK_XYZW
;
1359 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1360 /* Make sure that A in the dest is 1. The actual src format
1361 * may be RGBA and have undefined A values.
1363 return TGSI_WRITEMASK_XYZ
;
1365 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1366 /* Make sure that A in the dest is 1. The actual dst format
1367 * may be RGBA and will need A=1 to provide proper alpha values
1368 * when sampled later.
1370 return TGSI_WRITEMASK_XYZ
;
1373 if (ST_DEBUG
& DEBUG_FALLBACK
)
1374 debug_printf("%s failed for src %s, dst %s\n",
1376 _mesa_lookup_enum_by_nr(srcFormat
),
1377 _mesa_lookup_enum_by_nr(dstFormat
));
1388 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1389 * Note that the region to copy has already been clipped so we know we
1390 * won't read from outside the source renderbuffer's bounds.
1392 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1395 st_copy_texsubimage(struct gl_context
*ctx
,
1396 GLenum target
, GLint level
,
1397 GLint destX
, GLint destY
, GLint destZ
,
1398 GLint srcX
, GLint srcY
,
1399 GLsizei width
, GLsizei height
)
1401 struct gl_texture_unit
*texUnit
=
1402 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1403 struct gl_texture_object
*texObj
=
1404 _mesa_select_tex_object(ctx
, texUnit
, target
);
1405 struct gl_texture_image
*texImage
=
1406 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1407 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1408 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1409 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1410 struct st_renderbuffer
*strb
;
1411 struct st_context
*st
= st_context(ctx
);
1412 struct pipe_context
*pipe
= st
->pipe
;
1413 struct pipe_screen
*screen
= pipe
->screen
;
1414 enum pipe_format dest_format
, src_format
;
1415 GLboolean use_fallback
= GL_TRUE
;
1416 GLboolean matching_base_formats
;
1417 GLuint format_writemask
, sample_count
;
1418 struct pipe_surface
*dest_surface
= NULL
;
1419 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1421 /* make sure finalize_textures has been called?
1423 if (0) st_validate_state(st
);
1425 /* determine if copying depth or color data */
1426 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1427 texBaseFormat
== GL_DEPTH_STENCIL
) {
1428 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1429 if (strb
->Base
.Wrapped
) {
1430 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
1434 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1435 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1438 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1439 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1443 sample_count
= strb
->surface
->texture
->nr_samples
;
1444 /* I believe this would be legal, presumably would need to do a resolve
1445 for color, and for depth/stencil spec says to just use one of the
1446 depth/stencil samples per pixel? Need some transfer clarifications. */
1447 assert(sample_count
< 2);
1473 if (width
< 0 || height
< 0)
1478 assert(strb
->surface
);
1479 assert(stImage
->pt
);
1481 src_format
= strb
->surface
->format
;
1482 dest_format
= stImage
->pt
->format
;
1485 * Determine if the src framebuffer and dest texture have the same
1486 * base format. We need this to detect a case such as the framebuffer
1487 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1488 * texture format stores RGBA we need to set A=1 (overriding the
1489 * framebuffer's alpha values). We can't do that with the blit or
1490 * textured-quad paths.
1492 matching_base_formats
=
1493 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1494 _mesa_get_format_base_format(texImage
->TexFormat
));
1495 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1497 if (ctx
->_ImageTransferState
== 0x0) {
1499 if (matching_base_formats
&&
1500 src_format
== dest_format
&&
1503 /* use surface_copy() / blit */
1504 struct pipe_box src_box
;
1505 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
1506 width
, height
, &src_box
);
1508 /* for resource_copy_region(), y=0=top, always */
1509 pipe
->resource_copy_region(pipe
,
1513 destX
, destY
, destZ
+ stImage
->face
,
1516 strb
->surface
->u
.tex
.level
,
1518 use_fallback
= GL_FALSE
;
1520 else if (format_writemask
&&
1521 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1522 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1523 screen
->is_format_supported(screen
, src_format
,
1524 PIPE_TEXTURE_2D
, sample_count
,
1525 PIPE_BIND_SAMPLER_VIEW
) &&
1526 screen
->is_format_supported(screen
, dest_format
,
1528 PIPE_BIND_RENDER_TARGET
)) {
1529 /* draw textured quad to do the copy */
1531 struct pipe_surface surf_tmpl
;
1532 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1533 surf_tmpl
.format
= stImage
->pt
->format
;
1534 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
1535 surf_tmpl
.u
.tex
.level
= stImage
->level
;
1536 surf_tmpl
.u
.tex
.first_layer
= stImage
->face
+ destZ
;
1537 surf_tmpl
.u
.tex
.last_layer
= stImage
->face
+ destZ
;
1539 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1543 srcY1
= strb
->Base
.Height
- srcY
- height
;
1544 srcY0
= srcY1
+ height
;
1548 srcY1
= srcY0
+ height
;
1551 util_blit_pixels_writemask(st
->blit
,
1553 strb
->surface
->u
.tex
.level
,
1555 srcX
+ width
, srcY1
,
1556 strb
->surface
->u
.tex
.first_layer
,
1559 destX
+ width
, destY
+ height
,
1560 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1562 use_fallback
= GL_FALSE
;
1566 pipe_surface_reference(&dest_surface
, NULL
);
1570 /* software fallback */
1571 fallback_copy_texsubimage(ctx
, target
, level
,
1572 strb
, stImage
, texBaseFormat
,
1573 destX
, destY
, destZ
,
1574 srcX
, srcY
, width
, height
);
1581 st_CopyTexImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1582 GLenum internalFormat
,
1583 GLint x
, GLint y
, GLsizei width
, GLint border
)
1585 struct gl_texture_unit
*texUnit
=
1586 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1587 struct gl_texture_object
*texObj
=
1588 _mesa_select_tex_object(ctx
, texUnit
, target
);
1589 struct gl_texture_image
*texImage
=
1590 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1592 /* Setup or redefine the texture object, texture and texture
1593 * image. Don't populate yet.
1595 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1597 GL_RGBA
, CHAN_TYPE
, NULL
,
1598 &ctx
->DefaultPacking
, texObj
, texImage
);
1600 st_copy_texsubimage(ctx
, target
, level
,
1601 0, 0, 0, /* destX,Y,Z */
1602 x
, y
, width
, 1); /* src X, Y, size */
1607 st_CopyTexImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1608 GLenum internalFormat
,
1609 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1612 struct gl_texture_unit
*texUnit
=
1613 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1614 struct gl_texture_object
*texObj
=
1615 _mesa_select_tex_object(ctx
, texUnit
, target
);
1616 struct gl_texture_image
*texImage
=
1617 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1619 /* Setup or redefine the texture object, texture and texture
1620 * image. Don't populate yet.
1622 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1623 width
, height
, border
,
1624 GL_RGBA
, CHAN_TYPE
, NULL
,
1625 &ctx
->DefaultPacking
, texObj
, texImage
);
1627 st_copy_texsubimage(ctx
, target
, level
,
1628 0, 0, 0, /* destX,Y,Z */
1629 x
, y
, width
, height
); /* src X, Y, size */
1634 st_CopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1635 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1637 const GLint yoffset
= 0, zoffset
= 0;
1638 const GLsizei height
= 1;
1639 st_copy_texsubimage(ctx
, target
, level
,
1640 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1641 x
, y
, width
, height
); /* src X, Y, size */
1646 st_CopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1647 GLint xoffset
, GLint yoffset
,
1648 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1650 const GLint zoffset
= 0;
1651 st_copy_texsubimage(ctx
, target
, level
,
1652 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1653 x
, y
, width
, height
); /* src X, Y, size */
1658 st_CopyTexSubImage3D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1659 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1660 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1662 st_copy_texsubimage(ctx
, target
, level
,
1663 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1664 x
, y
, width
, height
); /* src X, Y, size */
1669 * Copy image data from stImage into the texture object 'stObj' at level
1673 copy_image_data_to_texture(struct st_context
*st
,
1674 struct st_texture_object
*stObj
,
1676 struct st_texture_image
*stImage
)
1680 const struct gl_texture_image
*dstImage
=
1681 stObj
->base
.Image
[stImage
->face
][stImage
->level
];
1683 assert(dstImage
->Width
== stImage
->base
.Width
);
1684 assert(dstImage
->Height
== stImage
->base
.Height
);
1685 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1689 /* Copy potentially with the blitter:
1691 st_texture_image_copy(st
->pipe
,
1692 stObj
->pt
, dstLevel
, /* dest texture, level */
1693 stImage
->pt
, stImage
->level
, /* src texture, level */
1696 pipe_resource_reference(&stImage
->pt
, NULL
);
1698 else if (stImage
->base
.Data
) {
1699 st_texture_image_data(st
,
1704 stImage
->base
.RowStride
*
1705 util_format_get_blocksize(stObj
->pt
->format
),
1706 stImage
->base
.RowStride
*
1707 stImage
->base
.Height
*
1708 util_format_get_blocksize(stObj
->pt
->format
));
1709 _mesa_align_free(stImage
->base
.Data
);
1710 stImage
->base
.Data
= NULL
;
1713 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1718 * Called during state validation. When this function is finished,
1719 * the texture object should be ready for rendering.
1720 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1723 st_finalize_texture(struct gl_context
*ctx
,
1724 struct pipe_context
*pipe
,
1725 struct gl_texture_object
*tObj
)
1727 struct st_context
*st
= st_context(ctx
);
1728 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1729 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1731 struct st_texture_image
*firstImage
;
1732 enum pipe_format firstImageFormat
;
1733 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1735 if (stObj
->base
._Complete
) {
1736 /* The texture is complete and we know exactly how many mipmap levels
1737 * are present/needed. This is conditional because we may be called
1738 * from the st_generate_mipmap() function when the texture object is
1739 * incomplete. In that case, we'll have set stObj->lastLevel before
1742 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1743 stObj
->base
.MinFilter
== GL_NEAREST
)
1744 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1746 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1749 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1752 /* If both firstImage and stObj point to a texture which can contain
1753 * all active images, favour firstImage. Note that because of the
1754 * completeness requirement, we know that the image dimensions
1757 if (firstImage
->pt
&&
1758 firstImage
->pt
!= stObj
->pt
&&
1759 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1760 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1761 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1764 /* Find gallium format for the Mesa texture */
1765 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1767 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1769 GLuint width
, height
, depth
;
1770 if (!guess_base_level_size(stObj
->base
.Target
,
1771 firstImage
->base
.Width2
,
1772 firstImage
->base
.Height2
,
1773 firstImage
->base
.Depth2
,
1774 stObj
->base
.BaseLevel
,
1775 &width
, &height
, &depth
)) {
1776 width
= stObj
->width0
;
1777 height
= stObj
->height0
;
1778 depth
= stObj
->depth0
;
1780 /* convert GL dims to Gallium dims */
1781 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1782 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1785 /* If we already have a gallium texture, check that it matches the texture
1786 * object's format, target, size, num_levels, etc.
1789 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1790 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1791 stObj
->pt
->last_level
< stObj
->lastLevel
||
1792 stObj
->pt
->width0
!= ptWidth
||
1793 stObj
->pt
->height0
!= ptHeight
||
1794 stObj
->pt
->depth0
!= ptDepth
||
1795 stObj
->pt
->array_size
!= ptLayers
)
1797 /* The gallium texture does not match the Mesa texture so delete the
1798 * gallium texture now. We'll make a new one below.
1800 pipe_resource_reference(&stObj
->pt
, NULL
);
1801 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1802 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1806 /* May need to create a new gallium texture:
1809 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1811 stObj
->pt
= st_texture_create(st
,
1812 gl_target_to_pipe(stObj
->base
.Target
),
1822 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1827 /* Pull in any images not in the object's texture:
1829 for (face
= 0; face
< nr_faces
; face
++) {
1831 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1832 struct st_texture_image
*stImage
=
1833 st_texture_image(stObj
->base
.Image
[face
][level
]);
1835 /* Need to import images in main memory or held in other textures.
1837 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1838 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1848 * Returns pointer to a default/dummy texture.
1849 * This is typically used when the current shader has tex/sample instructions
1850 * but the user has not provided a (any) texture(s).
1852 struct gl_texture_object
*
1853 st_get_default_texture(struct st_context
*st
)
1855 if (!st
->default_texture
) {
1856 static const GLenum target
= GL_TEXTURE_2D
;
1857 GLubyte pixels
[16][16][4];
1858 struct gl_texture_object
*texObj
;
1859 struct gl_texture_image
*texImg
;
1862 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1863 * when attempting to sample incomplete textures.
1865 for (i
= 0; i
< 16; i
++) {
1866 for (j
= 0; j
< 16; j
++) {
1867 pixels
[i
][j
][0] = 0;
1868 pixels
[i
][j
][1] = 0;
1869 pixels
[i
][j
][2] = 0;
1870 pixels
[i
][j
][3] = 255;
1874 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1876 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1878 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1879 16, 16, 1, 0, /* w, h, d, border */
1880 GL_RGBA
, MESA_FORMAT_RGBA8888
);
1882 st_TexImage(st
->ctx
, 2, target
,
1883 0, GL_RGBA
, /* level, intformat */
1884 16, 16, 1, 0, /* w, h, d, border */
1885 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1886 &st
->ctx
->DefaultPacking
,
1890 texObj
->MinFilter
= GL_NEAREST
;
1891 texObj
->MagFilter
= GL_NEAREST
;
1892 texObj
->_Complete
= GL_TRUE
;
1894 st
->default_texture
= texObj
;
1896 return st
->default_texture
;
1901 st_init_texture_functions(struct dd_function_table
*functions
)
1903 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1904 functions
->TexImage1D
= st_TexImage1D
;
1905 functions
->TexImage2D
= st_TexImage2D
;
1906 functions
->TexImage3D
= st_TexImage3D
;
1907 functions
->TexSubImage1D
= st_TexSubImage1D
;
1908 functions
->TexSubImage2D
= st_TexSubImage2D
;
1909 functions
->TexSubImage3D
= st_TexSubImage3D
;
1910 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1911 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1912 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1913 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1914 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1915 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1916 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1917 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1918 functions
->GenerateMipmap
= st_generate_mipmap
;
1920 functions
->GetTexImage
= st_GetTexImage
;
1922 /* compressed texture functions */
1923 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1924 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1926 functions
->NewTextureObject
= st_NewTextureObject
;
1927 functions
->NewTextureImage
= st_NewTextureImage
;
1928 functions
->DeleteTexture
= st_DeleteTextureObject
;
1929 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1931 functions
->TextureMemCpy
= do_memcpy
;
1933 /* XXX Temporary until we can query pipe's texture sizes */
1934 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;