1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 #include "main/bufferobj.h"
30 #include "main/enums.h"
31 #include "main/fbobject.h"
32 #include "main/formats.h"
33 #include "main/format_utils.h"
34 #include "main/glformats.h"
35 #include "main/image.h"
36 #include "main/imports.h"
37 #include "main/macros.h"
38 #include "main/mipmap.h"
39 #include "main/pack.h"
41 #include "main/pixeltransfer.h"
42 #include "main/texcompress.h"
43 #include "main/texcompress_etc.h"
44 #include "main/texgetimage.h"
45 #include "main/teximage.h"
46 #include "main/texobj.h"
47 #include "main/texstore.h"
49 #include "state_tracker/st_debug.h"
50 #include "state_tracker/st_context.h"
51 #include "state_tracker/st_cb_bitmap.h"
52 #include "state_tracker/st_cb_fbo.h"
53 #include "state_tracker/st_cb_flush.h"
54 #include "state_tracker/st_cb_texture.h"
55 #include "state_tracker/st_cb_bufferobjects.h"
56 #include "state_tracker/st_cb_memoryobjects.h"
57 #include "state_tracker/st_format.h"
58 #include "state_tracker/st_pbo.h"
59 #include "state_tracker/st_texture.h"
60 #include "state_tracker/st_gen_mipmap.h"
61 #include "state_tracker/st_atom.h"
62 #include "state_tracker/st_sampler_view.h"
64 #include "pipe/p_context.h"
65 #include "pipe/p_defines.h"
66 #include "util/u_inlines.h"
67 #include "util/u_upload_mgr.h"
68 #include "pipe/p_shader_tokens.h"
69 #include "util/u_tile.h"
70 #include "util/u_format.h"
71 #include "util/u_surface.h"
72 #include "util/u_sampler.h"
73 #include "util/u_math.h"
74 #include "util/u_box.h"
75 #include "util/u_simple_shaders.h"
76 #include "cso_cache/cso_context.h"
77 #include "tgsi/tgsi_ureg.h"
79 #define DBG if (0) printf
82 enum pipe_texture_target
83 gl_target_to_pipe(GLenum target
)
87 case GL_PROXY_TEXTURE_1D
:
88 return PIPE_TEXTURE_1D
;
90 case GL_PROXY_TEXTURE_2D
:
91 case GL_TEXTURE_EXTERNAL_OES
:
92 case GL_TEXTURE_2D_MULTISAMPLE
:
93 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
94 return PIPE_TEXTURE_2D
;
95 case GL_TEXTURE_RECTANGLE_NV
:
96 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
97 return PIPE_TEXTURE_RECT
;
99 case GL_PROXY_TEXTURE_3D
:
100 return PIPE_TEXTURE_3D
;
101 case GL_TEXTURE_CUBE_MAP_ARB
:
102 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
103 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
104 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
105 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
106 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
107 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
108 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
109 return PIPE_TEXTURE_CUBE
;
110 case GL_TEXTURE_1D_ARRAY_EXT
:
111 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
112 return PIPE_TEXTURE_1D_ARRAY
;
113 case GL_TEXTURE_2D_ARRAY_EXT
:
114 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
115 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
116 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
117 return PIPE_TEXTURE_2D_ARRAY
;
118 case GL_TEXTURE_BUFFER
:
120 case GL_TEXTURE_CUBE_MAP_ARRAY
:
121 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
122 return PIPE_TEXTURE_CUBE_ARRAY
;
130 /** called via ctx->Driver.NewTextureImage() */
131 static struct gl_texture_image
*
132 st_NewTextureImage(struct gl_context
* ctx
)
134 DBG("%s\n", __func__
);
136 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
140 /** called via ctx->Driver.DeleteTextureImage() */
142 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
144 /* nothing special (yet) for st_texture_image */
145 _mesa_delete_texture_image(ctx
, img
);
149 /** called via ctx->Driver.NewTextureObject() */
150 static struct gl_texture_object
*
151 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
153 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
155 DBG("%s\n", __func__
);
156 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
158 obj
->needs_validation
= true;
163 /** called via ctx->Driver.DeleteTextureObject() */
165 st_DeleteTextureObject(struct gl_context
*ctx
,
166 struct gl_texture_object
*texObj
)
168 struct st_context
*st
= st_context(ctx
);
169 struct st_texture_object
*stObj
= st_texture_object(texObj
);
171 pipe_resource_reference(&stObj
->pt
, NULL
);
172 st_texture_release_all_sampler_views(st
, stObj
);
173 st_texture_free_sampler_views(stObj
);
174 _mesa_delete_texture_object(ctx
, texObj
);
178 /** called via ctx->Driver.FreeTextureImageBuffer() */
180 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
181 struct gl_texture_image
*texImage
)
183 struct st_context
*st
= st_context(ctx
);
184 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
185 struct st_texture_image
*stImage
= st_texture_image(texImage
);
187 DBG("%s\n", __func__
);
190 pipe_resource_reference(&stImage
->pt
, NULL
);
193 free(stImage
->transfer
);
194 stImage
->transfer
= NULL
;
195 stImage
->num_transfers
= 0;
197 if (stImage
->etc_data
) {
198 free(stImage
->etc_data
);
199 stImage
->etc_data
= NULL
;
202 /* if the texture image is being deallocated, the structure of the
203 * texture is changing so we'll likely need a new sampler view.
205 st_texture_release_all_sampler_views(st
, stObj
);
209 st_etc_fallback(struct st_context
*st
, struct gl_texture_image
*texImage
)
211 return (_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
212 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
);
216 etc_fallback_allocate(struct st_context
*st
, struct st_texture_image
*stImage
)
218 struct gl_texture_image
*texImage
= &stImage
->base
;
220 if (!st_etc_fallback(st
, texImage
))
223 if (stImage
->etc_data
)
224 free(stImage
->etc_data
);
226 unsigned data_size
= _mesa_format_image_size(texImage
->TexFormat
,
232 malloc(data_size
* _mesa_num_tex_faces(texImage
->TexObject
->Target
));
235 /** called via ctx->Driver.MapTextureImage() */
237 st_MapTextureImage(struct gl_context
*ctx
,
238 struct gl_texture_image
*texImage
,
239 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
241 GLubyte
**mapOut
, GLint
*rowStrideOut
)
243 struct st_context
*st
= st_context(ctx
);
244 struct st_texture_image
*stImage
= st_texture_image(texImage
);
247 struct pipe_transfer
*transfer
;
250 if (mode
& GL_MAP_READ_BIT
)
251 pipeMode
|= PIPE_TRANSFER_READ
;
252 if (mode
& GL_MAP_WRITE_BIT
)
253 pipeMode
|= PIPE_TRANSFER_WRITE
;
254 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
255 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
257 map
= st_texture_image_map(st
, stImage
, pipeMode
, x
, y
, slice
, w
, h
, 1,
260 if (st_etc_fallback(st
, texImage
)) {
261 /* ETC isn't supported by all gallium drivers, where it's represented
262 * by uncompressed formats. We store the compressed data (as it's
263 * needed for image copies in OES_copy_image), and decompress as
264 * necessary in Unmap.
266 * Note: all ETC1/ETC2 formats have 4x4 block sizes.
268 unsigned z
= transfer
->box
.z
;
269 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
271 unsigned bytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
272 unsigned stride
= *rowStrideOut
= itransfer
->temp_stride
=
273 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width2
);
274 *mapOut
= itransfer
->temp_data
=
275 stImage
->etc_data
+ ((x
/ 4) * bytes
+ (y
/ 4) * stride
) +
276 z
* stride
* texImage
->Height2
/ 4;
277 itransfer
->map
= map
;
280 /* supported mapping */
282 *rowStrideOut
= transfer
->stride
;
292 /** called via ctx->Driver.UnmapTextureImage() */
294 st_UnmapTextureImage(struct gl_context
*ctx
,
295 struct gl_texture_image
*texImage
,
298 struct st_context
*st
= st_context(ctx
);
299 struct st_texture_image
*stImage
= st_texture_image(texImage
);
301 if (st_etc_fallback(st
, texImage
)) {
302 /* Decompress the ETC texture to the mapped one. */
303 unsigned z
= slice
+ stImage
->base
.Face
;
304 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
305 struct pipe_transfer
*transfer
= itransfer
->transfer
;
307 assert(z
== transfer
->box
.z
);
309 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
310 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
311 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
312 itransfer
->temp_data
,
313 itransfer
->temp_stride
,
314 transfer
->box
.width
, transfer
->box
.height
);
317 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
318 itransfer
->temp_data
, itransfer
->temp_stride
,
319 transfer
->box
.width
, transfer
->box
.height
,
320 texImage
->TexFormat
);
324 itransfer
->temp_data
= NULL
;
325 itransfer
->temp_stride
= 0;
329 st_texture_image_unmap(st
, stImage
, slice
);
334 * Return default texture resource binding bitmask for the given format.
337 default_bindings(struct st_context
*st
, enum pipe_format format
)
339 struct pipe_screen
*screen
= st
->pipe
->screen
;
340 const unsigned target
= PIPE_TEXTURE_2D
;
343 if (util_format_is_depth_or_stencil(format
))
344 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
346 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
348 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
352 format
= util_format_linear(format
);
354 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
357 return PIPE_BIND_SAMPLER_VIEW
;
363 * Given the size of a mipmap image, try to compute the size of the level=0
366 * Note that this isn't always accurate for odd-sized, non-POW textures.
367 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
369 * \return GL_TRUE for success, GL_FALSE for failure
372 guess_base_level_size(GLenum target
,
373 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
374 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
381 /* Guess the size of the base level.
382 * Depending on the image's size, we can't always make a guess here.
386 case GL_TEXTURE_1D_ARRAY
:
391 case GL_TEXTURE_2D_ARRAY
:
392 /* We can't make a good guess here, because the base level dimensions
395 if (width
== 1 || height
== 1) {
402 case GL_TEXTURE_CUBE_MAP
:
403 case GL_TEXTURE_CUBE_MAP_ARRAY
:
409 /* We can't make a good guess here, because the base level dimensions
412 if (width
== 1 || height
== 1 || depth
== 1) {
420 case GL_TEXTURE_RECTANGLE
:
437 * Try to determine whether we should allocate memory for a full texture
438 * mipmap. The problem is when we get a glTexImage(level=0) call, we
439 * can't immediately know if other mipmap levels are coming next. Here
440 * we try to guess whether to allocate memory for a mipmap or just the
443 * If we guess incorrectly here we'll later reallocate the right amount of
444 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
446 * \param stObj the texture object we're going to allocate memory for.
447 * \param stImage describes the incoming image which we need to store.
450 allocate_full_mipmap(const struct st_texture_object
*stObj
,
451 const struct st_texture_image
*stImage
)
453 switch (stObj
->base
.Target
) {
454 case GL_TEXTURE_RECTANGLE_NV
:
455 case GL_TEXTURE_BUFFER
:
456 case GL_TEXTURE_EXTERNAL_OES
:
457 case GL_TEXTURE_2D_MULTISAMPLE
:
458 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
459 /* these texture types cannot be mipmapped */
463 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
466 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
467 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
468 /* depth/stencil textures are seldom mipmapped */
471 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
474 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
475 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
476 /* not a mipmap minification filter */
479 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
480 /* 3D textures are seldom mipmapped */
488 * Try to allocate a pipe_resource object for the given st_texture_object.
490 * We use the given st_texture_image as a clue to determine the size of the
491 * mipmap image at level=0.
493 * \return GL_TRUE for success, GL_FALSE if out of memory.
496 guess_and_alloc_texture(struct st_context
*st
,
497 struct st_texture_object
*stObj
,
498 const struct st_texture_image
*stImage
)
500 const struct gl_texture_image
*firstImage
;
501 GLuint lastLevel
, width
, height
, depth
;
504 uint16_t ptHeight
, ptDepth
, ptLayers
;
505 enum pipe_format fmt
;
506 bool guessed_box
= false;
508 DBG("%s\n", __func__
);
512 /* If a base level image with compatible size exists, use that as our guess.
514 firstImage
= _mesa_base_tex_image(&stObj
->base
);
516 firstImage
->Width2
> 0 &&
517 firstImage
->Height2
> 0 &&
518 firstImage
->Depth2
> 0 &&
519 guess_base_level_size(stObj
->base
.Target
,
524 &width
, &height
, &depth
)) {
525 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
526 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
527 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
532 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
533 stImage
->base
.Width2
,
534 stImage
->base
.Height2
,
535 stImage
->base
.Depth2
,
537 &width
, &height
, &depth
);
540 /* we can't determine the image size at level=0 */
541 /* this is not an out of memory error */
545 /* At this point, (width x height x depth) is the expected size of
546 * the level=0 mipmap image.
549 /* Guess a reasonable value for lastLevel. With OpenGL we have no
550 * idea how many mipmap levels will be in a texture until we start
551 * to render with it. Make an educated guess here but be prepared
552 * to re-allocating a texture buffer with space for more (or fewer)
553 * mipmap levels later.
555 if (allocate_full_mipmap(stObj
, stImage
)) {
556 /* alloc space for a full mipmap */
557 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
558 width
, height
, depth
) - 1;
561 /* only alloc space for a single mipmap level */
565 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
567 bindings
= default_bindings(st
, fmt
);
569 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
570 width
, height
, depth
,
571 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
573 stObj
->pt
= st_texture_create(st
,
574 gl_target_to_pipe(stObj
->base
.Target
),
583 stObj
->lastLevel
= lastLevel
;
585 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
587 return stObj
->pt
!= NULL
;
592 * Called via ctx->Driver.AllocTextureImageBuffer().
593 * If the texture object/buffer already has space for the indicated image,
594 * we're done. Otherwise, allocate memory for the new texture image.
597 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
598 struct gl_texture_image
*texImage
)
600 struct st_context
*st
= st_context(ctx
);
601 struct st_texture_image
*stImage
= st_texture_image(texImage
);
602 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
603 const GLuint level
= texImage
->Level
;
604 GLuint width
= texImage
->Width
;
605 GLuint height
= texImage
->Height
;
606 GLuint depth
= texImage
->Depth
;
608 DBG("%s\n", __func__
);
610 assert(!stImage
->pt
); /* xxx this might be wrong */
612 stObj
->needs_validation
= true;
614 etc_fallback_allocate(st
, stImage
);
616 /* Look if the parent texture object has space for this image */
618 level
<= stObj
->pt
->last_level
&&
619 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
620 /* this image will fit in the existing texture object's memory */
621 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
625 /* The parent texture object does not have space for this image */
627 pipe_resource_reference(&stObj
->pt
, NULL
);
628 st_texture_release_all_sampler_views(st
, stObj
);
630 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
631 /* Probably out of memory.
632 * Try flushing any pending rendering, then retry.
635 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
636 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
642 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
643 /* The image will live in the object's mipmap memory */
644 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
649 /* Create a new, temporary texture/resource/buffer to hold this
650 * one texture image. Note that when we later access this image
651 * (either for mapping or copying) we'll want to always specify
652 * mipmap level=0, even if the image represents some other mipmap
655 enum pipe_format format
=
656 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
657 GLuint bindings
= default_bindings(st
, format
);
659 uint16_t ptHeight
, ptDepth
, ptLayers
;
661 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
662 width
, height
, depth
,
663 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
665 stImage
->pt
= st_texture_create(st
,
666 gl_target_to_pipe(stObj
->base
.Target
),
674 return stImage
->pt
!= NULL
;
680 * Preparation prior to glTexImage. Basically check the 'surface_based'
681 * field and switch to a "normal" tex image if necessary.
684 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
685 GLenum format
, GLenum type
)
687 struct gl_texture_object
*texObj
= texImage
->TexObject
;
688 struct st_texture_object
*stObj
= st_texture_object(texObj
);
690 /* switch to "normal" */
691 if (stObj
->surface_based
) {
692 const GLenum target
= texObj
->Target
;
693 const GLuint level
= texImage
->Level
;
694 mesa_format texFormat
;
696 assert(!st_texture_image(texImage
)->pt
);
697 _mesa_clear_texture_object(ctx
, texObj
, texImage
);
698 stObj
->layer_override
= 0;
699 stObj
->level_override
= 0;
700 pipe_resource_reference(&stObj
->pt
, NULL
);
702 /* oops, need to init this image again */
703 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
704 texImage
->InternalFormat
, format
,
707 _mesa_init_teximage_fields(ctx
, texImage
,
708 texImage
->Width
, texImage
->Height
,
709 texImage
->Depth
, texImage
->Border
,
710 texImage
->InternalFormat
, texFormat
);
712 stObj
->surface_based
= GL_FALSE
;
718 * Return a writemask for the gallium blit. The parameters can be base
719 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
722 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
725 case GL_DEPTH_STENCIL
:
727 case GL_DEPTH_STENCIL
:
729 case GL_DEPTH_COMPONENT
:
731 case GL_STENCIL_INDEX
:
738 case GL_DEPTH_COMPONENT
:
740 case GL_DEPTH_STENCIL
:
741 case GL_DEPTH_COMPONENT
:
748 case GL_STENCIL_INDEX
:
750 case GL_STENCIL_INDEX
:
758 return PIPE_MASK_RGBA
;
763 * Converts format to a format with the same components, types
764 * and sizes, but with the components in RGBA order.
766 static enum pipe_format
767 unswizzle_format(enum pipe_format format
)
771 case PIPE_FORMAT_B8G8R8A8_UNORM
:
772 case PIPE_FORMAT_A8R8G8B8_UNORM
:
773 case PIPE_FORMAT_A8B8G8R8_UNORM
:
774 return PIPE_FORMAT_R8G8B8A8_UNORM
;
776 case PIPE_FORMAT_B10G10R10A2_UNORM
:
777 return PIPE_FORMAT_R10G10B10A2_UNORM
;
779 case PIPE_FORMAT_B10G10R10A2_SNORM
:
780 return PIPE_FORMAT_R10G10B10A2_SNORM
;
782 case PIPE_FORMAT_B10G10R10A2_UINT
:
783 return PIPE_FORMAT_R10G10B10A2_UINT
;
791 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
793 static enum pipe_format
794 alpha_to_red(enum pipe_format format
)
798 case PIPE_FORMAT_A8_UNORM
:
799 return PIPE_FORMAT_R8_UNORM
;
800 case PIPE_FORMAT_A8_SNORM
:
801 return PIPE_FORMAT_R8_SNORM
;
802 case PIPE_FORMAT_A8_UINT
:
803 return PIPE_FORMAT_R8_UINT
;
804 case PIPE_FORMAT_A8_SINT
:
805 return PIPE_FORMAT_R8_SINT
;
807 case PIPE_FORMAT_A16_UNORM
:
808 return PIPE_FORMAT_R16_UNORM
;
809 case PIPE_FORMAT_A16_SNORM
:
810 return PIPE_FORMAT_R16_SNORM
;
811 case PIPE_FORMAT_A16_UINT
:
812 return PIPE_FORMAT_R16_UINT
;
813 case PIPE_FORMAT_A16_SINT
:
814 return PIPE_FORMAT_R16_SINT
;
815 case PIPE_FORMAT_A16_FLOAT
:
816 return PIPE_FORMAT_R16_FLOAT
;
818 case PIPE_FORMAT_A32_UINT
:
819 return PIPE_FORMAT_R32_UINT
;
820 case PIPE_FORMAT_A32_SINT
:
821 return PIPE_FORMAT_R32_SINT
;
822 case PIPE_FORMAT_A32_FLOAT
:
823 return PIPE_FORMAT_R32_FLOAT
;
831 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
833 static enum pipe_format
834 red_alpha_to_red_green(enum pipe_format format
)
838 case PIPE_FORMAT_R8A8_UNORM
:
839 return PIPE_FORMAT_R8G8_UNORM
;
840 case PIPE_FORMAT_R8A8_SNORM
:
841 return PIPE_FORMAT_R8G8_SNORM
;
842 case PIPE_FORMAT_R8A8_UINT
:
843 return PIPE_FORMAT_R8G8_UINT
;
844 case PIPE_FORMAT_R8A8_SINT
:
845 return PIPE_FORMAT_R8G8_SINT
;
847 case PIPE_FORMAT_R16A16_UNORM
:
848 return PIPE_FORMAT_R16G16_UNORM
;
849 case PIPE_FORMAT_R16A16_SNORM
:
850 return PIPE_FORMAT_R16G16_SNORM
;
851 case PIPE_FORMAT_R16A16_UINT
:
852 return PIPE_FORMAT_R16G16_UINT
;
853 case PIPE_FORMAT_R16A16_SINT
:
854 return PIPE_FORMAT_R16G16_SINT
;
855 case PIPE_FORMAT_R16A16_FLOAT
:
856 return PIPE_FORMAT_R16G16_FLOAT
;
858 case PIPE_FORMAT_R32A32_UINT
:
859 return PIPE_FORMAT_R32G32_UINT
;
860 case PIPE_FORMAT_R32A32_SINT
:
861 return PIPE_FORMAT_R32G32_SINT
;
862 case PIPE_FORMAT_R32A32_FLOAT
:
863 return PIPE_FORMAT_R32G32_FLOAT
;
871 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
873 static enum pipe_format
874 luminance_alpha_to_red_green(enum pipe_format format
)
878 case PIPE_FORMAT_L8A8_UNORM
:
879 return PIPE_FORMAT_R8G8_UNORM
;
880 case PIPE_FORMAT_L8A8_SNORM
:
881 return PIPE_FORMAT_R8G8_SNORM
;
882 case PIPE_FORMAT_L8A8_UINT
:
883 return PIPE_FORMAT_R8G8_UINT
;
884 case PIPE_FORMAT_L8A8_SINT
:
885 return PIPE_FORMAT_R8G8_SINT
;
887 case PIPE_FORMAT_L16A16_UNORM
:
888 return PIPE_FORMAT_R16G16_UNORM
;
889 case PIPE_FORMAT_L16A16_SNORM
:
890 return PIPE_FORMAT_R16G16_SNORM
;
891 case PIPE_FORMAT_L16A16_UINT
:
892 return PIPE_FORMAT_R16G16_UINT
;
893 case PIPE_FORMAT_L16A16_SINT
:
894 return PIPE_FORMAT_R16G16_SINT
;
895 case PIPE_FORMAT_L16A16_FLOAT
:
896 return PIPE_FORMAT_R16G16_FLOAT
;
898 case PIPE_FORMAT_L32A32_UINT
:
899 return PIPE_FORMAT_R32G32_UINT
;
900 case PIPE_FORMAT_L32A32_SINT
:
901 return PIPE_FORMAT_R32G32_SINT
;
902 case PIPE_FORMAT_L32A32_FLOAT
:
903 return PIPE_FORMAT_R32G32_FLOAT
;
911 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
914 format_is_alpha(enum pipe_format format
)
916 const struct util_format_description
*desc
= util_format_description(format
);
918 if (desc
->nr_channels
== 1 &&
919 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
920 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
921 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
922 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
929 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
932 format_is_red(enum pipe_format format
)
934 const struct util_format_description
*desc
= util_format_description(format
);
936 if (desc
->nr_channels
== 1 &&
937 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
938 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
939 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
940 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
948 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
951 format_is_luminance(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_X
&&
957 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
958 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
959 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
966 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
969 format_is_red_alpha(enum pipe_format format
)
971 const struct util_format_description
*desc
= util_format_description(format
);
973 if (desc
->nr_channels
== 2 &&
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_Y
)
984 format_is_swizzled_rgba(enum pipe_format format
)
986 const struct util_format_description
*desc
= util_format_description(format
);
988 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
989 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
990 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
991 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
999 unsigned char swizzle
[4];
1000 enum pipe_format format
;
1003 static const struct format_table table_8888_unorm
[] = {
1004 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
1005 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
1006 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
1007 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
1010 static const struct format_table table_1010102_unorm
[] = {
1011 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
1012 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
1015 static const struct format_table table_1010102_snorm
[] = {
1016 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
1017 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
1020 static const struct format_table table_1010102_uint
[] = {
1021 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
1022 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
1025 static enum pipe_format
1026 swizzle_format(enum pipe_format format
, const int * const swizzle
)
1031 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1032 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1033 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1034 case PIPE_FORMAT_A8B8G8R8_UNORM
:
1035 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
1036 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
1037 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
1038 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
1039 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
1040 return table_8888_unorm
[i
].format
;
1044 case PIPE_FORMAT_R10G10B10A2_UNORM
:
1045 case PIPE_FORMAT_B10G10R10A2_UNORM
:
1046 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
1047 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
1048 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
1049 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
1050 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
1051 return table_1010102_unorm
[i
].format
;
1055 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1056 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1057 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1058 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1059 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1060 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1061 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1062 return table_1010102_snorm
[i
].format
;
1066 case PIPE_FORMAT_R10G10B10A2_UINT
:
1067 case PIPE_FORMAT_B10G10R10A2_UINT
:
1068 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1069 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1070 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1071 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1072 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1073 return table_1010102_uint
[i
].format
;
1081 return PIPE_FORMAT_NONE
;
1085 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1087 enum pipe_format src
= *src_format
;
1088 enum pipe_format dst
= *dst_format
;
1090 /* Note: dst_format has already been transformed from luminance/intensity
1091 * to red when this function is called. The source format will never
1092 * be an intensity format, because GL_INTENSITY is not a legal value
1093 * for the format parameter in glTex(Sub)Image(). */
1095 if (format_is_alpha(src
)) {
1096 if (!format_is_alpha(dst
))
1099 src
= alpha_to_red(src
);
1100 dst
= alpha_to_red(dst
);
1101 } else if (format_is_luminance(src
)) {
1102 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1105 src
= util_format_luminance_to_red(src
);
1106 } else if (util_format_is_luminance_alpha(src
)) {
1107 src
= luminance_alpha_to_red_green(src
);
1109 if (format_is_red_alpha(dst
)) {
1110 dst
= red_alpha_to_red_green(dst
);
1111 } else if (!format_is_red(dst
))
1113 } else if (format_is_swizzled_rgba(src
)) {
1114 const struct util_format_description
*src_desc
= util_format_description(src
);
1115 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1119 /* Make sure the format is an RGBA and not an RGBX format */
1120 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1123 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1126 for (i
= 0; i
< 4; i
++)
1127 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1129 dst
= swizzle_format(dst
, swizzle
);
1130 if (dst
== PIPE_FORMAT_NONE
)
1133 src
= unswizzle_format(src
);
1142 try_pbo_upload_common(struct gl_context
*ctx
,
1143 struct pipe_surface
*surface
,
1144 const struct st_pbo_addresses
*addr
,
1145 enum pipe_format src_format
)
1147 struct st_context
*st
= st_context(ctx
);
1148 struct cso_context
*cso
= st
->cso_context
;
1149 struct pipe_context
*pipe
= st
->pipe
;
1150 bool success
= false;
1153 fs
= st_pbo_get_upload_fs(st
, src_format
, surface
->format
);
1157 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1158 CSO_BIT_FRAGMENT_SAMPLERS
|
1159 CSO_BIT_VERTEX_ELEMENTS
|
1160 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1161 CSO_BIT_FRAMEBUFFER
|
1164 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1165 CSO_BIT_RASTERIZER
|
1166 CSO_BIT_STREAM_OUTPUTS
|
1167 CSO_BIT_PAUSE_QUERIES
|
1168 CSO_BIT_SAMPLE_MASK
|
1169 CSO_BIT_MIN_SAMPLES
|
1170 CSO_BIT_RENDER_CONDITION
|
1171 CSO_BITS_ALL_SHADERS
));
1172 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1174 cso_set_sample_mask(cso
, ~0);
1175 cso_set_min_samples(cso
, 1);
1176 cso_set_render_condition(cso
, NULL
, FALSE
, 0);
1178 /* Set up the sampler_view */
1180 struct pipe_sampler_view templ
;
1181 struct pipe_sampler_view
*sampler_view
;
1182 struct pipe_sampler_state sampler
= {0};
1183 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
1185 memset(&templ
, 0, sizeof(templ
));
1186 templ
.target
= PIPE_BUFFER
;
1187 templ
.format
= src_format
;
1188 templ
.u
.buf
.offset
= addr
->first_element
* addr
->bytes_per_pixel
;
1189 templ
.u
.buf
.size
= (addr
->last_element
- addr
->first_element
+ 1) *
1190 addr
->bytes_per_pixel
;
1191 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1192 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1193 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1194 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1196 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1197 if (sampler_view
== NULL
)
1200 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1202 pipe_sampler_view_reference(&sampler_view
, NULL
);
1204 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
1207 /* Framebuffer_state */
1209 struct pipe_framebuffer_state fb
;
1210 memset(&fb
, 0, sizeof(fb
));
1211 fb
.width
= surface
->width
;
1212 fb
.height
= surface
->height
;
1214 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1216 cso_set_framebuffer(cso
, &fb
);
1218 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1221 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1224 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1226 /* Depth/stencil/alpha state */
1228 struct pipe_depth_stencil_alpha_state dsa
;
1229 memset(&dsa
, 0, sizeof(dsa
));
1230 cso_set_depth_stencil_alpha(cso
, &dsa
);
1233 /* Set up the fragment shader */
1234 cso_set_fragment_shader_handle(cso
, fs
);
1236 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1239 cso_restore_state(cso
);
1240 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1246 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1247 struct gl_texture_image
*texImage
,
1248 GLenum format
, GLenum type
,
1249 enum pipe_format dst_format
,
1250 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1251 GLint width
, GLint height
, GLint depth
,
1253 const struct gl_pixelstore_attrib
*unpack
)
1255 struct st_context
*st
= st_context(ctx
);
1256 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1257 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1258 struct pipe_resource
*texture
= stImage
->pt
;
1259 struct pipe_context
*pipe
= st
->pipe
;
1260 struct pipe_screen
*screen
= pipe
->screen
;
1261 struct pipe_surface
*surface
= NULL
;
1262 struct st_pbo_addresses addr
;
1263 enum pipe_format src_format
;
1264 const struct util_format_description
*desc
;
1265 GLenum gl_target
= texImage
->TexObject
->Target
;
1268 if (!st
->pbo
.upload_enabled
)
1271 /* From now on, we need the gallium representation of dimensions. */
1272 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1279 if (depth
!= 1 && !st
->pbo
.layers
)
1282 /* Choose the source format. Initially, we do so without checking driver
1283 * support at all because of the remapping we later perform and because
1284 * at least the Radeon driver actually supports some formats for texture
1285 * buffers which it doesn't support for regular textures. */
1286 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1291 src_format
= util_format_linear(src_format
);
1292 desc
= util_format_description(src_format
);
1294 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1297 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1300 if (st
->pbo
.rgba_only
) {
1301 enum pipe_format orig_dst_format
= dst_format
;
1303 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1307 if (dst_format
!= orig_dst_format
&&
1308 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1309 PIPE_BIND_RENDER_TARGET
)) {
1315 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0,
1316 PIPE_BIND_SAMPLER_VIEW
)) {
1320 /* Compute buffer addresses */
1321 addr
.xoffset
= xoffset
;
1322 addr
.yoffset
= yoffset
;
1324 addr
.height
= height
;
1326 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1328 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1332 /* Set up the surface */
1334 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1335 unsigned max_layer
= util_max_layer(texture
, level
);
1337 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1339 struct pipe_surface templ
;
1340 memset(&templ
, 0, sizeof(templ
));
1341 templ
.format
= dst_format
;
1342 templ
.u
.tex
.level
= level
;
1343 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1344 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1346 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1351 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1353 pipe_surface_reference(&surface
, NULL
);
1359 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1360 struct gl_texture_image
*texImage
,
1361 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1362 GLint width
, GLint height
, GLint depth
,
1363 GLenum format
, GLenum type
, const void *pixels
,
1364 const struct gl_pixelstore_attrib
*unpack
)
1366 struct st_context
*st
= st_context(ctx
);
1367 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1368 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1369 struct pipe_context
*pipe
= st
->pipe
;
1370 struct pipe_screen
*screen
= pipe
->screen
;
1371 struct pipe_resource
*dst
= stImage
->pt
;
1372 struct pipe_resource
*src
= NULL
;
1373 struct pipe_resource src_templ
;
1374 struct pipe_transfer
*transfer
;
1375 struct pipe_blit_info blit
;
1376 enum pipe_format src_format
, dst_format
;
1377 mesa_format mesa_src_format
;
1378 GLenum gl_target
= texImage
->TexObject
->Target
;
1381 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1382 unsigned dst_level
= 0;
1384 st_flush_bitmap_cache(st
);
1385 st_invalidate_readpix_cache(st
);
1387 if (stObj
->pt
== stImage
->pt
)
1388 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1390 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1391 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1396 /* Try texture_subdata, which should be the fastest memcpy path. */
1398 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1399 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1400 texImage
->TexFormat
, format
, type
,
1402 struct pipe_box box
;
1403 unsigned stride
, layer_stride
;
1406 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1407 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1409 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1412 /* Convert to Gallium coordinates. */
1413 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1418 layer_stride
= stride
;
1421 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1422 pipe
->texture_subdata(pipe
, dst
, dst_level
, 0,
1423 &box
, data
, stride
, layer_stride
);
1427 if (!st
->prefer_blit_based_texture_transfer
) {
1431 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1432 * blit implementation in some drivers. */
1433 if (format
== GL_DEPTH_STENCIL
) {
1437 /* If the base internal format and the texture format don't match,
1438 * we can't use blit-based TexSubImage. */
1439 if (texImage
->_BaseFormat
!=
1440 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1445 /* See if the destination format is supported. */
1446 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1447 bind
= PIPE_BIND_DEPTH_STENCIL
;
1449 bind
= PIPE_BIND_RENDER_TARGET
;
1451 /* For luminance and intensity, only the red channel is stored
1452 * in the destination. */
1453 dst_format
= util_format_linear(dst
->format
);
1454 dst_format
= util_format_luminance_to_red(dst_format
);
1455 dst_format
= util_format_intensity_to_red(dst_format
);
1458 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1459 dst
->nr_samples
, bind
)) {
1463 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1464 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1465 xoffset
, yoffset
, zoffset
,
1466 width
, height
, depth
, pixels
, unpack
))
1470 /* See if the texture format already matches the format and type,
1471 * in which case the memcpy-based fast path will likely be used and
1472 * we don't have to blit. */
1473 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1474 type
, unpack
->SwapBytes
, NULL
)) {
1478 /* Choose the source format. */
1479 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1480 format
, type
, unpack
->SwapBytes
);
1485 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1487 /* There is no reason to do this if we cannot use memcpy for the temporary
1488 * source texture at least. This also takes transfer ops into account,
1490 if (!_mesa_texstore_can_use_memcpy(ctx
,
1491 _mesa_get_format_base_format(mesa_src_format
),
1492 mesa_src_format
, format
, type
, unpack
)) {
1496 /* TexSubImage only sets a single cubemap face. */
1497 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1498 gl_target
= GL_TEXTURE_2D
;
1500 /* TexSubImage can specify subsets of cube map array faces
1501 * so we need to upload via 2D array instead */
1502 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1503 gl_target
= GL_TEXTURE_2D_ARRAY
;
1506 /* Initialize the source texture description. */
1507 memset(&src_templ
, 0, sizeof(src_templ
));
1508 src_templ
.target
= gl_target_to_pipe(gl_target
);
1509 src_templ
.format
= src_format
;
1510 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1511 src_templ
.usage
= PIPE_USAGE_STAGING
;
1513 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1514 &src_templ
.width0
, &src_templ
.height0
,
1515 &src_templ
.depth0
, &src_templ
.array_size
);
1517 /* Check for NPOT texture support. */
1518 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1519 (!util_is_power_of_two(src_templ
.width0
) ||
1520 !util_is_power_of_two(src_templ
.height0
) ||
1521 !util_is_power_of_two(src_templ
.depth0
))) {
1525 /* Create the source texture. */
1526 src
= screen
->resource_create(screen
, &src_templ
);
1531 /* Map source pixels. */
1532 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1533 format
, type
, pixels
, unpack
,
1536 /* This is a GL error. */
1537 pipe_resource_reference(&src
, NULL
);
1541 /* From now on, we need the gallium representation of dimensions. */
1542 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1549 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1550 width
, height
, depth
, &transfer
);
1552 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1553 pipe_resource_reference(&src
, NULL
);
1557 /* Upload pixels (just memcpy). */
1559 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1562 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1563 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1564 /* 1D array textures.
1565 * We need to convert gallium coords to GL coords.
1567 void *src
= _mesa_image_address2d(unpack
, pixels
,
1568 width
, depth
, format
,
1570 memcpy(map
, src
, bytesPerRow
);
1573 ubyte
*slice_map
= map
;
1575 for (row
= 0; row
< (unsigned) height
; row
++) {
1576 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1577 width
, height
, format
,
1578 type
, slice
, row
, 0);
1579 memcpy(slice_map
, src
, bytesPerRow
);
1580 slice_map
+= transfer
->stride
;
1583 map
+= transfer
->layer_stride
;
1587 pipe_transfer_unmap(pipe
, transfer
);
1588 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1591 memset(&blit
, 0, sizeof(blit
));
1592 blit
.src
.resource
= src
;
1594 blit
.src
.format
= src_format
;
1595 blit
.dst
.resource
= dst
;
1596 blit
.dst
.level
= dst_level
;
1597 blit
.dst
.format
= dst_format
;
1598 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1599 blit
.dst
.box
.x
= xoffset
;
1600 blit
.dst
.box
.y
= yoffset
;
1601 blit
.dst
.box
.z
= zoffset
+ dstz
;
1602 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1603 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1604 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1605 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1606 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1607 blit
.scissor_enable
= FALSE
;
1609 st
->pipe
->blit(st
->pipe
, &blit
);
1611 pipe_resource_reference(&src
, NULL
);
1615 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1616 width
, height
, depth
, format
, type
, pixels
,
1621 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1622 struct gl_texture_image
*texImage
,
1623 GLenum format
, GLenum type
, const void *pixels
,
1624 const struct gl_pixelstore_attrib
*unpack
)
1626 assert(dims
== 1 || dims
== 2 || dims
== 3);
1628 prep_teximage(ctx
, texImage
, format
, type
);
1630 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1633 /* allocate storage for texture data */
1634 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1635 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1639 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1640 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1641 format
, type
, pixels
, unpack
);
1646 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1647 struct gl_texture_image
*texImage
,
1648 GLint x
, GLint y
, GLint z
,
1649 GLsizei w
, GLsizei h
, GLsizei d
,
1650 GLenum format
, GLsizei imageSize
, const void *data
)
1652 struct st_context
*st
= st_context(ctx
);
1653 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1654 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1655 struct pipe_resource
*texture
= stImage
->pt
;
1656 struct pipe_context
*pipe
= st
->pipe
;
1657 struct pipe_screen
*screen
= pipe
->screen
;
1658 struct pipe_resource
*dst
= stImage
->pt
;
1659 struct pipe_surface
*surface
= NULL
;
1660 struct compressed_pixelstore store
;
1661 struct st_pbo_addresses addr
;
1662 enum pipe_format copy_format
;
1664 intptr_t buf_offset
;
1665 bool success
= false;
1667 /* Check basic pre-conditions for PBO upload */
1668 if (!st
->prefer_blit_based_texture_transfer
) {
1672 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1675 if (st_etc_fallback(st
, texImage
)) {
1676 /* ETC isn't supported and is represented by uncompressed formats. */
1684 if (!st
->pbo
.upload_enabled
||
1685 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1689 /* Choose the pipe format for the upload. */
1690 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1691 bw
= util_format_get_blockwidth(dst
->format
);
1692 bh
= util_format_get_blockheight(dst
->format
);
1694 switch (addr
.bytes_per_pixel
) {
1696 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1699 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1705 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0,
1706 PIPE_BIND_SAMPLER_VIEW
)) {
1710 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1711 dst
->nr_samples
, PIPE_BIND_RENDER_TARGET
)) {
1715 /* Interpret the pixelstore settings. */
1716 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1717 &ctx
->Unpack
, &store
);
1718 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1719 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1721 /* Compute the offset into the buffer */
1722 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1724 if (buf_offset
% addr
.bytes_per_pixel
) {
1728 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1730 addr
.xoffset
= x
/ bw
;
1731 addr
.yoffset
= y
/ bh
;
1732 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1733 addr
.height
= store
.CopyRowsPerSlice
;
1735 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1736 addr
.image_height
= store
.TotalRowsPerSlice
;
1738 if (!st_pbo_addresses_setup(st
, st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1742 /* Set up the surface. */
1744 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1745 unsigned max_layer
= util_max_layer(texture
, level
);
1747 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1749 struct pipe_surface templ
;
1750 memset(&templ
, 0, sizeof(templ
));
1751 templ
.format
= copy_format
;
1752 templ
.u
.tex
.level
= level
;
1753 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1754 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1756 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1761 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1763 pipe_surface_reference(&surface
, NULL
);
1769 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1771 format
, imageSize
, data
);
1775 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1776 struct gl_texture_image
*texImage
,
1777 GLsizei imageSize
, const void *data
)
1779 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1781 /* only 2D and 3D compressed images are supported at this time */
1783 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1787 /* This is pretty simple, because unlike the general texstore path we don't
1788 * have to worry about the usual image unpacking or image transfer
1792 assert(texImage
->Width
> 0);
1793 assert(texImage
->Height
> 0);
1794 assert(texImage
->Depth
> 0);
1796 /* allocate storage for texture data */
1797 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1798 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1802 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1804 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1805 texImage
->TexFormat
,
1813 * Called via ctx->Driver.GetTexSubImage()
1815 * This uses a blit to copy the texture to a texture format which matches
1816 * the format and type combo and then a fast read-back is done using memcpy.
1817 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1818 * a format which matches the swizzling.
1820 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1822 * NOTE: Drivers usually do a blit to convert between tiled and linear
1823 * texture layouts during texture uploads/downloads, so the blit
1824 * we do here should be free in such cases.
1827 st_GetTexSubImage(struct gl_context
* ctx
,
1828 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1829 GLsizei width
, GLsizei height
, GLint depth
,
1830 GLenum format
, GLenum type
, void * pixels
,
1831 struct gl_texture_image
*texImage
)
1833 struct st_context
*st
= st_context(ctx
);
1834 struct pipe_context
*pipe
= st
->pipe
;
1835 struct pipe_screen
*screen
= pipe
->screen
;
1836 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1837 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1838 struct pipe_resource
*src
= stObj
->pt
;
1839 struct pipe_resource
*dst
= NULL
;
1840 struct pipe_resource dst_templ
;
1841 enum pipe_format dst_format
, src_format
;
1842 mesa_format mesa_format
;
1843 GLenum gl_target
= texImage
->TexObject
->Target
;
1844 enum pipe_texture_target pipe_target
;
1846 struct pipe_blit_info blit
;
1848 struct pipe_transfer
*tex_xfer
;
1850 boolean done
= FALSE
;
1852 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1853 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1855 st_flush_bitmap_cache(st
);
1857 if (!st
->prefer_blit_based_texture_transfer
&&
1858 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1859 /* Try to avoid the fallback if we're doing texture decompression here */
1863 /* Handle non-finalized textures. */
1864 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1868 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1869 * due to an incomplete stencil blit implementation in some drivers. */
1870 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1874 /* If the base internal format and the texture format don't match, we have
1875 * to fall back to _mesa_GetTexImage_sw. */
1876 if (texImage
->_BaseFormat
!=
1877 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1881 /* See if the texture format already matches the format and type,
1882 * in which case the memcpy-based fast path will be used. */
1883 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1884 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1888 /* Convert the source format to what is expected by GetTexImage
1889 * and see if it's supported.
1891 * This only applies to glGetTexImage:
1892 * - Luminance must be returned as (L,0,0,1).
1893 * - Luminance alpha must be returned as (L,0,0,A).
1894 * - Intensity must be returned as (I,0,0,1)
1896 if (stObj
->surface_based
)
1897 src_format
= util_format_linear(stObj
->surface_format
);
1899 src_format
= util_format_linear(src
->format
);
1900 src_format
= util_format_luminance_to_red(src_format
);
1901 src_format
= util_format_intensity_to_red(src_format
);
1904 !screen
->is_format_supported(screen
, src_format
, src
->target
,
1906 PIPE_BIND_SAMPLER_VIEW
)) {
1910 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1911 bind
= PIPE_BIND_DEPTH_STENCIL
;
1913 bind
= PIPE_BIND_RENDER_TARGET
;
1915 /* GetTexImage only returns a single face for cubemaps. */
1916 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1917 gl_target
= GL_TEXTURE_2D
;
1919 pipe_target
= gl_target_to_pipe(gl_target
);
1921 /* Choose the destination format by finding the best match
1922 * for the format+type combo. */
1923 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1924 ctx
->Pack
.SwapBytes
);
1926 if (dst_format
== PIPE_FORMAT_NONE
) {
1927 GLenum dst_glformat
;
1929 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1930 * where decompression with a blit is always preferred. */
1931 if (!util_format_is_compressed(src
->format
)) {
1935 /* Set the appropriate format for the decompressed texture.
1936 * Luminance and sRGB formats shouldn't appear here.*/
1937 switch (src_format
) {
1938 case PIPE_FORMAT_DXT1_RGB
:
1939 case PIPE_FORMAT_DXT1_RGBA
:
1940 case PIPE_FORMAT_DXT3_RGBA
:
1941 case PIPE_FORMAT_DXT5_RGBA
:
1942 case PIPE_FORMAT_RGTC1_UNORM
:
1943 case PIPE_FORMAT_RGTC2_UNORM
:
1944 case PIPE_FORMAT_ETC1_RGB8
:
1945 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1946 dst_glformat
= GL_RGBA8
;
1948 case PIPE_FORMAT_RGTC1_SNORM
:
1949 case PIPE_FORMAT_RGTC2_SNORM
:
1950 if (!ctx
->Extensions
.EXT_texture_snorm
)
1952 dst_glformat
= GL_RGBA8_SNORM
;
1954 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1955 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1956 if (!ctx
->Extensions
.ARB_texture_float
)
1958 dst_glformat
= GL_RGBA32F
;
1965 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
1966 pipe_target
, 0, bind
, FALSE
);
1968 if (dst_format
== PIPE_FORMAT_NONE
) {
1969 /* unable to get an rgba format!?! */
1974 /* create the destination texture of size (width X height X depth) */
1975 memset(&dst_templ
, 0, sizeof(dst_templ
));
1976 dst_templ
.target
= pipe_target
;
1977 dst_templ
.format
= dst_format
;
1978 dst_templ
.bind
= bind
;
1979 dst_templ
.usage
= PIPE_USAGE_STAGING
;
1981 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1982 &dst_templ
.width0
, &dst_templ
.height0
,
1983 &dst_templ
.depth0
, &dst_templ
.array_size
);
1985 dst
= screen
->resource_create(screen
, &dst_templ
);
1990 /* From now on, we need the gallium representation of dimensions. */
1991 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1998 assert(texImage
->Face
== 0 ||
1999 texImage
->TexObject
->MinLayer
== 0 ||
2002 memset(&blit
, 0, sizeof(blit
));
2003 blit
.src
.resource
= src
;
2004 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2005 blit
.src
.format
= src_format
;
2006 blit
.dst
.resource
= dst
;
2008 blit
.dst
.format
= dst
->format
;
2009 blit
.src
.box
.x
= xoffset
;
2011 blit
.src
.box
.y
= yoffset
;
2013 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2015 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2016 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2017 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2018 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2019 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2020 blit
.scissor_enable
= FALSE
;
2022 /* blit/render/decompress */
2023 st
->pipe
->blit(st
->pipe
, &blit
);
2025 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2027 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2028 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2033 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2034 dims
= _mesa_get_texture_dimensions(gl_target
);
2036 /* copy/pack data into user buffer */
2037 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2038 ctx
->Pack
.SwapBytes
, NULL
)) {
2040 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2043 for (slice
= 0; slice
< depth
; slice
++) {
2044 ubyte
*slice_map
= map
;
2046 for (row
= 0; row
< height
; row
++) {
2047 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2048 width
, height
, format
, type
,
2051 memcpy(dest
, slice_map
, bytesPerRow
);
2053 slice_map
+= tex_xfer
->stride
;
2056 map
+= tex_xfer
->layer_stride
;
2060 /* format translation via floats */
2063 uint32_t dstMesaFormat
;
2064 int dstStride
, srcStride
;
2066 assert(util_format_is_compressed(src
->format
));
2068 rgba
= malloc(width
* height
* 4 * sizeof(GLfloat
));
2073 if (ST_DEBUG
& DEBUG_FALLBACK
)
2074 debug_printf("%s: fallback format translation\n", __func__
);
2076 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2077 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2078 srcStride
= 4 * width
* sizeof(GLfloat
);
2079 for (slice
= 0; slice
< depth
; slice
++) {
2080 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2081 width
, height
, format
, type
,
2084 /* get float[4] rgba row from surface */
2085 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, height
,
2088 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2089 rgba
, RGBA32_FLOAT
, srcStride
,
2090 width
, height
, NULL
);
2092 /* Handle byte swapping if required */
2093 if (ctx
->Pack
.SwapBytes
) {
2094 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
2095 width
, height
, dest
, dest
);
2098 map
+= tex_xfer
->layer_stride
;
2107 pipe_transfer_unmap(pipe
, tex_xfer
);
2109 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2110 pipe_resource_reference(&dst
, NULL
);
2114 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2115 width
, height
, depth
,
2116 format
, type
, pixels
, texImage
);
2122 * Do a CopyTexSubImage operation using a read transfer from the source,
2123 * a write transfer to the destination and get_tile()/put_tile() to access
2124 * the pixels/texels.
2126 * Note: srcY=0=TOP of renderbuffer
2129 fallback_copy_texsubimage(struct gl_context
*ctx
,
2130 struct st_renderbuffer
*strb
,
2131 struct st_texture_image
*stImage
,
2133 GLint destX
, GLint destY
, GLint slice
,
2134 GLint srcX
, GLint srcY
,
2135 GLsizei width
, GLsizei height
)
2137 struct st_context
*st
= st_context(ctx
);
2138 struct pipe_context
*pipe
= st
->pipe
;
2139 struct pipe_transfer
*src_trans
;
2141 enum pipe_transfer_usage transfer_usage
;
2143 unsigned dst_width
= width
;
2144 unsigned dst_height
= height
;
2145 unsigned dst_depth
= 1;
2146 struct pipe_transfer
*transfer
;
2148 if (ST_DEBUG
& DEBUG_FALLBACK
)
2149 debug_printf("%s: fallback processing\n", __func__
);
2151 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2152 srcY
= strb
->Base
.Height
- srcY
- height
;
2155 map
= pipe_transfer_map(pipe
,
2157 strb
->surface
->u
.tex
.level
,
2158 strb
->surface
->u
.tex
.first_layer
,
2161 width
, height
, &src_trans
);
2163 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2164 baseFormat
== GL_DEPTH_STENCIL
) &&
2165 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2166 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2168 transfer_usage
= PIPE_TRANSFER_WRITE
;
2170 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2171 destX
, destY
, slice
,
2172 dst_width
, dst_height
, dst_depth
,
2175 if (baseFormat
== GL_DEPTH_COMPONENT
||
2176 baseFormat
== GL_DEPTH_STENCIL
) {
2177 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2178 ctx
->Pixel
.DepthBias
!= 0.0F
);
2182 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2183 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2192 data
= malloc(width
* sizeof(uint
));
2195 /* To avoid a large temp memory allocation, do copy row by row */
2196 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2197 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2199 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2202 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2203 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2204 0, 0, width
, 1, data
);
2207 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2212 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2220 malloc(width
* height
* 4 * sizeof(GLfloat
));
2222 if (tempSrc
&& texDest
) {
2223 const GLint dims
= 2;
2225 struct gl_texture_image
*texImage
= &stImage
->base
;
2226 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2228 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2229 unpack
.Invert
= GL_TRUE
;
2232 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2233 dstRowStride
= transfer
->layer_stride
;
2236 dstRowStride
= transfer
->stride
;
2239 /* get float/RGBA image from framebuffer */
2240 /* XXX this usually involves a lot of int/float conversion.
2241 * try to avoid that someday.
2243 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2244 util_format_linear(strb
->texture
->format
),
2247 /* Store into texture memory.
2248 * Note that this does some special things such as pixel transfer
2249 * ops and format conversion. In particular, if the dest tex format
2250 * is actually RGBA but the user created the texture as GL_RGB we
2251 * need to fill-in/override the alpha channel with 1.0.
2253 _mesa_texstore(ctx
, dims
,
2254 texImage
->_BaseFormat
,
2255 texImage
->TexFormat
,
2259 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2263 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2269 st_texture_image_unmap(st
, stImage
, slice
);
2270 pipe
->transfer_unmap(pipe
, src_trans
);
2275 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2276 * Note that the region to copy has already been clipped so we know we
2277 * won't read from outside the source renderbuffer's bounds.
2279 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2282 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2283 struct gl_texture_image
*texImage
,
2284 GLint destX
, GLint destY
, GLint slice
,
2285 struct gl_renderbuffer
*rb
,
2286 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2288 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2289 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2290 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2291 struct st_context
*st
= st_context(ctx
);
2292 struct pipe_context
*pipe
= st
->pipe
;
2293 struct pipe_screen
*screen
= pipe
->screen
;
2294 struct pipe_blit_info blit
;
2295 enum pipe_format dst_format
;
2296 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2300 st_flush_bitmap_cache(st
);
2301 st_invalidate_readpix_cache(st
);
2303 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2304 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2306 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2307 debug_printf("%s: null strb or stImage\n", __func__
);
2311 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2312 texImage
->TexFormat
)) {
2316 /* The base internal format must match the mesa format, so make sure
2317 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
2319 if (texImage
->_BaseFormat
!=
2320 _mesa_get_format_base_format(texImage
->TexFormat
) ||
2321 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
2325 /* Choose the destination format to match the TexImage behavior. */
2326 dst_format
= util_format_linear(stImage
->pt
->format
);
2327 dst_format
= util_format_luminance_to_red(dst_format
);
2328 dst_format
= util_format_intensity_to_red(dst_format
);
2330 /* See if the destination format is supported. */
2331 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2332 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2333 bind
= PIPE_BIND_DEPTH_STENCIL
;
2336 bind
= PIPE_BIND_RENDER_TARGET
;
2340 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2341 stImage
->pt
->nr_samples
, bind
)) {
2345 /* Y flipping for the main framebuffer. */
2347 srcY1
= strb
->Base
.Height
- srcY
- height
;
2348 srcY0
= srcY1
+ height
;
2352 srcY1
= srcY0
+ height
;
2355 /* Blit the texture.
2356 * This supports flipping, format conversions, and downsampling.
2358 memset(&blit
, 0, sizeof(blit
));
2359 blit
.src
.resource
= strb
->texture
;
2360 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2361 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2362 blit
.src
.box
.x
= srcX
;
2363 blit
.src
.box
.y
= srcY0
;
2364 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2365 blit
.src
.box
.width
= width
;
2366 blit
.src
.box
.height
= srcY1
- srcY0
;
2367 blit
.src
.box
.depth
= 1;
2368 blit
.dst
.resource
= stImage
->pt
;
2369 blit
.dst
.format
= dst_format
;
2370 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2371 blit
.dst
.box
.x
= destX
;
2372 blit
.dst
.box
.y
= destY
;
2373 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2374 blit
.dst
.box
.width
= width
;
2375 blit
.dst
.box
.height
= height
;
2376 blit
.dst
.box
.depth
= 1;
2377 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2378 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2379 pipe
->blit(pipe
, &blit
);
2383 /* software fallback */
2384 fallback_copy_texsubimage(ctx
,
2385 strb
, stImage
, texImage
->_BaseFormat
,
2386 destX
, destY
, slice
,
2387 srcX
, srcY
, width
, height
);
2392 * Copy image data from stImage into the texture object 'stObj' at level
2396 copy_image_data_to_texture(struct st_context
*st
,
2397 struct st_texture_object
*stObj
,
2399 struct st_texture_image
*stImage
)
2403 const struct gl_texture_image MAYBE_UNUSED
*dstImage
=
2404 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2406 assert(dstImage
->Width
== stImage
->base
.Width
);
2407 assert(dstImage
->Height
== stImage
->base
.Height
);
2408 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2412 /* Copy potentially with the blitter:
2415 if (stImage
->pt
->last_level
== 0)
2418 src_level
= stImage
->base
.Level
;
2420 assert(src_level
<= stImage
->pt
->last_level
);
2421 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2422 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2423 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2424 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2425 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2426 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2428 st_texture_image_copy(st
->pipe
,
2429 stObj
->pt
, dstLevel
, /* dest texture, level */
2430 stImage
->pt
, src_level
, /* src texture, level */
2431 stImage
->base
.Face
);
2433 pipe_resource_reference(&stImage
->pt
, NULL
);
2435 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2440 * Called during state validation. When this function is finished,
2441 * the texture object should be ready for rendering.
2442 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2445 st_finalize_texture(struct gl_context
*ctx
,
2446 struct pipe_context
*pipe
,
2447 struct gl_texture_object
*tObj
,
2450 struct st_context
*st
= st_context(ctx
);
2451 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2452 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2454 const struct st_texture_image
*firstImage
;
2455 enum pipe_format firstImageFormat
;
2457 uint16_t ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2459 if (tObj
->Immutable
)
2462 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
2463 /* The texture is complete and we know exactly how many mipmap levels
2464 * are present/needed. This is conditional because we may be called
2465 * from the st_generate_mipmap() function when the texture object is
2466 * incomplete. In that case, we'll have set stObj->lastLevel before
2469 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
2470 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
2471 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2473 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2476 firstImage
= st_texture_image_const(stObj
->base
.Image
[cubeMapFace
][stObj
->base
.BaseLevel
]);
2479 /* Skip the loop over images in the common case of no images having
2480 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
2481 * haven't looked at, then we do need to look at those new images.
2483 if (!stObj
->needs_validation
&&
2484 stObj
->base
.BaseLevel
>= stObj
->validated_first_level
&&
2485 stObj
->lastLevel
<= stObj
->validated_last_level
) {
2489 /* If both firstImage and stObj point to a texture which can contain
2490 * all active images, favour firstImage. Note that because of the
2491 * completeness requirement, we know that the image dimensions
2494 if (firstImage
->pt
&&
2495 firstImage
->pt
!= stObj
->pt
&&
2496 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2497 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2498 st_texture_release_all_sampler_views(st
, stObj
);
2501 /* If this texture comes from a window system, there is nothing else to do. */
2502 if (stObj
->surface_based
) {
2506 /* Find gallium format for the Mesa texture */
2508 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2510 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2513 uint16_t height
, depth
;
2515 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2516 firstImage
->base
.Width2
,
2517 firstImage
->base
.Height2
,
2518 firstImage
->base
.Depth2
,
2519 &width
, &height
, &depth
, &ptLayers
);
2521 /* If we previously allocated a pipe texture and its sizes are
2522 * compatible, use them.
2525 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2526 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2527 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2528 ptWidth
= stObj
->pt
->width0
;
2529 ptHeight
= stObj
->pt
->height0
;
2530 ptDepth
= stObj
->pt
->depth0
;
2532 /* Otherwise, compute a new level=0 size that is compatible with the
2535 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2536 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2537 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2539 /* If the base level image is 1x1x1, we still need to ensure that the
2540 * resulting pipe texture ends up with the required number of levels
2543 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2544 ptWidth
<<= firstImage
->base
.Level
;
2546 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2547 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2551 /* At this point, the texture may be incomplete (mismatched cube
2552 * face sizes, for example). If that's the case, give up, but
2553 * don't return GL_FALSE as that would raise an incorrect
2554 * GL_OUT_OF_MEMORY error. See Piglit fbo-incomplete-texture-03 test.
2556 if (!stObj
->base
._BaseComplete
) {
2557 _mesa_test_texobj_completeness(ctx
, &stObj
->base
);
2558 if (!stObj
->base
._BaseComplete
) {
2564 ptNumSamples
= firstImage
->base
.NumSamples
;
2567 /* If we already have a gallium texture, check that it matches the texture
2568 * object's format, target, size, num_levels, etc.
2571 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2572 stObj
->pt
->format
!= firstImageFormat
||
2573 stObj
->pt
->last_level
< stObj
->lastLevel
||
2574 stObj
->pt
->width0
!= ptWidth
||
2575 stObj
->pt
->height0
!= ptHeight
||
2576 stObj
->pt
->depth0
!= ptDepth
||
2577 stObj
->pt
->nr_samples
!= ptNumSamples
||
2578 stObj
->pt
->array_size
!= ptLayers
)
2580 /* The gallium texture does not match the Mesa texture so delete the
2581 * gallium texture now. We'll make a new one below.
2583 pipe_resource_reference(&stObj
->pt
, NULL
);
2584 st_texture_release_all_sampler_views(st
, stObj
);
2585 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
2589 /* May need to create a new gallium texture:
2592 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2594 stObj
->pt
= st_texture_create(st
,
2595 gl_target_to_pipe(stObj
->base
.Target
),
2601 ptLayers
, ptNumSamples
,
2605 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2610 /* Pull in any images not in the object's texture:
2612 for (face
= 0; face
< nr_faces
; face
++) {
2614 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2615 struct st_texture_image
*stImage
=
2616 st_texture_image(stObj
->base
.Image
[face
][level
]);
2618 /* Need to import images in main memory or held in other textures.
2620 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2624 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2625 height
= u_minify(ptHeight
, level
);
2629 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2630 depth
= u_minify(ptDepth
, level
);
2631 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2637 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2638 stImage
->base
.Height
== height
&&
2639 stImage
->base
.Depth
== depth
)) {
2640 /* src image fits expected dest mipmap level size */
2641 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2647 stObj
->validated_first_level
= stObj
->base
.BaseLevel
;
2648 stObj
->validated_last_level
= stObj
->lastLevel
;
2649 stObj
->needs_validation
= false;
2655 * Allocate a new pipe_resource object
2656 * width0, height0, depth0 are the dimensions of the level 0 image
2657 * (the highest resolution). last_level indicates how many mipmap levels
2658 * to allocate storage for. For non-mipmapped textures, this will be zero.
2660 static struct pipe_resource
*
2661 st_texture_create_from_memory(struct st_context
*st
,
2662 struct st_memory_object
*memObj
,
2664 enum pipe_texture_target target
,
2665 enum pipe_format format
,
2674 struct pipe_resource pt
, *newtex
;
2675 struct pipe_screen
*screen
= st
->pipe
->screen
;
2677 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
2679 assert(height0
> 0);
2681 if (target
== PIPE_TEXTURE_CUBE
)
2682 assert(layers
== 6);
2684 DBG("%s target %d format %s last_level %d\n", __func__
,
2685 (int) target
, util_format_name(format
), last_level
);
2688 assert(screen
->is_format_supported(screen
, format
, target
, 0,
2689 PIPE_BIND_SAMPLER_VIEW
));
2691 memset(&pt
, 0, sizeof(pt
));
2694 pt
.last_level
= last_level
;
2696 pt
.height0
= height0
;
2698 pt
.array_size
= layers
;
2699 pt
.usage
= PIPE_USAGE_DEFAULT
;
2701 /* only set this for OpenGL textures, not renderbuffers */
2702 pt
.flags
= PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY
;
2703 pt
.nr_samples
= nr_samples
;
2705 newtex
= screen
->resource_from_memobj(screen
, &pt
, memObj
->memory
, offset
);
2707 assert(!newtex
|| pipe_is_referenced(&newtex
->reference
));
2713 * Allocate texture memory for a whole mipmap stack.
2714 * Note: for multisample textures if the requested sample count is not
2715 * supported, we search for the next higher supported sample count.
2718 st_texture_storage(struct gl_context
*ctx
,
2719 struct gl_texture_object
*texObj
,
2720 GLsizei levels
, GLsizei width
,
2721 GLsizei height
, GLsizei depth
,
2722 struct gl_memory_object
*memObj
,
2725 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2726 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2727 struct st_context
*st
= st_context(ctx
);
2728 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2729 struct st_memory_object
*smObj
= st_memory_object(memObj
);
2730 struct pipe_screen
*screen
= st
->pipe
->screen
;
2731 unsigned ptWidth
, bindings
;
2732 uint16_t ptHeight
, ptDepth
, ptLayers
;
2733 enum pipe_format fmt
;
2735 GLuint num_samples
= texImage
->NumSamples
;
2739 stObj
->lastLevel
= levels
- 1;
2741 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2743 bindings
= default_bindings(st
, fmt
);
2745 if (num_samples
> 0) {
2746 /* Find msaa sample count which is actually supported. For example,
2747 * if the user requests 1x but only 4x or 8x msaa is supported, we'll
2750 enum pipe_texture_target ptarget
= gl_target_to_pipe(texObj
->Target
);
2751 boolean found
= FALSE
;
2753 if (ctx
->Const
.MaxSamples
> 1 && num_samples
== 1) {
2754 /* don't try num_samples = 1 with drivers that support real msaa */
2758 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2759 if (screen
->is_format_supported(screen
, fmt
, ptarget
,
2761 PIPE_BIND_SAMPLER_VIEW
)) {
2762 /* Update the sample count in gl_texture_image as well. */
2763 texImage
->NumSamples
= num_samples
;
2774 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2775 width
, height
, depth
,
2776 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2779 stObj
->pt
= st_texture_create_from_memory(st
,
2782 gl_target_to_pipe(texObj
->Target
),
2788 ptLayers
, num_samples
,
2792 stObj
->pt
= st_texture_create(st
,
2793 gl_target_to_pipe(texObj
->Target
),
2799 ptLayers
, num_samples
,
2806 /* Set image resource pointers */
2807 for (level
= 0; level
< levels
; level
++) {
2809 for (face
= 0; face
< numFaces
; face
++) {
2810 struct st_texture_image
*stImage
=
2811 st_texture_image(texObj
->Image
[face
][level
]);
2812 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2814 etc_fallback_allocate(st
, stImage
);
2818 /* The texture is in a validated state, so no need to check later. */
2819 stObj
->needs_validation
= false;
2820 stObj
->validated_first_level
= 0;
2821 stObj
->validated_last_level
= levels
- 1;
2827 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2828 * for a whole mipmap stack.
2831 st_AllocTextureStorage(struct gl_context
*ctx
,
2832 struct gl_texture_object
*texObj
,
2833 GLsizei levels
, GLsizei width
,
2834 GLsizei height
, GLsizei depth
)
2836 return st_texture_storage(ctx
, texObj
, levels
,
2837 width
, height
, depth
,
2843 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2844 GLuint numLevels
, GLint level
,
2845 mesa_format format
, GLuint numSamples
,
2846 GLint width
, GLint height
, GLint depth
)
2848 struct st_context
*st
= st_context(ctx
);
2849 struct pipe_context
*pipe
= st
->pipe
;
2851 if (width
== 0 || height
== 0 || depth
== 0) {
2852 /* zero-sized images are legal, and always fit! */
2856 if (pipe
->screen
->can_create_resource
) {
2857 /* Ask the gallium driver if the texture is too large */
2858 struct gl_texture_object
*texObj
=
2859 _mesa_get_current_tex_object(ctx
, target
);
2860 struct pipe_resource pt
;
2862 /* Setup the pipe_resource object
2864 memset(&pt
, 0, sizeof(pt
));
2866 pt
.target
= gl_target_to_pipe(target
);
2867 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
2868 pt
.nr_samples
= numSamples
;
2870 st_gl_texture_dims_to_pipe_dims(target
,
2871 width
, height
, depth
,
2872 &pt
.width0
, &pt
.height0
,
2873 &pt
.depth0
, &pt
.array_size
);
2875 if (numLevels
> 0) {
2876 /* For immutable textures we know the final number of mip levels */
2877 pt
.last_level
= numLevels
- 1;
2879 else if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
2880 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
2881 /* assume just one mipmap level */
2885 /* assume a full set of mipmaps */
2886 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
2889 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
2892 /* Use core Mesa fallback */
2893 return _mesa_test_proxy_teximage(ctx
, target
, numLevels
, level
, format
,
2894 numSamples
, width
, height
, depth
);
2899 st_TextureView(struct gl_context
*ctx
,
2900 struct gl_texture_object
*texObj
,
2901 struct gl_texture_object
*origTexObj
)
2903 struct st_context
*st
= st_context(ctx
);
2904 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
2905 struct st_texture_object
*tex
= st_texture_object(texObj
);
2906 struct gl_texture_image
*image
= texObj
->Image
[0][0];
2908 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2909 const int numLevels
= texObj
->NumLevels
;
2914 pipe_resource_reference(&tex
->pt
, orig
->pt
);
2916 /* Set image resource pointers */
2917 for (level
= 0; level
< numLevels
; level
++) {
2918 for (face
= 0; face
< numFaces
; face
++) {
2919 struct st_texture_image
*stImage
=
2920 st_texture_image(texObj
->Image
[face
][level
]);
2921 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
2925 tex
->surface_based
= GL_TRUE
;
2926 tex
->surface_format
=
2927 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
2929 tex
->lastLevel
= numLevels
- 1;
2931 /* free texture sampler views. They need to be recreated when we
2932 * change the texture view parameters.
2934 st_texture_release_all_sampler_views(st
, tex
);
2936 /* The texture is in a validated state, so no need to check later. */
2937 tex
->needs_validation
= false;
2938 tex
->validated_first_level
= 0;
2939 tex
->validated_last_level
= numLevels
- 1;
2946 * Find the mipmap level in 'pt' which matches the level described by
2950 find_mipmap_level(const struct gl_texture_image
*texImage
,
2951 const struct pipe_resource
*pt
)
2953 const GLenum target
= texImage
->TexObject
->Target
;
2954 GLint texWidth
= texImage
->Width
;
2955 GLint texHeight
= texImage
->Height
;
2956 GLint texDepth
= texImage
->Depth
;
2958 uint16_t h
, d
, layers
;
2960 st_gl_texture_dims_to_pipe_dims(target
, texWidth
, texHeight
, texDepth
,
2961 &w
, &h
, &d
, &layers
);
2963 for (level
= 0; level
<= pt
->last_level
; level
++) {
2964 if (u_minify(pt
->width0
, level
) == w
&&
2965 u_minify(pt
->height0
, level
) == h
&&
2966 u_minify(pt
->depth0
, level
) == d
) {
2971 /* If we get here, there must be some sort of inconsistency between
2972 * the Mesa texture object/images and the gallium resource.
2974 debug_printf("Inconsistent textures in find_mipmap_level()\n");
2976 return texImage
->Level
;
2981 st_ClearTexSubImage(struct gl_context
*ctx
,
2982 struct gl_texture_image
*texImage
,
2983 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2984 GLsizei width
, GLsizei height
, GLsizei depth
,
2985 const void *clearValue
)
2987 static const char zeros
[16] = {0};
2988 struct gl_texture_object
*texObj
= texImage
->TexObject
;
2989 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2990 struct pipe_resource
*pt
= stImage
->pt
;
2991 struct st_context
*st
= st_context(ctx
);
2992 struct pipe_context
*pipe
= st
->pipe
;
2994 struct pipe_box box
;
2999 st_flush_bitmap_cache(st
);
3000 st_invalidate_readpix_cache(st
);
3002 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
3003 width
, height
, depth
, &box
);
3004 if (texObj
->Immutable
) {
3005 /* The texture object has to be consistent (no "loose", per-image
3006 * gallium resources). If this texture is a view into another
3007 * texture, we have to apply the MinLevel/Layer offsets. If this is
3008 * not a texture view, the offsets will be zero.
3010 assert(stImage
->pt
== st_texture_object(texObj
)->pt
);
3011 level
= texImage
->Level
+ texObj
->MinLevel
;
3012 box
.z
+= texObj
->MinLayer
;
3015 /* Texture level sizes may be inconsistent. We my have "loose",
3016 * per-image gallium resources. The texImage->Level may not match
3017 * the gallium resource texture level.
3019 level
= find_mipmap_level(texImage
, pt
);
3022 assert(level
<= pt
->last_level
);
3024 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
3029 * Called via the glTexParam*() function, but only when some texture object
3030 * state has actually changed.
3033 st_TexParameter(struct gl_context
*ctx
,
3034 struct gl_texture_object
*texObj
, GLenum pname
)
3036 struct st_context
*st
= st_context(ctx
);
3037 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3040 case GL_TEXTURE_BASE_LEVEL
:
3041 case GL_TEXTURE_MAX_LEVEL
:
3042 case GL_DEPTH_TEXTURE_MODE
:
3043 case GL_DEPTH_STENCIL_TEXTURE_MODE
:
3044 case GL_TEXTURE_SRGB_DECODE_EXT
:
3045 case GL_TEXTURE_SWIZZLE_R
:
3046 case GL_TEXTURE_SWIZZLE_G
:
3047 case GL_TEXTURE_SWIZZLE_B
:
3048 case GL_TEXTURE_SWIZZLE_A
:
3049 case GL_TEXTURE_SWIZZLE_RGBA
:
3050 case GL_TEXTURE_BUFFER_SIZE
:
3051 case GL_TEXTURE_BUFFER_OFFSET
:
3052 /* changing any of these texture parameters means we must create
3053 * new sampler views.
3055 st_texture_release_all_sampler_views(st
, stObj
);
3063 st_SetTextureStorageForMemoryObject(struct gl_context
*ctx
,
3064 struct gl_texture_object
*texObj
,
3065 struct gl_memory_object
*memObj
,
3066 GLsizei levels
, GLsizei width
,
3067 GLsizei height
, GLsizei depth
,
3070 return st_texture_storage(ctx
, texObj
, levels
,
3071 width
, height
, depth
,
3076 st_NewTextureHandle(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
3077 struct gl_sampler_object
*sampObj
)
3079 struct st_context
*st
= st_context(ctx
);
3080 struct st_texture_object
*stObj
= st_texture_object(texObj
);
3081 struct pipe_context
*pipe
= st
->pipe
;
3082 struct pipe_sampler_view
*view
;
3083 struct pipe_sampler_state sampler
= {0};
3085 if (texObj
->Target
!= GL_TEXTURE_BUFFER
) {
3086 if (!st_finalize_texture(ctx
, pipe
, texObj
, 0))
3089 st_convert_sampler(st
, texObj
, sampObj
, 0, &sampler
);
3091 /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
3092 view
= st_get_texture_sampler_view_from_stobj(st
, stObj
, sampObj
, 0, true);
3094 view
= st_get_buffer_sampler_view_from_stobj(st
, stObj
);
3097 return pipe
->create_texture_handle(pipe
, view
, &sampler
);
3102 st_DeleteTextureHandle(struct gl_context
*ctx
, GLuint64 handle
)
3104 struct st_context
*st
= st_context(ctx
);
3105 struct pipe_context
*pipe
= st
->pipe
;
3107 pipe
->delete_texture_handle(pipe
, handle
);
3112 st_MakeTextureHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3115 struct st_context
*st
= st_context(ctx
);
3116 struct pipe_context
*pipe
= st
->pipe
;
3118 pipe
->make_texture_handle_resident(pipe
, handle
, resident
);
3123 st_NewImageHandle(struct gl_context
*ctx
, struct gl_image_unit
*imgObj
)
3125 struct st_context
*st
= st_context(ctx
);
3126 struct pipe_context
*pipe
= st
->pipe
;
3127 struct pipe_image_view image
;
3129 st_convert_image(st
, imgObj
, &image
);
3131 return pipe
->create_image_handle(pipe
, &image
);
3136 st_DeleteImageHandle(struct gl_context
*ctx
, GLuint64 handle
)
3138 struct st_context
*st
= st_context(ctx
);
3139 struct pipe_context
*pipe
= st
->pipe
;
3141 pipe
->delete_image_handle(pipe
, handle
);
3146 st_MakeImageHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3147 GLenum access
, bool resident
)
3149 struct st_context
*st
= st_context(ctx
);
3150 struct pipe_context
*pipe
= st
->pipe
;
3152 pipe
->make_image_handle_resident(pipe
, handle
, access
, resident
);
3157 st_init_texture_functions(struct dd_function_table
*functions
)
3159 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
3160 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
3161 functions
->TexImage
= st_TexImage
;
3162 functions
->TexSubImage
= st_TexSubImage
;
3163 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
3164 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
3165 functions
->GenerateMipmap
= st_generate_mipmap
;
3167 functions
->GetTexSubImage
= st_GetTexSubImage
;
3169 /* compressed texture functions */
3170 functions
->CompressedTexImage
= st_CompressedTexImage
;
3171 functions
->GetCompressedTexSubImage
= _mesa_GetCompressedTexSubImage_sw
;
3173 functions
->NewTextureObject
= st_NewTextureObject
;
3174 functions
->NewTextureImage
= st_NewTextureImage
;
3175 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
3176 functions
->DeleteTexture
= st_DeleteTextureObject
;
3177 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
3178 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
3179 functions
->MapTextureImage
= st_MapTextureImage
;
3180 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
3182 /* XXX Temporary until we can query pipe's texture sizes */
3183 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
3185 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
3186 functions
->TextureView
= st_TextureView
;
3187 functions
->ClearTexSubImage
= st_ClearTexSubImage
;
3189 functions
->TexParameter
= st_TexParameter
;
3191 /* bindless functions */
3192 functions
->NewTextureHandle
= st_NewTextureHandle
;
3193 functions
->DeleteTextureHandle
= st_DeleteTextureHandle
;
3194 functions
->MakeTextureHandleResident
= st_MakeTextureHandleResident
;
3195 functions
->NewImageHandle
= st_NewImageHandle
;
3196 functions
->DeleteImageHandle
= st_DeleteImageHandle
;
3197 functions
->MakeImageHandleResident
= st_MakeImageHandleResident
;
3199 /* external object functions */
3200 functions
->SetTextureStorageForMemoryObject
= st_SetTextureStorageForMemoryObject
;