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;
191 if (stImage
->etc_data
) {
192 free(stImage
->etc_data
);
193 stImage
->etc_data
= NULL
;
198 st_etc_fallback(struct st_context
*st
, struct gl_texture_image
*texImage
)
200 return (_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
201 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
);
205 etc_fallback_allocate(struct st_context
*st
, struct st_texture_image
*stImage
)
207 struct gl_texture_image
*texImage
= &stImage
->base
;
209 if (!st_etc_fallback(st
, texImage
))
212 if (stImage
->etc_data
)
213 free(stImage
->etc_data
);
215 unsigned data_size
= _mesa_format_image_size(texImage
->TexFormat
,
221 malloc(data_size
* _mesa_num_tex_faces(texImage
->TexObject
->Target
));
224 /** called via ctx->Driver.MapTextureImage() */
226 st_MapTextureImage(struct gl_context
*ctx
,
227 struct gl_texture_image
*texImage
,
228 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
230 GLubyte
**mapOut
, GLint
*rowStrideOut
)
232 struct st_context
*st
= st_context(ctx
);
233 struct st_texture_image
*stImage
= st_texture_image(texImage
);
236 struct pipe_transfer
*transfer
;
239 if (mode
& GL_MAP_READ_BIT
)
240 pipeMode
|= PIPE_TRANSFER_READ
;
241 if (mode
& GL_MAP_WRITE_BIT
)
242 pipeMode
|= PIPE_TRANSFER_WRITE
;
243 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
244 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
246 map
= st_texture_image_map(st
, stImage
, pipeMode
, x
, y
, slice
, w
, h
, 1,
249 if (st_etc_fallback(st
, texImage
)) {
250 /* ETC isn't supported by all gallium drivers, where it's represented
251 * by uncompressed formats. We store the compressed data (as it's
252 * needed for image copies in OES_copy_image), and decompress as
253 * necessary in Unmap.
255 * Note: all ETC1/ETC2 formats have 4x4 block sizes.
257 unsigned z
= transfer
->box
.z
;
258 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
260 unsigned bytes
= _mesa_get_format_bytes(texImage
->TexFormat
);
261 unsigned stride
= *rowStrideOut
= itransfer
->temp_stride
=
262 _mesa_format_row_stride(texImage
->TexFormat
, texImage
->Width2
);
263 *mapOut
= itransfer
->temp_data
=
264 stImage
->etc_data
+ ((x
/ 4) * bytes
+ (y
/ 4) * stride
) +
265 z
* stride
* texImage
->Height2
/ 4;
266 itransfer
->map
= map
;
269 /* supported mapping */
271 *rowStrideOut
= transfer
->stride
;
281 /** called via ctx->Driver.UnmapTextureImage() */
283 st_UnmapTextureImage(struct gl_context
*ctx
,
284 struct gl_texture_image
*texImage
,
287 struct st_context
*st
= st_context(ctx
);
288 struct st_texture_image
*stImage
= st_texture_image(texImage
);
290 if (st_etc_fallback(st
, texImage
)) {
291 /* Decompress the ETC texture to the mapped one. */
292 unsigned z
= slice
+ stImage
->base
.Face
;
293 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
294 struct pipe_transfer
*transfer
= itransfer
->transfer
;
296 assert(z
== transfer
->box
.z
);
298 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
299 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
300 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
301 itransfer
->temp_data
,
302 itransfer
->temp_stride
,
303 transfer
->box
.width
, transfer
->box
.height
);
306 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
307 itransfer
->temp_data
, itransfer
->temp_stride
,
308 transfer
->box
.width
, transfer
->box
.height
,
309 texImage
->TexFormat
);
313 itransfer
->temp_data
= NULL
;
314 itransfer
->temp_stride
= 0;
318 st_texture_image_unmap(st
, stImage
, slice
);
323 * Return default texture resource binding bitmask for the given format.
326 default_bindings(struct st_context
*st
, enum pipe_format format
)
328 struct pipe_screen
*screen
= st
->pipe
->screen
;
329 const unsigned target
= PIPE_TEXTURE_2D
;
332 if (util_format_is_depth_or_stencil(format
))
333 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
335 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
337 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
341 format
= util_format_linear(format
);
343 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
346 return PIPE_BIND_SAMPLER_VIEW
;
352 * Given the size of a mipmap image, try to compute the size of the level=0
355 * Note that this isn't always accurate for odd-sized, non-POW textures.
356 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
358 * \return GL_TRUE for success, GL_FALSE for failure
361 guess_base_level_size(GLenum target
,
362 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
363 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
370 /* Guess the size of the base level.
371 * Depending on the image's size, we can't always make a guess here.
375 case GL_TEXTURE_1D_ARRAY
:
380 case GL_TEXTURE_2D_ARRAY
:
381 /* We can't make a good guess here, because the base level dimensions
384 if (width
== 1 || height
== 1) {
391 case GL_TEXTURE_CUBE_MAP
:
392 case GL_TEXTURE_CUBE_MAP_ARRAY
:
398 /* We can't make a good guess here, because the base level dimensions
401 if (width
== 1 || height
== 1 || depth
== 1) {
409 case GL_TEXTURE_RECTANGLE
:
426 * Try to determine whether we should allocate memory for a full texture
427 * mipmap. The problem is when we get a glTexImage(level=0) call, we
428 * can't immediately know if other mipmap levels are coming next. Here
429 * we try to guess whether to allocate memory for a mipmap or just the
432 * If we guess incorrectly here we'll later reallocate the right amount of
433 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
435 * \param stObj the texture object we're going to allocate memory for.
436 * \param stImage describes the incoming image which we need to store.
439 allocate_full_mipmap(const struct st_texture_object
*stObj
,
440 const struct st_texture_image
*stImage
)
442 switch (stObj
->base
.Target
) {
443 case GL_TEXTURE_RECTANGLE_NV
:
444 case GL_TEXTURE_BUFFER
:
445 case GL_TEXTURE_EXTERNAL_OES
:
446 case GL_TEXTURE_2D_MULTISAMPLE
:
447 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
448 /* these texture types cannot be mipmapped */
452 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
455 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
456 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
457 /* depth/stencil textures are seldom mipmapped */
460 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
463 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
464 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
465 /* not a mipmap minification filter */
468 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
469 /* 3D textures are seldom mipmapped */
477 * Try to allocate a pipe_resource object for the given st_texture_object.
479 * We use the given st_texture_image as a clue to determine the size of the
480 * mipmap image at level=0.
482 * \return GL_TRUE for success, GL_FALSE if out of memory.
485 guess_and_alloc_texture(struct st_context
*st
,
486 struct st_texture_object
*stObj
,
487 const struct st_texture_image
*stImage
)
489 const struct gl_texture_image
*firstImage
;
490 GLuint lastLevel
, width
, height
, depth
;
492 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
493 enum pipe_format fmt
;
494 bool guessed_box
= false;
496 DBG("%s\n", __func__
);
500 /* If a base level image with compatible size exists, use that as our guess.
502 firstImage
= _mesa_base_tex_image(&stObj
->base
);
504 firstImage
->Width2
> 0 &&
505 firstImage
->Height2
> 0 &&
506 firstImage
->Depth2
> 0 &&
507 guess_base_level_size(stObj
->base
.Target
,
512 &width
, &height
, &depth
)) {
513 if (stImage
->base
.Width2
== u_minify(width
, stImage
->base
.Level
) &&
514 stImage
->base
.Height2
== u_minify(height
, stImage
->base
.Level
) &&
515 stImage
->base
.Depth2
== u_minify(depth
, stImage
->base
.Level
))
520 guessed_box
= guess_base_level_size(stObj
->base
.Target
,
521 stImage
->base
.Width2
,
522 stImage
->base
.Height2
,
523 stImage
->base
.Depth2
,
525 &width
, &height
, &depth
);
528 /* we can't determine the image size at level=0 */
529 /* this is not an out of memory error */
533 /* At this point, (width x height x depth) is the expected size of
534 * the level=0 mipmap image.
537 /* Guess a reasonable value for lastLevel. With OpenGL we have no
538 * idea how many mipmap levels will be in a texture until we start
539 * to render with it. Make an educated guess here but be prepared
540 * to re-allocating a texture buffer with space for more (or fewer)
541 * mipmap levels later.
543 if (allocate_full_mipmap(stObj
, stImage
)) {
544 /* alloc space for a full mipmap */
545 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
546 width
, height
, depth
) - 1;
549 /* only alloc space for a single mipmap level */
553 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
555 bindings
= default_bindings(st
, fmt
);
557 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
558 width
, height
, depth
,
559 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
561 stObj
->pt
= st_texture_create(st
,
562 gl_target_to_pipe(stObj
->base
.Target
),
571 stObj
->lastLevel
= lastLevel
;
573 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
575 return stObj
->pt
!= NULL
;
580 * Called via ctx->Driver.AllocTextureImageBuffer().
581 * If the texture object/buffer already has space for the indicated image,
582 * we're done. Otherwise, allocate memory for the new texture image.
585 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
586 struct gl_texture_image
*texImage
)
588 struct st_context
*st
= st_context(ctx
);
589 struct st_texture_image
*stImage
= st_texture_image(texImage
);
590 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
591 const GLuint level
= texImage
->Level
;
592 GLuint width
= texImage
->Width
;
593 GLuint height
= texImage
->Height
;
594 GLuint depth
= texImage
->Depth
;
596 DBG("%s\n", __func__
);
598 assert(!stImage
->pt
); /* xxx this might be wrong */
600 etc_fallback_allocate(st
, stImage
);
602 /* Look if the parent texture object has space for this image */
604 level
<= stObj
->pt
->last_level
&&
605 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
606 /* this image will fit in the existing texture object's memory */
607 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
611 /* The parent texture object does not have space for this image */
613 pipe_resource_reference(&stObj
->pt
, NULL
);
614 st_texture_release_all_sampler_views(st
, stObj
);
616 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
617 /* Probably out of memory.
618 * Try flushing any pending rendering, then retry.
621 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
622 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
628 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
629 /* The image will live in the object's mipmap memory */
630 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
635 /* Create a new, temporary texture/resource/buffer to hold this
636 * one texture image. Note that when we later access this image
637 * (either for mapping or copying) we'll want to always specify
638 * mipmap level=0, even if the image represents some other mipmap
641 enum pipe_format format
=
642 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
643 GLuint bindings
= default_bindings(st
, format
);
644 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
646 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
647 width
, height
, depth
,
648 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
650 stImage
->pt
= st_texture_create(st
,
651 gl_target_to_pipe(stObj
->base
.Target
),
659 return stImage
->pt
!= NULL
;
665 * Preparation prior to glTexImage. Basically check the 'surface_based'
666 * field and switch to a "normal" tex image if necessary.
669 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
670 GLenum format
, GLenum type
)
672 struct gl_texture_object
*texObj
= texImage
->TexObject
;
673 struct st_texture_object
*stObj
= st_texture_object(texObj
);
675 /* switch to "normal" */
676 if (stObj
->surface_based
) {
677 const GLenum target
= texObj
->Target
;
678 const GLuint level
= texImage
->Level
;
679 mesa_format texFormat
;
681 _mesa_clear_texture_object(ctx
, texObj
);
682 pipe_resource_reference(&stObj
->pt
, NULL
);
684 /* oops, need to init this image again */
685 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
686 texImage
->InternalFormat
, format
,
689 _mesa_init_teximage_fields(ctx
, texImage
,
690 texImage
->Width
, texImage
->Height
,
691 texImage
->Depth
, texImage
->Border
,
692 texImage
->InternalFormat
, texFormat
);
694 stObj
->surface_based
= GL_FALSE
;
700 * Return a writemask for the gallium blit. The parameters can be base
701 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
704 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
707 case GL_DEPTH_STENCIL
:
709 case GL_DEPTH_STENCIL
:
711 case GL_DEPTH_COMPONENT
:
713 case GL_STENCIL_INDEX
:
720 case GL_DEPTH_COMPONENT
:
722 case GL_DEPTH_STENCIL
:
723 case GL_DEPTH_COMPONENT
:
730 case GL_STENCIL_INDEX
:
732 case GL_STENCIL_INDEX
:
740 return PIPE_MASK_RGBA
;
745 * Converts format to a format with the same components, types
746 * and sizes, but with the components in RGBA order.
748 static enum pipe_format
749 unswizzle_format(enum pipe_format format
)
753 case PIPE_FORMAT_B8G8R8A8_UNORM
:
754 case PIPE_FORMAT_A8R8G8B8_UNORM
:
755 case PIPE_FORMAT_A8B8G8R8_UNORM
:
756 return PIPE_FORMAT_R8G8B8A8_UNORM
;
758 case PIPE_FORMAT_B10G10R10A2_UNORM
:
759 return PIPE_FORMAT_R10G10B10A2_UNORM
;
761 case PIPE_FORMAT_B10G10R10A2_SNORM
:
762 return PIPE_FORMAT_R10G10B10A2_SNORM
;
764 case PIPE_FORMAT_B10G10R10A2_UINT
:
765 return PIPE_FORMAT_R10G10B10A2_UINT
;
773 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
775 static enum pipe_format
776 alpha_to_red(enum pipe_format format
)
780 case PIPE_FORMAT_A8_UNORM
:
781 return PIPE_FORMAT_R8_UNORM
;
782 case PIPE_FORMAT_A8_SNORM
:
783 return PIPE_FORMAT_R8_SNORM
;
784 case PIPE_FORMAT_A8_UINT
:
785 return PIPE_FORMAT_R8_UINT
;
786 case PIPE_FORMAT_A8_SINT
:
787 return PIPE_FORMAT_R8_SINT
;
789 case PIPE_FORMAT_A16_UNORM
:
790 return PIPE_FORMAT_R16_UNORM
;
791 case PIPE_FORMAT_A16_SNORM
:
792 return PIPE_FORMAT_R16_SNORM
;
793 case PIPE_FORMAT_A16_UINT
:
794 return PIPE_FORMAT_R16_UINT
;
795 case PIPE_FORMAT_A16_SINT
:
796 return PIPE_FORMAT_R16_SINT
;
797 case PIPE_FORMAT_A16_FLOAT
:
798 return PIPE_FORMAT_R16_FLOAT
;
800 case PIPE_FORMAT_A32_UINT
:
801 return PIPE_FORMAT_R32_UINT
;
802 case PIPE_FORMAT_A32_SINT
:
803 return PIPE_FORMAT_R32_SINT
;
804 case PIPE_FORMAT_A32_FLOAT
:
805 return PIPE_FORMAT_R32_FLOAT
;
813 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
815 static enum pipe_format
816 red_alpha_to_red_green(enum pipe_format format
)
820 case PIPE_FORMAT_R8A8_UNORM
:
821 return PIPE_FORMAT_R8G8_UNORM
;
822 case PIPE_FORMAT_R8A8_SNORM
:
823 return PIPE_FORMAT_R8G8_SNORM
;
824 case PIPE_FORMAT_R8A8_UINT
:
825 return PIPE_FORMAT_R8G8_UINT
;
826 case PIPE_FORMAT_R8A8_SINT
:
827 return PIPE_FORMAT_R8G8_SINT
;
829 case PIPE_FORMAT_R16A16_UNORM
:
830 return PIPE_FORMAT_R16G16_UNORM
;
831 case PIPE_FORMAT_R16A16_SNORM
:
832 return PIPE_FORMAT_R16G16_SNORM
;
833 case PIPE_FORMAT_R16A16_UINT
:
834 return PIPE_FORMAT_R16G16_UINT
;
835 case PIPE_FORMAT_R16A16_SINT
:
836 return PIPE_FORMAT_R16G16_SINT
;
837 case PIPE_FORMAT_R16A16_FLOAT
:
838 return PIPE_FORMAT_R16G16_FLOAT
;
840 case PIPE_FORMAT_R32A32_UINT
:
841 return PIPE_FORMAT_R32G32_UINT
;
842 case PIPE_FORMAT_R32A32_SINT
:
843 return PIPE_FORMAT_R32G32_SINT
;
844 case PIPE_FORMAT_R32A32_FLOAT
:
845 return PIPE_FORMAT_R32G32_FLOAT
;
853 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
855 static enum pipe_format
856 luminance_alpha_to_red_green(enum pipe_format format
)
860 case PIPE_FORMAT_L8A8_UNORM
:
861 return PIPE_FORMAT_R8G8_UNORM
;
862 case PIPE_FORMAT_L8A8_SNORM
:
863 return PIPE_FORMAT_R8G8_SNORM
;
864 case PIPE_FORMAT_L8A8_UINT
:
865 return PIPE_FORMAT_R8G8_UINT
;
866 case PIPE_FORMAT_L8A8_SINT
:
867 return PIPE_FORMAT_R8G8_SINT
;
869 case PIPE_FORMAT_L16A16_UNORM
:
870 return PIPE_FORMAT_R16G16_UNORM
;
871 case PIPE_FORMAT_L16A16_SNORM
:
872 return PIPE_FORMAT_R16G16_SNORM
;
873 case PIPE_FORMAT_L16A16_UINT
:
874 return PIPE_FORMAT_R16G16_UINT
;
875 case PIPE_FORMAT_L16A16_SINT
:
876 return PIPE_FORMAT_R16G16_SINT
;
877 case PIPE_FORMAT_L16A16_FLOAT
:
878 return PIPE_FORMAT_R16G16_FLOAT
;
880 case PIPE_FORMAT_L32A32_UINT
:
881 return PIPE_FORMAT_R32G32_UINT
;
882 case PIPE_FORMAT_L32A32_SINT
:
883 return PIPE_FORMAT_R32G32_SINT
;
884 case PIPE_FORMAT_L32A32_FLOAT
:
885 return PIPE_FORMAT_R32G32_FLOAT
;
893 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
896 format_is_alpha(enum pipe_format format
)
898 const struct util_format_description
*desc
= util_format_description(format
);
900 if (desc
->nr_channels
== 1 &&
901 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
902 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
903 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
904 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
911 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
914 format_is_red(enum pipe_format format
)
916 const struct util_format_description
*desc
= util_format_description(format
);
918 if (desc
->nr_channels
== 1 &&
919 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
920 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
921 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
922 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
930 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
933 format_is_luminance(enum pipe_format format
)
935 const struct util_format_description
*desc
= util_format_description(format
);
937 if (desc
->nr_channels
== 1 &&
938 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
939 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
940 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
941 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
948 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
951 format_is_red_alpha(enum pipe_format format
)
953 const struct util_format_description
*desc
= util_format_description(format
);
955 if (desc
->nr_channels
== 2 &&
956 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
957 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
958 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
959 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
966 format_is_swizzled_rgba(enum pipe_format format
)
968 const struct util_format_description
*desc
= util_format_description(format
);
970 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
971 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
972 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
973 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
981 unsigned char swizzle
[4];
982 enum pipe_format format
;
985 static const struct format_table table_8888_unorm
[] = {
986 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
987 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
988 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
989 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
992 static const struct format_table table_1010102_unorm
[] = {
993 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
994 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
997 static const struct format_table table_1010102_snorm
[] = {
998 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
999 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
1002 static const struct format_table table_1010102_uint
[] = {
1003 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
1004 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
1007 static enum pipe_format
1008 swizzle_format(enum pipe_format format
, const int * const swizzle
)
1013 case PIPE_FORMAT_R8G8B8A8_UNORM
:
1014 case PIPE_FORMAT_B8G8R8A8_UNORM
:
1015 case PIPE_FORMAT_A8R8G8B8_UNORM
:
1016 case PIPE_FORMAT_A8B8G8R8_UNORM
:
1017 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
1018 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
1019 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
1020 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
1021 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
1022 return table_8888_unorm
[i
].format
;
1026 case PIPE_FORMAT_R10G10B10A2_UNORM
:
1027 case PIPE_FORMAT_B10G10R10A2_UNORM
:
1028 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
1029 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
1030 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
1031 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
1032 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
1033 return table_1010102_unorm
[i
].format
;
1037 case PIPE_FORMAT_R10G10B10A2_SNORM
:
1038 case PIPE_FORMAT_B10G10R10A2_SNORM
:
1039 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
1040 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
1041 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
1042 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
1043 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
1044 return table_1010102_snorm
[i
].format
;
1048 case PIPE_FORMAT_R10G10B10A2_UINT
:
1049 case PIPE_FORMAT_B10G10R10A2_UINT
:
1050 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1051 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1052 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1053 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1054 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1055 return table_1010102_uint
[i
].format
;
1063 return PIPE_FORMAT_NONE
;
1067 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1069 enum pipe_format src
= *src_format
;
1070 enum pipe_format dst
= *dst_format
;
1072 /* Note: dst_format has already been transformed from luminance/intensity
1073 * to red when this function is called. The source format will never
1074 * be an intensity format, because GL_INTENSITY is not a legal value
1075 * for the format parameter in glTex(Sub)Image(). */
1077 if (format_is_alpha(src
)) {
1078 if (!format_is_alpha(dst
))
1081 src
= alpha_to_red(src
);
1082 dst
= alpha_to_red(dst
);
1083 } else if (format_is_luminance(src
)) {
1084 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1087 src
= util_format_luminance_to_red(src
);
1088 } else if (util_format_is_luminance_alpha(src
)) {
1089 src
= luminance_alpha_to_red_green(src
);
1091 if (format_is_red_alpha(dst
)) {
1092 dst
= red_alpha_to_red_green(dst
);
1093 } else if (!format_is_red(dst
))
1095 } else if (format_is_swizzled_rgba(src
)) {
1096 const struct util_format_description
*src_desc
= util_format_description(src
);
1097 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1101 /* Make sure the format is an RGBA and not an RGBX format */
1102 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1105 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1108 for (i
= 0; i
< 4; i
++)
1109 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1111 dst
= swizzle_format(dst
, swizzle
);
1112 if (dst
== PIPE_FORMAT_NONE
)
1115 src
= unswizzle_format(src
);
1124 try_pbo_upload_common(struct gl_context
*ctx
,
1125 struct pipe_surface
*surface
,
1126 const struct st_pbo_addresses
*addr
,
1127 enum pipe_format src_format
)
1129 struct st_context
*st
= st_context(ctx
);
1130 struct cso_context
*cso
= st
->cso_context
;
1131 struct pipe_context
*pipe
= st
->pipe
;
1132 bool success
= false;
1134 /* Create fragment shader */
1135 if (!st
->pbo
.upload_fs
) {
1136 st
->pbo
.upload_fs
= st_pbo_create_upload_fs(st
);
1137 if (!st
->pbo
.upload_fs
)
1141 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1142 CSO_BIT_FRAGMENT_SAMPLERS
|
1143 CSO_BIT_VERTEX_ELEMENTS
|
1144 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1145 CSO_BIT_FRAMEBUFFER
|
1148 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1149 CSO_BIT_RASTERIZER
|
1150 CSO_BIT_STREAM_OUTPUTS
|
1151 CSO_BIT_PAUSE_QUERIES
|
1152 CSO_BITS_ALL_SHADERS
));
1153 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1156 /* Set up the sampler_view */
1158 struct pipe_sampler_view templ
;
1159 struct pipe_sampler_view
*sampler_view
;
1160 struct pipe_sampler_state sampler
= {0};
1161 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
1163 memset(&templ
, 0, sizeof(templ
));
1164 templ
.target
= PIPE_BUFFER
;
1165 templ
.format
= src_format
;
1166 templ
.u
.buf
.first_element
= addr
->first_element
;
1167 templ
.u
.buf
.last_element
= addr
->last_element
;
1168 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1169 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1170 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1171 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1173 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1174 if (sampler_view
== NULL
)
1177 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1179 pipe_sampler_view_reference(&sampler_view
, NULL
);
1181 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
1184 /* Framebuffer_state */
1186 struct pipe_framebuffer_state fb
;
1187 memset(&fb
, 0, sizeof(fb
));
1188 fb
.width
= surface
->width
;
1189 fb
.height
= surface
->height
;
1191 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1193 cso_set_framebuffer(cso
, &fb
);
1195 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1198 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1201 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1203 /* Depth/stencil/alpha state */
1205 struct pipe_depth_stencil_alpha_state dsa
;
1206 memset(&dsa
, 0, sizeof(dsa
));
1207 cso_set_depth_stencil_alpha(cso
, &dsa
);
1210 /* Set up the fragment shader */
1211 cso_set_fragment_shader_handle(cso
, st
->pbo
.upload_fs
);
1213 success
= st_pbo_draw(st
, addr
, surface
->width
, surface
->height
);
1216 cso_restore_state(cso
);
1217 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1223 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1224 struct gl_texture_image
*texImage
,
1225 GLenum format
, GLenum type
,
1226 enum pipe_format dst_format
,
1227 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1228 GLint width
, GLint height
, GLint depth
,
1230 const struct gl_pixelstore_attrib
*unpack
)
1232 struct st_context
*st
= st_context(ctx
);
1233 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1234 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1235 struct pipe_resource
*texture
= stImage
->pt
;
1236 struct pipe_context
*pipe
= st
->pipe
;
1237 struct pipe_screen
*screen
= pipe
->screen
;
1238 struct pipe_surface
*surface
= NULL
;
1239 struct st_pbo_addresses addr
;
1240 enum pipe_format src_format
;
1241 const struct util_format_description
*desc
;
1242 GLenum gl_target
= texImage
->TexObject
->Target
;
1245 if (!st
->pbo
.upload_enabled
)
1248 /* From now on, we need the gallium representation of dimensions. */
1249 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1256 if (depth
!= 1 && !st
->pbo
.layers
)
1259 /* Choose the source format. Initially, we do so without checking driver
1260 * support at all because of the remapping we later perform and because
1261 * at least the Radeon driver actually supports some formats for texture
1262 * buffers which it doesn't support for regular textures. */
1263 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1268 src_format
= util_format_linear(src_format
);
1269 desc
= util_format_description(src_format
);
1271 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1274 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1277 if (st
->pbo
.rgba_only
) {
1278 enum pipe_format orig_dst_format
= dst_format
;
1280 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1284 if (dst_format
!= orig_dst_format
&&
1285 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1286 PIPE_BIND_RENDER_TARGET
)) {
1292 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0,
1293 PIPE_BIND_SAMPLER_VIEW
)) {
1297 /* Compute buffer addresses */
1298 addr
.xoffset
= xoffset
;
1299 addr
.yoffset
= yoffset
;
1301 addr
.height
= height
;
1303 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1305 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1309 /* Set up the surface */
1311 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1312 unsigned max_layer
= util_max_layer(texture
, level
);
1314 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1316 struct pipe_surface templ
;
1317 memset(&templ
, 0, sizeof(templ
));
1318 templ
.format
= dst_format
;
1319 templ
.u
.tex
.level
= level
;
1320 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1321 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1323 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1328 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1330 pipe_surface_reference(&surface
, NULL
);
1336 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1337 struct gl_texture_image
*texImage
,
1338 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1339 GLint width
, GLint height
, GLint depth
,
1340 GLenum format
, GLenum type
, const void *pixels
,
1341 const struct gl_pixelstore_attrib
*unpack
)
1343 struct st_context
*st
= st_context(ctx
);
1344 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1345 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1346 struct pipe_context
*pipe
= st
->pipe
;
1347 struct pipe_screen
*screen
= pipe
->screen
;
1348 struct pipe_resource
*dst
= stImage
->pt
;
1349 struct pipe_resource
*src
= NULL
;
1350 struct pipe_resource src_templ
;
1351 struct pipe_transfer
*transfer
;
1352 struct pipe_blit_info blit
;
1353 enum pipe_format src_format
, dst_format
;
1354 mesa_format mesa_src_format
;
1355 GLenum gl_target
= texImage
->TexObject
->Target
;
1358 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1359 unsigned dst_level
= 0;
1361 st_flush_bitmap_cache(st
);
1362 st_invalidate_readpix_cache(st
);
1364 if (stObj
->pt
== stImage
->pt
)
1365 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1367 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1368 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1373 /* Try texture_subdata, which should be the fastest memcpy path. */
1375 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1376 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1377 texImage
->TexFormat
, format
, type
,
1379 struct pipe_box box
;
1380 unsigned stride
, layer_stride
;
1383 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1384 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1386 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1389 /* Convert to Gallium coordinates. */
1390 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1395 layer_stride
= stride
;
1398 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1399 pipe
->texture_subdata(pipe
, dst
, dst_level
, 0,
1400 &box
, data
, stride
, layer_stride
);
1404 if (!st
->prefer_blit_based_texture_transfer
) {
1408 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1409 * blit implementation in some drivers. */
1410 if (format
== GL_DEPTH_STENCIL
) {
1414 /* If the base internal format and the texture format don't match,
1415 * we can't use blit-based TexSubImage. */
1416 if (texImage
->_BaseFormat
!=
1417 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1422 /* See if the destination format is supported. */
1423 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1424 bind
= PIPE_BIND_DEPTH_STENCIL
;
1426 bind
= PIPE_BIND_RENDER_TARGET
;
1428 /* For luminance and intensity, only the red channel is stored
1429 * in the destination. */
1430 dst_format
= util_format_linear(dst
->format
);
1431 dst_format
= util_format_luminance_to_red(dst_format
);
1432 dst_format
= util_format_intensity_to_red(dst_format
);
1435 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1436 dst
->nr_samples
, bind
)) {
1440 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1441 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1442 xoffset
, yoffset
, zoffset
,
1443 width
, height
, depth
, pixels
, unpack
))
1447 /* See if the texture format already matches the format and type,
1448 * in which case the memcpy-based fast path will likely be used and
1449 * we don't have to blit. */
1450 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1451 type
, unpack
->SwapBytes
, NULL
)) {
1455 /* Choose the source format. */
1456 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1457 format
, type
, unpack
->SwapBytes
);
1462 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1464 /* There is no reason to do this if we cannot use memcpy for the temporary
1465 * source texture at least. This also takes transfer ops into account,
1467 if (!_mesa_texstore_can_use_memcpy(ctx
,
1468 _mesa_get_format_base_format(mesa_src_format
),
1469 mesa_src_format
, format
, type
, unpack
)) {
1473 /* TexSubImage only sets a single cubemap face. */
1474 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1475 gl_target
= GL_TEXTURE_2D
;
1477 /* TexSubImage can specify subsets of cube map array faces
1478 * so we need to upload via 2D array instead */
1479 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1480 gl_target
= GL_TEXTURE_2D_ARRAY
;
1483 /* Initialize the source texture description. */
1484 memset(&src_templ
, 0, sizeof(src_templ
));
1485 src_templ
.target
= gl_target_to_pipe(gl_target
);
1486 src_templ
.format
= src_format
;
1487 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1488 src_templ
.usage
= PIPE_USAGE_STAGING
;
1490 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1491 &src_templ
.width0
, &src_templ
.height0
,
1492 &src_templ
.depth0
, &src_templ
.array_size
);
1494 /* Check for NPOT texture support. */
1495 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1496 (!util_is_power_of_two(src_templ
.width0
) ||
1497 !util_is_power_of_two(src_templ
.height0
) ||
1498 !util_is_power_of_two(src_templ
.depth0
))) {
1502 /* Create the source texture. */
1503 src
= screen
->resource_create(screen
, &src_templ
);
1508 /* Map source pixels. */
1509 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1510 format
, type
, pixels
, unpack
,
1513 /* This is a GL error. */
1514 pipe_resource_reference(&src
, NULL
);
1518 /* From now on, we need the gallium representation of dimensions. */
1519 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1526 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1527 width
, height
, depth
, &transfer
);
1529 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1530 pipe_resource_reference(&src
, NULL
);
1534 /* Upload pixels (just memcpy). */
1536 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1539 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1540 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1541 /* 1D array textures.
1542 * We need to convert gallium coords to GL coords.
1544 void *src
= _mesa_image_address2d(unpack
, pixels
,
1545 width
, depth
, format
,
1547 memcpy(map
, src
, bytesPerRow
);
1550 ubyte
*slice_map
= map
;
1552 for (row
= 0; row
< (unsigned) height
; row
++) {
1553 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1554 width
, height
, format
,
1555 type
, slice
, row
, 0);
1556 memcpy(slice_map
, src
, bytesPerRow
);
1557 slice_map
+= transfer
->stride
;
1560 map
+= transfer
->layer_stride
;
1564 pipe_transfer_unmap(pipe
, transfer
);
1565 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1568 memset(&blit
, 0, sizeof(blit
));
1569 blit
.src
.resource
= src
;
1571 blit
.src
.format
= src_format
;
1572 blit
.dst
.resource
= dst
;
1573 blit
.dst
.level
= dst_level
;
1574 blit
.dst
.format
= dst_format
;
1575 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1576 blit
.dst
.box
.x
= xoffset
;
1577 blit
.dst
.box
.y
= yoffset
;
1578 blit
.dst
.box
.z
= zoffset
+ dstz
;
1579 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1580 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1581 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1582 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1583 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1584 blit
.scissor_enable
= FALSE
;
1586 st
->pipe
->blit(st
->pipe
, &blit
);
1588 pipe_resource_reference(&src
, NULL
);
1592 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1593 width
, height
, depth
, format
, type
, pixels
,
1598 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1599 struct gl_texture_image
*texImage
,
1600 GLenum format
, GLenum type
, const void *pixels
,
1601 const struct gl_pixelstore_attrib
*unpack
)
1603 assert(dims
== 1 || dims
== 2 || dims
== 3);
1605 prep_teximage(ctx
, texImage
, format
, type
);
1607 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1610 /* allocate storage for texture data */
1611 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1612 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1616 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1617 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1618 format
, type
, pixels
, unpack
);
1623 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1624 struct gl_texture_image
*texImage
,
1625 GLint x
, GLint y
, GLint z
,
1626 GLsizei w
, GLsizei h
, GLsizei d
,
1627 GLenum format
, GLsizei imageSize
, const void *data
)
1629 struct st_context
*st
= st_context(ctx
);
1630 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1631 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1632 struct pipe_resource
*texture
= stImage
->pt
;
1633 struct pipe_context
*pipe
= st
->pipe
;
1634 struct pipe_screen
*screen
= pipe
->screen
;
1635 struct pipe_resource
*dst
= stImage
->pt
;
1636 struct pipe_surface
*surface
= NULL
;
1637 struct compressed_pixelstore store
;
1638 struct st_pbo_addresses addr
;
1639 enum pipe_format copy_format
;
1641 intptr_t buf_offset
;
1642 bool success
= false;
1644 /* Check basic pre-conditions for PBO upload */
1645 if (!st
->prefer_blit_based_texture_transfer
) {
1649 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1652 if (st_etc_fallback(st
, texImage
)) {
1653 /* ETC isn't supported and is represented by uncompressed formats. */
1661 if (!st
->pbo
.upload_enabled
||
1662 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1666 /* Choose the pipe format for the upload. */
1667 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1668 bw
= util_format_get_blockwidth(dst
->format
);
1669 bh
= util_format_get_blockheight(dst
->format
);
1671 switch (addr
.bytes_per_pixel
) {
1673 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1676 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1682 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0,
1683 PIPE_BIND_SAMPLER_VIEW
)) {
1687 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1688 dst
->nr_samples
, PIPE_BIND_RENDER_TARGET
)) {
1692 /* Interpret the pixelstore settings. */
1693 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1694 &ctx
->Unpack
, &store
);
1695 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1696 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1698 /* Compute the offset into the buffer */
1699 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1701 if (buf_offset
% addr
.bytes_per_pixel
) {
1705 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1707 addr
.xoffset
= x
/ bw
;
1708 addr
.yoffset
= y
/ bh
;
1709 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1710 addr
.height
= store
.CopyRowsPerSlice
;
1712 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1713 addr
.image_height
= store
.TotalRowsPerSlice
;
1715 if (!st_pbo_addresses_setup(st
, st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1719 /* Set up the surface. */
1721 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1722 unsigned max_layer
= util_max_layer(texture
, level
);
1724 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1726 struct pipe_surface templ
;
1727 memset(&templ
, 0, sizeof(templ
));
1728 templ
.format
= copy_format
;
1729 templ
.u
.tex
.level
= level
;
1730 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1731 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1733 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1738 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1740 pipe_surface_reference(&surface
, NULL
);
1746 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1748 format
, imageSize
, data
);
1752 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1753 struct gl_texture_image
*texImage
,
1754 GLsizei imageSize
, const void *data
)
1756 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1758 /* only 2D and 3D compressed images are supported at this time */
1760 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1764 /* This is pretty simple, because unlike the general texstore path we don't
1765 * have to worry about the usual image unpacking or image transfer
1769 assert(texImage
->Width
> 0);
1770 assert(texImage
->Height
> 0);
1771 assert(texImage
->Depth
> 0);
1773 /* allocate storage for texture data */
1774 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1775 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1779 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1781 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1782 texImage
->TexFormat
,
1790 * Called via ctx->Driver.GetTexSubImage()
1792 * This uses a blit to copy the texture to a texture format which matches
1793 * the format and type combo and then a fast read-back is done using memcpy.
1794 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1795 * a format which matches the swizzling.
1797 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1799 * NOTE: Drivers usually do a blit to convert between tiled and linear
1800 * texture layouts during texture uploads/downloads, so the blit
1801 * we do here should be free in such cases.
1804 st_GetTexSubImage(struct gl_context
* ctx
,
1805 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1806 GLsizei width
, GLsizei height
, GLint depth
,
1807 GLenum format
, GLenum type
, void * pixels
,
1808 struct gl_texture_image
*texImage
)
1810 struct st_context
*st
= st_context(ctx
);
1811 struct pipe_context
*pipe
= st
->pipe
;
1812 struct pipe_screen
*screen
= pipe
->screen
;
1813 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1814 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1815 struct pipe_resource
*src
= stObj
->pt
;
1816 struct pipe_resource
*dst
= NULL
;
1817 struct pipe_resource dst_templ
;
1818 enum pipe_format dst_format
, src_format
;
1819 mesa_format mesa_format
;
1820 GLenum gl_target
= texImage
->TexObject
->Target
;
1821 enum pipe_texture_target pipe_target
;
1822 struct pipe_blit_info blit
;
1823 unsigned bind
= PIPE_BIND_TRANSFER_READ
;
1824 struct pipe_transfer
*tex_xfer
;
1826 boolean done
= FALSE
;
1828 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1829 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1831 st_flush_bitmap_cache(st
);
1833 if (!st
->prefer_blit_based_texture_transfer
&&
1834 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1835 /* Try to avoid the fallback if we're doing texture decompression here */
1839 /* Handle non-finalized textures. */
1840 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1844 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1845 * due to an incomplete stencil blit implementation in some drivers. */
1846 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1850 /* If the base internal format and the texture format don't match, we have
1851 * to fall back to _mesa_GetTexImage_sw. */
1852 if (texImage
->_BaseFormat
!=
1853 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1857 /* See if the texture format already matches the format and type,
1858 * in which case the memcpy-based fast path will be used. */
1859 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1860 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1864 /* Convert the source format to what is expected by GetTexImage
1865 * and see if it's supported.
1867 * This only applies to glGetTexImage:
1868 * - Luminance must be returned as (L,0,0,1).
1869 * - Luminance alpha must be returned as (L,0,0,A).
1870 * - Intensity must be returned as (I,0,0,1)
1872 if (stObj
->surface_based
)
1873 src_format
= util_format_linear(stObj
->surface_format
);
1875 src_format
= util_format_linear(src
->format
);
1876 src_format
= util_format_luminance_to_red(src_format
);
1877 src_format
= util_format_intensity_to_red(src_format
);
1880 !screen
->is_format_supported(screen
, src_format
, src
->target
,
1882 PIPE_BIND_SAMPLER_VIEW
)) {
1886 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1887 bind
|= PIPE_BIND_DEPTH_STENCIL
;
1889 bind
|= PIPE_BIND_RENDER_TARGET
;
1891 /* GetTexImage only returns a single face for cubemaps. */
1892 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1893 gl_target
= GL_TEXTURE_2D
;
1895 pipe_target
= gl_target_to_pipe(gl_target
);
1897 /* Choose the destination format by finding the best match
1898 * for the format+type combo. */
1899 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1900 ctx
->Pack
.SwapBytes
);
1902 if (dst_format
== PIPE_FORMAT_NONE
) {
1903 GLenum dst_glformat
;
1905 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1906 * where decompression with a blit is always preferred. */
1907 if (!util_format_is_compressed(src
->format
)) {
1911 /* Set the appropriate format for the decompressed texture.
1912 * Luminance and sRGB formats shouldn't appear here.*/
1913 switch (src_format
) {
1914 case PIPE_FORMAT_DXT1_RGB
:
1915 case PIPE_FORMAT_DXT1_RGBA
:
1916 case PIPE_FORMAT_DXT3_RGBA
:
1917 case PIPE_FORMAT_DXT5_RGBA
:
1918 case PIPE_FORMAT_RGTC1_UNORM
:
1919 case PIPE_FORMAT_RGTC2_UNORM
:
1920 case PIPE_FORMAT_ETC1_RGB8
:
1921 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1922 dst_glformat
= GL_RGBA8
;
1924 case PIPE_FORMAT_RGTC1_SNORM
:
1925 case PIPE_FORMAT_RGTC2_SNORM
:
1926 if (!ctx
->Extensions
.EXT_texture_snorm
)
1928 dst_glformat
= GL_RGBA8_SNORM
;
1930 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1931 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1932 if (!ctx
->Extensions
.ARB_texture_float
)
1934 dst_glformat
= GL_RGBA32F
;
1941 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
1942 pipe_target
, 0, bind
, FALSE
);
1944 if (dst_format
== PIPE_FORMAT_NONE
) {
1945 /* unable to get an rgba format!?! */
1950 /* create the destination texture of size (width X height X depth) */
1951 memset(&dst_templ
, 0, sizeof(dst_templ
));
1952 dst_templ
.target
= pipe_target
;
1953 dst_templ
.format
= dst_format
;
1954 dst_templ
.bind
= bind
;
1955 dst_templ
.usage
= PIPE_USAGE_STAGING
;
1957 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1958 &dst_templ
.width0
, &dst_templ
.height0
,
1959 &dst_templ
.depth0
, &dst_templ
.array_size
);
1961 dst
= screen
->resource_create(screen
, &dst_templ
);
1966 /* From now on, we need the gallium representation of dimensions. */
1967 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1974 assert(texImage
->Face
== 0 ||
1975 texImage
->TexObject
->MinLayer
== 0 ||
1978 memset(&blit
, 0, sizeof(blit
));
1979 blit
.src
.resource
= src
;
1980 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
1981 blit
.src
.format
= src_format
;
1982 blit
.dst
.resource
= dst
;
1984 blit
.dst
.format
= dst
->format
;
1985 blit
.src
.box
.x
= xoffset
;
1987 blit
.src
.box
.y
= yoffset
;
1989 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
1991 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1992 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1993 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1994 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
1995 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1996 blit
.scissor_enable
= FALSE
;
1998 /* blit/render/decompress */
1999 st
->pipe
->blit(st
->pipe
, &blit
);
2001 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2003 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2004 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2009 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2011 /* copy/pack data into user buffer */
2012 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2013 ctx
->Pack
.SwapBytes
, NULL
)) {
2015 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2018 for (slice
= 0; slice
< depth
; slice
++) {
2019 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2020 /* 1D array textures.
2021 * We need to convert gallium coords to GL coords.
2023 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2024 width
, depth
, format
,
2026 memcpy(dest
, map
, bytesPerRow
);
2029 ubyte
*slice_map
= map
;
2031 for (row
= 0; row
< height
; row
++) {
2032 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2033 width
, height
, format
,
2034 type
, slice
, row
, 0);
2035 memcpy(dest
, slice_map
, bytesPerRow
);
2036 slice_map
+= tex_xfer
->stride
;
2039 map
+= tex_xfer
->layer_stride
;
2043 /* format translation via floats */
2046 uint32_t dstMesaFormat
;
2047 int dstStride
, srcStride
;
2049 assert(util_format_is_compressed(src
->format
));
2051 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
2056 if (ST_DEBUG
& DEBUG_FALLBACK
)
2057 debug_printf("%s: fallback format translation\n", __func__
);
2059 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2060 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2061 srcStride
= 4 * width
* sizeof(GLfloat
);
2062 for (slice
= 0; slice
< depth
; slice
++) {
2063 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2064 /* 1D array textures.
2065 * We need to convert gallium coords to GL coords.
2067 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2068 width
, depth
, format
,
2071 /* get float[4] rgba row from surface */
2072 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, 1,
2075 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2076 rgba
, RGBA32_FLOAT
, srcStride
,
2080 for (row
= 0; row
< height
; row
++) {
2081 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2082 width
, height
, format
,
2083 type
, slice
, row
, 0);
2085 /* get float[4] rgba row from surface */
2086 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, row
, width
, 1,
2089 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2090 rgba
, RGBA32_FLOAT
, srcStride
,
2094 map
+= tex_xfer
->layer_stride
;
2103 pipe_transfer_unmap(pipe
, tex_xfer
);
2105 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2106 pipe_resource_reference(&dst
, NULL
);
2110 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2111 width
, height
, depth
,
2112 format
, type
, pixels
, texImage
);
2118 * Do a CopyTexSubImage operation using a read transfer from the source,
2119 * a write transfer to the destination and get_tile()/put_tile() to access
2120 * the pixels/texels.
2122 * Note: srcY=0=TOP of renderbuffer
2125 fallback_copy_texsubimage(struct gl_context
*ctx
,
2126 struct st_renderbuffer
*strb
,
2127 struct st_texture_image
*stImage
,
2129 GLint destX
, GLint destY
, GLint slice
,
2130 GLint srcX
, GLint srcY
,
2131 GLsizei width
, GLsizei height
)
2133 struct st_context
*st
= st_context(ctx
);
2134 struct pipe_context
*pipe
= st
->pipe
;
2135 struct pipe_transfer
*src_trans
;
2137 enum pipe_transfer_usage transfer_usage
;
2139 unsigned dst_width
= width
;
2140 unsigned dst_height
= height
;
2141 unsigned dst_depth
= 1;
2142 struct pipe_transfer
*transfer
;
2144 if (ST_DEBUG
& DEBUG_FALLBACK
)
2145 debug_printf("%s: fallback processing\n", __func__
);
2147 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2148 srcY
= strb
->Base
.Height
- srcY
- height
;
2151 map
= pipe_transfer_map(pipe
,
2153 strb
->surface
->u
.tex
.level
,
2154 strb
->surface
->u
.tex
.first_layer
,
2157 width
, height
, &src_trans
);
2159 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2160 baseFormat
== GL_DEPTH_STENCIL
) &&
2161 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2162 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2164 transfer_usage
= PIPE_TRANSFER_WRITE
;
2166 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2167 destX
, destY
, slice
,
2168 dst_width
, dst_height
, dst_depth
,
2171 if (baseFormat
== GL_DEPTH_COMPONENT
||
2172 baseFormat
== GL_DEPTH_STENCIL
) {
2173 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2174 ctx
->Pixel
.DepthBias
!= 0.0F
);
2178 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2179 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2188 data
= malloc(width
* sizeof(uint
));
2191 /* To avoid a large temp memory allocation, do copy row by row */
2192 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2193 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2195 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2198 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2199 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2200 0, 0, width
, 1, data
);
2203 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2208 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2216 malloc(width
* height
* 4 * sizeof(GLfloat
));
2218 if (tempSrc
&& texDest
) {
2219 const GLint dims
= 2;
2221 struct gl_texture_image
*texImage
= &stImage
->base
;
2222 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2224 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2225 unpack
.Invert
= GL_TRUE
;
2228 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2229 dstRowStride
= transfer
->layer_stride
;
2232 dstRowStride
= transfer
->stride
;
2235 /* get float/RGBA image from framebuffer */
2236 /* XXX this usually involves a lot of int/float conversion.
2237 * try to avoid that someday.
2239 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2240 util_format_linear(strb
->texture
->format
),
2243 /* Store into texture memory.
2244 * Note that this does some special things such as pixel transfer
2245 * ops and format conversion. In particular, if the dest tex format
2246 * is actually RGBA but the user created the texture as GL_RGB we
2247 * need to fill-in/override the alpha channel with 1.0.
2249 _mesa_texstore(ctx
, dims
,
2250 texImage
->_BaseFormat
,
2251 texImage
->TexFormat
,
2255 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2259 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2265 st_texture_image_unmap(st
, stImage
, slice
);
2266 pipe
->transfer_unmap(pipe
, src_trans
);
2271 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2272 * Note that the region to copy has already been clipped so we know we
2273 * won't read from outside the source renderbuffer's bounds.
2275 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2278 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2279 struct gl_texture_image
*texImage
,
2280 GLint destX
, GLint destY
, GLint slice
,
2281 struct gl_renderbuffer
*rb
,
2282 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2284 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2285 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2286 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2287 struct st_context
*st
= st_context(ctx
);
2288 struct pipe_context
*pipe
= st
->pipe
;
2289 struct pipe_screen
*screen
= pipe
->screen
;
2290 struct pipe_blit_info blit
;
2291 enum pipe_format dst_format
;
2292 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2296 st_flush_bitmap_cache(st
);
2297 st_invalidate_readpix_cache(st
);
2299 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2300 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2302 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2303 debug_printf("%s: null strb or stImage\n", __func__
);
2307 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2308 texImage
->TexFormat
)) {
2312 /* The base internal format must match the mesa format, so make sure
2313 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
2315 if (texImage
->_BaseFormat
!=
2316 _mesa_get_format_base_format(texImage
->TexFormat
) ||
2317 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
2321 /* Choose the destination format to match the TexImage behavior. */
2322 dst_format
= util_format_linear(stImage
->pt
->format
);
2323 dst_format
= util_format_luminance_to_red(dst_format
);
2324 dst_format
= util_format_intensity_to_red(dst_format
);
2326 /* See if the destination format is supported. */
2327 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2328 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2329 bind
= PIPE_BIND_DEPTH_STENCIL
;
2332 bind
= PIPE_BIND_RENDER_TARGET
;
2336 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2337 stImage
->pt
->nr_samples
, bind
)) {
2341 /* Y flipping for the main framebuffer. */
2343 srcY1
= strb
->Base
.Height
- srcY
- height
;
2344 srcY0
= srcY1
+ height
;
2348 srcY1
= srcY0
+ height
;
2351 /* Blit the texture.
2352 * This supports flipping, format conversions, and downsampling.
2354 memset(&blit
, 0, sizeof(blit
));
2355 blit
.src
.resource
= strb
->texture
;
2356 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2357 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2358 blit
.src
.box
.x
= srcX
;
2359 blit
.src
.box
.y
= srcY0
;
2360 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2361 blit
.src
.box
.width
= width
;
2362 blit
.src
.box
.height
= srcY1
- srcY0
;
2363 blit
.src
.box
.depth
= 1;
2364 blit
.dst
.resource
= stImage
->pt
;
2365 blit
.dst
.format
= dst_format
;
2366 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2367 blit
.dst
.box
.x
= destX
;
2368 blit
.dst
.box
.y
= destY
;
2369 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2370 blit
.dst
.box
.width
= width
;
2371 blit
.dst
.box
.height
= height
;
2372 blit
.dst
.box
.depth
= 1;
2373 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2374 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2375 pipe
->blit(pipe
, &blit
);
2379 /* software fallback */
2380 fallback_copy_texsubimage(ctx
,
2381 strb
, stImage
, texImage
->_BaseFormat
,
2382 destX
, destY
, slice
,
2383 srcX
, srcY
, width
, height
);
2388 * Copy image data from stImage into the texture object 'stObj' at level
2392 copy_image_data_to_texture(struct st_context
*st
,
2393 struct st_texture_object
*stObj
,
2395 struct st_texture_image
*stImage
)
2399 const struct gl_texture_image
*dstImage
=
2400 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2402 assert(dstImage
->Width
== stImage
->base
.Width
);
2403 assert(dstImage
->Height
== stImage
->base
.Height
);
2404 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2408 /* Copy potentially with the blitter:
2411 if (stImage
->pt
->last_level
== 0)
2414 src_level
= stImage
->base
.Level
;
2416 assert(src_level
<= stImage
->pt
->last_level
);
2417 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2418 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2419 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2420 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2421 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2422 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2424 st_texture_image_copy(st
->pipe
,
2425 stObj
->pt
, dstLevel
, /* dest texture, level */
2426 stImage
->pt
, src_level
, /* src texture, level */
2427 stImage
->base
.Face
);
2429 pipe_resource_reference(&stImage
->pt
, NULL
);
2431 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2436 * Called during state validation. When this function is finished,
2437 * the texture object should be ready for rendering.
2438 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2441 st_finalize_texture(struct gl_context
*ctx
,
2442 struct pipe_context
*pipe
,
2443 struct gl_texture_object
*tObj
)
2445 struct st_context
*st
= st_context(ctx
);
2446 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2447 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2449 const struct st_texture_image
*firstImage
;
2450 enum pipe_format firstImageFormat
;
2451 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2453 if (tObj
->Immutable
)
2456 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
2457 /* The texture is complete and we know exactly how many mipmap levels
2458 * are present/needed. This is conditional because we may be called
2459 * from the st_generate_mipmap() function when the texture object is
2460 * incomplete. In that case, we'll have set stObj->lastLevel before
2463 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
2464 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
2465 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2467 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2470 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
2471 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
2474 pipe_resource_reference(&stObj
->pt
, NULL
);
2475 st_texture_release_all_sampler_views(st
, stObj
);
2479 if (st_obj
->buffer
!= stObj
->pt
) {
2480 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
2481 st_texture_release_all_sampler_views(st
, stObj
);
2487 firstImage
= st_texture_image_const(_mesa_base_tex_image(&stObj
->base
));
2490 /* If both firstImage and stObj point to a texture which can contain
2491 * all active images, favour firstImage. Note that because of the
2492 * completeness requirement, we know that the image dimensions
2495 if (firstImage
->pt
&&
2496 firstImage
->pt
!= stObj
->pt
&&
2497 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2498 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2499 st_texture_release_all_sampler_views(st
, stObj
);
2502 /* If this texture comes from a window system, there is nothing else to do. */
2503 if (stObj
->surface_based
) {
2507 /* Find gallium format for the Mesa texture */
2509 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2511 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2513 GLuint width
, height
, depth
;
2515 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
2516 firstImage
->base
.Width2
,
2517 firstImage
->base
.Height2
,
2518 firstImage
->base
.Depth2
,
2519 &width
, &height
, &depth
, &ptLayers
);
2521 /* If we previously allocated a pipe texture and its sizes are
2522 * compatible, use them.
2525 u_minify(stObj
->pt
->width0
, firstImage
->base
.Level
) == width
&&
2526 u_minify(stObj
->pt
->height0
, firstImage
->base
.Level
) == height
&&
2527 u_minify(stObj
->pt
->depth0
, firstImage
->base
.Level
) == depth
) {
2528 ptWidth
= stObj
->pt
->width0
;
2529 ptHeight
= stObj
->pt
->height0
;
2530 ptDepth
= stObj
->pt
->depth0
;
2532 /* Otherwise, compute a new level=0 size that is compatible with the
2535 ptWidth
= width
> 1 ? width
<< firstImage
->base
.Level
: 1;
2536 ptHeight
= height
> 1 ? height
<< firstImage
->base
.Level
: 1;
2537 ptDepth
= depth
> 1 ? depth
<< firstImage
->base
.Level
: 1;
2539 /* If the base level image is 1x1x1, we still need to ensure that the
2540 * resulting pipe texture ends up with the required number of levels
2543 if (ptWidth
== 1 && ptHeight
== 1 && ptDepth
== 1) {
2544 ptWidth
<<= firstImage
->base
.Level
;
2546 if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
||
2547 stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP_ARRAY
)
2552 ptNumSamples
= firstImage
->base
.NumSamples
;
2555 /* If we already have a gallium texture, check that it matches the texture
2556 * object's format, target, size, num_levels, etc.
2559 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2560 stObj
->pt
->format
!= firstImageFormat
||
2561 stObj
->pt
->last_level
< stObj
->lastLevel
||
2562 stObj
->pt
->width0
!= ptWidth
||
2563 stObj
->pt
->height0
!= ptHeight
||
2564 stObj
->pt
->depth0
!= ptDepth
||
2565 stObj
->pt
->nr_samples
!= ptNumSamples
||
2566 stObj
->pt
->array_size
!= ptLayers
)
2568 /* The gallium texture does not match the Mesa texture so delete the
2569 * gallium texture now. We'll make a new one below.
2571 pipe_resource_reference(&stObj
->pt
, NULL
);
2572 st_texture_release_all_sampler_views(st
, stObj
);
2573 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
2577 /* May need to create a new gallium texture:
2580 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2582 stObj
->pt
= st_texture_create(st
,
2583 gl_target_to_pipe(stObj
->base
.Target
),
2589 ptLayers
, ptNumSamples
,
2593 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2598 /* Pull in any images not in the object's texture:
2600 for (face
= 0; face
< nr_faces
; face
++) {
2602 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2603 struct st_texture_image
*stImage
=
2604 st_texture_image(stObj
->base
.Image
[face
][level
]);
2606 /* Need to import images in main memory or held in other textures.
2608 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2612 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2613 height
= u_minify(ptHeight
, level
);
2617 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2618 depth
= u_minify(ptDepth
, level
);
2619 else if (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
)
2625 (stImage
->base
.Width
== u_minify(ptWidth
, level
) &&
2626 stImage
->base
.Height
== height
&&
2627 stImage
->base
.Depth
== depth
)) {
2628 /* src image fits expected dest mipmap level size */
2629 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2640 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2641 * for a whole mipmap stack.
2644 st_AllocTextureStorage(struct gl_context
*ctx
,
2645 struct gl_texture_object
*texObj
,
2646 GLsizei levels
, GLsizei width
,
2647 GLsizei height
, GLsizei depth
)
2649 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2650 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2651 struct st_context
*st
= st_context(ctx
);
2652 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2653 struct pipe_screen
*screen
= st
->pipe
->screen
;
2654 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
2655 enum pipe_format fmt
;
2657 GLuint num_samples
= texImage
->NumSamples
;
2661 stObj
->lastLevel
= levels
- 1;
2663 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2665 bindings
= default_bindings(st
, fmt
);
2667 /* Raise the sample count if the requested one is unsupported. */
2668 if (num_samples
> 1) {
2669 boolean found
= FALSE
;
2671 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2672 if (screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
2674 PIPE_BIND_SAMPLER_VIEW
)) {
2675 /* Update the sample count in gl_texture_image as well. */
2676 texImage
->NumSamples
= num_samples
;
2687 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2688 width
, height
, depth
,
2689 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2691 stObj
->pt
= st_texture_create(st
,
2692 gl_target_to_pipe(texObj
->Target
),
2698 ptLayers
, num_samples
,
2703 /* Set image resource pointers */
2704 for (level
= 0; level
< levels
; level
++) {
2706 for (face
= 0; face
< numFaces
; face
++) {
2707 struct st_texture_image
*stImage
=
2708 st_texture_image(texObj
->Image
[face
][level
]);
2709 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2711 etc_fallback_allocate(st
, stImage
);
2720 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2721 GLuint numLevels
, GLint level
,
2722 mesa_format format
, GLuint numSamples
,
2723 GLint width
, GLint height
, GLint depth
)
2725 struct st_context
*st
= st_context(ctx
);
2726 struct pipe_context
*pipe
= st
->pipe
;
2728 if (width
== 0 || height
== 0 || depth
== 0) {
2729 /* zero-sized images are legal, and always fit! */
2733 if (pipe
->screen
->can_create_resource
) {
2734 /* Ask the gallium driver if the texture is too large */
2735 struct gl_texture_object
*texObj
=
2736 _mesa_get_current_tex_object(ctx
, target
);
2737 struct pipe_resource pt
;
2739 /* Setup the pipe_resource object
2741 memset(&pt
, 0, sizeof(pt
));
2743 pt
.target
= gl_target_to_pipe(target
);
2744 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
2745 pt
.nr_samples
= numSamples
;
2747 st_gl_texture_dims_to_pipe_dims(target
,
2748 width
, height
, depth
,
2749 &pt
.width0
, &pt
.height0
,
2750 &pt
.depth0
, &pt
.array_size
);
2752 if (numLevels
> 0) {
2753 /* For immutable textures we know the final number of mip levels */
2754 pt
.last_level
= numLevels
- 1;
2756 else if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
2757 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
2758 /* assume just one mipmap level */
2762 /* assume a full set of mipmaps */
2763 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
2766 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
2769 /* Use core Mesa fallback */
2770 return _mesa_test_proxy_teximage(ctx
, target
, numLevels
, level
, format
,
2771 numSamples
, width
, height
, depth
);
2776 st_TextureView(struct gl_context
*ctx
,
2777 struct gl_texture_object
*texObj
,
2778 struct gl_texture_object
*origTexObj
)
2780 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
2781 struct st_texture_object
*tex
= st_texture_object(texObj
);
2782 struct gl_texture_image
*image
= texObj
->Image
[0][0];
2784 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2785 const int numLevels
= texObj
->NumLevels
;
2790 pipe_resource_reference(&tex
->pt
, orig
->pt
);
2792 /* Set image resource pointers */
2793 for (level
= 0; level
< numLevels
; level
++) {
2794 for (face
= 0; face
< numFaces
; face
++) {
2795 struct st_texture_image
*stImage
=
2796 st_texture_image(texObj
->Image
[face
][level
]);
2797 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
2801 tex
->surface_based
= GL_TRUE
;
2802 tex
->surface_format
=
2803 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
2805 tex
->lastLevel
= numLevels
- 1;
2811 st_ClearTexSubImage(struct gl_context
*ctx
,
2812 struct gl_texture_image
*texImage
,
2813 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2814 GLsizei width
, GLsizei height
, GLsizei depth
,
2815 const void *clearValue
)
2817 static const char zeros
[16] = {0};
2818 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2819 struct pipe_resource
*pt
= stImage
->pt
;
2820 struct st_context
*st
= st_context(ctx
);
2821 struct pipe_context
*pipe
= st
->pipe
;
2822 unsigned level
= texImage
->Level
;
2823 struct pipe_box box
;
2828 st_flush_bitmap_cache(st
);
2829 st_invalidate_readpix_cache(st
);
2831 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
2832 width
, height
, depth
, &box
);
2833 if (texImage
->TexObject
->Immutable
) {
2834 level
+= texImage
->TexObject
->MinLevel
;
2835 box
.z
+= texImage
->TexObject
->MinLayer
;
2838 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
2842 st_init_texture_functions(struct dd_function_table
*functions
)
2844 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
2845 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
2846 functions
->TexImage
= st_TexImage
;
2847 functions
->TexSubImage
= st_TexSubImage
;
2848 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
2849 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
2850 functions
->GenerateMipmap
= st_generate_mipmap
;
2852 functions
->GetTexSubImage
= st_GetTexSubImage
;
2854 /* compressed texture functions */
2855 functions
->CompressedTexImage
= st_CompressedTexImage
;
2856 functions
->GetCompressedTexSubImage
= _mesa_GetCompressedTexSubImage_sw
;
2858 functions
->NewTextureObject
= st_NewTextureObject
;
2859 functions
->NewTextureImage
= st_NewTextureImage
;
2860 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
2861 functions
->DeleteTexture
= st_DeleteTextureObject
;
2862 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
2863 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
2864 functions
->MapTextureImage
= st_MapTextureImage
;
2865 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
2867 /* XXX Temporary until we can query pipe's texture sizes */
2868 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
2870 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
2871 functions
->TextureView
= st_TextureView
;
2872 functions
->ClearTexSubImage
= st_ClearTexSubImage
;