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"
38 #include "main/pixeltransfer.h"
39 #include "main/texcompress.h"
40 #include "main/texfetch.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
;
79 return PIPE_TEXTURE_2D
;
81 case GL_TEXTURE_RECTANGLE_NV
:
82 return PIPE_TEXTURE_RECT
;
85 return PIPE_TEXTURE_3D
;
87 case GL_TEXTURE_CUBE_MAP_ARB
:
88 return PIPE_TEXTURE_CUBE
;
97 /** called via ctx->Driver.NewTextureImage() */
98 static struct gl_texture_image
*
99 st_NewTextureImage(struct gl_context
* ctx
)
101 DBG("%s\n", __FUNCTION__
);
103 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
107 /** called via ctx->Driver.NewTextureObject() */
108 static struct gl_texture_object
*
109 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
111 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
113 DBG("%s\n", __FUNCTION__
);
114 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
119 /** called via ctx->Driver.DeleteTextureObject() */
121 st_DeleteTextureObject(struct gl_context
*ctx
,
122 struct gl_texture_object
*texObj
)
124 struct st_context
*st
= st_context(ctx
);
125 struct st_texture_object
*stObj
= st_texture_object(texObj
);
127 pipe_resource_reference(&stObj
->pt
, NULL
);
128 if (stObj
->sampler_view
) {
129 if (stObj
->sampler_view
->context
!= st
->pipe
) {
130 /* Take "ownership" of this texture sampler view by setting
131 * its context pointer to this context. This avoids potential
132 * crashes when the texture object is shared among contexts
133 * and the original/owner context has already been destroyed.
135 stObj
->sampler_view
->context
= st
->pipe
;
137 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
139 _mesa_delete_texture_object(ctx
, texObj
);
143 /** called via ctx->Driver.FreeTexImageData() */
145 st_FreeTextureImageData(struct gl_context
* ctx
, struct gl_texture_image
*texImage
)
147 struct st_texture_image
*stImage
= st_texture_image(texImage
);
149 DBG("%s\n", __FUNCTION__
);
152 pipe_resource_reference(&stImage
->pt
, NULL
);
155 if (texImage
->Data
) {
156 _mesa_align_free(texImage
->Data
);
157 texImage
->Data
= NULL
;
163 * From linux kernel i386 header files, copes with odd sizes better
164 * than COPY_DWORDS would:
165 * XXX Put this in src/mesa/main/imports.h ???
167 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
169 __memcpy(void *to
, const void *from
, size_t n
)
172 __asm__
__volatile__("rep ; movsl\n\t"
176 "1:\ttestb $1,%b4\n\t"
178 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
179 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
184 #define __memcpy(a,b,c) memcpy(a,b,c)
189 * The system memcpy (at least on ubuntu 5.10) has problems copying
190 * to agp (writecombined) memory from a source which isn't 64-byte
191 * aligned - there is a 4x performance falloff.
193 * The x86 __memcpy is immune to this but is slightly slower
194 * (10%-ish) than the system memcpy.
196 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
197 * isn't much faster than x86_memcpy for agp copies.
199 * TODO: switch dynamically.
202 do_memcpy(void *dest
, const void *src
, size_t n
)
204 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
205 return __memcpy(dest
, src
, n
);
208 return memcpy(dest
, src
, n
);
213 * Return default texture resource binding bitmask for the given format.
216 default_bindings(struct st_context
*st
, enum pipe_format format
)
218 struct pipe_screen
*screen
= st
->pipe
->screen
;
219 const unsigned target
= PIPE_TEXTURE_2D
;
220 const unsigned geom
= 0x0;
223 if (util_format_is_depth_or_stencil(format
))
224 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
226 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
228 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
, geom
))
231 return PIPE_BIND_SAMPLER_VIEW
;
235 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
237 get_texture_dims(GLenum target
)
241 case GL_TEXTURE_1D_ARRAY_EXT
:
244 case GL_TEXTURE_CUBE_MAP_ARB
:
245 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
246 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
247 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
248 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
249 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
250 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
251 case GL_TEXTURE_RECTANGLE_NV
:
252 case GL_TEXTURE_2D_ARRAY_EXT
:
257 assert(0 && "invalid texture target in get_texture_dims()");
264 * Try to allocate a pipe_resource object for the given st_texture_object.
266 * We use the given st_texture_image as a clue to determine the size of the
267 * mipmap image at level=0.
269 * \return GL_TRUE for success, GL_FALSE if out of memory.
272 guess_and_alloc_texture(struct st_context
*st
,
273 struct st_texture_object
*stObj
,
274 const struct st_texture_image
*stImage
)
276 const GLuint dims
= get_texture_dims(stObj
->base
.Target
);
277 GLuint level
, lastLevel
, width
, height
, depth
;
279 enum pipe_format fmt
;
281 DBG("%s\n", __FUNCTION__
);
285 level
= stImage
->level
;
286 width
= stImage
->base
.Width2
; /* size w/out border */
287 height
= stImage
->base
.Height2
;
288 depth
= stImage
->base
.Depth2
;
294 /* Depending on the image's size, we can't always make a guess here.
297 if ( (dims
>= 1 && width
== 1) ||
298 (dims
>= 2 && height
== 1) ||
299 (dims
>= 3 && depth
== 1) ) {
300 /* we can't determine the image size at level=0 */
301 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
302 /* this is not an out of memory error */
307 /* grow the image size until we hit level = 0 */
320 /* At this point, (width x height x depth) is the expected size of
321 * the level=0 mipmap image.
324 /* Guess a reasonable value for lastLevel. With OpenGL we have no
325 * idea how many mipmap levels will be in a texture until we start
326 * to render with it. Make an educated guess here but be prepared
327 * to re-allocating a texture buffer with space for more (or fewer)
328 * mipmap levels later.
330 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
331 stObj
->base
.MinFilter
== GL_LINEAR
||
332 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
333 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
334 !stObj
->base
.GenerateMipmap
&&
335 stImage
->level
== 0) {
336 /* only alloc space for a single mipmap level */
340 /* alloc space for a full mipmap */
341 GLuint l2width
= util_logbase2(width
);
342 GLuint l2height
= util_logbase2(height
);
343 GLuint l2depth
= util_logbase2(depth
);
344 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
347 /* Save the level=0 dimensions */
348 stObj
->width0
= width
;
349 stObj
->height0
= height
;
350 stObj
->depth0
= depth
;
352 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
354 bindings
= default_bindings(st
, fmt
);
356 stObj
->pt
= st_texture_create(st
,
357 gl_target_to_pipe(stObj
->base
.Target
),
365 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
367 return stObj
->pt
!= NULL
;
372 * Adjust pixel unpack params and image dimensions to strip off the
374 * Gallium doesn't support texture borders. They've seldem been used
375 * and seldom been implemented correctly anyway.
376 * \param unpackNew returns the new pixel unpack parameters
379 strip_texture_border(GLint border
,
380 GLint
*width
, GLint
*height
, GLint
*depth
,
381 const struct gl_pixelstore_attrib
*unpack
,
382 struct gl_pixelstore_attrib
*unpackNew
)
384 assert(border
> 0); /* sanity check */
386 *unpackNew
= *unpack
;
388 if (unpackNew
->RowLength
== 0)
389 unpackNew
->RowLength
= *width
;
391 if (depth
&& unpackNew
->ImageHeight
== 0)
392 unpackNew
->ImageHeight
= *height
;
394 unpackNew
->SkipPixels
+= border
;
396 unpackNew
->SkipRows
+= border
;
398 unpackNew
->SkipImages
+= border
;
401 *width
= *width
- 2 * border
;
402 if (height
&& *height
>= 3)
403 *height
= *height
- 2 * border
;
404 if (depth
&& *depth
>= 3)
405 *depth
= *depth
- 2 * border
;
410 * Try to do texture compression via rendering. If the Gallium driver
411 * can render into a compressed surface this will allow us to do texture
413 * \return GL_TRUE for success, GL_FALSE for failure
416 compress_with_blit(struct gl_context
* ctx
,
417 GLenum target
, GLint level
,
418 GLint xoffset
, GLint yoffset
, GLint zoffset
,
419 GLint width
, GLint height
, GLint depth
,
420 GLenum format
, GLenum type
, const void *pixels
,
421 const struct gl_pixelstore_attrib
*unpack
,
422 struct gl_texture_image
*texImage
)
424 const GLuint dstImageOffsets
[1] = {0};
425 struct st_texture_image
*stImage
= st_texture_image(texImage
);
426 struct st_context
*st
= st_context(ctx
);
427 struct pipe_context
*pipe
= st
->pipe
;
428 struct pipe_screen
*screen
= pipe
->screen
;
429 gl_format mesa_format
;
430 struct pipe_resource templ
;
431 struct pipe_resource
*src_tex
;
432 struct pipe_sampler_view view_templ
;
433 struct pipe_sampler_view
*src_view
;
434 struct pipe_surface
*dst_surface
, surf_tmpl
;
435 struct pipe_transfer
*tex_xfer
;
439 /* XXX: Can this happen? Should we assert? */
443 /* get destination surface (in the compressed texture) */
444 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
445 surf_tmpl
.format
= stImage
->pt
->format
;
446 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
447 surf_tmpl
.u
.tex
.level
= stImage
->level
;
448 surf_tmpl
.u
.tex
.first_layer
= stImage
->face
;
449 surf_tmpl
.u
.tex
.last_layer
= stImage
->face
;
450 dst_surface
= pipe
->create_surface(pipe
, stImage
->pt
, &surf_tmpl
);
452 /* can't render into this format (or other problem) */
456 /* Choose format for the temporary RGBA texture image.
458 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
463 /* Create the temporary source texture
465 memset(&templ
, 0, sizeof(templ
));
466 templ
.target
= st
->internal_target
;
467 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
);
468 templ
.width0
= width
;
469 templ
.height0
= height
;
471 templ
.array_size
= 1;
472 templ
.last_level
= 0;
473 templ
.usage
= PIPE_USAGE_DEFAULT
;
474 templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
475 src_tex
= screen
->resource_create(screen
, &templ
);
480 /* Put user's tex data into the temporary texture
482 tex_xfer
= pipe_get_transfer(st_context(ctx
)->pipe
, src_tex
,
483 0, 0, /* layer, level are zero */
485 0, 0, width
, height
); /* x, y, w, h */
486 map
= pipe_transfer_map(pipe
, tex_xfer
);
488 _mesa_texstore(ctx
, 2, GL_RGBA
, mesa_format
,
490 0, 0, 0, /* dest x/y/z offset */
491 tex_xfer
->stride
, /* dest row stride (bytes) */
492 dstImageOffsets
, /* image offsets (for 3D only) */
493 width
, height
, 1, /* size */
494 format
, type
, /* source format/type */
495 pixels
, /* source data */
496 unpack
); /* source data packing */
498 pipe_transfer_unmap(pipe
, tex_xfer
);
499 pipe
->transfer_destroy(pipe
, tex_xfer
);
501 /* Create temporary sampler view */
502 u_sampler_view_default_template(&view_templ
,
505 src_view
= pipe
->create_sampler_view(pipe
, src_tex
, &view_templ
);
508 /* copy / compress image */
509 util_blit_pixels_tex(st
->blit
,
510 src_view
, /* sampler view (src) */
511 0, 0, /* src x0, y0 */
512 width
, height
, /* src x1, y1 */
513 dst_surface
, /* pipe_surface (dst) */
514 xoffset
, yoffset
, /* dst x0, y0 */
515 xoffset
+ width
, /* dst x1 */
516 yoffset
+ height
, /* dst y1 */
518 PIPE_TEX_MIPFILTER_NEAREST
);
520 pipe_surface_reference(&dst_surface
, NULL
);
521 pipe_resource_reference(&src_tex
, NULL
);
522 pipe_sampler_view_reference(&src_view
, NULL
);
529 * Do glTexImage1/2/3D().
532 st_TexImage(struct gl_context
* ctx
,
534 GLenum target
, GLint level
,
535 GLint internalFormat
,
536 GLint width
, GLint height
, GLint depth
,
538 GLenum format
, GLenum type
, const void *pixels
,
539 const struct gl_pixelstore_attrib
*unpack
,
540 struct gl_texture_object
*texObj
,
541 struct gl_texture_image
*texImage
,
542 GLsizei imageSize
, GLboolean compressed_src
)
544 struct st_context
*st
= st_context(ctx
);
545 struct pipe_screen
*screen
= st
->pipe
->screen
;
546 struct st_texture_object
*stObj
= st_texture_object(texObj
);
547 struct st_texture_image
*stImage
= st_texture_image(texImage
);
548 GLuint dstRowStride
= 0;
549 struct gl_pixelstore_attrib unpackNB
;
550 enum pipe_transfer_usage transfer_usage
= 0;
552 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
553 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
555 /* switch to "normal" */
556 if (stObj
->surface_based
) {
559 _mesa_clear_texture_object(ctx
, texObj
);
560 pipe_resource_reference(&stObj
->pt
, NULL
);
562 /* oops, need to init this image again */
563 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
564 internalFormat
, format
, type
);
566 _mesa_init_teximage_fields(ctx
, target
, texImage
,
567 width
, height
, depth
, border
,
568 internalFormat
, texFormat
);
570 stObj
->surface_based
= GL_FALSE
;
573 /* gallium does not support texture borders, strip it off */
575 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
577 texImage
->Width
= width
;
578 texImage
->Height
= height
;
579 texImage
->Depth
= depth
;
580 texImage
->Border
= 0;
584 assert(texImage
->Width
== width
);
585 assert(texImage
->Height
== height
);
586 assert(texImage
->Depth
== depth
);
589 stImage
->face
= _mesa_tex_target_to_face(target
);
590 stImage
->level
= level
;
592 _mesa_set_fetch_functions(texImage
, dims
);
594 /* Release the reference to a potentially orphaned buffer.
595 * Release any old malloced memory.
598 pipe_resource_reference(&stImage
->pt
, NULL
);
599 assert(!texImage
->Data
);
601 else if (texImage
->Data
) {
602 _mesa_align_free(texImage
->Data
);
606 * See if the new image is somehow incompatible with the existing
607 * mipmap. If so, free the old mipmap.
610 if (level
> (GLint
) stObj
->pt
->last_level
||
611 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
612 stImage
->face
, stImage
->level
)) {
614 pipe_resource_reference(&stObj
->pt
, NULL
);
616 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
620 if (width
== 0 || height
== 0 || depth
== 0) {
621 /* stop after freeing old image */
626 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
627 /* Probably out of memory.
628 * Try flushing any pending rendering, then retry.
631 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
632 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
638 assert(!stImage
->pt
);
640 /* Check if this texture image can live inside the texture object's buffer.
641 * If so, store the image there. Otherwise the image will temporarily live
645 st_texture_match_image(stObj
->pt
, &stImage
->base
,
646 stImage
->face
, stImage
->level
)) {
648 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
653 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
655 /* Pixel data may come from regular user memory or a PBO. For the later,
656 * do bounds checking and map the PBO to read pixels data from it.
658 * XXX we should try to use a GPU-accelerated path to copy the image data
659 * from the PBO to the texture.
661 if (compressed_src
) {
662 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
664 "glCompressedTexImage");
667 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
669 pixels
, unpack
, "glTexImage");
672 /* See if we can do texture compression with a blit/render.
674 if (!compressed_src
&&
676 _mesa_is_format_compressed(texImage
->TexFormat
) &&
677 screen
->is_format_supported(screen
,
679 stImage
->pt
->target
, 0,
680 PIPE_BIND_RENDER_TARGET
, 0)) {
684 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
685 format
, type
, pixels
, unpack
, texImage
)) {
691 * Prepare to store the texture data. Either map the gallium texture buffer
692 * memory or malloc space for it.
695 /* Store the image in the gallium texture memory buffer */
696 if (format
== GL_DEPTH_COMPONENT
&&
697 util_format_is_depth_and_stencil(stImage
->pt
->format
))
698 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
700 transfer_usage
= PIPE_TRANSFER_WRITE
;
702 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
703 transfer_usage
, 0, 0, width
, height
);
704 if(stImage
->transfer
)
705 dstRowStride
= stImage
->transfer
->stride
;
708 /* Allocate regular memory and store the image there temporarily. */
709 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
710 width
, height
, depth
);
711 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
713 texImage
->Data
= _mesa_align_malloc(imageSize
, 16);
716 if (!texImage
->Data
) {
717 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
722 /* We've allocated texture memory, but have no pixel data - all done. */
726 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
727 width
, height
, depth
, width
, dstRowStride
);
729 /* Copy user texture image into the texture buffer.
731 if (compressed_src
) {
732 const GLuint srcRowStride
=
733 _mesa_format_row_stride(texImage
->TexFormat
, width
);
734 if (dstRowStride
== srcRowStride
) {
735 memcpy(texImage
->Data
, pixels
, imageSize
);
738 char *dst
= texImage
->Data
;
739 const char *src
= pixels
;
740 GLuint i
, bw
, bh
, lines
;
741 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
742 lines
= (height
+ bh
- 1) / bh
;
744 for (i
= 0; i
< lines
; ++i
) {
745 memcpy(dst
, src
, srcRowStride
);
752 const GLuint srcImageStride
=
753 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
755 const GLubyte
*src
= (const GLubyte
*) pixels
;
757 for (i
= 0; i
< depth
; i
++) {
758 if (!_mesa_texstore(ctx
, dims
,
759 texImage
->_BaseFormat
,
762 0, 0, 0, /* dstX/Y/Zoffset */
764 texImage
->ImageOffsets
,
766 format
, type
, src
, unpack
)) {
767 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
770 if (stImage
->pt
&& i
+ 1 < depth
) {
771 /* unmap this slice */
772 st_texture_image_unmap(st
, stImage
);
773 /* map next slice of 3D texture */
774 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
775 transfer_usage
, 0, 0,
777 src
+= srcImageStride
;
783 _mesa_unmap_teximage_pbo(ctx
, unpack
);
785 if (stImage
->pt
&& texImage
->Data
) {
786 st_texture_image_unmap(st
, stImage
);
787 texImage
->Data
= NULL
;
793 st_TexImage3D(struct gl_context
* ctx
,
794 GLenum target
, GLint level
,
795 GLint internalFormat
,
796 GLint width
, GLint height
, GLint depth
,
798 GLenum format
, GLenum type
, const void *pixels
,
799 const struct gl_pixelstore_attrib
*unpack
,
800 struct gl_texture_object
*texObj
,
801 struct gl_texture_image
*texImage
)
803 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
804 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
810 st_TexImage2D(struct gl_context
* ctx
,
811 GLenum target
, GLint level
,
812 GLint internalFormat
,
813 GLint width
, GLint height
, GLint border
,
814 GLenum format
, GLenum type
, const void *pixels
,
815 const struct gl_pixelstore_attrib
*unpack
,
816 struct gl_texture_object
*texObj
,
817 struct gl_texture_image
*texImage
)
819 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
820 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
825 st_TexImage1D(struct gl_context
* ctx
,
826 GLenum target
, GLint level
,
827 GLint internalFormat
,
828 GLint width
, GLint border
,
829 GLenum format
, GLenum type
, const void *pixels
,
830 const struct gl_pixelstore_attrib
*unpack
,
831 struct gl_texture_object
*texObj
,
832 struct gl_texture_image
*texImage
)
834 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
835 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
840 st_CompressedTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
841 GLint internalFormat
,
842 GLint width
, GLint height
, GLint border
,
843 GLsizei imageSize
, const GLvoid
*data
,
844 struct gl_texture_object
*texObj
,
845 struct gl_texture_image
*texImage
)
847 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
848 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
854 * glGetTexImage() helper: decompress a compressed texture by rendering
855 * a textured quad. Store the results in the user's buffer.
858 decompress_with_blit(struct gl_context
* ctx
, GLenum target
, GLint level
,
859 GLenum format
, GLenum type
, GLvoid
*pixels
,
860 struct gl_texture_object
*texObj
,
861 struct gl_texture_image
*texImage
)
863 struct st_context
*st
= st_context(ctx
);
864 struct pipe_context
*pipe
= st
->pipe
;
865 struct st_texture_image
*stImage
= st_texture_image(texImage
);
866 struct st_texture_object
*stObj
= st_texture_object(texObj
);
867 struct pipe_sampler_view
*src_view
=
868 st_get_texture_sampler_view(stObj
, pipe
);
869 const GLuint width
= texImage
->Width
;
870 const GLuint height
= texImage
->Height
;
871 struct pipe_surface
*dst_surface
;
872 struct pipe_resource
*dst_texture
;
873 struct pipe_transfer
*tex_xfer
;
874 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
875 PIPE_BIND_TRANSFER_READ
);
877 /* create temp / dest surface */
878 if (!util_create_rgba_surface(pipe
, width
, height
, bind
,
879 &dst_texture
, &dst_surface
)) {
880 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
881 "in decompress_with_blit()");
885 /* blit/render/decompress */
886 util_blit_pixels_tex(st
->blit
,
887 src_view
, /* pipe_resource (src) */
888 0, 0, /* src x0, y0 */
889 width
, height
, /* src x1, y1 */
890 dst_surface
, /* pipe_surface (dst) */
891 0, 0, /* dst x0, y0 */
892 width
, height
, /* dst x1, y1 */
894 PIPE_TEX_MIPFILTER_NEAREST
);
896 /* map the dst_surface so we can read from it */
897 tex_xfer
= pipe_get_transfer(st_context(ctx
)->pipe
,
900 0, 0, width
, height
);
902 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
904 /* copy/pack data into user buffer */
905 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
907 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
908 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
910 for (row
= 0; row
< height
; row
++) {
911 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
912 height
, format
, type
, row
, 0);
913 memcpy(dest
, map
, bytesPerRow
);
914 map
+= tex_xfer
->stride
;
916 pipe_transfer_unmap(pipe
, tex_xfer
);
919 /* format translation via floats */
921 for (row
= 0; row
< height
; row
++) {
922 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
923 GLfloat rgba
[4 * MAX_WIDTH
];
924 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
925 height
, format
, type
, row
, 0);
927 if (ST_DEBUG
& DEBUG_FALLBACK
)
928 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
930 /* get float[4] rgba row from surface */
931 pipe_get_tile_rgba(pipe
, tex_xfer
, 0, row
, width
, 1, rgba
);
933 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
934 type
, dest
, &ctx
->Pack
, transferOps
);
938 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
940 pipe
->transfer_destroy(pipe
, tex_xfer
);
942 /* destroy the temp / dest surface */
943 util_destroy_rgba_surface(dst_texture
, dst_surface
);
949 * Need to map texture image into memory before copying image data,
953 st_get_tex_image(struct gl_context
* ctx
, GLenum target
, GLint level
,
954 GLenum format
, GLenum type
, GLvoid
* pixels
,
955 struct gl_texture_object
*texObj
,
956 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
958 struct st_context
*st
= st_context(ctx
);
959 struct st_texture_image
*stImage
= st_texture_image(texImage
);
960 const GLuint dstImageStride
=
961 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
967 util_format_is_s3tc(stImage
->pt
->format
) &&
969 /* Need to decompress the texture.
970 * We'll do this by rendering a textured quad.
971 * Note that we only expect RGBA formats (no Z/depth formats).
973 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
980 /* Image is stored in hardware format in a buffer managed by the
981 * kernel. Need to explicitly map and unmap it.
983 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
984 PIPE_TRANSFER_READ
, 0, 0,
986 stImage
->base
.Height
);
987 /* compute stride in texels from stride in bytes */
988 texImage
->RowStride
= stImage
->transfer
->stride
989 * util_format_get_blockwidth(stImage
->pt
->format
)
990 / util_format_get_blocksize(stImage
->pt
->format
);
993 /* Otherwise, the image should actually be stored in
994 * texImage->Data. This is pretty confusing for
995 * everybody, I'd much prefer to separate the two functions of
996 * texImage->Data - storage for texture images in main memory
997 * and access (ie mappings) of images. In other words, we'd
998 * create a new texImage->Map field and leave Data simply for
1001 assert(texImage
->Data
);
1004 depth
= texImage
->Depth
;
1005 texImage
->Depth
= 1;
1007 dest
= (GLubyte
*) pixels
;
1009 _mesa_set_fetch_functions(texImage
, get_texture_dims(target
));
1011 for (i
= 0; i
< depth
; i
++) {
1012 if (compressed_dst
) {
1013 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
1017 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
1021 if (stImage
->pt
&& i
+ 1 < depth
) {
1022 /* unmap this slice */
1023 st_texture_image_unmap(st
, stImage
);
1024 /* map next slice of 3D texture */
1025 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
1026 PIPE_TRANSFER_READ
, 0, 0,
1027 stImage
->base
.Width
,
1028 stImage
->base
.Height
);
1029 dest
+= dstImageStride
;
1033 texImage
->Depth
= depth
;
1037 st_texture_image_unmap(st
, stImage
);
1038 texImage
->Data
= NULL
;
1044 st_GetTexImage(struct gl_context
* ctx
, GLenum target
, GLint level
,
1045 GLenum format
, GLenum type
, GLvoid
* pixels
,
1046 struct gl_texture_object
*texObj
,
1047 struct gl_texture_image
*texImage
)
1049 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1055 st_GetCompressedTexImage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1057 struct gl_texture_object
*texObj
,
1058 struct gl_texture_image
*texImage
)
1060 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1067 st_TexSubimage(struct gl_context
*ctx
, GLint dims
, GLenum target
, GLint level
,
1068 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1069 GLint width
, GLint height
, GLint depth
,
1070 GLenum format
, GLenum type
, const void *pixels
,
1071 const struct gl_pixelstore_attrib
*packing
,
1072 struct gl_texture_object
*texObj
,
1073 struct gl_texture_image
*texImage
)
1075 struct st_context
*st
= st_context(ctx
);
1076 struct pipe_screen
*screen
= st
->pipe
->screen
;
1077 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1078 GLuint dstRowStride
;
1079 const GLuint srcImageStride
=
1080 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1083 /* init to silence warning only: */
1084 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1086 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1087 _mesa_lookup_enum_by_nr(target
),
1088 level
, xoffset
, yoffset
, width
, height
);
1091 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1092 type
, pixels
, packing
, "glTexSubImage2D");
1096 /* See if we can do texture compression with a blit/render.
1098 if (!ctx
->Mesa_DXTn
&&
1099 _mesa_is_format_compressed(texImage
->TexFormat
) &&
1100 screen
->is_format_supported(screen
,
1101 stImage
->pt
->format
,
1102 stImage
->pt
->target
, 0,
1103 PIPE_BIND_RENDER_TARGET
, 0)) {
1104 if (compress_with_blit(ctx
, target
, level
,
1105 xoffset
, yoffset
, zoffset
,
1106 width
, height
, depth
,
1107 format
, type
, pixels
, packing
, texImage
)) {
1112 /* Map buffer if necessary. Need to lock to prevent other contexts
1113 * from uploading the buffer under us.
1116 if (format
== GL_DEPTH_COMPONENT
&&
1117 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1118 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1120 transfer_usage
= PIPE_TRANSFER_WRITE
;
1122 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1128 if (!texImage
->Data
) {
1129 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1133 src
= (const GLubyte
*) pixels
;
1134 dstRowStride
= stImage
->transfer
->stride
;
1136 for (i
= 0; i
< depth
; i
++) {
1137 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1138 texImage
->TexFormat
,
1142 texImage
->ImageOffsets
,
1144 format
, type
, src
, packing
)) {
1145 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1148 if (stImage
->pt
&& i
+ 1 < depth
) {
1149 /* unmap this slice */
1150 st_texture_image_unmap(st
, stImage
);
1151 /* map next slice of 3D texture */
1152 texImage
->Data
= st_texture_image_map(st
, stImage
,
1157 src
+= srcImageStride
;
1162 _mesa_unmap_teximage_pbo(ctx
, packing
);
1164 if (stImage
->pt
&& texImage
->Data
) {
1165 st_texture_image_unmap(st
, stImage
);
1166 texImage
->Data
= NULL
;
1173 st_TexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1174 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1175 GLsizei width
, GLsizei height
, GLsizei depth
,
1176 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1177 const struct gl_pixelstore_attrib
*packing
,
1178 struct gl_texture_object
*texObj
,
1179 struct gl_texture_image
*texImage
)
1181 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1182 width
, height
, depth
, format
, type
,
1183 pixels
, packing
, texObj
, texImage
);
1188 st_TexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1189 GLint xoffset
, GLint yoffset
,
1190 GLsizei width
, GLsizei height
,
1191 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1192 const struct gl_pixelstore_attrib
*packing
,
1193 struct gl_texture_object
*texObj
,
1194 struct gl_texture_image
*texImage
)
1196 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1197 width
, height
, 1, format
, type
,
1198 pixels
, packing
, texObj
, texImage
);
1203 st_TexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1204 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1205 const GLvoid
* pixels
,
1206 const struct gl_pixelstore_attrib
*packing
,
1207 struct gl_texture_object
*texObj
,
1208 struct gl_texture_image
*texImage
)
1210 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1211 format
, type
, pixels
, packing
, texObj
, texImage
);
1216 st_CompressedTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1217 GLint xoffset
, GLsizei width
,
1219 GLsizei imageSize
, const GLvoid
*data
,
1220 struct gl_texture_object
*texObj
,
1221 struct gl_texture_image
*texImage
)
1228 st_CompressedTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1229 GLint xoffset
, GLint yoffset
,
1230 GLsizei width
, GLint height
,
1232 GLsizei imageSize
, const GLvoid
*data
,
1233 struct gl_texture_object
*texObj
,
1234 struct gl_texture_image
*texImage
)
1236 struct st_context
*st
= st_context(ctx
);
1237 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1241 enum pipe_format pformat
;
1244 pformat
= stImage
->pt
->format
;
1246 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1247 PIPE_TRANSFER_WRITE
,
1251 srcBlockStride
= util_format_get_stride(pformat
, width
);
1252 dstBlockStride
= stImage
->transfer
->stride
;
1254 assert(stImage
->pt
);
1255 /* TODO find good values for block and strides */
1256 /* TODO also adjust texImage->data for yoffset/xoffset */
1260 if (!texImage
->Data
) {
1261 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1265 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1266 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1268 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1269 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1270 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1271 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1272 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1276 st_texture_image_unmap(st
, stImage
);
1277 texImage
->Data
= NULL
;
1283 st_CompressedTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1284 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1285 GLsizei width
, GLint height
, GLint depth
,
1287 GLsizei imageSize
, const GLvoid
*data
,
1288 struct gl_texture_object
*texObj
,
1289 struct gl_texture_image
*texImage
)
1297 * Do a CopyTexSubImage operation using a read transfer from the source,
1298 * a write transfer to the destination and get_tile()/put_tile() to access
1299 * the pixels/texels.
1301 * Note: srcY=0=TOP of renderbuffer
1304 fallback_copy_texsubimage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1305 struct st_renderbuffer
*strb
,
1306 struct st_texture_image
*stImage
,
1308 GLint destX
, GLint destY
, GLint destZ
,
1309 GLint srcX
, GLint srcY
,
1310 GLsizei width
, GLsizei height
)
1312 struct st_context
*st
= st_context(ctx
);
1313 struct pipe_context
*pipe
= st
->pipe
;
1314 struct pipe_transfer
*src_trans
;
1316 enum pipe_transfer_usage transfer_usage
;
1318 if (ST_DEBUG
& DEBUG_FALLBACK
)
1319 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1321 assert(width
<= MAX_WIDTH
);
1323 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1324 srcY
= strb
->Base
.Height
- srcY
- height
;
1327 src_trans
= pipe_get_transfer(st_context(ctx
)->pipe
,
1334 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1335 baseFormat
== GL_DEPTH_STENCIL
) &&
1336 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1337 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1339 transfer_usage
= PIPE_TRANSFER_WRITE
;
1341 /* XXX this used to ignore destZ param */
1342 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
1343 destX
, destY
, width
, height
);
1345 if (baseFormat
== GL_DEPTH_COMPONENT
||
1346 baseFormat
== GL_DEPTH_STENCIL
) {
1347 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1348 ctx
->Pixel
.DepthBias
!= 0.0F
);
1351 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1352 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1361 /* To avoid a large temp memory allocation, do copy row by row */
1362 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1363 uint data
[MAX_WIDTH
];
1364 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1366 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1368 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1374 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1376 if (tempSrc
&& texDest
) {
1377 const GLint dims
= 2;
1378 const GLint dstRowStride
= stImage
->transfer
->stride
;
1379 struct gl_texture_image
*texImage
= &stImage
->base
;
1380 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1382 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1383 unpack
.Invert
= GL_TRUE
;
1386 /* get float/RGBA image from framebuffer */
1387 /* XXX this usually involves a lot of int/float conversion.
1388 * try to avoid that someday.
1390 pipe_get_tile_rgba(pipe
, src_trans
, 0, 0, width
, height
, tempSrc
);
1392 /* Store into texture memory.
1393 * Note that this does some special things such as pixel transfer
1394 * ops and format conversion. In particular, if the dest tex format
1395 * is actually RGBA but the user created the texture as GL_RGB we
1396 * need to fill-in/override the alpha channel with 1.0.
1398 _mesa_texstore(ctx
, dims
,
1399 texImage
->_BaseFormat
,
1400 texImage
->TexFormat
,
1404 texImage
->ImageOffsets
,
1406 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1410 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1417 st_texture_image_unmap(st
, stImage
);
1418 pipe
->transfer_destroy(pipe
, src_trans
);
1424 * If the format of the src renderbuffer and the format of the dest
1425 * texture are compatible (in terms of blitting), return a TGSI writemask
1426 * to be used during the blit.
1427 * If the src/dest are incompatible, return 0.
1430 compatible_src_dst_formats(struct gl_context
*ctx
,
1431 const struct gl_renderbuffer
*src
,
1432 const struct gl_texture_image
*dst
)
1434 /* Get logical base formats for the src and dest.
1435 * That is, use the user-requested formats and not the actual, device-
1437 * For example, the user may have requested an A8 texture but the
1438 * driver may actually be using an RGBA texture format. When we
1439 * copy/blit to that texture, we only want to copy the Alpha channel
1440 * and not the RGB channels.
1442 * Similarly, when the src FBO was created an RGB format may have been
1443 * requested but the driver actually chose an RGBA format. In that case,
1444 * we don't want to copy the undefined Alpha channel to the dest texture
1445 * (it should be 1.0).
1447 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1448 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1451 * XXX when we have red-only and red/green renderbuffers we'll need
1452 * to add more cases here (or implement a general-purpose routine that
1453 * queries the existance of the R,G,B,A channels in the src and dest).
1455 if (srcFormat
== dstFormat
) {
1456 /* This is the same as matching_base_formats, which should
1457 * always pass, as it did previously.
1459 return TGSI_WRITEMASK_XYZW
;
1461 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1462 /* Make sure that A in the dest is 1. The actual src format
1463 * may be RGBA and have undefined A values.
1465 return TGSI_WRITEMASK_XYZ
;
1467 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1468 /* Make sure that A in the dest is 1. The actual dst format
1469 * may be RGBA and will need A=1 to provide proper alpha values
1470 * when sampled later.
1472 return TGSI_WRITEMASK_XYZ
;
1475 if (ST_DEBUG
& DEBUG_FALLBACK
)
1476 debug_printf("%s failed for src %s, dst %s\n",
1478 _mesa_lookup_enum_by_nr(srcFormat
),
1479 _mesa_lookup_enum_by_nr(dstFormat
));
1490 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1491 * Note that the region to copy has already been clipped so we know we
1492 * won't read from outside the source renderbuffer's bounds.
1494 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1497 st_copy_texsubimage(struct gl_context
*ctx
,
1498 GLenum target
, GLint level
,
1499 GLint destX
, GLint destY
, GLint destZ
,
1500 GLint srcX
, GLint srcY
,
1501 GLsizei width
, GLsizei height
)
1503 struct gl_texture_unit
*texUnit
=
1504 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1505 struct gl_texture_object
*texObj
=
1506 _mesa_select_tex_object(ctx
, texUnit
, target
);
1507 struct gl_texture_image
*texImage
=
1508 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1509 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1510 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1511 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1512 struct st_renderbuffer
*strb
;
1513 struct st_context
*st
= st_context(ctx
);
1514 struct pipe_context
*pipe
= st
->pipe
;
1515 struct pipe_screen
*screen
= pipe
->screen
;
1516 enum pipe_format dest_format
, src_format
;
1517 GLboolean use_fallback
= GL_TRUE
;
1518 GLboolean matching_base_formats
;
1519 GLuint format_writemask
, sample_count
;
1520 struct pipe_surface
*dest_surface
= NULL
;
1521 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1523 /* make sure finalize_textures has been called?
1525 if (0) st_validate_state(st
);
1527 /* determine if copying depth or color data */
1528 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1529 texBaseFormat
== GL_DEPTH_STENCIL
) {
1530 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1531 if (strb
->Base
.Wrapped
) {
1532 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
1536 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1537 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1540 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1541 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1545 sample_count
= strb
->surface
->texture
->nr_samples
;
1546 /* I believe this would be legal, presumably would need to do a resolve
1547 for color, and for depth/stencil spec says to just use one of the
1548 depth/stencil samples per pixel? Need some transfer clarifications. */
1549 assert(sample_count
< 2);
1575 if (width
< 0 || height
< 0)
1580 assert(strb
->surface
);
1581 assert(stImage
->pt
);
1583 src_format
= strb
->surface
->format
;
1584 dest_format
= stImage
->pt
->format
;
1587 * Determine if the src framebuffer and dest texture have the same
1588 * base format. We need this to detect a case such as the framebuffer
1589 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1590 * texture format stores RGBA we need to set A=1 (overriding the
1591 * framebuffer's alpha values). We can't do that with the blit or
1592 * textured-quad paths.
1594 matching_base_formats
=
1595 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1596 _mesa_get_format_base_format(texImage
->TexFormat
));
1597 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1599 if (ctx
->_ImageTransferState
== 0x0) {
1601 if (matching_base_formats
&&
1602 src_format
== dest_format
&&
1605 /* use surface_copy() / blit */
1606 struct pipe_box src_box
;
1607 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
1608 width
, height
, &src_box
);
1610 /* for resource_copy_region(), y=0=top, always */
1611 pipe
->resource_copy_region(pipe
,
1615 destX
, destY
, destZ
+ stImage
->face
,
1618 strb
->surface
->u
.tex
.level
,
1620 use_fallback
= GL_FALSE
;
1622 else if (format_writemask
&&
1623 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1624 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1625 screen
->is_format_supported(screen
, src_format
,
1626 PIPE_TEXTURE_2D
, sample_count
,
1627 PIPE_BIND_SAMPLER_VIEW
,
1629 screen
->is_format_supported(screen
, dest_format
,
1631 PIPE_BIND_RENDER_TARGET
,
1633 /* draw textured quad to do the copy */
1635 struct pipe_surface surf_tmpl
;
1636 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1637 surf_tmpl
.format
= stImage
->pt
->format
;
1638 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
1639 surf_tmpl
.u
.tex
.level
= stImage
->level
;
1640 surf_tmpl
.u
.tex
.first_layer
= stImage
->face
+ destZ
;
1641 surf_tmpl
.u
.tex
.last_layer
= stImage
->face
+ destZ
;
1643 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1647 srcY1
= strb
->Base
.Height
- srcY
- height
;
1648 srcY0
= srcY1
+ height
;
1652 srcY1
= srcY0
+ height
;
1655 util_blit_pixels_writemask(st
->blit
,
1657 strb
->surface
->u
.tex
.level
,
1659 srcX
+ width
, srcY1
,
1660 strb
->surface
->u
.tex
.first_layer
,
1663 destX
+ width
, destY
+ height
,
1664 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1666 use_fallback
= GL_FALSE
;
1670 pipe_surface_reference(&dest_surface
, NULL
);
1674 /* software fallback */
1675 fallback_copy_texsubimage(ctx
, target
, level
,
1676 strb
, stImage
, texBaseFormat
,
1677 destX
, destY
, destZ
,
1678 srcX
, srcY
, width
, height
);
1685 st_CopyTexImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1686 GLenum internalFormat
,
1687 GLint x
, GLint y
, GLsizei width
, GLint border
)
1689 struct gl_texture_unit
*texUnit
=
1690 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1691 struct gl_texture_object
*texObj
=
1692 _mesa_select_tex_object(ctx
, texUnit
, target
);
1693 struct gl_texture_image
*texImage
=
1694 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1696 /* Setup or redefine the texture object, texture and texture
1697 * image. Don't populate yet.
1699 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1701 GL_RGBA
, CHAN_TYPE
, NULL
,
1702 &ctx
->DefaultPacking
, texObj
, texImage
);
1704 st_copy_texsubimage(ctx
, target
, level
,
1705 0, 0, 0, /* destX,Y,Z */
1706 x
, y
, width
, 1); /* src X, Y, size */
1711 st_CopyTexImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1712 GLenum internalFormat
,
1713 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1716 struct gl_texture_unit
*texUnit
=
1717 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1718 struct gl_texture_object
*texObj
=
1719 _mesa_select_tex_object(ctx
, texUnit
, target
);
1720 struct gl_texture_image
*texImage
=
1721 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1723 /* Setup or redefine the texture object, texture and texture
1724 * image. Don't populate yet.
1726 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1727 width
, height
, border
,
1728 GL_RGBA
, CHAN_TYPE
, NULL
,
1729 &ctx
->DefaultPacking
, texObj
, texImage
);
1731 st_copy_texsubimage(ctx
, target
, level
,
1732 0, 0, 0, /* destX,Y,Z */
1733 x
, y
, width
, height
); /* src X, Y, size */
1738 st_CopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1739 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1741 const GLint yoffset
= 0, zoffset
= 0;
1742 const GLsizei height
= 1;
1743 st_copy_texsubimage(ctx
, target
, level
,
1744 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1745 x
, y
, width
, height
); /* src X, Y, size */
1750 st_CopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1751 GLint xoffset
, GLint yoffset
,
1752 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1754 const GLint zoffset
= 0;
1755 st_copy_texsubimage(ctx
, target
, level
,
1756 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1757 x
, y
, width
, height
); /* src X, Y, size */
1762 st_CopyTexSubImage3D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1763 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1764 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1766 st_copy_texsubimage(ctx
, target
, level
,
1767 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1768 x
, y
, width
, height
); /* src X, Y, size */
1773 * Copy image data from stImage into the texture object 'stObj' at level
1777 copy_image_data_to_texture(struct st_context
*st
,
1778 struct st_texture_object
*stObj
,
1780 struct st_texture_image
*stImage
)
1784 const struct gl_texture_image
*dstImage
=
1785 stObj
->base
.Image
[stImage
->face
][stImage
->level
];
1787 assert(dstImage
->Width
== stImage
->base
.Width
);
1788 assert(dstImage
->Height
== stImage
->base
.Height
);
1789 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1793 /* Copy potentially with the blitter:
1795 st_texture_image_copy(st
->pipe
,
1796 stObj
->pt
, dstLevel
, /* dest texture, level */
1797 stImage
->pt
, stImage
->level
, /* src texture, level */
1800 pipe_resource_reference(&stImage
->pt
, NULL
);
1802 else if (stImage
->base
.Data
) {
1803 st_texture_image_data(st
,
1808 stImage
->base
.RowStride
*
1809 util_format_get_blocksize(stObj
->pt
->format
),
1810 stImage
->base
.RowStride
*
1811 stImage
->base
.Height
*
1812 util_format_get_blocksize(stObj
->pt
->format
));
1813 _mesa_align_free(stImage
->base
.Data
);
1814 stImage
->base
.Data
= NULL
;
1817 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1822 * Called during state validation. When this function is finished,
1823 * the texture object should be ready for rendering.
1824 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1827 st_finalize_texture(struct gl_context
*ctx
,
1828 struct pipe_context
*pipe
,
1829 struct gl_texture_object
*tObj
)
1831 struct st_context
*st
= st_context(ctx
);
1832 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1833 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1835 struct st_texture_image
*firstImage
;
1836 enum pipe_format firstImageFormat
;
1838 if (stObj
->base
._Complete
) {
1839 /* The texture is complete and we know exactly how many mipmap levels
1840 * are present/needed. This is conditional because we may be called
1841 * from the st_generate_mipmap() function when the texture object is
1842 * incomplete. In that case, we'll have set stObj->lastLevel before
1845 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1846 stObj
->base
.MinFilter
== GL_NEAREST
)
1847 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1849 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1852 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1855 /* If both firstImage and stObj point to a texture which can contain
1856 * all active images, favour firstImage. Note that because of the
1857 * completeness requirement, we know that the image dimensions
1860 if (firstImage
->pt
&&
1861 firstImage
->pt
!= stObj
->pt
&&
1862 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1863 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1864 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1867 /* Find gallium format for the Mesa texture */
1868 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1870 /* If we already have a gallium texture, check that it matches the texture
1871 * object's format, target, size, num_levels, etc.
1874 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1875 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1876 stObj
->pt
->last_level
< stObj
->lastLevel
||
1877 stObj
->pt
->width0
!= stObj
->width0
||
1878 stObj
->pt
->height0
!= stObj
->height0
||
1879 stObj
->pt
->depth0
!= stObj
->depth0
)
1881 /* The gallium texture does not match the Mesa texture so delete the
1882 * gallium texture now. We'll make a new one below.
1884 pipe_resource_reference(&stObj
->pt
, NULL
);
1885 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1886 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1890 /* May need to create a new gallium texture:
1893 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1895 stObj
->pt
= st_texture_create(st
,
1896 gl_target_to_pipe(stObj
->base
.Target
),
1905 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1910 /* Pull in any images not in the object's texture:
1912 for (face
= 0; face
< nr_faces
; face
++) {
1914 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1915 struct st_texture_image
*stImage
=
1916 st_texture_image(stObj
->base
.Image
[face
][level
]);
1918 /* Need to import images in main memory or held in other textures.
1920 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1921 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1931 * Returns pointer to a default/dummy texture.
1932 * This is typically used when the current shader has tex/sample instructions
1933 * but the user has not provided a (any) texture(s).
1935 struct gl_texture_object
*
1936 st_get_default_texture(struct st_context
*st
)
1938 if (!st
->default_texture
) {
1939 static const GLenum target
= GL_TEXTURE_2D
;
1940 GLubyte pixels
[16][16][4];
1941 struct gl_texture_object
*texObj
;
1942 struct gl_texture_image
*texImg
;
1945 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1946 * when attempting to sample incomplete textures.
1948 for (i
= 0; i
< 16; i
++) {
1949 for (j
= 0; j
< 16; j
++) {
1950 pixels
[i
][j
][0] = 0;
1951 pixels
[i
][j
][1] = 0;
1952 pixels
[i
][j
][2] = 0;
1953 pixels
[i
][j
][3] = 255;
1957 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1959 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1961 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1962 16, 16, 1, 0, /* w, h, d, border */
1963 GL_RGBA
, MESA_FORMAT_RGBA8888
);
1965 st_TexImage(st
->ctx
, 2, target
,
1966 0, GL_RGBA
, /* level, intformat */
1967 16, 16, 1, 0, /* w, h, d, border */
1968 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1969 &st
->ctx
->DefaultPacking
,
1973 texObj
->MinFilter
= GL_NEAREST
;
1974 texObj
->MagFilter
= GL_NEAREST
;
1975 texObj
->_Complete
= GL_TRUE
;
1977 st
->default_texture
= texObj
;
1979 return st
->default_texture
;
1984 st_init_texture_functions(struct dd_function_table
*functions
)
1986 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1987 functions
->TexImage1D
= st_TexImage1D
;
1988 functions
->TexImage2D
= st_TexImage2D
;
1989 functions
->TexImage3D
= st_TexImage3D
;
1990 functions
->TexSubImage1D
= st_TexSubImage1D
;
1991 functions
->TexSubImage2D
= st_TexSubImage2D
;
1992 functions
->TexSubImage3D
= st_TexSubImage3D
;
1993 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1994 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1995 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1996 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1997 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1998 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1999 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
2000 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
2001 functions
->GenerateMipmap
= st_generate_mipmap
;
2003 functions
->GetTexImage
= st_GetTexImage
;
2005 /* compressed texture functions */
2006 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
2007 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
2009 functions
->NewTextureObject
= st_NewTextureObject
;
2010 functions
->NewTextureImage
= st_NewTextureImage
;
2011 functions
->DeleteTexture
= st_DeleteTextureObject
;
2012 functions
->FreeTexImageData
= st_FreeTextureImageData
;
2014 functions
->TextureMemCpy
= do_memcpy
;
2016 /* XXX Temporary until we can query pipe's texture sizes */
2017 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;