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
;
90 case GL_TEXTURE_BUFFER
:
99 /** called via ctx->Driver.NewTextureImage() */
100 static struct gl_texture_image
*
101 st_NewTextureImage(struct gl_context
* ctx
)
103 DBG("%s\n", __FUNCTION__
);
105 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
109 /** called via ctx->Driver.NewTextureObject() */
110 static struct gl_texture_object
*
111 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
113 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
115 DBG("%s\n", __FUNCTION__
);
116 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
121 /** called via ctx->Driver.DeleteTextureObject() */
123 st_DeleteTextureObject(struct gl_context
*ctx
,
124 struct gl_texture_object
*texObj
)
126 struct st_context
*st
= st_context(ctx
);
127 struct st_texture_object
*stObj
= st_texture_object(texObj
);
129 pipe_resource_reference(&stObj
->pt
, NULL
);
130 if (stObj
->sampler_view
) {
131 if (stObj
->sampler_view
->context
!= st
->pipe
) {
132 /* Take "ownership" of this texture sampler view by setting
133 * its context pointer to this context. This avoids potential
134 * crashes when the texture object is shared among contexts
135 * and the original/owner context has already been destroyed.
137 stObj
->sampler_view
->context
= st
->pipe
;
139 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
141 _mesa_delete_texture_object(ctx
, texObj
);
145 /** called via ctx->Driver.FreeTextureImageBuffer() */
147 st_FreeTextureImageBuffer(struct gl_context
* ctx
, struct gl_texture_image
*texImage
)
149 struct st_texture_image
*stImage
= st_texture_image(texImage
);
151 DBG("%s\n", __FUNCTION__
);
154 pipe_resource_reference(&stImage
->pt
, NULL
);
157 if (texImage
->Data
) {
158 _mesa_align_free(texImage
->Data
);
159 texImage
->Data
= NULL
;
165 * From linux kernel i386 header files, copes with odd sizes better
166 * than COPY_DWORDS would:
167 * XXX Put this in src/mesa/main/imports.h ???
169 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
171 __memcpy(void *to
, const void *from
, size_t n
)
174 __asm__
__volatile__("rep ; movsl\n\t"
178 "1:\ttestb $1,%b4\n\t"
180 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
181 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
186 #define __memcpy(a,b,c) memcpy(a,b,c)
191 * The system memcpy (at least on ubuntu 5.10) has problems copying
192 * to agp (writecombined) memory from a source which isn't 64-byte
193 * aligned - there is a 4x performance falloff.
195 * The x86 __memcpy is immune to this but is slightly slower
196 * (10%-ish) than the system memcpy.
198 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
199 * isn't much faster than x86_memcpy for agp copies.
201 * TODO: switch dynamically.
204 do_memcpy(void *dest
, const void *src
, size_t n
)
206 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
207 return __memcpy(dest
, src
, n
);
210 return memcpy(dest
, src
, n
);
215 * Return default texture resource binding bitmask for the given format.
218 default_bindings(struct st_context
*st
, enum pipe_format format
)
220 struct pipe_screen
*screen
= st
->pipe
->screen
;
221 const unsigned target
= PIPE_TEXTURE_2D
;
224 if (util_format_is_depth_or_stencil(format
))
225 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
227 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
229 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
233 format
= util_format_linear(format
);
235 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
238 return PIPE_BIND_SAMPLER_VIEW
;
243 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
245 get_texture_dims(GLenum target
)
249 case GL_TEXTURE_1D_ARRAY_EXT
:
250 case GL_TEXTURE_BUFFER
:
253 case GL_TEXTURE_CUBE_MAP_ARB
:
254 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
255 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
256 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
257 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
258 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
259 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
260 case GL_TEXTURE_RECTANGLE_NV
:
261 case GL_TEXTURE_2D_ARRAY_EXT
:
266 assert(0 && "invalid texture target in get_texture_dims()");
273 * Given the size of a mipmap image, try to compute the size of the level=0
276 * Note that this isn't always accurate for odd-sized, non-POW textures.
277 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
279 * \return GL_TRUE for success, GL_FALSE for failure
282 guess_base_level_size(GLenum target
,
283 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
284 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
286 const GLuint dims
= get_texture_dims(target
);
293 /* Depending on the image's size, we can't always make a guess here */
294 if ((dims
>= 1 && width
== 1) ||
295 (dims
>= 2 && height
== 1) ||
296 (dims
>= 3 && depth
== 1)) {
297 /* we can't determine the image size at level=0 */
301 /* grow the image size until we hit level = 0 */
322 * Try to allocate a pipe_resource object for the given st_texture_object.
324 * We use the given st_texture_image as a clue to determine the size of the
325 * mipmap image at level=0.
327 * \return GL_TRUE for success, GL_FALSE if out of memory.
330 guess_and_alloc_texture(struct st_context
*st
,
331 struct st_texture_object
*stObj
,
332 const struct st_texture_image
*stImage
)
334 GLuint lastLevel
, width
, height
, depth
;
336 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
337 enum pipe_format fmt
;
339 DBG("%s\n", __FUNCTION__
);
343 if (!guess_base_level_size(stObj
->base
.Target
,
344 stImage
->base
.Width2
,
345 stImage
->base
.Height2
,
346 stImage
->base
.Depth2
,
348 &width
, &height
, &depth
)) {
349 /* we can't determine the image size at level=0 */
350 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
351 /* this is not an out of memory error */
355 /* At this point, (width x height x depth) is the expected size of
356 * the level=0 mipmap image.
359 /* Guess a reasonable value for lastLevel. With OpenGL we have no
360 * idea how many mipmap levels will be in a texture until we start
361 * to render with it. Make an educated guess here but be prepared
362 * to re-allocating a texture buffer with space for more (or fewer)
363 * mipmap levels later.
365 if ((stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
366 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
367 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
368 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
369 !stObj
->base
.GenerateMipmap
&&
370 stImage
->base
.Level
== 0) {
371 /* only alloc space for a single mipmap level */
375 /* alloc space for a full mipmap */
376 GLuint l2width
= util_logbase2(width
);
377 GLuint l2height
= util_logbase2(height
);
378 GLuint l2depth
= util_logbase2(depth
);
379 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
382 /* Save the level=0 dimensions */
383 stObj
->width0
= width
;
384 stObj
->height0
= height
;
385 stObj
->depth0
= depth
;
387 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
389 bindings
= default_bindings(st
, fmt
);
391 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
392 width
, height
, depth
,
393 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
395 stObj
->pt
= st_texture_create(st
,
396 gl_target_to_pipe(stObj
->base
.Target
),
405 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
407 return stObj
->pt
!= NULL
;
412 * Adjust pixel unpack params and image dimensions to strip off the
414 * Gallium doesn't support texture borders. They've seldem been used
415 * and seldom been implemented correctly anyway.
416 * \param unpackNew returns the new pixel unpack parameters
419 strip_texture_border(GLint border
,
420 GLint
*width
, GLint
*height
, GLint
*depth
,
421 const struct gl_pixelstore_attrib
*unpack
,
422 struct gl_pixelstore_attrib
*unpackNew
)
424 assert(border
> 0); /* sanity check */
426 *unpackNew
= *unpack
;
428 if (unpackNew
->RowLength
== 0)
429 unpackNew
->RowLength
= *width
;
431 if (depth
&& unpackNew
->ImageHeight
== 0)
432 unpackNew
->ImageHeight
= *height
;
434 unpackNew
->SkipPixels
+= border
;
436 unpackNew
->SkipRows
+= border
;
438 unpackNew
->SkipImages
+= border
;
441 *width
= *width
- 2 * border
;
442 if (height
&& *height
>= 3)
443 *height
= *height
- 2 * border
;
444 if (depth
&& *depth
>= 3)
445 *depth
= *depth
- 2 * border
;
450 * Do glTexImage1/2/3D().
453 st_TexImage(struct gl_context
* ctx
,
455 GLenum target
, GLint level
,
456 GLint internalFormat
,
457 GLint width
, GLint height
, GLint depth
,
459 GLenum format
, GLenum type
, const void *pixels
,
460 const struct gl_pixelstore_attrib
*unpack
,
461 struct gl_texture_object
*texObj
,
462 struct gl_texture_image
*texImage
,
463 GLsizei imageSize
, GLboolean compressed_src
)
465 struct st_context
*st
= st_context(ctx
);
466 struct st_texture_object
*stObj
= st_texture_object(texObj
);
467 struct st_texture_image
*stImage
= st_texture_image(texImage
);
468 GLuint dstRowStride
= 0;
469 struct gl_pixelstore_attrib unpackNB
;
470 enum pipe_transfer_usage transfer_usage
= 0;
472 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
473 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
475 /* switch to "normal" */
476 if (stObj
->surface_based
) {
479 _mesa_clear_texture_object(ctx
, texObj
);
480 pipe_resource_reference(&stObj
->pt
, NULL
);
482 /* oops, need to init this image again */
483 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
484 internalFormat
, format
, type
);
486 _mesa_init_teximage_fields(ctx
, target
, texImage
,
487 width
, height
, depth
, border
,
488 internalFormat
, texFormat
);
490 stObj
->surface_based
= GL_FALSE
;
493 /* gallium does not support texture borders, strip it off */
495 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
497 texImage
->Width
= width
;
498 texImage
->Height
= height
;
499 texImage
->Depth
= depth
;
500 texImage
->Border
= 0;
504 assert(texImage
->Width
== width
);
505 assert(texImage
->Height
== height
);
506 assert(texImage
->Depth
== depth
);
509 stImage
->base
.Face
= _mesa_tex_target_to_face(target
);
510 stImage
->base
.Level
= level
;
512 _mesa_set_fetch_functions(texImage
, dims
);
514 /* Release the reference to a potentially orphaned buffer.
515 * Release any old malloced memory.
518 pipe_resource_reference(&stImage
->pt
, NULL
);
519 assert(!texImage
->Data
);
521 else if (texImage
->Data
) {
522 _mesa_align_free(texImage
->Data
);
526 * See if the new image is somehow incompatible with the existing
527 * mipmap. If so, free the old mipmap.
530 if (level
> (GLint
) stObj
->pt
->last_level
||
531 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
532 stImage
->base
.Face
, stImage
->base
.Level
)) {
534 pipe_resource_reference(&stObj
->pt
, NULL
);
536 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
540 if (width
== 0 || height
== 0 || depth
== 0) {
541 /* stop after freeing old image */
546 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
547 /* Probably out of memory.
548 * Try flushing any pending rendering, then retry.
551 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
552 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
558 assert(!stImage
->pt
);
560 /* Check if this texture image can live inside the texture object's buffer.
561 * If so, store the image there. Otherwise the image will temporarily live
565 st_texture_match_image(stObj
->pt
, &stImage
->base
,
566 stImage
->base
.Face
, stImage
->base
.Level
)) {
568 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
573 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
575 /* Pixel data may come from regular user memory or a PBO. For the later,
576 * do bounds checking and map the PBO to read pixels data from it.
578 * XXX we should try to use a GPU-accelerated path to copy the image data
579 * from the PBO to the texture.
581 if (compressed_src
) {
582 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
584 "glCompressedTexImage");
587 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
589 pixels
, unpack
, "glTexImage");
592 /* for a 1D array upload the image as a series of layer with height = 1 */
593 if (target
== GL_TEXTURE_1D_ARRAY
) {
599 * Prepare to store the texture data. Either map the gallium texture buffer
600 * memory or malloc space for it.
604 /* We've allocated texture resource, but have no pixel data - all done. */
608 /* Store the image in the gallium transfer object */
609 if (format
== GL_DEPTH_COMPONENT
&&
610 util_format_is_depth_and_stencil(stImage
->pt
->format
))
611 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
613 transfer_usage
= PIPE_TRANSFER_WRITE
;
615 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
616 transfer_usage
, 0, 0, width
, height
);
617 if(stImage
->transfer
)
618 dstRowStride
= stImage
->transfer
->stride
;
621 /* Allocate regular memory and store the image there temporarily. */
622 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
623 width
, height
, depth
);
624 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
626 texImage
->Data
= _mesa_align_malloc(imageSize
, 16);
629 if (!texImage
->Data
) {
630 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
635 /* We've allocated texture memory, but have no pixel data - all done. */
639 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
640 width
, height
, depth
, width
, dstRowStride
);
642 /* Copy user texture image into the texture buffer.
644 if (compressed_src
) {
645 const GLuint srcRowStride
=
646 _mesa_format_row_stride(texImage
->TexFormat
, width
);
647 if (dstRowStride
== srcRowStride
) {
648 memcpy(texImage
->Data
, pixels
, imageSize
);
651 char *dst
= texImage
->Data
;
652 const char *src
= pixels
;
653 GLuint i
, bw
, bh
, lines
;
654 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
655 lines
= (height
+ bh
- 1) / bh
;
657 for (i
= 0; i
< lines
; ++i
) {
658 memcpy(dst
, src
, srcRowStride
);
665 const GLuint srcImageStride
=
666 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
668 const GLubyte
*src
= (const GLubyte
*) pixels
;
670 for (i
= 0; i
< depth
; i
++) {
671 if (!_mesa_texstore(ctx
, dims
,
672 texImage
->_BaseFormat
,
675 0, 0, 0, /* dstX/Y/Zoffset */
677 texImage
->ImageOffsets
,
679 format
, type
, src
, unpack
)) {
680 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
683 if (stImage
->pt
&& i
+ 1 < depth
) {
684 /* unmap this slice */
685 st_texture_image_unmap(st
, stImage
);
686 /* map next slice of 3D texture */
687 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
688 transfer_usage
, 0, 0,
690 src
+= srcImageStride
;
696 _mesa_unmap_teximage_pbo(ctx
, unpack
);
698 if (stImage
->pt
&& texImage
->Data
) {
699 st_texture_image_unmap(st
, stImage
);
700 texImage
->Data
= NULL
;
706 st_TexImage3D(struct gl_context
* ctx
,
707 GLenum target
, GLint level
,
708 GLint internalFormat
,
709 GLint width
, GLint height
, GLint depth
,
711 GLenum format
, GLenum type
, const void *pixels
,
712 const struct gl_pixelstore_attrib
*unpack
,
713 struct gl_texture_object
*texObj
,
714 struct gl_texture_image
*texImage
)
716 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
717 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
723 st_TexImage2D(struct gl_context
* ctx
,
724 GLenum target
, GLint level
,
725 GLint internalFormat
,
726 GLint width
, GLint height
, GLint border
,
727 GLenum format
, GLenum type
, const void *pixels
,
728 const struct gl_pixelstore_attrib
*unpack
,
729 struct gl_texture_object
*texObj
,
730 struct gl_texture_image
*texImage
)
732 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
733 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
738 st_TexImage1D(struct gl_context
* ctx
,
739 GLenum target
, GLint level
,
740 GLint internalFormat
,
741 GLint width
, GLint border
,
742 GLenum format
, GLenum type
, const void *pixels
,
743 const struct gl_pixelstore_attrib
*unpack
,
744 struct gl_texture_object
*texObj
,
745 struct gl_texture_image
*texImage
)
747 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
748 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
753 st_CompressedTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
754 GLint internalFormat
,
755 GLint width
, GLint height
, GLint border
,
756 GLsizei imageSize
, const GLvoid
*data
,
757 struct gl_texture_object
*texObj
,
758 struct gl_texture_image
*texImage
)
760 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
761 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
767 * glGetTexImage() helper: decompress a compressed texture by rendering
768 * a textured quad. Store the results in the user's buffer.
771 decompress_with_blit(struct gl_context
* ctx
, GLenum target
, GLint level
,
772 GLenum format
, GLenum type
, GLvoid
*pixels
,
773 struct gl_texture_object
*texObj
,
774 struct gl_texture_image
*texImage
)
776 struct st_context
*st
= st_context(ctx
);
777 struct pipe_context
*pipe
= st
->pipe
;
778 struct st_texture_image
*stImage
= st_texture_image(texImage
);
779 struct st_texture_object
*stObj
= st_texture_object(texObj
);
780 struct pipe_sampler_view
*src_view
=
781 st_get_texture_sampler_view(stObj
, pipe
);
782 const GLuint width
= texImage
->Width
;
783 const GLuint height
= texImage
->Height
;
784 struct pipe_surface
*dst_surface
;
785 struct pipe_resource
*dst_texture
;
786 struct pipe_transfer
*tex_xfer
;
787 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
788 PIPE_BIND_TRANSFER_READ
);
790 /* create temp / dest surface */
791 if (!util_create_rgba_surface(pipe
, width
, height
, bind
,
792 &dst_texture
, &dst_surface
)) {
793 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
794 "in decompress_with_blit()");
798 /* Disable conditional rendering. */
799 if (st
->render_condition
) {
800 pipe
->render_condition(pipe
, NULL
, 0);
803 /* blit/render/decompress */
804 util_blit_pixels_tex(st
->blit
,
805 src_view
, /* pipe_resource (src) */
806 0, 0, /* src x0, y0 */
807 width
, height
, /* src x1, y1 */
808 dst_surface
, /* pipe_surface (dst) */
809 0, 0, /* dst x0, y0 */
810 width
, height
, /* dst x1, y1 */
812 PIPE_TEX_MIPFILTER_NEAREST
);
814 /* Restore conditional rendering state. */
815 if (st
->render_condition
) {
816 pipe
->render_condition(pipe
, st
->render_condition
,
820 /* map the dst_surface so we can read from it */
821 tex_xfer
= pipe_get_transfer(pipe
,
824 0, 0, width
, height
);
826 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
828 /* copy/pack data into user buffer */
829 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
831 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
832 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
834 for (row
= 0; row
< height
; row
++) {
835 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
836 height
, format
, type
, row
, 0);
837 memcpy(dest
, map
, bytesPerRow
);
838 map
+= tex_xfer
->stride
;
840 pipe_transfer_unmap(pipe
, tex_xfer
);
843 /* format translation via floats */
845 enum pipe_format pformat
= util_format_linear(dst_texture
->format
);
846 for (row
= 0; row
< height
; row
++) {
847 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
848 GLfloat rgba
[4 * MAX_WIDTH
];
849 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
850 height
, format
, type
, row
, 0);
852 if (ST_DEBUG
& DEBUG_FALLBACK
)
853 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
855 /* get float[4] rgba row from surface */
856 pipe_get_tile_rgba_format(pipe
, tex_xfer
, 0, row
, width
, 1,
859 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
860 type
, dest
, &ctx
->Pack
, transferOps
);
864 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
866 pipe
->transfer_destroy(pipe
, tex_xfer
);
868 /* destroy the temp / dest surface */
869 util_destroy_rgba_surface(dst_texture
, dst_surface
);
875 * Need to map texture image into memory before copying image data,
879 st_get_tex_image(struct gl_context
* ctx
, GLenum target
, GLint level
,
880 GLenum format
, GLenum type
, GLvoid
* pixels
,
881 struct gl_texture_object
*texObj
,
882 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
884 struct st_context
*st
= st_context(ctx
);
885 struct st_texture_image
*stImage
= st_texture_image(texImage
);
886 const GLuint dstImageStride
=
887 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
893 util_format_is_s3tc(stImage
->pt
->format
) &&
895 /* Need to decompress the texture.
896 * We'll do this by rendering a textured quad.
897 * Note that we only expect RGBA formats (no Z/depth formats).
899 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
906 /* Image is stored in hardware format in a buffer managed by the
907 * kernel. Need to explicitly map and unmap it.
909 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
910 PIPE_TRANSFER_READ
, 0, 0,
912 stImage
->base
.Height
);
913 /* compute stride in texels from stride in bytes */
914 texImage
->RowStride
= stImage
->transfer
->stride
915 * util_format_get_blockwidth(stImage
->pt
->format
)
916 / util_format_get_blocksize(stImage
->pt
->format
);
919 /* Otherwise, the image should actually be stored in
920 * texImage->Data. This is pretty confusing for
921 * everybody, I'd much prefer to separate the two functions of
922 * texImage->Data - storage for texture images in main memory
923 * and access (ie mappings) of images. In other words, we'd
924 * create a new texImage->Map field and leave Data simply for
927 assert(texImage
->Data
);
930 depth
= texImage
->Depth
;
933 dest
= (GLubyte
*) pixels
;
935 _mesa_set_fetch_functions(texImage
, get_texture_dims(target
));
937 for (i
= 0; i
< depth
; i
++) {
938 if (compressed_dst
) {
939 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
943 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
947 if (stImage
->pt
&& i
+ 1 < depth
) {
948 /* unmap this slice */
949 st_texture_image_unmap(st
, stImage
);
950 /* map next slice of 3D texture */
951 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
952 PIPE_TRANSFER_READ
, 0, 0,
954 stImage
->base
.Height
);
955 dest
+= dstImageStride
;
959 texImage
->Depth
= depth
;
963 st_texture_image_unmap(st
, stImage
);
964 texImage
->Data
= NULL
;
970 st_GetTexImage(struct gl_context
* ctx
, GLenum target
, GLint level
,
971 GLenum format
, GLenum type
, GLvoid
* pixels
,
972 struct gl_texture_object
*texObj
,
973 struct gl_texture_image
*texImage
)
975 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
981 st_GetCompressedTexImage(struct gl_context
*ctx
, GLenum target
, GLint level
,
983 struct gl_texture_object
*texObj
,
984 struct gl_texture_image
*texImage
)
986 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
993 st_TexSubimage(struct gl_context
*ctx
, GLint dims
, GLenum target
, GLint level
,
994 GLint xoffset
, GLint yoffset
, GLint zoffset
,
995 GLint width
, GLint height
, GLint depth
,
996 GLenum format
, GLenum type
, const void *pixels
,
997 const struct gl_pixelstore_attrib
*packing
,
998 struct gl_texture_object
*texObj
,
999 struct gl_texture_image
*texImage
)
1001 struct st_context
*st
= st_context(ctx
);
1002 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1003 GLuint dstRowStride
;
1004 const GLuint srcImageStride
=
1005 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1008 /* init to silence warning only: */
1009 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1011 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1012 _mesa_lookup_enum_by_nr(target
),
1013 level
, xoffset
, yoffset
, width
, height
);
1016 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1017 type
, pixels
, packing
, "glTexSubImage2D");
1021 /* for a 1D array upload the image as a series of layer with height = 1 */
1022 if (target
== GL_TEXTURE_1D_ARRAY
) {
1027 /* Map buffer if necessary. Need to lock to prevent other contexts
1028 * from uploading the buffer under us.
1031 if (format
== GL_DEPTH_COMPONENT
&&
1032 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1033 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1035 transfer_usage
= PIPE_TRANSFER_WRITE
;
1037 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1043 if (!texImage
->Data
) {
1044 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1048 src
= (const GLubyte
*) pixels
;
1049 dstRowStride
= stImage
->transfer
->stride
;
1051 for (i
= 0; i
< depth
; i
++) {
1052 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1053 texImage
->TexFormat
,
1057 texImage
->ImageOffsets
,
1059 format
, type
, src
, packing
)) {
1060 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1063 if (stImage
->pt
&& i
+ 1 < depth
) {
1064 /* unmap this slice */
1065 st_texture_image_unmap(st
, stImage
);
1066 /* map next slice of 3D texture */
1067 texImage
->Data
= st_texture_image_map(st
, stImage
,
1072 src
+= srcImageStride
;
1077 _mesa_unmap_teximage_pbo(ctx
, packing
);
1079 if (stImage
->pt
&& texImage
->Data
) {
1080 st_texture_image_unmap(st
, stImage
);
1081 texImage
->Data
= NULL
;
1088 st_TexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1089 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1090 GLsizei width
, GLsizei height
, GLsizei depth
,
1091 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1092 const struct gl_pixelstore_attrib
*packing
,
1093 struct gl_texture_object
*texObj
,
1094 struct gl_texture_image
*texImage
)
1096 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1097 width
, height
, depth
, format
, type
,
1098 pixels
, packing
, texObj
, texImage
);
1103 st_TexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1104 GLint xoffset
, GLint yoffset
,
1105 GLsizei width
, GLsizei height
,
1106 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1107 const struct gl_pixelstore_attrib
*packing
,
1108 struct gl_texture_object
*texObj
,
1109 struct gl_texture_image
*texImage
)
1111 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1112 width
, height
, 1, format
, type
,
1113 pixels
, packing
, texObj
, texImage
);
1118 st_TexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1119 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1120 const GLvoid
* pixels
,
1121 const struct gl_pixelstore_attrib
*packing
,
1122 struct gl_texture_object
*texObj
,
1123 struct gl_texture_image
*texImage
)
1125 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1126 format
, type
, pixels
, packing
, texObj
, texImage
);
1131 st_CompressedTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1132 GLint xoffset
, GLsizei width
,
1134 GLsizei imageSize
, const GLvoid
*data
,
1135 struct gl_texture_object
*texObj
,
1136 struct gl_texture_image
*texImage
)
1143 st_CompressedTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1144 GLint xoffset
, GLint yoffset
,
1145 GLsizei width
, GLint height
,
1147 GLsizei imageSize
, const GLvoid
*data
,
1148 struct gl_texture_object
*texObj
,
1149 struct gl_texture_image
*texImage
)
1151 struct st_context
*st
= st_context(ctx
);
1152 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1156 enum pipe_format pformat
;
1159 pformat
= stImage
->pt
->format
;
1161 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1162 PIPE_TRANSFER_WRITE
,
1166 srcBlockStride
= util_format_get_stride(pformat
, width
);
1167 dstBlockStride
= stImage
->transfer
->stride
;
1169 assert(stImage
->pt
);
1170 /* TODO find good values for block and strides */
1171 /* TODO also adjust texImage->data for yoffset/xoffset */
1175 if (!texImage
->Data
) {
1176 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1180 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1181 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1183 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1184 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1185 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1186 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1187 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1191 st_texture_image_unmap(st
, stImage
);
1192 texImage
->Data
= NULL
;
1198 st_CompressedTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1199 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1200 GLsizei width
, GLint height
, GLint depth
,
1202 GLsizei imageSize
, const GLvoid
*data
,
1203 struct gl_texture_object
*texObj
,
1204 struct gl_texture_image
*texImage
)
1212 * Do a CopyTexSubImage operation using a read transfer from the source,
1213 * a write transfer to the destination and get_tile()/put_tile() to access
1214 * the pixels/texels.
1216 * Note: srcY=0=TOP of renderbuffer
1219 fallback_copy_texsubimage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1220 struct st_renderbuffer
*strb
,
1221 struct st_texture_image
*stImage
,
1223 GLint destX
, GLint destY
, GLint destZ
,
1224 GLint srcX
, GLint srcY
,
1225 GLsizei width
, GLsizei height
)
1227 struct st_context
*st
= st_context(ctx
);
1228 struct pipe_context
*pipe
= st
->pipe
;
1229 struct pipe_transfer
*src_trans
;
1231 enum pipe_transfer_usage transfer_usage
;
1233 if (ST_DEBUG
& DEBUG_FALLBACK
)
1234 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1236 assert(width
<= MAX_WIDTH
);
1238 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1239 srcY
= strb
->Base
.Height
- srcY
- height
;
1242 src_trans
= pipe_get_transfer(pipe
,
1245 strb
->rtt_face
+ strb
->rtt_slice
,
1250 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1251 baseFormat
== GL_DEPTH_STENCIL
) &&
1252 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1253 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1255 transfer_usage
= PIPE_TRANSFER_WRITE
;
1257 /* XXX this used to ignore destZ param */
1258 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
1259 destX
, destY
, width
, height
);
1261 if (baseFormat
== GL_DEPTH_COMPONENT
||
1262 baseFormat
== GL_DEPTH_STENCIL
) {
1263 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1264 ctx
->Pixel
.DepthBias
!= 0.0F
);
1267 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1268 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1277 /* To avoid a large temp memory allocation, do copy row by row */
1278 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1279 uint data
[MAX_WIDTH
];
1280 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1282 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1284 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1290 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1292 if (tempSrc
&& texDest
) {
1293 const GLint dims
= 2;
1294 const GLint dstRowStride
= stImage
->transfer
->stride
;
1295 struct gl_texture_image
*texImage
= &stImage
->base
;
1296 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1298 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1299 unpack
.Invert
= GL_TRUE
;
1302 /* get float/RGBA image from framebuffer */
1303 /* XXX this usually involves a lot of int/float conversion.
1304 * try to avoid that someday.
1306 pipe_get_tile_rgba_format(pipe
, src_trans
, 0, 0, width
, height
,
1307 util_format_linear(strb
->texture
->format
),
1310 /* Store into texture memory.
1311 * Note that this does some special things such as pixel transfer
1312 * ops and format conversion. In particular, if the dest tex format
1313 * is actually RGBA but the user created the texture as GL_RGB we
1314 * need to fill-in/override the alpha channel with 1.0.
1316 _mesa_texstore(ctx
, dims
,
1317 texImage
->_BaseFormat
,
1318 texImage
->TexFormat
,
1322 texImage
->ImageOffsets
,
1324 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1328 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1335 st_texture_image_unmap(st
, stImage
);
1336 pipe
->transfer_destroy(pipe
, src_trans
);
1342 * If the format of the src renderbuffer and the format of the dest
1343 * texture are compatible (in terms of blitting), return a TGSI writemask
1344 * to be used during the blit.
1345 * If the src/dest are incompatible, return 0.
1348 compatible_src_dst_formats(struct gl_context
*ctx
,
1349 const struct gl_renderbuffer
*src
,
1350 const struct gl_texture_image
*dst
)
1352 /* Get logical base formats for the src and dest.
1353 * That is, use the user-requested formats and not the actual, device-
1355 * For example, the user may have requested an A8 texture but the
1356 * driver may actually be using an RGBA texture format. When we
1357 * copy/blit to that texture, we only want to copy the Alpha channel
1358 * and not the RGB channels.
1360 * Similarly, when the src FBO was created an RGB format may have been
1361 * requested but the driver actually chose an RGBA format. In that case,
1362 * we don't want to copy the undefined Alpha channel to the dest texture
1363 * (it should be 1.0).
1365 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1366 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1369 * XXX when we have red-only and red/green renderbuffers we'll need
1370 * to add more cases here (or implement a general-purpose routine that
1371 * queries the existance of the R,G,B,A channels in the src and dest).
1373 if (srcFormat
== dstFormat
) {
1374 /* This is the same as matching_base_formats, which should
1375 * always pass, as it did previously.
1377 return TGSI_WRITEMASK_XYZW
;
1379 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1380 /* Make sure that A in the dest is 1. The actual src format
1381 * may be RGBA and have undefined A values.
1383 return TGSI_WRITEMASK_XYZ
;
1385 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1386 /* Make sure that A in the dest is 1. The actual dst format
1387 * may be RGBA and will need A=1 to provide proper alpha values
1388 * when sampled later.
1390 return TGSI_WRITEMASK_XYZ
;
1393 if (ST_DEBUG
& DEBUG_FALLBACK
)
1394 debug_printf("%s failed for src %s, dst %s\n",
1396 _mesa_lookup_enum_by_nr(srcFormat
),
1397 _mesa_lookup_enum_by_nr(dstFormat
));
1408 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1409 * Note that the region to copy has already been clipped so we know we
1410 * won't read from outside the source renderbuffer's bounds.
1412 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1415 st_copy_texsubimage(struct gl_context
*ctx
,
1416 GLenum target
, GLint level
,
1417 GLint destX
, GLint destY
, GLint destZ
,
1418 GLint srcX
, GLint srcY
,
1419 GLsizei width
, GLsizei height
)
1421 struct gl_texture_unit
*texUnit
=
1422 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1423 struct gl_texture_object
*texObj
=
1424 _mesa_select_tex_object(ctx
, texUnit
, target
);
1425 struct gl_texture_image
*texImage
=
1426 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1427 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1428 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1429 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1430 struct st_renderbuffer
*strb
;
1431 struct st_context
*st
= st_context(ctx
);
1432 struct pipe_context
*pipe
= st
->pipe
;
1433 struct pipe_screen
*screen
= pipe
->screen
;
1434 enum pipe_format dest_format
, src_format
;
1435 GLboolean use_fallback
= GL_TRUE
;
1436 GLboolean matching_base_formats
;
1437 GLuint format_writemask
, sample_count
;
1438 struct pipe_surface
*dest_surface
= NULL
;
1439 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1441 /* make sure finalize_textures has been called?
1443 if (0) st_validate_state(st
);
1445 /* determine if copying depth or color data */
1446 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1447 texBaseFormat
== GL_DEPTH_STENCIL
) {
1448 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1449 if (strb
->Base
.Wrapped
) {
1450 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
1454 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1455 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1458 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1459 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1463 sample_count
= strb
->surface
->texture
->nr_samples
;
1464 /* I believe this would be legal, presumably would need to do a resolve
1465 for color, and for depth/stencil spec says to just use one of the
1466 depth/stencil samples per pixel? Need some transfer clarifications. */
1467 assert(sample_count
< 2);
1470 assert(strb
->surface
);
1471 assert(stImage
->pt
);
1473 src_format
= strb
->surface
->format
;
1474 dest_format
= stImage
->pt
->format
;
1477 * Determine if the src framebuffer and dest texture have the same
1478 * base format. We need this to detect a case such as the framebuffer
1479 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1480 * texture format stores RGBA we need to set A=1 (overriding the
1481 * framebuffer's alpha values). We can't do that with the blit or
1482 * textured-quad paths.
1484 matching_base_formats
=
1485 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1486 _mesa_get_format_base_format(texImage
->TexFormat
));
1487 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1489 if (ctx
->_ImageTransferState
== 0x0) {
1491 if (matching_base_formats
&&
1492 src_format
== dest_format
&&
1495 /* use surface_copy() / blit */
1496 struct pipe_box src_box
;
1497 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
1498 width
, height
, &src_box
);
1500 /* for resource_copy_region(), y=0=top, always */
1501 pipe
->resource_copy_region(pipe
,
1504 stImage
->base
.Level
,
1505 destX
, destY
, destZ
+ stImage
->base
.Face
,
1508 strb
->surface
->u
.tex
.level
,
1510 use_fallback
= GL_FALSE
;
1512 else if (format_writemask
&&
1513 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1514 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1515 screen
->is_format_supported(screen
, src_format
,
1516 PIPE_TEXTURE_2D
, sample_count
,
1517 PIPE_BIND_SAMPLER_VIEW
) &&
1518 screen
->is_format_supported(screen
, dest_format
,
1520 PIPE_BIND_RENDER_TARGET
)) {
1521 /* draw textured quad to do the copy */
1523 struct pipe_surface surf_tmpl
;
1524 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1525 surf_tmpl
.format
= util_format_linear(stImage
->pt
->format
);
1526 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
1527 surf_tmpl
.u
.tex
.level
= stImage
->base
.Level
;
1528 surf_tmpl
.u
.tex
.first_layer
= stImage
->base
.Face
+ destZ
;
1529 surf_tmpl
.u
.tex
.last_layer
= stImage
->base
.Face
+ destZ
;
1531 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1535 srcY1
= strb
->Base
.Height
- srcY
- height
;
1536 srcY0
= srcY1
+ height
;
1540 srcY1
= srcY0
+ height
;
1543 /* Disable conditional rendering. */
1544 if (st
->render_condition
) {
1545 pipe
->render_condition(pipe
, NULL
, 0);
1548 util_blit_pixels_writemask(st
->blit
,
1550 strb
->surface
->u
.tex
.level
,
1552 srcX
+ width
, srcY1
,
1553 strb
->surface
->u
.tex
.first_layer
,
1556 destX
+ width
, destY
+ height
,
1557 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1560 /* Restore conditional rendering state. */
1561 if (st
->render_condition
) {
1562 pipe
->render_condition(pipe
, st
->render_condition
,
1563 st
->condition_mode
);
1566 use_fallback
= GL_FALSE
;
1570 pipe_surface_reference(&dest_surface
, NULL
);
1574 /* software fallback */
1575 fallback_copy_texsubimage(ctx
, target
, level
,
1576 strb
, stImage
, texBaseFormat
,
1577 destX
, destY
, destZ
,
1578 srcX
, srcY
, width
, height
);
1585 st_CopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1586 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1588 const GLint yoffset
= 0, zoffset
= 0;
1589 const GLsizei height
= 1;
1590 st_copy_texsubimage(ctx
, target
, level
,
1591 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1592 x
, y
, width
, height
); /* src X, Y, size */
1597 st_CopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1598 GLint xoffset
, GLint yoffset
,
1599 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1601 const GLint zoffset
= 0;
1602 st_copy_texsubimage(ctx
, target
, level
,
1603 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1604 x
, y
, width
, height
); /* src X, Y, size */
1609 st_CopyTexSubImage3D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1610 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1611 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1613 st_copy_texsubimage(ctx
, target
, level
,
1614 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1615 x
, y
, width
, height
); /* src X, Y, size */
1620 * Copy image data from stImage into the texture object 'stObj' at level
1624 copy_image_data_to_texture(struct st_context
*st
,
1625 struct st_texture_object
*stObj
,
1627 struct st_texture_image
*stImage
)
1631 const struct gl_texture_image
*dstImage
=
1632 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
1634 assert(dstImage
->Width
== stImage
->base
.Width
);
1635 assert(dstImage
->Height
== stImage
->base
.Height
);
1636 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1640 /* Copy potentially with the blitter:
1642 st_texture_image_copy(st
->pipe
,
1643 stObj
->pt
, dstLevel
, /* dest texture, level */
1644 stImage
->pt
, stImage
->base
.Level
, /* src texture, level */
1645 stImage
->base
.Face
);
1647 pipe_resource_reference(&stImage
->pt
, NULL
);
1649 else if (stImage
->base
.Data
) {
1650 st_texture_image_data(st
,
1655 stImage
->base
.RowStride
*
1656 util_format_get_blocksize(stObj
->pt
->format
),
1657 stImage
->base
.RowStride
*
1658 stImage
->base
.Height
*
1659 util_format_get_blocksize(stObj
->pt
->format
));
1660 _mesa_align_free(stImage
->base
.Data
);
1661 stImage
->base
.Data
= NULL
;
1664 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1669 * Called during state validation. When this function is finished,
1670 * the texture object should be ready for rendering.
1671 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1674 st_finalize_texture(struct gl_context
*ctx
,
1675 struct pipe_context
*pipe
,
1676 struct gl_texture_object
*tObj
)
1678 struct st_context
*st
= st_context(ctx
);
1679 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1680 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1682 struct st_texture_image
*firstImage
;
1683 enum pipe_format firstImageFormat
;
1684 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1686 if (stObj
->base
._Complete
) {
1687 /* The texture is complete and we know exactly how many mipmap levels
1688 * are present/needed. This is conditional because we may be called
1689 * from the st_generate_mipmap() function when the texture object is
1690 * incomplete. In that case, we'll have set stObj->lastLevel before
1693 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1694 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1695 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1697 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1700 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1703 /* If both firstImage and stObj point to a texture which can contain
1704 * all active images, favour firstImage. Note that because of the
1705 * completeness requirement, we know that the image dimensions
1708 if (firstImage
->pt
&&
1709 firstImage
->pt
!= stObj
->pt
&&
1710 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1711 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1712 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1715 /* Find gallium format for the Mesa texture */
1716 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1718 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1720 GLuint width
, height
, depth
;
1721 if (!guess_base_level_size(stObj
->base
.Target
,
1722 firstImage
->base
.Width2
,
1723 firstImage
->base
.Height2
,
1724 firstImage
->base
.Depth2
,
1725 stObj
->base
.BaseLevel
,
1726 &width
, &height
, &depth
)) {
1727 width
= stObj
->width0
;
1728 height
= stObj
->height0
;
1729 depth
= stObj
->depth0
;
1731 /* convert GL dims to Gallium dims */
1732 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1733 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1736 /* If we already have a gallium texture, check that it matches the texture
1737 * object's format, target, size, num_levels, etc.
1740 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1741 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1742 stObj
->pt
->last_level
< stObj
->lastLevel
||
1743 stObj
->pt
->width0
!= ptWidth
||
1744 stObj
->pt
->height0
!= ptHeight
||
1745 stObj
->pt
->depth0
!= ptDepth
||
1746 stObj
->pt
->array_size
!= ptLayers
)
1748 /* The gallium texture does not match the Mesa texture so delete the
1749 * gallium texture now. We'll make a new one below.
1751 pipe_resource_reference(&stObj
->pt
, NULL
);
1752 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1753 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1757 /* May need to create a new gallium texture:
1760 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1762 stObj
->pt
= st_texture_create(st
,
1763 gl_target_to_pipe(stObj
->base
.Target
),
1773 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1778 /* Pull in any images not in the object's texture:
1780 for (face
= 0; face
< nr_faces
; face
++) {
1782 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1783 struct st_texture_image
*stImage
=
1784 st_texture_image(stObj
->base
.Image
[face
][level
]);
1786 /* Need to import images in main memory or held in other textures.
1788 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1789 if (level
== 0 || (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1790 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1791 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
1792 /* src image fits expected dest mipmap level size */
1793 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1804 * Returns pointer to a default/dummy texture.
1805 * This is typically used when the current shader has tex/sample instructions
1806 * but the user has not provided a (any) texture(s).
1808 struct gl_texture_object
*
1809 st_get_default_texture(struct st_context
*st
)
1811 if (!st
->default_texture
) {
1812 static const GLenum target
= GL_TEXTURE_2D
;
1813 GLubyte pixels
[16][16][4];
1814 struct gl_texture_object
*texObj
;
1815 struct gl_texture_image
*texImg
;
1818 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1819 * when attempting to sample incomplete textures.
1821 for (i
= 0; i
< 16; i
++) {
1822 for (j
= 0; j
< 16; j
++) {
1823 pixels
[i
][j
][0] = 0;
1824 pixels
[i
][j
][1] = 0;
1825 pixels
[i
][j
][2] = 0;
1826 pixels
[i
][j
][3] = 255;
1830 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1832 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1834 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1835 16, 16, 1, 0, /* w, h, d, border */
1836 GL_RGBA
, MESA_FORMAT_RGBA8888
);
1838 st_TexImage(st
->ctx
, 2, target
,
1839 0, GL_RGBA
, /* level, intformat */
1840 16, 16, 1, 0, /* w, h, d, border */
1841 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1842 &st
->ctx
->DefaultPacking
,
1846 texObj
->Sampler
.MinFilter
= GL_NEAREST
;
1847 texObj
->Sampler
.MagFilter
= GL_NEAREST
;
1848 texObj
->_Complete
= GL_TRUE
;
1850 st
->default_texture
= texObj
;
1852 return st
->default_texture
;
1857 st_init_texture_functions(struct dd_function_table
*functions
)
1859 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1860 functions
->TexImage1D
= st_TexImage1D
;
1861 functions
->TexImage2D
= st_TexImage2D
;
1862 functions
->TexImage3D
= st_TexImage3D
;
1863 functions
->TexSubImage1D
= st_TexSubImage1D
;
1864 functions
->TexSubImage2D
= st_TexSubImage2D
;
1865 functions
->TexSubImage3D
= st_TexSubImage3D
;
1866 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1867 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1868 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1869 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1870 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1871 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1872 functions
->GenerateMipmap
= st_generate_mipmap
;
1874 functions
->GetTexImage
= st_GetTexImage
;
1876 /* compressed texture functions */
1877 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1878 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1880 functions
->NewTextureObject
= st_NewTextureObject
;
1881 functions
->NewTextureImage
= st_NewTextureImage
;
1882 functions
->DeleteTexture
= st_DeleteTextureObject
;
1883 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
1885 functions
->TextureMemCpy
= do_memcpy
;
1887 /* XXX Temporary until we can query pipe's texture sizes */
1888 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;