1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "main/mfeatures.h"
29 #include "main/bufferobj.h"
30 #include "main/enums.h"
31 #include "main/fbobject.h"
32 #include "main/formats.h"
33 #include "main/image.h"
34 #include "main/imports.h"
35 #include "main/macros.h"
36 #include "main/mipmap.h"
37 #include "main/pack.h"
38 #include "main/pixeltransfer.h"
39 #include "main/texcompress.h"
40 #include "main/texfetch.h"
41 #include "main/texgetimage.h"
42 #include "main/teximage.h"
43 #include "main/texobj.h"
44 #include "main/texstore.h"
46 #include "state_tracker/st_debug.h"
47 #include "state_tracker/st_context.h"
48 #include "state_tracker/st_cb_fbo.h"
49 #include "state_tracker/st_cb_flush.h"
50 #include "state_tracker/st_cb_texture.h"
51 #include "state_tracker/st_format.h"
52 #include "state_tracker/st_texture.h"
53 #include "state_tracker/st_gen_mipmap.h"
54 #include "state_tracker/st_atom.h"
56 #include "pipe/p_context.h"
57 #include "pipe/p_defines.h"
58 #include "util/u_inlines.h"
59 #include "pipe/p_shader_tokens.h"
60 #include "util/u_tile.h"
61 #include "util/u_blit.h"
62 #include "util/u_format.h"
63 #include "util/u_surface.h"
64 #include "util/u_sampler.h"
65 #include "util/u_math.h"
66 #include "util/u_box.h"
68 #define DBG if (0) printf
71 static enum pipe_texture_target
72 gl_target_to_pipe(GLenum target
)
76 return PIPE_TEXTURE_1D
;
78 return PIPE_TEXTURE_2D
;
79 case GL_TEXTURE_RECTANGLE_NV
:
80 return PIPE_TEXTURE_RECT
;
82 return PIPE_TEXTURE_3D
;
83 case GL_TEXTURE_CUBE_MAP_ARB
:
84 return PIPE_TEXTURE_CUBE
;
85 case GL_TEXTURE_1D_ARRAY_EXT
:
86 return PIPE_TEXTURE_1D_ARRAY
;
87 case GL_TEXTURE_2D_ARRAY_EXT
:
88 return PIPE_TEXTURE_2D_ARRAY
;
96 /** called via ctx->Driver.NewTextureImage() */
97 static struct gl_texture_image
*
98 st_NewTextureImage(struct gl_context
* 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(struct gl_context
* 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.DeleteTextureObject() */
120 st_DeleteTextureObject(struct gl_context
*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(struct gl_context
* 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
, 0, bindings
, geom
))
230 return PIPE_BIND_SAMPLER_VIEW
;
234 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
236 get_texture_dims(GLenum target
)
240 case GL_TEXTURE_1D_ARRAY_EXT
:
243 case GL_TEXTURE_CUBE_MAP_ARB
:
244 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
245 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
246 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
247 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
248 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
249 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
250 case GL_TEXTURE_RECTANGLE_NV
:
251 case GL_TEXTURE_2D_ARRAY_EXT
:
256 assert(0 && "invalid texture target in get_texture_dims()");
263 * Try to allocate a pipe_resource object for the given st_texture_object.
265 * We use the given st_texture_image as a clue to determine the size of the
266 * mipmap image at level=0.
268 * \return GL_TRUE for success, GL_FALSE if out of memory.
271 guess_and_alloc_texture(struct st_context
*st
,
272 struct st_texture_object
*stObj
,
273 const struct st_texture_image
*stImage
)
275 const GLuint dims
= get_texture_dims(stObj
->base
.Target
);
276 GLuint level
, lastLevel
, width
, height
, depth
;
278 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
279 enum pipe_format fmt
;
281 DBG("%s\n", __FUNCTION__
);
285 level
= stImage
->level
;
286 width
= stImage
->base
.Width2
; /* size w/out border */
287 height
= stImage
->base
.Height2
;
288 depth
= stImage
->base
.Depth2
;
294 /* Depending on the image's size, we can't always make a guess here.
297 if ( (dims
>= 1 && width
== 1) ||
298 (dims
>= 2 && height
== 1) ||
299 (dims
>= 3 && depth
== 1) ) {
300 /* we can't determine the image size at level=0 */
301 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
302 /* this is not an out of memory error */
307 /* grow the image size until we hit level = 0 */
320 /* At this point, (width x height x depth) is the expected size of
321 * the level=0 mipmap image.
324 /* Guess a reasonable value for lastLevel. With OpenGL we have no
325 * idea how many mipmap levels will be in a texture until we start
326 * to render with it. Make an educated guess here but be prepared
327 * to re-allocating a texture buffer with space for more (or fewer)
328 * mipmap levels later.
330 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
331 stObj
->base
.MinFilter
== GL_LINEAR
||
332 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
333 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
334 !stObj
->base
.GenerateMipmap
&&
335 stImage
->level
== 0) {
336 /* only alloc space for a single mipmap level */
340 /* alloc space for a full mipmap */
341 GLuint l2width
= util_logbase2(width
);
342 GLuint l2height
= util_logbase2(height
);
343 GLuint l2depth
= util_logbase2(depth
);
344 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
347 /* Save the level=0 dimensions */
348 stObj
->width0
= width
;
349 stObj
->height0
= height
;
350 stObj
->depth0
= depth
;
352 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
354 bindings
= default_bindings(st
, fmt
);
356 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
357 width
, height
, depth
,
358 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
360 stObj
->pt
= st_texture_create(st
,
361 gl_target_to_pipe(stObj
->base
.Target
),
370 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
372 return stObj
->pt
!= NULL
;
377 * Adjust pixel unpack params and image dimensions to strip off the
379 * Gallium doesn't support texture borders. They've seldem been used
380 * and seldom been implemented correctly anyway.
381 * \param unpackNew returns the new pixel unpack parameters
384 strip_texture_border(GLint border
,
385 GLint
*width
, GLint
*height
, GLint
*depth
,
386 const struct gl_pixelstore_attrib
*unpack
,
387 struct gl_pixelstore_attrib
*unpackNew
)
389 assert(border
> 0); /* sanity check */
391 *unpackNew
= *unpack
;
393 if (unpackNew
->RowLength
== 0)
394 unpackNew
->RowLength
= *width
;
396 if (depth
&& unpackNew
->ImageHeight
== 0)
397 unpackNew
->ImageHeight
= *height
;
399 unpackNew
->SkipPixels
+= border
;
401 unpackNew
->SkipRows
+= border
;
403 unpackNew
->SkipImages
+= border
;
406 *width
= *width
- 2 * border
;
407 if (height
&& *height
>= 3)
408 *height
= *height
- 2 * border
;
409 if (depth
&& *depth
>= 3)
410 *depth
= *depth
- 2 * border
;
415 * Do glTexImage1/2/3D().
418 st_TexImage(struct gl_context
* ctx
,
420 GLenum target
, GLint level
,
421 GLint internalFormat
,
422 GLint width
, GLint height
, GLint depth
,
424 GLenum format
, GLenum type
, const void *pixels
,
425 const struct gl_pixelstore_attrib
*unpack
,
426 struct gl_texture_object
*texObj
,
427 struct gl_texture_image
*texImage
,
428 GLsizei imageSize
, GLboolean compressed_src
)
430 struct st_context
*st
= st_context(ctx
);
431 struct pipe_screen
*screen
= st
->pipe
->screen
;
432 struct st_texture_object
*stObj
= st_texture_object(texObj
);
433 struct st_texture_image
*stImage
= st_texture_image(texImage
);
434 GLuint dstRowStride
= 0;
435 struct gl_pixelstore_attrib unpackNB
;
436 enum pipe_transfer_usage transfer_usage
= 0;
438 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
439 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
441 /* switch to "normal" */
442 if (stObj
->surface_based
) {
445 _mesa_clear_texture_object(ctx
, texObj
);
446 pipe_resource_reference(&stObj
->pt
, NULL
);
448 /* oops, need to init this image again */
449 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
450 internalFormat
, format
, type
);
452 _mesa_init_teximage_fields(ctx
, target
, texImage
,
453 width
, height
, depth
, border
,
454 internalFormat
, texFormat
);
456 stObj
->surface_based
= GL_FALSE
;
459 /* gallium does not support texture borders, strip it off */
461 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
463 texImage
->Width
= width
;
464 texImage
->Height
= height
;
465 texImage
->Depth
= depth
;
466 texImage
->Border
= 0;
470 assert(texImage
->Width
== width
);
471 assert(texImage
->Height
== height
);
472 assert(texImage
->Depth
== depth
);
475 stImage
->face
= _mesa_tex_target_to_face(target
);
476 stImage
->level
= level
;
478 _mesa_set_fetch_functions(texImage
, dims
);
480 /* Release the reference to a potentially orphaned buffer.
481 * Release any old malloced memory.
484 pipe_resource_reference(&stImage
->pt
, NULL
);
485 assert(!texImage
->Data
);
487 else if (texImage
->Data
) {
488 _mesa_align_free(texImage
->Data
);
492 * See if the new image is somehow incompatible with the existing
493 * mipmap. If so, free the old mipmap.
496 if (level
> (GLint
) stObj
->pt
->last_level
||
497 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
498 stImage
->face
, stImage
->level
)) {
500 pipe_resource_reference(&stObj
->pt
, NULL
);
502 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
506 if (width
== 0 || height
== 0 || depth
== 0) {
507 /* stop after freeing old image */
512 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
513 /* Probably out of memory.
514 * Try flushing any pending rendering, then retry.
517 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
518 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
524 assert(!stImage
->pt
);
526 /* Check if this texture image can live inside the texture object's buffer.
527 * If so, store the image there. Otherwise the image will temporarily live
531 st_texture_match_image(stObj
->pt
, &stImage
->base
,
532 stImage
->face
, stImage
->level
)) {
534 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
539 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
541 /* Pixel data may come from regular user memory or a PBO. For the later,
542 * do bounds checking and map the PBO to read pixels data from it.
544 * XXX we should try to use a GPU-accelerated path to copy the image data
545 * from the PBO to the texture.
547 if (compressed_src
) {
548 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
550 "glCompressedTexImage");
553 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
555 pixels
, unpack
, "glTexImage");
559 * Prepare to store the texture data. Either map the gallium texture buffer
560 * memory or malloc space for it.
563 /* Store the image in the gallium texture memory buffer */
564 if (format
== GL_DEPTH_COMPONENT
&&
565 util_format_is_depth_and_stencil(stImage
->pt
->format
))
566 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
568 transfer_usage
= PIPE_TRANSFER_WRITE
;
570 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
571 transfer_usage
, 0, 0, width
, height
);
572 if(stImage
->transfer
)
573 dstRowStride
= stImage
->transfer
->stride
;
576 /* Allocate regular memory and store the image there temporarily. */
577 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
578 width
, height
, depth
);
579 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
581 texImage
->Data
= _mesa_align_malloc(imageSize
, 16);
584 if (!texImage
->Data
) {
585 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
590 /* We've allocated texture memory, but have no pixel data - all done. */
594 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
595 width
, height
, depth
, width
, dstRowStride
);
597 /* Copy user texture image into the texture buffer.
599 if (compressed_src
) {
600 const GLuint srcRowStride
=
601 _mesa_format_row_stride(texImage
->TexFormat
, width
);
602 if (dstRowStride
== srcRowStride
) {
603 memcpy(texImage
->Data
, pixels
, imageSize
);
606 char *dst
= texImage
->Data
;
607 const char *src
= pixels
;
608 GLuint i
, bw
, bh
, lines
;
609 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
610 lines
= (height
+ bh
- 1) / bh
;
612 for (i
= 0; i
< lines
; ++i
) {
613 memcpy(dst
, src
, srcRowStride
);
620 const GLuint srcImageStride
=
621 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
623 const GLubyte
*src
= (const GLubyte
*) pixels
;
625 for (i
= 0; i
< depth
; i
++) {
626 if (!_mesa_texstore(ctx
, dims
,
627 texImage
->_BaseFormat
,
630 0, 0, 0, /* dstX/Y/Zoffset */
632 texImage
->ImageOffsets
,
634 format
, type
, src
, unpack
)) {
635 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
638 if (stImage
->pt
&& i
+ 1 < depth
) {
639 /* unmap this slice */
640 st_texture_image_unmap(st
, stImage
);
641 /* map next slice of 3D texture */
642 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
643 transfer_usage
, 0, 0,
645 src
+= srcImageStride
;
651 _mesa_unmap_teximage_pbo(ctx
, unpack
);
653 if (stImage
->pt
&& texImage
->Data
) {
654 st_texture_image_unmap(st
, stImage
);
655 texImage
->Data
= NULL
;
661 st_TexImage3D(struct gl_context
* ctx
,
662 GLenum target
, GLint level
,
663 GLint internalFormat
,
664 GLint width
, GLint height
, GLint depth
,
666 GLenum format
, GLenum type
, const void *pixels
,
667 const struct gl_pixelstore_attrib
*unpack
,
668 struct gl_texture_object
*texObj
,
669 struct gl_texture_image
*texImage
)
671 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
672 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
678 st_TexImage2D(struct gl_context
* ctx
,
679 GLenum target
, GLint level
,
680 GLint internalFormat
,
681 GLint width
, GLint height
, GLint border
,
682 GLenum format
, GLenum type
, const void *pixels
,
683 const struct gl_pixelstore_attrib
*unpack
,
684 struct gl_texture_object
*texObj
,
685 struct gl_texture_image
*texImage
)
687 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
688 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
693 st_TexImage1D(struct gl_context
* ctx
,
694 GLenum target
, GLint level
,
695 GLint internalFormat
,
696 GLint width
, GLint border
,
697 GLenum format
, GLenum type
, const void *pixels
,
698 const struct gl_pixelstore_attrib
*unpack
,
699 struct gl_texture_object
*texObj
,
700 struct gl_texture_image
*texImage
)
702 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
703 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
708 st_CompressedTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
709 GLint internalFormat
,
710 GLint width
, GLint height
, GLint border
,
711 GLsizei imageSize
, const GLvoid
*data
,
712 struct gl_texture_object
*texObj
,
713 struct gl_texture_image
*texImage
)
715 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
716 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
722 * glGetTexImage() helper: decompress a compressed texture by rendering
723 * a textured quad. Store the results in the user's buffer.
726 decompress_with_blit(struct gl_context
* ctx
, GLenum target
, GLint level
,
727 GLenum format
, GLenum type
, GLvoid
*pixels
,
728 struct gl_texture_object
*texObj
,
729 struct gl_texture_image
*texImage
)
731 struct st_context
*st
= st_context(ctx
);
732 struct pipe_context
*pipe
= st
->pipe
;
733 struct st_texture_image
*stImage
= st_texture_image(texImage
);
734 struct st_texture_object
*stObj
= st_texture_object(texObj
);
735 struct pipe_sampler_view
*src_view
=
736 st_get_texture_sampler_view(stObj
, pipe
);
737 const GLuint width
= texImage
->Width
;
738 const GLuint height
= texImage
->Height
;
739 struct pipe_surface
*dst_surface
;
740 struct pipe_resource
*dst_texture
;
741 struct pipe_transfer
*tex_xfer
;
742 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
743 PIPE_BIND_TRANSFER_READ
);
745 /* create temp / dest surface */
746 if (!util_create_rgba_surface(pipe
, width
, height
, bind
,
747 &dst_texture
, &dst_surface
)) {
748 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
749 "in decompress_with_blit()");
753 /* blit/render/decompress */
754 util_blit_pixels_tex(st
->blit
,
755 src_view
, /* pipe_resource (src) */
756 0, 0, /* src x0, y0 */
757 width
, height
, /* src x1, y1 */
758 dst_surface
, /* pipe_surface (dst) */
759 0, 0, /* dst x0, y0 */
760 width
, height
, /* dst x1, y1 */
762 PIPE_TEX_MIPFILTER_NEAREST
);
764 /* map the dst_surface so we can read from it */
765 tex_xfer
= pipe_get_transfer(st_context(ctx
)->pipe
,
768 0, 0, width
, height
);
770 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
772 /* copy/pack data into user buffer */
773 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
775 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
776 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
778 for (row
= 0; row
< height
; row
++) {
779 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
780 height
, format
, type
, row
, 0);
781 memcpy(dest
, map
, bytesPerRow
);
782 map
+= tex_xfer
->stride
;
784 pipe_transfer_unmap(pipe
, tex_xfer
);
787 /* format translation via floats */
789 enum pipe_format format
= util_format_linear(dst_texture
->format
);
790 for (row
= 0; row
< height
; row
++) {
791 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
792 GLfloat rgba
[4 * MAX_WIDTH
];
793 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
794 height
, format
, type
, row
, 0);
796 if (ST_DEBUG
& DEBUG_FALLBACK
)
797 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
799 /* get float[4] rgba row from surface */
800 pipe_get_tile_rgba_format(pipe
, tex_xfer
, 0, row
, width
, 1,
803 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
804 type
, dest
, &ctx
->Pack
, transferOps
);
808 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
810 pipe
->transfer_destroy(pipe
, tex_xfer
);
812 /* destroy the temp / dest surface */
813 util_destroy_rgba_surface(dst_texture
, dst_surface
);
819 * Need to map texture image into memory before copying image data,
823 st_get_tex_image(struct gl_context
* ctx
, GLenum target
, GLint level
,
824 GLenum format
, GLenum type
, GLvoid
* pixels
,
825 struct gl_texture_object
*texObj
,
826 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
828 struct st_context
*st
= st_context(ctx
);
829 struct st_texture_image
*stImage
= st_texture_image(texImage
);
830 const GLuint dstImageStride
=
831 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
837 util_format_is_s3tc(stImage
->pt
->format
) &&
839 /* Need to decompress the texture.
840 * We'll do this by rendering a textured quad.
841 * Note that we only expect RGBA formats (no Z/depth formats).
843 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
850 /* Image is stored in hardware format in a buffer managed by the
851 * kernel. Need to explicitly map and unmap it.
853 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
854 PIPE_TRANSFER_READ
, 0, 0,
856 stImage
->base
.Height
);
857 /* compute stride in texels from stride in bytes */
858 texImage
->RowStride
= stImage
->transfer
->stride
859 * util_format_get_blockwidth(stImage
->pt
->format
)
860 / util_format_get_blocksize(stImage
->pt
->format
);
863 /* Otherwise, the image should actually be stored in
864 * texImage->Data. This is pretty confusing for
865 * everybody, I'd much prefer to separate the two functions of
866 * texImage->Data - storage for texture images in main memory
867 * and access (ie mappings) of images. In other words, we'd
868 * create a new texImage->Map field and leave Data simply for
871 assert(texImage
->Data
);
874 depth
= texImage
->Depth
;
877 dest
= (GLubyte
*) pixels
;
879 _mesa_set_fetch_functions(texImage
, get_texture_dims(target
));
881 for (i
= 0; i
< depth
; i
++) {
882 if (compressed_dst
) {
883 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
887 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
891 if (stImage
->pt
&& i
+ 1 < depth
) {
892 /* unmap this slice */
893 st_texture_image_unmap(st
, stImage
);
894 /* map next slice of 3D texture */
895 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
896 PIPE_TRANSFER_READ
, 0, 0,
898 stImage
->base
.Height
);
899 dest
+= dstImageStride
;
903 texImage
->Depth
= depth
;
907 st_texture_image_unmap(st
, stImage
);
908 texImage
->Data
= NULL
;
914 st_GetTexImage(struct gl_context
* ctx
, GLenum target
, GLint level
,
915 GLenum format
, GLenum type
, GLvoid
* pixels
,
916 struct gl_texture_object
*texObj
,
917 struct gl_texture_image
*texImage
)
919 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
925 st_GetCompressedTexImage(struct gl_context
*ctx
, GLenum target
, GLint level
,
927 struct gl_texture_object
*texObj
,
928 struct gl_texture_image
*texImage
)
930 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
937 st_TexSubimage(struct gl_context
*ctx
, GLint dims
, GLenum target
, GLint level
,
938 GLint xoffset
, GLint yoffset
, GLint zoffset
,
939 GLint width
, GLint height
, GLint depth
,
940 GLenum format
, GLenum type
, const void *pixels
,
941 const struct gl_pixelstore_attrib
*packing
,
942 struct gl_texture_object
*texObj
,
943 struct gl_texture_image
*texImage
)
945 struct st_context
*st
= st_context(ctx
);
946 struct pipe_screen
*screen
= st
->pipe
->screen
;
947 struct st_texture_image
*stImage
= st_texture_image(texImage
);
949 const GLuint srcImageStride
=
950 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
953 /* init to silence warning only: */
954 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
956 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
957 _mesa_lookup_enum_by_nr(target
),
958 level
, xoffset
, yoffset
, width
, height
);
961 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
962 type
, pixels
, packing
, "glTexSubImage2D");
966 /* Map buffer if necessary. Need to lock to prevent other contexts
967 * from uploading the buffer under us.
970 if (format
== GL_DEPTH_COMPONENT
&&
971 util_format_is_depth_and_stencil(stImage
->pt
->format
))
972 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
974 transfer_usage
= PIPE_TRANSFER_WRITE
;
976 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
982 if (!texImage
->Data
) {
983 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
987 src
= (const GLubyte
*) pixels
;
988 dstRowStride
= stImage
->transfer
->stride
;
990 for (i
= 0; i
< depth
; i
++) {
991 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
996 texImage
->ImageOffsets
,
998 format
, type
, src
, packing
)) {
999 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1002 if (stImage
->pt
&& i
+ 1 < depth
) {
1003 /* unmap this slice */
1004 st_texture_image_unmap(st
, stImage
);
1005 /* map next slice of 3D texture */
1006 texImage
->Data
= st_texture_image_map(st
, stImage
,
1011 src
+= srcImageStride
;
1016 _mesa_unmap_teximage_pbo(ctx
, packing
);
1018 if (stImage
->pt
&& texImage
->Data
) {
1019 st_texture_image_unmap(st
, stImage
);
1020 texImage
->Data
= NULL
;
1027 st_TexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1028 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1029 GLsizei width
, GLsizei height
, GLsizei depth
,
1030 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1031 const struct gl_pixelstore_attrib
*packing
,
1032 struct gl_texture_object
*texObj
,
1033 struct gl_texture_image
*texImage
)
1035 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1036 width
, height
, depth
, format
, type
,
1037 pixels
, packing
, texObj
, texImage
);
1042 st_TexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1043 GLint xoffset
, GLint yoffset
,
1044 GLsizei width
, GLsizei height
,
1045 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1046 const struct gl_pixelstore_attrib
*packing
,
1047 struct gl_texture_object
*texObj
,
1048 struct gl_texture_image
*texImage
)
1050 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1051 width
, height
, 1, format
, type
,
1052 pixels
, packing
, texObj
, texImage
);
1057 st_TexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1058 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1059 const GLvoid
* pixels
,
1060 const struct gl_pixelstore_attrib
*packing
,
1061 struct gl_texture_object
*texObj
,
1062 struct gl_texture_image
*texImage
)
1064 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1065 format
, type
, pixels
, packing
, texObj
, texImage
);
1070 st_CompressedTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1071 GLint xoffset
, GLsizei width
,
1073 GLsizei imageSize
, const GLvoid
*data
,
1074 struct gl_texture_object
*texObj
,
1075 struct gl_texture_image
*texImage
)
1082 st_CompressedTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1083 GLint xoffset
, GLint yoffset
,
1084 GLsizei width
, GLint height
,
1086 GLsizei imageSize
, const GLvoid
*data
,
1087 struct gl_texture_object
*texObj
,
1088 struct gl_texture_image
*texImage
)
1090 struct st_context
*st
= st_context(ctx
);
1091 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1095 enum pipe_format pformat
;
1098 pformat
= stImage
->pt
->format
;
1100 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1101 PIPE_TRANSFER_WRITE
,
1105 srcBlockStride
= util_format_get_stride(pformat
, width
);
1106 dstBlockStride
= stImage
->transfer
->stride
;
1108 assert(stImage
->pt
);
1109 /* TODO find good values for block and strides */
1110 /* TODO also adjust texImage->data for yoffset/xoffset */
1114 if (!texImage
->Data
) {
1115 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1119 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1120 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1122 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1123 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1124 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1125 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1126 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1130 st_texture_image_unmap(st
, stImage
);
1131 texImage
->Data
= NULL
;
1137 st_CompressedTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1138 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1139 GLsizei width
, GLint height
, GLint depth
,
1141 GLsizei imageSize
, const GLvoid
*data
,
1142 struct gl_texture_object
*texObj
,
1143 struct gl_texture_image
*texImage
)
1151 * Do a CopyTexSubImage operation using a read transfer from the source,
1152 * a write transfer to the destination and get_tile()/put_tile() to access
1153 * the pixels/texels.
1155 * Note: srcY=0=TOP of renderbuffer
1158 fallback_copy_texsubimage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1159 struct st_renderbuffer
*strb
,
1160 struct st_texture_image
*stImage
,
1162 GLint destX
, GLint destY
, GLint destZ
,
1163 GLint srcX
, GLint srcY
,
1164 GLsizei width
, GLsizei height
)
1166 struct st_context
*st
= st_context(ctx
);
1167 struct pipe_context
*pipe
= st
->pipe
;
1168 struct pipe_transfer
*src_trans
;
1170 enum pipe_transfer_usage transfer_usage
;
1172 if (ST_DEBUG
& DEBUG_FALLBACK
)
1173 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1175 assert(width
<= MAX_WIDTH
);
1177 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1178 srcY
= strb
->Base
.Height
- srcY
- height
;
1181 src_trans
= pipe_get_transfer(st_context(ctx
)->pipe
,
1188 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1189 baseFormat
== GL_DEPTH_STENCIL
) &&
1190 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1191 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1193 transfer_usage
= PIPE_TRANSFER_WRITE
;
1195 /* XXX this used to ignore destZ param */
1196 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
1197 destX
, destY
, width
, height
);
1199 if (baseFormat
== GL_DEPTH_COMPONENT
||
1200 baseFormat
== GL_DEPTH_STENCIL
) {
1201 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1202 ctx
->Pixel
.DepthBias
!= 0.0F
);
1205 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1206 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1215 /* To avoid a large temp memory allocation, do copy row by row */
1216 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1217 uint data
[MAX_WIDTH
];
1218 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1220 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1222 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1228 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1230 if (tempSrc
&& texDest
) {
1231 const GLint dims
= 2;
1232 const GLint dstRowStride
= stImage
->transfer
->stride
;
1233 struct gl_texture_image
*texImage
= &stImage
->base
;
1234 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1236 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1237 unpack
.Invert
= GL_TRUE
;
1240 /* get float/RGBA image from framebuffer */
1241 /* XXX this usually involves a lot of int/float conversion.
1242 * try to avoid that someday.
1244 pipe_get_tile_rgba_format(pipe
, src_trans
, 0, 0, width
, height
,
1245 util_format_linear(strb
->texture
->format
),
1248 /* Store into texture memory.
1249 * Note that this does some special things such as pixel transfer
1250 * ops and format conversion. In particular, if the dest tex format
1251 * is actually RGBA but the user created the texture as GL_RGB we
1252 * need to fill-in/override the alpha channel with 1.0.
1254 _mesa_texstore(ctx
, dims
,
1255 texImage
->_BaseFormat
,
1256 texImage
->TexFormat
,
1260 texImage
->ImageOffsets
,
1262 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1266 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1273 st_texture_image_unmap(st
, stImage
);
1274 pipe
->transfer_destroy(pipe
, src_trans
);
1280 * If the format of the src renderbuffer and the format of the dest
1281 * texture are compatible (in terms of blitting), return a TGSI writemask
1282 * to be used during the blit.
1283 * If the src/dest are incompatible, return 0.
1286 compatible_src_dst_formats(struct gl_context
*ctx
,
1287 const struct gl_renderbuffer
*src
,
1288 const struct gl_texture_image
*dst
)
1290 /* Get logical base formats for the src and dest.
1291 * That is, use the user-requested formats and not the actual, device-
1293 * For example, the user may have requested an A8 texture but the
1294 * driver may actually be using an RGBA texture format. When we
1295 * copy/blit to that texture, we only want to copy the Alpha channel
1296 * and not the RGB channels.
1298 * Similarly, when the src FBO was created an RGB format may have been
1299 * requested but the driver actually chose an RGBA format. In that case,
1300 * we don't want to copy the undefined Alpha channel to the dest texture
1301 * (it should be 1.0).
1303 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1304 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1307 * XXX when we have red-only and red/green renderbuffers we'll need
1308 * to add more cases here (or implement a general-purpose routine that
1309 * queries the existance of the R,G,B,A channels in the src and dest).
1311 if (srcFormat
== dstFormat
) {
1312 /* This is the same as matching_base_formats, which should
1313 * always pass, as it did previously.
1315 return TGSI_WRITEMASK_XYZW
;
1317 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1318 /* Make sure that A in the dest is 1. The actual src format
1319 * may be RGBA and have undefined A values.
1321 return TGSI_WRITEMASK_XYZ
;
1323 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1324 /* Make sure that A in the dest is 1. The actual dst format
1325 * may be RGBA and will need A=1 to provide proper alpha values
1326 * when sampled later.
1328 return TGSI_WRITEMASK_XYZ
;
1331 if (ST_DEBUG
& DEBUG_FALLBACK
)
1332 debug_printf("%s failed for src %s, dst %s\n",
1334 _mesa_lookup_enum_by_nr(srcFormat
),
1335 _mesa_lookup_enum_by_nr(dstFormat
));
1346 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1347 * Note that the region to copy has already been clipped so we know we
1348 * won't read from outside the source renderbuffer's bounds.
1350 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1353 st_copy_texsubimage(struct gl_context
*ctx
,
1354 GLenum target
, GLint level
,
1355 GLint destX
, GLint destY
, GLint destZ
,
1356 GLint srcX
, GLint srcY
,
1357 GLsizei width
, GLsizei height
)
1359 struct gl_texture_unit
*texUnit
=
1360 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1361 struct gl_texture_object
*texObj
=
1362 _mesa_select_tex_object(ctx
, texUnit
, target
);
1363 struct gl_texture_image
*texImage
=
1364 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1365 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1366 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1367 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1368 struct st_renderbuffer
*strb
;
1369 struct st_context
*st
= st_context(ctx
);
1370 struct pipe_context
*pipe
= st
->pipe
;
1371 struct pipe_screen
*screen
= pipe
->screen
;
1372 enum pipe_format dest_format
, src_format
;
1373 GLboolean use_fallback
= GL_TRUE
;
1374 GLboolean matching_base_formats
;
1375 GLuint format_writemask
, sample_count
;
1376 struct pipe_surface
*dest_surface
= NULL
;
1377 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1379 /* make sure finalize_textures has been called?
1381 if (0) st_validate_state(st
);
1383 /* determine if copying depth or color data */
1384 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1385 texBaseFormat
== GL_DEPTH_STENCIL
) {
1386 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1387 if (strb
->Base
.Wrapped
) {
1388 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
1392 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1393 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1396 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1397 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1401 sample_count
= strb
->surface
->texture
->nr_samples
;
1402 /* I believe this would be legal, presumably would need to do a resolve
1403 for color, and for depth/stencil spec says to just use one of the
1404 depth/stencil samples per pixel? Need some transfer clarifications. */
1405 assert(sample_count
< 2);
1431 if (width
< 0 || height
< 0)
1436 assert(strb
->surface
);
1437 assert(stImage
->pt
);
1439 src_format
= strb
->surface
->format
;
1440 dest_format
= stImage
->pt
->format
;
1443 * Determine if the src framebuffer and dest texture have the same
1444 * base format. We need this to detect a case such as the framebuffer
1445 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1446 * texture format stores RGBA we need to set A=1 (overriding the
1447 * framebuffer's alpha values). We can't do that with the blit or
1448 * textured-quad paths.
1450 matching_base_formats
=
1451 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1452 _mesa_get_format_base_format(texImage
->TexFormat
));
1453 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1455 if (ctx
->_ImageTransferState
== 0x0) {
1457 if (matching_base_formats
&&
1458 src_format
== dest_format
&&
1461 /* use surface_copy() / blit */
1462 struct pipe_box src_box
;
1463 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
1464 width
, height
, &src_box
);
1466 /* for resource_copy_region(), y=0=top, always */
1467 pipe
->resource_copy_region(pipe
,
1471 destX
, destY
, destZ
+ stImage
->face
,
1474 strb
->surface
->u
.tex
.level
,
1476 use_fallback
= GL_FALSE
;
1478 else if (format_writemask
&&
1479 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1480 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1481 screen
->is_format_supported(screen
, src_format
,
1482 PIPE_TEXTURE_2D
, sample_count
,
1483 PIPE_BIND_SAMPLER_VIEW
,
1485 screen
->is_format_supported(screen
, dest_format
,
1487 PIPE_BIND_RENDER_TARGET
,
1489 /* draw textured quad to do the copy */
1491 struct pipe_surface surf_tmpl
;
1492 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1493 surf_tmpl
.format
= stImage
->pt
->format
;
1494 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
1495 surf_tmpl
.u
.tex
.level
= stImage
->level
;
1496 surf_tmpl
.u
.tex
.first_layer
= stImage
->face
+ destZ
;
1497 surf_tmpl
.u
.tex
.last_layer
= stImage
->face
+ destZ
;
1499 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1503 srcY1
= strb
->Base
.Height
- srcY
- height
;
1504 srcY0
= srcY1
+ height
;
1508 srcY1
= srcY0
+ height
;
1511 util_blit_pixels_writemask(st
->blit
,
1513 strb
->surface
->u
.tex
.level
,
1515 srcX
+ width
, srcY1
,
1516 strb
->surface
->u
.tex
.first_layer
,
1519 destX
+ width
, destY
+ height
,
1520 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1522 use_fallback
= GL_FALSE
;
1526 pipe_surface_reference(&dest_surface
, NULL
);
1530 /* software fallback */
1531 fallback_copy_texsubimage(ctx
, target
, level
,
1532 strb
, stImage
, texBaseFormat
,
1533 destX
, destY
, destZ
,
1534 srcX
, srcY
, width
, height
);
1541 st_CopyTexImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1542 GLenum internalFormat
,
1543 GLint x
, GLint y
, GLsizei width
, GLint border
)
1545 struct gl_texture_unit
*texUnit
=
1546 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1547 struct gl_texture_object
*texObj
=
1548 _mesa_select_tex_object(ctx
, texUnit
, target
);
1549 struct gl_texture_image
*texImage
=
1550 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1552 /* Setup or redefine the texture object, texture and texture
1553 * image. Don't populate yet.
1555 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1557 GL_RGBA
, CHAN_TYPE
, NULL
,
1558 &ctx
->DefaultPacking
, texObj
, texImage
);
1560 st_copy_texsubimage(ctx
, target
, level
,
1561 0, 0, 0, /* destX,Y,Z */
1562 x
, y
, width
, 1); /* src X, Y, size */
1567 st_CopyTexImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1568 GLenum internalFormat
,
1569 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1572 struct gl_texture_unit
*texUnit
=
1573 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1574 struct gl_texture_object
*texObj
=
1575 _mesa_select_tex_object(ctx
, texUnit
, target
);
1576 struct gl_texture_image
*texImage
=
1577 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1579 /* Setup or redefine the texture object, texture and texture
1580 * image. Don't populate yet.
1582 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1583 width
, height
, border
,
1584 GL_RGBA
, CHAN_TYPE
, NULL
,
1585 &ctx
->DefaultPacking
, texObj
, texImage
);
1587 st_copy_texsubimage(ctx
, target
, level
,
1588 0, 0, 0, /* destX,Y,Z */
1589 x
, y
, width
, height
); /* src X, Y, size */
1594 st_CopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1595 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1597 const GLint yoffset
= 0, zoffset
= 0;
1598 const GLsizei height
= 1;
1599 st_copy_texsubimage(ctx
, target
, level
,
1600 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1601 x
, y
, width
, height
); /* src X, Y, size */
1606 st_CopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1607 GLint xoffset
, GLint yoffset
,
1608 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1610 const GLint zoffset
= 0;
1611 st_copy_texsubimage(ctx
, target
, level
,
1612 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1613 x
, y
, width
, height
); /* src X, Y, size */
1618 st_CopyTexSubImage3D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1619 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1620 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1622 st_copy_texsubimage(ctx
, target
, level
,
1623 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1624 x
, y
, width
, height
); /* src X, Y, size */
1629 * Copy image data from stImage into the texture object 'stObj' at level
1633 copy_image_data_to_texture(struct st_context
*st
,
1634 struct st_texture_object
*stObj
,
1636 struct st_texture_image
*stImage
)
1640 const struct gl_texture_image
*dstImage
=
1641 stObj
->base
.Image
[stImage
->face
][stImage
->level
];
1643 assert(dstImage
->Width
== stImage
->base
.Width
);
1644 assert(dstImage
->Height
== stImage
->base
.Height
);
1645 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1649 /* Copy potentially with the blitter:
1651 st_texture_image_copy(st
->pipe
,
1652 stObj
->pt
, dstLevel
, /* dest texture, level */
1653 stImage
->pt
, stImage
->level
, /* src texture, level */
1656 pipe_resource_reference(&stImage
->pt
, NULL
);
1658 else if (stImage
->base
.Data
) {
1659 st_texture_image_data(st
,
1664 stImage
->base
.RowStride
*
1665 util_format_get_blocksize(stObj
->pt
->format
),
1666 stImage
->base
.RowStride
*
1667 stImage
->base
.Height
*
1668 util_format_get_blocksize(stObj
->pt
->format
));
1669 _mesa_align_free(stImage
->base
.Data
);
1670 stImage
->base
.Data
= NULL
;
1673 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1678 * Called during state validation. When this function is finished,
1679 * the texture object should be ready for rendering.
1680 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1683 st_finalize_texture(struct gl_context
*ctx
,
1684 struct pipe_context
*pipe
,
1685 struct gl_texture_object
*tObj
)
1687 struct st_context
*st
= st_context(ctx
);
1688 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1689 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1691 struct st_texture_image
*firstImage
;
1692 enum pipe_format firstImageFormat
;
1693 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1695 if (stObj
->base
._Complete
) {
1696 /* The texture is complete and we know exactly how many mipmap levels
1697 * are present/needed. This is conditional because we may be called
1698 * from the st_generate_mipmap() function when the texture object is
1699 * incomplete. In that case, we'll have set stObj->lastLevel before
1702 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1703 stObj
->base
.MinFilter
== GL_NEAREST
)
1704 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1706 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1709 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1712 /* If both firstImage and stObj point to a texture which can contain
1713 * all active images, favour firstImage. Note that because of the
1714 * completeness requirement, we know that the image dimensions
1717 if (firstImage
->pt
&&
1718 firstImage
->pt
!= stObj
->pt
&&
1719 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1720 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1721 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1724 /* Find gallium format for the Mesa texture */
1725 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1726 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, stObj
->width0
,
1727 stObj
->height0
, stObj
->depth0
,
1728 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1730 /* If we already have a gallium texture, check that it matches the texture
1731 * object's format, target, size, num_levels, etc.
1734 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1735 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1736 stObj
->pt
->last_level
< stObj
->lastLevel
||
1737 stObj
->pt
->width0
!= ptWidth
||
1738 stObj
->pt
->height0
!= ptHeight
||
1739 stObj
->pt
->depth0
!= ptDepth
||
1740 stObj
->pt
->array_size
!= ptLayers
)
1742 /* The gallium texture does not match the Mesa texture so delete the
1743 * gallium texture now. We'll make a new one below.
1745 pipe_resource_reference(&stObj
->pt
, NULL
);
1746 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1747 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1751 /* May need to create a new gallium texture:
1754 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1756 stObj
->pt
= st_texture_create(st
,
1757 gl_target_to_pipe(stObj
->base
.Target
),
1767 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1772 /* Pull in any images not in the object's texture:
1774 for (face
= 0; face
< nr_faces
; face
++) {
1776 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1777 struct st_texture_image
*stImage
=
1778 st_texture_image(stObj
->base
.Image
[face
][level
]);
1780 /* Need to import images in main memory or held in other textures.
1782 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1783 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1793 * Returns pointer to a default/dummy texture.
1794 * This is typically used when the current shader has tex/sample instructions
1795 * but the user has not provided a (any) texture(s).
1797 struct gl_texture_object
*
1798 st_get_default_texture(struct st_context
*st
)
1800 if (!st
->default_texture
) {
1801 static const GLenum target
= GL_TEXTURE_2D
;
1802 GLubyte pixels
[16][16][4];
1803 struct gl_texture_object
*texObj
;
1804 struct gl_texture_image
*texImg
;
1807 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1808 * when attempting to sample incomplete textures.
1810 for (i
= 0; i
< 16; i
++) {
1811 for (j
= 0; j
< 16; j
++) {
1812 pixels
[i
][j
][0] = 0;
1813 pixels
[i
][j
][1] = 0;
1814 pixels
[i
][j
][2] = 0;
1815 pixels
[i
][j
][3] = 255;
1819 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1821 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1823 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1824 16, 16, 1, 0, /* w, h, d, border */
1825 GL_RGBA
, MESA_FORMAT_RGBA8888
);
1827 st_TexImage(st
->ctx
, 2, target
,
1828 0, GL_RGBA
, /* level, intformat */
1829 16, 16, 1, 0, /* w, h, d, border */
1830 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1831 &st
->ctx
->DefaultPacking
,
1835 texObj
->MinFilter
= GL_NEAREST
;
1836 texObj
->MagFilter
= GL_NEAREST
;
1837 texObj
->_Complete
= GL_TRUE
;
1839 st
->default_texture
= texObj
;
1841 return st
->default_texture
;
1846 st_init_texture_functions(struct dd_function_table
*functions
)
1848 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1849 functions
->TexImage1D
= st_TexImage1D
;
1850 functions
->TexImage2D
= st_TexImage2D
;
1851 functions
->TexImage3D
= st_TexImage3D
;
1852 functions
->TexSubImage1D
= st_TexSubImage1D
;
1853 functions
->TexSubImage2D
= st_TexSubImage2D
;
1854 functions
->TexSubImage3D
= st_TexSubImage3D
;
1855 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1856 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1857 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1858 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1859 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1860 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1861 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1862 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1863 functions
->GenerateMipmap
= st_generate_mipmap
;
1865 functions
->GetTexImage
= st_GetTexImage
;
1867 /* compressed texture functions */
1868 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1869 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1871 functions
->NewTextureObject
= st_NewTextureObject
;
1872 functions
->NewTextureImage
= st_NewTextureImage
;
1873 functions
->DeleteTexture
= st_DeleteTextureObject
;
1874 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1876 functions
->TextureMemCpy
= do_memcpy
;
1878 /* XXX Temporary until we can query pipe's texture sizes */
1879 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;