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)
77 irb
= intel_renderbuffer(intel
->ctx
.ReadBuffer
->_ColorReadBuffer
);
78 /* We're required to set alpha to 1.0 in this case, but we can't
79 * do that with the blitter, so fall back. We could use the 3D
80 * engine or do two passes with the blitter, but it doesn't seem
81 * worth it for this case. */
82 if (irb
->Base
._BaseFormat
== GL_RGB
)
87 return intel_readbuf_region(intel
);
95 do_copy_texsubimage(struct intel_context
*intel
,
97 struct intel_texture_image
*intelImage
,
98 GLenum internalFormat
,
99 GLint dstx
, GLint dsty
,
100 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
102 GLcontext
*ctx
= &intel
->ctx
;
103 const struct intel_region
*src
= get_teximage_source(intel
, internalFormat
);
105 if (!intelImage
->mt
|| !src
|| !src
->buffer
) {
106 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
107 fprintf(stderr
, "%s fail %p %p (0x%08x)\n",
108 __FUNCTION__
, intelImage
->mt
, src
, internalFormat
);
112 if (intelImage
->mt
->cpp
!= src
->cpp
) {
113 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
114 fprintf(stderr
, "%s fail %d vs %d cpp\n",
115 __FUNCTION__
, intelImage
->mt
->cpp
, src
->cpp
);
119 /* intel_flush(ctx); */
120 intel_prepare_render(intel
);
122 drm_intel_bo
*dst_bo
= intel_region_buffer(intel
,
123 intelImage
->mt
->region
,
125 GLuint image_x
, image_y
;
128 /* get dest x/y in destination texture */
129 intel_miptree_get_image_offset(intelImage
->mt
,
135 /* The blitter can't handle Y-tiled buffers. */
136 if (intelImage
->mt
->region
->tiling
== I915_TILING_Y
) {
140 if (ctx
->ReadBuffer
->Name
== 0) {
141 /* Flip vertical orientation for system framebuffers */
142 y
= ctx
->ReadBuffer
->Height
- (y
+ height
);
143 src_pitch
= -src
->pitch
;
145 /* reading from a FBO, y is already oriented the way we like */
146 src_pitch
= src
->pitch
;
149 /* blit from src buffer to texture */
150 if (!intelEmitCopyBlit(intel
,
156 intelImage
->mt
->region
->pitch
,
159 intelImage
->mt
->region
->tiling
,
160 src
->draw_x
+ x
, src
->draw_y
+ y
,
161 image_x
+ dstx
, image_y
+ dsty
,
173 intelCopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
174 GLenum internalFormat
,
175 GLint x
, GLint y
, GLsizei width
, GLint border
)
177 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
178 struct gl_texture_object
*texObj
=
179 _mesa_select_tex_object(ctx
, texUnit
, target
);
180 struct gl_texture_image
*texImage
=
181 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
182 int srcx
, srcy
, dstx
, dsty
, height
;
187 /* Setup or redefine the texture object, mipmap tree and texture
188 * image. Don't populate yet.
190 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
192 GL_RGBA
, CHAN_TYPE
, NULL
,
193 &ctx
->DefaultPacking
, texObj
, texImage
);
199 if (!_mesa_clip_copytexsubimage(ctx
,
205 if (!do_copy_texsubimage(intel_context(ctx
), target
,
206 intel_texture_image(texImage
),
207 internalFormat
, 0, 0, x
, y
, width
, height
))
213 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
214 fprintf(stderr
, "%s - fallback to swrast\n", __FUNCTION__
);
215 _mesa_meta_CopyTexImage1D(ctx
, target
, level
, internalFormat
, x
, y
,
221 intelCopyTexImage2D(GLcontext
* 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 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
263 fprintf(stderr
, "%s - fallback to swrast\n", __FUNCTION__
);
264 _mesa_meta_CopyTexImage2D(ctx
, target
, level
, internalFormat
, x
, y
,
265 width
, height
, border
);
270 intelCopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
271 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
273 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
274 struct gl_texture_object
*texObj
=
275 _mesa_select_tex_object(ctx
, texUnit
, target
);
276 struct gl_texture_image
*texImage
=
277 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
278 GLenum internalFormat
= texImage
->InternalFormat
;
280 /* XXX need to check <border> as in above function? */
282 /* Need to check texture is compatible with source format.
285 if (!do_copy_texsubimage(intel_context(ctx
), target
,
286 intel_texture_image(texImage
),
287 internalFormat
, xoffset
, 0, x
, y
, width
, 1)) {
288 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
289 fprintf(stderr
, "%s - fallback to swrast\n", __FUNCTION__
);
290 _mesa_meta_CopyTexSubImage1D(ctx
, target
, level
, xoffset
, x
, y
, width
);
296 intelCopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
297 GLint xoffset
, GLint yoffset
,
298 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
300 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
301 struct gl_texture_object
*texObj
=
302 _mesa_select_tex_object(ctx
, texUnit
, target
);
303 struct gl_texture_image
*texImage
=
304 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
305 GLenum internalFormat
= texImage
->InternalFormat
;
307 /* Need to check texture is compatible with source format.
310 if (!do_copy_texsubimage(intel_context(ctx
), target
,
311 intel_texture_image(texImage
),
313 xoffset
, yoffset
, x
, y
, width
, height
)) {
315 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
316 fprintf(stderr
, "%s - fallback to swrast\n", __FUNCTION__
);
317 _mesa_meta_CopyTexSubImage2D(ctx
, target
, level
,
318 xoffset
, yoffset
, x
, y
, width
, height
);
324 intelInitTextureCopyImageFuncs(struct dd_function_table
*functions
)
326 functions
->CopyTexImage1D
= intelCopyTexImage1D
;
327 functions
->CopyTexImage2D
= intelCopyTexImage2D
;
328 functions
->CopyTexSubImage1D
= intelCopyTexSubImage1D
;
329 functions
->CopyTexSubImage2D
= intelCopyTexSubImage2D
;