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_texture.h"
48 #include "state_tracker/st_format.h"
49 #include "state_tracker/st_texture.h"
50 #include "state_tracker/st_gen_mipmap.h"
51 #include "state_tracker/st_inlines.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
, 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.
260 guess_and_alloc_texture(struct st_context
*st
,
261 struct st_texture_object
*stObj
,
262 const struct st_texture_image
*stImage
)
264 const GLuint dims
= get_texture_dims(stObj
->base
.Target
);
265 GLuint level
, lastLevel
, width
, height
, depth
;
267 enum pipe_format fmt
;
269 DBG("%s\n", __FUNCTION__
);
273 level
= stImage
->level
;
274 width
= stImage
->base
.Width2
; /* size w/out border */
275 height
= stImage
->base
.Height2
;
276 depth
= stImage
->base
.Depth2
;
282 /* Depending on the image's size, we can't always make a guess here.
285 if ( (dims
>= 1 && width
== 1) ||
286 (dims
>= 2 && height
== 1) ||
287 (dims
>= 3 && depth
== 1) ) {
288 /* we can't determine the image size at level=0 */
289 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
294 /* grow the image size until we hit level = 0 */
307 /* At this point, (width x height x depth) is the expected size of
308 * the level=0 mipmap image.
311 /* Guess a reasonable value for lastLevel. With OpenGL we have no
312 * idea how many mipmap levels will be in a texture until we start
313 * to render with it. Make an educated guess here but be prepared
314 * to re-allocating a texture buffer with space for more (or fewer)
315 * mipmap levels later.
317 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
318 stObj
->base
.MinFilter
== GL_LINEAR
||
319 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
320 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
321 !stObj
->base
.GenerateMipmap
&&
322 stImage
->level
== 0) {
323 /* only alloc space for a single mipmap level */
327 /* alloc space for a full mipmap */
328 GLuint l2width
= util_logbase2(width
);
329 GLuint l2height
= util_logbase2(height
);
330 GLuint l2depth
= util_logbase2(depth
);
331 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
334 /* Save the level=0 dimensions */
335 stObj
->width0
= width
;
336 stObj
->height0
= height
;
337 stObj
->depth0
= depth
;
339 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
341 bindings
= default_bindings(st
, fmt
);
343 stObj
->pt
= st_texture_create(st
,
344 gl_target_to_pipe(stObj
->base
.Target
),
352 DBG("%s - success\n", __FUNCTION__
);
357 * Adjust pixel unpack params and image dimensions to strip off the
359 * Gallium doesn't support texture borders. They've seldem been used
360 * and seldom been implemented correctly anyway.
361 * \param unpackNew returns the new pixel unpack parameters
364 strip_texture_border(GLint border
,
365 GLint
*width
, GLint
*height
, GLint
*depth
,
366 const struct gl_pixelstore_attrib
*unpack
,
367 struct gl_pixelstore_attrib
*unpackNew
)
369 assert(border
> 0); /* sanity check */
371 *unpackNew
= *unpack
;
373 if (unpackNew
->RowLength
== 0)
374 unpackNew
->RowLength
= *width
;
376 if (depth
&& unpackNew
->ImageHeight
== 0)
377 unpackNew
->ImageHeight
= *height
;
379 unpackNew
->SkipPixels
+= border
;
381 unpackNew
->SkipRows
+= border
;
383 unpackNew
->SkipImages
+= border
;
386 *width
= *width
- 2 * border
;
387 if (height
&& *height
>= 3)
388 *height
= *height
- 2 * border
;
389 if (depth
&& *depth
>= 3)
390 *depth
= *depth
- 2 * border
;
395 * Try to do texture compression via rendering. If the Gallium driver
396 * can render into a compressed surface this will allow us to do texture
398 * \return GL_TRUE for success, GL_FALSE for failure
401 compress_with_blit(GLcontext
* ctx
,
402 GLenum target
, GLint level
,
403 GLint xoffset
, GLint yoffset
, GLint zoffset
,
404 GLint width
, GLint height
, GLint depth
,
405 GLenum format
, GLenum type
, const void *pixels
,
406 const struct gl_pixelstore_attrib
*unpack
,
407 struct gl_texture_image
*texImage
)
409 const GLuint dstImageOffsets
[1] = {0};
410 struct st_texture_image
*stImage
= st_texture_image(texImage
);
411 struct st_context
*st
= st_context(ctx
);
412 struct pipe_context
*pipe
= st
->pipe
;
413 struct pipe_screen
*screen
= pipe
->screen
;
414 gl_format mesa_format
;
415 struct pipe_resource templ
;
416 struct pipe_resource
*src_tex
;
417 struct pipe_sampler_view view_templ
;
418 struct pipe_sampler_view
*src_view
;
419 struct pipe_surface
*dst_surface
;
420 struct pipe_transfer
*tex_xfer
;
424 /* XXX: Can this happen? Should we assert? */
428 /* get destination surface (in the compressed texture) */
429 dst_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
430 stImage
->face
, stImage
->level
, 0,
431 PIPE_BIND_BLIT_DESTINATION
);
433 /* can't render into this format (or other problem) */
437 /* Choose format for the temporary RGBA texture image.
439 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
444 /* Create the temporary source texture
446 memset(&templ
, 0, sizeof(templ
));
447 templ
.target
= PIPE_TEXTURE_2D
;
448 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
);
449 templ
.width0
= width
;
450 templ
.height0
= height
;
452 templ
.last_level
= 0;
453 templ
.usage
= PIPE_USAGE_DEFAULT
;
454 templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
455 src_tex
= screen
->resource_create(screen
, &templ
);
460 /* Put user's tex data into the temporary texture
462 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
), src_tex
,
463 0, 0, 0, /* face, level are zero */
465 0, 0, width
, height
); /* x, y, w, h */
466 map
= pipe_transfer_map(pipe
, tex_xfer
);
468 _mesa_texstore(ctx
, 2, GL_RGBA
, mesa_format
,
470 0, 0, 0, /* dest x/y/z offset */
471 tex_xfer
->stride
, /* dest row stride (bytes) */
472 dstImageOffsets
, /* image offsets (for 3D only) */
473 width
, height
, 1, /* size */
474 format
, type
, /* source format/type */
475 pixels
, /* source data */
476 unpack
); /* source data packing */
478 pipe_transfer_unmap(pipe
, tex_xfer
);
479 pipe
->transfer_destroy(pipe
, tex_xfer
);
481 /* Create temporary sampler view */
482 u_sampler_view_default_template(&view_templ
,
485 src_view
= pipe
->create_sampler_view(pipe
, src_tex
, &view_templ
);
488 /* copy / compress image */
489 util_blit_pixels_tex(st
->blit
,
490 src_view
, /* sampler view (src) */
491 0, 0, /* src x0, y0 */
492 width
, height
, /* src x1, y1 */
493 dst_surface
, /* pipe_surface (dst) */
494 xoffset
, yoffset
, /* dst x0, y0 */
495 xoffset
+ width
, /* dst x1 */
496 yoffset
+ height
, /* dst y1 */
498 PIPE_TEX_MIPFILTER_NEAREST
);
500 pipe_surface_reference(&dst_surface
, NULL
);
501 pipe_resource_reference(&src_tex
, NULL
);
502 pipe_sampler_view_reference(&src_view
, NULL
);
509 * Do glTexImage1/2/3D().
512 st_TexImage(GLcontext
* ctx
,
514 GLenum target
, GLint level
,
515 GLint internalFormat
,
516 GLint width
, GLint height
, GLint depth
,
518 GLenum format
, GLenum type
, const void *pixels
,
519 const struct gl_pixelstore_attrib
*unpack
,
520 struct gl_texture_object
*texObj
,
521 struct gl_texture_image
*texImage
,
522 GLsizei imageSize
, GLboolean compressed_src
)
524 struct st_context
*st
= st_context(ctx
);
525 struct pipe_screen
*screen
= st
->pipe
->screen
;
526 struct st_texture_object
*stObj
= st_texture_object(texObj
);
527 struct st_texture_image
*stImage
= st_texture_image(texImage
);
528 GLuint dstRowStride
= 0;
529 struct gl_pixelstore_attrib unpackNB
;
530 enum pipe_transfer_usage transfer_usage
= 0;
532 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
533 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
535 /* The Mesa/Gallium state tracker does not implement the imaging extensions
536 * such as convolution.
538 assert(!ctx
->Extensions
.ARB_imaging
);
539 assert(!ctx
->Extensions
.EXT_convolution
);
541 /* switch to "normal" */
542 if (stObj
->surface_based
) {
543 _mesa_clear_texture_object(ctx
, texObj
);
544 stObj
->surface_based
= GL_FALSE
;
547 /* gallium does not support texture borders, strip it off */
549 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
551 texImage
->Width
= width
;
552 texImage
->Height
= height
;
553 texImage
->Depth
= depth
;
554 texImage
->Border
= 0;
558 assert(texImage
->Width
== width
);
559 assert(texImage
->Height
== height
);
560 assert(texImage
->Depth
== depth
);
563 stImage
->face
= _mesa_tex_target_to_face(target
);
564 stImage
->level
= level
;
566 _mesa_set_fetch_functions(texImage
, dims
);
568 /* Release the reference to a potentially orphaned buffer.
569 * Release any old malloced memory.
572 pipe_resource_reference(&stImage
->pt
, NULL
);
573 assert(!texImage
->Data
);
575 else if (texImage
->Data
) {
576 _mesa_align_free(texImage
->Data
);
580 * See if the new image is somehow incompatible with the existing
581 * mipmap. If so, free the old mipmap.
584 if (level
> (GLint
) stObj
->pt
->last_level
||
585 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
586 stImage
->face
, stImage
->level
)) {
588 pipe_resource_reference(&stObj
->pt
, NULL
);
590 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
594 if (width
== 0 || height
== 0 || depth
== 0) {
595 /* stop after freeing old image */
600 guess_and_alloc_texture(st
, stObj
, stImage
);
602 /* Probably out of memory.
603 * Try flushing any pending rendering, then retry.
606 guess_and_alloc_texture(st
, stObj
, stImage
);
608 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
614 assert(!stImage
->pt
);
616 /* Check if this texture image can live inside the texture object's buffer.
617 * If so, store the image there. Otherwise the image will temporarily live
621 st_texture_match_image(stObj
->pt
, &stImage
->base
,
622 stImage
->face
, stImage
->level
)) {
624 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
629 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
631 /* Pixel data may come from regular user memory or a PBO. For the later,
632 * do bounds checking and map the PBO to read pixels data from it.
634 * XXX we should try to use a GPU-accelerated path to copy the image data
635 * from the PBO to the texture.
637 if (compressed_src
) {
638 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
640 "glCompressedTexImage");
643 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
645 pixels
, unpack
, "glTexImage");
648 /* See if we can do texture compression with a blit/render.
650 if (!compressed_src
&&
652 _mesa_is_format_compressed(texImage
->TexFormat
) &&
653 screen
->is_format_supported(screen
,
656 PIPE_BIND_RENDER_TARGET
, 0)) {
660 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
661 format
, type
, pixels
, unpack
, texImage
)) {
667 * Prepare to store the texture data. Either map the gallium texture buffer
668 * memory or malloc space for it.
671 /* Store the image in the gallium texture memory buffer */
672 if (format
== GL_DEPTH_COMPONENT
&&
673 util_format_is_depth_and_stencil(stImage
->pt
->format
))
674 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
676 transfer_usage
= PIPE_TRANSFER_WRITE
;
678 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
679 transfer_usage
, 0, 0, width
, height
);
680 if(stImage
->transfer
)
681 dstRowStride
= stImage
->transfer
->stride
;
684 /* Allocate regular memory and store the image there temporarily. */
685 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
686 width
, height
, depth
);
687 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
689 texImage
->Data
= _mesa_align_malloc(imageSize
, 16);
692 if (!texImage
->Data
) {
693 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
698 /* We've allocated texture memory, but have no pixel data - all done. */
702 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
703 width
, height
, depth
, width
, dstRowStride
);
705 /* Copy user texture image into the texture buffer.
707 if (compressed_src
) {
708 const GLuint srcRowStride
=
709 _mesa_format_row_stride(texImage
->TexFormat
, width
);
710 if (dstRowStride
== srcRowStride
) {
711 memcpy(texImage
->Data
, pixels
, imageSize
);
714 char *dst
= texImage
->Data
;
715 const char *src
= pixels
;
716 GLuint i
, bw
, bh
, lines
;
717 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
718 lines
= (height
+ bh
- 1) / bh
;
720 for (i
= 0; i
< lines
; ++i
) {
721 memcpy(dst
, src
, srcRowStride
);
728 const GLuint srcImageStride
=
729 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
731 const GLubyte
*src
= (const GLubyte
*) pixels
;
733 for (i
= 0; i
< depth
; i
++) {
734 if (!_mesa_texstore(ctx
, dims
,
735 texImage
->_BaseFormat
,
738 0, 0, 0, /* dstX/Y/Zoffset */
740 texImage
->ImageOffsets
,
742 format
, type
, src
, unpack
)) {
743 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
746 if (stImage
->pt
&& i
+ 1 < depth
) {
747 /* unmap this slice */
748 st_texture_image_unmap(st
, stImage
);
749 /* map next slice of 3D texture */
750 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
751 transfer_usage
, 0, 0,
753 src
+= srcImageStride
;
759 _mesa_unmap_teximage_pbo(ctx
, unpack
);
761 if (stImage
->pt
&& texImage
->Data
) {
762 st_texture_image_unmap(st
, stImage
);
763 texImage
->Data
= NULL
;
769 st_TexImage3D(GLcontext
* ctx
,
770 GLenum target
, GLint level
,
771 GLint internalFormat
,
772 GLint width
, GLint height
, GLint depth
,
774 GLenum format
, GLenum type
, const void *pixels
,
775 const struct gl_pixelstore_attrib
*unpack
,
776 struct gl_texture_object
*texObj
,
777 struct gl_texture_image
*texImage
)
779 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
780 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
786 st_TexImage2D(GLcontext
* ctx
,
787 GLenum target
, GLint level
,
788 GLint internalFormat
,
789 GLint width
, GLint height
, GLint border
,
790 GLenum format
, GLenum type
, const void *pixels
,
791 const struct gl_pixelstore_attrib
*unpack
,
792 struct gl_texture_object
*texObj
,
793 struct gl_texture_image
*texImage
)
795 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
796 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
801 st_TexImage1D(GLcontext
* ctx
,
802 GLenum target
, GLint level
,
803 GLint internalFormat
,
804 GLint width
, GLint border
,
805 GLenum format
, GLenum type
, const void *pixels
,
806 const struct gl_pixelstore_attrib
*unpack
,
807 struct gl_texture_object
*texObj
,
808 struct gl_texture_image
*texImage
)
810 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
811 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
816 st_CompressedTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
817 GLint internalFormat
,
818 GLint width
, GLint height
, GLint border
,
819 GLsizei imageSize
, const GLvoid
*data
,
820 struct gl_texture_object
*texObj
,
821 struct gl_texture_image
*texImage
)
823 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
824 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
830 * glGetTexImage() helper: decompress a compressed texture by rendering
831 * a textured quad. Store the results in the user's buffer.
834 decompress_with_blit(GLcontext
* ctx
, GLenum target
, GLint level
,
835 GLenum format
, GLenum type
, GLvoid
*pixels
,
836 struct gl_texture_object
*texObj
,
837 struct gl_texture_image
*texImage
)
839 struct st_context
*st
= st_context(ctx
);
840 struct pipe_context
*pipe
= st
->pipe
;
841 struct pipe_screen
*screen
= pipe
->screen
;
842 struct st_texture_image
*stImage
= st_texture_image(texImage
);
843 struct st_texture_object
*stObj
= st_texture_object(texObj
);
844 struct pipe_sampler_view
*src_view
=
845 st_get_texture_sampler_view(stObj
, pipe
);
846 const GLuint width
= texImage
->Width
;
847 const GLuint height
= texImage
->Height
;
848 struct pipe_surface
*dst_surface
;
849 struct pipe_resource
*dst_texture
;
850 struct pipe_transfer
*tex_xfer
;
851 unsigned bind
= (PIPE_BIND_BLIT_DESTINATION
|
852 PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
853 PIPE_BIND_TRANSFER_READ
);
855 /* create temp / dest surface */
856 if (!util_create_rgba_surface(screen
, width
, height
, bind
,
857 &dst_texture
, &dst_surface
)) {
858 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
859 "in decompress_with_blit()");
863 /* blit/render/decompress */
864 util_blit_pixels_tex(st
->blit
,
865 src_view
, /* pipe_resource (src) */
866 0, 0, /* src x0, y0 */
867 width
, height
, /* src x1, y1 */
868 dst_surface
, /* pipe_surface (dst) */
869 0, 0, /* dst x0, y0 */
870 width
, height
, /* dst x1, y1 */
872 PIPE_TEX_MIPFILTER_NEAREST
);
874 /* map the dst_surface so we can read from it */
875 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
),
876 dst_texture
, 0, 0, 0,
878 0, 0, width
, height
);
880 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
882 /* copy/pack data into user buffer */
883 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
885 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
886 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
888 for (row
= 0; row
< height
; row
++) {
889 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
890 height
, format
, type
, row
, 0);
891 memcpy(dest
, map
, bytesPerRow
);
892 map
+= tex_xfer
->stride
;
894 pipe_transfer_unmap(pipe
, tex_xfer
);
897 /* format translation via floats */
899 for (row
= 0; row
< height
; row
++) {
900 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
901 GLfloat rgba
[4 * MAX_WIDTH
];
902 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
903 height
, format
, type
, row
, 0);
905 if (ST_DEBUG
& DEBUG_FALLBACK
)
906 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
908 /* get float[4] rgba row from surface */
909 pipe_get_tile_rgba(pipe
, tex_xfer
, 0, row
, width
, 1, rgba
);
911 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
912 type
, dest
, &ctx
->Pack
, transferOps
);
916 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
918 pipe
->transfer_destroy(pipe
, tex_xfer
);
920 /* destroy the temp / dest surface */
921 util_destroy_rgba_surface(dst_texture
, dst_surface
);
927 * Need to map texture image into memory before copying image data,
931 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
932 GLenum format
, GLenum type
, GLvoid
* pixels
,
933 struct gl_texture_object
*texObj
,
934 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
936 struct st_context
*st
= st_context(ctx
);
937 struct st_texture_image
*stImage
= st_texture_image(texImage
);
938 const GLuint dstImageStride
=
939 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
945 util_format_is_s3tc(stImage
->pt
->format
) &&
947 /* Need to decompress the texture.
948 * We'll do this by rendering a textured quad.
949 * Note that we only expect RGBA formats (no Z/depth formats).
951 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
958 /* Image is stored in hardware format in a buffer managed by the
959 * kernel. Need to explicitly map and unmap it.
961 unsigned face
= _mesa_tex_target_to_face(target
);
963 st_teximage_flush_before_map(st
, stImage
->pt
, face
, level
,
966 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
967 PIPE_TRANSFER_READ
, 0, 0,
969 stImage
->base
.Height
);
970 /* compute stride in texels from stride in bytes */
971 texImage
->RowStride
= stImage
->transfer
->stride
972 * util_format_get_blockwidth(stImage
->pt
->format
)
973 / util_format_get_blocksize(stImage
->pt
->format
);
976 /* Otherwise, the image should actually be stored in
977 * texImage->Data. This is pretty confusing for
978 * everybody, I'd much prefer to separate the two functions of
979 * texImage->Data - storage for texture images in main memory
980 * and access (ie mappings) of images. In other words, we'd
981 * create a new texImage->Map field and leave Data simply for
984 assert(texImage
->Data
);
987 depth
= texImage
->Depth
;
990 dest
= (GLubyte
*) pixels
;
992 for (i
= 0; i
< depth
; i
++) {
993 if (compressed_dst
) {
994 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
998 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
1002 if (stImage
->pt
&& i
+ 1 < depth
) {
1003 /* unmap this slice */
1004 st_texture_image_unmap(st
, stImage
);
1005 /* map next slice of 3D texture */
1006 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
1007 PIPE_TRANSFER_READ
, 0, 0,
1008 stImage
->base
.Width
,
1009 stImage
->base
.Height
);
1010 dest
+= dstImageStride
;
1014 texImage
->Depth
= depth
;
1018 st_texture_image_unmap(st
, stImage
);
1019 texImage
->Data
= NULL
;
1025 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
1026 GLenum format
, GLenum type
, GLvoid
* pixels
,
1027 struct gl_texture_object
*texObj
,
1028 struct gl_texture_image
*texImage
)
1030 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1036 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
1038 struct gl_texture_object
*texObj
,
1039 struct gl_texture_image
*texImage
)
1041 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1048 st_TexSubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
1049 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1050 GLint width
, GLint height
, GLint depth
,
1051 GLenum format
, GLenum type
, const void *pixels
,
1052 const struct gl_pixelstore_attrib
*packing
,
1053 struct gl_texture_object
*texObj
,
1054 struct gl_texture_image
*texImage
)
1056 struct st_context
*st
= st_context(ctx
);
1057 struct pipe_screen
*screen
= st
->pipe
->screen
;
1058 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1059 GLuint dstRowStride
;
1060 const GLuint srcImageStride
=
1061 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1064 /* init to silence warning only: */
1065 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1067 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1068 _mesa_lookup_enum_by_nr(target
),
1069 level
, xoffset
, yoffset
, width
, height
);
1072 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1073 type
, pixels
, packing
, "glTexSubImage2D");
1077 /* See if we can do texture compression with a blit/render.
1079 if (!ctx
->Mesa_DXTn
&&
1080 _mesa_is_format_compressed(texImage
->TexFormat
) &&
1081 screen
->is_format_supported(screen
,
1082 stImage
->pt
->format
,
1083 stImage
->pt
->target
,
1084 PIPE_BIND_RENDER_TARGET
, 0)) {
1085 if (compress_with_blit(ctx
, target
, level
,
1086 xoffset
, yoffset
, zoffset
,
1087 width
, height
, depth
,
1088 format
, type
, pixels
, packing
, texImage
)) {
1093 /* Map buffer if necessary. Need to lock to prevent other contexts
1094 * from uploading the buffer under us.
1097 unsigned face
= _mesa_tex_target_to_face(target
);
1099 if (format
== GL_DEPTH_COMPONENT
&&
1100 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1101 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1103 transfer_usage
= PIPE_TRANSFER_WRITE
;
1105 st_teximage_flush_before_map(st
, stImage
->pt
, face
, level
,
1107 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1113 if (!texImage
->Data
) {
1114 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1118 src
= (const GLubyte
*) pixels
;
1119 dstRowStride
= stImage
->transfer
->stride
;
1121 for (i
= 0; i
< depth
; i
++) {
1122 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1123 texImage
->TexFormat
,
1127 texImage
->ImageOffsets
,
1129 format
, type
, src
, packing
)) {
1130 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1133 if (stImage
->pt
&& i
+ 1 < depth
) {
1134 /* unmap this slice */
1135 st_texture_image_unmap(st
, stImage
);
1136 /* map next slice of 3D texture */
1137 texImage
->Data
= st_texture_image_map(st
, stImage
,
1142 src
+= srcImageStride
;
1147 _mesa_unmap_teximage_pbo(ctx
, packing
);
1149 if (stImage
->pt
&& texImage
->Data
) {
1150 st_texture_image_unmap(st
, stImage
);
1151 texImage
->Data
= NULL
;
1158 st_TexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1159 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1160 GLsizei width
, GLsizei height
, GLsizei depth
,
1161 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1162 const struct gl_pixelstore_attrib
*packing
,
1163 struct gl_texture_object
*texObj
,
1164 struct gl_texture_image
*texImage
)
1166 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1167 width
, height
, depth
, format
, type
,
1168 pixels
, packing
, texObj
, texImage
);
1173 st_TexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1174 GLint xoffset
, GLint yoffset
,
1175 GLsizei width
, GLsizei height
,
1176 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1177 const struct gl_pixelstore_attrib
*packing
,
1178 struct gl_texture_object
*texObj
,
1179 struct gl_texture_image
*texImage
)
1181 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1182 width
, height
, 1, format
, type
,
1183 pixels
, packing
, texObj
, texImage
);
1188 st_TexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1189 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1190 const GLvoid
* pixels
,
1191 const struct gl_pixelstore_attrib
*packing
,
1192 struct gl_texture_object
*texObj
,
1193 struct gl_texture_image
*texImage
)
1195 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1196 format
, type
, pixels
, packing
, texObj
, texImage
);
1201 st_CompressedTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1202 GLint xoffset
, GLsizei width
,
1204 GLsizei imageSize
, const GLvoid
*data
,
1205 struct gl_texture_object
*texObj
,
1206 struct gl_texture_image
*texImage
)
1213 st_CompressedTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1214 GLint xoffset
, GLint yoffset
,
1215 GLsizei width
, GLint height
,
1217 GLsizei imageSize
, const GLvoid
*data
,
1218 struct gl_texture_object
*texObj
,
1219 struct gl_texture_image
*texImage
)
1221 struct st_context
*st
= st_context(ctx
);
1222 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1226 enum pipe_format pformat
;
1229 unsigned face
= _mesa_tex_target_to_face(target
);
1230 pformat
= stImage
->pt
->format
;
1232 st_teximage_flush_before_map(st
, stImage
->pt
, face
, level
,
1233 PIPE_TRANSFER_WRITE
);
1234 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1235 PIPE_TRANSFER_WRITE
,
1239 srcBlockStride
= util_format_get_stride(pformat
, width
);
1240 dstBlockStride
= stImage
->transfer
->stride
;
1242 assert(stImage
->pt
);
1243 /* TODO find good values for block and strides */
1244 /* TODO also adjust texImage->data for yoffset/xoffset */
1248 if (!texImage
->Data
) {
1249 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1253 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1254 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1255 assert(width
% util_format_get_blockwidth(pformat
) == 0);
1256 assert(height
% 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(GLcontext
*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(GLcontext
*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
= st_cond_flush_get_tex_transfer( st_context(ctx
),
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 st_teximage_flush_before_map(st
, stImage
->pt
, 0, 0,
1334 texDest
= st_texture_image_map(st
, stImage
, 0, transfer_usage
,
1335 destX
, destY
, width
, height
);
1337 if (baseFormat
== GL_DEPTH_COMPONENT
||
1338 baseFormat
== GL_DEPTH_STENCIL
) {
1339 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1340 ctx
->Pixel
.DepthBias
!= 0.0F
);
1343 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1344 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1353 /* To avoid a large temp memory allocation, do copy row by row */
1354 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1355 uint data
[MAX_WIDTH
];
1356 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1358 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1360 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1366 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1368 if (tempSrc
&& texDest
) {
1369 const GLint dims
= 2;
1370 const GLint dstRowStride
= stImage
->transfer
->stride
;
1371 struct gl_texture_image
*texImage
= &stImage
->base
;
1372 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1374 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1375 unpack
.Invert
= GL_TRUE
;
1378 /* get float/RGBA image from framebuffer */
1379 /* XXX this usually involves a lot of int/float conversion.
1380 * try to avoid that someday.
1382 pipe_get_tile_rgba(pipe
, src_trans
, 0, 0, width
, height
, tempSrc
);
1384 /* Store into texture memory.
1385 * Note that this does some special things such as pixel transfer
1386 * ops and format conversion. In particular, if the dest tex format
1387 * is actually RGBA but the user created the texture as GL_RGB we
1388 * need to fill-in/override the alpha channel with 1.0.
1390 _mesa_texstore(ctx
, dims
,
1391 texImage
->_BaseFormat
,
1392 texImage
->TexFormat
,
1396 texImage
->ImageOffsets
,
1398 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1402 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1409 st_texture_image_unmap(st
, stImage
);
1410 pipe
->transfer_destroy(pipe
, src_trans
);
1416 * If the format of the src renderbuffer and the format of the dest
1417 * texture are compatible (in terms of blitting), return a TGSI writemask
1418 * to be used during the blit.
1419 * If the src/dest are incompatible, return 0.
1422 compatible_src_dst_formats(GLcontext
*ctx
,
1423 const struct gl_renderbuffer
*src
,
1424 const struct gl_texture_image
*dst
)
1426 /* Get logical base formats for the src and dest.
1427 * That is, use the user-requested formats and not the actual, device-
1429 * For example, the user may have requested an A8 texture but the
1430 * driver may actually be using an RGBA texture format. When we
1431 * copy/blit to that texture, we only want to copy the Alpha channel
1432 * and not the RGB channels.
1434 * Similarly, when the src FBO was created an RGB format may have been
1435 * requested but the driver actually chose an RGBA format. In that case,
1436 * we don't want to copy the undefined Alpha channel to the dest texture
1437 * (it should be 1.0).
1439 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1440 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1443 * XXX when we have red-only and red/green renderbuffers we'll need
1444 * to add more cases here (or implement a general-purpose routine that
1445 * queries the existance of the R,G,B,A channels in the src and dest).
1447 if (srcFormat
== dstFormat
) {
1448 /* This is the same as matching_base_formats, which should
1449 * always pass, as it did previously.
1451 return TGSI_WRITEMASK_XYZW
;
1453 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1454 /* Make sure that A in the dest is 1. The actual src format
1455 * may be RGBA and have undefined A values.
1457 return TGSI_WRITEMASK_XYZ
;
1459 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1460 /* Make sure that A in the dest is 1. The actual dst format
1461 * may be RGBA and will need A=1 to provide proper alpha values
1462 * when sampled later.
1464 return TGSI_WRITEMASK_XYZ
;
1467 if (ST_DEBUG
& DEBUG_FALLBACK
)
1468 debug_printf("%s failed for src %s, dst %s\n",
1470 _mesa_lookup_enum_by_nr(srcFormat
),
1471 _mesa_lookup_enum_by_nr(dstFormat
));
1482 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1483 * Note that the region to copy has already been clipped so we know we
1484 * won't read from outside the source renderbuffer's bounds.
1486 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1489 st_copy_texsubimage(GLcontext
*ctx
,
1490 GLenum target
, GLint level
,
1491 GLint destX
, GLint destY
, GLint destZ
,
1492 GLint srcX
, GLint srcY
,
1493 GLsizei width
, GLsizei height
)
1495 struct gl_texture_unit
*texUnit
=
1496 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1497 struct gl_texture_object
*texObj
=
1498 _mesa_select_tex_object(ctx
, texUnit
, target
);
1499 struct gl_texture_image
*texImage
=
1500 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1501 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1502 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1503 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1504 struct st_renderbuffer
*strb
;
1505 struct st_context
*st
= st_context(ctx
);
1506 struct pipe_context
*pipe
= st
->pipe
;
1507 struct pipe_screen
*screen
= pipe
->screen
;
1508 enum pipe_format dest_format
, src_format
;
1509 GLboolean use_fallback
= GL_TRUE
;
1510 GLboolean matching_base_formats
;
1511 GLuint format_writemask
;
1512 struct pipe_surface
*dest_surface
= NULL
;
1513 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1515 /* any rendering in progress must flushed before we grab the fb image */
1516 st_flush(st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
1518 /* make sure finalize_textures has been called?
1520 if (0) st_validate_state(st
);
1522 /* determine if copying depth or color data */
1523 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1524 texBaseFormat
== GL_DEPTH_STENCIL
) {
1525 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1528 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1529 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1532 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1533 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
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 */
1593 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1594 stImage
->face
, stImage
->level
,
1596 PIPE_BIND_BLIT_DESTINATION
);
1598 /* for surface_copy(), y=0=top, always */
1599 pipe
->surface_copy(pipe
,
1608 use_fallback
= GL_FALSE
;
1610 else if (format_writemask
&&
1611 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1612 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1613 screen
->is_format_supported(screen
, src_format
,
1615 PIPE_BIND_SAMPLER_VIEW
,
1617 screen
->is_format_supported(screen
, dest_format
,
1619 PIPE_BIND_RENDER_TARGET
,
1621 /* draw textured quad to do the copy */
1624 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1625 stImage
->face
, stImage
->level
,
1627 PIPE_BIND_BLIT_DESTINATION
);
1630 srcY1
= strb
->Base
.Height
- srcY
- height
;
1631 srcY0
= srcY1
+ height
;
1635 srcY1
= srcY0
+ height
;
1637 util_blit_pixels_writemask(st
->blit
,
1639 st_get_renderbuffer_sampler_view(strb
, pipe
),
1641 srcX
+ width
, srcY1
,
1644 destX
+ width
, destY
+ height
,
1645 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1647 use_fallback
= GL_FALSE
;
1651 pipe_surface_reference(&dest_surface
, NULL
);
1655 /* software fallback */
1656 fallback_copy_texsubimage(ctx
, target
, level
,
1657 strb
, stImage
, texBaseFormat
,
1658 destX
, destY
, destZ
,
1659 srcX
, srcY
, width
, height
);
1666 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1667 GLenum internalFormat
,
1668 GLint x
, GLint y
, GLsizei width
, GLint border
)
1670 struct gl_texture_unit
*texUnit
=
1671 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1672 struct gl_texture_object
*texObj
=
1673 _mesa_select_tex_object(ctx
, texUnit
, target
);
1674 struct gl_texture_image
*texImage
=
1675 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1677 /* Setup or redefine the texture object, texture and texture
1678 * image. Don't populate yet.
1680 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1682 GL_RGBA
, CHAN_TYPE
, NULL
,
1683 &ctx
->DefaultPacking
, texObj
, texImage
);
1685 st_copy_texsubimage(ctx
, target
, level
,
1686 0, 0, 0, /* destX,Y,Z */
1687 x
, y
, width
, 1); /* src X, Y, size */
1692 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1693 GLenum internalFormat
,
1694 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1697 struct gl_texture_unit
*texUnit
=
1698 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1699 struct gl_texture_object
*texObj
=
1700 _mesa_select_tex_object(ctx
, texUnit
, target
);
1701 struct gl_texture_image
*texImage
=
1702 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1704 /* Setup or redefine the texture object, texture and texture
1705 * image. Don't populate yet.
1707 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1708 width
, height
, border
,
1709 GL_RGBA
, CHAN_TYPE
, NULL
,
1710 &ctx
->DefaultPacking
, texObj
, texImage
);
1712 st_copy_texsubimage(ctx
, target
, level
,
1713 0, 0, 0, /* destX,Y,Z */
1714 x
, y
, width
, height
); /* src X, Y, size */
1719 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1720 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1722 const GLint yoffset
= 0, zoffset
= 0;
1723 const GLsizei height
= 1;
1724 st_copy_texsubimage(ctx
, target
, level
,
1725 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1726 x
, y
, width
, height
); /* src X, Y, size */
1731 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1732 GLint xoffset
, GLint yoffset
,
1733 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1735 const GLint zoffset
= 0;
1736 st_copy_texsubimage(ctx
, target
, level
,
1737 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1738 x
, y
, width
, height
); /* src X, Y, size */
1743 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1744 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1745 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1747 st_copy_texsubimage(ctx
, target
, level
,
1748 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1749 x
, y
, width
, height
); /* src X, Y, size */
1754 * Copy image data from stImage into the texture object 'stObj' at level
1758 copy_image_data_to_texture(struct st_context
*st
,
1759 struct st_texture_object
*stObj
,
1761 struct st_texture_image
*stImage
)
1765 const struct gl_texture_image
*dstImage
=
1766 stObj
->base
.Image
[stImage
->face
][stImage
->level
];
1768 assert(dstImage
->Width
== stImage
->base
.Width
);
1769 assert(dstImage
->Height
== stImage
->base
.Height
);
1770 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1774 /* Copy potentially with the blitter:
1776 st_texture_image_copy(st
->pipe
,
1777 stObj
->pt
, dstLevel
, /* dest texture, level */
1778 stImage
->pt
, /* src texture */
1781 pipe_resource_reference(&stImage
->pt
, NULL
);
1783 else if (stImage
->base
.Data
) {
1784 /* More straightforward upload.
1786 st_teximage_flush_before_map(st
, stObj
->pt
, stImage
->face
, dstLevel
,
1787 PIPE_TRANSFER_WRITE
);
1789 st_texture_image_data(st
,
1794 stImage
->base
.RowStride
*
1795 util_format_get_blocksize(stObj
->pt
->format
),
1796 stImage
->base
.RowStride
*
1797 stImage
->base
.Height
*
1798 util_format_get_blocksize(stObj
->pt
->format
));
1799 _mesa_align_free(stImage
->base
.Data
);
1800 stImage
->base
.Data
= NULL
;
1803 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1808 * Called during state validation. When this function is finished,
1809 * the texture object should be ready for rendering.
1810 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1813 st_finalize_texture(GLcontext
*ctx
,
1814 struct pipe_context
*pipe
,
1815 struct gl_texture_object
*tObj
)
1817 struct st_context
*st
= st_context(ctx
);
1818 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1819 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1821 struct st_texture_image
*firstImage
;
1822 enum pipe_format firstImageFormat
;
1824 if (stObj
->base
._Complete
) {
1825 /* The texture is complete and we know exactly how many mipmap levels
1826 * are present/needed. This is conditional because we may be called
1827 * from the st_generate_mipmap() function when the texture object is
1828 * incomplete. In that case, we'll have set stObj->lastLevel before
1831 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1832 stObj
->base
.MinFilter
== GL_NEAREST
)
1833 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1835 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1838 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1841 /* If both firstImage and stObj point to a texture which can contain
1842 * all active images, favour firstImage. Note that because of the
1843 * completeness requirement, we know that the image dimensions
1846 if (firstImage
->pt
&&
1847 firstImage
->pt
!= stObj
->pt
&&
1848 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1849 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1850 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1853 /* Find gallium format for the Mesa texture */
1854 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1856 /* If we already have a gallium texture, check that it matches the texture
1857 * object's format, target, size, num_levels, etc.
1860 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1861 stObj
->pt
->format
!= firstImageFormat
||
1862 stObj
->pt
->last_level
< stObj
->lastLevel
||
1863 stObj
->pt
->width0
!= stObj
->width0
||
1864 stObj
->pt
->height0
!= stObj
->height0
||
1865 stObj
->pt
->depth0
!= stObj
->depth0
)
1867 /* The gallium texture does not match the Mesa texture so delete the
1868 * gallium texture now. We'll make a new one below.
1870 pipe_resource_reference(&stObj
->pt
, NULL
);
1871 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1872 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1876 /* May need to create a new gallium texture:
1879 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1881 stObj
->pt
= st_texture_create(st
,
1882 gl_target_to_pipe(stObj
->base
.Target
),
1891 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1896 /* Pull in any images not in the object's texture:
1898 for (face
= 0; face
< nr_faces
; face
++) {
1900 for (level
= 0; level
<= stObj
->lastLevel
; level
++) {
1901 struct st_texture_image
*stImage
=
1902 st_texture_image(stObj
->base
.Image
[face
][level
]);
1904 /* Need to import images in main memory or held in other textures.
1906 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1907 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1917 * Returns pointer to a default/dummy texture.
1918 * This is typically used when the current shader has tex/sample instructions
1919 * but the user has not provided a (any) texture(s).
1921 struct gl_texture_object
*
1922 st_get_default_texture(struct st_context
*st
)
1924 if (!st
->default_texture
) {
1925 static const GLenum target
= GL_TEXTURE_2D
;
1926 GLubyte pixels
[16][16][4];
1927 struct gl_texture_object
*texObj
;
1928 struct gl_texture_image
*texImg
;
1931 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1932 * when attempting to sample incomplete textures.
1934 for (i
= 0; i
< 16; i
++) {
1935 for (j
= 0; j
< 16; j
++) {
1936 pixels
[i
][j
][0] = 0;
1937 pixels
[i
][j
][1] = 0;
1938 pixels
[i
][j
][2] = 0;
1939 pixels
[i
][j
][3] = 255;
1943 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1945 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1947 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1948 16, 16, 1, 0, /* w, h, d, border */
1951 st_TexImage(st
->ctx
, 2, target
,
1952 0, GL_RGBA
, /* level, intformat */
1953 16, 16, 1, 0, /* w, h, d, border */
1954 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1955 &st
->ctx
->DefaultPacking
,
1959 texObj
->MinFilter
= GL_NEAREST
;
1960 texObj
->MagFilter
= GL_NEAREST
;
1961 texObj
->_Complete
= GL_TRUE
;
1963 st
->default_texture
= texObj
;
1965 return st
->default_texture
;
1970 st_init_texture_functions(struct dd_function_table
*functions
)
1972 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1973 functions
->TexImage1D
= st_TexImage1D
;
1974 functions
->TexImage2D
= st_TexImage2D
;
1975 functions
->TexImage3D
= st_TexImage3D
;
1976 functions
->TexSubImage1D
= st_TexSubImage1D
;
1977 functions
->TexSubImage2D
= st_TexSubImage2D
;
1978 functions
->TexSubImage3D
= st_TexSubImage3D
;
1979 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1980 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1981 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1982 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1983 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1984 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1985 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1986 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1987 functions
->GenerateMipmap
= st_generate_mipmap
;
1989 functions
->GetTexImage
= st_GetTexImage
;
1991 /* compressed texture functions */
1992 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1993 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1995 functions
->NewTextureObject
= st_NewTextureObject
;
1996 functions
->NewTextureImage
= st_NewTextureImage
;
1997 functions
->DeleteTexture
= st_DeleteTextureObject
;
1998 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1999 functions
->UpdateTexturePalette
= 0;
2001 functions
->TextureMemCpy
= do_memcpy
;
2003 /* XXX Temporary until we can query pipe's texture sizes */
2004 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;