1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "main/mfeatures.h"
29 #include "main/bufferobj.h"
31 #include "main/convolve.h"
33 #include "main/enums.h"
34 #include "main/formats.h"
35 #include "main/image.h"
36 #include "main/imports.h"
37 #include "main/macros.h"
38 #include "main/mipmap.h"
39 #include "main/pixel.h"
40 #include "main/texcompress.h"
41 #include "main/texfetch.h"
42 #include "main/texformat.h"
43 #include "main/texgetimage.h"
44 #include "main/teximage.h"
45 #include "main/texobj.h"
46 #include "main/texstore.h"
48 #include "state_tracker/st_debug.h"
49 #include "state_tracker/st_context.h"
50 #include "state_tracker/st_cb_fbo.h"
51 #include "state_tracker/st_cb_texture.h"
52 #include "state_tracker/st_format.h"
53 #include "state_tracker/st_public.h"
54 #include "state_tracker/st_texture.h"
55 #include "state_tracker/st_gen_mipmap.h"
56 #include "state_tracker/st_inlines.h"
57 #include "state_tracker/st_atom.h"
59 #include "pipe/p_context.h"
60 #include "pipe/p_defines.h"
61 #include "pipe/p_inlines.h"
62 #include "pipe/p_shader_tokens.h"
63 #include "util/u_tile.h"
64 #include "util/u_blit.h"
65 #include "util/u_format.h"
66 #include "util/u_surface.h"
67 #include "util/u_math.h"
70 #define DBG if (0) printf
73 static enum pipe_texture_target
74 gl_target_to_pipe(GLenum target
)
78 return PIPE_TEXTURE_1D
;
81 case GL_TEXTURE_RECTANGLE_NV
:
82 return PIPE_TEXTURE_2D
;
85 return PIPE_TEXTURE_3D
;
87 case GL_TEXTURE_CUBE_MAP_ARB
:
88 return PIPE_TEXTURE_CUBE
;
97 /** called via ctx->Driver.NewTextureImage() */
98 static struct gl_texture_image
*
99 st_NewTextureImage(GLcontext
* ctx
)
101 DBG("%s\n", __FUNCTION__
);
103 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
107 /** called via ctx->Driver.NewTextureObject() */
108 static struct gl_texture_object
*
109 st_NewTextureObject(GLcontext
* ctx
, GLuint name
, GLenum target
)
111 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
113 DBG("%s\n", __FUNCTION__
);
114 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
119 /** called via ctx->Driver.DeleteTextureImage() */
121 st_DeleteTextureObject(GLcontext
*ctx
,
122 struct gl_texture_object
*texObj
)
124 struct st_texture_object
*stObj
= st_texture_object(texObj
);
126 pipe_texture_reference(&stObj
->pt
, NULL
);
128 _mesa_delete_texture_object(ctx
, texObj
);
132 /** called via ctx->Driver.FreeTexImageData() */
134 st_FreeTextureImageData(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
136 struct st_texture_image
*stImage
= st_texture_image(texImage
);
138 DBG("%s\n", __FUNCTION__
);
141 pipe_texture_reference(&stImage
->pt
, NULL
);
144 if (texImage
->Data
) {
145 _mesa_align_free(texImage
->Data
);
146 texImage
->Data
= NULL
;
152 * From linux kernel i386 header files, copes with odd sizes better
153 * than COPY_DWORDS would:
154 * XXX Put this in src/mesa/main/imports.h ???
156 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
158 __memcpy(void *to
, const void *from
, size_t n
)
161 __asm__
__volatile__("rep ; movsl\n\t"
165 "1:\ttestb $1,%b4\n\t"
167 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
168 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
173 #define __memcpy(a,b,c) memcpy(a,b,c)
178 * The system memcpy (at least on ubuntu 5.10) has problems copying
179 * to agp (writecombined) memory from a source which isn't 64-byte
180 * aligned - there is a 4x performance falloff.
182 * The x86 __memcpy is immune to this but is slightly slower
183 * (10%-ish) than the system memcpy.
185 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
186 * isn't much faster than x86_memcpy for agp copies.
188 * TODO: switch dynamically.
191 do_memcpy(void *dest
, const void *src
, size_t n
)
193 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
194 return __memcpy(dest
, src
, n
);
197 return memcpy(dest
, src
, n
);
202 * Return default texture usage bitmask for the given texture format.
205 default_usage(enum pipe_format fmt
)
207 GLuint usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
208 if (util_format_is_depth_or_stencil(fmt
))
209 usage
|= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
211 usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
217 * Allocate a pipe_texture object for the given st_texture_object using
218 * the given st_texture_image to guess the mipmap size/levels.
221 * Otherwise, store it in memory if (Border != 0) or (any dimension ==
224 * Otherwise, if max_level >= level >= min_level, create texture with
225 * space for images from min_level down to max_level.
227 * Otherwise, create texture with space for images from (level 0)..(1x1).
228 * Consider pruning this texture at a validation if the saving is worth it.
231 guess_and_alloc_texture(struct st_context
*st
,
232 struct st_texture_object
*stObj
,
233 const struct st_texture_image
*stImage
)
237 GLuint width
= stImage
->base
.Width2
; /* size w/out border */
238 GLuint height
= stImage
->base
.Height2
;
239 GLuint depth
= stImage
->base
.Depth2
;
241 enum pipe_format fmt
;
243 DBG("%s\n", __FUNCTION__
);
248 (GLint
) stImage
->level
> stObj
->base
.BaseLevel
&&
249 (stImage
->base
.Width
== 1 ||
250 (stObj
->base
.Target
!= GL_TEXTURE_1D
&&
251 stImage
->base
.Height
== 1) ||
252 (stObj
->base
.Target
== GL_TEXTURE_3D
&&
253 stImage
->base
.Depth
== 1)))
256 /* If this image disrespects BaseLevel, allocate from level zero.
257 * Usually BaseLevel == 0, so it's unlikely to happen.
259 if ((GLint
) stImage
->level
< stObj
->base
.BaseLevel
)
262 firstLevel
= stObj
->base
.BaseLevel
;
265 /* Figure out image dimensions at start level.
267 for (i
= stImage
->level
; i
> firstLevel
; i
--) {
276 if (width
== 0 || height
== 0 || depth
== 0) {
277 /* no texture needed */
281 /* Guess a reasonable value for lastLevel. This is probably going
282 * to be wrong fairly often and might mean that we have to look at
283 * resizable buffers, or require that buffers implement lazy
284 * pagetable arrangements.
286 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
287 stObj
->base
.MinFilter
== GL_LINEAR
||
288 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
289 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
290 !stObj
->base
.GenerateMipmap
&&
291 stImage
->level
== firstLevel
) {
292 /* only alloc space for a single mipmap level */
293 lastLevel
= firstLevel
;
296 /* alloc space for a full mipmap */
297 GLuint l2width
= util_logbase2(width
);
298 GLuint l2height
= util_logbase2(height
);
299 GLuint l2depth
= util_logbase2(depth
);
300 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
303 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
305 usage
= default_usage(fmt
);
307 stObj
->pt
= st_texture_create(st
,
308 gl_target_to_pipe(stObj
->base
.Target
),
316 DBG("%s - success\n", __FUNCTION__
);
321 * Adjust pixel unpack params and image dimensions to strip off the
323 * Gallium doesn't support texture borders. They've seldem been used
324 * and seldom been implemented correctly anyway.
325 * \param unpackNew returns the new pixel unpack parameters
328 strip_texture_border(GLint border
,
329 GLint
*width
, GLint
*height
, GLint
*depth
,
330 const struct gl_pixelstore_attrib
*unpack
,
331 struct gl_pixelstore_attrib
*unpackNew
)
333 assert(border
> 0); /* sanity check */
335 *unpackNew
= *unpack
;
337 if (unpackNew
->RowLength
== 0)
338 unpackNew
->RowLength
= *width
;
340 if (depth
&& unpackNew
->ImageHeight
== 0)
341 unpackNew
->ImageHeight
= *height
;
343 unpackNew
->SkipPixels
+= border
;
345 unpackNew
->SkipRows
+= border
;
347 unpackNew
->SkipImages
+= border
;
350 *width
= *width
- 2 * border
;
351 if (height
&& *height
>= 3)
352 *height
= *height
- 2 * border
;
353 if (depth
&& *depth
>= 3)
354 *depth
= *depth
- 2 * border
;
359 * Try to do texture compression via rendering. If the Gallium driver
360 * can render into a compressed surface this will allow us to do texture
362 * \return GL_TRUE for success, GL_FALSE for failure
365 compress_with_blit(GLcontext
* ctx
,
366 GLenum target
, GLint level
,
367 GLint xoffset
, GLint yoffset
, GLint zoffset
,
368 GLint width
, GLint height
, GLint depth
,
369 GLenum format
, GLenum type
, const void *pixels
,
370 const struct gl_pixelstore_attrib
*unpack
,
371 struct gl_texture_image
*texImage
)
373 const GLuint dstImageOffsets
[1] = {0};
374 struct st_texture_image
*stImage
= st_texture_image(texImage
);
375 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
376 gl_format mesa_format
;
377 struct pipe_texture templ
;
378 struct pipe_texture
*src_tex
;
379 struct pipe_surface
*dst_surface
;
380 struct pipe_transfer
*tex_xfer
;
384 /* XXX: Can this happen? Should we assert? */
388 /* get destination surface (in the compressed texture) */
389 dst_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
390 stImage
->face
, stImage
->level
, 0,
391 PIPE_BUFFER_USAGE_GPU_WRITE
);
393 /* can't render into this format (or other problem) */
397 /* Choose format for the temporary RGBA texture image.
399 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
404 /* Create the temporary source texture
406 memset(&templ
, 0, sizeof(templ
));
407 templ
.target
= PIPE_TEXTURE_2D
;
408 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
);
409 pf_get_block(templ
.format
, &templ
.block
);
410 templ
.width0
= width
;
411 templ
.height0
= height
;
413 templ
.last_level
= 0;
414 templ
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
415 src_tex
= screen
->texture_create(screen
, &templ
);
420 /* Put user's tex data into the temporary texture
422 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
), src_tex
,
423 0, 0, 0, /* face, level are zero */
425 0, 0, width
, height
); /* x, y, w, h */
426 map
= screen
->transfer_map(screen
, tex_xfer
);
428 _mesa_texstore(ctx
, 2, GL_RGBA
, mesa_format
,
430 0, 0, 0, /* dest x/y/z offset */
431 tex_xfer
->stride
, /* dest row stride (bytes) */
432 dstImageOffsets
, /* image offsets (for 3D only) */
433 width
, height
, 1, /* size */
434 format
, type
, /* source format/type */
435 pixels
, /* source data */
436 unpack
); /* source data packing */
438 screen
->transfer_unmap(screen
, tex_xfer
);
439 screen
->tex_transfer_destroy(tex_xfer
);
441 /* copy / compress image */
442 util_blit_pixels_tex(ctx
->st
->blit
,
443 src_tex
, /* pipe_texture (src) */
444 0, 0, /* src x0, y0 */
445 width
, height
, /* src x1, y1 */
446 dst_surface
, /* pipe_surface (dst) */
447 xoffset
, yoffset
, /* dst x0, y0 */
448 xoffset
+ width
, /* dst x1 */
449 yoffset
+ height
, /* dst y1 */
451 PIPE_TEX_MIPFILTER_NEAREST
);
453 pipe_surface_reference(&dst_surface
, NULL
);
454 pipe_texture_reference(&src_tex
, NULL
);
461 * Do glTexImage1/2/3D().
464 st_TexImage(GLcontext
* ctx
,
466 GLenum target
, GLint level
,
467 GLint internalFormat
,
468 GLint width
, GLint height
, GLint depth
,
470 GLenum format
, GLenum type
, const void *pixels
,
471 const struct gl_pixelstore_attrib
*unpack
,
472 struct gl_texture_object
*texObj
,
473 struct gl_texture_image
*texImage
,
474 GLsizei imageSize
, GLboolean compressed_src
)
476 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
477 struct st_texture_object
*stObj
= st_texture_object(texObj
);
478 struct st_texture_image
*stImage
= st_texture_image(texImage
);
479 GLint postConvWidth
, postConvHeight
;
480 GLint texelBytes
, sizeInBytes
;
481 GLuint dstRowStride
= 0;
482 struct gl_pixelstore_attrib unpackNB
;
483 enum pipe_transfer_usage transfer_usage
= 0;
485 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
486 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
488 /* switch to "normal" */
489 if (stObj
->surface_based
) {
490 _mesa_clear_texture_object(ctx
, texObj
);
491 stObj
->surface_based
= GL_FALSE
;
494 /* gallium does not support texture borders, strip it off */
496 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
498 texImage
->Width
= width
;
499 texImage
->Height
= height
;
500 texImage
->Depth
= depth
;
501 texImage
->Border
= 0;
505 postConvWidth
= width
;
506 postConvHeight
= height
;
508 stImage
->face
= _mesa_tex_target_to_face(target
);
509 stImage
->level
= level
;
512 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
513 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
518 _mesa_set_fetch_functions(texImage
, dims
);
520 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
521 /* must be a compressed format */
525 texelBytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
527 /* Minimum pitch of 32 bytes */
528 if (postConvWidth
* texelBytes
< 32) {
529 postConvWidth
= 32 / texelBytes
;
530 texImage
->RowStride
= postConvWidth
;
533 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
534 /*assert(texImage->RowStride == postConvWidth);*/
537 /* Release the reference to a potentially orphaned buffer.
538 * Release any old malloced memory.
541 pipe_texture_reference(&stImage
->pt
, NULL
);
542 assert(!texImage
->Data
);
544 else if (texImage
->Data
) {
545 _mesa_align_free(texImage
->Data
);
548 if (width
== 0 || height
== 0 || depth
== 0) {
549 /* stop after freeing old image */
553 /* If this is the only mipmap level in the texture, could call
554 * bmBufferData with NULL data to free the old block and avoid
555 * waiting on any outstanding fences.
558 if (stObj
->teximage_realloc
||
559 level
> (GLint
) stObj
->pt
->last_level
||
560 (stObj
->pt
->last_level
== level
&&
561 stObj
->pt
->target
!= PIPE_TEXTURE_CUBE
&&
562 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
563 stImage
->face
, stImage
->level
))) {
565 pipe_texture_reference(&stObj
->pt
, NULL
);
567 stObj
->teximage_realloc
= FALSE
;
572 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
574 /* Probably out of memory.
575 * Try flushing any pending rendering, then retry.
578 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
580 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
586 assert(!stImage
->pt
);
589 st_texture_match_image(stObj
->pt
, &stImage
->base
,
590 stImage
->face
, stImage
->level
)) {
592 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
597 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
599 /* st_CopyTexImage calls this function with pixels == NULL, with
600 * the expectation that the texture will be set up but nothing
601 * more will be done. This is where those calls return:
603 if (compressed_src
) {
604 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
606 "glCompressedTexImage");
609 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
611 pixels
, unpack
, "glTexImage");
614 /* Note: we can't check for pixels==NULL until after we've allocated
615 * memory for the texture.
618 /* See if we can do texture compression with a blit/render.
620 if (!compressed_src
&&
622 _mesa_is_format_compressed(texImage
->TexFormat
) &&
623 screen
->is_format_supported(screen
,
626 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
630 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
631 format
, type
, pixels
, unpack
, texImage
)) {
637 if (format
== GL_DEPTH_COMPONENT
&&
638 pf_is_depth_and_stencil(stImage
->pt
->format
))
639 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
641 transfer_usage
= PIPE_TRANSFER_WRITE
;
643 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
644 transfer_usage
, 0, 0,
646 stImage
->base
.Height
);
647 if(stImage
->transfer
)
648 dstRowStride
= stImage
->transfer
->stride
;
651 /* Allocate regular memory and store the image there temporarily. */
652 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
653 sizeInBytes
= _mesa_format_image_size(texImage
->TexFormat
,
657 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
661 dstRowStride
= postConvWidth
* texelBytes
;
662 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
665 texImage
->Data
= _mesa_align_malloc(sizeInBytes
, 16);
668 if (!texImage
->Data
) {
669 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
676 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
677 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
679 /* Copy data. Would like to know when it's ok for us to eg. use
680 * the blitter to copy. Or, use the hardware to do the format
681 * conversion and copy:
683 if (compressed_src
) {
684 memcpy(texImage
->Data
, pixels
, imageSize
);
687 const GLuint srcImageStride
=
688 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
690 const GLubyte
*src
= (const GLubyte
*) pixels
;
692 for (i
= 0; i
< depth
; i
++) {
693 if (!_mesa_texstore(ctx
, dims
,
694 texImage
->_BaseFormat
,
697 0, 0, 0, /* dstX/Y/Zoffset */
699 texImage
->ImageOffsets
,
701 format
, type
, src
, unpack
)) {
702 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
705 if (stImage
->pt
&& i
+ 1 < depth
) {
706 /* unmap this slice */
707 st_texture_image_unmap(ctx
->st
, stImage
);
708 /* map next slice of 3D texture */
709 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
710 transfer_usage
, 0, 0,
712 stImage
->base
.Height
);
713 src
+= srcImageStride
;
719 _mesa_unmap_teximage_pbo(ctx
, unpack
);
721 if (stImage
->pt
&& texImage
->Data
) {
722 st_texture_image_unmap(ctx
->st
, stImage
);
723 texImage
->Data
= NULL
;
729 st_TexImage3D(GLcontext
* ctx
,
730 GLenum target
, GLint level
,
731 GLint internalFormat
,
732 GLint width
, GLint height
, GLint depth
,
734 GLenum format
, GLenum type
, const void *pixels
,
735 const struct gl_pixelstore_attrib
*unpack
,
736 struct gl_texture_object
*texObj
,
737 struct gl_texture_image
*texImage
)
739 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
740 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
746 st_TexImage2D(GLcontext
* ctx
,
747 GLenum target
, GLint level
,
748 GLint internalFormat
,
749 GLint width
, GLint height
, GLint border
,
750 GLenum format
, GLenum type
, const void *pixels
,
751 const struct gl_pixelstore_attrib
*unpack
,
752 struct gl_texture_object
*texObj
,
753 struct gl_texture_image
*texImage
)
755 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
756 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
761 st_TexImage1D(GLcontext
* ctx
,
762 GLenum target
, GLint level
,
763 GLint internalFormat
,
764 GLint width
, GLint border
,
765 GLenum format
, GLenum type
, const void *pixels
,
766 const struct gl_pixelstore_attrib
*unpack
,
767 struct gl_texture_object
*texObj
,
768 struct gl_texture_image
*texImage
)
770 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
771 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
776 st_CompressedTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
777 GLint internalFormat
,
778 GLint width
, GLint height
, GLint border
,
779 GLsizei imageSize
, const GLvoid
*data
,
780 struct gl_texture_object
*texObj
,
781 struct gl_texture_image
*texImage
)
783 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
784 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
790 * glGetTexImage() helper: decompress a compressed texture by rendering
791 * a textured quad. Store the results in the user's buffer.
794 decompress_with_blit(GLcontext
* ctx
, GLenum target
, GLint level
,
795 GLenum format
, GLenum type
, GLvoid
*pixels
,
796 struct gl_texture_object
*texObj
,
797 struct gl_texture_image
*texImage
)
799 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
800 struct st_texture_image
*stImage
= st_texture_image(texImage
);
801 const GLuint width
= texImage
->Width
;
802 const GLuint height
= texImage
->Height
;
803 struct pipe_surface
*dst_surface
;
804 struct pipe_texture
*dst_texture
;
805 struct pipe_transfer
*tex_xfer
;
807 /* create temp / dest surface */
808 if (!util_create_rgba_surface(screen
, width
, height
,
809 &dst_texture
, &dst_surface
)) {
810 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
811 "in decompress_with_blit()");
815 /* blit/render/decompress */
816 util_blit_pixels_tex(ctx
->st
->blit
,
817 stImage
->pt
, /* pipe_texture (src) */
818 0, 0, /* src x0, y0 */
819 width
, height
, /* src x1, y1 */
820 dst_surface
, /* pipe_surface (dst) */
821 0, 0, /* dst x0, y0 */
822 width
, height
, /* dst x1, y1 */
824 PIPE_TEX_MIPFILTER_NEAREST
);
826 /* map the dst_surface so we can read from it */
827 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
),
828 dst_texture
, 0, 0, 0,
830 0, 0, width
, height
);
832 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
834 /* copy/pack data into user buffer */
835 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
837 const uint bytesPerRow
= width
* pf_get_size(stImage
->pt
->format
);
838 ubyte
*map
= screen
->transfer_map(screen
, tex_xfer
);
840 for (row
= 0; row
< height
; row
++) {
841 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
842 height
, format
, type
, row
, 0);
843 memcpy(dest
, map
, bytesPerRow
);
844 map
+= tex_xfer
->stride
;
846 screen
->transfer_unmap(screen
, tex_xfer
);
849 /* format translation via floats */
851 for (row
= 0; row
< height
; row
++) {
852 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
853 GLfloat rgba
[4 * MAX_WIDTH
];
854 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
855 height
, format
, type
, row
, 0);
857 if (ST_DEBUG
& DEBUG_FALLBACK
)
858 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
860 /* get float[4] rgba row from surface */
861 pipe_get_tile_rgba(tex_xfer
, 0, row
, width
, 1, rgba
);
863 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
864 type
, dest
, &ctx
->Pack
, transferOps
);
868 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
870 /* destroy the temp / dest surface */
871 util_destroy_rgba_surface(dst_texture
, dst_surface
);
877 * Need to map texture image into memory before copying image data,
881 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
882 GLenum format
, GLenum type
, GLvoid
* pixels
,
883 struct gl_texture_object
*texObj
,
884 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
886 struct st_texture_image
*stImage
= st_texture_image(texImage
);
887 const GLuint dstImageStride
=
888 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
894 util_format_is_compressed(stImage
->pt
->format
) &&
896 /* Need to decompress the texture.
897 * We'll do this by rendering a textured quad.
898 * Note that we only expect RGBA formats (no Z/depth formats).
900 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
907 /* Image is stored in hardware format in a buffer managed by the
908 * kernel. Need to explicitly map and unmap it.
910 unsigned face
= _mesa_tex_target_to_face(target
);
912 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
915 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
916 PIPE_TRANSFER_READ
, 0, 0,
918 stImage
->base
.Height
);
919 texImage
->RowStride
= stImage
->transfer
->stride
/ stImage
->pt
->block
.size
;
922 /* Otherwise, the image should actually be stored in
923 * texImage->Data. This is pretty confusing for
924 * everybody, I'd much prefer to separate the two functions of
925 * texImage->Data - storage for texture images in main memory
926 * and access (ie mappings) of images. In other words, we'd
927 * create a new texImage->Map field and leave Data simply for
930 assert(texImage
->Data
);
933 depth
= texImage
->Depth
;
936 dest
= (GLubyte
*) pixels
;
938 for (i
= 0; i
< depth
; i
++) {
939 if (compressed_dst
) {
940 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
944 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
948 if (stImage
->pt
&& i
+ 1 < depth
) {
949 /* unmap this slice */
950 st_texture_image_unmap(ctx
->st
, stImage
);
951 /* map next slice of 3D texture */
952 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
953 PIPE_TRANSFER_READ
, 0, 0,
955 stImage
->base
.Height
);
956 dest
+= dstImageStride
;
960 texImage
->Depth
= depth
;
964 st_texture_image_unmap(ctx
->st
, stImage
);
965 texImage
->Data
= NULL
;
971 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
972 GLenum format
, GLenum type
, GLvoid
* pixels
,
973 struct gl_texture_object
*texObj
,
974 struct gl_texture_image
*texImage
)
976 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
982 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
984 struct gl_texture_object
*texObj
,
985 struct gl_texture_image
*texImage
)
987 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
994 st_TexSubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
995 GLint xoffset
, GLint yoffset
, GLint zoffset
,
996 GLint width
, GLint height
, GLint depth
,
997 GLenum format
, GLenum type
, const void *pixels
,
998 const struct gl_pixelstore_attrib
*packing
,
999 struct gl_texture_object
*texObj
,
1000 struct gl_texture_image
*texImage
)
1002 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
1003 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1004 GLuint dstRowStride
;
1005 const GLuint srcImageStride
=
1006 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1009 /* init to silence warning only: */
1010 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1012 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1013 _mesa_lookup_enum_by_nr(target
),
1014 level
, xoffset
, yoffset
, width
, height
);
1017 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1018 type
, pixels
, packing
, "glTexSubImage2D");
1022 /* See if we can do texture compression with a blit/render.
1024 if (!ctx
->Mesa_DXTn
&&
1025 _mesa_is_format_compressed(texImage
->TexFormat
) &&
1026 screen
->is_format_supported(screen
,
1027 stImage
->pt
->format
,
1028 stImage
->pt
->target
,
1029 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
1030 if (compress_with_blit(ctx
, target
, level
,
1031 xoffset
, yoffset
, zoffset
,
1032 width
, height
, depth
,
1033 format
, type
, pixels
, packing
, texImage
)) {
1038 /* Map buffer if necessary. Need to lock to prevent other contexts
1039 * from uploading the buffer under us.
1042 unsigned face
= _mesa_tex_target_to_face(target
);
1044 if (format
== GL_DEPTH_COMPONENT
&&
1045 pf_is_depth_and_stencil(stImage
->pt
->format
))
1046 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1048 transfer_usage
= PIPE_TRANSFER_WRITE
;
1050 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1052 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
,
1058 if (!texImage
->Data
) {
1059 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1063 src
= (const GLubyte
*) pixels
;
1064 dstRowStride
= stImage
->transfer
->stride
;
1066 for (i
= 0; i
< depth
; i
++) {
1067 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1068 texImage
->TexFormat
,
1072 texImage
->ImageOffsets
,
1074 format
, type
, src
, packing
)) {
1075 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1078 if (stImage
->pt
&& i
+ 1 < depth
) {
1079 /* unmap this slice */
1080 st_texture_image_unmap(ctx
->st
, stImage
);
1081 /* map next slice of 3D texture */
1082 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
,
1087 src
+= srcImageStride
;
1092 _mesa_unmap_teximage_pbo(ctx
, packing
);
1095 st_texture_image_unmap(ctx
->st
, stImage
);
1096 texImage
->Data
= NULL
;
1103 st_TexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1104 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1105 GLsizei width
, GLsizei height
, GLsizei depth
,
1106 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1107 const struct gl_pixelstore_attrib
*packing
,
1108 struct gl_texture_object
*texObj
,
1109 struct gl_texture_image
*texImage
)
1111 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1112 width
, height
, depth
, format
, type
,
1113 pixels
, packing
, texObj
, texImage
);
1118 st_TexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1119 GLint xoffset
, GLint yoffset
,
1120 GLsizei width
, GLsizei height
,
1121 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1122 const struct gl_pixelstore_attrib
*packing
,
1123 struct gl_texture_object
*texObj
,
1124 struct gl_texture_image
*texImage
)
1126 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1127 width
, height
, 1, format
, type
,
1128 pixels
, packing
, texObj
, texImage
);
1133 st_TexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1134 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1135 const GLvoid
* pixels
,
1136 const struct gl_pixelstore_attrib
*packing
,
1137 struct gl_texture_object
*texObj
,
1138 struct gl_texture_image
*texImage
)
1140 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1141 format
, type
, pixels
, packing
, texObj
, texImage
);
1146 st_CompressedTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1147 GLint xoffset
, GLsizei width
,
1149 GLsizei imageSize
, const GLvoid
*data
,
1150 struct gl_texture_object
*texObj
,
1151 struct gl_texture_image
*texImage
)
1158 st_CompressedTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1159 GLint xoffset
, GLint yoffset
,
1160 GLsizei width
, GLint height
,
1162 GLsizei imageSize
, const GLvoid
*data
,
1163 struct gl_texture_object
*texObj
,
1164 struct gl_texture_image
*texImage
)
1166 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1167 struct pipe_format_block block
;
1173 unsigned face
= _mesa_tex_target_to_face(target
);
1175 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1176 PIPE_TRANSFER_WRITE
);
1177 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
1178 PIPE_TRANSFER_WRITE
,
1182 block
= stImage
->pt
->block
;
1183 srcBlockStride
= pf_get_stride(&block
, width
);
1184 dstBlockStride
= stImage
->transfer
->stride
;
1186 assert(stImage
->pt
);
1187 /* TODO find good values for block and strides */
1188 /* TODO also adjust texImage->data for yoffset/xoffset */
1192 if (!texImage
->Data
) {
1193 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1197 assert(xoffset
% block
.width
== 0);
1198 assert(yoffset
% block
.height
== 0);
1199 assert(width
% block
.width
== 0);
1200 assert(height
% block
.height
== 0);
1202 for (y
= 0; y
< height
; y
+= block
.height
) {
1203 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1204 const char *src
= (const char*)data
+ srcBlockStride
* pf_get_nblocksy(&block
, y
);
1205 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* pf_get_nblocksy(&block
, y
);
1206 memcpy(dst
, src
, pf_get_stride(&block
, width
));
1210 st_texture_image_unmap(ctx
->st
, stImage
);
1211 texImage
->Data
= NULL
;
1217 st_CompressedTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1218 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1219 GLsizei width
, GLint height
, GLint depth
,
1221 GLsizei imageSize
, const GLvoid
*data
,
1222 struct gl_texture_object
*texObj
,
1223 struct gl_texture_image
*texImage
)
1231 * Do a CopyTexSubImage operation using a read transfer from the source,
1232 * a write transfer to the destination and get_tile()/put_tile() to access
1233 * the pixels/texels.
1235 * Note: srcY=0=TOP of renderbuffer
1238 fallback_copy_texsubimage(GLcontext
*ctx
, GLenum target
, GLint level
,
1239 struct st_renderbuffer
*strb
,
1240 struct st_texture_image
*stImage
,
1242 GLint destX
, GLint destY
, GLint destZ
,
1243 GLint srcX
, GLint srcY
,
1244 GLsizei width
, GLsizei height
)
1246 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1247 struct pipe_screen
*screen
= pipe
->screen
;
1248 struct pipe_transfer
*src_trans
;
1250 enum pipe_transfer_usage transfer_usage
;
1252 if (ST_DEBUG
& DEBUG_FALLBACK
)
1253 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1255 assert(width
<= MAX_WIDTH
);
1257 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1258 srcY
= strb
->Base
.Height
- srcY
- height
;
1261 src_trans
= st_cond_flush_get_tex_transfer( st_context(ctx
),
1268 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1269 baseFormat
== GL_DEPTH_STENCIL
) &&
1270 pf_is_depth_and_stencil(stImage
->pt
->format
))
1271 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1273 transfer_usage
= PIPE_TRANSFER_WRITE
;
1275 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, 0, 0,
1278 texDest
= st_texture_image_map(ctx
->st
, stImage
, 0, transfer_usage
,
1279 destX
, destY
, width
, height
);
1281 if (baseFormat
== GL_DEPTH_COMPONENT
||
1282 baseFormat
== GL_DEPTH_STENCIL
) {
1283 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1284 ctx
->Pixel
.DepthBias
!= 0.0F
);
1287 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1288 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1297 /* To avoid a large temp memory allocation, do copy row by row */
1298 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1299 uint data
[MAX_WIDTH
];
1300 pipe_get_tile_z(src_trans
, 0, srcY
, width
, 1, data
);
1302 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1304 pipe_put_tile_z(stImage
->transfer
, 0, row
, width
, 1, data
);
1310 (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
1312 if (tempSrc
&& texDest
) {
1313 const GLint dims
= 2;
1314 const GLint dstRowStride
= stImage
->transfer
->stride
;
1315 struct gl_texture_image
*texImage
= &stImage
->base
;
1316 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1318 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1319 unpack
.Invert
= GL_TRUE
;
1322 /* get float/RGBA image from framebuffer */
1323 /* XXX this usually involves a lot of int/float conversion.
1324 * try to avoid that someday.
1326 pipe_get_tile_rgba(src_trans
, 0, 0, width
, height
, tempSrc
);
1328 /* Store into texture memory.
1329 * Note that this does some special things such as pixel transfer
1330 * ops and format conversion. In particular, if the dest tex format
1331 * is actually RGBA but the user created the texture as GL_RGB we
1332 * need to fill-in/override the alpha channel with 1.0.
1334 _mesa_texstore(ctx
, dims
,
1335 texImage
->_BaseFormat
,
1336 texImage
->TexFormat
,
1340 texImage
->ImageOffsets
,
1342 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1346 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1350 _mesa_free(tempSrc
);
1353 st_texture_image_unmap(ctx
->st
, stImage
);
1354 screen
->tex_transfer_destroy(src_trans
);
1359 compatible_src_dst_formats(const struct gl_renderbuffer
*src
,
1360 const struct gl_texture_image
*dst
)
1362 const GLenum srcFormat
= _mesa_get_format_base_format(src
->Format
);
1363 const GLenum dstLogicalFormat
= _mesa_get_format_base_format(dst
->TexFormat
);
1365 if (srcFormat
== dstLogicalFormat
) {
1366 /* This is the same as matching_base_formats, which should
1367 * always pass, as it did previously.
1369 return TGSI_WRITEMASK_XYZW
;
1371 else if (srcFormat
== GL_RGBA
&&
1372 dstLogicalFormat
== GL_RGB
) {
1373 /* Add a single special case to cope with RGBA->RGB transfers,
1374 * setting A to 1.0 to cope with situations where the RGB
1375 * destination is actually stored as RGBA.
1377 return TGSI_WRITEMASK_XYZ
; /* A ==> 1.0 */
1380 if (ST_DEBUG
& DEBUG_FALLBACK
)
1381 debug_printf("%s failed for src %s, dst %s\n",
1383 _mesa_lookup_enum_by_nr(srcFormat
),
1384 _mesa_lookup_enum_by_nr(dstLogicalFormat
));
1395 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1396 * Note that the region to copy has already been clipped so we know we
1397 * won't read from outside the source renderbuffer's bounds.
1399 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1402 st_copy_texsubimage(GLcontext
*ctx
,
1403 GLenum target
, GLint level
,
1404 GLint destX
, GLint destY
, GLint destZ
,
1405 GLint srcX
, GLint srcY
,
1406 GLsizei width
, GLsizei height
)
1408 struct gl_texture_unit
*texUnit
=
1409 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1410 struct gl_texture_object
*texObj
=
1411 _mesa_select_tex_object(ctx
, texUnit
, target
);
1412 struct gl_texture_image
*texImage
=
1413 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1414 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1415 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1416 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1417 struct st_renderbuffer
*strb
;
1418 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1419 struct pipe_screen
*screen
= pipe
->screen
;
1420 enum pipe_format dest_format
, src_format
;
1421 GLboolean use_fallback
= GL_TRUE
;
1422 GLboolean matching_base_formats
;
1423 GLuint format_writemask
;
1424 struct pipe_surface
*dest_surface
= NULL
;
1425 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1427 /* any rendering in progress must flushed before we grab the fb image */
1428 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
1430 /* make sure finalize_textures has been called?
1432 if (0) st_validate_state(ctx
->st
);
1434 /* determine if copying depth or color data */
1435 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1436 texBaseFormat
== GL_DEPTH_STENCIL
) {
1437 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1440 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1441 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1444 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1445 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1473 if (width
< 0 || height
< 0)
1478 assert(strb
->surface
);
1479 assert(stImage
->pt
);
1481 src_format
= strb
->surface
->format
;
1482 dest_format
= stImage
->pt
->format
;
1485 * Determine if the src framebuffer and dest texture have the same
1486 * base format. We need this to detect a case such as the framebuffer
1487 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1488 * texture format stores RGBA we need to set A=1 (overriding the
1489 * framebuffer's alpha values). We can't do that with the blit or
1490 * textured-quad paths.
1492 matching_base_formats
=
1493 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1494 _mesa_get_format_base_format(texImage
->TexFormat
));
1495 format_writemask
= compatible_src_dst_formats(&strb
->Base
, texImage
);
1497 if (ctx
->_ImageTransferState
== 0x0) {
1499 if (pipe
->surface_copy
&&
1500 matching_base_formats
&&
1501 src_format
== dest_format
&&
1504 /* use surface_copy() / blit */
1506 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1507 stImage
->face
, stImage
->level
,
1509 PIPE_BUFFER_USAGE_GPU_WRITE
);
1511 /* for surface_copy(), y=0=top, always */
1512 pipe
->surface_copy(pipe
,
1521 use_fallback
= GL_FALSE
;
1523 else if (format_writemask
&&
1524 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1525 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1526 screen
->is_format_supported(screen
, src_format
,
1528 PIPE_TEXTURE_USAGE_SAMPLER
,
1530 screen
->is_format_supported(screen
, dest_format
,
1532 PIPE_TEXTURE_USAGE_RENDER_TARGET
,
1534 /* draw textured quad to do the copy */
1537 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1538 stImage
->face
, stImage
->level
,
1540 PIPE_BUFFER_USAGE_GPU_WRITE
);
1543 srcY1
= strb
->Base
.Height
- srcY
- height
;
1544 srcY0
= srcY1
+ height
;
1548 srcY1
= srcY0
+ height
;
1550 util_blit_pixels_writemask(ctx
->st
->blit
,
1553 srcX
+ width
, srcY1
,
1556 destX
+ width
, destY
+ height
,
1557 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1559 use_fallback
= GL_FALSE
;
1563 pipe_surface_reference(&dest_surface
, NULL
);
1567 /* software fallback */
1568 fallback_copy_texsubimage(ctx
, target
, level
,
1569 strb
, stImage
, texBaseFormat
,
1570 destX
, destY
, destZ
,
1571 srcX
, srcY
, width
, height
);
1578 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1579 GLenum internalFormat
,
1580 GLint x
, GLint y
, GLsizei width
, GLint border
)
1582 struct gl_texture_unit
*texUnit
=
1583 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1584 struct gl_texture_object
*texObj
=
1585 _mesa_select_tex_object(ctx
, texUnit
, target
);
1586 struct gl_texture_image
*texImage
=
1587 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1589 /* Setup or redefine the texture object, texture and texture
1590 * image. Don't populate yet.
1592 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1594 GL_RGBA
, CHAN_TYPE
, NULL
,
1595 &ctx
->DefaultPacking
, texObj
, texImage
);
1597 st_copy_texsubimage(ctx
, target
, level
,
1598 0, 0, 0, /* destX,Y,Z */
1599 x
, y
, width
, 1); /* src X, Y, size */
1604 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1605 GLenum internalFormat
,
1606 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1609 struct gl_texture_unit
*texUnit
=
1610 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1611 struct gl_texture_object
*texObj
=
1612 _mesa_select_tex_object(ctx
, texUnit
, target
);
1613 struct gl_texture_image
*texImage
=
1614 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1616 /* Setup or redefine the texture object, texture and texture
1617 * image. Don't populate yet.
1619 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1620 width
, height
, border
,
1621 GL_RGBA
, CHAN_TYPE
, NULL
,
1622 &ctx
->DefaultPacking
, texObj
, texImage
);
1624 st_copy_texsubimage(ctx
, target
, level
,
1625 0, 0, 0, /* destX,Y,Z */
1626 x
, y
, width
, height
); /* src X, Y, size */
1631 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1632 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1634 const GLint yoffset
= 0, zoffset
= 0;
1635 const GLsizei height
= 1;
1636 st_copy_texsubimage(ctx
, target
, level
,
1637 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1638 x
, y
, width
, height
); /* src X, Y, size */
1643 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1644 GLint xoffset
, GLint yoffset
,
1645 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1647 const GLint zoffset
= 0;
1648 st_copy_texsubimage(ctx
, target
, level
,
1649 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1650 x
, y
, width
, height
); /* src X, Y, size */
1655 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1656 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1657 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1659 st_copy_texsubimage(ctx
, target
, level
,
1660 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1661 x
, y
, width
, height
); /* src X, Y, size */
1666 copy_image_data_to_texture(struct st_context
*st
,
1667 struct st_texture_object
*stObj
,
1669 struct st_texture_image
*stImage
)
1672 /* Copy potentially with the blitter:
1674 st_texture_image_copy(st
->pipe
,
1675 stObj
->pt
, dstLevel
, /* dest texture, level */
1676 stImage
->pt
, /* src texture */
1680 pipe_texture_reference(&stImage
->pt
, NULL
);
1682 else if (stImage
->base
.Data
) {
1683 /* More straightforward upload.
1686 st_teximage_flush_before_map(st
, stObj
->pt
, stImage
->face
, dstLevel
,
1687 PIPE_TRANSFER_WRITE
);
1690 st_texture_image_data(st
,
1695 stImage
->base
.RowStride
*
1696 stObj
->pt
->block
.size
,
1697 stImage
->base
.RowStride
*
1698 stImage
->base
.Height
*
1699 stObj
->pt
->block
.size
);
1700 _mesa_align_free(stImage
->base
.Data
);
1701 stImage
->base
.Data
= NULL
;
1704 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
1709 * Called during state validation. When this function is finished,
1710 * the texture object should be ready for rendering.
1711 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1714 st_finalize_texture(GLcontext
*ctx
,
1715 struct pipe_context
*pipe
,
1716 struct gl_texture_object
*tObj
,
1717 GLboolean
*needFlush
)
1719 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1720 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1721 GLuint blockSize
, face
;
1722 struct st_texture_image
*firstImage
;
1724 *needFlush
= GL_FALSE
;
1726 if (stObj
->base
._Complete
) {
1727 /* The texture is complete and we know exactly how many mipmap levels
1728 * are present/needed. This is conditional because we may be called
1729 * from the st_generate_mipmap() function when the texture object is
1730 * incomplete. In that case, we'll have set stObj->lastLevel before
1733 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1734 stObj
->base
.MinFilter
== GL_NEAREST
)
1735 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1737 stObj
->lastLevel
= stObj
->base
._MaxLevel
- stObj
->base
.BaseLevel
;
1740 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1742 /* If both firstImage and stObj point to a texture which can contain
1743 * all active images, favour firstImage. Note that because of the
1744 * completeness requirement, we know that the image dimensions
1747 if (firstImage
->pt
&&
1748 firstImage
->pt
!= stObj
->pt
&&
1749 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1750 pipe_texture_reference(&stObj
->pt
, firstImage
->pt
);
1753 /* bytes per pixel block (blocks are usually 1x1) */
1754 blockSize
= _mesa_get_format_bytes(firstImage
->base
.TexFormat
);
1756 /* If we already have a gallium texture, check that it matches the texture
1757 * object's format, target, size, num_levels, etc.
1760 const enum pipe_format fmt
=
1761 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1762 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1763 stObj
->pt
->format
!= fmt
||
1764 stObj
->pt
->last_level
< stObj
->lastLevel
||
1765 stObj
->pt
->width0
!= firstImage
->base
.Width2
||
1766 stObj
->pt
->height0
!= firstImage
->base
.Height2
||
1767 stObj
->pt
->depth0
!= firstImage
->base
.Depth2
||
1768 stObj
->pt
->block
.size
!= blockSize
)
1770 pipe_texture_reference(&stObj
->pt
, NULL
);
1771 ctx
->st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1775 /* May need to create a new gallium texture:
1778 const enum pipe_format fmt
=
1779 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1780 GLuint usage
= default_usage(fmt
);
1782 stObj
->pt
= st_texture_create(ctx
->st
,
1783 gl_target_to_pipe(stObj
->base
.Target
),
1786 firstImage
->base
.Width2
,
1787 firstImage
->base
.Height2
,
1788 firstImage
->base
.Depth2
,
1792 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1797 /* Pull in any images not in the object's texture:
1799 for (face
= 0; face
< nr_faces
; face
++) {
1801 for (level
= 0; level
<= stObj
->lastLevel
; level
++) {
1802 struct st_texture_image
*stImage
=
1803 st_texture_image(stObj
->base
.Image
[face
][stObj
->base
.BaseLevel
+ level
]);
1805 /* Need to import images in main memory or held in other textures.
1807 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1808 copy_image_data_to_texture(ctx
->st
, stObj
, level
, stImage
);
1809 *needFlush
= GL_TRUE
;
1819 * Returns pointer to a default/dummy texture.
1820 * This is typically used when the current shader has tex/sample instructions
1821 * but the user has not provided a (any) texture(s).
1823 struct gl_texture_object
*
1824 st_get_default_texture(struct st_context
*st
)
1826 if (!st
->default_texture
) {
1827 static const GLenum target
= GL_TEXTURE_2D
;
1828 GLubyte pixels
[16][16][4];
1829 struct gl_texture_object
*texObj
;
1830 struct gl_texture_image
*texImg
;
1833 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1834 * when attempting to sample incomplete textures.
1836 for (i
= 0; i
< 16; i
++) {
1837 for (j
= 0; j
< 16; j
++) {
1838 pixels
[i
][j
][0] = 0;
1839 pixels
[i
][j
][1] = 0;
1840 pixels
[i
][j
][2] = 0;
1841 pixels
[i
][j
][3] = 255;
1845 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1847 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1849 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1850 16, 16, 1, 0, /* w, h, d, border */
1853 st_TexImage(st
->ctx
, 2, target
,
1854 0, GL_RGBA
, /* level, intformat */
1855 16, 16, 1, 0, /* w, h, d, border */
1856 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1857 &st
->ctx
->DefaultPacking
,
1861 texObj
->MinFilter
= GL_NEAREST
;
1862 texObj
->MagFilter
= GL_NEAREST
;
1863 texObj
->_Complete
= GL_TRUE
;
1865 st
->default_texture
= texObj
;
1867 return st
->default_texture
;
1872 st_init_texture_functions(struct dd_function_table
*functions
)
1874 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1875 functions
->TexImage1D
= st_TexImage1D
;
1876 functions
->TexImage2D
= st_TexImage2D
;
1877 functions
->TexImage3D
= st_TexImage3D
;
1878 functions
->TexSubImage1D
= st_TexSubImage1D
;
1879 functions
->TexSubImage2D
= st_TexSubImage2D
;
1880 functions
->TexSubImage3D
= st_TexSubImage3D
;
1881 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1882 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1883 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1884 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1885 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1886 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1887 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1888 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1889 functions
->GenerateMipmap
= st_generate_mipmap
;
1891 functions
->GetTexImage
= st_GetTexImage
;
1893 /* compressed texture functions */
1894 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1895 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1897 functions
->NewTextureObject
= st_NewTextureObject
;
1898 functions
->NewTextureImage
= st_NewTextureImage
;
1899 functions
->DeleteTexture
= st_DeleteTextureObject
;
1900 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1901 functions
->UpdateTexturePalette
= 0;
1903 functions
->TextureMemCpy
= do_memcpy
;
1905 /* XXX Temporary until we can query pipe's texture sizes */
1906 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;