1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "main/mfeatures.h"
29 #include "main/bufferobj.h"
31 #include "main/convolve.h"
33 #include "main/enums.h"
34 #include "main/formats.h"
35 #include "main/image.h"
36 #include "main/imports.h"
37 #include "main/macros.h"
38 #include "main/mipmap.h"
39 #include "main/pixel.h"
40 #include "main/texcompress.h"
41 #include "main/texfetch.h"
42 #include "main/texformat.h"
43 #include "main/texgetimage.h"
44 #include "main/teximage.h"
45 #include "main/texobj.h"
46 #include "main/texstore.h"
48 #include "state_tracker/st_debug.h"
49 #include "state_tracker/st_context.h"
50 #include "state_tracker/st_cb_fbo.h"
51 #include "state_tracker/st_cb_texture.h"
52 #include "state_tracker/st_format.h"
53 #include "state_tracker/st_public.h"
54 #include "state_tracker/st_texture.h"
55 #include "state_tracker/st_gen_mipmap.h"
56 #include "state_tracker/st_inlines.h"
57 #include "state_tracker/st_atom.h"
59 #include "pipe/p_context.h"
60 #include "pipe/p_defines.h"
61 #include "pipe/p_inlines.h"
62 #include "pipe/p_shader_tokens.h"
63 #include "util/u_tile.h"
64 #include "util/u_blit.h"
65 #include "util/u_surface.h"
66 #include "util/u_math.h"
69 #define DBG if (0) printf
72 static enum pipe_texture_target
73 gl_target_to_pipe(GLenum target
)
77 return PIPE_TEXTURE_1D
;
80 case GL_TEXTURE_RECTANGLE_NV
:
81 return PIPE_TEXTURE_2D
;
84 return PIPE_TEXTURE_3D
;
86 case GL_TEXTURE_CUBE_MAP_ARB
:
87 return PIPE_TEXTURE_CUBE
;
96 /** called via ctx->Driver.NewTextureImage() */
97 static struct gl_texture_image
*
98 st_NewTextureImage(GLcontext
* ctx
)
100 DBG("%s\n", __FUNCTION__
);
102 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
106 /** called via ctx->Driver.NewTextureObject() */
107 static struct gl_texture_object
*
108 st_NewTextureObject(GLcontext
* ctx
, GLuint name
, GLenum target
)
110 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
112 DBG("%s\n", __FUNCTION__
);
113 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
118 /** called via ctx->Driver.DeleteTextureImage() */
120 st_DeleteTextureObject(GLcontext
*ctx
,
121 struct gl_texture_object
*texObj
)
123 struct st_texture_object
*stObj
= st_texture_object(texObj
);
125 pipe_texture_reference(&stObj
->pt
, NULL
);
127 _mesa_delete_texture_object(ctx
, texObj
);
131 /** called via ctx->Driver.FreeTexImageData() */
133 st_FreeTextureImageData(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
135 struct st_texture_image
*stImage
= st_texture_image(texImage
);
137 DBG("%s\n", __FUNCTION__
);
140 pipe_texture_reference(&stImage
->pt
, NULL
);
143 if (texImage
->Data
) {
144 _mesa_align_free(texImage
->Data
);
145 texImage
->Data
= NULL
;
151 * From linux kernel i386 header files, copes with odd sizes better
152 * than COPY_DWORDS would:
153 * XXX Put this in src/mesa/main/imports.h ???
155 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
157 __memcpy(void *to
, const void *from
, size_t n
)
160 __asm__
__volatile__("rep ; movsl\n\t"
164 "1:\ttestb $1,%b4\n\t"
166 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
167 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
172 #define __memcpy(a,b,c) memcpy(a,b,c)
177 * The system memcpy (at least on ubuntu 5.10) has problems copying
178 * to agp (writecombined) memory from a source which isn't 64-byte
179 * aligned - there is a 4x performance falloff.
181 * The x86 __memcpy is immune to this but is slightly slower
182 * (10%-ish) than the system memcpy.
184 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
185 * isn't much faster than x86_memcpy for agp copies.
187 * TODO: switch dynamically.
190 do_memcpy(void *dest
, const void *src
, size_t n
)
192 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
193 return __memcpy(dest
, src
, n
);
196 return memcpy(dest
, src
, n
);
201 * Return default texture usage bitmask for the given texture format.
204 default_usage(enum pipe_format fmt
)
206 GLuint usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
207 if (pf_is_depth_stencil(fmt
))
208 usage
|= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
210 usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
216 * Allocate a pipe_texture object for the given st_texture_object using
217 * the given st_texture_image to guess the mipmap size/levels.
220 * Otherwise, store it in memory if (Border != 0) or (any dimension ==
223 * Otherwise, if max_level >= level >= min_level, create texture with
224 * space for images from min_level down to max_level.
226 * Otherwise, create texture with space for images from (level 0)..(1x1).
227 * Consider pruning this texture at a validation if the saving is worth it.
230 guess_and_alloc_texture(struct st_context
*st
,
231 struct st_texture_object
*stObj
,
232 const struct st_texture_image
*stImage
)
236 GLuint width
= stImage
->base
.Width2
; /* size w/out border */
237 GLuint height
= stImage
->base
.Height2
;
238 GLuint depth
= stImage
->base
.Depth2
;
240 enum pipe_format fmt
;
242 DBG("%s\n", __FUNCTION__
);
247 (GLint
) stImage
->level
> stObj
->base
.BaseLevel
&&
248 (stImage
->base
.Width
== 1 ||
249 (stObj
->base
.Target
!= GL_TEXTURE_1D
&&
250 stImage
->base
.Height
== 1) ||
251 (stObj
->base
.Target
== GL_TEXTURE_3D
&&
252 stImage
->base
.Depth
== 1)))
255 /* If this image disrespects BaseLevel, allocate from level zero.
256 * Usually BaseLevel == 0, so it's unlikely to happen.
258 if ((GLint
) stImage
->level
< stObj
->base
.BaseLevel
)
261 firstLevel
= stObj
->base
.BaseLevel
;
264 /* Figure out image dimensions at start level.
266 for (i
= stImage
->level
; i
> firstLevel
; i
--) {
275 if (width
== 0 || height
== 0 || depth
== 0) {
276 /* no texture needed */
280 /* Guess a reasonable value for lastLevel. This is probably going
281 * to be wrong fairly often and might mean that we have to look at
282 * resizable buffers, or require that buffers implement lazy
283 * pagetable arrangements.
285 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
286 stObj
->base
.MinFilter
== GL_LINEAR
||
287 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
288 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
289 !stObj
->base
.GenerateMipmap
&&
290 stImage
->level
== firstLevel
) {
291 /* only alloc space for a single mipmap level */
292 lastLevel
= firstLevel
;
295 /* alloc space for a full mipmap */
296 GLuint l2width
= util_logbase2(width
);
297 GLuint l2height
= util_logbase2(height
);
298 GLuint l2depth
= util_logbase2(depth
);
299 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
302 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
304 usage
= default_usage(fmt
);
306 stObj
->pt
= st_texture_create(st
,
307 gl_target_to_pipe(stObj
->base
.Target
),
315 DBG("%s - success\n", __FUNCTION__
);
320 * Adjust pixel unpack params and image dimensions to strip off the
322 * Gallium doesn't support texture borders. They've seldem been used
323 * and seldom been implemented correctly anyway.
324 * \param unpackNew returns the new pixel unpack parameters
327 strip_texture_border(GLint border
,
328 GLint
*width
, GLint
*height
, GLint
*depth
,
329 const struct gl_pixelstore_attrib
*unpack
,
330 struct gl_pixelstore_attrib
*unpackNew
)
332 assert(border
> 0); /* sanity check */
334 *unpackNew
= *unpack
;
336 if (unpackNew
->RowLength
== 0)
337 unpackNew
->RowLength
= *width
;
339 if (depth
&& unpackNew
->ImageHeight
== 0)
340 unpackNew
->ImageHeight
= *height
;
342 unpackNew
->SkipPixels
+= border
;
344 unpackNew
->SkipRows
+= border
;
346 unpackNew
->SkipImages
+= border
;
349 *width
= *width
- 2 * border
;
350 if (height
&& *height
>= 3)
351 *height
= *height
- 2 * border
;
352 if (depth
&& *depth
>= 3)
353 *depth
= *depth
- 2 * border
;
358 * Try to do texture compression via rendering. If the Gallium driver
359 * can render into a compressed surface this will allow us to do texture
361 * \return GL_TRUE for success, GL_FALSE for failure
364 compress_with_blit(GLcontext
* ctx
,
365 GLenum target
, GLint level
,
366 GLint xoffset
, GLint yoffset
, GLint zoffset
,
367 GLint width
, GLint height
, GLint depth
,
368 GLenum format
, GLenum type
, const void *pixels
,
369 const struct gl_pixelstore_attrib
*unpack
,
370 struct gl_texture_image
*texImage
)
372 const GLuint dstImageOffsets
[1] = {0};
373 struct st_texture_image
*stImage
= st_texture_image(texImage
);
374 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
375 gl_format mesa_format
;
376 struct pipe_texture templ
;
377 struct pipe_texture
*src_tex
;
378 struct pipe_surface
*dst_surface
;
379 struct pipe_transfer
*tex_xfer
;
383 /* XXX: Can this happen? Should we assert? */
387 /* get destination surface (in the compressed texture) */
388 dst_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
389 stImage
->face
, stImage
->level
, 0,
390 PIPE_BUFFER_USAGE_GPU_WRITE
);
392 /* can't render into this format (or other problem) */
396 /* Choose format for the temporary RGBA texture image.
398 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
403 /* Create the temporary source texture
405 memset(&templ
, 0, sizeof(templ
));
406 templ
.target
= PIPE_TEXTURE_2D
;
407 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
);
408 templ
.width0
= width
;
409 templ
.height0
= height
;
411 templ
.last_level
= 0;
412 templ
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
413 src_tex
= screen
->texture_create(screen
, &templ
);
418 /* Put user's tex data into the temporary texture
420 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
), src_tex
,
421 0, 0, 0, /* face, level are zero */
423 0, 0, width
, height
); /* x, y, w, h */
424 map
= screen
->transfer_map(screen
, tex_xfer
);
426 _mesa_texstore(ctx
, 2, GL_RGBA
, mesa_format
,
428 0, 0, 0, /* dest x/y/z offset */
429 tex_xfer
->stride
, /* dest row stride (bytes) */
430 dstImageOffsets
, /* image offsets (for 3D only) */
431 width
, height
, 1, /* size */
432 format
, type
, /* source format/type */
433 pixels
, /* source data */
434 unpack
); /* source data packing */
436 screen
->transfer_unmap(screen
, tex_xfer
);
437 screen
->tex_transfer_destroy(tex_xfer
);
439 /* copy / compress image */
440 util_blit_pixels_tex(ctx
->st
->blit
,
441 src_tex
, /* pipe_texture (src) */
442 0, 0, /* src x0, y0 */
443 width
, height
, /* src x1, y1 */
444 dst_surface
, /* pipe_surface (dst) */
445 xoffset
, yoffset
, /* dst x0, y0 */
446 xoffset
+ width
, /* dst x1 */
447 yoffset
+ height
, /* dst y1 */
449 PIPE_TEX_MIPFILTER_NEAREST
);
451 pipe_surface_reference(&dst_surface
, NULL
);
452 pipe_texture_reference(&src_tex
, NULL
);
459 * Do glTexImage1/2/3D().
462 st_TexImage(GLcontext
* ctx
,
464 GLenum target
, GLint level
,
465 GLint internalFormat
,
466 GLint width
, GLint height
, GLint depth
,
468 GLenum format
, GLenum type
, const void *pixels
,
469 const struct gl_pixelstore_attrib
*unpack
,
470 struct gl_texture_object
*texObj
,
471 struct gl_texture_image
*texImage
,
472 GLsizei imageSize
, GLboolean compressed_src
)
474 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
475 struct st_texture_object
*stObj
= st_texture_object(texObj
);
476 struct st_texture_image
*stImage
= st_texture_image(texImage
);
477 GLint postConvWidth
, postConvHeight
;
478 GLint texelBytes
, sizeInBytes
;
479 GLuint dstRowStride
= 0;
480 struct gl_pixelstore_attrib unpackNB
;
481 enum pipe_transfer_usage transfer_usage
= 0;
483 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
484 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
486 /* switch to "normal" */
487 if (stObj
->surface_based
) {
488 _mesa_clear_texture_object(ctx
, texObj
);
489 stObj
->surface_based
= GL_FALSE
;
492 /* gallium does not support texture borders, strip it off */
494 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
496 texImage
->Width
= width
;
497 texImage
->Height
= height
;
498 texImage
->Depth
= depth
;
499 texImage
->Border
= 0;
503 postConvWidth
= width
;
504 postConvHeight
= height
;
506 stImage
->face
= _mesa_tex_target_to_face(target
);
507 stImage
->level
= level
;
510 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
511 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
516 _mesa_set_fetch_functions(texImage
, dims
);
518 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
519 /* must be a compressed format */
523 texelBytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
525 /* Minimum pitch of 32 bytes */
526 if (postConvWidth
* texelBytes
< 32) {
527 postConvWidth
= 32 / texelBytes
;
528 texImage
->RowStride
= postConvWidth
;
531 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
532 /*assert(texImage->RowStride == postConvWidth);*/
535 /* Release the reference to a potentially orphaned buffer.
536 * Release any old malloced memory.
539 pipe_texture_reference(&stImage
->pt
, NULL
);
540 assert(!texImage
->Data
);
542 else if (texImage
->Data
) {
543 _mesa_align_free(texImage
->Data
);
546 if (width
== 0 || height
== 0 || depth
== 0) {
547 /* stop after freeing old image */
551 /* If this is the only mipmap level in the texture, could call
552 * bmBufferData with NULL data to free the old block and avoid
553 * waiting on any outstanding fences.
556 if (stObj
->teximage_realloc
||
557 level
> (GLint
) stObj
->pt
->last_level
||
558 (stObj
->pt
->last_level
== level
&&
559 stObj
->pt
->target
!= PIPE_TEXTURE_CUBE
&&
560 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
561 stImage
->face
, stImage
->level
))) {
563 pipe_texture_reference(&stObj
->pt
, NULL
);
565 stObj
->teximage_realloc
= FALSE
;
570 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
572 /* Probably out of memory.
573 * Try flushing any pending rendering, then retry.
576 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
578 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
584 assert(!stImage
->pt
);
587 st_texture_match_image(stObj
->pt
, &stImage
->base
,
588 stImage
->face
, stImage
->level
)) {
590 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
595 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
597 /* st_CopyTexImage calls this function with pixels == NULL, with
598 * the expectation that the texture will be set up but nothing
599 * more will be done. This is where those calls return:
601 if (compressed_src
) {
602 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
604 "glCompressedTexImage");
607 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
609 pixels
, unpack
, "glTexImage");
612 /* Note: we can't check for pixels==NULL until after we've allocated
613 * memory for the texture.
616 /* See if we can do texture compression with a blit/render.
618 if (!compressed_src
&&
620 _mesa_is_format_compressed(texImage
->TexFormat
) &&
621 screen
->is_format_supported(screen
,
624 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
628 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
629 format
, type
, pixels
, unpack
, texImage
)) {
635 if (format
== GL_DEPTH_COMPONENT
&&
636 pf_is_depth_and_stencil(stImage
->pt
->format
))
637 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
639 transfer_usage
= PIPE_TRANSFER_WRITE
;
641 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
642 transfer_usage
, 0, 0,
644 stImage
->base
.Height
);
645 if(stImage
->transfer
)
646 dstRowStride
= stImage
->transfer
->stride
;
649 /* Allocate regular memory and store the image there temporarily. */
650 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
651 sizeInBytes
= _mesa_format_image_size(texImage
->TexFormat
,
655 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
659 dstRowStride
= postConvWidth
* texelBytes
;
660 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
663 texImage
->Data
= _mesa_align_malloc(sizeInBytes
, 16);
666 if (!texImage
->Data
) {
667 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
674 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
675 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
677 /* Copy data. Would like to know when it's ok for us to eg. use
678 * the blitter to copy. Or, use the hardware to do the format
679 * conversion and copy:
681 if (compressed_src
) {
682 memcpy(texImage
->Data
, pixels
, imageSize
);
685 const GLuint srcImageStride
=
686 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
688 const GLubyte
*src
= (const GLubyte
*) pixels
;
690 for (i
= 0; i
< depth
; i
++) {
691 if (!_mesa_texstore(ctx
, dims
,
692 texImage
->_BaseFormat
,
695 0, 0, 0, /* dstX/Y/Zoffset */
697 texImage
->ImageOffsets
,
699 format
, type
, src
, unpack
)) {
700 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
703 if (stImage
->pt
&& i
+ 1 < depth
) {
704 /* unmap this slice */
705 st_texture_image_unmap(ctx
->st
, stImage
);
706 /* map next slice of 3D texture */
707 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
708 transfer_usage
, 0, 0,
710 stImage
->base
.Height
);
711 src
+= srcImageStride
;
717 _mesa_unmap_teximage_pbo(ctx
, unpack
);
719 if (stImage
->pt
&& texImage
->Data
) {
720 st_texture_image_unmap(ctx
->st
, stImage
);
721 texImage
->Data
= NULL
;
727 st_TexImage3D(GLcontext
* ctx
,
728 GLenum target
, GLint level
,
729 GLint internalFormat
,
730 GLint width
, GLint height
, GLint depth
,
732 GLenum format
, GLenum type
, const void *pixels
,
733 const struct gl_pixelstore_attrib
*unpack
,
734 struct gl_texture_object
*texObj
,
735 struct gl_texture_image
*texImage
)
737 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
738 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
744 st_TexImage2D(GLcontext
* ctx
,
745 GLenum target
, GLint level
,
746 GLint internalFormat
,
747 GLint width
, GLint height
, GLint border
,
748 GLenum format
, GLenum type
, const void *pixels
,
749 const struct gl_pixelstore_attrib
*unpack
,
750 struct gl_texture_object
*texObj
,
751 struct gl_texture_image
*texImage
)
753 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
754 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
759 st_TexImage1D(GLcontext
* ctx
,
760 GLenum target
, GLint level
,
761 GLint internalFormat
,
762 GLint width
, GLint border
,
763 GLenum format
, GLenum type
, const void *pixels
,
764 const struct gl_pixelstore_attrib
*unpack
,
765 struct gl_texture_object
*texObj
,
766 struct gl_texture_image
*texImage
)
768 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
769 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
774 st_CompressedTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
775 GLint internalFormat
,
776 GLint width
, GLint height
, GLint border
,
777 GLsizei imageSize
, const GLvoid
*data
,
778 struct gl_texture_object
*texObj
,
779 struct gl_texture_image
*texImage
)
781 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
782 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
788 * glGetTexImage() helper: decompress a compressed texture by rendering
789 * a textured quad. Store the results in the user's buffer.
792 decompress_with_blit(GLcontext
* ctx
, GLenum target
, GLint level
,
793 GLenum format
, GLenum type
, GLvoid
*pixels
,
794 struct gl_texture_object
*texObj
,
795 struct gl_texture_image
*texImage
)
797 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
798 struct st_texture_image
*stImage
= st_texture_image(texImage
);
799 const GLuint width
= texImage
->Width
;
800 const GLuint height
= texImage
->Height
;
801 struct pipe_surface
*dst_surface
;
802 struct pipe_texture
*dst_texture
;
803 struct pipe_transfer
*tex_xfer
;
805 /* create temp / dest surface */
806 if (!util_create_rgba_surface(screen
, width
, height
,
807 &dst_texture
, &dst_surface
)) {
808 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
809 "in decompress_with_blit()");
813 /* blit/render/decompress */
814 util_blit_pixels_tex(ctx
->st
->blit
,
815 stImage
->pt
, /* pipe_texture (src) */
816 0, 0, /* src x0, y0 */
817 width
, height
, /* src x1, y1 */
818 dst_surface
, /* pipe_surface (dst) */
819 0, 0, /* dst x0, y0 */
820 width
, height
, /* dst x1, y1 */
822 PIPE_TEX_MIPFILTER_NEAREST
);
824 /* map the dst_surface so we can read from it */
825 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
),
826 dst_texture
, 0, 0, 0,
828 0, 0, width
, height
);
830 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
832 /* copy/pack data into user buffer */
833 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
835 const uint bytesPerRow
= width
* pf_get_blocksize(stImage
->pt
->format
);
836 ubyte
*map
= screen
->transfer_map(screen
, tex_xfer
);
838 for (row
= 0; row
< height
; row
++) {
839 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
840 height
, format
, type
, row
, 0);
841 memcpy(dest
, map
, bytesPerRow
);
842 map
+= tex_xfer
->stride
;
844 screen
->transfer_unmap(screen
, tex_xfer
);
847 /* format translation via floats */
849 for (row
= 0; row
< height
; row
++) {
850 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
851 GLfloat rgba
[4 * MAX_WIDTH
];
852 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
853 height
, format
, type
, row
, 0);
855 if (ST_DEBUG
& DEBUG_FALLBACK
)
856 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
858 /* get float[4] rgba row from surface */
859 pipe_get_tile_rgba(tex_xfer
, 0, row
, width
, 1, rgba
);
861 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
862 type
, dest
, &ctx
->Pack
, transferOps
);
866 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
868 /* destroy the temp / dest surface */
869 util_destroy_rgba_surface(dst_texture
, dst_surface
);
875 * Need to map texture image into memory before copying image data,
879 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
880 GLenum format
, GLenum type
, GLvoid
* pixels
,
881 struct gl_texture_object
*texObj
,
882 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
884 struct st_texture_image
*stImage
= st_texture_image(texImage
);
885 const GLuint dstImageStride
=
886 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
892 pf_is_compressed(stImage
->pt
->format
) &&
894 /* Need to decompress the texture.
895 * We'll do this by rendering a textured quad.
896 * Note that we only expect RGBA formats (no Z/depth formats).
898 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
905 /* Image is stored in hardware format in a buffer managed by the
906 * kernel. Need to explicitly map and unmap it.
908 unsigned face
= _mesa_tex_target_to_face(target
);
910 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
913 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
914 PIPE_TRANSFER_READ
, 0, 0,
916 stImage
->base
.Height
);
917 texImage
->RowStride
= stImage
->transfer
->stride
/ pf_get_blocksize(stImage
->pt
->format
);
920 /* Otherwise, the image should actually be stored in
921 * texImage->Data. This is pretty confusing for
922 * everybody, I'd much prefer to separate the two functions of
923 * texImage->Data - storage for texture images in main memory
924 * and access (ie mappings) of images. In other words, we'd
925 * create a new texImage->Map field and leave Data simply for
928 assert(texImage
->Data
);
931 depth
= texImage
->Depth
;
934 dest
= (GLubyte
*) pixels
;
936 for (i
= 0; i
< depth
; i
++) {
937 if (compressed_dst
) {
938 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
942 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
946 if (stImage
->pt
&& i
+ 1 < depth
) {
947 /* unmap this slice */
948 st_texture_image_unmap(ctx
->st
, stImage
);
949 /* map next slice of 3D texture */
950 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
951 PIPE_TRANSFER_READ
, 0, 0,
953 stImage
->base
.Height
);
954 dest
+= dstImageStride
;
958 texImage
->Depth
= depth
;
962 st_texture_image_unmap(ctx
->st
, stImage
);
963 texImage
->Data
= NULL
;
969 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
970 GLenum format
, GLenum type
, GLvoid
* pixels
,
971 struct gl_texture_object
*texObj
,
972 struct gl_texture_image
*texImage
)
974 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
980 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
982 struct gl_texture_object
*texObj
,
983 struct gl_texture_image
*texImage
)
985 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
992 st_TexSubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
993 GLint xoffset
, GLint yoffset
, GLint zoffset
,
994 GLint width
, GLint height
, GLint depth
,
995 GLenum format
, GLenum type
, const void *pixels
,
996 const struct gl_pixelstore_attrib
*packing
,
997 struct gl_texture_object
*texObj
,
998 struct gl_texture_image
*texImage
)
1000 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
1001 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1002 GLuint dstRowStride
;
1003 const GLuint srcImageStride
=
1004 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1007 /* init to silence warning only: */
1008 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1010 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1011 _mesa_lookup_enum_by_nr(target
),
1012 level
, xoffset
, yoffset
, width
, height
);
1015 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1016 type
, pixels
, packing
, "glTexSubImage2D");
1020 /* See if we can do texture compression with a blit/render.
1022 if (!ctx
->Mesa_DXTn
&&
1023 _mesa_is_format_compressed(texImage
->TexFormat
) &&
1024 screen
->is_format_supported(screen
,
1025 stImage
->pt
->format
,
1026 stImage
->pt
->target
,
1027 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
1028 if (compress_with_blit(ctx
, target
, level
,
1029 xoffset
, yoffset
, zoffset
,
1030 width
, height
, depth
,
1031 format
, type
, pixels
, packing
, texImage
)) {
1036 /* Map buffer if necessary. Need to lock to prevent other contexts
1037 * from uploading the buffer under us.
1040 unsigned face
= _mesa_tex_target_to_face(target
);
1042 if (format
== GL_DEPTH_COMPONENT
&&
1043 pf_is_depth_and_stencil(stImage
->pt
->format
))
1044 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1046 transfer_usage
= PIPE_TRANSFER_WRITE
;
1048 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1050 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
,
1056 if (!texImage
->Data
) {
1057 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1061 src
= (const GLubyte
*) pixels
;
1062 dstRowStride
= stImage
->transfer
->stride
;
1064 for (i
= 0; i
< depth
; i
++) {
1065 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1066 texImage
->TexFormat
,
1070 texImage
->ImageOffsets
,
1072 format
, type
, src
, packing
)) {
1073 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1076 if (stImage
->pt
&& i
+ 1 < depth
) {
1077 /* unmap this slice */
1078 st_texture_image_unmap(ctx
->st
, stImage
);
1079 /* map next slice of 3D texture */
1080 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
,
1085 src
+= srcImageStride
;
1090 _mesa_unmap_teximage_pbo(ctx
, packing
);
1093 st_texture_image_unmap(ctx
->st
, stImage
);
1094 texImage
->Data
= NULL
;
1101 st_TexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1102 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1103 GLsizei width
, GLsizei height
, GLsizei depth
,
1104 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1105 const struct gl_pixelstore_attrib
*packing
,
1106 struct gl_texture_object
*texObj
,
1107 struct gl_texture_image
*texImage
)
1109 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1110 width
, height
, depth
, format
, type
,
1111 pixels
, packing
, texObj
, texImage
);
1116 st_TexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1117 GLint xoffset
, GLint yoffset
,
1118 GLsizei width
, GLsizei height
,
1119 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1120 const struct gl_pixelstore_attrib
*packing
,
1121 struct gl_texture_object
*texObj
,
1122 struct gl_texture_image
*texImage
)
1124 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1125 width
, height
, 1, format
, type
,
1126 pixels
, packing
, texObj
, texImage
);
1131 st_TexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1132 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1133 const GLvoid
* pixels
,
1134 const struct gl_pixelstore_attrib
*packing
,
1135 struct gl_texture_object
*texObj
,
1136 struct gl_texture_image
*texImage
)
1138 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1139 format
, type
, pixels
, packing
, texObj
, texImage
);
1144 st_CompressedTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1145 GLint xoffset
, GLsizei width
,
1147 GLsizei imageSize
, const GLvoid
*data
,
1148 struct gl_texture_object
*texObj
,
1149 struct gl_texture_image
*texImage
)
1156 st_CompressedTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1157 GLint xoffset
, GLint yoffset
,
1158 GLsizei width
, GLint height
,
1160 GLsizei imageSize
, const GLvoid
*data
,
1161 struct gl_texture_object
*texObj
,
1162 struct gl_texture_image
*texImage
)
1164 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1168 enum pipe_format pformat
= stImage
->pt
->format
;
1171 unsigned face
= _mesa_tex_target_to_face(target
);
1173 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1174 PIPE_TRANSFER_WRITE
);
1175 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
1176 PIPE_TRANSFER_WRITE
,
1180 srcBlockStride
= pf_get_stride(pformat
, width
);
1181 dstBlockStride
= stImage
->transfer
->stride
;
1183 assert(stImage
->pt
);
1184 /* TODO find good values for block and strides */
1185 /* TODO also adjust texImage->data for yoffset/xoffset */
1189 if (!texImage
->Data
) {
1190 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1194 assert(xoffset
% pf_get_blockwidth(pformat
) == 0);
1195 assert(yoffset
% pf_get_blockheight(pformat
) == 0);
1196 assert(width
% pf_get_blockwidth(pformat
) == 0);
1197 assert(height
% pf_get_blockheight(pformat
) == 0);
1199 for (y
= 0; y
< height
; y
+= pf_get_blockheight(pformat
)) {
1200 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1201 const char *src
= (const char*)data
+ srcBlockStride
* pf_get_nblocksy(pformat
, y
);
1202 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* pf_get_nblocksy(pformat
, y
);
1203 memcpy(dst
, src
, pf_get_stride(pformat
, width
));
1207 st_texture_image_unmap(ctx
->st
, stImage
);
1208 texImage
->Data
= NULL
;
1214 st_CompressedTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1215 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1216 GLsizei width
, GLint height
, GLint depth
,
1218 GLsizei imageSize
, const GLvoid
*data
,
1219 struct gl_texture_object
*texObj
,
1220 struct gl_texture_image
*texImage
)
1228 * Do a CopyTexSubImage operation using a read transfer from the source,
1229 * a write transfer to the destination and get_tile()/put_tile() to access
1230 * the pixels/texels.
1232 * Note: srcY=0=TOP of renderbuffer
1235 fallback_copy_texsubimage(GLcontext
*ctx
, GLenum target
, GLint level
,
1236 struct st_renderbuffer
*strb
,
1237 struct st_texture_image
*stImage
,
1239 GLint destX
, GLint destY
, GLint destZ
,
1240 GLint srcX
, GLint srcY
,
1241 GLsizei width
, GLsizei height
)
1243 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1244 struct pipe_screen
*screen
= pipe
->screen
;
1245 struct pipe_transfer
*src_trans
;
1247 enum pipe_transfer_usage transfer_usage
;
1249 if (ST_DEBUG
& DEBUG_FALLBACK
)
1250 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1252 assert(width
<= MAX_WIDTH
);
1254 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1255 srcY
= strb
->Base
.Height
- srcY
- height
;
1258 src_trans
= st_cond_flush_get_tex_transfer( st_context(ctx
),
1265 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1266 baseFormat
== GL_DEPTH_STENCIL
) &&
1267 pf_is_depth_and_stencil(stImage
->pt
->format
))
1268 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1270 transfer_usage
= PIPE_TRANSFER_WRITE
;
1272 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, 0, 0,
1275 texDest
= st_texture_image_map(ctx
->st
, stImage
, 0, transfer_usage
,
1276 destX
, destY
, width
, height
);
1278 if (baseFormat
== GL_DEPTH_COMPONENT
||
1279 baseFormat
== GL_DEPTH_STENCIL
) {
1280 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1281 ctx
->Pixel
.DepthBias
!= 0.0F
);
1284 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1285 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1294 /* To avoid a large temp memory allocation, do copy row by row */
1295 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1296 uint data
[MAX_WIDTH
];
1297 pipe_get_tile_z(src_trans
, 0, srcY
, width
, 1, data
);
1299 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1301 pipe_put_tile_z(stImage
->transfer
, 0, row
, width
, 1, data
);
1307 (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
1309 if (tempSrc
&& texDest
) {
1310 const GLint dims
= 2;
1311 const GLint dstRowStride
= stImage
->transfer
->stride
;
1312 struct gl_texture_image
*texImage
= &stImage
->base
;
1313 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1315 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1316 unpack
.Invert
= GL_TRUE
;
1319 /* get float/RGBA image from framebuffer */
1320 /* XXX this usually involves a lot of int/float conversion.
1321 * try to avoid that someday.
1323 pipe_get_tile_rgba(src_trans
, 0, 0, width
, height
, tempSrc
);
1325 /* Store into texture memory.
1326 * Note that this does some special things such as pixel transfer
1327 * ops and format conversion. In particular, if the dest tex format
1328 * is actually RGBA but the user created the texture as GL_RGB we
1329 * need to fill-in/override the alpha channel with 1.0.
1331 _mesa_texstore(ctx
, dims
,
1332 texImage
->_BaseFormat
,
1333 texImage
->TexFormat
,
1337 texImage
->ImageOffsets
,
1339 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1343 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1347 _mesa_free(tempSrc
);
1350 st_texture_image_unmap(ctx
->st
, stImage
);
1351 screen
->tex_transfer_destroy(src_trans
);
1356 compatible_src_dst_formats(const struct gl_renderbuffer
*src
,
1357 const struct gl_texture_image
*dst
)
1359 const GLenum srcFormat
= _mesa_get_format_base_format(src
->Format
);
1360 const GLenum dstLogicalFormat
= _mesa_get_format_base_format(dst
->TexFormat
);
1362 if (srcFormat
== dstLogicalFormat
) {
1363 /* This is the same as matching_base_formats, which should
1364 * always pass, as it did previously.
1366 return TGSI_WRITEMASK_XYZW
;
1368 else if (srcFormat
== GL_RGBA
&&
1369 dstLogicalFormat
== GL_RGB
) {
1370 /* Add a single special case to cope with RGBA->RGB transfers,
1371 * setting A to 1.0 to cope with situations where the RGB
1372 * destination is actually stored as RGBA.
1374 return TGSI_WRITEMASK_XYZ
; /* A ==> 1.0 */
1377 if (ST_DEBUG
& DEBUG_FALLBACK
)
1378 debug_printf("%s failed for src %s, dst %s\n",
1380 _mesa_lookup_enum_by_nr(srcFormat
),
1381 _mesa_lookup_enum_by_nr(dstLogicalFormat
));
1392 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1393 * Note that the region to copy has already been clipped so we know we
1394 * won't read from outside the source renderbuffer's bounds.
1396 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1399 st_copy_texsubimage(GLcontext
*ctx
,
1400 GLenum target
, GLint level
,
1401 GLint destX
, GLint destY
, GLint destZ
,
1402 GLint srcX
, GLint srcY
,
1403 GLsizei width
, GLsizei height
)
1405 struct gl_texture_unit
*texUnit
=
1406 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1407 struct gl_texture_object
*texObj
=
1408 _mesa_select_tex_object(ctx
, texUnit
, target
);
1409 struct gl_texture_image
*texImage
=
1410 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1411 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1412 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1413 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1414 struct st_renderbuffer
*strb
;
1415 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1416 struct pipe_screen
*screen
= pipe
->screen
;
1417 enum pipe_format dest_format
, src_format
;
1418 GLboolean use_fallback
= GL_TRUE
;
1419 GLboolean matching_base_formats
;
1420 GLuint format_writemask
;
1421 struct pipe_surface
*dest_surface
= NULL
;
1422 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1424 /* any rendering in progress must flushed before we grab the fb image */
1425 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
1427 /* make sure finalize_textures has been called?
1429 if (0) st_validate_state(ctx
->st
);
1431 /* determine if copying depth or color data */
1432 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1433 texBaseFormat
== GL_DEPTH_STENCIL
) {
1434 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1437 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1438 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1441 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1442 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1470 if (width
< 0 || height
< 0)
1475 assert(strb
->surface
);
1476 assert(stImage
->pt
);
1478 src_format
= strb
->surface
->format
;
1479 dest_format
= stImage
->pt
->format
;
1482 * Determine if the src framebuffer and dest texture have the same
1483 * base format. We need this to detect a case such as the framebuffer
1484 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1485 * texture format stores RGBA we need to set A=1 (overriding the
1486 * framebuffer's alpha values). We can't do that with the blit or
1487 * textured-quad paths.
1489 matching_base_formats
=
1490 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1491 _mesa_get_format_base_format(texImage
->TexFormat
));
1492 format_writemask
= compatible_src_dst_formats(&strb
->Base
, texImage
);
1494 if (ctx
->_ImageTransferState
== 0x0) {
1496 if (pipe
->surface_copy
&&
1497 matching_base_formats
&&
1498 src_format
== dest_format
&&
1501 /* use surface_copy() / blit */
1503 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1504 stImage
->face
, stImage
->level
,
1506 PIPE_BUFFER_USAGE_GPU_WRITE
);
1508 /* for surface_copy(), y=0=top, always */
1509 pipe
->surface_copy(pipe
,
1518 use_fallback
= GL_FALSE
;
1520 else if (format_writemask
&&
1521 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1522 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1523 screen
->is_format_supported(screen
, src_format
,
1525 PIPE_TEXTURE_USAGE_SAMPLER
,
1527 screen
->is_format_supported(screen
, dest_format
,
1529 PIPE_TEXTURE_USAGE_RENDER_TARGET
,
1531 /* draw textured quad to do the copy */
1534 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1535 stImage
->face
, stImage
->level
,
1537 PIPE_BUFFER_USAGE_GPU_WRITE
);
1540 srcY1
= strb
->Base
.Height
- srcY
- height
;
1541 srcY0
= srcY1
+ height
;
1545 srcY1
= srcY0
+ height
;
1547 util_blit_pixels_writemask(ctx
->st
->blit
,
1550 srcX
+ width
, srcY1
,
1553 destX
+ width
, destY
+ height
,
1554 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1556 use_fallback
= GL_FALSE
;
1560 pipe_surface_reference(&dest_surface
, NULL
);
1564 /* software fallback */
1565 fallback_copy_texsubimage(ctx
, target
, level
,
1566 strb
, stImage
, texBaseFormat
,
1567 destX
, destY
, destZ
,
1568 srcX
, srcY
, width
, height
);
1575 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1576 GLenum internalFormat
,
1577 GLint x
, GLint y
, GLsizei width
, GLint border
)
1579 struct gl_texture_unit
*texUnit
=
1580 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1581 struct gl_texture_object
*texObj
=
1582 _mesa_select_tex_object(ctx
, texUnit
, target
);
1583 struct gl_texture_image
*texImage
=
1584 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1586 /* Setup or redefine the texture object, texture and texture
1587 * image. Don't populate yet.
1589 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1591 GL_RGBA
, CHAN_TYPE
, NULL
,
1592 &ctx
->DefaultPacking
, texObj
, texImage
);
1594 st_copy_texsubimage(ctx
, target
, level
,
1595 0, 0, 0, /* destX,Y,Z */
1596 x
, y
, width
, 1); /* src X, Y, size */
1601 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1602 GLenum internalFormat
,
1603 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1606 struct gl_texture_unit
*texUnit
=
1607 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1608 struct gl_texture_object
*texObj
=
1609 _mesa_select_tex_object(ctx
, texUnit
, target
);
1610 struct gl_texture_image
*texImage
=
1611 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1613 /* Setup or redefine the texture object, texture and texture
1614 * image. Don't populate yet.
1616 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1617 width
, height
, border
,
1618 GL_RGBA
, CHAN_TYPE
, NULL
,
1619 &ctx
->DefaultPacking
, texObj
, texImage
);
1621 st_copy_texsubimage(ctx
, target
, level
,
1622 0, 0, 0, /* destX,Y,Z */
1623 x
, y
, width
, height
); /* src X, Y, size */
1628 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1629 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1631 const GLint yoffset
= 0, zoffset
= 0;
1632 const GLsizei height
= 1;
1633 st_copy_texsubimage(ctx
, target
, level
,
1634 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1635 x
, y
, width
, height
); /* src X, Y, size */
1640 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1641 GLint xoffset
, GLint yoffset
,
1642 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1644 const GLint zoffset
= 0;
1645 st_copy_texsubimage(ctx
, target
, level
,
1646 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1647 x
, y
, width
, height
); /* src X, Y, size */
1652 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1653 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1654 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1656 st_copy_texsubimage(ctx
, target
, level
,
1657 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1658 x
, y
, width
, height
); /* src X, Y, size */
1663 copy_image_data_to_texture(struct st_context
*st
,
1664 struct st_texture_object
*stObj
,
1666 struct st_texture_image
*stImage
)
1669 /* Copy potentially with the blitter:
1671 st_texture_image_copy(st
->pipe
,
1672 stObj
->pt
, dstLevel
, /* dest texture, level */
1673 stImage
->pt
, /* src texture */
1677 pipe_texture_reference(&stImage
->pt
, NULL
);
1679 else if (stImage
->base
.Data
) {
1680 /* More straightforward upload.
1683 st_teximage_flush_before_map(st
, stObj
->pt
, stImage
->face
, dstLevel
,
1684 PIPE_TRANSFER_WRITE
);
1687 st_texture_image_data(st
,
1692 stImage
->base
.RowStride
*
1693 pf_get_blocksize(stObj
->pt
->format
),
1694 stImage
->base
.RowStride
*
1695 stImage
->base
.Height
*
1696 pf_get_blocksize(stObj
->pt
->format
));
1697 _mesa_align_free(stImage
->base
.Data
);
1698 stImage
->base
.Data
= NULL
;
1701 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
1706 * Called during state validation. When this function is finished,
1707 * the texture object should be ready for rendering.
1708 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1711 st_finalize_texture(GLcontext
*ctx
,
1712 struct pipe_context
*pipe
,
1713 struct gl_texture_object
*tObj
,
1714 GLboolean
*needFlush
)
1716 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1717 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1718 GLuint blockSize
, face
;
1719 struct st_texture_image
*firstImage
;
1721 *needFlush
= GL_FALSE
;
1723 if (stObj
->base
._Complete
) {
1724 /* The texture is complete and we know exactly how many mipmap levels
1725 * are present/needed. This is conditional because we may be called
1726 * from the st_generate_mipmap() function when the texture object is
1727 * incomplete. In that case, we'll have set stObj->lastLevel before
1730 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1731 stObj
->base
.MinFilter
== GL_NEAREST
)
1732 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1734 stObj
->lastLevel
= stObj
->base
._MaxLevel
- stObj
->base
.BaseLevel
;
1737 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1739 /* If both firstImage and stObj point to a texture which can contain
1740 * all active images, favour firstImage. Note that because of the
1741 * completeness requirement, we know that the image dimensions
1744 if (firstImage
->pt
&&
1745 firstImage
->pt
!= stObj
->pt
&&
1746 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1747 pipe_texture_reference(&stObj
->pt
, firstImage
->pt
);
1750 /* bytes per pixel block (blocks are usually 1x1) */
1751 blockSize
= _mesa_get_format_bytes(firstImage
->base
.TexFormat
);
1753 /* If we already have a gallium texture, check that it matches the texture
1754 * object's format, target, size, num_levels, etc.
1757 const enum pipe_format fmt
=
1758 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1759 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1760 stObj
->pt
->format
!= fmt
||
1761 stObj
->pt
->last_level
< stObj
->lastLevel
||
1762 stObj
->pt
->width0
!= firstImage
->base
.Width2
||
1763 stObj
->pt
->height0
!= firstImage
->base
.Height2
||
1764 stObj
->pt
->depth0
!= firstImage
->base
.Depth2
)
1766 pipe_texture_reference(&stObj
->pt
, NULL
);
1767 ctx
->st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1771 /* May need to create a new gallium texture:
1774 const enum pipe_format fmt
=
1775 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1776 GLuint usage
= default_usage(fmt
);
1778 stObj
->pt
= st_texture_create(ctx
->st
,
1779 gl_target_to_pipe(stObj
->base
.Target
),
1782 firstImage
->base
.Width2
,
1783 firstImage
->base
.Height2
,
1784 firstImage
->base
.Depth2
,
1788 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1793 /* Pull in any images not in the object's texture:
1795 for (face
= 0; face
< nr_faces
; face
++) {
1797 for (level
= 0; level
<= stObj
->lastLevel
; level
++) {
1798 struct st_texture_image
*stImage
=
1799 st_texture_image(stObj
->base
.Image
[face
][stObj
->base
.BaseLevel
+ level
]);
1801 /* Need to import images in main memory or held in other textures.
1803 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1804 copy_image_data_to_texture(ctx
->st
, stObj
, level
, stImage
);
1805 *needFlush
= GL_TRUE
;
1815 * Returns pointer to a default/dummy texture.
1816 * This is typically used when the current shader has tex/sample instructions
1817 * but the user has not provided a (any) texture(s).
1819 struct gl_texture_object
*
1820 st_get_default_texture(struct st_context
*st
)
1822 if (!st
->default_texture
) {
1823 static const GLenum target
= GL_TEXTURE_2D
;
1824 GLubyte pixels
[16][16][4];
1825 struct gl_texture_object
*texObj
;
1826 struct gl_texture_image
*texImg
;
1829 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1830 * when attempting to sample incomplete textures.
1832 for (i
= 0; i
< 16; i
++) {
1833 for (j
= 0; j
< 16; j
++) {
1834 pixels
[i
][j
][0] = 0;
1835 pixels
[i
][j
][1] = 0;
1836 pixels
[i
][j
][2] = 0;
1837 pixels
[i
][j
][3] = 255;
1841 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1843 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1845 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1846 16, 16, 1, 0, /* w, h, d, border */
1849 st_TexImage(st
->ctx
, 2, target
,
1850 0, GL_RGBA
, /* level, intformat */
1851 16, 16, 1, 0, /* w, h, d, border */
1852 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1853 &st
->ctx
->DefaultPacking
,
1857 texObj
->MinFilter
= GL_NEAREST
;
1858 texObj
->MagFilter
= GL_NEAREST
;
1859 texObj
->_Complete
= GL_TRUE
;
1861 st
->default_texture
= texObj
;
1863 return st
->default_texture
;
1868 st_init_texture_functions(struct dd_function_table
*functions
)
1870 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1871 functions
->TexImage1D
= st_TexImage1D
;
1872 functions
->TexImage2D
= st_TexImage2D
;
1873 functions
->TexImage3D
= st_TexImage3D
;
1874 functions
->TexSubImage1D
= st_TexSubImage1D
;
1875 functions
->TexSubImage2D
= st_TexSubImage2D
;
1876 functions
->TexSubImage3D
= st_TexSubImage3D
;
1877 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1878 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1879 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1880 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1881 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1882 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1883 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1884 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1885 functions
->GenerateMipmap
= st_generate_mipmap
;
1887 functions
->GetTexImage
= st_GetTexImage
;
1889 /* compressed texture functions */
1890 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1891 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1893 functions
->NewTextureObject
= st_NewTextureObject
;
1894 functions
->NewTextureImage
= st_NewTextureImage
;
1895 functions
->DeleteTexture
= st_DeleteTextureObject
;
1896 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1897 functions
->UpdateTexturePalette
= 0;
1899 functions
->TextureMemCpy
= do_memcpy
;
1901 /* XXX Temporary until we can query pipe's texture sizes */
1902 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;