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/fbobject.h"
32 #include "main/formats.h"
33 #include "main/format_utils.h"
34 #include "main/glformats.h"
35 #include "main/image.h"
36 #include "main/imports.h"
37 #include "main/macros.h"
38 #include "main/mipmap.h"
39 #include "main/pack.h"
41 #include "main/pixeltransfer.h"
42 #include "main/texcompress.h"
43 #include "main/texcompress_etc.h"
44 #include "main/texgetimage.h"
45 #include "main/teximage.h"
46 #include "main/texobj.h"
47 #include "main/texstore.h"
49 #include "state_tracker/st_debug.h"
50 #include "state_tracker/st_context.h"
51 #include "state_tracker/st_cb_bitmap.h"
52 #include "state_tracker/st_cb_fbo.h"
53 #include "state_tracker/st_cb_flush.h"
54 #include "state_tracker/st_cb_texture.h"
55 #include "state_tracker/st_cb_bufferobjects.h"
56 #include "state_tracker/st_cb_memoryobjects.h"
57 #include "state_tracker/st_format.h"
58 #include "state_tracker/st_pbo.h"
59 #include "state_tracker/st_texture.h"
60 #include "state_tracker/st_gen_mipmap.h"
61 #include "state_tracker/st_atom.h"
62 #include "state_tracker/st_sampler_view.h"
64 #include "pipe/p_context.h"
65 #include "pipe/p_defines.h"
66 #include "util/u_inlines.h"
67 #include "util/u_upload_mgr.h"
68 #include "pipe/p_shader_tokens.h"
69 #include "util/u_tile.h"
70 #include "util/u_format.h"
71 #include "util/u_surface.h"
72 #include "util/u_sampler.h"
73 #include "util/u_math.h"
74 #include "util/u_box.h"
75 #include "util/u_simple_shaders.h"
76 #include "cso_cache/cso_context.h"
77 #include "tgsi/tgsi_ureg.h"
79 #define DBG if (0) printf
82 enum pipe_texture_target
83 gl_target_to_pipe(GLenum target
)
87 case GL_PROXY_TEXTURE_1D
:
88 return PIPE_TEXTURE_1D
;
90 case GL_PROXY_TEXTURE_2D
:
91 case GL_TEXTURE_EXTERNAL_OES
:
92 case GL_TEXTURE_2D_MULTISAMPLE
:
93 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
94 return PIPE_TEXTURE_2D
;
95 case GL_TEXTURE_RECTANGLE_NV
:
96 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
97 return PIPE_TEXTURE_RECT
;
99 case GL_PROXY_TEXTURE_3D
:
100 return PIPE_TEXTURE_3D
;
101 case GL_TEXTURE_CUBE_MAP_ARB
:
102 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
103 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
104 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
105 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
106 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
107 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
108 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
109 return PIPE_TEXTURE_CUBE
;
110 case GL_TEXTURE_1D_ARRAY_EXT
:
111 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
112 return PIPE_TEXTURE_1D_ARRAY
;
113 case GL_TEXTURE_2D_ARRAY_EXT
:
114 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
115 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
116 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
117 return PIPE_TEXTURE_2D_ARRAY
;
118 case GL_TEXTURE_BUFFER
:
120 case GL_TEXTURE_CUBE_MAP_ARRAY
:
121 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
122 return PIPE_TEXTURE_CUBE_ARRAY
;
130 /** called via ctx->Driver.NewTextureImage() */
131 static struct gl_texture_image
*
132 st_NewTextureImage(struct gl_context
* ctx
)
134 DBG("%s\n", __func__
);
136 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
140 /** called via ctx->Driver.DeleteTextureImage() */
142 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
144 /* nothing special (yet) for st_texture_image */
145 _mesa_delete_texture_image(ctx
, img
);
149 /** called via ctx->Driver.NewTextureObject() */
150 static struct gl_texture_object
*
151 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
153 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
157 /* Pre-allocate a sampler views container to save a branch in the fast path. */
158 obj
->sampler_views
= calloc(1, sizeof(struct st_sampler_views
) + sizeof(struct st_sampler_view
));
159 if (!obj
->sampler_views
) {
163 obj
->sampler_views
->max
= 1;
165 DBG("%s\n", __func__
);
166 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
168 simple_mtx_init(&obj
->validate_mutex
, mtx_plain
);
169 obj
->needs_validation
= true;
174 /** called via ctx->Driver.DeleteTextureObject() */
176 st_DeleteTextureObject(struct gl_context
*ctx
,
177 struct gl_texture_object
*texObj
)
179 struct st_context
*st
= st_context(ctx
);
180 struct st_texture_object
*stObj
= st_texture_object(texObj
);
182 pipe_resource_reference(&stObj
->pt
, NULL
);
183 st_texture_release_all_sampler_views(st
, stObj
);
184 st_texture_free_sampler_views(stObj
);
185 simple_mtx_destroy(&stObj
->validate_mutex
);
186 _mesa_delete_texture_object(ctx
, texObj
);
190 /** called via ctx->Driver.FreeTextureImageBuffer() */
192 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
193 struct gl_texture_image
*texImage
)
195 struct st_context
*st
= st_context(ctx
);
196 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
197 struct st_texture_image
*stImage
= st_texture_image(texImage
);
199 DBG("%s\n", __func__
);
202 pipe_resource_reference(&stImage
->pt
, NULL
);
205 free(stImage
->transfer
);
206 stImage
->transfer
= NULL
;
207 stImage
->num_transfers
= 0;
209 if (stImage
->etc_data
) {
210 free(stImage
->etc_data
);
211 stImage
->etc_data
= NULL
;
214 /* if the texture image is being deallocated, the structure of the
215 * texture is changing so we'll likely need a new sampler view.
217 st_texture_release_all_sampler_views(st
, stObj
);
221 st_etc_fallback(struct st_context
*st
, struct gl_texture_image
*texImage
)
223 return (_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
224 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
);
228 etc_fallback_allocate(struct st_context
*st
, struct st_texture_image
*stImage
)
230 struct gl_texture_image
*texImage
= &stImage
->base
;
232 if (!st_etc_fallback(st
, texImage
))
235 if (stImage
->etc_data
)
236 free(stImage
->etc_data
);
238 unsigned data_size
= _mesa_format_image_size(texImage
->TexFormat
,
244 malloc(data_size
* _mesa_num_tex_faces(texImage
->TexObject
->Target
));
247 /** called via ctx->Driver.MapTextureImage() */
249 st_MapTextureImage(struct gl_context
*ctx
,
250 struct gl_texture_image
*texImage
,
251 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
253 GLubyte
**mapOut
, GLint
*rowStrideOut
)
255 struct st_context
*st
= st_context(ctx
);
256 struct st_texture_image
*stImage
= st_texture_image(texImage
);
258 struct pipe_transfer
*transfer
;
260 /* Check for unexpected flags */
261 assert((mode
& ~(GL_MAP_READ_BIT
|
263 GL_MAP_INVALIDATE_RANGE_BIT
)) == 0);
265 const enum pipe_transfer_usage transfer_flags
=
266 st_access_flags_to_transfer_flags(mode
, false);
268 map
= st_texture_image_map(st
, stImage
, transfer_flags
, x
, y
, slice
, w
, h
, 1,
271 if (st_etc_fallback(st
, texImage
)) {
272 /* ETC isn't supported by all gallium drivers, where it's represented
273 * by uncompressed formats. We store the compressed data (as it's
274 * needed for image copies in OES_copy_image), and decompress as
275 * necessary in Unmap.
277 * Note: all ETC1/ETC2 formats have 4x4 block sizes.
279 unsigned z
= transfer
->box
.z
;
280 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
282 unsigned bytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
283 unsigned stride
= *rowStrideOut
= itransfer
->temp_stride
=
284 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width2
);
285 *mapOut
= itransfer
->temp_data
=
286 stImage
->etc_data
+ ((x
/ 4) * bytes
+ (y
/ 4) * stride
) +
287 z
* stride
* texImage
->Height2
/ 4;
288 itransfer
->map
= map
;
291 /* supported mapping */
293 *rowStrideOut
= transfer
->stride
;
303 /** called via ctx->Driver.UnmapTextureImage() */
305 st_UnmapTextureImage(struct gl_context
*ctx
,
306 struct gl_texture_image
*texImage
,
309 struct st_context
*st
= st_context(ctx
);
310 struct st_texture_image
*stImage
= st_texture_image(texImage
);
312 if (st_etc_fallback(st
, texImage
)) {
313 /* Decompress the ETC texture to the mapped one. */
314 unsigned z
= slice
+ stImage
->base
.Face
;
315 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
316 struct pipe_transfer
*transfer
= itransfer
->transfer
;
318 assert(z
== transfer
->box
.z
);
320 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
321 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
322 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
323 itransfer
->temp_data
,
324 itransfer
->temp_stride
,
325 transfer
->box
.width
, transfer
->box
.height
);
328 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
329 itransfer
->temp_data
, itransfer
->temp_stride
,
330 transfer
->box
.width
, transfer
->box
.height
,
331 texImage
->TexFormat
);
335 itransfer
->temp_data
= NULL
;
336 itransfer
->temp_stride
= 0;
340 st_texture_image_unmap(st
, stImage
, slice
);
345 * Return default texture resource binding bitmask for the given format.
348 default_bindings(struct st_context
*st
, enum pipe_format format
)
350 struct pipe_screen
*screen
= st
->pipe
->screen
;
351 const unsigned target
= PIPE_TEXTURE_2D
;
354 if (util_format_is_depth_or_stencil(format
))
355 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
357 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
359 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
363 format
= util_format_linear(format
);
365 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
368 return PIPE_BIND_SAMPLER_VIEW
;
374 * Given the size of a mipmap image, try to compute the size of the level=0
377 * Note that this isn't always accurate for odd-sized, non-POW textures.
378 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
380 * \return GL_TRUE for success, GL_FALSE for failure
383 guess_base_level_size(GLenum target
,
384 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
385 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
392 /* Guess the size of the base level.
393 * Depending on the image's size, we can't always make a guess here.
397 case GL_TEXTURE_1D_ARRAY
:
402 case GL_TEXTURE_2D_ARRAY
:
403 /* We can't make a good guess here, because the base level dimensions
406 if (width
== 1 || height
== 1) {
413 case GL_TEXTURE_CUBE_MAP
:
414 case GL_TEXTURE_CUBE_MAP_ARRAY
:
420 /* We can't make a good guess here, because the base level dimensions
423 if (width
== 1 || height
== 1 || depth
== 1) {
431 case GL_TEXTURE_RECTANGLE
:
448 * Try to determine whether we should allocate memory for a full texture
449 * mipmap. The problem is when we get a glTexImage(level=0) call, we
450 * can't immediately know if other mipmap levels are coming next. Here
451 * we try to guess whether to allocate memory for a mipmap or just the
454 * If we guess incorrectly here we'll later reallocate the right amount of
455 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
457 * \param stObj the texture object we're going to allocate memory for.
458 * \param stImage describes the incoming image which we need to store.
461 allocate_full_mipmap(const struct st_texture_object
*stObj
,
462 const struct st_texture_image
*stImage
)
464 switch (stObj
->base
.Target
) {
465 case GL_TEXTURE_RECTANGLE_NV
:
466 case GL_TEXTURE_BUFFER
:
467 case GL_TEXTURE_EXTERNAL_OES
:
468 case GL_TEXTURE_2D_MULTISAMPLE
:
469 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
470 /* these texture types cannot be mipmapped */
474 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
477 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
478 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
479 /* depth/stencil textures are seldom mipmapped */
482 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
485 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
486 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
487 /* not a mipmap minification filter */
490 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
491 /* 3D textures are seldom mipmapped */
499 * Try to allocate a pipe_resource object for the given st_texture_object.
501 * We use the given st_texture_image as a clue to determine the size of the
502 * mipmap image at level=0.
504 * \return GL_TRUE for success, GL_FALSE if out of memory.
507 guess_and_alloc_texture(struct st_context
*st
,
508 struct st_texture_object
*stObj
,
509 const struct st_texture_image
*stImage
)
511 const struct gl_texture_image
*firstImage
;
512 GLuint lastLevel
, width
, height
, depth
;
515 uint16_t ptHeight
, ptDepth
, ptLayers
;
516 enum pipe_format fmt
;
517 bool guessed_box
= false;
519 DBG("%s\n", __func__
);
523 /* If a base level image with compatible size exists, use that as our guess.
525 firstImage
= _mesa_base_tex_image(&stObj
->base
);
527 firstImage
->Width2
> 0 &&
528 firstImage
->Height2
> 0 &&
529 firstImage
->Depth2
> 0 &&
530 guess_base_level_size(stObj
->base
.Target
,
535 &width
, &height
, &depth
)) {
536 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
537 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
538 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
543 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
544 stImage
->base
.Width2
,
545 stImage
->base
.Height2
,
546 stImage
->base
.Depth2
,
548 &width
, &height
, &depth
);
551 /* we can't determine the image size at level=0 */
552 /* this is not an out of memory error */
556 /* At this point, (width x height x depth) is the expected size of
557 * the level=0 mipmap image.
560 /* Guess a reasonable value for lastLevel. With OpenGL we have no
561 * idea how many mipmap levels will be in a texture until we start
562 * to render with it. Make an educated guess here but be prepared
563 * to re-allocating a texture buffer with space for more (or fewer)
564 * mipmap levels later.
566 if (allocate_full_mipmap(stObj
, stImage
)) {
567 /* alloc space for a full mipmap */
568 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
569 width
, height
, depth
) - 1;
572 /* only alloc space for a single mipmap level */
576 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
578 bindings
= default_bindings(st
, fmt
);
580 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
581 width
, height
, depth
,
582 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
584 stObj
->pt
= st_texture_create(st
,
585 gl_target_to_pipe(stObj
->base
.Target
),
594 stObj
->lastLevel
= lastLevel
;
596 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
598 return stObj
->pt
!= NULL
;
603 * Called via ctx->Driver.AllocTextureImageBuffer().
604 * If the texture object/buffer already has space for the indicated image,
605 * we're done. Otherwise, allocate memory for the new texture image.
608 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
609 struct gl_texture_image
*texImage
)
611 struct st_context
*st
= st_context(ctx
);
612 struct st_texture_image
*stImage
= st_texture_image(texImage
);
613 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
614 const GLuint level
= texImage
->Level
;
615 GLuint width
= texImage
->Width
;
616 GLuint height
= texImage
->Height
;
617 GLuint depth
= texImage
->Depth
;
619 DBG("%s\n", __func__
);
621 assert(!stImage
->pt
); /* xxx this might be wrong */
623 stObj
->needs_validation
= true;
625 etc_fallback_allocate(st
, stImage
);
627 /* Look if the parent texture object has space for this image */
629 level
<= stObj
->pt
->last_level
&&
630 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
631 /* this image will fit in the existing texture object's memory */
632 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
636 /* The parent texture object does not have space for this image */
638 pipe_resource_reference(&stObj
->pt
, NULL
);
639 st_texture_release_all_sampler_views(st
, stObj
);
641 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
642 /* Probably out of memory.
643 * Try flushing any pending rendering, then retry.
646 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
647 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
653 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
654 /* The image will live in the object's mipmap memory */
655 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
660 /* Create a new, temporary texture/resource/buffer to hold this
661 * one texture image. Note that when we later access this image
662 * (either for mapping or copying) we'll want to always specify
663 * mipmap level=0, even if the image represents some other mipmap
666 enum pipe_format format
=
667 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
668 GLuint bindings
= default_bindings(st
, format
);
670 uint16_t ptHeight
, ptDepth
, ptLayers
;
672 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
673 width
, height
, depth
,
674 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
676 stImage
->pt
= st_texture_create(st
,
677 gl_target_to_pipe(stObj
->base
.Target
),
685 return stImage
->pt
!= NULL
;
691 * Preparation prior to glTexImage. Basically check the 'surface_based'
692 * field and switch to a "normal" tex image if necessary.
695 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
696 GLenum format
, GLenum type
)
698 struct gl_texture_object
*texObj
= texImage
->TexObject
;
699 struct st_texture_object
*stObj
= st_texture_object(texObj
);
701 /* switch to "normal" */
702 if (stObj
->surface_based
) {
703 const GLenum target
= texObj
->Target
;
704 const GLuint level
= texImage
->Level
;
705 mesa_format texFormat
;
707 assert(!st_texture_image(texImage
)->pt
);
708 _mesa_clear_texture_object(ctx
, texObj
, texImage
);
709 stObj
->layer_override
= 0;
710 stObj
->level_override
= 0;
711 pipe_resource_reference(&stObj
->pt
, NULL
);
713 /* oops, need to init this image again */
714 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
715 texImage
->InternalFormat
, format
,
718 _mesa_init_teximage_fields(ctx
, texImage
,
719 texImage
->Width
, texImage
->Height
,
720 texImage
->Depth
, texImage
->Border
,
721 texImage
->InternalFormat
, texFormat
);
723 stObj
->surface_based
= GL_FALSE
;
729 * Return a writemask for the gallium blit. The parameters can be base
730 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
733 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
736 case GL_DEPTH_STENCIL
:
738 case GL_DEPTH_STENCIL
:
740 case GL_DEPTH_COMPONENT
:
742 case GL_STENCIL_INDEX
:
749 case GL_DEPTH_COMPONENT
:
751 case GL_DEPTH_STENCIL
:
752 case GL_DEPTH_COMPONENT
:
759 case GL_STENCIL_INDEX
:
761 case GL_STENCIL_INDEX
:
769 return PIPE_MASK_RGBA
;
774 * Converts format to a format with the same components, types
775 * and sizes, but with the components in RGBA order.
777 static enum pipe_format
778 unswizzle_format(enum pipe_format format
)
782 case PIPE_FORMAT_B8G8R8A8_UNORM
:
783 case PIPE_FORMAT_A8R8G8B8_UNORM
:
784 case PIPE_FORMAT_A8B8G8R8_UNORM
:
785 return PIPE_FORMAT_R8G8B8A8_UNORM
;
787 case PIPE_FORMAT_B10G10R10A2_UNORM
:
788 return PIPE_FORMAT_R10G10B10A2_UNORM
;
790 case PIPE_FORMAT_B10G10R10A2_SNORM
:
791 return PIPE_FORMAT_R10G10B10A2_SNORM
;
793 case PIPE_FORMAT_B10G10R10A2_UINT
:
794 return PIPE_FORMAT_R10G10B10A2_UINT
;
802 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
804 static enum pipe_format
805 alpha_to_red(enum pipe_format format
)
809 case PIPE_FORMAT_A8_UNORM
:
810 return PIPE_FORMAT_R8_UNORM
;
811 case PIPE_FORMAT_A8_SNORM
:
812 return PIPE_FORMAT_R8_SNORM
;
813 case PIPE_FORMAT_A8_UINT
:
814 return PIPE_FORMAT_R8_UINT
;
815 case PIPE_FORMAT_A8_SINT
:
816 return PIPE_FORMAT_R8_SINT
;
818 case PIPE_FORMAT_A16_UNORM
:
819 return PIPE_FORMAT_R16_UNORM
;
820 case PIPE_FORMAT_A16_SNORM
:
821 return PIPE_FORMAT_R16_SNORM
;
822 case PIPE_FORMAT_A16_UINT
:
823 return PIPE_FORMAT_R16_UINT
;
824 case PIPE_FORMAT_A16_SINT
:
825 return PIPE_FORMAT_R16_SINT
;
826 case PIPE_FORMAT_A16_FLOAT
:
827 return PIPE_FORMAT_R16_FLOAT
;
829 case PIPE_FORMAT_A32_UINT
:
830 return PIPE_FORMAT_R32_UINT
;
831 case PIPE_FORMAT_A32_SINT
:
832 return PIPE_FORMAT_R32_SINT
;
833 case PIPE_FORMAT_A32_FLOAT
:
834 return PIPE_FORMAT_R32_FLOAT
;
842 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
844 static enum pipe_format
845 red_alpha_to_red_green(enum pipe_format format
)
849 case PIPE_FORMAT_R8A8_UNORM
:
850 return PIPE_FORMAT_R8G8_UNORM
;
851 case PIPE_FORMAT_R8A8_SNORM
:
852 return PIPE_FORMAT_R8G8_SNORM
;
853 case PIPE_FORMAT_R8A8_UINT
:
854 return PIPE_FORMAT_R8G8_UINT
;
855 case PIPE_FORMAT_R8A8_SINT
:
856 return PIPE_FORMAT_R8G8_SINT
;
858 case PIPE_FORMAT_R16A16_UNORM
:
859 return PIPE_FORMAT_R16G16_UNORM
;
860 case PIPE_FORMAT_R16A16_SNORM
:
861 return PIPE_FORMAT_R16G16_SNORM
;
862 case PIPE_FORMAT_R16A16_UINT
:
863 return PIPE_FORMAT_R16G16_UINT
;
864 case PIPE_FORMAT_R16A16_SINT
:
865 return PIPE_FORMAT_R16G16_SINT
;
866 case PIPE_FORMAT_R16A16_FLOAT
:
867 return PIPE_FORMAT_R16G16_FLOAT
;
869 case PIPE_FORMAT_R32A32_UINT
:
870 return PIPE_FORMAT_R32G32_UINT
;
871 case PIPE_FORMAT_R32A32_SINT
:
872 return PIPE_FORMAT_R32G32_SINT
;
873 case PIPE_FORMAT_R32A32_FLOAT
:
874 return PIPE_FORMAT_R32G32_FLOAT
;
882 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
884 static enum pipe_format
885 luminance_alpha_to_red_green(enum pipe_format format
)
889 case PIPE_FORMAT_L8A8_UNORM
:
890 return PIPE_FORMAT_R8G8_UNORM
;
891 case PIPE_FORMAT_L8A8_SNORM
:
892 return PIPE_FORMAT_R8G8_SNORM
;
893 case PIPE_FORMAT_L8A8_UINT
:
894 return PIPE_FORMAT_R8G8_UINT
;
895 case PIPE_FORMAT_L8A8_SINT
:
896 return PIPE_FORMAT_R8G8_SINT
;
898 case PIPE_FORMAT_L16A16_UNORM
:
899 return PIPE_FORMAT_R16G16_UNORM
;
900 case PIPE_FORMAT_L16A16_SNORM
:
901 return PIPE_FORMAT_R16G16_SNORM
;
902 case PIPE_FORMAT_L16A16_UINT
:
903 return PIPE_FORMAT_R16G16_UINT
;
904 case PIPE_FORMAT_L16A16_SINT
:
905 return PIPE_FORMAT_R16G16_SINT
;
906 case PIPE_FORMAT_L16A16_FLOAT
:
907 return PIPE_FORMAT_R16G16_FLOAT
;
909 case PIPE_FORMAT_L32A32_UINT
:
910 return PIPE_FORMAT_R32G32_UINT
;
911 case PIPE_FORMAT_L32A32_SINT
:
912 return PIPE_FORMAT_R32G32_SINT
;
913 case PIPE_FORMAT_L32A32_FLOAT
:
914 return PIPE_FORMAT_R32G32_FLOAT
;
922 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
925 format_is_alpha(enum pipe_format format
)
927 const struct util_format_description
*desc
= util_format_description(format
);
929 if (desc
->nr_channels
== 1 &&
930 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
931 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
932 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
933 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
940 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
943 format_is_red(enum pipe_format format
)
945 const struct util_format_description
*desc
= util_format_description(format
);
947 if (desc
->nr_channels
== 1 &&
948 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
949 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
950 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
951 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
959 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
962 format_is_luminance(enum pipe_format format
)
964 const struct util_format_description
*desc
= util_format_description(format
);
966 if (desc
->nr_channels
== 1 &&
967 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
968 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
969 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
970 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
977 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
980 format_is_red_alpha(enum pipe_format format
)
982 const struct util_format_description
*desc
= util_format_description(format
);
984 if (desc
->nr_channels
== 2 &&
985 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
986 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
987 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
988 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
995 format_is_swizzled_rgba(enum pipe_format format
)
997 const struct util_format_description
*desc
= util_format_description(format
);
999 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
1000 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
1001 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
1002 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
1010 unsigned char swizzle
[4];
1011 enum pipe_format format
;
1014 static const struct format_table table_8888_unorm
[] = {
1015 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
1016 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
1017 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
1018 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
1021 static const struct format_table table_1010102_unorm
[] = {
1022 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
1023 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
1026 static const struct format_table table_1010102_snorm
[] = {
1027 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
1028 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
1031 static const struct format_table table_1010102_uint
[] = {
1032 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
1033 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
1036 static enum pipe_format
1037 swizzle_format(enum pipe_format format
, const int * const swizzle
)
1042 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1043 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1044 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1045 case PIPE_FORMAT_A8B8G8R8_UNORM
:
1046 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
1047 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
1048 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
1049 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
1050 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
1051 return table_8888_unorm
[i
].format
;
1055 case PIPE_FORMAT_R10G10B10A2_UNORM
:
1056 case PIPE_FORMAT_B10G10R10A2_UNORM
:
1057 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
1058 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
1059 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
1060 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
1061 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
1062 return table_1010102_unorm
[i
].format
;
1066 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1067 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1068 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1069 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1070 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1071 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1072 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1073 return table_1010102_snorm
[i
].format
;
1077 case PIPE_FORMAT_R10G10B10A2_UINT
:
1078 case PIPE_FORMAT_B10G10R10A2_UINT
:
1079 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1080 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1081 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1082 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1083 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1084 return table_1010102_uint
[i
].format
;
1092 return PIPE_FORMAT_NONE
;
1096 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1098 enum pipe_format src
= *src_format
;
1099 enum pipe_format dst
= *dst_format
;
1101 /* Note: dst_format has already been transformed from luminance/intensity
1102 * to red when this function is called. The source format will never
1103 * be an intensity format, because GL_INTENSITY is not a legal value
1104 * for the format parameter in glTex(Sub)Image(). */
1106 if (format_is_alpha(src
)) {
1107 if (!format_is_alpha(dst
))
1110 src
= alpha_to_red(src
);
1111 dst
= alpha_to_red(dst
);
1112 } else if (format_is_luminance(src
)) {
1113 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1116 src
= util_format_luminance_to_red(src
);
1117 } else if (util_format_is_luminance_alpha(src
)) {
1118 src
= luminance_alpha_to_red_green(src
);
1120 if (format_is_red_alpha(dst
)) {
1121 dst
= red_alpha_to_red_green(dst
);
1122 } else if (!format_is_red(dst
))
1124 } else if (format_is_swizzled_rgba(src
)) {
1125 const struct util_format_description
*src_desc
= util_format_description(src
);
1126 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1130 /* Make sure the format is an RGBA and not an RGBX format */
1131 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1134 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1137 for (i
= 0; i
< 4; i
++)
1138 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1140 dst
= swizzle_format(dst
, swizzle
);
1141 if (dst
== PIPE_FORMAT_NONE
)
1144 src
= unswizzle_format(src
);
1153 try_pbo_upload_common(struct gl_context
*ctx
,
1154 struct pipe_surface
*surface
,
1155 const struct st_pbo_addresses
*addr
,
1156 enum pipe_format src_format
)
1158 struct st_context
*st
= st_context(ctx
);
1159 struct cso_context
*cso
= st
->cso_context
;
1160 struct pipe_context
*pipe
= st
->pipe
;
1161 bool success
= false;
1164 fs
= st_pbo_get_upload_fs(st
, src_format
, surface
->format
);
1168 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1169 CSO_BIT_FRAGMENT_SAMPLERS
|
1170 CSO_BIT_VERTEX_ELEMENTS
|
1171 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1172 CSO_BIT_FRAMEBUFFER
|
1175 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1176 CSO_BIT_RASTERIZER
|
1177 CSO_BIT_STREAM_OUTPUTS
|
1178 CSO_BIT_PAUSE_QUERIES
|
1179 CSO_BIT_SAMPLE_MASK
|
1180 CSO_BIT_MIN_SAMPLES
|
1181 CSO_BIT_RENDER_CONDITION
|
1182 CSO_BITS_ALL_SHADERS
));
1183 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1185 cso_set_sample_mask(cso
, ~0);
1186 cso_set_min_samples(cso
, 1);
1187 cso_set_render_condition(cso
, NULL
, FALSE
, 0);
1189 /* Set up the sampler_view */
1191 struct pipe_sampler_view templ
;
1192 struct pipe_sampler_view
*sampler_view
;
1193 struct pipe_sampler_state sampler
= {0};
1194 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
1196 memset(&templ
, 0, sizeof(templ
));
1197 templ
.target
= PIPE_BUFFER
;
1198 templ
.format
= src_format
;
1199 templ
.u
.buf
.offset
= addr
->first_element
* addr
->bytes_per_pixel
;
1200 templ
.u
.buf
.size
= (addr
->last_element
- addr
->first_element
+ 1) *
1201 addr
->bytes_per_pixel
;
1202 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1203 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1204 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1205 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1207 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1208 if (sampler_view
== NULL
)
1211 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1213 pipe_sampler_view_reference(&sampler_view
, NULL
);
1215 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
1218 /* Framebuffer_state */
1220 struct pipe_framebuffer_state fb
;
1221 memset(&fb
, 0, sizeof(fb
));
1222 fb
.width
= surface
->width
;
1223 fb
.height
= surface
->height
;
1225 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1227 cso_set_framebuffer(cso
, &fb
);
1229 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1232 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1235 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1237 /* Depth/stencil/alpha state */
1239 struct pipe_depth_stencil_alpha_state dsa
;
1240 memset(&dsa
, 0, sizeof(dsa
));
1241 cso_set_depth_stencil_alpha(cso
, &dsa
);
1244 /* Set up the fragment shader */
1245 cso_set_fragment_shader_handle(cso
, fs
);
1247 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1250 cso_restore_state(cso
);
1251 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1257 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1258 struct gl_texture_image
*texImage
,
1259 GLenum format
, GLenum type
,
1260 enum pipe_format dst_format
,
1261 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1262 GLint width
, GLint height
, GLint depth
,
1264 const struct gl_pixelstore_attrib
*unpack
)
1266 struct st_context
*st
= st_context(ctx
);
1267 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1268 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1269 struct pipe_resource
*texture
= stImage
->pt
;
1270 struct pipe_context
*pipe
= st
->pipe
;
1271 struct pipe_screen
*screen
= pipe
->screen
;
1272 struct pipe_surface
*surface
= NULL
;
1273 struct st_pbo_addresses addr
;
1274 enum pipe_format src_format
;
1275 const struct util_format_description
*desc
;
1276 GLenum gl_target
= texImage
->TexObject
->Target
;
1279 if (!st
->pbo
.upload_enabled
)
1282 /* From now on, we need the gallium representation of dimensions. */
1283 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1290 if (depth
!= 1 && !st
->pbo
.layers
)
1293 /* Choose the source format. Initially, we do so without checking driver
1294 * support at all because of the remapping we later perform and because
1295 * at least the Radeon driver actually supports some formats for texture
1296 * buffers which it doesn't support for regular textures. */
1297 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1302 src_format
= util_format_linear(src_format
);
1303 desc
= util_format_description(src_format
);
1305 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1308 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1311 if (st
->pbo
.rgba_only
) {
1312 enum pipe_format orig_dst_format
= dst_format
;
1314 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1318 if (dst_format
!= orig_dst_format
&&
1319 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1320 PIPE_BIND_RENDER_TARGET
)) {
1326 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0,
1327 PIPE_BIND_SAMPLER_VIEW
)) {
1331 /* Compute buffer addresses */
1332 addr
.xoffset
= xoffset
;
1333 addr
.yoffset
= yoffset
;
1335 addr
.height
= height
;
1337 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1339 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1343 /* Set up the surface */
1345 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1346 unsigned max_layer
= util_max_layer(texture
, level
);
1348 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1350 struct pipe_surface templ
;
1351 memset(&templ
, 0, sizeof(templ
));
1352 templ
.format
= dst_format
;
1353 templ
.u
.tex
.level
= level
;
1354 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1355 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1357 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1362 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1364 pipe_surface_reference(&surface
, NULL
);
1370 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1371 struct gl_texture_image
*texImage
,
1372 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1373 GLint width
, GLint height
, GLint depth
,
1374 GLenum format
, GLenum type
, const void *pixels
,
1375 const struct gl_pixelstore_attrib
*unpack
)
1377 struct st_context
*st
= st_context(ctx
);
1378 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1379 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1380 struct pipe_context
*pipe
= st
->pipe
;
1381 struct pipe_screen
*screen
= pipe
->screen
;
1382 struct pipe_resource
*dst
= stImage
->pt
;
1383 struct pipe_resource
*src
= NULL
;
1384 struct pipe_resource src_templ
;
1385 struct pipe_transfer
*transfer
;
1386 struct pipe_blit_info blit
;
1387 enum pipe_format src_format
, dst_format
;
1388 mesa_format mesa_src_format
;
1389 GLenum gl_target
= texImage
->TexObject
->Target
;
1392 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1393 unsigned dst_level
= 0;
1395 st_flush_bitmap_cache(st
);
1396 st_invalidate_readpix_cache(st
);
1398 if (stObj
->pt
== stImage
->pt
)
1399 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1401 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1402 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1407 /* Try texture_subdata, which should be the fastest memcpy path. */
1409 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1410 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1411 texImage
->TexFormat
, format
, type
,
1413 struct pipe_box box
;
1414 unsigned stride
, layer_stride
;
1417 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1418 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1420 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1423 /* Convert to Gallium coordinates. */
1424 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1429 layer_stride
= stride
;
1432 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1433 pipe
->texture_subdata(pipe
, dst
, dst_level
, 0,
1434 &box
, data
, stride
, layer_stride
);
1438 if (!st
->prefer_blit_based_texture_transfer
) {
1442 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1443 * blit implementation in some drivers. */
1444 if (format
== GL_DEPTH_STENCIL
) {
1448 /* If the base internal format and the texture format don't match,
1449 * we can't use blit-based TexSubImage. */
1450 if (texImage
->_BaseFormat
!=
1451 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1456 /* See if the destination format is supported. */
1457 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1458 bind
= PIPE_BIND_DEPTH_STENCIL
;
1460 bind
= PIPE_BIND_RENDER_TARGET
;
1462 /* For luminance and intensity, only the red channel is stored
1463 * in the destination. */
1464 dst_format
= util_format_linear(dst
->format
);
1465 dst_format
= util_format_luminance_to_red(dst_format
);
1466 dst_format
= util_format_intensity_to_red(dst_format
);
1469 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1470 dst
->nr_samples
, bind
)) {
1474 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1475 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1476 xoffset
, yoffset
, zoffset
,
1477 width
, height
, depth
, pixels
, unpack
))
1481 /* See if the texture format already matches the format and type,
1482 * in which case the memcpy-based fast path will likely be used and
1483 * we don't have to blit. */
1484 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1485 type
, unpack
->SwapBytes
, NULL
)) {
1489 /* Choose the source format. */
1490 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1491 format
, type
, unpack
->SwapBytes
);
1496 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1498 /* There is no reason to do this if we cannot use memcpy for the temporary
1499 * source texture at least. This also takes transfer ops into account,
1501 if (!_mesa_texstore_can_use_memcpy(ctx
,
1502 _mesa_get_format_base_format(mesa_src_format
),
1503 mesa_src_format
, format
, type
, unpack
)) {
1507 /* TexSubImage only sets a single cubemap face. */
1508 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1509 gl_target
= GL_TEXTURE_2D
;
1511 /* TexSubImage can specify subsets of cube map array faces
1512 * so we need to upload via 2D array instead */
1513 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1514 gl_target
= GL_TEXTURE_2D_ARRAY
;
1517 /* Initialize the source texture description. */
1518 memset(&src_templ
, 0, sizeof(src_templ
));
1519 src_templ
.target
= gl_target_to_pipe(gl_target
);
1520 src_templ
.format
= src_format
;
1521 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1522 src_templ
.usage
= PIPE_USAGE_STAGING
;
1524 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1525 &src_templ
.width0
, &src_templ
.height0
,
1526 &src_templ
.depth0
, &src_templ
.array_size
);
1528 /* Check for NPOT texture support. */
1529 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1530 (!util_is_power_of_two(src_templ
.width0
) ||
1531 !util_is_power_of_two(src_templ
.height0
) ||
1532 !util_is_power_of_two(src_templ
.depth0
))) {
1536 /* Create the source texture. */
1537 src
= screen
->resource_create(screen
, &src_templ
);
1542 /* Map source pixels. */
1543 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1544 format
, type
, pixels
, unpack
,
1547 /* This is a GL error. */
1548 pipe_resource_reference(&src
, NULL
);
1552 /* From now on, we need the gallium representation of dimensions. */
1553 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1560 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1561 width
, height
, depth
, &transfer
);
1563 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1564 pipe_resource_reference(&src
, NULL
);
1568 /* Upload pixels (just memcpy). */
1570 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1573 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1574 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1575 /* 1D array textures.
1576 * We need to convert gallium coords to GL coords.
1578 void *src
= _mesa_image_address2d(unpack
, pixels
,
1579 width
, depth
, format
,
1581 memcpy(map
, src
, bytesPerRow
);
1584 ubyte
*slice_map
= map
;
1586 for (row
= 0; row
< (unsigned) height
; row
++) {
1587 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1588 width
, height
, format
,
1589 type
, slice
, row
, 0);
1590 memcpy(slice_map
, src
, bytesPerRow
);
1591 slice_map
+= transfer
->stride
;
1594 map
+= transfer
->layer_stride
;
1598 pipe_transfer_unmap(pipe
, transfer
);
1599 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1602 memset(&blit
, 0, sizeof(blit
));
1603 blit
.src
.resource
= src
;
1605 blit
.src
.format
= src_format
;
1606 blit
.dst
.resource
= dst
;
1607 blit
.dst
.level
= dst_level
;
1608 blit
.dst
.format
= dst_format
;
1609 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1610 blit
.dst
.box
.x
= xoffset
;
1611 blit
.dst
.box
.y
= yoffset
;
1612 blit
.dst
.box
.z
= zoffset
+ dstz
;
1613 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1614 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1615 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1616 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1617 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1618 blit
.scissor_enable
= FALSE
;
1620 st
->pipe
->blit(st
->pipe
, &blit
);
1622 pipe_resource_reference(&src
, NULL
);
1626 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1627 width
, height
, depth
, format
, type
, pixels
,
1632 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1633 struct gl_texture_image
*texImage
,
1634 GLenum format
, GLenum type
, const void *pixels
,
1635 const struct gl_pixelstore_attrib
*unpack
)
1637 assert(dims
== 1 || dims
== 2 || dims
== 3);
1639 prep_teximage(ctx
, texImage
, format
, type
);
1641 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1644 /* allocate storage for texture data */
1645 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1646 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1650 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1651 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1652 format
, type
, pixels
, unpack
);
1657 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1658 struct gl_texture_image
*texImage
,
1659 GLint x
, GLint y
, GLint z
,
1660 GLsizei w
, GLsizei h
, GLsizei d
,
1661 GLenum format
, GLsizei imageSize
, const void *data
)
1663 struct st_context
*st
= st_context(ctx
);
1664 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1665 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1666 struct pipe_resource
*texture
= stImage
->pt
;
1667 struct pipe_context
*pipe
= st
->pipe
;
1668 struct pipe_screen
*screen
= pipe
->screen
;
1669 struct pipe_resource
*dst
= stImage
->pt
;
1670 struct pipe_surface
*surface
= NULL
;
1671 struct compressed_pixelstore store
;
1672 struct st_pbo_addresses addr
;
1673 enum pipe_format copy_format
;
1675 intptr_t buf_offset
;
1676 bool success
= false;
1678 /* Check basic pre-conditions for PBO upload */
1679 if (!st
->prefer_blit_based_texture_transfer
) {
1683 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1686 if (st_etc_fallback(st
, texImage
)) {
1687 /* ETC isn't supported and is represented by uncompressed formats. */
1695 if (!st
->pbo
.upload_enabled
||
1696 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1700 /* Choose the pipe format for the upload. */
1701 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1702 bw
= util_format_get_blockwidth(dst
->format
);
1703 bh
= util_format_get_blockheight(dst
->format
);
1705 switch (addr
.bytes_per_pixel
) {
1707 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1710 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1716 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0,
1717 PIPE_BIND_SAMPLER_VIEW
)) {
1721 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1722 dst
->nr_samples
, PIPE_BIND_RENDER_TARGET
)) {
1726 /* Interpret the pixelstore settings. */
1727 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1728 &ctx
->Unpack
, &store
);
1729 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1730 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1732 /* Compute the offset into the buffer */
1733 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1735 if (buf_offset
% addr
.bytes_per_pixel
) {
1739 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1741 addr
.xoffset
= x
/ bw
;
1742 addr
.yoffset
= y
/ bh
;
1743 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1744 addr
.height
= store
.CopyRowsPerSlice
;
1746 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1747 addr
.image_height
= store
.TotalRowsPerSlice
;
1749 if (!st_pbo_addresses_setup(st
, st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1753 /* Set up the surface. */
1755 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1756 unsigned max_layer
= util_max_layer(texture
, level
);
1758 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1760 struct pipe_surface templ
;
1761 memset(&templ
, 0, sizeof(templ
));
1762 templ
.format
= copy_format
;
1763 templ
.u
.tex
.level
= level
;
1764 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1765 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1767 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1772 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1774 pipe_surface_reference(&surface
, NULL
);
1780 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1782 format
, imageSize
, data
);
1786 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1787 struct gl_texture_image
*texImage
,
1788 GLsizei imageSize
, const void *data
)
1790 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1792 /* only 2D and 3D compressed images are supported at this time */
1794 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1798 /* This is pretty simple, because unlike the general texstore path we don't
1799 * have to worry about the usual image unpacking or image transfer
1803 assert(texImage
->Width
> 0);
1804 assert(texImage
->Height
> 0);
1805 assert(texImage
->Depth
> 0);
1807 /* allocate storage for texture data */
1808 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1809 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1813 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1815 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1816 texImage
->TexFormat
,
1824 * Called via ctx->Driver.GetTexSubImage()
1826 * This uses a blit to copy the texture to a texture format which matches
1827 * the format and type combo and then a fast read-back is done using memcpy.
1828 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1829 * a format which matches the swizzling.
1831 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1833 * NOTE: Drivers usually do a blit to convert between tiled and linear
1834 * texture layouts during texture uploads/downloads, so the blit
1835 * we do here should be free in such cases.
1838 st_GetTexSubImage(struct gl_context
* ctx
,
1839 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1840 GLsizei width
, GLsizei height
, GLint depth
,
1841 GLenum format
, GLenum type
, void * pixels
,
1842 struct gl_texture_image
*texImage
)
1844 struct st_context
*st
= st_context(ctx
);
1845 struct pipe_context
*pipe
= st
->pipe
;
1846 struct pipe_screen
*screen
= pipe
->screen
;
1847 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1848 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1849 struct pipe_resource
*src
= stObj
->pt
;
1850 struct pipe_resource
*dst
= NULL
;
1851 struct pipe_resource dst_templ
;
1852 enum pipe_format dst_format
, src_format
;
1853 mesa_format mesa_format
;
1854 GLenum gl_target
= texImage
->TexObject
->Target
;
1855 enum pipe_texture_target pipe_target
;
1857 struct pipe_blit_info blit
;
1859 struct pipe_transfer
*tex_xfer
;
1861 boolean done
= FALSE
;
1863 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1864 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1866 st_flush_bitmap_cache(st
);
1868 if (!st
->prefer_blit_based_texture_transfer
&&
1869 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1870 /* Try to avoid the fallback if we're doing texture decompression here */
1874 /* Handle non-finalized textures. */
1875 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1879 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1880 * due to an incomplete stencil blit implementation in some drivers. */
1881 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1885 /* If the base internal format and the texture format don't match, we have
1886 * to fall back to _mesa_GetTexImage_sw. */
1887 if (texImage
->_BaseFormat
!=
1888 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1892 /* See if the texture format already matches the format and type,
1893 * in which case the memcpy-based fast path will be used. */
1894 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1895 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1899 /* Convert the source format to what is expected by GetTexImage
1900 * and see if it's supported.
1902 * This only applies to glGetTexImage:
1903 * - Luminance must be returned as (L,0,0,1).
1904 * - Luminance alpha must be returned as (L,0,0,A).
1905 * - Intensity must be returned as (I,0,0,1)
1907 if (stObj
->surface_based
)
1908 src_format
= util_format_linear(stObj
->surface_format
);
1910 src_format
= util_format_linear(src
->format
);
1911 src_format
= util_format_luminance_to_red(src_format
);
1912 src_format
= util_format_intensity_to_red(src_format
);
1915 !screen
->is_format_supported(screen
, src_format
, src
->target
,
1917 PIPE_BIND_SAMPLER_VIEW
)) {
1921 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1922 bind
= PIPE_BIND_DEPTH_STENCIL
;
1924 bind
= PIPE_BIND_RENDER_TARGET
;
1926 /* GetTexImage only returns a single face for cubemaps. */
1927 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1928 gl_target
= GL_TEXTURE_2D
;
1930 pipe_target
= gl_target_to_pipe(gl_target
);
1932 /* Choose the destination format by finding the best match
1933 * for the format+type combo. */
1934 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1935 ctx
->Pack
.SwapBytes
);
1937 if (dst_format
== PIPE_FORMAT_NONE
) {
1938 GLenum dst_glformat
;
1940 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1941 * where decompression with a blit is always preferred. */
1942 if (!util_format_is_compressed(src
->format
)) {
1946 /* Set the appropriate format for the decompressed texture.
1947 * Luminance and sRGB formats shouldn't appear here.*/
1948 switch (src_format
) {
1949 case PIPE_FORMAT_DXT1_RGB
:
1950 case PIPE_FORMAT_DXT1_RGBA
:
1951 case PIPE_FORMAT_DXT3_RGBA
:
1952 case PIPE_FORMAT_DXT5_RGBA
:
1953 case PIPE_FORMAT_RGTC1_UNORM
:
1954 case PIPE_FORMAT_RGTC2_UNORM
:
1955 case PIPE_FORMAT_ETC1_RGB8
:
1956 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1957 dst_glformat
= GL_RGBA8
;
1959 case PIPE_FORMAT_RGTC1_SNORM
:
1960 case PIPE_FORMAT_RGTC2_SNORM
:
1961 if (!ctx
->Extensions
.EXT_texture_snorm
)
1963 dst_glformat
= GL_RGBA8_SNORM
;
1965 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1966 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1967 if (!ctx
->Extensions
.ARB_texture_float
)
1969 dst_glformat
= GL_RGBA32F
;
1976 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
1977 pipe_target
, 0, bind
, FALSE
);
1979 if (dst_format
== PIPE_FORMAT_NONE
) {
1980 /* unable to get an rgba format!?! */
1985 /* create the destination texture of size (width X height X depth) */
1986 memset(&dst_templ
, 0, sizeof(dst_templ
));
1987 dst_templ
.target
= pipe_target
;
1988 dst_templ
.format
= dst_format
;
1989 dst_templ
.bind
= bind
;
1990 dst_templ
.usage
= PIPE_USAGE_STAGING
;
1992 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1993 &dst_templ
.width0
, &dst_templ
.height0
,
1994 &dst_templ
.depth0
, &dst_templ
.array_size
);
1996 dst
= screen
->resource_create(screen
, &dst_templ
);
2001 /* From now on, we need the gallium representation of dimensions. */
2002 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2009 assert(texImage
->Face
== 0 ||
2010 texImage
->TexObject
->MinLayer
== 0 ||
2013 memset(&blit
, 0, sizeof(blit
));
2014 blit
.src
.resource
= src
;
2015 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2016 blit
.src
.format
= src_format
;
2017 blit
.dst
.resource
= dst
;
2019 blit
.dst
.format
= dst
->format
;
2020 blit
.src
.box
.x
= xoffset
;
2022 blit
.src
.box
.y
= yoffset
;
2024 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2026 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2027 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2028 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2029 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2030 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2031 blit
.scissor_enable
= FALSE
;
2033 /* blit/render/decompress */
2034 st
->pipe
->blit(st
->pipe
, &blit
);
2036 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2038 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2039 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2044 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2045 dims
= _mesa_get_texture_dimensions(gl_target
);
2047 /* copy/pack data into user buffer */
2048 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2049 ctx
->Pack
.SwapBytes
, NULL
)) {
2051 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2054 for (slice
= 0; slice
< depth
; slice
++) {
2055 ubyte
*slice_map
= map
;
2057 for (row
= 0; row
< height
; row
++) {
2058 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2059 width
, height
, format
, type
,
2062 memcpy(dest
, slice_map
, bytesPerRow
);
2064 slice_map
+= tex_xfer
->stride
;
2067 map
+= tex_xfer
->layer_stride
;
2071 /* format translation via floats */
2074 uint32_t dstMesaFormat
;
2075 int dstStride
, srcStride
;
2077 assert(util_format_is_compressed(src
->format
));
2079 rgba
= malloc(width
* height
* 4 * sizeof(GLfloat
));
2084 if (ST_DEBUG
& DEBUG_FALLBACK
)
2085 debug_printf("%s: fallback format translation\n", __func__
);
2087 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2088 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2089 srcStride
= 4 * width
* sizeof(GLfloat
);
2090 for (slice
= 0; slice
< depth
; slice
++) {
2091 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2092 width
, height
, format
, type
,
2095 /* get float[4] rgba row from surface */
2096 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, height
,
2099 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2100 rgba
, RGBA32_FLOAT
, srcStride
,
2101 width
, height
, NULL
);
2103 /* Handle byte swapping if required */
2104 if (ctx
->Pack
.SwapBytes
) {
2105 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
2106 width
, height
, dest
, dest
);
2109 map
+= tex_xfer
->layer_stride
;
2118 pipe_transfer_unmap(pipe
, tex_xfer
);
2120 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2121 pipe_resource_reference(&dst
, NULL
);
2125 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2126 width
, height
, depth
,
2127 format
, type
, pixels
, texImage
);
2133 * Do a CopyTexSubImage operation using a read transfer from the source,
2134 * a write transfer to the destination and get_tile()/put_tile() to access
2135 * the pixels/texels.
2137 * Note: srcY=0=TOP of renderbuffer
2140 fallback_copy_texsubimage(struct gl_context
*ctx
,
2141 struct st_renderbuffer
*strb
,
2142 struct st_texture_image
*stImage
,
2144 GLint destX
, GLint destY
, GLint slice
,
2145 GLint srcX
, GLint srcY
,
2146 GLsizei width
, GLsizei height
)
2148 struct st_context
*st
= st_context(ctx
);
2149 struct pipe_context
*pipe
= st
->pipe
;
2150 struct pipe_transfer
*src_trans
;
2152 enum pipe_transfer_usage transfer_usage
;
2154 unsigned dst_width
= width
;
2155 unsigned dst_height
= height
;
2156 unsigned dst_depth
= 1;
2157 struct pipe_transfer
*transfer
;
2159 if (ST_DEBUG
& DEBUG_FALLBACK
)
2160 debug_printf("%s: fallback processing\n", __func__
);
2162 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2163 srcY
= strb
->Base
.Height
- srcY
- height
;
2166 map
= pipe_transfer_map(pipe
,
2168 strb
->surface
->u
.tex
.level
,
2169 strb
->surface
->u
.tex
.first_layer
,
2172 width
, height
, &src_trans
);
2174 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2175 baseFormat
== GL_DEPTH_STENCIL
) &&
2176 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2177 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2179 transfer_usage
= PIPE_TRANSFER_WRITE
;
2181 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2182 destX
, destY
, slice
,
2183 dst_width
, dst_height
, dst_depth
,
2186 if (baseFormat
== GL_DEPTH_COMPONENT
||
2187 baseFormat
== GL_DEPTH_STENCIL
) {
2188 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2189 ctx
->Pixel
.DepthBias
!= 0.0F
);
2193 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2194 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2203 data
= malloc(width
* sizeof(uint
));
2206 /* To avoid a large temp memory allocation, do copy row by row */
2207 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2208 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2210 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2213 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2214 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2215 0, 0, width
, 1, data
);
2218 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2223 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2231 malloc(width
* height
* 4 * sizeof(GLfloat
));
2233 if (tempSrc
&& texDest
) {
2234 const GLint dims
= 2;
2236 struct gl_texture_image
*texImage
= &stImage
->base
;
2237 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2239 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2240 unpack
.Invert
= GL_TRUE
;
2243 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2244 dstRowStride
= transfer
->layer_stride
;
2247 dstRowStride
= transfer
->stride
;
2250 /* get float/RGBA image from framebuffer */
2251 /* XXX this usually involves a lot of int/float conversion.
2252 * try to avoid that someday.
2254 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2255 util_format_linear(strb
->texture
->format
),
2258 /* Store into texture memory.
2259 * Note that this does some special things such as pixel transfer
2260 * ops and format conversion. In particular, if the dest tex format
2261 * is actually RGBA but the user created the texture as GL_RGB we
2262 * need to fill-in/override the alpha channel with 1.0.
2264 _mesa_texstore(ctx
, dims
,
2265 texImage
->_BaseFormat
,
2266 texImage
->TexFormat
,
2270 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2274 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2280 st_texture_image_unmap(st
, stImage
, slice
);
2281 pipe
->transfer_unmap(pipe
, src_trans
);
2286 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2287 * Note that the region to copy has already been clipped so we know we
2288 * won't read from outside the source renderbuffer's bounds.
2290 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2293 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2294 struct gl_texture_image
*texImage
,
2295 GLint destX
, GLint destY
, GLint slice
,
2296 struct gl_renderbuffer
*rb
,
2297 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2299 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2300 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2301 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2302 struct st_context
*st
= st_context(ctx
);
2303 struct pipe_context
*pipe
= st
->pipe
;
2304 struct pipe_screen
*screen
= pipe
->screen
;
2305 struct pipe_blit_info blit
;
2306 enum pipe_format dst_format
;
2307 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2311 st_flush_bitmap_cache(st
);
2312 st_invalidate_readpix_cache(st
);
2314 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2315 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2317 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2318 debug_printf("%s: null strb or stImage\n", __func__
);
2322 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2323 texImage
->TexFormat
)) {
2327 /* The base internal format must match the mesa format, so make sure
2328 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
2330 if (texImage
->_BaseFormat
!=
2331 _mesa_get_format_base_format(texImage
->TexFormat
) ||
2332 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
2336 /* Choose the destination format to match the TexImage behavior. */
2337 dst_format
= util_format_linear(stImage
->pt
->format
);
2338 dst_format
= util_format_luminance_to_red(dst_format
);
2339 dst_format
= util_format_intensity_to_red(dst_format
);
2341 /* See if the destination format is supported. */
2342 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2343 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2344 bind
= PIPE_BIND_DEPTH_STENCIL
;
2347 bind
= PIPE_BIND_RENDER_TARGET
;
2351 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2352 stImage
->pt
->nr_samples
, bind
)) {
2356 /* Y flipping for the main framebuffer. */
2358 srcY1
= strb
->Base
.Height
- srcY
- height
;
2359 srcY0
= srcY1
+ height
;
2363 srcY1
= srcY0
+ height
;
2366 /* Blit the texture.
2367 * This supports flipping, format conversions, and downsampling.
2369 memset(&blit
, 0, sizeof(blit
));
2370 blit
.src
.resource
= strb
->texture
;
2371 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2372 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2373 blit
.src
.box
.x
= srcX
;
2374 blit
.src
.box
.y
= srcY0
;
2375 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2376 blit
.src
.box
.width
= width
;
2377 blit
.src
.box
.height
= srcY1
- srcY0
;
2378 blit
.src
.box
.depth
= 1;
2379 blit
.dst
.resource
= stImage
->pt
;
2380 blit
.dst
.format
= dst_format
;
2381 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2382 blit
.dst
.box
.x
= destX
;
2383 blit
.dst
.box
.y
= destY
;
2384 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2385 blit
.dst
.box
.width
= width
;
2386 blit
.dst
.box
.height
= height
;
2387 blit
.dst
.box
.depth
= 1;
2388 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2389 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2390 pipe
->blit(pipe
, &blit
);
2394 /* software fallback */
2395 fallback_copy_texsubimage(ctx
,
2396 strb
, stImage
, texImage
->_BaseFormat
,
2397 destX
, destY
, slice
,
2398 srcX
, srcY
, width
, height
);
2403 * Copy image data from stImage into the texture object 'stObj' at level
2407 copy_image_data_to_texture(struct st_context
*st
,
2408 struct st_texture_object
*stObj
,
2410 struct st_texture_image
*stImage
)
2414 const struct gl_texture_image MAYBE_UNUSED
*dstImage
=
2415 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2417 assert(dstImage
->Width
== stImage
->base
.Width
);
2418 assert(dstImage
->Height
== stImage
->base
.Height
);
2419 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2423 /* Copy potentially with the blitter:
2426 if (stImage
->pt
->last_level
== 0)
2429 src_level
= stImage
->base
.Level
;
2431 assert(src_level
<= stImage
->pt
->last_level
);
2432 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2433 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2434 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2435 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2436 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2437 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2439 st_texture_image_copy(st
->pipe
,
2440 stObj
->pt
, dstLevel
, /* dest texture, level */
2441 stImage
->pt
, src_level
, /* src texture, level */
2442 stImage
->base
.Face
);
2444 pipe_resource_reference(&stImage
->pt
, NULL
);
2446 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2451 * Called during state validation. When this function is finished,
2452 * the texture object should be ready for rendering.
2453 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2456 st_finalize_texture(struct gl_context
*ctx
,
2457 struct pipe_context
*pipe
,
2458 struct gl_texture_object
*tObj
,
2461 struct st_context
*st
= st_context(ctx
);
2462 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2463 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2465 const struct st_texture_image
*firstImage
;
2466 enum pipe_format firstImageFormat
;
2468 uint16_t ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2470 if (tObj
->Immutable
)
2473 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
2474 /* The texture is complete and we know exactly how many mipmap levels
2475 * are present/needed. This is conditional because we may be called
2476 * from the st_generate_mipmap() function when the texture object is
2477 * incomplete. In that case, we'll have set stObj->lastLevel before
2480 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
2481 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
2482 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2484 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2487 /* Skip the loop over images in the common case of no images having
2488 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
2489 * haven't looked at, then we do need to look at those new images.
2491 if (!stObj
->needs_validation
&&
2492 stObj
->base
.BaseLevel
>= stObj
->validated_first_level
&&
2493 stObj
->lastLevel
<= stObj
->validated_last_level
) {
2497 /* If this texture comes from a window system, there is nothing else to do. */
2498 if (stObj
->surface_based
) {
2502 firstImage
= st_texture_image_const(stObj
->base
.Image
[cubeMapFace
][stObj
->base
.BaseLevel
]);
2505 /* If both firstImage and stObj point to a texture which can contain
2506 * all active images, favour firstImage. Note that because of the
2507 * completeness requirement, we know that the image dimensions
2510 if (firstImage
->pt
&&
2511 firstImage
->pt
!= stObj
->pt
&&
2512 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2513 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2514 st_texture_release_all_sampler_views(st
, stObj
);
2517 /* Find gallium format for the Mesa texture */
2519 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2521 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2524 uint16_t height
, depth
;
2526 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2527 firstImage
->base
.Width2
,
2528 firstImage
->base
.Height2
,
2529 firstImage
->base
.Depth2
,
2530 &width
, &height
, &depth
, &ptLayers
);
2532 /* If we previously allocated a pipe texture and its sizes are
2533 * compatible, use them.
2536 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2537 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2538 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2539 ptWidth
= stObj
->pt
->width0
;
2540 ptHeight
= stObj
->pt
->height0
;
2541 ptDepth
= stObj
->pt
->depth0
;
2543 /* Otherwise, compute a new level=0 size that is compatible with the
2546 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2547 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2548 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2550 /* If the base level image is 1x1x1, we still need to ensure that the
2551 * resulting pipe texture ends up with the required number of levels
2554 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2555 ptWidth
<<= firstImage
->base
.Level
;
2557 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2558 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2562 /* At this point, the texture may be incomplete (mismatched cube
2563 * face sizes, for example). If that's the case, give up, but
2564 * don't return GL_FALSE as that would raise an incorrect
2565 * GL_OUT_OF_MEMORY error. See Piglit fbo-incomplete-texture-03 test.
2567 if (!stObj
->base
._BaseComplete
) {
2568 _mesa_test_texobj_completeness(ctx
, &stObj
->base
);
2569 if (!stObj
->base
._BaseComplete
) {
2575 ptNumSamples
= firstImage
->base
.NumSamples
;
2578 /* If we already have a gallium texture, check that it matches the texture
2579 * object's format, target, size, num_levels, etc.
2582 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2583 stObj
->pt
->format
!= firstImageFormat
||
2584 stObj
->pt
->last_level
< stObj
->lastLevel
||
2585 stObj
->pt
->width0
!= ptWidth
||
2586 stObj
->pt
->height0
!= ptHeight
||
2587 stObj
->pt
->depth0
!= ptDepth
||
2588 stObj
->pt
->nr_samples
!= ptNumSamples
||
2589 stObj
->pt
->array_size
!= ptLayers
)
2591 /* The gallium texture does not match the Mesa texture so delete the
2592 * gallium texture now. We'll make a new one below.
2594 pipe_resource_reference(&stObj
->pt
, NULL
);
2595 st_texture_release_all_sampler_views(st
, stObj
);
2596 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
2600 /* May need to create a new gallium texture:
2603 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2605 stObj
->pt
= st_texture_create(st
,
2606 gl_target_to_pipe(stObj
->base
.Target
),
2612 ptLayers
, ptNumSamples
,
2616 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2621 /* Pull in any images not in the object's texture:
2623 for (face
= 0; face
< nr_faces
; face
++) {
2625 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2626 struct st_texture_image
*stImage
=
2627 st_texture_image(stObj
->base
.Image
[face
][level
]);
2629 /* Need to import images in main memory or held in other textures.
2631 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2635 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2636 height
= u_minify(ptHeight
, level
);
2640 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2641 depth
= u_minify(ptDepth
, level
);
2642 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2648 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2649 stImage
->base
.Height
== height
&&
2650 stImage
->base
.Depth
== depth
)) {
2651 /* src image fits expected dest mipmap level size */
2652 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2658 stObj
->validated_first_level
= stObj
->base
.BaseLevel
;
2659 stObj
->validated_last_level
= stObj
->lastLevel
;
2660 stObj
->needs_validation
= false;
2666 * Allocate a new pipe_resource object
2667 * width0, height0, depth0 are the dimensions of the level 0 image
2668 * (the highest resolution). last_level indicates how many mipmap levels
2669 * to allocate storage for. For non-mipmapped textures, this will be zero.
2671 static struct pipe_resource
*
2672 st_texture_create_from_memory(struct st_context
*st
,
2673 struct st_memory_object
*memObj
,
2675 enum pipe_texture_target target
,
2676 enum pipe_format format
,
2685 struct pipe_resource pt
, *newtex
;
2686 struct pipe_screen
*screen
= st
->pipe
->screen
;
2688 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
2690 assert(height0
> 0);
2692 if (target
== PIPE_TEXTURE_CUBE
)
2693 assert(layers
== 6);
2695 DBG("%s target %d format %s last_level %d\n", __func__
,
2696 (int) target
, util_format_name(format
), last_level
);
2699 assert(screen
->is_format_supported(screen
, format
, target
, 0,
2700 PIPE_BIND_SAMPLER_VIEW
));
2702 memset(&pt
, 0, sizeof(pt
));
2705 pt
.last_level
= last_level
;
2707 pt
.height0
= height0
;
2709 pt
.array_size
= layers
;
2710 pt
.usage
= PIPE_USAGE_DEFAULT
;
2712 /* only set this for OpenGL textures, not renderbuffers */
2713 pt
.flags
= PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
;
2714 pt
.nr_samples
= nr_samples
;
2716 newtex
= screen
->resource_from_memobj(screen
, &pt
, memObj
->memory
, offset
);
2718 assert(!newtex
|| pipe_is_referenced(&newtex
->reference
));
2724 * Allocate texture memory for a whole mipmap stack.
2725 * Note: for multisample textures if the requested sample count is not
2726 * supported, we search for the next higher supported sample count.
2729 st_texture_storage(struct gl_context
*ctx
,
2730 struct gl_texture_object
*texObj
,
2731 GLsizei levels
, GLsizei width
,
2732 GLsizei height
, GLsizei depth
,
2733 struct gl_memory_object
*memObj
,
2736 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2737 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2738 struct st_context
*st
= st_context(ctx
);
2739 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2740 struct st_memory_object
*smObj
= st_memory_object(memObj
);
2741 struct pipe_screen
*screen
= st
->pipe
->screen
;
2742 unsigned ptWidth
, bindings
;
2743 uint16_t ptHeight
, ptDepth
, ptLayers
;
2744 enum pipe_format fmt
;
2746 GLuint num_samples
= texImage
->NumSamples
;
2750 stObj
->lastLevel
= levels
- 1;
2752 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2754 bindings
= default_bindings(st
, fmt
);
2756 if (num_samples
> 0) {
2757 /* Find msaa sample count which is actually supported. For example,
2758 * if the user requests 1x but only 4x or 8x msaa is supported, we'll
2761 enum pipe_texture_target ptarget
= gl_target_to_pipe(texObj
->Target
);
2762 boolean found
= FALSE
;
2764 if (ctx
->Const
.MaxSamples
> 1 && num_samples
== 1) {
2765 /* don't try num_samples = 1 with drivers that support real msaa */
2769 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2770 if (screen
->is_format_supported(screen
, fmt
, ptarget
,
2772 PIPE_BIND_SAMPLER_VIEW
)) {
2773 /* Update the sample count in gl_texture_image as well. */
2774 texImage
->NumSamples
= num_samples
;
2785 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2786 width
, height
, depth
,
2787 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2790 stObj
->pt
= st_texture_create_from_memory(st
,
2793 gl_target_to_pipe(texObj
->Target
),
2799 ptLayers
, num_samples
,
2803 stObj
->pt
= st_texture_create(st
,
2804 gl_target_to_pipe(texObj
->Target
),
2810 ptLayers
, num_samples
,
2817 /* Set image resource pointers */
2818 for (level
= 0; level
< levels
; level
++) {
2820 for (face
= 0; face
< numFaces
; face
++) {
2821 struct st_texture_image
*stImage
=
2822 st_texture_image(texObj
->Image
[face
][level
]);
2823 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2825 etc_fallback_allocate(st
, stImage
);
2829 /* The texture is in a validated state, so no need to check later. */
2830 stObj
->needs_validation
= false;
2831 stObj
->validated_first_level
= 0;
2832 stObj
->validated_last_level
= levels
- 1;
2838 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2839 * for a whole mipmap stack.
2842 st_AllocTextureStorage(struct gl_context
*ctx
,
2843 struct gl_texture_object
*texObj
,
2844 GLsizei levels
, GLsizei width
,
2845 GLsizei height
, GLsizei depth
)
2847 return st_texture_storage(ctx
, texObj
, levels
,
2848 width
, height
, depth
,
2854 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2855 GLuint numLevels
, GLint level
,
2856 mesa_format format
, GLuint numSamples
,
2857 GLint width
, GLint height
, GLint depth
)
2859 struct st_context
*st
= st_context(ctx
);
2860 struct pipe_context
*pipe
= st
->pipe
;
2862 if (width
== 0 || height
== 0 || depth
== 0) {
2863 /* zero-sized images are legal, and always fit! */
2867 if (pipe
->screen
->can_create_resource
) {
2868 /* Ask the gallium driver if the texture is too large */
2869 struct gl_texture_object
*texObj
=
2870 _mesa_get_current_tex_object(ctx
, target
);
2871 struct pipe_resource pt
;
2873 /* Setup the pipe_resource object
2875 memset(&pt
, 0, sizeof(pt
));
2877 pt
.target
= gl_target_to_pipe(target
);
2878 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
2879 pt
.nr_samples
= numSamples
;
2881 st_gl_texture_dims_to_pipe_dims(target
,
2882 width
, height
, depth
,
2883 &pt
.width0
, &pt
.height0
,
2884 &pt
.depth0
, &pt
.array_size
);
2886 if (numLevels
> 0) {
2887 /* For immutable textures we know the final number of mip levels */
2888 pt
.last_level
= numLevels
- 1;
2890 else if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
2891 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
2892 /* assume just one mipmap level */
2896 /* assume a full set of mipmaps */
2897 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
2900 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
2903 /* Use core Mesa fallback */
2904 return _mesa_test_proxy_teximage(ctx
, target
, numLevels
, level
, format
,
2905 numSamples
, width
, height
, depth
);
2910 st_TextureView(struct gl_context
*ctx
,
2911 struct gl_texture_object
*texObj
,
2912 struct gl_texture_object
*origTexObj
)
2914 struct st_context
*st
= st_context(ctx
);
2915 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
2916 struct st_texture_object
*tex
= st_texture_object(texObj
);
2917 struct gl_texture_image
*image
= texObj
->Image
[0][0];
2919 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2920 const int numLevels
= texObj
->NumLevels
;
2925 pipe_resource_reference(&tex
->pt
, orig
->pt
);
2927 /* Set image resource pointers */
2928 for (level
= 0; level
< numLevels
; level
++) {
2929 for (face
= 0; face
< numFaces
; face
++) {
2930 struct st_texture_image
*stImage
=
2931 st_texture_image(texObj
->Image
[face
][level
]);
2932 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
2936 tex
->surface_based
= GL_TRUE
;
2937 tex
->surface_format
=
2938 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
2940 tex
->lastLevel
= numLevels
- 1;
2942 /* free texture sampler views. They need to be recreated when we
2943 * change the texture view parameters.
2945 st_texture_release_all_sampler_views(st
, tex
);
2947 /* The texture is in a validated state, so no need to check later. */
2948 tex
->needs_validation
= false;
2949 tex
->validated_first_level
= 0;
2950 tex
->validated_last_level
= numLevels
- 1;
2957 * Find the mipmap level in 'pt' which matches the level described by
2961 find_mipmap_level(const struct gl_texture_image
*texImage
,
2962 const struct pipe_resource
*pt
)
2964 const GLenum target
= texImage
->TexObject
->Target
;
2965 GLint texWidth
= texImage
->Width
;
2966 GLint texHeight
= texImage
->Height
;
2967 GLint texDepth
= texImage
->Depth
;
2969 uint16_t h
, d
, layers
;
2971 st_gl_texture_dims_to_pipe_dims(target
, texWidth
, texHeight
, texDepth
,
2972 &w
, &h
, &d
, &layers
);
2974 for (level
= 0; level
<= pt
->last_level
; level
++) {
2975 if (u_minify(pt
->width0
, level
) == w
&&
2976 u_minify(pt
->height0
, level
) == h
&&
2977 u_minify(pt
->depth0
, level
) == d
) {
2982 /* If we get here, there must be some sort of inconsistency between
2983 * the Mesa texture object/images and the gallium resource.
2985 debug_printf("Inconsistent textures in find_mipmap_level()\n");
2987 return texImage
->Level
;
2992 st_ClearTexSubImage(struct gl_context
*ctx
,
2993 struct gl_texture_image
*texImage
,
2994 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2995 GLsizei width
, GLsizei height
, GLsizei depth
,
2996 const void *clearValue
)
2998 static const char zeros
[16] = {0};
2999 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3000 struct st_texture_image
*stImage
= st_texture_image(texImage
);
3001 struct pipe_resource
*pt
= stImage
->pt
;
3002 struct st_context
*st
= st_context(ctx
);
3003 struct pipe_context
*pipe
= st
->pipe
;
3005 struct pipe_box box
;
3010 st_flush_bitmap_cache(st
);
3011 st_invalidate_readpix_cache(st
);
3013 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
3014 width
, height
, depth
, &box
);
3015 if (texObj
->Immutable
) {
3016 /* The texture object has to be consistent (no "loose", per-image
3017 * gallium resources). If this texture is a view into another
3018 * texture, we have to apply the MinLevel/Layer offsets. If this is
3019 * not a texture view, the offsets will be zero.
3021 assert(stImage
->pt
== st_texture_object(texObj
)->pt
);
3022 level
= texImage
->Level
+ texObj
->MinLevel
;
3023 box
.z
+= texObj
->MinLayer
;
3026 /* Texture level sizes may be inconsistent. We my have "loose",
3027 * per-image gallium resources. The texImage->Level may not match
3028 * the gallium resource texture level.
3030 level
= find_mipmap_level(texImage
, pt
);
3033 assert(level
<= pt
->last_level
);
3035 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
3040 * Called via the glTexParam*() function, but only when some texture object
3041 * state has actually changed.
3044 st_TexParameter(struct gl_context
*ctx
,
3045 struct gl_texture_object
*texObj
, GLenum pname
)
3047 struct st_context
*st
= st_context(ctx
);
3048 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3051 case GL_TEXTURE_BASE_LEVEL
:
3052 case GL_TEXTURE_MAX_LEVEL
:
3053 case GL_DEPTH_TEXTURE_MODE
:
3054 case GL_DEPTH_STENCIL_TEXTURE_MODE
:
3055 case GL_TEXTURE_SRGB_DECODE_EXT
:
3056 case GL_TEXTURE_SWIZZLE_R
:
3057 case GL_TEXTURE_SWIZZLE_G
:
3058 case GL_TEXTURE_SWIZZLE_B
:
3059 case GL_TEXTURE_SWIZZLE_A
:
3060 case GL_TEXTURE_SWIZZLE_RGBA
:
3061 case GL_TEXTURE_BUFFER_SIZE
:
3062 case GL_TEXTURE_BUFFER_OFFSET
:
3063 /* changing any of these texture parameters means we must create
3064 * new sampler views.
3066 st_texture_release_all_sampler_views(st
, stObj
);
3074 st_SetTextureStorageForMemoryObject(struct gl_context
*ctx
,
3075 struct gl_texture_object
*texObj
,
3076 struct gl_memory_object
*memObj
,
3077 GLsizei levels
, GLsizei width
,
3078 GLsizei height
, GLsizei depth
,
3081 return st_texture_storage(ctx
, texObj
, levels
,
3082 width
, height
, depth
,
3087 st_NewTextureHandle(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
3088 struct gl_sampler_object
*sampObj
)
3090 struct st_context
*st
= st_context(ctx
);
3091 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3092 struct pipe_context
*pipe
= st
->pipe
;
3093 struct pipe_sampler_view
*view
;
3094 struct pipe_sampler_state sampler
= {0};
3096 if (texObj
->Target
!= GL_TEXTURE_BUFFER
) {
3097 if (!st_finalize_texture(ctx
, pipe
, texObj
, 0))
3100 st_convert_sampler(st
, texObj
, sampObj
, 0, &sampler
);
3102 /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
3103 view
= st_get_texture_sampler_view_from_stobj(st
, stObj
, sampObj
, 0, true);
3105 view
= st_get_buffer_sampler_view_from_stobj(st
, stObj
);
3108 return pipe
->create_texture_handle(pipe
, view
, &sampler
);
3113 st_DeleteTextureHandle(struct gl_context
*ctx
, GLuint64 handle
)
3115 struct st_context
*st
= st_context(ctx
);
3116 struct pipe_context
*pipe
= st
->pipe
;
3118 pipe
->delete_texture_handle(pipe
, handle
);
3123 st_MakeTextureHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3126 struct st_context
*st
= st_context(ctx
);
3127 struct pipe_context
*pipe
= st
->pipe
;
3129 pipe
->make_texture_handle_resident(pipe
, handle
, resident
);
3134 st_NewImageHandle(struct gl_context
*ctx
, struct gl_image_unit
*imgObj
)
3136 struct st_context
*st
= st_context(ctx
);
3137 struct pipe_context
*pipe
= st
->pipe
;
3138 struct pipe_image_view image
;
3140 st_convert_image(st
, imgObj
, &image
);
3142 return pipe
->create_image_handle(pipe
, &image
);
3147 st_DeleteImageHandle(struct gl_context
*ctx
, GLuint64 handle
)
3149 struct st_context
*st
= st_context(ctx
);
3150 struct pipe_context
*pipe
= st
->pipe
;
3152 pipe
->delete_image_handle(pipe
, handle
);
3157 st_MakeImageHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3158 GLenum access
, bool resident
)
3160 struct st_context
*st
= st_context(ctx
);
3161 struct pipe_context
*pipe
= st
->pipe
;
3163 pipe
->make_image_handle_resident(pipe
, handle
, access
, resident
);
3168 st_init_texture_functions(struct dd_function_table
*functions
)
3170 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
3171 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
3172 functions
->TexImage
= st_TexImage
;
3173 functions
->TexSubImage
= st_TexSubImage
;
3174 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
3175 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
3176 functions
->GenerateMipmap
= st_generate_mipmap
;
3178 functions
->GetTexSubImage
= st_GetTexSubImage
;
3180 /* compressed texture functions */
3181 functions
->CompressedTexImage
= st_CompressedTexImage
;
3183 functions
->NewTextureObject
= st_NewTextureObject
;
3184 functions
->NewTextureImage
= st_NewTextureImage
;
3185 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
3186 functions
->DeleteTexture
= st_DeleteTextureObject
;
3187 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
3188 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
3189 functions
->MapTextureImage
= st_MapTextureImage
;
3190 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
3192 /* XXX Temporary until we can query pipe's texture sizes */
3193 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
3195 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
3196 functions
->TextureView
= st_TextureView
;
3197 functions
->ClearTexSubImage
= st_ClearTexSubImage
;
3199 functions
->TexParameter
= st_TexParameter
;
3201 /* bindless functions */
3202 functions
->NewTextureHandle
= st_NewTextureHandle
;
3203 functions
->DeleteTextureHandle
= st_DeleteTextureHandle
;
3204 functions
->MakeTextureHandleResident
= st_MakeTextureHandleResident
;
3205 functions
->NewImageHandle
= st_NewImageHandle
;
3206 functions
->DeleteImageHandle
= st_DeleteImageHandle
;
3207 functions
->MakeImageHandleResident
= st_MakeImageHandleResident
;
3209 /* external object functions */
3210 functions
->SetTextureStorageForMemoryObject
= st_SetTextureStorageForMemoryObject
;