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 guess_base_level_size(stObj
->base
.Target
,
480 &width
, &height
, &depth
)) {
481 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
482 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
483 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
488 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
489 stImage
->base
.Width2
,
490 stImage
->base
.Height2
,
491 stImage
->base
.Depth2
,
493 &width
, &height
, &depth
);
496 /* we can't determine the image size at level=0 */
497 /* this is not an out of memory error */
501 /* At this point, (width x height x depth) is the expected size of
502 * the level=0 mipmap image.
505 /* Guess a reasonable value for lastLevel. With OpenGL we have no
506 * idea how many mipmap levels will be in a texture until we start
507 * to render with it. Make an educated guess here but be prepared
508 * to re-allocating a texture buffer with space for more (or fewer)
509 * mipmap levels later.
511 if (allocate_full_mipmap(stObj
, stImage
)) {
512 /* alloc space for a full mipmap */
513 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
514 width
, height
, depth
) - 1;
517 /* only alloc space for a single mipmap level */
521 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
523 bindings
= default_bindings(st
, fmt
);
525 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
526 width
, height
, depth
,
527 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
529 stObj
->pt
= st_texture_create(st
,
530 gl_target_to_pipe(stObj
->base
.Target
),
539 stObj
->lastLevel
= lastLevel
;
541 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
543 return stObj
->pt
!= NULL
;
548 * Called via ctx->Driver.AllocTextureImageBuffer().
549 * If the texture object/buffer already has space for the indicated image,
550 * we're done. Otherwise, allocate memory for the new texture image.
553 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
554 struct gl_texture_image
*texImage
)
556 struct st_context
*st
= st_context(ctx
);
557 struct st_texture_image
*stImage
= st_texture_image(texImage
);
558 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
559 const GLuint level
= texImage
->Level
;
560 GLuint width
= texImage
->Width
;
561 GLuint height
= texImage
->Height
;
562 GLuint depth
= texImage
->Depth
;
564 DBG("%s\n", __func__
);
566 assert(!stImage
->pt
); /* xxx this might be wrong */
568 /* Look if the parent texture object has space for this image */
570 level
<= stObj
->pt
->last_level
&&
571 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
572 /* this image will fit in the existing texture object's memory */
573 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
577 /* The parent texture object does not have space for this image */
579 pipe_resource_reference(&stObj
->pt
, NULL
);
580 st_texture_release_all_sampler_views(st
, stObj
);
582 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
583 /* Probably out of memory.
584 * Try flushing any pending rendering, then retry.
587 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
588 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
594 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
595 /* The image will live in the object's mipmap memory */
596 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
601 /* Create a new, temporary texture/resource/buffer to hold this
602 * one texture image. Note that when we later access this image
603 * (either for mapping or copying) we'll want to always specify
604 * mipmap level=0, even if the image represents some other mipmap
607 enum pipe_format format
=
608 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
609 GLuint bindings
= default_bindings(st
, format
);
610 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
612 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
613 width
, height
, depth
,
614 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
616 stImage
->pt
= st_texture_create(st
,
617 gl_target_to_pipe(stObj
->base
.Target
),
625 return stImage
->pt
!= NULL
;
631 * Preparation prior to glTexImage. Basically check the 'surface_based'
632 * field and switch to a "normal" tex image if necessary.
635 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
636 GLenum format
, GLenum type
)
638 struct gl_texture_object
*texObj
= texImage
->TexObject
;
639 struct st_texture_object
*stObj
= st_texture_object(texObj
);
641 /* switch to "normal" */
642 if (stObj
->surface_based
) {
643 const GLenum target
= texObj
->Target
;
644 const GLuint level
= texImage
->Level
;
645 mesa_format texFormat
;
647 _mesa_clear_texture_object(ctx
, texObj
);
648 pipe_resource_reference(&stObj
->pt
, NULL
);
650 /* oops, need to init this image again */
651 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
652 texImage
->InternalFormat
, format
,
655 _mesa_init_teximage_fields(ctx
, texImage
,
656 texImage
->Width
, texImage
->Height
,
657 texImage
->Depth
, texImage
->Border
,
658 texImage
->InternalFormat
, texFormat
);
660 stObj
->surface_based
= GL_FALSE
;
666 * Return a writemask for the gallium blit. The parameters can be base
667 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
670 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
673 case GL_DEPTH_STENCIL
:
675 case GL_DEPTH_STENCIL
:
677 case GL_DEPTH_COMPONENT
:
679 case GL_STENCIL_INDEX
:
686 case GL_DEPTH_COMPONENT
:
688 case GL_DEPTH_STENCIL
:
689 case GL_DEPTH_COMPONENT
:
696 case GL_STENCIL_INDEX
:
698 case GL_STENCIL_INDEX
:
706 return PIPE_MASK_RGBA
;
711 * Converts format to a format with the same components, types
712 * and sizes, but with the components in RGBA order.
714 static enum pipe_format
715 unswizzle_format(enum pipe_format format
)
719 case PIPE_FORMAT_B8G8R8A8_UNORM
:
720 case PIPE_FORMAT_A8R8G8B8_UNORM
:
721 case PIPE_FORMAT_A8B8G8R8_UNORM
:
722 return PIPE_FORMAT_R8G8B8A8_UNORM
;
724 case PIPE_FORMAT_B10G10R10A2_UNORM
:
725 return PIPE_FORMAT_R10G10B10A2_UNORM
;
727 case PIPE_FORMAT_B10G10R10A2_SNORM
:
728 return PIPE_FORMAT_R10G10B10A2_SNORM
;
730 case PIPE_FORMAT_B10G10R10A2_UINT
:
731 return PIPE_FORMAT_R10G10B10A2_UINT
;
739 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
741 static enum pipe_format
742 alpha_to_red(enum pipe_format format
)
746 case PIPE_FORMAT_A8_UNORM
:
747 return PIPE_FORMAT_R8_UNORM
;
748 case PIPE_FORMAT_A8_SNORM
:
749 return PIPE_FORMAT_R8_SNORM
;
750 case PIPE_FORMAT_A8_UINT
:
751 return PIPE_FORMAT_R8_UINT
;
752 case PIPE_FORMAT_A8_SINT
:
753 return PIPE_FORMAT_R8_SINT
;
755 case PIPE_FORMAT_A16_UNORM
:
756 return PIPE_FORMAT_R16_UNORM
;
757 case PIPE_FORMAT_A16_SNORM
:
758 return PIPE_FORMAT_R16_SNORM
;
759 case PIPE_FORMAT_A16_UINT
:
760 return PIPE_FORMAT_R16_UINT
;
761 case PIPE_FORMAT_A16_SINT
:
762 return PIPE_FORMAT_R16_SINT
;
763 case PIPE_FORMAT_A16_FLOAT
:
764 return PIPE_FORMAT_R16_FLOAT
;
766 case PIPE_FORMAT_A32_UINT
:
767 return PIPE_FORMAT_R32_UINT
;
768 case PIPE_FORMAT_A32_SINT
:
769 return PIPE_FORMAT_R32_SINT
;
770 case PIPE_FORMAT_A32_FLOAT
:
771 return PIPE_FORMAT_R32_FLOAT
;
779 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
781 static enum pipe_format
782 red_alpha_to_red_green(enum pipe_format format
)
786 case PIPE_FORMAT_R8A8_UNORM
:
787 return PIPE_FORMAT_R8G8_UNORM
;
788 case PIPE_FORMAT_R8A8_SNORM
:
789 return PIPE_FORMAT_R8G8_SNORM
;
790 case PIPE_FORMAT_R8A8_UINT
:
791 return PIPE_FORMAT_R8G8_UINT
;
792 case PIPE_FORMAT_R8A8_SINT
:
793 return PIPE_FORMAT_R8G8_SINT
;
795 case PIPE_FORMAT_R16A16_UNORM
:
796 return PIPE_FORMAT_R16G16_UNORM
;
797 case PIPE_FORMAT_R16A16_SNORM
:
798 return PIPE_FORMAT_R16G16_SNORM
;
799 case PIPE_FORMAT_R16A16_UINT
:
800 return PIPE_FORMAT_R16G16_UINT
;
801 case PIPE_FORMAT_R16A16_SINT
:
802 return PIPE_FORMAT_R16G16_SINT
;
803 case PIPE_FORMAT_R16A16_FLOAT
:
804 return PIPE_FORMAT_R16G16_FLOAT
;
806 case PIPE_FORMAT_R32A32_UINT
:
807 return PIPE_FORMAT_R32G32_UINT
;
808 case PIPE_FORMAT_R32A32_SINT
:
809 return PIPE_FORMAT_R32G32_SINT
;
810 case PIPE_FORMAT_R32A32_FLOAT
:
811 return PIPE_FORMAT_R32G32_FLOAT
;
819 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
821 static enum pipe_format
822 luminance_alpha_to_red_green(enum pipe_format format
)
826 case PIPE_FORMAT_L8A8_UNORM
:
827 return PIPE_FORMAT_R8G8_UNORM
;
828 case PIPE_FORMAT_L8A8_SNORM
:
829 return PIPE_FORMAT_R8G8_SNORM
;
830 case PIPE_FORMAT_L8A8_UINT
:
831 return PIPE_FORMAT_R8G8_UINT
;
832 case PIPE_FORMAT_L8A8_SINT
:
833 return PIPE_FORMAT_R8G8_SINT
;
835 case PIPE_FORMAT_L16A16_UNORM
:
836 return PIPE_FORMAT_R16G16_UNORM
;
837 case PIPE_FORMAT_L16A16_SNORM
:
838 return PIPE_FORMAT_R16G16_SNORM
;
839 case PIPE_FORMAT_L16A16_UINT
:
840 return PIPE_FORMAT_R16G16_UINT
;
841 case PIPE_FORMAT_L16A16_SINT
:
842 return PIPE_FORMAT_R16G16_SINT
;
843 case PIPE_FORMAT_L16A16_FLOAT
:
844 return PIPE_FORMAT_R16G16_FLOAT
;
846 case PIPE_FORMAT_L32A32_UINT
:
847 return PIPE_FORMAT_R32G32_UINT
;
848 case PIPE_FORMAT_L32A32_SINT
:
849 return PIPE_FORMAT_R32G32_SINT
;
850 case PIPE_FORMAT_L32A32_FLOAT
:
851 return PIPE_FORMAT_R32G32_FLOAT
;
859 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
862 format_is_alpha(enum pipe_format format
)
864 const struct util_format_description
*desc
= util_format_description(format
);
866 if (desc
->nr_channels
== 1 &&
867 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
868 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
869 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
870 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
877 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
880 format_is_red(enum pipe_format format
)
882 const struct util_format_description
*desc
= util_format_description(format
);
884 if (desc
->nr_channels
== 1 &&
885 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
886 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
887 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
888 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
896 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
899 format_is_luminance(enum pipe_format format
)
901 const struct util_format_description
*desc
= util_format_description(format
);
903 if (desc
->nr_channels
== 1 &&
904 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
905 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
906 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
907 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
914 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
917 format_is_red_alpha(enum pipe_format format
)
919 const struct util_format_description
*desc
= util_format_description(format
);
921 if (desc
->nr_channels
== 2 &&
922 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
923 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
924 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
925 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
932 format_is_swizzled_rgba(enum pipe_format format
)
934 const struct util_format_description
*desc
= util_format_description(format
);
936 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
937 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
938 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
939 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
947 unsigned char swizzle
[4];
948 enum pipe_format format
;
951 static const struct format_table table_8888_unorm
[] = {
952 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
953 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
954 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
955 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
958 static const struct format_table table_1010102_unorm
[] = {
959 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
960 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
963 static const struct format_table table_1010102_snorm
[] = {
964 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
965 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
968 static const struct format_table table_1010102_uint
[] = {
969 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
970 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
973 static enum pipe_format
974 swizzle_format(enum pipe_format format
, const int * const swizzle
)
979 case PIPE_FORMAT_R8G8B8A8_UNORM
:
980 case PIPE_FORMAT_B8G8R8A8_UNORM
:
981 case PIPE_FORMAT_A8R8G8B8_UNORM
:
982 case PIPE_FORMAT_A8B8G8R8_UNORM
:
983 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
984 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
985 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
986 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
987 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
988 return table_8888_unorm
[i
].format
;
992 case PIPE_FORMAT_R10G10B10A2_UNORM
:
993 case PIPE_FORMAT_B10G10R10A2_UNORM
:
994 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
995 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
996 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
997 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
998 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
999 return table_1010102_unorm
[i
].format
;
1003 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1004 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1005 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1006 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1007 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1008 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1009 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1010 return table_1010102_snorm
[i
].format
;
1014 case PIPE_FORMAT_R10G10B10A2_UINT
:
1015 case PIPE_FORMAT_B10G10R10A2_UINT
:
1016 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1017 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1018 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1019 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1020 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1021 return table_1010102_uint
[i
].format
;
1029 return PIPE_FORMAT_NONE
;
1033 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1035 enum pipe_format src
= *src_format
;
1036 enum pipe_format dst
= *dst_format
;
1038 /* Note: dst_format has already been transformed from luminance/intensity
1039 * to red when this function is called. The source format will never
1040 * be an intensity format, because GL_INTENSITY is not a legal value
1041 * for the format parameter in glTex(Sub)Image(). */
1043 if (format_is_alpha(src
)) {
1044 if (!format_is_alpha(dst
))
1047 src
= alpha_to_red(src
);
1048 dst
= alpha_to_red(dst
);
1049 } else if (format_is_luminance(src
)) {
1050 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1053 src
= util_format_luminance_to_red(src
);
1054 } else if (util_format_is_luminance_alpha(src
)) {
1055 src
= luminance_alpha_to_red_green(src
);
1057 if (format_is_red_alpha(dst
)) {
1058 dst
= red_alpha_to_red_green(dst
);
1059 } else if (!format_is_red(dst
))
1061 } else if (format_is_swizzled_rgba(src
)) {
1062 const struct util_format_description
*src_desc
= util_format_description(src
);
1063 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1067 /* Make sure the format is an RGBA and not an RGBX format */
1068 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1071 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1074 for (i
= 0; i
< 4; i
++)
1075 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1077 dst
= swizzle_format(dst
, swizzle
);
1078 if (dst
== PIPE_FORMAT_NONE
)
1081 src
= unswizzle_format(src
);
1090 try_pbo_upload_common(struct gl_context
*ctx
,
1091 struct pipe_surface
*surface
,
1092 const struct st_pbo_addresses
*addr
,
1093 enum pipe_format src_format
)
1095 struct st_context
*st
= st_context(ctx
);
1096 struct cso_context
*cso
= st
->cso_context
;
1097 struct pipe_context
*pipe
= st
->pipe
;
1098 bool success
= false;
1100 /* Create fragment shader */
1101 if (!st
->pbo
.upload_fs
) {
1102 st
->pbo
.upload_fs
= st_pbo_create_upload_fs(st
);
1103 if (!st
->pbo
.upload_fs
)
1107 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1108 CSO_BIT_FRAGMENT_SAMPLERS
|
1109 CSO_BIT_VERTEX_ELEMENTS
|
1110 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1111 CSO_BIT_FRAMEBUFFER
|
1114 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1115 CSO_BIT_RASTERIZER
|
1116 CSO_BIT_STREAM_OUTPUTS
|
1117 CSO_BIT_PAUSE_QUERIES
|
1118 CSO_BITS_ALL_SHADERS
));
1119 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1122 /* Set up the sampler_view */
1124 struct pipe_sampler_view templ
;
1125 struct pipe_sampler_view
*sampler_view
;
1126 struct pipe_sampler_state sampler
= {0};
1127 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
1129 memset(&templ
, 0, sizeof(templ
));
1130 templ
.target
= PIPE_BUFFER
;
1131 templ
.format
= src_format
;
1132 templ
.u
.buf
.first_element
= addr
->first_element
;
1133 templ
.u
.buf
.last_element
= addr
->last_element
;
1134 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1135 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1136 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1137 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1139 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1140 if (sampler_view
== NULL
)
1143 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1145 pipe_sampler_view_reference(&sampler_view
, NULL
);
1147 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
1150 /* Framebuffer_state */
1152 struct pipe_framebuffer_state fb
;
1153 memset(&fb
, 0, sizeof(fb
));
1154 fb
.width
= surface
->width
;
1155 fb
.height
= surface
->height
;
1157 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1159 cso_set_framebuffer(cso
, &fb
);
1161 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1164 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1167 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1169 /* Depth/stencil/alpha state */
1171 struct pipe_depth_stencil_alpha_state dsa
;
1172 memset(&dsa
, 0, sizeof(dsa
));
1173 cso_set_depth_stencil_alpha(cso
, &dsa
);
1176 /* Set up the fragment shader */
1177 cso_set_fragment_shader_handle(cso
, st
->pbo
.upload_fs
);
1179 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1182 cso_restore_state(cso
);
1183 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1189 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1190 struct gl_texture_image
*texImage
,
1191 GLenum format
, GLenum type
,
1192 enum pipe_format dst_format
,
1193 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1194 GLint width
, GLint height
, GLint depth
,
1196 const struct gl_pixelstore_attrib
*unpack
)
1198 struct st_context
*st
= st_context(ctx
);
1199 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1200 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1201 struct pipe_resource
*texture
= stImage
->pt
;
1202 struct pipe_context
*pipe
= st
->pipe
;
1203 struct pipe_screen
*screen
= pipe
->screen
;
1204 struct pipe_surface
*surface
= NULL
;
1205 struct st_pbo_addresses addr
;
1206 enum pipe_format src_format
;
1207 const struct util_format_description
*desc
;
1208 GLenum gl_target
= texImage
->TexObject
->Target
;
1211 if (!st
->pbo
.upload_enabled
)
1214 /* From now on, we need the gallium representation of dimensions. */
1215 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1222 if (depth
!= 1 && !st
->pbo
.layers
)
1225 /* Choose the source format. Initially, we do so without checking driver
1226 * support at all because of the remapping we later perform and because
1227 * at least the Radeon driver actually supports some formats for texture
1228 * buffers which it doesn't support for regular textures. */
1229 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1234 src_format
= util_format_linear(src_format
);
1235 desc
= util_format_description(src_format
);
1237 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1240 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1243 if (st
->pbo
.rgba_only
) {
1244 enum pipe_format orig_dst_format
= dst_format
;
1246 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1250 if (dst_format
!= orig_dst_format
&&
1251 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1252 PIPE_BIND_RENDER_TARGET
)) {
1258 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0,
1259 PIPE_BIND_SAMPLER_VIEW
)) {
1263 /* Compute buffer addresses */
1264 addr
.xoffset
= xoffset
;
1265 addr
.yoffset
= yoffset
;
1267 addr
.height
= height
;
1269 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1271 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1275 /* Set up the surface */
1277 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1278 unsigned max_layer
= util_max_layer(texture
, level
);
1280 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1282 struct pipe_surface templ
;
1283 memset(&templ
, 0, sizeof(templ
));
1284 templ
.format
= dst_format
;
1285 templ
.u
.tex
.level
= level
;
1286 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1287 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1289 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1294 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1296 pipe_surface_reference(&surface
, NULL
);
1302 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1303 struct gl_texture_image
*texImage
,
1304 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1305 GLint width
, GLint height
, GLint depth
,
1306 GLenum format
, GLenum type
, const void *pixels
,
1307 const struct gl_pixelstore_attrib
*unpack
)
1309 struct st_context
*st
= st_context(ctx
);
1310 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1311 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1312 struct pipe_context
*pipe
= st
->pipe
;
1313 struct pipe_screen
*screen
= pipe
->screen
;
1314 struct pipe_resource
*dst
= stImage
->pt
;
1315 struct pipe_resource
*src
= NULL
;
1316 struct pipe_resource src_templ
;
1317 struct pipe_transfer
*transfer
;
1318 struct pipe_blit_info blit
;
1319 enum pipe_format src_format
, dst_format
;
1320 mesa_format mesa_src_format
;
1321 GLenum gl_target
= texImage
->TexObject
->Target
;
1324 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1325 unsigned dst_level
= 0;
1327 st_flush_bitmap_cache(st
);
1329 if (stObj
->pt
== stImage
->pt
)
1330 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1332 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1333 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1338 /* Try transfer_inline_write, which should be the fastest memcpy path. */
1340 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1341 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1342 texImage
->TexFormat
, format
, type
,
1344 struct pipe_box box
;
1345 unsigned stride
, layer_stride
;
1348 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1349 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1351 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1354 /* Convert to Gallium coordinates. */
1355 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1360 layer_stride
= stride
;
1363 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1364 pipe
->transfer_inline_write(pipe
, dst
, dst_level
, 0,
1365 &box
, data
, stride
, layer_stride
);
1369 if (!st
->prefer_blit_based_texture_transfer
) {
1373 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1374 * blit implementation in some drivers. */
1375 if (format
== GL_DEPTH_STENCIL
) {
1379 /* If the base internal format and the texture format don't match,
1380 * we can't use blit-based TexSubImage. */
1381 if (texImage
->_BaseFormat
!=
1382 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1387 /* See if the destination format is supported. */
1388 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1389 bind
= PIPE_BIND_DEPTH_STENCIL
;
1391 bind
= PIPE_BIND_RENDER_TARGET
;
1393 /* For luminance and intensity, only the red channel is stored
1394 * in the destination. */
1395 dst_format
= util_format_linear(dst
->format
);
1396 dst_format
= util_format_luminance_to_red(dst_format
);
1397 dst_format
= util_format_intensity_to_red(dst_format
);
1400 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1401 dst
->nr_samples
, bind
)) {
1405 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1406 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1407 xoffset
, yoffset
, zoffset
,
1408 width
, height
, depth
, pixels
, unpack
))
1412 /* See if the texture format already matches the format and type,
1413 * in which case the memcpy-based fast path will likely be used and
1414 * we don't have to blit. */
1415 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1416 type
, unpack
->SwapBytes
, NULL
)) {
1420 /* Choose the source format. */
1421 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1422 format
, type
, unpack
->SwapBytes
);
1427 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1429 /* There is no reason to do this if we cannot use memcpy for the temporary
1430 * source texture at least. This also takes transfer ops into account,
1432 if (!_mesa_texstore_can_use_memcpy(ctx
,
1433 _mesa_get_format_base_format(mesa_src_format
),
1434 mesa_src_format
, format
, type
, unpack
)) {
1438 /* TexSubImage only sets a single cubemap face. */
1439 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1440 gl_target
= GL_TEXTURE_2D
;
1442 /* TexSubImage can specify subsets of cube map array faces
1443 * so we need to upload via 2D array instead */
1444 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1445 gl_target
= GL_TEXTURE_2D_ARRAY
;
1448 /* Initialize the source texture description. */
1449 memset(&src_templ
, 0, sizeof(src_templ
));
1450 src_templ
.target
= gl_target_to_pipe(gl_target
);
1451 src_templ
.format
= src_format
;
1452 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1453 src_templ
.usage
= PIPE_USAGE_STAGING
;
1455 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1456 &src_templ
.width0
, &src_templ
.height0
,
1457 &src_templ
.depth0
, &src_templ
.array_size
);
1459 /* Check for NPOT texture support. */
1460 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1461 (!util_is_power_of_two(src_templ
.width0
) ||
1462 !util_is_power_of_two(src_templ
.height0
) ||
1463 !util_is_power_of_two(src_templ
.depth0
))) {
1467 /* Create the source texture. */
1468 src
= screen
->resource_create(screen
, &src_templ
);
1473 /* Map source pixels. */
1474 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1475 format
, type
, pixels
, unpack
,
1478 /* This is a GL error. */
1479 pipe_resource_reference(&src
, NULL
);
1483 /* From now on, we need the gallium representation of dimensions. */
1484 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1491 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1492 width
, height
, depth
, &transfer
);
1494 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1495 pipe_resource_reference(&src
, NULL
);
1499 /* Upload pixels (just memcpy). */
1501 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1504 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1505 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1506 /* 1D array textures.
1507 * We need to convert gallium coords to GL coords.
1509 void *src
= _mesa_image_address2d(unpack
, pixels
,
1510 width
, depth
, format
,
1512 memcpy(map
, src
, bytesPerRow
);
1515 ubyte
*slice_map
= map
;
1517 for (row
= 0; row
< (unsigned) height
; row
++) {
1518 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1519 width
, height
, format
,
1520 type
, slice
, row
, 0);
1521 memcpy(slice_map
, src
, bytesPerRow
);
1522 slice_map
+= transfer
->stride
;
1525 map
+= transfer
->layer_stride
;
1529 pipe_transfer_unmap(pipe
, transfer
);
1530 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1533 memset(&blit
, 0, sizeof(blit
));
1534 blit
.src
.resource
= src
;
1536 blit
.src
.format
= src_format
;
1537 blit
.dst
.resource
= dst
;
1538 blit
.dst
.level
= dst_level
;
1539 blit
.dst
.format
= dst_format
;
1540 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1541 blit
.dst
.box
.x
= xoffset
;
1542 blit
.dst
.box
.y
= yoffset
;
1543 blit
.dst
.box
.z
= zoffset
+ dstz
;
1544 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1545 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1546 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1547 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1548 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1549 blit
.scissor_enable
= FALSE
;
1551 st
->pipe
->blit(st
->pipe
, &blit
);
1553 pipe_resource_reference(&src
, NULL
);
1557 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1558 width
, height
, depth
, format
, type
, pixels
,
1563 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1564 struct gl_texture_image
*texImage
,
1565 GLenum format
, GLenum type
, const void *pixels
,
1566 const struct gl_pixelstore_attrib
*unpack
)
1568 assert(dims
== 1 || dims
== 2 || dims
== 3);
1570 prep_teximage(ctx
, texImage
, format
, type
);
1572 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1575 /* allocate storage for texture data */
1576 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1577 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1581 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1582 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1583 format
, type
, pixels
, unpack
);
1588 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1589 struct gl_texture_image
*texImage
,
1590 GLint x
, GLint y
, GLint z
,
1591 GLsizei w
, GLsizei h
, GLsizei d
,
1592 GLenum format
, GLsizei imageSize
, const void *data
)
1594 struct st_context
*st
= st_context(ctx
);
1595 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1596 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1597 struct pipe_resource
*texture
= stImage
->pt
;
1598 struct pipe_context
*pipe
= st
->pipe
;
1599 struct pipe_screen
*screen
= pipe
->screen
;
1600 struct pipe_resource
*dst
= stImage
->pt
;
1601 struct pipe_surface
*surface
= NULL
;
1602 struct compressed_pixelstore store
;
1603 struct st_pbo_addresses addr
;
1604 enum pipe_format copy_format
;
1606 intptr_t buf_offset
;
1607 bool success
= false;
1609 /* Check basic pre-conditions for PBO upload */
1610 if (!st
->prefer_blit_based_texture_transfer
) {
1614 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1617 if ((_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
1618 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
)) {
1619 /* ETC isn't supported and is represented by uncompressed formats. */
1627 if (!st
->pbo
.upload_enabled
||
1628 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1632 /* Choose the pipe format for the upload. */
1633 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1634 bw
= util_format_get_blockwidth(dst
->format
);
1635 bh
= util_format_get_blockheight(dst
->format
);
1637 switch (addr
.bytes_per_pixel
) {
1639 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1642 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1648 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0,
1649 PIPE_BIND_SAMPLER_VIEW
)) {
1653 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1654 dst
->nr_samples
, PIPE_BIND_RENDER_TARGET
)) {
1658 /* Interpret the pixelstore settings. */
1659 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1660 &ctx
->Unpack
, &store
);
1661 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1662 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1664 /* Compute the offset into the buffer */
1665 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1667 if (buf_offset
% addr
.bytes_per_pixel
) {
1671 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1673 addr
.xoffset
= x
/ bw
;
1674 addr
.yoffset
= y
/ bh
;
1675 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1676 addr
.height
= store
.CopyRowsPerSlice
;
1678 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1679 addr
.image_height
= store
.TotalRowsPerSlice
;
1681 if (!st_pbo_addresses_setup(st
, st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1685 /* Set up the surface. */
1687 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1688 unsigned max_layer
= util_max_layer(texture
, level
);
1690 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1692 struct pipe_surface templ
;
1693 memset(&templ
, 0, sizeof(templ
));
1694 templ
.format
= copy_format
;
1695 templ
.u
.tex
.level
= level
;
1696 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1697 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1699 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1704 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1706 pipe_surface_reference(&surface
, NULL
);
1712 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1714 format
, imageSize
, data
);
1718 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1719 struct gl_texture_image
*texImage
,
1720 GLsizei imageSize
, const void *data
)
1722 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1724 /* only 2D and 3D compressed images are supported at this time */
1726 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1730 /* This is pretty simple, because unlike the general texstore path we don't
1731 * have to worry about the usual image unpacking or image transfer
1735 assert(texImage
->Width
> 0);
1736 assert(texImage
->Height
> 0);
1737 assert(texImage
->Depth
> 0);
1739 /* allocate storage for texture data */
1740 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1741 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1745 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1747 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1748 texImage
->TexFormat
,
1756 * Called via ctx->Driver.GetTexSubImage()
1758 * This uses a blit to copy the texture to a texture format which matches
1759 * the format and type combo and then a fast read-back is done using memcpy.
1760 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1761 * a format which matches the swizzling.
1763 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1765 * NOTE: Drivers usually do a blit to convert between tiled and linear
1766 * texture layouts during texture uploads/downloads, so the blit
1767 * we do here should be free in such cases.
1770 st_GetTexSubImage(struct gl_context
* ctx
,
1771 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1772 GLsizei width
, GLsizei height
, GLint depth
,
1773 GLenum format
, GLenum type
, void * pixels
,
1774 struct gl_texture_image
*texImage
)
1776 struct st_context
*st
= st_context(ctx
);
1777 struct pipe_context
*pipe
= st
->pipe
;
1778 struct pipe_screen
*screen
= pipe
->screen
;
1779 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1780 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1781 struct pipe_resource
*src
= stObj
->pt
;
1782 struct pipe_resource
*dst
= NULL
;
1783 struct pipe_resource dst_templ
;
1784 enum pipe_format dst_format
, src_format
;
1785 mesa_format mesa_format
;
1786 GLenum gl_target
= texImage
->TexObject
->Target
;
1787 enum pipe_texture_target pipe_target
;
1788 struct pipe_blit_info blit
;
1789 unsigned bind
= PIPE_BIND_TRANSFER_READ
;
1790 struct pipe_transfer
*tex_xfer
;
1792 boolean done
= FALSE
;
1794 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1795 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1797 st_flush_bitmap_cache(st
);
1799 if (!st
->prefer_blit_based_texture_transfer
&&
1800 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1801 /* Try to avoid the fallback if we're doing texture decompression here */
1805 /* Handle non-finalized textures. */
1806 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1810 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1811 * due to an incomplete stencil blit implementation in some drivers. */
1812 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1816 /* If the base internal format and the texture format don't match, we have
1817 * to fall back to _mesa_GetTexImage_sw. */
1818 if (texImage
->_BaseFormat
!=
1819 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1823 /* See if the texture format already matches the format and type,
1824 * in which case the memcpy-based fast path will be used. */
1825 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1826 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1830 /* Convert the source format to what is expected by GetTexImage
1831 * and see if it's supported.
1833 * This only applies to glGetTexImage:
1834 * - Luminance must be returned as (L,0,0,1).
1835 * - Luminance alpha must be returned as (L,0,0,A).
1836 * - Intensity must be returned as (I,0,0,1)
1838 if (stObj
->surface_based
)
1839 src_format
= util_format_linear(stObj
->surface_format
);
1841 src_format
= util_format_linear(src
->format
);
1842 src_format
= util_format_luminance_to_red(src_format
);
1843 src_format
= util_format_intensity_to_red(src_format
);
1846 !screen
->is_format_supported(screen
, src_format
, src
->target
,
1848 PIPE_BIND_SAMPLER_VIEW
)) {
1852 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1853 bind
|= PIPE_BIND_DEPTH_STENCIL
;
1855 bind
|= PIPE_BIND_RENDER_TARGET
;
1857 /* GetTexImage only returns a single face for cubemaps. */
1858 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1859 gl_target
= GL_TEXTURE_2D
;
1861 pipe_target
= gl_target_to_pipe(gl_target
);
1863 /* Choose the destination format by finding the best match
1864 * for the format+type combo. */
1865 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1866 ctx
->Pack
.SwapBytes
);
1868 if (dst_format
== PIPE_FORMAT_NONE
) {
1869 GLenum dst_glformat
;
1871 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1872 * where decompression with a blit is always preferred. */
1873 if (!util_format_is_compressed(src
->format
)) {
1877 /* Set the appropriate format for the decompressed texture.
1878 * Luminance and sRGB formats shouldn't appear here.*/
1879 switch (src_format
) {
1880 case PIPE_FORMAT_DXT1_RGB
:
1881 case PIPE_FORMAT_DXT1_RGBA
:
1882 case PIPE_FORMAT_DXT3_RGBA
:
1883 case PIPE_FORMAT_DXT5_RGBA
:
1884 case PIPE_FORMAT_RGTC1_UNORM
:
1885 case PIPE_FORMAT_RGTC2_UNORM
:
1886 case PIPE_FORMAT_ETC1_RGB8
:
1887 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1888 dst_glformat
= GL_RGBA8
;
1890 case PIPE_FORMAT_RGTC1_SNORM
:
1891 case PIPE_FORMAT_RGTC2_SNORM
:
1892 if (!ctx
->Extensions
.EXT_texture_snorm
)
1894 dst_glformat
= GL_RGBA8_SNORM
;
1896 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1897 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1898 if (!ctx
->Extensions
.ARB_texture_float
)
1900 dst_glformat
= GL_RGBA32F
;
1907 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
1908 pipe_target
, 0, bind
, FALSE
);
1910 if (dst_format
== PIPE_FORMAT_NONE
) {
1911 /* unable to get an rgba format!?! */
1916 /* create the destination texture of size (width X height X depth) */
1917 memset(&dst_templ
, 0, sizeof(dst_templ
));
1918 dst_templ
.target
= pipe_target
;
1919 dst_templ
.format
= dst_format
;
1920 dst_templ
.bind
= bind
;
1921 dst_templ
.usage
= PIPE_USAGE_STAGING
;
1923 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1924 &dst_templ
.width0
, &dst_templ
.height0
,
1925 &dst_templ
.depth0
, &dst_templ
.array_size
);
1927 dst
= screen
->resource_create(screen
, &dst_templ
);
1932 /* From now on, we need the gallium representation of dimensions. */
1933 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1940 assert(texImage
->Face
== 0 ||
1941 texImage
->TexObject
->MinLayer
== 0 ||
1944 memset(&blit
, 0, sizeof(blit
));
1945 blit
.src
.resource
= src
;
1946 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
1947 blit
.src
.format
= src_format
;
1948 blit
.dst
.resource
= dst
;
1950 blit
.dst
.format
= dst
->format
;
1951 blit
.src
.box
.x
= xoffset
;
1953 blit
.src
.box
.y
= yoffset
;
1955 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
1957 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1958 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1959 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1960 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
1961 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1962 blit
.scissor_enable
= FALSE
;
1964 /* blit/render/decompress */
1965 st
->pipe
->blit(st
->pipe
, &blit
);
1967 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
1969 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
1970 0, 0, 0, width
, height
, depth
, &tex_xfer
);
1975 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
1977 /* copy/pack data into user buffer */
1978 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
1979 ctx
->Pack
.SwapBytes
, NULL
)) {
1981 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
1984 for (slice
= 0; slice
< depth
; slice
++) {
1985 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1986 /* 1D array textures.
1987 * We need to convert gallium coords to GL coords.
1989 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1990 width
, depth
, format
,
1992 memcpy(dest
, map
, bytesPerRow
);
1995 ubyte
*slice_map
= map
;
1997 for (row
= 0; row
< height
; row
++) {
1998 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1999 width
, height
, format
,
2000 type
, slice
, row
, 0);
2001 memcpy(dest
, slice_map
, bytesPerRow
);
2002 slice_map
+= tex_xfer
->stride
;
2005 map
+= tex_xfer
->layer_stride
;
2009 /* format translation via floats */
2012 uint32_t dstMesaFormat
;
2013 int dstStride
, srcStride
;
2015 assert(util_format_is_compressed(src
->format
));
2017 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
2022 if (ST_DEBUG
& DEBUG_FALLBACK
)
2023 debug_printf("%s: fallback format translation\n", __func__
);
2025 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2026 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2027 srcStride
= 4 * width
* sizeof(GLfloat
);
2028 for (slice
= 0; slice
< depth
; slice
++) {
2029 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2030 /* 1D array textures.
2031 * We need to convert gallium coords to GL coords.
2033 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2034 width
, depth
, format
,
2037 /* get float[4] rgba row from surface */
2038 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, 1,
2041 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2042 rgba
, RGBA32_FLOAT
, srcStride
,
2046 for (row
= 0; row
< height
; row
++) {
2047 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2048 width
, height
, format
,
2049 type
, slice
, row
, 0);
2051 /* get float[4] rgba row from surface */
2052 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, row
, width
, 1,
2055 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2056 rgba
, RGBA32_FLOAT
, srcStride
,
2060 map
+= tex_xfer
->layer_stride
;
2069 pipe_transfer_unmap(pipe
, tex_xfer
);
2071 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2072 pipe_resource_reference(&dst
, NULL
);
2076 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2077 width
, height
, depth
,
2078 format
, type
, pixels
, texImage
);
2084 * Do a CopyTexSubImage operation using a read transfer from the source,
2085 * a write transfer to the destination and get_tile()/put_tile() to access
2086 * the pixels/texels.
2088 * Note: srcY=0=TOP of renderbuffer
2091 fallback_copy_texsubimage(struct gl_context
*ctx
,
2092 struct st_renderbuffer
*strb
,
2093 struct st_texture_image
*stImage
,
2095 GLint destX
, GLint destY
, GLint slice
,
2096 GLint srcX
, GLint srcY
,
2097 GLsizei width
, GLsizei height
)
2099 struct st_context
*st
= st_context(ctx
);
2100 struct pipe_context
*pipe
= st
->pipe
;
2101 struct pipe_transfer
*src_trans
;
2103 enum pipe_transfer_usage transfer_usage
;
2105 unsigned dst_width
= width
;
2106 unsigned dst_height
= height
;
2107 unsigned dst_depth
= 1;
2108 struct pipe_transfer
*transfer
;
2110 if (ST_DEBUG
& DEBUG_FALLBACK
)
2111 debug_printf("%s: fallback processing\n", __func__
);
2113 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2114 srcY
= strb
->Base
.Height
- srcY
- height
;
2117 map
= pipe_transfer_map(pipe
,
2119 strb
->surface
->u
.tex
.level
,
2120 strb
->surface
->u
.tex
.first_layer
,
2123 width
, height
, &src_trans
);
2125 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2126 baseFormat
== GL_DEPTH_STENCIL
) &&
2127 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2128 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2130 transfer_usage
= PIPE_TRANSFER_WRITE
;
2132 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2133 destX
, destY
, slice
,
2134 dst_width
, dst_height
, dst_depth
,
2137 if (baseFormat
== GL_DEPTH_COMPONENT
||
2138 baseFormat
== GL_DEPTH_STENCIL
) {
2139 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2140 ctx
->Pixel
.DepthBias
!= 0.0F
);
2144 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2145 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2154 data
= malloc(width
* sizeof(uint
));
2157 /* To avoid a large temp memory allocation, do copy row by row */
2158 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2159 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2161 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2164 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2165 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2166 0, 0, width
, 1, data
);
2169 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2174 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2182 malloc(width
* height
* 4 * sizeof(GLfloat
));
2184 if (tempSrc
&& texDest
) {
2185 const GLint dims
= 2;
2187 struct gl_texture_image
*texImage
= &stImage
->base
;
2188 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2190 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2191 unpack
.Invert
= GL_TRUE
;
2194 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2195 dstRowStride
= transfer
->layer_stride
;
2198 dstRowStride
= transfer
->stride
;
2201 /* get float/RGBA image from framebuffer */
2202 /* XXX this usually involves a lot of int/float conversion.
2203 * try to avoid that someday.
2205 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2206 util_format_linear(strb
->texture
->format
),
2209 /* Store into texture memory.
2210 * Note that this does some special things such as pixel transfer
2211 * ops and format conversion. In particular, if the dest tex format
2212 * is actually RGBA but the user created the texture as GL_RGB we
2213 * need to fill-in/override the alpha channel with 1.0.
2215 _mesa_texstore(ctx
, dims
,
2216 texImage
->_BaseFormat
,
2217 texImage
->TexFormat
,
2221 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2225 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2231 st_texture_image_unmap(st
, stImage
, slice
);
2232 pipe
->transfer_unmap(pipe
, src_trans
);
2237 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2238 * Note that the region to copy has already been clipped so we know we
2239 * won't read from outside the source renderbuffer's bounds.
2241 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2244 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2245 struct gl_texture_image
*texImage
,
2246 GLint destX
, GLint destY
, GLint slice
,
2247 struct gl_renderbuffer
*rb
,
2248 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2250 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2251 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2252 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2253 struct st_context
*st
= st_context(ctx
);
2254 struct pipe_context
*pipe
= st
->pipe
;
2255 struct pipe_screen
*screen
= pipe
->screen
;
2256 struct pipe_blit_info blit
;
2257 enum pipe_format dst_format
;
2258 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2262 st_flush_bitmap_cache(st
);
2264 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2265 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2267 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2268 debug_printf("%s: null strb or stImage\n", __func__
);
2272 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2273 texImage
->TexFormat
)) {
2277 /* The base internal format must match the mesa format, so make sure
2278 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
2280 if (texImage
->_BaseFormat
!=
2281 _mesa_get_format_base_format(texImage
->TexFormat
) ||
2282 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
2286 /* Choose the destination format to match the TexImage behavior. */
2287 dst_format
= util_format_linear(stImage
->pt
->format
);
2288 dst_format
= util_format_luminance_to_red(dst_format
);
2289 dst_format
= util_format_intensity_to_red(dst_format
);
2291 /* See if the destination format is supported. */
2292 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2293 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2294 bind
= PIPE_BIND_DEPTH_STENCIL
;
2297 bind
= PIPE_BIND_RENDER_TARGET
;
2301 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2302 stImage
->pt
->nr_samples
, bind
)) {
2306 /* Y flipping for the main framebuffer. */
2308 srcY1
= strb
->Base
.Height
- srcY
- height
;
2309 srcY0
= srcY1
+ height
;
2313 srcY1
= srcY0
+ height
;
2316 /* Blit the texture.
2317 * This supports flipping, format conversions, and downsampling.
2319 memset(&blit
, 0, sizeof(blit
));
2320 blit
.src
.resource
= strb
->texture
;
2321 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2322 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2323 blit
.src
.box
.x
= srcX
;
2324 blit
.src
.box
.y
= srcY0
;
2325 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2326 blit
.src
.box
.width
= width
;
2327 blit
.src
.box
.height
= srcY1
- srcY0
;
2328 blit
.src
.box
.depth
= 1;
2329 blit
.dst
.resource
= stImage
->pt
;
2330 blit
.dst
.format
= dst_format
;
2331 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2332 blit
.dst
.box
.x
= destX
;
2333 blit
.dst
.box
.y
= destY
;
2334 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2335 blit
.dst
.box
.width
= width
;
2336 blit
.dst
.box
.height
= height
;
2337 blit
.dst
.box
.depth
= 1;
2338 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2339 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2340 pipe
->blit(pipe
, &blit
);
2344 /* software fallback */
2345 fallback_copy_texsubimage(ctx
,
2346 strb
, stImage
, texImage
->_BaseFormat
,
2347 destX
, destY
, slice
,
2348 srcX
, srcY
, width
, height
);
2353 * Copy image data from stImage into the texture object 'stObj' at level
2357 copy_image_data_to_texture(struct st_context
*st
,
2358 struct st_texture_object
*stObj
,
2360 struct st_texture_image
*stImage
)
2364 const struct gl_texture_image
*dstImage
=
2365 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2367 assert(dstImage
->Width
== stImage
->base
.Width
);
2368 assert(dstImage
->Height
== stImage
->base
.Height
);
2369 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2373 /* Copy potentially with the blitter:
2376 if (stImage
->pt
->last_level
== 0)
2379 src_level
= stImage
->base
.Level
;
2381 assert(src_level
<= stImage
->pt
->last_level
);
2382 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2383 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2384 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2385 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2386 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2387 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2389 st_texture_image_copy(st
->pipe
,
2390 stObj
->pt
, dstLevel
, /* dest texture, level */
2391 stImage
->pt
, src_level
, /* src texture, level */
2392 stImage
->base
.Face
);
2394 pipe_resource_reference(&stImage
->pt
, NULL
);
2396 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2401 * Called during state validation. When this function is finished,
2402 * the texture object should be ready for rendering.
2403 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2406 st_finalize_texture(struct gl_context
*ctx
,
2407 struct pipe_context
*pipe
,
2408 struct gl_texture_object
*tObj
)
2410 struct st_context
*st
= st_context(ctx
);
2411 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2412 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2414 const struct st_texture_image
*firstImage
;
2415 enum pipe_format firstImageFormat
;
2416 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2418 if (tObj
->Immutable
)
2421 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
2422 /* The texture is complete and we know exactly how many mipmap levels
2423 * are present/needed. This is conditional because we may be called
2424 * from the st_generate_mipmap() function when the texture object is
2425 * incomplete. In that case, we'll have set stObj->lastLevel before
2428 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
2429 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
2430 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2432 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2435 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
2436 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
2439 pipe_resource_reference(&stObj
->pt
, NULL
);
2440 st_texture_release_all_sampler_views(st
, stObj
);
2444 if (st_obj
->buffer
!= stObj
->pt
) {
2445 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
2446 st_texture_release_all_sampler_views(st
, stObj
);
2452 firstImage
= st_texture_image_const(_mesa_base_tex_image(&stObj
->base
));
2455 /* If both firstImage and stObj point to a texture which can contain
2456 * all active images, favour firstImage. Note that because of the
2457 * completeness requirement, we know that the image dimensions
2460 if (firstImage
->pt
&&
2461 firstImage
->pt
!= stObj
->pt
&&
2462 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2463 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2464 st_texture_release_all_sampler_views(st
, stObj
);
2467 /* If this texture comes from a window system, there is nothing else to do. */
2468 if (stObj
->surface_based
) {
2472 /* Find gallium format for the Mesa texture */
2474 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2476 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2478 GLuint width
, height
, depth
;
2480 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2481 firstImage
->base
.Width2
,
2482 firstImage
->base
.Height2
,
2483 firstImage
->base
.Depth2
,
2484 &width
, &height
, &depth
, &ptLayers
);
2486 /* If we previously allocated a pipe texture and its sizes are
2487 * compatible, use them.
2490 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2491 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2492 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2493 ptWidth
= stObj
->pt
->width0
;
2494 ptHeight
= stObj
->pt
->height0
;
2495 ptDepth
= stObj
->pt
->depth0
;
2497 /* Otherwise, compute a new level=0 size that is compatible with the
2500 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2501 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2502 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2504 /* If the base level image is 1x1x1, we still need to ensure that the
2505 * resulting pipe texture ends up with the required number of levels
2508 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2509 ptWidth
<<= firstImage
->base
.Level
;
2511 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2512 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2517 ptNumSamples
= firstImage
->base
.NumSamples
;
2520 /* If we already have a gallium texture, check that it matches the texture
2521 * object's format, target, size, num_levels, etc.
2524 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2525 stObj
->pt
->format
!= firstImageFormat
||
2526 stObj
->pt
->last_level
< stObj
->lastLevel
||
2527 stObj
->pt
->width0
!= ptWidth
||
2528 stObj
->pt
->height0
!= ptHeight
||
2529 stObj
->pt
->depth0
!= ptDepth
||
2530 stObj
->pt
->nr_samples
!= ptNumSamples
||
2531 stObj
->pt
->array_size
!= ptLayers
)
2533 /* The gallium texture does not match the Mesa texture so delete the
2534 * gallium texture now. We'll make a new one below.
2536 pipe_resource_reference(&stObj
->pt
, NULL
);
2537 st_texture_release_all_sampler_views(st
, stObj
);
2538 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
2542 /* May need to create a new gallium texture:
2545 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2547 stObj
->pt
= st_texture_create(st
,
2548 gl_target_to_pipe(stObj
->base
.Target
),
2554 ptLayers
, ptNumSamples
,
2558 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2563 /* Pull in any images not in the object's texture:
2565 for (face
= 0; face
< nr_faces
; face
++) {
2567 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2568 struct st_texture_image
*stImage
=
2569 st_texture_image(stObj
->base
.Image
[face
][level
]);
2571 /* Need to import images in main memory or held in other textures.
2573 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2577 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2578 height
= u_minify(ptHeight
, level
);
2582 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2583 depth
= u_minify(ptDepth
, level
);
2584 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2590 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2591 stImage
->base
.Height
== height
&&
2592 stImage
->base
.Depth
== depth
)) {
2593 /* src image fits expected dest mipmap level size */
2594 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2605 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2606 * for a whole mipmap stack.
2609 st_AllocTextureStorage(struct gl_context
*ctx
,
2610 struct gl_texture_object
*texObj
,
2611 GLsizei levels
, GLsizei width
,
2612 GLsizei height
, GLsizei depth
)
2614 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2615 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2616 struct st_context
*st
= st_context(ctx
);
2617 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2618 struct pipe_screen
*screen
= st
->pipe
->screen
;
2619 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
2620 enum pipe_format fmt
;
2622 GLuint num_samples
= texImage
->NumSamples
;
2626 stObj
->lastLevel
= levels
- 1;
2628 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2630 bindings
= default_bindings(st
, fmt
);
2632 /* Raise the sample count if the requested one is unsupported. */
2633 if (num_samples
> 1) {
2634 boolean found
= FALSE
;
2636 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2637 if (screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
2639 PIPE_BIND_SAMPLER_VIEW
)) {
2640 /* Update the sample count in gl_texture_image as well. */
2641 texImage
->NumSamples
= num_samples
;
2652 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2653 width
, height
, depth
,
2654 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2656 stObj
->pt
= st_texture_create(st
,
2657 gl_target_to_pipe(texObj
->Target
),
2663 ptLayers
, num_samples
,
2668 /* Set image resource pointers */
2669 for (level
= 0; level
< levels
; level
++) {
2671 for (face
= 0; face
< numFaces
; face
++) {
2672 struct st_texture_image
*stImage
=
2673 st_texture_image(texObj
->Image
[face
][level
]);
2674 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2683 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2684 GLint level
, mesa_format format
,
2685 GLint width
, GLint height
,
2686 GLint depth
, GLint border
)
2688 struct st_context
*st
= st_context(ctx
);
2689 struct pipe_context
*pipe
= st
->pipe
;
2691 if (width
== 0 || height
== 0 || depth
== 0) {
2692 /* zero-sized images are legal, and always fit! */
2696 if (pipe
->screen
->can_create_resource
) {
2697 /* Ask the gallium driver if the texture is too large */
2698 struct gl_texture_object
*texObj
=
2699 _mesa_get_current_tex_object(ctx
, target
);
2700 struct pipe_resource pt
;
2702 /* Setup the pipe_resource object
2704 memset(&pt
, 0, sizeof(pt
));
2706 pt
.target
= gl_target_to_pipe(target
);
2707 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
2709 st_gl_texture_dims_to_pipe_dims(target
,
2710 width
, height
, depth
,
2711 &pt
.width0
, &pt
.height0
,
2712 &pt
.depth0
, &pt
.array_size
);
2714 if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
2715 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
2716 /* assume just one mipmap level */
2720 /* assume a full set of mipmaps */
2721 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
2724 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
2727 /* Use core Mesa fallback */
2728 return _mesa_test_proxy_teximage(ctx
, target
, level
, format
,
2729 width
, height
, depth
, border
);
2734 st_TextureView(struct gl_context
*ctx
,
2735 struct gl_texture_object
*texObj
,
2736 struct gl_texture_object
*origTexObj
)
2738 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
2739 struct st_texture_object
*tex
= st_texture_object(texObj
);
2740 struct gl_texture_image
*image
= texObj
->Image
[0][0];
2742 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2743 const int numLevels
= texObj
->NumLevels
;
2748 pipe_resource_reference(&tex
->pt
, orig
->pt
);
2750 /* Set image resource pointers */
2751 for (level
= 0; level
< numLevels
; level
++) {
2752 for (face
= 0; face
< numFaces
; face
++) {
2753 struct st_texture_image
*stImage
=
2754 st_texture_image(texObj
->Image
[face
][level
]);
2755 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
2759 tex
->surface_based
= GL_TRUE
;
2760 tex
->surface_format
=
2761 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
2763 tex
->lastLevel
= numLevels
- 1;
2769 st_ClearTexSubImage(struct gl_context
*ctx
,
2770 struct gl_texture_image
*texImage
,
2771 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2772 GLsizei width
, GLsizei height
, GLsizei depth
,
2773 const void *clearValue
)
2775 static const char zeros
[16] = {0};
2776 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2777 struct pipe_resource
*pt
= stImage
->pt
;
2778 struct st_context
*st
= st_context(ctx
);
2779 struct pipe_context
*pipe
= st
->pipe
;
2780 unsigned level
= texImage
->Level
;
2781 struct pipe_box box
;
2786 st_flush_bitmap_cache(st
);
2788 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
2789 width
, height
, depth
, &box
);
2790 if (texImage
->TexObject
->Immutable
) {
2791 level
+= texImage
->TexObject
->MinLevel
;
2792 box
.z
+= texImage
->TexObject
->MinLayer
;
2795 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
2799 st_init_texture_functions(struct dd_function_table
*functions
)
2801 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
2802 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
2803 functions
->TexImage
= st_TexImage
;
2804 functions
->TexSubImage
= st_TexSubImage
;
2805 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
2806 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
2807 functions
->GenerateMipmap
= st_generate_mipmap
;
2809 functions
->GetTexSubImage
= st_GetTexSubImage
;
2811 /* compressed texture functions */
2812 functions
->CompressedTexImage
= st_CompressedTexImage
;
2813 functions
->GetCompressedTexSubImage
= _mesa_GetCompressedTexSubImage_sw
;
2815 functions
->NewTextureObject
= st_NewTextureObject
;
2816 functions
->NewTextureImage
= st_NewTextureImage
;
2817 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
2818 functions
->DeleteTexture
= st_DeleteTextureObject
;
2819 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
2820 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
2821 functions
->MapTextureImage
= st_MapTextureImage
;
2822 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
2824 /* XXX Temporary until we can query pipe's texture sizes */
2825 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
2827 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
2828 functions
->TextureView
= st_TextureView
;
2829 functions
->ClearTexSubImage
= st_ClearTexSubImage
;