intel: fix src offset bug in do_copy_texsubimage()
[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 case GL_RGB8:
78 return intel_readbuf_region(intel);
79 default:
80 return NULL;
81 }
82 }
83
84
85 static GLboolean
86 do_copy_texsubimage(struct intel_context *intel,
87 GLenum target,
88 struct intel_texture_image *intelImage,
89 GLenum internalFormat,
90 GLint dstx, GLint dsty,
91 GLint x, GLint y, GLsizei width, GLsizei height)
92 {
93 GLcontext *ctx = &intel->ctx;
94 struct gl_texture_object *texObj = intelImage->base.TexObject;
95 const struct intel_region *src =
96 get_teximage_source(intel, internalFormat);
97
98 if (!intelImage->mt || !src) {
99 if (INTEL_DEBUG & DEBUG_FALLBACKS)
100 fprintf(stderr, "%s fail %p %p (0x%08x)\n",
101 __FUNCTION__, intelImage->mt, src, internalFormat);
102 return GL_FALSE;
103 }
104
105 if (intelImage->mt->cpp != src->cpp) {
106 if (INTEL_DEBUG & DEBUG_FALLBACKS)
107 fprintf(stderr, "%s fail %d vs %d cpp\n",
108 __FUNCTION__, intelImage->mt->cpp, src->cpp);
109 return GL_FALSE;
110 }
111
112 intelFlush(ctx);
113 LOCK_HARDWARE(intel);
114 {
115 drm_intel_bo *dst_bo = intel_region_buffer(intel,
116 intelImage->mt->region,
117 INTEL_WRITE_PART);
118 const GLint orig_x = x;
119 const GLint orig_y = y;
120 GLuint image_x, image_y;
121 GLshort src_pitch;
122
123 intel_miptree_get_image_offset(intelImage->mt,
124 intelImage->level,
125 intelImage->face,
126 0,
127 &image_x, &image_y);
128 /* Update dst for clipped src. Need to also clip the source rect. */
129 dstx += x - orig_x;
130 dsty += y - orig_y;
131
132 /* Can't blit to tiled buffers with non-tile-aligned offset. */
133 if (intelImage->mt->region->tiling == I915_TILING_Y) {
134 UNLOCK_HARDWARE(intel);
135 return GL_FALSE;
136 }
137
138 if (ctx->ReadBuffer->Name == 0) {
139 /* reading from a window, adjust x, y */
140 const __DRIdrawablePrivate *dPriv = intel->driReadDrawable;
141 y = dPriv->y + (dPriv->h - (y + height));
142 x += dPriv->x;
143
144 /* Invert the data coming from the source rectangle due to GL
145 * and hardware disagreeing on where y=0 is.
146 *
147 * It appears that our offsets and pitches get mangled
148 * appropriately by the hardware, and we don't need to adjust them
149 * on our own.
150 */
151 src_pitch = -src->pitch;
152 } else {
153 /* reading from a FBO, y is already oriented the way we like */
154 src_pitch = src->pitch;
155 }
156
157 if (!intelEmitCopyBlit(intel,
158 intelImage->mt->cpp,
159 src_pitch,
160 src->buffer,
161 src->draw_offset,
162 src->tiling,
163 intelImage->mt->pitch,
164 dst_bo,
165 0,
166 intelImage->mt->region->tiling,
167 x, y, image_x + dstx, image_y + dsty,
168 width, height,
169 GL_COPY)) {
170 UNLOCK_HARDWARE(intel);
171 return GL_FALSE;
172 }
173 }
174
175 UNLOCK_HARDWARE(intel);
176
177 /* GL_SGIS_generate_mipmap */
178 if (intelImage->level == texObj->BaseLevel && texObj->GenerateMipmap) {
179 intel_generate_mipmap(ctx, target, texObj);
180 }
181
182 return GL_TRUE;
183 }
184
185
186 static void
187 intelCopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
188 GLenum internalFormat,
189 GLint x, GLint y, GLsizei width, GLint border)
190 {
191 struct gl_texture_unit *texUnit =
192 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
193 struct gl_texture_object *texObj =
194 _mesa_select_tex_object(ctx, texUnit, target);
195 struct gl_texture_image *texImage =
196 _mesa_select_tex_image(ctx, texObj, target, level);
197 int srcx, srcy, dstx, dsty, height;
198
199 if (border)
200 goto fail;
201
202 /* Setup or redefine the texture object, mipmap tree and texture
203 * image. Don't populate yet.
204 */
205 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
206 width, border,
207 GL_RGBA, CHAN_TYPE, NULL,
208 &ctx->DefaultPacking, texObj, texImage);
209 srcx = x;
210 srcy = y;
211 dstx = 0;
212 dsty = 0;
213 height = 1;
214 if (!_mesa_clip_copytexsubimage(ctx,
215 &dstx, &dsty,
216 &srcx, &srcy,
217 &width, &height))
218 return;
219
220 if (!do_copy_texsubimage(intel_context(ctx), target,
221 intel_texture_image(texImage),
222 internalFormat, 0, 0, x, y, width, height))
223 goto fail;
224
225 return;
226
227 fail:
228 _swrast_copy_teximage1d(ctx, target, level, internalFormat, x, y,
229 width, border);
230 }
231
232
233 static void
234 intelCopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
235 GLenum internalFormat,
236 GLint x, GLint y, GLsizei width, GLsizei height,
237 GLint border)
238 {
239 struct gl_texture_unit *texUnit =
240 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
241 struct gl_texture_object *texObj =
242 _mesa_select_tex_object(ctx, texUnit, target);
243 struct gl_texture_image *texImage =
244 _mesa_select_tex_image(ctx, texObj, target, level);
245 int srcx, srcy, dstx, dsty;
246
247 if (border)
248 goto fail;
249
250 /* Setup or redefine the texture object, mipmap tree and texture
251 * image. Don't populate yet.
252 */
253 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
254 width, height, border,
255 GL_RGBA, CHAN_TYPE, NULL,
256 &ctx->DefaultPacking, texObj, texImage);
257
258 srcx = x;
259 srcy = y;
260 dstx = 0;
261 dsty = 0;
262 if (!_mesa_clip_copytexsubimage(ctx,
263 &dstx, &dsty,
264 &srcx, &srcy,
265 &width, &height))
266 return;
267
268 if (!do_copy_texsubimage(intel_context(ctx), target,
269 intel_texture_image(texImage),
270 internalFormat, 0, 0, x, y, width, height))
271 goto fail;
272
273 return;
274
275 fail:
276 _swrast_copy_teximage2d(ctx, target, level, internalFormat, x, y,
277 width, height, border);
278 }
279
280
281 static void
282 intelCopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
283 GLint xoffset, GLint x, GLint y, GLsizei width)
284 {
285 struct gl_texture_unit *texUnit =
286 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
287 struct gl_texture_object *texObj =
288 _mesa_select_tex_object(ctx, texUnit, target);
289 struct gl_texture_image *texImage =
290 _mesa_select_tex_image(ctx, texObj, target, level);
291 GLenum internalFormat = texImage->InternalFormat;
292
293 /* XXX need to check <border> as in above function? */
294
295 /* Need to check texture is compatible with source format.
296 */
297
298 if (!do_copy_texsubimage(intel_context(ctx), target,
299 intel_texture_image(texImage),
300 internalFormat, xoffset, 0, x, y, width, 1)) {
301 _swrast_copy_texsubimage1d(ctx, target, level, xoffset, x, y, width);
302 }
303 }
304
305
306 static void
307 intelCopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
308 GLint xoffset, GLint yoffset,
309 GLint x, GLint y, GLsizei width, GLsizei height)
310 {
311 struct gl_texture_unit *texUnit =
312 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
313 struct gl_texture_object *texObj =
314 _mesa_select_tex_object(ctx, texUnit, target);
315 struct gl_texture_image *texImage =
316 _mesa_select_tex_image(ctx, texObj, target, level);
317 GLenum internalFormat = texImage->InternalFormat;
318
319 /* Need to check texture is compatible with source format.
320 */
321
322 if (!do_copy_texsubimage(intel_context(ctx), target,
323 intel_texture_image(texImage),
324 internalFormat,
325 xoffset, yoffset, x, y, width, height)) {
326
327 DBG("%s - fallback to swrast\n", __FUNCTION__);
328
329 _swrast_copy_texsubimage2d(ctx, target, level,
330 xoffset, yoffset, x, y, width, height);
331 }
332 }
333
334
335 void
336 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
337 {
338 functions->CopyTexImage1D = intelCopyTexImage1D;
339 functions->CopyTexImage2D = intelCopyTexImage2D;
340 functions->CopyTexSubImage1D = intelCopyTexSubImage1D;
341 functions->CopyTexSubImage2D = intelCopyTexSubImage2D;
342 }