diff options
| author | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 02:29:34 +0100 |
|---|---|---|
| committer | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 02:29:34 +0100 |
| commit | 2cb28a4239df522a6644dd31bc1427a5e13d7af4 (patch) | |
| tree | bbc511044e9a8e7e51685fbd2e479b0ec21264aa /runtime/src | |
| parent | c432cdce127ed2d22a99aa6ef57d91db4a10b5bb (diff) | |
feat(runtime): clear selected slices of 3D render targets
A volume mip has a single tracked layout, but its color attachment may cover only part of its depth. Use the view to select W slices and keep barriers on the whole mip so clears preserve neighboring slices.
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 3a5db673e..d983566d7 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h | |||
| @@ -684,6 +684,14 @@ inline VkFormat color_view_format(const ResourceBinding& resource, const TargetV | |||
| 684 | inline bool view_range(const ResourceBinding& resource, const TargetView& view, VkImageAspectFlags aspects, | 684 | inline bool view_range(const ResourceBinding& resource, const TargetView& view, VkImageAspectFlags aspects, |
| 685 | VkImageSubresourceRange& out) { | 685 | VkImageSubresourceRange& out) { |
| 686 | if (view.mip >= resource.mip_levels) return false; | 686 | if (view.mip >= resource.mip_levels) return false; |
| 687 | if (resource.volume) { | ||
| 688 | const auto depth = std::max(1u, resource.depth >> view.mip); | ||
| 689 | if (aspects != VK_IMAGE_ASPECT_COLOR_BIT || view.first_layer >= depth || | ||
| 690 | !view.layer_count || view.layer_count > depth - view.first_layer) return false; | ||
| 691 | // Layouts and transfers address one array layer for the whole 3D mip. | ||
| 692 | out = {aspects, view.mip, 1, 0, 1}; | ||
| 693 | return true; | ||
| 694 | } | ||
| 687 | if (view.first_layer >= resource.array_layers || view.layer_count > resource.array_layers - view.first_layer) | 695 | if (view.first_layer >= resource.array_layers || view.layer_count > resource.array_layers - view.first_layer) |
| 688 | return false; | 696 | return false; |
| 689 | out = {aspects, view.mip, 1, view.first_layer, view.layer_count}; | 697 | out = {aspects, view.mip, 1, view.first_layer, view.layer_count}; |
| @@ -714,7 +722,13 @@ inline std::vector<VkClearRect> clip(std::span<const Rect> rects, VkExtent2D ext | |||
| 714 | return out; | 722 | return out; |
| 715 | } | 723 | } |
| 716 | 724 | ||
| 717 | inline void image_barrier(const ReplayFunctions& vk, VkCommandBuffer command_buffer, VkImage image, | 725 | // A D3D12 barrier on a volume mip covers every W slice, including on devices with maintenance9. |
| 726 | inline VkImageSubresourceRange layout_range(const ResourceBinding& resource, VkImageSubresourceRange range) { | ||
| 727 | if (resource.volume) range.layerCount = VK_REMAINING_ARRAY_LAYERS; | ||
| 728 | return range; | ||
| 729 | } | ||
| 730 | |||
| 731 | inline void image_barrier(const ReplayFunctions& vk, VkCommandBuffer command_buffer, const ResourceBinding& resource, | ||
| 718 | const VkImageSubresourceRange& range, VkImageLayout old_layout, VkImageLayout new_layout, | 732 | const VkImageSubresourceRange& range, VkImageLayout old_layout, VkImageLayout new_layout, |
| 719 | VkAccessFlags src_access, VkPipelineStageFlags src_stages, VkAccessFlags dst_access, | 733 | VkAccessFlags src_access, VkPipelineStageFlags src_stages, VkAccessFlags dst_access, |
| 720 | VkPipelineStageFlags dst_stages) { | 734 | VkPipelineStageFlags dst_stages) { |
| @@ -726,8 +740,8 @@ inline void image_barrier(const ReplayFunctions& vk, VkCommandBuffer command_buf | |||
| 726 | barrier.newLayout = new_layout; | 740 | barrier.newLayout = new_layout; |
| 727 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | 741 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 728 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | 742 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 729 | barrier.image = image; | 743 | barrier.image = resource.image; |
| 730 | barrier.subresourceRange = range; | 744 | barrier.subresourceRange = layout_range(resource, range); |
| 731 | vk.pipeline_barrier(command_buffer, src_stages, dst_stages, 0, 0, nullptr, 0, nullptr, 1, &barrier); | 745 | vk.pipeline_barrier(command_buffer, src_stages, dst_stages, 0, 0, nullptr, 0, nullptr, 1, &barrier); |
| 732 | } | 746 | } |
| 733 | 747 | ||
| @@ -760,11 +774,11 @@ inline void initialize(const ReplayFunctions& vk, VkCommandBuffer command_buffer | |||
| 760 | // Vulkan cannot clear block-compressed images; those start undefined (applications upload them first). | 774 | // Vulkan cannot clear block-compressed images; those start undefined (applications upload them first). |
| 761 | const bool compressed = block_format(resource.format, VK_IMAGE_ASPECT_COLOR_BIT).block > 1; | 775 | const bool compressed = block_format(resource.format, VK_IMAGE_ASPECT_COLOR_BIT).block > 1; |
| 762 | if (resource.not_zeroed || compressed) { | 776 | if (resource.not_zeroed || compressed) { |
| 763 | image_barrier(vk, command_buffer, resource.image, piece.range, VK_IMAGE_LAYOUT_UNDEFINED, layout, 0, | 777 | image_barrier(vk, command_buffer, resource, piece.range, VK_IMAGE_LAYOUT_UNDEFINED, layout, 0, |
| 764 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, any_memory, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); | 778 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, any_memory, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); |
| 765 | } else { | 779 | } else { |
| 766 | const VkImageLayout dst = image_layout(resource, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); | 780 | const VkImageLayout dst = image_layout(resource, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); |
| 767 | image_barrier(vk, command_buffer, resource.image, piece.range, VK_IMAGE_LAYOUT_UNDEFINED, dst, 0, | 781 | image_barrier(vk, command_buffer, resource, piece.range, VK_IMAGE_LAYOUT_UNDEFINED, dst, 0, |
| 768 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, | 782 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, |
| 769 | VK_PIPELINE_STAGE_TRANSFER_BIT); | 783 | VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 770 | if (piece.range.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) { | 784 | if (piece.range.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) { |
| @@ -774,7 +788,7 @@ inline void initialize(const ReplayFunctions& vk, VkCommandBuffer command_buffer | |||
| 774 | const VkClearDepthStencilValue zero{}; | 788 | const VkClearDepthStencilValue zero{}; |
| 775 | vk.clear_depth_stencil_image(command_buffer, resource.image, dst, &zero, 1, &piece.range); | 789 | vk.clear_depth_stencil_image(command_buffer, resource.image, dst, &zero, 1, &piece.range); |
| 776 | } | 790 | } |
| 777 | image_barrier(vk, command_buffer, resource.image, piece.range, dst, layout, VK_ACCESS_TRANSFER_WRITE_BIT, | 791 | image_barrier(vk, command_buffer, resource, piece.range, dst, layout, VK_ACCESS_TRANSFER_WRITE_BIT, |
| 778 | VK_PIPELINE_STAGE_TRANSFER_BIT, any_memory, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); | 792 | VK_PIPELINE_STAGE_TRANSFER_BIT, any_memory, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); |
| 779 | } | 793 | } |
| 780 | piece.layout = layout; | 794 | piece.layout = layout; |
| @@ -951,7 +965,7 @@ inline VkImageLayout copy_layout(const ReplayFunctions& vk, VkCommandBuffer comm | |||
| 951 | return pieces[0].layout; | 965 | return pieces[0].layout; |
| 952 | for (const auto& piece : pieces) { | 966 | for (const auto& piece : pieces) { |
| 953 | if (piece.layout == target) continue; | 967 | if (piece.layout == target) continue; |
| 954 | image_barrier(vk, command_buffer, resource.image, piece.range, piece.layout, target, | 968 | image_barrier(vk, command_buffer, resource, piece.range, piece.layout, target, |
| 955 | VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, access, | 969 | VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, access, |
| 956 | VK_PIPELINE_STAGE_TRANSFER_BIT); | 970 | VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 957 | } | 971 | } |
| @@ -962,7 +976,7 @@ inline VkImageLayout copy_layout(const ReplayFunctions& vk, VkCommandBuffer comm | |||
| 962 | // Moves every piece to `target` (src: the D3D12 state the operation requires), runs `body`, and moves the pieces | 976 | // Moves every piece to `target` (src: the D3D12 state the operation requires), runs `body`, and moves the pieces |
| 963 | // back to where they were. Pieces already in `target` need no transition. | 977 | // back to where they were. Pieces already in `target` need no transition. |
| 964 | template<typename Body> | 978 | template<typename Body> |
| 965 | void round_trip(const ReplayFunctions& vk, VkCommandBuffer command_buffer, VkImage image, | 979 | void round_trip(const ReplayFunctions& vk, VkCommandBuffer command_buffer, const ResourceBinding& resource, |
| 966 | const std::vector<LayoutPiece>& pieces, VkImageLayout target, const VulkanAccess& state, | 980 | const std::vector<LayoutPiece>& pieces, VkImageLayout target, const VulkanAccess& state, |
| 967 | VkAccessFlags target_access, VkPipelineStageFlags target_stages, Body&& body) { | 981 | VkAccessFlags target_access, VkPipelineStageFlags target_stages, Body&& body) { |
| 968 | for (const auto& piece : pieces) { | 982 | for (const auto& piece : pieces) { |
| @@ -970,7 +984,7 @@ void round_trip(const ReplayFunctions& vk, VkCommandBuffer command_buffer, VkIma | |||
| 970 | memory_barrier(vk, command_buffer, state.access, state.stages, target_access, target_stages); | 984 | memory_barrier(vk, command_buffer, state.access, state.stages, target_access, target_stages); |
| 971 | continue; | 985 | continue; |
| 972 | } | 986 | } |
| 973 | image_barrier(vk, command_buffer, image, piece.range, piece.layout, target, state.access, state.stages, | 987 | image_barrier(vk, command_buffer, resource, piece.range, piece.layout, target, state.access, state.stages, |
| 974 | target_access, target_stages); | 988 | target_access, target_stages); |
| 975 | } | 989 | } |
| 976 | body(); | 990 | body(); |
| @@ -979,7 +993,7 @@ void round_trip(const ReplayFunctions& vk, VkCommandBuffer command_buffer, VkIma | |||
| 979 | memory_barrier(vk, command_buffer, target_access, target_stages, state.access, state.stages); | 993 | memory_barrier(vk, command_buffer, target_access, target_stages, state.access, state.stages); |
| 980 | continue; | 994 | continue; |
| 981 | } | 995 | } |
| 982 | image_barrier(vk, command_buffer, image, piece.range, target, piece.layout, target_access, target_stages, | 996 | image_barrier(vk, command_buffer, resource, piece.range, target, piece.layout, target_access, target_stages, |
| 983 | state.access, state.stages); | 997 | state.access, state.stages); |
| 984 | } | 998 | } |
| 985 | } | 999 | } |
| @@ -1044,6 +1058,11 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 1044 | } | 1058 | } |
| 1045 | VkImageSubresourceRange range = p.range; | 1059 | VkImageSubresourceRange range = p.range; |
| 1046 | range.aspectMask = view_aspects; | 1060 | range.aspectMask = view_aspects; |
| 1061 | if (p.binding.volume) { | ||
| 1062 | // A 2D-array attachment view of a 3D image selects W slices here. | ||
| 1063 | range.baseArrayLayer = target.first_layer; | ||
| 1064 | range.layerCount = target.layer_count; | ||
| 1065 | } | ||
| 1047 | ResourceBinding view_binding = p.binding; | 1066 | ResourceBinding view_binding = p.binding; |
| 1048 | if (p.clear_format != VK_FORMAT_UNDEFINED) view_binding.format = p.clear_format; | 1067 | if (p.clear_format != VK_FORMAT_UNDEFINED) view_binding.format = p.clear_format; |
| 1049 | p.view = vk.image_view(view_binding, range); | 1068 | p.view = vk.image_view(view_binding, range); |
| @@ -1078,9 +1097,10 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 1078 | if (p.clear_format == VK_FORMAT_UNDEFINED && | 1097 | if (p.clear_format == VK_FORMAT_UNDEFINED && |
| 1079 | (p.binding.rgba8_typeless || color_clear->target.format != ColorViewFormat::resource)) | 1098 | (p.binding.rgba8_typeless || color_clear->target.format != ColorViewFormat::resource)) |
| 1080 | return ReplayResult::invalid_clear; | 1099 | return ReplayResult::invalid_clear; |
| 1081 | if (p.clear_format != p.binding.format && color_clear->rects.empty()) { | 1100 | const bool partial_volume = p.binding.volume && |
| 1082 | // vkCmdClearColorImage has no view-format parameter. Reinterpreted whole-view clears need | 1101 | color_clear->target.layer_count != std::max(1u, p.binding.depth >> color_clear->target.mip); |
| 1083 | // an attachment view too, including sRGB conversion and UINT numeric interpretation. | 1102 | if ((p.clear_format != p.binding.format || partial_volume) && color_clear->rects.empty()) { |
| 1103 | // vkCmdClearColorImage can neither reinterpret the format nor select W slices of a mip. | ||
| 1084 | const auto extent = detail::mip_extent(p.binding, color_clear->target.mip); | 1104 | const auto extent = detail::mip_extent(p.binding, color_clear->target.mip); |
| 1085 | const Rect full{0, 0, std::int32_t(extent.width), std::int32_t(extent.height)}; | 1105 | const Rect full{0, 0, std::int32_t(extent.width), std::int32_t(extent.height)}; |
| 1086 | if (const auto r = rect_view(std::span<const Rect>(&full, 1), color_clear->target, | 1106 | if (const auto r = rect_view(std::span<const Rect>(&full, 1), color_clear->target, |
| @@ -1194,7 +1214,7 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1194 | auto pieces = detail::current(tracker, id, range, image_layout(p.binding, from.layout)); | 1214 | auto pieces = detail::current(tracker, id, range, image_layout(p.binding, from.layout)); |
| 1195 | detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, from.layout), tracker); | 1215 | detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, from.layout), tracker); |
| 1196 | for (const auto& piece : pieces) | 1216 | for (const auto& piece : pieces) |
| 1197 | detail::image_barrier(vk, command_buffer, p.binding.image, piece.range, piece.layout, new_layout, | 1217 | detail::image_barrier(vk, command_buffer, p.binding, piece.range, piece.layout, new_layout, |
| 1198 | from.access, from.stages, to.access, to.stages); | 1218 | from.access, from.stages, to.access, to.stages); |
| 1199 | if (tracker) tracker->set(id, range, new_layout); | 1219 | if (tracker) tracker->set(id, range, new_layout); |
| 1200 | } else if (std::holds_alternative<UavBarrier>(command)) { | 1220 | } else if (std::holds_alternative<UavBarrier>(command)) { |
| @@ -1253,7 +1273,7 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1253 | b.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | 1273 | b.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1254 | b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | 1274 | b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1255 | b.image = p.binding.image; | 1275 | b.image = p.binding.image; |
| 1256 | b.subresourceRange = piece.range; | 1276 | b.subresourceRange = detail::layout_range(p.binding, piece.range); |
| 1257 | barriers.push_back(b); | 1277 | barriers.push_back(b); |
| 1258 | } | 1278 | } |
| 1259 | VkDependencyInfo dependency{VK_STRUCTURE_TYPE_DEPENDENCY_INFO}; | 1279 | VkDependencyInfo dependency{VK_STRUCTURE_TYPE_DEPENDENCY_INFO}; |
| @@ -1268,7 +1288,7 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1268 | detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker); | 1288 | detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker); |
| 1269 | const VkClearColorValue color = detail::render_target_clear_color(p.clear_format, color_clear->color); | 1289 | const VkClearColorValue color = detail::render_target_clear_color(p.clear_format, color_clear->color); |
| 1270 | if (p.rects.empty()) { | 1290 | if (p.rects.empty()) { |
| 1271 | detail::round_trip(vk, command_buffer, p.binding.image, pieces, image_layout(p.binding, transfer_dst.layout), | 1291 | detail::round_trip(vk, command_buffer, p.binding, pieces, image_layout(p.binding, transfer_dst.layout), |
| 1272 | state, transfer_dst.access, transfer_dst.stages, [&] { | 1292 | state, transfer_dst.access, transfer_dst.stages, [&] { |
| 1273 | vk.clear_color_image(command_buffer, p.binding.image, image_layout(p.binding, transfer_dst.layout), | 1293 | vk.clear_color_image(command_buffer, p.binding.image, image_layout(p.binding, transfer_dst.layout), |
| 1274 | &color, 1, &p.range); | 1294 | &color, 1, &p.range); |
| @@ -1276,7 +1296,7 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1276 | continue; | 1296 | continue; |
| 1277 | } | 1297 | } |
| 1278 | const VkImageLayout attachment = image_layout(p.binding, state.layout); | 1298 | const VkImageLayout attachment = image_layout(p.binding, state.layout); |
| 1279 | detail::round_trip(vk, command_buffer, p.binding.image, pieces, attachment, state, state.access, | 1299 | detail::round_trip(vk, command_buffer, p.binding, pieces, attachment, state, state.access, |
| 1280 | state.stages, [&] { | 1300 | state.stages, [&] { |
| 1281 | VkRenderingAttachmentInfo info{VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO}; | 1301 | VkRenderingAttachmentInfo info{VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO}; |
| 1282 | info.imageView = p.view; | 1302 | info.imageView = p.view; |
| @@ -1303,7 +1323,7 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1303 | detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker); | 1323 | detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker); |
| 1304 | const VkClearDepthStencilValue value{depth_clear->depth, depth_clear->stencil}; | 1324 | const VkClearDepthStencilValue value{depth_clear->depth, depth_clear->stencil}; |
| 1305 | if (p.rects.empty()) { | 1325 | if (p.rects.empty()) { |
| 1306 | detail::round_trip(vk, command_buffer, p.binding.image, pieces, image_layout(p.binding, transfer_dst.layout), | 1326 | detail::round_trip(vk, command_buffer, p.binding, pieces, image_layout(p.binding, transfer_dst.layout), |
| 1307 | state, transfer_dst.access, transfer_dst.stages, [&] { | 1327 | state, transfer_dst.access, transfer_dst.stages, [&] { |
| 1308 | vk.clear_depth_stencil_image(command_buffer, p.binding.image, | 1328 | vk.clear_depth_stencil_image(command_buffer, p.binding.image, |
| 1309 | image_layout(p.binding, transfer_dst.layout), &value, 1, &p.range); | 1329 | image_layout(p.binding, transfer_dst.layout), &value, 1, &p.range); |
| @@ -1311,7 +1331,7 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 1311 | continue; | 1331 | continue; |
| 1312 | } | 1332 | } |
| 1313 | const VkImageLayout attachment = image_layout(p.binding, state.layout); | 1333 | const VkImageLayout attachment = image_layout(p.binding, state.layout); |
| 1314 | detail::round_trip(vk, command_buffer, p.binding.image, pieces, attachment, state, state.access, | 1334 | detail::round_trip(vk, command_buffer, p.binding, pieces, attachment, state, state.access, |
| 1315 | state.stages, [&] { | 1335 | state.stages, [&] { |
| 1316 | VkRenderingAttachmentInfo info{VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO}; | 1336 | VkRenderingAttachmentInfo info{VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO}; |
| 1317 | info.imageView = p.view; | 1337 | info.imageView = p.view; |