74f7f58bbe4c595dfe3f1a89a01c986b99a55cc3
[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 GLuint image_offset = intel_miptree_image_offset(intelImage->mt,
119 intelImage->face,
120 intelImage->level);
121 const GLint orig_x = x;
122 const GLint orig_y = y;
123 GLshort src_pitch;
124
125 /* Update dst for clipped src. Need to also clip the source rect. */
126 dstx += x - orig_x;
127 dsty += y - orig_y;
128
129 /* Can't blit to tiled buffers with non-tile-aligned offset. */
130 if (intelImage->mt->region->tiling != I915_TILING_NONE &&
131 (image_offset & 4095) != 0) {
132 UNLOCK_HARDWARE(intel);
133 return GL_FALSE;
134 }
135
136 if (ctx->ReadBuffer->Name == 0) {
137 /* reading from a window, adjust x, y */
138 __DRIdrawablePrivate *dPriv = intel->driDrawable;
139 y = dPriv->y + (dPriv->h - (y + height));
140 x += dPriv->x;
141
142 /* Invert the data coming from the source rectangle due to GL
143 * and hardware disagreeing on where y=0 is.
144 *
145 * It appears that our offsets and pitches get mangled
146 * appropriately by the hardware, and we don't need to adjust them
147 * on our own.
148 */
149 src_pitch = -src->pitch;
150 } else {
151 /* reading from a FBO, y is already oriented the way we like */
152 src_pitch = src->pitch;
153 }
154
155 if (!intelEmitCopyBlit(intel,
156 intelImage->mt->cpp,
157 src_pitch,
158 src->buffer,
159 0,
160 src->tiling,
161 intelImage->mt->pitch,
162 dst_bo,
163 image_offset,
164 intelImage->mt->region->tiling,
165 x, y, dstx, dsty, width, height,
166 GL_COPY)) {
167 UNLOCK_HARDWARE(intel);
168 return GL_FALSE;
169 }
170 }
171
172 UNLOCK_HARDWARE(intel);
173
174 /* GL_SGIS_generate_mipmap */
175 if (intelImage->level == texObj->BaseLevel && texObj->GenerateMipmap) {
176 intel_generate_mipmap(ctx, target, texObj);
177 }
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 =
189 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
190 struct gl_texture_object *texObj =
191 _mesa_select_tex_object(ctx, texUnit, target);
192 struct gl_texture_image *texImage =
193 _mesa_select_tex_image(ctx, texObj, target, level);
194 int srcx, srcy, dstx, dsty, height;
195
196 if (border)
197 goto fail;
198
199 /* Setup or redefine the texture object, mipmap tree and texture
200 * image. Don't populate yet.
201 */
202 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
203 width, border,
204 GL_RGBA, CHAN_TYPE, NULL,
205 &ctx->DefaultPacking, texObj, texImage);
206 srcx = x;
207 srcy = y;
208 dstx = 0;
209 dsty = 0;
210 height = 1;
211 if (!_mesa_clip_copytexsubimage(ctx,
212 &dstx, &dsty,
213 &srcx, &srcy,
214 &width, &height))
215 return;
216
217 if (!do_copy_texsubimage(intel_context(ctx), target,
218 intel_texture_image(texImage),
219 internalFormat, 0, 0, x, y, width, height))
220 goto fail;
221
222 return;
223
224 fail:
225 _swrast_copy_teximage1d(ctx, target, level, internalFormat, x, y,
226 width, border);
227 }
228
229
230 static void
231 intelCopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
232 GLenum internalFormat,
233 GLint x, GLint y, GLsizei width, GLsizei height,
234 GLint border)
235 {
236 struct gl_texture_unit *texUnit =
237 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
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, CHAN_TYPE, 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 _swrast_copy_teximage2d(ctx, target, level, internalFormat, x, y,
274 width, height, border);
275 }
276
277
278 static void
279 intelCopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
280 GLint xoffset, GLint x, GLint y, GLsizei width)
281 {
282 struct gl_texture_unit *texUnit =
283 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
284 struct gl_texture_object *texObj =
285 _mesa_select_tex_object(ctx, texUnit, target);
286 struct gl_texture_image *texImage =
287 _mesa_select_tex_image(ctx, texObj, target, level);
288 GLenum internalFormat = texImage->InternalFormat;
289
290 /* XXX need to check <border> as in above function? */
291
292 /* Need to check texture is compatible with source format.
293 */
294
295 if (!do_copy_texsubimage(intel_context(ctx), target,
296 intel_texture_image(texImage),
297 internalFormat, xoffset, 0, x, y, width, 1)) {
298 _swrast_copy_texsubimage1d(ctx, target, level, xoffset, x, y, width);
299 }
300 }
301
302
303 static void
304 intelCopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
305 GLint xoffset, GLint yoffset,
306 GLint x, GLint y, GLsizei width, GLsizei height)
307 {
308 struct gl_texture_unit *texUnit =
309 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
310 struct gl_texture_object *texObj =
311 _mesa_select_tex_object(ctx, texUnit, target);
312 struct gl_texture_image *texImage =
313 _mesa_select_tex_image(ctx, texObj, target, level);
314 GLenum internalFormat = texImage->InternalFormat;
315
316 /* Need to check texture is compatible with source format.
317 */
318
319 if (!do_copy_texsubimage(intel_context(ctx), target,
320 intel_texture_image(texImage),
321 internalFormat,
322 xoffset, yoffset, x, y, width, height)) {
323
324 DBG("%s - fallback to swrast\n", __FUNCTION__);
325
326 _swrast_copy_texsubimage2d(ctx, target, level,
327 xoffset, yoffset, x, y, width, height);
328 }
329 }
330
331
332 void
333 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
334 {
335 functions->CopyTexImage1D = intelCopyTexImage1D;
336 functions->CopyTexImage2D = intelCopyTexImage2D;
337 functions->CopyTexSubImage1D = intelCopyTexSubImage1D;
338 functions->CopyTexSubImage2D = intelCopyTexSubImage2D;
339 }