1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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/mfeatures.h"
29 #include "main/bufferobj.h"
30 #include "main/enums.h"
31 #include "main/fbobject.h"
32 #include "main/formats.h"
33 #include "main/image.h"
34 #include "main/imports.h"
35 #include "main/macros.h"
36 #include "main/mipmap.h"
37 #include "main/pack.h"
39 #include "main/pixeltransfer.h"
40 #include "main/texcompress.h"
41 #include "main/texgetimage.h"
42 #include "main/teximage.h"
43 #include "main/texobj.h"
44 #include "main/texstore.h"
46 #include "state_tracker/st_debug.h"
47 #include "state_tracker/st_context.h"
48 #include "state_tracker/st_cb_fbo.h"
49 #include "state_tracker/st_cb_flush.h"
50 #include "state_tracker/st_cb_texture.h"
51 #include "state_tracker/st_cb_bufferobjects.h"
52 #include "state_tracker/st_format.h"
53 #include "state_tracker/st_texture.h"
54 #include "state_tracker/st_gen_mipmap.h"
55 #include "state_tracker/st_atom.h"
57 #include "pipe/p_context.h"
58 #include "pipe/p_defines.h"
59 #include "util/u_inlines.h"
60 #include "pipe/p_shader_tokens.h"
61 #include "util/u_tile.h"
62 #include "util/u_blit.h"
63 #include "util/u_format.h"
64 #include "util/u_surface.h"
65 #include "util/u_sampler.h"
66 #include "util/u_math.h"
67 #include "util/u_box.h"
69 #define DBG if (0) printf
72 static enum pipe_texture_target
73 gl_target_to_pipe(GLenum target
)
77 case GL_PROXY_TEXTURE_1D
:
78 return PIPE_TEXTURE_1D
;
80 case GL_PROXY_TEXTURE_2D
:
81 case GL_TEXTURE_EXTERNAL_OES
:
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 return PIPE_TEXTURE_2D_ARRAY
;
104 case GL_TEXTURE_BUFFER
:
106 case GL_TEXTURE_CUBE_MAP_ARRAY
:
107 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY
:
108 return PIPE_TEXTURE_CUBE_ARRAY
;
116 /** called via ctx->Driver.NewTextureImage() */
117 static struct gl_texture_image
*
118 st_NewTextureImage(struct gl_context
* ctx
)
120 DBG("%s\n", __FUNCTION__
);
122 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
126 /** called via ctx->Driver.DeleteTextureImage() */
128 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
130 /* nothing special (yet) for st_texture_image */
131 _mesa_delete_texture_image(ctx
, img
);
135 /** called via ctx->Driver.NewTextureObject() */
136 static struct gl_texture_object
*
137 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
139 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
141 DBG("%s\n", __FUNCTION__
);
142 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
147 /** called via ctx->Driver.DeleteTextureObject() */
149 st_DeleteTextureObject(struct gl_context
*ctx
,
150 struct gl_texture_object
*texObj
)
152 struct st_context
*st
= st_context(ctx
);
153 struct st_texture_object
*stObj
= st_texture_object(texObj
);
155 pipe_resource_reference(&stObj
->pt
, NULL
);
156 if (stObj
->sampler_view
) {
157 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
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 if (stImage
->TexData
) {
177 _mesa_align_free(stImage
->TexData
);
178 stImage
->TexData
= NULL
;
183 /** called via ctx->Driver.MapTextureImage() */
185 st_MapTextureImage(struct gl_context
*ctx
,
186 struct gl_texture_image
*texImage
,
187 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
189 GLubyte
**mapOut
, GLint
*rowStrideOut
)
191 struct st_context
*st
= st_context(ctx
);
192 struct st_texture_image
*stImage
= st_texture_image(texImage
);
197 if (mode
& GL_MAP_READ_BIT
)
198 pipeMode
|= PIPE_TRANSFER_READ
;
199 if (mode
& GL_MAP_WRITE_BIT
)
200 pipeMode
|= PIPE_TRANSFER_WRITE
;
201 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
202 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
204 map
= st_texture_image_map(st
, stImage
, pipeMode
, x
, y
, slice
, w
, h
, 1);
207 *rowStrideOut
= stImage
->transfer
->stride
;
216 /** called via ctx->Driver.UnmapTextureImage() */
218 st_UnmapTextureImage(struct gl_context
*ctx
,
219 struct gl_texture_image
*texImage
,
222 struct st_context
*st
= st_context(ctx
);
223 struct st_texture_image
*stImage
= st_texture_image(texImage
);
224 st_texture_image_unmap(st
, stImage
);
229 * Return default texture resource binding bitmask for the given format.
232 default_bindings(struct st_context
*st
, enum pipe_format format
)
234 struct pipe_screen
*screen
= st
->pipe
->screen
;
235 const unsigned target
= PIPE_TEXTURE_2D
;
238 if (util_format_is_depth_or_stencil(format
))
239 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
241 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
243 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
247 format
= util_format_linear(format
);
249 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
252 return PIPE_BIND_SAMPLER_VIEW
;
258 * Given the size of a mipmap image, try to compute the size of the level=0
261 * Note that this isn't always accurate for odd-sized, non-POW textures.
262 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
264 * \return GL_TRUE for success, GL_FALSE for failure
267 guess_base_level_size(GLenum target
,
268 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
269 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
276 /* Guess the size of the base level.
277 * Depending on the image's size, we can't always make a guess here.
281 case GL_TEXTURE_1D_ARRAY
:
286 case GL_TEXTURE_2D_ARRAY
:
287 /* We can't make a good guess here, because the base level dimensions
290 if (width
== 1 || height
== 1) {
297 case GL_TEXTURE_CUBE_MAP
:
298 case GL_TEXTURE_CUBE_MAP_ARRAY
:
304 /* We can't make a good guess here, because the base level dimensions
307 if (width
== 1 || height
== 1 || depth
== 1) {
315 case GL_TEXTURE_RECTANGLE
:
332 * Try to allocate a pipe_resource object for the given st_texture_object.
334 * We use the given st_texture_image as a clue to determine the size of the
335 * mipmap image at level=0.
337 * \return GL_TRUE for success, GL_FALSE if out of memory.
340 guess_and_alloc_texture(struct st_context
*st
,
341 struct st_texture_object
*stObj
,
342 const struct st_texture_image
*stImage
)
344 GLuint lastLevel
, width
, height
, depth
;
346 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
347 enum pipe_format fmt
;
349 DBG("%s\n", __FUNCTION__
);
353 if (!guess_base_level_size(stObj
->base
.Target
,
354 stImage
->base
.Width2
,
355 stImage
->base
.Height2
,
356 stImage
->base
.Depth2
,
358 &width
, &height
, &depth
)) {
359 /* we can't determine the image size at level=0 */
360 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
361 /* this is not an out of memory error */
365 /* At this point, (width x height x depth) is the expected size of
366 * the level=0 mipmap image.
369 /* Guess a reasonable value for lastLevel. With OpenGL we have no
370 * idea how many mipmap levels will be in a texture until we start
371 * to render with it. Make an educated guess here but be prepared
372 * to re-allocating a texture buffer with space for more (or fewer)
373 * mipmap levels later.
375 if ((stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
376 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
377 (stObj
->base
.BaseLevel
== 0 &&
378 stObj
->base
.MaxLevel
== 0) ||
379 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
380 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
381 !stObj
->base
.GenerateMipmap
&&
382 stImage
->base
.Level
== 0) {
383 /* only alloc space for a single mipmap level */
387 /* alloc space for a full mipmap */
388 lastLevel
= _mesa_get_tex_max_num_levels(stObj
->base
.Target
,
389 width
, height
, depth
) - 1;
392 /* Save the level=0 dimensions */
393 stObj
->width0
= width
;
394 stObj
->height0
= height
;
395 stObj
->depth0
= depth
;
397 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
399 bindings
= default_bindings(st
, fmt
);
401 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
402 width
, height
, depth
,
403 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
405 stObj
->pt
= st_texture_create(st
,
406 gl_target_to_pipe(stObj
->base
.Target
),
415 stObj
->lastLevel
= lastLevel
;
417 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
419 return stObj
->pt
!= NULL
;
424 * Called via ctx->Driver.AllocTextureImageBuffer().
425 * If the texture object/buffer already has space for the indicated image,
426 * we're done. Otherwise, allocate memory for the new texture image.
429 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
430 struct gl_texture_image
*texImage
)
432 struct st_context
*st
= st_context(ctx
);
433 struct st_texture_image
*stImage
= st_texture_image(texImage
);
434 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
435 const GLuint level
= texImage
->Level
;
436 GLuint width
= texImage
->Width
;
437 GLuint height
= texImage
->Height
;
438 GLuint depth
= texImage
->Depth
;
440 DBG("%s\n", __FUNCTION__
);
442 assert(!stImage
->TexData
);
443 assert(!stImage
->pt
); /* xxx this might be wrong */
445 /* Look if the parent texture object has space for this image */
447 level
<= stObj
->pt
->last_level
&&
448 st_texture_match_image(stObj
->pt
, texImage
)) {
449 /* this image will fit in the existing texture object's memory */
450 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
454 /* The parent texture object does not have space for this image */
456 pipe_resource_reference(&stObj
->pt
, NULL
);
457 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
459 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
460 /* Probably out of memory.
461 * Try flushing any pending rendering, then retry.
464 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
465 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
471 st_texture_match_image(stObj
->pt
, texImage
)) {
472 /* The image will live in the object's mipmap memory */
473 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
478 /* Create a new, temporary texture/resource/buffer to hold this
479 * one texture image. Note that when we later access this image
480 * (either for mapping or copying) we'll want to always specify
481 * mipmap level=0, even if the image represents some other mipmap
484 enum pipe_format format
=
485 st_mesa_format_to_pipe_format(texImage
->TexFormat
);
486 GLuint bindings
= default_bindings(st
, format
);
487 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
489 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
490 width
, height
, depth
,
491 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
493 stImage
->pt
= st_texture_create(st
,
494 gl_target_to_pipe(stObj
->base
.Target
),
502 return stImage
->pt
!= NULL
;
508 * Preparation prior to glTexImage. Basically check the 'surface_based'
509 * field and switch to a "normal" tex image if necessary.
512 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
513 GLenum format
, GLenum type
)
515 struct gl_texture_object
*texObj
= texImage
->TexObject
;
516 struct st_texture_object
*stObj
= st_texture_object(texObj
);
518 /* switch to "normal" */
519 if (stObj
->surface_based
) {
520 const GLenum target
= texObj
->Target
;
521 const GLuint level
= texImage
->Level
;
524 _mesa_clear_texture_object(ctx
, texObj
);
525 pipe_resource_reference(&stObj
->pt
, NULL
);
527 /* oops, need to init this image again */
528 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
529 texImage
->InternalFormat
, format
,
532 _mesa_init_teximage_fields(ctx
, texImage
,
533 texImage
->Width
, texImage
->Height
,
534 texImage
->Depth
, texImage
->Border
,
535 texImage
->InternalFormat
, texFormat
);
537 stObj
->surface_based
= GL_FALSE
;
543 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
544 struct gl_texture_image
*texImage
,
545 GLenum format
, GLenum type
, const void *pixels
,
546 const struct gl_pixelstore_attrib
*unpack
)
548 prep_teximage(ctx
, texImage
, format
, type
);
549 _mesa_store_teximage(ctx
, dims
, texImage
, format
, type
, pixels
, unpack
);
554 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
555 struct gl_texture_image
*texImage
,
556 GLsizei imageSize
, const GLvoid
*data
)
558 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
559 _mesa_store_compressed_teximage(ctx
, dims
, texImage
, imageSize
, data
);
563 static enum pipe_format
564 choose_matching_format(struct pipe_screen
*screen
, unsigned bind
,
565 GLenum format
, GLenum type
, GLboolean swapBytes
)
567 gl_format mesa_format
;
569 for (mesa_format
= 1; mesa_format
< MESA_FORMAT_COUNT
; mesa_format
++) {
570 if (_mesa_get_format_color_encoding(mesa_format
) == GL_SRGB
) {
574 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
576 enum pipe_format format
= st_mesa_format_to_pipe_format(mesa_format
);
579 screen
->is_format_supported(screen
, format
, PIPE_TEXTURE_2D
, 0,
583 /* It's unlikely to find 2 matching Mesa formats. */
587 return PIPE_FORMAT_NONE
;
592 * Called via ctx->Driver.GetTexImage()
594 * This uses a blit to copy the texture to a texture format which matches
595 * the format and type combo and then a fast read-back is done using memcpy.
596 * We can do arbitrary X/Y/Z/W/0/1 swizzling here as long as there is
597 * a format which matches the swizzling.
599 * If such a format isn't available, it falls back to _mesa_get_teximage.
601 * NOTE: Drivers usually do a blit to convert between tiled and linear
602 * texture layouts during texture uploads/downloads, so the blit
603 * we do here should be free in such cases.
606 st_GetTexImage(struct gl_context
* ctx
,
607 GLenum format
, GLenum type
, GLvoid
* pixels
,
608 struct gl_texture_image
*texImage
)
610 struct st_context
*st
= st_context(ctx
);
611 struct pipe_context
*pipe
= st
->pipe
;
612 struct pipe_screen
*screen
= pipe
->screen
;
613 const GLuint width
= texImage
->Width
;
614 const GLuint height
= texImage
->Height
;
615 const GLuint depth
= texImage
->Depth
;
616 struct st_texture_image
*stImage
= st_texture_image(texImage
);
617 struct pipe_resource
*src
= st_texture_object(texImage
->TexObject
)->pt
;
618 struct pipe_resource
*dst
= NULL
;
619 struct pipe_resource dst_templ
;
620 enum pipe_format dst_format
, src_format
;
621 gl_format mesa_format
;
622 GLenum gl_target
= texImage
->TexObject
->Target
;
623 enum pipe_texture_target pipe_target
;
624 struct pipe_blit_info blit
;
625 unsigned bind
= PIPE_BIND_TRANSFER_READ
;
626 struct pipe_transfer
*tex_xfer
;
628 boolean done
= FALSE
;
634 /* XXX Fallback to _mesa_get_teximage for depth-stencil formats
635 * due to an incomplete stencil blit implementation in some drivers. */
636 if (format
== GL_DEPTH_STENCIL
) {
640 /* If the base internal format and the texture format don't match, we have
641 * to fall back to _mesa_get_teximage. */
642 if (texImage
->_BaseFormat
!=
643 _mesa_get_format_base_format(texImage
->TexFormat
)) {
647 /* See if the texture format already matches the format and type,
648 * in which case the memcpy-based fast path will be used. */
649 if (_mesa_format_matches_format_and_type(texImage
->TexFormat
, format
,
650 type
, ctx
->Pack
.SwapBytes
)) {
654 /* Convert the source format to what is expected by GetTexImage
655 * and see if it's supported.
657 * This only applies to glGetTexImage:
658 * - Luminance must be returned as (L,0,0,1).
659 * - Luminance alpha must be returned as (L,0,0,A).
660 * - Intensity must be returned as (I,0,0,1)
662 src_format
= util_format_linear(src
->format
);
663 src_format
= util_format_luminance_to_red(src_format
);
664 src_format
= util_format_intensity_to_red(src_format
);
667 !screen
->is_format_supported(screen
, src_format
, src
->target
,
669 PIPE_BIND_SAMPLER_VIEW
)) {
673 if (format
== GL_DEPTH_COMPONENT
|| format
== GL_DEPTH_STENCIL
)
674 bind
|= PIPE_BIND_DEPTH_STENCIL
;
676 bind
|= PIPE_BIND_RENDER_TARGET
;
678 /* GetTexImage only returns a single face for cubemaps. */
679 if (gl_target
== GL_TEXTURE_CUBE_MAP
) {
680 gl_target
= GL_TEXTURE_2D
;
682 pipe_target
= gl_target_to_pipe(gl_target
);
684 /* Choose the destination format by finding the best match
685 * for the format+type combo. */
686 dst_format
= choose_matching_format(screen
, bind
, format
, type
,
687 ctx
->Pack
.SwapBytes
);
689 if (dst_format
== PIPE_FORMAT_NONE
) {
692 /* Fall back to _mesa_get_teximage except for compressed formats,
693 * where decompression with a blit is always preferred. */
694 if (!util_format_is_compressed(src
->format
)) {
698 /* Set the appropriate format for the decompressed texture.
699 * Luminance and sRGB formats shouldn't appear here.*/
700 switch (src_format
) {
701 case PIPE_FORMAT_DXT1_RGB
:
702 case PIPE_FORMAT_DXT1_RGBA
:
703 case PIPE_FORMAT_DXT3_RGBA
:
704 case PIPE_FORMAT_DXT5_RGBA
:
705 case PIPE_FORMAT_RGTC1_UNORM
:
706 case PIPE_FORMAT_RGTC2_UNORM
:
707 case PIPE_FORMAT_ETC1_RGB8
:
708 dst_glformat
= GL_RGBA8
;
710 case PIPE_FORMAT_RGTC1_SNORM
:
711 case PIPE_FORMAT_RGTC2_SNORM
:
712 if (!ctx
->Extensions
.EXT_texture_snorm
)
714 dst_glformat
= GL_RGBA8_SNORM
;
716 /* TODO: for BPTC_*FLOAT, set RGBA32F and check for ARB_texture_float */
722 dst_format
= st_choose_format(st
, dst_glformat
, format
, type
,
723 pipe_target
, 0, bind
, FALSE
);
725 if (dst_format
== PIPE_FORMAT_NONE
) {
726 /* unable to get an rgba format!?! */
731 /* create the destination texture */
732 memset(&dst_templ
, 0, sizeof(dst_templ
));
733 dst_templ
.target
= pipe_target
;
734 dst_templ
.format
= dst_format
;
735 dst_templ
.bind
= bind
;
736 dst_templ
.usage
= PIPE_USAGE_STAGING
;
738 st_gl_texture_dims_to_pipe_dims(gl_target
, width
, height
, depth
,
739 &dst_templ
.width0
, &dst_templ
.height0
,
740 &dst_templ
.depth0
, &dst_templ
.array_size
);
742 dst
= screen
->resource_create(screen
, &dst_templ
);
747 blit
.src
.resource
= src
;
748 blit
.src
.level
= texImage
->Level
;
749 blit
.src
.format
= src_format
;
750 blit
.dst
.resource
= dst
;
752 blit
.dst
.format
= dst
->format
;
753 blit
.src
.box
.x
= blit
.dst
.box
.x
= 0;
754 blit
.src
.box
.y
= blit
.dst
.box
.y
= 0;
755 blit
.src
.box
.z
= texImage
->Face
;
757 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
758 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
759 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= depth
;
760 blit
.mask
= PIPE_MASK_RGBA
;
761 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
762 blit
.scissor_enable
= FALSE
;
764 /* blit/render/decompress */
765 st
->pipe
->blit(st
->pipe
, &blit
);
767 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
769 map
= pipe_transfer_map_3d(pipe
, dst
, 0, PIPE_TRANSFER_READ
,
770 0, 0, 0, width
, height
, depth
, &tex_xfer
);
775 mesa_format
= st_pipe_format_to_mesa_format(dst_format
);
777 /* copy/pack data into user buffer */
778 if (_mesa_format_matches_format_and_type(mesa_format
, format
, type
,
779 ctx
->Pack
.SwapBytes
)) {
781 const uint bytesPerRow
= width
* util_format_get_blocksize(dst_format
);
784 for (slice
= 0; slice
< depth
; slice
++) {
785 ubyte
*slice_map
= map
;
787 for (row
= 0; row
< height
; row
++) {
788 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
789 width
, height
, format
,
790 type
, slice
, row
, 0);
791 memcpy(dest
, slice_map
, bytesPerRow
);
792 slice_map
+= tex_xfer
->stride
;
794 map
+= tex_xfer
->layer_stride
;
798 /* format translation via floats */
802 assert(util_format_is_compressed(src
->format
));
804 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
809 for (slice
= 0; slice
< depth
; slice
++) {
810 for (row
= 0; row
< height
; row
++) {
811 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
812 GLvoid
*dest
= _mesa_image_address3d(&ctx
->Pack
, pixels
,
813 width
, height
, format
,
814 type
, slice
, row
, 0);
816 if (ST_DEBUG
& DEBUG_FALLBACK
)
817 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
819 /* get float[4] rgba row from surface */
820 pipe_get_tile_rgba_format(tex_xfer
, map
, 0, row
, width
, 1,
823 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
824 type
, dest
, &ctx
->Pack
, transferOps
);
826 map
+= tex_xfer
->layer_stride
;
835 pipe_transfer_unmap(pipe
, tex_xfer
);
837 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
838 pipe_resource_reference(&dst
, NULL
);
842 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
848 * Do a CopyTexSubImage operation using a read transfer from the source,
849 * a write transfer to the destination and get_tile()/put_tile() to access
852 * Note: srcY=0=TOP of renderbuffer
855 fallback_copy_texsubimage(struct gl_context
*ctx
,
856 struct st_renderbuffer
*strb
,
857 struct st_texture_image
*stImage
,
859 GLint destX
, GLint destY
, GLint destZ
,
860 GLint srcX
, GLint srcY
,
861 GLsizei width
, GLsizei height
)
863 struct st_context
*st
= st_context(ctx
);
864 struct pipe_context
*pipe
= st
->pipe
;
865 struct pipe_transfer
*src_trans
;
867 enum pipe_transfer_usage transfer_usage
;
869 unsigned dst_width
= width
;
870 unsigned dst_height
= height
;
871 unsigned dst_depth
= 1;
873 if (ST_DEBUG
& DEBUG_FALLBACK
)
874 debug_printf("%s: fallback processing\n", __FUNCTION__
);
876 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
877 srcY
= strb
->Base
.Height
- srcY
- height
;
880 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
881 /* Move y/height to z/depth for 1D array textures. */
884 dst_depth
= dst_height
;
888 map
= pipe_transfer_map(pipe
,
891 strb
->rtt_face
+ strb
->rtt_slice
,
894 width
, height
, &src_trans
);
896 if ((baseFormat
== GL_DEPTH_COMPONENT
||
897 baseFormat
== GL_DEPTH_STENCIL
) &&
898 util_format_is_depth_and_stencil(stImage
->pt
->format
))
899 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
901 transfer_usage
= PIPE_TRANSFER_WRITE
;
903 texDest
= st_texture_image_map(st
, stImage
, transfer_usage
,
905 dst_width
, dst_height
, dst_depth
);
907 if (baseFormat
== GL_DEPTH_COMPONENT
||
908 baseFormat
== GL_DEPTH_STENCIL
) {
909 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
910 ctx
->Pixel
.DepthBias
!= 0.0F
);
914 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
915 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
924 data
= malloc(width
* sizeof(uint
));
927 /* To avoid a large temp memory allocation, do copy row by row */
928 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
929 pipe_get_tile_z(src_trans
, map
, 0, srcY
, width
, 1, data
);
931 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
934 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
935 pipe_put_tile_z(stImage
->transfer
,
936 texDest
+ row
*stImage
->transfer
->layer_stride
,
937 0, 0, width
, 1, data
);
940 pipe_put_tile_z(stImage
->transfer
, texDest
, 0, row
, width
, 1,
946 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
954 malloc(width
* height
* 4 * sizeof(GLfloat
));
956 if (tempSrc
&& texDest
) {
957 const GLint dims
= 2;
959 struct gl_texture_image
*texImage
= &stImage
->base
;
960 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
962 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
963 unpack
.Invert
= GL_TRUE
;
966 if (stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
) {
967 dstRowStride
= stImage
->transfer
->layer_stride
;
970 dstRowStride
= stImage
->transfer
->stride
;
973 /* get float/RGBA image from framebuffer */
974 /* XXX this usually involves a lot of int/float conversion.
975 * try to avoid that someday.
977 pipe_get_tile_rgba_format(src_trans
, map
, 0, 0, width
, height
,
978 util_format_linear(strb
->texture
->format
),
981 /* Store into texture memory.
982 * Note that this does some special things such as pixel transfer
983 * ops and format conversion. In particular, if the dest tex format
984 * is actually RGBA but the user created the texture as GL_RGB we
985 * need to fill-in/override the alpha channel with 1.0.
987 _mesa_texstore(ctx
, dims
,
988 texImage
->_BaseFormat
,
993 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
997 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1003 st_texture_image_unmap(st
, stImage
);
1004 pipe
->transfer_unmap(pipe
, src_trans
);
1010 * If the format of the src renderbuffer and the format of the dest
1011 * texture are compatible (in terms of blitting), return a TGSI writemask
1012 * to be used during the blit.
1013 * If the src/dest are incompatible, return 0.
1016 compatible_src_dst_formats(struct gl_context
*ctx
,
1017 const struct gl_renderbuffer
*src
,
1018 const struct gl_texture_image
*dst
)
1020 /* Get logical base formats for the src and dest.
1021 * That is, use the user-requested formats and not the actual, device-
1023 * For example, the user may have requested an A8 texture but the
1024 * driver may actually be using an RGBA texture format. When we
1025 * copy/blit to that texture, we only want to copy the Alpha channel
1026 * and not the RGB channels.
1028 * Similarly, when the src FBO was created an RGB format may have been
1029 * requested but the driver actually chose an RGBA format. In that case,
1030 * we don't want to copy the undefined Alpha channel to the dest texture
1031 * (it should be 1.0).
1033 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1034 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1037 * XXX when we have red-only and red/green renderbuffers we'll need
1038 * to add more cases here (or implement a general-purpose routine that
1039 * queries the existance of the R,G,B,A channels in the src and dest).
1041 if (srcFormat
== dstFormat
) {
1042 /* This is the same as matching_base_formats, which should
1043 * always pass, as it did previously.
1045 return TGSI_WRITEMASK_XYZW
;
1047 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1048 /* Make sure that A in the dest is 1. The actual src format
1049 * may be RGBA and have undefined A values.
1051 return TGSI_WRITEMASK_XYZ
;
1053 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1054 /* Make sure that A in the dest is 1. The actual dst format
1055 * may be RGBA and will need A=1 to provide proper alpha values
1056 * when sampled later.
1058 return TGSI_WRITEMASK_XYZ
;
1061 if (ST_DEBUG
& DEBUG_FALLBACK
)
1062 debug_printf("%s failed for src %s, dst %s\n",
1064 _mesa_lookup_enum_by_nr(srcFormat
),
1065 _mesa_lookup_enum_by_nr(dstFormat
));
1075 * Do pipe->blit. Return FALSE if the blitting is unsupported
1076 * for the given formats.
1079 st_pipe_blit(struct pipe_context
*pipe
, struct pipe_blit_info
*blit
)
1081 struct pipe_screen
*screen
= pipe
->screen
;
1084 if (util_format_is_depth_or_stencil(blit
->dst
.format
)) {
1085 dst_usage
= PIPE_BIND_DEPTH_STENCIL
;
1088 dst_usage
= PIPE_BIND_RENDER_TARGET
;
1091 /* try resource_copy_region in case the format is not supported
1093 if (util_try_blit_via_copy_region(pipe
, blit
)) {
1094 return GL_TRUE
; /* done */
1097 /* check the format support */
1098 if (!screen
->is_format_supported(screen
, blit
->src
.format
,
1100 PIPE_BIND_SAMPLER_VIEW
) ||
1101 !screen
->is_format_supported(screen
, blit
->dst
.format
,
1107 pipe
->blit(pipe
, blit
);
1113 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1114 * Note that the region to copy has already been clipped so we know we
1115 * won't read from outside the source renderbuffer's bounds.
1117 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1120 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
1121 struct gl_texture_image
*texImage
,
1122 GLint destX
, GLint destY
, GLint destZ
,
1123 struct gl_renderbuffer
*rb
,
1124 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
1126 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1127 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1128 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
1129 struct st_context
*st
= st_context(ctx
);
1130 struct pipe_context
*pipe
= st
->pipe
;
1131 struct pipe_screen
*screen
= pipe
->screen
;
1132 enum pipe_format dest_format
, src_format
;
1133 GLuint color_writemask
;
1134 struct pipe_surface
*dest_surface
= NULL
;
1135 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1136 struct pipe_surface surf_tmpl
;
1139 GLint srcY0
, srcY1
, yStep
;
1141 /* make sure finalize_textures has been called?
1143 if (0) st_validate_state(st
);
1145 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1146 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1151 assert(strb
->surface
);
1152 assert(stImage
->pt
);
1154 src_format
= strb
->surface
->format
;
1155 dest_format
= stImage
->pt
->format
;
1158 srcY1
= strb
->Base
.Height
- srcY
- height
;
1159 srcY0
= srcY1
+ height
;
1164 srcY1
= srcY0
+ height
;
1168 if (ctx
->_ImageTransferState
) {
1172 /* Compressed and subsampled textures aren't supported for blitting. */
1173 if (!util_format_is_plain(dest_format
)) {
1177 /* Set the blit writemask. */
1178 switch (texBaseFormat
) {
1179 case GL_DEPTH_STENCIL
:
1180 switch (strb
->Base
._BaseFormat
) {
1181 case GL_DEPTH_STENCIL
:
1182 blit_mask
= PIPE_MASK_ZS
;
1184 case GL_DEPTH_COMPONENT
:
1185 blit_mask
= PIPE_MASK_Z
;
1187 case GL_STENCIL_INDEX
:
1188 blit_mask
= PIPE_MASK_S
;
1194 dst_usage
= PIPE_BIND_DEPTH_STENCIL
;
1197 case GL_DEPTH_COMPONENT
:
1198 blit_mask
= PIPE_MASK_Z
;
1199 dst_usage
= PIPE_BIND_DEPTH_STENCIL
;
1205 * Determine if the src framebuffer and dest texture have the same
1206 * base format. We need this to detect a case such as the framebuffer
1207 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1208 * texture format stores RGBA we need to set A=1 (overriding the
1209 * framebuffer's alpha values).
1211 * XXX util_blit_pixels doesn't support MSAA resolve, so always use
1214 if (texBaseFormat
== strb
->Base
._BaseFormat
||
1215 strb
->texture
->nr_samples
> 1) {
1216 blit_mask
= PIPE_MASK_RGBA
;
1221 dst_usage
= PIPE_BIND_RENDER_TARGET
;
1224 /* Blit the texture.
1225 * This supports flipping, format conversions, and downsampling.
1228 /* If stImage->pt is an independent image (not a pointer into a full
1229 * mipmap) stImage->pt.last_level will be zero and we need to use that
1230 * as the dest level.
1232 unsigned dstLevel
= MIN2(stImage
->base
.Level
, stImage
->pt
->last_level
);
1233 struct pipe_blit_info blit
;
1235 memset(&blit
, 0, sizeof(blit
));
1236 blit
.src
.resource
= strb
->texture
;
1237 blit
.src
.format
= src_format
;
1238 blit
.src
.level
= strb
->surface
->u
.tex
.level
;
1239 blit
.src
.box
.x
= srcX
;
1240 blit
.src
.box
.y
= srcY0
;
1241 blit
.src
.box
.z
= strb
->surface
->u
.tex
.first_layer
;
1242 blit
.src
.box
.width
= width
;
1243 blit
.src
.box
.height
= srcY1
- srcY0
;
1244 blit
.src
.box
.depth
= 1;
1245 blit
.dst
.resource
= stImage
->pt
;
1246 blit
.dst
.format
= dest_format
;
1247 blit
.dst
.level
= dstLevel
;
1248 blit
.dst
.box
.x
= destX
;
1249 blit
.dst
.box
.y
= destY
;
1250 blit
.dst
.box
.z
= stImage
->base
.Face
+ destZ
;
1251 blit
.dst
.box
.width
= width
;
1252 blit
.dst
.box
.height
= height
;
1253 blit
.dst
.box
.depth
= 1;
1254 blit
.mask
= blit_mask
;
1255 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
1257 /* 1D array textures need special treatment.
1258 * Blit rows from the source to layers in the destination. */
1259 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
1262 for (y
= srcY0
, layer
= 0; layer
< height
; y
+= yStep
, layer
++) {
1264 blit
.src
.box
.height
= 1;
1266 blit
.dst
.box
.height
= 1;
1267 blit
.dst
.box
.z
= destY
+ layer
;
1269 if (!st_pipe_blit(pipe
, &blit
)) {
1275 /* All the other texture targets. */
1276 if (!st_pipe_blit(pipe
, &blit
)) {
1284 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
1285 /* u_blit cannot copy 1D array textures as required by CopyTexSubImage */
1289 color_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1291 if (!color_writemask
||
1292 !screen
->is_format_supported(screen
, src_format
,
1294 PIPE_BIND_SAMPLER_VIEW
) ||
1295 !screen
->is_format_supported(screen
, dest_format
,
1301 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1302 surf_tmpl
.format
= util_format_linear(stImage
->pt
->format
);
1303 surf_tmpl
.u
.tex
.level
= stImage
->base
.Level
;
1304 surf_tmpl
.u
.tex
.first_layer
= stImage
->base
.Face
+ destZ
;
1305 surf_tmpl
.u
.tex
.last_layer
= stImage
->base
.Face
+ destZ
;
1307 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1309 util_blit_pixels(st
->blit
,
1311 strb
->surface
->u
.tex
.level
,
1313 srcX
+ width
, srcY1
,
1314 strb
->surface
->u
.tex
.first_layer
,
1317 destX
+ width
, destY
+ height
,
1318 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1319 color_writemask
, 0);
1320 pipe_surface_reference(&dest_surface
, NULL
);
1324 /* software fallback */
1325 fallback_copy_texsubimage(ctx
,
1326 strb
, stImage
, texBaseFormat
,
1327 destX
, destY
, destZ
,
1328 srcX
, srcY
, width
, height
);
1333 * Copy image data from stImage into the texture object 'stObj' at level
1337 copy_image_data_to_texture(struct st_context
*st
,
1338 struct st_texture_object
*stObj
,
1340 struct st_texture_image
*stImage
)
1344 const struct gl_texture_image
*dstImage
=
1345 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
1347 assert(dstImage
->Width
== stImage
->base
.Width
);
1348 assert(dstImage
->Height
== stImage
->base
.Height
);
1349 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1353 /* Copy potentially with the blitter:
1356 if (stImage
->pt
->last_level
== 0)
1359 src_level
= stImage
->base
.Level
;
1361 assert(src_level
<= stImage
->pt
->last_level
);
1362 assert(u_minify(stImage
->pt
->width0
, src_level
) == stImage
->base
.Width
);
1363 assert(stImage
->pt
->target
== PIPE_TEXTURE_1D_ARRAY
||
1364 u_minify(stImage
->pt
->height0
, src_level
) == stImage
->base
.Height
);
1365 assert(stImage
->pt
->target
== PIPE_TEXTURE_2D_ARRAY
||
1366 stImage
->pt
->target
== PIPE_TEXTURE_CUBE_ARRAY
||
1367 u_minify(stImage
->pt
->depth0
, src_level
) == stImage
->base
.Depth
);
1369 st_texture_image_copy(st
->pipe
,
1370 stObj
->pt
, dstLevel
, /* dest texture, level */
1371 stImage
->pt
, src_level
, /* src texture, level */
1372 stImage
->base
.Face
);
1374 pipe_resource_reference(&stImage
->pt
, NULL
);
1376 else if (stImage
->TexData
) {
1377 /* Copy from malloc'd memory */
1378 /* XXX this should be re-examined/tested with a compressed format */
1379 GLuint blockSize
= util_format_get_blocksize(stObj
->pt
->format
);
1380 GLuint srcRowStride
= stImage
->base
.Width
* blockSize
;
1381 GLuint srcSliceStride
= stImage
->base
.Height
* srcRowStride
;
1382 st_texture_image_data(st
,
1389 _mesa_align_free(stImage
->TexData
);
1390 stImage
->TexData
= NULL
;
1393 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1398 * Called during state validation. When this function is finished,
1399 * the texture object should be ready for rendering.
1400 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1403 st_finalize_texture(struct gl_context
*ctx
,
1404 struct pipe_context
*pipe
,
1405 struct gl_texture_object
*tObj
)
1407 struct st_context
*st
= st_context(ctx
);
1408 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1409 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1411 struct st_texture_image
*firstImage
;
1412 enum pipe_format firstImageFormat
;
1413 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1415 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
1416 /* The texture is complete and we know exactly how many mipmap levels
1417 * are present/needed. This is conditional because we may be called
1418 * from the st_generate_mipmap() function when the texture object is
1419 * incomplete. In that case, we'll have set stObj->lastLevel before
1422 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1423 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1424 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1426 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1429 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
1430 struct st_buffer_object
*st_obj
= st_buffer_object(tObj
->BufferObject
);
1432 if (st_obj
->buffer
!= stObj
->pt
) {
1433 pipe_resource_reference(&stObj
->pt
, st_obj
->buffer
);
1434 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
1435 stObj
->width0
= stObj
->pt
->width0
/ _mesa_get_format_bytes(tObj
->_BufferObjectFormat
);
1443 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1446 /* If both firstImage and stObj point to a texture which can contain
1447 * all active images, favour firstImage. Note that because of the
1448 * completeness requirement, we know that the image dimensions
1451 if (firstImage
->pt
&&
1452 firstImage
->pt
!= stObj
->pt
&&
1453 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1454 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1455 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
1458 /* Find gallium format for the Mesa texture */
1459 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1461 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1463 GLuint width
, height
, depth
;
1464 if (!guess_base_level_size(stObj
->base
.Target
,
1465 firstImage
->base
.Width2
,
1466 firstImage
->base
.Height2
,
1467 firstImage
->base
.Depth2
,
1468 firstImage
->base
.Level
,
1469 &width
, &height
, &depth
)) {
1470 width
= stObj
->width0
;
1471 height
= stObj
->height0
;
1472 depth
= stObj
->depth0
;
1474 /* convert GL dims to Gallium dims */
1475 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1476 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1479 /* If we already have a gallium texture, check that it matches the texture
1480 * object's format, target, size, num_levels, etc.
1483 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1484 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1485 stObj
->pt
->last_level
< stObj
->lastLevel
||
1486 stObj
->pt
->width0
!= ptWidth
||
1487 stObj
->pt
->height0
!= ptHeight
||
1488 stObj
->pt
->depth0
!= ptDepth
||
1489 stObj
->pt
->array_size
!= ptLayers
)
1491 /* The gallium texture does not match the Mesa texture so delete the
1492 * gallium texture now. We'll make a new one below.
1494 pipe_resource_reference(&stObj
->pt
, NULL
);
1495 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
1496 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1500 /* May need to create a new gallium texture:
1503 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1505 stObj
->pt
= st_texture_create(st
,
1506 gl_target_to_pipe(stObj
->base
.Target
),
1516 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1521 /* Pull in any images not in the object's texture:
1523 for (face
= 0; face
< nr_faces
; face
++) {
1525 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1526 struct st_texture_image
*stImage
=
1527 st_texture_image(stObj
->base
.Image
[face
][level
]);
1529 /* Need to import images in main memory or held in other textures.
1531 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1533 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1534 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1535 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
1536 /* src image fits expected dest mipmap level size */
1537 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1548 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1549 * for a whole mipmap stack.
1552 st_AllocTextureStorage(struct gl_context
*ctx
,
1553 struct gl_texture_object
*texObj
,
1554 GLsizei levels
, GLsizei width
,
1555 GLsizei height
, GLsizei depth
)
1557 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
1558 struct st_context
*st
= st_context(ctx
);
1559 struct st_texture_object
*stObj
= st_texture_object(texObj
);
1560 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
1561 enum pipe_format fmt
;
1566 /* Save the level=0 dimensions */
1567 stObj
->width0
= width
;
1568 stObj
->height0
= height
;
1569 stObj
->depth0
= depth
;
1570 stObj
->lastLevel
= levels
- 1;
1572 fmt
= st_mesa_format_to_pipe_format(texObj
->Image
[0][0]->TexFormat
);
1574 bindings
= default_bindings(st
, fmt
);
1576 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
1577 width
, height
, depth
,
1578 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1580 stObj
->pt
= st_texture_create(st
,
1581 gl_target_to_pipe(texObj
->Target
),
1592 /* Set image resource pointers */
1593 for (level
= 0; level
< levels
; level
++) {
1595 for (face
= 0; face
< numFaces
; face
++) {
1596 struct st_texture_image
*stImage
=
1597 st_texture_image(texObj
->Image
[face
][level
]);
1598 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1607 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
1608 GLint level
, gl_format format
,
1609 GLint width
, GLint height
,
1610 GLint depth
, GLint border
)
1612 struct st_context
*st
= st_context(ctx
);
1613 struct pipe_context
*pipe
= st
->pipe
;
1615 if (width
== 0 || height
== 0 || depth
== 0) {
1616 /* zero-sized images are legal, and always fit! */
1620 if (pipe
->screen
->can_create_resource
) {
1621 /* Ask the gallium driver if the texture is too large */
1622 struct gl_texture_object
*texObj
=
1623 _mesa_get_current_tex_object(ctx
, target
);
1624 struct pipe_resource pt
;
1626 /* Setup the pipe_resource object
1628 memset(&pt
, 0, sizeof(pt
));
1630 pt
.target
= gl_target_to_pipe(target
);
1631 pt
.format
= st_mesa_format_to_pipe_format(format
);
1633 st_gl_texture_dims_to_pipe_dims(target
,
1634 width
, height
, depth
,
1635 &pt
.width0
, &pt
.height0
,
1636 &pt
.depth0
, &pt
.array_size
);
1638 if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
1639 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
1640 /* assume just one mipmap level */
1644 /* assume a full set of mipmaps */
1645 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
1648 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
1651 /* Use core Mesa fallback */
1652 return _mesa_test_proxy_teximage(ctx
, target
, level
, format
,
1653 width
, height
, depth
, border
);
1659 st_init_texture_functions(struct dd_function_table
*functions
)
1661 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1662 functions
->QuerySamplesForFormat
= st_QuerySamplesForFormat
;
1663 functions
->TexImage
= st_TexImage
;
1664 functions
->TexSubImage
= _mesa_store_texsubimage
;
1665 functions
->CompressedTexSubImage
= _mesa_store_compressed_texsubimage
;
1666 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
1667 functions
->GenerateMipmap
= st_generate_mipmap
;
1669 functions
->GetTexImage
= st_GetTexImage
;
1671 /* compressed texture functions */
1672 functions
->CompressedTexImage
= st_CompressedTexImage
;
1673 functions
->GetCompressedTexImage
= _mesa_get_compressed_teximage
;
1675 functions
->NewTextureObject
= st_NewTextureObject
;
1676 functions
->NewTextureImage
= st_NewTextureImage
;
1677 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
1678 functions
->DeleteTexture
= st_DeleteTextureObject
;
1679 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
1680 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
1681 functions
->MapTextureImage
= st_MapTextureImage
;
1682 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
1684 /* XXX Temporary until we can query pipe's texture sizes */
1685 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
1687 functions
->AllocTextureStorage
= st_AllocTextureStorage
;