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__
);
382 /* There are actually quite a few combinations this will work for,
383 * more than what I've listed here.
386 check_pbo_format(GLint internalFormat
,
387 GLenum format
, GLenum type
,
388 const struct gl_texture_format
*mesa_format
)
390 switch (internalFormat
) {
393 return (format
== GL_BGRA
&&
394 (type
== GL_UNSIGNED_BYTE
||
395 type
== GL_UNSIGNED_INT_8_8_8_8_REV
) &&
396 mesa_format
== &_mesa_texformat_argb8888
);
399 return (format
== GL_RGB
&&
400 type
== GL_UNSIGNED_SHORT_5_6_5
&&
401 mesa_format
== &_mesa_texformat_rgb565
);
403 return (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
);
410 /* XXX: Do this for TexSubImage also:
413 try_pbo_upload(GLcontext
*ctx
,
414 struct st_texture_image
*stImage
,
415 const struct gl_pixelstore_attrib
*unpack
,
416 GLint internalFormat
,
417 GLint width
, GLint height
,
418 GLenum format
, GLenum type
, const void *pixels
)
420 return GL_FALSE
; /* XXX fix flushing/locking/blitting below */
422 struct intel_context
*intel
= intel_context(ctx
);
423 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
424 GLuint src_offset
, src_stride
;
425 GLuint dst_offset
, dst_stride
;
428 ctx
._ImageTransferState
||
429 unpack
->SkipPixels
|| unpack
->SkipRows
) {
430 _mesa_printf("%s: failure 1\n", __FUNCTION__
);
434 src_offset
= (GLuint
) pixels
;
436 if (unpack
->RowLength
> 0)
437 src_stride
= unpack
->RowLength
;
441 dst_offset
= st_texture_image_offset(stImage
->pt
,
445 dst_stride
= stImage
->pt
->pitch
;
448 struct _DriBufferObject
*src_buffer
=
449 intel_bufferobj_buffer(intel
, pbo
, INTEL_READ
);
451 /* Temporary hack: cast to _DriBufferObject:
453 struct _DriBufferObject
*dst_buffer
=
454 (struct _DriBufferObject
*)stImage
->pt
->region
->buffer
;
457 intelEmitCopyBlit(intel
,
459 src_stride
, src_buffer
, src_offset
,
460 dst_stride
, dst_buffer
, dst_offset
,
461 0, 0, 0, 0, width
, height
,
472 st_TexImage(GLcontext
* ctx
,
474 GLenum target
, GLint level
,
475 GLint internalFormat
,
476 GLint width
, GLint height
, GLint depth
,
478 GLenum format
, GLenum type
, const void *pixels
,
479 const struct gl_pixelstore_attrib
*unpack
,
480 struct gl_texture_object
*texObj
,
481 struct gl_texture_image
*texImage
,
482 GLsizei imageSize
, int compressed
)
484 struct st_texture_object
*stObj
= st_texture_object(texObj
);
485 struct st_texture_image
*stImage
= st_texture_image(texImage
);
486 GLint postConvWidth
= width
;
487 GLint postConvHeight
= height
;
488 GLint texelBytes
, sizeInBytes
;
492 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
493 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
495 stImage
->face
= _mesa_tex_target_to_face(target
);
496 stImage
->level
= level
;
498 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
499 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
503 /* choose the texture format */
504 texImage
->TexFormat
= st_ChooseTextureFormat(ctx
, internalFormat
,
507 _mesa_set_fetch_functions(texImage
, dims
);
509 if (texImage
->TexFormat
->TexelBytes
== 0) {
510 /* must be a compressed format */
512 texImage
->IsCompressed
= GL_TRUE
;
513 texImage
->CompressedSize
=
514 ctx
->Driver
.CompressedTextureSize(ctx
, texImage
->Width
,
515 texImage
->Height
, texImage
->Depth
,
516 texImage
->TexFormat
->MesaFormat
);
519 texelBytes
= texImage
->TexFormat
->TexelBytes
;
521 /* Minimum pitch of 32 bytes */
522 if (postConvWidth
* texelBytes
< 32) {
523 postConvWidth
= 32 / texelBytes
;
524 texImage
->RowStride
= postConvWidth
;
527 assert(texImage
->RowStride
== postConvWidth
);
530 /* Release the reference to a potentially orphaned buffer.
531 * Release any old malloced memory.
534 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stImage
->pt
);
535 assert(!texImage
->Data
);
537 else if (texImage
->Data
) {
538 _mesa_align_free(texImage
->Data
);
541 /* If this is the only texture image in the texture, could call
542 * bmBufferData with NULL data to free the old block and avoid
543 * waiting on any outstanding fences.
546 stObj
->pt
->first_level
== level
&&
547 stObj
->pt
->last_level
== level
&&
548 stObj
->pt
->target
!= PIPE_TEXTURE_CUBE
&&
549 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
550 stImage
->face
, stImage
->level
)) {
553 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stObj
->pt
);
558 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
560 DBG("guess_and_alloc_texture: failed\n");
564 assert(!stImage
->pt
);
567 st_texture_match_image(stObj
->pt
, &stImage
->base
,
568 stImage
->face
, stImage
->level
)) {
570 pipe_texture_reference(ctx
->st
->pipe
, &stImage
->pt
, stObj
->pt
);
575 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
577 #if 0 /* XXX FIX when st_buffer_objects are in place */
582 intel_buffer_object(unpack
->BufferObj
) &&
583 check_pbo_format(internalFormat
, format
,
584 type
, texImage
->TexFormat
)) {
586 DBG("trying pbo upload\n");
590 /* Otherwise, attempt to use the blitter for PBO image uploads.
592 if (try_pbo_upload(intel
, stImage
, unpack
,
594 width
, height
, format
, type
, pixels
)) {
595 DBG("pbo upload succeeded\n");
599 DBG("pbo upload failed\n");
602 (void) try_pbo_upload
;
603 (void) check_pbo_format
;
607 /* st_CopyTexImage calls this function with pixels == NULL, with
608 * the expectation that the texture will be set up but nothing
609 * more will be done. This is where those calls return:
612 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
614 "glCompressedTexImage");
616 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
618 pixels
, unpack
, "glTexImage");
624 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0);
625 dstRowStride
= stImage
->surface
->pitch
* stImage
->surface
->cpp
;
628 /* Allocate regular memory and store the image there temporarily. */
629 if (texImage
->IsCompressed
) {
630 sizeInBytes
= texImage
->CompressedSize
;
632 _mesa_compressed_row_stride(texImage
->TexFormat
->MesaFormat
, width
);
636 dstRowStride
= postConvWidth
* texelBytes
;
637 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
640 texImage
->Data
= malloc(sizeInBytes
);
643 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
644 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
646 /* Copy data. Would like to know when it's ok for us to eg. use
647 * the blitter to copy. Or, use the hardware to do the format
648 * conversion and copy:
651 memcpy(texImage
->Data
, pixels
, imageSize
);
654 GLuint srcImageStride
= _mesa_image_image_stride(unpack
, width
, height
,
657 const GLubyte
*src
= (const GLubyte
*) pixels
;
659 for (i
= 0; i
++ < depth
;) {
660 if (!texImage
->TexFormat
->StoreImage(ctx
, dims
,
661 texImage
->_BaseFormat
,
664 0, 0, 0, /* dstX/Y/Zoffset */
666 texImage
->ImageOffsets
,
668 format
, type
, src
, unpack
)) {
669 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
672 if (stImage
->pt
&& i
< depth
) {
673 st_texture_image_unmap(stImage
);
674 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
);
675 src
+= srcImageStride
;
680 _mesa_unmap_teximage_pbo(ctx
, unpack
);
683 st_texture_image_unmap(stImage
);
684 texImage
->Data
= NULL
;
688 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
689 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
696 st_TexImage3D(GLcontext
* ctx
,
697 GLenum target
, GLint level
,
698 GLint internalFormat
,
699 GLint width
, GLint height
, GLint depth
,
701 GLenum format
, GLenum type
, const void *pixels
,
702 const struct gl_pixelstore_attrib
*unpack
,
703 struct gl_texture_object
*texObj
,
704 struct gl_texture_image
*texImage
)
706 st_TexImage(ctx
, 3, target
, level
,
707 internalFormat
, width
, height
, depth
, border
,
708 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
713 st_TexImage2D(GLcontext
* ctx
,
714 GLenum target
, GLint level
,
715 GLint internalFormat
,
716 GLint width
, GLint height
, GLint border
,
717 GLenum format
, GLenum type
, const void *pixels
,
718 const struct gl_pixelstore_attrib
*unpack
,
719 struct gl_texture_object
*texObj
,
720 struct gl_texture_image
*texImage
)
722 st_TexImage(ctx
, 2, target
, level
,
723 internalFormat
, width
, height
, 1, border
,
724 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
729 st_TexImage1D(GLcontext
* ctx
,
730 GLenum target
, GLint level
,
731 GLint internalFormat
,
732 GLint width
, GLint border
,
733 GLenum format
, GLenum type
, const void *pixels
,
734 const struct gl_pixelstore_attrib
*unpack
,
735 struct gl_texture_object
*texObj
,
736 struct gl_texture_image
*texImage
)
738 st_TexImage(ctx
, 1, target
, level
,
739 internalFormat
, width
, 1, 1, border
,
740 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
745 st_CompressedTexImage2D( GLcontext
*ctx
, GLenum target
, GLint level
,
746 GLint internalFormat
,
747 GLint width
, GLint height
, GLint border
,
748 GLsizei imageSize
, const GLvoid
*data
,
749 struct gl_texture_object
*texObj
,
750 struct gl_texture_image
*texImage
)
752 st_TexImage(ctx
, 2, target
, level
,
753 internalFormat
, width
, height
, 1, border
,
754 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, 1);
759 * Need to map texture image into memory before copying image data,
763 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
764 GLenum format
, GLenum type
, GLvoid
* pixels
,
765 struct gl_texture_object
*texObj
,
766 struct gl_texture_image
*texImage
, int compressed
)
768 struct st_texture_image
*stImage
= st_texture_image(texImage
);
769 GLuint dstImageStride
= _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
,
770 texImage
->Height
, format
,
778 /* Image is stored in hardware format in a buffer managed by the
779 * kernel. Need to explicitly map and unmap it.
781 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0);
782 texImage
->RowStride
= stImage
->surface
->pitch
;
785 /* Otherwise, the image should actually be stored in
786 * texImage->Data. This is pretty confusing for
787 * everybody, I'd much prefer to separate the two functions of
788 * texImage->Data - storage for texture images in main memory
789 * and access (ie mappings) of images. In other words, we'd
790 * create a new texImage->Map field and leave Data simply for
793 assert(texImage
->Data
);
796 depth
= texImage
->Depth
;
799 dest
= (GLubyte
*) pixels
;
801 for (i
= 0; i
++ < depth
;) {
803 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
806 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
810 if (stImage
->pt
&& i
< depth
) {
811 st_texture_image_unmap(stImage
);
812 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
);
813 dest
+= dstImageStride
;
817 texImage
->Depth
= depth
;
821 st_texture_image_unmap(stImage
);
822 texImage
->Data
= NULL
;
828 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
829 GLenum format
, GLenum type
, GLvoid
* pixels
,
830 struct gl_texture_object
*texObj
,
831 struct gl_texture_image
*texImage
)
833 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
,
834 texObj
, texImage
, 0);
839 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
841 const struct gl_texture_object
*texObj
,
842 const struct gl_texture_image
*texImage
)
844 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
,
845 (struct gl_texture_object
*) texObj
,
846 (struct gl_texture_image
*) texImage
, 1);
852 st_TexSubimage(GLcontext
* ctx
,
854 GLenum target
, GLint level
,
855 GLint xoffset
, GLint yoffset
, GLint zoffset
,
856 GLint width
, GLint height
, GLint depth
,
857 GLenum format
, GLenum type
, const void *pixels
,
858 const struct gl_pixelstore_attrib
*packing
,
859 struct gl_texture_object
*texObj
,
860 struct gl_texture_image
*texImage
)
862 struct st_texture_image
*stImage
= st_texture_image(texImage
);
864 GLuint srcImageStride
= _mesa_image_image_stride(packing
, width
, height
,
869 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
870 _mesa_lookup_enum_by_nr(target
),
871 level
, xoffset
, yoffset
, width
, height
);
874 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
875 type
, pixels
, packing
, "glTexSubImage2D");
879 /* Map buffer if necessary. Need to lock to prevent other contexts
880 * from uploading the buffer under us.
883 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
);
884 dstRowStride
= stImage
->surface
->pitch
* stImage
->surface
->cpp
;
887 src
= (const GLubyte
*) pixels
;
889 for (i
= 0; i
++ < depth
;) {
890 if (!texImage
->TexFormat
->StoreImage(ctx
, dims
, texImage
->_BaseFormat
,
895 texImage
->ImageOffsets
,
897 format
, type
, src
, packing
)) {
898 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "st_TexSubImage");
901 if (stImage
->pt
&& i
< depth
) {
902 st_texture_image_unmap(stImage
);
903 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
+ i
);
904 src
+= srcImageStride
;
909 /* GL_SGIS_generate_mipmap */
910 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
911 _mesa_generate_mipmap(ctx
, target
,
912 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
917 _mesa_unmap_teximage_pbo(ctx
, packing
);
920 st_texture_image_unmap(stImage
);
921 texImage
->Data
= NULL
;
928 st_TexSubImage3D(GLcontext
* ctx
,
931 GLint xoffset
, GLint yoffset
, GLint zoffset
,
932 GLsizei width
, GLsizei height
, GLsizei depth
,
933 GLenum format
, GLenum type
,
934 const GLvoid
* pixels
,
935 const struct gl_pixelstore_attrib
*packing
,
936 struct gl_texture_object
*texObj
,
937 struct gl_texture_image
*texImage
)
939 st_TexSubimage(ctx
, 3, target
, level
,
940 xoffset
, yoffset
, zoffset
,
941 width
, height
, depth
,
942 format
, type
, pixels
, packing
, texObj
, texImage
);
948 st_TexSubImage2D(GLcontext
* ctx
,
951 GLint xoffset
, GLint yoffset
,
952 GLsizei width
, GLsizei height
,
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
, 2, target
, level
,
962 format
, type
, pixels
, packing
, texObj
, texImage
);
967 st_TexSubImage1D(GLcontext
* ctx
,
972 GLenum format
, GLenum type
,
973 const GLvoid
* pixels
,
974 const struct gl_pixelstore_attrib
*packing
,
975 struct gl_texture_object
*texObj
,
976 struct gl_texture_image
*texImage
)
978 st_TexSubimage(ctx
, 1, target
, level
,
981 format
, type
, pixels
, packing
, texObj
, texImage
);
987 * Return 0 for GL_TEXTURE_CUBE_MAP_POSITIVE_X,
988 * 1 for GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
990 * XXX duplicated from main/teximage.c
993 texture_face(GLenum target
)
995 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
996 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
)
997 return (GLuint
) target
- (GLuint
) GL_TEXTURE_CUBE_MAP_POSITIVE_X
;
1005 * Do a CopyTexSubImage operation by mapping the source surface and
1006 * dest surface and using get_tile()/put_tile() to access the pixels/texels.
1008 * Note: srcY=0=TOP of renderbuffer
1011 fallback_copy_texsubimage(GLcontext
*ctx
,
1014 struct st_renderbuffer
*strb
,
1015 struct st_texture_image
*stImage
,
1017 GLint destX
, GLint destY
, GLint destZ
,
1018 GLint srcX
, GLint srcY
,
1019 GLsizei width
, GLsizei height
)
1021 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1022 const uint face
= texture_face(target
);
1023 struct pipe_texture
*pt
= stImage
->pt
;
1024 struct pipe_surface
*src_surf
, *dest_surf
;
1028 /* determine bottom-to-top vs. top-to-bottom order */
1029 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1030 destY
= height
- 1 - destY
;
1037 src_surf
= strb
->surface
;
1039 dest_surf
= pipe
->get_tex_surface(pipe
, pt
,
1040 face
, level
, destZ
);
1042 /* buffer for one row */
1043 data
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
1045 /* do copy row by row */
1046 for (row
= 0; row
< height
; row
++) {
1047 pipe_get_tile_rgba(pipe
, src_surf
, srcX
, srcY
+ row
, width
, 1, data
);
1049 /* XXX we're ignoring convolution for now */
1050 if (ctx
->_ImageTransferState
) {
1051 _mesa_apply_rgba_transfer_ops(ctx
,
1052 ctx
->_ImageTransferState
& ~IMAGE_CONVOLUTION_BIT
,
1053 width
, (GLfloat (*)[4])data
);
1056 pipe_put_tile_rgba(pipe
, dest_surf
, destX
, destY
, width
, 1, data
);
1067 * Do a CopyTex[Sub]Image using an optimized hardware (blit) path.
1068 * Note that the region to copy has already been clip tested.
1070 * Note: srcY=0=Bottom of renderbuffer
1072 * \return GL_TRUE if success, GL_FALSE if failure (use a fallback)
1075 do_copy_texsubimage(GLcontext
*ctx
,
1076 GLenum target
, GLint level
,
1077 GLint destX
, GLint destY
, GLint destZ
,
1078 GLint srcX
, GLint srcY
,
1079 GLsizei width
, GLsizei height
)
1081 struct gl_texture_unit
*texUnit
=
1082 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1083 struct gl_texture_object
*texObj
=
1084 _mesa_select_tex_object(ctx
, texUnit
, target
);
1085 struct gl_texture_image
*texImage
=
1086 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1087 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1088 GLenum baseFormat
= texImage
->InternalFormat
;
1089 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1090 struct st_renderbuffer
*strb
;
1091 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1092 struct pipe_surface
*dest_surface
;
1093 uint dest_format
, src_format
;
1097 /* determine if copying depth or color data */
1098 if (baseFormat
== GL_DEPTH_COMPONENT
) {
1099 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1101 else if (baseFormat
== GL_DEPTH_STENCIL_EXT
) {
1102 strb
= st_renderbuffer(fb
->_StencilBuffer
);
1105 /* baseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1106 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1110 assert(strb
->surface
);
1111 assert(stImage
->pt
);
1113 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1114 srcY
= strb
->Base
.Height
- srcY
- height
;
1117 src_format
= strb
->surface
->format
;
1118 dest_format
= stImage
->pt
->format
;
1120 dest_surface
= pipe
->get_tex_surface(pipe
, stImage
->pt
, stImage
->face
,
1121 stImage
->level
, destZ
);
1123 if (src_format
== dest_format
&&
1124 ctx
->_ImageTransferState
== 0x0 &&
1125 strb
->surface
->buffer
&&
1126 dest_surface
->buffer
&&
1127 strb
->surface
->cpp
== stImage
->pt
->cpp
) {
1128 /* do blit-style copy */
1130 /* XXX may need to invert image depending on window
1131 * vs. user-created FBO
1135 /* A bit of fiddling to get the blitter to work with -ve
1136 * pitches. But we get a nice inverted blit this way, so it's
1139 intelEmitCopyBlit(intel
,
1143 src
->height
* src
->pitch
* src
->cpp
,
1145 stImage
->pt
->region
->buffer
,
1147 x
, y
+ height
, dstx
, dsty
, width
, height
,
1151 pipe
->surface_copy(pipe
,
1163 fallback_copy_texsubimage(ctx
, target
, level
,
1164 strb
, stImage
, baseFormat
,
1165 destX
, destY
, destZ
,
1166 srcX
, srcY
, width
, height
);
1169 pipe_surface_reference(&dest_surface
, NULL
);
1172 /* GL_SGIS_generate_mipmap -- this can be accelerated now.
1173 * XXX Add a ctx->Driver.GenerateMipmaps() function?
1175 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1176 intel_generate_mipmap(ctx
, target
,
1177 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
],
1187 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1188 GLenum internalFormat
,
1189 GLint x
, GLint y
, GLsizei width
, GLint border
)
1191 struct gl_texture_unit
*texUnit
=
1192 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1193 struct gl_texture_object
*texObj
=
1194 _mesa_select_tex_object(ctx
, texUnit
, target
);
1195 struct gl_texture_image
*texImage
=
1196 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1203 /* Setup or redefine the texture object, texture and texture
1204 * image. Don't populate yet.
1206 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1208 GL_RGBA
, CHAN_TYPE
, NULL
,
1209 &ctx
->DefaultPacking
, texObj
, texImage
);
1211 do_copy_texsubimage(ctx
, target
, level
,
1218 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1219 GLenum internalFormat
,
1220 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1223 struct gl_texture_unit
*texUnit
=
1224 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1225 struct gl_texture_object
*texObj
=
1226 _mesa_select_tex_object(ctx
, texUnit
, target
);
1227 struct gl_texture_image
*texImage
=
1228 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1235 /* Setup or redefine the texture object, texture and texture
1236 * image. Don't populate yet.
1238 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1239 width
, height
, border
,
1240 GL_RGBA
, CHAN_TYPE
, NULL
,
1241 &ctx
->DefaultPacking
, texObj
, texImage
);
1244 do_copy_texsubimage(ctx
, target
, level
,
1246 x
, y
, width
, height
);
1251 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1252 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1254 const GLint yoffset
= 0, zoffset
= 0;
1255 const GLsizei height
= 1;
1256 do_copy_texsubimage(ctx
, target
, level
,
1257 xoffset
, yoffset
, zoffset
,
1258 x
, y
, width
, height
);
1263 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1264 GLint xoffset
, GLint yoffset
,
1265 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1267 const GLint zoffset
= 0;
1268 do_copy_texsubimage(ctx
, target
, level
,
1269 xoffset
, yoffset
, zoffset
,
1270 x
, y
, width
, height
);
1275 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1276 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1277 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1279 do_copy_texsubimage(ctx
, target
, level
,
1280 xoffset
, yoffset
, zoffset
,
1281 x
, y
, width
, height
);
1288 * Compute which mipmap levels that really need to be sent to the hardware.
1289 * This depends on the base image size, GL_TEXTURE_MIN_LOD,
1290 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
1293 calculate_first_last_level(struct st_texture_object
*stObj
)
1295 struct gl_texture_object
*tObj
= &stObj
->base
;
1296 const struct gl_texture_image
*const baseImage
=
1297 tObj
->Image
[0][tObj
->BaseLevel
];
1299 /* These must be signed values. MinLod and MaxLod can be negative numbers,
1300 * and having firstLevel and lastLevel as signed prevents the need for
1301 * extra sign checks.
1306 /* Yes, this looks overly complicated, but it's all needed.
1308 switch (tObj
->Target
) {
1312 case GL_TEXTURE_CUBE_MAP
:
1313 if (tObj
->MinFilter
== GL_NEAREST
|| tObj
->MinFilter
== GL_LINEAR
) {
1314 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
1316 firstLevel
= lastLevel
= tObj
->BaseLevel
;
1319 firstLevel
= tObj
->BaseLevel
+ (GLint
) (tObj
->MinLod
+ 0.5);
1320 firstLevel
= MAX2(firstLevel
, tObj
->BaseLevel
);
1321 lastLevel
= tObj
->BaseLevel
+ (GLint
) (tObj
->MaxLod
+ 0.5);
1322 lastLevel
= MAX2(lastLevel
, tObj
->BaseLevel
);
1323 lastLevel
= MIN2(lastLevel
, tObj
->BaseLevel
+ baseImage
->MaxLog2
);
1324 lastLevel
= MIN2(lastLevel
, tObj
->MaxLevel
);
1325 lastLevel
= MAX2(firstLevel
, lastLevel
); /* need at least one level */
1328 case GL_TEXTURE_RECTANGLE_NV
:
1329 case GL_TEXTURE_4D_SGIS
:
1330 firstLevel
= lastLevel
= 0;
1336 /* save these values */
1337 stObj
->firstLevel
= firstLevel
;
1338 stObj
->lastLevel
= lastLevel
;
1343 copy_image_data_to_texture(struct st_context
*st
,
1344 struct st_texture_object
*stObj
,
1345 struct st_texture_image
*stImage
)
1348 /* Copy potentially with the blitter:
1350 st_texture_image_copy(st
->pipe
,
1351 stObj
->pt
, /* dest texture */
1352 stImage
->face
, stImage
->level
,
1353 stImage
->pt
/* src texture */
1356 st
->pipe
->texture_release(st
->pipe
, &stImage
->pt
);
1359 assert(stImage
->base
.Data
!= NULL
);
1361 /* More straightforward upload.
1363 st_texture_image_data(st
->pipe
,
1368 stImage
->base
.RowStride
,
1369 stImage
->base
.RowStride
*
1370 stImage
->base
.Height
);
1371 _mesa_align_free(stImage
->base
.Data
);
1372 stImage
->base
.Data
= NULL
;
1375 pipe_texture_reference(st
->pipe
, &stImage
->pt
, stObj
->pt
);
1380 * Called during state validation. When this function is finished,
1381 * the texture object should be ready for rendering.
1382 * \return GL_FALSE if a texture border is present, GL_TRUE otherwise
1385 st_finalize_texture(GLcontext
*ctx
,
1386 struct pipe_context
*pipe
,
1387 struct gl_texture_object
*tObj
,
1388 GLboolean
*needFlush
)
1390 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1391 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1395 struct st_texture_image
*firstImage
;
1397 *needFlush
= GL_FALSE
;
1399 /* We know/require this is true by now:
1401 assert(stObj
->base
._Complete
);
1403 /* What levels must the texture include at a minimum?
1405 calculate_first_last_level(stObj
);
1406 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->firstLevel
]);
1410 if (firstImage
->base
.Border
) {
1412 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stObj
->pt
);
1418 /* If both firstImage and stObj point to a texture which can contain
1419 * all active images, favour firstImage. Note that because of the
1420 * completeness requirement, we know that the image dimensions
1423 if (firstImage
->pt
&&
1424 firstImage
->pt
!= stObj
->pt
&&
1425 firstImage
->pt
->first_level
<= stObj
->firstLevel
&&
1426 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1429 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stObj
->pt
);
1431 pipe_texture_reference(ctx
->st
->pipe
, &stObj
->pt
, firstImage
->pt
);
1434 if (firstImage
->base
.IsCompressed
) {
1435 comp_byte
= compressed_num_bytes(firstImage
->base
.TexFormat
->MesaFormat
);
1439 cpp
= firstImage
->base
.TexFormat
->TexelBytes
;
1442 /* Check texture can hold all active levels. Check texture matches
1443 * target, imageFormat, etc.
1445 * XXX: For some layouts (eg i945?), the test might have to be
1446 * first_level == firstLevel, as the texture isn't valid except at the
1447 * original start level. Hope to get around this by
1448 * programming minLod, maxLod, baseLevel into the hardware and
1449 * leaving the texture alone.
1452 (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1453 stObj
->pt
->format
!=
1454 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
->MesaFormat
) ||
1455 stObj
->pt
->first_level
!= stObj
->firstLevel
||
1456 stObj
->pt
->last_level
!= stObj
->lastLevel
||
1457 stObj
->pt
->width
[0] != firstImage
->base
.Width
||
1458 stObj
->pt
->height
[0] != firstImage
->base
.Height
||
1459 stObj
->pt
->depth
[0] != firstImage
->base
.Depth
||
1460 stObj
->pt
->cpp
!= cpp
||
1461 stObj
->pt
->compressed
!= firstImage
->base
.IsCompressed
)) {
1462 ctx
->st
->pipe
->texture_release(ctx
->st
->pipe
, &stObj
->pt
);
1466 /* May need to create a new texture:
1469 stObj
->pt
= st_texture_create(ctx
->st
,
1470 gl_target_to_pipe(stObj
->base
.Target
),
1471 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
->MesaFormat
),
1474 firstImage
->base
.Width
,
1475 firstImage
->base
.Height
,
1476 firstImage
->base
.Depth
,
1480 /* Pull in any images not in the object's texture:
1482 for (face
= 0; face
< nr_faces
; face
++) {
1483 for (i
= stObj
->firstLevel
; i
<= stObj
->lastLevel
; i
++) {
1484 struct st_texture_image
*stImage
=
1485 st_texture_image(stObj
->base
.Image
[face
][i
]);
1487 /* Need to import images in main memory or held in other textures.
1489 if (stObj
->pt
!= stImage
->pt
) {
1490 copy_image_data_to_texture(ctx
->st
, stObj
, stImage
);
1491 *needFlush
= GL_TRUE
;
1504 st_init_texture_functions(struct dd_function_table
*functions
)
1506 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1507 functions
->TexImage1D
= st_TexImage1D
;
1508 functions
->TexImage2D
= st_TexImage2D
;
1509 functions
->TexImage3D
= st_TexImage3D
;
1510 functions
->TexSubImage1D
= st_TexSubImage1D
;
1511 functions
->TexSubImage2D
= st_TexSubImage2D
;
1512 functions
->TexSubImage3D
= st_TexSubImage3D
;
1513 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1514 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1515 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1516 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1517 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1518 functions
->GenerateMipmap
= st_generate_mipmap
;
1520 functions
->GetTexImage
= st_GetTexImage
;
1522 /* compressed texture functions */
1523 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1524 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1526 functions
->NewTextureObject
= st_NewTextureObject
;
1527 functions
->NewTextureImage
= st_NewTextureImage
;
1528 functions
->DeleteTexture
= st_DeleteTextureObject
;
1529 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1530 functions
->UpdateTexturePalette
= 0;
1531 functions
->IsTextureResident
= st_IsTextureResident
;
1533 functions
->TextureMemCpy
= do_memcpy
;
1535 /* XXX Temporary until we can query pipe's texture sizes */
1536 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;