a40011ab40c396aad71a2873a2cb62ae2e4f4e0b
[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_mipmap_tree.h"
39 #include "intel_regions.h"
40 #include "intel_fbo.h"
41 #include "intel_tex.h"
42 #include "intel_blit.h"
43
44 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
45
46 /**
47 * Get the intel_region which is the source for any glCopyTex[Sub]Image call.
48 *
49 * Do the best we can using the blitter. A future project is to use
50 * the texture engine and fragment programs for these copies.
51 */
52 static struct intel_renderbuffer *
53 get_teximage_readbuffer(struct intel_context *intel, GLenum internalFormat)
54 {
55 DBG("%s %s\n", __FUNCTION__,
56 _mesa_lookup_enum_by_nr(internalFormat));
57
58 switch (internalFormat) {
59 case GL_DEPTH_COMPONENT:
60 case GL_DEPTH_COMPONENT16:
61 case GL_DEPTH24_STENCIL8_EXT:
62 case GL_DEPTH_STENCIL_EXT:
63 return intel_get_renderbuffer(intel->ctx.ReadBuffer, BUFFER_DEPTH);
64 default:
65 return intel_renderbuffer(intel->ctx.ReadBuffer->_ColorReadBuffer);
66 }
67 }
68
69
70 static GLboolean
71 do_copy_texsubimage(struct intel_context *intel,
72 GLenum target,
73 struct intel_texture_image *intelImage,
74 GLenum internalFormat,
75 GLint dstx, GLint dsty,
76 GLint x, GLint y, GLsizei width, GLsizei height)
77 {
78 struct gl_context *ctx = &intel->ctx;
79 struct intel_renderbuffer *irb;
80 bool copy_supported_with_alpha_override = false;
81
82 intel_prepare_render(intel);
83
84 irb = get_teximage_readbuffer(intel, internalFormat);
85 if (!intelImage->mt || !irb || !irb->region) {
86 if (unlikely(INTEL_DEBUG & DEBUG_FALLBACKS))
87 fprintf(stderr, "%s fail %p %p (0x%08x)\n",
88 __FUNCTION__, intelImage->mt, irb, internalFormat);
89 return GL_FALSE;
90 }
91
92 if (irb->Base.Format == MESA_FORMAT_XRGB8888 &&
93 intelImage->base.TexFormat == MESA_FORMAT_ARGB8888) {
94 copy_supported_with_alpha_override = true;
95 }
96
97 if (intelImage->base.TexFormat != irb->Base.Format &&
98 !copy_supported_with_alpha_override) {
99 if (unlikely(INTEL_DEBUG & DEBUG_FALLBACKS))
100 fprintf(stderr, "%s mismatched formats %s, %s\n",
101 __FUNCTION__,
102 _mesa_get_format_name(intelImage->base.TexFormat),
103 _mesa_get_format_name(irb->Base.Format));
104 return GL_FALSE;
105 }
106
107 {
108 drm_intel_bo *dst_bo = intel_region_buffer(intel,
109 intelImage->mt->region,
110 INTEL_WRITE_PART);
111 GLuint image_x, image_y;
112 GLshort src_pitch;
113
114 /* get dest x/y in destination texture */
115 intel_miptree_get_image_offset(intelImage->mt,
116 intelImage->level,
117 intelImage->face,
118 0,
119 &image_x, &image_y);
120
121 /* The blitter can't handle Y-tiled buffers. */
122 if (intelImage->mt->region->tiling == I915_TILING_Y) {
123 return GL_FALSE;
124 }
125
126 if (ctx->ReadBuffer->Name == 0) {
127 /* Flip vertical orientation for system framebuffers */
128 y = ctx->ReadBuffer->Height - (y + height);
129 src_pitch = -irb->region->pitch;
130 } else {
131 /* reading from a FBO, y is already oriented the way we like */
132 src_pitch = irb->region->pitch;
133 }
134
135 /* blit from src buffer to texture */
136 if (!intelEmitCopyBlit(intel,
137 intelImage->mt->cpp,
138 src_pitch,
139 irb->region->buffer,
140 0,
141 irb->region->tiling,
142 intelImage->mt->region->pitch,
143 dst_bo,
144 0,
145 intelImage->mt->region->tiling,
146 irb->region->draw_x + x, irb->region->draw_y + y,
147 image_x + dstx, image_y + dsty,
148 width, height,
149 GL_COPY)) {
150 return GL_FALSE;
151 }
152 }
153
154 if (copy_supported_with_alpha_override)
155 intel_set_teximage_alpha_to_one(ctx, intelImage);
156
157 return GL_TRUE;
158 }
159
160
161 static void
162 intelCopyTexImage1D(struct gl_context * ctx, GLenum target, GLint level,
163 GLenum internalFormat,
164 GLint x, GLint y, GLsizei width, GLint border)
165 {
166 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
167 struct gl_texture_object *texObj =
168 _mesa_select_tex_object(ctx, texUnit, target);
169 struct gl_texture_image *texImage =
170 _mesa_select_tex_image(ctx, texObj, target, level);
171 int srcx, srcy, dstx, dsty, height;
172
173 if (border)
174 goto fail;
175
176 /* Setup or redefine the texture object, mipmap tree and texture
177 * image. Don't populate yet.
178 */
179 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
180 width, border,
181 GL_RGBA, CHAN_TYPE, NULL,
182 &ctx->DefaultPacking, texObj, texImage);
183 srcx = x;
184 srcy = y;
185 dstx = 0;
186 dsty = 0;
187 height = 1;
188 if (!_mesa_clip_copytexsubimage(ctx,
189 &dstx, &dsty,
190 &srcx, &srcy,
191 &width, &height))
192 return;
193
194 if (!do_copy_texsubimage(intel_context(ctx), target,
195 intel_texture_image(texImage),
196 internalFormat, 0, 0, x, y, width, height))
197 goto fail;
198
199 return;
200
201 fail:
202 fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
203 _mesa_meta_CopyTexImage1D(ctx, target, level, internalFormat, x, y,
204 width, border);
205 }
206
207
208 static void
209 intelCopyTexImage2D(struct gl_context * ctx, GLenum target, GLint level,
210 GLenum internalFormat,
211 GLint x, GLint y, GLsizei width, GLsizei height,
212 GLint border)
213 {
214 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
215 struct gl_texture_object *texObj =
216 _mesa_select_tex_object(ctx, texUnit, target);
217 struct gl_texture_image *texImage =
218 _mesa_select_tex_image(ctx, texObj, target, level);
219 int srcx, srcy, dstx, dsty;
220
221 if (border)
222 goto fail;
223
224 /* Setup or redefine the texture object, mipmap tree and texture
225 * image. Don't populate yet.
226 */
227 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
228 width, height, border,
229 GL_RGBA, GL_UNSIGNED_BYTE, NULL,
230 &ctx->DefaultPacking, texObj, texImage);
231
232 srcx = x;
233 srcy = y;
234 dstx = 0;
235 dsty = 0;
236 if (!_mesa_clip_copytexsubimage(ctx,
237 &dstx, &dsty,
238 &srcx, &srcy,
239 &width, &height))
240 return;
241
242 if (!do_copy_texsubimage(intel_context(ctx), target,
243 intel_texture_image(texImage),
244 internalFormat, 0, 0, x, y, width, height))
245 goto fail;
246
247 return;
248
249 fail:
250 fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
251 _mesa_meta_CopyTexImage2D(ctx, target, level, internalFormat, x, y,
252 width, height, border);
253 }
254
255
256 static void
257 intelCopyTexSubImage1D(struct gl_context * ctx, GLenum target, GLint level,
258 GLint xoffset, GLint x, GLint y, GLsizei width)
259 {
260 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
261 struct gl_texture_object *texObj =
262 _mesa_select_tex_object(ctx, texUnit, target);
263 struct gl_texture_image *texImage =
264 _mesa_select_tex_image(ctx, texObj, target, level);
265 GLenum internalFormat = texImage->InternalFormat;
266
267 /* XXX need to check <border> as in above function? */
268
269 /* Need to check texture is compatible with source format.
270 */
271
272 if (!do_copy_texsubimage(intel_context(ctx), target,
273 intel_texture_image(texImage),
274 internalFormat, xoffset, 0, x, y, width, 1)) {
275 fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
276 _mesa_meta_CopyTexSubImage1D(ctx, target, level, xoffset, x, y, width);
277 }
278 }
279
280
281 static void
282 intelCopyTexSubImage2D(struct gl_context * ctx, GLenum target, GLint level,
283 GLint xoffset, GLint yoffset,
284 GLint x, GLint y, GLsizei width, GLsizei height)
285 {
286 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
287 struct gl_texture_object *texObj =
288 _mesa_select_tex_object(ctx, texUnit, target);
289 struct gl_texture_image *texImage =
290 _mesa_select_tex_image(ctx, texObj, target, level);
291 GLenum internalFormat = texImage->InternalFormat;
292
293 /* Need to check texture is compatible with source format.
294 */
295
296 if (!do_copy_texsubimage(intel_context(ctx), target,
297 intel_texture_image(texImage),
298 internalFormat,
299 xoffset, yoffset, x, y, width, height)) {
300
301 fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
302 _mesa_meta_CopyTexSubImage2D(ctx, target, level,
303 xoffset, yoffset, x, y, width, height);
304 }
305 }
306
307
308 void
309 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
310 {
311 functions->CopyTexImage1D = intelCopyTexImage1D;
312 functions->CopyTexImage2D = intelCopyTexImage2D;
313 functions->CopyTexSubImage1D = intelCopyTexSubImage1D;
314 functions->CopyTexSubImage2D = intelCopyTexSubImage2D;
315 }