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