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"
38 #include "main/pixeltransfer.h"
39 #include "main/texcompress.h"
40 #include "main/texfetch.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 return PIPE_TEXTURE_2D
;
79 case GL_TEXTURE_RECTANGLE_NV
:
80 return PIPE_TEXTURE_RECT
;
82 return PIPE_TEXTURE_3D
;
83 case GL_TEXTURE_CUBE_MAP_ARB
:
84 return PIPE_TEXTURE_CUBE
;
85 case GL_TEXTURE_1D_ARRAY_EXT
:
86 return PIPE_TEXTURE_1D_ARRAY
;
87 case GL_TEXTURE_2D_ARRAY_EXT
:
88 return PIPE_TEXTURE_2D_ARRAY
;
96 /** called via ctx->Driver.NewTextureImage() */
97 static struct gl_texture_image
*
98 st_NewTextureImage(struct gl_context
* ctx
)
100 DBG("%s\n", __FUNCTION__
);
102 return (struct gl_texture_image
*) ST_CALLOC_STRUCT(st_texture_image
);
106 /** called via ctx->Driver.NewTextureObject() */
107 static struct gl_texture_object
*
108 st_NewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
110 struct st_texture_object
*obj
= ST_CALLOC_STRUCT(st_texture_object
);
112 DBG("%s\n", __FUNCTION__
);
113 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
118 /** called via ctx->Driver.DeleteTextureObject() */
120 st_DeleteTextureObject(struct gl_context
*ctx
,
121 struct gl_texture_object
*texObj
)
123 struct st_context
*st
= st_context(ctx
);
124 struct st_texture_object
*stObj
= st_texture_object(texObj
);
126 pipe_resource_reference(&stObj
->pt
, NULL
);
127 if (stObj
->sampler_view
) {
128 if (stObj
->sampler_view
->context
!= st
->pipe
) {
129 /* Take "ownership" of this texture sampler view by setting
130 * its context pointer to this context. This avoids potential
131 * crashes when the texture object is shared among contexts
132 * and the original/owner context has already been destroyed.
134 stObj
->sampler_view
->context
= st
->pipe
;
136 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
138 _mesa_delete_texture_object(ctx
, texObj
);
142 /** called via ctx->Driver.FreeTexImageData() */
144 st_FreeTextureImageData(struct gl_context
* ctx
, struct gl_texture_image
*texImage
)
146 struct st_texture_image
*stImage
= st_texture_image(texImage
);
148 DBG("%s\n", __FUNCTION__
);
151 pipe_resource_reference(&stImage
->pt
, NULL
);
154 if (texImage
->Data
) {
155 _mesa_align_free(texImage
->Data
);
156 texImage
->Data
= NULL
;
162 * From linux kernel i386 header files, copes with odd sizes better
163 * than COPY_DWORDS would:
164 * XXX Put this in src/mesa/main/imports.h ???
166 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
168 __memcpy(void *to
, const void *from
, size_t n
)
171 __asm__
__volatile__("rep ; movsl\n\t"
175 "1:\ttestb $1,%b4\n\t"
177 "movsb\n" "2:":"=&c"(d0
), "=&D"(d1
), "=&S"(d2
)
178 :"0"(n
/ 4), "q"(n
), "1"((long) to
), "2"((long) from
)
183 #define __memcpy(a,b,c) memcpy(a,b,c)
188 * The system memcpy (at least on ubuntu 5.10) has problems copying
189 * to agp (writecombined) memory from a source which isn't 64-byte
190 * aligned - there is a 4x performance falloff.
192 * The x86 __memcpy is immune to this but is slightly slower
193 * (10%-ish) than the system memcpy.
195 * The sse_memcpy seems to have a slight cliff at 64/32 bytes, but
196 * isn't much faster than x86_memcpy for agp copies.
198 * TODO: switch dynamically.
201 do_memcpy(void *dest
, const void *src
, size_t n
)
203 if ((((unsigned long) src
) & 63) || (((unsigned long) dest
) & 63)) {
204 return __memcpy(dest
, src
, n
);
207 return memcpy(dest
, src
, n
);
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
;
219 const unsigned geom
= 0x0;
222 if (util_format_is_depth_or_stencil(format
))
223 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_DEPTH_STENCIL
;
225 bindings
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
227 if (screen
->is_format_supported(screen
, format
, target
, 0, bindings
, geom
))
230 return PIPE_BIND_SAMPLER_VIEW
;
234 /** Return number of image dimensions (1, 2 or 3) for a texture target. */
236 get_texture_dims(GLenum target
)
240 case GL_TEXTURE_1D_ARRAY_EXT
:
243 case GL_TEXTURE_CUBE_MAP_ARB
:
244 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
245 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
246 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
247 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
248 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
249 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
250 case GL_TEXTURE_RECTANGLE_NV
:
251 case GL_TEXTURE_2D_ARRAY_EXT
:
256 assert(0 && "invalid texture target in get_texture_dims()");
263 * Try to allocate a pipe_resource object for the given st_texture_object.
265 * We use the given st_texture_image as a clue to determine the size of the
266 * mipmap image at level=0.
268 * \return GL_TRUE for success, GL_FALSE if out of memory.
271 guess_and_alloc_texture(struct st_context
*st
,
272 struct st_texture_object
*stObj
,
273 const struct st_texture_image
*stImage
)
275 const GLuint dims
= get_texture_dims(stObj
->base
.Target
);
276 GLuint level
, lastLevel
, width
, height
, depth
;
278 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
279 enum pipe_format fmt
;
281 DBG("%s\n", __FUNCTION__
);
285 level
= stImage
->level
;
286 width
= stImage
->base
.Width2
; /* size w/out border */
287 height
= stImage
->base
.Height2
;
288 depth
= stImage
->base
.Depth2
;
294 /* Depending on the image's size, we can't always make a guess here.
297 if ( (dims
>= 1 && width
== 1) ||
298 (dims
>= 2 && height
== 1) ||
299 (dims
>= 3 && depth
== 1) ) {
300 /* we can't determine the image size at level=0 */
301 stObj
->width0
= stObj
->height0
= stObj
->depth0
= 0;
302 /* this is not an out of memory error */
307 /* grow the image size until we hit level = 0 */
320 /* At this point, (width x height x depth) is the expected size of
321 * the level=0 mipmap image.
324 /* Guess a reasonable value for lastLevel. With OpenGL we have no
325 * idea how many mipmap levels will be in a texture until we start
326 * to render with it. Make an educated guess here but be prepared
327 * to re-allocating a texture buffer with space for more (or fewer)
328 * mipmap levels later.
330 if ((stObj
->base
.MinFilter
== GL_NEAREST
||
331 stObj
->base
.MinFilter
== GL_LINEAR
||
332 stImage
->base
._BaseFormat
== GL_DEPTH_COMPONENT
||
333 stImage
->base
._BaseFormat
== GL_DEPTH_STENCIL_EXT
) &&
334 !stObj
->base
.GenerateMipmap
&&
335 stImage
->level
== 0) {
336 /* only alloc space for a single mipmap level */
340 /* alloc space for a full mipmap */
341 GLuint l2width
= util_logbase2(width
);
342 GLuint l2height
= util_logbase2(height
);
343 GLuint l2depth
= util_logbase2(depth
);
344 lastLevel
= MAX2(MAX2(l2width
, l2height
), l2depth
);
347 /* Save the level=0 dimensions */
348 stObj
->width0
= width
;
349 stObj
->height0
= height
;
350 stObj
->depth0
= depth
;
352 fmt
= st_mesa_format_to_pipe_format(stImage
->base
.TexFormat
);
354 bindings
= default_bindings(st
, fmt
);
356 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
,
357 width
, height
, depth
,
358 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
360 stObj
->pt
= st_texture_create(st
,
361 gl_target_to_pipe(stObj
->base
.Target
),
370 DBG("%s returning %d\n", __FUNCTION__
, (stObj
->pt
!= NULL
));
372 return stObj
->pt
!= NULL
;
377 * Adjust pixel unpack params and image dimensions to strip off the
379 * Gallium doesn't support texture borders. They've seldem been used
380 * and seldom been implemented correctly anyway.
381 * \param unpackNew returns the new pixel unpack parameters
384 strip_texture_border(GLint border
,
385 GLint
*width
, GLint
*height
, GLint
*depth
,
386 const struct gl_pixelstore_attrib
*unpack
,
387 struct gl_pixelstore_attrib
*unpackNew
)
389 assert(border
> 0); /* sanity check */
391 *unpackNew
= *unpack
;
393 if (unpackNew
->RowLength
== 0)
394 unpackNew
->RowLength
= *width
;
396 if (depth
&& unpackNew
->ImageHeight
== 0)
397 unpackNew
->ImageHeight
= *height
;
399 unpackNew
->SkipPixels
+= border
;
401 unpackNew
->SkipRows
+= border
;
403 unpackNew
->SkipImages
+= border
;
406 *width
= *width
- 2 * border
;
407 if (height
&& *height
>= 3)
408 *height
= *height
- 2 * border
;
409 if (depth
&& *depth
>= 3)
410 *depth
= *depth
- 2 * border
;
415 * Try to do texture compression via rendering. If the Gallium driver
416 * can render into a compressed surface this will allow us to do texture
418 * \return GL_TRUE for success, GL_FALSE for failure
421 compress_with_blit(struct gl_context
* ctx
,
422 GLenum target
, GLint level
,
423 GLint xoffset
, GLint yoffset
, GLint zoffset
,
424 GLint width
, GLint height
, GLint depth
,
425 GLenum format
, GLenum type
, const void *pixels
,
426 const struct gl_pixelstore_attrib
*unpack
,
427 struct gl_texture_image
*texImage
)
429 const GLuint dstImageOffsets
[1] = {0};
430 struct st_texture_image
*stImage
= st_texture_image(texImage
);
431 struct st_context
*st
= st_context(ctx
);
432 struct pipe_context
*pipe
= st
->pipe
;
433 struct pipe_screen
*screen
= pipe
->screen
;
434 gl_format mesa_format
;
435 struct pipe_resource templ
;
436 struct pipe_resource
*src_tex
;
437 struct pipe_sampler_view view_templ
;
438 struct pipe_sampler_view
*src_view
;
439 struct pipe_surface
*dst_surface
, surf_tmpl
;
440 struct pipe_transfer
*tex_xfer
;
444 /* XXX: Can this happen? Should we assert? */
448 /* get destination surface (in the compressed texture) */
449 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
450 surf_tmpl
.format
= stImage
->pt
->format
;
451 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
452 surf_tmpl
.u
.tex
.level
= stImage
->level
;
453 surf_tmpl
.u
.tex
.first_layer
= stImage
->face
;
454 surf_tmpl
.u
.tex
.last_layer
= stImage
->face
;
455 dst_surface
= pipe
->create_surface(pipe
, stImage
->pt
, &surf_tmpl
);
457 /* can't render into this format (or other problem) */
461 /* Choose format for the temporary RGBA texture image.
463 mesa_format
= st_ChooseTextureFormat(ctx
, GL_RGBA
, format
, type
);
468 /* Create the temporary source texture
470 memset(&templ
, 0, sizeof(templ
));
471 templ
.target
= st
->internal_target
;
472 templ
.format
= st_mesa_format_to_pipe_format(mesa_format
);
473 templ
.width0
= width
;
474 templ
.height0
= height
;
476 templ
.array_size
= 1;
477 templ
.last_level
= 0;
478 templ
.usage
= PIPE_USAGE_DEFAULT
;
479 templ
.bind
= PIPE_BIND_SAMPLER_VIEW
;
480 src_tex
= screen
->resource_create(screen
, &templ
);
485 /* Put user's tex data into the temporary texture
487 tex_xfer
= pipe_get_transfer(st_context(ctx
)->pipe
, src_tex
,
488 0, 0, /* layer, level are zero */
490 0, 0, width
, height
); /* x, y, w, h */
491 map
= pipe_transfer_map(pipe
, tex_xfer
);
493 _mesa_texstore(ctx
, 2, GL_RGBA
, mesa_format
,
495 0, 0, 0, /* dest x/y/z offset */
496 tex_xfer
->stride
, /* dest row stride (bytes) */
497 dstImageOffsets
, /* image offsets (for 3D only) */
498 width
, height
, 1, /* size */
499 format
, type
, /* source format/type */
500 pixels
, /* source data */
501 unpack
); /* source data packing */
503 pipe_transfer_unmap(pipe
, tex_xfer
);
504 pipe
->transfer_destroy(pipe
, tex_xfer
);
506 /* Create temporary sampler view */
507 u_sampler_view_default_template(&view_templ
,
510 src_view
= pipe
->create_sampler_view(pipe
, src_tex
, &view_templ
);
513 /* copy / compress image */
514 util_blit_pixels_tex(st
->blit
,
515 src_view
, /* sampler view (src) */
516 0, 0, /* src x0, y0 */
517 width
, height
, /* src x1, y1 */
518 dst_surface
, /* pipe_surface (dst) */
519 xoffset
, yoffset
, /* dst x0, y0 */
520 xoffset
+ width
, /* dst x1 */
521 yoffset
+ height
, /* dst y1 */
523 PIPE_TEX_MIPFILTER_NEAREST
);
525 pipe_surface_reference(&dst_surface
, NULL
);
526 pipe_resource_reference(&src_tex
, NULL
);
527 pipe_sampler_view_reference(&src_view
, NULL
);
534 * Do glTexImage1/2/3D().
537 st_TexImage(struct gl_context
* ctx
,
539 GLenum target
, GLint level
,
540 GLint internalFormat
,
541 GLint width
, GLint height
, GLint depth
,
543 GLenum format
, GLenum type
, const void *pixels
,
544 const struct gl_pixelstore_attrib
*unpack
,
545 struct gl_texture_object
*texObj
,
546 struct gl_texture_image
*texImage
,
547 GLsizei imageSize
, GLboolean compressed_src
)
549 struct st_context
*st
= st_context(ctx
);
550 struct pipe_screen
*screen
= st
->pipe
->screen
;
551 struct st_texture_object
*stObj
= st_texture_object(texObj
);
552 struct st_texture_image
*stImage
= st_texture_image(texImage
);
553 GLuint dstRowStride
= 0;
554 struct gl_pixelstore_attrib unpackNB
;
555 enum pipe_transfer_usage transfer_usage
= 0;
557 DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__
,
558 _mesa_lookup_enum_by_nr(target
), level
, width
, height
, depth
, border
);
560 /* switch to "normal" */
561 if (stObj
->surface_based
) {
564 _mesa_clear_texture_object(ctx
, texObj
);
565 pipe_resource_reference(&stObj
->pt
, NULL
);
567 /* oops, need to init this image again */
568 texFormat
= _mesa_choose_texture_format(ctx
, texObj
, target
, level
,
569 internalFormat
, format
, type
);
571 _mesa_init_teximage_fields(ctx
, target
, texImage
,
572 width
, height
, depth
, border
,
573 internalFormat
, texFormat
);
575 stObj
->surface_based
= GL_FALSE
;
578 /* gallium does not support texture borders, strip it off */
580 strip_texture_border(border
, &width
, &height
, &depth
, unpack
, &unpackNB
);
582 texImage
->Width
= width
;
583 texImage
->Height
= height
;
584 texImage
->Depth
= depth
;
585 texImage
->Border
= 0;
589 assert(texImage
->Width
== width
);
590 assert(texImage
->Height
== height
);
591 assert(texImage
->Depth
== depth
);
594 stImage
->face
= _mesa_tex_target_to_face(target
);
595 stImage
->level
= level
;
597 _mesa_set_fetch_functions(texImage
, dims
);
599 /* Release the reference to a potentially orphaned buffer.
600 * Release any old malloced memory.
603 pipe_resource_reference(&stImage
->pt
, NULL
);
604 assert(!texImage
->Data
);
606 else if (texImage
->Data
) {
607 _mesa_align_free(texImage
->Data
);
611 * See if the new image is somehow incompatible with the existing
612 * mipmap. If so, free the old mipmap.
615 if (level
> (GLint
) stObj
->pt
->last_level
||
616 !st_texture_match_image(stObj
->pt
, &stImage
->base
,
617 stImage
->face
, stImage
->level
)) {
619 pipe_resource_reference(&stObj
->pt
, NULL
);
621 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
625 if (width
== 0 || height
== 0 || depth
== 0) {
626 /* stop after freeing old image */
631 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
632 /* Probably out of memory.
633 * Try flushing any pending rendering, then retry.
636 if (!guess_and_alloc_texture(st
, stObj
, stImage
)) {
637 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
643 assert(!stImage
->pt
);
645 /* Check if this texture image can live inside the texture object's buffer.
646 * If so, store the image there. Otherwise the image will temporarily live
650 st_texture_match_image(stObj
->pt
, &stImage
->base
,
651 stImage
->face
, stImage
->level
)) {
653 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
658 DBG("XXX: Image did not fit into texture - storing in local memory!\n");
660 /* Pixel data may come from regular user memory or a PBO. For the later,
661 * do bounds checking and map the PBO to read pixels data from it.
663 * XXX we should try to use a GPU-accelerated path to copy the image data
664 * from the PBO to the texture.
666 if (compressed_src
) {
667 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
, imageSize
, pixels
,
669 "glCompressedTexImage");
672 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, 1,
674 pixels
, unpack
, "glTexImage");
677 /* See if we can do texture compression with a blit/render.
679 if (!compressed_src
&&
681 _mesa_is_format_compressed(texImage
->TexFormat
) &&
682 screen
->is_format_supported(screen
,
684 stImage
->pt
->target
, 0,
685 PIPE_BIND_RENDER_TARGET
, 0)) {
689 if (compress_with_blit(ctx
, target
, level
, 0, 0, 0, width
, height
, depth
,
690 format
, type
, pixels
, unpack
, texImage
)) {
696 * Prepare to store the texture data. Either map the gallium texture buffer
697 * memory or malloc space for it.
700 /* Store the image in the gallium texture memory buffer */
701 if (format
== GL_DEPTH_COMPONENT
&&
702 util_format_is_depth_and_stencil(stImage
->pt
->format
))
703 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
705 transfer_usage
= PIPE_TRANSFER_WRITE
;
707 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
708 transfer_usage
, 0, 0, width
, height
);
709 if(stImage
->transfer
)
710 dstRowStride
= stImage
->transfer
->stride
;
713 /* Allocate regular memory and store the image there temporarily. */
714 GLuint imageSize
= _mesa_format_image_size(texImage
->TexFormat
,
715 width
, height
, depth
);
716 dstRowStride
= _mesa_format_row_stride(texImage
->TexFormat
, width
);
718 texImage
->Data
= _mesa_align_malloc(imageSize
, 16);
721 if (!texImage
->Data
) {
722 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
727 /* We've allocated texture memory, but have no pixel data - all done. */
731 DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
732 width
, height
, depth
, width
, dstRowStride
);
734 /* Copy user texture image into the texture buffer.
736 if (compressed_src
) {
737 const GLuint srcRowStride
=
738 _mesa_format_row_stride(texImage
->TexFormat
, width
);
739 if (dstRowStride
== srcRowStride
) {
740 memcpy(texImage
->Data
, pixels
, imageSize
);
743 char *dst
= texImage
->Data
;
744 const char *src
= pixels
;
745 GLuint i
, bw
, bh
, lines
;
746 _mesa_get_format_block_size(texImage
->TexFormat
, &bw
, &bh
);
747 lines
= (height
+ bh
- 1) / bh
;
749 for (i
= 0; i
< lines
; ++i
) {
750 memcpy(dst
, src
, srcRowStride
);
757 const GLuint srcImageStride
=
758 _mesa_image_image_stride(unpack
, width
, height
, format
, type
);
760 const GLubyte
*src
= (const GLubyte
*) pixels
;
762 for (i
= 0; i
< depth
; i
++) {
763 if (!_mesa_texstore(ctx
, dims
,
764 texImage
->_BaseFormat
,
767 0, 0, 0, /* dstX/Y/Zoffset */
769 texImage
->ImageOffsets
,
771 format
, type
, src
, unpack
)) {
772 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
775 if (stImage
->pt
&& i
+ 1 < depth
) {
776 /* unmap this slice */
777 st_texture_image_unmap(st
, stImage
);
778 /* map next slice of 3D texture */
779 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
780 transfer_usage
, 0, 0,
782 src
+= srcImageStride
;
788 _mesa_unmap_teximage_pbo(ctx
, unpack
);
790 if (stImage
->pt
&& texImage
->Data
) {
791 st_texture_image_unmap(st
, stImage
);
792 texImage
->Data
= NULL
;
798 st_TexImage3D(struct gl_context
* ctx
,
799 GLenum target
, GLint level
,
800 GLint internalFormat
,
801 GLint width
, GLint height
, GLint depth
,
803 GLenum format
, GLenum type
, const void *pixels
,
804 const struct gl_pixelstore_attrib
*unpack
,
805 struct gl_texture_object
*texObj
,
806 struct gl_texture_image
*texImage
)
808 st_TexImage(ctx
, 3, target
, level
, internalFormat
, width
, height
, depth
,
809 border
, format
, type
, pixels
, unpack
, texObj
, texImage
,
815 st_TexImage2D(struct gl_context
* ctx
,
816 GLenum target
, GLint level
,
817 GLint internalFormat
,
818 GLint width
, GLint height
, GLint border
,
819 GLenum format
, GLenum type
, const void *pixels
,
820 const struct gl_pixelstore_attrib
*unpack
,
821 struct gl_texture_object
*texObj
,
822 struct gl_texture_image
*texImage
)
824 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
825 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
830 st_TexImage1D(struct gl_context
* ctx
,
831 GLenum target
, GLint level
,
832 GLint internalFormat
,
833 GLint width
, GLint border
,
834 GLenum format
, GLenum type
, const void *pixels
,
835 const struct gl_pixelstore_attrib
*unpack
,
836 struct gl_texture_object
*texObj
,
837 struct gl_texture_image
*texImage
)
839 st_TexImage(ctx
, 1, target
, level
, internalFormat
, width
, 1, 1, border
,
840 format
, type
, pixels
, unpack
, texObj
, texImage
, 0, GL_FALSE
);
845 st_CompressedTexImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
846 GLint internalFormat
,
847 GLint width
, GLint height
, GLint border
,
848 GLsizei imageSize
, const GLvoid
*data
,
849 struct gl_texture_object
*texObj
,
850 struct gl_texture_image
*texImage
)
852 st_TexImage(ctx
, 2, target
, level
, internalFormat
, width
, height
, 1, border
,
853 0, 0, data
, &ctx
->Unpack
, texObj
, texImage
, imageSize
, GL_TRUE
);
859 * glGetTexImage() helper: decompress a compressed texture by rendering
860 * a textured quad. Store the results in the user's buffer.
863 decompress_with_blit(struct gl_context
* ctx
, GLenum target
, GLint level
,
864 GLenum format
, GLenum type
, GLvoid
*pixels
,
865 struct gl_texture_object
*texObj
,
866 struct gl_texture_image
*texImage
)
868 struct st_context
*st
= st_context(ctx
);
869 struct pipe_context
*pipe
= st
->pipe
;
870 struct st_texture_image
*stImage
= st_texture_image(texImage
);
871 struct st_texture_object
*stObj
= st_texture_object(texObj
);
872 struct pipe_sampler_view
*src_view
=
873 st_get_texture_sampler_view(stObj
, pipe
);
874 const GLuint width
= texImage
->Width
;
875 const GLuint height
= texImage
->Height
;
876 struct pipe_surface
*dst_surface
;
877 struct pipe_resource
*dst_texture
;
878 struct pipe_transfer
*tex_xfer
;
879 unsigned bind
= (PIPE_BIND_RENDER_TARGET
| /* util_blit may choose to render */
880 PIPE_BIND_TRANSFER_READ
);
882 /* create temp / dest surface */
883 if (!util_create_rgba_surface(pipe
, width
, height
, bind
,
884 &dst_texture
, &dst_surface
)) {
885 _mesa_problem(ctx
, "util_create_rgba_surface() failed "
886 "in decompress_with_blit()");
890 /* blit/render/decompress */
891 util_blit_pixels_tex(st
->blit
,
892 src_view
, /* pipe_resource (src) */
893 0, 0, /* src x0, y0 */
894 width
, height
, /* src x1, y1 */
895 dst_surface
, /* pipe_surface (dst) */
896 0, 0, /* dst x0, y0 */
897 width
, height
, /* dst x1, y1 */
899 PIPE_TEX_MIPFILTER_NEAREST
);
901 /* map the dst_surface so we can read from it */
902 tex_xfer
= pipe_get_transfer(st_context(ctx
)->pipe
,
905 0, 0, width
, height
);
907 pixels
= _mesa_map_pbo_dest(ctx
, &ctx
->Pack
, pixels
);
909 /* copy/pack data into user buffer */
910 if (st_equal_formats(stImage
->pt
->format
, format
, type
)) {
912 const uint bytesPerRow
= width
* util_format_get_blocksize(stImage
->pt
->format
);
913 ubyte
*map
= pipe_transfer_map(pipe
, tex_xfer
);
915 for (row
= 0; row
< height
; row
++) {
916 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
917 height
, format
, type
, row
, 0);
918 memcpy(dest
, map
, bytesPerRow
);
919 map
+= tex_xfer
->stride
;
921 pipe_transfer_unmap(pipe
, tex_xfer
);
924 /* format translation via floats */
926 enum pipe_format format
= util_format_linear(dst_texture
->format
);
927 for (row
= 0; row
< height
; row
++) {
928 const GLbitfield transferOps
= 0x0; /* bypassed for glGetTexImage() */
929 GLfloat rgba
[4 * MAX_WIDTH
];
930 GLvoid
*dest
= _mesa_image_address2d(&ctx
->Pack
, pixels
, width
,
931 height
, format
, type
, row
, 0);
933 if (ST_DEBUG
& DEBUG_FALLBACK
)
934 debug_printf("%s: fallback format translation\n", __FUNCTION__
);
936 /* get float[4] rgba row from surface */
937 pipe_get_tile_rgba_format(pipe
, tex_xfer
, 0, row
, width
, 1,
940 _mesa_pack_rgba_span_float(ctx
, width
, (GLfloat (*)[4]) rgba
, format
,
941 type
, dest
, &ctx
->Pack
, transferOps
);
945 _mesa_unmap_pbo_dest(ctx
, &ctx
->Pack
);
947 pipe
->transfer_destroy(pipe
, tex_xfer
);
949 /* destroy the temp / dest surface */
950 util_destroy_rgba_surface(dst_texture
, dst_surface
);
956 * Need to map texture image into memory before copying image data,
960 st_get_tex_image(struct gl_context
* ctx
, GLenum target
, GLint level
,
961 GLenum format
, GLenum type
, GLvoid
* pixels
,
962 struct gl_texture_object
*texObj
,
963 struct gl_texture_image
*texImage
, GLboolean compressed_dst
)
965 struct st_context
*st
= st_context(ctx
);
966 struct st_texture_image
*stImage
= st_texture_image(texImage
);
967 const GLuint dstImageStride
=
968 _mesa_image_image_stride(&ctx
->Pack
, texImage
->Width
, texImage
->Height
,
974 util_format_is_s3tc(stImage
->pt
->format
) &&
976 /* Need to decompress the texture.
977 * We'll do this by rendering a textured quad.
978 * Note that we only expect RGBA formats (no Z/depth formats).
980 decompress_with_blit(ctx
, target
, level
, format
, type
, pixels
,
987 /* Image is stored in hardware format in a buffer managed by the
988 * kernel. Need to explicitly map and unmap it.
990 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
991 PIPE_TRANSFER_READ
, 0, 0,
993 stImage
->base
.Height
);
994 /* compute stride in texels from stride in bytes */
995 texImage
->RowStride
= stImage
->transfer
->stride
996 * util_format_get_blockwidth(stImage
->pt
->format
)
997 / util_format_get_blocksize(stImage
->pt
->format
);
1000 /* Otherwise, the image should actually be stored in
1001 * texImage->Data. This is pretty confusing for
1002 * everybody, I'd much prefer to separate the two functions of
1003 * texImage->Data - storage for texture images in main memory
1004 * and access (ie mappings) of images. In other words, we'd
1005 * create a new texImage->Map field and leave Data simply for
1008 assert(texImage
->Data
);
1011 depth
= texImage
->Depth
;
1012 texImage
->Depth
= 1;
1014 dest
= (GLubyte
*) pixels
;
1016 _mesa_set_fetch_functions(texImage
, get_texture_dims(target
));
1018 for (i
= 0; i
< depth
; i
++) {
1019 if (compressed_dst
) {
1020 _mesa_get_compressed_teximage(ctx
, target
, level
, dest
,
1024 _mesa_get_teximage(ctx
, target
, level
, format
, type
, dest
,
1028 if (stImage
->pt
&& i
+ 1 < depth
) {
1029 /* unmap this slice */
1030 st_texture_image_unmap(st
, stImage
);
1031 /* map next slice of 3D texture */
1032 texImage
->Data
= st_texture_image_map(st
, stImage
, i
+ 1,
1033 PIPE_TRANSFER_READ
, 0, 0,
1034 stImage
->base
.Width
,
1035 stImage
->base
.Height
);
1036 dest
+= dstImageStride
;
1040 texImage
->Depth
= depth
;
1044 st_texture_image_unmap(st
, stImage
);
1045 texImage
->Data
= NULL
;
1051 st_GetTexImage(struct gl_context
* ctx
, GLenum target
, GLint level
,
1052 GLenum format
, GLenum type
, GLvoid
* pixels
,
1053 struct gl_texture_object
*texObj
,
1054 struct gl_texture_image
*texImage
)
1056 st_get_tex_image(ctx
, target
, level
, format
, type
, pixels
, texObj
, texImage
,
1062 st_GetCompressedTexImage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1064 struct gl_texture_object
*texObj
,
1065 struct gl_texture_image
*texImage
)
1067 st_get_tex_image(ctx
, target
, level
, 0, 0, pixels
, texObj
, texImage
,
1074 st_TexSubimage(struct gl_context
*ctx
, GLint dims
, GLenum target
, GLint level
,
1075 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1076 GLint width
, GLint height
, GLint depth
,
1077 GLenum format
, GLenum type
, const void *pixels
,
1078 const struct gl_pixelstore_attrib
*packing
,
1079 struct gl_texture_object
*texObj
,
1080 struct gl_texture_image
*texImage
)
1082 struct st_context
*st
= st_context(ctx
);
1083 struct pipe_screen
*screen
= st
->pipe
->screen
;
1084 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1085 GLuint dstRowStride
;
1086 const GLuint srcImageStride
=
1087 _mesa_image_image_stride(packing
, width
, height
, format
, type
);
1090 /* init to silence warning only: */
1091 enum pipe_transfer_usage transfer_usage
= PIPE_TRANSFER_WRITE
;
1093 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__
,
1094 _mesa_lookup_enum_by_nr(target
),
1095 level
, xoffset
, yoffset
, width
, height
);
1098 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
, format
,
1099 type
, pixels
, packing
, "glTexSubImage2D");
1103 /* See if we can do texture compression with a blit/render.
1105 if (!ctx
->Mesa_DXTn
&&
1106 _mesa_is_format_compressed(texImage
->TexFormat
) &&
1107 screen
->is_format_supported(screen
,
1108 stImage
->pt
->format
,
1109 stImage
->pt
->target
, 0,
1110 PIPE_BIND_RENDER_TARGET
, 0)) {
1111 if (compress_with_blit(ctx
, target
, level
,
1112 xoffset
, yoffset
, zoffset
,
1113 width
, height
, depth
,
1114 format
, type
, pixels
, packing
, texImage
)) {
1119 /* Map buffer if necessary. Need to lock to prevent other contexts
1120 * from uploading the buffer under us.
1123 if (format
== GL_DEPTH_COMPONENT
&&
1124 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1125 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1127 transfer_usage
= PIPE_TRANSFER_WRITE
;
1129 texImage
->Data
= st_texture_image_map(st
, stImage
, zoffset
,
1135 if (!texImage
->Data
) {
1136 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1140 src
= (const GLubyte
*) pixels
;
1141 dstRowStride
= stImage
->transfer
->stride
;
1143 for (i
= 0; i
< depth
; i
++) {
1144 if (!_mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1145 texImage
->TexFormat
,
1149 texImage
->ImageOffsets
,
1151 format
, type
, src
, packing
)) {
1152 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1155 if (stImage
->pt
&& i
+ 1 < depth
) {
1156 /* unmap this slice */
1157 st_texture_image_unmap(st
, stImage
);
1158 /* map next slice of 3D texture */
1159 texImage
->Data
= st_texture_image_map(st
, stImage
,
1164 src
+= srcImageStride
;
1169 _mesa_unmap_teximage_pbo(ctx
, packing
);
1171 if (stImage
->pt
&& texImage
->Data
) {
1172 st_texture_image_unmap(st
, stImage
);
1173 texImage
->Data
= NULL
;
1180 st_TexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1181 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1182 GLsizei width
, GLsizei height
, GLsizei depth
,
1183 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1184 const struct gl_pixelstore_attrib
*packing
,
1185 struct gl_texture_object
*texObj
,
1186 struct gl_texture_image
*texImage
)
1188 st_TexSubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
1189 width
, height
, depth
, format
, type
,
1190 pixels
, packing
, texObj
, texImage
);
1195 st_TexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1196 GLint xoffset
, GLint yoffset
,
1197 GLsizei width
, GLsizei height
,
1198 GLenum format
, GLenum type
, const GLvoid
* pixels
,
1199 const struct gl_pixelstore_attrib
*packing
,
1200 struct gl_texture_object
*texObj
,
1201 struct gl_texture_image
*texImage
)
1203 st_TexSubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
1204 width
, height
, 1, format
, type
,
1205 pixels
, packing
, texObj
, texImage
);
1210 st_TexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1211 GLint xoffset
, GLsizei width
, GLenum format
, GLenum type
,
1212 const GLvoid
* pixels
,
1213 const struct gl_pixelstore_attrib
*packing
,
1214 struct gl_texture_object
*texObj
,
1215 struct gl_texture_image
*texImage
)
1217 st_TexSubimage(ctx
, 1, target
, level
, xoffset
, 0, 0, width
, 1, 1,
1218 format
, type
, pixels
, packing
, texObj
, texImage
);
1223 st_CompressedTexSubImage1D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1224 GLint xoffset
, GLsizei width
,
1226 GLsizei imageSize
, const GLvoid
*data
,
1227 struct gl_texture_object
*texObj
,
1228 struct gl_texture_image
*texImage
)
1235 st_CompressedTexSubImage2D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1236 GLint xoffset
, GLint yoffset
,
1237 GLsizei width
, GLint height
,
1239 GLsizei imageSize
, const GLvoid
*data
,
1240 struct gl_texture_object
*texObj
,
1241 struct gl_texture_image
*texImage
)
1243 struct st_context
*st
= st_context(ctx
);
1244 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1248 enum pipe_format pformat
;
1251 pformat
= stImage
->pt
->format
;
1253 texImage
->Data
= st_texture_image_map(st
, stImage
, 0,
1254 PIPE_TRANSFER_WRITE
,
1258 srcBlockStride
= util_format_get_stride(pformat
, width
);
1259 dstBlockStride
= stImage
->transfer
->stride
;
1261 assert(stImage
->pt
);
1262 /* TODO find good values for block and strides */
1263 /* TODO also adjust texImage->data for yoffset/xoffset */
1267 if (!texImage
->Data
) {
1268 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage");
1272 assert(xoffset
% util_format_get_blockwidth(pformat
) == 0);
1273 assert(yoffset
% util_format_get_blockheight(pformat
) == 0);
1275 for (y
= 0; y
< height
; y
+= util_format_get_blockheight(pformat
)) {
1276 /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1277 const char *src
= (const char*)data
+ srcBlockStride
* util_format_get_nblocksy(pformat
, y
);
1278 char *dst
= (char*)texImage
->Data
+ dstBlockStride
* util_format_get_nblocksy(pformat
, y
);
1279 memcpy(dst
, src
, util_format_get_stride(pformat
, width
));
1283 st_texture_image_unmap(st
, stImage
);
1284 texImage
->Data
= NULL
;
1290 st_CompressedTexSubImage3D(struct gl_context
*ctx
, GLenum target
, GLint level
,
1291 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1292 GLsizei width
, GLint height
, GLint depth
,
1294 GLsizei imageSize
, const GLvoid
*data
,
1295 struct gl_texture_object
*texObj
,
1296 struct gl_texture_image
*texImage
)
1304 * Do a CopyTexSubImage operation using a read transfer from the source,
1305 * a write transfer to the destination and get_tile()/put_tile() to access
1306 * the pixels/texels.
1308 * Note: srcY=0=TOP of renderbuffer
1311 fallback_copy_texsubimage(struct gl_context
*ctx
, GLenum target
, GLint level
,
1312 struct st_renderbuffer
*strb
,
1313 struct st_texture_image
*stImage
,
1315 GLint destX
, GLint destY
, GLint destZ
,
1316 GLint srcX
, GLint srcY
,
1317 GLsizei width
, GLsizei height
)
1319 struct st_context
*st
= st_context(ctx
);
1320 struct pipe_context
*pipe
= st
->pipe
;
1321 struct pipe_transfer
*src_trans
;
1323 enum pipe_transfer_usage transfer_usage
;
1325 if (ST_DEBUG
& DEBUG_FALLBACK
)
1326 debug_printf("%s: fallback processing\n", __FUNCTION__
);
1328 assert(width
<= MAX_WIDTH
);
1330 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1331 srcY
= strb
->Base
.Height
- srcY
- height
;
1334 src_trans
= pipe_get_transfer(st_context(ctx
)->pipe
,
1341 if ((baseFormat
== GL_DEPTH_COMPONENT
||
1342 baseFormat
== GL_DEPTH_STENCIL
) &&
1343 util_format_is_depth_and_stencil(stImage
->pt
->format
))
1344 transfer_usage
= PIPE_TRANSFER_READ_WRITE
;
1346 transfer_usage
= PIPE_TRANSFER_WRITE
;
1348 /* XXX this used to ignore destZ param */
1349 texDest
= st_texture_image_map(st
, stImage
, destZ
, transfer_usage
,
1350 destX
, destY
, width
, height
);
1352 if (baseFormat
== GL_DEPTH_COMPONENT
||
1353 baseFormat
== GL_DEPTH_STENCIL
) {
1354 const GLboolean scaleOrBias
= (ctx
->Pixel
.DepthScale
!= 1.0F
||
1355 ctx
->Pixel
.DepthBias
!= 0.0F
);
1358 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
1359 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1368 /* To avoid a large temp memory allocation, do copy row by row */
1369 for (row
= 0; row
< height
; row
++, srcY
+= yStep
) {
1370 uint data
[MAX_WIDTH
];
1371 pipe_get_tile_z(pipe
, src_trans
, 0, srcY
, width
, 1, data
);
1373 _mesa_scale_and_bias_depth_uint(ctx
, width
, data
);
1375 pipe_put_tile_z(pipe
, stImage
->transfer
, 0, row
, width
, 1, data
);
1381 (GLfloat
*) malloc(width
* height
* 4 * sizeof(GLfloat
));
1383 if (tempSrc
&& texDest
) {
1384 const GLint dims
= 2;
1385 const GLint dstRowStride
= stImage
->transfer
->stride
;
1386 struct gl_texture_image
*texImage
= &stImage
->base
;
1387 struct gl_pixelstore_attrib unpack
= ctx
->DefaultPacking
;
1389 if (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
) {
1390 unpack
.Invert
= GL_TRUE
;
1393 /* get float/RGBA image from framebuffer */
1394 /* XXX this usually involves a lot of int/float conversion.
1395 * try to avoid that someday.
1397 pipe_get_tile_rgba_format(pipe
, src_trans
, 0, 0, width
, height
,
1398 util_format_linear(strb
->texture
->format
),
1401 /* Store into texture memory.
1402 * Note that this does some special things such as pixel transfer
1403 * ops and format conversion. In particular, if the dest tex format
1404 * is actually RGBA but the user created the texture as GL_RGB we
1405 * need to fill-in/override the alpha channel with 1.0.
1407 _mesa_texstore(ctx
, dims
,
1408 texImage
->_BaseFormat
,
1409 texImage
->TexFormat
,
1413 texImage
->ImageOffsets
,
1415 GL_RGBA
, GL_FLOAT
, tempSrc
, /* src */
1419 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexSubImage");
1426 st_texture_image_unmap(st
, stImage
);
1427 pipe
->transfer_destroy(pipe
, src_trans
);
1433 * If the format of the src renderbuffer and the format of the dest
1434 * texture are compatible (in terms of blitting), return a TGSI writemask
1435 * to be used during the blit.
1436 * If the src/dest are incompatible, return 0.
1439 compatible_src_dst_formats(struct gl_context
*ctx
,
1440 const struct gl_renderbuffer
*src
,
1441 const struct gl_texture_image
*dst
)
1443 /* Get logical base formats for the src and dest.
1444 * That is, use the user-requested formats and not the actual, device-
1446 * For example, the user may have requested an A8 texture but the
1447 * driver may actually be using an RGBA texture format. When we
1448 * copy/blit to that texture, we only want to copy the Alpha channel
1449 * and not the RGB channels.
1451 * Similarly, when the src FBO was created an RGB format may have been
1452 * requested but the driver actually chose an RGBA format. In that case,
1453 * we don't want to copy the undefined Alpha channel to the dest texture
1454 * (it should be 1.0).
1456 const GLenum srcFormat
= _mesa_base_fbo_format(ctx
, src
->InternalFormat
);
1457 const GLenum dstFormat
= _mesa_base_tex_format(ctx
, dst
->InternalFormat
);
1460 * XXX when we have red-only and red/green renderbuffers we'll need
1461 * to add more cases here (or implement a general-purpose routine that
1462 * queries the existance of the R,G,B,A channels in the src and dest).
1464 if (srcFormat
== dstFormat
) {
1465 /* This is the same as matching_base_formats, which should
1466 * always pass, as it did previously.
1468 return TGSI_WRITEMASK_XYZW
;
1470 else if (srcFormat
== GL_RGB
&& dstFormat
== GL_RGBA
) {
1471 /* Make sure that A in the dest is 1. The actual src format
1472 * may be RGBA and have undefined A values.
1474 return TGSI_WRITEMASK_XYZ
;
1476 else if (srcFormat
== GL_RGBA
&& dstFormat
== GL_RGB
) {
1477 /* Make sure that A in the dest is 1. The actual dst format
1478 * may be RGBA and will need A=1 to provide proper alpha values
1479 * when sampled later.
1481 return TGSI_WRITEMASK_XYZ
;
1484 if (ST_DEBUG
& DEBUG_FALLBACK
)
1485 debug_printf("%s failed for src %s, dst %s\n",
1487 _mesa_lookup_enum_by_nr(srcFormat
),
1488 _mesa_lookup_enum_by_nr(dstFormat
));
1499 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
1500 * Note that the region to copy has already been clipped so we know we
1501 * won't read from outside the source renderbuffer's bounds.
1503 * Note: srcY=0=Bottom of renderbuffer (GL convention)
1506 st_copy_texsubimage(struct gl_context
*ctx
,
1507 GLenum target
, GLint level
,
1508 GLint destX
, GLint destY
, GLint destZ
,
1509 GLint srcX
, GLint srcY
,
1510 GLsizei width
, GLsizei height
)
1512 struct gl_texture_unit
*texUnit
=
1513 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1514 struct gl_texture_object
*texObj
=
1515 _mesa_select_tex_object(ctx
, texUnit
, target
);
1516 struct gl_texture_image
*texImage
=
1517 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1518 struct st_texture_image
*stImage
= st_texture_image(texImage
);
1519 const GLenum texBaseFormat
= texImage
->_BaseFormat
;
1520 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
1521 struct st_renderbuffer
*strb
;
1522 struct st_context
*st
= st_context(ctx
);
1523 struct pipe_context
*pipe
= st
->pipe
;
1524 struct pipe_screen
*screen
= pipe
->screen
;
1525 enum pipe_format dest_format
, src_format
;
1526 GLboolean use_fallback
= GL_TRUE
;
1527 GLboolean matching_base_formats
;
1528 GLuint format_writemask
, sample_count
;
1529 struct pipe_surface
*dest_surface
= NULL
;
1530 GLboolean do_flip
= (st_fb_orientation(ctx
->ReadBuffer
) == Y_0_TOP
);
1532 /* make sure finalize_textures has been called?
1534 if (0) st_validate_state(st
);
1536 /* determine if copying depth or color data */
1537 if (texBaseFormat
== GL_DEPTH_COMPONENT
||
1538 texBaseFormat
== GL_DEPTH_STENCIL
) {
1539 strb
= st_renderbuffer(fb
->_DepthBuffer
);
1540 if (strb
->Base
.Wrapped
) {
1541 strb
= st_renderbuffer(strb
->Base
.Wrapped
);
1545 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
1546 strb
= st_renderbuffer(fb
->_ColorReadBuffer
);
1549 if (!strb
|| !strb
->surface
|| !stImage
->pt
) {
1550 debug_printf("%s: null strb or stImage\n", __FUNCTION__
);
1554 sample_count
= strb
->surface
->texture
->nr_samples
;
1555 /* I believe this would be legal, presumably would need to do a resolve
1556 for color, and for depth/stencil spec says to just use one of the
1557 depth/stencil samples per pixel? Need some transfer clarifications. */
1558 assert(sample_count
< 2);
1584 if (width
< 0 || height
< 0)
1589 assert(strb
->surface
);
1590 assert(stImage
->pt
);
1592 src_format
= strb
->surface
->format
;
1593 dest_format
= stImage
->pt
->format
;
1596 * Determine if the src framebuffer and dest texture have the same
1597 * base format. We need this to detect a case such as the framebuffer
1598 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
1599 * texture format stores RGBA we need to set A=1 (overriding the
1600 * framebuffer's alpha values). We can't do that with the blit or
1601 * textured-quad paths.
1603 matching_base_formats
=
1604 (_mesa_get_format_base_format(strb
->Base
.Format
) ==
1605 _mesa_get_format_base_format(texImage
->TexFormat
));
1606 format_writemask
= compatible_src_dst_formats(ctx
, &strb
->Base
, texImage
);
1608 if (ctx
->_ImageTransferState
== 0x0) {
1610 if (matching_base_formats
&&
1611 src_format
== dest_format
&&
1614 /* use surface_copy() / blit */
1615 struct pipe_box src_box
;
1616 u_box_2d_zslice(srcX
, srcY
, strb
->surface
->u
.tex
.first_layer
,
1617 width
, height
, &src_box
);
1619 /* for resource_copy_region(), y=0=top, always */
1620 pipe
->resource_copy_region(pipe
,
1624 destX
, destY
, destZ
+ stImage
->face
,
1627 strb
->surface
->u
.tex
.level
,
1629 use_fallback
= GL_FALSE
;
1631 else if (format_writemask
&&
1632 texBaseFormat
!= GL_DEPTH_COMPONENT
&&
1633 texBaseFormat
!= GL_DEPTH_STENCIL
&&
1634 screen
->is_format_supported(screen
, src_format
,
1635 PIPE_TEXTURE_2D
, sample_count
,
1636 PIPE_BIND_SAMPLER_VIEW
,
1638 screen
->is_format_supported(screen
, dest_format
,
1640 PIPE_BIND_RENDER_TARGET
,
1642 /* draw textured quad to do the copy */
1644 struct pipe_surface surf_tmpl
;
1645 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1646 surf_tmpl
.format
= stImage
->pt
->format
;
1647 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
1648 surf_tmpl
.u
.tex
.level
= stImage
->level
;
1649 surf_tmpl
.u
.tex
.first_layer
= stImage
->face
+ destZ
;
1650 surf_tmpl
.u
.tex
.last_layer
= stImage
->face
+ destZ
;
1652 dest_surface
= pipe
->create_surface(pipe
, stImage
->pt
,
1656 srcY1
= strb
->Base
.Height
- srcY
- height
;
1657 srcY0
= srcY1
+ height
;
1661 srcY1
= srcY0
+ height
;
1664 util_blit_pixels_writemask(st
->blit
,
1666 strb
->surface
->u
.tex
.level
,
1668 srcX
+ width
, srcY1
,
1669 strb
->surface
->u
.tex
.first_layer
,
1672 destX
+ width
, destY
+ height
,
1673 0.0, PIPE_TEX_MIPFILTER_NEAREST
,
1675 use_fallback
= GL_FALSE
;
1679 pipe_surface_reference(&dest_surface
, NULL
);
1683 /* software fallback */
1684 fallback_copy_texsubimage(ctx
, target
, level
,
1685 strb
, stImage
, texBaseFormat
,
1686 destX
, destY
, destZ
,
1687 srcX
, srcY
, width
, height
);
1694 st_CopyTexImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1695 GLenum internalFormat
,
1696 GLint x
, GLint y
, GLsizei width
, GLint border
)
1698 struct gl_texture_unit
*texUnit
=
1699 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1700 struct gl_texture_object
*texObj
=
1701 _mesa_select_tex_object(ctx
, texUnit
, target
);
1702 struct gl_texture_image
*texImage
=
1703 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1705 /* Setup or redefine the texture object, texture and texture
1706 * image. Don't populate yet.
1708 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
1710 GL_RGBA
, CHAN_TYPE
, NULL
,
1711 &ctx
->DefaultPacking
, texObj
, texImage
);
1713 st_copy_texsubimage(ctx
, target
, level
,
1714 0, 0, 0, /* destX,Y,Z */
1715 x
, y
, width
, 1); /* src X, Y, size */
1720 st_CopyTexImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1721 GLenum internalFormat
,
1722 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
1725 struct gl_texture_unit
*texUnit
=
1726 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
1727 struct gl_texture_object
*texObj
=
1728 _mesa_select_tex_object(ctx
, texUnit
, target
);
1729 struct gl_texture_image
*texImage
=
1730 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
1732 /* Setup or redefine the texture object, texture and texture
1733 * image. Don't populate yet.
1735 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
1736 width
, height
, border
,
1737 GL_RGBA
, CHAN_TYPE
, NULL
,
1738 &ctx
->DefaultPacking
, texObj
, texImage
);
1740 st_copy_texsubimage(ctx
, target
, level
,
1741 0, 0, 0, /* destX,Y,Z */
1742 x
, y
, width
, height
); /* src X, Y, size */
1747 st_CopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1748 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
1750 const GLint yoffset
= 0, zoffset
= 0;
1751 const GLsizei height
= 1;
1752 st_copy_texsubimage(ctx
, target
, level
,
1753 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1754 x
, y
, width
, height
); /* src X, Y, size */
1759 st_CopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1760 GLint xoffset
, GLint yoffset
,
1761 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1763 const GLint zoffset
= 0;
1764 st_copy_texsubimage(ctx
, target
, level
,
1765 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1766 x
, y
, width
, height
); /* src X, Y, size */
1771 st_CopyTexSubImage3D(struct gl_context
* ctx
, GLenum target
, GLint level
,
1772 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1773 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1775 st_copy_texsubimage(ctx
, target
, level
,
1776 xoffset
, yoffset
, zoffset
, /* destX,Y,Z */
1777 x
, y
, width
, height
); /* src X, Y, size */
1782 * Copy image data from stImage into the texture object 'stObj' at level
1786 copy_image_data_to_texture(struct st_context
*st
,
1787 struct st_texture_object
*stObj
,
1789 struct st_texture_image
*stImage
)
1793 const struct gl_texture_image
*dstImage
=
1794 stObj
->base
.Image
[stImage
->face
][stImage
->level
];
1796 assert(dstImage
->Width
== stImage
->base
.Width
);
1797 assert(dstImage
->Height
== stImage
->base
.Height
);
1798 assert(dstImage
->Depth
== stImage
->base
.Depth
);
1802 /* Copy potentially with the blitter:
1804 st_texture_image_copy(st
->pipe
,
1805 stObj
->pt
, dstLevel
, /* dest texture, level */
1806 stImage
->pt
, stImage
->level
, /* src texture, level */
1809 pipe_resource_reference(&stImage
->pt
, NULL
);
1811 else if (stImage
->base
.Data
) {
1812 st_texture_image_data(st
,
1817 stImage
->base
.RowStride
*
1818 util_format_get_blocksize(stObj
->pt
->format
),
1819 stImage
->base
.RowStride
*
1820 stImage
->base
.Height
*
1821 util_format_get_blocksize(stObj
->pt
->format
));
1822 _mesa_align_free(stImage
->base
.Data
);
1823 stImage
->base
.Data
= NULL
;
1826 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
1831 * Called during state validation. When this function is finished,
1832 * the texture object should be ready for rendering.
1833 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1836 st_finalize_texture(struct gl_context
*ctx
,
1837 struct pipe_context
*pipe
,
1838 struct gl_texture_object
*tObj
)
1840 struct st_context
*st
= st_context(ctx
);
1841 struct st_texture_object
*stObj
= st_texture_object(tObj
);
1842 const GLuint nr_faces
= (stObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
1844 struct st_texture_image
*firstImage
;
1845 enum pipe_format firstImageFormat
;
1846 GLuint ptWidth
, ptHeight
, ptDepth
, ptLayers
;
1848 if (stObj
->base
._Complete
) {
1849 /* The texture is complete and we know exactly how many mipmap levels
1850 * are present/needed. This is conditional because we may be called
1851 * from the st_generate_mipmap() function when the texture object is
1852 * incomplete. In that case, we'll have set stObj->lastLevel before
1855 if (stObj
->base
.MinFilter
== GL_LINEAR
||
1856 stObj
->base
.MinFilter
== GL_NEAREST
)
1857 stObj
->lastLevel
= stObj
->base
.BaseLevel
;
1859 stObj
->lastLevel
= stObj
->base
._MaxLevel
;
1862 firstImage
= st_texture_image(stObj
->base
.Image
[0][stObj
->base
.BaseLevel
]);
1865 /* If both firstImage and stObj point to a texture which can contain
1866 * all active images, favour firstImage. Note that because of the
1867 * completeness requirement, we know that the image dimensions
1870 if (firstImage
->pt
&&
1871 firstImage
->pt
!= stObj
->pt
&&
1872 (!stObj
->pt
|| firstImage
->pt
->last_level
>= stObj
->pt
->last_level
)) {
1873 pipe_resource_reference(&stObj
->pt
, firstImage
->pt
);
1874 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1877 /* Find gallium format for the Mesa texture */
1878 firstImageFormat
= st_mesa_format_to_pipe_format(firstImage
->base
.TexFormat
);
1879 st_gl_texture_dims_to_pipe_dims(stObj
->base
.Target
, stObj
->width0
,
1880 stObj
->height0
, stObj
->depth0
,
1881 &ptWidth
, &ptHeight
, &ptDepth
, &ptLayers
);
1883 /* If we already have a gallium texture, check that it matches the texture
1884 * object's format, target, size, num_levels, etc.
1887 if (stObj
->pt
->target
!= gl_target_to_pipe(stObj
->base
.Target
) ||
1888 !st_sampler_compat_formats(stObj
->pt
->format
, firstImageFormat
) ||
1889 stObj
->pt
->last_level
< stObj
->lastLevel
||
1890 stObj
->pt
->width0
!= ptWidth
||
1891 stObj
->pt
->height0
!= ptHeight
||
1892 stObj
->pt
->depth0
!= ptDepth
||
1893 stObj
->pt
->array_size
!= ptLayers
)
1895 /* The gallium texture does not match the Mesa texture so delete the
1896 * gallium texture now. We'll make a new one below.
1898 pipe_resource_reference(&stObj
->pt
, NULL
);
1899 pipe_sampler_view_reference(&stObj
->sampler_view
, NULL
);
1900 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
1904 /* May need to create a new gallium texture:
1907 GLuint bindings
= default_bindings(st
, firstImageFormat
);
1909 stObj
->pt
= st_texture_create(st
,
1910 gl_target_to_pipe(stObj
->base
.Target
),
1920 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage");
1925 /* Pull in any images not in the object's texture:
1927 for (face
= 0; face
< nr_faces
; face
++) {
1929 for (level
= stObj
->base
.BaseLevel
; level
<= stObj
->lastLevel
; level
++) {
1930 struct st_texture_image
*stImage
=
1931 st_texture_image(stObj
->base
.Image
[face
][level
]);
1933 /* Need to import images in main memory or held in other textures.
1935 if (stImage
&& stObj
->pt
!= stImage
->pt
) {
1936 copy_image_data_to_texture(st
, stObj
, level
, stImage
);
1946 * Returns pointer to a default/dummy texture.
1947 * This is typically used when the current shader has tex/sample instructions
1948 * but the user has not provided a (any) texture(s).
1950 struct gl_texture_object
*
1951 st_get_default_texture(struct st_context
*st
)
1953 if (!st
->default_texture
) {
1954 static const GLenum target
= GL_TEXTURE_2D
;
1955 GLubyte pixels
[16][16][4];
1956 struct gl_texture_object
*texObj
;
1957 struct gl_texture_image
*texImg
;
1960 /* The ARB_fragment_program spec says (0,0,0,1) should be returned
1961 * when attempting to sample incomplete textures.
1963 for (i
= 0; i
< 16; i
++) {
1964 for (j
= 0; j
< 16; j
++) {
1965 pixels
[i
][j
][0] = 0;
1966 pixels
[i
][j
][1] = 0;
1967 pixels
[i
][j
][2] = 0;
1968 pixels
[i
][j
][3] = 255;
1972 texObj
= st
->ctx
->Driver
.NewTextureObject(st
->ctx
, 0, target
);
1974 texImg
= _mesa_get_tex_image(st
->ctx
, texObj
, target
, 0);
1976 _mesa_init_teximage_fields(st
->ctx
, target
, texImg
,
1977 16, 16, 1, 0, /* w, h, d, border */
1978 GL_RGBA
, MESA_FORMAT_RGBA8888
);
1980 st_TexImage(st
->ctx
, 2, target
,
1981 0, GL_RGBA
, /* level, intformat */
1982 16, 16, 1, 0, /* w, h, d, border */
1983 GL_RGBA
, GL_UNSIGNED_BYTE
, pixels
,
1984 &st
->ctx
->DefaultPacking
,
1988 texObj
->MinFilter
= GL_NEAREST
;
1989 texObj
->MagFilter
= GL_NEAREST
;
1990 texObj
->_Complete
= GL_TRUE
;
1992 st
->default_texture
= texObj
;
1994 return st
->default_texture
;
1999 st_init_texture_functions(struct dd_function_table
*functions
)
2001 functions
->ChooseTextureFormat
= st_ChooseTextureFormat
;
2002 functions
->TexImage1D
= st_TexImage1D
;
2003 functions
->TexImage2D
= st_TexImage2D
;
2004 functions
->TexImage3D
= st_TexImage3D
;
2005 functions
->TexSubImage1D
= st_TexSubImage1D
;
2006 functions
->TexSubImage2D
= st_TexSubImage2D
;
2007 functions
->TexSubImage3D
= st_TexSubImage3D
;
2008 functions
->CompressedTexSubImage1D
= st_CompressedTexSubImage1D
;
2009 functions
->CompressedTexSubImage2D
= st_CompressedTexSubImage2D
;
2010 functions
->CompressedTexSubImage3D
= st_CompressedTexSubImage3D
;
2011 functions
->CopyTexImage1D
= st_CopyTexImage1D
;
2012 functions
->CopyTexImage2D
= st_CopyTexImage2D
;
2013 functions
->CopyTexSubImage1D
= st_CopyTexSubImage1D
;
2014 functions
->CopyTexSubImage2D
= st_CopyTexSubImage2D
;
2015 functions
->CopyTexSubImage3D
= st_CopyTexSubImage3D
;
2016 functions
->GenerateMipmap
= st_generate_mipmap
;
2018 functions
->GetTexImage
= st_GetTexImage
;
2020 /* compressed texture functions */
2021 functions
->CompressedTexImage2D
= st_CompressedTexImage2D
;
2022 functions
->GetCompressedTexImage
= st_GetCompressedTexImage
;
2024 functions
->NewTextureObject
= st_NewTextureObject
;
2025 functions
->NewTextureImage
= st_NewTextureImage
;
2026 functions
->DeleteTexture
= st_DeleteTextureObject
;
2027 functions
->FreeTexImageData
= st_FreeTextureImageData
;
2029 functions
->TextureMemCpy
= do_memcpy
;
2031 /* XXX Temporary until we can query pipe's texture sizes */
2032 functions
->TestProxyTexImage
= _mesa_test_proxy_teximage
;