draw: corrections to allow for different cliptest cases
[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 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 GL_RGB:
86 case GL_RGB8:
87 return intel_readbuf_region(intel);
88 default:
89 return NULL;
90 }
91 }
92
93
94 static GLboolean
95 do_copy_texsubimage(struct intel_context *intel,
96 GLenum target,
97 struct intel_texture_image *intelImage,
98 GLenum internalFormat,
99 GLint dstx, GLint dsty,
100 GLint x, GLint y, GLsizei width, GLsizei height)
101 {
102 GLcontext *ctx = &intel->ctx;
103 const struct intel_region *src = get_teximage_source(intel, internalFormat);
104
105 if (!intelImage->mt || !src || !src->buffer) {
106 if (INTEL_DEBUG & DEBUG_FALLBACKS)
107 fprintf(stderr, "%s fail %p %p (0x%08x)\n",
108 __FUNCTION__, intelImage->mt, src, internalFormat);
109 return GL_FALSE;
110 }
111
112 if (intelImage->mt->cpp != src->cpp) {
113 if (INTEL_DEBUG & DEBUG_FALLBACKS)
114 fprintf(stderr, "%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(GLcontext * 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 if (INTEL_DEBUG & DEBUG_FALLBACKS)
214 fprintf(stderr, "%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(GLcontext * 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 if (INTEL_DEBUG & DEBUG_FALLBACKS)
263 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
264 _mesa_meta_CopyTexImage2D(ctx, target, level, internalFormat, x, y,
265 width, height, border);
266 }
267
268
269 static void
270 intelCopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
271 GLint xoffset, GLint x, GLint y, GLsizei width)
272 {
273 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
274 struct gl_texture_object *texObj =
275 _mesa_select_tex_object(ctx, texUnit, target);
276 struct gl_texture_image *texImage =
277 _mesa_select_tex_image(ctx, texObj, target, level);
278 GLenum internalFormat = texImage->InternalFormat;
279
280 /* XXX need to check <border> as in above function? */
281
282 /* Need to check texture is compatible with source format.
283 */
284
285 if (!do_copy_texsubimage(intel_context(ctx), target,
286 intel_texture_image(texImage),
287 internalFormat, xoffset, 0, x, y, width, 1)) {
288 if (INTEL_DEBUG & DEBUG_FALLBACKS)
289 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
290 _mesa_meta_CopyTexSubImage1D(ctx, target, level, xoffset, x, y, width);
291 }
292 }
293
294
295 static void
296 intelCopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
297 GLint xoffset, GLint yoffset,
298 GLint x, GLint y, GLsizei width, GLsizei height)
299 {
300 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
301 struct gl_texture_object *texObj =
302 _mesa_select_tex_object(ctx, texUnit, target);
303 struct gl_texture_image *texImage =
304 _mesa_select_tex_image(ctx, texObj, target, level);
305 GLenum internalFormat = texImage->InternalFormat;
306
307 /* Need to check texture is compatible with source format.
308 */
309
310 if (!do_copy_texsubimage(intel_context(ctx), target,
311 intel_texture_image(texImage),
312 internalFormat,
313 xoffset, yoffset, x, y, width, height)) {
314
315 if (INTEL_DEBUG & DEBUG_FALLBACKS)
316 fprintf(stderr, "%s - fallback to swrast\n", __FUNCTION__);
317 _mesa_meta_CopyTexSubImage2D(ctx, target, level,
318 xoffset, yoffset, x, y, width, height);
319 }
320 }
321
322
323 void
324 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
325 {
326 functions->CopyTexImage1D = intelCopyTexImage1D;
327 functions->CopyTexImage2D = intelCopyTexImage2D;
328 functions->CopyTexSubImage1D = intelCopyTexSubImage1D;
329 functions->CopyTexSubImage2D = intelCopyTexSubImage2D;
330 }