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_etc.h"
45 #include "main/texgetimage.h"
46 #include "main/teximage.h"
47 #include "main/texobj.h"
48 #include "main/texstore.h"
50 #include "state_tracker/st_debug.h"
51 #include "state_tracker/st_context.h"
52 #include "state_tracker/st_cb_bitmap.h"
53 #include "state_tracker/st_cb_fbo.h"
54 #include "state_tracker/st_cb_flush.h"
55 #include "state_tracker/st_cb_texture.h"
56 #include "state_tracker/st_cb_bufferobjects.h"
57 #include "state_tracker/st_cb_memoryobjects.h"
58 #include "state_tracker/st_format.h"
59 #include "state_tracker/st_pbo.h"
60 #include "state_tracker/st_texture.h"
61 #include "state_tracker/st_gen_mipmap.h"
62 #include "state_tracker/st_atom.h"
63 #include "state_tracker/st_sampler_view.h"
65 #include "pipe/p_context.h"
66 #include "pipe/p_defines.h"
67 #include "util/u_inlines.h"
68 #include "util/u_upload_mgr.h"
69 #include "pipe/p_shader_tokens.h"
70 #include "util/u_tile.h"
71 #include "util/u_format.h"
72 #include "util/u_surface.h"
73 #include "util/u_sampler.h"
74 #include "util/u_math.h"
75 #include "util/u_box.h"
76 #include "util/u_simple_shaders.h"
77 #include "cso_cache/cso_context.h"
78 #include "tgsi/tgsi_ureg.h"
80 #define DBG if (0) printf
83 enum pipe_texture_target
84 gl_target_to_pipe(GLenum target
)
88 case GL_PROXY_TEXTURE_1D
:
89 return PIPE_TEXTURE_1D
;
91 case GL_PROXY_TEXTURE_2D
:
92 case GL_TEXTURE_EXTERNAL_OES
:
93 case GL_TEXTURE_2D_MULTISAMPLE
:
94 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
95 return PIPE_TEXTURE_2D
;
96 case GL_TEXTURE_RECTANGLE_NV
:
97 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
98 return PIPE_TEXTURE_RECT
;
100 case GL_PROXY_TEXTURE_3D
:
101 return PIPE_TEXTURE_3D
;
102 case GL_TEXTURE_CUBE_MAP_ARB
:
103 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
104 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
105 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
106 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
107 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
108 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
109 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
110 return PIPE_TEXTURE_CUBE
;
111 case GL_TEXTURE_1D_ARRAY_EXT
:
112 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
113 return PIPE_TEXTURE_1D_ARRAY
;
114 case GL_TEXTURE_2D_ARRAY_EXT
:
115 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
116 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
117 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
118 return PIPE_TEXTURE_2D_ARRAY
;
119 case GL_TEXTURE_BUFFER
:
121 case GL_TEXTURE_CUBE_MAP_ARRAY
:
122 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
123 return PIPE_TEXTURE_CUBE_ARRAY
;
131 /** called via ctx->Driver.NewTextureImage() */
132 static struct gl_texture_image
*
133 st_NewTextureImage(struct gl_context
* ctx
)
135 DBG("%s\n", __func__
);
137 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
141 /** called via ctx->Driver.DeleteTextureImage() */
143 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
145 /* nothing special (yet) for st_texture_image */
146 _mesa_delete_texture_image(ctx
, img
);
150 /** called via ctx->Driver.NewTextureObject() */
151 static struct gl_texture_object
*
152 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
154 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
158 /* Pre-allocate a sampler views container to save a branch in the fast path. */
159 obj
->sampler_views
= calloc(1, sizeof(struct st_sampler_views
) + sizeof(struct st_sampler_view
));
160 if (!obj
->sampler_views
) {
164 obj
->sampler_views
->max
= 1;
166 DBG("%s\n", __func__
);
167 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
169 simple_mtx_init(&obj
->validate_mutex
, mtx_plain
);
170 obj
->needs_validation
= true;
175 /** called via ctx->Driver.DeleteTextureObject() */
177 st_DeleteTextureObject(struct gl_context
*ctx
,
178 struct gl_texture_object
*texObj
)
180 struct st_context
*st
= st_context(ctx
);
181 struct st_texture_object
*stObj
= st_texture_object(texObj
);
183 pipe_resource_reference(&stObj
->pt
, NULL
);
184 st_texture_release_all_sampler_views(st
, stObj
);
185 st_texture_free_sampler_views(stObj
);
186 simple_mtx_destroy(&stObj
->validate_mutex
);
187 _mesa_delete_texture_object(ctx
, texObj
);
191 /** called via ctx->Driver.FreeTextureImageBuffer() */
193 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
194 struct gl_texture_image
*texImage
)
196 struct st_context
*st
= st_context(ctx
);
197 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
198 struct st_texture_image
*stImage
= st_texture_image(texImage
);
200 DBG("%s\n", __func__
);
203 pipe_resource_reference(&stImage
->pt
, NULL
);
206 free(stImage
->transfer
);
207 stImage
->transfer
= NULL
;
208 stImage
->num_transfers
= 0;
210 if (stImage
->etc_data
) {
211 free(stImage
->etc_data
);
212 stImage
->etc_data
= NULL
;
215 /* if the texture image is being deallocated, the structure of the
216 * texture is changing so we'll likely need a new sampler view.
218 st_texture_release_all_sampler_views(st
, stObj
);
222 st_compressed_format_fallback(struct st_context
*st
, mesa_format format
)
224 if (format
== MESA_FORMAT_ETC1_RGB8
)
225 return !st
->has_etc1
;
227 if (_mesa_is_format_etc2(format
))
228 return !st
->has_etc2
;
234 etc_fallback_allocate(struct st_context
*st
, struct st_texture_image
*stImage
)
236 struct gl_texture_image
*texImage
= &stImage
->base
;
238 if (!st_compressed_format_fallback(st
, texImage
->TexFormat
))
241 if (stImage
->etc_data
)
242 free(stImage
->etc_data
);
244 unsigned data_size
= _mesa_format_image_size(texImage
->TexFormat
,
250 malloc(data_size
* _mesa_num_tex_faces(texImage
->TexObject
->Target
));
253 /** called via ctx->Driver.MapTextureImage() */
255 st_MapTextureImage(struct gl_context
*ctx
,
256 struct gl_texture_image
*texImage
,
257 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
259 GLubyte
**mapOut
, GLint
*rowStrideOut
)
261 struct st_context
*st
= st_context(ctx
);
262 struct st_texture_image
*stImage
= st_texture_image(texImage
);
264 struct pipe_transfer
*transfer
;
266 /* Check for unexpected flags */
267 assert((mode
& ~(GL_MAP_READ_BIT
|
269 GL_MAP_INVALIDATE_RANGE_BIT
)) == 0);
271 const enum pipe_transfer_usage transfer_flags
=
272 st_access_flags_to_transfer_flags(mode
, false);
274 map
= st_texture_image_map(st
, stImage
, transfer_flags
, x
, y
, slice
, w
, h
, 1,
277 if (st_compressed_format_fallback(st
, texImage
->TexFormat
)) {
278 /* ETC isn't supported by all gallium drivers, where it's represented
279 * by uncompressed formats. We store the compressed data (as it's
280 * needed for image copies in OES_copy_image), and decompress as
281 * necessary in Unmap.
283 * Note: all ETC1/ETC2 formats have 4x4 block sizes.
285 unsigned z
= transfer
->box
.z
;
286 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
288 unsigned bytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
289 unsigned stride
= *rowStrideOut
= itransfer
->temp_stride
=
290 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width2
);
291 *mapOut
= itransfer
->temp_data
=
292 stImage
->etc_data
+ ((x
/ 4) * bytes
+ (y
/ 4) * stride
) +
293 z
* stride
* texImage
->Height2
/ 4;
294 itransfer
->map
= map
;
297 /* supported mapping */
299 *rowStrideOut
= transfer
->stride
;
309 /** called via ctx->Driver.UnmapTextureImage() */
311 st_UnmapTextureImage(struct gl_context
*ctx
,
312 struct gl_texture_image
*texImage
,
315 struct st_context
*st
= st_context(ctx
);
316 struct st_texture_image
*stImage
= st_texture_image(texImage
);
318 if (st_compressed_format_fallback(st
, texImage
->TexFormat
)) {
319 /* Decompress the compressed image on upload if the driver doesn't
320 * support the compressed format. */
321 unsigned z
= slice
+ stImage
->base
.Face
;
322 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
323 struct pipe_transfer
*transfer
= itransfer
->transfer
;
325 assert(z
== transfer
->box
.z
);
327 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
328 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
329 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
330 itransfer
->temp_data
,
331 itransfer
->temp_stride
,
332 transfer
->box
.width
, transfer
->box
.height
);
335 bool bgra
= stImage
->pt
->format
== PIPE_FORMAT_B8G8R8A8_SRGB
;
336 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
337 itransfer
->temp_data
, itransfer
->temp_stride
,
338 transfer
->box
.width
, transfer
->box
.height
,
344 itransfer
->temp_data
= NULL
;
345 itransfer
->temp_stride
= 0;
349 st_texture_image_unmap(st
, stImage
, slice
);
354 * Return default texture resource binding bitmask for the given format.
357 default_bindings(struct st_context
*st
, enum pipe_format format
)
359 struct pipe_screen
*screen
= st
->pipe
->screen
;
360 const unsigned target
= PIPE_TEXTURE_2D
;
363 if (util_format_is_depth_or_stencil(format
))
364 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
366 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
368 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
372 format
= util_format_linear(format
);
374 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
377 return PIPE_BIND_SAMPLER_VIEW
;
383 * Given the size of a mipmap image, try to compute the size of the level=0
386 * Note that this isn't always accurate for odd-sized, non-POW textures.
387 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
389 * \return GL_TRUE for success, GL_FALSE for failure
392 guess_base_level_size(GLenum target
,
393 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
394 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
401 /* Guess the size of the base level.
402 * Depending on the image's size, we can't always make a guess here.
406 case GL_TEXTURE_1D_ARRAY
:
411 case GL_TEXTURE_2D_ARRAY
:
412 /* We can't make a good guess here, because the base level dimensions
415 if (width
== 1 || height
== 1) {
422 case GL_TEXTURE_CUBE_MAP
:
423 case GL_TEXTURE_CUBE_MAP_ARRAY
:
429 /* We can't make a good guess here, because the base level dimensions
432 if (width
== 1 || height
== 1 || depth
== 1) {
440 case GL_TEXTURE_RECTANGLE
:
457 * Try to determine whether we should allocate memory for a full texture
458 * mipmap. The problem is when we get a glTexImage(level=0) call, we
459 * can't immediately know if other mipmap levels are coming next. Here
460 * we try to guess whether to allocate memory for a mipmap or just the
463 * If we guess incorrectly here we'll later reallocate the right amount of
464 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
466 * \param stObj the texture object we're going to allocate memory for.
467 * \param stImage describes the incoming image which we need to store.
470 allocate_full_mipmap(const struct st_texture_object
*stObj
,
471 const struct st_texture_image
*stImage
)
473 switch (stObj
->base
.Target
) {
474 case GL_TEXTURE_RECTANGLE_NV
:
475 case GL_TEXTURE_BUFFER
:
476 case GL_TEXTURE_EXTERNAL_OES
:
477 case GL_TEXTURE_2D_MULTISAMPLE
:
478 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
479 /* these texture types cannot be mipmapped */
483 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
486 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
487 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
488 /* depth/stencil textures are seldom mipmapped */
491 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
494 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
495 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
496 /* not a mipmap minification filter */
499 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
500 /* 3D textures are seldom mipmapped */
508 * Try to allocate a pipe_resource object for the given st_texture_object.
510 * We use the given st_texture_image as a clue to determine the size of the
511 * mipmap image at level=0.
513 * \return GL_TRUE for success, GL_FALSE if out of memory.
516 guess_and_alloc_texture(struct st_context
*st
,
517 struct st_texture_object
*stObj
,
518 const struct st_texture_image
*stImage
)
520 const struct gl_texture_image
*firstImage
;
521 GLuint lastLevel
, width
, height
, depth
;
524 uint16_t ptHeight
, ptDepth
, ptLayers
;
525 enum pipe_format fmt
;
526 bool guessed_box
= false;
528 DBG("%s\n", __func__
);
532 /* If a base level image with compatible size exists, use that as our guess.
534 firstImage
= _mesa_base_tex_image(&stObj
->base
);
536 firstImage
->Width2
> 0 &&
537 firstImage
->Height2
> 0 &&
538 firstImage
->Depth2
> 0 &&
539 guess_base_level_size(stObj
->base
.Target
,
544 &width
, &height
, &depth
)) {
545 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
546 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
547 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
552 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
553 stImage
->base
.Width2
,
554 stImage
->base
.Height2
,
555 stImage
->base
.Depth2
,
557 &width
, &height
, &depth
);
560 /* we can't determine the image size at level=0 */
561 /* this is not an out of memory error */
565 /* At this point, (width x height x depth) is the expected size of
566 * the level=0 mipmap image.
569 /* Guess a reasonable value for lastLevel. With OpenGL we have no
570 * idea how many mipmap levels will be in a texture until we start
571 * to render with it. Make an educated guess here but be prepared
572 * to re-allocating a texture buffer with space for more (or fewer)
573 * mipmap levels later.
575 if (allocate_full_mipmap(stObj
, stImage
)) {
576 /* alloc space for a full mipmap */
577 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
578 width
, height
, depth
) - 1;
581 /* only alloc space for a single mipmap level */
585 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
587 bindings
= default_bindings(st
, fmt
);
589 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
590 width
, height
, depth
,
591 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
593 stObj
->pt
= st_texture_create(st
,
594 gl_target_to_pipe(stObj
->base
.Target
),
603 stObj
->lastLevel
= lastLevel
;
605 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
607 return stObj
->pt
!= NULL
;
612 * Called via ctx->Driver.AllocTextureImageBuffer().
613 * If the texture object/buffer already has space for the indicated image,
614 * we're done. Otherwise, allocate memory for the new texture image.
617 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
618 struct gl_texture_image
*texImage
)
620 struct st_context
*st
= st_context(ctx
);
621 struct st_texture_image
*stImage
= st_texture_image(texImage
);
622 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
623 const GLuint level
= texImage
->Level
;
624 GLuint width
= texImage
->Width
;
625 GLuint height
= texImage
->Height
;
626 GLuint depth
= texImage
->Depth
;
628 DBG("%s\n", __func__
);
630 assert(!stImage
->pt
); /* xxx this might be wrong */
632 stObj
->needs_validation
= true;
634 etc_fallback_allocate(st
, stImage
);
636 /* Look if the parent texture object has space for this image */
638 level
<= stObj
->pt
->last_level
&&
639 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
640 /* this image will fit in the existing texture object's memory */
641 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
645 /* The parent texture object does not have space for this image */
647 pipe_resource_reference(&stObj
->pt
, NULL
);
648 st_texture_release_all_sampler_views(st
, stObj
);
650 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
651 /* Probably out of memory.
652 * Try flushing any pending rendering, then retry.
655 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
656 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
662 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
663 /* The image will live in the object's mipmap memory */
664 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
669 /* Create a new, temporary texture/resource/buffer to hold this
670 * one texture image. Note that when we later access this image
671 * (either for mapping or copying) we'll want to always specify
672 * mipmap level=0, even if the image represents some other mipmap
675 enum pipe_format format
=
676 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
677 GLuint bindings
= default_bindings(st
, format
);
679 uint16_t ptHeight
, ptDepth
, ptLayers
;
681 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
682 width
, height
, depth
,
683 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
685 stImage
->pt
= st_texture_create(st
,
686 gl_target_to_pipe(stObj
->base
.Target
),
694 return stImage
->pt
!= NULL
;
700 * Preparation prior to glTexImage. Basically check the 'surface_based'
701 * field and switch to a "normal" tex image if necessary.
704 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
705 GLenum format
, GLenum type
)
707 struct gl_texture_object
*texObj
= texImage
->TexObject
;
708 struct st_texture_object
*stObj
= st_texture_object(texObj
);
710 /* switch to "normal" */
711 if (stObj
->surface_based
) {
712 const GLenum target
= texObj
->Target
;
713 const GLuint level
= texImage
->Level
;
714 mesa_format texFormat
;
716 assert(!st_texture_image(texImage
)->pt
);
717 _mesa_clear_texture_object(ctx
, texObj
, texImage
);
718 stObj
->layer_override
= 0;
719 stObj
->level_override
= 0;
720 pipe_resource_reference(&stObj
->pt
, NULL
);
722 /* oops, need to init this image again */
723 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
724 texImage
->InternalFormat
, format
,
727 _mesa_init_teximage_fields(ctx
, texImage
,
728 texImage
->Width
, texImage
->Height
,
729 texImage
->Depth
, texImage
->Border
,
730 texImage
->InternalFormat
, texFormat
);
732 stObj
->surface_based
= GL_FALSE
;
738 * Return a writemask for the gallium blit. The parameters can be base
739 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
742 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
745 case GL_DEPTH_STENCIL
:
747 case GL_DEPTH_STENCIL
:
749 case GL_DEPTH_COMPONENT
:
751 case GL_STENCIL_INDEX
:
758 case GL_DEPTH_COMPONENT
:
760 case GL_DEPTH_STENCIL
:
761 case GL_DEPTH_COMPONENT
:
768 case GL_STENCIL_INDEX
:
770 case GL_STENCIL_INDEX
:
778 return PIPE_MASK_RGBA
;
783 * Converts format to a format with the same components, types
784 * and sizes, but with the components in RGBA order.
786 static enum pipe_format
787 unswizzle_format(enum pipe_format format
)
791 case PIPE_FORMAT_B8G8R8A8_UNORM
:
792 case PIPE_FORMAT_A8R8G8B8_UNORM
:
793 case PIPE_FORMAT_A8B8G8R8_UNORM
:
794 return PIPE_FORMAT_R8G8B8A8_UNORM
;
796 case PIPE_FORMAT_B10G10R10A2_UNORM
:
797 return PIPE_FORMAT_R10G10B10A2_UNORM
;
799 case PIPE_FORMAT_B10G10R10A2_SNORM
:
800 return PIPE_FORMAT_R10G10B10A2_SNORM
;
802 case PIPE_FORMAT_B10G10R10A2_UINT
:
803 return PIPE_FORMAT_R10G10B10A2_UINT
;
811 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
813 static enum pipe_format
814 alpha_to_red(enum pipe_format format
)
818 case PIPE_FORMAT_A8_UNORM
:
819 return PIPE_FORMAT_R8_UNORM
;
820 case PIPE_FORMAT_A8_SNORM
:
821 return PIPE_FORMAT_R8_SNORM
;
822 case PIPE_FORMAT_A8_UINT
:
823 return PIPE_FORMAT_R8_UINT
;
824 case PIPE_FORMAT_A8_SINT
:
825 return PIPE_FORMAT_R8_SINT
;
827 case PIPE_FORMAT_A16_UNORM
:
828 return PIPE_FORMAT_R16_UNORM
;
829 case PIPE_FORMAT_A16_SNORM
:
830 return PIPE_FORMAT_R16_SNORM
;
831 case PIPE_FORMAT_A16_UINT
:
832 return PIPE_FORMAT_R16_UINT
;
833 case PIPE_FORMAT_A16_SINT
:
834 return PIPE_FORMAT_R16_SINT
;
835 case PIPE_FORMAT_A16_FLOAT
:
836 return PIPE_FORMAT_R16_FLOAT
;
838 case PIPE_FORMAT_A32_UINT
:
839 return PIPE_FORMAT_R32_UINT
;
840 case PIPE_FORMAT_A32_SINT
:
841 return PIPE_FORMAT_R32_SINT
;
842 case PIPE_FORMAT_A32_FLOAT
:
843 return PIPE_FORMAT_R32_FLOAT
;
851 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
853 static enum pipe_format
854 red_alpha_to_red_green(enum pipe_format format
)
858 case PIPE_FORMAT_R8A8_UNORM
:
859 return PIPE_FORMAT_R8G8_UNORM
;
860 case PIPE_FORMAT_R8A8_SNORM
:
861 return PIPE_FORMAT_R8G8_SNORM
;
862 case PIPE_FORMAT_R8A8_UINT
:
863 return PIPE_FORMAT_R8G8_UINT
;
864 case PIPE_FORMAT_R8A8_SINT
:
865 return PIPE_FORMAT_R8G8_SINT
;
867 case PIPE_FORMAT_R16A16_UNORM
:
868 return PIPE_FORMAT_R16G16_UNORM
;
869 case PIPE_FORMAT_R16A16_SNORM
:
870 return PIPE_FORMAT_R16G16_SNORM
;
871 case PIPE_FORMAT_R16A16_UINT
:
872 return PIPE_FORMAT_R16G16_UINT
;
873 case PIPE_FORMAT_R16A16_SINT
:
874 return PIPE_FORMAT_R16G16_SINT
;
875 case PIPE_FORMAT_R16A16_FLOAT
:
876 return PIPE_FORMAT_R16G16_FLOAT
;
878 case PIPE_FORMAT_R32A32_UINT
:
879 return PIPE_FORMAT_R32G32_UINT
;
880 case PIPE_FORMAT_R32A32_SINT
:
881 return PIPE_FORMAT_R32G32_SINT
;
882 case PIPE_FORMAT_R32A32_FLOAT
:
883 return PIPE_FORMAT_R32G32_FLOAT
;
891 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
893 static enum pipe_format
894 luminance_alpha_to_red_green(enum pipe_format format
)
898 case PIPE_FORMAT_L8A8_UNORM
:
899 return PIPE_FORMAT_R8G8_UNORM
;
900 case PIPE_FORMAT_L8A8_SNORM
:
901 return PIPE_FORMAT_R8G8_SNORM
;
902 case PIPE_FORMAT_L8A8_UINT
:
903 return PIPE_FORMAT_R8G8_UINT
;
904 case PIPE_FORMAT_L8A8_SINT
:
905 return PIPE_FORMAT_R8G8_SINT
;
907 case PIPE_FORMAT_L16A16_UNORM
:
908 return PIPE_FORMAT_R16G16_UNORM
;
909 case PIPE_FORMAT_L16A16_SNORM
:
910 return PIPE_FORMAT_R16G16_SNORM
;
911 case PIPE_FORMAT_L16A16_UINT
:
912 return PIPE_FORMAT_R16G16_UINT
;
913 case PIPE_FORMAT_L16A16_SINT
:
914 return PIPE_FORMAT_R16G16_SINT
;
915 case PIPE_FORMAT_L16A16_FLOAT
:
916 return PIPE_FORMAT_R16G16_FLOAT
;
918 case PIPE_FORMAT_L32A32_UINT
:
919 return PIPE_FORMAT_R32G32_UINT
;
920 case PIPE_FORMAT_L32A32_SINT
:
921 return PIPE_FORMAT_R32G32_SINT
;
922 case PIPE_FORMAT_L32A32_FLOAT
:
923 return PIPE_FORMAT_R32G32_FLOAT
;
931 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
934 format_is_alpha(enum pipe_format format
)
936 const struct util_format_description
*desc
= util_format_description(format
);
938 if (desc
->nr_channels
== 1 &&
939 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
940 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
941 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
942 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
949 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
952 format_is_red(enum pipe_format format
)
954 const struct util_format_description
*desc
= util_format_description(format
);
956 if (desc
->nr_channels
== 1 &&
957 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
958 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
959 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
960 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
968 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
971 format_is_luminance(enum pipe_format format
)
973 const struct util_format_description
*desc
= util_format_description(format
);
975 if (desc
->nr_channels
== 1 &&
976 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
977 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
978 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
979 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
986 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
989 format_is_red_alpha(enum pipe_format format
)
991 const struct util_format_description
*desc
= util_format_description(format
);
993 if (desc
->nr_channels
== 2 &&
994 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
995 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
996 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
997 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
1004 format_is_swizzled_rgba(enum pipe_format format
)
1006 const struct util_format_description
*desc
= util_format_description(format
);
1008 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
1009 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
1010 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
1011 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
1019 unsigned char swizzle
[4];
1020 enum pipe_format format
;
1023 static const struct format_table table_8888_unorm
[] = {
1024 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
1025 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
1026 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
1027 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
1030 static const struct format_table table_1010102_unorm
[] = {
1031 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
1032 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
1035 static const struct format_table table_1010102_snorm
[] = {
1036 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
1037 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
1040 static const struct format_table table_1010102_uint
[] = {
1041 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
1042 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
1045 static enum pipe_format
1046 swizzle_format(enum pipe_format format
, const int * const swizzle
)
1051 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1052 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1053 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1054 case PIPE_FORMAT_A8B8G8R8_UNORM
:
1055 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
1056 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
1057 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
1058 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
1059 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
1060 return table_8888_unorm
[i
].format
;
1064 case PIPE_FORMAT_R10G10B10A2_UNORM
:
1065 case PIPE_FORMAT_B10G10R10A2_UNORM
:
1066 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
1067 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
1068 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
1069 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
1070 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
1071 return table_1010102_unorm
[i
].format
;
1075 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1076 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1077 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1078 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1079 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1080 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1081 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1082 return table_1010102_snorm
[i
].format
;
1086 case PIPE_FORMAT_R10G10B10A2_UINT
:
1087 case PIPE_FORMAT_B10G10R10A2_UINT
:
1088 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1089 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1090 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1091 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1092 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1093 return table_1010102_uint
[i
].format
;
1101 return PIPE_FORMAT_NONE
;
1105 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1107 enum pipe_format src
= *src_format
;
1108 enum pipe_format dst
= *dst_format
;
1110 /* Note: dst_format has already been transformed from luminance/intensity
1111 * to red when this function is called. The source format will never
1112 * be an intensity format, because GL_INTENSITY is not a legal value
1113 * for the format parameter in glTex(Sub)Image(). */
1115 if (format_is_alpha(src
)) {
1116 if (!format_is_alpha(dst
))
1119 src
= alpha_to_red(src
);
1120 dst
= alpha_to_red(dst
);
1121 } else if (format_is_luminance(src
)) {
1122 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1125 src
= util_format_luminance_to_red(src
);
1126 } else if (util_format_is_luminance_alpha(src
)) {
1127 src
= luminance_alpha_to_red_green(src
);
1129 if (format_is_red_alpha(dst
)) {
1130 dst
= red_alpha_to_red_green(dst
);
1131 } else if (!format_is_red(dst
))
1133 } else if (format_is_swizzled_rgba(src
)) {
1134 const struct util_format_description
*src_desc
= util_format_description(src
);
1135 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1139 /* Make sure the format is an RGBA and not an RGBX format */
1140 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1143 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1146 for (i
= 0; i
< 4; i
++)
1147 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1149 dst
= swizzle_format(dst
, swizzle
);
1150 if (dst
== PIPE_FORMAT_NONE
)
1153 src
= unswizzle_format(src
);
1162 try_pbo_upload_common(struct gl_context
*ctx
,
1163 struct pipe_surface
*surface
,
1164 const struct st_pbo_addresses
*addr
,
1165 enum pipe_format src_format
)
1167 struct st_context
*st
= st_context(ctx
);
1168 struct cso_context
*cso
= st
->cso_context
;
1169 struct pipe_context
*pipe
= st
->pipe
;
1170 bool success
= false;
1173 fs
= st_pbo_get_upload_fs(st
, src_format
, surface
->format
);
1177 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1178 CSO_BIT_FRAGMENT_SAMPLERS
|
1179 CSO_BIT_VERTEX_ELEMENTS
|
1180 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1181 CSO_BIT_FRAMEBUFFER
|
1184 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1185 CSO_BIT_RASTERIZER
|
1186 CSO_BIT_STREAM_OUTPUTS
|
1187 CSO_BIT_PAUSE_QUERIES
|
1188 CSO_BIT_SAMPLE_MASK
|
1189 CSO_BIT_MIN_SAMPLES
|
1190 CSO_BIT_RENDER_CONDITION
|
1191 CSO_BITS_ALL_SHADERS
));
1192 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1194 cso_set_sample_mask(cso
, ~0);
1195 cso_set_min_samples(cso
, 1);
1196 cso_set_render_condition(cso
, NULL
, FALSE
, 0);
1198 /* Set up the sampler_view */
1200 struct pipe_sampler_view templ
;
1201 struct pipe_sampler_view
*sampler_view
;
1202 struct pipe_sampler_state sampler
= {0};
1203 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
1205 memset(&templ
, 0, sizeof(templ
));
1206 templ
.target
= PIPE_BUFFER
;
1207 templ
.format
= src_format
;
1208 templ
.u
.buf
.offset
= addr
->first_element
* addr
->bytes_per_pixel
;
1209 templ
.u
.buf
.size
= (addr
->last_element
- addr
->first_element
+ 1) *
1210 addr
->bytes_per_pixel
;
1211 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1212 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1213 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1214 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1216 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1217 if (sampler_view
== NULL
)
1220 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1222 pipe_sampler_view_reference(&sampler_view
, NULL
);
1224 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
1227 /* Framebuffer_state */
1229 struct pipe_framebuffer_state fb
;
1230 memset(&fb
, 0, sizeof(fb
));
1231 fb
.width
= surface
->width
;
1232 fb
.height
= surface
->height
;
1234 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1236 cso_set_framebuffer(cso
, &fb
);
1238 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1241 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1244 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1246 /* Depth/stencil/alpha state */
1248 struct pipe_depth_stencil_alpha_state dsa
;
1249 memset(&dsa
, 0, sizeof(dsa
));
1250 cso_set_depth_stencil_alpha(cso
, &dsa
);
1253 /* Set up the fragment shader */
1254 cso_set_fragment_shader_handle(cso
, fs
);
1256 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1259 cso_restore_state(cso
);
1260 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1266 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1267 struct gl_texture_image
*texImage
,
1268 GLenum format
, GLenum type
,
1269 enum pipe_format dst_format
,
1270 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1271 GLint width
, GLint height
, GLint depth
,
1273 const struct gl_pixelstore_attrib
*unpack
)
1275 struct st_context
*st
= st_context(ctx
);
1276 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1277 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1278 struct pipe_resource
*texture
= stImage
->pt
;
1279 struct pipe_context
*pipe
= st
->pipe
;
1280 struct pipe_screen
*screen
= pipe
->screen
;
1281 struct pipe_surface
*surface
= NULL
;
1282 struct st_pbo_addresses addr
;
1283 enum pipe_format src_format
;
1284 const struct util_format_description
*desc
;
1285 GLenum gl_target
= texImage
->TexObject
->Target
;
1288 if (!st
->pbo
.upload_enabled
)
1291 /* From now on, we need the gallium representation of dimensions. */
1292 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1299 if (depth
!= 1 && !st
->pbo
.layers
)
1302 /* Choose the source format. Initially, we do so without checking driver
1303 * support at all because of the remapping we later perform and because
1304 * at least the Radeon driver actually supports some formats for texture
1305 * buffers which it doesn't support for regular textures. */
1306 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1311 src_format
= util_format_linear(src_format
);
1312 desc
= util_format_description(src_format
);
1314 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1317 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1320 if (st
->pbo
.rgba_only
) {
1321 enum pipe_format orig_dst_format
= dst_format
;
1323 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1327 if (dst_format
!= orig_dst_format
&&
1328 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1329 PIPE_BIND_RENDER_TARGET
)) {
1335 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0,
1336 PIPE_BIND_SAMPLER_VIEW
)) {
1340 /* Compute buffer addresses */
1341 addr
.xoffset
= xoffset
;
1342 addr
.yoffset
= yoffset
;
1344 addr
.height
= height
;
1346 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1348 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1352 /* Set up the surface */
1354 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1355 unsigned max_layer
= util_max_layer(texture
, level
);
1357 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1359 struct pipe_surface templ
;
1360 memset(&templ
, 0, sizeof(templ
));
1361 templ
.format
= dst_format
;
1362 templ
.u
.tex
.level
= level
;
1363 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1364 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1366 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1371 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1373 pipe_surface_reference(&surface
, NULL
);
1379 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1380 struct gl_texture_image
*texImage
,
1381 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1382 GLint width
, GLint height
, GLint depth
,
1383 GLenum format
, GLenum type
, const void *pixels
,
1384 const struct gl_pixelstore_attrib
*unpack
)
1386 struct st_context
*st
= st_context(ctx
);
1387 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1388 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1389 struct pipe_context
*pipe
= st
->pipe
;
1390 struct pipe_screen
*screen
= pipe
->screen
;
1391 struct pipe_resource
*dst
= stImage
->pt
;
1392 struct pipe_resource
*src
= NULL
;
1393 struct pipe_resource src_templ
;
1394 struct pipe_transfer
*transfer
;
1395 struct pipe_blit_info blit
;
1396 enum pipe_format src_format
, dst_format
;
1397 mesa_format mesa_src_format
;
1398 GLenum gl_target
= texImage
->TexObject
->Target
;
1401 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1402 unsigned dst_level
= 0;
1404 st_flush_bitmap_cache(st
);
1405 st_invalidate_readpix_cache(st
);
1407 if (stObj
->pt
== stImage
->pt
)
1408 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1410 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1411 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1416 /* Try texture_subdata, which should be the fastest memcpy path. */
1418 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1419 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1420 texImage
->TexFormat
, format
, type
,
1422 struct pipe_box box
;
1423 unsigned stride
, layer_stride
;
1426 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1427 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1429 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1432 /* Convert to Gallium coordinates. */
1433 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1438 layer_stride
= stride
;
1441 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1442 pipe
->texture_subdata(pipe
, dst
, dst_level
, 0,
1443 &box
, data
, stride
, layer_stride
);
1447 if (!st
->prefer_blit_based_texture_transfer
) {
1451 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1452 * blit implementation in some drivers. */
1453 if (format
== GL_DEPTH_STENCIL
) {
1457 /* If the base internal format and the texture format don't match,
1458 * we can't use blit-based TexSubImage. */
1459 if (texImage
->_BaseFormat
!=
1460 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1465 /* See if the destination format is supported. */
1466 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1467 bind
= PIPE_BIND_DEPTH_STENCIL
;
1469 bind
= PIPE_BIND_RENDER_TARGET
;
1471 /* For luminance and intensity, only the red channel is stored
1472 * in the destination. */
1473 dst_format
= util_format_linear(dst
->format
);
1474 dst_format
= util_format_luminance_to_red(dst_format
);
1475 dst_format
= util_format_intensity_to_red(dst_format
);
1478 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1479 dst
->nr_samples
, bind
)) {
1483 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1484 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1485 xoffset
, yoffset
, zoffset
,
1486 width
, height
, depth
, pixels
, unpack
))
1490 /* See if the texture format already matches the format and type,
1491 * in which case the memcpy-based fast path will likely be used and
1492 * we don't have to blit. */
1493 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1494 type
, unpack
->SwapBytes
, NULL
)) {
1498 /* Choose the source format. */
1499 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1500 format
, type
, unpack
->SwapBytes
);
1505 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1507 /* There is no reason to do this if we cannot use memcpy for the temporary
1508 * source texture at least. This also takes transfer ops into account,
1510 if (!_mesa_texstore_can_use_memcpy(ctx
,
1511 _mesa_get_format_base_format(mesa_src_format
),
1512 mesa_src_format
, format
, type
, unpack
)) {
1516 /* TexSubImage only sets a single cubemap face. */
1517 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1518 gl_target
= GL_TEXTURE_2D
;
1520 /* TexSubImage can specify subsets of cube map array faces
1521 * so we need to upload via 2D array instead */
1522 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1523 gl_target
= GL_TEXTURE_2D_ARRAY
;
1526 /* Initialize the source texture description. */
1527 memset(&src_templ
, 0, sizeof(src_templ
));
1528 src_templ
.target
= gl_target_to_pipe(gl_target
);
1529 src_templ
.format
= src_format
;
1530 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1531 src_templ
.usage
= PIPE_USAGE_STAGING
;
1533 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1534 &src_templ
.width0
, &src_templ
.height0
,
1535 &src_templ
.depth0
, &src_templ
.array_size
);
1537 /* Check for NPOT texture support. */
1538 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1539 (!util_is_power_of_two_or_zero(src_templ
.width0
) ||
1540 !util_is_power_of_two_or_zero(src_templ
.height0
) ||
1541 !util_is_power_of_two_or_zero(src_templ
.depth0
))) {
1545 /* Create the source texture. */
1546 src
= screen
->resource_create(screen
, &src_templ
);
1551 /* Map source pixels. */
1552 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1553 format
, type
, pixels
, unpack
,
1556 /* This is a GL error. */
1557 pipe_resource_reference(&src
, NULL
);
1561 /* From now on, we need the gallium representation of dimensions. */
1562 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1569 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1570 width
, height
, depth
, &transfer
);
1572 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1573 pipe_resource_reference(&src
, NULL
);
1577 /* Upload pixels (just memcpy). */
1579 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1582 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1583 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1584 /* 1D array textures.
1585 * We need to convert gallium coords to GL coords.
1587 void *src
= _mesa_image_address2d(unpack
, pixels
,
1588 width
, depth
, format
,
1590 memcpy(map
, src
, bytesPerRow
);
1593 ubyte
*slice_map
= map
;
1595 for (row
= 0; row
< (unsigned) height
; row
++) {
1596 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1597 width
, height
, format
,
1598 type
, slice
, row
, 0);
1599 memcpy(slice_map
, src
, bytesPerRow
);
1600 slice_map
+= transfer
->stride
;
1603 map
+= transfer
->layer_stride
;
1607 pipe_transfer_unmap(pipe
, transfer
);
1608 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1611 memset(&blit
, 0, sizeof(blit
));
1612 blit
.src
.resource
= src
;
1614 blit
.src
.format
= src_format
;
1615 blit
.dst
.resource
= dst
;
1616 blit
.dst
.level
= dst_level
;
1617 blit
.dst
.format
= dst_format
;
1618 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1619 blit
.dst
.box
.x
= xoffset
;
1620 blit
.dst
.box
.y
= yoffset
;
1621 blit
.dst
.box
.z
= zoffset
+ dstz
;
1622 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1623 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1624 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1625 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1626 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1627 blit
.scissor_enable
= FALSE
;
1629 st
->pipe
->blit(st
->pipe
, &blit
);
1631 pipe_resource_reference(&src
, NULL
);
1635 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1636 width
, height
, depth
, format
, type
, pixels
,
1641 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1642 struct gl_texture_image
*texImage
,
1643 GLenum format
, GLenum type
, const void *pixels
,
1644 const struct gl_pixelstore_attrib
*unpack
)
1646 assert(dims
== 1 || dims
== 2 || dims
== 3);
1648 prep_teximage(ctx
, texImage
, format
, type
);
1650 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1653 /* allocate storage for texture data */
1654 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1655 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1659 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1660 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1661 format
, type
, pixels
, unpack
);
1666 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1667 struct gl_texture_image
*texImage
,
1668 GLint x
, GLint y
, GLint z
,
1669 GLsizei w
, GLsizei h
, GLsizei d
,
1670 GLenum format
, GLsizei imageSize
, const void *data
)
1672 struct st_context
*st
= st_context(ctx
);
1673 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1674 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1675 struct pipe_resource
*texture
= stImage
->pt
;
1676 struct pipe_context
*pipe
= st
->pipe
;
1677 struct pipe_screen
*screen
= pipe
->screen
;
1678 struct pipe_resource
*dst
= stImage
->pt
;
1679 struct pipe_surface
*surface
= NULL
;
1680 struct compressed_pixelstore store
;
1681 struct st_pbo_addresses addr
;
1682 enum pipe_format copy_format
;
1684 intptr_t buf_offset
;
1685 bool success
= false;
1687 /* Check basic pre-conditions for PBO upload */
1688 if (!st
->prefer_blit_based_texture_transfer
) {
1692 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1695 if (st_compressed_format_fallback(st
, texImage
->TexFormat
))
1702 if (!st
->pbo
.upload_enabled
||
1703 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1707 /* Choose the pipe format for the upload. */
1708 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1709 bw
= util_format_get_blockwidth(dst
->format
);
1710 bh
= util_format_get_blockheight(dst
->format
);
1712 switch (addr
.bytes_per_pixel
) {
1714 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1717 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1723 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0,
1724 PIPE_BIND_SAMPLER_VIEW
)) {
1728 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1729 dst
->nr_samples
, PIPE_BIND_RENDER_TARGET
)) {
1733 /* Interpret the pixelstore settings. */
1734 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1735 &ctx
->Unpack
, &store
);
1736 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1737 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1739 /* Compute the offset into the buffer */
1740 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1742 if (buf_offset
% addr
.bytes_per_pixel
) {
1746 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1748 addr
.xoffset
= x
/ bw
;
1749 addr
.yoffset
= y
/ bh
;
1750 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1751 addr
.height
= store
.CopyRowsPerSlice
;
1753 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1754 addr
.image_height
= store
.TotalRowsPerSlice
;
1756 if (!st_pbo_addresses_setup(st
, st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1760 /* Set up the surface. */
1762 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1763 unsigned max_layer
= util_max_layer(texture
, level
);
1765 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1767 struct pipe_surface templ
;
1768 memset(&templ
, 0, sizeof(templ
));
1769 templ
.format
= copy_format
;
1770 templ
.u
.tex
.level
= level
;
1771 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1772 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1774 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1779 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1781 pipe_surface_reference(&surface
, NULL
);
1787 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1789 format
, imageSize
, data
);
1793 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1794 struct gl_texture_image
*texImage
,
1795 GLsizei imageSize
, const void *data
)
1797 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1799 /* only 2D and 3D compressed images are supported at this time */
1801 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1805 /* This is pretty simple, because unlike the general texstore path we don't
1806 * have to worry about the usual image unpacking or image transfer
1810 assert(texImage
->Width
> 0);
1811 assert(texImage
->Height
> 0);
1812 assert(texImage
->Depth
> 0);
1814 /* allocate storage for texture data */
1815 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1816 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1820 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1822 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1823 texImage
->TexFormat
,
1831 * Called via ctx->Driver.GetTexSubImage()
1833 * This uses a blit to copy the texture to a texture format which matches
1834 * the format and type combo and then a fast read-back is done using memcpy.
1835 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1836 * a format which matches the swizzling.
1838 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1840 * NOTE: Drivers usually do a blit to convert between tiled and linear
1841 * texture layouts during texture uploads/downloads, so the blit
1842 * we do here should be free in such cases.
1845 st_GetTexSubImage(struct gl_context
* ctx
,
1846 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1847 GLsizei width
, GLsizei height
, GLint depth
,
1848 GLenum format
, GLenum type
, void * pixels
,
1849 struct gl_texture_image
*texImage
)
1851 struct st_context
*st
= st_context(ctx
);
1852 struct pipe_context
*pipe
= st
->pipe
;
1853 struct pipe_screen
*screen
= pipe
->screen
;
1854 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1855 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1856 struct pipe_resource
*src
= stObj
->pt
;
1857 struct pipe_resource
*dst
= NULL
;
1858 struct pipe_resource dst_templ
;
1859 enum pipe_format dst_format
, src_format
;
1860 mesa_format mesa_format
;
1861 GLenum gl_target
= texImage
->TexObject
->Target
;
1862 enum pipe_texture_target pipe_target
;
1864 struct pipe_blit_info blit
;
1866 struct pipe_transfer
*tex_xfer
;
1868 boolean done
= FALSE
;
1870 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1871 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1873 st_flush_bitmap_cache(st
);
1875 if (!st
->prefer_blit_based_texture_transfer
&&
1876 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1877 /* Try to avoid the fallback if we're doing texture decompression here */
1881 /* Handle non-finalized textures. */
1882 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1886 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1887 * due to an incomplete stencil blit implementation in some drivers. */
1888 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1892 /* If the base internal format and the texture format don't match, we have
1893 * to fall back to _mesa_GetTexImage_sw. */
1894 if (texImage
->_BaseFormat
!=
1895 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1899 /* See if the texture format already matches the format and type,
1900 * in which case the memcpy-based fast path will be used. */
1901 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1902 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1906 /* Convert the source format to what is expected by GetTexImage
1907 * and see if it's supported.
1909 * This only applies to glGetTexImage:
1910 * - Luminance must be returned as (L,0,0,1).
1911 * - Luminance alpha must be returned as (L,0,0,A).
1912 * - Intensity must be returned as (I,0,0,1)
1914 if (stObj
->surface_based
)
1915 src_format
= util_format_linear(stObj
->surface_format
);
1917 src_format
= util_format_linear(src
->format
);
1918 src_format
= util_format_luminance_to_red(src_format
);
1919 src_format
= util_format_intensity_to_red(src_format
);
1922 !screen
->is_format_supported(screen
, src_format
, src
->target
,
1924 PIPE_BIND_SAMPLER_VIEW
)) {
1928 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1929 bind
= PIPE_BIND_DEPTH_STENCIL
;
1931 bind
= PIPE_BIND_RENDER_TARGET
;
1933 /* GetTexImage only returns a single face for cubemaps. */
1934 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1935 gl_target
= GL_TEXTURE_2D
;
1937 pipe_target
= gl_target_to_pipe(gl_target
);
1939 /* Choose the destination format by finding the best match
1940 * for the format+type combo. */
1941 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1942 ctx
->Pack
.SwapBytes
);
1944 if (dst_format
== PIPE_FORMAT_NONE
) {
1945 GLenum dst_glformat
;
1947 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1948 * where decompression with a blit is always preferred. */
1949 if (!util_format_is_compressed(src
->format
)) {
1953 /* Set the appropriate format for the decompressed texture.
1954 * Luminance and sRGB formats shouldn't appear here.*/
1955 switch (src_format
) {
1956 case PIPE_FORMAT_DXT1_RGB
:
1957 case PIPE_FORMAT_DXT1_RGBA
:
1958 case PIPE_FORMAT_DXT3_RGBA
:
1959 case PIPE_FORMAT_DXT5_RGBA
:
1960 case PIPE_FORMAT_RGTC1_UNORM
:
1961 case PIPE_FORMAT_RGTC2_UNORM
:
1962 case PIPE_FORMAT_ETC1_RGB8
:
1963 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1964 dst_glformat
= GL_RGBA8
;
1966 case PIPE_FORMAT_RGTC1_SNORM
:
1967 case PIPE_FORMAT_RGTC2_SNORM
:
1968 if (!ctx
->Extensions
.EXT_texture_snorm
)
1970 dst_glformat
= GL_RGBA8_SNORM
;
1972 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1973 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1974 if (!ctx
->Extensions
.ARB_texture_float
)
1976 dst_glformat
= GL_RGBA32F
;
1983 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
1984 pipe_target
, 0, bind
, FALSE
);
1986 if (dst_format
== PIPE_FORMAT_NONE
) {
1987 /* unable to get an rgba format!?! */
1992 /* create the destination texture of size (width X height X depth) */
1993 memset(&dst_templ
, 0, sizeof(dst_templ
));
1994 dst_templ
.target
= pipe_target
;
1995 dst_templ
.format
= dst_format
;
1996 dst_templ
.bind
= bind
;
1997 dst_templ
.usage
= PIPE_USAGE_STAGING
;
1999 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
2000 &dst_templ
.width0
, &dst_templ
.height0
,
2001 &dst_templ
.depth0
, &dst_templ
.array_size
);
2003 dst
= screen
->resource_create(screen
, &dst_templ
);
2008 /* From now on, we need the gallium representation of dimensions. */
2009 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2016 assert(texImage
->Face
== 0 ||
2017 texImage
->TexObject
->MinLayer
== 0 ||
2020 memset(&blit
, 0, sizeof(blit
));
2021 blit
.src
.resource
= src
;
2022 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2023 blit
.src
.format
= src_format
;
2024 blit
.dst
.resource
= dst
;
2026 blit
.dst
.format
= dst
->format
;
2027 blit
.src
.box
.x
= xoffset
;
2029 blit
.src
.box
.y
= yoffset
;
2031 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2033 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2034 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2035 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2036 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2037 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2038 blit
.scissor_enable
= FALSE
;
2040 /* blit/render/decompress */
2041 st
->pipe
->blit(st
->pipe
, &blit
);
2043 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2045 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2046 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2051 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2052 dims
= _mesa_get_texture_dimensions(gl_target
);
2054 /* copy/pack data into user buffer */
2055 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2056 ctx
->Pack
.SwapBytes
, NULL
)) {
2058 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2061 for (slice
= 0; slice
< depth
; slice
++) {
2062 ubyte
*slice_map
= map
;
2064 for (row
= 0; row
< height
; row
++) {
2065 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2066 width
, height
, format
, type
,
2069 memcpy(dest
, slice_map
, bytesPerRow
);
2071 slice_map
+= tex_xfer
->stride
;
2074 map
+= tex_xfer
->layer_stride
;
2078 /* format translation via floats */
2081 uint32_t dstMesaFormat
;
2082 int dstStride
, srcStride
;
2084 assert(util_format_is_compressed(src
->format
));
2086 rgba
= malloc(width
* height
* 4 * sizeof(GLfloat
));
2091 if (ST_DEBUG
& DEBUG_FALLBACK
)
2092 debug_printf("%s: fallback format translation\n", __func__
);
2094 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2095 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2096 srcStride
= 4 * width
* sizeof(GLfloat
);
2097 for (slice
= 0; slice
< depth
; slice
++) {
2098 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2099 width
, height
, format
, type
,
2102 /* get float[4] rgba row from surface */
2103 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, height
,
2106 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2107 rgba
, RGBA32_FLOAT
, srcStride
,
2108 width
, height
, NULL
);
2110 /* Handle byte swapping if required */
2111 if (ctx
->Pack
.SwapBytes
) {
2112 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
2113 width
, height
, dest
, dest
);
2116 map
+= tex_xfer
->layer_stride
;
2125 pipe_transfer_unmap(pipe
, tex_xfer
);
2127 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2128 pipe_resource_reference(&dst
, NULL
);
2132 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2133 width
, height
, depth
,
2134 format
, type
, pixels
, texImage
);
2140 * Do a CopyTexSubImage operation using a read transfer from the source,
2141 * a write transfer to the destination and get_tile()/put_tile() to access
2142 * the pixels/texels.
2144 * Note: srcY=0=TOP of renderbuffer
2147 fallback_copy_texsubimage(struct gl_context
*ctx
,
2148 struct st_renderbuffer
*strb
,
2149 struct st_texture_image
*stImage
,
2151 GLint destX
, GLint destY
, GLint slice
,
2152 GLint srcX
, GLint srcY
,
2153 GLsizei width
, GLsizei height
)
2155 struct st_context
*st
= st_context(ctx
);
2156 struct pipe_context
*pipe
= st
->pipe
;
2157 struct pipe_transfer
*src_trans
;
2159 enum pipe_transfer_usage transfer_usage
;
2161 unsigned dst_width
= width
;
2162 unsigned dst_height
= height
;
2163 unsigned dst_depth
= 1;
2164 struct pipe_transfer
*transfer
;
2166 if (ST_DEBUG
& DEBUG_FALLBACK
)
2167 debug_printf("%s: fallback processing\n", __func__
);
2169 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2170 srcY
= strb
->Base
.Height
- srcY
- height
;
2173 map
= pipe_transfer_map(pipe
,
2175 strb
->surface
->u
.tex
.level
,
2176 strb
->surface
->u
.tex
.first_layer
,
2179 width
, height
, &src_trans
);
2181 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2182 baseFormat
== GL_DEPTH_STENCIL
) &&
2183 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2184 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2186 transfer_usage
= PIPE_TRANSFER_WRITE
;
2188 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2189 destX
, destY
, slice
,
2190 dst_width
, dst_height
, dst_depth
,
2193 if (baseFormat
== GL_DEPTH_COMPONENT
||
2194 baseFormat
== GL_DEPTH_STENCIL
) {
2195 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2196 ctx
->Pixel
.DepthBias
!= 0.0F
);
2200 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2201 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2210 data
= malloc(width
* sizeof(uint
));
2213 /* To avoid a large temp memory allocation, do copy row by row */
2214 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2215 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2217 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2220 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2221 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2222 0, 0, width
, 1, data
);
2225 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2230 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2238 malloc(width
* height
* 4 * sizeof(GLfloat
));
2240 if (tempSrc
&& texDest
) {
2241 const GLint dims
= 2;
2243 struct gl_texture_image
*texImage
= &stImage
->base
;
2244 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2246 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2247 unpack
.Invert
= GL_TRUE
;
2250 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2251 dstRowStride
= transfer
->layer_stride
;
2254 dstRowStride
= transfer
->stride
;
2257 /* get float/RGBA image from framebuffer */
2258 /* XXX this usually involves a lot of int/float conversion.
2259 * try to avoid that someday.
2261 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2262 util_format_linear(strb
->texture
->format
),
2265 /* Store into texture memory.
2266 * Note that this does some special things such as pixel transfer
2267 * ops and format conversion. In particular, if the dest tex format
2268 * is actually RGBA but the user created the texture as GL_RGB we
2269 * need to fill-in/override the alpha channel with 1.0.
2271 _mesa_texstore(ctx
, dims
,
2272 texImage
->_BaseFormat
,
2273 texImage
->TexFormat
,
2277 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2281 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2287 st_texture_image_unmap(st
, stImage
, slice
);
2288 pipe
->transfer_unmap(pipe
, src_trans
);
2292 st_can_copyteximage_using_blit(const struct gl_texture_image
*texImage
,
2293 const struct gl_renderbuffer
*rb
)
2295 GLenum tex_baseformat
= _mesa_get_format_base_format(texImage
->TexFormat
);
2297 /* We don't blit to a teximage where the GL base format doesn't match the
2298 * texture's chosen format, except in the case of a GL_RGB texture
2299 * represented with GL_RGBA (where the alpha channel is just being
2302 if (texImage
->_BaseFormat
!= tex_baseformat
&&
2303 ((texImage
->_BaseFormat
!= GL_RGB
|| tex_baseformat
!= GL_RGBA
))) {
2307 /* We can't blit from a RB where the GL base format doesn't match the RB's
2308 * chosen format (for example, GL RGB or ALPHA with rb->Format of an RGBA
2309 * type, because the other channels will be undefined).
2311 if (rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
))
2318 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2319 * Note that the region to copy has already been clipped so we know we
2320 * won't read from outside the source renderbuffer's bounds.
2322 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2325 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2326 struct gl_texture_image
*texImage
,
2327 GLint destX
, GLint destY
, GLint slice
,
2328 struct gl_renderbuffer
*rb
,
2329 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2331 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2332 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2333 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2334 struct st_context
*st
= st_context(ctx
);
2335 struct pipe_context
*pipe
= st
->pipe
;
2336 struct pipe_screen
*screen
= pipe
->screen
;
2337 struct pipe_blit_info blit
;
2338 enum pipe_format dst_format
;
2339 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2343 st_flush_bitmap_cache(st
);
2344 st_invalidate_readpix_cache(st
);
2346 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2347 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2349 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2350 debug_printf("%s: null strb or stImage\n", __func__
);
2354 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2355 texImage
->TexFormat
)) {
2359 if (!st_can_copyteximage_using_blit(texImage
, rb
)) {
2363 /* Choose the destination format to match the TexImage behavior. */
2364 dst_format
= util_format_linear(stImage
->pt
->format
);
2365 dst_format
= util_format_luminance_to_red(dst_format
);
2366 dst_format
= util_format_intensity_to_red(dst_format
);
2368 /* See if the destination format is supported. */
2369 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2370 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2371 bind
= PIPE_BIND_DEPTH_STENCIL
;
2374 bind
= PIPE_BIND_RENDER_TARGET
;
2378 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2379 stImage
->pt
->nr_samples
, bind
)) {
2383 /* Y flipping for the main framebuffer. */
2385 srcY1
= strb
->Base
.Height
- srcY
- height
;
2386 srcY0
= srcY1
+ height
;
2390 srcY1
= srcY0
+ height
;
2393 /* Blit the texture.
2394 * This supports flipping, format conversions, and downsampling.
2396 memset(&blit
, 0, sizeof(blit
));
2397 blit
.src
.resource
= strb
->texture
;
2398 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2399 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2400 blit
.src
.box
.x
= srcX
;
2401 blit
.src
.box
.y
= srcY0
;
2402 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2403 blit
.src
.box
.width
= width
;
2404 blit
.src
.box
.height
= srcY1
- srcY0
;
2405 blit
.src
.box
.depth
= 1;
2406 blit
.dst
.resource
= stImage
->pt
;
2407 blit
.dst
.format
= dst_format
;
2408 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2409 blit
.dst
.box
.x
= destX
;
2410 blit
.dst
.box
.y
= destY
;
2411 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2412 blit
.dst
.box
.width
= width
;
2413 blit
.dst
.box
.height
= height
;
2414 blit
.dst
.box
.depth
= 1;
2415 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2416 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2417 pipe
->blit(pipe
, &blit
);
2421 /* software fallback */
2422 fallback_copy_texsubimage(ctx
,
2423 strb
, stImage
, texImage
->_BaseFormat
,
2424 destX
, destY
, slice
,
2425 srcX
, srcY
, width
, height
);
2430 * Copy image data from stImage into the texture object 'stObj' at level
2434 copy_image_data_to_texture(struct st_context
*st
,
2435 struct st_texture_object
*stObj
,
2437 struct st_texture_image
*stImage
)
2441 const struct gl_texture_image MAYBE_UNUSED
*dstImage
=
2442 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2444 assert(dstImage
->Width
== stImage
->base
.Width
);
2445 assert(dstImage
->Height
== stImage
->base
.Height
);
2446 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2450 /* Copy potentially with the blitter:
2453 if (stImage
->pt
->last_level
== 0)
2456 src_level
= stImage
->base
.Level
;
2458 assert(src_level
<= stImage
->pt
->last_level
);
2459 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2460 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2461 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2462 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2463 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2464 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2466 st_texture_image_copy(st
->pipe
,
2467 stObj
->pt
, dstLevel
, /* dest texture, level */
2468 stImage
->pt
, src_level
, /* src texture, level */
2469 stImage
->base
.Face
);
2471 pipe_resource_reference(&stImage
->pt
, NULL
);
2473 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2478 * Called during state validation. When this function is finished,
2479 * the texture object should be ready for rendering.
2480 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2483 st_finalize_texture(struct gl_context
*ctx
,
2484 struct pipe_context
*pipe
,
2485 struct gl_texture_object
*tObj
,
2488 struct st_context
*st
= st_context(ctx
);
2489 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2490 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2492 const struct st_texture_image
*firstImage
;
2493 enum pipe_format firstImageFormat
;
2495 uint16_t ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2497 if (tObj
->Immutable
)
2500 if (tObj
->_MipmapComplete
)
2501 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2502 else if (tObj
->_BaseComplete
)
2503 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2505 /* Skip the loop over images in the common case of no images having
2506 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
2507 * haven't looked at, then we do need to look at those new images.
2509 if (!stObj
->needs_validation
&&
2510 stObj
->base
.BaseLevel
>= stObj
->validated_first_level
&&
2511 stObj
->lastLevel
<= stObj
->validated_last_level
) {
2515 /* If this texture comes from a window system, there is nothing else to do. */
2516 if (stObj
->surface_based
) {
2520 firstImage
= st_texture_image_const(stObj
->base
.Image
[cubeMapFace
][stObj
->base
.BaseLevel
]);
2523 /* If both firstImage and stObj point to a texture which can contain
2524 * all active images, favour firstImage. Note that because of the
2525 * completeness requirement, we know that the image dimensions
2528 if (firstImage
->pt
&&
2529 firstImage
->pt
!= stObj
->pt
&&
2530 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2531 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2532 st_texture_release_all_sampler_views(st
, stObj
);
2535 /* Find gallium format for the Mesa texture */
2537 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2539 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2542 uint16_t height
, depth
;
2544 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2545 firstImage
->base
.Width2
,
2546 firstImage
->base
.Height2
,
2547 firstImage
->base
.Depth2
,
2548 &width
, &height
, &depth
, &ptLayers
);
2550 /* If we previously allocated a pipe texture and its sizes are
2551 * compatible, use them.
2554 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2555 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2556 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2557 ptWidth
= stObj
->pt
->width0
;
2558 ptHeight
= stObj
->pt
->height0
;
2559 ptDepth
= stObj
->pt
->depth0
;
2561 /* Otherwise, compute a new level=0 size that is compatible with the
2564 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2565 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2566 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2568 /* If the base level image is 1x1x1, we still need to ensure that the
2569 * resulting pipe texture ends up with the required number of levels
2572 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2573 ptWidth
<<= firstImage
->base
.Level
;
2575 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2576 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2580 /* At this point, the texture may be incomplete (mismatched cube
2581 * face sizes, for example). If that's the case, give up, but
2582 * don't return GL_FALSE as that would raise an incorrect
2583 * GL_OUT_OF_MEMORY error. See Piglit fbo-incomplete-texture-03 test.
2585 if (!stObj
->base
._BaseComplete
) {
2586 _mesa_test_texobj_completeness(ctx
, &stObj
->base
);
2587 if (!stObj
->base
._BaseComplete
) {
2593 ptNumSamples
= firstImage
->base
.NumSamples
;
2596 /* If we already have a gallium texture, check that it matches the texture
2597 * object's format, target, size, num_levels, etc.
2600 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2601 stObj
->pt
->format
!= firstImageFormat
||
2602 stObj
->pt
->last_level
< stObj
->lastLevel
||
2603 stObj
->pt
->width0
!= ptWidth
||
2604 stObj
->pt
->height0
!= ptHeight
||
2605 stObj
->pt
->depth0
!= ptDepth
||
2606 stObj
->pt
->nr_samples
!= ptNumSamples
||
2607 stObj
->pt
->array_size
!= ptLayers
)
2609 /* The gallium texture does not match the Mesa texture so delete the
2610 * gallium texture now. We'll make a new one below.
2612 pipe_resource_reference(&stObj
->pt
, NULL
);
2613 st_texture_release_all_sampler_views(st
, stObj
);
2614 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
2618 /* May need to create a new gallium texture:
2621 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2623 stObj
->pt
= st_texture_create(st
,
2624 gl_target_to_pipe(stObj
->base
.Target
),
2630 ptLayers
, ptNumSamples
,
2634 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2639 /* Pull in any images not in the object's texture:
2641 for (face
= 0; face
< nr_faces
; face
++) {
2643 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2644 struct st_texture_image
*stImage
=
2645 st_texture_image(stObj
->base
.Image
[face
][level
]);
2647 /* Need to import images in main memory or held in other textures.
2649 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2653 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2654 height
= u_minify(ptHeight
, level
);
2658 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2659 depth
= u_minify(ptDepth
, level
);
2660 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2666 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2667 stImage
->base
.Height
== height
&&
2668 stImage
->base
.Depth
== depth
)) {
2669 /* src image fits expected dest mipmap level size */
2670 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2676 stObj
->validated_first_level
= stObj
->base
.BaseLevel
;
2677 stObj
->validated_last_level
= stObj
->lastLevel
;
2678 stObj
->needs_validation
= false;
2684 * Allocate a new pipe_resource object
2685 * width0, height0, depth0 are the dimensions of the level 0 image
2686 * (the highest resolution). last_level indicates how many mipmap levels
2687 * to allocate storage for. For non-mipmapped textures, this will be zero.
2689 static struct pipe_resource
*
2690 st_texture_create_from_memory(struct st_context
*st
,
2691 struct st_memory_object
*memObj
,
2693 enum pipe_texture_target target
,
2694 enum pipe_format format
,
2703 struct pipe_resource pt
, *newtex
;
2704 struct pipe_screen
*screen
= st
->pipe
->screen
;
2706 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
2708 assert(height0
> 0);
2710 if (target
== PIPE_TEXTURE_CUBE
)
2711 assert(layers
== 6);
2713 DBG("%s target %d format %s last_level %d\n", __func__
,
2714 (int) target
, util_format_name(format
), last_level
);
2717 assert(screen
->is_format_supported(screen
, format
, target
, 0,
2718 PIPE_BIND_SAMPLER_VIEW
));
2720 memset(&pt
, 0, sizeof(pt
));
2723 pt
.last_level
= last_level
;
2725 pt
.height0
= height0
;
2727 pt
.array_size
= layers
;
2728 pt
.usage
= PIPE_USAGE_DEFAULT
;
2730 /* only set this for OpenGL textures, not renderbuffers */
2731 pt
.flags
= PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
;
2732 pt
.nr_samples
= nr_samples
;
2734 newtex
= screen
->resource_from_memobj(screen
, &pt
, memObj
->memory
, offset
);
2736 assert(!newtex
|| pipe_is_referenced(&newtex
->reference
));
2742 * Allocate texture memory for a whole mipmap stack.
2743 * Note: for multisample textures if the requested sample count is not
2744 * supported, we search for the next higher supported sample count.
2747 st_texture_storage(struct gl_context
*ctx
,
2748 struct gl_texture_object
*texObj
,
2749 GLsizei levels
, GLsizei width
,
2750 GLsizei height
, GLsizei depth
,
2751 struct gl_memory_object
*memObj
,
2754 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2755 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2756 struct st_context
*st
= st_context(ctx
);
2757 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2758 struct st_memory_object
*smObj
= st_memory_object(memObj
);
2759 struct pipe_screen
*screen
= st
->pipe
->screen
;
2760 unsigned ptWidth
, bindings
;
2761 uint16_t ptHeight
, ptDepth
, ptLayers
;
2762 enum pipe_format fmt
;
2764 GLuint num_samples
= texImage
->NumSamples
;
2768 stObj
->lastLevel
= levels
- 1;
2770 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2772 bindings
= default_bindings(st
, fmt
);
2774 if (num_samples
> 0) {
2775 /* Find msaa sample count which is actually supported. For example,
2776 * if the user requests 1x but only 4x or 8x msaa is supported, we'll
2779 enum pipe_texture_target ptarget
= gl_target_to_pipe(texObj
->Target
);
2780 boolean found
= FALSE
;
2782 if (ctx
->Const
.MaxSamples
> 1 && num_samples
== 1) {
2783 /* don't try num_samples = 1 with drivers that support real msaa */
2787 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2788 if (screen
->is_format_supported(screen
, fmt
, ptarget
,
2790 PIPE_BIND_SAMPLER_VIEW
)) {
2791 /* Update the sample count in gl_texture_image as well. */
2792 texImage
->NumSamples
= num_samples
;
2803 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2804 width
, height
, depth
,
2805 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2808 stObj
->pt
= st_texture_create_from_memory(st
,
2811 gl_target_to_pipe(texObj
->Target
),
2817 ptLayers
, num_samples
,
2821 stObj
->pt
= st_texture_create(st
,
2822 gl_target_to_pipe(texObj
->Target
),
2828 ptLayers
, num_samples
,
2835 /* Set image resource pointers */
2836 for (level
= 0; level
< levels
; level
++) {
2838 for (face
= 0; face
< numFaces
; face
++) {
2839 struct st_texture_image
*stImage
=
2840 st_texture_image(texObj
->Image
[face
][level
]);
2841 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2843 etc_fallback_allocate(st
, stImage
);
2847 /* The texture is in a validated state, so no need to check later. */
2848 stObj
->needs_validation
= false;
2849 stObj
->validated_first_level
= 0;
2850 stObj
->validated_last_level
= levels
- 1;
2856 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2857 * for a whole mipmap stack.
2860 st_AllocTextureStorage(struct gl_context
*ctx
,
2861 struct gl_texture_object
*texObj
,
2862 GLsizei levels
, GLsizei width
,
2863 GLsizei height
, GLsizei depth
)
2865 return st_texture_storage(ctx
, texObj
, levels
,
2866 width
, height
, depth
,
2872 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2873 GLuint numLevels
, GLint level
,
2874 mesa_format format
, GLuint numSamples
,
2875 GLint width
, GLint height
, GLint depth
)
2877 struct st_context
*st
= st_context(ctx
);
2878 struct pipe_context
*pipe
= st
->pipe
;
2880 if (width
== 0 || height
== 0 || depth
== 0) {
2881 /* zero-sized images are legal, and always fit! */
2885 if (pipe
->screen
->can_create_resource
) {
2886 /* Ask the gallium driver if the texture is too large */
2887 struct gl_texture_object
*texObj
=
2888 _mesa_get_current_tex_object(ctx
, target
);
2889 struct pipe_resource pt
;
2891 /* Setup the pipe_resource object
2893 memset(&pt
, 0, sizeof(pt
));
2895 pt
.target
= gl_target_to_pipe(target
);
2896 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
2897 pt
.nr_samples
= numSamples
;
2899 st_gl_texture_dims_to_pipe_dims(target
,
2900 width
, height
, depth
,
2901 &pt
.width0
, &pt
.height0
,
2902 &pt
.depth0
, &pt
.array_size
);
2904 if (numLevels
> 0) {
2905 /* For immutable textures we know the final number of mip levels */
2906 pt
.last_level
= numLevels
- 1;
2908 else if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
2909 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
2910 /* assume just one mipmap level */
2914 /* assume a full set of mipmaps */
2915 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
2918 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
2921 /* Use core Mesa fallback */
2922 return _mesa_test_proxy_teximage(ctx
, target
, numLevels
, level
, format
,
2923 numSamples
, width
, height
, depth
);
2928 st_TextureView(struct gl_context
*ctx
,
2929 struct gl_texture_object
*texObj
,
2930 struct gl_texture_object
*origTexObj
)
2932 struct st_context
*st
= st_context(ctx
);
2933 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
2934 struct st_texture_object
*tex
= st_texture_object(texObj
);
2935 struct gl_texture_image
*image
= texObj
->Image
[0][0];
2937 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2938 const int numLevels
= texObj
->NumLevels
;
2943 pipe_resource_reference(&tex
->pt
, orig
->pt
);
2945 /* Set image resource pointers */
2946 for (level
= 0; level
< numLevels
; level
++) {
2947 for (face
= 0; face
< numFaces
; face
++) {
2948 struct st_texture_image
*stImage
=
2949 st_texture_image(texObj
->Image
[face
][level
]);
2950 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
2954 tex
->surface_based
= GL_TRUE
;
2955 tex
->surface_format
=
2956 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
2958 tex
->lastLevel
= numLevels
- 1;
2960 /* free texture sampler views. They need to be recreated when we
2961 * change the texture view parameters.
2963 st_texture_release_all_sampler_views(st
, tex
);
2965 /* The texture is in a validated state, so no need to check later. */
2966 tex
->needs_validation
= false;
2967 tex
->validated_first_level
= 0;
2968 tex
->validated_last_level
= numLevels
- 1;
2975 * Find the mipmap level in 'pt' which matches the level described by
2979 find_mipmap_level(const struct gl_texture_image
*texImage
,
2980 const struct pipe_resource
*pt
)
2982 const GLenum target
= texImage
->TexObject
->Target
;
2983 GLint texWidth
= texImage
->Width
;
2984 GLint texHeight
= texImage
->Height
;
2985 GLint texDepth
= texImage
->Depth
;
2987 uint16_t h
, d
, layers
;
2989 st_gl_texture_dims_to_pipe_dims(target
, texWidth
, texHeight
, texDepth
,
2990 &w
, &h
, &d
, &layers
);
2992 for (level
= 0; level
<= pt
->last_level
; level
++) {
2993 if (u_minify(pt
->width0
, level
) == w
&&
2994 u_minify(pt
->height0
, level
) == h
&&
2995 u_minify(pt
->depth0
, level
) == d
) {
3000 /* If we get here, there must be some sort of inconsistency between
3001 * the Mesa texture object/images and the gallium resource.
3003 debug_printf("Inconsistent textures in find_mipmap_level()\n");
3005 return texImage
->Level
;
3010 st_ClearTexSubImage(struct gl_context
*ctx
,
3011 struct gl_texture_image
*texImage
,
3012 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3013 GLsizei width
, GLsizei height
, GLsizei depth
,
3014 const void *clearValue
)
3016 static const char zeros
[16] = {0};
3017 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3018 struct st_texture_image
*stImage
= st_texture_image(texImage
);
3019 struct pipe_resource
*pt
= stImage
->pt
;
3020 struct st_context
*st
= st_context(ctx
);
3021 struct pipe_context
*pipe
= st
->pipe
;
3023 struct pipe_box box
;
3028 st_flush_bitmap_cache(st
);
3029 st_invalidate_readpix_cache(st
);
3031 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
3032 width
, height
, depth
, &box
);
3033 if (texObj
->Immutable
) {
3034 /* The texture object has to be consistent (no "loose", per-image
3035 * gallium resources). If this texture is a view into another
3036 * texture, we have to apply the MinLevel/Layer offsets. If this is
3037 * not a texture view, the offsets will be zero.
3039 assert(stImage
->pt
== st_texture_object(texObj
)->pt
);
3040 level
= texImage
->Level
+ texObj
->MinLevel
;
3041 box
.z
+= texObj
->MinLayer
;
3044 /* Texture level sizes may be inconsistent. We my have "loose",
3045 * per-image gallium resources. The texImage->Level may not match
3046 * the gallium resource texture level.
3048 level
= find_mipmap_level(texImage
, pt
);
3051 assert(level
<= pt
->last_level
);
3053 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
3058 * Called via the glTexParam*() function, but only when some texture object
3059 * state has actually changed.
3062 st_TexParameter(struct gl_context
*ctx
,
3063 struct gl_texture_object
*texObj
, GLenum pname
)
3065 struct st_context
*st
= st_context(ctx
);
3066 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3069 case GL_TEXTURE_BASE_LEVEL
:
3070 case GL_TEXTURE_MAX_LEVEL
:
3071 case GL_DEPTH_TEXTURE_MODE
:
3072 case GL_DEPTH_STENCIL_TEXTURE_MODE
:
3073 case GL_TEXTURE_SRGB_DECODE_EXT
:
3074 case GL_TEXTURE_SWIZZLE_R
:
3075 case GL_TEXTURE_SWIZZLE_G
:
3076 case GL_TEXTURE_SWIZZLE_B
:
3077 case GL_TEXTURE_SWIZZLE_A
:
3078 case GL_TEXTURE_SWIZZLE_RGBA
:
3079 case GL_TEXTURE_BUFFER_SIZE
:
3080 case GL_TEXTURE_BUFFER_OFFSET
:
3081 /* changing any of these texture parameters means we must create
3082 * new sampler views.
3084 st_texture_release_all_sampler_views(st
, stObj
);
3092 st_SetTextureStorageForMemoryObject(struct gl_context
*ctx
,
3093 struct gl_texture_object
*texObj
,
3094 struct gl_memory_object
*memObj
,
3095 GLsizei levels
, GLsizei width
,
3096 GLsizei height
, GLsizei depth
,
3099 return st_texture_storage(ctx
, texObj
, levels
,
3100 width
, height
, depth
,
3105 st_NewTextureHandle(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
3106 struct gl_sampler_object
*sampObj
)
3108 struct st_context
*st
= st_context(ctx
);
3109 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3110 struct pipe_context
*pipe
= st
->pipe
;
3111 struct pipe_sampler_view
*view
;
3112 struct pipe_sampler_state sampler
= {0};
3114 if (texObj
->Target
!= GL_TEXTURE_BUFFER
) {
3115 if (!st_finalize_texture(ctx
, pipe
, texObj
, 0))
3118 st_convert_sampler(st
, texObj
, sampObj
, 0, &sampler
);
3120 /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
3121 view
= st_get_texture_sampler_view_from_stobj(st
, stObj
, sampObj
, 0, true);
3123 view
= st_get_buffer_sampler_view_from_stobj(st
, stObj
);
3126 return pipe
->create_texture_handle(pipe
, view
, &sampler
);
3131 st_DeleteTextureHandle(struct gl_context
*ctx
, GLuint64 handle
)
3133 struct st_context
*st
= st_context(ctx
);
3134 struct pipe_context
*pipe
= st
->pipe
;
3136 pipe
->delete_texture_handle(pipe
, handle
);
3141 st_MakeTextureHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3144 struct st_context
*st
= st_context(ctx
);
3145 struct pipe_context
*pipe
= st
->pipe
;
3147 pipe
->make_texture_handle_resident(pipe
, handle
, resident
);
3152 st_NewImageHandle(struct gl_context
*ctx
, struct gl_image_unit
*imgObj
)
3154 struct st_context
*st
= st_context(ctx
);
3155 struct pipe_context
*pipe
= st
->pipe
;
3156 struct pipe_image_view image
;
3158 st_convert_image(st
, imgObj
, &image
);
3160 return pipe
->create_image_handle(pipe
, &image
);
3165 st_DeleteImageHandle(struct gl_context
*ctx
, GLuint64 handle
)
3167 struct st_context
*st
= st_context(ctx
);
3168 struct pipe_context
*pipe
= st
->pipe
;
3170 pipe
->delete_image_handle(pipe
, handle
);
3175 st_MakeImageHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3176 GLenum access
, bool resident
)
3178 struct st_context
*st
= st_context(ctx
);
3179 struct pipe_context
*pipe
= st
->pipe
;
3181 pipe
->make_image_handle_resident(pipe
, handle
, access
, resident
);
3186 st_init_texture_functions(struct dd_function_table
*functions
)
3188 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
3189 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
3190 functions
->TexImage
= st_TexImage
;
3191 functions
->TexSubImage
= st_TexSubImage
;
3192 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
3193 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
3194 functions
->GenerateMipmap
= st_generate_mipmap
;
3196 functions
->GetTexSubImage
= st_GetTexSubImage
;
3198 /* compressed texture functions */
3199 functions
->CompressedTexImage
= st_CompressedTexImage
;
3201 functions
->NewTextureObject
= st_NewTextureObject
;
3202 functions
->NewTextureImage
= st_NewTextureImage
;
3203 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
3204 functions
->DeleteTexture
= st_DeleteTextureObject
;
3205 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
3206 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
3207 functions
->MapTextureImage
= st_MapTextureImage
;
3208 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
3210 /* XXX Temporary until we can query pipe's texture sizes */
3211 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
3213 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
3214 functions
->TextureView
= st_TextureView
;
3215 functions
->ClearTexSubImage
= st_ClearTexSubImage
;
3217 functions
->TexParameter
= st_TexParameter
;
3219 /* bindless functions */
3220 functions
->NewTextureHandle
= st_NewTextureHandle
;
3221 functions
->DeleteTextureHandle
= st_DeleteTextureHandle
;
3222 functions
->MakeTextureHandleResident
= st_MakeTextureHandleResident
;
3223 functions
->NewImageHandle
= st_NewImageHandle
;
3224 functions
->DeleteImageHandle
= st_DeleteImageHandle
;
3225 functions
->MakeImageHandleResident
= st_MakeImageHandleResident
;
3227 /* external object functions */
3228 functions
->SetTextureStorageForMemoryObject
= st_SetTextureStorageForMemoryObject
;