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 !stObj
->base
.GenerateMipmap
&&
332 stImage
->level
== firstLevel
) {
333 /* only alloc space for a single mipmap level */
334 lastLevel
= firstLevel
;
337 /* alloc space for a full mipmap */
338 GLuint l2width
= util_logbase2(width
);
339 GLuint l2height
= util_logbase2(height
);
340 GLuint l2depth
= util_logbase2(depth
);
341 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
344 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
->MesaFormat
);
346 usage
= default_usage(fmt
);
348 stObj
->pt
= st_texture_create(st
,
349 gl_target_to_pipe(stObj
->base
.Target
),
357 DBG("%s - success\n", __FUNCTION__
);
362 * Adjust pixel unpack params and image dimensions to strip off the
364 * Gallium doesn't support texture borders. They've seldem been used
365 * and seldom been implemented correctly anyway.
366 * \param unpackNew returns the new pixel unpack parameters
369 strip_texture_border(GLint border
,
370 GLint
*width
, GLint
*height
, GLint
*depth
,
371 const struct gl_pixelstore_attrib
*unpack
,
372 struct gl_pixelstore_attrib
*unpackNew
)
374 assert(border
> 0); /* sanity check */
376 *unpackNew
= *unpack
;
378 if (unpackNew
->RowLength
== 0)
379 unpackNew
->RowLength
= *width
;
381 if (depth
&& unpackNew
->ImageHeight
== 0)
382 unpackNew
->ImageHeight
= *height
;
384 unpackNew
->SkipPixels
+= border
;
386 unpackNew
->SkipRows
+= border
;
388 unpackNew
->SkipImages
+= border
;
391 *width
= *width
- 2 * border
;
392 if (height
&& *height
>= 3)
393 *height
= *height
- 2 * border
;
394 if (depth
&& *depth
>= 3)
395 *depth
= *depth
- 2 * border
;
400 * Try to do texture compression via rendering. If the Gallium driver
401 * can render into a compressed surface this will allow us to do texture
403 * \return GL_TRUE for success, GL_FALSE for failure
406 compress_with_blit(GLcontext
* ctx
,
407 GLenum target
, GLint level
,
408 GLint xoffset
, GLint yoffset
, GLint zoffset
,
409 GLint width
, GLint height
, GLint depth
,
410 GLenum format
, GLenum type
, const void *pixels
,
411 const struct gl_pixelstore_attrib
*unpack
,
412 struct gl_texture_image
*texImage
)
414 const GLuint dstImageOffsets
[1] = {0};
415 struct st_texture_image
*stImage
= st_texture_image(texImage
);
416 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
417 const struct gl_texture_format
*mesa_format
;
418 struct pipe_texture templ
;
419 struct pipe_texture
*src_tex
;
420 struct pipe_surface
*dst_surface
;
421 struct pipe_transfer
*tex_xfer
;
426 /* XXX: Can this happen? Should we assert? */
430 /* get destination surface (in the compressed texture) */
431 dst_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
432 stImage
->face
, stImage
->level
, 0,
433 PIPE_BUFFER_USAGE_GPU_WRITE
);
435 /* can't render into this format (or other problem) */
439 /* Choose format for the temporary RGBA texture image.
441 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
446 /* Create the temporary source texture
448 memset(&templ
, 0, sizeof(templ
));
449 templ
.target
= PIPE_TEXTURE_2D
;
450 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
->MesaFormat
);
451 pf_get_block(templ
.format
, &templ
.block
);
452 templ
.width
[0] = width
;
453 templ
.height
[0] = height
;
455 templ
.last_level
= 0;
456 templ
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
457 src_tex
= screen
->texture_create(screen
, &templ
);
462 /* Put user's tex data into the temporary texture
464 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
), src_tex
,
465 0, 0, 0, /* face, level are zero */
467 0, 0, width
, height
); /* x, y, w, h */
468 map
= screen
->transfer_map(screen
, tex_xfer
);
470 mesa_format
->StoreImage(ctx
, 2, GL_RGBA
, mesa_format
,
472 0, 0, 0, /* dest x/y/z offset */
473 tex_xfer
->stride
, /* dest row stride (bytes) */
474 dstImageOffsets
, /* image offsets (for 3D only) */
475 width
, height
, 1, /* size */
476 format
, type
, /* source format/type */
477 pixels
, /* source data */
478 unpack
); /* source data packing */
480 screen
->transfer_unmap(screen
, tex_xfer
);
481 screen
->tex_transfer_destroy(tex_xfer
);
483 /* copy / compress image */
484 util_blit_pixels_tex(ctx
->st
->blit
,
485 src_tex
, /* pipe_texture (src) */
486 0, 0, /* src x0, y0 */
487 width
, height
, /* src x1, y1 */
488 dst_surface
, /* pipe_surface (dst) */
489 xoffset
, yoffset
, /* dst x0, y0 */
490 xoffset
+ width
, /* dst x1 */
491 yoffset
+ height
, /* dst y1 */
493 PIPE_TEX_MIPFILTER_NEAREST
);
495 pipe_surface_reference(&dst_surface
, NULL
);
496 pipe_texture_reference(&src_tex
, NULL
);
503 * Do glTexImage1/2/3D().
506 st_TexImage(GLcontext
* ctx
,
508 GLenum target
, GLint level
,
509 GLint internalFormat
,
510 GLint width
, GLint height
, GLint depth
,
512 GLenum format
, GLenum type
, const void *pixels
,
513 const struct gl_pixelstore_attrib
*unpack
,
514 struct gl_texture_object
*texObj
,
515 struct gl_texture_image
*texImage
,
516 GLsizei imageSize
, GLboolean compressed_src
)
518 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
519 struct st_texture_object
*stObj
= st_texture_object(texObj
);
520 struct st_texture_image
*stImage
= st_texture_image(texImage
);
521 GLint postConvWidth
, postConvHeight
;
522 GLint texelBytes
, sizeInBytes
;
523 GLuint dstRowStride
= 0;
524 struct gl_pixelstore_attrib unpackNB
;
525 enum pipe_transfer_usage transfer_usage
= 0;
527 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
528 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
530 /* switch to "normal" */
531 if (stObj
->surface_based
) {
532 _mesa_clear_texture_object(ctx
, texObj
);
533 stObj
->surface_based
= GL_FALSE
;
536 /* gallium does not support texture borders, strip it off */
538 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
540 texImage
->Width
= width
;
541 texImage
->Height
= height
;
542 texImage
->Depth
= depth
;
543 texImage
->Border
= 0;
547 postConvWidth
= width
;
548 postConvHeight
= height
;
550 stImage
->face
= _mesa_tex_target_to_face(target
);
551 stImage
->level
= level
;
554 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
555 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
560 /* choose the texture format */
561 texImage
->TexFormat
= st_ChooseTextureFormat(ctx
, internalFormat
,
564 _mesa_set_fetch_functions(texImage
, dims
);
566 if (texImage
->TexFormat
->TexelBytes
== 0) {
567 /* must be a compressed format */
569 texImage
->IsCompressed
= GL_TRUE
;
570 texImage
->CompressedSize
=
571 ctx
->Driver
.CompressedTextureSize(ctx
, texImage
->Width
,
572 texImage
->Height
, texImage
->Depth
,
573 texImage
->TexFormat
->MesaFormat
);
576 texelBytes
= texImage
->TexFormat
->TexelBytes
;
578 /* Minimum pitch of 32 bytes */
579 if (postConvWidth
* texelBytes
< 32) {
580 postConvWidth
= 32 / texelBytes
;
581 texImage
->RowStride
= postConvWidth
;
584 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
585 /*assert(texImage->RowStride == postConvWidth);*/
588 /* Release the reference to a potentially orphaned buffer.
589 * Release any old malloced memory.
592 pipe_texture_reference(&stImage
->pt
, NULL
);
593 assert(!texImage
->Data
);
595 else if (texImage
->Data
) {
596 _mesa_align_free(texImage
->Data
);
599 if (width
== 0 || height
== 0 || depth
== 0) {
600 /* stop after freeing old image */
604 /* If this is the only mipmap level in the texture, could call
605 * bmBufferData with NULL data to free the old block and avoid
606 * waiting on any outstanding fences.
609 if (stObj
->teximage_realloc
||
610 level
> (GLint
) stObj
->pt
->last_level
||
611 (stObj
->pt
->last_level
== level
&&
612 stObj
->pt
->target
!= PIPE_TEXTURE_CUBE
&&
613 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
614 stImage
->face
, stImage
->level
))) {
616 pipe_texture_reference(&stObj
->pt
, NULL
);
618 stObj
->teximage_realloc
= FALSE
;
623 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
625 /* Probably out of memory.
626 * Try flushing any pending rendering, then retry.
629 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
631 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
637 assert(!stImage
->pt
);
640 st_texture_match_image(stObj
->pt
, &stImage
->base
,
641 stImage
->face
, stImage
->level
)) {
643 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
648 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
650 /* st_CopyTexImage calls this function with pixels == NULL, with
651 * the expectation that the texture will be set up but nothing
652 * more will be done. This is where those calls return:
654 if (compressed_src
) {
655 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
657 "glCompressedTexImage");
660 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
662 pixels
, unpack
, "glTexImage");
665 /* Note: we can't check for pixels==NULL until after we've allocated
666 * memory for the texture.
669 /* See if we can do texture compression with a blit/render.
671 if (!compressed_src
&&
673 is_compressed_mesa_format(texImage
->TexFormat
) &&
674 screen
->is_format_supported(screen
,
677 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
681 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
682 format
, type
, pixels
, unpack
, texImage
)) {
688 if (format
== GL_DEPTH_COMPONENT
&&
689 pf_is_depth_and_stencil(stImage
->pt
->format
))
690 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
692 transfer_usage
= PIPE_TRANSFER_WRITE
;
694 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
695 transfer_usage
, 0, 0,
697 stImage
->base
.Height
);
698 if(stImage
->transfer
)
699 dstRowStride
= stImage
->transfer
->stride
;
702 /* Allocate regular memory and store the image there temporarily. */
703 if (texImage
->IsCompressed
) {
704 sizeInBytes
= texImage
->CompressedSize
;
706 _mesa_compressed_row_stride(texImage
->TexFormat
->MesaFormat
, width
);
710 dstRowStride
= postConvWidth
* texelBytes
;
711 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
714 texImage
->Data
= _mesa_align_malloc(sizeInBytes
, 16);
717 if (!texImage
->Data
) {
718 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
725 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
726 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
728 /* Copy data. Would like to know when it's ok for us to eg. use
729 * the blitter to copy. Or, use the hardware to do the format
730 * conversion and copy:
732 if (compressed_src
) {
733 memcpy(texImage
->Data
, pixels
, imageSize
);
736 const GLuint srcImageStride
=
737 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
739 const GLubyte
*src
= (const GLubyte
*) pixels
;
741 for (i
= 0; i
< depth
; i
++) {
742 if (!texImage
->TexFormat
->StoreImage(ctx
, dims
,
743 texImage
->_BaseFormat
,
746 0, 0, 0, /* dstX/Y/Zoffset */
748 texImage
->ImageOffsets
,
750 format
, type
, src
, unpack
)) {
751 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
754 if (stImage
->pt
&& i
+ 1 < depth
) {
755 /* unmap this slice */
756 st_texture_image_unmap(ctx
->st
, stImage
);
757 /* map next slice of 3D texture */
758 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
759 transfer_usage
, 0, 0,
761 stImage
->base
.Height
);
762 src
+= srcImageStride
;
768 _mesa_unmap_teximage_pbo(ctx
, unpack
);
770 if (stImage
->pt
&& texImage
->Data
) {
771 st_texture_image_unmap(ctx
->st
, stImage
);
772 texImage
->Data
= NULL
;
778 st_TexImage3D(GLcontext
* ctx
,
779 GLenum target
, GLint level
,
780 GLint internalFormat
,
781 GLint width
, GLint height
, GLint depth
,
783 GLenum format
, GLenum type
, const void *pixels
,
784 const struct gl_pixelstore_attrib
*unpack
,
785 struct gl_texture_object
*texObj
,
786 struct gl_texture_image
*texImage
)
788 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
789 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
795 st_TexImage2D(GLcontext
* ctx
,
796 GLenum target
, GLint level
,
797 GLint internalFormat
,
798 GLint width
, GLint height
, GLint border
,
799 GLenum format
, GLenum type
, const void *pixels
,
800 const struct gl_pixelstore_attrib
*unpack
,
801 struct gl_texture_object
*texObj
,
802 struct gl_texture_image
*texImage
)
804 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
805 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
810 st_TexImage1D(GLcontext
* ctx
,
811 GLenum target
, GLint level
,
812 GLint internalFormat
,
813 GLint width
, GLint border
,
814 GLenum format
, GLenum type
, const void *pixels
,
815 const struct gl_pixelstore_attrib
*unpack
,
816 struct gl_texture_object
*texObj
,
817 struct gl_texture_image
*texImage
)
819 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
820 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
825 st_CompressedTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
826 GLint internalFormat
,
827 GLint width
, GLint height
, GLint border
,
828 GLsizei imageSize
, const GLvoid
*data
,
829 struct gl_texture_object
*texObj
,
830 struct gl_texture_image
*texImage
)
832 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
833 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
839 * glGetTexImage() helper: decompress a compressed texture by rendering
840 * a textured quad. Store the results in the user's buffer.
843 decompress_with_blit(GLcontext
* ctx
, GLenum target
, GLint level
,
844 GLenum format
, GLenum type
, GLvoid
*pixels
,
845 struct gl_texture_object
*texObj
,
846 struct gl_texture_image
*texImage
)
848 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
849 struct st_texture_image
*stImage
= st_texture_image(texImage
);
850 const GLuint width
= texImage
->Width
;
851 const GLuint height
= texImage
->Height
;
852 struct pipe_surface
*dst_surface
;
853 struct pipe_texture
*dst_texture
;
854 struct pipe_transfer
*tex_xfer
;
856 /* create temp / dest surface */
857 if (!util_create_rgba_surface(screen
, width
, height
,
858 &dst_texture
, &dst_surface
)) {
859 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
860 "in decompress_with_blit()");
864 /* blit/render/decompress */
865 util_blit_pixels_tex(ctx
->st
->blit
,
866 stImage
->pt
, /* pipe_texture (src) */
867 0, 0, /* src x0, y0 */
868 width
, height
, /* src x1, y1 */
869 dst_surface
, /* pipe_surface (dst) */
870 0, 0, /* dst x0, y0 */
871 width
, height
, /* dst x1, y1 */
873 PIPE_TEX_MIPFILTER_NEAREST
);
875 /* map the dst_surface so we can read from it */
876 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
),
877 dst_texture
, 0, 0, 0,
879 0, 0, width
, height
);
881 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
883 /* copy/pack data into user buffer */
884 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
886 const uint bytesPerRow
= width
* pf_get_size(stImage
->pt
->format
);
887 ubyte
*map
= screen
->transfer_map(screen
, tex_xfer
);
889 for (row
= 0; row
< height
; row
++) {
890 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
891 height
, format
, type
, row
, 0);
892 memcpy(dest
, map
, bytesPerRow
);
893 map
+= tex_xfer
->stride
;
895 screen
->transfer_unmap(screen
, tex_xfer
);
898 /* format translation via floats */
900 for (row
= 0; row
< height
; row
++) {
901 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
902 GLfloat rgba
[4 * MAX_WIDTH
];
903 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
904 height
, format
, type
, row
, 0);
906 /* get float[4] rgba row from surface */
907 pipe_get_tile_rgba(tex_xfer
, 0, row
, width
, 1, rgba
);
909 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
910 type
, dest
, &ctx
->Pack
, transferOps
);
914 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
916 /* destroy the temp / dest surface */
917 util_destroy_rgba_surface(dst_texture
, dst_surface
);
923 * Need to map texture image into memory before copying image data,
927 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
928 GLenum format
, GLenum type
, GLvoid
* pixels
,
929 struct gl_texture_object
*texObj
,
930 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
932 struct st_texture_image
*stImage
= st_texture_image(texImage
);
933 const GLuint dstImageStride
=
934 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
940 pf_is_compressed(stImage
->pt
->format
) &&
942 /* Need to decompress the texture.
943 * We'll do this by rendering a textured quad.
944 * Note that we only expect RGBA formats (no Z/depth formats).
946 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
953 /* Image is stored in hardware format in a buffer managed by the
954 * kernel. Need to explicitly map and unmap it.
956 unsigned face
= _mesa_tex_target_to_face(target
);
958 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
961 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
962 PIPE_TRANSFER_READ
, 0, 0,
964 stImage
->base
.Height
);
965 texImage
->RowStride
= stImage
->transfer
->stride
/ stImage
->pt
->block
.size
;
968 /* Otherwise, the image should actually be stored in
969 * texImage->Data. This is pretty confusing for
970 * everybody, I'd much prefer to separate the two functions of
971 * texImage->Data - storage for texture images in main memory
972 * and access (ie mappings) of images. In other words, we'd
973 * create a new texImage->Map field and leave Data simply for
976 assert(texImage
->Data
);
979 depth
= texImage
->Depth
;
982 dest
= (GLubyte
*) pixels
;
984 for (i
= 0; i
< depth
; i
++) {
985 if (compressed_dst
) {
986 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
990 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
994 if (stImage
->pt
&& i
+ 1 < depth
) {
995 /* unmap this slice */
996 st_texture_image_unmap(ctx
->st
, stImage
);
997 /* map next slice of 3D texture */
998 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
999 PIPE_TRANSFER_READ
, 0, 0,
1000 stImage
->base
.Width
,
1001 stImage
->base
.Height
);
1002 dest
+= dstImageStride
;
1006 texImage
->Depth
= depth
;
1010 st_texture_image_unmap(ctx
->st
, stImage
);
1011 texImage
->Data
= NULL
;
1017 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
1018 GLenum format
, GLenum type
, GLvoid
* pixels
,
1019 struct gl_texture_object
*texObj
,
1020 struct gl_texture_image
*texImage
)
1022 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1028 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
1030 struct gl_texture_object
*texObj
,
1031 struct gl_texture_image
*texImage
)
1033 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1040 st_TexSubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
1041 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1042 GLint width
, GLint height
, GLint depth
,
1043 GLenum format
, GLenum type
, const void *pixels
,
1044 const struct gl_pixelstore_attrib
*packing
,
1045 struct gl_texture_object
*texObj
,
1046 struct gl_texture_image
*texImage
)
1048 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
1049 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1050 GLuint dstRowStride
;
1051 const GLuint srcImageStride
=
1052 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1055 /* init to silence warning only: */
1056 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1058 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1059 _mesa_lookup_enum_by_nr(target
),
1060 level
, xoffset
, yoffset
, width
, height
);
1063 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1064 type
, pixels
, packing
, "glTexSubImage2D");
1068 /* See if we can do texture compression with a blit/render.
1070 if (!ctx
->Mesa_DXTn
&&
1071 is_compressed_mesa_format(texImage
->TexFormat
) &&
1072 screen
->is_format_supported(screen
,
1073 stImage
->pt
->format
,
1074 stImage
->pt
->target
,
1075 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
1076 if (compress_with_blit(ctx
, target
, level
,
1077 xoffset
, yoffset
, zoffset
,
1078 width
, height
, depth
,
1079 format
, type
, pixels
, packing
, texImage
)) {
1084 /* Map buffer if necessary. Need to lock to prevent other contexts
1085 * from uploading the buffer under us.
1088 unsigned face
= _mesa_tex_target_to_face(target
);
1090 if (format
== GL_DEPTH_COMPONENT
&&
1091 pf_is_depth_and_stencil(stImage
->pt
->format
))
1092 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1094 transfer_usage
= PIPE_TRANSFER_WRITE
;
1096 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1098 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
,
1104 if (!texImage
->Data
) {
1105 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1109 src
= (const GLubyte
*) pixels
;
1110 dstRowStride
= stImage
->transfer
->stride
;
1112 for (i
= 0; i
< depth
; i
++) {
1113 if (!texImage
->TexFormat
->StoreImage(ctx
, dims
, texImage
->_BaseFormat
,
1114 texImage
->TexFormat
,
1118 texImage
->ImageOffsets
,
1120 format
, type
, src
, packing
)) {
1121 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1124 if (stImage
->pt
&& i
+ 1 < depth
) {
1125 /* unmap this slice */
1126 st_texture_image_unmap(ctx
->st
, stImage
);
1127 /* map next slice of 3D texture */
1128 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
,
1133 src
+= srcImageStride
;
1138 _mesa_unmap_teximage_pbo(ctx
, packing
);
1141 st_texture_image_unmap(ctx
->st
, stImage
);
1142 texImage
->Data
= NULL
;
1149 st_TexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1150 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1151 GLsizei width
, GLsizei height
, GLsizei depth
,
1152 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1153 const struct gl_pixelstore_attrib
*packing
,
1154 struct gl_texture_object
*texObj
,
1155 struct gl_texture_image
*texImage
)
1157 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1158 width
, height
, depth
, format
, type
,
1159 pixels
, packing
, texObj
, texImage
);
1164 st_TexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1165 GLint xoffset
, GLint yoffset
,
1166 GLsizei width
, GLsizei height
,
1167 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1168 const struct gl_pixelstore_attrib
*packing
,
1169 struct gl_texture_object
*texObj
,
1170 struct gl_texture_image
*texImage
)
1172 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1173 width
, height
, 1, format
, type
,
1174 pixels
, packing
, texObj
, texImage
);
1179 st_TexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1180 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1181 const GLvoid
* pixels
,
1182 const struct gl_pixelstore_attrib
*packing
,
1183 struct gl_texture_object
*texObj
,
1184 struct gl_texture_image
*texImage
)
1186 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1187 format
, type
, pixels
, packing
, texObj
, texImage
);
1192 st_CompressedTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1193 GLint xoffset
, GLsizei width
,
1195 GLsizei imageSize
, const GLvoid
*data
,
1196 struct gl_texture_object
*texObj
,
1197 struct gl_texture_image
*texImage
)
1204 st_CompressedTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1205 GLint xoffset
, GLint yoffset
,
1206 GLsizei width
, GLint height
,
1208 GLsizei imageSize
, const GLvoid
*data
,
1209 struct gl_texture_object
*texObj
,
1210 struct gl_texture_image
*texImage
)
1212 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1213 struct pipe_format_block block
;
1219 unsigned face
= _mesa_tex_target_to_face(target
);
1221 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1222 PIPE_TRANSFER_WRITE
);
1223 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
1224 PIPE_TRANSFER_WRITE
,
1228 block
= stImage
->pt
->block
;
1229 srcBlockStride
= pf_get_stride(&block
, width
);
1230 dstBlockStride
= stImage
->transfer
->stride
;
1232 assert(stImage
->pt
);
1233 /* TODO find good values for block and strides */
1234 /* TODO also adjust texImage->data for yoffset/xoffset */
1238 if (!texImage
->Data
) {
1239 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1243 assert(xoffset
% block
.width
== 0);
1244 assert(yoffset
% block
.height
== 0);
1245 assert(width
% block
.width
== 0);
1246 assert(height
% block
.height
== 0);
1248 for (y
= 0; y
< height
; y
+= block
.height
) {
1249 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1250 const char *src
= (const char*)data
+ srcBlockStride
* pf_get_nblocksy(&block
, y
);
1251 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* pf_get_nblocksy(&block
, y
);
1252 memcpy(dst
, src
, pf_get_stride(&block
, width
));
1256 st_texture_image_unmap(ctx
->st
, stImage
);
1257 texImage
->Data
= NULL
;
1263 st_CompressedTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1264 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1265 GLsizei width
, GLint height
, GLint depth
,
1267 GLsizei imageSize
, const GLvoid
*data
,
1268 struct gl_texture_object
*texObj
,
1269 struct gl_texture_image
*texImage
)
1277 * Do a CopyTexSubImage operation using a read transfer from the source,
1278 * a write transfer to the destination and get_tile()/put_tile() to access
1279 * the pixels/texels.
1281 * Note: srcY=0=TOP of renderbuffer
1284 fallback_copy_texsubimage(GLcontext
*ctx
, GLenum target
, GLint level
,
1285 struct st_renderbuffer
*strb
,
1286 struct st_texture_image
*stImage
,
1288 GLint destX
, GLint destY
, GLint destZ
,
1289 GLint srcX
, GLint srcY
,
1290 GLsizei width
, GLsizei height
)
1292 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1293 struct pipe_screen
*screen
= pipe
->screen
;
1294 struct pipe_transfer
*src_trans
;
1296 enum pipe_transfer_usage transfer_usage
;
1298 assert(width
<= MAX_WIDTH
);
1300 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1301 srcY
= strb
->Base
.Height
- srcY
- height
;
1304 src_trans
= st_cond_flush_get_tex_transfer( st_context(ctx
),
1311 if (baseFormat
== GL_DEPTH_COMPONENT
&&
1312 pf_is_depth_and_stencil(stImage
->pt
->format
))
1313 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1315 transfer_usage
= PIPE_TRANSFER_WRITE
;
1317 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, 0, 0,
1320 texDest
= st_texture_image_map(ctx
->st
, stImage
, 0, transfer_usage
,
1321 destX
, destY
, width
, height
);
1323 if (baseFormat
== GL_DEPTH_COMPONENT
||
1324 baseFormat
== GL_DEPTH24_STENCIL8
) {
1325 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1326 ctx
->Pixel
.DepthBias
!= 0.0F
);
1329 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1330 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1339 /* To avoid a large temp memory allocation, do copy row by row */
1340 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1341 uint data
[MAX_WIDTH
];
1342 pipe_get_tile_z(src_trans
, 0, srcY
, width
, 1, data
);
1344 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1346 pipe_put_tile_z(stImage
->transfer
, 0, row
, width
, 1, data
);
1352 (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
1354 if (tempSrc
&& texDest
) {
1355 const GLint dims
= 2;
1356 const GLint dstRowStride
= stImage
->transfer
->stride
;
1357 struct gl_texture_image
*texImage
= &stImage
->base
;
1358 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1360 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1361 unpack
.Invert
= GL_TRUE
;
1364 /* get float/RGBA image from framebuffer */
1365 /* XXX this usually involves a lot of int/float conversion.
1366 * try to avoid that someday.
1368 pipe_get_tile_rgba(src_trans
, 0, 0, width
, height
, tempSrc
);
1370 /* Store into texture memory.
1371 * Note that this does some special things such as pixel transfer
1372 * ops and format conversion. In particular, if the dest tex format
1373 * is actually RGBA but the user created the texture as GL_RGB we
1374 * need to fill-in/override the alpha channel with 1.0.
1376 texImage
->TexFormat
->StoreImage(ctx
, dims
,
1377 texImage
->_BaseFormat
,
1378 texImage
->TexFormat
,
1382 texImage
->ImageOffsets
,
1384 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1388 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1392 _mesa_free(tempSrc
);
1395 st_texture_image_unmap(ctx
->st
, stImage
);
1396 screen
->tex_transfer_destroy(src_trans
);
1401 compatible_src_dst_formats(const struct gl_renderbuffer
*src
,
1402 const struct gl_texture_image
*dst
)
1404 const GLenum srcFormat
= src
->_BaseFormat
;
1405 const GLenum dstLogicalFormat
= dst
->_BaseFormat
;
1407 if (srcFormat
== dstLogicalFormat
) {
1408 /* This is the same as matching_base_formats, which should
1409 * always pass, as it did previously.
1411 return TGSI_WRITEMASK_XYZW
;
1413 else if (srcFormat
== GL_RGBA
&&
1414 dstLogicalFormat
== GL_RGB
) {
1415 /* Add a single special case to cope with RGBA->RGB transfers,
1416 * setting A to 1.0 to cope with situations where the RGB
1417 * destination is actually stored as RGBA.
1419 return TGSI_WRITEMASK_XYZ
; /* A ==> 1.0 */
1431 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1432 * Note that the region to copy has already been clipped so we know we
1433 * won't read from outside the source renderbuffer's bounds.
1435 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1438 st_copy_texsubimage(GLcontext
*ctx
,
1439 GLenum target
, GLint level
,
1440 GLint destX
, GLint destY
, GLint destZ
,
1441 GLint srcX
, GLint srcY
,
1442 GLsizei width
, GLsizei height
)
1444 struct gl_texture_unit
*texUnit
=
1445 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1446 struct gl_texture_object
*texObj
=
1447 _mesa_select_tex_object(ctx
, texUnit
, target
);
1448 struct gl_texture_image
*texImage
=
1449 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1450 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1451 const GLenum texBaseFormat
= texImage
->InternalFormat
;
1452 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1453 struct st_renderbuffer
*strb
;
1454 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1455 struct pipe_screen
*screen
= pipe
->screen
;
1456 enum pipe_format dest_format
, src_format
;
1457 GLboolean use_fallback
= GL_TRUE
;
1458 GLboolean matching_base_formats
;
1459 GLuint format_writemask
;
1460 struct pipe_surface
*dest_surface
= NULL
;
1461 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1463 /* any rendering in progress must flushed before we grab the fb image */
1464 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
1466 /* make sure finalize_textures has been called?
1468 if (0) st_validate_state(ctx
->st
);
1470 /* determine if copying depth or color data */
1471 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1472 texBaseFormat
== GL_DEPTH24_STENCIL8
) {
1473 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1475 else if (texBaseFormat
== GL_DEPTH_STENCIL_EXT
) {
1476 strb
= st_renderbuffer(fb
->_StencilBuffer
);
1479 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1480 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1483 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1484 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1512 if (width
< 0 || height
< 0)
1517 assert(strb
->surface
);
1518 assert(stImage
->pt
);
1520 src_format
= strb
->surface
->format
;
1521 dest_format
= stImage
->pt
->format
;
1524 * Determine if the src framebuffer and dest texture have the same
1525 * base format. We need this to detect a case such as the framebuffer
1526 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1527 * texture format stores RGBA we need to set A=1 (overriding the
1528 * framebuffer's alpha values). We can't do that with the blit or
1529 * textured-quad paths.
1531 matching_base_formats
= (strb
->Base
._BaseFormat
== texImage
->_BaseFormat
);
1532 format_writemask
= compatible_src_dst_formats(&strb
->Base
, texImage
);
1534 if (ctx
->_ImageTransferState
== 0x0) {
1536 if (matching_base_formats
&&
1537 src_format
== dest_format
&&
1540 /* use surface_copy() / blit */
1542 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1543 stImage
->face
, stImage
->level
,
1545 PIPE_BUFFER_USAGE_GPU_WRITE
);
1547 /* for surface_copy(), y=0=top, always */
1548 pipe
->surface_copy(pipe
,
1557 use_fallback
= GL_FALSE
;
1559 else if (format_writemask
&&
1560 screen
->is_format_supported(screen
, src_format
,
1562 PIPE_TEXTURE_USAGE_SAMPLER
,
1564 screen
->is_format_supported(screen
, dest_format
,
1566 PIPE_TEXTURE_USAGE_RENDER_TARGET
,
1568 /* draw textured quad to do the copy */
1571 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1572 stImage
->face
, stImage
->level
,
1574 PIPE_BUFFER_USAGE_GPU_WRITE
);
1577 srcY1
= strb
->Base
.Height
- srcY
- height
;
1578 srcY0
= srcY1
+ height
;
1582 srcY1
= srcY0
+ height
;
1584 util_blit_pixels_writemask(ctx
->st
->blit
,
1587 srcX
+ width
, srcY1
,
1590 destX
+ width
, destY
+ height
,
1591 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1593 use_fallback
= GL_FALSE
;
1597 pipe_surface_reference(&dest_surface
, NULL
);
1601 /* software fallback */
1602 fallback_copy_texsubimage(ctx
, target
, level
,
1603 strb
, stImage
, texBaseFormat
,
1604 destX
, destY
, destZ
,
1605 srcX
, srcY
, width
, height
);
1612 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1613 GLenum internalFormat
,
1614 GLint x
, GLint y
, GLsizei width
, GLint border
)
1616 struct gl_texture_unit
*texUnit
=
1617 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1618 struct gl_texture_object
*texObj
=
1619 _mesa_select_tex_object(ctx
, texUnit
, target
);
1620 struct gl_texture_image
*texImage
=
1621 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1623 /* Setup or redefine the texture object, texture and texture
1624 * image. Don't populate yet.
1626 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1628 GL_RGBA
, CHAN_TYPE
, NULL
,
1629 &ctx
->DefaultPacking
, texObj
, texImage
);
1631 st_copy_texsubimage(ctx
, target
, level
,
1632 0, 0, 0, /* destX,Y,Z */
1633 x
, y
, width
, 1); /* src X, Y, size */
1638 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1639 GLenum internalFormat
,
1640 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1643 struct gl_texture_unit
*texUnit
=
1644 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1645 struct gl_texture_object
*texObj
=
1646 _mesa_select_tex_object(ctx
, texUnit
, target
);
1647 struct gl_texture_image
*texImage
=
1648 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1650 /* Setup or redefine the texture object, texture and texture
1651 * image. Don't populate yet.
1653 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1654 width
, height
, border
,
1655 GL_RGBA
, CHAN_TYPE
, NULL
,
1656 &ctx
->DefaultPacking
, texObj
, texImage
);
1658 st_copy_texsubimage(ctx
, target
, level
,
1659 0, 0, 0, /* destX,Y,Z */
1660 x
, y
, width
, height
); /* src X, Y, size */
1665 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1666 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1668 const GLint yoffset
= 0, zoffset
= 0;
1669 const GLsizei height
= 1;
1670 st_copy_texsubimage(ctx
, target
, level
,
1671 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1672 x
, y
, width
, height
); /* src X, Y, size */
1677 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1678 GLint xoffset
, GLint yoffset
,
1679 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1681 const GLint zoffset
= 0;
1682 st_copy_texsubimage(ctx
, target
, level
,
1683 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1684 x
, y
, width
, height
); /* src X, Y, size */
1689 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1690 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1691 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1693 st_copy_texsubimage(ctx
, target
, level
,
1694 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1695 x
, y
, width
, height
); /* src X, Y, size */
1700 copy_image_data_to_texture(struct st_context
*st
,
1701 struct st_texture_object
*stObj
,
1703 struct st_texture_image
*stImage
)
1706 /* Copy potentially with the blitter:
1708 st_texture_image_copy(st
->pipe
,
1709 stObj
->pt
, dstLevel
, /* dest texture, level */
1710 stImage
->pt
, /* src texture */
1714 pipe_texture_reference(&stImage
->pt
, NULL
);
1716 else if (stImage
->base
.Data
) {
1717 assert(stImage
->base
.Data
!= NULL
);
1719 /* More straightforward upload.
1722 st_teximage_flush_before_map(st
, stObj
->pt
, stImage
->face
, dstLevel
,
1723 PIPE_TRANSFER_WRITE
);
1726 st_texture_image_data(st
,
1731 stImage
->base
.RowStride
*
1732 stObj
->pt
->block
.size
,
1733 stImage
->base
.RowStride
*
1734 stImage
->base
.Height
*
1735 stObj
->pt
->block
.size
);
1736 _mesa_align_free(stImage
->base
.Data
);
1737 stImage
->base
.Data
= NULL
;
1740 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
1745 * Called during state validation. When this function is finished,
1746 * the texture object should be ready for rendering.
1747 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1750 st_finalize_texture(GLcontext
*ctx
,
1751 struct pipe_context
*pipe
,
1752 struct gl_texture_object
*tObj
,
1753 GLboolean
*needFlush
)
1755 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1756 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1758 struct st_texture_image
*firstImage
;
1760 *needFlush
= GL_FALSE
;
1762 if (stObj
->base
._Complete
) {
1763 /* The texture is complete and we know exactly how many mipmap levels
1764 * are present/needed. This is conditional because we may be called
1765 * from the st_generate_mipmap() function when the texture object is
1766 * incomplete. In that case, we'll have set stObj->lastLevel before
1769 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1770 stObj
->base
.MinFilter
== GL_NEAREST
)
1771 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1773 stObj
->lastLevel
= stObj
->base
._MaxLevel
- stObj
->base
.BaseLevel
;
1776 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1778 /* If both firstImage and stObj point to a texture which can contain
1779 * all active images, favour firstImage. Note that because of the
1780 * completeness requirement, we know that the image dimensions
1783 if (firstImage
->pt
&&
1784 firstImage
->pt
!= stObj
->pt
&&
1785 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1786 pipe_texture_reference(&stObj
->pt
, firstImage
->pt
);
1789 /* FIXME: determine format block instead of cpp */
1790 if (firstImage
->base
.IsCompressed
) {
1791 cpp
= compressed_num_bytes(firstImage
->base
.TexFormat
->MesaFormat
);
1794 cpp
= firstImage
->base
.TexFormat
->TexelBytes
;
1797 /* If we already have a gallium texture, check that it matches the texture
1798 * object's format, target, size, num_levels, etc.
1801 const enum pipe_format fmt
=
1802 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
->MesaFormat
);
1803 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1804 stObj
->pt
->format
!= fmt
||
1805 stObj
->pt
->last_level
< stObj
->lastLevel
||
1806 stObj
->pt
->width
[0] != firstImage
->base
.Width2
||
1807 stObj
->pt
->height
[0] != firstImage
->base
.Height2
||
1808 stObj
->pt
->depth
[0] != firstImage
->base
.Depth2
||
1809 /* Nominal bytes per pixel: */
1810 stObj
->pt
->block
.size
/ stObj
->pt
->block
.width
!= cpp
)
1812 pipe_texture_reference(&stObj
->pt
, NULL
);
1813 ctx
->st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1817 /* May need to create a new gallium texture:
1820 const enum pipe_format fmt
=
1821 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
->MesaFormat
);
1822 GLuint usage
= default_usage(fmt
);
1824 stObj
->pt
= st_texture_create(ctx
->st
,
1825 gl_target_to_pipe(stObj
->base
.Target
),
1828 firstImage
->base
.Width2
,
1829 firstImage
->base
.Height2
,
1830 firstImage
->base
.Depth2
,
1834 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1839 /* Pull in any images not in the object's texture:
1841 for (face
= 0; face
< nr_faces
; face
++) {
1843 for (level
= 0; level
<= stObj
->lastLevel
; level
++) {
1844 struct st_texture_image
*stImage
=
1845 st_texture_image(stObj
->base
.Image
[face
][stObj
->base
.BaseLevel
+ level
]);
1847 /* Need to import images in main memory or held in other textures.
1849 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1850 copy_image_data_to_texture(ctx
->st
, stObj
, level
, stImage
);
1851 *needFlush
= GL_TRUE
;
1861 * Returns pointer to a default/dummy texture.
1862 * This is typically used when the current shader has tex/sample instructions
1863 * but the user has not provided a (any) texture(s).
1865 struct gl_texture_object
*
1866 st_get_default_texture(struct st_context
*st
)
1868 if (!st
->default_texture
) {
1869 static const GLenum target
= GL_TEXTURE_2D
;
1870 GLubyte pixels
[16][16][4];
1871 struct gl_texture_object
*texObj
;
1872 struct gl_texture_image
*texImg
;
1875 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1876 * when attempting to sample incomplete textures.
1878 for (i
= 0; i
< 16; i
++) {
1879 for (j
= 0; j
< 16; j
++) {
1880 pixels
[i
][j
][0] = 0;
1881 pixels
[i
][j
][1] = 0;
1882 pixels
[i
][j
][2] = 0;
1883 pixels
[i
][j
][3] = 255;
1887 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1889 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1891 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1892 16, 16, 1, 0, /* w, h, d, border */
1895 st_TexImage(st
->ctx
, 2, target
,
1896 0, GL_RGBA
, /* level, intformat */
1897 16, 16, 1, 0, /* w, h, d, border */
1898 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1899 &st
->ctx
->DefaultPacking
,
1903 texObj
->MinFilter
= GL_NEAREST
;
1904 texObj
->MagFilter
= GL_NEAREST
;
1905 texObj
->_Complete
= GL_TRUE
;
1907 st
->default_texture
= texObj
;
1909 return st
->default_texture
;
1914 st_init_texture_functions(struct dd_function_table
*functions
)
1916 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1917 functions
->TexImage1D
= st_TexImage1D
;
1918 functions
->TexImage2D
= st_TexImage2D
;
1919 functions
->TexImage3D
= st_TexImage3D
;
1920 functions
->TexSubImage1D
= st_TexSubImage1D
;
1921 functions
->TexSubImage2D
= st_TexSubImage2D
;
1922 functions
->TexSubImage3D
= st_TexSubImage3D
;
1923 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1924 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1925 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1926 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1927 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1928 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1929 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1930 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1931 functions
->GenerateMipmap
= st_generate_mipmap
;
1933 functions
->GetTexImage
= st_GetTexImage
;
1935 /* compressed texture functions */
1936 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1937 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1938 functions
->CompressedTextureSize
= _mesa_compressed_texture_size
;
1940 functions
->NewTextureObject
= st_NewTextureObject
;
1941 functions
->NewTextureImage
= st_NewTextureImage
;
1942 functions
->DeleteTexture
= st_DeleteTextureObject
;
1943 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1944 functions
->UpdateTexturePalette
= 0;
1946 functions
->TextureMemCpy
= do_memcpy
;
1948 /* XXX Temporary until we can query pipe's texture sizes */
1949 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;