1 /**************************************************************************
3 * Copyright 2003 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/mtypes.h"
29 #include "main/enums.h"
30 #include "main/image.h"
31 #include "main/teximage.h"
32 #include "main/texstate.h"
34 #include "drivers/common/meta.h"
36 #include "intel_screen.h"
37 #include "intel_context.h"
38 #include "intel_mipmap_tree.h"
39 #include "intel_regions.h"
40 #include "intel_fbo.h"
41 #include "intel_tex.h"
42 #include "intel_blit.h"
44 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
47 * Get the intel_region which is the source for any glCopyTex[Sub]Image call.
49 * Do the best we can using the blitter. A future project is to use
50 * the texture engine and fragment programs for these copies.
52 static struct intel_renderbuffer
*
53 get_teximage_readbuffer(struct intel_context
*intel
, GLenum internalFormat
)
55 DBG("%s %s\n", __FUNCTION__
,
56 _mesa_lookup_enum_by_nr(internalFormat
));
58 if (_mesa_is_depth_format(internalFormat
) ||
59 _mesa_is_depthstencil_format(internalFormat
))
60 return intel_get_renderbuffer(intel
->ctx
.ReadBuffer
, BUFFER_DEPTH
);
62 return intel_renderbuffer(intel
->ctx
.ReadBuffer
->_ColorReadBuffer
);
67 intel_copy_texsubimage(struct intel_context
*intel
,
68 struct intel_texture_image
*intelImage
,
69 GLint dstx
, GLint dsty
,
70 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
72 struct gl_context
*ctx
= &intel
->ctx
;
73 struct intel_renderbuffer
*irb
;
74 struct intel_region
*region
;
75 const GLenum internalFormat
= intelImage
->base
.Base
.InternalFormat
;
76 bool copy_supported
= false;
77 bool copy_supported_with_alpha_override
= false;
79 intel_prepare_render(intel
);
81 irb
= get_teximage_readbuffer(intel
, internalFormat
);
82 if (!intelImage
->mt
|| !irb
|| !irb
->mt
) {
83 if (unlikely(INTEL_DEBUG
& DEBUG_FALLBACKS
))
84 fprintf(stderr
, "%s fail %p %p (0x%08x)\n",
85 __FUNCTION__
, intelImage
->mt
, irb
, internalFormat
);
88 region
= irb
->mt
->region
;
92 copy_supported
= intelImage
->base
.Base
.TexFormat
== irb
->Base
.Format
;
94 /* Converting ARGB8888 to XRGB8888 is trivial: ignore the alpha bits */
95 if (irb
->Base
.Format
== MESA_FORMAT_ARGB8888
&&
96 intelImage
->base
.Base
.TexFormat
== MESA_FORMAT_XRGB8888
) {
97 copy_supported
= true;
100 /* Converting XRGB8888 to ARGB8888 requires setting the alpha bits to 1.0 */
101 if (irb
->Base
.Format
== MESA_FORMAT_XRGB8888
&&
102 intelImage
->base
.Base
.TexFormat
== MESA_FORMAT_ARGB8888
) {
103 copy_supported_with_alpha_override
= true;
106 if (!copy_supported
&& !copy_supported_with_alpha_override
) {
107 if (unlikely(INTEL_DEBUG
& DEBUG_FALLBACKS
))
108 fprintf(stderr
, "%s mismatched formats %s, %s\n",
110 _mesa_get_format_name(intelImage
->base
.Base
.TexFormat
),
111 _mesa_get_format_name(irb
->Base
.Format
));
116 GLuint image_x
, image_y
;
119 /* get dest x/y in destination texture */
120 intel_miptree_get_image_offset(intelImage
->mt
,
121 intelImage
->base
.Base
.Level
,
122 intelImage
->base
.Base
.Face
,
126 /* The blitter can't handle Y-tiled buffers. */
127 if (intelImage
->mt
->region
->tiling
== I915_TILING_Y
) {
131 if (ctx
->ReadBuffer
->Name
== 0) {
132 /* Flip vertical orientation for system framebuffers */
133 y
= ctx
->ReadBuffer
->Height
- (y
+ height
);
134 src_pitch
= -region
->pitch
;
136 /* reading from a FBO, y is already oriented the way we like */
137 src_pitch
= region
->pitch
;
140 /* blit from src buffer to texture */
141 if (!intelEmitCopyBlit(intel
,
147 intelImage
->mt
->region
->pitch
,
148 intelImage
->mt
->region
->bo
,
150 intelImage
->mt
->region
->tiling
,
151 irb
->draw_x
+ x
, irb
->draw_y
+ y
,
152 image_x
+ dstx
, image_y
+ dsty
,
159 if (copy_supported_with_alpha_override
)
160 intel_set_teximage_alpha_to_one(ctx
, intelImage
);
167 intelCopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
168 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
170 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
171 struct gl_texture_object
*texObj
=
172 _mesa_select_tex_object(ctx
, texUnit
, target
);
173 struct gl_texture_image
*texImage
=
174 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
176 /* XXX need to check <border> as in above function? */
178 /* Need to check texture is compatible with source format.
181 if (!intel_copy_texsubimage(intel_context(ctx
),
182 intel_texture_image(texImage
),
183 xoffset
, 0, x
, y
, width
, 1)) {
184 fallback_debug("%s - fallback to swrast\n", __FUNCTION__
);
185 _mesa_meta_CopyTexSubImage1D(ctx
, target
, level
, xoffset
, x
, y
, width
);
191 intelCopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
192 GLint xoffset
, GLint yoffset
,
193 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
195 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
196 struct gl_texture_object
*texObj
=
197 _mesa_select_tex_object(ctx
, texUnit
, target
);
198 struct gl_texture_image
*texImage
=
199 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
201 /* Need to check texture is compatible with source format.
204 if (!intel_copy_texsubimage(intel_context(ctx
),
205 intel_texture_image(texImage
),
206 xoffset
, yoffset
, x
, y
, width
, height
)) {
207 fallback_debug("%s - fallback to swrast\n", __FUNCTION__
);
208 _mesa_meta_CopyTexSubImage2D(ctx
, target
, level
,
209 xoffset
, yoffset
, x
, y
, width
, height
);
215 intelInitTextureCopyImageFuncs(struct dd_function_table
*functions
)
217 functions
->CopyTexSubImage1D
= intelCopyTexSubImage1D
;
218 functions
->CopyTexSubImage2D
= intelCopyTexSubImage2D
;