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