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(struct gl_context
* 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(struct gl_context
* 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(struct gl_context
*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(struct gl_context
* 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(struct gl_context
* 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(struct gl_context
* 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 /* switch to "normal" */
549 if (stObj
->surface_based
) {
550 _mesa_clear_texture_object(ctx
, texObj
);
551 pipe_resource_reference(&stObj
->pt
, NULL
);
553 /* oops, need to init this image again */
554 _mesa_init_teximage_fields(ctx
, target
, texImage
,
555 width
, height
, depth
, border
, internalFormat
);
556 _mesa_choose_texture_format(ctx
, texObj
, texImage
, target
, level
,
557 internalFormat
, format
, type
);
559 stObj
->surface_based
= GL_FALSE
;
562 /* gallium does not support texture borders, strip it off */
564 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
566 texImage
->Width
= width
;
567 texImage
->Height
= height
;
568 texImage
->Depth
= depth
;
569 texImage
->Border
= 0;
573 assert(texImage
->Width
== width
);
574 assert(texImage
->Height
== height
);
575 assert(texImage
->Depth
== depth
);
578 stImage
->face
= _mesa_tex_target_to_face(target
);
579 stImage
->level
= level
;
581 _mesa_set_fetch_functions(texImage
, dims
);
583 /* Release the reference to a potentially orphaned buffer.
584 * Release any old malloced memory.
587 pipe_resource_reference(&stImage
->pt
, NULL
);
588 assert(!texImage
->Data
);
590 else if (texImage
->Data
) {
591 _mesa_align_free(texImage
->Data
);
595 * See if the new image is somehow incompatible with the existing
596 * mipmap. If so, free the old mipmap.
599 if (level
> (GLint
) stObj
->pt
->last_level
||
600 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
601 stImage
->face
, stImage
->level
)) {
603 pipe_resource_reference(&stObj
->pt
, NULL
);
605 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
609 if (width
== 0 || height
== 0 || depth
== 0) {
610 /* stop after freeing old image */
615 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
616 /* Probably out of memory.
617 * Try flushing any pending rendering, then retry.
620 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
621 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
627 assert(!stImage
->pt
);
629 /* Check if this texture image can live inside the texture object's buffer.
630 * If so, store the image there. Otherwise the image will temporarily live
634 st_texture_match_image(stObj
->pt
, &stImage
->base
,
635 stImage
->face
, stImage
->level
)) {
637 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
642 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
644 /* Pixel data may come from regular user memory or a PBO. For the later,
645 * do bounds checking and map the PBO to read pixels data from it.
647 * XXX we should try to use a GPU-accelerated path to copy the image data
648 * from the PBO to the texture.
650 if (compressed_src
) {
651 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
653 "glCompressedTexImage");
656 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
658 pixels
, unpack
, "glTexImage");
661 /* See if we can do texture compression with a blit/render.
663 if (!compressed_src
&&
665 _mesa_is_format_compressed(texImage
->TexFormat
) &&
666 screen
->is_format_supported(screen
,
668 stImage
->pt
->target
, 0,
669 PIPE_BIND_RENDER_TARGET
, 0)) {
673 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
674 format
, type
, pixels
, unpack
, texImage
)) {
680 * Prepare to store the texture data. Either map the gallium texture buffer
681 * memory or malloc space for it.
684 /* Store the image in the gallium texture memory buffer */
685 if (format
== GL_DEPTH_COMPONENT
&&
686 util_format_is_depth_and_stencil(stImage
->pt
->format
))
687 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
689 transfer_usage
= PIPE_TRANSFER_WRITE
;
691 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
692 transfer_usage
, 0, 0, width
, height
);
693 if(stImage
->transfer
)
694 dstRowStride
= stImage
->transfer
->stride
;
697 /* Allocate regular memory and store the image there temporarily. */
698 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
699 width
, height
, depth
);
700 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
702 texImage
->Data
= _mesa_align_malloc(imageSize
, 16);
705 if (!texImage
->Data
) {
706 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
711 /* We've allocated texture memory, but have no pixel data - all done. */
715 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
716 width
, height
, depth
, width
, dstRowStride
);
718 /* Copy user texture image into the texture buffer.
720 if (compressed_src
) {
721 const GLuint srcRowStride
=
722 _mesa_format_row_stride(texImage
->TexFormat
, width
);
723 if (dstRowStride
== srcRowStride
) {
724 memcpy(texImage
->Data
, pixels
, imageSize
);
727 char *dst
= texImage
->Data
;
728 const char *src
= pixels
;
729 GLuint i
, bw
, bh
, lines
;
730 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
731 lines
= (height
+ bh
- 1) / bh
;
733 for (i
= 0; i
< lines
; ++i
) {
734 memcpy(dst
, src
, srcRowStride
);
741 const GLuint srcImageStride
=
742 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
744 const GLubyte
*src
= (const GLubyte
*) pixels
;
746 for (i
= 0; i
< depth
; i
++) {
747 if (!_mesa_texstore(ctx
, dims
,
748 texImage
->_BaseFormat
,
751 0, 0, 0, /* dstX/Y/Zoffset */
753 texImage
->ImageOffsets
,
755 format
, type
, src
, unpack
)) {
756 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
759 if (stImage
->pt
&& i
+ 1 < depth
) {
760 /* unmap this slice */
761 st_texture_image_unmap(st
, stImage
);
762 /* map next slice of 3D texture */
763 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
764 transfer_usage
, 0, 0,
766 src
+= srcImageStride
;
772 _mesa_unmap_teximage_pbo(ctx
, unpack
);
774 if (stImage
->pt
&& texImage
->Data
) {
775 st_texture_image_unmap(st
, stImage
);
776 texImage
->Data
= NULL
;
782 st_TexImage3D(struct gl_context
* ctx
,
783 GLenum target
, GLint level
,
784 GLint internalFormat
,
785 GLint width
, GLint height
, GLint depth
,
787 GLenum format
, GLenum type
, const void *pixels
,
788 const struct gl_pixelstore_attrib
*unpack
,
789 struct gl_texture_object
*texObj
,
790 struct gl_texture_image
*texImage
)
792 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
793 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
799 st_TexImage2D(struct gl_context
* ctx
,
800 GLenum target
, GLint level
,
801 GLint internalFormat
,
802 GLint width
, GLint height
, GLint border
,
803 GLenum format
, GLenum type
, const void *pixels
,
804 const struct gl_pixelstore_attrib
*unpack
,
805 struct gl_texture_object
*texObj
,
806 struct gl_texture_image
*texImage
)
808 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
809 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
814 st_TexImage1D(struct gl_context
* ctx
,
815 GLenum target
, GLint level
,
816 GLint internalFormat
,
817 GLint width
, GLint border
,
818 GLenum format
, GLenum type
, const void *pixels
,
819 const struct gl_pixelstore_attrib
*unpack
,
820 struct gl_texture_object
*texObj
,
821 struct gl_texture_image
*texImage
)
823 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
824 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
829 st_CompressedTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
830 GLint internalFormat
,
831 GLint width
, GLint height
, GLint border
,
832 GLsizei imageSize
, const GLvoid
*data
,
833 struct gl_texture_object
*texObj
,
834 struct gl_texture_image
*texImage
)
836 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
837 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
843 * glGetTexImage() helper: decompress a compressed texture by rendering
844 * a textured quad. Store the results in the user's buffer.
847 decompress_with_blit(struct gl_context
* ctx
, GLenum target
, GLint level
,
848 GLenum format
, GLenum type
, GLvoid
*pixels
,
849 struct gl_texture_object
*texObj
,
850 struct gl_texture_image
*texImage
)
852 struct st_context
*st
= st_context(ctx
);
853 struct pipe_context
*pipe
= st
->pipe
;
854 struct pipe_screen
*screen
= pipe
->screen
;
855 struct st_texture_image
*stImage
= st_texture_image(texImage
);
856 struct st_texture_object
*stObj
= st_texture_object(texObj
);
857 struct pipe_sampler_view
*src_view
=
858 st_get_texture_sampler_view(stObj
, pipe
);
859 const GLuint width
= texImage
->Width
;
860 const GLuint height
= texImage
->Height
;
861 struct pipe_surface
*dst_surface
;
862 struct pipe_resource
*dst_texture
;
863 struct pipe_transfer
*tex_xfer
;
864 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
865 PIPE_BIND_TRANSFER_READ
);
867 /* create temp / dest surface */
868 if (!util_create_rgba_surface(screen
, width
, height
, bind
,
869 &dst_texture
, &dst_surface
)) {
870 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
871 "in decompress_with_blit()");
875 /* blit/render/decompress */
876 util_blit_pixels_tex(st
->blit
,
877 src_view
, /* pipe_resource (src) */
878 0, 0, /* src x0, y0 */
879 width
, height
, /* src x1, y1 */
880 dst_surface
, /* pipe_surface (dst) */
881 0, 0, /* dst x0, y0 */
882 width
, height
, /* dst x1, y1 */
884 PIPE_TEX_MIPFILTER_NEAREST
);
886 /* map the dst_surface so we can read from it */
887 tex_xfer
= pipe_get_transfer(st_context(ctx
)->pipe
,
888 dst_texture
, 0, 0, 0,
890 0, 0, width
, height
);
892 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
894 /* copy/pack data into user buffer */
895 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
897 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
898 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
900 for (row
= 0; row
< height
; row
++) {
901 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
902 height
, format
, type
, row
, 0);
903 memcpy(dest
, map
, bytesPerRow
);
904 map
+= tex_xfer
->stride
;
906 pipe_transfer_unmap(pipe
, tex_xfer
);
909 /* format translation via floats */
911 for (row
= 0; row
< height
; row
++) {
912 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
913 GLfloat rgba
[4 * MAX_WIDTH
];
914 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
915 height
, format
, type
, row
, 0);
917 if (ST_DEBUG
& DEBUG_FALLBACK
)
918 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
920 /* get float[4] rgba row from surface */
921 pipe_get_tile_rgba(pipe
, tex_xfer
, 0, row
, width
, 1, rgba
);
923 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
924 type
, dest
, &ctx
->Pack
, transferOps
);
928 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
930 pipe
->transfer_destroy(pipe
, tex_xfer
);
932 /* destroy the temp / dest surface */
933 util_destroy_rgba_surface(dst_texture
, dst_surface
);
939 * Need to map texture image into memory before copying image data,
943 st_get_tex_image(struct gl_context
* ctx
, GLenum target
, GLint level
,
944 GLenum format
, GLenum type
, GLvoid
* pixels
,
945 struct gl_texture_object
*texObj
,
946 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
948 struct st_context
*st
= st_context(ctx
);
949 struct st_texture_image
*stImage
= st_texture_image(texImage
);
950 const GLuint dstImageStride
=
951 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
957 util_format_is_s3tc(stImage
->pt
->format
) &&
959 /* Need to decompress the texture.
960 * We'll do this by rendering a textured quad.
961 * Note that we only expect RGBA formats (no Z/depth formats).
963 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
970 /* Image is stored in hardware format in a buffer managed by the
971 * kernel. Need to explicitly map and unmap it.
973 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
974 PIPE_TRANSFER_READ
, 0, 0,
976 stImage
->base
.Height
);
977 /* compute stride in texels from stride in bytes */
978 texImage
->RowStride
= stImage
->transfer
->stride
979 * util_format_get_blockwidth(stImage
->pt
->format
)
980 / util_format_get_blocksize(stImage
->pt
->format
);
983 /* Otherwise, the image should actually be stored in
984 * texImage->Data. This is pretty confusing for
985 * everybody, I'd much prefer to separate the two functions of
986 * texImage->Data - storage for texture images in main memory
987 * and access (ie mappings) of images. In other words, we'd
988 * create a new texImage->Map field and leave Data simply for
991 assert(texImage
->Data
);
994 depth
= texImage
->Depth
;
997 dest
= (GLubyte
*) pixels
;
999 _mesa_set_fetch_functions(texImage
, get_texture_dims(target
));
1001 for (i
= 0; i
< depth
; i
++) {
1002 if (compressed_dst
) {
1003 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
1007 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
1011 if (stImage
->pt
&& i
+ 1 < depth
) {
1012 /* unmap this slice */
1013 st_texture_image_unmap(st
, stImage
);
1014 /* map next slice of 3D texture */
1015 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
1016 PIPE_TRANSFER_READ
, 0, 0,
1017 stImage
->base
.Width
,
1018 stImage
->base
.Height
);
1019 dest
+= dstImageStride
;
1023 texImage
->Depth
= depth
;
1027 st_texture_image_unmap(st
, stImage
);
1028 texImage
->Data
= NULL
;
1034 st_GetTexImage(struct gl_context
* ctx
, GLenum target
, GLint level
,
1035 GLenum format
, GLenum type
, GLvoid
* pixels
,
1036 struct gl_texture_object
*texObj
,
1037 struct gl_texture_image
*texImage
)
1039 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1045 st_GetCompressedTexImage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1047 struct gl_texture_object
*texObj
,
1048 struct gl_texture_image
*texImage
)
1050 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1057 st_TexSubimage(struct gl_context
*ctx
, GLint dims
, GLenum target
, GLint level
,
1058 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1059 GLint width
, GLint height
, GLint depth
,
1060 GLenum format
, GLenum type
, const void *pixels
,
1061 const struct gl_pixelstore_attrib
*packing
,
1062 struct gl_texture_object
*texObj
,
1063 struct gl_texture_image
*texImage
)
1065 struct st_context
*st
= st_context(ctx
);
1066 struct pipe_screen
*screen
= st
->pipe
->screen
;
1067 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1068 GLuint dstRowStride
;
1069 const GLuint srcImageStride
=
1070 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1073 /* init to silence warning only: */
1074 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1076 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1077 _mesa_lookup_enum_by_nr(target
),
1078 level
, xoffset
, yoffset
, width
, height
);
1081 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1082 type
, pixels
, packing
, "glTexSubImage2D");
1086 /* See if we can do texture compression with a blit/render.
1088 if (!ctx
->Mesa_DXTn
&&
1089 _mesa_is_format_compressed(texImage
->TexFormat
) &&
1090 screen
->is_format_supported(screen
,
1091 stImage
->pt
->format
,
1092 stImage
->pt
->target
, 0,
1093 PIPE_BIND_RENDER_TARGET
, 0)) {
1094 if (compress_with_blit(ctx
, target
, level
,
1095 xoffset
, yoffset
, zoffset
,
1096 width
, height
, depth
,
1097 format
, type
, pixels
, packing
, texImage
)) {
1102 /* Map buffer if necessary. Need to lock to prevent other contexts
1103 * from uploading the buffer under us.
1106 if (format
== GL_DEPTH_COMPONENT
&&
1107 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1108 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1110 transfer_usage
= PIPE_TRANSFER_WRITE
;
1112 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1118 if (!texImage
->Data
) {
1119 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1123 src
= (const GLubyte
*) pixels
;
1124 dstRowStride
= stImage
->transfer
->stride
;
1126 for (i
= 0; i
< depth
; i
++) {
1127 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1128 texImage
->TexFormat
,
1132 texImage
->ImageOffsets
,
1134 format
, type
, src
, packing
)) {
1135 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1138 if (stImage
->pt
&& i
+ 1 < depth
) {
1139 /* unmap this slice */
1140 st_texture_image_unmap(st
, stImage
);
1141 /* map next slice of 3D texture */
1142 texImage
->Data
= st_texture_image_map(st
, stImage
,
1147 src
+= srcImageStride
;
1152 _mesa_unmap_teximage_pbo(ctx
, packing
);
1154 if (stImage
->pt
&& texImage
->Data
) {
1155 st_texture_image_unmap(st
, stImage
);
1156 texImage
->Data
= NULL
;
1163 st_TexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1164 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1165 GLsizei width
, GLsizei height
, GLsizei depth
,
1166 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1167 const struct gl_pixelstore_attrib
*packing
,
1168 struct gl_texture_object
*texObj
,
1169 struct gl_texture_image
*texImage
)
1171 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1172 width
, height
, depth
, format
, type
,
1173 pixels
, packing
, texObj
, texImage
);
1178 st_TexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1179 GLint xoffset
, GLint yoffset
,
1180 GLsizei width
, GLsizei height
,
1181 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1182 const struct gl_pixelstore_attrib
*packing
,
1183 struct gl_texture_object
*texObj
,
1184 struct gl_texture_image
*texImage
)
1186 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1187 width
, height
, 1, format
, type
,
1188 pixels
, packing
, texObj
, texImage
);
1193 st_TexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1194 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1195 const GLvoid
* pixels
,
1196 const struct gl_pixelstore_attrib
*packing
,
1197 struct gl_texture_object
*texObj
,
1198 struct gl_texture_image
*texImage
)
1200 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1201 format
, type
, pixels
, packing
, texObj
, texImage
);
1206 st_CompressedTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1207 GLint xoffset
, GLsizei width
,
1209 GLsizei imageSize
, const GLvoid
*data
,
1210 struct gl_texture_object
*texObj
,
1211 struct gl_texture_image
*texImage
)
1218 st_CompressedTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1219 GLint xoffset
, GLint yoffset
,
1220 GLsizei width
, GLint height
,
1222 GLsizei imageSize
, const GLvoid
*data
,
1223 struct gl_texture_object
*texObj
,
1224 struct gl_texture_image
*texImage
)
1226 struct st_context
*st
= st_context(ctx
);
1227 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1231 enum pipe_format pformat
;
1234 pformat
= stImage
->pt
->format
;
1236 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1237 PIPE_TRANSFER_WRITE
,
1241 srcBlockStride
= util_format_get_stride(pformat
, width
);
1242 dstBlockStride
= stImage
->transfer
->stride
;
1244 assert(stImage
->pt
);
1245 /* TODO find good values for block and strides */
1246 /* TODO also adjust texImage->data for yoffset/xoffset */
1250 if (!texImage
->Data
) {
1251 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1255 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1256 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1258 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1259 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1260 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1261 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1262 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1266 st_texture_image_unmap(st
, stImage
);
1267 texImage
->Data
= NULL
;
1273 st_CompressedTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1274 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1275 GLsizei width
, GLint height
, GLint depth
,
1277 GLsizei imageSize
, const GLvoid
*data
,
1278 struct gl_texture_object
*texObj
,
1279 struct gl_texture_image
*texImage
)
1287 * Do a CopyTexSubImage operation using a read transfer from the source,
1288 * a write transfer to the destination and get_tile()/put_tile() to access
1289 * the pixels/texels.
1291 * Note: srcY=0=TOP of renderbuffer
1294 fallback_copy_texsubimage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1295 struct st_renderbuffer
*strb
,
1296 struct st_texture_image
*stImage
,
1298 GLint destX
, GLint destY
, GLint destZ
,
1299 GLint srcX
, GLint srcY
,
1300 GLsizei width
, GLsizei height
)
1302 struct st_context
*st
= st_context(ctx
);
1303 struct pipe_context
*pipe
= st
->pipe
;
1304 struct pipe_transfer
*src_trans
;
1306 enum pipe_transfer_usage transfer_usage
;
1308 if (ST_DEBUG
& DEBUG_FALLBACK
)
1309 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1311 assert(width
<= MAX_WIDTH
);
1313 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1314 srcY
= strb
->Base
.Height
- srcY
- height
;
1317 src_trans
= pipe_get_transfer(st_context(ctx
)->pipe
,
1324 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1325 baseFormat
== GL_DEPTH_STENCIL
) &&
1326 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1327 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1329 transfer_usage
= PIPE_TRANSFER_WRITE
;
1331 texDest
= st_texture_image_map(st
, stImage
, 0, transfer_usage
,
1332 destX
, destY
, width
, height
);
1334 if (baseFormat
== GL_DEPTH_COMPONENT
||
1335 baseFormat
== GL_DEPTH_STENCIL
) {
1336 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1337 ctx
->Pixel
.DepthBias
!= 0.0F
);
1340 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1341 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1350 /* To avoid a large temp memory allocation, do copy row by row */
1351 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1352 uint data
[MAX_WIDTH
];
1353 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1355 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1357 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1363 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1365 if (tempSrc
&& texDest
) {
1366 const GLint dims
= 2;
1367 const GLint dstRowStride
= stImage
->transfer
->stride
;
1368 struct gl_texture_image
*texImage
= &stImage
->base
;
1369 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1371 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1372 unpack
.Invert
= GL_TRUE
;
1375 /* get float/RGBA image from framebuffer */
1376 /* XXX this usually involves a lot of int/float conversion.
1377 * try to avoid that someday.
1379 pipe_get_tile_rgba(pipe
, src_trans
, 0, 0, width
, height
, tempSrc
);
1381 /* Store into texture memory.
1382 * Note that this does some special things such as pixel transfer
1383 * ops and format conversion. In particular, if the dest tex format
1384 * is actually RGBA but the user created the texture as GL_RGB we
1385 * need to fill-in/override the alpha channel with 1.0.
1387 _mesa_texstore(ctx
, dims
,
1388 texImage
->_BaseFormat
,
1389 texImage
->TexFormat
,
1393 texImage
->ImageOffsets
,
1395 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1399 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1406 st_texture_image_unmap(st
, stImage
);
1407 pipe
->transfer_destroy(pipe
, src_trans
);
1413 * If the format of the src renderbuffer and the format of the dest
1414 * texture are compatible (in terms of blitting), return a TGSI writemask
1415 * to be used during the blit.
1416 * If the src/dest are incompatible, return 0.
1419 compatible_src_dst_formats(struct gl_context
*ctx
,
1420 const struct gl_renderbuffer
*src
,
1421 const struct gl_texture_image
*dst
)
1423 /* Get logical base formats for the src and dest.
1424 * That is, use the user-requested formats and not the actual, device-
1426 * For example, the user may have requested an A8 texture but the
1427 * driver may actually be using an RGBA texture format. When we
1428 * copy/blit to that texture, we only want to copy the Alpha channel
1429 * and not the RGB channels.
1431 * Similarly, when the src FBO was created an RGB format may have been
1432 * requested but the driver actually chose an RGBA format. In that case,
1433 * we don't want to copy the undefined Alpha channel to the dest texture
1434 * (it should be 1.0).
1436 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1437 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1440 * XXX when we have red-only and red/green renderbuffers we'll need
1441 * to add more cases here (or implement a general-purpose routine that
1442 * queries the existance of the R,G,B,A channels in the src and dest).
1444 if (srcFormat
== dstFormat
) {
1445 /* This is the same as matching_base_formats, which should
1446 * always pass, as it did previously.
1448 return TGSI_WRITEMASK_XYZW
;
1450 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1451 /* Make sure that A in the dest is 1. The actual src format
1452 * may be RGBA and have undefined A values.
1454 return TGSI_WRITEMASK_XYZ
;
1456 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1457 /* Make sure that A in the dest is 1. The actual dst format
1458 * may be RGBA and will need A=1 to provide proper alpha values
1459 * when sampled later.
1461 return TGSI_WRITEMASK_XYZ
;
1464 if (ST_DEBUG
& DEBUG_FALLBACK
)
1465 debug_printf("%s failed for src %s, dst %s\n",
1467 _mesa_lookup_enum_by_nr(srcFormat
),
1468 _mesa_lookup_enum_by_nr(dstFormat
));
1479 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1480 * Note that the region to copy has already been clipped so we know we
1481 * won't read from outside the source renderbuffer's bounds.
1483 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1486 st_copy_texsubimage(struct gl_context
*ctx
,
1487 GLenum target
, GLint level
,
1488 GLint destX
, GLint destY
, GLint destZ
,
1489 GLint srcX
, GLint srcY
,
1490 GLsizei width
, GLsizei height
)
1492 struct gl_texture_unit
*texUnit
=
1493 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1494 struct gl_texture_object
*texObj
=
1495 _mesa_select_tex_object(ctx
, texUnit
, target
);
1496 struct gl_texture_image
*texImage
=
1497 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1498 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1499 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1500 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1501 struct st_renderbuffer
*strb
;
1502 struct st_context
*st
= st_context(ctx
);
1503 struct pipe_context
*pipe
= st
->pipe
;
1504 struct pipe_screen
*screen
= pipe
->screen
;
1505 enum pipe_format dest_format
, src_format
;
1506 GLboolean use_fallback
= GL_TRUE
;
1507 GLboolean matching_base_formats
;
1508 GLuint format_writemask
, sample_count
;
1509 struct pipe_surface
*dest_surface
= NULL
;
1510 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1512 /* make sure finalize_textures has been called?
1514 if (0) st_validate_state(st
);
1516 /* determine if copying depth or color data */
1517 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1518 texBaseFormat
== GL_DEPTH_STENCIL
) {
1519 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1522 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1523 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1526 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1527 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1531 sample_count
= strb
->surface
->texture
->nr_samples
;
1532 /* I believe this would be legal, presumably would need to do a resolve
1533 for color, and for depth/stencil spec says to just use one of the
1534 depth/stencil samples per pixel? Need some transfer clarifications. */
1535 assert(sample_count
< 2);
1561 if (width
< 0 || height
< 0)
1566 assert(strb
->surface
);
1567 assert(stImage
->pt
);
1569 src_format
= strb
->surface
->format
;
1570 dest_format
= stImage
->pt
->format
;
1573 * Determine if the src framebuffer and dest texture have the same
1574 * base format. We need this to detect a case such as the framebuffer
1575 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1576 * texture format stores RGBA we need to set A=1 (overriding the
1577 * framebuffer's alpha values). We can't do that with the blit or
1578 * textured-quad paths.
1580 matching_base_formats
=
1581 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1582 _mesa_get_format_base_format(texImage
->TexFormat
));
1583 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1585 if (ctx
->_ImageTransferState
== 0x0) {
1587 if (matching_base_formats
&&
1588 src_format
== dest_format
&&
1591 /* use surface_copy() / blit */
1592 struct pipe_subresource subdst
, subsrc
;
1593 subdst
.face
= stImage
->face
;
1594 subdst
.level
= stImage
->level
;
1595 subsrc
.face
= strb
->surface
->face
;
1596 subsrc
.level
= strb
->surface
->level
;
1598 /* for resource_copy_region(), y=0=top, always */
1599 pipe
->resource_copy_region(pipe
,
1603 destX
, destY
, destZ
,
1607 srcX
, srcY
, strb
->surface
->zslice
,
1610 use_fallback
= GL_FALSE
;
1612 else if (format_writemask
&&
1613 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1614 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1615 screen
->is_format_supported(screen
, src_format
,
1616 PIPE_TEXTURE_2D
, sample_count
,
1617 PIPE_BIND_SAMPLER_VIEW
,
1619 screen
->is_format_supported(screen
, dest_format
,
1621 PIPE_BIND_RENDER_TARGET
,
1623 /* draw textured quad to do the copy */
1625 struct pipe_subresource subsrc
;
1627 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1628 stImage
->face
, stImage
->level
,
1630 PIPE_BIND_RENDER_TARGET
);
1633 srcY1
= strb
->Base
.Height
- srcY
- height
;
1634 srcY0
= srcY1
+ height
;
1638 srcY1
= srcY0
+ height
;
1640 subsrc
.face
= strb
->surface
->face
;
1641 subsrc
.level
= strb
->surface
->level
;
1643 util_blit_pixels_writemask(st
->blit
,
1647 srcX
+ width
, srcY1
,
1648 strb
->surface
->zslice
,
1651 destX
+ width
, destY
+ height
,
1652 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1654 use_fallback
= GL_FALSE
;
1658 pipe_surface_reference(&dest_surface
, NULL
);
1662 /* software fallback */
1663 fallback_copy_texsubimage(ctx
, target
, level
,
1664 strb
, stImage
, texBaseFormat
,
1665 destX
, destY
, destZ
,
1666 srcX
, srcY
, width
, height
);
1673 st_CopyTexImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1674 GLenum internalFormat
,
1675 GLint x
, GLint y
, GLsizei width
, GLint border
)
1677 struct gl_texture_unit
*texUnit
=
1678 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1679 struct gl_texture_object
*texObj
=
1680 _mesa_select_tex_object(ctx
, texUnit
, target
);
1681 struct gl_texture_image
*texImage
=
1682 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1684 /* Setup or redefine the texture object, texture and texture
1685 * image. Don't populate yet.
1687 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1689 GL_RGBA
, CHAN_TYPE
, NULL
,
1690 &ctx
->DefaultPacking
, texObj
, texImage
);
1692 st_copy_texsubimage(ctx
, target
, level
,
1693 0, 0, 0, /* destX,Y,Z */
1694 x
, y
, width
, 1); /* src X, Y, size */
1699 st_CopyTexImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1700 GLenum internalFormat
,
1701 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1704 struct gl_texture_unit
*texUnit
=
1705 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1706 struct gl_texture_object
*texObj
=
1707 _mesa_select_tex_object(ctx
, texUnit
, target
);
1708 struct gl_texture_image
*texImage
=
1709 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1711 /* Setup or redefine the texture object, texture and texture
1712 * image. Don't populate yet.
1714 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1715 width
, height
, border
,
1716 GL_RGBA
, CHAN_TYPE
, NULL
,
1717 &ctx
->DefaultPacking
, texObj
, texImage
);
1719 st_copy_texsubimage(ctx
, target
, level
,
1720 0, 0, 0, /* destX,Y,Z */
1721 x
, y
, width
, height
); /* src X, Y, size */
1726 st_CopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1727 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1729 const GLint yoffset
= 0, zoffset
= 0;
1730 const GLsizei height
= 1;
1731 st_copy_texsubimage(ctx
, target
, level
,
1732 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1733 x
, y
, width
, height
); /* src X, Y, size */
1738 st_CopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1739 GLint xoffset
, GLint yoffset
,
1740 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1742 const GLint zoffset
= 0;
1743 st_copy_texsubimage(ctx
, target
, level
,
1744 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1745 x
, y
, width
, height
); /* src X, Y, size */
1750 st_CopyTexSubImage3D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1751 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1752 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1754 st_copy_texsubimage(ctx
, target
, level
,
1755 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1756 x
, y
, width
, height
); /* src X, Y, size */
1761 * Copy image data from stImage into the texture object 'stObj' at level
1765 copy_image_data_to_texture(struct st_context
*st
,
1766 struct st_texture_object
*stObj
,
1768 struct st_texture_image
*stImage
)
1772 const struct gl_texture_image
*dstImage
=
1773 stObj
->base
.Image
[stImage
->face
][stImage
->level
];
1775 assert(dstImage
->Width
== stImage
->base
.Width
);
1776 assert(dstImage
->Height
== stImage
->base
.Height
);
1777 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1781 /* Copy potentially with the blitter:
1783 st_texture_image_copy(st
->pipe
,
1784 stObj
->pt
, dstLevel
, /* dest texture, level */
1785 stImage
->pt
, stImage
->level
, /* src texture, level */
1788 pipe_resource_reference(&stImage
->pt
, NULL
);
1790 else if (stImage
->base
.Data
) {
1791 st_texture_image_data(st
,
1796 stImage
->base
.RowStride
*
1797 util_format_get_blocksize(stObj
->pt
->format
),
1798 stImage
->base
.RowStride
*
1799 stImage
->base
.Height
*
1800 util_format_get_blocksize(stObj
->pt
->format
));
1801 _mesa_align_free(stImage
->base
.Data
);
1802 stImage
->base
.Data
= NULL
;
1805 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1810 * Called during state validation. When this function is finished,
1811 * the texture object should be ready for rendering.
1812 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1815 st_finalize_texture(struct gl_context
*ctx
,
1816 struct pipe_context
*pipe
,
1817 struct gl_texture_object
*tObj
)
1819 struct st_context
*st
= st_context(ctx
);
1820 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1821 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1823 struct st_texture_image
*firstImage
;
1824 enum pipe_format firstImageFormat
;
1826 if (stObj
->base
._Complete
) {
1827 /* The texture is complete and we know exactly how many mipmap levels
1828 * are present/needed. This is conditional because we may be called
1829 * from the st_generate_mipmap() function when the texture object is
1830 * incomplete. In that case, we'll have set stObj->lastLevel before
1833 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1834 stObj
->base
.MinFilter
== GL_NEAREST
)
1835 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1837 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1840 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1843 /* If both firstImage and stObj point to a texture which can contain
1844 * all active images, favour firstImage. Note that because of the
1845 * completeness requirement, we know that the image dimensions
1848 if (firstImage
->pt
&&
1849 firstImage
->pt
!= stObj
->pt
&&
1850 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1851 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1852 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1855 /* Find gallium format for the Mesa texture */
1856 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1858 /* If we already have a gallium texture, check that it matches the texture
1859 * object's format, target, size, num_levels, etc.
1862 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1863 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1864 stObj
->pt
->last_level
< stObj
->lastLevel
||
1865 stObj
->pt
->width0
!= stObj
->width0
||
1866 stObj
->pt
->height0
!= stObj
->height0
||
1867 stObj
->pt
->depth0
!= stObj
->depth0
)
1869 /* The gallium texture does not match the Mesa texture so delete the
1870 * gallium texture now. We'll make a new one below.
1872 pipe_resource_reference(&stObj
->pt
, NULL
);
1873 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1874 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1878 /* May need to create a new gallium texture:
1881 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1883 stObj
->pt
= st_texture_create(st
,
1884 gl_target_to_pipe(stObj
->base
.Target
),
1893 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1898 /* Pull in any images not in the object's texture:
1900 for (face
= 0; face
< nr_faces
; face
++) {
1902 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1903 struct st_texture_image
*stImage
=
1904 st_texture_image(stObj
->base
.Image
[face
][level
]);
1906 /* Need to import images in main memory or held in other textures.
1908 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1909 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1919 * Returns pointer to a default/dummy texture.
1920 * This is typically used when the current shader has tex/sample instructions
1921 * but the user has not provided a (any) texture(s).
1923 struct gl_texture_object
*
1924 st_get_default_texture(struct st_context
*st
)
1926 if (!st
->default_texture
) {
1927 static const GLenum target
= GL_TEXTURE_2D
;
1928 GLubyte pixels
[16][16][4];
1929 struct gl_texture_object
*texObj
;
1930 struct gl_texture_image
*texImg
;
1933 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1934 * when attempting to sample incomplete textures.
1936 for (i
= 0; i
< 16; i
++) {
1937 for (j
= 0; j
< 16; j
++) {
1938 pixels
[i
][j
][0] = 0;
1939 pixels
[i
][j
][1] = 0;
1940 pixels
[i
][j
][2] = 0;
1941 pixels
[i
][j
][3] = 255;
1945 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1947 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1949 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1950 16, 16, 1, 0, /* w, h, d, border */
1953 st_TexImage(st
->ctx
, 2, target
,
1954 0, GL_RGBA
, /* level, intformat */
1955 16, 16, 1, 0, /* w, h, d, border */
1956 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1957 &st
->ctx
->DefaultPacking
,
1961 texObj
->MinFilter
= GL_NEAREST
;
1962 texObj
->MagFilter
= GL_NEAREST
;
1963 texObj
->_Complete
= GL_TRUE
;
1965 st
->default_texture
= texObj
;
1967 return st
->default_texture
;
1972 st_init_texture_functions(struct dd_function_table
*functions
)
1974 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1975 functions
->TexImage1D
= st_TexImage1D
;
1976 functions
->TexImage2D
= st_TexImage2D
;
1977 functions
->TexImage3D
= st_TexImage3D
;
1978 functions
->TexSubImage1D
= st_TexSubImage1D
;
1979 functions
->TexSubImage2D
= st_TexSubImage2D
;
1980 functions
->TexSubImage3D
= st_TexSubImage3D
;
1981 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1982 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1983 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1984 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1985 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1986 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1987 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1988 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1989 functions
->GenerateMipmap
= st_generate_mipmap
;
1991 functions
->GetTexImage
= st_GetTexImage
;
1993 /* compressed texture functions */
1994 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1995 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1997 functions
->NewTextureObject
= st_NewTextureObject
;
1998 functions
->NewTextureImage
= st_NewTextureImage
;
1999 functions
->DeleteTexture
= st_DeleteTextureObject
;
2000 functions
->FreeTexImageData
= st_FreeTextureImageData
;
2002 functions
->TextureMemCpy
= do_memcpy
;
2004 /* XXX Temporary until we can query pipe's texture sizes */
2005 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;