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/texgetimage.h"
42 #include "main/teximage.h"
43 #include "main/texobj.h"
44 #include "main/texstore.h"
46 #include "state_tracker/st_debug.h"
47 #include "state_tracker/st_context.h"
48 #include "state_tracker/st_cb_fbo.h"
49 #include "state_tracker/st_cb_flush.h"
50 #include "state_tracker/st_cb_texture.h"
51 #include "state_tracker/st_format.h"
52 #include "state_tracker/st_texture.h"
53 #include "state_tracker/st_gen_mipmap.h"
54 #include "state_tracker/st_atom.h"
56 #include "pipe/p_context.h"
57 #include "pipe/p_defines.h"
58 #include "util/u_inlines.h"
59 #include "pipe/p_shader_tokens.h"
60 #include "util/u_tile.h"
61 #include "util/u_blit.h"
62 #include "util/u_format.h"
63 #include "util/u_surface.h"
64 #include "util/u_sampler.h"
65 #include "util/u_math.h"
66 #include "util/u_box.h"
68 #define DBG if (0) printf
71 static enum pipe_texture_target
72 gl_target_to_pipe(GLenum target
)
76 return PIPE_TEXTURE_1D
;
78 return PIPE_TEXTURE_2D
;
79 case GL_TEXTURE_RECTANGLE_NV
:
80 return PIPE_TEXTURE_RECT
;
82 return PIPE_TEXTURE_3D
;
83 case GL_TEXTURE_CUBE_MAP_ARB
:
84 return PIPE_TEXTURE_CUBE
;
85 case GL_TEXTURE_1D_ARRAY_EXT
:
86 return PIPE_TEXTURE_1D_ARRAY
;
87 case GL_TEXTURE_2D_ARRAY_EXT
:
88 return PIPE_TEXTURE_2D_ARRAY
;
89 case GL_TEXTURE_BUFFER
:
98 /** called via ctx->Driver.NewTextureImage() */
99 static struct gl_texture_image
*
100 st_NewTextureImage(struct gl_context
* ctx
)
102 DBG("%s\n", __FUNCTION__
);
104 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
108 /** called via ctx->Driver.DeleteTextureImage() */
110 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
112 /* nothing special (yet) for st_texture_image */
113 _mesa_delete_texture_image(ctx
, img
);
117 /** called via ctx->Driver.NewTextureObject() */
118 static struct gl_texture_object
*
119 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
121 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
123 DBG("%s\n", __FUNCTION__
);
124 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
129 /** called via ctx->Driver.DeleteTextureObject() */
131 st_DeleteTextureObject(struct gl_context
*ctx
,
132 struct gl_texture_object
*texObj
)
134 struct st_context
*st
= st_context(ctx
);
135 struct st_texture_object
*stObj
= st_texture_object(texObj
);
137 pipe_resource_reference(&stObj
->pt
, NULL
);
138 if (stObj
->sampler_view
) {
139 if (stObj
->sampler_view
->context
!= st
->pipe
) {
140 /* Take "ownership" of this texture sampler view by setting
141 * its context pointer to this context. This avoids potential
142 * crashes when the texture object is shared among contexts
143 * and the original/owner context has already been destroyed.
145 stObj
->sampler_view
->context
= st
->pipe
;
147 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
149 _mesa_delete_texture_object(ctx
, texObj
);
153 /** called via ctx->Driver.FreeTextureImageBuffer() */
155 st_FreeTextureImageBuffer(struct gl_context
* ctx
, struct gl_texture_image
*texImage
)
157 struct st_texture_image
*stImage
= st_texture_image(texImage
);
159 DBG("%s\n", __FUNCTION__
);
162 pipe_resource_reference(&stImage
->pt
, NULL
);
165 if (texImage
->Data
) {
166 _mesa_align_free(texImage
->Data
);
167 texImage
->Data
= NULL
;
172 /** called via ctx->Driver.MapTextureImage() */
174 st_MapTextureImage(struct gl_context
*ctx
,
175 struct gl_texture_image
*texImage
,
176 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
178 GLubyte
**mapOut
, GLint
*rowStrideOut
)
180 struct st_context
*st
= st_context(ctx
);
181 struct st_texture_image
*stImage
= st_texture_image(texImage
);
186 if (mode
& GL_MAP_READ_BIT
)
187 pipeMode
|= PIPE_TRANSFER_READ
;
188 if (mode
& GL_MAP_WRITE_BIT
)
189 pipeMode
|= PIPE_TRANSFER_WRITE
;
191 map
= st_texture_image_map(st
, stImage
, slice
, pipeMode
, x
, y
, w
, h
);
194 *rowStrideOut
= stImage
->transfer
->stride
;
203 /** called via ctx->Driver.UnmapTextureImage() */
205 st_UnmapTextureImage(struct gl_context
*ctx
,
206 struct gl_texture_image
*texImage
,
209 struct st_context
*st
= st_context(ctx
);
210 struct st_texture_image
*stImage
= st_texture_image(texImage
);
211 st_texture_image_unmap(st
, stImage
);
216 * From linux kernel i386 header files, copes with odd sizes better
217 * than COPY_DWORDS would:
218 * XXX Put this in src/mesa/main/imports.h ???
220 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
222 __memcpy(void *to
, const void *from
, size_t n
)
225 __asm__
__volatile__("rep ; movsl\n\t"
229 "1:\ttestb $1,%b4\n\t"
231 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
232 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
237 #define __memcpy(a,b,c) memcpy(a,b,c)
242 * The system memcpy (at least on ubuntu 5.10) has problems copying
243 * to agp (writecombined) memory from a source which isn't 64-byte
244 * aligned - there is a 4x performance falloff.
246 * The x86 __memcpy is immune to this but is slightly slower
247 * (10%-ish) than the system memcpy.
249 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
250 * isn't much faster than x86_memcpy for agp copies.
252 * TODO: switch dynamically.
255 do_memcpy(void *dest
, const void *src
, size_t n
)
257 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
258 return __memcpy(dest
, src
, n
);
261 return memcpy(dest
, src
, n
);
266 * Return default texture resource binding bitmask for the given format.
269 default_bindings(struct st_context
*st
, enum pipe_format format
)
271 struct pipe_screen
*screen
= st
->pipe
->screen
;
272 const unsigned target
= PIPE_TEXTURE_2D
;
275 if (util_format_is_depth_or_stencil(format
))
276 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
278 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
280 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
284 format
= util_format_linear(format
);
286 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
289 return PIPE_BIND_SAMPLER_VIEW
;
294 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
296 get_texture_dims(GLenum target
)
300 case GL_TEXTURE_1D_ARRAY_EXT
:
301 case GL_TEXTURE_BUFFER
:
304 case GL_TEXTURE_CUBE_MAP_ARB
:
305 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
306 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
307 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
308 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
309 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
310 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
311 case GL_TEXTURE_RECTANGLE_NV
:
312 case GL_TEXTURE_2D_ARRAY_EXT
:
317 assert(0 && "invalid texture target in get_texture_dims()");
324 * Given the size of a mipmap image, try to compute the size of the level=0
327 * Note that this isn't always accurate for odd-sized, non-POW textures.
328 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
330 * \return GL_TRUE for success, GL_FALSE for failure
333 guess_base_level_size(GLenum target
,
334 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
335 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
337 const GLuint dims
= get_texture_dims(target
);
344 /* Depending on the image's size, we can't always make a guess here */
345 if ((dims
>= 1 && width
== 1) ||
346 (dims
>= 2 && height
== 1) ||
347 (dims
>= 3 && depth
== 1)) {
348 /* we can't determine the image size at level=0 */
352 /* grow the image size until we hit level = 0 */
373 * Try to allocate a pipe_resource object for the given st_texture_object.
375 * We use the given st_texture_image as a clue to determine the size of the
376 * mipmap image at level=0.
378 * \return GL_TRUE for success, GL_FALSE if out of memory.
381 guess_and_alloc_texture(struct st_context
*st
,
382 struct st_texture_object
*stObj
,
383 const struct st_texture_image
*stImage
)
385 GLuint lastLevel
, width
, height
, depth
;
387 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
388 enum pipe_format fmt
;
390 DBG("%s\n", __FUNCTION__
);
394 if (!guess_base_level_size(stObj
->base
.Target
,
395 stImage
->base
.Width2
,
396 stImage
->base
.Height2
,
397 stImage
->base
.Depth2
,
399 &width
, &height
, &depth
)) {
400 /* we can't determine the image size at level=0 */
401 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
402 /* this is not an out of memory error */
406 /* At this point, (width x height x depth) is the expected size of
407 * the level=0 mipmap image.
410 /* Guess a reasonable value for lastLevel. With OpenGL we have no
411 * idea how many mipmap levels will be in a texture until we start
412 * to render with it. Make an educated guess here but be prepared
413 * to re-allocating a texture buffer with space for more (or fewer)
414 * mipmap levels later.
416 if ((stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
417 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
418 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
419 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
420 !stObj
->base
.GenerateMipmap
&&
421 stImage
->base
.Level
== 0) {
422 /* only alloc space for a single mipmap level */
426 /* alloc space for a full mipmap */
427 GLuint l2width
= util_logbase2(width
);
428 GLuint l2height
= util_logbase2(height
);
429 GLuint l2depth
= util_logbase2(depth
);
430 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
433 /* Save the level=0 dimensions */
434 stObj
->width0
= width
;
435 stObj
->height0
= height
;
436 stObj
->depth0
= depth
;
438 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
440 bindings
= default_bindings(st
, fmt
);
442 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
443 width
, height
, depth
,
444 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
446 stObj
->pt
= st_texture_create(st
,
447 gl_target_to_pipe(stObj
->base
.Target
),
456 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
458 return stObj
->pt
!= NULL
;
463 * Adjust pixel unpack params and image dimensions to strip off the
465 * Gallium doesn't support texture borders. They've seldem been used
466 * and seldom been implemented correctly anyway.
467 * \param unpackNew returns the new pixel unpack parameters
470 strip_texture_border(GLint border
,
471 GLint
*width
, GLint
*height
, GLint
*depth
,
472 const struct gl_pixelstore_attrib
*unpack
,
473 struct gl_pixelstore_attrib
*unpackNew
)
475 assert(border
> 0); /* sanity check */
477 *unpackNew
= *unpack
;
479 if (unpackNew
->RowLength
== 0)
480 unpackNew
->RowLength
= *width
;
482 if (depth
&& unpackNew
->ImageHeight
== 0)
483 unpackNew
->ImageHeight
= *height
;
485 unpackNew
->SkipPixels
+= border
;
487 unpackNew
->SkipRows
+= border
;
489 unpackNew
->SkipImages
+= border
;
492 *width
= *width
- 2 * border
;
493 if (height
&& *height
>= 3)
494 *height
= *height
- 2 * border
;
495 if (depth
&& *depth
>= 3)
496 *depth
= *depth
- 2 * border
;
501 * Do glTexImage1/2/3D().
504 st_TexImage(struct gl_context
* ctx
,
506 GLenum target
, GLint level
,
507 GLint internalFormat
,
508 GLint width
, GLint height
, GLint depth
,
510 GLenum format
, GLenum type
, const void *pixels
,
511 const struct gl_pixelstore_attrib
*unpack
,
512 struct gl_texture_object
*texObj
,
513 struct gl_texture_image
*texImage
,
514 GLsizei imageSize
, GLboolean compressed_src
)
516 struct st_context
*st
= st_context(ctx
);
517 struct st_texture_object
*stObj
= st_texture_object(texObj
);
518 struct st_texture_image
*stImage
= st_texture_image(texImage
);
519 GLuint dstRowStride
= 0;
520 struct gl_pixelstore_attrib unpackNB
;
521 enum pipe_transfer_usage transfer_usage
= 0;
523 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
524 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
526 /* switch to "normal" */
527 if (stObj
->surface_based
) {
530 _mesa_clear_texture_object(ctx
, texObj
);
531 pipe_resource_reference(&stObj
->pt
, NULL
);
533 /* oops, need to init this image again */
534 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
535 internalFormat
, format
, type
);
537 _mesa_init_teximage_fields(ctx
, target
, texImage
,
538 width
, height
, depth
, border
,
539 internalFormat
, texFormat
);
541 stObj
->surface_based
= GL_FALSE
;
544 /* gallium does not support texture borders, strip it off */
546 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
548 texImage
->Width
= width
;
549 texImage
->Height
= height
;
550 texImage
->Depth
= depth
;
551 texImage
->Border
= 0;
555 assert(texImage
->Width
== width
);
556 assert(texImage
->Height
== height
);
557 assert(texImage
->Depth
== depth
);
560 stImage
->base
.Face
= _mesa_tex_target_to_face(target
);
561 stImage
->base
.Level
= level
;
563 /* Release the reference to a potentially orphaned buffer.
564 * Release any old malloced memory.
567 pipe_resource_reference(&stImage
->pt
, NULL
);
568 assert(!texImage
->Data
);
570 else if (texImage
->Data
) {
571 _mesa_align_free(texImage
->Data
);
575 * See if the new image is somehow incompatible with the existing
576 * mipmap. If so, free the old mipmap.
579 if (level
> (GLint
) stObj
->pt
->last_level
||
580 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
581 stImage
->base
.Face
, stImage
->base
.Level
)) {
583 pipe_resource_reference(&stObj
->pt
, NULL
);
585 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
589 if (width
== 0 || height
== 0 || depth
== 0) {
590 /* stop after freeing old image */
595 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
596 /* Probably out of memory.
597 * Try flushing any pending rendering, then retry.
600 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
601 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
607 assert(!stImage
->pt
);
609 /* Check if this texture image can live inside the texture object's buffer.
610 * If so, store the image there. Otherwise the image will temporarily live
614 st_texture_match_image(stObj
->pt
, &stImage
->base
,
615 stImage
->base
.Face
, stImage
->base
.Level
)) {
617 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
622 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
624 /* Pixel data may come from regular user memory or a PBO. For the later,
625 * do bounds checking and map the PBO to read pixels data from it.
627 * XXX we should try to use a GPU-accelerated path to copy the image data
628 * from the PBO to the texture.
630 if (compressed_src
) {
631 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
633 "glCompressedTexImage");
636 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
638 pixels
, unpack
, "glTexImage");
641 /* for a 1D array upload the image as a series of layer with height = 1 */
642 if (target
== GL_TEXTURE_1D_ARRAY
) {
648 * Prepare to store the texture data. Either map the gallium texture buffer
649 * memory or malloc space for it.
653 /* We've allocated texture resource, but have no pixel data - all done. */
657 /* Store the image in the gallium transfer object */
658 if (format
== GL_DEPTH_COMPONENT
&&
659 util_format_is_depth_and_stencil(stImage
->pt
->format
))
660 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
662 transfer_usage
= PIPE_TRANSFER_WRITE
;
664 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
665 transfer_usage
, 0, 0, width
, height
);
666 if(stImage
->transfer
)
667 dstRowStride
= stImage
->transfer
->stride
;
670 /* Allocate regular memory and store the image there temporarily. */
671 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
672 width
, height
, depth
);
673 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
675 texImage
->Data
= _mesa_align_malloc(imageSize
, 16);
678 if (!texImage
->Data
) {
679 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
684 /* We've allocated texture memory, but have no pixel data - all done. */
688 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
689 width
, height
, depth
, width
, dstRowStride
);
691 /* Copy user texture image into the texture buffer.
693 if (compressed_src
) {
694 const GLuint srcRowStride
=
695 _mesa_format_row_stride(texImage
->TexFormat
, width
);
696 if (dstRowStride
== srcRowStride
) {
697 memcpy(texImage
->Data
, pixels
, imageSize
);
700 char *dst
= texImage
->Data
;
701 const char *src
= pixels
;
702 GLuint i
, bw
, bh
, lines
;
703 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
704 lines
= (height
+ bh
- 1) / bh
;
706 for (i
= 0; i
< lines
; ++i
) {
707 memcpy(dst
, src
, srcRowStride
);
714 const GLuint srcImageStride
=
715 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
717 const GLubyte
*src
= (const GLubyte
*) pixels
;
719 for (i
= 0; i
< depth
; i
++) {
720 if (!_mesa_texstore(ctx
, dims
,
721 texImage
->_BaseFormat
,
724 0, 0, 0, /* dstX/Y/Zoffset */
726 texImage
->ImageOffsets
,
728 format
, type
, src
, unpack
)) {
729 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
732 if (stImage
->pt
&& i
+ 1 < depth
) {
733 /* unmap this slice */
734 st_texture_image_unmap(st
, stImage
);
735 /* map next slice of 3D texture */
736 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
737 transfer_usage
, 0, 0,
739 src
+= srcImageStride
;
745 _mesa_unmap_teximage_pbo(ctx
, unpack
);
747 if (stImage
->pt
&& texImage
->Data
) {
748 st_texture_image_unmap(st
, stImage
);
749 texImage
->Data
= NULL
;
755 st_TexImage3D(struct gl_context
* ctx
,
756 GLenum target
, GLint level
,
757 GLint internalFormat
,
758 GLint width
, GLint height
, GLint depth
,
760 GLenum format
, GLenum type
, const void *pixels
,
761 const struct gl_pixelstore_attrib
*unpack
,
762 struct gl_texture_object
*texObj
,
763 struct gl_texture_image
*texImage
)
765 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
766 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
772 st_TexImage2D(struct gl_context
* ctx
,
773 GLenum target
, GLint level
,
774 GLint internalFormat
,
775 GLint width
, GLint height
, GLint border
,
776 GLenum format
, GLenum type
, const void *pixels
,
777 const struct gl_pixelstore_attrib
*unpack
,
778 struct gl_texture_object
*texObj
,
779 struct gl_texture_image
*texImage
)
781 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
782 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
787 st_TexImage1D(struct gl_context
* ctx
,
788 GLenum target
, GLint level
,
789 GLint internalFormat
,
790 GLint width
, GLint border
,
791 GLenum format
, GLenum type
, const void *pixels
,
792 const struct gl_pixelstore_attrib
*unpack
,
793 struct gl_texture_object
*texObj
,
794 struct gl_texture_image
*texImage
)
796 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
797 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
802 st_CompressedTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
803 GLint internalFormat
,
804 GLint width
, GLint height
, GLint border
,
805 GLsizei imageSize
, const GLvoid
*data
,
806 struct gl_texture_object
*texObj
,
807 struct gl_texture_image
*texImage
)
809 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
810 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
816 * glGetTexImage() helper: decompress a compressed texture by rendering
817 * a textured quad. Store the results in the user's buffer.
820 decompress_with_blit(struct gl_context
* ctx
, GLenum target
, GLint level
,
821 GLenum format
, GLenum type
, GLvoid
*pixels
,
822 struct gl_texture_object
*texObj
,
823 struct gl_texture_image
*texImage
)
825 struct st_context
*st
= st_context(ctx
);
826 struct pipe_context
*pipe
= st
->pipe
;
827 struct st_texture_image
*stImage
= st_texture_image(texImage
);
828 struct st_texture_object
*stObj
= st_texture_object(texObj
);
829 struct pipe_sampler_view
*src_view
=
830 st_get_texture_sampler_view(stObj
, pipe
);
831 const GLuint width
= texImage
->Width
;
832 const GLuint height
= texImage
->Height
;
833 struct pipe_surface
*dst_surface
;
834 struct pipe_resource
*dst_texture
;
835 struct pipe_transfer
*tex_xfer
;
836 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
837 PIPE_BIND_TRANSFER_READ
);
839 /* create temp / dest surface */
840 if (!util_create_rgba_surface(pipe
, width
, height
, bind
,
841 &dst_texture
, &dst_surface
)) {
842 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
843 "in decompress_with_blit()");
847 /* Disable conditional rendering. */
848 if (st
->render_condition
) {
849 pipe
->render_condition(pipe
, NULL
, 0);
852 /* Choose the source mipmap level */
853 src_view
->u
.tex
.first_level
= src_view
->u
.tex
.last_level
= level
;
855 /* blit/render/decompress */
856 util_blit_pixels_tex(st
->blit
,
857 src_view
, /* pipe_resource (src) */
858 0, 0, /* src x0, y0 */
859 width
, height
, /* src x1, y1 */
860 dst_surface
, /* pipe_surface (dst) */
861 0, 0, /* dst x0, y0 */
862 width
, height
, /* dst x1, y1 */
864 PIPE_TEX_MIPFILTER_NEAREST
);
866 /* Restore conditional rendering state. */
867 if (st
->render_condition
) {
868 pipe
->render_condition(pipe
, st
->render_condition
,
872 /* map the dst_surface so we can read from it */
873 tex_xfer
= pipe_get_transfer(pipe
,
876 0, 0, width
, height
);
878 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
880 /* copy/pack data into user buffer */
881 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
883 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
884 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
886 for (row
= 0; row
< height
; row
++) {
887 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
888 height
, format
, type
, row
, 0);
889 memcpy(dest
, map
, bytesPerRow
);
890 map
+= tex_xfer
->stride
;
892 pipe_transfer_unmap(pipe
, tex_xfer
);
895 /* format translation via floats */
897 enum pipe_format pformat
= util_format_linear(dst_texture
->format
);
898 for (row
= 0; row
< height
; row
++) {
899 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
900 GLfloat rgba
[4 * MAX_WIDTH
];
901 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
902 height
, format
, type
, row
, 0);
904 if (ST_DEBUG
& DEBUG_FALLBACK
)
905 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
907 /* get float[4] rgba row from surface */
908 pipe_get_tile_rgba_format(pipe
, tex_xfer
, 0, row
, width
, 1,
911 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
912 type
, dest
, &ctx
->Pack
, transferOps
);
916 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
918 pipe
->transfer_destroy(pipe
, tex_xfer
);
920 /* destroy the temp / dest surface */
921 util_destroy_rgba_surface(dst_texture
, dst_surface
);
927 * Need to map texture image into memory before copying image data,
931 st_get_tex_image(struct gl_context
* ctx
, GLenum target
, GLint level
,
932 GLenum format
, GLenum type
, GLvoid
* pixels
,
933 struct gl_texture_object
*texObj
,
934 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
936 struct st_context
*st
= st_context(ctx
);
937 struct st_texture_image
*stImage
= st_texture_image(texImage
);
938 const GLuint dstImageStride
=
939 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
945 util_format_is_s3tc(stImage
->pt
->format
) &&
947 /* Need to decompress the texture.
948 * We'll do this by rendering a textured quad.
949 * Note that we only expect RGBA formats (no Z/depth formats).
951 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
958 /* Image is stored in hardware format in a buffer managed by the
959 * kernel. Need to explicitly map and unmap it.
961 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
962 PIPE_TRANSFER_READ
, 0, 0,
964 stImage
->base
.Height
);
965 /* compute stride in texels from stride in bytes */
966 texImage
->RowStride
= stImage
->transfer
->stride
967 * util_format_get_blockwidth(stImage
->pt
->format
)
968 / util_format_get_blocksize(stImage
->pt
->format
);
971 /* Otherwise, the image should actually be stored in
972 * texImage->Data. This is pretty confusing for
973 * everybody, I'd much prefer to separate the two functions of
974 * texImage->Data - storage for texture images in main memory
975 * and access (ie mappings) of images. In other words, we'd
976 * create a new texImage->Map field and leave Data simply for
979 assert(texImage
->Data
);
982 depth
= texImage
->Depth
;
985 dest
= (GLubyte
*) pixels
;
987 for (i
= 0; i
< depth
; i
++) {
988 if (compressed_dst
) {
989 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
993 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
997 if (stImage
->pt
&& i
+ 1 < depth
) {
998 /* unmap this slice */
999 st_texture_image_unmap(st
, stImage
);
1000 /* map next slice of 3D texture */
1001 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
1002 PIPE_TRANSFER_READ
, 0, 0,
1003 stImage
->base
.Width
,
1004 stImage
->base
.Height
);
1005 dest
+= dstImageStride
;
1009 texImage
->Depth
= depth
;
1013 st_texture_image_unmap(st
, stImage
);
1014 texImage
->Data
= NULL
;
1020 st_GetTexImage(struct gl_context
* ctx
, GLenum target
, GLint level
,
1021 GLenum format
, GLenum type
, GLvoid
* pixels
,
1022 struct gl_texture_object
*texObj
,
1023 struct gl_texture_image
*texImage
)
1025 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1031 st_GetCompressedTexImage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1033 struct gl_texture_object
*texObj
,
1034 struct gl_texture_image
*texImage
)
1036 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1043 st_TexSubimage(struct gl_context
*ctx
, GLint dims
, GLenum target
, GLint level
,
1044 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1045 GLint width
, GLint height
, GLint depth
,
1046 GLenum format
, GLenum type
, const void *pixels
,
1047 const struct gl_pixelstore_attrib
*packing
,
1048 struct gl_texture_object
*texObj
,
1049 struct gl_texture_image
*texImage
)
1051 struct st_context
*st
= st_context(ctx
);
1052 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1053 GLuint dstRowStride
;
1054 const GLuint srcImageStride
=
1055 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1058 /* init to silence warning only: */
1059 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1061 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1062 _mesa_lookup_enum_by_nr(target
),
1063 level
, xoffset
, yoffset
, width
, height
);
1066 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1067 type
, pixels
, packing
, "glTexSubImage2D");
1071 /* for a 1D array upload the image as a series of layer with height = 1 */
1072 if (target
== GL_TEXTURE_1D_ARRAY
) {
1077 /* Map buffer if necessary. Need to lock to prevent other contexts
1078 * from uploading the buffer under us.
1081 if (format
== GL_DEPTH_COMPONENT
&&
1082 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1083 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1085 transfer_usage
= PIPE_TRANSFER_WRITE
;
1087 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1093 if (!texImage
->Data
) {
1094 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1098 src
= (const GLubyte
*) pixels
;
1099 dstRowStride
= stImage
->transfer
->stride
;
1101 for (i
= 0; i
< depth
; i
++) {
1102 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1103 texImage
->TexFormat
,
1107 texImage
->ImageOffsets
,
1109 format
, type
, src
, packing
)) {
1110 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1113 if (stImage
->pt
&& i
+ 1 < depth
) {
1114 /* unmap this slice */
1115 st_texture_image_unmap(st
, stImage
);
1116 /* map next slice of 3D texture */
1117 texImage
->Data
= st_texture_image_map(st
, stImage
,
1122 src
+= srcImageStride
;
1127 _mesa_unmap_teximage_pbo(ctx
, packing
);
1129 if (stImage
->pt
&& texImage
->Data
) {
1130 st_texture_image_unmap(st
, stImage
);
1131 texImage
->Data
= NULL
;
1138 st_TexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1139 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1140 GLsizei width
, GLsizei height
, GLsizei depth
,
1141 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1142 const struct gl_pixelstore_attrib
*packing
,
1143 struct gl_texture_object
*texObj
,
1144 struct gl_texture_image
*texImage
)
1146 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1147 width
, height
, depth
, format
, type
,
1148 pixels
, packing
, texObj
, texImage
);
1153 st_TexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1154 GLint xoffset
, GLint yoffset
,
1155 GLsizei width
, GLsizei height
,
1156 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1157 const struct gl_pixelstore_attrib
*packing
,
1158 struct gl_texture_object
*texObj
,
1159 struct gl_texture_image
*texImage
)
1161 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1162 width
, height
, 1, format
, type
,
1163 pixels
, packing
, texObj
, texImage
);
1168 st_TexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1169 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1170 const GLvoid
* pixels
,
1171 const struct gl_pixelstore_attrib
*packing
,
1172 struct gl_texture_object
*texObj
,
1173 struct gl_texture_image
*texImage
)
1175 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1176 format
, type
, pixels
, packing
, texObj
, texImage
);
1181 st_CompressedTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1182 GLint xoffset
, GLsizei width
,
1184 GLsizei imageSize
, const GLvoid
*data
,
1185 struct gl_texture_object
*texObj
,
1186 struct gl_texture_image
*texImage
)
1193 st_CompressedTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1194 GLint xoffset
, GLint yoffset
,
1195 GLsizei width
, GLint height
,
1197 GLsizei imageSize
, const GLvoid
*data
,
1198 struct gl_texture_object
*texObj
,
1199 struct gl_texture_image
*texImage
)
1201 struct st_context
*st
= st_context(ctx
);
1202 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1206 enum pipe_format pformat
;
1209 pformat
= stImage
->pt
->format
;
1211 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1212 PIPE_TRANSFER_WRITE
,
1216 srcBlockStride
= util_format_get_stride(pformat
, width
);
1217 dstBlockStride
= stImage
->transfer
->stride
;
1219 assert(stImage
->pt
);
1220 /* TODO find good values for block and strides */
1221 /* TODO also adjust texImage->data for yoffset/xoffset */
1225 if (!texImage
->Data
) {
1226 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1230 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1231 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1233 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1234 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1235 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1236 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1237 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1241 st_texture_image_unmap(st
, stImage
);
1242 texImage
->Data
= NULL
;
1248 st_CompressedTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1249 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1250 GLsizei width
, GLint height
, GLint depth
,
1252 GLsizei imageSize
, const GLvoid
*data
,
1253 struct gl_texture_object
*texObj
,
1254 struct gl_texture_image
*texImage
)
1262 * Do a CopyTexSubImage operation using a read transfer from the source,
1263 * a write transfer to the destination and get_tile()/put_tile() to access
1264 * the pixels/texels.
1266 * Note: srcY=0=TOP of renderbuffer
1269 fallback_copy_texsubimage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1270 struct st_renderbuffer
*strb
,
1271 struct st_texture_image
*stImage
,
1273 GLint destX
, GLint destY
, GLint destZ
,
1274 GLint srcX
, GLint srcY
,
1275 GLsizei width
, GLsizei height
)
1277 struct st_context
*st
= st_context(ctx
);
1278 struct pipe_context
*pipe
= st
->pipe
;
1279 struct pipe_transfer
*src_trans
;
1281 enum pipe_transfer_usage transfer_usage
;
1283 if (ST_DEBUG
& DEBUG_FALLBACK
)
1284 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1286 assert(width
<= MAX_WIDTH
);
1288 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1289 srcY
= strb
->Base
.Height
- srcY
- height
;
1292 src_trans
= pipe_get_transfer(pipe
,
1295 strb
->rtt_face
+ strb
->rtt_slice
,
1300 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1301 baseFormat
== GL_DEPTH_STENCIL
) &&
1302 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1303 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1305 transfer_usage
= PIPE_TRANSFER_WRITE
;
1307 /* XXX this used to ignore destZ param */
1308 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
1309 destX
, destY
, width
, height
);
1311 if (baseFormat
== GL_DEPTH_COMPONENT
||
1312 baseFormat
== GL_DEPTH_STENCIL
) {
1313 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1314 ctx
->Pixel
.DepthBias
!= 0.0F
);
1317 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1318 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1327 /* To avoid a large temp memory allocation, do copy row by row */
1328 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1329 uint data
[MAX_WIDTH
];
1330 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1332 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1334 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1340 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1342 if (tempSrc
&& texDest
) {
1343 const GLint dims
= 2;
1344 const GLint dstRowStride
= stImage
->transfer
->stride
;
1345 struct gl_texture_image
*texImage
= &stImage
->base
;
1346 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1348 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1349 unpack
.Invert
= GL_TRUE
;
1352 /* get float/RGBA image from framebuffer */
1353 /* XXX this usually involves a lot of int/float conversion.
1354 * try to avoid that someday.
1356 pipe_get_tile_rgba_format(pipe
, src_trans
, 0, 0, width
, height
,
1357 util_format_linear(strb
->texture
->format
),
1360 /* Store into texture memory.
1361 * Note that this does some special things such as pixel transfer
1362 * ops and format conversion. In particular, if the dest tex format
1363 * is actually RGBA but the user created the texture as GL_RGB we
1364 * need to fill-in/override the alpha channel with 1.0.
1366 _mesa_texstore(ctx
, dims
,
1367 texImage
->_BaseFormat
,
1368 texImage
->TexFormat
,
1372 texImage
->ImageOffsets
,
1374 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1378 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1385 st_texture_image_unmap(st
, stImage
);
1386 pipe
->transfer_destroy(pipe
, src_trans
);
1392 * If the format of the src renderbuffer and the format of the dest
1393 * texture are compatible (in terms of blitting), return a TGSI writemask
1394 * to be used during the blit.
1395 * If the src/dest are incompatible, return 0.
1398 compatible_src_dst_formats(struct gl_context
*ctx
,
1399 const struct gl_renderbuffer
*src
,
1400 const struct gl_texture_image
*dst
)
1402 /* Get logical base formats for the src and dest.
1403 * That is, use the user-requested formats and not the actual, device-
1405 * For example, the user may have requested an A8 texture but the
1406 * driver may actually be using an RGBA texture format. When we
1407 * copy/blit to that texture, we only want to copy the Alpha channel
1408 * and not the RGB channels.
1410 * Similarly, when the src FBO was created an RGB format may have been
1411 * requested but the driver actually chose an RGBA format. In that case,
1412 * we don't want to copy the undefined Alpha channel to the dest texture
1413 * (it should be 1.0).
1415 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1416 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1419 * XXX when we have red-only and red/green renderbuffers we'll need
1420 * to add more cases here (or implement a general-purpose routine that
1421 * queries the existance of the R,G,B,A channels in the src and dest).
1423 if (srcFormat
== dstFormat
) {
1424 /* This is the same as matching_base_formats, which should
1425 * always pass, as it did previously.
1427 return TGSI_WRITEMASK_XYZW
;
1429 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1430 /* Make sure that A in the dest is 1. The actual src format
1431 * may be RGBA and have undefined A values.
1433 return TGSI_WRITEMASK_XYZ
;
1435 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1436 /* Make sure that A in the dest is 1. The actual dst format
1437 * may be RGBA and will need A=1 to provide proper alpha values
1438 * when sampled later.
1440 return TGSI_WRITEMASK_XYZ
;
1443 if (ST_DEBUG
& DEBUG_FALLBACK
)
1444 debug_printf("%s failed for src %s, dst %s\n",
1446 _mesa_lookup_enum_by_nr(srcFormat
),
1447 _mesa_lookup_enum_by_nr(dstFormat
));
1458 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1459 * Note that the region to copy has already been clipped so we know we
1460 * won't read from outside the source renderbuffer's bounds.
1462 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1465 st_copy_texsubimage(struct gl_context
*ctx
,
1466 GLenum target
, GLint level
,
1467 GLint destX
, GLint destY
, GLint destZ
,
1468 GLint srcX
, GLint srcY
,
1469 GLsizei width
, GLsizei height
)
1471 struct gl_texture_unit
*texUnit
=
1472 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1473 struct gl_texture_object
*texObj
=
1474 _mesa_select_tex_object(ctx
, texUnit
, target
);
1475 struct gl_texture_image
*texImage
=
1476 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1477 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1478 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1479 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1480 struct st_renderbuffer
*strb
;
1481 struct st_context
*st
= st_context(ctx
);
1482 struct pipe_context
*pipe
= st
->pipe
;
1483 struct pipe_screen
*screen
= pipe
->screen
;
1484 enum pipe_format dest_format
, src_format
;
1485 GLboolean use_fallback
= GL_TRUE
;
1486 GLboolean matching_base_formats
;
1487 GLuint format_writemask
, sample_count
;
1488 struct pipe_surface
*dest_surface
= NULL
;
1489 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1491 /* make sure finalize_textures has been called?
1493 if (0) st_validate_state(st
);
1495 /* determine if copying depth or color data */
1496 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1497 texBaseFormat
== GL_DEPTH_STENCIL
) {
1498 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1499 if (strb
->Base
.Wrapped
) {
1500 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
1504 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1505 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1508 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1509 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1513 sample_count
= strb
->surface
->texture
->nr_samples
;
1514 /* I believe this would be legal, presumably would need to do a resolve
1515 for color, and for depth/stencil spec says to just use one of the
1516 depth/stencil samples per pixel? Need some transfer clarifications. */
1517 assert(sample_count
< 2);
1520 assert(strb
->surface
);
1521 assert(stImage
->pt
);
1523 src_format
= strb
->surface
->format
;
1524 dest_format
= stImage
->pt
->format
;
1527 * Determine if the src framebuffer and dest texture have the same
1528 * base format. We need this to detect a case such as the framebuffer
1529 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1530 * texture format stores RGBA we need to set A=1 (overriding the
1531 * framebuffer's alpha values). We can't do that with the blit or
1532 * textured-quad paths.
1534 matching_base_formats
=
1535 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1536 _mesa_get_format_base_format(texImage
->TexFormat
));
1537 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1539 if (ctx
->_ImageTransferState
== 0x0) {
1541 if (matching_base_formats
&&
1542 src_format
== dest_format
&&
1545 /* use surface_copy() / blit */
1546 struct pipe_box src_box
;
1547 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
1548 width
, height
, &src_box
);
1550 /* for resource_copy_region(), y=0=top, always */
1551 pipe
->resource_copy_region(pipe
,
1554 stImage
->base
.Level
,
1555 destX
, destY
, destZ
+ stImage
->base
.Face
,
1558 strb
->surface
->u
.tex
.level
,
1560 use_fallback
= GL_FALSE
;
1562 else if (format_writemask
&&
1563 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1564 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1565 screen
->is_format_supported(screen
, src_format
,
1566 PIPE_TEXTURE_2D
, sample_count
,
1567 PIPE_BIND_SAMPLER_VIEW
) &&
1568 screen
->is_format_supported(screen
, dest_format
,
1570 PIPE_BIND_RENDER_TARGET
)) {
1571 /* draw textured quad to do the copy */
1573 struct pipe_surface surf_tmpl
;
1574 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1575 surf_tmpl
.format
= util_format_linear(stImage
->pt
->format
);
1576 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
1577 surf_tmpl
.u
.tex
.level
= stImage
->base
.Level
;
1578 surf_tmpl
.u
.tex
.first_layer
= stImage
->base
.Face
+ destZ
;
1579 surf_tmpl
.u
.tex
.last_layer
= stImage
->base
.Face
+ destZ
;
1581 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1585 srcY1
= strb
->Base
.Height
- srcY
- height
;
1586 srcY0
= srcY1
+ height
;
1590 srcY1
= srcY0
+ height
;
1593 /* Disable conditional rendering. */
1594 if (st
->render_condition
) {
1595 pipe
->render_condition(pipe
, NULL
, 0);
1598 util_blit_pixels_writemask(st
->blit
,
1600 strb
->surface
->u
.tex
.level
,
1602 srcX
+ width
, srcY1
,
1603 strb
->surface
->u
.tex
.first_layer
,
1606 destX
+ width
, destY
+ height
,
1607 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1610 /* Restore conditional rendering state. */
1611 if (st
->render_condition
) {
1612 pipe
->render_condition(pipe
, st
->render_condition
,
1613 st
->condition_mode
);
1616 use_fallback
= GL_FALSE
;
1620 pipe_surface_reference(&dest_surface
, NULL
);
1624 /* software fallback */
1625 fallback_copy_texsubimage(ctx
, target
, level
,
1626 strb
, stImage
, texBaseFormat
,
1627 destX
, destY
, destZ
,
1628 srcX
, srcY
, width
, height
);
1635 st_CopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1636 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1638 const GLint yoffset
= 0, zoffset
= 0;
1639 const GLsizei height
= 1;
1640 st_copy_texsubimage(ctx
, target
, level
,
1641 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1642 x
, y
, width
, height
); /* src X, Y, size */
1647 st_CopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1648 GLint xoffset
, GLint yoffset
,
1649 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1651 const GLint zoffset
= 0;
1652 st_copy_texsubimage(ctx
, target
, level
,
1653 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1654 x
, y
, width
, height
); /* src X, Y, size */
1659 st_CopyTexSubImage3D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1660 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1661 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1663 st_copy_texsubimage(ctx
, target
, level
,
1664 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1665 x
, y
, width
, height
); /* src X, Y, size */
1670 * Copy image data from stImage into the texture object 'stObj' at level
1674 copy_image_data_to_texture(struct st_context
*st
,
1675 struct st_texture_object
*stObj
,
1677 struct st_texture_image
*stImage
)
1681 const struct gl_texture_image
*dstImage
=
1682 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
1684 assert(dstImage
->Width
== stImage
->base
.Width
);
1685 assert(dstImage
->Height
== stImage
->base
.Height
);
1686 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1690 /* Copy potentially with the blitter:
1692 st_texture_image_copy(st
->pipe
,
1693 stObj
->pt
, dstLevel
, /* dest texture, level */
1694 stImage
->pt
, stImage
->base
.Level
, /* src texture, level */
1695 stImage
->base
.Face
);
1697 pipe_resource_reference(&stImage
->pt
, NULL
);
1699 else if (stImage
->base
.Data
) {
1700 st_texture_image_data(st
,
1705 stImage
->base
.RowStride
*
1706 util_format_get_blocksize(stObj
->pt
->format
),
1707 stImage
->base
.RowStride
*
1708 stImage
->base
.Height
*
1709 util_format_get_blocksize(stObj
->pt
->format
));
1710 _mesa_align_free(stImage
->base
.Data
);
1711 stImage
->base
.Data
= NULL
;
1714 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1719 * Called during state validation. When this function is finished,
1720 * the texture object should be ready for rendering.
1721 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1724 st_finalize_texture(struct gl_context
*ctx
,
1725 struct pipe_context
*pipe
,
1726 struct gl_texture_object
*tObj
)
1728 struct st_context
*st
= st_context(ctx
);
1729 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1730 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1732 struct st_texture_image
*firstImage
;
1733 enum pipe_format firstImageFormat
;
1734 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1736 if (stObj
->base
._Complete
) {
1737 /* The texture is complete and we know exactly how many mipmap levels
1738 * are present/needed. This is conditional because we may be called
1739 * from the st_generate_mipmap() function when the texture object is
1740 * incomplete. In that case, we'll have set stObj->lastLevel before
1743 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1744 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1745 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1747 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1750 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1753 /* If both firstImage and stObj point to a texture which can contain
1754 * all active images, favour firstImage. Note that because of the
1755 * completeness requirement, we know that the image dimensions
1758 if (firstImage
->pt
&&
1759 firstImage
->pt
!= stObj
->pt
&&
1760 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1761 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1762 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1765 /* Find gallium format for the Mesa texture */
1766 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1768 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1770 GLuint width
, height
, depth
;
1771 if (!guess_base_level_size(stObj
->base
.Target
,
1772 firstImage
->base
.Width2
,
1773 firstImage
->base
.Height2
,
1774 firstImage
->base
.Depth2
,
1775 stObj
->base
.BaseLevel
,
1776 &width
, &height
, &depth
)) {
1777 width
= stObj
->width0
;
1778 height
= stObj
->height0
;
1779 depth
= stObj
->depth0
;
1781 /* convert GL dims to Gallium dims */
1782 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1783 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1786 /* If we already have a gallium texture, check that it matches the texture
1787 * object's format, target, size, num_levels, etc.
1790 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1791 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1792 stObj
->pt
->last_level
< stObj
->lastLevel
||
1793 stObj
->pt
->width0
!= ptWidth
||
1794 stObj
->pt
->height0
!= ptHeight
||
1795 stObj
->pt
->depth0
!= ptDepth
||
1796 stObj
->pt
->array_size
!= ptLayers
)
1798 /* The gallium texture does not match the Mesa texture so delete the
1799 * gallium texture now. We'll make a new one below.
1801 pipe_resource_reference(&stObj
->pt
, NULL
);
1802 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1803 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1807 /* May need to create a new gallium texture:
1810 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1812 stObj
->pt
= st_texture_create(st
,
1813 gl_target_to_pipe(stObj
->base
.Target
),
1823 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1828 /* Pull in any images not in the object's texture:
1830 for (face
= 0; face
< nr_faces
; face
++) {
1832 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1833 struct st_texture_image
*stImage
=
1834 st_texture_image(stObj
->base
.Image
[face
][level
]);
1836 /* Need to import images in main memory or held in other textures.
1838 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1840 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1841 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1842 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
1843 /* src image fits expected dest mipmap level size */
1844 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1855 * Returns pointer to a default/dummy texture.
1856 * This is typically used when the current shader has tex/sample instructions
1857 * but the user has not provided a (any) texture(s).
1859 struct gl_texture_object
*
1860 st_get_default_texture(struct st_context
*st
)
1862 if (!st
->default_texture
) {
1863 static const GLenum target
= GL_TEXTURE_2D
;
1864 GLubyte pixels
[16][16][4];
1865 struct gl_texture_object
*texObj
;
1866 struct gl_texture_image
*texImg
;
1869 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1870 * when attempting to sample incomplete textures.
1872 for (i
= 0; i
< 16; i
++) {
1873 for (j
= 0; j
< 16; j
++) {
1874 pixels
[i
][j
][0] = 0;
1875 pixels
[i
][j
][1] = 0;
1876 pixels
[i
][j
][2] = 0;
1877 pixels
[i
][j
][3] = 255;
1881 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1883 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1885 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1886 16, 16, 1, 0, /* w, h, d, border */
1887 GL_RGBA
, MESA_FORMAT_RGBA8888
);
1889 st_TexImage(st
->ctx
, 2, target
,
1890 0, GL_RGBA
, /* level, intformat */
1891 16, 16, 1, 0, /* w, h, d, border */
1892 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1893 &st
->ctx
->DefaultPacking
,
1897 texObj
->Sampler
.MinFilter
= GL_NEAREST
;
1898 texObj
->Sampler
.MagFilter
= GL_NEAREST
;
1899 texObj
->_Complete
= GL_TRUE
;
1901 st
->default_texture
= texObj
;
1903 return st
->default_texture
;
1908 st_init_texture_functions(struct dd_function_table
*functions
)
1910 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1911 functions
->TexImage1D
= st_TexImage1D
;
1912 functions
->TexImage2D
= st_TexImage2D
;
1913 functions
->TexImage3D
= st_TexImage3D
;
1914 functions
->TexSubImage1D
= st_TexSubImage1D
;
1915 functions
->TexSubImage2D
= st_TexSubImage2D
;
1916 functions
->TexSubImage3D
= st_TexSubImage3D
;
1917 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1918 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1919 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1920 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1921 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1922 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1923 functions
->GenerateMipmap
= st_generate_mipmap
;
1925 functions
->GetTexImage
= st_GetTexImage
;
1927 /* compressed texture functions */
1928 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1929 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1931 functions
->NewTextureObject
= st_NewTextureObject
;
1932 functions
->NewTextureImage
= st_NewTextureImage
;
1933 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
1934 functions
->DeleteTexture
= st_DeleteTextureObject
;
1935 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
1936 functions
->MapTextureImage
= st_MapTextureImage
;
1937 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
1939 functions
->TextureMemCpy
= do_memcpy
;
1941 /* XXX Temporary until we can query pipe's texture sizes */
1942 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;