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 **************************************************************************/
28 #include "main/bufferobj.h"
29 #include "main/enums.h"
30 #include "main/fbobject.h"
31 #include "main/formats.h"
32 #include "main/image.h"
33 #include "main/imports.h"
34 #include "main/macros.h"
35 #include "main/mipmap.h"
36 #include "main/pack.h"
38 #include "main/pixeltransfer.h"
39 #include "main/texcompress.h"
40 #include "main/texcompress_etc.h"
41 #include "main/texgetimage.h"
42 #include "main/teximage.h"
43 #include "main/texobj.h"
44 #include "main/texstore.h"
46 #include "state_tracker/st_debug.h"
47 #include "state_tracker/st_context.h"
48 #include "state_tracker/st_cb_fbo.h"
49 #include "state_tracker/st_cb_flush.h"
50 #include "state_tracker/st_cb_texture.h"
51 #include "state_tracker/st_cb_bufferobjects.h"
52 #include "state_tracker/st_format.h"
53 #include "state_tracker/st_texture.h"
54 #include "state_tracker/st_gen_mipmap.h"
55 #include "state_tracker/st_atom.h"
57 #include "pipe/p_context.h"
58 #include "pipe/p_defines.h"
59 #include "util/u_inlines.h"
60 #include "pipe/p_shader_tokens.h"
61 #include "util/u_tile.h"
62 #include "util/u_format.h"
63 #include "util/u_surface.h"
64 #include "util/u_sampler.h"
65 #include "util/u_math.h"
66 #include "util/u_box.h"
68 #define DBG if (0) printf
71 enum pipe_texture_target
72 gl_target_to_pipe(GLenum target
)
76 case GL_PROXY_TEXTURE_1D
:
77 return PIPE_TEXTURE_1D
;
79 case GL_PROXY_TEXTURE_2D
:
80 case GL_TEXTURE_EXTERNAL_OES
:
81 case GL_TEXTURE_2D_MULTISAMPLE
:
82 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
83 return PIPE_TEXTURE_2D
;
84 case GL_TEXTURE_RECTANGLE_NV
:
85 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
86 return PIPE_TEXTURE_RECT
;
88 case GL_PROXY_TEXTURE_3D
:
89 return PIPE_TEXTURE_3D
;
90 case GL_TEXTURE_CUBE_MAP_ARB
:
91 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
92 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
93 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
94 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
95 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
96 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
97 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
98 return PIPE_TEXTURE_CUBE
;
99 case GL_TEXTURE_1D_ARRAY_EXT
:
100 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
101 return PIPE_TEXTURE_1D_ARRAY
;
102 case GL_TEXTURE_2D_ARRAY_EXT
:
103 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
104 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
105 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
106 return PIPE_TEXTURE_2D_ARRAY
;
107 case GL_TEXTURE_BUFFER
:
109 case GL_TEXTURE_CUBE_MAP_ARRAY
:
110 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
111 return PIPE_TEXTURE_CUBE_ARRAY
;
119 /** called via ctx->Driver.NewTextureImage() */
120 static struct gl_texture_image
*
121 st_NewTextureImage(struct gl_context
* ctx
)
123 DBG("%s\n", __FUNCTION__
);
125 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
129 /** called via ctx->Driver.DeleteTextureImage() */
131 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
133 /* nothing special (yet) for st_texture_image */
134 _mesa_delete_texture_image(ctx
, img
);
138 /** called via ctx->Driver.NewTextureObject() */
139 static struct gl_texture_object
*
140 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
142 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
144 DBG("%s\n", __FUNCTION__
);
145 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
150 /** called via ctx->Driver.DeleteTextureObject() */
152 st_DeleteTextureObject(struct gl_context
*ctx
,
153 struct gl_texture_object
*texObj
)
155 struct st_texture_object
*stObj
= st_texture_object(texObj
);
157 pipe_resource_reference(&stObj
->pt
, NULL
);
158 st_texture_release_all_sampler_views(stObj
);
159 st_texture_free_sampler_views(stObj
);
160 _mesa_delete_texture_object(ctx
, texObj
);
164 /** called via ctx->Driver.FreeTextureImageBuffer() */
166 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
167 struct gl_texture_image
*texImage
)
169 struct st_texture_image
*stImage
= st_texture_image(texImage
);
171 DBG("%s\n", __FUNCTION__
);
174 pipe_resource_reference(&stImage
->pt
, NULL
);
177 _mesa_align_free(stImage
->TexData
);
178 stImage
->TexData
= NULL
;
180 free(stImage
->transfer
);
181 stImage
->transfer
= NULL
;
182 stImage
->num_transfers
= 0;
186 /** called via ctx->Driver.MapTextureImage() */
188 st_MapTextureImage(struct gl_context
*ctx
,
189 struct gl_texture_image
*texImage
,
190 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
192 GLubyte
**mapOut
, GLint
*rowStrideOut
)
194 struct st_context
*st
= st_context(ctx
);
195 struct st_texture_image
*stImage
= st_texture_image(texImage
);
198 struct pipe_transfer
*transfer
;
201 if (mode
& GL_MAP_READ_BIT
)
202 pipeMode
|= PIPE_TRANSFER_READ
;
203 if (mode
& GL_MAP_WRITE_BIT
)
204 pipeMode
|= PIPE_TRANSFER_WRITE
;
205 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
206 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
208 map
= st_texture_image_map(st
, stImage
, pipeMode
, x
, y
, slice
, w
, h
, 1,
211 if (_mesa_is_format_etc2(texImage
->TexFormat
)) {
212 /* ETC isn't supported by gallium and it's represented
213 * by uncompressed formats. Only write transfers with precompressed
214 * data are supported by ES3, which makes this really simple.
216 * Just create a temporary storage where the ETC texture will
217 * be stored. It will be decompressed in the Unmap function.
219 unsigned z
= transfer
->box
.z
;
220 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
222 itransfer
->temp_data
=
223 malloc(_mesa_format_image_size(texImage
->TexFormat
, w
, h
, 1));
224 itransfer
->temp_stride
=
225 _mesa_format_row_stride(texImage
->TexFormat
, w
);
226 itransfer
->map
= map
;
228 *mapOut
= itransfer
->temp_data
;
229 *rowStrideOut
= itransfer
->temp_stride
;
232 /* supported mapping */
234 *rowStrideOut
= transfer
->stride
;
244 /** called via ctx->Driver.UnmapTextureImage() */
246 st_UnmapTextureImage(struct gl_context
*ctx
,
247 struct gl_texture_image
*texImage
,
250 struct st_context
*st
= st_context(ctx
);
251 struct st_texture_image
*stImage
= st_texture_image(texImage
);
253 if (_mesa_is_format_etc2(texImage
->TexFormat
)) {
254 /* Decompress the ETC texture to the mapped one. */
255 unsigned z
= slice
+ stImage
->base
.Face
;
256 struct st_texture_image_transfer
*itransfer
= &stImage
->transfer
[z
];
257 struct pipe_transfer
*transfer
= itransfer
->transfer
;
259 assert(z
== transfer
->box
.z
);
261 _mesa_unpack_etc2_format(itransfer
->map
, transfer
->stride
,
262 itransfer
->temp_data
, itransfer
->temp_stride
,
263 transfer
->box
.width
, transfer
->box
.height
,
264 texImage
->TexFormat
);
266 free(itransfer
->temp_data
);
267 itransfer
->temp_data
= NULL
;
268 itransfer
->temp_stride
= 0;
272 st_texture_image_unmap(st
, stImage
, slice
);
277 * Return default texture resource binding bitmask for the given format.
280 default_bindings(struct st_context
*st
, enum pipe_format format
)
282 struct pipe_screen
*screen
= st
->pipe
->screen
;
283 const unsigned target
= PIPE_TEXTURE_2D
;
286 if (util_format_is_depth_or_stencil(format
))
287 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
289 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
291 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
295 format
= util_format_linear(format
);
297 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
300 return PIPE_BIND_SAMPLER_VIEW
;
306 * Given the size of a mipmap image, try to compute the size of the level=0
309 * Note that this isn't always accurate for odd-sized, non-POW textures.
310 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
312 * \return GL_TRUE for success, GL_FALSE for failure
315 guess_base_level_size(GLenum target
,
316 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
317 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
324 /* Guess the size of the base level.
325 * Depending on the image's size, we can't always make a guess here.
329 case GL_TEXTURE_1D_ARRAY
:
334 case GL_TEXTURE_2D_ARRAY
:
335 /* We can't make a good guess here, because the base level dimensions
338 if (width
== 1 || height
== 1) {
345 case GL_TEXTURE_CUBE_MAP
:
346 case GL_TEXTURE_CUBE_MAP_ARRAY
:
352 /* We can't make a good guess here, because the base level dimensions
355 if (width
== 1 || height
== 1 || depth
== 1) {
363 case GL_TEXTURE_RECTANGLE
:
380 * Try to allocate a pipe_resource object for the given st_texture_object.
382 * We use the given st_texture_image as a clue to determine the size of the
383 * mipmap image at level=0.
385 * \return GL_TRUE for success, GL_FALSE if out of memory.
388 guess_and_alloc_texture(struct st_context
*st
,
389 struct st_texture_object
*stObj
,
390 const struct st_texture_image
*stImage
)
392 GLuint lastLevel
, width
, height
, depth
;
394 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
395 enum pipe_format fmt
;
397 DBG("%s\n", __FUNCTION__
);
401 if (!guess_base_level_size(stObj
->base
.Target
,
402 stImage
->base
.Width2
,
403 stImage
->base
.Height2
,
404 stImage
->base
.Depth2
,
406 &width
, &height
, &depth
)) {
407 /* we can't determine the image size at level=0 */
408 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
409 /* this is not an out of memory error */
413 /* At this point, (width x height x depth) is the expected size of
414 * the level=0 mipmap image.
417 /* Guess a reasonable value for lastLevel. With OpenGL we have no
418 * idea how many mipmap levels will be in a texture until we start
419 * to render with it. Make an educated guess here but be prepared
420 * to re-allocating a texture buffer with space for more (or fewer)
421 * mipmap levels later.
423 if ((stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
424 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
425 (stObj
->base
.BaseLevel
== 0 &&
426 stObj
->base
.MaxLevel
== 0) ||
427 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
428 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
429 !stObj
->base
.GenerateMipmap
&&
430 stImage
->base
.Level
== 0) {
431 /* only alloc space for a single mipmap level */
435 /* alloc space for a full mipmap */
436 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
437 width
, height
, depth
) - 1;
440 /* Save the level=0 dimensions */
441 stObj
->width0
= width
;
442 stObj
->height0
= height
;
443 stObj
->depth0
= depth
;
445 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
447 bindings
= default_bindings(st
, fmt
);
449 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
450 width
, height
, depth
,
451 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
453 stObj
->pt
= st_texture_create(st
,
454 gl_target_to_pipe(stObj
->base
.Target
),
463 stObj
->lastLevel
= lastLevel
;
465 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
467 return stObj
->pt
!= NULL
;
472 * Called via ctx->Driver.AllocTextureImageBuffer().
473 * If the texture object/buffer already has space for the indicated image,
474 * we're done. Otherwise, allocate memory for the new texture image.
477 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
478 struct gl_texture_image
*texImage
)
480 struct st_context
*st
= st_context(ctx
);
481 struct st_texture_image
*stImage
= st_texture_image(texImage
);
482 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
483 const GLuint level
= texImage
->Level
;
484 GLuint width
= texImage
->Width
;
485 GLuint height
= texImage
->Height
;
486 GLuint depth
= texImage
->Depth
;
488 DBG("%s\n", __FUNCTION__
);
490 assert(!stImage
->TexData
);
491 assert(!stImage
->pt
); /* xxx this might be wrong */
493 /* Look if the parent texture object has space for this image */
495 level
<= stObj
->pt
->last_level
&&
496 st_texture_match_image(stObj
->pt
, texImage
)) {
497 /* this image will fit in the existing texture object's memory */
498 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
502 /* The parent texture object does not have space for this image */
504 pipe_resource_reference(&stObj
->pt
, NULL
);
505 st_texture_release_all_sampler_views(stObj
);
507 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
508 /* Probably out of memory.
509 * Try flushing any pending rendering, then retry.
512 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
513 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
519 st_texture_match_image(stObj
->pt
, texImage
)) {
520 /* The image will live in the object's mipmap memory */
521 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
526 /* Create a new, temporary texture/resource/buffer to hold this
527 * one texture image. Note that when we later access this image
528 * (either for mapping or copying) we'll want to always specify
529 * mipmap level=0, even if the image represents some other mipmap
532 enum pipe_format format
=
533 st_mesa_format_to_pipe_format(texImage
->TexFormat
);
534 GLuint bindings
= default_bindings(st
, format
);
535 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
537 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
538 width
, height
, depth
,
539 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
541 stImage
->pt
= st_texture_create(st
,
542 gl_target_to_pipe(stObj
->base
.Target
),
550 return stImage
->pt
!= NULL
;
556 * Preparation prior to glTexImage. Basically check the 'surface_based'
557 * field and switch to a "normal" tex image if necessary.
560 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
561 GLenum format
, GLenum type
)
563 struct gl_texture_object
*texObj
= texImage
->TexObject
;
564 struct st_texture_object
*stObj
= st_texture_object(texObj
);
566 /* switch to "normal" */
567 if (stObj
->surface_based
) {
568 const GLenum target
= texObj
->Target
;
569 const GLuint level
= texImage
->Level
;
570 mesa_format texFormat
;
572 _mesa_clear_texture_object(ctx
, texObj
);
573 pipe_resource_reference(&stObj
->pt
, NULL
);
575 /* oops, need to init this image again */
576 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
577 texImage
->InternalFormat
, format
,
580 _mesa_init_teximage_fields(ctx
, texImage
,
581 texImage
->Width
, texImage
->Height
,
582 texImage
->Depth
, texImage
->Border
,
583 texImage
->InternalFormat
, texFormat
);
585 stObj
->surface_based
= GL_FALSE
;
591 * Return a writemask for the gallium blit. The parameters can be base
592 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
595 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
598 case GL_DEPTH_STENCIL
:
600 case GL_DEPTH_STENCIL
:
602 case GL_DEPTH_COMPONENT
:
604 case GL_STENCIL_INDEX
:
611 case GL_DEPTH_COMPONENT
:
613 case GL_DEPTH_STENCIL
:
614 case GL_DEPTH_COMPONENT
:
621 case GL_STENCIL_INDEX
:
623 case GL_STENCIL_INDEX
:
631 return PIPE_MASK_RGBA
;
637 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
638 struct gl_texture_image
*texImage
,
639 GLint xoffset
, GLint yoffset
, GLint zoffset
,
640 GLint width
, GLint height
, GLint depth
,
641 GLenum format
, GLenum type
, const void *pixels
,
642 const struct gl_pixelstore_attrib
*unpack
)
644 struct st_context
*st
= st_context(ctx
);
645 struct st_texture_image
*stImage
= st_texture_image(texImage
);
646 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
647 struct pipe_context
*pipe
= st
->pipe
;
648 struct pipe_screen
*screen
= pipe
->screen
;
649 struct pipe_resource
*dst
= stImage
->pt
;
650 struct pipe_resource
*src
= NULL
;
651 struct pipe_resource src_templ
;
652 struct pipe_transfer
*transfer
;
653 struct pipe_blit_info blit
;
654 enum pipe_format src_format
, dst_format
;
655 mesa_format mesa_src_format
;
656 GLenum gl_target
= texImage
->TexObject
->Target
;
660 assert(!_mesa_is_format_etc2(texImage
->TexFormat
));
662 if (!st
->prefer_blit_based_texture_transfer
) {
670 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
671 * blit implementation in some drivers. */
672 if (format
== GL_DEPTH_STENCIL
) {
676 /* If the base internal format and the texture format don't match,
677 * we can't use blit-based TexSubImage. */
678 if (texImage
->_BaseFormat
!=
679 _mesa_get_format_base_format(texImage
->TexFormat
)) {
683 /* See if the texture format already matches the format and type,
684 * in which case the memcpy-based fast path will likely be used and
685 * we don't have to blit. */
686 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
687 type
, unpack
->SwapBytes
)) {
691 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
692 bind
= PIPE_BIND_DEPTH_STENCIL
;
694 bind
= PIPE_BIND_RENDER_TARGET
;
696 /* See if the destination format is supported.
697 * For luminance and intensity, only the red channel is stored there. */
698 dst_format
= util_format_linear(dst
->format
);
699 dst_format
= util_format_luminance_to_red(dst_format
);
700 dst_format
= util_format_intensity_to_red(dst_format
);
703 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
704 dst
->nr_samples
, bind
)) {
708 /* Choose the source format. */
709 src_format
= st_choose_matching_format(screen
, PIPE_BIND_SAMPLER_VIEW
,
710 format
, type
, unpack
->SwapBytes
);
715 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
717 /* There is no reason to do this if we cannot use memcpy for the temporary
718 * source texture at least. This also takes transfer ops into account,
720 if (!_mesa_texstore_can_use_memcpy(ctx
,
721 _mesa_get_format_base_format(mesa_src_format
),
722 mesa_src_format
, format
, type
, unpack
)) {
726 /* TexSubImage only sets a single cubemap face. */
727 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
728 gl_target
= GL_TEXTURE_2D
;
731 /* Initialize the source texture description. */
732 memset(&src_templ
, 0, sizeof(src_templ
));
733 src_templ
.target
= gl_target_to_pipe(gl_target
);
734 src_templ
.format
= src_format
;
735 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
736 src_templ
.usage
= PIPE_USAGE_STAGING
;
738 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
739 &src_templ
.width0
, &src_templ
.height0
,
740 &src_templ
.depth0
, &src_templ
.array_size
);
742 /* Check for NPOT texture support. */
743 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
744 (!util_is_power_of_two(src_templ
.width0
) ||
745 !util_is_power_of_two(src_templ
.height0
) ||
746 !util_is_power_of_two(src_templ
.depth0
))) {
750 /* Create the source texture. */
751 src
= screen
->resource_create(screen
, &src_templ
);
756 /* Map source pixels. */
757 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
758 format
, type
, pixels
, unpack
,
761 /* This is a GL error. */
762 pipe_resource_reference(&src
, NULL
);
766 /* From now on, we need the gallium representation of dimensions. */
767 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
774 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
775 width
, height
, depth
, &transfer
);
777 _mesa_unmap_teximage_pbo(ctx
, unpack
);
778 pipe_resource_reference(&src
, NULL
);
782 /* Upload pixels (just memcpy). */
784 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
787 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
788 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
789 /* 1D array textures.
790 * We need to convert gallium coords to GL coords.
792 GLvoid
*src
= _mesa_image_address3d(unpack
, pixels
,
793 width
, depth
, format
,
795 memcpy(map
, src
, bytesPerRow
);
798 ubyte
*slice_map
= map
;
800 for (row
= 0; row
< (unsigned) height
; row
++) {
801 GLvoid
*src
= _mesa_image_address3d(unpack
, pixels
,
802 width
, height
, format
,
803 type
, slice
, row
, 0);
804 memcpy(slice_map
, src
, bytesPerRow
);
805 slice_map
+= transfer
->stride
;
808 map
+= transfer
->layer_stride
;
812 pipe_transfer_unmap(pipe
, transfer
);
813 _mesa_unmap_teximage_pbo(ctx
, unpack
);
816 memset(&blit
, 0, sizeof(blit
));
817 blit
.src
.resource
= src
;
819 blit
.src
.format
= src_format
;
820 blit
.dst
.resource
= dst
;
821 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
;
822 blit
.dst
.format
= dst_format
;
823 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
824 blit
.dst
.box
.x
= xoffset
;
825 blit
.dst
.box
.y
= yoffset
;
826 blit
.dst
.box
.z
= zoffset
+ texImage
->Face
;
827 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
828 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
829 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
830 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
831 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
832 blit
.scissor_enable
= FALSE
;
834 st
->pipe
->blit(st
->pipe
, &blit
);
836 pipe_resource_reference(&src
, NULL
);
840 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
841 width
, height
, depth
, format
, type
, pixels
,
846 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
847 struct gl_texture_image
*texImage
,
848 GLenum format
, GLenum type
, const void *pixels
,
849 const struct gl_pixelstore_attrib
*unpack
)
851 assert(dims
== 1 || dims
== 2 || dims
== 3);
853 prep_teximage(ctx
, texImage
, format
, type
);
855 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
858 /* allocate storage for texture data */
859 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
860 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
864 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
865 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
866 format
, type
, pixels
, unpack
);
871 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
872 struct gl_texture_image
*texImage
,
873 GLsizei imageSize
, const GLvoid
*data
)
875 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
876 _mesa_store_compressed_teximage(ctx
, dims
, texImage
, imageSize
, data
);
883 * Called via ctx->Driver.GetTexImage()
885 * This uses a blit to copy the texture to a texture format which matches
886 * the format and type combo and then a fast read-back is done using memcpy.
887 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
888 * a format which matches the swizzling.
890 * If such a format isn't available, it falls back to _mesa_get_teximage.
892 * NOTE: Drivers usually do a blit to convert between tiled and linear
893 * texture layouts during texture uploads/downloads, so the blit
894 * we do here should be free in such cases.
897 st_GetTexImage(struct gl_context
* ctx
,
898 GLenum format
, GLenum type
, GLvoid
* pixels
,
899 struct gl_texture_image
*texImage
)
901 struct st_context
*st
= st_context(ctx
);
902 struct pipe_context
*pipe
= st
->pipe
;
903 struct pipe_screen
*screen
= pipe
->screen
;
904 GLuint width
= texImage
->Width
;
905 GLuint height
= texImage
->Height
;
906 GLuint depth
= texImage
->Depth
;
907 struct st_texture_image
*stImage
= st_texture_image(texImage
);
908 struct pipe_resource
*src
= st_texture_object(texImage
->TexObject
)->pt
;
909 struct pipe_resource
*dst
= NULL
;
910 struct pipe_resource dst_templ
;
911 enum pipe_format dst_format
, src_format
;
912 mesa_format mesa_format
;
913 GLenum gl_target
= texImage
->TexObject
->Target
;
914 enum pipe_texture_target pipe_target
;
915 struct pipe_blit_info blit
;
916 unsigned bind
= PIPE_BIND_TRANSFER_READ
;
917 struct pipe_transfer
*tex_xfer
;
919 boolean done
= FALSE
;
921 assert(!_mesa_is_format_etc2(texImage
->TexFormat
));
923 if (!st
->prefer_blit_based_texture_transfer
&&
924 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
925 /* Try to avoid the fallback if we're doing texture decompression here */
929 if (!stImage
->pt
|| !src
) {
933 /* XXX Fallback to _mesa_get_teximage for depth-stencil formats
934 * due to an incomplete stencil blit implementation in some drivers. */
935 if (format
== GL_DEPTH_STENCIL
) {
939 /* If the base internal format and the texture format don't match, we have
940 * to fall back to _mesa_get_teximage. */
941 if (texImage
->_BaseFormat
!=
942 _mesa_get_format_base_format(texImage
->TexFormat
)) {
946 /* See if the texture format already matches the format and type,
947 * in which case the memcpy-based fast path will be used. */
948 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
949 type
, ctx
->Pack
.SwapBytes
)) {
953 /* Convert the source format to what is expected by GetTexImage
954 * and see if it's supported.
956 * This only applies to glGetTexImage:
957 * - Luminance must be returned as (L,0,0,1).
958 * - Luminance alpha must be returned as (L,0,0,A).
959 * - Intensity must be returned as (I,0,0,1)
961 src_format
= util_format_linear(src
->format
);
962 src_format
= util_format_luminance_to_red(src_format
);
963 src_format
= util_format_intensity_to_red(src_format
);
966 !screen
->is_format_supported(screen
, src_format
, src
->target
,
968 PIPE_BIND_SAMPLER_VIEW
)) {
972 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
973 bind
|= PIPE_BIND_DEPTH_STENCIL
;
975 bind
|= PIPE_BIND_RENDER_TARGET
;
977 /* GetTexImage only returns a single face for cubemaps. */
978 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
979 gl_target
= GL_TEXTURE_2D
;
981 pipe_target
= gl_target_to_pipe(gl_target
);
983 /* Choose the destination format by finding the best match
984 * for the format+type combo. */
985 dst_format
= st_choose_matching_format(screen
, bind
, format
, type
,
986 ctx
->Pack
.SwapBytes
);
988 if (dst_format
== PIPE_FORMAT_NONE
) {
991 /* Fall back to _mesa_get_teximage except for compressed formats,
992 * where decompression with a blit is always preferred. */
993 if (!util_format_is_compressed(src
->format
)) {
997 /* Set the appropriate format for the decompressed texture.
998 * Luminance and sRGB formats shouldn't appear here.*/
999 switch (src_format
) {
1000 case PIPE_FORMAT_DXT1_RGB
:
1001 case PIPE_FORMAT_DXT1_RGBA
:
1002 case PIPE_FORMAT_DXT3_RGBA
:
1003 case PIPE_FORMAT_DXT5_RGBA
:
1004 case PIPE_FORMAT_RGTC1_UNORM
:
1005 case PIPE_FORMAT_RGTC2_UNORM
:
1006 case PIPE_FORMAT_ETC1_RGB8
:
1007 dst_glformat
= GL_RGBA8
;
1009 case PIPE_FORMAT_RGTC1_SNORM
:
1010 case PIPE_FORMAT_RGTC2_SNORM
:
1011 if (!ctx
->Extensions
.EXT_texture_snorm
)
1013 dst_glformat
= GL_RGBA8_SNORM
;
1015 /* TODO: for BPTC_*FLOAT, set RGBA32F and check for ARB_texture_float */
1021 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
1022 pipe_target
, 0, bind
, FALSE
);
1024 if (dst_format
== PIPE_FORMAT_NONE
) {
1025 /* unable to get an rgba format!?! */
1030 /* create the destination texture */
1031 memset(&dst_templ
, 0, sizeof(dst_templ
));
1032 dst_templ
.target
= pipe_target
;
1033 dst_templ
.format
= dst_format
;
1034 dst_templ
.bind
= bind
;
1035 dst_templ
.usage
= PIPE_USAGE_STAGING
;
1037 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
1038 &dst_templ
.width0
, &dst_templ
.height0
,
1039 &dst_templ
.depth0
, &dst_templ
.array_size
);
1041 dst
= screen
->resource_create(screen
, &dst_templ
);
1046 /* From now on, we need the gallium representation of dimensions. */
1047 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1052 memset(&blit
, 0, sizeof(blit
));
1053 blit
.src
.resource
= src
;
1054 blit
.src
.level
= texImage
->Level
;
1055 blit
.src
.format
= src_format
;
1056 blit
.dst
.resource
= dst
;
1058 blit
.dst
.format
= dst
->format
;
1059 blit
.src
.box
.x
= blit
.dst
.box
.x
= 0;
1060 blit
.src
.box
.y
= blit
.dst
.box
.y
= 0;
1061 blit
.src
.box
.z
= texImage
->Face
;
1063 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1064 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1065 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1066 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
1067 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1068 blit
.scissor_enable
= FALSE
;
1070 /* blit/render/decompress */
1071 st
->pipe
->blit(st
->pipe
, &blit
);
1073 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
1075 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
1076 0, 0, 0, width
, height
, depth
, &tex_xfer
);
1081 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
1083 /* copy/pack data into user buffer */
1084 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
1085 ctx
->Pack
.SwapBytes
)) {
1087 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
1090 for (slice
= 0; slice
< depth
; slice
++) {
1091 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1092 /* 1D array textures.
1093 * We need to convert gallium coords to GL coords.
1095 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1096 width
, depth
, format
,
1098 memcpy(dest
, map
, bytesPerRow
);
1101 ubyte
*slice_map
= map
;
1103 for (row
= 0; row
< height
; row
++) {
1104 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1105 width
, height
, format
,
1106 type
, slice
, row
, 0);
1107 memcpy(dest
, slice_map
, bytesPerRow
);
1108 slice_map
+= tex_xfer
->stride
;
1111 map
+= tex_xfer
->layer_stride
;
1115 /* format translation via floats */
1119 assert(util_format_is_compressed(src
->format
));
1121 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
1126 if (ST_DEBUG
& DEBUG_FALLBACK
)
1127 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
1129 for (slice
= 0; slice
< depth
; slice
++) {
1130 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1131 /* 1D array textures.
1132 * We need to convert gallium coords to GL coords.
1134 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1135 width
, depth
, format
,
1138 /* get float[4] rgba row from surface */
1139 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, 1,
1142 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
1143 type
, dest
, &ctx
->Pack
, 0);
1146 for (row
= 0; row
< height
; row
++) {
1147 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1148 width
, height
, format
,
1149 type
, slice
, row
, 0);
1151 /* get float[4] rgba row from surface */
1152 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, row
, width
, 1,
1155 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
1156 type
, dest
, &ctx
->Pack
, 0);
1159 map
+= tex_xfer
->layer_stride
;
1168 pipe_transfer_unmap(pipe
, tex_xfer
);
1170 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
1171 pipe_resource_reference(&dst
, NULL
);
1175 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
1181 * Do a CopyTexSubImage operation using a read transfer from the source,
1182 * a write transfer to the destination and get_tile()/put_tile() to access
1183 * the pixels/texels.
1185 * Note: srcY=0=TOP of renderbuffer
1188 fallback_copy_texsubimage(struct gl_context
*ctx
,
1189 struct st_renderbuffer
*strb
,
1190 struct st_texture_image
*stImage
,
1192 GLint destX
, GLint destY
, GLint slice
,
1193 GLint srcX
, GLint srcY
,
1194 GLsizei width
, GLsizei height
)
1196 struct st_context
*st
= st_context(ctx
);
1197 struct pipe_context
*pipe
= st
->pipe
;
1198 struct pipe_transfer
*src_trans
;
1200 enum pipe_transfer_usage transfer_usage
;
1202 unsigned dst_width
= width
;
1203 unsigned dst_height
= height
;
1204 unsigned dst_depth
= 1;
1205 struct pipe_transfer
*transfer
;
1207 if (ST_DEBUG
& DEBUG_FALLBACK
)
1208 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1210 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1211 srcY
= strb
->Base
.Height
- srcY
- height
;
1214 map
= pipe_transfer_map(pipe
,
1216 strb
->surface
->u
.tex
.level
,
1217 strb
->surface
->u
.tex
.first_layer
,
1220 width
, height
, &src_trans
);
1222 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1223 baseFormat
== GL_DEPTH_STENCIL
) &&
1224 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1225 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1227 transfer_usage
= PIPE_TRANSFER_WRITE
;
1229 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
1230 destX
, destY
, slice
,
1231 dst_width
, dst_height
, dst_depth
,
1234 if (baseFormat
== GL_DEPTH_COMPONENT
||
1235 baseFormat
== GL_DEPTH_STENCIL
) {
1236 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1237 ctx
->Pixel
.DepthBias
!= 0.0F
);
1241 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1242 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1251 data
= malloc(width
* sizeof(uint
));
1254 /* To avoid a large temp memory allocation, do copy row by row */
1255 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1256 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
1258 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1261 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
1262 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
1263 0, 0, width
, 1, data
);
1266 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
1271 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
1279 malloc(width
* height
* 4 * sizeof(GLfloat
));
1281 if (tempSrc
&& texDest
) {
1282 const GLint dims
= 2;
1284 struct gl_texture_image
*texImage
= &stImage
->base
;
1285 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1287 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1288 unpack
.Invert
= GL_TRUE
;
1291 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
1292 dstRowStride
= transfer
->layer_stride
;
1295 dstRowStride
= transfer
->stride
;
1298 /* get float/RGBA image from framebuffer */
1299 /* XXX this usually involves a lot of int/float conversion.
1300 * try to avoid that someday.
1302 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
1303 util_format_linear(strb
->texture
->format
),
1306 /* Store into texture memory.
1307 * Note that this does some special things such as pixel transfer
1308 * ops and format conversion. In particular, if the dest tex format
1309 * is actually RGBA but the user created the texture as GL_RGB we
1310 * need to fill-in/override the alpha channel with 1.0.
1312 _mesa_texstore(ctx
, dims
,
1313 texImage
->_BaseFormat
,
1314 texImage
->TexFormat
,
1318 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1322 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1328 st_texture_image_unmap(st
, stImage
, slice
);
1329 pipe
->transfer_unmap(pipe
, src_trans
);
1334 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1335 * Note that the region to copy has already been clipped so we know we
1336 * won't read from outside the source renderbuffer's bounds.
1338 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1341 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1342 struct gl_texture_image
*texImage
,
1343 GLint destX
, GLint destY
, GLint slice
,
1344 struct gl_renderbuffer
*rb
,
1345 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
1347 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1348 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1349 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
1350 struct st_context
*st
= st_context(ctx
);
1351 struct pipe_context
*pipe
= st
->pipe
;
1352 struct pipe_screen
*screen
= pipe
->screen
;
1353 struct pipe_blit_info blit
;
1354 enum pipe_format dst_format
;
1355 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1359 assert(!_mesa_is_format_etc2(texImage
->TexFormat
));
1361 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1362 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1366 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
1367 texImage
->TexFormat
)) {
1371 /* The base internal format must match the mesa format, so make sure
1372 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
1374 if (texImage
->_BaseFormat
!=
1375 _mesa_get_format_base_format(texImage
->TexFormat
) ||
1376 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
1380 /* Choose the destination format to match the TexImage behavior. */
1381 dst_format
= util_format_linear(stImage
->pt
->format
);
1382 dst_format
= util_format_luminance_to_red(dst_format
);
1383 dst_format
= util_format_intensity_to_red(dst_format
);
1385 /* See if the destination format is supported. */
1386 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
1387 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
1388 bind
= PIPE_BIND_DEPTH_STENCIL
;
1391 bind
= PIPE_BIND_RENDER_TARGET
;
1395 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
1396 stImage
->pt
->nr_samples
, bind
)) {
1400 /* Y flipping for the main framebuffer. */
1402 srcY1
= strb
->Base
.Height
- srcY
- height
;
1403 srcY0
= srcY1
+ height
;
1407 srcY1
= srcY0
+ height
;
1410 /* Blit the texture.
1411 * This supports flipping, format conversions, and downsampling.
1413 memset(&blit
, 0, sizeof(blit
));
1414 blit
.src
.resource
= strb
->texture
;
1415 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
1416 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
1417 blit
.src
.box
.x
= srcX
;
1418 blit
.src
.box
.y
= srcY0
;
1419 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
1420 blit
.src
.box
.width
= width
;
1421 blit
.src
.box
.height
= srcY1
- srcY0
;
1422 blit
.src
.box
.depth
= 1;
1423 blit
.dst
.resource
= stImage
->pt
;
1424 blit
.dst
.format
= dst_format
;
1425 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
;
1426 blit
.dst
.box
.x
= destX
;
1427 blit
.dst
.box
.y
= destY
;
1428 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
;
1429 blit
.dst
.box
.width
= width
;
1430 blit
.dst
.box
.height
= height
;
1431 blit
.dst
.box
.depth
= 1;
1432 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
1433 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1434 pipe
->blit(pipe
, &blit
);
1438 /* software fallback */
1439 fallback_copy_texsubimage(ctx
,
1440 strb
, stImage
, texImage
->_BaseFormat
,
1441 destX
, destY
, slice
,
1442 srcX
, srcY
, width
, height
);
1447 * Copy image data from stImage into the texture object 'stObj' at level
1451 copy_image_data_to_texture(struct st_context
*st
,
1452 struct st_texture_object
*stObj
,
1454 struct st_texture_image
*stImage
)
1458 const struct gl_texture_image
*dstImage
=
1459 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
1461 assert(dstImage
->Width
== stImage
->base
.Width
);
1462 assert(dstImage
->Height
== stImage
->base
.Height
);
1463 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1467 /* Copy potentially with the blitter:
1470 if (stImage
->pt
->last_level
== 0)
1473 src_level
= stImage
->base
.Level
;
1475 assert(src_level
<= stImage
->pt
->last_level
);
1476 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
1477 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
1478 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
1479 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
1480 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
1481 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
1483 st_texture_image_copy(st
->pipe
,
1484 stObj
->pt
, dstLevel
, /* dest texture, level */
1485 stImage
->pt
, src_level
, /* src texture, level */
1486 stImage
->base
.Face
);
1488 pipe_resource_reference(&stImage
->pt
, NULL
);
1490 else if (stImage
->TexData
) {
1491 /* Copy from malloc'd memory */
1492 /* XXX this should be re-examined/tested with a compressed format */
1493 GLuint blockSize
= util_format_get_blocksize(stObj
->pt
->format
);
1494 GLuint srcRowStride
= stImage
->base
.Width
* blockSize
;
1495 GLuint srcSliceStride
= stImage
->base
.Height
* srcRowStride
;
1496 st_texture_image_data(st
,
1503 _mesa_align_free(stImage
->TexData
);
1504 stImage
->TexData
= NULL
;
1507 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1512 * Called during state validation. When this function is finished,
1513 * the texture object should be ready for rendering.
1514 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1517 st_finalize_texture(struct gl_context
*ctx
,
1518 struct pipe_context
*pipe
,
1519 struct gl_texture_object
*tObj
)
1521 struct st_context
*st
= st_context(ctx
);
1522 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1523 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1525 struct st_texture_image
*firstImage
;
1526 enum pipe_format firstImageFormat
;
1527 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
1529 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
1530 /* The texture is complete and we know exactly how many mipmap levels
1531 * are present/needed. This is conditional because we may be called
1532 * from the st_generate_mipmap() function when the texture object is
1533 * incomplete. In that case, we'll have set stObj->lastLevel before
1536 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1537 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1538 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1540 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1543 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
1544 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
1547 pipe_resource_reference(&stObj
->pt
, NULL
);
1548 st_texture_release_all_sampler_views(stObj
);
1552 if (st_obj
->buffer
!= stObj
->pt
) {
1553 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
1554 st_texture_release_all_sampler_views(stObj
);
1555 stObj
->width0
= stObj
->pt
->width0
/ _mesa_get_format_bytes(tObj
->_BufferObjectFormat
);
1563 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1566 /* If both firstImage and stObj point to a texture which can contain
1567 * all active images, favour firstImage. Note that because of the
1568 * completeness requirement, we know that the image dimensions
1571 if (firstImage
->pt
&&
1572 firstImage
->pt
!= stObj
->pt
&&
1573 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1574 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1575 st_texture_release_all_sampler_views(stObj
);
1578 /* If this texture comes from a window system, there is nothing else to do. */
1579 if (stObj
->surface_based
) {
1583 /* Find gallium format for the Mesa texture */
1584 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1586 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1588 GLuint width
, height
, depth
;
1589 if (!guess_base_level_size(stObj
->base
.Target
,
1590 firstImage
->base
.Width2
,
1591 firstImage
->base
.Height2
,
1592 firstImage
->base
.Depth2
,
1593 firstImage
->base
.Level
,
1594 &width
, &height
, &depth
)) {
1595 width
= stObj
->width0
;
1596 height
= stObj
->height0
;
1597 depth
= stObj
->depth0
;
1599 /* convert GL dims to Gallium dims */
1600 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1601 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1602 ptNumSamples
= firstImage
->base
.NumSamples
;
1605 /* If we already have a gallium texture, check that it matches the texture
1606 * object's format, target, size, num_levels, etc.
1609 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1610 stObj
->pt
->format
!= firstImageFormat
||
1611 stObj
->pt
->last_level
< stObj
->lastLevel
||
1612 stObj
->pt
->width0
!= ptWidth
||
1613 stObj
->pt
->height0
!= ptHeight
||
1614 stObj
->pt
->depth0
!= ptDepth
||
1615 stObj
->pt
->nr_samples
!= ptNumSamples
||
1616 stObj
->pt
->array_size
!= ptLayers
)
1618 /* The gallium texture does not match the Mesa texture so delete the
1619 * gallium texture now. We'll make a new one below.
1621 pipe_resource_reference(&stObj
->pt
, NULL
);
1622 st_texture_release_all_sampler_views(stObj
);
1623 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1627 /* May need to create a new gallium texture:
1630 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1632 stObj
->pt
= st_texture_create(st
,
1633 gl_target_to_pipe(stObj
->base
.Target
),
1639 ptLayers
, ptNumSamples
,
1643 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1648 /* Pull in any images not in the object's texture:
1650 for (face
= 0; face
< nr_faces
; face
++) {
1652 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1653 struct st_texture_image
*stImage
=
1654 st_texture_image(stObj
->base
.Image
[face
][level
]);
1656 /* Need to import images in main memory or held in other textures.
1658 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1660 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1661 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1662 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
1663 /* src image fits expected dest mipmap level size */
1664 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1675 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1676 * for a whole mipmap stack.
1679 st_AllocTextureStorage(struct gl_context
*ctx
,
1680 struct gl_texture_object
*texObj
,
1681 GLsizei levels
, GLsizei width
,
1682 GLsizei height
, GLsizei depth
)
1684 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
1685 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
1686 struct st_context
*st
= st_context(ctx
);
1687 struct st_texture_object
*stObj
= st_texture_object(texObj
);
1688 struct pipe_screen
*screen
= st
->pipe
->screen
;
1689 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
1690 enum pipe_format fmt
;
1692 GLuint num_samples
= texImage
->NumSamples
;
1696 /* Save the level=0 dimensions */
1697 stObj
->width0
= width
;
1698 stObj
->height0
= height
;
1699 stObj
->depth0
= depth
;
1700 stObj
->lastLevel
= levels
- 1;
1702 fmt
= st_mesa_format_to_pipe_format(texImage
->TexFormat
);
1704 bindings
= default_bindings(st
, fmt
);
1706 /* Raise the sample count if the requested one is unsupported. */
1707 if (num_samples
> 1) {
1708 boolean found
= FALSE
;
1710 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
1711 if (screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
1713 PIPE_BIND_SAMPLER_VIEW
)) {
1714 /* Update the sample count in gl_texture_image as well. */
1715 texImage
->NumSamples
= num_samples
;
1726 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
1727 width
, height
, depth
,
1728 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1730 stObj
->pt
= st_texture_create(st
,
1731 gl_target_to_pipe(texObj
->Target
),
1737 ptLayers
, num_samples
,
1742 /* Set image resource pointers */
1743 for (level
= 0; level
< levels
; level
++) {
1745 for (face
= 0; face
< numFaces
; face
++) {
1746 struct st_texture_image
*stImage
=
1747 st_texture_image(texObj
->Image
[face
][level
]);
1748 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1757 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
1758 GLint level
, mesa_format format
,
1759 GLint width
, GLint height
,
1760 GLint depth
, GLint border
)
1762 struct st_context
*st
= st_context(ctx
);
1763 struct pipe_context
*pipe
= st
->pipe
;
1765 if (width
== 0 || height
== 0 || depth
== 0) {
1766 /* zero-sized images are legal, and always fit! */
1770 if (pipe
->screen
->can_create_resource
) {
1771 /* Ask the gallium driver if the texture is too large */
1772 struct gl_texture_object
*texObj
=
1773 _mesa_get_current_tex_object(ctx
, target
);
1774 struct pipe_resource pt
;
1776 /* Setup the pipe_resource object
1778 memset(&pt
, 0, sizeof(pt
));
1780 pt
.target
= gl_target_to_pipe(target
);
1781 pt
.format
= st_mesa_format_to_pipe_format(format
);
1783 st_gl_texture_dims_to_pipe_dims(target
,
1784 width
, height
, depth
,
1785 &pt
.width0
, &pt
.height0
,
1786 &pt
.depth0
, &pt
.array_size
);
1788 if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
1789 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
1790 /* assume just one mipmap level */
1794 /* assume a full set of mipmaps */
1795 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
1798 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
1801 /* Use core Mesa fallback */
1802 return _mesa_test_proxy_teximage(ctx
, target
, level
, format
,
1803 width
, height
, depth
, border
);
1809 st_init_texture_functions(struct dd_function_table
*functions
)
1811 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1812 functions
->QuerySamplesForFormat
= st_QuerySamplesForFormat
;
1813 functions
->TexImage
= st_TexImage
;
1814 functions
->TexSubImage
= st_TexSubImage
;
1815 functions
->CompressedTexSubImage
= _mesa_store_compressed_texsubimage
;
1816 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
1817 functions
->GenerateMipmap
= st_generate_mipmap
;
1819 functions
->GetTexImage
= st_GetTexImage
;
1821 /* compressed texture functions */
1822 functions
->CompressedTexImage
= st_CompressedTexImage
;
1823 functions
->GetCompressedTexImage
= _mesa_get_compressed_teximage
;
1825 functions
->NewTextureObject
= st_NewTextureObject
;
1826 functions
->NewTextureImage
= st_NewTextureImage
;
1827 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
1828 functions
->DeleteTexture
= st_DeleteTextureObject
;
1829 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
1830 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
1831 functions
->MapTextureImage
= st_MapTextureImage
;
1832 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
1834 /* XXX Temporary until we can query pipe's texture sizes */
1835 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
1837 functions
->AllocTextureStorage
= st_AllocTextureStorage
;