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
;
500 * Preparation prior to glTexImage. Basically check the 'surface_based'
501 * field and switch to a "normal" tex image if necessary.
504 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
505 GLint internalFormat
,
506 GLint width
, GLint height
, GLint depth
, GLint border
,
507 GLenum format
, GLenum type
)
509 struct gl_texture_object
*texObj
= texImage
->TexObject
;
510 struct st_texture_object
*stObj
= st_texture_object(texObj
);
512 /* switch to "normal" */
513 if (stObj
->surface_based
) {
514 const GLenum target
= texObj
->Target
;
515 const GLuint level
= texImage
->Level
;
518 _mesa_clear_texture_object(ctx
, texObj
);
519 pipe_resource_reference(&stObj
->pt
, NULL
);
521 /* oops, need to init this image again */
522 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
523 internalFormat
, format
, type
);
525 _mesa_init_teximage_fields(ctx
, texImage
,
526 width
, height
, depth
, border
,
527 internalFormat
, texFormat
);
529 stObj
->surface_based
= GL_FALSE
;
535 * Do glTexImage1/2/3D().
538 st_TexImage(struct gl_context
* ctx
,
540 struct gl_texture_image
*texImage
,
541 GLint internalFormat
,
542 GLint width
, GLint height
, GLint depth
,
544 GLenum format
, GLenum type
, const void *pixels
,
545 const struct gl_pixelstore_attrib
*unpack
,
546 GLsizei imageSize
, GLboolean compressed_src
)
548 struct st_context
*st
= st_context(ctx
);
549 struct gl_texture_object
*texObj
= texImage
->TexObject
;
550 struct st_texture_object
*stObj
= st_texture_object(texObj
);
551 struct st_texture_image
*stImage
= st_texture_image(texImage
);
552 const GLenum target
= texObj
->Target
;
553 const GLuint level
= texImage
->Level
;
554 GLuint dstRowStride
= 0;
555 enum pipe_transfer_usage transfer_usage
= 0;
558 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
559 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
561 prep_teximage(ctx
, texImage
, internalFormat
, width
, height
, depth
, border
,
564 assert(texImage
->Width
== width
);
565 assert(texImage
->Height
== height
);
566 assert(texImage
->Depth
== depth
);
568 /* Release the reference to a potentially orphaned buffer.
569 * Release any old malloced memory.
572 pipe_resource_reference(&stImage
->pt
, NULL
);
573 assert(!stImage
->TexData
);
575 else if (stImage
->TexData
) {
576 _mesa_align_free(stImage
->TexData
);
580 * See if the new image is somehow incompatible with the existing
581 * mipmap. If so, free the old mipmap.
584 if (level
> (GLint
) stObj
->pt
->last_level
||
585 !st_texture_match_image(stObj
->pt
, &stImage
->base
)) {
587 pipe_resource_reference(&stObj
->pt
, NULL
);
589 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
593 if (width
== 0 || height
== 0 || depth
== 0) {
594 /* stop after freeing old image */
599 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
600 /* Probably out of memory.
601 * Try flushing any pending rendering, then retry.
604 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
605 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
611 assert(!stImage
->pt
);
613 /* Check if this texture image can live inside the texture object's buffer.
614 * If so, store the image there. Otherwise the image will temporarily live
618 st_texture_match_image(stObj
->pt
, &stImage
->base
)) {
620 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
625 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
627 /* Pixel data may come from regular user memory or a PBO. For the later,
628 * do bounds checking and map the PBO to read pixels data from it.
630 * XXX we should try to use a GPU-accelerated path to copy the image data
631 * from the PBO to the texture.
633 if (compressed_src
) {
634 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
636 "glCompressedTexImage");
639 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
641 pixels
, unpack
, "glTexImage");
644 /* for a 1D array upload the image as a series of layer with height = 1 */
645 if (target
== GL_TEXTURE_1D_ARRAY
) {
651 * Prepare to store the texture data. Either map the gallium texture buffer
652 * memory or malloc space for it.
656 /* We've allocated texture resource, but have no pixel data - all done. */
660 /* Store the image in the gallium transfer object */
661 if (format
== GL_DEPTH_COMPONENT
&&
662 util_format_is_depth_and_stencil(stImage
->pt
->format
))
663 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
665 transfer_usage
= PIPE_TRANSFER_WRITE
;
667 dstMap
= st_texture_image_map(st
, stImage
, 0,
668 transfer_usage
, 0, 0, width
, height
);
669 if(stImage
->transfer
)
670 dstRowStride
= stImage
->transfer
->stride
;
673 /* Allocate regular memory and store the image there temporarily. */
674 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
675 width
, height
, depth
);
676 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
678 stImage
->TexData
= _mesa_align_malloc(imageSize
, 16);
679 dstMap
= stImage
->TexData
;
683 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
688 /* We've allocated texture memory, but have no pixel data - all done. */
692 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
693 width
, height
, depth
, width
, dstRowStride
);
695 /* Copy user texture image into the mapped texture buffer.
697 if (compressed_src
) {
698 const GLuint srcRowStride
=
699 _mesa_format_row_stride(texImage
->TexFormat
, width
);
700 if (dstRowStride
== srcRowStride
) {
701 memcpy(dstMap
, pixels
, imageSize
);
704 GLubyte
*dst
= dstMap
;
705 const char *src
= pixels
;
706 GLuint i
, bw
, bh
, lines
;
707 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
708 lines
= (height
+ bh
- 1) / bh
;
710 for (i
= 0; i
< lines
; ++i
) {
711 memcpy(dst
, src
, srcRowStride
);
718 const GLuint srcImageStride
=
719 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
721 const GLubyte
*src
= (const GLubyte
*) pixels
;
723 for (i
= 0; i
< depth
; i
++) {
724 if (!_mesa_texstore(ctx
, dims
,
725 texImage
->_BaseFormat
,
728 (GLubyte
**) &dstMap
, /* dstSlice */
730 format
, type
, src
, unpack
)) {
731 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
734 if (stImage
->pt
&& i
+ 1 < depth
) {
735 /* unmap this slice */
736 st_texture_image_unmap(st
, stImage
);
737 /* map next slice of 3D texture */
738 dstMap
= st_texture_image_map(st
, stImage
, i
+ 1,
739 transfer_usage
, 0, 0,
741 src
+= srcImageStride
;
747 _mesa_unmap_teximage_pbo(ctx
, unpack
);
749 if (stImage
->pt
&& stImage
->transfer
) {
750 st_texture_image_unmap(st
, stImage
);
756 st_TexImage3D(struct gl_context
* ctx
,
757 struct gl_texture_image
*texImage
,
758 GLint internalFormat
,
759 GLint width
, GLint height
, GLint depth
,
761 GLenum format
, GLenum type
, const void *pixels
,
762 const struct gl_pixelstore_attrib
*unpack
)
764 st_TexImage(ctx
, 3, texImage
, internalFormat
, width
, height
, depth
, border
,
765 format
, type
, pixels
, unpack
, 0, GL_FALSE
);
770 st_TexImage2D(struct gl_context
* ctx
,
771 struct gl_texture_image
*texImage
,
772 GLint internalFormat
,
773 GLint width
, GLint height
, GLint border
,
774 GLenum format
, GLenum type
, const void *pixels
,
775 const struct gl_pixelstore_attrib
*unpack
)
777 st_TexImage(ctx
, 2, texImage
, internalFormat
, width
, height
, 1, border
,
778 format
, type
, pixels
, unpack
, 0, GL_FALSE
);
783 st_TexImage1D(struct gl_context
* ctx
,
784 struct gl_texture_image
*texImage
,
785 GLint internalFormat
,
786 GLint width
, GLint border
,
787 GLenum format
, GLenum type
, const void *pixels
,
788 const struct gl_pixelstore_attrib
*unpack
)
790 st_TexImage(ctx
, 1, texImage
, internalFormat
, width
, 1, 1, border
,
791 format
, type
, pixels
, unpack
, 0, GL_FALSE
);
796 st_CompressedTexImage2D(struct gl_context
*ctx
,
797 struct gl_texture_image
*texImage
,
798 GLint internalFormat
,
799 GLint width
, GLint height
, GLint border
,
800 GLsizei imageSize
, const GLvoid
*data
)
802 st_TexImage(ctx
, 2, texImage
, internalFormat
, width
, height
, 1, border
,
803 0, 0, data
, &ctx
->Unpack
, imageSize
, GL_TRUE
);
809 * glGetTexImage() helper: decompress a compressed texture by rendering
810 * a textured quad. Store the results in the user's buffer.
813 decompress_with_blit(struct gl_context
* ctx
,
814 GLenum format
, GLenum type
, GLvoid
*pixels
,
815 struct gl_texture_image
*texImage
)
817 struct st_context
*st
= st_context(ctx
);
818 struct pipe_context
*pipe
= st
->pipe
;
819 struct st_texture_image
*stImage
= st_texture_image(texImage
);
820 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
821 struct pipe_sampler_view
*src_view
=
822 st_get_texture_sampler_view(stObj
, pipe
);
823 const GLuint width
= texImage
->Width
;
824 const GLuint height
= texImage
->Height
;
825 struct pipe_surface
*dst_surface
;
826 struct pipe_resource
*dst_texture
;
827 struct pipe_transfer
*tex_xfer
;
828 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
829 PIPE_BIND_TRANSFER_READ
);
831 /* create temp / dest surface */
832 if (!util_create_rgba_surface(pipe
, width
, height
, bind
,
833 &dst_texture
, &dst_surface
)) {
834 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
835 "in decompress_with_blit()");
839 /* Disable conditional rendering. */
840 if (st
->render_condition
) {
841 pipe
->render_condition(pipe
, NULL
, 0);
844 /* Choose the source mipmap level */
845 src_view
->u
.tex
.first_level
= src_view
->u
.tex
.last_level
= texImage
->Level
;
847 /* blit/render/decompress */
848 util_blit_pixels_tex(st
->blit
,
849 src_view
, /* pipe_resource (src) */
850 0, 0, /* src x0, y0 */
851 width
, height
, /* src x1, y1 */
852 dst_surface
, /* pipe_surface (dst) */
853 0, 0, /* dst x0, y0 */
854 width
, height
, /* dst x1, y1 */
856 PIPE_TEX_MIPFILTER_NEAREST
);
858 /* Restore conditional rendering state. */
859 if (st
->render_condition
) {
860 pipe
->render_condition(pipe
, st
->render_condition
,
864 /* map the dst_surface so we can read from it */
865 tex_xfer
= pipe_get_transfer(pipe
,
868 0, 0, width
, height
);
870 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
872 /* copy/pack data into user buffer */
873 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
875 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
876 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
878 for (row
= 0; row
< height
; row
++) {
879 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
880 height
, format
, type
, row
, 0);
881 memcpy(dest
, map
, bytesPerRow
);
882 map
+= tex_xfer
->stride
;
884 pipe_transfer_unmap(pipe
, tex_xfer
);
887 /* format translation via floats */
889 enum pipe_format pformat
= util_format_linear(dst_texture
->format
);
890 for (row
= 0; row
< height
; row
++) {
891 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
892 GLfloat rgba
[4 * MAX_WIDTH
];
893 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
894 height
, format
, type
, row
, 0);
896 if (ST_DEBUG
& DEBUG_FALLBACK
)
897 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
899 /* get float[4] rgba row from surface */
900 pipe_get_tile_rgba_format(pipe
, tex_xfer
, 0, row
, width
, 1,
903 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
904 type
, dest
, &ctx
->Pack
, transferOps
);
908 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
910 pipe
->transfer_destroy(pipe
, tex_xfer
);
912 /* destroy the temp / dest surface */
913 util_destroy_rgba_surface(dst_texture
, dst_surface
);
919 * Called via ctx->Driver.GetTexImage()
922 st_GetTexImage(struct gl_context
* ctx
,
923 GLenum format
, GLenum type
, GLvoid
* pixels
,
924 struct gl_texture_image
*texImage
)
926 struct st_texture_image
*stImage
= st_texture_image(texImage
);
928 if (stImage
->pt
&& util_format_is_s3tc(stImage
->pt
->format
)) {
929 /* Need to decompress the texture.
930 * We'll do this by rendering a textured quad (which is hopefully
931 * faster than using the fallback code in texcompress.c).
932 * Note that we only expect RGBA formats (no Z/depth formats).
934 decompress_with_blit(ctx
, format
, type
, pixels
, texImage
);
937 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
943 st_CompressedTexSubImage1D(struct gl_context
*ctx
,
944 struct gl_texture_image
*texImage
,
945 GLint xoffset
, GLsizei width
,
947 GLsizei imageSize
, const GLvoid
*data
)
954 st_CompressedTexSubImage2D(struct gl_context
*ctx
,
955 struct gl_texture_image
*texImage
,
956 GLint xoffset
, GLint yoffset
,
957 GLsizei width
, GLint height
,
959 GLsizei imageSize
, const GLvoid
*data
)
961 struct st_context
*st
= st_context(ctx
);
962 struct st_texture_image
*stImage
= st_texture_image(texImage
);
966 enum pipe_format pformat
;
970 pformat
= stImage
->pt
->format
;
972 dstMap
= st_texture_image_map(st
, stImage
, 0,
977 srcBlockStride
= util_format_get_stride(pformat
, width
);
978 dstBlockStride
= stImage
->transfer
->stride
;
981 /* TODO find good values for block and strides */
982 /* TODO also adjust texImage->data for yoffset/xoffset */
987 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
991 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
992 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
994 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
995 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
996 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
997 char *dst
= (char *) dstMap
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
998 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1001 if (stImage
->pt
&& stImage
->transfer
) {
1002 st_texture_image_unmap(st
, stImage
);
1008 st_CompressedTexSubImage3D(struct gl_context
*ctx
,
1009 struct gl_texture_image
*texImage
,
1010 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1011 GLsizei width
, GLint height
, GLint depth
,
1013 GLsizei imageSize
, const GLvoid
*data
)
1021 * Do a CopyTexSubImage operation using a read transfer from the source,
1022 * a write transfer to the destination and get_tile()/put_tile() to access
1023 * the pixels/texels.
1025 * Note: srcY=0=TOP of renderbuffer
1028 fallback_copy_texsubimage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1029 struct st_renderbuffer
*strb
,
1030 struct st_texture_image
*stImage
,
1032 GLint destX
, GLint destY
, GLint destZ
,
1033 GLint srcX
, GLint srcY
,
1034 GLsizei width
, GLsizei height
)
1036 struct st_context
*st
= st_context(ctx
);
1037 struct pipe_context
*pipe
= st
->pipe
;
1038 struct pipe_transfer
*src_trans
;
1040 enum pipe_transfer_usage transfer_usage
;
1042 if (ST_DEBUG
& DEBUG_FALLBACK
)
1043 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1045 assert(width
<= MAX_WIDTH
);
1047 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1048 srcY
= strb
->Base
.Height
- srcY
- height
;
1051 src_trans
= pipe_get_transfer(pipe
,
1054 strb
->rtt_face
+ strb
->rtt_slice
,
1059 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1060 baseFormat
== GL_DEPTH_STENCIL
) &&
1061 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1062 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1064 transfer_usage
= PIPE_TRANSFER_WRITE
;
1066 /* XXX this used to ignore destZ param */
1067 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
1068 destX
, destY
, width
, height
);
1070 if (baseFormat
== GL_DEPTH_COMPONENT
||
1071 baseFormat
== GL_DEPTH_STENCIL
) {
1072 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1073 ctx
->Pixel
.DepthBias
!= 0.0F
);
1076 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1077 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1086 /* To avoid a large temp memory allocation, do copy row by row */
1087 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1088 uint data
[MAX_WIDTH
];
1089 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1091 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1093 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1099 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1101 if (tempSrc
&& texDest
) {
1102 const GLint dims
= 2;
1103 const GLint dstRowStride
= stImage
->transfer
->stride
;
1104 struct gl_texture_image
*texImage
= &stImage
->base
;
1105 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1107 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1108 unpack
.Invert
= GL_TRUE
;
1111 /* get float/RGBA image from framebuffer */
1112 /* XXX this usually involves a lot of int/float conversion.
1113 * try to avoid that someday.
1115 pipe_get_tile_rgba_format(pipe
, src_trans
, 0, 0, width
, height
,
1116 util_format_linear(strb
->texture
->format
),
1119 /* Store into texture memory.
1120 * Note that this does some special things such as pixel transfer
1121 * ops and format conversion. In particular, if the dest tex format
1122 * is actually RGBA but the user created the texture as GL_RGB we
1123 * need to fill-in/override the alpha channel with 1.0.
1125 _mesa_texstore(ctx
, dims
,
1126 texImage
->_BaseFormat
,
1127 texImage
->TexFormat
,
1129 (GLubyte
**) &texDest
,
1131 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1135 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1142 st_texture_image_unmap(st
, stImage
);
1143 pipe
->transfer_destroy(pipe
, src_trans
);
1149 * If the format of the src renderbuffer and the format of the dest
1150 * texture are compatible (in terms of blitting), return a TGSI writemask
1151 * to be used during the blit.
1152 * If the src/dest are incompatible, return 0.
1155 compatible_src_dst_formats(struct gl_context
*ctx
,
1156 const struct gl_renderbuffer
*src
,
1157 const struct gl_texture_image
*dst
)
1159 /* Get logical base formats for the src and dest.
1160 * That is, use the user-requested formats and not the actual, device-
1162 * For example, the user may have requested an A8 texture but the
1163 * driver may actually be using an RGBA texture format. When we
1164 * copy/blit to that texture, we only want to copy the Alpha channel
1165 * and not the RGB channels.
1167 * Similarly, when the src FBO was created an RGB format may have been
1168 * requested but the driver actually chose an RGBA format. In that case,
1169 * we don't want to copy the undefined Alpha channel to the dest texture
1170 * (it should be 1.0).
1172 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1173 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1176 * XXX when we have red-only and red/green renderbuffers we'll need
1177 * to add more cases here (or implement a general-purpose routine that
1178 * queries the existance of the R,G,B,A channels in the src and dest).
1180 if (srcFormat
== dstFormat
) {
1181 /* This is the same as matching_base_formats, which should
1182 * always pass, as it did previously.
1184 return TGSI_WRITEMASK_XYZW
;
1186 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1187 /* Make sure that A in the dest is 1. The actual src format
1188 * may be RGBA and have undefined A values.
1190 return TGSI_WRITEMASK_XYZ
;
1192 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1193 /* Make sure that A in the dest is 1. The actual dst format
1194 * may be RGBA and will need A=1 to provide proper alpha values
1195 * when sampled later.
1197 return TGSI_WRITEMASK_XYZ
;
1200 if (ST_DEBUG
& DEBUG_FALLBACK
)
1201 debug_printf("%s failed for src %s, dst %s\n",
1203 _mesa_lookup_enum_by_nr(srcFormat
),
1204 _mesa_lookup_enum_by_nr(dstFormat
));
1215 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1216 * Note that the region to copy has already been clipped so we know we
1217 * won't read from outside the source renderbuffer's bounds.
1219 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1222 st_copy_texsubimage(struct gl_context
*ctx
,
1223 GLenum target
, GLint level
,
1224 GLint destX
, GLint destY
, GLint destZ
,
1225 GLint srcX
, GLint srcY
,
1226 GLsizei width
, GLsizei height
)
1228 struct gl_texture_unit
*texUnit
=
1229 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1230 struct gl_texture_object
*texObj
=
1231 _mesa_select_tex_object(ctx
, texUnit
, target
);
1232 struct gl_texture_image
*texImage
=
1233 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1234 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1235 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1236 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1237 struct st_renderbuffer
*strb
;
1238 struct st_context
*st
= st_context(ctx
);
1239 struct pipe_context
*pipe
= st
->pipe
;
1240 struct pipe_screen
*screen
= pipe
->screen
;
1241 enum pipe_format dest_format
, src_format
;
1242 GLboolean matching_base_formats
;
1243 GLuint format_writemask
, sample_count
;
1244 struct pipe_surface
*dest_surface
= NULL
;
1245 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1246 struct pipe_surface surf_tmpl
;
1247 unsigned int dst_usage
;
1250 /* make sure finalize_textures has been called?
1252 if (0) st_validate_state(st
);
1254 /* determine if copying depth or color data */
1255 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1256 texBaseFormat
== GL_DEPTH_STENCIL
) {
1257 strb
= st_renderbuffer(fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1260 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1261 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1264 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1265 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1269 sample_count
= strb
->surface
->texture
->nr_samples
;
1270 /* I believe this would be legal, presumably would need to do a resolve
1271 for color, and for depth/stencil spec says to just use one of the
1272 depth/stencil samples per pixel? Need some transfer clarifications. */
1273 assert(sample_count
< 2);
1276 assert(strb
->surface
);
1277 assert(stImage
->pt
);
1279 src_format
= strb
->surface
->format
;
1280 dest_format
= stImage
->pt
->format
;
1283 * Determine if the src framebuffer and dest texture have the same
1284 * base format. We need this to detect a case such as the framebuffer
1285 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1286 * texture format stores RGBA we need to set A=1 (overriding the
1287 * framebuffer's alpha values). We can't do that with the blit or
1288 * textured-quad paths.
1290 matching_base_formats
=
1291 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1292 _mesa_get_format_base_format(texImage
->TexFormat
));
1294 if (ctx
->_ImageTransferState
) {
1298 if (matching_base_formats
&&
1299 src_format
== dest_format
&&
1301 /* use surface_copy() / blit */
1302 struct pipe_box src_box
;
1303 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
1304 width
, height
, &src_box
);
1306 /* for resource_copy_region(), y=0=top, always */
1307 pipe
->resource_copy_region(pipe
,
1310 stImage
->base
.Level
,
1311 destX
, destY
, destZ
+ stImage
->base
.Face
,
1314 strb
->surface
->u
.tex
.level
,
1319 if (texBaseFormat
== GL_DEPTH_STENCIL
) {
1323 if (texBaseFormat
== GL_DEPTH_COMPONENT
) {
1324 format_writemask
= TGSI_WRITEMASK_XYZW
;
1325 dst_usage
= PIPE_BIND_DEPTH_STENCIL
;
1328 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1329 dst_usage
= PIPE_BIND_RENDER_TARGET
;
1332 if (!format_writemask
||
1333 !screen
->is_format_supported(screen
, src_format
,
1334 PIPE_TEXTURE_2D
, sample_count
,
1335 PIPE_BIND_SAMPLER_VIEW
) ||
1336 !screen
->is_format_supported(screen
, dest_format
,
1343 srcY1
= strb
->Base
.Height
- srcY
- height
;
1344 srcY0
= srcY1
+ height
;
1348 srcY1
= srcY0
+ height
;
1351 /* Disable conditional rendering. */
1352 if (st
->render_condition
) {
1353 pipe
->render_condition(pipe
, NULL
, 0);
1356 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1357 surf_tmpl
.format
= util_format_linear(stImage
->pt
->format
);
1358 surf_tmpl
.usage
= dst_usage
;
1359 surf_tmpl
.u
.tex
.level
= stImage
->base
.Level
;
1360 surf_tmpl
.u
.tex
.first_layer
= stImage
->base
.Face
+ destZ
;
1361 surf_tmpl
.u
.tex
.last_layer
= stImage
->base
.Face
+ destZ
;
1363 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1365 util_blit_pixels_writemask(st
->blit
,
1367 strb
->surface
->u
.tex
.level
,
1369 srcX
+ width
, srcY1
,
1370 strb
->surface
->u
.tex
.first_layer
,
1373 destX
+ width
, destY
+ height
,
1374 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1376 pipe_surface_reference(&dest_surface
, NULL
);
1378 /* Restore conditional rendering state. */
1379 if (st
->render_condition
) {
1380 pipe
->render_condition(pipe
, st
->render_condition
,
1381 st
->condition_mode
);
1387 /* software fallback */
1388 fallback_copy_texsubimage(ctx
, target
, level
,
1389 strb
, stImage
, texBaseFormat
,
1390 destX
, destY
, destZ
,
1391 srcX
, srcY
, width
, height
);
1397 st_CopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1398 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1400 const GLint yoffset
= 0, zoffset
= 0;
1401 const GLsizei height
= 1;
1402 st_copy_texsubimage(ctx
, target
, level
,
1403 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1404 x
, y
, width
, height
); /* src X, Y, size */
1409 st_CopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1410 GLint xoffset
, GLint yoffset
,
1411 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1413 const GLint zoffset
= 0;
1414 st_copy_texsubimage(ctx
, target
, level
,
1415 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1416 x
, y
, width
, height
); /* src X, Y, size */
1421 st_CopyTexSubImage3D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1422 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1423 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1425 st_copy_texsubimage(ctx
, target
, level
,
1426 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1427 x
, y
, width
, height
); /* src X, Y, size */
1432 * Copy image data from stImage into the texture object 'stObj' at level
1436 copy_image_data_to_texture(struct st_context
*st
,
1437 struct st_texture_object
*stObj
,
1439 struct st_texture_image
*stImage
)
1443 const struct gl_texture_image
*dstImage
=
1444 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
1446 assert(dstImage
->Width
== stImage
->base
.Width
);
1447 assert(dstImage
->Height
== stImage
->base
.Height
);
1448 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1452 /* Copy potentially with the blitter:
1454 st_texture_image_copy(st
->pipe
,
1455 stObj
->pt
, dstLevel
, /* dest texture, level */
1456 stImage
->pt
, stImage
->base
.Level
, /* src texture, level */
1457 stImage
->base
.Face
);
1459 pipe_resource_reference(&stImage
->pt
, NULL
);
1461 else if (stImage
->TexData
) {
1462 /* Copy from malloc'd memory */
1463 /* XXX this should be re-examined/tested with a compressed format */
1464 GLuint blockSize
= util_format_get_blocksize(stObj
->pt
->format
);
1465 GLuint srcRowStride
= stImage
->base
.Width
* blockSize
;
1466 GLuint srcSliceStride
= stImage
->base
.Height
* srcRowStride
;
1467 st_texture_image_data(st
,
1474 _mesa_align_free(stImage
->TexData
);
1475 stImage
->TexData
= NULL
;
1478 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1483 * Called during state validation. When this function is finished,
1484 * the texture object should be ready for rendering.
1485 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1488 st_finalize_texture(struct gl_context
*ctx
,
1489 struct pipe_context
*pipe
,
1490 struct gl_texture_object
*tObj
)
1492 struct st_context
*st
= st_context(ctx
);
1493 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1494 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1496 struct st_texture_image
*firstImage
;
1497 enum pipe_format firstImageFormat
;
1498 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1500 if (stObj
->base
._Complete
) {
1501 /* The texture is complete and we know exactly how many mipmap levels
1502 * are present/needed. This is conditional because we may be called
1503 * from the st_generate_mipmap() function when the texture object is
1504 * incomplete. In that case, we'll have set stObj->lastLevel before
1507 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1508 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1509 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1511 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1514 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1517 /* If both firstImage and stObj point to a texture which can contain
1518 * all active images, favour firstImage. Note that because of the
1519 * completeness requirement, we know that the image dimensions
1522 if (firstImage
->pt
&&
1523 firstImage
->pt
!= stObj
->pt
&&
1524 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1525 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1526 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1529 /* Find gallium format for the Mesa texture */
1530 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1532 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1534 GLuint width
, height
, depth
;
1535 if (!guess_base_level_size(stObj
->base
.Target
,
1536 firstImage
->base
.Width2
,
1537 firstImage
->base
.Height2
,
1538 firstImage
->base
.Depth2
,
1539 firstImage
->base
.Level
,
1540 &width
, &height
, &depth
)) {
1541 width
= stObj
->width0
;
1542 height
= stObj
->height0
;
1543 depth
= stObj
->depth0
;
1545 /* convert GL dims to Gallium dims */
1546 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1547 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1550 /* If we already have a gallium texture, check that it matches the texture
1551 * object's format, target, size, num_levels, etc.
1554 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1555 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1556 stObj
->pt
->last_level
< stObj
->lastLevel
||
1557 stObj
->pt
->width0
!= ptWidth
||
1558 stObj
->pt
->height0
!= ptHeight
||
1559 stObj
->pt
->depth0
!= ptDepth
||
1560 stObj
->pt
->array_size
!= ptLayers
)
1562 /* The gallium texture does not match the Mesa texture so delete the
1563 * gallium texture now. We'll make a new one below.
1565 pipe_resource_reference(&stObj
->pt
, NULL
);
1566 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1567 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1571 /* May need to create a new gallium texture:
1574 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1576 stObj
->pt
= st_texture_create(st
,
1577 gl_target_to_pipe(stObj
->base
.Target
),
1587 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1592 /* Pull in any images not in the object's texture:
1594 for (face
= 0; face
< nr_faces
; face
++) {
1596 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1597 struct st_texture_image
*stImage
=
1598 st_texture_image(stObj
->base
.Image
[face
][level
]);
1600 /* Need to import images in main memory or held in other textures.
1602 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1604 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1605 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1606 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
1607 /* src image fits expected dest mipmap level size */
1608 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1619 * Returns pointer to a default/dummy texture.
1620 * This is typically used when the current shader has tex/sample instructions
1621 * but the user has not provided a (any) texture(s).
1623 struct gl_texture_object
*
1624 st_get_default_texture(struct st_context
*st
)
1626 if (!st
->default_texture
) {
1627 static const GLenum target
= GL_TEXTURE_2D
;
1628 GLubyte pixels
[16][16][4];
1629 struct gl_texture_object
*texObj
;
1630 struct gl_texture_image
*texImg
;
1633 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1634 * when attempting to sample incomplete textures.
1636 for (i
= 0; i
< 16; i
++) {
1637 for (j
= 0; j
< 16; j
++) {
1638 pixels
[i
][j
][0] = 0;
1639 pixels
[i
][j
][1] = 0;
1640 pixels
[i
][j
][2] = 0;
1641 pixels
[i
][j
][3] = 255;
1645 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1647 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1649 _mesa_init_teximage_fields(st
->ctx
, texImg
,
1650 16, 16, 1, 0, /* w, h, d, border */
1651 GL_RGBA
, MESA_FORMAT_RGBA8888
);
1653 st_TexImage(st
->ctx
, 2,
1655 GL_RGBA
, /* level, intformat */
1656 16, 16, 1, 0, /* w, h, d, border */
1657 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1658 &st
->ctx
->DefaultPacking
,
1661 texObj
->Sampler
.MinFilter
= GL_NEAREST
;
1662 texObj
->Sampler
.MagFilter
= GL_NEAREST
;
1663 texObj
->_Complete
= GL_TRUE
;
1665 st
->default_texture
= texObj
;
1667 return st
->default_texture
;
1672 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1673 * for a whole mipmap stack.
1676 st_AllocTextureStorage(struct gl_context
*ctx
,
1677 struct gl_texture_object
*texObj
,
1678 GLsizei levels
, GLsizei width
,
1679 GLsizei height
, GLsizei depth
)
1681 const GLuint numFaces
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1682 struct st_context
*st
= st_context(ctx
);
1683 struct st_texture_object
*stObj
= st_texture_object(texObj
);
1684 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
1685 enum pipe_format fmt
;
1690 /* Save the level=0 dimensions */
1691 stObj
->width0
= width
;
1692 stObj
->height0
= height
;
1693 stObj
->depth0
= depth
;
1694 stObj
->lastLevel
= levels
- 1;
1696 fmt
= st_mesa_format_to_pipe_format(texObj
->Image
[0][0]->TexFormat
);
1698 bindings
= default_bindings(st
, fmt
);
1700 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
1701 width
, height
, depth
,
1702 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1704 stObj
->pt
= st_texture_create(st
,
1705 gl_target_to_pipe(texObj
->Target
),
1716 /* Set image resource pointers */
1717 for (level
= 0; level
< levels
; level
++) {
1719 for (face
= 0; face
< numFaces
; face
++) {
1720 struct st_texture_image
*stImage
=
1721 st_texture_image(texObj
->Image
[face
][level
]);
1722 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1732 st_init_texture_functions(struct dd_function_table
*functions
)
1734 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1735 functions
->TexImage1D
= st_TexImage1D
;
1736 functions
->TexImage2D
= st_TexImage2D
;
1737 functions
->TexImage3D
= st_TexImage3D
;
1738 functions
->TexSubImage1D
= _mesa_store_texsubimage1d
;
1739 functions
->TexSubImage2D
= _mesa_store_texsubimage2d
;
1740 functions
->TexSubImage3D
= _mesa_store_texsubimage3d
;
1741 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1742 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1743 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1744 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1745 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1746 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1747 functions
->GenerateMipmap
= st_generate_mipmap
;
1749 functions
->GetTexImage
= st_GetTexImage
;
1751 /* compressed texture functions */
1752 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1753 functions
->GetCompressedTexImage
= _mesa_get_compressed_teximage
;
1755 functions
->NewTextureObject
= st_NewTextureObject
;
1756 functions
->NewTextureImage
= st_NewTextureImage
;
1757 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
1758 functions
->DeleteTexture
= st_DeleteTextureObject
;
1759 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
1760 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
1761 functions
->MapTextureImage
= st_MapTextureImage
;
1762 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
1764 /* XXX Temporary until we can query pipe's texture sizes */
1765 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;
1767 functions
->AllocTextureStorage
= st_AllocTextureStorage
;