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 return PIPE_TEXTURE_1D
;
78 case GL_TEXTURE_EXTERNAL_OES
:
79 return PIPE_TEXTURE_2D
;
80 case GL_TEXTURE_RECTANGLE_NV
:
81 return PIPE_TEXTURE_RECT
;
83 return PIPE_TEXTURE_3D
;
84 case GL_TEXTURE_CUBE_MAP_ARB
:
85 return PIPE_TEXTURE_CUBE
;
86 case GL_TEXTURE_1D_ARRAY_EXT
:
87 return PIPE_TEXTURE_1D_ARRAY
;
88 case GL_TEXTURE_2D_ARRAY_EXT
:
89 return PIPE_TEXTURE_2D_ARRAY
;
90 case GL_TEXTURE_BUFFER
:
99 /** called via ctx->Driver.NewTextureImage() */
100 static struct gl_texture_image
*
101 st_NewTextureImage(struct gl_context
* ctx
)
103 DBG("%s\n", __FUNCTION__
);
105 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
109 /** called via ctx->Driver.DeleteTextureImage() */
111 st_DeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
113 /* nothing special (yet) for st_texture_image */
114 _mesa_delete_texture_image(ctx
, img
);
118 /** called via ctx->Driver.NewTextureObject() */
119 static struct gl_texture_object
*
120 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
122 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
124 DBG("%s\n", __FUNCTION__
);
125 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
130 /** called via ctx->Driver.DeleteTextureObject() */
132 st_DeleteTextureObject(struct gl_context
*ctx
,
133 struct gl_texture_object
*texObj
)
135 struct st_context
*st
= st_context(ctx
);
136 struct st_texture_object
*stObj
= st_texture_object(texObj
);
138 pipe_resource_reference(&stObj
->pt
, NULL
);
139 if (stObj
->sampler_view
) {
140 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
142 _mesa_delete_texture_object(ctx
, texObj
);
146 /** called via ctx->Driver.FreeTextureImageBuffer() */
148 st_FreeTextureImageBuffer(struct gl_context
*ctx
,
149 struct gl_texture_image
*texImage
)
151 struct st_texture_image
*stImage
= st_texture_image(texImage
);
153 DBG("%s\n", __FUNCTION__
);
156 pipe_resource_reference(&stImage
->pt
, NULL
);
159 if (stImage
->TexData
) {
160 _mesa_align_free(stImage
->TexData
);
161 stImage
->TexData
= NULL
;
166 /** called via ctx->Driver.MapTextureImage() */
168 st_MapTextureImage(struct gl_context
*ctx
,
169 struct gl_texture_image
*texImage
,
170 GLuint slice
, GLuint x
, GLuint y
, GLuint w
, GLuint h
,
172 GLubyte
**mapOut
, GLint
*rowStrideOut
)
174 struct st_context
*st
= st_context(ctx
);
175 struct st_texture_image
*stImage
= st_texture_image(texImage
);
180 if (mode
& GL_MAP_READ_BIT
)
181 pipeMode
|= PIPE_TRANSFER_READ
;
182 if (mode
& GL_MAP_WRITE_BIT
)
183 pipeMode
|= PIPE_TRANSFER_WRITE
;
184 if (mode
& GL_MAP_INVALIDATE_RANGE_BIT
)
185 pipeMode
|= PIPE_TRANSFER_DISCARD_RANGE
;
187 map
= st_texture_image_map(st
, stImage
, slice
, pipeMode
, x
, y
, w
, h
);
190 *rowStrideOut
= stImage
->transfer
->stride
;
199 /** called via ctx->Driver.UnmapTextureImage() */
201 st_UnmapTextureImage(struct gl_context
*ctx
,
202 struct gl_texture_image
*texImage
,
205 struct st_context
*st
= st_context(ctx
);
206 struct st_texture_image
*stImage
= st_texture_image(texImage
);
207 st_texture_image_unmap(st
, stImage
);
212 * Return default texture resource binding bitmask for the given format.
215 default_bindings(struct st_context
*st
, enum pipe_format format
)
217 struct pipe_screen
*screen
= st
->pipe
->screen
;
218 const unsigned target
= PIPE_TEXTURE_2D
;
221 if (util_format_is_depth_or_stencil(format
))
222 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
224 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
226 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
230 format
= util_format_linear(format
);
232 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
))
235 return PIPE_BIND_SAMPLER_VIEW
;
240 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
242 get_texture_dims(GLenum target
)
246 case GL_TEXTURE_1D_ARRAY_EXT
:
247 case GL_TEXTURE_BUFFER
:
250 case GL_TEXTURE_CUBE_MAP_ARB
:
251 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
252 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
253 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
254 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
255 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
256 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
257 case GL_TEXTURE_RECTANGLE_NV
:
258 case GL_TEXTURE_2D_ARRAY_EXT
:
259 case GL_TEXTURE_EXTERNAL_OES
:
264 assert(0 && "invalid texture target in get_texture_dims()");
271 * Given the size of a mipmap image, try to compute the size of the level=0
274 * Note that this isn't always accurate for odd-sized, non-POW textures.
275 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
277 * \return GL_TRUE for success, GL_FALSE for failure
280 guess_base_level_size(GLenum target
,
281 GLuint width
, GLuint height
, GLuint depth
, GLuint level
,
282 GLuint
*width0
, GLuint
*height0
, GLuint
*depth0
)
284 const GLuint dims
= get_texture_dims(target
);
291 /* Depending on the image's size, we can't always make a guess here */
292 if ((dims
>= 1 && width
== 1) ||
293 (dims
>= 2 && height
== 1) ||
294 (dims
>= 3 && depth
== 1)) {
295 /* we can't determine the image size at level=0 */
299 /* grow the image size until we hit level = 0 */
320 * Try to allocate a pipe_resource object for the given st_texture_object.
322 * We use the given st_texture_image as a clue to determine the size of the
323 * mipmap image at level=0.
325 * \return GL_TRUE for success, GL_FALSE if out of memory.
328 guess_and_alloc_texture(struct st_context
*st
,
329 struct st_texture_object
*stObj
,
330 const struct st_texture_image
*stImage
)
332 GLuint lastLevel
, width
, height
, depth
;
334 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
335 enum pipe_format fmt
;
337 DBG("%s\n", __FUNCTION__
);
341 if (!guess_base_level_size(stObj
->base
.Target
,
342 stImage
->base
.Width2
,
343 stImage
->base
.Height2
,
344 stImage
->base
.Depth2
,
346 &width
, &height
, &depth
)) {
347 /* we can't determine the image size at level=0 */
348 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
349 /* this is not an out of memory error */
353 /* At this point, (width x height x depth) is the expected size of
354 * the level=0 mipmap image.
357 /* Guess a reasonable value for lastLevel. With OpenGL we have no
358 * idea how many mipmap levels will be in a texture until we start
359 * to render with it. Make an educated guess here but be prepared
360 * to re-allocating a texture buffer with space for more (or fewer)
361 * mipmap levels later.
363 if ((stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
||
364 stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
365 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
366 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
367 !stObj
->base
.GenerateMipmap
&&
368 stImage
->base
.Level
== 0) {
369 /* only alloc space for a single mipmap level */
373 /* alloc space for a full mipmap */
374 GLuint l2width
= util_logbase2(width
);
375 GLuint l2height
= util_logbase2(height
);
376 GLuint l2depth
= util_logbase2(depth
);
377 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
380 /* Save the level=0 dimensions */
381 stObj
->width0
= width
;
382 stObj
->height0
= height
;
383 stObj
->depth0
= depth
;
385 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
387 bindings
= default_bindings(st
, fmt
);
389 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
390 width
, height
, depth
,
391 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
393 stObj
->pt
= st_texture_create(st
,
394 gl_target_to_pipe(stObj
->base
.Target
),
403 stObj
->lastLevel
= lastLevel
;
405 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
407 return stObj
->pt
!= NULL
;
412 * Called via ctx->Driver.AllocTextureImageBuffer().
413 * If the texture object/buffer already has space for the indicated image,
414 * we're done. Otherwise, allocate memory for the new texture image.
417 st_AllocTextureImageBuffer(struct gl_context
*ctx
,
418 struct gl_texture_image
*texImage
,
419 gl_format format
, GLsizei width
,
420 GLsizei height
, GLsizei depth
)
422 struct st_context
*st
= st_context(ctx
);
423 struct st_texture_image
*stImage
= st_texture_image(texImage
);
424 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
425 const GLuint level
= texImage
->Level
;
427 DBG("%s\n", __FUNCTION__
);
432 assert(!stImage
->TexData
);
433 assert(!stImage
->pt
); /* xxx this might be wrong */
435 /* Look if the parent texture object has space for this image */
437 level
<= stObj
->pt
->last_level
&&
438 st_texture_match_image(stObj
->pt
, texImage
)) {
439 /* this image will fit in the existing texture object's memory */
440 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
444 /* The parent texture object does not have space for this image */
446 pipe_resource_reference(&stObj
->pt
, NULL
);
447 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
449 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
450 /* Probably out of memory.
451 * Try flushing any pending rendering, then retry.
454 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
455 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
461 st_texture_match_image(stObj
->pt
, texImage
)) {
462 /* The image will live in the object's mipmap memory */
463 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
468 /* Create a new, temporary texture/resource/buffer to hold this
469 * one texture image. Note that when we later access this image
470 * (either for mapping or copying) we'll want to always specify
471 * mipmap level=0, even if the image represents some other mipmap
474 enum pipe_format format
=
475 st_mesa_format_to_pipe_format(texImage
->TexFormat
);
476 GLuint bindings
= default_bindings(st
, format
);
477 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
479 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
480 width
, height
, depth
,
481 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
483 stImage
->pt
= st_texture_create(st
,
484 gl_target_to_pipe(stObj
->base
.Target
),
492 return stImage
->pt
!= NULL
;
498 * Preparation prior to glTexImage. Basically check the 'surface_based'
499 * field and switch to a "normal" tex image if necessary.
502 prep_teximage(struct gl_context
*ctx
, struct gl_texture_image
*texImage
,
503 GLint internalFormat
,
504 GLint width
, GLint height
, GLint depth
, GLint border
,
505 GLenum format
, GLenum type
)
507 struct gl_texture_object
*texObj
= texImage
->TexObject
;
508 struct st_texture_object
*stObj
= st_texture_object(texObj
);
510 /* switch to "normal" */
511 if (stObj
->surface_based
) {
512 const GLenum target
= texObj
->Target
;
513 const GLuint level
= texImage
->Level
;
516 _mesa_clear_texture_object(ctx
, texObj
);
517 pipe_resource_reference(&stObj
->pt
, NULL
);
519 /* oops, need to init this image again */
520 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
521 internalFormat
, format
, type
);
523 _mesa_init_teximage_fields(ctx
, texImage
,
524 width
, height
, depth
, border
,
525 internalFormat
, texFormat
);
527 stObj
->surface_based
= GL_FALSE
;
533 st_TexImage(struct gl_context
* ctx
, GLuint dims
,
534 struct gl_texture_image
*texImage
,
535 GLint internalFormat
,
536 GLint width
, GLint height
, GLint depth
, GLint border
,
537 GLenum format
, GLenum type
, const void *pixels
,
538 const struct gl_pixelstore_attrib
*unpack
)
540 prep_teximage(ctx
, texImage
, internalFormat
, width
, height
, depth
, border
,
542 _mesa_store_teximage(ctx
, dims
, texImage
, internalFormat
, width
, height
, depth
,
543 border
, format
, type
, pixels
, unpack
);
548 st_CompressedTexImage(struct gl_context
*ctx
, GLuint dims
,
549 struct gl_texture_image
*texImage
,
550 GLint internalFormat
,
551 GLint width
, GLint height
, GLint border
, GLint depth
,
552 GLsizei imageSize
, const GLvoid
*data
)
554 prep_teximage(ctx
, texImage
, internalFormat
, width
, height
, depth
, border
,
556 _mesa_store_compressed_teximage(ctx
, dims
, texImage
, internalFormat
, width
,
557 height
, depth
, border
, imageSize
, data
);
563 * glGetTexImage() helper: decompress a compressed texture by rendering
564 * a textured quad. Store the results in the user's buffer.
567 decompress_with_blit(struct gl_context
* ctx
,
568 GLenum format
, GLenum type
, GLvoid
*pixels
,
569 struct gl_texture_image
*texImage
)
571 struct st_context
*st
= st_context(ctx
);
572 struct pipe_context
*pipe
= st
->pipe
;
573 struct st_texture_image
*stImage
= st_texture_image(texImage
);
574 struct st_texture_object
*stObj
= st_texture_object(texImage
->TexObject
);
575 struct pipe_sampler_view
*src_view
;
576 const GLuint width
= texImage
->Width
;
577 const GLuint height
= texImage
->Height
;
578 struct pipe_surface
*dst_surface
;
579 struct pipe_resource
*dst_texture
;
580 struct pipe_transfer
*tex_xfer
;
581 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
582 PIPE_BIND_TRANSFER_READ
);
584 /* create temp / dest surface */
585 if (!util_create_rgba_surface(pipe
, width
, height
, bind
,
586 &dst_texture
, &dst_surface
)) {
587 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
588 "in decompress_with_blit()");
592 /* Disable conditional rendering. */
593 if (st
->render_condition
) {
594 pipe
->render_condition(pipe
, NULL
, 0);
597 /* Create sampler view that limits fetches to the source mipmap level */
599 struct pipe_sampler_view sv_temp
;
601 u_sampler_view_default_template(&sv_temp
, stObj
->pt
, stObj
->pt
->format
);
603 sv_temp
.format
= util_format_linear(sv_temp
.format
);
604 sv_temp
.u
.tex
.first_level
=
605 sv_temp
.u
.tex
.last_level
= texImage
->Level
;
607 src_view
= pipe
->create_sampler_view(pipe
, stObj
->pt
, &sv_temp
);
609 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage");
614 /* blit/render/decompress */
615 util_blit_pixels_tex(st
->blit
,
616 src_view
, /* pipe_resource (src) */
617 0, 0, /* src x0, y0 */
618 width
, height
, /* src x1, y1 */
619 dst_surface
, /* pipe_surface (dst) */
620 0, 0, /* dst x0, y0 */
621 width
, height
, /* dst x1, y1 */
623 PIPE_TEX_MIPFILTER_NEAREST
);
625 /* Restore conditional rendering state. */
626 if (st
->render_condition
) {
627 pipe
->render_condition(pipe
, st
->render_condition
,
631 /* map the dst_surface so we can read from it */
632 tex_xfer
= pipe_get_transfer(pipe
,
635 0, 0, width
, height
);
637 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
639 /* copy/pack data into user buffer */
640 if (_mesa_format_matches_format_and_type(stImage
->base
.TexFormat
,
642 ctx
->Pack
.SwapBytes
)) {
644 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
645 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
647 for (row
= 0; row
< height
; row
++) {
648 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
649 height
, format
, type
, row
, 0);
650 memcpy(dest
, map
, bytesPerRow
);
651 map
+= tex_xfer
->stride
;
653 pipe_transfer_unmap(pipe
, tex_xfer
);
656 /* format translation via floats */
658 enum pipe_format pformat
= util_format_linear(dst_texture
->format
);
661 rgba
= (GLfloat
*) malloc(width
* 4 * sizeof(GLfloat
));
663 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGetTexImage()");
667 for (row
= 0; row
< height
; row
++) {
668 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
669 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
670 height
, format
, type
, row
, 0);
672 if (ST_DEBUG
& DEBUG_FALLBACK
)
673 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
675 /* get float[4] rgba row from surface */
676 pipe_get_tile_rgba_format(pipe
, tex_xfer
, 0, row
, width
, 1,
679 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
680 type
, dest
, &ctx
->Pack
, transferOps
);
687 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
689 pipe
->transfer_destroy(pipe
, tex_xfer
);
691 /* destroy the temp / dest surface */
692 util_destroy_rgba_surface(dst_texture
, dst_surface
);
694 pipe_sampler_view_release(pipe
, &src_view
);
700 * Called via ctx->Driver.GetTexImage()
703 st_GetTexImage(struct gl_context
* ctx
,
704 GLenum format
, GLenum type
, GLvoid
* pixels
,
705 struct gl_texture_image
*texImage
)
707 struct st_texture_image
*stImage
= st_texture_image(texImage
);
709 if (stImage
->pt
&& util_format_is_s3tc(stImage
->pt
->format
)) {
710 /* Need to decompress the texture.
711 * We'll do this by rendering a textured quad (which is hopefully
712 * faster than using the fallback code in texcompress.c).
713 * Note that we only expect RGBA formats (no Z/depth formats).
715 decompress_with_blit(ctx
, format
, type
, pixels
, texImage
);
718 _mesa_get_teximage(ctx
, format
, type
, pixels
, texImage
);
724 * Do a CopyTexSubImage operation using a read transfer from the source,
725 * a write transfer to the destination and get_tile()/put_tile() to access
728 * Note: srcY=0=TOP of renderbuffer
731 fallback_copy_texsubimage(struct gl_context
*ctx
,
732 struct st_renderbuffer
*strb
,
733 struct st_texture_image
*stImage
,
735 GLint destX
, GLint destY
, GLint destZ
,
736 GLint srcX
, GLint srcY
,
737 GLsizei width
, GLsizei height
)
739 struct st_context
*st
= st_context(ctx
);
740 struct pipe_context
*pipe
= st
->pipe
;
741 struct pipe_transfer
*src_trans
;
743 enum pipe_transfer_usage transfer_usage
;
745 if (ST_DEBUG
& DEBUG_FALLBACK
)
746 debug_printf("%s: fallback processing\n", __FUNCTION__
);
748 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
749 srcY
= strb
->Base
.Height
- srcY
- height
;
752 src_trans
= pipe_get_transfer(pipe
,
755 strb
->rtt_face
+ strb
->rtt_slice
,
760 if ((baseFormat
== GL_DEPTH_COMPONENT
||
761 baseFormat
== GL_DEPTH_STENCIL
) &&
762 util_format_is_depth_and_stencil(stImage
->pt
->format
))
763 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
765 transfer_usage
= PIPE_TRANSFER_WRITE
;
767 /* XXX this used to ignore destZ param */
768 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
769 destX
, destY
, width
, height
);
771 if (baseFormat
== GL_DEPTH_COMPONENT
||
772 baseFormat
== GL_DEPTH_STENCIL
) {
773 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
774 ctx
->Pixel
.DepthBias
!= 0.0F
);
778 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
779 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
788 data
= (uint
*) malloc(width
* sizeof(uint
));
791 /* To avoid a large temp memory allocation, do copy row by row */
792 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
793 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
795 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
797 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
801 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyTexSubImage()");
809 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
811 if (tempSrc
&& texDest
) {
812 const GLint dims
= 2;
813 const GLint dstRowStride
= stImage
->transfer
->stride
;
814 struct gl_texture_image
*texImage
= &stImage
->base
;
815 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
817 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
818 unpack
.Invert
= GL_TRUE
;
821 /* get float/RGBA image from framebuffer */
822 /* XXX this usually involves a lot of int/float conversion.
823 * try to avoid that someday.
825 pipe_get_tile_rgba_format(pipe
, src_trans
, 0, 0, width
, height
,
826 util_format_linear(strb
->texture
->format
),
829 /* Store into texture memory.
830 * Note that this does some special things such as pixel transfer
831 * ops and format conversion. In particular, if the dest tex format
832 * is actually RGBA but the user created the texture as GL_RGB we
833 * need to fill-in/override the alpha channel with 1.0.
835 _mesa_texstore(ctx
, dims
,
836 texImage
->_BaseFormat
,
839 (GLubyte
**) &texDest
,
841 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
845 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
852 st_texture_image_unmap(st
, stImage
);
853 pipe
->transfer_destroy(pipe
, src_trans
);
859 * If the format of the src renderbuffer and the format of the dest
860 * texture are compatible (in terms of blitting), return a TGSI writemask
861 * to be used during the blit.
862 * If the src/dest are incompatible, return 0.
865 compatible_src_dst_formats(struct gl_context
*ctx
,
866 const struct gl_renderbuffer
*src
,
867 const struct gl_texture_image
*dst
)
869 /* Get logical base formats for the src and dest.
870 * That is, use the user-requested formats and not the actual, device-
872 * For example, the user may have requested an A8 texture but the
873 * driver may actually be using an RGBA texture format. When we
874 * copy/blit to that texture, we only want to copy the Alpha channel
875 * and not the RGB channels.
877 * Similarly, when the src FBO was created an RGB format may have been
878 * requested but the driver actually chose an RGBA format. In that case,
879 * we don't want to copy the undefined Alpha channel to the dest texture
880 * (it should be 1.0).
882 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
883 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
886 * XXX when we have red-only and red/green renderbuffers we'll need
887 * to add more cases here (or implement a general-purpose routine that
888 * queries the existance of the R,G,B,A channels in the src and dest).
890 if (srcFormat
== dstFormat
) {
891 /* This is the same as matching_base_formats, which should
892 * always pass, as it did previously.
894 return TGSI_WRITEMASK_XYZW
;
896 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
897 /* Make sure that A in the dest is 1. The actual src format
898 * may be RGBA and have undefined A values.
900 return TGSI_WRITEMASK_XYZ
;
902 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
903 /* Make sure that A in the dest is 1. The actual dst format
904 * may be RGBA and will need A=1 to provide proper alpha values
905 * when sampled later.
907 return TGSI_WRITEMASK_XYZ
;
910 if (ST_DEBUG
& DEBUG_FALLBACK
)
911 debug_printf("%s failed for src %s, dst %s\n",
913 _mesa_lookup_enum_by_nr(srcFormat
),
914 _mesa_lookup_enum_by_nr(dstFormat
));
925 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
926 * Note that the region to copy has already been clipped so we know we
927 * won't read from outside the source renderbuffer's bounds.
929 * Note: srcY=0=Bottom of renderbuffer (GL convention)
932 st_CopyTexSubImage(struct gl_context
*ctx
, GLuint dims
,
933 struct gl_texture_image
*texImage
,
934 GLint destX
, GLint destY
, GLint destZ
,
935 struct gl_renderbuffer
*rb
,
936 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
)
938 struct st_texture_image
*stImage
= st_texture_image(texImage
);
939 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
940 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
941 struct st_renderbuffer
*strb
;
942 struct st_context
*st
= st_context(ctx
);
943 struct pipe_context
*pipe
= st
->pipe
;
944 struct pipe_screen
*screen
= pipe
->screen
;
945 enum pipe_format dest_format
, src_format
;
946 GLboolean matching_base_formats
;
947 GLuint color_writemask
, zs_writemask
, sample_count
;
948 struct pipe_surface
*dest_surface
= NULL
;
949 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
950 struct pipe_surface surf_tmpl
;
951 unsigned int dst_usage
;
954 /* make sure finalize_textures has been called?
956 if (0) st_validate_state(st
);
958 /* determine if copying depth or color data */
959 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
960 texBaseFormat
== GL_DEPTH_STENCIL
) {
961 strb
= st_renderbuffer(fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
964 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
965 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
968 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
969 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
973 sample_count
= strb
->surface
->texture
->nr_samples
;
974 /* I believe this would be legal, presumably would need to do a resolve
975 for color, and for depth/stencil spec says to just use one of the
976 depth/stencil samples per pixel? Need some transfer clarifications. */
977 assert(sample_count
< 2);
980 assert(strb
->surface
);
983 src_format
= strb
->surface
->format
;
984 dest_format
= stImage
->pt
->format
;
987 * Determine if the src framebuffer and dest texture have the same
988 * base format. We need this to detect a case such as the framebuffer
989 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
990 * texture format stores RGBA we need to set A=1 (overriding the
991 * framebuffer's alpha values). We can't do that with the blit or
992 * textured-quad paths.
994 matching_base_formats
=
995 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
996 _mesa_get_format_base_format(texImage
->TexFormat
));
998 if (ctx
->_ImageTransferState
) {
1002 if (matching_base_formats
&&
1003 src_format
== dest_format
&&
1005 /* use surface_copy() / blit */
1006 struct pipe_box src_box
;
1007 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
1008 width
, height
, &src_box
);
1010 /* for resource_copy_region(), y=0=top, always */
1011 pipe
->resource_copy_region(pipe
,
1014 stImage
->base
.Level
,
1015 destX
, destY
, destZ
+ stImage
->base
.Face
,
1018 strb
->surface
->u
.tex
.level
,
1023 if (texBaseFormat
== GL_DEPTH_STENCIL
) {
1027 if (texBaseFormat
== GL_DEPTH_COMPONENT
) {
1028 color_writemask
= 0;
1029 zs_writemask
= BLIT_WRITEMASK_Z
;
1030 dst_usage
= PIPE_BIND_DEPTH_STENCIL
;
1033 color_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1035 dst_usage
= PIPE_BIND_RENDER_TARGET
;
1038 if ((!color_writemask
&& !zs_writemask
) ||
1039 !screen
->is_format_supported(screen
, src_format
,
1040 PIPE_TEXTURE_2D
, sample_count
,
1041 PIPE_BIND_SAMPLER_VIEW
) ||
1042 !screen
->is_format_supported(screen
, dest_format
,
1049 srcY1
= strb
->Base
.Height
- srcY
- height
;
1050 srcY0
= srcY1
+ height
;
1054 srcY1
= srcY0
+ height
;
1057 /* Disable conditional rendering. */
1058 if (st
->render_condition
) {
1059 pipe
->render_condition(pipe
, NULL
, 0);
1062 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1063 surf_tmpl
.format
= util_format_linear(stImage
->pt
->format
);
1064 surf_tmpl
.usage
= dst_usage
;
1065 surf_tmpl
.u
.tex
.level
= stImage
->base
.Level
;
1066 surf_tmpl
.u
.tex
.first_layer
= stImage
->base
.Face
+ destZ
;
1067 surf_tmpl
.u
.tex
.last_layer
= stImage
->base
.Face
+ destZ
;
1069 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1071 util_blit_pixels(st
->blit
,
1073 strb
->surface
->u
.tex
.level
,
1075 srcX
+ width
, srcY1
,
1076 strb
->surface
->u
.tex
.first_layer
,
1079 destX
+ width
, destY
+ height
,
1080 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1081 color_writemask
, zs_writemask
);
1082 pipe_surface_reference(&dest_surface
, NULL
);
1084 /* Restore conditional rendering state. */
1085 if (st
->render_condition
) {
1086 pipe
->render_condition(pipe
, st
->render_condition
,
1087 st
->condition_mode
);
1093 /* software fallback */
1094 fallback_copy_texsubimage(ctx
,
1095 strb
, stImage
, texBaseFormat
,
1096 destX
, destY
, destZ
,
1097 srcX
, srcY
, width
, height
);
1102 * Copy image data from stImage into the texture object 'stObj' at level
1106 copy_image_data_to_texture(struct st_context
*st
,
1107 struct st_texture_object
*stObj
,
1109 struct st_texture_image
*stImage
)
1113 const struct gl_texture_image
*dstImage
=
1114 stObj
->base
.Image
[stImage
->base
.Face
][dstLevel
];
1116 assert(dstImage
->Width
== stImage
->base
.Width
);
1117 assert(dstImage
->Height
== stImage
->base
.Height
);
1118 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1122 /* Copy potentially with the blitter:
1125 if (stImage
->pt
!= stObj
->pt
)
1128 src_level
= stImage
->base
.Level
;
1130 st_texture_image_copy(st
->pipe
,
1131 stObj
->pt
, dstLevel
, /* dest texture, level */
1132 stImage
->pt
, src_level
, /* src texture, level */
1133 stImage
->base
.Face
);
1135 pipe_resource_reference(&stImage
->pt
, NULL
);
1137 else if (stImage
->TexData
) {
1138 /* Copy from malloc'd memory */
1139 /* XXX this should be re-examined/tested with a compressed format */
1140 GLuint blockSize
= util_format_get_blocksize(stObj
->pt
->format
);
1141 GLuint srcRowStride
= stImage
->base
.Width
* blockSize
;
1142 GLuint srcSliceStride
= stImage
->base
.Height
* srcRowStride
;
1143 st_texture_image_data(st
,
1150 _mesa_align_free(stImage
->TexData
);
1151 stImage
->TexData
= NULL
;
1154 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1159 * Called during state validation. When this function is finished,
1160 * the texture object should be ready for rendering.
1161 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1164 st_finalize_texture(struct gl_context
*ctx
,
1165 struct pipe_context
*pipe
,
1166 struct gl_texture_object
*tObj
)
1168 struct st_context
*st
= st_context(ctx
);
1169 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1170 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1172 struct st_texture_image
*firstImage
;
1173 enum pipe_format firstImageFormat
;
1174 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1176 if (_mesa_is_texture_complete(tObj
, &tObj
->Sampler
)) {
1177 /* The texture is complete and we know exactly how many mipmap levels
1178 * are present/needed. This is conditional because we may be called
1179 * from the st_generate_mipmap() function when the texture object is
1180 * incomplete. In that case, we'll have set stObj->lastLevel before
1183 if (stObj
->base
.Sampler
.MinFilter
== GL_LINEAR
||
1184 stObj
->base
.Sampler
.MinFilter
== GL_NEAREST
)
1185 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1187 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1190 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1193 /* If both firstImage and stObj point to a texture which can contain
1194 * all active images, favour firstImage. Note that because of the
1195 * completeness requirement, we know that the image dimensions
1198 if (firstImage
->pt
&&
1199 firstImage
->pt
!= stObj
->pt
&&
1200 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1201 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1202 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
1205 /* Find gallium format for the Mesa texture */
1206 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1208 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1210 GLuint width
, height
, depth
;
1211 if (!guess_base_level_size(stObj
->base
.Target
,
1212 firstImage
->base
.Width2
,
1213 firstImage
->base
.Height2
,
1214 firstImage
->base
.Depth2
,
1215 firstImage
->base
.Level
,
1216 &width
, &height
, &depth
)) {
1217 width
= stObj
->width0
;
1218 height
= stObj
->height0
;
1219 depth
= stObj
->depth0
;
1221 /* convert GL dims to Gallium dims */
1222 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, width
, height
, depth
,
1223 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1226 /* If we already have a gallium texture, check that it matches the texture
1227 * object's format, target, size, num_levels, etc.
1230 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1231 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1232 stObj
->pt
->last_level
< stObj
->lastLevel
||
1233 stObj
->pt
->width0
!= ptWidth
||
1234 stObj
->pt
->height0
!= ptHeight
||
1235 stObj
->pt
->depth0
!= ptDepth
||
1236 stObj
->pt
->array_size
!= ptLayers
)
1238 /* The gallium texture does not match the Mesa texture so delete the
1239 * gallium texture now. We'll make a new one below.
1241 pipe_resource_reference(&stObj
->pt
, NULL
);
1242 pipe_sampler_view_release(st
->pipe
, &stObj
->sampler_view
);
1243 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1247 /* May need to create a new gallium texture:
1250 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1252 stObj
->pt
= st_texture_create(st
,
1253 gl_target_to_pipe(stObj
->base
.Target
),
1263 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1268 /* Pull in any images not in the object's texture:
1270 for (face
= 0; face
< nr_faces
; face
++) {
1272 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1273 struct st_texture_image
*stImage
=
1274 st_texture_image(stObj
->base
.Image
[face
][level
]);
1276 /* Need to import images in main memory or held in other textures.
1278 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1280 (stImage
->base
.Width
== u_minify(stObj
->width0
, level
) &&
1281 stImage
->base
.Height
== u_minify(stObj
->height0
, level
) &&
1282 stImage
->base
.Depth
== u_minify(stObj
->depth0
, level
))) {
1283 /* src image fits expected dest mipmap level size */
1284 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1295 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1296 * for a whole mipmap stack.
1299 st_AllocTextureStorage(struct gl_context
*ctx
,
1300 struct gl_texture_object
*texObj
,
1301 GLsizei levels
, GLsizei width
,
1302 GLsizei height
, GLsizei depth
)
1304 const GLuint numFaces
= (texObj
->Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1305 struct st_context
*st
= st_context(ctx
);
1306 struct st_texture_object
*stObj
= st_texture_object(texObj
);
1307 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
, bindings
;
1308 enum pipe_format fmt
;
1313 /* Save the level=0 dimensions */
1314 stObj
->width0
= width
;
1315 stObj
->height0
= height
;
1316 stObj
->depth0
= depth
;
1317 stObj
->lastLevel
= levels
- 1;
1319 fmt
= st_mesa_format_to_pipe_format(texObj
->Image
[0][0]->TexFormat
);
1321 bindings
= default_bindings(st
, fmt
);
1323 st_gl_texture_dims_to_pipe_dims(texObj
->Target
,
1324 width
, height
, depth
,
1325 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1327 stObj
->pt
= st_texture_create(st
,
1328 gl_target_to_pipe(texObj
->Target
),
1339 /* Set image resource pointers */
1340 for (level
= 0; level
< levels
; level
++) {
1342 for (face
= 0; face
< numFaces
; face
++) {
1343 struct st_texture_image
*stImage
=
1344 st_texture_image(texObj
->Image
[face
][level
]);
1345 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1355 st_init_texture_functions(struct dd_function_table
*functions
)
1357 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
1358 functions
->TexImage
= st_TexImage
;
1359 functions
->TexSubImage
= _mesa_store_texsubimage
;
1360 functions
->CompressedTexSubImage
= _mesa_store_compressed_texsubimage
;
1361 functions
->CopyTexSubImage
= st_CopyTexSubImage
;
1362 functions
->GenerateMipmap
= st_generate_mipmap
;
1364 functions
->GetTexImage
= st_GetTexImage
;
1366 /* compressed texture functions */
1367 functions
->CompressedTexImage
= st_CompressedTexImage
;
1368 functions
->GetCompressedTexImage
= _mesa_get_compressed_teximage
;
1370 functions
->NewTextureObject
= st_NewTextureObject
;
1371 functions
->NewTextureImage
= st_NewTextureImage
;
1372 functions
->DeleteTextureImage
= st_DeleteTextureImage
;
1373 functions
->DeleteTexture
= st_DeleteTextureObject
;
1374 functions
->AllocTextureImageBuffer
= st_AllocTextureImageBuffer
;
1375 functions
->FreeTextureImageBuffer
= st_FreeTextureImageBuffer
;
1376 functions
->MapTextureImage
= st_MapTextureImage
;
1377 functions
->UnmapTextureImage
= st_UnmapTextureImage
;
1379 /* XXX Temporary until we can query pipe's texture sizes */
1380 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;
1382 functions
->AllocTextureStorage
= st_AllocTextureStorage
;