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