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/format/u_format.h"
74 #include "util/u_surface.h"
75 #include "util/u_sampler.h"
76 #include "util/u_math.h"
77 #include "util/u_box.h"
78 #include "util/u_simple_shaders.h"
79 #include "cso_cache/cso_context.h"
80 #include "tgsi/tgsi_ureg.h"
82 #define DBG if (0) printf
85 enum pipe_texture_target
86 gl_target_to_pipe(GLenum target
)
90 case GL_PROXY_TEXTURE_1D
:
91 return PIPE_TEXTURE_1D
;
93 case GL_PROXY_TEXTURE_2D
:
94 case GL_TEXTURE_EXTERNAL_OES
:
95 case GL_TEXTURE_2D_MULTISAMPLE
:
96 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
97 return PIPE_TEXTURE_2D
;
98 case GL_TEXTURE_RECTANGLE_NV
:
99 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
100 return PIPE_TEXTURE_RECT
;
102 case GL_PROXY_TEXTURE_3D
:
103 return PIPE_TEXTURE_3D
;
104 case GL_TEXTURE_CUBE_MAP_ARB
:
105 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
106 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
107 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
108 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
109 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
110 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
111 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
112 return PIPE_TEXTURE_CUBE
;
113 case GL_TEXTURE_1D_ARRAY_EXT
:
114 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
115 return PIPE_TEXTURE_1D_ARRAY
;
116 case GL_TEXTURE_2D_ARRAY_EXT
:
117 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
118 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
119 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
120 return PIPE_TEXTURE_2D_ARRAY
;
121 case GL_TEXTURE_BUFFER
:
123 case GL_TEXTURE_CUBE_MAP_ARRAY
:
124 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
125 return PIPE_TEXTURE_CUBE_ARRAY
;
133 /** called via ctx->Driver.NewTextureImage() */
134 static struct gl_texture_image
*
135 st_NewTextureImage(struct gl_context
* ctx
)
137 DBG("%s\n", __func__
);
139 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
143 /** called via ctx->Driver.DeleteTextureImage() */
145 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
147 /* nothing special (yet) for st_texture_image */
148 _mesa_delete_texture_image(ctx
, img
);
152 /** called via ctx->Driver.NewTextureObject() */
153 static struct gl_texture_object
*
154 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
156 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
160 obj
->level_override
= -1;
161 obj
->layer_override
= -1;
163 /* Pre-allocate a sampler views container to save a branch in the
166 obj
->sampler_views
= calloc(1, sizeof(struct st_sampler_views
)
167 + sizeof(struct st_sampler_view
));
168 if (!obj
->sampler_views
) {
172 obj
->sampler_views
->max
= 1;
174 DBG("%s\n", __func__
);
175 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
177 simple_mtx_init(&obj
->validate_mutex
, mtx_plain
);
178 obj
->needs_validation
= true;
184 /** called via ctx->Driver.DeleteTextureObject() */
186 st_DeleteTextureObject(struct gl_context
*ctx
,
187 struct gl_texture_object
*texObj
)
189 struct st_context
*st
= st_context(ctx
);
190 struct st_texture_object
*stObj
= st_texture_object(texObj
);
192 pipe_resource_reference(&stObj
->pt
, NULL
);
193 st_delete_texture_sampler_views(st
, stObj
);
194 simple_mtx_destroy(&stObj
->validate_mutex
);
195 _mesa_delete_texture_object(ctx
, texObj
);
199 /** called via ctx->Driver.FreeTextureImageBuffer() */
201 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
202 struct gl_texture_image
*texImage
)
204 struct st_context
*st
= st_context(ctx
);
205 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
206 struct st_texture_image
*stImage
= st_texture_image(texImage
);
208 DBG("%s\n", __func__
);
211 pipe_resource_reference(&stImage
->pt
, NULL
);
214 free(stImage
->transfer
);
215 stImage
->transfer
= NULL
;
216 stImage
->num_transfers
= 0;
218 if (stImage
->compressed_data
) {
219 free(stImage
->compressed_data
);
220 stImage
->compressed_data
= NULL
;
223 /* if the texture image is being deallocated, the structure of the
224 * texture is changing so we'll likely need a new sampler view.
226 st_texture_release_all_sampler_views(st
, stObj
);
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 case PIPE_FORMAT_FXT1_RGB
:
2050 case PIPE_FORMAT_FXT1_RGBA
:
2051 dst_glformat
= GL_RGBA8
;
2053 case PIPE_FORMAT_RGTC1_SNORM
:
2054 case PIPE_FORMAT_RGTC2_SNORM
:
2055 if (!ctx
->Extensions
.EXT_texture_snorm
)
2057 dst_glformat
= GL_RGBA8_SNORM
;
2059 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
2060 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
2061 if (!ctx
->Extensions
.ARB_texture_float
)
2063 dst_glformat
= GL_RGBA32F
;
2065 case PIPE_FORMAT_ETC2_R11_UNORM
:
2066 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16_UNORM
,
2067 pipe_target
, 0, 0, bind
))
2069 dst_glformat
= GL_R16
;
2071 case PIPE_FORMAT_ETC2_R11_SNORM
:
2072 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16_SNORM
,
2073 pipe_target
, 0, 0, bind
))
2075 dst_glformat
= GL_R16_SNORM
;
2077 case PIPE_FORMAT_ETC2_RG11_UNORM
:
2078 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16G16_UNORM
,
2079 pipe_target
, 0, 0, bind
))
2081 dst_glformat
= GL_RG16
;
2083 case PIPE_FORMAT_ETC2_RG11_SNORM
:
2084 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16G16_SNORM
,
2085 pipe_target
, 0, 0, bind
))
2087 dst_glformat
= GL_RG16_SNORM
;
2094 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
2095 pipe_target
, 0, 0, bind
,
2098 if (dst_format
== PIPE_FORMAT_NONE
) {
2099 /* unable to get an rgba format!?! */
2104 /* create the destination texture of size (width X height X depth) */
2105 memset(&dst_templ
, 0, sizeof(dst_templ
));
2106 dst_templ
.target
= pipe_target
;
2107 dst_templ
.format
= dst_format
;
2108 dst_templ
.bind
= bind
;
2109 dst_templ
.usage
= PIPE_USAGE_STAGING
;
2111 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
2112 &dst_templ
.width0
, &dst_templ
.height0
,
2113 &dst_templ
.depth0
, &dst_templ
.array_size
);
2115 dst
= screen
->resource_create(screen
, &dst_templ
);
2120 /* From now on, we need the gallium representation of dimensions. */
2121 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2128 assert(texImage
->Face
== 0 ||
2129 texImage
->TexObject
->MinLayer
== 0 ||
2132 memset(&blit
, 0, sizeof(blit
));
2133 blit
.src
.resource
= src
;
2134 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2135 blit
.src
.format
= src_format
;
2136 blit
.dst
.resource
= dst
;
2138 blit
.dst
.format
= dst
->format
;
2139 blit
.src
.box
.x
= xoffset
;
2141 blit
.src
.box
.y
= yoffset
;
2143 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2145 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2146 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2147 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2148 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2149 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2150 blit
.scissor_enable
= FALSE
;
2152 /* blit/render/decompress */
2153 st
->pipe
->blit(st
->pipe
, &blit
);
2155 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2157 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2158 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2163 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2164 dims
= _mesa_get_texture_dimensions(gl_target
);
2166 /* copy/pack data into user buffer */
2167 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2168 ctx
->Pack
.SwapBytes
, NULL
)) {
2170 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2173 for (slice
= 0; slice
< depth
; slice
++) {
2174 ubyte
*slice_map
= map
;
2176 for (row
= 0; row
< height
; row
++) {
2177 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2178 width
, height
, format
, type
,
2181 memcpy(dest
, slice_map
, bytesPerRow
);
2183 slice_map
+= tex_xfer
->stride
;
2186 map
+= tex_xfer
->layer_stride
;
2190 /* format translation via floats */
2193 uint32_t dstMesaFormat
;
2194 int dstStride
, srcStride
;
2196 assert(util_format_is_compressed(src
->format
));
2198 rgba
= malloc(width
* height
* 4 * sizeof(GLfloat
));
2203 if (ST_DEBUG
& DEBUG_FALLBACK
)
2204 debug_printf("%s: fallback format translation\n", __func__
);
2206 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2207 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2208 srcStride
= 4 * width
* sizeof(GLfloat
);
2209 for (slice
= 0; slice
< depth
; slice
++) {
2210 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2211 width
, height
, format
, type
,
2214 /* get float[4] rgba row from surface */
2215 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, height
,
2218 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2219 rgba
, RGBA32_FLOAT
, srcStride
,
2220 width
, height
, NULL
);
2222 /* Handle byte swapping if required */
2223 if (ctx
->Pack
.SwapBytes
) {
2224 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
2225 width
, height
, dest
, dest
);
2228 map
+= tex_xfer
->layer_stride
;
2237 pipe_transfer_unmap(pipe
, tex_xfer
);
2239 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2240 pipe_resource_reference(&dst
, NULL
);
2244 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2245 width
, height
, depth
,
2246 format
, type
, pixels
, texImage
);
2252 * Do a CopyTexSubImage operation using a read transfer from the source,
2253 * a write transfer to the destination and get_tile()/put_tile() to access
2254 * the pixels/texels.
2256 * Note: srcY=0=TOP of renderbuffer
2259 fallback_copy_texsubimage(struct gl_context
*ctx
,
2260 struct st_renderbuffer
*strb
,
2261 struct st_texture_image
*stImage
,
2263 GLint destX
, GLint destY
, GLint slice
,
2264 GLint srcX
, GLint srcY
,
2265 GLsizei width
, GLsizei height
)
2267 struct st_context
*st
= st_context(ctx
);
2268 struct pipe_context
*pipe
= st
->pipe
;
2269 struct pipe_transfer
*src_trans
;
2271 enum pipe_transfer_usage transfer_usage
;
2273 unsigned dst_width
= width
;
2274 unsigned dst_height
= height
;
2275 unsigned dst_depth
= 1;
2276 struct pipe_transfer
*transfer
;
2278 if (ST_DEBUG
& DEBUG_FALLBACK
)
2279 debug_printf("%s: fallback processing\n", __func__
);
2281 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2282 srcY
= strb
->Base
.Height
- srcY
- height
;
2285 map
= pipe_transfer_map(pipe
,
2287 strb
->surface
->u
.tex
.level
,
2288 strb
->surface
->u
.tex
.first_layer
,
2291 width
, height
, &src_trans
);
2293 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2294 baseFormat
== GL_DEPTH_STENCIL
) &&
2295 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2296 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2298 transfer_usage
= PIPE_TRANSFER_WRITE
;
2300 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2301 destX
, destY
, slice
,
2302 dst_width
, dst_height
, dst_depth
,
2305 if (baseFormat
== GL_DEPTH_COMPONENT
||
2306 baseFormat
== GL_DEPTH_STENCIL
) {
2307 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2308 ctx
->Pixel
.DepthBias
!= 0.0F
);
2312 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2313 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2322 data
= malloc(width
* sizeof(uint
));
2325 /* To avoid a large temp memory allocation, do copy row by row */
2326 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2327 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2329 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2332 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2333 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2334 0, 0, width
, 1, data
);
2337 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2342 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2350 malloc(width
* height
* 4 * sizeof(GLfloat
));
2352 if (tempSrc
&& texDest
) {
2353 const GLint dims
= 2;
2355 struct gl_texture_image
*texImage
= &stImage
->base
;
2356 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2358 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2359 unpack
.Invert
= GL_TRUE
;
2362 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2363 dstRowStride
= transfer
->layer_stride
;
2366 dstRowStride
= transfer
->stride
;
2369 /* get float/RGBA image from framebuffer */
2370 /* XXX this usually involves a lot of int/float conversion.
2371 * try to avoid that someday.
2373 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2374 util_format_linear(strb
->texture
->format
),
2377 /* Store into texture memory.
2378 * Note that this does some special things such as pixel transfer
2379 * ops and format conversion. In particular, if the dest tex format
2380 * is actually RGBA but the user created the texture as GL_RGB we
2381 * need to fill-in/override the alpha channel with 1.0.
2383 _mesa_texstore(ctx
, dims
,
2384 texImage
->_BaseFormat
,
2385 texImage
->TexFormat
,
2389 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2393 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2399 st_texture_image_unmap(st
, stImage
, slice
);
2400 pipe
->transfer_unmap(pipe
, src_trans
);
2405 st_can_copyteximage_using_blit(const struct gl_texture_image
*texImage
,
2406 const struct gl_renderbuffer
*rb
)
2408 GLenum tex_baseformat
= _mesa_get_format_base_format(texImage
->TexFormat
);
2410 /* We don't blit to a teximage where the GL base format doesn't match the
2411 * texture's chosen format, except in the case of a GL_RGB texture
2412 * represented with GL_RGBA (where the alpha channel is just being
2415 if (texImage
->_BaseFormat
!= tex_baseformat
&&
2416 ((texImage
->_BaseFormat
!= GL_RGB
|| tex_baseformat
!= GL_RGBA
))) {
2420 /* We can't blit from a RB where the GL base format doesn't match the RB's
2421 * chosen format (for example, GL RGB or ALPHA with rb->Format of an RGBA
2422 * type, because the other channels will be undefined).
2424 if (rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
))
2432 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2433 * Note that the region to copy has already been clipped so we know we
2434 * won't read from outside the source renderbuffer's bounds.
2436 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2439 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2440 struct gl_texture_image
*texImage
,
2441 GLint destX
, GLint destY
, GLint slice
,
2442 struct gl_renderbuffer
*rb
,
2443 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2445 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2446 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2447 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2448 struct st_context
*st
= st_context(ctx
);
2449 struct pipe_context
*pipe
= st
->pipe
;
2450 struct pipe_screen
*screen
= pipe
->screen
;
2451 struct pipe_blit_info blit
;
2452 enum pipe_format dst_format
;
2453 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2457 st_flush_bitmap_cache(st
);
2458 st_invalidate_readpix_cache(st
);
2460 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2461 !_mesa_is_format_astc_2d(texImage
->TexFormat
) &&
2462 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2464 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2465 debug_printf("%s: null strb or stImage\n", __func__
);
2469 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2470 texImage
->TexFormat
)) {
2474 if (!st_can_copyteximage_using_blit(texImage
, rb
)) {
2478 /* Choose the destination format to match the TexImage behavior. */
2479 dst_format
= util_format_linear(stImage
->pt
->format
);
2480 dst_format
= util_format_luminance_to_red(dst_format
);
2481 dst_format
= util_format_intensity_to_red(dst_format
);
2483 /* See if the destination format is supported. */
2484 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2485 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2486 bind
= PIPE_BIND_DEPTH_STENCIL
;
2489 bind
= PIPE_BIND_RENDER_TARGET
;
2493 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2494 stImage
->pt
->nr_samples
,
2495 stImage
->pt
->nr_storage_samples
, bind
)) {
2499 /* Y flipping for the main framebuffer. */
2501 srcY1
= strb
->Base
.Height
- srcY
- height
;
2502 srcY0
= srcY1
+ height
;
2506 srcY1
= srcY0
+ height
;
2509 /* Blit the texture.
2510 * This supports flipping, format conversions, and downsampling.
2512 memset(&blit
, 0, sizeof(blit
));
2513 blit
.src
.resource
= strb
->texture
;
2514 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2515 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2516 blit
.src
.box
.x
= srcX
;
2517 blit
.src
.box
.y
= srcY0
;
2518 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2519 blit
.src
.box
.width
= width
;
2520 blit
.src
.box
.height
= srcY1
- srcY0
;
2521 blit
.src
.box
.depth
= 1;
2522 blit
.dst
.resource
= stImage
->pt
;
2523 blit
.dst
.format
= dst_format
;
2524 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
2525 ? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2526 blit
.dst
.box
.x
= destX
;
2527 blit
.dst
.box
.y
= destY
;
2528 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2529 blit
.dst
.box
.width
= width
;
2530 blit
.dst
.box
.height
= height
;
2531 blit
.dst
.box
.depth
= 1;
2532 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2533 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2534 pipe
->blit(pipe
, &blit
);
2538 /* software fallback */
2539 fallback_copy_texsubimage(ctx
,
2540 strb
, stImage
, texImage
->_BaseFormat
,
2541 destX
, destY
, slice
,
2542 srcX
, srcY
, width
, height
);
2547 * Copy image data from stImage into the texture object 'stObj' at level
2551 copy_image_data_to_texture(struct st_context
*st
,
2552 struct st_texture_object
*stObj
,
2554 struct st_texture_image
*stImage
)
2558 ASSERTED
const struct gl_texture_image
*dstImage
=
2559 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2561 assert(dstImage
->Width
== stImage
->base
.Width
);
2562 assert(dstImage
->Height
== stImage
->base
.Height
);
2563 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2567 /* Copy potentially with the blitter:
2570 if (stImage
->pt
->last_level
== 0)
2573 src_level
= stImage
->base
.Level
;
2575 assert(src_level
<= stImage
->pt
->last_level
);
2576 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2577 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2578 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2579 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2580 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2581 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2583 st_texture_image_copy(st
->pipe
,
2584 stObj
->pt
, dstLevel
, /* dest texture, level */
2585 stImage
->pt
, src_level
, /* src texture, level */
2586 stImage
->base
.Face
);
2588 pipe_resource_reference(&stImage
->pt
, NULL
);
2590 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2595 * Called during state validation. When this function is finished,
2596 * the texture object should be ready for rendering.
2597 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2600 st_finalize_texture(struct gl_context
*ctx
,
2601 struct pipe_context
*pipe
,
2602 struct gl_texture_object
*tObj
,
2605 struct st_context
*st
= st_context(ctx
);
2606 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2607 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2609 const struct st_texture_image
*firstImage
;
2610 enum pipe_format firstImageFormat
;
2612 uint16_t ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2614 if (tObj
->Immutable
)
2617 if (tObj
->_MipmapComplete
)
2618 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2619 else if (tObj
->_BaseComplete
)
2620 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2622 /* Skip the loop over images in the common case of no images having
2623 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
2624 * haven't looked at, then we do need to look at those new images.
2626 if (!stObj
->needs_validation
&&
2627 stObj
->base
.BaseLevel
>= stObj
->validated_first_level
&&
2628 stObj
->lastLevel
<= stObj
->validated_last_level
) {
2632 /* If this texture comes from a window system, there is nothing else to do. */
2633 if (stObj
->surface_based
) {
2637 firstImage
= st_texture_image_const(stObj
->base
.Image
[cubeMapFace
]
2638 [stObj
->base
.BaseLevel
]);
2641 /* If both firstImage and stObj point to a texture which can contain
2642 * all active images, favour firstImage. Note that because of the
2643 * completeness requirement, we know that the image dimensions
2646 if (firstImage
->pt
&&
2647 firstImage
->pt
!= stObj
->pt
&&
2648 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2649 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2650 st_texture_release_all_sampler_views(st
, stObj
);
2653 /* Find gallium format for the Mesa texture */
2655 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2657 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2660 uint16_t height
, depth
;
2662 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2663 firstImage
->base
.Width2
,
2664 firstImage
->base
.Height2
,
2665 firstImage
->base
.Depth2
,
2666 &width
, &height
, &depth
, &ptLayers
);
2668 /* If we previously allocated a pipe texture and its sizes are
2669 * compatible, use them.
2672 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2673 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2674 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2675 ptWidth
= stObj
->pt
->width0
;
2676 ptHeight
= stObj
->pt
->height0
;
2677 ptDepth
= stObj
->pt
->depth0
;
2679 /* Otherwise, compute a new level=0 size that is compatible with the
2682 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2683 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2684 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2686 /* If the base level image is 1x1x1, we still need to ensure that the
2687 * resulting pipe texture ends up with the required number of levels
2690 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2691 ptWidth
<<= firstImage
->base
.Level
;
2693 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2694 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2698 /* At this point, the texture may be incomplete (mismatched cube
2699 * face sizes, for example). If that's the case, give up, but
2700 * don't return GL_FALSE as that would raise an incorrect
2701 * GL_OUT_OF_MEMORY error. See Piglit fbo-incomplete-texture-03 test.
2703 if (!stObj
->base
._BaseComplete
) {
2704 _mesa_test_texobj_completeness(ctx
, &stObj
->base
);
2705 if (!stObj
->base
._BaseComplete
) {
2711 ptNumSamples
= firstImage
->base
.NumSamples
;
2714 /* If we already have a gallium texture, check that it matches the texture
2715 * object's format, target, size, num_levels, etc.
2718 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2719 stObj
->pt
->format
!= firstImageFormat
||
2720 stObj
->pt
->last_level
< stObj
->lastLevel
||
2721 stObj
->pt
->width0
!= ptWidth
||
2722 stObj
->pt
->height0
!= ptHeight
||
2723 stObj
->pt
->depth0
!= ptDepth
||
2724 stObj
->pt
->nr_samples
!= ptNumSamples
||
2725 stObj
->pt
->array_size
!= ptLayers
)
2727 /* The gallium texture does not match the Mesa texture so delete the
2728 * gallium texture now. We'll make a new one below.
2730 pipe_resource_reference(&stObj
->pt
, NULL
);
2731 st_texture_release_all_sampler_views(st
, stObj
);
2732 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
2736 /* May need to create a new gallium texture:
2739 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2741 stObj
->pt
= st_texture_create(st
,
2742 gl_target_to_pipe(stObj
->base
.Target
),
2748 ptLayers
, ptNumSamples
,
2752 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2757 /* Pull in any images not in the object's texture:
2759 for (face
= 0; face
< nr_faces
; face
++) {
2761 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2762 struct st_texture_image
*stImage
=
2763 st_texture_image(stObj
->base
.Image
[face
][level
]);
2765 /* Need to import images in main memory or held in other textures.
2767 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2771 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2772 height
= u_minify(ptHeight
, level
);
2776 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2777 depth
= u_minify(ptDepth
, level
);
2778 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2784 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2785 stImage
->base
.Height
== height
&&
2786 stImage
->base
.Depth
== depth
)) {
2787 /* src image fits expected dest mipmap level size */
2788 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2794 stObj
->validated_first_level
= stObj
->base
.BaseLevel
;
2795 stObj
->validated_last_level
= stObj
->lastLevel
;
2796 stObj
->needs_validation
= false;
2803 * Allocate a new pipe_resource object
2804 * width0, height0, depth0 are the dimensions of the level 0 image
2805 * (the highest resolution). last_level indicates how many mipmap levels
2806 * to allocate storage for. For non-mipmapped textures, this will be zero.
2808 static struct pipe_resource
*
2809 st_texture_create_from_memory(struct st_context
*st
,
2810 struct st_memory_object
*memObj
,
2812 enum pipe_texture_target target
,
2813 enum pipe_format format
,
2822 struct pipe_resource pt
, *newtex
;
2823 struct pipe_screen
*screen
= st
->pipe
->screen
;
2825 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
2827 assert(height0
> 0);
2829 if (target
== PIPE_TEXTURE_CUBE
)
2830 assert(layers
== 6);
2832 DBG("%s target %d format %s last_level %d\n", __func__
,
2833 (int) target
, util_format_name(format
), last_level
);
2836 assert(screen
->is_format_supported(screen
, format
, target
, 0, 0,
2837 PIPE_BIND_SAMPLER_VIEW
));
2839 memset(&pt
, 0, sizeof(pt
));
2842 pt
.last_level
= last_level
;
2844 pt
.height0
= height0
;
2846 pt
.array_size
= layers
;
2847 pt
.usage
= PIPE_USAGE_DEFAULT
;
2849 /* only set this for OpenGL textures, not renderbuffers */
2850 pt
.flags
= PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
;
2851 pt
.nr_samples
= nr_samples
;
2852 pt
.nr_storage_samples
= nr_samples
;
2854 newtex
= screen
->resource_from_memobj(screen
, &pt
, memObj
->memory
, offset
);
2856 assert(!newtex
|| pipe_is_referenced(&newtex
->reference
));
2863 * Allocate texture memory for a whole mipmap stack.
2864 * Note: for multisample textures if the requested sample count is not
2865 * supported, we search for the next higher supported sample count.
2868 st_texture_storage(struct gl_context
*ctx
,
2869 struct gl_texture_object
*texObj
,
2870 GLsizei levels
, GLsizei width
,
2871 GLsizei height
, GLsizei depth
,
2872 struct gl_memory_object
*memObj
,
2875 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2876 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2877 struct st_context
*st
= st_context(ctx
);
2878 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2879 struct st_memory_object
*smObj
= st_memory_object(memObj
);
2880 struct pipe_screen
*screen
= st
->pipe
->screen
;
2881 unsigned ptWidth
, bindings
;
2882 uint16_t ptHeight
, ptDepth
, ptLayers
;
2883 enum pipe_format fmt
;
2885 GLuint num_samples
= texImage
->NumSamples
;
2889 stObj
->lastLevel
= levels
- 1;
2891 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2893 bindings
= default_bindings(st
, fmt
);
2895 if (num_samples
> 0) {
2896 /* Find msaa sample count which is actually supported. For example,
2897 * if the user requests 1x but only 4x or 8x msaa is supported, we'll
2900 enum pipe_texture_target ptarget
= gl_target_to_pipe(texObj
->Target
);
2901 boolean found
= FALSE
;
2903 if (ctx
->Const
.MaxSamples
> 1 && num_samples
== 1) {
2904 /* don't try num_samples = 1 with drivers that support real msaa */
2908 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2909 if (screen
->is_format_supported(screen
, fmt
, ptarget
,
2910 num_samples
, num_samples
,
2911 PIPE_BIND_SAMPLER_VIEW
)) {
2912 /* Update the sample count in gl_texture_image as well. */
2913 texImage
->NumSamples
= num_samples
;
2924 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2925 width
, height
, depth
,
2926 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2929 stObj
->pt
= st_texture_create_from_memory(st
,
2932 gl_target_to_pipe(texObj
->Target
),
2938 ptLayers
, num_samples
,
2942 stObj
->pt
= st_texture_create(st
,
2943 gl_target_to_pipe(texObj
->Target
),
2949 ptLayers
, num_samples
,
2956 /* Set image resource pointers */
2957 for (level
= 0; level
< levels
; level
++) {
2959 for (face
= 0; face
< numFaces
; face
++) {
2960 struct st_texture_image
*stImage
=
2961 st_texture_image(texObj
->Image
[face
][level
]);
2962 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2964 compressed_tex_fallback_allocate(st
, stImage
);
2968 /* The texture is in a validated state, so no need to check later. */
2969 stObj
->needs_validation
= false;
2970 stObj
->validated_first_level
= 0;
2971 stObj
->validated_last_level
= levels
- 1;
2977 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2978 * for a whole mipmap stack.
2981 st_AllocTextureStorage(struct gl_context
*ctx
,
2982 struct gl_texture_object
*texObj
,
2983 GLsizei levels
, GLsizei width
,
2984 GLsizei height
, GLsizei depth
)
2986 return st_texture_storage(ctx
, texObj
, levels
,
2987 width
, height
, depth
,
2993 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2994 GLuint numLevels
, GLint level
,
2995 mesa_format format
, GLuint numSamples
,
2996 GLint width
, GLint height
, GLint depth
)
2998 struct st_context
*st
= st_context(ctx
);
2999 struct pipe_context
*pipe
= st
->pipe
;
3001 if (width
== 0 || height
== 0 || depth
== 0) {
3002 /* zero-sized images are legal, and always fit! */
3006 if (pipe
->screen
->can_create_resource
) {
3007 /* Ask the gallium driver if the texture is too large */
3008 struct gl_texture_object
*texObj
=
3009 _mesa_get_current_tex_object(ctx
, target
);
3010 struct pipe_resource pt
;
3012 /* Setup the pipe_resource object
3014 memset(&pt
, 0, sizeof(pt
));
3016 pt
.target
= gl_target_to_pipe(target
);
3017 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
3018 pt
.nr_samples
= numSamples
;
3019 pt
.nr_storage_samples
= numSamples
;
3021 st_gl_texture_dims_to_pipe_dims(target
,
3022 width
, height
, depth
,
3023 &pt
.width0
, &pt
.height0
,
3024 &pt
.depth0
, &pt
.array_size
);
3026 if (numLevels
> 0) {
3027 /* For immutable textures we know the final number of mip levels */
3028 pt
.last_level
= numLevels
- 1;
3030 else if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
3031 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
3032 /* assume just one mipmap level */
3036 /* assume a full set of mipmaps */
3037 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
3040 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
3043 /* Use core Mesa fallback */
3044 return _mesa_test_proxy_teximage(ctx
, target
, numLevels
, level
, format
,
3045 numSamples
, width
, height
, depth
);
3050 st_TextureView(struct gl_context
*ctx
,
3051 struct gl_texture_object
*texObj
,
3052 struct gl_texture_object
*origTexObj
)
3054 struct st_context
*st
= st_context(ctx
);
3055 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
3056 struct st_texture_object
*tex
= st_texture_object(texObj
);
3057 struct gl_texture_image
*image
= texObj
->Image
[0][0];
3059 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
3060 const int numLevels
= texObj
->NumLevels
;
3065 pipe_resource_reference(&tex
->pt
, orig
->pt
);
3067 /* Set image resource pointers */
3068 for (level
= 0; level
< numLevels
; level
++) {
3069 for (face
= 0; face
< numFaces
; face
++) {
3070 struct st_texture_image
*stImage
=
3071 st_texture_image(texObj
->Image
[face
][level
]);
3072 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
3076 tex
->surface_based
= GL_TRUE
;
3077 tex
->surface_format
=
3078 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
3080 tex
->lastLevel
= numLevels
- 1;
3082 /* free texture sampler views. They need to be recreated when we
3083 * change the texture view parameters.
3085 st_texture_release_all_sampler_views(st
, tex
);
3087 /* The texture is in a validated state, so no need to check later. */
3088 tex
->needs_validation
= false;
3089 tex
->validated_first_level
= 0;
3090 tex
->validated_last_level
= numLevels
- 1;
3097 * Find the mipmap level in 'pt' which matches the level described by
3101 find_mipmap_level(const struct gl_texture_image
*texImage
,
3102 const struct pipe_resource
*pt
)
3104 const GLenum target
= texImage
->TexObject
->Target
;
3105 GLint texWidth
= texImage
->Width
;
3106 GLint texHeight
= texImage
->Height
;
3107 GLint texDepth
= texImage
->Depth
;
3109 uint16_t h
, d
, layers
;
3111 st_gl_texture_dims_to_pipe_dims(target
, texWidth
, texHeight
, texDepth
,
3112 &w
, &h
, &d
, &layers
);
3114 for (level
= 0; level
<= pt
->last_level
; level
++) {
3115 if (u_minify(pt
->width0
, level
) == w
&&
3116 u_minify(pt
->height0
, level
) == h
&&
3117 u_minify(pt
->depth0
, level
) == d
) {
3122 /* If we get here, there must be some sort of inconsistency between
3123 * the Mesa texture object/images and the gallium resource.
3125 debug_printf("Inconsistent textures in find_mipmap_level()\n");
3127 return texImage
->Level
;
3132 st_ClearTexSubImage(struct gl_context
*ctx
,
3133 struct gl_texture_image
*texImage
,
3134 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3135 GLsizei width
, GLsizei height
, GLsizei depth
,
3136 const void *clearValue
)
3138 static const char zeros
[16] = {0};
3139 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3140 struct st_texture_image
*stImage
= st_texture_image(texImage
);
3141 struct pipe_resource
*pt
= stImage
->pt
;
3142 struct st_context
*st
= st_context(ctx
);
3143 struct pipe_context
*pipe
= st
->pipe
;
3145 struct pipe_box box
;
3150 st_flush_bitmap_cache(st
);
3151 st_invalidate_readpix_cache(st
);
3153 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
3154 width
, height
, depth
, &box
);
3155 if (texObj
->Immutable
) {
3156 /* The texture object has to be consistent (no "loose", per-image
3157 * gallium resources). If this texture is a view into another
3158 * texture, we have to apply the MinLevel/Layer offsets. If this is
3159 * not a texture view, the offsets will be zero.
3161 assert(stImage
->pt
== st_texture_object(texObj
)->pt
);
3162 level
= texImage
->Level
+ texObj
->MinLevel
;
3163 box
.z
+= texObj
->MinLayer
;
3166 /* Texture level sizes may be inconsistent. We my have "loose",
3167 * per-image gallium resources. The texImage->Level may not match
3168 * the gallium resource texture level.
3170 level
= find_mipmap_level(texImage
, pt
);
3173 assert(level
<= pt
->last_level
);
3175 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
3180 * Called via the glTexParam*() function, but only when some texture object
3181 * state has actually changed.
3184 st_TexParameter(struct gl_context
*ctx
,
3185 struct gl_texture_object
*texObj
, GLenum pname
)
3187 struct st_context
*st
= st_context(ctx
);
3188 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3191 case GL_TEXTURE_BASE_LEVEL
:
3192 case GL_TEXTURE_MAX_LEVEL
:
3193 case GL_DEPTH_TEXTURE_MODE
:
3194 case GL_DEPTH_STENCIL_TEXTURE_MODE
:
3195 case GL_TEXTURE_SRGB_DECODE_EXT
:
3196 case GL_TEXTURE_SWIZZLE_R
:
3197 case GL_TEXTURE_SWIZZLE_G
:
3198 case GL_TEXTURE_SWIZZLE_B
:
3199 case GL_TEXTURE_SWIZZLE_A
:
3200 case GL_TEXTURE_SWIZZLE_RGBA
:
3201 case GL_TEXTURE_BUFFER_SIZE
:
3202 case GL_TEXTURE_BUFFER_OFFSET
:
3203 /* changing any of these texture parameters means we must create
3204 * new sampler views.
3206 st_texture_release_all_sampler_views(st
, stObj
);
3214 st_SetTextureStorageForMemoryObject(struct gl_context
*ctx
,
3215 struct gl_texture_object
*texObj
,
3216 struct gl_memory_object
*memObj
,
3217 GLsizei levels
, GLsizei width
,
3218 GLsizei height
, GLsizei depth
,
3221 return st_texture_storage(ctx
, texObj
, levels
,
3222 width
, height
, depth
,
3227 st_NewTextureHandle(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
3228 struct gl_sampler_object
*sampObj
)
3230 struct st_context
*st
= st_context(ctx
);
3231 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3232 struct pipe_context
*pipe
= st
->pipe
;
3233 struct pipe_sampler_view
*view
;
3234 struct pipe_sampler_state sampler
= {0};
3236 if (texObj
->Target
!= GL_TEXTURE_BUFFER
) {
3237 if (!st_finalize_texture(ctx
, pipe
, texObj
, 0))
3240 st_convert_sampler(st
, texObj
, sampObj
, 0, &sampler
);
3242 /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
3243 view
= st_get_texture_sampler_view_from_stobj(st
, stObj
, sampObj
, 0, true);
3245 view
= st_get_buffer_sampler_view_from_stobj(st
, stObj
);
3248 return pipe
->create_texture_handle(pipe
, view
, &sampler
);
3253 st_DeleteTextureHandle(struct gl_context
*ctx
, GLuint64 handle
)
3255 struct st_context
*st
= st_context(ctx
);
3256 struct pipe_context
*pipe
= st
->pipe
;
3258 pipe
->delete_texture_handle(pipe
, handle
);
3263 st_MakeTextureHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3266 struct st_context
*st
= st_context(ctx
);
3267 struct pipe_context
*pipe
= st
->pipe
;
3269 pipe
->make_texture_handle_resident(pipe
, handle
, resident
);
3274 st_NewImageHandle(struct gl_context
*ctx
, struct gl_image_unit
*imgObj
)
3276 struct st_context
*st
= st_context(ctx
);
3277 struct pipe_context
*pipe
= st
->pipe
;
3278 struct pipe_image_view image
;
3280 st_convert_image(st
, imgObj
, &image
, GL_READ_WRITE
);
3282 return pipe
->create_image_handle(pipe
, &image
);
3287 st_DeleteImageHandle(struct gl_context
*ctx
, GLuint64 handle
)
3289 struct st_context
*st
= st_context(ctx
);
3290 struct pipe_context
*pipe
= st
->pipe
;
3292 pipe
->delete_image_handle(pipe
, handle
);
3297 st_MakeImageHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3298 GLenum access
, bool resident
)
3300 struct st_context
*st
= st_context(ctx
);
3301 struct pipe_context
*pipe
= st
->pipe
;
3303 pipe
->make_image_handle_resident(pipe
, handle
, access
, resident
);
3308 st_init_texture_functions(struct dd_function_table
*functions
)
3310 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
3311 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
3312 functions
->TexImage
= st_TexImage
;
3313 functions
->TexSubImage
= st_TexSubImage
;
3314 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
3315 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
3316 functions
->GenerateMipmap
= st_generate_mipmap
;
3318 functions
->GetTexSubImage
= st_GetTexSubImage
;
3320 /* compressed texture functions */
3321 functions
->CompressedTexImage
= st_CompressedTexImage
;
3323 functions
->NewTextureObject
= st_NewTextureObject
;
3324 functions
->NewTextureImage
= st_NewTextureImage
;
3325 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
3326 functions
->DeleteTexture
= st_DeleteTextureObject
;
3327 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
3328 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
3329 functions
->MapTextureImage
= st_MapTextureImage
;
3330 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
3332 /* XXX Temporary until we can query pipe's texture sizes */
3333 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
3335 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
3336 functions
->TextureView
= st_TextureView
;
3337 functions
->ClearTexSubImage
= st_ClearTexSubImage
;
3339 functions
->TexParameter
= st_TexParameter
;
3341 /* bindless functions */
3342 functions
->NewTextureHandle
= st_NewTextureHandle
;
3343 functions
->DeleteTextureHandle
= st_DeleteTextureHandle
;
3344 functions
->MakeTextureHandleResident
= st_MakeTextureHandleResident
;
3345 functions
->NewImageHandle
= st_NewImageHandle
;
3346 functions
->DeleteImageHandle
= st_DeleteImageHandle
;
3347 functions
->MakeImageHandleResident
= st_MakeImageHandleResident
;
3349 /* external object functions */
3350 functions
->SetTextureStorageForMemoryObject
= st_SetTextureStorageForMemoryObject
;