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/fbobject.h"
35 #include "main/formats.h"
36 #include "main/image.h"
37 #include "main/imports.h"
38 #include "main/macros.h"
39 #include "main/mipmap.h"
40 #include "main/texcompress.h"
41 #include "main/texfetch.h"
42 #include "main/texgetimage.h"
43 #include "main/teximage.h"
44 #include "main/texobj.h"
45 #include "main/texstore.h"
47 #include "state_tracker/st_debug.h"
48 #include "state_tracker/st_context.h"
49 #include "state_tracker/st_cb_fbo.h"
50 #include "state_tracker/st_cb_texture.h"
51 #include "state_tracker/st_format.h"
52 #include "state_tracker/st_public.h"
53 #include "state_tracker/st_texture.h"
54 #include "state_tracker/st_gen_mipmap.h"
55 #include "state_tracker/st_inlines.h"
56 #include "state_tracker/st_atom.h"
58 #include "pipe/p_context.h"
59 #include "pipe/p_defines.h"
60 #include "util/u_inlines.h"
61 #include "pipe/p_shader_tokens.h"
62 #include "util/u_tile.h"
63 #include "util/u_blit.h"
64 #include "util/u_format.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
);
126 if (stObj
->sampler_view
)
127 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
129 _mesa_delete_texture_object(ctx
, texObj
);
133 /** called via ctx->Driver.FreeTexImageData() */
135 st_FreeTextureImageData(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
137 struct st_texture_image
*stImage
= st_texture_image(texImage
);
139 DBG("%s\n", __FUNCTION__
);
142 pipe_texture_reference(&stImage
->pt
, NULL
);
145 if (texImage
->Data
) {
146 _mesa_align_free(texImage
->Data
);
147 texImage
->Data
= NULL
;
153 * From linux kernel i386 header files, copes with odd sizes better
154 * than COPY_DWORDS would:
155 * XXX Put this in src/mesa/main/imports.h ???
157 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
159 __memcpy(void *to
, const void *from
, size_t n
)
162 __asm__
__volatile__("rep ; movsl\n\t"
166 "1:\ttestb $1,%b4\n\t"
168 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
169 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
174 #define __memcpy(a,b,c) memcpy(a,b,c)
179 * The system memcpy (at least on ubuntu 5.10) has problems copying
180 * to agp (writecombined) memory from a source which isn't 64-byte
181 * aligned - there is a 4x performance falloff.
183 * The x86 __memcpy is immune to this but is slightly slower
184 * (10%-ish) than the system memcpy.
186 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
187 * isn't much faster than x86_memcpy for agp copies.
189 * TODO: switch dynamically.
192 do_memcpy(void *dest
, const void *src
, size_t n
)
194 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
195 return __memcpy(dest
, src
, n
);
198 return memcpy(dest
, src
, n
);
203 * Return default texture usage bitmask for the given texture format.
206 default_usage(enum pipe_format fmt
)
208 GLuint usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
209 if (util_format_is_depth_or_stencil(fmt
))
210 usage
|= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
212 usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
218 * Allocate a pipe_texture object for the given st_texture_object using
219 * the given st_texture_image to guess the mipmap size/levels.
222 * Otherwise, store it in memory if (Border != 0) or (any dimension ==
225 * Otherwise, if max_level >= level >= min_level, create texture with
226 * space for images from min_level down to max_level.
228 * Otherwise, create texture with space for images from (level 0)..(1x1).
229 * Consider pruning this texture at a validation if the saving is worth it.
232 guess_and_alloc_texture(struct st_context
*st
,
233 struct st_texture_object
*stObj
,
234 const struct st_texture_image
*stImage
)
238 GLuint width
= stImage
->base
.Width2
; /* size w/out border */
239 GLuint height
= stImage
->base
.Height2
;
240 GLuint depth
= stImage
->base
.Depth2
;
242 enum pipe_format fmt
;
244 DBG("%s\n", __FUNCTION__
);
249 (GLint
) stImage
->level
> stObj
->base
.BaseLevel
&&
250 (stImage
->base
.Width
== 1 ||
251 (stObj
->base
.Target
!= GL_TEXTURE_1D
&&
252 stImage
->base
.Height
== 1) ||
253 (stObj
->base
.Target
== GL_TEXTURE_3D
&&
254 stImage
->base
.Depth
== 1)))
257 /* If this image disrespects BaseLevel, allocate from level zero.
258 * Usually BaseLevel == 0, so it's unlikely to happen.
260 if ((GLint
) stImage
->level
< stObj
->base
.BaseLevel
)
263 firstLevel
= stObj
->base
.BaseLevel
;
266 /* Figure out image dimensions at start level.
268 for (i
= stImage
->level
; i
> firstLevel
; i
--) {
277 if (width
== 0 || height
== 0 || depth
== 0) {
278 /* no texture needed */
282 /* Guess a reasonable value for lastLevel. This is probably going
283 * to be wrong fairly often and might mean that we have to look at
284 * resizable buffers, or require that buffers implement lazy
285 * pagetable arrangements.
287 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
288 stObj
->base
.MinFilter
== GL_LINEAR
||
289 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
290 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
291 !stObj
->base
.GenerateMipmap
&&
292 stImage
->level
== firstLevel
) {
293 /* only alloc space for a single mipmap level */
294 lastLevel
= firstLevel
;
297 /* alloc space for a full mipmap */
298 GLuint l2width
= util_logbase2(width
);
299 GLuint l2height
= util_logbase2(height
);
300 GLuint l2depth
= util_logbase2(depth
);
301 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
304 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
306 usage
= default_usage(fmt
);
308 stObj
->pt
= st_texture_create(st
,
309 gl_target_to_pipe(stObj
->base
.Target
),
317 stObj
->pipe
= st
->pipe
;
319 DBG("%s - success\n", __FUNCTION__
);
324 * Adjust pixel unpack params and image dimensions to strip off the
326 * Gallium doesn't support texture borders. They've seldem been used
327 * and seldom been implemented correctly anyway.
328 * \param unpackNew returns the new pixel unpack parameters
331 strip_texture_border(GLint border
,
332 GLint
*width
, GLint
*height
, GLint
*depth
,
333 const struct gl_pixelstore_attrib
*unpack
,
334 struct gl_pixelstore_attrib
*unpackNew
)
336 assert(border
> 0); /* sanity check */
338 *unpackNew
= *unpack
;
340 if (unpackNew
->RowLength
== 0)
341 unpackNew
->RowLength
= *width
;
343 if (depth
&& unpackNew
->ImageHeight
== 0)
344 unpackNew
->ImageHeight
= *height
;
346 unpackNew
->SkipPixels
+= border
;
348 unpackNew
->SkipRows
+= border
;
350 unpackNew
->SkipImages
+= border
;
353 *width
= *width
- 2 * border
;
354 if (height
&& *height
>= 3)
355 *height
= *height
- 2 * border
;
356 if (depth
&& *depth
>= 3)
357 *depth
= *depth
- 2 * border
;
362 * Try to do texture compression via rendering. If the Gallium driver
363 * can render into a compressed surface this will allow us to do texture
365 * \return GL_TRUE for success, GL_FALSE for failure
368 compress_with_blit(GLcontext
* ctx
,
369 GLenum target
, GLint level
,
370 GLint xoffset
, GLint yoffset
, GLint zoffset
,
371 GLint width
, GLint height
, GLint depth
,
372 GLenum format
, GLenum type
, const void *pixels
,
373 const struct gl_pixelstore_attrib
*unpack
,
374 struct gl_texture_image
*texImage
)
376 const GLuint dstImageOffsets
[1] = {0};
377 struct st_texture_image
*stImage
= st_texture_image(texImage
);
378 struct pipe_context
*pipe
= ctx
->st
->pipe
;
379 struct pipe_screen
*screen
= pipe
->screen
;
380 gl_format mesa_format
;
381 struct pipe_texture templ
;
382 struct pipe_texture
*src_tex
;
383 struct pipe_surface
*dst_surface
;
384 struct pipe_transfer
*tex_xfer
;
388 /* XXX: Can this happen? Should we assert? */
392 /* get destination surface (in the compressed texture) */
393 dst_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
394 stImage
->face
, stImage
->level
, 0,
395 PIPE_BUFFER_USAGE_GPU_WRITE
);
397 /* can't render into this format (or other problem) */
401 /* Choose format for the temporary RGBA texture image.
403 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
408 /* Create the temporary source texture
410 memset(&templ
, 0, sizeof(templ
));
411 templ
.target
= PIPE_TEXTURE_2D
;
412 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
);
413 templ
.width0
= width
;
414 templ
.height0
= height
;
416 templ
.last_level
= 0;
417 templ
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
418 src_tex
= screen
->texture_create(screen
, &templ
);
423 /* Put user's tex data into the temporary texture
425 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
), src_tex
,
426 0, 0, 0, /* face, level are zero */
428 0, 0, width
, height
); /* x, y, w, h */
429 map
= pipe
->transfer_map(pipe
, tex_xfer
);
431 _mesa_texstore(ctx
, 2, GL_RGBA
, mesa_format
,
433 0, 0, 0, /* dest x/y/z offset */
434 tex_xfer
->stride
, /* dest row stride (bytes) */
435 dstImageOffsets
, /* image offsets (for 3D only) */
436 width
, height
, 1, /* size */
437 format
, type
, /* source format/type */
438 pixels
, /* source data */
439 unpack
); /* source data packing */
441 pipe
->transfer_unmap(pipe
, tex_xfer
);
442 pipe
->tex_transfer_destroy(pipe
, tex_xfer
);
444 /* copy / compress image */
445 util_blit_pixels_tex(ctx
->st
->blit
,
446 src_tex
, /* pipe_texture (src) */
447 0, 0, /* src x0, y0 */
448 width
, height
, /* src x1, y1 */
449 dst_surface
, /* pipe_surface (dst) */
450 xoffset
, yoffset
, /* dst x0, y0 */
451 xoffset
+ width
, /* dst x1 */
452 yoffset
+ height
, /* dst y1 */
454 PIPE_TEX_MIPFILTER_NEAREST
);
456 pipe_surface_reference(&dst_surface
, NULL
);
457 pipe_texture_reference(&src_tex
, NULL
);
464 * Do glTexImage1/2/3D().
467 st_TexImage(GLcontext
* ctx
,
469 GLenum target
, GLint level
,
470 GLint internalFormat
,
471 GLint width
, GLint height
, GLint depth
,
473 GLenum format
, GLenum type
, const void *pixels
,
474 const struct gl_pixelstore_attrib
*unpack
,
475 struct gl_texture_object
*texObj
,
476 struct gl_texture_image
*texImage
,
477 GLsizei imageSize
, GLboolean compressed_src
)
479 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
480 struct st_texture_object
*stObj
= st_texture_object(texObj
);
481 struct st_texture_image
*stImage
= st_texture_image(texImage
);
482 GLint postConvWidth
, postConvHeight
;
483 GLint texelBytes
, sizeInBytes
;
484 GLuint dstRowStride
= 0;
485 struct gl_pixelstore_attrib unpackNB
;
486 enum pipe_transfer_usage transfer_usage
= 0;
488 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
489 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
491 /* switch to "normal" */
492 if (stObj
->surface_based
) {
493 _mesa_clear_texture_object(ctx
, texObj
);
494 stObj
->surface_based
= GL_FALSE
;
497 /* gallium does not support texture borders, strip it off */
499 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
501 texImage
->Width
= width
;
502 texImage
->Height
= height
;
503 texImage
->Depth
= depth
;
504 texImage
->Border
= 0;
508 postConvWidth
= width
;
509 postConvHeight
= height
;
511 stImage
->face
= _mesa_tex_target_to_face(target
);
512 stImage
->level
= level
;
515 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
516 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
521 _mesa_set_fetch_functions(texImage
, dims
);
523 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
524 /* must be a compressed format */
528 texelBytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
530 /* Minimum pitch of 32 bytes */
531 if (postConvWidth
* texelBytes
< 32) {
532 postConvWidth
= 32 / texelBytes
;
533 texImage
->RowStride
= postConvWidth
;
536 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
537 /*assert(texImage->RowStride == postConvWidth);*/
540 /* Release the reference to a potentially orphaned buffer.
541 * Release any old malloced memory.
544 pipe_texture_reference(&stImage
->pt
, NULL
);
545 assert(!texImage
->Data
);
547 else if (texImage
->Data
) {
548 _mesa_align_free(texImage
->Data
);
552 * See if the new image is somehow incompatible with the existing
553 * mipmap. If so, free the old mipmap.
556 if (stObj
->teximage_realloc
||
557 level
> (GLint
) stObj
->pt
->last_level
||
558 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
559 stImage
->face
, stImage
->level
)) {
561 pipe_texture_reference(&stObj
->pt
, NULL
);
563 stObj
->teximage_realloc
= FALSE
;
567 if (width
== 0 || height
== 0 || depth
== 0) {
568 /* stop after freeing old image */
573 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
575 /* Probably out of memory.
576 * Try flushing any pending rendering, then retry.
579 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
581 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
587 assert(!stImage
->pt
);
590 st_texture_match_image(stObj
->pt
, &stImage
->base
,
591 stImage
->face
, stImage
->level
)) {
593 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
598 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
600 /* st_CopyTexImage calls this function with pixels == NULL, with
601 * the expectation that the texture will be set up but nothing
602 * more will be done. This is where those calls return:
604 if (compressed_src
) {
605 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
607 "glCompressedTexImage");
610 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
612 pixels
, unpack
, "glTexImage");
615 /* Note: we can't check for pixels==NULL until after we've allocated
616 * memory for the texture.
619 /* See if we can do texture compression with a blit/render.
621 if (!compressed_src
&&
623 _mesa_is_format_compressed(texImage
->TexFormat
) &&
624 screen
->is_format_supported(screen
,
627 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
631 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
632 format
, type
, pixels
, unpack
, texImage
)) {
638 if (format
== GL_DEPTH_COMPONENT
&&
639 util_format_is_depth_and_stencil(stImage
->pt
->format
))
640 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
642 transfer_usage
= PIPE_TRANSFER_WRITE
;
644 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
645 transfer_usage
, 0, 0,
647 stImage
->base
.Height
);
648 if(stImage
->transfer
)
649 dstRowStride
= stImage
->transfer
->stride
;
652 /* Allocate regular memory and store the image there temporarily. */
653 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
654 sizeInBytes
= _mesa_format_image_size(texImage
->TexFormat
,
658 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
662 dstRowStride
= postConvWidth
* texelBytes
;
663 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
666 texImage
->Data
= _mesa_align_malloc(sizeInBytes
, 16);
669 if (!texImage
->Data
) {
670 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
677 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
678 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
680 /* Copy data. Would like to know when it's ok for us to eg. use
681 * the blitter to copy. Or, use the hardware to do the format
682 * conversion and copy:
684 if (compressed_src
) {
685 const GLuint srcImageStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
686 if(dstRowStride
== srcImageStride
)
687 memcpy(texImage
->Data
, pixels
, imageSize
);
690 char *dst
= texImage
->Data
;
691 const char *src
= pixels
;
692 GLuint i
, bw
, bh
, lines
;
693 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
694 lines
= (height
+ bh
- 1) / bh
;
696 for(i
= 0; i
< lines
; ++i
)
698 memcpy(dst
, src
, srcImageStride
);
700 src
+= srcImageStride
;
705 const GLuint srcImageStride
=
706 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
708 const GLubyte
*src
= (const GLubyte
*) pixels
;
710 for (i
= 0; i
< depth
; i
++) {
711 if (!_mesa_texstore(ctx
, dims
,
712 texImage
->_BaseFormat
,
715 0, 0, 0, /* dstX/Y/Zoffset */
717 texImage
->ImageOffsets
,
719 format
, type
, src
, unpack
)) {
720 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
723 if (stImage
->pt
&& i
+ 1 < depth
) {
724 /* unmap this slice */
725 st_texture_image_unmap(ctx
->st
, stImage
);
726 /* map next slice of 3D texture */
727 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
728 transfer_usage
, 0, 0,
730 stImage
->base
.Height
);
731 src
+= srcImageStride
;
737 _mesa_unmap_teximage_pbo(ctx
, unpack
);
739 if (stImage
->pt
&& texImage
->Data
) {
740 st_texture_image_unmap(ctx
->st
, stImage
);
741 texImage
->Data
= NULL
;
747 st_TexImage3D(GLcontext
* ctx
,
748 GLenum target
, GLint level
,
749 GLint internalFormat
,
750 GLint width
, GLint height
, GLint depth
,
752 GLenum format
, GLenum type
, const void *pixels
,
753 const struct gl_pixelstore_attrib
*unpack
,
754 struct gl_texture_object
*texObj
,
755 struct gl_texture_image
*texImage
)
757 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
758 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
764 st_TexImage2D(GLcontext
* ctx
,
765 GLenum target
, GLint level
,
766 GLint internalFormat
,
767 GLint width
, GLint height
, GLint border
,
768 GLenum format
, GLenum type
, const void *pixels
,
769 const struct gl_pixelstore_attrib
*unpack
,
770 struct gl_texture_object
*texObj
,
771 struct gl_texture_image
*texImage
)
773 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
774 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
779 st_TexImage1D(GLcontext
* ctx
,
780 GLenum target
, GLint level
,
781 GLint internalFormat
,
782 GLint width
, GLint border
,
783 GLenum format
, GLenum type
, const void *pixels
,
784 const struct gl_pixelstore_attrib
*unpack
,
785 struct gl_texture_object
*texObj
,
786 struct gl_texture_image
*texImage
)
788 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
789 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
794 st_CompressedTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
795 GLint internalFormat
,
796 GLint width
, GLint height
, GLint border
,
797 GLsizei imageSize
, const GLvoid
*data
,
798 struct gl_texture_object
*texObj
,
799 struct gl_texture_image
*texImage
)
801 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
802 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
808 * glGetTexImage() helper: decompress a compressed texture by rendering
809 * a textured quad. Store the results in the user's buffer.
812 decompress_with_blit(GLcontext
* ctx
, GLenum target
, GLint level
,
813 GLenum format
, GLenum type
, GLvoid
*pixels
,
814 struct gl_texture_object
*texObj
,
815 struct gl_texture_image
*texImage
)
817 struct pipe_context
*pipe
= ctx
->st
->pipe
;
818 struct pipe_screen
*screen
= pipe
->screen
;
819 struct st_texture_image
*stImage
= st_texture_image(texImage
);
820 const GLuint width
= texImage
->Width
;
821 const GLuint height
= texImage
->Height
;
822 struct pipe_surface
*dst_surface
;
823 struct pipe_texture
*dst_texture
;
824 struct pipe_transfer
*tex_xfer
;
826 /* create temp / dest surface */
827 if (!util_create_rgba_surface(screen
, width
, height
,
828 &dst_texture
, &dst_surface
)) {
829 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
830 "in decompress_with_blit()");
834 /* blit/render/decompress */
835 util_blit_pixels_tex(ctx
->st
->blit
,
836 stImage
->pt
, /* pipe_texture (src) */
837 0, 0, /* src x0, y0 */
838 width
, height
, /* src x1, y1 */
839 dst_surface
, /* pipe_surface (dst) */
840 0, 0, /* dst x0, y0 */
841 width
, height
, /* dst x1, y1 */
843 PIPE_TEX_MIPFILTER_NEAREST
);
845 /* map the dst_surface so we can read from it */
846 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
),
847 dst_texture
, 0, 0, 0,
849 0, 0, width
, height
);
851 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
853 /* copy/pack data into user buffer */
854 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
856 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
857 ubyte
*map
= pipe
->transfer_map(pipe
, tex_xfer
);
859 for (row
= 0; row
< height
; row
++) {
860 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
861 height
, format
, type
, row
, 0);
862 memcpy(dest
, map
, bytesPerRow
);
863 map
+= tex_xfer
->stride
;
865 pipe
->transfer_unmap(pipe
, tex_xfer
);
868 /* format translation via floats */
870 for (row
= 0; row
< height
; row
++) {
871 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
872 GLfloat rgba
[4 * MAX_WIDTH
];
873 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
874 height
, format
, type
, row
, 0);
876 if (ST_DEBUG
& DEBUG_FALLBACK
)
877 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
879 /* get float[4] rgba row from surface */
880 pipe_get_tile_rgba(pipe
, tex_xfer
, 0, row
, width
, 1, rgba
);
882 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
883 type
, dest
, &ctx
->Pack
, transferOps
);
887 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
889 /* destroy the temp / dest surface */
890 util_destroy_rgba_surface(dst_texture
, dst_surface
);
896 * Need to map texture image into memory before copying image data,
900 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
901 GLenum format
, GLenum type
, GLvoid
* pixels
,
902 struct gl_texture_object
*texObj
,
903 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
905 struct st_texture_image
*stImage
= st_texture_image(texImage
);
906 const GLuint dstImageStride
=
907 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
913 util_format_is_compressed(stImage
->pt
->format
) &&
915 /* Need to decompress the texture.
916 * We'll do this by rendering a textured quad.
917 * Note that we only expect RGBA formats (no Z/depth formats).
919 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
926 /* Image is stored in hardware format in a buffer managed by the
927 * kernel. Need to explicitly map and unmap it.
929 unsigned face
= _mesa_tex_target_to_face(target
);
931 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
934 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
935 PIPE_TRANSFER_READ
, 0, 0,
937 stImage
->base
.Height
);
938 texImage
->RowStride
= stImage
->transfer
->stride
/ util_format_get_blocksize(stImage
->pt
->format
);
941 /* Otherwise, the image should actually be stored in
942 * texImage->Data. This is pretty confusing for
943 * everybody, I'd much prefer to separate the two functions of
944 * texImage->Data - storage for texture images in main memory
945 * and access (ie mappings) of images. In other words, we'd
946 * create a new texImage->Map field and leave Data simply for
949 assert(texImage
->Data
);
952 depth
= texImage
->Depth
;
955 dest
= (GLubyte
*) pixels
;
957 for (i
= 0; i
< depth
; i
++) {
958 if (compressed_dst
) {
959 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
963 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
967 if (stImage
->pt
&& i
+ 1 < depth
) {
968 /* unmap this slice */
969 st_texture_image_unmap(ctx
->st
, stImage
);
970 /* map next slice of 3D texture */
971 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
972 PIPE_TRANSFER_READ
, 0, 0,
974 stImage
->base
.Height
);
975 dest
+= dstImageStride
;
979 texImage
->Depth
= depth
;
983 st_texture_image_unmap(ctx
->st
, stImage
);
984 texImage
->Data
= NULL
;
990 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
991 GLenum format
, GLenum type
, GLvoid
* pixels
,
992 struct gl_texture_object
*texObj
,
993 struct gl_texture_image
*texImage
)
995 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1001 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
1003 struct gl_texture_object
*texObj
,
1004 struct gl_texture_image
*texImage
)
1006 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1013 st_TexSubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
1014 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1015 GLint width
, GLint height
, GLint depth
,
1016 GLenum format
, GLenum type
, const void *pixels
,
1017 const struct gl_pixelstore_attrib
*packing
,
1018 struct gl_texture_object
*texObj
,
1019 struct gl_texture_image
*texImage
)
1021 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
1022 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1023 GLuint dstRowStride
;
1024 const GLuint srcImageStride
=
1025 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1028 /* init to silence warning only: */
1029 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1031 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1032 _mesa_lookup_enum_by_nr(target
),
1033 level
, xoffset
, yoffset
, width
, height
);
1036 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1037 type
, pixels
, packing
, "glTexSubImage2D");
1041 /* See if we can do texture compression with a blit/render.
1043 if (!ctx
->Mesa_DXTn
&&
1044 _mesa_is_format_compressed(texImage
->TexFormat
) &&
1045 screen
->is_format_supported(screen
,
1046 stImage
->pt
->format
,
1047 stImage
->pt
->target
,
1048 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
1049 if (compress_with_blit(ctx
, target
, level
,
1050 xoffset
, yoffset
, zoffset
,
1051 width
, height
, depth
,
1052 format
, type
, pixels
, packing
, texImage
)) {
1057 /* Map buffer if necessary. Need to lock to prevent other contexts
1058 * from uploading the buffer under us.
1061 unsigned face
= _mesa_tex_target_to_face(target
);
1063 if (format
== GL_DEPTH_COMPONENT
&&
1064 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1065 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1067 transfer_usage
= PIPE_TRANSFER_WRITE
;
1069 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1071 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
,
1077 if (!texImage
->Data
) {
1078 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1082 src
= (const GLubyte
*) pixels
;
1083 dstRowStride
= stImage
->transfer
->stride
;
1085 for (i
= 0; i
< depth
; i
++) {
1086 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1087 texImage
->TexFormat
,
1091 texImage
->ImageOffsets
,
1093 format
, type
, src
, packing
)) {
1094 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1097 if (stImage
->pt
&& i
+ 1 < depth
) {
1098 /* unmap this slice */
1099 st_texture_image_unmap(ctx
->st
, stImage
);
1100 /* map next slice of 3D texture */
1101 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
,
1106 src
+= srcImageStride
;
1111 _mesa_unmap_teximage_pbo(ctx
, packing
);
1113 if (stImage
->pt
&& texImage
->Data
) {
1114 st_texture_image_unmap(ctx
->st
, stImage
);
1115 texImage
->Data
= NULL
;
1122 st_TexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1123 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1124 GLsizei width
, GLsizei height
, GLsizei depth
,
1125 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1126 const struct gl_pixelstore_attrib
*packing
,
1127 struct gl_texture_object
*texObj
,
1128 struct gl_texture_image
*texImage
)
1130 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1131 width
, height
, depth
, format
, type
,
1132 pixels
, packing
, texObj
, texImage
);
1137 st_TexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1138 GLint xoffset
, GLint yoffset
,
1139 GLsizei width
, GLsizei height
,
1140 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1141 const struct gl_pixelstore_attrib
*packing
,
1142 struct gl_texture_object
*texObj
,
1143 struct gl_texture_image
*texImage
)
1145 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1146 width
, height
, 1, format
, type
,
1147 pixels
, packing
, texObj
, texImage
);
1152 st_TexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1153 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1154 const GLvoid
* pixels
,
1155 const struct gl_pixelstore_attrib
*packing
,
1156 struct gl_texture_object
*texObj
,
1157 struct gl_texture_image
*texImage
)
1159 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1160 format
, type
, pixels
, packing
, texObj
, texImage
);
1165 st_CompressedTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1166 GLint xoffset
, GLsizei width
,
1168 GLsizei imageSize
, const GLvoid
*data
,
1169 struct gl_texture_object
*texObj
,
1170 struct gl_texture_image
*texImage
)
1177 st_CompressedTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1178 GLint xoffset
, GLint yoffset
,
1179 GLsizei width
, GLint height
,
1181 GLsizei imageSize
, const GLvoid
*data
,
1182 struct gl_texture_object
*texObj
,
1183 struct gl_texture_image
*texImage
)
1185 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1189 enum pipe_format pformat
= stImage
->pt
->format
;
1192 unsigned face
= _mesa_tex_target_to_face(target
);
1194 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1195 PIPE_TRANSFER_WRITE
);
1196 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
1197 PIPE_TRANSFER_WRITE
,
1201 srcBlockStride
= util_format_get_stride(pformat
, width
);
1202 dstBlockStride
= stImage
->transfer
->stride
;
1204 assert(stImage
->pt
);
1205 /* TODO find good values for block and strides */
1206 /* TODO also adjust texImage->data for yoffset/xoffset */
1210 if (!texImage
->Data
) {
1211 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1215 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1216 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1217 assert(width
% util_format_get_blockwidth(pformat
) == 0);
1218 assert(height
% util_format_get_blockheight(pformat
) == 0);
1220 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1221 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1222 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1223 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1224 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1228 st_texture_image_unmap(ctx
->st
, stImage
);
1229 texImage
->Data
= NULL
;
1235 st_CompressedTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1236 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1237 GLsizei width
, GLint height
, GLint depth
,
1239 GLsizei imageSize
, const GLvoid
*data
,
1240 struct gl_texture_object
*texObj
,
1241 struct gl_texture_image
*texImage
)
1249 * Do a CopyTexSubImage operation using a read transfer from the source,
1250 * a write transfer to the destination and get_tile()/put_tile() to access
1251 * the pixels/texels.
1253 * Note: srcY=0=TOP of renderbuffer
1256 fallback_copy_texsubimage(GLcontext
*ctx
, GLenum target
, GLint level
,
1257 struct st_renderbuffer
*strb
,
1258 struct st_texture_image
*stImage
,
1260 GLint destX
, GLint destY
, GLint destZ
,
1261 GLint srcX
, GLint srcY
,
1262 GLsizei width
, GLsizei height
)
1264 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1265 struct pipe_transfer
*src_trans
;
1267 enum pipe_transfer_usage transfer_usage
;
1269 if (ST_DEBUG
& DEBUG_FALLBACK
)
1270 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1272 assert(width
<= MAX_WIDTH
);
1274 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1275 srcY
= strb
->Base
.Height
- srcY
- height
;
1278 src_trans
= st_cond_flush_get_tex_transfer( st_context(ctx
),
1285 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1286 baseFormat
== GL_DEPTH_STENCIL
) &&
1287 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1288 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1290 transfer_usage
= PIPE_TRANSFER_WRITE
;
1292 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, 0, 0,
1295 texDest
= st_texture_image_map(ctx
->st
, stImage
, 0, transfer_usage
,
1296 destX
, destY
, width
, height
);
1298 if (baseFormat
== GL_DEPTH_COMPONENT
||
1299 baseFormat
== GL_DEPTH_STENCIL
) {
1300 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1301 ctx
->Pixel
.DepthBias
!= 0.0F
);
1304 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1305 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1314 /* To avoid a large temp memory allocation, do copy row by row */
1315 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1316 uint data
[MAX_WIDTH
];
1317 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1319 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1321 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1327 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1329 if (tempSrc
&& texDest
) {
1330 const GLint dims
= 2;
1331 const GLint dstRowStride
= stImage
->transfer
->stride
;
1332 struct gl_texture_image
*texImage
= &stImage
->base
;
1333 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1335 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1336 unpack
.Invert
= GL_TRUE
;
1339 /* get float/RGBA image from framebuffer */
1340 /* XXX this usually involves a lot of int/float conversion.
1341 * try to avoid that someday.
1343 pipe_get_tile_rgba(pipe
, src_trans
, 0, 0, width
, height
, tempSrc
);
1345 /* Store into texture memory.
1346 * Note that this does some special things such as pixel transfer
1347 * ops and format conversion. In particular, if the dest tex format
1348 * is actually RGBA but the user created the texture as GL_RGB we
1349 * need to fill-in/override the alpha channel with 1.0.
1351 _mesa_texstore(ctx
, dims
,
1352 texImage
->_BaseFormat
,
1353 texImage
->TexFormat
,
1357 texImage
->ImageOffsets
,
1359 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1363 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1370 st_texture_image_unmap(ctx
->st
, stImage
);
1371 pipe
->tex_transfer_destroy(pipe
, src_trans
);
1377 * If the format of the src renderbuffer and the format of the dest
1378 * texture are compatible (in terms of blitting), return a TGSI writemask
1379 * to be used during the blit.
1380 * If the src/dest are incompatible, return 0.
1383 compatible_src_dst_formats(GLcontext
*ctx
,
1384 const struct gl_renderbuffer
*src
,
1385 const struct gl_texture_image
*dst
)
1387 /* Get logical base formats for the src and dest.
1388 * That is, use the user-requested formats and not the actual, device-
1390 * For example, the user may have requested an A8 texture but the
1391 * driver may actually be using an RGBA texture format. When we
1392 * copy/blit to that texture, we only want to copy the Alpha channel
1393 * and not the RGB channels.
1395 * Similarly, when the src FBO was created an RGB format may have been
1396 * requested but the driver actually chose an RGBA format. In that case,
1397 * we don't want to copy the undefined Alpha channel to the dest texture
1398 * (it should be 1.0).
1400 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1401 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1404 * XXX when we have red-only and red/green renderbuffers we'll need
1405 * to add more cases here (or implement a general-purpose routine that
1406 * queries the existance of the R,G,B,A channels in the src and dest).
1408 if (srcFormat
== dstFormat
) {
1409 /* This is the same as matching_base_formats, which should
1410 * always pass, as it did previously.
1412 return TGSI_WRITEMASK_XYZW
;
1414 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1415 /* Make sure that A in the dest is 1. The actual src format
1416 * may be RGBA and have undefined A values.
1418 return TGSI_WRITEMASK_XYZ
;
1420 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1421 /* Make sure that A in the dest is 1. The actual dst format
1422 * may be RGBA and will need A=1 to provide proper alpha values
1423 * when sampled later.
1425 return TGSI_WRITEMASK_XYZ
;
1428 if (ST_DEBUG
& DEBUG_FALLBACK
)
1429 debug_printf("%s failed for src %s, dst %s\n",
1431 _mesa_lookup_enum_by_nr(srcFormat
),
1432 _mesa_lookup_enum_by_nr(dstFormat
));
1443 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1444 * Note that the region to copy has already been clipped so we know we
1445 * won't read from outside the source renderbuffer's bounds.
1447 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1450 st_copy_texsubimage(GLcontext
*ctx
,
1451 GLenum target
, GLint level
,
1452 GLint destX
, GLint destY
, GLint destZ
,
1453 GLint srcX
, GLint srcY
,
1454 GLsizei width
, GLsizei height
)
1456 struct gl_texture_unit
*texUnit
=
1457 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1458 struct gl_texture_object
*texObj
=
1459 _mesa_select_tex_object(ctx
, texUnit
, target
);
1460 struct st_texture_object
*stObj
= st_texture_object(texObj
);
1461 struct gl_texture_image
*texImage
=
1462 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1463 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1464 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1465 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1466 struct st_renderbuffer
*strb
;
1467 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1468 struct pipe_screen
*screen
= pipe
->screen
;
1469 enum pipe_format dest_format
, src_format
;
1470 GLboolean use_fallback
= GL_TRUE
;
1471 GLboolean matching_base_formats
;
1472 GLuint format_writemask
;
1473 struct pipe_surface
*dest_surface
= NULL
;
1474 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1476 /* any rendering in progress must flushed before we grab the fb image */
1477 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
1479 /* make sure finalize_textures has been called?
1481 if (0) st_validate_state(ctx
->st
);
1483 /* determine if copying depth or color data */
1484 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1485 texBaseFormat
== GL_DEPTH_STENCIL
) {
1486 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1489 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1490 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1493 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1494 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1522 if (width
< 0 || height
< 0)
1527 assert(strb
->surface
);
1528 assert(stImage
->pt
);
1530 src_format
= strb
->surface
->format
;
1531 dest_format
= stImage
->pt
->format
;
1534 * Determine if the src framebuffer and dest texture have the same
1535 * base format. We need this to detect a case such as the framebuffer
1536 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1537 * texture format stores RGBA we need to set A=1 (overriding the
1538 * framebuffer's alpha values). We can't do that with the blit or
1539 * textured-quad paths.
1541 matching_base_formats
=
1542 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1543 _mesa_get_format_base_format(texImage
->TexFormat
));
1544 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1546 if (ctx
->_ImageTransferState
== 0x0) {
1548 if (pipe
->surface_copy
&&
1549 matching_base_formats
&&
1550 src_format
== dest_format
&&
1553 /* use surface_copy() / blit */
1555 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1556 stImage
->face
, stImage
->level
,
1558 PIPE_BUFFER_USAGE_GPU_WRITE
);
1560 /* for surface_copy(), y=0=top, always */
1561 pipe
->surface_copy(pipe
,
1570 use_fallback
= GL_FALSE
;
1572 else if (format_writemask
&&
1573 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1574 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1575 screen
->is_format_supported(screen
, src_format
,
1577 PIPE_TEXTURE_USAGE_SAMPLER
,
1579 screen
->is_format_supported(screen
, dest_format
,
1581 PIPE_TEXTURE_USAGE_RENDER_TARGET
,
1583 /* draw textured quad to do the copy */
1586 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1587 stImage
->face
, stImage
->level
,
1589 PIPE_BUFFER_USAGE_GPU_WRITE
);
1592 srcY1
= strb
->Base
.Height
- srcY
- height
;
1593 srcY0
= srcY1
+ height
;
1597 srcY1
= srcY0
+ height
;
1599 util_blit_pixels_writemask(ctx
->st
->blit
,
1601 st_renderbuffer_get_sampler_view(strb
, pipe
),
1603 srcX
+ width
, srcY1
,
1606 destX
+ width
, destY
+ height
,
1607 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1609 use_fallback
= GL_FALSE
;
1613 pipe_surface_reference(&dest_surface
, NULL
);
1617 /* software fallback */
1618 fallback_copy_texsubimage(ctx
, target
, level
,
1619 strb
, stImage
, texBaseFormat
,
1620 destX
, destY
, destZ
,
1621 srcX
, srcY
, width
, height
);
1628 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1629 GLenum internalFormat
,
1630 GLint x
, GLint y
, GLsizei width
, GLint border
)
1632 struct gl_texture_unit
*texUnit
=
1633 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1634 struct gl_texture_object
*texObj
=
1635 _mesa_select_tex_object(ctx
, texUnit
, target
);
1636 struct gl_texture_image
*texImage
=
1637 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1639 /* Setup or redefine the texture object, texture and texture
1640 * image. Don't populate yet.
1642 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1644 GL_RGBA
, CHAN_TYPE
, NULL
,
1645 &ctx
->DefaultPacking
, texObj
, texImage
);
1647 st_copy_texsubimage(ctx
, target
, level
,
1648 0, 0, 0, /* destX,Y,Z */
1649 x
, y
, width
, 1); /* src X, Y, size */
1654 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1655 GLenum internalFormat
,
1656 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1659 struct gl_texture_unit
*texUnit
=
1660 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1661 struct gl_texture_object
*texObj
=
1662 _mesa_select_tex_object(ctx
, texUnit
, target
);
1663 struct gl_texture_image
*texImage
=
1664 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1666 /* Setup or redefine the texture object, texture and texture
1667 * image. Don't populate yet.
1669 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1670 width
, height
, border
,
1671 GL_RGBA
, CHAN_TYPE
, NULL
,
1672 &ctx
->DefaultPacking
, texObj
, texImage
);
1674 st_copy_texsubimage(ctx
, target
, level
,
1675 0, 0, 0, /* destX,Y,Z */
1676 x
, y
, width
, height
); /* src X, Y, size */
1681 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1682 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1684 const GLint yoffset
= 0, zoffset
= 0;
1685 const GLsizei height
= 1;
1686 st_copy_texsubimage(ctx
, target
, level
,
1687 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1688 x
, y
, width
, height
); /* src X, Y, size */
1693 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1694 GLint xoffset
, GLint yoffset
,
1695 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1697 const GLint zoffset
= 0;
1698 st_copy_texsubimage(ctx
, target
, level
,
1699 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1700 x
, y
, width
, height
); /* src X, Y, size */
1705 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1706 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1707 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1709 st_copy_texsubimage(ctx
, target
, level
,
1710 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1711 x
, y
, width
, height
); /* src X, Y, size */
1716 copy_image_data_to_texture(struct st_context
*st
,
1717 struct st_texture_object
*stObj
,
1719 struct st_texture_image
*stImage
)
1722 /* Copy potentially with the blitter:
1724 st_texture_image_copy(st
->pipe
,
1725 stObj
->pt
, dstLevel
, /* dest texture, level */
1726 stImage
->pt
, /* src texture */
1729 pipe_texture_reference(&stImage
->pt
, NULL
);
1731 else if (stImage
->base
.Data
) {
1732 /* More straightforward upload.
1734 st_teximage_flush_before_map(st
, stObj
->pt
, stImage
->face
, dstLevel
,
1735 PIPE_TRANSFER_WRITE
);
1737 st_texture_image_data(st
,
1742 stImage
->base
.RowStride
*
1743 util_format_get_blocksize(stObj
->pt
->format
),
1744 stImage
->base
.RowStride
*
1745 stImage
->base
.Height
*
1746 util_format_get_blocksize(stObj
->pt
->format
));
1747 _mesa_align_free(stImage
->base
.Data
);
1748 stImage
->base
.Data
= NULL
;
1751 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
1756 * Called during state validation. When this function is finished,
1757 * the texture object should be ready for rendering.
1758 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1761 st_finalize_texture(GLcontext
*ctx
,
1762 struct pipe_context
*pipe
,
1763 struct gl_texture_object
*tObj
,
1764 GLboolean
*needFlush
)
1766 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1767 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1768 GLuint blockSize
, face
;
1769 struct st_texture_image
*firstImage
;
1771 *needFlush
= GL_FALSE
;
1773 if (stObj
->base
._Complete
) {
1774 /* The texture is complete and we know exactly how many mipmap levels
1775 * are present/needed. This is conditional because we may be called
1776 * from the st_generate_mipmap() function when the texture object is
1777 * incomplete. In that case, we'll have set stObj->lastLevel before
1780 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1781 stObj
->base
.MinFilter
== GL_NEAREST
)
1782 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1784 stObj
->lastLevel
= stObj
->base
._MaxLevel
- stObj
->base
.BaseLevel
;
1787 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1789 /* If both firstImage and stObj point to a texture which can contain
1790 * all active images, favour firstImage. Note that because of the
1791 * completeness requirement, we know that the image dimensions
1794 if (firstImage
->pt
&&
1795 firstImage
->pt
!= stObj
->pt
&&
1796 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1797 pipe_texture_reference(&stObj
->pt
, firstImage
->pt
);
1800 /* bytes per pixel block (blocks are usually 1x1) */
1801 blockSize
= _mesa_get_format_bytes(firstImage
->base
.TexFormat
);
1803 /* If we already have a gallium texture, check that it matches the texture
1804 * object's format, target, size, num_levels, etc.
1807 const enum pipe_format fmt
=
1808 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1809 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1810 stObj
->pt
->format
!= fmt
||
1811 stObj
->pt
->last_level
< stObj
->lastLevel
||
1812 stObj
->pt
->width0
!= firstImage
->base
.Width2
||
1813 stObj
->pt
->height0
!= firstImage
->base
.Height2
||
1814 stObj
->pt
->depth0
!= firstImage
->base
.Depth2
)
1816 pipe_texture_reference(&stObj
->pt
, NULL
);
1817 ctx
->st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1821 /* May need to create a new gallium texture:
1824 const enum pipe_format fmt
=
1825 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1826 GLuint usage
= default_usage(fmt
);
1828 stObj
->pt
= st_texture_create(ctx
->st
,
1829 gl_target_to_pipe(stObj
->base
.Target
),
1832 firstImage
->base
.Width2
,
1833 firstImage
->base
.Height2
,
1834 firstImage
->base
.Depth2
,
1838 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1843 /* Pull in any images not in the object's texture:
1845 for (face
= 0; face
< nr_faces
; face
++) {
1847 for (level
= 0; level
<= stObj
->lastLevel
; level
++) {
1848 struct st_texture_image
*stImage
=
1849 st_texture_image(stObj
->base
.Image
[face
][stObj
->base
.BaseLevel
+ level
]);
1851 /* Need to import images in main memory or held in other textures.
1853 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1854 copy_image_data_to_texture(ctx
->st
, stObj
, level
, stImage
);
1855 *needFlush
= GL_TRUE
;
1865 * Returns pointer to a default/dummy texture.
1866 * This is typically used when the current shader has tex/sample instructions
1867 * but the user has not provided a (any) texture(s).
1869 struct gl_texture_object
*
1870 st_get_default_texture(struct st_context
*st
)
1872 if (!st
->default_texture
) {
1873 static const GLenum target
= GL_TEXTURE_2D
;
1874 GLubyte pixels
[16][16][4];
1875 struct gl_texture_object
*texObj
;
1876 struct gl_texture_image
*texImg
;
1879 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1880 * when attempting to sample incomplete textures.
1882 for (i
= 0; i
< 16; i
++) {
1883 for (j
= 0; j
< 16; j
++) {
1884 pixels
[i
][j
][0] = 0;
1885 pixels
[i
][j
][1] = 0;
1886 pixels
[i
][j
][2] = 0;
1887 pixels
[i
][j
][3] = 255;
1891 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1893 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1895 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1896 16, 16, 1, 0, /* w, h, d, border */
1899 st_TexImage(st
->ctx
, 2, target
,
1900 0, GL_RGBA
, /* level, intformat */
1901 16, 16, 1, 0, /* w, h, d, border */
1902 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1903 &st
->ctx
->DefaultPacking
,
1907 texObj
->MinFilter
= GL_NEAREST
;
1908 texObj
->MagFilter
= GL_NEAREST
;
1909 texObj
->_Complete
= GL_TRUE
;
1911 st
->default_texture
= texObj
;
1913 return st
->default_texture
;
1918 st_init_texture_functions(struct dd_function_table
*functions
)
1920 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1921 functions
->TexImage1D
= st_TexImage1D
;
1922 functions
->TexImage2D
= st_TexImage2D
;
1923 functions
->TexImage3D
= st_TexImage3D
;
1924 functions
->TexSubImage1D
= st_TexSubImage1D
;
1925 functions
->TexSubImage2D
= st_TexSubImage2D
;
1926 functions
->TexSubImage3D
= st_TexSubImage3D
;
1927 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1928 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1929 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1930 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1931 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1932 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1933 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1934 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1935 functions
->GenerateMipmap
= st_generate_mipmap
;
1937 functions
->GetTexImage
= st_GetTexImage
;
1939 /* compressed texture functions */
1940 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1941 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1943 functions
->NewTextureObject
= st_NewTextureObject
;
1944 functions
->NewTextureImage
= st_NewTextureImage
;
1945 functions
->DeleteTexture
= st_DeleteTextureObject
;
1946 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1947 functions
->UpdateTexturePalette
= 0;
1949 functions
->TextureMemCpy
= do_memcpy
;
1951 /* XXX Temporary until we can query pipe's texture sizes */
1952 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;