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_sampler.h"
67 #include "util/u_math.h"
70 #define DBG if (0) printf
73 static enum pipe_texture_target
74 gl_target_to_pipe(GLenum target
)
78 return PIPE_TEXTURE_1D
;
81 case GL_TEXTURE_RECTANGLE_NV
:
82 return PIPE_TEXTURE_2D
;
85 return PIPE_TEXTURE_3D
;
87 case GL_TEXTURE_CUBE_MAP_ARB
:
88 return PIPE_TEXTURE_CUBE
;
97 /** called via ctx->Driver.NewTextureImage() */
98 static struct gl_texture_image
*
99 st_NewTextureImage(GLcontext
* ctx
)
101 DBG("%s\n", __FUNCTION__
);
103 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
107 /** called via ctx->Driver.NewTextureObject() */
108 static struct gl_texture_object
*
109 st_NewTextureObject(GLcontext
* ctx
, GLuint name
, GLenum target
)
111 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
113 DBG("%s\n", __FUNCTION__
);
114 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
119 /** called via ctx->Driver.DeleteTextureImage() */
121 st_DeleteTextureObject(GLcontext
*ctx
,
122 struct gl_texture_object
*texObj
)
124 struct st_texture_object
*stObj
= st_texture_object(texObj
);
126 pipe_texture_reference(&stObj
->pt
, NULL
);
127 if (stObj
->sampler_view
)
128 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
130 _mesa_delete_texture_object(ctx
, texObj
);
134 /** called via ctx->Driver.FreeTexImageData() */
136 st_FreeTextureImageData(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
138 struct st_texture_image
*stImage
= st_texture_image(texImage
);
140 DBG("%s\n", __FUNCTION__
);
143 pipe_texture_reference(&stImage
->pt
, NULL
);
146 if (texImage
->Data
) {
147 _mesa_align_free(texImage
->Data
);
148 texImage
->Data
= NULL
;
154 * From linux kernel i386 header files, copes with odd sizes better
155 * than COPY_DWORDS would:
156 * XXX Put this in src/mesa/main/imports.h ???
158 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
160 __memcpy(void *to
, const void *from
, size_t n
)
163 __asm__
__volatile__("rep ; movsl\n\t"
167 "1:\ttestb $1,%b4\n\t"
169 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
170 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
175 #define __memcpy(a,b,c) memcpy(a,b,c)
180 * The system memcpy (at least on ubuntu 5.10) has problems copying
181 * to agp (writecombined) memory from a source which isn't 64-byte
182 * aligned - there is a 4x performance falloff.
184 * The x86 __memcpy is immune to this but is slightly slower
185 * (10%-ish) than the system memcpy.
187 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
188 * isn't much faster than x86_memcpy for agp copies.
190 * TODO: switch dynamically.
193 do_memcpy(void *dest
, const void *src
, size_t n
)
195 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
196 return __memcpy(dest
, src
, n
);
199 return memcpy(dest
, src
, n
);
204 * Return default texture usage bitmask for the given texture format.
207 default_usage(enum pipe_format fmt
)
209 GLuint usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
210 if (util_format_is_depth_or_stencil(fmt
))
211 usage
|= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
213 usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
219 * Allocate a pipe_texture object for the given st_texture_object using
220 * the given st_texture_image to guess the mipmap size/levels.
223 * Otherwise, store it in memory if (Border != 0) or (any dimension ==
226 * Otherwise, if max_level >= level >= min_level, create texture with
227 * space for images from min_level down to max_level.
229 * Otherwise, create texture with space for images from (level 0)..(1x1).
230 * Consider pruning this texture at a validation if the saving is worth it.
233 guess_and_alloc_texture(struct st_context
*st
,
234 struct st_texture_object
*stObj
,
235 const struct st_texture_image
*stImage
)
239 GLuint width
= stImage
->base
.Width2
; /* size w/out border */
240 GLuint height
= stImage
->base
.Height2
;
241 GLuint depth
= stImage
->base
.Depth2
;
243 enum pipe_format fmt
;
245 DBG("%s\n", __FUNCTION__
);
250 (GLint
) stImage
->level
> stObj
->base
.BaseLevel
&&
251 (stImage
->base
.Width
== 1 ||
252 (stObj
->base
.Target
!= GL_TEXTURE_1D
&&
253 stImage
->base
.Height
== 1) ||
254 (stObj
->base
.Target
== GL_TEXTURE_3D
&&
255 stImage
->base
.Depth
== 1)))
258 /* If this image disrespects BaseLevel, allocate from level zero.
259 * Usually BaseLevel == 0, so it's unlikely to happen.
261 if ((GLint
) stImage
->level
< stObj
->base
.BaseLevel
)
264 firstLevel
= stObj
->base
.BaseLevel
;
267 /* Figure out image dimensions at start level.
269 for (i
= stImage
->level
; i
> firstLevel
; i
--) {
278 if (width
== 0 || height
== 0 || depth
== 0) {
279 /* no texture needed */
283 /* Guess a reasonable value for lastLevel. This is probably going
284 * to be wrong fairly often and might mean that we have to look at
285 * resizable buffers, or require that buffers implement lazy
286 * pagetable arrangements.
288 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
289 stObj
->base
.MinFilter
== GL_LINEAR
||
290 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
291 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
292 !stObj
->base
.GenerateMipmap
&&
293 stImage
->level
== firstLevel
) {
294 /* only alloc space for a single mipmap level */
295 lastLevel
= firstLevel
;
298 /* alloc space for a full mipmap */
299 GLuint l2width
= util_logbase2(width
);
300 GLuint l2height
= util_logbase2(height
);
301 GLuint l2depth
= util_logbase2(depth
);
302 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
305 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
307 usage
= default_usage(fmt
);
309 stObj
->pt
= st_texture_create(st
,
310 gl_target_to_pipe(stObj
->base
.Target
),
318 stObj
->pipe
= st
->pipe
;
320 DBG("%s - success\n", __FUNCTION__
);
325 * Adjust pixel unpack params and image dimensions to strip off the
327 * Gallium doesn't support texture borders. They've seldem been used
328 * and seldom been implemented correctly anyway.
329 * \param unpackNew returns the new pixel unpack parameters
332 strip_texture_border(GLint border
,
333 GLint
*width
, GLint
*height
, GLint
*depth
,
334 const struct gl_pixelstore_attrib
*unpack
,
335 struct gl_pixelstore_attrib
*unpackNew
)
337 assert(border
> 0); /* sanity check */
339 *unpackNew
= *unpack
;
341 if (unpackNew
->RowLength
== 0)
342 unpackNew
->RowLength
= *width
;
344 if (depth
&& unpackNew
->ImageHeight
== 0)
345 unpackNew
->ImageHeight
= *height
;
347 unpackNew
->SkipPixels
+= border
;
349 unpackNew
->SkipRows
+= border
;
351 unpackNew
->SkipImages
+= border
;
354 *width
= *width
- 2 * border
;
355 if (height
&& *height
>= 3)
356 *height
= *height
- 2 * border
;
357 if (depth
&& *depth
>= 3)
358 *depth
= *depth
- 2 * border
;
363 * Try to do texture compression via rendering. If the Gallium driver
364 * can render into a compressed surface this will allow us to do texture
366 * \return GL_TRUE for success, GL_FALSE for failure
369 compress_with_blit(GLcontext
* ctx
,
370 GLenum target
, GLint level
,
371 GLint xoffset
, GLint yoffset
, GLint zoffset
,
372 GLint width
, GLint height
, GLint depth
,
373 GLenum format
, GLenum type
, const void *pixels
,
374 const struct gl_pixelstore_attrib
*unpack
,
375 struct gl_texture_image
*texImage
)
377 const GLuint dstImageOffsets
[1] = {0};
378 struct st_texture_image
*stImage
= st_texture_image(texImage
);
379 struct pipe_context
*pipe
= ctx
->st
->pipe
;
380 struct pipe_screen
*screen
= pipe
->screen
;
381 gl_format mesa_format
;
382 struct pipe_texture templ
;
383 struct pipe_texture
*src_tex
;
384 struct pipe_sampler_view view_templ
;
385 struct pipe_sampler_view
*src_view
;
386 struct pipe_surface
*dst_surface
;
387 struct pipe_transfer
*tex_xfer
;
391 /* XXX: Can this happen? Should we assert? */
395 /* get destination surface (in the compressed texture) */
396 dst_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
397 stImage
->face
, stImage
->level
, 0,
398 PIPE_BUFFER_USAGE_GPU_WRITE
);
400 /* can't render into this format (or other problem) */
404 /* Choose format for the temporary RGBA texture image.
406 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
411 /* Create the temporary source texture
413 memset(&templ
, 0, sizeof(templ
));
414 templ
.target
= PIPE_TEXTURE_2D
;
415 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
);
416 templ
.width0
= width
;
417 templ
.height0
= height
;
419 templ
.last_level
= 0;
420 templ
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
421 src_tex
= screen
->texture_create(screen
, &templ
);
426 /* Put user's tex data into the temporary texture
428 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
), src_tex
,
429 0, 0, 0, /* face, level are zero */
431 0, 0, width
, height
); /* x, y, w, h */
432 map
= pipe
->transfer_map(pipe
, tex_xfer
);
434 _mesa_texstore(ctx
, 2, GL_RGBA
, mesa_format
,
436 0, 0, 0, /* dest x/y/z offset */
437 tex_xfer
->stride
, /* dest row stride (bytes) */
438 dstImageOffsets
, /* image offsets (for 3D only) */
439 width
, height
, 1, /* size */
440 format
, type
, /* source format/type */
441 pixels
, /* source data */
442 unpack
); /* source data packing */
444 pipe
->transfer_unmap(pipe
, tex_xfer
);
445 pipe
->tex_transfer_destroy(pipe
, tex_xfer
);
447 /* Create temporary sampler view */
448 u_sampler_view_default_template(&view_templ
,
451 src_view
= pipe
->create_sampler_view(pipe
, src_tex
, &view_templ
);
454 /* copy / compress image */
455 util_blit_pixels_tex(ctx
->st
->blit
,
456 src_view
, /* sampler view (src) */
457 0, 0, /* src x0, y0 */
458 width
, height
, /* src x1, y1 */
459 dst_surface
, /* pipe_surface (dst) */
460 xoffset
, yoffset
, /* dst x0, y0 */
461 xoffset
+ width
, /* dst x1 */
462 yoffset
+ height
, /* dst y1 */
464 PIPE_TEX_MIPFILTER_NEAREST
);
466 pipe_surface_reference(&dst_surface
, NULL
);
467 pipe_texture_reference(&src_tex
, NULL
);
468 pipe_sampler_view_reference(&src_view
, NULL
);
475 * Do glTexImage1/2/3D().
478 st_TexImage(GLcontext
* ctx
,
480 GLenum target
, GLint level
,
481 GLint internalFormat
,
482 GLint width
, GLint height
, GLint depth
,
484 GLenum format
, GLenum type
, const void *pixels
,
485 const struct gl_pixelstore_attrib
*unpack
,
486 struct gl_texture_object
*texObj
,
487 struct gl_texture_image
*texImage
,
488 GLsizei imageSize
, GLboolean compressed_src
)
490 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
491 struct st_texture_object
*stObj
= st_texture_object(texObj
);
492 struct st_texture_image
*stImage
= st_texture_image(texImage
);
493 GLint postConvWidth
, postConvHeight
;
494 GLint texelBytes
, sizeInBytes
;
495 GLuint dstRowStride
= 0;
496 struct gl_pixelstore_attrib unpackNB
;
497 enum pipe_transfer_usage transfer_usage
= 0;
499 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
500 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
502 /* switch to "normal" */
503 if (stObj
->surface_based
) {
504 _mesa_clear_texture_object(ctx
, texObj
);
505 stObj
->surface_based
= GL_FALSE
;
508 /* gallium does not support texture borders, strip it off */
510 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
512 texImage
->Width
= width
;
513 texImage
->Height
= height
;
514 texImage
->Depth
= depth
;
515 texImage
->Border
= 0;
519 postConvWidth
= width
;
520 postConvHeight
= height
;
522 stImage
->face
= _mesa_tex_target_to_face(target
);
523 stImage
->level
= level
;
526 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
527 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
532 _mesa_set_fetch_functions(texImage
, dims
);
534 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
535 /* must be a compressed format */
539 texelBytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
541 /* Minimum pitch of 32 bytes */
542 if (postConvWidth
* texelBytes
< 32) {
543 postConvWidth
= 32 / texelBytes
;
544 texImage
->RowStride
= postConvWidth
;
547 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
548 /*assert(texImage->RowStride == postConvWidth);*/
551 /* Release the reference to a potentially orphaned buffer.
552 * Release any old malloced memory.
555 pipe_texture_reference(&stImage
->pt
, NULL
);
556 assert(!texImage
->Data
);
558 else if (texImage
->Data
) {
559 _mesa_align_free(texImage
->Data
);
563 * See if the new image is somehow incompatible with the existing
564 * mipmap. If so, free the old mipmap.
567 if (stObj
->teximage_realloc
||
568 level
> (GLint
) stObj
->pt
->last_level
||
569 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
570 stImage
->face
, stImage
->level
)) {
572 pipe_texture_reference(&stObj
->pt
, NULL
);
574 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
575 stObj
->teximage_realloc
= FALSE
;
579 if (width
== 0 || height
== 0 || depth
== 0) {
580 /* stop after freeing old image */
585 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
587 /* Probably out of memory.
588 * Try flushing any pending rendering, then retry.
591 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
593 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
599 assert(!stImage
->pt
);
602 st_texture_match_image(stObj
->pt
, &stImage
->base
,
603 stImage
->face
, stImage
->level
)) {
605 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
610 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
612 /* st_CopyTexImage calls this function with pixels == NULL, with
613 * the expectation that the texture will be set up but nothing
614 * more will be done. This is where those calls return:
616 if (compressed_src
) {
617 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
619 "glCompressedTexImage");
622 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
624 pixels
, unpack
, "glTexImage");
627 /* Note: we can't check for pixels==NULL until after we've allocated
628 * memory for the texture.
631 /* See if we can do texture compression with a blit/render.
633 if (!compressed_src
&&
635 _mesa_is_format_compressed(texImage
->TexFormat
) &&
636 screen
->is_format_supported(screen
,
639 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
643 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
644 format
, type
, pixels
, unpack
, texImage
)) {
650 if (format
== GL_DEPTH_COMPONENT
&&
651 util_format_is_depth_and_stencil(stImage
->pt
->format
))
652 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
654 transfer_usage
= PIPE_TRANSFER_WRITE
;
656 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
657 transfer_usage
, 0, 0,
659 stImage
->base
.Height
);
660 if(stImage
->transfer
)
661 dstRowStride
= stImage
->transfer
->stride
;
664 /* Allocate regular memory and store the image there temporarily. */
665 if (_mesa_is_format_compressed(texImage
->TexFormat
)) {
666 sizeInBytes
= _mesa_format_image_size(texImage
->TexFormat
,
670 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
674 dstRowStride
= postConvWidth
* texelBytes
;
675 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
678 texImage
->Data
= _mesa_align_malloc(sizeInBytes
, 16);
681 if (!texImage
->Data
) {
682 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
689 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
690 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
692 /* Copy data. Would like to know when it's ok for us to eg. use
693 * the blitter to copy. Or, use the hardware to do the format
694 * conversion and copy:
696 if (compressed_src
) {
697 const GLuint srcImageStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
698 if(dstRowStride
== srcImageStride
)
699 memcpy(texImage
->Data
, pixels
, imageSize
);
702 char *dst
= texImage
->Data
;
703 const char *src
= pixels
;
704 GLuint i
, bw
, bh
, lines
;
705 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
706 lines
= (height
+ bh
- 1) / bh
;
708 for(i
= 0; i
< lines
; ++i
)
710 memcpy(dst
, src
, srcImageStride
);
712 src
+= srcImageStride
;
717 const GLuint srcImageStride
=
718 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
720 const GLubyte
*src
= (const GLubyte
*) pixels
;
722 for (i
= 0; i
< depth
; i
++) {
723 if (!_mesa_texstore(ctx
, dims
,
724 texImage
->_BaseFormat
,
727 0, 0, 0, /* dstX/Y/Zoffset */
729 texImage
->ImageOffsets
,
731 format
, type
, src
, unpack
)) {
732 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
735 if (stImage
->pt
&& i
+ 1 < depth
) {
736 /* unmap this slice */
737 st_texture_image_unmap(ctx
->st
, stImage
);
738 /* map next slice of 3D texture */
739 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
740 transfer_usage
, 0, 0,
742 stImage
->base
.Height
);
743 src
+= srcImageStride
;
749 _mesa_unmap_teximage_pbo(ctx
, unpack
);
751 if (stImage
->pt
&& texImage
->Data
) {
752 st_texture_image_unmap(ctx
->st
, stImage
);
753 texImage
->Data
= NULL
;
759 st_TexImage3D(GLcontext
* ctx
,
760 GLenum target
, GLint level
,
761 GLint internalFormat
,
762 GLint width
, GLint height
, GLint depth
,
764 GLenum format
, GLenum type
, const void *pixels
,
765 const struct gl_pixelstore_attrib
*unpack
,
766 struct gl_texture_object
*texObj
,
767 struct gl_texture_image
*texImage
)
769 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
770 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
776 st_TexImage2D(GLcontext
* ctx
,
777 GLenum target
, GLint level
,
778 GLint internalFormat
,
779 GLint width
, GLint height
, GLint border
,
780 GLenum format
, GLenum type
, const void *pixels
,
781 const struct gl_pixelstore_attrib
*unpack
,
782 struct gl_texture_object
*texObj
,
783 struct gl_texture_image
*texImage
)
785 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
786 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
791 st_TexImage1D(GLcontext
* ctx
,
792 GLenum target
, GLint level
,
793 GLint internalFormat
,
794 GLint width
, GLint border
,
795 GLenum format
, GLenum type
, const void *pixels
,
796 const struct gl_pixelstore_attrib
*unpack
,
797 struct gl_texture_object
*texObj
,
798 struct gl_texture_image
*texImage
)
800 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
801 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
806 st_CompressedTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
807 GLint internalFormat
,
808 GLint width
, GLint height
, GLint border
,
809 GLsizei imageSize
, const GLvoid
*data
,
810 struct gl_texture_object
*texObj
,
811 struct gl_texture_image
*texImage
)
813 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
814 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
820 * glGetTexImage() helper: decompress a compressed texture by rendering
821 * a textured quad. Store the results in the user's buffer.
824 decompress_with_blit(GLcontext
* ctx
, GLenum target
, GLint level
,
825 GLenum format
, GLenum type
, GLvoid
*pixels
,
826 struct gl_texture_object
*texObj
,
827 struct gl_texture_image
*texImage
)
829 struct pipe_context
*pipe
= ctx
->st
->pipe
;
830 struct pipe_screen
*screen
= pipe
->screen
;
831 struct st_texture_image
*stImage
= st_texture_image(texImage
);
832 struct st_texture_object
*stObj
= st_texture_object(texObj
);
833 struct pipe_sampler_view
*src_view
= st_get_stobj_sampler_view(stObj
);
834 const GLuint width
= texImage
->Width
;
835 const GLuint height
= texImage
->Height
;
836 struct pipe_surface
*dst_surface
;
837 struct pipe_texture
*dst_texture
;
838 struct pipe_transfer
*tex_xfer
;
840 /* create temp / dest surface */
841 if (!util_create_rgba_surface(screen
, width
, height
,
842 &dst_texture
, &dst_surface
)) {
843 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
844 "in decompress_with_blit()");
848 /* blit/render/decompress */
849 util_blit_pixels_tex(ctx
->st
->blit
,
850 src_view
, /* pipe_texture (src) */
851 0, 0, /* src x0, y0 */
852 width
, height
, /* src x1, y1 */
853 dst_surface
, /* pipe_surface (dst) */
854 0, 0, /* dst x0, y0 */
855 width
, height
, /* dst x1, y1 */
857 PIPE_TEX_MIPFILTER_NEAREST
);
859 /* map the dst_surface so we can read from it */
860 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
),
861 dst_texture
, 0, 0, 0,
863 0, 0, width
, height
);
865 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
867 /* copy/pack data into user buffer */
868 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
870 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
871 ubyte
*map
= pipe
->transfer_map(pipe
, tex_xfer
);
873 for (row
= 0; row
< height
; row
++) {
874 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
875 height
, format
, type
, row
, 0);
876 memcpy(dest
, map
, bytesPerRow
);
877 map
+= tex_xfer
->stride
;
879 pipe
->transfer_unmap(pipe
, tex_xfer
);
882 /* format translation via floats */
884 for (row
= 0; row
< height
; row
++) {
885 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
886 GLfloat rgba
[4 * MAX_WIDTH
];
887 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
888 height
, format
, type
, row
, 0);
890 if (ST_DEBUG
& DEBUG_FALLBACK
)
891 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
893 /* get float[4] rgba row from surface */
894 pipe_get_tile_rgba(pipe
, tex_xfer
, 0, row
, width
, 1, rgba
);
896 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
897 type
, dest
, &ctx
->Pack
, transferOps
);
901 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
903 screen
->tex_transfer_destroy(tex_xfer
);
905 /* destroy the temp / dest surface */
906 util_destroy_rgba_surface(dst_texture
, dst_surface
);
912 * Need to map texture image into memory before copying image data,
916 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
917 GLenum format
, GLenum type
, GLvoid
* pixels
,
918 struct gl_texture_object
*texObj
,
919 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
921 struct st_texture_image
*stImage
= st_texture_image(texImage
);
922 const GLuint dstImageStride
=
923 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
929 util_format_is_compressed(stImage
->pt
->format
) &&
931 /* Need to decompress the texture.
932 * We'll do this by rendering a textured quad.
933 * Note that we only expect RGBA formats (no Z/depth formats).
935 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
942 /* Image is stored in hardware format in a buffer managed by the
943 * kernel. Need to explicitly map and unmap it.
945 unsigned face
= _mesa_tex_target_to_face(target
);
947 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
950 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
951 PIPE_TRANSFER_READ
, 0, 0,
953 stImage
->base
.Height
);
954 texImage
->RowStride
= stImage
->transfer
->stride
/ util_format_get_blocksize(stImage
->pt
->format
);
957 /* Otherwise, the image should actually be stored in
958 * texImage->Data. This is pretty confusing for
959 * everybody, I'd much prefer to separate the two functions of
960 * texImage->Data - storage for texture images in main memory
961 * and access (ie mappings) of images. In other words, we'd
962 * create a new texImage->Map field and leave Data simply for
965 assert(texImage
->Data
);
968 depth
= texImage
->Depth
;
971 dest
= (GLubyte
*) pixels
;
973 for (i
= 0; i
< depth
; i
++) {
974 if (compressed_dst
) {
975 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
979 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
983 if (stImage
->pt
&& i
+ 1 < depth
) {
984 /* unmap this slice */
985 st_texture_image_unmap(ctx
->st
, stImage
);
986 /* map next slice of 3D texture */
987 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
988 PIPE_TRANSFER_READ
, 0, 0,
990 stImage
->base
.Height
);
991 dest
+= dstImageStride
;
995 texImage
->Depth
= depth
;
999 st_texture_image_unmap(ctx
->st
, stImage
);
1000 texImage
->Data
= NULL
;
1006 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
1007 GLenum format
, GLenum type
, GLvoid
* pixels
,
1008 struct gl_texture_object
*texObj
,
1009 struct gl_texture_image
*texImage
)
1011 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1017 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
1019 struct gl_texture_object
*texObj
,
1020 struct gl_texture_image
*texImage
)
1022 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1029 st_TexSubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
1030 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1031 GLint width
, GLint height
, GLint depth
,
1032 GLenum format
, GLenum type
, const void *pixels
,
1033 const struct gl_pixelstore_attrib
*packing
,
1034 struct gl_texture_object
*texObj
,
1035 struct gl_texture_image
*texImage
)
1037 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
1038 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1039 GLuint dstRowStride
;
1040 const GLuint srcImageStride
=
1041 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1044 /* init to silence warning only: */
1045 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1047 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1048 _mesa_lookup_enum_by_nr(target
),
1049 level
, xoffset
, yoffset
, width
, height
);
1052 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1053 type
, pixels
, packing
, "glTexSubImage2D");
1057 /* See if we can do texture compression with a blit/render.
1059 if (!ctx
->Mesa_DXTn
&&
1060 _mesa_is_format_compressed(texImage
->TexFormat
) &&
1061 screen
->is_format_supported(screen
,
1062 stImage
->pt
->format
,
1063 stImage
->pt
->target
,
1064 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
1065 if (compress_with_blit(ctx
, target
, level
,
1066 xoffset
, yoffset
, zoffset
,
1067 width
, height
, depth
,
1068 format
, type
, pixels
, packing
, texImage
)) {
1073 /* Map buffer if necessary. Need to lock to prevent other contexts
1074 * from uploading the buffer under us.
1077 unsigned face
= _mesa_tex_target_to_face(target
);
1079 if (format
== GL_DEPTH_COMPONENT
&&
1080 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1081 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1083 transfer_usage
= PIPE_TRANSFER_WRITE
;
1085 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1087 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
,
1093 if (!texImage
->Data
) {
1094 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1098 src
= (const GLubyte
*) pixels
;
1099 dstRowStride
= stImage
->transfer
->stride
;
1101 for (i
= 0; i
< depth
; i
++) {
1102 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1103 texImage
->TexFormat
,
1107 texImage
->ImageOffsets
,
1109 format
, type
, src
, packing
)) {
1110 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1113 if (stImage
->pt
&& i
+ 1 < depth
) {
1114 /* unmap this slice */
1115 st_texture_image_unmap(ctx
->st
, stImage
);
1116 /* map next slice of 3D texture */
1117 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
,
1122 src
+= srcImageStride
;
1127 _mesa_unmap_teximage_pbo(ctx
, packing
);
1129 if (stImage
->pt
&& texImage
->Data
) {
1130 st_texture_image_unmap(ctx
->st
, stImage
);
1131 texImage
->Data
= NULL
;
1138 st_TexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1139 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1140 GLsizei width
, GLsizei height
, GLsizei depth
,
1141 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1142 const struct gl_pixelstore_attrib
*packing
,
1143 struct gl_texture_object
*texObj
,
1144 struct gl_texture_image
*texImage
)
1146 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1147 width
, height
, depth
, format
, type
,
1148 pixels
, packing
, texObj
, texImage
);
1153 st_TexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1154 GLint xoffset
, GLint yoffset
,
1155 GLsizei width
, GLsizei height
,
1156 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1157 const struct gl_pixelstore_attrib
*packing
,
1158 struct gl_texture_object
*texObj
,
1159 struct gl_texture_image
*texImage
)
1161 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1162 width
, height
, 1, format
, type
,
1163 pixels
, packing
, texObj
, texImage
);
1168 st_TexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1169 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1170 const GLvoid
* pixels
,
1171 const struct gl_pixelstore_attrib
*packing
,
1172 struct gl_texture_object
*texObj
,
1173 struct gl_texture_image
*texImage
)
1175 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1176 format
, type
, pixels
, packing
, texObj
, texImage
);
1181 st_CompressedTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1182 GLint xoffset
, GLsizei width
,
1184 GLsizei imageSize
, const GLvoid
*data
,
1185 struct gl_texture_object
*texObj
,
1186 struct gl_texture_image
*texImage
)
1193 st_CompressedTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1194 GLint xoffset
, GLint yoffset
,
1195 GLsizei width
, GLint height
,
1197 GLsizei imageSize
, const GLvoid
*data
,
1198 struct gl_texture_object
*texObj
,
1199 struct gl_texture_image
*texImage
)
1201 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1205 enum pipe_format pformat
= stImage
->pt
->format
;
1208 unsigned face
= _mesa_tex_target_to_face(target
);
1210 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1211 PIPE_TRANSFER_WRITE
);
1212 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
1213 PIPE_TRANSFER_WRITE
,
1217 srcBlockStride
= util_format_get_stride(pformat
, width
);
1218 dstBlockStride
= stImage
->transfer
->stride
;
1220 assert(stImage
->pt
);
1221 /* TODO find good values for block and strides */
1222 /* TODO also adjust texImage->data for yoffset/xoffset */
1226 if (!texImage
->Data
) {
1227 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1231 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1232 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1233 assert(width
% util_format_get_blockwidth(pformat
) == 0);
1234 assert(height
% util_format_get_blockheight(pformat
) == 0);
1236 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1237 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1238 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1239 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1240 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1244 st_texture_image_unmap(ctx
->st
, stImage
);
1245 texImage
->Data
= NULL
;
1251 st_CompressedTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1252 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1253 GLsizei width
, GLint height
, GLint depth
,
1255 GLsizei imageSize
, const GLvoid
*data
,
1256 struct gl_texture_object
*texObj
,
1257 struct gl_texture_image
*texImage
)
1265 * Do a CopyTexSubImage operation using a read transfer from the source,
1266 * a write transfer to the destination and get_tile()/put_tile() to access
1267 * the pixels/texels.
1269 * Note: srcY=0=TOP of renderbuffer
1272 fallback_copy_texsubimage(GLcontext
*ctx
, GLenum target
, GLint level
,
1273 struct st_renderbuffer
*strb
,
1274 struct st_texture_image
*stImage
,
1276 GLint destX
, GLint destY
, GLint destZ
,
1277 GLint srcX
, GLint srcY
,
1278 GLsizei width
, GLsizei height
)
1280 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1281 struct pipe_transfer
*src_trans
;
1283 enum pipe_transfer_usage transfer_usage
;
1285 if (ST_DEBUG
& DEBUG_FALLBACK
)
1286 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1288 assert(width
<= MAX_WIDTH
);
1290 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1291 srcY
= strb
->Base
.Height
- srcY
- height
;
1294 src_trans
= st_cond_flush_get_tex_transfer( st_context(ctx
),
1301 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1302 baseFormat
== GL_DEPTH_STENCIL
) &&
1303 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1304 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1306 transfer_usage
= PIPE_TRANSFER_WRITE
;
1308 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, 0, 0,
1311 texDest
= st_texture_image_map(ctx
->st
, stImage
, 0, transfer_usage
,
1312 destX
, destY
, width
, height
);
1314 if (baseFormat
== GL_DEPTH_COMPONENT
||
1315 baseFormat
== GL_DEPTH_STENCIL
) {
1316 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1317 ctx
->Pixel
.DepthBias
!= 0.0F
);
1320 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1321 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1330 /* To avoid a large temp memory allocation, do copy row by row */
1331 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1332 uint data
[MAX_WIDTH
];
1333 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1335 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1337 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1343 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1345 if (tempSrc
&& texDest
) {
1346 const GLint dims
= 2;
1347 const GLint dstRowStride
= stImage
->transfer
->stride
;
1348 struct gl_texture_image
*texImage
= &stImage
->base
;
1349 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1351 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1352 unpack
.Invert
= GL_TRUE
;
1355 /* get float/RGBA image from framebuffer */
1356 /* XXX this usually involves a lot of int/float conversion.
1357 * try to avoid that someday.
1359 pipe_get_tile_rgba(pipe
, src_trans
, 0, 0, width
, height
, tempSrc
);
1361 /* Store into texture memory.
1362 * Note that this does some special things such as pixel transfer
1363 * ops and format conversion. In particular, if the dest tex format
1364 * is actually RGBA but the user created the texture as GL_RGB we
1365 * need to fill-in/override the alpha channel with 1.0.
1367 _mesa_texstore(ctx
, dims
,
1368 texImage
->_BaseFormat
,
1369 texImage
->TexFormat
,
1373 texImage
->ImageOffsets
,
1375 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1379 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1386 st_texture_image_unmap(ctx
->st
, stImage
);
1387 pipe
->tex_transfer_destroy(pipe
, src_trans
);
1393 * If the format of the src renderbuffer and the format of the dest
1394 * texture are compatible (in terms of blitting), return a TGSI writemask
1395 * to be used during the blit.
1396 * If the src/dest are incompatible, return 0.
1399 compatible_src_dst_formats(GLcontext
*ctx
,
1400 const struct gl_renderbuffer
*src
,
1401 const struct gl_texture_image
*dst
)
1403 /* Get logical base formats for the src and dest.
1404 * That is, use the user-requested formats and not the actual, device-
1406 * For example, the user may have requested an A8 texture but the
1407 * driver may actually be using an RGBA texture format. When we
1408 * copy/blit to that texture, we only want to copy the Alpha channel
1409 * and not the RGB channels.
1411 * Similarly, when the src FBO was created an RGB format may have been
1412 * requested but the driver actually chose an RGBA format. In that case,
1413 * we don't want to copy the undefined Alpha channel to the dest texture
1414 * (it should be 1.0).
1416 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1417 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1420 * XXX when we have red-only and red/green renderbuffers we'll need
1421 * to add more cases here (or implement a general-purpose routine that
1422 * queries the existance of the R,G,B,A channels in the src and dest).
1424 if (srcFormat
== dstFormat
) {
1425 /* This is the same as matching_base_formats, which should
1426 * always pass, as it did previously.
1428 return TGSI_WRITEMASK_XYZW
;
1430 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1431 /* Make sure that A in the dest is 1. The actual src format
1432 * may be RGBA and have undefined A values.
1434 return TGSI_WRITEMASK_XYZ
;
1436 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1437 /* Make sure that A in the dest is 1. The actual dst format
1438 * may be RGBA and will need A=1 to provide proper alpha values
1439 * when sampled later.
1441 return TGSI_WRITEMASK_XYZ
;
1444 if (ST_DEBUG
& DEBUG_FALLBACK
)
1445 debug_printf("%s failed for src %s, dst %s\n",
1447 _mesa_lookup_enum_by_nr(srcFormat
),
1448 _mesa_lookup_enum_by_nr(dstFormat
));
1459 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1460 * Note that the region to copy has already been clipped so we know we
1461 * won't read from outside the source renderbuffer's bounds.
1463 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1466 st_copy_texsubimage(GLcontext
*ctx
,
1467 GLenum target
, GLint level
,
1468 GLint destX
, GLint destY
, GLint destZ
,
1469 GLint srcX
, GLint srcY
,
1470 GLsizei width
, GLsizei height
)
1472 struct gl_texture_unit
*texUnit
=
1473 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1474 struct gl_texture_object
*texObj
=
1475 _mesa_select_tex_object(ctx
, texUnit
, target
);
1476 struct gl_texture_image
*texImage
=
1477 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1478 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1479 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1480 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1481 struct st_renderbuffer
*strb
;
1482 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1483 struct pipe_screen
*screen
= pipe
->screen
;
1484 enum pipe_format dest_format
, src_format
;
1485 GLboolean use_fallback
= GL_TRUE
;
1486 GLboolean matching_base_formats
;
1487 GLuint format_writemask
;
1488 struct pipe_surface
*dest_surface
= NULL
;
1489 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1491 /* any rendering in progress must flushed before we grab the fb image */
1492 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
1494 /* make sure finalize_textures has been called?
1496 if (0) st_validate_state(ctx
->st
);
1498 /* determine if copying depth or color data */
1499 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1500 texBaseFormat
== GL_DEPTH_STENCIL
) {
1501 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1504 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1505 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1508 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1509 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1537 if (width
< 0 || height
< 0)
1542 assert(strb
->surface
);
1543 assert(stImage
->pt
);
1545 src_format
= strb
->surface
->format
;
1546 dest_format
= stImage
->pt
->format
;
1549 * Determine if the src framebuffer and dest texture have the same
1550 * base format. We need this to detect a case such as the framebuffer
1551 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1552 * texture format stores RGBA we need to set A=1 (overriding the
1553 * framebuffer's alpha values). We can't do that with the blit or
1554 * textured-quad paths.
1556 matching_base_formats
=
1557 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1558 _mesa_get_format_base_format(texImage
->TexFormat
));
1559 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1561 if (ctx
->_ImageTransferState
== 0x0) {
1563 if (pipe
->surface_copy
&&
1564 matching_base_formats
&&
1565 src_format
== dest_format
&&
1568 /* use surface_copy() / blit */
1570 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1571 stImage
->face
, stImage
->level
,
1573 PIPE_BUFFER_USAGE_GPU_WRITE
);
1575 /* for surface_copy(), y=0=top, always */
1576 pipe
->surface_copy(pipe
,
1585 use_fallback
= GL_FALSE
;
1587 else if (format_writemask
&&
1588 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1589 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1590 screen
->is_format_supported(screen
, src_format
,
1592 PIPE_TEXTURE_USAGE_SAMPLER
,
1594 screen
->is_format_supported(screen
, dest_format
,
1596 PIPE_TEXTURE_USAGE_RENDER_TARGET
,
1598 /* draw textured quad to do the copy */
1601 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1602 stImage
->face
, stImage
->level
,
1604 PIPE_BUFFER_USAGE_GPU_WRITE
);
1607 srcY1
= strb
->Base
.Height
- srcY
- height
;
1608 srcY0
= srcY1
+ height
;
1612 srcY1
= srcY0
+ height
;
1614 util_blit_pixels_writemask(ctx
->st
->blit
,
1616 st_renderbuffer_get_sampler_view(strb
, pipe
),
1618 srcX
+ width
, srcY1
,
1621 destX
+ width
, destY
+ height
,
1622 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1624 use_fallback
= GL_FALSE
;
1628 pipe_surface_reference(&dest_surface
, NULL
);
1632 /* software fallback */
1633 fallback_copy_texsubimage(ctx
, target
, level
,
1634 strb
, stImage
, texBaseFormat
,
1635 destX
, destY
, destZ
,
1636 srcX
, srcY
, width
, height
);
1643 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1644 GLenum internalFormat
,
1645 GLint x
, GLint y
, GLsizei width
, GLint border
)
1647 struct gl_texture_unit
*texUnit
=
1648 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1649 struct gl_texture_object
*texObj
=
1650 _mesa_select_tex_object(ctx
, texUnit
, target
);
1651 struct gl_texture_image
*texImage
=
1652 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1654 /* Setup or redefine the texture object, texture and texture
1655 * image. Don't populate yet.
1657 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1659 GL_RGBA
, CHAN_TYPE
, NULL
,
1660 &ctx
->DefaultPacking
, texObj
, texImage
);
1662 st_copy_texsubimage(ctx
, target
, level
,
1663 0, 0, 0, /* destX,Y,Z */
1664 x
, y
, width
, 1); /* src X, Y, size */
1669 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1670 GLenum internalFormat
,
1671 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1674 struct gl_texture_unit
*texUnit
=
1675 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1676 struct gl_texture_object
*texObj
=
1677 _mesa_select_tex_object(ctx
, texUnit
, target
);
1678 struct gl_texture_image
*texImage
=
1679 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1681 /* Setup or redefine the texture object, texture and texture
1682 * image. Don't populate yet.
1684 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1685 width
, height
, border
,
1686 GL_RGBA
, CHAN_TYPE
, NULL
,
1687 &ctx
->DefaultPacking
, texObj
, texImage
);
1689 st_copy_texsubimage(ctx
, target
, level
,
1690 0, 0, 0, /* destX,Y,Z */
1691 x
, y
, width
, height
); /* src X, Y, size */
1696 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1697 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1699 const GLint yoffset
= 0, zoffset
= 0;
1700 const GLsizei height
= 1;
1701 st_copy_texsubimage(ctx
, target
, level
,
1702 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1703 x
, y
, width
, height
); /* src X, Y, size */
1708 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1709 GLint xoffset
, GLint yoffset
,
1710 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1712 const GLint zoffset
= 0;
1713 st_copy_texsubimage(ctx
, target
, level
,
1714 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1715 x
, y
, width
, height
); /* src X, Y, size */
1720 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1721 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1722 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1724 st_copy_texsubimage(ctx
, target
, level
,
1725 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1726 x
, y
, width
, height
); /* src X, Y, size */
1731 copy_image_data_to_texture(struct st_context
*st
,
1732 struct st_texture_object
*stObj
,
1734 struct st_texture_image
*stImage
)
1737 /* Copy potentially with the blitter:
1739 st_texture_image_copy(st
->pipe
,
1740 stObj
->pt
, dstLevel
, /* dest texture, level */
1741 stImage
->pt
, /* src texture */
1744 pipe_texture_reference(&stImage
->pt
, NULL
);
1746 else if (stImage
->base
.Data
) {
1747 /* More straightforward upload.
1749 st_teximage_flush_before_map(st
, stObj
->pt
, stImage
->face
, dstLevel
,
1750 PIPE_TRANSFER_WRITE
);
1752 st_texture_image_data(st
,
1757 stImage
->base
.RowStride
*
1758 util_format_get_blocksize(stObj
->pt
->format
),
1759 stImage
->base
.RowStride
*
1760 stImage
->base
.Height
*
1761 util_format_get_blocksize(stObj
->pt
->format
));
1762 _mesa_align_free(stImage
->base
.Data
);
1763 stImage
->base
.Data
= NULL
;
1766 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
1771 * Called during state validation. When this function is finished,
1772 * the texture object should be ready for rendering.
1773 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1776 st_finalize_texture(GLcontext
*ctx
,
1777 struct pipe_context
*pipe
,
1778 struct gl_texture_object
*tObj
,
1779 GLboolean
*needFlush
)
1781 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1782 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1783 GLuint blockSize
, face
;
1784 struct st_texture_image
*firstImage
;
1786 *needFlush
= GL_FALSE
;
1788 if (stObj
->base
._Complete
) {
1789 /* The texture is complete and we know exactly how many mipmap levels
1790 * are present/needed. This is conditional because we may be called
1791 * from the st_generate_mipmap() function when the texture object is
1792 * incomplete. In that case, we'll have set stObj->lastLevel before
1795 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1796 stObj
->base
.MinFilter
== GL_NEAREST
)
1797 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1799 stObj
->lastLevel
= stObj
->base
._MaxLevel
- stObj
->base
.BaseLevel
;
1802 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1804 /* If both firstImage and stObj point to a texture which can contain
1805 * all active images, favour firstImage. Note that because of the
1806 * completeness requirement, we know that the image dimensions
1809 if (firstImage
->pt
&&
1810 firstImage
->pt
!= stObj
->pt
&&
1811 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1812 pipe_texture_reference(&stObj
->pt
, firstImage
->pt
);
1813 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1816 /* bytes per pixel block (blocks are usually 1x1) */
1817 blockSize
= _mesa_get_format_bytes(firstImage
->base
.TexFormat
);
1819 /* If we already have a gallium texture, check that it matches the texture
1820 * object's format, target, size, num_levels, etc.
1823 const enum pipe_format fmt
=
1824 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1825 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1826 stObj
->pt
->format
!= fmt
||
1827 stObj
->pt
->last_level
< stObj
->lastLevel
||
1828 stObj
->pt
->width0
!= firstImage
->base
.Width2
||
1829 stObj
->pt
->height0
!= firstImage
->base
.Height2
||
1830 stObj
->pt
->depth0
!= firstImage
->base
.Depth2
)
1832 pipe_texture_reference(&stObj
->pt
, NULL
);
1833 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1834 ctx
->st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1838 /* May need to create a new gallium texture:
1841 const enum pipe_format fmt
=
1842 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1843 GLuint usage
= default_usage(fmt
);
1845 stObj
->pt
= st_texture_create(ctx
->st
,
1846 gl_target_to_pipe(stObj
->base
.Target
),
1849 firstImage
->base
.Width2
,
1850 firstImage
->base
.Height2
,
1851 firstImage
->base
.Depth2
,
1855 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1860 /* Pull in any images not in the object's texture:
1862 for (face
= 0; face
< nr_faces
; face
++) {
1864 for (level
= 0; level
<= stObj
->lastLevel
; level
++) {
1865 struct st_texture_image
*stImage
=
1866 st_texture_image(stObj
->base
.Image
[face
][stObj
->base
.BaseLevel
+ level
]);
1868 /* Need to import images in main memory or held in other textures.
1870 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1871 copy_image_data_to_texture(ctx
->st
, stObj
, level
, stImage
);
1872 *needFlush
= GL_TRUE
;
1882 * Returns pointer to a default/dummy texture.
1883 * This is typically used when the current shader has tex/sample instructions
1884 * but the user has not provided a (any) texture(s).
1886 struct gl_texture_object
*
1887 st_get_default_texture(struct st_context
*st
)
1889 if (!st
->default_texture
) {
1890 static const GLenum target
= GL_TEXTURE_2D
;
1891 GLubyte pixels
[16][16][4];
1892 struct gl_texture_object
*texObj
;
1893 struct gl_texture_image
*texImg
;
1896 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1897 * when attempting to sample incomplete textures.
1899 for (i
= 0; i
< 16; i
++) {
1900 for (j
= 0; j
< 16; j
++) {
1901 pixels
[i
][j
][0] = 0;
1902 pixels
[i
][j
][1] = 0;
1903 pixels
[i
][j
][2] = 0;
1904 pixels
[i
][j
][3] = 255;
1908 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1910 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1912 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1913 16, 16, 1, 0, /* w, h, d, border */
1916 st_TexImage(st
->ctx
, 2, target
,
1917 0, GL_RGBA
, /* level, intformat */
1918 16, 16, 1, 0, /* w, h, d, border */
1919 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1920 &st
->ctx
->DefaultPacking
,
1924 texObj
->MinFilter
= GL_NEAREST
;
1925 texObj
->MagFilter
= GL_NEAREST
;
1926 texObj
->_Complete
= GL_TRUE
;
1928 st
->default_texture
= texObj
;
1930 return st
->default_texture
;
1935 st_init_texture_functions(struct dd_function_table
*functions
)
1937 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1938 functions
->TexImage1D
= st_TexImage1D
;
1939 functions
->TexImage2D
= st_TexImage2D
;
1940 functions
->TexImage3D
= st_TexImage3D
;
1941 functions
->TexSubImage1D
= st_TexSubImage1D
;
1942 functions
->TexSubImage2D
= st_TexSubImage2D
;
1943 functions
->TexSubImage3D
= st_TexSubImage3D
;
1944 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1945 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1946 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1947 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1948 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1949 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1950 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1951 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1952 functions
->GenerateMipmap
= st_generate_mipmap
;
1954 functions
->GetTexImage
= st_GetTexImage
;
1956 /* compressed texture functions */
1957 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1958 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1960 functions
->NewTextureObject
= st_NewTextureObject
;
1961 functions
->NewTextureImage
= st_NewTextureImage
;
1962 functions
->DeleteTexture
= st_DeleteTextureObject
;
1963 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1964 functions
->UpdateTexturePalette
= 0;
1966 functions
->TextureMemCpy
= do_memcpy
;
1968 /* XXX Temporary until we can query pipe's texture sizes */
1969 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;