1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "main/mfeatures.h"
29 #include "main/bufferobj.h"
30 #include "main/enums.h"
31 #include "main/fbobject.h"
32 #include "main/formats.h"
33 #include "main/image.h"
34 #include "main/imports.h"
35 #include "main/macros.h"
36 #include "main/mipmap.h"
37 #include "main/pack.h"
39 #include "main/pixeltransfer.h"
40 #include "main/texcompress.h"
41 #include "main/texfetch.h"
42 #include "main/texgetimage.h"
43 #include "main/teximage.h"
44 #include "main/texobj.h"
45 #include "main/texstore.h"
47 #include "state_tracker/st_debug.h"
48 #include "state_tracker/st_context.h"
49 #include "state_tracker/st_cb_fbo.h"
50 #include "state_tracker/st_cb_flush.h"
51 #include "state_tracker/st_cb_texture.h"
52 #include "state_tracker/st_format.h"
53 #include "state_tracker/st_texture.h"
54 #include "state_tracker/st_gen_mipmap.h"
55 #include "state_tracker/st_atom.h"
57 #include "pipe/p_context.h"
58 #include "pipe/p_defines.h"
59 #include "util/u_inlines.h"
60 #include "pipe/p_shader_tokens.h"
61 #include "util/u_tile.h"
62 #include "util/u_blit.h"
63 #include "util/u_format.h"
64 #include "util/u_surface.h"
65 #include "util/u_sampler.h"
66 #include "util/u_math.h"
67 #include "util/u_box.h"
69 #define DBG if (0) printf
72 static enum pipe_texture_target
73 gl_target_to_pipe(GLenum target
)
77 return PIPE_TEXTURE_1D
;
79 return PIPE_TEXTURE_2D
;
80 case GL_TEXTURE_RECTANGLE_NV
:
81 return PIPE_TEXTURE_RECT
;
83 return PIPE_TEXTURE_3D
;
84 case GL_TEXTURE_CUBE_MAP_ARB
:
85 return PIPE_TEXTURE_CUBE
;
86 case GL_TEXTURE_1D_ARRAY_EXT
:
87 return PIPE_TEXTURE_1D_ARRAY
;
88 case GL_TEXTURE_2D_ARRAY_EXT
:
89 return PIPE_TEXTURE_2D_ARRAY
;
90 case GL_TEXTURE_BUFFER
:
99 /** called via ctx->Driver.NewTextureImage() */
100 static struct gl_texture_image
*
101 st_NewTextureImage(struct gl_context
* ctx
)
103 DBG("%s\n", __FUNCTION__
);
105 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
109 /** called via ctx->Driver.NewTextureObject() */
110 static struct gl_texture_object
*
111 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
113 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
115 DBG("%s\n", __FUNCTION__
);
116 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
121 /** called via ctx->Driver.DeleteTextureObject() */
123 st_DeleteTextureObject(struct gl_context
*ctx
,
124 struct gl_texture_object
*texObj
)
126 struct st_context
*st
= st_context(ctx
);
127 struct st_texture_object
*stObj
= st_texture_object(texObj
);
129 pipe_resource_reference(&stObj
->pt
, NULL
);
130 if (stObj
->sampler_view
) {
131 if (stObj
->sampler_view
->context
!= st
->pipe
) {
132 /* Take "ownership" of this texture sampler view by setting
133 * its context pointer to this context. This avoids potential
134 * crashes when the texture object is shared among contexts
135 * and the original/owner context has already been destroyed.
137 stObj
->sampler_view
->context
= st
->pipe
;
139 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
141 _mesa_delete_texture_object(ctx
, texObj
);
145 /** called via ctx->Driver.FreeTextureImageBuffer() */
147 st_FreeTextureImageBuffer(struct gl_context
* ctx
, struct gl_texture_image
*texImage
)
149 struct st_texture_image
*stImage
= st_texture_image(texImage
);
151 DBG("%s\n", __FUNCTION__
);
154 pipe_resource_reference(&stImage
->pt
, NULL
);
157 if (texImage
->Data
) {
158 _mesa_align_free(texImage
->Data
);
159 texImage
->Data
= NULL
;
164 /** called via ctx->Driver.MapTextureImage() */
166 st_MapTextureImage(struct gl_context
*ctx
,
167 struct gl_texture_image
*texImage
,
168 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
170 GLubyte
**mapOut
, GLint
*rowStrideOut
)
172 struct st_context
*st
= st_context(ctx
);
173 struct st_texture_image
*stImage
= st_texture_image(texImage
);
178 if (mode
& GL_MAP_READ_BIT
)
179 pipeMode
|= PIPE_TRANSFER_READ
;
180 if (mode
& GL_MAP_WRITE_BIT
)
181 pipeMode
|= PIPE_TRANSFER_WRITE
;
183 map
= st_texture_image_map(st
, stImage
, slice
, pipeMode
, x
, y
, w
, h
);
186 *rowStrideOut
= stImage
->transfer
->stride
;
195 /** called via ctx->Driver.UnmapTextureImage() */
197 st_UnmapTextureImage(struct gl_context
*ctx
,
198 struct gl_texture_image
*texImage
,
201 struct st_context
*st
= st_context(ctx
);
202 struct st_texture_image
*stImage
= st_texture_image(texImage
);
203 st_texture_image_unmap(st
, stImage
);
208 * From linux kernel i386 header files, copes with odd sizes better
209 * than COPY_DWORDS would:
210 * XXX Put this in src/mesa/main/imports.h ???
212 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
214 __memcpy(void *to
, const void *from
, size_t n
)
217 __asm__
__volatile__("rep ; movsl\n\t"
221 "1:\ttestb $1,%b4\n\t"
223 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
224 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
229 #define __memcpy(a,b,c) memcpy(a,b,c)
234 * The system memcpy (at least on ubuntu 5.10) has problems copying
235 * to agp (writecombined) memory from a source which isn't 64-byte
236 * aligned - there is a 4x performance falloff.
238 * The x86 __memcpy is immune to this but is slightly slower
239 * (10%-ish) than the system memcpy.
241 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
242 * isn't much faster than x86_memcpy for agp copies.
244 * TODO: switch dynamically.
247 do_memcpy(void *dest
, const void *src
, size_t n
)
249 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
250 return __memcpy(dest
, src
, n
);
253 return memcpy(dest
, src
, n
);
258 * Return default texture resource binding bitmask for the given format.
261 default_bindings(struct st_context
*st
, enum pipe_format format
)
263 struct pipe_screen
*screen
= st
->pipe
->screen
;
264 const unsigned target
= PIPE_TEXTURE_2D
;
267 if (util_format_is_depth_or_stencil(format
))
268 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
270 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
272 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
276 format
= util_format_linear(format
);
278 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
281 return PIPE_BIND_SAMPLER_VIEW
;
286 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
288 get_texture_dims(GLenum target
)
292 case GL_TEXTURE_1D_ARRAY_EXT
:
293 case GL_TEXTURE_BUFFER
:
296 case GL_TEXTURE_CUBE_MAP_ARB
:
297 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
298 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
299 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
300 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
301 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
302 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
303 case GL_TEXTURE_RECTANGLE_NV
:
304 case GL_TEXTURE_2D_ARRAY_EXT
:
309 assert(0 && "invalid texture target in get_texture_dims()");
316 * Given the size of a mipmap image, try to compute the size of the level=0
319 * Note that this isn't always accurate for odd-sized, non-POW textures.
320 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
322 * \return GL_TRUE for success, GL_FALSE for failure
325 guess_base_level_size(GLenum target
,
326 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
327 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
329 const GLuint dims
= get_texture_dims(target
);
336 /* Depending on the image's size, we can't always make a guess here */
337 if ((dims
>= 1 && width
== 1) ||
338 (dims
>= 2 && height
== 1) ||
339 (dims
>= 3 && depth
== 1)) {
340 /* we can't determine the image size at level=0 */
344 /* grow the image size until we hit level = 0 */
365 * Try to allocate a pipe_resource object for the given st_texture_object.
367 * We use the given st_texture_image as a clue to determine the size of the
368 * mipmap image at level=0.
370 * \return GL_TRUE for success, GL_FALSE if out of memory.
373 guess_and_alloc_texture(struct st_context
*st
,
374 struct st_texture_object
*stObj
,
375 const struct st_texture_image
*stImage
)
377 GLuint lastLevel
, width
, height
, depth
;
379 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
380 enum pipe_format fmt
;
382 DBG("%s\n", __FUNCTION__
);
386 if (!guess_base_level_size(stObj
->base
.Target
,
387 stImage
->base
.Width2
,
388 stImage
->base
.Height2
,
389 stImage
->base
.Depth2
,
391 &width
, &height
, &depth
)) {
392 /* we can't determine the image size at level=0 */
393 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
394 /* this is not an out of memory error */
398 /* At this point, (width x height x depth) is the expected size of
399 * the level=0 mipmap image.
402 /* Guess a reasonable value for lastLevel. With OpenGL we have no
403 * idea how many mipmap levels will be in a texture until we start
404 * to render with it. Make an educated guess here but be prepared
405 * to re-allocating a texture buffer with space for more (or fewer)
406 * mipmap levels later.
408 if ((stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
409 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
410 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
411 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
412 !stObj
->base
.GenerateMipmap
&&
413 stImage
->base
.Level
== 0) {
414 /* only alloc space for a single mipmap level */
418 /* alloc space for a full mipmap */
419 GLuint l2width
= util_logbase2(width
);
420 GLuint l2height
= util_logbase2(height
);
421 GLuint l2depth
= util_logbase2(depth
);
422 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
425 /* Save the level=0 dimensions */
426 stObj
->width0
= width
;
427 stObj
->height0
= height
;
428 stObj
->depth0
= depth
;
430 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
432 bindings
= default_bindings(st
, fmt
);
434 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
435 width
, height
, depth
,
436 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
438 stObj
->pt
= st_texture_create(st
,
439 gl_target_to_pipe(stObj
->base
.Target
),
448 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
450 return stObj
->pt
!= NULL
;
455 * Adjust pixel unpack params and image dimensions to strip off the
457 * Gallium doesn't support texture borders. They've seldem been used
458 * and seldom been implemented correctly anyway.
459 * \param unpackNew returns the new pixel unpack parameters
462 strip_texture_border(GLint border
,
463 GLint
*width
, GLint
*height
, GLint
*depth
,
464 const struct gl_pixelstore_attrib
*unpack
,
465 struct gl_pixelstore_attrib
*unpackNew
)
467 assert(border
> 0); /* sanity check */
469 *unpackNew
= *unpack
;
471 if (unpackNew
->RowLength
== 0)
472 unpackNew
->RowLength
= *width
;
474 if (depth
&& unpackNew
->ImageHeight
== 0)
475 unpackNew
->ImageHeight
= *height
;
477 unpackNew
->SkipPixels
+= border
;
479 unpackNew
->SkipRows
+= border
;
481 unpackNew
->SkipImages
+= border
;
484 *width
= *width
- 2 * border
;
485 if (height
&& *height
>= 3)
486 *height
= *height
- 2 * border
;
487 if (depth
&& *depth
>= 3)
488 *depth
= *depth
- 2 * border
;
493 * Do glTexImage1/2/3D().
496 st_TexImage(struct gl_context
* ctx
,
498 GLenum target
, GLint level
,
499 GLint internalFormat
,
500 GLint width
, GLint height
, GLint depth
,
502 GLenum format
, GLenum type
, const void *pixels
,
503 const struct gl_pixelstore_attrib
*unpack
,
504 struct gl_texture_object
*texObj
,
505 struct gl_texture_image
*texImage
,
506 GLsizei imageSize
, GLboolean compressed_src
)
508 struct st_context
*st
= st_context(ctx
);
509 struct st_texture_object
*stObj
= st_texture_object(texObj
);
510 struct st_texture_image
*stImage
= st_texture_image(texImage
);
511 GLuint dstRowStride
= 0;
512 struct gl_pixelstore_attrib unpackNB
;
513 enum pipe_transfer_usage transfer_usage
= 0;
515 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
516 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
518 /* switch to "normal" */
519 if (stObj
->surface_based
) {
522 _mesa_clear_texture_object(ctx
, texObj
);
523 pipe_resource_reference(&stObj
->pt
, NULL
);
525 /* oops, need to init this image again */
526 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
527 internalFormat
, format
, type
);
529 _mesa_init_teximage_fields(ctx
, target
, texImage
,
530 width
, height
, depth
, border
,
531 internalFormat
, texFormat
);
533 stObj
->surface_based
= GL_FALSE
;
536 /* gallium does not support texture borders, strip it off */
538 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
540 texImage
->Width
= width
;
541 texImage
->Height
= height
;
542 texImage
->Depth
= depth
;
543 texImage
->Border
= 0;
547 assert(texImage
->Width
== width
);
548 assert(texImage
->Height
== height
);
549 assert(texImage
->Depth
== depth
);
552 stImage
->base
.Face
= _mesa_tex_target_to_face(target
);
553 stImage
->base
.Level
= level
;
555 _mesa_set_fetch_functions(texImage
, dims
);
557 /* Release the reference to a potentially orphaned buffer.
558 * Release any old malloced memory.
561 pipe_resource_reference(&stImage
->pt
, NULL
);
562 assert(!texImage
->Data
);
564 else if (texImage
->Data
) {
565 _mesa_align_free(texImage
->Data
);
569 * See if the new image is somehow incompatible with the existing
570 * mipmap. If so, free the old mipmap.
573 if (level
> (GLint
) stObj
->pt
->last_level
||
574 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
575 stImage
->base
.Face
, stImage
->base
.Level
)) {
577 pipe_resource_reference(&stObj
->pt
, NULL
);
579 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
583 if (width
== 0 || height
== 0 || depth
== 0) {
584 /* stop after freeing old image */
589 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
590 /* Probably out of memory.
591 * Try flushing any pending rendering, then retry.
594 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
595 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
601 assert(!stImage
->pt
);
603 /* Check if this texture image can live inside the texture object's buffer.
604 * If so, store the image there. Otherwise the image will temporarily live
608 st_texture_match_image(stObj
->pt
, &stImage
->base
,
609 stImage
->base
.Face
, stImage
->base
.Level
)) {
611 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
616 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
618 /* Pixel data may come from regular user memory or a PBO. For the later,
619 * do bounds checking and map the PBO to read pixels data from it.
621 * XXX we should try to use a GPU-accelerated path to copy the image data
622 * from the PBO to the texture.
624 if (compressed_src
) {
625 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
627 "glCompressedTexImage");
630 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
632 pixels
, unpack
, "glTexImage");
635 /* for a 1D array upload the image as a series of layer with height = 1 */
636 if (target
== GL_TEXTURE_1D_ARRAY
) {
642 * Prepare to store the texture data. Either map the gallium texture buffer
643 * memory or malloc space for it.
647 /* We've allocated texture resource, but have no pixel data - all done. */
651 /* Store the image in the gallium transfer object */
652 if (format
== GL_DEPTH_COMPONENT
&&
653 util_format_is_depth_and_stencil(stImage
->pt
->format
))
654 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
656 transfer_usage
= PIPE_TRANSFER_WRITE
;
658 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
659 transfer_usage
, 0, 0, width
, height
);
660 if(stImage
->transfer
)
661 dstRowStride
= stImage
->transfer
->stride
;
664 /* Allocate regular memory and store the image there temporarily. */
665 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
666 width
, height
, depth
);
667 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
669 texImage
->Data
= _mesa_align_malloc(imageSize
, 16);
672 if (!texImage
->Data
) {
673 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
678 /* We've allocated texture memory, but have no pixel data - all done. */
682 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
683 width
, height
, depth
, width
, dstRowStride
);
685 /* Copy user texture image into the texture buffer.
687 if (compressed_src
) {
688 const GLuint srcRowStride
=
689 _mesa_format_row_stride(texImage
->TexFormat
, width
);
690 if (dstRowStride
== srcRowStride
) {
691 memcpy(texImage
->Data
, pixels
, imageSize
);
694 char *dst
= texImage
->Data
;
695 const char *src
= pixels
;
696 GLuint i
, bw
, bh
, lines
;
697 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
698 lines
= (height
+ bh
- 1) / bh
;
700 for (i
= 0; i
< lines
; ++i
) {
701 memcpy(dst
, src
, srcRowStride
);
708 const GLuint srcImageStride
=
709 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
711 const GLubyte
*src
= (const GLubyte
*) pixels
;
713 for (i
= 0; i
< depth
; i
++) {
714 if (!_mesa_texstore(ctx
, dims
,
715 texImage
->_BaseFormat
,
718 0, 0, 0, /* dstX/Y/Zoffset */
720 texImage
->ImageOffsets
,
722 format
, type
, src
, unpack
)) {
723 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
726 if (stImage
->pt
&& i
+ 1 < depth
) {
727 /* unmap this slice */
728 st_texture_image_unmap(st
, stImage
);
729 /* map next slice of 3D texture */
730 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
731 transfer_usage
, 0, 0,
733 src
+= srcImageStride
;
739 _mesa_unmap_teximage_pbo(ctx
, unpack
);
741 if (stImage
->pt
&& texImage
->Data
) {
742 st_texture_image_unmap(st
, stImage
);
743 texImage
->Data
= NULL
;
749 st_TexImage3D(struct gl_context
* ctx
,
750 GLenum target
, GLint level
,
751 GLint internalFormat
,
752 GLint width
, GLint height
, GLint depth
,
754 GLenum format
, GLenum type
, const void *pixels
,
755 const struct gl_pixelstore_attrib
*unpack
,
756 struct gl_texture_object
*texObj
,
757 struct gl_texture_image
*texImage
)
759 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
760 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
766 st_TexImage2D(struct gl_context
* ctx
,
767 GLenum target
, GLint level
,
768 GLint internalFormat
,
769 GLint width
, GLint height
, GLint border
,
770 GLenum format
, GLenum type
, const void *pixels
,
771 const struct gl_pixelstore_attrib
*unpack
,
772 struct gl_texture_object
*texObj
,
773 struct gl_texture_image
*texImage
)
775 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
776 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
781 st_TexImage1D(struct gl_context
* ctx
,
782 GLenum target
, GLint level
,
783 GLint internalFormat
,
784 GLint width
, GLint border
,
785 GLenum format
, GLenum type
, const void *pixels
,
786 const struct gl_pixelstore_attrib
*unpack
,
787 struct gl_texture_object
*texObj
,
788 struct gl_texture_image
*texImage
)
790 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
791 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
796 st_CompressedTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
797 GLint internalFormat
,
798 GLint width
, GLint height
, GLint border
,
799 GLsizei imageSize
, const GLvoid
*data
,
800 struct gl_texture_object
*texObj
,
801 struct gl_texture_image
*texImage
)
803 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
804 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
810 * glGetTexImage() helper: decompress a compressed texture by rendering
811 * a textured quad. Store the results in the user's buffer.
814 decompress_with_blit(struct gl_context
* ctx
, GLenum target
, GLint level
,
815 GLenum format
, GLenum type
, GLvoid
*pixels
,
816 struct gl_texture_object
*texObj
,
817 struct gl_texture_image
*texImage
)
819 struct st_context
*st
= st_context(ctx
);
820 struct pipe_context
*pipe
= st
->pipe
;
821 struct st_texture_image
*stImage
= st_texture_image(texImage
);
822 struct st_texture_object
*stObj
= st_texture_object(texObj
);
823 struct pipe_sampler_view
*src_view
=
824 st_get_texture_sampler_view(stObj
, pipe
);
825 const GLuint width
= texImage
->Width
;
826 const GLuint height
= texImage
->Height
;
827 struct pipe_surface
*dst_surface
;
828 struct pipe_resource
*dst_texture
;
829 struct pipe_transfer
*tex_xfer
;
830 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
831 PIPE_BIND_TRANSFER_READ
);
833 /* create temp / dest surface */
834 if (!util_create_rgba_surface(pipe
, width
, height
, bind
,
835 &dst_texture
, &dst_surface
)) {
836 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
837 "in decompress_with_blit()");
841 /* Disable conditional rendering. */
842 if (st
->render_condition
) {
843 pipe
->render_condition(pipe
, NULL
, 0);
846 /* blit/render/decompress */
847 util_blit_pixels_tex(st
->blit
,
848 src_view
, /* pipe_resource (src) */
849 0, 0, /* src x0, y0 */
850 width
, height
, /* src x1, y1 */
851 dst_surface
, /* pipe_surface (dst) */
852 0, 0, /* dst x0, y0 */
853 width
, height
, /* dst x1, y1 */
855 PIPE_TEX_MIPFILTER_NEAREST
);
857 /* Restore conditional rendering state. */
858 if (st
->render_condition
) {
859 pipe
->render_condition(pipe
, st
->render_condition
,
863 /* map the dst_surface so we can read from it */
864 tex_xfer
= pipe_get_transfer(pipe
,
867 0, 0, width
, height
);
869 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
871 /* copy/pack data into user buffer */
872 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
874 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
875 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
877 for (row
= 0; row
< height
; row
++) {
878 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
879 height
, format
, type
, row
, 0);
880 memcpy(dest
, map
, bytesPerRow
);
881 map
+= tex_xfer
->stride
;
883 pipe_transfer_unmap(pipe
, tex_xfer
);
886 /* format translation via floats */
888 enum pipe_format pformat
= util_format_linear(dst_texture
->format
);
889 for (row
= 0; row
< height
; row
++) {
890 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
891 GLfloat rgba
[4 * MAX_WIDTH
];
892 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
893 height
, format
, type
, row
, 0);
895 if (ST_DEBUG
& DEBUG_FALLBACK
)
896 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
898 /* get float[4] rgba row from surface */
899 pipe_get_tile_rgba_format(pipe
, tex_xfer
, 0, row
, width
, 1,
902 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
903 type
, dest
, &ctx
->Pack
, transferOps
);
907 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
909 pipe
->transfer_destroy(pipe
, tex_xfer
);
911 /* destroy the temp / dest surface */
912 util_destroy_rgba_surface(dst_texture
, dst_surface
);
918 * Need to map texture image into memory before copying image data,
922 st_get_tex_image(struct gl_context
* ctx
, GLenum target
, GLint level
,
923 GLenum format
, GLenum type
, GLvoid
* pixels
,
924 struct gl_texture_object
*texObj
,
925 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
927 struct st_context
*st
= st_context(ctx
);
928 struct st_texture_image
*stImage
= st_texture_image(texImage
);
929 const GLuint dstImageStride
=
930 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
936 util_format_is_s3tc(stImage
->pt
->format
) &&
938 /* Need to decompress the texture.
939 * We'll do this by rendering a textured quad.
940 * Note that we only expect RGBA formats (no Z/depth formats).
942 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
949 /* Image is stored in hardware format in a buffer managed by the
950 * kernel. Need to explicitly map and unmap it.
952 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
953 PIPE_TRANSFER_READ
, 0, 0,
955 stImage
->base
.Height
);
956 /* compute stride in texels from stride in bytes */
957 texImage
->RowStride
= stImage
->transfer
->stride
958 * util_format_get_blockwidth(stImage
->pt
->format
)
959 / util_format_get_blocksize(stImage
->pt
->format
);
962 /* Otherwise, the image should actually be stored in
963 * texImage->Data. This is pretty confusing for
964 * everybody, I'd much prefer to separate the two functions of
965 * texImage->Data - storage for texture images in main memory
966 * and access (ie mappings) of images. In other words, we'd
967 * create a new texImage->Map field and leave Data simply for
970 assert(texImage
->Data
);
973 depth
= texImage
->Depth
;
976 dest
= (GLubyte
*) pixels
;
978 _mesa_set_fetch_functions(texImage
, get_texture_dims(target
));
980 for (i
= 0; i
< depth
; i
++) {
981 if (compressed_dst
) {
982 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
986 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
990 if (stImage
->pt
&& i
+ 1 < depth
) {
991 /* unmap this slice */
992 st_texture_image_unmap(st
, stImage
);
993 /* map next slice of 3D texture */
994 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
995 PIPE_TRANSFER_READ
, 0, 0,
997 stImage
->base
.Height
);
998 dest
+= dstImageStride
;
1002 texImage
->Depth
= depth
;
1006 st_texture_image_unmap(st
, stImage
);
1007 texImage
->Data
= NULL
;
1013 st_GetTexImage(struct gl_context
* ctx
, GLenum target
, GLint level
,
1014 GLenum format
, GLenum type
, GLvoid
* pixels
,
1015 struct gl_texture_object
*texObj
,
1016 struct gl_texture_image
*texImage
)
1018 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1024 st_GetCompressedTexImage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1026 struct gl_texture_object
*texObj
,
1027 struct gl_texture_image
*texImage
)
1029 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1036 st_TexSubimage(struct gl_context
*ctx
, GLint dims
, GLenum target
, GLint level
,
1037 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1038 GLint width
, GLint height
, GLint depth
,
1039 GLenum format
, GLenum type
, const void *pixels
,
1040 const struct gl_pixelstore_attrib
*packing
,
1041 struct gl_texture_object
*texObj
,
1042 struct gl_texture_image
*texImage
)
1044 struct st_context
*st
= st_context(ctx
);
1045 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1046 GLuint dstRowStride
;
1047 const GLuint srcImageStride
=
1048 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1051 /* init to silence warning only: */
1052 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1054 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1055 _mesa_lookup_enum_by_nr(target
),
1056 level
, xoffset
, yoffset
, width
, height
);
1059 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1060 type
, pixels
, packing
, "glTexSubImage2D");
1064 /* for a 1D array upload the image as a series of layer with height = 1 */
1065 if (target
== GL_TEXTURE_1D_ARRAY
) {
1070 /* Map buffer if necessary. Need to lock to prevent other contexts
1071 * from uploading the buffer under us.
1074 if (format
== GL_DEPTH_COMPONENT
&&
1075 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1076 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1078 transfer_usage
= PIPE_TRANSFER_WRITE
;
1080 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1086 if (!texImage
->Data
) {
1087 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1091 src
= (const GLubyte
*) pixels
;
1092 dstRowStride
= stImage
->transfer
->stride
;
1094 for (i
= 0; i
< depth
; i
++) {
1095 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1096 texImage
->TexFormat
,
1100 texImage
->ImageOffsets
,
1102 format
, type
, src
, packing
)) {
1103 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1106 if (stImage
->pt
&& i
+ 1 < depth
) {
1107 /* unmap this slice */
1108 st_texture_image_unmap(st
, stImage
);
1109 /* map next slice of 3D texture */
1110 texImage
->Data
= st_texture_image_map(st
, stImage
,
1115 src
+= srcImageStride
;
1120 _mesa_unmap_teximage_pbo(ctx
, packing
);
1122 if (stImage
->pt
&& texImage
->Data
) {
1123 st_texture_image_unmap(st
, stImage
);
1124 texImage
->Data
= NULL
;
1131 st_TexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1132 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1133 GLsizei width
, GLsizei height
, GLsizei depth
,
1134 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1135 const struct gl_pixelstore_attrib
*packing
,
1136 struct gl_texture_object
*texObj
,
1137 struct gl_texture_image
*texImage
)
1139 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1140 width
, height
, depth
, format
, type
,
1141 pixels
, packing
, texObj
, texImage
);
1146 st_TexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1147 GLint xoffset
, GLint yoffset
,
1148 GLsizei width
, GLsizei height
,
1149 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1150 const struct gl_pixelstore_attrib
*packing
,
1151 struct gl_texture_object
*texObj
,
1152 struct gl_texture_image
*texImage
)
1154 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1155 width
, height
, 1, format
, type
,
1156 pixels
, packing
, texObj
, texImage
);
1161 st_TexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1162 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1163 const GLvoid
* pixels
,
1164 const struct gl_pixelstore_attrib
*packing
,
1165 struct gl_texture_object
*texObj
,
1166 struct gl_texture_image
*texImage
)
1168 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1169 format
, type
, pixels
, packing
, texObj
, texImage
);
1174 st_CompressedTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1175 GLint xoffset
, GLsizei width
,
1177 GLsizei imageSize
, const GLvoid
*data
,
1178 struct gl_texture_object
*texObj
,
1179 struct gl_texture_image
*texImage
)
1186 st_CompressedTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1187 GLint xoffset
, GLint yoffset
,
1188 GLsizei width
, GLint height
,
1190 GLsizei imageSize
, const GLvoid
*data
,
1191 struct gl_texture_object
*texObj
,
1192 struct gl_texture_image
*texImage
)
1194 struct st_context
*st
= st_context(ctx
);
1195 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1199 enum pipe_format pformat
;
1202 pformat
= stImage
->pt
->format
;
1204 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1205 PIPE_TRANSFER_WRITE
,
1209 srcBlockStride
= util_format_get_stride(pformat
, width
);
1210 dstBlockStride
= stImage
->transfer
->stride
;
1212 assert(stImage
->pt
);
1213 /* TODO find good values for block and strides */
1214 /* TODO also adjust texImage->data for yoffset/xoffset */
1218 if (!texImage
->Data
) {
1219 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1223 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1224 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1226 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1227 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1228 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1229 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1230 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1234 st_texture_image_unmap(st
, stImage
);
1235 texImage
->Data
= NULL
;
1241 st_CompressedTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1242 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1243 GLsizei width
, GLint height
, GLint depth
,
1245 GLsizei imageSize
, const GLvoid
*data
,
1246 struct gl_texture_object
*texObj
,
1247 struct gl_texture_image
*texImage
)
1255 * Do a CopyTexSubImage operation using a read transfer from the source,
1256 * a write transfer to the destination and get_tile()/put_tile() to access
1257 * the pixels/texels.
1259 * Note: srcY=0=TOP of renderbuffer
1262 fallback_copy_texsubimage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1263 struct st_renderbuffer
*strb
,
1264 struct st_texture_image
*stImage
,
1266 GLint destX
, GLint destY
, GLint destZ
,
1267 GLint srcX
, GLint srcY
,
1268 GLsizei width
, GLsizei height
)
1270 struct st_context
*st
= st_context(ctx
);
1271 struct pipe_context
*pipe
= st
->pipe
;
1272 struct pipe_transfer
*src_trans
;
1274 enum pipe_transfer_usage transfer_usage
;
1276 if (ST_DEBUG
& DEBUG_FALLBACK
)
1277 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1279 assert(width
<= MAX_WIDTH
);
1281 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1282 srcY
= strb
->Base
.Height
- srcY
- height
;
1285 src_trans
= pipe_get_transfer(pipe
,
1288 strb
->rtt_face
+ strb
->rtt_slice
,
1293 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1294 baseFormat
== GL_DEPTH_STENCIL
) &&
1295 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1296 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1298 transfer_usage
= PIPE_TRANSFER_WRITE
;
1300 /* XXX this used to ignore destZ param */
1301 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
1302 destX
, destY
, width
, height
);
1304 if (baseFormat
== GL_DEPTH_COMPONENT
||
1305 baseFormat
== GL_DEPTH_STENCIL
) {
1306 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1307 ctx
->Pixel
.DepthBias
!= 0.0F
);
1310 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1311 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1320 /* To avoid a large temp memory allocation, do copy row by row */
1321 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1322 uint data
[MAX_WIDTH
];
1323 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1325 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1327 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1333 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1335 if (tempSrc
&& texDest
) {
1336 const GLint dims
= 2;
1337 const GLint dstRowStride
= stImage
->transfer
->stride
;
1338 struct gl_texture_image
*texImage
= &stImage
->base
;
1339 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1341 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1342 unpack
.Invert
= GL_TRUE
;
1345 /* get float/RGBA image from framebuffer */
1346 /* XXX this usually involves a lot of int/float conversion.
1347 * try to avoid that someday.
1349 pipe_get_tile_rgba_format(pipe
, src_trans
, 0, 0, width
, height
,
1350 util_format_linear(strb
->texture
->format
),
1353 /* Store into texture memory.
1354 * Note that this does some special things such as pixel transfer
1355 * ops and format conversion. In particular, if the dest tex format
1356 * is actually RGBA but the user created the texture as GL_RGB we
1357 * need to fill-in/override the alpha channel with 1.0.
1359 _mesa_texstore(ctx
, dims
,
1360 texImage
->_BaseFormat
,
1361 texImage
->TexFormat
,
1365 texImage
->ImageOffsets
,
1367 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1371 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1378 st_texture_image_unmap(st
, stImage
);
1379 pipe
->transfer_destroy(pipe
, src_trans
);
1385 * If the format of the src renderbuffer and the format of the dest
1386 * texture are compatible (in terms of blitting), return a TGSI writemask
1387 * to be used during the blit.
1388 * If the src/dest are incompatible, return 0.
1391 compatible_src_dst_formats(struct gl_context
*ctx
,
1392 const struct gl_renderbuffer
*src
,
1393 const struct gl_texture_image
*dst
)
1395 /* Get logical base formats for the src and dest.
1396 * That is, use the user-requested formats and not the actual, device-
1398 * For example, the user may have requested an A8 texture but the
1399 * driver may actually be using an RGBA texture format. When we
1400 * copy/blit to that texture, we only want to copy the Alpha channel
1401 * and not the RGB channels.
1403 * Similarly, when the src FBO was created an RGB format may have been
1404 * requested but the driver actually chose an RGBA format. In that case,
1405 * we don't want to copy the undefined Alpha channel to the dest texture
1406 * (it should be 1.0).
1408 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1409 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1412 * XXX when we have red-only and red/green renderbuffers we'll need
1413 * to add more cases here (or implement a general-purpose routine that
1414 * queries the existance of the R,G,B,A channels in the src and dest).
1416 if (srcFormat
== dstFormat
) {
1417 /* This is the same as matching_base_formats, which should
1418 * always pass, as it did previously.
1420 return TGSI_WRITEMASK_XYZW
;
1422 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1423 /* Make sure that A in the dest is 1. The actual src format
1424 * may be RGBA and have undefined A values.
1426 return TGSI_WRITEMASK_XYZ
;
1428 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1429 /* Make sure that A in the dest is 1. The actual dst format
1430 * may be RGBA and will need A=1 to provide proper alpha values
1431 * when sampled later.
1433 return TGSI_WRITEMASK_XYZ
;
1436 if (ST_DEBUG
& DEBUG_FALLBACK
)
1437 debug_printf("%s failed for src %s, dst %s\n",
1439 _mesa_lookup_enum_by_nr(srcFormat
),
1440 _mesa_lookup_enum_by_nr(dstFormat
));
1451 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1452 * Note that the region to copy has already been clipped so we know we
1453 * won't read from outside the source renderbuffer's bounds.
1455 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1458 st_copy_texsubimage(struct gl_context
*ctx
,
1459 GLenum target
, GLint level
,
1460 GLint destX
, GLint destY
, GLint destZ
,
1461 GLint srcX
, GLint srcY
,
1462 GLsizei width
, GLsizei height
)
1464 struct gl_texture_unit
*texUnit
=
1465 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1466 struct gl_texture_object
*texObj
=
1467 _mesa_select_tex_object(ctx
, texUnit
, target
);
1468 struct gl_texture_image
*texImage
=
1469 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1470 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1471 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1472 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1473 struct st_renderbuffer
*strb
;
1474 struct st_context
*st
= st_context(ctx
);
1475 struct pipe_context
*pipe
= st
->pipe
;
1476 struct pipe_screen
*screen
= pipe
->screen
;
1477 enum pipe_format dest_format
, src_format
;
1478 GLboolean use_fallback
= GL_TRUE
;
1479 GLboolean matching_base_formats
;
1480 GLuint format_writemask
, sample_count
;
1481 struct pipe_surface
*dest_surface
= NULL
;
1482 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1484 /* make sure finalize_textures has been called?
1486 if (0) st_validate_state(st
);
1488 /* determine if copying depth or color data */
1489 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1490 texBaseFormat
== GL_DEPTH_STENCIL
) {
1491 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1492 if (strb
->Base
.Wrapped
) {
1493 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
1497 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1498 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1501 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1502 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1506 sample_count
= strb
->surface
->texture
->nr_samples
;
1507 /* I believe this would be legal, presumably would need to do a resolve
1508 for color, and for depth/stencil spec says to just use one of the
1509 depth/stencil samples per pixel? Need some transfer clarifications. */
1510 assert(sample_count
< 2);
1513 assert(strb
->surface
);
1514 assert(stImage
->pt
);
1516 src_format
= strb
->surface
->format
;
1517 dest_format
= stImage
->pt
->format
;
1520 * Determine if the src framebuffer and dest texture have the same
1521 * base format. We need this to detect a case such as the framebuffer
1522 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1523 * texture format stores RGBA we need to set A=1 (overriding the
1524 * framebuffer's alpha values). We can't do that with the blit or
1525 * textured-quad paths.
1527 matching_base_formats
=
1528 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1529 _mesa_get_format_base_format(texImage
->TexFormat
));
1530 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1532 if (ctx
->_ImageTransferState
== 0x0) {
1534 if (matching_base_formats
&&
1535 src_format
== dest_format
&&
1538 /* use surface_copy() / blit */
1539 struct pipe_box src_box
;
1540 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
1541 width
, height
, &src_box
);
1543 /* for resource_copy_region(), y=0=top, always */
1544 pipe
->resource_copy_region(pipe
,
1547 stImage
->base
.Level
,
1548 destX
, destY
, destZ
+ stImage
->base
.Face
,
1551 strb
->surface
->u
.tex
.level
,
1553 use_fallback
= GL_FALSE
;
1555 else if (format_writemask
&&
1556 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1557 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1558 screen
->is_format_supported(screen
, src_format
,
1559 PIPE_TEXTURE_2D
, sample_count
,
1560 PIPE_BIND_SAMPLER_VIEW
) &&
1561 screen
->is_format_supported(screen
, dest_format
,
1563 PIPE_BIND_RENDER_TARGET
)) {
1564 /* draw textured quad to do the copy */
1566 struct pipe_surface surf_tmpl
;
1567 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1568 surf_tmpl
.format
= util_format_linear(stImage
->pt
->format
);
1569 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
1570 surf_tmpl
.u
.tex
.level
= stImage
->base
.Level
;
1571 surf_tmpl
.u
.tex
.first_layer
= stImage
->base
.Face
+ destZ
;
1572 surf_tmpl
.u
.tex
.last_layer
= stImage
->base
.Face
+ destZ
;
1574 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1578 srcY1
= strb
->Base
.Height
- srcY
- height
;
1579 srcY0
= srcY1
+ height
;
1583 srcY1
= srcY0
+ height
;
1586 /* Disable conditional rendering. */
1587 if (st
->render_condition
) {
1588 pipe
->render_condition(pipe
, NULL
, 0);
1591 util_blit_pixels_writemask(st
->blit
,
1593 strb
->surface
->u
.tex
.level
,
1595 srcX
+ width
, srcY1
,
1596 strb
->surface
->u
.tex
.first_layer
,
1599 destX
+ width
, destY
+ height
,
1600 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1603 /* Restore conditional rendering state. */
1604 if (st
->render_condition
) {
1605 pipe
->render_condition(pipe
, st
->render_condition
,
1606 st
->condition_mode
);
1609 use_fallback
= GL_FALSE
;
1613 pipe_surface_reference(&dest_surface
, NULL
);
1617 /* software fallback */
1618 fallback_copy_texsubimage(ctx
, target
, level
,
1619 strb
, stImage
, texBaseFormat
,
1620 destX
, destY
, destZ
,
1621 srcX
, srcY
, width
, height
);
1628 st_CopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1629 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1631 const GLint yoffset
= 0, zoffset
= 0;
1632 const GLsizei height
= 1;
1633 st_copy_texsubimage(ctx
, target
, level
,
1634 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1635 x
, y
, width
, height
); /* src X, Y, size */
1640 st_CopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1641 GLint xoffset
, GLint yoffset
,
1642 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1644 const GLint zoffset
= 0;
1645 st_copy_texsubimage(ctx
, target
, level
,
1646 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1647 x
, y
, width
, height
); /* src X, Y, size */
1652 st_CopyTexSubImage3D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1653 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1654 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1656 st_copy_texsubimage(ctx
, target
, level
,
1657 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1658 x
, y
, width
, height
); /* src X, Y, size */
1663 * Copy image data from stImage into the texture object 'stObj' at level
1667 copy_image_data_to_texture(struct st_context
*st
,
1668 struct st_texture_object
*stObj
,
1670 struct st_texture_image
*stImage
)
1674 const struct gl_texture_image
*dstImage
=
1675 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
1677 assert(dstImage
->Width
== stImage
->base
.Width
);
1678 assert(dstImage
->Height
== stImage
->base
.Height
);
1679 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1683 /* Copy potentially with the blitter:
1685 st_texture_image_copy(st
->pipe
,
1686 stObj
->pt
, dstLevel
, /* dest texture, level */
1687 stImage
->pt
, stImage
->base
.Level
, /* src texture, level */
1688 stImage
->base
.Face
);
1690 pipe_resource_reference(&stImage
->pt
, NULL
);
1692 else if (stImage
->base
.Data
) {
1693 st_texture_image_data(st
,
1698 stImage
->base
.RowStride
*
1699 util_format_get_blocksize(stObj
->pt
->format
),
1700 stImage
->base
.RowStride
*
1701 stImage
->base
.Height
*
1702 util_format_get_blocksize(stObj
->pt
->format
));
1703 _mesa_align_free(stImage
->base
.Data
);
1704 stImage
->base
.Data
= NULL
;
1707 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1712 * Called during state validation. When this function is finished,
1713 * the texture object should be ready for rendering.
1714 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1717 st_finalize_texture(struct gl_context
*ctx
,
1718 struct pipe_context
*pipe
,
1719 struct gl_texture_object
*tObj
)
1721 struct st_context
*st
= st_context(ctx
);
1722 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1723 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1725 struct st_texture_image
*firstImage
;
1726 enum pipe_format firstImageFormat
;
1727 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1729 if (stObj
->base
._Complete
) {
1730 /* The texture is complete and we know exactly how many mipmap levels
1731 * are present/needed. This is conditional because we may be called
1732 * from the st_generate_mipmap() function when the texture object is
1733 * incomplete. In that case, we'll have set stObj->lastLevel before
1736 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1737 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1738 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1740 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1743 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1746 /* If both firstImage and stObj point to a texture which can contain
1747 * all active images, favour firstImage. Note that because of the
1748 * completeness requirement, we know that the image dimensions
1751 if (firstImage
->pt
&&
1752 firstImage
->pt
!= stObj
->pt
&&
1753 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1754 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1755 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1758 /* Find gallium format for the Mesa texture */
1759 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1761 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1763 GLuint width
, height
, depth
;
1764 if (!guess_base_level_size(stObj
->base
.Target
,
1765 firstImage
->base
.Width2
,
1766 firstImage
->base
.Height2
,
1767 firstImage
->base
.Depth2
,
1768 stObj
->base
.BaseLevel
,
1769 &width
, &height
, &depth
)) {
1770 width
= stObj
->width0
;
1771 height
= stObj
->height0
;
1772 depth
= stObj
->depth0
;
1774 /* convert GL dims to Gallium dims */
1775 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1776 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1779 /* If we already have a gallium texture, check that it matches the texture
1780 * object's format, target, size, num_levels, etc.
1783 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1784 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1785 stObj
->pt
->last_level
< stObj
->lastLevel
||
1786 stObj
->pt
->width0
!= ptWidth
||
1787 stObj
->pt
->height0
!= ptHeight
||
1788 stObj
->pt
->depth0
!= ptDepth
||
1789 stObj
->pt
->array_size
!= ptLayers
)
1791 /* The gallium texture does not match the Mesa texture so delete the
1792 * gallium texture now. We'll make a new one below.
1794 pipe_resource_reference(&stObj
->pt
, NULL
);
1795 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1796 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1800 /* May need to create a new gallium texture:
1803 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1805 stObj
->pt
= st_texture_create(st
,
1806 gl_target_to_pipe(stObj
->base
.Target
),
1816 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1821 /* Pull in any images not in the object's texture:
1823 for (face
= 0; face
< nr_faces
; face
++) {
1825 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1826 struct st_texture_image
*stImage
=
1827 st_texture_image(stObj
->base
.Image
[face
][level
]);
1829 /* Need to import images in main memory or held in other textures.
1831 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1833 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1834 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1835 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
1836 /* src image fits expected dest mipmap level size */
1837 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1848 * Returns pointer to a default/dummy texture.
1849 * This is typically used when the current shader has tex/sample instructions
1850 * but the user has not provided a (any) texture(s).
1852 struct gl_texture_object
*
1853 st_get_default_texture(struct st_context
*st
)
1855 if (!st
->default_texture
) {
1856 static const GLenum target
= GL_TEXTURE_2D
;
1857 GLubyte pixels
[16][16][4];
1858 struct gl_texture_object
*texObj
;
1859 struct gl_texture_image
*texImg
;
1862 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1863 * when attempting to sample incomplete textures.
1865 for (i
= 0; i
< 16; i
++) {
1866 for (j
= 0; j
< 16; j
++) {
1867 pixels
[i
][j
][0] = 0;
1868 pixels
[i
][j
][1] = 0;
1869 pixels
[i
][j
][2] = 0;
1870 pixels
[i
][j
][3] = 255;
1874 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1876 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1878 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1879 16, 16, 1, 0, /* w, h, d, border */
1880 GL_RGBA
, MESA_FORMAT_RGBA8888
);
1882 st_TexImage(st
->ctx
, 2, target
,
1883 0, GL_RGBA
, /* level, intformat */
1884 16, 16, 1, 0, /* w, h, d, border */
1885 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1886 &st
->ctx
->DefaultPacking
,
1890 texObj
->Sampler
.MinFilter
= GL_NEAREST
;
1891 texObj
->Sampler
.MagFilter
= GL_NEAREST
;
1892 texObj
->_Complete
= GL_TRUE
;
1894 st
->default_texture
= texObj
;
1896 return st
->default_texture
;
1901 st_init_texture_functions(struct dd_function_table
*functions
)
1903 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1904 functions
->TexImage1D
= st_TexImage1D
;
1905 functions
->TexImage2D
= st_TexImage2D
;
1906 functions
->TexImage3D
= st_TexImage3D
;
1907 functions
->TexSubImage1D
= st_TexSubImage1D
;
1908 functions
->TexSubImage2D
= st_TexSubImage2D
;
1909 functions
->TexSubImage3D
= st_TexSubImage3D
;
1910 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1911 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1912 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1913 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1914 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1915 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1916 functions
->GenerateMipmap
= st_generate_mipmap
;
1918 functions
->GetTexImage
= st_GetTexImage
;
1920 /* compressed texture functions */
1921 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1922 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1924 functions
->NewTextureObject
= st_NewTextureObject
;
1925 functions
->NewTextureImage
= st_NewTextureImage
;
1926 functions
->DeleteTexture
= st_DeleteTextureObject
;
1927 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
1928 functions
->MapTextureImage
= st_MapTextureImage
;
1929 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
1931 functions
->TextureMemCpy
= do_memcpy
;
1933 /* XXX Temporary until we can query pipe's texture sizes */
1934 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;