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_cb_bufferobjects.h"
52 #include "state_tracker/st_format.h"
53 #include "state_tracker/st_texture.h"
54 #include "state_tracker/st_gen_mipmap.h"
55 #include "state_tracker/st_atom.h"
57 #include "pipe/p_context.h"
58 #include "pipe/p_defines.h"
59 #include "util/u_inlines.h"
60 #include "pipe/p_shader_tokens.h"
61 #include "util/u_tile.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 enum pipe_texture_target
72 gl_target_to_pipe(GLenum target
)
76 case GL_PROXY_TEXTURE_1D
:
77 return PIPE_TEXTURE_1D
;
79 case GL_PROXY_TEXTURE_2D
:
80 case GL_TEXTURE_EXTERNAL_OES
:
81 return PIPE_TEXTURE_2D
;
82 case GL_TEXTURE_RECTANGLE_NV
:
83 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
84 return PIPE_TEXTURE_RECT
;
86 case GL_PROXY_TEXTURE_3D
:
87 return PIPE_TEXTURE_3D
;
88 case GL_TEXTURE_CUBE_MAP_ARB
:
89 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
90 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
91 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
92 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
93 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
94 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
95 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
96 return PIPE_TEXTURE_CUBE
;
97 case GL_TEXTURE_1D_ARRAY_EXT
:
98 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
99 return PIPE_TEXTURE_1D_ARRAY
;
100 case GL_TEXTURE_2D_ARRAY_EXT
:
101 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
102 return PIPE_TEXTURE_2D_ARRAY
;
103 case GL_TEXTURE_BUFFER
:
105 case GL_TEXTURE_CUBE_MAP_ARRAY
:
106 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
107 return PIPE_TEXTURE_CUBE_ARRAY
;
115 /** called via ctx->Driver.NewTextureImage() */
116 static struct gl_texture_image
*
117 st_NewTextureImage(struct gl_context
* ctx
)
119 DBG("%s\n", __FUNCTION__
);
121 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
125 /** called via ctx->Driver.DeleteTextureImage() */
127 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
129 /* nothing special (yet) for st_texture_image */
130 _mesa_delete_texture_image(ctx
, img
);
134 /** called via ctx->Driver.NewTextureObject() */
135 static struct gl_texture_object
*
136 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
138 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
140 DBG("%s\n", __FUNCTION__
);
141 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
146 /** called via ctx->Driver.DeleteTextureObject() */
148 st_DeleteTextureObject(struct gl_context
*ctx
,
149 struct gl_texture_object
*texObj
)
151 struct st_context
*st
= st_context(ctx
);
152 struct st_texture_object
*stObj
= st_texture_object(texObj
);
154 pipe_resource_reference(&stObj
->pt
, NULL
);
155 if (stObj
->sampler_view
) {
156 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
158 _mesa_delete_texture_object(ctx
, texObj
);
162 /** called via ctx->Driver.FreeTextureImageBuffer() */
164 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
165 struct gl_texture_image
*texImage
)
167 struct st_texture_image
*stImage
= st_texture_image(texImage
);
169 DBG("%s\n", __FUNCTION__
);
172 pipe_resource_reference(&stImage
->pt
, NULL
);
175 if (stImage
->TexData
) {
176 _mesa_align_free(stImage
->TexData
);
177 stImage
->TexData
= NULL
;
182 /** called via ctx->Driver.MapTextureImage() */
184 st_MapTextureImage(struct gl_context
*ctx
,
185 struct gl_texture_image
*texImage
,
186 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
188 GLubyte
**mapOut
, GLint
*rowStrideOut
)
190 struct st_context
*st
= st_context(ctx
);
191 struct st_texture_image
*stImage
= st_texture_image(texImage
);
196 if (mode
& GL_MAP_READ_BIT
)
197 pipeMode
|= PIPE_TRANSFER_READ
;
198 if (mode
& GL_MAP_WRITE_BIT
)
199 pipeMode
|= PIPE_TRANSFER_WRITE
;
200 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
201 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
203 map
= st_texture_image_map(st
, stImage
, pipeMode
, x
, y
, slice
, w
, h
, 1);
206 *rowStrideOut
= stImage
->transfer
->stride
;
215 /** called via ctx->Driver.UnmapTextureImage() */
217 st_UnmapTextureImage(struct gl_context
*ctx
,
218 struct gl_texture_image
*texImage
,
221 struct st_context
*st
= st_context(ctx
);
222 struct st_texture_image
*stImage
= st_texture_image(texImage
);
223 st_texture_image_unmap(st
, stImage
);
228 * Return default texture resource binding bitmask for the given format.
231 default_bindings(struct st_context
*st
, enum pipe_format format
)
233 struct pipe_screen
*screen
= st
->pipe
->screen
;
234 const unsigned target
= PIPE_TEXTURE_2D
;
237 if (util_format_is_depth_or_stencil(format
))
238 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
240 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
242 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
246 format
= util_format_linear(format
);
248 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
251 return PIPE_BIND_SAMPLER_VIEW
;
257 * Given the size of a mipmap image, try to compute the size of the level=0
260 * Note that this isn't always accurate for odd-sized, non-POW textures.
261 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
263 * \return GL_TRUE for success, GL_FALSE for failure
266 guess_base_level_size(GLenum target
,
267 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
268 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
275 /* Guess the size of the base level.
276 * Depending on the image's size, we can't always make a guess here.
280 case GL_TEXTURE_1D_ARRAY
:
285 case GL_TEXTURE_2D_ARRAY
:
286 /* We can't make a good guess here, because the base level dimensions
289 if (width
== 1 || height
== 1) {
296 case GL_TEXTURE_CUBE_MAP
:
297 case GL_TEXTURE_CUBE_MAP_ARRAY
:
303 /* We can't make a good guess here, because the base level dimensions
306 if (width
== 1 || height
== 1 || depth
== 1) {
314 case GL_TEXTURE_RECTANGLE
:
331 * Try to allocate a pipe_resource object for the given st_texture_object.
333 * We use the given st_texture_image as a clue to determine the size of the
334 * mipmap image at level=0.
336 * \return GL_TRUE for success, GL_FALSE if out of memory.
339 guess_and_alloc_texture(struct st_context
*st
,
340 struct st_texture_object
*stObj
,
341 const struct st_texture_image
*stImage
)
343 GLuint lastLevel
, width
, height
, depth
;
345 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
346 enum pipe_format fmt
;
348 DBG("%s\n", __FUNCTION__
);
352 if (!guess_base_level_size(stObj
->base
.Target
,
353 stImage
->base
.Width2
,
354 stImage
->base
.Height2
,
355 stImage
->base
.Depth2
,
357 &width
, &height
, &depth
)) {
358 /* we can't determine the image size at level=0 */
359 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
360 /* this is not an out of memory error */
364 /* At this point, (width x height x depth) is the expected size of
365 * the level=0 mipmap image.
368 /* Guess a reasonable value for lastLevel. With OpenGL we have no
369 * idea how many mipmap levels will be in a texture until we start
370 * to render with it. Make an educated guess here but be prepared
371 * to re-allocating a texture buffer with space for more (or fewer)
372 * mipmap levels later.
374 if ((stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
375 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
376 (stObj
->base
.BaseLevel
== 0 &&
377 stObj
->base
.MaxLevel
== 0) ||
378 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
379 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
380 !stObj
->base
.GenerateMipmap
&&
381 stImage
->base
.Level
== 0) {
382 /* only alloc space for a single mipmap level */
386 /* alloc space for a full mipmap */
387 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
388 width
, height
, depth
) - 1;
391 /* Save the level=0 dimensions */
392 stObj
->width0
= width
;
393 stObj
->height0
= height
;
394 stObj
->depth0
= depth
;
396 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
398 bindings
= default_bindings(st
, fmt
);
400 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
401 width
, height
, depth
,
402 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
404 stObj
->pt
= st_texture_create(st
,
405 gl_target_to_pipe(stObj
->base
.Target
),
414 stObj
->lastLevel
= lastLevel
;
416 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
418 return stObj
->pt
!= NULL
;
423 * Called via ctx->Driver.AllocTextureImageBuffer().
424 * If the texture object/buffer already has space for the indicated image,
425 * we're done. Otherwise, allocate memory for the new texture image.
428 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
429 struct gl_texture_image
*texImage
)
431 struct st_context
*st
= st_context(ctx
);
432 struct st_texture_image
*stImage
= st_texture_image(texImage
);
433 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
434 const GLuint level
= texImage
->Level
;
435 GLuint width
= texImage
->Width
;
436 GLuint height
= texImage
->Height
;
437 GLuint depth
= texImage
->Depth
;
439 DBG("%s\n", __FUNCTION__
);
441 assert(!stImage
->TexData
);
442 assert(!stImage
->pt
); /* xxx this might be wrong */
444 /* Look if the parent texture object has space for this image */
446 level
<= stObj
->pt
->last_level
&&
447 st_texture_match_image(stObj
->pt
, texImage
)) {
448 /* this image will fit in the existing texture object's memory */
449 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
453 /* The parent texture object does not have space for this image */
455 pipe_resource_reference(&stObj
->pt
, NULL
);
456 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
458 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
459 /* Probably out of memory.
460 * Try flushing any pending rendering, then retry.
463 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
464 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
470 st_texture_match_image(stObj
->pt
, texImage
)) {
471 /* The image will live in the object's mipmap memory */
472 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
477 /* Create a new, temporary texture/resource/buffer to hold this
478 * one texture image. Note that when we later access this image
479 * (either for mapping or copying) we'll want to always specify
480 * mipmap level=0, even if the image represents some other mipmap
483 enum pipe_format format
=
484 st_mesa_format_to_pipe_format(texImage
->TexFormat
);
485 GLuint bindings
= default_bindings(st
, format
);
486 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
488 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
489 width
, height
, depth
,
490 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
492 stImage
->pt
= st_texture_create(st
,
493 gl_target_to_pipe(stObj
->base
.Target
),
501 return stImage
->pt
!= NULL
;
507 * Preparation prior to glTexImage. Basically check the 'surface_based'
508 * field and switch to a "normal" tex image if necessary.
511 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
512 GLenum format
, GLenum type
)
514 struct gl_texture_object
*texObj
= texImage
->TexObject
;
515 struct st_texture_object
*stObj
= st_texture_object(texObj
);
517 /* switch to "normal" */
518 if (stObj
->surface_based
) {
519 const GLenum target
= texObj
->Target
;
520 const GLuint level
= texImage
->Level
;
523 _mesa_clear_texture_object(ctx
, texObj
);
524 pipe_resource_reference(&stObj
->pt
, NULL
);
526 /* oops, need to init this image again */
527 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
528 texImage
->InternalFormat
, format
,
531 _mesa_init_teximage_fields(ctx
, texImage
,
532 texImage
->Width
, texImage
->Height
,
533 texImage
->Depth
, texImage
->Border
,
534 texImage
->InternalFormat
, texFormat
);
536 stObj
->surface_based
= GL_FALSE
;
542 * Return a writemask for the gallium blit. The parameters can be base
543 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
546 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
549 case GL_DEPTH_STENCIL
:
551 case GL_DEPTH_STENCIL
:
553 case GL_DEPTH_COMPONENT
:
555 case GL_STENCIL_INDEX
:
562 case GL_DEPTH_COMPONENT
:
564 case GL_DEPTH_STENCIL
:
565 case GL_DEPTH_COMPONENT
:
572 case GL_STENCIL_INDEX
:
574 case GL_STENCIL_INDEX
:
582 return PIPE_MASK_RGBA
;
588 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
589 struct gl_texture_image
*texImage
,
590 GLint xoffset
, GLint yoffset
, GLint zoffset
,
591 GLint width
, GLint height
, GLint depth
,
592 GLenum format
, GLenum type
, const void *pixels
,
593 const struct gl_pixelstore_attrib
*unpack
)
595 struct st_context
*st
= st_context(ctx
);
596 struct st_texture_image
*stImage
= st_texture_image(texImage
);
597 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
598 struct pipe_context
*pipe
= st
->pipe
;
599 struct pipe_screen
*screen
= pipe
->screen
;
600 struct pipe_resource
*dst
= stImage
->pt
;
601 struct pipe_resource
*src
= NULL
;
602 struct pipe_resource src_templ
;
603 struct pipe_transfer
*transfer
;
604 struct pipe_blit_info blit
;
605 enum pipe_format src_format
, dst_format
;
606 gl_format mesa_src_format
;
607 GLenum gl_target
= texImage
->TexObject
->Target
;
615 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
616 * blit implementation in some drivers. */
617 if (format
== GL_DEPTH_STENCIL
) {
621 /* If the base internal format and the texture format don't match,
622 * we can't use blit-based TexSubImage. */
623 if (texImage
->_BaseFormat
!=
624 _mesa_get_format_base_format(texImage
->TexFormat
)) {
628 /* See if the texture format already matches the format and type,
629 * in which case the memcpy-based fast path will likely be used and
630 * we don't have to blit. */
631 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
632 type
, unpack
->SwapBytes
)) {
636 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
637 bind
= PIPE_BIND_DEPTH_STENCIL
;
639 bind
= PIPE_BIND_RENDER_TARGET
;
641 /* See if the destination format is supported.
642 * For luminance and intensity, only the red channel is stored there. */
643 dst_format
= util_format_linear(dst
->format
);
644 dst_format
= util_format_luminance_to_red(dst_format
);
645 dst_format
= util_format_intensity_to_red(dst_format
);
648 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
649 dst
->nr_samples
, bind
)) {
653 /* Choose the source format. */
654 src_format
= st_choose_matching_format(screen
, PIPE_BIND_SAMPLER_VIEW
,
655 format
, type
, unpack
->SwapBytes
);
660 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
662 /* There is no reason to do this if we cannot use memcpy for the temporary
663 * source texture at least. This also takes transfer ops into account,
665 if (!_mesa_texstore_can_use_memcpy(ctx
,
666 _mesa_get_format_base_format(mesa_src_format
),
667 mesa_src_format
, format
, type
, unpack
)) {
671 /* TexSubImage only sets a single cubemap face. */
672 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
673 gl_target
= GL_TEXTURE_2D
;
676 /* Initialize the source texture description. */
677 memset(&src_templ
, 0, sizeof(src_templ
));
678 src_templ
.target
= gl_target_to_pipe(gl_target
);
679 src_templ
.format
= src_format
;
680 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
681 src_templ
.usage
= PIPE_USAGE_STAGING
;
683 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
684 &src_templ
.width0
, &src_templ
.height0
,
685 &src_templ
.depth0
, &src_templ
.array_size
);
687 /* Check for NPOT texture support. */
688 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
689 (!util_is_power_of_two(src_templ
.width0
) ||
690 !util_is_power_of_two(src_templ
.height0
) ||
691 !util_is_power_of_two(src_templ
.depth0
))) {
695 /* Create the source texture. */
696 src
= screen
->resource_create(screen
, &src_templ
);
701 /* Map source pixels. */
702 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
703 format
, type
, pixels
, unpack
,
706 /* This is a GL error. */
707 pipe_resource_reference(&src
, NULL
);
711 /* From now on, we need the gallium representation of dimensions. */
712 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
717 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
718 width
, height
, depth
, &transfer
);
720 _mesa_unmap_teximage_pbo(ctx
, unpack
);
721 pipe_resource_reference(&src
, NULL
);
725 /* Upload pixels (just memcpy). */
727 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
730 for (slice
= 0; slice
< depth
; slice
++) {
731 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
732 /* 1D array textures.
733 * We need to convert gallium coords to GL coords.
735 GLvoid
*src
= _mesa_image_address3d(unpack
, pixels
,
736 width
, depth
, format
,
738 memcpy(map
, src
, bytesPerRow
);
741 ubyte
*slice_map
= map
;
743 for (row
= 0; row
< height
; row
++) {
744 GLvoid
*src
= _mesa_image_address3d(unpack
, pixels
,
745 width
, height
, format
,
746 type
, slice
, row
, 0);
747 memcpy(slice_map
, src
, bytesPerRow
);
748 slice_map
+= transfer
->stride
;
751 map
+= transfer
->layer_stride
;
755 pipe_transfer_unmap(pipe
, transfer
);
756 _mesa_unmap_teximage_pbo(ctx
, unpack
);
759 blit
.src
.resource
= src
;
761 blit
.src
.format
= src_format
;
762 blit
.dst
.resource
= dst
;
763 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
;
764 blit
.dst
.format
= dst_format
;
765 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
766 blit
.dst
.box
.x
= xoffset
;
767 blit
.dst
.box
.y
= yoffset
;
768 blit
.dst
.box
.z
= zoffset
+ texImage
->Face
;
769 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
770 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
771 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
772 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
773 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
774 blit
.scissor_enable
= FALSE
;
776 st
->pipe
->blit(st
->pipe
, &blit
);
778 pipe_resource_reference(&src
, NULL
);
782 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
783 width
, height
, depth
, format
, type
, pixels
,
788 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
789 struct gl_texture_image
*texImage
,
790 GLenum format
, GLenum type
, const void *pixels
,
791 const struct gl_pixelstore_attrib
*unpack
)
793 assert(dims
== 1 || dims
== 2 || dims
== 3);
795 prep_teximage(ctx
, texImage
, format
, type
);
797 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
800 /* allocate storage for texture data */
801 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
802 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
806 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
807 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
808 format
, type
, pixels
, unpack
);
813 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
814 struct gl_texture_image
*texImage
,
815 GLsizei imageSize
, const GLvoid
*data
)
817 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
818 _mesa_store_compressed_teximage(ctx
, dims
, texImage
, imageSize
, data
);
825 * Called via ctx->Driver.GetTexImage()
827 * This uses a blit to copy the texture to a texture format which matches
828 * the format and type combo and then a fast read-back is done using memcpy.
829 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
830 * a format which matches the swizzling.
832 * If such a format isn't available, it falls back to _mesa_get_teximage.
834 * NOTE: Drivers usually do a blit to convert between tiled and linear
835 * texture layouts during texture uploads/downloads, so the blit
836 * we do here should be free in such cases.
839 st_GetTexImage(struct gl_context
* ctx
,
840 GLenum format
, GLenum type
, GLvoid
* pixels
,
841 struct gl_texture_image
*texImage
)
843 struct st_context
*st
= st_context(ctx
);
844 struct pipe_context
*pipe
= st
->pipe
;
845 struct pipe_screen
*screen
= pipe
->screen
;
846 GLuint width
= texImage
->Width
;
847 GLuint height
= texImage
->Height
;
848 GLuint depth
= texImage
->Depth
;
849 struct st_texture_image
*stImage
= st_texture_image(texImage
);
850 struct pipe_resource
*src
= st_texture_object(texImage
->TexObject
)->pt
;
851 struct pipe_resource
*dst
= NULL
;
852 struct pipe_resource dst_templ
;
853 enum pipe_format dst_format
, src_format
;
854 gl_format mesa_format
;
855 GLenum gl_target
= texImage
->TexObject
->Target
;
856 enum pipe_texture_target pipe_target
;
857 struct pipe_blit_info blit
;
858 unsigned bind
= PIPE_BIND_TRANSFER_READ
;
859 struct pipe_transfer
*tex_xfer
;
861 boolean done
= FALSE
;
867 /* XXX Fallback to _mesa_get_teximage for depth-stencil formats
868 * due to an incomplete stencil blit implementation in some drivers. */
869 if (format
== GL_DEPTH_STENCIL
) {
873 /* If the base internal format and the texture format don't match, we have
874 * to fall back to _mesa_get_teximage. */
875 if (texImage
->_BaseFormat
!=
876 _mesa_get_format_base_format(texImage
->TexFormat
)) {
880 /* See if the texture format already matches the format and type,
881 * in which case the memcpy-based fast path will be used. */
882 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
883 type
, ctx
->Pack
.SwapBytes
)) {
887 /* Convert the source format to what is expected by GetTexImage
888 * and see if it's supported.
890 * This only applies to glGetTexImage:
891 * - Luminance must be returned as (L,0,0,1).
892 * - Luminance alpha must be returned as (L,0,0,A).
893 * - Intensity must be returned as (I,0,0,1)
895 src_format
= util_format_linear(src
->format
);
896 src_format
= util_format_luminance_to_red(src_format
);
897 src_format
= util_format_intensity_to_red(src_format
);
900 !screen
->is_format_supported(screen
, src_format
, src
->target
,
902 PIPE_BIND_SAMPLER_VIEW
)) {
906 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
907 bind
|= PIPE_BIND_DEPTH_STENCIL
;
909 bind
|= PIPE_BIND_RENDER_TARGET
;
911 /* GetTexImage only returns a single face for cubemaps. */
912 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
913 gl_target
= GL_TEXTURE_2D
;
915 pipe_target
= gl_target_to_pipe(gl_target
);
917 /* Choose the destination format by finding the best match
918 * for the format+type combo. */
919 dst_format
= st_choose_matching_format(screen
, bind
, format
, type
,
920 ctx
->Pack
.SwapBytes
);
922 if (dst_format
== PIPE_FORMAT_NONE
) {
925 /* Fall back to _mesa_get_teximage except for compressed formats,
926 * where decompression with a blit is always preferred. */
927 if (!util_format_is_compressed(src
->format
)) {
931 /* Set the appropriate format for the decompressed texture.
932 * Luminance and sRGB formats shouldn't appear here.*/
933 switch (src_format
) {
934 case PIPE_FORMAT_DXT1_RGB
:
935 case PIPE_FORMAT_DXT1_RGBA
:
936 case PIPE_FORMAT_DXT3_RGBA
:
937 case PIPE_FORMAT_DXT5_RGBA
:
938 case PIPE_FORMAT_RGTC1_UNORM
:
939 case PIPE_FORMAT_RGTC2_UNORM
:
940 case PIPE_FORMAT_ETC1_RGB8
:
941 dst_glformat
= GL_RGBA8
;
943 case PIPE_FORMAT_RGTC1_SNORM
:
944 case PIPE_FORMAT_RGTC2_SNORM
:
945 if (!ctx
->Extensions
.EXT_texture_snorm
)
947 dst_glformat
= GL_RGBA8_SNORM
;
949 /* TODO: for BPTC_*FLOAT, set RGBA32F and check for ARB_texture_float */
955 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
956 pipe_target
, 0, bind
, FALSE
);
958 if (dst_format
== PIPE_FORMAT_NONE
) {
959 /* unable to get an rgba format!?! */
964 /* create the destination texture */
965 memset(&dst_templ
, 0, sizeof(dst_templ
));
966 dst_templ
.target
= pipe_target
;
967 dst_templ
.format
= dst_format
;
968 dst_templ
.bind
= bind
;
969 dst_templ
.usage
= PIPE_USAGE_STAGING
;
971 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
972 &dst_templ
.width0
, &dst_templ
.height0
,
973 &dst_templ
.depth0
, &dst_templ
.array_size
);
975 dst
= screen
->resource_create(screen
, &dst_templ
);
980 /* From now on, we need the gallium representation of dimensions. */
981 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
986 blit
.src
.resource
= src
;
987 blit
.src
.level
= texImage
->Level
;
988 blit
.src
.format
= src_format
;
989 blit
.dst
.resource
= dst
;
991 blit
.dst
.format
= dst
->format
;
992 blit
.src
.box
.x
= blit
.dst
.box
.x
= 0;
993 blit
.src
.box
.y
= blit
.dst
.box
.y
= 0;
994 blit
.src
.box
.z
= texImage
->Face
;
996 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
997 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
998 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
999 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
1000 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1001 blit
.scissor_enable
= FALSE
;
1003 /* blit/render/decompress */
1004 st
->pipe
->blit(st
->pipe
, &blit
);
1006 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
1008 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
1009 0, 0, 0, width
, height
, depth
, &tex_xfer
);
1014 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
1016 /* copy/pack data into user buffer */
1017 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
1018 ctx
->Pack
.SwapBytes
)) {
1020 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
1023 for (slice
= 0; slice
< depth
; slice
++) {
1024 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1025 /* 1D array textures.
1026 * We need to convert gallium coords to GL coords.
1028 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1029 width
, depth
, format
,
1031 memcpy(dest
, map
, bytesPerRow
);
1034 ubyte
*slice_map
= map
;
1036 for (row
= 0; row
< height
; row
++) {
1037 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1038 width
, height
, format
,
1039 type
, slice
, row
, 0);
1040 memcpy(dest
, slice_map
, bytesPerRow
);
1041 slice_map
+= tex_xfer
->stride
;
1044 map
+= tex_xfer
->layer_stride
;
1048 /* format translation via floats */
1052 assert(util_format_is_compressed(src
->format
));
1054 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
1059 if (ST_DEBUG
& DEBUG_FALLBACK
)
1060 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
1062 for (slice
= 0; slice
< depth
; slice
++) {
1063 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1064 /* 1D array textures.
1065 * We need to convert gallium coords to GL coords.
1067 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1068 width
, depth
, format
,
1071 /* get float[4] rgba row from surface */
1072 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, 1,
1075 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
1076 type
, dest
, &ctx
->Pack
, 0);
1079 for (row
= 0; row
< height
; row
++) {
1080 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1081 width
, height
, format
,
1082 type
, slice
, row
, 0);
1084 /* get float[4] rgba row from surface */
1085 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, row
, width
, 1,
1088 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
1089 type
, dest
, &ctx
->Pack
, 0);
1092 map
+= tex_xfer
->layer_stride
;
1101 pipe_transfer_unmap(pipe
, tex_xfer
);
1103 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
1104 pipe_resource_reference(&dst
, NULL
);
1108 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
1114 * Do a CopyTexSubImage operation using a read transfer from the source,
1115 * a write transfer to the destination and get_tile()/put_tile() to access
1116 * the pixels/texels.
1118 * Note: srcY=0=TOP of renderbuffer
1121 fallback_copy_texsubimage(struct gl_context
*ctx
,
1122 struct st_renderbuffer
*strb
,
1123 struct st_texture_image
*stImage
,
1125 GLint destX
, GLint destY
, GLint destZ
,
1126 GLint srcX
, GLint srcY
,
1127 GLsizei width
, GLsizei height
)
1129 struct st_context
*st
= st_context(ctx
);
1130 struct pipe_context
*pipe
= st
->pipe
;
1131 struct pipe_transfer
*src_trans
;
1133 enum pipe_transfer_usage transfer_usage
;
1135 unsigned dst_width
= width
;
1136 unsigned dst_height
= height
;
1137 unsigned dst_depth
= 1;
1139 if (ST_DEBUG
& DEBUG_FALLBACK
)
1140 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1142 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1143 srcY
= strb
->Base
.Height
- srcY
- height
;
1146 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
1147 /* Move y/height to z/depth for 1D array textures. */
1150 dst_depth
= dst_height
;
1154 map
= pipe_transfer_map(pipe
,
1157 strb
->rtt_face
+ strb
->rtt_slice
,
1160 width
, height
, &src_trans
);
1162 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1163 baseFormat
== GL_DEPTH_STENCIL
) &&
1164 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1165 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1167 transfer_usage
= PIPE_TRANSFER_WRITE
;
1169 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
1170 destX
, destY
, destZ
,
1171 dst_width
, dst_height
, dst_depth
);
1173 if (baseFormat
== GL_DEPTH_COMPONENT
||
1174 baseFormat
== GL_DEPTH_STENCIL
) {
1175 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1176 ctx
->Pixel
.DepthBias
!= 0.0F
);
1180 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1181 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1190 data
= malloc(width
* sizeof(uint
));
1193 /* To avoid a large temp memory allocation, do copy row by row */
1194 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1195 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
1197 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1200 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
1201 pipe_put_tile_z(stImage
->transfer
,
1202 texDest
+ row
*stImage
->transfer
->layer_stride
,
1203 0, 0, width
, 1, data
);
1206 pipe_put_tile_z(stImage
->transfer
, texDest
, 0, row
, width
, 1,
1212 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
1220 malloc(width
* height
* 4 * sizeof(GLfloat
));
1222 if (tempSrc
&& texDest
) {
1223 const GLint dims
= 2;
1225 struct gl_texture_image
*texImage
= &stImage
->base
;
1226 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1228 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1229 unpack
.Invert
= GL_TRUE
;
1232 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
1233 dstRowStride
= stImage
->transfer
->layer_stride
;
1236 dstRowStride
= stImage
->transfer
->stride
;
1239 /* get float/RGBA image from framebuffer */
1240 /* XXX this usually involves a lot of int/float conversion.
1241 * try to avoid that someday.
1243 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
1244 util_format_linear(strb
->texture
->format
),
1247 /* Store into texture memory.
1248 * Note that this does some special things such as pixel transfer
1249 * ops and format conversion. In particular, if the dest tex format
1250 * is actually RGBA but the user created the texture as GL_RGB we
1251 * need to fill-in/override the alpha channel with 1.0.
1253 _mesa_texstore(ctx
, dims
,
1254 texImage
->_BaseFormat
,
1255 texImage
->TexFormat
,
1259 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1263 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1269 st_texture_image_unmap(st
, stImage
);
1270 pipe
->transfer_unmap(pipe
, src_trans
);
1275 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1276 * Note that the region to copy has already been clipped so we know we
1277 * won't read from outside the source renderbuffer's bounds.
1279 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1282 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1283 struct gl_texture_image
*texImage
,
1284 GLint destX
, GLint destY
, GLint destZ
,
1285 struct gl_renderbuffer
*rb
,
1286 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
1288 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1289 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1290 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
1291 struct st_context
*st
= st_context(ctx
);
1292 struct pipe_context
*pipe
= st
->pipe
;
1293 struct pipe_screen
*screen
= pipe
->screen
;
1294 struct pipe_blit_info blit
;
1295 enum pipe_format dst_format
;
1296 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1298 GLint srcY0
, srcY1
, yStep
;
1300 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1301 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1305 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
1306 texImage
->TexFormat
)) {
1310 /* The base internal format must match the mesa format, so make sure
1311 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
1313 if (texImage
->_BaseFormat
!=
1314 _mesa_get_format_base_format(texImage
->TexFormat
) ||
1315 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
1319 /* Choose the destination format to match the TexImage behavior. */
1320 dst_format
= util_format_linear(stImage
->pt
->format
);
1321 dst_format
= util_format_luminance_to_red(dst_format
);
1322 dst_format
= util_format_intensity_to_red(dst_format
);
1324 /* See if the destination format is supported. */
1325 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
1326 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
1327 bind
= PIPE_BIND_DEPTH_STENCIL
;
1330 bind
= PIPE_BIND_RENDER_TARGET
;
1334 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
1335 stImage
->pt
->nr_samples
, bind
)) {
1339 /* Y flipping for the main framebuffer. */
1341 srcY1
= strb
->Base
.Height
- srcY
- height
;
1342 srcY0
= srcY1
+ height
;
1347 srcY1
= srcY0
+ height
;
1351 /* Blit the texture.
1352 * This supports flipping, format conversions, and downsampling.
1354 memset(&blit
, 0, sizeof(blit
));
1355 blit
.src
.resource
= strb
->texture
;
1356 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
1357 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
1358 blit
.src
.box
.x
= srcX
;
1359 blit
.src
.box
.y
= srcY0
;
1360 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
1361 blit
.src
.box
.width
= width
;
1362 blit
.src
.box
.height
= srcY1
- srcY0
;
1363 blit
.src
.box
.depth
= 1;
1364 blit
.dst
.resource
= stImage
->pt
;
1365 blit
.dst
.format
= dst_format
;
1366 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
;
1367 blit
.dst
.box
.x
= destX
;
1368 blit
.dst
.box
.y
= destY
;
1369 blit
.dst
.box
.z
= stImage
->base
.Face
+ destZ
;
1370 blit
.dst
.box
.width
= width
;
1371 blit
.dst
.box
.height
= height
;
1372 blit
.dst
.box
.depth
= 1;
1373 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
1374 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1376 /* 1D array textures need special treatment.
1377 * Blit rows from the source to layers in the destination. */
1378 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
1381 for (y
= srcY0
, layer
= 0; layer
< height
; y
+= yStep
, layer
++) {
1383 blit
.src
.box
.height
= 1;
1385 blit
.dst
.box
.height
= 1;
1386 blit
.dst
.box
.z
= destY
+ layer
;
1388 pipe
->blit(pipe
, &blit
);
1392 /* All the other texture targets. */
1393 pipe
->blit(pipe
, &blit
);
1398 /* software fallback */
1399 fallback_copy_texsubimage(ctx
,
1400 strb
, stImage
, texImage
->_BaseFormat
,
1401 destX
, destY
, destZ
,
1402 srcX
, srcY
, width
, height
);
1407 * Copy image data from stImage into the texture object 'stObj' at level
1411 copy_image_data_to_texture(struct st_context
*st
,
1412 struct st_texture_object
*stObj
,
1414 struct st_texture_image
*stImage
)
1418 const struct gl_texture_image
*dstImage
=
1419 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
1421 assert(dstImage
->Width
== stImage
->base
.Width
);
1422 assert(dstImage
->Height
== stImage
->base
.Height
);
1423 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1427 /* Copy potentially with the blitter:
1430 if (stImage
->pt
->last_level
== 0)
1433 src_level
= stImage
->base
.Level
;
1435 assert(src_level
<= stImage
->pt
->last_level
);
1436 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
1437 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
1438 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
1439 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
1440 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
1441 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
1443 st_texture_image_copy(st
->pipe
,
1444 stObj
->pt
, dstLevel
, /* dest texture, level */
1445 stImage
->pt
, src_level
, /* src texture, level */
1446 stImage
->base
.Face
);
1448 pipe_resource_reference(&stImage
->pt
, NULL
);
1450 else if (stImage
->TexData
) {
1451 /* Copy from malloc'd memory */
1452 /* XXX this should be re-examined/tested with a compressed format */
1453 GLuint blockSize
= util_format_get_blocksize(stObj
->pt
->format
);
1454 GLuint srcRowStride
= stImage
->base
.Width
* blockSize
;
1455 GLuint srcSliceStride
= stImage
->base
.Height
* srcRowStride
;
1456 st_texture_image_data(st
,
1463 _mesa_align_free(stImage
->TexData
);
1464 stImage
->TexData
= NULL
;
1467 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1472 * Called during state validation. When this function is finished,
1473 * the texture object should be ready for rendering.
1474 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1477 st_finalize_texture(struct gl_context
*ctx
,
1478 struct pipe_context
*pipe
,
1479 struct gl_texture_object
*tObj
)
1481 struct st_context
*st
= st_context(ctx
);
1482 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1483 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1485 struct st_texture_image
*firstImage
;
1486 enum pipe_format firstImageFormat
;
1487 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1489 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
1490 /* The texture is complete and we know exactly how many mipmap levels
1491 * are present/needed. This is conditional because we may be called
1492 * from the st_generate_mipmap() function when the texture object is
1493 * incomplete. In that case, we'll have set stObj->lastLevel before
1496 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1497 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1498 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1500 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1503 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
1504 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
1506 if (st_obj
->buffer
!= stObj
->pt
) {
1507 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
1508 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
1509 stObj
->width0
= stObj
->pt
->width0
/ _mesa_get_format_bytes(tObj
->_BufferObjectFormat
);
1517 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1520 /* If both firstImage and stObj point to a texture which can contain
1521 * all active images, favour firstImage. Note that because of the
1522 * completeness requirement, we know that the image dimensions
1525 if (firstImage
->pt
&&
1526 firstImage
->pt
!= stObj
->pt
&&
1527 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1528 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1529 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
1532 /* Find gallium format for the Mesa texture */
1533 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1535 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1537 GLuint width
, height
, depth
;
1538 if (!guess_base_level_size(stObj
->base
.Target
,
1539 firstImage
->base
.Width2
,
1540 firstImage
->base
.Height2
,
1541 firstImage
->base
.Depth2
,
1542 firstImage
->base
.Level
,
1543 &width
, &height
, &depth
)) {
1544 width
= stObj
->width0
;
1545 height
= stObj
->height0
;
1546 depth
= stObj
->depth0
;
1548 /* convert GL dims to Gallium dims */
1549 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1550 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1553 /* If we already have a gallium texture, check that it matches the texture
1554 * object's format, target, size, num_levels, etc.
1557 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1558 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1559 stObj
->pt
->last_level
< stObj
->lastLevel
||
1560 stObj
->pt
->width0
!= ptWidth
||
1561 stObj
->pt
->height0
!= ptHeight
||
1562 stObj
->pt
->depth0
!= ptDepth
||
1563 stObj
->pt
->array_size
!= ptLayers
)
1565 /* The gallium texture does not match the Mesa texture so delete the
1566 * gallium texture now. We'll make a new one below.
1568 pipe_resource_reference(&stObj
->pt
, NULL
);
1569 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
1570 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1574 /* May need to create a new gallium texture:
1577 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1579 stObj
->pt
= st_texture_create(st
,
1580 gl_target_to_pipe(stObj
->base
.Target
),
1590 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1595 /* Pull in any images not in the object's texture:
1597 for (face
= 0; face
< nr_faces
; face
++) {
1599 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1600 struct st_texture_image
*stImage
=
1601 st_texture_image(stObj
->base
.Image
[face
][level
]);
1603 /* Need to import images in main memory or held in other textures.
1605 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1607 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1608 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1609 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
1610 /* src image fits expected dest mipmap level size */
1611 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1622 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1623 * for a whole mipmap stack.
1626 st_AllocTextureStorage(struct gl_context
*ctx
,
1627 struct gl_texture_object
*texObj
,
1628 GLsizei levels
, GLsizei width
,
1629 GLsizei height
, GLsizei depth
)
1631 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
1632 struct st_context
*st
= st_context(ctx
);
1633 struct st_texture_object
*stObj
= st_texture_object(texObj
);
1634 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
1635 enum pipe_format fmt
;
1640 /* Save the level=0 dimensions */
1641 stObj
->width0
= width
;
1642 stObj
->height0
= height
;
1643 stObj
->depth0
= depth
;
1644 stObj
->lastLevel
= levels
- 1;
1646 fmt
= st_mesa_format_to_pipe_format(texObj
->Image
[0][0]->TexFormat
);
1648 bindings
= default_bindings(st
, fmt
);
1650 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
1651 width
, height
, depth
,
1652 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1654 stObj
->pt
= st_texture_create(st
,
1655 gl_target_to_pipe(texObj
->Target
),
1666 /* Set image resource pointers */
1667 for (level
= 0; level
< levels
; level
++) {
1669 for (face
= 0; face
< numFaces
; face
++) {
1670 struct st_texture_image
*stImage
=
1671 st_texture_image(texObj
->Image
[face
][level
]);
1672 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1681 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
1682 GLint level
, gl_format format
,
1683 GLint width
, GLint height
,
1684 GLint depth
, GLint border
)
1686 struct st_context
*st
= st_context(ctx
);
1687 struct pipe_context
*pipe
= st
->pipe
;
1689 if (width
== 0 || height
== 0 || depth
== 0) {
1690 /* zero-sized images are legal, and always fit! */
1694 if (pipe
->screen
->can_create_resource
) {
1695 /* Ask the gallium driver if the texture is too large */
1696 struct gl_texture_object
*texObj
=
1697 _mesa_get_current_tex_object(ctx
, target
);
1698 struct pipe_resource pt
;
1700 /* Setup the pipe_resource object
1702 memset(&pt
, 0, sizeof(pt
));
1704 pt
.target
= gl_target_to_pipe(target
);
1705 pt
.format
= st_mesa_format_to_pipe_format(format
);
1707 st_gl_texture_dims_to_pipe_dims(target
,
1708 width
, height
, depth
,
1709 &pt
.width0
, &pt
.height0
,
1710 &pt
.depth0
, &pt
.array_size
);
1712 if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
1713 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
1714 /* assume just one mipmap level */
1718 /* assume a full set of mipmaps */
1719 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
1722 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
1725 /* Use core Mesa fallback */
1726 return _mesa_test_proxy_teximage(ctx
, target
, level
, format
,
1727 width
, height
, depth
, border
);
1733 st_init_texture_functions(struct dd_function_table
*functions
)
1735 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1736 functions
->QuerySamplesForFormat
= st_QuerySamplesForFormat
;
1737 functions
->TexImage
= st_TexImage
;
1738 functions
->TexSubImage
= st_TexSubImage
;
1739 functions
->CompressedTexSubImage
= _mesa_store_compressed_texsubimage
;
1740 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
1741 functions
->GenerateMipmap
= st_generate_mipmap
;
1743 functions
->GetTexImage
= st_GetTexImage
;
1745 /* compressed texture functions */
1746 functions
->CompressedTexImage
= st_CompressedTexImage
;
1747 functions
->GetCompressedTexImage
= _mesa_get_compressed_teximage
;
1749 functions
->NewTextureObject
= st_NewTextureObject
;
1750 functions
->NewTextureImage
= st_NewTextureImage
;
1751 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
1752 functions
->DeleteTexture
= st_DeleteTextureObject
;
1753 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
1754 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
1755 functions
->MapTextureImage
= st_MapTextureImage
;
1756 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
1758 /* XXX Temporary until we can query pipe's texture sizes */
1759 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
1761 functions
->AllocTextureStorage
= st_AllocTextureStorage
;