1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "main/mfeatures.h"
29 #include "main/bufferobj.h"
30 #include "main/enums.h"
31 #include "main/fbobject.h"
32 #include "main/formats.h"
33 #include "main/image.h"
34 #include "main/imports.h"
35 #include "main/macros.h"
36 #include "main/mipmap.h"
37 #include "main/pack.h"
39 #include "main/pixeltransfer.h"
40 #include "main/texcompress.h"
41 #include "main/texgetimage.h"
42 #include "main/teximage.h"
43 #include "main/texobj.h"
44 #include "main/texstore.h"
46 #include "state_tracker/st_debug.h"
47 #include "state_tracker/st_context.h"
48 #include "state_tracker/st_cb_fbo.h"
49 #include "state_tracker/st_cb_flush.h"
50 #include "state_tracker/st_cb_texture.h"
51 #include "state_tracker/st_format.h"
52 #include "state_tracker/st_texture.h"
53 #include "state_tracker/st_gen_mipmap.h"
54 #include "state_tracker/st_atom.h"
56 #include "pipe/p_context.h"
57 #include "pipe/p_defines.h"
58 #include "util/u_inlines.h"
59 #include "pipe/p_shader_tokens.h"
60 #include "util/u_tile.h"
61 #include "util/u_blit.h"
62 #include "util/u_format.h"
63 #include "util/u_surface.h"
64 #include "util/u_sampler.h"
65 #include "util/u_math.h"
66 #include "util/u_box.h"
68 #define DBG if (0) printf
71 static enum pipe_texture_target
72 gl_target_to_pipe(GLenum target
)
76 return PIPE_TEXTURE_1D
;
78 case GL_TEXTURE_EXTERNAL_OES
:
79 return PIPE_TEXTURE_2D
;
80 case GL_TEXTURE_RECTANGLE_NV
:
81 return PIPE_TEXTURE_RECT
;
83 return PIPE_TEXTURE_3D
;
84 case GL_TEXTURE_CUBE_MAP_ARB
:
85 return PIPE_TEXTURE_CUBE
;
86 case GL_TEXTURE_1D_ARRAY_EXT
:
87 return PIPE_TEXTURE_1D_ARRAY
;
88 case GL_TEXTURE_2D_ARRAY_EXT
:
89 return PIPE_TEXTURE_2D_ARRAY
;
90 case GL_TEXTURE_BUFFER
:
99 /** called via ctx->Driver.NewTextureImage() */
100 static struct gl_texture_image
*
101 st_NewTextureImage(struct gl_context
* ctx
)
103 DBG("%s\n", __FUNCTION__
);
105 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
109 /** called via ctx->Driver.DeleteTextureImage() */
111 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
113 /* nothing special (yet) for st_texture_image */
114 _mesa_delete_texture_image(ctx
, img
);
118 /** called via ctx->Driver.NewTextureObject() */
119 static struct gl_texture_object
*
120 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
122 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
124 DBG("%s\n", __FUNCTION__
);
125 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
130 /** called via ctx->Driver.DeleteTextureObject() */
132 st_DeleteTextureObject(struct gl_context
*ctx
,
133 struct gl_texture_object
*texObj
)
135 struct st_context
*st
= st_context(ctx
);
136 struct st_texture_object
*stObj
= st_texture_object(texObj
);
138 pipe_resource_reference(&stObj
->pt
, NULL
);
139 if (stObj
->sampler_view
) {
140 if (stObj
->sampler_view
->context
!= st
->pipe
) {
141 /* Take "ownership" of this texture sampler view by setting
142 * its context pointer to this context. This avoids potential
143 * crashes when the texture object is shared among contexts
144 * and the original/owner context has already been destroyed.
146 stObj
->sampler_view
->context
= st
->pipe
;
148 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
150 _mesa_delete_texture_object(ctx
, texObj
);
154 /** called via ctx->Driver.FreeTextureImageBuffer() */
156 st_FreeTextureImageBuffer(struct gl_context
* ctx
, struct gl_texture_image
*texImage
)
158 struct st_texture_image
*stImage
= st_texture_image(texImage
);
160 DBG("%s\n", __FUNCTION__
);
163 pipe_resource_reference(&stImage
->pt
, NULL
);
166 if (stImage
->TexData
) {
167 _mesa_align_free(stImage
->TexData
);
168 stImage
->TexData
= NULL
;
173 /** called via ctx->Driver.MapTextureImage() */
175 st_MapTextureImage(struct gl_context
*ctx
,
176 struct gl_texture_image
*texImage
,
177 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
179 GLubyte
**mapOut
, GLint
*rowStrideOut
)
181 struct st_context
*st
= st_context(ctx
);
182 struct st_texture_image
*stImage
= st_texture_image(texImage
);
187 if (mode
& GL_MAP_READ_BIT
)
188 pipeMode
|= PIPE_TRANSFER_READ
;
189 if (mode
& GL_MAP_WRITE_BIT
)
190 pipeMode
|= PIPE_TRANSFER_WRITE
;
192 map
= st_texture_image_map(st
, stImage
, slice
, pipeMode
, x
, y
, w
, h
);
195 *rowStrideOut
= stImage
->transfer
->stride
;
204 /** called via ctx->Driver.UnmapTextureImage() */
206 st_UnmapTextureImage(struct gl_context
*ctx
,
207 struct gl_texture_image
*texImage
,
210 struct st_context
*st
= st_context(ctx
);
211 struct st_texture_image
*stImage
= st_texture_image(texImage
);
212 st_texture_image_unmap(st
, stImage
);
217 * Return default texture resource binding bitmask for the given format.
220 default_bindings(struct st_context
*st
, enum pipe_format format
)
222 struct pipe_screen
*screen
= st
->pipe
->screen
;
223 const unsigned target
= PIPE_TEXTURE_2D
;
226 if (util_format_is_depth_or_stencil(format
))
227 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
229 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
231 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
235 format
= util_format_linear(format
);
237 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
240 return PIPE_BIND_SAMPLER_VIEW
;
245 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
247 get_texture_dims(GLenum target
)
251 case GL_TEXTURE_1D_ARRAY_EXT
:
252 case GL_TEXTURE_BUFFER
:
255 case GL_TEXTURE_CUBE_MAP_ARB
:
256 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
257 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
258 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
259 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
260 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
261 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
262 case GL_TEXTURE_RECTANGLE_NV
:
263 case GL_TEXTURE_2D_ARRAY_EXT
:
264 case GL_TEXTURE_EXTERNAL_OES
:
269 assert(0 && "invalid texture target in get_texture_dims()");
276 * Given the size of a mipmap image, try to compute the size of the level=0
279 * Note that this isn't always accurate for odd-sized, non-POW textures.
280 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
282 * \return GL_TRUE for success, GL_FALSE for failure
285 guess_base_level_size(GLenum target
,
286 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
287 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
289 const GLuint dims
= get_texture_dims(target
);
296 /* Depending on the image's size, we can't always make a guess here */
297 if ((dims
>= 1 && width
== 1) ||
298 (dims
>= 2 && height
== 1) ||
299 (dims
>= 3 && depth
== 1)) {
300 /* we can't determine the image size at level=0 */
304 /* grow the image size until we hit level = 0 */
325 * Try to allocate a pipe_resource object for the given st_texture_object.
327 * We use the given st_texture_image as a clue to determine the size of the
328 * mipmap image at level=0.
330 * \return GL_TRUE for success, GL_FALSE if out of memory.
333 guess_and_alloc_texture(struct st_context
*st
,
334 struct st_texture_object
*stObj
,
335 const struct st_texture_image
*stImage
)
337 GLuint lastLevel
, width
, height
, depth
;
339 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
340 enum pipe_format fmt
;
342 DBG("%s\n", __FUNCTION__
);
346 if (!guess_base_level_size(stObj
->base
.Target
,
347 stImage
->base
.Width2
,
348 stImage
->base
.Height2
,
349 stImage
->base
.Depth2
,
351 &width
, &height
, &depth
)) {
352 /* we can't determine the image size at level=0 */
353 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
354 /* this is not an out of memory error */
358 /* At this point, (width x height x depth) is the expected size of
359 * the level=0 mipmap image.
362 /* Guess a reasonable value for lastLevel. With OpenGL we have no
363 * idea how many mipmap levels will be in a texture until we start
364 * to render with it. Make an educated guess here but be prepared
365 * to re-allocating a texture buffer with space for more (or fewer)
366 * mipmap levels later.
368 if ((stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
369 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
370 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
371 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
372 !stObj
->base
.GenerateMipmap
&&
373 stImage
->base
.Level
== 0) {
374 /* only alloc space for a single mipmap level */
378 /* alloc space for a full mipmap */
379 GLuint l2width
= util_logbase2(width
);
380 GLuint l2height
= util_logbase2(height
);
381 GLuint l2depth
= util_logbase2(depth
);
382 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
385 /* Save the level=0 dimensions */
386 stObj
->width0
= width
;
387 stObj
->height0
= height
;
388 stObj
->depth0
= depth
;
390 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
392 bindings
= default_bindings(st
, fmt
);
394 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
395 width
, height
, depth
,
396 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
398 stObj
->pt
= st_texture_create(st
,
399 gl_target_to_pipe(stObj
->base
.Target
),
408 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
410 return stObj
->pt
!= NULL
;
415 * Called via ctx->Driver.AllocTextureImageBuffer().
416 * If the texture object/buffer already has space for the indicated image,
417 * we're done. Otherwise, allocate memory for the new texture image.
418 * XXX This function and st_TexImage() have some duplicated code. That
419 * can be cleaned up in the future.
422 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
423 struct gl_texture_image
*texImage
,
424 gl_format format
, GLsizei width
,
425 GLsizei height
, GLsizei depth
)
427 struct st_context
*st
= st_context(ctx
);
428 struct st_texture_image
*stImage
= st_texture_image(texImage
);
429 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
430 const GLuint level
= texImage
->Level
;
432 DBG("%s\n", __FUNCTION__
);
437 assert(!stImage
->TexData
);
438 assert(!stImage
->pt
); /* xxx this might be wrong */
440 /* Look if the parent texture object has space for this image */
442 level
<= stObj
->pt
->last_level
&&
443 st_texture_match_image(stObj
->pt
, texImage
)) {
444 /* this image will fit in the existing texture object's memory */
445 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
449 /* The parent texture object does not have space for this image */
451 pipe_resource_reference(&stObj
->pt
, NULL
);
452 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
454 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
455 /* Probably out of memory.
456 * Try flushing any pending rendering, then retry.
459 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
460 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
466 st_texture_match_image(stObj
->pt
, texImage
)) {
467 /* The image will live in the object's mipmap memory */
468 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
473 /* Create a new, temporary texture/resource/buffer to hold this
476 enum pipe_format format
=
477 st_mesa_format_to_pipe_format(texImage
->TexFormat
);
478 GLuint bindings
= default_bindings(st
, format
);
479 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
481 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
482 width
, height
, depth
,
483 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
485 stImage
->pt
= st_texture_create(st
,
486 gl_target_to_pipe(stObj
->base
.Target
),
494 return stImage
->pt
!= NULL
;
499 * Do glTexImage1/2/3D().
502 st_TexImage(struct gl_context
* ctx
,
504 GLenum target
, GLint level
,
505 GLint internalFormat
,
506 GLint width
, GLint height
, GLint depth
,
508 GLenum format
, GLenum type
, const void *pixels
,
509 const struct gl_pixelstore_attrib
*unpack
,
510 struct gl_texture_object
*texObj
,
511 struct gl_texture_image
*texImage
,
512 GLsizei imageSize
, GLboolean compressed_src
)
514 struct st_context
*st
= st_context(ctx
);
515 struct st_texture_object
*stObj
= st_texture_object(texObj
);
516 struct st_texture_image
*stImage
= st_texture_image(texImage
);
517 GLuint dstRowStride
= 0;
518 enum pipe_transfer_usage transfer_usage
= 0;
521 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
522 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
524 /* switch to "normal" */
525 if (stObj
->surface_based
) {
528 _mesa_clear_texture_object(ctx
, texObj
);
529 pipe_resource_reference(&stObj
->pt
, NULL
);
531 /* oops, need to init this image again */
532 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
533 internalFormat
, format
, type
);
535 _mesa_init_teximage_fields(ctx
, target
, texImage
,
536 width
, height
, depth
, border
,
537 internalFormat
, texFormat
);
539 stObj
->surface_based
= GL_FALSE
;
542 assert(texImage
->Width
== width
);
543 assert(texImage
->Height
== height
);
544 assert(texImage
->Depth
== depth
);
546 stImage
->base
.Face
= _mesa_tex_target_to_face(target
);
547 stImage
->base
.Level
= level
;
549 /* Release the reference to a potentially orphaned buffer.
550 * Release any old malloced memory.
553 pipe_resource_reference(&stImage
->pt
, NULL
);
554 assert(!stImage
->TexData
);
556 else if (stImage
->TexData
) {
557 _mesa_align_free(stImage
->TexData
);
561 * See if the new image is somehow incompatible with the existing
562 * mipmap. If so, free the old mipmap.
565 if (level
> (GLint
) stObj
->pt
->last_level
||
566 !st_texture_match_image(stObj
->pt
, &stImage
->base
)) {
568 pipe_resource_reference(&stObj
->pt
, NULL
);
570 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
574 if (width
== 0 || height
== 0 || depth
== 0) {
575 /* stop after freeing old image */
580 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
581 /* Probably out of memory.
582 * Try flushing any pending rendering, then retry.
585 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
586 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
592 assert(!stImage
->pt
);
594 /* Check if this texture image can live inside the texture object's buffer.
595 * If so, store the image there. Otherwise the image will temporarily live
599 st_texture_match_image(stObj
->pt
, &stImage
->base
)) {
601 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
606 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
608 /* Pixel data may come from regular user memory or a PBO. For the later,
609 * do bounds checking and map the PBO to read pixels data from it.
611 * XXX we should try to use a GPU-accelerated path to copy the image data
612 * from the PBO to the texture.
614 if (compressed_src
) {
615 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
617 "glCompressedTexImage");
620 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
622 pixels
, unpack
, "glTexImage");
625 /* for a 1D array upload the image as a series of layer with height = 1 */
626 if (target
== GL_TEXTURE_1D_ARRAY
) {
632 * Prepare to store the texture data. Either map the gallium texture buffer
633 * memory or malloc space for it.
637 /* We've allocated texture resource, but have no pixel data - all done. */
641 /* Store the image in the gallium transfer object */
642 if (format
== GL_DEPTH_COMPONENT
&&
643 util_format_is_depth_and_stencil(stImage
->pt
->format
))
644 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
646 transfer_usage
= PIPE_TRANSFER_WRITE
;
648 dstMap
= st_texture_image_map(st
, stImage
, 0,
649 transfer_usage
, 0, 0, width
, height
);
650 if(stImage
->transfer
)
651 dstRowStride
= stImage
->transfer
->stride
;
654 /* Allocate regular memory and store the image there temporarily. */
655 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
656 width
, height
, depth
);
657 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
659 stImage
->TexData
= _mesa_align_malloc(imageSize
, 16);
660 dstMap
= stImage
->TexData
;
664 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
669 /* We've allocated texture memory, but have no pixel data - all done. */
673 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
674 width
, height
, depth
, width
, dstRowStride
);
676 /* Copy user texture image into the mapped texture buffer.
678 if (compressed_src
) {
679 const GLuint srcRowStride
=
680 _mesa_format_row_stride(texImage
->TexFormat
, width
);
681 if (dstRowStride
== srcRowStride
) {
682 memcpy(dstMap
, pixels
, imageSize
);
685 GLubyte
*dst
= dstMap
;
686 const char *src
= pixels
;
687 GLuint i
, bw
, bh
, lines
;
688 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
689 lines
= (height
+ bh
- 1) / bh
;
691 for (i
= 0; i
< lines
; ++i
) {
692 memcpy(dst
, src
, srcRowStride
);
699 const GLuint srcImageStride
=
700 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
702 const GLubyte
*src
= (const GLubyte
*) pixels
;
704 for (i
= 0; i
< depth
; i
++) {
705 if (!_mesa_texstore(ctx
, dims
,
706 texImage
->_BaseFormat
,
708 0, 0, 0, /* dstX/Y/Zoffset */
710 (GLubyte
**) &dstMap
, /* dstSlice */
712 format
, type
, src
, unpack
)) {
713 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
716 if (stImage
->pt
&& i
+ 1 < depth
) {
717 /* unmap this slice */
718 st_texture_image_unmap(st
, stImage
);
719 /* map next slice of 3D texture */
720 dstMap
= st_texture_image_map(st
, stImage
, i
+ 1,
721 transfer_usage
, 0, 0,
723 src
+= srcImageStride
;
729 _mesa_unmap_teximage_pbo(ctx
, unpack
);
731 if (stImage
->pt
&& stImage
->transfer
) {
732 st_texture_image_unmap(st
, stImage
);
738 st_TexImage3D(struct gl_context
* ctx
,
739 GLenum target
, GLint level
,
740 GLint internalFormat
,
741 GLint width
, GLint height
, GLint depth
,
743 GLenum format
, GLenum type
, const void *pixels
,
744 const struct gl_pixelstore_attrib
*unpack
,
745 struct gl_texture_object
*texObj
,
746 struct gl_texture_image
*texImage
)
748 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
749 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
755 st_TexImage2D(struct gl_context
* ctx
,
756 GLenum target
, GLint level
,
757 GLint internalFormat
,
758 GLint width
, GLint height
, GLint border
,
759 GLenum format
, GLenum type
, const void *pixels
,
760 const struct gl_pixelstore_attrib
*unpack
,
761 struct gl_texture_object
*texObj
,
762 struct gl_texture_image
*texImage
)
764 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
765 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
770 st_TexImage1D(struct gl_context
* ctx
,
771 GLenum target
, GLint level
,
772 GLint internalFormat
,
773 GLint width
, GLint border
,
774 GLenum format
, GLenum type
, const void *pixels
,
775 const struct gl_pixelstore_attrib
*unpack
,
776 struct gl_texture_object
*texObj
,
777 struct gl_texture_image
*texImage
)
779 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
780 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
785 st_CompressedTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
786 GLint internalFormat
,
787 GLint width
, GLint height
, GLint border
,
788 GLsizei imageSize
, const GLvoid
*data
,
789 struct gl_texture_object
*texObj
,
790 struct gl_texture_image
*texImage
)
792 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
793 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
799 * glGetTexImage() helper: decompress a compressed texture by rendering
800 * a textured quad. Store the results in the user's buffer.
803 decompress_with_blit(struct gl_context
* ctx
,
804 GLenum format
, GLenum type
, GLvoid
*pixels
,
805 struct gl_texture_image
*texImage
)
807 struct st_context
*st
= st_context(ctx
);
808 struct pipe_context
*pipe
= st
->pipe
;
809 struct st_texture_image
*stImage
= st_texture_image(texImage
);
810 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
811 struct pipe_sampler_view
*src_view
=
812 st_get_texture_sampler_view(stObj
, pipe
);
813 const GLuint width
= texImage
->Width
;
814 const GLuint height
= texImage
->Height
;
815 struct pipe_surface
*dst_surface
;
816 struct pipe_resource
*dst_texture
;
817 struct pipe_transfer
*tex_xfer
;
818 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
819 PIPE_BIND_TRANSFER_READ
);
821 /* create temp / dest surface */
822 if (!util_create_rgba_surface(pipe
, width
, height
, bind
,
823 &dst_texture
, &dst_surface
)) {
824 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
825 "in decompress_with_blit()");
829 /* Disable conditional rendering. */
830 if (st
->render_condition
) {
831 pipe
->render_condition(pipe
, NULL
, 0);
834 /* Choose the source mipmap level */
835 src_view
->u
.tex
.first_level
= src_view
->u
.tex
.last_level
= texImage
->Level
;
837 /* blit/render/decompress */
838 util_blit_pixels_tex(st
->blit
,
839 src_view
, /* pipe_resource (src) */
840 0, 0, /* src x0, y0 */
841 width
, height
, /* src x1, y1 */
842 dst_surface
, /* pipe_surface (dst) */
843 0, 0, /* dst x0, y0 */
844 width
, height
, /* dst x1, y1 */
846 PIPE_TEX_MIPFILTER_NEAREST
);
848 /* Restore conditional rendering state. */
849 if (st
->render_condition
) {
850 pipe
->render_condition(pipe
, st
->render_condition
,
854 /* map the dst_surface so we can read from it */
855 tex_xfer
= pipe_get_transfer(pipe
,
858 0, 0, width
, height
);
860 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
862 /* copy/pack data into user buffer */
863 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
865 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
866 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
868 for (row
= 0; row
< height
; row
++) {
869 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
870 height
, format
, type
, row
, 0);
871 memcpy(dest
, map
, bytesPerRow
);
872 map
+= tex_xfer
->stride
;
874 pipe_transfer_unmap(pipe
, tex_xfer
);
877 /* format translation via floats */
879 enum pipe_format pformat
= util_format_linear(dst_texture
->format
);
880 for (row
= 0; row
< height
; row
++) {
881 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
882 GLfloat rgba
[4 * MAX_WIDTH
];
883 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
884 height
, format
, type
, row
, 0);
886 if (ST_DEBUG
& DEBUG_FALLBACK
)
887 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
889 /* get float[4] rgba row from surface */
890 pipe_get_tile_rgba_format(pipe
, tex_xfer
, 0, row
, width
, 1,
893 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
894 type
, dest
, &ctx
->Pack
, transferOps
);
898 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
900 pipe
->transfer_destroy(pipe
, tex_xfer
);
902 /* destroy the temp / dest surface */
903 util_destroy_rgba_surface(dst_texture
, dst_surface
);
909 * Called via ctx->Driver.GetTexImage()
912 st_GetTexImage(struct gl_context
* ctx
,
913 GLenum format
, GLenum type
, GLvoid
* pixels
,
914 struct gl_texture_image
*texImage
)
916 struct st_texture_image
*stImage
= st_texture_image(texImage
);
918 if (stImage
->pt
&& util_format_is_s3tc(stImage
->pt
->format
)) {
919 /* Need to decompress the texture.
920 * We'll do this by rendering a textured quad (which is hopefully
921 * faster than using the fallback code in texcompress.c).
922 * Note that we only expect RGBA formats (no Z/depth formats).
924 decompress_with_blit(ctx
, format
, type
, pixels
, texImage
);
927 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
933 st_TexSubimage(struct gl_context
*ctx
, GLint dims
, GLenum target
, GLint level
,
934 GLint xoffset
, GLint yoffset
, GLint zoffset
,
935 GLint width
, GLint height
, GLint depth
,
936 GLenum format
, GLenum type
, const void *pixels
,
937 const struct gl_pixelstore_attrib
*packing
,
938 struct gl_texture_object
*texObj
,
939 struct gl_texture_image
*texImage
)
941 struct st_context
*st
= st_context(ctx
);
942 struct st_texture_image
*stImage
= st_texture_image(texImage
);
944 const GLuint srcImageStride
=
945 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
948 /* init to silence warning only: */
949 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
952 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
953 _mesa_lookup_enum_by_nr(target
),
954 level
, xoffset
, yoffset
, width
, height
);
957 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
958 type
, pixels
, packing
, "glTexSubImage2D");
962 /* for a 1D array upload the image as a series of layer with height = 1 */
963 if (target
== GL_TEXTURE_1D_ARRAY
) {
968 /* Map buffer if necessary. Need to lock to prevent other contexts
969 * from uploading the buffer under us.
972 if (format
== GL_DEPTH_COMPONENT
&&
973 util_format_is_depth_and_stencil(stImage
->pt
->format
))
974 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
976 transfer_usage
= PIPE_TRANSFER_WRITE
;
978 dstMap
= st_texture_image_map(st
, stImage
, zoffset
,
985 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
989 src
= (const GLubyte
*) pixels
;
990 dstRowStride
= stImage
->transfer
->stride
;
992 for (i
= 0; i
< depth
; i
++) {
993 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
997 (GLubyte
**) &dstMap
,
999 format
, type
, src
, packing
)) {
1000 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1003 if (stImage
->pt
&& i
+ 1 < depth
) {
1004 /* unmap this slice */
1005 st_texture_image_unmap(st
, stImage
);
1006 /* map next slice of 3D texture */
1007 dstMap
= st_texture_image_map(st
, stImage
,
1012 src
+= srcImageStride
;
1017 _mesa_unmap_teximage_pbo(ctx
, packing
);
1019 if (stImage
->pt
&& stImage
->transfer
) {
1020 st_texture_image_unmap(st
, stImage
);
1027 st_TexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1028 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1029 GLsizei width
, GLsizei height
, GLsizei depth
,
1030 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1031 const struct gl_pixelstore_attrib
*packing
,
1032 struct gl_texture_object
*texObj
,
1033 struct gl_texture_image
*texImage
)
1035 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1036 width
, height
, depth
, format
, type
,
1037 pixels
, packing
, texObj
, texImage
);
1042 st_TexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1043 GLint xoffset
, GLint yoffset
,
1044 GLsizei width
, GLsizei height
,
1045 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1046 const struct gl_pixelstore_attrib
*packing
,
1047 struct gl_texture_object
*texObj
,
1048 struct gl_texture_image
*texImage
)
1050 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1051 width
, height
, 1, format
, type
,
1052 pixels
, packing
, texObj
, texImage
);
1057 st_TexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1058 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1059 const GLvoid
* pixels
,
1060 const struct gl_pixelstore_attrib
*packing
,
1061 struct gl_texture_object
*texObj
,
1062 struct gl_texture_image
*texImage
)
1064 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1065 format
, type
, pixels
, packing
, texObj
, texImage
);
1070 st_CompressedTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1071 GLint xoffset
, GLsizei width
,
1073 GLsizei imageSize
, const GLvoid
*data
,
1074 struct gl_texture_object
*texObj
,
1075 struct gl_texture_image
*texImage
)
1082 st_CompressedTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1083 GLint xoffset
, GLint yoffset
,
1084 GLsizei width
, GLint height
,
1086 GLsizei imageSize
, const GLvoid
*data
,
1087 struct gl_texture_object
*texObj
,
1088 struct gl_texture_image
*texImage
)
1090 struct st_context
*st
= st_context(ctx
);
1091 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1095 enum pipe_format pformat
;
1099 pformat
= stImage
->pt
->format
;
1101 dstMap
= st_texture_image_map(st
, stImage
, 0,
1102 PIPE_TRANSFER_WRITE
,
1106 srcBlockStride
= util_format_get_stride(pformat
, width
);
1107 dstBlockStride
= stImage
->transfer
->stride
;
1109 assert(stImage
->pt
);
1110 /* TODO find good values for block and strides */
1111 /* TODO also adjust texImage->data for yoffset/xoffset */
1116 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1120 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1121 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1123 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1124 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1125 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1126 char *dst
= (char *) dstMap
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1127 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1130 if (stImage
->pt
&& stImage
->transfer
) {
1131 st_texture_image_unmap(st
, stImage
);
1137 st_CompressedTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1138 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1139 GLsizei width
, GLint height
, GLint depth
,
1141 GLsizei imageSize
, const GLvoid
*data
,
1142 struct gl_texture_object
*texObj
,
1143 struct gl_texture_image
*texImage
)
1151 * Do a CopyTexSubImage operation using a read transfer from the source,
1152 * a write transfer to the destination and get_tile()/put_tile() to access
1153 * the pixels/texels.
1155 * Note: srcY=0=TOP of renderbuffer
1158 fallback_copy_texsubimage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1159 struct st_renderbuffer
*strb
,
1160 struct st_texture_image
*stImage
,
1162 GLint destX
, GLint destY
, GLint destZ
,
1163 GLint srcX
, GLint srcY
,
1164 GLsizei width
, GLsizei height
)
1166 struct st_context
*st
= st_context(ctx
);
1167 struct pipe_context
*pipe
= st
->pipe
;
1168 struct pipe_transfer
*src_trans
;
1170 enum pipe_transfer_usage transfer_usage
;
1172 if (ST_DEBUG
& DEBUG_FALLBACK
)
1173 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1175 assert(width
<= MAX_WIDTH
);
1177 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1178 srcY
= strb
->Base
.Height
- srcY
- height
;
1181 src_trans
= pipe_get_transfer(pipe
,
1184 strb
->rtt_face
+ strb
->rtt_slice
,
1189 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1190 baseFormat
== GL_DEPTH_STENCIL
) &&
1191 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1192 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1194 transfer_usage
= PIPE_TRANSFER_WRITE
;
1196 /* XXX this used to ignore destZ param */
1197 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
1198 destX
, destY
, width
, height
);
1200 if (baseFormat
== GL_DEPTH_COMPONENT
||
1201 baseFormat
== GL_DEPTH_STENCIL
) {
1202 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1203 ctx
->Pixel
.DepthBias
!= 0.0F
);
1206 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1207 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1216 /* To avoid a large temp memory allocation, do copy row by row */
1217 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1218 uint data
[MAX_WIDTH
];
1219 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1221 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1223 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1229 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1231 if (tempSrc
&& texDest
) {
1232 const GLint dims
= 2;
1233 const GLint dstRowStride
= stImage
->transfer
->stride
;
1234 struct gl_texture_image
*texImage
= &stImage
->base
;
1235 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1237 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1238 unpack
.Invert
= GL_TRUE
;
1241 /* get float/RGBA image from framebuffer */
1242 /* XXX this usually involves a lot of int/float conversion.
1243 * try to avoid that someday.
1245 pipe_get_tile_rgba_format(pipe
, src_trans
, 0, 0, width
, height
,
1246 util_format_linear(strb
->texture
->format
),
1249 /* Store into texture memory.
1250 * Note that this does some special things such as pixel transfer
1251 * ops and format conversion. In particular, if the dest tex format
1252 * is actually RGBA but the user created the texture as GL_RGB we
1253 * need to fill-in/override the alpha channel with 1.0.
1255 _mesa_texstore(ctx
, dims
,
1256 texImage
->_BaseFormat
,
1257 texImage
->TexFormat
,
1260 (GLubyte
**) &texDest
,
1262 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1266 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1273 st_texture_image_unmap(st
, stImage
);
1274 pipe
->transfer_destroy(pipe
, src_trans
);
1280 * If the format of the src renderbuffer and the format of the dest
1281 * texture are compatible (in terms of blitting), return a TGSI writemask
1282 * to be used during the blit.
1283 * If the src/dest are incompatible, return 0.
1286 compatible_src_dst_formats(struct gl_context
*ctx
,
1287 const struct gl_renderbuffer
*src
,
1288 const struct gl_texture_image
*dst
)
1290 /* Get logical base formats for the src and dest.
1291 * That is, use the user-requested formats and not the actual, device-
1293 * For example, the user may have requested an A8 texture but the
1294 * driver may actually be using an RGBA texture format. When we
1295 * copy/blit to that texture, we only want to copy the Alpha channel
1296 * and not the RGB channels.
1298 * Similarly, when the src FBO was created an RGB format may have been
1299 * requested but the driver actually chose an RGBA format. In that case,
1300 * we don't want to copy the undefined Alpha channel to the dest texture
1301 * (it should be 1.0).
1303 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1304 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1307 * XXX when we have red-only and red/green renderbuffers we'll need
1308 * to add more cases here (or implement a general-purpose routine that
1309 * queries the existance of the R,G,B,A channels in the src and dest).
1311 if (srcFormat
== dstFormat
) {
1312 /* This is the same as matching_base_formats, which should
1313 * always pass, as it did previously.
1315 return TGSI_WRITEMASK_XYZW
;
1317 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1318 /* Make sure that A in the dest is 1. The actual src format
1319 * may be RGBA and have undefined A values.
1321 return TGSI_WRITEMASK_XYZ
;
1323 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1324 /* Make sure that A in the dest is 1. The actual dst format
1325 * may be RGBA and will need A=1 to provide proper alpha values
1326 * when sampled later.
1328 return TGSI_WRITEMASK_XYZ
;
1331 if (ST_DEBUG
& DEBUG_FALLBACK
)
1332 debug_printf("%s failed for src %s, dst %s\n",
1334 _mesa_lookup_enum_by_nr(srcFormat
),
1335 _mesa_lookup_enum_by_nr(dstFormat
));
1346 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1347 * Note that the region to copy has already been clipped so we know we
1348 * won't read from outside the source renderbuffer's bounds.
1350 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1353 st_copy_texsubimage(struct gl_context
*ctx
,
1354 GLenum target
, GLint level
,
1355 GLint destX
, GLint destY
, GLint destZ
,
1356 GLint srcX
, GLint srcY
,
1357 GLsizei width
, GLsizei height
)
1359 struct gl_texture_unit
*texUnit
=
1360 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1361 struct gl_texture_object
*texObj
=
1362 _mesa_select_tex_object(ctx
, texUnit
, target
);
1363 struct gl_texture_image
*texImage
=
1364 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1365 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1366 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1367 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1368 struct st_renderbuffer
*strb
;
1369 struct st_context
*st
= st_context(ctx
);
1370 struct pipe_context
*pipe
= st
->pipe
;
1371 struct pipe_screen
*screen
= pipe
->screen
;
1372 enum pipe_format dest_format
, src_format
;
1373 GLboolean matching_base_formats
;
1374 GLuint format_writemask
, sample_count
;
1375 struct pipe_surface
*dest_surface
= NULL
;
1376 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1377 struct pipe_surface surf_tmpl
;
1378 unsigned int dst_usage
;
1381 /* make sure finalize_textures has been called?
1383 if (0) st_validate_state(st
);
1385 /* determine if copying depth or color data */
1386 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1387 texBaseFormat
== GL_DEPTH_STENCIL
) {
1388 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1389 if (strb
->Base
.Wrapped
) {
1390 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
1394 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1395 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1398 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1399 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1403 sample_count
= strb
->surface
->texture
->nr_samples
;
1404 /* I believe this would be legal, presumably would need to do a resolve
1405 for color, and for depth/stencil spec says to just use one of the
1406 depth/stencil samples per pixel? Need some transfer clarifications. */
1407 assert(sample_count
< 2);
1410 assert(strb
->surface
);
1411 assert(stImage
->pt
);
1413 src_format
= strb
->surface
->format
;
1414 dest_format
= stImage
->pt
->format
;
1417 * Determine if the src framebuffer and dest texture have the same
1418 * base format. We need this to detect a case such as the framebuffer
1419 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1420 * texture format stores RGBA we need to set A=1 (overriding the
1421 * framebuffer's alpha values). We can't do that with the blit or
1422 * textured-quad paths.
1424 matching_base_formats
=
1425 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1426 _mesa_get_format_base_format(texImage
->TexFormat
));
1428 if (ctx
->_ImageTransferState
) {
1432 if (matching_base_formats
&&
1433 src_format
== dest_format
&&
1435 /* use surface_copy() / blit */
1436 struct pipe_box src_box
;
1437 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
1438 width
, height
, &src_box
);
1440 /* for resource_copy_region(), y=0=top, always */
1441 pipe
->resource_copy_region(pipe
,
1444 stImage
->base
.Level
,
1445 destX
, destY
, destZ
+ stImage
->base
.Face
,
1448 strb
->surface
->u
.tex
.level
,
1453 if (texBaseFormat
== GL_DEPTH_STENCIL
) {
1457 if (texBaseFormat
== GL_DEPTH_COMPONENT
) {
1458 format_writemask
= TGSI_WRITEMASK_XYZW
;
1459 dst_usage
= PIPE_BIND_DEPTH_STENCIL
;
1462 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1463 dst_usage
= PIPE_BIND_RENDER_TARGET
;
1466 if (!format_writemask
||
1467 !screen
->is_format_supported(screen
, src_format
,
1468 PIPE_TEXTURE_2D
, sample_count
,
1469 PIPE_BIND_SAMPLER_VIEW
) ||
1470 !screen
->is_format_supported(screen
, dest_format
,
1477 srcY1
= strb
->Base
.Height
- srcY
- height
;
1478 srcY0
= srcY1
+ height
;
1482 srcY1
= srcY0
+ height
;
1485 /* Disable conditional rendering. */
1486 if (st
->render_condition
) {
1487 pipe
->render_condition(pipe
, NULL
, 0);
1490 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1491 surf_tmpl
.format
= util_format_linear(stImage
->pt
->format
);
1492 surf_tmpl
.usage
= dst_usage
;
1493 surf_tmpl
.u
.tex
.level
= stImage
->base
.Level
;
1494 surf_tmpl
.u
.tex
.first_layer
= stImage
->base
.Face
+ destZ
;
1495 surf_tmpl
.u
.tex
.last_layer
= stImage
->base
.Face
+ destZ
;
1497 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1499 util_blit_pixels_writemask(st
->blit
,
1501 strb
->surface
->u
.tex
.level
,
1503 srcX
+ width
, srcY1
,
1504 strb
->surface
->u
.tex
.first_layer
,
1507 destX
+ width
, destY
+ height
,
1508 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1510 pipe_surface_reference(&dest_surface
, NULL
);
1512 /* Restore conditional rendering state. */
1513 if (st
->render_condition
) {
1514 pipe
->render_condition(pipe
, st
->render_condition
,
1515 st
->condition_mode
);
1521 /* software fallback */
1522 fallback_copy_texsubimage(ctx
, target
, level
,
1523 strb
, stImage
, texBaseFormat
,
1524 destX
, destY
, destZ
,
1525 srcX
, srcY
, width
, height
);
1531 st_CopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1532 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1534 const GLint yoffset
= 0, zoffset
= 0;
1535 const GLsizei height
= 1;
1536 st_copy_texsubimage(ctx
, target
, level
,
1537 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1538 x
, y
, width
, height
); /* src X, Y, size */
1543 st_CopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1544 GLint xoffset
, GLint yoffset
,
1545 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1547 const GLint zoffset
= 0;
1548 st_copy_texsubimage(ctx
, target
, level
,
1549 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1550 x
, y
, width
, height
); /* src X, Y, size */
1555 st_CopyTexSubImage3D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1556 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1557 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1559 st_copy_texsubimage(ctx
, target
, level
,
1560 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1561 x
, y
, width
, height
); /* src X, Y, size */
1566 * Copy image data from stImage into the texture object 'stObj' at level
1570 copy_image_data_to_texture(struct st_context
*st
,
1571 struct st_texture_object
*stObj
,
1573 struct st_texture_image
*stImage
)
1577 const struct gl_texture_image
*dstImage
=
1578 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
1580 assert(dstImage
->Width
== stImage
->base
.Width
);
1581 assert(dstImage
->Height
== stImage
->base
.Height
);
1582 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1586 /* Copy potentially with the blitter:
1588 st_texture_image_copy(st
->pipe
,
1589 stObj
->pt
, dstLevel
, /* dest texture, level */
1590 stImage
->pt
, stImage
->base
.Level
, /* src texture, level */
1591 stImage
->base
.Face
);
1593 pipe_resource_reference(&stImage
->pt
, NULL
);
1595 else if (stImage
->TexData
) {
1596 /* Copy from malloc'd memory */
1597 /* XXX this should be re-examined/tested with a compressed format */
1598 GLuint blockSize
= util_format_get_blocksize(stObj
->pt
->format
);
1599 GLuint srcRowStride
= stImage
->base
.Width
* blockSize
;
1600 GLuint srcSliceStride
= stImage
->base
.Height
* srcRowStride
;
1601 st_texture_image_data(st
,
1608 _mesa_align_free(stImage
->TexData
);
1609 stImage
->TexData
= NULL
;
1612 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1617 * Called during state validation. When this function is finished,
1618 * the texture object should be ready for rendering.
1619 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1622 st_finalize_texture(struct gl_context
*ctx
,
1623 struct pipe_context
*pipe
,
1624 struct gl_texture_object
*tObj
)
1626 struct st_context
*st
= st_context(ctx
);
1627 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1628 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1630 struct st_texture_image
*firstImage
;
1631 enum pipe_format firstImageFormat
;
1632 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1634 if (stObj
->base
._Complete
) {
1635 /* The texture is complete and we know exactly how many mipmap levels
1636 * are present/needed. This is conditional because we may be called
1637 * from the st_generate_mipmap() function when the texture object is
1638 * incomplete. In that case, we'll have set stObj->lastLevel before
1641 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1642 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1643 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1645 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1648 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1651 /* If both firstImage and stObj point to a texture which can contain
1652 * all active images, favour firstImage. Note that because of the
1653 * completeness requirement, we know that the image dimensions
1656 if (firstImage
->pt
&&
1657 firstImage
->pt
!= stObj
->pt
&&
1658 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1659 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1660 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1663 /* Find gallium format for the Mesa texture */
1664 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1666 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1668 GLuint width
, height
, depth
;
1669 if (!guess_base_level_size(stObj
->base
.Target
,
1670 firstImage
->base
.Width2
,
1671 firstImage
->base
.Height2
,
1672 firstImage
->base
.Depth2
,
1673 firstImage
->base
.Level
,
1674 &width
, &height
, &depth
)) {
1675 width
= stObj
->width0
;
1676 height
= stObj
->height0
;
1677 depth
= stObj
->depth0
;
1679 /* convert GL dims to Gallium dims */
1680 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1681 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1684 /* If we already have a gallium texture, check that it matches the texture
1685 * object's format, target, size, num_levels, etc.
1688 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1689 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1690 stObj
->pt
->last_level
< stObj
->lastLevel
||
1691 stObj
->pt
->width0
!= ptWidth
||
1692 stObj
->pt
->height0
!= ptHeight
||
1693 stObj
->pt
->depth0
!= ptDepth
||
1694 stObj
->pt
->array_size
!= ptLayers
)
1696 /* The gallium texture does not match the Mesa texture so delete the
1697 * gallium texture now. We'll make a new one below.
1699 pipe_resource_reference(&stObj
->pt
, NULL
);
1700 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1701 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1705 /* May need to create a new gallium texture:
1708 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1710 stObj
->pt
= st_texture_create(st
,
1711 gl_target_to_pipe(stObj
->base
.Target
),
1721 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1726 /* Pull in any images not in the object's texture:
1728 for (face
= 0; face
< nr_faces
; face
++) {
1730 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1731 struct st_texture_image
*stImage
=
1732 st_texture_image(stObj
->base
.Image
[face
][level
]);
1734 /* Need to import images in main memory or held in other textures.
1736 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1738 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1739 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1740 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
1741 /* src image fits expected dest mipmap level size */
1742 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1753 * Returns pointer to a default/dummy texture.
1754 * This is typically used when the current shader has tex/sample instructions
1755 * but the user has not provided a (any) texture(s).
1757 struct gl_texture_object
*
1758 st_get_default_texture(struct st_context
*st
)
1760 if (!st
->default_texture
) {
1761 static const GLenum target
= GL_TEXTURE_2D
;
1762 GLubyte pixels
[16][16][4];
1763 struct gl_texture_object
*texObj
;
1764 struct gl_texture_image
*texImg
;
1767 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1768 * when attempting to sample incomplete textures.
1770 for (i
= 0; i
< 16; i
++) {
1771 for (j
= 0; j
< 16; j
++) {
1772 pixels
[i
][j
][0] = 0;
1773 pixels
[i
][j
][1] = 0;
1774 pixels
[i
][j
][2] = 0;
1775 pixels
[i
][j
][3] = 255;
1779 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1781 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1783 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1784 16, 16, 1, 0, /* w, h, d, border */
1785 GL_RGBA
, MESA_FORMAT_RGBA8888
);
1787 st_TexImage(st
->ctx
, 2, target
,
1788 0, GL_RGBA
, /* level, intformat */
1789 16, 16, 1, 0, /* w, h, d, border */
1790 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1791 &st
->ctx
->DefaultPacking
,
1795 texObj
->Sampler
.MinFilter
= GL_NEAREST
;
1796 texObj
->Sampler
.MagFilter
= GL_NEAREST
;
1797 texObj
->_Complete
= GL_TRUE
;
1799 st
->default_texture
= texObj
;
1801 return st
->default_texture
;
1806 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1807 * for a whole mipmap stack.
1810 st_AllocTextureStorage(struct gl_context
*ctx
,
1811 struct gl_texture_object
*texObj
,
1812 GLsizei levels
, GLsizei width
,
1813 GLsizei height
, GLsizei depth
)
1815 const GLuint numFaces
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1816 struct st_context
*st
= st_context(ctx
);
1817 struct st_texture_object
*stObj
= st_texture_object(texObj
);
1818 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
1819 enum pipe_format fmt
;
1824 /* Save the level=0 dimensions */
1825 stObj
->width0
= width
;
1826 stObj
->height0
= height
;
1827 stObj
->depth0
= depth
;
1828 stObj
->lastLevel
= levels
- 1;
1830 fmt
= st_mesa_format_to_pipe_format(texObj
->Image
[0][0]->TexFormat
);
1832 bindings
= default_bindings(st
, fmt
);
1834 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
1835 width
, height
, depth
,
1836 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1838 stObj
->pt
= st_texture_create(st
,
1839 gl_target_to_pipe(texObj
->Target
),
1850 /* Set image resource pointers */
1851 for (level
= 0; level
< levels
; level
++) {
1853 for (face
= 0; face
< numFaces
; face
++) {
1854 struct st_texture_image
*stImage
=
1855 st_texture_image(texObj
->Image
[face
][level
]);
1856 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1866 st_init_texture_functions(struct dd_function_table
*functions
)
1868 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1869 functions
->TexImage1D
= st_TexImage1D
;
1870 functions
->TexImage2D
= st_TexImage2D
;
1871 functions
->TexImage3D
= st_TexImage3D
;
1872 functions
->TexSubImage1D
= st_TexSubImage1D
;
1873 functions
->TexSubImage2D
= st_TexSubImage2D
;
1874 functions
->TexSubImage3D
= st_TexSubImage3D
;
1875 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1876 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1877 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1878 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1879 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1880 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1881 functions
->GenerateMipmap
= st_generate_mipmap
;
1883 functions
->GetTexImage
= st_GetTexImage
;
1885 /* compressed texture functions */
1886 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1887 functions
->GetCompressedTexImage
= _mesa_get_compressed_teximage
;
1889 functions
->NewTextureObject
= st_NewTextureObject
;
1890 functions
->NewTextureImage
= st_NewTextureImage
;
1891 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
1892 functions
->DeleteTexture
= st_DeleteTextureObject
;
1893 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
1894 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
1895 functions
->MapTextureImage
= st_MapTextureImage
;
1896 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
1898 /* XXX Temporary until we can query pipe's texture sizes */
1899 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;
1901 functions
->AllocTextureStorage
= st_AllocTextureStorage
;