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