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
;
181 /** called via ctx->Driver.MapTextureImage() */
183 st_MapTextureImage(struct gl_context
*ctx
,
184 struct gl_texture_image
*texImage
,
185 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
187 GLubyte
**mapOut
, GLint
*rowStrideOut
)
189 struct st_context
*st
= st_context(ctx
);
190 struct st_texture_image
*stImage
= st_texture_image(texImage
);
195 if (mode
& GL_MAP_READ_BIT
)
196 pipeMode
|= PIPE_TRANSFER_READ
;
197 if (mode
& GL_MAP_WRITE_BIT
)
198 pipeMode
|= PIPE_TRANSFER_WRITE
;
199 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
200 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
202 map
= st_texture_image_map(st
, stImage
, pipeMode
, x
, y
, slice
, w
, h
, 1);
205 *rowStrideOut
= stImage
->transfer
->stride
;
214 /** called via ctx->Driver.UnmapTextureImage() */
216 st_UnmapTextureImage(struct gl_context
*ctx
,
217 struct gl_texture_image
*texImage
,
220 struct st_context
*st
= st_context(ctx
);
221 struct st_texture_image
*stImage
= st_texture_image(texImage
);
222 st_texture_image_unmap(st
, stImage
);
227 * Return default texture resource binding bitmask for the given format.
230 default_bindings(struct st_context
*st
, enum pipe_format format
)
232 struct pipe_screen
*screen
= st
->pipe
->screen
;
233 const unsigned target
= PIPE_TEXTURE_2D
;
236 if (util_format_is_depth_or_stencil(format
))
237 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
239 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
241 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
245 format
= util_format_linear(format
);
247 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
250 return PIPE_BIND_SAMPLER_VIEW
;
256 * Given the size of a mipmap image, try to compute the size of the level=0
259 * Note that this isn't always accurate for odd-sized, non-POW textures.
260 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
262 * \return GL_TRUE for success, GL_FALSE for failure
265 guess_base_level_size(GLenum target
,
266 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
267 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
274 /* Guess the size of the base level.
275 * Depending on the image's size, we can't always make a guess here.
279 case GL_TEXTURE_1D_ARRAY
:
284 case GL_TEXTURE_2D_ARRAY
:
285 /* We can't make a good guess here, because the base level dimensions
288 if (width
== 1 || height
== 1) {
295 case GL_TEXTURE_CUBE_MAP
:
296 case GL_TEXTURE_CUBE_MAP_ARRAY
:
302 /* We can't make a good guess here, because the base level dimensions
305 if (width
== 1 || height
== 1 || depth
== 1) {
313 case GL_TEXTURE_RECTANGLE
:
330 * Try to allocate a pipe_resource object for the given st_texture_object.
332 * We use the given st_texture_image as a clue to determine the size of the
333 * mipmap image at level=0.
335 * \return GL_TRUE for success, GL_FALSE if out of memory.
338 guess_and_alloc_texture(struct st_context
*st
,
339 struct st_texture_object
*stObj
,
340 const struct st_texture_image
*stImage
)
342 GLuint lastLevel
, width
, height
, depth
;
344 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
345 enum pipe_format fmt
;
347 DBG("%s\n", __FUNCTION__
);
351 if (!guess_base_level_size(stObj
->base
.Target
,
352 stImage
->base
.Width2
,
353 stImage
->base
.Height2
,
354 stImage
->base
.Depth2
,
356 &width
, &height
, &depth
)) {
357 /* we can't determine the image size at level=0 */
358 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
359 /* this is not an out of memory error */
363 /* At this point, (width x height x depth) is the expected size of
364 * the level=0 mipmap image.
367 /* Guess a reasonable value for lastLevel. With OpenGL we have no
368 * idea how many mipmap levels will be in a texture until we start
369 * to render with it. Make an educated guess here but be prepared
370 * to re-allocating a texture buffer with space for more (or fewer)
371 * mipmap levels later.
373 if ((stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
374 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
375 (stObj
->base
.BaseLevel
== 0 &&
376 stObj
->base
.MaxLevel
== 0) ||
377 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
378 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
379 !stObj
->base
.GenerateMipmap
&&
380 stImage
->base
.Level
== 0) {
381 /* only alloc space for a single mipmap level */
385 /* alloc space for a full mipmap */
386 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
387 width
, height
, depth
) - 1;
390 /* Save the level=0 dimensions */
391 stObj
->width0
= width
;
392 stObj
->height0
= height
;
393 stObj
->depth0
= depth
;
395 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
397 bindings
= default_bindings(st
, fmt
);
399 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
400 width
, height
, depth
,
401 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
403 stObj
->pt
= st_texture_create(st
,
404 gl_target_to_pipe(stObj
->base
.Target
),
413 stObj
->lastLevel
= lastLevel
;
415 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
417 return stObj
->pt
!= NULL
;
422 * Called via ctx->Driver.AllocTextureImageBuffer().
423 * If the texture object/buffer already has space for the indicated image,
424 * we're done. Otherwise, allocate memory for the new texture image.
427 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
428 struct gl_texture_image
*texImage
)
430 struct st_context
*st
= st_context(ctx
);
431 struct st_texture_image
*stImage
= st_texture_image(texImage
);
432 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
433 const GLuint level
= texImage
->Level
;
434 GLuint width
= texImage
->Width
;
435 GLuint height
= texImage
->Height
;
436 GLuint depth
= texImage
->Depth
;
438 DBG("%s\n", __FUNCTION__
);
440 assert(!stImage
->TexData
);
441 assert(!stImage
->pt
); /* xxx this might be wrong */
443 /* Look if the parent texture object has space for this image */
445 level
<= stObj
->pt
->last_level
&&
446 st_texture_match_image(stObj
->pt
, texImage
)) {
447 /* this image will fit in the existing texture object's memory */
448 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
452 /* The parent texture object does not have space for this image */
454 pipe_resource_reference(&stObj
->pt
, NULL
);
455 st_texture_release_all_sampler_views(stObj
);
457 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
458 /* Probably out of memory.
459 * Try flushing any pending rendering, then retry.
462 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
463 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
469 st_texture_match_image(stObj
->pt
, texImage
)) {
470 /* The image will live in the object's mipmap memory */
471 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
476 /* Create a new, temporary texture/resource/buffer to hold this
477 * one texture image. Note that when we later access this image
478 * (either for mapping or copying) we'll want to always specify
479 * mipmap level=0, even if the image represents some other mipmap
482 enum pipe_format format
=
483 st_mesa_format_to_pipe_format(texImage
->TexFormat
);
484 GLuint bindings
= default_bindings(st
, format
);
485 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
487 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
488 width
, height
, depth
,
489 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
491 stImage
->pt
= st_texture_create(st
,
492 gl_target_to_pipe(stObj
->base
.Target
),
500 return stImage
->pt
!= NULL
;
506 * Preparation prior to glTexImage. Basically check the 'surface_based'
507 * field and switch to a "normal" tex image if necessary.
510 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
511 GLenum format
, GLenum type
)
513 struct gl_texture_object
*texObj
= texImage
->TexObject
;
514 struct st_texture_object
*stObj
= st_texture_object(texObj
);
516 /* switch to "normal" */
517 if (stObj
->surface_based
) {
518 const GLenum target
= texObj
->Target
;
519 const GLuint level
= texImage
->Level
;
520 mesa_format texFormat
;
522 _mesa_clear_texture_object(ctx
, texObj
);
523 pipe_resource_reference(&stObj
->pt
, NULL
);
525 /* oops, need to init this image again */
526 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
527 texImage
->InternalFormat
, format
,
530 _mesa_init_teximage_fields(ctx
, texImage
,
531 texImage
->Width
, texImage
->Height
,
532 texImage
->Depth
, texImage
->Border
,
533 texImage
->InternalFormat
, texFormat
);
535 stObj
->surface_based
= GL_FALSE
;
541 * Return a writemask for the gallium blit. The parameters can be base
542 * formats or "format" from glDrawPixels/glTexImage/glGetTexImage.
545 st_get_blit_mask(GLenum srcFormat
, GLenum dstFormat
)
548 case GL_DEPTH_STENCIL
:
550 case GL_DEPTH_STENCIL
:
552 case GL_DEPTH_COMPONENT
:
554 case GL_STENCIL_INDEX
:
561 case GL_DEPTH_COMPONENT
:
563 case GL_DEPTH_STENCIL
:
564 case GL_DEPTH_COMPONENT
:
571 case GL_STENCIL_INDEX
:
573 case GL_STENCIL_INDEX
:
581 return PIPE_MASK_RGBA
;
587 st_TexSubImage(struct gl_context
*ctx
, GLuint dims
,
588 struct gl_texture_image
*texImage
,
589 GLint xoffset
, GLint yoffset
, GLint zoffset
,
590 GLint width
, GLint height
, GLint depth
,
591 GLenum format
, GLenum type
, const void *pixels
,
592 const struct gl_pixelstore_attrib
*unpack
)
594 struct st_context
*st
= st_context(ctx
);
595 struct st_texture_image
*stImage
= st_texture_image(texImage
);
596 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
597 struct pipe_context
*pipe
= st
->pipe
;
598 struct pipe_screen
*screen
= pipe
->screen
;
599 struct pipe_resource
*dst
= stImage
->pt
;
600 struct pipe_resource
*src
= NULL
;
601 struct pipe_resource src_templ
;
602 struct pipe_transfer
*transfer
;
603 struct pipe_blit_info blit
;
604 enum pipe_format src_format
, dst_format
;
605 mesa_format mesa_src_format
;
606 GLenum gl_target
= texImage
->TexObject
->Target
;
610 if (!st
->prefer_blit_based_texture_transfer
) {
618 /* XXX Fallback for depth-stencil formats due to an incomplete stencil
619 * blit implementation in some drivers. */
620 if (format
== GL_DEPTH_STENCIL
) {
624 /* If the base internal format and the texture format don't match,
625 * we can't use blit-based TexSubImage. */
626 if (texImage
->_BaseFormat
!=
627 _mesa_get_format_base_format(texImage
->TexFormat
)) {
631 /* See if the texture format already matches the format and type,
632 * in which case the memcpy-based fast path will likely be used and
633 * we don't have to blit. */
634 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
635 type
, unpack
->SwapBytes
)) {
639 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
640 bind
= PIPE_BIND_DEPTH_STENCIL
;
642 bind
= PIPE_BIND_RENDER_TARGET
;
644 /* See if the destination format is supported.
645 * For luminance and intensity, only the red channel is stored there. */
646 dst_format
= util_format_linear(dst
->format
);
647 dst_format
= util_format_luminance_to_red(dst_format
);
648 dst_format
= util_format_intensity_to_red(dst_format
);
651 !screen
->is_format_supported(screen
, dst_format
, dst
->target
,
652 dst
->nr_samples
, bind
)) {
656 /* Choose the source format. */
657 src_format
= st_choose_matching_format(screen
, PIPE_BIND_SAMPLER_VIEW
,
658 format
, type
, unpack
->SwapBytes
);
663 mesa_src_format
= st_pipe_format_to_mesa_format(src_format
);
665 /* There is no reason to do this if we cannot use memcpy for the temporary
666 * source texture at least. This also takes transfer ops into account,
668 if (!_mesa_texstore_can_use_memcpy(ctx
,
669 _mesa_get_format_base_format(mesa_src_format
),
670 mesa_src_format
, format
, type
, unpack
)) {
674 /* TexSubImage only sets a single cubemap face. */
675 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
676 gl_target
= GL_TEXTURE_2D
;
679 /* Initialize the source texture description. */
680 memset(&src_templ
, 0, sizeof(src_templ
));
681 src_templ
.target
= gl_target_to_pipe(gl_target
);
682 src_templ
.format
= src_format
;
683 src_templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
684 src_templ
.usage
= PIPE_USAGE_STAGING
;
686 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
687 &src_templ
.width0
, &src_templ
.height0
,
688 &src_templ
.depth0
, &src_templ
.array_size
);
690 /* Check for NPOT texture support. */
691 if (!screen
->get_param(screen
, PIPE_CAP_NPOT_TEXTURES
) &&
692 (!util_is_power_of_two(src_templ
.width0
) ||
693 !util_is_power_of_two(src_templ
.height0
) ||
694 !util_is_power_of_two(src_templ
.depth0
))) {
698 /* Create the source texture. */
699 src
= screen
->resource_create(screen
, &src_templ
);
704 /* Map source pixels. */
705 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
706 format
, type
, pixels
, unpack
,
709 /* This is a GL error. */
710 pipe_resource_reference(&src
, NULL
);
714 /* From now on, we need the gallium representation of dimensions. */
715 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
720 map
= pipe_transfer_map_3d(pipe
, src
, 0, PIPE_TRANSFER_WRITE
, 0, 0, 0,
721 width
, height
, depth
, &transfer
);
723 _mesa_unmap_teximage_pbo(ctx
, unpack
);
724 pipe_resource_reference(&src
, NULL
);
728 /* Upload pixels (just memcpy). */
730 const uint bytesPerRow
= width
* util_format_get_blocksize(src_format
);
733 for (slice
= 0; slice
< (unsigned) depth
; slice
++) {
734 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
735 /* 1D array textures.
736 * We need to convert gallium coords to GL coords.
738 GLvoid
*src
= _mesa_image_address3d(unpack
, pixels
,
739 width
, depth
, format
,
741 memcpy(map
, src
, bytesPerRow
);
744 ubyte
*slice_map
= map
;
746 for (row
= 0; row
< (unsigned) height
; row
++) {
747 GLvoid
*src
= _mesa_image_address3d(unpack
, pixels
,
748 width
, height
, format
,
749 type
, slice
, row
, 0);
750 memcpy(slice_map
, src
, bytesPerRow
);
751 slice_map
+= transfer
->stride
;
754 map
+= transfer
->layer_stride
;
758 pipe_transfer_unmap(pipe
, transfer
);
759 _mesa_unmap_teximage_pbo(ctx
, unpack
);
762 blit
.src
.resource
= src
;
764 blit
.src
.format
= src_format
;
765 blit
.dst
.resource
= dst
;
766 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
;
767 blit
.dst
.format
= dst_format
;
768 blit
.src
.box
.x
= blit
.src
.box
.y
= blit
.src
.box
.z
= 0;
769 blit
.dst
.box
.x
= xoffset
;
770 blit
.dst
.box
.y
= yoffset
;
771 blit
.dst
.box
.z
= zoffset
+ texImage
->Face
;
772 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
773 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
774 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
775 blit
.mask
= st_get_blit_mask(format
, texImage
->_BaseFormat
);
776 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
777 blit
.scissor_enable
= FALSE
;
779 st
->pipe
->blit(st
->pipe
, &blit
);
781 pipe_resource_reference(&src
, NULL
);
785 _mesa_store_texsubimage(ctx
, dims
, texImage
, xoffset
, yoffset
, zoffset
,
786 width
, height
, depth
, format
, type
, pixels
,
791 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
792 struct gl_texture_image
*texImage
,
793 GLenum format
, GLenum type
, const void *pixels
,
794 const struct gl_pixelstore_attrib
*unpack
)
796 assert(dims
== 1 || dims
== 2 || dims
== 3);
798 prep_teximage(ctx
, texImage
, format
, type
);
800 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
803 /* allocate storage for texture data */
804 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
805 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
809 st_TexSubImage(ctx
, dims
, texImage
, 0, 0, 0,
810 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
811 format
, type
, pixels
, unpack
);
816 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
817 struct gl_texture_image
*texImage
,
818 GLsizei imageSize
, const GLvoid
*data
)
820 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
821 _mesa_store_compressed_teximage(ctx
, dims
, texImage
, imageSize
, data
);
828 * Called via ctx->Driver.GetTexImage()
830 * This uses a blit to copy the texture to a texture format which matches
831 * the format and type combo and then a fast read-back is done using memcpy.
832 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
833 * a format which matches the swizzling.
835 * If such a format isn't available, it falls back to _mesa_get_teximage.
837 * NOTE: Drivers usually do a blit to convert between tiled and linear
838 * texture layouts during texture uploads/downloads, so the blit
839 * we do here should be free in such cases.
842 st_GetTexImage(struct gl_context
* ctx
,
843 GLenum format
, GLenum type
, GLvoid
* pixels
,
844 struct gl_texture_image
*texImage
)
846 struct st_context
*st
= st_context(ctx
);
847 struct pipe_context
*pipe
= st
->pipe
;
848 struct pipe_screen
*screen
= pipe
->screen
;
849 GLuint width
= texImage
->Width
;
850 GLuint height
= texImage
->Height
;
851 GLuint depth
= texImage
->Depth
;
852 struct st_texture_image
*stImage
= st_texture_image(texImage
);
853 struct pipe_resource
*src
= st_texture_object(texImage
->TexObject
)->pt
;
854 struct pipe_resource
*dst
= NULL
;
855 struct pipe_resource dst_templ
;
856 enum pipe_format dst_format
, src_format
;
857 mesa_format mesa_format
;
858 GLenum gl_target
= texImage
->TexObject
->Target
;
859 enum pipe_texture_target pipe_target
;
860 struct pipe_blit_info blit
;
861 unsigned bind
= PIPE_BIND_TRANSFER_READ
;
862 struct pipe_transfer
*tex_xfer
;
864 boolean done
= FALSE
;
866 if (!st
->prefer_blit_based_texture_transfer
&&
867 !_mesa_is_format_compressed(texImage
->TexFormat
)) {
868 /* Try to avoid the fallback if we're doing texture decompression here */
872 if (!stImage
->pt
|| !src
) {
876 /* XXX Fallback to _mesa_get_teximage for depth-stencil formats
877 * due to an incomplete stencil blit implementation in some drivers. */
878 if (format
== GL_DEPTH_STENCIL
) {
882 /* If the base internal format and the texture format don't match, we have
883 * to fall back to _mesa_get_teximage. */
884 if (texImage
->_BaseFormat
!=
885 _mesa_get_format_base_format(texImage
->TexFormat
)) {
889 /* See if the texture format already matches the format and type,
890 * in which case the memcpy-based fast path will be used. */
891 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
892 type
, ctx
->Pack
.SwapBytes
)) {
896 /* Convert the source format to what is expected by GetTexImage
897 * and see if it's supported.
899 * This only applies to glGetTexImage:
900 * - Luminance must be returned as (L,0,0,1).
901 * - Luminance alpha must be returned as (L,0,0,A).
902 * - Intensity must be returned as (I,0,0,1)
904 src_format
= util_format_linear(src
->format
);
905 src_format
= util_format_luminance_to_red(src_format
);
906 src_format
= util_format_intensity_to_red(src_format
);
909 !screen
->is_format_supported(screen
, src_format
, src
->target
,
911 PIPE_BIND_SAMPLER_VIEW
)) {
915 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
916 bind
|= PIPE_BIND_DEPTH_STENCIL
;
918 bind
|= PIPE_BIND_RENDER_TARGET
;
920 /* GetTexImage only returns a single face for cubemaps. */
921 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
922 gl_target
= GL_TEXTURE_2D
;
924 pipe_target
= gl_target_to_pipe(gl_target
);
926 /* Choose the destination format by finding the best match
927 * for the format+type combo. */
928 dst_format
= st_choose_matching_format(screen
, bind
, format
, type
,
929 ctx
->Pack
.SwapBytes
);
931 if (dst_format
== PIPE_FORMAT_NONE
) {
934 /* Fall back to _mesa_get_teximage except for compressed formats,
935 * where decompression with a blit is always preferred. */
936 if (!util_format_is_compressed(src
->format
)) {
940 /* Set the appropriate format for the decompressed texture.
941 * Luminance and sRGB formats shouldn't appear here.*/
942 switch (src_format
) {
943 case PIPE_FORMAT_DXT1_RGB
:
944 case PIPE_FORMAT_DXT1_RGBA
:
945 case PIPE_FORMAT_DXT3_RGBA
:
946 case PIPE_FORMAT_DXT5_RGBA
:
947 case PIPE_FORMAT_RGTC1_UNORM
:
948 case PIPE_FORMAT_RGTC2_UNORM
:
949 case PIPE_FORMAT_ETC1_RGB8
:
950 dst_glformat
= GL_RGBA8
;
952 case PIPE_FORMAT_RGTC1_SNORM
:
953 case PIPE_FORMAT_RGTC2_SNORM
:
954 if (!ctx
->Extensions
.EXT_texture_snorm
)
956 dst_glformat
= GL_RGBA8_SNORM
;
958 /* TODO: for BPTC_*FLOAT, set RGBA32F and check for ARB_texture_float */
964 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
965 pipe_target
, 0, bind
, FALSE
);
967 if (dst_format
== PIPE_FORMAT_NONE
) {
968 /* unable to get an rgba format!?! */
973 /* create the destination texture */
974 memset(&dst_templ
, 0, sizeof(dst_templ
));
975 dst_templ
.target
= pipe_target
;
976 dst_templ
.format
= dst_format
;
977 dst_templ
.bind
= bind
;
978 dst_templ
.usage
= PIPE_USAGE_STAGING
;
980 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
981 &dst_templ
.width0
, &dst_templ
.height0
,
982 &dst_templ
.depth0
, &dst_templ
.array_size
);
984 dst
= screen
->resource_create(screen
, &dst_templ
);
989 /* From now on, we need the gallium representation of dimensions. */
990 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
995 blit
.src
.resource
= src
;
996 blit
.src
.level
= texImage
->Level
;
997 blit
.src
.format
= src_format
;
998 blit
.dst
.resource
= dst
;
1000 blit
.dst
.format
= dst
->format
;
1001 blit
.src
.box
.x
= blit
.dst
.box
.x
= 0;
1002 blit
.src
.box
.y
= blit
.dst
.box
.y
= 0;
1003 blit
.src
.box
.z
= texImage
->Face
;
1005 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
1006 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
1007 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
1008 blit
.mask
= st_get_blit_mask(texImage
->_BaseFormat
, format
);
1009 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1010 blit
.scissor_enable
= FALSE
;
1012 /* blit/render/decompress */
1013 st
->pipe
->blit(st
->pipe
, &blit
);
1015 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
1017 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
1018 0, 0, 0, width
, height
, depth
, &tex_xfer
);
1023 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
1025 /* copy/pack data into user buffer */
1026 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
1027 ctx
->Pack
.SwapBytes
)) {
1029 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
1032 for (slice
= 0; slice
< depth
; slice
++) {
1033 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1034 /* 1D array textures.
1035 * We need to convert gallium coords to GL coords.
1037 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1038 width
, depth
, format
,
1040 memcpy(dest
, map
, bytesPerRow
);
1043 ubyte
*slice_map
= map
;
1045 for (row
= 0; row
< height
; row
++) {
1046 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1047 width
, height
, format
,
1048 type
, slice
, row
, 0);
1049 memcpy(dest
, slice_map
, bytesPerRow
);
1050 slice_map
+= tex_xfer
->stride
;
1053 map
+= tex_xfer
->layer_stride
;
1057 /* format translation via floats */
1061 assert(util_format_is_compressed(src
->format
));
1063 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
1068 if (ST_DEBUG
& DEBUG_FALLBACK
)
1069 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
1071 for (slice
= 0; slice
< depth
; slice
++) {
1072 if (gl_target
== GL_TEXTURE_1D_ARRAY
) {
1073 /* 1D array textures.
1074 * We need to convert gallium coords to GL coords.
1076 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1077 width
, depth
, format
,
1080 /* get float[4] rgba row from surface */
1081 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, 0, width
, 1,
1084 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
1085 type
, dest
, &ctx
->Pack
, 0);
1088 for (row
= 0; row
< height
; row
++) {
1089 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
1090 width
, height
, format
,
1091 type
, slice
, row
, 0);
1093 /* get float[4] rgba row from surface */
1094 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, row
, width
, 1,
1097 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
1098 type
, dest
, &ctx
->Pack
, 0);
1101 map
+= tex_xfer
->layer_stride
;
1110 pipe_transfer_unmap(pipe
, tex_xfer
);
1112 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
1113 pipe_resource_reference(&dst
, NULL
);
1117 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
1123 * Do a CopyTexSubImage operation using a read transfer from the source,
1124 * a write transfer to the destination and get_tile()/put_tile() to access
1125 * the pixels/texels.
1127 * Note: srcY=0=TOP of renderbuffer
1130 fallback_copy_texsubimage(struct gl_context
*ctx
,
1131 struct st_renderbuffer
*strb
,
1132 struct st_texture_image
*stImage
,
1134 GLint destX
, GLint destY
, GLint slice
,
1135 GLint srcX
, GLint srcY
,
1136 GLsizei width
, GLsizei height
)
1138 struct st_context
*st
= st_context(ctx
);
1139 struct pipe_context
*pipe
= st
->pipe
;
1140 struct pipe_transfer
*src_trans
;
1142 enum pipe_transfer_usage transfer_usage
;
1144 unsigned dst_width
= width
;
1145 unsigned dst_height
= height
;
1146 unsigned dst_depth
= 1;
1148 if (ST_DEBUG
& DEBUG_FALLBACK
)
1149 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1151 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1152 srcY
= strb
->Base
.Height
- srcY
- height
;
1155 map
= pipe_transfer_map(pipe
,
1157 strb
->surface
->u
.tex
.level
,
1158 strb
->surface
->u
.tex
.first_layer
,
1161 width
, height
, &src_trans
);
1163 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1164 baseFormat
== GL_DEPTH_STENCIL
) &&
1165 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1166 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1168 transfer_usage
= PIPE_TRANSFER_WRITE
;
1170 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
1171 destX
, destY
, slice
,
1172 dst_width
, dst_height
, dst_depth
);
1174 if (baseFormat
== GL_DEPTH_COMPONENT
||
1175 baseFormat
== GL_DEPTH_STENCIL
) {
1176 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1177 ctx
->Pixel
.DepthBias
!= 0.0F
);
1181 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1182 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1191 data
= malloc(width
* sizeof(uint
));
1194 /* To avoid a large temp memory allocation, do copy row by row */
1195 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1196 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
1198 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1201 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
1202 pipe_put_tile_z(stImage
->transfer
,
1203 texDest
+ row
*stImage
->transfer
->layer_stride
,
1204 0, 0, width
, 1, data
);
1207 pipe_put_tile_z(stImage
->transfer
, texDest
, 0, row
, width
, 1,
1213 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
1221 malloc(width
* height
* 4 * sizeof(GLfloat
));
1223 if (tempSrc
&& texDest
) {
1224 const GLint dims
= 2;
1226 struct gl_texture_image
*texImage
= &stImage
->base
;
1227 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1229 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1230 unpack
.Invert
= GL_TRUE
;
1233 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
1234 dstRowStride
= stImage
->transfer
->layer_stride
;
1237 dstRowStride
= stImage
->transfer
->stride
;
1240 /* get float/RGBA image from framebuffer */
1241 /* XXX this usually involves a lot of int/float conversion.
1242 * try to avoid that someday.
1244 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
1245 util_format_linear(strb
->texture
->format
),
1248 /* Store into texture memory.
1249 * Note that this does some special things such as pixel transfer
1250 * ops and format conversion. In particular, if the dest tex format
1251 * is actually RGBA but the user created the texture as GL_RGB we
1252 * need to fill-in/override the alpha channel with 1.0.
1254 _mesa_texstore(ctx
, dims
,
1255 texImage
->_BaseFormat
,
1256 texImage
->TexFormat
,
1260 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1264 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1270 st_texture_image_unmap(st
, stImage
);
1271 pipe
->transfer_unmap(pipe
, src_trans
);
1276 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1277 * Note that the region to copy has already been clipped so we know we
1278 * won't read from outside the source renderbuffer's bounds.
1280 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1283 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1284 struct gl_texture_image
*texImage
,
1285 GLint destX
, GLint destY
, GLint slice
,
1286 struct gl_renderbuffer
*rb
,
1287 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
1289 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1290 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
1291 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
1292 struct st_context
*st
= st_context(ctx
);
1293 struct pipe_context
*pipe
= st
->pipe
;
1294 struct pipe_screen
*screen
= pipe
->screen
;
1295 struct pipe_blit_info blit
;
1296 enum pipe_format dst_format
;
1297 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1301 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1302 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1306 if (_mesa_texstore_needs_transfer_ops(ctx
, texImage
->_BaseFormat
,
1307 texImage
->TexFormat
)) {
1311 /* The base internal format must match the mesa format, so make sure
1312 * e.g. an RGB internal format is really allocated as RGB and not as RGBA.
1314 if (texImage
->_BaseFormat
!=
1315 _mesa_get_format_base_format(texImage
->TexFormat
) ||
1316 rb
->_BaseFormat
!= _mesa_get_format_base_format(rb
->Format
)) {
1320 /* Choose the destination format to match the TexImage behavior. */
1321 dst_format
= util_format_linear(stImage
->pt
->format
);
1322 dst_format
= util_format_luminance_to_red(dst_format
);
1323 dst_format
= util_format_intensity_to_red(dst_format
);
1325 /* See if the destination format is supported. */
1326 if (texImage
->_BaseFormat
== GL_DEPTH_STENCIL
||
1327 texImage
->_BaseFormat
== GL_DEPTH_COMPONENT
) {
1328 bind
= PIPE_BIND_DEPTH_STENCIL
;
1331 bind
= PIPE_BIND_RENDER_TARGET
;
1335 !screen
->is_format_supported(screen
, dst_format
, stImage
->pt
->target
,
1336 stImage
->pt
->nr_samples
, bind
)) {
1340 /* Y flipping for the main framebuffer. */
1342 srcY1
= strb
->Base
.Height
- srcY
- height
;
1343 srcY0
= srcY1
+ height
;
1347 srcY1
= srcY0
+ height
;
1350 /* Blit the texture.
1351 * This supports flipping, format conversions, and downsampling.
1353 memset(&blit
, 0, sizeof(blit
));
1354 blit
.src
.resource
= strb
->texture
;
1355 blit
.src
.format
= util_format_linear(strb
->surface
->format
);
1356 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
1357 blit
.src
.box
.x
= srcX
;
1358 blit
.src
.box
.y
= srcY0
;
1359 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
1360 blit
.src
.box
.width
= width
;
1361 blit
.src
.box
.height
= srcY1
- srcY0
;
1362 blit
.src
.box
.depth
= 1;
1363 blit
.dst
.resource
= stImage
->pt
;
1364 blit
.dst
.format
= dst_format
;
1365 blit
.dst
.level
= stObj
->pt
!= stImage
->pt
? 0 : texImage
->Level
;
1366 blit
.dst
.box
.x
= destX
;
1367 blit
.dst
.box
.y
= destY
;
1368 blit
.dst
.box
.z
= stImage
->base
.Face
+ slice
;
1369 blit
.dst
.box
.width
= width
;
1370 blit
.dst
.box
.height
= height
;
1371 blit
.dst
.box
.depth
= 1;
1372 blit
.mask
= st_get_blit_mask(rb
->_BaseFormat
, texImage
->_BaseFormat
);
1373 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1374 pipe
->blit(pipe
, &blit
);
1378 /* software fallback */
1379 fallback_copy_texsubimage(ctx
,
1380 strb
, stImage
, texImage
->_BaseFormat
,
1381 destX
, destY
, slice
,
1382 srcX
, srcY
, width
, height
);
1387 * Copy image data from stImage into the texture object 'stObj' at level
1391 copy_image_data_to_texture(struct st_context
*st
,
1392 struct st_texture_object
*stObj
,
1394 struct st_texture_image
*stImage
)
1398 const struct gl_texture_image
*dstImage
=
1399 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
1401 assert(dstImage
->Width
== stImage
->base
.Width
);
1402 assert(dstImage
->Height
== stImage
->base
.Height
);
1403 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1407 /* Copy potentially with the blitter:
1410 if (stImage
->pt
->last_level
== 0)
1413 src_level
= stImage
->base
.Level
;
1415 assert(src_level
<= stImage
->pt
->last_level
);
1416 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
1417 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
1418 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
1419 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
1420 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
1421 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
1423 st_texture_image_copy(st
->pipe
,
1424 stObj
->pt
, dstLevel
, /* dest texture, level */
1425 stImage
->pt
, src_level
, /* src texture, level */
1426 stImage
->base
.Face
);
1428 pipe_resource_reference(&stImage
->pt
, NULL
);
1430 else if (stImage
->TexData
) {
1431 /* Copy from malloc'd memory */
1432 /* XXX this should be re-examined/tested with a compressed format */
1433 GLuint blockSize
= util_format_get_blocksize(stObj
->pt
->format
);
1434 GLuint srcRowStride
= stImage
->base
.Width
* blockSize
;
1435 GLuint srcSliceStride
= stImage
->base
.Height
* srcRowStride
;
1436 st_texture_image_data(st
,
1443 _mesa_align_free(stImage
->TexData
);
1444 stImage
->TexData
= NULL
;
1447 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1452 * Called during state validation. When this function is finished,
1453 * the texture object should be ready for rendering.
1454 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1457 st_finalize_texture(struct gl_context
*ctx
,
1458 struct pipe_context
*pipe
,
1459 struct gl_texture_object
*tObj
)
1461 struct st_context
*st
= st_context(ctx
);
1462 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1463 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1465 struct st_texture_image
*firstImage
;
1466 enum pipe_format firstImageFormat
;
1467 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, ptNumSamples
;
1469 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
1470 /* The texture is complete and we know exactly how many mipmap levels
1471 * are present/needed. This is conditional because we may be called
1472 * from the st_generate_mipmap() function when the texture object is
1473 * incomplete. In that case, we'll have set stObj->lastLevel before
1476 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1477 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1478 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1480 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1483 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
1484 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
1487 pipe_resource_reference(&stObj
->pt
, NULL
);
1488 st_texture_release_all_sampler_views(stObj
);
1492 if (st_obj
->buffer
!= stObj
->pt
) {
1493 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
1494 st_texture_release_all_sampler_views(stObj
);
1495 stObj
->width0
= stObj
->pt
->width0
/ _mesa_get_format_bytes(tObj
->_BufferObjectFormat
);
1503 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1506 /* If both firstImage and stObj point to a texture which can contain
1507 * all active images, favour firstImage. Note that because of the
1508 * completeness requirement, we know that the image dimensions
1511 if (firstImage
->pt
&&
1512 firstImage
->pt
!= stObj
->pt
&&
1513 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1514 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1515 st_texture_release_all_sampler_views(stObj
);
1518 /* If this texture comes from a window system, there is nothing else to do. */
1519 if (stObj
->surface_based
) {
1523 /* Find gallium format for the Mesa texture */
1524 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1526 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1528 GLuint width
, height
, depth
;
1529 if (!guess_base_level_size(stObj
->base
.Target
,
1530 firstImage
->base
.Width2
,
1531 firstImage
->base
.Height2
,
1532 firstImage
->base
.Depth2
,
1533 firstImage
->base
.Level
,
1534 &width
, &height
, &depth
)) {
1535 width
= stObj
->width0
;
1536 height
= stObj
->height0
;
1537 depth
= stObj
->depth0
;
1539 /* convert GL dims to Gallium dims */
1540 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1541 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1542 ptNumSamples
= firstImage
->base
.NumSamples
;
1545 /* If we already have a gallium texture, check that it matches the texture
1546 * object's format, target, size, num_levels, etc.
1549 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1550 stObj
->pt
->format
!= firstImageFormat
||
1551 stObj
->pt
->last_level
< stObj
->lastLevel
||
1552 stObj
->pt
->width0
!= ptWidth
||
1553 stObj
->pt
->height0
!= ptHeight
||
1554 stObj
->pt
->depth0
!= ptDepth
||
1555 stObj
->pt
->nr_samples
!= ptNumSamples
||
1556 stObj
->pt
->array_size
!= ptLayers
)
1558 /* The gallium texture does not match the Mesa texture so delete the
1559 * gallium texture now. We'll make a new one below.
1561 pipe_resource_reference(&stObj
->pt
, NULL
);
1562 st_texture_release_all_sampler_views(stObj
);
1563 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1567 /* May need to create a new gallium texture:
1570 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1572 stObj
->pt
= st_texture_create(st
,
1573 gl_target_to_pipe(stObj
->base
.Target
),
1579 ptLayers
, ptNumSamples
,
1583 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1588 /* Pull in any images not in the object's texture:
1590 for (face
= 0; face
< nr_faces
; face
++) {
1592 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1593 struct st_texture_image
*stImage
=
1594 st_texture_image(stObj
->base
.Image
[face
][level
]);
1596 /* Need to import images in main memory or held in other textures.
1598 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1600 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1601 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1602 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
1603 /* src image fits expected dest mipmap level size */
1604 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1615 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1616 * for a whole mipmap stack.
1619 st_AllocTextureStorage(struct gl_context
*ctx
,
1620 struct gl_texture_object
*texObj
,
1621 GLsizei levels
, GLsizei width
,
1622 GLsizei height
, GLsizei depth
)
1624 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
1625 struct gl_texture_image
*texImage
= texObj
->Image
[0][0];
1626 struct st_context
*st
= st_context(ctx
);
1627 struct st_texture_object
*stObj
= st_texture_object(texObj
);
1628 struct pipe_screen
*screen
= st
->pipe
->screen
;
1629 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
1630 enum pipe_format fmt
;
1632 GLuint num_samples
= texImage
->NumSamples
;
1636 /* Save the level=0 dimensions */
1637 stObj
->width0
= width
;
1638 stObj
->height0
= height
;
1639 stObj
->depth0
= depth
;
1640 stObj
->lastLevel
= levels
- 1;
1642 fmt
= st_mesa_format_to_pipe_format(texImage
->TexFormat
);
1644 bindings
= default_bindings(st
, fmt
);
1646 /* Raise the sample count if the requested one is unsupported. */
1647 if (num_samples
> 1) {
1648 boolean found
= FALSE
;
1650 for (; num_samples
<= ctx
->Const
.MaxSamples
; num_samples
++) {
1651 if (screen
->is_format_supported(screen
, fmt
, PIPE_TEXTURE_2D
,
1653 PIPE_BIND_SAMPLER_VIEW
)) {
1654 /* Update the sample count in gl_texture_image as well. */
1655 texImage
->NumSamples
= num_samples
;
1666 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
1667 width
, height
, depth
,
1668 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1670 stObj
->pt
= st_texture_create(st
,
1671 gl_target_to_pipe(texObj
->Target
),
1677 ptLayers
, num_samples
,
1682 /* Set image resource pointers */
1683 for (level
= 0; level
< levels
; level
++) {
1685 for (face
= 0; face
< numFaces
; face
++) {
1686 struct st_texture_image
*stImage
=
1687 st_texture_image(texObj
->Image
[face
][level
]);
1688 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1697 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
1698 GLint level
, mesa_format format
,
1699 GLint width
, GLint height
,
1700 GLint depth
, GLint border
)
1702 struct st_context
*st
= st_context(ctx
);
1703 struct pipe_context
*pipe
= st
->pipe
;
1705 if (width
== 0 || height
== 0 || depth
== 0) {
1706 /* zero-sized images are legal, and always fit! */
1710 if (pipe
->screen
->can_create_resource
) {
1711 /* Ask the gallium driver if the texture is too large */
1712 struct gl_texture_object
*texObj
=
1713 _mesa_get_current_tex_object(ctx
, target
);
1714 struct pipe_resource pt
;
1716 /* Setup the pipe_resource object
1718 memset(&pt
, 0, sizeof(pt
));
1720 pt
.target
= gl_target_to_pipe(target
);
1721 pt
.format
= st_mesa_format_to_pipe_format(format
);
1723 st_gl_texture_dims_to_pipe_dims(target
,
1724 width
, height
, depth
,
1725 &pt
.width0
, &pt
.height0
,
1726 &pt
.depth0
, &pt
.array_size
);
1728 if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
1729 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
1730 /* assume just one mipmap level */
1734 /* assume a full set of mipmaps */
1735 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
1738 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
1741 /* Use core Mesa fallback */
1742 return _mesa_test_proxy_teximage(ctx
, target
, level
, format
,
1743 width
, height
, depth
, border
);
1749 st_init_texture_functions(struct dd_function_table
*functions
)
1751 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1752 functions
->QuerySamplesForFormat
= st_QuerySamplesForFormat
;
1753 functions
->TexImage
= st_TexImage
;
1754 functions
->TexSubImage
= st_TexSubImage
;
1755 functions
->CompressedTexSubImage
= _mesa_store_compressed_texsubimage
;
1756 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
1757 functions
->GenerateMipmap
= st_generate_mipmap
;
1759 functions
->GetTexImage
= st_GetTexImage
;
1761 /* compressed texture functions */
1762 functions
->CompressedTexImage
= st_CompressedTexImage
;
1763 functions
->GetCompressedTexImage
= _mesa_get_compressed_teximage
;
1765 functions
->NewTextureObject
= st_NewTextureObject
;
1766 functions
->NewTextureImage
= st_NewTextureImage
;
1767 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
1768 functions
->DeleteTexture
= st_DeleteTextureObject
;
1769 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
1770 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
1771 functions
->MapTextureImage
= st_MapTextureImage
;
1772 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
1774 /* XXX Temporary until we can query pipe's texture sizes */
1775 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
1777 functions
->AllocTextureStorage
= st_AllocTextureStorage
;