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_public.h"
53 #include "state_tracker/st_texture.h"
54 #include "state_tracker/st_gen_mipmap.h"
55 #include "state_tracker/st_inlines.h"
56 #include "state_tracker/st_atom.h"
58 #include "pipe/p_context.h"
59 #include "pipe/p_defines.h"
60 #include "util/u_inlines.h"
61 #include "pipe/p_shader_tokens.h"
62 #include "util/u_tile.h"
63 #include "util/u_blit.h"
64 #include "util/u_format.h"
65 #include "util/u_surface.h"
66 #include "util/u_sampler.h"
67 #include "util/u_math.h"
70 #define DBG if (0) printf
73 static enum pipe_texture_target
74 gl_target_to_pipe(GLenum target
)
78 return PIPE_TEXTURE_1D
;
81 case GL_TEXTURE_RECTANGLE_NV
:
82 return PIPE_TEXTURE_2D
;
85 return PIPE_TEXTURE_3D
;
87 case GL_TEXTURE_CUBE_MAP_ARB
:
88 return PIPE_TEXTURE_CUBE
;
97 /** called via ctx->Driver.NewTextureImage() */
98 static struct gl_texture_image
*
99 st_NewTextureImage(GLcontext
* ctx
)
101 DBG("%s\n", __FUNCTION__
);
103 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
107 /** called via ctx->Driver.NewTextureObject() */
108 static struct gl_texture_object
*
109 st_NewTextureObject(GLcontext
* ctx
, GLuint name
, GLenum target
)
111 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
113 DBG("%s\n", __FUNCTION__
);
114 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
119 /** called via ctx->Driver.DeleteTextureImage() */
121 st_DeleteTextureObject(GLcontext
*ctx
,
122 struct gl_texture_object
*texObj
)
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
!= ctx
->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
= ctx
->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 usage bitmask for the given texture format.
215 default_usage(enum pipe_format fmt
)
217 GLuint usage
= PIPE_BIND_SAMPLER_VIEW
;
218 if (util_format_is_depth_or_stencil(fmt
))
219 usage
|= PIPE_BIND_DEPTH_STENCIL
;
221 usage
|= PIPE_BIND_RENDER_TARGET
;
227 * Allocate a pipe_resource object for the given st_texture_object using
228 * the given st_texture_image to guess the mipmap size/levels.
231 * Otherwise, store it in memory if (Border != 0) or (any dimension ==
234 * Otherwise, if max_level >= level >= min_level, create texture with
235 * space for images from min_level down to max_level.
237 * Otherwise, create texture with space for images from (level 0)..(1x1).
238 * Consider pruning this texture at a validation if the saving is worth it.
241 guess_and_alloc_texture(struct st_context
*st
,
242 struct st_texture_object
*stObj
,
243 const struct st_texture_image
*stImage
)
247 GLuint width
= stImage
->base
.Width2
; /* size w/out border */
248 GLuint height
= stImage
->base
.Height2
;
249 GLuint depth
= stImage
->base
.Depth2
;
251 enum pipe_format fmt
;
253 DBG("%s\n", __FUNCTION__
);
258 (GLint
) stImage
->level
> stObj
->base
.BaseLevel
&&
259 (stImage
->base
.Width
== 1 ||
260 (stObj
->base
.Target
!= GL_TEXTURE_1D
&&
261 stImage
->base
.Height
== 1) ||
262 (stObj
->base
.Target
== GL_TEXTURE_3D
&&
263 stImage
->base
.Depth
== 1)))
266 /* If this image disrespects BaseLevel, allocate from level zero.
267 * Usually BaseLevel == 0, so it's unlikely to happen.
269 if ((GLint
) stImage
->level
< stObj
->base
.BaseLevel
)
272 firstLevel
= stObj
->base
.BaseLevel
;
275 /* Figure out image dimensions at start level.
277 for (i
= stImage
->level
; i
> firstLevel
; i
--) {
286 if (width
== 0 || height
== 0 || depth
== 0) {
287 /* no texture needed */
291 /* Guess a reasonable value for lastLevel. This is probably going
292 * to be wrong fairly often and might mean that we have to look at
293 * resizable buffers, or require that buffers implement lazy
294 * pagetable arrangements.
296 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
297 stObj
->base
.MinFilter
== GL_LINEAR
||
298 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
299 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
300 !stObj
->base
.GenerateMipmap
&&
301 stImage
->level
== firstLevel
) {
302 /* only alloc space for a single mipmap level */
303 lastLevel
= firstLevel
;
306 /* alloc space for a full mipmap */
307 GLuint l2width
= util_logbase2(width
);
308 GLuint l2height
= util_logbase2(height
);
309 GLuint l2depth
= util_logbase2(depth
);
310 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
313 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
315 usage
= default_usage(fmt
);
317 stObj
->pt
= st_texture_create(st
,
318 gl_target_to_pipe(stObj
->base
.Target
),
326 stObj
->pipe
= st
->pipe
;
328 DBG("%s - success\n", __FUNCTION__
);
333 * Adjust pixel unpack params and image dimensions to strip off the
335 * Gallium doesn't support texture borders. They've seldem been used
336 * and seldom been implemented correctly anyway.
337 * \param unpackNew returns the new pixel unpack parameters
340 strip_texture_border(GLint border
,
341 GLint
*width
, GLint
*height
, GLint
*depth
,
342 const struct gl_pixelstore_attrib
*unpack
,
343 struct gl_pixelstore_attrib
*unpackNew
)
345 assert(border
> 0); /* sanity check */
347 *unpackNew
= *unpack
;
349 if (unpackNew
->RowLength
== 0)
350 unpackNew
->RowLength
= *width
;
352 if (depth
&& unpackNew
->ImageHeight
== 0)
353 unpackNew
->ImageHeight
= *height
;
355 unpackNew
->SkipPixels
+= border
;
357 unpackNew
->SkipRows
+= border
;
359 unpackNew
->SkipImages
+= border
;
362 *width
= *width
- 2 * border
;
363 if (height
&& *height
>= 3)
364 *height
= *height
- 2 * border
;
365 if (depth
&& *depth
>= 3)
366 *depth
= *depth
- 2 * border
;
371 * Try to do texture compression via rendering. If the Gallium driver
372 * can render into a compressed surface this will allow us to do texture
374 * \return GL_TRUE for success, GL_FALSE for failure
377 compress_with_blit(GLcontext
* ctx
,
378 GLenum target
, GLint level
,
379 GLint xoffset
, GLint yoffset
, GLint zoffset
,
380 GLint width
, GLint height
, GLint depth
,
381 GLenum format
, GLenum type
, const void *pixels
,
382 const struct gl_pixelstore_attrib
*unpack
,
383 struct gl_texture_image
*texImage
)
385 const GLuint dstImageOffsets
[1] = {0};
386 struct st_texture_image
*stImage
= st_texture_image(texImage
);
387 struct pipe_context
*pipe
= ctx
->st
->pipe
;
388 struct pipe_screen
*screen
= pipe
->screen
;
389 gl_format mesa_format
;
390 struct pipe_resource templ
;
391 struct pipe_resource
*src_tex
;
392 struct pipe_sampler_view view_templ
;
393 struct pipe_sampler_view
*src_view
;
394 struct pipe_surface
*dst_surface
;
395 struct pipe_transfer
*tex_xfer
;
399 /* XXX: Can this happen? Should we assert? */
403 /* get destination surface (in the compressed texture) */
404 dst_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
405 stImage
->face
, stImage
->level
, 0,
406 PIPE_BIND_BLIT_DESTINATION
);
408 /* can't render into this format (or other problem) */
412 /* Choose format for the temporary RGBA texture image.
414 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
419 /* Create the temporary source texture
421 memset(&templ
, 0, sizeof(templ
));
422 templ
.target
= PIPE_TEXTURE_2D
;
423 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
);
424 templ
.width0
= width
;
425 templ
.height0
= height
;
427 templ
.last_level
= 0;
428 templ
._usage
= PIPE_USAGE_DEFAULT
;
429 templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
430 src_tex
= screen
->resource_create(screen
, &templ
);
435 /* Put user's tex data into the temporary texture
437 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
), src_tex
,
438 0, 0, 0, /* face, level are zero */
440 0, 0, width
, height
); /* x, y, w, h */
441 map
= pipe_transfer_map(pipe
, tex_xfer
);
443 _mesa_texstore(ctx
, 2, GL_RGBA
, mesa_format
,
445 0, 0, 0, /* dest x/y/z offset */
446 tex_xfer
->stride
, /* dest row stride (bytes) */
447 dstImageOffsets
, /* image offsets (for 3D only) */
448 width
, height
, 1, /* size */
449 format
, type
, /* source format/type */
450 pixels
, /* source data */
451 unpack
); /* source data packing */
453 pipe_transfer_unmap(pipe
, tex_xfer
);
454 pipe
->transfer_destroy(pipe
, tex_xfer
);
456 /* Create temporary sampler view */
457 u_sampler_view_default_template(&view_templ
,
460 src_view
= pipe
->create_sampler_view(pipe
, src_tex
, &view_templ
);
463 /* copy / compress image */
464 util_blit_pixels_tex(ctx
->st
->blit
,
465 src_view
, /* sampler view (src) */
466 0, 0, /* src x0, y0 */
467 width
, height
, /* src x1, y1 */
468 dst_surface
, /* pipe_surface (dst) */
469 xoffset
, yoffset
, /* dst x0, y0 */
470 xoffset
+ width
, /* dst x1 */
471 yoffset
+ height
, /* dst y1 */
473 PIPE_TEX_MIPFILTER_NEAREST
);
475 pipe_surface_reference(&dst_surface
, NULL
);
476 pipe_resource_reference(&src_tex
, NULL
);
477 pipe_sampler_view_reference(&src_view
, NULL
);
484 * Do glTexImage1/2/3D().
487 st_TexImage(GLcontext
* ctx
,
489 GLenum target
, GLint level
,
490 GLint internalFormat
,
491 GLint width
, GLint height
, GLint depth
,
493 GLenum format
, GLenum type
, const void *pixels
,
494 const struct gl_pixelstore_attrib
*unpack
,
495 struct gl_texture_object
*texObj
,
496 struct gl_texture_image
*texImage
,
497 GLsizei imageSize
, GLboolean compressed_src
)
499 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
500 struct st_texture_object
*stObj
= st_texture_object(texObj
);
501 struct st_texture_image
*stImage
= st_texture_image(texImage
);
502 GLint postConvWidth
, postConvHeight
;
503 GLint texelBytes
, sizeInBytes
;
504 GLuint dstRowStride
= 0;
505 struct gl_pixelstore_attrib unpackNB
;
506 enum pipe_transfer_usage transfer_usage
= 0;
508 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
509 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
511 /* switch to "normal" */
512 if (stObj
->surface_based
) {
513 _mesa_clear_texture_object(ctx
, texObj
);
514 stObj
->surface_based
= GL_FALSE
;
517 /* gallium does not support texture borders, strip it off */
519 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
521 texImage
->Width
= width
;
522 texImage
->Height
= height
;
523 texImage
->Depth
= depth
;
524 texImage
->Border
= 0;
528 postConvWidth
= width
;
529 postConvHeight
= height
;
531 stImage
->face
= _mesa_tex_target_to_face(target
);
532 stImage
->level
= level
;
535 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
536 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
541 _mesa_set_fetch_functions(texImage
, dims
);
543 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
544 /* must be a compressed format */
548 texelBytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
550 /* Minimum pitch of 32 bytes */
551 if (postConvWidth
* texelBytes
< 32) {
552 postConvWidth
= 32 / texelBytes
;
553 texImage
->RowStride
= postConvWidth
;
556 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
557 /*assert(texImage->RowStride == postConvWidth);*/
560 /* Release the reference to a potentially orphaned buffer.
561 * Release any old malloced memory.
564 pipe_resource_reference(&stImage
->pt
, NULL
);
565 assert(!texImage
->Data
);
567 else if (texImage
->Data
) {
568 _mesa_align_free(texImage
->Data
);
572 * See if the new image is somehow incompatible with the existing
573 * mipmap. If so, free the old mipmap.
576 if (stObj
->teximage_realloc
||
577 level
> (GLint
) stObj
->pt
->last_level
||
578 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
579 stImage
->face
, stImage
->level
)) {
581 pipe_resource_reference(&stObj
->pt
, NULL
);
583 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
584 stObj
->teximage_realloc
= FALSE
;
588 if (width
== 0 || height
== 0 || depth
== 0) {
589 /* stop after freeing old image */
594 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
596 /* Probably out of memory.
597 * Try flushing any pending rendering, then retry.
600 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
602 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
608 assert(!stImage
->pt
);
611 st_texture_match_image(stObj
->pt
, &stImage
->base
,
612 stImage
->face
, stImage
->level
)) {
614 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
619 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
621 /* st_CopyTexImage calls this function with pixels == NULL, with
622 * the expectation that the texture will be set up but nothing
623 * more will be done. This is where those calls return:
625 if (compressed_src
) {
626 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
628 "glCompressedTexImage");
631 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
633 pixels
, unpack
, "glTexImage");
636 /* Note: we can't check for pixels==NULL until after we've allocated
637 * memory for the texture.
640 /* See if we can do texture compression with a blit/render.
642 if (!compressed_src
&&
644 _mesa_is_format_compressed(texImage
->TexFormat
) &&
645 screen
->is_format_supported(screen
,
648 PIPE_BIND_RENDER_TARGET
, 0)) {
652 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
653 format
, type
, pixels
, unpack
, texImage
)) {
659 if (format
== GL_DEPTH_COMPONENT
&&
660 util_format_is_depth_and_stencil(stImage
->pt
->format
))
661 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
663 transfer_usage
= PIPE_TRANSFER_WRITE
;
665 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
666 transfer_usage
, 0, 0,
668 stImage
->base
.Height
);
669 if(stImage
->transfer
)
670 dstRowStride
= stImage
->transfer
->stride
;
673 /* Allocate regular memory and store the image there temporarily. */
674 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
675 sizeInBytes
= _mesa_format_image_size(texImage
->TexFormat
,
679 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
683 dstRowStride
= postConvWidth
* texelBytes
;
684 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
687 texImage
->Data
= _mesa_align_malloc(sizeInBytes
, 16);
690 if (!texImage
->Data
) {
691 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
698 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
699 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
701 /* Copy data. Would like to know when it's ok for us to eg. use
702 * the blitter to copy. Or, use the hardware to do the format
703 * conversion and copy:
705 if (compressed_src
) {
706 const GLuint srcImageStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
707 if(dstRowStride
== srcImageStride
)
708 memcpy(texImage
->Data
, pixels
, imageSize
);
711 char *dst
= texImage
->Data
;
712 const char *src
= pixels
;
713 GLuint i
, bw
, bh
, lines
;
714 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
715 lines
= (height
+ bh
- 1) / bh
;
717 for(i
= 0; i
< lines
; ++i
)
719 memcpy(dst
, src
, srcImageStride
);
721 src
+= srcImageStride
;
726 const GLuint srcImageStride
=
727 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
729 const GLubyte
*src
= (const GLubyte
*) pixels
;
731 for (i
= 0; i
< depth
; i
++) {
732 if (!_mesa_texstore(ctx
, dims
,
733 texImage
->_BaseFormat
,
736 0, 0, 0, /* dstX/Y/Zoffset */
738 texImage
->ImageOffsets
,
740 format
, type
, src
, unpack
)) {
741 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
744 if (stImage
->pt
&& i
+ 1 < depth
) {
745 /* unmap this slice */
746 st_texture_image_unmap(ctx
->st
, stImage
);
747 /* map next slice of 3D texture */
748 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
749 transfer_usage
, 0, 0,
751 stImage
->base
.Height
);
752 src
+= srcImageStride
;
758 _mesa_unmap_teximage_pbo(ctx
, unpack
);
760 if (stImage
->pt
&& texImage
->Data
) {
761 st_texture_image_unmap(ctx
->st
, stImage
);
762 texImage
->Data
= NULL
;
768 st_TexImage3D(GLcontext
* ctx
,
769 GLenum target
, GLint level
,
770 GLint internalFormat
,
771 GLint width
, GLint height
, GLint depth
,
773 GLenum format
, GLenum type
, const void *pixels
,
774 const struct gl_pixelstore_attrib
*unpack
,
775 struct gl_texture_object
*texObj
,
776 struct gl_texture_image
*texImage
)
778 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
779 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
785 st_TexImage2D(GLcontext
* ctx
,
786 GLenum target
, GLint level
,
787 GLint internalFormat
,
788 GLint width
, GLint height
, GLint border
,
789 GLenum format
, GLenum type
, const void *pixels
,
790 const struct gl_pixelstore_attrib
*unpack
,
791 struct gl_texture_object
*texObj
,
792 struct gl_texture_image
*texImage
)
794 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
795 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
800 st_TexImage1D(GLcontext
* ctx
,
801 GLenum target
, GLint level
,
802 GLint internalFormat
,
803 GLint width
, GLint border
,
804 GLenum format
, GLenum type
, const void *pixels
,
805 const struct gl_pixelstore_attrib
*unpack
,
806 struct gl_texture_object
*texObj
,
807 struct gl_texture_image
*texImage
)
809 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
810 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
815 st_CompressedTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
816 GLint internalFormat
,
817 GLint width
, GLint height
, GLint border
,
818 GLsizei imageSize
, const GLvoid
*data
,
819 struct gl_texture_object
*texObj
,
820 struct gl_texture_image
*texImage
)
822 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
823 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
829 * glGetTexImage() helper: decompress a compressed texture by rendering
830 * a textured quad. Store the results in the user's buffer.
833 decompress_with_blit(GLcontext
* ctx
, GLenum target
, GLint level
,
834 GLenum format
, GLenum type
, GLvoid
*pixels
,
835 struct gl_texture_object
*texObj
,
836 struct gl_texture_image
*texImage
)
838 struct pipe_context
*pipe
= ctx
->st
->pipe
;
839 struct pipe_screen
*screen
= pipe
->screen
;
840 struct st_texture_image
*stImage
= st_texture_image(texImage
);
841 struct st_texture_object
*stObj
= st_texture_object(texObj
);
842 struct pipe_sampler_view
*src_view
= st_get_stobj_sampler_view(stObj
);
843 const GLuint width
= texImage
->Width
;
844 const GLuint height
= texImage
->Height
;
845 struct pipe_surface
*dst_surface
;
846 struct pipe_resource
*dst_texture
;
847 struct pipe_transfer
*tex_xfer
;
848 unsigned bind
= (PIPE_BIND_BLIT_DESTINATION
|
849 PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
850 PIPE_BIND_TRANSFER_READ
);
852 /* create temp / dest surface */
853 if (!util_create_rgba_surface(screen
, width
, height
, bind
,
854 &dst_texture
, &dst_surface
)) {
855 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
856 "in decompress_with_blit()");
860 /* blit/render/decompress */
861 util_blit_pixels_tex(ctx
->st
->blit
,
862 src_view
, /* pipe_resource (src) */
863 0, 0, /* src x0, y0 */
864 width
, height
, /* src x1, y1 */
865 dst_surface
, /* pipe_surface (dst) */
866 0, 0, /* dst x0, y0 */
867 width
, height
, /* dst x1, y1 */
869 PIPE_TEX_MIPFILTER_NEAREST
);
871 /* map the dst_surface so we can read from it */
872 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
),
873 dst_texture
, 0, 0, 0,
875 0, 0, width
, height
);
877 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
879 /* copy/pack data into user buffer */
880 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
882 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
883 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
885 for (row
= 0; row
< height
; row
++) {
886 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
887 height
, format
, type
, row
, 0);
888 memcpy(dest
, map
, bytesPerRow
);
889 map
+= tex_xfer
->stride
;
891 pipe_transfer_unmap(pipe
, tex_xfer
);
894 /* format translation via floats */
896 for (row
= 0; row
< height
; row
++) {
897 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
898 GLfloat rgba
[4 * MAX_WIDTH
];
899 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
900 height
, format
, type
, row
, 0);
902 if (ST_DEBUG
& DEBUG_FALLBACK
)
903 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
905 /* get float[4] rgba row from surface */
906 pipe_get_tile_rgba(pipe
, tex_xfer
, 0, row
, width
, 1, rgba
);
908 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
909 type
, dest
, &ctx
->Pack
, transferOps
);
913 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
915 pipe
->transfer_destroy(pipe
, tex_xfer
);
917 /* destroy the temp / dest surface */
918 util_destroy_rgba_surface(dst_texture
, dst_surface
);
924 * Need to map texture image into memory before copying image data,
928 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
929 GLenum format
, GLenum type
, GLvoid
* pixels
,
930 struct gl_texture_object
*texObj
,
931 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
933 struct st_texture_image
*stImage
= st_texture_image(texImage
);
934 const GLuint dstImageStride
=
935 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
941 util_format_is_s3tc(stImage
->pt
->format
) &&
943 /* Need to decompress the texture.
944 * We'll do this by rendering a textured quad.
945 * Note that we only expect RGBA formats (no Z/depth formats).
947 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
954 /* Image is stored in hardware format in a buffer managed by the
955 * kernel. Need to explicitly map and unmap it.
957 unsigned face
= _mesa_tex_target_to_face(target
);
959 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
962 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
963 PIPE_TRANSFER_READ
, 0, 0,
965 stImage
->base
.Height
);
966 texImage
->RowStride
= stImage
->transfer
->stride
/ util_format_get_blocksize(stImage
->pt
->format
);
969 /* Otherwise, the image should actually be stored in
970 * texImage->Data. This is pretty confusing for
971 * everybody, I'd much prefer to separate the two functions of
972 * texImage->Data - storage for texture images in main memory
973 * and access (ie mappings) of images. In other words, we'd
974 * create a new texImage->Map field and leave Data simply for
977 assert(texImage
->Data
);
980 depth
= texImage
->Depth
;
983 dest
= (GLubyte
*) pixels
;
985 for (i
= 0; i
< depth
; i
++) {
986 if (compressed_dst
) {
987 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
991 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
995 if (stImage
->pt
&& i
+ 1 < depth
) {
996 /* unmap this slice */
997 st_texture_image_unmap(ctx
->st
, stImage
);
998 /* map next slice of 3D texture */
999 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
1000 PIPE_TRANSFER_READ
, 0, 0,
1001 stImage
->base
.Width
,
1002 stImage
->base
.Height
);
1003 dest
+= dstImageStride
;
1007 texImage
->Depth
= depth
;
1011 st_texture_image_unmap(ctx
->st
, stImage
);
1012 texImage
->Data
= NULL
;
1018 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
1019 GLenum format
, GLenum type
, GLvoid
* pixels
,
1020 struct gl_texture_object
*texObj
,
1021 struct gl_texture_image
*texImage
)
1023 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1029 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
1031 struct gl_texture_object
*texObj
,
1032 struct gl_texture_image
*texImage
)
1034 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1041 st_TexSubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
1042 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1043 GLint width
, GLint height
, GLint depth
,
1044 GLenum format
, GLenum type
, const void *pixels
,
1045 const struct gl_pixelstore_attrib
*packing
,
1046 struct gl_texture_object
*texObj
,
1047 struct gl_texture_image
*texImage
)
1049 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
1050 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1051 GLuint dstRowStride
;
1052 const GLuint srcImageStride
=
1053 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1056 /* init to silence warning only: */
1057 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1059 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1060 _mesa_lookup_enum_by_nr(target
),
1061 level
, xoffset
, yoffset
, width
, height
);
1064 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1065 type
, pixels
, packing
, "glTexSubImage2D");
1069 /* See if we can do texture compression with a blit/render.
1071 if (!ctx
->Mesa_DXTn
&&
1072 _mesa_is_format_compressed(texImage
->TexFormat
) &&
1073 screen
->is_format_supported(screen
,
1074 stImage
->pt
->format
,
1075 stImage
->pt
->target
,
1076 PIPE_BIND_RENDER_TARGET
, 0)) {
1077 if (compress_with_blit(ctx
, target
, level
,
1078 xoffset
, yoffset
, zoffset
,
1079 width
, height
, depth
,
1080 format
, type
, pixels
, packing
, texImage
)) {
1085 /* Map buffer if necessary. Need to lock to prevent other contexts
1086 * from uploading the buffer under us.
1089 unsigned face
= _mesa_tex_target_to_face(target
);
1091 if (format
== GL_DEPTH_COMPONENT
&&
1092 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1093 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1095 transfer_usage
= PIPE_TRANSFER_WRITE
;
1097 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1099 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
,
1105 if (!texImage
->Data
) {
1106 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1110 src
= (const GLubyte
*) pixels
;
1111 dstRowStride
= stImage
->transfer
->stride
;
1113 for (i
= 0; i
< depth
; i
++) {
1114 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1115 texImage
->TexFormat
,
1119 texImage
->ImageOffsets
,
1121 format
, type
, src
, packing
)) {
1122 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1125 if (stImage
->pt
&& i
+ 1 < depth
) {
1126 /* unmap this slice */
1127 st_texture_image_unmap(ctx
->st
, stImage
);
1128 /* map next slice of 3D texture */
1129 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
,
1134 src
+= srcImageStride
;
1139 _mesa_unmap_teximage_pbo(ctx
, packing
);
1141 if (stImage
->pt
&& texImage
->Data
) {
1142 st_texture_image_unmap(ctx
->st
, stImage
);
1143 texImage
->Data
= NULL
;
1150 st_TexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1151 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1152 GLsizei width
, GLsizei height
, GLsizei depth
,
1153 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1154 const struct gl_pixelstore_attrib
*packing
,
1155 struct gl_texture_object
*texObj
,
1156 struct gl_texture_image
*texImage
)
1158 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1159 width
, height
, depth
, format
, type
,
1160 pixels
, packing
, texObj
, texImage
);
1165 st_TexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1166 GLint xoffset
, GLint yoffset
,
1167 GLsizei width
, GLsizei height
,
1168 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1169 const struct gl_pixelstore_attrib
*packing
,
1170 struct gl_texture_object
*texObj
,
1171 struct gl_texture_image
*texImage
)
1173 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1174 width
, height
, 1, format
, type
,
1175 pixels
, packing
, texObj
, texImage
);
1180 st_TexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1181 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1182 const GLvoid
* pixels
,
1183 const struct gl_pixelstore_attrib
*packing
,
1184 struct gl_texture_object
*texObj
,
1185 struct gl_texture_image
*texImage
)
1187 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1188 format
, type
, pixels
, packing
, texObj
, texImage
);
1193 st_CompressedTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1194 GLint xoffset
, GLsizei width
,
1196 GLsizei imageSize
, const GLvoid
*data
,
1197 struct gl_texture_object
*texObj
,
1198 struct gl_texture_image
*texImage
)
1205 st_CompressedTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1206 GLint xoffset
, GLint yoffset
,
1207 GLsizei width
, GLint height
,
1209 GLsizei imageSize
, const GLvoid
*data
,
1210 struct gl_texture_object
*texObj
,
1211 struct gl_texture_image
*texImage
)
1213 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1217 enum pipe_format pformat
= stImage
->pt
->format
;
1220 unsigned face
= _mesa_tex_target_to_face(target
);
1222 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1223 PIPE_TRANSFER_WRITE
);
1224 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
1225 PIPE_TRANSFER_WRITE
,
1229 srcBlockStride
= util_format_get_stride(pformat
, width
);
1230 dstBlockStride
= stImage
->transfer
->stride
;
1232 assert(stImage
->pt
);
1233 /* TODO find good values for block and strides */
1234 /* TODO also adjust texImage->data for yoffset/xoffset */
1238 if (!texImage
->Data
) {
1239 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1243 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1244 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1245 assert(width
% util_format_get_blockwidth(pformat
) == 0);
1246 assert(height
% util_format_get_blockheight(pformat
) == 0);
1248 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1249 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1250 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1251 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1252 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1256 st_texture_image_unmap(ctx
->st
, stImage
);
1257 texImage
->Data
= NULL
;
1263 st_CompressedTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1264 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1265 GLsizei width
, GLint height
, GLint depth
,
1267 GLsizei imageSize
, const GLvoid
*data
,
1268 struct gl_texture_object
*texObj
,
1269 struct gl_texture_image
*texImage
)
1277 * Do a CopyTexSubImage operation using a read transfer from the source,
1278 * a write transfer to the destination and get_tile()/put_tile() to access
1279 * the pixels/texels.
1281 * Note: srcY=0=TOP of renderbuffer
1284 fallback_copy_texsubimage(GLcontext
*ctx
, GLenum target
, GLint level
,
1285 struct st_renderbuffer
*strb
,
1286 struct st_texture_image
*stImage
,
1288 GLint destX
, GLint destY
, GLint destZ
,
1289 GLint srcX
, GLint srcY
,
1290 GLsizei width
, GLsizei height
)
1292 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1293 struct pipe_transfer
*src_trans
;
1295 enum pipe_transfer_usage transfer_usage
;
1297 if (ST_DEBUG
& DEBUG_FALLBACK
)
1298 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1300 assert(width
<= MAX_WIDTH
);
1302 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1303 srcY
= strb
->Base
.Height
- srcY
- height
;
1306 src_trans
= st_cond_flush_get_tex_transfer( st_context(ctx
),
1313 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1314 baseFormat
== GL_DEPTH_STENCIL
) &&
1315 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1316 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1318 transfer_usage
= PIPE_TRANSFER_WRITE
;
1320 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, 0, 0,
1323 texDest
= st_texture_image_map(ctx
->st
, stImage
, 0, transfer_usage
,
1324 destX
, destY
, width
, height
);
1326 if (baseFormat
== GL_DEPTH_COMPONENT
||
1327 baseFormat
== GL_DEPTH_STENCIL
) {
1328 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1329 ctx
->Pixel
.DepthBias
!= 0.0F
);
1332 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1333 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1342 /* To avoid a large temp memory allocation, do copy row by row */
1343 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1344 uint data
[MAX_WIDTH
];
1345 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1347 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1349 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1355 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1357 if (tempSrc
&& texDest
) {
1358 const GLint dims
= 2;
1359 const GLint dstRowStride
= stImage
->transfer
->stride
;
1360 struct gl_texture_image
*texImage
= &stImage
->base
;
1361 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1363 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1364 unpack
.Invert
= GL_TRUE
;
1367 /* get float/RGBA image from framebuffer */
1368 /* XXX this usually involves a lot of int/float conversion.
1369 * try to avoid that someday.
1371 pipe_get_tile_rgba(pipe
, src_trans
, 0, 0, width
, height
, tempSrc
);
1373 /* Store into texture memory.
1374 * Note that this does some special things such as pixel transfer
1375 * ops and format conversion. In particular, if the dest tex format
1376 * is actually RGBA but the user created the texture as GL_RGB we
1377 * need to fill-in/override the alpha channel with 1.0.
1379 _mesa_texstore(ctx
, dims
,
1380 texImage
->_BaseFormat
,
1381 texImage
->TexFormat
,
1385 texImage
->ImageOffsets
,
1387 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1391 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1398 st_texture_image_unmap(ctx
->st
, stImage
);
1399 pipe
->transfer_destroy(pipe
, src_trans
);
1405 * If the format of the src renderbuffer and the format of the dest
1406 * texture are compatible (in terms of blitting), return a TGSI writemask
1407 * to be used during the blit.
1408 * If the src/dest are incompatible, return 0.
1411 compatible_src_dst_formats(GLcontext
*ctx
,
1412 const struct gl_renderbuffer
*src
,
1413 const struct gl_texture_image
*dst
)
1415 /* Get logical base formats for the src and dest.
1416 * That is, use the user-requested formats and not the actual, device-
1418 * For example, the user may have requested an A8 texture but the
1419 * driver may actually be using an RGBA texture format. When we
1420 * copy/blit to that texture, we only want to copy the Alpha channel
1421 * and not the RGB channels.
1423 * Similarly, when the src FBO was created an RGB format may have been
1424 * requested but the driver actually chose an RGBA format. In that case,
1425 * we don't want to copy the undefined Alpha channel to the dest texture
1426 * (it should be 1.0).
1428 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1429 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1432 * XXX when we have red-only and red/green renderbuffers we'll need
1433 * to add more cases here (or implement a general-purpose routine that
1434 * queries the existance of the R,G,B,A channels in the src and dest).
1436 if (srcFormat
== dstFormat
) {
1437 /* This is the same as matching_base_formats, which should
1438 * always pass, as it did previously.
1440 return TGSI_WRITEMASK_XYZW
;
1442 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1443 /* Make sure that A in the dest is 1. The actual src format
1444 * may be RGBA and have undefined A values.
1446 return TGSI_WRITEMASK_XYZ
;
1448 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1449 /* Make sure that A in the dest is 1. The actual dst format
1450 * may be RGBA and will need A=1 to provide proper alpha values
1451 * when sampled later.
1453 return TGSI_WRITEMASK_XYZ
;
1456 if (ST_DEBUG
& DEBUG_FALLBACK
)
1457 debug_printf("%s failed for src %s, dst %s\n",
1459 _mesa_lookup_enum_by_nr(srcFormat
),
1460 _mesa_lookup_enum_by_nr(dstFormat
));
1471 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1472 * Note that the region to copy has already been clipped so we know we
1473 * won't read from outside the source renderbuffer's bounds.
1475 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1478 st_copy_texsubimage(GLcontext
*ctx
,
1479 GLenum target
, GLint level
,
1480 GLint destX
, GLint destY
, GLint destZ
,
1481 GLint srcX
, GLint srcY
,
1482 GLsizei width
, GLsizei height
)
1484 struct gl_texture_unit
*texUnit
=
1485 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1486 struct gl_texture_object
*texObj
=
1487 _mesa_select_tex_object(ctx
, texUnit
, target
);
1488 struct gl_texture_image
*texImage
=
1489 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1490 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1491 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1492 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1493 struct st_renderbuffer
*strb
;
1494 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1495 struct pipe_screen
*screen
= pipe
->screen
;
1496 enum pipe_format dest_format
, src_format
;
1497 GLboolean use_fallback
= GL_TRUE
;
1498 GLboolean matching_base_formats
;
1499 GLuint format_writemask
;
1500 struct pipe_surface
*dest_surface
= NULL
;
1501 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1503 /* any rendering in progress must flushed before we grab the fb image */
1504 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
1506 /* make sure finalize_textures has been called?
1508 if (0) st_validate_state(ctx
->st
);
1510 /* determine if copying depth or color data */
1511 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1512 texBaseFormat
== GL_DEPTH_STENCIL
) {
1513 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1516 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1517 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1520 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1521 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1549 if (width
< 0 || height
< 0)
1554 assert(strb
->surface
);
1555 assert(stImage
->pt
);
1557 src_format
= strb
->surface
->format
;
1558 dest_format
= stImage
->pt
->format
;
1561 * Determine if the src framebuffer and dest texture have the same
1562 * base format. We need this to detect a case such as the framebuffer
1563 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1564 * texture format stores RGBA we need to set A=1 (overriding the
1565 * framebuffer's alpha values). We can't do that with the blit or
1566 * textured-quad paths.
1568 matching_base_formats
=
1569 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1570 _mesa_get_format_base_format(texImage
->TexFormat
));
1571 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1573 if (ctx
->_ImageTransferState
== 0x0) {
1575 if (matching_base_formats
&&
1576 src_format
== dest_format
&&
1579 /* use surface_copy() / blit */
1581 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1582 stImage
->face
, stImage
->level
,
1584 PIPE_BIND_BLIT_DESTINATION
);
1586 /* for surface_copy(), y=0=top, always */
1587 pipe
->surface_copy(pipe
,
1596 use_fallback
= GL_FALSE
;
1598 else if (format_writemask
&&
1599 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1600 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1601 screen
->is_format_supported(screen
, src_format
,
1603 PIPE_BIND_SAMPLER_VIEW
,
1605 screen
->is_format_supported(screen
, dest_format
,
1607 PIPE_BIND_RENDER_TARGET
,
1609 /* draw textured quad to do the copy */
1612 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1613 stImage
->face
, stImage
->level
,
1615 PIPE_BIND_BLIT_DESTINATION
);
1618 srcY1
= strb
->Base
.Height
- srcY
- height
;
1619 srcY0
= srcY1
+ height
;
1623 srcY1
= srcY0
+ height
;
1625 util_blit_pixels_writemask(ctx
->st
->blit
,
1627 st_renderbuffer_get_sampler_view(strb
, pipe
),
1629 srcX
+ width
, srcY1
,
1632 destX
+ width
, destY
+ height
,
1633 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1635 use_fallback
= GL_FALSE
;
1639 pipe_surface_reference(&dest_surface
, NULL
);
1643 /* software fallback */
1644 fallback_copy_texsubimage(ctx
, target
, level
,
1645 strb
, stImage
, texBaseFormat
,
1646 destX
, destY
, destZ
,
1647 srcX
, srcY
, width
, height
);
1654 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1655 GLenum internalFormat
,
1656 GLint x
, GLint y
, GLsizei width
, GLint border
)
1658 struct gl_texture_unit
*texUnit
=
1659 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1660 struct gl_texture_object
*texObj
=
1661 _mesa_select_tex_object(ctx
, texUnit
, target
);
1662 struct gl_texture_image
*texImage
=
1663 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1665 /* Setup or redefine the texture object, texture and texture
1666 * image. Don't populate yet.
1668 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1670 GL_RGBA
, CHAN_TYPE
, NULL
,
1671 &ctx
->DefaultPacking
, texObj
, texImage
);
1673 st_copy_texsubimage(ctx
, target
, level
,
1674 0, 0, 0, /* destX,Y,Z */
1675 x
, y
, width
, 1); /* src X, Y, size */
1680 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1681 GLenum internalFormat
,
1682 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1685 struct gl_texture_unit
*texUnit
=
1686 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1687 struct gl_texture_object
*texObj
=
1688 _mesa_select_tex_object(ctx
, texUnit
, target
);
1689 struct gl_texture_image
*texImage
=
1690 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1692 /* Setup or redefine the texture object, texture and texture
1693 * image. Don't populate yet.
1695 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1696 width
, height
, border
,
1697 GL_RGBA
, CHAN_TYPE
, NULL
,
1698 &ctx
->DefaultPacking
, texObj
, texImage
);
1700 st_copy_texsubimage(ctx
, target
, level
,
1701 0, 0, 0, /* destX,Y,Z */
1702 x
, y
, width
, height
); /* src X, Y, size */
1707 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1708 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1710 const GLint yoffset
= 0, zoffset
= 0;
1711 const GLsizei height
= 1;
1712 st_copy_texsubimage(ctx
, target
, level
,
1713 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1714 x
, y
, width
, height
); /* src X, Y, size */
1719 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1720 GLint xoffset
, GLint yoffset
,
1721 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1723 const GLint zoffset
= 0;
1724 st_copy_texsubimage(ctx
, target
, level
,
1725 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1726 x
, y
, width
, height
); /* src X, Y, size */
1731 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1732 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1733 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1735 st_copy_texsubimage(ctx
, target
, level
,
1736 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1737 x
, y
, width
, height
); /* src X, Y, size */
1742 copy_image_data_to_texture(struct st_context
*st
,
1743 struct st_texture_object
*stObj
,
1745 struct st_texture_image
*stImage
)
1748 /* Copy potentially with the blitter:
1750 st_texture_image_copy(st
->pipe
,
1751 stObj
->pt
, dstLevel
, /* dest texture, level */
1752 stImage
->pt
, /* src texture */
1755 pipe_resource_reference(&stImage
->pt
, NULL
);
1757 else if (stImage
->base
.Data
) {
1758 /* More straightforward upload.
1760 st_teximage_flush_before_map(st
, stObj
->pt
, stImage
->face
, dstLevel
,
1761 PIPE_TRANSFER_WRITE
);
1763 st_texture_image_data(st
,
1768 stImage
->base
.RowStride
*
1769 util_format_get_blocksize(stObj
->pt
->format
),
1770 stImage
->base
.RowStride
*
1771 stImage
->base
.Height
*
1772 util_format_get_blocksize(stObj
->pt
->format
));
1773 _mesa_align_free(stImage
->base
.Data
);
1774 stImage
->base
.Data
= NULL
;
1777 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1782 * Called during state validation. When this function is finished,
1783 * the texture object should be ready for rendering.
1784 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1787 st_finalize_texture(GLcontext
*ctx
,
1788 struct pipe_context
*pipe
,
1789 struct gl_texture_object
*tObj
,
1790 GLboolean
*needFlush
)
1792 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1793 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1794 GLuint blockSize
, face
;
1795 struct st_texture_image
*firstImage
;
1797 *needFlush
= GL_FALSE
;
1799 if (stObj
->base
._Complete
) {
1800 /* The texture is complete and we know exactly how many mipmap levels
1801 * are present/needed. This is conditional because we may be called
1802 * from the st_generate_mipmap() function when the texture object is
1803 * incomplete. In that case, we'll have set stObj->lastLevel before
1806 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1807 stObj
->base
.MinFilter
== GL_NEAREST
)
1808 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1810 stObj
->lastLevel
= stObj
->base
._MaxLevel
- stObj
->base
.BaseLevel
;
1813 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1815 /* If both firstImage and stObj point to a texture which can contain
1816 * all active images, favour firstImage. Note that because of the
1817 * completeness requirement, we know that the image dimensions
1820 if (firstImage
->pt
&&
1821 firstImage
->pt
!= stObj
->pt
&&
1822 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1823 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1824 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1827 /* bytes per pixel block (blocks are usually 1x1) */
1828 blockSize
= _mesa_get_format_bytes(firstImage
->base
.TexFormat
);
1830 /* If we already have a gallium texture, check that it matches the texture
1831 * object's format, target, size, num_levels, etc.
1834 const enum pipe_format fmt
=
1835 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1836 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1837 stObj
->pt
->format
!= fmt
||
1838 stObj
->pt
->last_level
< stObj
->lastLevel
||
1839 stObj
->pt
->width0
!= firstImage
->base
.Width2
||
1840 stObj
->pt
->height0
!= firstImage
->base
.Height2
||
1841 stObj
->pt
->depth0
!= firstImage
->base
.Depth2
)
1843 pipe_resource_reference(&stObj
->pt
, NULL
);
1844 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1845 ctx
->st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1849 /* May need to create a new gallium texture:
1852 const enum pipe_format fmt
=
1853 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1854 GLuint usage
= default_usage(fmt
);
1856 stObj
->pt
= st_texture_create(ctx
->st
,
1857 gl_target_to_pipe(stObj
->base
.Target
),
1860 firstImage
->base
.Width2
,
1861 firstImage
->base
.Height2
,
1862 firstImage
->base
.Depth2
,
1866 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1871 /* Pull in any images not in the object's texture:
1873 for (face
= 0; face
< nr_faces
; face
++) {
1875 for (level
= 0; level
<= stObj
->lastLevel
; level
++) {
1876 struct st_texture_image
*stImage
=
1877 st_texture_image(stObj
->base
.Image
[face
][stObj
->base
.BaseLevel
+ level
]);
1879 /* Need to import images in main memory or held in other textures.
1881 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1882 copy_image_data_to_texture(ctx
->st
, stObj
, level
, stImage
);
1883 *needFlush
= GL_TRUE
;
1893 * Returns pointer to a default/dummy texture.
1894 * This is typically used when the current shader has tex/sample instructions
1895 * but the user has not provided a (any) texture(s).
1897 struct gl_texture_object
*
1898 st_get_default_texture(struct st_context
*st
)
1900 if (!st
->default_texture
) {
1901 static const GLenum target
= GL_TEXTURE_2D
;
1902 GLubyte pixels
[16][16][4];
1903 struct gl_texture_object
*texObj
;
1904 struct gl_texture_image
*texImg
;
1907 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1908 * when attempting to sample incomplete textures.
1910 for (i
= 0; i
< 16; i
++) {
1911 for (j
= 0; j
< 16; j
++) {
1912 pixels
[i
][j
][0] = 0;
1913 pixels
[i
][j
][1] = 0;
1914 pixels
[i
][j
][2] = 0;
1915 pixels
[i
][j
][3] = 255;
1919 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1921 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1923 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1924 16, 16, 1, 0, /* w, h, d, border */
1927 st_TexImage(st
->ctx
, 2, target
,
1928 0, GL_RGBA
, /* level, intformat */
1929 16, 16, 1, 0, /* w, h, d, border */
1930 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1931 &st
->ctx
->DefaultPacking
,
1935 texObj
->MinFilter
= GL_NEAREST
;
1936 texObj
->MagFilter
= GL_NEAREST
;
1937 texObj
->_Complete
= GL_TRUE
;
1939 st
->default_texture
= texObj
;
1941 return st
->default_texture
;
1946 st_init_texture_functions(struct dd_function_table
*functions
)
1948 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1949 functions
->TexImage1D
= st_TexImage1D
;
1950 functions
->TexImage2D
= st_TexImage2D
;
1951 functions
->TexImage3D
= st_TexImage3D
;
1952 functions
->TexSubImage1D
= st_TexSubImage1D
;
1953 functions
->TexSubImage2D
= st_TexSubImage2D
;
1954 functions
->TexSubImage3D
= st_TexSubImage3D
;
1955 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1956 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1957 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1958 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1959 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1960 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1961 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1962 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1963 functions
->GenerateMipmap
= st_generate_mipmap
;
1965 functions
->GetTexImage
= st_GetTexImage
;
1967 /* compressed texture functions */
1968 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1969 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1971 functions
->NewTextureObject
= st_NewTextureObject
;
1972 functions
->NewTextureImage
= st_NewTextureImage
;
1973 functions
->DeleteTexture
= st_DeleteTextureObject
;
1974 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1975 functions
->UpdateTexturePalette
= 0;
1977 functions
->TextureMemCpy
= do_memcpy
;
1979 /* XXX Temporary until we can query pipe's texture sizes */
1980 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;