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.FreeTexImageData() */
147 st_FreeTextureImageData(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
->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
->face
= _mesa_tex_target_to_face(target
);
510 stImage
->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
->face
, stImage
->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
->face
, stImage
->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 /* blit/render/decompress */
799 util_blit_pixels_tex(st
->blit
,
800 src_view
, /* pipe_resource (src) */
801 0, 0, /* src x0, y0 */
802 width
, height
, /* src x1, y1 */
803 dst_surface
, /* pipe_surface (dst) */
804 0, 0, /* dst x0, y0 */
805 width
, height
, /* dst x1, y1 */
807 PIPE_TEX_MIPFILTER_NEAREST
);
809 /* map the dst_surface so we can read from it */
810 tex_xfer
= pipe_get_transfer(st_context(ctx
)->pipe
,
813 0, 0, width
, height
);
815 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
817 /* copy/pack data into user buffer */
818 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
820 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
821 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
823 for (row
= 0; row
< height
; row
++) {
824 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
825 height
, format
, type
, row
, 0);
826 memcpy(dest
, map
, bytesPerRow
);
827 map
+= tex_xfer
->stride
;
829 pipe_transfer_unmap(pipe
, tex_xfer
);
832 /* format translation via floats */
834 enum pipe_format format
= util_format_linear(dst_texture
->format
);
835 for (row
= 0; row
< height
; row
++) {
836 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
837 GLfloat rgba
[4 * MAX_WIDTH
];
838 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
839 height
, format
, type
, row
, 0);
841 if (ST_DEBUG
& DEBUG_FALLBACK
)
842 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
844 /* get float[4] rgba row from surface */
845 pipe_get_tile_rgba_format(pipe
, tex_xfer
, 0, row
, width
, 1,
848 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
849 type
, dest
, &ctx
->Pack
, transferOps
);
853 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
855 pipe
->transfer_destroy(pipe
, tex_xfer
);
857 /* destroy the temp / dest surface */
858 util_destroy_rgba_surface(dst_texture
, dst_surface
);
864 * Need to map texture image into memory before copying image data,
868 st_get_tex_image(struct gl_context
* ctx
, GLenum target
, GLint level
,
869 GLenum format
, GLenum type
, GLvoid
* pixels
,
870 struct gl_texture_object
*texObj
,
871 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
873 struct st_context
*st
= st_context(ctx
);
874 struct st_texture_image
*stImage
= st_texture_image(texImage
);
875 const GLuint dstImageStride
=
876 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
882 util_format_is_s3tc(stImage
->pt
->format
) &&
884 /* Need to decompress the texture.
885 * We'll do this by rendering a textured quad.
886 * Note that we only expect RGBA formats (no Z/depth formats).
888 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
895 /* Image is stored in hardware format in a buffer managed by the
896 * kernel. Need to explicitly map and unmap it.
898 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
899 PIPE_TRANSFER_READ
, 0, 0,
901 stImage
->base
.Height
);
902 /* compute stride in texels from stride in bytes */
903 texImage
->RowStride
= stImage
->transfer
->stride
904 * util_format_get_blockwidth(stImage
->pt
->format
)
905 / util_format_get_blocksize(stImage
->pt
->format
);
908 /* Otherwise, the image should actually be stored in
909 * texImage->Data. This is pretty confusing for
910 * everybody, I'd much prefer to separate the two functions of
911 * texImage->Data - storage for texture images in main memory
912 * and access (ie mappings) of images. In other words, we'd
913 * create a new texImage->Map field and leave Data simply for
916 assert(texImage
->Data
);
919 depth
= texImage
->Depth
;
922 dest
= (GLubyte
*) pixels
;
924 _mesa_set_fetch_functions(texImage
, get_texture_dims(target
));
926 for (i
= 0; i
< depth
; i
++) {
927 if (compressed_dst
) {
928 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
932 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
936 if (stImage
->pt
&& i
+ 1 < depth
) {
937 /* unmap this slice */
938 st_texture_image_unmap(st
, stImage
);
939 /* map next slice of 3D texture */
940 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
941 PIPE_TRANSFER_READ
, 0, 0,
943 stImage
->base
.Height
);
944 dest
+= dstImageStride
;
948 texImage
->Depth
= depth
;
952 st_texture_image_unmap(st
, stImage
);
953 texImage
->Data
= NULL
;
959 st_GetTexImage(struct gl_context
* ctx
, GLenum target
, GLint level
,
960 GLenum format
, GLenum type
, GLvoid
* pixels
,
961 struct gl_texture_object
*texObj
,
962 struct gl_texture_image
*texImage
)
964 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
970 st_GetCompressedTexImage(struct gl_context
*ctx
, GLenum target
, GLint level
,
972 struct gl_texture_object
*texObj
,
973 struct gl_texture_image
*texImage
)
975 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
982 st_TexSubimage(struct gl_context
*ctx
, GLint dims
, GLenum target
, GLint level
,
983 GLint xoffset
, GLint yoffset
, GLint zoffset
,
984 GLint width
, GLint height
, GLint depth
,
985 GLenum format
, GLenum type
, const void *pixels
,
986 const struct gl_pixelstore_attrib
*packing
,
987 struct gl_texture_object
*texObj
,
988 struct gl_texture_image
*texImage
)
990 struct st_context
*st
= st_context(ctx
);
991 struct st_texture_image
*stImage
= st_texture_image(texImage
);
993 const GLuint srcImageStride
=
994 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
997 /* init to silence warning only: */
998 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1000 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1001 _mesa_lookup_enum_by_nr(target
),
1002 level
, xoffset
, yoffset
, width
, height
);
1005 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1006 type
, pixels
, packing
, "glTexSubImage2D");
1010 /* for a 1D array upload the image as a series of layer with height = 1 */
1011 if (target
== GL_TEXTURE_1D_ARRAY
) {
1016 /* Map buffer if necessary. Need to lock to prevent other contexts
1017 * from uploading the buffer under us.
1020 if (format
== GL_DEPTH_COMPONENT
&&
1021 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1022 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1024 transfer_usage
= PIPE_TRANSFER_WRITE
;
1026 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1032 if (!texImage
->Data
) {
1033 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1037 src
= (const GLubyte
*) pixels
;
1038 dstRowStride
= stImage
->transfer
->stride
;
1040 for (i
= 0; i
< depth
; i
++) {
1041 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1042 texImage
->TexFormat
,
1046 texImage
->ImageOffsets
,
1048 format
, type
, src
, packing
)) {
1049 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1052 if (stImage
->pt
&& i
+ 1 < depth
) {
1053 /* unmap this slice */
1054 st_texture_image_unmap(st
, stImage
);
1055 /* map next slice of 3D texture */
1056 texImage
->Data
= st_texture_image_map(st
, stImage
,
1061 src
+= srcImageStride
;
1066 _mesa_unmap_teximage_pbo(ctx
, packing
);
1068 if (stImage
->pt
&& texImage
->Data
) {
1069 st_texture_image_unmap(st
, stImage
);
1070 texImage
->Data
= NULL
;
1077 st_TexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1078 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1079 GLsizei width
, GLsizei height
, GLsizei depth
,
1080 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1081 const struct gl_pixelstore_attrib
*packing
,
1082 struct gl_texture_object
*texObj
,
1083 struct gl_texture_image
*texImage
)
1085 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1086 width
, height
, depth
, format
, type
,
1087 pixels
, packing
, texObj
, texImage
);
1092 st_TexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1093 GLint xoffset
, GLint yoffset
,
1094 GLsizei width
, GLsizei height
,
1095 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1096 const struct gl_pixelstore_attrib
*packing
,
1097 struct gl_texture_object
*texObj
,
1098 struct gl_texture_image
*texImage
)
1100 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1101 width
, height
, 1, format
, type
,
1102 pixels
, packing
, texObj
, texImage
);
1107 st_TexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1108 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1109 const GLvoid
* pixels
,
1110 const struct gl_pixelstore_attrib
*packing
,
1111 struct gl_texture_object
*texObj
,
1112 struct gl_texture_image
*texImage
)
1114 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1115 format
, type
, pixels
, packing
, texObj
, texImage
);
1120 st_CompressedTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1121 GLint xoffset
, GLsizei width
,
1123 GLsizei imageSize
, const GLvoid
*data
,
1124 struct gl_texture_object
*texObj
,
1125 struct gl_texture_image
*texImage
)
1132 st_CompressedTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1133 GLint xoffset
, GLint yoffset
,
1134 GLsizei width
, GLint height
,
1136 GLsizei imageSize
, const GLvoid
*data
,
1137 struct gl_texture_object
*texObj
,
1138 struct gl_texture_image
*texImage
)
1140 struct st_context
*st
= st_context(ctx
);
1141 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1145 enum pipe_format pformat
;
1148 pformat
= stImage
->pt
->format
;
1150 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1151 PIPE_TRANSFER_WRITE
,
1155 srcBlockStride
= util_format_get_stride(pformat
, width
);
1156 dstBlockStride
= stImage
->transfer
->stride
;
1158 assert(stImage
->pt
);
1159 /* TODO find good values for block and strides */
1160 /* TODO also adjust texImage->data for yoffset/xoffset */
1164 if (!texImage
->Data
) {
1165 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1169 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1170 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1172 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1173 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1174 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1175 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1176 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1180 st_texture_image_unmap(st
, stImage
);
1181 texImage
->Data
= NULL
;
1187 st_CompressedTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1188 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1189 GLsizei width
, GLint height
, GLint depth
,
1191 GLsizei imageSize
, const GLvoid
*data
,
1192 struct gl_texture_object
*texObj
,
1193 struct gl_texture_image
*texImage
)
1201 * Do a CopyTexSubImage operation using a read transfer from the source,
1202 * a write transfer to the destination and get_tile()/put_tile() to access
1203 * the pixels/texels.
1205 * Note: srcY=0=TOP of renderbuffer
1208 fallback_copy_texsubimage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1209 struct st_renderbuffer
*strb
,
1210 struct st_texture_image
*stImage
,
1212 GLint destX
, GLint destY
, GLint destZ
,
1213 GLint srcX
, GLint srcY
,
1214 GLsizei width
, GLsizei height
)
1216 struct st_context
*st
= st_context(ctx
);
1217 struct pipe_context
*pipe
= st
->pipe
;
1218 struct pipe_transfer
*src_trans
;
1220 enum pipe_transfer_usage transfer_usage
;
1222 if (ST_DEBUG
& DEBUG_FALLBACK
)
1223 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1225 assert(width
<= MAX_WIDTH
);
1227 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1228 srcY
= strb
->Base
.Height
- srcY
- height
;
1231 src_trans
= pipe_get_transfer(st_context(ctx
)->pipe
,
1238 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1239 baseFormat
== GL_DEPTH_STENCIL
) &&
1240 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1241 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1243 transfer_usage
= PIPE_TRANSFER_WRITE
;
1245 /* XXX this used to ignore destZ param */
1246 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
1247 destX
, destY
, width
, height
);
1249 if (baseFormat
== GL_DEPTH_COMPONENT
||
1250 baseFormat
== GL_DEPTH_STENCIL
) {
1251 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1252 ctx
->Pixel
.DepthBias
!= 0.0F
);
1255 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1256 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1265 /* To avoid a large temp memory allocation, do copy row by row */
1266 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1267 uint data
[MAX_WIDTH
];
1268 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1270 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1272 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1278 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1280 if (tempSrc
&& texDest
) {
1281 const GLint dims
= 2;
1282 const GLint dstRowStride
= stImage
->transfer
->stride
;
1283 struct gl_texture_image
*texImage
= &stImage
->base
;
1284 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1286 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1287 unpack
.Invert
= GL_TRUE
;
1290 /* get float/RGBA image from framebuffer */
1291 /* XXX this usually involves a lot of int/float conversion.
1292 * try to avoid that someday.
1294 pipe_get_tile_rgba_format(pipe
, src_trans
, 0, 0, width
, height
,
1295 util_format_linear(strb
->texture
->format
),
1298 /* Store into texture memory.
1299 * Note that this does some special things such as pixel transfer
1300 * ops and format conversion. In particular, if the dest tex format
1301 * is actually RGBA but the user created the texture as GL_RGB we
1302 * need to fill-in/override the alpha channel with 1.0.
1304 _mesa_texstore(ctx
, dims
,
1305 texImage
->_BaseFormat
,
1306 texImage
->TexFormat
,
1310 texImage
->ImageOffsets
,
1312 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1316 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1323 st_texture_image_unmap(st
, stImage
);
1324 pipe
->transfer_destroy(pipe
, src_trans
);
1330 * If the format of the src renderbuffer and the format of the dest
1331 * texture are compatible (in terms of blitting), return a TGSI writemask
1332 * to be used during the blit.
1333 * If the src/dest are incompatible, return 0.
1336 compatible_src_dst_formats(struct gl_context
*ctx
,
1337 const struct gl_renderbuffer
*src
,
1338 const struct gl_texture_image
*dst
)
1340 /* Get logical base formats for the src and dest.
1341 * That is, use the user-requested formats and not the actual, device-
1343 * For example, the user may have requested an A8 texture but the
1344 * driver may actually be using an RGBA texture format. When we
1345 * copy/blit to that texture, we only want to copy the Alpha channel
1346 * and not the RGB channels.
1348 * Similarly, when the src FBO was created an RGB format may have been
1349 * requested but the driver actually chose an RGBA format. In that case,
1350 * we don't want to copy the undefined Alpha channel to the dest texture
1351 * (it should be 1.0).
1353 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1354 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1357 * XXX when we have red-only and red/green renderbuffers we'll need
1358 * to add more cases here (or implement a general-purpose routine that
1359 * queries the existance of the R,G,B,A channels in the src and dest).
1361 if (srcFormat
== dstFormat
) {
1362 /* This is the same as matching_base_formats, which should
1363 * always pass, as it did previously.
1365 return TGSI_WRITEMASK_XYZW
;
1367 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1368 /* Make sure that A in the dest is 1. The actual src format
1369 * may be RGBA and have undefined A values.
1371 return TGSI_WRITEMASK_XYZ
;
1373 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1374 /* Make sure that A in the dest is 1. The actual dst format
1375 * may be RGBA and will need A=1 to provide proper alpha values
1376 * when sampled later.
1378 return TGSI_WRITEMASK_XYZ
;
1381 if (ST_DEBUG
& DEBUG_FALLBACK
)
1382 debug_printf("%s failed for src %s, dst %s\n",
1384 _mesa_lookup_enum_by_nr(srcFormat
),
1385 _mesa_lookup_enum_by_nr(dstFormat
));
1396 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1397 * Note that the region to copy has already been clipped so we know we
1398 * won't read from outside the source renderbuffer's bounds.
1400 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1403 st_copy_texsubimage(struct gl_context
*ctx
,
1404 GLenum target
, GLint level
,
1405 GLint destX
, GLint destY
, GLint destZ
,
1406 GLint srcX
, GLint srcY
,
1407 GLsizei width
, GLsizei height
)
1409 struct gl_texture_unit
*texUnit
=
1410 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1411 struct gl_texture_object
*texObj
=
1412 _mesa_select_tex_object(ctx
, texUnit
, target
);
1413 struct gl_texture_image
*texImage
=
1414 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1415 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1416 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1417 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1418 struct st_renderbuffer
*strb
;
1419 struct st_context
*st
= st_context(ctx
);
1420 struct pipe_context
*pipe
= st
->pipe
;
1421 struct pipe_screen
*screen
= pipe
->screen
;
1422 enum pipe_format dest_format
, src_format
;
1423 GLboolean use_fallback
= GL_TRUE
;
1424 GLboolean matching_base_formats
;
1425 GLuint format_writemask
, sample_count
;
1426 struct pipe_surface
*dest_surface
= NULL
;
1427 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1429 /* make sure finalize_textures has been called?
1431 if (0) st_validate_state(st
);
1433 /* determine if copying depth or color data */
1434 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1435 texBaseFormat
== GL_DEPTH_STENCIL
) {
1436 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1437 if (strb
->Base
.Wrapped
) {
1438 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
1442 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1443 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1446 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1447 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1451 sample_count
= strb
->surface
->texture
->nr_samples
;
1452 /* I believe this would be legal, presumably would need to do a resolve
1453 for color, and for depth/stencil spec says to just use one of the
1454 depth/stencil samples per pixel? Need some transfer clarifications. */
1455 assert(sample_count
< 2);
1481 if (width
< 0 || height
< 0)
1486 assert(strb
->surface
);
1487 assert(stImage
->pt
);
1489 src_format
= strb
->surface
->format
;
1490 dest_format
= stImage
->pt
->format
;
1493 * Determine if the src framebuffer and dest texture have the same
1494 * base format. We need this to detect a case such as the framebuffer
1495 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1496 * texture format stores RGBA we need to set A=1 (overriding the
1497 * framebuffer's alpha values). We can't do that with the blit or
1498 * textured-quad paths.
1500 matching_base_formats
=
1501 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1502 _mesa_get_format_base_format(texImage
->TexFormat
));
1503 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1505 if (ctx
->_ImageTransferState
== 0x0) {
1507 if (matching_base_formats
&&
1508 src_format
== dest_format
&&
1511 /* use surface_copy() / blit */
1512 struct pipe_box src_box
;
1513 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
1514 width
, height
, &src_box
);
1516 /* for resource_copy_region(), y=0=top, always */
1517 pipe
->resource_copy_region(pipe
,
1521 destX
, destY
, destZ
+ stImage
->face
,
1524 strb
->surface
->u
.tex
.level
,
1526 use_fallback
= GL_FALSE
;
1528 else if (format_writemask
&&
1529 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1530 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1531 screen
->is_format_supported(screen
, src_format
,
1532 PIPE_TEXTURE_2D
, sample_count
,
1533 PIPE_BIND_SAMPLER_VIEW
) &&
1534 screen
->is_format_supported(screen
, dest_format
,
1536 PIPE_BIND_RENDER_TARGET
)) {
1537 /* draw textured quad to do the copy */
1539 struct pipe_surface surf_tmpl
;
1540 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1541 surf_tmpl
.format
= util_format_linear(stImage
->pt
->format
);
1542 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
1543 surf_tmpl
.u
.tex
.level
= stImage
->level
;
1544 surf_tmpl
.u
.tex
.first_layer
= stImage
->face
+ destZ
;
1545 surf_tmpl
.u
.tex
.last_layer
= stImage
->face
+ destZ
;
1547 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1551 srcY1
= strb
->Base
.Height
- srcY
- height
;
1552 srcY0
= srcY1
+ height
;
1556 srcY1
= srcY0
+ height
;
1559 util_blit_pixels_writemask(st
->blit
,
1561 strb
->surface
->u
.tex
.level
,
1563 srcX
+ width
, srcY1
,
1564 strb
->surface
->u
.tex
.first_layer
,
1567 destX
+ width
, destY
+ height
,
1568 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1570 use_fallback
= GL_FALSE
;
1574 pipe_surface_reference(&dest_surface
, NULL
);
1578 /* software fallback */
1579 fallback_copy_texsubimage(ctx
, target
, level
,
1580 strb
, stImage
, texBaseFormat
,
1581 destX
, destY
, destZ
,
1582 srcX
, srcY
, width
, height
);
1589 st_CopyTexImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1590 GLenum internalFormat
,
1591 GLint x
, GLint y
, GLsizei width
, GLint border
)
1593 struct gl_texture_unit
*texUnit
=
1594 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1595 struct gl_texture_object
*texObj
=
1596 _mesa_select_tex_object(ctx
, texUnit
, target
);
1597 struct gl_texture_image
*texImage
=
1598 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1600 /* Setup or redefine the texture object, texture and texture
1601 * image. Don't populate yet.
1603 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1605 GL_RGBA
, CHAN_TYPE
, NULL
,
1606 &ctx
->DefaultPacking
, texObj
, texImage
);
1608 st_copy_texsubimage(ctx
, target
, level
,
1609 0, 0, 0, /* destX,Y,Z */
1610 x
, y
, width
, 1); /* src X, Y, size */
1615 st_CopyTexImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1616 GLenum internalFormat
,
1617 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1620 struct gl_texture_unit
*texUnit
=
1621 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1622 struct gl_texture_object
*texObj
=
1623 _mesa_select_tex_object(ctx
, texUnit
, target
);
1624 struct gl_texture_image
*texImage
=
1625 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1627 /* Setup or redefine the texture object, texture and texture
1628 * image. Don't populate yet.
1630 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1631 width
, height
, border
,
1632 GL_RGBA
, CHAN_TYPE
, NULL
,
1633 &ctx
->DefaultPacking
, texObj
, texImage
);
1635 st_copy_texsubimage(ctx
, target
, level
,
1636 0, 0, 0, /* destX,Y,Z */
1637 x
, y
, width
, height
); /* src X, Y, size */
1642 st_CopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1643 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1645 const GLint yoffset
= 0, zoffset
= 0;
1646 const GLsizei height
= 1;
1647 st_copy_texsubimage(ctx
, target
, level
,
1648 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1649 x
, y
, width
, height
); /* src X, Y, size */
1654 st_CopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1655 GLint xoffset
, GLint yoffset
,
1656 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1658 const GLint zoffset
= 0;
1659 st_copy_texsubimage(ctx
, target
, level
,
1660 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1661 x
, y
, width
, height
); /* src X, Y, size */
1666 st_CopyTexSubImage3D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1667 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1668 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1670 st_copy_texsubimage(ctx
, target
, level
,
1671 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1672 x
, y
, width
, height
); /* src X, Y, size */
1677 * Copy image data from stImage into the texture object 'stObj' at level
1681 copy_image_data_to_texture(struct st_context
*st
,
1682 struct st_texture_object
*stObj
,
1684 struct st_texture_image
*stImage
)
1688 const struct gl_texture_image
*dstImage
=
1689 stObj
->base
.Image
[stImage
->face
][dstLevel
];
1691 assert(dstImage
->Width
== stImage
->base
.Width
);
1692 assert(dstImage
->Height
== stImage
->base
.Height
);
1693 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1697 /* Copy potentially with the blitter:
1699 st_texture_image_copy(st
->pipe
,
1700 stObj
->pt
, dstLevel
, /* dest texture, level */
1701 stImage
->pt
, stImage
->level
, /* src texture, level */
1704 pipe_resource_reference(&stImage
->pt
, NULL
);
1706 else if (stImage
->base
.Data
) {
1707 st_texture_image_data(st
,
1712 stImage
->base
.RowStride
*
1713 util_format_get_blocksize(stObj
->pt
->format
),
1714 stImage
->base
.RowStride
*
1715 stImage
->base
.Height
*
1716 util_format_get_blocksize(stObj
->pt
->format
));
1717 _mesa_align_free(stImage
->base
.Data
);
1718 stImage
->base
.Data
= NULL
;
1721 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1726 * Called during state validation. When this function is finished,
1727 * the texture object should be ready for rendering.
1728 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1731 st_finalize_texture(struct gl_context
*ctx
,
1732 struct pipe_context
*pipe
,
1733 struct gl_texture_object
*tObj
)
1735 struct st_context
*st
= st_context(ctx
);
1736 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1737 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1739 struct st_texture_image
*firstImage
;
1740 enum pipe_format firstImageFormat
;
1741 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1743 if (stObj
->base
._Complete
) {
1744 /* The texture is complete and we know exactly how many mipmap levels
1745 * are present/needed. This is conditional because we may be called
1746 * from the st_generate_mipmap() function when the texture object is
1747 * incomplete. In that case, we'll have set stObj->lastLevel before
1750 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1751 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1752 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1754 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1757 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1760 /* If both firstImage and stObj point to a texture which can contain
1761 * all active images, favour firstImage. Note that because of the
1762 * completeness requirement, we know that the image dimensions
1765 if (firstImage
->pt
&&
1766 firstImage
->pt
!= stObj
->pt
&&
1767 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1768 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1769 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1772 /* Find gallium format for the Mesa texture */
1773 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1775 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1777 GLuint width
, height
, depth
;
1778 if (!guess_base_level_size(stObj
->base
.Target
,
1779 firstImage
->base
.Width2
,
1780 firstImage
->base
.Height2
,
1781 firstImage
->base
.Depth2
,
1782 stObj
->base
.BaseLevel
,
1783 &width
, &height
, &depth
)) {
1784 width
= stObj
->width0
;
1785 height
= stObj
->height0
;
1786 depth
= stObj
->depth0
;
1788 /* convert GL dims to Gallium dims */
1789 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1790 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1793 /* If we already have a gallium texture, check that it matches the texture
1794 * object's format, target, size, num_levels, etc.
1797 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1798 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1799 stObj
->pt
->last_level
< stObj
->lastLevel
||
1800 stObj
->pt
->width0
!= ptWidth
||
1801 stObj
->pt
->height0
!= ptHeight
||
1802 stObj
->pt
->depth0
!= ptDepth
||
1803 stObj
->pt
->array_size
!= ptLayers
)
1805 /* The gallium texture does not match the Mesa texture so delete the
1806 * gallium texture now. We'll make a new one below.
1808 pipe_resource_reference(&stObj
->pt
, NULL
);
1809 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1810 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1814 /* May need to create a new gallium texture:
1817 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1819 stObj
->pt
= st_texture_create(st
,
1820 gl_target_to_pipe(stObj
->base
.Target
),
1830 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1835 /* Pull in any images not in the object's texture:
1837 for (face
= 0; face
< nr_faces
; face
++) {
1839 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1840 struct st_texture_image
*stImage
=
1841 st_texture_image(stObj
->base
.Image
[face
][level
]);
1843 /* Need to import images in main memory or held in other textures.
1845 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1846 if (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1847 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1848 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
)) {
1849 /* src image fits expected dest mipmap level size */
1850 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1861 * Returns pointer to a default/dummy texture.
1862 * This is typically used when the current shader has tex/sample instructions
1863 * but the user has not provided a (any) texture(s).
1865 struct gl_texture_object
*
1866 st_get_default_texture(struct st_context
*st
)
1868 if (!st
->default_texture
) {
1869 static const GLenum target
= GL_TEXTURE_2D
;
1870 GLubyte pixels
[16][16][4];
1871 struct gl_texture_object
*texObj
;
1872 struct gl_texture_image
*texImg
;
1875 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1876 * when attempting to sample incomplete textures.
1878 for (i
= 0; i
< 16; i
++) {
1879 for (j
= 0; j
< 16; j
++) {
1880 pixels
[i
][j
][0] = 0;
1881 pixels
[i
][j
][1] = 0;
1882 pixels
[i
][j
][2] = 0;
1883 pixels
[i
][j
][3] = 255;
1887 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1889 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1891 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1892 16, 16, 1, 0, /* w, h, d, border */
1893 GL_RGBA
, MESA_FORMAT_RGBA8888
);
1895 st_TexImage(st
->ctx
, 2, target
,
1896 0, GL_RGBA
, /* level, intformat */
1897 16, 16, 1, 0, /* w, h, d, border */
1898 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1899 &st
->ctx
->DefaultPacking
,
1903 texObj
->Sampler
.MinFilter
= GL_NEAREST
;
1904 texObj
->Sampler
.MagFilter
= GL_NEAREST
;
1905 texObj
->_Complete
= GL_TRUE
;
1907 st
->default_texture
= texObj
;
1909 return st
->default_texture
;
1914 st_init_texture_functions(struct dd_function_table
*functions
)
1916 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1917 functions
->TexImage1D
= st_TexImage1D
;
1918 functions
->TexImage2D
= st_TexImage2D
;
1919 functions
->TexImage3D
= st_TexImage3D
;
1920 functions
->TexSubImage1D
= st_TexSubImage1D
;
1921 functions
->TexSubImage2D
= st_TexSubImage2D
;
1922 functions
->TexSubImage3D
= st_TexSubImage3D
;
1923 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1924 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1925 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1926 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1927 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1928 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1929 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1930 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1931 functions
->GenerateMipmap
= st_generate_mipmap
;
1933 functions
->GetTexImage
= st_GetTexImage
;
1935 /* compressed texture functions */
1936 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1937 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1939 functions
->NewTextureObject
= st_NewTextureObject
;
1940 functions
->NewTextureImage
= st_NewTextureImage
;
1941 functions
->DeleteTexture
= st_DeleteTextureObject
;
1942 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1944 functions
->TextureMemCpy
= do_memcpy
;
1946 /* XXX Temporary until we can query pipe's texture sizes */
1947 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;