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
;
500 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
501 enum pipe_format fmt
;
502 bool guessed_box
= false;
504 DBG("%s\n", __func__
);
508 /* If a base level image with compatible size exists, use that as our guess.
510 firstImage
= _mesa_base_tex_image(&stObj
->base
);
512 firstImage
->Width2
> 0 &&
513 firstImage
->Height2
> 0 &&
514 firstImage
->Depth2
> 0 &&
515 guess_base_level_size(stObj
->base
.Target
,
520 &width
, &height
, &depth
)) {
521 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
522 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
523 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
528 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
529 stImage
->base
.Width2
,
530 stImage
->base
.Height2
,
531 stImage
->base
.Depth2
,
533 &width
, &height
, &depth
);
536 /* we can't determine the image size at level=0 */
537 /* this is not an out of memory error */
541 /* At this point, (width x height x depth) is the expected size of
542 * the level=0 mipmap image.
545 /* Guess a reasonable value for lastLevel. With OpenGL we have no
546 * idea how many mipmap levels will be in a texture until we start
547 * to render with it. Make an educated guess here but be prepared
548 * to re-allocating a texture buffer with space for more (or fewer)
549 * mipmap levels later.
551 if (allocate_full_mipmap(stObj
, stImage
)) {
552 /* alloc space for a full mipmap */
553 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
554 width
, height
, depth
) - 1;
557 /* only alloc space for a single mipmap level */
561 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
563 bindings
= default_bindings(st
, fmt
);
565 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
566 width
, height
, depth
,
567 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
569 stObj
->pt
= st_texture_create(st
,
570 gl_target_to_pipe(stObj
->base
.Target
),
579 stObj
->lastLevel
= lastLevel
;
581 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
583 return stObj
->pt
!= NULL
;
588 * Called via ctx->Driver.AllocTextureImageBuffer().
589 * If the texture object/buffer already has space for the indicated image,
590 * we're done. Otherwise, allocate memory for the new texture image.
593 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
594 struct gl_texture_image
*texImage
)
596 struct st_context
*st
= st_context(ctx
);
597 struct st_texture_image
*stImage
= st_texture_image(texImage
);
598 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
599 const GLuint level
= texImage
->Level
;
600 GLuint width
= texImage
->Width
;
601 GLuint height
= texImage
->Height
;
602 GLuint depth
= texImage
->Depth
;
604 DBG("%s\n", __func__
);
606 assert(!stImage
->pt
); /* xxx this might be wrong */
608 etc_fallback_allocate(st
, stImage
);
610 /* Look if the parent texture object has space for this image */
612 level
<= stObj
->pt
->last_level
&&
613 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
614 /* this image will fit in the existing texture object's memory */
615 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
619 /* The parent texture object does not have space for this image */
621 pipe_resource_reference(&stObj
->pt
, NULL
);
622 st_texture_release_all_sampler_views(st
, stObj
);
624 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
625 /* Probably out of memory.
626 * Try flushing any pending rendering, then retry.
629 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
630 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
636 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
637 /* The image will live in the object's mipmap memory */
638 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
643 /* Create a new, temporary texture/resource/buffer to hold this
644 * one texture image. Note that when we later access this image
645 * (either for mapping or copying) we'll want to always specify
646 * mipmap level=0, even if the image represents some other mipmap
649 enum pipe_format format
=
650 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
651 GLuint bindings
= default_bindings(st
, format
);
652 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
654 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
655 width
, height
, depth
,
656 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
658 stImage
->pt
= st_texture_create(st
,
659 gl_target_to_pipe(stObj
->base
.Target
),
667 return stImage
->pt
!= NULL
;
673 * Preparation prior to glTexImage. Basically check the 'surface_based'
674 * field and switch to a "normal" tex image if necessary.
677 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
678 GLenum format
, GLenum type
)
680 struct gl_texture_object
*texObj
= texImage
->TexObject
;
681 struct st_texture_object
*stObj
= st_texture_object(texObj
);
683 /* switch to "normal" */
684 if (stObj
->surface_based
) {
685 const GLenum target
= texObj
->Target
;
686 const GLuint level
= texImage
->Level
;
687 mesa_format texFormat
;
689 _mesa_clear_texture_object(ctx
, texObj
);
690 pipe_resource_reference(&stObj
->pt
, NULL
);
692 /* oops, need to init this image again */
693 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
694 texImage
->InternalFormat
, format
,
697 _mesa_init_teximage_fields(ctx
, texImage
,
698 texImage
->Width
, texImage
->Height
,
699 texImage
->Depth
, texImage
->Border
,
700 texImage
->InternalFormat
, texFormat
);
702 stObj
->surface_based
= GL_FALSE
;
708 * Return a writemask for the gallium blit. The parameters can be base
709 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
712 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
715 case GL_DEPTH_STENCIL
:
717 case GL_DEPTH_STENCIL
:
719 case GL_DEPTH_COMPONENT
:
721 case GL_STENCIL_INDEX
:
728 case GL_DEPTH_COMPONENT
:
730 case GL_DEPTH_STENCIL
:
731 case GL_DEPTH_COMPONENT
:
738 case GL_STENCIL_INDEX
:
740 case GL_STENCIL_INDEX
:
748 return PIPE_MASK_RGBA
;
753 * Converts format to a format with the same components, types
754 * and sizes, but with the components in RGBA order.
756 static enum pipe_format
757 unswizzle_format(enum pipe_format format
)
761 case PIPE_FORMAT_B8G8R8A8_UNORM
:
762 case PIPE_FORMAT_A8R8G8B8_UNORM
:
763 case PIPE_FORMAT_A8B8G8R8_UNORM
:
764 return PIPE_FORMAT_R8G8B8A8_UNORM
;
766 case PIPE_FORMAT_B10G10R10A2_UNORM
:
767 return PIPE_FORMAT_R10G10B10A2_UNORM
;
769 case PIPE_FORMAT_B10G10R10A2_SNORM
:
770 return PIPE_FORMAT_R10G10B10A2_SNORM
;
772 case PIPE_FORMAT_B10G10R10A2_UINT
:
773 return PIPE_FORMAT_R10G10B10A2_UINT
;
781 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
783 static enum pipe_format
784 alpha_to_red(enum pipe_format format
)
788 case PIPE_FORMAT_A8_UNORM
:
789 return PIPE_FORMAT_R8_UNORM
;
790 case PIPE_FORMAT_A8_SNORM
:
791 return PIPE_FORMAT_R8_SNORM
;
792 case PIPE_FORMAT_A8_UINT
:
793 return PIPE_FORMAT_R8_UINT
;
794 case PIPE_FORMAT_A8_SINT
:
795 return PIPE_FORMAT_R8_SINT
;
797 case PIPE_FORMAT_A16_UNORM
:
798 return PIPE_FORMAT_R16_UNORM
;
799 case PIPE_FORMAT_A16_SNORM
:
800 return PIPE_FORMAT_R16_SNORM
;
801 case PIPE_FORMAT_A16_UINT
:
802 return PIPE_FORMAT_R16_UINT
;
803 case PIPE_FORMAT_A16_SINT
:
804 return PIPE_FORMAT_R16_SINT
;
805 case PIPE_FORMAT_A16_FLOAT
:
806 return PIPE_FORMAT_R16_FLOAT
;
808 case PIPE_FORMAT_A32_UINT
:
809 return PIPE_FORMAT_R32_UINT
;
810 case PIPE_FORMAT_A32_SINT
:
811 return PIPE_FORMAT_R32_SINT
;
812 case PIPE_FORMAT_A32_FLOAT
:
813 return PIPE_FORMAT_R32_FLOAT
;
821 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
823 static enum pipe_format
824 red_alpha_to_red_green(enum pipe_format format
)
828 case PIPE_FORMAT_R8A8_UNORM
:
829 return PIPE_FORMAT_R8G8_UNORM
;
830 case PIPE_FORMAT_R8A8_SNORM
:
831 return PIPE_FORMAT_R8G8_SNORM
;
832 case PIPE_FORMAT_R8A8_UINT
:
833 return PIPE_FORMAT_R8G8_UINT
;
834 case PIPE_FORMAT_R8A8_SINT
:
835 return PIPE_FORMAT_R8G8_SINT
;
837 case PIPE_FORMAT_R16A16_UNORM
:
838 return PIPE_FORMAT_R16G16_UNORM
;
839 case PIPE_FORMAT_R16A16_SNORM
:
840 return PIPE_FORMAT_R16G16_SNORM
;
841 case PIPE_FORMAT_R16A16_UINT
:
842 return PIPE_FORMAT_R16G16_UINT
;
843 case PIPE_FORMAT_R16A16_SINT
:
844 return PIPE_FORMAT_R16G16_SINT
;
845 case PIPE_FORMAT_R16A16_FLOAT
:
846 return PIPE_FORMAT_R16G16_FLOAT
;
848 case PIPE_FORMAT_R32A32_UINT
:
849 return PIPE_FORMAT_R32G32_UINT
;
850 case PIPE_FORMAT_R32A32_SINT
:
851 return PIPE_FORMAT_R32G32_SINT
;
852 case PIPE_FORMAT_R32A32_FLOAT
:
853 return PIPE_FORMAT_R32G32_FLOAT
;
861 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
863 static enum pipe_format
864 luminance_alpha_to_red_green(enum pipe_format format
)
868 case PIPE_FORMAT_L8A8_UNORM
:
869 return PIPE_FORMAT_R8G8_UNORM
;
870 case PIPE_FORMAT_L8A8_SNORM
:
871 return PIPE_FORMAT_R8G8_SNORM
;
872 case PIPE_FORMAT_L8A8_UINT
:
873 return PIPE_FORMAT_R8G8_UINT
;
874 case PIPE_FORMAT_L8A8_SINT
:
875 return PIPE_FORMAT_R8G8_SINT
;
877 case PIPE_FORMAT_L16A16_UNORM
:
878 return PIPE_FORMAT_R16G16_UNORM
;
879 case PIPE_FORMAT_L16A16_SNORM
:
880 return PIPE_FORMAT_R16G16_SNORM
;
881 case PIPE_FORMAT_L16A16_UINT
:
882 return PIPE_FORMAT_R16G16_UINT
;
883 case PIPE_FORMAT_L16A16_SINT
:
884 return PIPE_FORMAT_R16G16_SINT
;
885 case PIPE_FORMAT_L16A16_FLOAT
:
886 return PIPE_FORMAT_R16G16_FLOAT
;
888 case PIPE_FORMAT_L32A32_UINT
:
889 return PIPE_FORMAT_R32G32_UINT
;
890 case PIPE_FORMAT_L32A32_SINT
:
891 return PIPE_FORMAT_R32G32_SINT
;
892 case PIPE_FORMAT_L32A32_FLOAT
:
893 return PIPE_FORMAT_R32G32_FLOAT
;
901 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
904 format_is_alpha(enum pipe_format format
)
906 const struct util_format_description
*desc
= util_format_description(format
);
908 if (desc
->nr_channels
== 1 &&
909 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
910 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
911 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
912 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
919 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
922 format_is_red(enum pipe_format format
)
924 const struct util_format_description
*desc
= util_format_description(format
);
926 if (desc
->nr_channels
== 1 &&
927 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
928 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
929 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
930 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
938 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
941 format_is_luminance(enum pipe_format format
)
943 const struct util_format_description
*desc
= util_format_description(format
);
945 if (desc
->nr_channels
== 1 &&
946 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
947 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
948 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
949 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
956 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
959 format_is_red_alpha(enum pipe_format format
)
961 const struct util_format_description
*desc
= util_format_description(format
);
963 if (desc
->nr_channels
== 2 &&
964 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
965 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
966 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
967 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
974 format_is_swizzled_rgba(enum pipe_format format
)
976 const struct util_format_description
*desc
= util_format_description(format
);
978 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
979 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
980 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
981 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
989 unsigned char swizzle
[4];
990 enum pipe_format format
;
993 static const struct format_table table_8888_unorm
[] = {
994 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
995 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
996 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
997 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
1000 static const struct format_table table_1010102_unorm
[] = {
1001 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
1002 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
1005 static const struct format_table table_1010102_snorm
[] = {
1006 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
1007 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
1010 static const struct format_table table_1010102_uint
[] = {
1011 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
1012 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
1015 static enum pipe_format
1016 swizzle_format(enum pipe_format format
, const int * const swizzle
)
1021 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1022 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1023 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1024 case PIPE_FORMAT_A8B8G8R8_UNORM
:
1025 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
1026 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
1027 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
1028 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
1029 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
1030 return table_8888_unorm
[i
].format
;
1034 case PIPE_FORMAT_R10G10B10A2_UNORM
:
1035 case PIPE_FORMAT_B10G10R10A2_UNORM
:
1036 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
1037 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
1038 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
1039 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
1040 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
1041 return table_1010102_unorm
[i
].format
;
1045 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1046 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1047 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1048 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1049 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1050 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1051 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1052 return table_1010102_snorm
[i
].format
;
1056 case PIPE_FORMAT_R10G10B10A2_UINT
:
1057 case PIPE_FORMAT_B10G10R10A2_UINT
:
1058 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1059 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1060 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1061 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1062 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1063 return table_1010102_uint
[i
].format
;
1071 return PIPE_FORMAT_NONE
;
1075 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1077 enum pipe_format src
= *src_format
;
1078 enum pipe_format dst
= *dst_format
;
1080 /* Note: dst_format has already been transformed from luminance/intensity
1081 * to red when this function is called. The source format will never
1082 * be an intensity format, because GL_INTENSITY is not a legal value
1083 * for the format parameter in glTex(Sub)Image(). */
1085 if (format_is_alpha(src
)) {
1086 if (!format_is_alpha(dst
))
1089 src
= alpha_to_red(src
);
1090 dst
= alpha_to_red(dst
);
1091 } else if (format_is_luminance(src
)) {
1092 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1095 src
= util_format_luminance_to_red(src
);
1096 } else if (util_format_is_luminance_alpha(src
)) {
1097 src
= luminance_alpha_to_red_green(src
);
1099 if (format_is_red_alpha(dst
)) {
1100 dst
= red_alpha_to_red_green(dst
);
1101 } else if (!format_is_red(dst
))
1103 } else if (format_is_swizzled_rgba(src
)) {
1104 const struct util_format_description
*src_desc
= util_format_description(src
);
1105 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1109 /* Make sure the format is an RGBA and not an RGBX format */
1110 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1113 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1116 for (i
= 0; i
< 4; i
++)
1117 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1119 dst
= swizzle_format(dst
, swizzle
);
1120 if (dst
== PIPE_FORMAT_NONE
)
1123 src
= unswizzle_format(src
);
1132 try_pbo_upload_common(struct gl_context
*ctx
,
1133 struct pipe_surface
*surface
,
1134 const struct st_pbo_addresses
*addr
,
1135 enum pipe_format src_format
)
1137 struct st_context
*st
= st_context(ctx
);
1138 struct cso_context
*cso
= st
->cso_context
;
1139 struct pipe_context
*pipe
= st
->pipe
;
1140 bool success
= false;
1143 fs
= st_pbo_get_upload_fs(st
, src_format
, surface
->format
);
1147 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1148 CSO_BIT_FRAGMENT_SAMPLERS
|
1149 CSO_BIT_VERTEX_ELEMENTS
|
1150 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1151 CSO_BIT_FRAMEBUFFER
|
1154 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1155 CSO_BIT_RASTERIZER
|
1156 CSO_BIT_STREAM_OUTPUTS
|
1157 CSO_BIT_PAUSE_QUERIES
|
1158 CSO_BIT_SAMPLE_MASK
|
1159 CSO_BIT_MIN_SAMPLES
|
1160 CSO_BIT_RENDER_CONDITION
|
1161 CSO_BITS_ALL_SHADERS
));
1162 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1164 cso_set_sample_mask(cso
, ~0);
1165 cso_set_min_samples(cso
, 1);
1166 cso_set_render_condition(cso
, NULL
, FALSE
, 0);
1168 /* Set up the sampler_view */
1170 struct pipe_sampler_view templ
;
1171 struct pipe_sampler_view
*sampler_view
;
1172 struct pipe_sampler_state sampler
= {0};
1173 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
1175 memset(&templ
, 0, sizeof(templ
));
1176 templ
.target
= PIPE_BUFFER
;
1177 templ
.format
= src_format
;
1178 templ
.u
.buf
.offset
= addr
->first_element
* addr
->bytes_per_pixel
;
1179 templ
.u
.buf
.size
= (addr
->last_element
- addr
->first_element
+ 1) *
1180 addr
->bytes_per_pixel
;
1181 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1182 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1183 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1184 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1186 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1187 if (sampler_view
== NULL
)
1190 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1192 pipe_sampler_view_reference(&sampler_view
, NULL
);
1194 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
1197 /* Framebuffer_state */
1199 struct pipe_framebuffer_state fb
;
1200 memset(&fb
, 0, sizeof(fb
));
1201 fb
.width
= surface
->width
;
1202 fb
.height
= surface
->height
;
1204 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1206 cso_set_framebuffer(cso
, &fb
);
1208 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1211 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1214 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1216 /* Depth/stencil/alpha state */
1218 struct pipe_depth_stencil_alpha_state dsa
;
1219 memset(&dsa
, 0, sizeof(dsa
));
1220 cso_set_depth_stencil_alpha(cso
, &dsa
);
1223 /* Set up the fragment shader */
1224 cso_set_fragment_shader_handle(cso
, fs
);
1226 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1229 cso_restore_state(cso
);
1230 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1236 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1237 struct gl_texture_image
*texImage
,
1238 GLenum format
, GLenum type
,
1239 enum pipe_format dst_format
,
1240 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1241 GLint width
, GLint height
, GLint depth
,
1243 const struct gl_pixelstore_attrib
*unpack
)
1245 struct st_context
*st
= st_context(ctx
);
1246 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1247 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1248 struct pipe_resource
*texture
= stImage
->pt
;
1249 struct pipe_context
*pipe
= st
->pipe
;
1250 struct pipe_screen
*screen
= pipe
->screen
;
1251 struct pipe_surface
*surface
= NULL
;
1252 struct st_pbo_addresses addr
;
1253 enum pipe_format src_format
;
1254 const struct util_format_description
*desc
;
1255 GLenum gl_target
= texImage
->TexObject
->Target
;
1258 if (!st
->pbo
.upload_enabled
)
1261 /* From now on, we need the gallium representation of dimensions. */
1262 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1269 if (depth
!= 1 && !st
->pbo
.layers
)
1272 /* Choose the source format. Initially, we do so without checking driver
1273 * support at all because of the remapping we later perform and because
1274 * at least the Radeon driver actually supports some formats for texture
1275 * buffers which it doesn't support for regular textures. */
1276 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1281 src_format
= util_format_linear(src_format
);
1282 desc
= util_format_description(src_format
);
1284 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1287 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1290 if (st
->pbo
.rgba_only
) {
1291 enum pipe_format orig_dst_format
= dst_format
;
1293 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1297 if (dst_format
!= orig_dst_format
&&
1298 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1299 PIPE_BIND_RENDER_TARGET
)) {
1305 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0,
1306 PIPE_BIND_SAMPLER_VIEW
)) {
1310 /* Compute buffer addresses */
1311 addr
.xoffset
= xoffset
;
1312 addr
.yoffset
= yoffset
;
1314 addr
.height
= height
;
1316 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1318 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1322 /* Set up the surface */
1324 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1325 unsigned max_layer
= util_max_layer(texture
, level
);
1327 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1329 struct pipe_surface templ
;
1330 memset(&templ
, 0, sizeof(templ
));
1331 templ
.format
= dst_format
;
1332 templ
.u
.tex
.level
= level
;
1333 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1334 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1336 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1341 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1343 pipe_surface_reference(&surface
, NULL
);
1349 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1350 struct gl_texture_image
*texImage
,
1351 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1352 GLint width
, GLint height
, GLint depth
,
1353 GLenum format
, GLenum type
, const void *pixels
,
1354 const struct gl_pixelstore_attrib
*unpack
)
1356 struct st_context
*st
= st_context(ctx
);
1357 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1358 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1359 struct pipe_context
*pipe
= st
->pipe
;
1360 struct pipe_screen
*screen
= pipe
->screen
;
1361 struct pipe_resource
*dst
= stImage
->pt
;
1362 struct pipe_resource
*src
= NULL
;
1363 struct pipe_resource src_templ
;
1364 struct pipe_transfer
*transfer
;
1365 struct pipe_blit_info blit
;
1366 enum pipe_format src_format
, dst_format
;
1367 mesa_format mesa_src_format
;
1368 GLenum gl_target
= texImage
->TexObject
->Target
;
1371 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1372 unsigned dst_level
= 0;
1374 st_flush_bitmap_cache(st
);
1375 st_invalidate_readpix_cache(st
);
1377 if (stObj
->pt
== stImage
->pt
)
1378 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1380 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1381 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1386 /* Try texture_subdata, which should be the fastest memcpy path. */
1388 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1389 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1390 texImage
->TexFormat
, format
, type
,
1392 struct pipe_box box
;
1393 unsigned stride
, layer_stride
;
1396 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1397 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1399 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1402 /* Convert to Gallium coordinates. */
1403 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1408 layer_stride
= stride
;
1411 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1412 pipe
->texture_subdata(pipe
, dst
, dst_level
, 0,
1413 &box
, data
, stride
, layer_stride
);
1417 if (!st
->prefer_blit_based_texture_transfer
) {
1421 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1422 * blit implementation in some drivers. */
1423 if (format
== GL_DEPTH_STENCIL
) {
1427 /* If the base internal format and the texture format don't match,
1428 * we can't use blit-based TexSubImage. */
1429 if (texImage
->_BaseFormat
!=
1430 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1435 /* See if the destination format is supported. */
1436 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1437 bind
= PIPE_BIND_DEPTH_STENCIL
;
1439 bind
= PIPE_BIND_RENDER_TARGET
;
1441 /* For luminance and intensity, only the red channel is stored
1442 * in the destination. */
1443 dst_format
= util_format_linear(dst
->format
);
1444 dst_format
= util_format_luminance_to_red(dst_format
);
1445 dst_format
= util_format_intensity_to_red(dst_format
);
1448 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1449 dst
->nr_samples
, bind
)) {
1453 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1454 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1455 xoffset
, yoffset
, zoffset
,
1456 width
, height
, depth
, pixels
, unpack
))
1460 /* See if the texture format already matches the format and type,
1461 * in which case the memcpy-based fast path will likely be used and
1462 * we don't have to blit. */
1463 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1464 type
, unpack
->SwapBytes
, NULL
)) {
1468 /* Choose the source format. */
1469 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1470 format
, type
, unpack
->SwapBytes
);
1475 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1477 /* There is no reason to do this if we cannot use memcpy for the temporary
1478 * source texture at least. This also takes transfer ops into account,
1480 if (!_mesa_texstore_can_use_memcpy(ctx
,
1481 _mesa_get_format_base_format(mesa_src_format
),
1482 mesa_src_format
, format
, type
, unpack
)) {
1486 /* TexSubImage only sets a single cubemap face. */
1487 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1488 gl_target
= GL_TEXTURE_2D
;
1490 /* TexSubImage can specify subsets of cube map array faces
1491 * so we need to upload via 2D array instead */
1492 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1493 gl_target
= GL_TEXTURE_2D_ARRAY
;
1496 /* Initialize the source texture description. */
1497 memset(&src_templ
, 0, sizeof(src_templ
));
1498 src_templ
.target
= gl_target_to_pipe(gl_target
);
1499 src_templ
.format
= src_format
;
1500 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1501 src_templ
.usage
= PIPE_USAGE_STAGING
;
1503 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1504 &src_templ
.width0
, &src_templ
.height0
,
1505 &src_templ
.depth0
, &src_templ
.array_size
);
1507 /* Check for NPOT texture support. */
1508 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1509 (!util_is_power_of_two(src_templ
.width0
) ||
1510 !util_is_power_of_two(src_templ
.height0
) ||
1511 !util_is_power_of_two(src_templ
.depth0
))) {
1515 /* Create the source texture. */
1516 src
= screen
->resource_create(screen
, &src_templ
);
1521 /* Map source pixels. */
1522 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1523 format
, type
, pixels
, unpack
,
1526 /* This is a GL error. */
1527 pipe_resource_reference(&src
, NULL
);
1531 /* From now on, we need the gallium representation of dimensions. */
1532 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1539 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1540 width
, height
, depth
, &transfer
);
1542 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1543 pipe_resource_reference(&src
, NULL
);
1547 /* Upload pixels (just memcpy). */
1549 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1552 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1553 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1554 /* 1D array textures.
1555 * We need to convert gallium coords to GL coords.
1557 void *src
= _mesa_image_address2d(unpack
, pixels
,
1558 width
, depth
, format
,
1560 memcpy(map
, src
, bytesPerRow
);
1563 ubyte
*slice_map
= map
;
1565 for (row
= 0; row
< (unsigned) height
; row
++) {
1566 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1567 width
, height
, format
,
1568 type
, slice
, row
, 0);
1569 memcpy(slice_map
, src
, bytesPerRow
);
1570 slice_map
+= transfer
->stride
;
1573 map
+= transfer
->layer_stride
;
1577 pipe_transfer_unmap(pipe
, transfer
);
1578 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1581 memset(&blit
, 0, sizeof(blit
));
1582 blit
.src
.resource
= src
;
1584 blit
.src
.format
= src_format
;
1585 blit
.dst
.resource
= dst
;
1586 blit
.dst
.level
= dst_level
;
1587 blit
.dst
.format
= dst_format
;
1588 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1589 blit
.dst
.box
.x
= xoffset
;
1590 blit
.dst
.box
.y
= yoffset
;
1591 blit
.dst
.box
.z
= zoffset
+ dstz
;
1592 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1593 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1594 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1595 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1596 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1597 blit
.scissor_enable
= FALSE
;
1599 st
->pipe
->blit(st
->pipe
, &blit
);
1601 pipe_resource_reference(&src
, NULL
);
1605 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1606 width
, height
, depth
, format
, type
, pixels
,
1611 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1612 struct gl_texture_image
*texImage
,
1613 GLenum format
, GLenum type
, const void *pixels
,
1614 const struct gl_pixelstore_attrib
*unpack
)
1616 assert(dims
== 1 || dims
== 2 || dims
== 3);
1618 prep_teximage(ctx
, texImage
, format
, type
);
1620 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1623 /* allocate storage for texture data */
1624 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1625 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1629 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1630 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1631 format
, type
, pixels
, unpack
);
1636 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1637 struct gl_texture_image
*texImage
,
1638 GLint x
, GLint y
, GLint z
,
1639 GLsizei w
, GLsizei h
, GLsizei d
,
1640 GLenum format
, GLsizei imageSize
, const void *data
)
1642 struct st_context
*st
= st_context(ctx
);
1643 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1644 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1645 struct pipe_resource
*texture
= stImage
->pt
;
1646 struct pipe_context
*pipe
= st
->pipe
;
1647 struct pipe_screen
*screen
= pipe
->screen
;
1648 struct pipe_resource
*dst
= stImage
->pt
;
1649 struct pipe_surface
*surface
= NULL
;
1650 struct compressed_pixelstore store
;
1651 struct st_pbo_addresses addr
;
1652 enum pipe_format copy_format
;
1654 intptr_t buf_offset
;
1655 bool success
= false;
1657 /* Check basic pre-conditions for PBO upload */
1658 if (!st
->prefer_blit_based_texture_transfer
) {
1662 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1665 if (st_etc_fallback(st
, texImage
)) {
1666 /* ETC isn't supported and is represented by uncompressed formats. */
1674 if (!st
->pbo
.upload_enabled
||
1675 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1679 /* Choose the pipe format for the upload. */
1680 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1681 bw
= util_format_get_blockwidth(dst
->format
);
1682 bh
= util_format_get_blockheight(dst
->format
);
1684 switch (addr
.bytes_per_pixel
) {
1686 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1689 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1695 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0,
1696 PIPE_BIND_SAMPLER_VIEW
)) {
1700 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1701 dst
->nr_samples
, PIPE_BIND_RENDER_TARGET
)) {
1705 /* Interpret the pixelstore settings. */
1706 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1707 &ctx
->Unpack
, &store
);
1708 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1709 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1711 /* Compute the offset into the buffer */
1712 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1714 if (buf_offset
% addr
.bytes_per_pixel
) {
1718 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1720 addr
.xoffset
= x
/ bw
;
1721 addr
.yoffset
= y
/ bh
;
1722 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1723 addr
.height
= store
.CopyRowsPerSlice
;
1725 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1726 addr
.image_height
= store
.TotalRowsPerSlice
;
1728 if (!st_pbo_addresses_setup(st
, st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1732 /* Set up the surface. */
1734 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1735 unsigned max_layer
= util_max_layer(texture
, level
);
1737 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1739 struct pipe_surface templ
;
1740 memset(&templ
, 0, sizeof(templ
));
1741 templ
.format
= copy_format
;
1742 templ
.u
.tex
.level
= level
;
1743 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1744 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1746 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1751 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1753 pipe_surface_reference(&surface
, NULL
);
1759 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1761 format
, imageSize
, data
);
1765 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1766 struct gl_texture_image
*texImage
,
1767 GLsizei imageSize
, const void *data
)
1769 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1771 /* only 2D and 3D compressed images are supported at this time */
1773 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1777 /* This is pretty simple, because unlike the general texstore path we don't
1778 * have to worry about the usual image unpacking or image transfer
1782 assert(texImage
->Width
> 0);
1783 assert(texImage
->Height
> 0);
1784 assert(texImage
->Depth
> 0);
1786 /* allocate storage for texture data */
1787 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1788 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1792 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1794 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1795 texImage
->TexFormat
,
1803 * Called via ctx->Driver.GetTexSubImage()
1805 * This uses a blit to copy the texture to a texture format which matches
1806 * the format and type combo and then a fast read-back is done using memcpy.
1807 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1808 * a format which matches the swizzling.
1810 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1812 * NOTE: Drivers usually do a blit to convert between tiled and linear
1813 * texture layouts during texture uploads/downloads, so the blit
1814 * we do here should be free in such cases.
1817 st_GetTexSubImage(struct gl_context
* ctx
,
1818 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1819 GLsizei width
, GLsizei height
, GLint depth
,
1820 GLenum format
, GLenum type
, void * pixels
,
1821 struct gl_texture_image
*texImage
)
1823 struct st_context
*st
= st_context(ctx
);
1824 struct pipe_context
*pipe
= st
->pipe
;
1825 struct pipe_screen
*screen
= pipe
->screen
;
1826 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1827 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1828 struct pipe_resource
*src
= stObj
->pt
;
1829 struct pipe_resource
*dst
= NULL
;
1830 struct pipe_resource dst_templ
;
1831 enum pipe_format dst_format
, src_format
;
1832 mesa_format mesa_format
;
1833 GLenum gl_target
= texImage
->TexObject
->Target
;
1834 enum pipe_texture_target pipe_target
;
1836 struct pipe_blit_info blit
;
1838 struct pipe_transfer
*tex_xfer
;
1840 boolean done
= FALSE
;
1842 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1843 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1845 st_flush_bitmap_cache(st
);
1847 if (!st
->prefer_blit_based_texture_transfer
&&
1848 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1849 /* Try to avoid the fallback if we're doing texture decompression here */
1853 /* Handle non-finalized textures. */
1854 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1858 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1859 * due to an incomplete stencil blit implementation in some drivers. */
1860 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1864 /* If the base internal format and the texture format don't match, we have
1865 * to fall back to _mesa_GetTexImage_sw. */
1866 if (texImage
->_BaseFormat
!=
1867 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1871 /* See if the texture format already matches the format and type,
1872 * in which case the memcpy-based fast path will be used. */
1873 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1874 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1878 /* Convert the source format to what is expected by GetTexImage
1879 * and see if it's supported.
1881 * This only applies to glGetTexImage:
1882 * - Luminance must be returned as (L,0,0,1).
1883 * - Luminance alpha must be returned as (L,0,0,A).
1884 * - Intensity must be returned as (I,0,0,1)
1886 if (stObj
->surface_based
)
1887 src_format
= util_format_linear(stObj
->surface_format
);
1889 src_format
= util_format_linear(src
->format
);
1890 src_format
= util_format_luminance_to_red(src_format
);
1891 src_format
= util_format_intensity_to_red(src_format
);
1894 !screen
->is_format_supported(screen
, src_format
, src
->target
,
1896 PIPE_BIND_SAMPLER_VIEW
)) {
1900 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1901 bind
= PIPE_BIND_DEPTH_STENCIL
;
1903 bind
= PIPE_BIND_RENDER_TARGET
;
1905 /* GetTexImage only returns a single face for cubemaps. */
1906 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1907 gl_target
= GL_TEXTURE_2D
;
1909 pipe_target
= gl_target_to_pipe(gl_target
);
1911 /* Choose the destination format by finding the best match
1912 * for the format+type combo. */
1913 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1914 ctx
->Pack
.SwapBytes
);
1916 if (dst_format
== PIPE_FORMAT_NONE
) {
1917 GLenum dst_glformat
;
1919 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1920 * where decompression with a blit is always preferred. */
1921 if (!util_format_is_compressed(src
->format
)) {
1925 /* Set the appropriate format for the decompressed texture.
1926 * Luminance and sRGB formats shouldn't appear here.*/
1927 switch (src_format
) {
1928 case PIPE_FORMAT_DXT1_RGB
:
1929 case PIPE_FORMAT_DXT1_RGBA
:
1930 case PIPE_FORMAT_DXT3_RGBA
:
1931 case PIPE_FORMAT_DXT5_RGBA
:
1932 case PIPE_FORMAT_RGTC1_UNORM
:
1933 case PIPE_FORMAT_RGTC2_UNORM
:
1934 case PIPE_FORMAT_ETC1_RGB8
:
1935 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1936 dst_glformat
= GL_RGBA8
;
1938 case PIPE_FORMAT_RGTC1_SNORM
:
1939 case PIPE_FORMAT_RGTC2_SNORM
:
1940 if (!ctx
->Extensions
.EXT_texture_snorm
)
1942 dst_glformat
= GL_RGBA8_SNORM
;
1944 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1945 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1946 if (!ctx
->Extensions
.ARB_texture_float
)
1948 dst_glformat
= GL_RGBA32F
;
1955 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
1956 pipe_target
, 0, bind
, FALSE
);
1958 if (dst_format
== PIPE_FORMAT_NONE
) {
1959 /* unable to get an rgba format!?! */
1964 /* create the destination texture of size (width X height X depth) */
1965 memset(&dst_templ
, 0, sizeof(dst_templ
));
1966 dst_templ
.target
= pipe_target
;
1967 dst_templ
.format
= dst_format
;
1968 dst_templ
.bind
= bind
;
1969 dst_templ
.usage
= PIPE_USAGE_STAGING
;
1971 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1972 &dst_templ
.width0
, &dst_templ
.height0
,
1973 &dst_templ
.depth0
, &dst_templ
.array_size
);
1975 dst
= screen
->resource_create(screen
, &dst_templ
);
1980 /* From now on, we need the gallium representation of dimensions. */
1981 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1988 assert(texImage
->Face
== 0 ||
1989 texImage
->TexObject
->MinLayer
== 0 ||
1992 memset(&blit
, 0, sizeof(blit
));
1993 blit
.src
.resource
= src
;
1994 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
1995 blit
.src
.format
= src_format
;
1996 blit
.dst
.resource
= dst
;
1998 blit
.dst
.format
= dst
->format
;
1999 blit
.src
.box
.x
= xoffset
;
2001 blit
.src
.box
.y
= yoffset
;
2003 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2005 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2006 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2007 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2008 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2009 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2010 blit
.scissor_enable
= FALSE
;
2012 /* blit/render/decompress */
2013 st
->pipe
->blit(st
->pipe
, &blit
);
2015 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2017 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2018 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2023 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2024 dims
= _mesa_get_texture_dimensions(gl_target
);
2026 /* copy/pack data into user buffer */
2027 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2028 ctx
->Pack
.SwapBytes
, NULL
)) {
2030 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2033 for (slice
= 0; slice
< depth
; slice
++) {
2034 ubyte
*slice_map
= map
;
2036 for (row
= 0; row
< height
; row
++) {
2037 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2038 width
, height
, format
, type
,
2041 memcpy(dest
, slice_map
, bytesPerRow
);
2043 slice_map
+= tex_xfer
->stride
;
2046 map
+= tex_xfer
->layer_stride
;
2050 /* format translation via floats */
2053 uint32_t dstMesaFormat
;
2054 int dstStride
, srcStride
;
2056 assert(util_format_is_compressed(src
->format
));
2058 rgba
= malloc(width
* height
* 4 * sizeof(GLfloat
));
2063 if (ST_DEBUG
& DEBUG_FALLBACK
)
2064 debug_printf("%s: fallback format translation\n", __func__
);
2066 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2067 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2068 srcStride
= 4 * width
* sizeof(GLfloat
);
2069 for (slice
= 0; slice
< depth
; slice
++) {
2070 void *dest
= _mesa_image_address(dims
, &ctx
->Pack
, pixels
,
2071 width
, height
, format
, type
,
2074 /* get float[4] rgba row from surface */
2075 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, height
,
2078 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2079 rgba
, RGBA32_FLOAT
, srcStride
,
2080 width
, height
, NULL
);
2082 /* Handle byte swapping if required */
2083 if (ctx
->Pack
.SwapBytes
) {
2084 _mesa_swap_bytes_2d_image(format
, type
, &ctx
->Pack
,
2085 width
, height
, dest
, dest
);
2088 map
+= tex_xfer
->layer_stride
;
2097 pipe_transfer_unmap(pipe
, tex_xfer
);
2099 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2100 pipe_resource_reference(&dst
, NULL
);
2104 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2105 width
, height
, depth
,
2106 format
, type
, pixels
, texImage
);
2112 * Do a CopyTexSubImage operation using a read transfer from the source,
2113 * a write transfer to the destination and get_tile()/put_tile() to access
2114 * the pixels/texels.
2116 * Note: srcY=0=TOP of renderbuffer
2119 fallback_copy_texsubimage(struct gl_context
*ctx
,
2120 struct st_renderbuffer
*strb
,
2121 struct st_texture_image
*stImage
,
2123 GLint destX
, GLint destY
, GLint slice
,
2124 GLint srcX
, GLint srcY
,
2125 GLsizei width
, GLsizei height
)
2127 struct st_context
*st
= st_context(ctx
);
2128 struct pipe_context
*pipe
= st
->pipe
;
2129 struct pipe_transfer
*src_trans
;
2131 enum pipe_transfer_usage transfer_usage
;
2133 unsigned dst_width
= width
;
2134 unsigned dst_height
= height
;
2135 unsigned dst_depth
= 1;
2136 struct pipe_transfer
*transfer
;
2138 if (ST_DEBUG
& DEBUG_FALLBACK
)
2139 debug_printf("%s: fallback processing\n", __func__
);
2141 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2142 srcY
= strb
->Base
.Height
- srcY
- height
;
2145 map
= pipe_transfer_map(pipe
,
2147 strb
->surface
->u
.tex
.level
,
2148 strb
->surface
->u
.tex
.first_layer
,
2151 width
, height
, &src_trans
);
2153 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2154 baseFormat
== GL_DEPTH_STENCIL
) &&
2155 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2156 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2158 transfer_usage
= PIPE_TRANSFER_WRITE
;
2160 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2161 destX
, destY
, slice
,
2162 dst_width
, dst_height
, dst_depth
,
2165 if (baseFormat
== GL_DEPTH_COMPONENT
||
2166 baseFormat
== GL_DEPTH_STENCIL
) {
2167 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2168 ctx
->Pixel
.DepthBias
!= 0.0F
);
2172 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2173 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2182 data
= malloc(width
* sizeof(uint
));
2185 /* To avoid a large temp memory allocation, do copy row by row */
2186 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2187 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2189 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2192 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2193 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2194 0, 0, width
, 1, data
);
2197 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2202 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2210 malloc(width
* height
* 4 * sizeof(GLfloat
));
2212 if (tempSrc
&& texDest
) {
2213 const GLint dims
= 2;
2215 struct gl_texture_image
*texImage
= &stImage
->base
;
2216 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2218 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2219 unpack
.Invert
= GL_TRUE
;
2222 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2223 dstRowStride
= transfer
->layer_stride
;
2226 dstRowStride
= transfer
->stride
;
2229 /* get float/RGBA image from framebuffer */
2230 /* XXX this usually involves a lot of int/float conversion.
2231 * try to avoid that someday.
2233 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2234 util_format_linear(strb
->texture
->format
),
2237 /* Store into texture memory.
2238 * Note that this does some special things such as pixel transfer
2239 * ops and format conversion. In particular, if the dest tex format
2240 * is actually RGBA but the user created the texture as GL_RGB we
2241 * need to fill-in/override the alpha channel with 1.0.
2243 _mesa_texstore(ctx
, dims
,
2244 texImage
->_BaseFormat
,
2245 texImage
->TexFormat
,
2249 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2253 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2259 st_texture_image_unmap(st
, stImage
, slice
);
2260 pipe
->transfer_unmap(pipe
, src_trans
);
2265 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2266 * Note that the region to copy has already been clipped so we know we
2267 * won't read from outside the source renderbuffer's bounds.
2269 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2272 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2273 struct gl_texture_image
*texImage
,
2274 GLint destX
, GLint destY
, GLint slice
,
2275 struct gl_renderbuffer
*rb
,
2276 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2278 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2279 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2280 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2281 struct st_context
*st
= st_context(ctx
);
2282 struct pipe_context
*pipe
= st
->pipe
;
2283 struct pipe_screen
*screen
= pipe
->screen
;
2284 struct pipe_blit_info blit
;
2285 enum pipe_format dst_format
;
2286 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2290 st_flush_bitmap_cache(st
);
2291 st_invalidate_readpix_cache(st
);
2293 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2294 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2296 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2297 debug_printf("%s: null strb or stImage\n", __func__
);
2301 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2302 texImage
->TexFormat
)) {
2306 /* The base internal format must match the mesa format, so make sure
2307 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
2309 if (texImage
->_BaseFormat
!=
2310 _mesa_get_format_base_format(texImage
->TexFormat
) ||
2311 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
2315 /* Choose the destination format to match the TexImage behavior. */
2316 dst_format
= util_format_linear(stImage
->pt
->format
);
2317 dst_format
= util_format_luminance_to_red(dst_format
);
2318 dst_format
= util_format_intensity_to_red(dst_format
);
2320 /* See if the destination format is supported. */
2321 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2322 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2323 bind
= PIPE_BIND_DEPTH_STENCIL
;
2326 bind
= PIPE_BIND_RENDER_TARGET
;
2330 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2331 stImage
->pt
->nr_samples
, bind
)) {
2335 /* Y flipping for the main framebuffer. */
2337 srcY1
= strb
->Base
.Height
- srcY
- height
;
2338 srcY0
= srcY1
+ height
;
2342 srcY1
= srcY0
+ height
;
2345 /* Blit the texture.
2346 * This supports flipping, format conversions, and downsampling.
2348 memset(&blit
, 0, sizeof(blit
));
2349 blit
.src
.resource
= strb
->texture
;
2350 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2351 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2352 blit
.src
.box
.x
= srcX
;
2353 blit
.src
.box
.y
= srcY0
;
2354 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2355 blit
.src
.box
.width
= width
;
2356 blit
.src
.box
.height
= srcY1
- srcY0
;
2357 blit
.src
.box
.depth
= 1;
2358 blit
.dst
.resource
= stImage
->pt
;
2359 blit
.dst
.format
= dst_format
;
2360 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2361 blit
.dst
.box
.x
= destX
;
2362 blit
.dst
.box
.y
= destY
;
2363 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2364 blit
.dst
.box
.width
= width
;
2365 blit
.dst
.box
.height
= height
;
2366 blit
.dst
.box
.depth
= 1;
2367 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2368 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2369 pipe
->blit(pipe
, &blit
);
2373 /* software fallback */
2374 fallback_copy_texsubimage(ctx
,
2375 strb
, stImage
, texImage
->_BaseFormat
,
2376 destX
, destY
, slice
,
2377 srcX
, srcY
, width
, height
);
2382 * Copy image data from stImage into the texture object 'stObj' at level
2386 copy_image_data_to_texture(struct st_context
*st
,
2387 struct st_texture_object
*stObj
,
2389 struct st_texture_image
*stImage
)
2393 const struct gl_texture_image
*dstImage
=
2394 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2396 assert(dstImage
->Width
== stImage
->base
.Width
);
2397 assert(dstImage
->Height
== stImage
->base
.Height
);
2398 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2402 /* Copy potentially with the blitter:
2405 if (stImage
->pt
->last_level
== 0)
2408 src_level
= stImage
->base
.Level
;
2410 assert(src_level
<= stImage
->pt
->last_level
);
2411 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2412 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2413 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2414 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2415 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2416 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2418 st_texture_image_copy(st
->pipe
,
2419 stObj
->pt
, dstLevel
, /* dest texture, level */
2420 stImage
->pt
, src_level
, /* src texture, level */
2421 stImage
->base
.Face
);
2423 pipe_resource_reference(&stImage
->pt
, NULL
);
2425 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2430 * Called during state validation. When this function is finished,
2431 * the texture object should be ready for rendering.
2432 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2435 st_finalize_texture(struct gl_context
*ctx
,
2436 struct pipe_context
*pipe
,
2437 struct gl_texture_object
*tObj
,
2440 struct st_context
*st
= st_context(ctx
);
2441 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2442 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2444 const struct st_texture_image
*firstImage
;
2445 enum pipe_format firstImageFormat
;
2446 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2448 if (tObj
->Immutable
)
2451 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
2452 /* The texture is complete and we know exactly how many mipmap levels
2453 * are present/needed. This is conditional because we may be called
2454 * from the st_generate_mipmap() function when the texture object is
2455 * incomplete. In that case, we'll have set stObj->lastLevel before
2458 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
2459 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
2460 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2462 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2465 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
2466 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
2469 pipe_resource_reference(&stObj
->pt
, NULL
);
2470 st_texture_release_all_sampler_views(st
, stObj
);
2474 if (st_obj
->buffer
!= stObj
->pt
) {
2475 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
2476 st_texture_release_all_sampler_views(st
, stObj
);
2482 firstImage
= st_texture_image_const(stObj
->base
.Image
[cubeMapFace
][stObj
->base
.BaseLevel
]);
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 */
2508 GLuint width
, height
, depth
;
2510 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2511 firstImage
->base
.Width2
,
2512 firstImage
->base
.Height2
,
2513 firstImage
->base
.Depth2
,
2514 &width
, &height
, &depth
, &ptLayers
);
2516 /* If we previously allocated a pipe texture and its sizes are
2517 * compatible, use them.
2520 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2521 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2522 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2523 ptWidth
= stObj
->pt
->width0
;
2524 ptHeight
= stObj
->pt
->height0
;
2525 ptDepth
= stObj
->pt
->depth0
;
2527 /* Otherwise, compute a new level=0 size that is compatible with the
2530 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2531 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2532 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2534 /* If the base level image is 1x1x1, we still need to ensure that the
2535 * resulting pipe texture ends up with the required number of levels
2538 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2539 ptWidth
<<= firstImage
->base
.Level
;
2541 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2542 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2547 ptNumSamples
= firstImage
->base
.NumSamples
;
2550 /* If we already have a gallium texture, check that it matches the texture
2551 * object's format, target, size, num_levels, etc.
2554 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2555 stObj
->pt
->format
!= firstImageFormat
||
2556 stObj
->pt
->last_level
< stObj
->lastLevel
||
2557 stObj
->pt
->width0
!= ptWidth
||
2558 stObj
->pt
->height0
!= ptHeight
||
2559 stObj
->pt
->depth0
!= ptDepth
||
2560 stObj
->pt
->nr_samples
!= ptNumSamples
||
2561 stObj
->pt
->array_size
!= ptLayers
)
2563 /* The gallium texture does not match the Mesa texture so delete the
2564 * gallium texture now. We'll make a new one below.
2566 pipe_resource_reference(&stObj
->pt
, NULL
);
2567 st_texture_release_all_sampler_views(st
, stObj
);
2568 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
2572 /* May need to create a new gallium texture:
2575 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2577 stObj
->pt
= st_texture_create(st
,
2578 gl_target_to_pipe(stObj
->base
.Target
),
2584 ptLayers
, ptNumSamples
,
2588 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2593 /* Pull in any images not in the object's texture:
2595 for (face
= 0; face
< nr_faces
; face
++) {
2597 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2598 struct st_texture_image
*stImage
=
2599 st_texture_image(stObj
->base
.Image
[face
][level
]);
2601 /* Need to import images in main memory or held in other textures.
2603 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2607 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2608 height
= u_minify(ptHeight
, level
);
2612 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2613 depth
= u_minify(ptDepth
, level
);
2614 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2620 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2621 stImage
->base
.Height
== height
&&
2622 stImage
->base
.Depth
== depth
)) {
2623 /* src image fits expected dest mipmap level size */
2624 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2635 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2636 * for a whole mipmap stack.
2639 st_AllocTextureStorage(struct gl_context
*ctx
,
2640 struct gl_texture_object
*texObj
,
2641 GLsizei levels
, GLsizei width
,
2642 GLsizei height
, GLsizei depth
)
2644 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2645 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2646 struct st_context
*st
= st_context(ctx
);
2647 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2648 struct pipe_screen
*screen
= st
->pipe
->screen
;
2649 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
2650 enum pipe_format fmt
;
2652 GLuint num_samples
= texImage
->NumSamples
;
2656 stObj
->lastLevel
= levels
- 1;
2658 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2660 bindings
= default_bindings(st
, fmt
);
2662 /* Raise the sample count if the requested one is unsupported. */
2663 if (num_samples
> 1) {
2664 boolean found
= FALSE
;
2666 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2667 if (screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
2669 PIPE_BIND_SAMPLER_VIEW
)) {
2670 /* Update the sample count in gl_texture_image as well. */
2671 texImage
->NumSamples
= num_samples
;
2682 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2683 width
, height
, depth
,
2684 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2686 stObj
->pt
= st_texture_create(st
,
2687 gl_target_to_pipe(texObj
->Target
),
2693 ptLayers
, num_samples
,
2698 /* Set image resource pointers */
2699 for (level
= 0; level
< levels
; level
++) {
2701 for (face
= 0; face
< numFaces
; face
++) {
2702 struct st_texture_image
*stImage
=
2703 st_texture_image(texObj
->Image
[face
][level
]);
2704 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2706 etc_fallback_allocate(st
, stImage
);
2715 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2716 GLuint numLevels
, GLint level
,
2717 mesa_format format
, GLuint numSamples
,
2718 GLint width
, GLint height
, GLint depth
)
2720 struct st_context
*st
= st_context(ctx
);
2721 struct pipe_context
*pipe
= st
->pipe
;
2723 if (width
== 0 || height
== 0 || depth
== 0) {
2724 /* zero-sized images are legal, and always fit! */
2728 if (pipe
->screen
->can_create_resource
) {
2729 /* Ask the gallium driver if the texture is too large */
2730 struct gl_texture_object
*texObj
=
2731 _mesa_get_current_tex_object(ctx
, target
);
2732 struct pipe_resource pt
;
2734 /* Setup the pipe_resource object
2736 memset(&pt
, 0, sizeof(pt
));
2738 pt
.target
= gl_target_to_pipe(target
);
2739 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
2740 pt
.nr_samples
= numSamples
;
2742 st_gl_texture_dims_to_pipe_dims(target
,
2743 width
, height
, depth
,
2744 &pt
.width0
, &pt
.height0
,
2745 &pt
.depth0
, &pt
.array_size
);
2747 if (numLevels
> 0) {
2748 /* For immutable textures we know the final number of mip levels */
2749 pt
.last_level
= numLevels
- 1;
2751 else if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
2752 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
2753 /* assume just one mipmap level */
2757 /* assume a full set of mipmaps */
2758 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
2761 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
2764 /* Use core Mesa fallback */
2765 return _mesa_test_proxy_teximage(ctx
, target
, numLevels
, level
, format
,
2766 numSamples
, width
, height
, depth
);
2771 st_TextureView(struct gl_context
*ctx
,
2772 struct gl_texture_object
*texObj
,
2773 struct gl_texture_object
*origTexObj
)
2775 struct st_context
*st
= st_context(ctx
);
2776 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
2777 struct st_texture_object
*tex
= st_texture_object(texObj
);
2778 struct gl_texture_image
*image
= texObj
->Image
[0][0];
2780 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2781 const int numLevels
= texObj
->NumLevels
;
2786 pipe_resource_reference(&tex
->pt
, orig
->pt
);
2788 /* Set image resource pointers */
2789 for (level
= 0; level
< numLevels
; level
++) {
2790 for (face
= 0; face
< numFaces
; face
++) {
2791 struct st_texture_image
*stImage
=
2792 st_texture_image(texObj
->Image
[face
][level
]);
2793 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
2797 tex
->surface_based
= GL_TRUE
;
2798 tex
->surface_format
=
2799 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
2801 tex
->lastLevel
= numLevels
- 1;
2803 /* free texture sampler views. They need to be recreated when we
2804 * change the texture view parameters.
2806 st_texture_release_all_sampler_views(st
, tex
);
2812 st_ClearTexSubImage(struct gl_context
*ctx
,
2813 struct gl_texture_image
*texImage
,
2814 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2815 GLsizei width
, GLsizei height
, GLsizei depth
,
2816 const void *clearValue
)
2818 static const char zeros
[16] = {0};
2819 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2820 struct pipe_resource
*pt
= stImage
->pt
;
2821 struct st_context
*st
= st_context(ctx
);
2822 struct pipe_context
*pipe
= st
->pipe
;
2823 unsigned level
= texImage
->Level
;
2824 struct pipe_box box
;
2829 st_flush_bitmap_cache(st
);
2830 st_invalidate_readpix_cache(st
);
2832 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
2833 width
, height
, depth
, &box
);
2834 if (texImage
->TexObject
->Immutable
) {
2835 level
+= texImage
->TexObject
->MinLevel
;
2836 box
.z
+= texImage
->TexObject
->MinLayer
;
2839 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
2844 * Called via the glTexParam*() function, but only when some texture object
2845 * state has actually changed.
2848 st_TexParameter(struct gl_context
*ctx
,
2849 struct gl_texture_object
*texObj
, GLenum pname
)
2851 struct st_context
*st
= st_context(ctx
);
2852 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2855 case GL_TEXTURE_BASE_LEVEL
:
2856 case GL_TEXTURE_MAX_LEVEL
:
2857 case GL_DEPTH_TEXTURE_MODE
:
2858 case GL_DEPTH_STENCIL_TEXTURE_MODE
:
2859 case GL_TEXTURE_SRGB_DECODE_EXT
:
2860 case GL_TEXTURE_SWIZZLE_R
:
2861 case GL_TEXTURE_SWIZZLE_G
:
2862 case GL_TEXTURE_SWIZZLE_B
:
2863 case GL_TEXTURE_SWIZZLE_A
:
2864 case GL_TEXTURE_SWIZZLE_RGBA
:
2865 case GL_TEXTURE_BUFFER_SIZE
:
2866 case GL_TEXTURE_BUFFER_OFFSET
:
2867 /* changing any of these texture parameters means we must create
2868 * new sampler views.
2870 st_texture_release_all_sampler_views(st
, stObj
);
2879 st_init_texture_functions(struct dd_function_table
*functions
)
2881 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
2882 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
2883 functions
->TexImage
= st_TexImage
;
2884 functions
->TexSubImage
= st_TexSubImage
;
2885 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
2886 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
2887 functions
->GenerateMipmap
= st_generate_mipmap
;
2889 functions
->GetTexSubImage
= st_GetTexSubImage
;
2891 /* compressed texture functions */
2892 functions
->CompressedTexImage
= st_CompressedTexImage
;
2893 functions
->GetCompressedTexSubImage
= _mesa_GetCompressedTexSubImage_sw
;
2895 functions
->NewTextureObject
= st_NewTextureObject
;
2896 functions
->NewTextureImage
= st_NewTextureImage
;
2897 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
2898 functions
->DeleteTexture
= st_DeleteTextureObject
;
2899 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
2900 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
2901 functions
->MapTextureImage
= st_MapTextureImage
;
2902 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
2904 /* XXX Temporary until we can query pipe's texture sizes */
2905 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
2907 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
2908 functions
->TextureView
= st_TextureView
;
2909 functions
->ClearTexSubImage
= st_ClearTexSubImage
;
2911 functions
->TexParameter
= st_TexParameter
;