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"
31 #include "main/convolve.h"
33 #include "main/enums.h"
34 #include "main/formats.h"
35 #include "main/image.h"
36 #include "main/imports.h"
37 #include "main/macros.h"
38 #include "main/mipmap.h"
39 #include "main/pixel.h"
40 #include "main/texcompress.h"
41 #include "main/texfetch.h"
42 #include "main/texformat.h"
43 #include "main/texgetimage.h"
44 #include "main/teximage.h"
45 #include "main/texobj.h"
46 #include "main/texstore.h"
48 #include "state_tracker/st_debug.h"
49 #include "state_tracker/st_context.h"
50 #include "state_tracker/st_cb_fbo.h"
51 #include "state_tracker/st_cb_texture.h"
52 #include "state_tracker/st_format.h"
53 #include "state_tracker/st_public.h"
54 #include "state_tracker/st_texture.h"
55 #include "state_tracker/st_gen_mipmap.h"
56 #include "state_tracker/st_inlines.h"
57 #include "state_tracker/st_atom.h"
59 #include "pipe/p_context.h"
60 #include "pipe/p_defines.h"
61 #include "pipe/p_inlines.h"
62 #include "pipe/p_shader_tokens.h"
63 #include "util/u_tile.h"
64 #include "util/u_blit.h"
65 #include "util/u_surface.h"
66 #include "util/u_math.h"
69 #define DBG if (0) printf
72 static enum pipe_texture_target
73 gl_target_to_pipe(GLenum target
)
77 return PIPE_TEXTURE_1D
;
80 case GL_TEXTURE_RECTANGLE_NV
:
81 return PIPE_TEXTURE_2D
;
84 return PIPE_TEXTURE_3D
;
86 case GL_TEXTURE_CUBE_MAP_ARB
:
87 return PIPE_TEXTURE_CUBE
;
97 * Return nominal bytes per texel for a compressed format, 0 for non-compressed
101 compressed_num_bytes(gl_format format
)
104 #if FEATURE_texture_fxt1
105 case MESA_FORMAT_RGB_FXT1
:
106 case MESA_FORMAT_RGBA_FXT1
:
108 #if FEATURE_texture_s3tc
109 case MESA_FORMAT_RGB_DXT1
:
110 case MESA_FORMAT_RGBA_DXT1
:
112 case MESA_FORMAT_RGBA_DXT3
:
113 case MESA_FORMAT_RGBA_DXT5
:
123 is_compressed_mesa_format(gl_format format
)
126 case MESA_FORMAT_RGB_DXT1
:
127 case MESA_FORMAT_RGBA_DXT1
:
128 case MESA_FORMAT_RGBA_DXT3
:
129 case MESA_FORMAT_RGBA_DXT5
:
130 case MESA_FORMAT_SRGB_DXT1
:
131 case MESA_FORMAT_SRGBA_DXT1
:
132 case MESA_FORMAT_SRGBA_DXT3
:
133 case MESA_FORMAT_SRGBA_DXT5
:
141 /** called via ctx->Driver.NewTextureImage() */
142 static struct gl_texture_image
*
143 st_NewTextureImage(GLcontext
* ctx
)
145 DBG("%s\n", __FUNCTION__
);
147 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
151 /** called via ctx->Driver.NewTextureObject() */
152 static struct gl_texture_object
*
153 st_NewTextureObject(GLcontext
* ctx
, GLuint name
, GLenum target
)
155 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
157 DBG("%s\n", __FUNCTION__
);
158 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
163 /** called via ctx->Driver.DeleteTextureImage() */
165 st_DeleteTextureObject(GLcontext
*ctx
,
166 struct gl_texture_object
*texObj
)
168 struct st_texture_object
*stObj
= st_texture_object(texObj
);
170 pipe_texture_reference(&stObj
->pt
, NULL
);
172 _mesa_delete_texture_object(ctx
, texObj
);
176 /** called via ctx->Driver.FreeTexImageData() */
178 st_FreeTextureImageData(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
180 struct st_texture_image
*stImage
= st_texture_image(texImage
);
182 DBG("%s\n", __FUNCTION__
);
185 pipe_texture_reference(&stImage
->pt
, NULL
);
188 if (texImage
->Data
) {
189 _mesa_align_free(texImage
->Data
);
190 texImage
->Data
= NULL
;
196 * From linux kernel i386 header files, copes with odd sizes better
197 * than COPY_DWORDS would:
198 * XXX Put this in src/mesa/main/imports.h ???
200 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
202 __memcpy(void *to
, const void *from
, size_t n
)
205 __asm__
__volatile__("rep ; movsl\n\t"
209 "1:\ttestb $1,%b4\n\t"
211 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
212 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
217 #define __memcpy(a,b,c) memcpy(a,b,c)
222 * The system memcpy (at least on ubuntu 5.10) has problems copying
223 * to agp (writecombined) memory from a source which isn't 64-byte
224 * aligned - there is a 4x performance falloff.
226 * The x86 __memcpy is immune to this but is slightly slower
227 * (10%-ish) than the system memcpy.
229 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
230 * isn't much faster than x86_memcpy for agp copies.
232 * TODO: switch dynamically.
235 do_memcpy(void *dest
, const void *src
, size_t n
)
237 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
238 return __memcpy(dest
, src
, n
);
241 return memcpy(dest
, src
, n
);
246 * Return default texture usage bitmask for the given texture format.
249 default_usage(enum pipe_format fmt
)
251 GLuint usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
252 if (pf_is_depth_stencil(fmt
))
253 usage
|= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
255 usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
261 * Allocate a pipe_texture object for the given st_texture_object using
262 * the given st_texture_image to guess the mipmap size/levels.
265 * Otherwise, store it in memory if (Border != 0) or (any dimension ==
268 * Otherwise, if max_level >= level >= min_level, create texture with
269 * space for images from min_level down to max_level.
271 * Otherwise, create texture with space for images from (level 0)..(1x1).
272 * Consider pruning this texture at a validation if the saving is worth it.
275 guess_and_alloc_texture(struct st_context
*st
,
276 struct st_texture_object
*stObj
,
277 const struct st_texture_image
*stImage
)
281 GLuint width
= stImage
->base
.Width2
; /* size w/out border */
282 GLuint height
= stImage
->base
.Height2
;
283 GLuint depth
= stImage
->base
.Depth2
;
285 enum pipe_format fmt
;
287 DBG("%s\n", __FUNCTION__
);
292 (GLint
) stImage
->level
> stObj
->base
.BaseLevel
&&
293 (stImage
->base
.Width
== 1 ||
294 (stObj
->base
.Target
!= GL_TEXTURE_1D
&&
295 stImage
->base
.Height
== 1) ||
296 (stObj
->base
.Target
== GL_TEXTURE_3D
&&
297 stImage
->base
.Depth
== 1)))
300 /* If this image disrespects BaseLevel, allocate from level zero.
301 * Usually BaseLevel == 0, so it's unlikely to happen.
303 if ((GLint
) stImage
->level
< stObj
->base
.BaseLevel
)
306 firstLevel
= stObj
->base
.BaseLevel
;
309 /* Figure out image dimensions at start level.
311 for (i
= stImage
->level
; i
> firstLevel
; i
--) {
320 if (width
== 0 || height
== 0 || depth
== 0) {
321 /* no texture needed */
325 /* Guess a reasonable value for lastLevel. This is probably going
326 * to be wrong fairly often and might mean that we have to look at
327 * resizable buffers, or require that buffers implement lazy
328 * pagetable arrangements.
330 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
331 stObj
->base
.MinFilter
== GL_LINEAR
||
332 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
333 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
334 !stObj
->base
.GenerateMipmap
&&
335 stImage
->level
== firstLevel
) {
336 /* only alloc space for a single mipmap level */
337 lastLevel
= firstLevel
;
340 /* alloc space for a full mipmap */
341 GLuint l2width
= util_logbase2(width
);
342 GLuint l2height
= util_logbase2(height
);
343 GLuint l2depth
= util_logbase2(depth
);
344 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
347 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
349 usage
= default_usage(fmt
);
351 stObj
->pt
= st_texture_create(st
,
352 gl_target_to_pipe(stObj
->base
.Target
),
360 DBG("%s - success\n", __FUNCTION__
);
365 * Adjust pixel unpack params and image dimensions to strip off the
367 * Gallium doesn't support texture borders. They've seldem been used
368 * and seldom been implemented correctly anyway.
369 * \param unpackNew returns the new pixel unpack parameters
372 strip_texture_border(GLint border
,
373 GLint
*width
, GLint
*height
, GLint
*depth
,
374 const struct gl_pixelstore_attrib
*unpack
,
375 struct gl_pixelstore_attrib
*unpackNew
)
377 assert(border
> 0); /* sanity check */
379 *unpackNew
= *unpack
;
381 if (unpackNew
->RowLength
== 0)
382 unpackNew
->RowLength
= *width
;
384 if (depth
&& unpackNew
->ImageHeight
== 0)
385 unpackNew
->ImageHeight
= *height
;
387 unpackNew
->SkipPixels
+= border
;
389 unpackNew
->SkipRows
+= border
;
391 unpackNew
->SkipImages
+= border
;
394 *width
= *width
- 2 * border
;
395 if (height
&& *height
>= 3)
396 *height
= *height
- 2 * border
;
397 if (depth
&& *depth
>= 3)
398 *depth
= *depth
- 2 * border
;
403 * Try to do texture compression via rendering. If the Gallium driver
404 * can render into a compressed surface this will allow us to do texture
406 * \return GL_TRUE for success, GL_FALSE for failure
409 compress_with_blit(GLcontext
* ctx
,
410 GLenum target
, GLint level
,
411 GLint xoffset
, GLint yoffset
, GLint zoffset
,
412 GLint width
, GLint height
, GLint depth
,
413 GLenum format
, GLenum type
, const void *pixels
,
414 const struct gl_pixelstore_attrib
*unpack
,
415 struct gl_texture_image
*texImage
)
417 const GLuint dstImageOffsets
[1] = {0};
418 struct st_texture_image
*stImage
= st_texture_image(texImage
);
419 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
420 gl_format mesa_format
;
421 struct pipe_texture templ
;
422 struct pipe_texture
*src_tex
;
423 struct pipe_surface
*dst_surface
;
424 struct pipe_transfer
*tex_xfer
;
428 /* XXX: Can this happen? Should we assert? */
432 /* get destination surface (in the compressed texture) */
433 dst_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
434 stImage
->face
, stImage
->level
, 0,
435 PIPE_BUFFER_USAGE_GPU_WRITE
);
437 /* can't render into this format (or other problem) */
441 /* Choose format for the temporary RGBA texture image.
443 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
448 /* Create the temporary source texture
450 memset(&templ
, 0, sizeof(templ
));
451 templ
.target
= PIPE_TEXTURE_2D
;
452 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
);
453 pf_get_block(templ
.format
, &templ
.block
);
454 templ
.width
[0] = width
;
455 templ
.height
[0] = height
;
457 templ
.last_level
= 0;
458 templ
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
459 src_tex
= screen
->texture_create(screen
, &templ
);
464 /* Put user's tex data into the temporary texture
466 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
), src_tex
,
467 0, 0, 0, /* face, level are zero */
469 0, 0, width
, height
); /* x, y, w, h */
470 map
= screen
->transfer_map(screen
, tex_xfer
);
472 _mesa_texstore(ctx
, 2, GL_RGBA
, mesa_format
,
474 0, 0, 0, /* dest x/y/z offset */
475 tex_xfer
->stride
, /* dest row stride (bytes) */
476 dstImageOffsets
, /* image offsets (for 3D only) */
477 width
, height
, 1, /* size */
478 format
, type
, /* source format/type */
479 pixels
, /* source data */
480 unpack
); /* source data packing */
482 screen
->transfer_unmap(screen
, tex_xfer
);
483 screen
->tex_transfer_destroy(tex_xfer
);
485 /* copy / compress image */
486 util_blit_pixels_tex(ctx
->st
->blit
,
487 src_tex
, /* pipe_texture (src) */
488 0, 0, /* src x0, y0 */
489 width
, height
, /* src x1, y1 */
490 dst_surface
, /* pipe_surface (dst) */
491 xoffset
, yoffset
, /* dst x0, y0 */
492 xoffset
+ width
, /* dst x1 */
493 yoffset
+ height
, /* dst y1 */
495 PIPE_TEX_MIPFILTER_NEAREST
);
497 pipe_surface_reference(&dst_surface
, NULL
);
498 pipe_texture_reference(&src_tex
, NULL
);
505 * Do glTexImage1/2/3D().
508 st_TexImage(GLcontext
* ctx
,
510 GLenum target
, GLint level
,
511 GLint internalFormat
,
512 GLint width
, GLint height
, GLint depth
,
514 GLenum format
, GLenum type
, const void *pixels
,
515 const struct gl_pixelstore_attrib
*unpack
,
516 struct gl_texture_object
*texObj
,
517 struct gl_texture_image
*texImage
,
518 GLsizei imageSize
, GLboolean compressed_src
)
520 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
521 struct st_texture_object
*stObj
= st_texture_object(texObj
);
522 struct st_texture_image
*stImage
= st_texture_image(texImage
);
523 GLint postConvWidth
, postConvHeight
;
524 GLint texelBytes
, sizeInBytes
;
525 GLuint dstRowStride
= 0;
526 struct gl_pixelstore_attrib unpackNB
;
527 enum pipe_transfer_usage transfer_usage
= 0;
529 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
530 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
532 /* switch to "normal" */
533 if (stObj
->surface_based
) {
534 _mesa_clear_texture_object(ctx
, texObj
);
535 stObj
->surface_based
= GL_FALSE
;
538 /* gallium does not support texture borders, strip it off */
540 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
542 texImage
->Width
= width
;
543 texImage
->Height
= height
;
544 texImage
->Depth
= depth
;
545 texImage
->Border
= 0;
549 postConvWidth
= width
;
550 postConvHeight
= height
;
552 stImage
->face
= _mesa_tex_target_to_face(target
);
553 stImage
->level
= level
;
556 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
557 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
562 _mesa_set_fetch_functions(texImage
, dims
);
564 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
565 /* must be a compressed format */
569 texelBytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
571 /* Minimum pitch of 32 bytes */
572 if (postConvWidth
* texelBytes
< 32) {
573 postConvWidth
= 32 / texelBytes
;
574 texImage
->RowStride
= postConvWidth
;
577 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
578 /*assert(texImage->RowStride == postConvWidth);*/
581 /* Release the reference to a potentially orphaned buffer.
582 * Release any old malloced memory.
585 pipe_texture_reference(&stImage
->pt
, NULL
);
586 assert(!texImage
->Data
);
588 else if (texImage
->Data
) {
589 _mesa_align_free(texImage
->Data
);
592 if (width
== 0 || height
== 0 || depth
== 0) {
593 /* stop after freeing old image */
597 /* If this is the only mipmap level in the texture, could call
598 * bmBufferData with NULL data to free the old block and avoid
599 * waiting on any outstanding fences.
602 if (stObj
->teximage_realloc
||
603 level
> (GLint
) stObj
->pt
->last_level
||
604 (stObj
->pt
->last_level
== level
&&
605 stObj
->pt
->target
!= PIPE_TEXTURE_CUBE
&&
606 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
607 stImage
->face
, stImage
->level
))) {
609 pipe_texture_reference(&stObj
->pt
, NULL
);
611 stObj
->teximage_realloc
= FALSE
;
616 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
618 /* Probably out of memory.
619 * Try flushing any pending rendering, then retry.
622 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
624 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
630 assert(!stImage
->pt
);
633 st_texture_match_image(stObj
->pt
, &stImage
->base
,
634 stImage
->face
, stImage
->level
)) {
636 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
641 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
643 /* st_CopyTexImage calls this function with pixels == NULL, with
644 * the expectation that the texture will be set up but nothing
645 * more will be done. This is where those calls return:
647 if (compressed_src
) {
648 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
650 "glCompressedTexImage");
653 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
655 pixels
, unpack
, "glTexImage");
658 /* Note: we can't check for pixels==NULL until after we've allocated
659 * memory for the texture.
662 /* See if we can do texture compression with a blit/render.
664 if (!compressed_src
&&
666 is_compressed_mesa_format(texImage
->TexFormat
) &&
667 screen
->is_format_supported(screen
,
670 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
674 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
675 format
, type
, pixels
, unpack
, texImage
)) {
681 if (format
== GL_DEPTH_COMPONENT
&&
682 pf_is_depth_and_stencil(stImage
->pt
->format
))
683 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
685 transfer_usage
= PIPE_TRANSFER_WRITE
;
687 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
688 transfer_usage
, 0, 0,
690 stImage
->base
.Height
);
691 if(stImage
->transfer
)
692 dstRowStride
= stImage
->transfer
->stride
;
695 /* Allocate regular memory and store the image there temporarily. */
696 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
697 sizeInBytes
= _mesa_format_image_size(texImage
->TexFormat
,
701 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
705 dstRowStride
= postConvWidth
* texelBytes
;
706 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
709 texImage
->Data
= _mesa_align_malloc(sizeInBytes
, 16);
712 if (!texImage
->Data
) {
713 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
720 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
721 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
723 /* Copy data. Would like to know when it's ok for us to eg. use
724 * the blitter to copy. Or, use the hardware to do the format
725 * conversion and copy:
727 if (compressed_src
) {
728 memcpy(texImage
->Data
, pixels
, imageSize
);
731 const GLuint srcImageStride
=
732 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
734 const GLubyte
*src
= (const GLubyte
*) pixels
;
736 for (i
= 0; i
< depth
; i
++) {
737 if (!_mesa_texstore(ctx
, dims
,
738 texImage
->_BaseFormat
,
741 0, 0, 0, /* dstX/Y/Zoffset */
743 texImage
->ImageOffsets
,
745 format
, type
, src
, unpack
)) {
746 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
749 if (stImage
->pt
&& i
+ 1 < depth
) {
750 /* unmap this slice */
751 st_texture_image_unmap(ctx
->st
, stImage
);
752 /* map next slice of 3D texture */
753 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
754 transfer_usage
, 0, 0,
756 stImage
->base
.Height
);
757 src
+= srcImageStride
;
763 _mesa_unmap_teximage_pbo(ctx
, unpack
);
765 if (stImage
->pt
&& texImage
->Data
) {
766 st_texture_image_unmap(ctx
->st
, stImage
);
767 texImage
->Data
= NULL
;
773 st_TexImage3D(GLcontext
* ctx
,
774 GLenum target
, GLint level
,
775 GLint internalFormat
,
776 GLint width
, GLint height
, GLint depth
,
778 GLenum format
, GLenum type
, const void *pixels
,
779 const struct gl_pixelstore_attrib
*unpack
,
780 struct gl_texture_object
*texObj
,
781 struct gl_texture_image
*texImage
)
783 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
784 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
790 st_TexImage2D(GLcontext
* ctx
,
791 GLenum target
, GLint level
,
792 GLint internalFormat
,
793 GLint width
, GLint height
, GLint border
,
794 GLenum format
, GLenum type
, const void *pixels
,
795 const struct gl_pixelstore_attrib
*unpack
,
796 struct gl_texture_object
*texObj
,
797 struct gl_texture_image
*texImage
)
799 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
800 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
805 st_TexImage1D(GLcontext
* ctx
,
806 GLenum target
, GLint level
,
807 GLint internalFormat
,
808 GLint width
, GLint border
,
809 GLenum format
, GLenum type
, const void *pixels
,
810 const struct gl_pixelstore_attrib
*unpack
,
811 struct gl_texture_object
*texObj
,
812 struct gl_texture_image
*texImage
)
814 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
815 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
820 st_CompressedTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
821 GLint internalFormat
,
822 GLint width
, GLint height
, GLint border
,
823 GLsizei imageSize
, const GLvoid
*data
,
824 struct gl_texture_object
*texObj
,
825 struct gl_texture_image
*texImage
)
827 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
828 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
834 * glGetTexImage() helper: decompress a compressed texture by rendering
835 * a textured quad. Store the results in the user's buffer.
838 decompress_with_blit(GLcontext
* ctx
, GLenum target
, GLint level
,
839 GLenum format
, GLenum type
, GLvoid
*pixels
,
840 struct gl_texture_object
*texObj
,
841 struct gl_texture_image
*texImage
)
843 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
844 struct st_texture_image
*stImage
= st_texture_image(texImage
);
845 const GLuint width
= texImage
->Width
;
846 const GLuint height
= texImage
->Height
;
847 struct pipe_surface
*dst_surface
;
848 struct pipe_texture
*dst_texture
;
849 struct pipe_transfer
*tex_xfer
;
851 /* create temp / dest surface */
852 if (!util_create_rgba_surface(screen
, width
, height
,
853 &dst_texture
, &dst_surface
)) {
854 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
855 "in decompress_with_blit()");
859 /* blit/render/decompress */
860 util_blit_pixels_tex(ctx
->st
->blit
,
861 stImage
->pt
, /* pipe_texture (src) */
862 0, 0, /* src x0, y0 */
863 width
, height
, /* src x1, y1 */
864 dst_surface
, /* pipe_surface (dst) */
865 0, 0, /* dst x0, y0 */
866 width
, height
, /* dst x1, y1 */
868 PIPE_TEX_MIPFILTER_NEAREST
);
870 /* map the dst_surface so we can read from it */
871 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
),
872 dst_texture
, 0, 0, 0,
874 0, 0, width
, height
);
876 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
878 /* copy/pack data into user buffer */
879 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
881 const uint bytesPerRow
= width
* pf_get_size(stImage
->pt
->format
);
882 ubyte
*map
= screen
->transfer_map(screen
, tex_xfer
);
884 for (row
= 0; row
< height
; row
++) {
885 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
886 height
, format
, type
, row
, 0);
887 memcpy(dest
, map
, bytesPerRow
);
888 map
+= tex_xfer
->stride
;
890 screen
->transfer_unmap(screen
, tex_xfer
);
893 /* format translation via floats */
895 for (row
= 0; row
< height
; row
++) {
896 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
897 GLfloat rgba
[4 * MAX_WIDTH
];
898 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
899 height
, format
, type
, row
, 0);
901 if (ST_DEBUG
& DEBUG_FALLBACK
)
902 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
904 /* get float[4] rgba row from surface */
905 pipe_get_tile_rgba(tex_xfer
, 0, row
, width
, 1, rgba
);
907 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
908 type
, dest
, &ctx
->Pack
, transferOps
);
912 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
914 /* destroy the temp / dest surface */
915 util_destroy_rgba_surface(dst_texture
, dst_surface
);
921 * Need to map texture image into memory before copying image data,
925 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
926 GLenum format
, GLenum type
, GLvoid
* pixels
,
927 struct gl_texture_object
*texObj
,
928 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
930 struct st_texture_image
*stImage
= st_texture_image(texImage
);
931 const GLuint dstImageStride
=
932 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
938 pf_is_compressed(stImage
->pt
->format
) &&
940 /* Need to decompress the texture.
941 * We'll do this by rendering a textured quad.
942 * Note that we only expect RGBA formats (no Z/depth formats).
944 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
951 /* Image is stored in hardware format in a buffer managed by the
952 * kernel. Need to explicitly map and unmap it.
954 unsigned face
= _mesa_tex_target_to_face(target
);
956 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
959 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
960 PIPE_TRANSFER_READ
, 0, 0,
962 stImage
->base
.Height
);
963 texImage
->RowStride
= stImage
->transfer
->stride
/ stImage
->pt
->block
.size
;
966 /* Otherwise, the image should actually be stored in
967 * texImage->Data. This is pretty confusing for
968 * everybody, I'd much prefer to separate the two functions of
969 * texImage->Data - storage for texture images in main memory
970 * and access (ie mappings) of images. In other words, we'd
971 * create a new texImage->Map field and leave Data simply for
974 assert(texImage
->Data
);
977 depth
= texImage
->Depth
;
980 dest
= (GLubyte
*) pixels
;
982 for (i
= 0; i
< depth
; i
++) {
983 if (compressed_dst
) {
984 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
988 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
992 if (stImage
->pt
&& i
+ 1 < depth
) {
993 /* unmap this slice */
994 st_texture_image_unmap(ctx
->st
, stImage
);
995 /* map next slice of 3D texture */
996 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
997 PIPE_TRANSFER_READ
, 0, 0,
999 stImage
->base
.Height
);
1000 dest
+= dstImageStride
;
1004 texImage
->Depth
= depth
;
1008 st_texture_image_unmap(ctx
->st
, stImage
);
1009 texImage
->Data
= NULL
;
1015 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
1016 GLenum format
, GLenum type
, GLvoid
* pixels
,
1017 struct gl_texture_object
*texObj
,
1018 struct gl_texture_image
*texImage
)
1020 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1026 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
1028 struct gl_texture_object
*texObj
,
1029 struct gl_texture_image
*texImage
)
1031 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1038 st_TexSubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
1039 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1040 GLint width
, GLint height
, GLint depth
,
1041 GLenum format
, GLenum type
, const void *pixels
,
1042 const struct gl_pixelstore_attrib
*packing
,
1043 struct gl_texture_object
*texObj
,
1044 struct gl_texture_image
*texImage
)
1046 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
1047 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1048 GLuint dstRowStride
;
1049 const GLuint srcImageStride
=
1050 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1053 /* init to silence warning only: */
1054 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1056 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1057 _mesa_lookup_enum_by_nr(target
),
1058 level
, xoffset
, yoffset
, width
, height
);
1061 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1062 type
, pixels
, packing
, "glTexSubImage2D");
1066 /* See if we can do texture compression with a blit/render.
1068 if (!ctx
->Mesa_DXTn
&&
1069 is_compressed_mesa_format(texImage
->TexFormat
) &&
1070 screen
->is_format_supported(screen
,
1071 stImage
->pt
->format
,
1072 stImage
->pt
->target
,
1073 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
1074 if (compress_with_blit(ctx
, target
, level
,
1075 xoffset
, yoffset
, zoffset
,
1076 width
, height
, depth
,
1077 format
, type
, pixels
, packing
, texImage
)) {
1082 /* Map buffer if necessary. Need to lock to prevent other contexts
1083 * from uploading the buffer under us.
1086 unsigned face
= _mesa_tex_target_to_face(target
);
1088 if (format
== GL_DEPTH_COMPONENT
&&
1089 pf_is_depth_and_stencil(stImage
->pt
->format
))
1090 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1092 transfer_usage
= PIPE_TRANSFER_WRITE
;
1094 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1096 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
,
1102 if (!texImage
->Data
) {
1103 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1107 src
= (const GLubyte
*) pixels
;
1108 dstRowStride
= stImage
->transfer
->stride
;
1110 for (i
= 0; i
< depth
; i
++) {
1111 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1112 texImage
->TexFormat
,
1116 texImage
->ImageOffsets
,
1118 format
, type
, src
, packing
)) {
1119 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1122 if (stImage
->pt
&& i
+ 1 < depth
) {
1123 /* unmap this slice */
1124 st_texture_image_unmap(ctx
->st
, stImage
);
1125 /* map next slice of 3D texture */
1126 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
,
1131 src
+= srcImageStride
;
1136 _mesa_unmap_teximage_pbo(ctx
, packing
);
1139 st_texture_image_unmap(ctx
->st
, stImage
);
1140 texImage
->Data
= NULL
;
1147 st_TexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1148 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1149 GLsizei width
, GLsizei height
, GLsizei depth
,
1150 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1151 const struct gl_pixelstore_attrib
*packing
,
1152 struct gl_texture_object
*texObj
,
1153 struct gl_texture_image
*texImage
)
1155 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1156 width
, height
, depth
, format
, type
,
1157 pixels
, packing
, texObj
, texImage
);
1162 st_TexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1163 GLint xoffset
, GLint yoffset
,
1164 GLsizei width
, GLsizei height
,
1165 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1166 const struct gl_pixelstore_attrib
*packing
,
1167 struct gl_texture_object
*texObj
,
1168 struct gl_texture_image
*texImage
)
1170 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1171 width
, height
, 1, format
, type
,
1172 pixels
, packing
, texObj
, texImage
);
1177 st_TexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1178 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1179 const GLvoid
* pixels
,
1180 const struct gl_pixelstore_attrib
*packing
,
1181 struct gl_texture_object
*texObj
,
1182 struct gl_texture_image
*texImage
)
1184 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1185 format
, type
, pixels
, packing
, texObj
, texImage
);
1190 st_CompressedTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1191 GLint xoffset
, GLsizei width
,
1193 GLsizei imageSize
, const GLvoid
*data
,
1194 struct gl_texture_object
*texObj
,
1195 struct gl_texture_image
*texImage
)
1202 st_CompressedTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1203 GLint xoffset
, GLint yoffset
,
1204 GLsizei width
, GLint height
,
1206 GLsizei imageSize
, const GLvoid
*data
,
1207 struct gl_texture_object
*texObj
,
1208 struct gl_texture_image
*texImage
)
1210 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1211 struct pipe_format_block block
;
1217 unsigned face
= _mesa_tex_target_to_face(target
);
1219 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1220 PIPE_TRANSFER_WRITE
);
1221 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
1222 PIPE_TRANSFER_WRITE
,
1226 block
= stImage
->pt
->block
;
1227 srcBlockStride
= pf_get_stride(&block
, width
);
1228 dstBlockStride
= stImage
->transfer
->stride
;
1230 assert(stImage
->pt
);
1231 /* TODO find good values for block and strides */
1232 /* TODO also adjust texImage->data for yoffset/xoffset */
1236 if (!texImage
->Data
) {
1237 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1241 assert(xoffset
% block
.width
== 0);
1242 assert(yoffset
% block
.height
== 0);
1243 assert(width
% block
.width
== 0);
1244 assert(height
% block
.height
== 0);
1246 for (y
= 0; y
< height
; y
+= block
.height
) {
1247 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1248 const char *src
= (const char*)data
+ srcBlockStride
* pf_get_nblocksy(&block
, y
);
1249 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* pf_get_nblocksy(&block
, y
);
1250 memcpy(dst
, src
, pf_get_stride(&block
, width
));
1254 st_texture_image_unmap(ctx
->st
, stImage
);
1255 texImage
->Data
= NULL
;
1261 st_CompressedTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1262 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1263 GLsizei width
, GLint height
, GLint depth
,
1265 GLsizei imageSize
, const GLvoid
*data
,
1266 struct gl_texture_object
*texObj
,
1267 struct gl_texture_image
*texImage
)
1275 * Do a CopyTexSubImage operation using a read transfer from the source,
1276 * a write transfer to the destination and get_tile()/put_tile() to access
1277 * the pixels/texels.
1279 * Note: srcY=0=TOP of renderbuffer
1282 fallback_copy_texsubimage(GLcontext
*ctx
, GLenum target
, GLint level
,
1283 struct st_renderbuffer
*strb
,
1284 struct st_texture_image
*stImage
,
1286 GLint destX
, GLint destY
, GLint destZ
,
1287 GLint srcX
, GLint srcY
,
1288 GLsizei width
, GLsizei height
)
1290 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1291 struct pipe_screen
*screen
= pipe
->screen
;
1292 struct pipe_transfer
*src_trans
;
1294 enum pipe_transfer_usage transfer_usage
;
1296 if (ST_DEBUG
& DEBUG_FALLBACK
)
1297 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1299 assert(width
<= MAX_WIDTH
);
1301 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1302 srcY
= strb
->Base
.Height
- srcY
- height
;
1305 src_trans
= st_cond_flush_get_tex_transfer( st_context(ctx
),
1312 if (baseFormat
== GL_DEPTH_COMPONENT
&&
1313 pf_is_depth_and_stencil(stImage
->pt
->format
))
1314 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1316 transfer_usage
= PIPE_TRANSFER_WRITE
;
1318 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, 0, 0,
1321 texDest
= st_texture_image_map(ctx
->st
, stImage
, 0, transfer_usage
,
1322 destX
, destY
, width
, height
);
1324 if (baseFormat
== GL_DEPTH_COMPONENT
||
1325 baseFormat
== GL_DEPTH24_STENCIL8
) {
1326 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1327 ctx
->Pixel
.DepthBias
!= 0.0F
);
1330 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1331 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1340 /* To avoid a large temp memory allocation, do copy row by row */
1341 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1342 uint data
[MAX_WIDTH
];
1343 pipe_get_tile_z(src_trans
, 0, srcY
, width
, 1, data
);
1345 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1347 pipe_put_tile_z(stImage
->transfer
, 0, row
, width
, 1, data
);
1353 (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
1355 if (tempSrc
&& texDest
) {
1356 const GLint dims
= 2;
1357 const GLint dstRowStride
= stImage
->transfer
->stride
;
1358 struct gl_texture_image
*texImage
= &stImage
->base
;
1359 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1361 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1362 unpack
.Invert
= GL_TRUE
;
1365 /* get float/RGBA image from framebuffer */
1366 /* XXX this usually involves a lot of int/float conversion.
1367 * try to avoid that someday.
1369 pipe_get_tile_rgba(src_trans
, 0, 0, width
, height
, tempSrc
);
1371 /* Store into texture memory.
1372 * Note that this does some special things such as pixel transfer
1373 * ops and format conversion. In particular, if the dest tex format
1374 * is actually RGBA but the user created the texture as GL_RGB we
1375 * need to fill-in/override the alpha channel with 1.0.
1377 _mesa_texstore(ctx
, dims
,
1378 texImage
->_BaseFormat
,
1379 texImage
->TexFormat
,
1383 texImage
->ImageOffsets
,
1385 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1389 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1393 _mesa_free(tempSrc
);
1396 st_texture_image_unmap(ctx
->st
, stImage
);
1397 screen
->tex_transfer_destroy(src_trans
);
1402 compatible_src_dst_formats(const struct gl_renderbuffer
*src
,
1403 const struct gl_texture_image
*dst
)
1405 const GLenum srcFormat
= _mesa_get_format_base_format(src
->Format
);
1406 const GLenum dstLogicalFormat
= _mesa_get_format_base_format(dst
->TexFormat
);
1408 if (srcFormat
== dstLogicalFormat
) {
1409 /* This is the same as matching_base_formats, which should
1410 * always pass, as it did previously.
1412 return TGSI_WRITEMASK_XYZW
;
1414 else if (srcFormat
== GL_RGBA
&&
1415 dstLogicalFormat
== GL_RGB
) {
1416 /* Add a single special case to cope with RGBA->RGB transfers,
1417 * setting A to 1.0 to cope with situations where the RGB
1418 * destination is actually stored as RGBA.
1420 return TGSI_WRITEMASK_XYZ
; /* A ==> 1.0 */
1423 if (ST_DEBUG
& DEBUG_FALLBACK
)
1424 debug_printf("%s failed for src %s, dst %s\n",
1426 _mesa_lookup_enum_by_nr(srcFormat
),
1427 _mesa_lookup_enum_by_nr(dstLogicalFormat
));
1438 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1439 * Note that the region to copy has already been clipped so we know we
1440 * won't read from outside the source renderbuffer's bounds.
1442 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1445 st_copy_texsubimage(GLcontext
*ctx
,
1446 GLenum target
, GLint level
,
1447 GLint destX
, GLint destY
, GLint destZ
,
1448 GLint srcX
, GLint srcY
,
1449 GLsizei width
, GLsizei height
)
1451 struct gl_texture_unit
*texUnit
=
1452 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1453 struct gl_texture_object
*texObj
=
1454 _mesa_select_tex_object(ctx
, texUnit
, target
);
1455 struct gl_texture_image
*texImage
=
1456 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1457 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1458 const GLenum texBaseFormat
= texImage
->InternalFormat
;
1459 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1460 struct st_renderbuffer
*strb
;
1461 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1462 struct pipe_screen
*screen
= pipe
->screen
;
1463 enum pipe_format dest_format
, src_format
;
1464 GLboolean use_fallback
= GL_TRUE
;
1465 GLboolean matching_base_formats
;
1466 GLuint format_writemask
;
1467 struct pipe_surface
*dest_surface
= NULL
;
1468 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1470 /* any rendering in progress must flushed before we grab the fb image */
1471 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
1473 /* make sure finalize_textures has been called?
1475 if (0) st_validate_state(ctx
->st
);
1477 /* determine if copying depth or color data */
1478 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1479 texBaseFormat
== GL_DEPTH24_STENCIL8
) {
1480 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1482 else if (texBaseFormat
== GL_DEPTH_STENCIL_EXT
) {
1483 strb
= st_renderbuffer(fb
->_StencilBuffer
);
1486 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1487 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1490 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1491 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1519 if (width
< 0 || height
< 0)
1524 assert(strb
->surface
);
1525 assert(stImage
->pt
);
1527 src_format
= strb
->surface
->format
;
1528 dest_format
= stImage
->pt
->format
;
1531 * Determine if the src framebuffer and dest texture have the same
1532 * base format. We need this to detect a case such as the framebuffer
1533 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1534 * texture format stores RGBA we need to set A=1 (overriding the
1535 * framebuffer's alpha values). We can't do that with the blit or
1536 * textured-quad paths.
1538 matching_base_formats
=
1539 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1540 _mesa_get_format_base_format(texImage
->TexFormat
));
1541 format_writemask
= compatible_src_dst_formats(&strb
->Base
, texImage
);
1543 if (ctx
->_ImageTransferState
== 0x0) {
1545 if (pipe
->surface_copy
&&
1546 matching_base_formats
&&
1547 src_format
== dest_format
&&
1550 /* use surface_copy() / blit */
1552 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1553 stImage
->face
, stImage
->level
,
1555 PIPE_BUFFER_USAGE_GPU_WRITE
);
1557 /* for surface_copy(), y=0=top, always */
1558 pipe
->surface_copy(pipe
,
1567 use_fallback
= GL_FALSE
;
1569 else if (format_writemask
&&
1570 screen
->is_format_supported(screen
, src_format
,
1572 PIPE_TEXTURE_USAGE_SAMPLER
,
1574 screen
->is_format_supported(screen
, dest_format
,
1576 PIPE_TEXTURE_USAGE_RENDER_TARGET
,
1578 /* draw textured quad to do the copy */
1581 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1582 stImage
->face
, stImage
->level
,
1584 PIPE_BUFFER_USAGE_GPU_WRITE
);
1587 srcY1
= strb
->Base
.Height
- srcY
- height
;
1588 srcY0
= srcY1
+ height
;
1592 srcY1
= srcY0
+ height
;
1594 util_blit_pixels_writemask(ctx
->st
->blit
,
1597 srcX
+ width
, srcY1
,
1600 destX
+ width
, destY
+ height
,
1601 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1603 use_fallback
= GL_FALSE
;
1607 pipe_surface_reference(&dest_surface
, NULL
);
1611 /* software fallback */
1612 fallback_copy_texsubimage(ctx
, target
, level
,
1613 strb
, stImage
, texBaseFormat
,
1614 destX
, destY
, destZ
,
1615 srcX
, srcY
, width
, height
);
1622 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1623 GLenum internalFormat
,
1624 GLint x
, GLint y
, GLsizei width
, GLint border
)
1626 struct gl_texture_unit
*texUnit
=
1627 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1628 struct gl_texture_object
*texObj
=
1629 _mesa_select_tex_object(ctx
, texUnit
, target
);
1630 struct gl_texture_image
*texImage
=
1631 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1633 /* Setup or redefine the texture object, texture and texture
1634 * image. Don't populate yet.
1636 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1638 GL_RGBA
, CHAN_TYPE
, NULL
,
1639 &ctx
->DefaultPacking
, texObj
, texImage
);
1641 st_copy_texsubimage(ctx
, target
, level
,
1642 0, 0, 0, /* destX,Y,Z */
1643 x
, y
, width
, 1); /* src X, Y, size */
1648 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1649 GLenum internalFormat
,
1650 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1653 struct gl_texture_unit
*texUnit
=
1654 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1655 struct gl_texture_object
*texObj
=
1656 _mesa_select_tex_object(ctx
, texUnit
, target
);
1657 struct gl_texture_image
*texImage
=
1658 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1660 /* Setup or redefine the texture object, texture and texture
1661 * image. Don't populate yet.
1663 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1664 width
, height
, border
,
1665 GL_RGBA
, CHAN_TYPE
, NULL
,
1666 &ctx
->DefaultPacking
, texObj
, texImage
);
1668 st_copy_texsubimage(ctx
, target
, level
,
1669 0, 0, 0, /* destX,Y,Z */
1670 x
, y
, width
, height
); /* src X, Y, size */
1675 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1676 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1678 const GLint yoffset
= 0, zoffset
= 0;
1679 const GLsizei height
= 1;
1680 st_copy_texsubimage(ctx
, target
, level
,
1681 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1682 x
, y
, width
, height
); /* src X, Y, size */
1687 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1688 GLint xoffset
, GLint yoffset
,
1689 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1691 const GLint zoffset
= 0;
1692 st_copy_texsubimage(ctx
, target
, level
,
1693 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1694 x
, y
, width
, height
); /* src X, Y, size */
1699 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1700 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1701 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1703 st_copy_texsubimage(ctx
, target
, level
,
1704 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1705 x
, y
, width
, height
); /* src X, Y, size */
1710 copy_image_data_to_texture(struct st_context
*st
,
1711 struct st_texture_object
*stObj
,
1713 struct st_texture_image
*stImage
)
1716 /* Copy potentially with the blitter:
1718 st_texture_image_copy(st
->pipe
,
1719 stObj
->pt
, dstLevel
, /* dest texture, level */
1720 stImage
->pt
, /* src texture */
1724 pipe_texture_reference(&stImage
->pt
, NULL
);
1726 else if (stImage
->base
.Data
) {
1727 assert(stImage
->base
.Data
!= NULL
);
1729 /* More straightforward upload.
1732 st_teximage_flush_before_map(st
, stObj
->pt
, stImage
->face
, dstLevel
,
1733 PIPE_TRANSFER_WRITE
);
1736 st_texture_image_data(st
,
1741 stImage
->base
.RowStride
*
1742 stObj
->pt
->block
.size
,
1743 stImage
->base
.RowStride
*
1744 stImage
->base
.Height
*
1745 stObj
->pt
->block
.size
);
1746 _mesa_align_free(stImage
->base
.Data
);
1747 stImage
->base
.Data
= NULL
;
1750 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
1755 * Called during state validation. When this function is finished,
1756 * the texture object should be ready for rendering.
1757 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1760 st_finalize_texture(GLcontext
*ctx
,
1761 struct pipe_context
*pipe
,
1762 struct gl_texture_object
*tObj
,
1763 GLboolean
*needFlush
)
1765 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1766 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1768 struct st_texture_image
*firstImage
;
1770 *needFlush
= GL_FALSE
;
1772 if (stObj
->base
._Complete
) {
1773 /* The texture is complete and we know exactly how many mipmap levels
1774 * are present/needed. This is conditional because we may be called
1775 * from the st_generate_mipmap() function when the texture object is
1776 * incomplete. In that case, we'll have set stObj->lastLevel before
1779 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1780 stObj
->base
.MinFilter
== GL_NEAREST
)
1781 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1783 stObj
->lastLevel
= stObj
->base
._MaxLevel
- stObj
->base
.BaseLevel
;
1786 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1788 /* If both firstImage and stObj point to a texture which can contain
1789 * all active images, favour firstImage. Note that because of the
1790 * completeness requirement, we know that the image dimensions
1793 if (firstImage
->pt
&&
1794 firstImage
->pt
!= stObj
->pt
&&
1795 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1796 pipe_texture_reference(&stObj
->pt
, firstImage
->pt
);
1799 /* FIXME: determine format block instead of cpp */
1800 if (_mesa_is_format_compressed(firstImage
->base
.TexFormat
)) {
1801 cpp
= compressed_num_bytes(firstImage
->base
.TexFormat
);
1804 cpp
= _mesa_get_format_bytes(firstImage
->base
.TexFormat
);
1807 /* If we already have a gallium texture, check that it matches the texture
1808 * object's format, target, size, num_levels, etc.
1811 const enum pipe_format fmt
=
1812 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1813 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1814 stObj
->pt
->format
!= fmt
||
1815 stObj
->pt
->last_level
< stObj
->lastLevel
||
1816 stObj
->pt
->width
[0] != firstImage
->base
.Width2
||
1817 stObj
->pt
->height
[0] != firstImage
->base
.Height2
||
1818 stObj
->pt
->depth
[0] != firstImage
->base
.Depth2
||
1819 /* Nominal bytes per pixel: */
1820 stObj
->pt
->block
.size
/ stObj
->pt
->block
.width
!= cpp
)
1822 pipe_texture_reference(&stObj
->pt
, NULL
);
1823 ctx
->st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1827 /* May need to create a new gallium texture:
1830 const enum pipe_format fmt
=
1831 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1832 GLuint usage
= default_usage(fmt
);
1834 stObj
->pt
= st_texture_create(ctx
->st
,
1835 gl_target_to_pipe(stObj
->base
.Target
),
1838 firstImage
->base
.Width2
,
1839 firstImage
->base
.Height2
,
1840 firstImage
->base
.Depth2
,
1844 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1849 /* Pull in any images not in the object's texture:
1851 for (face
= 0; face
< nr_faces
; face
++) {
1853 for (level
= 0; level
<= stObj
->lastLevel
; level
++) {
1854 struct st_texture_image
*stImage
=
1855 st_texture_image(stObj
->base
.Image
[face
][stObj
->base
.BaseLevel
+ level
]);
1857 /* Need to import images in main memory or held in other textures.
1859 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1860 copy_image_data_to_texture(ctx
->st
, stObj
, level
, stImage
);
1861 *needFlush
= GL_TRUE
;
1871 * Returns pointer to a default/dummy texture.
1872 * This is typically used when the current shader has tex/sample instructions
1873 * but the user has not provided a (any) texture(s).
1875 struct gl_texture_object
*
1876 st_get_default_texture(struct st_context
*st
)
1878 if (!st
->default_texture
) {
1879 static const GLenum target
= GL_TEXTURE_2D
;
1880 GLubyte pixels
[16][16][4];
1881 struct gl_texture_object
*texObj
;
1882 struct gl_texture_image
*texImg
;
1885 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1886 * when attempting to sample incomplete textures.
1888 for (i
= 0; i
< 16; i
++) {
1889 for (j
= 0; j
< 16; j
++) {
1890 pixels
[i
][j
][0] = 0;
1891 pixels
[i
][j
][1] = 0;
1892 pixels
[i
][j
][2] = 0;
1893 pixels
[i
][j
][3] = 255;
1897 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1899 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1901 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1902 16, 16, 1, 0, /* w, h, d, border */
1905 st_TexImage(st
->ctx
, 2, target
,
1906 0, GL_RGBA
, /* level, intformat */
1907 16, 16, 1, 0, /* w, h, d, border */
1908 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1909 &st
->ctx
->DefaultPacking
,
1913 texObj
->MinFilter
= GL_NEAREST
;
1914 texObj
->MagFilter
= GL_NEAREST
;
1915 texObj
->_Complete
= GL_TRUE
;
1917 st
->default_texture
= texObj
;
1919 return st
->default_texture
;
1924 st_init_texture_functions(struct dd_function_table
*functions
)
1926 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1927 functions
->TexImage1D
= st_TexImage1D
;
1928 functions
->TexImage2D
= st_TexImage2D
;
1929 functions
->TexImage3D
= st_TexImage3D
;
1930 functions
->TexSubImage1D
= st_TexSubImage1D
;
1931 functions
->TexSubImage2D
= st_TexSubImage2D
;
1932 functions
->TexSubImage3D
= st_TexSubImage3D
;
1933 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1934 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1935 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1936 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1937 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1938 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1939 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1940 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1941 functions
->GenerateMipmap
= st_generate_mipmap
;
1943 functions
->GetTexImage
= st_GetTexImage
;
1945 /* compressed texture functions */
1946 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1947 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1949 functions
->NewTextureObject
= st_NewTextureObject
;
1950 functions
->NewTextureImage
= st_NewTextureImage
;
1951 functions
->DeleteTexture
= st_DeleteTextureObject
;
1952 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1953 functions
->UpdateTexturePalette
= 0;
1955 functions
->TextureMemCpy
= do_memcpy
;
1957 /* XXX Temporary until we can query pipe's texture sizes */
1958 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;