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"
37 #include "util/imports.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.FreeTextureImageBuffer() */
201 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
202 struct gl_texture_image
*texImage
)
204 struct st_context
*st
= st_context(ctx
);
205 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
206 struct st_texture_image
*stImage
= st_texture_image(texImage
);
208 DBG("%s\n", __func__
);
211 pipe_resource_reference(&stImage
->pt
, NULL
);
214 free(stImage
->transfer
);
215 stImage
->transfer
= NULL
;
216 stImage
->num_transfers
= 0;
218 if (stImage
->compressed_data
) {
219 free(stImage
->compressed_data
);
220 stImage
->compressed_data
= NULL
;
223 /* if the texture image is being deallocated, the structure of the
224 * texture is changing so we'll likely need a new sampler view.
226 st_texture_release_all_sampler_views(st
, stObj
);
230 st_astc_format_fallback(const struct st_context
*st
, mesa_format format
)
232 if (!_mesa_is_format_astc_2d(format
))
235 if (format
== MESA_FORMAT_RGBA_ASTC_5x5
||
236 format
== MESA_FORMAT_SRGB8_ALPHA8_ASTC_5x5
)
237 return !st
->has_astc_5x5_ldr
;
239 return !st
->has_astc_2d_ldr
;
243 st_compressed_format_fallback(struct st_context
*st
, mesa_format format
)
245 if (format
== MESA_FORMAT_ETC1_RGB8
)
246 return !st
->has_etc1
;
248 if (_mesa_is_format_etc2(format
))
249 return !st
->has_etc2
;
251 if (st_astc_format_fallback(st
, format
))
259 compressed_tex_fallback_allocate(struct st_context
*st
,
260 struct st_texture_image
*stImage
)
262 struct gl_texture_image
*texImage
= &stImage
->base
;
264 if (!st_compressed_format_fallback(st
, texImage
->TexFormat
))
267 if (stImage
->compressed_data
)
268 free(stImage
->compressed_data
);
270 unsigned data_size
= _mesa_format_image_size(texImage
->TexFormat
,
275 stImage
->compressed_data
=
276 malloc(data_size
* _mesa_num_tex_faces(texImage
->TexObject
->Target
));
280 /** called via ctx->Driver.MapTextureImage() */
282 st_MapTextureImage(struct gl_context
*ctx
,
283 struct gl_texture_image
*texImage
,
284 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
286 GLubyte
**mapOut
, GLint
*rowStrideOut
)
288 struct st_context
*st
= st_context(ctx
);
289 struct st_texture_image
*stImage
= st_texture_image(texImage
);
291 struct pipe_transfer
*transfer
;
293 /* Check for unexpected flags */
294 assert((mode
& ~(GL_MAP_READ_BIT
|
296 GL_MAP_INVALIDATE_RANGE_BIT
)) == 0);
298 const enum pipe_transfer_usage transfer_flags
=
299 st_access_flags_to_transfer_flags(mode
, false);
301 map
= st_texture_image_map(st
, stImage
, transfer_flags
, x
, y
, slice
, w
, h
, 1,
304 if (st_compressed_format_fallback(st
, texImage
->TexFormat
)) {
305 /* Some compressed formats don't have to be supported by drivers,
306 * and st/mesa transparently handles decompression on upload (Unmap),
307 * so that drivers don't see the compressed formats.
309 * We store the compressed data (it's needed for glGetCompressedTex-
310 * Image and image copies in OES_copy_image).
312 unsigned z
= transfer
->box
.z
;
313 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
315 unsigned blk_w
, blk_h
;
316 _mesa_get_format_block_size(texImage
->TexFormat
, &blk_w
, &blk_h
);
318 unsigned y_blocks
= DIV_ROUND_UP(texImage
->Height2
, blk_h
);
319 unsigned stride
= *rowStrideOut
= itransfer
->temp_stride
=
320 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width2
);
321 unsigned block_size
= _mesa_get_format_bytes(texImage
->TexFormat
);
323 *mapOut
= itransfer
->temp_data
=
324 stImage
->compressed_data
+
325 (z
* y_blocks
+ (y
/ blk_h
)) * stride
+
326 (x
/ blk_w
) * block_size
;
327 itransfer
->map
= map
;
330 /* supported mapping */
332 *rowStrideOut
= transfer
->stride
;
342 /** called via ctx->Driver.UnmapTextureImage() */
344 st_UnmapTextureImage(struct gl_context
*ctx
,
345 struct gl_texture_image
*texImage
,
348 struct st_context
*st
= st_context(ctx
);
349 struct st_texture_image
*stImage
= st_texture_image(texImage
);
351 if (st_compressed_format_fallback(st
, texImage
->TexFormat
)) {
352 /* Decompress the compressed image on upload if the driver doesn't
353 * support the compressed format. */
354 unsigned z
= slice
+ stImage
->base
.Face
;
355 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
356 struct pipe_transfer
*transfer
= itransfer
->transfer
;
358 assert(z
== transfer
->box
.z
);
360 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
361 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
362 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
363 itransfer
->temp_data
,
364 itransfer
->temp_stride
,
366 transfer
->box
.height
);
367 } else if (_mesa_is_format_etc2(texImage
->TexFormat
)) {
368 bool bgra
= stImage
->pt
->format
== PIPE_FORMAT_B8G8R8A8_SRGB
;
369 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
370 itransfer
->temp_data
,
371 itransfer
->temp_stride
,
372 transfer
->box
.width
, transfer
->box
.height
,
375 } else if (_mesa_is_format_astc_2d(texImage
->TexFormat
)) {
376 _mesa_unpack_astc_2d_ldr(itransfer
->map
, transfer
->stride
,
377 itransfer
->temp_data
,
378 itransfer
->temp_stride
,
379 transfer
->box
.width
, transfer
->box
.height
,
380 texImage
->TexFormat
);
382 unreachable("unexpected format for a compressed format fallback");
386 itransfer
->temp_data
= NULL
;
387 itransfer
->temp_stride
= 0;
391 st_texture_image_unmap(st
, stImage
, slice
);
396 * Return default texture resource binding bitmask for the given format.
399 default_bindings(struct st_context
*st
, enum pipe_format format
)
401 struct pipe_screen
*screen
= st
->pipe
->screen
;
402 const unsigned target
= PIPE_TEXTURE_2D
;
405 if (util_format_is_depth_or_stencil(format
))
406 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
408 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
410 if (screen
->is_format_supported(screen
, format
, target
, 0, 0, bindings
))
414 format
= util_format_linear(format
);
416 if (screen
->is_format_supported(screen
, format
, target
, 0, 0, bindings
))
419 return PIPE_BIND_SAMPLER_VIEW
;
425 * Given the size of a mipmap image, try to compute the size of the level=0
428 * Note that this isn't always accurate for odd-sized, non-POW textures.
429 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
431 * \return GL_TRUE for success, GL_FALSE for failure
434 guess_base_level_size(GLenum target
,
435 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
436 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
443 /* Guess the size of the base level.
444 * Depending on the image's size, we can't always make a guess here.
448 case GL_TEXTURE_1D_ARRAY
:
453 case GL_TEXTURE_2D_ARRAY
:
454 /* We can't make a good guess here, because the base level dimensions
457 if (width
== 1 || height
== 1) {
464 case GL_TEXTURE_CUBE_MAP
:
465 case GL_TEXTURE_CUBE_MAP_ARRAY
:
471 /* We can't make a good guess here, because the base level dimensions
474 if (width
== 1 || height
== 1 || depth
== 1) {
482 case GL_TEXTURE_RECTANGLE
:
499 * Try to determine whether we should allocate memory for a full texture
500 * mipmap. The problem is when we get a glTexImage(level=0) call, we
501 * can't immediately know if other mipmap levels are coming next. Here
502 * we try to guess whether to allocate memory for a mipmap or just the
505 * If we guess incorrectly here we'll later reallocate the right amount of
506 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
508 * \param stObj the texture object we're going to allocate memory for.
509 * \param stImage describes the incoming image which we need to store.
512 allocate_full_mipmap(const struct st_texture_object
*stObj
,
513 const struct st_texture_image
*stImage
)
515 switch (stObj
->base
.Target
) {
516 case GL_TEXTURE_RECTANGLE_NV
:
517 case GL_TEXTURE_BUFFER
:
518 case GL_TEXTURE_EXTERNAL_OES
:
519 case GL_TEXTURE_2D_MULTISAMPLE
:
520 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
521 /* these texture types cannot be mipmapped */
525 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
528 /* If the application has explicitly called glTextureParameter to set
529 * GL_TEXTURE_MAX_LEVEL, such that (max - base) > 0, then they're trying
530 * to communicate that they will have multiple miplevels.
532 * Core Mesa will initialize MaxLevel to value much larger than
533 * MAX_TEXTURE_LEVELS, so we check that to see if it's been set at all.
535 if (stObj
->base
.MaxLevel
< MAX_TEXTURE_LEVELS
&&
536 stObj
->base
.MaxLevel
- stObj
->base
.BaseLevel
> 0)
539 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
540 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
541 /* depth/stencil textures are seldom mipmapped */
544 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
547 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
548 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
549 /* not a mipmap minification filter */
552 /* If the following sequence of GL calls is used:
553 * glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, ...
554 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
556 * we would needlessly allocate a mipmapped texture, because the initial
557 * MinFilter is GL_NEAREST_MIPMAP_LINEAR. Catch this case and don't
558 * allocate a mipmapped texture by default. This may cause texture
559 * reallocation later, but GL_NEAREST_MIPMAP_LINEAR is pretty rare.
561 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST_MIPMAP_LINEAR
)
564 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
565 /* 3D textures are seldom mipmapped */
573 * Try to allocate a pipe_resource object for the given st_texture_object.
575 * We use the given st_texture_image as a clue to determine the size of the
576 * mipmap image at level=0.
578 * \return GL_TRUE for success, GL_FALSE if out of memory.
581 guess_and_alloc_texture(struct st_context
*st
,
582 struct st_texture_object
*stObj
,
583 const struct st_texture_image
*stImage
)
585 const struct gl_texture_image
*firstImage
;
586 GLuint lastLevel
, width
, height
, depth
;
589 uint16_t ptHeight
, ptDepth
, ptLayers
;
590 enum pipe_format fmt
;
591 bool guessed_box
= false;
593 DBG("%s\n", __func__
);
597 /* If a base level image with compatible size exists, use that as our guess.
599 firstImage
= _mesa_base_tex_image(&stObj
->base
);
601 firstImage
->Width2
> 0 &&
602 firstImage
->Height2
> 0 &&
603 firstImage
->Depth2
> 0 &&
604 guess_base_level_size(stObj
->base
.Target
,
609 &width
, &height
, &depth
)) {
610 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
611 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
612 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
617 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
618 stImage
->base
.Width2
,
619 stImage
->base
.Height2
,
620 stImage
->base
.Depth2
,
622 &width
, &height
, &depth
);
625 /* we can't determine the image size at level=0 */
626 /* this is not an out of memory error */
630 /* At this point, (width x height x depth) is the expected size of
631 * the level=0 mipmap image.
634 /* Guess a reasonable value for lastLevel. With OpenGL we have no
635 * idea how many mipmap levels will be in a texture until we start
636 * to render with it. Make an educated guess here but be prepared
637 * to re-allocating a texture buffer with space for more (or fewer)
638 * mipmap levels later.
640 if (allocate_full_mipmap(stObj
, stImage
)) {
641 /* alloc space for a full mipmap */
642 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
643 width
, height
, depth
) - 1;
646 /* only alloc space for a single mipmap level */
650 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
652 bindings
= default_bindings(st
, fmt
);
654 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
655 width
, height
, depth
,
656 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
658 stObj
->pt
= st_texture_create(st
,
659 gl_target_to_pipe(stObj
->base
.Target
),
668 stObj
->lastLevel
= lastLevel
;
670 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
672 return stObj
->pt
!= NULL
;
677 * Called via ctx->Driver.AllocTextureImageBuffer().
678 * If the texture object/buffer already has space for the indicated image,
679 * we're done. Otherwise, allocate memory for the new texture image.
682 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
683 struct gl_texture_image
*texImage
)
685 struct st_context
*st
= st_context(ctx
);
686 struct st_texture_image
*stImage
= st_texture_image(texImage
);
687 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
688 const GLuint level
= texImage
->Level
;
689 GLuint width
= texImage
->Width
;
690 GLuint height
= texImage
->Height
;
691 GLuint depth
= texImage
->Depth
;
693 DBG("%s\n", __func__
);
695 assert(!stImage
->pt
); /* xxx this might be wrong */
697 stObj
->needs_validation
= true;
699 compressed_tex_fallback_allocate(st
, stImage
);
701 /* Look if the parent texture object has space for this image */
703 level
<= stObj
->pt
->last_level
&&
704 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
705 /* this image will fit in the existing texture object's memory */
706 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
710 /* The parent texture object does not have space for this image */
712 pipe_resource_reference(&stObj
->pt
, NULL
);
713 st_texture_release_all_sampler_views(st
, stObj
);
715 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
716 /* Probably out of memory.
717 * Try flushing any pending rendering, then retry.
720 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
721 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
727 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
728 /* The image will live in the object's mipmap memory */
729 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
734 /* Create a new, temporary texture/resource/buffer to hold this
735 * one texture image. Note that when we later access this image
736 * (either for mapping or copying) we'll want to always specify
737 * mipmap level=0, even if the image represents some other mipmap
740 enum pipe_format format
=
741 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
742 GLuint bindings
= default_bindings(st
, format
);
744 uint16_t ptHeight
, ptDepth
, ptLayers
;
746 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
747 width
, height
, depth
,
748 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
750 stImage
->pt
= st_texture_create(st
,
751 gl_target_to_pipe(stObj
->base
.Target
),
759 return stImage
->pt
!= NULL
;
765 * Preparation prior to glTexImage. Basically check the 'surface_based'
766 * field and switch to a "normal" tex image if necessary.
769 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
770 GLenum format
, GLenum type
)
772 struct gl_texture_object
*texObj
= texImage
->TexObject
;
773 struct st_texture_object
*stObj
= st_texture_object(texObj
);
775 /* switch to "normal" */
776 if (stObj
->surface_based
) {
777 const GLenum target
= texObj
->Target
;
778 const GLuint level
= texImage
->Level
;
779 mesa_format texFormat
;
781 assert(!st_texture_image(texImage
)->pt
);
782 _mesa_clear_texture_object(ctx
, texObj
, texImage
);
783 stObj
->layer_override
= -1;
784 stObj
->level_override
= -1;
785 pipe_resource_reference(&stObj
->pt
, NULL
);
787 /* oops, need to init this image again */
788 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
789 texImage
->InternalFormat
, format
,
792 _mesa_init_teximage_fields(ctx
, texImage
,
793 texImage
->Width
, texImage
->Height
,
794 texImage
->Depth
, texImage
->Border
,
795 texImage
->InternalFormat
, texFormat
);
797 stObj
->surface_based
= GL_FALSE
;
803 * Return a writemask for the gallium blit. The parameters can be base
804 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
807 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
810 case GL_DEPTH_STENCIL
:
812 case GL_DEPTH_STENCIL
:
814 case GL_DEPTH_COMPONENT
:
816 case GL_STENCIL_INDEX
:
823 case GL_DEPTH_COMPONENT
:
825 case GL_DEPTH_STENCIL
:
826 case GL_DEPTH_COMPONENT
:
833 case GL_STENCIL_INDEX
:
835 case GL_DEPTH_STENCIL
:
836 case GL_STENCIL_INDEX
:
844 return PIPE_MASK_RGBA
;
849 * Converts format to a format with the same components, types
850 * and sizes, but with the components in RGBA order.
852 static enum pipe_format
853 unswizzle_format(enum pipe_format format
)
857 case PIPE_FORMAT_B8G8R8A8_UNORM
:
858 case PIPE_FORMAT_A8R8G8B8_UNORM
:
859 case PIPE_FORMAT_A8B8G8R8_UNORM
:
860 return PIPE_FORMAT_R8G8B8A8_UNORM
;
862 case PIPE_FORMAT_B10G10R10A2_UNORM
:
863 return PIPE_FORMAT_R10G10B10A2_UNORM
;
865 case PIPE_FORMAT_B10G10R10A2_SNORM
:
866 return PIPE_FORMAT_R10G10B10A2_SNORM
;
868 case PIPE_FORMAT_B10G10R10A2_UINT
:
869 return PIPE_FORMAT_R10G10B10A2_UINT
;
878 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
880 static enum pipe_format
881 alpha_to_red(enum pipe_format format
)
885 case PIPE_FORMAT_A8_UNORM
:
886 return PIPE_FORMAT_R8_UNORM
;
887 case PIPE_FORMAT_A8_SNORM
:
888 return PIPE_FORMAT_R8_SNORM
;
889 case PIPE_FORMAT_A8_UINT
:
890 return PIPE_FORMAT_R8_UINT
;
891 case PIPE_FORMAT_A8_SINT
:
892 return PIPE_FORMAT_R8_SINT
;
894 case PIPE_FORMAT_A16_UNORM
:
895 return PIPE_FORMAT_R16_UNORM
;
896 case PIPE_FORMAT_A16_SNORM
:
897 return PIPE_FORMAT_R16_SNORM
;
898 case PIPE_FORMAT_A16_UINT
:
899 return PIPE_FORMAT_R16_UINT
;
900 case PIPE_FORMAT_A16_SINT
:
901 return PIPE_FORMAT_R16_SINT
;
902 case PIPE_FORMAT_A16_FLOAT
:
903 return PIPE_FORMAT_R16_FLOAT
;
905 case PIPE_FORMAT_A32_UINT
:
906 return PIPE_FORMAT_R32_UINT
;
907 case PIPE_FORMAT_A32_SINT
:
908 return PIPE_FORMAT_R32_SINT
;
909 case PIPE_FORMAT_A32_FLOAT
:
910 return PIPE_FORMAT_R32_FLOAT
;
919 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
921 static enum pipe_format
922 red_alpha_to_red_green(enum pipe_format format
)
926 case PIPE_FORMAT_R8A8_UNORM
:
927 return PIPE_FORMAT_R8G8_UNORM
;
928 case PIPE_FORMAT_R8A8_SNORM
:
929 return PIPE_FORMAT_R8G8_SNORM
;
930 case PIPE_FORMAT_R8A8_UINT
:
931 return PIPE_FORMAT_R8G8_UINT
;
932 case PIPE_FORMAT_R8A8_SINT
:
933 return PIPE_FORMAT_R8G8_SINT
;
935 case PIPE_FORMAT_R16A16_UNORM
:
936 return PIPE_FORMAT_R16G16_UNORM
;
937 case PIPE_FORMAT_R16A16_SNORM
:
938 return PIPE_FORMAT_R16G16_SNORM
;
939 case PIPE_FORMAT_R16A16_UINT
:
940 return PIPE_FORMAT_R16G16_UINT
;
941 case PIPE_FORMAT_R16A16_SINT
:
942 return PIPE_FORMAT_R16G16_SINT
;
943 case PIPE_FORMAT_R16A16_FLOAT
:
944 return PIPE_FORMAT_R16G16_FLOAT
;
946 case PIPE_FORMAT_R32A32_UINT
:
947 return PIPE_FORMAT_R32G32_UINT
;
948 case PIPE_FORMAT_R32A32_SINT
:
949 return PIPE_FORMAT_R32G32_SINT
;
950 case PIPE_FORMAT_R32A32_FLOAT
:
951 return PIPE_FORMAT_R32G32_FLOAT
;
960 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
962 static enum pipe_format
963 luminance_alpha_to_red_green(enum pipe_format format
)
967 case PIPE_FORMAT_L8A8_UNORM
:
968 return PIPE_FORMAT_R8G8_UNORM
;
969 case PIPE_FORMAT_L8A8_SNORM
:
970 return PIPE_FORMAT_R8G8_SNORM
;
971 case PIPE_FORMAT_L8A8_UINT
:
972 return PIPE_FORMAT_R8G8_UINT
;
973 case PIPE_FORMAT_L8A8_SINT
:
974 return PIPE_FORMAT_R8G8_SINT
;
976 case PIPE_FORMAT_L16A16_UNORM
:
977 return PIPE_FORMAT_R16G16_UNORM
;
978 case PIPE_FORMAT_L16A16_SNORM
:
979 return PIPE_FORMAT_R16G16_SNORM
;
980 case PIPE_FORMAT_L16A16_UINT
:
981 return PIPE_FORMAT_R16G16_UINT
;
982 case PIPE_FORMAT_L16A16_SINT
:
983 return PIPE_FORMAT_R16G16_SINT
;
984 case PIPE_FORMAT_L16A16_FLOAT
:
985 return PIPE_FORMAT_R16G16_FLOAT
;
987 case PIPE_FORMAT_L32A32_UINT
:
988 return PIPE_FORMAT_R32G32_UINT
;
989 case PIPE_FORMAT_L32A32_SINT
:
990 return PIPE_FORMAT_R32G32_SINT
;
991 case PIPE_FORMAT_L32A32_FLOAT
:
992 return PIPE_FORMAT_R32G32_FLOAT
;
1001 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
1004 format_is_alpha(enum pipe_format format
)
1006 const struct util_format_description
*desc
= util_format_description(format
);
1008 if (desc
->nr_channels
== 1 &&
1009 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
1010 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
1011 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
1012 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
1020 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
1023 format_is_red(enum pipe_format format
)
1025 const struct util_format_description
*desc
= util_format_description(format
);
1027 if (desc
->nr_channels
== 1 &&
1028 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
1029 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
1030 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
1031 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1039 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
1042 format_is_luminance(enum pipe_format format
)
1044 const struct util_format_description
*desc
= util_format_description(format
);
1046 if (desc
->nr_channels
== 1 &&
1047 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
1048 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
1049 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
1050 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1057 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
1060 format_is_red_alpha(enum pipe_format format
)
1062 const struct util_format_description
*desc
= util_format_description(format
);
1064 if (desc
->nr_channels
== 2 &&
1065 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
1066 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
1067 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
1068 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
1076 format_is_swizzled_rgba(enum pipe_format format
)
1078 const struct util_format_description
*desc
= util_format_description(format
);
1080 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
1081 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
1082 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
1083 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
1092 unsigned char swizzle
[4];
1093 enum pipe_format format
;
1096 static const struct format_table table_8888_unorm
[] = {
1097 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
1098 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
1099 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
1100 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
1103 static const struct format_table table_1010102_unorm
[] = {
1104 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
1105 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
1108 static const struct format_table table_1010102_snorm
[] = {
1109 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
1110 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
1113 static const struct format_table table_1010102_uint
[] = {
1114 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
1115 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
1118 static enum pipe_format
1119 swizzle_format(enum pipe_format format
, const int * const swizzle
)
1124 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1125 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1126 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1127 case PIPE_FORMAT_A8B8G8R8_UNORM
:
1128 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
1129 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
1130 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
1131 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
1132 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
1133 return table_8888_unorm
[i
].format
;
1137 case PIPE_FORMAT_R10G10B10A2_UNORM
:
1138 case PIPE_FORMAT_B10G10R10A2_UNORM
:
1139 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
1140 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
1141 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
1142 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
1143 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
1144 return table_1010102_unorm
[i
].format
;
1148 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1149 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1150 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1151 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1152 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1153 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1154 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1155 return table_1010102_snorm
[i
].format
;
1159 case PIPE_FORMAT_R10G10B10A2_UINT
:
1160 case PIPE_FORMAT_B10G10R10A2_UINT
:
1161 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1162 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1163 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1164 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1165 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1166 return table_1010102_uint
[i
].format
;
1174 return PIPE_FORMAT_NONE
;
1178 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1180 enum pipe_format src
= *src_format
;
1181 enum pipe_format dst
= *dst_format
;
1183 /* Note: dst_format has already been transformed from luminance/intensity
1184 * to red when this function is called. The source format will never
1185 * be an intensity format, because GL_INTENSITY is not a legal value
1186 * for the format parameter in glTex(Sub)Image(). */
1188 if (format_is_alpha(src
)) {
1189 if (!format_is_alpha(dst
))
1192 src
= alpha_to_red(src
);
1193 dst
= alpha_to_red(dst
);
1194 } else if (format_is_luminance(src
)) {
1195 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1198 src
= util_format_luminance_to_red(src
);
1199 } else if (util_format_is_luminance_alpha(src
)) {
1200 src
= luminance_alpha_to_red_green(src
);
1202 if (format_is_red_alpha(dst
)) {
1203 dst
= red_alpha_to_red_green(dst
);
1204 } else if (!format_is_red(dst
))
1206 } else if (format_is_swizzled_rgba(src
)) {
1207 const struct util_format_description
*src_desc
= util_format_description(src
);
1208 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1212 /* Make sure the format is an RGBA and not an RGBX format */
1213 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1216 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1219 for (i
= 0; i
< 4; i
++)
1220 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1222 dst
= swizzle_format(dst
, swizzle
);
1223 if (dst
== PIPE_FORMAT_NONE
)
1226 src
= unswizzle_format(src
);
1235 try_pbo_upload_common(struct gl_context
*ctx
,
1236 struct pipe_surface
*surface
,
1237 const struct st_pbo_addresses
*addr
,
1238 enum pipe_format src_format
)
1240 struct st_context
*st
= st_context(ctx
);
1241 struct cso_context
*cso
= st
->cso_context
;
1242 struct pipe_context
*pipe
= st
->pipe
;
1243 bool success
= false;
1246 fs
= st_pbo_get_upload_fs(st
, src_format
, surface
->format
);
1250 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1251 CSO_BIT_VERTEX_ELEMENTS
|
1252 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1253 CSO_BIT_FRAMEBUFFER
|
1256 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1257 CSO_BIT_RASTERIZER
|
1258 CSO_BIT_STREAM_OUTPUTS
|
1259 (st
->active_queries
? CSO_BIT_PAUSE_QUERIES
: 0) |
1260 CSO_BIT_SAMPLE_MASK
|
1261 CSO_BIT_MIN_SAMPLES
|
1262 CSO_BIT_RENDER_CONDITION
|
1263 CSO_BITS_ALL_SHADERS
));
1264 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1266 cso_set_sample_mask(cso
, ~0);
1267 cso_set_min_samples(cso
, 1);
1268 cso_set_render_condition(cso
, NULL
, FALSE
, 0);
1270 /* Set up the sampler_view */
1272 struct pipe_sampler_view templ
;
1273 struct pipe_sampler_view
*sampler_view
;
1275 memset(&templ
, 0, sizeof(templ
));
1276 templ
.target
= PIPE_BUFFER
;
1277 templ
.format
= src_format
;
1278 templ
.u
.buf
.offset
= addr
->first_element
* addr
->bytes_per_pixel
;
1279 templ
.u
.buf
.size
= (addr
->last_element
- addr
->first_element
+ 1) *
1280 addr
->bytes_per_pixel
;
1281 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1282 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1283 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1284 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1286 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1287 if (sampler_view
== NULL
)
1290 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1292 pipe_sampler_view_reference(&sampler_view
, NULL
);
1295 /* Framebuffer_state */
1297 struct pipe_framebuffer_state fb
;
1298 memset(&fb
, 0, sizeof(fb
));
1299 fb
.width
= surface
->width
;
1300 fb
.height
= surface
->height
;
1302 fb
.cbufs
[0] = surface
;
1304 cso_set_framebuffer(cso
, &fb
);
1307 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1310 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1312 /* Depth/stencil/alpha state */
1314 struct pipe_depth_stencil_alpha_state dsa
;
1315 memset(&dsa
, 0, sizeof(dsa
));
1316 cso_set_depth_stencil_alpha(cso
, &dsa
);
1319 /* Set up the fragment shader */
1320 cso_set_fragment_shader_handle(cso
, fs
);
1322 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1325 cso_restore_state(cso
);
1326 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1333 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1334 struct gl_texture_image
*texImage
,
1335 GLenum format
, GLenum type
,
1336 enum pipe_format dst_format
,
1337 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1338 GLint width
, GLint height
, GLint depth
,
1340 const struct gl_pixelstore_attrib
*unpack
)
1342 struct st_context
*st
= st_context(ctx
);
1343 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1344 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1345 struct pipe_resource
*texture
= stImage
->pt
;
1346 struct pipe_context
*pipe
= st
->pipe
;
1347 struct pipe_screen
*screen
= pipe
->screen
;
1348 struct pipe_surface
*surface
= NULL
;
1349 struct st_pbo_addresses addr
;
1350 enum pipe_format src_format
;
1351 const struct util_format_description
*desc
;
1352 GLenum gl_target
= texImage
->TexObject
->Target
;
1355 if (!st
->pbo
.upload_enabled
)
1358 /* From now on, we need the gallium representation of dimensions. */
1359 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1366 if (depth
!= 1 && !st
->pbo
.layers
)
1369 /* Choose the source format. Initially, we do so without checking driver
1370 * support at all because of the remapping we later perform and because
1371 * at least the Radeon driver actually supports some formats for texture
1372 * buffers which it doesn't support for regular textures. */
1373 src_format
= st_choose_matching_format(st
, 0, format
, type
,
1379 src_format
= util_format_linear(src_format
);
1380 desc
= util_format_description(src_format
);
1382 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1385 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1388 if (st
->pbo
.rgba_only
) {
1389 enum pipe_format orig_dst_format
= dst_format
;
1391 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1395 if (dst_format
!= orig_dst_format
&&
1396 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1397 0, PIPE_BIND_RENDER_TARGET
)) {
1403 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0, 0,
1404 PIPE_BIND_SAMPLER_VIEW
)) {
1408 /* Compute buffer addresses */
1409 addr
.xoffset
= xoffset
;
1410 addr
.yoffset
= yoffset
;
1412 addr
.height
= height
;
1414 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1416 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1420 /* Set up the surface */
1422 unsigned level
= stObj
->pt
!= stImage
->pt
1423 ? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1424 unsigned max_layer
= util_max_layer(texture
, level
);
1426 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1428 struct pipe_surface templ
;
1429 memset(&templ
, 0, sizeof(templ
));
1430 templ
.format
= dst_format
;
1431 templ
.u
.tex
.level
= level
;
1432 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1433 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1435 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1440 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1442 pipe_surface_reference(&surface
, NULL
);
1449 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1450 struct gl_texture_image
*texImage
,
1451 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1452 GLint width
, GLint height
, GLint depth
,
1453 GLenum format
, GLenum type
, const void *pixels
,
1454 const struct gl_pixelstore_attrib
*unpack
)
1456 struct st_context
*st
= st_context(ctx
);
1457 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1458 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1459 struct pipe_context
*pipe
= st
->pipe
;
1460 struct pipe_screen
*screen
= pipe
->screen
;
1461 struct pipe_resource
*dst
= stImage
->pt
;
1462 struct pipe_resource
*src
= NULL
;
1463 struct pipe_resource src_templ
;
1464 struct pipe_transfer
*transfer
;
1465 struct pipe_blit_info blit
;
1466 enum pipe_format src_format
, dst_format
;
1467 mesa_format mesa_src_format
;
1468 GLenum gl_target
= texImage
->TexObject
->Target
;
1471 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1472 unsigned dst_level
= 0;
1473 bool throttled
= false;
1475 st_flush_bitmap_cache(st
);
1476 st_invalidate_readpix_cache(st
);
1478 if (stObj
->pt
== stImage
->pt
)
1479 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1481 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1482 !_mesa_is_format_astc_2d(texImage
->TexFormat
) &&
1483 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1488 /* Try texture_subdata, which should be the fastest memcpy path. */
1490 !unpack
->BufferObj
&&
1491 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1492 texImage
->TexFormat
, format
, type
,
1494 struct pipe_box box
;
1495 unsigned stride
, layer_stride
;
1498 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1499 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1501 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1504 /* Convert to Gallium coordinates. */
1505 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1510 layer_stride
= stride
;
1513 util_throttle_memory_usage(pipe
, &st
->throttle
,
1514 (uint64_t) width
* height
* depth
*
1515 util_format_get_blocksize(dst
->format
));
1517 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1518 pipe
->texture_subdata(pipe
, dst
, dst_level
, 0,
1519 &box
, data
, stride
, layer_stride
);
1523 if (!st
->prefer_blit_based_texture_transfer
) {
1527 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1528 * blit implementation in some drivers. */
1529 if (format
== GL_DEPTH_STENCIL
) {
1533 /* If the base internal format and the texture format don't match,
1534 * we can't use blit-based TexSubImage. */
1535 if (texImage
->_BaseFormat
!=
1536 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1541 /* See if the destination format is supported. */
1542 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1543 bind
= PIPE_BIND_DEPTH_STENCIL
;
1545 bind
= PIPE_BIND_RENDER_TARGET
;
1547 /* For luminance and intensity, only the red channel is stored
1548 * in the destination. */
1549 dst_format
= util_format_linear(dst
->format
);
1550 dst_format
= util_format_luminance_to_red(dst_format
);
1551 dst_format
= util_format_intensity_to_red(dst_format
);
1554 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1555 dst
->nr_samples
, dst
->nr_storage_samples
,
1560 if (unpack
->BufferObj
) {
1561 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1562 xoffset
, yoffset
, zoffset
,
1563 width
, height
, depth
, pixels
, unpack
))
1567 /* See if the texture format already matches the format and type,
1568 * in which case the memcpy-based fast path will likely be used and
1569 * we don't have to blit. */
1570 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1571 type
, unpack
->SwapBytes
, NULL
)) {
1575 /* Choose the source format. */
1576 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1577 format
, type
, unpack
->SwapBytes
);
1582 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1584 /* There is no reason to do this if we cannot use memcpy for the temporary
1585 * source texture at least. This also takes transfer ops into account,
1587 if (!_mesa_texstore_can_use_memcpy(ctx
,
1588 _mesa_get_format_base_format(mesa_src_format
),
1589 mesa_src_format
, format
, type
, unpack
)) {
1593 /* TexSubImage only sets a single cubemap face. */
1594 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1595 gl_target
= GL_TEXTURE_2D
;
1597 /* TexSubImage can specify subsets of cube map array faces
1598 * so we need to upload via 2D array instead */
1599 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1600 gl_target
= GL_TEXTURE_2D_ARRAY
;
1603 /* Initialize the source texture description. */
1604 memset(&src_templ
, 0, sizeof(src_templ
));
1605 src_templ
.target
= gl_target_to_pipe(gl_target
);
1606 src_templ
.format
= src_format
;
1607 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1608 src_templ
.usage
= PIPE_USAGE_STAGING
;
1610 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1611 &src_templ
.width0
, &src_templ
.height0
,
1612 &src_templ
.depth0
, &src_templ
.array_size
);
1614 /* Check for NPOT texture support. */
1615 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1616 (!util_is_power_of_two_or_zero(src_templ
.width0
) ||
1617 !util_is_power_of_two_or_zero(src_templ
.height0
) ||
1618 !util_is_power_of_two_or_zero(src_templ
.depth0
))) {
1622 util_throttle_memory_usage(pipe
, &st
->throttle
,
1623 (uint64_t) width
* height
* depth
*
1624 util_format_get_blocksize(src_templ
.format
));
1627 /* Create the source texture. */
1628 src
= screen
->resource_create(screen
, &src_templ
);
1633 /* Map source pixels. */
1634 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1635 format
, type
, pixels
, unpack
,
1638 /* This is a GL error. */
1639 pipe_resource_reference(&src
, NULL
);
1643 /* From now on, we need the gallium representation of dimensions. */
1644 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1651 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1652 width
, height
, depth
, &transfer
);
1654 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1655 pipe_resource_reference(&src
, NULL
);
1659 /* Upload pixels (just memcpy). */
1661 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1664 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1665 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1666 /* 1D array textures.
1667 * We need to convert gallium coords to GL coords.
1669 void *src
= _mesa_image_address2d(unpack
, pixels
,
1670 width
, depth
, format
,
1672 memcpy(map
, src
, bytesPerRow
);
1675 ubyte
*slice_map
= map
;
1677 for (row
= 0; row
< (unsigned) height
; row
++) {
1678 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1679 width
, height
, format
,
1680 type
, slice
, row
, 0);
1681 memcpy(slice_map
, src
, bytesPerRow
);
1682 slice_map
+= transfer
->stride
;
1685 map
+= transfer
->layer_stride
;
1689 pipe_transfer_unmap(pipe
, transfer
);
1690 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1693 memset(&blit
, 0, sizeof(blit
));
1694 blit
.src
.resource
= src
;
1696 blit
.src
.format
= src_format
;
1697 blit
.dst
.resource
= dst
;
1698 blit
.dst
.level
= dst_level
;
1699 blit
.dst
.format
= dst_format
;
1700 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1701 blit
.dst
.box
.x
= xoffset
;
1702 blit
.dst
.box
.y
= yoffset
;
1703 blit
.dst
.box
.z
= zoffset
+ dstz
;
1704 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1705 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1706 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1707 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1708 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1709 blit
.scissor_enable
= FALSE
;
1711 st
->pipe
->blit(st
->pipe
, &blit
);
1713 pipe_resource_reference(&src
, NULL
);
1718 util_throttle_memory_usage(pipe
, &st
->throttle
,
1719 (uint64_t) width
* height
* depth
*
1720 _mesa_get_format_bytes(texImage
->TexFormat
));
1722 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1723 width
, height
, depth
, format
, type
, pixels
,
1729 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1730 struct gl_texture_image
*texImage
,
1731 GLenum format
, GLenum type
, const void *pixels
,
1732 const struct gl_pixelstore_attrib
*unpack
)
1734 assert(dims
== 1 || dims
== 2 || dims
== 3);
1736 prep_teximage(ctx
, texImage
, format
, type
);
1738 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1741 /* allocate storage for texture data */
1742 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1743 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1747 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1748 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1749 format
, type
, pixels
, unpack
);
1754 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1755 struct gl_texture_image
*texImage
,
1756 GLint x
, GLint y
, GLint z
,
1757 GLsizei w
, GLsizei h
, GLsizei d
,
1758 GLenum format
, GLsizei imageSize
, const void *data
)
1760 struct st_context
*st
= st_context(ctx
);
1761 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1762 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1763 struct pipe_resource
*texture
= stImage
->pt
;
1764 struct pipe_context
*pipe
= st
->pipe
;
1765 struct pipe_screen
*screen
= pipe
->screen
;
1766 struct pipe_resource
*dst
= stImage
->pt
;
1767 struct pipe_surface
*surface
= NULL
;
1768 struct compressed_pixelstore store
;
1769 struct st_pbo_addresses addr
;
1770 enum pipe_format copy_format
;
1772 intptr_t buf_offset
;
1773 bool success
= false;
1775 /* Check basic pre-conditions for PBO upload */
1776 if (!st
->prefer_blit_based_texture_transfer
) {
1780 if (!ctx
->Unpack
.BufferObj
)
1783 if (st_compressed_format_fallback(st
, texImage
->TexFormat
))
1790 if (!st
->pbo
.upload_enabled
||
1791 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1795 /* Choose the pipe format for the upload. */
1796 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1797 bw
= util_format_get_blockwidth(dst
->format
);
1798 bh
= util_format_get_blockheight(dst
->format
);
1800 switch (addr
.bytes_per_pixel
) {
1802 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1805 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1811 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0, 0,
1812 PIPE_BIND_SAMPLER_VIEW
)) {
1816 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1817 dst
->nr_samples
, dst
->nr_storage_samples
,
1818 PIPE_BIND_RENDER_TARGET
)) {
1822 /* Interpret the pixelstore settings. */
1823 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1824 &ctx
->Unpack
, &store
);
1825 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1826 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1828 /* Compute the offset into the buffer */
1829 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1831 if (buf_offset
% addr
.bytes_per_pixel
) {
1835 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1837 addr
.xoffset
= x
/ bw
;
1838 addr
.yoffset
= y
/ bh
;
1839 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1840 addr
.height
= store
.CopyRowsPerSlice
;
1842 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1843 addr
.image_height
= store
.TotalRowsPerSlice
;
1845 if (!st_pbo_addresses_setup(st
,
1846 st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1850 /* Set up the surface. */
1852 unsigned level
= stObj
->pt
!= stImage
->pt
1853 ? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1854 unsigned max_layer
= util_max_layer(texture
, level
);
1856 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1858 struct pipe_surface templ
;
1859 memset(&templ
, 0, sizeof(templ
));
1860 templ
.format
= copy_format
;
1861 templ
.u
.tex
.level
= level
;
1862 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1863 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1865 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1870 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1872 pipe_surface_reference(&surface
, NULL
);
1878 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1880 format
, imageSize
, data
);
1885 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1886 struct gl_texture_image
*texImage
,
1887 GLsizei imageSize
, const void *data
)
1889 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1891 /* only 2D and 3D compressed images are supported at this time */
1893 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1897 /* This is pretty simple, because unlike the general texstore path we don't
1898 * have to worry about the usual image unpacking or image transfer
1902 assert(texImage
->Width
> 0);
1903 assert(texImage
->Height
> 0);
1904 assert(texImage
->Depth
> 0);
1906 /* allocate storage for texture data */
1907 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1908 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1912 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1914 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1915 texImage
->TexFormat
,
1921 * Called via ctx->Driver.GetTexSubImage()
1923 * This uses a blit to copy the texture to a texture format which matches
1924 * the format and type combo and then a fast read-back is done using memcpy.
1925 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1926 * a format which matches the swizzling.
1928 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1930 * NOTE: Drivers usually do a blit to convert between tiled and linear
1931 * texture layouts during texture uploads/downloads, so the blit
1932 * we do here should be free in such cases.
1935 st_GetTexSubImage(struct gl_context
* ctx
,
1936 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1937 GLsizei width
, GLsizei height
, GLint depth
,
1938 GLenum format
, GLenum type
, void * pixels
,
1939 struct gl_texture_image
*texImage
)
1941 struct st_context
*st
= st_context(ctx
);
1942 struct pipe_context
*pipe
= st
->pipe
;
1943 struct pipe_screen
*screen
= pipe
->screen
;
1944 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1945 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1946 struct pipe_resource
*src
= stObj
->pt
;
1947 struct pipe_resource
*dst
= NULL
;
1948 struct pipe_resource dst_templ
;
1949 enum pipe_format dst_format
, src_format
;
1950 mesa_format mesa_format
;
1951 GLenum gl_target
= texImage
->TexObject
->Target
;
1952 enum pipe_texture_target pipe_target
;
1954 struct pipe_blit_info blit
;
1956 struct pipe_transfer
*tex_xfer
;
1958 boolean done
= FALSE
;
1960 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1961 !_mesa_is_format_astc_2d(texImage
->TexFormat
) &&
1962 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1964 st_flush_bitmap_cache(st
);
1966 if (!st
->prefer_blit_based_texture_transfer
&&
1967 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1968 /* Try to avoid the fallback if we're doing texture decompression here */
1972 /* Handle non-finalized textures. */
1973 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1977 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1978 * due to an incomplete stencil blit implementation in some drivers. */
1979 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1983 /* If the base internal format and the texture format don't match, we have
1984 * to fall back to _mesa_GetTexImage_sw. */
1985 if (texImage
->_BaseFormat
!=
1986 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1990 /* See if the texture format already matches the format and type,
1991 * in which case the memcpy-based fast path will be used. */
1992 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1993 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1997 /* Convert the source format to what is expected by GetTexImage
1998 * and see if it's supported.
2000 * This only applies to glGetTexImage:
2001 * - Luminance must be returned as (L,0,0,1).
2002 * - Luminance alpha must be returned as (L,0,0,A).
2003 * - Intensity must be returned as (I,0,0,1)
2005 if (stObj
->surface_based
)
2006 src_format
= util_format_linear(stObj
->surface_format
);
2008 src_format
= util_format_linear(src
->format
);
2009 src_format
= util_format_luminance_to_red(src_format
);
2010 src_format
= util_format_intensity_to_red(src_format
);
2013 !screen
->is_format_supported(screen
, src_format
, src
->target
,
2014 src
->nr_samples
, src
->nr_storage_samples
,
2015 PIPE_BIND_SAMPLER_VIEW
)) {
2019 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
2020 bind
= PIPE_BIND_DEPTH_STENCIL
;
2022 bind
= PIPE_BIND_RENDER_TARGET
;
2024 /* GetTexImage only returns a single face for cubemaps. */
2025 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
2026 gl_target
= GL_TEXTURE_2D
;
2028 pipe_target
= gl_target_to_pipe(gl_target
);
2030 /* Choose the destination format by finding the best match
2031 * for the format+type combo. */
2032 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
2033 ctx
->Pack
.SwapBytes
);
2035 if (dst_format
== PIPE_FORMAT_NONE
) {
2036 GLenum dst_glformat
;
2038 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
2039 * where decompression with a blit is always preferred. */
2040 if (!util_format_is_compressed(src
->format
)) {
2044 /* Set the appropriate format for the decompressed texture.
2045 * Luminance and sRGB formats shouldn't appear here.*/
2046 switch (src_format
) {
2047 case PIPE_FORMAT_DXT1_RGB
:
2048 case PIPE_FORMAT_DXT1_RGBA
:
2049 case PIPE_FORMAT_DXT3_RGBA
:
2050 case PIPE_FORMAT_DXT5_RGBA
:
2051 case PIPE_FORMAT_RGTC1_UNORM
:
2052 case PIPE_FORMAT_RGTC2_UNORM
:
2053 case PIPE_FORMAT_ETC1_RGB8
:
2054 case PIPE_FORMAT_ETC2_RGB8
:
2055 case PIPE_FORMAT_ETC2_RGB8A1
:
2056 case PIPE_FORMAT_ETC2_RGBA8
:
2057 case PIPE_FORMAT_ASTC_4x4
:
2058 case PIPE_FORMAT_ASTC_5x4
:
2059 case PIPE_FORMAT_ASTC_5x5
:
2060 case PIPE_FORMAT_ASTC_6x5
:
2061 case PIPE_FORMAT_ASTC_6x6
:
2062 case PIPE_FORMAT_ASTC_8x5
:
2063 case PIPE_FORMAT_ASTC_8x6
:
2064 case PIPE_FORMAT_ASTC_8x8
:
2065 case PIPE_FORMAT_ASTC_10x5
:
2066 case PIPE_FORMAT_ASTC_10x6
:
2067 case PIPE_FORMAT_ASTC_10x8
:
2068 case PIPE_FORMAT_ASTC_10x10
:
2069 case PIPE_FORMAT_ASTC_12x10
:
2070 case PIPE_FORMAT_ASTC_12x12
:
2071 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
2072 case PIPE_FORMAT_FXT1_RGB
:
2073 case PIPE_FORMAT_FXT1_RGBA
:
2074 dst_glformat
= GL_RGBA8
;
2076 case PIPE_FORMAT_RGTC1_SNORM
:
2077 case PIPE_FORMAT_RGTC2_SNORM
:
2078 if (!ctx
->Extensions
.EXT_texture_snorm
)
2080 dst_glformat
= GL_RGBA8_SNORM
;
2082 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
2083 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
2084 if (!ctx
->Extensions
.ARB_texture_float
)
2086 dst_glformat
= GL_RGBA32F
;
2088 case PIPE_FORMAT_ETC2_R11_UNORM
:
2089 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16_UNORM
,
2090 pipe_target
, 0, 0, bind
))
2092 dst_glformat
= GL_R16
;
2094 case PIPE_FORMAT_ETC2_R11_SNORM
:
2095 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16_SNORM
,
2096 pipe_target
, 0, 0, bind
))
2098 dst_glformat
= GL_R16_SNORM
;
2100 case PIPE_FORMAT_ETC2_RG11_UNORM
:
2101 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16G16_UNORM
,
2102 pipe_target
, 0, 0, bind
))
2104 dst_glformat
= GL_RG16
;
2106 case PIPE_FORMAT_ETC2_RG11_SNORM
:
2107 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16G16_SNORM
,
2108 pipe_target
, 0, 0, bind
))
2110 dst_glformat
= GL_RG16_SNORM
;
2117 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
2118 pipe_target
, 0, 0, bind
,
2121 if (dst_format
== PIPE_FORMAT_NONE
) {
2122 /* unable to get an rgba format!?! */
2127 /* create the destination texture of size (width X height X depth) */
2128 memset(&dst_templ
, 0, sizeof(dst_templ
));
2129 dst_templ
.target
= pipe_target
;
2130 dst_templ
.format
= dst_format
;
2131 dst_templ
.bind
= bind
;
2132 dst_templ
.usage
= PIPE_USAGE_STAGING
;
2134 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
2135 &dst_templ
.width0
, &dst_templ
.height0
,
2136 &dst_templ
.depth0
, &dst_templ
.array_size
);
2138 dst
= screen
->resource_create(screen
, &dst_templ
);
2143 /* From now on, we need the gallium representation of dimensions. */
2144 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2151 assert(texImage
->Face
== 0 ||
2152 texImage
->TexObject
->MinLayer
== 0 ||
2155 memset(&blit
, 0, sizeof(blit
));
2156 blit
.src
.resource
= src
;
2157 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2158 blit
.src
.format
= src_format
;
2159 blit
.dst
.resource
= dst
;
2161 blit
.dst
.format
= dst
->format
;
2162 blit
.src
.box
.x
= xoffset
;
2164 blit
.src
.box
.y
= yoffset
;
2166 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2168 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2169 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2170 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2171 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2172 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2173 blit
.scissor_enable
= FALSE
;
2175 /* blit/render/decompress */
2176 st
->pipe
->blit(st
->pipe
, &blit
);
2178 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2180 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2181 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2186 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2187 dims
= _mesa_get_texture_dimensions(gl_target
);
2189 /* copy/pack data into user buffer */
2190 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2191 ctx
->Pack
.SwapBytes
, NULL
)) {
2193 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2196 for (slice
= 0; slice
< depth
; slice
++) {
2197 ubyte
*slice_map
= map
;
2199 for (row
= 0; row
< height
; row
++) {
2200 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2201 width
, height
, format
, type
,
2204 memcpy(dest
, slice_map
, bytesPerRow
);
2206 slice_map
+= tex_xfer
->stride
;
2209 map
+= tex_xfer
->layer_stride
;
2213 /* format translation via floats */
2216 uint32_t dstMesaFormat
;
2217 int dstStride
, srcStride
;
2219 assert(util_format_is_compressed(src
->format
));
2221 rgba
= malloc(width
* height
* 4 * sizeof(GLfloat
));
2226 if (ST_DEBUG
& DEBUG_FALLBACK
)
2227 debug_printf("%s: fallback format translation\n", __func__
);
2229 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2230 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2231 srcStride
= 4 * width
* sizeof(GLfloat
);
2232 for (slice
= 0; slice
< depth
; slice
++) {
2233 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2234 width
, height
, format
, type
,
2237 /* get float[4] rgba row from surface */
2238 pipe_get_tile_rgba(tex_xfer
, map
, 0, 0, width
, height
, dst_format
,
2241 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2242 rgba
, RGBA32_FLOAT
, srcStride
,
2243 width
, height
, NULL
);
2245 /* Handle byte swapping if required */
2246 if (ctx
->Pack
.SwapBytes
) {
2247 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
2248 width
, height
, dest
, dest
);
2251 map
+= tex_xfer
->layer_stride
;
2260 pipe_transfer_unmap(pipe
, tex_xfer
);
2262 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2263 pipe_resource_reference(&dst
, NULL
);
2267 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2268 width
, height
, depth
,
2269 format
, type
, pixels
, texImage
);
2275 * Do a CopyTexSubImage operation using a read transfer from the source,
2276 * a write transfer to the destination and get_tile()/put_tile() to access
2277 * the pixels/texels.
2279 * Note: srcY=0=TOP of renderbuffer
2282 fallback_copy_texsubimage(struct gl_context
*ctx
,
2283 struct st_renderbuffer
*strb
,
2284 struct st_texture_image
*stImage
,
2286 GLint destX
, GLint destY
, GLint slice
,
2287 GLint srcX
, GLint srcY
,
2288 GLsizei width
, GLsizei height
)
2290 struct st_context
*st
= st_context(ctx
);
2291 struct pipe_context
*pipe
= st
->pipe
;
2292 struct pipe_transfer
*src_trans
;
2294 enum pipe_transfer_usage transfer_usage
;
2296 unsigned dst_width
= width
;
2297 unsigned dst_height
= height
;
2298 unsigned dst_depth
= 1;
2299 struct pipe_transfer
*transfer
;
2301 if (ST_DEBUG
& DEBUG_FALLBACK
)
2302 debug_printf("%s: fallback processing\n", __func__
);
2304 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2305 srcY
= strb
->Base
.Height
- srcY
- height
;
2308 map
= pipe_transfer_map(pipe
,
2310 strb
->surface
->u
.tex
.level
,
2311 strb
->surface
->u
.tex
.first_layer
,
2314 width
, height
, &src_trans
);
2316 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2320 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2321 baseFormat
== GL_DEPTH_STENCIL
) &&
2322 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2323 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2325 transfer_usage
= PIPE_TRANSFER_WRITE
;
2327 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2328 destX
, destY
, slice
,
2329 dst_width
, dst_height
, dst_depth
,
2332 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2336 if (baseFormat
== GL_DEPTH_COMPONENT
||
2337 baseFormat
== GL_DEPTH_STENCIL
) {
2338 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2339 ctx
->Pixel
.DepthBias
!= 0.0F
);
2343 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2344 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2353 data
= malloc(width
* sizeof(uint
));
2356 unsigned dst_stride
= (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
?
2357 transfer
->layer_stride
: transfer
->stride
);
2358 /* To avoid a large temp memory allocation, do copy row by row */
2359 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2360 util_format_unpack_z_32unorm(strb
->texture
->format
,
2361 data
, (uint8_t *)map
+ src_trans
->stride
* srcY
,
2364 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2367 util_format_pack_z_32unorm(stImage
->pt
->format
,
2368 texDest
+ row
* dst_stride
, data
, width
);
2372 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2380 malloc(width
* height
* 4 * sizeof(GLfloat
));
2383 const GLint dims
= 2;
2385 struct gl_texture_image
*texImage
= &stImage
->base
;
2386 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2388 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2389 unpack
.Invert
= GL_TRUE
;
2392 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2393 dstRowStride
= transfer
->layer_stride
;
2396 dstRowStride
= transfer
->stride
;
2399 /* get float/RGBA image from framebuffer */
2400 /* XXX this usually involves a lot of int/float conversion.
2401 * try to avoid that someday.
2403 pipe_get_tile_rgba(src_trans
, map
, 0, 0, width
, height
,
2404 util_format_linear(strb
->texture
->format
),
2407 /* Store into texture memory.
2408 * Note that this does some special things such as pixel transfer
2409 * ops and format conversion. In particular, if the dest tex format
2410 * is actually RGBA but the user created the texture as GL_RGB we
2411 * need to fill-in/override the alpha channel with 1.0.
2413 _mesa_texstore(ctx
, dims
,
2414 texImage
->_BaseFormat
,
2415 texImage
->TexFormat
,
2419 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2423 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2429 st_texture_image_unmap(st
, stImage
, slice
);
2431 pipe
->transfer_unmap(pipe
, src_trans
);
2436 st_can_copyteximage_using_blit(const struct gl_texture_image
*texImage
,
2437 const struct gl_renderbuffer
*rb
)
2439 GLenum tex_baseformat
= _mesa_get_format_base_format(texImage
->TexFormat
);
2441 /* We don't blit to a teximage where the GL base format doesn't match the
2442 * texture's chosen format, except in the case of a GL_RGB texture
2443 * represented with GL_RGBA (where the alpha channel is just being
2446 if (texImage
->_BaseFormat
!= tex_baseformat
&&
2447 ((texImage
->_BaseFormat
!= GL_RGB
|| tex_baseformat
!= GL_RGBA
))) {
2451 /* We can't blit from a RB where the GL base format doesn't match the RB's
2452 * chosen format (for example, GL RGB or ALPHA with rb->Format of an RGBA
2453 * type, because the other channels will be undefined).
2455 if (rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
))
2463 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2464 * Note that the region to copy has already been clipped so we know we
2465 * won't read from outside the source renderbuffer's bounds.
2467 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2470 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2471 struct gl_texture_image
*texImage
,
2472 GLint destX
, GLint destY
, GLint slice
,
2473 struct gl_renderbuffer
*rb
,
2474 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2476 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2477 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2478 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2479 struct st_context
*st
= st_context(ctx
);
2480 struct pipe_context
*pipe
= st
->pipe
;
2481 struct pipe_screen
*screen
= pipe
->screen
;
2482 struct pipe_blit_info blit
;
2483 enum pipe_format dst_format
;
2484 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2488 st_flush_bitmap_cache(st
);
2489 st_invalidate_readpix_cache(st
);
2491 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2492 !_mesa_is_format_astc_2d(texImage
->TexFormat
) &&
2493 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2495 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2496 debug_printf("%s: null strb or stImage\n", __func__
);
2500 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2501 texImage
->TexFormat
)) {
2505 if (!st_can_copyteximage_using_blit(texImage
, rb
)) {
2509 /* Choose the destination format to match the TexImage behavior. */
2510 dst_format
= util_format_linear(stImage
->pt
->format
);
2511 dst_format
= util_format_luminance_to_red(dst_format
);
2512 dst_format
= util_format_intensity_to_red(dst_format
);
2514 /* See if the destination format is supported. */
2515 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2516 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2517 bind
= PIPE_BIND_DEPTH_STENCIL
;
2520 bind
= PIPE_BIND_RENDER_TARGET
;
2524 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2525 stImage
->pt
->nr_samples
,
2526 stImage
->pt
->nr_storage_samples
, bind
)) {
2530 /* Y flipping for the main framebuffer. */
2532 srcY1
= strb
->Base
.Height
- srcY
- height
;
2533 srcY0
= srcY1
+ height
;
2537 srcY1
= srcY0
+ height
;
2540 /* Blit the texture.
2541 * This supports flipping, format conversions, and downsampling.
2543 memset(&blit
, 0, sizeof(blit
));
2544 blit
.src
.resource
= strb
->texture
;
2545 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2546 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2547 blit
.src
.box
.x
= srcX
;
2548 blit
.src
.box
.y
= srcY0
;
2549 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2550 blit
.src
.box
.width
= width
;
2551 blit
.src
.box
.height
= srcY1
- srcY0
;
2552 blit
.src
.box
.depth
= 1;
2553 blit
.dst
.resource
= stImage
->pt
;
2554 blit
.dst
.format
= dst_format
;
2555 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
2556 ? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2557 blit
.dst
.box
.x
= destX
;
2558 blit
.dst
.box
.y
= destY
;
2559 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2560 blit
.dst
.box
.width
= width
;
2561 blit
.dst
.box
.height
= height
;
2562 blit
.dst
.box
.depth
= 1;
2563 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2564 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2565 pipe
->blit(pipe
, &blit
);
2569 /* software fallback */
2570 fallback_copy_texsubimage(ctx
,
2571 strb
, stImage
, texImage
->_BaseFormat
,
2572 destX
, destY
, slice
,
2573 srcX
, srcY
, width
, height
);
2578 * Copy image data from stImage into the texture object 'stObj' at level
2582 copy_image_data_to_texture(struct st_context
*st
,
2583 struct st_texture_object
*stObj
,
2585 struct st_texture_image
*stImage
)
2589 ASSERTED
const struct gl_texture_image
*dstImage
=
2590 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2592 assert(dstImage
->Width
== stImage
->base
.Width
);
2593 assert(dstImage
->Height
== stImage
->base
.Height
);
2594 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2598 /* Copy potentially with the blitter:
2601 if (stImage
->pt
->last_level
== 0)
2604 src_level
= stImage
->base
.Level
;
2606 assert(src_level
<= stImage
->pt
->last_level
);
2607 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2608 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2609 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2610 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2611 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2612 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2614 st_texture_image_copy(st
->pipe
,
2615 stObj
->pt
, dstLevel
, /* dest texture, level */
2616 stImage
->pt
, src_level
, /* src texture, level */
2617 stImage
->base
.Face
);
2619 pipe_resource_reference(&stImage
->pt
, NULL
);
2621 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2626 * Called during state validation. When this function is finished,
2627 * the texture object should be ready for rendering.
2628 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2631 st_finalize_texture(struct gl_context
*ctx
,
2632 struct pipe_context
*pipe
,
2633 struct gl_texture_object
*tObj
,
2636 struct st_context
*st
= st_context(ctx
);
2637 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2638 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2640 const struct st_texture_image
*firstImage
;
2641 enum pipe_format firstImageFormat
;
2643 uint16_t ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2645 if (tObj
->Immutable
)
2648 if (tObj
->_MipmapComplete
)
2649 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2650 else if (tObj
->_BaseComplete
)
2651 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2653 /* Skip the loop over images in the common case of no images having
2654 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
2655 * haven't looked at, then we do need to look at those new images.
2657 if (!stObj
->needs_validation
&&
2658 stObj
->base
.BaseLevel
>= stObj
->validated_first_level
&&
2659 stObj
->lastLevel
<= stObj
->validated_last_level
) {
2663 /* If this texture comes from a window system, there is nothing else to do. */
2664 if (stObj
->surface_based
) {
2668 firstImage
= st_texture_image_const(stObj
->base
.Image
[cubeMapFace
]
2669 [stObj
->base
.BaseLevel
]);
2672 /* If both firstImage and stObj point to a texture which can contain
2673 * all active images, favour firstImage. Note that because of the
2674 * completeness requirement, we know that the image dimensions
2677 if (firstImage
->pt
&&
2678 firstImage
->pt
!= stObj
->pt
&&
2679 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2680 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2681 st_texture_release_all_sampler_views(st
, stObj
);
2684 /* Find gallium format for the Mesa texture */
2686 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2688 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2691 uint16_t height
, depth
;
2693 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2694 firstImage
->base
.Width2
,
2695 firstImage
->base
.Height2
,
2696 firstImage
->base
.Depth2
,
2697 &width
, &height
, &depth
, &ptLayers
);
2699 /* If we previously allocated a pipe texture and its sizes are
2700 * compatible, use them.
2703 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2704 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2705 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2706 ptWidth
= stObj
->pt
->width0
;
2707 ptHeight
= stObj
->pt
->height0
;
2708 ptDepth
= stObj
->pt
->depth0
;
2710 /* Otherwise, compute a new level=0 size that is compatible with the
2713 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2714 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2715 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2717 /* If the base level image is 1x1x1, we still need to ensure that the
2718 * resulting pipe texture ends up with the required number of levels
2721 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2722 ptWidth
<<= firstImage
->base
.Level
;
2724 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2725 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2729 /* At this point, the texture may be incomplete (mismatched cube
2730 * face sizes, for example). If that's the case, give up, but
2731 * don't return GL_FALSE as that would raise an incorrect
2732 * GL_OUT_OF_MEMORY error. See Piglit fbo-incomplete-texture-03 test.
2734 if (!stObj
->base
._BaseComplete
) {
2735 _mesa_test_texobj_completeness(ctx
, &stObj
->base
);
2736 if (!stObj
->base
._BaseComplete
) {
2742 ptNumSamples
= firstImage
->base
.NumSamples
;
2745 /* If we already have a gallium texture, check that it matches the texture
2746 * object's format, target, size, num_levels, etc.
2749 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2750 stObj
->pt
->format
!= firstImageFormat
||
2751 stObj
->pt
->last_level
< stObj
->lastLevel
||
2752 stObj
->pt
->width0
!= ptWidth
||
2753 stObj
->pt
->height0
!= ptHeight
||
2754 stObj
->pt
->depth0
!= ptDepth
||
2755 stObj
->pt
->nr_samples
!= ptNumSamples
||
2756 stObj
->pt
->array_size
!= ptLayers
)
2758 /* The gallium texture does not match the Mesa texture so delete the
2759 * gallium texture now. We'll make a new one below.
2761 pipe_resource_reference(&stObj
->pt
, NULL
);
2762 st_texture_release_all_sampler_views(st
, stObj
);
2763 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
2767 /* May need to create a new gallium texture:
2770 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2772 stObj
->pt
= st_texture_create(st
,
2773 gl_target_to_pipe(stObj
->base
.Target
),
2779 ptLayers
, ptNumSamples
,
2783 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2788 /* Pull in any images not in the object's texture:
2790 for (face
= 0; face
< nr_faces
; face
++) {
2792 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2793 struct st_texture_image
*stImage
=
2794 st_texture_image(stObj
->base
.Image
[face
][level
]);
2796 /* Need to import images in main memory or held in other textures.
2798 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2802 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2803 height
= u_minify(ptHeight
, level
);
2807 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2808 depth
= u_minify(ptDepth
, level
);
2809 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2815 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2816 stImage
->base
.Height
== height
&&
2817 stImage
->base
.Depth
== depth
)) {
2818 /* src image fits expected dest mipmap level size */
2819 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2825 stObj
->validated_first_level
= stObj
->base
.BaseLevel
;
2826 stObj
->validated_last_level
= stObj
->lastLevel
;
2827 stObj
->needs_validation
= false;
2834 * Allocate a new pipe_resource object
2835 * width0, height0, depth0 are the dimensions of the level 0 image
2836 * (the highest resolution). last_level indicates how many mipmap levels
2837 * to allocate storage for. For non-mipmapped textures, this will be zero.
2839 static struct pipe_resource
*
2840 st_texture_create_from_memory(struct st_context
*st
,
2841 struct st_memory_object
*memObj
,
2843 enum pipe_texture_target target
,
2844 enum pipe_format format
,
2853 struct pipe_resource pt
, *newtex
;
2854 struct pipe_screen
*screen
= st
->pipe
->screen
;
2856 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
2858 assert(height0
> 0);
2860 if (target
== PIPE_TEXTURE_CUBE
)
2861 assert(layers
== 6);
2863 DBG("%s target %d format %s last_level %d\n", __func__
,
2864 (int) target
, util_format_name(format
), last_level
);
2867 assert(screen
->is_format_supported(screen
, format
, target
, 0, 0,
2868 PIPE_BIND_SAMPLER_VIEW
));
2870 memset(&pt
, 0, sizeof(pt
));
2873 pt
.last_level
= last_level
;
2875 pt
.height0
= height0
;
2877 pt
.array_size
= layers
;
2878 pt
.usage
= PIPE_USAGE_DEFAULT
;
2880 /* only set this for OpenGL textures, not renderbuffers */
2881 pt
.flags
= PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
;
2882 pt
.nr_samples
= nr_samples
;
2883 pt
.nr_storage_samples
= nr_samples
;
2885 newtex
= screen
->resource_from_memobj(screen
, &pt
, memObj
->memory
, offset
);
2887 assert(!newtex
|| pipe_is_referenced(&newtex
->reference
));
2894 * Allocate texture memory for a whole mipmap stack.
2895 * Note: for multisample textures if the requested sample count is not
2896 * supported, we search for the next higher supported sample count.
2899 st_texture_storage(struct gl_context
*ctx
,
2900 struct gl_texture_object
*texObj
,
2901 GLsizei levels
, GLsizei width
,
2902 GLsizei height
, GLsizei depth
,
2903 struct gl_memory_object
*memObj
,
2906 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2907 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2908 struct st_context
*st
= st_context(ctx
);
2909 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2910 struct st_memory_object
*smObj
= st_memory_object(memObj
);
2911 struct pipe_screen
*screen
= st
->pipe
->screen
;
2912 unsigned ptWidth
, bindings
;
2913 uint16_t ptHeight
, ptDepth
, ptLayers
;
2914 enum pipe_format fmt
;
2916 GLuint num_samples
= texImage
->NumSamples
;
2920 stObj
->lastLevel
= levels
- 1;
2922 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2924 bindings
= default_bindings(st
, fmt
);
2926 if (num_samples
> 0) {
2927 /* Find msaa sample count which is actually supported. For example,
2928 * if the user requests 1x but only 4x or 8x msaa is supported, we'll
2931 enum pipe_texture_target ptarget
= gl_target_to_pipe(texObj
->Target
);
2932 boolean found
= FALSE
;
2934 if (ctx
->Const
.MaxSamples
> 1 && num_samples
== 1) {
2935 /* don't try num_samples = 1 with drivers that support real msaa */
2939 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2940 if (screen
->is_format_supported(screen
, fmt
, ptarget
,
2941 num_samples
, num_samples
,
2942 PIPE_BIND_SAMPLER_VIEW
)) {
2943 /* Update the sample count in gl_texture_image as well. */
2944 texImage
->NumSamples
= num_samples
;
2955 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2956 width
, height
, depth
,
2957 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2960 stObj
->pt
= st_texture_create_from_memory(st
,
2963 gl_target_to_pipe(texObj
->Target
),
2969 ptLayers
, num_samples
,
2973 stObj
->pt
= st_texture_create(st
,
2974 gl_target_to_pipe(texObj
->Target
),
2980 ptLayers
, num_samples
,
2987 /* Set image resource pointers */
2988 for (level
= 0; level
< levels
; level
++) {
2990 for (face
= 0; face
< numFaces
; face
++) {
2991 struct st_texture_image
*stImage
=
2992 st_texture_image(texObj
->Image
[face
][level
]);
2993 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2995 compressed_tex_fallback_allocate(st
, stImage
);
2999 /* The texture is in a validated state, so no need to check later. */
3000 stObj
->needs_validation
= false;
3001 stObj
->validated_first_level
= 0;
3002 stObj
->validated_last_level
= levels
- 1;
3008 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
3009 * for a whole mipmap stack.
3012 st_AllocTextureStorage(struct gl_context
*ctx
,
3013 struct gl_texture_object
*texObj
,
3014 GLsizei levels
, GLsizei width
,
3015 GLsizei height
, GLsizei depth
)
3017 return st_texture_storage(ctx
, texObj
, levels
,
3018 width
, height
, depth
,
3024 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
3025 GLuint numLevels
, GLint level
,
3026 mesa_format format
, GLuint numSamples
,
3027 GLint width
, GLint height
, GLint depth
)
3029 struct st_context
*st
= st_context(ctx
);
3030 struct pipe_context
*pipe
= st
->pipe
;
3032 if (width
== 0 || height
== 0 || depth
== 0) {
3033 /* zero-sized images are legal, and always fit! */
3037 if (pipe
->screen
->can_create_resource
) {
3038 /* Ask the gallium driver if the texture is too large */
3039 struct gl_texture_object
*texObj
=
3040 _mesa_get_current_tex_object(ctx
, target
);
3041 struct pipe_resource pt
;
3043 /* Setup the pipe_resource object
3045 memset(&pt
, 0, sizeof(pt
));
3047 pt
.target
= gl_target_to_pipe(target
);
3048 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
3049 pt
.nr_samples
= numSamples
;
3050 pt
.nr_storage_samples
= numSamples
;
3052 st_gl_texture_dims_to_pipe_dims(target
,
3053 width
, height
, depth
,
3054 &pt
.width0
, &pt
.height0
,
3055 &pt
.depth0
, &pt
.array_size
);
3057 if (numLevels
> 0) {
3058 /* For immutable textures we know the final number of mip levels */
3059 pt
.last_level
= numLevels
- 1;
3061 else if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
3062 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
3063 /* assume just one mipmap level */
3067 /* assume a full set of mipmaps */
3068 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
3071 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
3074 /* Use core Mesa fallback */
3075 return _mesa_test_proxy_teximage(ctx
, target
, numLevels
, level
, format
,
3076 numSamples
, width
, height
, depth
);
3081 st_TextureView(struct gl_context
*ctx
,
3082 struct gl_texture_object
*texObj
,
3083 struct gl_texture_object
*origTexObj
)
3085 struct st_context
*st
= st_context(ctx
);
3086 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
3087 struct st_texture_object
*tex
= st_texture_object(texObj
);
3088 struct gl_texture_image
*image
= texObj
->Image
[0][0];
3090 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
3091 const int numLevels
= texObj
->NumLevels
;
3096 pipe_resource_reference(&tex
->pt
, orig
->pt
);
3098 /* Set image resource pointers */
3099 for (level
= 0; level
< numLevels
; level
++) {
3100 for (face
= 0; face
< numFaces
; face
++) {
3101 struct st_texture_image
*stImage
=
3102 st_texture_image(texObj
->Image
[face
][level
]);
3103 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
3107 tex
->surface_based
= GL_TRUE
;
3108 tex
->surface_format
=
3109 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
3111 tex
->lastLevel
= numLevels
- 1;
3113 /* free texture sampler views. They need to be recreated when we
3114 * change the texture view parameters.
3116 st_texture_release_all_sampler_views(st
, tex
);
3118 /* The texture is in a validated state, so no need to check later. */
3119 tex
->needs_validation
= false;
3120 tex
->validated_first_level
= 0;
3121 tex
->validated_last_level
= numLevels
- 1;
3128 * Find the mipmap level in 'pt' which matches the level described by
3132 find_mipmap_level(const struct gl_texture_image
*texImage
,
3133 const struct pipe_resource
*pt
)
3135 const GLenum target
= texImage
->TexObject
->Target
;
3136 GLint texWidth
= texImage
->Width
;
3137 GLint texHeight
= texImage
->Height
;
3138 GLint texDepth
= texImage
->Depth
;
3140 uint16_t h
, d
, layers
;
3142 st_gl_texture_dims_to_pipe_dims(target
, texWidth
, texHeight
, texDepth
,
3143 &w
, &h
, &d
, &layers
);
3145 for (level
= 0; level
<= pt
->last_level
; level
++) {
3146 if (u_minify(pt
->width0
, level
) == w
&&
3147 u_minify(pt
->height0
, level
) == h
&&
3148 u_minify(pt
->depth0
, level
) == d
) {
3153 /* If we get here, there must be some sort of inconsistency between
3154 * the Mesa texture object/images and the gallium resource.
3156 debug_printf("Inconsistent textures in find_mipmap_level()\n");
3158 return texImage
->Level
;
3163 st_ClearTexSubImage(struct gl_context
*ctx
,
3164 struct gl_texture_image
*texImage
,
3165 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3166 GLsizei width
, GLsizei height
, GLsizei depth
,
3167 const void *clearValue
)
3169 static const char zeros
[16] = {0};
3170 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3171 struct st_texture_image
*stImage
= st_texture_image(texImage
);
3172 struct pipe_resource
*pt
= stImage
->pt
;
3173 struct st_context
*st
= st_context(ctx
);
3174 struct pipe_context
*pipe
= st
->pipe
;
3176 struct pipe_box box
;
3181 st_flush_bitmap_cache(st
);
3182 st_invalidate_readpix_cache(st
);
3184 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
3185 width
, height
, depth
, &box
);
3186 if (texObj
->Immutable
) {
3187 /* The texture object has to be consistent (no "loose", per-image
3188 * gallium resources). If this texture is a view into another
3189 * texture, we have to apply the MinLevel/Layer offsets. If this is
3190 * not a texture view, the offsets will be zero.
3192 assert(stImage
->pt
== st_texture_object(texObj
)->pt
);
3193 level
= texImage
->Level
+ texObj
->MinLevel
;
3194 box
.z
+= texObj
->MinLayer
;
3197 /* Texture level sizes may be inconsistent. We my have "loose",
3198 * per-image gallium resources. The texImage->Level may not match
3199 * the gallium resource texture level.
3201 level
= find_mipmap_level(texImage
, pt
);
3204 assert(level
<= pt
->last_level
);
3206 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
3211 * Called via the glTexParam*() function, but only when some texture object
3212 * state has actually changed.
3215 st_TexParameter(struct gl_context
*ctx
,
3216 struct gl_texture_object
*texObj
, GLenum pname
)
3218 struct st_context
*st
= st_context(ctx
);
3219 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3222 case GL_TEXTURE_BASE_LEVEL
:
3223 case GL_TEXTURE_MAX_LEVEL
:
3224 case GL_DEPTH_TEXTURE_MODE
:
3225 case GL_DEPTH_STENCIL_TEXTURE_MODE
:
3226 case GL_TEXTURE_SRGB_DECODE_EXT
:
3227 case GL_TEXTURE_SWIZZLE_R
:
3228 case GL_TEXTURE_SWIZZLE_G
:
3229 case GL_TEXTURE_SWIZZLE_B
:
3230 case GL_TEXTURE_SWIZZLE_A
:
3231 case GL_TEXTURE_SWIZZLE_RGBA
:
3232 case GL_TEXTURE_BUFFER_SIZE
:
3233 case GL_TEXTURE_BUFFER_OFFSET
:
3234 /* changing any of these texture parameters means we must create
3235 * new sampler views.
3237 st_texture_release_all_sampler_views(st
, stObj
);
3245 st_SetTextureStorageForMemoryObject(struct gl_context
*ctx
,
3246 struct gl_texture_object
*texObj
,
3247 struct gl_memory_object
*memObj
,
3248 GLsizei levels
, GLsizei width
,
3249 GLsizei height
, GLsizei depth
,
3252 return st_texture_storage(ctx
, texObj
, levels
,
3253 width
, height
, depth
,
3258 st_NewTextureHandle(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
3259 struct gl_sampler_object
*sampObj
)
3261 struct st_context
*st
= st_context(ctx
);
3262 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3263 struct pipe_context
*pipe
= st
->pipe
;
3264 struct pipe_sampler_view
*view
;
3265 struct pipe_sampler_state sampler
= {0};
3267 if (texObj
->Target
!= GL_TEXTURE_BUFFER
) {
3268 if (!st_finalize_texture(ctx
, pipe
, texObj
, 0))
3271 st_convert_sampler(st
, texObj
, sampObj
, 0, &sampler
);
3273 /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
3274 view
= st_get_texture_sampler_view_from_stobj(st
, stObj
, sampObj
, 0, true);
3276 view
= st_get_buffer_sampler_view_from_stobj(st
, stObj
);
3279 return pipe
->create_texture_handle(pipe
, view
, &sampler
);
3284 st_DeleteTextureHandle(struct gl_context
*ctx
, GLuint64 handle
)
3286 struct st_context
*st
= st_context(ctx
);
3287 struct pipe_context
*pipe
= st
->pipe
;
3289 pipe
->delete_texture_handle(pipe
, handle
);
3294 st_MakeTextureHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3297 struct st_context
*st
= st_context(ctx
);
3298 struct pipe_context
*pipe
= st
->pipe
;
3300 pipe
->make_texture_handle_resident(pipe
, handle
, resident
);
3305 st_NewImageHandle(struct gl_context
*ctx
, struct gl_image_unit
*imgObj
)
3307 struct st_context
*st
= st_context(ctx
);
3308 struct pipe_context
*pipe
= st
->pipe
;
3309 struct pipe_image_view image
;
3311 st_convert_image(st
, imgObj
, &image
, GL_READ_WRITE
);
3313 return pipe
->create_image_handle(pipe
, &image
);
3318 st_DeleteImageHandle(struct gl_context
*ctx
, GLuint64 handle
)
3320 struct st_context
*st
= st_context(ctx
);
3321 struct pipe_context
*pipe
= st
->pipe
;
3323 pipe
->delete_image_handle(pipe
, handle
);
3328 st_MakeImageHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3329 GLenum access
, bool resident
)
3331 struct st_context
*st
= st_context(ctx
);
3332 struct pipe_context
*pipe
= st
->pipe
;
3334 pipe
->make_image_handle_resident(pipe
, handle
, access
, resident
);
3339 st_init_texture_functions(struct dd_function_table
*functions
)
3341 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
3342 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
3343 functions
->TexImage
= st_TexImage
;
3344 functions
->TexSubImage
= st_TexSubImage
;
3345 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
3346 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
3347 functions
->GenerateMipmap
= st_generate_mipmap
;
3349 functions
->GetTexSubImage
= st_GetTexSubImage
;
3351 /* compressed texture functions */
3352 functions
->CompressedTexImage
= st_CompressedTexImage
;
3354 functions
->NewTextureObject
= st_NewTextureObject
;
3355 functions
->NewTextureImage
= st_NewTextureImage
;
3356 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
3357 functions
->DeleteTexture
= st_DeleteTextureObject
;
3358 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
3359 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
3360 functions
->MapTextureImage
= st_MapTextureImage
;
3361 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
3363 /* XXX Temporary until we can query pipe's texture sizes */
3364 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
3366 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
3367 functions
->TextureView
= st_TextureView
;
3368 functions
->ClearTexSubImage
= st_ClearTexSubImage
;
3370 functions
->TexParameter
= st_TexParameter
;
3372 /* bindless functions */
3373 functions
->NewTextureHandle
= st_NewTextureHandle
;
3374 functions
->DeleteTextureHandle
= st_DeleteTextureHandle
;
3375 functions
->MakeTextureHandleResident
= st_MakeTextureHandleResident
;
3376 functions
->NewImageHandle
= st_NewImageHandle
;
3377 functions
->DeleteImageHandle
= st_DeleteImageHandle
;
3378 functions
->MakeImageHandleResident
= st_MakeImageHandleResident
;
3380 /* external object functions */
3381 functions
->SetTextureStorageForMemoryObject
= st_SetTextureStorageForMemoryObject
;