i965: fix bugs in projective texture coordinates
[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 return intel_readbuf_region(intel);
77 case GL_RGB:
78 if (intel->ctx.Visual.rgbBits == 16)
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 GLenum target,
90 struct intel_texture_image *intelImage,
91 GLenum internalFormat,
92 GLint dstx, GLint dsty,
93 GLint x, GLint y, GLsizei width, GLsizei height)
94 {
95 GLcontext *ctx = &intel->ctx;
96 struct gl_texture_object *texObj = intelImage->base.TexObject;
97 const struct intel_region *src =
98 get_teximage_source(intel, internalFormat);
99
100 if (!intelImage->mt || !src) {
101 if (INTEL_DEBUG & DEBUG_FALLBACKS)
102 fprintf(stderr, "%s fail %p %p\n",
103 __FUNCTION__, intelImage->mt, src);
104 return GL_FALSE;
105 }
106
107 intelFlush(ctx);
108 LOCK_HARDWARE(intel);
109 {
110 GLuint image_offset = intel_miptree_image_offset(intelImage->mt,
111 intelImage->face,
112 intelImage->level);
113 const GLint orig_x = x;
114 const GLint orig_y = y;
115 GLshort src_pitch;
116
117 /* Update dst for clipped src. Need to also clip the source rect. */
118 dstx += x - orig_x;
119 dsty += y - orig_y;
120
121 /* image_offset may be non-page-aligned, but that's illegal for tiling. */
122 assert(intelImage->mt->region->tiling == I915_TILING_NONE);
123
124 if (ctx->ReadBuffer->Name == 0) {
125 /* reading from a window, adjust x, y */
126 __DRIdrawablePrivate *dPriv = intel->driDrawable;
127 y = dPriv->y + (dPriv->h - (y + height));
128 x += dPriv->x;
129
130 /* Invert the data coming from the source rectangle due to GL
131 * and hardware disagreeing on where y=0 is.
132 *
133 * It appears that our offsets and pitches get mangled
134 * appropriately by the hardware, and we don't need to adjust them
135 * on our own.
136 */
137 src_pitch = -src->pitch;
138 } else {
139 /* reading from a FBO, y is already oriented the way we like */
140 src_pitch = src->pitch;
141 }
142
143 intelEmitCopyBlit(intel,
144 intelImage->mt->cpp,
145 src_pitch,
146 src->buffer,
147 0,
148 src->tiling,
149 intelImage->mt->pitch,
150 intelImage->mt->region->buffer,
151 image_offset,
152 intelImage->mt->region->tiling,
153 x, y, dstx, dsty, width, height,
154 GL_COPY);
155 }
156
157 UNLOCK_HARDWARE(intel);
158
159 /* GL_SGIS_generate_mipmap */
160 if (intelImage->level == texObj->BaseLevel && texObj->GenerateMipmap) {
161 ctx->Driver.GenerateMipmap(ctx, target, texObj);
162 }
163
164 return GL_TRUE;
165 }
166
167
168
169
170
171 void
172 intelCopyTexImage1D(GLcontext * ctx, GLenum target, GLint level,
173 GLenum internalFormat,
174 GLint x, GLint y, GLsizei width, GLint border)
175 {
176 struct gl_texture_unit *texUnit =
177 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
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 _swrast_copy_teximage1d(ctx, target, level, internalFormat, x, y,
214 width, border);
215 }
216
217 void
218 intelCopyTexImage2D(GLcontext * ctx, GLenum target, GLint level,
219 GLenum internalFormat,
220 GLint x, GLint y, GLsizei width, GLsizei height,
221 GLint border)
222 {
223 struct gl_texture_unit *texUnit =
224 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
225 struct gl_texture_object *texObj =
226 _mesa_select_tex_object(ctx, texUnit, target);
227 struct gl_texture_image *texImage =
228 _mesa_select_tex_image(ctx, texObj, target, level);
229 int srcx, srcy, dstx, dsty;
230
231 if (border)
232 goto fail;
233
234 /* Setup or redefine the texture object, mipmap tree and texture
235 * image. Don't populate yet.
236 */
237 ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
238 width, height, border,
239 GL_RGBA, CHAN_TYPE, NULL,
240 &ctx->DefaultPacking, texObj, texImage);
241
242 srcx = x;
243 srcy = y;
244 dstx = 0;
245 dsty = 0;
246 if (!_mesa_clip_copytexsubimage(ctx,
247 &dstx, &dsty,
248 &srcx, &srcy,
249 &width, &height))
250 return;
251
252 if (!do_copy_texsubimage(intel_context(ctx), target,
253 intel_texture_image(texImage),
254 internalFormat, 0, 0, x, y, width, height))
255 goto fail;
256
257 return;
258
259 fail:
260 _swrast_copy_teximage2d(ctx, target, level, internalFormat, x, y,
261 width, height, border);
262 }
263
264
265 void
266 intelCopyTexSubImage1D(GLcontext * ctx, GLenum target, GLint level,
267 GLint xoffset, GLint x, GLint y, GLsizei width)
268 {
269 struct gl_texture_unit *texUnit =
270 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
271 struct gl_texture_object *texObj =
272 _mesa_select_tex_object(ctx, texUnit, target);
273 struct gl_texture_image *texImage =
274 _mesa_select_tex_image(ctx, texObj, target, level);
275 GLenum internalFormat = texImage->InternalFormat;
276
277 /* XXX need to check <border> as in above function? */
278
279 /* Need to check texture is compatible with source format.
280 */
281
282 if (!do_copy_texsubimage(intel_context(ctx), target,
283 intel_texture_image(texImage),
284 internalFormat, xoffset, 0, x, y, width, 1)) {
285 _swrast_copy_texsubimage1d(ctx, target, level, xoffset, x, y, width);
286 }
287 }
288
289
290
291 void
292 intelCopyTexSubImage2D(GLcontext * ctx, GLenum target, GLint level,
293 GLint xoffset, GLint yoffset,
294 GLint x, GLint y, GLsizei width, GLsizei height)
295 {
296 struct gl_texture_unit *texUnit =
297 &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
298 struct gl_texture_object *texObj =
299 _mesa_select_tex_object(ctx, texUnit, target);
300 struct gl_texture_image *texImage =
301 _mesa_select_tex_image(ctx, texObj, target, level);
302 GLenum internalFormat = texImage->InternalFormat;
303
304
305 /* Need to check texture is compatible with source format.
306 */
307
308 if (!do_copy_texsubimage(intel_context(ctx), target,
309 intel_texture_image(texImage),
310 internalFormat,
311 xoffset, yoffset, x, y, width, height)) {
312
313 DBG("%s - fallback to swrast\n", __FUNCTION__);
314
315 _swrast_copy_texsubimage2d(ctx, target, level,
316 xoffset, yoffset, x, y, width, height);
317 }
318 }