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 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 /* get dest x/y in destination texture */
124 intel_miptree_get_image_offset(intelImage->mt,
125 intelImage->level,
126 intelImage->face,
127 0,
128 &image_x, &image_y);
129 /* Update dst for clipped src. Need to also clip the source rect. */
130 dstx += x - orig_x;
131 dsty += y - orig_y;
132
133 /* Can't blit to tiled buffers with non-tile-aligned offset. */
134 if (intelImage->mt->region->tiling == I915_TILING_Y) {
135 UNLOCK_HARDWARE(intel);
136 return GL_FALSE;
137 }
138
139 if (ctx->ReadBuffer->Name == 0) {
140 /* reading from a window, adjust x, y */
141 const __DRIdrawablePrivate *dPriv = intel->driReadDrawable;
142 y = dPriv->y + (dPriv->h - (y + height));
143 x += dPriv->x;
144
145 /* Invert the data coming from the source rectangle due to GL
146 * and hardware disagreeing on where y=0 is.
147 *
148 * It appears that our offsets and pitches get mangled
149 * appropriately by the hardware, and we don't need to adjust them
150 * on our own.
151 */
152 src_pitch = -src->pitch;
153 } else {
154 /* reading from a FBO, y is already oriented the way we like */
155 src_pitch = src->pitch;
156 }
157
158 /* blit from src buffer to texture */
159 if (!intelEmitCopyBlit(intel,
160 intelImage->mt->cpp,
161 src_pitch,
162 src->buffer,
163 src->draw_offset,
164 src->tiling,
165 intelImage->mt->pitch,
166 dst_bo,
167 0,
168 intelImage->mt->region->tiling,
169 x, y, image_x + dstx, image_y + dsty,
170 width, height,
171 GL_COPY)) {
172 UNLOCK_HARDWARE(intel);
173 return GL_FALSE;
174 }
175 }
176
177 UNLOCK_HARDWARE(intel);
178
179 return GL_TRUE;
180 }
181
182
183 static void
184 intelCopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
185 GLenum internalFormat,
186 GLint x, GLint y, GLsizei width, GLint border)
187 {
188 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
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 if (INTEL_DEBUG & DEBUG_FALLBACKS)
225 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
226 _mesa_meta_CopyTexImage1D(ctx, target, level, internalFormat, x, y,
227 width, border);
228 }
229
230
231 static void
232 intelCopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
233 GLenum internalFormat,
234 GLint x, GLint y, GLsizei width, GLsizei height,
235 GLint border)
236 {
237 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
238 struct gl_texture_object *texObj =
239 _mesa_select_tex_object(ctx, texUnit, target);
240 struct gl_texture_image *texImage =
241 _mesa_select_tex_image(ctx, texObj, target, level);
242 int srcx, srcy, dstx, dsty;
243
244 if (border)
245 goto fail;
246
247 /* Setup or redefine the texture object, mipmap tree and texture
248 * image. Don't populate yet.
249 */
250 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
251 width, height, border,
252 GL_RGBA, GL_UNSIGNED_BYTE, NULL,
253 &ctx->DefaultPacking, texObj, texImage);
254
255 srcx = x;
256 srcy = y;
257 dstx = 0;
258 dsty = 0;
259 if (!_mesa_clip_copytexsubimage(ctx,
260 &dstx, &dsty,
261 &srcx, &srcy,
262 &width, &height))
263 return;
264
265 if (!do_copy_texsubimage(intel_context(ctx), target,
266 intel_texture_image(texImage),
267 internalFormat, 0, 0, x, y, width, height))
268 goto fail;
269
270 return;
271
272 fail:
273 if (INTEL_DEBUG & DEBUG_FALLBACKS)
274 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
275 _mesa_meta_CopyTexImage2D(ctx, target, level, internalFormat, x, y,
276 width, height, border);
277 }
278
279
280 static void
281 intelCopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
282 GLint xoffset, GLint x, GLint y, GLsizei width)
283 {
284 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
285 struct gl_texture_object *texObj =
286 _mesa_select_tex_object(ctx, texUnit, target);
287 struct gl_texture_image *texImage =
288 _mesa_select_tex_image(ctx, texObj, target, level);
289 GLenum internalFormat = texImage->InternalFormat;
290
291 /* XXX need to check <border> as in above function? */
292
293 /* Need to check texture is compatible with source format.
294 */
295
296 if (!do_copy_texsubimage(intel_context(ctx), target,
297 intel_texture_image(texImage),
298 internalFormat, xoffset, 0, x, y, width, 1)) {
299 if (INTEL_DEBUG & DEBUG_FALLBACKS)
300 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
301 _mesa_meta_CopyTexSubImage1D(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 = _mesa_get_current_tex_unit(ctx);
312 struct gl_texture_object *texObj =
313 _mesa_select_tex_object(ctx, texUnit, target);
314 struct gl_texture_image *texImage =
315 _mesa_select_tex_image(ctx, texObj, target, level);
316 GLenum internalFormat = texImage->InternalFormat;
317
318 /* Need to check texture is compatible with source format.
319 */
320
321 if (!do_copy_texsubimage(intel_context(ctx), target,
322 intel_texture_image(texImage),
323 internalFormat,
324 xoffset, yoffset, x, y, width, height)) {
325
326 if (INTEL_DEBUG & DEBUG_FALLBACKS)
327 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
328 _mesa_meta_CopyTexSubImage2D(ctx, target, level,
329 xoffset, yoffset, x, y, width, height);
330 }
331 }
332
333
334 void
335 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
336 {
337 functions->CopyTexImage1D = intelCopyTexImage1D;
338 functions->CopyTexImage2D = intelCopyTexImage2D;
339 functions->CopyTexSubImage1D = intelCopyTexSubImage1D;
340 functions->CopyTexSubImage2D = intelCopyTexSubImage2D;
341 }