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/errors.h"
32 #include "main/fbobject.h"
33 #include "main/formats.h"
34 #include "main/format_utils.h"
35 #include "main/glformats.h"
36 #include "main/image.h"
38 #include "main/macros.h"
39 #include "main/mipmap.h"
40 #include "main/pack.h"
42 #include "main/pixeltransfer.h"
43 #include "main/texcompress.h"
44 #include "main/texcompress_astc.h"
45 #include "main/texcompress_etc.h"
46 #include "main/texgetimage.h"
47 #include "main/teximage.h"
48 #include "main/texobj.h"
49 #include "main/texstore.h"
51 #include "state_tracker/st_debug.h"
52 #include "state_tracker/st_context.h"
53 #include "state_tracker/st_cb_bitmap.h"
54 #include "state_tracker/st_cb_fbo.h"
55 #include "state_tracker/st_cb_flush.h"
56 #include "state_tracker/st_cb_texture.h"
57 #include "state_tracker/st_cb_bufferobjects.h"
58 #include "state_tracker/st_cb_memoryobjects.h"
59 #include "state_tracker/st_format.h"
60 #include "state_tracker/st_pbo.h"
61 #include "state_tracker/st_texture.h"
62 #include "state_tracker/st_gen_mipmap.h"
63 #include "state_tracker/st_atom.h"
64 #include "state_tracker/st_sampler_view.h"
65 #include "state_tracker/st_util.h"
67 #include "pipe/p_context.h"
68 #include "pipe/p_defines.h"
69 #include "util/u_inlines.h"
70 #include "util/u_upload_mgr.h"
71 #include "pipe/p_shader_tokens.h"
72 #include "util/u_tile.h"
73 #include "util/format/u_format.h"
74 #include "util/u_surface.h"
75 #include "util/u_sampler.h"
76 #include "util/u_math.h"
77 #include "util/u_box.h"
78 #include "util/u_simple_shaders.h"
79 #include "cso_cache/cso_context.h"
80 #include "tgsi/tgsi_ureg.h"
82 #define DBG if (0) printf
85 enum pipe_texture_target
86 gl_target_to_pipe(GLenum target
)
90 case GL_PROXY_TEXTURE_1D
:
91 return PIPE_TEXTURE_1D
;
93 case GL_PROXY_TEXTURE_2D
:
94 case GL_TEXTURE_EXTERNAL_OES
:
95 case GL_TEXTURE_2D_MULTISAMPLE
:
96 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
97 return PIPE_TEXTURE_2D
;
98 case GL_TEXTURE_RECTANGLE_NV
:
99 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
100 return PIPE_TEXTURE_RECT
;
102 case GL_PROXY_TEXTURE_3D
:
103 return PIPE_TEXTURE_3D
;
104 case GL_TEXTURE_CUBE_MAP_ARB
:
105 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
106 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
107 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
108 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
109 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
110 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
111 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
112 return PIPE_TEXTURE_CUBE
;
113 case GL_TEXTURE_1D_ARRAY_EXT
:
114 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
115 return PIPE_TEXTURE_1D_ARRAY
;
116 case GL_TEXTURE_2D_ARRAY_EXT
:
117 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
118 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
119 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
120 return PIPE_TEXTURE_2D_ARRAY
;
121 case GL_TEXTURE_BUFFER
:
123 case GL_TEXTURE_CUBE_MAP_ARRAY
:
124 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
125 return PIPE_TEXTURE_CUBE_ARRAY
;
133 /** called via ctx->Driver.NewTextureImage() */
134 static struct gl_texture_image
*
135 st_NewTextureImage(struct gl_context
* ctx
)
137 DBG("%s\n", __func__
);
139 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
143 /** called via ctx->Driver.DeleteTextureImage() */
145 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
147 /* nothing special (yet) for st_texture_image */
148 _mesa_delete_texture_image(ctx
, img
);
152 /** called via ctx->Driver.NewTextureObject() */
153 static struct gl_texture_object
*
154 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
156 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
160 obj
->level_override
= -1;
161 obj
->layer_override
= -1;
163 /* Pre-allocate a sampler views container to save a branch in the
166 obj
->sampler_views
= calloc(1, sizeof(struct st_sampler_views
)
167 + sizeof(struct st_sampler_view
));
168 if (!obj
->sampler_views
) {
172 obj
->sampler_views
->max
= 1;
174 DBG("%s\n", __func__
);
175 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
177 simple_mtx_init(&obj
->validate_mutex
, mtx_plain
);
178 obj
->needs_validation
= true;
184 /** called via ctx->Driver.DeleteTextureObject() */
186 st_DeleteTextureObject(struct gl_context
*ctx
,
187 struct gl_texture_object
*texObj
)
189 struct st_context
*st
= st_context(ctx
);
190 struct st_texture_object
*stObj
= st_texture_object(texObj
);
192 pipe_resource_reference(&stObj
->pt
, NULL
);
193 st_delete_texture_sampler_views(st
, stObj
);
194 simple_mtx_destroy(&stObj
->validate_mutex
);
195 _mesa_delete_texture_object(ctx
, texObj
);
199 * Called via ctx->Driver.TextureRemovedFromShared()
200 * When texture is removed from ctx->Shared->TexObjects we lose
201 * the ability to clean up views on context destruction, which may
202 * lead to dangling pointers to destroyed contexts.
203 * Release the views to prevent this.
206 st_TextureReleaseAllSamplerViews(struct gl_context
*ctx
,
207 struct gl_texture_object
*texObj
)
209 struct st_context
*st
= st_context(ctx
);
210 struct st_texture_object
*stObj
= st_texture_object(texObj
);
212 st_texture_release_all_sampler_views(st
, stObj
);
215 /** called via ctx->Driver.FreeTextureImageBuffer() */
217 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
218 struct gl_texture_image
*texImage
)
220 struct st_context
*st
= st_context(ctx
);
221 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
222 struct st_texture_image
*stImage
= st_texture_image(texImage
);
224 DBG("%s\n", __func__
);
227 pipe_resource_reference(&stImage
->pt
, NULL
);
230 free(stImage
->transfer
);
231 stImage
->transfer
= NULL
;
232 stImage
->num_transfers
= 0;
234 if (stImage
->compressed_data
&&
235 pipe_reference(&stImage
->compressed_data
->reference
, NULL
)) {
236 free(stImage
->compressed_data
->ptr
);
237 free(stImage
->compressed_data
);
240 /* if the texture image is being deallocated, the structure of the
241 * texture is changing so we'll likely need a new sampler view.
243 st_texture_release_all_sampler_views(st
, stObj
);
247 st_astc_format_fallback(const struct st_context
*st
, mesa_format format
)
249 if (!_mesa_is_format_astc_2d(format
))
252 if (format
== MESA_FORMAT_RGBA_ASTC_5x5
||
253 format
== MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5
)
254 return !st
->has_astc_5x5_ldr
;
256 return !st
->has_astc_2d_ldr
;
260 st_compressed_format_fallback(struct st_context
*st
, mesa_format format
)
262 if (format
== MESA_FORMAT_ETC1_RGB8
)
263 return !st
->has_etc1
;
265 if (_mesa_is_format_etc2(format
))
266 return !st
->has_etc2
;
268 if (st_astc_format_fallback(st
, format
))
276 compressed_tex_fallback_allocate(struct st_context
*st
,
277 struct st_texture_image
*stImage
)
279 struct gl_texture_image
*texImage
= &stImage
->base
;
281 if (!st_compressed_format_fallback(st
, texImage
->TexFormat
))
284 if (stImage
->compressed_data
&&
285 pipe_reference(&stImage
->compressed_data
->reference
, NULL
)) {
286 free(stImage
->compressed_data
->ptr
);
287 free(stImage
->compressed_data
);
290 unsigned data_size
= _mesa_format_image_size(texImage
->TexFormat
,
295 stImage
->compressed_data
= ST_CALLOC_STRUCT(st_compressed_data
);
296 stImage
->compressed_data
->ptr
=
297 malloc(data_size
* _mesa_num_tex_faces(texImage
->TexObject
->Target
));
298 pipe_reference_init(&stImage
->compressed_data
->reference
, 1);
302 /** called via ctx->Driver.MapTextureImage() */
304 st_MapTextureImage(struct gl_context
*ctx
,
305 struct gl_texture_image
*texImage
,
306 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
308 GLubyte
**mapOut
, GLint
*rowStrideOut
)
310 struct st_context
*st
= st_context(ctx
);
311 struct st_texture_image
*stImage
= st_texture_image(texImage
);
313 struct pipe_transfer
*transfer
;
315 /* Check for unexpected flags */
316 assert((mode
& ~(GL_MAP_READ_BIT
|
318 GL_MAP_INVALIDATE_RANGE_BIT
)) == 0);
320 const enum pipe_transfer_usage transfer_flags
=
321 st_access_flags_to_transfer_flags(mode
, false);
323 map
= st_texture_image_map(st
, stImage
, transfer_flags
, x
, y
, slice
, w
, h
, 1,
326 if (st_compressed_format_fallback(st
, texImage
->TexFormat
)) {
327 /* Some compressed formats don't have to be supported by drivers,
328 * and st/mesa transparently handles decompression on upload (Unmap),
329 * so that drivers don't see the compressed formats.
331 * We store the compressed data (it's needed for glGetCompressedTex-
332 * Image and image copies in OES_copy_image).
334 unsigned z
= transfer
->box
.z
;
335 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
337 unsigned blk_w
, blk_h
;
338 _mesa_get_format_block_size(texImage
->TexFormat
, &blk_w
, &blk_h
);
340 unsigned y_blocks
= DIV_ROUND_UP(texImage
->Height2
, blk_h
);
341 unsigned stride
= *rowStrideOut
= itransfer
->temp_stride
=
342 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width2
);
343 unsigned block_size
= _mesa_get_format_bytes(texImage
->TexFormat
);
345 assert(stImage
->compressed_data
);
346 *mapOut
= itransfer
->temp_data
=
347 stImage
->compressed_data
->ptr
+
348 (z
* y_blocks
+ (y
/ blk_h
)) * stride
+
349 (x
/ blk_w
) * block_size
;
350 itransfer
->map
= map
;
353 /* supported mapping */
355 *rowStrideOut
= transfer
->stride
;
365 /** called via ctx->Driver.UnmapTextureImage() */
367 st_UnmapTextureImage(struct gl_context
*ctx
,
368 struct gl_texture_image
*texImage
,
371 struct st_context
*st
= st_context(ctx
);
372 struct st_texture_image
*stImage
= st_texture_image(texImage
);
374 if (st_compressed_format_fallback(st
, texImage
->TexFormat
)) {
375 /* Decompress the compressed image on upload if the driver doesn't
376 * support the compressed format. */
377 unsigned z
= slice
+ stImage
->base
.Face
;
378 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
379 struct pipe_transfer
*transfer
= itransfer
->transfer
;
381 assert(z
== transfer
->box
.z
);
383 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
384 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
385 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
386 itransfer
->temp_data
,
387 itransfer
->temp_stride
,
389 transfer
->box
.height
);
390 } else if (_mesa_is_format_etc2(texImage
->TexFormat
)) {
391 bool bgra
= stImage
->pt
->format
== PIPE_FORMAT_B8G8R8A8_SRGB
;
392 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
393 itransfer
->temp_data
,
394 itransfer
->temp_stride
,
395 transfer
->box
.width
, transfer
->box
.height
,
398 } else if (_mesa_is_format_astc_2d(texImage
->TexFormat
)) {
399 _mesa_unpack_astc_2d_ldr(itransfer
->map
, transfer
->stride
,
400 itransfer
->temp_data
,
401 itransfer
->temp_stride
,
402 transfer
->box
.width
, transfer
->box
.height
,
403 texImage
->TexFormat
);
405 unreachable("unexpected format for a compressed format fallback");
409 itransfer
->temp_data
= NULL
;
410 itransfer
->temp_stride
= 0;
414 st_texture_image_unmap(st
, stImage
, slice
);
419 * Return default texture resource binding bitmask for the given format.
422 default_bindings(struct st_context
*st
, enum pipe_format format
)
424 struct pipe_screen
*screen
= st
->pipe
->screen
;
425 const unsigned target
= PIPE_TEXTURE_2D
;
428 if (util_format_is_depth_or_stencil(format
))
429 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
431 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
433 if (screen
->is_format_supported(screen
, format
, target
, 0, 0, bindings
))
437 format
= util_format_linear(format
);
439 if (screen
->is_format_supported(screen
, format
, target
, 0, 0, bindings
))
442 return PIPE_BIND_SAMPLER_VIEW
;
448 * Given the size of a mipmap image, try to compute the size of the level=0
451 * Note that this isn't always accurate for odd-sized, non-POW textures.
452 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
454 * \return GL_TRUE for success, GL_FALSE for failure
457 guess_base_level_size(GLenum target
,
458 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
459 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
466 /* Guess the size of the base level.
467 * Depending on the image's size, we can't always make a guess here.
471 case GL_TEXTURE_1D_ARRAY
:
476 case GL_TEXTURE_2D_ARRAY
:
477 /* We can't make a good guess here, because the base level dimensions
480 if (width
== 1 || height
== 1) {
487 case GL_TEXTURE_CUBE_MAP
:
488 case GL_TEXTURE_CUBE_MAP_ARRAY
:
494 /* We can't make a good guess here, because the base level dimensions
497 if (width
== 1 || height
== 1 || depth
== 1) {
505 case GL_TEXTURE_RECTANGLE
:
522 * Try to determine whether we should allocate memory for a full texture
523 * mipmap. The problem is when we get a glTexImage(level=0) call, we
524 * can't immediately know if other mipmap levels are coming next. Here
525 * we try to guess whether to allocate memory for a mipmap or just the
528 * If we guess incorrectly here we'll later reallocate the right amount of
529 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
531 * \param stObj the texture object we're going to allocate memory for.
532 * \param stImage describes the incoming image which we need to store.
535 allocate_full_mipmap(const struct st_texture_object
*stObj
,
536 const struct st_texture_image
*stImage
)
538 switch (stObj
->base
.Target
) {
539 case GL_TEXTURE_RECTANGLE_NV
:
540 case GL_TEXTURE_BUFFER
:
541 case GL_TEXTURE_EXTERNAL_OES
:
542 case GL_TEXTURE_2D_MULTISAMPLE
:
543 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
544 /* these texture types cannot be mipmapped */
548 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
551 /* If the application has explicitly called glTextureParameter to set
552 * GL_TEXTURE_MAX_LEVEL, such that (max - base) > 0, then they're trying
553 * to communicate that they will have multiple miplevels.
555 * Core Mesa will initialize MaxLevel to value much larger than
556 * MAX_TEXTURE_LEVELS, so we check that to see if it's been set at all.
558 if (stObj
->base
.MaxLevel
< MAX_TEXTURE_LEVELS
&&
559 stObj
->base
.MaxLevel
- stObj
->base
.BaseLevel
> 0)
562 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
563 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
564 /* depth/stencil textures are seldom mipmapped */
567 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
570 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
571 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
572 /* not a mipmap minification filter */
575 /* If the following sequence of GL calls is used:
576 * glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, ...
577 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
579 * we would needlessly allocate a mipmapped texture, because the initial
580 * MinFilter is GL_NEAREST_MIPMAP_LINEAR. Catch this case and don't
581 * allocate a mipmapped texture by default. This may cause texture
582 * reallocation later, but GL_NEAREST_MIPMAP_LINEAR is pretty rare.
584 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST_MIPMAP_LINEAR
)
587 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
588 /* 3D textures are seldom mipmapped */
596 * Try to allocate a pipe_resource object for the given st_texture_object.
598 * We use the given st_texture_image as a clue to determine the size of the
599 * mipmap image at level=0.
601 * \return GL_TRUE for success, GL_FALSE if out of memory.
604 guess_and_alloc_texture(struct st_context
*st
,
605 struct st_texture_object
*stObj
,
606 const struct st_texture_image
*stImage
)
608 const struct gl_texture_image
*firstImage
;
609 GLuint lastLevel
, width
, height
, depth
;
612 uint16_t ptHeight
, ptDepth
, ptLayers
;
613 enum pipe_format fmt
;
614 bool guessed_box
= false;
616 DBG("%s\n", __func__
);
620 /* If a base level image with compatible size exists, use that as our guess.
622 firstImage
= _mesa_base_tex_image(&stObj
->base
);
624 firstImage
->Width2
> 0 &&
625 firstImage
->Height2
> 0 &&
626 firstImage
->Depth2
> 0 &&
627 guess_base_level_size(stObj
->base
.Target
,
632 &width
, &height
, &depth
)) {
633 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
634 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
635 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
640 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
641 stImage
->base
.Width2
,
642 stImage
->base
.Height2
,
643 stImage
->base
.Depth2
,
645 &width
, &height
, &depth
);
648 /* we can't determine the image size at level=0 */
649 /* this is not an out of memory error */
653 /* At this point, (width x height x depth) is the expected size of
654 * the level=0 mipmap image.
657 /* Guess a reasonable value for lastLevel. With OpenGL we have no
658 * idea how many mipmap levels will be in a texture until we start
659 * to render with it. Make an educated guess here but be prepared
660 * to re-allocating a texture buffer with space for more (or fewer)
661 * mipmap levels later.
663 if (allocate_full_mipmap(stObj
, stImage
)) {
664 /* alloc space for a full mipmap */
665 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
666 width
, height
, depth
) - 1;
669 /* only alloc space for a single mipmap level */
673 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
675 bindings
= default_bindings(st
, fmt
);
677 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
678 width
, height
, depth
,
679 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
681 stObj
->pt
= st_texture_create(st
,
682 gl_target_to_pipe(stObj
->base
.Target
),
691 stObj
->lastLevel
= lastLevel
;
693 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
695 return stObj
->pt
!= NULL
;
700 * Called via ctx->Driver.AllocTextureImageBuffer().
701 * If the texture object/buffer already has space for the indicated image,
702 * we're done. Otherwise, allocate memory for the new texture image.
705 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
706 struct gl_texture_image
*texImage
)
708 struct st_context
*st
= st_context(ctx
);
709 struct st_texture_image
*stImage
= st_texture_image(texImage
);
710 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
711 const GLuint level
= texImage
->Level
;
712 GLuint width
= texImage
->Width
;
713 GLuint height
= texImage
->Height
;
714 GLuint depth
= texImage
->Depth
;
716 DBG("%s\n", __func__
);
718 assert(!stImage
->pt
); /* xxx this might be wrong */
720 stObj
->needs_validation
= true;
722 compressed_tex_fallback_allocate(st
, stImage
);
724 /* Look if the parent texture object has space for this image */
726 level
<= stObj
->pt
->last_level
&&
727 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
728 /* this image will fit in the existing texture object's memory */
729 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
733 /* The parent texture object does not have space for this image */
735 pipe_resource_reference(&stObj
->pt
, NULL
);
736 st_texture_release_all_sampler_views(st
, stObj
);
738 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
739 /* Probably out of memory.
740 * Try flushing any pending rendering, then retry.
743 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
744 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
750 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
751 /* The image will live in the object's mipmap memory */
752 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
757 /* Create a new, temporary texture/resource/buffer to hold this
758 * one texture image. Note that when we later access this image
759 * (either for mapping or copying) we'll want to always specify
760 * mipmap level=0, even if the image represents some other mipmap
763 enum pipe_format format
=
764 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
765 GLuint bindings
= default_bindings(st
, format
);
767 uint16_t ptHeight
, ptDepth
, ptLayers
;
769 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
770 width
, height
, depth
,
771 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
773 stImage
->pt
= st_texture_create(st
,
774 gl_target_to_pipe(stObj
->base
.Target
),
782 return stImage
->pt
!= NULL
;
788 * Preparation prior to glTexImage. Basically check the 'surface_based'
789 * field and switch to a "normal" tex image if necessary.
792 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
793 GLenum format
, GLenum type
)
795 struct gl_texture_object
*texObj
= texImage
->TexObject
;
796 struct st_texture_object
*stObj
= st_texture_object(texObj
);
798 /* switch to "normal" */
799 if (stObj
->surface_based
) {
800 const GLenum target
= texObj
->Target
;
801 const GLuint level
= texImage
->Level
;
802 mesa_format texFormat
;
804 assert(!st_texture_image(texImage
)->pt
);
805 _mesa_clear_texture_object(ctx
, texObj
, texImage
);
806 stObj
->layer_override
= -1;
807 stObj
->level_override
= -1;
808 pipe_resource_reference(&stObj
->pt
, NULL
);
810 /* oops, need to init this image again */
811 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
812 texImage
->InternalFormat
, format
,
815 _mesa_init_teximage_fields(ctx
, texImage
,
816 texImage
->Width
, texImage
->Height
,
817 texImage
->Depth
, texImage
->Border
,
818 texImage
->InternalFormat
, texFormat
);
820 stObj
->surface_based
= GL_FALSE
;
826 * Return a writemask for the gallium blit. The parameters can be base
827 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
830 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
833 case GL_DEPTH_STENCIL
:
835 case GL_DEPTH_STENCIL
:
837 case GL_DEPTH_COMPONENT
:
839 case GL_STENCIL_INDEX
:
846 case GL_DEPTH_COMPONENT
:
848 case GL_DEPTH_STENCIL
:
849 case GL_DEPTH_COMPONENT
:
856 case GL_STENCIL_INDEX
:
858 case GL_DEPTH_STENCIL
:
859 case GL_STENCIL_INDEX
:
867 return PIPE_MASK_RGBA
;
872 * Converts format to a format with the same components, types
873 * and sizes, but with the components in RGBA order.
875 static enum pipe_format
876 unswizzle_format(enum pipe_format format
)
880 case PIPE_FORMAT_B8G8R8A8_UNORM
:
881 case PIPE_FORMAT_A8R8G8B8_UNORM
:
882 case PIPE_FORMAT_A8B8G8R8_UNORM
:
883 return PIPE_FORMAT_R8G8B8A8_UNORM
;
885 case PIPE_FORMAT_B10G10R10A2_UNORM
:
886 return PIPE_FORMAT_R10G10B10A2_UNORM
;
888 case PIPE_FORMAT_B10G10R10A2_SNORM
:
889 return PIPE_FORMAT_R10G10B10A2_SNORM
;
891 case PIPE_FORMAT_B10G10R10A2_UINT
:
892 return PIPE_FORMAT_R10G10B10A2_UINT
;
901 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
903 static enum pipe_format
904 alpha_to_red(enum pipe_format format
)
908 case PIPE_FORMAT_A8_UNORM
:
909 return PIPE_FORMAT_R8_UNORM
;
910 case PIPE_FORMAT_A8_SNORM
:
911 return PIPE_FORMAT_R8_SNORM
;
912 case PIPE_FORMAT_A8_UINT
:
913 return PIPE_FORMAT_R8_UINT
;
914 case PIPE_FORMAT_A8_SINT
:
915 return PIPE_FORMAT_R8_SINT
;
917 case PIPE_FORMAT_A16_UNORM
:
918 return PIPE_FORMAT_R16_UNORM
;
919 case PIPE_FORMAT_A16_SNORM
:
920 return PIPE_FORMAT_R16_SNORM
;
921 case PIPE_FORMAT_A16_UINT
:
922 return PIPE_FORMAT_R16_UINT
;
923 case PIPE_FORMAT_A16_SINT
:
924 return PIPE_FORMAT_R16_SINT
;
925 case PIPE_FORMAT_A16_FLOAT
:
926 return PIPE_FORMAT_R16_FLOAT
;
928 case PIPE_FORMAT_A32_UINT
:
929 return PIPE_FORMAT_R32_UINT
;
930 case PIPE_FORMAT_A32_SINT
:
931 return PIPE_FORMAT_R32_SINT
;
932 case PIPE_FORMAT_A32_FLOAT
:
933 return PIPE_FORMAT_R32_FLOAT
;
942 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
944 static enum pipe_format
945 red_alpha_to_red_green(enum pipe_format format
)
949 case PIPE_FORMAT_R8A8_UNORM
:
950 return PIPE_FORMAT_R8G8_UNORM
;
951 case PIPE_FORMAT_R8A8_SNORM
:
952 return PIPE_FORMAT_R8G8_SNORM
;
953 case PIPE_FORMAT_R8A8_UINT
:
954 return PIPE_FORMAT_R8G8_UINT
;
955 case PIPE_FORMAT_R8A8_SINT
:
956 return PIPE_FORMAT_R8G8_SINT
;
958 case PIPE_FORMAT_R16A16_UNORM
:
959 return PIPE_FORMAT_R16G16_UNORM
;
960 case PIPE_FORMAT_R16A16_SNORM
:
961 return PIPE_FORMAT_R16G16_SNORM
;
962 case PIPE_FORMAT_R16A16_UINT
:
963 return PIPE_FORMAT_R16G16_UINT
;
964 case PIPE_FORMAT_R16A16_SINT
:
965 return PIPE_FORMAT_R16G16_SINT
;
966 case PIPE_FORMAT_R16A16_FLOAT
:
967 return PIPE_FORMAT_R16G16_FLOAT
;
969 case PIPE_FORMAT_R32A32_UINT
:
970 return PIPE_FORMAT_R32G32_UINT
;
971 case PIPE_FORMAT_R32A32_SINT
:
972 return PIPE_FORMAT_R32G32_SINT
;
973 case PIPE_FORMAT_R32A32_FLOAT
:
974 return PIPE_FORMAT_R32G32_FLOAT
;
983 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
985 static enum pipe_format
986 luminance_alpha_to_red_green(enum pipe_format format
)
990 case PIPE_FORMAT_L8A8_UNORM
:
991 return PIPE_FORMAT_R8G8_UNORM
;
992 case PIPE_FORMAT_L8A8_SNORM
:
993 return PIPE_FORMAT_R8G8_SNORM
;
994 case PIPE_FORMAT_L8A8_UINT
:
995 return PIPE_FORMAT_R8G8_UINT
;
996 case PIPE_FORMAT_L8A8_SINT
:
997 return PIPE_FORMAT_R8G8_SINT
;
999 case PIPE_FORMAT_L16A16_UNORM
:
1000 return PIPE_FORMAT_R16G16_UNORM
;
1001 case PIPE_FORMAT_L16A16_SNORM
:
1002 return PIPE_FORMAT_R16G16_SNORM
;
1003 case PIPE_FORMAT_L16A16_UINT
:
1004 return PIPE_FORMAT_R16G16_UINT
;
1005 case PIPE_FORMAT_L16A16_SINT
:
1006 return PIPE_FORMAT_R16G16_SINT
;
1007 case PIPE_FORMAT_L16A16_FLOAT
:
1008 return PIPE_FORMAT_R16G16_FLOAT
;
1010 case PIPE_FORMAT_L32A32_UINT
:
1011 return PIPE_FORMAT_R32G32_UINT
;
1012 case PIPE_FORMAT_L32A32_SINT
:
1013 return PIPE_FORMAT_R32G32_SINT
;
1014 case PIPE_FORMAT_L32A32_FLOAT
:
1015 return PIPE_FORMAT_R32G32_FLOAT
;
1024 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
1027 format_is_alpha(enum pipe_format format
)
1029 const struct util_format_description
*desc
= util_format_description(format
);
1031 if (desc
->nr_channels
== 1 &&
1032 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
1033 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
1034 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
1035 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
1043 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
1046 format_is_red(enum pipe_format format
)
1048 const struct util_format_description
*desc
= util_format_description(format
);
1050 if (desc
->nr_channels
== 1 &&
1051 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
1052 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
1053 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
1054 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1062 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
1065 format_is_luminance(enum pipe_format format
)
1067 const struct util_format_description
*desc
= util_format_description(format
);
1069 if (desc
->nr_channels
== 1 &&
1070 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
1071 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
1072 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
1073 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1080 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
1083 format_is_red_alpha(enum pipe_format format
)
1085 const struct util_format_description
*desc
= util_format_description(format
);
1087 if (desc
->nr_channels
== 2 &&
1088 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
1089 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
1090 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
1091 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
1099 format_is_swizzled_rgba(enum pipe_format format
)
1101 const struct util_format_description
*desc
= util_format_description(format
);
1103 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
1104 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
1105 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
1106 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
1115 unsigned char swizzle
[4];
1116 enum pipe_format format
;
1119 static const struct format_table table_8888_unorm
[] = {
1120 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
1121 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
1122 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
1123 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
1126 static const struct format_table table_1010102_unorm
[] = {
1127 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
1128 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
1131 static const struct format_table table_1010102_snorm
[] = {
1132 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
1133 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
1136 static const struct format_table table_1010102_uint
[] = {
1137 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
1138 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
1141 static enum pipe_format
1142 swizzle_format(enum pipe_format format
, const int * const swizzle
)
1147 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1148 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1149 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1150 case PIPE_FORMAT_A8B8G8R8_UNORM
:
1151 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
1152 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
1153 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
1154 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
1155 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
1156 return table_8888_unorm
[i
].format
;
1160 case PIPE_FORMAT_R10G10B10A2_UNORM
:
1161 case PIPE_FORMAT_B10G10R10A2_UNORM
:
1162 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
1163 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
1164 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
1165 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
1166 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
1167 return table_1010102_unorm
[i
].format
;
1171 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1172 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1173 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1174 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1175 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1176 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1177 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1178 return table_1010102_snorm
[i
].format
;
1182 case PIPE_FORMAT_R10G10B10A2_UINT
:
1183 case PIPE_FORMAT_B10G10R10A2_UINT
:
1184 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1185 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1186 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1187 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1188 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1189 return table_1010102_uint
[i
].format
;
1197 return PIPE_FORMAT_NONE
;
1201 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1203 enum pipe_format src
= *src_format
;
1204 enum pipe_format dst
= *dst_format
;
1206 /* Note: dst_format has already been transformed from luminance/intensity
1207 * to red when this function is called. The source format will never
1208 * be an intensity format, because GL_INTENSITY is not a legal value
1209 * for the format parameter in glTex(Sub)Image(). */
1211 if (format_is_alpha(src
)) {
1212 if (!format_is_alpha(dst
))
1215 src
= alpha_to_red(src
);
1216 dst
= alpha_to_red(dst
);
1217 } else if (format_is_luminance(src
)) {
1218 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1221 src
= util_format_luminance_to_red(src
);
1222 } else if (util_format_is_luminance_alpha(src
)) {
1223 src
= luminance_alpha_to_red_green(src
);
1225 if (format_is_red_alpha(dst
)) {
1226 dst
= red_alpha_to_red_green(dst
);
1227 } else if (!format_is_red(dst
))
1229 } else if (format_is_swizzled_rgba(src
)) {
1230 const struct util_format_description
*src_desc
= util_format_description(src
);
1231 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1235 /* Make sure the format is an RGBA and not an RGBX format */
1236 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1239 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1242 for (i
= 0; i
< 4; i
++)
1243 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1245 dst
= swizzle_format(dst
, swizzle
);
1246 if (dst
== PIPE_FORMAT_NONE
)
1249 src
= unswizzle_format(src
);
1258 try_pbo_upload_common(struct gl_context
*ctx
,
1259 struct pipe_surface
*surface
,
1260 const struct st_pbo_addresses
*addr
,
1261 enum pipe_format src_format
)
1263 struct st_context
*st
= st_context(ctx
);
1264 struct cso_context
*cso
= st
->cso_context
;
1265 struct pipe_context
*pipe
= st
->pipe
;
1266 bool success
= false;
1269 fs
= st_pbo_get_upload_fs(st
, src_format
, surface
->format
);
1273 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1274 CSO_BIT_VERTEX_ELEMENTS
|
1275 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1276 CSO_BIT_FRAMEBUFFER
|
1279 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1280 CSO_BIT_RASTERIZER
|
1281 CSO_BIT_STREAM_OUTPUTS
|
1282 (st
->active_queries
? CSO_BIT_PAUSE_QUERIES
: 0) |
1283 CSO_BIT_SAMPLE_MASK
|
1284 CSO_BIT_MIN_SAMPLES
|
1285 CSO_BIT_RENDER_CONDITION
|
1286 CSO_BITS_ALL_SHADERS
));
1287 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1289 cso_set_sample_mask(cso
, ~0);
1290 cso_set_min_samples(cso
, 1);
1291 cso_set_render_condition(cso
, NULL
, FALSE
, 0);
1293 /* Set up the sampler_view */
1295 struct pipe_sampler_view templ
;
1296 struct pipe_sampler_view
*sampler_view
;
1298 memset(&templ
, 0, sizeof(templ
));
1299 templ
.target
= PIPE_BUFFER
;
1300 templ
.format
= src_format
;
1301 templ
.u
.buf
.offset
= addr
->first_element
* addr
->bytes_per_pixel
;
1302 templ
.u
.buf
.size
= (addr
->last_element
- addr
->first_element
+ 1) *
1303 addr
->bytes_per_pixel
;
1304 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1305 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1306 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1307 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1309 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1310 if (sampler_view
== NULL
)
1313 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1315 pipe_sampler_view_reference(&sampler_view
, NULL
);
1318 /* Framebuffer_state */
1320 struct pipe_framebuffer_state fb
;
1321 memset(&fb
, 0, sizeof(fb
));
1322 fb
.width
= surface
->width
;
1323 fb
.height
= surface
->height
;
1325 fb
.cbufs
[0] = surface
;
1327 cso_set_framebuffer(cso
, &fb
);
1330 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1333 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1335 /* Depth/stencil/alpha state */
1337 struct pipe_depth_stencil_alpha_state dsa
;
1338 memset(&dsa
, 0, sizeof(dsa
));
1339 cso_set_depth_stencil_alpha(cso
, &dsa
);
1342 /* Set up the fragment shader */
1343 cso_set_fragment_shader_handle(cso
, fs
);
1345 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1348 cso_restore_state(cso
);
1349 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1356 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1357 struct gl_texture_image
*texImage
,
1358 GLenum format
, GLenum type
,
1359 enum pipe_format dst_format
,
1360 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1361 GLint width
, GLint height
, GLint depth
,
1363 const struct gl_pixelstore_attrib
*unpack
)
1365 struct st_context
*st
= st_context(ctx
);
1366 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1367 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1368 struct pipe_resource
*texture
= stImage
->pt
;
1369 struct pipe_context
*pipe
= st
->pipe
;
1370 struct pipe_screen
*screen
= pipe
->screen
;
1371 struct pipe_surface
*surface
= NULL
;
1372 struct st_pbo_addresses addr
;
1373 enum pipe_format src_format
;
1374 const struct util_format_description
*desc
;
1375 GLenum gl_target
= texImage
->TexObject
->Target
;
1378 if (!st
->pbo
.upload_enabled
)
1381 /* From now on, we need the gallium representation of dimensions. */
1382 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1389 if (depth
!= 1 && !st
->pbo
.layers
)
1392 /* Choose the source format. Initially, we do so without checking driver
1393 * support at all because of the remapping we later perform and because
1394 * at least the Radeon driver actually supports some formats for texture
1395 * buffers which it doesn't support for regular textures. */
1396 src_format
= st_choose_matching_format(st
, 0, format
, type
,
1402 src_format
= util_format_linear(src_format
);
1403 desc
= util_format_description(src_format
);
1405 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1408 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1411 if (st
->pbo
.rgba_only
) {
1412 enum pipe_format orig_dst_format
= dst_format
;
1414 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1418 if (dst_format
!= orig_dst_format
&&
1419 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1420 0, PIPE_BIND_RENDER_TARGET
)) {
1426 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0, 0,
1427 PIPE_BIND_SAMPLER_VIEW
)) {
1431 /* Compute buffer addresses */
1432 addr
.xoffset
= xoffset
;
1433 addr
.yoffset
= yoffset
;
1435 addr
.height
= height
;
1437 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1439 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1443 /* Set up the surface */
1445 unsigned level
= stObj
->pt
!= stImage
->pt
1446 ? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1447 unsigned max_layer
= util_max_layer(texture
, level
);
1449 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1451 struct pipe_surface templ
;
1452 memset(&templ
, 0, sizeof(templ
));
1453 templ
.format
= dst_format
;
1454 templ
.u
.tex
.level
= level
;
1455 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1456 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1458 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1463 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1465 pipe_surface_reference(&surface
, NULL
);
1472 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1473 struct gl_texture_image
*texImage
,
1474 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1475 GLint width
, GLint height
, GLint depth
,
1476 GLenum format
, GLenum type
, const void *pixels
,
1477 const struct gl_pixelstore_attrib
*unpack
)
1479 struct st_context
*st
= st_context(ctx
);
1480 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1481 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1482 struct pipe_context
*pipe
= st
->pipe
;
1483 struct pipe_screen
*screen
= pipe
->screen
;
1484 struct pipe_resource
*dst
= stImage
->pt
;
1485 struct pipe_resource
*src
= NULL
;
1486 struct pipe_resource src_templ
;
1487 struct pipe_transfer
*transfer
;
1488 struct pipe_blit_info blit
;
1489 enum pipe_format src_format
, dst_format
;
1490 mesa_format mesa_src_format
;
1491 GLenum gl_target
= texImage
->TexObject
->Target
;
1494 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1495 unsigned dst_level
= 0;
1496 bool throttled
= false;
1498 st_flush_bitmap_cache(st
);
1499 st_invalidate_readpix_cache(st
);
1501 if (stObj
->pt
== stImage
->pt
)
1502 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1504 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1505 !_mesa_is_format_astc_2d(texImage
->TexFormat
) &&
1506 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1511 /* Try texture_subdata, which should be the fastest memcpy path. */
1513 !unpack
->BufferObj
&&
1514 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1515 texImage
->TexFormat
, format
, type
,
1517 struct pipe_box box
;
1518 unsigned stride
, layer_stride
;
1521 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1522 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1524 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1527 /* Convert to Gallium coordinates. */
1528 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1533 layer_stride
= stride
;
1536 util_throttle_memory_usage(pipe
, &st
->throttle
,
1537 (uint64_t) width
* height
* depth
*
1538 util_format_get_blocksize(dst
->format
));
1540 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1541 pipe
->texture_subdata(pipe
, dst
, dst_level
, 0,
1542 &box
, data
, stride
, layer_stride
);
1546 if (!st
->prefer_blit_based_texture_transfer
) {
1550 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1551 * blit implementation in some drivers. */
1552 if (format
== GL_DEPTH_STENCIL
) {
1556 /* If the base internal format and the texture format don't match,
1557 * we can't use blit-based TexSubImage. */
1558 if (texImage
->_BaseFormat
!=
1559 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1564 /* See if the destination format is supported. */
1565 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1566 bind
= PIPE_BIND_DEPTH_STENCIL
;
1568 bind
= PIPE_BIND_RENDER_TARGET
;
1570 /* For luminance and intensity, only the red channel is stored
1571 * in the destination. */
1572 dst_format
= util_format_linear(dst
->format
);
1573 dst_format
= util_format_luminance_to_red(dst_format
);
1574 dst_format
= util_format_intensity_to_red(dst_format
);
1577 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1578 dst
->nr_samples
, dst
->nr_storage_samples
,
1583 if (unpack
->BufferObj
) {
1584 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1585 xoffset
, yoffset
, zoffset
,
1586 width
, height
, depth
, pixels
, unpack
))
1590 /* See if the texture format already matches the format and type,
1591 * in which case the memcpy-based fast path will likely be used and
1592 * we don't have to blit. */
1593 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1594 type
, unpack
->SwapBytes
, NULL
)) {
1598 /* Choose the source format. */
1599 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1600 format
, type
, unpack
->SwapBytes
);
1605 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1607 /* There is no reason to do this if we cannot use memcpy for the temporary
1608 * source texture at least. This also takes transfer ops into account,
1610 if (!_mesa_texstore_can_use_memcpy(ctx
,
1611 _mesa_get_format_base_format(mesa_src_format
),
1612 mesa_src_format
, format
, type
, unpack
)) {
1616 /* TexSubImage only sets a single cubemap face. */
1617 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1618 gl_target
= GL_TEXTURE_2D
;
1620 /* TexSubImage can specify subsets of cube map array faces
1621 * so we need to upload via 2D array instead */
1622 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1623 gl_target
= GL_TEXTURE_2D_ARRAY
;
1626 /* Initialize the source texture description. */
1627 memset(&src_templ
, 0, sizeof(src_templ
));
1628 src_templ
.target
= gl_target_to_pipe(gl_target
);
1629 src_templ
.format
= src_format
;
1630 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1631 src_templ
.usage
= PIPE_USAGE_STAGING
;
1633 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1634 &src_templ
.width0
, &src_templ
.height0
,
1635 &src_templ
.depth0
, &src_templ
.array_size
);
1637 /* Check for NPOT texture support. */
1638 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1639 (!util_is_power_of_two_or_zero(src_templ
.width0
) ||
1640 !util_is_power_of_two_or_zero(src_templ
.height0
) ||
1641 !util_is_power_of_two_or_zero(src_templ
.depth0
))) {
1645 util_throttle_memory_usage(pipe
, &st
->throttle
,
1646 (uint64_t) width
* height
* depth
*
1647 util_format_get_blocksize(src_templ
.format
));
1650 /* Create the source texture. */
1651 src
= screen
->resource_create(screen
, &src_templ
);
1656 /* Map source pixels. */
1657 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1658 format
, type
, pixels
, unpack
,
1661 /* This is a GL error. */
1662 pipe_resource_reference(&src
, NULL
);
1666 /* From now on, we need the gallium representation of dimensions. */
1667 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1674 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1675 width
, height
, depth
, &transfer
);
1677 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1678 pipe_resource_reference(&src
, NULL
);
1682 /* Upload pixels (just memcpy). */
1684 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1687 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1688 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1689 /* 1D array textures.
1690 * We need to convert gallium coords to GL coords.
1692 void *src
= _mesa_image_address2d(unpack
, pixels
,
1693 width
, depth
, format
,
1695 memcpy(map
, src
, bytesPerRow
);
1698 ubyte
*slice_map
= map
;
1700 for (row
= 0; row
< (unsigned) height
; row
++) {
1701 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1702 width
, height
, format
,
1703 type
, slice
, row
, 0);
1704 memcpy(slice_map
, src
, bytesPerRow
);
1705 slice_map
+= transfer
->stride
;
1708 map
+= transfer
->layer_stride
;
1712 pipe_transfer_unmap(pipe
, transfer
);
1713 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1716 memset(&blit
, 0, sizeof(blit
));
1717 blit
.src
.resource
= src
;
1719 blit
.src
.format
= src_format
;
1720 blit
.dst
.resource
= dst
;
1721 blit
.dst
.level
= dst_level
;
1722 blit
.dst
.format
= dst_format
;
1723 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1724 blit
.dst
.box
.x
= xoffset
;
1725 blit
.dst
.box
.y
= yoffset
;
1726 blit
.dst
.box
.z
= zoffset
+ dstz
;
1727 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1728 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1729 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1730 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1731 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1732 blit
.scissor_enable
= FALSE
;
1734 st
->pipe
->blit(st
->pipe
, &blit
);
1736 pipe_resource_reference(&src
, NULL
);
1741 util_throttle_memory_usage(pipe
, &st
->throttle
,
1742 (uint64_t) width
* height
* depth
*
1743 _mesa_get_format_bytes(texImage
->TexFormat
));
1745 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1746 width
, height
, depth
, format
, type
, pixels
,
1752 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1753 struct gl_texture_image
*texImage
,
1754 GLenum format
, GLenum type
, const void *pixels
,
1755 const struct gl_pixelstore_attrib
*unpack
)
1757 assert(dims
== 1 || dims
== 2 || dims
== 3);
1759 prep_teximage(ctx
, texImage
, format
, type
);
1761 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1764 /* allocate storage for texture data */
1765 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1766 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1770 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1771 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1772 format
, type
, pixels
, unpack
);
1777 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1778 struct gl_texture_image
*texImage
,
1779 GLint x
, GLint y
, GLint z
,
1780 GLsizei w
, GLsizei h
, GLsizei d
,
1781 GLenum format
, GLsizei imageSize
, const void *data
)
1783 struct st_context
*st
= st_context(ctx
);
1784 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1785 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1786 struct pipe_resource
*texture
= stImage
->pt
;
1787 struct pipe_context
*pipe
= st
->pipe
;
1788 struct pipe_screen
*screen
= pipe
->screen
;
1789 struct pipe_resource
*dst
= stImage
->pt
;
1790 struct pipe_surface
*surface
= NULL
;
1791 struct compressed_pixelstore store
;
1792 struct st_pbo_addresses addr
;
1793 enum pipe_format copy_format
;
1795 intptr_t buf_offset
;
1796 bool success
= false;
1798 /* Check basic pre-conditions for PBO upload */
1799 if (!st
->prefer_blit_based_texture_transfer
) {
1803 if (!ctx
->Unpack
.BufferObj
)
1806 if (st_compressed_format_fallback(st
, texImage
->TexFormat
))
1813 if (!st
->pbo
.upload_enabled
||
1814 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1818 /* Choose the pipe format for the upload. */
1819 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1820 bw
= util_format_get_blockwidth(dst
->format
);
1821 bh
= util_format_get_blockheight(dst
->format
);
1823 switch (addr
.bytes_per_pixel
) {
1825 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1828 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1834 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0, 0,
1835 PIPE_BIND_SAMPLER_VIEW
)) {
1839 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1840 dst
->nr_samples
, dst
->nr_storage_samples
,
1841 PIPE_BIND_RENDER_TARGET
)) {
1845 /* Interpret the pixelstore settings. */
1846 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1847 &ctx
->Unpack
, &store
);
1848 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1849 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1851 /* Compute the offset into the buffer */
1852 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1854 if (buf_offset
% addr
.bytes_per_pixel
) {
1858 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1860 addr
.xoffset
= x
/ bw
;
1861 addr
.yoffset
= y
/ bh
;
1862 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1863 addr
.height
= store
.CopyRowsPerSlice
;
1865 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1866 addr
.image_height
= store
.TotalRowsPerSlice
;
1868 if (!st_pbo_addresses_setup(st
,
1869 st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1873 /* Set up the surface. */
1875 unsigned level
= stObj
->pt
!= stImage
->pt
1876 ? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1877 unsigned max_layer
= util_max_layer(texture
, level
);
1879 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1881 struct pipe_surface templ
;
1882 memset(&templ
, 0, sizeof(templ
));
1883 templ
.format
= copy_format
;
1884 templ
.u
.tex
.level
= level
;
1885 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1886 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1888 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1893 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1895 pipe_surface_reference(&surface
, NULL
);
1901 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1903 format
, imageSize
, data
);
1908 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1909 struct gl_texture_image
*texImage
,
1910 GLsizei imageSize
, const void *data
)
1912 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1914 /* only 2D and 3D compressed images are supported at this time */
1916 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1920 /* This is pretty simple, because unlike the general texstore path we don't
1921 * have to worry about the usual image unpacking or image transfer
1925 assert(texImage
->Width
> 0);
1926 assert(texImage
->Height
> 0);
1927 assert(texImage
->Depth
> 0);
1929 /* allocate storage for texture data */
1930 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1931 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1935 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1937 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1938 texImage
->TexFormat
,
1944 * Called via ctx->Driver.GetTexSubImage()
1946 * This uses a blit to copy the texture to a texture format which matches
1947 * the format and type combo and then a fast read-back is done using memcpy.
1948 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1949 * a format which matches the swizzling.
1951 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1953 * NOTE: Drivers usually do a blit to convert between tiled and linear
1954 * texture layouts during texture uploads/downloads, so the blit
1955 * we do here should be free in such cases.
1958 st_GetTexSubImage(struct gl_context
* ctx
,
1959 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1960 GLsizei width
, GLsizei height
, GLint depth
,
1961 GLenum format
, GLenum type
, void * pixels
,
1962 struct gl_texture_image
*texImage
)
1964 struct st_context
*st
= st_context(ctx
);
1965 struct pipe_context
*pipe
= st
->pipe
;
1966 struct pipe_screen
*screen
= pipe
->screen
;
1967 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1968 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1969 struct pipe_resource
*src
= stObj
->pt
;
1970 struct pipe_resource
*dst
= NULL
;
1971 struct pipe_resource dst_templ
;
1972 enum pipe_format dst_format
, src_format
;
1973 mesa_format mesa_format
;
1974 GLenum gl_target
= texImage
->TexObject
->Target
;
1975 enum pipe_texture_target pipe_target
;
1977 struct pipe_blit_info blit
;
1979 struct pipe_transfer
*tex_xfer
;
1981 boolean done
= FALSE
;
1983 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1984 !_mesa_is_format_astc_2d(texImage
->TexFormat
) &&
1985 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1987 st_flush_bitmap_cache(st
);
1989 if (!st
->prefer_blit_based_texture_transfer
&&
1990 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1991 /* Try to avoid the fallback if we're doing texture decompression here */
1995 /* Handle non-finalized textures. */
1996 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
2000 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
2001 * due to an incomplete stencil blit implementation in some drivers. */
2002 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
2006 /* If the base internal format and the texture format don't match, we have
2007 * to fall back to _mesa_GetTexImage_sw. */
2008 if (texImage
->_BaseFormat
!=
2009 _mesa_get_format_base_format(texImage
->TexFormat
)) {
2013 /* See if the texture format already matches the format and type,
2014 * in which case the memcpy-based fast path will be used. */
2015 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
2016 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
2020 /* Convert the source format to what is expected by GetTexImage
2021 * and see if it's supported.
2023 * This only applies to glGetTexImage:
2024 * - Luminance must be returned as (L,0,0,1).
2025 * - Luminance alpha must be returned as (L,0,0,A).
2026 * - Intensity must be returned as (I,0,0,1)
2028 if (stObj
->surface_based
)
2029 src_format
= util_format_linear(stObj
->surface_format
);
2031 src_format
= util_format_linear(src
->format
);
2032 src_format
= util_format_luminance_to_red(src_format
);
2033 src_format
= util_format_intensity_to_red(src_format
);
2036 !screen
->is_format_supported(screen
, src_format
, src
->target
,
2037 src
->nr_samples
, src
->nr_storage_samples
,
2038 PIPE_BIND_SAMPLER_VIEW
)) {
2042 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
2043 bind
= PIPE_BIND_DEPTH_STENCIL
;
2045 bind
= PIPE_BIND_RENDER_TARGET
;
2047 /* GetTexImage only returns a single face for cubemaps. */
2048 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
2049 gl_target
= GL_TEXTURE_2D
;
2051 pipe_target
= gl_target_to_pipe(gl_target
);
2053 /* Choose the destination format by finding the best match
2054 * for the format+type combo. */
2055 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
2056 ctx
->Pack
.SwapBytes
);
2058 if (dst_format
== PIPE_FORMAT_NONE
) {
2059 GLenum dst_glformat
;
2061 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
2062 * where decompression with a blit is always preferred. */
2063 if (!util_format_is_compressed(src
->format
)) {
2067 /* Set the appropriate format for the decompressed texture.
2068 * Luminance and sRGB formats shouldn't appear here.*/
2069 switch (src_format
) {
2070 case PIPE_FORMAT_DXT1_RGB
:
2071 case PIPE_FORMAT_DXT1_RGBA
:
2072 case PIPE_FORMAT_DXT3_RGBA
:
2073 case PIPE_FORMAT_DXT5_RGBA
:
2074 case PIPE_FORMAT_RGTC1_UNORM
:
2075 case PIPE_FORMAT_RGTC2_UNORM
:
2076 case PIPE_FORMAT_ETC1_RGB8
:
2077 case PIPE_FORMAT_ETC2_RGB8
:
2078 case PIPE_FORMAT_ETC2_RGB8A1
:
2079 case PIPE_FORMAT_ETC2_RGBA8
:
2080 case PIPE_FORMAT_ASTC_4x4
:
2081 case PIPE_FORMAT_ASTC_5x4
:
2082 case PIPE_FORMAT_ASTC_5x5
:
2083 case PIPE_FORMAT_ASTC_6x5
:
2084 case PIPE_FORMAT_ASTC_6x6
:
2085 case PIPE_FORMAT_ASTC_8x5
:
2086 case PIPE_FORMAT_ASTC_8x6
:
2087 case PIPE_FORMAT_ASTC_8x8
:
2088 case PIPE_FORMAT_ASTC_10x5
:
2089 case PIPE_FORMAT_ASTC_10x6
:
2090 case PIPE_FORMAT_ASTC_10x8
:
2091 case PIPE_FORMAT_ASTC_10x10
:
2092 case PIPE_FORMAT_ASTC_12x10
:
2093 case PIPE_FORMAT_ASTC_12x12
:
2094 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
2095 case PIPE_FORMAT_FXT1_RGB
:
2096 case PIPE_FORMAT_FXT1_RGBA
:
2097 dst_glformat
= GL_RGBA8
;
2099 case PIPE_FORMAT_RGTC1_SNORM
:
2100 case PIPE_FORMAT_RGTC2_SNORM
:
2101 if (!ctx
->Extensions
.EXT_texture_snorm
)
2103 dst_glformat
= GL_RGBA8_SNORM
;
2105 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
2106 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
2107 if (!ctx
->Extensions
.ARB_texture_float
)
2109 dst_glformat
= GL_RGBA32F
;
2111 case PIPE_FORMAT_ETC2_R11_UNORM
:
2112 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16_UNORM
,
2113 pipe_target
, 0, 0, bind
))
2115 dst_glformat
= GL_R16
;
2117 case PIPE_FORMAT_ETC2_R11_SNORM
:
2118 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16_SNORM
,
2119 pipe_target
, 0, 0, bind
))
2121 dst_glformat
= GL_R16_SNORM
;
2123 case PIPE_FORMAT_ETC2_RG11_UNORM
:
2124 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16G16_UNORM
,
2125 pipe_target
, 0, 0, bind
))
2127 dst_glformat
= GL_RG16
;
2129 case PIPE_FORMAT_ETC2_RG11_SNORM
:
2130 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16G16_SNORM
,
2131 pipe_target
, 0, 0, bind
))
2133 dst_glformat
= GL_RG16_SNORM
;
2140 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
2141 pipe_target
, 0, 0, bind
,
2144 if (dst_format
== PIPE_FORMAT_NONE
) {
2145 /* unable to get an rgba format!?! */
2150 /* create the destination texture of size (width X height X depth) */
2151 memset(&dst_templ
, 0, sizeof(dst_templ
));
2152 dst_templ
.target
= pipe_target
;
2153 dst_templ
.format
= dst_format
;
2154 dst_templ
.bind
= bind
;
2155 dst_templ
.usage
= PIPE_USAGE_STAGING
;
2157 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
2158 &dst_templ
.width0
, &dst_templ
.height0
,
2159 &dst_templ
.depth0
, &dst_templ
.array_size
);
2161 dst
= screen
->resource_create(screen
, &dst_templ
);
2166 /* From now on, we need the gallium representation of dimensions. */
2167 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2174 assert(texImage
->Face
== 0 ||
2175 texImage
->TexObject
->MinLayer
== 0 ||
2178 memset(&blit
, 0, sizeof(blit
));
2179 blit
.src
.resource
= src
;
2180 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2181 blit
.src
.format
= src_format
;
2182 blit
.dst
.resource
= dst
;
2184 blit
.dst
.format
= dst
->format
;
2185 blit
.src
.box
.x
= xoffset
;
2187 blit
.src
.box
.y
= yoffset
;
2189 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2191 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2192 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2193 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2194 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2195 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2196 blit
.scissor_enable
= FALSE
;
2198 /* blit/render/decompress */
2199 st
->pipe
->blit(st
->pipe
, &blit
);
2201 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2203 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2204 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2209 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2210 dims
= _mesa_get_texture_dimensions(gl_target
);
2212 /* copy/pack data into user buffer */
2213 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2214 ctx
->Pack
.SwapBytes
, NULL
)) {
2216 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2219 for (slice
= 0; slice
< depth
; slice
++) {
2220 ubyte
*slice_map
= map
;
2222 for (row
= 0; row
< height
; row
++) {
2223 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2224 width
, height
, format
, type
,
2227 memcpy(dest
, slice_map
, bytesPerRow
);
2229 slice_map
+= tex_xfer
->stride
;
2232 map
+= tex_xfer
->layer_stride
;
2236 /* format translation via floats */
2239 uint32_t dstMesaFormat
;
2240 int dstStride
, srcStride
;
2242 assert(util_format_is_compressed(src
->format
));
2244 rgba
= malloc(width
* height
* 4 * sizeof(GLfloat
));
2249 if (ST_DEBUG
& DEBUG_FALLBACK
)
2250 debug_printf("%s: fallback format translation\n", __func__
);
2252 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2253 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2254 srcStride
= 4 * width
* sizeof(GLfloat
);
2255 for (slice
= 0; slice
< depth
; slice
++) {
2256 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2257 width
, height
, format
, type
,
2260 /* get float[4] rgba row from surface */
2261 pipe_get_tile_rgba(tex_xfer
, map
, 0, 0, width
, height
, dst_format
,
2264 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2265 rgba
, RGBA32_FLOAT
, srcStride
,
2266 width
, height
, NULL
);
2268 /* Handle byte swapping if required */
2269 if (ctx
->Pack
.SwapBytes
) {
2270 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
2271 width
, height
, dest
, dest
);
2274 map
+= tex_xfer
->layer_stride
;
2283 pipe_transfer_unmap(pipe
, tex_xfer
);
2285 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2286 pipe_resource_reference(&dst
, NULL
);
2290 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2291 width
, height
, depth
,
2292 format
, type
, pixels
, texImage
);
2298 * Do a CopyTexSubImage operation using a read transfer from the source,
2299 * a write transfer to the destination and get_tile()/put_tile() to access
2300 * the pixels/texels.
2302 * Note: srcY=0=TOP of renderbuffer
2305 fallback_copy_texsubimage(struct gl_context
*ctx
,
2306 struct st_renderbuffer
*strb
,
2307 struct st_texture_image
*stImage
,
2309 GLint destX
, GLint destY
, GLint slice
,
2310 GLint srcX
, GLint srcY
,
2311 GLsizei width
, GLsizei height
)
2313 struct st_context
*st
= st_context(ctx
);
2314 struct pipe_context
*pipe
= st
->pipe
;
2315 struct pipe_transfer
*src_trans
;
2317 enum pipe_transfer_usage transfer_usage
;
2319 unsigned dst_width
= width
;
2320 unsigned dst_height
= height
;
2321 unsigned dst_depth
= 1;
2322 struct pipe_transfer
*transfer
;
2324 if (ST_DEBUG
& DEBUG_FALLBACK
)
2325 debug_printf("%s: fallback processing\n", __func__
);
2327 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2328 srcY
= strb
->Base
.Height
- srcY
- height
;
2331 map
= pipe_transfer_map(pipe
,
2333 strb
->surface
->u
.tex
.level
,
2334 strb
->surface
->u
.tex
.first_layer
,
2337 width
, height
, &src_trans
);
2339 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2343 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2344 baseFormat
== GL_DEPTH_STENCIL
) &&
2345 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2346 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2348 transfer_usage
= PIPE_TRANSFER_WRITE
;
2350 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2351 destX
, destY
, slice
,
2352 dst_width
, dst_height
, dst_depth
,
2355 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2359 if (baseFormat
== GL_DEPTH_COMPONENT
||
2360 baseFormat
== GL_DEPTH_STENCIL
) {
2361 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2362 ctx
->Pixel
.DepthBias
!= 0.0F
);
2366 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2367 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2376 data
= malloc(width
* sizeof(uint
));
2379 unsigned dst_stride
= (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
?
2380 transfer
->layer_stride
: transfer
->stride
);
2381 /* To avoid a large temp memory allocation, do copy row by row */
2382 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2383 util_format_unpack_z_32unorm(strb
->texture
->format
,
2384 data
, (uint8_t *)map
+ src_trans
->stride
* srcY
,
2387 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2390 util_format_pack_z_32unorm(stImage
->pt
->format
,
2391 texDest
+ row
* dst_stride
, data
, width
);
2395 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2403 malloc(width
* height
* 4 * sizeof(GLfloat
));
2406 const GLint dims
= 2;
2408 struct gl_texture_image
*texImage
= &stImage
->base
;
2409 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2411 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2412 unpack
.Invert
= GL_TRUE
;
2415 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2416 dstRowStride
= transfer
->layer_stride
;
2419 dstRowStride
= transfer
->stride
;
2422 /* get float/RGBA image from framebuffer */
2423 /* XXX this usually involves a lot of int/float conversion.
2424 * try to avoid that someday.
2426 pipe_get_tile_rgba(src_trans
, map
, 0, 0, width
, height
,
2427 util_format_linear(strb
->texture
->format
),
2430 /* Store into texture memory.
2431 * Note that this does some special things such as pixel transfer
2432 * ops and format conversion. In particular, if the dest tex format
2433 * is actually RGBA but the user created the texture as GL_RGB we
2434 * need to fill-in/override the alpha channel with 1.0.
2436 _mesa_texstore(ctx
, dims
,
2437 texImage
->_BaseFormat
,
2438 texImage
->TexFormat
,
2442 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2446 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2452 st_texture_image_unmap(st
, stImage
, slice
);
2454 pipe
->transfer_unmap(pipe
, src_trans
);
2459 st_can_copyteximage_using_blit(const struct gl_texture_image
*texImage
,
2460 const struct gl_renderbuffer
*rb
)
2462 GLenum tex_baseformat
= _mesa_get_format_base_format(texImage
->TexFormat
);
2464 /* We don't blit to a teximage where the GL base format doesn't match the
2465 * texture's chosen format, except in the case of a GL_RGB texture
2466 * represented with GL_RGBA (where the alpha channel is just being
2469 if (texImage
->_BaseFormat
!= tex_baseformat
&&
2470 ((texImage
->_BaseFormat
!= GL_RGB
|| tex_baseformat
!= GL_RGBA
))) {
2474 /* We can't blit from a RB where the GL base format doesn't match the RB's
2475 * chosen format (for example, GL RGB or ALPHA with rb->Format of an RGBA
2476 * type, because the other channels will be undefined).
2478 if (rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
))
2486 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2487 * Note that the region to copy has already been clipped so we know we
2488 * won't read from outside the source renderbuffer's bounds.
2490 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2493 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2494 struct gl_texture_image
*texImage
,
2495 GLint destX
, GLint destY
, GLint slice
,
2496 struct gl_renderbuffer
*rb
,
2497 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2499 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2500 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2501 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2502 struct st_context
*st
= st_context(ctx
);
2503 struct pipe_context
*pipe
= st
->pipe
;
2504 struct pipe_screen
*screen
= pipe
->screen
;
2505 struct pipe_blit_info blit
;
2506 enum pipe_format dst_format
;
2507 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2511 st_flush_bitmap_cache(st
);
2512 st_invalidate_readpix_cache(st
);
2514 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2515 !_mesa_is_format_astc_2d(texImage
->TexFormat
) &&
2516 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2518 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2519 debug_printf("%s: null strb or stImage\n", __func__
);
2523 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2524 texImage
->TexFormat
)) {
2528 if (!st_can_copyteximage_using_blit(texImage
, rb
)) {
2532 /* Choose the destination format to match the TexImage behavior. */
2533 dst_format
= util_format_linear(stImage
->pt
->format
);
2534 dst_format
= util_format_luminance_to_red(dst_format
);
2535 dst_format
= util_format_intensity_to_red(dst_format
);
2537 /* See if the destination format is supported. */
2538 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2539 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2540 bind
= PIPE_BIND_DEPTH_STENCIL
;
2543 bind
= PIPE_BIND_RENDER_TARGET
;
2547 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2548 stImage
->pt
->nr_samples
,
2549 stImage
->pt
->nr_storage_samples
, bind
)) {
2553 /* Y flipping for the main framebuffer. */
2555 srcY1
= strb
->Base
.Height
- srcY
- height
;
2556 srcY0
= srcY1
+ height
;
2560 srcY1
= srcY0
+ height
;
2563 /* Blit the texture.
2564 * This supports flipping, format conversions, and downsampling.
2566 memset(&blit
, 0, sizeof(blit
));
2567 blit
.src
.resource
= strb
->texture
;
2568 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2569 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2570 blit
.src
.box
.x
= srcX
;
2571 blit
.src
.box
.y
= srcY0
;
2572 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2573 blit
.src
.box
.width
= width
;
2574 blit
.src
.box
.height
= srcY1
- srcY0
;
2575 blit
.src
.box
.depth
= 1;
2576 blit
.dst
.resource
= stImage
->pt
;
2577 blit
.dst
.format
= dst_format
;
2578 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
2579 ? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2580 blit
.dst
.box
.x
= destX
;
2581 blit
.dst
.box
.y
= destY
;
2582 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2583 blit
.dst
.box
.width
= width
;
2584 blit
.dst
.box
.height
= height
;
2585 blit
.dst
.box
.depth
= 1;
2586 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2587 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2588 pipe
->blit(pipe
, &blit
);
2592 /* software fallback */
2593 fallback_copy_texsubimage(ctx
,
2594 strb
, stImage
, texImage
->_BaseFormat
,
2595 destX
, destY
, slice
,
2596 srcX
, srcY
, width
, height
);
2601 * Copy image data from stImage into the texture object 'stObj' at level
2605 copy_image_data_to_texture(struct st_context
*st
,
2606 struct st_texture_object
*stObj
,
2608 struct st_texture_image
*stImage
)
2612 ASSERTED
const struct gl_texture_image
*dstImage
=
2613 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2615 assert(dstImage
->Width
== stImage
->base
.Width
);
2616 assert(dstImage
->Height
== stImage
->base
.Height
);
2617 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2621 /* Copy potentially with the blitter:
2624 if (stImage
->pt
->last_level
== 0)
2627 src_level
= stImage
->base
.Level
;
2629 assert(src_level
<= stImage
->pt
->last_level
);
2630 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2631 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2632 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2633 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2634 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2635 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2637 st_texture_image_copy(st
->pipe
,
2638 stObj
->pt
, dstLevel
, /* dest texture, level */
2639 stImage
->pt
, src_level
, /* src texture, level */
2640 stImage
->base
.Face
);
2642 pipe_resource_reference(&stImage
->pt
, NULL
);
2644 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2649 * Called during state validation. When this function is finished,
2650 * the texture object should be ready for rendering.
2651 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2654 st_finalize_texture(struct gl_context
*ctx
,
2655 struct pipe_context
*pipe
,
2656 struct gl_texture_object
*tObj
,
2659 struct st_context
*st
= st_context(ctx
);
2660 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2661 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2663 const struct st_texture_image
*firstImage
;
2664 enum pipe_format firstImageFormat
;
2666 uint16_t ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2668 if (tObj
->Immutable
)
2671 if (tObj
->_MipmapComplete
)
2672 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2673 else if (tObj
->_BaseComplete
)
2674 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2676 /* Skip the loop over images in the common case of no images having
2677 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
2678 * haven't looked at, then we do need to look at those new images.
2680 if (!stObj
->needs_validation
&&
2681 stObj
->base
.BaseLevel
>= stObj
->validated_first_level
&&
2682 stObj
->lastLevel
<= stObj
->validated_last_level
) {
2686 /* If this texture comes from a window system, there is nothing else to do. */
2687 if (stObj
->surface_based
) {
2691 firstImage
= st_texture_image_const(stObj
->base
.Image
[cubeMapFace
]
2692 [stObj
->base
.BaseLevel
]);
2695 /* If both firstImage and stObj point to a texture which can contain
2696 * all active images, favour firstImage. Note that because of the
2697 * completeness requirement, we know that the image dimensions
2700 if (firstImage
->pt
&&
2701 firstImage
->pt
!= stObj
->pt
&&
2702 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2703 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2704 st_texture_release_all_sampler_views(st
, stObj
);
2707 /* Find gallium format for the Mesa texture */
2709 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2711 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2714 uint16_t height
, depth
;
2716 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2717 firstImage
->base
.Width2
,
2718 firstImage
->base
.Height2
,
2719 firstImage
->base
.Depth2
,
2720 &width
, &height
, &depth
, &ptLayers
);
2722 /* If we previously allocated a pipe texture and its sizes are
2723 * compatible, use them.
2726 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2727 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2728 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2729 ptWidth
= stObj
->pt
->width0
;
2730 ptHeight
= stObj
->pt
->height0
;
2731 ptDepth
= stObj
->pt
->depth0
;
2733 /* Otherwise, compute a new level=0 size that is compatible with the
2736 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2737 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2738 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2740 /* If the base level image is 1x1x1, we still need to ensure that the
2741 * resulting pipe texture ends up with the required number of levels
2744 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2745 ptWidth
<<= firstImage
->base
.Level
;
2747 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2748 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2752 /* At this point, the texture may be incomplete (mismatched cube
2753 * face sizes, for example). If that's the case, give up, but
2754 * don't return GL_FALSE as that would raise an incorrect
2755 * GL_OUT_OF_MEMORY error. See Piglit fbo-incomplete-texture-03 test.
2757 if (!stObj
->base
._BaseComplete
) {
2758 _mesa_test_texobj_completeness(ctx
, &stObj
->base
);
2759 if (!stObj
->base
._BaseComplete
) {
2765 ptNumSamples
= firstImage
->base
.NumSamples
;
2768 /* If we already have a gallium texture, check that it matches the texture
2769 * object's format, target, size, num_levels, etc.
2772 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2773 stObj
->pt
->format
!= firstImageFormat
||
2774 stObj
->pt
->last_level
< stObj
->lastLevel
||
2775 stObj
->pt
->width0
!= ptWidth
||
2776 stObj
->pt
->height0
!= ptHeight
||
2777 stObj
->pt
->depth0
!= ptDepth
||
2778 stObj
->pt
->nr_samples
!= ptNumSamples
||
2779 stObj
->pt
->array_size
!= ptLayers
)
2781 /* The gallium texture does not match the Mesa texture so delete the
2782 * gallium texture now. We'll make a new one below.
2784 pipe_resource_reference(&stObj
->pt
, NULL
);
2785 st_texture_release_all_sampler_views(st
, stObj
);
2786 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
2790 /* May need to create a new gallium texture:
2793 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2795 stObj
->pt
= st_texture_create(st
,
2796 gl_target_to_pipe(stObj
->base
.Target
),
2802 ptLayers
, ptNumSamples
,
2806 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2811 /* Pull in any images not in the object's texture:
2813 for (face
= 0; face
< nr_faces
; face
++) {
2815 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2816 struct st_texture_image
*stImage
=
2817 st_texture_image(stObj
->base
.Image
[face
][level
]);
2819 /* Need to import images in main memory or held in other textures.
2821 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2825 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2826 height
= u_minify(ptHeight
, level
);
2830 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2831 depth
= u_minify(ptDepth
, level
);
2832 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2838 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2839 stImage
->base
.Height
== height
&&
2840 stImage
->base
.Depth
== depth
)) {
2841 /* src image fits expected dest mipmap level size */
2842 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2848 stObj
->validated_first_level
= stObj
->base
.BaseLevel
;
2849 stObj
->validated_last_level
= stObj
->lastLevel
;
2850 stObj
->needs_validation
= false;
2857 * Allocate a new pipe_resource object
2858 * width0, height0, depth0 are the dimensions of the level 0 image
2859 * (the highest resolution). last_level indicates how many mipmap levels
2860 * to allocate storage for. For non-mipmapped textures, this will be zero.
2862 static struct pipe_resource
*
2863 st_texture_create_from_memory(struct st_context
*st
,
2864 struct st_memory_object
*memObj
,
2866 enum pipe_texture_target target
,
2867 enum pipe_format format
,
2876 struct pipe_resource pt
, *newtex
;
2877 struct pipe_screen
*screen
= st
->pipe
->screen
;
2879 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
2881 assert(height0
> 0);
2883 if (target
== PIPE_TEXTURE_CUBE
)
2884 assert(layers
== 6);
2886 DBG("%s target %d format %s last_level %d\n", __func__
,
2887 (int) target
, util_format_name(format
), last_level
);
2890 assert(screen
->is_format_supported(screen
, format
, target
, 0, 0,
2891 PIPE_BIND_SAMPLER_VIEW
));
2893 memset(&pt
, 0, sizeof(pt
));
2896 pt
.last_level
= last_level
;
2898 pt
.height0
= height0
;
2900 pt
.array_size
= layers
;
2901 pt
.usage
= PIPE_USAGE_DEFAULT
;
2903 /* only set this for OpenGL textures, not renderbuffers */
2904 pt
.flags
= PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
;
2905 pt
.nr_samples
= nr_samples
;
2906 pt
.nr_storage_samples
= nr_samples
;
2908 newtex
= screen
->resource_from_memobj(screen
, &pt
, memObj
->memory
, offset
);
2910 assert(!newtex
|| pipe_is_referenced(&newtex
->reference
));
2917 * Allocate texture memory for a whole mipmap stack.
2918 * Note: for multisample textures if the requested sample count is not
2919 * supported, we search for the next higher supported sample count.
2922 st_texture_storage(struct gl_context
*ctx
,
2923 struct gl_texture_object
*texObj
,
2924 GLsizei levels
, GLsizei width
,
2925 GLsizei height
, GLsizei depth
,
2926 struct gl_memory_object
*memObj
,
2929 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2930 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2931 struct st_context
*st
= st_context(ctx
);
2932 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2933 struct st_memory_object
*smObj
= st_memory_object(memObj
);
2934 struct pipe_screen
*screen
= st
->pipe
->screen
;
2935 unsigned ptWidth
, bindings
;
2936 uint16_t ptHeight
, ptDepth
, ptLayers
;
2937 enum pipe_format fmt
;
2939 GLuint num_samples
= texImage
->NumSamples
;
2943 stObj
->lastLevel
= levels
- 1;
2945 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2947 bindings
= default_bindings(st
, fmt
);
2949 if (num_samples
> 0) {
2950 /* Find msaa sample count which is actually supported. For example,
2951 * if the user requests 1x but only 4x or 8x msaa is supported, we'll
2954 enum pipe_texture_target ptarget
= gl_target_to_pipe(texObj
->Target
);
2955 boolean found
= FALSE
;
2957 if (ctx
->Const
.MaxSamples
> 1 && num_samples
== 1) {
2958 /* don't try num_samples = 1 with drivers that support real msaa */
2962 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2963 if (screen
->is_format_supported(screen
, fmt
, ptarget
,
2964 num_samples
, num_samples
,
2965 PIPE_BIND_SAMPLER_VIEW
)) {
2966 /* Update the sample count in gl_texture_image as well. */
2967 texImage
->NumSamples
= num_samples
;
2978 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2979 width
, height
, depth
,
2980 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2983 stObj
->pt
= st_texture_create_from_memory(st
,
2986 gl_target_to_pipe(texObj
->Target
),
2992 ptLayers
, num_samples
,
2996 stObj
->pt
= st_texture_create(st
,
2997 gl_target_to_pipe(texObj
->Target
),
3003 ptLayers
, num_samples
,
3010 /* Set image resource pointers */
3011 for (level
= 0; level
< levels
; level
++) {
3013 for (face
= 0; face
< numFaces
; face
++) {
3014 struct st_texture_image
*stImage
=
3015 st_texture_image(texObj
->Image
[face
][level
]);
3016 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
3018 compressed_tex_fallback_allocate(st
, stImage
);
3022 /* The texture is in a validated state, so no need to check later. */
3023 stObj
->needs_validation
= false;
3024 stObj
->validated_first_level
= 0;
3025 stObj
->validated_last_level
= levels
- 1;
3031 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
3032 * for a whole mipmap stack.
3035 st_AllocTextureStorage(struct gl_context
*ctx
,
3036 struct gl_texture_object
*texObj
,
3037 GLsizei levels
, GLsizei width
,
3038 GLsizei height
, GLsizei depth
)
3040 return st_texture_storage(ctx
, texObj
, levels
,
3041 width
, height
, depth
,
3047 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
3048 GLuint numLevels
, GLint level
,
3049 mesa_format format
, GLuint numSamples
,
3050 GLint width
, GLint height
, GLint depth
)
3052 struct st_context
*st
= st_context(ctx
);
3053 struct pipe_context
*pipe
= st
->pipe
;
3055 if (width
== 0 || height
== 0 || depth
== 0) {
3056 /* zero-sized images are legal, and always fit! */
3060 if (pipe
->screen
->can_create_resource
) {
3061 /* Ask the gallium driver if the texture is too large */
3062 struct gl_texture_object
*texObj
=
3063 _mesa_get_current_tex_object(ctx
, target
);
3064 struct pipe_resource pt
;
3066 /* Setup the pipe_resource object
3068 memset(&pt
, 0, sizeof(pt
));
3070 pt
.target
= gl_target_to_pipe(target
);
3071 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
3072 pt
.nr_samples
= numSamples
;
3073 pt
.nr_storage_samples
= numSamples
;
3075 st_gl_texture_dims_to_pipe_dims(target
,
3076 width
, height
, depth
,
3077 &pt
.width0
, &pt
.height0
,
3078 &pt
.depth0
, &pt
.array_size
);
3080 if (numLevels
> 0) {
3081 /* For immutable textures we know the final number of mip levels */
3082 pt
.last_level
= numLevels
- 1;
3084 else if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
3085 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
3086 /* assume just one mipmap level */
3090 /* assume a full set of mipmaps */
3091 pt
.last_level
= util_logbase2(MAX3(width
, height
, depth
));
3094 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
3097 /* Use core Mesa fallback */
3098 return _mesa_test_proxy_teximage(ctx
, target
, numLevels
, level
, format
,
3099 numSamples
, width
, height
, depth
);
3104 st_TextureView(struct gl_context
*ctx
,
3105 struct gl_texture_object
*texObj
,
3106 struct gl_texture_object
*origTexObj
)
3108 struct st_context
*st
= st_context(ctx
);
3109 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
3110 struct st_texture_object
*tex
= st_texture_object(texObj
);
3111 struct gl_texture_image
*image
= texObj
->Image
[0][0];
3113 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
3114 const int numLevels
= texObj
->NumLevels
;
3119 pipe_resource_reference(&tex
->pt
, orig
->pt
);
3121 /* Set image resource pointers */
3122 for (level
= 0; level
< numLevels
; level
++) {
3123 for (face
= 0; face
< numFaces
; face
++) {
3124 struct st_texture_image
*stImage
=
3125 st_texture_image(texObj
->Image
[face
][level
]);
3126 struct st_texture_image
*origImage
=
3127 st_texture_image(origTexObj
->Image
[face
][level
]);
3128 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
3130 origImage
->compressed_data
) {
3131 pipe_reference(NULL
,
3132 &origImage
->compressed_data
->reference
);
3133 stImage
->compressed_data
= origImage
->compressed_data
;
3138 tex
->surface_based
= GL_TRUE
;
3139 tex
->surface_format
=
3140 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
3142 tex
->lastLevel
= numLevels
- 1;
3144 /* free texture sampler views. They need to be recreated when we
3145 * change the texture view parameters.
3147 st_texture_release_all_sampler_views(st
, tex
);
3149 /* The texture is in a validated state, so no need to check later. */
3150 tex
->needs_validation
= false;
3151 tex
->validated_first_level
= 0;
3152 tex
->validated_last_level
= numLevels
- 1;
3159 * Find the mipmap level in 'pt' which matches the level described by
3163 find_mipmap_level(const struct gl_texture_image
*texImage
,
3164 const struct pipe_resource
*pt
)
3166 const GLenum target
= texImage
->TexObject
->Target
;
3167 GLint texWidth
= texImage
->Width
;
3168 GLint texHeight
= texImage
->Height
;
3169 GLint texDepth
= texImage
->Depth
;
3171 uint16_t h
, d
, layers
;
3173 st_gl_texture_dims_to_pipe_dims(target
, texWidth
, texHeight
, texDepth
,
3174 &w
, &h
, &d
, &layers
);
3176 for (level
= 0; level
<= pt
->last_level
; level
++) {
3177 if (u_minify(pt
->width0
, level
) == w
&&
3178 u_minify(pt
->height0
, level
) == h
&&
3179 u_minify(pt
->depth0
, level
) == d
) {
3184 /* If we get here, there must be some sort of inconsistency between
3185 * the Mesa texture object/images and the gallium resource.
3187 debug_printf("Inconsistent textures in find_mipmap_level()\n");
3189 return texImage
->Level
;
3194 st_ClearTexSubImage(struct gl_context
*ctx
,
3195 struct gl_texture_image
*texImage
,
3196 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3197 GLsizei width
, GLsizei height
, GLsizei depth
,
3198 const void *clearValue
)
3200 static const char zeros
[16] = {0};
3201 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3202 struct st_texture_image
*stImage
= st_texture_image(texImage
);
3203 struct pipe_resource
*pt
= stImage
->pt
;
3204 struct st_context
*st
= st_context(ctx
);
3205 struct pipe_context
*pipe
= st
->pipe
;
3207 struct pipe_box box
;
3212 st_flush_bitmap_cache(st
);
3213 st_invalidate_readpix_cache(st
);
3215 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
3216 width
, height
, depth
, &box
);
3217 if (texObj
->Immutable
) {
3218 /* The texture object has to be consistent (no "loose", per-image
3219 * gallium resources). If this texture is a view into another
3220 * texture, we have to apply the MinLevel/Layer offsets. If this is
3221 * not a texture view, the offsets will be zero.
3223 assert(stImage
->pt
== st_texture_object(texObj
)->pt
);
3224 level
= texImage
->Level
+ texObj
->MinLevel
;
3225 box
.z
+= texObj
->MinLayer
;
3228 /* Texture level sizes may be inconsistent. We my have "loose",
3229 * per-image gallium resources. The texImage->Level may not match
3230 * the gallium resource texture level.
3232 level
= find_mipmap_level(texImage
, pt
);
3235 assert(level
<= pt
->last_level
);
3237 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
3242 * Called via the glTexParam*() function, but only when some texture object
3243 * state has actually changed.
3246 st_TexParameter(struct gl_context
*ctx
,
3247 struct gl_texture_object
*texObj
, GLenum pname
)
3249 struct st_context
*st
= st_context(ctx
);
3250 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3253 case GL_TEXTURE_BASE_LEVEL
:
3254 case GL_TEXTURE_MAX_LEVEL
:
3255 case GL_DEPTH_TEXTURE_MODE
:
3256 case GL_DEPTH_STENCIL_TEXTURE_MODE
:
3257 case GL_TEXTURE_SRGB_DECODE_EXT
:
3258 case GL_TEXTURE_SWIZZLE_R
:
3259 case GL_TEXTURE_SWIZZLE_G
:
3260 case GL_TEXTURE_SWIZZLE_B
:
3261 case GL_TEXTURE_SWIZZLE_A
:
3262 case GL_TEXTURE_SWIZZLE_RGBA
:
3263 case GL_TEXTURE_BUFFER_SIZE
:
3264 case GL_TEXTURE_BUFFER_OFFSET
:
3265 /* changing any of these texture parameters means we must create
3266 * new sampler views.
3268 st_texture_release_all_sampler_views(st
, stObj
);
3276 st_SetTextureStorageForMemoryObject(struct gl_context
*ctx
,
3277 struct gl_texture_object
*texObj
,
3278 struct gl_memory_object
*memObj
,
3279 GLsizei levels
, GLsizei width
,
3280 GLsizei height
, GLsizei depth
,
3283 return st_texture_storage(ctx
, texObj
, levels
,
3284 width
, height
, depth
,
3289 st_NewTextureHandle(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
3290 struct gl_sampler_object
*sampObj
)
3292 struct st_context
*st
= st_context(ctx
);
3293 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3294 struct pipe_context
*pipe
= st
->pipe
;
3295 struct pipe_sampler_view
*view
;
3296 struct pipe_sampler_state sampler
= {0};
3298 if (texObj
->Target
!= GL_TEXTURE_BUFFER
) {
3299 if (!st_finalize_texture(ctx
, pipe
, texObj
, 0))
3302 st_convert_sampler(st
, texObj
, sampObj
, 0, &sampler
);
3304 /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
3305 view
= st_get_texture_sampler_view_from_stobj(st
, stObj
, sampObj
, 0, true);
3307 view
= st_get_buffer_sampler_view_from_stobj(st
, stObj
);
3310 return pipe
->create_texture_handle(pipe
, view
, &sampler
);
3315 st_DeleteTextureHandle(struct gl_context
*ctx
, GLuint64 handle
)
3317 struct st_context
*st
= st_context(ctx
);
3318 struct pipe_context
*pipe
= st
->pipe
;
3320 pipe
->delete_texture_handle(pipe
, handle
);
3325 st_MakeTextureHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3328 struct st_context
*st
= st_context(ctx
);
3329 struct pipe_context
*pipe
= st
->pipe
;
3331 pipe
->make_texture_handle_resident(pipe
, handle
, resident
);
3336 st_NewImageHandle(struct gl_context
*ctx
, struct gl_image_unit
*imgObj
)
3338 struct st_context
*st
= st_context(ctx
);
3339 struct pipe_context
*pipe
= st
->pipe
;
3340 struct pipe_image_view image
;
3342 st_convert_image(st
, imgObj
, &image
, GL_READ_WRITE
);
3344 return pipe
->create_image_handle(pipe
, &image
);
3349 st_DeleteImageHandle(struct gl_context
*ctx
, GLuint64 handle
)
3351 struct st_context
*st
= st_context(ctx
);
3352 struct pipe_context
*pipe
= st
->pipe
;
3354 pipe
->delete_image_handle(pipe
, handle
);
3359 st_MakeImageHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3360 GLenum access
, bool resident
)
3362 struct st_context
*st
= st_context(ctx
);
3363 struct pipe_context
*pipe
= st
->pipe
;
3365 pipe
->make_image_handle_resident(pipe
, handle
, access
, resident
);
3370 st_init_texture_functions(struct dd_function_table
*functions
)
3372 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
3373 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
3374 functions
->TexImage
= st_TexImage
;
3375 functions
->TexSubImage
= st_TexSubImage
;
3376 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
3377 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
3378 functions
->GenerateMipmap
= st_generate_mipmap
;
3380 functions
->GetTexSubImage
= st_GetTexSubImage
;
3382 /* compressed texture functions */
3383 functions
->CompressedTexImage
= st_CompressedTexImage
;
3385 functions
->NewTextureObject
= st_NewTextureObject
;
3386 functions
->NewTextureImage
= st_NewTextureImage
;
3387 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
3388 functions
->DeleteTexture
= st_DeleteTextureObject
;
3389 functions
->TextureRemovedFromShared
= st_TextureReleaseAllSamplerViews
;
3390 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
3391 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
3392 functions
->MapTextureImage
= st_MapTextureImage
;
3393 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
3395 /* XXX Temporary until we can query pipe's texture sizes */
3396 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
3398 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
3399 functions
->TextureView
= st_TextureView
;
3400 functions
->ClearTexSubImage
= st_ClearTexSubImage
;
3402 functions
->TexParameter
= st_TexParameter
;
3404 /* bindless functions */
3405 functions
->NewTextureHandle
= st_NewTextureHandle
;
3406 functions
->DeleteTextureHandle
= st_DeleteTextureHandle
;
3407 functions
->MakeTextureHandleResident
= st_MakeTextureHandleResident
;
3408 functions
->NewImageHandle
= st_NewImageHandle
;
3409 functions
->DeleteImageHandle
= st_DeleteImageHandle
;
3410 functions
->MakeImageHandleResident
= st_MakeImageHandleResident
;
3412 /* external object functions */
3413 functions
->SetTextureStorageForMemoryObject
= st_SetTextureStorageForMemoryObject
;