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
);
160 /** called via ctx->Driver.DeleteTextureObject() */
162 st_DeleteTextureObject(struct gl_context
*ctx
,
163 struct gl_texture_object
*texObj
)
165 struct st_context
*st
= st_context(ctx
);
166 struct st_texture_object
*stObj
= st_texture_object(texObj
);
168 pipe_resource_reference(&stObj
->pt
, NULL
);
169 st_texture_release_all_sampler_views(st
, stObj
);
170 st_texture_free_sampler_views(stObj
);
171 _mesa_delete_texture_object(ctx
, texObj
);
175 /** called via ctx->Driver.FreeTextureImageBuffer() */
177 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
178 struct gl_texture_image
*texImage
)
180 struct st_context
*st
= st_context(ctx
);
181 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
182 struct st_texture_image
*stImage
= st_texture_image(texImage
);
184 DBG("%s\n", __func__
);
187 pipe_resource_reference(&stImage
->pt
, NULL
);
190 free(stImage
->transfer
);
191 stImage
->transfer
= NULL
;
192 stImage
->num_transfers
= 0;
194 if (stImage
->etc_data
) {
195 free(stImage
->etc_data
);
196 stImage
->etc_data
= NULL
;
199 /* if the texture image is being deallocated, the structure of the
200 * texture is changing so we'll likely need a new sampler view.
202 st_texture_release_all_sampler_views(st
, stObj
);
206 st_etc_fallback(struct st_context
*st
, struct gl_texture_image
*texImage
)
208 return (_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
209 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
);
213 etc_fallback_allocate(struct st_context
*st
, struct st_texture_image
*stImage
)
215 struct gl_texture_image
*texImage
= &stImage
->base
;
217 if (!st_etc_fallback(st
, texImage
))
220 if (stImage
->etc_data
)
221 free(stImage
->etc_data
);
223 unsigned data_size
= _mesa_format_image_size(texImage
->TexFormat
,
229 malloc(data_size
* _mesa_num_tex_faces(texImage
->TexObject
->Target
));
232 /** called via ctx->Driver.MapTextureImage() */
234 st_MapTextureImage(struct gl_context
*ctx
,
235 struct gl_texture_image
*texImage
,
236 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
238 GLubyte
**mapOut
, GLint
*rowStrideOut
)
240 struct st_context
*st
= st_context(ctx
);
241 struct st_texture_image
*stImage
= st_texture_image(texImage
);
244 struct pipe_transfer
*transfer
;
247 if (mode
& GL_MAP_READ_BIT
)
248 pipeMode
|= PIPE_TRANSFER_READ
;
249 if (mode
& GL_MAP_WRITE_BIT
)
250 pipeMode
|= PIPE_TRANSFER_WRITE
;
251 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
252 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
254 map
= st_texture_image_map(st
, stImage
, pipeMode
, x
, y
, slice
, w
, h
, 1,
257 if (st_etc_fallback(st
, texImage
)) {
258 /* ETC isn't supported by all gallium drivers, where it's represented
259 * by uncompressed formats. We store the compressed data (as it's
260 * needed for image copies in OES_copy_image), and decompress as
261 * necessary in Unmap.
263 * Note: all ETC1/ETC2 formats have 4x4 block sizes.
265 unsigned z
= transfer
->box
.z
;
266 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
268 unsigned bytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
269 unsigned stride
= *rowStrideOut
= itransfer
->temp_stride
=
270 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width2
);
271 *mapOut
= itransfer
->temp_data
=
272 stImage
->etc_data
+ ((x
/ 4) * bytes
+ (y
/ 4) * stride
) +
273 z
* stride
* texImage
->Height2
/ 4;
274 itransfer
->map
= map
;
277 /* supported mapping */
279 *rowStrideOut
= transfer
->stride
;
289 /** called via ctx->Driver.UnmapTextureImage() */
291 st_UnmapTextureImage(struct gl_context
*ctx
,
292 struct gl_texture_image
*texImage
,
295 struct st_context
*st
= st_context(ctx
);
296 struct st_texture_image
*stImage
= st_texture_image(texImage
);
298 if (st_etc_fallback(st
, texImage
)) {
299 /* Decompress the ETC texture to the mapped one. */
300 unsigned z
= slice
+ stImage
->base
.Face
;
301 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
302 struct pipe_transfer
*transfer
= itransfer
->transfer
;
304 assert(z
== transfer
->box
.z
);
306 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
307 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
308 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
309 itransfer
->temp_data
,
310 itransfer
->temp_stride
,
311 transfer
->box
.width
, transfer
->box
.height
);
314 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
315 itransfer
->temp_data
, itransfer
->temp_stride
,
316 transfer
->box
.width
, transfer
->box
.height
,
317 texImage
->TexFormat
);
321 itransfer
->temp_data
= NULL
;
322 itransfer
->temp_stride
= 0;
326 st_texture_image_unmap(st
, stImage
, slice
);
331 * Return default texture resource binding bitmask for the given format.
334 default_bindings(struct st_context
*st
, enum pipe_format format
)
336 struct pipe_screen
*screen
= st
->pipe
->screen
;
337 const unsigned target
= PIPE_TEXTURE_2D
;
340 if (util_format_is_depth_or_stencil(format
))
341 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
343 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
345 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
349 format
= util_format_linear(format
);
351 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
354 return PIPE_BIND_SAMPLER_VIEW
;
360 * Given the size of a mipmap image, try to compute the size of the level=0
363 * Note that this isn't always accurate for odd-sized, non-POW textures.
364 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
366 * \return GL_TRUE for success, GL_FALSE for failure
369 guess_base_level_size(GLenum target
,
370 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
371 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
378 /* Guess the size of the base level.
379 * Depending on the image's size, we can't always make a guess here.
383 case GL_TEXTURE_1D_ARRAY
:
388 case GL_TEXTURE_2D_ARRAY
:
389 /* We can't make a good guess here, because the base level dimensions
392 if (width
== 1 || height
== 1) {
399 case GL_TEXTURE_CUBE_MAP
:
400 case GL_TEXTURE_CUBE_MAP_ARRAY
:
406 /* We can't make a good guess here, because the base level dimensions
409 if (width
== 1 || height
== 1 || depth
== 1) {
417 case GL_TEXTURE_RECTANGLE
:
434 * Try to determine whether we should allocate memory for a full texture
435 * mipmap. The problem is when we get a glTexImage(level=0) call, we
436 * can't immediately know if other mipmap levels are coming next. Here
437 * we try to guess whether to allocate memory for a mipmap or just the
440 * If we guess incorrectly here we'll later reallocate the right amount of
441 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
443 * \param stObj the texture object we're going to allocate memory for.
444 * \param stImage describes the incoming image which we need to store.
447 allocate_full_mipmap(const struct st_texture_object
*stObj
,
448 const struct st_texture_image
*stImage
)
450 switch (stObj
->base
.Target
) {
451 case GL_TEXTURE_RECTANGLE_NV
:
452 case GL_TEXTURE_BUFFER
:
453 case GL_TEXTURE_EXTERNAL_OES
:
454 case GL_TEXTURE_2D_MULTISAMPLE
:
455 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
456 /* these texture types cannot be mipmapped */
460 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
463 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
464 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
465 /* depth/stencil textures are seldom mipmapped */
468 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
471 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
472 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
473 /* not a mipmap minification filter */
476 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
477 /* 3D textures are seldom mipmapped */
485 * Try to allocate a pipe_resource object for the given st_texture_object.
487 * We use the given st_texture_image as a clue to determine the size of the
488 * mipmap image at level=0.
490 * \return GL_TRUE for success, GL_FALSE if out of memory.
493 guess_and_alloc_texture(struct st_context
*st
,
494 struct st_texture_object
*stObj
,
495 const struct st_texture_image
*stImage
)
497 const struct gl_texture_image
*firstImage
;
498 GLuint lastLevel
, width
, height
, depth
;
501 uint16_t ptHeight
, ptDepth
, ptLayers
;
502 enum pipe_format fmt
;
503 bool guessed_box
= false;
505 DBG("%s\n", __func__
);
509 /* If a base level image with compatible size exists, use that as our guess.
511 firstImage
= _mesa_base_tex_image(&stObj
->base
);
513 firstImage
->Width2
> 0 &&
514 firstImage
->Height2
> 0 &&
515 firstImage
->Depth2
> 0 &&
516 guess_base_level_size(stObj
->base
.Target
,
521 &width
, &height
, &depth
)) {
522 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
523 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
524 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
529 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
530 stImage
->base
.Width2
,
531 stImage
->base
.Height2
,
532 stImage
->base
.Depth2
,
534 &width
, &height
, &depth
);
537 /* we can't determine the image size at level=0 */
538 /* this is not an out of memory error */
542 /* At this point, (width x height x depth) is the expected size of
543 * the level=0 mipmap image.
546 /* Guess a reasonable value for lastLevel. With OpenGL we have no
547 * idea how many mipmap levels will be in a texture until we start
548 * to render with it. Make an educated guess here but be prepared
549 * to re-allocating a texture buffer with space for more (or fewer)
550 * mipmap levels later.
552 if (allocate_full_mipmap(stObj
, stImage
)) {
553 /* alloc space for a full mipmap */
554 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
555 width
, height
, depth
) - 1;
558 /* only alloc space for a single mipmap level */
562 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
564 bindings
= default_bindings(st
, fmt
);
566 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
567 width
, height
, depth
,
568 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
570 stObj
->pt
= st_texture_create(st
,
571 gl_target_to_pipe(stObj
->base
.Target
),
580 stObj
->lastLevel
= lastLevel
;
582 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
584 return stObj
->pt
!= NULL
;
589 * Called via ctx->Driver.AllocTextureImageBuffer().
590 * If the texture object/buffer already has space for the indicated image,
591 * we're done. Otherwise, allocate memory for the new texture image.
594 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
595 struct gl_texture_image
*texImage
)
597 struct st_context
*st
= st_context(ctx
);
598 struct st_texture_image
*stImage
= st_texture_image(texImage
);
599 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
600 const GLuint level
= texImage
->Level
;
601 GLuint width
= texImage
->Width
;
602 GLuint height
= texImage
->Height
;
603 GLuint depth
= texImage
->Depth
;
605 DBG("%s\n", __func__
);
607 assert(!stImage
->pt
); /* xxx this might be wrong */
609 etc_fallback_allocate(st
, stImage
);
611 /* Look if the parent texture object has space for this image */
613 level
<= stObj
->pt
->last_level
&&
614 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
615 /* this image will fit in the existing texture object's memory */
616 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
620 /* The parent texture object does not have space for this image */
622 pipe_resource_reference(&stObj
->pt
, NULL
);
623 st_texture_release_all_sampler_views(st
, stObj
);
625 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
626 /* Probably out of memory.
627 * Try flushing any pending rendering, then retry.
630 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
631 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
637 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
638 /* The image will live in the object's mipmap memory */
639 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
644 /* Create a new, temporary texture/resource/buffer to hold this
645 * one texture image. Note that when we later access this image
646 * (either for mapping or copying) we'll want to always specify
647 * mipmap level=0, even if the image represents some other mipmap
650 enum pipe_format format
=
651 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
652 GLuint bindings
= default_bindings(st
, format
);
654 uint16_t ptHeight
, ptDepth
, ptLayers
;
656 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
657 width
, height
, depth
,
658 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
660 stImage
->pt
= st_texture_create(st
,
661 gl_target_to_pipe(stObj
->base
.Target
),
669 return stImage
->pt
!= NULL
;
675 * Preparation prior to glTexImage. Basically check the 'surface_based'
676 * field and switch to a "normal" tex image if necessary.
679 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
680 GLenum format
, GLenum type
)
682 struct gl_texture_object
*texObj
= texImage
->TexObject
;
683 struct st_texture_object
*stObj
= st_texture_object(texObj
);
685 /* switch to "normal" */
686 if (stObj
->surface_based
) {
687 const GLenum target
= texObj
->Target
;
688 const GLuint level
= texImage
->Level
;
689 mesa_format texFormat
;
691 _mesa_clear_texture_object(ctx
, texObj
);
692 pipe_resource_reference(&stObj
->pt
, NULL
);
694 /* oops, need to init this image again */
695 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
696 texImage
->InternalFormat
, format
,
699 _mesa_init_teximage_fields(ctx
, texImage
,
700 texImage
->Width
, texImage
->Height
,
701 texImage
->Depth
, texImage
->Border
,
702 texImage
->InternalFormat
, texFormat
);
704 stObj
->surface_based
= GL_FALSE
;
710 * Return a writemask for the gallium blit. The parameters can be base
711 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
714 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
717 case GL_DEPTH_STENCIL
:
719 case GL_DEPTH_STENCIL
:
721 case GL_DEPTH_COMPONENT
:
723 case GL_STENCIL_INDEX
:
730 case GL_DEPTH_COMPONENT
:
732 case GL_DEPTH_STENCIL
:
733 case GL_DEPTH_COMPONENT
:
740 case GL_STENCIL_INDEX
:
742 case GL_STENCIL_INDEX
:
750 return PIPE_MASK_RGBA
;
755 * Converts format to a format with the same components, types
756 * and sizes, but with the components in RGBA order.
758 static enum pipe_format
759 unswizzle_format(enum pipe_format format
)
763 case PIPE_FORMAT_B8G8R8A8_UNORM
:
764 case PIPE_FORMAT_A8R8G8B8_UNORM
:
765 case PIPE_FORMAT_A8B8G8R8_UNORM
:
766 return PIPE_FORMAT_R8G8B8A8_UNORM
;
768 case PIPE_FORMAT_B10G10R10A2_UNORM
:
769 return PIPE_FORMAT_R10G10B10A2_UNORM
;
771 case PIPE_FORMAT_B10G10R10A2_SNORM
:
772 return PIPE_FORMAT_R10G10B10A2_SNORM
;
774 case PIPE_FORMAT_B10G10R10A2_UINT
:
775 return PIPE_FORMAT_R10G10B10A2_UINT
;
783 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
785 static enum pipe_format
786 alpha_to_red(enum pipe_format format
)
790 case PIPE_FORMAT_A8_UNORM
:
791 return PIPE_FORMAT_R8_UNORM
;
792 case PIPE_FORMAT_A8_SNORM
:
793 return PIPE_FORMAT_R8_SNORM
;
794 case PIPE_FORMAT_A8_UINT
:
795 return PIPE_FORMAT_R8_UINT
;
796 case PIPE_FORMAT_A8_SINT
:
797 return PIPE_FORMAT_R8_SINT
;
799 case PIPE_FORMAT_A16_UNORM
:
800 return PIPE_FORMAT_R16_UNORM
;
801 case PIPE_FORMAT_A16_SNORM
:
802 return PIPE_FORMAT_R16_SNORM
;
803 case PIPE_FORMAT_A16_UINT
:
804 return PIPE_FORMAT_R16_UINT
;
805 case PIPE_FORMAT_A16_SINT
:
806 return PIPE_FORMAT_R16_SINT
;
807 case PIPE_FORMAT_A16_FLOAT
:
808 return PIPE_FORMAT_R16_FLOAT
;
810 case PIPE_FORMAT_A32_UINT
:
811 return PIPE_FORMAT_R32_UINT
;
812 case PIPE_FORMAT_A32_SINT
:
813 return PIPE_FORMAT_R32_SINT
;
814 case PIPE_FORMAT_A32_FLOAT
:
815 return PIPE_FORMAT_R32_FLOAT
;
823 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
825 static enum pipe_format
826 red_alpha_to_red_green(enum pipe_format format
)
830 case PIPE_FORMAT_R8A8_UNORM
:
831 return PIPE_FORMAT_R8G8_UNORM
;
832 case PIPE_FORMAT_R8A8_SNORM
:
833 return PIPE_FORMAT_R8G8_SNORM
;
834 case PIPE_FORMAT_R8A8_UINT
:
835 return PIPE_FORMAT_R8G8_UINT
;
836 case PIPE_FORMAT_R8A8_SINT
:
837 return PIPE_FORMAT_R8G8_SINT
;
839 case PIPE_FORMAT_R16A16_UNORM
:
840 return PIPE_FORMAT_R16G16_UNORM
;
841 case PIPE_FORMAT_R16A16_SNORM
:
842 return PIPE_FORMAT_R16G16_SNORM
;
843 case PIPE_FORMAT_R16A16_UINT
:
844 return PIPE_FORMAT_R16G16_UINT
;
845 case PIPE_FORMAT_R16A16_SINT
:
846 return PIPE_FORMAT_R16G16_SINT
;
847 case PIPE_FORMAT_R16A16_FLOAT
:
848 return PIPE_FORMAT_R16G16_FLOAT
;
850 case PIPE_FORMAT_R32A32_UINT
:
851 return PIPE_FORMAT_R32G32_UINT
;
852 case PIPE_FORMAT_R32A32_SINT
:
853 return PIPE_FORMAT_R32G32_SINT
;
854 case PIPE_FORMAT_R32A32_FLOAT
:
855 return PIPE_FORMAT_R32G32_FLOAT
;
863 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
865 static enum pipe_format
866 luminance_alpha_to_red_green(enum pipe_format format
)
870 case PIPE_FORMAT_L8A8_UNORM
:
871 return PIPE_FORMAT_R8G8_UNORM
;
872 case PIPE_FORMAT_L8A8_SNORM
:
873 return PIPE_FORMAT_R8G8_SNORM
;
874 case PIPE_FORMAT_L8A8_UINT
:
875 return PIPE_FORMAT_R8G8_UINT
;
876 case PIPE_FORMAT_L8A8_SINT
:
877 return PIPE_FORMAT_R8G8_SINT
;
879 case PIPE_FORMAT_L16A16_UNORM
:
880 return PIPE_FORMAT_R16G16_UNORM
;
881 case PIPE_FORMAT_L16A16_SNORM
:
882 return PIPE_FORMAT_R16G16_SNORM
;
883 case PIPE_FORMAT_L16A16_UINT
:
884 return PIPE_FORMAT_R16G16_UINT
;
885 case PIPE_FORMAT_L16A16_SINT
:
886 return PIPE_FORMAT_R16G16_SINT
;
887 case PIPE_FORMAT_L16A16_FLOAT
:
888 return PIPE_FORMAT_R16G16_FLOAT
;
890 case PIPE_FORMAT_L32A32_UINT
:
891 return PIPE_FORMAT_R32G32_UINT
;
892 case PIPE_FORMAT_L32A32_SINT
:
893 return PIPE_FORMAT_R32G32_SINT
;
894 case PIPE_FORMAT_L32A32_FLOAT
:
895 return PIPE_FORMAT_R32G32_FLOAT
;
903 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
906 format_is_alpha(enum pipe_format format
)
908 const struct util_format_description
*desc
= util_format_description(format
);
910 if (desc
->nr_channels
== 1 &&
911 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
912 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
913 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
914 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
921 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
924 format_is_red(enum pipe_format format
)
926 const struct util_format_description
*desc
= util_format_description(format
);
928 if (desc
->nr_channels
== 1 &&
929 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
930 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
931 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
932 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
940 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
943 format_is_luminance(enum pipe_format format
)
945 const struct util_format_description
*desc
= util_format_description(format
);
947 if (desc
->nr_channels
== 1 &&
948 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
949 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
950 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
951 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
958 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
961 format_is_red_alpha(enum pipe_format format
)
963 const struct util_format_description
*desc
= util_format_description(format
);
965 if (desc
->nr_channels
== 2 &&
966 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
967 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
968 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
969 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
976 format_is_swizzled_rgba(enum pipe_format format
)
978 const struct util_format_description
*desc
= util_format_description(format
);
980 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
981 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
982 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
983 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
991 unsigned char swizzle
[4];
992 enum pipe_format format
;
995 static const struct format_table table_8888_unorm
[] = {
996 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
997 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
998 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
999 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
1002 static const struct format_table table_1010102_unorm
[] = {
1003 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
1004 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
1007 static const struct format_table table_1010102_snorm
[] = {
1008 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
1009 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
1012 static const struct format_table table_1010102_uint
[] = {
1013 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
1014 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
1017 static enum pipe_format
1018 swizzle_format(enum pipe_format format
, const int * const swizzle
)
1023 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1024 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1025 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1026 case PIPE_FORMAT_A8B8G8R8_UNORM
:
1027 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
1028 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
1029 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
1030 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
1031 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
1032 return table_8888_unorm
[i
].format
;
1036 case PIPE_FORMAT_R10G10B10A2_UNORM
:
1037 case PIPE_FORMAT_B10G10R10A2_UNORM
:
1038 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
1039 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
1040 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
1041 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
1042 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
1043 return table_1010102_unorm
[i
].format
;
1047 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1048 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1049 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1050 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1051 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1052 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1053 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1054 return table_1010102_snorm
[i
].format
;
1058 case PIPE_FORMAT_R10G10B10A2_UINT
:
1059 case PIPE_FORMAT_B10G10R10A2_UINT
:
1060 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1061 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1062 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1063 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1064 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1065 return table_1010102_uint
[i
].format
;
1073 return PIPE_FORMAT_NONE
;
1077 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1079 enum pipe_format src
= *src_format
;
1080 enum pipe_format dst
= *dst_format
;
1082 /* Note: dst_format has already been transformed from luminance/intensity
1083 * to red when this function is called. The source format will never
1084 * be an intensity format, because GL_INTENSITY is not a legal value
1085 * for the format parameter in glTex(Sub)Image(). */
1087 if (format_is_alpha(src
)) {
1088 if (!format_is_alpha(dst
))
1091 src
= alpha_to_red(src
);
1092 dst
= alpha_to_red(dst
);
1093 } else if (format_is_luminance(src
)) {
1094 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1097 src
= util_format_luminance_to_red(src
);
1098 } else if (util_format_is_luminance_alpha(src
)) {
1099 src
= luminance_alpha_to_red_green(src
);
1101 if (format_is_red_alpha(dst
)) {
1102 dst
= red_alpha_to_red_green(dst
);
1103 } else if (!format_is_red(dst
))
1105 } else if (format_is_swizzled_rgba(src
)) {
1106 const struct util_format_description
*src_desc
= util_format_description(src
);
1107 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1111 /* Make sure the format is an RGBA and not an RGBX format */
1112 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1115 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1118 for (i
= 0; i
< 4; i
++)
1119 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1121 dst
= swizzle_format(dst
, swizzle
);
1122 if (dst
== PIPE_FORMAT_NONE
)
1125 src
= unswizzle_format(src
);
1134 try_pbo_upload_common(struct gl_context
*ctx
,
1135 struct pipe_surface
*surface
,
1136 const struct st_pbo_addresses
*addr
,
1137 enum pipe_format src_format
)
1139 struct st_context
*st
= st_context(ctx
);
1140 struct cso_context
*cso
= st
->cso_context
;
1141 struct pipe_context
*pipe
= st
->pipe
;
1142 bool success
= false;
1145 fs
= st_pbo_get_upload_fs(st
, src_format
, surface
->format
);
1149 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1150 CSO_BIT_FRAGMENT_SAMPLERS
|
1151 CSO_BIT_VERTEX_ELEMENTS
|
1152 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1153 CSO_BIT_FRAMEBUFFER
|
1156 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1157 CSO_BIT_RASTERIZER
|
1158 CSO_BIT_STREAM_OUTPUTS
|
1159 CSO_BIT_PAUSE_QUERIES
|
1160 CSO_BIT_SAMPLE_MASK
|
1161 CSO_BIT_MIN_SAMPLES
|
1162 CSO_BIT_RENDER_CONDITION
|
1163 CSO_BITS_ALL_SHADERS
));
1164 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1166 cso_set_sample_mask(cso
, ~0);
1167 cso_set_min_samples(cso
, 1);
1168 cso_set_render_condition(cso
, NULL
, FALSE
, 0);
1170 /* Set up the sampler_view */
1172 struct pipe_sampler_view templ
;
1173 struct pipe_sampler_view
*sampler_view
;
1174 struct pipe_sampler_state sampler
= {0};
1175 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
1177 memset(&templ
, 0, sizeof(templ
));
1178 templ
.target
= PIPE_BUFFER
;
1179 templ
.format
= src_format
;
1180 templ
.u
.buf
.offset
= addr
->first_element
* addr
->bytes_per_pixel
;
1181 templ
.u
.buf
.size
= (addr
->last_element
- addr
->first_element
+ 1) *
1182 addr
->bytes_per_pixel
;
1183 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1184 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1185 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1186 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1188 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1189 if (sampler_view
== NULL
)
1192 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1194 pipe_sampler_view_reference(&sampler_view
, NULL
);
1196 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
1199 /* Framebuffer_state */
1201 struct pipe_framebuffer_state fb
;
1202 memset(&fb
, 0, sizeof(fb
));
1203 fb
.width
= surface
->width
;
1204 fb
.height
= surface
->height
;
1206 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1208 cso_set_framebuffer(cso
, &fb
);
1210 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1213 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1216 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1218 /* Depth/stencil/alpha state */
1220 struct pipe_depth_stencil_alpha_state dsa
;
1221 memset(&dsa
, 0, sizeof(dsa
));
1222 cso_set_depth_stencil_alpha(cso
, &dsa
);
1225 /* Set up the fragment shader */
1226 cso_set_fragment_shader_handle(cso
, fs
);
1228 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1231 cso_restore_state(cso
);
1232 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1238 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1239 struct gl_texture_image
*texImage
,
1240 GLenum format
, GLenum type
,
1241 enum pipe_format dst_format
,
1242 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1243 GLint width
, GLint height
, GLint depth
,
1245 const struct gl_pixelstore_attrib
*unpack
)
1247 struct st_context
*st
= st_context(ctx
);
1248 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1249 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1250 struct pipe_resource
*texture
= stImage
->pt
;
1251 struct pipe_context
*pipe
= st
->pipe
;
1252 struct pipe_screen
*screen
= pipe
->screen
;
1253 struct pipe_surface
*surface
= NULL
;
1254 struct st_pbo_addresses addr
;
1255 enum pipe_format src_format
;
1256 const struct util_format_description
*desc
;
1257 GLenum gl_target
= texImage
->TexObject
->Target
;
1260 if (!st
->pbo
.upload_enabled
)
1263 /* From now on, we need the gallium representation of dimensions. */
1264 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1271 if (depth
!= 1 && !st
->pbo
.layers
)
1274 /* Choose the source format. Initially, we do so without checking driver
1275 * support at all because of the remapping we later perform and because
1276 * at least the Radeon driver actually supports some formats for texture
1277 * buffers which it doesn't support for regular textures. */
1278 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1283 src_format
= util_format_linear(src_format
);
1284 desc
= util_format_description(src_format
);
1286 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1289 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1292 if (st
->pbo
.rgba_only
) {
1293 enum pipe_format orig_dst_format
= dst_format
;
1295 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1299 if (dst_format
!= orig_dst_format
&&
1300 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1301 PIPE_BIND_RENDER_TARGET
)) {
1307 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0,
1308 PIPE_BIND_SAMPLER_VIEW
)) {
1312 /* Compute buffer addresses */
1313 addr
.xoffset
= xoffset
;
1314 addr
.yoffset
= yoffset
;
1316 addr
.height
= height
;
1318 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1320 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1324 /* Set up the surface */
1326 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1327 unsigned max_layer
= util_max_layer(texture
, level
);
1329 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1331 struct pipe_surface templ
;
1332 memset(&templ
, 0, sizeof(templ
));
1333 templ
.format
= dst_format
;
1334 templ
.u
.tex
.level
= level
;
1335 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1336 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1338 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1343 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1345 pipe_surface_reference(&surface
, NULL
);
1351 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1352 struct gl_texture_image
*texImage
,
1353 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1354 GLint width
, GLint height
, GLint depth
,
1355 GLenum format
, GLenum type
, const void *pixels
,
1356 const struct gl_pixelstore_attrib
*unpack
)
1358 struct st_context
*st
= st_context(ctx
);
1359 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1360 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1361 struct pipe_context
*pipe
= st
->pipe
;
1362 struct pipe_screen
*screen
= pipe
->screen
;
1363 struct pipe_resource
*dst
= stImage
->pt
;
1364 struct pipe_resource
*src
= NULL
;
1365 struct pipe_resource src_templ
;
1366 struct pipe_transfer
*transfer
;
1367 struct pipe_blit_info blit
;
1368 enum pipe_format src_format
, dst_format
;
1369 mesa_format mesa_src_format
;
1370 GLenum gl_target
= texImage
->TexObject
->Target
;
1373 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1374 unsigned dst_level
= 0;
1376 st_flush_bitmap_cache(st
);
1377 st_invalidate_readpix_cache(st
);
1379 if (stObj
->pt
== stImage
->pt
)
1380 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1382 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1383 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1388 /* Try texture_subdata, which should be the fastest memcpy path. */
1390 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1391 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1392 texImage
->TexFormat
, format
, type
,
1394 struct pipe_box box
;
1395 unsigned stride
, layer_stride
;
1398 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1399 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1401 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1404 /* Convert to Gallium coordinates. */
1405 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1410 layer_stride
= stride
;
1413 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1414 pipe
->texture_subdata(pipe
, dst
, dst_level
, 0,
1415 &box
, data
, stride
, layer_stride
);
1419 if (!st
->prefer_blit_based_texture_transfer
) {
1423 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1424 * blit implementation in some drivers. */
1425 if (format
== GL_DEPTH_STENCIL
) {
1429 /* If the base internal format and the texture format don't match,
1430 * we can't use blit-based TexSubImage. */
1431 if (texImage
->_BaseFormat
!=
1432 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1437 /* See if the destination format is supported. */
1438 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1439 bind
= PIPE_BIND_DEPTH_STENCIL
;
1441 bind
= PIPE_BIND_RENDER_TARGET
;
1443 /* For luminance and intensity, only the red channel is stored
1444 * in the destination. */
1445 dst_format
= util_format_linear(dst
->format
);
1446 dst_format
= util_format_luminance_to_red(dst_format
);
1447 dst_format
= util_format_intensity_to_red(dst_format
);
1450 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1451 dst
->nr_samples
, bind
)) {
1455 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1456 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1457 xoffset
, yoffset
, zoffset
,
1458 width
, height
, depth
, pixels
, unpack
))
1462 /* See if the texture format already matches the format and type,
1463 * in which case the memcpy-based fast path will likely be used and
1464 * we don't have to blit. */
1465 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1466 type
, unpack
->SwapBytes
, NULL
)) {
1470 /* Choose the source format. */
1471 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1472 format
, type
, unpack
->SwapBytes
);
1477 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1479 /* There is no reason to do this if we cannot use memcpy for the temporary
1480 * source texture at least. This also takes transfer ops into account,
1482 if (!_mesa_texstore_can_use_memcpy(ctx
,
1483 _mesa_get_format_base_format(mesa_src_format
),
1484 mesa_src_format
, format
, type
, unpack
)) {
1488 /* TexSubImage only sets a single cubemap face. */
1489 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1490 gl_target
= GL_TEXTURE_2D
;
1492 /* TexSubImage can specify subsets of cube map array faces
1493 * so we need to upload via 2D array instead */
1494 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1495 gl_target
= GL_TEXTURE_2D_ARRAY
;
1498 /* Initialize the source texture description. */
1499 memset(&src_templ
, 0, sizeof(src_templ
));
1500 src_templ
.target
= gl_target_to_pipe(gl_target
);
1501 src_templ
.format
= src_format
;
1502 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1503 src_templ
.usage
= PIPE_USAGE_STAGING
;
1505 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1506 &src_templ
.width0
, &src_templ
.height0
,
1507 &src_templ
.depth0
, &src_templ
.array_size
);
1509 /* Check for NPOT texture support. */
1510 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1511 (!util_is_power_of_two(src_templ
.width0
) ||
1512 !util_is_power_of_two(src_templ
.height0
) ||
1513 !util_is_power_of_two(src_templ
.depth0
))) {
1517 /* Create the source texture. */
1518 src
= screen
->resource_create(screen
, &src_templ
);
1523 /* Map source pixels. */
1524 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1525 format
, type
, pixels
, unpack
,
1528 /* This is a GL error. */
1529 pipe_resource_reference(&src
, NULL
);
1533 /* From now on, we need the gallium representation of dimensions. */
1534 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1541 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1542 width
, height
, depth
, &transfer
);
1544 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1545 pipe_resource_reference(&src
, NULL
);
1549 /* Upload pixels (just memcpy). */
1551 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1554 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1555 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1556 /* 1D array textures.
1557 * We need to convert gallium coords to GL coords.
1559 void *src
= _mesa_image_address2d(unpack
, pixels
,
1560 width
, depth
, format
,
1562 memcpy(map
, src
, bytesPerRow
);
1565 ubyte
*slice_map
= map
;
1567 for (row
= 0; row
< (unsigned) height
; row
++) {
1568 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1569 width
, height
, format
,
1570 type
, slice
, row
, 0);
1571 memcpy(slice_map
, src
, bytesPerRow
);
1572 slice_map
+= transfer
->stride
;
1575 map
+= transfer
->layer_stride
;
1579 pipe_transfer_unmap(pipe
, transfer
);
1580 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1583 memset(&blit
, 0, sizeof(blit
));
1584 blit
.src
.resource
= src
;
1586 blit
.src
.format
= src_format
;
1587 blit
.dst
.resource
= dst
;
1588 blit
.dst
.level
= dst_level
;
1589 blit
.dst
.format
= dst_format
;
1590 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1591 blit
.dst
.box
.x
= xoffset
;
1592 blit
.dst
.box
.y
= yoffset
;
1593 blit
.dst
.box
.z
= zoffset
+ dstz
;
1594 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1595 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1596 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1597 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1598 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1599 blit
.scissor_enable
= FALSE
;
1601 st
->pipe
->blit(st
->pipe
, &blit
);
1603 pipe_resource_reference(&src
, NULL
);
1607 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1608 width
, height
, depth
, format
, type
, pixels
,
1613 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1614 struct gl_texture_image
*texImage
,
1615 GLenum format
, GLenum type
, const void *pixels
,
1616 const struct gl_pixelstore_attrib
*unpack
)
1618 assert(dims
== 1 || dims
== 2 || dims
== 3);
1620 prep_teximage(ctx
, texImage
, format
, type
);
1622 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1625 /* allocate storage for texture data */
1626 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1627 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1631 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1632 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1633 format
, type
, pixels
, unpack
);
1638 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1639 struct gl_texture_image
*texImage
,
1640 GLint x
, GLint y
, GLint z
,
1641 GLsizei w
, GLsizei h
, GLsizei d
,
1642 GLenum format
, GLsizei imageSize
, const void *data
)
1644 struct st_context
*st
= st_context(ctx
);
1645 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1646 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1647 struct pipe_resource
*texture
= stImage
->pt
;
1648 struct pipe_context
*pipe
= st
->pipe
;
1649 struct pipe_screen
*screen
= pipe
->screen
;
1650 struct pipe_resource
*dst
= stImage
->pt
;
1651 struct pipe_surface
*surface
= NULL
;
1652 struct compressed_pixelstore store
;
1653 struct st_pbo_addresses addr
;
1654 enum pipe_format copy_format
;
1656 intptr_t buf_offset
;
1657 bool success
= false;
1659 /* Check basic pre-conditions for PBO upload */
1660 if (!st
->prefer_blit_based_texture_transfer
) {
1664 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1667 if (st_etc_fallback(st
, texImage
)) {
1668 /* ETC isn't supported and is represented by uncompressed formats. */
1676 if (!st
->pbo
.upload_enabled
||
1677 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1681 /* Choose the pipe format for the upload. */
1682 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1683 bw
= util_format_get_blockwidth(dst
->format
);
1684 bh
= util_format_get_blockheight(dst
->format
);
1686 switch (addr
.bytes_per_pixel
) {
1688 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1691 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1697 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0,
1698 PIPE_BIND_SAMPLER_VIEW
)) {
1702 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1703 dst
->nr_samples
, PIPE_BIND_RENDER_TARGET
)) {
1707 /* Interpret the pixelstore settings. */
1708 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1709 &ctx
->Unpack
, &store
);
1710 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1711 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1713 /* Compute the offset into the buffer */
1714 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1716 if (buf_offset
% addr
.bytes_per_pixel
) {
1720 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1722 addr
.xoffset
= x
/ bw
;
1723 addr
.yoffset
= y
/ bh
;
1724 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1725 addr
.height
= store
.CopyRowsPerSlice
;
1727 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1728 addr
.image_height
= store
.TotalRowsPerSlice
;
1730 if (!st_pbo_addresses_setup(st
, st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1734 /* Set up the surface. */
1736 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1737 unsigned max_layer
= util_max_layer(texture
, level
);
1739 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1741 struct pipe_surface templ
;
1742 memset(&templ
, 0, sizeof(templ
));
1743 templ
.format
= copy_format
;
1744 templ
.u
.tex
.level
= level
;
1745 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1746 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1748 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1753 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1755 pipe_surface_reference(&surface
, NULL
);
1761 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1763 format
, imageSize
, data
);
1767 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1768 struct gl_texture_image
*texImage
,
1769 GLsizei imageSize
, const void *data
)
1771 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1773 /* only 2D and 3D compressed images are supported at this time */
1775 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1779 /* This is pretty simple, because unlike the general texstore path we don't
1780 * have to worry about the usual image unpacking or image transfer
1784 assert(texImage
->Width
> 0);
1785 assert(texImage
->Height
> 0);
1786 assert(texImage
->Depth
> 0);
1788 /* allocate storage for texture data */
1789 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1790 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1794 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1796 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1797 texImage
->TexFormat
,
1805 * Called via ctx->Driver.GetTexSubImage()
1807 * This uses a blit to copy the texture to a texture format which matches
1808 * the format and type combo and then a fast read-back is done using memcpy.
1809 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1810 * a format which matches the swizzling.
1812 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1814 * NOTE: Drivers usually do a blit to convert between tiled and linear
1815 * texture layouts during texture uploads/downloads, so the blit
1816 * we do here should be free in such cases.
1819 st_GetTexSubImage(struct gl_context
* ctx
,
1820 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1821 GLsizei width
, GLsizei height
, GLint depth
,
1822 GLenum format
, GLenum type
, void * pixels
,
1823 struct gl_texture_image
*texImage
)
1825 struct st_context
*st
= st_context(ctx
);
1826 struct pipe_context
*pipe
= st
->pipe
;
1827 struct pipe_screen
*screen
= pipe
->screen
;
1828 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1829 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1830 struct pipe_resource
*src
= stObj
->pt
;
1831 struct pipe_resource
*dst
= NULL
;
1832 struct pipe_resource dst_templ
;
1833 enum pipe_format dst_format
, src_format
;
1834 mesa_format mesa_format
;
1835 GLenum gl_target
= texImage
->TexObject
->Target
;
1836 enum pipe_texture_target pipe_target
;
1838 struct pipe_blit_info blit
;
1840 struct pipe_transfer
*tex_xfer
;
1842 boolean done
= FALSE
;
1844 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1845 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1847 st_flush_bitmap_cache(st
);
1849 if (!st
->prefer_blit_based_texture_transfer
&&
1850 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1851 /* Try to avoid the fallback if we're doing texture decompression here */
1855 /* Handle non-finalized textures. */
1856 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1860 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1861 * due to an incomplete stencil blit implementation in some drivers. */
1862 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1866 /* If the base internal format and the texture format don't match, we have
1867 * to fall back to _mesa_GetTexImage_sw. */
1868 if (texImage
->_BaseFormat
!=
1869 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1873 /* See if the texture format already matches the format and type,
1874 * in which case the memcpy-based fast path will be used. */
1875 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1876 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1880 /* Convert the source format to what is expected by GetTexImage
1881 * and see if it's supported.
1883 * This only applies to glGetTexImage:
1884 * - Luminance must be returned as (L,0,0,1).
1885 * - Luminance alpha must be returned as (L,0,0,A).
1886 * - Intensity must be returned as (I,0,0,1)
1888 if (stObj
->surface_based
)
1889 src_format
= util_format_linear(stObj
->surface_format
);
1891 src_format
= util_format_linear(src
->format
);
1892 src_format
= util_format_luminance_to_red(src_format
);
1893 src_format
= util_format_intensity_to_red(src_format
);
1896 !screen
->is_format_supported(screen
, src_format
, src
->target
,
1898 PIPE_BIND_SAMPLER_VIEW
)) {
1902 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1903 bind
= PIPE_BIND_DEPTH_STENCIL
;
1905 bind
= PIPE_BIND_RENDER_TARGET
;
1907 /* GetTexImage only returns a single face for cubemaps. */
1908 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1909 gl_target
= GL_TEXTURE_2D
;
1911 pipe_target
= gl_target_to_pipe(gl_target
);
1913 /* Choose the destination format by finding the best match
1914 * for the format+type combo. */
1915 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1916 ctx
->Pack
.SwapBytes
);
1918 if (dst_format
== PIPE_FORMAT_NONE
) {
1919 GLenum dst_glformat
;
1921 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1922 * where decompression with a blit is always preferred. */
1923 if (!util_format_is_compressed(src
->format
)) {
1927 /* Set the appropriate format for the decompressed texture.
1928 * Luminance and sRGB formats shouldn't appear here.*/
1929 switch (src_format
) {
1930 case PIPE_FORMAT_DXT1_RGB
:
1931 case PIPE_FORMAT_DXT1_RGBA
:
1932 case PIPE_FORMAT_DXT3_RGBA
:
1933 case PIPE_FORMAT_DXT5_RGBA
:
1934 case PIPE_FORMAT_RGTC1_UNORM
:
1935 case PIPE_FORMAT_RGTC2_UNORM
:
1936 case PIPE_FORMAT_ETC1_RGB8
:
1937 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1938 dst_glformat
= GL_RGBA8
;
1940 case PIPE_FORMAT_RGTC1_SNORM
:
1941 case PIPE_FORMAT_RGTC2_SNORM
:
1942 if (!ctx
->Extensions
.EXT_texture_snorm
)
1944 dst_glformat
= GL_RGBA8_SNORM
;
1946 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1947 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1948 if (!ctx
->Extensions
.ARB_texture_float
)
1950 dst_glformat
= GL_RGBA32F
;
1957 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
1958 pipe_target
, 0, bind
, FALSE
);
1960 if (dst_format
== PIPE_FORMAT_NONE
) {
1961 /* unable to get an rgba format!?! */
1966 /* create the destination texture of size (width X height X depth) */
1967 memset(&dst_templ
, 0, sizeof(dst_templ
));
1968 dst_templ
.target
= pipe_target
;
1969 dst_templ
.format
= dst_format
;
1970 dst_templ
.bind
= bind
;
1971 dst_templ
.usage
= PIPE_USAGE_STAGING
;
1973 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1974 &dst_templ
.width0
, &dst_templ
.height0
,
1975 &dst_templ
.depth0
, &dst_templ
.array_size
);
1977 dst
= screen
->resource_create(screen
, &dst_templ
);
1982 /* From now on, we need the gallium representation of dimensions. */
1983 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1990 assert(texImage
->Face
== 0 ||
1991 texImage
->TexObject
->MinLayer
== 0 ||
1994 memset(&blit
, 0, sizeof(blit
));
1995 blit
.src
.resource
= src
;
1996 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
1997 blit
.src
.format
= src_format
;
1998 blit
.dst
.resource
= dst
;
2000 blit
.dst
.format
= dst
->format
;
2001 blit
.src
.box
.x
= xoffset
;
2003 blit
.src
.box
.y
= yoffset
;
2005 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2007 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2008 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2009 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2010 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2011 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2012 blit
.scissor_enable
= FALSE
;
2014 /* blit/render/decompress */
2015 st
->pipe
->blit(st
->pipe
, &blit
);
2017 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2019 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2020 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2025 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2026 dims
= _mesa_get_texture_dimensions(gl_target
);
2028 /* copy/pack data into user buffer */
2029 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2030 ctx
->Pack
.SwapBytes
, NULL
)) {
2032 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2035 for (slice
= 0; slice
< depth
; slice
++) {
2036 ubyte
*slice_map
= map
;
2038 for (row
= 0; row
< height
; row
++) {
2039 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2040 width
, height
, format
, type
,
2043 memcpy(dest
, slice_map
, bytesPerRow
);
2045 slice_map
+= tex_xfer
->stride
;
2048 map
+= tex_xfer
->layer_stride
;
2052 /* format translation via floats */
2055 uint32_t dstMesaFormat
;
2056 int dstStride
, srcStride
;
2058 assert(util_format_is_compressed(src
->format
));
2060 rgba
= malloc(width
* height
* 4 * sizeof(GLfloat
));
2065 if (ST_DEBUG
& DEBUG_FALLBACK
)
2066 debug_printf("%s: fallback format translation\n", __func__
);
2068 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2069 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2070 srcStride
= 4 * width
* sizeof(GLfloat
);
2071 for (slice
= 0; slice
< depth
; slice
++) {
2072 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2073 width
, height
, format
, type
,
2076 /* get float[4] rgba row from surface */
2077 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, height
,
2080 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2081 rgba
, RGBA32_FLOAT
, srcStride
,
2082 width
, height
, NULL
);
2084 /* Handle byte swapping if required */
2085 if (ctx
->Pack
.SwapBytes
) {
2086 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
2087 width
, height
, dest
, dest
);
2090 map
+= tex_xfer
->layer_stride
;
2099 pipe_transfer_unmap(pipe
, tex_xfer
);
2101 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2102 pipe_resource_reference(&dst
, NULL
);
2106 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2107 width
, height
, depth
,
2108 format
, type
, pixels
, texImage
);
2114 * Do a CopyTexSubImage operation using a read transfer from the source,
2115 * a write transfer to the destination and get_tile()/put_tile() to access
2116 * the pixels/texels.
2118 * Note: srcY=0=TOP of renderbuffer
2121 fallback_copy_texsubimage(struct gl_context
*ctx
,
2122 struct st_renderbuffer
*strb
,
2123 struct st_texture_image
*stImage
,
2125 GLint destX
, GLint destY
, GLint slice
,
2126 GLint srcX
, GLint srcY
,
2127 GLsizei width
, GLsizei height
)
2129 struct st_context
*st
= st_context(ctx
);
2130 struct pipe_context
*pipe
= st
->pipe
;
2131 struct pipe_transfer
*src_trans
;
2133 enum pipe_transfer_usage transfer_usage
;
2135 unsigned dst_width
= width
;
2136 unsigned dst_height
= height
;
2137 unsigned dst_depth
= 1;
2138 struct pipe_transfer
*transfer
;
2140 if (ST_DEBUG
& DEBUG_FALLBACK
)
2141 debug_printf("%s: fallback processing\n", __func__
);
2143 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2144 srcY
= strb
->Base
.Height
- srcY
- height
;
2147 map
= pipe_transfer_map(pipe
,
2149 strb
->surface
->u
.tex
.level
,
2150 strb
->surface
->u
.tex
.first_layer
,
2153 width
, height
, &src_trans
);
2155 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2156 baseFormat
== GL_DEPTH_STENCIL
) &&
2157 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2158 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2160 transfer_usage
= PIPE_TRANSFER_WRITE
;
2162 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2163 destX
, destY
, slice
,
2164 dst_width
, dst_height
, dst_depth
,
2167 if (baseFormat
== GL_DEPTH_COMPONENT
||
2168 baseFormat
== GL_DEPTH_STENCIL
) {
2169 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2170 ctx
->Pixel
.DepthBias
!= 0.0F
);
2174 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2175 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2184 data
= malloc(width
* sizeof(uint
));
2187 /* To avoid a large temp memory allocation, do copy row by row */
2188 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2189 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2191 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2194 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2195 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2196 0, 0, width
, 1, data
);
2199 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2204 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2212 malloc(width
* height
* 4 * sizeof(GLfloat
));
2214 if (tempSrc
&& texDest
) {
2215 const GLint dims
= 2;
2217 struct gl_texture_image
*texImage
= &stImage
->base
;
2218 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2220 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2221 unpack
.Invert
= GL_TRUE
;
2224 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2225 dstRowStride
= transfer
->layer_stride
;
2228 dstRowStride
= transfer
->stride
;
2231 /* get float/RGBA image from framebuffer */
2232 /* XXX this usually involves a lot of int/float conversion.
2233 * try to avoid that someday.
2235 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2236 util_format_linear(strb
->texture
->format
),
2239 /* Store into texture memory.
2240 * Note that this does some special things such as pixel transfer
2241 * ops and format conversion. In particular, if the dest tex format
2242 * is actually RGBA but the user created the texture as GL_RGB we
2243 * need to fill-in/override the alpha channel with 1.0.
2245 _mesa_texstore(ctx
, dims
,
2246 texImage
->_BaseFormat
,
2247 texImage
->TexFormat
,
2251 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2255 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2261 st_texture_image_unmap(st
, stImage
, slice
);
2262 pipe
->transfer_unmap(pipe
, src_trans
);
2267 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2268 * Note that the region to copy has already been clipped so we know we
2269 * won't read from outside the source renderbuffer's bounds.
2271 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2274 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2275 struct gl_texture_image
*texImage
,
2276 GLint destX
, GLint destY
, GLint slice
,
2277 struct gl_renderbuffer
*rb
,
2278 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2280 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2281 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2282 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2283 struct st_context
*st
= st_context(ctx
);
2284 struct pipe_context
*pipe
= st
->pipe
;
2285 struct pipe_screen
*screen
= pipe
->screen
;
2286 struct pipe_blit_info blit
;
2287 enum pipe_format dst_format
;
2288 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2292 st_flush_bitmap_cache(st
);
2293 st_invalidate_readpix_cache(st
);
2295 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2296 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2298 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2299 debug_printf("%s: null strb or stImage\n", __func__
);
2303 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2304 texImage
->TexFormat
)) {
2308 /* The base internal format must match the mesa format, so make sure
2309 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
2311 if (texImage
->_BaseFormat
!=
2312 _mesa_get_format_base_format(texImage
->TexFormat
) ||
2313 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
2317 /* Choose the destination format to match the TexImage behavior. */
2318 dst_format
= util_format_linear(stImage
->pt
->format
);
2319 dst_format
= util_format_luminance_to_red(dst_format
);
2320 dst_format
= util_format_intensity_to_red(dst_format
);
2322 /* See if the destination format is supported. */
2323 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2324 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2325 bind
= PIPE_BIND_DEPTH_STENCIL
;
2328 bind
= PIPE_BIND_RENDER_TARGET
;
2332 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2333 stImage
->pt
->nr_samples
, bind
)) {
2337 /* Y flipping for the main framebuffer. */
2339 srcY1
= strb
->Base
.Height
- srcY
- height
;
2340 srcY0
= srcY1
+ height
;
2344 srcY1
= srcY0
+ height
;
2347 /* Blit the texture.
2348 * This supports flipping, format conversions, and downsampling.
2350 memset(&blit
, 0, sizeof(blit
));
2351 blit
.src
.resource
= strb
->texture
;
2352 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2353 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2354 blit
.src
.box
.x
= srcX
;
2355 blit
.src
.box
.y
= srcY0
;
2356 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2357 blit
.src
.box
.width
= width
;
2358 blit
.src
.box
.height
= srcY1
- srcY0
;
2359 blit
.src
.box
.depth
= 1;
2360 blit
.dst
.resource
= stImage
->pt
;
2361 blit
.dst
.format
= dst_format
;
2362 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2363 blit
.dst
.box
.x
= destX
;
2364 blit
.dst
.box
.y
= destY
;
2365 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2366 blit
.dst
.box
.width
= width
;
2367 blit
.dst
.box
.height
= height
;
2368 blit
.dst
.box
.depth
= 1;
2369 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2370 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2371 pipe
->blit(pipe
, &blit
);
2375 /* software fallback */
2376 fallback_copy_texsubimage(ctx
,
2377 strb
, stImage
, texImage
->_BaseFormat
,
2378 destX
, destY
, slice
,
2379 srcX
, srcY
, width
, height
);
2384 * Copy image data from stImage into the texture object 'stObj' at level
2388 copy_image_data_to_texture(struct st_context
*st
,
2389 struct st_texture_object
*stObj
,
2391 struct st_texture_image
*stImage
)
2395 const struct gl_texture_image
*dstImage
=
2396 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2398 assert(dstImage
->Width
== stImage
->base
.Width
);
2399 assert(dstImage
->Height
== stImage
->base
.Height
);
2400 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2404 /* Copy potentially with the blitter:
2407 if (stImage
->pt
->last_level
== 0)
2410 src_level
= stImage
->base
.Level
;
2412 assert(src_level
<= stImage
->pt
->last_level
);
2413 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2414 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2415 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2416 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2417 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2418 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2420 st_texture_image_copy(st
->pipe
,
2421 stObj
->pt
, dstLevel
, /* dest texture, level */
2422 stImage
->pt
, src_level
, /* src texture, level */
2423 stImage
->base
.Face
);
2425 pipe_resource_reference(&stImage
->pt
, NULL
);
2427 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2432 * Called during state validation. When this function is finished,
2433 * the texture object should be ready for rendering.
2434 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2437 st_finalize_texture(struct gl_context
*ctx
,
2438 struct pipe_context
*pipe
,
2439 struct gl_texture_object
*tObj
,
2442 struct st_context
*st
= st_context(ctx
);
2443 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2444 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2446 const struct st_texture_image
*firstImage
;
2447 enum pipe_format firstImageFormat
;
2449 uint16_t ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2451 if (tObj
->Immutable
)
2454 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
2455 /* The texture is complete and we know exactly how many mipmap levels
2456 * are present/needed. This is conditional because we may be called
2457 * from the st_generate_mipmap() function when the texture object is
2458 * incomplete. In that case, we'll have set stObj->lastLevel before
2461 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
2462 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
2463 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2465 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2468 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
2469 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
2472 pipe_resource_reference(&stObj
->pt
, NULL
);
2473 st_texture_release_all_sampler_views(st
, stObj
);
2477 if (st_obj
->buffer
!= stObj
->pt
) {
2478 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
2479 st_texture_release_all_sampler_views(st
, stObj
);
2485 firstImage
= st_texture_image_const(stObj
->base
.Image
[cubeMapFace
][stObj
->base
.BaseLevel
]);
2488 /* If both firstImage and stObj point to a texture which can contain
2489 * all active images, favour firstImage. Note that because of the
2490 * completeness requirement, we know that the image dimensions
2493 if (firstImage
->pt
&&
2494 firstImage
->pt
!= stObj
->pt
&&
2495 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2496 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2497 st_texture_release_all_sampler_views(st
, stObj
);
2500 /* If this texture comes from a window system, there is nothing else to do. */
2501 if (stObj
->surface_based
) {
2505 /* Find gallium format for the Mesa texture */
2507 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2509 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2512 uint16_t height
, depth
;
2514 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2515 firstImage
->base
.Width2
,
2516 firstImage
->base
.Height2
,
2517 firstImage
->base
.Depth2
,
2518 &width
, &height
, &depth
, &ptLayers
);
2520 /* If we previously allocated a pipe texture and its sizes are
2521 * compatible, use them.
2524 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2525 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2526 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2527 ptWidth
= stObj
->pt
->width0
;
2528 ptHeight
= stObj
->pt
->height0
;
2529 ptDepth
= stObj
->pt
->depth0
;
2531 /* Otherwise, compute a new level=0 size that is compatible with the
2534 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2535 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2536 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2538 /* If the base level image is 1x1x1, we still need to ensure that the
2539 * resulting pipe texture ends up with the required number of levels
2542 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2543 ptWidth
<<= firstImage
->base
.Level
;
2545 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2546 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2551 ptNumSamples
= firstImage
->base
.NumSamples
;
2554 /* If we already have a gallium texture, check that it matches the texture
2555 * object's format, target, size, num_levels, etc.
2558 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2559 stObj
->pt
->format
!= firstImageFormat
||
2560 stObj
->pt
->last_level
< stObj
->lastLevel
||
2561 stObj
->pt
->width0
!= ptWidth
||
2562 stObj
->pt
->height0
!= ptHeight
||
2563 stObj
->pt
->depth0
!= ptDepth
||
2564 stObj
->pt
->nr_samples
!= ptNumSamples
||
2565 stObj
->pt
->array_size
!= ptLayers
)
2567 /* The gallium texture does not match the Mesa texture so delete the
2568 * gallium texture now. We'll make a new one below.
2570 pipe_resource_reference(&stObj
->pt
, NULL
);
2571 st_texture_release_all_sampler_views(st
, stObj
);
2572 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
2576 /* May need to create a new gallium texture:
2579 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2581 stObj
->pt
= st_texture_create(st
,
2582 gl_target_to_pipe(stObj
->base
.Target
),
2588 ptLayers
, ptNumSamples
,
2592 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2597 /* Pull in any images not in the object's texture:
2599 for (face
= 0; face
< nr_faces
; face
++) {
2601 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2602 struct st_texture_image
*stImage
=
2603 st_texture_image(stObj
->base
.Image
[face
][level
]);
2605 /* Need to import images in main memory or held in other textures.
2607 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2611 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2612 height
= u_minify(ptHeight
, level
);
2616 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2617 depth
= u_minify(ptDepth
, level
);
2618 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2624 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2625 stImage
->base
.Height
== height
&&
2626 stImage
->base
.Depth
== depth
)) {
2627 /* src image fits expected dest mipmap level size */
2628 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2639 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2640 * for a whole mipmap stack.
2643 st_AllocTextureStorage(struct gl_context
*ctx
,
2644 struct gl_texture_object
*texObj
,
2645 GLsizei levels
, GLsizei width
,
2646 GLsizei height
, GLsizei depth
)
2648 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2649 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2650 struct st_context
*st
= st_context(ctx
);
2651 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2652 struct pipe_screen
*screen
= st
->pipe
->screen
;
2653 unsigned ptWidth
, bindings
;
2654 uint16_t ptHeight
, ptDepth
, ptLayers
;
2655 enum pipe_format fmt
;
2657 GLuint num_samples
= texImage
->NumSamples
;
2661 stObj
->lastLevel
= levels
- 1;
2663 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2665 bindings
= default_bindings(st
, fmt
);
2667 /* Raise the sample count if the requested one is unsupported. */
2668 if (num_samples
> 1) {
2669 boolean found
= FALSE
;
2671 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2672 if (screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
2674 PIPE_BIND_SAMPLER_VIEW
)) {
2675 /* Update the sample count in gl_texture_image as well. */
2676 texImage
->NumSamples
= num_samples
;
2687 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2688 width
, height
, depth
,
2689 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2691 stObj
->pt
= st_texture_create(st
,
2692 gl_target_to_pipe(texObj
->Target
),
2698 ptLayers
, num_samples
,
2703 /* Set image resource pointers */
2704 for (level
= 0; level
< levels
; level
++) {
2706 for (face
= 0; face
< numFaces
; face
++) {
2707 struct st_texture_image
*stImage
=
2708 st_texture_image(texObj
->Image
[face
][level
]);
2709 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2711 etc_fallback_allocate(st
, stImage
);
2720 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2721 GLuint numLevels
, GLint level
,
2722 mesa_format format
, GLuint numSamples
,
2723 GLint width
, GLint height
, GLint depth
)
2725 struct st_context
*st
= st_context(ctx
);
2726 struct pipe_context
*pipe
= st
->pipe
;
2728 if (width
== 0 || height
== 0 || depth
== 0) {
2729 /* zero-sized images are legal, and always fit! */
2733 if (pipe
->screen
->can_create_resource
) {
2734 /* Ask the gallium driver if the texture is too large */
2735 struct gl_texture_object
*texObj
=
2736 _mesa_get_current_tex_object(ctx
, target
);
2737 struct pipe_resource pt
;
2739 /* Setup the pipe_resource object
2741 memset(&pt
, 0, sizeof(pt
));
2743 pt
.target
= gl_target_to_pipe(target
);
2744 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
2745 pt
.nr_samples
= numSamples
;
2747 st_gl_texture_dims_to_pipe_dims(target
,
2748 width
, height
, depth
,
2749 &pt
.width0
, &pt
.height0
,
2750 &pt
.depth0
, &pt
.array_size
);
2752 if (numLevels
> 0) {
2753 /* For immutable textures we know the final number of mip levels */
2754 pt
.last_level
= numLevels
- 1;
2756 else if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
2757 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
2758 /* assume just one mipmap level */
2762 /* assume a full set of mipmaps */
2763 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
2766 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
2769 /* Use core Mesa fallback */
2770 return _mesa_test_proxy_teximage(ctx
, target
, numLevels
, level
, format
,
2771 numSamples
, width
, height
, depth
);
2776 st_TextureView(struct gl_context
*ctx
,
2777 struct gl_texture_object
*texObj
,
2778 struct gl_texture_object
*origTexObj
)
2780 struct st_context
*st
= st_context(ctx
);
2781 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
2782 struct st_texture_object
*tex
= st_texture_object(texObj
);
2783 struct gl_texture_image
*image
= texObj
->Image
[0][0];
2785 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2786 const int numLevels
= texObj
->NumLevels
;
2791 pipe_resource_reference(&tex
->pt
, orig
->pt
);
2793 /* Set image resource pointers */
2794 for (level
= 0; level
< numLevels
; level
++) {
2795 for (face
= 0; face
< numFaces
; face
++) {
2796 struct st_texture_image
*stImage
=
2797 st_texture_image(texObj
->Image
[face
][level
]);
2798 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
2802 tex
->surface_based
= GL_TRUE
;
2803 tex
->surface_format
=
2804 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
2806 tex
->lastLevel
= numLevels
- 1;
2808 /* free texture sampler views. They need to be recreated when we
2809 * change the texture view parameters.
2811 st_texture_release_all_sampler_views(st
, tex
);
2817 st_ClearTexSubImage(struct gl_context
*ctx
,
2818 struct gl_texture_image
*texImage
,
2819 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2820 GLsizei width
, GLsizei height
, GLsizei depth
,
2821 const void *clearValue
)
2823 static const char zeros
[16] = {0};
2824 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2825 struct pipe_resource
*pt
= stImage
->pt
;
2826 struct st_context
*st
= st_context(ctx
);
2827 struct pipe_context
*pipe
= st
->pipe
;
2828 unsigned level
= texImage
->Level
;
2829 struct pipe_box box
;
2834 st_flush_bitmap_cache(st
);
2835 st_invalidate_readpix_cache(st
);
2837 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
2838 width
, height
, depth
, &box
);
2839 if (texImage
->TexObject
->Immutable
) {
2840 level
+= texImage
->TexObject
->MinLevel
;
2841 box
.z
+= texImage
->TexObject
->MinLayer
;
2844 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
2849 * Called via the glTexParam*() function, but only when some texture object
2850 * state has actually changed.
2853 st_TexParameter(struct gl_context
*ctx
,
2854 struct gl_texture_object
*texObj
, GLenum pname
)
2856 struct st_context
*st
= st_context(ctx
);
2857 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2860 case GL_TEXTURE_BASE_LEVEL
:
2861 case GL_TEXTURE_MAX_LEVEL
:
2862 case GL_DEPTH_TEXTURE_MODE
:
2863 case GL_DEPTH_STENCIL_TEXTURE_MODE
:
2864 case GL_TEXTURE_SRGB_DECODE_EXT
:
2865 case GL_TEXTURE_SWIZZLE_R
:
2866 case GL_TEXTURE_SWIZZLE_G
:
2867 case GL_TEXTURE_SWIZZLE_B
:
2868 case GL_TEXTURE_SWIZZLE_A
:
2869 case GL_TEXTURE_SWIZZLE_RGBA
:
2870 case GL_TEXTURE_BUFFER_SIZE
:
2871 case GL_TEXTURE_BUFFER_OFFSET
:
2872 /* changing any of these texture parameters means we must create
2873 * new sampler views.
2875 st_texture_release_all_sampler_views(st
, stObj
);
2884 st_init_texture_functions(struct dd_function_table
*functions
)
2886 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
2887 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
2888 functions
->TexImage
= st_TexImage
;
2889 functions
->TexSubImage
= st_TexSubImage
;
2890 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
2891 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
2892 functions
->GenerateMipmap
= st_generate_mipmap
;
2894 functions
->GetTexSubImage
= st_GetTexSubImage
;
2896 /* compressed texture functions */
2897 functions
->CompressedTexImage
= st_CompressedTexImage
;
2898 functions
->GetCompressedTexSubImage
= _mesa_GetCompressedTexSubImage_sw
;
2900 functions
->NewTextureObject
= st_NewTextureObject
;
2901 functions
->NewTextureImage
= st_NewTextureImage
;
2902 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
2903 functions
->DeleteTexture
= st_DeleteTextureObject
;
2904 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
2905 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
2906 functions
->MapTextureImage
= st_MapTextureImage
;
2907 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
2909 /* XXX Temporary until we can query pipe's texture sizes */
2910 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
2912 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
2913 functions
->TextureView
= st_TextureView
;
2914 functions
->ClearTexSubImage
= st_ClearTexSubImage
;
2916 functions
->TexParameter
= st_TexParameter
;