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_etc_fallback(struct st_context
*st
, struct gl_texture_image
*texImage
)
224 return (_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
225 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
);
229 etc_fallback_allocate(struct st_context
*st
, struct st_texture_image
*stImage
)
231 struct gl_texture_image
*texImage
= &stImage
->base
;
233 if (!st_etc_fallback(st
, texImage
))
236 if (stImage
->etc_data
)
237 free(stImage
->etc_data
);
239 unsigned data_size
= _mesa_format_image_size(texImage
->TexFormat
,
245 malloc(data_size
* _mesa_num_tex_faces(texImage
->TexObject
->Target
));
248 /** called via ctx->Driver.MapTextureImage() */
250 st_MapTextureImage(struct gl_context
*ctx
,
251 struct gl_texture_image
*texImage
,
252 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
254 GLubyte
**mapOut
, GLint
*rowStrideOut
)
256 struct st_context
*st
= st_context(ctx
);
257 struct st_texture_image
*stImage
= st_texture_image(texImage
);
259 struct pipe_transfer
*transfer
;
261 /* Check for unexpected flags */
262 assert((mode
& ~(GL_MAP_READ_BIT
|
264 GL_MAP_INVALIDATE_RANGE_BIT
)) == 0);
266 const enum pipe_transfer_usage transfer_flags
=
267 st_access_flags_to_transfer_flags(mode
, false);
269 map
= st_texture_image_map(st
, stImage
, transfer_flags
, x
, y
, slice
, w
, h
, 1,
272 if (st_etc_fallback(st
, texImage
)) {
273 /* ETC isn't supported by all gallium drivers, where it's represented
274 * by uncompressed formats. We store the compressed data (as it's
275 * needed for image copies in OES_copy_image), and decompress as
276 * necessary in Unmap.
278 * Note: all ETC1/ETC2 formats have 4x4 block sizes.
280 unsigned z
= transfer
->box
.z
;
281 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
283 unsigned bytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
284 unsigned stride
= *rowStrideOut
= itransfer
->temp_stride
=
285 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width2
);
286 *mapOut
= itransfer
->temp_data
=
287 stImage
->etc_data
+ ((x
/ 4) * bytes
+ (y
/ 4) * stride
) +
288 z
* stride
* texImage
->Height2
/ 4;
289 itransfer
->map
= map
;
292 /* supported mapping */
294 *rowStrideOut
= transfer
->stride
;
304 /** called via ctx->Driver.UnmapTextureImage() */
306 st_UnmapTextureImage(struct gl_context
*ctx
,
307 struct gl_texture_image
*texImage
,
310 struct st_context
*st
= st_context(ctx
);
311 struct st_texture_image
*stImage
= st_texture_image(texImage
);
313 if (st_etc_fallback(st
, texImage
)) {
314 /* Decompress the ETC texture to the mapped one. */
315 unsigned z
= slice
+ stImage
->base
.Face
;
316 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
317 struct pipe_transfer
*transfer
= itransfer
->transfer
;
319 assert(z
== transfer
->box
.z
);
321 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
322 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
323 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
324 itransfer
->temp_data
,
325 itransfer
->temp_stride
,
326 transfer
->box
.width
, transfer
->box
.height
);
329 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
330 itransfer
->temp_data
, itransfer
->temp_stride
,
331 transfer
->box
.width
, transfer
->box
.height
,
332 texImage
->TexFormat
);
336 itransfer
->temp_data
= NULL
;
337 itransfer
->temp_stride
= 0;
341 st_texture_image_unmap(st
, stImage
, slice
);
346 * Return default texture resource binding bitmask for the given format.
349 default_bindings(struct st_context
*st
, enum pipe_format format
)
351 struct pipe_screen
*screen
= st
->pipe
->screen
;
352 const unsigned target
= PIPE_TEXTURE_2D
;
355 if (util_format_is_depth_or_stencil(format
))
356 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
358 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
360 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
364 format
= util_format_linear(format
);
366 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
369 return PIPE_BIND_SAMPLER_VIEW
;
375 * Given the size of a mipmap image, try to compute the size of the level=0
378 * Note that this isn't always accurate for odd-sized, non-POW textures.
379 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
381 * \return GL_TRUE for success, GL_FALSE for failure
384 guess_base_level_size(GLenum target
,
385 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
386 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
393 /* Guess the size of the base level.
394 * Depending on the image's size, we can't always make a guess here.
398 case GL_TEXTURE_1D_ARRAY
:
403 case GL_TEXTURE_2D_ARRAY
:
404 /* We can't make a good guess here, because the base level dimensions
407 if (width
== 1 || height
== 1) {
414 case GL_TEXTURE_CUBE_MAP
:
415 case GL_TEXTURE_CUBE_MAP_ARRAY
:
421 /* We can't make a good guess here, because the base level dimensions
424 if (width
== 1 || height
== 1 || depth
== 1) {
432 case GL_TEXTURE_RECTANGLE
:
449 * Try to determine whether we should allocate memory for a full texture
450 * mipmap. The problem is when we get a glTexImage(level=0) call, we
451 * can't immediately know if other mipmap levels are coming next. Here
452 * we try to guess whether to allocate memory for a mipmap or just the
455 * If we guess incorrectly here we'll later reallocate the right amount of
456 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
458 * \param stObj the texture object we're going to allocate memory for.
459 * \param stImage describes the incoming image which we need to store.
462 allocate_full_mipmap(const struct st_texture_object
*stObj
,
463 const struct st_texture_image
*stImage
)
465 switch (stObj
->base
.Target
) {
466 case GL_TEXTURE_RECTANGLE_NV
:
467 case GL_TEXTURE_BUFFER
:
468 case GL_TEXTURE_EXTERNAL_OES
:
469 case GL_TEXTURE_2D_MULTISAMPLE
:
470 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
471 /* these texture types cannot be mipmapped */
475 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
478 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
479 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
480 /* depth/stencil textures are seldom mipmapped */
483 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
486 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
487 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
488 /* not a mipmap minification filter */
491 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
492 /* 3D textures are seldom mipmapped */
500 * Try to allocate a pipe_resource object for the given st_texture_object.
502 * We use the given st_texture_image as a clue to determine the size of the
503 * mipmap image at level=0.
505 * \return GL_TRUE for success, GL_FALSE if out of memory.
508 guess_and_alloc_texture(struct st_context
*st
,
509 struct st_texture_object
*stObj
,
510 const struct st_texture_image
*stImage
)
512 const struct gl_texture_image
*firstImage
;
513 GLuint lastLevel
, width
, height
, depth
;
516 uint16_t ptHeight
, ptDepth
, ptLayers
;
517 enum pipe_format fmt
;
518 bool guessed_box
= false;
520 DBG("%s\n", __func__
);
524 /* If a base level image with compatible size exists, use that as our guess.
526 firstImage
= _mesa_base_tex_image(&stObj
->base
);
528 firstImage
->Width2
> 0 &&
529 firstImage
->Height2
> 0 &&
530 firstImage
->Depth2
> 0 &&
531 guess_base_level_size(stObj
->base
.Target
,
536 &width
, &height
, &depth
)) {
537 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
538 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
539 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
544 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
545 stImage
->base
.Width2
,
546 stImage
->base
.Height2
,
547 stImage
->base
.Depth2
,
549 &width
, &height
, &depth
);
552 /* we can't determine the image size at level=0 */
553 /* this is not an out of memory error */
557 /* At this point, (width x height x depth) is the expected size of
558 * the level=0 mipmap image.
561 /* Guess a reasonable value for lastLevel. With OpenGL we have no
562 * idea how many mipmap levels will be in a texture until we start
563 * to render with it. Make an educated guess here but be prepared
564 * to re-allocating a texture buffer with space for more (or fewer)
565 * mipmap levels later.
567 if (allocate_full_mipmap(stObj
, stImage
)) {
568 /* alloc space for a full mipmap */
569 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
570 width
, height
, depth
) - 1;
573 /* only alloc space for a single mipmap level */
577 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
579 bindings
= default_bindings(st
, fmt
);
581 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
582 width
, height
, depth
,
583 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
585 stObj
->pt
= st_texture_create(st
,
586 gl_target_to_pipe(stObj
->base
.Target
),
595 stObj
->lastLevel
= lastLevel
;
597 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
599 return stObj
->pt
!= NULL
;
604 * Called via ctx->Driver.AllocTextureImageBuffer().
605 * If the texture object/buffer already has space for the indicated image,
606 * we're done. Otherwise, allocate memory for the new texture image.
609 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
610 struct gl_texture_image
*texImage
)
612 struct st_context
*st
= st_context(ctx
);
613 struct st_texture_image
*stImage
= st_texture_image(texImage
);
614 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
615 const GLuint level
= texImage
->Level
;
616 GLuint width
= texImage
->Width
;
617 GLuint height
= texImage
->Height
;
618 GLuint depth
= texImage
->Depth
;
620 DBG("%s\n", __func__
);
622 assert(!stImage
->pt
); /* xxx this might be wrong */
624 stObj
->needs_validation
= true;
626 etc_fallback_allocate(st
, stImage
);
628 /* Look if the parent texture object has space for this image */
630 level
<= stObj
->pt
->last_level
&&
631 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
632 /* this image will fit in the existing texture object's memory */
633 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
637 /* The parent texture object does not have space for this image */
639 pipe_resource_reference(&stObj
->pt
, NULL
);
640 st_texture_release_all_sampler_views(st
, stObj
);
642 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
643 /* Probably out of memory.
644 * Try flushing any pending rendering, then retry.
647 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
648 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
654 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
655 /* The image will live in the object's mipmap memory */
656 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
661 /* Create a new, temporary texture/resource/buffer to hold this
662 * one texture image. Note that when we later access this image
663 * (either for mapping or copying) we'll want to always specify
664 * mipmap level=0, even if the image represents some other mipmap
667 enum pipe_format format
=
668 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
669 GLuint bindings
= default_bindings(st
, format
);
671 uint16_t ptHeight
, ptDepth
, ptLayers
;
673 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
674 width
, height
, depth
,
675 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
677 stImage
->pt
= st_texture_create(st
,
678 gl_target_to_pipe(stObj
->base
.Target
),
686 return stImage
->pt
!= NULL
;
692 * Preparation prior to glTexImage. Basically check the 'surface_based'
693 * field and switch to a "normal" tex image if necessary.
696 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
697 GLenum format
, GLenum type
)
699 struct gl_texture_object
*texObj
= texImage
->TexObject
;
700 struct st_texture_object
*stObj
= st_texture_object(texObj
);
702 /* switch to "normal" */
703 if (stObj
->surface_based
) {
704 const GLenum target
= texObj
->Target
;
705 const GLuint level
= texImage
->Level
;
706 mesa_format texFormat
;
708 assert(!st_texture_image(texImage
)->pt
);
709 _mesa_clear_texture_object(ctx
, texObj
, texImage
);
710 stObj
->layer_override
= 0;
711 stObj
->level_override
= 0;
712 pipe_resource_reference(&stObj
->pt
, NULL
);
714 /* oops, need to init this image again */
715 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
716 texImage
->InternalFormat
, format
,
719 _mesa_init_teximage_fields(ctx
, texImage
,
720 texImage
->Width
, texImage
->Height
,
721 texImage
->Depth
, texImage
->Border
,
722 texImage
->InternalFormat
, texFormat
);
724 stObj
->surface_based
= GL_FALSE
;
730 * Return a writemask for the gallium blit. The parameters can be base
731 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
734 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
737 case GL_DEPTH_STENCIL
:
739 case GL_DEPTH_STENCIL
:
741 case GL_DEPTH_COMPONENT
:
743 case GL_STENCIL_INDEX
:
750 case GL_DEPTH_COMPONENT
:
752 case GL_DEPTH_STENCIL
:
753 case GL_DEPTH_COMPONENT
:
760 case GL_STENCIL_INDEX
:
762 case GL_STENCIL_INDEX
:
770 return PIPE_MASK_RGBA
;
775 * Converts format to a format with the same components, types
776 * and sizes, but with the components in RGBA order.
778 static enum pipe_format
779 unswizzle_format(enum pipe_format format
)
783 case PIPE_FORMAT_B8G8R8A8_UNORM
:
784 case PIPE_FORMAT_A8R8G8B8_UNORM
:
785 case PIPE_FORMAT_A8B8G8R8_UNORM
:
786 return PIPE_FORMAT_R8G8B8A8_UNORM
;
788 case PIPE_FORMAT_B10G10R10A2_UNORM
:
789 return PIPE_FORMAT_R10G10B10A2_UNORM
;
791 case PIPE_FORMAT_B10G10R10A2_SNORM
:
792 return PIPE_FORMAT_R10G10B10A2_SNORM
;
794 case PIPE_FORMAT_B10G10R10A2_UINT
:
795 return PIPE_FORMAT_R10G10B10A2_UINT
;
803 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
805 static enum pipe_format
806 alpha_to_red(enum pipe_format format
)
810 case PIPE_FORMAT_A8_UNORM
:
811 return PIPE_FORMAT_R8_UNORM
;
812 case PIPE_FORMAT_A8_SNORM
:
813 return PIPE_FORMAT_R8_SNORM
;
814 case PIPE_FORMAT_A8_UINT
:
815 return PIPE_FORMAT_R8_UINT
;
816 case PIPE_FORMAT_A8_SINT
:
817 return PIPE_FORMAT_R8_SINT
;
819 case PIPE_FORMAT_A16_UNORM
:
820 return PIPE_FORMAT_R16_UNORM
;
821 case PIPE_FORMAT_A16_SNORM
:
822 return PIPE_FORMAT_R16_SNORM
;
823 case PIPE_FORMAT_A16_UINT
:
824 return PIPE_FORMAT_R16_UINT
;
825 case PIPE_FORMAT_A16_SINT
:
826 return PIPE_FORMAT_R16_SINT
;
827 case PIPE_FORMAT_A16_FLOAT
:
828 return PIPE_FORMAT_R16_FLOAT
;
830 case PIPE_FORMAT_A32_UINT
:
831 return PIPE_FORMAT_R32_UINT
;
832 case PIPE_FORMAT_A32_SINT
:
833 return PIPE_FORMAT_R32_SINT
;
834 case PIPE_FORMAT_A32_FLOAT
:
835 return PIPE_FORMAT_R32_FLOAT
;
843 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
845 static enum pipe_format
846 red_alpha_to_red_green(enum pipe_format format
)
850 case PIPE_FORMAT_R8A8_UNORM
:
851 return PIPE_FORMAT_R8G8_UNORM
;
852 case PIPE_FORMAT_R8A8_SNORM
:
853 return PIPE_FORMAT_R8G8_SNORM
;
854 case PIPE_FORMAT_R8A8_UINT
:
855 return PIPE_FORMAT_R8G8_UINT
;
856 case PIPE_FORMAT_R8A8_SINT
:
857 return PIPE_FORMAT_R8G8_SINT
;
859 case PIPE_FORMAT_R16A16_UNORM
:
860 return PIPE_FORMAT_R16G16_UNORM
;
861 case PIPE_FORMAT_R16A16_SNORM
:
862 return PIPE_FORMAT_R16G16_SNORM
;
863 case PIPE_FORMAT_R16A16_UINT
:
864 return PIPE_FORMAT_R16G16_UINT
;
865 case PIPE_FORMAT_R16A16_SINT
:
866 return PIPE_FORMAT_R16G16_SINT
;
867 case PIPE_FORMAT_R16A16_FLOAT
:
868 return PIPE_FORMAT_R16G16_FLOAT
;
870 case PIPE_FORMAT_R32A32_UINT
:
871 return PIPE_FORMAT_R32G32_UINT
;
872 case PIPE_FORMAT_R32A32_SINT
:
873 return PIPE_FORMAT_R32G32_SINT
;
874 case PIPE_FORMAT_R32A32_FLOAT
:
875 return PIPE_FORMAT_R32G32_FLOAT
;
883 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
885 static enum pipe_format
886 luminance_alpha_to_red_green(enum pipe_format format
)
890 case PIPE_FORMAT_L8A8_UNORM
:
891 return PIPE_FORMAT_R8G8_UNORM
;
892 case PIPE_FORMAT_L8A8_SNORM
:
893 return PIPE_FORMAT_R8G8_SNORM
;
894 case PIPE_FORMAT_L8A8_UINT
:
895 return PIPE_FORMAT_R8G8_UINT
;
896 case PIPE_FORMAT_L8A8_SINT
:
897 return PIPE_FORMAT_R8G8_SINT
;
899 case PIPE_FORMAT_L16A16_UNORM
:
900 return PIPE_FORMAT_R16G16_UNORM
;
901 case PIPE_FORMAT_L16A16_SNORM
:
902 return PIPE_FORMAT_R16G16_SNORM
;
903 case PIPE_FORMAT_L16A16_UINT
:
904 return PIPE_FORMAT_R16G16_UINT
;
905 case PIPE_FORMAT_L16A16_SINT
:
906 return PIPE_FORMAT_R16G16_SINT
;
907 case PIPE_FORMAT_L16A16_FLOAT
:
908 return PIPE_FORMAT_R16G16_FLOAT
;
910 case PIPE_FORMAT_L32A32_UINT
:
911 return PIPE_FORMAT_R32G32_UINT
;
912 case PIPE_FORMAT_L32A32_SINT
:
913 return PIPE_FORMAT_R32G32_SINT
;
914 case PIPE_FORMAT_L32A32_FLOAT
:
915 return PIPE_FORMAT_R32G32_FLOAT
;
923 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
926 format_is_alpha(enum pipe_format format
)
928 const struct util_format_description
*desc
= util_format_description(format
);
930 if (desc
->nr_channels
== 1 &&
931 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
932 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
933 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
934 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
941 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
944 format_is_red(enum pipe_format format
)
946 const struct util_format_description
*desc
= util_format_description(format
);
948 if (desc
->nr_channels
== 1 &&
949 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
950 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
951 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
952 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
960 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
963 format_is_luminance(enum pipe_format format
)
965 const struct util_format_description
*desc
= util_format_description(format
);
967 if (desc
->nr_channels
== 1 &&
968 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
969 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
970 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
971 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
978 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
981 format_is_red_alpha(enum pipe_format format
)
983 const struct util_format_description
*desc
= util_format_description(format
);
985 if (desc
->nr_channels
== 2 &&
986 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
987 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
988 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
989 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
996 format_is_swizzled_rgba(enum pipe_format format
)
998 const struct util_format_description
*desc
= util_format_description(format
);
1000 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
1001 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
1002 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
1003 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
1011 unsigned char swizzle
[4];
1012 enum pipe_format format
;
1015 static const struct format_table table_8888_unorm
[] = {
1016 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
1017 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
1018 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
1019 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
1022 static const struct format_table table_1010102_unorm
[] = {
1023 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
1024 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
1027 static const struct format_table table_1010102_snorm
[] = {
1028 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
1029 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
1032 static const struct format_table table_1010102_uint
[] = {
1033 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
1034 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
1037 static enum pipe_format
1038 swizzle_format(enum pipe_format format
, const int * const swizzle
)
1043 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1044 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1045 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1046 case PIPE_FORMAT_A8B8G8R8_UNORM
:
1047 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
1048 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
1049 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
1050 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
1051 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
1052 return table_8888_unorm
[i
].format
;
1056 case PIPE_FORMAT_R10G10B10A2_UNORM
:
1057 case PIPE_FORMAT_B10G10R10A2_UNORM
:
1058 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
1059 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
1060 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
1061 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
1062 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
1063 return table_1010102_unorm
[i
].format
;
1067 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1068 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1069 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1070 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1071 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1072 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1073 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1074 return table_1010102_snorm
[i
].format
;
1078 case PIPE_FORMAT_R10G10B10A2_UINT
:
1079 case PIPE_FORMAT_B10G10R10A2_UINT
:
1080 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1081 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1082 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1083 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1084 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1085 return table_1010102_uint
[i
].format
;
1093 return PIPE_FORMAT_NONE
;
1097 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1099 enum pipe_format src
= *src_format
;
1100 enum pipe_format dst
= *dst_format
;
1102 /* Note: dst_format has already been transformed from luminance/intensity
1103 * to red when this function is called. The source format will never
1104 * be an intensity format, because GL_INTENSITY is not a legal value
1105 * for the format parameter in glTex(Sub)Image(). */
1107 if (format_is_alpha(src
)) {
1108 if (!format_is_alpha(dst
))
1111 src
= alpha_to_red(src
);
1112 dst
= alpha_to_red(dst
);
1113 } else if (format_is_luminance(src
)) {
1114 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1117 src
= util_format_luminance_to_red(src
);
1118 } else if (util_format_is_luminance_alpha(src
)) {
1119 src
= luminance_alpha_to_red_green(src
);
1121 if (format_is_red_alpha(dst
)) {
1122 dst
= red_alpha_to_red_green(dst
);
1123 } else if (!format_is_red(dst
))
1125 } else if (format_is_swizzled_rgba(src
)) {
1126 const struct util_format_description
*src_desc
= util_format_description(src
);
1127 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1131 /* Make sure the format is an RGBA and not an RGBX format */
1132 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1135 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1138 for (i
= 0; i
< 4; i
++)
1139 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1141 dst
= swizzle_format(dst
, swizzle
);
1142 if (dst
== PIPE_FORMAT_NONE
)
1145 src
= unswizzle_format(src
);
1154 try_pbo_upload_common(struct gl_context
*ctx
,
1155 struct pipe_surface
*surface
,
1156 const struct st_pbo_addresses
*addr
,
1157 enum pipe_format src_format
)
1159 struct st_context
*st
= st_context(ctx
);
1160 struct cso_context
*cso
= st
->cso_context
;
1161 struct pipe_context
*pipe
= st
->pipe
;
1162 bool success
= false;
1165 fs
= st_pbo_get_upload_fs(st
, src_format
, surface
->format
);
1169 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1170 CSO_BIT_FRAGMENT_SAMPLERS
|
1171 CSO_BIT_VERTEX_ELEMENTS
|
1172 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1173 CSO_BIT_FRAMEBUFFER
|
1176 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1177 CSO_BIT_RASTERIZER
|
1178 CSO_BIT_STREAM_OUTPUTS
|
1179 CSO_BIT_PAUSE_QUERIES
|
1180 CSO_BIT_SAMPLE_MASK
|
1181 CSO_BIT_MIN_SAMPLES
|
1182 CSO_BIT_RENDER_CONDITION
|
1183 CSO_BITS_ALL_SHADERS
));
1184 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1186 cso_set_sample_mask(cso
, ~0);
1187 cso_set_min_samples(cso
, 1);
1188 cso_set_render_condition(cso
, NULL
, FALSE
, 0);
1190 /* Set up the sampler_view */
1192 struct pipe_sampler_view templ
;
1193 struct pipe_sampler_view
*sampler_view
;
1194 struct pipe_sampler_state sampler
= {0};
1195 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
1197 memset(&templ
, 0, sizeof(templ
));
1198 templ
.target
= PIPE_BUFFER
;
1199 templ
.format
= src_format
;
1200 templ
.u
.buf
.offset
= addr
->first_element
* addr
->bytes_per_pixel
;
1201 templ
.u
.buf
.size
= (addr
->last_element
- addr
->first_element
+ 1) *
1202 addr
->bytes_per_pixel
;
1203 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1204 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1205 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1206 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1208 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1209 if (sampler_view
== NULL
)
1212 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1214 pipe_sampler_view_reference(&sampler_view
, NULL
);
1216 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
1219 /* Framebuffer_state */
1221 struct pipe_framebuffer_state fb
;
1222 memset(&fb
, 0, sizeof(fb
));
1223 fb
.width
= surface
->width
;
1224 fb
.height
= surface
->height
;
1226 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1228 cso_set_framebuffer(cso
, &fb
);
1230 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1233 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1236 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1238 /* Depth/stencil/alpha state */
1240 struct pipe_depth_stencil_alpha_state dsa
;
1241 memset(&dsa
, 0, sizeof(dsa
));
1242 cso_set_depth_stencil_alpha(cso
, &dsa
);
1245 /* Set up the fragment shader */
1246 cso_set_fragment_shader_handle(cso
, fs
);
1248 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1251 cso_restore_state(cso
);
1252 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1258 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1259 struct gl_texture_image
*texImage
,
1260 GLenum format
, GLenum type
,
1261 enum pipe_format dst_format
,
1262 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1263 GLint width
, GLint height
, GLint depth
,
1265 const struct gl_pixelstore_attrib
*unpack
)
1267 struct st_context
*st
= st_context(ctx
);
1268 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1269 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1270 struct pipe_resource
*texture
= stImage
->pt
;
1271 struct pipe_context
*pipe
= st
->pipe
;
1272 struct pipe_screen
*screen
= pipe
->screen
;
1273 struct pipe_surface
*surface
= NULL
;
1274 struct st_pbo_addresses addr
;
1275 enum pipe_format src_format
;
1276 const struct util_format_description
*desc
;
1277 GLenum gl_target
= texImage
->TexObject
->Target
;
1280 if (!st
->pbo
.upload_enabled
)
1283 /* From now on, we need the gallium representation of dimensions. */
1284 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1291 if (depth
!= 1 && !st
->pbo
.layers
)
1294 /* Choose the source format. Initially, we do so without checking driver
1295 * support at all because of the remapping we later perform and because
1296 * at least the Radeon driver actually supports some formats for texture
1297 * buffers which it doesn't support for regular textures. */
1298 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1303 src_format
= util_format_linear(src_format
);
1304 desc
= util_format_description(src_format
);
1306 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1309 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1312 if (st
->pbo
.rgba_only
) {
1313 enum pipe_format orig_dst_format
= dst_format
;
1315 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1319 if (dst_format
!= orig_dst_format
&&
1320 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1321 PIPE_BIND_RENDER_TARGET
)) {
1327 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0,
1328 PIPE_BIND_SAMPLER_VIEW
)) {
1332 /* Compute buffer addresses */
1333 addr
.xoffset
= xoffset
;
1334 addr
.yoffset
= yoffset
;
1336 addr
.height
= height
;
1338 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1340 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1344 /* Set up the surface */
1346 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1347 unsigned max_layer
= util_max_layer(texture
, level
);
1349 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1351 struct pipe_surface templ
;
1352 memset(&templ
, 0, sizeof(templ
));
1353 templ
.format
= dst_format
;
1354 templ
.u
.tex
.level
= level
;
1355 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1356 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1358 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1363 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1365 pipe_surface_reference(&surface
, NULL
);
1371 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1372 struct gl_texture_image
*texImage
,
1373 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1374 GLint width
, GLint height
, GLint depth
,
1375 GLenum format
, GLenum type
, const void *pixels
,
1376 const struct gl_pixelstore_attrib
*unpack
)
1378 struct st_context
*st
= st_context(ctx
);
1379 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1380 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1381 struct pipe_context
*pipe
= st
->pipe
;
1382 struct pipe_screen
*screen
= pipe
->screen
;
1383 struct pipe_resource
*dst
= stImage
->pt
;
1384 struct pipe_resource
*src
= NULL
;
1385 struct pipe_resource src_templ
;
1386 struct pipe_transfer
*transfer
;
1387 struct pipe_blit_info blit
;
1388 enum pipe_format src_format
, dst_format
;
1389 mesa_format mesa_src_format
;
1390 GLenum gl_target
= texImage
->TexObject
->Target
;
1393 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1394 unsigned dst_level
= 0;
1396 st_flush_bitmap_cache(st
);
1397 st_invalidate_readpix_cache(st
);
1399 if (stObj
->pt
== stImage
->pt
)
1400 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1402 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1403 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1408 /* Try texture_subdata, which should be the fastest memcpy path. */
1410 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1411 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1412 texImage
->TexFormat
, format
, type
,
1414 struct pipe_box box
;
1415 unsigned stride
, layer_stride
;
1418 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1419 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1421 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1424 /* Convert to Gallium coordinates. */
1425 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1430 layer_stride
= stride
;
1433 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1434 pipe
->texture_subdata(pipe
, dst
, dst_level
, 0,
1435 &box
, data
, stride
, layer_stride
);
1439 if (!st
->prefer_blit_based_texture_transfer
) {
1443 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1444 * blit implementation in some drivers. */
1445 if (format
== GL_DEPTH_STENCIL
) {
1449 /* If the base internal format and the texture format don't match,
1450 * we can't use blit-based TexSubImage. */
1451 if (texImage
->_BaseFormat
!=
1452 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1457 /* See if the destination format is supported. */
1458 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1459 bind
= PIPE_BIND_DEPTH_STENCIL
;
1461 bind
= PIPE_BIND_RENDER_TARGET
;
1463 /* For luminance and intensity, only the red channel is stored
1464 * in the destination. */
1465 dst_format
= util_format_linear(dst
->format
);
1466 dst_format
= util_format_luminance_to_red(dst_format
);
1467 dst_format
= util_format_intensity_to_red(dst_format
);
1470 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1471 dst
->nr_samples
, bind
)) {
1475 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1476 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1477 xoffset
, yoffset
, zoffset
,
1478 width
, height
, depth
, pixels
, unpack
))
1482 /* See if the texture format already matches the format and type,
1483 * in which case the memcpy-based fast path will likely be used and
1484 * we don't have to blit. */
1485 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1486 type
, unpack
->SwapBytes
, NULL
)) {
1490 /* Choose the source format. */
1491 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1492 format
, type
, unpack
->SwapBytes
);
1497 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1499 /* There is no reason to do this if we cannot use memcpy for the temporary
1500 * source texture at least. This also takes transfer ops into account,
1502 if (!_mesa_texstore_can_use_memcpy(ctx
,
1503 _mesa_get_format_base_format(mesa_src_format
),
1504 mesa_src_format
, format
, type
, unpack
)) {
1508 /* TexSubImage only sets a single cubemap face. */
1509 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1510 gl_target
= GL_TEXTURE_2D
;
1512 /* TexSubImage can specify subsets of cube map array faces
1513 * so we need to upload via 2D array instead */
1514 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1515 gl_target
= GL_TEXTURE_2D_ARRAY
;
1518 /* Initialize the source texture description. */
1519 memset(&src_templ
, 0, sizeof(src_templ
));
1520 src_templ
.target
= gl_target_to_pipe(gl_target
);
1521 src_templ
.format
= src_format
;
1522 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1523 src_templ
.usage
= PIPE_USAGE_STAGING
;
1525 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1526 &src_templ
.width0
, &src_templ
.height0
,
1527 &src_templ
.depth0
, &src_templ
.array_size
);
1529 /* Check for NPOT texture support. */
1530 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1531 (!util_is_power_of_two_or_zero(src_templ
.width0
) ||
1532 !util_is_power_of_two_or_zero(src_templ
.height0
) ||
1533 !util_is_power_of_two_or_zero(src_templ
.depth0
))) {
1537 /* Create the source texture. */
1538 src
= screen
->resource_create(screen
, &src_templ
);
1543 /* Map source pixels. */
1544 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1545 format
, type
, pixels
, unpack
,
1548 /* This is a GL error. */
1549 pipe_resource_reference(&src
, NULL
);
1553 /* From now on, we need the gallium representation of dimensions. */
1554 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1561 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1562 width
, height
, depth
, &transfer
);
1564 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1565 pipe_resource_reference(&src
, NULL
);
1569 /* Upload pixels (just memcpy). */
1571 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1574 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1575 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1576 /* 1D array textures.
1577 * We need to convert gallium coords to GL coords.
1579 void *src
= _mesa_image_address2d(unpack
, pixels
,
1580 width
, depth
, format
,
1582 memcpy(map
, src
, bytesPerRow
);
1585 ubyte
*slice_map
= map
;
1587 for (row
= 0; row
< (unsigned) height
; row
++) {
1588 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1589 width
, height
, format
,
1590 type
, slice
, row
, 0);
1591 memcpy(slice_map
, src
, bytesPerRow
);
1592 slice_map
+= transfer
->stride
;
1595 map
+= transfer
->layer_stride
;
1599 pipe_transfer_unmap(pipe
, transfer
);
1600 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1603 memset(&blit
, 0, sizeof(blit
));
1604 blit
.src
.resource
= src
;
1606 blit
.src
.format
= src_format
;
1607 blit
.dst
.resource
= dst
;
1608 blit
.dst
.level
= dst_level
;
1609 blit
.dst
.format
= dst_format
;
1610 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1611 blit
.dst
.box
.x
= xoffset
;
1612 blit
.dst
.box
.y
= yoffset
;
1613 blit
.dst
.box
.z
= zoffset
+ dstz
;
1614 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1615 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1616 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1617 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1618 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1619 blit
.scissor_enable
= FALSE
;
1621 st
->pipe
->blit(st
->pipe
, &blit
);
1623 pipe_resource_reference(&src
, NULL
);
1627 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1628 width
, height
, depth
, format
, type
, pixels
,
1633 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1634 struct gl_texture_image
*texImage
,
1635 GLenum format
, GLenum type
, const void *pixels
,
1636 const struct gl_pixelstore_attrib
*unpack
)
1638 assert(dims
== 1 || dims
== 2 || dims
== 3);
1640 prep_teximage(ctx
, texImage
, format
, type
);
1642 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1645 /* allocate storage for texture data */
1646 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1647 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1651 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1652 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1653 format
, type
, pixels
, unpack
);
1658 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1659 struct gl_texture_image
*texImage
,
1660 GLint x
, GLint y
, GLint z
,
1661 GLsizei w
, GLsizei h
, GLsizei d
,
1662 GLenum format
, GLsizei imageSize
, const void *data
)
1664 struct st_context
*st
= st_context(ctx
);
1665 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1666 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1667 struct pipe_resource
*texture
= stImage
->pt
;
1668 struct pipe_context
*pipe
= st
->pipe
;
1669 struct pipe_screen
*screen
= pipe
->screen
;
1670 struct pipe_resource
*dst
= stImage
->pt
;
1671 struct pipe_surface
*surface
= NULL
;
1672 struct compressed_pixelstore store
;
1673 struct st_pbo_addresses addr
;
1674 enum pipe_format copy_format
;
1676 intptr_t buf_offset
;
1677 bool success
= false;
1679 /* Check basic pre-conditions for PBO upload */
1680 if (!st
->prefer_blit_based_texture_transfer
) {
1684 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1687 if (st_etc_fallback(st
, texImage
)) {
1688 /* ETC isn't supported and is represented by uncompressed formats. */
1696 if (!st
->pbo
.upload_enabled
||
1697 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1701 /* Choose the pipe format for the upload. */
1702 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1703 bw
= util_format_get_blockwidth(dst
->format
);
1704 bh
= util_format_get_blockheight(dst
->format
);
1706 switch (addr
.bytes_per_pixel
) {
1708 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1711 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1717 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0,
1718 PIPE_BIND_SAMPLER_VIEW
)) {
1722 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1723 dst
->nr_samples
, PIPE_BIND_RENDER_TARGET
)) {
1727 /* Interpret the pixelstore settings. */
1728 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1729 &ctx
->Unpack
, &store
);
1730 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1731 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1733 /* Compute the offset into the buffer */
1734 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1736 if (buf_offset
% addr
.bytes_per_pixel
) {
1740 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1742 addr
.xoffset
= x
/ bw
;
1743 addr
.yoffset
= y
/ bh
;
1744 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1745 addr
.height
= store
.CopyRowsPerSlice
;
1747 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1748 addr
.image_height
= store
.TotalRowsPerSlice
;
1750 if (!st_pbo_addresses_setup(st
, st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1754 /* Set up the surface. */
1756 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1757 unsigned max_layer
= util_max_layer(texture
, level
);
1759 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1761 struct pipe_surface templ
;
1762 memset(&templ
, 0, sizeof(templ
));
1763 templ
.format
= copy_format
;
1764 templ
.u
.tex
.level
= level
;
1765 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1766 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1768 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1773 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1775 pipe_surface_reference(&surface
, NULL
);
1781 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1783 format
, imageSize
, data
);
1787 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1788 struct gl_texture_image
*texImage
,
1789 GLsizei imageSize
, const void *data
)
1791 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1793 /* only 2D and 3D compressed images are supported at this time */
1795 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1799 /* This is pretty simple, because unlike the general texstore path we don't
1800 * have to worry about the usual image unpacking or image transfer
1804 assert(texImage
->Width
> 0);
1805 assert(texImage
->Height
> 0);
1806 assert(texImage
->Depth
> 0);
1808 /* allocate storage for texture data */
1809 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1810 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1814 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1816 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1817 texImage
->TexFormat
,
1825 * Called via ctx->Driver.GetTexSubImage()
1827 * This uses a blit to copy the texture to a texture format which matches
1828 * the format and type combo and then a fast read-back is done using memcpy.
1829 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1830 * a format which matches the swizzling.
1832 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1834 * NOTE: Drivers usually do a blit to convert between tiled and linear
1835 * texture layouts during texture uploads/downloads, so the blit
1836 * we do here should be free in such cases.
1839 st_GetTexSubImage(struct gl_context
* ctx
,
1840 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1841 GLsizei width
, GLsizei height
, GLint depth
,
1842 GLenum format
, GLenum type
, void * pixels
,
1843 struct gl_texture_image
*texImage
)
1845 struct st_context
*st
= st_context(ctx
);
1846 struct pipe_context
*pipe
= st
->pipe
;
1847 struct pipe_screen
*screen
= pipe
->screen
;
1848 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1849 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1850 struct pipe_resource
*src
= stObj
->pt
;
1851 struct pipe_resource
*dst
= NULL
;
1852 struct pipe_resource dst_templ
;
1853 enum pipe_format dst_format
, src_format
;
1854 mesa_format mesa_format
;
1855 GLenum gl_target
= texImage
->TexObject
->Target
;
1856 enum pipe_texture_target pipe_target
;
1858 struct pipe_blit_info blit
;
1860 struct pipe_transfer
*tex_xfer
;
1862 boolean done
= FALSE
;
1864 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1865 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1867 st_flush_bitmap_cache(st
);
1869 if (!st
->prefer_blit_based_texture_transfer
&&
1870 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1871 /* Try to avoid the fallback if we're doing texture decompression here */
1875 /* Handle non-finalized textures. */
1876 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1880 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1881 * due to an incomplete stencil blit implementation in some drivers. */
1882 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1886 /* If the base internal format and the texture format don't match, we have
1887 * to fall back to _mesa_GetTexImage_sw. */
1888 if (texImage
->_BaseFormat
!=
1889 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1893 /* See if the texture format already matches the format and type,
1894 * in which case the memcpy-based fast path will be used. */
1895 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1896 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1900 /* Convert the source format to what is expected by GetTexImage
1901 * and see if it's supported.
1903 * This only applies to glGetTexImage:
1904 * - Luminance must be returned as (L,0,0,1).
1905 * - Luminance alpha must be returned as (L,0,0,A).
1906 * - Intensity must be returned as (I,0,0,1)
1908 if (stObj
->surface_based
)
1909 src_format
= util_format_linear(stObj
->surface_format
);
1911 src_format
= util_format_linear(src
->format
);
1912 src_format
= util_format_luminance_to_red(src_format
);
1913 src_format
= util_format_intensity_to_red(src_format
);
1916 !screen
->is_format_supported(screen
, src_format
, src
->target
,
1918 PIPE_BIND_SAMPLER_VIEW
)) {
1922 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1923 bind
= PIPE_BIND_DEPTH_STENCIL
;
1925 bind
= PIPE_BIND_RENDER_TARGET
;
1927 /* GetTexImage only returns a single face for cubemaps. */
1928 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1929 gl_target
= GL_TEXTURE_2D
;
1931 pipe_target
= gl_target_to_pipe(gl_target
);
1933 /* Choose the destination format by finding the best match
1934 * for the format+type combo. */
1935 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1936 ctx
->Pack
.SwapBytes
);
1938 if (dst_format
== PIPE_FORMAT_NONE
) {
1939 GLenum dst_glformat
;
1941 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1942 * where decompression with a blit is always preferred. */
1943 if (!util_format_is_compressed(src
->format
)) {
1947 /* Set the appropriate format for the decompressed texture.
1948 * Luminance and sRGB formats shouldn't appear here.*/
1949 switch (src_format
) {
1950 case PIPE_FORMAT_DXT1_RGB
:
1951 case PIPE_FORMAT_DXT1_RGBA
:
1952 case PIPE_FORMAT_DXT3_RGBA
:
1953 case PIPE_FORMAT_DXT5_RGBA
:
1954 case PIPE_FORMAT_RGTC1_UNORM
:
1955 case PIPE_FORMAT_RGTC2_UNORM
:
1956 case PIPE_FORMAT_ETC1_RGB8
:
1957 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1958 dst_glformat
= GL_RGBA8
;
1960 case PIPE_FORMAT_RGTC1_SNORM
:
1961 case PIPE_FORMAT_RGTC2_SNORM
:
1962 if (!ctx
->Extensions
.EXT_texture_snorm
)
1964 dst_glformat
= GL_RGBA8_SNORM
;
1966 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1967 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1968 if (!ctx
->Extensions
.ARB_texture_float
)
1970 dst_glformat
= GL_RGBA32F
;
1977 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
1978 pipe_target
, 0, bind
, FALSE
);
1980 if (dst_format
== PIPE_FORMAT_NONE
) {
1981 /* unable to get an rgba format!?! */
1986 /* create the destination texture of size (width X height X depth) */
1987 memset(&dst_templ
, 0, sizeof(dst_templ
));
1988 dst_templ
.target
= pipe_target
;
1989 dst_templ
.format
= dst_format
;
1990 dst_templ
.bind
= bind
;
1991 dst_templ
.usage
= PIPE_USAGE_STAGING
;
1993 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1994 &dst_templ
.width0
, &dst_templ
.height0
,
1995 &dst_templ
.depth0
, &dst_templ
.array_size
);
1997 dst
= screen
->resource_create(screen
, &dst_templ
);
2002 /* From now on, we need the gallium representation of dimensions. */
2003 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2010 assert(texImage
->Face
== 0 ||
2011 texImage
->TexObject
->MinLayer
== 0 ||
2014 memset(&blit
, 0, sizeof(blit
));
2015 blit
.src
.resource
= src
;
2016 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2017 blit
.src
.format
= src_format
;
2018 blit
.dst
.resource
= dst
;
2020 blit
.dst
.format
= dst
->format
;
2021 blit
.src
.box
.x
= xoffset
;
2023 blit
.src
.box
.y
= yoffset
;
2025 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2027 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2028 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2029 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2030 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2031 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2032 blit
.scissor_enable
= FALSE
;
2034 /* blit/render/decompress */
2035 st
->pipe
->blit(st
->pipe
, &blit
);
2037 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2039 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2040 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2045 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2046 dims
= _mesa_get_texture_dimensions(gl_target
);
2048 /* copy/pack data into user buffer */
2049 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2050 ctx
->Pack
.SwapBytes
, NULL
)) {
2052 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2055 for (slice
= 0; slice
< depth
; slice
++) {
2056 ubyte
*slice_map
= map
;
2058 for (row
= 0; row
< height
; row
++) {
2059 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2060 width
, height
, format
, type
,
2063 memcpy(dest
, slice_map
, bytesPerRow
);
2065 slice_map
+= tex_xfer
->stride
;
2068 map
+= tex_xfer
->layer_stride
;
2072 /* format translation via floats */
2075 uint32_t dstMesaFormat
;
2076 int dstStride
, srcStride
;
2078 assert(util_format_is_compressed(src
->format
));
2080 rgba
= malloc(width
* height
* 4 * sizeof(GLfloat
));
2085 if (ST_DEBUG
& DEBUG_FALLBACK
)
2086 debug_printf("%s: fallback format translation\n", __func__
);
2088 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2089 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2090 srcStride
= 4 * width
* sizeof(GLfloat
);
2091 for (slice
= 0; slice
< depth
; slice
++) {
2092 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2093 width
, height
, format
, type
,
2096 /* get float[4] rgba row from surface */
2097 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, height
,
2100 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2101 rgba
, RGBA32_FLOAT
, srcStride
,
2102 width
, height
, NULL
);
2104 /* Handle byte swapping if required */
2105 if (ctx
->Pack
.SwapBytes
) {
2106 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
2107 width
, height
, dest
, dest
);
2110 map
+= tex_xfer
->layer_stride
;
2119 pipe_transfer_unmap(pipe
, tex_xfer
);
2121 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2122 pipe_resource_reference(&dst
, NULL
);
2126 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2127 width
, height
, depth
,
2128 format
, type
, pixels
, texImage
);
2134 * Do a CopyTexSubImage operation using a read transfer from the source,
2135 * a write transfer to the destination and get_tile()/put_tile() to access
2136 * the pixels/texels.
2138 * Note: srcY=0=TOP of renderbuffer
2141 fallback_copy_texsubimage(struct gl_context
*ctx
,
2142 struct st_renderbuffer
*strb
,
2143 struct st_texture_image
*stImage
,
2145 GLint destX
, GLint destY
, GLint slice
,
2146 GLint srcX
, GLint srcY
,
2147 GLsizei width
, GLsizei height
)
2149 struct st_context
*st
= st_context(ctx
);
2150 struct pipe_context
*pipe
= st
->pipe
;
2151 struct pipe_transfer
*src_trans
;
2153 enum pipe_transfer_usage transfer_usage
;
2155 unsigned dst_width
= width
;
2156 unsigned dst_height
= height
;
2157 unsigned dst_depth
= 1;
2158 struct pipe_transfer
*transfer
;
2160 if (ST_DEBUG
& DEBUG_FALLBACK
)
2161 debug_printf("%s: fallback processing\n", __func__
);
2163 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2164 srcY
= strb
->Base
.Height
- srcY
- height
;
2167 map
= pipe_transfer_map(pipe
,
2169 strb
->surface
->u
.tex
.level
,
2170 strb
->surface
->u
.tex
.first_layer
,
2173 width
, height
, &src_trans
);
2175 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2176 baseFormat
== GL_DEPTH_STENCIL
) &&
2177 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2178 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2180 transfer_usage
= PIPE_TRANSFER_WRITE
;
2182 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2183 destX
, destY
, slice
,
2184 dst_width
, dst_height
, dst_depth
,
2187 if (baseFormat
== GL_DEPTH_COMPONENT
||
2188 baseFormat
== GL_DEPTH_STENCIL
) {
2189 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2190 ctx
->Pixel
.DepthBias
!= 0.0F
);
2194 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2195 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2204 data
= malloc(width
* sizeof(uint
));
2207 /* To avoid a large temp memory allocation, do copy row by row */
2208 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2209 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2211 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2214 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2215 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2216 0, 0, width
, 1, data
);
2219 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2224 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2232 malloc(width
* height
* 4 * sizeof(GLfloat
));
2234 if (tempSrc
&& texDest
) {
2235 const GLint dims
= 2;
2237 struct gl_texture_image
*texImage
= &stImage
->base
;
2238 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2240 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2241 unpack
.Invert
= GL_TRUE
;
2244 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2245 dstRowStride
= transfer
->layer_stride
;
2248 dstRowStride
= transfer
->stride
;
2251 /* get float/RGBA image from framebuffer */
2252 /* XXX this usually involves a lot of int/float conversion.
2253 * try to avoid that someday.
2255 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2256 util_format_linear(strb
->texture
->format
),
2259 /* Store into texture memory.
2260 * Note that this does some special things such as pixel transfer
2261 * ops and format conversion. In particular, if the dest tex format
2262 * is actually RGBA but the user created the texture as GL_RGB we
2263 * need to fill-in/override the alpha channel with 1.0.
2265 _mesa_texstore(ctx
, dims
,
2266 texImage
->_BaseFormat
,
2267 texImage
->TexFormat
,
2271 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2275 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2281 st_texture_image_unmap(st
, stImage
, slice
);
2282 pipe
->transfer_unmap(pipe
, src_trans
);
2286 st_can_copyteximage_using_blit(const struct gl_texture_image
*texImage
,
2287 const struct gl_renderbuffer
*rb
)
2289 GLenum tex_baseformat
= _mesa_get_format_base_format(texImage
->TexFormat
);
2291 /* We don't blit to a teximage where the GL base format doesn't match the
2292 * texture's chosen format, except in the case of a GL_RGB texture
2293 * represented with GL_RGBA (where the alpha channel is just being
2296 if (texImage
->_BaseFormat
!= tex_baseformat
&&
2297 ((texImage
->_BaseFormat
!= GL_RGB
|| tex_baseformat
!= GL_RGBA
))) {
2301 /* We can't blit from a RB where the GL base format doesn't match the RB's
2302 * chosen format (for example, GL RGB or ALPHA with rb->Format of an RGBA
2303 * type, because the other channels will be undefined).
2305 if (rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
))
2312 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2313 * Note that the region to copy has already been clipped so we know we
2314 * won't read from outside the source renderbuffer's bounds.
2316 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2319 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2320 struct gl_texture_image
*texImage
,
2321 GLint destX
, GLint destY
, GLint slice
,
2322 struct gl_renderbuffer
*rb
,
2323 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2325 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2326 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2327 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2328 struct st_context
*st
= st_context(ctx
);
2329 struct pipe_context
*pipe
= st
->pipe
;
2330 struct pipe_screen
*screen
= pipe
->screen
;
2331 struct pipe_blit_info blit
;
2332 enum pipe_format dst_format
;
2333 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2337 st_flush_bitmap_cache(st
);
2338 st_invalidate_readpix_cache(st
);
2340 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2341 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2343 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2344 debug_printf("%s: null strb or stImage\n", __func__
);
2348 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2349 texImage
->TexFormat
)) {
2353 if (!st_can_copyteximage_using_blit(texImage
, rb
)) {
2357 /* Choose the destination format to match the TexImage behavior. */
2358 dst_format
= util_format_linear(stImage
->pt
->format
);
2359 dst_format
= util_format_luminance_to_red(dst_format
);
2360 dst_format
= util_format_intensity_to_red(dst_format
);
2362 /* See if the destination format is supported. */
2363 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2364 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2365 bind
= PIPE_BIND_DEPTH_STENCIL
;
2368 bind
= PIPE_BIND_RENDER_TARGET
;
2372 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2373 stImage
->pt
->nr_samples
, bind
)) {
2377 /* Y flipping for the main framebuffer. */
2379 srcY1
= strb
->Base
.Height
- srcY
- height
;
2380 srcY0
= srcY1
+ height
;
2384 srcY1
= srcY0
+ height
;
2387 /* Blit the texture.
2388 * This supports flipping, format conversions, and downsampling.
2390 memset(&blit
, 0, sizeof(blit
));
2391 blit
.src
.resource
= strb
->texture
;
2392 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2393 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2394 blit
.src
.box
.x
= srcX
;
2395 blit
.src
.box
.y
= srcY0
;
2396 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2397 blit
.src
.box
.width
= width
;
2398 blit
.src
.box
.height
= srcY1
- srcY0
;
2399 blit
.src
.box
.depth
= 1;
2400 blit
.dst
.resource
= stImage
->pt
;
2401 blit
.dst
.format
= dst_format
;
2402 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2403 blit
.dst
.box
.x
= destX
;
2404 blit
.dst
.box
.y
= destY
;
2405 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2406 blit
.dst
.box
.width
= width
;
2407 blit
.dst
.box
.height
= height
;
2408 blit
.dst
.box
.depth
= 1;
2409 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2410 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2411 pipe
->blit(pipe
, &blit
);
2415 /* software fallback */
2416 fallback_copy_texsubimage(ctx
,
2417 strb
, stImage
, texImage
->_BaseFormat
,
2418 destX
, destY
, slice
,
2419 srcX
, srcY
, width
, height
);
2424 * Copy image data from stImage into the texture object 'stObj' at level
2428 copy_image_data_to_texture(struct st_context
*st
,
2429 struct st_texture_object
*stObj
,
2431 struct st_texture_image
*stImage
)
2435 const struct gl_texture_image MAYBE_UNUSED
*dstImage
=
2436 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2438 assert(dstImage
->Width
== stImage
->base
.Width
);
2439 assert(dstImage
->Height
== stImage
->base
.Height
);
2440 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2444 /* Copy potentially with the blitter:
2447 if (stImage
->pt
->last_level
== 0)
2450 src_level
= stImage
->base
.Level
;
2452 assert(src_level
<= stImage
->pt
->last_level
);
2453 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2454 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2455 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2456 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2457 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2458 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2460 st_texture_image_copy(st
->pipe
,
2461 stObj
->pt
, dstLevel
, /* dest texture, level */
2462 stImage
->pt
, src_level
, /* src texture, level */
2463 stImage
->base
.Face
);
2465 pipe_resource_reference(&stImage
->pt
, NULL
);
2467 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2472 * Called during state validation. When this function is finished,
2473 * the texture object should be ready for rendering.
2474 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2477 st_finalize_texture(struct gl_context
*ctx
,
2478 struct pipe_context
*pipe
,
2479 struct gl_texture_object
*tObj
,
2482 struct st_context
*st
= st_context(ctx
);
2483 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2484 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2486 const struct st_texture_image
*firstImage
;
2487 enum pipe_format firstImageFormat
;
2489 uint16_t ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2491 if (tObj
->Immutable
)
2494 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
2495 /* The texture is complete and we know exactly how many mipmap levels
2496 * are present/needed. This is conditional because we may be called
2497 * from the st_generate_mipmap() function when the texture object is
2498 * incomplete. In that case, we'll have set stObj->lastLevel before
2501 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
2502 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
2503 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2505 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2508 /* Skip the loop over images in the common case of no images having
2509 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
2510 * haven't looked at, then we do need to look at those new images.
2512 if (!stObj
->needs_validation
&&
2513 stObj
->base
.BaseLevel
>= stObj
->validated_first_level
&&
2514 stObj
->lastLevel
<= stObj
->validated_last_level
) {
2518 /* If this texture comes from a window system, there is nothing else to do. */
2519 if (stObj
->surface_based
) {
2523 firstImage
= st_texture_image_const(stObj
->base
.Image
[cubeMapFace
][stObj
->base
.BaseLevel
]);
2526 /* If both firstImage and stObj point to a texture which can contain
2527 * all active images, favour firstImage. Note that because of the
2528 * completeness requirement, we know that the image dimensions
2531 if (firstImage
->pt
&&
2532 firstImage
->pt
!= stObj
->pt
&&
2533 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2534 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2535 st_texture_release_all_sampler_views(st
, stObj
);
2538 /* Find gallium format for the Mesa texture */
2540 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2542 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2545 uint16_t height
, depth
;
2547 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2548 firstImage
->base
.Width2
,
2549 firstImage
->base
.Height2
,
2550 firstImage
->base
.Depth2
,
2551 &width
, &height
, &depth
, &ptLayers
);
2553 /* If we previously allocated a pipe texture and its sizes are
2554 * compatible, use them.
2557 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2558 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2559 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2560 ptWidth
= stObj
->pt
->width0
;
2561 ptHeight
= stObj
->pt
->height0
;
2562 ptDepth
= stObj
->pt
->depth0
;
2564 /* Otherwise, compute a new level=0 size that is compatible with the
2567 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2568 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2569 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2571 /* If the base level image is 1x1x1, we still need to ensure that the
2572 * resulting pipe texture ends up with the required number of levels
2575 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2576 ptWidth
<<= firstImage
->base
.Level
;
2578 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2579 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2583 /* At this point, the texture may be incomplete (mismatched cube
2584 * face sizes, for example). If that's the case, give up, but
2585 * don't return GL_FALSE as that would raise an incorrect
2586 * GL_OUT_OF_MEMORY error. See Piglit fbo-incomplete-texture-03 test.
2588 if (!stObj
->base
._BaseComplete
) {
2589 _mesa_test_texobj_completeness(ctx
, &stObj
->base
);
2590 if (!stObj
->base
._BaseComplete
) {
2596 ptNumSamples
= firstImage
->base
.NumSamples
;
2599 /* If we already have a gallium texture, check that it matches the texture
2600 * object's format, target, size, num_levels, etc.
2603 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2604 stObj
->pt
->format
!= firstImageFormat
||
2605 stObj
->pt
->last_level
< stObj
->lastLevel
||
2606 stObj
->pt
->width0
!= ptWidth
||
2607 stObj
->pt
->height0
!= ptHeight
||
2608 stObj
->pt
->depth0
!= ptDepth
||
2609 stObj
->pt
->nr_samples
!= ptNumSamples
||
2610 stObj
->pt
->array_size
!= ptLayers
)
2612 /* The gallium texture does not match the Mesa texture so delete the
2613 * gallium texture now. We'll make a new one below.
2615 pipe_resource_reference(&stObj
->pt
, NULL
);
2616 st_texture_release_all_sampler_views(st
, stObj
);
2617 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
2621 /* May need to create a new gallium texture:
2624 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2626 stObj
->pt
= st_texture_create(st
,
2627 gl_target_to_pipe(stObj
->base
.Target
),
2633 ptLayers
, ptNumSamples
,
2637 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2642 /* Pull in any images not in the object's texture:
2644 for (face
= 0; face
< nr_faces
; face
++) {
2646 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2647 struct st_texture_image
*stImage
=
2648 st_texture_image(stObj
->base
.Image
[face
][level
]);
2650 /* Need to import images in main memory or held in other textures.
2652 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2656 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2657 height
= u_minify(ptHeight
, level
);
2661 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2662 depth
= u_minify(ptDepth
, level
);
2663 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2669 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2670 stImage
->base
.Height
== height
&&
2671 stImage
->base
.Depth
== depth
)) {
2672 /* src image fits expected dest mipmap level size */
2673 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2679 stObj
->validated_first_level
= stObj
->base
.BaseLevel
;
2680 stObj
->validated_last_level
= stObj
->lastLevel
;
2681 stObj
->needs_validation
= false;
2687 * Allocate a new pipe_resource object
2688 * width0, height0, depth0 are the dimensions of the level 0 image
2689 * (the highest resolution). last_level indicates how many mipmap levels
2690 * to allocate storage for. For non-mipmapped textures, this will be zero.
2692 static struct pipe_resource
*
2693 st_texture_create_from_memory(struct st_context
*st
,
2694 struct st_memory_object
*memObj
,
2696 enum pipe_texture_target target
,
2697 enum pipe_format format
,
2706 struct pipe_resource pt
, *newtex
;
2707 struct pipe_screen
*screen
= st
->pipe
->screen
;
2709 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
2711 assert(height0
> 0);
2713 if (target
== PIPE_TEXTURE_CUBE
)
2714 assert(layers
== 6);
2716 DBG("%s target %d format %s last_level %d\n", __func__
,
2717 (int) target
, util_format_name(format
), last_level
);
2720 assert(screen
->is_format_supported(screen
, format
, target
, 0,
2721 PIPE_BIND_SAMPLER_VIEW
));
2723 memset(&pt
, 0, sizeof(pt
));
2726 pt
.last_level
= last_level
;
2728 pt
.height0
= height0
;
2730 pt
.array_size
= layers
;
2731 pt
.usage
= PIPE_USAGE_DEFAULT
;
2733 /* only set this for OpenGL textures, not renderbuffers */
2734 pt
.flags
= PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
;
2735 pt
.nr_samples
= nr_samples
;
2737 newtex
= screen
->resource_from_memobj(screen
, &pt
, memObj
->memory
, offset
);
2739 assert(!newtex
|| pipe_is_referenced(&newtex
->reference
));
2745 * Allocate texture memory for a whole mipmap stack.
2746 * Note: for multisample textures if the requested sample count is not
2747 * supported, we search for the next higher supported sample count.
2750 st_texture_storage(struct gl_context
*ctx
,
2751 struct gl_texture_object
*texObj
,
2752 GLsizei levels
, GLsizei width
,
2753 GLsizei height
, GLsizei depth
,
2754 struct gl_memory_object
*memObj
,
2757 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2758 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2759 struct st_context
*st
= st_context(ctx
);
2760 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2761 struct st_memory_object
*smObj
= st_memory_object(memObj
);
2762 struct pipe_screen
*screen
= st
->pipe
->screen
;
2763 unsigned ptWidth
, bindings
;
2764 uint16_t ptHeight
, ptDepth
, ptLayers
;
2765 enum pipe_format fmt
;
2767 GLuint num_samples
= texImage
->NumSamples
;
2771 stObj
->lastLevel
= levels
- 1;
2773 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2775 bindings
= default_bindings(st
, fmt
);
2777 if (num_samples
> 0) {
2778 /* Find msaa sample count which is actually supported. For example,
2779 * if the user requests 1x but only 4x or 8x msaa is supported, we'll
2782 enum pipe_texture_target ptarget
= gl_target_to_pipe(texObj
->Target
);
2783 boolean found
= FALSE
;
2785 if (ctx
->Const
.MaxSamples
> 1 && num_samples
== 1) {
2786 /* don't try num_samples = 1 with drivers that support real msaa */
2790 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2791 if (screen
->is_format_supported(screen
, fmt
, ptarget
,
2793 PIPE_BIND_SAMPLER_VIEW
)) {
2794 /* Update the sample count in gl_texture_image as well. */
2795 texImage
->NumSamples
= num_samples
;
2806 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2807 width
, height
, depth
,
2808 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2811 stObj
->pt
= st_texture_create_from_memory(st
,
2814 gl_target_to_pipe(texObj
->Target
),
2820 ptLayers
, num_samples
,
2824 stObj
->pt
= st_texture_create(st
,
2825 gl_target_to_pipe(texObj
->Target
),
2831 ptLayers
, num_samples
,
2838 /* Set image resource pointers */
2839 for (level
= 0; level
< levels
; level
++) {
2841 for (face
= 0; face
< numFaces
; face
++) {
2842 struct st_texture_image
*stImage
=
2843 st_texture_image(texObj
->Image
[face
][level
]);
2844 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2846 etc_fallback_allocate(st
, stImage
);
2850 /* The texture is in a validated state, so no need to check later. */
2851 stObj
->needs_validation
= false;
2852 stObj
->validated_first_level
= 0;
2853 stObj
->validated_last_level
= levels
- 1;
2859 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2860 * for a whole mipmap stack.
2863 st_AllocTextureStorage(struct gl_context
*ctx
,
2864 struct gl_texture_object
*texObj
,
2865 GLsizei levels
, GLsizei width
,
2866 GLsizei height
, GLsizei depth
)
2868 return st_texture_storage(ctx
, texObj
, levels
,
2869 width
, height
, depth
,
2875 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2876 GLuint numLevels
, GLint level
,
2877 mesa_format format
, GLuint numSamples
,
2878 GLint width
, GLint height
, GLint depth
)
2880 struct st_context
*st
= st_context(ctx
);
2881 struct pipe_context
*pipe
= st
->pipe
;
2883 if (width
== 0 || height
== 0 || depth
== 0) {
2884 /* zero-sized images are legal, and always fit! */
2888 if (pipe
->screen
->can_create_resource
) {
2889 /* Ask the gallium driver if the texture is too large */
2890 struct gl_texture_object
*texObj
=
2891 _mesa_get_current_tex_object(ctx
, target
);
2892 struct pipe_resource pt
;
2894 /* Setup the pipe_resource object
2896 memset(&pt
, 0, sizeof(pt
));
2898 pt
.target
= gl_target_to_pipe(target
);
2899 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
2900 pt
.nr_samples
= numSamples
;
2902 st_gl_texture_dims_to_pipe_dims(target
,
2903 width
, height
, depth
,
2904 &pt
.width0
, &pt
.height0
,
2905 &pt
.depth0
, &pt
.array_size
);
2907 if (numLevels
> 0) {
2908 /* For immutable textures we know the final number of mip levels */
2909 pt
.last_level
= numLevels
- 1;
2911 else if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
2912 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
2913 /* assume just one mipmap level */
2917 /* assume a full set of mipmaps */
2918 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
2921 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
2924 /* Use core Mesa fallback */
2925 return _mesa_test_proxy_teximage(ctx
, target
, numLevels
, level
, format
,
2926 numSamples
, width
, height
, depth
);
2931 st_TextureView(struct gl_context
*ctx
,
2932 struct gl_texture_object
*texObj
,
2933 struct gl_texture_object
*origTexObj
)
2935 struct st_context
*st
= st_context(ctx
);
2936 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
2937 struct st_texture_object
*tex
= st_texture_object(texObj
);
2938 struct gl_texture_image
*image
= texObj
->Image
[0][0];
2940 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2941 const int numLevels
= texObj
->NumLevels
;
2946 pipe_resource_reference(&tex
->pt
, orig
->pt
);
2948 /* Set image resource pointers */
2949 for (level
= 0; level
< numLevels
; level
++) {
2950 for (face
= 0; face
< numFaces
; face
++) {
2951 struct st_texture_image
*stImage
=
2952 st_texture_image(texObj
->Image
[face
][level
]);
2953 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
2957 tex
->surface_based
= GL_TRUE
;
2958 tex
->surface_format
=
2959 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
2961 tex
->lastLevel
= numLevels
- 1;
2963 /* free texture sampler views. They need to be recreated when we
2964 * change the texture view parameters.
2966 st_texture_release_all_sampler_views(st
, tex
);
2968 /* The texture is in a validated state, so no need to check later. */
2969 tex
->needs_validation
= false;
2970 tex
->validated_first_level
= 0;
2971 tex
->validated_last_level
= numLevels
- 1;
2978 * Find the mipmap level in 'pt' which matches the level described by
2982 find_mipmap_level(const struct gl_texture_image
*texImage
,
2983 const struct pipe_resource
*pt
)
2985 const GLenum target
= texImage
->TexObject
->Target
;
2986 GLint texWidth
= texImage
->Width
;
2987 GLint texHeight
= texImage
->Height
;
2988 GLint texDepth
= texImage
->Depth
;
2990 uint16_t h
, d
, layers
;
2992 st_gl_texture_dims_to_pipe_dims(target
, texWidth
, texHeight
, texDepth
,
2993 &w
, &h
, &d
, &layers
);
2995 for (level
= 0; level
<= pt
->last_level
; level
++) {
2996 if (u_minify(pt
->width0
, level
) == w
&&
2997 u_minify(pt
->height0
, level
) == h
&&
2998 u_minify(pt
->depth0
, level
) == d
) {
3003 /* If we get here, there must be some sort of inconsistency between
3004 * the Mesa texture object/images and the gallium resource.
3006 debug_printf("Inconsistent textures in find_mipmap_level()\n");
3008 return texImage
->Level
;
3013 st_ClearTexSubImage(struct gl_context
*ctx
,
3014 struct gl_texture_image
*texImage
,
3015 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3016 GLsizei width
, GLsizei height
, GLsizei depth
,
3017 const void *clearValue
)
3019 static const char zeros
[16] = {0};
3020 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3021 struct st_texture_image
*stImage
= st_texture_image(texImage
);
3022 struct pipe_resource
*pt
= stImage
->pt
;
3023 struct st_context
*st
= st_context(ctx
);
3024 struct pipe_context
*pipe
= st
->pipe
;
3026 struct pipe_box box
;
3031 st_flush_bitmap_cache(st
);
3032 st_invalidate_readpix_cache(st
);
3034 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
3035 width
, height
, depth
, &box
);
3036 if (texObj
->Immutable
) {
3037 /* The texture object has to be consistent (no "loose", per-image
3038 * gallium resources). If this texture is a view into another
3039 * texture, we have to apply the MinLevel/Layer offsets. If this is
3040 * not a texture view, the offsets will be zero.
3042 assert(stImage
->pt
== st_texture_object(texObj
)->pt
);
3043 level
= texImage
->Level
+ texObj
->MinLevel
;
3044 box
.z
+= texObj
->MinLayer
;
3047 /* Texture level sizes may be inconsistent. We my have "loose",
3048 * per-image gallium resources. The texImage->Level may not match
3049 * the gallium resource texture level.
3051 level
= find_mipmap_level(texImage
, pt
);
3054 assert(level
<= pt
->last_level
);
3056 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
3061 * Called via the glTexParam*() function, but only when some texture object
3062 * state has actually changed.
3065 st_TexParameter(struct gl_context
*ctx
,
3066 struct gl_texture_object
*texObj
, GLenum pname
)
3068 struct st_context
*st
= st_context(ctx
);
3069 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3072 case GL_TEXTURE_BASE_LEVEL
:
3073 case GL_TEXTURE_MAX_LEVEL
:
3074 case GL_DEPTH_TEXTURE_MODE
:
3075 case GL_DEPTH_STENCIL_TEXTURE_MODE
:
3076 case GL_TEXTURE_SRGB_DECODE_EXT
:
3077 case GL_TEXTURE_SWIZZLE_R
:
3078 case GL_TEXTURE_SWIZZLE_G
:
3079 case GL_TEXTURE_SWIZZLE_B
:
3080 case GL_TEXTURE_SWIZZLE_A
:
3081 case GL_TEXTURE_SWIZZLE_RGBA
:
3082 case GL_TEXTURE_BUFFER_SIZE
:
3083 case GL_TEXTURE_BUFFER_OFFSET
:
3084 /* changing any of these texture parameters means we must create
3085 * new sampler views.
3087 st_texture_release_all_sampler_views(st
, stObj
);
3095 st_SetTextureStorageForMemoryObject(struct gl_context
*ctx
,
3096 struct gl_texture_object
*texObj
,
3097 struct gl_memory_object
*memObj
,
3098 GLsizei levels
, GLsizei width
,
3099 GLsizei height
, GLsizei depth
,
3102 return st_texture_storage(ctx
, texObj
, levels
,
3103 width
, height
, depth
,
3108 st_NewTextureHandle(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
3109 struct gl_sampler_object
*sampObj
)
3111 struct st_context
*st
= st_context(ctx
);
3112 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3113 struct pipe_context
*pipe
= st
->pipe
;
3114 struct pipe_sampler_view
*view
;
3115 struct pipe_sampler_state sampler
= {0};
3117 if (texObj
->Target
!= GL_TEXTURE_BUFFER
) {
3118 if (!st_finalize_texture(ctx
, pipe
, texObj
, 0))
3121 st_convert_sampler(st
, texObj
, sampObj
, 0, &sampler
);
3123 /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
3124 view
= st_get_texture_sampler_view_from_stobj(st
, stObj
, sampObj
, 0, true);
3126 view
= st_get_buffer_sampler_view_from_stobj(st
, stObj
);
3129 return pipe
->create_texture_handle(pipe
, view
, &sampler
);
3134 st_DeleteTextureHandle(struct gl_context
*ctx
, GLuint64 handle
)
3136 struct st_context
*st
= st_context(ctx
);
3137 struct pipe_context
*pipe
= st
->pipe
;
3139 pipe
->delete_texture_handle(pipe
, handle
);
3144 st_MakeTextureHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3147 struct st_context
*st
= st_context(ctx
);
3148 struct pipe_context
*pipe
= st
->pipe
;
3150 pipe
->make_texture_handle_resident(pipe
, handle
, resident
);
3155 st_NewImageHandle(struct gl_context
*ctx
, struct gl_image_unit
*imgObj
)
3157 struct st_context
*st
= st_context(ctx
);
3158 struct pipe_context
*pipe
= st
->pipe
;
3159 struct pipe_image_view image
;
3161 st_convert_image(st
, imgObj
, &image
);
3163 return pipe
->create_image_handle(pipe
, &image
);
3168 st_DeleteImageHandle(struct gl_context
*ctx
, GLuint64 handle
)
3170 struct st_context
*st
= st_context(ctx
);
3171 struct pipe_context
*pipe
= st
->pipe
;
3173 pipe
->delete_image_handle(pipe
, handle
);
3178 st_MakeImageHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3179 GLenum access
, bool resident
)
3181 struct st_context
*st
= st_context(ctx
);
3182 struct pipe_context
*pipe
= st
->pipe
;
3184 pipe
->make_image_handle_resident(pipe
, handle
, access
, resident
);
3189 st_init_texture_functions(struct dd_function_table
*functions
)
3191 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
3192 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
3193 functions
->TexImage
= st_TexImage
;
3194 functions
->TexSubImage
= st_TexSubImage
;
3195 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
3196 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
3197 functions
->GenerateMipmap
= st_generate_mipmap
;
3199 functions
->GetTexSubImage
= st_GetTexSubImage
;
3201 /* compressed texture functions */
3202 functions
->CompressedTexImage
= st_CompressedTexImage
;
3204 functions
->NewTextureObject
= st_NewTextureObject
;
3205 functions
->NewTextureImage
= st_NewTextureImage
;
3206 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
3207 functions
->DeleteTexture
= st_DeleteTextureObject
;
3208 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
3209 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
3210 functions
->MapTextureImage
= st_MapTextureImage
;
3211 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
3213 /* XXX Temporary until we can query pipe's texture sizes */
3214 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
3216 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
3217 functions
->TextureView
= st_TextureView
;
3218 functions
->ClearTexSubImage
= st_ClearTexSubImage
;
3220 functions
->TexParameter
= st_TexParameter
;
3222 /* bindless functions */
3223 functions
->NewTextureHandle
= st_NewTextureHandle
;
3224 functions
->DeleteTextureHandle
= st_DeleteTextureHandle
;
3225 functions
->MakeTextureHandleResident
= st_MakeTextureHandleResident
;
3226 functions
->NewImageHandle
= st_NewImageHandle
;
3227 functions
->DeleteImageHandle
= st_DeleteImageHandle
;
3228 functions
->MakeImageHandleResident
= st_MakeImageHandleResident
;
3230 /* external object functions */
3231 functions
->SetTextureStorageForMemoryObject
= st_SetTextureStorageForMemoryObject
;