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_format.h"
57 #include "state_tracker/st_pbo.h"
58 #include "state_tracker/st_texture.h"
59 #include "state_tracker/st_gen_mipmap.h"
60 #include "state_tracker/st_atom.h"
61 #include "state_tracker/st_sampler_view.h"
63 #include "pipe/p_context.h"
64 #include "pipe/p_defines.h"
65 #include "util/u_inlines.h"
66 #include "util/u_upload_mgr.h"
67 #include "pipe/p_shader_tokens.h"
68 #include "util/u_tile.h"
69 #include "util/u_format.h"
70 #include "util/u_surface.h"
71 #include "util/u_sampler.h"
72 #include "util/u_math.h"
73 #include "util/u_box.h"
74 #include "util/u_simple_shaders.h"
75 #include "cso_cache/cso_context.h"
76 #include "tgsi/tgsi_ureg.h"
78 #define DBG if (0) printf
81 enum pipe_texture_target
82 gl_target_to_pipe(GLenum target
)
86 case GL_PROXY_TEXTURE_1D
:
87 return PIPE_TEXTURE_1D
;
89 case GL_PROXY_TEXTURE_2D
:
90 case GL_TEXTURE_EXTERNAL_OES
:
91 case GL_TEXTURE_2D_MULTISAMPLE
:
92 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
93 return PIPE_TEXTURE_2D
;
94 case GL_TEXTURE_RECTANGLE_NV
:
95 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
96 return PIPE_TEXTURE_RECT
;
98 case GL_PROXY_TEXTURE_3D
:
99 return PIPE_TEXTURE_3D
;
100 case GL_TEXTURE_CUBE_MAP_ARB
:
101 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
102 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
103 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
104 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
105 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
106 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
107 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
108 return PIPE_TEXTURE_CUBE
;
109 case GL_TEXTURE_1D_ARRAY_EXT
:
110 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
111 return PIPE_TEXTURE_1D_ARRAY
;
112 case GL_TEXTURE_2D_ARRAY_EXT
:
113 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
114 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
115 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
116 return PIPE_TEXTURE_2D_ARRAY
;
117 case GL_TEXTURE_BUFFER
:
119 case GL_TEXTURE_CUBE_MAP_ARRAY
:
120 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
121 return PIPE_TEXTURE_CUBE_ARRAY
;
129 /** called via ctx->Driver.NewTextureImage() */
130 static struct gl_texture_image
*
131 st_NewTextureImage(struct gl_context
* ctx
)
133 DBG("%s\n", __func__
);
135 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
139 /** called via ctx->Driver.DeleteTextureImage() */
141 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
143 /* nothing special (yet) for st_texture_image */
144 _mesa_delete_texture_image(ctx
, img
);
148 /** called via ctx->Driver.NewTextureObject() */
149 static struct gl_texture_object
*
150 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
152 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
154 DBG("%s\n", __func__
);
155 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
157 obj
->needs_validation
= true;
162 /** called via ctx->Driver.DeleteTextureObject() */
164 st_DeleteTextureObject(struct gl_context
*ctx
,
165 struct gl_texture_object
*texObj
)
167 struct st_context
*st
= st_context(ctx
);
168 struct st_texture_object
*stObj
= st_texture_object(texObj
);
170 pipe_resource_reference(&stObj
->pt
, NULL
);
171 st_texture_release_all_sampler_views(st
, stObj
);
172 st_texture_free_sampler_views(stObj
);
173 _mesa_delete_texture_object(ctx
, texObj
);
177 /** called via ctx->Driver.FreeTextureImageBuffer() */
179 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
180 struct gl_texture_image
*texImage
)
182 struct st_context
*st
= st_context(ctx
);
183 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
184 struct st_texture_image
*stImage
= st_texture_image(texImage
);
186 DBG("%s\n", __func__
);
189 pipe_resource_reference(&stImage
->pt
, NULL
);
192 free(stImage
->transfer
);
193 stImage
->transfer
= NULL
;
194 stImage
->num_transfers
= 0;
196 if (stImage
->etc_data
) {
197 free(stImage
->etc_data
);
198 stImage
->etc_data
= NULL
;
201 /* if the texture image is being deallocated, the structure of the
202 * texture is changing so we'll likely need a new sampler view.
204 st_texture_release_all_sampler_views(st
, stObj
);
208 st_etc_fallback(struct st_context
*st
, struct gl_texture_image
*texImage
)
210 return (_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
211 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
);
215 etc_fallback_allocate(struct st_context
*st
, struct st_texture_image
*stImage
)
217 struct gl_texture_image
*texImage
= &stImage
->base
;
219 if (!st_etc_fallback(st
, texImage
))
222 if (stImage
->etc_data
)
223 free(stImage
->etc_data
);
225 unsigned data_size
= _mesa_format_image_size(texImage
->TexFormat
,
231 malloc(data_size
* _mesa_num_tex_faces(texImage
->TexObject
->Target
));
234 /** called via ctx->Driver.MapTextureImage() */
236 st_MapTextureImage(struct gl_context
*ctx
,
237 struct gl_texture_image
*texImage
,
238 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
240 GLubyte
**mapOut
, GLint
*rowStrideOut
)
242 struct st_context
*st
= st_context(ctx
);
243 struct st_texture_image
*stImage
= st_texture_image(texImage
);
246 struct pipe_transfer
*transfer
;
249 if (mode
& GL_MAP_READ_BIT
)
250 pipeMode
|= PIPE_TRANSFER_READ
;
251 if (mode
& GL_MAP_WRITE_BIT
)
252 pipeMode
|= PIPE_TRANSFER_WRITE
;
253 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
254 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
256 map
= st_texture_image_map(st
, stImage
, pipeMode
, x
, y
, slice
, w
, h
, 1,
259 if (st_etc_fallback(st
, texImage
)) {
260 /* ETC isn't supported by all gallium drivers, where it's represented
261 * by uncompressed formats. We store the compressed data (as it's
262 * needed for image copies in OES_copy_image), and decompress as
263 * necessary in Unmap.
265 * Note: all ETC1/ETC2 formats have 4x4 block sizes.
267 unsigned z
= transfer
->box
.z
;
268 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
270 unsigned bytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
271 unsigned stride
= *rowStrideOut
= itransfer
->temp_stride
=
272 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width2
);
273 *mapOut
= itransfer
->temp_data
=
274 stImage
->etc_data
+ ((x
/ 4) * bytes
+ (y
/ 4) * stride
) +
275 z
* stride
* texImage
->Height2
/ 4;
276 itransfer
->map
= map
;
279 /* supported mapping */
281 *rowStrideOut
= transfer
->stride
;
291 /** called via ctx->Driver.UnmapTextureImage() */
293 st_UnmapTextureImage(struct gl_context
*ctx
,
294 struct gl_texture_image
*texImage
,
297 struct st_context
*st
= st_context(ctx
);
298 struct st_texture_image
*stImage
= st_texture_image(texImage
);
300 if (st_etc_fallback(st
, texImage
)) {
301 /* Decompress the ETC texture to the mapped one. */
302 unsigned z
= slice
+ stImage
->base
.Face
;
303 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
304 struct pipe_transfer
*transfer
= itransfer
->transfer
;
306 assert(z
== transfer
->box
.z
);
308 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
309 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
310 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
311 itransfer
->temp_data
,
312 itransfer
->temp_stride
,
313 transfer
->box
.width
, transfer
->box
.height
);
316 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
317 itransfer
->temp_data
, itransfer
->temp_stride
,
318 transfer
->box
.width
, transfer
->box
.height
,
319 texImage
->TexFormat
);
323 itransfer
->temp_data
= NULL
;
324 itransfer
->temp_stride
= 0;
328 st_texture_image_unmap(st
, stImage
, slice
);
333 * Return default texture resource binding bitmask for the given format.
336 default_bindings(struct st_context
*st
, enum pipe_format format
)
338 struct pipe_screen
*screen
= st
->pipe
->screen
;
339 const unsigned target
= PIPE_TEXTURE_2D
;
342 if (util_format_is_depth_or_stencil(format
))
343 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
345 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
347 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
351 format
= util_format_linear(format
);
353 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
356 return PIPE_BIND_SAMPLER_VIEW
;
362 * Given the size of a mipmap image, try to compute the size of the level=0
365 * Note that this isn't always accurate for odd-sized, non-POW textures.
366 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
368 * \return GL_TRUE for success, GL_FALSE for failure
371 guess_base_level_size(GLenum target
,
372 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
373 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
380 /* Guess the size of the base level.
381 * Depending on the image's size, we can't always make a guess here.
385 case GL_TEXTURE_1D_ARRAY
:
390 case GL_TEXTURE_2D_ARRAY
:
391 /* We can't make a good guess here, because the base level dimensions
394 if (width
== 1 || height
== 1) {
401 case GL_TEXTURE_CUBE_MAP
:
402 case GL_TEXTURE_CUBE_MAP_ARRAY
:
408 /* We can't make a good guess here, because the base level dimensions
411 if (width
== 1 || height
== 1 || depth
== 1) {
419 case GL_TEXTURE_RECTANGLE
:
436 * Try to determine whether we should allocate memory for a full texture
437 * mipmap. The problem is when we get a glTexImage(level=0) call, we
438 * can't immediately know if other mipmap levels are coming next. Here
439 * we try to guess whether to allocate memory for a mipmap or just the
442 * If we guess incorrectly here we'll later reallocate the right amount of
443 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
445 * \param stObj the texture object we're going to allocate memory for.
446 * \param stImage describes the incoming image which we need to store.
449 allocate_full_mipmap(const struct st_texture_object
*stObj
,
450 const struct st_texture_image
*stImage
)
452 switch (stObj
->base
.Target
) {
453 case GL_TEXTURE_RECTANGLE_NV
:
454 case GL_TEXTURE_BUFFER
:
455 case GL_TEXTURE_EXTERNAL_OES
:
456 case GL_TEXTURE_2D_MULTISAMPLE
:
457 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
458 /* these texture types cannot be mipmapped */
462 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
465 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
466 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
467 /* depth/stencil textures are seldom mipmapped */
470 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
473 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
474 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
475 /* not a mipmap minification filter */
478 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
479 /* 3D textures are seldom mipmapped */
487 * Try to allocate a pipe_resource object for the given st_texture_object.
489 * We use the given st_texture_image as a clue to determine the size of the
490 * mipmap image at level=0.
492 * \return GL_TRUE for success, GL_FALSE if out of memory.
495 guess_and_alloc_texture(struct st_context
*st
,
496 struct st_texture_object
*stObj
,
497 const struct st_texture_image
*stImage
)
499 const struct gl_texture_image
*firstImage
;
500 GLuint lastLevel
, width
, height
, depth
;
503 uint16_t ptHeight
, ptDepth
, ptLayers
;
504 enum pipe_format fmt
;
505 bool guessed_box
= false;
507 DBG("%s\n", __func__
);
511 /* If a base level image with compatible size exists, use that as our guess.
513 firstImage
= _mesa_base_tex_image(&stObj
->base
);
515 firstImage
->Width2
> 0 &&
516 firstImage
->Height2
> 0 &&
517 firstImage
->Depth2
> 0 &&
518 guess_base_level_size(stObj
->base
.Target
,
523 &width
, &height
, &depth
)) {
524 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
525 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
526 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
531 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
532 stImage
->base
.Width2
,
533 stImage
->base
.Height2
,
534 stImage
->base
.Depth2
,
536 &width
, &height
, &depth
);
539 /* we can't determine the image size at level=0 */
540 /* this is not an out of memory error */
544 /* At this point, (width x height x depth) is the expected size of
545 * the level=0 mipmap image.
548 /* Guess a reasonable value for lastLevel. With OpenGL we have no
549 * idea how many mipmap levels will be in a texture until we start
550 * to render with it. Make an educated guess here but be prepared
551 * to re-allocating a texture buffer with space for more (or fewer)
552 * mipmap levels later.
554 if (allocate_full_mipmap(stObj
, stImage
)) {
555 /* alloc space for a full mipmap */
556 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
557 width
, height
, depth
) - 1;
560 /* only alloc space for a single mipmap level */
564 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
566 bindings
= default_bindings(st
, fmt
);
568 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
569 width
, height
, depth
,
570 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
572 stObj
->pt
= st_texture_create(st
,
573 gl_target_to_pipe(stObj
->base
.Target
),
582 stObj
->lastLevel
= lastLevel
;
584 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
586 return stObj
->pt
!= NULL
;
591 * Called via ctx->Driver.AllocTextureImageBuffer().
592 * If the texture object/buffer already has space for the indicated image,
593 * we're done. Otherwise, allocate memory for the new texture image.
596 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
597 struct gl_texture_image
*texImage
)
599 struct st_context
*st
= st_context(ctx
);
600 struct st_texture_image
*stImage
= st_texture_image(texImage
);
601 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
602 const GLuint level
= texImage
->Level
;
603 GLuint width
= texImage
->Width
;
604 GLuint height
= texImage
->Height
;
605 GLuint depth
= texImage
->Depth
;
607 DBG("%s\n", __func__
);
609 assert(!stImage
->pt
); /* xxx this might be wrong */
611 stObj
->needs_validation
= true;
613 etc_fallback_allocate(st
, stImage
);
615 /* Look if the parent texture object has space for this image */
617 level
<= stObj
->pt
->last_level
&&
618 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
619 /* this image will fit in the existing texture object's memory */
620 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
624 /* The parent texture object does not have space for this image */
626 pipe_resource_reference(&stObj
->pt
, NULL
);
627 st_texture_release_all_sampler_views(st
, stObj
);
629 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
630 /* Probably out of memory.
631 * Try flushing any pending rendering, then retry.
634 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
635 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
641 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
642 /* The image will live in the object's mipmap memory */
643 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
648 /* Create a new, temporary texture/resource/buffer to hold this
649 * one texture image. Note that when we later access this image
650 * (either for mapping or copying) we'll want to always specify
651 * mipmap level=0, even if the image represents some other mipmap
654 enum pipe_format format
=
655 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
656 GLuint bindings
= default_bindings(st
, format
);
658 uint16_t ptHeight
, ptDepth
, ptLayers
;
660 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
661 width
, height
, depth
,
662 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
664 stImage
->pt
= st_texture_create(st
,
665 gl_target_to_pipe(stObj
->base
.Target
),
673 return stImage
->pt
!= NULL
;
679 * Preparation prior to glTexImage. Basically check the 'surface_based'
680 * field and switch to a "normal" tex image if necessary.
683 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
684 GLenum format
, GLenum type
)
686 struct gl_texture_object
*texObj
= texImage
->TexObject
;
687 struct st_texture_object
*stObj
= st_texture_object(texObj
);
689 /* switch to "normal" */
690 if (stObj
->surface_based
) {
691 const GLenum target
= texObj
->Target
;
692 const GLuint level
= texImage
->Level
;
693 mesa_format texFormat
;
695 _mesa_clear_texture_object(ctx
, texObj
);
696 pipe_resource_reference(&stObj
->pt
, NULL
);
698 /* oops, need to init this image again */
699 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
700 texImage
->InternalFormat
, format
,
703 _mesa_init_teximage_fields(ctx
, texImage
,
704 texImage
->Width
, texImage
->Height
,
705 texImage
->Depth
, texImage
->Border
,
706 texImage
->InternalFormat
, texFormat
);
708 stObj
->surface_based
= GL_FALSE
;
714 * Return a writemask for the gallium blit. The parameters can be base
715 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
718 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
721 case GL_DEPTH_STENCIL
:
723 case GL_DEPTH_STENCIL
:
725 case GL_DEPTH_COMPONENT
:
727 case GL_STENCIL_INDEX
:
734 case GL_DEPTH_COMPONENT
:
736 case GL_DEPTH_STENCIL
:
737 case GL_DEPTH_COMPONENT
:
744 case GL_STENCIL_INDEX
:
746 case GL_STENCIL_INDEX
:
754 return PIPE_MASK_RGBA
;
759 * Converts format to a format with the same components, types
760 * and sizes, but with the components in RGBA order.
762 static enum pipe_format
763 unswizzle_format(enum pipe_format format
)
767 case PIPE_FORMAT_B8G8R8A8_UNORM
:
768 case PIPE_FORMAT_A8R8G8B8_UNORM
:
769 case PIPE_FORMAT_A8B8G8R8_UNORM
:
770 return PIPE_FORMAT_R8G8B8A8_UNORM
;
772 case PIPE_FORMAT_B10G10R10A2_UNORM
:
773 return PIPE_FORMAT_R10G10B10A2_UNORM
;
775 case PIPE_FORMAT_B10G10R10A2_SNORM
:
776 return PIPE_FORMAT_R10G10B10A2_SNORM
;
778 case PIPE_FORMAT_B10G10R10A2_UINT
:
779 return PIPE_FORMAT_R10G10B10A2_UINT
;
787 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
789 static enum pipe_format
790 alpha_to_red(enum pipe_format format
)
794 case PIPE_FORMAT_A8_UNORM
:
795 return PIPE_FORMAT_R8_UNORM
;
796 case PIPE_FORMAT_A8_SNORM
:
797 return PIPE_FORMAT_R8_SNORM
;
798 case PIPE_FORMAT_A8_UINT
:
799 return PIPE_FORMAT_R8_UINT
;
800 case PIPE_FORMAT_A8_SINT
:
801 return PIPE_FORMAT_R8_SINT
;
803 case PIPE_FORMAT_A16_UNORM
:
804 return PIPE_FORMAT_R16_UNORM
;
805 case PIPE_FORMAT_A16_SNORM
:
806 return PIPE_FORMAT_R16_SNORM
;
807 case PIPE_FORMAT_A16_UINT
:
808 return PIPE_FORMAT_R16_UINT
;
809 case PIPE_FORMAT_A16_SINT
:
810 return PIPE_FORMAT_R16_SINT
;
811 case PIPE_FORMAT_A16_FLOAT
:
812 return PIPE_FORMAT_R16_FLOAT
;
814 case PIPE_FORMAT_A32_UINT
:
815 return PIPE_FORMAT_R32_UINT
;
816 case PIPE_FORMAT_A32_SINT
:
817 return PIPE_FORMAT_R32_SINT
;
818 case PIPE_FORMAT_A32_FLOAT
:
819 return PIPE_FORMAT_R32_FLOAT
;
827 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
829 static enum pipe_format
830 red_alpha_to_red_green(enum pipe_format format
)
834 case PIPE_FORMAT_R8A8_UNORM
:
835 return PIPE_FORMAT_R8G8_UNORM
;
836 case PIPE_FORMAT_R8A8_SNORM
:
837 return PIPE_FORMAT_R8G8_SNORM
;
838 case PIPE_FORMAT_R8A8_UINT
:
839 return PIPE_FORMAT_R8G8_UINT
;
840 case PIPE_FORMAT_R8A8_SINT
:
841 return PIPE_FORMAT_R8G8_SINT
;
843 case PIPE_FORMAT_R16A16_UNORM
:
844 return PIPE_FORMAT_R16G16_UNORM
;
845 case PIPE_FORMAT_R16A16_SNORM
:
846 return PIPE_FORMAT_R16G16_SNORM
;
847 case PIPE_FORMAT_R16A16_UINT
:
848 return PIPE_FORMAT_R16G16_UINT
;
849 case PIPE_FORMAT_R16A16_SINT
:
850 return PIPE_FORMAT_R16G16_SINT
;
851 case PIPE_FORMAT_R16A16_FLOAT
:
852 return PIPE_FORMAT_R16G16_FLOAT
;
854 case PIPE_FORMAT_R32A32_UINT
:
855 return PIPE_FORMAT_R32G32_UINT
;
856 case PIPE_FORMAT_R32A32_SINT
:
857 return PIPE_FORMAT_R32G32_SINT
;
858 case PIPE_FORMAT_R32A32_FLOAT
:
859 return PIPE_FORMAT_R32G32_FLOAT
;
867 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
869 static enum pipe_format
870 luminance_alpha_to_red_green(enum pipe_format format
)
874 case PIPE_FORMAT_L8A8_UNORM
:
875 return PIPE_FORMAT_R8G8_UNORM
;
876 case PIPE_FORMAT_L8A8_SNORM
:
877 return PIPE_FORMAT_R8G8_SNORM
;
878 case PIPE_FORMAT_L8A8_UINT
:
879 return PIPE_FORMAT_R8G8_UINT
;
880 case PIPE_FORMAT_L8A8_SINT
:
881 return PIPE_FORMAT_R8G8_SINT
;
883 case PIPE_FORMAT_L16A16_UNORM
:
884 return PIPE_FORMAT_R16G16_UNORM
;
885 case PIPE_FORMAT_L16A16_SNORM
:
886 return PIPE_FORMAT_R16G16_SNORM
;
887 case PIPE_FORMAT_L16A16_UINT
:
888 return PIPE_FORMAT_R16G16_UINT
;
889 case PIPE_FORMAT_L16A16_SINT
:
890 return PIPE_FORMAT_R16G16_SINT
;
891 case PIPE_FORMAT_L16A16_FLOAT
:
892 return PIPE_FORMAT_R16G16_FLOAT
;
894 case PIPE_FORMAT_L32A32_UINT
:
895 return PIPE_FORMAT_R32G32_UINT
;
896 case PIPE_FORMAT_L32A32_SINT
:
897 return PIPE_FORMAT_R32G32_SINT
;
898 case PIPE_FORMAT_L32A32_FLOAT
:
899 return PIPE_FORMAT_R32G32_FLOAT
;
907 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
910 format_is_alpha(enum pipe_format format
)
912 const struct util_format_description
*desc
= util_format_description(format
);
914 if (desc
->nr_channels
== 1 &&
915 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
916 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
917 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
918 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
925 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
928 format_is_red(enum pipe_format format
)
930 const struct util_format_description
*desc
= util_format_description(format
);
932 if (desc
->nr_channels
== 1 &&
933 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
934 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
935 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
936 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
944 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
947 format_is_luminance(enum pipe_format format
)
949 const struct util_format_description
*desc
= util_format_description(format
);
951 if (desc
->nr_channels
== 1 &&
952 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
953 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
954 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
955 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
962 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
965 format_is_red_alpha(enum pipe_format format
)
967 const struct util_format_description
*desc
= util_format_description(format
);
969 if (desc
->nr_channels
== 2 &&
970 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
971 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
972 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
973 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
980 format_is_swizzled_rgba(enum pipe_format format
)
982 const struct util_format_description
*desc
= util_format_description(format
);
984 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
985 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
986 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
987 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
995 unsigned char swizzle
[4];
996 enum pipe_format format
;
999 static const struct format_table table_8888_unorm
[] = {
1000 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
1001 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
1002 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
1003 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
1006 static const struct format_table table_1010102_unorm
[] = {
1007 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
1008 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
1011 static const struct format_table table_1010102_snorm
[] = {
1012 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
1013 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
1016 static const struct format_table table_1010102_uint
[] = {
1017 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
1018 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
1021 static enum pipe_format
1022 swizzle_format(enum pipe_format format
, const int * const swizzle
)
1027 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1028 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1029 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1030 case PIPE_FORMAT_A8B8G8R8_UNORM
:
1031 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
1032 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
1033 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
1034 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
1035 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
1036 return table_8888_unorm
[i
].format
;
1040 case PIPE_FORMAT_R10G10B10A2_UNORM
:
1041 case PIPE_FORMAT_B10G10R10A2_UNORM
:
1042 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
1043 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
1044 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
1045 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
1046 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
1047 return table_1010102_unorm
[i
].format
;
1051 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1052 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1053 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1054 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1055 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1056 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1057 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1058 return table_1010102_snorm
[i
].format
;
1062 case PIPE_FORMAT_R10G10B10A2_UINT
:
1063 case PIPE_FORMAT_B10G10R10A2_UINT
:
1064 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1065 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1066 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1067 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1068 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1069 return table_1010102_uint
[i
].format
;
1077 return PIPE_FORMAT_NONE
;
1081 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1083 enum pipe_format src
= *src_format
;
1084 enum pipe_format dst
= *dst_format
;
1086 /* Note: dst_format has already been transformed from luminance/intensity
1087 * to red when this function is called. The source format will never
1088 * be an intensity format, because GL_INTENSITY is not a legal value
1089 * for the format parameter in glTex(Sub)Image(). */
1091 if (format_is_alpha(src
)) {
1092 if (!format_is_alpha(dst
))
1095 src
= alpha_to_red(src
);
1096 dst
= alpha_to_red(dst
);
1097 } else if (format_is_luminance(src
)) {
1098 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1101 src
= util_format_luminance_to_red(src
);
1102 } else if (util_format_is_luminance_alpha(src
)) {
1103 src
= luminance_alpha_to_red_green(src
);
1105 if (format_is_red_alpha(dst
)) {
1106 dst
= red_alpha_to_red_green(dst
);
1107 } else if (!format_is_red(dst
))
1109 } else if (format_is_swizzled_rgba(src
)) {
1110 const struct util_format_description
*src_desc
= util_format_description(src
);
1111 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1115 /* Make sure the format is an RGBA and not an RGBX format */
1116 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1119 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1122 for (i
= 0; i
< 4; i
++)
1123 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1125 dst
= swizzle_format(dst
, swizzle
);
1126 if (dst
== PIPE_FORMAT_NONE
)
1129 src
= unswizzle_format(src
);
1138 try_pbo_upload_common(struct gl_context
*ctx
,
1139 struct pipe_surface
*surface
,
1140 const struct st_pbo_addresses
*addr
,
1141 enum pipe_format src_format
)
1143 struct st_context
*st
= st_context(ctx
);
1144 struct cso_context
*cso
= st
->cso_context
;
1145 struct pipe_context
*pipe
= st
->pipe
;
1146 bool success
= false;
1149 fs
= st_pbo_get_upload_fs(st
, src_format
, surface
->format
);
1153 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1154 CSO_BIT_FRAGMENT_SAMPLERS
|
1155 CSO_BIT_VERTEX_ELEMENTS
|
1156 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1157 CSO_BIT_FRAMEBUFFER
|
1160 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1161 CSO_BIT_RASTERIZER
|
1162 CSO_BIT_STREAM_OUTPUTS
|
1163 CSO_BIT_PAUSE_QUERIES
|
1164 CSO_BIT_SAMPLE_MASK
|
1165 CSO_BIT_MIN_SAMPLES
|
1166 CSO_BIT_RENDER_CONDITION
|
1167 CSO_BITS_ALL_SHADERS
));
1168 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1170 cso_set_sample_mask(cso
, ~0);
1171 cso_set_min_samples(cso
, 1);
1172 cso_set_render_condition(cso
, NULL
, FALSE
, 0);
1174 /* Set up the sampler_view */
1176 struct pipe_sampler_view templ
;
1177 struct pipe_sampler_view
*sampler_view
;
1178 struct pipe_sampler_state sampler
= {0};
1179 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
1181 memset(&templ
, 0, sizeof(templ
));
1182 templ
.target
= PIPE_BUFFER
;
1183 templ
.format
= src_format
;
1184 templ
.u
.buf
.offset
= addr
->first_element
* addr
->bytes_per_pixel
;
1185 templ
.u
.buf
.size
= (addr
->last_element
- addr
->first_element
+ 1) *
1186 addr
->bytes_per_pixel
;
1187 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1188 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1189 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1190 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1192 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1193 if (sampler_view
== NULL
)
1196 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1198 pipe_sampler_view_reference(&sampler_view
, NULL
);
1200 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
1203 /* Framebuffer_state */
1205 struct pipe_framebuffer_state fb
;
1206 memset(&fb
, 0, sizeof(fb
));
1207 fb
.width
= surface
->width
;
1208 fb
.height
= surface
->height
;
1210 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1212 cso_set_framebuffer(cso
, &fb
);
1214 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1217 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1220 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1222 /* Depth/stencil/alpha state */
1224 struct pipe_depth_stencil_alpha_state dsa
;
1225 memset(&dsa
, 0, sizeof(dsa
));
1226 cso_set_depth_stencil_alpha(cso
, &dsa
);
1229 /* Set up the fragment shader */
1230 cso_set_fragment_shader_handle(cso
, fs
);
1232 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1235 cso_restore_state(cso
);
1236 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1242 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1243 struct gl_texture_image
*texImage
,
1244 GLenum format
, GLenum type
,
1245 enum pipe_format dst_format
,
1246 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1247 GLint width
, GLint height
, GLint depth
,
1249 const struct gl_pixelstore_attrib
*unpack
)
1251 struct st_context
*st
= st_context(ctx
);
1252 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1253 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1254 struct pipe_resource
*texture
= stImage
->pt
;
1255 struct pipe_context
*pipe
= st
->pipe
;
1256 struct pipe_screen
*screen
= pipe
->screen
;
1257 struct pipe_surface
*surface
= NULL
;
1258 struct st_pbo_addresses addr
;
1259 enum pipe_format src_format
;
1260 const struct util_format_description
*desc
;
1261 GLenum gl_target
= texImage
->TexObject
->Target
;
1264 if (!st
->pbo
.upload_enabled
)
1267 /* From now on, we need the gallium representation of dimensions. */
1268 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1275 if (depth
!= 1 && !st
->pbo
.layers
)
1278 /* Choose the source format. Initially, we do so without checking driver
1279 * support at all because of the remapping we later perform and because
1280 * at least the Radeon driver actually supports some formats for texture
1281 * buffers which it doesn't support for regular textures. */
1282 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1287 src_format
= util_format_linear(src_format
);
1288 desc
= util_format_description(src_format
);
1290 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1293 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1296 if (st
->pbo
.rgba_only
) {
1297 enum pipe_format orig_dst_format
= dst_format
;
1299 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1303 if (dst_format
!= orig_dst_format
&&
1304 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1305 PIPE_BIND_RENDER_TARGET
)) {
1311 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0,
1312 PIPE_BIND_SAMPLER_VIEW
)) {
1316 /* Compute buffer addresses */
1317 addr
.xoffset
= xoffset
;
1318 addr
.yoffset
= yoffset
;
1320 addr
.height
= height
;
1322 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1324 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1328 /* Set up the surface */
1330 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1331 unsigned max_layer
= util_max_layer(texture
, level
);
1333 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1335 struct pipe_surface templ
;
1336 memset(&templ
, 0, sizeof(templ
));
1337 templ
.format
= dst_format
;
1338 templ
.u
.tex
.level
= level
;
1339 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1340 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1342 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1347 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1349 pipe_surface_reference(&surface
, NULL
);
1355 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1356 struct gl_texture_image
*texImage
,
1357 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1358 GLint width
, GLint height
, GLint depth
,
1359 GLenum format
, GLenum type
, const void *pixels
,
1360 const struct gl_pixelstore_attrib
*unpack
)
1362 struct st_context
*st
= st_context(ctx
);
1363 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1364 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1365 struct pipe_context
*pipe
= st
->pipe
;
1366 struct pipe_screen
*screen
= pipe
->screen
;
1367 struct pipe_resource
*dst
= stImage
->pt
;
1368 struct pipe_resource
*src
= NULL
;
1369 struct pipe_resource src_templ
;
1370 struct pipe_transfer
*transfer
;
1371 struct pipe_blit_info blit
;
1372 enum pipe_format src_format
, dst_format
;
1373 mesa_format mesa_src_format
;
1374 GLenum gl_target
= texImage
->TexObject
->Target
;
1377 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1378 unsigned dst_level
= 0;
1380 st_flush_bitmap_cache(st
);
1381 st_invalidate_readpix_cache(st
);
1383 if (stObj
->pt
== stImage
->pt
)
1384 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1386 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1387 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1392 /* Try texture_subdata, which should be the fastest memcpy path. */
1394 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1395 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1396 texImage
->TexFormat
, format
, type
,
1398 struct pipe_box box
;
1399 unsigned stride
, layer_stride
;
1402 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1403 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1405 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1408 /* Convert to Gallium coordinates. */
1409 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1414 layer_stride
= stride
;
1417 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1418 pipe
->texture_subdata(pipe
, dst
, dst_level
, 0,
1419 &box
, data
, stride
, layer_stride
);
1423 if (!st
->prefer_blit_based_texture_transfer
) {
1427 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1428 * blit implementation in some drivers. */
1429 if (format
== GL_DEPTH_STENCIL
) {
1433 /* If the base internal format and the texture format don't match,
1434 * we can't use blit-based TexSubImage. */
1435 if (texImage
->_BaseFormat
!=
1436 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1441 /* See if the destination format is supported. */
1442 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1443 bind
= PIPE_BIND_DEPTH_STENCIL
;
1445 bind
= PIPE_BIND_RENDER_TARGET
;
1447 /* For luminance and intensity, only the red channel is stored
1448 * in the destination. */
1449 dst_format
= util_format_linear(dst
->format
);
1450 dst_format
= util_format_luminance_to_red(dst_format
);
1451 dst_format
= util_format_intensity_to_red(dst_format
);
1454 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1455 dst
->nr_samples
, bind
)) {
1459 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1460 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1461 xoffset
, yoffset
, zoffset
,
1462 width
, height
, depth
, pixels
, unpack
))
1466 /* See if the texture format already matches the format and type,
1467 * in which case the memcpy-based fast path will likely be used and
1468 * we don't have to blit. */
1469 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1470 type
, unpack
->SwapBytes
, NULL
)) {
1474 /* Choose the source format. */
1475 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1476 format
, type
, unpack
->SwapBytes
);
1481 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1483 /* There is no reason to do this if we cannot use memcpy for the temporary
1484 * source texture at least. This also takes transfer ops into account,
1486 if (!_mesa_texstore_can_use_memcpy(ctx
,
1487 _mesa_get_format_base_format(mesa_src_format
),
1488 mesa_src_format
, format
, type
, unpack
)) {
1492 /* TexSubImage only sets a single cubemap face. */
1493 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1494 gl_target
= GL_TEXTURE_2D
;
1496 /* TexSubImage can specify subsets of cube map array faces
1497 * so we need to upload via 2D array instead */
1498 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1499 gl_target
= GL_TEXTURE_2D_ARRAY
;
1502 /* Initialize the source texture description. */
1503 memset(&src_templ
, 0, sizeof(src_templ
));
1504 src_templ
.target
= gl_target_to_pipe(gl_target
);
1505 src_templ
.format
= src_format
;
1506 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1507 src_templ
.usage
= PIPE_USAGE_STAGING
;
1509 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1510 &src_templ
.width0
, &src_templ
.height0
,
1511 &src_templ
.depth0
, &src_templ
.array_size
);
1513 /* Check for NPOT texture support. */
1514 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1515 (!util_is_power_of_two(src_templ
.width0
) ||
1516 !util_is_power_of_two(src_templ
.height0
) ||
1517 !util_is_power_of_two(src_templ
.depth0
))) {
1521 /* Create the source texture. */
1522 src
= screen
->resource_create(screen
, &src_templ
);
1527 /* Map source pixels. */
1528 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1529 format
, type
, pixels
, unpack
,
1532 /* This is a GL error. */
1533 pipe_resource_reference(&src
, NULL
);
1537 /* From now on, we need the gallium representation of dimensions. */
1538 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1545 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1546 width
, height
, depth
, &transfer
);
1548 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1549 pipe_resource_reference(&src
, NULL
);
1553 /* Upload pixels (just memcpy). */
1555 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1558 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1559 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1560 /* 1D array textures.
1561 * We need to convert gallium coords to GL coords.
1563 void *src
= _mesa_image_address2d(unpack
, pixels
,
1564 width
, depth
, format
,
1566 memcpy(map
, src
, bytesPerRow
);
1569 ubyte
*slice_map
= map
;
1571 for (row
= 0; row
< (unsigned) height
; row
++) {
1572 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1573 width
, height
, format
,
1574 type
, slice
, row
, 0);
1575 memcpy(slice_map
, src
, bytesPerRow
);
1576 slice_map
+= transfer
->stride
;
1579 map
+= transfer
->layer_stride
;
1583 pipe_transfer_unmap(pipe
, transfer
);
1584 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1587 memset(&blit
, 0, sizeof(blit
));
1588 blit
.src
.resource
= src
;
1590 blit
.src
.format
= src_format
;
1591 blit
.dst
.resource
= dst
;
1592 blit
.dst
.level
= dst_level
;
1593 blit
.dst
.format
= dst_format
;
1594 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1595 blit
.dst
.box
.x
= xoffset
;
1596 blit
.dst
.box
.y
= yoffset
;
1597 blit
.dst
.box
.z
= zoffset
+ dstz
;
1598 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1599 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1600 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1601 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1602 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1603 blit
.scissor_enable
= FALSE
;
1605 st
->pipe
->blit(st
->pipe
, &blit
);
1607 pipe_resource_reference(&src
, NULL
);
1611 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1612 width
, height
, depth
, format
, type
, pixels
,
1617 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1618 struct gl_texture_image
*texImage
,
1619 GLenum format
, GLenum type
, const void *pixels
,
1620 const struct gl_pixelstore_attrib
*unpack
)
1622 assert(dims
== 1 || dims
== 2 || dims
== 3);
1624 prep_teximage(ctx
, texImage
, format
, type
);
1626 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1629 /* allocate storage for texture data */
1630 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1631 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1635 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1636 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1637 format
, type
, pixels
, unpack
);
1642 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1643 struct gl_texture_image
*texImage
,
1644 GLint x
, GLint y
, GLint z
,
1645 GLsizei w
, GLsizei h
, GLsizei d
,
1646 GLenum format
, GLsizei imageSize
, const void *data
)
1648 struct st_context
*st
= st_context(ctx
);
1649 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1650 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1651 struct pipe_resource
*texture
= stImage
->pt
;
1652 struct pipe_context
*pipe
= st
->pipe
;
1653 struct pipe_screen
*screen
= pipe
->screen
;
1654 struct pipe_resource
*dst
= stImage
->pt
;
1655 struct pipe_surface
*surface
= NULL
;
1656 struct compressed_pixelstore store
;
1657 struct st_pbo_addresses addr
;
1658 enum pipe_format copy_format
;
1660 intptr_t buf_offset
;
1661 bool success
= false;
1663 /* Check basic pre-conditions for PBO upload */
1664 if (!st
->prefer_blit_based_texture_transfer
) {
1668 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1671 if (st_etc_fallback(st
, texImage
)) {
1672 /* ETC isn't supported and is represented by uncompressed formats. */
1680 if (!st
->pbo
.upload_enabled
||
1681 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1685 /* Choose the pipe format for the upload. */
1686 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1687 bw
= util_format_get_blockwidth(dst
->format
);
1688 bh
= util_format_get_blockheight(dst
->format
);
1690 switch (addr
.bytes_per_pixel
) {
1692 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1695 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1701 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0,
1702 PIPE_BIND_SAMPLER_VIEW
)) {
1706 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1707 dst
->nr_samples
, PIPE_BIND_RENDER_TARGET
)) {
1711 /* Interpret the pixelstore settings. */
1712 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1713 &ctx
->Unpack
, &store
);
1714 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1715 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1717 /* Compute the offset into the buffer */
1718 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1720 if (buf_offset
% addr
.bytes_per_pixel
) {
1724 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1726 addr
.xoffset
= x
/ bw
;
1727 addr
.yoffset
= y
/ bh
;
1728 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1729 addr
.height
= store
.CopyRowsPerSlice
;
1731 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1732 addr
.image_height
= store
.TotalRowsPerSlice
;
1734 if (!st_pbo_addresses_setup(st
, st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1738 /* Set up the surface. */
1740 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1741 unsigned max_layer
= util_max_layer(texture
, level
);
1743 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1745 struct pipe_surface templ
;
1746 memset(&templ
, 0, sizeof(templ
));
1747 templ
.format
= copy_format
;
1748 templ
.u
.tex
.level
= level
;
1749 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1750 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1752 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1757 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1759 pipe_surface_reference(&surface
, NULL
);
1765 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1767 format
, imageSize
, data
);
1771 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1772 struct gl_texture_image
*texImage
,
1773 GLsizei imageSize
, const void *data
)
1775 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1777 /* only 2D and 3D compressed images are supported at this time */
1779 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1783 /* This is pretty simple, because unlike the general texstore path we don't
1784 * have to worry about the usual image unpacking or image transfer
1788 assert(texImage
->Width
> 0);
1789 assert(texImage
->Height
> 0);
1790 assert(texImage
->Depth
> 0);
1792 /* allocate storage for texture data */
1793 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1794 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1798 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1800 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1801 texImage
->TexFormat
,
1809 * Called via ctx->Driver.GetTexSubImage()
1811 * This uses a blit to copy the texture to a texture format which matches
1812 * the format and type combo and then a fast read-back is done using memcpy.
1813 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1814 * a format which matches the swizzling.
1816 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1818 * NOTE: Drivers usually do a blit to convert between tiled and linear
1819 * texture layouts during texture uploads/downloads, so the blit
1820 * we do here should be free in such cases.
1823 st_GetTexSubImage(struct gl_context
* ctx
,
1824 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1825 GLsizei width
, GLsizei height
, GLint depth
,
1826 GLenum format
, GLenum type
, void * pixels
,
1827 struct gl_texture_image
*texImage
)
1829 struct st_context
*st
= st_context(ctx
);
1830 struct pipe_context
*pipe
= st
->pipe
;
1831 struct pipe_screen
*screen
= pipe
->screen
;
1832 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1833 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1834 struct pipe_resource
*src
= stObj
->pt
;
1835 struct pipe_resource
*dst
= NULL
;
1836 struct pipe_resource dst_templ
;
1837 enum pipe_format dst_format
, src_format
;
1838 mesa_format mesa_format
;
1839 GLenum gl_target
= texImage
->TexObject
->Target
;
1840 enum pipe_texture_target pipe_target
;
1842 struct pipe_blit_info blit
;
1844 struct pipe_transfer
*tex_xfer
;
1846 boolean done
= FALSE
;
1848 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1849 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1851 st_flush_bitmap_cache(st
);
1853 if (!st
->prefer_blit_based_texture_transfer
&&
1854 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1855 /* Try to avoid the fallback if we're doing texture decompression here */
1859 /* Handle non-finalized textures. */
1860 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1864 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1865 * due to an incomplete stencil blit implementation in some drivers. */
1866 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1870 /* If the base internal format and the texture format don't match, we have
1871 * to fall back to _mesa_GetTexImage_sw. */
1872 if (texImage
->_BaseFormat
!=
1873 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1877 /* See if the texture format already matches the format and type,
1878 * in which case the memcpy-based fast path will be used. */
1879 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1880 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1884 /* Convert the source format to what is expected by GetTexImage
1885 * and see if it's supported.
1887 * This only applies to glGetTexImage:
1888 * - Luminance must be returned as (L,0,0,1).
1889 * - Luminance alpha must be returned as (L,0,0,A).
1890 * - Intensity must be returned as (I,0,0,1)
1892 if (stObj
->surface_based
)
1893 src_format
= util_format_linear(stObj
->surface_format
);
1895 src_format
= util_format_linear(src
->format
);
1896 src_format
= util_format_luminance_to_red(src_format
);
1897 src_format
= util_format_intensity_to_red(src_format
);
1900 !screen
->is_format_supported(screen
, src_format
, src
->target
,
1902 PIPE_BIND_SAMPLER_VIEW
)) {
1906 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1907 bind
= PIPE_BIND_DEPTH_STENCIL
;
1909 bind
= PIPE_BIND_RENDER_TARGET
;
1911 /* GetTexImage only returns a single face for cubemaps. */
1912 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1913 gl_target
= GL_TEXTURE_2D
;
1915 pipe_target
= gl_target_to_pipe(gl_target
);
1917 /* Choose the destination format by finding the best match
1918 * for the format+type combo. */
1919 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1920 ctx
->Pack
.SwapBytes
);
1922 if (dst_format
== PIPE_FORMAT_NONE
) {
1923 GLenum dst_glformat
;
1925 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1926 * where decompression with a blit is always preferred. */
1927 if (!util_format_is_compressed(src
->format
)) {
1931 /* Set the appropriate format for the decompressed texture.
1932 * Luminance and sRGB formats shouldn't appear here.*/
1933 switch (src_format
) {
1934 case PIPE_FORMAT_DXT1_RGB
:
1935 case PIPE_FORMAT_DXT1_RGBA
:
1936 case PIPE_FORMAT_DXT3_RGBA
:
1937 case PIPE_FORMAT_DXT5_RGBA
:
1938 case PIPE_FORMAT_RGTC1_UNORM
:
1939 case PIPE_FORMAT_RGTC2_UNORM
:
1940 case PIPE_FORMAT_ETC1_RGB8
:
1941 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1942 dst_glformat
= GL_RGBA8
;
1944 case PIPE_FORMAT_RGTC1_SNORM
:
1945 case PIPE_FORMAT_RGTC2_SNORM
:
1946 if (!ctx
->Extensions
.EXT_texture_snorm
)
1948 dst_glformat
= GL_RGBA8_SNORM
;
1950 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1951 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1952 if (!ctx
->Extensions
.ARB_texture_float
)
1954 dst_glformat
= GL_RGBA32F
;
1961 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
1962 pipe_target
, 0, bind
, FALSE
);
1964 if (dst_format
== PIPE_FORMAT_NONE
) {
1965 /* unable to get an rgba format!?! */
1970 /* create the destination texture of size (width X height X depth) */
1971 memset(&dst_templ
, 0, sizeof(dst_templ
));
1972 dst_templ
.target
= pipe_target
;
1973 dst_templ
.format
= dst_format
;
1974 dst_templ
.bind
= bind
;
1975 dst_templ
.usage
= PIPE_USAGE_STAGING
;
1977 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1978 &dst_templ
.width0
, &dst_templ
.height0
,
1979 &dst_templ
.depth0
, &dst_templ
.array_size
);
1981 dst
= screen
->resource_create(screen
, &dst_templ
);
1986 /* From now on, we need the gallium representation of dimensions. */
1987 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1994 assert(texImage
->Face
== 0 ||
1995 texImage
->TexObject
->MinLayer
== 0 ||
1998 memset(&blit
, 0, sizeof(blit
));
1999 blit
.src
.resource
= src
;
2000 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2001 blit
.src
.format
= src_format
;
2002 blit
.dst
.resource
= dst
;
2004 blit
.dst
.format
= dst
->format
;
2005 blit
.src
.box
.x
= xoffset
;
2007 blit
.src
.box
.y
= yoffset
;
2009 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2011 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2012 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2013 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2014 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2015 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2016 blit
.scissor_enable
= FALSE
;
2018 /* blit/render/decompress */
2019 st
->pipe
->blit(st
->pipe
, &blit
);
2021 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2023 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2024 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2029 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2030 dims
= _mesa_get_texture_dimensions(gl_target
);
2032 /* copy/pack data into user buffer */
2033 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2034 ctx
->Pack
.SwapBytes
, NULL
)) {
2036 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2039 for (slice
= 0; slice
< depth
; slice
++) {
2040 ubyte
*slice_map
= map
;
2042 for (row
= 0; row
< height
; row
++) {
2043 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2044 width
, height
, format
, type
,
2047 memcpy(dest
, slice_map
, bytesPerRow
);
2049 slice_map
+= tex_xfer
->stride
;
2052 map
+= tex_xfer
->layer_stride
;
2056 /* format translation via floats */
2059 uint32_t dstMesaFormat
;
2060 int dstStride
, srcStride
;
2062 assert(util_format_is_compressed(src
->format
));
2064 rgba
= malloc(width
* height
* 4 * sizeof(GLfloat
));
2069 if (ST_DEBUG
& DEBUG_FALLBACK
)
2070 debug_printf("%s: fallback format translation\n", __func__
);
2072 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2073 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2074 srcStride
= 4 * width
* sizeof(GLfloat
);
2075 for (slice
= 0; slice
< depth
; slice
++) {
2076 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2077 width
, height
, format
, type
,
2080 /* get float[4] rgba row from surface */
2081 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, height
,
2084 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2085 rgba
, RGBA32_FLOAT
, srcStride
,
2086 width
, height
, NULL
);
2088 /* Handle byte swapping if required */
2089 if (ctx
->Pack
.SwapBytes
) {
2090 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
2091 width
, height
, dest
, dest
);
2094 map
+= tex_xfer
->layer_stride
;
2103 pipe_transfer_unmap(pipe
, tex_xfer
);
2105 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2106 pipe_resource_reference(&dst
, NULL
);
2110 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2111 width
, height
, depth
,
2112 format
, type
, pixels
, texImage
);
2118 * Do a CopyTexSubImage operation using a read transfer from the source,
2119 * a write transfer to the destination and get_tile()/put_tile() to access
2120 * the pixels/texels.
2122 * Note: srcY=0=TOP of renderbuffer
2125 fallback_copy_texsubimage(struct gl_context
*ctx
,
2126 struct st_renderbuffer
*strb
,
2127 struct st_texture_image
*stImage
,
2129 GLint destX
, GLint destY
, GLint slice
,
2130 GLint srcX
, GLint srcY
,
2131 GLsizei width
, GLsizei height
)
2133 struct st_context
*st
= st_context(ctx
);
2134 struct pipe_context
*pipe
= st
->pipe
;
2135 struct pipe_transfer
*src_trans
;
2137 enum pipe_transfer_usage transfer_usage
;
2139 unsigned dst_width
= width
;
2140 unsigned dst_height
= height
;
2141 unsigned dst_depth
= 1;
2142 struct pipe_transfer
*transfer
;
2144 if (ST_DEBUG
& DEBUG_FALLBACK
)
2145 debug_printf("%s: fallback processing\n", __func__
);
2147 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2148 srcY
= strb
->Base
.Height
- srcY
- height
;
2151 map
= pipe_transfer_map(pipe
,
2153 strb
->surface
->u
.tex
.level
,
2154 strb
->surface
->u
.tex
.first_layer
,
2157 width
, height
, &src_trans
);
2159 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2160 baseFormat
== GL_DEPTH_STENCIL
) &&
2161 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2162 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2164 transfer_usage
= PIPE_TRANSFER_WRITE
;
2166 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2167 destX
, destY
, slice
,
2168 dst_width
, dst_height
, dst_depth
,
2171 if (baseFormat
== GL_DEPTH_COMPONENT
||
2172 baseFormat
== GL_DEPTH_STENCIL
) {
2173 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2174 ctx
->Pixel
.DepthBias
!= 0.0F
);
2178 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2179 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2188 data
= malloc(width
* sizeof(uint
));
2191 /* To avoid a large temp memory allocation, do copy row by row */
2192 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2193 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2195 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2198 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2199 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2200 0, 0, width
, 1, data
);
2203 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2208 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2216 malloc(width
* height
* 4 * sizeof(GLfloat
));
2218 if (tempSrc
&& texDest
) {
2219 const GLint dims
= 2;
2221 struct gl_texture_image
*texImage
= &stImage
->base
;
2222 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2224 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2225 unpack
.Invert
= GL_TRUE
;
2228 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2229 dstRowStride
= transfer
->layer_stride
;
2232 dstRowStride
= transfer
->stride
;
2235 /* get float/RGBA image from framebuffer */
2236 /* XXX this usually involves a lot of int/float conversion.
2237 * try to avoid that someday.
2239 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2240 util_format_linear(strb
->texture
->format
),
2243 /* Store into texture memory.
2244 * Note that this does some special things such as pixel transfer
2245 * ops and format conversion. In particular, if the dest tex format
2246 * is actually RGBA but the user created the texture as GL_RGB we
2247 * need to fill-in/override the alpha channel with 1.0.
2249 _mesa_texstore(ctx
, dims
,
2250 texImage
->_BaseFormat
,
2251 texImage
->TexFormat
,
2255 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2259 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2265 st_texture_image_unmap(st
, stImage
, slice
);
2266 pipe
->transfer_unmap(pipe
, src_trans
);
2271 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2272 * Note that the region to copy has already been clipped so we know we
2273 * won't read from outside the source renderbuffer's bounds.
2275 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2278 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2279 struct gl_texture_image
*texImage
,
2280 GLint destX
, GLint destY
, GLint slice
,
2281 struct gl_renderbuffer
*rb
,
2282 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2284 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2285 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2286 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2287 struct st_context
*st
= st_context(ctx
);
2288 struct pipe_context
*pipe
= st
->pipe
;
2289 struct pipe_screen
*screen
= pipe
->screen
;
2290 struct pipe_blit_info blit
;
2291 enum pipe_format dst_format
;
2292 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2296 st_flush_bitmap_cache(st
);
2297 st_invalidate_readpix_cache(st
);
2299 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2300 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2302 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2303 debug_printf("%s: null strb or stImage\n", __func__
);
2307 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2308 texImage
->TexFormat
)) {
2312 /* The base internal format must match the mesa format, so make sure
2313 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
2315 if (texImage
->_BaseFormat
!=
2316 _mesa_get_format_base_format(texImage
->TexFormat
) ||
2317 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
2321 /* Choose the destination format to match the TexImage behavior. */
2322 dst_format
= util_format_linear(stImage
->pt
->format
);
2323 dst_format
= util_format_luminance_to_red(dst_format
);
2324 dst_format
= util_format_intensity_to_red(dst_format
);
2326 /* See if the destination format is supported. */
2327 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2328 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2329 bind
= PIPE_BIND_DEPTH_STENCIL
;
2332 bind
= PIPE_BIND_RENDER_TARGET
;
2336 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2337 stImage
->pt
->nr_samples
, bind
)) {
2341 /* Y flipping for the main framebuffer. */
2343 srcY1
= strb
->Base
.Height
- srcY
- height
;
2344 srcY0
= srcY1
+ height
;
2348 srcY1
= srcY0
+ height
;
2351 /* Blit the texture.
2352 * This supports flipping, format conversions, and downsampling.
2354 memset(&blit
, 0, sizeof(blit
));
2355 blit
.src
.resource
= strb
->texture
;
2356 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2357 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2358 blit
.src
.box
.x
= srcX
;
2359 blit
.src
.box
.y
= srcY0
;
2360 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2361 blit
.src
.box
.width
= width
;
2362 blit
.src
.box
.height
= srcY1
- srcY0
;
2363 blit
.src
.box
.depth
= 1;
2364 blit
.dst
.resource
= stImage
->pt
;
2365 blit
.dst
.format
= dst_format
;
2366 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2367 blit
.dst
.box
.x
= destX
;
2368 blit
.dst
.box
.y
= destY
;
2369 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2370 blit
.dst
.box
.width
= width
;
2371 blit
.dst
.box
.height
= height
;
2372 blit
.dst
.box
.depth
= 1;
2373 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2374 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2375 pipe
->blit(pipe
, &blit
);
2379 /* software fallback */
2380 fallback_copy_texsubimage(ctx
,
2381 strb
, stImage
, texImage
->_BaseFormat
,
2382 destX
, destY
, slice
,
2383 srcX
, srcY
, width
, height
);
2388 * Copy image data from stImage into the texture object 'stObj' at level
2392 copy_image_data_to_texture(struct st_context
*st
,
2393 struct st_texture_object
*stObj
,
2395 struct st_texture_image
*stImage
)
2399 const struct gl_texture_image MAYBE_UNUSED
*dstImage
=
2400 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2402 assert(dstImage
->Width
== stImage
->base
.Width
);
2403 assert(dstImage
->Height
== stImage
->base
.Height
);
2404 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2408 /* Copy potentially with the blitter:
2411 if (stImage
->pt
->last_level
== 0)
2414 src_level
= stImage
->base
.Level
;
2416 assert(src_level
<= stImage
->pt
->last_level
);
2417 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2418 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2419 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2420 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2421 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2422 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2424 st_texture_image_copy(st
->pipe
,
2425 stObj
->pt
, dstLevel
, /* dest texture, level */
2426 stImage
->pt
, src_level
, /* src texture, level */
2427 stImage
->base
.Face
);
2429 pipe_resource_reference(&stImage
->pt
, NULL
);
2431 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2436 * Called during state validation. When this function is finished,
2437 * the texture object should be ready for rendering.
2438 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2441 st_finalize_texture(struct gl_context
*ctx
,
2442 struct pipe_context
*pipe
,
2443 struct gl_texture_object
*tObj
,
2446 struct st_context
*st
= st_context(ctx
);
2447 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2448 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2450 const struct st_texture_image
*firstImage
;
2451 enum pipe_format firstImageFormat
;
2453 uint16_t ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2455 if (tObj
->Immutable
)
2458 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
2459 /* The texture is complete and we know exactly how many mipmap levels
2460 * are present/needed. This is conditional because we may be called
2461 * from the st_generate_mipmap() function when the texture object is
2462 * incomplete. In that case, we'll have set stObj->lastLevel before
2465 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
2466 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
2467 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2469 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2472 firstImage
= st_texture_image_const(stObj
->base
.Image
[cubeMapFace
][stObj
->base
.BaseLevel
]);
2475 /* Skip the loop over images in the common case of no images having
2476 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
2477 * haven't looked at, then we do need to look at those new images.
2479 if (!stObj
->needs_validation
&&
2480 stObj
->base
.BaseLevel
>= stObj
->validated_first_level
&&
2481 stObj
->lastLevel
<= stObj
->validated_last_level
) {
2485 /* If both firstImage and stObj point to a texture which can contain
2486 * all active images, favour firstImage. Note that because of the
2487 * completeness requirement, we know that the image dimensions
2490 if (firstImage
->pt
&&
2491 firstImage
->pt
!= stObj
->pt
&&
2492 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2493 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2494 st_texture_release_all_sampler_views(st
, stObj
);
2497 /* If this texture comes from a window system, there is nothing else to do. */
2498 if (stObj
->surface_based
) {
2502 /* Find gallium format for the Mesa texture */
2504 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2506 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2509 uint16_t height
, depth
;
2511 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2512 firstImage
->base
.Width2
,
2513 firstImage
->base
.Height2
,
2514 firstImage
->base
.Depth2
,
2515 &width
, &height
, &depth
, &ptLayers
);
2517 /* If we previously allocated a pipe texture and its sizes are
2518 * compatible, use them.
2521 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2522 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2523 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2524 ptWidth
= stObj
->pt
->width0
;
2525 ptHeight
= stObj
->pt
->height0
;
2526 ptDepth
= stObj
->pt
->depth0
;
2528 /* Otherwise, compute a new level=0 size that is compatible with the
2531 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2532 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2533 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2535 /* If the base level image is 1x1x1, we still need to ensure that the
2536 * resulting pipe texture ends up with the required number of levels
2539 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2540 ptWidth
<<= firstImage
->base
.Level
;
2542 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2543 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2547 /* At this point, the texture may be incomplete (mismatched cube
2548 * face sizes, for example). If that's the case, give up, but
2549 * don't return GL_FALSE as that would raise an incorrect
2550 * GL_OUT_OF_MEMORY error. See Piglit fbo-incomplete-texture-03 test.
2552 if (!stObj
->base
._BaseComplete
) {
2553 _mesa_test_texobj_completeness(ctx
, &stObj
->base
);
2554 if (!stObj
->base
._BaseComplete
) {
2560 ptNumSamples
= firstImage
->base
.NumSamples
;
2563 /* If we already have a gallium texture, check that it matches the texture
2564 * object's format, target, size, num_levels, etc.
2567 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2568 stObj
->pt
->format
!= firstImageFormat
||
2569 stObj
->pt
->last_level
< stObj
->lastLevel
||
2570 stObj
->pt
->width0
!= ptWidth
||
2571 stObj
->pt
->height0
!= ptHeight
||
2572 stObj
->pt
->depth0
!= ptDepth
||
2573 stObj
->pt
->nr_samples
!= ptNumSamples
||
2574 stObj
->pt
->array_size
!= ptLayers
)
2576 /* The gallium texture does not match the Mesa texture so delete the
2577 * gallium texture now. We'll make a new one below.
2579 pipe_resource_reference(&stObj
->pt
, NULL
);
2580 st_texture_release_all_sampler_views(st
, stObj
);
2581 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
2585 /* May need to create a new gallium texture:
2588 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2590 stObj
->pt
= st_texture_create(st
,
2591 gl_target_to_pipe(stObj
->base
.Target
),
2597 ptLayers
, ptNumSamples
,
2601 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2606 /* Pull in any images not in the object's texture:
2608 for (face
= 0; face
< nr_faces
; face
++) {
2610 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2611 struct st_texture_image
*stImage
=
2612 st_texture_image(stObj
->base
.Image
[face
][level
]);
2614 /* Need to import images in main memory or held in other textures.
2616 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2620 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2621 height
= u_minify(ptHeight
, level
);
2625 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2626 depth
= u_minify(ptDepth
, level
);
2627 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2633 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2634 stImage
->base
.Height
== height
&&
2635 stImage
->base
.Depth
== depth
)) {
2636 /* src image fits expected dest mipmap level size */
2637 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2643 stObj
->validated_first_level
= stObj
->base
.BaseLevel
;
2644 stObj
->validated_last_level
= stObj
->lastLevel
;
2645 stObj
->needs_validation
= false;
2651 * Allocate texture memory for a whole mipmap stack.
2652 * Note: for multisample textures if the requested sample count is not
2653 * supported, we search for the next higher supported sample count.
2656 st_texture_storage(struct gl_context
*ctx
,
2657 struct gl_texture_object
*texObj
,
2658 GLsizei levels
, GLsizei width
,
2659 GLsizei height
, GLsizei depth
)
2661 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2662 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2663 struct st_context
*st
= st_context(ctx
);
2664 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2665 struct pipe_screen
*screen
= st
->pipe
->screen
;
2666 unsigned ptWidth
, bindings
;
2667 uint16_t ptHeight
, ptDepth
, ptLayers
;
2668 enum pipe_format fmt
;
2670 GLuint num_samples
= texImage
->NumSamples
;
2674 stObj
->lastLevel
= levels
- 1;
2676 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2678 bindings
= default_bindings(st
, fmt
);
2680 /* Raise the sample count if the requested one is unsupported. */
2681 if (num_samples
> 0) {
2682 enum pipe_texture_target ptarget
= gl_target_to_pipe(texObj
->Target
);
2683 boolean found
= FALSE
;
2685 /* start the query with at least two samples */
2686 num_samples
= MAX2(num_samples
, 2);
2688 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2689 if (screen
->is_format_supported(screen
, fmt
, ptarget
,
2691 PIPE_BIND_SAMPLER_VIEW
)) {
2692 /* Update the sample count in gl_texture_image as well. */
2693 texImage
->NumSamples
= num_samples
;
2704 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2705 width
, height
, depth
,
2706 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2708 stObj
->pt
= st_texture_create(st
,
2709 gl_target_to_pipe(texObj
->Target
),
2715 ptLayers
, num_samples
,
2720 /* Set image resource pointers */
2721 for (level
= 0; level
< levels
; level
++) {
2723 for (face
= 0; face
< numFaces
; face
++) {
2724 struct st_texture_image
*stImage
=
2725 st_texture_image(texObj
->Image
[face
][level
]);
2726 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2728 etc_fallback_allocate(st
, stImage
);
2732 /* The texture is in a validated state, so no need to check later. */
2733 stObj
->needs_validation
= false;
2734 stObj
->validated_first_level
= 0;
2735 stObj
->validated_last_level
= levels
- 1;
2741 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2742 * for a whole mipmap stack.
2745 st_AllocTextureStorage(struct gl_context
*ctx
,
2746 struct gl_texture_object
*texObj
,
2747 GLsizei levels
, GLsizei width
,
2748 GLsizei height
, GLsizei depth
)
2750 return st_texture_storage(ctx
, texObj
, levels
,
2751 width
, height
, depth
);
2756 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2757 GLuint numLevels
, GLint level
,
2758 mesa_format format
, GLuint numSamples
,
2759 GLint width
, GLint height
, GLint depth
)
2761 struct st_context
*st
= st_context(ctx
);
2762 struct pipe_context
*pipe
= st
->pipe
;
2764 if (width
== 0 || height
== 0 || depth
== 0) {
2765 /* zero-sized images are legal, and always fit! */
2769 if (pipe
->screen
->can_create_resource
) {
2770 /* Ask the gallium driver if the texture is too large */
2771 struct gl_texture_object
*texObj
=
2772 _mesa_get_current_tex_object(ctx
, target
);
2773 struct pipe_resource pt
;
2775 /* Setup the pipe_resource object
2777 memset(&pt
, 0, sizeof(pt
));
2779 pt
.target
= gl_target_to_pipe(target
);
2780 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
2781 pt
.nr_samples
= numSamples
;
2783 st_gl_texture_dims_to_pipe_dims(target
,
2784 width
, height
, depth
,
2785 &pt
.width0
, &pt
.height0
,
2786 &pt
.depth0
, &pt
.array_size
);
2788 if (numLevels
> 0) {
2789 /* For immutable textures we know the final number of mip levels */
2790 pt
.last_level
= numLevels
- 1;
2792 else if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
2793 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
2794 /* assume just one mipmap level */
2798 /* assume a full set of mipmaps */
2799 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
2802 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
2805 /* Use core Mesa fallback */
2806 return _mesa_test_proxy_teximage(ctx
, target
, numLevels
, level
, format
,
2807 numSamples
, width
, height
, depth
);
2812 st_TextureView(struct gl_context
*ctx
,
2813 struct gl_texture_object
*texObj
,
2814 struct gl_texture_object
*origTexObj
)
2816 struct st_context
*st
= st_context(ctx
);
2817 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
2818 struct st_texture_object
*tex
= st_texture_object(texObj
);
2819 struct gl_texture_image
*image
= texObj
->Image
[0][0];
2821 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2822 const int numLevels
= texObj
->NumLevels
;
2827 pipe_resource_reference(&tex
->pt
, orig
->pt
);
2829 /* Set image resource pointers */
2830 for (level
= 0; level
< numLevels
; level
++) {
2831 for (face
= 0; face
< numFaces
; face
++) {
2832 struct st_texture_image
*stImage
=
2833 st_texture_image(texObj
->Image
[face
][level
]);
2834 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
2838 tex
->surface_based
= GL_TRUE
;
2839 tex
->surface_format
=
2840 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
2842 tex
->lastLevel
= numLevels
- 1;
2844 /* free texture sampler views. They need to be recreated when we
2845 * change the texture view parameters.
2847 st_texture_release_all_sampler_views(st
, tex
);
2849 /* The texture is in a validated state, so no need to check later. */
2850 tex
->needs_validation
= false;
2851 tex
->validated_first_level
= 0;
2852 tex
->validated_last_level
= numLevels
- 1;
2859 * Find the mipmap level in 'pt' which matches the level described by
2863 find_mipmap_level(const struct gl_texture_image
*texImage
,
2864 const struct pipe_resource
*pt
)
2866 const GLenum target
= texImage
->TexObject
->Target
;
2867 GLint texWidth
= texImage
->Width
;
2868 GLint texHeight
= texImage
->Height
;
2869 GLint texDepth
= texImage
->Depth
;
2871 uint16_t h
, d
, layers
;
2873 st_gl_texture_dims_to_pipe_dims(target
, texWidth
, texHeight
, texDepth
,
2874 &w
, &h
, &d
, &layers
);
2876 for (level
= 0; level
<= pt
->last_level
; level
++) {
2877 if (u_minify(pt
->width0
, level
) == w
&&
2878 u_minify(pt
->height0
, level
) == h
&&
2879 u_minify(pt
->depth0
, level
) == d
) {
2884 /* If we get here, there must be some sort of inconsistency between
2885 * the Mesa texture object/images and the gallium resource.
2887 debug_printf("Inconsistent textures in find_mipmap_level()\n");
2889 return texImage
->Level
;
2894 st_ClearTexSubImage(struct gl_context
*ctx
,
2895 struct gl_texture_image
*texImage
,
2896 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2897 GLsizei width
, GLsizei height
, GLsizei depth
,
2898 const void *clearValue
)
2900 static const char zeros
[16] = {0};
2901 struct gl_texture_object
*texObj
= texImage
->TexObject
;
2902 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2903 struct pipe_resource
*pt
= stImage
->pt
;
2904 struct st_context
*st
= st_context(ctx
);
2905 struct pipe_context
*pipe
= st
->pipe
;
2907 struct pipe_box box
;
2912 st_flush_bitmap_cache(st
);
2913 st_invalidate_readpix_cache(st
);
2915 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
2916 width
, height
, depth
, &box
);
2917 if (texObj
->Immutable
) {
2918 /* The texture object has to be consistent (no "loose", per-image
2919 * gallium resources). If this texture is a view into another
2920 * texture, we have to apply the MinLevel/Layer offsets. If this is
2921 * not a texture view, the offsets will be zero.
2923 assert(stImage
->pt
== st_texture_object(texObj
)->pt
);
2924 level
= texImage
->Level
+ texObj
->MinLevel
;
2925 box
.z
+= texObj
->MinLayer
;
2928 /* Texture level sizes may be inconsistent. We my have "loose",
2929 * per-image gallium resources. The texImage->Level may not match
2930 * the gallium resource texture level.
2932 level
= find_mipmap_level(texImage
, pt
);
2935 assert(level
<= pt
->last_level
);
2937 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
2942 * Called via the glTexParam*() function, but only when some texture object
2943 * state has actually changed.
2946 st_TexParameter(struct gl_context
*ctx
,
2947 struct gl_texture_object
*texObj
, GLenum pname
)
2949 struct st_context
*st
= st_context(ctx
);
2950 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2953 case GL_TEXTURE_BASE_LEVEL
:
2954 case GL_TEXTURE_MAX_LEVEL
:
2955 case GL_DEPTH_TEXTURE_MODE
:
2956 case GL_DEPTH_STENCIL_TEXTURE_MODE
:
2957 case GL_TEXTURE_SRGB_DECODE_EXT
:
2958 case GL_TEXTURE_SWIZZLE_R
:
2959 case GL_TEXTURE_SWIZZLE_G
:
2960 case GL_TEXTURE_SWIZZLE_B
:
2961 case GL_TEXTURE_SWIZZLE_A
:
2962 case GL_TEXTURE_SWIZZLE_RGBA
:
2963 case GL_TEXTURE_BUFFER_SIZE
:
2964 case GL_TEXTURE_BUFFER_OFFSET
:
2965 /* changing any of these texture parameters means we must create
2966 * new sampler views.
2968 st_texture_release_all_sampler_views(st
, stObj
);
2977 st_NewTextureHandle(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
2978 struct gl_sampler_object
*sampObj
)
2980 struct st_context
*st
= st_context(ctx
);
2981 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2982 struct pipe_context
*pipe
= st
->pipe
;
2983 struct pipe_sampler_view
*view
;
2984 struct pipe_sampler_state sampler
= {0};
2986 if (texObj
->Target
!= GL_TEXTURE_BUFFER
) {
2987 if (!st_finalize_texture(ctx
, pipe
, texObj
, 0))
2990 st_convert_sampler(st
, texObj
, sampObj
, 0, &sampler
);
2991 view
= st_get_texture_sampler_view_from_stobj(st
, stObj
, sampObj
, 0);
2993 view
= st_get_buffer_sampler_view_from_stobj(st
, stObj
);
2996 return pipe
->create_texture_handle(pipe
, view
, &sampler
);
3001 st_DeleteTextureHandle(struct gl_context
*ctx
, GLuint64 handle
)
3003 struct st_context
*st
= st_context(ctx
);
3004 struct pipe_context
*pipe
= st
->pipe
;
3006 pipe
->delete_texture_handle(pipe
, handle
);
3011 st_MakeTextureHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3014 struct st_context
*st
= st_context(ctx
);
3015 struct pipe_context
*pipe
= st
->pipe
;
3017 pipe
->make_texture_handle_resident(pipe
, handle
, resident
);
3022 st_NewImageHandle(struct gl_context
*ctx
, struct gl_image_unit
*imgObj
)
3024 struct st_context
*st
= st_context(ctx
);
3025 struct pipe_context
*pipe
= st
->pipe
;
3026 struct pipe_image_view image
;
3028 st_convert_image(st
, imgObj
, &image
);
3030 return pipe
->create_image_handle(pipe
, &image
);
3035 st_DeleteImageHandle(struct gl_context
*ctx
, GLuint64 handle
)
3037 struct st_context
*st
= st_context(ctx
);
3038 struct pipe_context
*pipe
= st
->pipe
;
3040 pipe
->delete_image_handle(pipe
, handle
);
3045 st_MakeImageHandleResident(struct gl_context
*ctx
, GLuint64 handle
,
3046 GLenum access
, bool resident
)
3048 struct st_context
*st
= st_context(ctx
);
3049 struct pipe_context
*pipe
= st
->pipe
;
3051 pipe
->make_image_handle_resident(pipe
, handle
, access
, resident
);
3056 st_init_texture_functions(struct dd_function_table
*functions
)
3058 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
3059 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
3060 functions
->TexImage
= st_TexImage
;
3061 functions
->TexSubImage
= st_TexSubImage
;
3062 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
3063 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
3064 functions
->GenerateMipmap
= st_generate_mipmap
;
3066 functions
->GetTexSubImage
= st_GetTexSubImage
;
3068 /* compressed texture functions */
3069 functions
->CompressedTexImage
= st_CompressedTexImage
;
3070 functions
->GetCompressedTexSubImage
= _mesa_GetCompressedTexSubImage_sw
;
3072 functions
->NewTextureObject
= st_NewTextureObject
;
3073 functions
->NewTextureImage
= st_NewTextureImage
;
3074 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
3075 functions
->DeleteTexture
= st_DeleteTextureObject
;
3076 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
3077 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
3078 functions
->MapTextureImage
= st_MapTextureImage
;
3079 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
3081 /* XXX Temporary until we can query pipe's texture sizes */
3082 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
3084 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
3085 functions
->TextureView
= st_TextureView
;
3086 functions
->ClearTexSubImage
= st_ClearTexSubImage
;
3088 functions
->TexParameter
= st_TexParameter
;
3090 /* bindless functions */
3091 functions
->NewTextureHandle
= st_NewTextureHandle
;
3092 functions
->DeleteTextureHandle
= st_DeleteTextureHandle
;
3093 functions
->MakeTextureHandleResident
= st_MakeTextureHandleResident
;
3094 functions
->NewImageHandle
= st_NewImageHandle
;
3095 functions
->DeleteImageHandle
= st_DeleteImageHandle
;
3096 functions
->MakeImageHandleResident
= st_MakeImageHandleResident
;