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/mipmap.h"
34 #include "main/texcompress.h"
35 #include "main/texformat.h"
36 #include "main/teximage.h"
37 #include "main/texobj.h"
38 #include "main/texstore.h"
40 #include "state_tracker/st_context.h"
41 #include "state_tracker/st_cb_fbo.h"
42 #include "state_tracker/st_cb_texture.h"
43 #include "state_tracker/st_format.h"
44 #include "state_tracker/st_texture.h"
45 #include "state_tracker/st_gen_mipmap.h"
47 #include "pipe/p_context.h"
48 #include "pipe/p_defines.h"
49 #include "pipe/p_inlines.h"
50 #include "pipe/util/p_tile.h"
53 #define DBG if (0) printf
56 struct st_texture_object
58 struct gl_texture_object base
; /* The "parent" object */
60 /* The texture must include at least these levels once validated:
65 /* Offset for firstLevel image:
69 /* On validation any active images held in main memory or in other
70 * textures will be copied to this texture and the old storage freed.
72 struct pipe_texture
*pt
;
74 GLboolean imageOverride
;
81 static INLINE
struct st_texture_object
*
82 st_texture_object(struct gl_texture_object
*obj
)
84 return (struct st_texture_object
*) obj
;
88 static INLINE
struct st_texture_image
*
89 st_texture_image(struct gl_texture_image
*img
)
91 return (struct st_texture_image
*) img
;
96 st_get_texobj_texture(struct gl_texture_object
*texObj
)
98 struct st_texture_object
*stObj
= st_texture_object(texObj
);
103 static enum pipe_texture_target
104 gl_target_to_pipe(GLenum target
)
108 return PIPE_TEXTURE_1D
;
111 case GL_TEXTURE_RECTANGLE_NV
:
112 return PIPE_TEXTURE_2D
;
115 return PIPE_TEXTURE_3D
;
117 case GL_TEXTURE_CUBE_MAP_ARB
:
118 return PIPE_TEXTURE_CUBE
;
128 * Return nominal bytes per texel for a compressed format, 0 for non-compressed
132 compressed_num_bytes(GLuint mesaFormat
)
135 case MESA_FORMAT_RGB_FXT1
:
136 case MESA_FORMAT_RGBA_FXT1
:
137 case MESA_FORMAT_RGB_DXT1
:
138 case MESA_FORMAT_RGBA_DXT1
:
140 case MESA_FORMAT_RGBA_DXT3
:
141 case MESA_FORMAT_RGBA_DXT5
:
150 st_IsTextureResident(GLcontext
* ctx
, struct gl_texture_object
*texObj
)
153 struct intel_context
*intel
= intel_context(ctx
);
154 struct st_texture_object
*stObj
= st_texture_object(texObj
);
159 intel_is_region_resident(intel
, stObj
->pt
->region
);
165 static struct gl_texture_image
*
166 st_NewTextureImage(GLcontext
* ctx
)
168 DBG("%s\n", __FUNCTION__
);
170 return (struct gl_texture_image
*) CALLOC_STRUCT(st_texture_image
);
174 static struct gl_texture_object
*
175 st_NewTextureObject(GLcontext
* ctx
, GLuint name
, GLenum target
)
177 struct st_texture_object
*obj
= CALLOC_STRUCT(st_texture_object
);
179 DBG("%s\n", __FUNCTION__
);
180 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
186 st_DeleteTextureObject(GLcontext
*ctx
,
187 struct gl_texture_object
*texObj
)
189 struct st_texture_object
*stObj
= st_texture_object(texObj
);
192 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stObj
->pt
);
194 _mesa_delete_texture_object(ctx
, texObj
);
199 st_FreeTextureImageData(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
201 struct st_texture_image
*stImage
= st_texture_image(texImage
);
203 DBG("%s\n", __FUNCTION__
);
206 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stImage
->pt
);
209 if (texImage
->Data
) {
210 free(texImage
->Data
);
211 texImage
->Data
= NULL
;
216 /* ================================================================
217 * From linux kernel i386 header files, copes with odd sizes better
218 * than COPY_DWORDS would:
219 * XXX Put this in src/mesa/main/imports.h ???
221 #if defined(i386) || defined(__i386__)
223 __memcpy(void *to
, const void *from
, size_t n
)
226 __asm__
__volatile__("rep ; movsl\n\t"
230 "1:\ttestb $1,%b4\n\t"
232 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
233 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
238 #define __memcpy(a,b,c) memcpy(a,b,c)
242 /* The system memcpy (at least on ubuntu 5.10) has problems copying
243 * to agp (writecombined) memory from a source which isn't 64-byte
244 * aligned - there is a 4x performance falloff.
246 * The x86 __memcpy is immune to this but is slightly slower
247 * (10%-ish) than the system memcpy.
249 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
250 * isn't much faster than x86_memcpy for agp copies.
252 * TODO: switch dynamically.
255 do_memcpy(void *dest
, const void *src
, size_t n
)
257 if ((((unsigned) src
) & 63) || (((unsigned) dest
) & 63)) {
258 return __memcpy(dest
, src
, n
);
261 return memcpy(dest
, src
, n
);
265 /* Functions to store texture images. Where possible, textures
266 * will be created or further instantiated with image data, otherwise
267 * images will be stored in malloc'd memory. A validation step is
268 * required to pull those images into a texture, or otherwise
269 * decide a fallback is required.
289 * Allocate a pipe_texture object for the given st_texture_object using
290 * the given st_texture_image to guess the mipmap size/levels.
293 * Otherwise, store it in memory if (Border != 0) or (any dimension ==
296 * Otherwise, if max_level >= level >= min_level, create texture with
297 * space for images from min_level down to max_level.
299 * Otherwise, create texture with space for images from (level 0)..(1x1).
300 * Consider pruning this texture at a validation if the saving is worth it.
303 guess_and_alloc_texture(struct st_context
*st
,
304 struct st_texture_object
*stObj
,
305 const struct st_texture_image
*stImage
)
309 GLuint width
= stImage
->base
.Width
;
310 GLuint height
= stImage
->base
.Height
;
311 GLuint depth
= stImage
->base
.Depth
;
312 GLuint i
, comp_byte
= 0;
314 DBG("%s\n", __FUNCTION__
);
318 if (stImage
->base
.Border
)
321 if (stImage
->level
> stObj
->base
.BaseLevel
&&
322 (stImage
->base
.Width
== 1 ||
323 (stObj
->base
.Target
!= GL_TEXTURE_1D
&&
324 stImage
->base
.Height
== 1) ||
325 (stObj
->base
.Target
== GL_TEXTURE_3D
&&
326 stImage
->base
.Depth
== 1)))
329 /* If this image disrespects BaseLevel, allocate from level zero.
330 * Usually BaseLevel == 0, so it's unlikely to happen.
332 if (stImage
->level
< stObj
->base
.BaseLevel
)
335 firstLevel
= stObj
->base
.BaseLevel
;
338 /* Figure out image dimensions at start level.
340 for (i
= stImage
->level
; i
> firstLevel
; i
--) {
348 /* Guess a reasonable value for lastLevel. This is probably going
349 * to be wrong fairly often and might mean that we have to look at
350 * resizable buffers, or require that buffers implement lazy
351 * pagetable arrangements.
353 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
354 stObj
->base
.MinFilter
== GL_LINEAR
) &&
355 stImage
->level
== firstLevel
) {
356 lastLevel
= firstLevel
;
359 GLuint l2width
= logbase2(width
);
360 GLuint l2height
= logbase2(height
);
361 GLuint l2depth
= logbase2(depth
);
362 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
365 if (stImage
->base
.IsCompressed
)
366 comp_byte
= compressed_num_bytes(stImage
->base
.TexFormat
->MesaFormat
);
368 stObj
->pt
= st_texture_create(st
,
369 gl_target_to_pipe(stObj
->base
.Target
),
370 st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
->MesaFormat
),
378 DBG("%s - success\n", __FUNCTION__
);
385 target_to_face(GLenum target
)
388 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
389 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
390 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
391 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
392 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
393 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
394 return ((GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
);
402 /* There are actually quite a few combinations this will work for,
403 * more than what I've listed here.
406 check_pbo_format(GLint internalFormat
,
407 GLenum format
, GLenum type
,
408 const struct gl_texture_format
*mesa_format
)
410 switch (internalFormat
) {
413 return (format
== GL_BGRA
&&
414 (type
== GL_UNSIGNED_BYTE
||
415 type
== GL_UNSIGNED_INT_8_8_8_8_REV
) &&
416 mesa_format
== &_mesa_texformat_argb8888
);
419 return (format
== GL_RGB
&&
420 type
== GL_UNSIGNED_SHORT_5_6_5
&&
421 mesa_format
== &_mesa_texformat_rgb565
);
423 return (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
);
430 /* XXX: Do this for TexSubImage also:
433 try_pbo_upload(GLcontext
*ctx
,
434 struct st_texture_image
*stImage
,
435 const struct gl_pixelstore_attrib
*unpack
,
436 GLint internalFormat
,
437 GLint width
, GLint height
,
438 GLenum format
, GLenum type
, const void *pixels
)
440 return GL_FALSE
; /* XXX fix flushing/locking/blitting below */
442 struct intel_context
*intel
= intel_context(ctx
);
443 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
444 GLuint src_offset
, src_stride
;
445 GLuint dst_offset
, dst_stride
;
448 ctx
._ImageTransferState
||
449 unpack
->SkipPixels
|| unpack
->SkipRows
) {
450 _mesa_printf("%s: failure 1\n", __FUNCTION__
);
454 src_offset
= (GLuint
) pixels
;
456 if (unpack
->RowLength
> 0)
457 src_stride
= unpack
->RowLength
;
461 dst_offset
= st_texture_image_offset(stImage
->pt
,
465 dst_stride
= stImage
->pt
->pitch
;
468 struct _DriBufferObject
*src_buffer
=
469 intel_bufferobj_buffer(intel
, pbo
, INTEL_READ
);
471 /* Temporary hack: cast to _DriBufferObject:
473 struct _DriBufferObject
*dst_buffer
=
474 (struct _DriBufferObject
*)stImage
->pt
->region
->buffer
;
477 intelEmitCopyBlit(intel
,
479 src_stride
, src_buffer
, src_offset
,
480 dst_stride
, dst_buffer
, dst_offset
,
481 0, 0, 0, 0, width
, height
,
492 st_TexImage(GLcontext
* ctx
,
494 GLenum target
, GLint level
,
495 GLint internalFormat
,
496 GLint width
, GLint height
, GLint depth
,
498 GLenum format
, GLenum type
, const void *pixels
,
499 const struct gl_pixelstore_attrib
*unpack
,
500 struct gl_texture_object
*texObj
,
501 struct gl_texture_image
*texImage
,
502 GLsizei imageSize
, int compressed
)
504 struct st_texture_object
*stObj
= st_texture_object(texObj
);
505 struct st_texture_image
*stImage
= st_texture_image(texImage
);
506 GLint postConvWidth
= width
;
507 GLint postConvHeight
= height
;
508 GLint texelBytes
, sizeInBytes
;
512 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
513 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
515 stImage
->face
= target_to_face(target
);
516 stImage
->level
= level
;
518 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
519 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
523 /* choose the texture format */
524 texImage
->TexFormat
= st_ChooseTextureFormat(ctx
, internalFormat
,
527 _mesa_set_fetch_functions(texImage
, dims
);
529 if (texImage
->TexFormat
->TexelBytes
== 0) {
530 /* must be a compressed format */
532 texImage
->IsCompressed
= GL_TRUE
;
533 texImage
->CompressedSize
=
534 ctx
->Driver
.CompressedTextureSize(ctx
, texImage
->Width
,
535 texImage
->Height
, texImage
->Depth
,
536 texImage
->TexFormat
->MesaFormat
);
539 texelBytes
= texImage
->TexFormat
->TexelBytes
;
541 /* Minimum pitch of 32 bytes */
542 if (postConvWidth
* texelBytes
< 32) {
543 postConvWidth
= 32 / texelBytes
;
544 texImage
->RowStride
= postConvWidth
;
547 assert(texImage
->RowStride
== postConvWidth
);
550 /* Release the reference to a potentially orphaned buffer.
551 * Release any old malloced memory.
554 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stImage
->pt
);
555 assert(!texImage
->Data
);
557 else if (texImage
->Data
) {
558 _mesa_align_free(texImage
->Data
);
561 /* If this is the only texture image in the texture, could call
562 * bmBufferData with NULL data to free the old block and avoid
563 * waiting on any outstanding fences.
566 stObj
->pt
->first_level
== level
&&
567 stObj
->pt
->last_level
== level
&&
568 stObj
->pt
->target
!= PIPE_TEXTURE_CUBE
&&
569 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
570 stImage
->face
, stImage
->level
)) {
573 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stObj
->pt
);
578 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
580 DBG("guess_and_alloc_texture: failed\n");
584 assert(!stImage
->pt
);
587 st_texture_match_image(stObj
->pt
, &stImage
->base
,
588 stImage
->face
, stImage
->level
)) {
590 pipe_texture_reference(ctx
->st
->pipe
, &stImage
->pt
, stObj
->pt
);
595 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
597 #if 0 /* XXX FIX when st_buffer_objects are in place */
602 intel_buffer_object(unpack
->BufferObj
) &&
603 check_pbo_format(internalFormat
, format
,
604 type
, texImage
->TexFormat
)) {
606 DBG("trying pbo upload\n");
610 /* Otherwise, attempt to use the blitter for PBO image uploads.
612 if (try_pbo_upload(intel
, stImage
, unpack
,
614 width
, height
, format
, type
, pixels
)) {
615 DBG("pbo upload succeeded\n");
619 DBG("pbo upload failed\n");
622 (void) try_pbo_upload
;
623 (void) check_pbo_format
;
627 /* st_CopyTexImage calls this function with pixels == NULL, with
628 * the expectation that the texture will be set up but nothing
629 * more will be done. This is where those calls return:
632 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
634 "glCompressedTexImage");
636 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
638 pixels
, unpack
, "glTexImage");
644 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0);
645 dstRowStride
= stImage
->surface
->pitch
* stImage
->surface
->cpp
;
648 /* Allocate regular memory and store the image there temporarily. */
649 if (texImage
->IsCompressed
) {
650 sizeInBytes
= texImage
->CompressedSize
;
652 _mesa_compressed_row_stride(texImage
->TexFormat
->MesaFormat
, width
);
656 dstRowStride
= postConvWidth
* texelBytes
;
657 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
660 texImage
->Data
= malloc(sizeInBytes
);
663 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
664 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
666 /* Copy data. Would like to know when it's ok for us to eg. use
667 * the blitter to copy. Or, use the hardware to do the format
668 * conversion and copy:
671 memcpy(texImage
->Data
, pixels
, imageSize
);
674 GLuint srcImageStride
= _mesa_image_image_stride(unpack
, width
, height
,
677 const GLubyte
*src
= (const GLubyte
*) pixels
;
679 for (i
= 0; i
++ < depth
;) {
680 if (!texImage
->TexFormat
->StoreImage(ctx
, dims
,
681 texImage
->_BaseFormat
,
684 0, 0, 0, /* dstX/Y/Zoffset */
686 texImage
->ImageOffsets
,
688 format
, type
, src
, unpack
)) {
689 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
692 if (stImage
->pt
&& i
< depth
) {
693 st_texture_image_unmap(stImage
);
694 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
);
695 src
+= srcImageStride
;
700 _mesa_unmap_teximage_pbo(ctx
, unpack
);
703 st_texture_image_unmap(stImage
);
704 texImage
->Data
= NULL
;
708 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
709 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
716 st_TexImage3D(GLcontext
* ctx
,
717 GLenum target
, GLint level
,
718 GLint internalFormat
,
719 GLint width
, GLint height
, GLint depth
,
721 GLenum format
, GLenum type
, const void *pixels
,
722 const struct gl_pixelstore_attrib
*unpack
,
723 struct gl_texture_object
*texObj
,
724 struct gl_texture_image
*texImage
)
726 st_TexImage(ctx
, 3, target
, level
,
727 internalFormat
, width
, height
, depth
, border
,
728 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
733 st_TexImage2D(GLcontext
* ctx
,
734 GLenum target
, GLint level
,
735 GLint internalFormat
,
736 GLint width
, GLint height
, GLint border
,
737 GLenum format
, GLenum type
, const void *pixels
,
738 const struct gl_pixelstore_attrib
*unpack
,
739 struct gl_texture_object
*texObj
,
740 struct gl_texture_image
*texImage
)
742 st_TexImage(ctx
, 2, target
, level
,
743 internalFormat
, width
, height
, 1, border
,
744 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
749 st_TexImage1D(GLcontext
* ctx
,
750 GLenum target
, GLint level
,
751 GLint internalFormat
,
752 GLint width
, GLint border
,
753 GLenum format
, GLenum type
, const void *pixels
,
754 const struct gl_pixelstore_attrib
*unpack
,
755 struct gl_texture_object
*texObj
,
756 struct gl_texture_image
*texImage
)
758 st_TexImage(ctx
, 1, target
, level
,
759 internalFormat
, width
, 1, 1, border
,
760 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
765 st_CompressedTexImage2D( GLcontext
*ctx
, GLenum target
, GLint level
,
766 GLint internalFormat
,
767 GLint width
, GLint height
, GLint border
,
768 GLsizei imageSize
, const GLvoid
*data
,
769 struct gl_texture_object
*texObj
,
770 struct gl_texture_image
*texImage
)
772 st_TexImage(ctx
, 2, target
, level
,
773 internalFormat
, width
, height
, 1, border
,
774 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, 1);
779 * Need to map texture image into memory before copying image data,
783 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
784 GLenum format
, GLenum type
, GLvoid
* pixels
,
785 struct gl_texture_object
*texObj
,
786 struct gl_texture_image
*texImage
, int compressed
)
788 struct st_texture_image
*stImage
= st_texture_image(texImage
);
789 GLuint dstImageStride
= _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
,
790 texImage
->Height
, format
,
798 /* Image is stored in hardware format in a buffer managed by the
799 * kernel. Need to explicitly map and unmap it.
801 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0);
802 texImage
->RowStride
= stImage
->surface
->pitch
;
805 /* Otherwise, the image should actually be stored in
806 * texImage->Data. This is pretty confusing for
807 * everybody, I'd much prefer to separate the two functions of
808 * texImage->Data - storage for texture images in main memory
809 * and access (ie mappings) of images. In other words, we'd
810 * create a new texImage->Map field and leave Data simply for
813 assert(texImage
->Data
);
816 depth
= texImage
->Depth
;
819 dest
= (GLubyte
*) pixels
;
821 for (i
= 0; i
++ < depth
;) {
823 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
826 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
830 if (stImage
->pt
&& i
< depth
) {
831 st_texture_image_unmap(stImage
);
832 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
);
833 dest
+= dstImageStride
;
837 texImage
->Depth
= depth
;
841 st_texture_image_unmap(stImage
);
842 texImage
->Data
= NULL
;
848 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
849 GLenum format
, GLenum type
, GLvoid
* pixels
,
850 struct gl_texture_object
*texObj
,
851 struct gl_texture_image
*texImage
)
853 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
,
854 texObj
, texImage
, 0);
859 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
861 const struct gl_texture_object
*texObj
,
862 const struct gl_texture_image
*texImage
)
864 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
,
865 (struct gl_texture_object
*) texObj
,
866 (struct gl_texture_image
*) texImage
, 1);
872 st_TexSubimage(GLcontext
* ctx
,
874 GLenum target
, GLint level
,
875 GLint xoffset
, GLint yoffset
, GLint zoffset
,
876 GLint width
, GLint height
, GLint depth
,
877 GLenum format
, GLenum type
, const void *pixels
,
878 const struct gl_pixelstore_attrib
*packing
,
879 struct gl_texture_object
*texObj
,
880 struct gl_texture_image
*texImage
)
882 struct st_texture_image
*stImage
= st_texture_image(texImage
);
884 GLuint srcImageStride
= _mesa_image_image_stride(packing
, width
, height
,
889 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
890 _mesa_lookup_enum_by_nr(target
),
891 level
, xoffset
, yoffset
, width
, height
);
894 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
895 type
, pixels
, packing
, "glTexSubImage2D");
899 /* Map buffer if necessary. Need to lock to prevent other contexts
900 * from uploading the buffer under us.
903 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
);
904 dstRowStride
= stImage
->surface
->pitch
* stImage
->surface
->cpp
;
907 src
= (const GLubyte
*) pixels
;
909 for (i
= 0; i
++ < depth
;) {
910 if (!texImage
->TexFormat
->StoreImage(ctx
, dims
, texImage
->_BaseFormat
,
915 texImage
->ImageOffsets
,
917 format
, type
, src
, packing
)) {
918 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "st_TexSubImage");
921 if (stImage
->pt
&& i
< depth
) {
922 st_texture_image_unmap(stImage
);
923 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
+ i
);
924 src
+= srcImageStride
;
929 /* GL_SGIS_generate_mipmap */
930 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
931 _mesa_generate_mipmap(ctx
, target
,
932 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
937 _mesa_unmap_teximage_pbo(ctx
, packing
);
940 st_texture_image_unmap(stImage
);
941 texImage
->Data
= NULL
;
948 st_TexSubImage3D(GLcontext
* ctx
,
951 GLint xoffset
, GLint yoffset
, GLint zoffset
,
952 GLsizei width
, GLsizei height
, GLsizei depth
,
953 GLenum format
, GLenum type
,
954 const GLvoid
* pixels
,
955 const struct gl_pixelstore_attrib
*packing
,
956 struct gl_texture_object
*texObj
,
957 struct gl_texture_image
*texImage
)
959 st_TexSubimage(ctx
, 3, target
, level
,
960 xoffset
, yoffset
, zoffset
,
961 width
, height
, depth
,
962 format
, type
, pixels
, packing
, texObj
, texImage
);
968 st_TexSubImage2D(GLcontext
* ctx
,
971 GLint xoffset
, GLint yoffset
,
972 GLsizei width
, GLsizei height
,
973 GLenum format
, GLenum type
,
974 const GLvoid
* pixels
,
975 const struct gl_pixelstore_attrib
*packing
,
976 struct gl_texture_object
*texObj
,
977 struct gl_texture_image
*texImage
)
979 st_TexSubimage(ctx
, 2, target
, level
,
982 format
, type
, pixels
, packing
, texObj
, texImage
);
987 st_TexSubImage1D(GLcontext
* ctx
,
992 GLenum format
, GLenum type
,
993 const GLvoid
* pixels
,
994 const struct gl_pixelstore_attrib
*packing
,
995 struct gl_texture_object
*texObj
,
996 struct gl_texture_image
*texImage
)
998 st_TexSubimage(ctx
, 1, target
, level
,
1001 format
, type
, pixels
, packing
, texObj
, texImage
);
1007 * Return 0 for GL_TEXTURE_CUBE_MAP_POSITIVE_X,
1008 * 1 for GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
1010 * XXX duplicated from main/teximage.c
1013 texture_face(GLenum target
)
1015 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
1016 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)
1017 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
1025 * Do a CopyTexSubImage operation by mapping the source surface and
1026 * dest surface and using get_tile()/put_tile() to access the pixels/texels.
1028 * Note: srcY=0=TOP of renderbuffer
1031 fallback_copy_texsubimage(GLcontext
*ctx
,
1034 struct st_renderbuffer
*strb
,
1035 struct st_texture_image
*stImage
,
1037 GLint destX
, GLint destY
, GLint destZ
,
1038 GLint srcX
, GLint srcY
,
1039 GLsizei width
, GLsizei height
)
1041 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1042 const uint face
= texture_face(target
);
1043 struct pipe_texture
*pt
= stImage
->pt
;
1044 struct pipe_surface
*src_surf
, *dest_surf
;
1048 /* determine bottom-to-top vs. top-to-bottom order */
1049 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1050 destY
= height
- 1 - destY
;
1057 src_surf
= strb
->surface
;
1059 dest_surf
= pipe
->get_tex_surface(pipe
, pt
,
1060 face
, level
, destZ
);
1062 /* buffer for one row */
1063 data
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
1065 /* do copy row by row */
1066 for (row
= 0; row
< height
; row
++) {
1067 pipe_get_tile_rgba(pipe
, src_surf
, srcX
, srcY
+ row
, width
, 1, data
);
1069 /* XXX we're ignoring convolution for now */
1070 if (ctx
->_ImageTransferState
) {
1071 _mesa_apply_rgba_transfer_ops(ctx
,
1072 ctx
->_ImageTransferState
& ~IMAGE_CONVOLUTION_BIT
,
1073 width
, (GLfloat (*)[4])data
);
1076 pipe_put_tile_rgba(pipe
, dest_surf
, destX
, destY
, width
, 1, data
);
1087 * Do a CopyTex[Sub]Image using an optimized hardware (blit) path.
1088 * Note that the region to copy has already been clip tested.
1090 * Note: srcY=0=Bottom of renderbuffer
1092 * \return GL_TRUE if success, GL_FALSE if failure (use a fallback)
1095 do_copy_texsubimage(GLcontext
*ctx
,
1096 GLenum target
, GLint level
,
1097 GLint destX
, GLint destY
, GLint destZ
,
1098 GLint srcX
, GLint srcY
,
1099 GLsizei width
, GLsizei height
)
1101 struct gl_texture_unit
*texUnit
=
1102 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1103 struct gl_texture_object
*texObj
=
1104 _mesa_select_tex_object(ctx
, texUnit
, target
);
1105 struct gl_texture_image
*texImage
=
1106 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1107 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1108 GLenum baseFormat
= texImage
->InternalFormat
;
1109 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1110 struct st_renderbuffer
*strb
;
1111 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1112 struct pipe_surface
*dest_surface
;
1113 uint dest_format
, src_format
;
1117 /* determine if copying depth or color data */
1118 if (baseFormat
== GL_DEPTH_COMPONENT
) {
1119 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1121 else if (baseFormat
== GL_DEPTH_STENCIL_EXT
) {
1122 strb
= st_renderbuffer(fb
->_StencilBuffer
);
1125 /* baseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1126 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1130 assert(strb
->surface
);
1131 assert(stImage
->pt
);
1133 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1134 srcY
= strb
->Base
.Height
- srcY
- height
;
1137 src_format
= strb
->surface
->format
;
1138 dest_format
= stImage
->pt
->format
;
1140 dest_surface
= pipe
->get_tex_surface(pipe
, stImage
->pt
, stImage
->face
,
1141 stImage
->level
, destZ
);
1143 if (src_format
== dest_format
&&
1144 ctx
->_ImageTransferState
== 0x0 &&
1145 strb
->surface
->buffer
&&
1146 dest_surface
->buffer
&&
1147 strb
->surface
->cpp
== stImage
->pt
->cpp
) {
1148 /* do blit-style copy */
1150 /* XXX may need to invert image depending on window
1151 * vs. user-created FBO
1155 /* A bit of fiddling to get the blitter to work with -ve
1156 * pitches. But we get a nice inverted blit this way, so it's
1159 intelEmitCopyBlit(intel
,
1163 src
->height
* src
->pitch
* src
->cpp
,
1165 stImage
->pt
->region
->buffer
,
1167 x
, y
+ height
, dstx
, dsty
, width
, height
,
1171 pipe
->surface_copy(pipe
,
1183 fallback_copy_texsubimage(ctx
, target
, level
,
1184 strb
, stImage
, baseFormat
,
1185 destX
, destY
, destZ
,
1186 srcX
, srcY
, width
, height
);
1189 pipe_surface_reference(&dest_surface
, NULL
);
1192 /* GL_SGIS_generate_mipmap -- this can be accelerated now.
1193 * XXX Add a ctx->Driver.GenerateMipmaps() function?
1195 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1196 intel_generate_mipmap(ctx
, target
,
1197 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
1207 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1208 GLenum internalFormat
,
1209 GLint x
, GLint y
, GLsizei width
, GLint border
)
1211 struct gl_texture_unit
*texUnit
=
1212 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1213 struct gl_texture_object
*texObj
=
1214 _mesa_select_tex_object(ctx
, texUnit
, target
);
1215 struct gl_texture_image
*texImage
=
1216 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1223 /* Setup or redefine the texture object, texture and texture
1224 * image. Don't populate yet.
1226 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1228 GL_RGBA
, CHAN_TYPE
, NULL
,
1229 &ctx
->DefaultPacking
, texObj
, texImage
);
1231 do_copy_texsubimage(ctx
, target
, level
,
1238 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1239 GLenum internalFormat
,
1240 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1243 struct gl_texture_unit
*texUnit
=
1244 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1245 struct gl_texture_object
*texObj
=
1246 _mesa_select_tex_object(ctx
, texUnit
, target
);
1247 struct gl_texture_image
*texImage
=
1248 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1255 /* Setup or redefine the texture object, texture and texture
1256 * image. Don't populate yet.
1258 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1259 width
, height
, border
,
1260 GL_RGBA
, CHAN_TYPE
, NULL
,
1261 &ctx
->DefaultPacking
, texObj
, texImage
);
1264 do_copy_texsubimage(ctx
, target
, level
,
1266 x
, y
, width
, height
);
1271 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1272 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1274 const GLint yoffset
= 0, zoffset
= 0;
1275 const GLsizei height
= 1;
1276 do_copy_texsubimage(ctx
, target
, level
,
1277 xoffset
, yoffset
, zoffset
,
1278 x
, y
, width
, height
);
1283 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1284 GLint xoffset
, GLint yoffset
,
1285 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1287 const GLint zoffset
= 0;
1288 do_copy_texsubimage(ctx
, target
, level
,
1289 xoffset
, yoffset
, zoffset
,
1290 x
, y
, width
, height
);
1295 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1296 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1297 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1299 do_copy_texsubimage(ctx
, target
, level
,
1300 xoffset
, yoffset
, zoffset
,
1301 x
, y
, width
, height
);
1308 * Compute which mipmap levels that really need to be sent to the hardware.
1309 * This depends on the base image size, GL_TEXTURE_MIN_LOD,
1310 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
1313 calculate_first_last_level(struct st_texture_object
*stObj
)
1315 struct gl_texture_object
*tObj
= &stObj
->base
;
1316 const struct gl_texture_image
*const baseImage
=
1317 tObj
->Image
[0][tObj
->BaseLevel
];
1319 /* These must be signed values. MinLod and MaxLod can be negative numbers,
1320 * and having firstLevel and lastLevel as signed prevents the need for
1321 * extra sign checks.
1326 /* Yes, this looks overly complicated, but it's all needed.
1328 switch (tObj
->Target
) {
1332 case GL_TEXTURE_CUBE_MAP
:
1333 if (tObj
->MinFilter
== GL_NEAREST
|| tObj
->MinFilter
== GL_LINEAR
) {
1334 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
1336 firstLevel
= lastLevel
= tObj
->BaseLevel
;
1339 firstLevel
= tObj
->BaseLevel
+ (GLint
) (tObj
->MinLod
+ 0.5);
1340 firstLevel
= MAX2(firstLevel
, tObj
->BaseLevel
);
1341 lastLevel
= tObj
->BaseLevel
+ (GLint
) (tObj
->MaxLod
+ 0.5);
1342 lastLevel
= MAX2(lastLevel
, tObj
->BaseLevel
);
1343 lastLevel
= MIN2(lastLevel
, tObj
->BaseLevel
+ baseImage
->MaxLog2
);
1344 lastLevel
= MIN2(lastLevel
, tObj
->MaxLevel
);
1345 lastLevel
= MAX2(firstLevel
, lastLevel
); /* need at least one level */
1348 case GL_TEXTURE_RECTANGLE_NV
:
1349 case GL_TEXTURE_4D_SGIS
:
1350 firstLevel
= lastLevel
= 0;
1356 /* save these values */
1357 stObj
->firstLevel
= firstLevel
;
1358 stObj
->lastLevel
= lastLevel
;
1363 copy_image_data_to_texture(struct st_context
*st
,
1364 struct st_texture_object
*stObj
,
1365 struct st_texture_image
*stImage
)
1368 /* Copy potentially with the blitter:
1370 st_texture_image_copy(st
->pipe
,
1371 stObj
->pt
, /* dest texture */
1372 stImage
->face
, stImage
->level
,
1373 stImage
->pt
/* src texture */
1376 st
->pipe
->texture_release(st
->pipe
, &stImage
->pt
);
1379 assert(stImage
->base
.Data
!= NULL
);
1381 /* More straightforward upload.
1383 st_texture_image_data(st
->pipe
,
1388 stImage
->base
.RowStride
,
1389 stImage
->base
.RowStride
*
1390 stImage
->base
.Height
);
1391 _mesa_align_free(stImage
->base
.Data
);
1392 stImage
->base
.Data
= NULL
;
1395 pipe_texture_reference(st
->pipe
, &stImage
->pt
, stObj
->pt
);
1400 * Called during state validation. When this function is finished,
1401 * the texture object should be ready for rendering.
1402 * \return GL_FALSE if a texture border is present, GL_TRUE otherwise
1405 st_finalize_texture(GLcontext
*ctx
,
1406 struct pipe_context
*pipe
,
1407 struct gl_texture_object
*tObj
,
1408 GLboolean
*needFlush
)
1410 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1411 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1415 struct st_texture_image
*firstImage
;
1417 *needFlush
= GL_FALSE
;
1419 /* We know/require this is true by now:
1421 assert(stObj
->base
._Complete
);
1423 /* What levels must the texture include at a minimum?
1425 calculate_first_last_level(stObj
);
1426 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->firstLevel
]);
1430 if (firstImage
->base
.Border
) {
1432 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stObj
->pt
);
1438 /* If both firstImage and stObj point to a texture which can contain
1439 * all active images, favour firstImage. Note that because of the
1440 * completeness requirement, we know that the image dimensions
1443 if (firstImage
->pt
&&
1444 firstImage
->pt
!= stObj
->pt
&&
1445 firstImage
->pt
->first_level
<= stObj
->firstLevel
&&
1446 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1449 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stObj
->pt
);
1451 pipe_texture_reference(ctx
->st
->pipe
, &stObj
->pt
, firstImage
->pt
);
1454 if (firstImage
->base
.IsCompressed
) {
1455 comp_byte
= compressed_num_bytes(firstImage
->base
.TexFormat
->MesaFormat
);
1459 cpp
= firstImage
->base
.TexFormat
->TexelBytes
;
1462 /* Check texture can hold all active levels. Check texture matches
1463 * target, imageFormat, etc.
1465 * XXX: For some layouts (eg i945?), the test might have to be
1466 * first_level == firstLevel, as the texture isn't valid except at the
1467 * original start level. Hope to get around this by
1468 * programming minLod, maxLod, baseLevel into the hardware and
1469 * leaving the texture alone.
1472 (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1473 stObj
->pt
->format
!=
1474 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
->MesaFormat
) ||
1475 stObj
->pt
->first_level
!= stObj
->firstLevel
||
1476 stObj
->pt
->last_level
!= stObj
->lastLevel
||
1477 stObj
->pt
->width
[0] != firstImage
->base
.Width
||
1478 stObj
->pt
->height
[0] != firstImage
->base
.Height
||
1479 stObj
->pt
->depth
[0] != firstImage
->base
.Depth
||
1480 stObj
->pt
->cpp
!= cpp
||
1481 stObj
->pt
->compressed
!= firstImage
->base
.IsCompressed
)) {
1482 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stObj
->pt
);
1486 /* May need to create a new texture:
1489 stObj
->pt
= st_texture_create(ctx
->st
,
1490 gl_target_to_pipe(stObj
->base
.Target
),
1491 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
->MesaFormat
),
1494 firstImage
->base
.Width
,
1495 firstImage
->base
.Height
,
1496 firstImage
->base
.Depth
,
1500 /* Pull in any images not in the object's texture:
1502 for (face
= 0; face
< nr_faces
; face
++) {
1503 for (i
= stObj
->firstLevel
; i
<= stObj
->lastLevel
; i
++) {
1504 struct st_texture_image
*stImage
=
1505 st_texture_image(stObj
->base
.Image
[face
][i
]);
1507 /* Need to import images in main memory or held in other textures.
1509 if (stObj
->pt
!= stImage
->pt
) {
1510 copy_image_data_to_texture(ctx
->st
, stObj
, stImage
);
1511 *needFlush
= GL_TRUE
;
1524 st_init_texture_functions(struct dd_function_table
*functions
)
1526 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1527 functions
->TexImage1D
= st_TexImage1D
;
1528 functions
->TexImage2D
= st_TexImage2D
;
1529 functions
->TexImage3D
= st_TexImage3D
;
1530 functions
->TexSubImage1D
= st_TexSubImage1D
;
1531 functions
->TexSubImage2D
= st_TexSubImage2D
;
1532 functions
->TexSubImage3D
= st_TexSubImage3D
;
1533 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1534 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1535 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1536 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1537 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1538 functions
->GenerateMipmap
= st_generate_mipmap
;
1540 functions
->GetTexImage
= st_GetTexImage
;
1542 /* compressed texture functions */
1543 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1544 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1546 functions
->NewTextureObject
= st_NewTextureObject
;
1547 functions
->NewTextureImage
= st_NewTextureImage
;
1548 functions
->DeleteTexture
= st_DeleteTextureObject
;
1549 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1550 functions
->UpdateTexturePalette
= 0;
1551 functions
->IsTextureResident
= st_IsTextureResident
;
1553 functions
->TextureMemCpy
= do_memcpy
;
1555 /* XXX Temporary until we can query pipe's texture sizes */
1556 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;