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 case GL_TEXTURE_RECTANGLE_NV
:
78 return PIPE_TEXTURE_2D
;
81 return PIPE_TEXTURE_3D
;
83 case GL_TEXTURE_CUBE_MAP_ARB
:
84 return PIPE_TEXTURE_CUBE
;
93 /** called via ctx->Driver.NewTextureImage() */
94 static struct gl_texture_image
*
95 st_NewTextureImage(GLcontext
* ctx
)
97 DBG("%s\n", __FUNCTION__
);
99 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
103 /** called via ctx->Driver.NewTextureObject() */
104 static struct gl_texture_object
*
105 st_NewTextureObject(GLcontext
* ctx
, GLuint name
, GLenum target
)
107 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
109 DBG("%s\n", __FUNCTION__
);
110 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
115 /** called via ctx->Driver.DeleteTextureImage() */
117 st_DeleteTextureObject(GLcontext
*ctx
,
118 struct gl_texture_object
*texObj
)
120 struct st_context
*st
= st_context(ctx
);
121 struct st_texture_object
*stObj
= st_texture_object(texObj
);
123 pipe_resource_reference(&stObj
->pt
, NULL
);
124 if (stObj
->sampler_view
) {
125 if (stObj
->sampler_view
->context
!= st
->pipe
) {
126 /* Take "ownership" of this texture sampler view by setting
127 * its context pointer to this context. This avoids potential
128 * crashes when the texture object is shared among contexts
129 * and the original/owner context has already been destroyed.
131 stObj
->sampler_view
->context
= st
->pipe
;
133 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
135 _mesa_delete_texture_object(ctx
, texObj
);
139 /** called via ctx->Driver.FreeTexImageData() */
141 st_FreeTextureImageData(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
143 struct st_texture_image
*stImage
= st_texture_image(texImage
);
145 DBG("%s\n", __FUNCTION__
);
148 pipe_resource_reference(&stImage
->pt
, NULL
);
151 if (texImage
->Data
) {
152 _mesa_align_free(texImage
->Data
);
153 texImage
->Data
= NULL
;
159 * From linux kernel i386 header files, copes with odd sizes better
160 * than COPY_DWORDS would:
161 * XXX Put this in src/mesa/main/imports.h ???
163 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
165 __memcpy(void *to
, const void *from
, size_t n
)
168 __asm__
__volatile__("rep ; movsl\n\t"
172 "1:\ttestb $1,%b4\n\t"
174 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
175 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
180 #define __memcpy(a,b,c) memcpy(a,b,c)
185 * The system memcpy (at least on ubuntu 5.10) has problems copying
186 * to agp (writecombined) memory from a source which isn't 64-byte
187 * aligned - there is a 4x performance falloff.
189 * The x86 __memcpy is immune to this but is slightly slower
190 * (10%-ish) than the system memcpy.
192 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
193 * isn't much faster than x86_memcpy for agp copies.
195 * TODO: switch dynamically.
198 do_memcpy(void *dest
, const void *src
, size_t n
)
200 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
201 return __memcpy(dest
, src
, n
);
204 return memcpy(dest
, src
, n
);
209 * Return default texture resource binding bitmask for the given format.
212 default_bindings(struct st_context
*st
, enum pipe_format format
)
214 struct pipe_screen
*screen
= st
->pipe
->screen
;
215 const unsigned target
= PIPE_TEXTURE_2D
;
216 const unsigned geom
= 0x0;
219 if (util_format_is_depth_or_stencil(format
))
220 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
222 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
224 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
, geom
))
227 return PIPE_BIND_SAMPLER_VIEW
;
231 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
233 get_texture_dims(GLenum target
)
237 case GL_TEXTURE_1D_ARRAY_EXT
:
240 case GL_TEXTURE_CUBE_MAP_ARB
:
241 case GL_TEXTURE_RECTANGLE_NV
:
242 case GL_TEXTURE_2D_ARRAY_EXT
:
247 assert(0 && "invalid texture target in get_texture_dims()");
254 * Try to allocate a pipe_resource object for the given st_texture_object.
256 * We use the given st_texture_image as a clue to determine the size of the
257 * mipmap image at level=0.
259 * \return GL_TRUE for success, GL_FALSE if out of memory.
262 guess_and_alloc_texture(struct st_context
*st
,
263 struct st_texture_object
*stObj
,
264 const struct st_texture_image
*stImage
)
266 const GLuint dims
= get_texture_dims(stObj
->base
.Target
);
267 GLuint level
, lastLevel
, width
, height
, depth
;
269 enum pipe_format fmt
;
271 DBG("%s\n", __FUNCTION__
);
275 level
= stImage
->level
;
276 width
= stImage
->base
.Width2
; /* size w/out border */
277 height
= stImage
->base
.Height2
;
278 depth
= stImage
->base
.Depth2
;
284 /* Depending on the image's size, we can't always make a guess here.
287 if ( (dims
>= 1 && width
== 1) ||
288 (dims
>= 2 && height
== 1) ||
289 (dims
>= 3 && depth
== 1) ) {
290 /* we can't determine the image size at level=0 */
291 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
292 /* this is not an out of memory error */
297 /* grow the image size until we hit level = 0 */
310 /* At this point, (width x height x depth) is the expected size of
311 * the level=0 mipmap image.
314 /* Guess a reasonable value for lastLevel. With OpenGL we have no
315 * idea how many mipmap levels will be in a texture until we start
316 * to render with it. Make an educated guess here but be prepared
317 * to re-allocating a texture buffer with space for more (or fewer)
318 * mipmap levels later.
320 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
321 stObj
->base
.MinFilter
== GL_LINEAR
||
322 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
323 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
324 !stObj
->base
.GenerateMipmap
&&
325 stImage
->level
== 0) {
326 /* only alloc space for a single mipmap level */
330 /* alloc space for a full mipmap */
331 GLuint l2width
= util_logbase2(width
);
332 GLuint l2height
= util_logbase2(height
);
333 GLuint l2depth
= util_logbase2(depth
);
334 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
337 /* Save the level=0 dimensions */
338 stObj
->width0
= width
;
339 stObj
->height0
= height
;
340 stObj
->depth0
= depth
;
342 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
344 bindings
= default_bindings(st
, fmt
);
346 stObj
->pt
= st_texture_create(st
,
347 gl_target_to_pipe(stObj
->base
.Target
),
355 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
357 return stObj
->pt
!= NULL
;
362 * Adjust pixel unpack params and image dimensions to strip off the
364 * Gallium doesn't support texture borders. They've seldem been used
365 * and seldom been implemented correctly anyway.
366 * \param unpackNew returns the new pixel unpack parameters
369 strip_texture_border(GLint border
,
370 GLint
*width
, GLint
*height
, GLint
*depth
,
371 const struct gl_pixelstore_attrib
*unpack
,
372 struct gl_pixelstore_attrib
*unpackNew
)
374 assert(border
> 0); /* sanity check */
376 *unpackNew
= *unpack
;
378 if (unpackNew
->RowLength
== 0)
379 unpackNew
->RowLength
= *width
;
381 if (depth
&& unpackNew
->ImageHeight
== 0)
382 unpackNew
->ImageHeight
= *height
;
384 unpackNew
->SkipPixels
+= border
;
386 unpackNew
->SkipRows
+= border
;
388 unpackNew
->SkipImages
+= border
;
391 *width
= *width
- 2 * border
;
392 if (height
&& *height
>= 3)
393 *height
= *height
- 2 * border
;
394 if (depth
&& *depth
>= 3)
395 *depth
= *depth
- 2 * border
;
400 * Try to do texture compression via rendering. If the Gallium driver
401 * can render into a compressed surface this will allow us to do texture
403 * \return GL_TRUE for success, GL_FALSE for failure
406 compress_with_blit(GLcontext
* ctx
,
407 GLenum target
, GLint level
,
408 GLint xoffset
, GLint yoffset
, GLint zoffset
,
409 GLint width
, GLint height
, GLint depth
,
410 GLenum format
, GLenum type
, const void *pixels
,
411 const struct gl_pixelstore_attrib
*unpack
,
412 struct gl_texture_image
*texImage
)
414 const GLuint dstImageOffsets
[1] = {0};
415 struct st_texture_image
*stImage
= st_texture_image(texImage
);
416 struct st_context
*st
= st_context(ctx
);
417 struct pipe_context
*pipe
= st
->pipe
;
418 struct pipe_screen
*screen
= pipe
->screen
;
419 gl_format mesa_format
;
420 struct pipe_resource templ
;
421 struct pipe_resource
*src_tex
;
422 struct pipe_sampler_view view_templ
;
423 struct pipe_sampler_view
*src_view
;
424 struct pipe_surface
*dst_surface
;
425 struct pipe_transfer
*tex_xfer
;
429 /* XXX: Can this happen? Should we assert? */
433 /* get destination surface (in the compressed texture) */
434 dst_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
435 stImage
->face
, stImage
->level
, 0,
438 /* can't render into this format (or other problem) */
442 /* Choose format for the temporary RGBA texture image.
444 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
449 /* Create the temporary source texture
451 memset(&templ
, 0, sizeof(templ
));
452 templ
.target
= PIPE_TEXTURE_2D
;
453 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
);
454 templ
.width0
= width
;
455 templ
.height0
= height
;
457 templ
.last_level
= 0;
458 templ
.usage
= PIPE_USAGE_DEFAULT
;
459 templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
460 src_tex
= screen
->resource_create(screen
, &templ
);
465 /* Put user's tex data into the temporary texture
467 tex_xfer
= pipe_get_transfer(st_context(ctx
)->pipe
, src_tex
,
468 0, 0, 0, /* face, level are zero */
470 0, 0, width
, height
); /* x, y, w, h */
471 map
= pipe_transfer_map(pipe
, tex_xfer
);
473 _mesa_texstore(ctx
, 2, GL_RGBA
, mesa_format
,
475 0, 0, 0, /* dest x/y/z offset */
476 tex_xfer
->stride
, /* dest row stride (bytes) */
477 dstImageOffsets
, /* image offsets (for 3D only) */
478 width
, height
, 1, /* size */
479 format
, type
, /* source format/type */
480 pixels
, /* source data */
481 unpack
); /* source data packing */
483 pipe_transfer_unmap(pipe
, tex_xfer
);
484 pipe
->transfer_destroy(pipe
, tex_xfer
);
486 /* Create temporary sampler view */
487 u_sampler_view_default_template(&view_templ
,
490 src_view
= pipe
->create_sampler_view(pipe
, src_tex
, &view_templ
);
493 /* copy / compress image */
494 util_blit_pixels_tex(st
->blit
,
495 src_view
, /* sampler view (src) */
496 0, 0, /* src x0, y0 */
497 width
, height
, /* src x1, y1 */
498 dst_surface
, /* pipe_surface (dst) */
499 xoffset
, yoffset
, /* dst x0, y0 */
500 xoffset
+ width
, /* dst x1 */
501 yoffset
+ height
, /* dst y1 */
503 PIPE_TEX_MIPFILTER_NEAREST
);
505 pipe_surface_reference(&dst_surface
, NULL
);
506 pipe_resource_reference(&src_tex
, NULL
);
507 pipe_sampler_view_reference(&src_view
, NULL
);
514 * Do glTexImage1/2/3D().
517 st_TexImage(GLcontext
* ctx
,
519 GLenum target
, GLint level
,
520 GLint internalFormat
,
521 GLint width
, GLint height
, GLint depth
,
523 GLenum format
, GLenum type
, const void *pixels
,
524 const struct gl_pixelstore_attrib
*unpack
,
525 struct gl_texture_object
*texObj
,
526 struct gl_texture_image
*texImage
,
527 GLsizei imageSize
, GLboolean compressed_src
)
529 struct st_context
*st
= st_context(ctx
);
530 struct pipe_screen
*screen
= st
->pipe
->screen
;
531 struct st_texture_object
*stObj
= st_texture_object(texObj
);
532 struct st_texture_image
*stImage
= st_texture_image(texImage
);
533 GLuint dstRowStride
= 0;
534 struct gl_pixelstore_attrib unpackNB
;
535 enum pipe_transfer_usage transfer_usage
= 0;
537 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
538 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
540 /* The Mesa/Gallium state tracker does not implement the imaging extensions
541 * such as convolution.
543 assert(!ctx
->Extensions
.ARB_imaging
);
544 assert(!ctx
->Extensions
.EXT_convolution
);
546 /* switch to "normal" */
547 if (stObj
->surface_based
) {
548 _mesa_clear_texture_object(ctx
, texObj
);
549 stObj
->surface_based
= GL_FALSE
;
552 /* gallium does not support texture borders, strip it off */
554 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
556 texImage
->Width
= width
;
557 texImage
->Height
= height
;
558 texImage
->Depth
= depth
;
559 texImage
->Border
= 0;
563 assert(texImage
->Width
== width
);
564 assert(texImage
->Height
== height
);
565 assert(texImage
->Depth
== depth
);
568 stImage
->face
= _mesa_tex_target_to_face(target
);
569 stImage
->level
= level
;
571 _mesa_set_fetch_functions(texImage
, dims
);
573 /* Release the reference to a potentially orphaned buffer.
574 * Release any old malloced memory.
577 pipe_resource_reference(&stImage
->pt
, NULL
);
578 assert(!texImage
->Data
);
580 else if (texImage
->Data
) {
581 _mesa_align_free(texImage
->Data
);
585 * See if the new image is somehow incompatible with the existing
586 * mipmap. If so, free the old mipmap.
589 if (level
> (GLint
) stObj
->pt
->last_level
||
590 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
591 stImage
->face
, stImage
->level
)) {
593 pipe_resource_reference(&stObj
->pt
, NULL
);
595 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
599 if (width
== 0 || height
== 0 || depth
== 0) {
600 /* stop after freeing old image */
605 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
606 /* Probably out of memory.
607 * Try flushing any pending rendering, then retry.
610 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
611 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
617 assert(!stImage
->pt
);
619 /* Check if this texture image can live inside the texture object's buffer.
620 * If so, store the image there. Otherwise the image will temporarily live
624 st_texture_match_image(stObj
->pt
, &stImage
->base
,
625 stImage
->face
, stImage
->level
)) {
627 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
632 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
634 /* Pixel data may come from regular user memory or a PBO. For the later,
635 * do bounds checking and map the PBO to read pixels data from it.
637 * XXX we should try to use a GPU-accelerated path to copy the image data
638 * from the PBO to the texture.
640 if (compressed_src
) {
641 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
643 "glCompressedTexImage");
646 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
648 pixels
, unpack
, "glTexImage");
651 /* See if we can do texture compression with a blit/render.
653 if (!compressed_src
&&
655 _mesa_is_format_compressed(texImage
->TexFormat
) &&
656 screen
->is_format_supported(screen
,
658 stImage
->pt
->target
, 0,
659 PIPE_BIND_RENDER_TARGET
, 0)) {
663 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
664 format
, type
, pixels
, unpack
, texImage
)) {
670 * Prepare to store the texture data. Either map the gallium texture buffer
671 * memory or malloc space for it.
674 /* Store the image in the gallium texture memory buffer */
675 if (format
== GL_DEPTH_COMPONENT
&&
676 util_format_is_depth_and_stencil(stImage
->pt
->format
))
677 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
679 transfer_usage
= PIPE_TRANSFER_WRITE
;
681 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
682 transfer_usage
, 0, 0, width
, height
);
683 if(stImage
->transfer
)
684 dstRowStride
= stImage
->transfer
->stride
;
687 /* Allocate regular memory and store the image there temporarily. */
688 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
689 width
, height
, depth
);
690 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
692 texImage
->Data
= _mesa_align_malloc(imageSize
, 16);
695 if (!texImage
->Data
) {
696 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
701 /* We've allocated texture memory, but have no pixel data - all done. */
705 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
706 width
, height
, depth
, width
, dstRowStride
);
708 /* Copy user texture image into the texture buffer.
710 if (compressed_src
) {
711 const GLuint srcRowStride
=
712 _mesa_format_row_stride(texImage
->TexFormat
, width
);
713 if (dstRowStride
== srcRowStride
) {
714 memcpy(texImage
->Data
, pixels
, imageSize
);
717 char *dst
= texImage
->Data
;
718 const char *src
= pixels
;
719 GLuint i
, bw
, bh
, lines
;
720 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
721 lines
= (height
+ bh
- 1) / bh
;
723 for (i
= 0; i
< lines
; ++i
) {
724 memcpy(dst
, src
, srcRowStride
);
731 const GLuint srcImageStride
=
732 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
734 const GLubyte
*src
= (const GLubyte
*) pixels
;
736 for (i
= 0; i
< depth
; i
++) {
737 if (!_mesa_texstore(ctx
, dims
,
738 texImage
->_BaseFormat
,
741 0, 0, 0, /* dstX/Y/Zoffset */
743 texImage
->ImageOffsets
,
745 format
, type
, src
, unpack
)) {
746 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
749 if (stImage
->pt
&& i
+ 1 < depth
) {
750 /* unmap this slice */
751 st_texture_image_unmap(st
, stImage
);
752 /* map next slice of 3D texture */
753 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
754 transfer_usage
, 0, 0,
756 src
+= srcImageStride
;
762 _mesa_unmap_teximage_pbo(ctx
, unpack
);
764 if (stImage
->pt
&& texImage
->Data
) {
765 st_texture_image_unmap(st
, stImage
);
766 texImage
->Data
= NULL
;
772 st_TexImage3D(GLcontext
* ctx
,
773 GLenum target
, GLint level
,
774 GLint internalFormat
,
775 GLint width
, GLint height
, GLint depth
,
777 GLenum format
, GLenum type
, const void *pixels
,
778 const struct gl_pixelstore_attrib
*unpack
,
779 struct gl_texture_object
*texObj
,
780 struct gl_texture_image
*texImage
)
782 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
783 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
789 st_TexImage2D(GLcontext
* ctx
,
790 GLenum target
, GLint level
,
791 GLint internalFormat
,
792 GLint width
, GLint height
, GLint border
,
793 GLenum format
, GLenum type
, const void *pixels
,
794 const struct gl_pixelstore_attrib
*unpack
,
795 struct gl_texture_object
*texObj
,
796 struct gl_texture_image
*texImage
)
798 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
799 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
804 st_TexImage1D(GLcontext
* ctx
,
805 GLenum target
, GLint level
,
806 GLint internalFormat
,
807 GLint width
, 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
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
814 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
819 st_CompressedTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
820 GLint internalFormat
,
821 GLint width
, GLint height
, GLint border
,
822 GLsizei imageSize
, const GLvoid
*data
,
823 struct gl_texture_object
*texObj
,
824 struct gl_texture_image
*texImage
)
826 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
827 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
833 * glGetTexImage() helper: decompress a compressed texture by rendering
834 * a textured quad. Store the results in the user's buffer.
837 decompress_with_blit(GLcontext
* ctx
, GLenum target
, GLint level
,
838 GLenum format
, GLenum type
, GLvoid
*pixels
,
839 struct gl_texture_object
*texObj
,
840 struct gl_texture_image
*texImage
)
842 struct st_context
*st
= st_context(ctx
);
843 struct pipe_context
*pipe
= st
->pipe
;
844 struct pipe_screen
*screen
= pipe
->screen
;
845 struct st_texture_image
*stImage
= st_texture_image(texImage
);
846 struct st_texture_object
*stObj
= st_texture_object(texObj
);
847 struct pipe_sampler_view
*src_view
=
848 st_get_texture_sampler_view(stObj
, pipe
);
849 const GLuint width
= texImage
->Width
;
850 const GLuint height
= texImage
->Height
;
851 struct pipe_surface
*dst_surface
;
852 struct pipe_resource
*dst_texture
;
853 struct pipe_transfer
*tex_xfer
;
854 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
855 PIPE_BIND_TRANSFER_READ
);
857 /* create temp / dest surface */
858 if (!util_create_rgba_surface(screen
, width
, height
, bind
,
859 &dst_texture
, &dst_surface
)) {
860 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
861 "in decompress_with_blit()");
865 /* blit/render/decompress */
866 util_blit_pixels_tex(st
->blit
,
867 src_view
, /* pipe_resource (src) */
868 0, 0, /* src x0, y0 */
869 width
, height
, /* src x1, y1 */
870 dst_surface
, /* pipe_surface (dst) */
871 0, 0, /* dst x0, y0 */
872 width
, height
, /* dst x1, y1 */
874 PIPE_TEX_MIPFILTER_NEAREST
);
876 /* map the dst_surface so we can read from it */
877 tex_xfer
= pipe_get_transfer(st_context(ctx
)->pipe
,
878 dst_texture
, 0, 0, 0,
880 0, 0, width
, height
);
882 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
884 /* copy/pack data into user buffer */
885 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
887 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
888 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
890 for (row
= 0; row
< height
; row
++) {
891 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
892 height
, format
, type
, row
, 0);
893 memcpy(dest
, map
, bytesPerRow
);
894 map
+= tex_xfer
->stride
;
896 pipe_transfer_unmap(pipe
, tex_xfer
);
899 /* format translation via floats */
901 for (row
= 0; row
< height
; row
++) {
902 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
903 GLfloat rgba
[4 * MAX_WIDTH
];
904 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
905 height
, format
, type
, row
, 0);
907 if (ST_DEBUG
& DEBUG_FALLBACK
)
908 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
910 /* get float[4] rgba row from surface */
911 pipe_get_tile_rgba(pipe
, tex_xfer
, 0, row
, width
, 1, rgba
);
913 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
914 type
, dest
, &ctx
->Pack
, transferOps
);
918 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
920 pipe
->transfer_destroy(pipe
, tex_xfer
);
922 /* destroy the temp / dest surface */
923 util_destroy_rgba_surface(dst_texture
, dst_surface
);
929 * Need to map texture image into memory before copying image data,
933 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
934 GLenum format
, GLenum type
, GLvoid
* pixels
,
935 struct gl_texture_object
*texObj
,
936 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
938 struct st_context
*st
= st_context(ctx
);
939 struct st_texture_image
*stImage
= st_texture_image(texImage
);
940 const GLuint dstImageStride
=
941 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
947 util_format_is_s3tc(stImage
->pt
->format
) &&
949 /* Need to decompress the texture.
950 * We'll do this by rendering a textured quad.
951 * Note that we only expect RGBA formats (no Z/depth formats).
953 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
960 /* Image is stored in hardware format in a buffer managed by the
961 * kernel. Need to explicitly map and unmap it.
963 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
964 PIPE_TRANSFER_READ
, 0, 0,
966 stImage
->base
.Height
);
967 /* compute stride in texels from stride in bytes */
968 texImage
->RowStride
= stImage
->transfer
->stride
969 * util_format_get_blockwidth(stImage
->pt
->format
)
970 / util_format_get_blocksize(stImage
->pt
->format
);
973 /* Otherwise, the image should actually be stored in
974 * texImage->Data. This is pretty confusing for
975 * everybody, I'd much prefer to separate the two functions of
976 * texImage->Data - storage for texture images in main memory
977 * and access (ie mappings) of images. In other words, we'd
978 * create a new texImage->Map field and leave Data simply for
981 assert(texImage
->Data
);
984 depth
= texImage
->Depth
;
987 dest
= (GLubyte
*) pixels
;
989 for (i
= 0; i
< depth
; i
++) {
990 if (compressed_dst
) {
991 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
995 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
999 if (stImage
->pt
&& i
+ 1 < depth
) {
1000 /* unmap this slice */
1001 st_texture_image_unmap(st
, stImage
);
1002 /* map next slice of 3D texture */
1003 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
1004 PIPE_TRANSFER_READ
, 0, 0,
1005 stImage
->base
.Width
,
1006 stImage
->base
.Height
);
1007 dest
+= dstImageStride
;
1011 texImage
->Depth
= depth
;
1015 st_texture_image_unmap(st
, stImage
);
1016 texImage
->Data
= NULL
;
1022 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
1023 GLenum format
, GLenum type
, GLvoid
* pixels
,
1024 struct gl_texture_object
*texObj
,
1025 struct gl_texture_image
*texImage
)
1027 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1033 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
1035 struct gl_texture_object
*texObj
,
1036 struct gl_texture_image
*texImage
)
1038 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1045 st_TexSubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
1046 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1047 GLint width
, GLint height
, GLint depth
,
1048 GLenum format
, GLenum type
, const void *pixels
,
1049 const struct gl_pixelstore_attrib
*packing
,
1050 struct gl_texture_object
*texObj
,
1051 struct gl_texture_image
*texImage
)
1053 struct st_context
*st
= st_context(ctx
);
1054 struct pipe_screen
*screen
= st
->pipe
->screen
;
1055 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1056 GLuint dstRowStride
;
1057 const GLuint srcImageStride
=
1058 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1061 /* init to silence warning only: */
1062 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1064 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1065 _mesa_lookup_enum_by_nr(target
),
1066 level
, xoffset
, yoffset
, width
, height
);
1069 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1070 type
, pixels
, packing
, "glTexSubImage2D");
1074 /* See if we can do texture compression with a blit/render.
1076 if (!ctx
->Mesa_DXTn
&&
1077 _mesa_is_format_compressed(texImage
->TexFormat
) &&
1078 screen
->is_format_supported(screen
,
1079 stImage
->pt
->format
,
1080 stImage
->pt
->target
, 0,
1081 PIPE_BIND_RENDER_TARGET
, 0)) {
1082 if (compress_with_blit(ctx
, target
, level
,
1083 xoffset
, yoffset
, zoffset
,
1084 width
, height
, depth
,
1085 format
, type
, pixels
, packing
, texImage
)) {
1090 /* Map buffer if necessary. Need to lock to prevent other contexts
1091 * from uploading the buffer under us.
1094 if (format
== GL_DEPTH_COMPONENT
&&
1095 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1096 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1098 transfer_usage
= PIPE_TRANSFER_WRITE
;
1100 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1106 if (!texImage
->Data
) {
1107 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1111 src
= (const GLubyte
*) pixels
;
1112 dstRowStride
= stImage
->transfer
->stride
;
1114 for (i
= 0; i
< depth
; i
++) {
1115 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1116 texImage
->TexFormat
,
1120 texImage
->ImageOffsets
,
1122 format
, type
, src
, packing
)) {
1123 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1126 if (stImage
->pt
&& i
+ 1 < depth
) {
1127 /* unmap this slice */
1128 st_texture_image_unmap(st
, stImage
);
1129 /* map next slice of 3D texture */
1130 texImage
->Data
= st_texture_image_map(st
, stImage
,
1135 src
+= srcImageStride
;
1140 _mesa_unmap_teximage_pbo(ctx
, packing
);
1142 if (stImage
->pt
&& texImage
->Data
) {
1143 st_texture_image_unmap(st
, stImage
);
1144 texImage
->Data
= NULL
;
1151 st_TexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1152 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1153 GLsizei width
, GLsizei height
, GLsizei depth
,
1154 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1155 const struct gl_pixelstore_attrib
*packing
,
1156 struct gl_texture_object
*texObj
,
1157 struct gl_texture_image
*texImage
)
1159 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1160 width
, height
, depth
, format
, type
,
1161 pixels
, packing
, texObj
, texImage
);
1166 st_TexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1167 GLint xoffset
, GLint yoffset
,
1168 GLsizei width
, GLsizei height
,
1169 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1170 const struct gl_pixelstore_attrib
*packing
,
1171 struct gl_texture_object
*texObj
,
1172 struct gl_texture_image
*texImage
)
1174 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1175 width
, height
, 1, format
, type
,
1176 pixels
, packing
, texObj
, texImage
);
1181 st_TexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1182 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1183 const GLvoid
* pixels
,
1184 const struct gl_pixelstore_attrib
*packing
,
1185 struct gl_texture_object
*texObj
,
1186 struct gl_texture_image
*texImage
)
1188 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1189 format
, type
, pixels
, packing
, texObj
, texImage
);
1194 st_CompressedTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1195 GLint xoffset
, GLsizei width
,
1197 GLsizei imageSize
, const GLvoid
*data
,
1198 struct gl_texture_object
*texObj
,
1199 struct gl_texture_image
*texImage
)
1206 st_CompressedTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1207 GLint xoffset
, GLint yoffset
,
1208 GLsizei width
, GLint height
,
1210 GLsizei imageSize
, const GLvoid
*data
,
1211 struct gl_texture_object
*texObj
,
1212 struct gl_texture_image
*texImage
)
1214 struct st_context
*st
= st_context(ctx
);
1215 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1219 enum pipe_format pformat
;
1222 pformat
= stImage
->pt
->format
;
1224 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1225 PIPE_TRANSFER_WRITE
,
1229 srcBlockStride
= util_format_get_stride(pformat
, width
);
1230 dstBlockStride
= stImage
->transfer
->stride
;
1232 assert(stImage
->pt
);
1233 /* TODO find good values for block and strides */
1234 /* TODO also adjust texImage->data for yoffset/xoffset */
1238 if (!texImage
->Data
) {
1239 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1243 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1244 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1245 assert(width
% util_format_get_blockwidth(pformat
) == 0);
1246 assert(height
% util_format_get_blockheight(pformat
) == 0);
1248 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1249 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1250 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1251 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1252 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1256 st_texture_image_unmap(st
, stImage
);
1257 texImage
->Data
= NULL
;
1263 st_CompressedTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1264 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1265 GLsizei width
, GLint height
, GLint depth
,
1267 GLsizei imageSize
, const GLvoid
*data
,
1268 struct gl_texture_object
*texObj
,
1269 struct gl_texture_image
*texImage
)
1277 * Do a CopyTexSubImage operation using a read transfer from the source,
1278 * a write transfer to the destination and get_tile()/put_tile() to access
1279 * the pixels/texels.
1281 * Note: srcY=0=TOP of renderbuffer
1284 fallback_copy_texsubimage(GLcontext
*ctx
, GLenum target
, GLint level
,
1285 struct st_renderbuffer
*strb
,
1286 struct st_texture_image
*stImage
,
1288 GLint destX
, GLint destY
, GLint destZ
,
1289 GLint srcX
, GLint srcY
,
1290 GLsizei width
, GLsizei height
)
1292 struct st_context
*st
= st_context(ctx
);
1293 struct pipe_context
*pipe
= st
->pipe
;
1294 struct pipe_transfer
*src_trans
;
1296 enum pipe_transfer_usage transfer_usage
;
1298 if (ST_DEBUG
& DEBUG_FALLBACK
)
1299 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1301 assert(width
<= MAX_WIDTH
);
1303 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1304 srcY
= strb
->Base
.Height
- srcY
- height
;
1307 src_trans
= pipe_get_transfer(st_context(ctx
)->pipe
,
1314 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1315 baseFormat
== GL_DEPTH_STENCIL
) &&
1316 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1317 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1319 transfer_usage
= PIPE_TRANSFER_WRITE
;
1321 texDest
= st_texture_image_map(st
, stImage
, 0, transfer_usage
,
1322 destX
, destY
, width
, height
);
1324 if (baseFormat
== GL_DEPTH_COMPONENT
||
1325 baseFormat
== GL_DEPTH_STENCIL
) {
1326 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1327 ctx
->Pixel
.DepthBias
!= 0.0F
);
1330 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1331 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1340 /* To avoid a large temp memory allocation, do copy row by row */
1341 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1342 uint data
[MAX_WIDTH
];
1343 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1345 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1347 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1353 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1355 if (tempSrc
&& texDest
) {
1356 const GLint dims
= 2;
1357 const GLint dstRowStride
= stImage
->transfer
->stride
;
1358 struct gl_texture_image
*texImage
= &stImage
->base
;
1359 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1361 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1362 unpack
.Invert
= GL_TRUE
;
1365 /* get float/RGBA image from framebuffer */
1366 /* XXX this usually involves a lot of int/float conversion.
1367 * try to avoid that someday.
1369 pipe_get_tile_rgba(pipe
, src_trans
, 0, 0, width
, height
, tempSrc
);
1371 /* Store into texture memory.
1372 * Note that this does some special things such as pixel transfer
1373 * ops and format conversion. In particular, if the dest tex format
1374 * is actually RGBA but the user created the texture as GL_RGB we
1375 * need to fill-in/override the alpha channel with 1.0.
1377 _mesa_texstore(ctx
, dims
,
1378 texImage
->_BaseFormat
,
1379 texImage
->TexFormat
,
1383 texImage
->ImageOffsets
,
1385 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1389 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1396 st_texture_image_unmap(st
, stImage
);
1397 pipe
->transfer_destroy(pipe
, src_trans
);
1403 * If the format of the src renderbuffer and the format of the dest
1404 * texture are compatible (in terms of blitting), return a TGSI writemask
1405 * to be used during the blit.
1406 * If the src/dest are incompatible, return 0.
1409 compatible_src_dst_formats(GLcontext
*ctx
,
1410 const struct gl_renderbuffer
*src
,
1411 const struct gl_texture_image
*dst
)
1413 /* Get logical base formats for the src and dest.
1414 * That is, use the user-requested formats and not the actual, device-
1416 * For example, the user may have requested an A8 texture but the
1417 * driver may actually be using an RGBA texture format. When we
1418 * copy/blit to that texture, we only want to copy the Alpha channel
1419 * and not the RGB channels.
1421 * Similarly, when the src FBO was created an RGB format may have been
1422 * requested but the driver actually chose an RGBA format. In that case,
1423 * we don't want to copy the undefined Alpha channel to the dest texture
1424 * (it should be 1.0).
1426 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1427 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1430 * XXX when we have red-only and red/green renderbuffers we'll need
1431 * to add more cases here (or implement a general-purpose routine that
1432 * queries the existance of the R,G,B,A channels in the src and dest).
1434 if (srcFormat
== dstFormat
) {
1435 /* This is the same as matching_base_formats, which should
1436 * always pass, as it did previously.
1438 return TGSI_WRITEMASK_XYZW
;
1440 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1441 /* Make sure that A in the dest is 1. The actual src format
1442 * may be RGBA and have undefined A values.
1444 return TGSI_WRITEMASK_XYZ
;
1446 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1447 /* Make sure that A in the dest is 1. The actual dst format
1448 * may be RGBA and will need A=1 to provide proper alpha values
1449 * when sampled later.
1451 return TGSI_WRITEMASK_XYZ
;
1454 if (ST_DEBUG
& DEBUG_FALLBACK
)
1455 debug_printf("%s failed for src %s, dst %s\n",
1457 _mesa_lookup_enum_by_nr(srcFormat
),
1458 _mesa_lookup_enum_by_nr(dstFormat
));
1469 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1470 * Note that the region to copy has already been clipped so we know we
1471 * won't read from outside the source renderbuffer's bounds.
1473 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1476 st_copy_texsubimage(GLcontext
*ctx
,
1477 GLenum target
, GLint level
,
1478 GLint destX
, GLint destY
, GLint destZ
,
1479 GLint srcX
, GLint srcY
,
1480 GLsizei width
, GLsizei height
)
1482 struct gl_texture_unit
*texUnit
=
1483 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1484 struct gl_texture_object
*texObj
=
1485 _mesa_select_tex_object(ctx
, texUnit
, target
);
1486 struct gl_texture_image
*texImage
=
1487 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1488 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1489 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1490 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1491 struct st_renderbuffer
*strb
;
1492 struct st_context
*st
= st_context(ctx
);
1493 struct pipe_context
*pipe
= st
->pipe
;
1494 struct pipe_screen
*screen
= pipe
->screen
;
1495 enum pipe_format dest_format
, src_format
;
1496 GLboolean use_fallback
= GL_TRUE
;
1497 GLboolean matching_base_formats
;
1498 GLuint format_writemask
, sample_count
;
1499 struct pipe_surface
*dest_surface
= NULL
;
1500 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1502 /* make sure finalize_textures has been called?
1504 if (0) st_validate_state(st
);
1506 /* determine if copying depth or color data */
1507 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1508 texBaseFormat
== GL_DEPTH_STENCIL
) {
1509 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1512 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1513 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1516 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1517 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1521 sample_count
= strb
->surface
->texture
->nr_samples
;
1522 /* I believe this would be legal, presumably would need to do a resolve
1523 for color, and for depth/stencil spec says to just use one of the
1524 depth/stencil samples per pixel? Need some transfer clarifications. */
1525 assert(sample_count
< 2);
1551 if (width
< 0 || height
< 0)
1556 assert(strb
->surface
);
1557 assert(stImage
->pt
);
1559 src_format
= strb
->surface
->format
;
1560 dest_format
= stImage
->pt
->format
;
1563 * Determine if the src framebuffer and dest texture have the same
1564 * base format. We need this to detect a case such as the framebuffer
1565 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1566 * texture format stores RGBA we need to set A=1 (overriding the
1567 * framebuffer's alpha values). We can't do that with the blit or
1568 * textured-quad paths.
1570 matching_base_formats
=
1571 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1572 _mesa_get_format_base_format(texImage
->TexFormat
));
1573 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1575 if (ctx
->_ImageTransferState
== 0x0) {
1577 if (matching_base_formats
&&
1578 src_format
== dest_format
&&
1581 /* use surface_copy() / blit */
1582 struct pipe_subresource subdst
, subsrc
;
1583 subdst
.face
= stImage
->face
;
1584 subdst
.level
= stImage
->level
;
1585 subsrc
.face
= strb
->surface
->face
;
1586 subsrc
.level
= strb
->surface
->level
;
1588 /* for resource_copy_region(), y=0=top, always */
1589 pipe
->resource_copy_region(pipe
,
1593 destX
, destY
, destZ
,
1597 srcX
, srcY
, strb
->surface
->zslice
,
1600 use_fallback
= GL_FALSE
;
1602 else if (format_writemask
&&
1603 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1604 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1605 screen
->is_format_supported(screen
, src_format
,
1606 PIPE_TEXTURE_2D
, sample_count
,
1607 PIPE_BIND_SAMPLER_VIEW
,
1609 screen
->is_format_supported(screen
, dest_format
,
1611 PIPE_BIND_RENDER_TARGET
,
1613 /* draw textured quad to do the copy */
1615 struct pipe_subresource subsrc
;
1617 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1618 stImage
->face
, stImage
->level
,
1620 PIPE_BIND_RENDER_TARGET
);
1623 srcY1
= strb
->Base
.Height
- srcY
- height
;
1624 srcY0
= srcY1
+ height
;
1628 srcY1
= srcY0
+ height
;
1630 subsrc
.face
= strb
->surface
->face
;
1631 subsrc
.level
= strb
->surface
->level
;
1633 util_blit_pixels_writemask(st
->blit
,
1637 srcX
+ width
, srcY1
,
1638 strb
->surface
->zslice
,
1641 destX
+ width
, destY
+ height
,
1642 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1644 use_fallback
= GL_FALSE
;
1648 pipe_surface_reference(&dest_surface
, NULL
);
1652 /* software fallback */
1653 fallback_copy_texsubimage(ctx
, target
, level
,
1654 strb
, stImage
, texBaseFormat
,
1655 destX
, destY
, destZ
,
1656 srcX
, srcY
, width
, height
);
1663 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1664 GLenum internalFormat
,
1665 GLint x
, GLint y
, GLsizei width
, GLint border
)
1667 struct gl_texture_unit
*texUnit
=
1668 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1669 struct gl_texture_object
*texObj
=
1670 _mesa_select_tex_object(ctx
, texUnit
, target
);
1671 struct gl_texture_image
*texImage
=
1672 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1674 /* Setup or redefine the texture object, texture and texture
1675 * image. Don't populate yet.
1677 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1679 GL_RGBA
, CHAN_TYPE
, NULL
,
1680 &ctx
->DefaultPacking
, texObj
, texImage
);
1682 st_copy_texsubimage(ctx
, target
, level
,
1683 0, 0, 0, /* destX,Y,Z */
1684 x
, y
, width
, 1); /* src X, Y, size */
1689 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1690 GLenum internalFormat
,
1691 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1694 struct gl_texture_unit
*texUnit
=
1695 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1696 struct gl_texture_object
*texObj
=
1697 _mesa_select_tex_object(ctx
, texUnit
, target
);
1698 struct gl_texture_image
*texImage
=
1699 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1701 /* Setup or redefine the texture object, texture and texture
1702 * image. Don't populate yet.
1704 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1705 width
, height
, border
,
1706 GL_RGBA
, CHAN_TYPE
, NULL
,
1707 &ctx
->DefaultPacking
, texObj
, texImage
);
1709 st_copy_texsubimage(ctx
, target
, level
,
1710 0, 0, 0, /* destX,Y,Z */
1711 x
, y
, width
, height
); /* src X, Y, size */
1716 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1717 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1719 const GLint yoffset
= 0, zoffset
= 0;
1720 const GLsizei height
= 1;
1721 st_copy_texsubimage(ctx
, target
, level
,
1722 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1723 x
, y
, width
, height
); /* src X, Y, size */
1728 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1729 GLint xoffset
, GLint yoffset
,
1730 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1732 const GLint zoffset
= 0;
1733 st_copy_texsubimage(ctx
, target
, level
,
1734 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1735 x
, y
, width
, height
); /* src X, Y, size */
1740 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1741 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1742 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1744 st_copy_texsubimage(ctx
, target
, level
,
1745 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1746 x
, y
, width
, height
); /* src X, Y, size */
1751 * Copy image data from stImage into the texture object 'stObj' at level
1755 copy_image_data_to_texture(struct st_context
*st
,
1756 struct st_texture_object
*stObj
,
1758 struct st_texture_image
*stImage
)
1762 const struct gl_texture_image
*dstImage
=
1763 stObj
->base
.Image
[stImage
->face
][stImage
->level
];
1765 assert(dstImage
->Width
== stImage
->base
.Width
);
1766 assert(dstImage
->Height
== stImage
->base
.Height
);
1767 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1771 /* Copy potentially with the blitter:
1773 st_texture_image_copy(st
->pipe
,
1774 stObj
->pt
, dstLevel
, /* dest texture, level */
1775 stImage
->pt
, stImage
->level
, /* src texture, level */
1778 pipe_resource_reference(&stImage
->pt
, NULL
);
1780 else if (stImage
->base
.Data
) {
1781 st_texture_image_data(st
,
1786 stImage
->base
.RowStride
*
1787 util_format_get_blocksize(stObj
->pt
->format
),
1788 stImage
->base
.RowStride
*
1789 stImage
->base
.Height
*
1790 util_format_get_blocksize(stObj
->pt
->format
));
1791 _mesa_align_free(stImage
->base
.Data
);
1792 stImage
->base
.Data
= NULL
;
1795 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1800 * Called during state validation. When this function is finished,
1801 * the texture object should be ready for rendering.
1802 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1805 st_finalize_texture(GLcontext
*ctx
,
1806 struct pipe_context
*pipe
,
1807 struct gl_texture_object
*tObj
)
1809 struct st_context
*st
= st_context(ctx
);
1810 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1811 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1813 struct st_texture_image
*firstImage
;
1814 enum pipe_format firstImageFormat
;
1816 if (stObj
->base
._Complete
) {
1817 /* The texture is complete and we know exactly how many mipmap levels
1818 * are present/needed. This is conditional because we may be called
1819 * from the st_generate_mipmap() function when the texture object is
1820 * incomplete. In that case, we'll have set stObj->lastLevel before
1823 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1824 stObj
->base
.MinFilter
== GL_NEAREST
)
1825 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1827 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1830 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1833 /* If both firstImage and stObj point to a texture which can contain
1834 * all active images, favour firstImage. Note that because of the
1835 * completeness requirement, we know that the image dimensions
1838 if (firstImage
->pt
&&
1839 firstImage
->pt
!= stObj
->pt
&&
1840 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1841 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1842 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1845 /* Find gallium format for the Mesa texture */
1846 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1848 /* If we already have a gallium texture, check that it matches the texture
1849 * object's format, target, size, num_levels, etc.
1852 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1853 stObj
->pt
->format
!= firstImageFormat
||
1854 stObj
->pt
->last_level
< stObj
->lastLevel
||
1855 stObj
->pt
->width0
!= stObj
->width0
||
1856 stObj
->pt
->height0
!= stObj
->height0
||
1857 stObj
->pt
->depth0
!= stObj
->depth0
)
1859 /* The gallium texture does not match the Mesa texture so delete the
1860 * gallium texture now. We'll make a new one below.
1862 pipe_resource_reference(&stObj
->pt
, NULL
);
1863 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1864 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1868 /* May need to create a new gallium texture:
1871 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1873 stObj
->pt
= st_texture_create(st
,
1874 gl_target_to_pipe(stObj
->base
.Target
),
1883 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1888 /* Pull in any images not in the object's texture:
1890 for (face
= 0; face
< nr_faces
; face
++) {
1892 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1893 struct st_texture_image
*stImage
=
1894 st_texture_image(stObj
->base
.Image
[face
][level
]);
1896 /* Need to import images in main memory or held in other textures.
1898 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1899 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1909 * Returns pointer to a default/dummy texture.
1910 * This is typically used when the current shader has tex/sample instructions
1911 * but the user has not provided a (any) texture(s).
1913 struct gl_texture_object
*
1914 st_get_default_texture(struct st_context
*st
)
1916 if (!st
->default_texture
) {
1917 static const GLenum target
= GL_TEXTURE_2D
;
1918 GLubyte pixels
[16][16][4];
1919 struct gl_texture_object
*texObj
;
1920 struct gl_texture_image
*texImg
;
1923 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1924 * when attempting to sample incomplete textures.
1926 for (i
= 0; i
< 16; i
++) {
1927 for (j
= 0; j
< 16; j
++) {
1928 pixels
[i
][j
][0] = 0;
1929 pixels
[i
][j
][1] = 0;
1930 pixels
[i
][j
][2] = 0;
1931 pixels
[i
][j
][3] = 255;
1935 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1937 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1939 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1940 16, 16, 1, 0, /* w, h, d, border */
1943 st_TexImage(st
->ctx
, 2, target
,
1944 0, GL_RGBA
, /* level, intformat */
1945 16, 16, 1, 0, /* w, h, d, border */
1946 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1947 &st
->ctx
->DefaultPacking
,
1951 texObj
->MinFilter
= GL_NEAREST
;
1952 texObj
->MagFilter
= GL_NEAREST
;
1953 texObj
->_Complete
= GL_TRUE
;
1955 st
->default_texture
= texObj
;
1957 return st
->default_texture
;
1962 st_init_texture_functions(struct dd_function_table
*functions
)
1964 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1965 functions
->TexImage1D
= st_TexImage1D
;
1966 functions
->TexImage2D
= st_TexImage2D
;
1967 functions
->TexImage3D
= st_TexImage3D
;
1968 functions
->TexSubImage1D
= st_TexSubImage1D
;
1969 functions
->TexSubImage2D
= st_TexSubImage2D
;
1970 functions
->TexSubImage3D
= st_TexSubImage3D
;
1971 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1972 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1973 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1974 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1975 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1976 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1977 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1978 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1979 functions
->GenerateMipmap
= st_generate_mipmap
;
1981 functions
->GetTexImage
= st_GetTexImage
;
1983 /* compressed texture functions */
1984 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1985 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1987 functions
->NewTextureObject
= st_NewTextureObject
;
1988 functions
->NewTextureImage
= st_NewTextureImage
;
1989 functions
->DeleteTexture
= st_DeleteTextureObject
;
1990 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1991 functions
->UpdateTexturePalette
= 0;
1993 functions
->TextureMemCpy
= do_memcpy
;
1995 /* XXX Temporary until we can query pipe's texture sizes */
1996 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;