1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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/bufferobj.h"
29 #include "main/enums.h"
30 #include "main/fbobject.h"
31 #include "main/formats.h"
32 #include "main/format_utils.h"
33 #include "main/glformats.h"
34 #include "main/image.h"
35 #include "main/imports.h"
36 #include "main/macros.h"
37 #include "main/mipmap.h"
38 #include "main/pack.h"
40 #include "main/pixeltransfer.h"
41 #include "main/texcompress.h"
42 #include "main/texcompress_etc.h"
43 #include "main/texgetimage.h"
44 #include "main/teximage.h"
45 #include "main/texobj.h"
46 #include "main/texstore.h"
48 #include "state_tracker/st_debug.h"
49 #include "state_tracker/st_context.h"
50 #include "state_tracker/st_cb_fbo.h"
51 #include "state_tracker/st_cb_flush.h"
52 #include "state_tracker/st_cb_texture.h"
53 #include "state_tracker/st_cb_bufferobjects.h"
54 #include "state_tracker/st_format.h"
55 #include "state_tracker/st_texture.h"
56 #include "state_tracker/st_gen_mipmap.h"
57 #include "state_tracker/st_atom.h"
59 #include "pipe/p_context.h"
60 #include "pipe/p_defines.h"
61 #include "util/u_inlines.h"
62 #include "pipe/p_shader_tokens.h"
63 #include "util/u_tile.h"
64 #include "util/u_format.h"
65 #include "util/u_surface.h"
66 #include "util/u_sampler.h"
67 #include "util/u_math.h"
68 #include "util/u_box.h"
70 #define DBG if (0) printf
73 enum pipe_texture_target
74 gl_target_to_pipe(GLenum target
)
78 case GL_PROXY_TEXTURE_1D
:
79 return PIPE_TEXTURE_1D
;
81 case GL_PROXY_TEXTURE_2D
:
82 case GL_TEXTURE_EXTERNAL_OES
:
83 case GL_TEXTURE_2D_MULTISAMPLE
:
84 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
85 return PIPE_TEXTURE_2D
;
86 case GL_TEXTURE_RECTANGLE_NV
:
87 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
88 return PIPE_TEXTURE_RECT
;
90 case GL_PROXY_TEXTURE_3D
:
91 return PIPE_TEXTURE_3D
;
92 case GL_TEXTURE_CUBE_MAP_ARB
:
93 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
94 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
95 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
96 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
97 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
98 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
99 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
100 return PIPE_TEXTURE_CUBE
;
101 case GL_TEXTURE_1D_ARRAY_EXT
:
102 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
103 return PIPE_TEXTURE_1D_ARRAY
;
104 case GL_TEXTURE_2D_ARRAY_EXT
:
105 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
106 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
107 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
108 return PIPE_TEXTURE_2D_ARRAY
;
109 case GL_TEXTURE_BUFFER
:
111 case GL_TEXTURE_CUBE_MAP_ARRAY
:
112 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
113 return PIPE_TEXTURE_CUBE_ARRAY
;
121 /** called via ctx->Driver.NewTextureImage() */
122 static struct gl_texture_image
*
123 st_NewTextureImage(struct gl_context
* ctx
)
125 DBG("%s\n", __FUNCTION__
);
127 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
131 /** called via ctx->Driver.DeleteTextureImage() */
133 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
135 /* nothing special (yet) for st_texture_image */
136 _mesa_delete_texture_image(ctx
, img
);
140 /** called via ctx->Driver.NewTextureObject() */
141 static struct gl_texture_object
*
142 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
144 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
146 DBG("%s\n", __FUNCTION__
);
147 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
152 /** called via ctx->Driver.DeleteTextureObject() */
154 st_DeleteTextureObject(struct gl_context
*ctx
,
155 struct gl_texture_object
*texObj
)
157 struct st_context
*st
= st_context(ctx
);
158 struct st_texture_object
*stObj
= st_texture_object(texObj
);
160 pipe_resource_reference(&stObj
->pt
, NULL
);
161 st_texture_release_all_sampler_views(st
, stObj
);
162 st_texture_free_sampler_views(stObj
);
163 _mesa_delete_texture_object(ctx
, texObj
);
167 /** called via ctx->Driver.FreeTextureImageBuffer() */
169 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
170 struct gl_texture_image
*texImage
)
172 struct st_texture_image
*stImage
= st_texture_image(texImage
);
174 DBG("%s\n", __FUNCTION__
);
177 pipe_resource_reference(&stImage
->pt
, NULL
);
180 _mesa_align_free(stImage
->TexData
);
181 stImage
->TexData
= NULL
;
183 free(stImage
->transfer
);
184 stImage
->transfer
= NULL
;
185 stImage
->num_transfers
= 0;
189 /** called via ctx->Driver.MapTextureImage() */
191 st_MapTextureImage(struct gl_context
*ctx
,
192 struct gl_texture_image
*texImage
,
193 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
195 GLubyte
**mapOut
, GLint
*rowStrideOut
)
197 struct st_context
*st
= st_context(ctx
);
198 struct st_texture_image
*stImage
= st_texture_image(texImage
);
201 struct pipe_transfer
*transfer
;
204 if (mode
& GL_MAP_READ_BIT
)
205 pipeMode
|= PIPE_TRANSFER_READ
;
206 if (mode
& GL_MAP_WRITE_BIT
)
207 pipeMode
|= PIPE_TRANSFER_WRITE
;
208 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
209 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
211 map
= st_texture_image_map(st
, stImage
, pipeMode
, x
, y
, slice
, w
, h
, 1,
214 if ((_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
215 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
)) {
216 /* ETC isn't supported by gallium and it's represented
217 * by uncompressed formats. Only write transfers with precompressed
218 * data are supported by ES3, which makes this really simple.
220 * Just create a temporary storage where the ETC texture will
221 * be stored. It will be decompressed in the Unmap function.
223 unsigned z
= transfer
->box
.z
;
224 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
226 itransfer
->temp_data
=
227 malloc(_mesa_format_image_size(texImage
->TexFormat
, w
, h
, 1));
228 itransfer
->temp_stride
=
229 _mesa_format_row_stride(texImage
->TexFormat
, w
);
230 itransfer
->map
= map
;
232 *mapOut
= itransfer
->temp_data
;
233 *rowStrideOut
= itransfer
->temp_stride
;
236 /* supported mapping */
238 *rowStrideOut
= transfer
->stride
;
248 /** called via ctx->Driver.UnmapTextureImage() */
250 st_UnmapTextureImage(struct gl_context
*ctx
,
251 struct gl_texture_image
*texImage
,
254 struct st_context
*st
= st_context(ctx
);
255 struct st_texture_image
*stImage
= st_texture_image(texImage
);
257 if ((_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
258 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
)) {
259 /* Decompress the ETC texture to the mapped one. */
260 unsigned z
= slice
+ stImage
->base
.Face
;
261 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
262 struct pipe_transfer
*transfer
= itransfer
->transfer
;
264 assert(z
== transfer
->box
.z
);
266 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
267 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
268 itransfer
->temp_data
,
269 itransfer
->temp_stride
,
270 transfer
->box
.width
, transfer
->box
.height
);
273 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
274 itransfer
->temp_data
, itransfer
->temp_stride
,
275 transfer
->box
.width
, transfer
->box
.height
,
276 texImage
->TexFormat
);
279 free(itransfer
->temp_data
);
280 itransfer
->temp_data
= NULL
;
281 itransfer
->temp_stride
= 0;
285 st_texture_image_unmap(st
, stImage
, slice
);
290 * Return default texture resource binding bitmask for the given format.
293 default_bindings(struct st_context
*st
, enum pipe_format format
)
295 struct pipe_screen
*screen
= st
->pipe
->screen
;
296 const unsigned target
= PIPE_TEXTURE_2D
;
299 if (util_format_is_depth_or_stencil(format
))
300 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
302 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
304 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
308 format
= util_format_linear(format
);
310 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
313 return PIPE_BIND_SAMPLER_VIEW
;
319 * Given the size of a mipmap image, try to compute the size of the level=0
322 * Note that this isn't always accurate for odd-sized, non-POW textures.
323 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
325 * \return GL_TRUE for success, GL_FALSE for failure
328 guess_base_level_size(GLenum target
,
329 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
330 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
337 /* Guess the size of the base level.
338 * Depending on the image's size, we can't always make a guess here.
342 case GL_TEXTURE_1D_ARRAY
:
347 case GL_TEXTURE_2D_ARRAY
:
348 /* We can't make a good guess here, because the base level dimensions
351 if (width
== 1 || height
== 1) {
358 case GL_TEXTURE_CUBE_MAP
:
359 case GL_TEXTURE_CUBE_MAP_ARRAY
:
365 /* We can't make a good guess here, because the base level dimensions
368 if (width
== 1 || height
== 1 || depth
== 1) {
376 case GL_TEXTURE_RECTANGLE
:
393 * Try to allocate a pipe_resource object for the given st_texture_object.
395 * We use the given st_texture_image as a clue to determine the size of the
396 * mipmap image at level=0.
398 * \return GL_TRUE for success, GL_FALSE if out of memory.
401 guess_and_alloc_texture(struct st_context
*st
,
402 struct st_texture_object
*stObj
,
403 const struct st_texture_image
*stImage
)
405 GLuint lastLevel
, width
, height
, depth
;
407 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
408 enum pipe_format fmt
;
410 DBG("%s\n", __FUNCTION__
);
414 if (!guess_base_level_size(stObj
->base
.Target
,
415 stImage
->base
.Width2
,
416 stImage
->base
.Height2
,
417 stImage
->base
.Depth2
,
419 &width
, &height
, &depth
)) {
420 /* we can't determine the image size at level=0 */
421 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
422 /* this is not an out of memory error */
426 /* At this point, (width x height x depth) is the expected size of
427 * the level=0 mipmap image.
430 /* Guess a reasonable value for lastLevel. With OpenGL we have no
431 * idea how many mipmap levels will be in a texture until we start
432 * to render with it. Make an educated guess here but be prepared
433 * to re-allocating a texture buffer with space for more (or fewer)
434 * mipmap levels later.
436 if ((stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
437 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
438 (stObj
->base
.BaseLevel
== 0 &&
439 stObj
->base
.MaxLevel
== 0) ||
440 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
441 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
442 !stObj
->base
.GenerateMipmap
&&
443 stImage
->base
.Level
== 0) {
444 /* only alloc space for a single mipmap level */
448 /* alloc space for a full mipmap */
449 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
450 width
, height
, depth
) - 1;
453 /* Save the level=0 dimensions */
454 stObj
->width0
= width
;
455 stObj
->height0
= height
;
456 stObj
->depth0
= depth
;
458 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
460 bindings
= default_bindings(st
, fmt
);
462 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
463 width
, height
, depth
,
464 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
466 stObj
->pt
= st_texture_create(st
,
467 gl_target_to_pipe(stObj
->base
.Target
),
476 stObj
->lastLevel
= lastLevel
;
478 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
480 return stObj
->pt
!= NULL
;
485 * Called via ctx->Driver.AllocTextureImageBuffer().
486 * If the texture object/buffer already has space for the indicated image,
487 * we're done. Otherwise, allocate memory for the new texture image.
490 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
491 struct gl_texture_image
*texImage
)
493 struct st_context
*st
= st_context(ctx
);
494 struct st_texture_image
*stImage
= st_texture_image(texImage
);
495 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
496 const GLuint level
= texImage
->Level
;
497 GLuint width
= texImage
->Width
;
498 GLuint height
= texImage
->Height
;
499 GLuint depth
= texImage
->Depth
;
501 DBG("%s\n", __FUNCTION__
);
503 assert(!stImage
->TexData
);
504 assert(!stImage
->pt
); /* xxx this might be wrong */
506 /* Look if the parent texture object has space for this image */
508 level
<= stObj
->pt
->last_level
&&
509 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
510 /* this image will fit in the existing texture object's memory */
511 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
515 /* The parent texture object does not have space for this image */
517 pipe_resource_reference(&stObj
->pt
, NULL
);
518 st_texture_release_all_sampler_views(st
, stObj
);
520 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
521 /* Probably out of memory.
522 * Try flushing any pending rendering, then retry.
525 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
526 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
532 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
533 /* The image will live in the object's mipmap memory */
534 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
539 /* Create a new, temporary texture/resource/buffer to hold this
540 * one texture image. Note that when we later access this image
541 * (either for mapping or copying) we'll want to always specify
542 * mipmap level=0, even if the image represents some other mipmap
545 enum pipe_format format
=
546 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
547 GLuint bindings
= default_bindings(st
, format
);
548 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
550 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
551 width
, height
, depth
,
552 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
554 stImage
->pt
= st_texture_create(st
,
555 gl_target_to_pipe(stObj
->base
.Target
),
563 return stImage
->pt
!= NULL
;
569 * Preparation prior to glTexImage. Basically check the 'surface_based'
570 * field and switch to a "normal" tex image if necessary.
573 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
574 GLenum format
, GLenum type
)
576 struct gl_texture_object
*texObj
= texImage
->TexObject
;
577 struct st_texture_object
*stObj
= st_texture_object(texObj
);
579 /* switch to "normal" */
580 if (stObj
->surface_based
) {
581 const GLenum target
= texObj
->Target
;
582 const GLuint level
= texImage
->Level
;
583 mesa_format texFormat
;
585 _mesa_clear_texture_object(ctx
, texObj
);
586 pipe_resource_reference(&stObj
->pt
, NULL
);
588 /* oops, need to init this image again */
589 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
590 texImage
->InternalFormat
, format
,
593 _mesa_init_teximage_fields(ctx
, texImage
,
594 texImage
->Width
, texImage
->Height
,
595 texImage
->Depth
, texImage
->Border
,
596 texImage
->InternalFormat
, texFormat
);
598 stObj
->surface_based
= GL_FALSE
;
604 * Return a writemask for the gallium blit. The parameters can be base
605 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
608 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
611 case GL_DEPTH_STENCIL
:
613 case GL_DEPTH_STENCIL
:
615 case GL_DEPTH_COMPONENT
:
617 case GL_STENCIL_INDEX
:
624 case GL_DEPTH_COMPONENT
:
626 case GL_DEPTH_STENCIL
:
627 case GL_DEPTH_COMPONENT
:
634 case GL_STENCIL_INDEX
:
636 case GL_STENCIL_INDEX
:
644 return PIPE_MASK_RGBA
;
650 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
651 struct gl_texture_image
*texImage
,
652 GLint xoffset
, GLint yoffset
, GLint zoffset
,
653 GLint width
, GLint height
, GLint depth
,
654 GLenum format
, GLenum type
, const void *pixels
,
655 const struct gl_pixelstore_attrib
*unpack
)
657 struct st_context
*st
= st_context(ctx
);
658 struct st_texture_image
*stImage
= st_texture_image(texImage
);
659 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
660 struct pipe_context
*pipe
= st
->pipe
;
661 struct pipe_screen
*screen
= pipe
->screen
;
662 struct pipe_resource
*dst
= stImage
->pt
;
663 struct pipe_resource
*src
= NULL
;
664 struct pipe_resource src_templ
;
665 struct pipe_transfer
*transfer
;
666 struct pipe_blit_info blit
;
667 enum pipe_format src_format
, dst_format
;
668 mesa_format mesa_src_format
;
669 GLenum gl_target
= texImage
->TexObject
->Target
;
673 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
674 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
676 if (!st
->prefer_blit_based_texture_transfer
) {
684 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
685 * blit implementation in some drivers. */
686 if (format
== GL_DEPTH_STENCIL
) {
690 /* If the base internal format and the texture format don't match,
691 * we can't use blit-based TexSubImage. */
692 if (texImage
->_BaseFormat
!=
693 _mesa_get_format_base_format(texImage
->TexFormat
)) {
697 /* See if the texture format already matches the format and type,
698 * in which case the memcpy-based fast path will likely be used and
699 * we don't have to blit. */
700 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
701 type
, unpack
->SwapBytes
)) {
705 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
706 bind
= PIPE_BIND_DEPTH_STENCIL
;
708 bind
= PIPE_BIND_RENDER_TARGET
;
710 /* See if the destination format is supported.
711 * For luminance and intensity, only the red channel is stored there. */
712 dst_format
= util_format_linear(dst
->format
);
713 dst_format
= util_format_luminance_to_red(dst_format
);
714 dst_format
= util_format_intensity_to_red(dst_format
);
717 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
718 dst
->nr_samples
, bind
)) {
722 /* Choose the source format. */
723 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
724 format
, type
, unpack
->SwapBytes
);
729 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
731 /* There is no reason to do this if we cannot use memcpy for the temporary
732 * source texture at least. This also takes transfer ops into account,
734 if (!_mesa_texstore_can_use_memcpy(ctx
,
735 _mesa_get_format_base_format(mesa_src_format
),
736 mesa_src_format
, format
, type
, unpack
)) {
740 /* TexSubImage only sets a single cubemap face. */
741 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
742 gl_target
= GL_TEXTURE_2D
;
745 /* Initialize the source texture description. */
746 memset(&src_templ
, 0, sizeof(src_templ
));
747 src_templ
.target
= gl_target_to_pipe(gl_target
);
748 src_templ
.format
= src_format
;
749 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
750 src_templ
.usage
= PIPE_USAGE_STAGING
;
752 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
753 &src_templ
.width0
, &src_templ
.height0
,
754 &src_templ
.depth0
, &src_templ
.array_size
);
756 /* Check for NPOT texture support. */
757 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
758 (!util_is_power_of_two(src_templ
.width0
) ||
759 !util_is_power_of_two(src_templ
.height0
) ||
760 !util_is_power_of_two(src_templ
.depth0
))) {
764 /* Create the source texture. */
765 src
= screen
->resource_create(screen
, &src_templ
);
770 /* Map source pixels. */
771 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
772 format
, type
, pixels
, unpack
,
775 /* This is a GL error. */
776 pipe_resource_reference(&src
, NULL
);
780 /* From now on, we need the gallium representation of dimensions. */
781 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
788 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
789 width
, height
, depth
, &transfer
);
791 _mesa_unmap_teximage_pbo(ctx
, unpack
);
792 pipe_resource_reference(&src
, NULL
);
796 /* Upload pixels (just memcpy). */
798 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
801 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
802 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
803 /* 1D array textures.
804 * We need to convert gallium coords to GL coords.
806 GLvoid
*src
= _mesa_image_address3d(unpack
, pixels
,
807 width
, depth
, format
,
809 memcpy(map
, src
, bytesPerRow
);
812 ubyte
*slice_map
= map
;
814 for (row
= 0; row
< (unsigned) height
; row
++) {
815 GLvoid
*src
= _mesa_image_address3d(unpack
, pixels
,
816 width
, height
, format
,
817 type
, slice
, row
, 0);
818 memcpy(slice_map
, src
, bytesPerRow
);
819 slice_map
+= transfer
->stride
;
822 map
+= transfer
->layer_stride
;
826 pipe_transfer_unmap(pipe
, transfer
);
827 _mesa_unmap_teximage_pbo(ctx
, unpack
);
830 memset(&blit
, 0, sizeof(blit
));
831 blit
.src
.resource
= src
;
833 blit
.src
.format
= src_format
;
834 blit
.dst
.resource
= dst
;
835 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
836 blit
.dst
.format
= dst_format
;
837 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
838 blit
.dst
.box
.x
= xoffset
;
839 blit
.dst
.box
.y
= yoffset
;
840 blit
.dst
.box
.z
= zoffset
+ texImage
->Face
+ texImage
->TexObject
->MinLayer
;
841 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
842 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
843 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
844 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
845 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
846 blit
.scissor_enable
= FALSE
;
848 st
->pipe
->blit(st
->pipe
, &blit
);
850 pipe_resource_reference(&src
, NULL
);
854 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
855 width
, height
, depth
, format
, type
, pixels
,
860 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
861 struct gl_texture_image
*texImage
,
862 GLenum format
, GLenum type
, const void *pixels
,
863 const struct gl_pixelstore_attrib
*unpack
)
865 assert(dims
== 1 || dims
== 2 || dims
== 3);
867 prep_teximage(ctx
, texImage
, format
, type
);
869 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
872 /* allocate storage for texture data */
873 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
874 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
878 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
879 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
880 format
, type
, pixels
, unpack
);
885 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
886 struct gl_texture_image
*texImage
,
887 GLsizei imageSize
, const GLvoid
*data
)
889 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
890 _mesa_store_compressed_teximage(ctx
, dims
, texImage
, imageSize
, data
);
897 * Called via ctx->Driver.GetTexImage()
899 * This uses a blit to copy the texture to a texture format which matches
900 * the format and type combo and then a fast read-back is done using memcpy.
901 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
902 * a format which matches the swizzling.
904 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
906 * NOTE: Drivers usually do a blit to convert between tiled and linear
907 * texture layouts during texture uploads/downloads, so the blit
908 * we do here should be free in such cases.
911 st_GetTexImage(struct gl_context
* ctx
,
912 GLenum format
, GLenum type
, GLvoid
* pixels
,
913 struct gl_texture_image
*texImage
)
915 struct st_context
*st
= st_context(ctx
);
916 struct pipe_context
*pipe
= st
->pipe
;
917 struct pipe_screen
*screen
= pipe
->screen
;
918 GLuint width
= texImage
->Width
;
919 GLuint height
= texImage
->Height
;
920 GLuint depth
= texImage
->Depth
;
921 struct st_texture_image
*stImage
= st_texture_image(texImage
);
922 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
923 struct pipe_resource
*src
= stObj
->pt
;
924 struct pipe_resource
*dst
= NULL
;
925 struct pipe_resource dst_templ
;
926 enum pipe_format dst_format
, src_format
;
927 mesa_format mesa_format
;
928 GLenum gl_target
= texImage
->TexObject
->Target
;
929 enum pipe_texture_target pipe_target
;
930 struct pipe_blit_info blit
;
931 unsigned bind
= PIPE_BIND_TRANSFER_READ
;
932 struct pipe_transfer
*tex_xfer
;
934 boolean done
= FALSE
;
936 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
937 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
939 if (!st
->prefer_blit_based_texture_transfer
&&
940 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
941 /* Try to avoid the fallback if we're doing texture decompression here */
945 if (!stImage
->pt
|| !src
) {
949 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
950 * due to an incomplete stencil blit implementation in some drivers. */
951 if (format
== GL_DEPTH_STENCIL
) {
955 /* If the base internal format and the texture format don't match, we have
956 * to fall back to _mesa_GetTexImage_sw. */
957 if (texImage
->_BaseFormat
!=
958 _mesa_get_format_base_format(texImage
->TexFormat
)) {
962 /* See if the texture format already matches the format and type,
963 * in which case the memcpy-based fast path will be used. */
964 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
965 type
, ctx
->Pack
.SwapBytes
)) {
969 /* Convert the source format to what is expected by GetTexImage
970 * and see if it's supported.
972 * This only applies to glGetTexImage:
973 * - Luminance must be returned as (L,0,0,1).
974 * - Luminance alpha must be returned as (L,0,0,A).
975 * - Intensity must be returned as (I,0,0,1)
977 if (stObj
->surface_based
)
978 src_format
= util_format_linear(stObj
->surface_format
);
980 src_format
= util_format_linear(src
->format
);
981 src_format
= util_format_luminance_to_red(src_format
);
982 src_format
= util_format_intensity_to_red(src_format
);
985 !screen
->is_format_supported(screen
, src_format
, src
->target
,
987 PIPE_BIND_SAMPLER_VIEW
)) {
991 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
992 bind
|= PIPE_BIND_DEPTH_STENCIL
;
994 bind
|= PIPE_BIND_RENDER_TARGET
;
996 /* GetTexImage only returns a single face for cubemaps. */
997 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
998 gl_target
= GL_TEXTURE_2D
;
1000 pipe_target
= gl_target_to_pipe(gl_target
);
1002 /* Choose the destination format by finding the best match
1003 * for the format+type combo. */
1004 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1005 ctx
->Pack
.SwapBytes
);
1007 if (dst_format
== PIPE_FORMAT_NONE
) {
1008 GLenum dst_glformat
;
1010 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1011 * where decompression with a blit is always preferred. */
1012 if (!util_format_is_compressed(src
->format
)) {
1016 /* Set the appropriate format for the decompressed texture.
1017 * Luminance and sRGB formats shouldn't appear here.*/
1018 switch (src_format
) {
1019 case PIPE_FORMAT_DXT1_RGB
:
1020 case PIPE_FORMAT_DXT1_RGBA
:
1021 case PIPE_FORMAT_DXT3_RGBA
:
1022 case PIPE_FORMAT_DXT5_RGBA
:
1023 case PIPE_FORMAT_RGTC1_UNORM
:
1024 case PIPE_FORMAT_RGTC2_UNORM
:
1025 case PIPE_FORMAT_ETC1_RGB8
:
1026 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1027 dst_glformat
= GL_RGBA8
;
1029 case PIPE_FORMAT_RGTC1_SNORM
:
1030 case PIPE_FORMAT_RGTC2_SNORM
:
1031 if (!ctx
->Extensions
.EXT_texture_snorm
)
1033 dst_glformat
= GL_RGBA8_SNORM
;
1035 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1036 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1037 if (!ctx
->Extensions
.ARB_texture_float
)
1039 dst_glformat
= GL_RGBA32F
;
1046 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
1047 pipe_target
, 0, bind
, FALSE
);
1049 if (dst_format
== PIPE_FORMAT_NONE
) {
1050 /* unable to get an rgba format!?! */
1055 /* create the destination texture */
1056 memset(&dst_templ
, 0, sizeof(dst_templ
));
1057 dst_templ
.target
= pipe_target
;
1058 dst_templ
.format
= dst_format
;
1059 dst_templ
.bind
= bind
;
1060 dst_templ
.usage
= PIPE_USAGE_STAGING
;
1062 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1063 &dst_templ
.width0
, &dst_templ
.height0
,
1064 &dst_templ
.depth0
, &dst_templ
.array_size
);
1066 dst
= screen
->resource_create(screen
, &dst_templ
);
1071 /* From now on, we need the gallium representation of dimensions. */
1072 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1077 memset(&blit
, 0, sizeof(blit
));
1078 blit
.src
.resource
= src
;
1079 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
1080 blit
.src
.format
= src_format
;
1081 blit
.dst
.resource
= dst
;
1083 blit
.dst
.format
= dst
->format
;
1084 blit
.src
.box
.x
= blit
.dst
.box
.x
= 0;
1085 blit
.src
.box
.y
= blit
.dst
.box
.y
= 0;
1086 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1088 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1089 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1090 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1091 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
1092 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1093 blit
.scissor_enable
= FALSE
;
1095 /* blit/render/decompress */
1096 st
->pipe
->blit(st
->pipe
, &blit
);
1098 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
1100 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
1101 0, 0, 0, width
, height
, depth
, &tex_xfer
);
1106 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
1108 /* copy/pack data into user buffer */
1109 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
1110 ctx
->Pack
.SwapBytes
)) {
1112 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
1115 for (slice
= 0; slice
< depth
; slice
++) {
1116 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1117 /* 1D array textures.
1118 * We need to convert gallium coords to GL coords.
1120 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1121 width
, depth
, format
,
1123 memcpy(dest
, map
, bytesPerRow
);
1126 ubyte
*slice_map
= map
;
1128 for (row
= 0; row
< height
; row
++) {
1129 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1130 width
, height
, format
,
1131 type
, slice
, row
, 0);
1132 memcpy(dest
, slice_map
, bytesPerRow
);
1133 slice_map
+= tex_xfer
->stride
;
1136 map
+= tex_xfer
->layer_stride
;
1140 /* format translation via floats */
1143 uint32_t dstMesaFormat
;
1144 int dstStride
, srcStride
;
1146 assert(util_format_is_compressed(src
->format
));
1148 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
1153 if (ST_DEBUG
& DEBUG_FALLBACK
)
1154 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
1156 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
1157 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
1158 srcStride
= 4 * width
* sizeof(GLfloat
);
1159 for (slice
= 0; slice
< depth
; slice
++) {
1160 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1161 /* 1D array textures.
1162 * We need to convert gallium coords to GL coords.
1164 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1165 width
, depth
, format
,
1168 /* get float[4] rgba row from surface */
1169 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, 1,
1172 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
1173 rgba
, RGBA32_FLOAT
, srcStride
,
1177 for (row
= 0; row
< height
; row
++) {
1178 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1179 width
, height
, format
,
1180 type
, slice
, row
, 0);
1182 /* get float[4] rgba row from surface */
1183 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, row
, width
, 1,
1186 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
1187 rgba
, RGBA32_FLOAT
, srcStride
,
1191 map
+= tex_xfer
->layer_stride
;
1200 pipe_transfer_unmap(pipe
, tex_xfer
);
1202 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
1203 pipe_resource_reference(&dst
, NULL
);
1207 _mesa_GetTexImage_sw(ctx
, format
, type
, pixels
, texImage
);
1213 * Do a CopyTexSubImage operation using a read transfer from the source,
1214 * a write transfer to the destination and get_tile()/put_tile() to access
1215 * the pixels/texels.
1217 * Note: srcY=0=TOP of renderbuffer
1220 fallback_copy_texsubimage(struct gl_context
*ctx
,
1221 struct st_renderbuffer
*strb
,
1222 struct st_texture_image
*stImage
,
1224 GLint destX
, GLint destY
, GLint slice
,
1225 GLint srcX
, GLint srcY
,
1226 GLsizei width
, GLsizei height
)
1228 struct st_context
*st
= st_context(ctx
);
1229 struct pipe_context
*pipe
= st
->pipe
;
1230 struct pipe_transfer
*src_trans
;
1232 enum pipe_transfer_usage transfer_usage
;
1234 unsigned dst_width
= width
;
1235 unsigned dst_height
= height
;
1236 unsigned dst_depth
= 1;
1237 struct pipe_transfer
*transfer
;
1239 if (ST_DEBUG
& DEBUG_FALLBACK
)
1240 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1242 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1243 srcY
= strb
->Base
.Height
- srcY
- height
;
1246 map
= pipe_transfer_map(pipe
,
1248 strb
->surface
->u
.tex
.level
,
1249 strb
->surface
->u
.tex
.first_layer
,
1252 width
, height
, &src_trans
);
1254 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1255 baseFormat
== GL_DEPTH_STENCIL
) &&
1256 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1257 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1259 transfer_usage
= PIPE_TRANSFER_WRITE
;
1261 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
1262 destX
, destY
, slice
,
1263 dst_width
, dst_height
, dst_depth
,
1266 if (baseFormat
== GL_DEPTH_COMPONENT
||
1267 baseFormat
== GL_DEPTH_STENCIL
) {
1268 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1269 ctx
->Pixel
.DepthBias
!= 0.0F
);
1273 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1274 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1283 data
= malloc(width
* sizeof(uint
));
1286 /* To avoid a large temp memory allocation, do copy row by row */
1287 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1288 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
1290 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1293 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
1294 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
1295 0, 0, width
, 1, data
);
1298 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
1303 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
1311 malloc(width
* height
* 4 * sizeof(GLfloat
));
1313 if (tempSrc
&& texDest
) {
1314 const GLint dims
= 2;
1316 struct gl_texture_image
*texImage
= &stImage
->base
;
1317 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1319 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1320 unpack
.Invert
= GL_TRUE
;
1323 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
1324 dstRowStride
= transfer
->layer_stride
;
1327 dstRowStride
= transfer
->stride
;
1330 /* get float/RGBA image from framebuffer */
1331 /* XXX this usually involves a lot of int/float conversion.
1332 * try to avoid that someday.
1334 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
1335 util_format_linear(strb
->texture
->format
),
1338 /* Store into texture memory.
1339 * Note that this does some special things such as pixel transfer
1340 * ops and format conversion. In particular, if the dest tex format
1341 * is actually RGBA but the user created the texture as GL_RGB we
1342 * need to fill-in/override the alpha channel with 1.0.
1344 _mesa_texstore(ctx
, dims
,
1345 texImage
->_BaseFormat
,
1346 texImage
->TexFormat
,
1350 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1354 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1360 st_texture_image_unmap(st
, stImage
, slice
);
1361 pipe
->transfer_unmap(pipe
, src_trans
);
1366 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1367 * Note that the region to copy has already been clipped so we know we
1368 * won't read from outside the source renderbuffer's bounds.
1370 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1373 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1374 struct gl_texture_image
*texImage
,
1375 GLint destX
, GLint destY
, GLint slice
,
1376 struct gl_renderbuffer
*rb
,
1377 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
1379 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1380 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1381 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
1382 struct st_context
*st
= st_context(ctx
);
1383 struct pipe_context
*pipe
= st
->pipe
;
1384 struct pipe_screen
*screen
= pipe
->screen
;
1385 struct pipe_blit_info blit
;
1386 enum pipe_format dst_format
;
1387 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1391 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1392 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1394 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1395 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1399 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
1400 texImage
->TexFormat
)) {
1404 /* The base internal format must match the mesa format, so make sure
1405 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
1407 if (texImage
->_BaseFormat
!=
1408 _mesa_get_format_base_format(texImage
->TexFormat
) ||
1409 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
1413 /* Choose the destination format to match the TexImage behavior. */
1414 dst_format
= util_format_linear(stImage
->pt
->format
);
1415 dst_format
= util_format_luminance_to_red(dst_format
);
1416 dst_format
= util_format_intensity_to_red(dst_format
);
1418 /* See if the destination format is supported. */
1419 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
1420 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
1421 bind
= PIPE_BIND_DEPTH_STENCIL
;
1424 bind
= PIPE_BIND_RENDER_TARGET
;
1428 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
1429 stImage
->pt
->nr_samples
, bind
)) {
1433 /* Y flipping for the main framebuffer. */
1435 srcY1
= strb
->Base
.Height
- srcY
- height
;
1436 srcY0
= srcY1
+ height
;
1440 srcY1
= srcY0
+ height
;
1443 /* Blit the texture.
1444 * This supports flipping, format conversions, and downsampling.
1446 memset(&blit
, 0, sizeof(blit
));
1447 blit
.src
.resource
= strb
->texture
;
1448 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
1449 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
1450 blit
.src
.box
.x
= srcX
;
1451 blit
.src
.box
.y
= srcY0
;
1452 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
1453 blit
.src
.box
.width
= width
;
1454 blit
.src
.box
.height
= srcY1
- srcY0
;
1455 blit
.src
.box
.depth
= 1;
1456 blit
.dst
.resource
= stImage
->pt
;
1457 blit
.dst
.format
= dst_format
;
1458 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
1459 blit
.dst
.box
.x
= destX
;
1460 blit
.dst
.box
.y
= destY
;
1461 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
1462 blit
.dst
.box
.width
= width
;
1463 blit
.dst
.box
.height
= height
;
1464 blit
.dst
.box
.depth
= 1;
1465 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
1466 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1467 pipe
->blit(pipe
, &blit
);
1471 /* software fallback */
1472 fallback_copy_texsubimage(ctx
,
1473 strb
, stImage
, texImage
->_BaseFormat
,
1474 destX
, destY
, slice
,
1475 srcX
, srcY
, width
, height
);
1480 * Copy image data from stImage into the texture object 'stObj' at level
1484 copy_image_data_to_texture(struct st_context
*st
,
1485 struct st_texture_object
*stObj
,
1487 struct st_texture_image
*stImage
)
1491 const struct gl_texture_image
*dstImage
=
1492 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
1494 assert(dstImage
->Width
== stImage
->base
.Width
);
1495 assert(dstImage
->Height
== stImage
->base
.Height
);
1496 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1500 /* Copy potentially with the blitter:
1503 if (stImage
->pt
->last_level
== 0)
1506 src_level
= stImage
->base
.Level
;
1508 assert(src_level
<= stImage
->pt
->last_level
);
1509 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
1510 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
1511 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
1512 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
1513 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
1514 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
1516 st_texture_image_copy(st
->pipe
,
1517 stObj
->pt
, dstLevel
, /* dest texture, level */
1518 stImage
->pt
, src_level
, /* src texture, level */
1519 stImage
->base
.Face
);
1521 pipe_resource_reference(&stImage
->pt
, NULL
);
1523 else if (stImage
->TexData
) {
1524 /* Copy from malloc'd memory */
1525 /* XXX this should be re-examined/tested with a compressed format */
1526 GLuint blockSize
= util_format_get_blocksize(stObj
->pt
->format
);
1527 GLuint srcRowStride
= stImage
->base
.Width
* blockSize
;
1528 GLuint srcSliceStride
= stImage
->base
.Height
* srcRowStride
;
1529 st_texture_image_data(st
,
1536 _mesa_align_free(stImage
->TexData
);
1537 stImage
->TexData
= NULL
;
1540 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1545 * Called during state validation. When this function is finished,
1546 * the texture object should be ready for rendering.
1547 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1550 st_finalize_texture(struct gl_context
*ctx
,
1551 struct pipe_context
*pipe
,
1552 struct gl_texture_object
*tObj
)
1554 struct st_context
*st
= st_context(ctx
);
1555 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1556 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1558 const struct st_texture_image
*firstImage
;
1559 enum pipe_format firstImageFormat
;
1560 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
1562 if (tObj
->Immutable
)
1565 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
1566 /* The texture is complete and we know exactly how many mipmap levels
1567 * are present/needed. This is conditional because we may be called
1568 * from the st_generate_mipmap() function when the texture object is
1569 * incomplete. In that case, we'll have set stObj->lastLevel before
1572 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1573 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1574 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1576 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1579 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
1580 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
1583 pipe_resource_reference(&stObj
->pt
, NULL
);
1584 st_texture_release_all_sampler_views(st
, stObj
);
1588 if (st_obj
->buffer
!= stObj
->pt
) {
1589 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
1590 st_texture_release_all_sampler_views(st
, stObj
);
1591 stObj
->width0
= stObj
->pt
->width0
/ _mesa_get_format_bytes(tObj
->_BufferObjectFormat
);
1599 firstImage
= st_texture_image_const(_mesa_base_tex_image(&stObj
->base
));
1602 /* If both firstImage and stObj point to a texture which can contain
1603 * all active images, favour firstImage. Note that because of the
1604 * completeness requirement, we know that the image dimensions
1607 if (firstImage
->pt
&&
1608 firstImage
->pt
!= stObj
->pt
&&
1609 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1610 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1611 st_texture_release_all_sampler_views(st
, stObj
);
1614 /* If this texture comes from a window system, there is nothing else to do. */
1615 if (stObj
->surface_based
) {
1619 /* Find gallium format for the Mesa texture */
1621 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
1623 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1625 GLuint width
, height
, depth
;
1626 if (!guess_base_level_size(stObj
->base
.Target
,
1627 firstImage
->base
.Width2
,
1628 firstImage
->base
.Height2
,
1629 firstImage
->base
.Depth2
,
1630 firstImage
->base
.Level
,
1631 &width
, &height
, &depth
)) {
1632 width
= stObj
->width0
;
1633 height
= stObj
->height0
;
1634 depth
= stObj
->depth0
;
1636 /* convert GL dims to Gallium dims */
1637 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1638 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1639 ptNumSamples
= firstImage
->base
.NumSamples
;
1642 /* If we already have a gallium texture, check that it matches the texture
1643 * object's format, target, size, num_levels, etc.
1646 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1647 stObj
->pt
->format
!= firstImageFormat
||
1648 stObj
->pt
->last_level
< stObj
->lastLevel
||
1649 stObj
->pt
->width0
!= ptWidth
||
1650 stObj
->pt
->height0
!= ptHeight
||
1651 stObj
->pt
->depth0
!= ptDepth
||
1652 stObj
->pt
->nr_samples
!= ptNumSamples
||
1653 stObj
->pt
->array_size
!= ptLayers
)
1655 /* The gallium texture does not match the Mesa texture so delete the
1656 * gallium texture now. We'll make a new one below.
1658 pipe_resource_reference(&stObj
->pt
, NULL
);
1659 st_texture_release_all_sampler_views(st
, stObj
);
1660 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1664 /* May need to create a new gallium texture:
1667 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1669 stObj
->pt
= st_texture_create(st
,
1670 gl_target_to_pipe(stObj
->base
.Target
),
1676 ptLayers
, ptNumSamples
,
1680 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1685 /* Pull in any images not in the object's texture:
1687 for (face
= 0; face
< nr_faces
; face
++) {
1689 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1690 struct st_texture_image
*stImage
=
1691 st_texture_image(stObj
->base
.Image
[face
][level
]);
1693 /* Need to import images in main memory or held in other textures.
1695 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1697 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1698 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1699 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
1700 /* src image fits expected dest mipmap level size */
1701 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1712 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1713 * for a whole mipmap stack.
1716 st_AllocTextureStorage(struct gl_context
*ctx
,
1717 struct gl_texture_object
*texObj
,
1718 GLsizei levels
, GLsizei width
,
1719 GLsizei height
, GLsizei depth
)
1721 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
1722 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
1723 struct st_context
*st
= st_context(ctx
);
1724 struct st_texture_object
*stObj
= st_texture_object(texObj
);
1725 struct pipe_screen
*screen
= st
->pipe
->screen
;
1726 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
1727 enum pipe_format fmt
;
1729 GLuint num_samples
= texImage
->NumSamples
;
1733 /* Save the level=0 dimensions */
1734 stObj
->width0
= width
;
1735 stObj
->height0
= height
;
1736 stObj
->depth0
= depth
;
1737 stObj
->lastLevel
= levels
- 1;
1739 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
1741 bindings
= default_bindings(st
, fmt
);
1743 /* Raise the sample count if the requested one is unsupported. */
1744 if (num_samples
> 1) {
1745 boolean found
= FALSE
;
1747 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
1748 if (screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
1750 PIPE_BIND_SAMPLER_VIEW
)) {
1751 /* Update the sample count in gl_texture_image as well. */
1752 texImage
->NumSamples
= num_samples
;
1763 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
1764 width
, height
, depth
,
1765 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1767 stObj
->pt
= st_texture_create(st
,
1768 gl_target_to_pipe(texObj
->Target
),
1774 ptLayers
, num_samples
,
1779 /* Set image resource pointers */
1780 for (level
= 0; level
< levels
; level
++) {
1782 for (face
= 0; face
< numFaces
; face
++) {
1783 struct st_texture_image
*stImage
=
1784 st_texture_image(texObj
->Image
[face
][level
]);
1785 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1794 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
1795 GLint level
, mesa_format format
,
1796 GLint width
, GLint height
,
1797 GLint depth
, GLint border
)
1799 struct st_context
*st
= st_context(ctx
);
1800 struct pipe_context
*pipe
= st
->pipe
;
1802 if (width
== 0 || height
== 0 || depth
== 0) {
1803 /* zero-sized images are legal, and always fit! */
1807 if (pipe
->screen
->can_create_resource
) {
1808 /* Ask the gallium driver if the texture is too large */
1809 struct gl_texture_object
*texObj
=
1810 _mesa_get_current_tex_object(ctx
, target
);
1811 struct pipe_resource pt
;
1813 /* Setup the pipe_resource object
1815 memset(&pt
, 0, sizeof(pt
));
1817 pt
.target
= gl_target_to_pipe(target
);
1818 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
1820 st_gl_texture_dims_to_pipe_dims(target
,
1821 width
, height
, depth
,
1822 &pt
.width0
, &pt
.height0
,
1823 &pt
.depth0
, &pt
.array_size
);
1825 if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
1826 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
1827 /* assume just one mipmap level */
1831 /* assume a full set of mipmaps */
1832 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
1835 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
1838 /* Use core Mesa fallback */
1839 return _mesa_test_proxy_teximage(ctx
, target
, level
, format
,
1840 width
, height
, depth
, border
);
1845 st_TextureView(struct gl_context
*ctx
,
1846 struct gl_texture_object
*texObj
,
1847 struct gl_texture_object
*origTexObj
)
1849 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
1850 struct st_texture_object
*tex
= st_texture_object(texObj
);
1851 struct gl_texture_image
*image
= texObj
->Image
[0][0];
1853 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
1854 const int numLevels
= texObj
->NumLevels
;
1859 pipe_resource_reference(&tex
->pt
, orig
->pt
);
1861 /* Set image resource pointers */
1862 for (level
= 0; level
< numLevels
; level
++) {
1863 for (face
= 0; face
< numFaces
; face
++) {
1864 struct st_texture_image
*stImage
=
1865 st_texture_image(texObj
->Image
[face
][level
]);
1866 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
1870 tex
->surface_based
= GL_TRUE
;
1871 tex
->surface_format
=
1872 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
1874 tex
->width0
= image
->Width
;
1875 tex
->height0
= image
->Height
;
1876 tex
->depth0
= image
->Depth
;
1877 tex
->lastLevel
= numLevels
- 1;
1884 st_init_texture_functions(struct dd_function_table
*functions
)
1886 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1887 functions
->QuerySamplesForFormat
= st_QuerySamplesForFormat
;
1888 functions
->TexImage
= st_TexImage
;
1889 functions
->TexSubImage
= st_TexSubImage
;
1890 functions
->CompressedTexSubImage
= _mesa_store_compressed_texsubimage
;
1891 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
1892 functions
->GenerateMipmap
= st_generate_mipmap
;
1894 functions
->GetTexImage
= st_GetTexImage
;
1896 /* compressed texture functions */
1897 functions
->CompressedTexImage
= st_CompressedTexImage
;
1898 functions
->GetCompressedTexImage
= _mesa_GetCompressedTexImage_sw
;
1900 functions
->NewTextureObject
= st_NewTextureObject
;
1901 functions
->NewTextureImage
= st_NewTextureImage
;
1902 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
1903 functions
->DeleteTexture
= st_DeleteTextureObject
;
1904 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
1905 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
1906 functions
->MapTextureImage
= st_MapTextureImage
;
1907 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
1909 /* XXX Temporary until we can query pipe's texture sizes */
1910 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
1912 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
1913 functions
->TextureView
= st_TextureView
;