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 st_etc_fallback(struct st_context
*st
, struct gl_texture_image
*texImage
)
195 return (_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
196 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
);
199 /** called via ctx->Driver.MapTextureImage() */
201 st_MapTextureImage(struct gl_context
*ctx
,
202 struct gl_texture_image
*texImage
,
203 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
205 GLubyte
**mapOut
, GLint
*rowStrideOut
)
207 struct st_context
*st
= st_context(ctx
);
208 struct st_texture_image
*stImage
= st_texture_image(texImage
);
211 struct pipe_transfer
*transfer
;
214 if (mode
& GL_MAP_READ_BIT
)
215 pipeMode
|= PIPE_TRANSFER_READ
;
216 if (mode
& GL_MAP_WRITE_BIT
)
217 pipeMode
|= PIPE_TRANSFER_WRITE
;
218 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
219 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
221 map
= st_texture_image_map(st
, stImage
, pipeMode
, x
, y
, slice
, w
, h
, 1,
224 if (st_etc_fallback(st
, texImage
)) {
225 /* ETC isn't supported by gallium and it's represented
226 * by uncompressed formats. Only write transfers with precompressed
227 * data are supported by ES3, which makes this really simple.
229 * Just create a temporary storage where the ETC texture will
230 * be stored. It will be decompressed in the Unmap function.
232 unsigned z
= transfer
->box
.z
;
233 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
235 itransfer
->temp_data
=
236 malloc(_mesa_format_image_size(texImage
->TexFormat
, w
, h
, 1));
237 itransfer
->temp_stride
=
238 _mesa_format_row_stride(texImage
->TexFormat
, w
);
239 itransfer
->map
= map
;
241 *mapOut
= itransfer
->temp_data
;
242 *rowStrideOut
= itransfer
->temp_stride
;
245 /* supported mapping */
247 *rowStrideOut
= transfer
->stride
;
257 /** called via ctx->Driver.UnmapTextureImage() */
259 st_UnmapTextureImage(struct gl_context
*ctx
,
260 struct gl_texture_image
*texImage
,
263 struct st_context
*st
= st_context(ctx
);
264 struct st_texture_image
*stImage
= st_texture_image(texImage
);
266 if (st_etc_fallback(st
, texImage
)) {
267 /* Decompress the ETC texture to the mapped one. */
268 unsigned z
= slice
+ stImage
->base
.Face
;
269 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
270 struct pipe_transfer
*transfer
= itransfer
->transfer
;
272 assert(z
== transfer
->box
.z
);
274 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
275 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
276 itransfer
->temp_data
,
277 itransfer
->temp_stride
,
278 transfer
->box
.width
, transfer
->box
.height
);
281 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
282 itransfer
->temp_data
, itransfer
->temp_stride
,
283 transfer
->box
.width
, transfer
->box
.height
,
284 texImage
->TexFormat
);
287 free(itransfer
->temp_data
);
288 itransfer
->temp_data
= NULL
;
289 itransfer
->temp_stride
= 0;
293 st_texture_image_unmap(st
, stImage
, slice
);
298 * Return default texture resource binding bitmask for the given format.
301 default_bindings(struct st_context
*st
, enum pipe_format format
)
303 struct pipe_screen
*screen
= st
->pipe
->screen
;
304 const unsigned target
= PIPE_TEXTURE_2D
;
307 if (util_format_is_depth_or_stencil(format
))
308 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
310 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
312 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
316 format
= util_format_linear(format
);
318 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
321 return PIPE_BIND_SAMPLER_VIEW
;
327 * Given the size of a mipmap image, try to compute the size of the level=0
330 * Note that this isn't always accurate for odd-sized, non-POW textures.
331 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
333 * \return GL_TRUE for success, GL_FALSE for failure
336 guess_base_level_size(GLenum target
,
337 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
338 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
345 /* Guess the size of the base level.
346 * Depending on the image's size, we can't always make a guess here.
350 case GL_TEXTURE_1D_ARRAY
:
355 case GL_TEXTURE_2D_ARRAY
:
356 /* We can't make a good guess here, because the base level dimensions
359 if (width
== 1 || height
== 1) {
366 case GL_TEXTURE_CUBE_MAP
:
367 case GL_TEXTURE_CUBE_MAP_ARRAY
:
373 /* We can't make a good guess here, because the base level dimensions
376 if (width
== 1 || height
== 1 || depth
== 1) {
384 case GL_TEXTURE_RECTANGLE
:
401 * Try to determine whether we should allocate memory for a full texture
402 * mipmap. The problem is when we get a glTexImage(level=0) call, we
403 * can't immediately know if other mipmap levels are coming next. Here
404 * we try to guess whether to allocate memory for a mipmap or just the
407 * If we guess incorrectly here we'll later reallocate the right amount of
408 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
410 * \param stObj the texture object we're going to allocate memory for.
411 * \param stImage describes the incoming image which we need to store.
414 allocate_full_mipmap(const struct st_texture_object
*stObj
,
415 const struct st_texture_image
*stImage
)
417 switch (stObj
->base
.Target
) {
418 case GL_TEXTURE_RECTANGLE_NV
:
419 case GL_TEXTURE_BUFFER
:
420 case GL_TEXTURE_EXTERNAL_OES
:
421 case GL_TEXTURE_2D_MULTISAMPLE
:
422 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
423 /* these texture types cannot be mipmapped */
427 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
430 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
431 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
432 /* depth/stencil textures are seldom mipmapped */
435 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
438 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
439 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
440 /* not a mipmap minification filter */
443 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
444 /* 3D textures are seldom mipmapped */
452 * Try to allocate a pipe_resource object for the given st_texture_object.
454 * We use the given st_texture_image as a clue to determine the size of the
455 * mipmap image at level=0.
457 * \return GL_TRUE for success, GL_FALSE if out of memory.
460 guess_and_alloc_texture(struct st_context
*st
,
461 struct st_texture_object
*stObj
,
462 const struct st_texture_image
*stImage
)
464 const struct gl_texture_image
*firstImage
;
465 GLuint lastLevel
, width
, height
, depth
;
467 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
468 enum pipe_format fmt
;
469 bool guessed_box
= false;
471 DBG("%s\n", __func__
);
475 /* If a base level image with compatible size exists, use that as our guess.
477 firstImage
= _mesa_base_tex_image(&stObj
->base
);
479 firstImage
->Width2
> 0 &&
480 firstImage
->Height2
> 0 &&
481 firstImage
->Depth2
> 0 &&
482 guess_base_level_size(stObj
->base
.Target
,
487 &width
, &height
, &depth
)) {
488 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
489 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
490 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
495 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
496 stImage
->base
.Width2
,
497 stImage
->base
.Height2
,
498 stImage
->base
.Depth2
,
500 &width
, &height
, &depth
);
503 /* we can't determine the image size at level=0 */
504 /* this is not an out of memory error */
508 /* At this point, (width x height x depth) is the expected size of
509 * the level=0 mipmap image.
512 /* Guess a reasonable value for lastLevel. With OpenGL we have no
513 * idea how many mipmap levels will be in a texture until we start
514 * to render with it. Make an educated guess here but be prepared
515 * to re-allocating a texture buffer with space for more (or fewer)
516 * mipmap levels later.
518 if (allocate_full_mipmap(stObj
, stImage
)) {
519 /* alloc space for a full mipmap */
520 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
521 width
, height
, depth
) - 1;
524 /* only alloc space for a single mipmap level */
528 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
530 bindings
= default_bindings(st
, fmt
);
532 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
533 width
, height
, depth
,
534 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
536 stObj
->pt
= st_texture_create(st
,
537 gl_target_to_pipe(stObj
->base
.Target
),
546 stObj
->lastLevel
= lastLevel
;
548 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
550 return stObj
->pt
!= NULL
;
555 * Called via ctx->Driver.AllocTextureImageBuffer().
556 * If the texture object/buffer already has space for the indicated image,
557 * we're done. Otherwise, allocate memory for the new texture image.
560 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
561 struct gl_texture_image
*texImage
)
563 struct st_context
*st
= st_context(ctx
);
564 struct st_texture_image
*stImage
= st_texture_image(texImage
);
565 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
566 const GLuint level
= texImage
->Level
;
567 GLuint width
= texImage
->Width
;
568 GLuint height
= texImage
->Height
;
569 GLuint depth
= texImage
->Depth
;
571 DBG("%s\n", __func__
);
573 assert(!stImage
->pt
); /* xxx this might be wrong */
575 /* Look if the parent texture object has space for this image */
577 level
<= stObj
->pt
->last_level
&&
578 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
579 /* this image will fit in the existing texture object's memory */
580 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
584 /* The parent texture object does not have space for this image */
586 pipe_resource_reference(&stObj
->pt
, NULL
);
587 st_texture_release_all_sampler_views(st
, stObj
);
589 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
590 /* Probably out of memory.
591 * Try flushing any pending rendering, then retry.
594 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
595 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
601 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
602 /* The image will live in the object's mipmap memory */
603 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
608 /* Create a new, temporary texture/resource/buffer to hold this
609 * one texture image. Note that when we later access this image
610 * (either for mapping or copying) we'll want to always specify
611 * mipmap level=0, even if the image represents some other mipmap
614 enum pipe_format format
=
615 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
616 GLuint bindings
= default_bindings(st
, format
);
617 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
619 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
620 width
, height
, depth
,
621 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
623 stImage
->pt
= st_texture_create(st
,
624 gl_target_to_pipe(stObj
->base
.Target
),
632 return stImage
->pt
!= NULL
;
638 * Preparation prior to glTexImage. Basically check the 'surface_based'
639 * field and switch to a "normal" tex image if necessary.
642 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
643 GLenum format
, GLenum type
)
645 struct gl_texture_object
*texObj
= texImage
->TexObject
;
646 struct st_texture_object
*stObj
= st_texture_object(texObj
);
648 /* switch to "normal" */
649 if (stObj
->surface_based
) {
650 const GLenum target
= texObj
->Target
;
651 const GLuint level
= texImage
->Level
;
652 mesa_format texFormat
;
654 _mesa_clear_texture_object(ctx
, texObj
);
655 pipe_resource_reference(&stObj
->pt
, NULL
);
657 /* oops, need to init this image again */
658 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
659 texImage
->InternalFormat
, format
,
662 _mesa_init_teximage_fields(ctx
, texImage
,
663 texImage
->Width
, texImage
->Height
,
664 texImage
->Depth
, texImage
->Border
,
665 texImage
->InternalFormat
, texFormat
);
667 stObj
->surface_based
= GL_FALSE
;
673 * Return a writemask for the gallium blit. The parameters can be base
674 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
677 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
680 case GL_DEPTH_STENCIL
:
682 case GL_DEPTH_STENCIL
:
684 case GL_DEPTH_COMPONENT
:
686 case GL_STENCIL_INDEX
:
693 case GL_DEPTH_COMPONENT
:
695 case GL_DEPTH_STENCIL
:
696 case GL_DEPTH_COMPONENT
:
703 case GL_STENCIL_INDEX
:
705 case GL_STENCIL_INDEX
:
713 return PIPE_MASK_RGBA
;
718 * Converts format to a format with the same components, types
719 * and sizes, but with the components in RGBA order.
721 static enum pipe_format
722 unswizzle_format(enum pipe_format format
)
726 case PIPE_FORMAT_B8G8R8A8_UNORM
:
727 case PIPE_FORMAT_A8R8G8B8_UNORM
:
728 case PIPE_FORMAT_A8B8G8R8_UNORM
:
729 return PIPE_FORMAT_R8G8B8A8_UNORM
;
731 case PIPE_FORMAT_B10G10R10A2_UNORM
:
732 return PIPE_FORMAT_R10G10B10A2_UNORM
;
734 case PIPE_FORMAT_B10G10R10A2_SNORM
:
735 return PIPE_FORMAT_R10G10B10A2_SNORM
;
737 case PIPE_FORMAT_B10G10R10A2_UINT
:
738 return PIPE_FORMAT_R10G10B10A2_UINT
;
746 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
748 static enum pipe_format
749 alpha_to_red(enum pipe_format format
)
753 case PIPE_FORMAT_A8_UNORM
:
754 return PIPE_FORMAT_R8_UNORM
;
755 case PIPE_FORMAT_A8_SNORM
:
756 return PIPE_FORMAT_R8_SNORM
;
757 case PIPE_FORMAT_A8_UINT
:
758 return PIPE_FORMAT_R8_UINT
;
759 case PIPE_FORMAT_A8_SINT
:
760 return PIPE_FORMAT_R8_SINT
;
762 case PIPE_FORMAT_A16_UNORM
:
763 return PIPE_FORMAT_R16_UNORM
;
764 case PIPE_FORMAT_A16_SNORM
:
765 return PIPE_FORMAT_R16_SNORM
;
766 case PIPE_FORMAT_A16_UINT
:
767 return PIPE_FORMAT_R16_UINT
;
768 case PIPE_FORMAT_A16_SINT
:
769 return PIPE_FORMAT_R16_SINT
;
770 case PIPE_FORMAT_A16_FLOAT
:
771 return PIPE_FORMAT_R16_FLOAT
;
773 case PIPE_FORMAT_A32_UINT
:
774 return PIPE_FORMAT_R32_UINT
;
775 case PIPE_FORMAT_A32_SINT
:
776 return PIPE_FORMAT_R32_SINT
;
777 case PIPE_FORMAT_A32_FLOAT
:
778 return PIPE_FORMAT_R32_FLOAT
;
786 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
788 static enum pipe_format
789 red_alpha_to_red_green(enum pipe_format format
)
793 case PIPE_FORMAT_R8A8_UNORM
:
794 return PIPE_FORMAT_R8G8_UNORM
;
795 case PIPE_FORMAT_R8A8_SNORM
:
796 return PIPE_FORMAT_R8G8_SNORM
;
797 case PIPE_FORMAT_R8A8_UINT
:
798 return PIPE_FORMAT_R8G8_UINT
;
799 case PIPE_FORMAT_R8A8_SINT
:
800 return PIPE_FORMAT_R8G8_SINT
;
802 case PIPE_FORMAT_R16A16_UNORM
:
803 return PIPE_FORMAT_R16G16_UNORM
;
804 case PIPE_FORMAT_R16A16_SNORM
:
805 return PIPE_FORMAT_R16G16_SNORM
;
806 case PIPE_FORMAT_R16A16_UINT
:
807 return PIPE_FORMAT_R16G16_UINT
;
808 case PIPE_FORMAT_R16A16_SINT
:
809 return PIPE_FORMAT_R16G16_SINT
;
810 case PIPE_FORMAT_R16A16_FLOAT
:
811 return PIPE_FORMAT_R16G16_FLOAT
;
813 case PIPE_FORMAT_R32A32_UINT
:
814 return PIPE_FORMAT_R32G32_UINT
;
815 case PIPE_FORMAT_R32A32_SINT
:
816 return PIPE_FORMAT_R32G32_SINT
;
817 case PIPE_FORMAT_R32A32_FLOAT
:
818 return PIPE_FORMAT_R32G32_FLOAT
;
826 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
828 static enum pipe_format
829 luminance_alpha_to_red_green(enum pipe_format format
)
833 case PIPE_FORMAT_L8A8_UNORM
:
834 return PIPE_FORMAT_R8G8_UNORM
;
835 case PIPE_FORMAT_L8A8_SNORM
:
836 return PIPE_FORMAT_R8G8_SNORM
;
837 case PIPE_FORMAT_L8A8_UINT
:
838 return PIPE_FORMAT_R8G8_UINT
;
839 case PIPE_FORMAT_L8A8_SINT
:
840 return PIPE_FORMAT_R8G8_SINT
;
842 case PIPE_FORMAT_L16A16_UNORM
:
843 return PIPE_FORMAT_R16G16_UNORM
;
844 case PIPE_FORMAT_L16A16_SNORM
:
845 return PIPE_FORMAT_R16G16_SNORM
;
846 case PIPE_FORMAT_L16A16_UINT
:
847 return PIPE_FORMAT_R16G16_UINT
;
848 case PIPE_FORMAT_L16A16_SINT
:
849 return PIPE_FORMAT_R16G16_SINT
;
850 case PIPE_FORMAT_L16A16_FLOAT
:
851 return PIPE_FORMAT_R16G16_FLOAT
;
853 case PIPE_FORMAT_L32A32_UINT
:
854 return PIPE_FORMAT_R32G32_UINT
;
855 case PIPE_FORMAT_L32A32_SINT
:
856 return PIPE_FORMAT_R32G32_SINT
;
857 case PIPE_FORMAT_L32A32_FLOAT
:
858 return PIPE_FORMAT_R32G32_FLOAT
;
866 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
869 format_is_alpha(enum pipe_format format
)
871 const struct util_format_description
*desc
= util_format_description(format
);
873 if (desc
->nr_channels
== 1 &&
874 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
875 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
876 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
877 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
884 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
887 format_is_red(enum pipe_format format
)
889 const struct util_format_description
*desc
= util_format_description(format
);
891 if (desc
->nr_channels
== 1 &&
892 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
893 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
894 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
895 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
903 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
906 format_is_luminance(enum pipe_format format
)
908 const struct util_format_description
*desc
= util_format_description(format
);
910 if (desc
->nr_channels
== 1 &&
911 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
912 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
913 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
914 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
921 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
924 format_is_red_alpha(enum pipe_format format
)
926 const struct util_format_description
*desc
= util_format_description(format
);
928 if (desc
->nr_channels
== 2 &&
929 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
930 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
931 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
932 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
939 format_is_swizzled_rgba(enum pipe_format format
)
941 const struct util_format_description
*desc
= util_format_description(format
);
943 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
944 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
945 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
946 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
954 unsigned char swizzle
[4];
955 enum pipe_format format
;
958 static const struct format_table table_8888_unorm
[] = {
959 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
960 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
961 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
962 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
965 static const struct format_table table_1010102_unorm
[] = {
966 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
967 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
970 static const struct format_table table_1010102_snorm
[] = {
971 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
972 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
975 static const struct format_table table_1010102_uint
[] = {
976 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
977 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
980 static enum pipe_format
981 swizzle_format(enum pipe_format format
, const int * const swizzle
)
986 case PIPE_FORMAT_R8G8B8A8_UNORM
:
987 case PIPE_FORMAT_B8G8R8A8_UNORM
:
988 case PIPE_FORMAT_A8R8G8B8_UNORM
:
989 case PIPE_FORMAT_A8B8G8R8_UNORM
:
990 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
991 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
992 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
993 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
994 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
995 return table_8888_unorm
[i
].format
;
999 case PIPE_FORMAT_R10G10B10A2_UNORM
:
1000 case PIPE_FORMAT_B10G10R10A2_UNORM
:
1001 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
1002 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
1003 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
1004 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
1005 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
1006 return table_1010102_unorm
[i
].format
;
1010 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1011 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1012 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1013 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1014 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1015 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1016 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1017 return table_1010102_snorm
[i
].format
;
1021 case PIPE_FORMAT_R10G10B10A2_UINT
:
1022 case PIPE_FORMAT_B10G10R10A2_UINT
:
1023 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1024 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1025 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1026 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1027 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1028 return table_1010102_uint
[i
].format
;
1036 return PIPE_FORMAT_NONE
;
1040 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1042 enum pipe_format src
= *src_format
;
1043 enum pipe_format dst
= *dst_format
;
1045 /* Note: dst_format has already been transformed from luminance/intensity
1046 * to red when this function is called. The source format will never
1047 * be an intensity format, because GL_INTENSITY is not a legal value
1048 * for the format parameter in glTex(Sub)Image(). */
1050 if (format_is_alpha(src
)) {
1051 if (!format_is_alpha(dst
))
1054 src
= alpha_to_red(src
);
1055 dst
= alpha_to_red(dst
);
1056 } else if (format_is_luminance(src
)) {
1057 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1060 src
= util_format_luminance_to_red(src
);
1061 } else if (util_format_is_luminance_alpha(src
)) {
1062 src
= luminance_alpha_to_red_green(src
);
1064 if (format_is_red_alpha(dst
)) {
1065 dst
= red_alpha_to_red_green(dst
);
1066 } else if (!format_is_red(dst
))
1068 } else if (format_is_swizzled_rgba(src
)) {
1069 const struct util_format_description
*src_desc
= util_format_description(src
);
1070 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1074 /* Make sure the format is an RGBA and not an RGBX format */
1075 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1078 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1081 for (i
= 0; i
< 4; i
++)
1082 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1084 dst
= swizzle_format(dst
, swizzle
);
1085 if (dst
== PIPE_FORMAT_NONE
)
1088 src
= unswizzle_format(src
);
1097 try_pbo_upload_common(struct gl_context
*ctx
,
1098 struct pipe_surface
*surface
,
1099 const struct st_pbo_addresses
*addr
,
1100 enum pipe_format src_format
)
1102 struct st_context
*st
= st_context(ctx
);
1103 struct cso_context
*cso
= st
->cso_context
;
1104 struct pipe_context
*pipe
= st
->pipe
;
1105 bool success
= false;
1107 /* Create fragment shader */
1108 if (!st
->pbo
.upload_fs
) {
1109 st
->pbo
.upload_fs
= st_pbo_create_upload_fs(st
);
1110 if (!st
->pbo
.upload_fs
)
1114 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1115 CSO_BIT_FRAGMENT_SAMPLERS
|
1116 CSO_BIT_VERTEX_ELEMENTS
|
1117 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1118 CSO_BIT_FRAMEBUFFER
|
1121 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1122 CSO_BIT_RASTERIZER
|
1123 CSO_BIT_STREAM_OUTPUTS
|
1124 CSO_BIT_PAUSE_QUERIES
|
1125 CSO_BITS_ALL_SHADERS
));
1126 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1129 /* Set up the sampler_view */
1131 struct pipe_sampler_view templ
;
1132 struct pipe_sampler_view
*sampler_view
;
1133 struct pipe_sampler_state sampler
= {0};
1134 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
1136 memset(&templ
, 0, sizeof(templ
));
1137 templ
.target
= PIPE_BUFFER
;
1138 templ
.format
= src_format
;
1139 templ
.u
.buf
.first_element
= addr
->first_element
;
1140 templ
.u
.buf
.last_element
= addr
->last_element
;
1141 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1142 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1143 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1144 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1146 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1147 if (sampler_view
== NULL
)
1150 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1152 pipe_sampler_view_reference(&sampler_view
, NULL
);
1154 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
1157 /* Framebuffer_state */
1159 struct pipe_framebuffer_state fb
;
1160 memset(&fb
, 0, sizeof(fb
));
1161 fb
.width
= surface
->width
;
1162 fb
.height
= surface
->height
;
1164 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1166 cso_set_framebuffer(cso
, &fb
);
1168 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1171 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1174 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1176 /* Depth/stencil/alpha state */
1178 struct pipe_depth_stencil_alpha_state dsa
;
1179 memset(&dsa
, 0, sizeof(dsa
));
1180 cso_set_depth_stencil_alpha(cso
, &dsa
);
1183 /* Set up the fragment shader */
1184 cso_set_fragment_shader_handle(cso
, st
->pbo
.upload_fs
);
1186 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1189 cso_restore_state(cso
);
1190 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1196 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1197 struct gl_texture_image
*texImage
,
1198 GLenum format
, GLenum type
,
1199 enum pipe_format dst_format
,
1200 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1201 GLint width
, GLint height
, GLint depth
,
1203 const struct gl_pixelstore_attrib
*unpack
)
1205 struct st_context
*st
= st_context(ctx
);
1206 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1207 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1208 struct pipe_resource
*texture
= stImage
->pt
;
1209 struct pipe_context
*pipe
= st
->pipe
;
1210 struct pipe_screen
*screen
= pipe
->screen
;
1211 struct pipe_surface
*surface
= NULL
;
1212 struct st_pbo_addresses addr
;
1213 enum pipe_format src_format
;
1214 const struct util_format_description
*desc
;
1215 GLenum gl_target
= texImage
->TexObject
->Target
;
1218 if (!st
->pbo
.upload_enabled
)
1221 /* From now on, we need the gallium representation of dimensions. */
1222 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1229 if (depth
!= 1 && !st
->pbo
.layers
)
1232 /* Choose the source format. Initially, we do so without checking driver
1233 * support at all because of the remapping we later perform and because
1234 * at least the Radeon driver actually supports some formats for texture
1235 * buffers which it doesn't support for regular textures. */
1236 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1241 src_format
= util_format_linear(src_format
);
1242 desc
= util_format_description(src_format
);
1244 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1247 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1250 if (st
->pbo
.rgba_only
) {
1251 enum pipe_format orig_dst_format
= dst_format
;
1253 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1257 if (dst_format
!= orig_dst_format
&&
1258 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1259 PIPE_BIND_RENDER_TARGET
)) {
1265 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0,
1266 PIPE_BIND_SAMPLER_VIEW
)) {
1270 /* Compute buffer addresses */
1271 addr
.xoffset
= xoffset
;
1272 addr
.yoffset
= yoffset
;
1274 addr
.height
= height
;
1276 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1278 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1282 /* Set up the surface */
1284 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1285 unsigned max_layer
= util_max_layer(texture
, level
);
1287 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1289 struct pipe_surface templ
;
1290 memset(&templ
, 0, sizeof(templ
));
1291 templ
.format
= dst_format
;
1292 templ
.u
.tex
.level
= level
;
1293 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1294 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1296 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1301 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1303 pipe_surface_reference(&surface
, NULL
);
1309 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1310 struct gl_texture_image
*texImage
,
1311 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1312 GLint width
, GLint height
, GLint depth
,
1313 GLenum format
, GLenum type
, const void *pixels
,
1314 const struct gl_pixelstore_attrib
*unpack
)
1316 struct st_context
*st
= st_context(ctx
);
1317 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1318 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1319 struct pipe_context
*pipe
= st
->pipe
;
1320 struct pipe_screen
*screen
= pipe
->screen
;
1321 struct pipe_resource
*dst
= stImage
->pt
;
1322 struct pipe_resource
*src
= NULL
;
1323 struct pipe_resource src_templ
;
1324 struct pipe_transfer
*transfer
;
1325 struct pipe_blit_info blit
;
1326 enum pipe_format src_format
, dst_format
;
1327 mesa_format mesa_src_format
;
1328 GLenum gl_target
= texImage
->TexObject
->Target
;
1331 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1332 unsigned dst_level
= 0;
1334 st_flush_bitmap_cache(st
);
1335 st_invalidate_readpix_cache(st
);
1337 if (stObj
->pt
== stImage
->pt
)
1338 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1340 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1341 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1346 /* Try texture_subdata, which should be the fastest memcpy path. */
1348 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1349 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1350 texImage
->TexFormat
, format
, type
,
1352 struct pipe_box box
;
1353 unsigned stride
, layer_stride
;
1356 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1357 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1359 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1362 /* Convert to Gallium coordinates. */
1363 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1368 layer_stride
= stride
;
1371 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1372 pipe
->texture_subdata(pipe
, dst
, dst_level
, 0,
1373 &box
, data
, stride
, layer_stride
);
1377 if (!st
->prefer_blit_based_texture_transfer
) {
1381 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1382 * blit implementation in some drivers. */
1383 if (format
== GL_DEPTH_STENCIL
) {
1387 /* If the base internal format and the texture format don't match,
1388 * we can't use blit-based TexSubImage. */
1389 if (texImage
->_BaseFormat
!=
1390 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1395 /* See if the destination format is supported. */
1396 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1397 bind
= PIPE_BIND_DEPTH_STENCIL
;
1399 bind
= PIPE_BIND_RENDER_TARGET
;
1401 /* For luminance and intensity, only the red channel is stored
1402 * in the destination. */
1403 dst_format
= util_format_linear(dst
->format
);
1404 dst_format
= util_format_luminance_to_red(dst_format
);
1405 dst_format
= util_format_intensity_to_red(dst_format
);
1408 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1409 dst
->nr_samples
, bind
)) {
1413 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1414 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1415 xoffset
, yoffset
, zoffset
,
1416 width
, height
, depth
, pixels
, unpack
))
1420 /* See if the texture format already matches the format and type,
1421 * in which case the memcpy-based fast path will likely be used and
1422 * we don't have to blit. */
1423 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1424 type
, unpack
->SwapBytes
, NULL
)) {
1428 /* Choose the source format. */
1429 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1430 format
, type
, unpack
->SwapBytes
);
1435 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1437 /* There is no reason to do this if we cannot use memcpy for the temporary
1438 * source texture at least. This also takes transfer ops into account,
1440 if (!_mesa_texstore_can_use_memcpy(ctx
,
1441 _mesa_get_format_base_format(mesa_src_format
),
1442 mesa_src_format
, format
, type
, unpack
)) {
1446 /* TexSubImage only sets a single cubemap face. */
1447 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1448 gl_target
= GL_TEXTURE_2D
;
1450 /* TexSubImage can specify subsets of cube map array faces
1451 * so we need to upload via 2D array instead */
1452 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1453 gl_target
= GL_TEXTURE_2D_ARRAY
;
1456 /* Initialize the source texture description. */
1457 memset(&src_templ
, 0, sizeof(src_templ
));
1458 src_templ
.target
= gl_target_to_pipe(gl_target
);
1459 src_templ
.format
= src_format
;
1460 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1461 src_templ
.usage
= PIPE_USAGE_STAGING
;
1463 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1464 &src_templ
.width0
, &src_templ
.height0
,
1465 &src_templ
.depth0
, &src_templ
.array_size
);
1467 /* Check for NPOT texture support. */
1468 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1469 (!util_is_power_of_two(src_templ
.width0
) ||
1470 !util_is_power_of_two(src_templ
.height0
) ||
1471 !util_is_power_of_two(src_templ
.depth0
))) {
1475 /* Create the source texture. */
1476 src
= screen
->resource_create(screen
, &src_templ
);
1481 /* Map source pixels. */
1482 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1483 format
, type
, pixels
, unpack
,
1486 /* This is a GL error. */
1487 pipe_resource_reference(&src
, NULL
);
1491 /* From now on, we need the gallium representation of dimensions. */
1492 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1499 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1500 width
, height
, depth
, &transfer
);
1502 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1503 pipe_resource_reference(&src
, NULL
);
1507 /* Upload pixels (just memcpy). */
1509 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1512 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1513 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1514 /* 1D array textures.
1515 * We need to convert gallium coords to GL coords.
1517 void *src
= _mesa_image_address2d(unpack
, pixels
,
1518 width
, depth
, format
,
1520 memcpy(map
, src
, bytesPerRow
);
1523 ubyte
*slice_map
= map
;
1525 for (row
= 0; row
< (unsigned) height
; row
++) {
1526 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1527 width
, height
, format
,
1528 type
, slice
, row
, 0);
1529 memcpy(slice_map
, src
, bytesPerRow
);
1530 slice_map
+= transfer
->stride
;
1533 map
+= transfer
->layer_stride
;
1537 pipe_transfer_unmap(pipe
, transfer
);
1538 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1541 memset(&blit
, 0, sizeof(blit
));
1542 blit
.src
.resource
= src
;
1544 blit
.src
.format
= src_format
;
1545 blit
.dst
.resource
= dst
;
1546 blit
.dst
.level
= dst_level
;
1547 blit
.dst
.format
= dst_format
;
1548 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1549 blit
.dst
.box
.x
= xoffset
;
1550 blit
.dst
.box
.y
= yoffset
;
1551 blit
.dst
.box
.z
= zoffset
+ dstz
;
1552 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1553 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1554 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1555 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1556 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1557 blit
.scissor_enable
= FALSE
;
1559 st
->pipe
->blit(st
->pipe
, &blit
);
1561 pipe_resource_reference(&src
, NULL
);
1565 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1566 width
, height
, depth
, format
, type
, pixels
,
1571 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1572 struct gl_texture_image
*texImage
,
1573 GLenum format
, GLenum type
, const void *pixels
,
1574 const struct gl_pixelstore_attrib
*unpack
)
1576 assert(dims
== 1 || dims
== 2 || dims
== 3);
1578 prep_teximage(ctx
, texImage
, format
, type
);
1580 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1583 /* allocate storage for texture data */
1584 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1585 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1589 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1590 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1591 format
, type
, pixels
, unpack
);
1596 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1597 struct gl_texture_image
*texImage
,
1598 GLint x
, GLint y
, GLint z
,
1599 GLsizei w
, GLsizei h
, GLsizei d
,
1600 GLenum format
, GLsizei imageSize
, const void *data
)
1602 struct st_context
*st
= st_context(ctx
);
1603 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1604 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1605 struct pipe_resource
*texture
= stImage
->pt
;
1606 struct pipe_context
*pipe
= st
->pipe
;
1607 struct pipe_screen
*screen
= pipe
->screen
;
1608 struct pipe_resource
*dst
= stImage
->pt
;
1609 struct pipe_surface
*surface
= NULL
;
1610 struct compressed_pixelstore store
;
1611 struct st_pbo_addresses addr
;
1612 enum pipe_format copy_format
;
1614 intptr_t buf_offset
;
1615 bool success
= false;
1617 /* Check basic pre-conditions for PBO upload */
1618 if (!st
->prefer_blit_based_texture_transfer
) {
1622 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1625 if (st_etc_fallback(st
, texImage
)) {
1626 /* ETC isn't supported and is represented by uncompressed formats. */
1634 if (!st
->pbo
.upload_enabled
||
1635 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1639 /* Choose the pipe format for the upload. */
1640 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1641 bw
= util_format_get_blockwidth(dst
->format
);
1642 bh
= util_format_get_blockheight(dst
->format
);
1644 switch (addr
.bytes_per_pixel
) {
1646 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1649 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1655 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0,
1656 PIPE_BIND_SAMPLER_VIEW
)) {
1660 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1661 dst
->nr_samples
, PIPE_BIND_RENDER_TARGET
)) {
1665 /* Interpret the pixelstore settings. */
1666 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1667 &ctx
->Unpack
, &store
);
1668 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1669 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1671 /* Compute the offset into the buffer */
1672 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1674 if (buf_offset
% addr
.bytes_per_pixel
) {
1678 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1680 addr
.xoffset
= x
/ bw
;
1681 addr
.yoffset
= y
/ bh
;
1682 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1683 addr
.height
= store
.CopyRowsPerSlice
;
1685 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1686 addr
.image_height
= store
.TotalRowsPerSlice
;
1688 if (!st_pbo_addresses_setup(st
, st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1692 /* Set up the surface. */
1694 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1695 unsigned max_layer
= util_max_layer(texture
, level
);
1697 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1699 struct pipe_surface templ
;
1700 memset(&templ
, 0, sizeof(templ
));
1701 templ
.format
= copy_format
;
1702 templ
.u
.tex
.level
= level
;
1703 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1704 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1706 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1711 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1713 pipe_surface_reference(&surface
, NULL
);
1719 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1721 format
, imageSize
, data
);
1725 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1726 struct gl_texture_image
*texImage
,
1727 GLsizei imageSize
, const void *data
)
1729 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1731 /* only 2D and 3D compressed images are supported at this time */
1733 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1737 /* This is pretty simple, because unlike the general texstore path we don't
1738 * have to worry about the usual image unpacking or image transfer
1742 assert(texImage
->Width
> 0);
1743 assert(texImage
->Height
> 0);
1744 assert(texImage
->Depth
> 0);
1746 /* allocate storage for texture data */
1747 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1748 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1752 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1754 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1755 texImage
->TexFormat
,
1763 * Called via ctx->Driver.GetTexSubImage()
1765 * This uses a blit to copy the texture to a texture format which matches
1766 * the format and type combo and then a fast read-back is done using memcpy.
1767 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1768 * a format which matches the swizzling.
1770 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1772 * NOTE: Drivers usually do a blit to convert between tiled and linear
1773 * texture layouts during texture uploads/downloads, so the blit
1774 * we do here should be free in such cases.
1777 st_GetTexSubImage(struct gl_context
* ctx
,
1778 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1779 GLsizei width
, GLsizei height
, GLint depth
,
1780 GLenum format
, GLenum type
, void * pixels
,
1781 struct gl_texture_image
*texImage
)
1783 struct st_context
*st
= st_context(ctx
);
1784 struct pipe_context
*pipe
= st
->pipe
;
1785 struct pipe_screen
*screen
= pipe
->screen
;
1786 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1787 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1788 struct pipe_resource
*src
= stObj
->pt
;
1789 struct pipe_resource
*dst
= NULL
;
1790 struct pipe_resource dst_templ
;
1791 enum pipe_format dst_format
, src_format
;
1792 mesa_format mesa_format
;
1793 GLenum gl_target
= texImage
->TexObject
->Target
;
1794 enum pipe_texture_target pipe_target
;
1795 struct pipe_blit_info blit
;
1796 unsigned bind
= PIPE_BIND_TRANSFER_READ
;
1797 struct pipe_transfer
*tex_xfer
;
1799 boolean done
= FALSE
;
1801 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1802 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1804 st_flush_bitmap_cache(st
);
1806 if (!st
->prefer_blit_based_texture_transfer
&&
1807 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1808 /* Try to avoid the fallback if we're doing texture decompression here */
1812 /* Handle non-finalized textures. */
1813 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1817 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1818 * due to an incomplete stencil blit implementation in some drivers. */
1819 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1823 /* If the base internal format and the texture format don't match, we have
1824 * to fall back to _mesa_GetTexImage_sw. */
1825 if (texImage
->_BaseFormat
!=
1826 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1830 /* See if the texture format already matches the format and type,
1831 * in which case the memcpy-based fast path will be used. */
1832 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1833 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1837 /* Convert the source format to what is expected by GetTexImage
1838 * and see if it's supported.
1840 * This only applies to glGetTexImage:
1841 * - Luminance must be returned as (L,0,0,1).
1842 * - Luminance alpha must be returned as (L,0,0,A).
1843 * - Intensity must be returned as (I,0,0,1)
1845 if (stObj
->surface_based
)
1846 src_format
= util_format_linear(stObj
->surface_format
);
1848 src_format
= util_format_linear(src
->format
);
1849 src_format
= util_format_luminance_to_red(src_format
);
1850 src_format
= util_format_intensity_to_red(src_format
);
1853 !screen
->is_format_supported(screen
, src_format
, src
->target
,
1855 PIPE_BIND_SAMPLER_VIEW
)) {
1859 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1860 bind
|= PIPE_BIND_DEPTH_STENCIL
;
1862 bind
|= PIPE_BIND_RENDER_TARGET
;
1864 /* GetTexImage only returns a single face for cubemaps. */
1865 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1866 gl_target
= GL_TEXTURE_2D
;
1868 pipe_target
= gl_target_to_pipe(gl_target
);
1870 /* Choose the destination format by finding the best match
1871 * for the format+type combo. */
1872 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1873 ctx
->Pack
.SwapBytes
);
1875 if (dst_format
== PIPE_FORMAT_NONE
) {
1876 GLenum dst_glformat
;
1878 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1879 * where decompression with a blit is always preferred. */
1880 if (!util_format_is_compressed(src
->format
)) {
1884 /* Set the appropriate format for the decompressed texture.
1885 * Luminance and sRGB formats shouldn't appear here.*/
1886 switch (src_format
) {
1887 case PIPE_FORMAT_DXT1_RGB
:
1888 case PIPE_FORMAT_DXT1_RGBA
:
1889 case PIPE_FORMAT_DXT3_RGBA
:
1890 case PIPE_FORMAT_DXT5_RGBA
:
1891 case PIPE_FORMAT_RGTC1_UNORM
:
1892 case PIPE_FORMAT_RGTC2_UNORM
:
1893 case PIPE_FORMAT_ETC1_RGB8
:
1894 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1895 dst_glformat
= GL_RGBA8
;
1897 case PIPE_FORMAT_RGTC1_SNORM
:
1898 case PIPE_FORMAT_RGTC2_SNORM
:
1899 if (!ctx
->Extensions
.EXT_texture_snorm
)
1901 dst_glformat
= GL_RGBA8_SNORM
;
1903 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1904 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1905 if (!ctx
->Extensions
.ARB_texture_float
)
1907 dst_glformat
= GL_RGBA32F
;
1914 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
1915 pipe_target
, 0, bind
, FALSE
);
1917 if (dst_format
== PIPE_FORMAT_NONE
) {
1918 /* unable to get an rgba format!?! */
1923 /* create the destination texture of size (width X height X depth) */
1924 memset(&dst_templ
, 0, sizeof(dst_templ
));
1925 dst_templ
.target
= pipe_target
;
1926 dst_templ
.format
= dst_format
;
1927 dst_templ
.bind
= bind
;
1928 dst_templ
.usage
= PIPE_USAGE_STAGING
;
1930 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1931 &dst_templ
.width0
, &dst_templ
.height0
,
1932 &dst_templ
.depth0
, &dst_templ
.array_size
);
1934 dst
= screen
->resource_create(screen
, &dst_templ
);
1939 /* From now on, we need the gallium representation of dimensions. */
1940 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1947 assert(texImage
->Face
== 0 ||
1948 texImage
->TexObject
->MinLayer
== 0 ||
1951 memset(&blit
, 0, sizeof(blit
));
1952 blit
.src
.resource
= src
;
1953 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
1954 blit
.src
.format
= src_format
;
1955 blit
.dst
.resource
= dst
;
1957 blit
.dst
.format
= dst
->format
;
1958 blit
.src
.box
.x
= xoffset
;
1960 blit
.src
.box
.y
= yoffset
;
1962 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
1964 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1965 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1966 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1967 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
1968 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1969 blit
.scissor_enable
= FALSE
;
1971 /* blit/render/decompress */
1972 st
->pipe
->blit(st
->pipe
, &blit
);
1974 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
1976 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
1977 0, 0, 0, width
, height
, depth
, &tex_xfer
);
1982 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
1984 /* copy/pack data into user buffer */
1985 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
1986 ctx
->Pack
.SwapBytes
, NULL
)) {
1988 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
1991 for (slice
= 0; slice
< depth
; slice
++) {
1992 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1993 /* 1D array textures.
1994 * We need to convert gallium coords to GL coords.
1996 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1997 width
, depth
, format
,
1999 memcpy(dest
, map
, bytesPerRow
);
2002 ubyte
*slice_map
= map
;
2004 for (row
= 0; row
< height
; row
++) {
2005 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2006 width
, height
, format
,
2007 type
, slice
, row
, 0);
2008 memcpy(dest
, slice_map
, bytesPerRow
);
2009 slice_map
+= tex_xfer
->stride
;
2012 map
+= tex_xfer
->layer_stride
;
2016 /* format translation via floats */
2019 uint32_t dstMesaFormat
;
2020 int dstStride
, srcStride
;
2022 assert(util_format_is_compressed(src
->format
));
2024 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
2029 if (ST_DEBUG
& DEBUG_FALLBACK
)
2030 debug_printf("%s: fallback format translation\n", __func__
);
2032 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2033 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2034 srcStride
= 4 * width
* sizeof(GLfloat
);
2035 for (slice
= 0; slice
< depth
; slice
++) {
2036 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2037 /* 1D array textures.
2038 * We need to convert gallium coords to GL coords.
2040 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2041 width
, depth
, format
,
2044 /* get float[4] rgba row from surface */
2045 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, 1,
2048 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2049 rgba
, RGBA32_FLOAT
, srcStride
,
2053 for (row
= 0; row
< height
; row
++) {
2054 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2055 width
, height
, format
,
2056 type
, slice
, row
, 0);
2058 /* get float[4] rgba row from surface */
2059 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, row
, width
, 1,
2062 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2063 rgba
, RGBA32_FLOAT
, srcStride
,
2067 map
+= tex_xfer
->layer_stride
;
2076 pipe_transfer_unmap(pipe
, tex_xfer
);
2078 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2079 pipe_resource_reference(&dst
, NULL
);
2083 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2084 width
, height
, depth
,
2085 format
, type
, pixels
, texImage
);
2091 * Do a CopyTexSubImage operation using a read transfer from the source,
2092 * a write transfer to the destination and get_tile()/put_tile() to access
2093 * the pixels/texels.
2095 * Note: srcY=0=TOP of renderbuffer
2098 fallback_copy_texsubimage(struct gl_context
*ctx
,
2099 struct st_renderbuffer
*strb
,
2100 struct st_texture_image
*stImage
,
2102 GLint destX
, GLint destY
, GLint slice
,
2103 GLint srcX
, GLint srcY
,
2104 GLsizei width
, GLsizei height
)
2106 struct st_context
*st
= st_context(ctx
);
2107 struct pipe_context
*pipe
= st
->pipe
;
2108 struct pipe_transfer
*src_trans
;
2110 enum pipe_transfer_usage transfer_usage
;
2112 unsigned dst_width
= width
;
2113 unsigned dst_height
= height
;
2114 unsigned dst_depth
= 1;
2115 struct pipe_transfer
*transfer
;
2117 if (ST_DEBUG
& DEBUG_FALLBACK
)
2118 debug_printf("%s: fallback processing\n", __func__
);
2120 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2121 srcY
= strb
->Base
.Height
- srcY
- height
;
2124 map
= pipe_transfer_map(pipe
,
2126 strb
->surface
->u
.tex
.level
,
2127 strb
->surface
->u
.tex
.first_layer
,
2130 width
, height
, &src_trans
);
2132 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2133 baseFormat
== GL_DEPTH_STENCIL
) &&
2134 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2135 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2137 transfer_usage
= PIPE_TRANSFER_WRITE
;
2139 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2140 destX
, destY
, slice
,
2141 dst_width
, dst_height
, dst_depth
,
2144 if (baseFormat
== GL_DEPTH_COMPONENT
||
2145 baseFormat
== GL_DEPTH_STENCIL
) {
2146 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2147 ctx
->Pixel
.DepthBias
!= 0.0F
);
2151 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2152 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2161 data
= malloc(width
* sizeof(uint
));
2164 /* To avoid a large temp memory allocation, do copy row by row */
2165 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2166 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2168 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2171 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2172 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2173 0, 0, width
, 1, data
);
2176 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2181 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2189 malloc(width
* height
* 4 * sizeof(GLfloat
));
2191 if (tempSrc
&& texDest
) {
2192 const GLint dims
= 2;
2194 struct gl_texture_image
*texImage
= &stImage
->base
;
2195 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2197 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2198 unpack
.Invert
= GL_TRUE
;
2201 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2202 dstRowStride
= transfer
->layer_stride
;
2205 dstRowStride
= transfer
->stride
;
2208 /* get float/RGBA image from framebuffer */
2209 /* XXX this usually involves a lot of int/float conversion.
2210 * try to avoid that someday.
2212 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2213 util_format_linear(strb
->texture
->format
),
2216 /* Store into texture memory.
2217 * Note that this does some special things such as pixel transfer
2218 * ops and format conversion. In particular, if the dest tex format
2219 * is actually RGBA but the user created the texture as GL_RGB we
2220 * need to fill-in/override the alpha channel with 1.0.
2222 _mesa_texstore(ctx
, dims
,
2223 texImage
->_BaseFormat
,
2224 texImage
->TexFormat
,
2228 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2232 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2238 st_texture_image_unmap(st
, stImage
, slice
);
2239 pipe
->transfer_unmap(pipe
, src_trans
);
2244 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2245 * Note that the region to copy has already been clipped so we know we
2246 * won't read from outside the source renderbuffer's bounds.
2248 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2251 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2252 struct gl_texture_image
*texImage
,
2253 GLint destX
, GLint destY
, GLint slice
,
2254 struct gl_renderbuffer
*rb
,
2255 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2257 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2258 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2259 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2260 struct st_context
*st
= st_context(ctx
);
2261 struct pipe_context
*pipe
= st
->pipe
;
2262 struct pipe_screen
*screen
= pipe
->screen
;
2263 struct pipe_blit_info blit
;
2264 enum pipe_format dst_format
;
2265 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2269 st_flush_bitmap_cache(st
);
2270 st_invalidate_readpix_cache(st
);
2272 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2273 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2275 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2276 debug_printf("%s: null strb or stImage\n", __func__
);
2280 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2281 texImage
->TexFormat
)) {
2285 /* The base internal format must match the mesa format, so make sure
2286 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
2288 if (texImage
->_BaseFormat
!=
2289 _mesa_get_format_base_format(texImage
->TexFormat
) ||
2290 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
2294 /* Choose the destination format to match the TexImage behavior. */
2295 dst_format
= util_format_linear(stImage
->pt
->format
);
2296 dst_format
= util_format_luminance_to_red(dst_format
);
2297 dst_format
= util_format_intensity_to_red(dst_format
);
2299 /* See if the destination format is supported. */
2300 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2301 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2302 bind
= PIPE_BIND_DEPTH_STENCIL
;
2305 bind
= PIPE_BIND_RENDER_TARGET
;
2309 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2310 stImage
->pt
->nr_samples
, bind
)) {
2314 /* Y flipping for the main framebuffer. */
2316 srcY1
= strb
->Base
.Height
- srcY
- height
;
2317 srcY0
= srcY1
+ height
;
2321 srcY1
= srcY0
+ height
;
2324 /* Blit the texture.
2325 * This supports flipping, format conversions, and downsampling.
2327 memset(&blit
, 0, sizeof(blit
));
2328 blit
.src
.resource
= strb
->texture
;
2329 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2330 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2331 blit
.src
.box
.x
= srcX
;
2332 blit
.src
.box
.y
= srcY0
;
2333 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2334 blit
.src
.box
.width
= width
;
2335 blit
.src
.box
.height
= srcY1
- srcY0
;
2336 blit
.src
.box
.depth
= 1;
2337 blit
.dst
.resource
= stImage
->pt
;
2338 blit
.dst
.format
= dst_format
;
2339 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2340 blit
.dst
.box
.x
= destX
;
2341 blit
.dst
.box
.y
= destY
;
2342 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2343 blit
.dst
.box
.width
= width
;
2344 blit
.dst
.box
.height
= height
;
2345 blit
.dst
.box
.depth
= 1;
2346 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2347 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2348 pipe
->blit(pipe
, &blit
);
2352 /* software fallback */
2353 fallback_copy_texsubimage(ctx
,
2354 strb
, stImage
, texImage
->_BaseFormat
,
2355 destX
, destY
, slice
,
2356 srcX
, srcY
, width
, height
);
2361 * Copy image data from stImage into the texture object 'stObj' at level
2365 copy_image_data_to_texture(struct st_context
*st
,
2366 struct st_texture_object
*stObj
,
2368 struct st_texture_image
*stImage
)
2372 const struct gl_texture_image
*dstImage
=
2373 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2375 assert(dstImage
->Width
== stImage
->base
.Width
);
2376 assert(dstImage
->Height
== stImage
->base
.Height
);
2377 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2381 /* Copy potentially with the blitter:
2384 if (stImage
->pt
->last_level
== 0)
2387 src_level
= stImage
->base
.Level
;
2389 assert(src_level
<= stImage
->pt
->last_level
);
2390 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2391 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2392 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2393 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2394 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2395 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2397 st_texture_image_copy(st
->pipe
,
2398 stObj
->pt
, dstLevel
, /* dest texture, level */
2399 stImage
->pt
, src_level
, /* src texture, level */
2400 stImage
->base
.Face
);
2402 pipe_resource_reference(&stImage
->pt
, NULL
);
2404 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2409 * Called during state validation. When this function is finished,
2410 * the texture object should be ready for rendering.
2411 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2414 st_finalize_texture(struct gl_context
*ctx
,
2415 struct pipe_context
*pipe
,
2416 struct gl_texture_object
*tObj
)
2418 struct st_context
*st
= st_context(ctx
);
2419 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2420 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2422 const struct st_texture_image
*firstImage
;
2423 enum pipe_format firstImageFormat
;
2424 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2426 if (tObj
->Immutable
)
2429 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
2430 /* The texture is complete and we know exactly how many mipmap levels
2431 * are present/needed. This is conditional because we may be called
2432 * from the st_generate_mipmap() function when the texture object is
2433 * incomplete. In that case, we'll have set stObj->lastLevel before
2436 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
2437 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
2438 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2440 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2443 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
2444 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
2447 pipe_resource_reference(&stObj
->pt
, NULL
);
2448 st_texture_release_all_sampler_views(st
, stObj
);
2452 if (st_obj
->buffer
!= stObj
->pt
) {
2453 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
2454 st_texture_release_all_sampler_views(st
, stObj
);
2460 firstImage
= st_texture_image_const(_mesa_base_tex_image(&stObj
->base
));
2463 /* If both firstImage and stObj point to a texture which can contain
2464 * all active images, favour firstImage. Note that because of the
2465 * completeness requirement, we know that the image dimensions
2468 if (firstImage
->pt
&&
2469 firstImage
->pt
!= stObj
->pt
&&
2470 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2471 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2472 st_texture_release_all_sampler_views(st
, stObj
);
2475 /* If this texture comes from a window system, there is nothing else to do. */
2476 if (stObj
->surface_based
) {
2480 /* Find gallium format for the Mesa texture */
2482 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2484 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2486 GLuint width
, height
, depth
;
2488 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2489 firstImage
->base
.Width2
,
2490 firstImage
->base
.Height2
,
2491 firstImage
->base
.Depth2
,
2492 &width
, &height
, &depth
, &ptLayers
);
2494 /* If we previously allocated a pipe texture and its sizes are
2495 * compatible, use them.
2498 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2499 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2500 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2501 ptWidth
= stObj
->pt
->width0
;
2502 ptHeight
= stObj
->pt
->height0
;
2503 ptDepth
= stObj
->pt
->depth0
;
2505 /* Otherwise, compute a new level=0 size that is compatible with the
2508 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2509 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2510 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2512 /* If the base level image is 1x1x1, we still need to ensure that the
2513 * resulting pipe texture ends up with the required number of levels
2516 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2517 ptWidth
<<= firstImage
->base
.Level
;
2519 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2520 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2525 ptNumSamples
= firstImage
->base
.NumSamples
;
2528 /* If we already have a gallium texture, check that it matches the texture
2529 * object's format, target, size, num_levels, etc.
2532 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2533 stObj
->pt
->format
!= firstImageFormat
||
2534 stObj
->pt
->last_level
< stObj
->lastLevel
||
2535 stObj
->pt
->width0
!= ptWidth
||
2536 stObj
->pt
->height0
!= ptHeight
||
2537 stObj
->pt
->depth0
!= ptDepth
||
2538 stObj
->pt
->nr_samples
!= ptNumSamples
||
2539 stObj
->pt
->array_size
!= ptLayers
)
2541 /* The gallium texture does not match the Mesa texture so delete the
2542 * gallium texture now. We'll make a new one below.
2544 pipe_resource_reference(&stObj
->pt
, NULL
);
2545 st_texture_release_all_sampler_views(st
, stObj
);
2546 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
2550 /* May need to create a new gallium texture:
2553 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2555 stObj
->pt
= st_texture_create(st
,
2556 gl_target_to_pipe(stObj
->base
.Target
),
2562 ptLayers
, ptNumSamples
,
2566 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2571 /* Pull in any images not in the object's texture:
2573 for (face
= 0; face
< nr_faces
; face
++) {
2575 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2576 struct st_texture_image
*stImage
=
2577 st_texture_image(stObj
->base
.Image
[face
][level
]);
2579 /* Need to import images in main memory or held in other textures.
2581 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2585 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2586 height
= u_minify(ptHeight
, level
);
2590 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2591 depth
= u_minify(ptDepth
, level
);
2592 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2598 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2599 stImage
->base
.Height
== height
&&
2600 stImage
->base
.Depth
== depth
)) {
2601 /* src image fits expected dest mipmap level size */
2602 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2613 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2614 * for a whole mipmap stack.
2617 st_AllocTextureStorage(struct gl_context
*ctx
,
2618 struct gl_texture_object
*texObj
,
2619 GLsizei levels
, GLsizei width
,
2620 GLsizei height
, GLsizei depth
)
2622 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2623 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2624 struct st_context
*st
= st_context(ctx
);
2625 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2626 struct pipe_screen
*screen
= st
->pipe
->screen
;
2627 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
2628 enum pipe_format fmt
;
2630 GLuint num_samples
= texImage
->NumSamples
;
2634 stObj
->lastLevel
= levels
- 1;
2636 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2638 bindings
= default_bindings(st
, fmt
);
2640 /* Raise the sample count if the requested one is unsupported. */
2641 if (num_samples
> 1) {
2642 boolean found
= FALSE
;
2644 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2645 if (screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
2647 PIPE_BIND_SAMPLER_VIEW
)) {
2648 /* Update the sample count in gl_texture_image as well. */
2649 texImage
->NumSamples
= num_samples
;
2660 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2661 width
, height
, depth
,
2662 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2664 stObj
->pt
= st_texture_create(st
,
2665 gl_target_to_pipe(texObj
->Target
),
2671 ptLayers
, num_samples
,
2676 /* Set image resource pointers */
2677 for (level
= 0; level
< levels
; level
++) {
2679 for (face
= 0; face
< numFaces
; face
++) {
2680 struct st_texture_image
*stImage
=
2681 st_texture_image(texObj
->Image
[face
][level
]);
2682 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2691 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2692 GLuint numLevels
, GLint level
,
2693 mesa_format format
, GLuint numSamples
,
2694 GLint width
, GLint height
, GLint depth
)
2696 struct st_context
*st
= st_context(ctx
);
2697 struct pipe_context
*pipe
= st
->pipe
;
2699 if (width
== 0 || height
== 0 || depth
== 0) {
2700 /* zero-sized images are legal, and always fit! */
2704 if (pipe
->screen
->can_create_resource
) {
2705 /* Ask the gallium driver if the texture is too large */
2706 struct gl_texture_object
*texObj
=
2707 _mesa_get_current_tex_object(ctx
, target
);
2708 struct pipe_resource pt
;
2710 /* Setup the pipe_resource object
2712 memset(&pt
, 0, sizeof(pt
));
2714 pt
.target
= gl_target_to_pipe(target
);
2715 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
2716 pt
.nr_samples
= numSamples
;
2718 st_gl_texture_dims_to_pipe_dims(target
,
2719 width
, height
, depth
,
2720 &pt
.width0
, &pt
.height0
,
2721 &pt
.depth0
, &pt
.array_size
);
2723 if (numLevels
> 0) {
2724 /* For immutable textures we know the final number of mip levels */
2725 pt
.last_level
= numLevels
- 1;
2727 else if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
2728 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
2729 /* assume just one mipmap level */
2733 /* assume a full set of mipmaps */
2734 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
2737 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
2740 /* Use core Mesa fallback */
2741 return _mesa_test_proxy_teximage(ctx
, target
, numLevels
, level
, format
,
2742 numSamples
, width
, height
, depth
);
2747 st_TextureView(struct gl_context
*ctx
,
2748 struct gl_texture_object
*texObj
,
2749 struct gl_texture_object
*origTexObj
)
2751 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
2752 struct st_texture_object
*tex
= st_texture_object(texObj
);
2753 struct gl_texture_image
*image
= texObj
->Image
[0][0];
2755 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2756 const int numLevels
= texObj
->NumLevels
;
2761 pipe_resource_reference(&tex
->pt
, orig
->pt
);
2763 /* Set image resource pointers */
2764 for (level
= 0; level
< numLevels
; level
++) {
2765 for (face
= 0; face
< numFaces
; face
++) {
2766 struct st_texture_image
*stImage
=
2767 st_texture_image(texObj
->Image
[face
][level
]);
2768 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
2772 tex
->surface_based
= GL_TRUE
;
2773 tex
->surface_format
=
2774 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
2776 tex
->lastLevel
= numLevels
- 1;
2782 st_ClearTexSubImage(struct gl_context
*ctx
,
2783 struct gl_texture_image
*texImage
,
2784 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2785 GLsizei width
, GLsizei height
, GLsizei depth
,
2786 const void *clearValue
)
2788 static const char zeros
[16] = {0};
2789 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2790 struct pipe_resource
*pt
= stImage
->pt
;
2791 struct st_context
*st
= st_context(ctx
);
2792 struct pipe_context
*pipe
= st
->pipe
;
2793 unsigned level
= texImage
->Level
;
2794 struct pipe_box box
;
2799 st_flush_bitmap_cache(st
);
2800 st_invalidate_readpix_cache(st
);
2802 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
2803 width
, height
, depth
, &box
);
2804 if (texImage
->TexObject
->Immutable
) {
2805 level
+= texImage
->TexObject
->MinLevel
;
2806 box
.z
+= texImage
->TexObject
->MinLayer
;
2809 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
2813 st_init_texture_functions(struct dd_function_table
*functions
)
2815 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
2816 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
2817 functions
->TexImage
= st_TexImage
;
2818 functions
->TexSubImage
= st_TexSubImage
;
2819 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
2820 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
2821 functions
->GenerateMipmap
= st_generate_mipmap
;
2823 functions
->GetTexSubImage
= st_GetTexSubImage
;
2825 /* compressed texture functions */
2826 functions
->CompressedTexImage
= st_CompressedTexImage
;
2827 functions
->GetCompressedTexSubImage
= _mesa_GetCompressedTexSubImage_sw
;
2829 functions
->NewTextureObject
= st_NewTextureObject
;
2830 functions
->NewTextureImage
= st_NewTextureImage
;
2831 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
2832 functions
->DeleteTexture
= st_DeleteTextureObject
;
2833 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
2834 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
2835 functions
->MapTextureImage
= st_MapTextureImage
;
2836 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
2838 /* XXX Temporary until we can query pipe's texture sizes */
2839 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
2841 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
2842 functions
->TextureView
= st_TextureView
;
2843 functions
->ClearTexSubImage
= st_ClearTexSubImage
;