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