radv: add a workaround for Monster Hunter World and LLVM 7&8
[mesa.git] / src / amd / vulkan / radv_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 "radv_meta.h"
25 #include "vk_format.h"
26
27 static VkExtent3D
28 meta_image_block_size(const struct radv_image *image)
29 {
30 const struct vk_format_description *desc = vk_format_description(image->vk_format);
31 return (VkExtent3D) { desc->block.width, desc->block.height, 1 };
32 }
33
34 /* Returns the user-provided VkBufferImageCopy::imageExtent in units of
35 * elements rather than texels. One element equals one texel or one block
36 * if Image is uncompressed or compressed, respectively.
37 */
38 static struct VkExtent3D
39 meta_region_extent_el(const struct radv_image *image,
40 const VkImageType imageType,
41 const struct VkExtent3D *extent)
42 {
43 const VkExtent3D block = meta_image_block_size(image);
44 return radv_sanitize_image_extent(imageType, (VkExtent3D) {
45 .width = DIV_ROUND_UP(extent->width , block.width),
46 .height = DIV_ROUND_UP(extent->height, block.height),
47 .depth = DIV_ROUND_UP(extent->depth , block.depth),
48 });
49 }
50
51 /* Returns the user-provided VkBufferImageCopy::imageOffset in units of
52 * elements rather than texels. One element equals one texel or one block
53 * if Image is uncompressed or compressed, respectively.
54 */
55 static struct VkOffset3D
56 meta_region_offset_el(const struct radv_image *image,
57 const struct VkOffset3D *offset)
58 {
59 const VkExtent3D block = meta_image_block_size(image);
60 return radv_sanitize_image_offset(image->type, (VkOffset3D) {
61 .x = offset->x / block.width,
62 .y = offset->y / block.height,
63 .z = offset->z / block.depth,
64 });
65 }
66
67 static VkFormat
68 vk_format_for_size(int bs)
69 {
70 switch (bs) {
71 case 1: return VK_FORMAT_R8_UINT;
72 case 2: return VK_FORMAT_R8G8_UINT;
73 case 4: return VK_FORMAT_R8G8B8A8_UINT;
74 case 8: return VK_FORMAT_R16G16B16A16_UINT;
75 case 12: return VK_FORMAT_R32G32B32_UINT;
76 case 16: return VK_FORMAT_R32G32B32A32_UINT;
77 default:
78 unreachable("Invalid format block size");
79 }
80 }
81
82 static struct radv_meta_blit2d_surf
83 blit_surf_for_image_level_layer(struct radv_image *image,
84 VkImageLayout layout,
85 const VkImageSubresourceLayers *subres,
86 VkImageAspectFlags aspect_mask)
87 {
88 VkFormat format = radv_get_aspect_format(image, aspect_mask);
89
90 if (!radv_image_has_dcc(image) &&
91 !(radv_image_is_tc_compat_htile(image)))
92 format = vk_format_for_size(vk_format_get_blocksize(format));
93
94 format = vk_format_no_srgb(format);
95
96 return (struct radv_meta_blit2d_surf) {
97 .format = format,
98 .bs = vk_format_get_blocksize(format),
99 .level = subres->mipLevel,
100 .layer = subres->baseArrayLayer,
101 .image = image,
102 .aspect_mask = aspect_mask,
103 .current_layout = layout,
104 };
105 }
106
107 static bool
108 image_is_renderable(struct radv_device *device, struct radv_image *image)
109 {
110 if (image->vk_format == VK_FORMAT_R32G32B32_UINT ||
111 image->vk_format == VK_FORMAT_R32G32B32_SINT ||
112 image->vk_format == VK_FORMAT_R32G32B32_SFLOAT)
113 return false;
114
115 if (device->physical_device->rad_info.chip_class >= GFX9 &&
116 image->type == VK_IMAGE_TYPE_3D &&
117 vk_format_get_blocksizebits(image->vk_format) == 128 &&
118 vk_format_is_compressed(image->vk_format))
119 return false;
120 return true;
121 }
122
123 static void
124 meta_copy_buffer_to_image(struct radv_cmd_buffer *cmd_buffer,
125 struct radv_buffer* buffer,
126 struct radv_image* image,
127 VkImageLayout layout,
128 uint32_t regionCount,
129 const VkBufferImageCopy* pRegions)
130 {
131 bool cs = cmd_buffer->queue_family_index == RADV_QUEUE_COMPUTE;
132 struct radv_meta_saved_state saved_state;
133 bool old_predicating;
134
135 /* The Vulkan 1.0 spec says "dstImage must have a sample count equal to
136 * VK_SAMPLE_COUNT_1_BIT."
137 */
138 assert(image->info.samples == 1);
139
140 radv_meta_save(&saved_state, cmd_buffer,
141 (cs ? RADV_META_SAVE_COMPUTE_PIPELINE :
142 RADV_META_SAVE_GRAPHICS_PIPELINE) |
143 RADV_META_SAVE_CONSTANTS |
144 RADV_META_SAVE_DESCRIPTORS);
145
146 /* VK_EXT_conditional_rendering says that copy commands should not be
147 * affected by conditional rendering.
148 */
149 old_predicating = cmd_buffer->state.predicating;
150 cmd_buffer->state.predicating = false;
151
152 for (unsigned r = 0; r < regionCount; r++) {
153
154 /**
155 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
156 * extent is the size in texels of the source image to copy in width,
157 * height and depth. 1D images use only x and width. 2D images use x, y,
158 * width and height. 3D images use x, y, z, width, height and depth.
159 *
160 *
161 * Also, convert the offsets and extent from units of texels to units of
162 * blocks - which is the highest resolution accessible in this command.
163 */
164 const VkOffset3D img_offset_el =
165 meta_region_offset_el(image, &pRegions[r].imageOffset);
166 const VkExtent3D bufferExtent = {
167 .width = pRegions[r].bufferRowLength ?
168 pRegions[r].bufferRowLength : pRegions[r].imageExtent.width,
169 .height = pRegions[r].bufferImageHeight ?
170 pRegions[r].bufferImageHeight : pRegions[r].imageExtent.height,
171 };
172 const VkExtent3D buf_extent_el =
173 meta_region_extent_el(image, image->type, &bufferExtent);
174
175 /* Start creating blit rect */
176 const VkExtent3D img_extent_el =
177 meta_region_extent_el(image, image->type, &pRegions[r].imageExtent);
178 struct radv_meta_blit2d_rect rect = {
179 .width = img_extent_el.width,
180 .height = img_extent_el.height,
181 };
182
183 /* Create blit surfaces */
184 struct radv_meta_blit2d_surf img_bsurf =
185 blit_surf_for_image_level_layer(image,
186 layout,
187 &pRegions[r].imageSubresource,
188 pRegions[r].imageSubresource.aspectMask);
189
190 struct radv_meta_blit2d_buffer buf_bsurf = {
191 .bs = img_bsurf.bs,
192 .format = img_bsurf.format,
193 .buffer = buffer,
194 .offset = pRegions[r].bufferOffset,
195 .pitch = buf_extent_el.width,
196 };
197
198 if (image->type == VK_IMAGE_TYPE_3D)
199 img_bsurf.layer = img_offset_el.z;
200 /* Loop through each 3D or array slice */
201 unsigned num_slices_3d = img_extent_el.depth;
202 unsigned num_slices_array = pRegions[r].imageSubresource.layerCount;
203 unsigned slice_3d = 0;
204 unsigned slice_array = 0;
205 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
206
207 rect.dst_x = img_offset_el.x;
208 rect.dst_y = img_offset_el.y;
209
210
211 /* Perform Blit */
212 if (cs ||
213 !image_is_renderable(cmd_buffer->device, img_bsurf.image)) {
214 radv_meta_buffer_to_image_cs(cmd_buffer, &buf_bsurf, &img_bsurf, 1, &rect);
215 } else {
216 radv_meta_blit2d(cmd_buffer, NULL, &buf_bsurf, &img_bsurf, 1, &rect);
217 }
218
219 /* Once we've done the blit, all of the actual information about
220 * the image is embedded in the command buffer so we can just
221 * increment the offset directly in the image effectively
222 * re-binding it to different backing memory.
223 */
224 buf_bsurf.offset += buf_extent_el.width *
225 buf_extent_el.height * buf_bsurf.bs;
226 img_bsurf.layer++;
227 if (image->type == VK_IMAGE_TYPE_3D)
228 slice_3d++;
229 else
230 slice_array++;
231 }
232 }
233
234 /* Restore conditional rendering. */
235 cmd_buffer->state.predicating = old_predicating;
236
237 radv_meta_restore(&saved_state, cmd_buffer);
238 }
239
240 void radv_CmdCopyBufferToImage(
241 VkCommandBuffer commandBuffer,
242 VkBuffer srcBuffer,
243 VkImage destImage,
244 VkImageLayout destImageLayout,
245 uint32_t regionCount,
246 const VkBufferImageCopy* pRegions)
247 {
248 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
249 RADV_FROM_HANDLE(radv_image, dest_image, destImage);
250 RADV_FROM_HANDLE(radv_buffer, src_buffer, srcBuffer);
251
252 meta_copy_buffer_to_image(cmd_buffer, src_buffer, dest_image, destImageLayout,
253 regionCount, pRegions);
254 }
255
256 static void
257 meta_copy_image_to_buffer(struct radv_cmd_buffer *cmd_buffer,
258 struct radv_buffer* buffer,
259 struct radv_image* image,
260 VkImageLayout layout,
261 uint32_t regionCount,
262 const VkBufferImageCopy* pRegions)
263 {
264 struct radv_meta_saved_state saved_state;
265 bool old_predicating;
266
267 radv_meta_save(&saved_state, cmd_buffer,
268 RADV_META_SAVE_COMPUTE_PIPELINE |
269 RADV_META_SAVE_CONSTANTS |
270 RADV_META_SAVE_DESCRIPTORS);
271
272 /* VK_EXT_conditional_rendering says that copy commands should not be
273 * affected by conditional rendering.
274 */
275 old_predicating = cmd_buffer->state.predicating;
276 cmd_buffer->state.predicating = false;
277
278 for (unsigned r = 0; r < regionCount; r++) {
279
280 /**
281 * From the Vulkan 1.0.6 spec: 18.3 Copying Data Between Images
282 * extent is the size in texels of the source image to copy in width,
283 * height and depth. 1D images use only x and width. 2D images use x, y,
284 * width and height. 3D images use x, y, z, width, height and depth.
285 *
286 *
287 * Also, convert the offsets and extent from units of texels to units of
288 * blocks - which is the highest resolution accessible in this command.
289 */
290 const VkOffset3D img_offset_el =
291 meta_region_offset_el(image, &pRegions[r].imageOffset);
292 const VkExtent3D bufferExtent = {
293 .width = pRegions[r].bufferRowLength ?
294 pRegions[r].bufferRowLength : pRegions[r].imageExtent.width,
295 .height = pRegions[r].bufferImageHeight ?
296 pRegions[r].bufferImageHeight : pRegions[r].imageExtent.height,
297 };
298 const VkExtent3D buf_extent_el =
299 meta_region_extent_el(image, image->type, &bufferExtent);
300
301 /* Start creating blit rect */
302 const VkExtent3D img_extent_el =
303 meta_region_extent_el(image, image->type, &pRegions[r].imageExtent);
304 struct radv_meta_blit2d_rect rect = {
305 .width = img_extent_el.width,
306 .height = img_extent_el.height,
307 };
308
309 /* Create blit surfaces */
310 struct radv_meta_blit2d_surf img_info =
311 blit_surf_for_image_level_layer(image,
312 layout,
313 &pRegions[r].imageSubresource,
314 pRegions[r].imageSubresource.aspectMask);
315
316 struct radv_meta_blit2d_buffer buf_info = {
317 .bs = img_info.bs,
318 .format = img_info.format,
319 .buffer = buffer,
320 .offset = pRegions[r].bufferOffset,
321 .pitch = buf_extent_el.width,
322 };
323
324 if (image->type == VK_IMAGE_TYPE_3D)
325 img_info.layer = img_offset_el.z;
326 /* Loop through each 3D or array slice */
327 unsigned num_slices_3d = img_extent_el.depth;
328 unsigned num_slices_array = pRegions[r].imageSubresource.layerCount;
329 unsigned slice_3d = 0;
330 unsigned slice_array = 0;
331 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
332
333 rect.src_x = img_offset_el.x;
334 rect.src_y = img_offset_el.y;
335
336
337 /* Perform Blit */
338 radv_meta_image_to_buffer(cmd_buffer, &img_info, &buf_info, 1, &rect);
339
340 buf_info.offset += buf_extent_el.width *
341 buf_extent_el.height * buf_info.bs;
342 img_info.layer++;
343 if (image->type == VK_IMAGE_TYPE_3D)
344 slice_3d++;
345 else
346 slice_array++;
347 }
348 }
349
350 /* Restore conditional rendering. */
351 cmd_buffer->state.predicating = old_predicating;
352
353 radv_meta_restore(&saved_state, cmd_buffer);
354 }
355
356 void radv_CmdCopyImageToBuffer(
357 VkCommandBuffer commandBuffer,
358 VkImage srcImage,
359 VkImageLayout srcImageLayout,
360 VkBuffer destBuffer,
361 uint32_t regionCount,
362 const VkBufferImageCopy* pRegions)
363 {
364 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
365 RADV_FROM_HANDLE(radv_image, src_image, srcImage);
366 RADV_FROM_HANDLE(radv_buffer, dst_buffer, destBuffer);
367
368 meta_copy_image_to_buffer(cmd_buffer, dst_buffer, src_image,
369 srcImageLayout,
370 regionCount, pRegions);
371 }
372
373 static void
374 meta_copy_image(struct radv_cmd_buffer *cmd_buffer,
375 struct radv_image *src_image,
376 VkImageLayout src_image_layout,
377 struct radv_image *dest_image,
378 VkImageLayout dest_image_layout,
379 uint32_t regionCount,
380 const VkImageCopy *pRegions)
381 {
382 bool cs = cmd_buffer->queue_family_index == RADV_QUEUE_COMPUTE;
383 struct radv_meta_saved_state saved_state;
384 bool old_predicating;
385
386 /* From the Vulkan 1.0 spec:
387 *
388 * vkCmdCopyImage can be used to copy image data between multisample
389 * images, but both images must have the same number of samples.
390 */
391 assert(src_image->info.samples == dest_image->info.samples);
392
393 radv_meta_save(&saved_state, cmd_buffer,
394 (cs ? RADV_META_SAVE_COMPUTE_PIPELINE :
395 RADV_META_SAVE_GRAPHICS_PIPELINE) |
396 RADV_META_SAVE_CONSTANTS |
397 RADV_META_SAVE_DESCRIPTORS);
398
399 /* VK_EXT_conditional_rendering says that copy commands should not be
400 * affected by conditional rendering.
401 */
402 old_predicating = cmd_buffer->state.predicating;
403 cmd_buffer->state.predicating = false;
404
405 for (unsigned r = 0; r < regionCount; r++) {
406 VkImageAspectFlags src_aspects[3] = {VK_IMAGE_ASPECT_PLANE_0_BIT, VK_IMAGE_ASPECT_PLANE_1_BIT, VK_IMAGE_ASPECT_PLANE_2_BIT};
407 VkImageAspectFlags dst_aspects[3] = {VK_IMAGE_ASPECT_PLANE_0_BIT, VK_IMAGE_ASPECT_PLANE_1_BIT, VK_IMAGE_ASPECT_PLANE_2_BIT};
408 unsigned aspect_count = pRegions[r].srcSubresource.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT ? src_image->plane_count : 1;
409 if (pRegions[r].srcSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT)
410 src_aspects[0] = pRegions[r].srcSubresource.aspectMask;
411 if (pRegions[r].dstSubresource.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT)
412 dst_aspects[0] = pRegions[r].dstSubresource.aspectMask;
413
414 for (unsigned a = 0; a < aspect_count; ++a) {
415 /* Create blit surfaces */
416 struct radv_meta_blit2d_surf b_src =
417 blit_surf_for_image_level_layer(src_image,
418 src_image_layout,
419 &pRegions[r].srcSubresource,
420 src_aspects[a]);
421
422 struct radv_meta_blit2d_surf b_dst =
423 blit_surf_for_image_level_layer(dest_image,
424 dest_image_layout,
425 &pRegions[r].dstSubresource,
426 dst_aspects[a]);
427
428 uint32_t dst_queue_mask = radv_image_queue_family_mask(dest_image,
429 cmd_buffer->queue_family_index,
430 cmd_buffer->queue_family_index);
431 bool dst_compressed = radv_layout_dcc_compressed(dest_image, dest_image_layout, dst_queue_mask);
432 uint32_t src_queue_mask = radv_image_queue_family_mask(src_image,
433 cmd_buffer->queue_family_index,
434 cmd_buffer->queue_family_index);
435 bool src_compressed = radv_layout_dcc_compressed(src_image, src_image_layout, src_queue_mask);
436
437 if (!src_compressed || radv_dcc_formats_compatible(b_src.format, b_dst.format)) {
438 b_src.format = b_dst.format;
439 } else if (!dst_compressed) {
440 b_dst.format = b_src.format;
441 } else {
442 radv_decompress_dcc(cmd_buffer, dest_image, &(VkImageSubresourceRange) {
443 .aspectMask = dst_aspects[a],
444 .baseMipLevel = pRegions[r].dstSubresource.mipLevel,
445 .levelCount = 1,
446 .baseArrayLayer = pRegions[r].dstSubresource.baseArrayLayer,
447 .layerCount = pRegions[r].dstSubresource.layerCount,
448 });
449 b_dst.format = b_src.format;
450 b_dst.current_layout = VK_IMAGE_LAYOUT_GENERAL;
451 }
452
453
454 /**
455 * From the Vulkan 1.0.6 spec: 18.4 Copying Data Between Buffers and Images
456 * imageExtent is the size in texels of the image to copy in width, height
457 * and depth. 1D images use only x and width. 2D images use x, y, width
458 * and height. 3D images use x, y, z, width, height and depth.
459 *
460 * Also, convert the offsets and extent from units of texels to units of
461 * blocks - which is the highest resolution accessible in this command.
462 */
463 const VkOffset3D dst_offset_el =
464 meta_region_offset_el(dest_image, &pRegions[r].dstOffset);
465 const VkOffset3D src_offset_el =
466 meta_region_offset_el(src_image, &pRegions[r].srcOffset);
467
468 /*
469 * From Vulkan 1.0.68, "Copying Data Between Images":
470 * "When copying between compressed and uncompressed formats
471 * the extent members represent the texel dimensions of the
472 * source image and not the destination."
473 * However, we must use the destination image type to avoid
474 * clamping depth when copying multiple layers of a 2D image to
475 * a 3D image.
476 */
477 const VkExtent3D img_extent_el =
478 meta_region_extent_el(src_image, dest_image->type, &pRegions[r].extent);
479
480 /* Start creating blit rect */
481 struct radv_meta_blit2d_rect rect = {
482 .width = img_extent_el.width,
483 .height = img_extent_el.height,
484 };
485
486 if (src_image->type == VK_IMAGE_TYPE_3D)
487 b_src.layer = src_offset_el.z;
488
489 if (dest_image->type == VK_IMAGE_TYPE_3D)
490 b_dst.layer = dst_offset_el.z;
491
492 /* Loop through each 3D or array slice */
493 unsigned num_slices_3d = img_extent_el.depth;
494 unsigned num_slices_array = pRegions[r].dstSubresource.layerCount;
495 unsigned slice_3d = 0;
496 unsigned slice_array = 0;
497 while (slice_3d < num_slices_3d && slice_array < num_slices_array) {
498
499 /* Finish creating blit rect */
500 rect.dst_x = dst_offset_el.x;
501 rect.dst_y = dst_offset_el.y;
502 rect.src_x = src_offset_el.x;
503 rect.src_y = src_offset_el.y;
504
505 /* Perform Blit */
506 if (cs ||
507 !image_is_renderable(cmd_buffer->device, b_dst.image)) {
508 radv_meta_image_to_image_cs(cmd_buffer, &b_src, &b_dst, 1, &rect);
509 } else {
510 radv_meta_blit2d(cmd_buffer, &b_src, NULL, &b_dst, 1, &rect);
511 }
512
513 b_src.layer++;
514 b_dst.layer++;
515 if (dest_image->type == VK_IMAGE_TYPE_3D)
516 slice_3d++;
517 else
518 slice_array++;
519 }
520 }
521 }
522
523 /* Restore conditional rendering. */
524 cmd_buffer->state.predicating = old_predicating;
525
526 radv_meta_restore(&saved_state, cmd_buffer);
527 }
528
529 void radv_CmdCopyImage(
530 VkCommandBuffer commandBuffer,
531 VkImage srcImage,
532 VkImageLayout srcImageLayout,
533 VkImage destImage,
534 VkImageLayout destImageLayout,
535 uint32_t regionCount,
536 const VkImageCopy* pRegions)
537 {
538 RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
539 RADV_FROM_HANDLE(radv_image, src_image, srcImage);
540 RADV_FROM_HANDLE(radv_image, dest_image, destImage);
541
542 meta_copy_image(cmd_buffer,
543 src_image, srcImageLayout,
544 dest_image, destImageLayout,
545 regionCount, pRegions);
546 }