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_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_blit.h"
62 #include "util/u_format.h"
63 #include "util/u_surface.h"
64 #include "util/u_sampler.h"
65 #include "util/u_math.h"
66 #include "util/u_box.h"
68 #define DBG if (0) printf
71 static enum pipe_texture_target
72 gl_target_to_pipe(GLenum target
)
76 case GL_PROXY_TEXTURE_1D
:
77 return PIPE_TEXTURE_1D
;
79 case GL_PROXY_TEXTURE_2D
:
80 case GL_TEXTURE_EXTERNAL_OES
:
81 return PIPE_TEXTURE_2D
;
82 case GL_TEXTURE_RECTANGLE_NV
:
83 case GL_PROXY_TEXTURE_RECTANGLE_NV
:
84 return PIPE_TEXTURE_RECT
;
86 case GL_PROXY_TEXTURE_3D
:
87 return PIPE_TEXTURE_3D
;
88 case GL_TEXTURE_CUBE_MAP_ARB
:
89 case GL_PROXY_TEXTURE_CUBE_MAP_ARB
:
90 case GL_TEXTURE_CUBE_MAP_POSITIVE_X
:
91 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X
:
92 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y
:
93 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
:
94 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z
:
95 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
:
96 return PIPE_TEXTURE_CUBE
;
97 case GL_TEXTURE_1D_ARRAY_EXT
:
98 case GL_PROXY_TEXTURE_1D_ARRAY_EXT
:
99 return PIPE_TEXTURE_1D_ARRAY
;
100 case GL_TEXTURE_2D_ARRAY_EXT
:
101 case GL_PROXY_TEXTURE_2D_ARRAY_EXT
:
102 return PIPE_TEXTURE_2D_ARRAY
;
103 case GL_TEXTURE_BUFFER
:
112 /** called via ctx->Driver.NewTextureImage() */
113 static struct gl_texture_image
*
114 st_NewTextureImage(struct gl_context
* ctx
)
116 DBG("%s\n", __FUNCTION__
);
118 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
122 /** called via ctx->Driver.DeleteTextureImage() */
124 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
126 /* nothing special (yet) for st_texture_image */
127 _mesa_delete_texture_image(ctx
, img
);
131 /** called via ctx->Driver.NewTextureObject() */
132 static struct gl_texture_object
*
133 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
135 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
137 DBG("%s\n", __FUNCTION__
);
138 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
143 /** called via ctx->Driver.DeleteTextureObject() */
145 st_DeleteTextureObject(struct gl_context
*ctx
,
146 struct gl_texture_object
*texObj
)
148 struct st_context
*st
= st_context(ctx
);
149 struct st_texture_object
*stObj
= st_texture_object(texObj
);
151 pipe_resource_reference(&stObj
->pt
, NULL
);
152 if (stObj
->sampler_view
) {
153 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
155 _mesa_delete_texture_object(ctx
, texObj
);
159 /** called via ctx->Driver.FreeTextureImageBuffer() */
161 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
162 struct gl_texture_image
*texImage
)
164 struct st_texture_image
*stImage
= st_texture_image(texImage
);
166 DBG("%s\n", __FUNCTION__
);
169 pipe_resource_reference(&stImage
->pt
, NULL
);
172 if (stImage
->TexData
) {
173 _mesa_align_free(stImage
->TexData
);
174 stImage
->TexData
= NULL
;
179 /** called via ctx->Driver.MapTextureImage() */
181 st_MapTextureImage(struct gl_context
*ctx
,
182 struct gl_texture_image
*texImage
,
183 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
185 GLubyte
**mapOut
, GLint
*rowStrideOut
)
187 struct st_context
*st
= st_context(ctx
);
188 struct st_texture_image
*stImage
= st_texture_image(texImage
);
193 if (mode
& GL_MAP_READ_BIT
)
194 pipeMode
|= PIPE_TRANSFER_READ
;
195 if (mode
& GL_MAP_WRITE_BIT
)
196 pipeMode
|= PIPE_TRANSFER_WRITE
;
197 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
198 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
200 map
= st_texture_image_map(st
, stImage
, slice
, pipeMode
, x
, y
, w
, h
);
203 *rowStrideOut
= stImage
->transfer
->stride
;
212 /** called via ctx->Driver.UnmapTextureImage() */
214 st_UnmapTextureImage(struct gl_context
*ctx
,
215 struct gl_texture_image
*texImage
,
218 struct st_context
*st
= st_context(ctx
);
219 struct st_texture_image
*stImage
= st_texture_image(texImage
);
220 st_texture_image_unmap(st
, stImage
);
225 * Return default texture resource binding bitmask for the given format.
228 default_bindings(struct st_context
*st
, enum pipe_format format
)
230 struct pipe_screen
*screen
= st
->pipe
->screen
;
231 const unsigned target
= PIPE_TEXTURE_2D
;
234 if (util_format_is_depth_or_stencil(format
))
235 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
237 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
239 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
243 format
= util_format_linear(format
);
245 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
248 return PIPE_BIND_SAMPLER_VIEW
;
253 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
255 get_texture_dims(GLenum target
)
259 case GL_TEXTURE_1D_ARRAY_EXT
:
260 case GL_TEXTURE_BUFFER
:
263 case GL_TEXTURE_CUBE_MAP_ARB
:
264 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
265 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
266 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
267 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
268 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
269 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
270 case GL_TEXTURE_RECTANGLE_NV
:
271 case GL_TEXTURE_2D_ARRAY_EXT
:
272 case GL_TEXTURE_EXTERNAL_OES
:
277 assert(0 && "invalid texture target in get_texture_dims()");
284 * Given the size of a mipmap image, try to compute the size of the level=0
287 * Note that this isn't always accurate for odd-sized, non-POW textures.
288 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
290 * \return GL_TRUE for success, GL_FALSE for failure
293 guess_base_level_size(GLenum target
,
294 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
295 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
297 const GLuint dims
= get_texture_dims(target
);
304 /* Depending on the image's size, we can't always make a guess here */
305 if ((dims
>= 1 && width
== 1) ||
306 (dims
>= 2 && height
== 1) ||
307 (dims
>= 3 && depth
== 1)) {
308 /* we can't determine the image size at level=0 */
312 /* grow the image size until we hit level = 0 */
333 * Try to allocate a pipe_resource object for the given st_texture_object.
335 * We use the given st_texture_image as a clue to determine the size of the
336 * mipmap image at level=0.
338 * \return GL_TRUE for success, GL_FALSE if out of memory.
341 guess_and_alloc_texture(struct st_context
*st
,
342 struct st_texture_object
*stObj
,
343 const struct st_texture_image
*stImage
)
345 GLuint lastLevel
, width
, height
, depth
;
347 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
348 enum pipe_format fmt
;
350 DBG("%s\n", __FUNCTION__
);
354 if (!guess_base_level_size(stObj
->base
.Target
,
355 stImage
->base
.Width2
,
356 stImage
->base
.Height2
,
357 stImage
->base
.Depth2
,
359 &width
, &height
, &depth
)) {
360 /* we can't determine the image size at level=0 */
361 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
362 /* this is not an out of memory error */
366 /* At this point, (width x height x depth) is the expected size of
367 * the level=0 mipmap image.
370 /* Guess a reasonable value for lastLevel. With OpenGL we have no
371 * idea how many mipmap levels will be in a texture until we start
372 * to render with it. Make an educated guess here but be prepared
373 * to re-allocating a texture buffer with space for more (or fewer)
374 * mipmap levels later.
376 if ((stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
377 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
378 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
379 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
380 !stObj
->base
.GenerateMipmap
&&
381 stImage
->base
.Level
== 0) {
382 /* only alloc space for a single mipmap level */
386 /* alloc space for a full mipmap */
387 GLuint l2width
= util_logbase2(width
);
388 GLuint l2height
= util_logbase2(height
);
389 GLuint l2depth
= util_logbase2(depth
);
390 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
393 /* Save the level=0 dimensions */
394 stObj
->width0
= width
;
395 stObj
->height0
= height
;
396 stObj
->depth0
= depth
;
398 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
400 bindings
= default_bindings(st
, fmt
);
402 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
403 width
, height
, depth
,
404 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
406 stObj
->pt
= st_texture_create(st
,
407 gl_target_to_pipe(stObj
->base
.Target
),
416 stObj
->lastLevel
= lastLevel
;
418 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
420 return stObj
->pt
!= NULL
;
425 * Called via ctx->Driver.AllocTextureImageBuffer().
426 * If the texture object/buffer already has space for the indicated image,
427 * we're done. Otherwise, allocate memory for the new texture image.
430 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
431 struct gl_texture_image
*texImage
)
433 struct st_context
*st
= st_context(ctx
);
434 struct st_texture_image
*stImage
= st_texture_image(texImage
);
435 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
436 const GLuint level
= texImage
->Level
;
437 GLuint width
= texImage
->Width
;
438 GLuint height
= texImage
->Height
;
439 GLuint depth
= texImage
->Depth
;
441 DBG("%s\n", __FUNCTION__
);
443 assert(!stImage
->TexData
);
444 assert(!stImage
->pt
); /* xxx this might be wrong */
446 /* Look if the parent texture object has space for this image */
448 level
<= stObj
->pt
->last_level
&&
449 st_texture_match_image(stObj
->pt
, texImage
)) {
450 /* this image will fit in the existing texture object's memory */
451 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
455 /* The parent texture object does not have space for this image */
457 pipe_resource_reference(&stObj
->pt
, NULL
);
458 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
460 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
461 /* Probably out of memory.
462 * Try flushing any pending rendering, then retry.
465 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
466 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
472 st_texture_match_image(stObj
->pt
, texImage
)) {
473 /* The image will live in the object's mipmap memory */
474 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
479 /* Create a new, temporary texture/resource/buffer to hold this
480 * one texture image. Note that when we later access this image
481 * (either for mapping or copying) we'll want to always specify
482 * mipmap level=0, even if the image represents some other mipmap
485 enum pipe_format format
=
486 st_mesa_format_to_pipe_format(texImage
->TexFormat
);
487 GLuint bindings
= default_bindings(st
, format
);
488 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
490 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
491 width
, height
, depth
,
492 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
494 stImage
->pt
= st_texture_create(st
,
495 gl_target_to_pipe(stObj
->base
.Target
),
503 return stImage
->pt
!= NULL
;
509 * Preparation prior to glTexImage. Basically check the 'surface_based'
510 * field and switch to a "normal" tex image if necessary.
513 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
514 GLenum format
, GLenum type
)
516 struct gl_texture_object
*texObj
= texImage
->TexObject
;
517 struct st_texture_object
*stObj
= st_texture_object(texObj
);
519 /* switch to "normal" */
520 if (stObj
->surface_based
) {
521 const GLenum target
= texObj
->Target
;
522 const GLuint level
= texImage
->Level
;
525 _mesa_clear_texture_object(ctx
, texObj
);
526 pipe_resource_reference(&stObj
->pt
, NULL
);
528 /* oops, need to init this image again */
529 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
530 texImage
->InternalFormat
, format
,
533 _mesa_init_teximage_fields(ctx
, texImage
,
534 texImage
->Width
, texImage
->Height
,
535 texImage
->Depth
, texImage
->Border
,
536 texImage
->InternalFormat
, texFormat
);
538 stObj
->surface_based
= GL_FALSE
;
544 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
545 struct gl_texture_image
*texImage
,
546 GLenum format
, GLenum type
, const void *pixels
,
547 const struct gl_pixelstore_attrib
*unpack
)
549 prep_teximage(ctx
, texImage
, format
, type
);
550 _mesa_store_teximage(ctx
, dims
, texImage
, format
, type
, pixels
, unpack
);
555 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
556 struct gl_texture_image
*texImage
,
557 GLsizei imageSize
, const GLvoid
*data
)
559 prep_teximage(ctx
, texImage
, GL_NONE
, GL_NONE
);
560 _mesa_store_compressed_teximage(ctx
, dims
, texImage
, imageSize
, data
);
566 * glGetTexImage() helper: decompress a compressed texture by rendering
567 * a textured quad. Store the results in the user's buffer.
570 decompress_with_blit(struct gl_context
* ctx
,
571 GLenum format
, GLenum type
, GLvoid
*pixels
,
572 struct gl_texture_image
*texImage
)
574 struct st_context
*st
= st_context(ctx
);
575 struct pipe_context
*pipe
= st
->pipe
;
576 struct st_texture_image
*stImage
= st_texture_image(texImage
);
577 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
578 const GLuint width
= texImage
->Width
;
579 const GLuint height
= texImage
->Height
;
580 struct pipe_resource
*dst_texture
;
581 struct pipe_transfer
*tex_xfer
;
582 struct pipe_blit_info blit
;
583 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| PIPE_BIND_TRANSFER_READ
);
585 /* create temp / dest surface */
586 if (!util_create_rgba_texture(pipe
, width
, height
, bind
,
588 _mesa_problem(ctx
, "util_create_rgba_texture() failed "
589 "in decompress_with_blit()");
593 blit
.src
.resource
= stObj
->pt
;
594 blit
.src
.level
= texImage
->Level
;
595 blit
.src
.format
= util_format_linear(stObj
->pt
->format
);
596 blit
.dst
.resource
= dst_texture
;
598 blit
.dst
.format
= dst_texture
->format
;
599 blit
.src
.box
.x
= blit
.dst
.box
.x
= 0;
600 blit
.src
.box
.y
= blit
.dst
.box
.y
= 0;
601 blit
.src
.box
.z
= 0; /* XXX compressed array textures? */
603 blit
.src
.box
.width
= blit
.dst
.box
.width
= width
;
604 blit
.src
.box
.height
= blit
.dst
.box
.height
= height
;
605 blit
.src
.box
.depth
= blit
.dst
.box
.depth
= 1;
606 blit
.mask
= PIPE_MASK_RGBA
;
607 blit
.filter
= PIPE_TEX_FILTER_NEAREST
;
608 blit
.scissor_enable
= FALSE
;
610 /* blit/render/decompress */
611 st
->pipe
->blit(st
->pipe
, &blit
);
613 /* map the dst_surface so we can read from it */
614 tex_xfer
= pipe_get_transfer(pipe
,
617 0, 0, width
, height
);
619 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
621 /* copy/pack data into user buffer */
622 if (_mesa_format_matches_format_and_type(stImage
->base
.TexFormat
,
624 ctx
->Pack
.SwapBytes
)) {
626 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
627 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
629 for (row
= 0; row
< height
; row
++) {
630 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
631 height
, format
, type
, row
, 0);
632 memcpy(dest
, map
, bytesPerRow
);
633 map
+= tex_xfer
->stride
;
635 pipe_transfer_unmap(pipe
, tex_xfer
);
638 /* format translation via floats */
640 enum pipe_format pformat
= util_format_linear(dst_texture
->format
);
643 rgba
= malloc(width
* 4 * sizeof(GLfloat
));
645 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
649 for (row
= 0; row
< height
; row
++) {
650 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
651 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
652 height
, format
, type
, row
, 0);
654 if (ST_DEBUG
& DEBUG_FALLBACK
)
655 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
657 /* get float[4] rgba row from surface */
658 pipe_get_tile_rgba_format(pipe
, tex_xfer
, 0, row
, width
, 1,
661 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
662 type
, dest
, &ctx
->Pack
, transferOps
);
669 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
671 pipe
->transfer_destroy(pipe
, tex_xfer
);
673 pipe_resource_reference(&dst_texture
, NULL
);
679 * Called via ctx->Driver.GetTexImage()
682 st_GetTexImage(struct gl_context
* ctx
,
683 GLenum format
, GLenum type
, GLvoid
* pixels
,
684 struct gl_texture_image
*texImage
)
686 struct st_texture_image
*stImage
= st_texture_image(texImage
);
688 if (stImage
->pt
&& util_format_is_s3tc(stImage
->pt
->format
)) {
689 /* Need to decompress the texture.
690 * We'll do this by rendering a textured quad (which is hopefully
691 * faster than using the fallback code in texcompress.c).
692 * Note that we only expect RGBA formats (no Z/depth formats).
694 decompress_with_blit(ctx
, format
, type
, pixels
, texImage
);
697 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
703 * Do a CopyTexSubImage operation using a read transfer from the source,
704 * a write transfer to the destination and get_tile()/put_tile() to access
707 * Note: srcY=0=TOP of renderbuffer
710 fallback_copy_texsubimage(struct gl_context
*ctx
,
711 struct st_renderbuffer
*strb
,
712 struct st_texture_image
*stImage
,
714 GLint destX
, GLint destY
, GLint destZ
,
715 GLint srcX
, GLint srcY
,
716 GLsizei width
, GLsizei height
)
718 struct st_context
*st
= st_context(ctx
);
719 struct pipe_context
*pipe
= st
->pipe
;
720 struct pipe_transfer
*src_trans
;
722 enum pipe_transfer_usage transfer_usage
;
724 if (ST_DEBUG
& DEBUG_FALLBACK
)
725 debug_printf("%s: fallback processing\n", __FUNCTION__
);
727 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
728 srcY
= strb
->Base
.Height
- srcY
- height
;
731 src_trans
= pipe_get_transfer(pipe
,
734 strb
->rtt_face
+ strb
->rtt_slice
,
739 if ((baseFormat
== GL_DEPTH_COMPONENT
||
740 baseFormat
== GL_DEPTH_STENCIL
) &&
741 util_format_is_depth_and_stencil(stImage
->pt
->format
))
742 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
744 transfer_usage
= PIPE_TRANSFER_WRITE
;
746 /* XXX this used to ignore destZ param */
747 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
748 destX
, destY
, width
, height
);
750 if (baseFormat
== GL_DEPTH_COMPONENT
||
751 baseFormat
== GL_DEPTH_STENCIL
) {
752 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
753 ctx
->Pixel
.DepthBias
!= 0.0F
);
757 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
758 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
767 data
= malloc(width
* sizeof(uint
));
770 /* To avoid a large temp memory allocation, do copy row by row */
771 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
772 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
774 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
776 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
780 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
788 malloc(width
* height
* 4 * sizeof(GLfloat
));
790 if (tempSrc
&& texDest
) {
791 const GLint dims
= 2;
792 const GLint dstRowStride
= stImage
->transfer
->stride
;
793 struct gl_texture_image
*texImage
= &stImage
->base
;
794 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
796 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
797 unpack
.Invert
= GL_TRUE
;
800 /* get float/RGBA image from framebuffer */
801 /* XXX this usually involves a lot of int/float conversion.
802 * try to avoid that someday.
804 pipe_get_tile_rgba_format(pipe
, src_trans
, 0, 0, width
, height
,
805 util_format_linear(strb
->texture
->format
),
808 /* Store into texture memory.
809 * Note that this does some special things such as pixel transfer
810 * ops and format conversion. In particular, if the dest tex format
811 * is actually RGBA but the user created the texture as GL_RGB we
812 * need to fill-in/override the alpha channel with 1.0.
814 _mesa_texstore(ctx
, dims
,
815 texImage
->_BaseFormat
,
818 (GLubyte
**) &texDest
,
820 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
824 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
830 st_texture_image_unmap(st
, stImage
);
831 pipe
->transfer_destroy(pipe
, src_trans
);
837 * If the format of the src renderbuffer and the format of the dest
838 * texture are compatible (in terms of blitting), return a TGSI writemask
839 * to be used during the blit.
840 * If the src/dest are incompatible, return 0.
843 compatible_src_dst_formats(struct gl_context
*ctx
,
844 const struct gl_renderbuffer
*src
,
845 const struct gl_texture_image
*dst
)
847 /* Get logical base formats for the src and dest.
848 * That is, use the user-requested formats and not the actual, device-
850 * For example, the user may have requested an A8 texture but the
851 * driver may actually be using an RGBA texture format. When we
852 * copy/blit to that texture, we only want to copy the Alpha channel
853 * and not the RGB channels.
855 * Similarly, when the src FBO was created an RGB format may have been
856 * requested but the driver actually chose an RGBA format. In that case,
857 * we don't want to copy the undefined Alpha channel to the dest texture
858 * (it should be 1.0).
860 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
861 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
864 * XXX when we have red-only and red/green renderbuffers we'll need
865 * to add more cases here (or implement a general-purpose routine that
866 * queries the existance of the R,G,B,A channels in the src and dest).
868 if (srcFormat
== dstFormat
) {
869 /* This is the same as matching_base_formats, which should
870 * always pass, as it did previously.
872 return TGSI_WRITEMASK_XYZW
;
874 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
875 /* Make sure that A in the dest is 1. The actual src format
876 * may be RGBA and have undefined A values.
878 return TGSI_WRITEMASK_XYZ
;
880 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
881 /* Make sure that A in the dest is 1. The actual dst format
882 * may be RGBA and will need A=1 to provide proper alpha values
883 * when sampled later.
885 return TGSI_WRITEMASK_XYZ
;
888 if (ST_DEBUG
& DEBUG_FALLBACK
)
889 debug_printf("%s failed for src %s, dst %s\n",
891 _mesa_lookup_enum_by_nr(srcFormat
),
892 _mesa_lookup_enum_by_nr(dstFormat
));
903 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
904 * Note that the region to copy has already been clipped so we know we
905 * won't read from outside the source renderbuffer's bounds.
907 * Note: srcY=0=Bottom of renderbuffer (GL convention)
910 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
911 struct gl_texture_image
*texImage
,
912 GLint destX
, GLint destY
, GLint destZ
,
913 struct gl_renderbuffer
*rb
,
914 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
916 struct st_texture_image
*stImage
= st_texture_image(texImage
);
917 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
918 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
919 struct st_renderbuffer
*strb
;
920 struct st_context
*st
= st_context(ctx
);
921 struct pipe_context
*pipe
= st
->pipe
;
922 struct pipe_screen
*screen
= pipe
->screen
;
923 enum pipe_format dest_format
, src_format
;
924 GLboolean matching_base_formats
;
925 GLuint color_writemask
, zs_writemask
, sample_count
;
926 struct pipe_surface
*dest_surface
= NULL
;
927 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
928 struct pipe_surface surf_tmpl
;
929 unsigned int dst_usage
;
932 /* make sure finalize_textures has been called?
934 if (0) st_validate_state(st
);
936 /* determine if copying depth or color data */
937 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
938 texBaseFormat
== GL_DEPTH_STENCIL
) {
939 strb
= st_renderbuffer(fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
942 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
943 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
946 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
947 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
951 sample_count
= strb
->surface
->texture
->nr_samples
;
952 /* I believe this would be legal, presumably would need to do a resolve
953 for color, and for depth/stencil spec says to just use one of the
954 depth/stencil samples per pixel? Need some transfer clarifications. */
955 assert(sample_count
< 2);
958 assert(strb
->surface
);
961 src_format
= strb
->surface
->format
;
962 dest_format
= stImage
->pt
->format
;
965 * Determine if the src framebuffer and dest texture have the same
966 * base format. We need this to detect a case such as the framebuffer
967 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
968 * texture format stores RGBA we need to set A=1 (overriding the
969 * framebuffer's alpha values). We can't do that with the blit or
970 * textured-quad paths.
972 matching_base_formats
=
973 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
974 _mesa_get_format_base_format(texImage
->TexFormat
));
976 if (ctx
->_ImageTransferState
) {
980 if (texImage
->TexObject
->Target
== GL_TEXTURE_1D_ARRAY
) {
981 /* 1D arrays might be thought of as 2D images but the actual layout
982 * might not be that way. At some points, we convert OpenGL's 1D
983 * array 'height' into gallium 'layers' and that prevents the blit
984 * utility code from doing the right thing. Simpy use the memcpy-based
990 if (matching_base_formats
&&
991 src_format
== dest_format
&&
993 /* use surface_copy() / blit */
994 struct pipe_box src_box
;
997 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
998 width
, height
, &src_box
);
1000 /* If stImage->pt is an independent image (not a pointer into a full
1001 * mipmap) stImage->pt.last_level will be zero and we need to use that
1002 * as the dest level.
1004 dstLevel
= MIN2(stImage
->base
.Level
, stImage
->pt
->last_level
);
1006 /* for resource_copy_region(), y=0=top, always */
1007 pipe
->resource_copy_region(pipe
,
1011 destX
, destY
, destZ
+ stImage
->base
.Face
,
1014 strb
->surface
->u
.tex
.level
,
1019 if (texBaseFormat
== GL_DEPTH_STENCIL
) {
1023 if (texBaseFormat
== GL_DEPTH_COMPONENT
) {
1024 color_writemask
= 0;
1025 zs_writemask
= BLIT_WRITEMASK_Z
;
1026 dst_usage
= PIPE_BIND_DEPTH_STENCIL
;
1029 color_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1031 dst_usage
= PIPE_BIND_RENDER_TARGET
;
1034 if ((!color_writemask
&& !zs_writemask
) ||
1035 !screen
->is_format_supported(screen
, src_format
,
1036 PIPE_TEXTURE_2D
, sample_count
,
1037 PIPE_BIND_SAMPLER_VIEW
) ||
1038 !screen
->is_format_supported(screen
, dest_format
,
1045 srcY1
= strb
->Base
.Height
- srcY
- height
;
1046 srcY0
= srcY1
+ height
;
1050 srcY1
= srcY0
+ height
;
1053 /* Disable conditional rendering. */
1054 if (st
->render_condition
) {
1055 pipe
->render_condition(pipe
, NULL
, 0);
1058 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1059 surf_tmpl
.format
= util_format_linear(stImage
->pt
->format
);
1060 surf_tmpl
.usage
= dst_usage
;
1061 surf_tmpl
.u
.tex
.level
= stImage
->base
.Level
;
1062 surf_tmpl
.u
.tex
.first_layer
= stImage
->base
.Face
+ destZ
;
1063 surf_tmpl
.u
.tex
.last_layer
= stImage
->base
.Face
+ destZ
;
1065 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1067 util_blit_pixels(st
->blit
,
1069 strb
->surface
->u
.tex
.level
,
1071 srcX
+ width
, srcY1
,
1072 strb
->surface
->u
.tex
.first_layer
,
1075 destX
+ width
, destY
+ height
,
1076 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1077 color_writemask
, zs_writemask
);
1078 pipe_surface_reference(&dest_surface
, NULL
);
1080 /* Restore conditional rendering state. */
1081 if (st
->render_condition
) {
1082 pipe
->render_condition(pipe
, st
->render_condition
,
1083 st
->condition_mode
);
1089 /* software fallback */
1090 fallback_copy_texsubimage(ctx
,
1091 strb
, stImage
, texBaseFormat
,
1092 destX
, destY
, destZ
,
1093 srcX
, srcY
, width
, height
);
1098 * Copy image data from stImage into the texture object 'stObj' at level
1102 copy_image_data_to_texture(struct st_context
*st
,
1103 struct st_texture_object
*stObj
,
1105 struct st_texture_image
*stImage
)
1109 const struct gl_texture_image
*dstImage
=
1110 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
1112 assert(dstImage
->Width
== stImage
->base
.Width
);
1113 assert(dstImage
->Height
== stImage
->base
.Height
);
1114 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1118 /* Copy potentially with the blitter:
1121 if (stImage
->pt
!= stObj
->pt
)
1124 src_level
= stImage
->base
.Level
;
1126 st_texture_image_copy(st
->pipe
,
1127 stObj
->pt
, dstLevel
, /* dest texture, level */
1128 stImage
->pt
, src_level
, /* src texture, level */
1129 stImage
->base
.Face
);
1131 pipe_resource_reference(&stImage
->pt
, NULL
);
1133 else if (stImage
->TexData
) {
1134 /* Copy from malloc'd memory */
1135 /* XXX this should be re-examined/tested with a compressed format */
1136 GLuint blockSize
= util_format_get_blocksize(stObj
->pt
->format
);
1137 GLuint srcRowStride
= stImage
->base
.Width
* blockSize
;
1138 GLuint srcSliceStride
= stImage
->base
.Height
* srcRowStride
;
1139 st_texture_image_data(st
,
1146 _mesa_align_free(stImage
->TexData
);
1147 stImage
->TexData
= NULL
;
1150 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1155 * Called during state validation. When this function is finished,
1156 * the texture object should be ready for rendering.
1157 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1160 st_finalize_texture(struct gl_context
*ctx
,
1161 struct pipe_context
*pipe
,
1162 struct gl_texture_object
*tObj
)
1164 struct st_context
*st
= st_context(ctx
);
1165 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1166 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1168 struct st_texture_image
*firstImage
;
1169 enum pipe_format firstImageFormat
;
1170 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1172 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
1173 /* The texture is complete and we know exactly how many mipmap levels
1174 * are present/needed. This is conditional because we may be called
1175 * from the st_generate_mipmap() function when the texture object is
1176 * incomplete. In that case, we'll have set stObj->lastLevel before
1179 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1180 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1181 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1183 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1186 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1189 /* If both firstImage and stObj point to a texture which can contain
1190 * all active images, favour firstImage. Note that because of the
1191 * completeness requirement, we know that the image dimensions
1194 if (firstImage
->pt
&&
1195 firstImage
->pt
!= stObj
->pt
&&
1196 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1197 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1198 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
1201 /* Find gallium format for the Mesa texture */
1202 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1204 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1206 GLuint width
, height
, depth
;
1207 if (!guess_base_level_size(stObj
->base
.Target
,
1208 firstImage
->base
.Width2
,
1209 firstImage
->base
.Height2
,
1210 firstImage
->base
.Depth2
,
1211 firstImage
->base
.Level
,
1212 &width
, &height
, &depth
)) {
1213 width
= stObj
->width0
;
1214 height
= stObj
->height0
;
1215 depth
= stObj
->depth0
;
1217 /* convert GL dims to Gallium dims */
1218 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1219 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1222 /* If we already have a gallium texture, check that it matches the texture
1223 * object's format, target, size, num_levels, etc.
1226 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1227 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1228 stObj
->pt
->last_level
< stObj
->lastLevel
||
1229 stObj
->pt
->width0
!= ptWidth
||
1230 stObj
->pt
->height0
!= ptHeight
||
1231 stObj
->pt
->depth0
!= ptDepth
||
1232 stObj
->pt
->array_size
!= ptLayers
)
1234 /* The gallium texture does not match the Mesa texture so delete the
1235 * gallium texture now. We'll make a new one below.
1237 pipe_resource_reference(&stObj
->pt
, NULL
);
1238 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
1239 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1243 /* May need to create a new gallium texture:
1246 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1248 stObj
->pt
= st_texture_create(st
,
1249 gl_target_to_pipe(stObj
->base
.Target
),
1259 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1264 /* Pull in any images not in the object's texture:
1266 for (face
= 0; face
< nr_faces
; face
++) {
1268 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1269 struct st_texture_image
*stImage
=
1270 st_texture_image(stObj
->base
.Image
[face
][level
]);
1272 /* Need to import images in main memory or held in other textures.
1274 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1276 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1277 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1278 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
1279 /* src image fits expected dest mipmap level size */
1280 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1291 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1292 * for a whole mipmap stack.
1295 st_AllocTextureStorage(struct gl_context
*ctx
,
1296 struct gl_texture_object
*texObj
,
1297 GLsizei levels
, GLsizei width
,
1298 GLsizei height
, GLsizei depth
)
1300 const GLuint numFaces
= _mesa_num_tex_faces(texObj
->Target
);
1301 struct st_context
*st
= st_context(ctx
);
1302 struct st_texture_object
*stObj
= st_texture_object(texObj
);
1303 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
1304 enum pipe_format fmt
;
1309 /* Save the level=0 dimensions */
1310 stObj
->width0
= width
;
1311 stObj
->height0
= height
;
1312 stObj
->depth0
= depth
;
1313 stObj
->lastLevel
= levels
- 1;
1315 fmt
= st_mesa_format_to_pipe_format(texObj
->Image
[0][0]->TexFormat
);
1317 bindings
= default_bindings(st
, fmt
);
1319 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
1320 width
, height
, depth
,
1321 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1323 stObj
->pt
= st_texture_create(st
,
1324 gl_target_to_pipe(texObj
->Target
),
1335 /* Set image resource pointers */
1336 for (level
= 0; level
< levels
; level
++) {
1338 for (face
= 0; face
< numFaces
; face
++) {
1339 struct st_texture_image
*stImage
=
1340 st_texture_image(texObj
->Image
[face
][level
]);
1341 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1350 st_TestProxyTexImage(struct gl_context
*ctx
, GLenum target
,
1351 GLint level
, gl_format format
,
1352 GLint width
, GLint height
,
1353 GLint depth
, GLint border
)
1355 struct st_context
*st
= st_context(ctx
);
1356 struct pipe_context
*pipe
= st
->pipe
;
1358 if (width
== 0 || height
== 0 || depth
== 0) {
1359 /* zero-sized images are legal, and always fit! */
1363 if (pipe
->screen
->can_create_resource
) {
1364 /* Ask the gallium driver if the texture is too large */
1365 struct gl_texture_object
*texObj
=
1366 _mesa_get_current_tex_object(ctx
, target
);
1367 struct pipe_resource pt
;
1369 /* Setup the pipe_resource object
1371 memset(&pt
, 0, sizeof(pt
));
1373 pt
.target
= gl_target_to_pipe(target
);
1374 pt
.format
= st_mesa_format_to_pipe_format(format
);
1376 st_gl_texture_dims_to_pipe_dims(target
,
1377 width
, height
, depth
,
1378 &pt
.width0
, &pt
.height0
,
1379 &pt
.depth0
, &pt
.array_size
);
1381 if (level
== 0 && (texObj
->Sampler
.MinFilter
== GL_LINEAR
||
1382 texObj
->Sampler
.MinFilter
== GL_NEAREST
)) {
1383 /* assume just one mipmap level */
1387 /* assume a full set of mipmaps */
1388 pt
.last_level
= _mesa_logbase2(MAX3(width
, height
, depth
));
1391 return pipe
->screen
->can_create_resource(pipe
->screen
, &pt
);
1394 /* Use core Mesa fallback */
1395 return _mesa_test_proxy_teximage(ctx
, target
, level
, format
,
1396 width
, height
, depth
, border
);
1402 st_init_texture_functions(struct dd_function_table
*functions
)
1404 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1405 functions
->TexImage
= st_TexImage
;
1406 functions
->TexSubImage
= _mesa_store_texsubimage
;
1407 functions
->CompressedTexSubImage
= _mesa_store_compressed_texsubimage
;
1408 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
1409 functions
->GenerateMipmap
= st_generate_mipmap
;
1411 functions
->GetTexImage
= st_GetTexImage
;
1413 /* compressed texture functions */
1414 functions
->CompressedTexImage
= st_CompressedTexImage
;
1415 functions
->GetCompressedTexImage
= _mesa_get_compressed_teximage
;
1417 functions
->NewTextureObject
= st_NewTextureObject
;
1418 functions
->NewTextureImage
= st_NewTextureImage
;
1419 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
1420 functions
->DeleteTexture
= st_DeleteTextureObject
;
1421 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
1422 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
1423 functions
->MapTextureImage
= st_MapTextureImage
;
1424 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
1426 /* XXX Temporary until we can query pipe's texture sizes */
1427 functions
->TestProxyTexImage
= st_TestProxyTexImage
;
1429 functions
->AllocTextureStorage
= st_AllocTextureStorage
;