Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[mesa.git] / src / mesa / drivers / dri / intel / intel_tex_copy.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
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"
34
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"
44
45 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
46
47 /**
48 * Get the intel_region which is the source for any glCopyTex[Sub]Image call.
49 *
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.
52 */
53 static const struct intel_region *
54 get_teximage_source(struct intel_context *intel, GLenum internalFormat)
55 {
56 struct intel_renderbuffer *irb;
57
58 DBG("%s %s\n", __FUNCTION__,
59 _mesa_lookup_enum_by_nr(internalFormat));
60
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)
66 return irb->region;
67 return NULL;
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)
72 return irb->region;
73 return NULL;
74 case GL_RGBA:
75 case GL_RGBA8:
76 case GL_RGB:
77 return intel_readbuf_region(intel);
78 default:
79 return NULL;
80 }
81 }
82
83
84 static GLboolean
85 do_copy_texsubimage(struct intel_context *intel,
86 GLenum target,
87 struct intel_texture_image *intelImage,
88 GLenum internalFormat,
89 GLint dstx, GLint dsty,
90 GLint x, GLint y, GLsizei width, GLsizei height)
91 {
92 GLcontext *ctx = &intel->ctx;
93 struct gl_texture_object *texObj = intelImage->base.TexObject;
94 const struct intel_region *src =
95 get_teximage_source(intel, internalFormat);
96
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);
101 return GL_FALSE;
102 }
103
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);
108 return GL_FALSE;
109 }
110
111 intelFlush(ctx);
112 LOCK_HARDWARE(intel);
113 {
114 drm_intel_bo *dst_bo = intel_region_buffer(intel,
115 intelImage->mt->region,
116 INTEL_WRITE_PART);
117 GLuint image_offset = intel_miptree_image_offset(intelImage->mt,
118 intelImage->face,
119 intelImage->level);
120 const GLint orig_x = x;
121 const GLint orig_y = y;
122 GLshort src_pitch;
123
124 /* Update dst for clipped src. Need to also clip the source rect. */
125 dstx += x - orig_x;
126 dsty += y - orig_y;
127
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);
132 return GL_FALSE;
133 }
134
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));
139 x += dPriv->x;
140
141 /* Invert the data coming from the source rectangle due to GL
142 * and hardware disagreeing on where y=0 is.
143 *
144 * It appears that our offsets and pitches get mangled
145 * appropriately by the hardware, and we don't need to adjust them
146 * on our own.
147 */
148 src_pitch = -src->pitch;
149 } else {
150 /* reading from a FBO, y is already oriented the way we like */
151 src_pitch = src->pitch;
152 }
153
154 if (!intelEmitCopyBlit(intel,
155 intelImage->mt->cpp,
156 src_pitch,
157 src->buffer,
158 0,
159 src->tiling,
160 intelImage->mt->pitch,
161 dst_bo,
162 image_offset,
163 intelImage->mt->region->tiling,
164 x, y, dstx, dsty, width, height,
165 GL_COPY)) {
166 UNLOCK_HARDWARE(intel);
167 return GL_FALSE;
168 }
169 }
170
171 UNLOCK_HARDWARE(intel);
172
173 /* GL_SGIS_generate_mipmap */
174 if (intelImage->level == texObj->BaseLevel && texObj->GenerateMipmap) {
175 intel_generate_mipmap(ctx, target, texObj);
176 }
177
178 return GL_TRUE;
179 }
180
181
182 static void
183 intelCopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
184 GLenum internalFormat,
185 GLint x, GLint y, GLsizei width, GLint border)
186 {
187 struct gl_texture_unit *texUnit =
188 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
189 struct gl_texture_object *texObj =
190 _mesa_select_tex_object(ctx, texUnit, target);
191 struct gl_texture_image *texImage =
192 _mesa_select_tex_image(ctx, texObj, target, level);
193 int srcx, srcy, dstx, dsty, height;
194
195 if (border)
196 goto fail;
197
198 /* Setup or redefine the texture object, mipmap tree and texture
199 * image. Don't populate yet.
200 */
201 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
202 width, border,
203 GL_RGBA, CHAN_TYPE, NULL,
204 &ctx->DefaultPacking, texObj, texImage);
205 srcx = x;
206 srcy = y;
207 dstx = 0;
208 dsty = 0;
209 height = 1;
210 if (!_mesa_clip_copytexsubimage(ctx,
211 &dstx, &dsty,
212 &srcx, &srcy,
213 &width, &height))
214 return;
215
216 if (!do_copy_texsubimage(intel_context(ctx), target,
217 intel_texture_image(texImage),
218 internalFormat, 0, 0, x, y, width, height))
219 goto fail;
220
221 return;
222
223 fail:
224 _swrast_copy_teximage1d(ctx, target, level, internalFormat, x, y,
225 width, border);
226 }
227
228
229 static void
230 intelCopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
231 GLenum internalFormat,
232 GLint x, GLint y, GLsizei width, GLsizei height,
233 GLint border)
234 {
235 struct gl_texture_unit *texUnit =
236 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
237 struct gl_texture_object *texObj =
238 _mesa_select_tex_object(ctx, texUnit, target);
239 struct gl_texture_image *texImage =
240 _mesa_select_tex_image(ctx, texObj, target, level);
241 int srcx, srcy, dstx, dsty;
242
243 if (border)
244 goto fail;
245
246 /* Setup or redefine the texture object, mipmap tree and texture
247 * image. Don't populate yet.
248 */
249 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
250 width, height, border,
251 GL_RGBA, CHAN_TYPE, NULL,
252 &ctx->DefaultPacking, texObj, texImage);
253
254 srcx = x;
255 srcy = y;
256 dstx = 0;
257 dsty = 0;
258 if (!_mesa_clip_copytexsubimage(ctx,
259 &dstx, &dsty,
260 &srcx, &srcy,
261 &width, &height))
262 return;
263
264 if (!do_copy_texsubimage(intel_context(ctx), target,
265 intel_texture_image(texImage),
266 internalFormat, 0, 0, x, y, width, height))
267 goto fail;
268
269 return;
270
271 fail:
272 _swrast_copy_teximage2d(ctx, target, level, internalFormat, x, y,
273 width, height, border);
274 }
275
276
277 static void
278 intelCopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
279 GLint xoffset, GLint x, GLint y, GLsizei width)
280 {
281 struct gl_texture_unit *texUnit =
282 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
283 struct gl_texture_object *texObj =
284 _mesa_select_tex_object(ctx, texUnit, target);
285 struct gl_texture_image *texImage =
286 _mesa_select_tex_image(ctx, texObj, target, level);
287 GLenum internalFormat = texImage->InternalFormat;
288
289 /* XXX need to check <border> as in above function? */
290
291 /* Need to check texture is compatible with source format.
292 */
293
294 if (!do_copy_texsubimage(intel_context(ctx), target,
295 intel_texture_image(texImage),
296 internalFormat, xoffset, 0, x, y, width, 1)) {
297 _swrast_copy_texsubimage1d(ctx, target, level, xoffset, x, y, width);
298 }
299 }
300
301
302 static void
303 intelCopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
304 GLint xoffset, GLint yoffset,
305 GLint x, GLint y, GLsizei width, GLsizei height)
306 {
307 struct gl_texture_unit *texUnit =
308 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
309 struct gl_texture_object *texObj =
310 _mesa_select_tex_object(ctx, texUnit, target);
311 struct gl_texture_image *texImage =
312 _mesa_select_tex_image(ctx, texObj, target, level);
313 GLenum internalFormat = texImage->InternalFormat;
314
315 /* Need to check texture is compatible with source format.
316 */
317
318 if (!do_copy_texsubimage(intel_context(ctx), target,
319 intel_texture_image(texImage),
320 internalFormat,
321 xoffset, yoffset, x, y, width, height)) {
322
323 DBG("%s - fallback to swrast\n", __FUNCTION__);
324
325 _swrast_copy_texsubimage2d(ctx, target, level,
326 xoffset, yoffset, x, y, width, height);
327 }
328 }
329
330
331 void
332 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
333 {
334 functions->CopyTexImage1D = intelCopyTexImage1D;
335 functions->CopyTexImage2D = intelCopyTexImage2D;
336 functions->CopyTexSubImage1D = intelCopyTexSubImage1D;
337 functions->CopyTexSubImage2D = intelCopyTexSubImage2D;
338 }