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"
33 #include "main/mipmap.h"
35 #include "drivers/common/meta.h"
37 #include "intel_screen.h"
38 #include "intel_context.h"
39 #include "intel_buffers.h"
40 #include "intel_mipmap_tree.h"
41 #include "intel_regions.h"
42 #include "intel_fbo.h"
43 #include "intel_tex.h"
44 #include "intel_blit.h"
46 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
49 * Get the intel_region which is the source for any glCopyTex[Sub]Image call.
51 * Do the best we can using the blitter. A future project is to use
52 * the texture engine and fragment programs for these copies.
54 static const struct intel_region
*
55 get_teximage_source(struct intel_context
*intel
, GLenum internalFormat
)
57 struct intel_renderbuffer
*irb
;
59 DBG("%s %s\n", __FUNCTION__
,
60 _mesa_lookup_enum_by_nr(internalFormat
));
62 switch (internalFormat
) {
63 case GL_DEPTH_COMPONENT
:
64 case GL_DEPTH_COMPONENT16
:
65 irb
= intel_get_renderbuffer(intel
->ctx
.ReadBuffer
, BUFFER_DEPTH
);
66 if (irb
&& irb
->region
&& irb
->region
->cpp
== 2)
69 case GL_DEPTH24_STENCIL8_EXT
:
70 case GL_DEPTH_STENCIL_EXT
:
71 irb
= intel_get_renderbuffer(intel
->ctx
.ReadBuffer
, BUFFER_DEPTH
);
72 if (irb
&& irb
->region
&& irb
->region
->cpp
== 4)
78 irb
= intel_renderbuffer(intel
->ctx
.ReadBuffer
->_ColorReadBuffer
);
79 /* We're required to set alpha to 1.0 in this case, but we can't
80 * do that with the blitter, so fall back. We could use the 3D
81 * engine or do two passes with the blitter, but it doesn't seem
82 * worth it for this case. */
83 if (irb
->Base
._BaseFormat
== GL_RGB
)
89 return intel_readbuf_region(intel
);
97 do_copy_texsubimage(struct intel_context
*intel
,
99 struct intel_texture_image
*intelImage
,
100 GLenum internalFormat
,
101 GLint dstx
, GLint dsty
,
102 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
104 struct gl_context
*ctx
= &intel
->ctx
;
105 const struct intel_region
*src
= get_teximage_source(intel
, internalFormat
);
107 if (!intelImage
->mt
|| !src
|| !src
->buffer
) {
108 if (unlikely(INTEL_DEBUG
& DEBUG_FALLBACKS
))
109 fprintf(stderr
, "%s fail %p %p (0x%08x)\n",
110 __FUNCTION__
, intelImage
->mt
, src
, internalFormat
);
114 if (intelImage
->mt
->cpp
!= src
->cpp
) {
115 fallback_debug("%s fail %d vs %d cpp\n",
116 __FUNCTION__
, intelImage
->mt
->cpp
, src
->cpp
);
120 /* intel_flush(ctx); */
121 intel_prepare_render(intel
);
123 drm_intel_bo
*dst_bo
= intel_region_buffer(intel
,
124 intelImage
->mt
->region
,
126 GLuint image_x
, image_y
;
129 /* get dest x/y in destination texture */
130 intel_miptree_get_image_offset(intelImage
->mt
,
136 /* The blitter can't handle Y-tiled buffers. */
137 if (intelImage
->mt
->region
->tiling
== I915_TILING_Y
) {
141 if (ctx
->ReadBuffer
->Name
== 0) {
142 /* Flip vertical orientation for system framebuffers */
143 y
= ctx
->ReadBuffer
->Height
- (y
+ height
);
144 src_pitch
= -src
->pitch
;
146 /* reading from a FBO, y is already oriented the way we like */
147 src_pitch
= src
->pitch
;
150 /* blit from src buffer to texture */
151 if (!intelEmitCopyBlit(intel
,
157 intelImage
->mt
->region
->pitch
,
160 intelImage
->mt
->region
->tiling
,
161 src
->draw_x
+ x
, src
->draw_y
+ y
,
162 image_x
+ dstx
, image_y
+ dsty
,
174 intelCopyTexImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
175 GLenum internalFormat
,
176 GLint x
, GLint y
, GLsizei width
, GLint border
)
178 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
179 struct gl_texture_object
*texObj
=
180 _mesa_select_tex_object(ctx
, texUnit
, target
);
181 struct gl_texture_image
*texImage
=
182 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
183 int srcx
, srcy
, dstx
, dsty
, height
;
188 /* Setup or redefine the texture object, mipmap tree and texture
189 * image. Don't populate yet.
191 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
193 GL_RGBA
, CHAN_TYPE
, NULL
,
194 &ctx
->DefaultPacking
, texObj
, texImage
);
200 if (!_mesa_clip_copytexsubimage(ctx
,
206 if (!do_copy_texsubimage(intel_context(ctx
), target
,
207 intel_texture_image(texImage
),
208 internalFormat
, 0, 0, x
, y
, width
, height
))
214 fallback_debug("%s - fallback to swrast\n", __FUNCTION__
);
215 _mesa_meta_CopyTexImage1D(ctx
, target
, level
, internalFormat
, x
, y
,
221 intelCopyTexImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
222 GLenum internalFormat
,
223 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
226 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
227 struct gl_texture_object
*texObj
=
228 _mesa_select_tex_object(ctx
, texUnit
, target
);
229 struct gl_texture_image
*texImage
=
230 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
231 int srcx
, srcy
, dstx
, dsty
;
236 /* Setup or redefine the texture object, mipmap tree and texture
237 * image. Don't populate yet.
239 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
240 width
, height
, border
,
241 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
,
242 &ctx
->DefaultPacking
, texObj
, texImage
);
248 if (!_mesa_clip_copytexsubimage(ctx
,
254 if (!do_copy_texsubimage(intel_context(ctx
), target
,
255 intel_texture_image(texImage
),
256 internalFormat
, 0, 0, x
, y
, width
, height
))
262 fallback_debug("%s - fallback to swrast\n", __FUNCTION__
);
263 _mesa_meta_CopyTexImage2D(ctx
, target
, level
, internalFormat
, x
, y
,
264 width
, height
, border
);
269 intelCopyTexSubImage1D(struct gl_context
* ctx
, GLenum target
, GLint level
,
270 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
272 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
273 struct gl_texture_object
*texObj
=
274 _mesa_select_tex_object(ctx
, texUnit
, target
);
275 struct gl_texture_image
*texImage
=
276 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
277 GLenum internalFormat
= texImage
->InternalFormat
;
279 /* XXX need to check <border> as in above function? */
281 /* Need to check texture is compatible with source format.
284 if (!do_copy_texsubimage(intel_context(ctx
), target
,
285 intel_texture_image(texImage
),
286 internalFormat
, xoffset
, 0, x
, y
, width
, 1)) {
287 fallback_debug("%s - fallback to swrast\n", __FUNCTION__
);
288 _mesa_meta_CopyTexSubImage1D(ctx
, target
, level
, xoffset
, x
, y
, width
);
294 intelCopyTexSubImage2D(struct gl_context
* ctx
, GLenum target
, GLint level
,
295 GLint xoffset
, GLint yoffset
,
296 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
298 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
299 struct gl_texture_object
*texObj
=
300 _mesa_select_tex_object(ctx
, texUnit
, target
);
301 struct gl_texture_image
*texImage
=
302 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
303 GLenum internalFormat
= texImage
->InternalFormat
;
305 /* Need to check texture is compatible with source format.
308 if (!do_copy_texsubimage(intel_context(ctx
), target
,
309 intel_texture_image(texImage
),
311 xoffset
, yoffset
, x
, y
, width
, height
)) {
313 fallback_debug("%s - fallback to swrast\n", __FUNCTION__
);
314 _mesa_meta_CopyTexSubImage2D(ctx
, target
, level
,
315 xoffset
, yoffset
, x
, y
, width
, height
);
321 intelInitTextureCopyImageFuncs(struct dd_function_table
*functions
)
323 functions
->CopyTexImage1D
= intelCopyTexImage1D
;
324 functions
->CopyTexImage2D
= intelCopyTexImage2D
;
325 functions
->CopyTexSubImage1D
= intelCopyTexSubImage1D
;
326 functions
->CopyTexSubImage2D
= intelCopyTexSubImage2D
;