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/texgetimage.h"
41 #include "main/teximage.h"
42 #include "main/texobj.h"
43 #include "main/texstore.h"
45 #include "state_tracker/st_debug.h"
46 #include "state_tracker/st_context.h"
47 #include "state_tracker/st_cb_fbo.h"
48 #include "state_tracker/st_cb_flush.h"
49 #include "state_tracker/st_cb_texture.h"
50 #include "state_tracker/st_cb_bufferobjects.h"
51 #include "state_tracker/st_format.h"
52 #include "state_tracker/st_texture.h"
53 #include "state_tracker/st_gen_mipmap.h"
54 #include "state_tracker/st_atom.h"
56 #include "pipe/p_context.h"
57 #include "pipe/p_defines.h"
58 #include "util/u_inlines.h"
59 #include "pipe/p_shader_tokens.h"
60 #include "util/u_tile.h"
61 #include "util/u_format.h"
62 #include "util/u_surface.h"
63 #include "util/u_sampler.h"
64 #include "util/u_math.h"
65 #include "util/u_box.h"
67 #define DBG if (0) printf
70 enum pipe_texture_target
71 gl_target_to_pipe(GLenum target
)
75 case GL_PROXY_TEXTURE_1D
:
76 return PIPE_TEXTURE_1D
;
78 case GL_PROXY_TEXTURE_2D
:
79 case GL_TEXTURE_EXTERNAL_OES
:
80 case GL_TEXTURE_2D_MULTISAMPLE
:
81 case GL_PROXY_TEXTURE_2D_MULTISAMPLE
:
82 return PIPE_TEXTURE_2D
;
83 case GL_TEXTURE_RECTANGLE_NV
:
84 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
85 return PIPE_TEXTURE_RECT
;
87 case GL_PROXY_TEXTURE_3D
:
88 return PIPE_TEXTURE_3D
;
89 case GL_TEXTURE_CUBE_MAP_ARB
:
90 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
91 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
92 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
93 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
94 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
95 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
96 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
97 return PIPE_TEXTURE_CUBE
;
98 case GL_TEXTURE_1D_ARRAY_EXT
:
99 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
100 return PIPE_TEXTURE_1D_ARRAY
;
101 case GL_TEXTURE_2D_ARRAY_EXT
:
102 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
103 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
104 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY
:
105 return PIPE_TEXTURE_2D_ARRAY
;
106 case GL_TEXTURE_BUFFER
:
108 case GL_TEXTURE_CUBE_MAP_ARRAY
:
109 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
110 return PIPE_TEXTURE_CUBE_ARRAY
;
118 /** called via ctx->Driver.NewTextureImage() */
119 static struct gl_texture_image
*
120 st_NewTextureImage(struct gl_context
* ctx
)
122 DBG("%s\n", __FUNCTION__
);
124 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
128 /** called via ctx->Driver.DeleteTextureImage() */
130 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
132 /* nothing special (yet) for st_texture_image */
133 _mesa_delete_texture_image(ctx
, img
);
137 /** called via ctx->Driver.NewTextureObject() */
138 static struct gl_texture_object
*
139 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
141 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
143 DBG("%s\n", __FUNCTION__
);
144 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
149 /** called via ctx->Driver.DeleteTextureObject() */
151 st_DeleteTextureObject(struct gl_context
*ctx
,
152 struct gl_texture_object
*texObj
)
154 struct st_texture_object
*stObj
= st_texture_object(texObj
);
156 pipe_resource_reference(&stObj
->pt
, NULL
);
157 st_texture_release_all_sampler_views(stObj
);
158 st_texture_free_sampler_views(stObj
);
159 _mesa_delete_texture_object(ctx
, texObj
);
163 /** called via ctx->Driver.FreeTextureImageBuffer() */
165 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
166 struct gl_texture_image
*texImage
)
168 struct st_texture_image
*stImage
= st_texture_image(texImage
);
170 DBG("%s\n", __FUNCTION__
);
173 pipe_resource_reference(&stImage
->pt
, NULL
);
176 _mesa_align_free(stImage
->TexData
);
177 stImage
->TexData
= NULL
;
179 free(stImage
->transfer
);
180 stImage
->transfer
= NULL
;
181 stImage
->num_transfers
= 0;
185 /** called via ctx->Driver.MapTextureImage() */
187 st_MapTextureImage(struct gl_context
*ctx
,
188 struct gl_texture_image
*texImage
,
189 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
191 GLubyte
**mapOut
, GLint
*rowStrideOut
)
193 struct st_context
*st
= st_context(ctx
);
194 struct st_texture_image
*stImage
= st_texture_image(texImage
);
197 struct pipe_transfer
*transfer
;
200 if (mode
& GL_MAP_READ_BIT
)
201 pipeMode
|= PIPE_TRANSFER_READ
;
202 if (mode
& GL_MAP_WRITE_BIT
)
203 pipeMode
|= PIPE_TRANSFER_WRITE
;
204 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
205 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
207 map
= st_texture_image_map(st
, stImage
, pipeMode
, x
, y
, slice
, w
, h
, 1,
211 *rowStrideOut
= transfer
->stride
;
220 /** called via ctx->Driver.UnmapTextureImage() */
222 st_UnmapTextureImage(struct gl_context
*ctx
,
223 struct gl_texture_image
*texImage
,
226 struct st_context
*st
= st_context(ctx
);
227 struct st_texture_image
*stImage
= st_texture_image(texImage
);
228 st_texture_image_unmap(st
, stImage
, slice
);
233 * Return default texture resource binding bitmask for the given format.
236 default_bindings(struct st_context
*st
, enum pipe_format format
)
238 struct pipe_screen
*screen
= st
->pipe
->screen
;
239 const unsigned target
= PIPE_TEXTURE_2D
;
242 if (util_format_is_depth_or_stencil(format
))
243 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
245 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
247 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
251 format
= util_format_linear(format
);
253 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
256 return PIPE_BIND_SAMPLER_VIEW
;
262 * Given the size of a mipmap image, try to compute the size of the level=0
265 * Note that this isn't always accurate for odd-sized, non-POW textures.
266 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
268 * \return GL_TRUE for success, GL_FALSE for failure
271 guess_base_level_size(GLenum target
,
272 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
273 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
280 /* Guess the size of the base level.
281 * Depending on the image's size, we can't always make a guess here.
285 case GL_TEXTURE_1D_ARRAY
:
290 case GL_TEXTURE_2D_ARRAY
:
291 /* We can't make a good guess here, because the base level dimensions
294 if (width
== 1 || height
== 1) {
301 case GL_TEXTURE_CUBE_MAP
:
302 case GL_TEXTURE_CUBE_MAP_ARRAY
:
308 /* We can't make a good guess here, because the base level dimensions
311 if (width
== 1 || height
== 1 || depth
== 1) {
319 case GL_TEXTURE_RECTANGLE
:
336 * Try to allocate a pipe_resource object for the given st_texture_object.
338 * We use the given st_texture_image as a clue to determine the size of the
339 * mipmap image at level=0.
341 * \return GL_TRUE for success, GL_FALSE if out of memory.
344 guess_and_alloc_texture(struct st_context
*st
,
345 struct st_texture_object
*stObj
,
346 const struct st_texture_image
*stImage
)
348 GLuint lastLevel
, width
, height
, depth
;
350 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
351 enum pipe_format fmt
;
353 DBG("%s\n", __FUNCTION__
);
357 if (!guess_base_level_size(stObj
->base
.Target
,
358 stImage
->base
.Width2
,
359 stImage
->base
.Height2
,
360 stImage
->base
.Depth2
,
362 &width
, &height
, &depth
)) {
363 /* we can't determine the image size at level=0 */
364 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
365 /* this is not an out of memory error */
369 /* At this point, (width x height x depth) is the expected size of
370 * the level=0 mipmap image.
373 /* Guess a reasonable value for lastLevel. With OpenGL we have no
374 * idea how many mipmap levels will be in a texture until we start
375 * to render with it. Make an educated guess here but be prepared
376 * to re-allocating a texture buffer with space for more (or fewer)
377 * mipmap levels later.
379 if ((stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
380 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
381 (stObj
->base
.BaseLevel
== 0 &&
382 stObj
->base
.MaxLevel
== 0) ||
383 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
384 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
385 !stObj
->base
.GenerateMipmap
&&
386 stImage
->base
.Level
== 0) {
387 /* only alloc space for a single mipmap level */
391 /* alloc space for a full mipmap */
392 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
393 width
, height
, depth
) - 1;
396 /* Save the level=0 dimensions */
397 stObj
->width0
= width
;
398 stObj
->height0
= height
;
399 stObj
->depth0
= depth
;
401 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
403 bindings
= default_bindings(st
, fmt
);
405 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
406 width
, height
, depth
,
407 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
409 stObj
->pt
= st_texture_create(st
,
410 gl_target_to_pipe(stObj
->base
.Target
),
419 stObj
->lastLevel
= lastLevel
;
421 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
423 return stObj
->pt
!= NULL
;
428 * Called via ctx->Driver.AllocTextureImageBuffer().
429 * If the texture object/buffer already has space for the indicated image,
430 * we're done. Otherwise, allocate memory for the new texture image.
433 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
434 struct gl_texture_image
*texImage
)
436 struct st_context
*st
= st_context(ctx
);
437 struct st_texture_image
*stImage
= st_texture_image(texImage
);
438 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
439 const GLuint level
= texImage
->Level
;
440 GLuint width
= texImage
->Width
;
441 GLuint height
= texImage
->Height
;
442 GLuint depth
= texImage
->Depth
;
444 DBG("%s\n", __FUNCTION__
);
446 assert(!stImage
->TexData
);
447 assert(!stImage
->pt
); /* xxx this might be wrong */
449 /* Look if the parent texture object has space for this image */
451 level
<= stObj
->pt
->last_level
&&
452 st_texture_match_image(stObj
->pt
, texImage
)) {
453 /* this image will fit in the existing texture object's memory */
454 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
458 /* The parent texture object does not have space for this image */
460 pipe_resource_reference(&stObj
->pt
, NULL
);
461 st_texture_release_all_sampler_views(stObj
);
463 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
464 /* Probably out of memory.
465 * Try flushing any pending rendering, then retry.
468 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
469 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
475 st_texture_match_image(stObj
->pt
, texImage
)) {
476 /* The image will live in the object's mipmap memory */
477 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
482 /* Create a new, temporary texture/resource/buffer to hold this
483 * one texture image. Note that when we later access this image
484 * (either for mapping or copying) we'll want to always specify
485 * mipmap level=0, even if the image represents some other mipmap
488 enum pipe_format format
=
489 st_mesa_format_to_pipe_format(texImage
->TexFormat
);
490 GLuint bindings
= default_bindings(st
, format
);
491 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
493 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
494 width
, height
, depth
,
495 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
497 stImage
->pt
= st_texture_create(st
,
498 gl_target_to_pipe(stObj
->base
.Target
),
506 return stImage
->pt
!= NULL
;
512 * Preparation prior to glTexImage. Basically check the 'surface_based'
513 * field and switch to a "normal" tex image if necessary.
516 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
517 GLenum format
, GLenum type
)
519 struct gl_texture_object
*texObj
= texImage
->TexObject
;
520 struct st_texture_object
*stObj
= st_texture_object(texObj
);
522 /* switch to "normal" */
523 if (stObj
->surface_based
) {
524 const GLenum target
= texObj
->Target
;
525 const GLuint level
= texImage
->Level
;
526 mesa_format texFormat
;
528 _mesa_clear_texture_object(ctx
, texObj
);
529 pipe_resource_reference(&stObj
->pt
, NULL
);
531 /* oops, need to init this image again */
532 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
533 texImage
->InternalFormat
, format
,
536 _mesa_init_teximage_fields(ctx
, texImage
,
537 texImage
->Width
, texImage
->Height
,
538 texImage
->Depth
, texImage
->Border
,
539 texImage
->InternalFormat
, texFormat
);
541 stObj
->surface_based
= GL_FALSE
;
547 * Return a writemask for the gallium blit. The parameters can be base
548 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
551 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
554 case GL_DEPTH_STENCIL
:
556 case GL_DEPTH_STENCIL
:
558 case GL_DEPTH_COMPONENT
:
560 case GL_STENCIL_INDEX
:
567 case GL_DEPTH_COMPONENT
:
569 case GL_DEPTH_STENCIL
:
570 case GL_DEPTH_COMPONENT
:
577 case GL_STENCIL_INDEX
:
579 case GL_STENCIL_INDEX
:
587 return PIPE_MASK_RGBA
;
593 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
594 struct gl_texture_image
*texImage
,
595 GLint xoffset
, GLint yoffset
, GLint zoffset
,
596 GLint width
, GLint height
, GLint depth
,
597 GLenum format
, GLenum type
, const void *pixels
,
598 const struct gl_pixelstore_attrib
*unpack
)
600 struct st_context
*st
= st_context(ctx
);
601 struct st_texture_image
*stImage
= st_texture_image(texImage
);
602 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
603 struct pipe_context
*pipe
= st
->pipe
;
604 struct pipe_screen
*screen
= pipe
->screen
;
605 struct pipe_resource
*dst
= stImage
->pt
;
606 struct pipe_resource
*src
= NULL
;
607 struct pipe_resource src_templ
;
608 struct pipe_transfer
*transfer
;
609 struct pipe_blit_info blit
;
610 enum pipe_format src_format
, dst_format
;
611 mesa_format mesa_src_format
;
612 GLenum gl_target
= texImage
->TexObject
->Target
;
616 if (!st
->prefer_blit_based_texture_transfer
) {
624 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
625 * blit implementation in some drivers. */
626 if (format
== GL_DEPTH_STENCIL
) {
630 /* If the base internal format and the texture format don't match,
631 * we can't use blit-based TexSubImage. */
632 if (texImage
->_BaseFormat
!=
633 _mesa_get_format_base_format(texImage
->TexFormat
)) {
637 /* See if the texture format already matches the format and type,
638 * in which case the memcpy-based fast path will likely be used and
639 * we don't have to blit. */
640 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
641 type
, unpack
->SwapBytes
)) {
645 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
646 bind
= PIPE_BIND_DEPTH_STENCIL
;
648 bind
= PIPE_BIND_RENDER_TARGET
;
650 /* See if the destination format is supported.
651 * For luminance and intensity, only the red channel is stored there. */
652 dst_format
= util_format_linear(dst
->format
);
653 dst_format
= util_format_luminance_to_red(dst_format
);
654 dst_format
= util_format_intensity_to_red(dst_format
);
657 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
658 dst
->nr_samples
, bind
)) {
662 /* Choose the source format. */
663 src_format
= st_choose_matching_format(screen
, PIPE_BIND_SAMPLER_VIEW
,
664 format
, type
, unpack
->SwapBytes
);
669 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
671 /* There is no reason to do this if we cannot use memcpy for the temporary
672 * source texture at least. This also takes transfer ops into account,
674 if (!_mesa_texstore_can_use_memcpy(ctx
,
675 _mesa_get_format_base_format(mesa_src_format
),
676 mesa_src_format
, format
, type
, unpack
)) {
680 /* TexSubImage only sets a single cubemap face. */
681 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
682 gl_target
= GL_TEXTURE_2D
;
685 /* Initialize the source texture description. */
686 memset(&src_templ
, 0, sizeof(src_templ
));
687 src_templ
.target
= gl_target_to_pipe(gl_target
);
688 src_templ
.format
= src_format
;
689 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
690 src_templ
.usage
= PIPE_USAGE_STAGING
;
692 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
693 &src_templ
.width0
, &src_templ
.height0
,
694 &src_templ
.depth0
, &src_templ
.array_size
);
696 /* Check for NPOT texture support. */
697 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
698 (!util_is_power_of_two(src_templ
.width0
) ||
699 !util_is_power_of_two(src_templ
.height0
) ||
700 !util_is_power_of_two(src_templ
.depth0
))) {
704 /* Create the source texture. */
705 src
= screen
->resource_create(screen
, &src_templ
);
710 /* Map source pixels. */
711 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
712 format
, type
, pixels
, unpack
,
715 /* This is a GL error. */
716 pipe_resource_reference(&src
, NULL
);
720 /* From now on, we need the gallium representation of dimensions. */
721 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
728 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
729 width
, height
, depth
, &transfer
);
731 _mesa_unmap_teximage_pbo(ctx
, unpack
);
732 pipe_resource_reference(&src
, NULL
);
736 /* Upload pixels (just memcpy). */
738 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
741 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
742 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
743 /* 1D array textures.
744 * We need to convert gallium coords to GL coords.
746 GLvoid
*src
= _mesa_image_address3d(unpack
, pixels
,
747 width
, depth
, format
,
749 memcpy(map
, src
, bytesPerRow
);
752 ubyte
*slice_map
= map
;
754 for (row
= 0; row
< (unsigned) height
; row
++) {
755 GLvoid
*src
= _mesa_image_address3d(unpack
, pixels
,
756 width
, height
, format
,
757 type
, slice
, row
, 0);
758 memcpy(slice_map
, src
, bytesPerRow
);
759 slice_map
+= transfer
->stride
;
762 map
+= transfer
->layer_stride
;
766 pipe_transfer_unmap(pipe
, transfer
);
767 _mesa_unmap_teximage_pbo(ctx
, unpack
);
770 memset(&blit
, 0, sizeof(blit
));
771 blit
.src
.resource
= src
;
773 blit
.src
.format
= src_format
;
774 blit
.dst
.resource
= dst
;
775 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
;
776 blit
.dst
.format
= dst_format
;
777 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
778 blit
.dst
.box
.x
= xoffset
;
779 blit
.dst
.box
.y
= yoffset
;
780 blit
.dst
.box
.z
= zoffset
+ texImage
->Face
;
781 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
782 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
783 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
784 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
785 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
786 blit
.scissor_enable
= FALSE
;
788 st
->pipe
->blit(st
->pipe
, &blit
);
790 pipe_resource_reference(&src
, NULL
);
794 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
795 width
, height
, depth
, format
, type
, pixels
,
800 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
801 struct gl_texture_image
*texImage
,
802 GLenum format
, GLenum type
, const void *pixels
,
803 const struct gl_pixelstore_attrib
*unpack
)
805 assert(dims
== 1 || dims
== 2 || dims
== 3);
807 prep_teximage(ctx
, texImage
, format
, type
);
809 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
812 /* allocate storage for texture data */
813 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
814 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
818 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
819 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
820 format
, type
, pixels
, unpack
);
825 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
826 struct gl_texture_image
*texImage
,
827 GLsizei imageSize
, const GLvoid
*data
)
829 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
830 _mesa_store_compressed_teximage(ctx
, dims
, texImage
, imageSize
, data
);
837 * Called via ctx->Driver.GetTexImage()
839 * This uses a blit to copy the texture to a texture format which matches
840 * the format and type combo and then a fast read-back is done using memcpy.
841 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
842 * a format which matches the swizzling.
844 * If such a format isn't available, it falls back to _mesa_get_teximage.
846 * NOTE: Drivers usually do a blit to convert between tiled and linear
847 * texture layouts during texture uploads/downloads, so the blit
848 * we do here should be free in such cases.
851 st_GetTexImage(struct gl_context
* ctx
,
852 GLenum format
, GLenum type
, GLvoid
* pixels
,
853 struct gl_texture_image
*texImage
)
855 struct st_context
*st
= st_context(ctx
);
856 struct pipe_context
*pipe
= st
->pipe
;
857 struct pipe_screen
*screen
= pipe
->screen
;
858 GLuint width
= texImage
->Width
;
859 GLuint height
= texImage
->Height
;
860 GLuint depth
= texImage
->Depth
;
861 struct st_texture_image
*stImage
= st_texture_image(texImage
);
862 struct pipe_resource
*src
= st_texture_object(texImage
->TexObject
)->pt
;
863 struct pipe_resource
*dst
= NULL
;
864 struct pipe_resource dst_templ
;
865 enum pipe_format dst_format
, src_format
;
866 mesa_format mesa_format
;
867 GLenum gl_target
= texImage
->TexObject
->Target
;
868 enum pipe_texture_target pipe_target
;
869 struct pipe_blit_info blit
;
870 unsigned bind
= PIPE_BIND_TRANSFER_READ
;
871 struct pipe_transfer
*tex_xfer
;
873 boolean done
= FALSE
;
875 if (!st
->prefer_blit_based_texture_transfer
&&
876 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
877 /* Try to avoid the fallback if we're doing texture decompression here */
881 if (!stImage
->pt
|| !src
) {
885 /* XXX Fallback to _mesa_get_teximage for depth-stencil formats
886 * due to an incomplete stencil blit implementation in some drivers. */
887 if (format
== GL_DEPTH_STENCIL
) {
891 /* If the base internal format and the texture format don't match, we have
892 * to fall back to _mesa_get_teximage. */
893 if (texImage
->_BaseFormat
!=
894 _mesa_get_format_base_format(texImage
->TexFormat
)) {
898 /* See if the texture format already matches the format and type,
899 * in which case the memcpy-based fast path will be used. */
900 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
901 type
, ctx
->Pack
.SwapBytes
)) {
905 /* Convert the source format to what is expected by GetTexImage
906 * and see if it's supported.
908 * This only applies to glGetTexImage:
909 * - Luminance must be returned as (L,0,0,1).
910 * - Luminance alpha must be returned as (L,0,0,A).
911 * - Intensity must be returned as (I,0,0,1)
913 src_format
= util_format_linear(src
->format
);
914 src_format
= util_format_luminance_to_red(src_format
);
915 src_format
= util_format_intensity_to_red(src_format
);
918 !screen
->is_format_supported(screen
, src_format
, src
->target
,
920 PIPE_BIND_SAMPLER_VIEW
)) {
924 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
925 bind
|= PIPE_BIND_DEPTH_STENCIL
;
927 bind
|= PIPE_BIND_RENDER_TARGET
;
929 /* GetTexImage only returns a single face for cubemaps. */
930 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
931 gl_target
= GL_TEXTURE_2D
;
933 pipe_target
= gl_target_to_pipe(gl_target
);
935 /* Choose the destination format by finding the best match
936 * for the format+type combo. */
937 dst_format
= st_choose_matching_format(screen
, bind
, format
, type
,
938 ctx
->Pack
.SwapBytes
);
940 if (dst_format
== PIPE_FORMAT_NONE
) {
943 /* Fall back to _mesa_get_teximage except for compressed formats,
944 * where decompression with a blit is always preferred. */
945 if (!util_format_is_compressed(src
->format
)) {
949 /* Set the appropriate format for the decompressed texture.
950 * Luminance and sRGB formats shouldn't appear here.*/
951 switch (src_format
) {
952 case PIPE_FORMAT_DXT1_RGB
:
953 case PIPE_FORMAT_DXT1_RGBA
:
954 case PIPE_FORMAT_DXT3_RGBA
:
955 case PIPE_FORMAT_DXT5_RGBA
:
956 case PIPE_FORMAT_RGTC1_UNORM
:
957 case PIPE_FORMAT_RGTC2_UNORM
:
958 case PIPE_FORMAT_ETC1_RGB8
:
959 dst_glformat
= GL_RGBA8
;
961 case PIPE_FORMAT_RGTC1_SNORM
:
962 case PIPE_FORMAT_RGTC2_SNORM
:
963 if (!ctx
->Extensions
.EXT_texture_snorm
)
965 dst_glformat
= GL_RGBA8_SNORM
;
967 /* TODO: for BPTC_*FLOAT, set RGBA32F and check for ARB_texture_float */
973 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
974 pipe_target
, 0, bind
, FALSE
);
976 if (dst_format
== PIPE_FORMAT_NONE
) {
977 /* unable to get an rgba format!?! */
982 /* create the destination texture */
983 memset(&dst_templ
, 0, sizeof(dst_templ
));
984 dst_templ
.target
= pipe_target
;
985 dst_templ
.format
= dst_format
;
986 dst_templ
.bind
= bind
;
987 dst_templ
.usage
= PIPE_USAGE_STAGING
;
989 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
990 &dst_templ
.width0
, &dst_templ
.height0
,
991 &dst_templ
.depth0
, &dst_templ
.array_size
);
993 dst
= screen
->resource_create(screen
, &dst_templ
);
998 /* From now on, we need the gallium representation of dimensions. */
999 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1004 memset(&blit
, 0, sizeof(blit
));
1005 blit
.src
.resource
= src
;
1006 blit
.src
.level
= texImage
->Level
;
1007 blit
.src
.format
= src_format
;
1008 blit
.dst
.resource
= dst
;
1010 blit
.dst
.format
= dst
->format
;
1011 blit
.src
.box
.x
= blit
.dst
.box
.x
= 0;
1012 blit
.src
.box
.y
= blit
.dst
.box
.y
= 0;
1013 blit
.src
.box
.z
= texImage
->Face
;
1015 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1016 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1017 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1018 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
1019 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1020 blit
.scissor_enable
= FALSE
;
1022 /* blit/render/decompress */
1023 st
->pipe
->blit(st
->pipe
, &blit
);
1025 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
1027 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
1028 0, 0, 0, width
, height
, depth
, &tex_xfer
);
1033 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
1035 /* copy/pack data into user buffer */
1036 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
1037 ctx
->Pack
.SwapBytes
)) {
1039 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
1042 for (slice
= 0; slice
< depth
; slice
++) {
1043 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1044 /* 1D array textures.
1045 * We need to convert gallium coords to GL coords.
1047 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1048 width
, depth
, format
,
1050 memcpy(dest
, map
, bytesPerRow
);
1053 ubyte
*slice_map
= map
;
1055 for (row
= 0; row
< height
; row
++) {
1056 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1057 width
, height
, format
,
1058 type
, slice
, row
, 0);
1059 memcpy(dest
, slice_map
, bytesPerRow
);
1060 slice_map
+= tex_xfer
->stride
;
1063 map
+= tex_xfer
->layer_stride
;
1067 /* format translation via floats */
1071 assert(util_format_is_compressed(src
->format
));
1073 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
1078 if (ST_DEBUG
& DEBUG_FALLBACK
)
1079 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
1081 for (slice
= 0; slice
< depth
; slice
++) {
1082 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1083 /* 1D array textures.
1084 * We need to convert gallium coords to GL coords.
1086 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1087 width
, depth
, format
,
1090 /* get float[4] rgba row from surface */
1091 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, 1,
1094 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
1095 type
, dest
, &ctx
->Pack
, 0);
1098 for (row
= 0; row
< height
; row
++) {
1099 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1100 width
, height
, format
,
1101 type
, slice
, row
, 0);
1103 /* get float[4] rgba row from surface */
1104 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, row
, width
, 1,
1107 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
1108 type
, dest
, &ctx
->Pack
, 0);
1111 map
+= tex_xfer
->layer_stride
;
1120 pipe_transfer_unmap(pipe
, tex_xfer
);
1122 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
1123 pipe_resource_reference(&dst
, NULL
);
1127 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
1133 * Do a CopyTexSubImage operation using a read transfer from the source,
1134 * a write transfer to the destination and get_tile()/put_tile() to access
1135 * the pixels/texels.
1137 * Note: srcY=0=TOP of renderbuffer
1140 fallback_copy_texsubimage(struct gl_context
*ctx
,
1141 struct st_renderbuffer
*strb
,
1142 struct st_texture_image
*stImage
,
1144 GLint destX
, GLint destY
, GLint slice
,
1145 GLint srcX
, GLint srcY
,
1146 GLsizei width
, GLsizei height
)
1148 struct st_context
*st
= st_context(ctx
);
1149 struct pipe_context
*pipe
= st
->pipe
;
1150 struct pipe_transfer
*src_trans
;
1152 enum pipe_transfer_usage transfer_usage
;
1154 unsigned dst_width
= width
;
1155 unsigned dst_height
= height
;
1156 unsigned dst_depth
= 1;
1157 struct pipe_transfer
*transfer
;
1159 if (ST_DEBUG
& DEBUG_FALLBACK
)
1160 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1162 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1163 srcY
= strb
->Base
.Height
- srcY
- height
;
1166 map
= pipe_transfer_map(pipe
,
1168 strb
->surface
->u
.tex
.level
,
1169 strb
->surface
->u
.tex
.first_layer
,
1172 width
, height
, &src_trans
);
1174 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1175 baseFormat
== GL_DEPTH_STENCIL
) &&
1176 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1177 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1179 transfer_usage
= PIPE_TRANSFER_WRITE
;
1181 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
1182 destX
, destY
, slice
,
1183 dst_width
, dst_height
, dst_depth
,
1186 if (baseFormat
== GL_DEPTH_COMPONENT
||
1187 baseFormat
== GL_DEPTH_STENCIL
) {
1188 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1189 ctx
->Pixel
.DepthBias
!= 0.0F
);
1193 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1194 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1203 data
= malloc(width
* sizeof(uint
));
1206 /* To avoid a large temp memory allocation, do copy row by row */
1207 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1208 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
1210 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1213 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
1214 pipe_put_tile_z(transfer
, texDest
+ row
*transfer
->layer_stride
,
1215 0, 0, width
, 1, data
);
1218 pipe_put_tile_z(transfer
, texDest
, 0, row
, width
, 1, data
);
1223 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
1231 malloc(width
* height
* 4 * sizeof(GLfloat
));
1233 if (tempSrc
&& texDest
) {
1234 const GLint dims
= 2;
1236 struct gl_texture_image
*texImage
= &stImage
->base
;
1237 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1239 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1240 unpack
.Invert
= GL_TRUE
;
1243 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
1244 dstRowStride
= transfer
->layer_stride
;
1247 dstRowStride
= transfer
->stride
;
1250 /* get float/RGBA image from framebuffer */
1251 /* XXX this usually involves a lot of int/float conversion.
1252 * try to avoid that someday.
1254 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
1255 util_format_linear(strb
->texture
->format
),
1258 /* Store into texture memory.
1259 * Note that this does some special things such as pixel transfer
1260 * ops and format conversion. In particular, if the dest tex format
1261 * is actually RGBA but the user created the texture as GL_RGB we
1262 * need to fill-in/override the alpha channel with 1.0.
1264 _mesa_texstore(ctx
, dims
,
1265 texImage
->_BaseFormat
,
1266 texImage
->TexFormat
,
1270 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1274 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1280 st_texture_image_unmap(st
, stImage
, slice
);
1281 pipe
->transfer_unmap(pipe
, src_trans
);
1286 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1287 * Note that the region to copy has already been clipped so we know we
1288 * won't read from outside the source renderbuffer's bounds.
1290 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1293 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1294 struct gl_texture_image
*texImage
,
1295 GLint destX
, GLint destY
, GLint slice
,
1296 struct gl_renderbuffer
*rb
,
1297 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
1299 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1300 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1301 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
1302 struct st_context
*st
= st_context(ctx
);
1303 struct pipe_context
*pipe
= st
->pipe
;
1304 struct pipe_screen
*screen
= pipe
->screen
;
1305 struct pipe_blit_info blit
;
1306 enum pipe_format dst_format
;
1307 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1311 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1312 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1316 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
1317 texImage
->TexFormat
)) {
1321 /* The base internal format must match the mesa format, so make sure
1322 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
1324 if (texImage
->_BaseFormat
!=
1325 _mesa_get_format_base_format(texImage
->TexFormat
) ||
1326 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
1330 /* Choose the destination format to match the TexImage behavior. */
1331 dst_format
= util_format_linear(stImage
->pt
->format
);
1332 dst_format
= util_format_luminance_to_red(dst_format
);
1333 dst_format
= util_format_intensity_to_red(dst_format
);
1335 /* See if the destination format is supported. */
1336 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
1337 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
1338 bind
= PIPE_BIND_DEPTH_STENCIL
;
1341 bind
= PIPE_BIND_RENDER_TARGET
;
1345 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
1346 stImage
->pt
->nr_samples
, bind
)) {
1350 /* Y flipping for the main framebuffer. */
1352 srcY1
= strb
->Base
.Height
- srcY
- height
;
1353 srcY0
= srcY1
+ height
;
1357 srcY1
= srcY0
+ height
;
1360 /* Blit the texture.
1361 * This supports flipping, format conversions, and downsampling.
1363 memset(&blit
, 0, sizeof(blit
));
1364 blit
.src
.resource
= strb
->texture
;
1365 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
1366 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
1367 blit
.src
.box
.x
= srcX
;
1368 blit
.src
.box
.y
= srcY0
;
1369 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
1370 blit
.src
.box
.width
= width
;
1371 blit
.src
.box
.height
= srcY1
- srcY0
;
1372 blit
.src
.box
.depth
= 1;
1373 blit
.dst
.resource
= stImage
->pt
;
1374 blit
.dst
.format
= dst_format
;
1375 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
;
1376 blit
.dst
.box
.x
= destX
;
1377 blit
.dst
.box
.y
= destY
;
1378 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
;
1379 blit
.dst
.box
.width
= width
;
1380 blit
.dst
.box
.height
= height
;
1381 blit
.dst
.box
.depth
= 1;
1382 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
1383 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1384 pipe
->blit(pipe
, &blit
);
1388 /* software fallback */
1389 fallback_copy_texsubimage(ctx
,
1390 strb
, stImage
, texImage
->_BaseFormat
,
1391 destX
, destY
, slice
,
1392 srcX
, srcY
, width
, height
);
1397 * Copy image data from stImage into the texture object 'stObj' at level
1401 copy_image_data_to_texture(struct st_context
*st
,
1402 struct st_texture_object
*stObj
,
1404 struct st_texture_image
*stImage
)
1408 const struct gl_texture_image
*dstImage
=
1409 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
1411 assert(dstImage
->Width
== stImage
->base
.Width
);
1412 assert(dstImage
->Height
== stImage
->base
.Height
);
1413 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1417 /* Copy potentially with the blitter:
1420 if (stImage
->pt
->last_level
== 0)
1423 src_level
= stImage
->base
.Level
;
1425 assert(src_level
<= stImage
->pt
->last_level
);
1426 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
1427 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
1428 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
1429 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
1430 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
1431 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
1433 st_texture_image_copy(st
->pipe
,
1434 stObj
->pt
, dstLevel
, /* dest texture, level */
1435 stImage
->pt
, src_level
, /* src texture, level */
1436 stImage
->base
.Face
);
1438 pipe_resource_reference(&stImage
->pt
, NULL
);
1440 else if (stImage
->TexData
) {
1441 /* Copy from malloc'd memory */
1442 /* XXX this should be re-examined/tested with a compressed format */
1443 GLuint blockSize
= util_format_get_blocksize(stObj
->pt
->format
);
1444 GLuint srcRowStride
= stImage
->base
.Width
* blockSize
;
1445 GLuint srcSliceStride
= stImage
->base
.Height
* srcRowStride
;
1446 st_texture_image_data(st
,
1453 _mesa_align_free(stImage
->TexData
);
1454 stImage
->TexData
= NULL
;
1457 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1462 * Called during state validation. When this function is finished,
1463 * the texture object should be ready for rendering.
1464 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1467 st_finalize_texture(struct gl_context
*ctx
,
1468 struct pipe_context
*pipe
,
1469 struct gl_texture_object
*tObj
)
1471 struct st_context
*st
= st_context(ctx
);
1472 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1473 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1475 struct st_texture_image
*firstImage
;
1476 enum pipe_format firstImageFormat
;
1477 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
1479 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
1480 /* The texture is complete and we know exactly how many mipmap levels
1481 * are present/needed. This is conditional because we may be called
1482 * from the st_generate_mipmap() function when the texture object is
1483 * incomplete. In that case, we'll have set stObj->lastLevel before
1486 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1487 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1488 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1490 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1493 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
1494 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
1497 pipe_resource_reference(&stObj
->pt
, NULL
);
1498 st_texture_release_all_sampler_views(stObj
);
1502 if (st_obj
->buffer
!= stObj
->pt
) {
1503 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
1504 st_texture_release_all_sampler_views(stObj
);
1505 stObj
->width0
= stObj
->pt
->width0
/ _mesa_get_format_bytes(tObj
->_BufferObjectFormat
);
1513 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1516 /* If both firstImage and stObj point to a texture which can contain
1517 * all active images, favour firstImage. Note that because of the
1518 * completeness requirement, we know that the image dimensions
1521 if (firstImage
->pt
&&
1522 firstImage
->pt
!= stObj
->pt
&&
1523 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1524 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1525 st_texture_release_all_sampler_views(stObj
);
1528 /* If this texture comes from a window system, there is nothing else to do. */
1529 if (stObj
->surface_based
) {
1533 /* Find gallium format for the Mesa texture */
1534 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1536 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1538 GLuint width
, height
, depth
;
1539 if (!guess_base_level_size(stObj
->base
.Target
,
1540 firstImage
->base
.Width2
,
1541 firstImage
->base
.Height2
,
1542 firstImage
->base
.Depth2
,
1543 firstImage
->base
.Level
,
1544 &width
, &height
, &depth
)) {
1545 width
= stObj
->width0
;
1546 height
= stObj
->height0
;
1547 depth
= stObj
->depth0
;
1549 /* convert GL dims to Gallium dims */
1550 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1551 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1552 ptNumSamples
= firstImage
->base
.NumSamples
;
1555 /* If we already have a gallium texture, check that it matches the texture
1556 * object's format, target, size, num_levels, etc.
1559 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1560 stObj
->pt
->format
!= firstImageFormat
||
1561 stObj
->pt
->last_level
< stObj
->lastLevel
||
1562 stObj
->pt
->width0
!= ptWidth
||
1563 stObj
->pt
->height0
!= ptHeight
||
1564 stObj
->pt
->depth0
!= ptDepth
||
1565 stObj
->pt
->nr_samples
!= ptNumSamples
||
1566 stObj
->pt
->array_size
!= ptLayers
)
1568 /* The gallium texture does not match the Mesa texture so delete the
1569 * gallium texture now. We'll make a new one below.
1571 pipe_resource_reference(&stObj
->pt
, NULL
);
1572 st_texture_release_all_sampler_views(stObj
);
1573 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1577 /* May need to create a new gallium texture:
1580 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1582 stObj
->pt
= st_texture_create(st
,
1583 gl_target_to_pipe(stObj
->base
.Target
),
1589 ptLayers
, ptNumSamples
,
1593 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1598 /* Pull in any images not in the object's texture:
1600 for (face
= 0; face
< nr_faces
; face
++) {
1602 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1603 struct st_texture_image
*stImage
=
1604 st_texture_image(stObj
->base
.Image
[face
][level
]);
1606 /* Need to import images in main memory or held in other textures.
1608 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1610 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1611 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1612 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
1613 /* src image fits expected dest mipmap level size */
1614 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1625 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1626 * for a whole mipmap stack.
1629 st_AllocTextureStorage(struct gl_context
*ctx
,
1630 struct gl_texture_object
*texObj
,
1631 GLsizei levels
, GLsizei width
,
1632 GLsizei height
, GLsizei depth
)
1634 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
1635 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
1636 struct st_context
*st
= st_context(ctx
);
1637 struct st_texture_object
*stObj
= st_texture_object(texObj
);
1638 struct pipe_screen
*screen
= st
->pipe
->screen
;
1639 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
1640 enum pipe_format fmt
;
1642 GLuint num_samples
= texImage
->NumSamples
;
1646 /* Save the level=0 dimensions */
1647 stObj
->width0
= width
;
1648 stObj
->height0
= height
;
1649 stObj
->depth0
= depth
;
1650 stObj
->lastLevel
= levels
- 1;
1652 fmt
= st_mesa_format_to_pipe_format(texImage
->TexFormat
);
1654 bindings
= default_bindings(st
, fmt
);
1656 /* Raise the sample count if the requested one is unsupported. */
1657 if (num_samples
> 1) {
1658 boolean found
= FALSE
;
1660 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
1661 if (screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
1663 PIPE_BIND_SAMPLER_VIEW
)) {
1664 /* Update the sample count in gl_texture_image as well. */
1665 texImage
->NumSamples
= num_samples
;
1676 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
1677 width
, height
, depth
,
1678 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1680 stObj
->pt
= st_texture_create(st
,
1681 gl_target_to_pipe(texObj
->Target
),
1687 ptLayers
, num_samples
,
1692 /* Set image resource pointers */
1693 for (level
= 0; level
< levels
; level
++) {
1695 for (face
= 0; face
< numFaces
; face
++) {
1696 struct st_texture_image
*stImage
=
1697 st_texture_image(texObj
->Image
[face
][level
]);
1698 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1707 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
1708 GLint level
, mesa_format format
,
1709 GLint width
, GLint height
,
1710 GLint depth
, GLint border
)
1712 struct st_context
*st
= st_context(ctx
);
1713 struct pipe_context
*pipe
= st
->pipe
;
1715 if (width
== 0 || height
== 0 || depth
== 0) {
1716 /* zero-sized images are legal, and always fit! */
1720 if (pipe
->screen
->can_create_resource
) {
1721 /* Ask the gallium driver if the texture is too large */
1722 struct gl_texture_object
*texObj
=
1723 _mesa_get_current_tex_object(ctx
, target
);
1724 struct pipe_resource pt
;
1726 /* Setup the pipe_resource object
1728 memset(&pt
, 0, sizeof(pt
));
1730 pt
.target
= gl_target_to_pipe(target
);
1731 pt
.format
= st_mesa_format_to_pipe_format(format
);
1733 st_gl_texture_dims_to_pipe_dims(target
,
1734 width
, height
, depth
,
1735 &pt
.width0
, &pt
.height0
,
1736 &pt
.depth0
, &pt
.array_size
);
1738 if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
1739 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
1740 /* assume just one mipmap level */
1744 /* assume a full set of mipmaps */
1745 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
1748 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
1751 /* Use core Mesa fallback */
1752 return _mesa_test_proxy_teximage(ctx
, target
, level
, format
,
1753 width
, height
, depth
, border
);
1759 st_init_texture_functions(struct dd_function_table
*functions
)
1761 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1762 functions
->QuerySamplesForFormat
= st_QuerySamplesForFormat
;
1763 functions
->TexImage
= st_TexImage
;
1764 functions
->TexSubImage
= st_TexSubImage
;
1765 functions
->CompressedTexSubImage
= _mesa_store_compressed_texsubimage
;
1766 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
1767 functions
->GenerateMipmap
= st_generate_mipmap
;
1769 functions
->GetTexImage
= st_GetTexImage
;
1771 /* compressed texture functions */
1772 functions
->CompressedTexImage
= st_CompressedTexImage
;
1773 functions
->GetCompressedTexImage
= _mesa_get_compressed_teximage
;
1775 functions
->NewTextureObject
= st_NewTextureObject
;
1776 functions
->NewTextureImage
= st_NewTextureImage
;
1777 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
1778 functions
->DeleteTexture
= st_DeleteTextureObject
;
1779 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
1780 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
1781 functions
->MapTextureImage
= st_MapTextureImage
;
1782 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
1784 /* XXX Temporary until we can query pipe's texture sizes */
1785 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
1787 functions
->AllocTextureStorage
= st_AllocTextureStorage
;