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 **************************************************************************/
29 #include "main/bufferobj.h"
30 #include "main/enums.h"
31 #include "main/fbobject.h"
32 #include "main/formats.h"
33 #include "main/format_utils.h"
34 #include "main/glformats.h"
35 #include "main/image.h"
36 #include "main/imports.h"
37 #include "main/macros.h"
38 #include "main/mipmap.h"
39 #include "main/pack.h"
41 #include "main/pixeltransfer.h"
42 #include "main/texcompress.h"
43 #include "main/texcompress_etc.h"
44 #include "main/texgetimage.h"
45 #include "main/teximage.h"
46 #include "main/texobj.h"
47 #include "main/texstore.h"
49 #include "state_tracker/st_debug.h"
50 #include "state_tracker/st_context.h"
51 #include "state_tracker/st_cb_fbo.h"
52 #include "state_tracker/st_cb_flush.h"
53 #include "state_tracker/st_cb_texture.h"
54 #include "state_tracker/st_cb_bufferobjects.h"
55 #include "state_tracker/st_format.h"
56 #include "state_tracker/st_texture.h"
57 #include "state_tracker/st_gen_mipmap.h"
58 #include "state_tracker/st_atom.h"
60 #include "pipe/p_context.h"
61 #include "pipe/p_defines.h"
62 #include "util/u_inlines.h"
63 #include "pipe/p_shader_tokens.h"
64 #include "util/u_tile.h"
65 #include "util/u_format.h"
66 #include "util/u_surface.h"
67 #include "util/u_sampler.h"
68 #include "util/u_math.h"
69 #include "util/u_box.h"
71 #define DBG if (0) printf
74 enum pipe_texture_target
75 gl_target_to_pipe(GLenum target
)
79 case GL_PROXY_TEXTURE_1D
:
80 return PIPE_TEXTURE_1D
;
82 case GL_PROXY_TEXTURE_2D
:
83 case GL_TEXTURE_EXTERNAL_OES
:
84 case GL_TEXTURE_2D_MULTISAMPLE
:
85 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
86 return PIPE_TEXTURE_2D
;
87 case GL_TEXTURE_RECTANGLE_NV
:
88 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
89 return PIPE_TEXTURE_RECT
;
91 case GL_PROXY_TEXTURE_3D
:
92 return PIPE_TEXTURE_3D
;
93 case GL_TEXTURE_CUBE_MAP_ARB
:
94 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
95 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
96 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
97 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
98 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
99 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
100 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
101 return PIPE_TEXTURE_CUBE
;
102 case GL_TEXTURE_1D_ARRAY_EXT
:
103 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
104 return PIPE_TEXTURE_1D_ARRAY
;
105 case GL_TEXTURE_2D_ARRAY_EXT
:
106 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
107 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
108 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
109 return PIPE_TEXTURE_2D_ARRAY
;
110 case GL_TEXTURE_BUFFER
:
112 case GL_TEXTURE_CUBE_MAP_ARRAY
:
113 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
114 return PIPE_TEXTURE_CUBE_ARRAY
;
122 /** called via ctx->Driver.NewTextureImage() */
123 static struct gl_texture_image
*
124 st_NewTextureImage(struct gl_context
* ctx
)
126 DBG("%s\n", __func__
);
128 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
132 /** called via ctx->Driver.DeleteTextureImage() */
134 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
136 /* nothing special (yet) for st_texture_image */
137 _mesa_delete_texture_image(ctx
, img
);
141 /** called via ctx->Driver.NewTextureObject() */
142 static struct gl_texture_object
*
143 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
145 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
147 DBG("%s\n", __func__
);
148 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
153 /** called via ctx->Driver.DeleteTextureObject() */
155 st_DeleteTextureObject(struct gl_context
*ctx
,
156 struct gl_texture_object
*texObj
)
158 struct st_context
*st
= st_context(ctx
);
159 struct st_texture_object
*stObj
= st_texture_object(texObj
);
161 pipe_resource_reference(&stObj
->pt
, NULL
);
162 st_texture_release_all_sampler_views(st
, stObj
);
163 st_texture_free_sampler_views(stObj
);
164 _mesa_delete_texture_object(ctx
, texObj
);
168 /** called via ctx->Driver.FreeTextureImageBuffer() */
170 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
171 struct gl_texture_image
*texImage
)
173 struct st_texture_image
*stImage
= st_texture_image(texImage
);
175 DBG("%s\n", __func__
);
178 pipe_resource_reference(&stImage
->pt
, NULL
);
181 free(stImage
->transfer
);
182 stImage
->transfer
= NULL
;
183 stImage
->num_transfers
= 0;
187 /** called via ctx->Driver.MapTextureImage() */
189 st_MapTextureImage(struct gl_context
*ctx
,
190 struct gl_texture_image
*texImage
,
191 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
193 GLubyte
**mapOut
, GLint
*rowStrideOut
)
195 struct st_context
*st
= st_context(ctx
);
196 struct st_texture_image
*stImage
= st_texture_image(texImage
);
199 struct pipe_transfer
*transfer
;
202 if (mode
& GL_MAP_READ_BIT
)
203 pipeMode
|= PIPE_TRANSFER_READ
;
204 if (mode
& GL_MAP_WRITE_BIT
)
205 pipeMode
|= PIPE_TRANSFER_WRITE
;
206 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
207 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
209 map
= st_texture_image_map(st
, stImage
, pipeMode
, x
, y
, slice
, w
, h
, 1,
212 if ((_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
213 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
)) {
214 /* ETC isn't supported by gallium and it's represented
215 * by uncompressed formats. Only write transfers with precompressed
216 * data are supported by ES3, which makes this really simple.
218 * Just create a temporary storage where the ETC texture will
219 * be stored. It will be decompressed in the Unmap function.
221 unsigned z
= transfer
->box
.z
;
222 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
224 itransfer
->temp_data
=
225 malloc(_mesa_format_image_size(texImage
->TexFormat
, w
, h
, 1));
226 itransfer
->temp_stride
=
227 _mesa_format_row_stride(texImage
->TexFormat
, w
);
228 itransfer
->map
= map
;
230 *mapOut
= itransfer
->temp_data
;
231 *rowStrideOut
= itransfer
->temp_stride
;
234 /* supported mapping */
236 *rowStrideOut
= transfer
->stride
;
246 /** called via ctx->Driver.UnmapTextureImage() */
248 st_UnmapTextureImage(struct gl_context
*ctx
,
249 struct gl_texture_image
*texImage
,
252 struct st_context
*st
= st_context(ctx
);
253 struct st_texture_image
*stImage
= st_texture_image(texImage
);
255 if ((_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
256 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
)) {
257 /* Decompress the ETC texture to the mapped one. */
258 unsigned z
= slice
+ stImage
->base
.Face
;
259 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
260 struct pipe_transfer
*transfer
= itransfer
->transfer
;
262 assert(z
== transfer
->box
.z
);
264 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
265 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
266 itransfer
->temp_data
,
267 itransfer
->temp_stride
,
268 transfer
->box
.width
, transfer
->box
.height
);
271 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
272 itransfer
->temp_data
, itransfer
->temp_stride
,
273 transfer
->box
.width
, transfer
->box
.height
,
274 texImage
->TexFormat
);
277 free(itransfer
->temp_data
);
278 itransfer
->temp_data
= NULL
;
279 itransfer
->temp_stride
= 0;
283 st_texture_image_unmap(st
, stImage
, slice
);
288 * Return default texture resource binding bitmask for the given format.
291 default_bindings(struct st_context
*st
, enum pipe_format format
)
293 struct pipe_screen
*screen
= st
->pipe
->screen
;
294 const unsigned target
= PIPE_TEXTURE_2D
;
297 if (util_format_is_depth_or_stencil(format
))
298 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
300 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
302 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
306 format
= util_format_linear(format
);
308 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
311 return PIPE_BIND_SAMPLER_VIEW
;
317 * Given the size of a mipmap image, try to compute the size of the level=0
320 * Note that this isn't always accurate for odd-sized, non-POW textures.
321 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
323 * \return GL_TRUE for success, GL_FALSE for failure
326 guess_base_level_size(GLenum target
,
327 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
328 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
335 /* Guess the size of the base level.
336 * Depending on the image's size, we can't always make a guess here.
340 case GL_TEXTURE_1D_ARRAY
:
345 case GL_TEXTURE_2D_ARRAY
:
346 /* We can't make a good guess here, because the base level dimensions
349 if (width
== 1 || height
== 1) {
356 case GL_TEXTURE_CUBE_MAP
:
357 case GL_TEXTURE_CUBE_MAP_ARRAY
:
363 /* We can't make a good guess here, because the base level dimensions
366 if (width
== 1 || height
== 1 || depth
== 1) {
374 case GL_TEXTURE_RECTANGLE
:
391 * Try to allocate a pipe_resource object for the given st_texture_object.
393 * We use the given st_texture_image as a clue to determine the size of the
394 * mipmap image at level=0.
396 * \return GL_TRUE for success, GL_FALSE if out of memory.
399 guess_and_alloc_texture(struct st_context
*st
,
400 struct st_texture_object
*stObj
,
401 const struct st_texture_image
*stImage
)
403 GLuint lastLevel
, width
, height
, depth
;
405 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
406 enum pipe_format fmt
;
408 DBG("%s\n", __func__
);
412 if (!guess_base_level_size(stObj
->base
.Target
,
413 stImage
->base
.Width2
,
414 stImage
->base
.Height2
,
415 stImage
->base
.Depth2
,
417 &width
, &height
, &depth
)) {
418 /* we can't determine the image size at level=0 */
419 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
420 /* this is not an out of memory error */
424 /* At this point, (width x height x depth) is the expected size of
425 * the level=0 mipmap image.
428 /* Guess a reasonable value for lastLevel. With OpenGL we have no
429 * idea how many mipmap levels will be in a texture until we start
430 * to render with it. Make an educated guess here but be prepared
431 * to re-allocating a texture buffer with space for more (or fewer)
432 * mipmap levels later.
434 if ((stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
435 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
436 (stObj
->base
.BaseLevel
== 0 &&
437 stObj
->base
.MaxLevel
== 0) ||
438 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
439 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
440 !stObj
->base
.GenerateMipmap
&&
441 stImage
->base
.Level
== 0) {
442 /* only alloc space for a single mipmap level */
446 /* alloc space for a full mipmap */
447 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
448 width
, height
, depth
) - 1;
451 /* Save the level=0 dimensions */
452 stObj
->width0
= width
;
453 stObj
->height0
= height
;
454 stObj
->depth0
= depth
;
456 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
458 bindings
= default_bindings(st
, fmt
);
460 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
461 width
, height
, depth
,
462 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
464 stObj
->pt
= st_texture_create(st
,
465 gl_target_to_pipe(stObj
->base
.Target
),
474 stObj
->lastLevel
= lastLevel
;
476 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
478 return stObj
->pt
!= NULL
;
483 * Called via ctx->Driver.AllocTextureImageBuffer().
484 * If the texture object/buffer already has space for the indicated image,
485 * we're done. Otherwise, allocate memory for the new texture image.
488 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
489 struct gl_texture_image
*texImage
)
491 struct st_context
*st
= st_context(ctx
);
492 struct st_texture_image
*stImage
= st_texture_image(texImage
);
493 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
494 const GLuint level
= texImage
->Level
;
495 GLuint width
= texImage
->Width
;
496 GLuint height
= texImage
->Height
;
497 GLuint depth
= texImage
->Depth
;
499 DBG("%s\n", __func__
);
501 assert(!stImage
->pt
); /* xxx this might be wrong */
503 /* Look if the parent texture object has space for this image */
505 level
<= stObj
->pt
->last_level
&&
506 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
507 /* this image will fit in the existing texture object's memory */
508 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
512 /* The parent texture object does not have space for this image */
514 pipe_resource_reference(&stObj
->pt
, NULL
);
515 st_texture_release_all_sampler_views(st
, stObj
);
517 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
518 /* Probably out of memory.
519 * Try flushing any pending rendering, then retry.
522 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
523 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
529 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
530 /* The image will live in the object's mipmap memory */
531 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
536 /* Create a new, temporary texture/resource/buffer to hold this
537 * one texture image. Note that when we later access this image
538 * (either for mapping or copying) we'll want to always specify
539 * mipmap level=0, even if the image represents some other mipmap
542 enum pipe_format format
=
543 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
544 GLuint bindings
= default_bindings(st
, format
);
545 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
547 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
548 width
, height
, depth
,
549 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
551 stImage
->pt
= st_texture_create(st
,
552 gl_target_to_pipe(stObj
->base
.Target
),
560 return stImage
->pt
!= NULL
;
566 * Preparation prior to glTexImage. Basically check the 'surface_based'
567 * field and switch to a "normal" tex image if necessary.
570 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
571 GLenum format
, GLenum type
)
573 struct gl_texture_object
*texObj
= texImage
->TexObject
;
574 struct st_texture_object
*stObj
= st_texture_object(texObj
);
576 /* switch to "normal" */
577 if (stObj
->surface_based
) {
578 const GLenum target
= texObj
->Target
;
579 const GLuint level
= texImage
->Level
;
580 mesa_format texFormat
;
582 _mesa_clear_texture_object(ctx
, texObj
);
583 pipe_resource_reference(&stObj
->pt
, NULL
);
585 /* oops, need to init this image again */
586 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
587 texImage
->InternalFormat
, format
,
590 _mesa_init_teximage_fields(ctx
, texImage
,
591 texImage
->Width
, texImage
->Height
,
592 texImage
->Depth
, texImage
->Border
,
593 texImage
->InternalFormat
, texFormat
);
595 stObj
->surface_based
= GL_FALSE
;
601 * Return a writemask for the gallium blit. The parameters can be base
602 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
605 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
608 case GL_DEPTH_STENCIL
:
610 case GL_DEPTH_STENCIL
:
612 case GL_DEPTH_COMPONENT
:
614 case GL_STENCIL_INDEX
:
621 case GL_DEPTH_COMPONENT
:
623 case GL_DEPTH_STENCIL
:
624 case GL_DEPTH_COMPONENT
:
631 case GL_STENCIL_INDEX
:
633 case GL_STENCIL_INDEX
:
641 return PIPE_MASK_RGBA
;
647 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
648 struct gl_texture_image
*texImage
,
649 GLint xoffset
, GLint yoffset
, GLint zoffset
,
650 GLint width
, GLint height
, GLint depth
,
651 GLenum format
, GLenum type
, const void *pixels
,
652 const struct gl_pixelstore_attrib
*unpack
)
654 struct st_context
*st
= st_context(ctx
);
655 struct st_texture_image
*stImage
= st_texture_image(texImage
);
656 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
657 struct pipe_context
*pipe
= st
->pipe
;
658 struct pipe_screen
*screen
= pipe
->screen
;
659 struct pipe_resource
*dst
= stImage
->pt
;
660 struct pipe_resource
*src
= NULL
;
661 struct pipe_resource src_templ
;
662 struct pipe_transfer
*transfer
;
663 struct pipe_blit_info blit
;
664 enum pipe_format src_format
, dst_format
;
665 mesa_format mesa_src_format
;
666 GLenum gl_target
= texImage
->TexObject
->Target
;
670 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
671 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
673 if (!st
->prefer_blit_based_texture_transfer
) {
681 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
682 * blit implementation in some drivers. */
683 if (format
== GL_DEPTH_STENCIL
) {
687 /* If the base internal format and the texture format don't match,
688 * we can't use blit-based TexSubImage. */
689 if (texImage
->_BaseFormat
!=
690 _mesa_get_format_base_format(texImage
->TexFormat
)) {
694 /* See if the texture format already matches the format and type,
695 * in which case the memcpy-based fast path will likely be used and
696 * we don't have to blit. */
697 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
698 type
, unpack
->SwapBytes
)) {
702 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
703 bind
= PIPE_BIND_DEPTH_STENCIL
;
705 bind
= PIPE_BIND_RENDER_TARGET
;
707 /* See if the destination format is supported.
708 * For luminance and intensity, only the red channel is stored there. */
709 dst_format
= util_format_linear(dst
->format
);
710 dst_format
= util_format_luminance_to_red(dst_format
);
711 dst_format
= util_format_intensity_to_red(dst_format
);
714 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
715 dst
->nr_samples
, bind
)) {
719 /* Choose the source format. */
720 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
721 format
, type
, unpack
->SwapBytes
);
726 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
728 /* There is no reason to do this if we cannot use memcpy for the temporary
729 * source texture at least. This also takes transfer ops into account,
731 if (!_mesa_texstore_can_use_memcpy(ctx
,
732 _mesa_get_format_base_format(mesa_src_format
),
733 mesa_src_format
, format
, type
, unpack
)) {
737 /* TexSubImage only sets a single cubemap face. */
738 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
739 gl_target
= GL_TEXTURE_2D
;
741 /* TexSubImage can specify subsets of cube map array faces
742 * so we need to upload via 2D array instead */
743 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
744 gl_target
= GL_TEXTURE_2D_ARRAY
;
747 /* Initialize the source texture description. */
748 memset(&src_templ
, 0, sizeof(src_templ
));
749 src_templ
.target
= gl_target_to_pipe(gl_target
);
750 src_templ
.format
= src_format
;
751 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
752 src_templ
.usage
= PIPE_USAGE_STAGING
;
754 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
755 &src_templ
.width0
, &src_templ
.height0
,
756 &src_templ
.depth0
, &src_templ
.array_size
);
758 /* Check for NPOT texture support. */
759 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
760 (!util_is_power_of_two(src_templ
.width0
) ||
761 !util_is_power_of_two(src_templ
.height0
) ||
762 !util_is_power_of_two(src_templ
.depth0
))) {
766 /* Create the source texture. */
767 src
= screen
->resource_create(screen
, &src_templ
);
772 /* Map source pixels. */
773 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
774 format
, type
, pixels
, unpack
,
777 /* This is a GL error. */
778 pipe_resource_reference(&src
, NULL
);
782 /* From now on, we need the gallium representation of dimensions. */
783 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
790 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
791 width
, height
, depth
, &transfer
);
793 _mesa_unmap_teximage_pbo(ctx
, unpack
);
794 pipe_resource_reference(&src
, NULL
);
798 /* Upload pixels (just memcpy). */
800 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
803 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
804 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
805 /* 1D array textures.
806 * We need to convert gallium coords to GL coords.
808 GLvoid
*src
= _mesa_image_address3d(unpack
, pixels
,
809 width
, depth
, format
,
811 memcpy(map
, src
, bytesPerRow
);
814 ubyte
*slice_map
= map
;
816 for (row
= 0; row
< (unsigned) height
; row
++) {
817 GLvoid
*src
= _mesa_image_address3d(unpack
, pixels
,
818 width
, height
, format
,
819 type
, slice
, row
, 0);
820 memcpy(slice_map
, src
, bytesPerRow
);
821 slice_map
+= transfer
->stride
;
824 map
+= transfer
->layer_stride
;
828 pipe_transfer_unmap(pipe
, transfer
);
829 _mesa_unmap_teximage_pbo(ctx
, unpack
);
832 memset(&blit
, 0, sizeof(blit
));
833 blit
.src
.resource
= src
;
835 blit
.src
.format
= src_format
;
836 blit
.dst
.resource
= dst
;
837 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
838 blit
.dst
.format
= dst_format
;
839 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
840 blit
.dst
.box
.x
= xoffset
;
841 blit
.dst
.box
.y
= yoffset
;
842 blit
.dst
.box
.z
= zoffset
+ texImage
->Face
+ texImage
->TexObject
->MinLayer
;
843 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
844 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
845 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
846 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
847 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
848 blit
.scissor_enable
= FALSE
;
850 st
->pipe
->blit(st
->pipe
, &blit
);
852 pipe_resource_reference(&src
, NULL
);
856 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
857 width
, height
, depth
, format
, type
, pixels
,
862 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
863 struct gl_texture_image
*texImage
,
864 GLenum format
, GLenum type
, const void *pixels
,
865 const struct gl_pixelstore_attrib
*unpack
)
867 assert(dims
== 1 || dims
== 2 || dims
== 3);
869 prep_teximage(ctx
, texImage
, format
, type
);
871 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
874 /* allocate storage for texture data */
875 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
876 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
880 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
881 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
882 format
, type
, pixels
, unpack
);
887 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
888 struct gl_texture_image
*texImage
,
889 GLsizei imageSize
, const GLvoid
*data
)
891 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
892 _mesa_store_compressed_teximage(ctx
, dims
, texImage
, imageSize
, data
);
899 * Called via ctx->Driver.GetTexSubImage()
901 * This uses a blit to copy the texture to a texture format which matches
902 * the format and type combo and then a fast read-back is done using memcpy.
903 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
904 * a format which matches the swizzling.
906 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
908 * NOTE: Drivers usually do a blit to convert between tiled and linear
909 * texture layouts during texture uploads/downloads, so the blit
910 * we do here should be free in such cases.
913 st_GetTexSubImage(struct gl_context
* ctx
,
914 GLint xoffset
, GLint yoffset
, GLint zoffset
,
915 GLsizei width
, GLsizei height
, GLint depth
,
916 GLenum format
, GLenum type
, GLvoid
* pixels
,
917 struct gl_texture_image
*texImage
)
919 struct st_context
*st
= st_context(ctx
);
920 struct pipe_context
*pipe
= st
->pipe
;
921 struct pipe_screen
*screen
= pipe
->screen
;
922 struct st_texture_image
*stImage
= st_texture_image(texImage
);
923 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
924 struct pipe_resource
*src
= stObj
->pt
;
925 struct pipe_resource
*dst
= NULL
;
926 struct pipe_resource dst_templ
;
927 enum pipe_format dst_format
, src_format
;
928 mesa_format mesa_format
;
929 GLenum gl_target
= texImage
->TexObject
->Target
;
930 enum pipe_texture_target pipe_target
;
931 struct pipe_blit_info blit
;
932 unsigned bind
= PIPE_BIND_TRANSFER_READ
;
933 struct pipe_transfer
*tex_xfer
;
935 boolean done
= FALSE
;
937 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
938 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
940 if (!st
->prefer_blit_based_texture_transfer
&&
941 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
942 /* Try to avoid the fallback if we're doing texture decompression here */
946 if (!stImage
->pt
|| !src
) {
950 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
951 * due to an incomplete stencil blit implementation in some drivers. */
952 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
956 /* If the base internal format and the texture format don't match, we have
957 * to fall back to _mesa_GetTexImage_sw. */
958 if (texImage
->_BaseFormat
!=
959 _mesa_get_format_base_format(texImage
->TexFormat
)) {
963 /* See if the texture format already matches the format and type,
964 * in which case the memcpy-based fast path will be used. */
965 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
966 type
, ctx
->Pack
.SwapBytes
)) {
970 /* Convert the source format to what is expected by GetTexImage
971 * and see if it's supported.
973 * This only applies to glGetTexImage:
974 * - Luminance must be returned as (L,0,0,1).
975 * - Luminance alpha must be returned as (L,0,0,A).
976 * - Intensity must be returned as (I,0,0,1)
978 if (stObj
->surface_based
)
979 src_format
= util_format_linear(stObj
->surface_format
);
981 src_format
= util_format_linear(src
->format
);
982 src_format
= util_format_luminance_to_red(src_format
);
983 src_format
= util_format_intensity_to_red(src_format
);
986 !screen
->is_format_supported(screen
, src_format
, src
->target
,
988 PIPE_BIND_SAMPLER_VIEW
)) {
992 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
993 bind
|= PIPE_BIND_DEPTH_STENCIL
;
995 bind
|= PIPE_BIND_RENDER_TARGET
;
997 /* GetTexImage only returns a single face for cubemaps. */
998 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
999 gl_target
= GL_TEXTURE_2D
;
1001 pipe_target
= gl_target_to_pipe(gl_target
);
1003 /* Choose the destination format by finding the best match
1004 * for the format+type combo. */
1005 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1006 ctx
->Pack
.SwapBytes
);
1008 if (dst_format
== PIPE_FORMAT_NONE
) {
1009 GLenum dst_glformat
;
1011 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1012 * where decompression with a blit is always preferred. */
1013 if (!util_format_is_compressed(src
->format
)) {
1017 /* Set the appropriate format for the decompressed texture.
1018 * Luminance and sRGB formats shouldn't appear here.*/
1019 switch (src_format
) {
1020 case PIPE_FORMAT_DXT1_RGB
:
1021 case PIPE_FORMAT_DXT1_RGBA
:
1022 case PIPE_FORMAT_DXT3_RGBA
:
1023 case PIPE_FORMAT_DXT5_RGBA
:
1024 case PIPE_FORMAT_RGTC1_UNORM
:
1025 case PIPE_FORMAT_RGTC2_UNORM
:
1026 case PIPE_FORMAT_ETC1_RGB8
:
1027 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1028 dst_glformat
= GL_RGBA8
;
1030 case PIPE_FORMAT_RGTC1_SNORM
:
1031 case PIPE_FORMAT_RGTC2_SNORM
:
1032 if (!ctx
->Extensions
.EXT_texture_snorm
)
1034 dst_glformat
= GL_RGBA8_SNORM
;
1036 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1037 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1038 if (!ctx
->Extensions
.ARB_texture_float
)
1040 dst_glformat
= GL_RGBA32F
;
1047 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
1048 pipe_target
, 0, bind
, FALSE
);
1050 if (dst_format
== PIPE_FORMAT_NONE
) {
1051 /* unable to get an rgba format!?! */
1056 /* create the destination texture of size (width X height X depth) */
1057 memset(&dst_templ
, 0, sizeof(dst_templ
));
1058 dst_templ
.target
= pipe_target
;
1059 dst_templ
.format
= dst_format
;
1060 dst_templ
.bind
= bind
;
1061 dst_templ
.usage
= PIPE_USAGE_STAGING
;
1063 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1064 &dst_templ
.width0
, &dst_templ
.height0
,
1065 &dst_templ
.depth0
, &dst_templ
.array_size
);
1067 dst
= screen
->resource_create(screen
, &dst_templ
);
1072 /* From now on, we need the gallium representation of dimensions. */
1073 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1078 assert(texImage
->Face
== 0 ||
1079 texImage
->TexObject
->MinLayer
== 0 ||
1082 memset(&blit
, 0, sizeof(blit
));
1083 blit
.src
.resource
= src
;
1084 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
1085 blit
.src
.format
= src_format
;
1086 blit
.dst
.resource
= dst
;
1088 blit
.dst
.format
= dst
->format
;
1089 blit
.src
.box
.x
= xoffset
;
1091 blit
.src
.box
.y
= yoffset
;
1093 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
1095 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1096 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1097 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1098 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
1099 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1100 blit
.scissor_enable
= FALSE
;
1102 /* blit/render/decompress */
1103 st
->pipe
->blit(st
->pipe
, &blit
);
1105 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
1107 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
1108 0, 0, 0, width
, height
, depth
, &tex_xfer
);
1113 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
1115 /* copy/pack data into user buffer */
1116 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
1117 ctx
->Pack
.SwapBytes
)) {
1119 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
1122 for (slice
= 0; slice
< depth
; slice
++) {
1123 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1124 /* 1D array textures.
1125 * We need to convert gallium coords to GL coords.
1127 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1128 width
, depth
, format
,
1130 memcpy(dest
, map
, bytesPerRow
);
1133 ubyte
*slice_map
= map
;
1135 for (row
= 0; row
< height
; row
++) {
1136 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1137 width
, height
, format
,
1138 type
, slice
, row
, 0);
1139 memcpy(dest
, slice_map
, bytesPerRow
);
1140 slice_map
+= tex_xfer
->stride
;
1143 map
+= tex_xfer
->layer_stride
;
1147 /* format translation via floats */
1150 uint32_t dstMesaFormat
;
1151 int dstStride
, srcStride
;
1153 assert(util_format_is_compressed(src
->format
));
1155 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
1160 if (ST_DEBUG
& DEBUG_FALLBACK
)
1161 debug_printf("%s: fallback format translation\n", __func__
);
1163 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
1164 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
1165 srcStride
= 4 * width
* sizeof(GLfloat
);
1166 for (slice
= 0; slice
< depth
; slice
++) {
1167 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1168 /* 1D array textures.
1169 * We need to convert gallium coords to GL coords.
1171 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1172 width
, depth
, format
,
1175 /* get float[4] rgba row from surface */
1176 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, 1,
1179 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
1180 rgba
, RGBA32_FLOAT
, srcStride
,
1184 for (row
= 0; row
< height
; row
++) {
1185 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1186 width
, height
, format
,
1187 type
, slice
, row
, 0);
1189 /* get float[4] rgba row from surface */
1190 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, row
, width
, 1,
1193 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
1194 rgba
, RGBA32_FLOAT
, srcStride
,
1198 map
+= tex_xfer
->layer_stride
;
1207 pipe_transfer_unmap(pipe
, tex_xfer
);
1209 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
1210 pipe_resource_reference(&dst
, NULL
);
1214 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
1215 width
, height
, depth
,
1216 format
, type
, pixels
, texImage
);
1222 * Do a CopyTexSubImage operation using a read transfer from the source,
1223 * a write transfer to the destination and get_tile()/put_tile() to access
1224 * the pixels/texels.
1226 * Note: srcY=0=TOP of renderbuffer
1229 fallback_copy_texsubimage(struct gl_context
*ctx
,
1230 struct st_renderbuffer
*strb
,
1231 struct st_texture_image
*stImage
,
1233 GLint destX
, GLint destY
, GLint slice
,
1234 GLint srcX
, GLint srcY
,
1235 GLsizei width
, GLsizei height
)
1237 struct st_context
*st
= st_context(ctx
);
1238 struct pipe_context
*pipe
= st
->pipe
;
1239 struct pipe_transfer
*src_trans
;
1241 enum pipe_transfer_usage transfer_usage
;
1243 unsigned dst_width
= width
;
1244 unsigned dst_height
= height
;
1245 unsigned dst_depth
= 1;
1246 struct pipe_transfer
*transfer
;
1248 if (ST_DEBUG
& DEBUG_FALLBACK
)
1249 debug_printf("%s: fallback processing\n", __func__
);
1251 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1252 srcY
= strb
->Base
.Height
- srcY
- height
;
1255 map
= pipe_transfer_map(pipe
,
1257 strb
->surface
->u
.tex
.level
,
1258 strb
->surface
->u
.tex
.first_layer
,
1261 width
, height
, &src_trans
);
1263 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1264 baseFormat
== GL_DEPTH_STENCIL
) &&
1265 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1266 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1268 transfer_usage
= PIPE_TRANSFER_WRITE
;
1270 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
1271 destX
, destY
, slice
,
1272 dst_width
, dst_height
, dst_depth
,
1275 if (baseFormat
== GL_DEPTH_COMPONENT
||
1276 baseFormat
== GL_DEPTH_STENCIL
) {
1277 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1278 ctx
->Pixel
.DepthBias
!= 0.0F
);
1282 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1283 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1292 data
= malloc(width
* sizeof(uint
));
1295 /* To avoid a large temp memory allocation, do copy row by row */
1296 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1297 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
1299 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1302 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
1303 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
1304 0, 0, width
, 1, data
);
1307 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
1312 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
1320 malloc(width
* height
* 4 * sizeof(GLfloat
));
1322 if (tempSrc
&& texDest
) {
1323 const GLint dims
= 2;
1325 struct gl_texture_image
*texImage
= &stImage
->base
;
1326 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1328 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1329 unpack
.Invert
= GL_TRUE
;
1332 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
1333 dstRowStride
= transfer
->layer_stride
;
1336 dstRowStride
= transfer
->stride
;
1339 /* get float/RGBA image from framebuffer */
1340 /* XXX this usually involves a lot of int/float conversion.
1341 * try to avoid that someday.
1343 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
1344 util_format_linear(strb
->texture
->format
),
1347 /* Store into texture memory.
1348 * Note that this does some special things such as pixel transfer
1349 * ops and format conversion. In particular, if the dest tex format
1350 * is actually RGBA but the user created the texture as GL_RGB we
1351 * need to fill-in/override the alpha channel with 1.0.
1353 _mesa_texstore(ctx
, dims
,
1354 texImage
->_BaseFormat
,
1355 texImage
->TexFormat
,
1359 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1363 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1369 st_texture_image_unmap(st
, stImage
, slice
);
1370 pipe
->transfer_unmap(pipe
, src_trans
);
1375 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1376 * Note that the region to copy has already been clipped so we know we
1377 * won't read from outside the source renderbuffer's bounds.
1379 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1382 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1383 struct gl_texture_image
*texImage
,
1384 GLint destX
, GLint destY
, GLint slice
,
1385 struct gl_renderbuffer
*rb
,
1386 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
1388 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1389 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1390 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
1391 struct st_context
*st
= st_context(ctx
);
1392 struct pipe_context
*pipe
= st
->pipe
;
1393 struct pipe_screen
*screen
= pipe
->screen
;
1394 struct pipe_blit_info blit
;
1395 enum pipe_format dst_format
;
1396 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1400 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1401 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1403 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1404 debug_printf("%s: null strb or stImage\n", __func__
);
1408 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
1409 texImage
->TexFormat
)) {
1413 /* The base internal format must match the mesa format, so make sure
1414 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
1416 if (texImage
->_BaseFormat
!=
1417 _mesa_get_format_base_format(texImage
->TexFormat
) ||
1418 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
1422 /* Choose the destination format to match the TexImage behavior. */
1423 dst_format
= util_format_linear(stImage
->pt
->format
);
1424 dst_format
= util_format_luminance_to_red(dst_format
);
1425 dst_format
= util_format_intensity_to_red(dst_format
);
1427 /* See if the destination format is supported. */
1428 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
1429 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
1430 bind
= PIPE_BIND_DEPTH_STENCIL
;
1433 bind
= PIPE_BIND_RENDER_TARGET
;
1437 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
1438 stImage
->pt
->nr_samples
, bind
)) {
1442 /* Y flipping for the main framebuffer. */
1444 srcY1
= strb
->Base
.Height
- srcY
- height
;
1445 srcY0
= srcY1
+ height
;
1449 srcY1
= srcY0
+ height
;
1452 /* Blit the texture.
1453 * This supports flipping, format conversions, and downsampling.
1455 memset(&blit
, 0, sizeof(blit
));
1456 blit
.src
.resource
= strb
->texture
;
1457 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
1458 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
1459 blit
.src
.box
.x
= srcX
;
1460 blit
.src
.box
.y
= srcY0
;
1461 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
1462 blit
.src
.box
.width
= width
;
1463 blit
.src
.box
.height
= srcY1
- srcY0
;
1464 blit
.src
.box
.depth
= 1;
1465 blit
.dst
.resource
= stImage
->pt
;
1466 blit
.dst
.format
= dst_format
;
1467 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
1468 blit
.dst
.box
.x
= destX
;
1469 blit
.dst
.box
.y
= destY
;
1470 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
1471 blit
.dst
.box
.width
= width
;
1472 blit
.dst
.box
.height
= height
;
1473 blit
.dst
.box
.depth
= 1;
1474 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
1475 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1476 pipe
->blit(pipe
, &blit
);
1480 /* software fallback */
1481 fallback_copy_texsubimage(ctx
,
1482 strb
, stImage
, texImage
->_BaseFormat
,
1483 destX
, destY
, slice
,
1484 srcX
, srcY
, width
, height
);
1489 * Copy image data from stImage into the texture object 'stObj' at level
1493 copy_image_data_to_texture(struct st_context
*st
,
1494 struct st_texture_object
*stObj
,
1496 struct st_texture_image
*stImage
)
1500 const struct gl_texture_image
*dstImage
=
1501 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
1503 assert(dstImage
->Width
== stImage
->base
.Width
);
1504 assert(dstImage
->Height
== stImage
->base
.Height
);
1505 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1509 /* Copy potentially with the blitter:
1512 if (stImage
->pt
->last_level
== 0)
1515 src_level
= stImage
->base
.Level
;
1517 assert(src_level
<= stImage
->pt
->last_level
);
1518 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
1519 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
1520 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
1521 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
1522 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
1523 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
1525 st_texture_image_copy(st
->pipe
,
1526 stObj
->pt
, dstLevel
, /* dest texture, level */
1527 stImage
->pt
, src_level
, /* src texture, level */
1528 stImage
->base
.Face
);
1530 pipe_resource_reference(&stImage
->pt
, NULL
);
1532 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1537 * Called during state validation. When this function is finished,
1538 * the texture object should be ready for rendering.
1539 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1542 st_finalize_texture(struct gl_context
*ctx
,
1543 struct pipe_context
*pipe
,
1544 struct gl_texture_object
*tObj
)
1546 struct st_context
*st
= st_context(ctx
);
1547 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1548 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1550 const struct st_texture_image
*firstImage
;
1551 enum pipe_format firstImageFormat
;
1552 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
1554 if (tObj
->Immutable
)
1557 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
1558 /* The texture is complete and we know exactly how many mipmap levels
1559 * are present/needed. This is conditional because we may be called
1560 * from the st_generate_mipmap() function when the texture object is
1561 * incomplete. In that case, we'll have set stObj->lastLevel before
1564 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1565 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1566 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1568 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1571 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
1572 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
1575 pipe_resource_reference(&stObj
->pt
, NULL
);
1576 st_texture_release_all_sampler_views(st
, stObj
);
1580 if (st_obj
->buffer
!= stObj
->pt
) {
1581 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
1582 st_texture_release_all_sampler_views(st
, stObj
);
1583 stObj
->width0
= stObj
->pt
->width0
/ _mesa_get_format_bytes(tObj
->_BufferObjectFormat
);
1591 firstImage
= st_texture_image_const(_mesa_base_tex_image(&stObj
->base
));
1594 /* If both firstImage and stObj point to a texture which can contain
1595 * all active images, favour firstImage. Note that because of the
1596 * completeness requirement, we know that the image dimensions
1599 if (firstImage
->pt
&&
1600 firstImage
->pt
!= stObj
->pt
&&
1601 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1602 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1603 st_texture_release_all_sampler_views(st
, stObj
);
1606 /* If this texture comes from a window system, there is nothing else to do. */
1607 if (stObj
->surface_based
) {
1611 /* Find gallium format for the Mesa texture */
1613 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
1615 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1617 GLuint width
, height
, depth
;
1618 if (!guess_base_level_size(stObj
->base
.Target
,
1619 firstImage
->base
.Width2
,
1620 firstImage
->base
.Height2
,
1621 firstImage
->base
.Depth2
,
1622 firstImage
->base
.Level
,
1623 &width
, &height
, &depth
)) {
1624 width
= stObj
->width0
;
1625 height
= stObj
->height0
;
1626 depth
= stObj
->depth0
;
1628 /* convert GL dims to Gallium dims */
1629 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1630 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1631 ptNumSamples
= firstImage
->base
.NumSamples
;
1634 /* If we already have a gallium texture, check that it matches the texture
1635 * object's format, target, size, num_levels, etc.
1638 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1639 stObj
->pt
->format
!= firstImageFormat
||
1640 stObj
->pt
->last_level
< stObj
->lastLevel
||
1641 stObj
->pt
->width0
!= ptWidth
||
1642 stObj
->pt
->height0
!= ptHeight
||
1643 stObj
->pt
->depth0
!= ptDepth
||
1644 stObj
->pt
->nr_samples
!= ptNumSamples
||
1645 stObj
->pt
->array_size
!= ptLayers
)
1647 /* The gallium texture does not match the Mesa texture so delete the
1648 * gallium texture now. We'll make a new one below.
1650 pipe_resource_reference(&stObj
->pt
, NULL
);
1651 st_texture_release_all_sampler_views(st
, stObj
);
1652 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1656 /* May need to create a new gallium texture:
1659 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1661 stObj
->pt
= st_texture_create(st
,
1662 gl_target_to_pipe(stObj
->base
.Target
),
1668 ptLayers
, ptNumSamples
,
1672 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1677 /* Pull in any images not in the object's texture:
1679 for (face
= 0; face
< nr_faces
; face
++) {
1681 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1682 struct st_texture_image
*stImage
=
1683 st_texture_image(stObj
->base
.Image
[face
][level
]);
1685 /* Need to import images in main memory or held in other textures.
1687 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1689 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1690 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1691 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
1692 /* src image fits expected dest mipmap level size */
1693 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1704 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1705 * for a whole mipmap stack.
1708 st_AllocTextureStorage(struct gl_context
*ctx
,
1709 struct gl_texture_object
*texObj
,
1710 GLsizei levels
, GLsizei width
,
1711 GLsizei height
, GLsizei depth
)
1713 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
1714 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
1715 struct st_context
*st
= st_context(ctx
);
1716 struct st_texture_object
*stObj
= st_texture_object(texObj
);
1717 struct pipe_screen
*screen
= st
->pipe
->screen
;
1718 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
1719 enum pipe_format fmt
;
1721 GLuint num_samples
= texImage
->NumSamples
;
1725 /* Save the level=0 dimensions */
1726 stObj
->width0
= width
;
1727 stObj
->height0
= height
;
1728 stObj
->depth0
= depth
;
1729 stObj
->lastLevel
= levels
- 1;
1731 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
1733 bindings
= default_bindings(st
, fmt
);
1735 /* Raise the sample count if the requested one is unsupported. */
1736 if (num_samples
> 1) {
1737 boolean found
= FALSE
;
1739 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
1740 if (screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
1742 PIPE_BIND_SAMPLER_VIEW
)) {
1743 /* Update the sample count in gl_texture_image as well. */
1744 texImage
->NumSamples
= num_samples
;
1755 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
1756 width
, height
, depth
,
1757 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1759 stObj
->pt
= st_texture_create(st
,
1760 gl_target_to_pipe(texObj
->Target
),
1766 ptLayers
, num_samples
,
1771 /* Set image resource pointers */
1772 for (level
= 0; level
< levels
; level
++) {
1774 for (face
= 0; face
< numFaces
; face
++) {
1775 struct st_texture_image
*stImage
=
1776 st_texture_image(texObj
->Image
[face
][level
]);
1777 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1786 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
1787 GLint level
, mesa_format format
,
1788 GLint width
, GLint height
,
1789 GLint depth
, GLint border
)
1791 struct st_context
*st
= st_context(ctx
);
1792 struct pipe_context
*pipe
= st
->pipe
;
1794 if (width
== 0 || height
== 0 || depth
== 0) {
1795 /* zero-sized images are legal, and always fit! */
1799 if (pipe
->screen
->can_create_resource
) {
1800 /* Ask the gallium driver if the texture is too large */
1801 struct gl_texture_object
*texObj
=
1802 _mesa_get_current_tex_object(ctx
, target
);
1803 struct pipe_resource pt
;
1805 /* Setup the pipe_resource object
1807 memset(&pt
, 0, sizeof(pt
));
1809 pt
.target
= gl_target_to_pipe(target
);
1810 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
1812 st_gl_texture_dims_to_pipe_dims(target
,
1813 width
, height
, depth
,
1814 &pt
.width0
, &pt
.height0
,
1815 &pt
.depth0
, &pt
.array_size
);
1817 if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
1818 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
1819 /* assume just one mipmap level */
1823 /* assume a full set of mipmaps */
1824 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
1827 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
1830 /* Use core Mesa fallback */
1831 return _mesa_test_proxy_teximage(ctx
, target
, level
, format
,
1832 width
, height
, depth
, border
);
1837 st_TextureView(struct gl_context
*ctx
,
1838 struct gl_texture_object
*texObj
,
1839 struct gl_texture_object
*origTexObj
)
1841 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
1842 struct st_texture_object
*tex
= st_texture_object(texObj
);
1843 struct gl_texture_image
*image
= texObj
->Image
[0][0];
1845 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
1846 const int numLevels
= texObj
->NumLevels
;
1851 pipe_resource_reference(&tex
->pt
, orig
->pt
);
1853 /* Set image resource pointers */
1854 for (level
= 0; level
< numLevels
; level
++) {
1855 for (face
= 0; face
< numFaces
; face
++) {
1856 struct st_texture_image
*stImage
=
1857 st_texture_image(texObj
->Image
[face
][level
]);
1858 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
1862 tex
->surface_based
= GL_TRUE
;
1863 tex
->surface_format
=
1864 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
1866 tex
->width0
= image
->Width
;
1867 tex
->height0
= image
->Height
;
1868 tex
->depth0
= image
->Depth
;
1869 tex
->lastLevel
= numLevels
- 1;
1876 st_init_texture_functions(struct dd_function_table
*functions
)
1878 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1879 functions
->QuerySamplesForFormat
= st_QuerySamplesForFormat
;
1880 functions
->TexImage
= st_TexImage
;
1881 functions
->TexSubImage
= st_TexSubImage
;
1882 functions
->CompressedTexSubImage
= _mesa_store_compressed_texsubimage
;
1883 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
1884 functions
->GenerateMipmap
= st_generate_mipmap
;
1886 functions
->GetTexSubImage
= st_GetTexSubImage
;
1888 /* compressed texture functions */
1889 functions
->CompressedTexImage
= st_CompressedTexImage
;
1890 functions
->GetCompressedTexImage
= _mesa_GetCompressedTexImage_sw
;
1892 functions
->NewTextureObject
= st_NewTextureObject
;
1893 functions
->NewTextureImage
= st_NewTextureImage
;
1894 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
1895 functions
->DeleteTexture
= st_DeleteTextureObject
;
1896 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
1897 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
1898 functions
->MapTextureImage
= st_MapTextureImage
;
1899 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
1901 /* XXX Temporary until we can query pipe's texture sizes */
1902 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
1904 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
1905 functions
->TextureView
= st_TextureView
;