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/mipmap.h"
34 #include "drivers/common/meta.h"
36 #include "intel_screen.h"
37 #include "intel_context.h"
38 #include "intel_batchbuffer.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 return intel_readbuf_region(intel
);
86 do_copy_texsubimage(struct intel_context
*intel
,
88 struct intel_texture_image
*intelImage
,
89 GLenum internalFormat
,
90 GLint dstx
, GLint dsty
,
91 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
93 GLcontext
*ctx
= &intel
->ctx
;
94 const struct intel_region
*src
=
95 get_teximage_source(intel
, internalFormat
);
97 if (!intelImage
->mt
|| !src
) {
98 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
99 fprintf(stderr
, "%s fail %p %p (0x%08x)\n",
100 __FUNCTION__
, intelImage
->mt
, src
, internalFormat
);
104 if (intelImage
->mt
->cpp
!= src
->cpp
) {
105 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
106 fprintf(stderr
, "%s fail %d vs %d cpp\n",
107 __FUNCTION__
, intelImage
->mt
->cpp
, src
->cpp
);
112 LOCK_HARDWARE(intel
);
114 drm_intel_bo
*dst_bo
= intel_region_buffer(intel
,
115 intelImage
->mt
->region
,
117 GLuint image_offset
= intel_miptree_image_offset(intelImage
->mt
,
120 const GLint orig_x
= x
;
121 const GLint orig_y
= y
;
124 /* Update dst for clipped src. Need to also clip the source rect. */
128 /* Can't blit to tiled buffers with non-tile-aligned offset. */
129 if (intelImage
->mt
->region
->tiling
!= I915_TILING_NONE
&&
130 (image_offset
& 4095) != 0) {
131 UNLOCK_HARDWARE(intel
);
135 if (ctx
->ReadBuffer
->Name
== 0) {
136 /* reading from a window, adjust x, y */
137 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
138 y
= dPriv
->y
+ (dPriv
->h
- (y
+ height
));
141 /* Invert the data coming from the source rectangle due to GL
142 * and hardware disagreeing on where y=0 is.
144 * It appears that our offsets and pitches get mangled
145 * appropriately by the hardware, and we don't need to adjust them
148 src_pitch
= -src
->pitch
;
150 /* reading from a FBO, y is already oriented the way we like */
151 src_pitch
= src
->pitch
;
154 if (!intelEmitCopyBlit(intel
,
160 intelImage
->mt
->pitch
,
163 intelImage
->mt
->region
->tiling
,
164 x
, y
, dstx
, dsty
, width
, height
,
166 UNLOCK_HARDWARE(intel
);
171 UNLOCK_HARDWARE(intel
);
178 intelCopyTexImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
179 GLenum internalFormat
,
180 GLint x
, GLint y
, GLsizei width
, GLint border
)
182 struct gl_texture_unit
*texUnit
=
183 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
184 struct gl_texture_object
*texObj
=
185 _mesa_select_tex_object(ctx
, texUnit
, target
);
186 struct gl_texture_image
*texImage
=
187 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
188 int srcx
, srcy
, dstx
, dsty
, height
;
193 /* Setup or redefine the texture object, mipmap tree and texture
194 * image. Don't populate yet.
196 ctx
->Driver
.TexImage1D(ctx
, target
, level
, internalFormat
,
198 GL_RGBA
, CHAN_TYPE
, NULL
,
199 &ctx
->DefaultPacking
, texObj
, texImage
);
205 if (!_mesa_clip_copytexsubimage(ctx
,
211 if (!do_copy_texsubimage(intel_context(ctx
), target
,
212 intel_texture_image(texImage
),
213 internalFormat
, 0, 0, x
, y
, width
, height
))
219 _mesa_meta_CopyTexImage1D(ctx
, target
, level
, internalFormat
, x
, y
,
225 intelCopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
226 GLenum internalFormat
,
227 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
230 struct gl_texture_unit
*texUnit
=
231 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
232 struct gl_texture_object
*texObj
=
233 _mesa_select_tex_object(ctx
, texUnit
, target
);
234 struct gl_texture_image
*texImage
=
235 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
236 int srcx
, srcy
, dstx
, dsty
;
241 /* Setup or redefine the texture object, mipmap tree and texture
242 * image. Don't populate yet.
244 ctx
->Driver
.TexImage2D(ctx
, target
, level
, internalFormat
,
245 width
, height
, border
,
246 GL_RGBA
, CHAN_TYPE
, NULL
,
247 &ctx
->DefaultPacking
, texObj
, texImage
);
253 if (!_mesa_clip_copytexsubimage(ctx
,
259 if (!do_copy_texsubimage(intel_context(ctx
), target
,
260 intel_texture_image(texImage
),
261 internalFormat
, 0, 0, x
, y
, width
, height
))
267 _mesa_meta_CopyTexImage2D(ctx
, target
, level
, internalFormat
, x
, y
,
268 width
, height
, border
);
273 intelCopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
274 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
276 struct gl_texture_unit
*texUnit
=
277 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
278 struct gl_texture_object
*texObj
=
279 _mesa_select_tex_object(ctx
, texUnit
, target
);
280 struct gl_texture_image
*texImage
=
281 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
282 GLenum internalFormat
= texImage
->InternalFormat
;
284 /* XXX need to check <border> as in above function? */
286 /* Need to check texture is compatible with source format.
289 if (!do_copy_texsubimage(intel_context(ctx
), target
,
290 intel_texture_image(texImage
),
291 internalFormat
, xoffset
, 0, x
, y
, width
, 1)) {
292 _mesa_meta_CopyTexSubImage1D(ctx
, target
, level
, xoffset
, x
, y
, width
);
298 intelCopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
299 GLint xoffset
, GLint yoffset
,
300 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
302 struct gl_texture_unit
*texUnit
=
303 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
304 struct gl_texture_object
*texObj
=
305 _mesa_select_tex_object(ctx
, texUnit
, target
);
306 struct gl_texture_image
*texImage
=
307 _mesa_select_tex_image(ctx
, texObj
, target
, level
);
308 GLenum internalFormat
= texImage
->InternalFormat
;
310 /* Need to check texture is compatible with source format.
313 if (!do_copy_texsubimage(intel_context(ctx
), target
,
314 intel_texture_image(texImage
),
316 xoffset
, yoffset
, x
, y
, width
, height
)) {
318 DBG("%s - fallback to _mesa_meta_CopyTexSubImage2D\n", __FUNCTION__
);
320 _mesa_meta_CopyTexSubImage2D(ctx
, target
, level
,
321 xoffset
, yoffset
, x
, y
, width
, height
);
327 intelInitTextureCopyImageFuncs(struct dd_function_table
*functions
)
329 functions
->CopyTexImage1D
= intelCopyTexImage1D
;
330 functions
->CopyTexImage2D
= intelCopyTexImage2D
;
331 functions
->CopyTexSubImage1D
= intelCopyTexSubImage1D
;
332 functions
->CopyTexSubImage2D
= intelCopyTexSubImage2D
;