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