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"
33 #include "swrast/swrast.h"
35 #include "intel_screen.h"
36 #include "intel_context.h"
37 #include "intel_batchbuffer.h"
38 #include "intel_buffers.h"
39 #include "intel_mipmap_tree.h"
40 #include "intel_regions.h"
41 #include "intel_fbo.h"
42 #include "intel_tex.h"
43 #include "intel_blit.h"
45 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
48 * Get the intel_region which is the source for any glCopyTex[Sub]Image call.
50 * Do the best we can using the blitter. A future project is to use
51 * the texture engine and fragment programs for these copies.
53 static const struct intel_region
*
54 get_teximage_source(struct intel_context
*intel
, GLenum internalFormat
)
56 struct intel_renderbuffer
*irb
;
58 DBG("%s %s\n", __FUNCTION__
,
59 _mesa_lookup_enum_by_nr(internalFormat
));
61 switch (internalFormat
) {
62 case GL_DEPTH_COMPONENT
:
63 case GL_DEPTH_COMPONENT16
:
64 irb
= intel_get_renderbuffer(intel
->ctx
.ReadBuffer
, BUFFER_DEPTH
);
65 if (irb
&& irb
->region
&& irb
->region
->cpp
== 2)
68 case GL_DEPTH24_STENCIL8_EXT
:
69 case GL_DEPTH_STENCIL_EXT
:
70 irb
= intel_get_renderbuffer(intel
->ctx
.ReadBuffer
, BUFFER_DEPTH
);
71 if (irb
&& irb
->region
&& irb
->region
->cpp
== 4)
76 return intel_readbuf_region(intel
);
78 if (intel
->ctx
.Visual
.rgbBits
== 16)
79 return intel_readbuf_region(intel
);
88 do_copy_texsubimage(struct intel_context
*intel
,
90 struct intel_texture_image
*intelImage
,
91 GLenum internalFormat
,
92 GLint dstx
, GLint dsty
,
93 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
95 GLcontext
*ctx
= &intel
->ctx
;
96 struct gl_texture_object
*texObj
= intelImage
->base
.TexObject
;
97 const struct intel_region
*src
=
98 get_teximage_source(intel
, internalFormat
);
100 if (!intelImage
->mt
|| !src
) {
101 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
102 fprintf(stderr
, "%s fail %p %p\n",
103 __FUNCTION__
, intelImage
->mt
, src
);
108 LOCK_HARDWARE(intel
);
110 GLuint image_offset
= intel_miptree_image_offset(intelImage
->mt
,
113 const GLint orig_x
= x
;
114 const GLint orig_y
= y
;
117 /* Update dst for clipped src. Need to also clip the source rect. */
121 /* Can't blit to tiled buffers with non-tile-aligned offset. */
122 if (intelImage
->mt
->region
->tiling
!= I915_TILING_NONE
&&
123 (image_offset
& 4095) != 0) {
124 UNLOCK_HARDWARE(intel
);
128 if (ctx
->ReadBuffer
->Name
== 0) {
129 /* reading from a window, adjust x, y */
130 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
131 y
= dPriv
->y
+ (dPriv
->h
- (y
+ height
));
134 /* Invert the data coming from the source rectangle due to GL
135 * and hardware disagreeing on where y=0 is.
137 * It appears that our offsets and pitches get mangled
138 * appropriately by the hardware, and we don't need to adjust them
141 src_pitch
= -src
->pitch
;
143 /* reading from a FBO, y is already oriented the way we like */
144 src_pitch
= src
->pitch
;
147 intelEmitCopyBlit(intel
,
153 intelImage
->mt
->pitch
,
154 intelImage
->mt
->region
->buffer
,
156 intelImage
->mt
->region
->tiling
,
157 x
, y
, dstx
, dsty
, width
, height
,
161 UNLOCK_HARDWARE(intel
);
163 /* GL_SGIS_generate_mipmap */
164 if (intelImage
->level
== texObj
->BaseLevel
&& texObj
->GenerateMipmap
) {
165 intel_generate_mipmap(ctx
, target
, texObj
);
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
=
178 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
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 _swrast_copy_teximage1d(ctx
, target
, level
, internalFormat
, x
, y
,
220 intelCopyTexImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
221 GLenum internalFormat
,
222 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
225 struct gl_texture_unit
*texUnit
=
226 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
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
, CHAN_TYPE
, 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 _swrast_copy_teximage2d(ctx
, target
, level
, internalFormat
, x
, y
,
263 width
, height
, border
);
268 intelCopyTexSubImage1D(GLcontext
* ctx
, GLenum target
, GLint level
,
269 GLint xoffset
, GLint x
, GLint y
, GLsizei width
)
271 struct gl_texture_unit
*texUnit
=
272 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
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 _swrast_copy_texsubimage1d(ctx
, target
, level
, xoffset
, x
, y
, width
);
293 intelCopyTexSubImage2D(GLcontext
* ctx
, GLenum target
, GLint level
,
294 GLint xoffset
, GLint yoffset
,
295 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
297 struct gl_texture_unit
*texUnit
=
298 &ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
];
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 DBG("%s - fallback to swrast\n", __FUNCTION__
);
315 _swrast_copy_texsubimage2d(ctx
, target
, level
,
316 xoffset
, yoffset
, x
, y
, width
, height
);
322 intelInitTextureCopyImageFuncs(struct dd_function_table
*functions
)
324 functions
->CopyTexImage1D
= intelCopyTexImage1D
;
325 functions
->CopyTexImage2D
= intelCopyTexImage2D
;
326 functions
->CopyTexSubImage1D
= intelCopyTexSubImage1D
;
327 functions
->CopyTexSubImage2D
= intelCopyTexSubImage2D
;