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/texcompress.h"
38 #include "main/texfetch.h"
39 #include "main/texgetimage.h"
40 #include "main/teximage.h"
41 #include "main/texobj.h"
42 #include "main/texstore.h"
44 #include "state_tracker/st_debug.h"
45 #include "state_tracker/st_context.h"
46 #include "state_tracker/st_cb_fbo.h"
47 #include "state_tracker/st_cb_flush.h"
48 #include "state_tracker/st_cb_texture.h"
49 #include "state_tracker/st_format.h"
50 #include "state_tracker/st_texture.h"
51 #include "state_tracker/st_gen_mipmap.h"
52 #include "state_tracker/st_atom.h"
54 #include "pipe/p_context.h"
55 #include "pipe/p_defines.h"
56 #include "util/u_inlines.h"
57 #include "pipe/p_shader_tokens.h"
58 #include "util/u_tile.h"
59 #include "util/u_blit.h"
60 #include "util/u_format.h"
61 #include "util/u_surface.h"
62 #include "util/u_sampler.h"
63 #include "util/u_math.h"
66 #define DBG if (0) printf
69 static enum pipe_texture_target
70 gl_target_to_pipe(GLenum target
)
74 return PIPE_TEXTURE_1D
;
77 return PIPE_TEXTURE_2D
;
79 case GL_TEXTURE_RECTANGLE_NV
:
80 return PIPE_TEXTURE_RECT
;
83 return PIPE_TEXTURE_3D
;
85 case GL_TEXTURE_CUBE_MAP_ARB
:
86 return PIPE_TEXTURE_CUBE
;
95 /** called via ctx->Driver.NewTextureImage() */
96 static struct gl_texture_image
*
97 st_NewTextureImage(GLcontext
* ctx
)
99 DBG("%s\n", __FUNCTION__
);
101 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
105 /** called via ctx->Driver.NewTextureObject() */
106 static struct gl_texture_object
*
107 st_NewTextureObject(GLcontext
* ctx
, GLuint name
, GLenum target
)
109 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
111 DBG("%s\n", __FUNCTION__
);
112 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
117 /** called via ctx->Driver.DeleteTextureObject() */
119 st_DeleteTextureObject(GLcontext
*ctx
,
120 struct gl_texture_object
*texObj
)
122 struct st_context
*st
= st_context(ctx
);
123 struct st_texture_object
*stObj
= st_texture_object(texObj
);
125 pipe_resource_reference(&stObj
->pt
, NULL
);
126 if (stObj
->sampler_view
) {
127 if (stObj
->sampler_view
->context
!= st
->pipe
) {
128 /* Take "ownership" of this texture sampler view by setting
129 * its context pointer to this context. This avoids potential
130 * crashes when the texture object is shared among contexts
131 * and the original/owner context has already been destroyed.
133 stObj
->sampler_view
->context
= st
->pipe
;
135 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
137 _mesa_delete_texture_object(ctx
, texObj
);
141 /** called via ctx->Driver.FreeTexImageData() */
143 st_FreeTextureImageData(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
145 struct st_texture_image
*stImage
= st_texture_image(texImage
);
147 DBG("%s\n", __FUNCTION__
);
150 pipe_resource_reference(&stImage
->pt
, NULL
);
153 if (texImage
->Data
) {
154 _mesa_align_free(texImage
->Data
);
155 texImage
->Data
= NULL
;
161 * From linux kernel i386 header files, copes with odd sizes better
162 * than COPY_DWORDS would:
163 * XXX Put this in src/mesa/main/imports.h ???
165 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
167 __memcpy(void *to
, const void *from
, size_t n
)
170 __asm__
__volatile__("rep ; movsl\n\t"
174 "1:\ttestb $1,%b4\n\t"
176 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
177 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
182 #define __memcpy(a,b,c) memcpy(a,b,c)
187 * The system memcpy (at least on ubuntu 5.10) has problems copying
188 * to agp (writecombined) memory from a source which isn't 64-byte
189 * aligned - there is a 4x performance falloff.
191 * The x86 __memcpy is immune to this but is slightly slower
192 * (10%-ish) than the system memcpy.
194 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
195 * isn't much faster than x86_memcpy for agp copies.
197 * TODO: switch dynamically.
200 do_memcpy(void *dest
, const void *src
, size_t n
)
202 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
203 return __memcpy(dest
, src
, n
);
206 return memcpy(dest
, src
, n
);
211 * Return default texture resource binding bitmask for the given format.
214 default_bindings(struct st_context
*st
, enum pipe_format format
)
216 struct pipe_screen
*screen
= st
->pipe
->screen
;
217 const unsigned target
= PIPE_TEXTURE_2D
;
218 const unsigned geom
= 0x0;
221 if (util_format_is_depth_or_stencil(format
))
222 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
224 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
226 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
, geom
))
229 return PIPE_BIND_SAMPLER_VIEW
;
233 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
235 get_texture_dims(GLenum target
)
239 case GL_TEXTURE_1D_ARRAY_EXT
:
242 case GL_TEXTURE_CUBE_MAP_ARB
:
243 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
244 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
245 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
246 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
247 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
248 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
249 case GL_TEXTURE_RECTANGLE_NV
:
250 case GL_TEXTURE_2D_ARRAY_EXT
:
255 assert(0 && "invalid texture target in get_texture_dims()");
262 * Try to allocate a pipe_resource object for the given st_texture_object.
264 * We use the given st_texture_image as a clue to determine the size of the
265 * mipmap image at level=0.
267 * \return GL_TRUE for success, GL_FALSE if out of memory.
270 guess_and_alloc_texture(struct st_context
*st
,
271 struct st_texture_object
*stObj
,
272 const struct st_texture_image
*stImage
)
274 const GLuint dims
= get_texture_dims(stObj
->base
.Target
);
275 GLuint level
, lastLevel
, width
, height
, depth
;
277 enum pipe_format fmt
;
279 DBG("%s\n", __FUNCTION__
);
283 level
= stImage
->level
;
284 width
= stImage
->base
.Width2
; /* size w/out border */
285 height
= stImage
->base
.Height2
;
286 depth
= stImage
->base
.Depth2
;
292 /* Depending on the image's size, we can't always make a guess here.
295 if ( (dims
>= 1 && width
== 1) ||
296 (dims
>= 2 && height
== 1) ||
297 (dims
>= 3 && depth
== 1) ) {
298 /* we can't determine the image size at level=0 */
299 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
300 /* this is not an out of memory error */
305 /* grow the image size until we hit level = 0 */
318 /* At this point, (width x height x depth) is the expected size of
319 * the level=0 mipmap image.
322 /* Guess a reasonable value for lastLevel. With OpenGL we have no
323 * idea how many mipmap levels will be in a texture until we start
324 * to render with it. Make an educated guess here but be prepared
325 * to re-allocating a texture buffer with space for more (or fewer)
326 * mipmap levels later.
328 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
329 stObj
->base
.MinFilter
== GL_LINEAR
||
330 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
331 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
332 !stObj
->base
.GenerateMipmap
&&
333 stImage
->level
== 0) {
334 /* only alloc space for a single mipmap level */
338 /* alloc space for a full mipmap */
339 GLuint l2width
= util_logbase2(width
);
340 GLuint l2height
= util_logbase2(height
);
341 GLuint l2depth
= util_logbase2(depth
);
342 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
345 /* Save the level=0 dimensions */
346 stObj
->width0
= width
;
347 stObj
->height0
= height
;
348 stObj
->depth0
= depth
;
350 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
352 bindings
= default_bindings(st
, fmt
);
354 stObj
->pt
= st_texture_create(st
,
355 gl_target_to_pipe(stObj
->base
.Target
),
363 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
365 return stObj
->pt
!= NULL
;
370 * Adjust pixel unpack params and image dimensions to strip off the
372 * Gallium doesn't support texture borders. They've seldem been used
373 * and seldom been implemented correctly anyway.
374 * \param unpackNew returns the new pixel unpack parameters
377 strip_texture_border(GLint border
,
378 GLint
*width
, GLint
*height
, GLint
*depth
,
379 const struct gl_pixelstore_attrib
*unpack
,
380 struct gl_pixelstore_attrib
*unpackNew
)
382 assert(border
> 0); /* sanity check */
384 *unpackNew
= *unpack
;
386 if (unpackNew
->RowLength
== 0)
387 unpackNew
->RowLength
= *width
;
389 if (depth
&& unpackNew
->ImageHeight
== 0)
390 unpackNew
->ImageHeight
= *height
;
392 unpackNew
->SkipPixels
+= border
;
394 unpackNew
->SkipRows
+= border
;
396 unpackNew
->SkipImages
+= border
;
399 *width
= *width
- 2 * border
;
400 if (height
&& *height
>= 3)
401 *height
= *height
- 2 * border
;
402 if (depth
&& *depth
>= 3)
403 *depth
= *depth
- 2 * border
;
408 * Try to do texture compression via rendering. If the Gallium driver
409 * can render into a compressed surface this will allow us to do texture
411 * \return GL_TRUE for success, GL_FALSE for failure
414 compress_with_blit(GLcontext
* ctx
,
415 GLenum target
, GLint level
,
416 GLint xoffset
, GLint yoffset
, GLint zoffset
,
417 GLint width
, GLint height
, GLint depth
,
418 GLenum format
, GLenum type
, const void *pixels
,
419 const struct gl_pixelstore_attrib
*unpack
,
420 struct gl_texture_image
*texImage
)
422 const GLuint dstImageOffsets
[1] = {0};
423 struct st_texture_image
*stImage
= st_texture_image(texImage
);
424 struct st_context
*st
= st_context(ctx
);
425 struct pipe_context
*pipe
= st
->pipe
;
426 struct pipe_screen
*screen
= pipe
->screen
;
427 gl_format mesa_format
;
428 struct pipe_resource templ
;
429 struct pipe_resource
*src_tex
;
430 struct pipe_sampler_view view_templ
;
431 struct pipe_sampler_view
*src_view
;
432 struct pipe_surface
*dst_surface
;
433 struct pipe_transfer
*tex_xfer
;
437 /* XXX: Can this happen? Should we assert? */
441 /* get destination surface (in the compressed texture) */
442 dst_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
443 stImage
->face
, stImage
->level
, 0,
446 /* can't render into this format (or other problem) */
450 /* Choose format for the temporary RGBA texture image.
452 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
457 /* Create the temporary source texture
459 memset(&templ
, 0, sizeof(templ
));
460 templ
.target
= st
->internal_target
;
461 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
);
462 templ
.width0
= width
;
463 templ
.height0
= height
;
465 templ
.last_level
= 0;
466 templ
.usage
= PIPE_USAGE_DEFAULT
;
467 templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
468 src_tex
= screen
->resource_create(screen
, &templ
);
473 /* Put user's tex data into the temporary texture
475 tex_xfer
= pipe_get_transfer(st_context(ctx
)->pipe
, src_tex
,
476 0, 0, 0, /* face, level are zero */
478 0, 0, width
, height
); /* x, y, w, h */
479 map
= pipe_transfer_map(pipe
, tex_xfer
);
481 _mesa_texstore(ctx
, 2, GL_RGBA
, mesa_format
,
483 0, 0, 0, /* dest x/y/z offset */
484 tex_xfer
->stride
, /* dest row stride (bytes) */
485 dstImageOffsets
, /* image offsets (for 3D only) */
486 width
, height
, 1, /* size */
487 format
, type
, /* source format/type */
488 pixels
, /* source data */
489 unpack
); /* source data packing */
491 pipe_transfer_unmap(pipe
, tex_xfer
);
492 pipe
->transfer_destroy(pipe
, tex_xfer
);
494 /* Create temporary sampler view */
495 u_sampler_view_default_template(&view_templ
,
498 src_view
= pipe
->create_sampler_view(pipe
, src_tex
, &view_templ
);
501 /* copy / compress image */
502 util_blit_pixels_tex(st
->blit
,
503 src_view
, /* sampler view (src) */
504 0, 0, /* src x0, y0 */
505 width
, height
, /* src x1, y1 */
506 dst_surface
, /* pipe_surface (dst) */
507 xoffset
, yoffset
, /* dst x0, y0 */
508 xoffset
+ width
, /* dst x1 */
509 yoffset
+ height
, /* dst y1 */
511 PIPE_TEX_MIPFILTER_NEAREST
);
513 pipe_surface_reference(&dst_surface
, NULL
);
514 pipe_resource_reference(&src_tex
, NULL
);
515 pipe_sampler_view_reference(&src_view
, NULL
);
522 * Do glTexImage1/2/3D().
525 st_TexImage(GLcontext
* ctx
,
527 GLenum target
, GLint level
,
528 GLint internalFormat
,
529 GLint width
, GLint height
, GLint depth
,
531 GLenum format
, GLenum type
, const void *pixels
,
532 const struct gl_pixelstore_attrib
*unpack
,
533 struct gl_texture_object
*texObj
,
534 struct gl_texture_image
*texImage
,
535 GLsizei imageSize
, GLboolean compressed_src
)
537 struct st_context
*st
= st_context(ctx
);
538 struct pipe_screen
*screen
= st
->pipe
->screen
;
539 struct st_texture_object
*stObj
= st_texture_object(texObj
);
540 struct st_texture_image
*stImage
= st_texture_image(texImage
);
541 GLuint dstRowStride
= 0;
542 struct gl_pixelstore_attrib unpackNB
;
543 enum pipe_transfer_usage transfer_usage
= 0;
545 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
546 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
548 /* The Mesa/Gallium state tracker does not implement the imaging extensions
549 * such as convolution.
551 assert(!ctx
->Extensions
.EXT_convolution
);
553 /* switch to "normal" */
554 if (stObj
->surface_based
) {
555 _mesa_clear_texture_object(ctx
, texObj
);
556 pipe_resource_reference(&stObj
->pt
, NULL
);
558 /* oops, need to init this image again */
559 _mesa_init_teximage_fields(ctx
, target
, texImage
,
560 width
, height
, depth
, border
, internalFormat
);
561 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
562 internalFormat
, format
, type
);
564 stObj
->surface_based
= GL_FALSE
;
567 /* gallium does not support texture borders, strip it off */
569 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
571 texImage
->Width
= width
;
572 texImage
->Height
= height
;
573 texImage
->Depth
= depth
;
574 texImage
->Border
= 0;
578 assert(texImage
->Width
== width
);
579 assert(texImage
->Height
== height
);
580 assert(texImage
->Depth
== depth
);
583 stImage
->face
= _mesa_tex_target_to_face(target
);
584 stImage
->level
= level
;
586 _mesa_set_fetch_functions(texImage
, dims
);
588 /* Release the reference to a potentially orphaned buffer.
589 * Release any old malloced memory.
592 pipe_resource_reference(&stImage
->pt
, NULL
);
593 assert(!texImage
->Data
);
595 else if (texImage
->Data
) {
596 _mesa_align_free(texImage
->Data
);
600 * See if the new image is somehow incompatible with the existing
601 * mipmap. If so, free the old mipmap.
604 if (level
> (GLint
) stObj
->pt
->last_level
||
605 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
606 stImage
->face
, stImage
->level
)) {
608 pipe_resource_reference(&stObj
->pt
, NULL
);
610 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
614 if (width
== 0 || height
== 0 || depth
== 0) {
615 /* stop after freeing old image */
620 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
621 /* Probably out of memory.
622 * Try flushing any pending rendering, then retry.
625 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
626 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
632 assert(!stImage
->pt
);
634 /* Check if this texture image can live inside the texture object's buffer.
635 * If so, store the image there. Otherwise the image will temporarily live
639 st_texture_match_image(stObj
->pt
, &stImage
->base
,
640 stImage
->face
, stImage
->level
)) {
642 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
647 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
649 /* Pixel data may come from regular user memory or a PBO. For the later,
650 * do bounds checking and map the PBO to read pixels data from it.
652 * XXX we should try to use a GPU-accelerated path to copy the image data
653 * from the PBO to the texture.
655 if (compressed_src
) {
656 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
658 "glCompressedTexImage");
661 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
663 pixels
, unpack
, "glTexImage");
666 /* See if we can do texture compression with a blit/render.
668 if (!compressed_src
&&
670 _mesa_is_format_compressed(texImage
->TexFormat
) &&
671 screen
->is_format_supported(screen
,
673 stImage
->pt
->target
, 0,
674 PIPE_BIND_RENDER_TARGET
, 0)) {
678 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
679 format
, type
, pixels
, unpack
, texImage
)) {
685 * Prepare to store the texture data. Either map the gallium texture buffer
686 * memory or malloc space for it.
689 /* Store the image in the gallium texture memory buffer */
690 if (format
== GL_DEPTH_COMPONENT
&&
691 util_format_is_depth_and_stencil(stImage
->pt
->format
))
692 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
694 transfer_usage
= PIPE_TRANSFER_WRITE
;
696 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
697 transfer_usage
, 0, 0, width
, height
);
698 if(stImage
->transfer
)
699 dstRowStride
= stImage
->transfer
->stride
;
702 /* Allocate regular memory and store the image there temporarily. */
703 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
704 width
, height
, depth
);
705 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
707 texImage
->Data
= _mesa_align_malloc(imageSize
, 16);
710 if (!texImage
->Data
) {
711 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
716 /* We've allocated texture memory, but have no pixel data - all done. */
720 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
721 width
, height
, depth
, width
, dstRowStride
);
723 /* Copy user texture image into the texture buffer.
725 if (compressed_src
) {
726 const GLuint srcRowStride
=
727 _mesa_format_row_stride(texImage
->TexFormat
, width
);
728 if (dstRowStride
== srcRowStride
) {
729 memcpy(texImage
->Data
, pixels
, imageSize
);
732 char *dst
= texImage
->Data
;
733 const char *src
= pixels
;
734 GLuint i
, bw
, bh
, lines
;
735 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
736 lines
= (height
+ bh
- 1) / bh
;
738 for (i
= 0; i
< lines
; ++i
) {
739 memcpy(dst
, src
, srcRowStride
);
746 const GLuint srcImageStride
=
747 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
749 const GLubyte
*src
= (const GLubyte
*) pixels
;
751 for (i
= 0; i
< depth
; i
++) {
752 if (!_mesa_texstore(ctx
, dims
,
753 texImage
->_BaseFormat
,
756 0, 0, 0, /* dstX/Y/Zoffset */
758 texImage
->ImageOffsets
,
760 format
, type
, src
, unpack
)) {
761 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
764 if (stImage
->pt
&& i
+ 1 < depth
) {
765 /* unmap this slice */
766 st_texture_image_unmap(st
, stImage
);
767 /* map next slice of 3D texture */
768 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
769 transfer_usage
, 0, 0,
771 src
+= srcImageStride
;
777 _mesa_unmap_teximage_pbo(ctx
, unpack
);
779 if (stImage
->pt
&& texImage
->Data
) {
780 st_texture_image_unmap(st
, stImage
);
781 texImage
->Data
= NULL
;
787 st_TexImage3D(GLcontext
* ctx
,
788 GLenum target
, GLint level
,
789 GLint internalFormat
,
790 GLint width
, GLint height
, GLint depth
,
792 GLenum format
, GLenum type
, const void *pixels
,
793 const struct gl_pixelstore_attrib
*unpack
,
794 struct gl_texture_object
*texObj
,
795 struct gl_texture_image
*texImage
)
797 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
798 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
804 st_TexImage2D(GLcontext
* ctx
,
805 GLenum target
, GLint level
,
806 GLint internalFormat
,
807 GLint width
, GLint height
, GLint border
,
808 GLenum format
, GLenum type
, const void *pixels
,
809 const struct gl_pixelstore_attrib
*unpack
,
810 struct gl_texture_object
*texObj
,
811 struct gl_texture_image
*texImage
)
813 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
814 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
819 st_TexImage1D(GLcontext
* ctx
,
820 GLenum target
, GLint level
,
821 GLint internalFormat
,
822 GLint width
, GLint border
,
823 GLenum format
, GLenum type
, const void *pixels
,
824 const struct gl_pixelstore_attrib
*unpack
,
825 struct gl_texture_object
*texObj
,
826 struct gl_texture_image
*texImage
)
828 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
829 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
834 st_CompressedTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
835 GLint internalFormat
,
836 GLint width
, GLint height
, GLint border
,
837 GLsizei imageSize
, const GLvoid
*data
,
838 struct gl_texture_object
*texObj
,
839 struct gl_texture_image
*texImage
)
841 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
842 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
848 * glGetTexImage() helper: decompress a compressed texture by rendering
849 * a textured quad. Store the results in the user's buffer.
852 decompress_with_blit(GLcontext
* ctx
, GLenum target
, GLint level
,
853 GLenum format
, GLenum type
, GLvoid
*pixels
,
854 struct gl_texture_object
*texObj
,
855 struct gl_texture_image
*texImage
)
857 struct st_context
*st
= st_context(ctx
);
858 struct pipe_context
*pipe
= st
->pipe
;
859 struct pipe_screen
*screen
= pipe
->screen
;
860 struct st_texture_image
*stImage
= st_texture_image(texImage
);
861 struct st_texture_object
*stObj
= st_texture_object(texObj
);
862 struct pipe_sampler_view
*src_view
=
863 st_get_texture_sampler_view(stObj
, pipe
);
864 const GLuint width
= texImage
->Width
;
865 const GLuint height
= texImage
->Height
;
866 struct pipe_surface
*dst_surface
;
867 struct pipe_resource
*dst_texture
;
868 struct pipe_transfer
*tex_xfer
;
869 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
870 PIPE_BIND_TRANSFER_READ
);
872 /* create temp / dest surface */
873 if (!util_create_rgba_surface(screen
, width
, height
, bind
,
874 &dst_texture
, &dst_surface
)) {
875 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
876 "in decompress_with_blit()");
880 /* blit/render/decompress */
881 util_blit_pixels_tex(st
->blit
,
882 src_view
, /* pipe_resource (src) */
883 0, 0, /* src x0, y0 */
884 width
, height
, /* src x1, y1 */
885 dst_surface
, /* pipe_surface (dst) */
886 0, 0, /* dst x0, y0 */
887 width
, height
, /* dst x1, y1 */
889 PIPE_TEX_MIPFILTER_NEAREST
);
891 /* map the dst_surface so we can read from it */
892 tex_xfer
= pipe_get_transfer(st_context(ctx
)->pipe
,
893 dst_texture
, 0, 0, 0,
895 0, 0, width
, height
);
897 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
899 /* copy/pack data into user buffer */
900 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
902 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
903 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
905 for (row
= 0; row
< height
; row
++) {
906 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
907 height
, format
, type
, row
, 0);
908 memcpy(dest
, map
, bytesPerRow
);
909 map
+= tex_xfer
->stride
;
911 pipe_transfer_unmap(pipe
, tex_xfer
);
914 /* format translation via floats */
916 for (row
= 0; row
< height
; row
++) {
917 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
918 GLfloat rgba
[4 * MAX_WIDTH
];
919 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
920 height
, format
, type
, row
, 0);
922 if (ST_DEBUG
& DEBUG_FALLBACK
)
923 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
925 /* get float[4] rgba row from surface */
926 pipe_get_tile_rgba(pipe
, tex_xfer
, 0, row
, width
, 1, rgba
);
928 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
929 type
, dest
, &ctx
->Pack
, transferOps
);
933 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
935 pipe
->transfer_destroy(pipe
, tex_xfer
);
937 /* destroy the temp / dest surface */
938 util_destroy_rgba_surface(dst_texture
, dst_surface
);
944 * Need to map texture image into memory before copying image data,
948 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
949 GLenum format
, GLenum type
, GLvoid
* pixels
,
950 struct gl_texture_object
*texObj
,
951 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
953 struct st_context
*st
= st_context(ctx
);
954 struct st_texture_image
*stImage
= st_texture_image(texImage
);
955 const GLuint dstImageStride
=
956 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
962 util_format_is_s3tc(stImage
->pt
->format
) &&
964 /* Need to decompress the texture.
965 * We'll do this by rendering a textured quad.
966 * Note that we only expect RGBA formats (no Z/depth formats).
968 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
975 /* Image is stored in hardware format in a buffer managed by the
976 * kernel. Need to explicitly map and unmap it.
978 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
979 PIPE_TRANSFER_READ
, 0, 0,
981 stImage
->base
.Height
);
982 /* compute stride in texels from stride in bytes */
983 texImage
->RowStride
= stImage
->transfer
->stride
984 * util_format_get_blockwidth(stImage
->pt
->format
)
985 / util_format_get_blocksize(stImage
->pt
->format
);
988 /* Otherwise, the image should actually be stored in
989 * texImage->Data. This is pretty confusing for
990 * everybody, I'd much prefer to separate the two functions of
991 * texImage->Data - storage for texture images in main memory
992 * and access (ie mappings) of images. In other words, we'd
993 * create a new texImage->Map field and leave Data simply for
996 assert(texImage
->Data
);
999 depth
= texImage
->Depth
;
1000 texImage
->Depth
= 1;
1002 dest
= (GLubyte
*) pixels
;
1004 _mesa_set_fetch_functions(texImage
, get_texture_dims(target
));
1006 for (i
= 0; i
< depth
; i
++) {
1007 if (compressed_dst
) {
1008 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
1012 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
1016 if (stImage
->pt
&& i
+ 1 < depth
) {
1017 /* unmap this slice */
1018 st_texture_image_unmap(st
, stImage
);
1019 /* map next slice of 3D texture */
1020 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
1021 PIPE_TRANSFER_READ
, 0, 0,
1022 stImage
->base
.Width
,
1023 stImage
->base
.Height
);
1024 dest
+= dstImageStride
;
1028 texImage
->Depth
= depth
;
1032 st_texture_image_unmap(st
, stImage
);
1033 texImage
->Data
= NULL
;
1039 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
1040 GLenum format
, GLenum type
, GLvoid
* pixels
,
1041 struct gl_texture_object
*texObj
,
1042 struct gl_texture_image
*texImage
)
1044 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1050 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
1052 struct gl_texture_object
*texObj
,
1053 struct gl_texture_image
*texImage
)
1055 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1062 st_TexSubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
1063 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1064 GLint width
, GLint height
, GLint depth
,
1065 GLenum format
, GLenum type
, const void *pixels
,
1066 const struct gl_pixelstore_attrib
*packing
,
1067 struct gl_texture_object
*texObj
,
1068 struct gl_texture_image
*texImage
)
1070 struct st_context
*st
= st_context(ctx
);
1071 struct pipe_screen
*screen
= st
->pipe
->screen
;
1072 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1073 GLuint dstRowStride
;
1074 const GLuint srcImageStride
=
1075 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1078 /* init to silence warning only: */
1079 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1081 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1082 _mesa_lookup_enum_by_nr(target
),
1083 level
, xoffset
, yoffset
, width
, height
);
1086 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1087 type
, pixels
, packing
, "glTexSubImage2D");
1091 /* See if we can do texture compression with a blit/render.
1093 if (!ctx
->Mesa_DXTn
&&
1094 _mesa_is_format_compressed(texImage
->TexFormat
) &&
1095 screen
->is_format_supported(screen
,
1096 stImage
->pt
->format
,
1097 stImage
->pt
->target
, 0,
1098 PIPE_BIND_RENDER_TARGET
, 0)) {
1099 if (compress_with_blit(ctx
, target
, level
,
1100 xoffset
, yoffset
, zoffset
,
1101 width
, height
, depth
,
1102 format
, type
, pixels
, packing
, texImage
)) {
1107 /* Map buffer if necessary. Need to lock to prevent other contexts
1108 * from uploading the buffer under us.
1111 if (format
== GL_DEPTH_COMPONENT
&&
1112 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1113 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1115 transfer_usage
= PIPE_TRANSFER_WRITE
;
1117 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1123 if (!texImage
->Data
) {
1124 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1128 src
= (const GLubyte
*) pixels
;
1129 dstRowStride
= stImage
->transfer
->stride
;
1131 for (i
= 0; i
< depth
; i
++) {
1132 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1133 texImage
->TexFormat
,
1137 texImage
->ImageOffsets
,
1139 format
, type
, src
, packing
)) {
1140 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1143 if (stImage
->pt
&& i
+ 1 < depth
) {
1144 /* unmap this slice */
1145 st_texture_image_unmap(st
, stImage
);
1146 /* map next slice of 3D texture */
1147 texImage
->Data
= st_texture_image_map(st
, stImage
,
1152 src
+= srcImageStride
;
1157 _mesa_unmap_teximage_pbo(ctx
, packing
);
1159 if (stImage
->pt
&& texImage
->Data
) {
1160 st_texture_image_unmap(st
, stImage
);
1161 texImage
->Data
= NULL
;
1168 st_TexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1169 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1170 GLsizei width
, GLsizei height
, GLsizei depth
,
1171 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1172 const struct gl_pixelstore_attrib
*packing
,
1173 struct gl_texture_object
*texObj
,
1174 struct gl_texture_image
*texImage
)
1176 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1177 width
, height
, depth
, format
, type
,
1178 pixels
, packing
, texObj
, texImage
);
1183 st_TexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1184 GLint xoffset
, GLint yoffset
,
1185 GLsizei width
, GLsizei height
,
1186 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1187 const struct gl_pixelstore_attrib
*packing
,
1188 struct gl_texture_object
*texObj
,
1189 struct gl_texture_image
*texImage
)
1191 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1192 width
, height
, 1, format
, type
,
1193 pixels
, packing
, texObj
, texImage
);
1198 st_TexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1199 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1200 const GLvoid
* pixels
,
1201 const struct gl_pixelstore_attrib
*packing
,
1202 struct gl_texture_object
*texObj
,
1203 struct gl_texture_image
*texImage
)
1205 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1206 format
, type
, pixels
, packing
, texObj
, texImage
);
1211 st_CompressedTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1212 GLint xoffset
, GLsizei width
,
1214 GLsizei imageSize
, const GLvoid
*data
,
1215 struct gl_texture_object
*texObj
,
1216 struct gl_texture_image
*texImage
)
1223 st_CompressedTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1224 GLint xoffset
, GLint yoffset
,
1225 GLsizei width
, GLint height
,
1227 GLsizei imageSize
, const GLvoid
*data
,
1228 struct gl_texture_object
*texObj
,
1229 struct gl_texture_image
*texImage
)
1231 struct st_context
*st
= st_context(ctx
);
1232 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1236 enum pipe_format pformat
;
1239 pformat
= stImage
->pt
->format
;
1241 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1242 PIPE_TRANSFER_WRITE
,
1246 srcBlockStride
= util_format_get_stride(pformat
, width
);
1247 dstBlockStride
= stImage
->transfer
->stride
;
1249 assert(stImage
->pt
);
1250 /* TODO find good values for block and strides */
1251 /* TODO also adjust texImage->data for yoffset/xoffset */
1255 if (!texImage
->Data
) {
1256 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1260 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1261 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1263 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1264 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1265 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1266 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1267 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1271 st_texture_image_unmap(st
, stImage
);
1272 texImage
->Data
= NULL
;
1278 st_CompressedTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1279 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1280 GLsizei width
, GLint height
, GLint depth
,
1282 GLsizei imageSize
, const GLvoid
*data
,
1283 struct gl_texture_object
*texObj
,
1284 struct gl_texture_image
*texImage
)
1292 * Do a CopyTexSubImage operation using a read transfer from the source,
1293 * a write transfer to the destination and get_tile()/put_tile() to access
1294 * the pixels/texels.
1296 * Note: srcY=0=TOP of renderbuffer
1299 fallback_copy_texsubimage(GLcontext
*ctx
, GLenum target
, GLint level
,
1300 struct st_renderbuffer
*strb
,
1301 struct st_texture_image
*stImage
,
1303 GLint destX
, GLint destY
, GLint destZ
,
1304 GLint srcX
, GLint srcY
,
1305 GLsizei width
, GLsizei height
)
1307 struct st_context
*st
= st_context(ctx
);
1308 struct pipe_context
*pipe
= st
->pipe
;
1309 struct pipe_transfer
*src_trans
;
1311 enum pipe_transfer_usage transfer_usage
;
1313 if (ST_DEBUG
& DEBUG_FALLBACK
)
1314 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1316 assert(width
<= MAX_WIDTH
);
1318 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1319 srcY
= strb
->Base
.Height
- srcY
- height
;
1322 src_trans
= pipe_get_transfer(st_context(ctx
)->pipe
,
1329 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1330 baseFormat
== GL_DEPTH_STENCIL
) &&
1331 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1332 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1334 transfer_usage
= PIPE_TRANSFER_WRITE
;
1336 texDest
= st_texture_image_map(st
, stImage
, 0, transfer_usage
,
1337 destX
, destY
, width
, height
);
1339 if (baseFormat
== GL_DEPTH_COMPONENT
||
1340 baseFormat
== GL_DEPTH_STENCIL
) {
1341 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1342 ctx
->Pixel
.DepthBias
!= 0.0F
);
1345 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1346 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1355 /* To avoid a large temp memory allocation, do copy row by row */
1356 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1357 uint data
[MAX_WIDTH
];
1358 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1360 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1362 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1368 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1370 if (tempSrc
&& texDest
) {
1371 const GLint dims
= 2;
1372 const GLint dstRowStride
= stImage
->transfer
->stride
;
1373 struct gl_texture_image
*texImage
= &stImage
->base
;
1374 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1376 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1377 unpack
.Invert
= GL_TRUE
;
1380 /* get float/RGBA image from framebuffer */
1381 /* XXX this usually involves a lot of int/float conversion.
1382 * try to avoid that someday.
1384 pipe_get_tile_rgba(pipe
, src_trans
, 0, 0, width
, height
, tempSrc
);
1386 /* Store into texture memory.
1387 * Note that this does some special things such as pixel transfer
1388 * ops and format conversion. In particular, if the dest tex format
1389 * is actually RGBA but the user created the texture as GL_RGB we
1390 * need to fill-in/override the alpha channel with 1.0.
1392 _mesa_texstore(ctx
, dims
,
1393 texImage
->_BaseFormat
,
1394 texImage
->TexFormat
,
1398 texImage
->ImageOffsets
,
1400 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1404 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1411 st_texture_image_unmap(st
, stImage
);
1412 pipe
->transfer_destroy(pipe
, src_trans
);
1418 * If the format of the src renderbuffer and the format of the dest
1419 * texture are compatible (in terms of blitting), return a TGSI writemask
1420 * to be used during the blit.
1421 * If the src/dest are incompatible, return 0.
1424 compatible_src_dst_formats(GLcontext
*ctx
,
1425 const struct gl_renderbuffer
*src
,
1426 const struct gl_texture_image
*dst
)
1428 /* Get logical base formats for the src and dest.
1429 * That is, use the user-requested formats and not the actual, device-
1431 * For example, the user may have requested an A8 texture but the
1432 * driver may actually be using an RGBA texture format. When we
1433 * copy/blit to that texture, we only want to copy the Alpha channel
1434 * and not the RGB channels.
1436 * Similarly, when the src FBO was created an RGB format may have been
1437 * requested but the driver actually chose an RGBA format. In that case,
1438 * we don't want to copy the undefined Alpha channel to the dest texture
1439 * (it should be 1.0).
1441 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1442 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1445 * XXX when we have red-only and red/green renderbuffers we'll need
1446 * to add more cases here (or implement a general-purpose routine that
1447 * queries the existance of the R,G,B,A channels in the src and dest).
1449 if (srcFormat
== dstFormat
) {
1450 /* This is the same as matching_base_formats, which should
1451 * always pass, as it did previously.
1453 return TGSI_WRITEMASK_XYZW
;
1455 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1456 /* Make sure that A in the dest is 1. The actual src format
1457 * may be RGBA and have undefined A values.
1459 return TGSI_WRITEMASK_XYZ
;
1461 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1462 /* Make sure that A in the dest is 1. The actual dst format
1463 * may be RGBA and will need A=1 to provide proper alpha values
1464 * when sampled later.
1466 return TGSI_WRITEMASK_XYZ
;
1469 if (ST_DEBUG
& DEBUG_FALLBACK
)
1470 debug_printf("%s failed for src %s, dst %s\n",
1472 _mesa_lookup_enum_by_nr(srcFormat
),
1473 _mesa_lookup_enum_by_nr(dstFormat
));
1484 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1485 * Note that the region to copy has already been clipped so we know we
1486 * won't read from outside the source renderbuffer's bounds.
1488 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1491 st_copy_texsubimage(GLcontext
*ctx
,
1492 GLenum target
, GLint level
,
1493 GLint destX
, GLint destY
, GLint destZ
,
1494 GLint srcX
, GLint srcY
,
1495 GLsizei width
, GLsizei height
)
1497 struct gl_texture_unit
*texUnit
=
1498 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1499 struct gl_texture_object
*texObj
=
1500 _mesa_select_tex_object(ctx
, texUnit
, target
);
1501 struct gl_texture_image
*texImage
=
1502 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1503 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1504 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1505 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1506 struct st_renderbuffer
*strb
;
1507 struct st_context
*st
= st_context(ctx
);
1508 struct pipe_context
*pipe
= st
->pipe
;
1509 struct pipe_screen
*screen
= pipe
->screen
;
1510 enum pipe_format dest_format
, src_format
;
1511 GLboolean use_fallback
= GL_TRUE
;
1512 GLboolean matching_base_formats
;
1513 GLuint format_writemask
, sample_count
;
1514 struct pipe_surface
*dest_surface
= NULL
;
1515 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1517 /* make sure finalize_textures has been called?
1519 if (0) st_validate_state(st
);
1521 /* determine if copying depth or color data */
1522 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1523 texBaseFormat
== GL_DEPTH_STENCIL
) {
1524 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1527 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1528 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1531 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1532 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1536 sample_count
= strb
->surface
->texture
->nr_samples
;
1537 /* I believe this would be legal, presumably would need to do a resolve
1538 for color, and for depth/stencil spec says to just use one of the
1539 depth/stencil samples per pixel? Need some transfer clarifications. */
1540 assert(sample_count
< 2);
1566 if (width
< 0 || height
< 0)
1571 assert(strb
->surface
);
1572 assert(stImage
->pt
);
1574 src_format
= strb
->surface
->format
;
1575 dest_format
= stImage
->pt
->format
;
1578 * Determine if the src framebuffer and dest texture have the same
1579 * base format. We need this to detect a case such as the framebuffer
1580 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1581 * texture format stores RGBA we need to set A=1 (overriding the
1582 * framebuffer's alpha values). We can't do that with the blit or
1583 * textured-quad paths.
1585 matching_base_formats
=
1586 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1587 _mesa_get_format_base_format(texImage
->TexFormat
));
1588 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1590 if (ctx
->_ImageTransferState
== 0x0) {
1592 if (matching_base_formats
&&
1593 src_format
== dest_format
&&
1596 /* use surface_copy() / blit */
1597 struct pipe_subresource subdst
, subsrc
;
1598 subdst
.face
= stImage
->face
;
1599 subdst
.level
= stImage
->level
;
1600 subsrc
.face
= strb
->surface
->face
;
1601 subsrc
.level
= strb
->surface
->level
;
1603 /* for resource_copy_region(), y=0=top, always */
1604 pipe
->resource_copy_region(pipe
,
1608 destX
, destY
, destZ
,
1612 srcX
, srcY
, strb
->surface
->zslice
,
1615 use_fallback
= GL_FALSE
;
1617 else if (format_writemask
&&
1618 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1619 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1620 screen
->is_format_supported(screen
, src_format
,
1621 PIPE_TEXTURE_2D
, sample_count
,
1622 PIPE_BIND_SAMPLER_VIEW
,
1624 screen
->is_format_supported(screen
, dest_format
,
1626 PIPE_BIND_RENDER_TARGET
,
1628 /* draw textured quad to do the copy */
1630 struct pipe_subresource subsrc
;
1632 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1633 stImage
->face
, stImage
->level
,
1635 PIPE_BIND_RENDER_TARGET
);
1638 srcY1
= strb
->Base
.Height
- srcY
- height
;
1639 srcY0
= srcY1
+ height
;
1643 srcY1
= srcY0
+ height
;
1645 subsrc
.face
= strb
->surface
->face
;
1646 subsrc
.level
= strb
->surface
->level
;
1648 util_blit_pixels_writemask(st
->blit
,
1652 srcX
+ width
, srcY1
,
1653 strb
->surface
->zslice
,
1656 destX
+ width
, destY
+ height
,
1657 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1659 use_fallback
= GL_FALSE
;
1663 pipe_surface_reference(&dest_surface
, NULL
);
1667 /* software fallback */
1668 fallback_copy_texsubimage(ctx
, target
, level
,
1669 strb
, stImage
, texBaseFormat
,
1670 destX
, destY
, destZ
,
1671 srcX
, srcY
, width
, height
);
1678 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1679 GLenum internalFormat
,
1680 GLint x
, GLint y
, GLsizei width
, GLint border
)
1682 struct gl_texture_unit
*texUnit
=
1683 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1684 struct gl_texture_object
*texObj
=
1685 _mesa_select_tex_object(ctx
, texUnit
, target
);
1686 struct gl_texture_image
*texImage
=
1687 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1689 /* Setup or redefine the texture object, texture and texture
1690 * image. Don't populate yet.
1692 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1694 GL_RGBA
, CHAN_TYPE
, NULL
,
1695 &ctx
->DefaultPacking
, texObj
, texImage
);
1697 st_copy_texsubimage(ctx
, target
, level
,
1698 0, 0, 0, /* destX,Y,Z */
1699 x
, y
, width
, 1); /* src X, Y, size */
1704 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1705 GLenum internalFormat
,
1706 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1709 struct gl_texture_unit
*texUnit
=
1710 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1711 struct gl_texture_object
*texObj
=
1712 _mesa_select_tex_object(ctx
, texUnit
, target
);
1713 struct gl_texture_image
*texImage
=
1714 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1716 /* Setup or redefine the texture object, texture and texture
1717 * image. Don't populate yet.
1719 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1720 width
, height
, border
,
1721 GL_RGBA
, CHAN_TYPE
, NULL
,
1722 &ctx
->DefaultPacking
, texObj
, texImage
);
1724 st_copy_texsubimage(ctx
, target
, level
,
1725 0, 0, 0, /* destX,Y,Z */
1726 x
, y
, width
, height
); /* src X, Y, size */
1731 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1732 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1734 const GLint yoffset
= 0, zoffset
= 0;
1735 const GLsizei height
= 1;
1736 st_copy_texsubimage(ctx
, target
, level
,
1737 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1738 x
, y
, width
, height
); /* src X, Y, size */
1743 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1744 GLint xoffset
, GLint yoffset
,
1745 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1747 const GLint zoffset
= 0;
1748 st_copy_texsubimage(ctx
, target
, level
,
1749 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1750 x
, y
, width
, height
); /* src X, Y, size */
1755 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1756 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1757 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1759 st_copy_texsubimage(ctx
, target
, level
,
1760 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1761 x
, y
, width
, height
); /* src X, Y, size */
1766 * Copy image data from stImage into the texture object 'stObj' at level
1770 copy_image_data_to_texture(struct st_context
*st
,
1771 struct st_texture_object
*stObj
,
1773 struct st_texture_image
*stImage
)
1777 const struct gl_texture_image
*dstImage
=
1778 stObj
->base
.Image
[stImage
->face
][stImage
->level
];
1780 assert(dstImage
->Width
== stImage
->base
.Width
);
1781 assert(dstImage
->Height
== stImage
->base
.Height
);
1782 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1786 /* Copy potentially with the blitter:
1788 st_texture_image_copy(st
->pipe
,
1789 stObj
->pt
, dstLevel
, /* dest texture, level */
1790 stImage
->pt
, stImage
->level
, /* src texture, level */
1793 pipe_resource_reference(&stImage
->pt
, NULL
);
1795 else if (stImage
->base
.Data
) {
1796 st_texture_image_data(st
,
1801 stImage
->base
.RowStride
*
1802 util_format_get_blocksize(stObj
->pt
->format
),
1803 stImage
->base
.RowStride
*
1804 stImage
->base
.Height
*
1805 util_format_get_blocksize(stObj
->pt
->format
));
1806 _mesa_align_free(stImage
->base
.Data
);
1807 stImage
->base
.Data
= NULL
;
1810 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1815 * Called during state validation. When this function is finished,
1816 * the texture object should be ready for rendering.
1817 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1820 st_finalize_texture(GLcontext
*ctx
,
1821 struct pipe_context
*pipe
,
1822 struct gl_texture_object
*tObj
)
1824 struct st_context
*st
= st_context(ctx
);
1825 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1826 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1828 struct st_texture_image
*firstImage
;
1829 enum pipe_format firstImageFormat
;
1831 if (stObj
->base
._Complete
) {
1832 /* The texture is complete and we know exactly how many mipmap levels
1833 * are present/needed. This is conditional because we may be called
1834 * from the st_generate_mipmap() function when the texture object is
1835 * incomplete. In that case, we'll have set stObj->lastLevel before
1838 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1839 stObj
->base
.MinFilter
== GL_NEAREST
)
1840 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1842 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1845 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1848 /* If both firstImage and stObj point to a texture which can contain
1849 * all active images, favour firstImage. Note that because of the
1850 * completeness requirement, we know that the image dimensions
1853 if (firstImage
->pt
&&
1854 firstImage
->pt
!= stObj
->pt
&&
1855 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1856 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1857 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1860 /* Find gallium format for the Mesa texture */
1861 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1863 /* If we already have a gallium texture, check that it matches the texture
1864 * object's format, target, size, num_levels, etc.
1867 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1868 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1869 stObj
->pt
->last_level
< stObj
->lastLevel
||
1870 stObj
->pt
->width0
!= stObj
->width0
||
1871 stObj
->pt
->height0
!= stObj
->height0
||
1872 stObj
->pt
->depth0
!= stObj
->depth0
)
1874 /* The gallium texture does not match the Mesa texture so delete the
1875 * gallium texture now. We'll make a new one below.
1877 pipe_resource_reference(&stObj
->pt
, NULL
);
1878 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1879 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1883 /* May need to create a new gallium texture:
1886 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1888 stObj
->pt
= st_texture_create(st
,
1889 gl_target_to_pipe(stObj
->base
.Target
),
1898 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1903 /* Pull in any images not in the object's texture:
1905 for (face
= 0; face
< nr_faces
; face
++) {
1907 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1908 struct st_texture_image
*stImage
=
1909 st_texture_image(stObj
->base
.Image
[face
][level
]);
1911 /* Need to import images in main memory or held in other textures.
1913 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1914 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1924 * Returns pointer to a default/dummy texture.
1925 * This is typically used when the current shader has tex/sample instructions
1926 * but the user has not provided a (any) texture(s).
1928 struct gl_texture_object
*
1929 st_get_default_texture(struct st_context
*st
)
1931 if (!st
->default_texture
) {
1932 static const GLenum target
= GL_TEXTURE_2D
;
1933 GLubyte pixels
[16][16][4];
1934 struct gl_texture_object
*texObj
;
1935 struct gl_texture_image
*texImg
;
1938 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1939 * when attempting to sample incomplete textures.
1941 for (i
= 0; i
< 16; i
++) {
1942 for (j
= 0; j
< 16; j
++) {
1943 pixels
[i
][j
][0] = 0;
1944 pixels
[i
][j
][1] = 0;
1945 pixels
[i
][j
][2] = 0;
1946 pixels
[i
][j
][3] = 255;
1950 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1952 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1954 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1955 16, 16, 1, 0, /* w, h, d, border */
1958 st_TexImage(st
->ctx
, 2, target
,
1959 0, GL_RGBA
, /* level, intformat */
1960 16, 16, 1, 0, /* w, h, d, border */
1961 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1962 &st
->ctx
->DefaultPacking
,
1966 texObj
->MinFilter
= GL_NEAREST
;
1967 texObj
->MagFilter
= GL_NEAREST
;
1968 texObj
->_Complete
= GL_TRUE
;
1970 st
->default_texture
= texObj
;
1972 return st
->default_texture
;
1977 st_init_texture_functions(struct dd_function_table
*functions
)
1979 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1980 functions
->TexImage1D
= st_TexImage1D
;
1981 functions
->TexImage2D
= st_TexImage2D
;
1982 functions
->TexImage3D
= st_TexImage3D
;
1983 functions
->TexSubImage1D
= st_TexSubImage1D
;
1984 functions
->TexSubImage2D
= st_TexSubImage2D
;
1985 functions
->TexSubImage3D
= st_TexSubImage3D
;
1986 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1987 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1988 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1989 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1990 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1991 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1992 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1993 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1994 functions
->GenerateMipmap
= st_generate_mipmap
;
1996 functions
->GetTexImage
= st_GetTexImage
;
1998 /* compressed texture functions */
1999 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
2000 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
2002 functions
->NewTextureObject
= st_NewTextureObject
;
2003 functions
->NewTextureImage
= st_NewTextureImage
;
2004 functions
->DeleteTexture
= st_DeleteTextureObject
;
2005 functions
->FreeTexImageData
= st_FreeTextureImageData
;
2006 functions
->UpdateTexturePalette
= 0;
2008 functions
->TextureMemCpy
= do_memcpy
;
2010 /* XXX Temporary until we can query pipe's texture sizes */
2011 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;