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/imports.h"
29 #include "main/convolve.h"
30 #include "main/enums.h"
31 #include "main/image.h"
32 #include "main/macros.h"
33 #include "main/texcompress.h"
34 #include "main/texformat.h"
35 #include "main/teximage.h"
36 #include "main/texobj.h"
37 #include "main/texstore.h"
39 #include "state_tracker/st_context.h"
40 #include "state_tracker/st_cb_fbo.h"
41 #include "state_tracker/st_cb_texture.h"
42 #include "state_tracker/st_format.h"
43 #include "state_tracker/st_texture.h"
45 #include "pipe/p_context.h"
46 #include "pipe/p_defines.h"
47 #include "pipe/p_inlines.h"
50 #define DBG if (0) printf
53 struct st_texture_object
55 struct gl_texture_object base
; /* The "parent" object */
57 /* The texture must include at least these levels once validated:
62 /* Offset for firstLevel image:
66 /* On validation any active images held in main memory or in other
67 * regions will be copied to this region and the old storage freed.
69 struct pipe_texture
*pt
;
71 GLboolean imageOverride
;
79 static INLINE
struct st_texture_object
*
80 st_texture_object(struct gl_texture_object
*obj
)
82 return (struct st_texture_object
*) obj
;
85 static INLINE
struct st_texture_image
*
86 st_texture_image(struct gl_texture_image
*img
)
88 return (struct st_texture_image
*) img
;
93 st_get_texobj_texture(struct gl_texture_object
*texObj
)
95 struct st_texture_object
*stObj
= st_texture_object(texObj
);
101 gl_target_to_pipe(GLenum target
)
105 return PIPE_TEXTURE_1D
;
108 case GL_TEXTURE_RECTANGLE_NV
:
109 return PIPE_TEXTURE_2D
;
112 return PIPE_TEXTURE_3D
;
114 case GL_TEXTURE_CUBE_MAP_ARB
:
115 return PIPE_TEXTURE_CUBE
;
125 compressed_num_bytes(GLuint mesaFormat
)
130 case MESA_FORMAT_RGB_FXT1
:
131 case MESA_FORMAT_RGBA_FXT1
:
132 case MESA_FORMAT_RGB_DXT1
:
133 case MESA_FORMAT_RGBA_DXT1
:
137 case MESA_FORMAT_RGBA_DXT3
:
138 case MESA_FORMAT_RGBA_DXT5
:
151 st_IsTextureResident(GLcontext
* ctx
, struct gl_texture_object
*texObj
)
154 struct intel_context
*intel
= intel_context(ctx
);
155 struct st_texture_object
*stObj
= st_texture_object(texObj
);
160 intel_is_region_resident(intel
, stObj
->pt
->region
);
167 static struct gl_texture_image
*
168 st_NewTextureImage(GLcontext
* ctx
)
170 DBG("%s\n", __FUNCTION__
);
172 return (struct gl_texture_image
*) CALLOC_STRUCT(st_texture_image
);
176 static struct gl_texture_object
*
177 st_NewTextureObject(GLcontext
* ctx
, GLuint name
, GLenum target
)
179 struct st_texture_object
*obj
= CALLOC_STRUCT(st_texture_object
);
181 DBG("%s\n", __FUNCTION__
);
182 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
188 st_DeleteTextureObject(GLcontext
*ctx
,
189 struct gl_texture_object
*texObj
)
191 struct st_texture_object
*stObj
= st_texture_object(texObj
);
194 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stObj
->pt
);
196 _mesa_delete_texture_object(ctx
, texObj
);
201 st_FreeTextureImageData(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
203 struct st_texture_image
*stImage
= st_texture_image(texImage
);
205 DBG("%s\n", __FUNCTION__
);
208 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stImage
->pt
);
211 if (texImage
->Data
) {
212 free(texImage
->Data
);
213 texImage
->Data
= NULL
;
220 /* ================================================================
221 * From linux kernel i386 header files, copes with odd sizes better
222 * than COPY_DWORDS would:
223 * XXX Put this in src/mesa/main/imports.h ???
225 #if defined(i386) || defined(__i386__)
227 __memcpy(void *to
, const void *from
, size_t n
)
230 __asm__
__volatile__("rep ; movsl\n\t"
234 "1:\ttestb $1,%b4\n\t"
236 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
237 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
242 #define __memcpy(a,b,c) memcpy(a,b,c)
246 /* The system memcpy (at least on ubuntu 5.10) has problems copying
247 * to agp (writecombined) memory from a source which isn't 64-byte
248 * aligned - there is a 4x performance falloff.
250 * The x86 __memcpy is immune to this but is slightly slower
251 * (10%-ish) than the system memcpy.
253 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
254 * isn't much faster than x86_memcpy for agp copies.
256 * TODO: switch dynamically.
259 do_memcpy(void *dest
, const void *src
, size_t n
)
261 if ((((unsigned) src
) & 63) || (((unsigned) dest
) & 63)) {
262 return __memcpy(dest
, src
, n
);
265 return memcpy(dest
, src
, n
);
269 /* Functions to store texture images. Where possible, textures
270 * will be created or further instantiated with image data, otherwise
271 * images will be stored in malloc'd memory. A validation step is
272 * required to pull those images into a texture, or otherwise
273 * decide a fallback is required.
292 /* Otherwise, store it in memory if (Border != 0) or (any dimension ==
295 * Otherwise, if max_level >= level >= min_level, create texture with
296 * space for images from min_level down to max_level.
298 * Otherwise, create texture with space for images from (level 0)..(1x1).
299 * Consider pruning this texture at a validation if the saving is worth it.
302 guess_and_alloc_texture(struct st_context
*st
,
303 struct st_texture_object
*stObj
,
304 struct st_texture_image
*stImage
)
308 GLuint width
= stImage
->base
.Width
;
309 GLuint height
= stImage
->base
.Height
;
310 GLuint depth
= stImage
->base
.Depth
;
311 GLuint l2width
, l2height
, l2depth
;
312 GLuint i
, comp_byte
= 0;
314 DBG("%s\n", __FUNCTION__
);
316 if (stImage
->base
.Border
)
319 if (stImage
->level
> stObj
->base
.BaseLevel
&&
320 (stImage
->base
.Width
== 1 ||
321 (stObj
->base
.Target
!= GL_TEXTURE_1D
&&
322 stImage
->base
.Height
== 1) ||
323 (stObj
->base
.Target
== GL_TEXTURE_3D
&&
324 stImage
->base
.Depth
== 1)))
327 /* If this image disrespects BaseLevel, allocate from level zero.
328 * Usually BaseLevel == 0, so it's unlikely to happen.
330 if (stImage
->level
< stObj
->base
.BaseLevel
)
333 firstLevel
= stObj
->base
.BaseLevel
;
336 /* Figure out image dimensions at start level.
338 for (i
= stImage
->level
; i
> firstLevel
; i
--) {
346 /* Guess a reasonable value for lastLevel. This is probably going
347 * to be wrong fairly often and might mean that we have to look at
348 * resizable buffers, or require that buffers implement lazy
349 * pagetable arrangements.
351 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
352 stObj
->base
.MinFilter
== GL_LINEAR
) &&
353 stImage
->level
== firstLevel
) {
354 lastLevel
= firstLevel
;
357 l2width
= logbase2(width
);
358 l2height
= logbase2(height
);
359 l2depth
= logbase2(depth
);
360 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
364 if (stImage
->base
.IsCompressed
)
365 comp_byte
= compressed_num_bytes(stImage
->base
.TexFormat
->MesaFormat
);
366 stObj
->pt
= st_texture_create(st
,
367 gl_target_to_pipe(stObj
->base
.Target
),
368 st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
->MesaFormat
),
369 stImage
->base
.InternalFormat
,
377 DBG("%s - success\n", __FUNCTION__
);
384 target_to_face(GLenum target
)
387 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
388 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
389 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
390 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
391 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
392 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
393 return ((GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
401 /* There are actually quite a few combinations this will work for,
402 * more than what I've listed here.
405 check_pbo_format(GLint internalFormat
,
406 GLenum format
, GLenum type
,
407 const struct gl_texture_format
*mesa_format
)
409 switch (internalFormat
) {
412 return (format
== GL_BGRA
&&
413 (type
== GL_UNSIGNED_BYTE
||
414 type
== GL_UNSIGNED_INT_8_8_8_8_REV
) &&
415 mesa_format
== &_mesa_texformat_argb8888
);
418 return (format
== GL_RGB
&&
419 type
== GL_UNSIGNED_SHORT_5_6_5
&&
420 mesa_format
== &_mesa_texformat_rgb565
);
422 return (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
);
429 /* XXX: Do this for TexSubImage also:
432 try_pbo_upload(GLcontext
*ctx
,
433 struct st_texture_image
*stImage
,
434 const struct gl_pixelstore_attrib
*unpack
,
435 GLint internalFormat
,
436 GLint width
, GLint height
,
437 GLenum format
, GLenum type
, const void *pixels
)
439 return GL_FALSE
; /* XXX fix flushing/locking/blitting below */
441 struct intel_context
*intel
= intel_context(ctx
);
442 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
443 GLuint src_offset
, src_stride
;
444 GLuint dst_offset
, dst_stride
;
447 ctx
._ImageTransferState
||
448 unpack
->SkipPixels
|| unpack
->SkipRows
) {
449 _mesa_printf("%s: failure 1\n", __FUNCTION__
);
453 src_offset
= (GLuint
) pixels
;
455 if (unpack
->RowLength
> 0)
456 src_stride
= unpack
->RowLength
;
460 dst_offset
= st_texture_image_offset(stImage
->pt
,
464 dst_stride
= stImage
->pt
->pitch
;
467 struct _DriBufferObject
*src_buffer
=
468 intel_bufferobj_buffer(intel
, pbo
, INTEL_READ
);
470 /* Temporary hack: cast to _DriBufferObject:
472 struct _DriBufferObject
*dst_buffer
=
473 (struct _DriBufferObject
*)stImage
->pt
->region
->buffer
;
476 intelEmitCopyBlit(intel
,
478 src_stride
, src_buffer
, src_offset
,
479 dst_stride
, dst_buffer
, dst_offset
,
480 0, 0, 0, 0, width
, height
,
491 try_pbo_zcopy(GLcontext
*ctx
,
492 struct st_texture_image
*stImage
,
493 const struct gl_pixelstore_attrib
*unpack
,
494 GLint internalFormat
,
495 GLint width
, GLint height
,
496 GLenum format
, GLenum type
, const void *pixels
)
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
, GLsizei imageSize
, int compressed
)
518 struct st_texture_object
*stObj
= st_texture_object(texObj
);
519 struct st_texture_image
*stImage
= st_texture_image(texImage
);
520 GLint postConvWidth
= width
;
521 GLint postConvHeight
= height
;
522 GLint texelBytes
, sizeInBytes
;
526 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
527 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
529 stImage
->face
= target_to_face(target
);
530 stImage
->level
= level
;
532 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
533 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
537 /* choose the texture format */
538 texImage
->TexFormat
= st_ChooseTextureFormat(ctx
, internalFormat
,
541 _mesa_set_fetch_functions(texImage
, dims
);
543 if (texImage
->TexFormat
->TexelBytes
== 0) {
544 /* must be a compressed format */
546 texImage
->IsCompressed
= GL_TRUE
;
547 texImage
->CompressedSize
=
548 ctx
->Driver
.CompressedTextureSize(ctx
, texImage
->Width
,
549 texImage
->Height
, texImage
->Depth
,
550 texImage
->TexFormat
->MesaFormat
);
552 texelBytes
= texImage
->TexFormat
->TexelBytes
;
554 /* Minimum pitch of 32 bytes */
555 if (postConvWidth
* texelBytes
< 32) {
556 postConvWidth
= 32 / texelBytes
;
557 texImage
->RowStride
= postConvWidth
;
560 assert(texImage
->RowStride
== postConvWidth
);
563 /* Release the reference to a potentially orphaned buffer.
564 * Release any old malloced memory.
567 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stImage
->pt
);
568 assert(!texImage
->Data
);
570 else if (texImage
->Data
) {
571 _mesa_align_free(texImage
->Data
);
574 /* If this is the only texture image in the texture, could call
575 * bmBufferData with NULL data to free the old block and avoid
576 * waiting on any outstanding fences.
579 stObj
->pt
->first_level
== level
&&
580 stObj
->pt
->last_level
== level
&&
581 stObj
->pt
->target
!= PIPE_TEXTURE_CUBE
&&
582 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
583 stImage
->face
, stImage
->level
)) {
586 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stObj
->pt
);
591 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
593 DBG("guess_and_alloc_texture: failed\n");
597 assert(!stImage
->pt
);
600 st_texture_match_image(stObj
->pt
, &stImage
->base
,
601 stImage
->face
, stImage
->level
)) {
603 pipe_texture_reference(ctx
->st
->pipe
, &stImage
->pt
, stObj
->pt
);
608 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
610 #if 0 /* XXX FIX when st_buffer_objects are in place */
615 intel_buffer_object(unpack
->BufferObj
) &&
616 check_pbo_format(internalFormat
, format
,
617 type
, texImage
->TexFormat
)) {
619 DBG("trying pbo upload\n");
621 /* Attempt to texture directly from PBO data (zero copy upload).
623 * Currently disable as it can lead to worse as well as better
624 * performance (in particular when pipe_region_cow() is
627 if (stObj
->pt
== stImage
->pt
&&
628 stObj
->pt
->first_level
== level
&&
629 stObj
->pt
->last_level
== level
) {
631 if (try_pbo_zcopy(intel
, stImage
, unpack
,
633 width
, height
, format
, type
, pixels
)) {
635 DBG("pbo zcopy upload succeeded\n");
641 /* Otherwise, attempt to use the blitter for PBO image uploads.
643 if (try_pbo_upload(intel
, stImage
, unpack
,
645 width
, height
, format
, type
, pixels
)) {
646 DBG("pbo upload succeeded\n");
650 DBG("pbo upload failed\n");
653 (void) try_pbo_upload
;
654 (void) check_pbo_format
;
655 (void) try_pbo_zcopy
;
659 /* intelCopyTexImage calls this function with pixels == NULL, with
660 * the expectation that the texture will be set up but nothing
661 * more will be done. This is where those calls return:
664 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
666 "glCompressedTexImage");
668 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
670 pixels
, unpack
, "glTexImage");
676 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0);
677 dstRowStride
= stImage
->surface
->pitch
* stImage
->surface
->cpp
;
680 /* Allocate regular memory and store the image there temporarily. */
681 if (texImage
->IsCompressed
) {
682 sizeInBytes
= texImage
->CompressedSize
;
684 _mesa_compressed_row_stride(texImage
->TexFormat
->MesaFormat
, width
);
688 dstRowStride
= postConvWidth
* texelBytes
;
689 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
692 texImage
->Data
= malloc(sizeInBytes
);
695 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
696 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
698 /* Copy data. Would like to know when it's ok for us to eg. use
699 * the blitter to copy. Or, use the hardware to do the format
700 * conversion and copy:
703 memcpy(texImage
->Data
, pixels
, imageSize
);
706 GLuint srcImageStride
= _mesa_image_image_stride(unpack
, width
, height
,
710 for (i
= 0; i
++ < depth
;) {
711 if (!texImage
->TexFormat
->StoreImage(ctx
, dims
,
712 texImage
->_BaseFormat
,
715 0, 0, 0, /* dstX/Y/Zoffset */
717 texImage
->ImageOffsets
,
719 format
, type
, pixels
, unpack
)) {
720 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
723 if (stImage
->pt
&& i
< depth
) {
724 st_texture_image_unmap(ctx
->st
, stImage
);
725 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
);
726 pixels
+= srcImageStride
;
731 _mesa_unmap_teximage_pbo(ctx
, unpack
);
734 st_texture_image_unmap(ctx
->st
, stImage
);
735 texImage
->Data
= NULL
;
739 /* GL_SGIS_generate_mipmap -- this can be accelerated now.
741 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
742 intel_generate_mipmap(ctx
, target
,
743 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
751 st_TexImage3D(GLcontext
* ctx
,
752 GLenum target
, GLint level
,
753 GLint internalFormat
,
754 GLint width
, GLint height
, GLint depth
,
756 GLenum format
, GLenum type
, const void *pixels
,
757 const struct gl_pixelstore_attrib
*unpack
,
758 struct gl_texture_object
*texObj
,
759 struct gl_texture_image
*texImage
)
761 st_TexImage(ctx
, 3, target
, level
,
762 internalFormat
, width
, height
, depth
, border
,
763 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
768 st_TexImage2D(GLcontext
* ctx
,
769 GLenum target
, GLint level
,
770 GLint internalFormat
,
771 GLint width
, GLint height
, GLint border
,
772 GLenum format
, GLenum type
, const void *pixels
,
773 const struct gl_pixelstore_attrib
*unpack
,
774 struct gl_texture_object
*texObj
,
775 struct gl_texture_image
*texImage
)
777 st_TexImage(ctx
, 2, target
, level
,
778 internalFormat
, width
, height
, 1, border
,
779 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
784 st_TexImage1D(GLcontext
* ctx
,
785 GLenum target
, GLint level
,
786 GLint internalFormat
,
787 GLint width
, GLint border
,
788 GLenum format
, GLenum type
, const void *pixels
,
789 const struct gl_pixelstore_attrib
*unpack
,
790 struct gl_texture_object
*texObj
,
791 struct gl_texture_image
*texImage
)
793 st_TexImage(ctx
, 1, target
, level
,
794 internalFormat
, width
, 1, 1, border
,
795 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
800 st_CompressedTexImage2D( GLcontext
*ctx
, GLenum target
, GLint level
,
801 GLint internalFormat
,
802 GLint width
, GLint height
, GLint border
,
803 GLsizei imageSize
, const GLvoid
*data
,
804 struct gl_texture_object
*texObj
,
805 struct gl_texture_image
*texImage
)
807 st_TexImage(ctx
, 2, target
, level
,
808 internalFormat
, width
, height
, 1, border
,
809 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, 1);
814 * Need to map texture image into memory before copying image data,
818 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
819 GLenum format
, GLenum type
, GLvoid
* pixels
,
820 struct gl_texture_object
*texObj
,
821 struct gl_texture_image
*texImage
, int compressed
)
824 struct intel_context *intel = intel_context(ctx);
826 struct st_texture_image
*stImage
= st_texture_image(texImage
);
827 GLuint dstImageStride
= _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
,
828 texImage
->Height
, format
,
835 /* Image is stored in hardware format in a buffer managed by the
836 * kernel. Need to explicitly map and unmap it.
838 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0);
839 texImage
->RowStride
= stImage
->surface
->pitch
;
842 /* Otherwise, the image should actually be stored in
843 * texImage->Data. This is pretty confusing for
844 * everybody, I'd much prefer to separate the two functions of
845 * texImage->Data - storage for texture images in main memory
846 * and access (ie mappings) of images. In other words, we'd
847 * create a new texImage->Map field and leave Data simply for
850 assert(texImage
->Data
);
853 depth
= texImage
->Depth
;
856 for (i
= 0; i
++ < depth
;) {
858 _mesa_get_compressed_teximage(ctx
, target
, level
, pixels
,
861 _mesa_get_teximage(ctx
, target
, level
, format
, type
, pixels
,
865 if (stImage
->pt
&& i
< depth
) {
866 st_texture_image_unmap(ctx
->st
, stImage
);
867 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
);
868 pixels
+= dstImageStride
;
872 texImage
->Depth
= depth
;
876 st_texture_image_unmap(ctx
->st
, stImage
);
877 texImage
->Data
= NULL
;
883 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
884 GLenum format
, GLenum type
, GLvoid
* pixels
,
885 struct gl_texture_object
*texObj
,
886 struct gl_texture_image
*texImage
)
888 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
,
889 texObj
, texImage
, 0);
894 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
896 const struct gl_texture_object
*texObj
,
897 const struct gl_texture_image
*texImage
)
899 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
,
900 (struct gl_texture_object
*) texObj
,
901 (struct gl_texture_image
*) texImage
, 1);
907 st_TexSubimage(GLcontext
* ctx
,
909 GLenum target
, GLint level
,
910 GLint xoffset
, GLint yoffset
, GLint zoffset
,
911 GLint width
, GLint height
, GLint depth
,
912 GLenum format
, GLenum type
, const void *pixels
,
913 const struct gl_pixelstore_attrib
*packing
,
914 struct gl_texture_object
*texObj
,
915 struct gl_texture_image
*texImage
)
917 struct st_texture_image
*stImage
= st_texture_image(texImage
);
919 GLuint srcImageStride
= _mesa_image_image_stride(packing
, width
, height
,
923 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
924 _mesa_lookup_enum_by_nr(target
),
925 level
, xoffset
, yoffset
, width
, height
);
928 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
929 type
, pixels
, packing
, "glTexSubImage2D");
933 /* Map buffer if necessary. Need to lock to prevent other contexts
934 * from uploading the buffer under us.
937 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
);
938 dstRowStride
= stImage
->surface
->pitch
* stImage
->surface
->cpp
;
941 for (i
= 0; i
++ < depth
;) {
942 if (!texImage
->TexFormat
->StoreImage(ctx
, dims
, texImage
->_BaseFormat
,
947 texImage
->ImageOffsets
,
949 format
, type
, pixels
, packing
)) {
950 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "intelTexSubImage");
953 if (stImage
->pt
&& i
< depth
) {
954 st_texture_image_unmap(ctx
->st
, stImage
);
955 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
+ i
);
956 pixels
+= srcImageStride
;
961 /* GL_SGIS_generate_mipmap */
962 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
963 _mesa_generate_mipmap(ctx
, target
,
964 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
969 _mesa_unmap_teximage_pbo(ctx
, packing
);
972 st_texture_image_unmap(ctx
->st
, stImage
);
973 texImage
->Data
= NULL
;
980 st_TexSubImage3D(GLcontext
* ctx
,
983 GLint xoffset
, GLint yoffset
, GLint zoffset
,
984 GLsizei width
, GLsizei height
, GLsizei depth
,
985 GLenum format
, GLenum type
,
986 const GLvoid
* pixels
,
987 const struct gl_pixelstore_attrib
*packing
,
988 struct gl_texture_object
*texObj
,
989 struct gl_texture_image
*texImage
)
991 st_TexSubimage(ctx
, 3, target
, level
,
992 xoffset
, yoffset
, zoffset
,
993 width
, height
, depth
,
994 format
, type
, pixels
, packing
, texObj
, texImage
);
1000 st_TexSubImage2D(GLcontext
* ctx
,
1003 GLint xoffset
, GLint yoffset
,
1004 GLsizei width
, GLsizei height
,
1005 GLenum format
, GLenum type
,
1006 const GLvoid
* pixels
,
1007 const struct gl_pixelstore_attrib
*packing
,
1008 struct gl_texture_object
*texObj
,
1009 struct gl_texture_image
*texImage
)
1011 st_TexSubimage(ctx
, 2, target
, level
,
1012 xoffset
, yoffset
, 0,
1014 format
, type
, pixels
, packing
, texObj
, texImage
);
1019 st_TexSubImage1D(GLcontext
* ctx
,
1024 GLenum format
, GLenum type
,
1025 const GLvoid
* pixels
,
1026 const struct gl_pixelstore_attrib
*packing
,
1027 struct gl_texture_object
*texObj
,
1028 struct gl_texture_image
*texImage
)
1030 st_TexSubimage(ctx
, 1, target
, level
,
1033 format
, type
, pixels
, packing
, texObj
, texImage
);
1039 * Return 0 for GL_TEXTURE_CUBE_MAP_POSITIVE_X,
1040 * 1 for GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
1042 * XXX duplicated from main/teximage.c
1045 texture_face(GLenum target
)
1047 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1048 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)
1049 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
1057 * Do a CopyTexSubImage operation by mapping the source region and
1058 * dest region and using get_tile()/put_tile() to access the pixels/texels.
1060 * Note: srcY=0=TOP of renderbuffer
1063 fallback_copy_texsubimage(GLcontext
*ctx
,
1066 struct st_renderbuffer
*strb
,
1067 struct st_texture_image
*stImage
,
1069 GLint destX
, GLint destY
, GLint destZ
,
1070 GLint srcX
, GLint srcY
,
1071 GLsizei width
, GLsizei height
)
1073 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1074 const uint face
= texture_face(target
);
1075 struct pipe_texture
*pt
= stImage
->pt
;
1076 struct pipe_surface
*src_surf
, *dest_surf
;
1080 /* determine bottom-to-top vs. top-to-bottom order */
1081 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1082 destY
= height
- 1 - destY
;
1089 src_surf
= strb
->surface
;
1091 dest_surf
= pipe
->get_tex_surface(pipe
, pt
,
1092 face
, level
, destZ
);
1094 (void) pipe
->region_map(pipe
, dest_surf
->region
);
1095 (void) pipe
->region_map(pipe
, src_surf
->region
);
1097 /* buffer for one row */
1098 data
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
1100 /* do copy row by row */
1101 for (row
= 0; row
< height
; row
++) {
1102 pipe
->get_tile_rgba(pipe
, src_surf
, srcX
, srcY
+ row
, width
, 1, data
);
1104 /* XXX we're ignoring convolution for now */
1105 if (ctx
->_ImageTransferState
) {
1106 _mesa_apply_rgba_transfer_ops(ctx
,
1107 ctx
->_ImageTransferState
& ~IMAGE_CONVOLUTION_BIT
,
1108 width
, (GLfloat (*)[4])data
);
1111 pipe
->put_tile_rgba(pipe
, dest_surf
, destX
, destY
, width
, 1, data
);
1116 (void) pipe
->region_unmap(pipe
, dest_surf
->region
);
1117 (void) pipe
->region_unmap(pipe
, src_surf
->region
);
1126 * Do a CopyTex[Sub]Image using an optimized hardware (blit) path.
1127 * Note that the region to copy has already been clip tested.
1129 * Note: srcY=0=Bottom of renderbuffer
1131 * \return GL_TRUE if success, GL_FALSE if failure (use a fallback)
1134 do_copy_texsubimage(GLcontext
*ctx
,
1135 GLenum target
, GLint level
,
1136 GLint destX
, GLint destY
, GLint destZ
,
1137 GLint srcX
, GLint srcY
,
1138 GLsizei width
, GLsizei height
)
1140 struct gl_texture_unit
*texUnit
=
1141 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1142 struct gl_texture_object
*texObj
=
1143 _mesa_select_tex_object(ctx
, texUnit
, target
);
1144 struct gl_texture_image
*texImage
=
1145 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1146 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1147 GLenum baseFormat
= texImage
->InternalFormat
;
1148 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1149 struct st_renderbuffer
*strb
;
1150 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1151 struct pipe_surface
*dest_surface
;
1152 uint dest_format
, src_format
;
1156 /* determine if copying depth or color data */
1157 if (baseFormat
== GL_DEPTH_COMPONENT
) {
1158 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1160 else if (baseFormat
== GL_DEPTH_STENCIL_EXT
) {
1161 strb
= st_renderbuffer(fb
->_StencilBuffer
);
1164 /* baseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1165 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1169 assert(strb
->surface
);
1170 assert(stImage
->pt
);
1172 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1173 srcY
= strb
->Base
.Height
- srcY
- height
;
1176 src_format
= strb
->surface
->format
;
1177 dest_format
= stImage
->pt
->format
;
1179 dest_surface
= pipe
->get_tex_surface(pipe
, stImage
->pt
, stImage
->face
,
1180 stImage
->level
, destZ
);
1182 if (src_format
== dest_format
&&
1183 ctx
->_ImageTransferState
== 0x0 &&
1184 strb
->surface
->region
&&
1185 dest_surface
->region
&&
1186 strb
->surface
->cpp
== stImage
->pt
->cpp
) {
1187 /* do blit-style copy */
1189 /* XXX may need to invert image depending on window
1190 * vs. user-created FBO
1194 /* A bit of fiddling to get the blitter to work with -ve
1195 * pitches. But we get a nice inverted blit this way, so it's
1198 intelEmitCopyBlit(intel
,
1202 src
->height
* src
->pitch
* src
->cpp
,
1204 stImage
->pt
->region
->buffer
,
1206 x
, y
+ height
, dstx
, dsty
, width
, height
,
1210 pipe
->surface_copy(pipe
,
1222 fallback_copy_texsubimage(ctx
, target
, level
,
1223 strb
, stImage
, baseFormat
,
1224 destX
, destY
, destZ
,
1225 srcX
, srcY
, width
, height
);
1228 pipe_surface_reference(&dest_surface
, NULL
);
1231 /* GL_SGIS_generate_mipmap -- this can be accelerated now.
1232 * XXX Add a ctx->Driver.GenerateMipmaps() function?
1234 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1235 intel_generate_mipmap(ctx
, target
,
1236 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
1246 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1247 GLenum internalFormat
,
1248 GLint x
, GLint y
, GLsizei width
, GLint border
)
1250 struct gl_texture_unit
*texUnit
=
1251 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1252 struct gl_texture_object
*texObj
=
1253 _mesa_select_tex_object(ctx
, texUnit
, target
);
1254 struct gl_texture_image
*texImage
=
1255 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1262 /* Setup or redefine the texture object, texture and texture
1263 * image. Don't populate yet.
1265 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1267 GL_RGBA
, CHAN_TYPE
, NULL
,
1268 &ctx
->DefaultPacking
, texObj
, texImage
);
1270 do_copy_texsubimage(ctx
, target
, level
,
1277 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1278 GLenum internalFormat
,
1279 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1282 struct gl_texture_unit
*texUnit
=
1283 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1284 struct gl_texture_object
*texObj
=
1285 _mesa_select_tex_object(ctx
, texUnit
, target
);
1286 struct gl_texture_image
*texImage
=
1287 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1294 /* Setup or redefine the texture object, texture and texture
1295 * image. Don't populate yet.
1297 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1298 width
, height
, border
,
1299 GL_RGBA
, CHAN_TYPE
, NULL
,
1300 &ctx
->DefaultPacking
, texObj
, texImage
);
1303 do_copy_texsubimage(ctx
, target
, level
,
1305 x
, y
, width
, height
);
1310 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1311 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1313 const GLint yoffset
= 0, zoffset
= 0;
1314 const GLsizei height
= 1;
1315 do_copy_texsubimage(ctx
, target
, level
,
1316 xoffset
, yoffset
, zoffset
,
1317 x
, y
, width
, height
);
1322 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1323 GLint xoffset
, GLint yoffset
,
1324 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1326 const GLint zoffset
= 0;
1327 do_copy_texsubimage(ctx
, target
, level
,
1328 xoffset
, yoffset
, zoffset
,
1329 x
, y
, width
, height
);
1334 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1335 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1336 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1338 do_copy_texsubimage(ctx
, target
, level
,
1339 xoffset
, yoffset
, zoffset
,
1340 x
, y
, width
, height
);
1347 * Compute which mipmap levels that really need to be sent to the hardware.
1348 * This depends on the base image size, GL_TEXTURE_MIN_LOD,
1349 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
1352 calculate_first_last_level(struct st_texture_object
*stObj
)
1354 struct gl_texture_object
*tObj
= &stObj
->base
;
1355 const struct gl_texture_image
*const baseImage
=
1356 tObj
->Image
[0][tObj
->BaseLevel
];
1358 /* These must be signed values. MinLod and MaxLod can be negative numbers,
1359 * and having firstLevel and lastLevel as signed prevents the need for
1360 * extra sign checks.
1365 /* Yes, this looks overly complicated, but it's all needed.
1367 switch (tObj
->Target
) {
1371 case GL_TEXTURE_CUBE_MAP
:
1372 if (tObj
->MinFilter
== GL_NEAREST
|| tObj
->MinFilter
== GL_LINEAR
) {
1373 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
1375 firstLevel
= lastLevel
= tObj
->BaseLevel
;
1378 firstLevel
= tObj
->BaseLevel
+ (GLint
) (tObj
->MinLod
+ 0.5);
1379 firstLevel
= MAX2(firstLevel
, tObj
->BaseLevel
);
1380 lastLevel
= tObj
->BaseLevel
+ (GLint
) (tObj
->MaxLod
+ 0.5);
1381 lastLevel
= MAX2(lastLevel
, tObj
->BaseLevel
);
1382 lastLevel
= MIN2(lastLevel
, tObj
->BaseLevel
+ baseImage
->MaxLog2
);
1383 lastLevel
= MIN2(lastLevel
, tObj
->MaxLevel
);
1384 lastLevel
= MAX2(firstLevel
, lastLevel
); /* need at least one level */
1387 case GL_TEXTURE_RECTANGLE_NV
:
1388 case GL_TEXTURE_4D_SGIS
:
1389 firstLevel
= lastLevel
= 0;
1395 /* save these values */
1396 stObj
->firstLevel
= firstLevel
;
1397 stObj
->lastLevel
= lastLevel
;
1402 copy_image_data_to_texture(struct st_context
*st
,
1403 struct st_texture_object
*stObj
,
1404 struct st_texture_image
*stImage
)
1407 /* Copy potentially with the blitter:
1409 st_texture_image_copy(st
->pipe
,
1410 stObj
->pt
, /* dest texture */
1411 stImage
->face
, stImage
->level
,
1412 stImage
->pt
/* src texture */
1415 st
->pipe
->texture_release(st
->pipe
, &stImage
->pt
);
1418 assert(stImage
->base
.Data
!= NULL
);
1420 /* More straightforward upload.
1422 st_texture_image_data(st
->pipe
,
1427 stImage
->base
.RowStride
,
1428 stImage
->base
.RowStride
*
1429 stImage
->base
.Height
);
1430 _mesa_align_free(stImage
->base
.Data
);
1431 stImage
->base
.Data
= NULL
;
1434 pipe_texture_reference(st
->pipe
, &stImage
->pt
, stObj
->pt
);
1441 st_finalize_texture(GLcontext
*ctx
,
1442 struct pipe_context
*pipe
, GLuint unit
,
1443 GLboolean
*needFlush
)
1445 struct gl_texture_object
*tObj
= ctx
->Texture
.Unit
[unit
]._Current
;
1446 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1451 GLuint nr_faces
= 0;
1452 struct st_texture_image
*firstImage
;
1454 *needFlush
= GL_FALSE
;
1456 /* We know/require this is true by now:
1458 assert(stObj
->base
._Complete
);
1460 /* What levels must the texture include at a minimum?
1462 calculate_first_last_level(stObj
);
1464 st_texture_image(stObj
->base
.Image
[0][stObj
->firstLevel
]);
1468 if (firstImage
->base
.Border
) {
1470 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stObj
->pt
);
1476 /* If both firstImage and stObj point to a texture which can contain
1477 * all active images, favour firstImage. Note that because of the
1478 * completeness requirement, we know that the image dimensions
1481 if (firstImage
->pt
&&
1482 firstImage
->pt
!= stObj
->pt
&&
1483 firstImage
->pt
->first_level
<= stObj
->firstLevel
&&
1484 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1487 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stObj
->pt
);
1489 pipe_texture_reference(ctx
->st
->pipe
, &stObj
->pt
, firstImage
->pt
);
1492 if (firstImage
->base
.IsCompressed
) {
1493 comp_byte
= compressed_num_bytes(firstImage
->base
.TexFormat
->MesaFormat
);
1497 cpp
= firstImage
->base
.TexFormat
->TexelBytes
;
1500 /* Check texture can hold all active levels. Check texture matches
1501 * target, imageFormat, etc.
1503 * XXX: For some layouts (eg i945?), the test might have to be
1504 * first_level == firstLevel, as the texture isn't valid except at the
1505 * original start level. Hope to get around this by
1506 * programming minLod, maxLod, baseLevel into the hardware and
1507 * leaving the texture alone.
1510 (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1511 stObj
->pt
->internal_format
!= firstImage
->base
.InternalFormat
||
1512 stObj
->pt
->first_level
!= stObj
->firstLevel
||
1513 stObj
->pt
->last_level
!= stObj
->lastLevel
||
1514 stObj
->pt
->width
[0] != firstImage
->base
.Width
||
1515 stObj
->pt
->height
[0] != firstImage
->base
.Height
||
1516 stObj
->pt
->depth
[0] != firstImage
->base
.Depth
||
1517 stObj
->pt
->cpp
!= cpp
||
1518 stObj
->pt
->compressed
!= firstImage
->base
.IsCompressed
)) {
1519 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stObj
->pt
);
1523 /* May need to create a new texture:
1526 stObj
->pt
= st_texture_create(ctx
->st
,
1527 gl_target_to_pipe(stObj
->base
.Target
),
1528 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
->MesaFormat
),
1529 firstImage
->base
.InternalFormat
,
1532 firstImage
->base
.Width
,
1533 firstImage
->base
.Height
,
1534 firstImage
->base
.Depth
,
1538 /* Pull in any images not in the object's texture:
1540 nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1541 for (face
= 0; face
< nr_faces
; face
++) {
1542 for (i
= stObj
->firstLevel
; i
<= stObj
->lastLevel
; i
++) {
1543 struct st_texture_image
*stImage
=
1544 st_texture_image(stObj
->base
.Image
[face
][i
]);
1546 /* Need to import images in main memory or held in other textures.
1548 if (stObj
->pt
!= stImage
->pt
) {
1549 copy_image_data_to_texture(ctx
->st
, stObj
, stImage
);
1550 *needFlush
= GL_TRUE
;
1563 st_init_texture_functions(struct dd_function_table
*functions
)
1565 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1566 functions
->TexImage1D
= st_TexImage1D
;
1567 functions
->TexImage2D
= st_TexImage2D
;
1568 functions
->TexImage3D
= st_TexImage3D
;
1569 functions
->TexSubImage1D
= st_TexSubImage1D
;
1570 functions
->TexSubImage2D
= st_TexSubImage2D
;
1571 functions
->TexSubImage3D
= st_TexSubImage3D
;
1572 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1573 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1574 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1575 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1576 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1578 functions
->GetTexImage
= st_GetTexImage
;
1580 /* compressed texture functions */
1581 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1582 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1584 functions
->NewTextureObject
= st_NewTextureObject
;
1585 functions
->NewTextureImage
= st_NewTextureImage
;
1586 functions
->DeleteTexture
= st_DeleteTextureObject
;
1587 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1588 functions
->UpdateTexturePalette
= 0;
1589 functions
->IsTextureResident
= st_IsTextureResident
;
1591 functions
->TextureMemCpy
= do_memcpy
;
1593 /* XXX Temporary until we can query pipe's texture sizes */
1594 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;