i915: Simplify intel_wpos_* with a helper function.
[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 if (_mesa_is_depth_format(internalFormat) ||
59 _mesa_is_depthstencil_format(internalFormat))
60 return intel_get_renderbuffer(intel->ctx.ReadBuffer, BUFFER_DEPTH);
61
62 return intel_renderbuffer(intel->ctx.ReadBuffer->_ColorReadBuffer);
63 }
64
65
66 GLboolean
67 intel_copy_texsubimage(struct intel_context *intel,
68 GLenum target,
69 struct intel_texture_image *intelImage,
70 GLenum internalFormat,
71 GLint dstx, GLint dsty,
72 GLint x, GLint y, GLsizei width, GLsizei height)
73 {
74 struct gl_context *ctx = &intel->ctx;
75 struct intel_renderbuffer *irb;
76 bool copy_supported = false;
77 bool copy_supported_with_alpha_override = false;
78
79 intel_prepare_render(intel);
80
81 irb = get_teximage_readbuffer(intel, internalFormat);
82 if (!intelImage->mt || !irb || !irb->region) {
83 if (unlikely(INTEL_DEBUG & DEBUG_FALLBACKS))
84 fprintf(stderr, "%s fail %p %p (0x%08x)\n",
85 __FUNCTION__, intelImage->mt, irb, internalFormat);
86 return GL_FALSE;
87 }
88
89 copy_supported = intelImage->base.TexFormat == irb->Base.Format;
90
91 /* Converting ARGB8888 to XRGB8888 is trivial: ignore the alpha bits */
92 if (irb->Base.Format == MESA_FORMAT_ARGB8888 &&
93 intelImage->base.TexFormat == MESA_FORMAT_XRGB8888) {
94 copy_supported = true;
95 }
96
97 /* Converting XRGB8888 to ARGB8888 requires setting the alpha bits to 1.0 */
98 if (irb->Base.Format == MESA_FORMAT_XRGB8888 &&
99 intelImage->base.TexFormat == MESA_FORMAT_ARGB8888) {
100 copy_supported_with_alpha_override = true;
101 }
102
103 if (!copy_supported && !copy_supported_with_alpha_override) {
104 if (unlikely(INTEL_DEBUG & DEBUG_FALLBACKS))
105 fprintf(stderr, "%s mismatched formats %s, %s\n",
106 __FUNCTION__,
107 _mesa_get_format_name(intelImage->base.TexFormat),
108 _mesa_get_format_name(irb->Base.Format));
109 return GL_FALSE;
110 }
111
112 {
113 drm_intel_bo *dst_bo = intel_region_buffer(intel,
114 intelImage->mt->region,
115 INTEL_WRITE_PART);
116 GLuint image_x, image_y;
117 GLshort src_pitch;
118
119 /* get dest x/y in destination texture */
120 intel_miptree_get_image_offset(intelImage->mt,
121 intelImage->level,
122 intelImage->face,
123 0,
124 &image_x, &image_y);
125
126 /* The blitter can't handle Y-tiled buffers. */
127 if (intelImage->mt->region->tiling == I915_TILING_Y) {
128 return GL_FALSE;
129 }
130
131 if (ctx->ReadBuffer->Name == 0) {
132 /* Flip vertical orientation for system framebuffers */
133 y = ctx->ReadBuffer->Height - (y + height);
134 src_pitch = -irb->region->pitch;
135 } else {
136 /* reading from a FBO, y is already oriented the way we like */
137 src_pitch = irb->region->pitch;
138 }
139
140 /* blit from src buffer to texture */
141 if (!intelEmitCopyBlit(intel,
142 intelImage->mt->cpp,
143 src_pitch,
144 irb->region->buffer,
145 0,
146 irb->region->tiling,
147 intelImage->mt->region->pitch,
148 dst_bo,
149 0,
150 intelImage->mt->region->tiling,
151 irb->draw_x + x, irb->draw_y + y,
152 image_x + dstx, image_y + dsty,
153 width, height,
154 GL_COPY)) {
155 return GL_FALSE;
156 }
157 }
158
159 if (copy_supported_with_alpha_override)
160 intel_set_teximage_alpha_to_one(ctx, intelImage);
161
162 return GL_TRUE;
163 }
164
165
166 static void
167 intelCopyTexImage1D(struct gl_context * ctx, GLenum target, GLint level,
168 GLenum internalFormat,
169 GLint x, GLint y, GLsizei width, GLint border)
170 {
171 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
172 struct gl_texture_object *texObj =
173 _mesa_select_tex_object(ctx, texUnit, target);
174 struct gl_texture_image *texImage =
175 _mesa_select_tex_image(ctx, texObj, target, level);
176 int srcx, srcy, dstx, dsty, height;
177
178 if (border)
179 goto fail;
180
181 /* Setup or redefine the texture object, mipmap tree and texture
182 * image. Don't populate yet.
183 */
184 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
185 width, border,
186 GL_RGBA, CHAN_TYPE, NULL,
187 &ctx->DefaultPacking, texObj, texImage);
188 srcx = x;
189 srcy = y;
190 dstx = 0;
191 dsty = 0;
192 height = 1;
193 if (!_mesa_clip_copytexsubimage(ctx,
194 &dstx, &dsty,
195 &srcx, &srcy,
196 &width, &height))
197 return;
198
199 if (!intel_copy_texsubimage(intel_context(ctx), target,
200 intel_texture_image(texImage),
201 internalFormat, 0, 0, x, y, width, height))
202 goto fail;
203
204 return;
205
206 fail:
207 fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
208 _mesa_meta_CopyTexImage1D(ctx, target, level, internalFormat, x, y,
209 width, border);
210 }
211
212
213 static void
214 intelCopyTexImage2D(struct gl_context * ctx, GLenum target, GLint level,
215 GLenum internalFormat,
216 GLint x, GLint y, GLsizei width, GLsizei height,
217 GLint border)
218 {
219 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
220 struct gl_texture_object *texObj =
221 _mesa_select_tex_object(ctx, texUnit, target);
222 struct gl_texture_image *texImage =
223 _mesa_select_tex_image(ctx, texObj, target, level);
224 int srcx, srcy, dstx, dsty;
225
226 if (border)
227 goto fail;
228
229 /* Setup or redefine the texture object, mipmap tree and texture
230 * image. Don't populate yet.
231 */
232 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
233 width, height, border,
234 GL_RGBA, GL_UNSIGNED_BYTE, NULL,
235 &ctx->DefaultPacking, texObj, texImage);
236
237 srcx = x;
238 srcy = y;
239 dstx = 0;
240 dsty = 0;
241 if (!_mesa_clip_copytexsubimage(ctx,
242 &dstx, &dsty,
243 &srcx, &srcy,
244 &width, &height))
245 return;
246
247 if (!intel_copy_texsubimage(intel_context(ctx), target,
248 intel_texture_image(texImage),
249 internalFormat, 0, 0, x, y, width, height))
250 goto fail;
251
252 return;
253
254 fail:
255 fallback_debug("%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(struct gl_context * 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 (!intel_copy_texsubimage(intel_context(ctx), target,
278 intel_texture_image(texImage),
279 internalFormat, xoffset, 0, x, y, width, 1)) {
280 fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
281 _mesa_meta_CopyTexSubImage1D(ctx, target, level, xoffset, x, y, width);
282 }
283 }
284
285
286 static void
287 intelCopyTexSubImage2D(struct gl_context * ctx, GLenum target, GLint level,
288 GLint xoffset, GLint yoffset,
289 GLint x, GLint y, GLsizei width, GLsizei height)
290 {
291 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
292 struct gl_texture_object *texObj =
293 _mesa_select_tex_object(ctx, texUnit, target);
294 struct gl_texture_image *texImage =
295 _mesa_select_tex_image(ctx, texObj, target, level);
296 GLenum internalFormat = texImage->InternalFormat;
297
298 /* Need to check texture is compatible with source format.
299 */
300
301 if (!intel_copy_texsubimage(intel_context(ctx), target,
302 intel_texture_image(texImage),
303 internalFormat,
304 xoffset, yoffset, x, y, width, height)) {
305 fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
306 _mesa_meta_CopyTexSubImage2D(ctx, target, level,
307 xoffset, yoffset, x, y, width, height);
308 }
309 }
310
311
312 void
313 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
314 {
315 functions->CopyTexImage1D = intelCopyTexImage1D;
316 functions->CopyTexImage2D = intelCopyTexImage2D;
317 functions->CopyTexSubImage1D = intelCopyTexSubImage1D;
318 functions->CopyTexSubImage2D = intelCopyTexSubImage2D;
319 }