1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 #include "main/bufferobj.h"
30 #include "main/enums.h"
31 #include "main/errors.h"
32 #include "main/fbobject.h"
33 #include "main/formats.h"
34 #include "main/format_utils.h"
35 #include "main/glformats.h"
36 #include "main/image.h"
37 #include "main/imports.h"
38 #include "main/macros.h"
39 #include "main/mipmap.h"
40 #include "main/pack.h"
42 #include "main/pixeltransfer.h"
43 #include "main/texcompress.h"
44 #include "main/texcompress_astc.h"
45 #include "main/texcompress_etc.h"
46 #include "main/texgetimage.h"
47 #include "main/teximage.h"
48 #include "main/texobj.h"
49 #include "main/texstore.h"
51 #include "state_tracker/st_debug.h"
52 #include "state_tracker/st_context.h"
53 #include "state_tracker/st_cb_bitmap.h"
54 #include "state_tracker/st_cb_fbo.h"
55 #include "state_tracker/st_cb_flush.h"
56 #include "state_tracker/st_cb_texture.h"
57 #include "state_tracker/st_cb_bufferobjects.h"
58 #include "state_tracker/st_cb_memoryobjects.h"
59 #include "state_tracker/st_format.h"
60 #include "state_tracker/st_pbo.h"
61 #include "state_tracker/st_texture.h"
62 #include "state_tracker/st_gen_mipmap.h"
63 #include "state_tracker/st_atom.h"
64 #include "state_tracker/st_sampler_view.h"
66 #include "pipe/p_context.h"
67 #include "pipe/p_defines.h"
68 #include "util/u_inlines.h"
69 #include "util/u_upload_mgr.h"
70 #include "pipe/p_shader_tokens.h"
71 #include "util/u_tile.h"
72 #include "util/u_format.h"
73 #include "util/u_surface.h"
74 #include "util/u_sampler.h"
75 #include "util/u_math.h"
76 #include "util/u_box.h"
77 #include "util/u_simple_shaders.h"
78 #include "cso_cache/cso_context.h"
79 #include "tgsi/tgsi_ureg.h"
81 #define DBG if (0) printf
84 enum pipe_texture_target
85 gl_target_to_pipe(GLenum target
)
89 case GL_PROXY_TEXTURE_1D
:
90 return PIPE_TEXTURE_1D
;
92 case GL_PROXY_TEXTURE_2D
:
93 case GL_TEXTURE_EXTERNAL_OES
:
94 case GL_TEXTURE_2D_MULTISAMPLE
:
95 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
96 return PIPE_TEXTURE_2D
;
97 case GL_TEXTURE_RECTANGLE_NV
:
98 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
99 return PIPE_TEXTURE_RECT
;
101 case GL_PROXY_TEXTURE_3D
:
102 return PIPE_TEXTURE_3D
;
103 case GL_TEXTURE_CUBE_MAP_ARB
:
104 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
105 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
106 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
107 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
108 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
109 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
110 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
111 return PIPE_TEXTURE_CUBE
;
112 case GL_TEXTURE_1D_ARRAY_EXT
:
113 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
114 return PIPE_TEXTURE_1D_ARRAY
;
115 case GL_TEXTURE_2D_ARRAY_EXT
:
116 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
117 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
118 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
119 return PIPE_TEXTURE_2D_ARRAY
;
120 case GL_TEXTURE_BUFFER
:
122 case GL_TEXTURE_CUBE_MAP_ARRAY
:
123 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
124 return PIPE_TEXTURE_CUBE_ARRAY
;
132 /** called via ctx->Driver.NewTextureImage() */
133 static struct gl_texture_image
*
134 st_NewTextureImage(struct gl_context
* ctx
)
136 DBG("%s\n", __func__
);
138 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
142 /** called via ctx->Driver.DeleteTextureImage() */
144 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
146 /* nothing special (yet) for st_texture_image */
147 _mesa_delete_texture_image(ctx
, img
);
151 /** called via ctx->Driver.NewTextureObject() */
152 static struct gl_texture_object
*
153 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
155 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
159 /* Pre-allocate a sampler views container to save a branch in the fast path. */
160 obj
->sampler_views
= calloc(1, sizeof(struct st_sampler_views
) + sizeof(struct st_sampler_view
));
161 if (!obj
->sampler_views
) {
165 obj
->sampler_views
->max
= 1;
167 DBG("%s\n", __func__
);
168 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
170 simple_mtx_init(&obj
->validate_mutex
, mtx_plain
);
171 obj
->needs_validation
= true;
176 /** called via ctx->Driver.DeleteTextureObject() */
178 st_DeleteTextureObject(struct gl_context
*ctx
,
179 struct gl_texture_object
*texObj
)
181 struct st_context
*st
= st_context(ctx
);
182 struct st_texture_object
*stObj
= st_texture_object(texObj
);
184 pipe_resource_reference(&stObj
->pt
, NULL
);
185 st_texture_release_all_sampler_views(st
, stObj
);
186 st_texture_free_sampler_views(stObj
);
187 simple_mtx_destroy(&stObj
->validate_mutex
);
188 _mesa_delete_texture_object(ctx
, texObj
);
192 /** called via ctx->Driver.FreeTextureImageBuffer() */
194 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
195 struct gl_texture_image
*texImage
)
197 struct st_context
*st
= st_context(ctx
);
198 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
199 struct st_texture_image
*stImage
= st_texture_image(texImage
);
201 DBG("%s\n", __func__
);
204 pipe_resource_reference(&stImage
->pt
, NULL
);
207 free(stImage
->transfer
);
208 stImage
->transfer
= NULL
;
209 stImage
->num_transfers
= 0;
211 if (stImage
->compressed_data
) {
212 free(stImage
->compressed_data
);
213 stImage
->compressed_data
= NULL
;
216 /* if the texture image is being deallocated, the structure of the
217 * texture is changing so we'll likely need a new sampler view.
219 st_texture_release_all_sampler_views(st
, stObj
);
223 st_compressed_format_fallback(struct st_context
*st
, mesa_format format
)
225 if (format
== MESA_FORMAT_ETC1_RGB8
)
226 return !st
->has_etc1
;
228 if (_mesa_is_format_etc2(format
))
229 return !st
->has_etc2
;
231 if (_mesa_is_format_astc_2d(format
))
232 return !st
->has_astc_2d_ldr
;
238 compressed_tex_fallback_allocate(struct st_context
*st
,
239 struct st_texture_image
*stImage
)
241 struct gl_texture_image
*texImage
= &stImage
->base
;
243 if (!st_compressed_format_fallback(st
, texImage
->TexFormat
))
246 if (stImage
->compressed_data
)
247 free(stImage
->compressed_data
);
249 unsigned data_size
= _mesa_format_image_size(texImage
->TexFormat
,
254 stImage
->compressed_data
=
255 malloc(data_size
* _mesa_num_tex_faces(texImage
->TexObject
->Target
));
258 /** called via ctx->Driver.MapTextureImage() */
260 st_MapTextureImage(struct gl_context
*ctx
,
261 struct gl_texture_image
*texImage
,
262 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
264 GLubyte
**mapOut
, GLint
*rowStrideOut
)
266 struct st_context
*st
= st_context(ctx
);
267 struct st_texture_image
*stImage
= st_texture_image(texImage
);
269 struct pipe_transfer
*transfer
;
271 /* Check for unexpected flags */
272 assert((mode
& ~(GL_MAP_READ_BIT
|
274 GL_MAP_INVALIDATE_RANGE_BIT
)) == 0);
276 const enum pipe_transfer_usage transfer_flags
=
277 st_access_flags_to_transfer_flags(mode
, false);
279 map
= st_texture_image_map(st
, stImage
, transfer_flags
, x
, y
, slice
, w
, h
, 1,
282 if (st_compressed_format_fallback(st
, texImage
->TexFormat
)) {
283 /* Some compressed formats don't have to be supported by drivers,
284 * and st/mesa transparently handles decompression on upload (Unmap),
285 * so that drivers don't see the compressed formats.
287 * We store the compressed data (it's needed for glGetCompressedTex-
288 * Image and image copies in OES_copy_image).
290 unsigned z
= transfer
->box
.z
;
291 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
293 unsigned blk_w
, blk_h
;
294 _mesa_get_format_block_size(texImage
->TexFormat
, &blk_w
, &blk_h
);
296 unsigned y_blocks
= DIV_ROUND_UP(texImage
->Height2
, blk_h
);
297 unsigned stride
= *rowStrideOut
= itransfer
->temp_stride
=
298 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width2
);
299 unsigned block_size
= _mesa_get_format_bytes(texImage
->TexFormat
);
301 *mapOut
= itransfer
->temp_data
=
302 stImage
->compressed_data
+
303 (z
* y_blocks
+ (y
/ blk_h
)) * stride
+
304 (x
/ blk_w
) * block_size
;
305 itransfer
->map
= map
;
308 /* supported mapping */
310 *rowStrideOut
= transfer
->stride
;
320 /** called via ctx->Driver.UnmapTextureImage() */
322 st_UnmapTextureImage(struct gl_context
*ctx
,
323 struct gl_texture_image
*texImage
,
326 struct st_context
*st
= st_context(ctx
);
327 struct st_texture_image
*stImage
= st_texture_image(texImage
);
329 if (st_compressed_format_fallback(st
, texImage
->TexFormat
)) {
330 /* Decompress the compressed image on upload if the driver doesn't
331 * support the compressed format. */
332 unsigned z
= slice
+ stImage
->base
.Face
;
333 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
334 struct pipe_transfer
*transfer
= itransfer
->transfer
;
336 assert(z
== transfer
->box
.z
);
338 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
339 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
340 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
341 itransfer
->temp_data
,
342 itransfer
->temp_stride
,
343 transfer
->box
.width
, transfer
->box
.height
);
344 } else if (_mesa_is_format_etc2(texImage
->TexFormat
)) {
345 bool bgra
= stImage
->pt
->format
== PIPE_FORMAT_B8G8R8A8_SRGB
;
346 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
347 itransfer
->temp_data
, itransfer
->temp_stride
,
348 transfer
->box
.width
, transfer
->box
.height
,
351 } else if (_mesa_is_format_astc_2d(texImage
->TexFormat
)) {
352 _mesa_unpack_astc_2d_ldr(itransfer
->map
, transfer
->stride
,
353 itransfer
->temp_data
, itransfer
->temp_stride
,
354 transfer
->box
.width
, transfer
->box
.height
,
355 texImage
->TexFormat
);
357 unreachable("unexpected format for a compressed format fallback");
361 itransfer
->temp_data
= NULL
;
362 itransfer
->temp_stride
= 0;
366 st_texture_image_unmap(st
, stImage
, slice
);
371 * Return default texture resource binding bitmask for the given format.
374 default_bindings(struct st_context
*st
, enum pipe_format format
)
376 struct pipe_screen
*screen
= st
->pipe
->screen
;
377 const unsigned target
= PIPE_TEXTURE_2D
;
380 if (util_format_is_depth_or_stencil(format
))
381 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
383 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
385 if (screen
->is_format_supported(screen
, format
, target
, 0, 0, bindings
))
389 format
= util_format_linear(format
);
391 if (screen
->is_format_supported(screen
, format
, target
, 0, 0, bindings
))
394 return PIPE_BIND_SAMPLER_VIEW
;
400 * Given the size of a mipmap image, try to compute the size of the level=0
403 * Note that this isn't always accurate for odd-sized, non-POW textures.
404 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
406 * \return GL_TRUE for success, GL_FALSE for failure
409 guess_base_level_size(GLenum target
,
410 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
411 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
418 /* Guess the size of the base level.
419 * Depending on the image's size, we can't always make a guess here.
423 case GL_TEXTURE_1D_ARRAY
:
428 case GL_TEXTURE_2D_ARRAY
:
429 /* We can't make a good guess here, because the base level dimensions
432 if (width
== 1 || height
== 1) {
439 case GL_TEXTURE_CUBE_MAP
:
440 case GL_TEXTURE_CUBE_MAP_ARRAY
:
446 /* We can't make a good guess here, because the base level dimensions
449 if (width
== 1 || height
== 1 || depth
== 1) {
457 case GL_TEXTURE_RECTANGLE
:
474 * Try to determine whether we should allocate memory for a full texture
475 * mipmap. The problem is when we get a glTexImage(level=0) call, we
476 * can't immediately know if other mipmap levels are coming next. Here
477 * we try to guess whether to allocate memory for a mipmap or just the
480 * If we guess incorrectly here we'll later reallocate the right amount of
481 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
483 * \param stObj the texture object we're going to allocate memory for.
484 * \param stImage describes the incoming image which we need to store.
487 allocate_full_mipmap(const struct st_texture_object
*stObj
,
488 const struct st_texture_image
*stImage
)
490 switch (stObj
->base
.Target
) {
491 case GL_TEXTURE_RECTANGLE_NV
:
492 case GL_TEXTURE_BUFFER
:
493 case GL_TEXTURE_EXTERNAL_OES
:
494 case GL_TEXTURE_2D_MULTISAMPLE
:
495 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
496 /* these texture types cannot be mipmapped */
500 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
503 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
504 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
505 /* depth/stencil textures are seldom mipmapped */
508 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
511 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
512 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
513 /* not a mipmap minification filter */
516 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
517 /* 3D textures are seldom mipmapped */
525 * Try to allocate a pipe_resource object for the given st_texture_object.
527 * We use the given st_texture_image as a clue to determine the size of the
528 * mipmap image at level=0.
530 * \return GL_TRUE for success, GL_FALSE if out of memory.
533 guess_and_alloc_texture(struct st_context
*st
,
534 struct st_texture_object
*stObj
,
535 const struct st_texture_image
*stImage
)
537 const struct gl_texture_image
*firstImage
;
538 GLuint lastLevel
, width
, height
, depth
;
541 uint16_t ptHeight
, ptDepth
, ptLayers
;
542 enum pipe_format fmt
;
543 bool guessed_box
= false;
545 DBG("%s\n", __func__
);
549 /* If a base level image with compatible size exists, use that as our guess.
551 firstImage
= _mesa_base_tex_image(&stObj
->base
);
553 firstImage
->Width2
> 0 &&
554 firstImage
->Height2
> 0 &&
555 firstImage
->Depth2
> 0 &&
556 guess_base_level_size(stObj
->base
.Target
,
561 &width
, &height
, &depth
)) {
562 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
563 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
564 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
569 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
570 stImage
->base
.Width2
,
571 stImage
->base
.Height2
,
572 stImage
->base
.Depth2
,
574 &width
, &height
, &depth
);
577 /* we can't determine the image size at level=0 */
578 /* this is not an out of memory error */
582 /* At this point, (width x height x depth) is the expected size of
583 * the level=0 mipmap image.
586 /* Guess a reasonable value for lastLevel. With OpenGL we have no
587 * idea how many mipmap levels will be in a texture until we start
588 * to render with it. Make an educated guess here but be prepared
589 * to re-allocating a texture buffer with space for more (or fewer)
590 * mipmap levels later.
592 if (allocate_full_mipmap(stObj
, stImage
)) {
593 /* alloc space for a full mipmap */
594 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
595 width
, height
, depth
) - 1;
598 /* only alloc space for a single mipmap level */
602 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
604 bindings
= default_bindings(st
, fmt
);
606 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
607 width
, height
, depth
,
608 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
610 stObj
->pt
= st_texture_create(st
,
611 gl_target_to_pipe(stObj
->base
.Target
),
620 stObj
->lastLevel
= lastLevel
;
622 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
624 return stObj
->pt
!= NULL
;
629 * Called via ctx->Driver.AllocTextureImageBuffer().
630 * If the texture object/buffer already has space for the indicated image,
631 * we're done. Otherwise, allocate memory for the new texture image.
634 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
635 struct gl_texture_image
*texImage
)
637 struct st_context
*st
= st_context(ctx
);
638 struct st_texture_image
*stImage
= st_texture_image(texImage
);
639 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
640 const GLuint level
= texImage
->Level
;
641 GLuint width
= texImage
->Width
;
642 GLuint height
= texImage
->Height
;
643 GLuint depth
= texImage
->Depth
;
645 DBG("%s\n", __func__
);
647 assert(!stImage
->pt
); /* xxx this might be wrong */
649 stObj
->needs_validation
= true;
651 compressed_tex_fallback_allocate(st
, stImage
);
653 /* Look if the parent texture object has space for this image */
655 level
<= stObj
->pt
->last_level
&&
656 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
657 /* this image will fit in the existing texture object's memory */
658 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
662 /* The parent texture object does not have space for this image */
664 pipe_resource_reference(&stObj
->pt
, NULL
);
665 st_texture_release_all_sampler_views(st
, stObj
);
667 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
668 /* Probably out of memory.
669 * Try flushing any pending rendering, then retry.
672 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
673 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
679 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
680 /* The image will live in the object's mipmap memory */
681 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
686 /* Create a new, temporary texture/resource/buffer to hold this
687 * one texture image. Note that when we later access this image
688 * (either for mapping or copying) we'll want to always specify
689 * mipmap level=0, even if the image represents some other mipmap
692 enum pipe_format format
=
693 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
694 GLuint bindings
= default_bindings(st
, format
);
696 uint16_t ptHeight
, ptDepth
, ptLayers
;
698 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
699 width
, height
, depth
,
700 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
702 stImage
->pt
= st_texture_create(st
,
703 gl_target_to_pipe(stObj
->base
.Target
),
711 return stImage
->pt
!= NULL
;
717 * Preparation prior to glTexImage. Basically check the 'surface_based'
718 * field and switch to a "normal" tex image if necessary.
721 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
722 GLenum format
, GLenum type
)
724 struct gl_texture_object
*texObj
= texImage
->TexObject
;
725 struct st_texture_object
*stObj
= st_texture_object(texObj
);
727 /* switch to "normal" */
728 if (stObj
->surface_based
) {
729 const GLenum target
= texObj
->Target
;
730 const GLuint level
= texImage
->Level
;
731 mesa_format texFormat
;
733 assert(!st_texture_image(texImage
)->pt
);
734 _mesa_clear_texture_object(ctx
, texObj
, texImage
);
735 stObj
->layer_override
= 0;
736 stObj
->level_override
= 0;
737 pipe_resource_reference(&stObj
->pt
, NULL
);
739 /* oops, need to init this image again */
740 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
741 texImage
->InternalFormat
, format
,
744 _mesa_init_teximage_fields(ctx
, texImage
,
745 texImage
->Width
, texImage
->Height
,
746 texImage
->Depth
, texImage
->Border
,
747 texImage
->InternalFormat
, texFormat
);
749 stObj
->surface_based
= GL_FALSE
;
755 * Return a writemask for the gallium blit. The parameters can be base
756 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
759 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
762 case GL_DEPTH_STENCIL
:
764 case GL_DEPTH_STENCIL
:
766 case GL_DEPTH_COMPONENT
:
768 case GL_STENCIL_INDEX
:
775 case GL_DEPTH_COMPONENT
:
777 case GL_DEPTH_STENCIL
:
778 case GL_DEPTH_COMPONENT
:
785 case GL_STENCIL_INDEX
:
787 case GL_STENCIL_INDEX
:
795 return PIPE_MASK_RGBA
;
800 * Converts format to a format with the same components, types
801 * and sizes, but with the components in RGBA order.
803 static enum pipe_format
804 unswizzle_format(enum pipe_format format
)
808 case PIPE_FORMAT_B8G8R8A8_UNORM
:
809 case PIPE_FORMAT_A8R8G8B8_UNORM
:
810 case PIPE_FORMAT_A8B8G8R8_UNORM
:
811 return PIPE_FORMAT_R8G8B8A8_UNORM
;
813 case PIPE_FORMAT_B10G10R10A2_UNORM
:
814 return PIPE_FORMAT_R10G10B10A2_UNORM
;
816 case PIPE_FORMAT_B10G10R10A2_SNORM
:
817 return PIPE_FORMAT_R10G10B10A2_SNORM
;
819 case PIPE_FORMAT_B10G10R10A2_UINT
:
820 return PIPE_FORMAT_R10G10B10A2_UINT
;
828 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
830 static enum pipe_format
831 alpha_to_red(enum pipe_format format
)
835 case PIPE_FORMAT_A8_UNORM
:
836 return PIPE_FORMAT_R8_UNORM
;
837 case PIPE_FORMAT_A8_SNORM
:
838 return PIPE_FORMAT_R8_SNORM
;
839 case PIPE_FORMAT_A8_UINT
:
840 return PIPE_FORMAT_R8_UINT
;
841 case PIPE_FORMAT_A8_SINT
:
842 return PIPE_FORMAT_R8_SINT
;
844 case PIPE_FORMAT_A16_UNORM
:
845 return PIPE_FORMAT_R16_UNORM
;
846 case PIPE_FORMAT_A16_SNORM
:
847 return PIPE_FORMAT_R16_SNORM
;
848 case PIPE_FORMAT_A16_UINT
:
849 return PIPE_FORMAT_R16_UINT
;
850 case PIPE_FORMAT_A16_SINT
:
851 return PIPE_FORMAT_R16_SINT
;
852 case PIPE_FORMAT_A16_FLOAT
:
853 return PIPE_FORMAT_R16_FLOAT
;
855 case PIPE_FORMAT_A32_UINT
:
856 return PIPE_FORMAT_R32_UINT
;
857 case PIPE_FORMAT_A32_SINT
:
858 return PIPE_FORMAT_R32_SINT
;
859 case PIPE_FORMAT_A32_FLOAT
:
860 return PIPE_FORMAT_R32_FLOAT
;
868 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
870 static enum pipe_format
871 red_alpha_to_red_green(enum pipe_format format
)
875 case PIPE_FORMAT_R8A8_UNORM
:
876 return PIPE_FORMAT_R8G8_UNORM
;
877 case PIPE_FORMAT_R8A8_SNORM
:
878 return PIPE_FORMAT_R8G8_SNORM
;
879 case PIPE_FORMAT_R8A8_UINT
:
880 return PIPE_FORMAT_R8G8_UINT
;
881 case PIPE_FORMAT_R8A8_SINT
:
882 return PIPE_FORMAT_R8G8_SINT
;
884 case PIPE_FORMAT_R16A16_UNORM
:
885 return PIPE_FORMAT_R16G16_UNORM
;
886 case PIPE_FORMAT_R16A16_SNORM
:
887 return PIPE_FORMAT_R16G16_SNORM
;
888 case PIPE_FORMAT_R16A16_UINT
:
889 return PIPE_FORMAT_R16G16_UINT
;
890 case PIPE_FORMAT_R16A16_SINT
:
891 return PIPE_FORMAT_R16G16_SINT
;
892 case PIPE_FORMAT_R16A16_FLOAT
:
893 return PIPE_FORMAT_R16G16_FLOAT
;
895 case PIPE_FORMAT_R32A32_UINT
:
896 return PIPE_FORMAT_R32G32_UINT
;
897 case PIPE_FORMAT_R32A32_SINT
:
898 return PIPE_FORMAT_R32G32_SINT
;
899 case PIPE_FORMAT_R32A32_FLOAT
:
900 return PIPE_FORMAT_R32G32_FLOAT
;
908 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
910 static enum pipe_format
911 luminance_alpha_to_red_green(enum pipe_format format
)
915 case PIPE_FORMAT_L8A8_UNORM
:
916 return PIPE_FORMAT_R8G8_UNORM
;
917 case PIPE_FORMAT_L8A8_SNORM
:
918 return PIPE_FORMAT_R8G8_SNORM
;
919 case PIPE_FORMAT_L8A8_UINT
:
920 return PIPE_FORMAT_R8G8_UINT
;
921 case PIPE_FORMAT_L8A8_SINT
:
922 return PIPE_FORMAT_R8G8_SINT
;
924 case PIPE_FORMAT_L16A16_UNORM
:
925 return PIPE_FORMAT_R16G16_UNORM
;
926 case PIPE_FORMAT_L16A16_SNORM
:
927 return PIPE_FORMAT_R16G16_SNORM
;
928 case PIPE_FORMAT_L16A16_UINT
:
929 return PIPE_FORMAT_R16G16_UINT
;
930 case PIPE_FORMAT_L16A16_SINT
:
931 return PIPE_FORMAT_R16G16_SINT
;
932 case PIPE_FORMAT_L16A16_FLOAT
:
933 return PIPE_FORMAT_R16G16_FLOAT
;
935 case PIPE_FORMAT_L32A32_UINT
:
936 return PIPE_FORMAT_R32G32_UINT
;
937 case PIPE_FORMAT_L32A32_SINT
:
938 return PIPE_FORMAT_R32G32_SINT
;
939 case PIPE_FORMAT_L32A32_FLOAT
:
940 return PIPE_FORMAT_R32G32_FLOAT
;
948 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
951 format_is_alpha(enum pipe_format format
)
953 const struct util_format_description
*desc
= util_format_description(format
);
955 if (desc
->nr_channels
== 1 &&
956 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
957 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
958 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
959 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
966 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
969 format_is_red(enum pipe_format format
)
971 const struct util_format_description
*desc
= util_format_description(format
);
973 if (desc
->nr_channels
== 1 &&
974 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
975 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
976 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
977 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
985 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
988 format_is_luminance(enum pipe_format format
)
990 const struct util_format_description
*desc
= util_format_description(format
);
992 if (desc
->nr_channels
== 1 &&
993 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
994 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
995 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
996 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1003 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
1006 format_is_red_alpha(enum pipe_format format
)
1008 const struct util_format_description
*desc
= util_format_description(format
);
1010 if (desc
->nr_channels
== 2 &&
1011 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
1012 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
1013 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
1014 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
1021 format_is_swizzled_rgba(enum pipe_format format
)
1023 const struct util_format_description
*desc
= util_format_description(format
);
1025 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
1026 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
1027 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
1028 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
1036 unsigned char swizzle
[4];
1037 enum pipe_format format
;
1040 static const struct format_table table_8888_unorm
[] = {
1041 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
1042 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
1043 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
1044 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
1047 static const struct format_table table_1010102_unorm
[] = {
1048 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
1049 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
1052 static const struct format_table table_1010102_snorm
[] = {
1053 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
1054 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
1057 static const struct format_table table_1010102_uint
[] = {
1058 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
1059 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
1062 static enum pipe_format
1063 swizzle_format(enum pipe_format format
, const int * const swizzle
)
1068 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1069 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1070 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1071 case PIPE_FORMAT_A8B8G8R8_UNORM
:
1072 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
1073 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
1074 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
1075 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
1076 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
1077 return table_8888_unorm
[i
].format
;
1081 case PIPE_FORMAT_R10G10B10A2_UNORM
:
1082 case PIPE_FORMAT_B10G10R10A2_UNORM
:
1083 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
1084 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
1085 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
1086 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
1087 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
1088 return table_1010102_unorm
[i
].format
;
1092 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1093 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1094 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1095 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1096 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1097 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1098 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1099 return table_1010102_snorm
[i
].format
;
1103 case PIPE_FORMAT_R10G10B10A2_UINT
:
1104 case PIPE_FORMAT_B10G10R10A2_UINT
:
1105 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1106 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1107 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1108 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1109 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1110 return table_1010102_uint
[i
].format
;
1118 return PIPE_FORMAT_NONE
;
1122 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1124 enum pipe_format src
= *src_format
;
1125 enum pipe_format dst
= *dst_format
;
1127 /* Note: dst_format has already been transformed from luminance/intensity
1128 * to red when this function is called. The source format will never
1129 * be an intensity format, because GL_INTENSITY is not a legal value
1130 * for the format parameter in glTex(Sub)Image(). */
1132 if (format_is_alpha(src
)) {
1133 if (!format_is_alpha(dst
))
1136 src
= alpha_to_red(src
);
1137 dst
= alpha_to_red(dst
);
1138 } else if (format_is_luminance(src
)) {
1139 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1142 src
= util_format_luminance_to_red(src
);
1143 } else if (util_format_is_luminance_alpha(src
)) {
1144 src
= luminance_alpha_to_red_green(src
);
1146 if (format_is_red_alpha(dst
)) {
1147 dst
= red_alpha_to_red_green(dst
);
1148 } else if (!format_is_red(dst
))
1150 } else if (format_is_swizzled_rgba(src
)) {
1151 const struct util_format_description
*src_desc
= util_format_description(src
);
1152 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1156 /* Make sure the format is an RGBA and not an RGBX format */
1157 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1160 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1163 for (i
= 0; i
< 4; i
++)
1164 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1166 dst
= swizzle_format(dst
, swizzle
);
1167 if (dst
== PIPE_FORMAT_NONE
)
1170 src
= unswizzle_format(src
);
1179 try_pbo_upload_common(struct gl_context
*ctx
,
1180 struct pipe_surface
*surface
,
1181 const struct st_pbo_addresses
*addr
,
1182 enum pipe_format src_format
)
1184 struct st_context
*st
= st_context(ctx
);
1185 struct cso_context
*cso
= st
->cso_context
;
1186 struct pipe_context
*pipe
= st
->pipe
;
1187 bool success
= false;
1190 fs
= st_pbo_get_upload_fs(st
, src_format
, surface
->format
);
1194 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1195 CSO_BIT_FRAGMENT_SAMPLERS
|
1196 CSO_BIT_VERTEX_ELEMENTS
|
1197 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1198 CSO_BIT_FRAMEBUFFER
|
1201 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1202 CSO_BIT_RASTERIZER
|
1203 CSO_BIT_STREAM_OUTPUTS
|
1204 CSO_BIT_PAUSE_QUERIES
|
1205 CSO_BIT_SAMPLE_MASK
|
1206 CSO_BIT_MIN_SAMPLES
|
1207 CSO_BIT_RENDER_CONDITION
|
1208 CSO_BITS_ALL_SHADERS
));
1209 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1211 cso_set_sample_mask(cso
, ~0);
1212 cso_set_min_samples(cso
, 1);
1213 cso_set_render_condition(cso
, NULL
, FALSE
, 0);
1215 /* Set up the sampler_view */
1217 struct pipe_sampler_view templ
;
1218 struct pipe_sampler_view
*sampler_view
;
1219 struct pipe_sampler_state sampler
= {0};
1220 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
1222 memset(&templ
, 0, sizeof(templ
));
1223 templ
.target
= PIPE_BUFFER
;
1224 templ
.format
= src_format
;
1225 templ
.u
.buf
.offset
= addr
->first_element
* addr
->bytes_per_pixel
;
1226 templ
.u
.buf
.size
= (addr
->last_element
- addr
->first_element
+ 1) *
1227 addr
->bytes_per_pixel
;
1228 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1229 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1230 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1231 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1233 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1234 if (sampler_view
== NULL
)
1237 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1239 pipe_sampler_view_reference(&sampler_view
, NULL
);
1241 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
1244 /* Framebuffer_state */
1246 struct pipe_framebuffer_state fb
;
1247 memset(&fb
, 0, sizeof(fb
));
1248 fb
.width
= surface
->width
;
1249 fb
.height
= surface
->height
;
1251 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1253 cso_set_framebuffer(cso
, &fb
);
1255 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1258 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1261 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1263 /* Depth/stencil/alpha state */
1265 struct pipe_depth_stencil_alpha_state dsa
;
1266 memset(&dsa
, 0, sizeof(dsa
));
1267 cso_set_depth_stencil_alpha(cso
, &dsa
);
1270 /* Set up the fragment shader */
1271 cso_set_fragment_shader_handle(cso
, fs
);
1273 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1276 cso_restore_state(cso
);
1277 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1283 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1284 struct gl_texture_image
*texImage
,
1285 GLenum format
, GLenum type
,
1286 enum pipe_format dst_format
,
1287 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1288 GLint width
, GLint height
, GLint depth
,
1290 const struct gl_pixelstore_attrib
*unpack
)
1292 struct st_context
*st
= st_context(ctx
);
1293 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1294 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1295 struct pipe_resource
*texture
= stImage
->pt
;
1296 struct pipe_context
*pipe
= st
->pipe
;
1297 struct pipe_screen
*screen
= pipe
->screen
;
1298 struct pipe_surface
*surface
= NULL
;
1299 struct st_pbo_addresses addr
;
1300 enum pipe_format src_format
;
1301 const struct util_format_description
*desc
;
1302 GLenum gl_target
= texImage
->TexObject
->Target
;
1305 if (!st
->pbo
.upload_enabled
)
1308 /* From now on, we need the gallium representation of dimensions. */
1309 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1316 if (depth
!= 1 && !st
->pbo
.layers
)
1319 /* Choose the source format. Initially, we do so without checking driver
1320 * support at all because of the remapping we later perform and because
1321 * at least the Radeon driver actually supports some formats for texture
1322 * buffers which it doesn't support for regular textures. */
1323 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1328 src_format
= util_format_linear(src_format
);
1329 desc
= util_format_description(src_format
);
1331 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1334 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1337 if (st
->pbo
.rgba_only
) {
1338 enum pipe_format orig_dst_format
= dst_format
;
1340 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1344 if (dst_format
!= orig_dst_format
&&
1345 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1346 0, PIPE_BIND_RENDER_TARGET
)) {
1352 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0, 0,
1353 PIPE_BIND_SAMPLER_VIEW
)) {
1357 /* Compute buffer addresses */
1358 addr
.xoffset
= xoffset
;
1359 addr
.yoffset
= yoffset
;
1361 addr
.height
= height
;
1363 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1365 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1369 /* Set up the surface */
1371 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1372 unsigned max_layer
= util_max_layer(texture
, level
);
1374 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1376 struct pipe_surface templ
;
1377 memset(&templ
, 0, sizeof(templ
));
1378 templ
.format
= dst_format
;
1379 templ
.u
.tex
.level
= level
;
1380 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1381 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1383 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1388 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1390 pipe_surface_reference(&surface
, NULL
);
1396 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1397 struct gl_texture_image
*texImage
,
1398 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1399 GLint width
, GLint height
, GLint depth
,
1400 GLenum format
, GLenum type
, const void *pixels
,
1401 const struct gl_pixelstore_attrib
*unpack
)
1403 struct st_context
*st
= st_context(ctx
);
1404 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1405 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1406 struct pipe_context
*pipe
= st
->pipe
;
1407 struct pipe_screen
*screen
= pipe
->screen
;
1408 struct pipe_resource
*dst
= stImage
->pt
;
1409 struct pipe_resource
*src
= NULL
;
1410 struct pipe_resource src_templ
;
1411 struct pipe_transfer
*transfer
;
1412 struct pipe_blit_info blit
;
1413 enum pipe_format src_format
, dst_format
;
1414 mesa_format mesa_src_format
;
1415 GLenum gl_target
= texImage
->TexObject
->Target
;
1418 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1419 unsigned dst_level
= 0;
1421 st_flush_bitmap_cache(st
);
1422 st_invalidate_readpix_cache(st
);
1424 if (stObj
->pt
== stImage
->pt
)
1425 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1427 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1428 !_mesa_is_format_astc_2d(texImage
->TexFormat
) &&
1429 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1434 /* Try texture_subdata, which should be the fastest memcpy path. */
1436 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1437 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1438 texImage
->TexFormat
, format
, type
,
1440 struct pipe_box box
;
1441 unsigned stride
, layer_stride
;
1444 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1445 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1447 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1450 /* Convert to Gallium coordinates. */
1451 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1456 layer_stride
= stride
;
1459 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1460 pipe
->texture_subdata(pipe
, dst
, dst_level
, 0,
1461 &box
, data
, stride
, layer_stride
);
1465 if (!st
->prefer_blit_based_texture_transfer
) {
1469 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1470 * blit implementation in some drivers. */
1471 if (format
== GL_DEPTH_STENCIL
) {
1475 /* If the base internal format and the texture format don't match,
1476 * we can't use blit-based TexSubImage. */
1477 if (texImage
->_BaseFormat
!=
1478 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1483 /* See if the destination format is supported. */
1484 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1485 bind
= PIPE_BIND_DEPTH_STENCIL
;
1487 bind
= PIPE_BIND_RENDER_TARGET
;
1489 /* For luminance and intensity, only the red channel is stored
1490 * in the destination. */
1491 dst_format
= util_format_linear(dst
->format
);
1492 dst_format
= util_format_luminance_to_red(dst_format
);
1493 dst_format
= util_format_intensity_to_red(dst_format
);
1496 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1497 dst
->nr_samples
, dst
->nr_storage_samples
,
1502 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1503 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1504 xoffset
, yoffset
, zoffset
,
1505 width
, height
, depth
, pixels
, unpack
))
1509 /* See if the texture format already matches the format and type,
1510 * in which case the memcpy-based fast path will likely be used and
1511 * we don't have to blit. */
1512 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1513 type
, unpack
->SwapBytes
, NULL
)) {
1517 /* Choose the source format. */
1518 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1519 format
, type
, unpack
->SwapBytes
);
1524 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1526 /* There is no reason to do this if we cannot use memcpy for the temporary
1527 * source texture at least. This also takes transfer ops into account,
1529 if (!_mesa_texstore_can_use_memcpy(ctx
,
1530 _mesa_get_format_base_format(mesa_src_format
),
1531 mesa_src_format
, format
, type
, unpack
)) {
1535 /* TexSubImage only sets a single cubemap face. */
1536 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1537 gl_target
= GL_TEXTURE_2D
;
1539 /* TexSubImage can specify subsets of cube map array faces
1540 * so we need to upload via 2D array instead */
1541 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1542 gl_target
= GL_TEXTURE_2D_ARRAY
;
1545 /* Initialize the source texture description. */
1546 memset(&src_templ
, 0, sizeof(src_templ
));
1547 src_templ
.target
= gl_target_to_pipe(gl_target
);
1548 src_templ
.format
= src_format
;
1549 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1550 src_templ
.usage
= PIPE_USAGE_STAGING
;
1552 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1553 &src_templ
.width0
, &src_templ
.height0
,
1554 &src_templ
.depth0
, &src_templ
.array_size
);
1556 /* Check for NPOT texture support. */
1557 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1558 (!util_is_power_of_two_or_zero(src_templ
.width0
) ||
1559 !util_is_power_of_two_or_zero(src_templ
.height0
) ||
1560 !util_is_power_of_two_or_zero(src_templ
.depth0
))) {
1564 /* Create the source texture. */
1565 src
= screen
->resource_create(screen
, &src_templ
);
1570 /* Map source pixels. */
1571 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1572 format
, type
, pixels
, unpack
,
1575 /* This is a GL error. */
1576 pipe_resource_reference(&src
, NULL
);
1580 /* From now on, we need the gallium representation of dimensions. */
1581 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1588 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1589 width
, height
, depth
, &transfer
);
1591 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1592 pipe_resource_reference(&src
, NULL
);
1596 /* Upload pixels (just memcpy). */
1598 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1601 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1602 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1603 /* 1D array textures.
1604 * We need to convert gallium coords to GL coords.
1606 void *src
= _mesa_image_address2d(unpack
, pixels
,
1607 width
, depth
, format
,
1609 memcpy(map
, src
, bytesPerRow
);
1612 ubyte
*slice_map
= map
;
1614 for (row
= 0; row
< (unsigned) height
; row
++) {
1615 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1616 width
, height
, format
,
1617 type
, slice
, row
, 0);
1618 memcpy(slice_map
, src
, bytesPerRow
);
1619 slice_map
+= transfer
->stride
;
1622 map
+= transfer
->layer_stride
;
1626 pipe_transfer_unmap(pipe
, transfer
);
1627 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1630 memset(&blit
, 0, sizeof(blit
));
1631 blit
.src
.resource
= src
;
1633 blit
.src
.format
= src_format
;
1634 blit
.dst
.resource
= dst
;
1635 blit
.dst
.level
= dst_level
;
1636 blit
.dst
.format
= dst_format
;
1637 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1638 blit
.dst
.box
.x
= xoffset
;
1639 blit
.dst
.box
.y
= yoffset
;
1640 blit
.dst
.box
.z
= zoffset
+ dstz
;
1641 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1642 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1643 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1644 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1645 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1646 blit
.scissor_enable
= FALSE
;
1648 st
->pipe
->blit(st
->pipe
, &blit
);
1650 pipe_resource_reference(&src
, NULL
);
1654 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1655 width
, height
, depth
, format
, type
, pixels
,
1660 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1661 struct gl_texture_image
*texImage
,
1662 GLenum format
, GLenum type
, const void *pixels
,
1663 const struct gl_pixelstore_attrib
*unpack
)
1665 assert(dims
== 1 || dims
== 2 || dims
== 3);
1667 prep_teximage(ctx
, texImage
, format
, type
);
1669 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1672 /* allocate storage for texture data */
1673 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1674 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1678 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1679 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1680 format
, type
, pixels
, unpack
);
1685 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1686 struct gl_texture_image
*texImage
,
1687 GLint x
, GLint y
, GLint z
,
1688 GLsizei w
, GLsizei h
, GLsizei d
,
1689 GLenum format
, GLsizei imageSize
, const void *data
)
1691 struct st_context
*st
= st_context(ctx
);
1692 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1693 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1694 struct pipe_resource
*texture
= stImage
->pt
;
1695 struct pipe_context
*pipe
= st
->pipe
;
1696 struct pipe_screen
*screen
= pipe
->screen
;
1697 struct pipe_resource
*dst
= stImage
->pt
;
1698 struct pipe_surface
*surface
= NULL
;
1699 struct compressed_pixelstore store
;
1700 struct st_pbo_addresses addr
;
1701 enum pipe_format copy_format
;
1703 intptr_t buf_offset
;
1704 bool success
= false;
1706 /* Check basic pre-conditions for PBO upload */
1707 if (!st
->prefer_blit_based_texture_transfer
) {
1711 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1714 if (st_compressed_format_fallback(st
, texImage
->TexFormat
))
1721 if (!st
->pbo
.upload_enabled
||
1722 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1726 /* Choose the pipe format for the upload. */
1727 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1728 bw
= util_format_get_blockwidth(dst
->format
);
1729 bh
= util_format_get_blockheight(dst
->format
);
1731 switch (addr
.bytes_per_pixel
) {
1733 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1736 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1742 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0, 0,
1743 PIPE_BIND_SAMPLER_VIEW
)) {
1747 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1748 dst
->nr_samples
, dst
->nr_storage_samples
,
1749 PIPE_BIND_RENDER_TARGET
)) {
1753 /* Interpret the pixelstore settings. */
1754 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1755 &ctx
->Unpack
, &store
);
1756 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1757 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1759 /* Compute the offset into the buffer */
1760 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1762 if (buf_offset
% addr
.bytes_per_pixel
) {
1766 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1768 addr
.xoffset
= x
/ bw
;
1769 addr
.yoffset
= y
/ bh
;
1770 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1771 addr
.height
= store
.CopyRowsPerSlice
;
1773 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1774 addr
.image_height
= store
.TotalRowsPerSlice
;
1776 if (!st_pbo_addresses_setup(st
, st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1780 /* Set up the surface. */
1782 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1783 unsigned max_layer
= util_max_layer(texture
, level
);
1785 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1787 struct pipe_surface templ
;
1788 memset(&templ
, 0, sizeof(templ
));
1789 templ
.format
= copy_format
;
1790 templ
.u
.tex
.level
= level
;
1791 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1792 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1794 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1799 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1801 pipe_surface_reference(&surface
, NULL
);
1807 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1809 format
, imageSize
, data
);
1813 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1814 struct gl_texture_image
*texImage
,
1815 GLsizei imageSize
, const void *data
)
1817 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1819 /* only 2D and 3D compressed images are supported at this time */
1821 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1825 /* This is pretty simple, because unlike the general texstore path we don't
1826 * have to worry about the usual image unpacking or image transfer
1830 assert(texImage
->Width
> 0);
1831 assert(texImage
->Height
> 0);
1832 assert(texImage
->Depth
> 0);
1834 /* allocate storage for texture data */
1835 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1836 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1840 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1842 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1843 texImage
->TexFormat
,
1851 * Called via ctx->Driver.GetTexSubImage()
1853 * This uses a blit to copy the texture to a texture format which matches
1854 * the format and type combo and then a fast read-back is done using memcpy.
1855 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1856 * a format which matches the swizzling.
1858 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1860 * NOTE: Drivers usually do a blit to convert between tiled and linear
1861 * texture layouts during texture uploads/downloads, so the blit
1862 * we do here should be free in such cases.
1865 st_GetTexSubImage(struct gl_context
* ctx
,
1866 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1867 GLsizei width
, GLsizei height
, GLint depth
,
1868 GLenum format
, GLenum type
, void * pixels
,
1869 struct gl_texture_image
*texImage
)
1871 struct st_context
*st
= st_context(ctx
);
1872 struct pipe_context
*pipe
= st
->pipe
;
1873 struct pipe_screen
*screen
= pipe
->screen
;
1874 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1875 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1876 struct pipe_resource
*src
= stObj
->pt
;
1877 struct pipe_resource
*dst
= NULL
;
1878 struct pipe_resource dst_templ
;
1879 enum pipe_format dst_format
, src_format
;
1880 mesa_format mesa_format
;
1881 GLenum gl_target
= texImage
->TexObject
->Target
;
1882 enum pipe_texture_target pipe_target
;
1884 struct pipe_blit_info blit
;
1886 struct pipe_transfer
*tex_xfer
;
1888 boolean done
= FALSE
;
1890 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1891 !_mesa_is_format_astc_2d(texImage
->TexFormat
) &&
1892 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1894 st_flush_bitmap_cache(st
);
1896 if (!st
->prefer_blit_based_texture_transfer
&&
1897 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1898 /* Try to avoid the fallback if we're doing texture decompression here */
1902 /* Handle non-finalized textures. */
1903 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1907 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1908 * due to an incomplete stencil blit implementation in some drivers. */
1909 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1913 /* If the base internal format and the texture format don't match, we have
1914 * to fall back to _mesa_GetTexImage_sw. */
1915 if (texImage
->_BaseFormat
!=
1916 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1920 /* See if the texture format already matches the format and type,
1921 * in which case the memcpy-based fast path will be used. */
1922 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1923 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1927 /* Convert the source format to what is expected by GetTexImage
1928 * and see if it's supported.
1930 * This only applies to glGetTexImage:
1931 * - Luminance must be returned as (L,0,0,1).
1932 * - Luminance alpha must be returned as (L,0,0,A).
1933 * - Intensity must be returned as (I,0,0,1)
1935 if (stObj
->surface_based
)
1936 src_format
= util_format_linear(stObj
->surface_format
);
1938 src_format
= util_format_linear(src
->format
);
1939 src_format
= util_format_luminance_to_red(src_format
);
1940 src_format
= util_format_intensity_to_red(src_format
);
1943 !screen
->is_format_supported(screen
, src_format
, src
->target
,
1944 src
->nr_samples
, src
->nr_storage_samples
,
1945 PIPE_BIND_SAMPLER_VIEW
)) {
1949 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1950 bind
= PIPE_BIND_DEPTH_STENCIL
;
1952 bind
= PIPE_BIND_RENDER_TARGET
;
1954 /* GetTexImage only returns a single face for cubemaps. */
1955 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1956 gl_target
= GL_TEXTURE_2D
;
1958 pipe_target
= gl_target_to_pipe(gl_target
);
1960 /* Choose the destination format by finding the best match
1961 * for the format+type combo. */
1962 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1963 ctx
->Pack
.SwapBytes
);
1965 if (dst_format
== PIPE_FORMAT_NONE
) {
1966 GLenum dst_glformat
;
1968 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1969 * where decompression with a blit is always preferred. */
1970 if (!util_format_is_compressed(src
->format
)) {
1974 /* Set the appropriate format for the decompressed texture.
1975 * Luminance and sRGB formats shouldn't appear here.*/
1976 switch (src_format
) {
1977 case PIPE_FORMAT_DXT1_RGB
:
1978 case PIPE_FORMAT_DXT1_RGBA
:
1979 case PIPE_FORMAT_DXT3_RGBA
:
1980 case PIPE_FORMAT_DXT5_RGBA
:
1981 case PIPE_FORMAT_RGTC1_UNORM
:
1982 case PIPE_FORMAT_RGTC2_UNORM
:
1983 case PIPE_FORMAT_ETC1_RGB8
:
1984 case PIPE_FORMAT_ETC2_RGB8
:
1985 case PIPE_FORMAT_ETC2_RGB8A1
:
1986 case PIPE_FORMAT_ETC2_RGBA8
:
1987 case PIPE_FORMAT_ASTC_4x4
:
1988 case PIPE_FORMAT_ASTC_5x4
:
1989 case PIPE_FORMAT_ASTC_5x5
:
1990 case PIPE_FORMAT_ASTC_6x5
:
1991 case PIPE_FORMAT_ASTC_6x6
:
1992 case PIPE_FORMAT_ASTC_8x5
:
1993 case PIPE_FORMAT_ASTC_8x6
:
1994 case PIPE_FORMAT_ASTC_8x8
:
1995 case PIPE_FORMAT_ASTC_10x5
:
1996 case PIPE_FORMAT_ASTC_10x6
:
1997 case PIPE_FORMAT_ASTC_10x8
:
1998 case PIPE_FORMAT_ASTC_10x10
:
1999 case PIPE_FORMAT_ASTC_12x10
:
2000 case PIPE_FORMAT_ASTC_12x12
:
2001 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
2002 dst_glformat
= GL_RGBA8
;
2004 case PIPE_FORMAT_RGTC1_SNORM
:
2005 case PIPE_FORMAT_RGTC2_SNORM
:
2006 if (!ctx
->Extensions
.EXT_texture_snorm
)
2008 dst_glformat
= GL_RGBA8_SNORM
;
2010 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
2011 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
2012 if (!ctx
->Extensions
.ARB_texture_float
)
2014 dst_glformat
= GL_RGBA32F
;
2016 case PIPE_FORMAT_ETC2_R11_UNORM
:
2017 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16_UNORM
,
2018 pipe_target
, 0, 0, bind
))
2020 dst_glformat
= GL_R16
;
2022 case PIPE_FORMAT_ETC2_R11_SNORM
:
2023 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16_SNORM
,
2024 pipe_target
, 0, 0, bind
))
2026 dst_glformat
= GL_R16_SNORM
;
2028 case PIPE_FORMAT_ETC2_RG11_UNORM
:
2029 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16G16_UNORM
,
2030 pipe_target
, 0, 0, bind
))
2032 dst_glformat
= GL_RG16
;
2034 case PIPE_FORMAT_ETC2_RG11_SNORM
:
2035 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R16G16_SNORM
,
2036 pipe_target
, 0, 0, bind
))
2038 dst_glformat
= GL_RG16_SNORM
;
2045 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
2046 pipe_target
, 0, 0, bind
, FALSE
);
2048 if (dst_format
== PIPE_FORMAT_NONE
) {
2049 /* unable to get an rgba format!?! */
2054 /* create the destination texture of size (width X height X depth) */
2055 memset(&dst_templ
, 0, sizeof(dst_templ
));
2056 dst_templ
.target
= pipe_target
;
2057 dst_templ
.format
= dst_format
;
2058 dst_templ
.bind
= bind
;
2059 dst_templ
.usage
= PIPE_USAGE_STAGING
;
2061 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
2062 &dst_templ
.width0
, &dst_templ
.height0
,
2063 &dst_templ
.depth0
, &dst_templ
.array_size
);
2065 dst
= screen
->resource_create(screen
, &dst_templ
);
2070 /* From now on, we need the gallium representation of dimensions. */
2071 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2078 assert(texImage
->Face
== 0 ||
2079 texImage
->TexObject
->MinLayer
== 0 ||
2082 memset(&blit
, 0, sizeof(blit
));
2083 blit
.src
.resource
= src
;
2084 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2085 blit
.src
.format
= src_format
;
2086 blit
.dst
.resource
= dst
;
2088 blit
.dst
.format
= dst
->format
;
2089 blit
.src
.box
.x
= xoffset
;
2091 blit
.src
.box
.y
= yoffset
;
2093 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2095 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2096 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2097 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2098 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2099 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2100 blit
.scissor_enable
= FALSE
;
2102 /* blit/render/decompress */
2103 st
->pipe
->blit(st
->pipe
, &blit
);
2105 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2107 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2108 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2113 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2114 dims
= _mesa_get_texture_dimensions(gl_target
);
2116 /* copy/pack data into user buffer */
2117 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2118 ctx
->Pack
.SwapBytes
, NULL
)) {
2120 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2123 for (slice
= 0; slice
< depth
; slice
++) {
2124 ubyte
*slice_map
= map
;
2126 for (row
= 0; row
< height
; row
++) {
2127 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2128 width
, height
, format
, type
,
2131 memcpy(dest
, slice_map
, bytesPerRow
);
2133 slice_map
+= tex_xfer
->stride
;
2136 map
+= tex_xfer
->layer_stride
;
2140 /* format translation via floats */
2143 uint32_t dstMesaFormat
;
2144 int dstStride
, srcStride
;
2146 assert(util_format_is_compressed(src
->format
));
2148 rgba
= malloc(width
* height
* 4 * sizeof(GLfloat
));
2153 if (ST_DEBUG
& DEBUG_FALLBACK
)
2154 debug_printf("%s: fallback format translation\n", __func__
);
2156 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2157 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2158 srcStride
= 4 * width
* sizeof(GLfloat
);
2159 for (slice
= 0; slice
< depth
; slice
++) {
2160 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2161 width
, height
, format
, type
,
2164 /* get float[4] rgba row from surface */
2165 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, height
,
2168 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2169 rgba
, RGBA32_FLOAT
, srcStride
,
2170 width
, height
, NULL
);
2172 /* Handle byte swapping if required */
2173 if (ctx
->Pack
.SwapBytes
) {
2174 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
2175 width
, height
, dest
, dest
);
2178 map
+= tex_xfer
->layer_stride
;
2187 pipe_transfer_unmap(pipe
, tex_xfer
);
2189 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2190 pipe_resource_reference(&dst
, NULL
);
2194 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2195 width
, height
, depth
,
2196 format
, type
, pixels
, texImage
);
2202 * Do a CopyTexSubImage operation using a read transfer from the source,
2203 * a write transfer to the destination and get_tile()/put_tile() to access
2204 * the pixels/texels.
2206 * Note: srcY=0=TOP of renderbuffer
2209 fallback_copy_texsubimage(struct gl_context
*ctx
,
2210 struct st_renderbuffer
*strb
,
2211 struct st_texture_image
*stImage
,
2213 GLint destX
, GLint destY
, GLint slice
,
2214 GLint srcX
, GLint srcY
,
2215 GLsizei width
, GLsizei height
)
2217 struct st_context
*st
= st_context(ctx
);
2218 struct pipe_context
*pipe
= st
->pipe
;
2219 struct pipe_transfer
*src_trans
;
2221 enum pipe_transfer_usage transfer_usage
;
2223 unsigned dst_width
= width
;
2224 unsigned dst_height
= height
;
2225 unsigned dst_depth
= 1;
2226 struct pipe_transfer
*transfer
;
2228 if (ST_DEBUG
& DEBUG_FALLBACK
)
2229 debug_printf("%s: fallback processing\n", __func__
);
2231 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2232 srcY
= strb
->Base
.Height
- srcY
- height
;
2235 map
= pipe_transfer_map(pipe
,
2237 strb
->surface
->u
.tex
.level
,
2238 strb
->surface
->u
.tex
.first_layer
,
2241 width
, height
, &src_trans
);
2243 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2244 baseFormat
== GL_DEPTH_STENCIL
) &&
2245 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2246 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2248 transfer_usage
= PIPE_TRANSFER_WRITE
;
2250 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2251 destX
, destY
, slice
,
2252 dst_width
, dst_height
, dst_depth
,
2255 if (baseFormat
== GL_DEPTH_COMPONENT
||
2256 baseFormat
== GL_DEPTH_STENCIL
) {
2257 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2258 ctx
->Pixel
.DepthBias
!= 0.0F
);
2262 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2263 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2272 data
= malloc(width
* sizeof(uint
));
2275 /* To avoid a large temp memory allocation, do copy row by row */
2276 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2277 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2279 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2282 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2283 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2284 0, 0, width
, 1, data
);
2287 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2292 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2300 malloc(width
* height
* 4 * sizeof(GLfloat
));
2302 if (tempSrc
&& texDest
) {
2303 const GLint dims
= 2;
2305 struct gl_texture_image
*texImage
= &stImage
->base
;
2306 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2308 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2309 unpack
.Invert
= GL_TRUE
;
2312 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2313 dstRowStride
= transfer
->layer_stride
;
2316 dstRowStride
= transfer
->stride
;
2319 /* get float/RGBA image from framebuffer */
2320 /* XXX this usually involves a lot of int/float conversion.
2321 * try to avoid that someday.
2323 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2324 util_format_linear(strb
->texture
->format
),
2327 /* Store into texture memory.
2328 * Note that this does some special things such as pixel transfer
2329 * ops and format conversion. In particular, if the dest tex format
2330 * is actually RGBA but the user created the texture as GL_RGB we
2331 * need to fill-in/override the alpha channel with 1.0.
2333 _mesa_texstore(ctx
, dims
,
2334 texImage
->_BaseFormat
,
2335 texImage
->TexFormat
,
2339 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2343 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2349 st_texture_image_unmap(st
, stImage
, slice
);
2350 pipe
->transfer_unmap(pipe
, src_trans
);
2354 st_can_copyteximage_using_blit(const struct gl_texture_image
*texImage
,
2355 const struct gl_renderbuffer
*rb
)
2357 GLenum tex_baseformat
= _mesa_get_format_base_format(texImage
->TexFormat
);
2359 /* We don't blit to a teximage where the GL base format doesn't match the
2360 * texture's chosen format, except in the case of a GL_RGB texture
2361 * represented with GL_RGBA (where the alpha channel is just being
2364 if (texImage
->_BaseFormat
!= tex_baseformat
&&
2365 ((texImage
->_BaseFormat
!= GL_RGB
|| tex_baseformat
!= GL_RGBA
))) {
2369 /* We can't blit from a RB where the GL base format doesn't match the RB's
2370 * chosen format (for example, GL RGB or ALPHA with rb->Format of an RGBA
2371 * type, because the other channels will be undefined).
2373 if (rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
))
2380 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2381 * Note that the region to copy has already been clipped so we know we
2382 * won't read from outside the source renderbuffer's bounds.
2384 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2387 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2388 struct gl_texture_image
*texImage
,
2389 GLint destX
, GLint destY
, GLint slice
,
2390 struct gl_renderbuffer
*rb
,
2391 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2393 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2394 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2395 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2396 struct st_context
*st
= st_context(ctx
);
2397 struct pipe_context
*pipe
= st
->pipe
;
2398 struct pipe_screen
*screen
= pipe
->screen
;
2399 struct pipe_blit_info blit
;
2400 enum pipe_format dst_format
;
2401 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2405 st_flush_bitmap_cache(st
);
2406 st_invalidate_readpix_cache(st
);
2408 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2409 !_mesa_is_format_astc_2d(texImage
->TexFormat
) &&
2410 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2412 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2413 debug_printf("%s: null strb or stImage\n", __func__
);
2417 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2418 texImage
->TexFormat
)) {
2422 if (!st_can_copyteximage_using_blit(texImage
, rb
)) {
2426 /* Choose the destination format to match the TexImage behavior. */
2427 dst_format
= util_format_linear(stImage
->pt
->format
);
2428 dst_format
= util_format_luminance_to_red(dst_format
);
2429 dst_format
= util_format_intensity_to_red(dst_format
);
2431 /* See if the destination format is supported. */
2432 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2433 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2434 bind
= PIPE_BIND_DEPTH_STENCIL
;
2437 bind
= PIPE_BIND_RENDER_TARGET
;
2441 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2442 stImage
->pt
->nr_samples
,
2443 stImage
->pt
->nr_storage_samples
, bind
)) {
2447 /* Y flipping for the main framebuffer. */
2449 srcY1
= strb
->Base
.Height
- srcY
- height
;
2450 srcY0
= srcY1
+ height
;
2454 srcY1
= srcY0
+ height
;
2457 /* Blit the texture.
2458 * This supports flipping, format conversions, and downsampling.
2460 memset(&blit
, 0, sizeof(blit
));
2461 blit
.src
.resource
= strb
->texture
;
2462 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2463 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2464 blit
.src
.box
.x
= srcX
;
2465 blit
.src
.box
.y
= srcY0
;
2466 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2467 blit
.src
.box
.width
= width
;
2468 blit
.src
.box
.height
= srcY1
- srcY0
;
2469 blit
.src
.box
.depth
= 1;
2470 blit
.dst
.resource
= stImage
->pt
;
2471 blit
.dst
.format
= dst_format
;
2472 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2473 blit
.dst
.box
.x
= destX
;
2474 blit
.dst
.box
.y
= destY
;
2475 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2476 blit
.dst
.box
.width
= width
;
2477 blit
.dst
.box
.height
= height
;
2478 blit
.dst
.box
.depth
= 1;
2479 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2480 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2481 pipe
->blit(pipe
, &blit
);
2485 /* software fallback */
2486 fallback_copy_texsubimage(ctx
,
2487 strb
, stImage
, texImage
->_BaseFormat
,
2488 destX
, destY
, slice
,
2489 srcX
, srcY
, width
, height
);
2494 * Copy image data from stImage into the texture object 'stObj' at level
2498 copy_image_data_to_texture(struct st_context
*st
,
2499 struct st_texture_object
*stObj
,
2501 struct st_texture_image
*stImage
)
2505 const struct gl_texture_image MAYBE_UNUSED
*dstImage
=
2506 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2508 assert(dstImage
->Width
== stImage
->base
.Width
);
2509 assert(dstImage
->Height
== stImage
->base
.Height
);
2510 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2514 /* Copy potentially with the blitter:
2517 if (stImage
->pt
->last_level
== 0)
2520 src_level
= stImage
->base
.Level
;
2522 assert(src_level
<= stImage
->pt
->last_level
);
2523 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2524 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2525 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2526 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2527 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2528 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2530 st_texture_image_copy(st
->pipe
,
2531 stObj
->pt
, dstLevel
, /* dest texture, level */
2532 stImage
->pt
, src_level
, /* src texture, level */
2533 stImage
->base
.Face
);
2535 pipe_resource_reference(&stImage
->pt
, NULL
);
2537 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2542 * Called during state validation. When this function is finished,
2543 * the texture object should be ready for rendering.
2544 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2547 st_finalize_texture(struct gl_context
*ctx
,
2548 struct pipe_context
*pipe
,
2549 struct gl_texture_object
*tObj
,
2552 struct st_context
*st
= st_context(ctx
);
2553 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2554 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2556 const struct st_texture_image
*firstImage
;
2557 enum pipe_format firstImageFormat
;
2559 uint16_t ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2561 if (tObj
->Immutable
)
2564 if (tObj
->_MipmapComplete
)
2565 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2566 else if (tObj
->_BaseComplete
)
2567 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2569 /* Skip the loop over images in the common case of no images having
2570 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
2571 * haven't looked at, then we do need to look at those new images.
2573 if (!stObj
->needs_validation
&&
2574 stObj
->base
.BaseLevel
>= stObj
->validated_first_level
&&
2575 stObj
->lastLevel
<= stObj
->validated_last_level
) {
2579 /* If this texture comes from a window system, there is nothing else to do. */
2580 if (stObj
->surface_based
) {
2584 firstImage
= st_texture_image_const(stObj
->base
.Image
[cubeMapFace
][stObj
->base
.BaseLevel
]);
2587 /* If both firstImage and stObj point to a texture which can contain
2588 * all active images, favour firstImage. Note that because of the
2589 * completeness requirement, we know that the image dimensions
2592 if (firstImage
->pt
&&
2593 firstImage
->pt
!= stObj
->pt
&&
2594 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2595 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2596 st_texture_release_all_sampler_views(st
, stObj
);
2599 /* Find gallium format for the Mesa texture */
2601 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2603 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2606 uint16_t height
, depth
;
2608 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2609 firstImage
->base
.Width2
,
2610 firstImage
->base
.Height2
,
2611 firstImage
->base
.Depth2
,
2612 &width
, &height
, &depth
, &ptLayers
);
2614 /* If we previously allocated a pipe texture and its sizes are
2615 * compatible, use them.
2618 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2619 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2620 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2621 ptWidth
= stObj
->pt
->width0
;
2622 ptHeight
= stObj
->pt
->height0
;
2623 ptDepth
= stObj
->pt
->depth0
;
2625 /* Otherwise, compute a new level=0 size that is compatible with the
2628 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2629 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2630 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2632 /* If the base level image is 1x1x1, we still need to ensure that the
2633 * resulting pipe texture ends up with the required number of levels
2636 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2637 ptWidth
<<= firstImage
->base
.Level
;
2639 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2640 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2644 /* At this point, the texture may be incomplete (mismatched cube
2645 * face sizes, for example). If that's the case, give up, but
2646 * don't return GL_FALSE as that would raise an incorrect
2647 * GL_OUT_OF_MEMORY error. See Piglit fbo-incomplete-texture-03 test.
2649 if (!stObj
->base
._BaseComplete
) {
2650 _mesa_test_texobj_completeness(ctx
, &stObj
->base
);
2651 if (!stObj
->base
._BaseComplete
) {
2657 ptNumSamples
= firstImage
->base
.NumSamples
;
2660 /* If we already have a gallium texture, check that it matches the texture
2661 * object's format, target, size, num_levels, etc.
2664 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2665 stObj
->pt
->format
!= firstImageFormat
||
2666 stObj
->pt
->last_level
< stObj
->lastLevel
||
2667 stObj
->pt
->width0
!= ptWidth
||
2668 stObj
->pt
->height0
!= ptHeight
||
2669 stObj
->pt
->depth0
!= ptDepth
||
2670 stObj
->pt
->nr_samples
!= ptNumSamples
||
2671 stObj
->pt
->array_size
!= ptLayers
)
2673 /* The gallium texture does not match the Mesa texture so delete the
2674 * gallium texture now. We'll make a new one below.
2676 pipe_resource_reference(&stObj
->pt
, NULL
);
2677 st_texture_release_all_sampler_views(st
, stObj
);
2678 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
2682 /* May need to create a new gallium texture:
2685 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2687 stObj
->pt
= st_texture_create(st
,
2688 gl_target_to_pipe(stObj
->base
.Target
),
2694 ptLayers
, ptNumSamples
,
2698 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2703 /* Pull in any images not in the object's texture:
2705 for (face
= 0; face
< nr_faces
; face
++) {
2707 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2708 struct st_texture_image
*stImage
=
2709 st_texture_image(stObj
->base
.Image
[face
][level
]);
2711 /* Need to import images in main memory or held in other textures.
2713 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2717 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2718 height
= u_minify(ptHeight
, level
);
2722 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2723 depth
= u_minify(ptDepth
, level
);
2724 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2730 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2731 stImage
->base
.Height
== height
&&
2732 stImage
->base
.Depth
== depth
)) {
2733 /* src image fits expected dest mipmap level size */
2734 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2740 stObj
->validated_first_level
= stObj
->base
.BaseLevel
;
2741 stObj
->validated_last_level
= stObj
->lastLevel
;
2742 stObj
->needs_validation
= false;
2748 * Allocate a new pipe_resource object
2749 * width0, height0, depth0 are the dimensions of the level 0 image
2750 * (the highest resolution). last_level indicates how many mipmap levels
2751 * to allocate storage for. For non-mipmapped textures, this will be zero.
2753 static struct pipe_resource
*
2754 st_texture_create_from_memory(struct st_context
*st
,
2755 struct st_memory_object
*memObj
,
2757 enum pipe_texture_target target
,
2758 enum pipe_format format
,
2767 struct pipe_resource pt
, *newtex
;
2768 struct pipe_screen
*screen
= st
->pipe
->screen
;
2770 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
2772 assert(height0
> 0);
2774 if (target
== PIPE_TEXTURE_CUBE
)
2775 assert(layers
== 6);
2777 DBG("%s target %d format %s last_level %d\n", __func__
,
2778 (int) target
, util_format_name(format
), last_level
);
2781 assert(screen
->is_format_supported(screen
, format
, target
, 0, 0,
2782 PIPE_BIND_SAMPLER_VIEW
));
2784 memset(&pt
, 0, sizeof(pt
));
2787 pt
.last_level
= last_level
;
2789 pt
.height0
= height0
;
2791 pt
.array_size
= layers
;
2792 pt
.usage
= PIPE_USAGE_DEFAULT
;
2794 /* only set this for OpenGL textures, not renderbuffers */
2795 pt
.flags
= PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
;
2796 pt
.nr_samples
= nr_samples
;
2797 pt
.nr_storage_samples
= nr_samples
;
2799 newtex
= screen
->resource_from_memobj(screen
, &pt
, memObj
->memory
, offset
);
2801 assert(!newtex
|| pipe_is_referenced(&newtex
->reference
));
2807 * Allocate texture memory for a whole mipmap stack.
2808 * Note: for multisample textures if the requested sample count is not
2809 * supported, we search for the next higher supported sample count.
2812 st_texture_storage(struct gl_context
*ctx
,
2813 struct gl_texture_object
*texObj
,
2814 GLsizei levels
, GLsizei width
,
2815 GLsizei height
, GLsizei depth
,
2816 struct gl_memory_object
*memObj
,
2819 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2820 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2821 struct st_context
*st
= st_context(ctx
);
2822 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2823 struct st_memory_object
*smObj
= st_memory_object(memObj
);
2824 struct pipe_screen
*screen
= st
->pipe
->screen
;
2825 unsigned ptWidth
, bindings
;
2826 uint16_t ptHeight
, ptDepth
, ptLayers
;
2827 enum pipe_format fmt
;
2829 GLuint num_samples
= texImage
->NumSamples
;
2833 stObj
->lastLevel
= levels
- 1;
2835 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2837 bindings
= default_bindings(st
, fmt
);
2839 if (num_samples
> 0) {
2840 /* Find msaa sample count which is actually supported. For example,
2841 * if the user requests 1x but only 4x or 8x msaa is supported, we'll
2844 enum pipe_texture_target ptarget
= gl_target_to_pipe(texObj
->Target
);
2845 boolean found
= FALSE
;
2847 if (ctx
->Const
.MaxSamples
> 1 && num_samples
== 1) {
2848 /* don't try num_samples = 1 with drivers that support real msaa */
2852 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2853 if (screen
->is_format_supported(screen
, fmt
, ptarget
,
2854 num_samples
, num_samples
,
2855 PIPE_BIND_SAMPLER_VIEW
)) {
2856 /* Update the sample count in gl_texture_image as well. */
2857 texImage
->NumSamples
= num_samples
;
2868 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2869 width
, height
, depth
,
2870 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2873 stObj
->pt
= st_texture_create_from_memory(st
,
2876 gl_target_to_pipe(texObj
->Target
),
2882 ptLayers
, num_samples
,
2886 stObj
->pt
= st_texture_create(st
,
2887 gl_target_to_pipe(texObj
->Target
),
2893 ptLayers
, num_samples
,
2900 /* Set image resource pointers */
2901 for (level
= 0; level
< levels
; level
++) {
2903 for (face
= 0; face
< numFaces
; face
++) {
2904 struct st_texture_image
*stImage
=
2905 st_texture_image(texObj
->Image
[face
][level
]);
2906 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2908 compressed_tex_fallback_allocate(st
, stImage
);
2912 /* The texture is in a validated state, so no need to check later. */
2913 stObj
->needs_validation
= false;
2914 stObj
->validated_first_level
= 0;
2915 stObj
->validated_last_level
= levels
- 1;
2921 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2922 * for a whole mipmap stack.
2925 st_AllocTextureStorage(struct gl_context
*ctx
,
2926 struct gl_texture_object
*texObj
,
2927 GLsizei levels
, GLsizei width
,
2928 GLsizei height
, GLsizei depth
)
2930 return st_texture_storage(ctx
, texObj
, levels
,
2931 width
, height
, depth
,
2937 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2938 GLuint numLevels
, GLint level
,
2939 mesa_format format
, GLuint numSamples
,
2940 GLint width
, GLint height
, GLint depth
)
2942 struct st_context
*st
= st_context(ctx
);
2943 struct pipe_context
*pipe
= st
->pipe
;
2945 if (width
== 0 || height
== 0 || depth
== 0) {
2946 /* zero-sized images are legal, and always fit! */
2950 if (pipe
->screen
->can_create_resource
) {
2951 /* Ask the gallium driver if the texture is too large */
2952 struct gl_texture_object
*texObj
=
2953 _mesa_get_current_tex_object(ctx
, target
);
2954 struct pipe_resource pt
;
2956 /* Setup the pipe_resource object
2958 memset(&pt
, 0, sizeof(pt
));
2960 pt
.target
= gl_target_to_pipe(target
);
2961 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
2962 pt
.nr_samples
= numSamples
;
2963 pt
.nr_storage_samples
= numSamples
;
2965 st_gl_texture_dims_to_pipe_dims(target
,
2966 width
, height
, depth
,
2967 &pt
.width0
, &pt
.height0
,
2968 &pt
.depth0
, &pt
.array_size
);
2970 if (numLevels
> 0) {
2971 /* For immutable textures we know the final number of mip levels */
2972 pt
.last_level
= numLevels
- 1;
2974 else if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
2975 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
2976 /* assume just one mipmap level */
2980 /* assume a full set of mipmaps */
2981 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
2984 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
2987 /* Use core Mesa fallback */
2988 return _mesa_test_proxy_teximage(ctx
, target
, numLevels
, level
, format
,
2989 numSamples
, width
, height
, depth
);
2994 st_TextureView(struct gl_context
*ctx
,
2995 struct gl_texture_object
*texObj
,
2996 struct gl_texture_object
*origTexObj
)
2998 struct st_context
*st
= st_context(ctx
);
2999 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
3000 struct st_texture_object
*tex
= st_texture_object(texObj
);
3001 struct gl_texture_image
*image
= texObj
->Image
[0][0];
3003 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
3004 const int numLevels
= texObj
->NumLevels
;
3009 pipe_resource_reference(&tex
->pt
, orig
->pt
);
3011 /* Set image resource pointers */
3012 for (level
= 0; level
< numLevels
; level
++) {
3013 for (face
= 0; face
< numFaces
; face
++) {
3014 struct st_texture_image
*stImage
=
3015 st_texture_image(texObj
->Image
[face
][level
]);
3016 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
3020 tex
->surface_based
= GL_TRUE
;
3021 tex
->surface_format
=
3022 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
3024 tex
->lastLevel
= numLevels
- 1;
3026 /* free texture sampler views. They need to be recreated when we
3027 * change the texture view parameters.
3029 st_texture_release_all_sampler_views(st
, tex
);
3031 /* The texture is in a validated state, so no need to check later. */
3032 tex
->needs_validation
= false;
3033 tex
->validated_first_level
= 0;
3034 tex
->validated_last_level
= numLevels
- 1;
3041 * Find the mipmap level in 'pt' which matches the level described by
3045 find_mipmap_level(const struct gl_texture_image
*texImage
,
3046 const struct pipe_resource
*pt
)
3048 const GLenum target
= texImage
->TexObject
->Target
;
3049 GLint texWidth
= texImage
->Width
;
3050 GLint texHeight
= texImage
->Height
;
3051 GLint texDepth
= texImage
->Depth
;
3053 uint16_t h
, d
, layers
;
3055 st_gl_texture_dims_to_pipe_dims(target
, texWidth
, texHeight
, texDepth
,
3056 &w
, &h
, &d
, &layers
);
3058 for (level
= 0; level
<= pt
->last_level
; level
++) {
3059 if (u_minify(pt
->width0
, level
) == w
&&
3060 u_minify(pt
->height0
, level
) == h
&&
3061 u_minify(pt
->depth0
, level
) == d
) {
3066 /* If we get here, there must be some sort of inconsistency between
3067 * the Mesa texture object/images and the gallium resource.
3069 debug_printf("Inconsistent textures in find_mipmap_level()\n");
3071 return texImage
->Level
;
3076 st_ClearTexSubImage(struct gl_context
*ctx
,
3077 struct gl_texture_image
*texImage
,
3078 GLint xoffset
, GLint yoffset
, GLint zoffset
,
3079 GLsizei width
, GLsizei height
, GLsizei depth
,
3080 const void *clearValue
)
3082 static const char zeros
[16] = {0};
3083 struct gl_texture_object
*texObj
= texImage
->TexObject
;
3084 struct st_texture_image
*stImage
= st_texture_image(texImage
);
3085 struct pipe_resource
*pt
= stImage
->pt
;
3086 struct st_context
*st
= st_context(ctx
);
3087 struct pipe_context
*pipe
= st
->pipe
;
3089 struct pipe_box box
;
3094 st_flush_bitmap_cache(st
);
3095 st_invalidate_readpix_cache(st
);
3097 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
3098 width
, height
, depth
, &box
);
3099 if (texObj
->Immutable
) {
3100 /* The texture object has to be consistent (no "loose", per-image
3101 * gallium resources). If this texture is a view into another
3102 * texture, we have to apply the MinLevel/Layer offsets. If this is
3103 * not a texture view, the offsets will be zero.
3105 assert(stImage
->pt
== st_texture_object(texObj
)->pt
);
3106 level
= texImage
->Level
+ texObj
->MinLevel
;
3107 box
.z
+= texObj
->MinLayer
;
3110 /* Texture level sizes may be inconsistent. We my have "loose",
3111 * per-image gallium resources. The texImage->Level may not match
3112 * the gallium resource texture level.
3114 level
= find_mipmap_level(texImage
, pt
);
3117 assert(level
<= pt
->last_level
);
3119 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
3124 * Called via the glTexParam*() function, but only when some texture object
3125 * state has actually changed.
3128 st_TexParameter(struct gl_context
*ctx
,
3129 struct gl_texture_object
*texObj
, GLenum pname
)
3131 struct st_context
*st
= st_context(ctx
);
3132 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3135 case GL_TEXTURE_BASE_LEVEL
:
3136 case GL_TEXTURE_MAX_LEVEL
:
3137 case GL_DEPTH_TEXTURE_MODE
:
3138 case GL_DEPTH_STENCIL_TEXTURE_MODE
:
3139 case GL_TEXTURE_SRGB_DECODE_EXT
:
3140 case GL_TEXTURE_SWIZZLE_R
:
3141 case GL_TEXTURE_SWIZZLE_G
:
3142 case GL_TEXTURE_SWIZZLE_B
:
3143 case GL_TEXTURE_SWIZZLE_A
:
3144 case GL_TEXTURE_SWIZZLE_RGBA
:
3145 case GL_TEXTURE_BUFFER_SIZE
:
3146 case GL_TEXTURE_BUFFER_OFFSET
:
3147 /* changing any of these texture parameters means we must create
3148 * new sampler views.
3150 st_texture_release_all_sampler_views(st
, stObj
);
3158 st_SetTextureStorageForMemoryObject(struct gl_context
*ctx
,
3159 struct gl_texture_object
*texObj
,
3160 struct gl_memory_object
*memObj
,
3161 GLsizei levels
, GLsizei width
,
3162 GLsizei height
, GLsizei depth
,
3165 return st_texture_storage(ctx
, texObj
, levels
,
3166 width
, height
, depth
,
3171 st_NewTextureHandle(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
3172 struct gl_sampler_object
*sampObj
)
3174 struct st_context
*st
= st_context(ctx
);
3175 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3176 struct pipe_context
*pipe
= st
->pipe
;
3177 struct pipe_sampler_view
*view
;
3178 struct pipe_sampler_state sampler
= {0};
3180 if (texObj
->Target
!= GL_TEXTURE_BUFFER
) {
3181 if (!st_finalize_texture(ctx
, pipe
, texObj
, 0))
3184 st_convert_sampler(st
, texObj
, sampObj
, 0, &sampler
);
3186 /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
3187 view
= st_get_texture_sampler_view_from_stobj(st
, stObj
, sampObj
, 0, true);
3189 view
= st_get_buffer_sampler_view_from_stobj(st
, stObj
);
3192 return pipe
->create_texture_handle(pipe
, view
, &sampler
);
3197 st_DeleteTextureHandle(struct gl_context
*ctx
, GLuint64 handle
)
3199 struct st_context
*st
= st_context(ctx
);
3200 struct pipe_context
*pipe
= st
->pipe
;
3202 pipe
->delete_texture_handle(pipe
, handle
);
3207 st_MakeTextureHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3210 struct st_context
*st
= st_context(ctx
);
3211 struct pipe_context
*pipe
= st
->pipe
;
3213 pipe
->make_texture_handle_resident(pipe
, handle
, resident
);
3218 st_NewImageHandle(struct gl_context
*ctx
, struct gl_image_unit
*imgObj
)
3220 struct st_context
*st
= st_context(ctx
);
3221 struct pipe_context
*pipe
= st
->pipe
;
3222 struct pipe_image_view image
;
3224 st_convert_image(st
, imgObj
, &image
);
3226 return pipe
->create_image_handle(pipe
, &image
);
3231 st_DeleteImageHandle(struct gl_context
*ctx
, GLuint64 handle
)
3233 struct st_context
*st
= st_context(ctx
);
3234 struct pipe_context
*pipe
= st
->pipe
;
3236 pipe
->delete_image_handle(pipe
, handle
);
3241 st_MakeImageHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3242 GLenum access
, bool resident
)
3244 struct st_context
*st
= st_context(ctx
);
3245 struct pipe_context
*pipe
= st
->pipe
;
3247 pipe
->make_image_handle_resident(pipe
, handle
, access
, resident
);
3252 st_init_texture_functions(struct dd_function_table
*functions
)
3254 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
3255 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
3256 functions
->TexImage
= st_TexImage
;
3257 functions
->TexSubImage
= st_TexSubImage
;
3258 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
3259 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
3260 functions
->GenerateMipmap
= st_generate_mipmap
;
3262 functions
->GetTexSubImage
= st_GetTexSubImage
;
3264 /* compressed texture functions */
3265 functions
->CompressedTexImage
= st_CompressedTexImage
;
3267 functions
->NewTextureObject
= st_NewTextureObject
;
3268 functions
->NewTextureImage
= st_NewTextureImage
;
3269 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
3270 functions
->DeleteTexture
= st_DeleteTextureObject
;
3271 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
3272 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
3273 functions
->MapTextureImage
= st_MapTextureImage
;
3274 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
3276 /* XXX Temporary until we can query pipe's texture sizes */
3277 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
3279 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
3280 functions
->TextureView
= st_TextureView
;
3281 functions
->ClearTexSubImage
= st_ClearTexSubImage
;
3283 functions
->TexParameter
= st_TexParameter
;
3285 /* bindless functions */
3286 functions
->NewTextureHandle
= st_NewTextureHandle
;
3287 functions
->DeleteTextureHandle
= st_DeleteTextureHandle
;
3288 functions
->MakeTextureHandleResident
= st_MakeTextureHandleResident
;
3289 functions
->NewImageHandle
= st_NewImageHandle
;
3290 functions
->DeleteImageHandle
= st_DeleteImageHandle
;
3291 functions
->MakeImageHandleResident
= st_MakeImageHandleResident
;
3293 /* external object functions */
3294 functions
->SetTextureStorageForMemoryObject
= st_SetTextureStorageForMemoryObject
;