Merge branch 'mesa_7_7_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/texstate.h"
33 #include "main/mipmap.h"
34
35 #include "drivers/common/meta.h"
36
37 #include "intel_screen.h"
38 #include "intel_context.h"
39 #include "intel_batchbuffer.h"
40 #include "intel_buffers.h"
41 #include "intel_mipmap_tree.h"
42 #include "intel_regions.h"
43 #include "intel_fbo.h"
44 #include "intel_tex.h"
45 #include "intel_blit.h"
46
47 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
48
49 /**
50 * Get the intel_region which is the source for any glCopyTex[Sub]Image call.
51 *
52 * Do the best we can using the blitter. A future project is to use
53 * the texture engine and fragment programs for these copies.
54 */
55 static const struct intel_region *
56 get_teximage_source(struct intel_context *intel, GLenum internalFormat)
57 {
58 struct intel_renderbuffer *irb;
59
60 DBG("%s %s\n", __FUNCTION__,
61 _mesa_lookup_enum_by_nr(internalFormat));
62
63 switch (internalFormat) {
64 case GL_DEPTH_COMPONENT:
65 case GL_DEPTH_COMPONENT16:
66 irb = intel_get_renderbuffer(intel->ctx.ReadBuffer, BUFFER_DEPTH);
67 if (irb && irb->region && irb->region->cpp == 2)
68 return irb->region;
69 return NULL;
70 case GL_DEPTH24_STENCIL8_EXT:
71 case GL_DEPTH_STENCIL_EXT:
72 irb = intel_get_renderbuffer(intel->ctx.ReadBuffer, BUFFER_DEPTH);
73 if (irb && irb->region && irb->region->cpp == 4)
74 return irb->region;
75 return NULL;
76 case GL_RGBA:
77 case GL_RGBA8:
78 case GL_RGB:
79 case GL_RGB8:
80 return intel_readbuf_region(intel);
81 default:
82 return NULL;
83 }
84 }
85
86
87 static GLboolean
88 do_copy_texsubimage(struct intel_context *intel,
89 GLenum target,
90 struct intel_texture_image *intelImage,
91 GLenum internalFormat,
92 GLint dstx, GLint dsty,
93 GLint x, GLint y, GLsizei width, GLsizei height)
94 {
95 GLcontext *ctx = &intel->ctx;
96 const struct intel_region *src = 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 {
114 drm_intel_bo *dst_bo = intel_region_buffer(intel,
115 intelImage->mt->region,
116 INTEL_WRITE_PART);
117 const GLint orig_x = x;
118 const GLint orig_y = y;
119 GLuint image_x, image_y;
120 GLshort src_pitch;
121
122 /* get dest x/y in destination texture */
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 return GL_FALSE;
135 }
136
137 if (ctx->ReadBuffer->Name == 0) {
138 /* reading from a window, adjust x, y */
139 const __DRIdrawable *dPriv = intel->driReadDrawable;
140 y = dPriv->y + (dPriv->h - (y + height));
141 x += dPriv->x;
142
143 /* Invert the data coming from the source rectangle due to GL
144 * and hardware disagreeing on where y=0 is.
145 *
146 * It appears that our offsets and pitches get mangled
147 * appropriately by the hardware, and we don't need to adjust them
148 * on our own.
149 */
150 src_pitch = -src->pitch;
151 } else {
152 /* reading from a FBO, y is already oriented the way we like */
153 src_pitch = src->pitch;
154 }
155
156 /* blit from src buffer to texture */
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 src->draw_x + x, src->draw_y + y,
168 image_x + dstx, image_y + dsty,
169 width, height,
170 GL_COPY)) {
171 return GL_FALSE;
172 }
173 }
174
175 return GL_TRUE;
176 }
177
178
179 static void
180 intelCopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
181 GLenum internalFormat,
182 GLint x, GLint y, GLsizei width, GLint border)
183 {
184 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
185 struct gl_texture_object *texObj =
186 _mesa_select_tex_object(ctx, texUnit, target);
187 struct gl_texture_image *texImage =
188 _mesa_select_tex_image(ctx, texObj, target, level);
189 int srcx, srcy, dstx, dsty, height;
190
191 if (border)
192 goto fail;
193
194 /* Setup or redefine the texture object, mipmap tree and texture
195 * image. Don't populate yet.
196 */
197 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
198 width, border,
199 GL_RGBA, CHAN_TYPE, NULL,
200 &ctx->DefaultPacking, texObj, texImage);
201 srcx = x;
202 srcy = y;
203 dstx = 0;
204 dsty = 0;
205 height = 1;
206 if (!_mesa_clip_copytexsubimage(ctx,
207 &dstx, &dsty,
208 &srcx, &srcy,
209 &width, &height))
210 return;
211
212 if (!do_copy_texsubimage(intel_context(ctx), target,
213 intel_texture_image(texImage),
214 internalFormat, 0, 0, x, y, width, height))
215 goto fail;
216
217 return;
218
219 fail:
220 if (INTEL_DEBUG & DEBUG_FALLBACKS)
221 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
222 _mesa_meta_CopyTexImage1D(ctx, target, level, internalFormat, x, y,
223 width, border);
224 }
225
226
227 static void
228 intelCopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
229 GLenum internalFormat,
230 GLint x, GLint y, GLsizei width, GLsizei height,
231 GLint border)
232 {
233 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
234 struct gl_texture_object *texObj =
235 _mesa_select_tex_object(ctx, texUnit, target);
236 struct gl_texture_image *texImage =
237 _mesa_select_tex_image(ctx, texObj, target, level);
238 int srcx, srcy, dstx, dsty;
239
240 if (border)
241 goto fail;
242
243 /* Setup or redefine the texture object, mipmap tree and texture
244 * image. Don't populate yet.
245 */
246 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
247 width, height, border,
248 GL_RGBA, GL_UNSIGNED_BYTE, NULL,
249 &ctx->DefaultPacking, texObj, texImage);
250
251 srcx = x;
252 srcy = y;
253 dstx = 0;
254 dsty = 0;
255 if (!_mesa_clip_copytexsubimage(ctx,
256 &dstx, &dsty,
257 &srcx, &srcy,
258 &width, &height))
259 return;
260
261 if (!do_copy_texsubimage(intel_context(ctx), target,
262 intel_texture_image(texImage),
263 internalFormat, 0, 0, x, y, width, height))
264 goto fail;
265
266 return;
267
268 fail:
269 if (INTEL_DEBUG & DEBUG_FALLBACKS)
270 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
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 = _mesa_get_current_tex_unit(ctx);
281 struct gl_texture_object *texObj =
282 _mesa_select_tex_object(ctx, texUnit, target);
283 struct gl_texture_image *texImage =
284 _mesa_select_tex_image(ctx, texObj, target, level);
285 GLenum internalFormat = texImage->InternalFormat;
286
287 /* XXX need to check <border> as in above function? */
288
289 /* Need to check texture is compatible with source format.
290 */
291
292 if (!do_copy_texsubimage(intel_context(ctx), target,
293 intel_texture_image(texImage),
294 internalFormat, xoffset, 0, x, y, width, 1)) {
295 if (INTEL_DEBUG & DEBUG_FALLBACKS)
296 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
297 _mesa_meta_CopyTexSubImage1D(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 = _mesa_get_current_tex_unit(ctx);
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 if (INTEL_DEBUG & DEBUG_FALLBACKS)
323 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
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 }