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"
62 #include "pipe/p_context.h"
63 #include "pipe/p_defines.h"
64 #include "util/u_inlines.h"
65 #include "util/u_upload_mgr.h"
66 #include "pipe/p_shader_tokens.h"
67 #include "util/u_tile.h"
68 #include "util/u_format.h"
69 #include "util/u_surface.h"
70 #include "util/u_sampler.h"
71 #include "util/u_math.h"
72 #include "util/u_box.h"
73 #include "util/u_simple_shaders.h"
74 #include "cso_cache/cso_context.h"
75 #include "tgsi/tgsi_ureg.h"
77 #define DBG if (0) printf
80 enum pipe_texture_target
81 gl_target_to_pipe(GLenum target
)
85 case GL_PROXY_TEXTURE_1D
:
86 return PIPE_TEXTURE_1D
;
88 case GL_PROXY_TEXTURE_2D
:
89 case GL_TEXTURE_EXTERNAL_OES
:
90 case GL_TEXTURE_2D_MULTISAMPLE
:
91 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
92 return PIPE_TEXTURE_2D
;
93 case GL_TEXTURE_RECTANGLE_NV
:
94 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
95 return PIPE_TEXTURE_RECT
;
97 case GL_PROXY_TEXTURE_3D
:
98 return PIPE_TEXTURE_3D
;
99 case GL_TEXTURE_CUBE_MAP_ARB
:
100 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
101 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
102 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
103 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
104 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
105 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
106 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
107 return PIPE_TEXTURE_CUBE
;
108 case GL_TEXTURE_1D_ARRAY_EXT
:
109 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
110 return PIPE_TEXTURE_1D_ARRAY
;
111 case GL_TEXTURE_2D_ARRAY_EXT
:
112 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
113 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
114 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
115 return PIPE_TEXTURE_2D_ARRAY
;
116 case GL_TEXTURE_BUFFER
:
118 case GL_TEXTURE_CUBE_MAP_ARRAY
:
119 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
120 return PIPE_TEXTURE_CUBE_ARRAY
;
128 /** called via ctx->Driver.NewTextureImage() */
129 static struct gl_texture_image
*
130 st_NewTextureImage(struct gl_context
* ctx
)
132 DBG("%s\n", __func__
);
134 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
138 /** called via ctx->Driver.DeleteTextureImage() */
140 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
142 /* nothing special (yet) for st_texture_image */
143 _mesa_delete_texture_image(ctx
, img
);
147 /** called via ctx->Driver.NewTextureObject() */
148 static struct gl_texture_object
*
149 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
151 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
153 DBG("%s\n", __func__
);
154 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
159 /** called via ctx->Driver.DeleteTextureObject() */
161 st_DeleteTextureObject(struct gl_context
*ctx
,
162 struct gl_texture_object
*texObj
)
164 struct st_context
*st
= st_context(ctx
);
165 struct st_texture_object
*stObj
= st_texture_object(texObj
);
167 pipe_resource_reference(&stObj
->pt
, NULL
);
168 st_texture_release_all_sampler_views(st
, stObj
);
169 st_texture_free_sampler_views(stObj
);
170 _mesa_delete_texture_object(ctx
, texObj
);
174 /** called via ctx->Driver.FreeTextureImageBuffer() */
176 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
177 struct gl_texture_image
*texImage
)
179 struct st_texture_image
*stImage
= st_texture_image(texImage
);
181 DBG("%s\n", __func__
);
184 pipe_resource_reference(&stImage
->pt
, NULL
);
187 free(stImage
->transfer
);
188 stImage
->transfer
= NULL
;
189 stImage
->num_transfers
= 0;
193 /** called via ctx->Driver.MapTextureImage() */
195 st_MapTextureImage(struct gl_context
*ctx
,
196 struct gl_texture_image
*texImage
,
197 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
199 GLubyte
**mapOut
, GLint
*rowStrideOut
)
201 struct st_context
*st
= st_context(ctx
);
202 struct st_texture_image
*stImage
= st_texture_image(texImage
);
205 struct pipe_transfer
*transfer
;
208 if (mode
& GL_MAP_READ_BIT
)
209 pipeMode
|= PIPE_TRANSFER_READ
;
210 if (mode
& GL_MAP_WRITE_BIT
)
211 pipeMode
|= PIPE_TRANSFER_WRITE
;
212 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
213 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
215 map
= st_texture_image_map(st
, stImage
, pipeMode
, x
, y
, slice
, w
, h
, 1,
218 if ((_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
219 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
)) {
220 /* ETC isn't supported by gallium and it's represented
221 * by uncompressed formats. Only write transfers with precompressed
222 * data are supported by ES3, which makes this really simple.
224 * Just create a temporary storage where the ETC texture will
225 * be stored. It will be decompressed in the Unmap function.
227 unsigned z
= transfer
->box
.z
;
228 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
230 itransfer
->temp_data
=
231 malloc(_mesa_format_image_size(texImage
->TexFormat
, w
, h
, 1));
232 itransfer
->temp_stride
=
233 _mesa_format_row_stride(texImage
->TexFormat
, w
);
234 itransfer
->map
= map
;
236 *mapOut
= itransfer
->temp_data
;
237 *rowStrideOut
= itransfer
->temp_stride
;
240 /* supported mapping */
242 *rowStrideOut
= transfer
->stride
;
252 /** called via ctx->Driver.UnmapTextureImage() */
254 st_UnmapTextureImage(struct gl_context
*ctx
,
255 struct gl_texture_image
*texImage
,
258 struct st_context
*st
= st_context(ctx
);
259 struct st_texture_image
*stImage
= st_texture_image(texImage
);
261 if ((_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
262 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
)) {
263 /* Decompress the ETC texture to the mapped one. */
264 unsigned z
= slice
+ stImage
->base
.Face
;
265 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
266 struct pipe_transfer
*transfer
= itransfer
->transfer
;
268 assert(z
== transfer
->box
.z
);
270 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
271 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
272 itransfer
->temp_data
,
273 itransfer
->temp_stride
,
274 transfer
->box
.width
, transfer
->box
.height
);
277 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
278 itransfer
->temp_data
, itransfer
->temp_stride
,
279 transfer
->box
.width
, transfer
->box
.height
,
280 texImage
->TexFormat
);
283 free(itransfer
->temp_data
);
284 itransfer
->temp_data
= NULL
;
285 itransfer
->temp_stride
= 0;
289 st_texture_image_unmap(st
, stImage
, slice
);
294 * Return default texture resource binding bitmask for the given format.
297 default_bindings(struct st_context
*st
, enum pipe_format format
)
299 struct pipe_screen
*screen
= st
->pipe
->screen
;
300 const unsigned target
= PIPE_TEXTURE_2D
;
303 if (util_format_is_depth_or_stencil(format
))
304 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
306 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
308 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
312 format
= util_format_linear(format
);
314 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
317 return PIPE_BIND_SAMPLER_VIEW
;
323 * Given the size of a mipmap image, try to compute the size of the level=0
326 * Note that this isn't always accurate for odd-sized, non-POW textures.
327 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
329 * \return GL_TRUE for success, GL_FALSE for failure
332 guess_base_level_size(GLenum target
,
333 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
334 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
341 /* Guess the size of the base level.
342 * Depending on the image's size, we can't always make a guess here.
346 case GL_TEXTURE_1D_ARRAY
:
351 case GL_TEXTURE_2D_ARRAY
:
352 /* We can't make a good guess here, because the base level dimensions
355 if (width
== 1 || height
== 1) {
362 case GL_TEXTURE_CUBE_MAP
:
363 case GL_TEXTURE_CUBE_MAP_ARRAY
:
369 /* We can't make a good guess here, because the base level dimensions
372 if (width
== 1 || height
== 1 || depth
== 1) {
380 case GL_TEXTURE_RECTANGLE
:
397 * Try to determine whether we should allocate memory for a full texture
398 * mipmap. The problem is when we get a glTexImage(level=0) call, we
399 * can't immediately know if other mipmap levels are coming next. Here
400 * we try to guess whether to allocate memory for a mipmap or just the
403 * If we guess incorrectly here we'll later reallocate the right amount of
404 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
406 * \param stObj the texture object we're going to allocate memory for.
407 * \param stImage describes the incoming image which we need to store.
410 allocate_full_mipmap(const struct st_texture_object
*stObj
,
411 const struct st_texture_image
*stImage
)
413 switch (stObj
->base
.Target
) {
414 case GL_TEXTURE_RECTANGLE_NV
:
415 case GL_TEXTURE_BUFFER
:
416 case GL_TEXTURE_EXTERNAL_OES
:
417 case GL_TEXTURE_2D_MULTISAMPLE
:
418 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
419 /* these texture types cannot be mipmapped */
423 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
426 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
427 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
428 /* depth/stencil textures are seldom mipmapped */
431 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
434 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
435 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
436 /* not a mipmap minification filter */
439 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
440 /* 3D textures are seldom mipmapped */
448 * Try to allocate a pipe_resource object for the given st_texture_object.
450 * We use the given st_texture_image as a clue to determine the size of the
451 * mipmap image at level=0.
453 * \return GL_TRUE for success, GL_FALSE if out of memory.
456 guess_and_alloc_texture(struct st_context
*st
,
457 struct st_texture_object
*stObj
,
458 const struct st_texture_image
*stImage
)
460 const struct gl_texture_image
*firstImage
;
461 GLuint lastLevel
, width
, height
, depth
;
463 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
464 enum pipe_format fmt
;
465 bool guessed_box
= false;
467 DBG("%s\n", __func__
);
471 /* If a base level image with compatible size exists, use that as our guess.
473 firstImage
= _mesa_base_tex_image(&stObj
->base
);
475 firstImage
->Width2
> 0 &&
476 firstImage
->Height2
> 0 &&
477 firstImage
->Depth2
> 0 &&
478 guess_base_level_size(stObj
->base
.Target
,
483 &width
, &height
, &depth
)) {
484 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
485 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
486 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
491 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
492 stImage
->base
.Width2
,
493 stImage
->base
.Height2
,
494 stImage
->base
.Depth2
,
496 &width
, &height
, &depth
);
499 /* we can't determine the image size at level=0 */
500 /* this is not an out of memory error */
504 /* At this point, (width x height x depth) is the expected size of
505 * the level=0 mipmap image.
508 /* Guess a reasonable value for lastLevel. With OpenGL we have no
509 * idea how many mipmap levels will be in a texture until we start
510 * to render with it. Make an educated guess here but be prepared
511 * to re-allocating a texture buffer with space for more (or fewer)
512 * mipmap levels later.
514 if (allocate_full_mipmap(stObj
, stImage
)) {
515 /* alloc space for a full mipmap */
516 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
517 width
, height
, depth
) - 1;
520 /* only alloc space for a single mipmap level */
524 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
526 bindings
= default_bindings(st
, fmt
);
528 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
529 width
, height
, depth
,
530 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
532 stObj
->pt
= st_texture_create(st
,
533 gl_target_to_pipe(stObj
->base
.Target
),
542 stObj
->lastLevel
= lastLevel
;
544 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
546 return stObj
->pt
!= NULL
;
551 * Called via ctx->Driver.AllocTextureImageBuffer().
552 * If the texture object/buffer already has space for the indicated image,
553 * we're done. Otherwise, allocate memory for the new texture image.
556 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
557 struct gl_texture_image
*texImage
)
559 struct st_context
*st
= st_context(ctx
);
560 struct st_texture_image
*stImage
= st_texture_image(texImage
);
561 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
562 const GLuint level
= texImage
->Level
;
563 GLuint width
= texImage
->Width
;
564 GLuint height
= texImage
->Height
;
565 GLuint depth
= texImage
->Depth
;
567 DBG("%s\n", __func__
);
569 assert(!stImage
->pt
); /* xxx this might be wrong */
571 /* Look if the parent texture object has space for this image */
573 level
<= stObj
->pt
->last_level
&&
574 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
575 /* this image will fit in the existing texture object's memory */
576 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
580 /* The parent texture object does not have space for this image */
582 pipe_resource_reference(&stObj
->pt
, NULL
);
583 st_texture_release_all_sampler_views(st
, stObj
);
585 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
586 /* Probably out of memory.
587 * Try flushing any pending rendering, then retry.
590 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
591 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
597 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
598 /* The image will live in the object's mipmap memory */
599 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
604 /* Create a new, temporary texture/resource/buffer to hold this
605 * one texture image. Note that when we later access this image
606 * (either for mapping or copying) we'll want to always specify
607 * mipmap level=0, even if the image represents some other mipmap
610 enum pipe_format format
=
611 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
612 GLuint bindings
= default_bindings(st
, format
);
613 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
615 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
616 width
, height
, depth
,
617 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
619 stImage
->pt
= st_texture_create(st
,
620 gl_target_to_pipe(stObj
->base
.Target
),
628 return stImage
->pt
!= NULL
;
634 * Preparation prior to glTexImage. Basically check the 'surface_based'
635 * field and switch to a "normal" tex image if necessary.
638 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
639 GLenum format
, GLenum type
)
641 struct gl_texture_object
*texObj
= texImage
->TexObject
;
642 struct st_texture_object
*stObj
= st_texture_object(texObj
);
644 /* switch to "normal" */
645 if (stObj
->surface_based
) {
646 const GLenum target
= texObj
->Target
;
647 const GLuint level
= texImage
->Level
;
648 mesa_format texFormat
;
650 _mesa_clear_texture_object(ctx
, texObj
);
651 pipe_resource_reference(&stObj
->pt
, NULL
);
653 /* oops, need to init this image again */
654 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
655 texImage
->InternalFormat
, format
,
658 _mesa_init_teximage_fields(ctx
, texImage
,
659 texImage
->Width
, texImage
->Height
,
660 texImage
->Depth
, texImage
->Border
,
661 texImage
->InternalFormat
, texFormat
);
663 stObj
->surface_based
= GL_FALSE
;
669 * Return a writemask for the gallium blit. The parameters can be base
670 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
673 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
676 case GL_DEPTH_STENCIL
:
678 case GL_DEPTH_STENCIL
:
680 case GL_DEPTH_COMPONENT
:
682 case GL_STENCIL_INDEX
:
689 case GL_DEPTH_COMPONENT
:
691 case GL_DEPTH_STENCIL
:
692 case GL_DEPTH_COMPONENT
:
699 case GL_STENCIL_INDEX
:
701 case GL_STENCIL_INDEX
:
709 return PIPE_MASK_RGBA
;
714 * Converts format to a format with the same components, types
715 * and sizes, but with the components in RGBA order.
717 static enum pipe_format
718 unswizzle_format(enum pipe_format format
)
722 case PIPE_FORMAT_B8G8R8A8_UNORM
:
723 case PIPE_FORMAT_A8R8G8B8_UNORM
:
724 case PIPE_FORMAT_A8B8G8R8_UNORM
:
725 return PIPE_FORMAT_R8G8B8A8_UNORM
;
727 case PIPE_FORMAT_B10G10R10A2_UNORM
:
728 return PIPE_FORMAT_R10G10B10A2_UNORM
;
730 case PIPE_FORMAT_B10G10R10A2_SNORM
:
731 return PIPE_FORMAT_R10G10B10A2_SNORM
;
733 case PIPE_FORMAT_B10G10R10A2_UINT
:
734 return PIPE_FORMAT_R10G10B10A2_UINT
;
742 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
744 static enum pipe_format
745 alpha_to_red(enum pipe_format format
)
749 case PIPE_FORMAT_A8_UNORM
:
750 return PIPE_FORMAT_R8_UNORM
;
751 case PIPE_FORMAT_A8_SNORM
:
752 return PIPE_FORMAT_R8_SNORM
;
753 case PIPE_FORMAT_A8_UINT
:
754 return PIPE_FORMAT_R8_UINT
;
755 case PIPE_FORMAT_A8_SINT
:
756 return PIPE_FORMAT_R8_SINT
;
758 case PIPE_FORMAT_A16_UNORM
:
759 return PIPE_FORMAT_R16_UNORM
;
760 case PIPE_FORMAT_A16_SNORM
:
761 return PIPE_FORMAT_R16_SNORM
;
762 case PIPE_FORMAT_A16_UINT
:
763 return PIPE_FORMAT_R16_UINT
;
764 case PIPE_FORMAT_A16_SINT
:
765 return PIPE_FORMAT_R16_SINT
;
766 case PIPE_FORMAT_A16_FLOAT
:
767 return PIPE_FORMAT_R16_FLOAT
;
769 case PIPE_FORMAT_A32_UINT
:
770 return PIPE_FORMAT_R32_UINT
;
771 case PIPE_FORMAT_A32_SINT
:
772 return PIPE_FORMAT_R32_SINT
;
773 case PIPE_FORMAT_A32_FLOAT
:
774 return PIPE_FORMAT_R32_FLOAT
;
782 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
784 static enum pipe_format
785 red_alpha_to_red_green(enum pipe_format format
)
789 case PIPE_FORMAT_R8A8_UNORM
:
790 return PIPE_FORMAT_R8G8_UNORM
;
791 case PIPE_FORMAT_R8A8_SNORM
:
792 return PIPE_FORMAT_R8G8_SNORM
;
793 case PIPE_FORMAT_R8A8_UINT
:
794 return PIPE_FORMAT_R8G8_UINT
;
795 case PIPE_FORMAT_R8A8_SINT
:
796 return PIPE_FORMAT_R8G8_SINT
;
798 case PIPE_FORMAT_R16A16_UNORM
:
799 return PIPE_FORMAT_R16G16_UNORM
;
800 case PIPE_FORMAT_R16A16_SNORM
:
801 return PIPE_FORMAT_R16G16_SNORM
;
802 case PIPE_FORMAT_R16A16_UINT
:
803 return PIPE_FORMAT_R16G16_UINT
;
804 case PIPE_FORMAT_R16A16_SINT
:
805 return PIPE_FORMAT_R16G16_SINT
;
806 case PIPE_FORMAT_R16A16_FLOAT
:
807 return PIPE_FORMAT_R16G16_FLOAT
;
809 case PIPE_FORMAT_R32A32_UINT
:
810 return PIPE_FORMAT_R32G32_UINT
;
811 case PIPE_FORMAT_R32A32_SINT
:
812 return PIPE_FORMAT_R32G32_SINT
;
813 case PIPE_FORMAT_R32A32_FLOAT
:
814 return PIPE_FORMAT_R32G32_FLOAT
;
822 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
824 static enum pipe_format
825 luminance_alpha_to_red_green(enum pipe_format format
)
829 case PIPE_FORMAT_L8A8_UNORM
:
830 return PIPE_FORMAT_R8G8_UNORM
;
831 case PIPE_FORMAT_L8A8_SNORM
:
832 return PIPE_FORMAT_R8G8_SNORM
;
833 case PIPE_FORMAT_L8A8_UINT
:
834 return PIPE_FORMAT_R8G8_UINT
;
835 case PIPE_FORMAT_L8A8_SINT
:
836 return PIPE_FORMAT_R8G8_SINT
;
838 case PIPE_FORMAT_L16A16_UNORM
:
839 return PIPE_FORMAT_R16G16_UNORM
;
840 case PIPE_FORMAT_L16A16_SNORM
:
841 return PIPE_FORMAT_R16G16_SNORM
;
842 case PIPE_FORMAT_L16A16_UINT
:
843 return PIPE_FORMAT_R16G16_UINT
;
844 case PIPE_FORMAT_L16A16_SINT
:
845 return PIPE_FORMAT_R16G16_SINT
;
846 case PIPE_FORMAT_L16A16_FLOAT
:
847 return PIPE_FORMAT_R16G16_FLOAT
;
849 case PIPE_FORMAT_L32A32_UINT
:
850 return PIPE_FORMAT_R32G32_UINT
;
851 case PIPE_FORMAT_L32A32_SINT
:
852 return PIPE_FORMAT_R32G32_SINT
;
853 case PIPE_FORMAT_L32A32_FLOAT
:
854 return PIPE_FORMAT_R32G32_FLOAT
;
862 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
865 format_is_alpha(enum pipe_format format
)
867 const struct util_format_description
*desc
= util_format_description(format
);
869 if (desc
->nr_channels
== 1 &&
870 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
871 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
872 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
873 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
880 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
883 format_is_red(enum pipe_format format
)
885 const struct util_format_description
*desc
= util_format_description(format
);
887 if (desc
->nr_channels
== 1 &&
888 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
889 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
890 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
891 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
899 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
902 format_is_luminance(enum pipe_format format
)
904 const struct util_format_description
*desc
= util_format_description(format
);
906 if (desc
->nr_channels
== 1 &&
907 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
908 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
909 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
910 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
917 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
920 format_is_red_alpha(enum pipe_format format
)
922 const struct util_format_description
*desc
= util_format_description(format
);
924 if (desc
->nr_channels
== 2 &&
925 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
926 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
927 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
928 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
935 format_is_swizzled_rgba(enum pipe_format format
)
937 const struct util_format_description
*desc
= util_format_description(format
);
939 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
940 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
941 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
942 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
950 unsigned char swizzle
[4];
951 enum pipe_format format
;
954 static const struct format_table table_8888_unorm
[] = {
955 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
956 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
957 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
958 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
961 static const struct format_table table_1010102_unorm
[] = {
962 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
963 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
966 static const struct format_table table_1010102_snorm
[] = {
967 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
968 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
971 static const struct format_table table_1010102_uint
[] = {
972 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
973 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
976 static enum pipe_format
977 swizzle_format(enum pipe_format format
, const int * const swizzle
)
982 case PIPE_FORMAT_R8G8B8A8_UNORM
:
983 case PIPE_FORMAT_B8G8R8A8_UNORM
:
984 case PIPE_FORMAT_A8R8G8B8_UNORM
:
985 case PIPE_FORMAT_A8B8G8R8_UNORM
:
986 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
987 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
988 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
989 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
990 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
991 return table_8888_unorm
[i
].format
;
995 case PIPE_FORMAT_R10G10B10A2_UNORM
:
996 case PIPE_FORMAT_B10G10R10A2_UNORM
:
997 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
998 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
999 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
1000 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
1001 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
1002 return table_1010102_unorm
[i
].format
;
1006 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1007 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1008 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1009 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1010 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1011 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1012 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1013 return table_1010102_snorm
[i
].format
;
1017 case PIPE_FORMAT_R10G10B10A2_UINT
:
1018 case PIPE_FORMAT_B10G10R10A2_UINT
:
1019 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1020 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1021 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1022 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1023 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1024 return table_1010102_uint
[i
].format
;
1032 return PIPE_FORMAT_NONE
;
1036 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1038 enum pipe_format src
= *src_format
;
1039 enum pipe_format dst
= *dst_format
;
1041 /* Note: dst_format has already been transformed from luminance/intensity
1042 * to red when this function is called. The source format will never
1043 * be an intensity format, because GL_INTENSITY is not a legal value
1044 * for the format parameter in glTex(Sub)Image(). */
1046 if (format_is_alpha(src
)) {
1047 if (!format_is_alpha(dst
))
1050 src
= alpha_to_red(src
);
1051 dst
= alpha_to_red(dst
);
1052 } else if (format_is_luminance(src
)) {
1053 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1056 src
= util_format_luminance_to_red(src
);
1057 } else if (util_format_is_luminance_alpha(src
)) {
1058 src
= luminance_alpha_to_red_green(src
);
1060 if (format_is_red_alpha(dst
)) {
1061 dst
= red_alpha_to_red_green(dst
);
1062 } else if (!format_is_red(dst
))
1064 } else if (format_is_swizzled_rgba(src
)) {
1065 const struct util_format_description
*src_desc
= util_format_description(src
);
1066 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1070 /* Make sure the format is an RGBA and not an RGBX format */
1071 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1074 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1077 for (i
= 0; i
< 4; i
++)
1078 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1080 dst
= swizzle_format(dst
, swizzle
);
1081 if (dst
== PIPE_FORMAT_NONE
)
1084 src
= unswizzle_format(src
);
1093 try_pbo_upload_common(struct gl_context
*ctx
,
1094 struct pipe_surface
*surface
,
1095 const struct st_pbo_addresses
*addr
,
1096 enum pipe_format src_format
)
1098 struct st_context
*st
= st_context(ctx
);
1099 struct cso_context
*cso
= st
->cso_context
;
1100 struct pipe_context
*pipe
= st
->pipe
;
1101 bool success
= false;
1103 /* Create fragment shader */
1104 if (!st
->pbo
.upload_fs
) {
1105 st
->pbo
.upload_fs
= st_pbo_create_upload_fs(st
);
1106 if (!st
->pbo
.upload_fs
)
1110 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1111 CSO_BIT_FRAGMENT_SAMPLERS
|
1112 CSO_BIT_VERTEX_ELEMENTS
|
1113 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1114 CSO_BIT_FRAMEBUFFER
|
1117 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1118 CSO_BIT_RASTERIZER
|
1119 CSO_BIT_STREAM_OUTPUTS
|
1120 CSO_BIT_PAUSE_QUERIES
|
1121 CSO_BITS_ALL_SHADERS
));
1122 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1125 /* Set up the sampler_view */
1127 struct pipe_sampler_view templ
;
1128 struct pipe_sampler_view
*sampler_view
;
1129 struct pipe_sampler_state sampler
= {0};
1130 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
1132 memset(&templ
, 0, sizeof(templ
));
1133 templ
.target
= PIPE_BUFFER
;
1134 templ
.format
= src_format
;
1135 templ
.u
.buf
.first_element
= addr
->first_element
;
1136 templ
.u
.buf
.last_element
= addr
->last_element
;
1137 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1138 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1139 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1140 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1142 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1143 if (sampler_view
== NULL
)
1146 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1148 pipe_sampler_view_reference(&sampler_view
, NULL
);
1150 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
1153 /* Framebuffer_state */
1155 struct pipe_framebuffer_state fb
;
1156 memset(&fb
, 0, sizeof(fb
));
1157 fb
.width
= surface
->width
;
1158 fb
.height
= surface
->height
;
1160 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1162 cso_set_framebuffer(cso
, &fb
);
1164 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1167 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1170 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1172 /* Depth/stencil/alpha state */
1174 struct pipe_depth_stencil_alpha_state dsa
;
1175 memset(&dsa
, 0, sizeof(dsa
));
1176 cso_set_depth_stencil_alpha(cso
, &dsa
);
1179 /* Set up the fragment shader */
1180 cso_set_fragment_shader_handle(cso
, st
->pbo
.upload_fs
);
1182 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1185 cso_restore_state(cso
);
1186 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1192 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1193 struct gl_texture_image
*texImage
,
1194 GLenum format
, GLenum type
,
1195 enum pipe_format dst_format
,
1196 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1197 GLint width
, GLint height
, GLint depth
,
1199 const struct gl_pixelstore_attrib
*unpack
)
1201 struct st_context
*st
= st_context(ctx
);
1202 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1203 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1204 struct pipe_resource
*texture
= stImage
->pt
;
1205 struct pipe_context
*pipe
= st
->pipe
;
1206 struct pipe_screen
*screen
= pipe
->screen
;
1207 struct pipe_surface
*surface
= NULL
;
1208 struct st_pbo_addresses addr
;
1209 enum pipe_format src_format
;
1210 const struct util_format_description
*desc
;
1211 GLenum gl_target
= texImage
->TexObject
->Target
;
1214 if (!st
->pbo
.upload_enabled
)
1217 /* From now on, we need the gallium representation of dimensions. */
1218 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1225 if (depth
!= 1 && !st
->pbo
.layers
)
1228 /* Choose the source format. Initially, we do so without checking driver
1229 * support at all because of the remapping we later perform and because
1230 * at least the Radeon driver actually supports some formats for texture
1231 * buffers which it doesn't support for regular textures. */
1232 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1237 src_format
= util_format_linear(src_format
);
1238 desc
= util_format_description(src_format
);
1240 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1243 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1246 if (st
->pbo
.rgba_only
) {
1247 enum pipe_format orig_dst_format
= dst_format
;
1249 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1253 if (dst_format
!= orig_dst_format
&&
1254 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1255 PIPE_BIND_RENDER_TARGET
)) {
1261 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0,
1262 PIPE_BIND_SAMPLER_VIEW
)) {
1266 /* Compute buffer addresses */
1267 addr
.xoffset
= xoffset
;
1268 addr
.yoffset
= yoffset
;
1270 addr
.height
= height
;
1272 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1274 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1278 /* Set up the surface */
1280 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1281 unsigned max_layer
= util_max_layer(texture
, level
);
1283 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1285 struct pipe_surface templ
;
1286 memset(&templ
, 0, sizeof(templ
));
1287 templ
.format
= dst_format
;
1288 templ
.u
.tex
.level
= level
;
1289 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1290 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1292 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1297 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1299 pipe_surface_reference(&surface
, NULL
);
1305 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1306 struct gl_texture_image
*texImage
,
1307 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1308 GLint width
, GLint height
, GLint depth
,
1309 GLenum format
, GLenum type
, const void *pixels
,
1310 const struct gl_pixelstore_attrib
*unpack
)
1312 struct st_context
*st
= st_context(ctx
);
1313 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1314 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1315 struct pipe_context
*pipe
= st
->pipe
;
1316 struct pipe_screen
*screen
= pipe
->screen
;
1317 struct pipe_resource
*dst
= stImage
->pt
;
1318 struct pipe_resource
*src
= NULL
;
1319 struct pipe_resource src_templ
;
1320 struct pipe_transfer
*transfer
;
1321 struct pipe_blit_info blit
;
1322 enum pipe_format src_format
, dst_format
;
1323 mesa_format mesa_src_format
;
1324 GLenum gl_target
= texImage
->TexObject
->Target
;
1327 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1328 unsigned dst_level
= 0;
1330 st_flush_bitmap_cache(st
);
1331 st_invalidate_readpix_cache(st
);
1333 if (stObj
->pt
== stImage
->pt
)
1334 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1336 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1337 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1342 /* Try transfer_inline_write, which should be the fastest memcpy path. */
1344 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1345 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1346 texImage
->TexFormat
, format
, type
,
1348 struct pipe_box box
;
1349 unsigned stride
, layer_stride
;
1352 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1353 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1355 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1358 /* Convert to Gallium coordinates. */
1359 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1364 layer_stride
= stride
;
1367 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1368 pipe
->transfer_inline_write(pipe
, dst
, dst_level
, 0,
1369 &box
, data
, stride
, layer_stride
);
1373 if (!st
->prefer_blit_based_texture_transfer
) {
1377 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1378 * blit implementation in some drivers. */
1379 if (format
== GL_DEPTH_STENCIL
) {
1383 /* If the base internal format and the texture format don't match,
1384 * we can't use blit-based TexSubImage. */
1385 if (texImage
->_BaseFormat
!=
1386 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1391 /* See if the destination format is supported. */
1392 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1393 bind
= PIPE_BIND_DEPTH_STENCIL
;
1395 bind
= PIPE_BIND_RENDER_TARGET
;
1397 /* For luminance and intensity, only the red channel is stored
1398 * in the destination. */
1399 dst_format
= util_format_linear(dst
->format
);
1400 dst_format
= util_format_luminance_to_red(dst_format
);
1401 dst_format
= util_format_intensity_to_red(dst_format
);
1404 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1405 dst
->nr_samples
, bind
)) {
1409 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1410 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1411 xoffset
, yoffset
, zoffset
,
1412 width
, height
, depth
, pixels
, unpack
))
1416 /* See if the texture format already matches the format and type,
1417 * in which case the memcpy-based fast path will likely be used and
1418 * we don't have to blit. */
1419 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1420 type
, unpack
->SwapBytes
, NULL
)) {
1424 /* Choose the source format. */
1425 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1426 format
, type
, unpack
->SwapBytes
);
1431 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1433 /* There is no reason to do this if we cannot use memcpy for the temporary
1434 * source texture at least. This also takes transfer ops into account,
1436 if (!_mesa_texstore_can_use_memcpy(ctx
,
1437 _mesa_get_format_base_format(mesa_src_format
),
1438 mesa_src_format
, format
, type
, unpack
)) {
1442 /* TexSubImage only sets a single cubemap face. */
1443 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1444 gl_target
= GL_TEXTURE_2D
;
1446 /* TexSubImage can specify subsets of cube map array faces
1447 * so we need to upload via 2D array instead */
1448 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1449 gl_target
= GL_TEXTURE_2D_ARRAY
;
1452 /* Initialize the source texture description. */
1453 memset(&src_templ
, 0, sizeof(src_templ
));
1454 src_templ
.target
= gl_target_to_pipe(gl_target
);
1455 src_templ
.format
= src_format
;
1456 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1457 src_templ
.usage
= PIPE_USAGE_STAGING
;
1459 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1460 &src_templ
.width0
, &src_templ
.height0
,
1461 &src_templ
.depth0
, &src_templ
.array_size
);
1463 /* Check for NPOT texture support. */
1464 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1465 (!util_is_power_of_two(src_templ
.width0
) ||
1466 !util_is_power_of_two(src_templ
.height0
) ||
1467 !util_is_power_of_two(src_templ
.depth0
))) {
1471 /* Create the source texture. */
1472 src
= screen
->resource_create(screen
, &src_templ
);
1477 /* Map source pixels. */
1478 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1479 format
, type
, pixels
, unpack
,
1482 /* This is a GL error. */
1483 pipe_resource_reference(&src
, NULL
);
1487 /* From now on, we need the gallium representation of dimensions. */
1488 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1495 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1496 width
, height
, depth
, &transfer
);
1498 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1499 pipe_resource_reference(&src
, NULL
);
1503 /* Upload pixels (just memcpy). */
1505 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1508 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1509 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1510 /* 1D array textures.
1511 * We need to convert gallium coords to GL coords.
1513 void *src
= _mesa_image_address2d(unpack
, pixels
,
1514 width
, depth
, format
,
1516 memcpy(map
, src
, bytesPerRow
);
1519 ubyte
*slice_map
= map
;
1521 for (row
= 0; row
< (unsigned) height
; row
++) {
1522 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1523 width
, height
, format
,
1524 type
, slice
, row
, 0);
1525 memcpy(slice_map
, src
, bytesPerRow
);
1526 slice_map
+= transfer
->stride
;
1529 map
+= transfer
->layer_stride
;
1533 pipe_transfer_unmap(pipe
, transfer
);
1534 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1537 memset(&blit
, 0, sizeof(blit
));
1538 blit
.src
.resource
= src
;
1540 blit
.src
.format
= src_format
;
1541 blit
.dst
.resource
= dst
;
1542 blit
.dst
.level
= dst_level
;
1543 blit
.dst
.format
= dst_format
;
1544 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1545 blit
.dst
.box
.x
= xoffset
;
1546 blit
.dst
.box
.y
= yoffset
;
1547 blit
.dst
.box
.z
= zoffset
+ dstz
;
1548 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1549 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1550 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1551 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1552 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1553 blit
.scissor_enable
= FALSE
;
1555 st
->pipe
->blit(st
->pipe
, &blit
);
1557 pipe_resource_reference(&src
, NULL
);
1561 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1562 width
, height
, depth
, format
, type
, pixels
,
1567 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1568 struct gl_texture_image
*texImage
,
1569 GLenum format
, GLenum type
, const void *pixels
,
1570 const struct gl_pixelstore_attrib
*unpack
)
1572 assert(dims
== 1 || dims
== 2 || dims
== 3);
1574 prep_teximage(ctx
, texImage
, format
, type
);
1576 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1579 /* allocate storage for texture data */
1580 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1581 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1585 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1586 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1587 format
, type
, pixels
, unpack
);
1592 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1593 struct gl_texture_image
*texImage
,
1594 GLint x
, GLint y
, GLint z
,
1595 GLsizei w
, GLsizei h
, GLsizei d
,
1596 GLenum format
, GLsizei imageSize
, const void *data
)
1598 struct st_context
*st
= st_context(ctx
);
1599 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1600 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1601 struct pipe_resource
*texture
= stImage
->pt
;
1602 struct pipe_context
*pipe
= st
->pipe
;
1603 struct pipe_screen
*screen
= pipe
->screen
;
1604 struct pipe_resource
*dst
= stImage
->pt
;
1605 struct pipe_surface
*surface
= NULL
;
1606 struct compressed_pixelstore store
;
1607 struct st_pbo_addresses addr
;
1608 enum pipe_format copy_format
;
1610 intptr_t buf_offset
;
1611 bool success
= false;
1613 /* Check basic pre-conditions for PBO upload */
1614 if (!st
->prefer_blit_based_texture_transfer
) {
1618 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1621 if ((_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
1622 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
)) {
1623 /* ETC isn't supported and is represented by uncompressed formats. */
1631 if (!st
->pbo
.upload_enabled
||
1632 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1636 /* Choose the pipe format for the upload. */
1637 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1638 bw
= util_format_get_blockwidth(dst
->format
);
1639 bh
= util_format_get_blockheight(dst
->format
);
1641 switch (addr
.bytes_per_pixel
) {
1643 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1646 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1652 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0,
1653 PIPE_BIND_SAMPLER_VIEW
)) {
1657 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1658 dst
->nr_samples
, PIPE_BIND_RENDER_TARGET
)) {
1662 /* Interpret the pixelstore settings. */
1663 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1664 &ctx
->Unpack
, &store
);
1665 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1666 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1668 /* Compute the offset into the buffer */
1669 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1671 if (buf_offset
% addr
.bytes_per_pixel
) {
1675 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1677 addr
.xoffset
= x
/ bw
;
1678 addr
.yoffset
= y
/ bh
;
1679 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1680 addr
.height
= store
.CopyRowsPerSlice
;
1682 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1683 addr
.image_height
= store
.TotalRowsPerSlice
;
1685 if (!st_pbo_addresses_setup(st
, st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1689 /* Set up the surface. */
1691 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1692 unsigned max_layer
= util_max_layer(texture
, level
);
1694 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1696 struct pipe_surface templ
;
1697 memset(&templ
, 0, sizeof(templ
));
1698 templ
.format
= copy_format
;
1699 templ
.u
.tex
.level
= level
;
1700 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1701 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1703 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1708 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1710 pipe_surface_reference(&surface
, NULL
);
1716 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1718 format
, imageSize
, data
);
1722 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1723 struct gl_texture_image
*texImage
,
1724 GLsizei imageSize
, const void *data
)
1726 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1728 /* only 2D and 3D compressed images are supported at this time */
1730 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1734 /* This is pretty simple, because unlike the general texstore path we don't
1735 * have to worry about the usual image unpacking or image transfer
1739 assert(texImage
->Width
> 0);
1740 assert(texImage
->Height
> 0);
1741 assert(texImage
->Depth
> 0);
1743 /* allocate storage for texture data */
1744 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1745 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1749 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1751 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1752 texImage
->TexFormat
,
1760 * Called via ctx->Driver.GetTexSubImage()
1762 * This uses a blit to copy the texture to a texture format which matches
1763 * the format and type combo and then a fast read-back is done using memcpy.
1764 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1765 * a format which matches the swizzling.
1767 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1769 * NOTE: Drivers usually do a blit to convert between tiled and linear
1770 * texture layouts during texture uploads/downloads, so the blit
1771 * we do here should be free in such cases.
1774 st_GetTexSubImage(struct gl_context
* ctx
,
1775 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1776 GLsizei width
, GLsizei height
, GLint depth
,
1777 GLenum format
, GLenum type
, void * pixels
,
1778 struct gl_texture_image
*texImage
)
1780 struct st_context
*st
= st_context(ctx
);
1781 struct pipe_context
*pipe
= st
->pipe
;
1782 struct pipe_screen
*screen
= pipe
->screen
;
1783 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1784 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1785 struct pipe_resource
*src
= stObj
->pt
;
1786 struct pipe_resource
*dst
= NULL
;
1787 struct pipe_resource dst_templ
;
1788 enum pipe_format dst_format
, src_format
;
1789 mesa_format mesa_format
;
1790 GLenum gl_target
= texImage
->TexObject
->Target
;
1791 enum pipe_texture_target pipe_target
;
1792 struct pipe_blit_info blit
;
1793 unsigned bind
= PIPE_BIND_TRANSFER_READ
;
1794 struct pipe_transfer
*tex_xfer
;
1796 boolean done
= FALSE
;
1798 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1799 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1801 st_flush_bitmap_cache(st
);
1803 if (!st
->prefer_blit_based_texture_transfer
&&
1804 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1805 /* Try to avoid the fallback if we're doing texture decompression here */
1809 /* Handle non-finalized textures. */
1810 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1814 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1815 * due to an incomplete stencil blit implementation in some drivers. */
1816 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1820 /* If the base internal format and the texture format don't match, we have
1821 * to fall back to _mesa_GetTexImage_sw. */
1822 if (texImage
->_BaseFormat
!=
1823 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1827 /* See if the texture format already matches the format and type,
1828 * in which case the memcpy-based fast path will be used. */
1829 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1830 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1834 /* Convert the source format to what is expected by GetTexImage
1835 * and see if it's supported.
1837 * This only applies to glGetTexImage:
1838 * - Luminance must be returned as (L,0,0,1).
1839 * - Luminance alpha must be returned as (L,0,0,A).
1840 * - Intensity must be returned as (I,0,0,1)
1842 if (stObj
->surface_based
)
1843 src_format
= util_format_linear(stObj
->surface_format
);
1845 src_format
= util_format_linear(src
->format
);
1846 src_format
= util_format_luminance_to_red(src_format
);
1847 src_format
= util_format_intensity_to_red(src_format
);
1850 !screen
->is_format_supported(screen
, src_format
, src
->target
,
1852 PIPE_BIND_SAMPLER_VIEW
)) {
1856 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1857 bind
|= PIPE_BIND_DEPTH_STENCIL
;
1859 bind
|= PIPE_BIND_RENDER_TARGET
;
1861 /* GetTexImage only returns a single face for cubemaps. */
1862 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1863 gl_target
= GL_TEXTURE_2D
;
1865 pipe_target
= gl_target_to_pipe(gl_target
);
1867 /* Choose the destination format by finding the best match
1868 * for the format+type combo. */
1869 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1870 ctx
->Pack
.SwapBytes
);
1872 if (dst_format
== PIPE_FORMAT_NONE
) {
1873 GLenum dst_glformat
;
1875 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1876 * where decompression with a blit is always preferred. */
1877 if (!util_format_is_compressed(src
->format
)) {
1881 /* Set the appropriate format for the decompressed texture.
1882 * Luminance and sRGB formats shouldn't appear here.*/
1883 switch (src_format
) {
1884 case PIPE_FORMAT_DXT1_RGB
:
1885 case PIPE_FORMAT_DXT1_RGBA
:
1886 case PIPE_FORMAT_DXT3_RGBA
:
1887 case PIPE_FORMAT_DXT5_RGBA
:
1888 case PIPE_FORMAT_RGTC1_UNORM
:
1889 case PIPE_FORMAT_RGTC2_UNORM
:
1890 case PIPE_FORMAT_ETC1_RGB8
:
1891 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1892 dst_glformat
= GL_RGBA8
;
1894 case PIPE_FORMAT_RGTC1_SNORM
:
1895 case PIPE_FORMAT_RGTC2_SNORM
:
1896 if (!ctx
->Extensions
.EXT_texture_snorm
)
1898 dst_glformat
= GL_RGBA8_SNORM
;
1900 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1901 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1902 if (!ctx
->Extensions
.ARB_texture_float
)
1904 dst_glformat
= GL_RGBA32F
;
1911 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
1912 pipe_target
, 0, bind
, FALSE
);
1914 if (dst_format
== PIPE_FORMAT_NONE
) {
1915 /* unable to get an rgba format!?! */
1920 /* create the destination texture of size (width X height X depth) */
1921 memset(&dst_templ
, 0, sizeof(dst_templ
));
1922 dst_templ
.target
= pipe_target
;
1923 dst_templ
.format
= dst_format
;
1924 dst_templ
.bind
= bind
;
1925 dst_templ
.usage
= PIPE_USAGE_STAGING
;
1927 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1928 &dst_templ
.width0
, &dst_templ
.height0
,
1929 &dst_templ
.depth0
, &dst_templ
.array_size
);
1931 dst
= screen
->resource_create(screen
, &dst_templ
);
1936 /* From now on, we need the gallium representation of dimensions. */
1937 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1944 assert(texImage
->Face
== 0 ||
1945 texImage
->TexObject
->MinLayer
== 0 ||
1948 memset(&blit
, 0, sizeof(blit
));
1949 blit
.src
.resource
= src
;
1950 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
1951 blit
.src
.format
= src_format
;
1952 blit
.dst
.resource
= dst
;
1954 blit
.dst
.format
= dst
->format
;
1955 blit
.src
.box
.x
= xoffset
;
1957 blit
.src
.box
.y
= yoffset
;
1959 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
1961 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1962 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1963 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1964 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
1965 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1966 blit
.scissor_enable
= FALSE
;
1968 /* blit/render/decompress */
1969 st
->pipe
->blit(st
->pipe
, &blit
);
1971 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
1973 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
1974 0, 0, 0, width
, height
, depth
, &tex_xfer
);
1979 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
1981 /* copy/pack data into user buffer */
1982 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
1983 ctx
->Pack
.SwapBytes
, NULL
)) {
1985 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
1988 for (slice
= 0; slice
< depth
; slice
++) {
1989 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1990 /* 1D array textures.
1991 * We need to convert gallium coords to GL coords.
1993 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1994 width
, depth
, format
,
1996 memcpy(dest
, map
, bytesPerRow
);
1999 ubyte
*slice_map
= map
;
2001 for (row
= 0; row
< height
; row
++) {
2002 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2003 width
, height
, format
,
2004 type
, slice
, row
, 0);
2005 memcpy(dest
, slice_map
, bytesPerRow
);
2006 slice_map
+= tex_xfer
->stride
;
2009 map
+= tex_xfer
->layer_stride
;
2013 /* format translation via floats */
2016 uint32_t dstMesaFormat
;
2017 int dstStride
, srcStride
;
2019 assert(util_format_is_compressed(src
->format
));
2021 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
2026 if (ST_DEBUG
& DEBUG_FALLBACK
)
2027 debug_printf("%s: fallback format translation\n", __func__
);
2029 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2030 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2031 srcStride
= 4 * width
* sizeof(GLfloat
);
2032 for (slice
= 0; slice
< depth
; slice
++) {
2033 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2034 /* 1D array textures.
2035 * We need to convert gallium coords to GL coords.
2037 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2038 width
, depth
, format
,
2041 /* get float[4] rgba row from surface */
2042 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, 1,
2045 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2046 rgba
, RGBA32_FLOAT
, srcStride
,
2050 for (row
= 0; row
< height
; row
++) {
2051 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2052 width
, height
, format
,
2053 type
, slice
, row
, 0);
2055 /* get float[4] rgba row from surface */
2056 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, row
, width
, 1,
2059 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2060 rgba
, RGBA32_FLOAT
, srcStride
,
2064 map
+= tex_xfer
->layer_stride
;
2073 pipe_transfer_unmap(pipe
, tex_xfer
);
2075 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2076 pipe_resource_reference(&dst
, NULL
);
2080 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2081 width
, height
, depth
,
2082 format
, type
, pixels
, texImage
);
2088 * Do a CopyTexSubImage operation using a read transfer from the source,
2089 * a write transfer to the destination and get_tile()/put_tile() to access
2090 * the pixels/texels.
2092 * Note: srcY=0=TOP of renderbuffer
2095 fallback_copy_texsubimage(struct gl_context
*ctx
,
2096 struct st_renderbuffer
*strb
,
2097 struct st_texture_image
*stImage
,
2099 GLint destX
, GLint destY
, GLint slice
,
2100 GLint srcX
, GLint srcY
,
2101 GLsizei width
, GLsizei height
)
2103 struct st_context
*st
= st_context(ctx
);
2104 struct pipe_context
*pipe
= st
->pipe
;
2105 struct pipe_transfer
*src_trans
;
2107 enum pipe_transfer_usage transfer_usage
;
2109 unsigned dst_width
= width
;
2110 unsigned dst_height
= height
;
2111 unsigned dst_depth
= 1;
2112 struct pipe_transfer
*transfer
;
2114 if (ST_DEBUG
& DEBUG_FALLBACK
)
2115 debug_printf("%s: fallback processing\n", __func__
);
2117 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2118 srcY
= strb
->Base
.Height
- srcY
- height
;
2121 map
= pipe_transfer_map(pipe
,
2123 strb
->surface
->u
.tex
.level
,
2124 strb
->surface
->u
.tex
.first_layer
,
2127 width
, height
, &src_trans
);
2129 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2130 baseFormat
== GL_DEPTH_STENCIL
) &&
2131 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2132 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2134 transfer_usage
= PIPE_TRANSFER_WRITE
;
2136 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2137 destX
, destY
, slice
,
2138 dst_width
, dst_height
, dst_depth
,
2141 if (baseFormat
== GL_DEPTH_COMPONENT
||
2142 baseFormat
== GL_DEPTH_STENCIL
) {
2143 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2144 ctx
->Pixel
.DepthBias
!= 0.0F
);
2148 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2149 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2158 data
= malloc(width
* sizeof(uint
));
2161 /* To avoid a large temp memory allocation, do copy row by row */
2162 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2163 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2165 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2168 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2169 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2170 0, 0, width
, 1, data
);
2173 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2178 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2186 malloc(width
* height
* 4 * sizeof(GLfloat
));
2188 if (tempSrc
&& texDest
) {
2189 const GLint dims
= 2;
2191 struct gl_texture_image
*texImage
= &stImage
->base
;
2192 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2194 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2195 unpack
.Invert
= GL_TRUE
;
2198 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2199 dstRowStride
= transfer
->layer_stride
;
2202 dstRowStride
= transfer
->stride
;
2205 /* get float/RGBA image from framebuffer */
2206 /* XXX this usually involves a lot of int/float conversion.
2207 * try to avoid that someday.
2209 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2210 util_format_linear(strb
->texture
->format
),
2213 /* Store into texture memory.
2214 * Note that this does some special things such as pixel transfer
2215 * ops and format conversion. In particular, if the dest tex format
2216 * is actually RGBA but the user created the texture as GL_RGB we
2217 * need to fill-in/override the alpha channel with 1.0.
2219 _mesa_texstore(ctx
, dims
,
2220 texImage
->_BaseFormat
,
2221 texImage
->TexFormat
,
2225 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2229 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2235 st_texture_image_unmap(st
, stImage
, slice
);
2236 pipe
->transfer_unmap(pipe
, src_trans
);
2241 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2242 * Note that the region to copy has already been clipped so we know we
2243 * won't read from outside the source renderbuffer's bounds.
2245 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2248 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2249 struct gl_texture_image
*texImage
,
2250 GLint destX
, GLint destY
, GLint slice
,
2251 struct gl_renderbuffer
*rb
,
2252 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2254 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2255 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2256 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2257 struct st_context
*st
= st_context(ctx
);
2258 struct pipe_context
*pipe
= st
->pipe
;
2259 struct pipe_screen
*screen
= pipe
->screen
;
2260 struct pipe_blit_info blit
;
2261 enum pipe_format dst_format
;
2262 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2266 st_flush_bitmap_cache(st
);
2267 st_invalidate_readpix_cache(st
);
2269 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2270 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2272 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2273 debug_printf("%s: null strb or stImage\n", __func__
);
2277 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2278 texImage
->TexFormat
)) {
2282 /* The base internal format must match the mesa format, so make sure
2283 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
2285 if (texImage
->_BaseFormat
!=
2286 _mesa_get_format_base_format(texImage
->TexFormat
) ||
2287 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
2291 /* Choose the destination format to match the TexImage behavior. */
2292 dst_format
= util_format_linear(stImage
->pt
->format
);
2293 dst_format
= util_format_luminance_to_red(dst_format
);
2294 dst_format
= util_format_intensity_to_red(dst_format
);
2296 /* See if the destination format is supported. */
2297 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2298 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2299 bind
= PIPE_BIND_DEPTH_STENCIL
;
2302 bind
= PIPE_BIND_RENDER_TARGET
;
2306 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2307 stImage
->pt
->nr_samples
, bind
)) {
2311 /* Y flipping for the main framebuffer. */
2313 srcY1
= strb
->Base
.Height
- srcY
- height
;
2314 srcY0
= srcY1
+ height
;
2318 srcY1
= srcY0
+ height
;
2321 /* Blit the texture.
2322 * This supports flipping, format conversions, and downsampling.
2324 memset(&blit
, 0, sizeof(blit
));
2325 blit
.src
.resource
= strb
->texture
;
2326 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2327 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2328 blit
.src
.box
.x
= srcX
;
2329 blit
.src
.box
.y
= srcY0
;
2330 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2331 blit
.src
.box
.width
= width
;
2332 blit
.src
.box
.height
= srcY1
- srcY0
;
2333 blit
.src
.box
.depth
= 1;
2334 blit
.dst
.resource
= stImage
->pt
;
2335 blit
.dst
.format
= dst_format
;
2336 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2337 blit
.dst
.box
.x
= destX
;
2338 blit
.dst
.box
.y
= destY
;
2339 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2340 blit
.dst
.box
.width
= width
;
2341 blit
.dst
.box
.height
= height
;
2342 blit
.dst
.box
.depth
= 1;
2343 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2344 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2345 pipe
->blit(pipe
, &blit
);
2349 /* software fallback */
2350 fallback_copy_texsubimage(ctx
,
2351 strb
, stImage
, texImage
->_BaseFormat
,
2352 destX
, destY
, slice
,
2353 srcX
, srcY
, width
, height
);
2358 * Copy image data from stImage into the texture object 'stObj' at level
2362 copy_image_data_to_texture(struct st_context
*st
,
2363 struct st_texture_object
*stObj
,
2365 struct st_texture_image
*stImage
)
2369 const struct gl_texture_image
*dstImage
=
2370 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2372 assert(dstImage
->Width
== stImage
->base
.Width
);
2373 assert(dstImage
->Height
== stImage
->base
.Height
);
2374 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2378 /* Copy potentially with the blitter:
2381 if (stImage
->pt
->last_level
== 0)
2384 src_level
= stImage
->base
.Level
;
2386 assert(src_level
<= stImage
->pt
->last_level
);
2387 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2388 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2389 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2390 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2391 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2392 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2394 st_texture_image_copy(st
->pipe
,
2395 stObj
->pt
, dstLevel
, /* dest texture, level */
2396 stImage
->pt
, src_level
, /* src texture, level */
2397 stImage
->base
.Face
);
2399 pipe_resource_reference(&stImage
->pt
, NULL
);
2401 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2406 * Called during state validation. When this function is finished,
2407 * the texture object should be ready for rendering.
2408 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2411 st_finalize_texture(struct gl_context
*ctx
,
2412 struct pipe_context
*pipe
,
2413 struct gl_texture_object
*tObj
)
2415 struct st_context
*st
= st_context(ctx
);
2416 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2417 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2419 const struct st_texture_image
*firstImage
;
2420 enum pipe_format firstImageFormat
;
2421 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2423 if (tObj
->Immutable
)
2426 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
2427 /* The texture is complete and we know exactly how many mipmap levels
2428 * are present/needed. This is conditional because we may be called
2429 * from the st_generate_mipmap() function when the texture object is
2430 * incomplete. In that case, we'll have set stObj->lastLevel before
2433 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
2434 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
2435 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2437 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2440 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
2441 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
2444 pipe_resource_reference(&stObj
->pt
, NULL
);
2445 st_texture_release_all_sampler_views(st
, stObj
);
2449 if (st_obj
->buffer
!= stObj
->pt
) {
2450 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
2451 st_texture_release_all_sampler_views(st
, stObj
);
2457 firstImage
= st_texture_image_const(_mesa_base_tex_image(&stObj
->base
));
2460 /* If both firstImage and stObj point to a texture which can contain
2461 * all active images, favour firstImage. Note that because of the
2462 * completeness requirement, we know that the image dimensions
2465 if (firstImage
->pt
&&
2466 firstImage
->pt
!= stObj
->pt
&&
2467 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2468 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2469 st_texture_release_all_sampler_views(st
, stObj
);
2472 /* If this texture comes from a window system, there is nothing else to do. */
2473 if (stObj
->surface_based
) {
2477 /* Find gallium format for the Mesa texture */
2479 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2481 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2483 GLuint width
, height
, depth
;
2485 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2486 firstImage
->base
.Width2
,
2487 firstImage
->base
.Height2
,
2488 firstImage
->base
.Depth2
,
2489 &width
, &height
, &depth
, &ptLayers
);
2491 /* If we previously allocated a pipe texture and its sizes are
2492 * compatible, use them.
2495 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2496 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2497 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2498 ptWidth
= stObj
->pt
->width0
;
2499 ptHeight
= stObj
->pt
->height0
;
2500 ptDepth
= stObj
->pt
->depth0
;
2502 /* Otherwise, compute a new level=0 size that is compatible with the
2505 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2506 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2507 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2509 /* If the base level image is 1x1x1, we still need to ensure that the
2510 * resulting pipe texture ends up with the required number of levels
2513 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2514 ptWidth
<<= firstImage
->base
.Level
;
2516 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2517 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2522 ptNumSamples
= firstImage
->base
.NumSamples
;
2525 /* If we already have a gallium texture, check that it matches the texture
2526 * object's format, target, size, num_levels, etc.
2529 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2530 stObj
->pt
->format
!= firstImageFormat
||
2531 stObj
->pt
->last_level
< stObj
->lastLevel
||
2532 stObj
->pt
->width0
!= ptWidth
||
2533 stObj
->pt
->height0
!= ptHeight
||
2534 stObj
->pt
->depth0
!= ptDepth
||
2535 stObj
->pt
->nr_samples
!= ptNumSamples
||
2536 stObj
->pt
->array_size
!= ptLayers
)
2538 /* The gallium texture does not match the Mesa texture so delete the
2539 * gallium texture now. We'll make a new one below.
2541 pipe_resource_reference(&stObj
->pt
, NULL
);
2542 st_texture_release_all_sampler_views(st
, stObj
);
2543 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
2547 /* May need to create a new gallium texture:
2550 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2552 stObj
->pt
= st_texture_create(st
,
2553 gl_target_to_pipe(stObj
->base
.Target
),
2559 ptLayers
, ptNumSamples
,
2563 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2568 /* Pull in any images not in the object's texture:
2570 for (face
= 0; face
< nr_faces
; face
++) {
2572 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2573 struct st_texture_image
*stImage
=
2574 st_texture_image(stObj
->base
.Image
[face
][level
]);
2576 /* Need to import images in main memory or held in other textures.
2578 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2582 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2583 height
= u_minify(ptHeight
, level
);
2587 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2588 depth
= u_minify(ptDepth
, level
);
2589 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2595 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2596 stImage
->base
.Height
== height
&&
2597 stImage
->base
.Depth
== depth
)) {
2598 /* src image fits expected dest mipmap level size */
2599 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2610 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2611 * for a whole mipmap stack.
2614 st_AllocTextureStorage(struct gl_context
*ctx
,
2615 struct gl_texture_object
*texObj
,
2616 GLsizei levels
, GLsizei width
,
2617 GLsizei height
, GLsizei depth
)
2619 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2620 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2621 struct st_context
*st
= st_context(ctx
);
2622 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2623 struct pipe_screen
*screen
= st
->pipe
->screen
;
2624 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
2625 enum pipe_format fmt
;
2627 GLuint num_samples
= texImage
->NumSamples
;
2631 stObj
->lastLevel
= levels
- 1;
2633 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2635 bindings
= default_bindings(st
, fmt
);
2637 /* Raise the sample count if the requested one is unsupported. */
2638 if (num_samples
> 1) {
2639 boolean found
= FALSE
;
2641 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2642 if (screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
2644 PIPE_BIND_SAMPLER_VIEW
)) {
2645 /* Update the sample count in gl_texture_image as well. */
2646 texImage
->NumSamples
= num_samples
;
2657 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2658 width
, height
, depth
,
2659 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2661 stObj
->pt
= st_texture_create(st
,
2662 gl_target_to_pipe(texObj
->Target
),
2668 ptLayers
, num_samples
,
2673 /* Set image resource pointers */
2674 for (level
= 0; level
< levels
; level
++) {
2676 for (face
= 0; face
< numFaces
; face
++) {
2677 struct st_texture_image
*stImage
=
2678 st_texture_image(texObj
->Image
[face
][level
]);
2679 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2688 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2689 GLint level
, mesa_format format
,
2690 GLint width
, GLint height
,
2691 GLint depth
, GLint border
)
2693 struct st_context
*st
= st_context(ctx
);
2694 struct pipe_context
*pipe
= st
->pipe
;
2696 if (width
== 0 || height
== 0 || depth
== 0) {
2697 /* zero-sized images are legal, and always fit! */
2701 if (pipe
->screen
->can_create_resource
) {
2702 /* Ask the gallium driver if the texture is too large */
2703 struct gl_texture_object
*texObj
=
2704 _mesa_get_current_tex_object(ctx
, target
);
2705 struct pipe_resource pt
;
2707 /* Setup the pipe_resource object
2709 memset(&pt
, 0, sizeof(pt
));
2711 pt
.target
= gl_target_to_pipe(target
);
2712 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
2714 st_gl_texture_dims_to_pipe_dims(target
,
2715 width
, height
, depth
,
2716 &pt
.width0
, &pt
.height0
,
2717 &pt
.depth0
, &pt
.array_size
);
2719 if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
2720 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
2721 /* assume just one mipmap level */
2725 /* assume a full set of mipmaps */
2726 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
2729 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
2732 /* Use core Mesa fallback */
2733 return _mesa_test_proxy_teximage(ctx
, target
, level
, format
,
2734 width
, height
, depth
, border
);
2739 st_TextureView(struct gl_context
*ctx
,
2740 struct gl_texture_object
*texObj
,
2741 struct gl_texture_object
*origTexObj
)
2743 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
2744 struct st_texture_object
*tex
= st_texture_object(texObj
);
2745 struct gl_texture_image
*image
= texObj
->Image
[0][0];
2747 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2748 const int numLevels
= texObj
->NumLevels
;
2753 pipe_resource_reference(&tex
->pt
, orig
->pt
);
2755 /* Set image resource pointers */
2756 for (level
= 0; level
< numLevels
; level
++) {
2757 for (face
= 0; face
< numFaces
; face
++) {
2758 struct st_texture_image
*stImage
=
2759 st_texture_image(texObj
->Image
[face
][level
]);
2760 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
2764 tex
->surface_based
= GL_TRUE
;
2765 tex
->surface_format
=
2766 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
2768 tex
->lastLevel
= numLevels
- 1;
2774 st_ClearTexSubImage(struct gl_context
*ctx
,
2775 struct gl_texture_image
*texImage
,
2776 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2777 GLsizei width
, GLsizei height
, GLsizei depth
,
2778 const void *clearValue
)
2780 static const char zeros
[16] = {0};
2781 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2782 struct pipe_resource
*pt
= stImage
->pt
;
2783 struct st_context
*st
= st_context(ctx
);
2784 struct pipe_context
*pipe
= st
->pipe
;
2785 unsigned level
= texImage
->Level
;
2786 struct pipe_box box
;
2791 st_flush_bitmap_cache(st
);
2792 st_invalidate_readpix_cache(st
);
2794 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
2795 width
, height
, depth
, &box
);
2796 if (texImage
->TexObject
->Immutable
) {
2797 level
+= texImage
->TexObject
->MinLevel
;
2798 box
.z
+= texImage
->TexObject
->MinLayer
;
2801 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
2805 st_init_texture_functions(struct dd_function_table
*functions
)
2807 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
2808 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
2809 functions
->TexImage
= st_TexImage
;
2810 functions
->TexSubImage
= st_TexSubImage
;
2811 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
2812 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
2813 functions
->GenerateMipmap
= st_generate_mipmap
;
2815 functions
->GetTexSubImage
= st_GetTexSubImage
;
2817 /* compressed texture functions */
2818 functions
->CompressedTexImage
= st_CompressedTexImage
;
2819 functions
->GetCompressedTexSubImage
= _mesa_GetCompressedTexSubImage_sw
;
2821 functions
->NewTextureObject
= st_NewTextureObject
;
2822 functions
->NewTextureImage
= st_NewTextureImage
;
2823 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
2824 functions
->DeleteTexture
= st_DeleteTextureObject
;
2825 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
2826 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
2827 functions
->MapTextureImage
= st_MapTextureImage
;
2828 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
2830 /* XXX Temporary until we can query pipe's texture sizes */
2831 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
2833 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
2834 functions
->TextureView
= st_TextureView
;
2835 functions
->ClearTexSubImage
= st_ClearTexSubImage
;