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/pixel.h"
35 #include "main/texcompress.h"
36 #include "main/texformat.h"
37 #include "main/teximage.h"
38 #include "main/texobj.h"
39 #include "main/texstore.h"
41 #include "state_tracker/st_context.h"
42 #include "state_tracker/st_cb_fbo.h"
43 #include "state_tracker/st_cb_texture.h"
44 #include "state_tracker/st_format.h"
45 #include "state_tracker/st_public.h"
46 #include "state_tracker/st_texture.h"
47 #include "state_tracker/st_gen_mipmap.h"
49 #include "pipe/p_context.h"
50 #include "pipe/p_defines.h"
51 #include "pipe/p_inlines.h"
52 #include "util/p_tile.h"
53 #include "util/u_blit.h"
56 #define DBG if (0) printf
59 static INLINE
struct st_texture_image
*
60 st_texture_image(struct gl_texture_image
*img
)
62 return (struct st_texture_image
*) img
;
66 static enum pipe_texture_target
67 gl_target_to_pipe(GLenum target
)
71 return PIPE_TEXTURE_1D
;
74 case GL_TEXTURE_RECTANGLE_NV
:
75 return PIPE_TEXTURE_2D
;
78 return PIPE_TEXTURE_3D
;
80 case GL_TEXTURE_CUBE_MAP_ARB
:
81 return PIPE_TEXTURE_CUBE
;
91 * Return nominal bytes per texel for a compressed format, 0 for non-compressed
95 compressed_num_bytes(GLuint mesaFormat
)
98 case MESA_FORMAT_RGB_FXT1
:
99 case MESA_FORMAT_RGBA_FXT1
:
100 case MESA_FORMAT_RGB_DXT1
:
101 case MESA_FORMAT_RGBA_DXT1
:
103 case MESA_FORMAT_RGBA_DXT3
:
104 case MESA_FORMAT_RGBA_DXT5
:
113 st_IsTextureResident(GLcontext
* ctx
, struct gl_texture_object
*texObj
)
116 struct intel_context
*intel
= intel_context(ctx
);
117 struct st_texture_object
*stObj
= st_texture_object(texObj
);
122 intel_is_region_resident(intel
, stObj
->pt
->region
);
128 static struct gl_texture_image
*
129 st_NewTextureImage(GLcontext
* ctx
)
131 DBG("%s\n", __FUNCTION__
);
133 return (struct gl_texture_image
*) CALLOC_STRUCT(st_texture_image
);
137 static struct gl_texture_object
*
138 st_NewTextureObject(GLcontext
* ctx
, GLuint name
, GLenum target
)
140 struct st_texture_object
*obj
= CALLOC_STRUCT(st_texture_object
);
142 DBG("%s\n", __FUNCTION__
);
143 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
149 st_DeleteTextureObject(GLcontext
*ctx
,
150 struct gl_texture_object
*texObj
)
152 struct st_texture_object
*stObj
= st_texture_object(texObj
);
154 pipe_texture_release(&stObj
->pt
);
156 _mesa_delete_texture_object(ctx
, texObj
);
161 st_FreeTextureImageData(GLcontext
* ctx
, struct gl_texture_image
*texImage
)
163 struct st_texture_image
*stImage
= st_texture_image(texImage
);
165 DBG("%s\n", __FUNCTION__
);
168 pipe_texture_release(&stImage
->pt
);
171 if (texImage
->Data
) {
172 free(texImage
->Data
);
173 texImage
->Data
= NULL
;
178 /* ================================================================
179 * From linux kernel i386 header files, copes with odd sizes better
180 * than COPY_DWORDS would:
181 * XXX Put this in src/mesa/main/imports.h ???
183 #if defined(i386) || defined(__i386__)
185 __memcpy(void *to
, const void *from
, size_t n
)
188 __asm__
__volatile__("rep ; movsl\n\t"
192 "1:\ttestb $1,%b4\n\t"
194 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
195 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
200 #define __memcpy(a,b,c) memcpy(a,b,c)
204 /* The system memcpy (at least on ubuntu 5.10) has problems copying
205 * to agp (writecombined) memory from a source which isn't 64-byte
206 * aligned - there is a 4x performance falloff.
208 * The x86 __memcpy is immune to this but is slightly slower
209 * (10%-ish) than the system memcpy.
211 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
212 * isn't much faster than x86_memcpy for agp copies.
214 * TODO: switch dynamically.
217 do_memcpy(void *dest
, const void *src
, size_t n
)
219 if ((((unsigned) src
) & 63) || (((unsigned) dest
) & 63)) {
220 return __memcpy(dest
, src
, n
);
223 return memcpy(dest
, src
, n
);
227 /* Functions to store texture images. Where possible, textures
228 * will be created or further instantiated with image data, otherwise
229 * images will be stored in malloc'd memory. A validation step is
230 * required to pull those images into a texture, or otherwise
231 * decide a fallback is required.
251 * Allocate a pipe_texture object for the given st_texture_object using
252 * the given st_texture_image to guess the mipmap size/levels.
255 * Otherwise, store it in memory if (Border != 0) or (any dimension ==
258 * Otherwise, if max_level >= level >= min_level, create texture with
259 * space for images from min_level down to max_level.
261 * Otherwise, create texture with space for images from (level 0)..(1x1).
262 * Consider pruning this texture at a validation if the saving is worth it.
265 guess_and_alloc_texture(struct st_context
*st
,
266 struct st_texture_object
*stObj
,
267 const struct st_texture_image
*stImage
)
271 GLuint width
= stImage
->base
.Width2
; /* size w/out border */
272 GLuint height
= stImage
->base
.Height2
;
273 GLuint depth
= stImage
->base
.Depth2
;
274 GLuint i
, comp_byte
= 0;
276 DBG("%s\n", __FUNCTION__
);
281 stImage
->level
> stObj
->base
.BaseLevel
&&
282 (stImage
->base
.Width
== 1 ||
283 (stObj
->base
.Target
!= GL_TEXTURE_1D
&&
284 stImage
->base
.Height
== 1) ||
285 (stObj
->base
.Target
== GL_TEXTURE_3D
&&
286 stImage
->base
.Depth
== 1)))
289 /* If this image disrespects BaseLevel, allocate from level zero.
290 * Usually BaseLevel == 0, so it's unlikely to happen.
292 if (stImage
->level
< stObj
->base
.BaseLevel
)
295 firstLevel
= stObj
->base
.BaseLevel
;
298 /* Figure out image dimensions at start level.
300 for (i
= stImage
->level
; i
> firstLevel
; i
--) {
309 /* Guess a reasonable value for lastLevel. This is probably going
310 * to be wrong fairly often and might mean that we have to look at
311 * resizable buffers, or require that buffers implement lazy
312 * pagetable arrangements.
314 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
315 stObj
->base
.MinFilter
== GL_LINEAR
) &&
316 stImage
->level
== firstLevel
) {
317 lastLevel
= firstLevel
;
320 GLuint l2width
= logbase2(width
);
321 GLuint l2height
= logbase2(height
);
322 GLuint l2depth
= logbase2(depth
);
323 lastLevel
= firstLevel
+ MAX2(MAX2(l2width
, l2height
), l2depth
);
326 if (stImage
->base
.IsCompressed
)
327 comp_byte
= compressed_num_bytes(stImage
->base
.TexFormat
->MesaFormat
);
329 stObj
->pt
= st_texture_create(st
,
330 gl_target_to_pipe(stObj
->base
.Target
),
331 st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
->MesaFormat
),
337 ( PIPE_TEXTURE_USAGE_RENDER_TARGET
|
338 PIPE_TEXTURE_USAGE_SAMPLER
));
340 DBG("%s - success\n", __FUNCTION__
);
344 /* There are actually quite a few combinations this will work for,
345 * more than what I've listed here.
348 check_pbo_format(GLint internalFormat
,
349 GLenum format
, GLenum type
,
350 const struct gl_texture_format
*mesa_format
)
352 switch (internalFormat
) {
355 return (format
== GL_BGRA
&&
356 (type
== GL_UNSIGNED_BYTE
||
357 type
== GL_UNSIGNED_INT_8_8_8_8_REV
) &&
358 mesa_format
== &_mesa_texformat_argb8888
);
361 return (format
== GL_RGB
&&
362 type
== GL_UNSIGNED_SHORT_5_6_5
&&
363 mesa_format
== &_mesa_texformat_rgb565
);
365 return (type
== GL_UNSIGNED_SHORT_8_8_MESA
|| type
== GL_UNSIGNED_BYTE
);
372 /* XXX: Do this for TexSubImage also:
375 try_pbo_upload(GLcontext
*ctx
,
376 struct st_texture_image
*stImage
,
377 const struct gl_pixelstore_attrib
*unpack
,
378 GLint internalFormat
,
379 GLint width
, GLint height
,
380 GLenum format
, GLenum type
, const void *pixels
)
382 return GL_FALSE
; /* XXX fix flushing/locking/blitting below */
384 struct intel_context
*intel
= intel_context(ctx
);
385 struct intel_buffer_object
*pbo
= intel_buffer_object(unpack
->BufferObj
);
386 GLuint src_offset
, src_stride
;
387 GLuint dst_offset
, dst_stride
;
390 ctx
._ImageTransferState
||
391 unpack
->SkipPixels
|| unpack
->SkipRows
) {
392 _mesa_printf("%s: failure 1\n", __FUNCTION__
);
396 src_offset
= (GLuint
) pixels
;
398 if (unpack
->RowLength
> 0)
399 src_stride
= unpack
->RowLength
;
403 dst_offset
= st_texture_image_offset(stImage
->pt
,
407 dst_stride
= stImage
->pt
->pitch
;
410 struct _DriBufferObject
*src_buffer
=
411 intel_bufferobj_buffer(intel
, pbo
, INTEL_READ
);
413 /* Temporary hack: cast to _DriBufferObject:
415 struct _DriBufferObject
*dst_buffer
=
416 (struct _DriBufferObject
*)stImage
->pt
->region
->buffer
;
419 intelEmitCopyBlit(intel
,
421 src_stride
, src_buffer
, src_offset
,
422 dst_stride
, dst_buffer
, dst_offset
,
423 0, 0, 0, 0, width
, height
,
433 * Adjust pixel unpack params and image dimensions to strip off the
435 * Gallium doesn't support texture borders. They've seldem been used
436 * and seldom been implemented correctly anyway.
437 * \param unpackNew returns the new pixel unpack parameters
440 strip_texture_border(GLint border
,
441 GLint
*width
, GLint
*height
, GLint
*depth
,
442 const struct gl_pixelstore_attrib
*unpack
,
443 struct gl_pixelstore_attrib
*unpackNew
)
445 assert(border
> 0); /* sanity check */
447 *unpackNew
= *unpack
;
449 if (unpackNew
->RowLength
== 0)
450 unpackNew
->RowLength
= *width
;
452 if (depth
&& unpackNew
->ImageHeight
== 0)
453 unpackNew
->ImageHeight
= *height
;
455 unpackNew
->SkipPixels
+= border
;
457 unpackNew
->SkipRows
+= border
;
459 unpackNew
->SkipImages
+= border
;
462 *width
= *width
- 2 * border
;
463 if (height
&& *height
>= 3)
464 *height
= *height
- 2 * border
;
465 if (depth
&& *depth
>= 3)
466 *depth
= *depth
- 2 * border
;
471 st_TexImage(GLcontext
* ctx
,
473 GLenum target
, GLint level
,
474 GLint internalFormat
,
475 GLint width
, GLint height
, GLint depth
,
477 GLenum format
, GLenum type
, const void *pixels
,
478 const struct gl_pixelstore_attrib
*unpack
,
479 struct gl_texture_object
*texObj
,
480 struct gl_texture_image
*texImage
,
481 GLsizei imageSize
, int compressed
)
483 struct st_texture_object
*stObj
= st_texture_object(texObj
);
484 struct st_texture_image
*stImage
= st_texture_image(texImage
);
485 GLint postConvWidth
, postConvHeight
;
486 GLint texelBytes
, sizeInBytes
;
488 struct gl_pixelstore_attrib unpackNB
;
490 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
491 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
493 /* gallium does not support texture borders, strip it off */
495 strip_texture_border(border
, &width
, &height
, &depth
,
498 texImage
->Width
= width
;
499 texImage
->Height
= height
;
500 texImage
->Depth
= depth
;
501 texImage
->Border
= 0;
505 postConvWidth
= width
;
506 postConvHeight
= height
;
508 stImage
->face
= _mesa_tex_target_to_face(target
);
509 stImage
->level
= level
;
511 if (ctx
->_ImageTransferState
& IMAGE_CONVOLUTION_BIT
) {
512 _mesa_adjust_image_for_convolution(ctx
, dims
, &postConvWidth
,
516 /* choose the texture format */
517 texImage
->TexFormat
= st_ChooseTextureFormat(ctx
, internalFormat
,
520 _mesa_set_fetch_functions(texImage
, dims
);
522 if (texImage
->TexFormat
->TexelBytes
== 0) {
523 /* must be a compressed format */
525 texImage
->IsCompressed
= GL_TRUE
;
526 texImage
->CompressedSize
=
527 ctx
->Driver
.CompressedTextureSize(ctx
, texImage
->Width
,
528 texImage
->Height
, texImage
->Depth
,
529 texImage
->TexFormat
->MesaFormat
);
532 texelBytes
= texImage
->TexFormat
->TexelBytes
;
534 /* Minimum pitch of 32 bytes */
535 if (postConvWidth
* texelBytes
< 32) {
536 postConvWidth
= 32 / texelBytes
;
537 texImage
->RowStride
= postConvWidth
;
540 /* we'll set RowStride elsewhere when the texture is a "mapped" state */
541 /*assert(texImage->RowStride == postConvWidth);*/
544 /* Release the reference to a potentially orphaned buffer.
545 * Release any old malloced memory.
548 pipe_texture_release(&stImage
->pt
);
549 assert(!texImage
->Data
);
551 else if (texImage
->Data
) {
552 _mesa_align_free(texImage
->Data
);
555 /* If this is the only mipmap level in the texture, could call
556 * bmBufferData with NULL data to free the old block and avoid
557 * waiting on any outstanding fences.
560 /*stObj->pt->first_level == level &&*/
561 stObj
->pt
->last_level
== level
&&
562 stObj
->pt
->target
!= PIPE_TEXTURE_CUBE
&&
563 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
564 stImage
->face
, stImage
->level
)) {
567 pipe_texture_release(&stObj
->pt
);
572 guess_and_alloc_texture(ctx
->st
, stObj
, stImage
);
574 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
579 assert(!stImage
->pt
);
582 st_texture_match_image(stObj
->pt
, &stImage
->base
,
583 stImage
->face
, stImage
->level
)) {
585 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
590 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
592 #if 0 /* XXX FIX when st_buffer_objects are in place */
597 intel_buffer_object(unpack
->BufferObj
) &&
598 check_pbo_format(internalFormat
, format
,
599 type
, texImage
->TexFormat
)) {
601 DBG("trying pbo upload\n");
605 /* Otherwise, attempt to use the blitter for PBO image uploads.
607 if (try_pbo_upload(intel
, stImage
, unpack
,
609 width
, height
, format
, type
, pixels
)) {
610 DBG("pbo upload succeeded\n");
614 DBG("pbo upload failed\n");
617 (void) try_pbo_upload
;
618 (void) check_pbo_format
;
622 /* st_CopyTexImage calls this function with pixels == NULL, with
623 * the expectation that the texture will be set up but nothing
624 * more will be done. This is where those calls return:
627 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
629 "glCompressedTexImage");
631 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
633 pixels
, unpack
, "glTexImage");
639 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
640 PIPE_BUFFER_USAGE_CPU_WRITE
);
641 dstRowStride
= stImage
->surface
->pitch
* stImage
->surface
->cpp
;
644 /* Allocate regular memory and store the image there temporarily. */
645 if (texImage
->IsCompressed
) {
646 sizeInBytes
= texImage
->CompressedSize
;
648 _mesa_compressed_row_stride(texImage
->TexFormat
->MesaFormat
, width
);
652 dstRowStride
= postConvWidth
* texelBytes
;
653 sizeInBytes
= depth
* dstRowStride
* postConvHeight
;
656 texImage
->Data
= malloc(sizeInBytes
);
659 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
660 width
, height
, depth
, width
* texelBytes
, dstRowStride
);
662 /* Copy data. Would like to know when it's ok for us to eg. use
663 * the blitter to copy. Or, use the hardware to do the format
664 * conversion and copy:
667 memcpy(texImage
->Data
, pixels
, imageSize
);
670 GLuint srcImageStride
= _mesa_image_image_stride(unpack
, width
, height
,
673 const GLubyte
*src
= (const GLubyte
*) pixels
;
675 for (i
= 0; i
++ < depth
;) {
676 if (!texImage
->TexFormat
->StoreImage(ctx
, dims
,
677 texImage
->_BaseFormat
,
680 0, 0, 0, /* dstX/Y/Zoffset */
682 texImage
->ImageOffsets
,
684 format
, type
, src
, unpack
)) {
685 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
688 if (stImage
->pt
&& i
< depth
) {
689 st_texture_image_unmap(ctx
->st
, stImage
);
690 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
,
691 PIPE_BUFFER_USAGE_CPU_WRITE
);
692 src
+= srcImageStride
;
697 _mesa_unmap_teximage_pbo(ctx
, unpack
);
700 st_texture_image_unmap(ctx
->st
, stImage
);
701 texImage
->Data
= NULL
;
704 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
705 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
711 st_TexImage3D(GLcontext
* ctx
,
712 GLenum target
, GLint level
,
713 GLint internalFormat
,
714 GLint width
, GLint height
, GLint depth
,
716 GLenum format
, GLenum type
, const void *pixels
,
717 const struct gl_pixelstore_attrib
*unpack
,
718 struct gl_texture_object
*texObj
,
719 struct gl_texture_image
*texImage
)
721 st_TexImage(ctx
, 3, target
, level
,
722 internalFormat
, width
, height
, depth
, border
,
723 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
728 st_TexImage2D(GLcontext
* ctx
,
729 GLenum target
, GLint level
,
730 GLint internalFormat
,
731 GLint width
, GLint height
, GLint border
,
732 GLenum format
, GLenum type
, const void *pixels
,
733 const struct gl_pixelstore_attrib
*unpack
,
734 struct gl_texture_object
*texObj
,
735 struct gl_texture_image
*texImage
)
737 st_TexImage(ctx
, 2, target
, level
,
738 internalFormat
, width
, height
, 1, border
,
739 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
744 st_TexImage1D(GLcontext
* ctx
,
745 GLenum target
, GLint level
,
746 GLint internalFormat
,
747 GLint width
, GLint border
,
748 GLenum format
, GLenum type
, const void *pixels
,
749 const struct gl_pixelstore_attrib
*unpack
,
750 struct gl_texture_object
*texObj
,
751 struct gl_texture_image
*texImage
)
753 st_TexImage(ctx
, 1, target
, level
,
754 internalFormat
, width
, 1, 1, border
,
755 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, 0);
760 st_CompressedTexImage2D( GLcontext
*ctx
, GLenum target
, GLint level
,
761 GLint internalFormat
,
762 GLint width
, GLint height
, GLint border
,
763 GLsizei imageSize
, const GLvoid
*data
,
764 struct gl_texture_object
*texObj
,
765 struct gl_texture_image
*texImage
)
767 st_TexImage(ctx
, 2, target
, level
,
768 internalFormat
, width
, height
, 1, border
,
769 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, 1);
774 * Need to map texture image into memory before copying image data,
778 st_get_tex_image(GLcontext
* ctx
, GLenum target
, GLint level
,
779 GLenum format
, GLenum type
, GLvoid
* pixels
,
780 struct gl_texture_object
*texObj
,
781 struct gl_texture_image
*texImage
, int compressed
)
783 struct st_texture_image
*stImage
= st_texture_image(texImage
);
784 GLuint dstImageStride
= _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
,
785 texImage
->Height
, format
,
793 /* Image is stored in hardware format in a buffer managed by the
794 * kernel. Need to explicitly map and unmap it.
796 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, 0,
797 PIPE_BUFFER_USAGE_CPU_READ
);
798 texImage
->RowStride
= stImage
->surface
->pitch
;
801 /* Otherwise, the image should actually be stored in
802 * texImage->Data. This is pretty confusing for
803 * everybody, I'd much prefer to separate the two functions of
804 * texImage->Data - storage for texture images in main memory
805 * and access (ie mappings) of images. In other words, we'd
806 * create a new texImage->Map field and leave Data simply for
809 assert(texImage
->Data
);
812 depth
= texImage
->Depth
;
815 dest
= (GLubyte
*) pixels
;
817 for (i
= 0; i
++ < depth
;) {
819 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
822 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
826 if (stImage
->pt
&& i
< depth
) {
827 st_texture_image_unmap(ctx
->st
, stImage
);
828 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, i
,
829 PIPE_BUFFER_USAGE_CPU_READ
);
830 dest
+= dstImageStride
;
834 texImage
->Depth
= depth
;
838 st_texture_image_unmap(ctx
->st
, stImage
);
839 texImage
->Data
= NULL
;
845 st_GetTexImage(GLcontext
* ctx
, GLenum target
, GLint level
,
846 GLenum format
, GLenum type
, GLvoid
* pixels
,
847 struct gl_texture_object
*texObj
,
848 struct gl_texture_image
*texImage
)
850 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
,
851 texObj
, texImage
, 0);
856 st_GetCompressedTexImage(GLcontext
*ctx
, GLenum target
, GLint level
,
858 const struct gl_texture_object
*texObj
,
859 const struct gl_texture_image
*texImage
)
861 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
,
862 (struct gl_texture_object
*) texObj
,
863 (struct gl_texture_image
*) texImage
, 1);
869 st_TexSubimage(GLcontext
* ctx
,
871 GLenum target
, GLint level
,
872 GLint xoffset
, GLint yoffset
, GLint zoffset
,
873 GLint width
, GLint height
, GLint depth
,
874 GLenum format
, GLenum type
, const void *pixels
,
875 const struct gl_pixelstore_attrib
*packing
,
876 struct gl_texture_object
*texObj
,
877 struct gl_texture_image
*texImage
)
879 struct st_texture_image
*stImage
= st_texture_image(texImage
);
881 GLuint srcImageStride
= _mesa_image_image_stride(packing
, width
, height
,
886 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
887 _mesa_lookup_enum_by_nr(target
),
888 level
, xoffset
, yoffset
, width
, height
);
891 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
892 type
, pixels
, packing
, "glTexSubImage2D");
896 /* Map buffer if necessary. Need to lock to prevent other contexts
897 * from uploading the buffer under us.
900 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
,
901 PIPE_BUFFER_USAGE_CPU_WRITE
);
902 dstRowStride
= stImage
->surface
->pitch
* stImage
->surface
->cpp
;
905 if (!texImage
->Data
) {
906 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
910 src
= (const GLubyte
*) pixels
;
912 for (i
= 0; i
++ < depth
;) {
913 if (!texImage
->TexFormat
->StoreImage(ctx
, dims
, texImage
->_BaseFormat
,
918 texImage
->ImageOffsets
,
920 format
, type
, src
, packing
)) {
921 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
924 if (stImage
->pt
&& i
< depth
) {
925 /* map next slice of 3D texture */
926 st_texture_image_unmap(ctx
->st
, stImage
);
927 texImage
->Data
= st_texture_image_map(ctx
->st
, stImage
, zoffset
+ i
,
928 PIPE_BUFFER_USAGE_CPU_WRITE
);
929 src
+= srcImageStride
;
933 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
934 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
937 _mesa_unmap_teximage_pbo(ctx
, packing
);
940 st_texture_image_unmap(ctx
->st
, 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 struct pipe_screen
*screen
= pipe
->screen
;
1043 const uint face
= texture_face(target
);
1044 struct pipe_texture
*pt
= stImage
->pt
;
1045 struct pipe_surface
*src_surf
, *dest_surf
;
1048 st_flush(ctx
->st
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
1050 /* determine bottom-to-top vs. top-to-bottom order */
1051 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1052 destY
= height
- 1 - destY
;
1059 src_surf
= strb
->surface
;
1061 dest_surf
= screen
->get_tex_surface(screen
, pt
, face
, level
, destZ
,
1062 PIPE_BUFFER_USAGE_CPU_WRITE
);
1064 assert(width
<= MAX_WIDTH
);
1067 * To avoid a large temp memory allocation, do copy row by row.
1069 if (baseFormat
== GL_DEPTH_COMPONENT
) {
1070 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1071 ctx
->Pixel
.DepthBias
!= 0.0F
);
1073 for (row
= 0; row
< height
; row
++, srcY
++, destY
+= yStep
) {
1074 uint data
[MAX_WIDTH
];
1075 pipe_get_tile_z(pipe
, src_surf
, srcX
, srcY
, width
, 1, data
);
1077 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1079 pipe_put_tile_z(pipe
, dest_surf
, destX
, destY
, width
, 1, data
);
1084 for (row
= 0; row
< height
; row
++, srcY
++, destY
+= yStep
) {
1085 float data
[4 * MAX_WIDTH
];
1086 pipe_get_tile_rgba(pipe
, src_surf
, srcX
, srcY
, width
, 1, data
);
1087 /* XXX we're ignoring convolution for now */
1088 if (ctx
->_ImageTransferState
) {
1089 _mesa_apply_rgba_transfer_ops(ctx
,
1090 ctx
->_ImageTransferState
& ~IMAGE_CONVOLUTION_BIT
,
1091 width
, (GLfloat (*)[4]) data
);
1093 pipe_put_tile_rgba(pipe
, dest_surf
, destX
, destY
, width
, 1, data
);
1102 * Do a CopyTex[Sub]Image using an optimized hardware (blit) path.
1103 * Note that the region to copy has already been clip tested.
1105 * Note: srcY=0=Bottom of renderbuffer
1107 * \return GL_TRUE if success, GL_FALSE if failure (use a fallback)
1110 do_copy_texsubimage(GLcontext
*ctx
,
1111 GLenum target
, GLint level
,
1112 GLint destX
, GLint destY
, GLint destZ
,
1113 GLint srcX
, GLint srcY
,
1114 GLsizei width
, GLsizei height
)
1116 struct gl_texture_unit
*texUnit
=
1117 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1118 struct gl_texture_object
*texObj
=
1119 _mesa_select_tex_object(ctx
, texUnit
, target
);
1120 struct gl_texture_image
*texImage
=
1121 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1122 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1123 GLenum baseFormat
= texImage
->InternalFormat
;
1124 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1125 struct st_renderbuffer
*strb
;
1126 struct pipe_context
*pipe
= ctx
->st
->pipe
;
1127 struct pipe_screen
*screen
= pipe
->screen
;
1128 struct pipe_surface
*dest_surface
;
1129 uint dest_format
, src_format
;
1130 uint do_flip
= FALSE
;
1131 GLboolean use_fallback
= GL_TRUE
;
1135 /* determine if copying depth or color data */
1136 if (baseFormat
== GL_DEPTH_COMPONENT
) {
1137 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1139 else if (baseFormat
== GL_DEPTH_STENCIL_EXT
) {
1140 strb
= st_renderbuffer(fb
->_StencilBuffer
);
1143 /* baseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1144 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1148 assert(strb
->surface
);
1149 assert(stImage
->pt
);
1151 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1152 srcY
= strb
->Base
.Height
- srcY
- height
;
1156 src_format
= strb
->surface
->format
;
1157 dest_format
= stImage
->pt
->format
;
1159 dest_surface
= screen
->get_tex_surface(screen
, stImage
->pt
, stImage
->face
,
1160 stImage
->level
, destZ
,
1161 PIPE_BUFFER_USAGE_CPU_WRITE
);
1163 if (ctx
->_ImageTransferState
== 0x0 &&
1164 strb
->surface
->buffer
&&
1165 dest_surface
->buffer
) {
1166 /* do blit-style copy */
1168 /* XXX may need to invert image depending on window
1169 * vs. user-created FBO
1173 /* A bit of fiddling to get the blitter to work with -ve
1174 * pitches. But we get a nice inverted blit this way, so it's
1177 intelEmitCopyBlit(intel
,
1181 src
->height
* src
->pitch
* src
->cpp
,
1183 stImage
->pt
->region
->buffer
,
1185 x
, y
+ height
, dstx
, dsty
, width
, height
,
1189 if (src_format
== dest_format
) {
1190 pipe
->surface_copy(pipe
,
1200 use_fallback
= GL_FALSE
;
1202 else if (screen
->is_format_supported(screen
, strb
->surface
->format
,
1204 screen
->is_format_supported(screen
, dest_surface
->format
,
1206 util_blit_pixels(ctx
->st
->blit
,
1208 srcX
, do_flip
? srcY
+ height
: srcY
,
1209 srcX
+ width
, do_flip
? srcY
: srcY
+ height
,
1211 destX
, destY
, destX
+ width
, destY
+ height
,
1212 0.0, PIPE_TEX_MIPFILTER_NEAREST
);
1213 use_fallback
= GL_FALSE
;
1219 fallback_copy_texsubimage(ctx
, target
, level
,
1220 strb
, stImage
, baseFormat
,
1221 destX
, destY
, destZ
,
1222 srcX
, srcY
, width
, height
);
1225 pipe_surface_reference(&dest_surface
, NULL
);
1227 if (level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
1228 ctx
->Driver
.GenerateMipmap(ctx
, target
, texObj
);
1235 st_CopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1236 GLenum internalFormat
,
1237 GLint x
, GLint y
, GLsizei width
, GLint border
)
1239 struct gl_texture_unit
*texUnit
=
1240 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1241 struct gl_texture_object
*texObj
=
1242 _mesa_select_tex_object(ctx
, texUnit
, target
);
1243 struct gl_texture_image
*texImage
=
1244 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1251 /* Setup or redefine the texture object, texture and texture
1252 * image. Don't populate yet.
1254 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1256 GL_RGBA
, CHAN_TYPE
, NULL
,
1257 &ctx
->DefaultPacking
, texObj
, texImage
);
1259 do_copy_texsubimage(ctx
, target
, level
,
1266 st_CopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1267 GLenum internalFormat
,
1268 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1271 struct gl_texture_unit
*texUnit
=
1272 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1273 struct gl_texture_object
*texObj
=
1274 _mesa_select_tex_object(ctx
, texUnit
, target
);
1275 struct gl_texture_image
*texImage
=
1276 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1283 /* Setup or redefine the texture object, texture and texture
1284 * image. Don't populate yet.
1286 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1287 width
, height
, border
,
1288 GL_RGBA
, CHAN_TYPE
, NULL
,
1289 &ctx
->DefaultPacking
, texObj
, texImage
);
1292 do_copy_texsubimage(ctx
, target
, level
,
1294 x
, y
, width
, height
);
1299 st_CopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
1300 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1302 const GLint yoffset
= 0, zoffset
= 0;
1303 const GLsizei height
= 1;
1304 do_copy_texsubimage(ctx
, target
, level
,
1305 xoffset
, yoffset
, zoffset
,
1306 x
, y
, width
, height
);
1311 st_CopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
1312 GLint xoffset
, GLint yoffset
,
1313 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1315 const GLint zoffset
= 0;
1316 do_copy_texsubimage(ctx
, target
, level
,
1317 xoffset
, yoffset
, zoffset
,
1318 x
, y
, width
, height
);
1323 st_CopyTexSubImage3D(GLcontext
* ctx
, GLenum target
, GLint level
,
1324 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1325 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1327 do_copy_texsubimage(ctx
, target
, level
,
1328 xoffset
, yoffset
, zoffset
,
1329 x
, y
, width
, height
);
1336 * Compute which mipmap levels that really need to be sent to the hardware.
1337 * This depends on the base image size, GL_TEXTURE_MIN_LOD,
1338 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
1341 calculate_first_last_level(struct st_texture_object
*stObj
)
1343 struct gl_texture_object
*tObj
= &stObj
->base
;
1345 /* These must be signed values. MinLod and MaxLod can be negative numbers,
1346 * and having firstLevel and lastLevel as signed prevents the need for
1347 * extra sign checks.
1352 /* Yes, this looks overly complicated, but it's all needed.
1354 switch (tObj
->Target
) {
1358 case GL_TEXTURE_CUBE_MAP
:
1359 if (tObj
->MinFilter
== GL_NEAREST
|| tObj
->MinFilter
== GL_LINEAR
) {
1360 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
1362 firstLevel
= lastLevel
= tObj
->BaseLevel
;
1366 lastLevel
= MIN2(tObj
->MaxLevel
, tObj
->Image
[0][tObj
->BaseLevel
]->WidthLog2
);
1369 case GL_TEXTURE_RECTANGLE_NV
:
1370 case GL_TEXTURE_4D_SGIS
:
1371 firstLevel
= lastLevel
= 0;
1377 stObj
->lastLevel
= lastLevel
;
1382 copy_image_data_to_texture(struct st_context
*st
,
1383 struct st_texture_object
*stObj
,
1385 struct st_texture_image
*stImage
)
1388 /* Copy potentially with the blitter:
1390 st_texture_image_copy(st
->pipe
,
1391 stObj
->pt
, dstLevel
, /* dest texture, level */
1392 stImage
->pt
, /* src texture */
1396 pipe_texture_release(&stImage
->pt
);
1398 else if (stImage
->base
.Data
) {
1399 assert(stImage
->base
.Data
!= NULL
);
1401 /* More straightforward upload.
1403 st_texture_image_data(st
->pipe
,
1408 stImage
->base
.RowStride
,
1409 stImage
->base
.RowStride
*
1410 stImage
->base
.Height
);
1411 _mesa_align_free(stImage
->base
.Data
);
1412 stImage
->base
.Data
= NULL
;
1415 pipe_texture_reference(&stImage
->pt
, stObj
->pt
);
1420 * Called during state validation. When this function is finished,
1421 * the texture object should be ready for rendering.
1422 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1425 st_finalize_texture(GLcontext
*ctx
,
1426 struct pipe_context
*pipe
,
1427 struct gl_texture_object
*tObj
,
1428 GLboolean
*needFlush
)
1430 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1431 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1435 struct st_texture_image
*firstImage
;
1437 *needFlush
= GL_FALSE
;
1439 /* We know/require this is true by now:
1441 assert(stObj
->base
._Complete
);
1443 /* What levels must the texture include at a minimum?
1445 calculate_first_last_level(stObj
);
1446 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1451 if (firstImage
->base
.Border
) {
1453 pipe_texture_release(&stObj
->pt
);
1459 /* If both firstImage and stObj point to a texture which can contain
1460 * all active images, favour firstImage. Note that because of the
1461 * completeness requirement, we know that the image dimensions
1464 if (firstImage
->pt
&&
1465 firstImage
->pt
!= stObj
->pt
&&
1466 firstImage
->pt
->last_level
>= stObj
->lastLevel
) {
1469 pipe_texture_release(&stObj
->pt
);
1471 pipe_texture_reference(&stObj
->pt
, firstImage
->pt
);
1474 if (firstImage
->base
.IsCompressed
) {
1475 comp_byte
= compressed_num_bytes(firstImage
->base
.TexFormat
->MesaFormat
);
1479 cpp
= firstImage
->base
.TexFormat
->TexelBytes
;
1482 /* Check texture can hold all active levels. Check texture matches
1483 * target, imageFormat, etc.
1486 (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1487 stObj
->pt
->format
!=
1488 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
->MesaFormat
) ||
1489 stObj
->pt
->last_level
< stObj
->lastLevel
||
1490 stObj
->pt
->cpp
!= cpp
||
1491 stObj
->pt
->width
[0] != firstImage
->base
.Width2
||
1492 stObj
->pt
->height
[0] != firstImage
->base
.Height2
||
1493 stObj
->pt
->depth
[0] != firstImage
->base
.Depth2
||
1494 stObj
->pt
->compressed
!= firstImage
->base
.IsCompressed
)) {
1495 pipe_texture_release(&stObj
->pt
);
1499 /* May need to create a new texture:
1502 stObj
->pt
= st_texture_create(ctx
->st
,
1503 gl_target_to_pipe(stObj
->base
.Target
),
1504 st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
->MesaFormat
),
1506 firstImage
->base
.Width2
,
1507 firstImage
->base
.Height2
,
1508 firstImage
->base
.Depth2
,
1511 ( PIPE_TEXTURE_USAGE_RENDER_TARGET
|
1512 PIPE_TEXTURE_USAGE_SAMPLER
));
1515 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1520 /* Pull in any images not in the object's texture:
1522 for (face
= 0; face
< nr_faces
; face
++) {
1524 for (level
= 0; level
<= stObj
->lastLevel
; level
++) {
1525 struct st_texture_image
*stImage
=
1526 st_texture_image(stObj
->base
.Image
[face
][stObj
->base
.BaseLevel
+ level
]);
1528 /* Need to import images in main memory or held in other textures.
1530 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1531 copy_image_data_to_texture(ctx
->st
, stObj
, level
, stImage
);
1532 *needFlush
= GL_TRUE
;
1544 st_init_texture_functions(struct dd_function_table
*functions
)
1546 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1547 functions
->TexImage1D
= st_TexImage1D
;
1548 functions
->TexImage2D
= st_TexImage2D
;
1549 functions
->TexImage3D
= st_TexImage3D
;
1550 functions
->TexSubImage1D
= st_TexSubImage1D
;
1551 functions
->TexSubImage2D
= st_TexSubImage2D
;
1552 functions
->TexSubImage3D
= st_TexSubImage3D
;
1553 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
1554 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
1555 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1556 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1557 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1558 functions
->GenerateMipmap
= st_generate_mipmap
;
1560 functions
->GetTexImage
= st_GetTexImage
;
1562 /* compressed texture functions */
1563 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1564 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
1565 functions
->CompressedTextureSize
= _mesa_compressed_texture_size
;
1567 functions
->NewTextureObject
= st_NewTextureObject
;
1568 functions
->NewTextureImage
= st_NewTextureImage
;
1569 functions
->DeleteTexture
= st_DeleteTextureObject
;
1570 functions
->FreeTexImageData
= st_FreeTextureImageData
;
1571 functions
->UpdateTexturePalette
= 0;
1572 functions
->IsTextureResident
= st_IsTextureResident
;
1574 functions
->TextureMemCpy
= do_memcpy
;
1576 /* XXX Temporary until we can query pipe's texture sizes */
1577 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;