anv/formats: Add an anv_get_format helper
[mesa.git] / src / intel / vulkan / anv_meta_copy.c
1 /*
2 * Copyright © 2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "anv_meta.h"
25
26 static VkExtent3D
27 meta_image_block_size(const struct anv_image *image)
28 {
29 if (image->aspects == VK_IMAGE_ASPECT_COLOR_BIT) {
30 const struct isl_format_layout *isl_layout =
31 isl_format_get_layout(image->color_surface.isl.format);
32 return (VkExtent3D) { isl_layout->bw, isl_layout->bh, isl_layout->bd };
33 } else {
34 return (VkExtent3D) { 1, 1, 1 };
35 }
36 }
37
38 /* Returns the user-provided VkBufferImageCopy::imageExtent in units of
39 * elements rather than texels. One element equals one texel or one block
40 * if Image is uncompressed or compressed, respectively.
41 */
42 static struct VkExtent3D
43 meta_region_extent_el(const struct anv_image *image,
44 const struct VkExtent3D *extent)
45 {
46 const VkExtent3D block = meta_image_block_size(image);
47 return anv_sanitize_image_extent(image->type, (VkExtent3D) {
48 .width = DIV_ROUND_UP(extent->width , block.width),
49 .height = DIV_ROUND_UP(extent->height, block.height),
50 .depth = DIV_ROUND_UP(extent->depth , block.depth),
51 });
52 }
53
54 /* Returns the user-provided VkBufferImageCopy::imageOffset in units of
55 * elements rather than texels. One element equals one texel or one block
56 * if Image is uncompressed or compressed, respectively.
57 */
58 static struct VkOffset3D
59 meta_region_offset_el(const struct anv_image *image,
60 const struct VkOffset3D *offset)
61 {
62 const VkExtent3D block = meta_image_block_size(image);
63 return anv_sanitize_image_offset(image->type, (VkOffset3D) {
64 .x = offset->x / block.width,
65 .y = offset->y / block.height,
66 .z = offset->z / block.depth,
67 });
68 }
69
70 static struct anv_meta_blit2d_surf
71 blit_surf_for_image(const struct anv_image* image,
72 const struct isl_surf *img_isl_surf)
73 {
74 return (struct anv_meta_blit2d_surf) {
75 .bo = image->bo,
76 .tiling = img_isl_surf->tiling,
77 .base_offset = image->offset,
78 .bs = isl_format_get_layout(img_isl_surf->format)->bs,
79 .pitch = isl_surf_get_row_pitch(img_isl_surf),
80 };
81 }
82
83 static void
84 do_buffer_copy(struct anv_cmd_buffer *cmd_buffer,
85 struct anv_bo *src, uint64_t src_offset,
86 struct anv_bo *dest, uint64_t dest_offset,
87 int width, int height, int bs)
88 {
89 struct anv_meta_blit2d_surf b_src = {
90 .bo = src,
91 .tiling = ISL_TILING_LINEAR,
92 .base_offset = src_offset,
93 .bs = bs,
94 .pitch = width * bs,
95 };
96 struct anv_meta_blit2d_surf b_dst = {
97 .bo = dest,
98 .tiling = ISL_TILING_LINEAR,
99 .base_offset = dest_offset,
100 .bs = bs,
101 .pitch = width * bs,
102 };
103 struct anv_meta_blit2d_rect rect = {
104 .width = width,
105 .height = height,
106 };
107 anv_meta_blit2d(cmd_buffer, &b_src, &b_dst, 1, &rect);
108 }
109
110 static void
111 meta_copy_buffer_to_image(struct anv_cmd_buffer *cmd_buffer,
112 struct anv_buffer* buffer,
113 struct anv_image* image,
114 uint32_t regionCount,
115 const VkBufferImageCopy* pRegions,
116 bool forward)
117 {
118 struct anv_meta_saved_state saved_state;
119
120 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
121 * VK_SAMPLE_COUNT_1_BIT."
122 */
123 assert(image->samples == 1);
124
125 anv_meta_begin_blit2d(cmd_buffer, &saved_state);
126
127 for (unsigned r = 0; r < regionCount; r++) {
128
129 /**
130 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
131 * extent is the size in texels of the source image to copy in width,
132 * height and depth. 1D images use only x and width. 2D images use x, y,
133 * width and height. 3D images use x, y, z, width, height and depth.
134 *
135 *
136 * Also, convert the offsets and extent from units of texels to units of
137 * blocks - which is the highest resolution accessible in this command.
138 */
139 const VkOffset3D img_offset_el =
140 meta_region_offset_el(image, &pRegions[r].imageOffset);
141 const VkExtent3D bufferExtent = {
142 .width = pRegions[r].bufferRowLength ?
143 pRegions[r].bufferRowLength : pRegions[r].imageExtent.width,
144 .height = pRegions[r].bufferImageHeight ?
145 pRegions[r].bufferImageHeight : pRegions[r].imageExtent.height,
146 };
147 const VkExtent3D buf_extent_el =
148 meta_region_extent_el(image, &bufferExtent);
149
150 /* Start creating blit rect */
151 const VkExtent3D img_extent_el =
152 meta_region_extent_el(image, &pRegions[r].imageExtent);
153 struct anv_meta_blit2d_rect rect = {
154 .width = img_extent_el.width,
155 .height = img_extent_el.height,
156 };
157
158 /* Create blit surfaces */
159 VkImageAspectFlags aspect = pRegions[r].imageSubresource.aspectMask;
160 const struct isl_surf *img_isl_surf =
161 &anv_image_get_surface_for_aspect_mask(image, aspect)->isl;
162 struct anv_meta_blit2d_surf img_bsurf =
163 blit_surf_for_image(image, img_isl_surf);
164 enum isl_format buf_format = anv_get_isl_format(image->vk_format, aspect,
165 VK_IMAGE_TILING_LINEAR);
166 struct anv_meta_blit2d_surf buf_bsurf = {
167 .bo = buffer->bo,
168 .tiling = ISL_TILING_LINEAR,
169 .base_offset = buffer->offset + pRegions[r].bufferOffset,
170 .bs = isl_format_get_layout(buf_format)->bs,
171 .pitch = buf_extent_el.width * buf_bsurf.bs,
172 };
173
174 /* Set direction-dependent variables */
175 struct anv_meta_blit2d_surf *dst_bsurf = forward ? &img_bsurf : &buf_bsurf;
176 struct anv_meta_blit2d_surf *src_bsurf = forward ? &buf_bsurf : &img_bsurf;
177 uint32_t *x_offset = forward ? &rect.dst_x : &rect.src_x;
178 uint32_t *y_offset = forward ? &rect.dst_y : &rect.src_y;
179
180 /* Loop through each 3D or array slice */
181 unsigned num_slices_3d = img_extent_el.depth;
182 unsigned num_slices_array = pRegions[r].imageSubresource.layerCount;
183 unsigned slice_3d = 0;
184 unsigned slice_array = 0;
185 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
186
187 /* Finish creating blit rect */
188 isl_surf_get_image_offset_el(img_isl_surf,
189 pRegions[r].imageSubresource.mipLevel,
190 pRegions[r].imageSubresource.baseArrayLayer
191 + slice_array,
192 img_offset_el.z + slice_3d,
193 x_offset,
194 y_offset);
195 *x_offset += img_offset_el.x;
196 *y_offset += img_offset_el.y;
197
198 /* Perform Blit */
199 anv_meta_blit2d(cmd_buffer, src_bsurf, dst_bsurf, 1, &rect);
200
201 /* Once we've done the blit, all of the actual information about
202 * the image is embedded in the command buffer so we can just
203 * increment the offset directly in the image effectively
204 * re-binding it to different backing memory.
205 */
206 buf_bsurf.base_offset += buf_extent_el.width *
207 buf_extent_el.height * buf_bsurf.bs;
208
209 if (image->type == VK_IMAGE_TYPE_3D)
210 slice_3d++;
211 else
212 slice_array++;
213 }
214 }
215 anv_meta_end_blit2d(cmd_buffer, &saved_state);
216 }
217
218 void anv_CmdCopyBufferToImage(
219 VkCommandBuffer commandBuffer,
220 VkBuffer srcBuffer,
221 VkImage destImage,
222 VkImageLayout destImageLayout,
223 uint32_t regionCount,
224 const VkBufferImageCopy* pRegions)
225 {
226 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
227 ANV_FROM_HANDLE(anv_image, dest_image, destImage);
228 ANV_FROM_HANDLE(anv_buffer, src_buffer, srcBuffer);
229
230 meta_copy_buffer_to_image(cmd_buffer, src_buffer, dest_image,
231 regionCount, pRegions, true);
232 }
233
234 void anv_CmdCopyImageToBuffer(
235 VkCommandBuffer commandBuffer,
236 VkImage srcImage,
237 VkImageLayout srcImageLayout,
238 VkBuffer destBuffer,
239 uint32_t regionCount,
240 const VkBufferImageCopy* pRegions)
241 {
242 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
243 ANV_FROM_HANDLE(anv_image, src_image, srcImage);
244 ANV_FROM_HANDLE(anv_buffer, dst_buffer, destBuffer);
245
246 meta_copy_buffer_to_image(cmd_buffer, dst_buffer, src_image,
247 regionCount, pRegions, false);
248 }
249
250 void anv_CmdCopyImage(
251 VkCommandBuffer commandBuffer,
252 VkImage srcImage,
253 VkImageLayout srcImageLayout,
254 VkImage destImage,
255 VkImageLayout destImageLayout,
256 uint32_t regionCount,
257 const VkImageCopy* pRegions)
258 {
259 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
260 ANV_FROM_HANDLE(anv_image, src_image, srcImage);
261 ANV_FROM_HANDLE(anv_image, dest_image, destImage);
262 struct anv_meta_saved_state saved_state;
263
264 /* From the Vulkan 1.0 spec:
265 *
266 * vkCmdCopyImage can be used to copy image data between multisample
267 * images, but both images must have the same number of samples.
268 */
269 assert(src_image->samples == dest_image->samples);
270
271 anv_meta_begin_blit2d(cmd_buffer, &saved_state);
272
273 for (unsigned r = 0; r < regionCount; r++) {
274 assert(pRegions[r].srcSubresource.aspectMask ==
275 pRegions[r].dstSubresource.aspectMask);
276
277 VkImageAspectFlags aspect = pRegions[r].srcSubresource.aspectMask;
278
279 /* Create blit surfaces */
280 struct isl_surf *src_isl_surf =
281 &anv_image_get_surface_for_aspect_mask(src_image, aspect)->isl;
282 struct isl_surf *dst_isl_surf =
283 &anv_image_get_surface_for_aspect_mask(dest_image, aspect)->isl;
284 struct anv_meta_blit2d_surf b_src =
285 blit_surf_for_image(src_image, src_isl_surf);
286 struct anv_meta_blit2d_surf b_dst =
287 blit_surf_for_image(dest_image, dst_isl_surf);
288
289 /**
290 * From the Vulkan 1.0.6 spec: 18.4 Copying Data Between Buffers and Images
291 * imageExtent is the size in texels of the image to copy in width, height
292 * and depth. 1D images use only x and width. 2D images use x, y, width
293 * and height. 3D images use x, y, z, width, height and depth.
294 *
295 * Also, convert the offsets and extent from units of texels to units of
296 * blocks - which is the highest resolution accessible in this command.
297 */
298 const VkOffset3D dst_offset_el =
299 meta_region_offset_el(dest_image, &pRegions[r].dstOffset);
300 const VkOffset3D src_offset_el =
301 meta_region_offset_el(src_image, &pRegions[r].srcOffset);
302 const VkExtent3D img_extent_el =
303 meta_region_extent_el(src_image, &pRegions[r].extent);
304
305 /* Start creating blit rect */
306 struct anv_meta_blit2d_rect rect = {
307 .width = img_extent_el.width,
308 .height = img_extent_el.height,
309 };
310
311 /* Loop through each 3D or array slice */
312 unsigned num_slices_3d = img_extent_el.depth;
313 unsigned num_slices_array = pRegions[r].dstSubresource.layerCount;
314 unsigned slice_3d = 0;
315 unsigned slice_array = 0;
316 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
317
318 /* Finish creating blit rect */
319 isl_surf_get_image_offset_el(dst_isl_surf,
320 pRegions[r].dstSubresource.mipLevel,
321 pRegions[r].dstSubresource.baseArrayLayer
322 + slice_array,
323 dst_offset_el.z + slice_3d,
324 &rect.dst_x,
325 &rect.dst_y);
326 isl_surf_get_image_offset_el(src_isl_surf,
327 pRegions[r].srcSubresource.mipLevel,
328 pRegions[r].srcSubresource.baseArrayLayer
329 + slice_array,
330 src_offset_el.z + slice_3d,
331 &rect.src_x,
332 &rect.src_y);
333 rect.dst_x += dst_offset_el.x;
334 rect.dst_y += dst_offset_el.y;
335 rect.src_x += src_offset_el.x;
336 rect.src_y += src_offset_el.y;
337
338 /* Perform Blit */
339 anv_meta_blit2d(cmd_buffer, &b_src, &b_dst, 1, &rect);
340
341 if (dest_image->type == VK_IMAGE_TYPE_3D)
342 slice_3d++;
343 else
344 slice_array++;
345 }
346 }
347
348 anv_meta_end_blit2d(cmd_buffer, &saved_state);
349 }
350
351 void anv_CmdCopyBuffer(
352 VkCommandBuffer commandBuffer,
353 VkBuffer srcBuffer,
354 VkBuffer destBuffer,
355 uint32_t regionCount,
356 const VkBufferCopy* pRegions)
357 {
358 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
359 ANV_FROM_HANDLE(anv_buffer, src_buffer, srcBuffer);
360 ANV_FROM_HANDLE(anv_buffer, dest_buffer, destBuffer);
361
362 struct anv_meta_saved_state saved_state;
363
364 anv_meta_begin_blit2d(cmd_buffer, &saved_state);
365
366 for (unsigned r = 0; r < regionCount; r++) {
367 uint64_t src_offset = src_buffer->offset + pRegions[r].srcOffset;
368 uint64_t dest_offset = dest_buffer->offset + pRegions[r].dstOffset;
369 uint64_t copy_size = pRegions[r].size;
370
371 /* First, we compute the biggest format that can be used with the
372 * given offsets and size.
373 */
374 int bs = 16;
375
376 int fs = ffs(src_offset) - 1;
377 if (fs != -1)
378 bs = MIN2(bs, 1 << fs);
379 assert(src_offset % bs == 0);
380
381 fs = ffs(dest_offset) - 1;
382 if (fs != -1)
383 bs = MIN2(bs, 1 << fs);
384 assert(dest_offset % bs == 0);
385
386 fs = ffs(pRegions[r].size) - 1;
387 if (fs != -1)
388 bs = MIN2(bs, 1 << fs);
389 assert(pRegions[r].size % bs == 0);
390
391 /* This is maximum possible width/height our HW can handle */
392 uint64_t max_surface_dim = 1 << 14;
393
394 /* First, we make a bunch of max-sized copies */
395 uint64_t max_copy_size = max_surface_dim * max_surface_dim * bs;
396 while (copy_size >= max_copy_size) {
397 do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
398 dest_buffer->bo, dest_offset,
399 max_surface_dim, max_surface_dim, bs);
400 copy_size -= max_copy_size;
401 src_offset += max_copy_size;
402 dest_offset += max_copy_size;
403 }
404
405 uint64_t height = copy_size / (max_surface_dim * bs);
406 assert(height < max_surface_dim);
407 if (height != 0) {
408 uint64_t rect_copy_size = height * max_surface_dim * bs;
409 do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
410 dest_buffer->bo, dest_offset,
411 max_surface_dim, height, bs);
412 copy_size -= rect_copy_size;
413 src_offset += rect_copy_size;
414 dest_offset += rect_copy_size;
415 }
416
417 if (copy_size != 0) {
418 do_buffer_copy(cmd_buffer, src_buffer->bo, src_offset,
419 dest_buffer->bo, dest_offset,
420 copy_size / bs, 1, bs);
421 }
422 }
423
424 anv_meta_end_blit2d(cmd_buffer, &saved_state);
425 }
426
427 void anv_CmdUpdateBuffer(
428 VkCommandBuffer commandBuffer,
429 VkBuffer dstBuffer,
430 VkDeviceSize dstOffset,
431 VkDeviceSize dataSize,
432 const uint32_t* pData)
433 {
434 ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
435 ANV_FROM_HANDLE(anv_buffer, dst_buffer, dstBuffer);
436 struct anv_meta_saved_state saved_state;
437
438 anv_meta_begin_blit2d(cmd_buffer, &saved_state);
439
440 /* We can't quite grab a full block because the state stream needs a
441 * little data at the top to build its linked list.
442 */
443 const uint32_t max_update_size =
444 cmd_buffer->device->dynamic_state_block_pool.block_size - 64;
445
446 assert(max_update_size < (1 << 14) * 4);
447
448 while (dataSize) {
449 const uint32_t copy_size = MIN2(dataSize, max_update_size);
450
451 struct anv_state tmp_data =
452 anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, copy_size, 64);
453
454 memcpy(tmp_data.map, pData, copy_size);
455
456 int bs;
457 if ((copy_size & 15) == 0 && (dstOffset & 15) == 0) {
458 bs = 16;
459 } else if ((copy_size & 7) == 0 && (dstOffset & 7) == 0) {
460 bs = 8;
461 } else {
462 assert((copy_size & 3) == 0 && (dstOffset & 3) == 0);
463 bs = 4;
464 }
465
466 do_buffer_copy(cmd_buffer,
467 &cmd_buffer->device->dynamic_state_block_pool.bo,
468 tmp_data.offset,
469 dst_buffer->bo, dst_buffer->offset + dstOffset,
470 copy_size / bs, 1, bs);
471
472 dataSize -= copy_size;
473 dstOffset += copy_size;
474 pData = (void *)pData + copy_size;
475 }
476
477 anv_meta_end_blit2d(cmd_buffer, &saved_state);
478 }