1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "main/mfeatures.h"
29 #include "main/bufferobj.h"
30 #include "main/enums.h"
31 #include "main/fbobject.h"
32 #include "main/formats.h"
33 #include "main/image.h"
34 #include "main/imports.h"
35 #include "main/macros.h"
36 #include "main/mipmap.h"
37 #include "main/pack.h"
39 #include "main/pixeltransfer.h"
40 #include "main/texcompress.h"
41 #include "main/texfetch.h"
42 #include "main/texgetimage.h"
43 #include "main/teximage.h"
44 #include "main/texobj.h"
45 #include "main/texstore.h"
47 #include "state_tracker/st_debug.h"
48 #include "state_tracker/st_context.h"
49 #include "state_tracker/st_cb_fbo.h"
50 #include "state_tracker/st_cb_flush.h"
51 #include "state_tracker/st_cb_texture.h"
52 #include "state_tracker/st_format.h"
53 #include "state_tracker/st_texture.h"
54 #include "state_tracker/st_gen_mipmap.h"
55 #include "state_tracker/st_atom.h"
57 #include "pipe/p_context.h"
58 #include "pipe/p_defines.h"
59 #include "util/u_inlines.h"
60 #include "pipe/p_shader_tokens.h"
61 #include "util/u_tile.h"
62 #include "util/u_blit.h"
63 #include "util/u_format.h"
64 #include "util/u_surface.h"
65 #include "util/u_sampler.h"
66 #include "util/u_math.h"
67 #include "util/u_box.h"
69 #define DBG if (0) printf
72 static enum pipe_texture_target
73 gl_target_to_pipe(GLenum target
)
77 return PIPE_TEXTURE_1D
;
79 return PIPE_TEXTURE_2D
;
80 case GL_TEXTURE_RECTANGLE_NV
:
81 return PIPE_TEXTURE_RECT
;
83 return PIPE_TEXTURE_3D
;
84 case GL_TEXTURE_CUBE_MAP_ARB
:
85 return PIPE_TEXTURE_CUBE
;
86 case GL_TEXTURE_1D_ARRAY_EXT
:
87 return PIPE_TEXTURE_1D_ARRAY
;
88 case GL_TEXTURE_2D_ARRAY_EXT
:
89 return PIPE_TEXTURE_2D_ARRAY
;
97 /** called via ctx->Driver.NewTextureImage() */
98 static struct gl_texture_image
*
99 st_NewTextureImage(struct gl_context
* ctx
)
101 DBG("%s\n", __FUNCTION__
);
103 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
107 /** called via ctx->Driver.NewTextureObject() */
108 static struct gl_texture_object
*
109 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
111 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
113 DBG("%s\n", __FUNCTION__
);
114 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
119 /** called via ctx->Driver.DeleteTextureObject() */
121 st_DeleteTextureObject(struct gl_context
*ctx
,
122 struct gl_texture_object
*texObj
)
124 struct st_context
*st
= st_context(ctx
);
125 struct st_texture_object
*stObj
= st_texture_object(texObj
);
127 pipe_resource_reference(&stObj
->pt
, NULL
);
128 if (stObj
->sampler_view
) {
129 if (stObj
->sampler_view
->context
!= st
->pipe
) {
130 /* Take "ownership" of this texture sampler view by setting
131 * its context pointer to this context. This avoids potential
132 * crashes when the texture object is shared among contexts
133 * and the original/owner context has already been destroyed.
135 stObj
->sampler_view
->context
= st
->pipe
;
137 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
139 _mesa_delete_texture_object(ctx
, texObj
);
143 /** called via ctx->Driver.FreeTexImageData() */
145 st_FreeTextureImageData(struct gl_context
* ctx
, struct gl_texture_image
*texImage
)
147 struct st_texture_image
*stImage
= st_texture_image(texImage
);
149 DBG("%s\n", __FUNCTION__
);
152 pipe_resource_reference(&stImage
->pt
, NULL
);
155 if (texImage
->Data
) {
156 _mesa_align_free(texImage
->Data
);
157 texImage
->Data
= NULL
;
163 * From linux kernel i386 header files, copes with odd sizes better
164 * than COPY_DWORDS would:
165 * XXX Put this in src/mesa/main/imports.h ???
167 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
169 __memcpy(void *to
, const void *from
, size_t n
)
172 __asm__
__volatile__("rep ; movsl\n\t"
176 "1:\ttestb $1,%b4\n\t"
178 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
179 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
184 #define __memcpy(a,b,c) memcpy(a,b,c)
189 * The system memcpy (at least on ubuntu 5.10) has problems copying
190 * to agp (writecombined) memory from a source which isn't 64-byte
191 * aligned - there is a 4x performance falloff.
193 * The x86 __memcpy is immune to this but is slightly slower
194 * (10%-ish) than the system memcpy.
196 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
197 * isn't much faster than x86_memcpy for agp copies.
199 * TODO: switch dynamically.
202 do_memcpy(void *dest
, const void *src
, size_t n
)
204 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
205 return __memcpy(dest
, src
, n
);
208 return memcpy(dest
, src
, n
);
213 * Return default texture resource binding bitmask for the given format.
216 default_bindings(struct st_context
*st
, enum pipe_format format
)
218 struct pipe_screen
*screen
= st
->pipe
->screen
;
219 const unsigned target
= PIPE_TEXTURE_2D
;
220 const unsigned geom
= 0x0;
223 if (util_format_is_depth_or_stencil(format
))
224 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
226 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
228 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
, geom
))
232 format
= util_format_linear(format
);
234 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
, geom
))
237 return PIPE_BIND_SAMPLER_VIEW
;
242 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
244 get_texture_dims(GLenum target
)
248 case GL_TEXTURE_1D_ARRAY_EXT
:
251 case GL_TEXTURE_CUBE_MAP_ARB
:
252 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
253 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
254 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
255 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
256 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
257 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
258 case GL_TEXTURE_RECTANGLE_NV
:
259 case GL_TEXTURE_2D_ARRAY_EXT
:
264 assert(0 && "invalid texture target in get_texture_dims()");
271 * Given the size of a mipmap image, try to compute the size of the level=0
274 * Note that this isn't always accurate for odd-sized, non-POW textures.
275 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
277 * \return GL_TRUE for success, GL_FALSE for failure
280 guess_base_level_size(GLenum target
,
281 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
282 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
284 const GLuint dims
= get_texture_dims(target
);
291 /* Depending on the image's size, we can't always make a guess here */
292 if ((dims
>= 1 && width
== 1) ||
293 (dims
>= 2 && height
== 1) ||
294 (dims
>= 3 && depth
== 1)) {
295 /* we can't determine the image size at level=0 */
299 /* grow the image size until we hit level = 0 */
320 * Try to allocate a pipe_resource object for the given st_texture_object.
322 * We use the given st_texture_image as a clue to determine the size of the
323 * mipmap image at level=0.
325 * \return GL_TRUE for success, GL_FALSE if out of memory.
328 guess_and_alloc_texture(struct st_context
*st
,
329 struct st_texture_object
*stObj
,
330 const struct st_texture_image
*stImage
)
332 GLuint lastLevel
, width
, height
, depth
;
334 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
335 enum pipe_format fmt
;
337 DBG("%s\n", __FUNCTION__
);
341 if (!guess_base_level_size(stObj
->base
.Target
,
342 stImage
->base
.Width2
,
343 stImage
->base
.Height2
,
344 stImage
->base
.Depth2
,
346 &width
, &height
, &depth
)) {
347 /* we can't determine the image size at level=0 */
348 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
349 /* this is not an out of memory error */
353 /* At this point, (width x height x depth) is the expected size of
354 * the level=0 mipmap image.
357 /* Guess a reasonable value for lastLevel. With OpenGL we have no
358 * idea how many mipmap levels will be in a texture until we start
359 * to render with it. Make an educated guess here but be prepared
360 * to re-allocating a texture buffer with space for more (or fewer)
361 * mipmap levels later.
363 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
364 stObj
->base
.MinFilter
== GL_LINEAR
||
365 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
366 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
367 !stObj
->base
.GenerateMipmap
&&
368 stImage
->level
== 0) {
369 /* only alloc space for a single mipmap level */
373 /* alloc space for a full mipmap */
374 GLuint l2width
= util_logbase2(width
);
375 GLuint l2height
= util_logbase2(height
);
376 GLuint l2depth
= util_logbase2(depth
);
377 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
380 /* Save the level=0 dimensions */
381 stObj
->width0
= width
;
382 stObj
->height0
= height
;
383 stObj
->depth0
= depth
;
385 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
387 bindings
= default_bindings(st
, fmt
);
389 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
390 width
, height
, depth
,
391 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
393 stObj
->pt
= st_texture_create(st
,
394 gl_target_to_pipe(stObj
->base
.Target
),
403 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
405 return stObj
->pt
!= NULL
;
410 * Adjust pixel unpack params and image dimensions to strip off the
412 * Gallium doesn't support texture borders. They've seldem been used
413 * and seldom been implemented correctly anyway.
414 * \param unpackNew returns the new pixel unpack parameters
417 strip_texture_border(GLint border
,
418 GLint
*width
, GLint
*height
, GLint
*depth
,
419 const struct gl_pixelstore_attrib
*unpack
,
420 struct gl_pixelstore_attrib
*unpackNew
)
422 assert(border
> 0); /* sanity check */
424 *unpackNew
= *unpack
;
426 if (unpackNew
->RowLength
== 0)
427 unpackNew
->RowLength
= *width
;
429 if (depth
&& unpackNew
->ImageHeight
== 0)
430 unpackNew
->ImageHeight
= *height
;
432 unpackNew
->SkipPixels
+= border
;
434 unpackNew
->SkipRows
+= border
;
436 unpackNew
->SkipImages
+= border
;
439 *width
= *width
- 2 * border
;
440 if (height
&& *height
>= 3)
441 *height
= *height
- 2 * border
;
442 if (depth
&& *depth
>= 3)
443 *depth
= *depth
- 2 * border
;
448 * Do glTexImage1/2/3D().
451 st_TexImage(struct gl_context
* ctx
,
453 GLenum target
, GLint level
,
454 GLint internalFormat
,
455 GLint width
, GLint height
, GLint depth
,
457 GLenum format
, GLenum type
, const void *pixels
,
458 const struct gl_pixelstore_attrib
*unpack
,
459 struct gl_texture_object
*texObj
,
460 struct gl_texture_image
*texImage
,
461 GLsizei imageSize
, GLboolean compressed_src
)
463 struct st_context
*st
= st_context(ctx
);
464 struct st_texture_object
*stObj
= st_texture_object(texObj
);
465 struct st_texture_image
*stImage
= st_texture_image(texImage
);
466 GLuint dstRowStride
= 0;
467 struct gl_pixelstore_attrib unpackNB
;
468 enum pipe_transfer_usage transfer_usage
= 0;
470 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
471 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
473 /* switch to "normal" */
474 if (stObj
->surface_based
) {
477 _mesa_clear_texture_object(ctx
, texObj
);
478 pipe_resource_reference(&stObj
->pt
, NULL
);
480 /* oops, need to init this image again */
481 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
482 internalFormat
, format
, type
);
484 _mesa_init_teximage_fields(ctx
, target
, texImage
,
485 width
, height
, depth
, border
,
486 internalFormat
, texFormat
);
488 stObj
->surface_based
= GL_FALSE
;
491 /* gallium does not support texture borders, strip it off */
493 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
495 texImage
->Width
= width
;
496 texImage
->Height
= height
;
497 texImage
->Depth
= depth
;
498 texImage
->Border
= 0;
502 assert(texImage
->Width
== width
);
503 assert(texImage
->Height
== height
);
504 assert(texImage
->Depth
== depth
);
507 stImage
->face
= _mesa_tex_target_to_face(target
);
508 stImage
->level
= level
;
510 _mesa_set_fetch_functions(texImage
, dims
);
512 /* Release the reference to a potentially orphaned buffer.
513 * Release any old malloced memory.
516 pipe_resource_reference(&stImage
->pt
, NULL
);
517 assert(!texImage
->Data
);
519 else if (texImage
->Data
) {
520 _mesa_align_free(texImage
->Data
);
524 * See if the new image is somehow incompatible with the existing
525 * mipmap. If so, free the old mipmap.
528 if (level
> (GLint
) stObj
->pt
->last_level
||
529 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
530 stImage
->face
, stImage
->level
)) {
532 pipe_resource_reference(&stObj
->pt
, NULL
);
534 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
538 if (width
== 0 || height
== 0 || depth
== 0) {
539 /* stop after freeing old image */
544 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
545 /* Probably out of memory.
546 * Try flushing any pending rendering, then retry.
549 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
550 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
556 assert(!stImage
->pt
);
558 /* Check if this texture image can live inside the texture object's buffer.
559 * If so, store the image there. Otherwise the image will temporarily live
563 st_texture_match_image(stObj
->pt
, &stImage
->base
,
564 stImage
->face
, stImage
->level
)) {
566 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
571 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
573 /* Pixel data may come from regular user memory or a PBO. For the later,
574 * do bounds checking and map the PBO to read pixels data from it.
576 * XXX we should try to use a GPU-accelerated path to copy the image data
577 * from the PBO to the texture.
579 if (compressed_src
) {
580 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
582 "glCompressedTexImage");
585 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
587 pixels
, unpack
, "glTexImage");
590 /* for a 1D array upload the image as a series of layer with height = 1 */
591 if (target
== GL_TEXTURE_1D_ARRAY
) {
597 * Prepare to store the texture data. Either map the gallium texture buffer
598 * memory or malloc space for it.
601 /* Store the image in the gallium texture memory buffer */
602 if (format
== GL_DEPTH_COMPONENT
&&
603 util_format_is_depth_and_stencil(stImage
->pt
->format
))
604 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
606 transfer_usage
= PIPE_TRANSFER_WRITE
;
608 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
609 transfer_usage
, 0, 0, width
, height
);
610 if(stImage
->transfer
)
611 dstRowStride
= stImage
->transfer
->stride
;
614 /* Allocate regular memory and store the image there temporarily. */
615 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
616 width
, height
, depth
);
617 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
619 texImage
->Data
= _mesa_align_malloc(imageSize
, 16);
622 if (!texImage
->Data
) {
623 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
628 /* We've allocated texture memory, but have no pixel data - all done. */
632 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
633 width
, height
, depth
, width
, dstRowStride
);
635 /* Copy user texture image into the texture buffer.
637 if (compressed_src
) {
638 const GLuint srcRowStride
=
639 _mesa_format_row_stride(texImage
->TexFormat
, width
);
640 if (dstRowStride
== srcRowStride
) {
641 memcpy(texImage
->Data
, pixels
, imageSize
);
644 char *dst
= texImage
->Data
;
645 const char *src
= pixels
;
646 GLuint i
, bw
, bh
, lines
;
647 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
648 lines
= (height
+ bh
- 1) / bh
;
650 for (i
= 0; i
< lines
; ++i
) {
651 memcpy(dst
, src
, srcRowStride
);
658 const GLuint srcImageStride
=
659 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
661 const GLubyte
*src
= (const GLubyte
*) pixels
;
663 for (i
= 0; i
< depth
; i
++) {
664 if (!_mesa_texstore(ctx
, dims
,
665 texImage
->_BaseFormat
,
668 0, 0, 0, /* dstX/Y/Zoffset */
670 texImage
->ImageOffsets
,
672 format
, type
, src
, unpack
)) {
673 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
676 if (stImage
->pt
&& i
+ 1 < depth
) {
677 /* unmap this slice */
678 st_texture_image_unmap(st
, stImage
);
679 /* map next slice of 3D texture */
680 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
681 transfer_usage
, 0, 0,
683 src
+= srcImageStride
;
689 _mesa_unmap_teximage_pbo(ctx
, unpack
);
691 if (stImage
->pt
&& texImage
->Data
) {
692 st_texture_image_unmap(st
, stImage
);
693 texImage
->Data
= NULL
;
699 st_TexImage3D(struct gl_context
* ctx
,
700 GLenum target
, GLint level
,
701 GLint internalFormat
,
702 GLint width
, GLint height
, GLint depth
,
704 GLenum format
, GLenum type
, const void *pixels
,
705 const struct gl_pixelstore_attrib
*unpack
,
706 struct gl_texture_object
*texObj
,
707 struct gl_texture_image
*texImage
)
709 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
710 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
716 st_TexImage2D(struct gl_context
* ctx
,
717 GLenum target
, GLint level
,
718 GLint internalFormat
,
719 GLint width
, GLint height
, GLint border
,
720 GLenum format
, GLenum type
, const void *pixels
,
721 const struct gl_pixelstore_attrib
*unpack
,
722 struct gl_texture_object
*texObj
,
723 struct gl_texture_image
*texImage
)
725 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
726 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
731 st_TexImage1D(struct gl_context
* ctx
,
732 GLenum target
, GLint level
,
733 GLint internalFormat
,
734 GLint width
, GLint border
,
735 GLenum format
, GLenum type
, const void *pixels
,
736 const struct gl_pixelstore_attrib
*unpack
,
737 struct gl_texture_object
*texObj
,
738 struct gl_texture_image
*texImage
)
740 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
741 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
746 st_CompressedTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
747 GLint internalFormat
,
748 GLint width
, GLint height
, GLint border
,
749 GLsizei imageSize
, const GLvoid
*data
,
750 struct gl_texture_object
*texObj
,
751 struct gl_texture_image
*texImage
)
753 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
754 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
760 * glGetTexImage() helper: decompress a compressed texture by rendering
761 * a textured quad. Store the results in the user's buffer.
764 decompress_with_blit(struct gl_context
* ctx
, GLenum target
, GLint level
,
765 GLenum format
, GLenum type
, GLvoid
*pixels
,
766 struct gl_texture_object
*texObj
,
767 struct gl_texture_image
*texImage
)
769 struct st_context
*st
= st_context(ctx
);
770 struct pipe_context
*pipe
= st
->pipe
;
771 struct st_texture_image
*stImage
= st_texture_image(texImage
);
772 struct st_texture_object
*stObj
= st_texture_object(texObj
);
773 struct pipe_sampler_view
*src_view
=
774 st_get_texture_sampler_view(stObj
, pipe
);
775 const GLuint width
= texImage
->Width
;
776 const GLuint height
= texImage
->Height
;
777 struct pipe_surface
*dst_surface
;
778 struct pipe_resource
*dst_texture
;
779 struct pipe_transfer
*tex_xfer
;
780 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
781 PIPE_BIND_TRANSFER_READ
);
783 /* create temp / dest surface */
784 if (!util_create_rgba_surface(pipe
, width
, height
, bind
,
785 &dst_texture
, &dst_surface
)) {
786 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
787 "in decompress_with_blit()");
791 /* blit/render/decompress */
792 util_blit_pixels_tex(st
->blit
,
793 src_view
, /* pipe_resource (src) */
794 0, 0, /* src x0, y0 */
795 width
, height
, /* src x1, y1 */
796 dst_surface
, /* pipe_surface (dst) */
797 0, 0, /* dst x0, y0 */
798 width
, height
, /* dst x1, y1 */
800 PIPE_TEX_MIPFILTER_NEAREST
);
802 /* map the dst_surface so we can read from it */
803 tex_xfer
= pipe_get_transfer(st_context(ctx
)->pipe
,
806 0, 0, width
, height
);
808 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
810 /* copy/pack data into user buffer */
811 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
813 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
814 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
816 for (row
= 0; row
< height
; row
++) {
817 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
818 height
, format
, type
, row
, 0);
819 memcpy(dest
, map
, bytesPerRow
);
820 map
+= tex_xfer
->stride
;
822 pipe_transfer_unmap(pipe
, tex_xfer
);
825 /* format translation via floats */
827 enum pipe_format format
= util_format_linear(dst_texture
->format
);
828 for (row
= 0; row
< height
; row
++) {
829 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
830 GLfloat rgba
[4 * MAX_WIDTH
];
831 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
832 height
, format
, type
, row
, 0);
834 if (ST_DEBUG
& DEBUG_FALLBACK
)
835 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
837 /* get float[4] rgba row from surface */
838 pipe_get_tile_rgba_format(pipe
, tex_xfer
, 0, row
, width
, 1,
841 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
842 type
, dest
, &ctx
->Pack
, transferOps
);
846 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
848 pipe
->transfer_destroy(pipe
, tex_xfer
);
850 /* destroy the temp / dest surface */
851 util_destroy_rgba_surface(dst_texture
, dst_surface
);
857 * Need to map texture image into memory before copying image data,
861 st_get_tex_image(struct gl_context
* ctx
, GLenum target
, GLint level
,
862 GLenum format
, GLenum type
, GLvoid
* pixels
,
863 struct gl_texture_object
*texObj
,
864 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
866 struct st_context
*st
= st_context(ctx
);
867 struct st_texture_image
*stImage
= st_texture_image(texImage
);
868 const GLuint dstImageStride
=
869 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
875 util_format_is_s3tc(stImage
->pt
->format
) &&
877 /* Need to decompress the texture.
878 * We'll do this by rendering a textured quad.
879 * Note that we only expect RGBA formats (no Z/depth formats).
881 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
888 /* Image is stored in hardware format in a buffer managed by the
889 * kernel. Need to explicitly map and unmap it.
891 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
892 PIPE_TRANSFER_READ
, 0, 0,
894 stImage
->base
.Height
);
895 /* compute stride in texels from stride in bytes */
896 texImage
->RowStride
= stImage
->transfer
->stride
897 * util_format_get_blockwidth(stImage
->pt
->format
)
898 / util_format_get_blocksize(stImage
->pt
->format
);
901 /* Otherwise, the image should actually be stored in
902 * texImage->Data. This is pretty confusing for
903 * everybody, I'd much prefer to separate the two functions of
904 * texImage->Data - storage for texture images in main memory
905 * and access (ie mappings) of images. In other words, we'd
906 * create a new texImage->Map field and leave Data simply for
909 assert(texImage
->Data
);
912 depth
= texImage
->Depth
;
915 dest
= (GLubyte
*) pixels
;
917 _mesa_set_fetch_functions(texImage
, get_texture_dims(target
));
919 for (i
= 0; i
< depth
; i
++) {
920 if (compressed_dst
) {
921 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
925 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
929 if (stImage
->pt
&& i
+ 1 < depth
) {
930 /* unmap this slice */
931 st_texture_image_unmap(st
, stImage
);
932 /* map next slice of 3D texture */
933 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
934 PIPE_TRANSFER_READ
, 0, 0,
936 stImage
->base
.Height
);
937 dest
+= dstImageStride
;
941 texImage
->Depth
= depth
;
945 st_texture_image_unmap(st
, stImage
);
946 texImage
->Data
= NULL
;
952 st_GetTexImage(struct gl_context
* ctx
, GLenum target
, GLint level
,
953 GLenum format
, GLenum type
, GLvoid
* pixels
,
954 struct gl_texture_object
*texObj
,
955 struct gl_texture_image
*texImage
)
957 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
963 st_GetCompressedTexImage(struct gl_context
*ctx
, GLenum target
, GLint level
,
965 struct gl_texture_object
*texObj
,
966 struct gl_texture_image
*texImage
)
968 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
975 st_TexSubimage(struct gl_context
*ctx
, GLint dims
, GLenum target
, GLint level
,
976 GLint xoffset
, GLint yoffset
, GLint zoffset
,
977 GLint width
, GLint height
, GLint depth
,
978 GLenum format
, GLenum type
, const void *pixels
,
979 const struct gl_pixelstore_attrib
*packing
,
980 struct gl_texture_object
*texObj
,
981 struct gl_texture_image
*texImage
)
983 struct st_context
*st
= st_context(ctx
);
984 struct st_texture_image
*stImage
= st_texture_image(texImage
);
986 const GLuint srcImageStride
=
987 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
990 /* init to silence warning only: */
991 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
993 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
994 _mesa_lookup_enum_by_nr(target
),
995 level
, xoffset
, yoffset
, width
, height
);
998 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
999 type
, pixels
, packing
, "glTexSubImage2D");
1003 /* for a 1D array upload the image as a series of layer with height = 1 */
1004 if (target
== GL_TEXTURE_1D_ARRAY
) {
1009 /* Map buffer if necessary. Need to lock to prevent other contexts
1010 * from uploading the buffer under us.
1013 if (format
== GL_DEPTH_COMPONENT
&&
1014 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1015 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1017 transfer_usage
= PIPE_TRANSFER_WRITE
;
1019 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1025 if (!texImage
->Data
) {
1026 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1030 src
= (const GLubyte
*) pixels
;
1031 dstRowStride
= stImage
->transfer
->stride
;
1033 for (i
= 0; i
< depth
; i
++) {
1034 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1035 texImage
->TexFormat
,
1039 texImage
->ImageOffsets
,
1041 format
, type
, src
, packing
)) {
1042 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1045 if (stImage
->pt
&& i
+ 1 < depth
) {
1046 /* unmap this slice */
1047 st_texture_image_unmap(st
, stImage
);
1048 /* map next slice of 3D texture */
1049 texImage
->Data
= st_texture_image_map(st
, stImage
,
1054 src
+= srcImageStride
;
1059 _mesa_unmap_teximage_pbo(ctx
, packing
);
1061 if (stImage
->pt
&& texImage
->Data
) {
1062 st_texture_image_unmap(st
, stImage
);
1063 texImage
->Data
= NULL
;
1070 st_TexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1071 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1072 GLsizei width
, GLsizei height
, GLsizei depth
,
1073 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1074 const struct gl_pixelstore_attrib
*packing
,
1075 struct gl_texture_object
*texObj
,
1076 struct gl_texture_image
*texImage
)
1078 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1079 width
, height
, depth
, format
, type
,
1080 pixels
, packing
, texObj
, texImage
);
1085 st_TexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1086 GLint xoffset
, GLint yoffset
,
1087 GLsizei width
, GLsizei height
,
1088 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1089 const struct gl_pixelstore_attrib
*packing
,
1090 struct gl_texture_object
*texObj
,
1091 struct gl_texture_image
*texImage
)
1093 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1094 width
, height
, 1, format
, type
,
1095 pixels
, packing
, texObj
, texImage
);
1100 st_TexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1101 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1102 const GLvoid
* pixels
,
1103 const struct gl_pixelstore_attrib
*packing
,
1104 struct gl_texture_object
*texObj
,
1105 struct gl_texture_image
*texImage
)
1107 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1108 format
, type
, pixels
, packing
, texObj
, texImage
);
1113 st_CompressedTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1114 GLint xoffset
, GLsizei width
,
1116 GLsizei imageSize
, const GLvoid
*data
,
1117 struct gl_texture_object
*texObj
,
1118 struct gl_texture_image
*texImage
)
1125 st_CompressedTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1126 GLint xoffset
, GLint yoffset
,
1127 GLsizei width
, GLint height
,
1129 GLsizei imageSize
, const GLvoid
*data
,
1130 struct gl_texture_object
*texObj
,
1131 struct gl_texture_image
*texImage
)
1133 struct st_context
*st
= st_context(ctx
);
1134 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1138 enum pipe_format pformat
;
1141 pformat
= stImage
->pt
->format
;
1143 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1144 PIPE_TRANSFER_WRITE
,
1148 srcBlockStride
= util_format_get_stride(pformat
, width
);
1149 dstBlockStride
= stImage
->transfer
->stride
;
1151 assert(stImage
->pt
);
1152 /* TODO find good values for block and strides */
1153 /* TODO also adjust texImage->data for yoffset/xoffset */
1157 if (!texImage
->Data
) {
1158 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1162 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1163 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1165 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1166 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1167 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1168 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1169 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1173 st_texture_image_unmap(st
, stImage
);
1174 texImage
->Data
= NULL
;
1180 st_CompressedTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1181 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1182 GLsizei width
, GLint height
, GLint depth
,
1184 GLsizei imageSize
, const GLvoid
*data
,
1185 struct gl_texture_object
*texObj
,
1186 struct gl_texture_image
*texImage
)
1194 * Do a CopyTexSubImage operation using a read transfer from the source,
1195 * a write transfer to the destination and get_tile()/put_tile() to access
1196 * the pixels/texels.
1198 * Note: srcY=0=TOP of renderbuffer
1201 fallback_copy_texsubimage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1202 struct st_renderbuffer
*strb
,
1203 struct st_texture_image
*stImage
,
1205 GLint destX
, GLint destY
, GLint destZ
,
1206 GLint srcX
, GLint srcY
,
1207 GLsizei width
, GLsizei height
)
1209 struct st_context
*st
= st_context(ctx
);
1210 struct pipe_context
*pipe
= st
->pipe
;
1211 struct pipe_transfer
*src_trans
;
1213 enum pipe_transfer_usage transfer_usage
;
1215 if (ST_DEBUG
& DEBUG_FALLBACK
)
1216 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1218 assert(width
<= MAX_WIDTH
);
1220 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1221 srcY
= strb
->Base
.Height
- srcY
- height
;
1224 src_trans
= pipe_get_transfer(st_context(ctx
)->pipe
,
1231 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1232 baseFormat
== GL_DEPTH_STENCIL
) &&
1233 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1234 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1236 transfer_usage
= PIPE_TRANSFER_WRITE
;
1238 /* XXX this used to ignore destZ param */
1239 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
1240 destX
, destY
, width
, height
);
1242 if (baseFormat
== GL_DEPTH_COMPONENT
||
1243 baseFormat
== GL_DEPTH_STENCIL
) {
1244 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1245 ctx
->Pixel
.DepthBias
!= 0.0F
);
1248 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1249 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1258 /* To avoid a large temp memory allocation, do copy row by row */
1259 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1260 uint data
[MAX_WIDTH
];
1261 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1263 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1265 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1271 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1273 if (tempSrc
&& texDest
) {
1274 const GLint dims
= 2;
1275 const GLint dstRowStride
= stImage
->transfer
->stride
;
1276 struct gl_texture_image
*texImage
= &stImage
->base
;
1277 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1279 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1280 unpack
.Invert
= GL_TRUE
;
1283 /* get float/RGBA image from framebuffer */
1284 /* XXX this usually involves a lot of int/float conversion.
1285 * try to avoid that someday.
1287 pipe_get_tile_rgba_format(pipe
, src_trans
, 0, 0, width
, height
,
1288 util_format_linear(strb
->texture
->format
),
1291 /* Store into texture memory.
1292 * Note that this does some special things such as pixel transfer
1293 * ops and format conversion. In particular, if the dest tex format
1294 * is actually RGBA but the user created the texture as GL_RGB we
1295 * need to fill-in/override the alpha channel with 1.0.
1297 _mesa_texstore(ctx
, dims
,
1298 texImage
->_BaseFormat
,
1299 texImage
->TexFormat
,
1303 texImage
->ImageOffsets
,
1305 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1309 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1316 st_texture_image_unmap(st
, stImage
);
1317 pipe
->transfer_destroy(pipe
, src_trans
);
1323 * If the format of the src renderbuffer and the format of the dest
1324 * texture are compatible (in terms of blitting), return a TGSI writemask
1325 * to be used during the blit.
1326 * If the src/dest are incompatible, return 0.
1329 compatible_src_dst_formats(struct gl_context
*ctx
,
1330 const struct gl_renderbuffer
*src
,
1331 const struct gl_texture_image
*dst
)
1333 /* Get logical base formats for the src and dest.
1334 * That is, use the user-requested formats and not the actual, device-
1336 * For example, the user may have requested an A8 texture but the
1337 * driver may actually be using an RGBA texture format. When we
1338 * copy/blit to that texture, we only want to copy the Alpha channel
1339 * and not the RGB channels.
1341 * Similarly, when the src FBO was created an RGB format may have been
1342 * requested but the driver actually chose an RGBA format. In that case,
1343 * we don't want to copy the undefined Alpha channel to the dest texture
1344 * (it should be 1.0).
1346 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1347 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1350 * XXX when we have red-only and red/green renderbuffers we'll need
1351 * to add more cases here (or implement a general-purpose routine that
1352 * queries the existance of the R,G,B,A channels in the src and dest).
1354 if (srcFormat
== dstFormat
) {
1355 /* This is the same as matching_base_formats, which should
1356 * always pass, as it did previously.
1358 return TGSI_WRITEMASK_XYZW
;
1360 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1361 /* Make sure that A in the dest is 1. The actual src format
1362 * may be RGBA and have undefined A values.
1364 return TGSI_WRITEMASK_XYZ
;
1366 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1367 /* Make sure that A in the dest is 1. The actual dst format
1368 * may be RGBA and will need A=1 to provide proper alpha values
1369 * when sampled later.
1371 return TGSI_WRITEMASK_XYZ
;
1374 if (ST_DEBUG
& DEBUG_FALLBACK
)
1375 debug_printf("%s failed for src %s, dst %s\n",
1377 _mesa_lookup_enum_by_nr(srcFormat
),
1378 _mesa_lookup_enum_by_nr(dstFormat
));
1389 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1390 * Note that the region to copy has already been clipped so we know we
1391 * won't read from outside the source renderbuffer's bounds.
1393 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1396 st_copy_texsubimage(struct gl_context
*ctx
,
1397 GLenum target
, GLint level
,
1398 GLint destX
, GLint destY
, GLint destZ
,
1399 GLint srcX
, GLint srcY
,
1400 GLsizei width
, GLsizei height
)
1402 struct gl_texture_unit
*texUnit
=
1403 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1404 struct gl_texture_object
*texObj
=
1405 _mesa_select_tex_object(ctx
, texUnit
, target
);
1406 struct gl_texture_image
*texImage
=
1407 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1408 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1409 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1410 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1411 struct st_renderbuffer
*strb
;
1412 struct st_context
*st
= st_context(ctx
);
1413 struct pipe_context
*pipe
= st
->pipe
;
1414 struct pipe_screen
*screen
= pipe
->screen
;
1415 enum pipe_format dest_format
, src_format
;
1416 GLboolean use_fallback
= GL_TRUE
;
1417 GLboolean matching_base_formats
;
1418 GLuint format_writemask
, sample_count
;
1419 struct pipe_surface
*dest_surface
= NULL
;
1420 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1422 /* make sure finalize_textures has been called?
1424 if (0) st_validate_state(st
);
1426 /* determine if copying depth or color data */
1427 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1428 texBaseFormat
== GL_DEPTH_STENCIL
) {
1429 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1430 if (strb
->Base
.Wrapped
) {
1431 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
1435 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1436 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1439 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1440 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1444 sample_count
= strb
->surface
->texture
->nr_samples
;
1445 /* I believe this would be legal, presumably would need to do a resolve
1446 for color, and for depth/stencil spec says to just use one of the
1447 depth/stencil samples per pixel? Need some transfer clarifications. */
1448 assert(sample_count
< 2);
1474 if (width
< 0 || height
< 0)
1479 assert(strb
->surface
);
1480 assert(stImage
->pt
);
1482 src_format
= strb
->surface
->format
;
1483 dest_format
= stImage
->pt
->format
;
1486 * Determine if the src framebuffer and dest texture have the same
1487 * base format. We need this to detect a case such as the framebuffer
1488 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1489 * texture format stores RGBA we need to set A=1 (overriding the
1490 * framebuffer's alpha values). We can't do that with the blit or
1491 * textured-quad paths.
1493 matching_base_formats
=
1494 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1495 _mesa_get_format_base_format(texImage
->TexFormat
));
1496 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1498 if (ctx
->_ImageTransferState
== 0x0) {
1500 if (matching_base_formats
&&
1501 src_format
== dest_format
&&
1504 /* use surface_copy() / blit */
1505 struct pipe_box src_box
;
1506 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
1507 width
, height
, &src_box
);
1509 /* for resource_copy_region(), y=0=top, always */
1510 pipe
->resource_copy_region(pipe
,
1514 destX
, destY
, destZ
+ stImage
->face
,
1517 strb
->surface
->u
.tex
.level
,
1519 use_fallback
= GL_FALSE
;
1521 else if (format_writemask
&&
1522 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1523 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1524 screen
->is_format_supported(screen
, src_format
,
1525 PIPE_TEXTURE_2D
, sample_count
,
1526 PIPE_BIND_SAMPLER_VIEW
,
1528 screen
->is_format_supported(screen
, dest_format
,
1530 PIPE_BIND_RENDER_TARGET
,
1532 /* draw textured quad to do the copy */
1534 struct pipe_surface surf_tmpl
;
1535 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1536 surf_tmpl
.format
= stImage
->pt
->format
;
1537 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
1538 surf_tmpl
.u
.tex
.level
= stImage
->level
;
1539 surf_tmpl
.u
.tex
.first_layer
= stImage
->face
+ destZ
;
1540 surf_tmpl
.u
.tex
.last_layer
= stImage
->face
+ destZ
;
1542 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1546 srcY1
= strb
->Base
.Height
- srcY
- height
;
1547 srcY0
= srcY1
+ height
;
1551 srcY1
= srcY0
+ height
;
1554 util_blit_pixels_writemask(st
->blit
,
1556 strb
->surface
->u
.tex
.level
,
1558 srcX
+ width
, srcY1
,
1559 strb
->surface
->u
.tex
.first_layer
,
1562 destX
+ width
, destY
+ height
,
1563 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1565 use_fallback
= GL_FALSE
;
1569 pipe_surface_reference(&dest_surface
, NULL
);
1573 /* software fallback */
1574 fallback_copy_texsubimage(ctx
, target
, level
,
1575 strb
, stImage
, texBaseFormat
,
1576 destX
, destY
, destZ
,
1577 srcX
, srcY
, width
, height
);
1584 st_CopyTexImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1585 GLenum internalFormat
,
1586 GLint x
, GLint y
, GLsizei width
, GLint border
)
1588 struct gl_texture_unit
*texUnit
=
1589 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1590 struct gl_texture_object
*texObj
=
1591 _mesa_select_tex_object(ctx
, texUnit
, target
);
1592 struct gl_texture_image
*texImage
=
1593 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1595 /* Setup or redefine the texture object, texture and texture
1596 * image. Don't populate yet.
1598 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1600 GL_RGBA
, CHAN_TYPE
, NULL
,
1601 &ctx
->DefaultPacking
, texObj
, texImage
);
1603 st_copy_texsubimage(ctx
, target
, level
,
1604 0, 0, 0, /* destX,Y,Z */
1605 x
, y
, width
, 1); /* src X, Y, size */
1610 st_CopyTexImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1611 GLenum internalFormat
,
1612 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1615 struct gl_texture_unit
*texUnit
=
1616 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1617 struct gl_texture_object
*texObj
=
1618 _mesa_select_tex_object(ctx
, texUnit
, target
);
1619 struct gl_texture_image
*texImage
=
1620 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1622 /* Setup or redefine the texture object, texture and texture
1623 * image. Don't populate yet.
1625 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1626 width
, height
, border
,
1627 GL_RGBA
, CHAN_TYPE
, NULL
,
1628 &ctx
->DefaultPacking
, texObj
, texImage
);
1630 st_copy_texsubimage(ctx
, target
, level
,
1631 0, 0, 0, /* destX,Y,Z */
1632 x
, y
, width
, height
); /* src X, Y, size */
1637 st_CopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1638 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1640 const GLint yoffset
= 0, zoffset
= 0;
1641 const GLsizei height
= 1;
1642 st_copy_texsubimage(ctx
, target
, level
,
1643 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1644 x
, y
, width
, height
); /* src X, Y, size */
1649 st_CopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1650 GLint xoffset
, GLint yoffset
,
1651 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1653 const GLint zoffset
= 0;
1654 st_copy_texsubimage(ctx
, target
, level
,
1655 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1656 x
, y
, width
, height
); /* src X, Y, size */
1661 st_CopyTexSubImage3D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1662 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1663 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1665 st_copy_texsubimage(ctx
, target
, level
,
1666 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1667 x
, y
, width
, height
); /* src X, Y, size */
1672 * Copy image data from stImage into the texture object 'stObj' at level
1676 copy_image_data_to_texture(struct st_context
*st
,
1677 struct st_texture_object
*stObj
,
1679 struct st_texture_image
*stImage
)
1683 const struct gl_texture_image
*dstImage
=
1684 stObj
->base
.Image
[stImage
->face
][stImage
->level
];
1686 assert(dstImage
->Width
== stImage
->base
.Width
);
1687 assert(dstImage
->Height
== stImage
->base
.Height
);
1688 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1692 /* Copy potentially with the blitter:
1694 st_texture_image_copy(st
->pipe
,
1695 stObj
->pt
, dstLevel
, /* dest texture, level */
1696 stImage
->pt
, stImage
->level
, /* src texture, level */
1699 pipe_resource_reference(&stImage
->pt
, NULL
);
1701 else if (stImage
->base
.Data
) {
1702 st_texture_image_data(st
,
1707 stImage
->base
.RowStride
*
1708 util_format_get_blocksize(stObj
->pt
->format
),
1709 stImage
->base
.RowStride
*
1710 stImage
->base
.Height
*
1711 util_format_get_blocksize(stObj
->pt
->format
));
1712 _mesa_align_free(stImage
->base
.Data
);
1713 stImage
->base
.Data
= NULL
;
1716 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1721 * Called during state validation. When this function is finished,
1722 * the texture object should be ready for rendering.
1723 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1726 st_finalize_texture(struct gl_context
*ctx
,
1727 struct pipe_context
*pipe
,
1728 struct gl_texture_object
*tObj
)
1730 struct st_context
*st
= st_context(ctx
);
1731 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1732 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1734 struct st_texture_image
*firstImage
;
1735 enum pipe_format firstImageFormat
;
1736 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1738 if (stObj
->base
._Complete
) {
1739 /* The texture is complete and we know exactly how many mipmap levels
1740 * are present/needed. This is conditional because we may be called
1741 * from the st_generate_mipmap() function when the texture object is
1742 * incomplete. In that case, we'll have set stObj->lastLevel before
1745 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1746 stObj
->base
.MinFilter
== GL_NEAREST
)
1747 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1749 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1752 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1755 /* If both firstImage and stObj point to a texture which can contain
1756 * all active images, favour firstImage. Note that because of the
1757 * completeness requirement, we know that the image dimensions
1760 if (firstImage
->pt
&&
1761 firstImage
->pt
!= stObj
->pt
&&
1762 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1763 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1764 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1767 /* Find gallium format for the Mesa texture */
1768 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1770 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1772 GLuint width
, height
, depth
;
1773 if (!guess_base_level_size(stObj
->base
.Target
,
1774 firstImage
->base
.Width2
,
1775 firstImage
->base
.Height2
,
1776 firstImage
->base
.Depth2
,
1777 stObj
->base
.BaseLevel
,
1778 &width
, &height
, &depth
)) {
1779 width
= stObj
->width0
;
1780 height
= stObj
->height0
;
1781 depth
= stObj
->depth0
;
1783 /* convert GL dims to Gallium dims */
1784 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1785 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1788 /* If we already have a gallium texture, check that it matches the texture
1789 * object's format, target, size, num_levels, etc.
1792 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1793 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1794 stObj
->pt
->last_level
< stObj
->lastLevel
||
1795 stObj
->pt
->width0
!= ptWidth
||
1796 stObj
->pt
->height0
!= ptHeight
||
1797 stObj
->pt
->depth0
!= ptDepth
||
1798 stObj
->pt
->array_size
!= ptLayers
)
1800 /* The gallium texture does not match the Mesa texture so delete the
1801 * gallium texture now. We'll make a new one below.
1803 pipe_resource_reference(&stObj
->pt
, NULL
);
1804 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1805 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1809 /* May need to create a new gallium texture:
1812 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1814 stObj
->pt
= st_texture_create(st
,
1815 gl_target_to_pipe(stObj
->base
.Target
),
1825 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1830 /* Pull in any images not in the object's texture:
1832 for (face
= 0; face
< nr_faces
; face
++) {
1834 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1835 struct st_texture_image
*stImage
=
1836 st_texture_image(stObj
->base
.Image
[face
][level
]);
1838 /* Need to import images in main memory or held in other textures.
1840 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1841 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1851 * Returns pointer to a default/dummy texture.
1852 * This is typically used when the current shader has tex/sample instructions
1853 * but the user has not provided a (any) texture(s).
1855 struct gl_texture_object
*
1856 st_get_default_texture(struct st_context
*st
)
1858 if (!st
->default_texture
) {
1859 static const GLenum target
= GL_TEXTURE_2D
;
1860 GLubyte pixels
[16][16][4];
1861 struct gl_texture_object
*texObj
;
1862 struct gl_texture_image
*texImg
;
1865 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1866 * when attempting to sample incomplete textures.
1868 for (i
= 0; i
< 16; i
++) {
1869 for (j
= 0; j
< 16; j
++) {
1870 pixels
[i
][j
][0] = 0;
1871 pixels
[i
][j
][1] = 0;
1872 pixels
[i
][j
][2] = 0;
1873 pixels
[i
][j
][3] = 255;
1877 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1879 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1881 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1882 16, 16, 1, 0, /* w, h, d, border */
1883 GL_RGBA
, MESA_FORMAT_RGBA8888
);
1885 st_TexImage(st
->ctx
, 2, target
,
1886 0, GL_RGBA
, /* level, intformat */
1887 16, 16, 1, 0, /* w, h, d, border */
1888 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1889 &st
->ctx
->DefaultPacking
,
1893 texObj
->MinFilter
= GL_NEAREST
;
1894 texObj
->MagFilter
= GL_NEAREST
;
1895 texObj
->_Complete
= GL_TRUE
;
1897 st
->default_texture
= texObj
;
1899 return st
->default_texture
;
1904 st_init_texture_functions(struct dd_function_table
*functions
)
1906 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1907 functions
->TexImage1D
= st_TexImage1D
;
1908 functions
->TexImage2D
= st_TexImage2D
;
1909 functions
->TexImage3D
= st_TexImage3D
;
1910 functions
->TexSubImage1D
= st_TexSubImage1D
;
1911 functions
->TexSubImage2D
= st_TexSubImage2D
;
1912 functions
->TexSubImage3D
= st_TexSubImage3D
;
1913 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1914 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1915 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1916 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1917 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1918 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1919 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1920 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1921 functions
->GenerateMipmap
= st_generate_mipmap
;
1923 functions
->GetTexImage
= st_GetTexImage
;
1925 /* compressed texture functions */
1926 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1927 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1929 functions
->NewTextureObject
= st_NewTextureObject
;
1930 functions
->NewTextureImage
= st_NewTextureImage
;
1931 functions
->DeleteTexture
= st_DeleteTextureObject
;
1932 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1934 functions
->TextureMemCpy
= do_memcpy
;
1936 /* XXX Temporary until we can query pipe's texture sizes */
1937 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;