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"
31 #include "main/convolve.h"
33 #include "main/enums.h"
34 #include "main/fbobject.h"
35 #include "main/formats.h"
36 #include "main/image.h"
37 #include "main/imports.h"
38 #include "main/macros.h"
39 #include "main/mipmap.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_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_inlines.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"
69 #define DBG if (0) printf
72 static enum pipe_texture_target
73 gl_target_to_pipe(GLenum target
)
77 return PIPE_TEXTURE_1D
;
80 case GL_TEXTURE_RECTANGLE_NV
:
81 return PIPE_TEXTURE_2D
;
84 return PIPE_TEXTURE_3D
;
86 case GL_TEXTURE_CUBE_MAP_ARB
:
87 return PIPE_TEXTURE_CUBE
;
96 /** called via ctx->Driver.NewTextureImage() */
97 static struct gl_texture_image
*
98 st_NewTextureImage(GLcontext
* ctx
)
100 DBG("%s\n", __FUNCTION__
);
102 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
106 /** called via ctx->Driver.NewTextureObject() */
107 static struct gl_texture_object
*
108 st_NewTextureObject(GLcontext
* ctx
, GLuint name
, GLenum target
)
110 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
112 DBG("%s\n", __FUNCTION__
);
113 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
118 /** called via ctx->Driver.DeleteTextureImage() */
120 st_DeleteTextureObject(GLcontext
*ctx
,
121 struct gl_texture_object
*texObj
)
123 struct st_context
*st
= st_context(ctx
);
124 struct st_texture_object
*stObj
= st_texture_object(texObj
);
126 pipe_resource_reference(&stObj
->pt
, NULL
);
127 if (stObj
->sampler_view
) {
128 if (stObj
->sampler_view
->context
!= st
->pipe
) {
129 /* Take "ownership" of this texture sampler view by setting
130 * its context pointer to this context. This avoids potential
131 * crashes when the texture object is shared among contexts
132 * and the original/owner context has already been destroyed.
134 stObj
->sampler_view
->context
= st
->pipe
;
136 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
138 _mesa_delete_texture_object(ctx
, texObj
);
142 /** called via ctx->Driver.FreeTexImageData() */
144 st_FreeTextureImageData(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
146 struct st_texture_image
*stImage
= st_texture_image(texImage
);
148 DBG("%s\n", __FUNCTION__
);
151 pipe_resource_reference(&stImage
->pt
, NULL
);
154 if (texImage
->Data
) {
155 _mesa_align_free(texImage
->Data
);
156 texImage
->Data
= NULL
;
162 * From linux kernel i386 header files, copes with odd sizes better
163 * than COPY_DWORDS would:
164 * XXX Put this in src/mesa/main/imports.h ???
166 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
168 __memcpy(void *to
, const void *from
, size_t n
)
171 __asm__
__volatile__("rep ; movsl\n\t"
175 "1:\ttestb $1,%b4\n\t"
177 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
178 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
183 #define __memcpy(a,b,c) memcpy(a,b,c)
188 * The system memcpy (at least on ubuntu 5.10) has problems copying
189 * to agp (writecombined) memory from a source which isn't 64-byte
190 * aligned - there is a 4x performance falloff.
192 * The x86 __memcpy is immune to this but is slightly slower
193 * (10%-ish) than the system memcpy.
195 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
196 * isn't much faster than x86_memcpy for agp copies.
198 * TODO: switch dynamically.
201 do_memcpy(void *dest
, const void *src
, size_t n
)
203 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
204 return __memcpy(dest
, src
, n
);
207 return memcpy(dest
, src
, n
);
212 * Return default texture resource binding bitmask for the given format.
215 default_bindings(struct st_context
*st
, enum pipe_format format
)
217 struct pipe_screen
*screen
= st
->pipe
->screen
;
218 const unsigned target
= PIPE_TEXTURE_2D
;
219 const unsigned geom
= 0x0;
222 if (util_format_is_depth_or_stencil(format
))
223 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
225 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
227 if (screen
->is_format_supported(screen
, format
, target
, bindings
, geom
))
230 return PIPE_BIND_SAMPLER_VIEW
;
234 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
236 get_texture_dims(GLenum target
)
240 case GL_TEXTURE_1D_ARRAY_EXT
:
243 case GL_TEXTURE_CUBE_MAP_ARB
:
244 case GL_TEXTURE_RECTANGLE_NV
:
245 case GL_TEXTURE_2D_ARRAY_EXT
:
250 assert(0 && "invalid texture target in get_texture_dims()");
257 * Try to allocate a pipe_resource object for the given st_texture_object.
259 * We use the given st_texture_image as a clue to determine the size of the
260 * mipmap image at level=0.
264 guess_and_alloc_texture(struct st_context
*st
,
265 struct st_texture_object
*stObj
,
266 const struct st_texture_image
*stImage
)
268 const GLuint dims
= get_texture_dims(stObj
->base
.Target
);
269 GLuint level
, lastLevel
, width
, height
, depth
;
271 enum pipe_format fmt
;
273 DBG("%s\n", __FUNCTION__
);
277 level
= stImage
->level
;
278 width
= stImage
->base
.Width2
; /* size w/out border */
279 height
= stImage
->base
.Height2
;
280 depth
= stImage
->base
.Depth2
;
286 /* Depending on the image's size, we can't always make a guess here.
289 if ( (dims
>= 1 && width
== 1) ||
290 (dims
>= 2 && height
== 1) ||
291 (dims
>= 3 && depth
== 1) ) {
292 /* we can't determine the image size at level=0 */
293 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
298 /* grow the image size until we hit level = 0 */
311 /* At this point, (width x height x depth) is the expected size of
312 * the level=0 mipmap image.
315 /* Guess a reasonable value for lastLevel. This is probably going
316 * to be wrong fairly often and might mean that we have to look at
317 * resizable buffers, or require that buffers implement lazy
318 * pagetable arrangements.
320 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
321 stObj
->base
.MinFilter
== GL_LINEAR
||
322 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
323 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
324 !stObj
->base
.GenerateMipmap
&&
325 stImage
->level
== 0) {
326 /* only alloc space for a single mipmap level */
330 /* alloc space for a full mipmap */
331 GLuint l2width
= util_logbase2(width
);
332 GLuint l2height
= util_logbase2(height
);
333 GLuint l2depth
= util_logbase2(depth
);
334 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
337 /* Save the level=0 dimensions */
338 stObj
->width0
= width
;
339 stObj
->height0
= height
;
340 stObj
->depth0
= depth
;
342 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
344 bindings
= default_bindings(st
, fmt
);
346 stObj
->pt
= st_texture_create(st
,
347 gl_target_to_pipe(stObj
->base
.Target
),
355 DBG("%s - success\n", __FUNCTION__
);
360 * Adjust pixel unpack params and image dimensions to strip off the
362 * Gallium doesn't support texture borders. They've seldem been used
363 * and seldom been implemented correctly anyway.
364 * \param unpackNew returns the new pixel unpack parameters
367 strip_texture_border(GLint border
,
368 GLint
*width
, GLint
*height
, GLint
*depth
,
369 const struct gl_pixelstore_attrib
*unpack
,
370 struct gl_pixelstore_attrib
*unpackNew
)
372 assert(border
> 0); /* sanity check */
374 *unpackNew
= *unpack
;
376 if (unpackNew
->RowLength
== 0)
377 unpackNew
->RowLength
= *width
;
379 if (depth
&& unpackNew
->ImageHeight
== 0)
380 unpackNew
->ImageHeight
= *height
;
382 unpackNew
->SkipPixels
+= border
;
384 unpackNew
->SkipRows
+= border
;
386 unpackNew
->SkipImages
+= border
;
389 *width
= *width
- 2 * border
;
390 if (height
&& *height
>= 3)
391 *height
= *height
- 2 * border
;
392 if (depth
&& *depth
>= 3)
393 *depth
= *depth
- 2 * border
;
398 * Try to do texture compression via rendering. If the Gallium driver
399 * can render into a compressed surface this will allow us to do texture
401 * \return GL_TRUE for success, GL_FALSE for failure
404 compress_with_blit(GLcontext
* ctx
,
405 GLenum target
, GLint level
,
406 GLint xoffset
, GLint yoffset
, GLint zoffset
,
407 GLint width
, GLint height
, GLint depth
,
408 GLenum format
, GLenum type
, const void *pixels
,
409 const struct gl_pixelstore_attrib
*unpack
,
410 struct gl_texture_image
*texImage
)
412 const GLuint dstImageOffsets
[1] = {0};
413 struct st_texture_image
*stImage
= st_texture_image(texImage
);
414 struct st_context
*st
= st_context(ctx
);
415 struct pipe_context
*pipe
= st
->pipe
;
416 struct pipe_screen
*screen
= pipe
->screen
;
417 gl_format mesa_format
;
418 struct pipe_resource templ
;
419 struct pipe_resource
*src_tex
;
420 struct pipe_sampler_view view_templ
;
421 struct pipe_sampler_view
*src_view
;
422 struct pipe_surface
*dst_surface
;
423 struct pipe_transfer
*tex_xfer
;
427 /* XXX: Can this happen? Should we assert? */
431 /* get destination surface (in the compressed texture) */
432 dst_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
433 stImage
->face
, stImage
->level
, 0,
434 PIPE_BIND_BLIT_DESTINATION
);
436 /* can't render into this format (or other problem) */
440 /* Choose format for the temporary RGBA texture image.
442 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
447 /* Create the temporary source texture
449 memset(&templ
, 0, sizeof(templ
));
450 templ
.target
= PIPE_TEXTURE_2D
;
451 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
);
452 templ
.width0
= width
;
453 templ
.height0
= height
;
455 templ
.last_level
= 0;
456 templ
.usage
= PIPE_USAGE_DEFAULT
;
457 templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
458 src_tex
= screen
->resource_create(screen
, &templ
);
463 /* Put user's tex data into the temporary texture
465 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
), src_tex
,
466 0, 0, 0, /* face, level are zero */
468 0, 0, width
, height
); /* x, y, w, h */
469 map
= pipe_transfer_map(pipe
, tex_xfer
);
471 _mesa_texstore(ctx
, 2, GL_RGBA
, mesa_format
,
473 0, 0, 0, /* dest x/y/z offset */
474 tex_xfer
->stride
, /* dest row stride (bytes) */
475 dstImageOffsets
, /* image offsets (for 3D only) */
476 width
, height
, 1, /* size */
477 format
, type
, /* source format/type */
478 pixels
, /* source data */
479 unpack
); /* source data packing */
481 pipe_transfer_unmap(pipe
, tex_xfer
);
482 pipe
->transfer_destroy(pipe
, tex_xfer
);
484 /* Create temporary sampler view */
485 u_sampler_view_default_template(&view_templ
,
488 src_view
= pipe
->create_sampler_view(pipe
, src_tex
, &view_templ
);
491 /* copy / compress image */
492 util_blit_pixels_tex(st
->blit
,
493 src_view
, /* sampler view (src) */
494 0, 0, /* src x0, y0 */
495 width
, height
, /* src x1, y1 */
496 dst_surface
, /* pipe_surface (dst) */
497 xoffset
, yoffset
, /* dst x0, y0 */
498 xoffset
+ width
, /* dst x1 */
499 yoffset
+ height
, /* dst y1 */
501 PIPE_TEX_MIPFILTER_NEAREST
);
503 pipe_surface_reference(&dst_surface
, NULL
);
504 pipe_resource_reference(&src_tex
, NULL
);
505 pipe_sampler_view_reference(&src_view
, NULL
);
512 * Do glTexImage1/2/3D().
515 st_TexImage(GLcontext
* ctx
,
517 GLenum target
, GLint level
,
518 GLint internalFormat
,
519 GLint width
, GLint height
, GLint depth
,
521 GLenum format
, GLenum type
, const void *pixels
,
522 const struct gl_pixelstore_attrib
*unpack
,
523 struct gl_texture_object
*texObj
,
524 struct gl_texture_image
*texImage
,
525 GLsizei imageSize
, GLboolean compressed_src
)
527 struct st_context
*st
= st_context(ctx
);
528 struct pipe_screen
*screen
= st
->pipe
->screen
;
529 struct st_texture_object
*stObj
= st_texture_object(texObj
);
530 struct st_texture_image
*stImage
= st_texture_image(texImage
);
531 GLint postConvWidth
, postConvHeight
;
532 GLint texelBytes
, sizeInBytes
;
533 GLuint dstRowStride
= 0;
534 struct gl_pixelstore_attrib unpackNB
;
535 enum pipe_transfer_usage transfer_usage
= 0;
537 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
538 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
540 /* switch to "normal" */
541 if (stObj
->surface_based
) {
542 _mesa_clear_texture_object(ctx
, texObj
);
543 stObj
->surface_based
= GL_FALSE
;
546 /* gallium does not support texture borders, strip it off */
548 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
550 texImage
->Width
= width
;
551 texImage
->Height
= height
;
552 texImage
->Depth
= depth
;
553 texImage
->Border
= 0;
557 postConvWidth
= width
;
558 postConvHeight
= height
;
560 stImage
->face
= _mesa_tex_target_to_face(target
);
561 stImage
->level
= level
;
564 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
565 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
570 _mesa_set_fetch_functions(texImage
, dims
);
572 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
573 /* must be a compressed format */
577 texelBytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
579 /* Minimum pitch of 32 bytes */
580 if (postConvWidth
* texelBytes
< 32) {
581 postConvWidth
= 32 / texelBytes
;
582 texImage
->RowStride
= postConvWidth
;
585 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
586 /*assert(texImage->RowStride == postConvWidth);*/
589 /* Release the reference to a potentially orphaned buffer.
590 * Release any old malloced memory.
593 pipe_resource_reference(&stImage
->pt
, NULL
);
594 assert(!texImage
->Data
);
596 else if (texImage
->Data
) {
597 _mesa_align_free(texImage
->Data
);
601 * See if the new image is somehow incompatible with the existing
602 * mipmap. If so, free the old mipmap.
605 if (level
> (GLint
) stObj
->pt
->last_level
||
606 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
607 stImage
->face
, stImage
->level
)) {
609 pipe_resource_reference(&stObj
->pt
, NULL
);
611 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
615 if (width
== 0 || height
== 0 || depth
== 0) {
616 /* stop after freeing old image */
621 guess_and_alloc_texture(st
, stObj
, stImage
);
623 /* Probably out of memory.
624 * Try flushing any pending rendering, then retry.
627 guess_and_alloc_texture(st
, stObj
, stImage
);
629 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
635 assert(!stImage
->pt
);
638 st_texture_match_image(stObj
->pt
, &stImage
->base
,
639 stImage
->face
, stImage
->level
)) {
641 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
646 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
648 /* st_CopyTexImage calls this function with pixels == NULL, with
649 * the expectation that the texture will be set up but nothing
650 * more will be done. This is where those calls return:
652 if (compressed_src
) {
653 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
655 "glCompressedTexImage");
658 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
660 pixels
, unpack
, "glTexImage");
663 /* Note: we can't check for pixels==NULL until after we've allocated
664 * memory for the texture.
667 /* See if we can do texture compression with a blit/render.
669 if (!compressed_src
&&
671 _mesa_is_format_compressed(texImage
->TexFormat
) &&
672 screen
->is_format_supported(screen
,
675 PIPE_BIND_RENDER_TARGET
, 0)) {
679 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
680 format
, type
, pixels
, unpack
, texImage
)) {
686 if (format
== GL_DEPTH_COMPONENT
&&
687 util_format_is_depth_and_stencil(stImage
->pt
->format
))
688 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
690 transfer_usage
= PIPE_TRANSFER_WRITE
;
692 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
693 transfer_usage
, 0, 0,
695 stImage
->base
.Height
);
696 if(stImage
->transfer
)
697 dstRowStride
= stImage
->transfer
->stride
;
700 /* Allocate regular memory and store the image there temporarily. */
701 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
702 sizeInBytes
= _mesa_format_image_size(texImage
->TexFormat
,
706 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
710 dstRowStride
= postConvWidth
* texelBytes
;
711 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
714 texImage
->Data
= _mesa_align_malloc(sizeInBytes
, 16);
717 if (!texImage
->Data
) {
718 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
725 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
726 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
728 /* Copy data. Would like to know when it's ok for us to eg. use
729 * the blitter to copy. Or, use the hardware to do the format
730 * conversion and copy:
732 if (compressed_src
) {
733 const GLuint srcImageStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
734 if(dstRowStride
== srcImageStride
)
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
)
746 memcpy(dst
, src
, srcImageStride
);
748 src
+= srcImageStride
;
753 const GLuint srcImageStride
=
754 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
756 const GLubyte
*src
= (const GLubyte
*) pixels
;
758 for (i
= 0; i
< depth
; i
++) {
759 if (!_mesa_texstore(ctx
, dims
,
760 texImage
->_BaseFormat
,
763 0, 0, 0, /* dstX/Y/Zoffset */
765 texImage
->ImageOffsets
,
767 format
, type
, src
, unpack
)) {
768 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
771 if (stImage
->pt
&& i
+ 1 < depth
) {
772 /* unmap this slice */
773 st_texture_image_unmap(st
, stImage
);
774 /* map next slice of 3D texture */
775 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
776 transfer_usage
, 0, 0,
778 stImage
->base
.Height
);
779 src
+= srcImageStride
;
785 _mesa_unmap_teximage_pbo(ctx
, unpack
);
787 if (stImage
->pt
&& texImage
->Data
) {
788 st_texture_image_unmap(st
, stImage
);
789 texImage
->Data
= NULL
;
795 st_TexImage3D(GLcontext
* ctx
,
796 GLenum target
, GLint level
,
797 GLint internalFormat
,
798 GLint width
, GLint height
, GLint depth
,
800 GLenum format
, GLenum type
, const void *pixels
,
801 const struct gl_pixelstore_attrib
*unpack
,
802 struct gl_texture_object
*texObj
,
803 struct gl_texture_image
*texImage
)
805 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
806 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
812 st_TexImage2D(GLcontext
* ctx
,
813 GLenum target
, GLint level
,
814 GLint internalFormat
,
815 GLint width
, GLint height
, GLint border
,
816 GLenum format
, GLenum type
, const void *pixels
,
817 const struct gl_pixelstore_attrib
*unpack
,
818 struct gl_texture_object
*texObj
,
819 struct gl_texture_image
*texImage
)
821 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
822 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
827 st_TexImage1D(GLcontext
* ctx
,
828 GLenum target
, GLint level
,
829 GLint internalFormat
,
830 GLint width
, GLint border
,
831 GLenum format
, GLenum type
, const void *pixels
,
832 const struct gl_pixelstore_attrib
*unpack
,
833 struct gl_texture_object
*texObj
,
834 struct gl_texture_image
*texImage
)
836 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
837 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
842 st_CompressedTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
843 GLint internalFormat
,
844 GLint width
, GLint height
, GLint border
,
845 GLsizei imageSize
, const GLvoid
*data
,
846 struct gl_texture_object
*texObj
,
847 struct gl_texture_image
*texImage
)
849 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
850 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
856 * glGetTexImage() helper: decompress a compressed texture by rendering
857 * a textured quad. Store the results in the user's buffer.
860 decompress_with_blit(GLcontext
* ctx
, GLenum target
, GLint level
,
861 GLenum format
, GLenum type
, GLvoid
*pixels
,
862 struct gl_texture_object
*texObj
,
863 struct gl_texture_image
*texImage
)
865 struct st_context
*st
= st_context(ctx
);
866 struct pipe_context
*pipe
= st
->pipe
;
867 struct pipe_screen
*screen
= pipe
->screen
;
868 struct st_texture_image
*stImage
= st_texture_image(texImage
);
869 struct st_texture_object
*stObj
= st_texture_object(texObj
);
870 struct pipe_sampler_view
*src_view
=
871 st_get_texture_sampler_view(stObj
, pipe
);
872 const GLuint width
= texImage
->Width
;
873 const GLuint height
= texImage
->Height
;
874 struct pipe_surface
*dst_surface
;
875 struct pipe_resource
*dst_texture
;
876 struct pipe_transfer
*tex_xfer
;
877 unsigned bind
= (PIPE_BIND_BLIT_DESTINATION
|
878 PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
879 PIPE_BIND_TRANSFER_READ
);
881 /* create temp / dest surface */
882 if (!util_create_rgba_surface(screen
, width
, height
, bind
,
883 &dst_texture
, &dst_surface
)) {
884 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
885 "in decompress_with_blit()");
889 /* blit/render/decompress */
890 util_blit_pixels_tex(st
->blit
,
891 src_view
, /* pipe_resource (src) */
892 0, 0, /* src x0, y0 */
893 width
, height
, /* src x1, y1 */
894 dst_surface
, /* pipe_surface (dst) */
895 0, 0, /* dst x0, y0 */
896 width
, height
, /* dst x1, y1 */
898 PIPE_TEX_MIPFILTER_NEAREST
);
900 /* map the dst_surface so we can read from it */
901 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
),
902 dst_texture
, 0, 0, 0,
904 0, 0, width
, height
);
906 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
908 /* copy/pack data into user buffer */
909 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
911 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
912 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
914 for (row
= 0; row
< height
; row
++) {
915 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
916 height
, format
, type
, row
, 0);
917 memcpy(dest
, map
, bytesPerRow
);
918 map
+= tex_xfer
->stride
;
920 pipe_transfer_unmap(pipe
, tex_xfer
);
923 /* format translation via floats */
925 for (row
= 0; row
< height
; row
++) {
926 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
927 GLfloat rgba
[4 * MAX_WIDTH
];
928 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
929 height
, format
, type
, row
, 0);
931 if (ST_DEBUG
& DEBUG_FALLBACK
)
932 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
934 /* get float[4] rgba row from surface */
935 pipe_get_tile_rgba(pipe
, tex_xfer
, 0, row
, width
, 1, rgba
);
937 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
938 type
, dest
, &ctx
->Pack
, transferOps
);
942 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
944 pipe
->transfer_destroy(pipe
, tex_xfer
);
946 /* destroy the temp / dest surface */
947 util_destroy_rgba_surface(dst_texture
, dst_surface
);
953 * Need to map texture image into memory before copying image data,
957 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
958 GLenum format
, GLenum type
, GLvoid
* pixels
,
959 struct gl_texture_object
*texObj
,
960 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
962 struct st_context
*st
= st_context(ctx
);
963 struct st_texture_image
*stImage
= st_texture_image(texImage
);
964 const GLuint dstImageStride
=
965 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
971 util_format_is_s3tc(stImage
->pt
->format
) &&
973 /* Need to decompress the texture.
974 * We'll do this by rendering a textured quad.
975 * Note that we only expect RGBA formats (no Z/depth formats).
977 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
984 /* Image is stored in hardware format in a buffer managed by the
985 * kernel. Need to explicitly map and unmap it.
987 unsigned face
= _mesa_tex_target_to_face(target
);
989 st_teximage_flush_before_map(st
, stImage
->pt
, face
, level
,
992 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
993 PIPE_TRANSFER_READ
, 0, 0,
995 stImage
->base
.Height
);
996 /* compute stride in texels from stride in bytes */
997 texImage
->RowStride
= stImage
->transfer
->stride
998 * util_format_get_blockwidth(stImage
->pt
->format
)
999 / util_format_get_blocksize(stImage
->pt
->format
);
1002 /* Otherwise, the image should actually be stored in
1003 * texImage->Data. This is pretty confusing for
1004 * everybody, I'd much prefer to separate the two functions of
1005 * texImage->Data - storage for texture images in main memory
1006 * and access (ie mappings) of images. In other words, we'd
1007 * create a new texImage->Map field and leave Data simply for
1010 assert(texImage
->Data
);
1013 depth
= texImage
->Depth
;
1014 texImage
->Depth
= 1;
1016 dest
= (GLubyte
*) pixels
;
1018 for (i
= 0; i
< depth
; i
++) {
1019 if (compressed_dst
) {
1020 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
1024 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
1028 if (stImage
->pt
&& i
+ 1 < depth
) {
1029 /* unmap this slice */
1030 st_texture_image_unmap(st
, stImage
);
1031 /* map next slice of 3D texture */
1032 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
1033 PIPE_TRANSFER_READ
, 0, 0,
1034 stImage
->base
.Width
,
1035 stImage
->base
.Height
);
1036 dest
+= dstImageStride
;
1040 texImage
->Depth
= depth
;
1044 st_texture_image_unmap(st
, stImage
);
1045 texImage
->Data
= NULL
;
1051 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
1052 GLenum format
, GLenum type
, GLvoid
* pixels
,
1053 struct gl_texture_object
*texObj
,
1054 struct gl_texture_image
*texImage
)
1056 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1062 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
1064 struct gl_texture_object
*texObj
,
1065 struct gl_texture_image
*texImage
)
1067 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1074 st_TexSubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
1075 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1076 GLint width
, GLint height
, GLint depth
,
1077 GLenum format
, GLenum type
, const void *pixels
,
1078 const struct gl_pixelstore_attrib
*packing
,
1079 struct gl_texture_object
*texObj
,
1080 struct gl_texture_image
*texImage
)
1082 struct st_context
*st
= st_context(ctx
);
1083 struct pipe_screen
*screen
= st
->pipe
->screen
;
1084 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1085 GLuint dstRowStride
;
1086 const GLuint srcImageStride
=
1087 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1090 /* init to silence warning only: */
1091 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1093 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1094 _mesa_lookup_enum_by_nr(target
),
1095 level
, xoffset
, yoffset
, width
, height
);
1098 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1099 type
, pixels
, packing
, "glTexSubImage2D");
1103 /* See if we can do texture compression with a blit/render.
1105 if (!ctx
->Mesa_DXTn
&&
1106 _mesa_is_format_compressed(texImage
->TexFormat
) &&
1107 screen
->is_format_supported(screen
,
1108 stImage
->pt
->format
,
1109 stImage
->pt
->target
,
1110 PIPE_BIND_RENDER_TARGET
, 0)) {
1111 if (compress_with_blit(ctx
, target
, level
,
1112 xoffset
, yoffset
, zoffset
,
1113 width
, height
, depth
,
1114 format
, type
, pixels
, packing
, texImage
)) {
1119 /* Map buffer if necessary. Need to lock to prevent other contexts
1120 * from uploading the buffer under us.
1123 unsigned face
= _mesa_tex_target_to_face(target
);
1125 if (format
== GL_DEPTH_COMPONENT
&&
1126 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1127 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1129 transfer_usage
= PIPE_TRANSFER_WRITE
;
1131 st_teximage_flush_before_map(st
, stImage
->pt
, face
, level
,
1133 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1139 if (!texImage
->Data
) {
1140 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1144 src
= (const GLubyte
*) pixels
;
1145 dstRowStride
= stImage
->transfer
->stride
;
1147 for (i
= 0; i
< depth
; i
++) {
1148 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1149 texImage
->TexFormat
,
1153 texImage
->ImageOffsets
,
1155 format
, type
, src
, packing
)) {
1156 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1159 if (stImage
->pt
&& i
+ 1 < depth
) {
1160 /* unmap this slice */
1161 st_texture_image_unmap(st
, stImage
);
1162 /* map next slice of 3D texture */
1163 texImage
->Data
= st_texture_image_map(st
, stImage
,
1168 src
+= srcImageStride
;
1173 _mesa_unmap_teximage_pbo(ctx
, packing
);
1175 if (stImage
->pt
&& texImage
->Data
) {
1176 st_texture_image_unmap(st
, stImage
);
1177 texImage
->Data
= NULL
;
1184 st_TexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1185 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1186 GLsizei width
, GLsizei height
, GLsizei depth
,
1187 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1188 const struct gl_pixelstore_attrib
*packing
,
1189 struct gl_texture_object
*texObj
,
1190 struct gl_texture_image
*texImage
)
1192 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1193 width
, height
, depth
, format
, type
,
1194 pixels
, packing
, texObj
, texImage
);
1199 st_TexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1200 GLint xoffset
, GLint yoffset
,
1201 GLsizei width
, GLsizei height
,
1202 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1203 const struct gl_pixelstore_attrib
*packing
,
1204 struct gl_texture_object
*texObj
,
1205 struct gl_texture_image
*texImage
)
1207 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1208 width
, height
, 1, format
, type
,
1209 pixels
, packing
, texObj
, texImage
);
1214 st_TexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1215 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1216 const GLvoid
* pixels
,
1217 const struct gl_pixelstore_attrib
*packing
,
1218 struct gl_texture_object
*texObj
,
1219 struct gl_texture_image
*texImage
)
1221 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1222 format
, type
, pixels
, packing
, texObj
, texImage
);
1227 st_CompressedTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1228 GLint xoffset
, GLsizei width
,
1230 GLsizei imageSize
, const GLvoid
*data
,
1231 struct gl_texture_object
*texObj
,
1232 struct gl_texture_image
*texImage
)
1239 st_CompressedTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1240 GLint xoffset
, GLint yoffset
,
1241 GLsizei width
, GLint height
,
1243 GLsizei imageSize
, const GLvoid
*data
,
1244 struct gl_texture_object
*texObj
,
1245 struct gl_texture_image
*texImage
)
1247 struct st_context
*st
= st_context(ctx
);
1248 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1252 enum pipe_format pformat
;
1255 unsigned face
= _mesa_tex_target_to_face(target
);
1256 pformat
= stImage
->pt
->format
;
1258 st_teximage_flush_before_map(st
, stImage
->pt
, face
, level
,
1259 PIPE_TRANSFER_WRITE
);
1260 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1261 PIPE_TRANSFER_WRITE
,
1265 srcBlockStride
= util_format_get_stride(pformat
, width
);
1266 dstBlockStride
= stImage
->transfer
->stride
;
1268 assert(stImage
->pt
);
1269 /* TODO find good values for block and strides */
1270 /* TODO also adjust texImage->data for yoffset/xoffset */
1274 if (!texImage
->Data
) {
1275 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1279 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1280 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1281 assert(width
% util_format_get_blockwidth(pformat
) == 0);
1282 assert(height
% util_format_get_blockheight(pformat
) == 0);
1284 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1285 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1286 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1287 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1288 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1292 st_texture_image_unmap(st
, stImage
);
1293 texImage
->Data
= NULL
;
1299 st_CompressedTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1300 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1301 GLsizei width
, GLint height
, GLint depth
,
1303 GLsizei imageSize
, const GLvoid
*data
,
1304 struct gl_texture_object
*texObj
,
1305 struct gl_texture_image
*texImage
)
1313 * Do a CopyTexSubImage operation using a read transfer from the source,
1314 * a write transfer to the destination and get_tile()/put_tile() to access
1315 * the pixels/texels.
1317 * Note: srcY=0=TOP of renderbuffer
1320 fallback_copy_texsubimage(GLcontext
*ctx
, GLenum target
, GLint level
,
1321 struct st_renderbuffer
*strb
,
1322 struct st_texture_image
*stImage
,
1324 GLint destX
, GLint destY
, GLint destZ
,
1325 GLint srcX
, GLint srcY
,
1326 GLsizei width
, GLsizei height
)
1328 struct st_context
*st
= st_context(ctx
);
1329 struct pipe_context
*pipe
= st
->pipe
;
1330 struct pipe_transfer
*src_trans
;
1332 enum pipe_transfer_usage transfer_usage
;
1334 if (ST_DEBUG
& DEBUG_FALLBACK
)
1335 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1337 assert(width
<= MAX_WIDTH
);
1339 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1340 srcY
= strb
->Base
.Height
- srcY
- height
;
1343 src_trans
= st_cond_flush_get_tex_transfer( st_context(ctx
),
1350 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1351 baseFormat
== GL_DEPTH_STENCIL
) &&
1352 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1353 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1355 transfer_usage
= PIPE_TRANSFER_WRITE
;
1357 st_teximage_flush_before_map(st
, stImage
->pt
, 0, 0,
1360 texDest
= st_texture_image_map(st
, stImage
, 0, transfer_usage
,
1361 destX
, destY
, width
, height
);
1363 if (baseFormat
== GL_DEPTH_COMPONENT
||
1364 baseFormat
== GL_DEPTH_STENCIL
) {
1365 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1366 ctx
->Pixel
.DepthBias
!= 0.0F
);
1369 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1370 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1379 /* To avoid a large temp memory allocation, do copy row by row */
1380 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1381 uint data
[MAX_WIDTH
];
1382 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1384 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1386 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1392 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1394 if (tempSrc
&& texDest
) {
1395 const GLint dims
= 2;
1396 const GLint dstRowStride
= stImage
->transfer
->stride
;
1397 struct gl_texture_image
*texImage
= &stImage
->base
;
1398 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1400 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1401 unpack
.Invert
= GL_TRUE
;
1404 /* get float/RGBA image from framebuffer */
1405 /* XXX this usually involves a lot of int/float conversion.
1406 * try to avoid that someday.
1408 pipe_get_tile_rgba(pipe
, src_trans
, 0, 0, width
, height
, tempSrc
);
1410 /* Store into texture memory.
1411 * Note that this does some special things such as pixel transfer
1412 * ops and format conversion. In particular, if the dest tex format
1413 * is actually RGBA but the user created the texture as GL_RGB we
1414 * need to fill-in/override the alpha channel with 1.0.
1416 _mesa_texstore(ctx
, dims
,
1417 texImage
->_BaseFormat
,
1418 texImage
->TexFormat
,
1422 texImage
->ImageOffsets
,
1424 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1428 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1435 st_texture_image_unmap(st
, stImage
);
1436 pipe
->transfer_destroy(pipe
, src_trans
);
1442 * If the format of the src renderbuffer and the format of the dest
1443 * texture are compatible (in terms of blitting), return a TGSI writemask
1444 * to be used during the blit.
1445 * If the src/dest are incompatible, return 0.
1448 compatible_src_dst_formats(GLcontext
*ctx
,
1449 const struct gl_renderbuffer
*src
,
1450 const struct gl_texture_image
*dst
)
1452 /* Get logical base formats for the src and dest.
1453 * That is, use the user-requested formats and not the actual, device-
1455 * For example, the user may have requested an A8 texture but the
1456 * driver may actually be using an RGBA texture format. When we
1457 * copy/blit to that texture, we only want to copy the Alpha channel
1458 * and not the RGB channels.
1460 * Similarly, when the src FBO was created an RGB format may have been
1461 * requested but the driver actually chose an RGBA format. In that case,
1462 * we don't want to copy the undefined Alpha channel to the dest texture
1463 * (it should be 1.0).
1465 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1466 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1469 * XXX when we have red-only and red/green renderbuffers we'll need
1470 * to add more cases here (or implement a general-purpose routine that
1471 * queries the existance of the R,G,B,A channels in the src and dest).
1473 if (srcFormat
== dstFormat
) {
1474 /* This is the same as matching_base_formats, which should
1475 * always pass, as it did previously.
1477 return TGSI_WRITEMASK_XYZW
;
1479 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1480 /* Make sure that A in the dest is 1. The actual src format
1481 * may be RGBA and have undefined A values.
1483 return TGSI_WRITEMASK_XYZ
;
1485 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1486 /* Make sure that A in the dest is 1. The actual dst format
1487 * may be RGBA and will need A=1 to provide proper alpha values
1488 * when sampled later.
1490 return TGSI_WRITEMASK_XYZ
;
1493 if (ST_DEBUG
& DEBUG_FALLBACK
)
1494 debug_printf("%s failed for src %s, dst %s\n",
1496 _mesa_lookup_enum_by_nr(srcFormat
),
1497 _mesa_lookup_enum_by_nr(dstFormat
));
1508 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1509 * Note that the region to copy has already been clipped so we know we
1510 * won't read from outside the source renderbuffer's bounds.
1512 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1515 st_copy_texsubimage(GLcontext
*ctx
,
1516 GLenum target
, GLint level
,
1517 GLint destX
, GLint destY
, GLint destZ
,
1518 GLint srcX
, GLint srcY
,
1519 GLsizei width
, GLsizei height
)
1521 struct gl_texture_unit
*texUnit
=
1522 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1523 struct gl_texture_object
*texObj
=
1524 _mesa_select_tex_object(ctx
, texUnit
, target
);
1525 struct gl_texture_image
*texImage
=
1526 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1527 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1528 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1529 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1530 struct st_renderbuffer
*strb
;
1531 struct st_context
*st
= st_context(ctx
);
1532 struct pipe_context
*pipe
= st
->pipe
;
1533 struct pipe_screen
*screen
= pipe
->screen
;
1534 enum pipe_format dest_format
, src_format
;
1535 GLboolean use_fallback
= GL_TRUE
;
1536 GLboolean matching_base_formats
;
1537 GLuint format_writemask
;
1538 struct pipe_surface
*dest_surface
= NULL
;
1539 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1541 /* any rendering in progress must flushed before we grab the fb image */
1542 st_flush(st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
1544 /* make sure finalize_textures has been called?
1546 if (0) st_validate_state(st
);
1548 /* determine if copying depth or color data */
1549 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1550 texBaseFormat
== GL_DEPTH_STENCIL
) {
1551 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1554 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1555 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1558 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1559 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1587 if (width
< 0 || height
< 0)
1592 assert(strb
->surface
);
1593 assert(stImage
->pt
);
1595 src_format
= strb
->surface
->format
;
1596 dest_format
= stImage
->pt
->format
;
1599 * Determine if the src framebuffer and dest texture have the same
1600 * base format. We need this to detect a case such as the framebuffer
1601 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1602 * texture format stores RGBA we need to set A=1 (overriding the
1603 * framebuffer's alpha values). We can't do that with the blit or
1604 * textured-quad paths.
1606 matching_base_formats
=
1607 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1608 _mesa_get_format_base_format(texImage
->TexFormat
));
1609 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1611 if (ctx
->_ImageTransferState
== 0x0) {
1613 if (matching_base_formats
&&
1614 src_format
== dest_format
&&
1617 /* use surface_copy() / blit */
1619 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1620 stImage
->face
, stImage
->level
,
1622 PIPE_BIND_BLIT_DESTINATION
);
1624 /* for surface_copy(), y=0=top, always */
1625 pipe
->surface_copy(pipe
,
1634 use_fallback
= GL_FALSE
;
1636 else if (format_writemask
&&
1637 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1638 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1639 screen
->is_format_supported(screen
, src_format
,
1641 PIPE_BIND_SAMPLER_VIEW
,
1643 screen
->is_format_supported(screen
, dest_format
,
1645 PIPE_BIND_RENDER_TARGET
,
1647 /* draw textured quad to do the copy */
1650 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1651 stImage
->face
, stImage
->level
,
1653 PIPE_BIND_BLIT_DESTINATION
);
1656 srcY1
= strb
->Base
.Height
- srcY
- height
;
1657 srcY0
= srcY1
+ height
;
1661 srcY1
= srcY0
+ height
;
1663 util_blit_pixels_writemask(st
->blit
,
1665 st_get_renderbuffer_sampler_view(strb
, pipe
),
1667 srcX
+ width
, srcY1
,
1670 destX
+ width
, destY
+ height
,
1671 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1673 use_fallback
= GL_FALSE
;
1677 pipe_surface_reference(&dest_surface
, NULL
);
1681 /* software fallback */
1682 fallback_copy_texsubimage(ctx
, target
, level
,
1683 strb
, stImage
, texBaseFormat
,
1684 destX
, destY
, destZ
,
1685 srcX
, srcY
, width
, height
);
1692 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1693 GLenum internalFormat
,
1694 GLint x
, GLint y
, GLsizei width
, GLint border
)
1696 struct gl_texture_unit
*texUnit
=
1697 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1698 struct gl_texture_object
*texObj
=
1699 _mesa_select_tex_object(ctx
, texUnit
, target
);
1700 struct gl_texture_image
*texImage
=
1701 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1703 /* Setup or redefine the texture object, texture and texture
1704 * image. Don't populate yet.
1706 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1708 GL_RGBA
, CHAN_TYPE
, NULL
,
1709 &ctx
->DefaultPacking
, texObj
, texImage
);
1711 st_copy_texsubimage(ctx
, target
, level
,
1712 0, 0, 0, /* destX,Y,Z */
1713 x
, y
, width
, 1); /* src X, Y, size */
1718 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1719 GLenum internalFormat
,
1720 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1723 struct gl_texture_unit
*texUnit
=
1724 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1725 struct gl_texture_object
*texObj
=
1726 _mesa_select_tex_object(ctx
, texUnit
, target
);
1727 struct gl_texture_image
*texImage
=
1728 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1730 /* Setup or redefine the texture object, texture and texture
1731 * image. Don't populate yet.
1733 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1734 width
, height
, border
,
1735 GL_RGBA
, CHAN_TYPE
, NULL
,
1736 &ctx
->DefaultPacking
, texObj
, texImage
);
1738 st_copy_texsubimage(ctx
, target
, level
,
1739 0, 0, 0, /* destX,Y,Z */
1740 x
, y
, width
, height
); /* src X, Y, size */
1745 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1746 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1748 const GLint yoffset
= 0, zoffset
= 0;
1749 const GLsizei height
= 1;
1750 st_copy_texsubimage(ctx
, target
, level
,
1751 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1752 x
, y
, width
, height
); /* src X, Y, size */
1757 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1758 GLint xoffset
, GLint yoffset
,
1759 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1761 const GLint zoffset
= 0;
1762 st_copy_texsubimage(ctx
, target
, level
,
1763 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1764 x
, y
, width
, height
); /* src X, Y, size */
1769 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1770 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1771 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1773 st_copy_texsubimage(ctx
, target
, level
,
1774 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1775 x
, y
, width
, height
); /* src X, Y, size */
1780 * Copy image data from stImage into the texture object 'stObj' at level
1784 copy_image_data_to_texture(struct st_context
*st
,
1785 struct st_texture_object
*stObj
,
1787 struct st_texture_image
*stImage
)
1791 const struct gl_texture_image
*dstImage
=
1792 stObj
->base
.Image
[stImage
->face
][stImage
->level
];
1794 assert(dstImage
->Width
== stImage
->base
.Width
);
1795 assert(dstImage
->Height
== stImage
->base
.Height
);
1796 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1800 /* Copy potentially with the blitter:
1802 st_texture_image_copy(st
->pipe
,
1803 stObj
->pt
, dstLevel
, /* dest texture, level */
1804 stImage
->pt
, /* src texture */
1807 pipe_resource_reference(&stImage
->pt
, NULL
);
1809 else if (stImage
->base
.Data
) {
1810 /* More straightforward upload.
1812 st_teximage_flush_before_map(st
, stObj
->pt
, stImage
->face
, dstLevel
,
1813 PIPE_TRANSFER_WRITE
);
1815 st_texture_image_data(st
,
1820 stImage
->base
.RowStride
*
1821 util_format_get_blocksize(stObj
->pt
->format
),
1822 stImage
->base
.RowStride
*
1823 stImage
->base
.Height
*
1824 util_format_get_blocksize(stObj
->pt
->format
));
1825 _mesa_align_free(stImage
->base
.Data
);
1826 stImage
->base
.Data
= NULL
;
1829 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1834 * Called during state validation. When this function is finished,
1835 * the texture object should be ready for rendering.
1836 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1839 st_finalize_texture(GLcontext
*ctx
,
1840 struct pipe_context
*pipe
,
1841 struct gl_texture_object
*tObj
,
1842 GLboolean
*needFlush
)
1844 struct st_context
*st
= st_context(ctx
);
1845 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1846 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1848 struct st_texture_image
*firstImage
;
1849 enum pipe_format firstImageFormat
;
1851 *needFlush
= GL_FALSE
;
1853 if (stObj
->base
._Complete
) {
1854 /* The texture is complete and we know exactly how many mipmap levels
1855 * are present/needed. This is conditional because we may be called
1856 * from the st_generate_mipmap() function when the texture object is
1857 * incomplete. In that case, we'll have set stObj->lastLevel before
1860 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1861 stObj
->base
.MinFilter
== GL_NEAREST
)
1862 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1864 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1867 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1870 /* If both firstImage and stObj point to a texture which can contain
1871 * all active images, favour firstImage. Note that because of the
1872 * completeness requirement, we know that the image dimensions
1875 if (firstImage
->pt
&&
1876 firstImage
->pt
!= stObj
->pt
&&
1877 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1878 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1879 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1882 /* Find gallium format for the Mesa texture */
1883 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1885 /* If we already have a gallium texture, check that it matches the texture
1886 * object's format, target, size, num_levels, etc.
1889 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1890 stObj
->pt
->format
!= firstImageFormat
||
1891 stObj
->pt
->last_level
< stObj
->lastLevel
||
1892 stObj
->pt
->width0
!= stObj
->width0
||
1893 stObj
->pt
->height0
!= stObj
->height0
||
1894 stObj
->pt
->depth0
!= stObj
->depth0
)
1896 /* The gallium texture does not match the Mesa texture so delete the
1897 * gallium texture now. We'll make a new one below.
1899 pipe_resource_reference(&stObj
->pt
, NULL
);
1900 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1901 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1905 /* May need to create a new gallium texture:
1908 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1910 stObj
->pt
= st_texture_create(st
,
1911 gl_target_to_pipe(stObj
->base
.Target
),
1920 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1925 /* Pull in any images not in the object's texture:
1927 for (face
= 0; face
< nr_faces
; face
++) {
1929 for (level
= 0; level
<= stObj
->lastLevel
; level
++) {
1930 struct st_texture_image
*stImage
=
1931 st_texture_image(stObj
->base
.Image
[face
][level
]);
1933 /* Need to import images in main memory or held in other textures.
1935 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1936 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1937 *needFlush
= GL_TRUE
;
1947 * Returns pointer to a default/dummy texture.
1948 * This is typically used when the current shader has tex/sample instructions
1949 * but the user has not provided a (any) texture(s).
1951 struct gl_texture_object
*
1952 st_get_default_texture(struct st_context
*st
)
1954 if (!st
->default_texture
) {
1955 static const GLenum target
= GL_TEXTURE_2D
;
1956 GLubyte pixels
[16][16][4];
1957 struct gl_texture_object
*texObj
;
1958 struct gl_texture_image
*texImg
;
1961 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1962 * when attempting to sample incomplete textures.
1964 for (i
= 0; i
< 16; i
++) {
1965 for (j
= 0; j
< 16; j
++) {
1966 pixels
[i
][j
][0] = 0;
1967 pixels
[i
][j
][1] = 0;
1968 pixels
[i
][j
][2] = 0;
1969 pixels
[i
][j
][3] = 255;
1973 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1975 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1977 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1978 16, 16, 1, 0, /* w, h, d, border */
1981 st_TexImage(st
->ctx
, 2, target
,
1982 0, GL_RGBA
, /* level, intformat */
1983 16, 16, 1, 0, /* w, h, d, border */
1984 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1985 &st
->ctx
->DefaultPacking
,
1989 texObj
->MinFilter
= GL_NEAREST
;
1990 texObj
->MagFilter
= GL_NEAREST
;
1991 texObj
->_Complete
= GL_TRUE
;
1993 st
->default_texture
= texObj
;
1995 return st
->default_texture
;
2000 st_init_texture_functions(struct dd_function_table
*functions
)
2002 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
2003 functions
->TexImage1D
= st_TexImage1D
;
2004 functions
->TexImage2D
= st_TexImage2D
;
2005 functions
->TexImage3D
= st_TexImage3D
;
2006 functions
->TexSubImage1D
= st_TexSubImage1D
;
2007 functions
->TexSubImage2D
= st_TexSubImage2D
;
2008 functions
->TexSubImage3D
= st_TexSubImage3D
;
2009 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
2010 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
2011 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
2012 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
2013 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
2014 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
2015 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
2016 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
2017 functions
->GenerateMipmap
= st_generate_mipmap
;
2019 functions
->GetTexImage
= st_GetTexImage
;
2021 /* compressed texture functions */
2022 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
2023 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
2025 functions
->NewTextureObject
= st_NewTextureObject
;
2026 functions
->NewTextureImage
= st_NewTextureImage
;
2027 functions
->DeleteTexture
= st_DeleteTextureObject
;
2028 functions
->FreeTexImageData
= st_FreeTextureImageData
;
2029 functions
->UpdateTexturePalette
= 0;
2031 functions
->TextureMemCpy
= do_memcpy
;
2033 /* XXX Temporary until we can query pipe's texture sizes */
2034 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;