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/image.h"
35 #include "main/imports.h"
36 #include "main/macros.h"
37 #include "main/mipmap.h"
38 #include "main/pixel.h"
39 #include "main/texcompress.h"
40 #include "main/texformat.h"
41 #include "main/texgetimage.h"
42 #include "main/teximage.h"
43 #include "main/texobj.h"
44 #include "main/texstore.h"
46 #include "state_tracker/st_debug.h"
47 #include "state_tracker/st_context.h"
48 #include "state_tracker/st_cb_fbo.h"
49 #include "state_tracker/st_cb_texture.h"
50 #include "state_tracker/st_format.h"
51 #include "state_tracker/st_public.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 "pipe/p_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_surface.h"
64 #include "util/u_math.h"
67 #define DBG if (0) printf
70 static enum pipe_texture_target
71 gl_target_to_pipe(GLenum target
)
75 return PIPE_TEXTURE_1D
;
78 case GL_TEXTURE_RECTANGLE_NV
:
79 return PIPE_TEXTURE_2D
;
82 return PIPE_TEXTURE_3D
;
84 case GL_TEXTURE_CUBE_MAP_ARB
:
85 return PIPE_TEXTURE_CUBE
;
95 * Return nominal bytes per texel for a compressed format, 0 for non-compressed
99 compressed_num_bytes(GLuint mesaFormat
)
102 #if FEATURE_texture_fxt1
103 case MESA_FORMAT_RGB_FXT1
:
104 case MESA_FORMAT_RGBA_FXT1
:
106 #if FEATURE_texture_s3tc
107 case MESA_FORMAT_RGB_DXT1
:
108 case MESA_FORMAT_RGBA_DXT1
:
110 case MESA_FORMAT_RGBA_DXT3
:
111 case MESA_FORMAT_RGBA_DXT5
:
121 is_compressed_mesa_format(const struct gl_texture_format
*format
)
123 switch (format
->MesaFormat
) {
124 case MESA_FORMAT_RGB_DXT1
:
125 case MESA_FORMAT_RGBA_DXT1
:
126 case MESA_FORMAT_RGBA_DXT3
:
127 case MESA_FORMAT_RGBA_DXT5
:
128 case MESA_FORMAT_SRGB_DXT1
:
129 case MESA_FORMAT_SRGBA_DXT1
:
130 case MESA_FORMAT_SRGBA_DXT3
:
131 case MESA_FORMAT_SRGBA_DXT5
:
139 /** called via ctx->Driver.NewTextureImage() */
140 static struct gl_texture_image
*
141 st_NewTextureImage(GLcontext
* ctx
)
143 DBG("%s\n", __FUNCTION__
);
145 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
149 /** called via ctx->Driver.NewTextureObject() */
150 static struct gl_texture_object
*
151 st_NewTextureObject(GLcontext
* ctx
, GLuint name
, GLenum target
)
153 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
155 DBG("%s\n", __FUNCTION__
);
156 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
161 /** called via ctx->Driver.DeleteTextureImage() */
163 st_DeleteTextureObject(GLcontext
*ctx
,
164 struct gl_texture_object
*texObj
)
166 struct st_texture_object
*stObj
= st_texture_object(texObj
);
168 pipe_texture_reference(&stObj
->pt
, NULL
);
170 _mesa_delete_texture_object(ctx
, texObj
);
174 /** called via ctx->Driver.FreeTexImageData() */
176 st_FreeTextureImageData(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
178 struct st_texture_image
*stImage
= st_texture_image(texImage
);
180 DBG("%s\n", __FUNCTION__
);
183 pipe_texture_reference(&stImage
->pt
, NULL
);
186 if (texImage
->Data
) {
187 _mesa_align_free(texImage
->Data
);
188 texImage
->Data
= NULL
;
194 * From linux kernel i386 header files, copes with odd sizes better
195 * than COPY_DWORDS would:
196 * XXX Put this in src/mesa/main/imports.h ???
198 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
200 __memcpy(void *to
, const void *from
, size_t n
)
203 __asm__
__volatile__("rep ; movsl\n\t"
207 "1:\ttestb $1,%b4\n\t"
209 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
210 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
215 #define __memcpy(a,b,c) memcpy(a,b,c)
220 * The system memcpy (at least on ubuntu 5.10) has problems copying
221 * to agp (writecombined) memory from a source which isn't 64-byte
222 * aligned - there is a 4x performance falloff.
224 * The x86 __memcpy is immune to this but is slightly slower
225 * (10%-ish) than the system memcpy.
227 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
228 * isn't much faster than x86_memcpy for agp copies.
230 * TODO: switch dynamically.
233 do_memcpy(void *dest
, const void *src
, size_t n
)
235 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
236 return __memcpy(dest
, src
, n
);
239 return memcpy(dest
, src
, n
);
244 * Return default texture usage bitmask for the given texture format.
247 default_usage(enum pipe_format fmt
)
249 GLuint usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
250 if (pf_is_depth_stencil(fmt
))
251 usage
|= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
253 usage
|= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
259 * Allocate a pipe_texture object for the given st_texture_object using
260 * the given st_texture_image to guess the mipmap size/levels.
263 * Otherwise, store it in memory if (Border != 0) or (any dimension ==
266 * Otherwise, if max_level >= level >= min_level, create texture with
267 * space for images from min_level down to max_level.
269 * Otherwise, create texture with space for images from (level 0)..(1x1).
270 * Consider pruning this texture at a validation if the saving is worth it.
273 guess_and_alloc_texture(struct st_context
*st
,
274 struct st_texture_object
*stObj
,
275 const struct st_texture_image
*stImage
)
279 GLuint width
= stImage
->base
.Width2
; /* size w/out border */
280 GLuint height
= stImage
->base
.Height2
;
281 GLuint depth
= stImage
->base
.Depth2
;
283 enum pipe_format fmt
;
285 DBG("%s\n", __FUNCTION__
);
290 (GLint
) stImage
->level
> stObj
->base
.BaseLevel
&&
291 (stImage
->base
.Width
== 1 ||
292 (stObj
->base
.Target
!= GL_TEXTURE_1D
&&
293 stImage
->base
.Height
== 1) ||
294 (stObj
->base
.Target
== GL_TEXTURE_3D
&&
295 stImage
->base
.Depth
== 1)))
298 /* If this image disrespects BaseLevel, allocate from level zero.
299 * Usually BaseLevel == 0, so it's unlikely to happen.
301 if ((GLint
) stImage
->level
< stObj
->base
.BaseLevel
)
304 firstLevel
= stObj
->base
.BaseLevel
;
307 /* Figure out image dimensions at start level.
309 for (i
= stImage
->level
; i
> firstLevel
; i
--) {
318 if (width
== 0 || height
== 0 || depth
== 0) {
319 /* no texture needed */
323 /* Guess a reasonable value for lastLevel. This is probably going
324 * to be wrong fairly often and might mean that we have to look at
325 * resizable buffers, or require that buffers implement lazy
326 * pagetable arrangements.
328 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
329 stObj
->base
.MinFilter
== GL_LINEAR
||
330 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
331 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
332 !stObj
->base
.GenerateMipmap
&&
333 stImage
->level
== firstLevel
) {
334 /* only alloc space for a single mipmap level */
335 lastLevel
= firstLevel
;
338 /* alloc space for a full mipmap */
339 GLuint l2width
= util_logbase2(width
);
340 GLuint l2height
= util_logbase2(height
);
341 GLuint l2depth
= util_logbase2(depth
);
342 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
345 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
->MesaFormat
);
347 usage
= default_usage(fmt
);
349 stObj
->pt
= st_texture_create(st
,
350 gl_target_to_pipe(stObj
->base
.Target
),
358 DBG("%s - success\n", __FUNCTION__
);
363 * Adjust pixel unpack params and image dimensions to strip off the
365 * Gallium doesn't support texture borders. They've seldem been used
366 * and seldom been implemented correctly anyway.
367 * \param unpackNew returns the new pixel unpack parameters
370 strip_texture_border(GLint border
,
371 GLint
*width
, GLint
*height
, GLint
*depth
,
372 const struct gl_pixelstore_attrib
*unpack
,
373 struct gl_pixelstore_attrib
*unpackNew
)
375 assert(border
> 0); /* sanity check */
377 *unpackNew
= *unpack
;
379 if (unpackNew
->RowLength
== 0)
380 unpackNew
->RowLength
= *width
;
382 if (depth
&& unpackNew
->ImageHeight
== 0)
383 unpackNew
->ImageHeight
= *height
;
385 unpackNew
->SkipPixels
+= border
;
387 unpackNew
->SkipRows
+= border
;
389 unpackNew
->SkipImages
+= border
;
392 *width
= *width
- 2 * border
;
393 if (height
&& *height
>= 3)
394 *height
= *height
- 2 * border
;
395 if (depth
&& *depth
>= 3)
396 *depth
= *depth
- 2 * border
;
401 * Try to do texture compression via rendering. If the Gallium driver
402 * can render into a compressed surface this will allow us to do texture
404 * \return GL_TRUE for success, GL_FALSE for failure
407 compress_with_blit(GLcontext
* ctx
,
408 GLenum target
, GLint level
,
409 GLint xoffset
, GLint yoffset
, GLint zoffset
,
410 GLint width
, GLint height
, GLint depth
,
411 GLenum format
, GLenum type
, const void *pixels
,
412 const struct gl_pixelstore_attrib
*unpack
,
413 struct gl_texture_image
*texImage
)
415 const GLuint dstImageOffsets
[1] = {0};
416 struct st_texture_image
*stImage
= st_texture_image(texImage
);
417 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
418 const struct gl_texture_format
*mesa_format
;
419 struct pipe_texture templ
;
420 struct pipe_texture
*src_tex
;
421 struct pipe_surface
*dst_surface
;
422 struct pipe_transfer
*tex_xfer
;
427 /* XXX: Can this happen? Should we assert? */
431 /* get destination surface (in the compressed texture) */
432 dst_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
433 stImage
->face
, stImage
->level
, 0,
434 PIPE_BUFFER_USAGE_GPU_WRITE
);
436 /* can't render into this format (or other problem) */
440 /* Choose format for the temporary RGBA texture image.
442 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
447 /* Create the temporary source texture
449 memset(&templ
, 0, sizeof(templ
));
450 templ
.target
= PIPE_TEXTURE_2D
;
451 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
->MesaFormat
);
452 pf_get_block(templ
.format
, &templ
.block
);
453 templ
.width
[0] = width
;
454 templ
.height
[0] = height
;
456 templ
.last_level
= 0;
457 templ
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
458 src_tex
= screen
->texture_create(screen
, &templ
);
463 /* Put user's tex data into the temporary texture
465 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
), src_tex
,
466 0, 0, 0, /* face, level are zero */
468 0, 0, width
, height
); /* x, y, w, h */
469 map
= screen
->transfer_map(screen
, tex_xfer
);
471 mesa_format
->StoreImage(ctx
, 2, GL_RGBA
, mesa_format
,
473 0, 0, 0, /* dest x/y/z offset */
474 tex_xfer
->stride
, /* dest row stride (bytes) */
475 dstImageOffsets
, /* image offsets (for 3D only) */
476 width
, height
, 1, /* size */
477 format
, type
, /* source format/type */
478 pixels
, /* source data */
479 unpack
); /* source data packing */
481 screen
->transfer_unmap(screen
, tex_xfer
);
482 screen
->tex_transfer_destroy(tex_xfer
);
484 /* copy / compress image */
485 util_blit_pixels_tex(ctx
->st
->blit
,
486 src_tex
, /* pipe_texture (src) */
487 0, 0, /* src x0, y0 */
488 width
, height
, /* src x1, y1 */
489 dst_surface
, /* pipe_surface (dst) */
490 xoffset
, yoffset
, /* dst x0, y0 */
491 xoffset
+ width
, /* dst x1 */
492 yoffset
+ height
, /* dst y1 */
494 PIPE_TEX_MIPFILTER_NEAREST
);
496 pipe_surface_reference(&dst_surface
, NULL
);
497 pipe_texture_reference(&src_tex
, NULL
);
504 * Do glTexImage1/2/3D().
507 st_TexImage(GLcontext
* ctx
,
509 GLenum target
, GLint level
,
510 GLint internalFormat
,
511 GLint width
, GLint height
, GLint depth
,
513 GLenum format
, GLenum type
, const void *pixels
,
514 const struct gl_pixelstore_attrib
*unpack
,
515 struct gl_texture_object
*texObj
,
516 struct gl_texture_image
*texImage
,
517 GLsizei imageSize
, GLboolean compressed_src
)
519 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
520 struct st_texture_object
*stObj
= st_texture_object(texObj
);
521 struct st_texture_image
*stImage
= st_texture_image(texImage
);
522 GLint postConvWidth
, postConvHeight
;
523 GLint texelBytes
, sizeInBytes
;
524 GLuint dstRowStride
= 0;
525 struct gl_pixelstore_attrib unpackNB
;
526 enum pipe_transfer_usage transfer_usage
= 0;
528 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
529 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
531 /* switch to "normal" */
532 if (stObj
->surface_based
) {
533 _mesa_clear_texture_object(ctx
, texObj
);
534 stObj
->surface_based
= GL_FALSE
;
537 /* gallium does not support texture borders, strip it off */
539 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
541 texImage
->Width
= width
;
542 texImage
->Height
= height
;
543 texImage
->Depth
= depth
;
544 texImage
->Border
= 0;
548 postConvWidth
= width
;
549 postConvHeight
= height
;
551 stImage
->face
= _mesa_tex_target_to_face(target
);
552 stImage
->level
= level
;
555 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
556 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
561 /* choose the texture format */
562 texImage
->TexFormat
= st_ChooseTextureFormat(ctx
, internalFormat
,
565 _mesa_set_fetch_functions(texImage
, dims
);
567 if (texImage
->TexFormat
->TexelBytes
== 0) {
568 /* must be a compressed format */
570 texImage
->IsCompressed
= GL_TRUE
;
571 texImage
->CompressedSize
=
572 ctx
->Driver
.CompressedTextureSize(ctx
, texImage
->Width
,
573 texImage
->Height
, texImage
->Depth
,
574 texImage
->TexFormat
->MesaFormat
);
577 texelBytes
= texImage
->TexFormat
->TexelBytes
;
579 /* Minimum pitch of 32 bytes */
580 if (postConvWidth
* texelBytes
< 32) {
581 postConvWidth
= 32 / texelBytes
;
582 texImage
->RowStride
= postConvWidth
;
585 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
586 /*assert(texImage->RowStride == postConvWidth);*/
589 /* Release the reference to a potentially orphaned buffer.
590 * Release any old malloced memory.
593 pipe_texture_reference(&stImage
->pt
, NULL
);
594 assert(!texImage
->Data
);
596 else if (texImage
->Data
) {
597 _mesa_align_free(texImage
->Data
);
600 if (width
== 0 || height
== 0 || depth
== 0) {
601 /* stop after freeing old image */
605 /* If this is the only mipmap level in the texture, could call
606 * bmBufferData with NULL data to free the old block and avoid
607 * waiting on any outstanding fences.
610 if (stObj
->teximage_realloc
||
611 level
> (GLint
) stObj
->pt
->last_level
||
612 (stObj
->pt
->last_level
== level
&&
613 stObj
->pt
->target
!= PIPE_TEXTURE_CUBE
&&
614 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
615 stImage
->face
, stImage
->level
))) {
617 pipe_texture_reference(&stObj
->pt
, NULL
);
619 stObj
->teximage_realloc
= FALSE
;
624 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
626 /* Probably out of memory.
627 * Try flushing any pending rendering, then retry.
630 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
632 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
638 assert(!stImage
->pt
);
641 st_texture_match_image(stObj
->pt
, &stImage
->base
,
642 stImage
->face
, stImage
->level
)) {
644 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
649 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
651 /* st_CopyTexImage calls this function with pixels == NULL, with
652 * the expectation that the texture will be set up but nothing
653 * more will be done. This is where those calls return:
655 if (compressed_src
) {
656 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
658 "glCompressedTexImage");
661 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
663 pixels
, unpack
, "glTexImage");
666 /* Note: we can't check for pixels==NULL until after we've allocated
667 * memory for the texture.
670 /* See if we can do texture compression with a blit/render.
672 if (!compressed_src
&&
674 is_compressed_mesa_format(texImage
->TexFormat
) &&
675 screen
->is_format_supported(screen
,
678 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
682 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
683 format
, type
, pixels
, unpack
, texImage
)) {
689 if (format
== GL_DEPTH_COMPONENT
&&
690 pf_is_depth_and_stencil(stImage
->pt
->format
))
691 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
693 transfer_usage
= PIPE_TRANSFER_WRITE
;
695 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
696 transfer_usage
, 0, 0,
698 stImage
->base
.Height
);
699 if(stImage
->transfer
)
700 dstRowStride
= stImage
->transfer
->stride
;
703 /* Allocate regular memory and store the image there temporarily. */
704 if (texImage
->IsCompressed
) {
705 sizeInBytes
= texImage
->CompressedSize
;
707 _mesa_compressed_row_stride(texImage
->TexFormat
->MesaFormat
, width
);
711 dstRowStride
= postConvWidth
* texelBytes
;
712 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
715 texImage
->Data
= _mesa_align_malloc(sizeInBytes
, 16);
718 if (!texImage
->Data
) {
719 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
726 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
727 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
729 /* Copy data. Would like to know when it's ok for us to eg. use
730 * the blitter to copy. Or, use the hardware to do the format
731 * conversion and copy:
733 if (compressed_src
) {
734 memcpy(texImage
->Data
, pixels
, imageSize
);
737 const GLuint srcImageStride
=
738 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
740 const GLubyte
*src
= (const GLubyte
*) pixels
;
742 for (i
= 0; i
< depth
; i
++) {
743 if (!texImage
->TexFormat
->StoreImage(ctx
, dims
,
744 texImage
->_BaseFormat
,
747 0, 0, 0, /* dstX/Y/Zoffset */
749 texImage
->ImageOffsets
,
751 format
, type
, src
, unpack
)) {
752 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
755 if (stImage
->pt
&& i
+ 1 < depth
) {
756 /* unmap this slice */
757 st_texture_image_unmap(ctx
->st
, stImage
);
758 /* map next slice of 3D texture */
759 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
760 transfer_usage
, 0, 0,
762 stImage
->base
.Height
);
763 src
+= srcImageStride
;
769 _mesa_unmap_teximage_pbo(ctx
, unpack
);
771 if (stImage
->pt
&& texImage
->Data
) {
772 st_texture_image_unmap(ctx
->st
, stImage
);
773 texImage
->Data
= NULL
;
779 st_TexImage3D(GLcontext
* ctx
,
780 GLenum target
, GLint level
,
781 GLint internalFormat
,
782 GLint width
, GLint height
, GLint depth
,
784 GLenum format
, GLenum type
, const void *pixels
,
785 const struct gl_pixelstore_attrib
*unpack
,
786 struct gl_texture_object
*texObj
,
787 struct gl_texture_image
*texImage
)
789 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
790 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
796 st_TexImage2D(GLcontext
* ctx
,
797 GLenum target
, GLint level
,
798 GLint internalFormat
,
799 GLint width
, GLint height
, GLint border
,
800 GLenum format
, GLenum type
, const void *pixels
,
801 const struct gl_pixelstore_attrib
*unpack
,
802 struct gl_texture_object
*texObj
,
803 struct gl_texture_image
*texImage
)
805 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
806 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
811 st_TexImage1D(GLcontext
* ctx
,
812 GLenum target
, GLint level
,
813 GLint internalFormat
,
814 GLint width
, GLint border
,
815 GLenum format
, GLenum type
, const void *pixels
,
816 const struct gl_pixelstore_attrib
*unpack
,
817 struct gl_texture_object
*texObj
,
818 struct gl_texture_image
*texImage
)
820 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
821 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
826 st_CompressedTexImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
827 GLint internalFormat
,
828 GLint width
, GLint height
, GLint border
,
829 GLsizei imageSize
, const GLvoid
*data
,
830 struct gl_texture_object
*texObj
,
831 struct gl_texture_image
*texImage
)
833 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
834 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
840 * glGetTexImage() helper: decompress a compressed texture by rendering
841 * a textured quad. Store the results in the user's buffer.
844 decompress_with_blit(GLcontext
* ctx
, GLenum target
, GLint level
,
845 GLenum format
, GLenum type
, GLvoid
*pixels
,
846 struct gl_texture_object
*texObj
,
847 struct gl_texture_image
*texImage
)
849 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
850 struct st_texture_image
*stImage
= st_texture_image(texImage
);
851 const GLuint width
= texImage
->Width
;
852 const GLuint height
= texImage
->Height
;
853 struct pipe_surface
*dst_surface
;
854 struct pipe_texture
*dst_texture
;
855 struct pipe_transfer
*tex_xfer
;
857 /* create temp / dest surface */
858 if (!util_create_rgba_surface(screen
, width
, height
,
859 &dst_texture
, &dst_surface
)) {
860 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
861 "in decompress_with_blit()");
865 /* blit/render/decompress */
866 util_blit_pixels_tex(ctx
->st
->blit
,
867 stImage
->pt
, /* pipe_texture (src) */
868 0, 0, /* src x0, y0 */
869 width
, height
, /* src x1, y1 */
870 dst_surface
, /* pipe_surface (dst) */
871 0, 0, /* dst x0, y0 */
872 width
, height
, /* dst x1, y1 */
874 PIPE_TEX_MIPFILTER_NEAREST
);
876 /* map the dst_surface so we can read from it */
877 tex_xfer
= st_cond_flush_get_tex_transfer(st_context(ctx
),
878 dst_texture
, 0, 0, 0,
880 0, 0, width
, height
);
882 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
884 /* copy/pack data into user buffer */
885 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
887 const uint bytesPerRow
= width
* pf_get_size(stImage
->pt
->format
);
888 ubyte
*map
= screen
->transfer_map(screen
, tex_xfer
);
890 for (row
= 0; row
< height
; row
++) {
891 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
892 height
, format
, type
, row
, 0);
893 memcpy(dest
, map
, bytesPerRow
);
894 map
+= tex_xfer
->stride
;
896 screen
->transfer_unmap(screen
, tex_xfer
);
899 /* format translation via floats */
901 for (row
= 0; row
< height
; row
++) {
902 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
903 GLfloat rgba
[4 * MAX_WIDTH
];
904 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
905 height
, format
, type
, row
, 0);
907 if (ST_DEBUG
& DEBUG_FALLBACK
)
908 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
910 /* get float[4] rgba row from surface */
911 pipe_get_tile_rgba(tex_xfer
, 0, row
, width
, 1, rgba
);
913 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
914 type
, dest
, &ctx
->Pack
, transferOps
);
918 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
920 /* destroy the temp / dest surface */
921 util_destroy_rgba_surface(dst_texture
, dst_surface
);
927 * Need to map texture image into memory before copying image data,
931 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
932 GLenum format
, GLenum type
, GLvoid
* pixels
,
933 struct gl_texture_object
*texObj
,
934 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
936 struct st_texture_image
*stImage
= st_texture_image(texImage
);
937 const GLuint dstImageStride
=
938 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
944 pf_is_compressed(stImage
->pt
->format
) &&
946 /* Need to decompress the texture.
947 * We'll do this by rendering a textured quad.
948 * Note that we only expect RGBA formats (no Z/depth formats).
950 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
957 /* Image is stored in hardware format in a buffer managed by the
958 * kernel. Need to explicitly map and unmap it.
960 unsigned face
= _mesa_tex_target_to_face(target
);
962 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
965 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
966 PIPE_TRANSFER_READ
, 0, 0,
968 stImage
->base
.Height
);
969 texImage
->RowStride
= stImage
->transfer
->stride
/ stImage
->pt
->block
.size
;
972 /* Otherwise, the image should actually be stored in
973 * texImage->Data. This is pretty confusing for
974 * everybody, I'd much prefer to separate the two functions of
975 * texImage->Data - storage for texture images in main memory
976 * and access (ie mappings) of images. In other words, we'd
977 * create a new texImage->Map field and leave Data simply for
980 assert(texImage
->Data
);
983 depth
= texImage
->Depth
;
986 dest
= (GLubyte
*) pixels
;
988 for (i
= 0; i
< depth
; i
++) {
989 if (compressed_dst
) {
990 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
994 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
998 if (stImage
->pt
&& i
+ 1 < depth
) {
999 /* unmap this slice */
1000 st_texture_image_unmap(ctx
->st
, stImage
);
1001 /* map next slice of 3D texture */
1002 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
+ 1,
1003 PIPE_TRANSFER_READ
, 0, 0,
1004 stImage
->base
.Width
,
1005 stImage
->base
.Height
);
1006 dest
+= dstImageStride
;
1010 texImage
->Depth
= depth
;
1014 st_texture_image_unmap(ctx
->st
, stImage
);
1015 texImage
->Data
= NULL
;
1021 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
1022 GLenum format
, GLenum type
, GLvoid
* pixels
,
1023 struct gl_texture_object
*texObj
,
1024 struct gl_texture_image
*texImage
)
1026 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1032 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
1034 struct gl_texture_object
*texObj
,
1035 struct gl_texture_image
*texImage
)
1037 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1044 st_TexSubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
1045 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1046 GLint width
, GLint height
, GLint depth
,
1047 GLenum format
, GLenum type
, const void *pixels
,
1048 const struct gl_pixelstore_attrib
*packing
,
1049 struct gl_texture_object
*texObj
,
1050 struct gl_texture_image
*texImage
)
1052 struct pipe_screen
*screen
= ctx
->st
->pipe
->screen
;
1053 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1054 GLuint dstRowStride
;
1055 const GLuint srcImageStride
=
1056 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1059 /* init to silence warning only: */
1060 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1062 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1063 _mesa_lookup_enum_by_nr(target
),
1064 level
, xoffset
, yoffset
, width
, height
);
1067 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1068 type
, pixels
, packing
, "glTexSubImage2D");
1072 /* See if we can do texture compression with a blit/render.
1074 if (!ctx
->Mesa_DXTn
&&
1075 is_compressed_mesa_format(texImage
->TexFormat
) &&
1076 screen
->is_format_supported(screen
,
1077 stImage
->pt
->format
,
1078 stImage
->pt
->target
,
1079 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
1080 if (compress_with_blit(ctx
, target
, level
,
1081 xoffset
, yoffset
, zoffset
,
1082 width
, height
, depth
,
1083 format
, type
, pixels
, packing
, texImage
)) {
1088 /* Map buffer if necessary. Need to lock to prevent other contexts
1089 * from uploading the buffer under us.
1092 unsigned face
= _mesa_tex_target_to_face(target
);
1094 if (format
== GL_DEPTH_COMPONENT
&&
1095 pf_is_depth_and_stencil(stImage
->pt
->format
))
1096 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1098 transfer_usage
= PIPE_TRANSFER_WRITE
;
1100 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1102 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
,
1108 if (!texImage
->Data
) {
1109 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1113 src
= (const GLubyte
*) pixels
;
1114 dstRowStride
= stImage
->transfer
->stride
;
1116 for (i
= 0; i
< depth
; i
++) {
1117 if (!texImage
->TexFormat
->StoreImage(ctx
, dims
, texImage
->_BaseFormat
,
1118 texImage
->TexFormat
,
1122 texImage
->ImageOffsets
,
1124 format
, type
, src
, packing
)) {
1125 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1128 if (stImage
->pt
&& i
+ 1 < depth
) {
1129 /* unmap this slice */
1130 st_texture_image_unmap(ctx
->st
, stImage
);
1131 /* map next slice of 3D texture */
1132 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
,
1137 src
+= srcImageStride
;
1142 _mesa_unmap_teximage_pbo(ctx
, packing
);
1145 st_texture_image_unmap(ctx
->st
, stImage
);
1146 texImage
->Data
= NULL
;
1153 st_TexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1154 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1155 GLsizei width
, GLsizei height
, GLsizei depth
,
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
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1162 width
, height
, depth
, format
, type
,
1163 pixels
, packing
, texObj
, texImage
);
1168 st_TexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1169 GLint xoffset
, GLint yoffset
,
1170 GLsizei width
, GLsizei height
,
1171 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1172 const struct gl_pixelstore_attrib
*packing
,
1173 struct gl_texture_object
*texObj
,
1174 struct gl_texture_image
*texImage
)
1176 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1177 width
, height
, 1, format
, type
,
1178 pixels
, packing
, texObj
, texImage
);
1183 st_TexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1184 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1185 const GLvoid
* pixels
,
1186 const struct gl_pixelstore_attrib
*packing
,
1187 struct gl_texture_object
*texObj
,
1188 struct gl_texture_image
*texImage
)
1190 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1191 format
, type
, pixels
, packing
, texObj
, texImage
);
1196 st_CompressedTexSubImage1D(GLcontext
*ctx
, GLenum target
, GLint level
,
1197 GLint xoffset
, GLsizei width
,
1199 GLsizei imageSize
, const GLvoid
*data
,
1200 struct gl_texture_object
*texObj
,
1201 struct gl_texture_image
*texImage
)
1208 st_CompressedTexSubImage2D(GLcontext
*ctx
, GLenum target
, GLint level
,
1209 GLint xoffset
, GLint yoffset
,
1210 GLsizei width
, GLint height
,
1212 GLsizei imageSize
, const GLvoid
*data
,
1213 struct gl_texture_object
*texObj
,
1214 struct gl_texture_image
*texImage
)
1216 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1217 struct pipe_format_block block
;
1223 unsigned face
= _mesa_tex_target_to_face(target
);
1225 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, face
, level
,
1226 PIPE_TRANSFER_WRITE
);
1227 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
1228 PIPE_TRANSFER_WRITE
,
1232 block
= stImage
->pt
->block
;
1233 srcBlockStride
= pf_get_stride(&block
, width
);
1234 dstBlockStride
= stImage
->transfer
->stride
;
1236 assert(stImage
->pt
);
1237 /* TODO find good values for block and strides */
1238 /* TODO also adjust texImage->data for yoffset/xoffset */
1242 if (!texImage
->Data
) {
1243 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1247 assert(xoffset
% block
.width
== 0);
1248 assert(yoffset
% block
.height
== 0);
1249 assert(width
% block
.width
== 0);
1250 assert(height
% block
.height
== 0);
1252 for (y
= 0; y
< height
; y
+= block
.height
) {
1253 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1254 const char *src
= (const char*)data
+ srcBlockStride
* pf_get_nblocksy(&block
, y
);
1255 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* pf_get_nblocksy(&block
, y
);
1256 memcpy(dst
, src
, pf_get_stride(&block
, width
));
1260 st_texture_image_unmap(ctx
->st
, stImage
);
1261 texImage
->Data
= NULL
;
1267 st_CompressedTexSubImage3D(GLcontext
*ctx
, GLenum target
, GLint level
,
1268 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1269 GLsizei width
, GLint height
, GLint depth
,
1271 GLsizei imageSize
, const GLvoid
*data
,
1272 struct gl_texture_object
*texObj
,
1273 struct gl_texture_image
*texImage
)
1281 * Do a CopyTexSubImage operation using a read transfer from the source,
1282 * a write transfer to the destination and get_tile()/put_tile() to access
1283 * the pixels/texels.
1285 * Note: srcY=0=TOP of renderbuffer
1288 fallback_copy_texsubimage(GLcontext
*ctx
, GLenum target
, GLint level
,
1289 struct st_renderbuffer
*strb
,
1290 struct st_texture_image
*stImage
,
1292 GLint destX
, GLint destY
, GLint destZ
,
1293 GLint srcX
, GLint srcY
,
1294 GLsizei width
, GLsizei height
)
1296 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1297 struct pipe_screen
*screen
= pipe
->screen
;
1298 struct pipe_transfer
*src_trans
;
1300 enum pipe_transfer_usage transfer_usage
;
1302 if (ST_DEBUG
& DEBUG_FALLBACK
)
1303 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1305 assert(width
<= MAX_WIDTH
);
1307 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1308 srcY
= strb
->Base
.Height
- srcY
- height
;
1311 src_trans
= st_cond_flush_get_tex_transfer( st_context(ctx
),
1318 if (baseFormat
== GL_DEPTH_COMPONENT
&&
1319 pf_is_depth_and_stencil(stImage
->pt
->format
))
1320 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1322 transfer_usage
= PIPE_TRANSFER_WRITE
;
1324 st_teximage_flush_before_map(ctx
->st
, stImage
->pt
, 0, 0,
1327 texDest
= st_texture_image_map(ctx
->st
, stImage
, 0, transfer_usage
,
1328 destX
, destY
, width
, height
);
1330 if (baseFormat
== GL_DEPTH_COMPONENT
||
1331 baseFormat
== GL_DEPTH24_STENCIL8
) {
1332 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1333 ctx
->Pixel
.DepthBias
!= 0.0F
);
1336 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1337 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1346 /* To avoid a large temp memory allocation, do copy row by row */
1347 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1348 uint data
[MAX_WIDTH
];
1349 pipe_get_tile_z(src_trans
, 0, srcY
, width
, 1, data
);
1351 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1353 pipe_put_tile_z(stImage
->transfer
, 0, row
, width
, 1, data
);
1359 (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
1361 if (tempSrc
&& texDest
) {
1362 const GLint dims
= 2;
1363 const GLint dstRowStride
= stImage
->transfer
->stride
;
1364 struct gl_texture_image
*texImage
= &stImage
->base
;
1365 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1367 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1368 unpack
.Invert
= GL_TRUE
;
1371 /* get float/RGBA image from framebuffer */
1372 /* XXX this usually involves a lot of int/float conversion.
1373 * try to avoid that someday.
1375 pipe_get_tile_rgba(src_trans
, 0, 0, width
, height
, tempSrc
);
1377 /* Store into texture memory.
1378 * Note that this does some special things such as pixel transfer
1379 * ops and format conversion. In particular, if the dest tex format
1380 * is actually RGBA but the user created the texture as GL_RGB we
1381 * need to fill-in/override the alpha channel with 1.0.
1383 texImage
->TexFormat
->StoreImage(ctx
, dims
,
1384 texImage
->_BaseFormat
,
1385 texImage
->TexFormat
,
1389 texImage
->ImageOffsets
,
1391 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1395 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1399 _mesa_free(tempSrc
);
1402 st_texture_image_unmap(ctx
->st
, stImage
);
1403 screen
->tex_transfer_destroy(src_trans
);
1408 compatible_src_dst_formats(const struct gl_renderbuffer
*src
,
1409 const struct gl_texture_image
*dst
)
1411 const GLenum srcFormat
= src
->_BaseFormat
;
1412 const GLenum dstLogicalFormat
= dst
->_BaseFormat
;
1414 if (srcFormat
== dstLogicalFormat
) {
1415 /* This is the same as matching_base_formats, which should
1416 * always pass, as it did previously.
1418 return TGSI_WRITEMASK_XYZW
;
1420 else if (srcFormat
== GL_RGBA
&&
1421 dstLogicalFormat
== GL_RGB
) {
1422 /* Add a single special case to cope with RGBA->RGB transfers,
1423 * setting A to 1.0 to cope with situations where the RGB
1424 * destination is actually stored as RGBA.
1426 return TGSI_WRITEMASK_XYZ
; /* A ==> 1.0 */
1429 if (ST_DEBUG
& DEBUG_FALLBACK
)
1430 debug_printf("%s failed for src %s, dst %s\n",
1432 _mesa_lookup_enum_by_nr(srcFormat
),
1433 _mesa_lookup_enum_by_nr(dstLogicalFormat
));
1444 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1445 * Note that the region to copy has already been clipped so we know we
1446 * won't read from outside the source renderbuffer's bounds.
1448 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1451 st_copy_texsubimage(GLcontext
*ctx
,
1452 GLenum target
, GLint level
,
1453 GLint destX
, GLint destY
, GLint destZ
,
1454 GLint srcX
, GLint srcY
,
1455 GLsizei width
, GLsizei height
)
1457 struct gl_texture_unit
*texUnit
=
1458 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1459 struct gl_texture_object
*texObj
=
1460 _mesa_select_tex_object(ctx
, texUnit
, target
);
1461 struct gl_texture_image
*texImage
=
1462 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1463 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1464 const GLenum texBaseFormat
= texImage
->InternalFormat
;
1465 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1466 struct st_renderbuffer
*strb
;
1467 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1468 struct pipe_screen
*screen
= pipe
->screen
;
1469 enum pipe_format dest_format
, src_format
;
1470 GLboolean use_fallback
= GL_TRUE
;
1471 GLboolean matching_base_formats
;
1472 GLuint format_writemask
;
1473 struct pipe_surface
*dest_surface
= NULL
;
1474 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1476 /* any rendering in progress must flushed before we grab the fb image */
1477 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
1479 /* make sure finalize_textures has been called?
1481 if (0) st_validate_state(ctx
->st
);
1483 /* determine if copying depth or color data */
1484 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1485 texBaseFormat
== GL_DEPTH24_STENCIL8
) {
1486 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1488 else if (texBaseFormat
== GL_DEPTH_STENCIL_EXT
) {
1489 strb
= st_renderbuffer(fb
->_StencilBuffer
);
1492 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1493 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1496 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1497 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1525 if (width
< 0 || height
< 0)
1530 assert(strb
->surface
);
1531 assert(stImage
->pt
);
1533 src_format
= strb
->surface
->format
;
1534 dest_format
= stImage
->pt
->format
;
1537 * Determine if the src framebuffer and dest texture have the same
1538 * base format. We need this to detect a case such as the framebuffer
1539 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1540 * texture format stores RGBA we need to set A=1 (overriding the
1541 * framebuffer's alpha values). We can't do that with the blit or
1542 * textured-quad paths.
1544 matching_base_formats
= (strb
->Base
._BaseFormat
== texImage
->_BaseFormat
);
1545 format_writemask
= compatible_src_dst_formats(&strb
->Base
, texImage
);
1547 if (ctx
->_ImageTransferState
== 0x0) {
1549 if (pipe
->surface_copy
&&
1550 matching_base_formats
&&
1551 src_format
== dest_format
&&
1554 /* use surface_copy() / blit */
1556 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1557 stImage
->face
, stImage
->level
,
1559 PIPE_BUFFER_USAGE_GPU_WRITE
);
1561 /* for surface_copy(), y=0=top, always */
1562 pipe
->surface_copy(pipe
,
1571 use_fallback
= GL_FALSE
;
1573 else if (format_writemask
&&
1574 screen
->is_format_supported(screen
, src_format
,
1576 PIPE_TEXTURE_USAGE_SAMPLER
,
1578 screen
->is_format_supported(screen
, dest_format
,
1580 PIPE_TEXTURE_USAGE_RENDER_TARGET
,
1582 /* draw textured quad to do the copy */
1585 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
,
1586 stImage
->face
, stImage
->level
,
1588 PIPE_BUFFER_USAGE_GPU_WRITE
);
1591 srcY1
= strb
->Base
.Height
- srcY
- height
;
1592 srcY0
= srcY1
+ height
;
1596 srcY1
= srcY0
+ height
;
1598 util_blit_pixels_writemask(ctx
->st
->blit
,
1601 srcX
+ width
, srcY1
,
1604 destX
+ width
, destY
+ height
,
1605 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1607 use_fallback
= GL_FALSE
;
1611 pipe_surface_reference(&dest_surface
, NULL
);
1615 /* software fallback */
1616 fallback_copy_texsubimage(ctx
, target
, level
,
1617 strb
, stImage
, texBaseFormat
,
1618 destX
, destY
, destZ
,
1619 srcX
, srcY
, width
, height
);
1626 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1627 GLenum internalFormat
,
1628 GLint x
, GLint y
, GLsizei width
, GLint border
)
1630 struct gl_texture_unit
*texUnit
=
1631 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1632 struct gl_texture_object
*texObj
=
1633 _mesa_select_tex_object(ctx
, texUnit
, target
);
1634 struct gl_texture_image
*texImage
=
1635 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1637 /* Setup or redefine the texture object, texture and texture
1638 * image. Don't populate yet.
1640 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1642 GL_RGBA
, CHAN_TYPE
, NULL
,
1643 &ctx
->DefaultPacking
, texObj
, texImage
);
1645 st_copy_texsubimage(ctx
, target
, level
,
1646 0, 0, 0, /* destX,Y,Z */
1647 x
, y
, width
, 1); /* src X, Y, size */
1652 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1653 GLenum internalFormat
,
1654 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1657 struct gl_texture_unit
*texUnit
=
1658 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1659 struct gl_texture_object
*texObj
=
1660 _mesa_select_tex_object(ctx
, texUnit
, target
);
1661 struct gl_texture_image
*texImage
=
1662 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1664 /* Setup or redefine the texture object, texture and texture
1665 * image. Don't populate yet.
1667 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1668 width
, height
, border
,
1669 GL_RGBA
, CHAN_TYPE
, NULL
,
1670 &ctx
->DefaultPacking
, texObj
, texImage
);
1672 st_copy_texsubimage(ctx
, target
, level
,
1673 0, 0, 0, /* destX,Y,Z */
1674 x
, y
, width
, height
); /* src X, Y, size */
1679 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1680 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1682 const GLint yoffset
= 0, zoffset
= 0;
1683 const GLsizei height
= 1;
1684 st_copy_texsubimage(ctx
, target
, level
,
1685 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1686 x
, y
, width
, height
); /* src X, Y, size */
1691 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1692 GLint xoffset
, GLint yoffset
,
1693 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1695 const GLint zoffset
= 0;
1696 st_copy_texsubimage(ctx
, target
, level
,
1697 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1698 x
, y
, width
, height
); /* src X, Y, size */
1703 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1704 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1705 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1707 st_copy_texsubimage(ctx
, target
, level
,
1708 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1709 x
, y
, width
, height
); /* src X, Y, size */
1714 copy_image_data_to_texture(struct st_context
*st
,
1715 struct st_texture_object
*stObj
,
1717 struct st_texture_image
*stImage
)
1720 /* Copy potentially with the blitter:
1722 st_texture_image_copy(st
->pipe
,
1723 stObj
->pt
, dstLevel
, /* dest texture, level */
1724 stImage
->pt
, /* src texture */
1728 pipe_texture_reference(&stImage
->pt
, NULL
);
1730 else if (stImage
->base
.Data
) {
1731 assert(stImage
->base
.Data
!= NULL
);
1733 /* More straightforward upload.
1736 st_teximage_flush_before_map(st
, stObj
->pt
, stImage
->face
, dstLevel
,
1737 PIPE_TRANSFER_WRITE
);
1740 st_texture_image_data(st
,
1745 stImage
->base
.RowStride
*
1746 stObj
->pt
->block
.size
,
1747 stImage
->base
.RowStride
*
1748 stImage
->base
.Height
*
1749 stObj
->pt
->block
.size
);
1750 _mesa_align_free(stImage
->base
.Data
);
1751 stImage
->base
.Data
= NULL
;
1754 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
1759 * Called during state validation. When this function is finished,
1760 * the texture object should be ready for rendering.
1761 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1764 st_finalize_texture(GLcontext
*ctx
,
1765 struct pipe_context
*pipe
,
1766 struct gl_texture_object
*tObj
,
1767 GLboolean
*needFlush
)
1769 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1770 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1772 struct st_texture_image
*firstImage
;
1774 *needFlush
= GL_FALSE
;
1776 if (stObj
->base
._Complete
) {
1777 /* The texture is complete and we know exactly how many mipmap levels
1778 * are present/needed. This is conditional because we may be called
1779 * from the st_generate_mipmap() function when the texture object is
1780 * incomplete. In that case, we'll have set stObj->lastLevel before
1783 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1784 stObj
->base
.MinFilter
== GL_NEAREST
)
1785 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1787 stObj
->lastLevel
= stObj
->base
._MaxLevel
- stObj
->base
.BaseLevel
;
1790 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1792 /* If both firstImage and stObj point to a texture which can contain
1793 * all active images, favour firstImage. Note that because of the
1794 * completeness requirement, we know that the image dimensions
1797 if (firstImage
->pt
&&
1798 firstImage
->pt
!= stObj
->pt
&&
1799 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1800 pipe_texture_reference(&stObj
->pt
, firstImage
->pt
);
1803 /* FIXME: determine format block instead of cpp */
1804 if (firstImage
->base
.IsCompressed
) {
1805 cpp
= compressed_num_bytes(firstImage
->base
.TexFormat
->MesaFormat
);
1808 cpp
= firstImage
->base
.TexFormat
->TexelBytes
;
1811 /* If we already have a gallium texture, check that it matches the texture
1812 * object's format, target, size, num_levels, etc.
1815 const enum pipe_format fmt
=
1816 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
->MesaFormat
);
1817 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1818 stObj
->pt
->format
!= fmt
||
1819 stObj
->pt
->last_level
< stObj
->lastLevel
||
1820 stObj
->pt
->width
[0] != firstImage
->base
.Width2
||
1821 stObj
->pt
->height
[0] != firstImage
->base
.Height2
||
1822 stObj
->pt
->depth
[0] != firstImage
->base
.Depth2
||
1823 /* Nominal bytes per pixel: */
1824 stObj
->pt
->block
.size
/ stObj
->pt
->block
.width
!= cpp
)
1826 pipe_texture_reference(&stObj
->pt
, NULL
);
1827 ctx
->st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1831 /* May need to create a new gallium texture:
1834 const enum pipe_format fmt
=
1835 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
->MesaFormat
);
1836 GLuint usage
= default_usage(fmt
);
1838 stObj
->pt
= st_texture_create(ctx
->st
,
1839 gl_target_to_pipe(stObj
->base
.Target
),
1842 firstImage
->base
.Width2
,
1843 firstImage
->base
.Height2
,
1844 firstImage
->base
.Depth2
,
1848 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1853 /* Pull in any images not in the object's texture:
1855 for (face
= 0; face
< nr_faces
; face
++) {
1857 for (level
= 0; level
<= stObj
->lastLevel
; level
++) {
1858 struct st_texture_image
*stImage
=
1859 st_texture_image(stObj
->base
.Image
[face
][stObj
->base
.BaseLevel
+ level
]);
1861 /* Need to import images in main memory or held in other textures.
1863 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1864 copy_image_data_to_texture(ctx
->st
, stObj
, level
, stImage
);
1865 *needFlush
= GL_TRUE
;
1875 * Returns pointer to a default/dummy texture.
1876 * This is typically used when the current shader has tex/sample instructions
1877 * but the user has not provided a (any) texture(s).
1879 struct gl_texture_object
*
1880 st_get_default_texture(struct st_context
*st
)
1882 if (!st
->default_texture
) {
1883 static const GLenum target
= GL_TEXTURE_2D
;
1884 GLubyte pixels
[16][16][4];
1885 struct gl_texture_object
*texObj
;
1886 struct gl_texture_image
*texImg
;
1889 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1890 * when attempting to sample incomplete textures.
1892 for (i
= 0; i
< 16; i
++) {
1893 for (j
= 0; j
< 16; j
++) {
1894 pixels
[i
][j
][0] = 0;
1895 pixels
[i
][j
][1] = 0;
1896 pixels
[i
][j
][2] = 0;
1897 pixels
[i
][j
][3] = 255;
1901 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1903 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1905 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1906 16, 16, 1, 0, /* w, h, d, border */
1909 st_TexImage(st
->ctx
, 2, target
,
1910 0, GL_RGBA
, /* level, intformat */
1911 16, 16, 1, 0, /* w, h, d, border */
1912 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1913 &st
->ctx
->DefaultPacking
,
1917 texObj
->MinFilter
= GL_NEAREST
;
1918 texObj
->MagFilter
= GL_NEAREST
;
1919 texObj
->_Complete
= GL_TRUE
;
1921 st
->default_texture
= texObj
;
1923 return st
->default_texture
;
1928 st_init_texture_functions(struct dd_function_table
*functions
)
1930 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1931 functions
->TexImage1D
= st_TexImage1D
;
1932 functions
->TexImage2D
= st_TexImage2D
;
1933 functions
->TexImage3D
= st_TexImage3D
;
1934 functions
->TexSubImage1D
= st_TexSubImage1D
;
1935 functions
->TexSubImage2D
= st_TexSubImage2D
;
1936 functions
->TexSubImage3D
= st_TexSubImage3D
;
1937 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1938 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1939 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1940 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1941 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1942 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1943 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1944 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1945 functions
->GenerateMipmap
= st_generate_mipmap
;
1947 functions
->GetTexImage
= st_GetTexImage
;
1949 /* compressed texture functions */
1950 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1951 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1952 functions
->CompressedTextureSize
= _mesa_compressed_texture_size
;
1954 functions
->NewTextureObject
= st_NewTextureObject
;
1955 functions
->NewTextureImage
= st_NewTextureImage
;
1956 functions
->DeleteTexture
= st_DeleteTextureObject
;
1957 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1958 functions
->UpdateTexturePalette
= 0;
1960 functions
->TextureMemCpy
= do_memcpy
;
1962 /* XXX Temporary until we can query pipe's texture sizes */
1963 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;