Merge remote branch 'origin/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
34 #include "drivers/common/meta.h"
35
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"
45
46 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
47
48 /**
49 * Get the intel_region which is the source for any glCopyTex[Sub]Image call.
50 *
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.
53 */
54 static const struct intel_region *
55 get_teximage_source(struct intel_context *intel, GLenum internalFormat)
56 {
57 struct intel_renderbuffer *irb;
58
59 DBG("%s %s\n", __FUNCTION__,
60 _mesa_lookup_enum_by_nr(internalFormat));
61
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)
67 return irb->region;
68 return NULL;
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)
73 return irb->region;
74 return NULL;
75 case GL_RGBA:
76 case GL_RGBA8:
77 case GL_RGB:
78 case GL_RGB8:
79 return intel_readbuf_region(intel);
80 default:
81 return NULL;
82 }
83 }
84
85
86 static GLboolean
87 do_copy_texsubimage(struct intel_context *intel,
88 GLenum target,
89 struct intel_texture_image *intelImage,
90 GLenum internalFormat,
91 GLint dstx, GLint dsty,
92 GLint x, GLint y, GLsizei width, GLsizei height)
93 {
94 GLcontext *ctx = &intel->ctx;
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 0,
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 return GL_TRUE;
178 }
179
180
181 static void
182 intelCopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
183 GLenum internalFormat,
184 GLint x, GLint y, GLsizei width, GLint border)
185 {
186 struct gl_texture_unit *texUnit =
187 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
188 struct gl_texture_object *texObj =
189 _mesa_select_tex_object(ctx, texUnit, target);
190 struct gl_texture_image *texImage =
191 _mesa_select_tex_image(ctx, texObj, target, level);
192 int srcx, srcy, dstx, dsty, height;
193
194 if (border)
195 goto fail;
196
197 /* Setup or redefine the texture object, mipmap tree and texture
198 * image. Don't populate yet.
199 */
200 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
201 width, border,
202 GL_RGBA, CHAN_TYPE, NULL,
203 &ctx->DefaultPacking, texObj, texImage);
204 srcx = x;
205 srcy = y;
206 dstx = 0;
207 dsty = 0;
208 height = 1;
209 if (!_mesa_clip_copytexsubimage(ctx,
210 &dstx, &dsty,
211 &srcx, &srcy,
212 &width, &height))
213 return;
214
215 if (!do_copy_texsubimage(intel_context(ctx), target,
216 intel_texture_image(texImage),
217 internalFormat, 0, 0, x, y, width, height))
218 goto fail;
219
220 return;
221
222 fail:
223 _mesa_meta_CopyTexImage1D(ctx, target, level, internalFormat, x, y,
224 width, border);
225 }
226
227
228 static void
229 intelCopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
230 GLenum internalFormat,
231 GLint x, GLint y, GLsizei width, GLsizei height,
232 GLint border)
233 {
234 struct gl_texture_unit *texUnit =
235 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
236 struct gl_texture_object *texObj =
237 _mesa_select_tex_object(ctx, texUnit, target);
238 struct gl_texture_image *texImage =
239 _mesa_select_tex_image(ctx, texObj, target, level);
240 int srcx, srcy, dstx, dsty;
241
242 if (border)
243 goto fail;
244
245 /* Setup or redefine the texture object, mipmap tree and texture
246 * image. Don't populate yet.
247 */
248 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
249 width, height, border,
250 GL_RGBA, CHAN_TYPE, NULL,
251 &ctx->DefaultPacking, texObj, texImage);
252
253 srcx = x;
254 srcy = y;
255 dstx = 0;
256 dsty = 0;
257 if (!_mesa_clip_copytexsubimage(ctx,
258 &dstx, &dsty,
259 &srcx, &srcy,
260 &width, &height))
261 return;
262
263 if (!do_copy_texsubimage(intel_context(ctx), target,
264 intel_texture_image(texImage),
265 internalFormat, 0, 0, x, y, width, height))
266 goto fail;
267
268 return;
269
270 fail:
271 _mesa_meta_CopyTexImage2D(ctx, target, level, internalFormat, x, y,
272 width, height, border);
273 }
274
275
276 static void
277 intelCopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
278 GLint xoffset, GLint x, GLint y, GLsizei width)
279 {
280 struct gl_texture_unit *texUnit =
281 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
282 struct gl_texture_object *texObj =
283 _mesa_select_tex_object(ctx, texUnit, target);
284 struct gl_texture_image *texImage =
285 _mesa_select_tex_image(ctx, texObj, target, level);
286 GLenum internalFormat = texImage->InternalFormat;
287
288 /* XXX need to check <border> as in above function? */
289
290 /* Need to check texture is compatible with source format.
291 */
292
293 if (!do_copy_texsubimage(intel_context(ctx), target,
294 intel_texture_image(texImage),
295 internalFormat, xoffset, 0, x, y, width, 1)) {
296 _mesa_meta_CopyTexSubImage1D(ctx, target, level, xoffset, x, y, width);
297 }
298 }
299
300
301 static void
302 intelCopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
303 GLint xoffset, GLint yoffset,
304 GLint x, GLint y, GLsizei width, GLsizei height)
305 {
306 struct gl_texture_unit *texUnit =
307 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
308 struct gl_texture_object *texObj =
309 _mesa_select_tex_object(ctx, texUnit, target);
310 struct gl_texture_image *texImage =
311 _mesa_select_tex_image(ctx, texObj, target, level);
312 GLenum internalFormat = texImage->InternalFormat;
313
314 /* Need to check texture is compatible with source format.
315 */
316
317 if (!do_copy_texsubimage(intel_context(ctx), target,
318 intel_texture_image(texImage),
319 internalFormat,
320 xoffset, yoffset, x, y, width, height)) {
321
322 DBG("%s - fallback to _mesa_meta_CopyTexSubImage2D\n", __FUNCTION__);
323
324 _mesa_meta_CopyTexSubImage2D(ctx, target, level,
325 xoffset, yoffset, x, y, width, height);
326 }
327 }
328
329
330 void
331 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
332 {
333 functions->CopyTexImage1D = intelCopyTexImage1D;
334 functions->CopyTexImage2D = intelCopyTexImage2D;
335 functions->CopyTexSubImage1D = intelCopyTexSubImage1D;
336 functions->CopyTexSubImage2D = intelCopyTexSubImage2D;
337 }