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 "main/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/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
);
231 st_compressed_format_fallback(struct st_context
*st
, mesa_format format
)
233 if (format
== MESA_FORMAT_ETC1_RGB8
)
234 return !st
->has_etc1
;
236 if (_mesa_is_format_etc2(format
))
237 return !st
->has_etc2
;
239 if (_mesa_is_format_astc_2d(format
))
240 return !st
->has_astc_2d_ldr
;
247 compressed_tex_fallback_allocate(struct st_context
*st
,
248 struct st_texture_image
*stImage
)
250 struct gl_texture_image
*texImage
= &stImage
->base
;
252 if (!st_compressed_format_fallback(st
, texImage
->TexFormat
))
255 if (stImage
->compressed_data
)
256 free(stImage
->compressed_data
);
258 unsigned data_size
= _mesa_format_image_size(texImage
->TexFormat
,
263 stImage
->compressed_data
=
264 malloc(data_size
* _mesa_num_tex_faces(texImage
->TexObject
->Target
));
268 /** called via ctx->Driver.MapTextureImage() */
270 st_MapTextureImage(struct gl_context
*ctx
,
271 struct gl_texture_image
*texImage
,
272 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
274 GLubyte
**mapOut
, GLint
*rowStrideOut
)
276 struct st_context
*st
= st_context(ctx
);
277 struct st_texture_image
*stImage
= st_texture_image(texImage
);
279 struct pipe_transfer
*transfer
;
281 /* Check for unexpected flags */
282 assert((mode
& ~(GL_MAP_READ_BIT
|
284 GL_MAP_INVALIDATE_RANGE_BIT
)) == 0);
286 const enum pipe_transfer_usage transfer_flags
=
287 st_access_flags_to_transfer_flags(mode
, false);
289 map
= st_texture_image_map(st
, stImage
, transfer_flags
, x
, y
, slice
, w
, h
, 1,
292 if (st_compressed_format_fallback(st
, texImage
->TexFormat
)) {
293 /* Some compressed formats don't have to be supported by drivers,
294 * and st/mesa transparently handles decompression on upload (Unmap),
295 * so that drivers don't see the compressed formats.
297 * We store the compressed data (it's needed for glGetCompressedTex-
298 * Image and image copies in OES_copy_image).
300 unsigned z
= transfer
->box
.z
;
301 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
303 unsigned blk_w
, blk_h
;
304 _mesa_get_format_block_size(texImage
->TexFormat
, &blk_w
, &blk_h
);
306 unsigned y_blocks
= DIV_ROUND_UP(texImage
->Height2
, blk_h
);
307 unsigned stride
= *rowStrideOut
= itransfer
->temp_stride
=
308 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width2
);
309 unsigned block_size
= _mesa_get_format_bytes(texImage
->TexFormat
);
311 *mapOut
= itransfer
->temp_data
=
312 stImage
->compressed_data
+
313 (z
* y_blocks
+ (y
/ blk_h
)) * stride
+
314 (x
/ blk_w
) * block_size
;
315 itransfer
->map
= map
;
318 /* supported mapping */
320 *rowStrideOut
= transfer
->stride
;
330 /** called via ctx->Driver.UnmapTextureImage() */
332 st_UnmapTextureImage(struct gl_context
*ctx
,
333 struct gl_texture_image
*texImage
,
336 struct st_context
*st
= st_context(ctx
);
337 struct st_texture_image
*stImage
= st_texture_image(texImage
);
339 if (st_compressed_format_fallback(st
, texImage
->TexFormat
)) {
340 /* Decompress the compressed image on upload if the driver doesn't
341 * support the compressed format. */
342 unsigned z
= slice
+ stImage
->base
.Face
;
343 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
344 struct pipe_transfer
*transfer
= itransfer
->transfer
;
346 assert(z
== transfer
->box
.z
);
348 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
349 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
350 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
351 itransfer
->temp_data
,
352 itransfer
->temp_stride
,
354 transfer
->box
.height
);
355 } else if (_mesa_is_format_etc2(texImage
->TexFormat
)) {
356 bool bgra
= stImage
->pt
->format
== PIPE_FORMAT_B8G8R8A8_SRGB
;
357 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
358 itransfer
->temp_data
,
359 itransfer
->temp_stride
,
360 transfer
->box
.width
, transfer
->box
.height
,
363 } else if (_mesa_is_format_astc_2d(texImage
->TexFormat
)) {
364 _mesa_unpack_astc_2d_ldr(itransfer
->map
, transfer
->stride
,
365 itransfer
->temp_data
,
366 itransfer
->temp_stride
,
367 transfer
->box
.width
, transfer
->box
.height
,
368 texImage
->TexFormat
);
370 unreachable("unexpected format for a compressed format fallback");
374 itransfer
->temp_data
= NULL
;
375 itransfer
->temp_stride
= 0;
379 st_texture_image_unmap(st
, stImage
, slice
);
384 * Return default texture resource binding bitmask for the given format.
387 default_bindings(struct st_context
*st
, enum pipe_format format
)
389 struct pipe_screen
*screen
= st
->pipe
->screen
;
390 const unsigned target
= PIPE_TEXTURE_2D
;
393 if (util_format_is_depth_or_stencil(format
))
394 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
396 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
398 if (screen
->is_format_supported(screen
, format
, target
, 0, 0, bindings
))
402 format
= util_format_linear(format
);
404 if (screen
->is_format_supported(screen
, format
, target
, 0, 0, bindings
))
407 return PIPE_BIND_SAMPLER_VIEW
;
413 * Given the size of a mipmap image, try to compute the size of the level=0
416 * Note that this isn't always accurate for odd-sized, non-POW textures.
417 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
419 * \return GL_TRUE for success, GL_FALSE for failure
422 guess_base_level_size(GLenum target
,
423 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
424 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
431 /* Guess the size of the base level.
432 * Depending on the image's size, we can't always make a guess here.
436 case GL_TEXTURE_1D_ARRAY
:
441 case GL_TEXTURE_2D_ARRAY
:
442 /* We can't make a good guess here, because the base level dimensions
445 if (width
== 1 || height
== 1) {
452 case GL_TEXTURE_CUBE_MAP
:
453 case GL_TEXTURE_CUBE_MAP_ARRAY
:
459 /* We can't make a good guess here, because the base level dimensions
462 if (width
== 1 || height
== 1 || depth
== 1) {
470 case GL_TEXTURE_RECTANGLE
:
487 * Try to determine whether we should allocate memory for a full texture
488 * mipmap. The problem is when we get a glTexImage(level=0) call, we
489 * can't immediately know if other mipmap levels are coming next. Here
490 * we try to guess whether to allocate memory for a mipmap or just the
493 * If we guess incorrectly here we'll later reallocate the right amount of
494 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
496 * \param stObj the texture object we're going to allocate memory for.
497 * \param stImage describes the incoming image which we need to store.
500 allocate_full_mipmap(const struct st_texture_object
*stObj
,
501 const struct st_texture_image
*stImage
)
503 switch (stObj
->base
.Target
) {
504 case GL_TEXTURE_RECTANGLE_NV
:
505 case GL_TEXTURE_BUFFER
:
506 case GL_TEXTURE_EXTERNAL_OES
:
507 case GL_TEXTURE_2D_MULTISAMPLE
:
508 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
509 /* these texture types cannot be mipmapped */
513 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
516 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
517 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
518 /* depth/stencil textures are seldom mipmapped */
521 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
524 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
525 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
526 /* not a mipmap minification filter */
529 /* If the following sequence of GL calls is used:
530 * glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, ...
531 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
533 * we would needlessly allocate a mipmapped texture, because the initial
534 * MinFilter is GL_NEAREST_MIPMAP_LINEAR. Catch this case and don't
535 * allocate a mipmapped texture by default. This may cause texture
536 * reallocation later, but GL_NEAREST_MIPMAP_LINEAR is pretty rare.
538 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST_MIPMAP_LINEAR
)
541 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
542 /* 3D textures are seldom mipmapped */
550 * Try to allocate a pipe_resource object for the given st_texture_object.
552 * We use the given st_texture_image as a clue to determine the size of the
553 * mipmap image at level=0.
555 * \return GL_TRUE for success, GL_FALSE if out of memory.
558 guess_and_alloc_texture(struct st_context
*st
,
559 struct st_texture_object
*stObj
,
560 const struct st_texture_image
*stImage
)
562 const struct gl_texture_image
*firstImage
;
563 GLuint lastLevel
, width
, height
, depth
;
566 uint16_t ptHeight
, ptDepth
, ptLayers
;
567 enum pipe_format fmt
;
568 bool guessed_box
= false;
570 DBG("%s\n", __func__
);
574 /* If a base level image with compatible size exists, use that as our guess.
576 firstImage
= _mesa_base_tex_image(&stObj
->base
);
578 firstImage
->Width2
> 0 &&
579 firstImage
->Height2
> 0 &&
580 firstImage
->Depth2
> 0 &&
581 guess_base_level_size(stObj
->base
.Target
,
586 &width
, &height
, &depth
)) {
587 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
588 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
589 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
594 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
595 stImage
->base
.Width2
,
596 stImage
->base
.Height2
,
597 stImage
->base
.Depth2
,
599 &width
, &height
, &depth
);
602 /* we can't determine the image size at level=0 */
603 /* this is not an out of memory error */
607 /* At this point, (width x height x depth) is the expected size of
608 * the level=0 mipmap image.
611 /* Guess a reasonable value for lastLevel. With OpenGL we have no
612 * idea how many mipmap levels will be in a texture until we start
613 * to render with it. Make an educated guess here but be prepared
614 * to re-allocating a texture buffer with space for more (or fewer)
615 * mipmap levels later.
617 if (allocate_full_mipmap(stObj
, stImage
)) {
618 /* alloc space for a full mipmap */
619 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
620 width
, height
, depth
) - 1;
623 /* only alloc space for a single mipmap level */
627 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
629 bindings
= default_bindings(st
, fmt
);
631 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
632 width
, height
, depth
,
633 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
635 stObj
->pt
= st_texture_create(st
,
636 gl_target_to_pipe(stObj
->base
.Target
),
645 stObj
->lastLevel
= lastLevel
;
647 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
649 return stObj
->pt
!= NULL
;
654 * Called via ctx->Driver.AllocTextureImageBuffer().
655 * If the texture object/buffer already has space for the indicated image,
656 * we're done. Otherwise, allocate memory for the new texture image.
659 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
660 struct gl_texture_image
*texImage
)
662 struct st_context
*st
= st_context(ctx
);
663 struct st_texture_image
*stImage
= st_texture_image(texImage
);
664 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
665 const GLuint level
= texImage
->Level
;
666 GLuint width
= texImage
->Width
;
667 GLuint height
= texImage
->Height
;
668 GLuint depth
= texImage
->Depth
;
670 DBG("%s\n", __func__
);
672 assert(!stImage
->pt
); /* xxx this might be wrong */
674 stObj
->needs_validation
= true;
676 compressed_tex_fallback_allocate(st
, stImage
);
678 /* Look if the parent texture object has space for this image */
680 level
<= stObj
->pt
->last_level
&&
681 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
682 /* this image will fit in the existing texture object's memory */
683 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
687 /* The parent texture object does not have space for this image */
689 pipe_resource_reference(&stObj
->pt
, NULL
);
690 st_texture_release_all_sampler_views(st
, stObj
);
692 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
693 /* Probably out of memory.
694 * Try flushing any pending rendering, then retry.
697 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
698 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
704 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
705 /* The image will live in the object's mipmap memory */
706 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
711 /* Create a new, temporary texture/resource/buffer to hold this
712 * one texture image. Note that when we later access this image
713 * (either for mapping or copying) we'll want to always specify
714 * mipmap level=0, even if the image represents some other mipmap
717 enum pipe_format format
=
718 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
719 GLuint bindings
= default_bindings(st
, format
);
721 uint16_t ptHeight
, ptDepth
, ptLayers
;
723 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
724 width
, height
, depth
,
725 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
727 stImage
->pt
= st_texture_create(st
,
728 gl_target_to_pipe(stObj
->base
.Target
),
736 return stImage
->pt
!= NULL
;
742 * Preparation prior to glTexImage. Basically check the 'surface_based'
743 * field and switch to a "normal" tex image if necessary.
746 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
747 GLenum format
, GLenum type
)
749 struct gl_texture_object
*texObj
= texImage
->TexObject
;
750 struct st_texture_object
*stObj
= st_texture_object(texObj
);
752 /* switch to "normal" */
753 if (stObj
->surface_based
) {
754 const GLenum target
= texObj
->Target
;
755 const GLuint level
= texImage
->Level
;
756 mesa_format texFormat
;
758 assert(!st_texture_image(texImage
)->pt
);
759 _mesa_clear_texture_object(ctx
, texObj
, texImage
);
760 stObj
->layer_override
= -1;
761 stObj
->level_override
= -1;
762 pipe_resource_reference(&stObj
->pt
, NULL
);
764 /* oops, need to init this image again */
765 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
766 texImage
->InternalFormat
, format
,
769 _mesa_init_teximage_fields(ctx
, texImage
,
770 texImage
->Width
, texImage
->Height
,
771 texImage
->Depth
, texImage
->Border
,
772 texImage
->InternalFormat
, texFormat
);
774 stObj
->surface_based
= GL_FALSE
;
780 * Return a writemask for the gallium blit. The parameters can be base
781 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
784 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
787 case GL_DEPTH_STENCIL
:
789 case GL_DEPTH_STENCIL
:
791 case GL_DEPTH_COMPONENT
:
793 case GL_STENCIL_INDEX
:
800 case GL_DEPTH_COMPONENT
:
802 case GL_DEPTH_STENCIL
:
803 case GL_DEPTH_COMPONENT
:
810 case GL_STENCIL_INDEX
:
812 case GL_DEPTH_STENCIL
:
813 case GL_STENCIL_INDEX
:
821 return PIPE_MASK_RGBA
;
826 * Converts format to a format with the same components, types
827 * and sizes, but with the components in RGBA order.
829 static enum pipe_format
830 unswizzle_format(enum pipe_format format
)
834 case PIPE_FORMAT_B8G8R8A8_UNORM
:
835 case PIPE_FORMAT_A8R8G8B8_UNORM
:
836 case PIPE_FORMAT_A8B8G8R8_UNORM
:
837 return PIPE_FORMAT_R8G8B8A8_UNORM
;
839 case PIPE_FORMAT_B10G10R10A2_UNORM
:
840 return PIPE_FORMAT_R10G10B10A2_UNORM
;
842 case PIPE_FORMAT_B10G10R10A2_SNORM
:
843 return PIPE_FORMAT_R10G10B10A2_SNORM
;
845 case PIPE_FORMAT_B10G10R10A2_UINT
:
846 return PIPE_FORMAT_R10G10B10A2_UINT
;
855 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
857 static enum pipe_format
858 alpha_to_red(enum pipe_format format
)
862 case PIPE_FORMAT_A8_UNORM
:
863 return PIPE_FORMAT_R8_UNORM
;
864 case PIPE_FORMAT_A8_SNORM
:
865 return PIPE_FORMAT_R8_SNORM
;
866 case PIPE_FORMAT_A8_UINT
:
867 return PIPE_FORMAT_R8_UINT
;
868 case PIPE_FORMAT_A8_SINT
:
869 return PIPE_FORMAT_R8_SINT
;
871 case PIPE_FORMAT_A16_UNORM
:
872 return PIPE_FORMAT_R16_UNORM
;
873 case PIPE_FORMAT_A16_SNORM
:
874 return PIPE_FORMAT_R16_SNORM
;
875 case PIPE_FORMAT_A16_UINT
:
876 return PIPE_FORMAT_R16_UINT
;
877 case PIPE_FORMAT_A16_SINT
:
878 return PIPE_FORMAT_R16_SINT
;
879 case PIPE_FORMAT_A16_FLOAT
:
880 return PIPE_FORMAT_R16_FLOAT
;
882 case PIPE_FORMAT_A32_UINT
:
883 return PIPE_FORMAT_R32_UINT
;
884 case PIPE_FORMAT_A32_SINT
:
885 return PIPE_FORMAT_R32_SINT
;
886 case PIPE_FORMAT_A32_FLOAT
:
887 return PIPE_FORMAT_R32_FLOAT
;
896 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
898 static enum pipe_format
899 red_alpha_to_red_green(enum pipe_format format
)
903 case PIPE_FORMAT_R8A8_UNORM
:
904 return PIPE_FORMAT_R8G8_UNORM
;
905 case PIPE_FORMAT_R8A8_SNORM
:
906 return PIPE_FORMAT_R8G8_SNORM
;
907 case PIPE_FORMAT_R8A8_UINT
:
908 return PIPE_FORMAT_R8G8_UINT
;
909 case PIPE_FORMAT_R8A8_SINT
:
910 return PIPE_FORMAT_R8G8_SINT
;
912 case PIPE_FORMAT_R16A16_UNORM
:
913 return PIPE_FORMAT_R16G16_UNORM
;
914 case PIPE_FORMAT_R16A16_SNORM
:
915 return PIPE_FORMAT_R16G16_SNORM
;
916 case PIPE_FORMAT_R16A16_UINT
:
917 return PIPE_FORMAT_R16G16_UINT
;
918 case PIPE_FORMAT_R16A16_SINT
:
919 return PIPE_FORMAT_R16G16_SINT
;
920 case PIPE_FORMAT_R16A16_FLOAT
:
921 return PIPE_FORMAT_R16G16_FLOAT
;
923 case PIPE_FORMAT_R32A32_UINT
:
924 return PIPE_FORMAT_R32G32_UINT
;
925 case PIPE_FORMAT_R32A32_SINT
:
926 return PIPE_FORMAT_R32G32_SINT
;
927 case PIPE_FORMAT_R32A32_FLOAT
:
928 return PIPE_FORMAT_R32G32_FLOAT
;
937 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
939 static enum pipe_format
940 luminance_alpha_to_red_green(enum pipe_format format
)
944 case PIPE_FORMAT_L8A8_UNORM
:
945 return PIPE_FORMAT_R8G8_UNORM
;
946 case PIPE_FORMAT_L8A8_SNORM
:
947 return PIPE_FORMAT_R8G8_SNORM
;
948 case PIPE_FORMAT_L8A8_UINT
:
949 return PIPE_FORMAT_R8G8_UINT
;
950 case PIPE_FORMAT_L8A8_SINT
:
951 return PIPE_FORMAT_R8G8_SINT
;
953 case PIPE_FORMAT_L16A16_UNORM
:
954 return PIPE_FORMAT_R16G16_UNORM
;
955 case PIPE_FORMAT_L16A16_SNORM
:
956 return PIPE_FORMAT_R16G16_SNORM
;
957 case PIPE_FORMAT_L16A16_UINT
:
958 return PIPE_FORMAT_R16G16_UINT
;
959 case PIPE_FORMAT_L16A16_SINT
:
960 return PIPE_FORMAT_R16G16_SINT
;
961 case PIPE_FORMAT_L16A16_FLOAT
:
962 return PIPE_FORMAT_R16G16_FLOAT
;
964 case PIPE_FORMAT_L32A32_UINT
:
965 return PIPE_FORMAT_R32G32_UINT
;
966 case PIPE_FORMAT_L32A32_SINT
:
967 return PIPE_FORMAT_R32G32_SINT
;
968 case PIPE_FORMAT_L32A32_FLOAT
:
969 return PIPE_FORMAT_R32G32_FLOAT
;
978 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
981 format_is_alpha(enum pipe_format format
)
983 const struct util_format_description
*desc
= util_format_description(format
);
985 if (desc
->nr_channels
== 1 &&
986 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
987 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
988 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
989 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
997 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
1000 format_is_red(enum pipe_format format
)
1002 const struct util_format_description
*desc
= util_format_description(format
);
1004 if (desc
->nr_channels
== 1 &&
1005 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
1006 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
1007 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
1008 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1016 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
1019 format_is_luminance(enum pipe_format format
)
1021 const struct util_format_description
*desc
= util_format_description(format
);
1023 if (desc
->nr_channels
== 1 &&
1024 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
1025 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
1026 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
1027 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1034 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
1037 format_is_red_alpha(enum pipe_format format
)
1039 const struct util_format_description
*desc
= util_format_description(format
);
1041 if (desc
->nr_channels
== 2 &&
1042 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
1043 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
1044 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
1045 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
1053 format_is_swizzled_rgba(enum pipe_format format
)
1055 const struct util_format_description
*desc
= util_format_description(format
);
1057 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
1058 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
1059 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
1060 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
1069 unsigned char swizzle
[4];
1070 enum pipe_format format
;
1073 static const struct format_table table_8888_unorm
[] = {
1074 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
1075 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
1076 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
1077 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
1080 static const struct format_table table_1010102_unorm
[] = {
1081 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
1082 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
1085 static const struct format_table table_1010102_snorm
[] = {
1086 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
1087 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
1090 static const struct format_table table_1010102_uint
[] = {
1091 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
1092 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
1095 static enum pipe_format
1096 swizzle_format(enum pipe_format format
, const int * const swizzle
)
1101 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1102 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1103 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1104 case PIPE_FORMAT_A8B8G8R8_UNORM
:
1105 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
1106 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
1107 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
1108 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
1109 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
1110 return table_8888_unorm
[i
].format
;
1114 case PIPE_FORMAT_R10G10B10A2_UNORM
:
1115 case PIPE_FORMAT_B10G10R10A2_UNORM
:
1116 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
1117 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
1118 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
1119 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
1120 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
1121 return table_1010102_unorm
[i
].format
;
1125 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1126 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1127 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1128 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1129 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1130 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1131 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1132 return table_1010102_snorm
[i
].format
;
1136 case PIPE_FORMAT_R10G10B10A2_UINT
:
1137 case PIPE_FORMAT_B10G10R10A2_UINT
:
1138 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1139 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1140 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1141 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1142 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1143 return table_1010102_uint
[i
].format
;
1151 return PIPE_FORMAT_NONE
;
1155 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1157 enum pipe_format src
= *src_format
;
1158 enum pipe_format dst
= *dst_format
;
1160 /* Note: dst_format has already been transformed from luminance/intensity
1161 * to red when this function is called. The source format will never
1162 * be an intensity format, because GL_INTENSITY is not a legal value
1163 * for the format parameter in glTex(Sub)Image(). */
1165 if (format_is_alpha(src
)) {
1166 if (!format_is_alpha(dst
))
1169 src
= alpha_to_red(src
);
1170 dst
= alpha_to_red(dst
);
1171 } else if (format_is_luminance(src
)) {
1172 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1175 src
= util_format_luminance_to_red(src
);
1176 } else if (util_format_is_luminance_alpha(src
)) {
1177 src
= luminance_alpha_to_red_green(src
);
1179 if (format_is_red_alpha(dst
)) {
1180 dst
= red_alpha_to_red_green(dst
);
1181 } else if (!format_is_red(dst
))
1183 } else if (format_is_swizzled_rgba(src
)) {
1184 const struct util_format_description
*src_desc
= util_format_description(src
);
1185 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1189 /* Make sure the format is an RGBA and not an RGBX format */
1190 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1193 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1196 for (i
= 0; i
< 4; i
++)
1197 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1199 dst
= swizzle_format(dst
, swizzle
);
1200 if (dst
== PIPE_FORMAT_NONE
)
1203 src
= unswizzle_format(src
);
1212 try_pbo_upload_common(struct gl_context
*ctx
,
1213 struct pipe_surface
*surface
,
1214 const struct st_pbo_addresses
*addr
,
1215 enum pipe_format src_format
)
1217 struct st_context
*st
= st_context(ctx
);
1218 struct cso_context
*cso
= st
->cso_context
;
1219 struct pipe_context
*pipe
= st
->pipe
;
1220 bool success
= false;
1223 fs
= st_pbo_get_upload_fs(st
, src_format
, surface
->format
);
1227 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1228 CSO_BIT_VERTEX_ELEMENTS
|
1229 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1230 CSO_BIT_FRAMEBUFFER
|
1233 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1234 CSO_BIT_RASTERIZER
|
1235 CSO_BIT_STREAM_OUTPUTS
|
1236 (st
->active_queries
? CSO_BIT_PAUSE_QUERIES
: 0) |
1237 CSO_BIT_SAMPLE_MASK
|
1238 CSO_BIT_MIN_SAMPLES
|
1239 CSO_BIT_RENDER_CONDITION
|
1240 CSO_BITS_ALL_SHADERS
));
1241 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1243 cso_set_sample_mask(cso
, ~0);
1244 cso_set_min_samples(cso
, 1);
1245 cso_set_render_condition(cso
, NULL
, FALSE
, 0);
1247 /* Set up the sampler_view */
1249 struct pipe_sampler_view templ
;
1250 struct pipe_sampler_view
*sampler_view
;
1252 memset(&templ
, 0, sizeof(templ
));
1253 templ
.target
= PIPE_BUFFER
;
1254 templ
.format
= src_format
;
1255 templ
.u
.buf
.offset
= addr
->first_element
* addr
->bytes_per_pixel
;
1256 templ
.u
.buf
.size
= (addr
->last_element
- addr
->first_element
+ 1) *
1257 addr
->bytes_per_pixel
;
1258 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1259 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1260 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1261 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1263 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1264 if (sampler_view
== NULL
)
1267 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1269 pipe_sampler_view_reference(&sampler_view
, NULL
);
1272 /* Framebuffer_state */
1274 struct pipe_framebuffer_state fb
;
1275 memset(&fb
, 0, sizeof(fb
));
1276 fb
.width
= surface
->width
;
1277 fb
.height
= surface
->height
;
1279 fb
.cbufs
[0] = surface
;
1281 cso_set_framebuffer(cso
, &fb
);
1284 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1287 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1289 /* Depth/stencil/alpha state */
1291 struct pipe_depth_stencil_alpha_state dsa
;
1292 memset(&dsa
, 0, sizeof(dsa
));
1293 cso_set_depth_stencil_alpha(cso
, &dsa
);
1296 /* Set up the fragment shader */
1297 cso_set_fragment_shader_handle(cso
, fs
);
1299 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1302 cso_restore_state(cso
);
1303 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1310 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1311 struct gl_texture_image
*texImage
,
1312 GLenum format
, GLenum type
,
1313 enum pipe_format dst_format
,
1314 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1315 GLint width
, GLint height
, GLint depth
,
1317 const struct gl_pixelstore_attrib
*unpack
)
1319 struct st_context
*st
= st_context(ctx
);
1320 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1321 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1322 struct pipe_resource
*texture
= stImage
->pt
;
1323 struct pipe_context
*pipe
= st
->pipe
;
1324 struct pipe_screen
*screen
= pipe
->screen
;
1325 struct pipe_surface
*surface
= NULL
;
1326 struct st_pbo_addresses addr
;
1327 enum pipe_format src_format
;
1328 const struct util_format_description
*desc
;
1329 GLenum gl_target
= texImage
->TexObject
->Target
;
1332 if (!st
->pbo
.upload_enabled
)
1335 /* From now on, we need the gallium representation of dimensions. */
1336 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1343 if (depth
!= 1 && !st
->pbo
.layers
)
1346 /* Choose the source format. Initially, we do so without checking driver
1347 * support at all because of the remapping we later perform and because
1348 * at least the Radeon driver actually supports some formats for texture
1349 * buffers which it doesn't support for regular textures. */
1350 src_format
= st_choose_matching_format(st
, 0, format
, type
,
1356 src_format
= util_format_linear(src_format
);
1357 desc
= util_format_description(src_format
);
1359 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1362 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1365 if (st
->pbo
.rgba_only
) {
1366 enum pipe_format orig_dst_format
= dst_format
;
1368 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1372 if (dst_format
!= orig_dst_format
&&
1373 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1374 0, PIPE_BIND_RENDER_TARGET
)) {
1380 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0, 0,
1381 PIPE_BIND_SAMPLER_VIEW
)) {
1385 /* Compute buffer addresses */
1386 addr
.xoffset
= xoffset
;
1387 addr
.yoffset
= yoffset
;
1389 addr
.height
= height
;
1391 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1393 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1397 /* Set up the surface */
1399 unsigned level
= stObj
->pt
!= stImage
->pt
1400 ? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1401 unsigned max_layer
= util_max_layer(texture
, level
);
1403 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1405 struct pipe_surface templ
;
1406 memset(&templ
, 0, sizeof(templ
));
1407 templ
.format
= dst_format
;
1408 templ
.u
.tex
.level
= level
;
1409 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1410 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1412 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1417 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1419 pipe_surface_reference(&surface
, NULL
);
1426 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1427 struct gl_texture_image
*texImage
,
1428 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1429 GLint width
, GLint height
, GLint depth
,
1430 GLenum format
, GLenum type
, const void *pixels
,
1431 const struct gl_pixelstore_attrib
*unpack
)
1433 struct st_context
*st
= st_context(ctx
);
1434 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1435 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1436 struct pipe_context
*pipe
= st
->pipe
;
1437 struct pipe_screen
*screen
= pipe
->screen
;
1438 struct pipe_resource
*dst
= stImage
->pt
;
1439 struct pipe_resource
*src
= NULL
;
1440 struct pipe_resource src_templ
;
1441 struct pipe_transfer
*transfer
;
1442 struct pipe_blit_info blit
;
1443 enum pipe_format src_format
, dst_format
;
1444 mesa_format mesa_src_format
;
1445 GLenum gl_target
= texImage
->TexObject
->Target
;
1448 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1449 unsigned dst_level
= 0;
1450 bool throttled
= false;
1452 st_flush_bitmap_cache(st
);
1453 st_invalidate_readpix_cache(st
);
1455 if (stObj
->pt
== stImage
->pt
)
1456 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1458 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1459 !_mesa_is_format_astc_2d(texImage
->TexFormat
) &&
1460 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1465 /* Try texture_subdata, which should be the fastest memcpy path. */
1467 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1468 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1469 texImage
->TexFormat
, format
, type
,
1471 struct pipe_box box
;
1472 unsigned stride
, layer_stride
;
1475 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1476 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1478 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1481 /* Convert to Gallium coordinates. */
1482 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1487 layer_stride
= stride
;
1490 util_throttle_memory_usage(pipe
, &st
->throttle
,
1491 width
* height
* depth
*
1492 util_format_get_blocksize(dst
->format
));
1494 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1495 pipe
->texture_subdata(pipe
, dst
, dst_level
, 0,
1496 &box
, data
, stride
, layer_stride
);
1500 if (!st
->prefer_blit_based_texture_transfer
) {
1504 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1505 * blit implementation in some drivers. */
1506 if (format
== GL_DEPTH_STENCIL
) {
1510 /* If the base internal format and the texture format don't match,
1511 * we can't use blit-based TexSubImage. */
1512 if (texImage
->_BaseFormat
!=
1513 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1518 /* See if the destination format is supported. */
1519 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1520 bind
= PIPE_BIND_DEPTH_STENCIL
;
1522 bind
= PIPE_BIND_RENDER_TARGET
;
1524 /* For luminance and intensity, only the red channel is stored
1525 * in the destination. */
1526 dst_format
= util_format_linear(dst
->format
);
1527 dst_format
= util_format_luminance_to_red(dst_format
);
1528 dst_format
= util_format_intensity_to_red(dst_format
);
1531 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1532 dst
->nr_samples
, dst
->nr_storage_samples
,
1537 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1538 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1539 xoffset
, yoffset
, zoffset
,
1540 width
, height
, depth
, pixels
, unpack
))
1544 /* See if the texture format already matches the format and type,
1545 * in which case the memcpy-based fast path will likely be used and
1546 * we don't have to blit. */
1547 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1548 type
, unpack
->SwapBytes
, NULL
)) {
1552 /* Choose the source format. */
1553 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1554 format
, type
, unpack
->SwapBytes
);
1559 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1561 /* There is no reason to do this if we cannot use memcpy for the temporary
1562 * source texture at least. This also takes transfer ops into account,
1564 if (!_mesa_texstore_can_use_memcpy(ctx
,
1565 _mesa_get_format_base_format(mesa_src_format
),
1566 mesa_src_format
, format
, type
, unpack
)) {
1570 /* TexSubImage only sets a single cubemap face. */
1571 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1572 gl_target
= GL_TEXTURE_2D
;
1574 /* TexSubImage can specify subsets of cube map array faces
1575 * so we need to upload via 2D array instead */
1576 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1577 gl_target
= GL_TEXTURE_2D_ARRAY
;
1580 /* Initialize the source texture description. */
1581 memset(&src_templ
, 0, sizeof(src_templ
));
1582 src_templ
.target
= gl_target_to_pipe(gl_target
);
1583 src_templ
.format
= src_format
;
1584 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1585 src_templ
.usage
= PIPE_USAGE_STAGING
;
1587 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1588 &src_templ
.width0
, &src_templ
.height0
,
1589 &src_templ
.depth0
, &src_templ
.array_size
);
1591 /* Check for NPOT texture support. */
1592 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1593 (!util_is_power_of_two_or_zero(src_templ
.width0
) ||
1594 !util_is_power_of_two_or_zero(src_templ
.height0
) ||
1595 !util_is_power_of_two_or_zero(src_templ
.depth0
))) {
1599 util_throttle_memory_usage(pipe
, &st
->throttle
,
1600 width
* height
* depth
*
1601 util_format_get_blocksize(src_templ
.format
));
1604 /* Create the source texture. */
1605 src
= screen
->resource_create(screen
, &src_templ
);
1610 /* Map source pixels. */
1611 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1612 format
, type
, pixels
, unpack
,
1615 /* This is a GL error. */
1616 pipe_resource_reference(&src
, NULL
);
1620 /* From now on, we need the gallium representation of dimensions. */
1621 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1628 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1629 width
, height
, depth
, &transfer
);
1631 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1632 pipe_resource_reference(&src
, NULL
);
1636 /* Upload pixels (just memcpy). */
1638 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1641 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1642 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1643 /* 1D array textures.
1644 * We need to convert gallium coords to GL coords.
1646 void *src
= _mesa_image_address2d(unpack
, pixels
,
1647 width
, depth
, format
,
1649 memcpy(map
, src
, bytesPerRow
);
1652 ubyte
*slice_map
= map
;
1654 for (row
= 0; row
< (unsigned) height
; row
++) {
1655 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1656 width
, height
, format
,
1657 type
, slice
, row
, 0);
1658 memcpy(slice_map
, src
, bytesPerRow
);
1659 slice_map
+= transfer
->stride
;
1662 map
+= transfer
->layer_stride
;
1666 pipe_transfer_unmap(pipe
, transfer
);
1667 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1670 memset(&blit
, 0, sizeof(blit
));
1671 blit
.src
.resource
= src
;
1673 blit
.src
.format
= src_format
;
1674 blit
.dst
.resource
= dst
;
1675 blit
.dst
.level
= dst_level
;
1676 blit
.dst
.format
= dst_format
;
1677 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1678 blit
.dst
.box
.x
= xoffset
;
1679 blit
.dst
.box
.y
= yoffset
;
1680 blit
.dst
.box
.z
= zoffset
+ dstz
;
1681 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1682 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1683 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1684 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1685 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1686 blit
.scissor_enable
= FALSE
;
1688 st
->pipe
->blit(st
->pipe
, &blit
);
1690 pipe_resource_reference(&src
, NULL
);
1695 util_throttle_memory_usage(pipe
, &st
->throttle
,
1696 width
* height
* depth
*
1697 _mesa_get_format_bytes(texImage
->TexFormat
));
1699 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1700 width
, height
, depth
, format
, type
, pixels
,
1706 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1707 struct gl_texture_image
*texImage
,
1708 GLenum format
, GLenum type
, const void *pixels
,
1709 const struct gl_pixelstore_attrib
*unpack
)
1711 assert(dims
== 1 || dims
== 2 || dims
== 3);
1713 prep_teximage(ctx
, texImage
, format
, type
);
1715 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1718 /* allocate storage for texture data */
1719 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1720 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1724 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1725 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1726 format
, type
, pixels
, unpack
);
1731 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1732 struct gl_texture_image
*texImage
,
1733 GLint x
, GLint y
, GLint z
,
1734 GLsizei w
, GLsizei h
, GLsizei d
,
1735 GLenum format
, GLsizei imageSize
, const void *data
)
1737 struct st_context
*st
= st_context(ctx
);
1738 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1739 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1740 struct pipe_resource
*texture
= stImage
->pt
;
1741 struct pipe_context
*pipe
= st
->pipe
;
1742 struct pipe_screen
*screen
= pipe
->screen
;
1743 struct pipe_resource
*dst
= stImage
->pt
;
1744 struct pipe_surface
*surface
= NULL
;
1745 struct compressed_pixelstore store
;
1746 struct st_pbo_addresses addr
;
1747 enum pipe_format copy_format
;
1749 intptr_t buf_offset
;
1750 bool success
= false;
1752 /* Check basic pre-conditions for PBO upload */
1753 if (!st
->prefer_blit_based_texture_transfer
) {
1757 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1760 if (st_compressed_format_fallback(st
, texImage
->TexFormat
))
1767 if (!st
->pbo
.upload_enabled
||
1768 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1772 /* Choose the pipe format for the upload. */
1773 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1774 bw
= util_format_get_blockwidth(dst
->format
);
1775 bh
= util_format_get_blockheight(dst
->format
);
1777 switch (addr
.bytes_per_pixel
) {
1779 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1782 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1788 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0, 0,
1789 PIPE_BIND_SAMPLER_VIEW
)) {
1793 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1794 dst
->nr_samples
, dst
->nr_storage_samples
,
1795 PIPE_BIND_RENDER_TARGET
)) {
1799 /* Interpret the pixelstore settings. */
1800 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1801 &ctx
->Unpack
, &store
);
1802 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1803 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1805 /* Compute the offset into the buffer */
1806 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1808 if (buf_offset
% addr
.bytes_per_pixel
) {
1812 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1814 addr
.xoffset
= x
/ bw
;
1815 addr
.yoffset
= y
/ bh
;
1816 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1817 addr
.height
= store
.CopyRowsPerSlice
;
1819 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1820 addr
.image_height
= store
.TotalRowsPerSlice
;
1822 if (!st_pbo_addresses_setup(st
,
1823 st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1827 /* Set up the surface. */
1829 unsigned level
= stObj
->pt
!= stImage
->pt
1830 ? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1831 unsigned max_layer
= util_max_layer(texture
, level
);
1833 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1835 struct pipe_surface templ
;
1836 memset(&templ
, 0, sizeof(templ
));
1837 templ
.format
= copy_format
;
1838 templ
.u
.tex
.level
= level
;
1839 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1840 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1842 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1847 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1849 pipe_surface_reference(&surface
, NULL
);
1855 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1857 format
, imageSize
, data
);
1862 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1863 struct gl_texture_image
*texImage
,
1864 GLsizei imageSize
, const void *data
)
1866 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1868 /* only 2D and 3D compressed images are supported at this time */
1870 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1874 /* This is pretty simple, because unlike the general texstore path we don't
1875 * have to worry about the usual image unpacking or image transfer
1879 assert(texImage
->Width
> 0);
1880 assert(texImage
->Height
> 0);
1881 assert(texImage
->Depth
> 0);
1883 /* allocate storage for texture data */
1884 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1885 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1889 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1891 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1892 texImage
->TexFormat
,
1898 * Called via ctx->Driver.GetTexSubImage()
1900 * This uses a blit to copy the texture to a texture format which matches
1901 * the format and type combo and then a fast read-back is done using memcpy.
1902 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1903 * a format which matches the swizzling.
1905 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1907 * NOTE: Drivers usually do a blit to convert between tiled and linear
1908 * texture layouts during texture uploads/downloads, so the blit
1909 * we do here should be free in such cases.
1912 st_GetTexSubImage(struct gl_context
* ctx
,
1913 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1914 GLsizei width
, GLsizei height
, GLint depth
,
1915 GLenum format
, GLenum type
, void * pixels
,
1916 struct gl_texture_image
*texImage
)
1918 struct st_context
*st
= st_context(ctx
);
1919 struct pipe_context
*pipe
= st
->pipe
;
1920 struct pipe_screen
*screen
= pipe
->screen
;
1921 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1922 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1923 struct pipe_resource
*src
= stObj
->pt
;
1924 struct pipe_resource
*dst
= NULL
;
1925 struct pipe_resource dst_templ
;
1926 enum pipe_format dst_format
, src_format
;
1927 mesa_format mesa_format
;
1928 GLenum gl_target
= texImage
->TexObject
->Target
;
1929 enum pipe_texture_target pipe_target
;
1931 struct pipe_blit_info blit
;
1933 struct pipe_transfer
*tex_xfer
;
1935 boolean done
= FALSE
;
1937 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1938 !_mesa_is_format_astc_2d(texImage
->TexFormat
) &&
1939 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1941 st_flush_bitmap_cache(st
);
1943 if (!st
->prefer_blit_based_texture_transfer
&&
1944 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1945 /* Try to avoid the fallback if we're doing texture decompression here */
1949 /* Handle non-finalized textures. */
1950 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1954 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1955 * due to an incomplete stencil blit implementation in some drivers. */
1956 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1960 /* If the base internal format and the texture format don't match, we have
1961 * to fall back to _mesa_GetTexImage_sw. */
1962 if (texImage
->_BaseFormat
!=
1963 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1967 /* See if the texture format already matches the format and type,
1968 * in which case the memcpy-based fast path will be used. */
1969 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1970 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1974 /* Convert the source format to what is expected by GetTexImage
1975 * and see if it's supported.
1977 * This only applies to glGetTexImage:
1978 * - Luminance must be returned as (L,0,0,1).
1979 * - Luminance alpha must be returned as (L,0,0,A).
1980 * - Intensity must be returned as (I,0,0,1)
1982 if (stObj
->surface_based
)
1983 src_format
= util_format_linear(stObj
->surface_format
);
1985 src_format
= util_format_linear(src
->format
);
1986 src_format
= util_format_luminance_to_red(src_format
);
1987 src_format
= util_format_intensity_to_red(src_format
);
1990 !screen
->is_format_supported(screen
, src_format
, src
->target
,
1991 src
->nr_samples
, src
->nr_storage_samples
,
1992 PIPE_BIND_SAMPLER_VIEW
)) {
1996 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1997 bind
= PIPE_BIND_DEPTH_STENCIL
;
1999 bind
= PIPE_BIND_RENDER_TARGET
;
2001 /* GetTexImage only returns a single face for cubemaps. */
2002 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
2003 gl_target
= GL_TEXTURE_2D
;
2005 pipe_target
= gl_target_to_pipe(gl_target
);
2007 /* Choose the destination format by finding the best match
2008 * for the format+type combo. */
2009 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
2010 ctx
->Pack
.SwapBytes
);
2012 if (dst_format
== PIPE_FORMAT_NONE
) {
2013 GLenum dst_glformat
;
2015 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
2016 * where decompression with a blit is always preferred. */
2017 if (!util_format_is_compressed(src
->format
)) {
2021 /* Set the appropriate format for the decompressed texture.
2022 * Luminance and sRGB formats shouldn't appear here.*/
2023 switch (src_format
) {
2024 case PIPE_FORMAT_DXT1_RGB
:
2025 case PIPE_FORMAT_DXT1_RGBA
:
2026 case PIPE_FORMAT_DXT3_RGBA
:
2027 case PIPE_FORMAT_DXT5_RGBA
:
2028 case PIPE_FORMAT_RGTC1_UNORM
:
2029 case PIPE_FORMAT_RGTC2_UNORM
:
2030 case PIPE_FORMAT_ETC1_RGB8
:
2031 case PIPE_FORMAT_ETC2_RGB8
:
2032 case PIPE_FORMAT_ETC2_RGB8A1
:
2033 case PIPE_FORMAT_ETC2_RGBA8
:
2034 case PIPE_FORMAT_ASTC_4x4
:
2035 case PIPE_FORMAT_ASTC_5x4
:
2036 case PIPE_FORMAT_ASTC_5x5
:
2037 case PIPE_FORMAT_ASTC_6x5
:
2038 case PIPE_FORMAT_ASTC_6x6
:
2039 case PIPE_FORMAT_ASTC_8x5
:
2040 case PIPE_FORMAT_ASTC_8x6
:
2041 case PIPE_FORMAT_ASTC_8x8
:
2042 case PIPE_FORMAT_ASTC_10x5
:
2043 case PIPE_FORMAT_ASTC_10x6
:
2044 case PIPE_FORMAT_ASTC_10x8
:
2045 case PIPE_FORMAT_ASTC_10x10
:
2046 case PIPE_FORMAT_ASTC_12x10
:
2047 case PIPE_FORMAT_ASTC_12x12
:
2048 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
2049 dst_glformat
= GL_RGBA8
;
2051 case PIPE_FORMAT_RGTC1_SNORM
:
2052 case PIPE_FORMAT_RGTC2_SNORM
:
2053 if (!ctx
->Extensions
.EXT_texture_snorm
)
2055 dst_glformat
= GL_RGBA8_SNORM
;
2057 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
2058 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
2059 if (!ctx
->Extensions
.ARB_texture_float
)
2061 dst_glformat
= GL_RGBA32F
;
2063 case PIPE_FORMAT_ETC2_R11_UNORM
:
2064 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16_UNORM
,
2065 pipe_target
, 0, 0, bind
))
2067 dst_glformat
= GL_R16
;
2069 case PIPE_FORMAT_ETC2_R11_SNORM
:
2070 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16_SNORM
,
2071 pipe_target
, 0, 0, bind
))
2073 dst_glformat
= GL_R16_SNORM
;
2075 case PIPE_FORMAT_ETC2_RG11_UNORM
:
2076 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16G16_UNORM
,
2077 pipe_target
, 0, 0, bind
))
2079 dst_glformat
= GL_RG16
;
2081 case PIPE_FORMAT_ETC2_RG11_SNORM
:
2082 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16G16_SNORM
,
2083 pipe_target
, 0, 0, bind
))
2085 dst_glformat
= GL_RG16_SNORM
;
2092 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
2093 pipe_target
, 0, 0, bind
, FALSE
);
2095 if (dst_format
== PIPE_FORMAT_NONE
) {
2096 /* unable to get an rgba format!?! */
2101 /* create the destination texture of size (width X height X depth) */
2102 memset(&dst_templ
, 0, sizeof(dst_templ
));
2103 dst_templ
.target
= pipe_target
;
2104 dst_templ
.format
= dst_format
;
2105 dst_templ
.bind
= bind
;
2106 dst_templ
.usage
= PIPE_USAGE_STAGING
;
2108 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
2109 &dst_templ
.width0
, &dst_templ
.height0
,
2110 &dst_templ
.depth0
, &dst_templ
.array_size
);
2112 dst
= screen
->resource_create(screen
, &dst_templ
);
2117 /* From now on, we need the gallium representation of dimensions. */
2118 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2125 assert(texImage
->Face
== 0 ||
2126 texImage
->TexObject
->MinLayer
== 0 ||
2129 memset(&blit
, 0, sizeof(blit
));
2130 blit
.src
.resource
= src
;
2131 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2132 blit
.src
.format
= src_format
;
2133 blit
.dst
.resource
= dst
;
2135 blit
.dst
.format
= dst
->format
;
2136 blit
.src
.box
.x
= xoffset
;
2138 blit
.src
.box
.y
= yoffset
;
2140 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2142 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2143 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2144 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2145 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2146 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2147 blit
.scissor_enable
= FALSE
;
2149 /* blit/render/decompress */
2150 st
->pipe
->blit(st
->pipe
, &blit
);
2152 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2154 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2155 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2160 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2161 dims
= _mesa_get_texture_dimensions(gl_target
);
2163 /* copy/pack data into user buffer */
2164 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2165 ctx
->Pack
.SwapBytes
, NULL
)) {
2167 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2170 for (slice
= 0; slice
< depth
; slice
++) {
2171 ubyte
*slice_map
= map
;
2173 for (row
= 0; row
< height
; row
++) {
2174 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2175 width
, height
, format
, type
,
2178 memcpy(dest
, slice_map
, bytesPerRow
);
2180 slice_map
+= tex_xfer
->stride
;
2183 map
+= tex_xfer
->layer_stride
;
2187 /* format translation via floats */
2190 uint32_t dstMesaFormat
;
2191 int dstStride
, srcStride
;
2193 assert(util_format_is_compressed(src
->format
));
2195 rgba
= malloc(width
* height
* 4 * sizeof(GLfloat
));
2200 if (ST_DEBUG
& DEBUG_FALLBACK
)
2201 debug_printf("%s: fallback format translation\n", __func__
);
2203 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2204 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2205 srcStride
= 4 * width
* sizeof(GLfloat
);
2206 for (slice
= 0; slice
< depth
; slice
++) {
2207 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2208 width
, height
, format
, type
,
2211 /* get float[4] rgba row from surface */
2212 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, height
,
2215 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2216 rgba
, RGBA32_FLOAT
, srcStride
,
2217 width
, height
, NULL
);
2219 /* Handle byte swapping if required */
2220 if (ctx
->Pack
.SwapBytes
) {
2221 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
2222 width
, height
, dest
, dest
);
2225 map
+= tex_xfer
->layer_stride
;
2234 pipe_transfer_unmap(pipe
, tex_xfer
);
2236 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2237 pipe_resource_reference(&dst
, NULL
);
2241 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2242 width
, height
, depth
,
2243 format
, type
, pixels
, texImage
);
2249 * Do a CopyTexSubImage operation using a read transfer from the source,
2250 * a write transfer to the destination and get_tile()/put_tile() to access
2251 * the pixels/texels.
2253 * Note: srcY=0=TOP of renderbuffer
2256 fallback_copy_texsubimage(struct gl_context
*ctx
,
2257 struct st_renderbuffer
*strb
,
2258 struct st_texture_image
*stImage
,
2260 GLint destX
, GLint destY
, GLint slice
,
2261 GLint srcX
, GLint srcY
,
2262 GLsizei width
, GLsizei height
)
2264 struct st_context
*st
= st_context(ctx
);
2265 struct pipe_context
*pipe
= st
->pipe
;
2266 struct pipe_transfer
*src_trans
;
2268 enum pipe_transfer_usage transfer_usage
;
2270 unsigned dst_width
= width
;
2271 unsigned dst_height
= height
;
2272 unsigned dst_depth
= 1;
2273 struct pipe_transfer
*transfer
;
2275 if (ST_DEBUG
& DEBUG_FALLBACK
)
2276 debug_printf("%s: fallback processing\n", __func__
);
2278 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2279 srcY
= strb
->Base
.Height
- srcY
- height
;
2282 map
= pipe_transfer_map(pipe
,
2284 strb
->surface
->u
.tex
.level
,
2285 strb
->surface
->u
.tex
.first_layer
,
2288 width
, height
, &src_trans
);
2290 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2291 baseFormat
== GL_DEPTH_STENCIL
) &&
2292 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2293 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2295 transfer_usage
= PIPE_TRANSFER_WRITE
;
2297 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2298 destX
, destY
, slice
,
2299 dst_width
, dst_height
, dst_depth
,
2302 if (baseFormat
== GL_DEPTH_COMPONENT
||
2303 baseFormat
== GL_DEPTH_STENCIL
) {
2304 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2305 ctx
->Pixel
.DepthBias
!= 0.0F
);
2309 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2310 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2319 data
= malloc(width
* sizeof(uint
));
2322 /* To avoid a large temp memory allocation, do copy row by row */
2323 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2324 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2326 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2329 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2330 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2331 0, 0, width
, 1, data
);
2334 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2339 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2347 malloc(width
* height
* 4 * sizeof(GLfloat
));
2349 if (tempSrc
&& texDest
) {
2350 const GLint dims
= 2;
2352 struct gl_texture_image
*texImage
= &stImage
->base
;
2353 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2355 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2356 unpack
.Invert
= GL_TRUE
;
2359 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2360 dstRowStride
= transfer
->layer_stride
;
2363 dstRowStride
= transfer
->stride
;
2366 /* get float/RGBA image from framebuffer */
2367 /* XXX this usually involves a lot of int/float conversion.
2368 * try to avoid that someday.
2370 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2371 util_format_linear(strb
->texture
->format
),
2374 /* Store into texture memory.
2375 * Note that this does some special things such as pixel transfer
2376 * ops and format conversion. In particular, if the dest tex format
2377 * is actually RGBA but the user created the texture as GL_RGB we
2378 * need to fill-in/override the alpha channel with 1.0.
2380 _mesa_texstore(ctx
, dims
,
2381 texImage
->_BaseFormat
,
2382 texImage
->TexFormat
,
2386 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2390 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2396 st_texture_image_unmap(st
, stImage
, slice
);
2397 pipe
->transfer_unmap(pipe
, src_trans
);
2402 st_can_copyteximage_using_blit(const struct gl_texture_image
*texImage
,
2403 const struct gl_renderbuffer
*rb
)
2405 GLenum tex_baseformat
= _mesa_get_format_base_format(texImage
->TexFormat
);
2407 /* We don't blit to a teximage where the GL base format doesn't match the
2408 * texture's chosen format, except in the case of a GL_RGB texture
2409 * represented with GL_RGBA (where the alpha channel is just being
2412 if (texImage
->_BaseFormat
!= tex_baseformat
&&
2413 ((texImage
->_BaseFormat
!= GL_RGB
|| tex_baseformat
!= GL_RGBA
))) {
2417 /* We can't blit from a RB where the GL base format doesn't match the RB's
2418 * chosen format (for example, GL RGB or ALPHA with rb->Format of an RGBA
2419 * type, because the other channels will be undefined).
2421 if (rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
))
2429 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2430 * Note that the region to copy has already been clipped so we know we
2431 * won't read from outside the source renderbuffer's bounds.
2433 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2436 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2437 struct gl_texture_image
*texImage
,
2438 GLint destX
, GLint destY
, GLint slice
,
2439 struct gl_renderbuffer
*rb
,
2440 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2442 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2443 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2444 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2445 struct st_context
*st
= st_context(ctx
);
2446 struct pipe_context
*pipe
= st
->pipe
;
2447 struct pipe_screen
*screen
= pipe
->screen
;
2448 struct pipe_blit_info blit
;
2449 enum pipe_format dst_format
;
2450 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2454 st_flush_bitmap_cache(st
);
2455 st_invalidate_readpix_cache(st
);
2457 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2458 !_mesa_is_format_astc_2d(texImage
->TexFormat
) &&
2459 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2461 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2462 debug_printf("%s: null strb or stImage\n", __func__
);
2466 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2467 texImage
->TexFormat
)) {
2471 if (!st_can_copyteximage_using_blit(texImage
, rb
)) {
2475 /* Choose the destination format to match the TexImage behavior. */
2476 dst_format
= util_format_linear(stImage
->pt
->format
);
2477 dst_format
= util_format_luminance_to_red(dst_format
);
2478 dst_format
= util_format_intensity_to_red(dst_format
);
2480 /* See if the destination format is supported. */
2481 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2482 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2483 bind
= PIPE_BIND_DEPTH_STENCIL
;
2486 bind
= PIPE_BIND_RENDER_TARGET
;
2490 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2491 stImage
->pt
->nr_samples
,
2492 stImage
->pt
->nr_storage_samples
, bind
)) {
2496 /* Y flipping for the main framebuffer. */
2498 srcY1
= strb
->Base
.Height
- srcY
- height
;
2499 srcY0
= srcY1
+ height
;
2503 srcY1
= srcY0
+ height
;
2506 /* Blit the texture.
2507 * This supports flipping, format conversions, and downsampling.
2509 memset(&blit
, 0, sizeof(blit
));
2510 blit
.src
.resource
= strb
->texture
;
2511 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2512 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2513 blit
.src
.box
.x
= srcX
;
2514 blit
.src
.box
.y
= srcY0
;
2515 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2516 blit
.src
.box
.width
= width
;
2517 blit
.src
.box
.height
= srcY1
- srcY0
;
2518 blit
.src
.box
.depth
= 1;
2519 blit
.dst
.resource
= stImage
->pt
;
2520 blit
.dst
.format
= dst_format
;
2521 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
2522 ? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2523 blit
.dst
.box
.x
= destX
;
2524 blit
.dst
.box
.y
= destY
;
2525 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2526 blit
.dst
.box
.width
= width
;
2527 blit
.dst
.box
.height
= height
;
2528 blit
.dst
.box
.depth
= 1;
2529 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2530 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2531 pipe
->blit(pipe
, &blit
);
2535 /* software fallback */
2536 fallback_copy_texsubimage(ctx
,
2537 strb
, stImage
, texImage
->_BaseFormat
,
2538 destX
, destY
, slice
,
2539 srcX
, srcY
, width
, height
);
2544 * Copy image data from stImage into the texture object 'stObj' at level
2548 copy_image_data_to_texture(struct st_context
*st
,
2549 struct st_texture_object
*stObj
,
2551 struct st_texture_image
*stImage
)
2555 ASSERTED
const struct gl_texture_image
*dstImage
=
2556 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2558 assert(dstImage
->Width
== stImage
->base
.Width
);
2559 assert(dstImage
->Height
== stImage
->base
.Height
);
2560 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2564 /* Copy potentially with the blitter:
2567 if (stImage
->pt
->last_level
== 0)
2570 src_level
= stImage
->base
.Level
;
2572 assert(src_level
<= stImage
->pt
->last_level
);
2573 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2574 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2575 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2576 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2577 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2578 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2580 st_texture_image_copy(st
->pipe
,
2581 stObj
->pt
, dstLevel
, /* dest texture, level */
2582 stImage
->pt
, src_level
, /* src texture, level */
2583 stImage
->base
.Face
);
2585 pipe_resource_reference(&stImage
->pt
, NULL
);
2587 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2592 * Called during state validation. When this function is finished,
2593 * the texture object should be ready for rendering.
2594 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2597 st_finalize_texture(struct gl_context
*ctx
,
2598 struct pipe_context
*pipe
,
2599 struct gl_texture_object
*tObj
,
2602 struct st_context
*st
= st_context(ctx
);
2603 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2604 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2606 const struct st_texture_image
*firstImage
;
2607 enum pipe_format firstImageFormat
;
2609 uint16_t ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2611 if (tObj
->Immutable
)
2614 if (tObj
->_MipmapComplete
)
2615 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2616 else if (tObj
->_BaseComplete
)
2617 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2619 /* Skip the loop over images in the common case of no images having
2620 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
2621 * haven't looked at, then we do need to look at those new images.
2623 if (!stObj
->needs_validation
&&
2624 stObj
->base
.BaseLevel
>= stObj
->validated_first_level
&&
2625 stObj
->lastLevel
<= stObj
->validated_last_level
) {
2629 /* If this texture comes from a window system, there is nothing else to do. */
2630 if (stObj
->surface_based
) {
2634 firstImage
= st_texture_image_const(stObj
->base
.Image
[cubeMapFace
]
2635 [stObj
->base
.BaseLevel
]);
2638 /* If both firstImage and stObj point to a texture which can contain
2639 * all active images, favour firstImage. Note that because of the
2640 * completeness requirement, we know that the image dimensions
2643 if (firstImage
->pt
&&
2644 firstImage
->pt
!= stObj
->pt
&&
2645 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2646 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2647 st_texture_release_all_sampler_views(st
, stObj
);
2650 /* Find gallium format for the Mesa texture */
2652 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2654 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2657 uint16_t height
, depth
;
2659 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2660 firstImage
->base
.Width2
,
2661 firstImage
->base
.Height2
,
2662 firstImage
->base
.Depth2
,
2663 &width
, &height
, &depth
, &ptLayers
);
2665 /* If we previously allocated a pipe texture and its sizes are
2666 * compatible, use them.
2669 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2670 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2671 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2672 ptWidth
= stObj
->pt
->width0
;
2673 ptHeight
= stObj
->pt
->height0
;
2674 ptDepth
= stObj
->pt
->depth0
;
2676 /* Otherwise, compute a new level=0 size that is compatible with the
2679 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2680 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2681 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2683 /* If the base level image is 1x1x1, we still need to ensure that the
2684 * resulting pipe texture ends up with the required number of levels
2687 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2688 ptWidth
<<= firstImage
->base
.Level
;
2690 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2691 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2695 /* At this point, the texture may be incomplete (mismatched cube
2696 * face sizes, for example). If that's the case, give up, but
2697 * don't return GL_FALSE as that would raise an incorrect
2698 * GL_OUT_OF_MEMORY error. See Piglit fbo-incomplete-texture-03 test.
2700 if (!stObj
->base
._BaseComplete
) {
2701 _mesa_test_texobj_completeness(ctx
, &stObj
->base
);
2702 if (!stObj
->base
._BaseComplete
) {
2708 ptNumSamples
= firstImage
->base
.NumSamples
;
2711 /* If we already have a gallium texture, check that it matches the texture
2712 * object's format, target, size, num_levels, etc.
2715 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2716 stObj
->pt
->format
!= firstImageFormat
||
2717 stObj
->pt
->last_level
< stObj
->lastLevel
||
2718 stObj
->pt
->width0
!= ptWidth
||
2719 stObj
->pt
->height0
!= ptHeight
||
2720 stObj
->pt
->depth0
!= ptDepth
||
2721 stObj
->pt
->nr_samples
!= ptNumSamples
||
2722 stObj
->pt
->array_size
!= ptLayers
)
2724 /* The gallium texture does not match the Mesa texture so delete the
2725 * gallium texture now. We'll make a new one below.
2727 pipe_resource_reference(&stObj
->pt
, NULL
);
2728 st_texture_release_all_sampler_views(st
, stObj
);
2729 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
2733 /* May need to create a new gallium texture:
2736 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2738 stObj
->pt
= st_texture_create(st
,
2739 gl_target_to_pipe(stObj
->base
.Target
),
2745 ptLayers
, ptNumSamples
,
2749 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2754 /* Pull in any images not in the object's texture:
2756 for (face
= 0; face
< nr_faces
; face
++) {
2758 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2759 struct st_texture_image
*stImage
=
2760 st_texture_image(stObj
->base
.Image
[face
][level
]);
2762 /* Need to import images in main memory or held in other textures.
2764 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2768 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2769 height
= u_minify(ptHeight
, level
);
2773 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2774 depth
= u_minify(ptDepth
, level
);
2775 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2781 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2782 stImage
->base
.Height
== height
&&
2783 stImage
->base
.Depth
== depth
)) {
2784 /* src image fits expected dest mipmap level size */
2785 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2791 stObj
->validated_first_level
= stObj
->base
.BaseLevel
;
2792 stObj
->validated_last_level
= stObj
->lastLevel
;
2793 stObj
->needs_validation
= false;
2800 * Allocate a new pipe_resource object
2801 * width0, height0, depth0 are the dimensions of the level 0 image
2802 * (the highest resolution). last_level indicates how many mipmap levels
2803 * to allocate storage for. For non-mipmapped textures, this will be zero.
2805 static struct pipe_resource
*
2806 st_texture_create_from_memory(struct st_context
*st
,
2807 struct st_memory_object
*memObj
,
2809 enum pipe_texture_target target
,
2810 enum pipe_format format
,
2819 struct pipe_resource pt
, *newtex
;
2820 struct pipe_screen
*screen
= st
->pipe
->screen
;
2822 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
2824 assert(height0
> 0);
2826 if (target
== PIPE_TEXTURE_CUBE
)
2827 assert(layers
== 6);
2829 DBG("%s target %d format %s last_level %d\n", __func__
,
2830 (int) target
, util_format_name(format
), last_level
);
2833 assert(screen
->is_format_supported(screen
, format
, target
, 0, 0,
2834 PIPE_BIND_SAMPLER_VIEW
));
2836 memset(&pt
, 0, sizeof(pt
));
2839 pt
.last_level
= last_level
;
2841 pt
.height0
= height0
;
2843 pt
.array_size
= layers
;
2844 pt
.usage
= PIPE_USAGE_DEFAULT
;
2846 /* only set this for OpenGL textures, not renderbuffers */
2847 pt
.flags
= PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
;
2848 pt
.nr_samples
= nr_samples
;
2849 pt
.nr_storage_samples
= nr_samples
;
2851 newtex
= screen
->resource_from_memobj(screen
, &pt
, memObj
->memory
, offset
);
2853 assert(!newtex
|| pipe_is_referenced(&newtex
->reference
));
2860 * Allocate texture memory for a whole mipmap stack.
2861 * Note: for multisample textures if the requested sample count is not
2862 * supported, we search for the next higher supported sample count.
2865 st_texture_storage(struct gl_context
*ctx
,
2866 struct gl_texture_object
*texObj
,
2867 GLsizei levels
, GLsizei width
,
2868 GLsizei height
, GLsizei depth
,
2869 struct gl_memory_object
*memObj
,
2872 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2873 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2874 struct st_context
*st
= st_context(ctx
);
2875 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2876 struct st_memory_object
*smObj
= st_memory_object(memObj
);
2877 struct pipe_screen
*screen
= st
->pipe
->screen
;
2878 unsigned ptWidth
, bindings
;
2879 uint16_t ptHeight
, ptDepth
, ptLayers
;
2880 enum pipe_format fmt
;
2882 GLuint num_samples
= texImage
->NumSamples
;
2886 stObj
->lastLevel
= levels
- 1;
2888 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2890 bindings
= default_bindings(st
, fmt
);
2892 if (num_samples
> 0) {
2893 /* Find msaa sample count which is actually supported. For example,
2894 * if the user requests 1x but only 4x or 8x msaa is supported, we'll
2897 enum pipe_texture_target ptarget
= gl_target_to_pipe(texObj
->Target
);
2898 boolean found
= FALSE
;
2900 if (ctx
->Const
.MaxSamples
> 1 && num_samples
== 1) {
2901 /* don't try num_samples = 1 with drivers that support real msaa */
2905 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2906 if (screen
->is_format_supported(screen
, fmt
, ptarget
,
2907 num_samples
, num_samples
,
2908 PIPE_BIND_SAMPLER_VIEW
)) {
2909 /* Update the sample count in gl_texture_image as well. */
2910 texImage
->NumSamples
= num_samples
;
2921 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2922 width
, height
, depth
,
2923 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2926 stObj
->pt
= st_texture_create_from_memory(st
,
2929 gl_target_to_pipe(texObj
->Target
),
2935 ptLayers
, num_samples
,
2939 stObj
->pt
= st_texture_create(st
,
2940 gl_target_to_pipe(texObj
->Target
),
2946 ptLayers
, num_samples
,
2953 /* Set image resource pointers */
2954 for (level
= 0; level
< levels
; level
++) {
2956 for (face
= 0; face
< numFaces
; face
++) {
2957 struct st_texture_image
*stImage
=
2958 st_texture_image(texObj
->Image
[face
][level
]);
2959 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2961 compressed_tex_fallback_allocate(st
, stImage
);
2965 /* The texture is in a validated state, so no need to check later. */
2966 stObj
->needs_validation
= false;
2967 stObj
->validated_first_level
= 0;
2968 stObj
->validated_last_level
= levels
- 1;
2974 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2975 * for a whole mipmap stack.
2978 st_AllocTextureStorage(struct gl_context
*ctx
,
2979 struct gl_texture_object
*texObj
,
2980 GLsizei levels
, GLsizei width
,
2981 GLsizei height
, GLsizei depth
)
2983 return st_texture_storage(ctx
, texObj
, levels
,
2984 width
, height
, depth
,
2990 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2991 GLuint numLevels
, GLint level
,
2992 mesa_format format
, GLuint numSamples
,
2993 GLint width
, GLint height
, GLint depth
)
2995 struct st_context
*st
= st_context(ctx
);
2996 struct pipe_context
*pipe
= st
->pipe
;
2998 if (width
== 0 || height
== 0 || depth
== 0) {
2999 /* zero-sized images are legal, and always fit! */
3003 if (pipe
->screen
->can_create_resource
) {
3004 /* Ask the gallium driver if the texture is too large */
3005 struct gl_texture_object
*texObj
=
3006 _mesa_get_current_tex_object(ctx
, target
);
3007 struct pipe_resource pt
;
3009 /* Setup the pipe_resource object
3011 memset(&pt
, 0, sizeof(pt
));
3013 pt
.target
= gl_target_to_pipe(target
);
3014 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
3015 pt
.nr_samples
= numSamples
;
3016 pt
.nr_storage_samples
= numSamples
;
3018 st_gl_texture_dims_to_pipe_dims(target
,
3019 width
, height
, depth
,
3020 &pt
.width0
, &pt
.height0
,
3021 &pt
.depth0
, &pt
.array_size
);
3023 if (numLevels
> 0) {
3024 /* For immutable textures we know the final number of mip levels */
3025 pt
.last_level
= numLevels
- 1;
3027 else if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
3028 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
3029 /* assume just one mipmap level */
3033 /* assume a full set of mipmaps */
3034 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
3037 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
3040 /* Use core Mesa fallback */
3041 return _mesa_test_proxy_teximage(ctx
, target
, numLevels
, level
, format
,
3042 numSamples
, width
, height
, depth
);
3047 st_TextureView(struct gl_context
*ctx
,
3048 struct gl_texture_object
*texObj
,
3049 struct gl_texture_object
*origTexObj
)
3051 struct st_context
*st
= st_context(ctx
);
3052 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
3053 struct st_texture_object
*tex
= st_texture_object(texObj
);
3054 struct gl_texture_image
*image
= texObj
->Image
[0][0];
3056 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
3057 const int numLevels
= texObj
->NumLevels
;
3062 pipe_resource_reference(&tex
->pt
, orig
->pt
);
3064 /* Set image resource pointers */
3065 for (level
= 0; level
< numLevels
; level
++) {
3066 for (face
= 0; face
< numFaces
; face
++) {
3067 struct st_texture_image
*stImage
=
3068 st_texture_image(texObj
->Image
[face
][level
]);
3069 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
3073 tex
->surface_based
= GL_TRUE
;
3074 tex
->surface_format
=
3075 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
3077 tex
->lastLevel
= numLevels
- 1;
3079 /* free texture sampler views. They need to be recreated when we
3080 * change the texture view parameters.
3082 st_texture_release_all_sampler_views(st
, tex
);
3084 /* The texture is in a validated state, so no need to check later. */
3085 tex
->needs_validation
= false;
3086 tex
->validated_first_level
= 0;
3087 tex
->validated_last_level
= numLevels
- 1;
3094 * Find the mipmap level in 'pt' which matches the level described by
3098 find_mipmap_level(const struct gl_texture_image
*texImage
,
3099 const struct pipe_resource
*pt
)
3101 const GLenum target
= texImage
->TexObject
->Target
;
3102 GLint texWidth
= texImage
->Width
;
3103 GLint texHeight
= texImage
->Height
;
3104 GLint texDepth
= texImage
->Depth
;
3106 uint16_t h
, d
, layers
;
3108 st_gl_texture_dims_to_pipe_dims(target
, texWidth
, texHeight
, texDepth
,
3109 &w
, &h
, &d
, &layers
);
3111 for (level
= 0; level
<= pt
->last_level
; level
++) {
3112 if (u_minify(pt
->width0
, level
) == w
&&
3113 u_minify(pt
->height0
, level
) == h
&&
3114 u_minify(pt
->depth0
, level
) == d
) {
3119 /* If we get here, there must be some sort of inconsistency between
3120 * the Mesa texture object/images and the gallium resource.
3122 debug_printf("Inconsistent textures in find_mipmap_level()\n");
3124 return texImage
->Level
;
3129 st_ClearTexSubImage(struct gl_context
*ctx
,
3130 struct gl_texture_image
*texImage
,
3131 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3132 GLsizei width
, GLsizei height
, GLsizei depth
,
3133 const void *clearValue
)
3135 static const char zeros
[16] = {0};
3136 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3137 struct st_texture_image
*stImage
= st_texture_image(texImage
);
3138 struct pipe_resource
*pt
= stImage
->pt
;
3139 struct st_context
*st
= st_context(ctx
);
3140 struct pipe_context
*pipe
= st
->pipe
;
3142 struct pipe_box box
;
3147 st_flush_bitmap_cache(st
);
3148 st_invalidate_readpix_cache(st
);
3150 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
3151 width
, height
, depth
, &box
);
3152 if (texObj
->Immutable
) {
3153 /* The texture object has to be consistent (no "loose", per-image
3154 * gallium resources). If this texture is a view into another
3155 * texture, we have to apply the MinLevel/Layer offsets. If this is
3156 * not a texture view, the offsets will be zero.
3158 assert(stImage
->pt
== st_texture_object(texObj
)->pt
);
3159 level
= texImage
->Level
+ texObj
->MinLevel
;
3160 box
.z
+= texObj
->MinLayer
;
3163 /* Texture level sizes may be inconsistent. We my have "loose",
3164 * per-image gallium resources. The texImage->Level may not match
3165 * the gallium resource texture level.
3167 level
= find_mipmap_level(texImage
, pt
);
3170 assert(level
<= pt
->last_level
);
3172 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
3177 * Called via the glTexParam*() function, but only when some texture object
3178 * state has actually changed.
3181 st_TexParameter(struct gl_context
*ctx
,
3182 struct gl_texture_object
*texObj
, GLenum pname
)
3184 struct st_context
*st
= st_context(ctx
);
3185 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3188 case GL_TEXTURE_BASE_LEVEL
:
3189 case GL_TEXTURE_MAX_LEVEL
:
3190 case GL_DEPTH_TEXTURE_MODE
:
3191 case GL_DEPTH_STENCIL_TEXTURE_MODE
:
3192 case GL_TEXTURE_SRGB_DECODE_EXT
:
3193 case GL_TEXTURE_SWIZZLE_R
:
3194 case GL_TEXTURE_SWIZZLE_G
:
3195 case GL_TEXTURE_SWIZZLE_B
:
3196 case GL_TEXTURE_SWIZZLE_A
:
3197 case GL_TEXTURE_SWIZZLE_RGBA
:
3198 case GL_TEXTURE_BUFFER_SIZE
:
3199 case GL_TEXTURE_BUFFER_OFFSET
:
3200 /* changing any of these texture parameters means we must create
3201 * new sampler views.
3203 st_texture_release_all_sampler_views(st
, stObj
);
3211 st_SetTextureStorageForMemoryObject(struct gl_context
*ctx
,
3212 struct gl_texture_object
*texObj
,
3213 struct gl_memory_object
*memObj
,
3214 GLsizei levels
, GLsizei width
,
3215 GLsizei height
, GLsizei depth
,
3218 return st_texture_storage(ctx
, texObj
, levels
,
3219 width
, height
, depth
,
3224 st_NewTextureHandle(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
3225 struct gl_sampler_object
*sampObj
)
3227 struct st_context
*st
= st_context(ctx
);
3228 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3229 struct pipe_context
*pipe
= st
->pipe
;
3230 struct pipe_sampler_view
*view
;
3231 struct pipe_sampler_state sampler
= {0};
3233 if (texObj
->Target
!= GL_TEXTURE_BUFFER
) {
3234 if (!st_finalize_texture(ctx
, pipe
, texObj
, 0))
3237 st_convert_sampler(st
, texObj
, sampObj
, 0, &sampler
);
3239 /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
3240 view
= st_get_texture_sampler_view_from_stobj(st
, stObj
, sampObj
, 0, true);
3242 view
= st_get_buffer_sampler_view_from_stobj(st
, stObj
);
3245 return pipe
->create_texture_handle(pipe
, view
, &sampler
);
3250 st_DeleteTextureHandle(struct gl_context
*ctx
, GLuint64 handle
)
3252 struct st_context
*st
= st_context(ctx
);
3253 struct pipe_context
*pipe
= st
->pipe
;
3255 pipe
->delete_texture_handle(pipe
, handle
);
3260 st_MakeTextureHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3263 struct st_context
*st
= st_context(ctx
);
3264 struct pipe_context
*pipe
= st
->pipe
;
3266 pipe
->make_texture_handle_resident(pipe
, handle
, resident
);
3271 st_NewImageHandle(struct gl_context
*ctx
, struct gl_image_unit
*imgObj
)
3273 struct st_context
*st
= st_context(ctx
);
3274 struct pipe_context
*pipe
= st
->pipe
;
3275 struct pipe_image_view image
;
3277 st_convert_image(st
, imgObj
, &image
, GL_READ_WRITE
);
3279 return pipe
->create_image_handle(pipe
, &image
);
3284 st_DeleteImageHandle(struct gl_context
*ctx
, GLuint64 handle
)
3286 struct st_context
*st
= st_context(ctx
);
3287 struct pipe_context
*pipe
= st
->pipe
;
3289 pipe
->delete_image_handle(pipe
, handle
);
3294 st_MakeImageHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3295 GLenum access
, bool resident
)
3297 struct st_context
*st
= st_context(ctx
);
3298 struct pipe_context
*pipe
= st
->pipe
;
3300 pipe
->make_image_handle_resident(pipe
, handle
, access
, resident
);
3305 st_init_texture_functions(struct dd_function_table
*functions
)
3307 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
3308 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
3309 functions
->TexImage
= st_TexImage
;
3310 functions
->TexSubImage
= st_TexSubImage
;
3311 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
3312 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
3313 functions
->GenerateMipmap
= st_generate_mipmap
;
3315 functions
->GetTexSubImage
= st_GetTexSubImage
;
3317 /* compressed texture functions */
3318 functions
->CompressedTexImage
= st_CompressedTexImage
;
3320 functions
->NewTextureObject
= st_NewTextureObject
;
3321 functions
->NewTextureImage
= st_NewTextureImage
;
3322 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
3323 functions
->DeleteTexture
= st_DeleteTextureObject
;
3324 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
3325 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
3326 functions
->MapTextureImage
= st_MapTextureImage
;
3327 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
3329 /* XXX Temporary until we can query pipe's texture sizes */
3330 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
3332 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
3333 functions
->TextureView
= st_TextureView
;
3334 functions
->ClearTexSubImage
= st_ClearTexSubImage
;
3336 functions
->TexParameter
= st_TexParameter
;
3338 /* bindless functions */
3339 functions
->NewTextureHandle
= st_NewTextureHandle
;
3340 functions
->DeleteTextureHandle
= st_DeleteTextureHandle
;
3341 functions
->MakeTextureHandleResident
= st_MakeTextureHandleResident
;
3342 functions
->NewImageHandle
= st_NewImageHandle
;
3343 functions
->DeleteImageHandle
= st_DeleteImageHandle
;
3344 functions
->MakeImageHandleResident
= st_MakeImageHandleResident
;
3346 /* external object functions */
3347 functions
->SetTextureStorageForMemoryObject
= st_SetTextureStorageForMemoryObject
;