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
->compressed_data
) {
211 free(stImage
->compressed_data
);
212 stImage
->compressed_data
= NULL
;
215 /* if the texture image is being deallocated, the structure of the
216 * texture is changing so we'll likely need a new sampler view.
218 st_texture_release_all_sampler_views(st
, stObj
);
222 st_compressed_format_fallback(struct st_context
*st
, mesa_format format
)
224 if (format
== MESA_FORMAT_ETC1_RGB8
)
225 return !st
->has_etc1
;
227 if (_mesa_is_format_etc2(format
))
228 return !st
->has_etc2
;
234 compressed_tex_fallback_allocate(struct st_context
*st
,
235 struct st_texture_image
*stImage
)
237 struct gl_texture_image
*texImage
= &stImage
->base
;
239 if (!st_compressed_format_fallback(st
, texImage
->TexFormat
))
242 if (stImage
->compressed_data
)
243 free(stImage
->compressed_data
);
245 unsigned data_size
= _mesa_format_image_size(texImage
->TexFormat
,
250 stImage
->compressed_data
=
251 malloc(data_size
* _mesa_num_tex_faces(texImage
->TexObject
->Target
));
254 /** called via ctx->Driver.MapTextureImage() */
256 st_MapTextureImage(struct gl_context
*ctx
,
257 struct gl_texture_image
*texImage
,
258 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
260 GLubyte
**mapOut
, GLint
*rowStrideOut
)
262 struct st_context
*st
= st_context(ctx
);
263 struct st_texture_image
*stImage
= st_texture_image(texImage
);
265 struct pipe_transfer
*transfer
;
267 /* Check for unexpected flags */
268 assert((mode
& ~(GL_MAP_READ_BIT
|
270 GL_MAP_INVALIDATE_RANGE_BIT
)) == 0);
272 const enum pipe_transfer_usage transfer_flags
=
273 st_access_flags_to_transfer_flags(mode
, false);
275 map
= st_texture_image_map(st
, stImage
, transfer_flags
, x
, y
, slice
, w
, h
, 1,
278 if (st_compressed_format_fallback(st
, texImage
->TexFormat
)) {
279 /* Some compressed formats don't have to be supported by drivers,
280 * and st/mesa transparently handles decompression on upload (Unmap),
281 * so that drivers don't see the compressed formats.
283 * We store the compressed data (it's needed for glGetCompressedTex-
284 * Image and image copies in OES_copy_image).
286 unsigned z
= transfer
->box
.z
;
287 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
289 unsigned blk_w
, blk_h
;
290 _mesa_get_format_block_size(texImage
->TexFormat
, &blk_w
, &blk_h
);
292 unsigned y_blocks
= DIV_ROUND_UP(texImage
->Height2
, blk_h
);
293 unsigned stride
= *rowStrideOut
= itransfer
->temp_stride
=
294 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width2
);
295 unsigned block_size
= _mesa_get_format_bytes(texImage
->TexFormat
);
297 *mapOut
= itransfer
->temp_data
=
298 stImage
->compressed_data
+
299 (z
* y_blocks
+ (y
/ blk_h
)) * stride
+
300 (x
/ blk_w
) * block_size
;
301 itransfer
->map
= map
;
304 /* supported mapping */
306 *rowStrideOut
= transfer
->stride
;
316 /** called via ctx->Driver.UnmapTextureImage() */
318 st_UnmapTextureImage(struct gl_context
*ctx
,
319 struct gl_texture_image
*texImage
,
322 struct st_context
*st
= st_context(ctx
);
323 struct st_texture_image
*stImage
= st_texture_image(texImage
);
325 if (st_compressed_format_fallback(st
, texImage
->TexFormat
)) {
326 /* Decompress the compressed image on upload if the driver doesn't
327 * support the compressed format. */
328 unsigned z
= slice
+ stImage
->base
.Face
;
329 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
330 struct pipe_transfer
*transfer
= itransfer
->transfer
;
332 assert(z
== transfer
->box
.z
);
334 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
335 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
336 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
337 itransfer
->temp_data
,
338 itransfer
->temp_stride
,
339 transfer
->box
.width
, transfer
->box
.height
);
342 bool bgra
= stImage
->pt
->format
== PIPE_FORMAT_B8G8R8A8_SRGB
;
343 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
344 itransfer
->temp_data
, itransfer
->temp_stride
,
345 transfer
->box
.width
, transfer
->box
.height
,
351 itransfer
->temp_data
= NULL
;
352 itransfer
->temp_stride
= 0;
356 st_texture_image_unmap(st
, stImage
, slice
);
361 * Return default texture resource binding bitmask for the given format.
364 default_bindings(struct st_context
*st
, enum pipe_format format
)
366 struct pipe_screen
*screen
= st
->pipe
->screen
;
367 const unsigned target
= PIPE_TEXTURE_2D
;
370 if (util_format_is_depth_or_stencil(format
))
371 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
373 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
375 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
379 format
= util_format_linear(format
);
381 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
384 return PIPE_BIND_SAMPLER_VIEW
;
390 * Given the size of a mipmap image, try to compute the size of the level=0
393 * Note that this isn't always accurate for odd-sized, non-POW textures.
394 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
396 * \return GL_TRUE for success, GL_FALSE for failure
399 guess_base_level_size(GLenum target
,
400 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
401 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
408 /* Guess the size of the base level.
409 * Depending on the image's size, we can't always make a guess here.
413 case GL_TEXTURE_1D_ARRAY
:
418 case GL_TEXTURE_2D_ARRAY
:
419 /* We can't make a good guess here, because the base level dimensions
422 if (width
== 1 || height
== 1) {
429 case GL_TEXTURE_CUBE_MAP
:
430 case GL_TEXTURE_CUBE_MAP_ARRAY
:
436 /* We can't make a good guess here, because the base level dimensions
439 if (width
== 1 || height
== 1 || depth
== 1) {
447 case GL_TEXTURE_RECTANGLE
:
464 * Try to determine whether we should allocate memory for a full texture
465 * mipmap. The problem is when we get a glTexImage(level=0) call, we
466 * can't immediately know if other mipmap levels are coming next. Here
467 * we try to guess whether to allocate memory for a mipmap or just the
470 * If we guess incorrectly here we'll later reallocate the right amount of
471 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
473 * \param stObj the texture object we're going to allocate memory for.
474 * \param stImage describes the incoming image which we need to store.
477 allocate_full_mipmap(const struct st_texture_object
*stObj
,
478 const struct st_texture_image
*stImage
)
480 switch (stObj
->base
.Target
) {
481 case GL_TEXTURE_RECTANGLE_NV
:
482 case GL_TEXTURE_BUFFER
:
483 case GL_TEXTURE_EXTERNAL_OES
:
484 case GL_TEXTURE_2D_MULTISAMPLE
:
485 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
486 /* these texture types cannot be mipmapped */
490 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
493 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
494 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
495 /* depth/stencil textures are seldom mipmapped */
498 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
501 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
502 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
503 /* not a mipmap minification filter */
506 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
507 /* 3D textures are seldom mipmapped */
515 * Try to allocate a pipe_resource object for the given st_texture_object.
517 * We use the given st_texture_image as a clue to determine the size of the
518 * mipmap image at level=0.
520 * \return GL_TRUE for success, GL_FALSE if out of memory.
523 guess_and_alloc_texture(struct st_context
*st
,
524 struct st_texture_object
*stObj
,
525 const struct st_texture_image
*stImage
)
527 const struct gl_texture_image
*firstImage
;
528 GLuint lastLevel
, width
, height
, depth
;
531 uint16_t ptHeight
, ptDepth
, ptLayers
;
532 enum pipe_format fmt
;
533 bool guessed_box
= false;
535 DBG("%s\n", __func__
);
539 /* If a base level image with compatible size exists, use that as our guess.
541 firstImage
= _mesa_base_tex_image(&stObj
->base
);
543 firstImage
->Width2
> 0 &&
544 firstImage
->Height2
> 0 &&
545 firstImage
->Depth2
> 0 &&
546 guess_base_level_size(stObj
->base
.Target
,
551 &width
, &height
, &depth
)) {
552 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
553 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
554 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
559 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
560 stImage
->base
.Width2
,
561 stImage
->base
.Height2
,
562 stImage
->base
.Depth2
,
564 &width
, &height
, &depth
);
567 /* we can't determine the image size at level=0 */
568 /* this is not an out of memory error */
572 /* At this point, (width x height x depth) is the expected size of
573 * the level=0 mipmap image.
576 /* Guess a reasonable value for lastLevel. With OpenGL we have no
577 * idea how many mipmap levels will be in a texture until we start
578 * to render with it. Make an educated guess here but be prepared
579 * to re-allocating a texture buffer with space for more (or fewer)
580 * mipmap levels later.
582 if (allocate_full_mipmap(stObj
, stImage
)) {
583 /* alloc space for a full mipmap */
584 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
585 width
, height
, depth
) - 1;
588 /* only alloc space for a single mipmap level */
592 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
594 bindings
= default_bindings(st
, fmt
);
596 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
597 width
, height
, depth
,
598 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
600 stObj
->pt
= st_texture_create(st
,
601 gl_target_to_pipe(stObj
->base
.Target
),
610 stObj
->lastLevel
= lastLevel
;
612 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
614 return stObj
->pt
!= NULL
;
619 * Called via ctx->Driver.AllocTextureImageBuffer().
620 * If the texture object/buffer already has space for the indicated image,
621 * we're done. Otherwise, allocate memory for the new texture image.
624 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
625 struct gl_texture_image
*texImage
)
627 struct st_context
*st
= st_context(ctx
);
628 struct st_texture_image
*stImage
= st_texture_image(texImage
);
629 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
630 const GLuint level
= texImage
->Level
;
631 GLuint width
= texImage
->Width
;
632 GLuint height
= texImage
->Height
;
633 GLuint depth
= texImage
->Depth
;
635 DBG("%s\n", __func__
);
637 assert(!stImage
->pt
); /* xxx this might be wrong */
639 stObj
->needs_validation
= true;
641 compressed_tex_fallback_allocate(st
, stImage
);
643 /* Look if the parent texture object has space for this image */
645 level
<= stObj
->pt
->last_level
&&
646 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
647 /* this image will fit in the existing texture object's memory */
648 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
652 /* The parent texture object does not have space for this image */
654 pipe_resource_reference(&stObj
->pt
, NULL
);
655 st_texture_release_all_sampler_views(st
, stObj
);
657 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
658 /* Probably out of memory.
659 * Try flushing any pending rendering, then retry.
662 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
663 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
669 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
670 /* The image will live in the object's mipmap memory */
671 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
676 /* Create a new, temporary texture/resource/buffer to hold this
677 * one texture image. Note that when we later access this image
678 * (either for mapping or copying) we'll want to always specify
679 * mipmap level=0, even if the image represents some other mipmap
682 enum pipe_format format
=
683 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
684 GLuint bindings
= default_bindings(st
, format
);
686 uint16_t ptHeight
, ptDepth
, ptLayers
;
688 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
689 width
, height
, depth
,
690 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
692 stImage
->pt
= st_texture_create(st
,
693 gl_target_to_pipe(stObj
->base
.Target
),
701 return stImage
->pt
!= NULL
;
707 * Preparation prior to glTexImage. Basically check the 'surface_based'
708 * field and switch to a "normal" tex image if necessary.
711 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
712 GLenum format
, GLenum type
)
714 struct gl_texture_object
*texObj
= texImage
->TexObject
;
715 struct st_texture_object
*stObj
= st_texture_object(texObj
);
717 /* switch to "normal" */
718 if (stObj
->surface_based
) {
719 const GLenum target
= texObj
->Target
;
720 const GLuint level
= texImage
->Level
;
721 mesa_format texFormat
;
723 assert(!st_texture_image(texImage
)->pt
);
724 _mesa_clear_texture_object(ctx
, texObj
, texImage
);
725 stObj
->layer_override
= 0;
726 stObj
->level_override
= 0;
727 pipe_resource_reference(&stObj
->pt
, NULL
);
729 /* oops, need to init this image again */
730 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
731 texImage
->InternalFormat
, format
,
734 _mesa_init_teximage_fields(ctx
, texImage
,
735 texImage
->Width
, texImage
->Height
,
736 texImage
->Depth
, texImage
->Border
,
737 texImage
->InternalFormat
, texFormat
);
739 stObj
->surface_based
= GL_FALSE
;
745 * Return a writemask for the gallium blit. The parameters can be base
746 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
749 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
752 case GL_DEPTH_STENCIL
:
754 case GL_DEPTH_STENCIL
:
756 case GL_DEPTH_COMPONENT
:
758 case GL_STENCIL_INDEX
:
765 case GL_DEPTH_COMPONENT
:
767 case GL_DEPTH_STENCIL
:
768 case GL_DEPTH_COMPONENT
:
775 case GL_STENCIL_INDEX
:
777 case GL_STENCIL_INDEX
:
785 return PIPE_MASK_RGBA
;
790 * Converts format to a format with the same components, types
791 * and sizes, but with the components in RGBA order.
793 static enum pipe_format
794 unswizzle_format(enum pipe_format format
)
798 case PIPE_FORMAT_B8G8R8A8_UNORM
:
799 case PIPE_FORMAT_A8R8G8B8_UNORM
:
800 case PIPE_FORMAT_A8B8G8R8_UNORM
:
801 return PIPE_FORMAT_R8G8B8A8_UNORM
;
803 case PIPE_FORMAT_B10G10R10A2_UNORM
:
804 return PIPE_FORMAT_R10G10B10A2_UNORM
;
806 case PIPE_FORMAT_B10G10R10A2_SNORM
:
807 return PIPE_FORMAT_R10G10B10A2_SNORM
;
809 case PIPE_FORMAT_B10G10R10A2_UINT
:
810 return PIPE_FORMAT_R10G10B10A2_UINT
;
818 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
820 static enum pipe_format
821 alpha_to_red(enum pipe_format format
)
825 case PIPE_FORMAT_A8_UNORM
:
826 return PIPE_FORMAT_R8_UNORM
;
827 case PIPE_FORMAT_A8_SNORM
:
828 return PIPE_FORMAT_R8_SNORM
;
829 case PIPE_FORMAT_A8_UINT
:
830 return PIPE_FORMAT_R8_UINT
;
831 case PIPE_FORMAT_A8_SINT
:
832 return PIPE_FORMAT_R8_SINT
;
834 case PIPE_FORMAT_A16_UNORM
:
835 return PIPE_FORMAT_R16_UNORM
;
836 case PIPE_FORMAT_A16_SNORM
:
837 return PIPE_FORMAT_R16_SNORM
;
838 case PIPE_FORMAT_A16_UINT
:
839 return PIPE_FORMAT_R16_UINT
;
840 case PIPE_FORMAT_A16_SINT
:
841 return PIPE_FORMAT_R16_SINT
;
842 case PIPE_FORMAT_A16_FLOAT
:
843 return PIPE_FORMAT_R16_FLOAT
;
845 case PIPE_FORMAT_A32_UINT
:
846 return PIPE_FORMAT_R32_UINT
;
847 case PIPE_FORMAT_A32_SINT
:
848 return PIPE_FORMAT_R32_SINT
;
849 case PIPE_FORMAT_A32_FLOAT
:
850 return PIPE_FORMAT_R32_FLOAT
;
858 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
860 static enum pipe_format
861 red_alpha_to_red_green(enum pipe_format format
)
865 case PIPE_FORMAT_R8A8_UNORM
:
866 return PIPE_FORMAT_R8G8_UNORM
;
867 case PIPE_FORMAT_R8A8_SNORM
:
868 return PIPE_FORMAT_R8G8_SNORM
;
869 case PIPE_FORMAT_R8A8_UINT
:
870 return PIPE_FORMAT_R8G8_UINT
;
871 case PIPE_FORMAT_R8A8_SINT
:
872 return PIPE_FORMAT_R8G8_SINT
;
874 case PIPE_FORMAT_R16A16_UNORM
:
875 return PIPE_FORMAT_R16G16_UNORM
;
876 case PIPE_FORMAT_R16A16_SNORM
:
877 return PIPE_FORMAT_R16G16_SNORM
;
878 case PIPE_FORMAT_R16A16_UINT
:
879 return PIPE_FORMAT_R16G16_UINT
;
880 case PIPE_FORMAT_R16A16_SINT
:
881 return PIPE_FORMAT_R16G16_SINT
;
882 case PIPE_FORMAT_R16A16_FLOAT
:
883 return PIPE_FORMAT_R16G16_FLOAT
;
885 case PIPE_FORMAT_R32A32_UINT
:
886 return PIPE_FORMAT_R32G32_UINT
;
887 case PIPE_FORMAT_R32A32_SINT
:
888 return PIPE_FORMAT_R32G32_SINT
;
889 case PIPE_FORMAT_R32A32_FLOAT
:
890 return PIPE_FORMAT_R32G32_FLOAT
;
898 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
900 static enum pipe_format
901 luminance_alpha_to_red_green(enum pipe_format format
)
905 case PIPE_FORMAT_L8A8_UNORM
:
906 return PIPE_FORMAT_R8G8_UNORM
;
907 case PIPE_FORMAT_L8A8_SNORM
:
908 return PIPE_FORMAT_R8G8_SNORM
;
909 case PIPE_FORMAT_L8A8_UINT
:
910 return PIPE_FORMAT_R8G8_UINT
;
911 case PIPE_FORMAT_L8A8_SINT
:
912 return PIPE_FORMAT_R8G8_SINT
;
914 case PIPE_FORMAT_L16A16_UNORM
:
915 return PIPE_FORMAT_R16G16_UNORM
;
916 case PIPE_FORMAT_L16A16_SNORM
:
917 return PIPE_FORMAT_R16G16_SNORM
;
918 case PIPE_FORMAT_L16A16_UINT
:
919 return PIPE_FORMAT_R16G16_UINT
;
920 case PIPE_FORMAT_L16A16_SINT
:
921 return PIPE_FORMAT_R16G16_SINT
;
922 case PIPE_FORMAT_L16A16_FLOAT
:
923 return PIPE_FORMAT_R16G16_FLOAT
;
925 case PIPE_FORMAT_L32A32_UINT
:
926 return PIPE_FORMAT_R32G32_UINT
;
927 case PIPE_FORMAT_L32A32_SINT
:
928 return PIPE_FORMAT_R32G32_SINT
;
929 case PIPE_FORMAT_L32A32_FLOAT
:
930 return PIPE_FORMAT_R32G32_FLOAT
;
938 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
941 format_is_alpha(enum pipe_format format
)
943 const struct util_format_description
*desc
= util_format_description(format
);
945 if (desc
->nr_channels
== 1 &&
946 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
947 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
948 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
949 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
956 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
959 format_is_red(enum pipe_format format
)
961 const struct util_format_description
*desc
= util_format_description(format
);
963 if (desc
->nr_channels
== 1 &&
964 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
965 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
966 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
967 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
975 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
978 format_is_luminance(enum pipe_format format
)
980 const struct util_format_description
*desc
= util_format_description(format
);
982 if (desc
->nr_channels
== 1 &&
983 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
984 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
985 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
986 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
993 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
996 format_is_red_alpha(enum pipe_format format
)
998 const struct util_format_description
*desc
= util_format_description(format
);
1000 if (desc
->nr_channels
== 2 &&
1001 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
1002 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
1003 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
1004 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
1011 format_is_swizzled_rgba(enum pipe_format format
)
1013 const struct util_format_description
*desc
= util_format_description(format
);
1015 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
1016 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
1017 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
1018 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
1026 unsigned char swizzle
[4];
1027 enum pipe_format format
;
1030 static const struct format_table table_8888_unorm
[] = {
1031 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
1032 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
1033 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
1034 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
1037 static const struct format_table table_1010102_unorm
[] = {
1038 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
1039 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
1042 static const struct format_table table_1010102_snorm
[] = {
1043 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
1044 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
1047 static const struct format_table table_1010102_uint
[] = {
1048 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
1049 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
1052 static enum pipe_format
1053 swizzle_format(enum pipe_format format
, const int * const swizzle
)
1058 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1059 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1060 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1061 case PIPE_FORMAT_A8B8G8R8_UNORM
:
1062 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
1063 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
1064 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
1065 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
1066 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
1067 return table_8888_unorm
[i
].format
;
1071 case PIPE_FORMAT_R10G10B10A2_UNORM
:
1072 case PIPE_FORMAT_B10G10R10A2_UNORM
:
1073 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
1074 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
1075 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
1076 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
1077 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
1078 return table_1010102_unorm
[i
].format
;
1082 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1083 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1084 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1085 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1086 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1087 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1088 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1089 return table_1010102_snorm
[i
].format
;
1093 case PIPE_FORMAT_R10G10B10A2_UINT
:
1094 case PIPE_FORMAT_B10G10R10A2_UINT
:
1095 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1096 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1097 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1098 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1099 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1100 return table_1010102_uint
[i
].format
;
1108 return PIPE_FORMAT_NONE
;
1112 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1114 enum pipe_format src
= *src_format
;
1115 enum pipe_format dst
= *dst_format
;
1117 /* Note: dst_format has already been transformed from luminance/intensity
1118 * to red when this function is called. The source format will never
1119 * be an intensity format, because GL_INTENSITY is not a legal value
1120 * for the format parameter in glTex(Sub)Image(). */
1122 if (format_is_alpha(src
)) {
1123 if (!format_is_alpha(dst
))
1126 src
= alpha_to_red(src
);
1127 dst
= alpha_to_red(dst
);
1128 } else if (format_is_luminance(src
)) {
1129 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1132 src
= util_format_luminance_to_red(src
);
1133 } else if (util_format_is_luminance_alpha(src
)) {
1134 src
= luminance_alpha_to_red_green(src
);
1136 if (format_is_red_alpha(dst
)) {
1137 dst
= red_alpha_to_red_green(dst
);
1138 } else if (!format_is_red(dst
))
1140 } else if (format_is_swizzled_rgba(src
)) {
1141 const struct util_format_description
*src_desc
= util_format_description(src
);
1142 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1146 /* Make sure the format is an RGBA and not an RGBX format */
1147 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1150 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1153 for (i
= 0; i
< 4; i
++)
1154 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1156 dst
= swizzle_format(dst
, swizzle
);
1157 if (dst
== PIPE_FORMAT_NONE
)
1160 src
= unswizzle_format(src
);
1169 try_pbo_upload_common(struct gl_context
*ctx
,
1170 struct pipe_surface
*surface
,
1171 const struct st_pbo_addresses
*addr
,
1172 enum pipe_format src_format
)
1174 struct st_context
*st
= st_context(ctx
);
1175 struct cso_context
*cso
= st
->cso_context
;
1176 struct pipe_context
*pipe
= st
->pipe
;
1177 bool success
= false;
1180 fs
= st_pbo_get_upload_fs(st
, src_format
, surface
->format
);
1184 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1185 CSO_BIT_FRAGMENT_SAMPLERS
|
1186 CSO_BIT_VERTEX_ELEMENTS
|
1187 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1188 CSO_BIT_FRAMEBUFFER
|
1191 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1192 CSO_BIT_RASTERIZER
|
1193 CSO_BIT_STREAM_OUTPUTS
|
1194 CSO_BIT_PAUSE_QUERIES
|
1195 CSO_BIT_SAMPLE_MASK
|
1196 CSO_BIT_MIN_SAMPLES
|
1197 CSO_BIT_RENDER_CONDITION
|
1198 CSO_BITS_ALL_SHADERS
));
1199 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1201 cso_set_sample_mask(cso
, ~0);
1202 cso_set_min_samples(cso
, 1);
1203 cso_set_render_condition(cso
, NULL
, FALSE
, 0);
1205 /* Set up the sampler_view */
1207 struct pipe_sampler_view templ
;
1208 struct pipe_sampler_view
*sampler_view
;
1209 struct pipe_sampler_state sampler
= {0};
1210 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
1212 memset(&templ
, 0, sizeof(templ
));
1213 templ
.target
= PIPE_BUFFER
;
1214 templ
.format
= src_format
;
1215 templ
.u
.buf
.offset
= addr
->first_element
* addr
->bytes_per_pixel
;
1216 templ
.u
.buf
.size
= (addr
->last_element
- addr
->first_element
+ 1) *
1217 addr
->bytes_per_pixel
;
1218 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1219 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1220 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1221 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1223 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1224 if (sampler_view
== NULL
)
1227 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1229 pipe_sampler_view_reference(&sampler_view
, NULL
);
1231 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
1234 /* Framebuffer_state */
1236 struct pipe_framebuffer_state fb
;
1237 memset(&fb
, 0, sizeof(fb
));
1238 fb
.width
= surface
->width
;
1239 fb
.height
= surface
->height
;
1241 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1243 cso_set_framebuffer(cso
, &fb
);
1245 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1248 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1251 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1253 /* Depth/stencil/alpha state */
1255 struct pipe_depth_stencil_alpha_state dsa
;
1256 memset(&dsa
, 0, sizeof(dsa
));
1257 cso_set_depth_stencil_alpha(cso
, &dsa
);
1260 /* Set up the fragment shader */
1261 cso_set_fragment_shader_handle(cso
, fs
);
1263 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1266 cso_restore_state(cso
);
1267 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1273 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1274 struct gl_texture_image
*texImage
,
1275 GLenum format
, GLenum type
,
1276 enum pipe_format dst_format
,
1277 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1278 GLint width
, GLint height
, GLint depth
,
1280 const struct gl_pixelstore_attrib
*unpack
)
1282 struct st_context
*st
= st_context(ctx
);
1283 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1284 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1285 struct pipe_resource
*texture
= stImage
->pt
;
1286 struct pipe_context
*pipe
= st
->pipe
;
1287 struct pipe_screen
*screen
= pipe
->screen
;
1288 struct pipe_surface
*surface
= NULL
;
1289 struct st_pbo_addresses addr
;
1290 enum pipe_format src_format
;
1291 const struct util_format_description
*desc
;
1292 GLenum gl_target
= texImage
->TexObject
->Target
;
1295 if (!st
->pbo
.upload_enabled
)
1298 /* From now on, we need the gallium representation of dimensions. */
1299 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1306 if (depth
!= 1 && !st
->pbo
.layers
)
1309 /* Choose the source format. Initially, we do so without checking driver
1310 * support at all because of the remapping we later perform and because
1311 * at least the Radeon driver actually supports some formats for texture
1312 * buffers which it doesn't support for regular textures. */
1313 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1318 src_format
= util_format_linear(src_format
);
1319 desc
= util_format_description(src_format
);
1321 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1324 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1327 if (st
->pbo
.rgba_only
) {
1328 enum pipe_format orig_dst_format
= dst_format
;
1330 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1334 if (dst_format
!= orig_dst_format
&&
1335 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1336 PIPE_BIND_RENDER_TARGET
)) {
1342 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0,
1343 PIPE_BIND_SAMPLER_VIEW
)) {
1347 /* Compute buffer addresses */
1348 addr
.xoffset
= xoffset
;
1349 addr
.yoffset
= yoffset
;
1351 addr
.height
= height
;
1353 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1355 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1359 /* Set up the surface */
1361 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1362 unsigned max_layer
= util_max_layer(texture
, level
);
1364 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1366 struct pipe_surface templ
;
1367 memset(&templ
, 0, sizeof(templ
));
1368 templ
.format
= dst_format
;
1369 templ
.u
.tex
.level
= level
;
1370 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1371 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1373 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1378 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1380 pipe_surface_reference(&surface
, NULL
);
1386 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1387 struct gl_texture_image
*texImage
,
1388 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1389 GLint width
, GLint height
, GLint depth
,
1390 GLenum format
, GLenum type
, const void *pixels
,
1391 const struct gl_pixelstore_attrib
*unpack
)
1393 struct st_context
*st
= st_context(ctx
);
1394 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1395 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1396 struct pipe_context
*pipe
= st
->pipe
;
1397 struct pipe_screen
*screen
= pipe
->screen
;
1398 struct pipe_resource
*dst
= stImage
->pt
;
1399 struct pipe_resource
*src
= NULL
;
1400 struct pipe_resource src_templ
;
1401 struct pipe_transfer
*transfer
;
1402 struct pipe_blit_info blit
;
1403 enum pipe_format src_format
, dst_format
;
1404 mesa_format mesa_src_format
;
1405 GLenum gl_target
= texImage
->TexObject
->Target
;
1408 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1409 unsigned dst_level
= 0;
1411 st_flush_bitmap_cache(st
);
1412 st_invalidate_readpix_cache(st
);
1414 if (stObj
->pt
== stImage
->pt
)
1415 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1417 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1418 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1423 /* Try texture_subdata, which should be the fastest memcpy path. */
1425 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1426 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1427 texImage
->TexFormat
, format
, type
,
1429 struct pipe_box box
;
1430 unsigned stride
, layer_stride
;
1433 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1434 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1436 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1439 /* Convert to Gallium coordinates. */
1440 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1445 layer_stride
= stride
;
1448 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1449 pipe
->texture_subdata(pipe
, dst
, dst_level
, 0,
1450 &box
, data
, stride
, layer_stride
);
1454 if (!st
->prefer_blit_based_texture_transfer
) {
1458 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1459 * blit implementation in some drivers. */
1460 if (format
== GL_DEPTH_STENCIL
) {
1464 /* If the base internal format and the texture format don't match,
1465 * we can't use blit-based TexSubImage. */
1466 if (texImage
->_BaseFormat
!=
1467 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1472 /* See if the destination format is supported. */
1473 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1474 bind
= PIPE_BIND_DEPTH_STENCIL
;
1476 bind
= PIPE_BIND_RENDER_TARGET
;
1478 /* For luminance and intensity, only the red channel is stored
1479 * in the destination. */
1480 dst_format
= util_format_linear(dst
->format
);
1481 dst_format
= util_format_luminance_to_red(dst_format
);
1482 dst_format
= util_format_intensity_to_red(dst_format
);
1485 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1486 dst
->nr_samples
, bind
)) {
1490 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1491 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1492 xoffset
, yoffset
, zoffset
,
1493 width
, height
, depth
, pixels
, unpack
))
1497 /* See if the texture format already matches the format and type,
1498 * in which case the memcpy-based fast path will likely be used and
1499 * we don't have to blit. */
1500 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1501 type
, unpack
->SwapBytes
, NULL
)) {
1505 /* Choose the source format. */
1506 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1507 format
, type
, unpack
->SwapBytes
);
1512 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1514 /* There is no reason to do this if we cannot use memcpy for the temporary
1515 * source texture at least. This also takes transfer ops into account,
1517 if (!_mesa_texstore_can_use_memcpy(ctx
,
1518 _mesa_get_format_base_format(mesa_src_format
),
1519 mesa_src_format
, format
, type
, unpack
)) {
1523 /* TexSubImage only sets a single cubemap face. */
1524 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1525 gl_target
= GL_TEXTURE_2D
;
1527 /* TexSubImage can specify subsets of cube map array faces
1528 * so we need to upload via 2D array instead */
1529 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1530 gl_target
= GL_TEXTURE_2D_ARRAY
;
1533 /* Initialize the source texture description. */
1534 memset(&src_templ
, 0, sizeof(src_templ
));
1535 src_templ
.target
= gl_target_to_pipe(gl_target
);
1536 src_templ
.format
= src_format
;
1537 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1538 src_templ
.usage
= PIPE_USAGE_STAGING
;
1540 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1541 &src_templ
.width0
, &src_templ
.height0
,
1542 &src_templ
.depth0
, &src_templ
.array_size
);
1544 /* Check for NPOT texture support. */
1545 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1546 (!util_is_power_of_two_or_zero(src_templ
.width0
) ||
1547 !util_is_power_of_two_or_zero(src_templ
.height0
) ||
1548 !util_is_power_of_two_or_zero(src_templ
.depth0
))) {
1552 /* Create the source texture. */
1553 src
= screen
->resource_create(screen
, &src_templ
);
1558 /* Map source pixels. */
1559 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1560 format
, type
, pixels
, unpack
,
1563 /* This is a GL error. */
1564 pipe_resource_reference(&src
, NULL
);
1568 /* From now on, we need the gallium representation of dimensions. */
1569 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1576 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1577 width
, height
, depth
, &transfer
);
1579 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1580 pipe_resource_reference(&src
, NULL
);
1584 /* Upload pixels (just memcpy). */
1586 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1589 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1590 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1591 /* 1D array textures.
1592 * We need to convert gallium coords to GL coords.
1594 void *src
= _mesa_image_address2d(unpack
, pixels
,
1595 width
, depth
, format
,
1597 memcpy(map
, src
, bytesPerRow
);
1600 ubyte
*slice_map
= map
;
1602 for (row
= 0; row
< (unsigned) height
; row
++) {
1603 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1604 width
, height
, format
,
1605 type
, slice
, row
, 0);
1606 memcpy(slice_map
, src
, bytesPerRow
);
1607 slice_map
+= transfer
->stride
;
1610 map
+= transfer
->layer_stride
;
1614 pipe_transfer_unmap(pipe
, transfer
);
1615 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1618 memset(&blit
, 0, sizeof(blit
));
1619 blit
.src
.resource
= src
;
1621 blit
.src
.format
= src_format
;
1622 blit
.dst
.resource
= dst
;
1623 blit
.dst
.level
= dst_level
;
1624 blit
.dst
.format
= dst_format
;
1625 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1626 blit
.dst
.box
.x
= xoffset
;
1627 blit
.dst
.box
.y
= yoffset
;
1628 blit
.dst
.box
.z
= zoffset
+ dstz
;
1629 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1630 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1631 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1632 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1633 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1634 blit
.scissor_enable
= FALSE
;
1636 st
->pipe
->blit(st
->pipe
, &blit
);
1638 pipe_resource_reference(&src
, NULL
);
1642 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1643 width
, height
, depth
, format
, type
, pixels
,
1648 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1649 struct gl_texture_image
*texImage
,
1650 GLenum format
, GLenum type
, const void *pixels
,
1651 const struct gl_pixelstore_attrib
*unpack
)
1653 assert(dims
== 1 || dims
== 2 || dims
== 3);
1655 prep_teximage(ctx
, texImage
, format
, type
);
1657 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1660 /* allocate storage for texture data */
1661 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1662 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1666 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1667 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1668 format
, type
, pixels
, unpack
);
1673 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1674 struct gl_texture_image
*texImage
,
1675 GLint x
, GLint y
, GLint z
,
1676 GLsizei w
, GLsizei h
, GLsizei d
,
1677 GLenum format
, GLsizei imageSize
, const void *data
)
1679 struct st_context
*st
= st_context(ctx
);
1680 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1681 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1682 struct pipe_resource
*texture
= stImage
->pt
;
1683 struct pipe_context
*pipe
= st
->pipe
;
1684 struct pipe_screen
*screen
= pipe
->screen
;
1685 struct pipe_resource
*dst
= stImage
->pt
;
1686 struct pipe_surface
*surface
= NULL
;
1687 struct compressed_pixelstore store
;
1688 struct st_pbo_addresses addr
;
1689 enum pipe_format copy_format
;
1691 intptr_t buf_offset
;
1692 bool success
= false;
1694 /* Check basic pre-conditions for PBO upload */
1695 if (!st
->prefer_blit_based_texture_transfer
) {
1699 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1702 if (st_compressed_format_fallback(st
, texImage
->TexFormat
))
1709 if (!st
->pbo
.upload_enabled
||
1710 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1714 /* Choose the pipe format for the upload. */
1715 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1716 bw
= util_format_get_blockwidth(dst
->format
);
1717 bh
= util_format_get_blockheight(dst
->format
);
1719 switch (addr
.bytes_per_pixel
) {
1721 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1724 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1730 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0,
1731 PIPE_BIND_SAMPLER_VIEW
)) {
1735 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1736 dst
->nr_samples
, PIPE_BIND_RENDER_TARGET
)) {
1740 /* Interpret the pixelstore settings. */
1741 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1742 &ctx
->Unpack
, &store
);
1743 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1744 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1746 /* Compute the offset into the buffer */
1747 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1749 if (buf_offset
% addr
.bytes_per_pixel
) {
1753 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1755 addr
.xoffset
= x
/ bw
;
1756 addr
.yoffset
= y
/ bh
;
1757 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1758 addr
.height
= store
.CopyRowsPerSlice
;
1760 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1761 addr
.image_height
= store
.TotalRowsPerSlice
;
1763 if (!st_pbo_addresses_setup(st
, st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1767 /* Set up the surface. */
1769 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1770 unsigned max_layer
= util_max_layer(texture
, level
);
1772 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1774 struct pipe_surface templ
;
1775 memset(&templ
, 0, sizeof(templ
));
1776 templ
.format
= copy_format
;
1777 templ
.u
.tex
.level
= level
;
1778 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1779 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1781 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1786 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1788 pipe_surface_reference(&surface
, NULL
);
1794 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1796 format
, imageSize
, data
);
1800 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1801 struct gl_texture_image
*texImage
,
1802 GLsizei imageSize
, const void *data
)
1804 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1806 /* only 2D and 3D compressed images are supported at this time */
1808 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1812 /* This is pretty simple, because unlike the general texstore path we don't
1813 * have to worry about the usual image unpacking or image transfer
1817 assert(texImage
->Width
> 0);
1818 assert(texImage
->Height
> 0);
1819 assert(texImage
->Depth
> 0);
1821 /* allocate storage for texture data */
1822 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1823 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1827 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1829 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1830 texImage
->TexFormat
,
1838 * Called via ctx->Driver.GetTexSubImage()
1840 * This uses a blit to copy the texture to a texture format which matches
1841 * the format and type combo and then a fast read-back is done using memcpy.
1842 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1843 * a format which matches the swizzling.
1845 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1847 * NOTE: Drivers usually do a blit to convert between tiled and linear
1848 * texture layouts during texture uploads/downloads, so the blit
1849 * we do here should be free in such cases.
1852 st_GetTexSubImage(struct gl_context
* ctx
,
1853 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1854 GLsizei width
, GLsizei height
, GLint depth
,
1855 GLenum format
, GLenum type
, void * pixels
,
1856 struct gl_texture_image
*texImage
)
1858 struct st_context
*st
= st_context(ctx
);
1859 struct pipe_context
*pipe
= st
->pipe
;
1860 struct pipe_screen
*screen
= pipe
->screen
;
1861 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1862 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1863 struct pipe_resource
*src
= stObj
->pt
;
1864 struct pipe_resource
*dst
= NULL
;
1865 struct pipe_resource dst_templ
;
1866 enum pipe_format dst_format
, src_format
;
1867 mesa_format mesa_format
;
1868 GLenum gl_target
= texImage
->TexObject
->Target
;
1869 enum pipe_texture_target pipe_target
;
1871 struct pipe_blit_info blit
;
1873 struct pipe_transfer
*tex_xfer
;
1875 boolean done
= FALSE
;
1877 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1878 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1880 st_flush_bitmap_cache(st
);
1882 if (!st
->prefer_blit_based_texture_transfer
&&
1883 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1884 /* Try to avoid the fallback if we're doing texture decompression here */
1888 /* Handle non-finalized textures. */
1889 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1893 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1894 * due to an incomplete stencil blit implementation in some drivers. */
1895 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1899 /* If the base internal format and the texture format don't match, we have
1900 * to fall back to _mesa_GetTexImage_sw. */
1901 if (texImage
->_BaseFormat
!=
1902 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1906 /* See if the texture format already matches the format and type,
1907 * in which case the memcpy-based fast path will be used. */
1908 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1909 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1913 /* Convert the source format to what is expected by GetTexImage
1914 * and see if it's supported.
1916 * This only applies to glGetTexImage:
1917 * - Luminance must be returned as (L,0,0,1).
1918 * - Luminance alpha must be returned as (L,0,0,A).
1919 * - Intensity must be returned as (I,0,0,1)
1921 if (stObj
->surface_based
)
1922 src_format
= util_format_linear(stObj
->surface_format
);
1924 src_format
= util_format_linear(src
->format
);
1925 src_format
= util_format_luminance_to_red(src_format
);
1926 src_format
= util_format_intensity_to_red(src_format
);
1929 !screen
->is_format_supported(screen
, src_format
, src
->target
,
1931 PIPE_BIND_SAMPLER_VIEW
)) {
1935 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1936 bind
= PIPE_BIND_DEPTH_STENCIL
;
1938 bind
= PIPE_BIND_RENDER_TARGET
;
1940 /* GetTexImage only returns a single face for cubemaps. */
1941 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1942 gl_target
= GL_TEXTURE_2D
;
1944 pipe_target
= gl_target_to_pipe(gl_target
);
1946 /* Choose the destination format by finding the best match
1947 * for the format+type combo. */
1948 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1949 ctx
->Pack
.SwapBytes
);
1951 if (dst_format
== PIPE_FORMAT_NONE
) {
1952 GLenum dst_glformat
;
1954 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1955 * where decompression with a blit is always preferred. */
1956 if (!util_format_is_compressed(src
->format
)) {
1960 /* Set the appropriate format for the decompressed texture.
1961 * Luminance and sRGB formats shouldn't appear here.*/
1962 switch (src_format
) {
1963 case PIPE_FORMAT_DXT1_RGB
:
1964 case PIPE_FORMAT_DXT1_RGBA
:
1965 case PIPE_FORMAT_DXT3_RGBA
:
1966 case PIPE_FORMAT_DXT5_RGBA
:
1967 case PIPE_FORMAT_RGTC1_UNORM
:
1968 case PIPE_FORMAT_RGTC2_UNORM
:
1969 case PIPE_FORMAT_ETC1_RGB8
:
1970 case PIPE_FORMAT_ETC2_RGB8
:
1971 case PIPE_FORMAT_ETC2_RGB8A1
:
1972 case PIPE_FORMAT_ETC2_RGBA8
:
1973 case PIPE_FORMAT_ASTC_4x4
:
1974 case PIPE_FORMAT_ASTC_5x4
:
1975 case PIPE_FORMAT_ASTC_5x5
:
1976 case PIPE_FORMAT_ASTC_6x5
:
1977 case PIPE_FORMAT_ASTC_6x6
:
1978 case PIPE_FORMAT_ASTC_8x5
:
1979 case PIPE_FORMAT_ASTC_8x6
:
1980 case PIPE_FORMAT_ASTC_8x8
:
1981 case PIPE_FORMAT_ASTC_10x5
:
1982 case PIPE_FORMAT_ASTC_10x6
:
1983 case PIPE_FORMAT_ASTC_10x8
:
1984 case PIPE_FORMAT_ASTC_10x10
:
1985 case PIPE_FORMAT_ASTC_12x10
:
1986 case PIPE_FORMAT_ASTC_12x12
:
1987 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1988 dst_glformat
= GL_RGBA8
;
1990 case PIPE_FORMAT_RGTC1_SNORM
:
1991 case PIPE_FORMAT_RGTC2_SNORM
:
1992 if (!ctx
->Extensions
.EXT_texture_snorm
)
1994 dst_glformat
= GL_RGBA8_SNORM
;
1996 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1997 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1998 if (!ctx
->Extensions
.ARB_texture_float
)
2000 dst_glformat
= GL_RGBA32F
;
2002 case PIPE_FORMAT_ETC2_R11_UNORM
:
2003 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16_UNORM
,
2004 pipe_target
, 0, bind
))
2006 dst_glformat
= GL_R16
;
2008 case PIPE_FORMAT_ETC2_R11_SNORM
:
2009 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16_SNORM
,
2010 pipe_target
, 0, bind
))
2012 dst_glformat
= GL_R16_SNORM
;
2014 case PIPE_FORMAT_ETC2_RG11_UNORM
:
2015 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16G16_UNORM
,
2016 pipe_target
, 0, bind
))
2018 dst_glformat
= GL_RG16
;
2020 case PIPE_FORMAT_ETC2_RG11_SNORM
:
2021 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16G16_SNORM
,
2022 pipe_target
, 0, bind
))
2024 dst_glformat
= GL_RG16_SNORM
;
2031 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
2032 pipe_target
, 0, bind
, FALSE
);
2034 if (dst_format
== PIPE_FORMAT_NONE
) {
2035 /* unable to get an rgba format!?! */
2040 /* create the destination texture of size (width X height X depth) */
2041 memset(&dst_templ
, 0, sizeof(dst_templ
));
2042 dst_templ
.target
= pipe_target
;
2043 dst_templ
.format
= dst_format
;
2044 dst_templ
.bind
= bind
;
2045 dst_templ
.usage
= PIPE_USAGE_STAGING
;
2047 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
2048 &dst_templ
.width0
, &dst_templ
.height0
,
2049 &dst_templ
.depth0
, &dst_templ
.array_size
);
2051 dst
= screen
->resource_create(screen
, &dst_templ
);
2056 /* From now on, we need the gallium representation of dimensions. */
2057 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2064 assert(texImage
->Face
== 0 ||
2065 texImage
->TexObject
->MinLayer
== 0 ||
2068 memset(&blit
, 0, sizeof(blit
));
2069 blit
.src
.resource
= src
;
2070 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2071 blit
.src
.format
= src_format
;
2072 blit
.dst
.resource
= dst
;
2074 blit
.dst
.format
= dst
->format
;
2075 blit
.src
.box
.x
= xoffset
;
2077 blit
.src
.box
.y
= yoffset
;
2079 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2081 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2082 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2083 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2084 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2085 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2086 blit
.scissor_enable
= FALSE
;
2088 /* blit/render/decompress */
2089 st
->pipe
->blit(st
->pipe
, &blit
);
2091 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2093 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2094 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2099 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2100 dims
= _mesa_get_texture_dimensions(gl_target
);
2102 /* copy/pack data into user buffer */
2103 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2104 ctx
->Pack
.SwapBytes
, NULL
)) {
2106 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2109 for (slice
= 0; slice
< depth
; slice
++) {
2110 ubyte
*slice_map
= map
;
2112 for (row
= 0; row
< height
; row
++) {
2113 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2114 width
, height
, format
, type
,
2117 memcpy(dest
, slice_map
, bytesPerRow
);
2119 slice_map
+= tex_xfer
->stride
;
2122 map
+= tex_xfer
->layer_stride
;
2126 /* format translation via floats */
2129 uint32_t dstMesaFormat
;
2130 int dstStride
, srcStride
;
2132 assert(util_format_is_compressed(src
->format
));
2134 rgba
= malloc(width
* height
* 4 * sizeof(GLfloat
));
2139 if (ST_DEBUG
& DEBUG_FALLBACK
)
2140 debug_printf("%s: fallback format translation\n", __func__
);
2142 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2143 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2144 srcStride
= 4 * width
* sizeof(GLfloat
);
2145 for (slice
= 0; slice
< depth
; slice
++) {
2146 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2147 width
, height
, format
, type
,
2150 /* get float[4] rgba row from surface */
2151 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, height
,
2154 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2155 rgba
, RGBA32_FLOAT
, srcStride
,
2156 width
, height
, NULL
);
2158 /* Handle byte swapping if required */
2159 if (ctx
->Pack
.SwapBytes
) {
2160 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
2161 width
, height
, dest
, dest
);
2164 map
+= tex_xfer
->layer_stride
;
2173 pipe_transfer_unmap(pipe
, tex_xfer
);
2175 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2176 pipe_resource_reference(&dst
, NULL
);
2180 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2181 width
, height
, depth
,
2182 format
, type
, pixels
, texImage
);
2188 * Do a CopyTexSubImage operation using a read transfer from the source,
2189 * a write transfer to the destination and get_tile()/put_tile() to access
2190 * the pixels/texels.
2192 * Note: srcY=0=TOP of renderbuffer
2195 fallback_copy_texsubimage(struct gl_context
*ctx
,
2196 struct st_renderbuffer
*strb
,
2197 struct st_texture_image
*stImage
,
2199 GLint destX
, GLint destY
, GLint slice
,
2200 GLint srcX
, GLint srcY
,
2201 GLsizei width
, GLsizei height
)
2203 struct st_context
*st
= st_context(ctx
);
2204 struct pipe_context
*pipe
= st
->pipe
;
2205 struct pipe_transfer
*src_trans
;
2207 enum pipe_transfer_usage transfer_usage
;
2209 unsigned dst_width
= width
;
2210 unsigned dst_height
= height
;
2211 unsigned dst_depth
= 1;
2212 struct pipe_transfer
*transfer
;
2214 if (ST_DEBUG
& DEBUG_FALLBACK
)
2215 debug_printf("%s: fallback processing\n", __func__
);
2217 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2218 srcY
= strb
->Base
.Height
- srcY
- height
;
2221 map
= pipe_transfer_map(pipe
,
2223 strb
->surface
->u
.tex
.level
,
2224 strb
->surface
->u
.tex
.first_layer
,
2227 width
, height
, &src_trans
);
2229 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2230 baseFormat
== GL_DEPTH_STENCIL
) &&
2231 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2232 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2234 transfer_usage
= PIPE_TRANSFER_WRITE
;
2236 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2237 destX
, destY
, slice
,
2238 dst_width
, dst_height
, dst_depth
,
2241 if (baseFormat
== GL_DEPTH_COMPONENT
||
2242 baseFormat
== GL_DEPTH_STENCIL
) {
2243 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2244 ctx
->Pixel
.DepthBias
!= 0.0F
);
2248 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2249 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2258 data
= malloc(width
* sizeof(uint
));
2261 /* To avoid a large temp memory allocation, do copy row by row */
2262 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2263 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2265 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2268 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2269 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2270 0, 0, width
, 1, data
);
2273 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2278 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2286 malloc(width
* height
* 4 * sizeof(GLfloat
));
2288 if (tempSrc
&& texDest
) {
2289 const GLint dims
= 2;
2291 struct gl_texture_image
*texImage
= &stImage
->base
;
2292 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2294 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2295 unpack
.Invert
= GL_TRUE
;
2298 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2299 dstRowStride
= transfer
->layer_stride
;
2302 dstRowStride
= transfer
->stride
;
2305 /* get float/RGBA image from framebuffer */
2306 /* XXX this usually involves a lot of int/float conversion.
2307 * try to avoid that someday.
2309 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2310 util_format_linear(strb
->texture
->format
),
2313 /* Store into texture memory.
2314 * Note that this does some special things such as pixel transfer
2315 * ops and format conversion. In particular, if the dest tex format
2316 * is actually RGBA but the user created the texture as GL_RGB we
2317 * need to fill-in/override the alpha channel with 1.0.
2319 _mesa_texstore(ctx
, dims
,
2320 texImage
->_BaseFormat
,
2321 texImage
->TexFormat
,
2325 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2329 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2335 st_texture_image_unmap(st
, stImage
, slice
);
2336 pipe
->transfer_unmap(pipe
, src_trans
);
2340 st_can_copyteximage_using_blit(const struct gl_texture_image
*texImage
,
2341 const struct gl_renderbuffer
*rb
)
2343 GLenum tex_baseformat
= _mesa_get_format_base_format(texImage
->TexFormat
);
2345 /* We don't blit to a teximage where the GL base format doesn't match the
2346 * texture's chosen format, except in the case of a GL_RGB texture
2347 * represented with GL_RGBA (where the alpha channel is just being
2350 if (texImage
->_BaseFormat
!= tex_baseformat
&&
2351 ((texImage
->_BaseFormat
!= GL_RGB
|| tex_baseformat
!= GL_RGBA
))) {
2355 /* We can't blit from a RB where the GL base format doesn't match the RB's
2356 * chosen format (for example, GL RGB or ALPHA with rb->Format of an RGBA
2357 * type, because the other channels will be undefined).
2359 if (rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
))
2366 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2367 * Note that the region to copy has already been clipped so we know we
2368 * won't read from outside the source renderbuffer's bounds.
2370 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2373 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2374 struct gl_texture_image
*texImage
,
2375 GLint destX
, GLint destY
, GLint slice
,
2376 struct gl_renderbuffer
*rb
,
2377 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2379 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2380 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2381 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2382 struct st_context
*st
= st_context(ctx
);
2383 struct pipe_context
*pipe
= st
->pipe
;
2384 struct pipe_screen
*screen
= pipe
->screen
;
2385 struct pipe_blit_info blit
;
2386 enum pipe_format dst_format
;
2387 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2391 st_flush_bitmap_cache(st
);
2392 st_invalidate_readpix_cache(st
);
2394 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2395 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2397 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2398 debug_printf("%s: null strb or stImage\n", __func__
);
2402 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2403 texImage
->TexFormat
)) {
2407 if (!st_can_copyteximage_using_blit(texImage
, rb
)) {
2411 /* Choose the destination format to match the TexImage behavior. */
2412 dst_format
= util_format_linear(stImage
->pt
->format
);
2413 dst_format
= util_format_luminance_to_red(dst_format
);
2414 dst_format
= util_format_intensity_to_red(dst_format
);
2416 /* See if the destination format is supported. */
2417 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2418 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2419 bind
= PIPE_BIND_DEPTH_STENCIL
;
2422 bind
= PIPE_BIND_RENDER_TARGET
;
2426 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2427 stImage
->pt
->nr_samples
, bind
)) {
2431 /* Y flipping for the main framebuffer. */
2433 srcY1
= strb
->Base
.Height
- srcY
- height
;
2434 srcY0
= srcY1
+ height
;
2438 srcY1
= srcY0
+ height
;
2441 /* Blit the texture.
2442 * This supports flipping, format conversions, and downsampling.
2444 memset(&blit
, 0, sizeof(blit
));
2445 blit
.src
.resource
= strb
->texture
;
2446 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2447 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2448 blit
.src
.box
.x
= srcX
;
2449 blit
.src
.box
.y
= srcY0
;
2450 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2451 blit
.src
.box
.width
= width
;
2452 blit
.src
.box
.height
= srcY1
- srcY0
;
2453 blit
.src
.box
.depth
= 1;
2454 blit
.dst
.resource
= stImage
->pt
;
2455 blit
.dst
.format
= dst_format
;
2456 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2457 blit
.dst
.box
.x
= destX
;
2458 blit
.dst
.box
.y
= destY
;
2459 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2460 blit
.dst
.box
.width
= width
;
2461 blit
.dst
.box
.height
= height
;
2462 blit
.dst
.box
.depth
= 1;
2463 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2464 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2465 pipe
->blit(pipe
, &blit
);
2469 /* software fallback */
2470 fallback_copy_texsubimage(ctx
,
2471 strb
, stImage
, texImage
->_BaseFormat
,
2472 destX
, destY
, slice
,
2473 srcX
, srcY
, width
, height
);
2478 * Copy image data from stImage into the texture object 'stObj' at level
2482 copy_image_data_to_texture(struct st_context
*st
,
2483 struct st_texture_object
*stObj
,
2485 struct st_texture_image
*stImage
)
2489 const struct gl_texture_image MAYBE_UNUSED
*dstImage
=
2490 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2492 assert(dstImage
->Width
== stImage
->base
.Width
);
2493 assert(dstImage
->Height
== stImage
->base
.Height
);
2494 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2498 /* Copy potentially with the blitter:
2501 if (stImage
->pt
->last_level
== 0)
2504 src_level
= stImage
->base
.Level
;
2506 assert(src_level
<= stImage
->pt
->last_level
);
2507 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2508 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2509 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2510 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2511 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2512 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2514 st_texture_image_copy(st
->pipe
,
2515 stObj
->pt
, dstLevel
, /* dest texture, level */
2516 stImage
->pt
, src_level
, /* src texture, level */
2517 stImage
->base
.Face
);
2519 pipe_resource_reference(&stImage
->pt
, NULL
);
2521 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2526 * Called during state validation. When this function is finished,
2527 * the texture object should be ready for rendering.
2528 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2531 st_finalize_texture(struct gl_context
*ctx
,
2532 struct pipe_context
*pipe
,
2533 struct gl_texture_object
*tObj
,
2536 struct st_context
*st
= st_context(ctx
);
2537 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2538 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2540 const struct st_texture_image
*firstImage
;
2541 enum pipe_format firstImageFormat
;
2543 uint16_t ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2545 if (tObj
->Immutable
)
2548 if (tObj
->_MipmapComplete
)
2549 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2550 else if (tObj
->_BaseComplete
)
2551 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2553 /* Skip the loop over images in the common case of no images having
2554 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
2555 * haven't looked at, then we do need to look at those new images.
2557 if (!stObj
->needs_validation
&&
2558 stObj
->base
.BaseLevel
>= stObj
->validated_first_level
&&
2559 stObj
->lastLevel
<= stObj
->validated_last_level
) {
2563 /* If this texture comes from a window system, there is nothing else to do. */
2564 if (stObj
->surface_based
) {
2568 firstImage
= st_texture_image_const(stObj
->base
.Image
[cubeMapFace
][stObj
->base
.BaseLevel
]);
2571 /* If both firstImage and stObj point to a texture which can contain
2572 * all active images, favour firstImage. Note that because of the
2573 * completeness requirement, we know that the image dimensions
2576 if (firstImage
->pt
&&
2577 firstImage
->pt
!= stObj
->pt
&&
2578 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2579 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2580 st_texture_release_all_sampler_views(st
, stObj
);
2583 /* Find gallium format for the Mesa texture */
2585 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2587 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2590 uint16_t height
, depth
;
2592 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2593 firstImage
->base
.Width2
,
2594 firstImage
->base
.Height2
,
2595 firstImage
->base
.Depth2
,
2596 &width
, &height
, &depth
, &ptLayers
);
2598 /* If we previously allocated a pipe texture and its sizes are
2599 * compatible, use them.
2602 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2603 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2604 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2605 ptWidth
= stObj
->pt
->width0
;
2606 ptHeight
= stObj
->pt
->height0
;
2607 ptDepth
= stObj
->pt
->depth0
;
2609 /* Otherwise, compute a new level=0 size that is compatible with the
2612 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2613 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2614 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2616 /* If the base level image is 1x1x1, we still need to ensure that the
2617 * resulting pipe texture ends up with the required number of levels
2620 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2621 ptWidth
<<= firstImage
->base
.Level
;
2623 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2624 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2628 /* At this point, the texture may be incomplete (mismatched cube
2629 * face sizes, for example). If that's the case, give up, but
2630 * don't return GL_FALSE as that would raise an incorrect
2631 * GL_OUT_OF_MEMORY error. See Piglit fbo-incomplete-texture-03 test.
2633 if (!stObj
->base
._BaseComplete
) {
2634 _mesa_test_texobj_completeness(ctx
, &stObj
->base
);
2635 if (!stObj
->base
._BaseComplete
) {
2641 ptNumSamples
= firstImage
->base
.NumSamples
;
2644 /* If we already have a gallium texture, check that it matches the texture
2645 * object's format, target, size, num_levels, etc.
2648 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2649 stObj
->pt
->format
!= firstImageFormat
||
2650 stObj
->pt
->last_level
< stObj
->lastLevel
||
2651 stObj
->pt
->width0
!= ptWidth
||
2652 stObj
->pt
->height0
!= ptHeight
||
2653 stObj
->pt
->depth0
!= ptDepth
||
2654 stObj
->pt
->nr_samples
!= ptNumSamples
||
2655 stObj
->pt
->array_size
!= ptLayers
)
2657 /* The gallium texture does not match the Mesa texture so delete the
2658 * gallium texture now. We'll make a new one below.
2660 pipe_resource_reference(&stObj
->pt
, NULL
);
2661 st_texture_release_all_sampler_views(st
, stObj
);
2662 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
2666 /* May need to create a new gallium texture:
2669 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2671 stObj
->pt
= st_texture_create(st
,
2672 gl_target_to_pipe(stObj
->base
.Target
),
2678 ptLayers
, ptNumSamples
,
2682 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2687 /* Pull in any images not in the object's texture:
2689 for (face
= 0; face
< nr_faces
; face
++) {
2691 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2692 struct st_texture_image
*stImage
=
2693 st_texture_image(stObj
->base
.Image
[face
][level
]);
2695 /* Need to import images in main memory or held in other textures.
2697 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2701 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2702 height
= u_minify(ptHeight
, level
);
2706 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2707 depth
= u_minify(ptDepth
, level
);
2708 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2714 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2715 stImage
->base
.Height
== height
&&
2716 stImage
->base
.Depth
== depth
)) {
2717 /* src image fits expected dest mipmap level size */
2718 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2724 stObj
->validated_first_level
= stObj
->base
.BaseLevel
;
2725 stObj
->validated_last_level
= stObj
->lastLevel
;
2726 stObj
->needs_validation
= false;
2732 * Allocate a new pipe_resource object
2733 * width0, height0, depth0 are the dimensions of the level 0 image
2734 * (the highest resolution). last_level indicates how many mipmap levels
2735 * to allocate storage for. For non-mipmapped textures, this will be zero.
2737 static struct pipe_resource
*
2738 st_texture_create_from_memory(struct st_context
*st
,
2739 struct st_memory_object
*memObj
,
2741 enum pipe_texture_target target
,
2742 enum pipe_format format
,
2751 struct pipe_resource pt
, *newtex
;
2752 struct pipe_screen
*screen
= st
->pipe
->screen
;
2754 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
2756 assert(height0
> 0);
2758 if (target
== PIPE_TEXTURE_CUBE
)
2759 assert(layers
== 6);
2761 DBG("%s target %d format %s last_level %d\n", __func__
,
2762 (int) target
, util_format_name(format
), last_level
);
2765 assert(screen
->is_format_supported(screen
, format
, target
, 0,
2766 PIPE_BIND_SAMPLER_VIEW
));
2768 memset(&pt
, 0, sizeof(pt
));
2771 pt
.last_level
= last_level
;
2773 pt
.height0
= height0
;
2775 pt
.array_size
= layers
;
2776 pt
.usage
= PIPE_USAGE_DEFAULT
;
2778 /* only set this for OpenGL textures, not renderbuffers */
2779 pt
.flags
= PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
;
2780 pt
.nr_samples
= nr_samples
;
2782 newtex
= screen
->resource_from_memobj(screen
, &pt
, memObj
->memory
, offset
);
2784 assert(!newtex
|| pipe_is_referenced(&newtex
->reference
));
2790 * Allocate texture memory for a whole mipmap stack.
2791 * Note: for multisample textures if the requested sample count is not
2792 * supported, we search for the next higher supported sample count.
2795 st_texture_storage(struct gl_context
*ctx
,
2796 struct gl_texture_object
*texObj
,
2797 GLsizei levels
, GLsizei width
,
2798 GLsizei height
, GLsizei depth
,
2799 struct gl_memory_object
*memObj
,
2802 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2803 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2804 struct st_context
*st
= st_context(ctx
);
2805 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2806 struct st_memory_object
*smObj
= st_memory_object(memObj
);
2807 struct pipe_screen
*screen
= st
->pipe
->screen
;
2808 unsigned ptWidth
, bindings
;
2809 uint16_t ptHeight
, ptDepth
, ptLayers
;
2810 enum pipe_format fmt
;
2812 GLuint num_samples
= texImage
->NumSamples
;
2816 stObj
->lastLevel
= levels
- 1;
2818 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2820 bindings
= default_bindings(st
, fmt
);
2822 if (num_samples
> 0) {
2823 /* Find msaa sample count which is actually supported. For example,
2824 * if the user requests 1x but only 4x or 8x msaa is supported, we'll
2827 enum pipe_texture_target ptarget
= gl_target_to_pipe(texObj
->Target
);
2828 boolean found
= FALSE
;
2830 if (ctx
->Const
.MaxSamples
> 1 && num_samples
== 1) {
2831 /* don't try num_samples = 1 with drivers that support real msaa */
2835 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2836 if (screen
->is_format_supported(screen
, fmt
, ptarget
,
2838 PIPE_BIND_SAMPLER_VIEW
)) {
2839 /* Update the sample count in gl_texture_image as well. */
2840 texImage
->NumSamples
= num_samples
;
2851 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2852 width
, height
, depth
,
2853 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2856 stObj
->pt
= st_texture_create_from_memory(st
,
2859 gl_target_to_pipe(texObj
->Target
),
2865 ptLayers
, num_samples
,
2869 stObj
->pt
= st_texture_create(st
,
2870 gl_target_to_pipe(texObj
->Target
),
2876 ptLayers
, num_samples
,
2883 /* Set image resource pointers */
2884 for (level
= 0; level
< levels
; level
++) {
2886 for (face
= 0; face
< numFaces
; face
++) {
2887 struct st_texture_image
*stImage
=
2888 st_texture_image(texObj
->Image
[face
][level
]);
2889 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2891 compressed_tex_fallback_allocate(st
, stImage
);
2895 /* The texture is in a validated state, so no need to check later. */
2896 stObj
->needs_validation
= false;
2897 stObj
->validated_first_level
= 0;
2898 stObj
->validated_last_level
= levels
- 1;
2904 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2905 * for a whole mipmap stack.
2908 st_AllocTextureStorage(struct gl_context
*ctx
,
2909 struct gl_texture_object
*texObj
,
2910 GLsizei levels
, GLsizei width
,
2911 GLsizei height
, GLsizei depth
)
2913 return st_texture_storage(ctx
, texObj
, levels
,
2914 width
, height
, depth
,
2920 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2921 GLuint numLevels
, GLint level
,
2922 mesa_format format
, GLuint numSamples
,
2923 GLint width
, GLint height
, GLint depth
)
2925 struct st_context
*st
= st_context(ctx
);
2926 struct pipe_context
*pipe
= st
->pipe
;
2928 if (width
== 0 || height
== 0 || depth
== 0) {
2929 /* zero-sized images are legal, and always fit! */
2933 if (pipe
->screen
->can_create_resource
) {
2934 /* Ask the gallium driver if the texture is too large */
2935 struct gl_texture_object
*texObj
=
2936 _mesa_get_current_tex_object(ctx
, target
);
2937 struct pipe_resource pt
;
2939 /* Setup the pipe_resource object
2941 memset(&pt
, 0, sizeof(pt
));
2943 pt
.target
= gl_target_to_pipe(target
);
2944 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
2945 pt
.nr_samples
= numSamples
;
2947 st_gl_texture_dims_to_pipe_dims(target
,
2948 width
, height
, depth
,
2949 &pt
.width0
, &pt
.height0
,
2950 &pt
.depth0
, &pt
.array_size
);
2952 if (numLevels
> 0) {
2953 /* For immutable textures we know the final number of mip levels */
2954 pt
.last_level
= numLevels
- 1;
2956 else if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
2957 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
2958 /* assume just one mipmap level */
2962 /* assume a full set of mipmaps */
2963 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
2966 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
2969 /* Use core Mesa fallback */
2970 return _mesa_test_proxy_teximage(ctx
, target
, numLevels
, level
, format
,
2971 numSamples
, width
, height
, depth
);
2976 st_TextureView(struct gl_context
*ctx
,
2977 struct gl_texture_object
*texObj
,
2978 struct gl_texture_object
*origTexObj
)
2980 struct st_context
*st
= st_context(ctx
);
2981 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
2982 struct st_texture_object
*tex
= st_texture_object(texObj
);
2983 struct gl_texture_image
*image
= texObj
->Image
[0][0];
2985 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2986 const int numLevels
= texObj
->NumLevels
;
2991 pipe_resource_reference(&tex
->pt
, orig
->pt
);
2993 /* Set image resource pointers */
2994 for (level
= 0; level
< numLevels
; level
++) {
2995 for (face
= 0; face
< numFaces
; face
++) {
2996 struct st_texture_image
*stImage
=
2997 st_texture_image(texObj
->Image
[face
][level
]);
2998 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
3002 tex
->surface_based
= GL_TRUE
;
3003 tex
->surface_format
=
3004 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
3006 tex
->lastLevel
= numLevels
- 1;
3008 /* free texture sampler views. They need to be recreated when we
3009 * change the texture view parameters.
3011 st_texture_release_all_sampler_views(st
, tex
);
3013 /* The texture is in a validated state, so no need to check later. */
3014 tex
->needs_validation
= false;
3015 tex
->validated_first_level
= 0;
3016 tex
->validated_last_level
= numLevels
- 1;
3023 * Find the mipmap level in 'pt' which matches the level described by
3027 find_mipmap_level(const struct gl_texture_image
*texImage
,
3028 const struct pipe_resource
*pt
)
3030 const GLenum target
= texImage
->TexObject
->Target
;
3031 GLint texWidth
= texImage
->Width
;
3032 GLint texHeight
= texImage
->Height
;
3033 GLint texDepth
= texImage
->Depth
;
3035 uint16_t h
, d
, layers
;
3037 st_gl_texture_dims_to_pipe_dims(target
, texWidth
, texHeight
, texDepth
,
3038 &w
, &h
, &d
, &layers
);
3040 for (level
= 0; level
<= pt
->last_level
; level
++) {
3041 if (u_minify(pt
->width0
, level
) == w
&&
3042 u_minify(pt
->height0
, level
) == h
&&
3043 u_minify(pt
->depth0
, level
) == d
) {
3048 /* If we get here, there must be some sort of inconsistency between
3049 * the Mesa texture object/images and the gallium resource.
3051 debug_printf("Inconsistent textures in find_mipmap_level()\n");
3053 return texImage
->Level
;
3058 st_ClearTexSubImage(struct gl_context
*ctx
,
3059 struct gl_texture_image
*texImage
,
3060 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3061 GLsizei width
, GLsizei height
, GLsizei depth
,
3062 const void *clearValue
)
3064 static const char zeros
[16] = {0};
3065 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3066 struct st_texture_image
*stImage
= st_texture_image(texImage
);
3067 struct pipe_resource
*pt
= stImage
->pt
;
3068 struct st_context
*st
= st_context(ctx
);
3069 struct pipe_context
*pipe
= st
->pipe
;
3071 struct pipe_box box
;
3076 st_flush_bitmap_cache(st
);
3077 st_invalidate_readpix_cache(st
);
3079 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
3080 width
, height
, depth
, &box
);
3081 if (texObj
->Immutable
) {
3082 /* The texture object has to be consistent (no "loose", per-image
3083 * gallium resources). If this texture is a view into another
3084 * texture, we have to apply the MinLevel/Layer offsets. If this is
3085 * not a texture view, the offsets will be zero.
3087 assert(stImage
->pt
== st_texture_object(texObj
)->pt
);
3088 level
= texImage
->Level
+ texObj
->MinLevel
;
3089 box
.z
+= texObj
->MinLayer
;
3092 /* Texture level sizes may be inconsistent. We my have "loose",
3093 * per-image gallium resources. The texImage->Level may not match
3094 * the gallium resource texture level.
3096 level
= find_mipmap_level(texImage
, pt
);
3099 assert(level
<= pt
->last_level
);
3101 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
3106 * Called via the glTexParam*() function, but only when some texture object
3107 * state has actually changed.
3110 st_TexParameter(struct gl_context
*ctx
,
3111 struct gl_texture_object
*texObj
, GLenum pname
)
3113 struct st_context
*st
= st_context(ctx
);
3114 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3117 case GL_TEXTURE_BASE_LEVEL
:
3118 case GL_TEXTURE_MAX_LEVEL
:
3119 case GL_DEPTH_TEXTURE_MODE
:
3120 case GL_DEPTH_STENCIL_TEXTURE_MODE
:
3121 case GL_TEXTURE_SRGB_DECODE_EXT
:
3122 case GL_TEXTURE_SWIZZLE_R
:
3123 case GL_TEXTURE_SWIZZLE_G
:
3124 case GL_TEXTURE_SWIZZLE_B
:
3125 case GL_TEXTURE_SWIZZLE_A
:
3126 case GL_TEXTURE_SWIZZLE_RGBA
:
3127 case GL_TEXTURE_BUFFER_SIZE
:
3128 case GL_TEXTURE_BUFFER_OFFSET
:
3129 /* changing any of these texture parameters means we must create
3130 * new sampler views.
3132 st_texture_release_all_sampler_views(st
, stObj
);
3140 st_SetTextureStorageForMemoryObject(struct gl_context
*ctx
,
3141 struct gl_texture_object
*texObj
,
3142 struct gl_memory_object
*memObj
,
3143 GLsizei levels
, GLsizei width
,
3144 GLsizei height
, GLsizei depth
,
3147 return st_texture_storage(ctx
, texObj
, levels
,
3148 width
, height
, depth
,
3153 st_NewTextureHandle(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
3154 struct gl_sampler_object
*sampObj
)
3156 struct st_context
*st
= st_context(ctx
);
3157 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3158 struct pipe_context
*pipe
= st
->pipe
;
3159 struct pipe_sampler_view
*view
;
3160 struct pipe_sampler_state sampler
= {0};
3162 if (texObj
->Target
!= GL_TEXTURE_BUFFER
) {
3163 if (!st_finalize_texture(ctx
, pipe
, texObj
, 0))
3166 st_convert_sampler(st
, texObj
, sampObj
, 0, &sampler
);
3168 /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
3169 view
= st_get_texture_sampler_view_from_stobj(st
, stObj
, sampObj
, 0, true);
3171 view
= st_get_buffer_sampler_view_from_stobj(st
, stObj
);
3174 return pipe
->create_texture_handle(pipe
, view
, &sampler
);
3179 st_DeleteTextureHandle(struct gl_context
*ctx
, GLuint64 handle
)
3181 struct st_context
*st
= st_context(ctx
);
3182 struct pipe_context
*pipe
= st
->pipe
;
3184 pipe
->delete_texture_handle(pipe
, handle
);
3189 st_MakeTextureHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3192 struct st_context
*st
= st_context(ctx
);
3193 struct pipe_context
*pipe
= st
->pipe
;
3195 pipe
->make_texture_handle_resident(pipe
, handle
, resident
);
3200 st_NewImageHandle(struct gl_context
*ctx
, struct gl_image_unit
*imgObj
)
3202 struct st_context
*st
= st_context(ctx
);
3203 struct pipe_context
*pipe
= st
->pipe
;
3204 struct pipe_image_view image
;
3206 st_convert_image(st
, imgObj
, &image
);
3208 return pipe
->create_image_handle(pipe
, &image
);
3213 st_DeleteImageHandle(struct gl_context
*ctx
, GLuint64 handle
)
3215 struct st_context
*st
= st_context(ctx
);
3216 struct pipe_context
*pipe
= st
->pipe
;
3218 pipe
->delete_image_handle(pipe
, handle
);
3223 st_MakeImageHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3224 GLenum access
, bool resident
)
3226 struct st_context
*st
= st_context(ctx
);
3227 struct pipe_context
*pipe
= st
->pipe
;
3229 pipe
->make_image_handle_resident(pipe
, handle
, access
, resident
);
3234 st_init_texture_functions(struct dd_function_table
*functions
)
3236 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
3237 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
3238 functions
->TexImage
= st_TexImage
;
3239 functions
->TexSubImage
= st_TexSubImage
;
3240 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
3241 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
3242 functions
->GenerateMipmap
= st_generate_mipmap
;
3244 functions
->GetTexSubImage
= st_GetTexSubImage
;
3246 /* compressed texture functions */
3247 functions
->CompressedTexImage
= st_CompressedTexImage
;
3249 functions
->NewTextureObject
= st_NewTextureObject
;
3250 functions
->NewTextureImage
= st_NewTextureImage
;
3251 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
3252 functions
->DeleteTexture
= st_DeleteTextureObject
;
3253 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
3254 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
3255 functions
->MapTextureImage
= st_MapTextureImage
;
3256 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
3258 /* XXX Temporary until we can query pipe's texture sizes */
3259 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
3261 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
3262 functions
->TextureView
= st_TextureView
;
3263 functions
->ClearTexSubImage
= st_ClearTexSubImage
;
3265 functions
->TexParameter
= st_TexParameter
;
3267 /* bindless functions */
3268 functions
->NewTextureHandle
= st_NewTextureHandle
;
3269 functions
->DeleteTextureHandle
= st_DeleteTextureHandle
;
3270 functions
->MakeTextureHandleResident
= st_MakeTextureHandleResident
;
3271 functions
->NewImageHandle
= st_NewImageHandle
;
3272 functions
->DeleteImageHandle
= st_DeleteImageHandle
;
3273 functions
->MakeImageHandleResident
= st_MakeImageHandleResident
;
3275 /* external object functions */
3276 functions
->SetTextureStorageForMemoryObject
= st_SetTextureStorageForMemoryObject
;