i965: Make a helper function for CopyImage of a miptree.
[mesa.git] / src / mesa / drivers / dri / i965 / intel_copy_image.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2014 Intel Corporation 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 "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Jason Ekstrand <jason.ekstrand@intel.com>
26 */
27
28 #include "intel_fbo.h"
29 #include "intel_tex.h"
30 #include "intel_blit.h"
31 #include "intel_mipmap_tree.h"
32 #include "main/formats.h"
33 #include "main/teximage.h"
34 #include "drivers/common/meta.h"
35
36 static bool
37 copy_image_with_blitter(struct brw_context *brw,
38 struct intel_mipmap_tree *src_mt, int src_level,
39 int src_x, int src_y, int src_z,
40 struct intel_mipmap_tree *dst_mt, int dst_level,
41 int dst_x, int dst_y, int dst_z,
42 int src_width, int src_height)
43 {
44 GLuint bw, bh;
45 uint32_t src_image_x, src_image_y, dst_image_x, dst_image_y;
46
47 /* The blitter doesn't understand multisampling at all. */
48 if (src_mt->num_samples > 0 || dst_mt->num_samples > 0)
49 return false;
50
51 /* According to the Ivy Bridge PRM, Vol1 Part4, section 1.2.1.2 (Graphics
52 * Data Size Limitations):
53 *
54 * The BLT engine is capable of transferring very large quantities of
55 * graphics data. Any graphics data read from and written to the
56 * destination is permitted to represent a number of pixels that
57 * occupies up to 65,536 scan lines and up to 32,768 bytes per scan line
58 * at the destination. The maximum number of pixels that may be
59 * represented per scan line’s worth of graphics data depends on the
60 * color depth.
61 *
62 * Furthermore, intelEmitCopyBlit (which is called below) uses a signed
63 * 16-bit integer to represent buffer pitch, so it can only handle buffer
64 * pitches < 32k.
65 *
66 * As a result of these two limitations, we can only use the blitter to do
67 * this copy when the miptree's pitch is less than 32k.
68 */
69 if (src_mt->pitch >= 32768 ||
70 dst_mt->pitch >= 32768) {
71 perf_debug("Falling back due to >=32k pitch\n");
72 return false;
73 }
74
75 intel_miptree_get_image_offset(src_mt, src_level, src_z,
76 &src_image_x, &src_image_y);
77
78 if (_mesa_is_format_compressed(src_mt->format)) {
79 _mesa_get_format_block_size(src_mt->format, &bw, &bh);
80
81 assert(src_x % bw == 0);
82 assert(src_y % bh == 0);
83 assert(src_width % bw == 0);
84 assert(src_height % bh == 0);
85
86 src_x /= (int)bw;
87 src_y /= (int)bh;
88 src_width /= (int)bw;
89 src_height /= (int)bh;
90 }
91 src_x += src_image_x;
92 src_y += src_image_y;
93
94 intel_miptree_get_image_offset(dst_mt, dst_level, dst_z,
95 &dst_image_x, &dst_image_y);
96
97 if (_mesa_is_format_compressed(dst_mt->format)) {
98 _mesa_get_format_block_size(dst_mt->format, &bw, &bh);
99
100 assert(dst_x % bw == 0);
101 assert(dst_y % bh == 0);
102
103 dst_x /= (int)bw;
104 dst_y /= (int)bh;
105 }
106 dst_x += dst_image_x;
107 dst_y += dst_image_y;
108
109 return intelEmitCopyBlit(brw,
110 src_mt->cpp,
111 src_mt->pitch,
112 src_mt->bo, src_mt->offset,
113 src_mt->tiling,
114 src_mt->tr_mode,
115 dst_mt->pitch,
116 dst_mt->bo, dst_mt->offset,
117 dst_mt->tiling,
118 dst_mt->tr_mode,
119 src_x, src_y,
120 dst_x, dst_y,
121 src_width, src_height,
122 GL_COPY);
123 }
124
125 static void
126 copy_image_with_memcpy(struct brw_context *brw,
127 struct intel_mipmap_tree *src_mt, int src_level,
128 int src_x, int src_y, int src_z,
129 struct intel_mipmap_tree *dst_mt, int dst_level,
130 int dst_x, int dst_y, int dst_z,
131 int src_width, int src_height)
132 {
133 bool same_slice;
134 void *mapped, *src_mapped, *dst_mapped;
135 ptrdiff_t src_stride, dst_stride, cpp;
136 int map_x1, map_y1, map_x2, map_y2;
137 GLuint src_bw, src_bh;
138
139 cpp = _mesa_get_format_bytes(src_mt->format);
140 _mesa_get_format_block_size(src_mt->format, &src_bw, &src_bh);
141
142 assert(src_width % src_bw == 0);
143 assert(src_height % src_bh == 0);
144 assert(src_x % src_bw == 0);
145 assert(src_y % src_bh == 0);
146
147 /* If we are on the same miptree, same level, and same slice, then
148 * intel_miptree_map won't let us map it twice. We have to do things a
149 * bit differently. In particular, we do a single map large enough for
150 * both portions and in read-write mode.
151 */
152 same_slice = src_mt == dst_mt && src_level == dst_level && src_z == dst_z;
153
154 if (same_slice) {
155 assert(dst_x % src_bw == 0);
156 assert(dst_y % src_bh == 0);
157
158 map_x1 = MIN2(src_x, dst_x);
159 map_y1 = MIN2(src_y, dst_y);
160 map_x2 = MAX2(src_x, dst_x) + src_width;
161 map_y2 = MAX2(src_y, dst_y) + src_height;
162
163 intel_miptree_map(brw, src_mt, src_level, src_z,
164 map_x1, map_y1, map_x2 - map_x1, map_y2 - map_y1,
165 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
166 &mapped, &src_stride);
167
168 dst_stride = src_stride;
169
170 /* Set the offsets here so we don't have to think about while looping */
171 src_mapped = mapped + ((src_y - map_y1) / src_bh) * src_stride +
172 ((src_x - map_x1) / src_bw) * cpp;
173 dst_mapped = mapped + ((dst_y - map_y1) / src_bh) * dst_stride +
174 ((dst_x - map_x1) / src_bw) * cpp;
175 } else {
176 intel_miptree_map(brw, src_mt, src_level, src_z,
177 src_x, src_y, src_width, src_height,
178 GL_MAP_READ_BIT, &src_mapped, &src_stride);
179 intel_miptree_map(brw, dst_mt, dst_level, dst_z,
180 dst_x, dst_y, src_width, src_height,
181 GL_MAP_WRITE_BIT, &dst_mapped, &dst_stride);
182 }
183
184 src_width /= (int)src_bw;
185 src_height /= (int)src_bh;
186
187 for (int i = 0; i < src_height; ++i) {
188 memcpy(dst_mapped, src_mapped, src_width * cpp);
189 src_mapped += src_stride;
190 dst_mapped += dst_stride;
191 }
192
193 if (same_slice) {
194 intel_miptree_unmap(brw, src_mt, src_level, src_z);
195 } else {
196 intel_miptree_unmap(brw, dst_mt, dst_level, dst_z);
197 intel_miptree_unmap(brw, src_mt, src_level, src_z);
198 }
199 }
200
201 static void
202 copy_miptrees(struct brw_context *brw,
203 struct intel_mipmap_tree *src_mt,
204 int src_x, int src_y, int src_z, unsigned src_level,
205 struct intel_mipmap_tree *dst_mt,
206 int dst_x, int dst_y, int dst_z, unsigned dst_level,
207 int src_width, int src_height)
208 {
209 unsigned bw, bh;
210
211 /* We are now going to try and copy the texture using the blitter. If
212 * that fails, we will fall back mapping the texture and using memcpy.
213 * In either case, we need to do a full resolve.
214 */
215 intel_miptree_all_slices_resolve_hiz(brw, src_mt);
216 intel_miptree_all_slices_resolve_depth(brw, src_mt);
217 intel_miptree_resolve_color(brw, src_mt, 0);
218
219 intel_miptree_all_slices_resolve_hiz(brw, dst_mt);
220 intel_miptree_all_slices_resolve_depth(brw, dst_mt);
221 intel_miptree_resolve_color(brw, dst_mt, 0);
222
223 _mesa_get_format_block_size(src_mt->format, &bw, &bh);
224
225 /* It's legal to have a WxH that's smaller than a compressed block. This
226 * happens for example when you are using a higher level LOD. For this case,
227 * we still want to copy the entire block, or else the decompression will be
228 * incorrect.
229 */
230 if (src_width < bw)
231 src_width = ALIGN_NPOT(src_width, bw);
232
233 if (src_height < bh)
234 src_height = ALIGN_NPOT(src_height, bh);
235
236 if (copy_image_with_blitter(brw, src_mt, src_level,
237 src_x, src_y, src_z,
238 dst_mt, dst_level,
239 dst_x, dst_y, dst_z,
240 src_width, src_height))
241 return;
242
243 /* This is a worst-case scenario software fallback that maps the two
244 * textures and does a memcpy between them.
245 */
246 copy_image_with_memcpy(brw, src_mt, src_level,
247 src_x, src_y, src_z,
248 dst_mt, dst_level,
249 dst_x, dst_y, dst_z,
250 src_width, src_height);
251 }
252
253 static void
254 intel_copy_image_sub_data(struct gl_context *ctx,
255 struct gl_texture_image *src_image,
256 struct gl_renderbuffer *src_renderbuffer,
257 int src_x, int src_y, int src_z,
258 struct gl_texture_image *dst_image,
259 struct gl_renderbuffer *dst_renderbuffer,
260 int dst_x, int dst_y, int dst_z,
261 int src_width, int src_height)
262 {
263 struct brw_context *brw = brw_context(ctx);
264 struct intel_mipmap_tree *src_mt, *dst_mt;
265 unsigned src_level, dst_level;
266
267 if (_mesa_meta_CopyImageSubData_uncompressed(ctx,
268 src_image, src_renderbuffer,
269 src_x, src_y, src_z,
270 dst_image, dst_renderbuffer,
271 dst_x, dst_y, dst_z,
272 src_width, src_height)) {
273 return;
274 }
275
276 if (src_image) {
277 src_mt = intel_texture_image(src_image)->mt;
278 src_level = src_image->Level + src_image->TexObject->MinLevel;
279
280 /* Cube maps actually have different images per face */
281 if (src_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
282 src_z = src_image->Face;
283
284 src_z += src_image->TexObject->MinLayer;
285 } else {
286 assert(src_renderbuffer);
287 src_mt = intel_renderbuffer(src_renderbuffer)->mt;
288 src_image = src_renderbuffer->TexImage;
289 src_level = 0;
290 }
291
292 if (dst_image) {
293 dst_mt = intel_texture_image(dst_image)->mt;
294
295 dst_level = dst_image->Level + dst_image->TexObject->MinLevel;
296
297 /* Cube maps actually have different images per face */
298 if (dst_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
299 dst_z = dst_image->Face;
300
301 dst_z += dst_image->TexObject->MinLayer;
302 } else {
303 assert(dst_renderbuffer);
304 dst_mt = intel_renderbuffer(dst_renderbuffer)->mt;
305 dst_image = dst_renderbuffer->TexImage;
306 dst_level = 0;
307 }
308
309 if (src_mt->num_samples > 0 || dst_mt->num_samples > 0) {
310 _mesa_problem(ctx, "Failed to copy multisampled texture with BLORP\n");
311 return;
312 }
313
314 copy_miptrees(brw, src_mt, src_x, src_y, src_z, src_level,
315 dst_mt, dst_x, dst_y, dst_z, dst_level,
316 src_width, src_height);
317 }
318
319 void
320 intelInitCopyImageFuncs(struct dd_function_table *functions)
321 {
322 functions->CopyImageSubData = intel_copy_image_sub_data;
323 }