diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 20:39:05 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 20:39:05 -0500 |
| commit | d7bceb8b9cd4fcc9ba49f202998d97487b1f5a08 (patch) | |
| tree | 76c01937fae6d1cd9b82fa61233d85c36996ab8a /runtime/src | |
| parent | 8f2b13876b74d02ef422f051020c56c46d19c864 (diff) | |
| parent | 2cb28a4239df522a6644dd31bc1427a5e13d7af4 (diff) | |
Merge pull request #643 from Vonsek
# Conflicts:
# docs/d3d12_vulkan.md
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/vulkan_replay.h | 58 |
1 files changed, 39 insertions, 19 deletions
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index b289acffa..b19b7b01a 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h | |||
| @@ -694,6 +694,14 @@ inline VkFormat color_view_format(const ResourceBinding& resource, const TargetV | |||
| 694 | inline bool view_range(const ResourceBinding& resource, const TargetView& view, VkImageAspectFlags aspects, | 694 | inline bool view_range(const ResourceBinding& resource, const TargetView& view, VkImageAspectFlags aspects, |
| 695 | VkImageSubresourceRange& out) { | 695 | VkImageSubresourceRange& out) { |
| 696 | if (view.mip >= resource.mip_levels) return false; | 696 | if (view.mip >= resource.mip_levels) return false; |
| 697 | if (resource.volume) { | ||
| 698 | const auto depth = std::max(1u, resource.depth >> view.mip); | ||
| 699 | if (aspects != VK_IMAGE_ASPECT_COLOR_BIT || view.first_layer >= depth || | ||
| 700 | !view.layer_count || view.layer_count > depth - view.first_layer) return false; | ||
| 701 | // Layouts and transfers address one array layer for the whole 3D mip. | ||
| 702 | out = {aspects, view.mip, 1, 0, 1}; | ||
| 703 | return true; | ||
| 704 | } | ||
| 697 | if (view.first_layer >= resource.array_layers || view.layer_count > resource.array_layers - view.first_layer) | 705 | if (view.first_layer >= resource.array_layers || view.layer_count > resource.array_layers - view.first_layer) |
| 698 | return false; | 706 | return false; |
| 699 | out = {aspects, view.mip, 1, view.first_layer, view.layer_count}; | 707 | out = {aspects, view.mip, 1, view.first_layer, view.layer_count}; |
| @@ -724,7 +732,13 @@ inline std::vector<VkClearRect> clip(std::span<const Rect> rects, VkExtent2D ext | |||
| 724 | return out; | 732 | return out; |
| 725 | } | 733 | } |
| 726 | 734 | ||
| 727 | inline void image_barrier(const ReplayFunctions& vk, VkCommandBuffer command_buffer, VkImage image, | 735 | // A D3D12 barrier on a volume mip covers every W slice, including on devices with maintenance9. |
| 736 | inline VkImageSubresourceRange layout_range(const ResourceBinding& resource, VkImageSubresourceRange range) { | ||
| 737 | if (resource.volume) range.layerCount = VK_REMAINING_ARRAY_LAYERS; | ||
| 738 | return range; | ||
| 739 | } | ||
| 740 | |||
| 741 | inline void image_barrier(const ReplayFunctions& vk, VkCommandBuffer command_buffer, const ResourceBinding& resource, | ||
| 728 | const VkImageSubresourceRange& range, VkImageLayout old_layout, VkImageLayout new_layout, | 742 | const VkImageSubresourceRange& range, VkImageLayout old_layout, VkImageLayout new_layout, |
| 729 | VkAccessFlags src_access, VkPipelineStageFlags src_stages, VkAccessFlags dst_access, | 743 | VkAccessFlags src_access, VkPipelineStageFlags src_stages, VkAccessFlags dst_access, |
| 730 | VkPipelineStageFlags dst_stages) { | 744 | VkPipelineStageFlags dst_stages) { |
| @@ -736,8 +750,8 @@ inline void image_barrier(const ReplayFunctions& vk, VkCommandBuffer command_buf | |||
| 736 | barrier.newLayout = new_layout; | 750 | barrier.newLayout = new_layout; |
| 737 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | 751 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 738 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | 752 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 739 | barrier.image = image; | 753 | barrier.image = resource.image; |
| 740 | barrier.subresourceRange = range; | 754 | barrier.subresourceRange = layout_range(resource, range); |
| 741 | vk.pipeline_barrier(command_buffer, src_stages, dst_stages, 0, 0, nullptr, 0, nullptr, 1, &barrier); | 755 | vk.pipeline_barrier(command_buffer, src_stages, dst_stages, 0, 0, nullptr, 0, nullptr, 1, &barrier); |
| 742 | } | 756 | } |
| 743 | 757 | ||
| @@ -770,11 +784,11 @@ inline void initialize(const ReplayFunctions& vk, VkCommandBuffer command_buffer | |||
| 770 | // Vulkan cannot clear block-compressed images; those start undefined (applications upload them first). | 784 | // Vulkan cannot clear block-compressed images; those start undefined (applications upload them first). |
| 771 | const bool compressed = block_format(resource.format, VK_IMAGE_ASPECT_COLOR_BIT).block > 1; | 785 | const bool compressed = block_format(resource.format, VK_IMAGE_ASPECT_COLOR_BIT).block > 1; |
| 772 | if (resource.not_zeroed || compressed) { | 786 | if (resource.not_zeroed || compressed) { |
| 773 | image_barrier(vk, command_buffer, resource.image, piece.range, VK_IMAGE_LAYOUT_UNDEFINED, layout, 0, | 787 | image_barrier(vk, command_buffer, resource, piece.range, VK_IMAGE_LAYOUT_UNDEFINED, layout, 0, |
| 774 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, any_memory, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); | 788 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, any_memory, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); |
| 775 | } else { | 789 | } else { |
| 776 | const VkImageLayout dst = image_layout(resource, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); | 790 | const VkImageLayout dst = image_layout(resource, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); |
| 777 | image_barrier(vk, command_buffer, resource.image, piece.range, VK_IMAGE_LAYOUT_UNDEFINED, dst, 0, | 791 | image_barrier(vk, command_buffer, resource, piece.range, VK_IMAGE_LAYOUT_UNDEFINED, dst, 0, |
| 778 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, | 792 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, |
| 779 | VK_PIPELINE_STAGE_TRANSFER_BIT); | 793 | VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 780 | if (piece.range.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) { | 794 | if (piece.range.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) { |
| @@ -784,7 +798,7 @@ inline void initialize(const ReplayFunctions& vk, VkCommandBuffer command_buffer | |||
| 784 | const VkClearDepthStencilValue zero{}; | 798 | const VkClearDepthStencilValue zero{}; |
| 785 | vk.clear_depth_stencil_image(command_buffer, resource.image, dst, &zero, 1, &piece.range); | 799 | vk.clear_depth_stencil_image(command_buffer, resource.image, dst, &zero, 1, &piece.range); |
| 786 | } | 800 | } |
| 787 | image_barrier(vk, command_buffer, resource.image, piece.range, dst, layout, VK_ACCESS_TRANSFER_WRITE_BIT, | 801 | image_barrier(vk, command_buffer, resource, piece.range, dst, layout, VK_ACCESS_TRANSFER_WRITE_BIT, |
| 788 | VK_PIPELINE_STAGE_TRANSFER_BIT, any_memory, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); | 802 | VK_PIPELINE_STAGE_TRANSFER_BIT, any_memory, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); |
| 789 | } | 803 | } |
| 790 | piece.layout = layout; | 804 | piece.layout = layout; |
| @@ -1042,7 +1056,7 @@ inline VkImageLayout copy_layout(const ReplayFunctions& vk, VkCommandBuffer comm | |||
| 1042 | return pieces[0].layout; | 1056 | return pieces[0].layout; |
| 1043 | for (const auto& piece : pieces) { | 1057 | for (const auto& piece : pieces) { |
| 1044 | if (piece.layout == target) continue; | 1058 | if (piece.layout == target) continue; |
| 1045 | image_barrier(vk, command_buffer, resource.image, piece.range, piece.layout, target, | 1059 | image_barrier(vk, command_buffer, resource, piece.range, piece.layout, target, |
| 1046 | VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, access, | 1060 | VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, access, |
| 1047 | VK_PIPELINE_STAGE_TRANSFER_BIT); | 1061 | VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 1048 | } | 1062 | } |
| @@ -1053,7 +1067,7 @@ inline VkImageLayout copy_layout(const ReplayFunctions& vk, VkCommandBuffer comm | |||
| 1053 | // Moves every piece to `target` (src: the D3D12 state the operation requires), runs `body`, and moves the pieces | 1067 | // Moves every piece to `target` (src: the D3D12 state the operation requires), runs `body`, and moves the pieces |
| 1054 | // back to where they were. Pieces already in `target` need no transition. | 1068 | // back to where they were. Pieces already in `target` need no transition. |
| 1055 | template<typename Body> | 1069 | template<typename Body> |
| 1056 | void round_trip(const ReplayFunctions& vk, VkCommandBuffer command_buffer, VkImage image, | 1070 | void round_trip(const ReplayFunctions& vk, VkCommandBuffer command_buffer, const ResourceBinding& resource, |
| 1057 | const std::vector<LayoutPiece>& pieces, VkImageLayout target, const VulkanAccess& state, | 1071 | const std::vector<LayoutPiece>& pieces, VkImageLayout target, const VulkanAccess& state, |
| 1058 | VkAccessFlags target_access, VkPipelineStageFlags target_stages, Body&& body) { | 1072 | VkAccessFlags target_access, VkPipelineStageFlags target_stages, Body&& body) { |
| 1059 | for (const auto& piece : pieces) { | 1073 | for (const auto& piece : pieces) { |
| @@ -1061,7 +1075,7 @@ void round_trip(const ReplayFunctions& vk, VkCommandBuffer command_buffer, VkIma | |||
| 1061 | memory_barrier(vk, command_buffer, state.access, state.stages, target_access, target_stages); | 1075 | memory_barrier(vk, command_buffer, state.access, state.stages, target_access, target_stages); |
| 1062 | continue; | 1076 | continue; |
| 1063 | } | 1077 | } |
| 1064 | image_barrier(vk, command_buffer, image, piece.range, piece.layout, target, state.access, state.stages, | 1078 | image_barrier(vk, command_buffer, resource, piece.range, piece.layout, target, state.access, state.stages, |
| 1065 | target_access, target_stages); | 1079 | target_access, target_stages); |
| 1066 | } | 1080 | } |
| 1067 | body(); | 1081 | body(); |
| @@ -1070,7 +1084,7 @@ void round_trip(const ReplayFunctions& vk, VkCommandBuffer command_buffer, VkIma | |||
| 1070 | memory_barrier(vk, command_buffer, target_access, target_stages, state.access, state.stages); | 1084 | memory_barrier(vk, command_buffer, target_access, target_stages, state.access, state.stages); |
| 1071 | continue; | 1085 | continue; |
| 1072 | } | 1086 | } |
| 1073 | image_barrier(vk, command_buffer, image, piece.range, target, piece.layout, target_access, target_stages, | 1087 | image_barrier(vk, command_buffer, resource, piece.range, target, piece.layout, target_access, target_stages, |
| 1074 | state.access, state.stages); | 1088 | state.access, state.stages); |
| 1075 | } | 1089 | } |
| 1076 | } | 1090 | } |
| @@ -1135,6 +1149,11 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 1135 | } | 1149 | } |
| 1136 | VkImageSubresourceRange range = p.range; | 1150 | VkImageSubresourceRange range = p.range; |
| 1137 | range.aspectMask = view_aspects; | 1151 | range.aspectMask = view_aspects; |
| 1152 | if (p.binding.volume) { | ||
| 1153 | // A 2D-array attachment view of a 3D image selects W slices here. | ||
| 1154 | range.baseArrayLayer = target.first_layer; | ||
| 1155 | range.layerCount = target.layer_count; | ||
| 1156 | } | ||
| 1138 | ResourceBinding view_binding = p.binding; | 1157 | ResourceBinding view_binding = p.binding; |
| 1139 | if (p.clear_format != VK_FORMAT_UNDEFINED) view_binding.format = p.clear_format; | 1158 | if (p.clear_format != VK_FORMAT_UNDEFINED) view_binding.format = p.clear_format; |
| 1140 | p.view = vk.image_view(view_binding, range); | 1159 | p.view = vk.image_view(view_binding, range); |
| @@ -1169,9 +1188,10 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 1169 | if (p.clear_format == VK_FORMAT_UNDEFINED && | 1188 | if (p.clear_format == VK_FORMAT_UNDEFINED && |
| 1170 | (p.binding.rgba8_typeless || color_clear->target.format != ColorViewFormat::resource)) | 1189 | (p.binding.rgba8_typeless || color_clear->target.format != ColorViewFormat::resource)) |
| 1171 | return ReplayResult::invalid_clear; | 1190 | return ReplayResult::invalid_clear; |
| 1172 | if (p.clear_format != p.binding.format && color_clear->rects.empty()) { | 1191 | const bool partial_volume = p.binding.volume && |
| 1173 | // vkCmdClearColorImage has no view-format parameter. Reinterpreted whole-view clears need | 1192 | color_clear->target.layer_count != std::max(1u, p.binding.depth >> color_clear->target.mip); |
| 1174 | // an attachment view too, including sRGB conversion and UINT numeric interpretation. | 1193 | if ((p.clear_format != p.binding.format || partial_volume) && color_clear->rects.empty()) { |
| 1194 | // vkCmdClearColorImage can neither reinterpret the format nor select W slices of a mip. | ||
| 1175 | const auto extent = detail::mip_extent(p.binding, color_clear->target.mip); | 1195 | const auto extent = detail::mip_extent(p.binding, color_clear->target.mip); |
| 1176 | const Rect full{0, 0, std::int32_t(extent.width), std::int32_t(extent.height)}; | 1196 | const Rect full{0, 0, std::int32_t(extent.width), std::int32_t(extent.height)}; |
| 1177 | if (const auto r = rect_view(std::span<const Rect>(&full, 1), color_clear->target, | 1197 | if (const auto r = rect_view(std::span<const Rect>(&full, 1), color_clear->target, |
| @@ -1314,7 +1334,7 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1314 | auto pieces = detail::current(tracker, id, range, image_layout(p.binding, from.layout)); | 1334 | auto pieces = detail::current(tracker, id, range, image_layout(p.binding, from.layout)); |
| 1315 | detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, from.layout), tracker); | 1335 | detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, from.layout), tracker); |
| 1316 | for (const auto& piece : pieces) | 1336 | for (const auto& piece : pieces) |
| 1317 | detail::image_barrier(vk, command_buffer, p.binding.image, piece.range, piece.layout, new_layout, | 1337 | detail::image_barrier(vk, command_buffer, p.binding, piece.range, piece.layout, new_layout, |
| 1318 | from.access, from.stages, to.access, to.stages); | 1338 | from.access, from.stages, to.access, to.stages); |
| 1319 | if (tracker) tracker->set(id, range, new_layout); | 1339 | if (tracker) tracker->set(id, range, new_layout); |
| 1320 | } else if (std::holds_alternative<UavBarrier>(command)) { | 1340 | } else if (std::holds_alternative<UavBarrier>(command)) { |
| @@ -1373,7 +1393,7 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1373 | b.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | 1393 | b.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1374 | b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | 1394 | b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1375 | b.image = p.binding.image; | 1395 | b.image = p.binding.image; |
| 1376 | b.subresourceRange = piece.range; | 1396 | b.subresourceRange = detail::layout_range(p.binding, piece.range); |
| 1377 | barriers.push_back(b); | 1397 | barriers.push_back(b); |
| 1378 | } | 1398 | } |
| 1379 | VkDependencyInfo dependency{VK_STRUCTURE_TYPE_DEPENDENCY_INFO}; | 1399 | VkDependencyInfo dependency{VK_STRUCTURE_TYPE_DEPENDENCY_INFO}; |
| @@ -1388,7 +1408,7 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1388 | detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker); | 1408 | detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker); |
| 1389 | const VkClearColorValue color = detail::render_target_clear_color(p.clear_format, color_clear->color); | 1409 | const VkClearColorValue color = detail::render_target_clear_color(p.clear_format, color_clear->color); |
| 1390 | if (p.rects.empty()) { | 1410 | if (p.rects.empty()) { |
| 1391 | detail::round_trip(vk, command_buffer, p.binding.image, pieces, image_layout(p.binding, transfer_dst.layout), | 1411 | detail::round_trip(vk, command_buffer, p.binding, pieces, image_layout(p.binding, transfer_dst.layout), |
| 1392 | state, transfer_dst.access, transfer_dst.stages, [&] { | 1412 | state, transfer_dst.access, transfer_dst.stages, [&] { |
| 1393 | vk.clear_color_image(command_buffer, p.binding.image, image_layout(p.binding, transfer_dst.layout), | 1413 | vk.clear_color_image(command_buffer, p.binding.image, image_layout(p.binding, transfer_dst.layout), |
| 1394 | &color, 1, &p.range); | 1414 | &color, 1, &p.range); |
| @@ -1396,7 +1416,7 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1396 | continue; | 1416 | continue; |
| 1397 | } | 1417 | } |
| 1398 | const VkImageLayout attachment = image_layout(p.binding, state.layout); | 1418 | const VkImageLayout attachment = image_layout(p.binding, state.layout); |
| 1399 | detail::round_trip(vk, command_buffer, p.binding.image, pieces, attachment, state, state.access, | 1419 | detail::round_trip(vk, command_buffer, p.binding, pieces, attachment, state, state.access, |
| 1400 | state.stages, [&] { | 1420 | state.stages, [&] { |
| 1401 | VkRenderingAttachmentInfo info{VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO}; | 1421 | VkRenderingAttachmentInfo info{VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO}; |
| 1402 | info.imageView = p.view; | 1422 | info.imageView = p.view; |
| @@ -1423,7 +1443,7 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1423 | detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker); | 1443 | detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker); |
| 1424 | const VkClearDepthStencilValue value{depth_clear->depth, depth_clear->stencil}; | 1444 | const VkClearDepthStencilValue value{depth_clear->depth, depth_clear->stencil}; |
| 1425 | if (p.rects.empty()) { | 1445 | if (p.rects.empty()) { |
| 1426 | detail::round_trip(vk, command_buffer, p.binding.image, pieces, image_layout(p.binding, transfer_dst.layout), | 1446 | detail::round_trip(vk, command_buffer, p.binding, pieces, image_layout(p.binding, transfer_dst.layout), |
| 1427 | state, transfer_dst.access, transfer_dst.stages, [&] { | 1447 | state, transfer_dst.access, transfer_dst.stages, [&] { |
| 1428 | vk.clear_depth_stencil_image(command_buffer, p.binding.image, | 1448 | vk.clear_depth_stencil_image(command_buffer, p.binding.image, |
| 1429 | image_layout(p.binding, transfer_dst.layout), &value, 1, &p.range); | 1449 | image_layout(p.binding, transfer_dst.layout), &value, 1, &p.range); |
| @@ -1431,7 +1451,7 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1431 | continue; | 1451 | continue; |
| 1432 | } | 1452 | } |
| 1433 | const VkImageLayout attachment = image_layout(p.binding, state.layout); | 1453 | const VkImageLayout attachment = image_layout(p.binding, state.layout); |
| 1434 | detail::round_trip(vk, command_buffer, p.binding.image, pieces, attachment, state, state.access, | 1454 | detail::round_trip(vk, command_buffer, p.binding, pieces, attachment, state, state.access, |
| 1435 | state.stages, [&] { | 1455 | state.stages, [&] { |
| 1436 | VkRenderingAttachmentInfo info{VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO}; | 1456 | VkRenderingAttachmentInfo info{VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO}; |
| 1437 | info.imageView = p.view; | 1457 | info.imageView = p.view; |