88b62e781c03a91781ceb5214331dc7a19c95936
[mesa.git] / src / mesa / drivers / dri / i915tex / 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 #include "intel_pixel.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_ARB:
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 return intel_readbuf_region(intel);
77 case GL_RGB:
78 if (intel->intelScreen->cpp == 2)
79 return intel_readbuf_region(intel);
80 return NULL;
81 default:
82 return NULL;
83 }
84 }
85
86
87 static GLboolean
88 do_copy_texsubimage(struct intel_context *intel,
89 struct intel_texture_image *intelImage,
90 GLenum internalFormat,
91 GLint dstx, GLint dsty,
92 GLint x, GLint y, GLsizei width, GLsizei height)
93 {
94 GLcontext *ctx = &intel->ctx;
95 const struct intel_region *src =
96 get_teximage_source(intel, internalFormat);
97
98 if (!intelImage->mt || !src) {
99 DBG("%s fail %p %p\n", __FUNCTION__, intelImage->mt, src);
100 return GL_FALSE;
101 }
102
103 intelFlush(ctx);
104 LOCK_HARDWARE(intel);
105 {
106 GLuint image_offset = intel_miptree_image_offset(intelImage->mt,
107 intelImage->face,
108 intelImage->level);
109 const GLint orig_x = x;
110 const GLint orig_y = y;
111 const struct gl_framebuffer *fb = ctx->DrawBuffer;
112
113 if (_mesa_clip_to_region(fb->_Xmin, fb->_Ymin, fb->_Xmax, fb->_Ymax,
114 &x, &y, &width, &height)) {
115 /* Update dst for clipped src. Need to also clip the source rect.
116 */
117 dstx += x - orig_x;
118 dsty += y - orig_y;
119
120 if (ctx->ReadBuffer->Name == 0) {
121 /* reading from a window, adjust x, y */
122 __DRIdrawablePrivate *dPriv = intel->driDrawable;
123 GLuint window_y;
124 /* window_y = position of window on screen if y=0=bottom */
125 window_y = intel->intelScreen->height - (dPriv->y + dPriv->h);
126 y = window_y + y;
127 x += dPriv->x;
128 }
129 else {
130 /* reading from a FBO */
131 /* invert Y */
132 y = ctx->ReadBuffer->Height - y - 1;
133 }
134
135
136 /* A bit of fiddling to get the blitter to work with -ve
137 * pitches. But we get a nice inverted blit this way, so it's
138 * worth it:
139 */
140 intelEmitCopyBlit(intel,
141 intelImage->mt->cpp,
142 -src->pitch,
143 src->buffer,
144 src->height * src->pitch * src->cpp,
145 intelImage->mt->pitch,
146 intelImage->mt->region->buffer,
147 image_offset,
148 x, y + height, dstx, dsty, width, height);
149
150 intel_batchbuffer_flush(intel->batch);
151 }
152 }
153
154
155 UNLOCK_HARDWARE(intel);
156
157 #if 0
158 /* GL_SGIS_generate_mipmap -- this can be accelerated now.
159 * XXX Add a ctx->Driver.GenerateMipmaps() function?
160 */
161 if (level == texObj->BaseLevel && texObj->GenerateMipmap) {
162 intel_generate_mipmap(ctx, target,
163 &ctx->Texture.Unit[ctx->Texture.CurrentUnit],
164 texObj);
165 }
166 #endif
167
168 return GL_TRUE;
169 }
170
171
172
173
174
175 void
176 intelCopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
177 GLenum internalFormat,
178 GLint x, GLint y, GLsizei width, GLint border)
179 {
180 struct gl_texture_unit *texUnit =
181 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
182 struct gl_texture_object *texObj =
183 _mesa_select_tex_object(ctx, texUnit, target);
184 struct gl_texture_image *texImage =
185 _mesa_select_tex_image(ctx, texObj, target, level);
186
187 if (border)
188 goto fail;
189
190 /* Setup or redefine the texture object, mipmap tree and texture
191 * image. Don't populate yet.
192 */
193 ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
194 width, border,
195 GL_RGBA, CHAN_TYPE, NULL,
196 &ctx->DefaultPacking, texObj, texImage);
197
198 if (!do_copy_texsubimage(intel_context(ctx),
199 intel_texture_image(texImage),
200 internalFormat, 0, 0, x, y, width, 1))
201 goto fail;
202
203 return;
204
205 fail:
206 _swrast_copy_teximage1d(ctx, target, level, internalFormat, x, y,
207 width, border);
208 }
209
210 void
211 intelCopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
212 GLenum internalFormat,
213 GLint x, GLint y, GLsizei width, GLsizei height,
214 GLint border)
215 {
216 struct gl_texture_unit *texUnit =
217 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
218 struct gl_texture_object *texObj =
219 _mesa_select_tex_object(ctx, texUnit, target);
220 struct gl_texture_image *texImage =
221 _mesa_select_tex_image(ctx, texObj, target, level);
222
223 if (border)
224 goto fail;
225
226 /* Setup or redefine the texture object, mipmap tree and texture
227 * image. Don't populate yet.
228 */
229 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
230 width, height, border,
231 GL_RGBA, CHAN_TYPE, NULL,
232 &ctx->DefaultPacking, texObj, texImage);
233
234
235 if (!do_copy_texsubimage(intel_context(ctx),
236 intel_texture_image(texImage),
237 internalFormat, 0, 0, x, y, width, height))
238 goto fail;
239
240 return;
241
242 fail:
243 _swrast_copy_teximage2d(ctx, target, level, internalFormat, x, y,
244 width, height, border);
245 }
246
247
248 void
249 intelCopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
250 GLint xoffset, GLint x, GLint y, GLsizei width)
251 {
252 struct gl_texture_unit *texUnit =
253 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
254 struct gl_texture_object *texObj =
255 _mesa_select_tex_object(ctx, texUnit, target);
256 struct gl_texture_image *texImage =
257 _mesa_select_tex_image(ctx, texObj, target, level);
258 GLenum internalFormat = texImage->InternalFormat;
259
260 /* XXX need to check <border> as in above function? */
261
262 /* Need to check texture is compatible with source format.
263 */
264
265 if (!do_copy_texsubimage(intel_context(ctx),
266 intel_texture_image(texImage),
267 internalFormat, xoffset, 0, x, y, width, 1)) {
268 _swrast_copy_texsubimage1d(ctx, target, level, xoffset, x, y, width);
269 }
270 }
271
272
273
274 void
275 intelCopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
276 GLint xoffset, GLint yoffset,
277 GLint x, GLint y, GLsizei width, GLsizei height)
278 {
279 struct gl_texture_unit *texUnit =
280 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
281 struct gl_texture_object *texObj =
282 _mesa_select_tex_object(ctx, texUnit, target);
283 struct gl_texture_image *texImage =
284 _mesa_select_tex_image(ctx, texObj, target, level);
285 GLenum internalFormat = texImage->InternalFormat;
286
287
288 /* Need to check texture is compatible with source format.
289 */
290
291 if (!do_copy_texsubimage(intel_context(ctx),
292 intel_texture_image(texImage),
293 internalFormat,
294 xoffset, yoffset, x, y, width, height)) {
295
296 DBG("%s - fallback to swrast\n", __FUNCTION__);
297
298 _swrast_copy_texsubimage2d(ctx, target, level,
299 xoffset, yoffset, x, y, width, height);
300 }
301 }