Merge branch 'radeon-texrewrite-clean' into 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 _mesa_meta_CopyTexImage1D(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 = _mesa_get_current_tex_unit(ctx);
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, GL_UNSIGNED_BYTE, 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 = _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 _mesa_meta_CopyTexSubImage1D(ctx, target, level, xoffset, x, y, width);
296 }
297 }
298
299
300 static void
301 intelCopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
302 GLint xoffset, GLint yoffset,
303 GLint x, GLint y, GLsizei width, GLsizei height)
304 {
305 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
306 struct gl_texture_object *texObj =
307 _mesa_select_tex_object(ctx, texUnit, target);
308 struct gl_texture_image *texImage =
309 _mesa_select_tex_image(ctx, texObj, target, level);
310 GLenum internalFormat = texImage->InternalFormat;
311
312 /* Need to check texture is compatible with source format.
313 */
314
315 if (!do_copy_texsubimage(intel_context(ctx), target,
316 intel_texture_image(texImage),
317 internalFormat,
318 xoffset, yoffset, x, y, width, height)) {
319
320 DBG("%s - fallback to _mesa_meta_CopyTexSubImage2D\n", __FUNCTION__);
321
322 _mesa_meta_CopyTexSubImage2D(ctx, target, level,
323 xoffset, yoffset, x, y, width, height);
324 }
325 }
326
327
328 void
329 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
330 {
331 functions->CopyTexImage1D = intelCopyTexImage1D;
332 functions->CopyTexImage2D = intelCopyTexImage2D;
333 functions->CopyTexSubImage1D = intelCopyTexSubImage1D;
334 functions->CopyTexSubImage2D = intelCopyTexSubImage2D;
335 }