Merge remote branch 'origin/master' into lp-setup-llvm
[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_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 4:
76 case GL_RGBA:
77 case GL_RGBA8:
78 irb = intel_renderbuffer(intel->ctx.ReadBuffer->_ColorReadBuffer);
79 /* We're required to set alpha to 1.0 in this case, but we can't
80 * do that with the blitter, so fall back. We could use the 3D
81 * engine or do two passes with the blitter, but it doesn't seem
82 * worth it for this case. */
83 if (irb->Base._BaseFormat == GL_RGB)
84 return NULL;
85 return irb->region;
86 case 3:
87 case GL_RGB:
88 case GL_RGB8:
89 return intel_readbuf_region(intel);
90 default:
91 return NULL;
92 }
93 }
94
95
96 static GLboolean
97 do_copy_texsubimage(struct intel_context *intel,
98 GLenum target,
99 struct intel_texture_image *intelImage,
100 GLenum internalFormat,
101 GLint dstx, GLint dsty,
102 GLint x, GLint y, GLsizei width, GLsizei height)
103 {
104 struct gl_context *ctx = &intel->ctx;
105 const struct intel_region *src = get_teximage_source(intel, internalFormat);
106
107 if (!intelImage->mt || !src || !src->buffer) {
108 if (INTEL_DEBUG & DEBUG_FALLBACKS)
109 fprintf(stderr, "%s fail %p %p (0x%08x)\n",
110 __FUNCTION__, intelImage->mt, src, internalFormat);
111 return GL_FALSE;
112 }
113
114 if (intelImage->mt->cpp != src->cpp) {
115 if (INTEL_DEBUG & DEBUG_FALLBACKS)
116 fprintf(stderr, "%s fail %d vs %d cpp\n",
117 __FUNCTION__, intelImage->mt->cpp, src->cpp);
118 return GL_FALSE;
119 }
120
121 /* intel_flush(ctx); */
122 intel_prepare_render(intel);
123 {
124 drm_intel_bo *dst_bo = intel_region_buffer(intel,
125 intelImage->mt->region,
126 INTEL_WRITE_PART);
127 GLuint image_x, image_y;
128 GLshort src_pitch;
129
130 /* get dest x/y in destination texture */
131 intel_miptree_get_image_offset(intelImage->mt,
132 intelImage->level,
133 intelImage->face,
134 0,
135 &image_x, &image_y);
136
137 /* The blitter can't handle Y-tiled buffers. */
138 if (intelImage->mt->region->tiling == I915_TILING_Y) {
139 return GL_FALSE;
140 }
141
142 if (ctx->ReadBuffer->Name == 0) {
143 /* Flip vertical orientation for system framebuffers */
144 y = ctx->ReadBuffer->Height - (y + height);
145 src_pitch = -src->pitch;
146 } else {
147 /* reading from a FBO, y is already oriented the way we like */
148 src_pitch = src->pitch;
149 }
150
151 /* blit from src buffer to texture */
152 if (!intelEmitCopyBlit(intel,
153 intelImage->mt->cpp,
154 src_pitch,
155 src->buffer,
156 0,
157 src->tiling,
158 intelImage->mt->region->pitch,
159 dst_bo,
160 0,
161 intelImage->mt->region->tiling,
162 src->draw_x + x, src->draw_y + y,
163 image_x + dstx, image_y + dsty,
164 width, height,
165 GL_COPY)) {
166 return GL_FALSE;
167 }
168 }
169
170 return GL_TRUE;
171 }
172
173
174 static void
175 intelCopyTexImage1D(struct gl_context * ctx, GLenum target, GLint level,
176 GLenum internalFormat,
177 GLint x, GLint y, GLsizei width, GLint border)
178 {
179 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
180 struct gl_texture_object *texObj =
181 _mesa_select_tex_object(ctx, texUnit, target);
182 struct gl_texture_image *texImage =
183 _mesa_select_tex_image(ctx, texObj, target, level);
184 int srcx, srcy, dstx, dsty, height;
185
186 if (border)
187 goto fail;
188
189 /* Setup or redefine the texture object, mipmap tree and texture
190 * image. Don't populate yet.
191 */
192 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
193 width, border,
194 GL_RGBA, CHAN_TYPE, NULL,
195 &ctx->DefaultPacking, texObj, texImage);
196 srcx = x;
197 srcy = y;
198 dstx = 0;
199 dsty = 0;
200 height = 1;
201 if (!_mesa_clip_copytexsubimage(ctx,
202 &dstx, &dsty,
203 &srcx, &srcy,
204 &width, &height))
205 return;
206
207 if (!do_copy_texsubimage(intel_context(ctx), target,
208 intel_texture_image(texImage),
209 internalFormat, 0, 0, x, y, width, height))
210 goto fail;
211
212 return;
213
214 fail:
215 if (INTEL_DEBUG & DEBUG_FALLBACKS)
216 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
217 _mesa_meta_CopyTexImage1D(ctx, target, level, internalFormat, x, y,
218 width, border);
219 }
220
221
222 static void
223 intelCopyTexImage2D(struct gl_context * ctx, GLenum target, GLint level,
224 GLenum internalFormat,
225 GLint x, GLint y, GLsizei width, GLsizei height,
226 GLint border)
227 {
228 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
229 struct gl_texture_object *texObj =
230 _mesa_select_tex_object(ctx, texUnit, target);
231 struct gl_texture_image *texImage =
232 _mesa_select_tex_image(ctx, texObj, target, level);
233 int srcx, srcy, dstx, dsty;
234
235 if (border)
236 goto fail;
237
238 /* Setup or redefine the texture object, mipmap tree and texture
239 * image. Don't populate yet.
240 */
241 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
242 width, height, border,
243 GL_RGBA, GL_UNSIGNED_BYTE, NULL,
244 &ctx->DefaultPacking, texObj, texImage);
245
246 srcx = x;
247 srcy = y;
248 dstx = 0;
249 dsty = 0;
250 if (!_mesa_clip_copytexsubimage(ctx,
251 &dstx, &dsty,
252 &srcx, &srcy,
253 &width, &height))
254 return;
255
256 if (!do_copy_texsubimage(intel_context(ctx), target,
257 intel_texture_image(texImage),
258 internalFormat, 0, 0, x, y, width, height))
259 goto fail;
260
261 return;
262
263 fail:
264 if (INTEL_DEBUG & DEBUG_FALLBACKS)
265 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
266 _mesa_meta_CopyTexImage2D(ctx, target, level, internalFormat, x, y,
267 width, height, border);
268 }
269
270
271 static void
272 intelCopyTexSubImage1D(struct gl_context * ctx, GLenum target, GLint level,
273 GLint xoffset, GLint x, GLint y, GLsizei width)
274 {
275 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
276 struct gl_texture_object *texObj =
277 _mesa_select_tex_object(ctx, texUnit, target);
278 struct gl_texture_image *texImage =
279 _mesa_select_tex_image(ctx, texObj, target, level);
280 GLenum internalFormat = texImage->InternalFormat;
281
282 /* XXX need to check <border> as in above function? */
283
284 /* Need to check texture is compatible with source format.
285 */
286
287 if (!do_copy_texsubimage(intel_context(ctx), target,
288 intel_texture_image(texImage),
289 internalFormat, xoffset, 0, x, y, width, 1)) {
290 if (INTEL_DEBUG & DEBUG_FALLBACKS)
291 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
292 _mesa_meta_CopyTexSubImage1D(ctx, target, level, xoffset, x, y, width);
293 }
294 }
295
296
297 static void
298 intelCopyTexSubImage2D(struct gl_context * ctx, GLenum target, GLint level,
299 GLint xoffset, GLint yoffset,
300 GLint x, GLint y, GLsizei width, GLsizei height)
301 {
302 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
303 struct gl_texture_object *texObj =
304 _mesa_select_tex_object(ctx, texUnit, target);
305 struct gl_texture_image *texImage =
306 _mesa_select_tex_image(ctx, texObj, target, level);
307 GLenum internalFormat = texImage->InternalFormat;
308
309 /* Need to check texture is compatible with source format.
310 */
311
312 if (!do_copy_texsubimage(intel_context(ctx), target,
313 intel_texture_image(texImage),
314 internalFormat,
315 xoffset, yoffset, x, y, width, height)) {
316
317 if (INTEL_DEBUG & DEBUG_FALLBACKS)
318 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
319 _mesa_meta_CopyTexSubImage2D(ctx, target, level,
320 xoffset, yoffset, x, y, width, height);
321 }
322 }
323
324
325 void
326 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
327 {
328 functions->CopyTexImage1D = intelCopyTexImage1D;
329 functions->CopyTexImage2D = intelCopyTexImage2D;
330 functions->CopyTexSubImage1D = intelCopyTexSubImage1D;
331 functions->CopyTexSubImage2D = intelCopyTexSubImage2D;
332 }