f1a455a04cd1f090247692eba9759a6d1828ee48
[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 "mtypes.h"
29 #include "enums.h"
30 #include "image.h"
31 #include "teximage.h"
32 #include "swrast/swrast.h"
33
34 #include "intel_screen.h"
35 #include "intel_context.h"
36 #include "intel_batchbuffer.h"
37 #include "intel_buffers.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 const struct intel_region *
53 get_teximage_source(struct intel_context *intel, GLenum internalFormat)
54 {
55 struct intel_renderbuffer *irb;
56
57 DBG("%s %s\n", __FUNCTION__,
58 _mesa_lookup_enum_by_nr(internalFormat));
59
60 switch (internalFormat) {
61 case GL_DEPTH_COMPONENT:
62 case GL_DEPTH_COMPONENT16_ARB:
63 irb = intel_get_renderbuffer(intel->ctx.ReadBuffer, BUFFER_DEPTH);
64 if (irb && irb->region && irb->region->cpp == 2)
65 return irb->region;
66 return NULL;
67 case GL_DEPTH24_STENCIL8_EXT:
68 case GL_DEPTH_STENCIL_EXT:
69 irb = intel_get_renderbuffer(intel->ctx.ReadBuffer, BUFFER_DEPTH);
70 if (irb && irb->region && irb->region->cpp == 4)
71 return irb->region;
72 return NULL;
73 case GL_RGBA:
74 case GL_RGBA8:
75 return intel_readbuf_region(intel);
76 case GL_RGB:
77 if (intel->intelScreen->cpp == 2)
78 return intel_readbuf_region(intel);
79 return NULL;
80 default:
81 return NULL;
82 }
83 }
84
85
86 static GLboolean
87 do_copy_texsubimage(struct intel_context *intel,
88 struct intel_texture_image *intelImage,
89 GLenum internalFormat,
90 GLint dstx, GLint dsty,
91 GLint x, GLint y, GLsizei width, GLsizei height)
92 {
93 GLcontext *ctx = &intel->ctx;
94 const struct intel_region *src =
95 get_teximage_source(intel, internalFormat);
96
97 if (!intelImage->mt || !src) {
98 DBG("%s fail %p %p\n", __FUNCTION__, intelImage->mt, src);
99 return GL_FALSE;
100 }
101
102 intelFlush(ctx);
103 LOCK_HARDWARE(intel);
104 {
105 GLuint image_offset = intel_miptree_image_offset(intelImage->mt,
106 intelImage->face,
107 intelImage->level);
108 const GLint orig_x = x;
109 const GLint orig_y = y;
110 const struct gl_framebuffer *fb = ctx->DrawBuffer;
111
112 if (_mesa_clip_to_region(fb->_Xmin, fb->_Ymin, fb->_Xmax, fb->_Ymax,
113 &x, &y, &width, &height)) {
114 /* Update dst for clipped src. Need to also clip the source rect.
115 */
116 dstx += x - orig_x;
117 dsty += y - orig_y;
118
119 if (ctx->ReadBuffer->Name == 0) {
120 /* reading from a window, adjust x, y */
121 __DRIdrawablePrivate *dPriv = intel->driDrawable;
122 GLuint window_y;
123 /* window_y = position of window on screen if y=0=bottom */
124 window_y = intel->intelScreen->height - (dPriv->y + dPriv->h);
125 y = window_y + y;
126 x += dPriv->x;
127 }
128 else {
129 /* reading from a FBO */
130 /* invert Y */
131 y = ctx->ReadBuffer->Height - y - 1;
132 }
133
134
135 /* A bit of fiddling to get the blitter to work with -ve
136 * pitches. But we get a nice inverted blit this way, so it's
137 * worth it:
138 */
139 intelEmitCopyBlit(intel,
140 intelImage->mt->cpp,
141 -src->pitch,
142 src->buffer,
143 src->height * src->pitch * src->cpp,
144 GL_FALSE,
145 intelImage->mt->pitch,
146 intelImage->mt->region->buffer,
147 image_offset,
148 intelImage->mt->region->tiled,
149 x, y + height, dstx, dsty, width, height,
150 GL_COPY); /* ? */
151
152 intel_batchbuffer_flush(intel->batch);
153 }
154 }
155
156
157 UNLOCK_HARDWARE(intel);
158
159 #if 0
160 /* GL_SGIS_generate_mipmap -- this can be accelerated now.
161 * XXX Add a ctx->Driver.GenerateMipmaps() function?
162 */
163 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
164 intel_generate_mipmap(ctx, target,
165 &ctx->Texture.Unit[ctx->Texture.CurrentUnit],
166 texObj);
167 }
168 #endif
169
170 return GL_TRUE;
171 }
172
173
174
175
176
177 void
178 intelCopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
179 GLenum internalFormat,
180 GLint x, GLint y, GLsizei width, GLint border)
181 {
182 struct gl_texture_unit *texUnit =
183 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
184 struct gl_texture_object *texObj =
185 _mesa_select_tex_object(ctx, texUnit, target);
186 struct gl_texture_image *texImage =
187 _mesa_select_tex_image(ctx, texObj, target, level);
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
200 if (!do_copy_texsubimage(intel_context(ctx),
201 intel_texture_image(texImage),
202 internalFormat, 0, 0, x, y, width, 1))
203 goto fail;
204
205 return;
206
207 fail:
208 _swrast_copy_teximage1d(ctx, target, level, internalFormat, x, y,
209 width, border);
210 }
211
212 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 =
219 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
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
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, CHAN_TYPE, NULL,
234 &ctx->DefaultPacking, texObj, texImage);
235
236
237 if (!do_copy_texsubimage(intel_context(ctx),
238 intel_texture_image(texImage),
239 internalFormat, 0, 0, x, y, width, height))
240 goto fail;
241
242 return;
243
244 fail:
245 _swrast_copy_teximage2d(ctx, target, level, internalFormat, x, y,
246 width, height, border);
247 }
248
249
250 void
251 intelCopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
252 GLint xoffset, GLint x, GLint y, GLsizei width)
253 {
254 struct gl_texture_unit *texUnit =
255 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
256 struct gl_texture_object *texObj =
257 _mesa_select_tex_object(ctx, texUnit, target);
258 struct gl_texture_image *texImage =
259 _mesa_select_tex_image(ctx, texObj, target, level);
260 GLenum internalFormat = texImage->InternalFormat;
261
262 /* XXX need to check <border> as in above function? */
263
264 /* Need to check texture is compatible with source format.
265 */
266
267 if (!do_copy_texsubimage(intel_context(ctx),
268 intel_texture_image(texImage),
269 internalFormat, xoffset, 0, x, y, width, 1)) {
270 _swrast_copy_texsubimage1d(ctx, target, level, xoffset, x, y, width);
271 }
272 }
273
274
275
276 void
277 intelCopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
278 GLint xoffset, GLint yoffset,
279 GLint x, GLint y, GLsizei width, GLsizei height)
280 {
281 struct gl_texture_unit *texUnit =
282 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
283 struct gl_texture_object *texObj =
284 _mesa_select_tex_object(ctx, texUnit, target);
285 struct gl_texture_image *texImage =
286 _mesa_select_tex_image(ctx, texObj, target, level);
287 GLenum internalFormat = texImage->InternalFormat;
288
289
290 /* Need to check texture is compatible with source format.
291 */
292
293 if (!do_copy_texsubimage(intel_context(ctx),
294 intel_texture_image(texImage),
295 internalFormat,
296 xoffset, yoffset, x, y, width, height)) {
297
298 DBG("%s - fallback to swrast\n", __FUNCTION__);
299
300 _swrast_copy_texsubimage2d(ctx, target, level,
301 xoffset, yoffset, x, y, width, height);
302 }
303 }