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_texture.h"
53 #include "state_tracker/st_gen_mipmap.h"
54 #include "state_tracker/st_inlines.h"
55 #include "state_tracker/st_atom.h"
57 #include "pipe/p_context.h"
58 #include "pipe/p_defines.h"
59 #include "util/u_inlines.h"
60 #include "pipe/p_shader_tokens.h"
61 #include "util/u_tile.h"
62 #include "util/u_blit.h"
63 #include "util/u_format.h"
64 #include "util/u_surface.h"
65 #include "util/u_sampler.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_resource_reference(&stObj
->pt
, NULL
);
126 if (stObj
->sampler_view
) {
127 if (stObj
->sampler_view
->context
!= ctx
->st
->pipe
) {
128 /* Take "ownership" of this texture sampler view by setting
129 * its context pointer to this context. This avoids potential
130 * crashes when the texture object is shared among contexts
131 * and the original/owner context has already been destroyed.
133 stObj
->sampler_view
->context
= ctx
->st
->pipe
;
135 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
137 _mesa_delete_texture_object(ctx
, texObj
);
141 /** called via ctx->Driver.FreeTexImageData() */
143 st_FreeTextureImageData(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
145 struct st_texture_image
*stImage
= st_texture_image(texImage
);
147 DBG("%s\n", __FUNCTION__
);
150 pipe_resource_reference(&stImage
->pt
, NULL
);
153 if (texImage
->Data
) {
154 _mesa_align_free(texImage
->Data
);
155 texImage
->Data
= NULL
;
161 * From linux kernel i386 header files, copes with odd sizes better
162 * than COPY_DWORDS would:
163 * XXX Put this in src/mesa/main/imports.h ???
165 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
167 __memcpy(void *to
, const void *from
, size_t n
)
170 __asm__
__volatile__("rep ; movsl\n\t"
174 "1:\ttestb $1,%b4\n\t"
176 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
177 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
182 #define __memcpy(a,b,c) memcpy(a,b,c)
187 * The system memcpy (at least on ubuntu 5.10) has problems copying
188 * to agp (writecombined) memory from a source which isn't 64-byte
189 * aligned - there is a 4x performance falloff.
191 * The x86 __memcpy is immune to this but is slightly slower
192 * (10%-ish) than the system memcpy.
194 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
195 * isn't much faster than x86_memcpy for agp copies.
197 * TODO: switch dynamically.
200 do_memcpy(void *dest
, const void *src
, size_t n
)
202 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
203 return __memcpy(dest
, src
, n
);
206 return memcpy(dest
, src
, n
);
211 * Return default texture resource binding bitmask for the given format.
214 default_bindings(struct st_context
*st
, enum pipe_format format
)
216 struct pipe_screen
*screen
= st
->pipe
->screen
;
217 const unsigned target
= PIPE_TEXTURE_2D
;
218 const unsigned geom
= 0x0;
221 if (util_format_is_depth_or_stencil(format
))
222 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
224 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
226 if (screen
->is_format_supported(screen
, format
, target
, bindings
, geom
))
229 return PIPE_BIND_SAMPLER_VIEW
;
234 * Allocate a pipe_resource object for the given st_texture_object using
235 * the given st_texture_image to guess the mipmap size/levels.
238 * Otherwise, store it in memory if (Border != 0) or (any dimension ==
241 * Otherwise, if max_level >= level >= min_level, create texture with
242 * space for images from min_level down to max_level.
244 * Otherwise, create texture with space for images from (level 0)..(1x1).
245 * Consider pruning this texture at a validation if the saving is worth it.
248 guess_and_alloc_texture(struct st_context
*st
,
249 struct st_texture_object
*stObj
,
250 const struct st_texture_image
*stImage
)
254 GLuint width
= stImage
->base
.Width2
; /* size w/out border */
255 GLuint height
= stImage
->base
.Height2
;
256 GLuint depth
= stImage
->base
.Depth2
;
258 enum pipe_format fmt
;
260 DBG("%s\n", __FUNCTION__
);
265 (GLint
) stImage
->level
> stObj
->base
.BaseLevel
&&
266 (stImage
->base
.Width
== 1 ||
267 (stObj
->base
.Target
!= GL_TEXTURE_1D
&&
268 stImage
->base
.Height
== 1) ||
269 (stObj
->base
.Target
== GL_TEXTURE_3D
&&
270 stImage
->base
.Depth
== 1)))
273 /* If this image disrespects BaseLevel, allocate from level zero.
274 * Usually BaseLevel == 0, so it's unlikely to happen.
276 if ((GLint
) stImage
->level
< stObj
->base
.BaseLevel
)
279 firstLevel
= stObj
->base
.BaseLevel
;
282 /* Figure out image dimensions at start level.
284 for (i
= stImage
->level
; i
> firstLevel
; i
--) {
293 if (width
== 0 || height
== 0 || depth
== 0) {
294 /* no texture needed */
298 /* Guess a reasonable value for lastLevel. This is probably going
299 * to be wrong fairly often and might mean that we have to look at
300 * resizable buffers, or require that buffers implement lazy
301 * pagetable arrangements.
303 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
304 stObj
->base
.MinFilter
== GL_LINEAR
||
305 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
306 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
307 !stObj
->base
.GenerateMipmap
&&
308 stImage
->level
== firstLevel
) {
309 /* only alloc space for a single mipmap level */
310 lastLevel
= firstLevel
;
313 /* alloc space for a full mipmap */
314 GLuint l2width
= util_logbase2(width
);
315 GLuint l2height
= util_logbase2(height
);
316 GLuint l2depth
= util_logbase2(depth
);
317 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
320 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
322 bindings
= default_bindings(st
, fmt
);
324 stObj
->pt
= st_texture_create(st
,
325 gl_target_to_pipe(stObj
->base
.Target
),
333 DBG("%s - success\n", __FUNCTION__
);
338 * Adjust pixel unpack params and image dimensions to strip off the
340 * Gallium doesn't support texture borders. They've seldem been used
341 * and seldom been implemented correctly anyway.
342 * \param unpackNew returns the new pixel unpack parameters
345 strip_texture_border(GLint border
,
346 GLint
*width
, GLint
*height
, GLint
*depth
,
347 const struct gl_pixelstore_attrib
*unpack
,
348 struct gl_pixelstore_attrib
*unpackNew
)
350 assert(border
> 0); /* sanity check */
352 *unpackNew
= *unpack
;
354 if (unpackNew
->RowLength
== 0)
355 unpackNew
->RowLength
= *width
;
357 if (depth
&& unpackNew
->ImageHeight
== 0)
358 unpackNew
->ImageHeight
= *height
;
360 unpackNew
->SkipPixels
+= border
;
362 unpackNew
->SkipRows
+= border
;
364 unpackNew
->SkipImages
+= border
;
367 *width
= *width
- 2 * border
;
368 if (height
&& *height
>= 3)
369 *height
= *height
- 2 * border
;
370 if (depth
&& *depth
>= 3)
371 *depth
= *depth
- 2 * border
;
376 * Try to do texture compression via rendering. If the Gallium driver
377 * can render into a compressed surface this will allow us to do texture
379 * \return GL_TRUE for success, GL_FALSE for failure
382 compress_with_blit(GLcontext
* ctx
,
383 GLenum target
, GLint level
,
384 GLint xoffset
, GLint yoffset
, GLint zoffset
,
385 GLint width
, GLint height
, GLint depth
,
386 GLenum format
, GLenum type
, const void *pixels
,
387 const struct gl_pixelstore_attrib
*unpack
,
388 struct gl_texture_image
*texImage
)
390 const GLuint dstImageOffsets
[1] = {0};
391 struct st_texture_image
*stImage
= st_texture_image(texImage
);
392 struct pipe_context
*pipe
= ctx
->st
->pipe
;
393 struct pipe_screen
*screen
= pipe
->screen
;
394 gl_format mesa_format
;
395 struct pipe_resource templ
;
396 struct pipe_resource
*src_tex
;
397 struct pipe_sampler_view view_templ
;
398 struct pipe_sampler_view
*src_view
;
399 struct pipe_surface
*dst_surface
;
400 struct pipe_transfer
*tex_xfer
;
404 /* XXX: Can this happen? Should we assert? */
408 /* get destination surface (in the compressed texture) */
409 dst_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
410 stImage
->face
, stImage
->level
, 0,
411 PIPE_BIND_BLIT_DESTINATION
);
413 /* can't render into this format (or other problem) */
417 /* Choose format for the temporary RGBA texture image.
419 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
424 /* Create the temporary source texture
426 memset(&templ
, 0, sizeof(templ
));
427 templ
.target
= PIPE_TEXTURE_2D
;
428 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
);
429 templ
.width0
= width
;
430 templ
.height0
= height
;
432 templ
.last_level
= 0;
433 templ
.usage
= PIPE_USAGE_DEFAULT
;
434 templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
435 src_tex
= screen
->resource_create(screen
, &templ
);
440 /* Put user's tex data into the temporary texture
442 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
), src_tex
,
443 0, 0, 0, /* face, level are zero */
445 0, 0, width
, height
); /* x, y, w, h */
446 map
= pipe_transfer_map(pipe
, tex_xfer
);
448 _mesa_texstore(ctx
, 2, GL_RGBA
, mesa_format
,
450 0, 0, 0, /* dest x/y/z offset */
451 tex_xfer
->stride
, /* dest row stride (bytes) */
452 dstImageOffsets
, /* image offsets (for 3D only) */
453 width
, height
, 1, /* size */
454 format
, type
, /* source format/type */
455 pixels
, /* source data */
456 unpack
); /* source data packing */
458 pipe_transfer_unmap(pipe
, tex_xfer
);
459 pipe
->transfer_destroy(pipe
, tex_xfer
);
461 /* Create temporary sampler view */
462 u_sampler_view_default_template(&view_templ
,
465 src_view
= pipe
->create_sampler_view(pipe
, src_tex
, &view_templ
);
468 /* copy / compress image */
469 util_blit_pixels_tex(ctx
->st
->blit
,
470 src_view
, /* sampler view (src) */
471 0, 0, /* src x0, y0 */
472 width
, height
, /* src x1, y1 */
473 dst_surface
, /* pipe_surface (dst) */
474 xoffset
, yoffset
, /* dst x0, y0 */
475 xoffset
+ width
, /* dst x1 */
476 yoffset
+ height
, /* dst y1 */
478 PIPE_TEX_MIPFILTER_NEAREST
);
480 pipe_surface_reference(&dst_surface
, NULL
);
481 pipe_resource_reference(&src_tex
, NULL
);
482 pipe_sampler_view_reference(&src_view
, NULL
);
489 * Do glTexImage1/2/3D().
492 st_TexImage(GLcontext
* ctx
,
494 GLenum target
, GLint level
,
495 GLint internalFormat
,
496 GLint width
, GLint height
, GLint depth
,
498 GLenum format
, GLenum type
, const void *pixels
,
499 const struct gl_pixelstore_attrib
*unpack
,
500 struct gl_texture_object
*texObj
,
501 struct gl_texture_image
*texImage
,
502 GLsizei imageSize
, GLboolean compressed_src
)
504 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
505 struct st_texture_object
*stObj
= st_texture_object(texObj
);
506 struct st_texture_image
*stImage
= st_texture_image(texImage
);
507 GLint postConvWidth
, postConvHeight
;
508 GLint texelBytes
, sizeInBytes
;
509 GLuint dstRowStride
= 0;
510 struct gl_pixelstore_attrib unpackNB
;
511 enum pipe_transfer_usage transfer_usage
= 0;
513 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
514 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
516 /* switch to "normal" */
517 if (stObj
->surface_based
) {
518 _mesa_clear_texture_object(ctx
, texObj
);
519 stObj
->surface_based
= GL_FALSE
;
522 /* gallium does not support texture borders, strip it off */
524 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
526 texImage
->Width
= width
;
527 texImage
->Height
= height
;
528 texImage
->Depth
= depth
;
529 texImage
->Border
= 0;
533 postConvWidth
= width
;
534 postConvHeight
= height
;
536 stImage
->face
= _mesa_tex_target_to_face(target
);
537 stImage
->level
= level
;
540 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
541 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
546 _mesa_set_fetch_functions(texImage
, dims
);
548 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
549 /* must be a compressed format */
553 texelBytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
555 /* Minimum pitch of 32 bytes */
556 if (postConvWidth
* texelBytes
< 32) {
557 postConvWidth
= 32 / texelBytes
;
558 texImage
->RowStride
= postConvWidth
;
561 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
562 /*assert(texImage->RowStride == postConvWidth);*/
565 /* Release the reference to a potentially orphaned buffer.
566 * Release any old malloced memory.
569 pipe_resource_reference(&stImage
->pt
, NULL
);
570 assert(!texImage
->Data
);
572 else if (texImage
->Data
) {
573 _mesa_align_free(texImage
->Data
);
577 * See if the new image is somehow incompatible with the existing
578 * mipmap. If so, free the old mipmap.
581 if (stObj
->teximage_realloc
||
582 level
> (GLint
) stObj
->pt
->last_level
||
583 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
584 stImage
->face
, stImage
->level
)) {
586 pipe_resource_reference(&stObj
->pt
, NULL
);
588 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
589 stObj
->teximage_realloc
= FALSE
;
593 if (width
== 0 || height
== 0 || depth
== 0) {
594 /* stop after freeing old image */
599 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
601 /* Probably out of memory.
602 * Try flushing any pending rendering, then retry.
605 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
607 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
613 assert(!stImage
->pt
);
616 st_texture_match_image(stObj
->pt
, &stImage
->base
,
617 stImage
->face
, stImage
->level
)) {
619 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
624 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
626 /* st_CopyTexImage calls this function with pixels == NULL, with
627 * the expectation that the texture will be set up but nothing
628 * more will be done. This is where those calls return:
630 if (compressed_src
) {
631 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
633 "glCompressedTexImage");
636 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
638 pixels
, unpack
, "glTexImage");
641 /* Note: we can't check for pixels==NULL until after we've allocated
642 * memory for the texture.
645 /* See if we can do texture compression with a blit/render.
647 if (!compressed_src
&&
649 _mesa_is_format_compressed(texImage
->TexFormat
) &&
650 screen
->is_format_supported(screen
,
653 PIPE_BIND_RENDER_TARGET
, 0)) {
657 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
658 format
, type
, pixels
, unpack
, texImage
)) {
664 if (format
== GL_DEPTH_COMPONENT
&&
665 util_format_is_depth_and_stencil(stImage
->pt
->format
))
666 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
668 transfer_usage
= PIPE_TRANSFER_WRITE
;
670 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
671 transfer_usage
, 0, 0,
673 stImage
->base
.Height
);
674 if(stImage
->transfer
)
675 dstRowStride
= stImage
->transfer
->stride
;
678 /* Allocate regular memory and store the image there temporarily. */
679 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
680 sizeInBytes
= _mesa_format_image_size(texImage
->TexFormat
,
684 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
688 dstRowStride
= postConvWidth
* texelBytes
;
689 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
692 texImage
->Data
= _mesa_align_malloc(sizeInBytes
, 16);
695 if (!texImage
->Data
) {
696 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
703 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
704 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
706 /* Copy data. Would like to know when it's ok for us to eg. use
707 * the blitter to copy. Or, use the hardware to do the format
708 * conversion and copy:
710 if (compressed_src
) {
711 const GLuint srcImageStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
712 if(dstRowStride
== srcImageStride
)
713 memcpy(texImage
->Data
, pixels
, imageSize
);
716 char *dst
= texImage
->Data
;
717 const char *src
= pixels
;
718 GLuint i
, bw
, bh
, lines
;
719 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
720 lines
= (height
+ bh
- 1) / bh
;
722 for(i
= 0; i
< lines
; ++i
)
724 memcpy(dst
, src
, srcImageStride
);
726 src
+= srcImageStride
;
731 const GLuint srcImageStride
=
732 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
734 const GLubyte
*src
= (const GLubyte
*) pixels
;
736 for (i
= 0; i
< depth
; i
++) {
737 if (!_mesa_texstore(ctx
, dims
,
738 texImage
->_BaseFormat
,
741 0, 0, 0, /* dstX/Y/Zoffset */
743 texImage
->ImageOffsets
,
745 format
, type
, src
, unpack
)) {
746 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
749 if (stImage
->pt
&& i
+ 1 < depth
) {
750 /* unmap this slice */
751 st_texture_image_unmap(ctx
->st
, stImage
);
752 /* map next slice of 3D texture */
753 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
754 transfer_usage
, 0, 0,
756 stImage
->base
.Height
);
757 src
+= srcImageStride
;
763 _mesa_unmap_teximage_pbo(ctx
, unpack
);
765 if (stImage
->pt
&& texImage
->Data
) {
766 st_texture_image_unmap(ctx
->st
, stImage
);
767 texImage
->Data
= NULL
;
773 st_TexImage3D(GLcontext
* ctx
,
774 GLenum target
, GLint level
,
775 GLint internalFormat
,
776 GLint width
, GLint height
, GLint depth
,
778 GLenum format
, GLenum type
, const void *pixels
,
779 const struct gl_pixelstore_attrib
*unpack
,
780 struct gl_texture_object
*texObj
,
781 struct gl_texture_image
*texImage
)
783 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
784 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
790 st_TexImage2D(GLcontext
* ctx
,
791 GLenum target
, GLint level
,
792 GLint internalFormat
,
793 GLint width
, GLint height
, GLint border
,
794 GLenum format
, GLenum type
, const void *pixels
,
795 const struct gl_pixelstore_attrib
*unpack
,
796 struct gl_texture_object
*texObj
,
797 struct gl_texture_image
*texImage
)
799 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
800 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
805 st_TexImage1D(GLcontext
* ctx
,
806 GLenum target
, GLint level
,
807 GLint internalFormat
,
808 GLint width
, GLint border
,
809 GLenum format
, GLenum type
, const void *pixels
,
810 const struct gl_pixelstore_attrib
*unpack
,
811 struct gl_texture_object
*texObj
,
812 struct gl_texture_image
*texImage
)
814 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
815 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
820 st_CompressedTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
821 GLint internalFormat
,
822 GLint width
, GLint height
, GLint border
,
823 GLsizei imageSize
, const GLvoid
*data
,
824 struct gl_texture_object
*texObj
,
825 struct gl_texture_image
*texImage
)
827 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
828 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
834 * glGetTexImage() helper: decompress a compressed texture by rendering
835 * a textured quad. Store the results in the user's buffer.
838 decompress_with_blit(GLcontext
* ctx
, GLenum target
, GLint level
,
839 GLenum format
, GLenum type
, GLvoid
*pixels
,
840 struct gl_texture_object
*texObj
,
841 struct gl_texture_image
*texImage
)
843 struct pipe_context
*pipe
= ctx
->st
->pipe
;
844 struct pipe_screen
*screen
= pipe
->screen
;
845 struct st_texture_image
*stImage
= st_texture_image(texImage
);
846 struct st_texture_object
*stObj
= st_texture_object(texObj
);
847 struct pipe_sampler_view
*src_view
=
848 st_get_texture_sampler_view(stObj
, pipe
);
849 const GLuint width
= texImage
->Width
;
850 const GLuint height
= texImage
->Height
;
851 struct pipe_surface
*dst_surface
;
852 struct pipe_resource
*dst_texture
;
853 struct pipe_transfer
*tex_xfer
;
854 unsigned bind
= (PIPE_BIND_BLIT_DESTINATION
|
855 PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
856 PIPE_BIND_TRANSFER_READ
);
858 /* create temp / dest surface */
859 if (!util_create_rgba_surface(screen
, width
, height
, bind
,
860 &dst_texture
, &dst_surface
)) {
861 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
862 "in decompress_with_blit()");
866 /* blit/render/decompress */
867 util_blit_pixels_tex(ctx
->st
->blit
,
868 src_view
, /* pipe_resource (src) */
869 0, 0, /* src x0, y0 */
870 width
, height
, /* src x1, y1 */
871 dst_surface
, /* pipe_surface (dst) */
872 0, 0, /* dst x0, y0 */
873 width
, height
, /* dst x1, y1 */
875 PIPE_TEX_MIPFILTER_NEAREST
);
877 /* map the dst_surface so we can read from it */
878 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
),
879 dst_texture
, 0, 0, 0,
881 0, 0, width
, height
);
883 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
885 /* copy/pack data into user buffer */
886 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
888 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
889 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
891 for (row
= 0; row
< height
; row
++) {
892 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
893 height
, format
, type
, row
, 0);
894 memcpy(dest
, map
, bytesPerRow
);
895 map
+= tex_xfer
->stride
;
897 pipe_transfer_unmap(pipe
, tex_xfer
);
900 /* format translation via floats */
902 for (row
= 0; row
< height
; row
++) {
903 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
904 GLfloat rgba
[4 * MAX_WIDTH
];
905 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
906 height
, format
, type
, row
, 0);
908 if (ST_DEBUG
& DEBUG_FALLBACK
)
909 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
911 /* get float[4] rgba row from surface */
912 pipe_get_tile_rgba(pipe
, tex_xfer
, 0, row
, width
, 1, rgba
);
914 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
915 type
, dest
, &ctx
->Pack
, transferOps
);
919 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
921 pipe
->transfer_destroy(pipe
, tex_xfer
);
923 /* destroy the temp / dest surface */
924 util_destroy_rgba_surface(dst_texture
, dst_surface
);
930 * Need to map texture image into memory before copying image data,
934 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
935 GLenum format
, GLenum type
, GLvoid
* pixels
,
936 struct gl_texture_object
*texObj
,
937 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
939 struct st_texture_image
*stImage
= st_texture_image(texImage
);
940 const GLuint dstImageStride
=
941 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
947 util_format_is_s3tc(stImage
->pt
->format
) &&
949 /* Need to decompress the texture.
950 * We'll do this by rendering a textured quad.
951 * Note that we only expect RGBA formats (no Z/depth formats).
953 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
960 /* Image is stored in hardware format in a buffer managed by the
961 * kernel. Need to explicitly map and unmap it.
963 unsigned face
= _mesa_tex_target_to_face(target
);
965 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
968 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
969 PIPE_TRANSFER_READ
, 0, 0,
971 stImage
->base
.Height
);
972 texImage
->RowStride
= stImage
->transfer
->stride
/ util_format_get_blocksize(stImage
->pt
->format
);
975 /* Otherwise, the image should actually be stored in
976 * texImage->Data. This is pretty confusing for
977 * everybody, I'd much prefer to separate the two functions of
978 * texImage->Data - storage for texture images in main memory
979 * and access (ie mappings) of images. In other words, we'd
980 * create a new texImage->Map field and leave Data simply for
983 assert(texImage
->Data
);
986 depth
= texImage
->Depth
;
989 dest
= (GLubyte
*) pixels
;
991 for (i
= 0; i
< depth
; i
++) {
992 if (compressed_dst
) {
993 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
997 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
1001 if (stImage
->pt
&& i
+ 1 < depth
) {
1002 /* unmap this slice */
1003 st_texture_image_unmap(ctx
->st
, stImage
);
1004 /* map next slice of 3D texture */
1005 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
1006 PIPE_TRANSFER_READ
, 0, 0,
1007 stImage
->base
.Width
,
1008 stImage
->base
.Height
);
1009 dest
+= dstImageStride
;
1013 texImage
->Depth
= depth
;
1017 st_texture_image_unmap(ctx
->st
, stImage
);
1018 texImage
->Data
= NULL
;
1024 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
1025 GLenum format
, GLenum type
, GLvoid
* pixels
,
1026 struct gl_texture_object
*texObj
,
1027 struct gl_texture_image
*texImage
)
1029 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1035 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
1037 struct gl_texture_object
*texObj
,
1038 struct gl_texture_image
*texImage
)
1040 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1047 st_TexSubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
1048 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1049 GLint width
, GLint height
, GLint depth
,
1050 GLenum format
, GLenum type
, const void *pixels
,
1051 const struct gl_pixelstore_attrib
*packing
,
1052 struct gl_texture_object
*texObj
,
1053 struct gl_texture_image
*texImage
)
1055 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
1056 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1057 GLuint dstRowStride
;
1058 const GLuint srcImageStride
=
1059 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1062 /* init to silence warning only: */
1063 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1065 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1066 _mesa_lookup_enum_by_nr(target
),
1067 level
, xoffset
, yoffset
, width
, height
);
1070 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1071 type
, pixels
, packing
, "glTexSubImage2D");
1075 /* See if we can do texture compression with a blit/render.
1077 if (!ctx
->Mesa_DXTn
&&
1078 _mesa_is_format_compressed(texImage
->TexFormat
) &&
1079 screen
->is_format_supported(screen
,
1080 stImage
->pt
->format
,
1081 stImage
->pt
->target
,
1082 PIPE_BIND_RENDER_TARGET
, 0)) {
1083 if (compress_with_blit(ctx
, target
, level
,
1084 xoffset
, yoffset
, zoffset
,
1085 width
, height
, depth
,
1086 format
, type
, pixels
, packing
, texImage
)) {
1091 /* Map buffer if necessary. Need to lock to prevent other contexts
1092 * from uploading the buffer under us.
1095 unsigned face
= _mesa_tex_target_to_face(target
);
1097 if (format
== GL_DEPTH_COMPONENT
&&
1098 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1099 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1101 transfer_usage
= PIPE_TRANSFER_WRITE
;
1103 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1105 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
,
1111 if (!texImage
->Data
) {
1112 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1116 src
= (const GLubyte
*) pixels
;
1117 dstRowStride
= stImage
->transfer
->stride
;
1119 for (i
= 0; i
< depth
; i
++) {
1120 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1121 texImage
->TexFormat
,
1125 texImage
->ImageOffsets
,
1127 format
, type
, src
, packing
)) {
1128 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1131 if (stImage
->pt
&& i
+ 1 < depth
) {
1132 /* unmap this slice */
1133 st_texture_image_unmap(ctx
->st
, stImage
);
1134 /* map next slice of 3D texture */
1135 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
,
1140 src
+= srcImageStride
;
1145 _mesa_unmap_teximage_pbo(ctx
, packing
);
1147 if (stImage
->pt
&& texImage
->Data
) {
1148 st_texture_image_unmap(ctx
->st
, stImage
);
1149 texImage
->Data
= NULL
;
1156 st_TexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1157 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1158 GLsizei width
, GLsizei height
, GLsizei depth
,
1159 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1160 const struct gl_pixelstore_attrib
*packing
,
1161 struct gl_texture_object
*texObj
,
1162 struct gl_texture_image
*texImage
)
1164 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1165 width
, height
, depth
, format
, type
,
1166 pixels
, packing
, texObj
, texImage
);
1171 st_TexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1172 GLint xoffset
, GLint yoffset
,
1173 GLsizei width
, GLsizei height
,
1174 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1175 const struct gl_pixelstore_attrib
*packing
,
1176 struct gl_texture_object
*texObj
,
1177 struct gl_texture_image
*texImage
)
1179 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1180 width
, height
, 1, format
, type
,
1181 pixels
, packing
, texObj
, texImage
);
1186 st_TexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1187 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1188 const GLvoid
* pixels
,
1189 const struct gl_pixelstore_attrib
*packing
,
1190 struct gl_texture_object
*texObj
,
1191 struct gl_texture_image
*texImage
)
1193 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1194 format
, type
, pixels
, packing
, texObj
, texImage
);
1199 st_CompressedTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1200 GLint xoffset
, GLsizei width
,
1202 GLsizei imageSize
, const GLvoid
*data
,
1203 struct gl_texture_object
*texObj
,
1204 struct gl_texture_image
*texImage
)
1211 st_CompressedTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1212 GLint xoffset
, GLint yoffset
,
1213 GLsizei width
, GLint height
,
1215 GLsizei imageSize
, const GLvoid
*data
,
1216 struct gl_texture_object
*texObj
,
1217 struct gl_texture_image
*texImage
)
1219 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1223 enum pipe_format pformat
;
1226 unsigned face
= _mesa_tex_target_to_face(target
);
1227 pformat
= stImage
->pt
->format
;
1229 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1230 PIPE_TRANSFER_WRITE
);
1231 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
1232 PIPE_TRANSFER_WRITE
,
1236 srcBlockStride
= util_format_get_stride(pformat
, width
);
1237 dstBlockStride
= stImage
->transfer
->stride
;
1239 assert(stImage
->pt
);
1240 /* TODO find good values for block and strides */
1241 /* TODO also adjust texImage->data for yoffset/xoffset */
1245 if (!texImage
->Data
) {
1246 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1250 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1251 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1252 assert(width
% util_format_get_blockwidth(pformat
) == 0);
1253 assert(height
% util_format_get_blockheight(pformat
) == 0);
1255 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1256 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1257 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1258 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1259 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1263 st_texture_image_unmap(ctx
->st
, stImage
);
1264 texImage
->Data
= NULL
;
1270 st_CompressedTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1271 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1272 GLsizei width
, GLint height
, GLint depth
,
1274 GLsizei imageSize
, const GLvoid
*data
,
1275 struct gl_texture_object
*texObj
,
1276 struct gl_texture_image
*texImage
)
1284 * Do a CopyTexSubImage operation using a read transfer from the source,
1285 * a write transfer to the destination and get_tile()/put_tile() to access
1286 * the pixels/texels.
1288 * Note: srcY=0=TOP of renderbuffer
1291 fallback_copy_texsubimage(GLcontext
*ctx
, GLenum target
, GLint level
,
1292 struct st_renderbuffer
*strb
,
1293 struct st_texture_image
*stImage
,
1295 GLint destX
, GLint destY
, GLint destZ
,
1296 GLint srcX
, GLint srcY
,
1297 GLsizei width
, GLsizei height
)
1299 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1300 struct pipe_transfer
*src_trans
;
1302 enum pipe_transfer_usage transfer_usage
;
1304 if (ST_DEBUG
& DEBUG_FALLBACK
)
1305 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1307 assert(width
<= MAX_WIDTH
);
1309 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1310 srcY
= strb
->Base
.Height
- srcY
- height
;
1313 src_trans
= st_cond_flush_get_tex_transfer( st_context(ctx
),
1320 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1321 baseFormat
== GL_DEPTH_STENCIL
) &&
1322 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1323 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1325 transfer_usage
= PIPE_TRANSFER_WRITE
;
1327 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, 0, 0,
1330 texDest
= st_texture_image_map(ctx
->st
, stImage
, 0, transfer_usage
,
1331 destX
, destY
, width
, height
);
1333 if (baseFormat
== GL_DEPTH_COMPONENT
||
1334 baseFormat
== GL_DEPTH_STENCIL
) {
1335 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1336 ctx
->Pixel
.DepthBias
!= 0.0F
);
1339 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1340 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1349 /* To avoid a large temp memory allocation, do copy row by row */
1350 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1351 uint data
[MAX_WIDTH
];
1352 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1354 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1356 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1362 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1364 if (tempSrc
&& texDest
) {
1365 const GLint dims
= 2;
1366 const GLint dstRowStride
= stImage
->transfer
->stride
;
1367 struct gl_texture_image
*texImage
= &stImage
->base
;
1368 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1370 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1371 unpack
.Invert
= GL_TRUE
;
1374 /* get float/RGBA image from framebuffer */
1375 /* XXX this usually involves a lot of int/float conversion.
1376 * try to avoid that someday.
1378 pipe_get_tile_rgba(pipe
, src_trans
, 0, 0, width
, height
, tempSrc
);
1380 /* Store into texture memory.
1381 * Note that this does some special things such as pixel transfer
1382 * ops and format conversion. In particular, if the dest tex format
1383 * is actually RGBA but the user created the texture as GL_RGB we
1384 * need to fill-in/override the alpha channel with 1.0.
1386 _mesa_texstore(ctx
, dims
,
1387 texImage
->_BaseFormat
,
1388 texImage
->TexFormat
,
1392 texImage
->ImageOffsets
,
1394 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1398 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1405 st_texture_image_unmap(ctx
->st
, stImage
);
1406 pipe
->transfer_destroy(pipe
, src_trans
);
1412 * If the format of the src renderbuffer and the format of the dest
1413 * texture are compatible (in terms of blitting), return a TGSI writemask
1414 * to be used during the blit.
1415 * If the src/dest are incompatible, return 0.
1418 compatible_src_dst_formats(GLcontext
*ctx
,
1419 const struct gl_renderbuffer
*src
,
1420 const struct gl_texture_image
*dst
)
1422 /* Get logical base formats for the src and dest.
1423 * That is, use the user-requested formats and not the actual, device-
1425 * For example, the user may have requested an A8 texture but the
1426 * driver may actually be using an RGBA texture format. When we
1427 * copy/blit to that texture, we only want to copy the Alpha channel
1428 * and not the RGB channels.
1430 * Similarly, when the src FBO was created an RGB format may have been
1431 * requested but the driver actually chose an RGBA format. In that case,
1432 * we don't want to copy the undefined Alpha channel to the dest texture
1433 * (it should be 1.0).
1435 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1436 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1439 * XXX when we have red-only and red/green renderbuffers we'll need
1440 * to add more cases here (or implement a general-purpose routine that
1441 * queries the existance of the R,G,B,A channels in the src and dest).
1443 if (srcFormat
== dstFormat
) {
1444 /* This is the same as matching_base_formats, which should
1445 * always pass, as it did previously.
1447 return TGSI_WRITEMASK_XYZW
;
1449 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1450 /* Make sure that A in the dest is 1. The actual src format
1451 * may be RGBA and have undefined A values.
1453 return TGSI_WRITEMASK_XYZ
;
1455 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1456 /* Make sure that A in the dest is 1. The actual dst format
1457 * may be RGBA and will need A=1 to provide proper alpha values
1458 * when sampled later.
1460 return TGSI_WRITEMASK_XYZ
;
1463 if (ST_DEBUG
& DEBUG_FALLBACK
)
1464 debug_printf("%s failed for src %s, dst %s\n",
1466 _mesa_lookup_enum_by_nr(srcFormat
),
1467 _mesa_lookup_enum_by_nr(dstFormat
));
1478 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1479 * Note that the region to copy has already been clipped so we know we
1480 * won't read from outside the source renderbuffer's bounds.
1482 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1485 st_copy_texsubimage(GLcontext
*ctx
,
1486 GLenum target
, GLint level
,
1487 GLint destX
, GLint destY
, GLint destZ
,
1488 GLint srcX
, GLint srcY
,
1489 GLsizei width
, GLsizei height
)
1491 struct gl_texture_unit
*texUnit
=
1492 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1493 struct gl_texture_object
*texObj
=
1494 _mesa_select_tex_object(ctx
, texUnit
, target
);
1495 struct gl_texture_image
*texImage
=
1496 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1497 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1498 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1499 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1500 struct st_renderbuffer
*strb
;
1501 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1502 struct pipe_screen
*screen
= pipe
->screen
;
1503 enum pipe_format dest_format
, src_format
;
1504 GLboolean use_fallback
= GL_TRUE
;
1505 GLboolean matching_base_formats
;
1506 GLuint format_writemask
;
1507 struct pipe_surface
*dest_surface
= NULL
;
1508 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1510 /* any rendering in progress must flushed before we grab the fb image */
1511 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
1513 /* make sure finalize_textures has been called?
1515 if (0) st_validate_state(ctx
->st
);
1517 /* determine if copying depth or color data */
1518 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1519 texBaseFormat
== GL_DEPTH_STENCIL
) {
1520 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1523 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1524 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1527 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1528 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1556 if (width
< 0 || height
< 0)
1561 assert(strb
->surface
);
1562 assert(stImage
->pt
);
1564 src_format
= strb
->surface
->format
;
1565 dest_format
= stImage
->pt
->format
;
1568 * Determine if the src framebuffer and dest texture have the same
1569 * base format. We need this to detect a case such as the framebuffer
1570 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1571 * texture format stores RGBA we need to set A=1 (overriding the
1572 * framebuffer's alpha values). We can't do that with the blit or
1573 * textured-quad paths.
1575 matching_base_formats
=
1576 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1577 _mesa_get_format_base_format(texImage
->TexFormat
));
1578 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1580 if (ctx
->_ImageTransferState
== 0x0) {
1582 if (matching_base_formats
&&
1583 src_format
== dest_format
&&
1586 /* use surface_copy() / blit */
1588 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1589 stImage
->face
, stImage
->level
,
1591 PIPE_BIND_BLIT_DESTINATION
);
1593 /* for surface_copy(), y=0=top, always */
1594 pipe
->surface_copy(pipe
,
1603 use_fallback
= GL_FALSE
;
1605 else if (format_writemask
&&
1606 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1607 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1608 screen
->is_format_supported(screen
, src_format
,
1610 PIPE_BIND_SAMPLER_VIEW
,
1612 screen
->is_format_supported(screen
, dest_format
,
1614 PIPE_BIND_RENDER_TARGET
,
1616 /* draw textured quad to do the copy */
1619 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1620 stImage
->face
, stImage
->level
,
1622 PIPE_BIND_BLIT_DESTINATION
);
1625 srcY1
= strb
->Base
.Height
- srcY
- height
;
1626 srcY0
= srcY1
+ height
;
1630 srcY1
= srcY0
+ height
;
1632 util_blit_pixels_writemask(ctx
->st
->blit
,
1634 st_get_renderbuffer_sampler_view(strb
, pipe
),
1636 srcX
+ width
, srcY1
,
1639 destX
+ width
, destY
+ height
,
1640 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1642 use_fallback
= GL_FALSE
;
1646 pipe_surface_reference(&dest_surface
, NULL
);
1650 /* software fallback */
1651 fallback_copy_texsubimage(ctx
, target
, level
,
1652 strb
, stImage
, texBaseFormat
,
1653 destX
, destY
, destZ
,
1654 srcX
, srcY
, width
, height
);
1661 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1662 GLenum internalFormat
,
1663 GLint x
, GLint y
, GLsizei width
, GLint border
)
1665 struct gl_texture_unit
*texUnit
=
1666 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1667 struct gl_texture_object
*texObj
=
1668 _mesa_select_tex_object(ctx
, texUnit
, target
);
1669 struct gl_texture_image
*texImage
=
1670 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1672 /* Setup or redefine the texture object, texture and texture
1673 * image. Don't populate yet.
1675 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1677 GL_RGBA
, CHAN_TYPE
, NULL
,
1678 &ctx
->DefaultPacking
, texObj
, texImage
);
1680 st_copy_texsubimage(ctx
, target
, level
,
1681 0, 0, 0, /* destX,Y,Z */
1682 x
, y
, width
, 1); /* src X, Y, size */
1687 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1688 GLenum internalFormat
,
1689 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1692 struct gl_texture_unit
*texUnit
=
1693 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1694 struct gl_texture_object
*texObj
=
1695 _mesa_select_tex_object(ctx
, texUnit
, target
);
1696 struct gl_texture_image
*texImage
=
1697 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1699 /* Setup or redefine the texture object, texture and texture
1700 * image. Don't populate yet.
1702 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1703 width
, height
, border
,
1704 GL_RGBA
, CHAN_TYPE
, NULL
,
1705 &ctx
->DefaultPacking
, texObj
, texImage
);
1707 st_copy_texsubimage(ctx
, target
, level
,
1708 0, 0, 0, /* destX,Y,Z */
1709 x
, y
, width
, height
); /* src X, Y, size */
1714 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1715 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1717 const GLint yoffset
= 0, zoffset
= 0;
1718 const GLsizei height
= 1;
1719 st_copy_texsubimage(ctx
, target
, level
,
1720 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1721 x
, y
, width
, height
); /* src X, Y, size */
1726 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1727 GLint xoffset
, GLint yoffset
,
1728 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1730 const GLint zoffset
= 0;
1731 st_copy_texsubimage(ctx
, target
, level
,
1732 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1733 x
, y
, width
, height
); /* src X, Y, size */
1738 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1739 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1740 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1742 st_copy_texsubimage(ctx
, target
, level
,
1743 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1744 x
, y
, width
, height
); /* src X, Y, size */
1749 copy_image_data_to_texture(struct st_context
*st
,
1750 struct st_texture_object
*stObj
,
1752 struct st_texture_image
*stImage
)
1755 /* Copy potentially with the blitter:
1757 st_texture_image_copy(st
->pipe
,
1758 stObj
->pt
, dstLevel
, /* dest texture, level */
1759 stImage
->pt
, /* src texture */
1762 pipe_resource_reference(&stImage
->pt
, NULL
);
1764 else if (stImage
->base
.Data
) {
1765 /* More straightforward upload.
1767 st_teximage_flush_before_map(st
, stObj
->pt
, stImage
->face
, dstLevel
,
1768 PIPE_TRANSFER_WRITE
);
1770 st_texture_image_data(st
,
1775 stImage
->base
.RowStride
*
1776 util_format_get_blocksize(stObj
->pt
->format
),
1777 stImage
->base
.RowStride
*
1778 stImage
->base
.Height
*
1779 util_format_get_blocksize(stObj
->pt
->format
));
1780 _mesa_align_free(stImage
->base
.Data
);
1781 stImage
->base
.Data
= NULL
;
1784 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1789 * Called during state validation. When this function is finished,
1790 * the texture object should be ready for rendering.
1791 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1794 st_finalize_texture(GLcontext
*ctx
,
1795 struct pipe_context
*pipe
,
1796 struct gl_texture_object
*tObj
,
1797 GLboolean
*needFlush
)
1799 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1800 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1801 GLuint blockSize
, face
;
1802 struct st_texture_image
*firstImage
;
1804 *needFlush
= GL_FALSE
;
1806 if (stObj
->base
._Complete
) {
1807 /* The texture is complete and we know exactly how many mipmap levels
1808 * are present/needed. This is conditional because we may be called
1809 * from the st_generate_mipmap() function when the texture object is
1810 * incomplete. In that case, we'll have set stObj->lastLevel before
1813 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1814 stObj
->base
.MinFilter
== GL_NEAREST
)
1815 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1817 stObj
->lastLevel
= stObj
->base
._MaxLevel
- stObj
->base
.BaseLevel
;
1820 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1822 /* If both firstImage and stObj point to a texture which can contain
1823 * all active images, favour firstImage. Note that because of the
1824 * completeness requirement, we know that the image dimensions
1827 if (firstImage
->pt
&&
1828 firstImage
->pt
!= stObj
->pt
&&
1829 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1830 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1831 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1834 /* bytes per pixel block (blocks are usually 1x1) */
1835 blockSize
= _mesa_get_format_bytes(firstImage
->base
.TexFormat
);
1837 /* If we already have a gallium texture, check that it matches the texture
1838 * object's format, target, size, num_levels, etc.
1841 const enum pipe_format fmt
=
1842 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1843 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1844 stObj
->pt
->format
!= fmt
||
1845 stObj
->pt
->last_level
< stObj
->lastLevel
||
1846 stObj
->pt
->width0
!= firstImage
->base
.Width2
||
1847 stObj
->pt
->height0
!= firstImage
->base
.Height2
||
1848 stObj
->pt
->depth0
!= firstImage
->base
.Depth2
)
1850 pipe_resource_reference(&stObj
->pt
, NULL
);
1851 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1852 ctx
->st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1856 /* May need to create a new gallium texture:
1859 const enum pipe_format fmt
=
1860 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1861 GLuint bindings
= default_bindings(ctx
->st
, fmt
);
1863 stObj
->pt
= st_texture_create(ctx
->st
,
1864 gl_target_to_pipe(stObj
->base
.Target
),
1867 firstImage
->base
.Width2
,
1868 firstImage
->base
.Height2
,
1869 firstImage
->base
.Depth2
,
1873 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1878 /* Pull in any images not in the object's texture:
1880 for (face
= 0; face
< nr_faces
; face
++) {
1882 for (level
= 0; level
<= stObj
->lastLevel
; level
++) {
1883 struct st_texture_image
*stImage
=
1884 st_texture_image(stObj
->base
.Image
[face
][stObj
->base
.BaseLevel
+ level
]);
1886 /* Need to import images in main memory or held in other textures.
1888 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1889 copy_image_data_to_texture(ctx
->st
, stObj
, level
, stImage
);
1890 *needFlush
= GL_TRUE
;
1900 * Returns pointer to a default/dummy texture.
1901 * This is typically used when the current shader has tex/sample instructions
1902 * but the user has not provided a (any) texture(s).
1904 struct gl_texture_object
*
1905 st_get_default_texture(struct st_context
*st
)
1907 if (!st
->default_texture
) {
1908 static const GLenum target
= GL_TEXTURE_2D
;
1909 GLubyte pixels
[16][16][4];
1910 struct gl_texture_object
*texObj
;
1911 struct gl_texture_image
*texImg
;
1914 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1915 * when attempting to sample incomplete textures.
1917 for (i
= 0; i
< 16; i
++) {
1918 for (j
= 0; j
< 16; j
++) {
1919 pixels
[i
][j
][0] = 0;
1920 pixels
[i
][j
][1] = 0;
1921 pixels
[i
][j
][2] = 0;
1922 pixels
[i
][j
][3] = 255;
1926 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1928 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1930 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1931 16, 16, 1, 0, /* w, h, d, border */
1934 st_TexImage(st
->ctx
, 2, target
,
1935 0, GL_RGBA
, /* level, intformat */
1936 16, 16, 1, 0, /* w, h, d, border */
1937 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1938 &st
->ctx
->DefaultPacking
,
1942 texObj
->MinFilter
= GL_NEAREST
;
1943 texObj
->MagFilter
= GL_NEAREST
;
1944 texObj
->_Complete
= GL_TRUE
;
1946 st
->default_texture
= texObj
;
1948 return st
->default_texture
;
1953 st_init_texture_functions(struct dd_function_table
*functions
)
1955 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1956 functions
->TexImage1D
= st_TexImage1D
;
1957 functions
->TexImage2D
= st_TexImage2D
;
1958 functions
->TexImage3D
= st_TexImage3D
;
1959 functions
->TexSubImage1D
= st_TexSubImage1D
;
1960 functions
->TexSubImage2D
= st_TexSubImage2D
;
1961 functions
->TexSubImage3D
= st_TexSubImage3D
;
1962 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1963 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1964 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1965 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1966 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1967 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1968 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1969 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1970 functions
->GenerateMipmap
= st_generate_mipmap
;
1972 functions
->GetTexImage
= st_GetTexImage
;
1974 /* compressed texture functions */
1975 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1976 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1978 functions
->NewTextureObject
= st_NewTextureObject
;
1979 functions
->NewTextureImage
= st_NewTextureImage
;
1980 functions
->DeleteTexture
= st_DeleteTextureObject
;
1981 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1982 functions
->UpdateTexturePalette
= 0;
1984 functions
->TextureMemCpy
= do_memcpy
;
1986 /* XXX Temporary until we can query pipe's texture sizes */
1987 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;