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/image.h"
35 #include "main/imports.h"
36 #include "main/macros.h"
37 #include "main/mipmap.h"
38 #include "main/pixel.h"
39 #include "main/texcompress.h"
40 #include "main/texformat.h"
41 #include "main/texgetimage.h"
42 #include "main/teximage.h"
43 #include "main/texobj.h"
44 #include "main/texstore.h"
46 #include "state_tracker/st_context.h"
47 #include "state_tracker/st_cb_fbo.h"
48 #include "state_tracker/st_cb_texture.h"
49 #include "state_tracker/st_format.h"
50 #include "state_tracker/st_public.h"
51 #include "state_tracker/st_texture.h"
52 #include "state_tracker/st_gen_mipmap.h"
53 #include "state_tracker/st_inlines.h"
54 #include "state_tracker/st_atom.h"
56 #include "pipe/p_context.h"
57 #include "pipe/p_defines.h"
58 #include "pipe/p_inlines.h"
59 #include "pipe/p_shader_tokens.h"
60 #include "util/u_tile.h"
61 #include "util/u_blit.h"
62 #include "util/u_surface.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
;
94 * Return nominal bytes per texel for a compressed format, 0 for non-compressed
98 compressed_num_bytes(GLuint mesaFormat
)
101 #if FEATURE_texture_fxt1
102 case MESA_FORMAT_RGB_FXT1
:
103 case MESA_FORMAT_RGBA_FXT1
:
105 #if FEATURE_texture_s3tc
106 case MESA_FORMAT_RGB_DXT1
:
107 case MESA_FORMAT_RGBA_DXT1
:
109 case MESA_FORMAT_RGBA_DXT3
:
110 case MESA_FORMAT_RGBA_DXT5
:
120 is_compressed_mesa_format(const struct gl_texture_format
*format
)
122 switch (format
->MesaFormat
) {
123 case MESA_FORMAT_RGB_DXT1
:
124 case MESA_FORMAT_RGBA_DXT1
:
125 case MESA_FORMAT_RGBA_DXT3
:
126 case MESA_FORMAT_RGBA_DXT5
:
127 case MESA_FORMAT_SRGB_DXT1
:
128 case MESA_FORMAT_SRGBA_DXT1
:
129 case MESA_FORMAT_SRGBA_DXT3
:
130 case MESA_FORMAT_SRGBA_DXT5
:
138 /** called via ctx->Driver.NewTextureImage() */
139 static struct gl_texture_image
*
140 st_NewTextureImage(GLcontext
* ctx
)
142 DBG("%s\n", __FUNCTION__
);
144 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
148 /** called via ctx->Driver.NewTextureObject() */
149 static struct gl_texture_object
*
150 st_NewTextureObject(GLcontext
* ctx
, GLuint name
, GLenum target
)
152 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
154 DBG("%s\n", __FUNCTION__
);
155 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
160 /** called via ctx->Driver.DeleteTextureImage() */
162 st_DeleteTextureObject(GLcontext
*ctx
,
163 struct gl_texture_object
*texObj
)
165 struct st_texture_object
*stObj
= st_texture_object(texObj
);
167 pipe_texture_reference(&stObj
->pt
, NULL
);
169 _mesa_delete_texture_object(ctx
, texObj
);
173 /** called via ctx->Driver.FreeTexImageData() */
175 st_FreeTextureImageData(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
177 struct st_texture_image
*stImage
= st_texture_image(texImage
);
179 DBG("%s\n", __FUNCTION__
);
182 pipe_texture_reference(&stImage
->pt
, NULL
);
185 if (texImage
->Data
) {
186 _mesa_align_free(texImage
->Data
);
187 texImage
->Data
= NULL
;
193 * From linux kernel i386 header files, copes with odd sizes better
194 * than COPY_DWORDS would:
195 * XXX Put this in src/mesa/main/imports.h ???
197 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
199 __memcpy(void *to
, const void *from
, size_t n
)
202 __asm__
__volatile__("rep ; movsl\n\t"
206 "1:\ttestb $1,%b4\n\t"
208 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
209 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
214 #define __memcpy(a,b,c) memcpy(a,b,c)
219 * The system memcpy (at least on ubuntu 5.10) has problems copying
220 * to agp (writecombined) memory from a source which isn't 64-byte
221 * aligned - there is a 4x performance falloff.
223 * The x86 __memcpy is immune to this but is slightly slower
224 * (10%-ish) than the system memcpy.
226 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
227 * isn't much faster than x86_memcpy for agp copies.
229 * TODO: switch dynamically.
232 do_memcpy(void *dest
, const void *src
, size_t n
)
234 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
235 return __memcpy(dest
, src
, n
);
238 return memcpy(dest
, src
, n
);
243 * Return default texture usage bitmask for the given texture format.
246 default_usage(enum pipe_format fmt
)
248 GLuint usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
249 if (pf_is_depth_stencil(fmt
))
250 usage
|= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
252 usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
258 * Allocate a pipe_texture object for the given st_texture_object using
259 * the given st_texture_image to guess the mipmap size/levels.
262 * Otherwise, store it in memory if (Border != 0) or (any dimension ==
265 * Otherwise, if max_level >= level >= min_level, create texture with
266 * space for images from min_level down to max_level.
268 * Otherwise, create texture with space for images from (level 0)..(1x1).
269 * Consider pruning this texture at a validation if the saving is worth it.
272 guess_and_alloc_texture(struct st_context
*st
,
273 struct st_texture_object
*stObj
,
274 const struct st_texture_image
*stImage
)
278 GLuint width
= stImage
->base
.Width2
; /* size w/out border */
279 GLuint height
= stImage
->base
.Height2
;
280 GLuint depth
= stImage
->base
.Depth2
;
282 enum pipe_format fmt
;
284 DBG("%s\n", __FUNCTION__
);
289 (GLint
) stImage
->level
> stObj
->base
.BaseLevel
&&
290 (stImage
->base
.Width
== 1 ||
291 (stObj
->base
.Target
!= GL_TEXTURE_1D
&&
292 stImage
->base
.Height
== 1) ||
293 (stObj
->base
.Target
== GL_TEXTURE_3D
&&
294 stImage
->base
.Depth
== 1)))
297 /* If this image disrespects BaseLevel, allocate from level zero.
298 * Usually BaseLevel == 0, so it's unlikely to happen.
300 if ((GLint
) stImage
->level
< stObj
->base
.BaseLevel
)
303 firstLevel
= stObj
->base
.BaseLevel
;
306 /* Figure out image dimensions at start level.
308 for (i
= stImage
->level
; i
> firstLevel
; i
--) {
317 if (width
== 0 || height
== 0 || depth
== 0) {
318 /* no texture needed */
322 /* Guess a reasonable value for lastLevel. This is probably going
323 * to be wrong fairly often and might mean that we have to look at
324 * resizable buffers, or require that buffers implement lazy
325 * pagetable arrangements.
327 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
328 stObj
->base
.MinFilter
== GL_LINEAR
||
329 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
330 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
331 stImage
->level
== firstLevel
) {
332 lastLevel
= firstLevel
;
335 GLuint l2width
= util_logbase2(width
);
336 GLuint l2height
= util_logbase2(height
);
337 GLuint l2depth
= util_logbase2(depth
);
338 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
341 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
->MesaFormat
);
343 usage
= default_usage(fmt
);
345 stObj
->pt
= st_texture_create(st
,
346 gl_target_to_pipe(stObj
->base
.Target
),
354 DBG("%s - success\n", __FUNCTION__
);
359 * Adjust pixel unpack params and image dimensions to strip off the
361 * Gallium doesn't support texture borders. They've seldem been used
362 * and seldom been implemented correctly anyway.
363 * \param unpackNew returns the new pixel unpack parameters
366 strip_texture_border(GLint border
,
367 GLint
*width
, GLint
*height
, GLint
*depth
,
368 const struct gl_pixelstore_attrib
*unpack
,
369 struct gl_pixelstore_attrib
*unpackNew
)
371 assert(border
> 0); /* sanity check */
373 *unpackNew
= *unpack
;
375 if (unpackNew
->RowLength
== 0)
376 unpackNew
->RowLength
= *width
;
378 if (depth
&& unpackNew
->ImageHeight
== 0)
379 unpackNew
->ImageHeight
= *height
;
381 unpackNew
->SkipPixels
+= border
;
383 unpackNew
->SkipRows
+= border
;
385 unpackNew
->SkipImages
+= border
;
388 *width
= *width
- 2 * border
;
389 if (height
&& *height
>= 3)
390 *height
= *height
- 2 * border
;
391 if (depth
&& *depth
>= 3)
392 *depth
= *depth
- 2 * border
;
397 * Try to do texture compression via rendering. If the Gallium driver
398 * can render into a compressed surface this will allow us to do texture
400 * \return GL_TRUE for success, GL_FALSE for failure
403 compress_with_blit(GLcontext
* ctx
,
404 GLenum target
, GLint level
,
405 GLint xoffset
, GLint yoffset
, GLint zoffset
,
406 GLint width
, GLint height
, GLint depth
,
407 GLenum format
, GLenum type
, const void *pixels
,
408 const struct gl_pixelstore_attrib
*unpack
,
409 struct gl_texture_image
*texImage
)
411 const GLuint dstImageOffsets
[1] = {0};
412 struct st_texture_image
*stImage
= st_texture_image(texImage
);
413 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
414 const struct gl_texture_format
*mesa_format
;
415 struct pipe_texture templ
;
416 struct pipe_texture
*src_tex
;
417 struct pipe_surface
*dst_surface
;
418 struct pipe_transfer
*tex_xfer
;
422 /* XXX: Can this happen? Should we assert? */
426 /* get destination surface (in the compressed texture) */
427 dst_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
428 stImage
->face
, stImage
->level
, 0,
429 PIPE_BUFFER_USAGE_GPU_WRITE
);
431 /* can't render into this format (or other problem) */
435 /* Choose format for the temporary RGBA texture image.
437 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
442 /* Create the temporary source texture
444 memset(&templ
, 0, sizeof(templ
));
445 templ
.target
= PIPE_TEXTURE_2D
;
446 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
->MesaFormat
);
447 pf_get_block(templ
.format
, &templ
.block
);
448 templ
.width
[0] = width
;
449 templ
.height
[0] = height
;
451 templ
.last_level
= 0;
452 templ
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
453 src_tex
= screen
->texture_create(screen
, &templ
);
458 /* Put user's tex data into the temporary texture
460 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
), src_tex
,
461 0, 0, 0, /* face, level are zero */
463 0, 0, width
, height
); /* x, y, w, h */
464 map
= screen
->transfer_map(screen
, tex_xfer
);
466 _mesa_texstore(ctx
, 2, GL_RGBA
, mesa_format
,
468 0, 0, 0, /* dest x/y/z offset */
469 tex_xfer
->stride
, /* dest row stride (bytes) */
470 dstImageOffsets
, /* image offsets (for 3D only) */
471 width
, height
, 1, /* size */
472 format
, type
, /* source format/type */
473 pixels
, /* source data */
474 unpack
); /* source data packing */
476 screen
->transfer_unmap(screen
, tex_xfer
);
477 screen
->tex_transfer_destroy(tex_xfer
);
479 /* copy / compress image */
480 util_blit_pixels_tex(ctx
->st
->blit
,
481 src_tex
, /* pipe_texture (src) */
482 0, 0, /* src x0, y0 */
483 width
, height
, /* src x1, y1 */
484 dst_surface
, /* pipe_surface (dst) */
485 xoffset
, yoffset
, /* dst x0, y0 */
486 xoffset
+ width
, /* dst x1 */
487 yoffset
+ height
, /* dst y1 */
489 PIPE_TEX_MIPFILTER_NEAREST
);
491 pipe_surface_reference(&dst_surface
, NULL
);
492 pipe_texture_reference(&src_tex
, NULL
);
499 * Do glTexImage1/2/3D().
502 st_TexImage(GLcontext
* ctx
,
504 GLenum target
, GLint level
,
505 GLint internalFormat
,
506 GLint width
, GLint height
, GLint depth
,
508 GLenum format
, GLenum type
, const void *pixels
,
509 const struct gl_pixelstore_attrib
*unpack
,
510 struct gl_texture_object
*texObj
,
511 struct gl_texture_image
*texImage
,
512 GLsizei imageSize
, GLboolean compressed_src
)
514 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
515 struct st_texture_object
*stObj
= st_texture_object(texObj
);
516 struct st_texture_image
*stImage
= st_texture_image(texImage
);
517 GLint postConvWidth
, postConvHeight
;
518 GLint texelBytes
, sizeInBytes
;
519 GLuint dstRowStride
= 0;
520 struct gl_pixelstore_attrib unpackNB
;
521 enum pipe_transfer_usage transfer_usage
= 0;
523 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
524 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
526 /* switch to "normal" */
527 if (stObj
->surface_based
) {
528 _mesa_clear_texture_object(ctx
, texObj
);
529 stObj
->surface_based
= GL_FALSE
;
532 /* gallium does not support texture borders, strip it off */
534 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
536 texImage
->Width
= width
;
537 texImage
->Height
= height
;
538 texImage
->Depth
= depth
;
539 texImage
->Border
= 0;
543 postConvWidth
= width
;
544 postConvHeight
= height
;
546 stImage
->face
= _mesa_tex_target_to_face(target
);
547 stImage
->level
= level
;
550 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
551 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
556 /* choose the texture format */
557 texImage
->TexFormat
= st_ChooseTextureFormat(ctx
, internalFormat
,
560 _mesa_set_fetch_functions(texImage
, dims
);
562 if (_mesa_is_format_compressed(texImage
->TexFormat
->MesaFormat
)) {
563 /* must be a compressed format */
565 texImage
->IsCompressed
= GL_TRUE
;
566 texImage
->CompressedSize
=
567 ctx
->Driver
.CompressedTextureSize(ctx
, texImage
->Width
,
568 texImage
->Height
, texImage
->Depth
,
569 texImage
->TexFormat
->MesaFormat
);
572 texelBytes
= _mesa_get_format_bytes(texImage
->TexFormat
->MesaFormat
);
574 /* Minimum pitch of 32 bytes */
575 if (postConvWidth
* texelBytes
< 32) {
576 postConvWidth
= 32 / texelBytes
;
577 texImage
->RowStride
= postConvWidth
;
580 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
581 /*assert(texImage->RowStride == postConvWidth);*/
584 /* Release the reference to a potentially orphaned buffer.
585 * Release any old malloced memory.
588 pipe_texture_reference(&stImage
->pt
, NULL
);
589 assert(!texImage
->Data
);
591 else if (texImage
->Data
) {
592 _mesa_align_free(texImage
->Data
);
595 if (width
== 0 || height
== 0 || depth
== 0) {
596 /* stop after freeing old image */
600 /* If this is the only mipmap level in the texture, could call
601 * bmBufferData with NULL data to free the old block and avoid
602 * waiting on any outstanding fences.
605 if (stObj
->teximage_realloc
||
606 level
> (GLint
) stObj
->pt
->last_level
||
607 (stObj
->pt
->last_level
== level
&&
608 stObj
->pt
->target
!= PIPE_TEXTURE_CUBE
&&
609 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
610 stImage
->face
, stImage
->level
))) {
612 pipe_texture_reference(&stObj
->pt
, NULL
);
614 stObj
->teximage_realloc
= FALSE
;
619 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
621 /* Probably out of memory.
622 * Try flushing any pending rendering, then retry.
625 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
627 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
633 assert(!stImage
->pt
);
636 st_texture_match_image(stObj
->pt
, &stImage
->base
,
637 stImage
->face
, stImage
->level
)) {
639 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
644 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
646 /* st_CopyTexImage calls this function with pixels == NULL, with
647 * the expectation that the texture will be set up but nothing
648 * more will be done. This is where those calls return:
650 if (compressed_src
) {
651 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
653 "glCompressedTexImage");
656 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
658 pixels
, unpack
, "glTexImage");
661 /* Note: we can't check for pixels==NULL until after we've allocated
662 * memory for the texture.
665 /* See if we can do texture compression with a blit/render.
667 if (!compressed_src
&&
669 is_compressed_mesa_format(texImage
->TexFormat
) &&
670 screen
->is_format_supported(screen
,
673 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
677 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
678 format
, type
, pixels
, unpack
, texImage
)) {
684 if (format
== GL_DEPTH_COMPONENT
&&
685 pf_is_depth_and_stencil(stImage
->pt
->format
))
686 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
688 transfer_usage
= PIPE_TRANSFER_WRITE
;
690 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
691 transfer_usage
, 0, 0,
693 stImage
->base
.Height
);
694 if(stImage
->transfer
)
695 dstRowStride
= stImage
->transfer
->stride
;
698 /* Allocate regular memory and store the image there temporarily. */
699 if (texImage
->IsCompressed
) {
700 sizeInBytes
= texImage
->CompressedSize
;
702 _mesa_compressed_row_stride(texImage
->TexFormat
->MesaFormat
, width
);
706 dstRowStride
= postConvWidth
* texelBytes
;
707 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
710 texImage
->Data
= _mesa_align_malloc(sizeInBytes
, 16);
713 if (!texImage
->Data
) {
714 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
721 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
722 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
724 /* Copy data. Would like to know when it's ok for us to eg. use
725 * the blitter to copy. Or, use the hardware to do the format
726 * conversion and copy:
728 if (compressed_src
) {
729 memcpy(texImage
->Data
, pixels
, imageSize
);
732 const GLuint srcImageStride
=
733 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
735 const GLubyte
*src
= (const GLubyte
*) pixels
;
737 for (i
= 0; i
< depth
; i
++) {
738 if (!_mesa_texstore(ctx
, dims
,
739 texImage
->_BaseFormat
,
742 0, 0, 0, /* dstX/Y/Zoffset */
744 texImage
->ImageOffsets
,
746 format
, type
, src
, unpack
)) {
747 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
750 if (stImage
->pt
&& i
+ 1 < depth
) {
751 /* unmap this slice */
752 st_texture_image_unmap(ctx
->st
, stImage
);
753 /* map next slice of 3D texture */
754 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
755 transfer_usage
, 0, 0,
757 stImage
->base
.Height
);
758 src
+= srcImageStride
;
764 _mesa_unmap_teximage_pbo(ctx
, unpack
);
766 if (stImage
->pt
&& texImage
->Data
) {
767 st_texture_image_unmap(ctx
->st
, stImage
);
768 texImage
->Data
= NULL
;
774 st_TexImage3D(GLcontext
* ctx
,
775 GLenum target
, GLint level
,
776 GLint internalFormat
,
777 GLint width
, GLint height
, GLint depth
,
779 GLenum format
, GLenum type
, const void *pixels
,
780 const struct gl_pixelstore_attrib
*unpack
,
781 struct gl_texture_object
*texObj
,
782 struct gl_texture_image
*texImage
)
784 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
785 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
791 st_TexImage2D(GLcontext
* ctx
,
792 GLenum target
, GLint level
,
793 GLint internalFormat
,
794 GLint width
, GLint height
, GLint border
,
795 GLenum format
, GLenum type
, const void *pixels
,
796 const struct gl_pixelstore_attrib
*unpack
,
797 struct gl_texture_object
*texObj
,
798 struct gl_texture_image
*texImage
)
800 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
801 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
806 st_TexImage1D(GLcontext
* ctx
,
807 GLenum target
, GLint level
,
808 GLint internalFormat
,
809 GLint width
, GLint border
,
810 GLenum format
, GLenum type
, const void *pixels
,
811 const struct gl_pixelstore_attrib
*unpack
,
812 struct gl_texture_object
*texObj
,
813 struct gl_texture_image
*texImage
)
815 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
816 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
821 st_CompressedTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
822 GLint internalFormat
,
823 GLint width
, GLint height
, GLint border
,
824 GLsizei imageSize
, const GLvoid
*data
,
825 struct gl_texture_object
*texObj
,
826 struct gl_texture_image
*texImage
)
828 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
829 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
835 * glGetTexImage() helper: decompress a compressed texture by rendering
836 * a textured quad. Store the results in the user's buffer.
839 decompress_with_blit(GLcontext
* ctx
, GLenum target
, GLint level
,
840 GLenum format
, GLenum type
, GLvoid
*pixels
,
841 struct gl_texture_object
*texObj
,
842 struct gl_texture_image
*texImage
)
844 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
845 struct st_texture_image
*stImage
= st_texture_image(texImage
);
846 const GLuint width
= texImage
->Width
;
847 const GLuint height
= texImage
->Height
;
848 struct pipe_surface
*dst_surface
;
849 struct pipe_texture
*dst_texture
;
850 struct pipe_transfer
*tex_xfer
;
852 /* create temp / dest surface */
853 if (!util_create_rgba_surface(screen
, width
, height
,
854 &dst_texture
, &dst_surface
)) {
855 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
856 "in decompress_with_blit()");
860 /* blit/render/decompress */
861 util_blit_pixels_tex(ctx
->st
->blit
,
862 stImage
->pt
, /* pipe_texture (src) */
863 0, 0, /* src x0, y0 */
864 width
, height
, /* src x1, y1 */
865 dst_surface
, /* pipe_surface (dst) */
866 0, 0, /* dst x0, y0 */
867 width
, height
, /* dst x1, y1 */
869 PIPE_TEX_MIPFILTER_NEAREST
);
871 /* map the dst_surface so we can read from it */
872 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
),
873 dst_texture
, 0, 0, 0,
875 0, 0, width
, height
);
877 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
879 /* copy/pack data into user buffer */
880 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
882 const uint bytesPerRow
= width
* pf_get_size(stImage
->pt
->format
);
883 ubyte
*map
= screen
->transfer_map(screen
, tex_xfer
);
885 for (row
= 0; row
< height
; row
++) {
886 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
887 height
, format
, type
, row
, 0);
888 memcpy(dest
, map
, bytesPerRow
);
889 map
+= tex_xfer
->stride
;
891 screen
->transfer_unmap(screen
, tex_xfer
);
894 /* format translation via floats */
896 for (row
= 0; row
< height
; row
++) {
897 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
898 GLfloat rgba
[4 * MAX_WIDTH
];
899 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
900 height
, format
, type
, row
, 0);
902 /* get float[4] rgba row from surface */
903 pipe_get_tile_rgba(tex_xfer
, 0, row
, width
, 1, rgba
);
905 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
906 type
, dest
, &ctx
->Pack
, transferOps
);
910 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
912 /* destroy the temp / dest surface */
913 util_destroy_rgba_surface(dst_texture
, dst_surface
);
919 * Need to map texture image into memory before copying image data,
923 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
924 GLenum format
, GLenum type
, GLvoid
* pixels
,
925 struct gl_texture_object
*texObj
,
926 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
928 struct st_texture_image
*stImage
= st_texture_image(texImage
);
929 const GLuint dstImageStride
=
930 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
936 pf_is_compressed(stImage
->pt
->format
) &&
938 /* Need to decompress the texture.
939 * We'll do this by rendering a textured quad.
940 * Note that we only expect RGBA formats (no Z/depth formats).
942 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
949 /* Image is stored in hardware format in a buffer managed by the
950 * kernel. Need to explicitly map and unmap it.
952 unsigned face
= _mesa_tex_target_to_face(target
);
954 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
957 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
958 PIPE_TRANSFER_READ
, 0, 0,
960 stImage
->base
.Height
);
961 texImage
->RowStride
= stImage
->transfer
->stride
/ stImage
->pt
->block
.size
;
964 /* Otherwise, the image should actually be stored in
965 * texImage->Data. This is pretty confusing for
966 * everybody, I'd much prefer to separate the two functions of
967 * texImage->Data - storage for texture images in main memory
968 * and access (ie mappings) of images. In other words, we'd
969 * create a new texImage->Map field and leave Data simply for
972 assert(texImage
->Data
);
975 depth
= texImage
->Depth
;
978 dest
= (GLubyte
*) pixels
;
980 for (i
= 0; i
< depth
; i
++) {
981 if (compressed_dst
) {
982 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
986 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
990 if (stImage
->pt
&& i
+ 1 < depth
) {
991 /* unmap this slice */
992 st_texture_image_unmap(ctx
->st
, stImage
);
993 /* map next slice of 3D texture */
994 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
995 PIPE_TRANSFER_READ
, 0, 0,
997 stImage
->base
.Height
);
998 dest
+= dstImageStride
;
1002 texImage
->Depth
= depth
;
1006 st_texture_image_unmap(ctx
->st
, stImage
);
1007 texImage
->Data
= NULL
;
1013 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
1014 GLenum format
, GLenum type
, GLvoid
* pixels
,
1015 struct gl_texture_object
*texObj
,
1016 struct gl_texture_image
*texImage
)
1018 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1024 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
1026 struct gl_texture_object
*texObj
,
1027 struct gl_texture_image
*texImage
)
1029 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1036 st_TexSubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
1037 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1038 GLint width
, GLint height
, GLint depth
,
1039 GLenum format
, GLenum type
, const void *pixels
,
1040 const struct gl_pixelstore_attrib
*packing
,
1041 struct gl_texture_object
*texObj
,
1042 struct gl_texture_image
*texImage
)
1044 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
1045 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1046 GLuint dstRowStride
;
1047 const GLuint srcImageStride
=
1048 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1051 /* init to silence warning only: */
1052 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1054 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1055 _mesa_lookup_enum_by_nr(target
),
1056 level
, xoffset
, yoffset
, width
, height
);
1059 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1060 type
, pixels
, packing
, "glTexSubImage2D");
1064 /* See if we can do texture compression with a blit/render.
1066 if (!ctx
->Mesa_DXTn
&&
1067 is_compressed_mesa_format(texImage
->TexFormat
) &&
1068 screen
->is_format_supported(screen
,
1069 stImage
->pt
->format
,
1070 stImage
->pt
->target
,
1071 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
1072 if (compress_with_blit(ctx
, target
, level
,
1073 xoffset
, yoffset
, zoffset
,
1074 width
, height
, depth
,
1075 format
, type
, pixels
, packing
, texImage
)) {
1080 /* Map buffer if necessary. Need to lock to prevent other contexts
1081 * from uploading the buffer under us.
1084 unsigned face
= _mesa_tex_target_to_face(target
);
1086 if (format
== GL_DEPTH_COMPONENT
&&
1087 pf_is_depth_and_stencil(stImage
->pt
->format
))
1088 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1090 transfer_usage
= PIPE_TRANSFER_WRITE
;
1092 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1094 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
,
1100 if (!texImage
->Data
) {
1101 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1105 src
= (const GLubyte
*) pixels
;
1106 dstRowStride
= stImage
->transfer
->stride
;
1108 for (i
= 0; i
< depth
; i
++) {
1109 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1110 texImage
->TexFormat
,
1114 texImage
->ImageOffsets
,
1116 format
, type
, src
, packing
)) {
1117 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1120 if (stImage
->pt
&& i
+ 1 < depth
) {
1121 /* unmap this slice */
1122 st_texture_image_unmap(ctx
->st
, stImage
);
1123 /* map next slice of 3D texture */
1124 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
,
1129 src
+= srcImageStride
;
1134 _mesa_unmap_teximage_pbo(ctx
, packing
);
1137 st_texture_image_unmap(ctx
->st
, stImage
);
1138 texImage
->Data
= NULL
;
1145 st_TexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1146 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1147 GLsizei width
, GLsizei height
, GLsizei depth
,
1148 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1149 const struct gl_pixelstore_attrib
*packing
,
1150 struct gl_texture_object
*texObj
,
1151 struct gl_texture_image
*texImage
)
1153 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1154 width
, height
, depth
, format
, type
,
1155 pixels
, packing
, texObj
, texImage
);
1160 st_TexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1161 GLint xoffset
, GLint yoffset
,
1162 GLsizei width
, GLsizei height
,
1163 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1164 const struct gl_pixelstore_attrib
*packing
,
1165 struct gl_texture_object
*texObj
,
1166 struct gl_texture_image
*texImage
)
1168 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1169 width
, height
, 1, format
, type
,
1170 pixels
, packing
, texObj
, texImage
);
1175 st_TexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1176 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1177 const GLvoid
* pixels
,
1178 const struct gl_pixelstore_attrib
*packing
,
1179 struct gl_texture_object
*texObj
,
1180 struct gl_texture_image
*texImage
)
1182 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1183 format
, type
, pixels
, packing
, texObj
, texImage
);
1188 st_CompressedTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1189 GLint xoffset
, GLsizei width
,
1191 GLsizei imageSize
, const GLvoid
*data
,
1192 struct gl_texture_object
*texObj
,
1193 struct gl_texture_image
*texImage
)
1200 st_CompressedTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1201 GLint xoffset
, GLint yoffset
,
1202 GLsizei width
, GLint height
,
1204 GLsizei imageSize
, const GLvoid
*data
,
1205 struct gl_texture_object
*texObj
,
1206 struct gl_texture_image
*texImage
)
1208 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1209 struct pipe_format_block block
;
1215 unsigned face
= _mesa_tex_target_to_face(target
);
1217 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1218 PIPE_TRANSFER_WRITE
);
1219 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
1220 PIPE_TRANSFER_WRITE
,
1224 block
= stImage
->pt
->block
;
1225 srcBlockStride
= pf_get_stride(&block
, width
);
1226 dstBlockStride
= stImage
->transfer
->stride
;
1228 assert(stImage
->pt
);
1229 /* TODO find good values for block and strides */
1230 /* TODO also adjust texImage->data for yoffset/xoffset */
1234 if (!texImage
->Data
) {
1235 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1239 assert(xoffset
% block
.width
== 0);
1240 assert(yoffset
% block
.height
== 0);
1241 assert(width
% block
.width
== 0);
1242 assert(height
% block
.height
== 0);
1244 for (y
= 0; y
< height
; y
+= block
.height
) {
1245 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1246 const char *src
= (const char*)data
+ srcBlockStride
* pf_get_nblocksy(&block
, y
);
1247 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* pf_get_nblocksy(&block
, y
);
1248 memcpy(dst
, src
, pf_get_stride(&block
, width
));
1252 st_texture_image_unmap(ctx
->st
, stImage
);
1253 texImage
->Data
= NULL
;
1259 st_CompressedTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1260 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1261 GLsizei width
, GLint height
, GLint depth
,
1263 GLsizei imageSize
, const GLvoid
*data
,
1264 struct gl_texture_object
*texObj
,
1265 struct gl_texture_image
*texImage
)
1273 * Do a CopyTexSubImage operation using a read transfer from the source,
1274 * a write transfer to the destination and get_tile()/put_tile() to access
1275 * the pixels/texels.
1277 * Note: srcY=0=TOP of renderbuffer
1280 fallback_copy_texsubimage(GLcontext
*ctx
, GLenum target
, GLint level
,
1281 struct st_renderbuffer
*strb
,
1282 struct st_texture_image
*stImage
,
1284 GLint destX
, GLint destY
, GLint destZ
,
1285 GLint srcX
, GLint srcY
,
1286 GLsizei width
, GLsizei height
)
1288 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1289 struct pipe_screen
*screen
= pipe
->screen
;
1290 struct pipe_transfer
*src_trans
;
1292 enum pipe_transfer_usage transfer_usage
;
1294 assert(width
<= MAX_WIDTH
);
1296 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1297 srcY
= strb
->Base
.Height
- srcY
- height
;
1300 src_trans
= st_cond_flush_get_tex_transfer( st_context(ctx
),
1307 if (baseFormat
== GL_DEPTH_COMPONENT
&&
1308 pf_is_depth_and_stencil(stImage
->pt
->format
))
1309 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1311 transfer_usage
= PIPE_TRANSFER_WRITE
;
1313 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, 0, 0,
1316 texDest
= st_texture_image_map(ctx
->st
, stImage
, 0, transfer_usage
,
1317 destX
, destY
, width
, height
);
1319 if (baseFormat
== GL_DEPTH_COMPONENT
||
1320 baseFormat
== GL_DEPTH24_STENCIL8
) {
1321 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1322 ctx
->Pixel
.DepthBias
!= 0.0F
);
1325 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1326 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1335 /* To avoid a large temp memory allocation, do copy row by row */
1336 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1337 uint data
[MAX_WIDTH
];
1338 pipe_get_tile_z(src_trans
, 0, srcY
, width
, 1, data
);
1340 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1342 pipe_put_tile_z(stImage
->transfer
, 0, row
, width
, 1, data
);
1348 (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
1350 if (tempSrc
&& texDest
) {
1351 const GLint dims
= 2;
1352 const GLint dstRowStride
= stImage
->transfer
->stride
;
1353 struct gl_texture_image
*texImage
= &stImage
->base
;
1354 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1356 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1357 unpack
.Invert
= GL_TRUE
;
1360 /* get float/RGBA image from framebuffer */
1361 /* XXX this usually involves a lot of int/float conversion.
1362 * try to avoid that someday.
1364 pipe_get_tile_rgba(src_trans
, 0, 0, width
, height
, tempSrc
);
1366 /* Store into texture memory.
1367 * Note that this does some special things such as pixel transfer
1368 * ops and format conversion. In particular, if the dest tex format
1369 * is actually RGBA but the user created the texture as GL_RGB we
1370 * need to fill-in/override the alpha channel with 1.0.
1372 _mesa_texstore(ctx
, dims
,
1373 texImage
->_BaseFormat
,
1374 texImage
->TexFormat
,
1378 texImage
->ImageOffsets
,
1380 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1384 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1388 _mesa_free(tempSrc
);
1391 st_texture_image_unmap(ctx
->st
, stImage
);
1392 screen
->tex_transfer_destroy(src_trans
);
1397 compatible_src_dst_formats(const struct gl_renderbuffer
*src
,
1398 const struct gl_texture_image
*dst
)
1400 const GLenum srcFormat
= src
->_BaseFormat
;
1401 const GLenum dstLogicalFormat
= dst
->_BaseFormat
;
1403 if (srcFormat
== dstLogicalFormat
) {
1404 /* This is the same as matching_base_formats, which should
1405 * always pass, as it did previously.
1407 return TGSI_WRITEMASK_XYZW
;
1409 else if (srcFormat
== GL_RGBA
&&
1410 dstLogicalFormat
== GL_RGB
) {
1411 /* Add a single special case to cope with RGBA->RGB transfers,
1412 * setting A to 1.0 to cope with situations where the RGB
1413 * destination is actually stored as RGBA.
1415 return TGSI_WRITEMASK_XYZ
; /* A ==> 1.0 */
1427 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1428 * Note that the region to copy has already been clipped so we know we
1429 * won't read from outside the source renderbuffer's bounds.
1431 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1434 st_copy_texsubimage(GLcontext
*ctx
,
1435 GLenum target
, GLint level
,
1436 GLint destX
, GLint destY
, GLint destZ
,
1437 GLint srcX
, GLint srcY
,
1438 GLsizei width
, GLsizei height
)
1440 struct gl_texture_unit
*texUnit
=
1441 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1442 struct gl_texture_object
*texObj
=
1443 _mesa_select_tex_object(ctx
, texUnit
, target
);
1444 struct gl_texture_image
*texImage
=
1445 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1446 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1447 const GLenum texBaseFormat
= texImage
->InternalFormat
;
1448 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1449 struct st_renderbuffer
*strb
;
1450 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1451 struct pipe_screen
*screen
= pipe
->screen
;
1452 enum pipe_format dest_format
, src_format
;
1453 GLboolean use_fallback
= GL_TRUE
;
1454 GLboolean matching_base_formats
;
1455 GLuint format_writemask
;
1456 struct pipe_surface
*dest_surface
= NULL
;
1457 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1459 /* any rendering in progress must flushed before we grab the fb image */
1460 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
1462 /* make sure finalize_textures has been called?
1464 if (0) st_validate_state(ctx
->st
);
1466 /* determine if copying depth or color data */
1467 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1468 texBaseFormat
== GL_DEPTH24_STENCIL8
) {
1469 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1471 else if (texBaseFormat
== GL_DEPTH_STENCIL_EXT
) {
1472 strb
= st_renderbuffer(fb
->_StencilBuffer
);
1475 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1476 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1479 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1480 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1508 if (width
< 0 || height
< 0)
1513 assert(strb
->surface
);
1514 assert(stImage
->pt
);
1516 src_format
= strb
->surface
->format
;
1517 dest_format
= stImage
->pt
->format
;
1520 * Determine if the src framebuffer and dest texture have the same
1521 * base format. We need this to detect a case such as the framebuffer
1522 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1523 * texture format stores RGBA we need to set A=1 (overriding the
1524 * framebuffer's alpha values). We can't do that with the blit or
1525 * textured-quad paths.
1527 matching_base_formats
= (strb
->Base
._BaseFormat
== texImage
->_BaseFormat
);
1528 format_writemask
= compatible_src_dst_formats(&strb
->Base
, texImage
);
1530 if (ctx
->_ImageTransferState
== 0x0) {
1532 if (matching_base_formats
&&
1533 src_format
== dest_format
&&
1536 /* use surface_copy() / blit */
1538 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1539 stImage
->face
, stImage
->level
,
1541 PIPE_BUFFER_USAGE_GPU_WRITE
);
1543 /* for surface_copy(), y=0=top, always */
1544 pipe
->surface_copy(pipe
,
1553 use_fallback
= GL_FALSE
;
1555 else if (format_writemask
&&
1556 screen
->is_format_supported(screen
, src_format
,
1558 PIPE_TEXTURE_USAGE_SAMPLER
,
1560 screen
->is_format_supported(screen
, dest_format
,
1562 PIPE_TEXTURE_USAGE_RENDER_TARGET
,
1564 /* draw textured quad to do the copy */
1567 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1568 stImage
->face
, stImage
->level
,
1570 PIPE_BUFFER_USAGE_GPU_WRITE
);
1573 srcY1
= strb
->Base
.Height
- srcY
- height
;
1574 srcY0
= srcY1
+ height
;
1578 srcY1
= srcY0
+ height
;
1580 util_blit_pixels_writemask(ctx
->st
->blit
,
1583 srcX
+ width
, srcY1
,
1586 destX
+ width
, destY
+ height
,
1587 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1589 use_fallback
= GL_FALSE
;
1593 pipe_surface_reference(&dest_surface
, NULL
);
1597 /* software fallback */
1598 fallback_copy_texsubimage(ctx
, target
, level
,
1599 strb
, stImage
, texBaseFormat
,
1600 destX
, destY
, destZ
,
1601 srcX
, srcY
, width
, height
);
1608 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1609 GLenum internalFormat
,
1610 GLint x
, GLint y
, GLsizei width
, GLint border
)
1612 struct gl_texture_unit
*texUnit
=
1613 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1614 struct gl_texture_object
*texObj
=
1615 _mesa_select_tex_object(ctx
, texUnit
, target
);
1616 struct gl_texture_image
*texImage
=
1617 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1619 /* Setup or redefine the texture object, texture and texture
1620 * image. Don't populate yet.
1622 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1624 GL_RGBA
, CHAN_TYPE
, NULL
,
1625 &ctx
->DefaultPacking
, texObj
, texImage
);
1627 st_copy_texsubimage(ctx
, target
, level
,
1628 0, 0, 0, /* destX,Y,Z */
1629 x
, y
, width
, 1); /* src X, Y, size */
1634 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1635 GLenum internalFormat
,
1636 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1639 struct gl_texture_unit
*texUnit
=
1640 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1641 struct gl_texture_object
*texObj
=
1642 _mesa_select_tex_object(ctx
, texUnit
, target
);
1643 struct gl_texture_image
*texImage
=
1644 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1646 /* Setup or redefine the texture object, texture and texture
1647 * image. Don't populate yet.
1649 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1650 width
, height
, border
,
1651 GL_RGBA
, CHAN_TYPE
, NULL
,
1652 &ctx
->DefaultPacking
, texObj
, texImage
);
1654 st_copy_texsubimage(ctx
, target
, level
,
1655 0, 0, 0, /* destX,Y,Z */
1656 x
, y
, width
, height
); /* src X, Y, size */
1661 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1662 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1664 const GLint yoffset
= 0, zoffset
= 0;
1665 const GLsizei height
= 1;
1666 st_copy_texsubimage(ctx
, target
, level
,
1667 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1668 x
, y
, width
, height
); /* src X, Y, size */
1673 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1674 GLint xoffset
, GLint yoffset
,
1675 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1677 const GLint zoffset
= 0;
1678 st_copy_texsubimage(ctx
, target
, level
,
1679 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1680 x
, y
, width
, height
); /* src X, Y, size */
1685 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1686 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1687 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1689 st_copy_texsubimage(ctx
, target
, level
,
1690 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1691 x
, y
, width
, height
); /* src X, Y, size */
1696 * Compute which mipmap levels that really need to be sent to the hardware.
1697 * This depends on the base image size, GL_TEXTURE_MIN_LOD,
1698 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
1701 calculate_first_last_level(struct st_texture_object
*stObj
)
1703 struct gl_texture_object
*tObj
= &stObj
->base
;
1705 /* These must be signed values. MinLod and MaxLod can be negative numbers,
1706 * and having firstLevel and lastLevel as signed prevents the need for
1707 * extra sign checks.
1712 /* Yes, this looks overly complicated, but it's all needed.
1714 switch (tObj
->Target
) {
1718 case GL_TEXTURE_CUBE_MAP
:
1719 if (tObj
->MinFilter
== GL_NEAREST
|| tObj
->MinFilter
== GL_LINEAR
) {
1720 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
1722 firstLevel
= lastLevel
= tObj
->BaseLevel
;
1726 lastLevel
= MIN2(tObj
->MaxLevel
,
1727 (int) tObj
->Image
[0][tObj
->BaseLevel
]->WidthLog2
);
1730 case GL_TEXTURE_RECTANGLE_NV
:
1731 case GL_TEXTURE_4D_SGIS
:
1732 firstLevel
= lastLevel
= 0;
1738 stObj
->lastLevel
= lastLevel
;
1743 copy_image_data_to_texture(struct st_context
*st
,
1744 struct st_texture_object
*stObj
,
1746 struct st_texture_image
*stImage
)
1749 /* Copy potentially with the blitter:
1751 st_texture_image_copy(st
->pipe
,
1752 stObj
->pt
, dstLevel
, /* dest texture, level */
1753 stImage
->pt
, /* src texture */
1757 pipe_texture_reference(&stImage
->pt
, NULL
);
1759 else if (stImage
->base
.Data
) {
1760 assert(stImage
->base
.Data
!= NULL
);
1762 /* More straightforward upload.
1765 st_teximage_flush_before_map(st
, stObj
->pt
, stImage
->face
, dstLevel
,
1766 PIPE_TRANSFER_WRITE
);
1769 st_texture_image_data(st
,
1774 stImage
->base
.RowStride
*
1775 stObj
->pt
->block
.size
,
1776 stImage
->base
.RowStride
*
1777 stImage
->base
.Height
*
1778 stObj
->pt
->block
.size
);
1779 _mesa_align_free(stImage
->base
.Data
);
1780 stImage
->base
.Data
= NULL
;
1783 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
1788 * Called during state validation. When this function is finished,
1789 * the texture object should be ready for rendering.
1790 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1793 st_finalize_texture(GLcontext
*ctx
,
1794 struct pipe_context
*pipe
,
1795 struct gl_texture_object
*tObj
,
1796 GLboolean
*needFlush
)
1798 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1799 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1801 struct st_texture_image
*firstImage
;
1803 *needFlush
= GL_FALSE
;
1805 /* We know/require this is true by now:
1807 assert(stObj
->base
._Complete
);
1809 /* What levels must the texture include at a minimum?
1811 calculate_first_last_level(stObj
);
1812 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1814 /* If both firstImage and stObj point to a texture which can contain
1815 * all active images, favour firstImage. Note that because of the
1816 * completeness requirement, we know that the image dimensions
1819 if (firstImage
->pt
&&
1820 firstImage
->pt
!= stObj
->pt
&&
1821 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1822 pipe_texture_reference(&stObj
->pt
, firstImage
->pt
);
1825 /* FIXME: determine format block instead of cpp */
1826 if (firstImage
->base
.IsCompressed
) {
1827 cpp
= compressed_num_bytes(firstImage
->base
.TexFormat
->MesaFormat
);
1830 cpp
= _mesa_get_format_bytes(firstImage
->base
.TexFormat
->MesaFormat
);
1833 /* If we already have a gallium texture, check that it matches the texture
1834 * object's format, target, size, num_levels, etc.
1837 const enum pipe_format fmt
=
1838 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
->MesaFormat
);
1839 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1840 stObj
->pt
->format
!= fmt
||
1841 stObj
->pt
->last_level
< stObj
->lastLevel
||
1842 stObj
->pt
->width
[0] != firstImage
->base
.Width2
||
1843 stObj
->pt
->height
[0] != firstImage
->base
.Height2
||
1844 stObj
->pt
->depth
[0] != firstImage
->base
.Depth2
||
1845 /* Nominal bytes per pixel: */
1846 stObj
->pt
->block
.size
/ stObj
->pt
->block
.width
!= cpp
)
1848 pipe_texture_reference(&stObj
->pt
, NULL
);
1849 ctx
->st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1853 /* May need to create a new gallium texture:
1856 const enum pipe_format fmt
=
1857 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
->MesaFormat
);
1858 GLuint usage
= default_usage(fmt
);
1860 stObj
->pt
= st_texture_create(ctx
->st
,
1861 gl_target_to_pipe(stObj
->base
.Target
),
1864 firstImage
->base
.Width2
,
1865 firstImage
->base
.Height2
,
1866 firstImage
->base
.Depth2
,
1870 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1875 /* Pull in any images not in the object's texture:
1877 for (face
= 0; face
< nr_faces
; face
++) {
1879 for (level
= 0; level
<= stObj
->lastLevel
; level
++) {
1880 struct st_texture_image
*stImage
=
1881 st_texture_image(stObj
->base
.Image
[face
][stObj
->base
.BaseLevel
+ level
]);
1883 /* Need to import images in main memory or held in other textures.
1885 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1886 copy_image_data_to_texture(ctx
->st
, stObj
, level
, stImage
);
1887 *needFlush
= GL_TRUE
;
1897 * Returns pointer to a default/dummy texture.
1898 * This is typically used when the current shader has tex/sample instructions
1899 * but the user has not provided a (any) texture(s).
1901 struct gl_texture_object
*
1902 st_get_default_texture(struct st_context
*st
)
1904 if (!st
->default_texture
) {
1905 static const GLenum target
= GL_TEXTURE_2D
;
1906 GLubyte pixels
[16][16][4];
1907 struct gl_texture_object
*texObj
;
1908 struct gl_texture_image
*texImg
;
1911 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1912 * when attempting to sample incomplete textures.
1914 for (i
= 0; i
< 16; i
++) {
1915 for (j
= 0; j
< 16; j
++) {
1916 pixels
[i
][j
][0] = 0;
1917 pixels
[i
][j
][1] = 0;
1918 pixels
[i
][j
][2] = 0;
1919 pixels
[i
][j
][3] = 255;
1923 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1925 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1927 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1928 16, 16, 1, 0, /* w, h, d, border */
1931 st_TexImage(st
->ctx
, 2, target
,
1932 0, GL_RGBA
, /* level, intformat */
1933 16, 16, 1, 0, /* w, h, d, border */
1934 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1935 &st
->ctx
->DefaultPacking
,
1939 texObj
->MinFilter
= GL_NEAREST
;
1940 texObj
->MagFilter
= GL_NEAREST
;
1941 texObj
->_Complete
= GL_TRUE
;
1943 st
->default_texture
= texObj
;
1945 return st
->default_texture
;
1950 st_init_texture_functions(struct dd_function_table
*functions
)
1952 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1953 functions
->TexImage1D
= st_TexImage1D
;
1954 functions
->TexImage2D
= st_TexImage2D
;
1955 functions
->TexImage3D
= st_TexImage3D
;
1956 functions
->TexSubImage1D
= st_TexSubImage1D
;
1957 functions
->TexSubImage2D
= st_TexSubImage2D
;
1958 functions
->TexSubImage3D
= st_TexSubImage3D
;
1959 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1960 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1961 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1962 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1963 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1964 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1965 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1966 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1967 functions
->GenerateMipmap
= st_generate_mipmap
;
1969 functions
->GetTexImage
= st_GetTexImage
;
1971 /* compressed texture functions */
1972 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1973 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1974 functions
->CompressedTextureSize
= _mesa_compressed_texture_size
;
1976 functions
->NewTextureObject
= st_NewTextureObject
;
1977 functions
->NewTextureImage
= st_NewTextureImage
;
1978 functions
->DeleteTexture
= st_DeleteTextureObject
;
1979 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1980 functions
->UpdateTexturePalette
= 0;
1982 functions
->TextureMemCpy
= do_memcpy
;
1984 /* XXX Temporary until we can query pipe's texture sizes */
1985 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;