i965: Add support for ARB_copy_image
[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_tex.h"
29 #include "intel_blit.h"
30 #include "intel_mipmap_tree.h"
31 #include "main/formats.h"
32 #include "drivers/common/meta.h"
33
34 static bool
35 copy_image_with_blitter(struct brw_context *brw,
36 struct intel_mipmap_tree *src_mt, int src_level,
37 int src_x, int src_y, int src_z,
38 struct intel_mipmap_tree *dst_mt, int dst_level,
39 int dst_x, int dst_y, int dst_z,
40 int src_width, int src_height)
41 {
42 GLuint bw, bh;
43 int cpp;
44
45 /* The blitter doesn't understand multisampling at all. */
46 if (src_mt->num_samples > 0 || dst_mt->num_samples > 0)
47 return false;
48
49 /* According to the Ivy Bridge PRM, Vol1 Part4, section 1.2.1.2 (Graphics
50 * Data Size Limitations):
51 *
52 * The BLT engine is capable of transferring very large quantities of
53 * graphics data. Any graphics data read from and written to the
54 * destination is permitted to represent a number of pixels that
55 * occupies up to 65,536 scan lines and up to 32,768 bytes per scan line
56 * at the destination. The maximum number of pixels that may be
57 * represented per scan line’s worth of graphics data depends on the
58 * color depth.
59 *
60 * Furthermore, intelEmitCopyBlit (which is called below) uses a signed
61 * 16-bit integer to represent buffer pitch, so it can only handle buffer
62 * pitches < 32k.
63 *
64 * As a result of these two limitations, we can only use the blitter to do
65 * this copy when the miptree's pitch is less than 32k.
66 */
67 if (src_mt->pitch >= 32768 ||
68 dst_mt->pitch >= 32768) {
69 perf_debug("Falling back due to >=32k pitch\n");
70 return false;
71 }
72
73 if (_mesa_is_format_compressed(src_mt->format)) {
74 _mesa_get_format_block_size(src_mt->format, &bw, &bh);
75
76 assert(src_x % bw == 0);
77 assert(src_y % bw == 0);
78 assert(src_width % bw == 0);
79 assert(src_height % bw == 0);
80
81 src_x /= (int)bw;
82 src_y /= (int)bw;
83 src_width /= (int)bw;
84 src_height /= (int)bw;
85
86 cpp = _mesa_get_format_bytes(src_mt->format);
87 } else {
88 cpp = src_mt->cpp;
89 }
90
91 if (_mesa_is_format_compressed(dst_mt->format)) {
92 _mesa_get_format_block_size(dst_mt->format, &bw, &bh);
93
94 assert(dst_x % bw == 0);
95 assert(dst_y % bw == 0);
96
97 dst_x /= (int)bw;
98 dst_y /= (int)bw;
99 }
100
101 uint32_t src_image_x, src_image_y;
102 intel_miptree_get_image_offset(src_mt, src_level, src_z,
103 &src_image_x, &src_image_y);
104 src_x += src_image_x;
105 src_y += src_image_y;
106
107 uint32_t dst_image_x, dst_image_y;
108 intel_miptree_get_image_offset(dst_mt, dst_level, dst_z,
109 &dst_image_x, &dst_image_y);
110 dst_x += dst_image_x;
111 dst_y += dst_image_y;
112
113 return intelEmitCopyBlit(brw,
114 cpp,
115 src_mt->pitch,
116 src_mt->bo, src_mt->offset,
117 src_mt->tiling,
118 dst_mt->pitch,
119 dst_mt->bo, dst_mt->offset,
120 dst_mt->tiling,
121 src_x, src_y,
122 dst_x, dst_y,
123 src_width, src_height,
124 GL_COPY);
125 }
126
127 static void
128 copy_image_with_memcpy(struct brw_context *brw,
129 struct intel_mipmap_tree *src_mt, int src_level,
130 int src_x, int src_y, int src_z,
131 struct intel_mipmap_tree *dst_mt, int dst_level,
132 int dst_x, int dst_y, int dst_z,
133 int src_width, int src_height)
134 {
135 bool same_slice;
136 uint8_t *mapped, *src_mapped, *dst_mapped;
137 int src_stride, dst_stride, i, cpp;
138 int map_x1, map_y1, map_x2, map_y2;
139 GLuint src_bw, src_bh;
140
141 cpp = _mesa_get_format_bytes(src_mt->format);
142 _mesa_get_format_block_size(src_mt->format, &src_bw, &src_bh);
143
144 assert(src_width % src_bw == 0);
145 assert(src_height % src_bw == 0);
146 assert(src_x % src_bw == 0);
147 assert(src_y % src_bw == 0);
148
149 /* If we are on the same miptree, same level, and same slice, then
150 * intel_miptree_map won't let us map it twice. We have to do things a
151 * bit differently. In particular, we do a single map large enough for
152 * both portions and in read-write mode.
153 */
154 same_slice = src_mt == dst_mt && src_level == dst_level && src_z == dst_z;
155
156 if (same_slice) {
157 assert(dst_x % src_bw == 0);
158 assert(dst_y % src_bw == 0);
159
160 map_x1 = MIN2(src_x, dst_x);
161 map_y1 = MIN2(src_y, dst_y);
162 map_x2 = MAX2(src_x, dst_x) + src_width;
163 map_y2 = MAX2(src_y, dst_y) + src_height;
164
165 intel_miptree_map(brw, src_mt, src_level, src_z,
166 map_x1, map_y1, map_x2 - map_x1, map_y2 - map_y1,
167 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
168 (void **)&mapped, &src_stride);
169
170 dst_stride = src_stride;
171
172 /* Set the offsets here so we don't have to think about while looping */
173 src_mapped = mapped + ((src_y - map_y1) / src_bh) * src_stride +
174 ((src_x - map_x1) / src_bw) * cpp;
175 dst_mapped = mapped + ((dst_y - map_y1) / src_bh) * dst_stride +
176 ((dst_x - map_x1) / src_bw) * cpp;
177 } else {
178 intel_miptree_map(brw, src_mt, src_level, src_z,
179 src_x, src_y, src_width, src_height,
180 GL_MAP_READ_BIT, (void **)&src_mapped, &src_stride);
181 intel_miptree_map(brw, dst_mt, dst_level, dst_z,
182 dst_x, dst_y, src_width, src_height,
183 GL_MAP_WRITE_BIT, (void **)&dst_mapped, &dst_stride);
184 }
185
186 src_width /= (int)src_bw;
187 src_height /= (int)src_bh;
188
189 for (i = 0; i < src_height; ++i) {
190 memcpy(dst_mapped, src_mapped, src_width * cpp);
191 src_mapped += src_stride;
192 dst_mapped += dst_stride;
193 }
194
195 if (same_slice) {
196 intel_miptree_unmap(brw, src_mt, src_level, src_z);
197 } else {
198 intel_miptree_unmap(brw, dst_mt, dst_level, dst_z);
199 intel_miptree_unmap(brw, src_mt, src_level, src_z);
200 }
201 }
202
203 static void
204 intel_copy_image_sub_data(struct gl_context *ctx,
205 struct gl_texture_image *src_image,
206 int src_x, int src_y, int src_z,
207 struct gl_texture_image *dst_image,
208 int dst_x, int dst_y, int dst_z,
209 int src_width, int src_height)
210 {
211 struct brw_context *brw = brw_context(ctx);
212 struct intel_texture_image *intel_src_image = intel_texture_image(src_image);
213 struct intel_texture_image *intel_dst_image = intel_texture_image(dst_image);
214
215 if (_mesa_meta_CopyImageSubData_uncompressed(ctx,
216 src_image, src_x, src_y, src_z,
217 dst_image, dst_x, dst_y, dst_z,
218 src_width, src_height)) {
219 return;
220 }
221
222 if (intel_src_image->mt->num_samples > 0 ||
223 intel_dst_image->mt->num_samples > 0) {
224 _mesa_problem(ctx, "Failed to copy multisampled texture with meta path\n");
225 return;
226 }
227
228 /* Cube maps actually have different images per face */
229 if (src_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
230 src_z = src_image->Face;
231 if (dst_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
232 dst_z = dst_image->Face;
233
234 /* We are now going to try and copy the texture using the blitter. If
235 * that fails, we will fall back mapping the texture and using memcpy.
236 * In either case, we need to do a full resolve.
237 */
238 intel_miptree_all_slices_resolve_hiz(brw, intel_src_image->mt);
239 intel_miptree_all_slices_resolve_depth(brw, intel_src_image->mt);
240 intel_miptree_resolve_color(brw, intel_src_image->mt);
241
242 intel_miptree_all_slices_resolve_hiz(brw, intel_dst_image->mt);
243 intel_miptree_all_slices_resolve_depth(brw, intel_dst_image->mt);
244 intel_miptree_resolve_color(brw, intel_dst_image->mt);
245
246 if (copy_image_with_blitter(brw, intel_src_image->mt, src_image->Level,
247 src_x, src_y, src_z,
248 intel_dst_image->mt, src_image->Level,
249 dst_x, dst_y, dst_z,
250 src_width, src_height))
251 return;
252
253 /* This is a worst-case scenario software fallback that maps the two
254 * textures and does a memcpy between them.
255 */
256 copy_image_with_memcpy(brw, intel_src_image->mt, src_image->Level,
257 src_x, src_y, src_z,
258 intel_dst_image->mt, src_image->Level,
259 dst_x, dst_y, dst_z,
260 src_width, src_height);
261 }
262
263 void
264 intelInitCopyImageFuncs(struct dd_function_table *functions)
265 {
266 functions->CopyImageSubData = intel_copy_image_sub_data;
267 }