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_fbo.h"
52 #include "state_tracker/st_cb_flush.h"
53 #include "state_tracker/st_cb_texture.h"
54 #include "state_tracker/st_cb_bufferobjects.h"
55 #include "state_tracker/st_format.h"
56 #include "state_tracker/st_pbo.h"
57 #include "state_tracker/st_texture.h"
58 #include "state_tracker/st_gen_mipmap.h"
59 #include "state_tracker/st_atom.h"
61 #include "pipe/p_context.h"
62 #include "pipe/p_defines.h"
63 #include "util/u_inlines.h"
64 #include "util/u_upload_mgr.h"
65 #include "pipe/p_shader_tokens.h"
66 #include "util/u_tile.h"
67 #include "util/u_format.h"
68 #include "util/u_surface.h"
69 #include "util/u_sampler.h"
70 #include "util/u_math.h"
71 #include "util/u_box.h"
72 #include "util/u_simple_shaders.h"
73 #include "cso_cache/cso_context.h"
74 #include "tgsi/tgsi_ureg.h"
76 #define DBG if (0) printf
79 enum pipe_texture_target
80 gl_target_to_pipe(GLenum target
)
84 case GL_PROXY_TEXTURE_1D
:
85 return PIPE_TEXTURE_1D
;
87 case GL_PROXY_TEXTURE_2D
:
88 case GL_TEXTURE_EXTERNAL_OES
:
89 case GL_TEXTURE_2D_MULTISAMPLE
:
90 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
91 return PIPE_TEXTURE_2D
;
92 case GL_TEXTURE_RECTANGLE_NV
:
93 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
94 return PIPE_TEXTURE_RECT
;
96 case GL_PROXY_TEXTURE_3D
:
97 return PIPE_TEXTURE_3D
;
98 case GL_TEXTURE_CUBE_MAP_ARB
:
99 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
100 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
101 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
102 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
103 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
104 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
105 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
106 return PIPE_TEXTURE_CUBE
;
107 case GL_TEXTURE_1D_ARRAY_EXT
:
108 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
109 return PIPE_TEXTURE_1D_ARRAY
;
110 case GL_TEXTURE_2D_ARRAY_EXT
:
111 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
112 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
113 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
114 return PIPE_TEXTURE_2D_ARRAY
;
115 case GL_TEXTURE_BUFFER
:
117 case GL_TEXTURE_CUBE_MAP_ARRAY
:
118 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
119 return PIPE_TEXTURE_CUBE_ARRAY
;
127 /** called via ctx->Driver.NewTextureImage() */
128 static struct gl_texture_image
*
129 st_NewTextureImage(struct gl_context
* ctx
)
131 DBG("%s\n", __func__
);
133 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
137 /** called via ctx->Driver.DeleteTextureImage() */
139 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
141 /* nothing special (yet) for st_texture_image */
142 _mesa_delete_texture_image(ctx
, img
);
146 /** called via ctx->Driver.NewTextureObject() */
147 static struct gl_texture_object
*
148 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
150 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
152 DBG("%s\n", __func__
);
153 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
158 /** called via ctx->Driver.DeleteTextureObject() */
160 st_DeleteTextureObject(struct gl_context
*ctx
,
161 struct gl_texture_object
*texObj
)
163 struct st_context
*st
= st_context(ctx
);
164 struct st_texture_object
*stObj
= st_texture_object(texObj
);
166 pipe_resource_reference(&stObj
->pt
, NULL
);
167 st_texture_release_all_sampler_views(st
, stObj
);
168 st_texture_free_sampler_views(stObj
);
169 _mesa_delete_texture_object(ctx
, texObj
);
173 /** called via ctx->Driver.FreeTextureImageBuffer() */
175 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
176 struct gl_texture_image
*texImage
)
178 struct st_texture_image
*stImage
= st_texture_image(texImage
);
180 DBG("%s\n", __func__
);
183 pipe_resource_reference(&stImage
->pt
, NULL
);
186 free(stImage
->transfer
);
187 stImage
->transfer
= NULL
;
188 stImage
->num_transfers
= 0;
192 /** called via ctx->Driver.MapTextureImage() */
194 st_MapTextureImage(struct gl_context
*ctx
,
195 struct gl_texture_image
*texImage
,
196 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
198 GLubyte
**mapOut
, GLint
*rowStrideOut
)
200 struct st_context
*st
= st_context(ctx
);
201 struct st_texture_image
*stImage
= st_texture_image(texImage
);
204 struct pipe_transfer
*transfer
;
207 if (mode
& GL_MAP_READ_BIT
)
208 pipeMode
|= PIPE_TRANSFER_READ
;
209 if (mode
& GL_MAP_WRITE_BIT
)
210 pipeMode
|= PIPE_TRANSFER_WRITE
;
211 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
212 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
214 map
= st_texture_image_map(st
, stImage
, pipeMode
, x
, y
, slice
, w
, h
, 1,
217 if ((_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
218 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
)) {
219 /* ETC isn't supported by gallium and it's represented
220 * by uncompressed formats. Only write transfers with precompressed
221 * data are supported by ES3, which makes this really simple.
223 * Just create a temporary storage where the ETC texture will
224 * be stored. It will be decompressed in the Unmap function.
226 unsigned z
= transfer
->box
.z
;
227 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
229 itransfer
->temp_data
=
230 malloc(_mesa_format_image_size(texImage
->TexFormat
, w
, h
, 1));
231 itransfer
->temp_stride
=
232 _mesa_format_row_stride(texImage
->TexFormat
, w
);
233 itransfer
->map
= map
;
235 *mapOut
= itransfer
->temp_data
;
236 *rowStrideOut
= itransfer
->temp_stride
;
239 /* supported mapping */
241 *rowStrideOut
= transfer
->stride
;
251 /** called via ctx->Driver.UnmapTextureImage() */
253 st_UnmapTextureImage(struct gl_context
*ctx
,
254 struct gl_texture_image
*texImage
,
257 struct st_context
*st
= st_context(ctx
);
258 struct st_texture_image
*stImage
= st_texture_image(texImage
);
260 if ((_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
261 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
)) {
262 /* Decompress the ETC texture to the mapped one. */
263 unsigned z
= slice
+ stImage
->base
.Face
;
264 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
265 struct pipe_transfer
*transfer
= itransfer
->transfer
;
267 assert(z
== transfer
->box
.z
);
269 if (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
) {
270 _mesa_etc1_unpack_rgba8888(itransfer
->map
, transfer
->stride
,
271 itransfer
->temp_data
,
272 itransfer
->temp_stride
,
273 transfer
->box
.width
, transfer
->box
.height
);
276 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
277 itransfer
->temp_data
, itransfer
->temp_stride
,
278 transfer
->box
.width
, transfer
->box
.height
,
279 texImage
->TexFormat
);
282 free(itransfer
->temp_data
);
283 itransfer
->temp_data
= NULL
;
284 itransfer
->temp_stride
= 0;
288 st_texture_image_unmap(st
, stImage
, slice
);
293 * Return default texture resource binding bitmask for the given format.
296 default_bindings(struct st_context
*st
, enum pipe_format format
)
298 struct pipe_screen
*screen
= st
->pipe
->screen
;
299 const unsigned target
= PIPE_TEXTURE_2D
;
302 if (util_format_is_depth_or_stencil(format
))
303 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
305 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
307 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
311 format
= util_format_linear(format
);
313 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
316 return PIPE_BIND_SAMPLER_VIEW
;
322 * Given the size of a mipmap image, try to compute the size of the level=0
325 * Note that this isn't always accurate for odd-sized, non-POW textures.
326 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
328 * \return GL_TRUE for success, GL_FALSE for failure
331 guess_base_level_size(GLenum target
,
332 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
333 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
340 /* Guess the size of the base level.
341 * Depending on the image's size, we can't always make a guess here.
345 case GL_TEXTURE_1D_ARRAY
:
350 case GL_TEXTURE_2D_ARRAY
:
351 /* We can't make a good guess here, because the base level dimensions
354 if (width
== 1 || height
== 1) {
361 case GL_TEXTURE_CUBE_MAP
:
362 case GL_TEXTURE_CUBE_MAP_ARRAY
:
368 /* We can't make a good guess here, because the base level dimensions
371 if (width
== 1 || height
== 1 || depth
== 1) {
379 case GL_TEXTURE_RECTANGLE
:
396 * Try to determine whether we should allocate memory for a full texture
397 * mipmap. The problem is when we get a glTexImage(level=0) call, we
398 * can't immediately know if other mipmap levels are coming next. Here
399 * we try to guess whether to allocate memory for a mipmap or just the
402 * If we guess incorrectly here we'll later reallocate the right amount of
403 * memory either in st_AllocTextureImageBuffer() or st_finalize_texture().
405 * \param stObj the texture object we're going to allocate memory for.
406 * \param stImage describes the incoming image which we need to store.
409 allocate_full_mipmap(const struct st_texture_object
*stObj
,
410 const struct st_texture_image
*stImage
)
412 switch (stObj
->base
.Target
) {
413 case GL_TEXTURE_RECTANGLE_NV
:
414 case GL_TEXTURE_BUFFER
:
415 case GL_TEXTURE_EXTERNAL_OES
:
416 case GL_TEXTURE_2D_MULTISAMPLE
:
417 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
418 /* these texture types cannot be mipmapped */
422 if (stImage
->base
.Level
> 0 || stObj
->base
.GenerateMipmap
)
425 if (stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
426 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
)
427 /* depth/stencil textures are seldom mipmapped */
430 if (stObj
->base
.BaseLevel
== 0 && stObj
->base
.MaxLevel
== 0)
433 if (stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
434 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
)
435 /* not a mipmap minification filter */
438 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
439 /* 3D textures are seldom mipmapped */
447 * Try to allocate a pipe_resource object for the given st_texture_object.
449 * We use the given st_texture_image as a clue to determine the size of the
450 * mipmap image at level=0.
452 * \return GL_TRUE for success, GL_FALSE if out of memory.
455 guess_and_alloc_texture(struct st_context
*st
,
456 struct st_texture_object
*stObj
,
457 const struct st_texture_image
*stImage
)
459 GLuint lastLevel
, width
, height
, depth
;
461 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
462 enum pipe_format fmt
;
464 DBG("%s\n", __func__
);
468 if (!guess_base_level_size(stObj
->base
.Target
,
469 stImage
->base
.Width2
,
470 stImage
->base
.Height2
,
471 stImage
->base
.Depth2
,
473 &width
, &height
, &depth
)) {
474 /* we can't determine the image size at level=0 */
475 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
476 /* this is not an out of memory error */
480 /* At this point, (width x height x depth) is the expected size of
481 * the level=0 mipmap image.
484 /* Guess a reasonable value for lastLevel. With OpenGL we have no
485 * idea how many mipmap levels will be in a texture until we start
486 * to render with it. Make an educated guess here but be prepared
487 * to re-allocating a texture buffer with space for more (or fewer)
488 * mipmap levels later.
490 if (allocate_full_mipmap(stObj
, stImage
)) {
491 /* alloc space for a full mipmap */
492 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
493 width
, height
, depth
) - 1;
496 /* only alloc space for a single mipmap level */
500 /* Save the level=0 dimensions */
501 stObj
->width0
= width
;
502 stObj
->height0
= height
;
503 stObj
->depth0
= depth
;
505 fmt
= st_mesa_format_to_pipe_format(st
, stImage
->base
.TexFormat
);
507 bindings
= default_bindings(st
, fmt
);
509 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
510 width
, height
, depth
,
511 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
513 stObj
->pt
= st_texture_create(st
,
514 gl_target_to_pipe(stObj
->base
.Target
),
523 stObj
->lastLevel
= lastLevel
;
525 DBG("%s returning %d\n", __func__
, (stObj
->pt
!= NULL
));
527 return stObj
->pt
!= NULL
;
532 * Called via ctx->Driver.AllocTextureImageBuffer().
533 * If the texture object/buffer already has space for the indicated image,
534 * we're done. Otherwise, allocate memory for the new texture image.
537 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
538 struct gl_texture_image
*texImage
)
540 struct st_context
*st
= st_context(ctx
);
541 struct st_texture_image
*stImage
= st_texture_image(texImage
);
542 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
543 const GLuint level
= texImage
->Level
;
544 GLuint width
= texImage
->Width
;
545 GLuint height
= texImage
->Height
;
546 GLuint depth
= texImage
->Depth
;
548 DBG("%s\n", __func__
);
550 assert(!stImage
->pt
); /* xxx this might be wrong */
552 /* Look if the parent texture object has space for this image */
554 level
<= stObj
->pt
->last_level
&&
555 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
556 /* this image will fit in the existing texture object's memory */
557 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
561 /* The parent texture object does not have space for this image */
563 pipe_resource_reference(&stObj
->pt
, NULL
);
564 st_texture_release_all_sampler_views(st
, stObj
);
566 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
567 /* Probably out of memory.
568 * Try flushing any pending rendering, then retry.
571 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
572 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
578 st_texture_match_image(st
, stObj
->pt
, texImage
)) {
579 /* The image will live in the object's mipmap memory */
580 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
585 /* Create a new, temporary texture/resource/buffer to hold this
586 * one texture image. Note that when we later access this image
587 * (either for mapping or copying) we'll want to always specify
588 * mipmap level=0, even if the image represents some other mipmap
591 enum pipe_format format
=
592 st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
593 GLuint bindings
= default_bindings(st
, format
);
594 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
596 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
597 width
, height
, depth
,
598 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
600 stImage
->pt
= st_texture_create(st
,
601 gl_target_to_pipe(stObj
->base
.Target
),
609 return stImage
->pt
!= NULL
;
615 * Preparation prior to glTexImage. Basically check the 'surface_based'
616 * field and switch to a "normal" tex image if necessary.
619 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
620 GLenum format
, GLenum type
)
622 struct gl_texture_object
*texObj
= texImage
->TexObject
;
623 struct st_texture_object
*stObj
= st_texture_object(texObj
);
625 /* switch to "normal" */
626 if (stObj
->surface_based
) {
627 const GLenum target
= texObj
->Target
;
628 const GLuint level
= texImage
->Level
;
629 mesa_format texFormat
;
631 _mesa_clear_texture_object(ctx
, texObj
);
632 pipe_resource_reference(&stObj
->pt
, NULL
);
634 /* oops, need to init this image again */
635 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
636 texImage
->InternalFormat
, format
,
639 _mesa_init_teximage_fields(ctx
, texImage
,
640 texImage
->Width
, texImage
->Height
,
641 texImage
->Depth
, texImage
->Border
,
642 texImage
->InternalFormat
, texFormat
);
644 stObj
->surface_based
= GL_FALSE
;
650 * Return a writemask for the gallium blit. The parameters can be base
651 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
654 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
657 case GL_DEPTH_STENCIL
:
659 case GL_DEPTH_STENCIL
:
661 case GL_DEPTH_COMPONENT
:
663 case GL_STENCIL_INDEX
:
670 case GL_DEPTH_COMPONENT
:
672 case GL_DEPTH_STENCIL
:
673 case GL_DEPTH_COMPONENT
:
680 case GL_STENCIL_INDEX
:
682 case GL_STENCIL_INDEX
:
690 return PIPE_MASK_RGBA
;
695 * Converts format to a format with the same components, types
696 * and sizes, but with the components in RGBA order.
698 static enum pipe_format
699 unswizzle_format(enum pipe_format format
)
703 case PIPE_FORMAT_B8G8R8A8_UNORM
:
704 case PIPE_FORMAT_A8R8G8B8_UNORM
:
705 case PIPE_FORMAT_A8B8G8R8_UNORM
:
706 return PIPE_FORMAT_R8G8B8A8_UNORM
;
708 case PIPE_FORMAT_B10G10R10A2_UNORM
:
709 return PIPE_FORMAT_R10G10B10A2_UNORM
;
711 case PIPE_FORMAT_B10G10R10A2_SNORM
:
712 return PIPE_FORMAT_R10G10B10A2_SNORM
;
714 case PIPE_FORMAT_B10G10R10A2_UINT
:
715 return PIPE_FORMAT_R10G10B10A2_UINT
;
723 * Converts PIPE_FORMAT_A* to PIPE_FORMAT_R*.
725 static enum pipe_format
726 alpha_to_red(enum pipe_format format
)
730 case PIPE_FORMAT_A8_UNORM
:
731 return PIPE_FORMAT_R8_UNORM
;
732 case PIPE_FORMAT_A8_SNORM
:
733 return PIPE_FORMAT_R8_SNORM
;
734 case PIPE_FORMAT_A8_UINT
:
735 return PIPE_FORMAT_R8_UINT
;
736 case PIPE_FORMAT_A8_SINT
:
737 return PIPE_FORMAT_R8_SINT
;
739 case PIPE_FORMAT_A16_UNORM
:
740 return PIPE_FORMAT_R16_UNORM
;
741 case PIPE_FORMAT_A16_SNORM
:
742 return PIPE_FORMAT_R16_SNORM
;
743 case PIPE_FORMAT_A16_UINT
:
744 return PIPE_FORMAT_R16_UINT
;
745 case PIPE_FORMAT_A16_SINT
:
746 return PIPE_FORMAT_R16_SINT
;
747 case PIPE_FORMAT_A16_FLOAT
:
748 return PIPE_FORMAT_R16_FLOAT
;
750 case PIPE_FORMAT_A32_UINT
:
751 return PIPE_FORMAT_R32_UINT
;
752 case PIPE_FORMAT_A32_SINT
:
753 return PIPE_FORMAT_R32_SINT
;
754 case PIPE_FORMAT_A32_FLOAT
:
755 return PIPE_FORMAT_R32_FLOAT
;
763 * Converts PIPE_FORMAT_R*A* to PIPE_FORMAT_R*G*.
765 static enum pipe_format
766 red_alpha_to_red_green(enum pipe_format format
)
770 case PIPE_FORMAT_R8A8_UNORM
:
771 return PIPE_FORMAT_R8G8_UNORM
;
772 case PIPE_FORMAT_R8A8_SNORM
:
773 return PIPE_FORMAT_R8G8_SNORM
;
774 case PIPE_FORMAT_R8A8_UINT
:
775 return PIPE_FORMAT_R8G8_UINT
;
776 case PIPE_FORMAT_R8A8_SINT
:
777 return PIPE_FORMAT_R8G8_SINT
;
779 case PIPE_FORMAT_R16A16_UNORM
:
780 return PIPE_FORMAT_R16G16_UNORM
;
781 case PIPE_FORMAT_R16A16_SNORM
:
782 return PIPE_FORMAT_R16G16_SNORM
;
783 case PIPE_FORMAT_R16A16_UINT
:
784 return PIPE_FORMAT_R16G16_UINT
;
785 case PIPE_FORMAT_R16A16_SINT
:
786 return PIPE_FORMAT_R16G16_SINT
;
787 case PIPE_FORMAT_R16A16_FLOAT
:
788 return PIPE_FORMAT_R16G16_FLOAT
;
790 case PIPE_FORMAT_R32A32_UINT
:
791 return PIPE_FORMAT_R32G32_UINT
;
792 case PIPE_FORMAT_R32A32_SINT
:
793 return PIPE_FORMAT_R32G32_SINT
;
794 case PIPE_FORMAT_R32A32_FLOAT
:
795 return PIPE_FORMAT_R32G32_FLOAT
;
803 * Converts PIPE_FORMAT_L*A* to PIPE_FORMAT_R*G*.
805 static enum pipe_format
806 luminance_alpha_to_red_green(enum pipe_format format
)
810 case PIPE_FORMAT_L8A8_UNORM
:
811 return PIPE_FORMAT_R8G8_UNORM
;
812 case PIPE_FORMAT_L8A8_SNORM
:
813 return PIPE_FORMAT_R8G8_SNORM
;
814 case PIPE_FORMAT_L8A8_UINT
:
815 return PIPE_FORMAT_R8G8_UINT
;
816 case PIPE_FORMAT_L8A8_SINT
:
817 return PIPE_FORMAT_R8G8_SINT
;
819 case PIPE_FORMAT_L16A16_UNORM
:
820 return PIPE_FORMAT_R16G16_UNORM
;
821 case PIPE_FORMAT_L16A16_SNORM
:
822 return PIPE_FORMAT_R16G16_SNORM
;
823 case PIPE_FORMAT_L16A16_UINT
:
824 return PIPE_FORMAT_R16G16_UINT
;
825 case PIPE_FORMAT_L16A16_SINT
:
826 return PIPE_FORMAT_R16G16_SINT
;
827 case PIPE_FORMAT_L16A16_FLOAT
:
828 return PIPE_FORMAT_R16G16_FLOAT
;
830 case PIPE_FORMAT_L32A32_UINT
:
831 return PIPE_FORMAT_R32G32_UINT
;
832 case PIPE_FORMAT_L32A32_SINT
:
833 return PIPE_FORMAT_R32G32_SINT
;
834 case PIPE_FORMAT_L32A32_FLOAT
:
835 return PIPE_FORMAT_R32G32_FLOAT
;
843 * Returns true if format is a PIPE_FORMAT_A* format, and false otherwise.
846 format_is_alpha(enum pipe_format format
)
848 const struct util_format_description
*desc
= util_format_description(format
);
850 if (desc
->nr_channels
== 1 &&
851 desc
->swizzle
[0] == PIPE_SWIZZLE_0
&&
852 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
853 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
854 desc
->swizzle
[3] == PIPE_SWIZZLE_X
)
861 * Returns true if format is a PIPE_FORMAT_R* format, and false otherwise.
864 format_is_red(enum pipe_format format
)
866 const struct util_format_description
*desc
= util_format_description(format
);
868 if (desc
->nr_channels
== 1 &&
869 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
870 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
871 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
872 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
880 * Returns true if format is a PIPE_FORMAT_L* format, and false otherwise.
883 format_is_luminance(enum pipe_format format
)
885 const struct util_format_description
*desc
= util_format_description(format
);
887 if (desc
->nr_channels
== 1 &&
888 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
889 desc
->swizzle
[1] == PIPE_SWIZZLE_X
&&
890 desc
->swizzle
[2] == PIPE_SWIZZLE_X
&&
891 desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
898 * Returns true if format is a PIPE_FORMAT_R*A* format, and false otherwise.
901 format_is_red_alpha(enum pipe_format format
)
903 const struct util_format_description
*desc
= util_format_description(format
);
905 if (desc
->nr_channels
== 2 &&
906 desc
->swizzle
[0] == PIPE_SWIZZLE_X
&&
907 desc
->swizzle
[1] == PIPE_SWIZZLE_0
&&
908 desc
->swizzle
[2] == PIPE_SWIZZLE_0
&&
909 desc
->swizzle
[3] == PIPE_SWIZZLE_Y
)
916 format_is_swizzled_rgba(enum pipe_format format
)
918 const struct util_format_description
*desc
= util_format_description(format
);
920 if ((desc
->swizzle
[0] == TGSI_SWIZZLE_X
|| desc
->swizzle
[0] == PIPE_SWIZZLE_0
) &&
921 (desc
->swizzle
[1] == TGSI_SWIZZLE_Y
|| desc
->swizzle
[1] == PIPE_SWIZZLE_0
) &&
922 (desc
->swizzle
[2] == TGSI_SWIZZLE_Z
|| desc
->swizzle
[2] == PIPE_SWIZZLE_0
) &&
923 (desc
->swizzle
[3] == TGSI_SWIZZLE_W
|| desc
->swizzle
[3] == PIPE_SWIZZLE_1
))
931 unsigned char swizzle
[4];
932 enum pipe_format format
;
935 static const struct format_table table_8888_unorm
[] = {
936 { { 0, 1, 2, 3 }, PIPE_FORMAT_R8G8B8A8_UNORM
},
937 { { 2, 1, 0, 3 }, PIPE_FORMAT_B8G8R8A8_UNORM
},
938 { { 3, 0, 1, 2 }, PIPE_FORMAT_A8R8G8B8_UNORM
},
939 { { 3, 2, 1, 0 }, PIPE_FORMAT_A8B8G8R8_UNORM
}
942 static const struct format_table table_1010102_unorm
[] = {
943 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UNORM
},
944 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UNORM
}
947 static const struct format_table table_1010102_snorm
[] = {
948 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_SNORM
},
949 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_SNORM
}
952 static const struct format_table table_1010102_uint
[] = {
953 { { 0, 1, 2, 3 }, PIPE_FORMAT_R10G10B10A2_UINT
},
954 { { 2, 1, 0, 3 }, PIPE_FORMAT_B10G10R10A2_UINT
}
957 static enum pipe_format
958 swizzle_format(enum pipe_format format
, const int * const swizzle
)
963 case PIPE_FORMAT_R8G8B8A8_UNORM
:
964 case PIPE_FORMAT_B8G8R8A8_UNORM
:
965 case PIPE_FORMAT_A8R8G8B8_UNORM
:
966 case PIPE_FORMAT_A8B8G8R8_UNORM
:
967 for (i
= 0; i
< ARRAY_SIZE(table_8888_unorm
); i
++) {
968 if (swizzle
[0] == table_8888_unorm
[i
].swizzle
[0] &&
969 swizzle
[1] == table_8888_unorm
[i
].swizzle
[1] &&
970 swizzle
[2] == table_8888_unorm
[i
].swizzle
[2] &&
971 swizzle
[3] == table_8888_unorm
[i
].swizzle
[3])
972 return table_8888_unorm
[i
].format
;
976 case PIPE_FORMAT_R10G10B10A2_UNORM
:
977 case PIPE_FORMAT_B10G10R10A2_UNORM
:
978 for (i
= 0; i
< ARRAY_SIZE(table_1010102_unorm
); i
++) {
979 if (swizzle
[0] == table_1010102_unorm
[i
].swizzle
[0] &&
980 swizzle
[1] == table_1010102_unorm
[i
].swizzle
[1] &&
981 swizzle
[2] == table_1010102_unorm
[i
].swizzle
[2] &&
982 swizzle
[3] == table_1010102_unorm
[i
].swizzle
[3])
983 return table_1010102_unorm
[i
].format
;
987 case PIPE_FORMAT_R10G10B10A2_SNORM
:
988 case PIPE_FORMAT_B10G10R10A2_SNORM
:
989 for (i
= 0; i
< ARRAY_SIZE(table_1010102_snorm
); i
++) {
990 if (swizzle
[0] == table_1010102_snorm
[i
].swizzle
[0] &&
991 swizzle
[1] == table_1010102_snorm
[i
].swizzle
[1] &&
992 swizzle
[2] == table_1010102_snorm
[i
].swizzle
[2] &&
993 swizzle
[3] == table_1010102_snorm
[i
].swizzle
[3])
994 return table_1010102_snorm
[i
].format
;
998 case PIPE_FORMAT_R10G10B10A2_UINT
:
999 case PIPE_FORMAT_B10G10R10A2_UINT
:
1000 for (i
= 0; i
< ARRAY_SIZE(table_1010102_uint
); i
++) {
1001 if (swizzle
[0] == table_1010102_uint
[i
].swizzle
[0] &&
1002 swizzle
[1] == table_1010102_uint
[i
].swizzle
[1] &&
1003 swizzle
[2] == table_1010102_uint
[i
].swizzle
[2] &&
1004 swizzle
[3] == table_1010102_uint
[i
].swizzle
[3])
1005 return table_1010102_uint
[i
].format
;
1013 return PIPE_FORMAT_NONE
;
1017 reinterpret_formats(enum pipe_format
*src_format
, enum pipe_format
*dst_format
)
1019 enum pipe_format src
= *src_format
;
1020 enum pipe_format dst
= *dst_format
;
1022 /* Note: dst_format has already been transformed from luminance/intensity
1023 * to red when this function is called. The source format will never
1024 * be an intensity format, because GL_INTENSITY is not a legal value
1025 * for the format parameter in glTex(Sub)Image(). */
1027 if (format_is_alpha(src
)) {
1028 if (!format_is_alpha(dst
))
1031 src
= alpha_to_red(src
);
1032 dst
= alpha_to_red(dst
);
1033 } else if (format_is_luminance(src
)) {
1034 if (!format_is_red(dst
) && !format_is_red_alpha(dst
))
1037 src
= util_format_luminance_to_red(src
);
1038 } else if (util_format_is_luminance_alpha(src
)) {
1039 src
= luminance_alpha_to_red_green(src
);
1041 if (format_is_red_alpha(dst
)) {
1042 dst
= red_alpha_to_red_green(dst
);
1043 } else if (!format_is_red(dst
))
1045 } else if (format_is_swizzled_rgba(src
)) {
1046 const struct util_format_description
*src_desc
= util_format_description(src
);
1047 const struct util_format_description
*dst_desc
= util_format_description(dst
);
1051 /* Make sure the format is an RGBA and not an RGBX format */
1052 if (src_desc
->nr_channels
!= 4 || src_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1055 if (dst_desc
->nr_channels
!= 4 || dst_desc
->swizzle
[3] == PIPE_SWIZZLE_1
)
1058 for (i
= 0; i
< 4; i
++)
1059 swizzle
[i
] = dst_desc
->swizzle
[src_desc
->swizzle
[i
]];
1061 dst
= swizzle_format(dst
, swizzle
);
1062 if (dst
== PIPE_FORMAT_NONE
)
1065 src
= unswizzle_format(src
);
1074 try_pbo_upload_common(struct gl_context
*ctx
,
1075 struct pipe_surface
*surface
,
1076 const struct st_pbo_addresses
*addr
,
1077 enum pipe_format src_format
)
1079 struct st_context
*st
= st_context(ctx
);
1080 struct cso_context
*cso
= st
->cso_context
;
1081 struct pipe_context
*pipe
= st
->pipe
;
1082 bool success
= false;
1084 /* Create the shaders */
1086 st
->pbo
.vs
= st_pbo_create_vs(st
);
1091 if (addr
->depth
!= 1 && st
->pbo
.use_gs
&& !st
->pbo
.gs
) {
1092 st
->pbo
.gs
= st_pbo_create_gs(st
);
1097 if (!st
->pbo
.upload_fs
) {
1098 st
->pbo
.upload_fs
= st_pbo_create_upload_fs(st
);
1099 if (!st
->pbo
.upload_fs
)
1103 cso_save_state(cso
, (CSO_BIT_FRAGMENT_SAMPLER_VIEWS
|
1104 CSO_BIT_FRAGMENT_SAMPLERS
|
1105 CSO_BIT_VERTEX_ELEMENTS
|
1106 CSO_BIT_AUX_VERTEX_BUFFER_SLOT
|
1107 CSO_BIT_FRAMEBUFFER
|
1110 CSO_BIT_DEPTH_STENCIL_ALPHA
|
1111 CSO_BIT_RASTERIZER
|
1112 CSO_BIT_STREAM_OUTPUTS
|
1113 CSO_BIT_PAUSE_QUERIES
|
1114 CSO_BITS_ALL_SHADERS
));
1115 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1118 /* Set up the sampler_view */
1120 struct pipe_sampler_view templ
;
1121 struct pipe_sampler_view
*sampler_view
;
1122 struct pipe_sampler_state sampler
= {0};
1123 const struct pipe_sampler_state
*samplers
[1] = {&sampler
};
1125 memset(&templ
, 0, sizeof(templ
));
1126 templ
.target
= PIPE_BUFFER
;
1127 templ
.format
= src_format
;
1128 templ
.u
.buf
.first_element
= addr
->first_element
;
1129 templ
.u
.buf
.last_element
= addr
->last_element
;
1130 templ
.swizzle_r
= PIPE_SWIZZLE_X
;
1131 templ
.swizzle_g
= PIPE_SWIZZLE_Y
;
1132 templ
.swizzle_b
= PIPE_SWIZZLE_Z
;
1133 templ
.swizzle_a
= PIPE_SWIZZLE_W
;
1135 sampler_view
= pipe
->create_sampler_view(pipe
, addr
->buffer
, &templ
);
1136 if (sampler_view
== NULL
)
1139 cso_set_sampler_views(cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
1141 pipe_sampler_view_reference(&sampler_view
, NULL
);
1143 cso_set_samplers(cso
, PIPE_SHADER_FRAGMENT
, 1, samplers
);
1146 /* Upload vertices */
1148 struct pipe_vertex_buffer vbo
;
1149 struct pipe_vertex_element velem
;
1151 float x0
= (float) addr
->xoffset
/ surface
->width
* 2.0f
- 1.0f
;
1152 float y0
= (float) addr
->yoffset
/ surface
->height
* 2.0f
- 1.0f
;
1153 float x1
= (float) (addr
->xoffset
+ addr
->width
) / surface
->width
* 2.0f
- 1.0f
;
1154 float y1
= (float) (addr
->yoffset
+ addr
->height
) / surface
->height
* 2.0f
- 1.0f
;
1156 float *verts
= NULL
;
1158 vbo
.user_buffer
= NULL
;
1160 vbo
.stride
= 2 * sizeof(float);
1162 u_upload_alloc(st
->uploader
, 0, 8 * sizeof(float), 4,
1163 &vbo
.buffer_offset
, &vbo
.buffer
, (void **) &verts
);
1176 u_upload_unmap(st
->uploader
);
1178 velem
.src_offset
= 0;
1179 velem
.instance_divisor
= 0;
1180 velem
.vertex_buffer_index
= cso_get_aux_vertex_buffer_slot(cso
);
1181 velem
.src_format
= PIPE_FORMAT_R32G32_FLOAT
;
1183 cso_set_vertex_elements(cso
, 1, &velem
);
1185 cso_set_vertex_buffers(cso
, velem
.vertex_buffer_index
, 1, &vbo
);
1187 pipe_resource_reference(&vbo
.buffer
, NULL
);
1190 /* Upload constants */
1192 struct pipe_constant_buffer cb
;
1194 if (st
->constbuf_uploader
) {
1196 cb
.user_buffer
= NULL
;
1197 u_upload_data(st
->constbuf_uploader
, 0, sizeof(addr
->constants
),
1198 ctx
->Const
.UniformBufferOffsetAlignment
,
1199 &addr
->constants
, &cb
.buffer_offset
, &cb
.buffer
);
1203 u_upload_unmap(st
->constbuf_uploader
);
1206 cb
.user_buffer
= &addr
->constants
;
1207 cb
.buffer_offset
= 0;
1209 cb
.buffer_size
= sizeof(addr
->constants
);
1211 cso_set_constant_buffer(cso
, PIPE_SHADER_FRAGMENT
, 0, &cb
);
1213 pipe_resource_reference(&cb
.buffer
, NULL
);
1216 /* Framebuffer_state */
1218 struct pipe_framebuffer_state fb
;
1219 memset(&fb
, 0, sizeof(fb
));
1220 fb
.width
= surface
->width
;
1221 fb
.height
= surface
->height
;
1223 pipe_surface_reference(&fb
.cbufs
[0], surface
);
1225 cso_set_framebuffer(cso
, &fb
);
1227 pipe_surface_reference(&fb
.cbufs
[0], NULL
);
1230 cso_set_viewport_dims(cso
, surface
->width
, surface
->height
, FALSE
);
1233 cso_set_blend(cso
, &st
->pbo
.upload_blend
);
1235 /* Depth/stencil/alpha state */
1237 struct pipe_depth_stencil_alpha_state dsa
;
1238 memset(&dsa
, 0, sizeof(dsa
));
1239 cso_set_depth_stencil_alpha(cso
, &dsa
);
1242 /* Rasterizer state */
1243 cso_set_rasterizer(cso
, &st
->pbo
.raster
);
1245 /* Set up the shaders */
1246 cso_set_vertex_shader_handle(cso
, st
->pbo
.vs
);
1248 cso_set_geometry_shader_handle(cso
, addr
->depth
!= 1 ? st
->pbo
.gs
: NULL
);
1250 cso_set_tessctrl_shader_handle(cso
, NULL
);
1252 cso_set_tesseval_shader_handle(cso
, NULL
);
1254 cso_set_fragment_shader_handle(cso
, st
->pbo
.upload_fs
);
1256 /* Disable stream output */
1257 cso_set_stream_outputs(cso
, 0, NULL
, 0);
1259 if (addr
->depth
== 1) {
1260 cso_draw_arrays(cso
, PIPE_PRIM_TRIANGLE_STRIP
, 0, 4);
1262 cso_draw_arrays_instanced(cso
, PIPE_PRIM_TRIANGLE_STRIP
,
1263 0, 4, 0, addr
->depth
);
1269 cso_restore_state(cso
);
1270 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
1276 try_pbo_upload(struct gl_context
*ctx
, GLuint dims
,
1277 struct gl_texture_image
*texImage
,
1278 GLenum format
, GLenum type
,
1279 enum pipe_format dst_format
,
1280 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1281 GLint width
, GLint height
, GLint depth
,
1283 const struct gl_pixelstore_attrib
*unpack
)
1285 struct st_context
*st
= st_context(ctx
);
1286 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1287 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1288 struct pipe_resource
*texture
= stImage
->pt
;
1289 struct pipe_context
*pipe
= st
->pipe
;
1290 struct pipe_screen
*screen
= pipe
->screen
;
1291 struct pipe_surface
*surface
= NULL
;
1292 struct st_pbo_addresses addr
;
1293 enum pipe_format src_format
;
1294 const struct util_format_description
*desc
;
1295 GLenum gl_target
= texImage
->TexObject
->Target
;
1298 if (!st
->pbo
.upload_enabled
)
1301 /* From now on, we need the gallium representation of dimensions. */
1302 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1309 if (depth
!= 1 && !st
->pbo
.layers
)
1312 /* Choose the source format. Initially, we do so without checking driver
1313 * support at all because of the remapping we later perform and because
1314 * at least the Radeon driver actually supports some formats for texture
1315 * buffers which it doesn't support for regular textures. */
1316 src_format
= st_choose_matching_format(st
, 0, format
, type
, unpack
->SwapBytes
);
1321 src_format
= util_format_linear(src_format
);
1322 desc
= util_format_description(src_format
);
1324 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
)
1327 if (desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
)
1330 if (st
->pbo
.rgba_only
) {
1331 enum pipe_format orig_dst_format
= dst_format
;
1333 if (!reinterpret_formats(&src_format
, &dst_format
)) {
1337 if (dst_format
!= orig_dst_format
&&
1338 !screen
->is_format_supported(screen
, dst_format
, PIPE_TEXTURE_2D
, 0,
1339 PIPE_BIND_RENDER_TARGET
)) {
1345 !screen
->is_format_supported(screen
, src_format
, PIPE_BUFFER
, 0,
1346 PIPE_BIND_SAMPLER_VIEW
)) {
1350 /* Compute buffer addresses */
1351 addr
.xoffset
= xoffset
;
1352 addr
.yoffset
= yoffset
;
1354 addr
.height
= height
;
1356 addr
.bytes_per_pixel
= desc
->block
.bits
/ 8;
1358 if (!st_pbo_addresses_pixelstore(st
, gl_target
, dims
== 3, unpack
, pixels
,
1362 /* Set up the surface */
1364 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1365 unsigned max_layer
= util_max_layer(texture
, level
);
1367 zoffset
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1369 struct pipe_surface templ
;
1370 memset(&templ
, 0, sizeof(templ
));
1371 templ
.format
= dst_format
;
1372 templ
.u
.tex
.level
= level
;
1373 templ
.u
.tex
.first_layer
= MIN2(zoffset
, max_layer
);
1374 templ
.u
.tex
.last_layer
= MIN2(zoffset
+ depth
- 1, max_layer
);
1376 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1381 success
= try_pbo_upload_common(ctx
, surface
, &addr
, src_format
);
1383 pipe_surface_reference(&surface
, NULL
);
1389 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
1390 struct gl_texture_image
*texImage
,
1391 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1392 GLint width
, GLint height
, GLint depth
,
1393 GLenum format
, GLenum type
, const void *pixels
,
1394 const struct gl_pixelstore_attrib
*unpack
)
1396 struct st_context
*st
= st_context(ctx
);
1397 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1398 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1399 struct pipe_context
*pipe
= st
->pipe
;
1400 struct pipe_screen
*screen
= pipe
->screen
;
1401 struct pipe_resource
*dst
= stImage
->pt
;
1402 struct pipe_resource
*src
= NULL
;
1403 struct pipe_resource src_templ
;
1404 struct pipe_transfer
*transfer
;
1405 struct pipe_blit_info blit
;
1406 enum pipe_format src_format
, dst_format
;
1407 mesa_format mesa_src_format
;
1408 GLenum gl_target
= texImage
->TexObject
->Target
;
1411 unsigned dstz
= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1412 unsigned dst_level
= 0;
1414 if (stObj
->pt
== stImage
->pt
)
1415 dst_level
= texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1417 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1418 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1423 /* Try transfer_inline_write, which should be the fastest memcpy path. */
1425 !_mesa_is_bufferobj(unpack
->BufferObj
) &&
1426 _mesa_texstore_can_use_memcpy(ctx
, texImage
->_BaseFormat
,
1427 texImage
->TexFormat
, format
, type
,
1429 struct pipe_box box
;
1430 unsigned stride
, layer_stride
;
1433 stride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
1434 layer_stride
= _mesa_image_image_stride(unpack
, width
, height
, format
,
1436 data
= _mesa_image_address(dims
, unpack
, pixels
, width
, height
, format
,
1439 /* Convert to Gallium coordinates. */
1440 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1445 layer_stride
= stride
;
1448 u_box_3d(xoffset
, yoffset
, zoffset
+ dstz
, width
, height
, depth
, &box
);
1449 pipe
->transfer_inline_write(pipe
, dst
, dst_level
, 0,
1450 &box
, data
, stride
, layer_stride
);
1454 if (!st
->prefer_blit_based_texture_transfer
) {
1458 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
1459 * blit implementation in some drivers. */
1460 if (format
== GL_DEPTH_STENCIL
) {
1464 /* If the base internal format and the texture format don't match,
1465 * we can't use blit-based TexSubImage. */
1466 if (texImage
->_BaseFormat
!=
1467 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1472 /* See if the destination format is supported. */
1473 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1474 bind
= PIPE_BIND_DEPTH_STENCIL
;
1476 bind
= PIPE_BIND_RENDER_TARGET
;
1478 /* For luminance and intensity, only the red channel is stored
1479 * in the destination. */
1480 dst_format
= util_format_linear(dst
->format
);
1481 dst_format
= util_format_luminance_to_red(dst_format
);
1482 dst_format
= util_format_intensity_to_red(dst_format
);
1485 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1486 dst
->nr_samples
, bind
)) {
1490 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
1491 if (try_pbo_upload(ctx
, dims
, texImage
, format
, type
, dst_format
,
1492 xoffset
, yoffset
, zoffset
,
1493 width
, height
, depth
, pixels
, unpack
))
1497 /* See if the texture format already matches the format and type,
1498 * in which case the memcpy-based fast path will likely be used and
1499 * we don't have to blit. */
1500 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1501 type
, unpack
->SwapBytes
, NULL
)) {
1505 /* Choose the source format. */
1506 src_format
= st_choose_matching_format(st
, PIPE_BIND_SAMPLER_VIEW
,
1507 format
, type
, unpack
->SwapBytes
);
1512 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
1514 /* There is no reason to do this if we cannot use memcpy for the temporary
1515 * source texture at least. This also takes transfer ops into account,
1517 if (!_mesa_texstore_can_use_memcpy(ctx
,
1518 _mesa_get_format_base_format(mesa_src_format
),
1519 mesa_src_format
, format
, type
, unpack
)) {
1523 /* TexSubImage only sets a single cubemap face. */
1524 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1525 gl_target
= GL_TEXTURE_2D
;
1527 /* TexSubImage can specify subsets of cube map array faces
1528 * so we need to upload via 2D array instead */
1529 if (gl_target
== GL_TEXTURE_CUBE_MAP_ARRAY
) {
1530 gl_target
= GL_TEXTURE_2D_ARRAY
;
1533 /* Initialize the source texture description. */
1534 memset(&src_templ
, 0, sizeof(src_templ
));
1535 src_templ
.target
= gl_target_to_pipe(gl_target
);
1536 src_templ
.format
= src_format
;
1537 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
1538 src_templ
.usage
= PIPE_USAGE_STAGING
;
1540 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1541 &src_templ
.width0
, &src_templ
.height0
,
1542 &src_templ
.depth0
, &src_templ
.array_size
);
1544 /* Check for NPOT texture support. */
1545 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
1546 (!util_is_power_of_two(src_templ
.width0
) ||
1547 !util_is_power_of_two(src_templ
.height0
) ||
1548 !util_is_power_of_two(src_templ
.depth0
))) {
1552 /* Create the source texture. */
1553 src
= screen
->resource_create(screen
, &src_templ
);
1558 /* Map source pixels. */
1559 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
1560 format
, type
, pixels
, unpack
,
1563 /* This is a GL error. */
1564 pipe_resource_reference(&src
, NULL
);
1568 /* From now on, we need the gallium representation of dimensions. */
1569 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1576 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
1577 width
, height
, depth
, &transfer
);
1579 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1580 pipe_resource_reference(&src
, NULL
);
1584 /* Upload pixels (just memcpy). */
1586 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
1589 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
1590 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1591 /* 1D array textures.
1592 * We need to convert gallium coords to GL coords.
1594 void *src
= _mesa_image_address2d(unpack
, pixels
,
1595 width
, depth
, format
,
1597 memcpy(map
, src
, bytesPerRow
);
1600 ubyte
*slice_map
= map
;
1602 for (row
= 0; row
< (unsigned) height
; row
++) {
1603 void *src
= _mesa_image_address(dims
, unpack
, pixels
,
1604 width
, height
, format
,
1605 type
, slice
, row
, 0);
1606 memcpy(slice_map
, src
, bytesPerRow
);
1607 slice_map
+= transfer
->stride
;
1610 map
+= transfer
->layer_stride
;
1614 pipe_transfer_unmap(pipe
, transfer
);
1615 _mesa_unmap_teximage_pbo(ctx
, unpack
);
1618 memset(&blit
, 0, sizeof(blit
));
1619 blit
.src
.resource
= src
;
1621 blit
.src
.format
= src_format
;
1622 blit
.dst
.resource
= dst
;
1623 blit
.dst
.level
= dst_level
;
1624 blit
.dst
.format
= dst_format
;
1625 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
1626 blit
.dst
.box
.x
= xoffset
;
1627 blit
.dst
.box
.y
= yoffset
;
1628 blit
.dst
.box
.z
= zoffset
+ dstz
;
1629 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1630 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1631 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1632 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
1633 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1634 blit
.scissor_enable
= FALSE
;
1636 st
->pipe
->blit(st
->pipe
, &blit
);
1638 pipe_resource_reference(&src
, NULL
);
1642 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
1643 width
, height
, depth
, format
, type
, pixels
,
1648 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
1649 struct gl_texture_image
*texImage
,
1650 GLenum format
, GLenum type
, const void *pixels
,
1651 const struct gl_pixelstore_attrib
*unpack
)
1653 assert(dims
== 1 || dims
== 2 || dims
== 3);
1655 prep_teximage(ctx
, texImage
, format
, type
);
1657 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1660 /* allocate storage for texture data */
1661 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1662 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1666 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
1667 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1668 format
, type
, pixels
, unpack
);
1673 st_CompressedTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1674 struct gl_texture_image
*texImage
,
1675 GLint x
, GLint y
, GLint z
,
1676 GLsizei w
, GLsizei h
, GLsizei d
,
1677 GLenum format
, GLsizei imageSize
, const void *data
)
1679 struct st_context
*st
= st_context(ctx
);
1680 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1681 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1682 struct pipe_resource
*texture
= stImage
->pt
;
1683 struct pipe_context
*pipe
= st
->pipe
;
1684 struct pipe_screen
*screen
= pipe
->screen
;
1685 struct pipe_resource
*dst
= stImage
->pt
;
1686 struct pipe_surface
*surface
= NULL
;
1687 struct compressed_pixelstore store
;
1688 struct st_pbo_addresses addr
;
1689 enum pipe_format copy_format
;
1691 intptr_t buf_offset
;
1692 bool success
= false;
1694 /* Check basic pre-conditions for PBO upload */
1695 if (!st
->prefer_blit_based_texture_transfer
) {
1699 if (!_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
))
1702 if ((_mesa_is_format_etc2(texImage
->TexFormat
) && !st
->has_etc2
) ||
1703 (texImage
->TexFormat
== MESA_FORMAT_ETC1_RGB8
&& !st
->has_etc1
)) {
1704 /* ETC isn't supported and is represented by uncompressed formats. */
1712 if (!st
->pbo
.upload_enabled
||
1713 !screen
->get_param(screen
, PIPE_CAP_SURFACE_REINTERPRET_BLOCKS
)) {
1717 /* Choose the pipe format for the upload. */
1718 addr
.bytes_per_pixel
= util_format_get_blocksize(dst
->format
);
1719 bw
= util_format_get_blockwidth(dst
->format
);
1720 bh
= util_format_get_blockheight(dst
->format
);
1722 switch (addr
.bytes_per_pixel
) {
1724 copy_format
= PIPE_FORMAT_R16G16B16A16_UINT
;
1727 copy_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
1733 if (!screen
->is_format_supported(screen
, copy_format
, PIPE_BUFFER
, 0,
1734 PIPE_BIND_SAMPLER_VIEW
)) {
1738 if (!screen
->is_format_supported(screen
, copy_format
, dst
->target
,
1739 dst
->nr_samples
, PIPE_BIND_RENDER_TARGET
)) {
1743 /* Interpret the pixelstore settings. */
1744 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
, w
, h
, d
,
1745 &ctx
->Unpack
, &store
);
1746 assert(store
.CopyBytesPerRow
% addr
.bytes_per_pixel
== 0);
1747 assert(store
.SkipBytes
% addr
.bytes_per_pixel
== 0);
1749 /* Compute the offset into the buffer */
1750 buf_offset
= (intptr_t)data
+ store
.SkipBytes
;
1752 if (buf_offset
% addr
.bytes_per_pixel
) {
1756 buf_offset
= buf_offset
/ addr
.bytes_per_pixel
;
1758 addr
.xoffset
= x
/ bw
;
1759 addr
.yoffset
= y
/ bh
;
1760 addr
.width
= store
.CopyBytesPerRow
/ addr
.bytes_per_pixel
;
1761 addr
.height
= store
.CopyRowsPerSlice
;
1763 addr
.pixels_per_row
= store
.TotalBytesPerRow
/ addr
.bytes_per_pixel
;
1764 addr
.image_height
= store
.TotalRowsPerSlice
;
1766 if (!st_pbo_addresses_setup(st
, st_buffer_object(ctx
->Unpack
.BufferObj
)->buffer
,
1770 /* Set up the surface. */
1772 unsigned level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->TexObject
->MinLevel
+ texImage
->Level
;
1773 unsigned max_layer
= util_max_layer(texture
, level
);
1775 z
+= texImage
->Face
+ texImage
->TexObject
->MinLayer
;
1777 struct pipe_surface templ
;
1778 memset(&templ
, 0, sizeof(templ
));
1779 templ
.format
= copy_format
;
1780 templ
.u
.tex
.level
= level
;
1781 templ
.u
.tex
.first_layer
= MIN2(z
, max_layer
);
1782 templ
.u
.tex
.last_layer
= MIN2(z
+ d
- 1, max_layer
);
1784 surface
= pipe
->create_surface(pipe
, texture
, &templ
);
1789 success
= try_pbo_upload_common(ctx
, surface
, &addr
, copy_format
);
1791 pipe_surface_reference(&surface
, NULL
);
1797 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1799 format
, imageSize
, data
);
1803 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
1804 struct gl_texture_image
*texImage
,
1805 GLsizei imageSize
, const void *data
)
1807 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
1809 /* only 2D and 3D compressed images are supported at this time */
1811 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1815 /* This is pretty simple, because unlike the general texstore path we don't
1816 * have to worry about the usual image unpacking or image transfer
1820 assert(texImage
->Width
> 0);
1821 assert(texImage
->Height
> 0);
1822 assert(texImage
->Depth
> 0);
1824 /* allocate storage for texture data */
1825 if (!st_AllocTextureImageBuffer(ctx
, texImage
)) {
1826 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1830 st_CompressedTexSubImage(ctx
, dims
, texImage
,
1832 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1833 texImage
->TexFormat
,
1841 * Called via ctx->Driver.GetTexSubImage()
1843 * This uses a blit to copy the texture to a texture format which matches
1844 * the format and type combo and then a fast read-back is done using memcpy.
1845 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
1846 * a format which matches the swizzling.
1848 * If such a format isn't available, it falls back to _mesa_GetTexImage_sw.
1850 * NOTE: Drivers usually do a blit to convert between tiled and linear
1851 * texture layouts during texture uploads/downloads, so the blit
1852 * we do here should be free in such cases.
1855 st_GetTexSubImage(struct gl_context
* ctx
,
1856 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1857 GLsizei width
, GLsizei height
, GLint depth
,
1858 GLenum format
, GLenum type
, void * pixels
,
1859 struct gl_texture_image
*texImage
)
1861 struct st_context
*st
= st_context(ctx
);
1862 struct pipe_context
*pipe
= st
->pipe
;
1863 struct pipe_screen
*screen
= pipe
->screen
;
1864 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1865 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1866 struct pipe_resource
*src
= stObj
->pt
;
1867 struct pipe_resource
*dst
= NULL
;
1868 struct pipe_resource dst_templ
;
1869 enum pipe_format dst_format
, src_format
;
1870 mesa_format mesa_format
;
1871 GLenum gl_target
= texImage
->TexObject
->Target
;
1872 enum pipe_texture_target pipe_target
;
1873 struct pipe_blit_info blit
;
1874 unsigned bind
= PIPE_BIND_TRANSFER_READ
;
1875 struct pipe_transfer
*tex_xfer
;
1877 boolean done
= FALSE
;
1879 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
1880 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
1882 if (!st
->prefer_blit_based_texture_transfer
&&
1883 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
1884 /* Try to avoid the fallback if we're doing texture decompression here */
1888 /* Handle non-finalized textures. */
1889 if (!stImage
->pt
|| stImage
->pt
!= stObj
->pt
|| !src
) {
1893 /* XXX Fallback to _mesa_GetTexImage_sw for depth-stencil formats
1894 * due to an incomplete stencil blit implementation in some drivers. */
1895 if (format
== GL_DEPTH_STENCIL
|| format
== GL_STENCIL_INDEX
) {
1899 /* If the base internal format and the texture format don't match, we have
1900 * to fall back to _mesa_GetTexImage_sw. */
1901 if (texImage
->_BaseFormat
!=
1902 _mesa_get_format_base_format(texImage
->TexFormat
)) {
1906 /* See if the texture format already matches the format and type,
1907 * in which case the memcpy-based fast path will be used. */
1908 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
1909 type
, ctx
->Pack
.SwapBytes
, NULL
)) {
1913 /* Convert the source format to what is expected by GetTexImage
1914 * and see if it's supported.
1916 * This only applies to glGetTexImage:
1917 * - Luminance must be returned as (L,0,0,1).
1918 * - Luminance alpha must be returned as (L,0,0,A).
1919 * - Intensity must be returned as (I,0,0,1)
1921 if (stObj
->surface_based
)
1922 src_format
= util_format_linear(stObj
->surface_format
);
1924 src_format
= util_format_linear(src
->format
);
1925 src_format
= util_format_luminance_to_red(src_format
);
1926 src_format
= util_format_intensity_to_red(src_format
);
1929 !screen
->is_format_supported(screen
, src_format
, src
->target
,
1931 PIPE_BIND_SAMPLER_VIEW
)) {
1935 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
1936 bind
|= PIPE_BIND_DEPTH_STENCIL
;
1938 bind
|= PIPE_BIND_RENDER_TARGET
;
1940 /* GetTexImage only returns a single face for cubemaps. */
1941 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
1942 gl_target
= GL_TEXTURE_2D
;
1944 pipe_target
= gl_target_to_pipe(gl_target
);
1946 /* Choose the destination format by finding the best match
1947 * for the format+type combo. */
1948 dst_format
= st_choose_matching_format(st
, bind
, format
, type
,
1949 ctx
->Pack
.SwapBytes
);
1951 if (dst_format
== PIPE_FORMAT_NONE
) {
1952 GLenum dst_glformat
;
1954 /* Fall back to _mesa_GetTexImage_sw except for compressed formats,
1955 * where decompression with a blit is always preferred. */
1956 if (!util_format_is_compressed(src
->format
)) {
1960 /* Set the appropriate format for the decompressed texture.
1961 * Luminance and sRGB formats shouldn't appear here.*/
1962 switch (src_format
) {
1963 case PIPE_FORMAT_DXT1_RGB
:
1964 case PIPE_FORMAT_DXT1_RGBA
:
1965 case PIPE_FORMAT_DXT3_RGBA
:
1966 case PIPE_FORMAT_DXT5_RGBA
:
1967 case PIPE_FORMAT_RGTC1_UNORM
:
1968 case PIPE_FORMAT_RGTC2_UNORM
:
1969 case PIPE_FORMAT_ETC1_RGB8
:
1970 case PIPE_FORMAT_BPTC_RGBA_UNORM
:
1971 dst_glformat
= GL_RGBA8
;
1973 case PIPE_FORMAT_RGTC1_SNORM
:
1974 case PIPE_FORMAT_RGTC2_SNORM
:
1975 if (!ctx
->Extensions
.EXT_texture_snorm
)
1977 dst_glformat
= GL_RGBA8_SNORM
;
1979 case PIPE_FORMAT_BPTC_RGB_FLOAT
:
1980 case PIPE_FORMAT_BPTC_RGB_UFLOAT
:
1981 if (!ctx
->Extensions
.ARB_texture_float
)
1983 dst_glformat
= GL_RGBA32F
;
1990 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
1991 pipe_target
, 0, bind
, FALSE
);
1993 if (dst_format
== PIPE_FORMAT_NONE
) {
1994 /* unable to get an rgba format!?! */
1999 /* create the destination texture of size (width X height X depth) */
2000 memset(&dst_templ
, 0, sizeof(dst_templ
));
2001 dst_templ
.target
= pipe_target
;
2002 dst_templ
.format
= dst_format
;
2003 dst_templ
.bind
= bind
;
2004 dst_templ
.usage
= PIPE_USAGE_STAGING
;
2006 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
2007 &dst_templ
.width0
, &dst_templ
.height0
,
2008 &dst_templ
.depth0
, &dst_templ
.array_size
);
2010 dst
= screen
->resource_create(screen
, &dst_templ
);
2015 /* From now on, we need the gallium representation of dimensions. */
2016 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2023 assert(texImage
->Face
== 0 ||
2024 texImage
->TexObject
->MinLayer
== 0 ||
2027 memset(&blit
, 0, sizeof(blit
));
2028 blit
.src
.resource
= src
;
2029 blit
.src
.level
= texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2030 blit
.src
.format
= src_format
;
2031 blit
.dst
.resource
= dst
;
2033 blit
.dst
.format
= dst
->format
;
2034 blit
.src
.box
.x
= xoffset
;
2036 blit
.src
.box
.y
= yoffset
;
2038 blit
.src
.box
.z
= texImage
->Face
+ texImage
->TexObject
->MinLayer
+ zoffset
;
2040 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
2041 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
2042 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
2043 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
2044 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2045 blit
.scissor_enable
= FALSE
;
2047 /* blit/render/decompress */
2048 st
->pipe
->blit(st
->pipe
, &blit
);
2050 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
2052 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
2053 0, 0, 0, width
, height
, depth
, &tex_xfer
);
2058 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
2060 /* copy/pack data into user buffer */
2061 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
2062 ctx
->Pack
.SwapBytes
, NULL
)) {
2064 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
2067 for (slice
= 0; slice
< depth
; slice
++) {
2068 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2069 /* 1D array textures.
2070 * We need to convert gallium coords to GL coords.
2072 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2073 width
, depth
, format
,
2075 memcpy(dest
, map
, bytesPerRow
);
2078 ubyte
*slice_map
= map
;
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);
2084 memcpy(dest
, slice_map
, bytesPerRow
);
2085 slice_map
+= tex_xfer
->stride
;
2088 map
+= tex_xfer
->layer_stride
;
2092 /* format translation via floats */
2095 uint32_t dstMesaFormat
;
2096 int dstStride
, srcStride
;
2098 assert(util_format_is_compressed(src
->format
));
2100 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
2105 if (ST_DEBUG
& DEBUG_FALLBACK
)
2106 debug_printf("%s: fallback format translation\n", __func__
);
2108 dstMesaFormat
= _mesa_format_from_format_and_type(format
, type
);
2109 dstStride
= _mesa_image_row_stride(&ctx
->Pack
, width
, format
, type
);
2110 srcStride
= 4 * width
* sizeof(GLfloat
);
2111 for (slice
= 0; slice
< depth
; slice
++) {
2112 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
2113 /* 1D array textures.
2114 * We need to convert gallium coords to GL coords.
2116 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2117 width
, depth
, format
,
2120 /* get float[4] rgba row from surface */
2121 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, 1,
2124 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2125 rgba
, RGBA32_FLOAT
, srcStride
,
2129 for (row
= 0; row
< height
; row
++) {
2130 void *dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
2131 width
, height
, format
,
2132 type
, slice
, row
, 0);
2134 /* get float[4] rgba row from surface */
2135 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, row
, width
, 1,
2138 _mesa_format_convert(dest
, dstMesaFormat
, dstStride
,
2139 rgba
, RGBA32_FLOAT
, srcStride
,
2143 map
+= tex_xfer
->layer_stride
;
2152 pipe_transfer_unmap(pipe
, tex_xfer
);
2154 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
2155 pipe_resource_reference(&dst
, NULL
);
2159 _mesa_GetTexSubImage_sw(ctx
, xoffset
, yoffset
, zoffset
,
2160 width
, height
, depth
,
2161 format
, type
, pixels
, texImage
);
2167 * Do a CopyTexSubImage operation using a read transfer from the source,
2168 * a write transfer to the destination and get_tile()/put_tile() to access
2169 * the pixels/texels.
2171 * Note: srcY=0=TOP of renderbuffer
2174 fallback_copy_texsubimage(struct gl_context
*ctx
,
2175 struct st_renderbuffer
*strb
,
2176 struct st_texture_image
*stImage
,
2178 GLint destX
, GLint destY
, GLint slice
,
2179 GLint srcX
, GLint srcY
,
2180 GLsizei width
, GLsizei height
)
2182 struct st_context
*st
= st_context(ctx
);
2183 struct pipe_context
*pipe
= st
->pipe
;
2184 struct pipe_transfer
*src_trans
;
2186 enum pipe_transfer_usage transfer_usage
;
2188 unsigned dst_width
= width
;
2189 unsigned dst_height
= height
;
2190 unsigned dst_depth
= 1;
2191 struct pipe_transfer
*transfer
;
2193 if (ST_DEBUG
& DEBUG_FALLBACK
)
2194 debug_printf("%s: fallback processing\n", __func__
);
2196 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2197 srcY
= strb
->Base
.Height
- srcY
- height
;
2200 map
= pipe_transfer_map(pipe
,
2202 strb
->surface
->u
.tex
.level
,
2203 strb
->surface
->u
.tex
.first_layer
,
2206 width
, height
, &src_trans
);
2208 if ((baseFormat
== GL_DEPTH_COMPONENT
||
2209 baseFormat
== GL_DEPTH_STENCIL
) &&
2210 util_format_is_depth_and_stencil(stImage
->pt
->format
))
2211 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
2213 transfer_usage
= PIPE_TRANSFER_WRITE
;
2215 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
2216 destX
, destY
, slice
,
2217 dst_width
, dst_height
, dst_depth
,
2220 if (baseFormat
== GL_DEPTH_COMPONENT
||
2221 baseFormat
== GL_DEPTH_STENCIL
) {
2222 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
2223 ctx
->Pixel
.DepthBias
!= 0.0F
);
2227 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
2228 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2237 data
= malloc(width
* sizeof(uint
));
2240 /* To avoid a large temp memory allocation, do copy row by row */
2241 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
2242 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
2244 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
2247 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2248 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
2249 0, 0, width
, 1, data
);
2252 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
2257 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
2265 malloc(width
* height
* 4 * sizeof(GLfloat
));
2267 if (tempSrc
&& texDest
) {
2268 const GLint dims
= 2;
2270 struct gl_texture_image
*texImage
= &stImage
->base
;
2271 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
2273 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
2274 unpack
.Invert
= GL_TRUE
;
2277 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
2278 dstRowStride
= transfer
->layer_stride
;
2281 dstRowStride
= transfer
->stride
;
2284 /* get float/RGBA image from framebuffer */
2285 /* XXX this usually involves a lot of int/float conversion.
2286 * try to avoid that someday.
2288 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
2289 util_format_linear(strb
->texture
->format
),
2292 /* Store into texture memory.
2293 * Note that this does some special things such as pixel transfer
2294 * ops and format conversion. In particular, if the dest tex format
2295 * is actually RGBA but the user created the texture as GL_RGB we
2296 * need to fill-in/override the alpha channel with 1.0.
2298 _mesa_texstore(ctx
, dims
,
2299 texImage
->_BaseFormat
,
2300 texImage
->TexFormat
,
2304 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
2308 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
2314 st_texture_image_unmap(st
, stImage
, slice
);
2315 pipe
->transfer_unmap(pipe
, src_trans
);
2320 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
2321 * Note that the region to copy has already been clipped so we know we
2322 * won't read from outside the source renderbuffer's bounds.
2324 * Note: srcY=0=Bottom of renderbuffer (GL convention)
2327 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
2328 struct gl_texture_image
*texImage
,
2329 GLint destX
, GLint destY
, GLint slice
,
2330 struct gl_renderbuffer
*rb
,
2331 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
2333 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2334 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
2335 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
2336 struct st_context
*st
= st_context(ctx
);
2337 struct pipe_context
*pipe
= st
->pipe
;
2338 struct pipe_screen
*screen
= pipe
->screen
;
2339 struct pipe_blit_info blit
;
2340 enum pipe_format dst_format
;
2341 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
2345 assert(!_mesa_is_format_etc2(texImage
->TexFormat
) &&
2346 texImage
->TexFormat
!= MESA_FORMAT_ETC1_RGB8
);
2348 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
2349 debug_printf("%s: null strb or stImage\n", __func__
);
2353 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
2354 texImage
->TexFormat
)) {
2358 /* The base internal format must match the mesa format, so make sure
2359 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
2361 if (texImage
->_BaseFormat
!=
2362 _mesa_get_format_base_format(texImage
->TexFormat
) ||
2363 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
2367 /* Choose the destination format to match the TexImage behavior. */
2368 dst_format
= util_format_linear(stImage
->pt
->format
);
2369 dst_format
= util_format_luminance_to_red(dst_format
);
2370 dst_format
= util_format_intensity_to_red(dst_format
);
2372 /* See if the destination format is supported. */
2373 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
2374 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
2375 bind
= PIPE_BIND_DEPTH_STENCIL
;
2378 bind
= PIPE_BIND_RENDER_TARGET
;
2382 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
2383 stImage
->pt
->nr_samples
, bind
)) {
2387 /* Y flipping for the main framebuffer. */
2389 srcY1
= strb
->Base
.Height
- srcY
- height
;
2390 srcY0
= srcY1
+ height
;
2394 srcY1
= srcY0
+ height
;
2397 /* Blit the texture.
2398 * This supports flipping, format conversions, and downsampling.
2400 memset(&blit
, 0, sizeof(blit
));
2401 blit
.src
.resource
= strb
->texture
;
2402 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
2403 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
2404 blit
.src
.box
.x
= srcX
;
2405 blit
.src
.box
.y
= srcY0
;
2406 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
2407 blit
.src
.box
.width
= width
;
2408 blit
.src
.box
.height
= srcY1
- srcY0
;
2409 blit
.src
.box
.depth
= 1;
2410 blit
.dst
.resource
= stImage
->pt
;
2411 blit
.dst
.format
= dst_format
;
2412 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
+ texImage
->TexObject
->MinLevel
;
2413 blit
.dst
.box
.x
= destX
;
2414 blit
.dst
.box
.y
= destY
;
2415 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
+ texImage
->TexObject
->MinLayer
;
2416 blit
.dst
.box
.width
= width
;
2417 blit
.dst
.box
.height
= height
;
2418 blit
.dst
.box
.depth
= 1;
2419 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
2420 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
2421 pipe
->blit(pipe
, &blit
);
2425 /* software fallback */
2426 fallback_copy_texsubimage(ctx
,
2427 strb
, stImage
, texImage
->_BaseFormat
,
2428 destX
, destY
, slice
,
2429 srcX
, srcY
, width
, height
);
2434 * Copy image data from stImage into the texture object 'stObj' at level
2438 copy_image_data_to_texture(struct st_context
*st
,
2439 struct st_texture_object
*stObj
,
2441 struct st_texture_image
*stImage
)
2445 const struct gl_texture_image
*dstImage
=
2446 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
2448 assert(dstImage
->Width
== stImage
->base
.Width
);
2449 assert(dstImage
->Height
== stImage
->base
.Height
);
2450 assert(dstImage
->Depth
== stImage
->base
.Depth
);
2454 /* Copy potentially with the blitter:
2457 if (stImage
->pt
->last_level
== 0)
2460 src_level
= stImage
->base
.Level
;
2462 assert(src_level
<= stImage
->pt
->last_level
);
2463 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
2464 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
2465 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
2466 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
2467 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
2468 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
2470 st_texture_image_copy(st
->pipe
,
2471 stObj
->pt
, dstLevel
, /* dest texture, level */
2472 stImage
->pt
, src_level
, /* src texture, level */
2473 stImage
->base
.Face
);
2475 pipe_resource_reference(&stImage
->pt
, NULL
);
2477 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2482 * Called during state validation. When this function is finished,
2483 * the texture object should be ready for rendering.
2484 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
2487 st_finalize_texture(struct gl_context
*ctx
,
2488 struct pipe_context
*pipe
,
2489 struct gl_texture_object
*tObj
)
2491 struct st_context
*st
= st_context(ctx
);
2492 struct st_texture_object
*stObj
= st_texture_object(tObj
);
2493 const GLuint nr_faces
= _mesa_num_tex_faces(stObj
->base
.Target
);
2495 const struct st_texture_image
*firstImage
;
2496 enum pipe_format firstImageFormat
;
2497 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
2499 if (tObj
->Immutable
)
2502 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
2503 /* The texture is complete and we know exactly how many mipmap levels
2504 * are present/needed. This is conditional because we may be called
2505 * from the st_generate_mipmap() function when the texture object is
2506 * incomplete. In that case, we'll have set stObj->lastLevel before
2509 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
2510 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
2511 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
2513 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
2516 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
2517 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
2520 pipe_resource_reference(&stObj
->pt
, NULL
);
2521 st_texture_release_all_sampler_views(st
, stObj
);
2525 if (st_obj
->buffer
!= stObj
->pt
) {
2526 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
2527 st_texture_release_all_sampler_views(st
, stObj
);
2528 stObj
->width0
= stObj
->pt
->width0
/ _mesa_get_format_bytes(tObj
->_BufferObjectFormat
);
2536 firstImage
= st_texture_image_const(_mesa_base_tex_image(&stObj
->base
));
2539 /* If both firstImage and stObj point to a texture which can contain
2540 * all active images, favour firstImage. Note that because of the
2541 * completeness requirement, we know that the image dimensions
2544 if (firstImage
->pt
&&
2545 firstImage
->pt
!= stObj
->pt
&&
2546 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
2547 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
2548 st_texture_release_all_sampler_views(st
, stObj
);
2551 /* If this texture comes from a window system, there is nothing else to do. */
2552 if (stObj
->surface_based
) {
2556 /* Find gallium format for the Mesa texture */
2558 st_mesa_format_to_pipe_format(st
, firstImage
->base
.TexFormat
);
2560 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
2562 GLuint width
, height
, depth
;
2563 if (!guess_base_level_size(stObj
->base
.Target
,
2564 firstImage
->base
.Width2
,
2565 firstImage
->base
.Height2
,
2566 firstImage
->base
.Depth2
,
2567 firstImage
->base
.Level
,
2568 &width
, &height
, &depth
)) {
2569 width
= stObj
->width0
;
2570 height
= stObj
->height0
;
2571 depth
= stObj
->depth0
;
2573 /* The width/height/depth may have been previously reset in
2574 * guess_and_alloc_texture. */
2575 stObj
->width0
= width
;
2576 stObj
->height0
= height
;
2577 stObj
->depth0
= depth
;
2579 /* convert GL dims to Gallium dims */
2580 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
2581 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2582 ptNumSamples
= firstImage
->base
.NumSamples
;
2585 /* If we already have a gallium texture, check that it matches the texture
2586 * object's format, target, size, num_levels, etc.
2589 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
2590 stObj
->pt
->format
!= firstImageFormat
||
2591 stObj
->pt
->last_level
< stObj
->lastLevel
||
2592 stObj
->pt
->width0
!= ptWidth
||
2593 stObj
->pt
->height0
!= ptHeight
||
2594 stObj
->pt
->depth0
!= ptDepth
||
2595 stObj
->pt
->nr_samples
!= ptNumSamples
||
2596 stObj
->pt
->array_size
!= ptLayers
)
2598 /* The gallium texture does not match the Mesa texture so delete the
2599 * gallium texture now. We'll make a new one below.
2601 pipe_resource_reference(&stObj
->pt
, NULL
);
2602 st_texture_release_all_sampler_views(st
, stObj
);
2603 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
2607 /* May need to create a new gallium texture:
2610 GLuint bindings
= default_bindings(st
, firstImageFormat
);
2612 stObj
->pt
= st_texture_create(st
,
2613 gl_target_to_pipe(stObj
->base
.Target
),
2619 ptLayers
, ptNumSamples
,
2623 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
2628 /* Pull in any images not in the object's texture:
2630 for (face
= 0; face
< nr_faces
; face
++) {
2632 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
2633 struct st_texture_image
*stImage
=
2634 st_texture_image(stObj
->base
.Image
[face
][level
]);
2636 /* Need to import images in main memory or held in other textures.
2638 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
2639 GLuint height
= stObj
->height0
;
2640 GLuint depth
= stObj
->depth0
;
2642 if (stObj
->base
.Target
!= GL_TEXTURE_1D_ARRAY
)
2643 height
= u_minify(height
, level
);
2644 if (stObj
->base
.Target
== GL_TEXTURE_3D
)
2645 depth
= u_minify(depth
, level
);
2648 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
2649 stImage
->base
.Height
== height
&&
2650 stImage
->base
.Depth
== depth
)) {
2651 /* src image fits expected dest mipmap level size */
2652 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
2663 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
2664 * for a whole mipmap stack.
2667 st_AllocTextureStorage(struct gl_context
*ctx
,
2668 struct gl_texture_object
*texObj
,
2669 GLsizei levels
, GLsizei width
,
2670 GLsizei height
, GLsizei depth
)
2672 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2673 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
2674 struct st_context
*st
= st_context(ctx
);
2675 struct st_texture_object
*stObj
= st_texture_object(texObj
);
2676 struct pipe_screen
*screen
= st
->pipe
->screen
;
2677 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
2678 enum pipe_format fmt
;
2680 GLuint num_samples
= texImage
->NumSamples
;
2684 /* Save the level=0 dimensions */
2685 stObj
->width0
= width
;
2686 stObj
->height0
= height
;
2687 stObj
->depth0
= depth
;
2688 stObj
->lastLevel
= levels
- 1;
2690 fmt
= st_mesa_format_to_pipe_format(st
, texImage
->TexFormat
);
2692 bindings
= default_bindings(st
, fmt
);
2694 /* Raise the sample count if the requested one is unsupported. */
2695 if (num_samples
> 1) {
2696 boolean found
= FALSE
;
2698 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
2699 if (screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
2701 PIPE_BIND_SAMPLER_VIEW
)) {
2702 /* Update the sample count in gl_texture_image as well. */
2703 texImage
->NumSamples
= num_samples
;
2714 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
2715 width
, height
, depth
,
2716 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
2718 stObj
->pt
= st_texture_create(st
,
2719 gl_target_to_pipe(texObj
->Target
),
2725 ptLayers
, num_samples
,
2730 /* Set image resource pointers */
2731 for (level
= 0; level
< levels
; level
++) {
2733 for (face
= 0; face
< numFaces
; face
++) {
2734 struct st_texture_image
*stImage
=
2735 st_texture_image(texObj
->Image
[face
][level
]);
2736 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
2745 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
2746 GLint level
, mesa_format format
,
2747 GLint width
, GLint height
,
2748 GLint depth
, GLint border
)
2750 struct st_context
*st
= st_context(ctx
);
2751 struct pipe_context
*pipe
= st
->pipe
;
2753 if (width
== 0 || height
== 0 || depth
== 0) {
2754 /* zero-sized images are legal, and always fit! */
2758 if (pipe
->screen
->can_create_resource
) {
2759 /* Ask the gallium driver if the texture is too large */
2760 struct gl_texture_object
*texObj
=
2761 _mesa_get_current_tex_object(ctx
, target
);
2762 struct pipe_resource pt
;
2764 /* Setup the pipe_resource object
2766 memset(&pt
, 0, sizeof(pt
));
2768 pt
.target
= gl_target_to_pipe(target
);
2769 pt
.format
= st_mesa_format_to_pipe_format(st
, format
);
2771 st_gl_texture_dims_to_pipe_dims(target
,
2772 width
, height
, depth
,
2773 &pt
.width0
, &pt
.height0
,
2774 &pt
.depth0
, &pt
.array_size
);
2776 if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
2777 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
2778 /* assume just one mipmap level */
2782 /* assume a full set of mipmaps */
2783 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
2786 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
2789 /* Use core Mesa fallback */
2790 return _mesa_test_proxy_teximage(ctx
, target
, level
, format
,
2791 width
, height
, depth
, border
);
2796 st_TextureView(struct gl_context
*ctx
,
2797 struct gl_texture_object
*texObj
,
2798 struct gl_texture_object
*origTexObj
)
2800 struct st_texture_object
*orig
= st_texture_object(origTexObj
);
2801 struct st_texture_object
*tex
= st_texture_object(texObj
);
2802 struct gl_texture_image
*image
= texObj
->Image
[0][0];
2804 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
2805 const int numLevels
= texObj
->NumLevels
;
2810 pipe_resource_reference(&tex
->pt
, orig
->pt
);
2812 /* Set image resource pointers */
2813 for (level
= 0; level
< numLevels
; level
++) {
2814 for (face
= 0; face
< numFaces
; face
++) {
2815 struct st_texture_image
*stImage
=
2816 st_texture_image(texObj
->Image
[face
][level
]);
2817 pipe_resource_reference(&stImage
->pt
, tex
->pt
);
2821 tex
->surface_based
= GL_TRUE
;
2822 tex
->surface_format
=
2823 st_mesa_format_to_pipe_format(st_context(ctx
), image
->TexFormat
);
2825 tex
->width0
= image
->Width
;
2826 tex
->height0
= image
->Height
;
2827 tex
->depth0
= image
->Depth
;
2828 tex
->lastLevel
= numLevels
- 1;
2834 st_ClearTexSubImage(struct gl_context
*ctx
,
2835 struct gl_texture_image
*texImage
,
2836 GLint xoffset
, GLint yoffset
, GLint zoffset
,
2837 GLsizei width
, GLsizei height
, GLsizei depth
,
2838 const void *clearValue
)
2840 static const char zeros
[16] = {0};
2841 struct st_texture_image
*stImage
= st_texture_image(texImage
);
2842 struct pipe_resource
*pt
= stImage
->pt
;
2843 struct st_context
*st
= st_context(ctx
);
2844 struct pipe_context
*pipe
= st
->pipe
;
2845 unsigned level
= texImage
->Level
;
2846 struct pipe_box box
;
2851 u_box_3d(xoffset
, yoffset
, zoffset
+ texImage
->Face
,
2852 width
, height
, depth
, &box
);
2853 if (texImage
->TexObject
->Immutable
) {
2854 level
+= texImage
->TexObject
->MinLevel
;
2855 box
.z
+= texImage
->TexObject
->MinLayer
;
2858 pipe
->clear_texture(pipe
, pt
, level
, &box
, clearValue
? clearValue
: zeros
);
2862 st_init_texture_functions(struct dd_function_table
*functions
)
2864 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
2865 functions
->QueryInternalFormat
= st_QueryInternalFormat
;
2866 functions
->TexImage
= st_TexImage
;
2867 functions
->TexSubImage
= st_TexSubImage
;
2868 functions
->CompressedTexSubImage
= st_CompressedTexSubImage
;
2869 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
2870 functions
->GenerateMipmap
= st_generate_mipmap
;
2872 functions
->GetTexSubImage
= st_GetTexSubImage
;
2874 /* compressed texture functions */
2875 functions
->CompressedTexImage
= st_CompressedTexImage
;
2876 functions
->GetCompressedTexSubImage
= _mesa_GetCompressedTexSubImage_sw
;
2878 functions
->NewTextureObject
= st_NewTextureObject
;
2879 functions
->NewTextureImage
= st_NewTextureImage
;
2880 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
2881 functions
->DeleteTexture
= st_DeleteTextureObject
;
2882 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
2883 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
2884 functions
->MapTextureImage
= st_MapTextureImage
;
2885 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
2887 /* XXX Temporary until we can query pipe's texture sizes */
2888 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
2890 functions
->AllocTextureStorage
= st_AllocTextureStorage
;
2891 functions
->TextureView
= st_TextureView
;
2892 functions
->ClearTexSubImage
= st_ClearTexSubImage
;