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
;
235 * Allocate a pipe_resource object for the given st_texture_object using
236 * the given st_texture_image to guess the mipmap size/levels.
239 * Otherwise, store it in memory if (Border != 0) or (any dimension ==
242 * Otherwise, if max_level >= level >= min_level, create texture with
243 * space for images from min_level down to max_level.
245 * Otherwise, create texture with space for images from (level 0)..(1x1).
246 * Consider pruning this texture at a validation if the saving is worth it.
249 guess_and_alloc_texture(struct st_context
*st
,
250 struct st_texture_object
*stObj
,
251 const struct st_texture_image
*stImage
)
255 GLuint width
= stImage
->base
.Width2
; /* size w/out border */
256 GLuint height
= stImage
->base
.Height2
;
257 GLuint depth
= stImage
->base
.Depth2
;
259 enum pipe_format fmt
;
261 DBG("%s\n", __FUNCTION__
);
266 (GLint
) stImage
->level
> stObj
->base
.BaseLevel
&&
267 (stImage
->base
.Width
== 1 ||
268 (stObj
->base
.Target
!= GL_TEXTURE_1D
&&
269 stImage
->base
.Height
== 1) ||
270 (stObj
->base
.Target
== GL_TEXTURE_3D
&&
271 stImage
->base
.Depth
== 1)))
274 /* If this image disrespects BaseLevel, allocate from level zero.
275 * Usually BaseLevel == 0, so it's unlikely to happen.
277 if ((GLint
) stImage
->level
< stObj
->base
.BaseLevel
)
280 firstLevel
= stObj
->base
.BaseLevel
;
283 /* Figure out image dimensions at start level.
285 for (i
= stImage
->level
; i
> firstLevel
; i
--) {
294 if (width
== 0 || height
== 0 || depth
== 0) {
295 /* no texture needed */
299 /* Guess a reasonable value for lastLevel. This is probably going
300 * to be wrong fairly often and might mean that we have to look at
301 * resizable buffers, or require that buffers implement lazy
302 * pagetable arrangements.
304 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
305 stObj
->base
.MinFilter
== GL_LINEAR
||
306 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
307 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
308 !stObj
->base
.GenerateMipmap
&&
309 stImage
->level
== firstLevel
) {
310 /* only alloc space for a single mipmap level */
311 lastLevel
= firstLevel
;
314 /* alloc space for a full mipmap */
315 GLuint l2width
= util_logbase2(width
);
316 GLuint l2height
= util_logbase2(height
);
317 GLuint l2depth
= util_logbase2(depth
);
318 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
321 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
323 bindings
= default_bindings(st
, fmt
);
325 stObj
->pt
= st_texture_create(st
,
326 gl_target_to_pipe(stObj
->base
.Target
),
334 DBG("%s - success\n", __FUNCTION__
);
339 * Adjust pixel unpack params and image dimensions to strip off the
341 * Gallium doesn't support texture borders. They've seldem been used
342 * and seldom been implemented correctly anyway.
343 * \param unpackNew returns the new pixel unpack parameters
346 strip_texture_border(GLint border
,
347 GLint
*width
, GLint
*height
, GLint
*depth
,
348 const struct gl_pixelstore_attrib
*unpack
,
349 struct gl_pixelstore_attrib
*unpackNew
)
351 assert(border
> 0); /* sanity check */
353 *unpackNew
= *unpack
;
355 if (unpackNew
->RowLength
== 0)
356 unpackNew
->RowLength
= *width
;
358 if (depth
&& unpackNew
->ImageHeight
== 0)
359 unpackNew
->ImageHeight
= *height
;
361 unpackNew
->SkipPixels
+= border
;
363 unpackNew
->SkipRows
+= border
;
365 unpackNew
->SkipImages
+= border
;
368 *width
= *width
- 2 * border
;
369 if (height
&& *height
>= 3)
370 *height
= *height
- 2 * border
;
371 if (depth
&& *depth
>= 3)
372 *depth
= *depth
- 2 * border
;
377 * Try to do texture compression via rendering. If the Gallium driver
378 * can render into a compressed surface this will allow us to do texture
380 * \return GL_TRUE for success, GL_FALSE for failure
383 compress_with_blit(GLcontext
* ctx
,
384 GLenum target
, GLint level
,
385 GLint xoffset
, GLint yoffset
, GLint zoffset
,
386 GLint width
, GLint height
, GLint depth
,
387 GLenum format
, GLenum type
, const void *pixels
,
388 const struct gl_pixelstore_attrib
*unpack
,
389 struct gl_texture_image
*texImage
)
391 const GLuint dstImageOffsets
[1] = {0};
392 struct st_texture_image
*stImage
= st_texture_image(texImage
);
393 struct st_context
*st
= st_context(ctx
);
394 struct pipe_context
*pipe
= st
->pipe
;
395 struct pipe_screen
*screen
= pipe
->screen
;
396 gl_format mesa_format
;
397 struct pipe_resource templ
;
398 struct pipe_resource
*src_tex
;
399 struct pipe_sampler_view view_templ
;
400 struct pipe_sampler_view
*src_view
;
401 struct pipe_surface
*dst_surface
;
402 struct pipe_transfer
*tex_xfer
;
406 /* XXX: Can this happen? Should we assert? */
410 /* get destination surface (in the compressed texture) */
411 dst_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
412 stImage
->face
, stImage
->level
, 0,
413 PIPE_BIND_BLIT_DESTINATION
);
415 /* can't render into this format (or other problem) */
419 /* Choose format for the temporary RGBA texture image.
421 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
426 /* Create the temporary source texture
428 memset(&templ
, 0, sizeof(templ
));
429 templ
.target
= PIPE_TEXTURE_2D
;
430 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
);
431 templ
.width0
= width
;
432 templ
.height0
= height
;
434 templ
.last_level
= 0;
435 templ
.usage
= PIPE_USAGE_DEFAULT
;
436 templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
437 src_tex
= screen
->resource_create(screen
, &templ
);
442 /* Put user's tex data into the temporary texture
444 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
), src_tex
,
445 0, 0, 0, /* face, level are zero */
447 0, 0, width
, height
); /* x, y, w, h */
448 map
= pipe_transfer_map(pipe
, tex_xfer
);
450 _mesa_texstore(ctx
, 2, GL_RGBA
, mesa_format
,
452 0, 0, 0, /* dest x/y/z offset */
453 tex_xfer
->stride
, /* dest row stride (bytes) */
454 dstImageOffsets
, /* image offsets (for 3D only) */
455 width
, height
, 1, /* size */
456 format
, type
, /* source format/type */
457 pixels
, /* source data */
458 unpack
); /* source data packing */
460 pipe_transfer_unmap(pipe
, tex_xfer
);
461 pipe
->transfer_destroy(pipe
, tex_xfer
);
463 /* Create temporary sampler view */
464 u_sampler_view_default_template(&view_templ
,
467 src_view
= pipe
->create_sampler_view(pipe
, src_tex
, &view_templ
);
470 /* copy / compress image */
471 util_blit_pixels_tex(st
->blit
,
472 src_view
, /* sampler view (src) */
473 0, 0, /* src x0, y0 */
474 width
, height
, /* src x1, y1 */
475 dst_surface
, /* pipe_surface (dst) */
476 xoffset
, yoffset
, /* dst x0, y0 */
477 xoffset
+ width
, /* dst x1 */
478 yoffset
+ height
, /* dst y1 */
480 PIPE_TEX_MIPFILTER_NEAREST
);
482 pipe_surface_reference(&dst_surface
, NULL
);
483 pipe_resource_reference(&src_tex
, NULL
);
484 pipe_sampler_view_reference(&src_view
, NULL
);
491 * Do glTexImage1/2/3D().
494 st_TexImage(GLcontext
* ctx
,
496 GLenum target
, GLint level
,
497 GLint internalFormat
,
498 GLint width
, GLint height
, GLint depth
,
500 GLenum format
, GLenum type
, const void *pixels
,
501 const struct gl_pixelstore_attrib
*unpack
,
502 struct gl_texture_object
*texObj
,
503 struct gl_texture_image
*texImage
,
504 GLsizei imageSize
, GLboolean compressed_src
)
506 struct st_context
*st
= st_context(ctx
);
507 struct pipe_screen
*screen
= st
->pipe
->screen
;
508 struct st_texture_object
*stObj
= st_texture_object(texObj
);
509 struct st_texture_image
*stImage
= st_texture_image(texImage
);
510 GLint postConvWidth
, postConvHeight
;
511 GLint texelBytes
, sizeInBytes
;
512 GLuint dstRowStride
= 0;
513 struct gl_pixelstore_attrib unpackNB
;
514 enum pipe_transfer_usage transfer_usage
= 0;
516 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
517 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
519 /* switch to "normal" */
520 if (stObj
->surface_based
) {
521 _mesa_clear_texture_object(ctx
, texObj
);
522 stObj
->surface_based
= GL_FALSE
;
525 /* gallium does not support texture borders, strip it off */
527 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
529 texImage
->Width
= width
;
530 texImage
->Height
= height
;
531 texImage
->Depth
= depth
;
532 texImage
->Border
= 0;
536 postConvWidth
= width
;
537 postConvHeight
= height
;
539 stImage
->face
= _mesa_tex_target_to_face(target
);
540 stImage
->level
= level
;
543 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
544 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
549 _mesa_set_fetch_functions(texImage
, dims
);
551 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
552 /* must be a compressed format */
556 texelBytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
558 /* Minimum pitch of 32 bytes */
559 if (postConvWidth
* texelBytes
< 32) {
560 postConvWidth
= 32 / texelBytes
;
561 texImage
->RowStride
= postConvWidth
;
564 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
565 /*assert(texImage->RowStride == postConvWidth);*/
568 /* Release the reference to a potentially orphaned buffer.
569 * Release any old malloced memory.
572 pipe_resource_reference(&stImage
->pt
, NULL
);
573 assert(!texImage
->Data
);
575 else if (texImage
->Data
) {
576 _mesa_align_free(texImage
->Data
);
580 * See if the new image is somehow incompatible with the existing
581 * mipmap. If so, free the old mipmap.
584 if (stObj
->teximage_realloc
||
585 level
> (GLint
) stObj
->pt
->last_level
||
586 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
587 stImage
->face
, stImage
->level
)) {
589 pipe_resource_reference(&stObj
->pt
, NULL
);
591 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
592 stObj
->teximage_realloc
= FALSE
;
596 if (width
== 0 || height
== 0 || depth
== 0) {
597 /* stop after freeing old image */
602 guess_and_alloc_texture(st
, stObj
, stImage
);
604 /* Probably out of memory.
605 * Try flushing any pending rendering, then retry.
608 guess_and_alloc_texture(st
, stObj
, stImage
);
610 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
616 assert(!stImage
->pt
);
619 st_texture_match_image(stObj
->pt
, &stImage
->base
,
620 stImage
->face
, stImage
->level
)) {
622 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
627 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
629 /* st_CopyTexImage calls this function with pixels == NULL, with
630 * the expectation that the texture will be set up but nothing
631 * more will be done. This is where those calls return:
633 if (compressed_src
) {
634 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
636 "glCompressedTexImage");
639 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
641 pixels
, unpack
, "glTexImage");
644 /* Note: we can't check for pixels==NULL until after we've allocated
645 * memory for the texture.
648 /* See if we can do texture compression with a blit/render.
650 if (!compressed_src
&&
652 _mesa_is_format_compressed(texImage
->TexFormat
) &&
653 screen
->is_format_supported(screen
,
656 PIPE_BIND_RENDER_TARGET
, 0)) {
660 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
661 format
, type
, pixels
, unpack
, texImage
)) {
667 if (format
== GL_DEPTH_COMPONENT
&&
668 util_format_is_depth_and_stencil(stImage
->pt
->format
))
669 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
671 transfer_usage
= PIPE_TRANSFER_WRITE
;
673 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
674 transfer_usage
, 0, 0,
676 stImage
->base
.Height
);
677 if(stImage
->transfer
)
678 dstRowStride
= stImage
->transfer
->stride
;
681 /* Allocate regular memory and store the image there temporarily. */
682 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
683 sizeInBytes
= _mesa_format_image_size(texImage
->TexFormat
,
687 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
691 dstRowStride
= postConvWidth
* texelBytes
;
692 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
695 texImage
->Data
= _mesa_align_malloc(sizeInBytes
, 16);
698 if (!texImage
->Data
) {
699 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
706 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
707 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
709 /* Copy data. Would like to know when it's ok for us to eg. use
710 * the blitter to copy. Or, use the hardware to do the format
711 * conversion and copy:
713 if (compressed_src
) {
714 const GLuint srcImageStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
715 if(dstRowStride
== srcImageStride
)
716 memcpy(texImage
->Data
, pixels
, imageSize
);
719 char *dst
= texImage
->Data
;
720 const char *src
= pixels
;
721 GLuint i
, bw
, bh
, lines
;
722 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
723 lines
= (height
+ bh
- 1) / bh
;
725 for(i
= 0; i
< lines
; ++i
)
727 memcpy(dst
, src
, srcImageStride
);
729 src
+= srcImageStride
;
734 const GLuint srcImageStride
=
735 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
737 const GLubyte
*src
= (const GLubyte
*) pixels
;
739 for (i
= 0; i
< depth
; i
++) {
740 if (!_mesa_texstore(ctx
, dims
,
741 texImage
->_BaseFormat
,
744 0, 0, 0, /* dstX/Y/Zoffset */
746 texImage
->ImageOffsets
,
748 format
, type
, src
, unpack
)) {
749 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
752 if (stImage
->pt
&& i
+ 1 < depth
) {
753 /* unmap this slice */
754 st_texture_image_unmap(st
, stImage
);
755 /* map next slice of 3D texture */
756 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
757 transfer_usage
, 0, 0,
759 stImage
->base
.Height
);
760 src
+= srcImageStride
;
766 _mesa_unmap_teximage_pbo(ctx
, unpack
);
768 if (stImage
->pt
&& texImage
->Data
) {
769 st_texture_image_unmap(st
, stImage
);
770 texImage
->Data
= NULL
;
776 st_TexImage3D(GLcontext
* ctx
,
777 GLenum target
, GLint level
,
778 GLint internalFormat
,
779 GLint width
, GLint height
, GLint depth
,
781 GLenum format
, GLenum type
, const void *pixels
,
782 const struct gl_pixelstore_attrib
*unpack
,
783 struct gl_texture_object
*texObj
,
784 struct gl_texture_image
*texImage
)
786 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
787 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
793 st_TexImage2D(GLcontext
* ctx
,
794 GLenum target
, GLint level
,
795 GLint internalFormat
,
796 GLint width
, GLint height
, GLint border
,
797 GLenum format
, GLenum type
, const void *pixels
,
798 const struct gl_pixelstore_attrib
*unpack
,
799 struct gl_texture_object
*texObj
,
800 struct gl_texture_image
*texImage
)
802 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
803 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
808 st_TexImage1D(GLcontext
* ctx
,
809 GLenum target
, GLint level
,
810 GLint internalFormat
,
811 GLint width
, GLint border
,
812 GLenum format
, GLenum type
, const void *pixels
,
813 const struct gl_pixelstore_attrib
*unpack
,
814 struct gl_texture_object
*texObj
,
815 struct gl_texture_image
*texImage
)
817 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
818 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
823 st_CompressedTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
824 GLint internalFormat
,
825 GLint width
, GLint height
, GLint border
,
826 GLsizei imageSize
, const GLvoid
*data
,
827 struct gl_texture_object
*texObj
,
828 struct gl_texture_image
*texImage
)
830 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
831 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
837 * glGetTexImage() helper: decompress a compressed texture by rendering
838 * a textured quad. Store the results in the user's buffer.
841 decompress_with_blit(GLcontext
* ctx
, GLenum target
, GLint level
,
842 GLenum format
, GLenum type
, GLvoid
*pixels
,
843 struct gl_texture_object
*texObj
,
844 struct gl_texture_image
*texImage
)
846 struct st_context
*st
= st_context(ctx
);
847 struct pipe_context
*pipe
= st
->pipe
;
848 struct pipe_screen
*screen
= pipe
->screen
;
849 struct st_texture_image
*stImage
= st_texture_image(texImage
);
850 struct st_texture_object
*stObj
= st_texture_object(texObj
);
851 struct pipe_sampler_view
*src_view
=
852 st_get_texture_sampler_view(stObj
, pipe
);
853 const GLuint width
= texImage
->Width
;
854 const GLuint height
= texImage
->Height
;
855 struct pipe_surface
*dst_surface
;
856 struct pipe_resource
*dst_texture
;
857 struct pipe_transfer
*tex_xfer
;
858 unsigned bind
= (PIPE_BIND_BLIT_DESTINATION
|
859 PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
860 PIPE_BIND_TRANSFER_READ
);
862 /* create temp / dest surface */
863 if (!util_create_rgba_surface(screen
, width
, height
, bind
,
864 &dst_texture
, &dst_surface
)) {
865 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
866 "in decompress_with_blit()");
870 /* blit/render/decompress */
871 util_blit_pixels_tex(st
->blit
,
872 src_view
, /* pipe_resource (src) */
873 0, 0, /* src x0, y0 */
874 width
, height
, /* src x1, y1 */
875 dst_surface
, /* pipe_surface (dst) */
876 0, 0, /* dst x0, y0 */
877 width
, height
, /* dst x1, y1 */
879 PIPE_TEX_MIPFILTER_NEAREST
);
881 /* map the dst_surface so we can read from it */
882 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
),
883 dst_texture
, 0, 0, 0,
885 0, 0, width
, height
);
887 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
889 /* copy/pack data into user buffer */
890 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
892 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
893 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
895 for (row
= 0; row
< height
; row
++) {
896 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
897 height
, format
, type
, row
, 0);
898 memcpy(dest
, map
, bytesPerRow
);
899 map
+= tex_xfer
->stride
;
901 pipe_transfer_unmap(pipe
, tex_xfer
);
904 /* format translation via floats */
906 for (row
= 0; row
< height
; row
++) {
907 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
908 GLfloat rgba
[4 * MAX_WIDTH
];
909 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
910 height
, format
, type
, row
, 0);
912 if (ST_DEBUG
& DEBUG_FALLBACK
)
913 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
915 /* get float[4] rgba row from surface */
916 pipe_get_tile_rgba(pipe
, tex_xfer
, 0, row
, width
, 1, rgba
);
918 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
919 type
, dest
, &ctx
->Pack
, transferOps
);
923 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
925 pipe
->transfer_destroy(pipe
, tex_xfer
);
927 /* destroy the temp / dest surface */
928 util_destroy_rgba_surface(dst_texture
, dst_surface
);
934 * Need to map texture image into memory before copying image data,
938 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
939 GLenum format
, GLenum type
, GLvoid
* pixels
,
940 struct gl_texture_object
*texObj
,
941 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
943 struct st_context
*st
= st_context(ctx
);
944 struct st_texture_image
*stImage
= st_texture_image(texImage
);
945 const GLuint dstImageStride
=
946 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
952 util_format_is_s3tc(stImage
->pt
->format
) &&
954 /* Need to decompress the texture.
955 * We'll do this by rendering a textured quad.
956 * Note that we only expect RGBA formats (no Z/depth formats).
958 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
965 /* Image is stored in hardware format in a buffer managed by the
966 * kernel. Need to explicitly map and unmap it.
968 unsigned face
= _mesa_tex_target_to_face(target
);
970 st_teximage_flush_before_map(st
, stImage
->pt
, face
, level
,
973 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
974 PIPE_TRANSFER_READ
, 0, 0,
976 stImage
->base
.Height
);
977 texImage
->RowStride
= stImage
->transfer
->stride
/ util_format_get_blocksize(stImage
->pt
->format
);
980 /* Otherwise, the image should actually be stored in
981 * texImage->Data. This is pretty confusing for
982 * everybody, I'd much prefer to separate the two functions of
983 * texImage->Data - storage for texture images in main memory
984 * and access (ie mappings) of images. In other words, we'd
985 * create a new texImage->Map field and leave Data simply for
988 assert(texImage
->Data
);
991 depth
= texImage
->Depth
;
994 dest
= (GLubyte
*) pixels
;
996 for (i
= 0; i
< depth
; i
++) {
997 if (compressed_dst
) {
998 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
1002 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
1006 if (stImage
->pt
&& i
+ 1 < depth
) {
1007 /* unmap this slice */
1008 st_texture_image_unmap(st
, stImage
);
1009 /* map next slice of 3D texture */
1010 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
1011 PIPE_TRANSFER_READ
, 0, 0,
1012 stImage
->base
.Width
,
1013 stImage
->base
.Height
);
1014 dest
+= dstImageStride
;
1018 texImage
->Depth
= depth
;
1022 st_texture_image_unmap(st
, stImage
);
1023 texImage
->Data
= NULL
;
1029 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
1030 GLenum format
, GLenum type
, GLvoid
* pixels
,
1031 struct gl_texture_object
*texObj
,
1032 struct gl_texture_image
*texImage
)
1034 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1040 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
1042 struct gl_texture_object
*texObj
,
1043 struct gl_texture_image
*texImage
)
1045 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1052 st_TexSubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
1053 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1054 GLint width
, GLint height
, GLint depth
,
1055 GLenum format
, GLenum type
, const void *pixels
,
1056 const struct gl_pixelstore_attrib
*packing
,
1057 struct gl_texture_object
*texObj
,
1058 struct gl_texture_image
*texImage
)
1060 struct st_context
*st
= st_context(ctx
);
1061 struct pipe_screen
*screen
= st
->pipe
->screen
;
1062 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1063 GLuint dstRowStride
;
1064 const GLuint srcImageStride
=
1065 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1068 /* init to silence warning only: */
1069 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1071 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1072 _mesa_lookup_enum_by_nr(target
),
1073 level
, xoffset
, yoffset
, width
, height
);
1076 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1077 type
, pixels
, packing
, "glTexSubImage2D");
1081 /* See if we can do texture compression with a blit/render.
1083 if (!ctx
->Mesa_DXTn
&&
1084 _mesa_is_format_compressed(texImage
->TexFormat
) &&
1085 screen
->is_format_supported(screen
,
1086 stImage
->pt
->format
,
1087 stImage
->pt
->target
,
1088 PIPE_BIND_RENDER_TARGET
, 0)) {
1089 if (compress_with_blit(ctx
, target
, level
,
1090 xoffset
, yoffset
, zoffset
,
1091 width
, height
, depth
,
1092 format
, type
, pixels
, packing
, texImage
)) {
1097 /* Map buffer if necessary. Need to lock to prevent other contexts
1098 * from uploading the buffer under us.
1101 unsigned face
= _mesa_tex_target_to_face(target
);
1103 if (format
== GL_DEPTH_COMPONENT
&&
1104 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1105 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1107 transfer_usage
= PIPE_TRANSFER_WRITE
;
1109 st_teximage_flush_before_map(st
, stImage
->pt
, face
, level
,
1111 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1117 if (!texImage
->Data
) {
1118 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1122 src
= (const GLubyte
*) pixels
;
1123 dstRowStride
= stImage
->transfer
->stride
;
1125 for (i
= 0; i
< depth
; i
++) {
1126 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1127 texImage
->TexFormat
,
1131 texImage
->ImageOffsets
,
1133 format
, type
, src
, packing
)) {
1134 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1137 if (stImage
->pt
&& i
+ 1 < depth
) {
1138 /* unmap this slice */
1139 st_texture_image_unmap(st
, stImage
);
1140 /* map next slice of 3D texture */
1141 texImage
->Data
= st_texture_image_map(st
, stImage
,
1146 src
+= srcImageStride
;
1151 _mesa_unmap_teximage_pbo(ctx
, packing
);
1153 if (stImage
->pt
&& texImage
->Data
) {
1154 st_texture_image_unmap(st
, stImage
);
1155 texImage
->Data
= NULL
;
1162 st_TexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1163 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1164 GLsizei width
, GLsizei height
, GLsizei depth
,
1165 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1166 const struct gl_pixelstore_attrib
*packing
,
1167 struct gl_texture_object
*texObj
,
1168 struct gl_texture_image
*texImage
)
1170 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1171 width
, height
, depth
, format
, type
,
1172 pixels
, packing
, texObj
, texImage
);
1177 st_TexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1178 GLint xoffset
, GLint yoffset
,
1179 GLsizei width
, GLsizei height
,
1180 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1181 const struct gl_pixelstore_attrib
*packing
,
1182 struct gl_texture_object
*texObj
,
1183 struct gl_texture_image
*texImage
)
1185 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1186 width
, height
, 1, format
, type
,
1187 pixels
, packing
, texObj
, texImage
);
1192 st_TexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1193 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1194 const GLvoid
* pixels
,
1195 const struct gl_pixelstore_attrib
*packing
,
1196 struct gl_texture_object
*texObj
,
1197 struct gl_texture_image
*texImage
)
1199 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1200 format
, type
, pixels
, packing
, texObj
, texImage
);
1205 st_CompressedTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1206 GLint xoffset
, GLsizei width
,
1208 GLsizei imageSize
, const GLvoid
*data
,
1209 struct gl_texture_object
*texObj
,
1210 struct gl_texture_image
*texImage
)
1217 st_CompressedTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1218 GLint xoffset
, GLint yoffset
,
1219 GLsizei width
, GLint height
,
1221 GLsizei imageSize
, const GLvoid
*data
,
1222 struct gl_texture_object
*texObj
,
1223 struct gl_texture_image
*texImage
)
1225 struct st_context
*st
= st_context(ctx
);
1226 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1230 enum pipe_format pformat
;
1233 unsigned face
= _mesa_tex_target_to_face(target
);
1234 pformat
= stImage
->pt
->format
;
1236 st_teximage_flush_before_map(st
, stImage
->pt
, face
, level
,
1237 PIPE_TRANSFER_WRITE
);
1238 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1239 PIPE_TRANSFER_WRITE
,
1243 srcBlockStride
= util_format_get_stride(pformat
, width
);
1244 dstBlockStride
= stImage
->transfer
->stride
;
1246 assert(stImage
->pt
);
1247 /* TODO find good values for block and strides */
1248 /* TODO also adjust texImage->data for yoffset/xoffset */
1252 if (!texImage
->Data
) {
1253 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1257 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1258 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1259 assert(width
% util_format_get_blockwidth(pformat
) == 0);
1260 assert(height
% util_format_get_blockheight(pformat
) == 0);
1262 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1263 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1264 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1265 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1266 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1270 st_texture_image_unmap(st
, stImage
);
1271 texImage
->Data
= NULL
;
1277 st_CompressedTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1278 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1279 GLsizei width
, GLint height
, GLint depth
,
1281 GLsizei imageSize
, const GLvoid
*data
,
1282 struct gl_texture_object
*texObj
,
1283 struct gl_texture_image
*texImage
)
1291 * Do a CopyTexSubImage operation using a read transfer from the source,
1292 * a write transfer to the destination and get_tile()/put_tile() to access
1293 * the pixels/texels.
1295 * Note: srcY=0=TOP of renderbuffer
1298 fallback_copy_texsubimage(GLcontext
*ctx
, GLenum target
, GLint level
,
1299 struct st_renderbuffer
*strb
,
1300 struct st_texture_image
*stImage
,
1302 GLint destX
, GLint destY
, GLint destZ
,
1303 GLint srcX
, GLint srcY
,
1304 GLsizei width
, GLsizei height
)
1306 struct st_context
*st
= st_context(ctx
);
1307 struct pipe_context
*pipe
= st
->pipe
;
1308 struct pipe_transfer
*src_trans
;
1310 enum pipe_transfer_usage transfer_usage
;
1312 if (ST_DEBUG
& DEBUG_FALLBACK
)
1313 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1315 assert(width
<= MAX_WIDTH
);
1317 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1318 srcY
= strb
->Base
.Height
- srcY
- height
;
1321 src_trans
= st_cond_flush_get_tex_transfer( st_context(ctx
),
1328 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1329 baseFormat
== GL_DEPTH_STENCIL
) &&
1330 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1331 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1333 transfer_usage
= PIPE_TRANSFER_WRITE
;
1335 st_teximage_flush_before_map(st
, stImage
->pt
, 0, 0,
1338 texDest
= st_texture_image_map(st
, stImage
, 0, transfer_usage
,
1339 destX
, destY
, width
, height
);
1341 if (baseFormat
== GL_DEPTH_COMPONENT
||
1342 baseFormat
== GL_DEPTH_STENCIL
) {
1343 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1344 ctx
->Pixel
.DepthBias
!= 0.0F
);
1347 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1348 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1357 /* To avoid a large temp memory allocation, do copy row by row */
1358 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1359 uint data
[MAX_WIDTH
];
1360 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1362 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1364 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1370 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1372 if (tempSrc
&& texDest
) {
1373 const GLint dims
= 2;
1374 const GLint dstRowStride
= stImage
->transfer
->stride
;
1375 struct gl_texture_image
*texImage
= &stImage
->base
;
1376 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1378 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1379 unpack
.Invert
= GL_TRUE
;
1382 /* get float/RGBA image from framebuffer */
1383 /* XXX this usually involves a lot of int/float conversion.
1384 * try to avoid that someday.
1386 pipe_get_tile_rgba(pipe
, src_trans
, 0, 0, width
, height
, tempSrc
);
1388 /* Store into texture memory.
1389 * Note that this does some special things such as pixel transfer
1390 * ops and format conversion. In particular, if the dest tex format
1391 * is actually RGBA but the user created the texture as GL_RGB we
1392 * need to fill-in/override the alpha channel with 1.0.
1394 _mesa_texstore(ctx
, dims
,
1395 texImage
->_BaseFormat
,
1396 texImage
->TexFormat
,
1400 texImage
->ImageOffsets
,
1402 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1406 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1413 st_texture_image_unmap(st
, stImage
);
1414 pipe
->transfer_destroy(pipe
, src_trans
);
1420 * If the format of the src renderbuffer and the format of the dest
1421 * texture are compatible (in terms of blitting), return a TGSI writemask
1422 * to be used during the blit.
1423 * If the src/dest are incompatible, return 0.
1426 compatible_src_dst_formats(GLcontext
*ctx
,
1427 const struct gl_renderbuffer
*src
,
1428 const struct gl_texture_image
*dst
)
1430 /* Get logical base formats for the src and dest.
1431 * That is, use the user-requested formats and not the actual, device-
1433 * For example, the user may have requested an A8 texture but the
1434 * driver may actually be using an RGBA texture format. When we
1435 * copy/blit to that texture, we only want to copy the Alpha channel
1436 * and not the RGB channels.
1438 * Similarly, when the src FBO was created an RGB format may have been
1439 * requested but the driver actually chose an RGBA format. In that case,
1440 * we don't want to copy the undefined Alpha channel to the dest texture
1441 * (it should be 1.0).
1443 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1444 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1447 * XXX when we have red-only and red/green renderbuffers we'll need
1448 * to add more cases here (or implement a general-purpose routine that
1449 * queries the existance of the R,G,B,A channels in the src and dest).
1451 if (srcFormat
== dstFormat
) {
1452 /* This is the same as matching_base_formats, which should
1453 * always pass, as it did previously.
1455 return TGSI_WRITEMASK_XYZW
;
1457 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1458 /* Make sure that A in the dest is 1. The actual src format
1459 * may be RGBA and have undefined A values.
1461 return TGSI_WRITEMASK_XYZ
;
1463 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1464 /* Make sure that A in the dest is 1. The actual dst format
1465 * may be RGBA and will need A=1 to provide proper alpha values
1466 * when sampled later.
1468 return TGSI_WRITEMASK_XYZ
;
1471 if (ST_DEBUG
& DEBUG_FALLBACK
)
1472 debug_printf("%s failed for src %s, dst %s\n",
1474 _mesa_lookup_enum_by_nr(srcFormat
),
1475 _mesa_lookup_enum_by_nr(dstFormat
));
1486 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1487 * Note that the region to copy has already been clipped so we know we
1488 * won't read from outside the source renderbuffer's bounds.
1490 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1493 st_copy_texsubimage(GLcontext
*ctx
,
1494 GLenum target
, GLint level
,
1495 GLint destX
, GLint destY
, GLint destZ
,
1496 GLint srcX
, GLint srcY
,
1497 GLsizei width
, GLsizei height
)
1499 struct gl_texture_unit
*texUnit
=
1500 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1501 struct gl_texture_object
*texObj
=
1502 _mesa_select_tex_object(ctx
, texUnit
, target
);
1503 struct gl_texture_image
*texImage
=
1504 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1505 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1506 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1507 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1508 struct st_renderbuffer
*strb
;
1509 struct st_context
*st
= st_context(ctx
);
1510 struct pipe_context
*pipe
= st
->pipe
;
1511 struct pipe_screen
*screen
= pipe
->screen
;
1512 enum pipe_format dest_format
, src_format
;
1513 GLboolean use_fallback
= GL_TRUE
;
1514 GLboolean matching_base_formats
;
1515 GLuint format_writemask
;
1516 struct pipe_surface
*dest_surface
= NULL
;
1517 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1519 /* any rendering in progress must flushed before we grab the fb image */
1520 st_flush(st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
1522 /* make sure finalize_textures has been called?
1524 if (0) st_validate_state(st
);
1526 /* determine if copying depth or color data */
1527 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1528 texBaseFormat
== GL_DEPTH_STENCIL
) {
1529 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1532 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1533 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1536 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1537 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1565 if (width
< 0 || height
< 0)
1570 assert(strb
->surface
);
1571 assert(stImage
->pt
);
1573 src_format
= strb
->surface
->format
;
1574 dest_format
= stImage
->pt
->format
;
1577 * Determine if the src framebuffer and dest texture have the same
1578 * base format. We need this to detect a case such as the framebuffer
1579 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1580 * texture format stores RGBA we need to set A=1 (overriding the
1581 * framebuffer's alpha values). We can't do that with the blit or
1582 * textured-quad paths.
1584 matching_base_formats
=
1585 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1586 _mesa_get_format_base_format(texImage
->TexFormat
));
1587 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1589 if (ctx
->_ImageTransferState
== 0x0) {
1591 if (matching_base_formats
&&
1592 src_format
== dest_format
&&
1595 /* use surface_copy() / blit */
1597 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1598 stImage
->face
, stImage
->level
,
1600 PIPE_BIND_BLIT_DESTINATION
);
1602 /* for surface_copy(), y=0=top, always */
1603 pipe
->surface_copy(pipe
,
1612 use_fallback
= GL_FALSE
;
1614 else if (format_writemask
&&
1615 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1616 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1617 screen
->is_format_supported(screen
, src_format
,
1619 PIPE_BIND_SAMPLER_VIEW
,
1621 screen
->is_format_supported(screen
, dest_format
,
1623 PIPE_BIND_RENDER_TARGET
,
1625 /* draw textured quad to do the copy */
1628 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1629 stImage
->face
, stImage
->level
,
1631 PIPE_BIND_BLIT_DESTINATION
);
1634 srcY1
= strb
->Base
.Height
- srcY
- height
;
1635 srcY0
= srcY1
+ height
;
1639 srcY1
= srcY0
+ height
;
1641 util_blit_pixels_writemask(st
->blit
,
1643 st_get_renderbuffer_sampler_view(strb
, pipe
),
1645 srcX
+ width
, srcY1
,
1648 destX
+ width
, destY
+ height
,
1649 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1651 use_fallback
= GL_FALSE
;
1655 pipe_surface_reference(&dest_surface
, NULL
);
1659 /* software fallback */
1660 fallback_copy_texsubimage(ctx
, target
, level
,
1661 strb
, stImage
, texBaseFormat
,
1662 destX
, destY
, destZ
,
1663 srcX
, srcY
, width
, height
);
1670 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1671 GLenum internalFormat
,
1672 GLint x
, GLint y
, GLsizei width
, GLint border
)
1674 struct gl_texture_unit
*texUnit
=
1675 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1676 struct gl_texture_object
*texObj
=
1677 _mesa_select_tex_object(ctx
, texUnit
, target
);
1678 struct gl_texture_image
*texImage
=
1679 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1681 /* Setup or redefine the texture object, texture and texture
1682 * image. Don't populate yet.
1684 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1686 GL_RGBA
, CHAN_TYPE
, NULL
,
1687 &ctx
->DefaultPacking
, texObj
, texImage
);
1689 st_copy_texsubimage(ctx
, target
, level
,
1690 0, 0, 0, /* destX,Y,Z */
1691 x
, y
, width
, 1); /* src X, Y, size */
1696 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1697 GLenum internalFormat
,
1698 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1701 struct gl_texture_unit
*texUnit
=
1702 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1703 struct gl_texture_object
*texObj
=
1704 _mesa_select_tex_object(ctx
, texUnit
, target
);
1705 struct gl_texture_image
*texImage
=
1706 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1708 /* Setup or redefine the texture object, texture and texture
1709 * image. Don't populate yet.
1711 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1712 width
, height
, border
,
1713 GL_RGBA
, CHAN_TYPE
, NULL
,
1714 &ctx
->DefaultPacking
, texObj
, texImage
);
1716 st_copy_texsubimage(ctx
, target
, level
,
1717 0, 0, 0, /* destX,Y,Z */
1718 x
, y
, width
, height
); /* src X, Y, size */
1723 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1724 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1726 const GLint yoffset
= 0, zoffset
= 0;
1727 const GLsizei height
= 1;
1728 st_copy_texsubimage(ctx
, target
, level
,
1729 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1730 x
, y
, width
, height
); /* src X, Y, size */
1735 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1736 GLint xoffset
, GLint yoffset
,
1737 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1739 const GLint zoffset
= 0;
1740 st_copy_texsubimage(ctx
, target
, level
,
1741 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1742 x
, y
, width
, height
); /* src X, Y, size */
1747 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1748 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1749 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1751 st_copy_texsubimage(ctx
, target
, level
,
1752 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1753 x
, y
, width
, height
); /* src X, Y, size */
1758 copy_image_data_to_texture(struct st_context
*st
,
1759 struct st_texture_object
*stObj
,
1761 struct st_texture_image
*stImage
)
1764 /* Copy potentially with the blitter:
1766 st_texture_image_copy(st
->pipe
,
1767 stObj
->pt
, dstLevel
, /* dest texture, level */
1768 stImage
->pt
, /* src texture */
1771 pipe_resource_reference(&stImage
->pt
, NULL
);
1773 else if (stImage
->base
.Data
) {
1774 /* More straightforward upload.
1776 st_teximage_flush_before_map(st
, stObj
->pt
, stImage
->face
, dstLevel
,
1777 PIPE_TRANSFER_WRITE
);
1779 st_texture_image_data(st
,
1784 stImage
->base
.RowStride
*
1785 util_format_get_blocksize(stObj
->pt
->format
),
1786 stImage
->base
.RowStride
*
1787 stImage
->base
.Height
*
1788 util_format_get_blocksize(stObj
->pt
->format
));
1789 _mesa_align_free(stImage
->base
.Data
);
1790 stImage
->base
.Data
= NULL
;
1793 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1798 * Called during state validation. When this function is finished,
1799 * the texture object should be ready for rendering.
1800 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1803 st_finalize_texture(GLcontext
*ctx
,
1804 struct pipe_context
*pipe
,
1805 struct gl_texture_object
*tObj
,
1806 GLboolean
*needFlush
)
1808 struct st_context
*st
= st_context(ctx
);
1809 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1810 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1811 GLuint blockSize
, face
;
1812 struct st_texture_image
*firstImage
;
1814 *needFlush
= GL_FALSE
;
1816 if (stObj
->base
._Complete
) {
1817 /* The texture is complete and we know exactly how many mipmap levels
1818 * are present/needed. This is conditional because we may be called
1819 * from the st_generate_mipmap() function when the texture object is
1820 * incomplete. In that case, we'll have set stObj->lastLevel before
1823 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1824 stObj
->base
.MinFilter
== GL_NEAREST
)
1825 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1827 stObj
->lastLevel
= stObj
->base
._MaxLevel
- stObj
->base
.BaseLevel
;
1830 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1832 /* If both firstImage and stObj point to a texture which can contain
1833 * all active images, favour firstImage. Note that because of the
1834 * completeness requirement, we know that the image dimensions
1837 if (firstImage
->pt
&&
1838 firstImage
->pt
!= stObj
->pt
&&
1839 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1840 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1841 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1844 /* bytes per pixel block (blocks are usually 1x1) */
1845 blockSize
= _mesa_get_format_bytes(firstImage
->base
.TexFormat
);
1847 /* If we already have a gallium texture, check that it matches the texture
1848 * object's format, target, size, num_levels, etc.
1851 const enum pipe_format fmt
=
1852 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1853 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1854 stObj
->pt
->format
!= fmt
||
1855 stObj
->pt
->last_level
< stObj
->lastLevel
||
1856 stObj
->pt
->width0
!= firstImage
->base
.Width2
||
1857 stObj
->pt
->height0
!= firstImage
->base
.Height2
||
1858 stObj
->pt
->depth0
!= firstImage
->base
.Depth2
)
1860 pipe_resource_reference(&stObj
->pt
, NULL
);
1861 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1862 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1866 /* May need to create a new gallium texture:
1869 const enum pipe_format fmt
=
1870 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1871 GLuint bindings
= default_bindings(st
, fmt
);
1873 stObj
->pt
= st_texture_create(st
,
1874 gl_target_to_pipe(stObj
->base
.Target
),
1877 firstImage
->base
.Width2
,
1878 firstImage
->base
.Height2
,
1879 firstImage
->base
.Depth2
,
1883 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1888 /* Pull in any images not in the object's texture:
1890 for (face
= 0; face
< nr_faces
; face
++) {
1892 for (level
= 0; level
<= stObj
->lastLevel
; level
++) {
1893 struct st_texture_image
*stImage
=
1894 st_texture_image(stObj
->base
.Image
[face
][stObj
->base
.BaseLevel
+ level
]);
1896 /* Need to import images in main memory or held in other textures.
1898 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1899 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1900 *needFlush
= GL_TRUE
;
1910 * Returns pointer to a default/dummy texture.
1911 * This is typically used when the current shader has tex/sample instructions
1912 * but the user has not provided a (any) texture(s).
1914 struct gl_texture_object
*
1915 st_get_default_texture(struct st_context
*st
)
1917 if (!st
->default_texture
) {
1918 static const GLenum target
= GL_TEXTURE_2D
;
1919 GLubyte pixels
[16][16][4];
1920 struct gl_texture_object
*texObj
;
1921 struct gl_texture_image
*texImg
;
1924 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1925 * when attempting to sample incomplete textures.
1927 for (i
= 0; i
< 16; i
++) {
1928 for (j
= 0; j
< 16; j
++) {
1929 pixels
[i
][j
][0] = 0;
1930 pixels
[i
][j
][1] = 0;
1931 pixels
[i
][j
][2] = 0;
1932 pixels
[i
][j
][3] = 255;
1936 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1938 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1940 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1941 16, 16, 1, 0, /* w, h, d, border */
1944 st_TexImage(st
->ctx
, 2, target
,
1945 0, GL_RGBA
, /* level, intformat */
1946 16, 16, 1, 0, /* w, h, d, border */
1947 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1948 &st
->ctx
->DefaultPacking
,
1952 texObj
->MinFilter
= GL_NEAREST
;
1953 texObj
->MagFilter
= GL_NEAREST
;
1954 texObj
->_Complete
= GL_TRUE
;
1956 st
->default_texture
= texObj
;
1958 return st
->default_texture
;
1963 st_init_texture_functions(struct dd_function_table
*functions
)
1965 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1966 functions
->TexImage1D
= st_TexImage1D
;
1967 functions
->TexImage2D
= st_TexImage2D
;
1968 functions
->TexImage3D
= st_TexImage3D
;
1969 functions
->TexSubImage1D
= st_TexSubImage1D
;
1970 functions
->TexSubImage2D
= st_TexSubImage2D
;
1971 functions
->TexSubImage3D
= st_TexSubImage3D
;
1972 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1973 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1974 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1975 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1976 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1977 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1978 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1979 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1980 functions
->GenerateMipmap
= st_generate_mipmap
;
1982 functions
->GetTexImage
= st_GetTexImage
;
1984 /* compressed texture functions */
1985 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1986 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1988 functions
->NewTextureObject
= st_NewTextureObject
;
1989 functions
->NewTextureImage
= st_NewTextureImage
;
1990 functions
->DeleteTexture
= st_DeleteTextureObject
;
1991 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1992 functions
->UpdateTexturePalette
= 0;
1994 functions
->TextureMemCpy
= do_memcpy
;
1996 /* XXX Temporary until we can query pipe's texture sizes */
1997 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;