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 struct gl_texture_image
*texImage
,
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 GLsizei imageSize
, GLboolean compressed_src
)
512 struct st_context
*st
= st_context(ctx
);
513 struct gl_texture_object
*texObj
= texImage
->TexObject
;
514 struct st_texture_object
*stObj
= st_texture_object(texObj
);
515 struct st_texture_image
*stImage
= st_texture_image(texImage
);
516 const GLenum target
= texObj
->Target
;
517 const GLuint level
= texImage
->Level
;
518 GLuint dstRowStride
= 0;
519 enum pipe_transfer_usage transfer_usage
= 0;
522 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
523 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
525 /* switch to "normal" */
526 if (stObj
->surface_based
) {
529 _mesa_clear_texture_object(ctx
, texObj
);
530 pipe_resource_reference(&stObj
->pt
, NULL
);
532 /* oops, need to init this image again */
533 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
534 internalFormat
, format
, type
);
536 _mesa_init_teximage_fields(ctx
, texImage
,
537 width
, height
, depth
, border
,
538 internalFormat
, texFormat
);
540 stObj
->surface_based
= GL_FALSE
;
543 assert(texImage
->Width
== width
);
544 assert(texImage
->Height
== height
);
545 assert(texImage
->Depth
== depth
);
547 /* Release the reference to a potentially orphaned buffer.
548 * Release any old malloced memory.
551 pipe_resource_reference(&stImage
->pt
, NULL
);
552 assert(!stImage
->TexData
);
554 else if (stImage
->TexData
) {
555 _mesa_align_free(stImage
->TexData
);
559 * See if the new image is somehow incompatible with the existing
560 * mipmap. If so, free the old mipmap.
563 if (level
> (GLint
) stObj
->pt
->last_level
||
564 !st_texture_match_image(stObj
->pt
, &stImage
->base
)) {
566 pipe_resource_reference(&stObj
->pt
, NULL
);
568 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
572 if (width
== 0 || height
== 0 || depth
== 0) {
573 /* stop after freeing old image */
578 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
579 /* Probably out of memory.
580 * Try flushing any pending rendering, then retry.
583 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
584 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
590 assert(!stImage
->pt
);
592 /* Check if this texture image can live inside the texture object's buffer.
593 * If so, store the image there. Otherwise the image will temporarily live
597 st_texture_match_image(stObj
->pt
, &stImage
->base
)) {
599 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
604 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
606 /* Pixel data may come from regular user memory or a PBO. For the later,
607 * do bounds checking and map the PBO to read pixels data from it.
609 * XXX we should try to use a GPU-accelerated path to copy the image data
610 * from the PBO to the texture.
612 if (compressed_src
) {
613 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
615 "glCompressedTexImage");
618 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
620 pixels
, unpack
, "glTexImage");
623 /* for a 1D array upload the image as a series of layer with height = 1 */
624 if (target
== GL_TEXTURE_1D_ARRAY
) {
630 * Prepare to store the texture data. Either map the gallium texture buffer
631 * memory or malloc space for it.
635 /* We've allocated texture resource, but have no pixel data - all done. */
639 /* Store the image in the gallium transfer object */
640 if (format
== GL_DEPTH_COMPONENT
&&
641 util_format_is_depth_and_stencil(stImage
->pt
->format
))
642 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
644 transfer_usage
= PIPE_TRANSFER_WRITE
;
646 dstMap
= st_texture_image_map(st
, stImage
, 0,
647 transfer_usage
, 0, 0, width
, height
);
648 if(stImage
->transfer
)
649 dstRowStride
= stImage
->transfer
->stride
;
652 /* Allocate regular memory and store the image there temporarily. */
653 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
654 width
, height
, depth
);
655 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
657 stImage
->TexData
= _mesa_align_malloc(imageSize
, 16);
658 dstMap
= stImage
->TexData
;
662 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
667 /* We've allocated texture memory, but have no pixel data - all done. */
671 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
672 width
, height
, depth
, width
, dstRowStride
);
674 /* Copy user texture image into the mapped texture buffer.
676 if (compressed_src
) {
677 const GLuint srcRowStride
=
678 _mesa_format_row_stride(texImage
->TexFormat
, width
);
679 if (dstRowStride
== srcRowStride
) {
680 memcpy(dstMap
, pixels
, imageSize
);
683 GLubyte
*dst
= dstMap
;
684 const char *src
= pixels
;
685 GLuint i
, bw
, bh
, lines
;
686 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
687 lines
= (height
+ bh
- 1) / bh
;
689 for (i
= 0; i
< lines
; ++i
) {
690 memcpy(dst
, src
, srcRowStride
);
697 const GLuint srcImageStride
=
698 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
700 const GLubyte
*src
= (const GLubyte
*) pixels
;
702 for (i
= 0; i
< depth
; i
++) {
703 if (!_mesa_texstore(ctx
, dims
,
704 texImage
->_BaseFormat
,
707 (GLubyte
**) &dstMap
, /* dstSlice */
709 format
, type
, src
, unpack
)) {
710 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
713 if (stImage
->pt
&& i
+ 1 < depth
) {
714 /* unmap this slice */
715 st_texture_image_unmap(st
, stImage
);
716 /* map next slice of 3D texture */
717 dstMap
= st_texture_image_map(st
, stImage
, i
+ 1,
718 transfer_usage
, 0, 0,
720 src
+= srcImageStride
;
726 _mesa_unmap_teximage_pbo(ctx
, unpack
);
728 if (stImage
->pt
&& stImage
->transfer
) {
729 st_texture_image_unmap(st
, stImage
);
735 st_TexImage3D(struct gl_context
* ctx
,
736 struct gl_texture_image
*texImage
,
737 GLint internalFormat
,
738 GLint width
, GLint height
, GLint depth
,
740 GLenum format
, GLenum type
, const void *pixels
,
741 const struct gl_pixelstore_attrib
*unpack
)
743 st_TexImage(ctx
, 3, texImage
, internalFormat
, width
, height
, depth
, border
,
744 format
, type
, pixels
, unpack
, 0, GL_FALSE
);
749 st_TexImage2D(struct gl_context
* ctx
,
750 struct gl_texture_image
*texImage
,
751 GLint internalFormat
,
752 GLint width
, GLint height
, GLint border
,
753 GLenum format
, GLenum type
, const void *pixels
,
754 const struct gl_pixelstore_attrib
*unpack
)
756 st_TexImage(ctx
, 2, texImage
, internalFormat
, width
, height
, 1, border
,
757 format
, type
, pixels
, unpack
, 0, GL_FALSE
);
762 st_TexImage1D(struct gl_context
* ctx
,
763 struct gl_texture_image
*texImage
,
764 GLint internalFormat
,
765 GLint width
, GLint border
,
766 GLenum format
, GLenum type
, const void *pixels
,
767 const struct gl_pixelstore_attrib
*unpack
)
769 st_TexImage(ctx
, 1, texImage
, internalFormat
, width
, 1, 1, border
,
770 format
, type
, pixels
, unpack
, 0, GL_FALSE
);
775 st_CompressedTexImage2D(struct gl_context
*ctx
,
776 struct gl_texture_image
*texImage
,
777 GLint internalFormat
,
778 GLint width
, GLint height
, GLint border
,
779 GLsizei imageSize
, const GLvoid
*data
)
781 st_TexImage(ctx
, 2, texImage
, internalFormat
, width
, height
, 1, border
,
782 0, 0, data
, &ctx
->Unpack
, imageSize
, GL_TRUE
);
788 * glGetTexImage() helper: decompress a compressed texture by rendering
789 * a textured quad. Store the results in the user's buffer.
792 decompress_with_blit(struct gl_context
* ctx
,
793 GLenum format
, GLenum type
, GLvoid
*pixels
,
794 struct gl_texture_image
*texImage
)
796 struct st_context
*st
= st_context(ctx
);
797 struct pipe_context
*pipe
= st
->pipe
;
798 struct st_texture_image
*stImage
= st_texture_image(texImage
);
799 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
800 struct pipe_sampler_view
*src_view
=
801 st_get_texture_sampler_view(stObj
, pipe
);
802 const GLuint width
= texImage
->Width
;
803 const GLuint height
= texImage
->Height
;
804 struct pipe_surface
*dst_surface
;
805 struct pipe_resource
*dst_texture
;
806 struct pipe_transfer
*tex_xfer
;
807 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
808 PIPE_BIND_TRANSFER_READ
);
810 /* create temp / dest surface */
811 if (!util_create_rgba_surface(pipe
, width
, height
, bind
,
812 &dst_texture
, &dst_surface
)) {
813 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
814 "in decompress_with_blit()");
818 /* Disable conditional rendering. */
819 if (st
->render_condition
) {
820 pipe
->render_condition(pipe
, NULL
, 0);
823 /* Choose the source mipmap level */
824 src_view
->u
.tex
.first_level
= src_view
->u
.tex
.last_level
= texImage
->Level
;
826 /* blit/render/decompress */
827 util_blit_pixels_tex(st
->blit
,
828 src_view
, /* pipe_resource (src) */
829 0, 0, /* src x0, y0 */
830 width
, height
, /* src x1, y1 */
831 dst_surface
, /* pipe_surface (dst) */
832 0, 0, /* dst x0, y0 */
833 width
, height
, /* dst x1, y1 */
835 PIPE_TEX_MIPFILTER_NEAREST
);
837 /* Restore conditional rendering state. */
838 if (st
->render_condition
) {
839 pipe
->render_condition(pipe
, st
->render_condition
,
843 /* map the dst_surface so we can read from it */
844 tex_xfer
= pipe_get_transfer(pipe
,
847 0, 0, width
, height
);
849 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
851 /* copy/pack data into user buffer */
852 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
854 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
855 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
857 for (row
= 0; row
< height
; row
++) {
858 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
859 height
, format
, type
, row
, 0);
860 memcpy(dest
, map
, bytesPerRow
);
861 map
+= tex_xfer
->stride
;
863 pipe_transfer_unmap(pipe
, tex_xfer
);
866 /* format translation via floats */
868 enum pipe_format pformat
= util_format_linear(dst_texture
->format
);
869 for (row
= 0; row
< height
; row
++) {
870 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
871 GLfloat rgba
[4 * MAX_WIDTH
];
872 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
873 height
, format
, type
, row
, 0);
875 if (ST_DEBUG
& DEBUG_FALLBACK
)
876 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
878 /* get float[4] rgba row from surface */
879 pipe_get_tile_rgba_format(pipe
, tex_xfer
, 0, row
, width
, 1,
882 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
883 type
, dest
, &ctx
->Pack
, transferOps
);
887 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
889 pipe
->transfer_destroy(pipe
, tex_xfer
);
891 /* destroy the temp / dest surface */
892 util_destroy_rgba_surface(dst_texture
, dst_surface
);
898 * Called via ctx->Driver.GetTexImage()
901 st_GetTexImage(struct gl_context
* ctx
,
902 GLenum format
, GLenum type
, GLvoid
* pixels
,
903 struct gl_texture_image
*texImage
)
905 struct st_texture_image
*stImage
= st_texture_image(texImage
);
907 if (stImage
->pt
&& util_format_is_s3tc(stImage
->pt
->format
)) {
908 /* Need to decompress the texture.
909 * We'll do this by rendering a textured quad (which is hopefully
910 * faster than using the fallback code in texcompress.c).
911 * Note that we only expect RGBA formats (no Z/depth formats).
913 decompress_with_blit(ctx
, format
, type
, pixels
, texImage
);
916 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
922 st_CompressedTexSubImage1D(struct gl_context
*ctx
,
923 struct gl_texture_image
*texImage
,
924 GLint xoffset
, GLsizei width
,
926 GLsizei imageSize
, const GLvoid
*data
)
933 st_CompressedTexSubImage2D(struct gl_context
*ctx
,
934 struct gl_texture_image
*texImage
,
935 GLint xoffset
, GLint yoffset
,
936 GLsizei width
, GLint height
,
938 GLsizei imageSize
, const GLvoid
*data
)
940 struct st_context
*st
= st_context(ctx
);
941 struct st_texture_image
*stImage
= st_texture_image(texImage
);
945 enum pipe_format pformat
;
949 pformat
= stImage
->pt
->format
;
951 dstMap
= st_texture_image_map(st
, stImage
, 0,
956 srcBlockStride
= util_format_get_stride(pformat
, width
);
957 dstBlockStride
= stImage
->transfer
->stride
;
960 /* TODO find good values for block and strides */
961 /* TODO also adjust texImage->data for yoffset/xoffset */
966 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
970 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
971 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
973 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
974 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
975 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
976 char *dst
= (char *) dstMap
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
977 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
980 if (stImage
->pt
&& stImage
->transfer
) {
981 st_texture_image_unmap(st
, stImage
);
987 st_CompressedTexSubImage3D(struct gl_context
*ctx
,
988 struct gl_texture_image
*texImage
,
989 GLint xoffset
, GLint yoffset
, GLint zoffset
,
990 GLsizei width
, GLint height
, GLint depth
,
992 GLsizei imageSize
, const GLvoid
*data
)
1000 * Do a CopyTexSubImage operation using a read transfer from the source,
1001 * a write transfer to the destination and get_tile()/put_tile() to access
1002 * the pixels/texels.
1004 * Note: srcY=0=TOP of renderbuffer
1007 fallback_copy_texsubimage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1008 struct st_renderbuffer
*strb
,
1009 struct st_texture_image
*stImage
,
1011 GLint destX
, GLint destY
, GLint destZ
,
1012 GLint srcX
, GLint srcY
,
1013 GLsizei width
, GLsizei height
)
1015 struct st_context
*st
= st_context(ctx
);
1016 struct pipe_context
*pipe
= st
->pipe
;
1017 struct pipe_transfer
*src_trans
;
1019 enum pipe_transfer_usage transfer_usage
;
1021 if (ST_DEBUG
& DEBUG_FALLBACK
)
1022 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1024 assert(width
<= MAX_WIDTH
);
1026 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1027 srcY
= strb
->Base
.Height
- srcY
- height
;
1030 src_trans
= pipe_get_transfer(pipe
,
1033 strb
->rtt_face
+ strb
->rtt_slice
,
1038 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1039 baseFormat
== GL_DEPTH_STENCIL
) &&
1040 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1041 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1043 transfer_usage
= PIPE_TRANSFER_WRITE
;
1045 /* XXX this used to ignore destZ param */
1046 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
1047 destX
, destY
, width
, height
);
1049 if (baseFormat
== GL_DEPTH_COMPONENT
||
1050 baseFormat
== GL_DEPTH_STENCIL
) {
1051 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1052 ctx
->Pixel
.DepthBias
!= 0.0F
);
1055 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1056 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1065 /* To avoid a large temp memory allocation, do copy row by row */
1066 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1067 uint data
[MAX_WIDTH
];
1068 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1070 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1072 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1078 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1080 if (tempSrc
&& texDest
) {
1081 const GLint dims
= 2;
1082 const GLint dstRowStride
= stImage
->transfer
->stride
;
1083 struct gl_texture_image
*texImage
= &stImage
->base
;
1084 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1086 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1087 unpack
.Invert
= GL_TRUE
;
1090 /* get float/RGBA image from framebuffer */
1091 /* XXX this usually involves a lot of int/float conversion.
1092 * try to avoid that someday.
1094 pipe_get_tile_rgba_format(pipe
, src_trans
, 0, 0, width
, height
,
1095 util_format_linear(strb
->texture
->format
),
1098 /* Store into texture memory.
1099 * Note that this does some special things such as pixel transfer
1100 * ops and format conversion. In particular, if the dest tex format
1101 * is actually RGBA but the user created the texture as GL_RGB we
1102 * need to fill-in/override the alpha channel with 1.0.
1104 _mesa_texstore(ctx
, dims
,
1105 texImage
->_BaseFormat
,
1106 texImage
->TexFormat
,
1108 (GLubyte
**) &texDest
,
1110 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1114 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1121 st_texture_image_unmap(st
, stImage
);
1122 pipe
->transfer_destroy(pipe
, src_trans
);
1128 * If the format of the src renderbuffer and the format of the dest
1129 * texture are compatible (in terms of blitting), return a TGSI writemask
1130 * to be used during the blit.
1131 * If the src/dest are incompatible, return 0.
1134 compatible_src_dst_formats(struct gl_context
*ctx
,
1135 const struct gl_renderbuffer
*src
,
1136 const struct gl_texture_image
*dst
)
1138 /* Get logical base formats for the src and dest.
1139 * That is, use the user-requested formats and not the actual, device-
1141 * For example, the user may have requested an A8 texture but the
1142 * driver may actually be using an RGBA texture format. When we
1143 * copy/blit to that texture, we only want to copy the Alpha channel
1144 * and not the RGB channels.
1146 * Similarly, when the src FBO was created an RGB format may have been
1147 * requested but the driver actually chose an RGBA format. In that case,
1148 * we don't want to copy the undefined Alpha channel to the dest texture
1149 * (it should be 1.0).
1151 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1152 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1155 * XXX when we have red-only and red/green renderbuffers we'll need
1156 * to add more cases here (or implement a general-purpose routine that
1157 * queries the existance of the R,G,B,A channels in the src and dest).
1159 if (srcFormat
== dstFormat
) {
1160 /* This is the same as matching_base_formats, which should
1161 * always pass, as it did previously.
1163 return TGSI_WRITEMASK_XYZW
;
1165 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1166 /* Make sure that A in the dest is 1. The actual src format
1167 * may be RGBA and have undefined A values.
1169 return TGSI_WRITEMASK_XYZ
;
1171 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1172 /* Make sure that A in the dest is 1. The actual dst format
1173 * may be RGBA and will need A=1 to provide proper alpha values
1174 * when sampled later.
1176 return TGSI_WRITEMASK_XYZ
;
1179 if (ST_DEBUG
& DEBUG_FALLBACK
)
1180 debug_printf("%s failed for src %s, dst %s\n",
1182 _mesa_lookup_enum_by_nr(srcFormat
),
1183 _mesa_lookup_enum_by_nr(dstFormat
));
1194 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1195 * Note that the region to copy has already been clipped so we know we
1196 * won't read from outside the source renderbuffer's bounds.
1198 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1201 st_copy_texsubimage(struct gl_context
*ctx
,
1202 GLenum target
, GLint level
,
1203 GLint destX
, GLint destY
, GLint destZ
,
1204 GLint srcX
, GLint srcY
,
1205 GLsizei width
, GLsizei height
)
1207 struct gl_texture_unit
*texUnit
=
1208 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1209 struct gl_texture_object
*texObj
=
1210 _mesa_select_tex_object(ctx
, texUnit
, target
);
1211 struct gl_texture_image
*texImage
=
1212 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1213 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1214 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1215 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1216 struct st_renderbuffer
*strb
;
1217 struct st_context
*st
= st_context(ctx
);
1218 struct pipe_context
*pipe
= st
->pipe
;
1219 struct pipe_screen
*screen
= pipe
->screen
;
1220 enum pipe_format dest_format
, src_format
;
1221 GLboolean matching_base_formats
;
1222 GLuint format_writemask
, sample_count
;
1223 struct pipe_surface
*dest_surface
= NULL
;
1224 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1225 struct pipe_surface surf_tmpl
;
1226 unsigned int dst_usage
;
1229 /* make sure finalize_textures has been called?
1231 if (0) st_validate_state(st
);
1233 /* determine if copying depth or color data */
1234 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1235 texBaseFormat
== GL_DEPTH_STENCIL
) {
1236 strb
= st_renderbuffer(fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
1239 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1240 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1243 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1244 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1248 sample_count
= strb
->surface
->texture
->nr_samples
;
1249 /* I believe this would be legal, presumably would need to do a resolve
1250 for color, and for depth/stencil spec says to just use one of the
1251 depth/stencil samples per pixel? Need some transfer clarifications. */
1252 assert(sample_count
< 2);
1255 assert(strb
->surface
);
1256 assert(stImage
->pt
);
1258 src_format
= strb
->surface
->format
;
1259 dest_format
= stImage
->pt
->format
;
1262 * Determine if the src framebuffer and dest texture have the same
1263 * base format. We need this to detect a case such as the framebuffer
1264 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1265 * texture format stores RGBA we need to set A=1 (overriding the
1266 * framebuffer's alpha values). We can't do that with the blit or
1267 * textured-quad paths.
1269 matching_base_formats
=
1270 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1271 _mesa_get_format_base_format(texImage
->TexFormat
));
1273 if (ctx
->_ImageTransferState
) {
1277 if (matching_base_formats
&&
1278 src_format
== dest_format
&&
1280 /* use surface_copy() / blit */
1281 struct pipe_box src_box
;
1282 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
1283 width
, height
, &src_box
);
1285 /* for resource_copy_region(), y=0=top, always */
1286 pipe
->resource_copy_region(pipe
,
1289 stImage
->base
.Level
,
1290 destX
, destY
, destZ
+ stImage
->base
.Face
,
1293 strb
->surface
->u
.tex
.level
,
1298 if (texBaseFormat
== GL_DEPTH_STENCIL
) {
1302 if (texBaseFormat
== GL_DEPTH_COMPONENT
) {
1303 format_writemask
= TGSI_WRITEMASK_XYZW
;
1304 dst_usage
= PIPE_BIND_DEPTH_STENCIL
;
1307 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1308 dst_usage
= PIPE_BIND_RENDER_TARGET
;
1311 if (!format_writemask
||
1312 !screen
->is_format_supported(screen
, src_format
,
1313 PIPE_TEXTURE_2D
, sample_count
,
1314 PIPE_BIND_SAMPLER_VIEW
) ||
1315 !screen
->is_format_supported(screen
, dest_format
,
1322 srcY1
= strb
->Base
.Height
- srcY
- height
;
1323 srcY0
= srcY1
+ height
;
1327 srcY1
= srcY0
+ height
;
1330 /* Disable conditional rendering. */
1331 if (st
->render_condition
) {
1332 pipe
->render_condition(pipe
, NULL
, 0);
1335 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1336 surf_tmpl
.format
= util_format_linear(stImage
->pt
->format
);
1337 surf_tmpl
.usage
= dst_usage
;
1338 surf_tmpl
.u
.tex
.level
= stImage
->base
.Level
;
1339 surf_tmpl
.u
.tex
.first_layer
= stImage
->base
.Face
+ destZ
;
1340 surf_tmpl
.u
.tex
.last_layer
= stImage
->base
.Face
+ destZ
;
1342 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1344 util_blit_pixels_writemask(st
->blit
,
1346 strb
->surface
->u
.tex
.level
,
1348 srcX
+ width
, srcY1
,
1349 strb
->surface
->u
.tex
.first_layer
,
1352 destX
+ width
, destY
+ height
,
1353 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1355 pipe_surface_reference(&dest_surface
, NULL
);
1357 /* Restore conditional rendering state. */
1358 if (st
->render_condition
) {
1359 pipe
->render_condition(pipe
, st
->render_condition
,
1360 st
->condition_mode
);
1366 /* software fallback */
1367 fallback_copy_texsubimage(ctx
, target
, level
,
1368 strb
, stImage
, texBaseFormat
,
1369 destX
, destY
, destZ
,
1370 srcX
, srcY
, width
, height
);
1376 st_CopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1377 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1379 const GLint yoffset
= 0, zoffset
= 0;
1380 const GLsizei height
= 1;
1381 st_copy_texsubimage(ctx
, target
, level
,
1382 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1383 x
, y
, width
, height
); /* src X, Y, size */
1388 st_CopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1389 GLint xoffset
, GLint yoffset
,
1390 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1392 const GLint zoffset
= 0;
1393 st_copy_texsubimage(ctx
, target
, level
,
1394 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1395 x
, y
, width
, height
); /* src X, Y, size */
1400 st_CopyTexSubImage3D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1401 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1402 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1404 st_copy_texsubimage(ctx
, target
, level
,
1405 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1406 x
, y
, width
, height
); /* src X, Y, size */
1411 * Copy image data from stImage into the texture object 'stObj' at level
1415 copy_image_data_to_texture(struct st_context
*st
,
1416 struct st_texture_object
*stObj
,
1418 struct st_texture_image
*stImage
)
1422 const struct gl_texture_image
*dstImage
=
1423 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
1425 assert(dstImage
->Width
== stImage
->base
.Width
);
1426 assert(dstImage
->Height
== stImage
->base
.Height
);
1427 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1431 /* Copy potentially with the blitter:
1433 st_texture_image_copy(st
->pipe
,
1434 stObj
->pt
, dstLevel
, /* dest texture, level */
1435 stImage
->pt
, stImage
->base
.Level
, /* src texture, level */
1436 stImage
->base
.Face
);
1438 pipe_resource_reference(&stImage
->pt
, NULL
);
1440 else if (stImage
->TexData
) {
1441 /* Copy from malloc'd memory */
1442 /* XXX this should be re-examined/tested with a compressed format */
1443 GLuint blockSize
= util_format_get_blocksize(stObj
->pt
->format
);
1444 GLuint srcRowStride
= stImage
->base
.Width
* blockSize
;
1445 GLuint srcSliceStride
= stImage
->base
.Height
* srcRowStride
;
1446 st_texture_image_data(st
,
1453 _mesa_align_free(stImage
->TexData
);
1454 stImage
->TexData
= NULL
;
1457 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1462 * Called during state validation. When this function is finished,
1463 * the texture object should be ready for rendering.
1464 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1467 st_finalize_texture(struct gl_context
*ctx
,
1468 struct pipe_context
*pipe
,
1469 struct gl_texture_object
*tObj
)
1471 struct st_context
*st
= st_context(ctx
);
1472 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1473 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1475 struct st_texture_image
*firstImage
;
1476 enum pipe_format firstImageFormat
;
1477 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1479 if (stObj
->base
._Complete
) {
1480 /* The texture is complete and we know exactly how many mipmap levels
1481 * are present/needed. This is conditional because we may be called
1482 * from the st_generate_mipmap() function when the texture object is
1483 * incomplete. In that case, we'll have set stObj->lastLevel before
1486 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1487 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1488 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1490 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1493 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1496 /* If both firstImage and stObj point to a texture which can contain
1497 * all active images, favour firstImage. Note that because of the
1498 * completeness requirement, we know that the image dimensions
1501 if (firstImage
->pt
&&
1502 firstImage
->pt
!= stObj
->pt
&&
1503 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1504 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1505 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1508 /* Find gallium format for the Mesa texture */
1509 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1511 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1513 GLuint width
, height
, depth
;
1514 if (!guess_base_level_size(stObj
->base
.Target
,
1515 firstImage
->base
.Width2
,
1516 firstImage
->base
.Height2
,
1517 firstImage
->base
.Depth2
,
1518 firstImage
->base
.Level
,
1519 &width
, &height
, &depth
)) {
1520 width
= stObj
->width0
;
1521 height
= stObj
->height0
;
1522 depth
= stObj
->depth0
;
1524 /* convert GL dims to Gallium dims */
1525 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1526 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1529 /* If we already have a gallium texture, check that it matches the texture
1530 * object's format, target, size, num_levels, etc.
1533 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1534 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1535 stObj
->pt
->last_level
< stObj
->lastLevel
||
1536 stObj
->pt
->width0
!= ptWidth
||
1537 stObj
->pt
->height0
!= ptHeight
||
1538 stObj
->pt
->depth0
!= ptDepth
||
1539 stObj
->pt
->array_size
!= ptLayers
)
1541 /* The gallium texture does not match the Mesa texture so delete the
1542 * gallium texture now. We'll make a new one below.
1544 pipe_resource_reference(&stObj
->pt
, NULL
);
1545 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1546 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1550 /* May need to create a new gallium texture:
1553 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1555 stObj
->pt
= st_texture_create(st
,
1556 gl_target_to_pipe(stObj
->base
.Target
),
1566 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1571 /* Pull in any images not in the object's texture:
1573 for (face
= 0; face
< nr_faces
; face
++) {
1575 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1576 struct st_texture_image
*stImage
=
1577 st_texture_image(stObj
->base
.Image
[face
][level
]);
1579 /* Need to import images in main memory or held in other textures.
1581 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1583 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1584 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1585 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
1586 /* src image fits expected dest mipmap level size */
1587 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1598 * Returns pointer to a default/dummy texture.
1599 * This is typically used when the current shader has tex/sample instructions
1600 * but the user has not provided a (any) texture(s).
1602 struct gl_texture_object
*
1603 st_get_default_texture(struct st_context
*st
)
1605 if (!st
->default_texture
) {
1606 static const GLenum target
= GL_TEXTURE_2D
;
1607 GLubyte pixels
[16][16][4];
1608 struct gl_texture_object
*texObj
;
1609 struct gl_texture_image
*texImg
;
1612 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1613 * when attempting to sample incomplete textures.
1615 for (i
= 0; i
< 16; i
++) {
1616 for (j
= 0; j
< 16; j
++) {
1617 pixels
[i
][j
][0] = 0;
1618 pixels
[i
][j
][1] = 0;
1619 pixels
[i
][j
][2] = 0;
1620 pixels
[i
][j
][3] = 255;
1624 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1626 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1628 _mesa_init_teximage_fields(st
->ctx
, texImg
,
1629 16, 16, 1, 0, /* w, h, d, border */
1630 GL_RGBA
, MESA_FORMAT_RGBA8888
);
1632 st_TexImage(st
->ctx
, 2,
1634 GL_RGBA
, /* level, intformat */
1635 16, 16, 1, 0, /* w, h, d, border */
1636 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1637 &st
->ctx
->DefaultPacking
,
1640 texObj
->Sampler
.MinFilter
= GL_NEAREST
;
1641 texObj
->Sampler
.MagFilter
= GL_NEAREST
;
1642 texObj
->_Complete
= GL_TRUE
;
1644 st
->default_texture
= texObj
;
1646 return st
->default_texture
;
1651 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1652 * for a whole mipmap stack.
1655 st_AllocTextureStorage(struct gl_context
*ctx
,
1656 struct gl_texture_object
*texObj
,
1657 GLsizei levels
, GLsizei width
,
1658 GLsizei height
, GLsizei depth
)
1660 const GLuint numFaces
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1661 struct st_context
*st
= st_context(ctx
);
1662 struct st_texture_object
*stObj
= st_texture_object(texObj
);
1663 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
1664 enum pipe_format fmt
;
1669 /* Save the level=0 dimensions */
1670 stObj
->width0
= width
;
1671 stObj
->height0
= height
;
1672 stObj
->depth0
= depth
;
1673 stObj
->lastLevel
= levels
- 1;
1675 fmt
= st_mesa_format_to_pipe_format(texObj
->Image
[0][0]->TexFormat
);
1677 bindings
= default_bindings(st
, fmt
);
1679 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
1680 width
, height
, depth
,
1681 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1683 stObj
->pt
= st_texture_create(st
,
1684 gl_target_to_pipe(texObj
->Target
),
1695 /* Set image resource pointers */
1696 for (level
= 0; level
< levels
; level
++) {
1698 for (face
= 0; face
< numFaces
; face
++) {
1699 struct st_texture_image
*stImage
=
1700 st_texture_image(texObj
->Image
[face
][level
]);
1701 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1711 st_init_texture_functions(struct dd_function_table
*functions
)
1713 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1714 functions
->TexImage1D
= st_TexImage1D
;
1715 functions
->TexImage2D
= st_TexImage2D
;
1716 functions
->TexImage3D
= st_TexImage3D
;
1717 functions
->TexSubImage1D
= _mesa_store_texsubimage1d
;
1718 functions
->TexSubImage2D
= _mesa_store_texsubimage2d
;
1719 functions
->TexSubImage3D
= _mesa_store_texsubimage3d
;
1720 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
1721 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
1722 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
1723 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
1724 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
1725 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
1726 functions
->GenerateMipmap
= st_generate_mipmap
;
1728 functions
->GetTexImage
= st_GetTexImage
;
1730 /* compressed texture functions */
1731 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
1732 functions
->GetCompressedTexImage
= _mesa_get_compressed_teximage
;
1734 functions
->NewTextureObject
= st_NewTextureObject
;
1735 functions
->NewTextureImage
= st_NewTextureImage
;
1736 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
1737 functions
->DeleteTexture
= st_DeleteTextureObject
;
1738 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
1739 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
1740 functions
->MapTextureImage
= st_MapTextureImage
;
1741 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
1743 /* XXX Temporary until we can query pipe's texture sizes */
1744 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;
1746 functions
->AllocTextureStorage
= st_AllocTextureStorage
;