diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 10:39:58 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 10:39:58 -0500 |
| commit | 58cc88f5ddd9dcee6722ef57ea25be86e6ed7755 (patch) | |
| tree | 48e439abbb0edac2b58e33b485fc2d91f0b573e1 /runtime | |
| parent | 0d06dec75b96a80c59c03075f94e2f6b05e013b9 (diff) | |
| parent | ef4d3053b1e0a7d6e19f97416cc703278dc823a0 (diff) | |
Merge pull request #1971 from Jolymmiles
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/src/vulkan_replay.h | 61 | ||||
| -rw-r--r-- | runtime/test/vk_command_replay_test.cpp | 113 |
2 files changed, 172 insertions, 2 deletions
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index addd01795..8ece08d1e 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h | |||
| @@ -1674,6 +1674,43 @@ inline ReplayResult prepare_resolve(const ResolveSubresource& resolve, Prepared& | |||
| 1674 | return ReplayResult::success; | 1674 | return ReplayResult::success; |
| 1675 | } | 1675 | } |
| 1676 | 1676 | ||
| 1677 | // A depth or stencil resolve of a rectangle runs in a rendering scope whose render area is the rectangle, and Vulkan | ||
| 1678 | // resolves only inside it. MoltenVK resolves the whole destination subresource instead: the resolve is Metal's | ||
| 1679 | // store action, which has no render area (measured on MoltenVK 1.4.2 and 1.4.3, docs/d3d12_vulkan.md). No Vulkan | ||
| 1680 | // property tells, so on every GPU the texels around the rectangle go into `staged` before the resolve and come back | ||
| 1681 | // after it, in up to four bands (`staged_reads`, one region per band, used in both directions). | ||
| 1682 | inline ReplayResult keep_around_resolve(const ReplayFunctions& vk, Prepared& p) { | ||
| 1683 | const VkExtent2D to = mip_extent(p.binding, p.range.baseMipLevel); | ||
| 1684 | const auto x0 = std::uint32_t(p.resolve.dstOffset.x), y0 = std::uint32_t(p.resolve.dstOffset.y); | ||
| 1685 | const std::uint32_t x1 = x0 + p.resolve.extent.width, y1 = y0 + p.resolve.extent.height; | ||
| 1686 | const VkRect2D bands[4] = { | ||
| 1687 | {{0, 0}, {to.width, y0}}, // above | ||
| 1688 | {{0, std::int32_t(y1)}, {to.width, to.height - y1}}, // below | ||
| 1689 | {{0, std::int32_t(y0)}, {x0, y1 - y0}}, // left | ||
| 1690 | {{std::int32_t(x1), std::int32_t(y0)}, {to.width - x1, y1 - y0}}, // right | ||
| 1691 | }; | ||
| 1692 | // Depth aspects want 4-byte buffer offsets; 16 covers every texel size here. | ||
| 1693 | const std::uint32_t texel = block_format(p.binding.format, p.range.aspectMask).bytes; | ||
| 1694 | VkDeviceSize total = 0; | ||
| 1695 | for (const VkRect2D& band : bands) { | ||
| 1696 | if (!band.extent.width || !band.extent.height) continue; | ||
| 1697 | VkBufferImageCopy region{}; | ||
| 1698 | region.bufferOffset = total; | ||
| 1699 | region.imageSubresource = p.resolve.dstSubresource; | ||
| 1700 | region.imageOffset = {band.offset.x, band.offset.y, 0}; | ||
| 1701 | region.imageExtent = {band.extent.width, band.extent.height, 1}; | ||
| 1702 | p.staged_reads.push_back(region); | ||
| 1703 | total += (VkDeviceSize(band.extent.width) * band.extent.height * texel + 15) / 16 * 16; | ||
| 1704 | } | ||
| 1705 | if (p.staged_reads.empty()) return ReplayResult::success; // the rectangle is the whole subresource | ||
| 1706 | VkDeviceSize base = 0; | ||
| 1707 | if (!texel || !vk.copy_image_to_buffer || !vk.copy_buffer_to_image || !vk.copy_scratch || | ||
| 1708 | !vk.copy_scratch(total, p.staged, base)) | ||
| 1709 | return ReplayResult::unsupported; | ||
| 1710 | for (VkBufferImageCopy& region : p.staged_reads) region.bufferOffset += base; | ||
| 1711 | return ReplayResult::success; | ||
| 1712 | } | ||
| 1713 | |||
| 1677 | // The layout a copy uses for a range: D3D12 requires copy_dest/copy_source, or common (GENERAL), where an implicit | 1714 | // The layout a copy uses for a range: D3D12 requires copy_dest/copy_source, or common (GENERAL), where an implicit |
| 1678 | // promotion lets the copy run in place. Anything else is moved to `wanted` and tracked. | 1715 | // promotion lets the copy run in place. Anything else is moved to `wanted` and tracked. |
| 1679 | inline VkImageLayout copy_layout(const ReplayFunctions& vk, VkCommandBuffer command_buffer, | 1716 | inline VkImageLayout copy_layout(const ReplayFunctions& vk, VkCommandBuffer command_buffer, |
| @@ -2343,6 +2380,8 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 2343 | p.view = vk.image_view(p.source, p.source_range); | 2380 | p.view = vk.image_view(p.source, p.source_range); |
| 2344 | p.resolve_view = vk.image_view(p.binding, p.range); | 2381 | p.resolve_view = vk.image_view(p.binding, p.range); |
| 2345 | if (!p.view || !p.resolve_view) return ReplayResult::unsupported; | 2382 | if (!p.view || !p.resolve_view) return ReplayResult::unsupported; |
| 2383 | if (const auto result = detail::keep_around_resolve(vk, p); result != ReplayResult::success) | ||
| 2384 | return result; | ||
| 2346 | } | 2385 | } |
| 2347 | } else if (const auto* discard = std::get_if<DiscardResource>(&command)) { | 2386 | } else if (const auto* discard = std::get_if<DiscardResource>(&command)) { |
| 2348 | // Buffers and partial (rectangle) discards keep their contents, which "undefined" allows. | 2387 | // Buffers and partial (rectangle) discards keep their contents, which "undefined" allows. |
| @@ -3137,6 +3176,22 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 3137 | auto dest_pieces = detail::current( | 3176 | auto dest_pieces = detail::current( |
| 3138 | tracker, dst_id, detail::planes_for_layout(p.binding, p.range, separate), dest_layout); | 3177 | tracker, dst_id, detail::planes_for_layout(p.binding, p.range, separate), dest_layout); |
| 3139 | detail::initialize(vk, command_buffer, p.binding, dst_id, dest_pieces, dest_layout, tracker); | 3178 | detail::initialize(vk, command_buffer, p.binding, dst_id, dest_pieces, dest_layout, tracker); |
| 3179 | // The texels around the rectangle, kept in `staged` across the resolve (keep_around_resolve). | ||
| 3180 | const auto keep = [&](VkImageLayout wanted, VkAccessFlags access, auto&& copy) { | ||
| 3181 | const VkImageLayout layout = image_layout(p.binding, wanted); | ||
| 3182 | detail::round_trip(vk, command_buffer, p.binding, dest_pieces, layout, dest_state, access, | ||
| 3183 | VK_PIPELINE_STAGE_TRANSFER_BIT, [&] { copy(layout); }); | ||
| 3184 | }; | ||
| 3185 | const auto bands = std::uint32_t(p.staged_reads.size()); | ||
| 3186 | if (p.staged) { | ||
| 3187 | keep(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_ACCESS_TRANSFER_READ_BIT, [&](VkImageLayout layout) { | ||
| 3188 | vk.copy_image_to_buffer(command_buffer, p.binding.image, layout, p.staged, bands, | ||
| 3189 | p.staged_reads.data()); | ||
| 3190 | }); | ||
| 3191 | detail::memory_barrier(vk, command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, | ||
| 3192 | VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT, | ||
| 3193 | VK_PIPELINE_STAGE_TRANSFER_BIT); | ||
| 3194 | } | ||
| 3140 | const VkImageLayout source_attachment = | 3195 | const VkImageLayout source_attachment = |
| 3141 | image_layout(p.source, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); | 3196 | image_layout(p.source, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); |
| 3142 | const VkImageLayout dest_attachment = | 3197 | const VkImageLayout dest_attachment = |
| @@ -3164,6 +3219,12 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 3164 | vk.end_rendering(command_buffer); | 3219 | vk.end_rendering(command_buffer); |
| 3165 | }); | 3220 | }); |
| 3166 | }); | 3221 | }); |
| 3222 | if (p.staged) { | ||
| 3223 | keep(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT, [&](VkImageLayout layout) { | ||
| 3224 | vk.copy_buffer_to_image(command_buffer, p.staged, p.binding.image, layout, bands, | ||
| 3225 | p.staged_reads.data()); | ||
| 3226 | }); | ||
| 3227 | } | ||
| 3167 | continue; | 3228 | continue; |
| 3168 | } | 3229 | } |
| 3169 | const VkImageLayout src_layout = detail::copy_layout( | 3230 | const VkImageLayout src_layout = detail::copy_layout( |
diff --git a/runtime/test/vk_command_replay_test.cpp b/runtime/test/vk_command_replay_test.cpp index cb2216a74..bb620a2f1 100644 --- a/runtime/test/vk_command_replay_test.cpp +++ b/runtime/test/vk_command_replay_test.cpp | |||
| @@ -6,7 +6,8 @@ | |||
| 6 | // validation layer must accept. Part E (where VK_EXT_conditional_rendering is available) checks SetPredication. | 6 | // validation layer must accept. Part E (where VK_EXT_conditional_rendering is available) checks SetPredication. |
| 7 | // Part F (D3D-26) runs immediate buffer writes, atomic copies and discards. Part G copies one subresource twice in one | 7 | // Part F (D3D-26) runs immediate buffer writes, atomic copies and discards. Part G copies one subresource twice in one |
| 8 | // list, which D3D12 orders on the queue and needs an explicit dependency for on Vulkan. Part H copies depth to colour | 8 | // list, which D3D12 orders on the queue and needs an explicit dependency for on Vulkan. Part H copies depth to colour |
| 9 | // and back (D32_FLOAT <-> R32_FLOAT), which the replay stages through a scratch buffer. | 9 | // and back (D32_FLOAT <-> R32_FLOAT), which the replay stages through a scratch buffer. Part I resolves a depth |
| 10 | // rectangle (MIN) and checks that the texels around it keep their values. | ||
| 10 | // Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation; any validation error fails the test. | 11 | // Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation; any validation error fails the test. |
| 11 | #include "vk_test_device.h" | 12 | #include "vk_test_device.h" |
| 12 | #include "../src/vulkan_replay.h" | 13 | #include "../src/vulkan_replay.h" |
| @@ -1079,13 +1080,121 @@ int part_h(vktest::Device& d) { | |||
| 1079 | return 0; | 1080 | return 0; |
| 1080 | } | 1081 | } |
| 1081 | 1082 | ||
| 1083 | // Part I: a depth MIN resolve of a rectangle (ResolveSubresourceRegion). A 4x D32 target cleared to 0.75 is resolved | ||
| 1084 | // in its rectangle (2,1)-(7,5) into a 1x image cleared to 0.25; every texel outside the rectangle must keep 0.25. | ||
| 1085 | // MoltenVK resolves the whole attachment whatever the render area, so the replay keeps the texels around the | ||
| 1086 | // rectangle in `copy_scratch` across the resolve; with no scratch the list is refused as unsupported. | ||
| 1087 | int part_i(vktest::Device& d) { | ||
| 1088 | VkPhysicalDeviceDepthStencilResolveProperties modes{ | ||
| 1089 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_STENCIL_RESOLVE_PROPERTIES}; | ||
| 1090 | VkPhysicalDeviceProperties2 properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &modes}; | ||
| 1091 | d.instance_fn<PFN_vkGetPhysicalDeviceProperties2>("vkGetPhysicalDeviceProperties2")(d.physical, &properties); | ||
| 1092 | if (!d.depth_format_usable(VK_FORMAT_D32_SFLOAT) || !(modes.supportedDepthResolveModes & VK_RESOLVE_MODE_MIN_BIT)) { | ||
| 1093 | std::printf("I: skipped, no D32_SFLOAT or no MIN depth resolve\n"); | ||
| 1094 | return 0; | ||
| 1095 | } | ||
| 1096 | constexpr std::uint32_t width = 8, height = 6, texels = width * height; | ||
| 1097 | constexpr VkDeviceSize scratch_bytes = 65536; | ||
| 1098 | constexpr Rect rect{2, 1, 7, 5}; | ||
| 1099 | const VkImage msaa = d.image(VK_FORMAT_D32_SFLOAT, width, height, 1, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, 1, 4); | ||
| 1100 | const VkImage single = d.image(VK_FORMAT_D32_SFLOAT, width, height, 1, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT); | ||
| 1101 | void *down_mapped = nullptr, *scratch_mapped = nullptr; | ||
| 1102 | const VkBuffer down = d.buffer(VkDeviceSize(texels) * 4, VK_BUFFER_USAGE_TRANSFER_DST_BIT, &down_mapped); | ||
| 1103 | const VkBuffer scratch = d.buffer(scratch_bytes, VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, | ||
| 1104 | &scratch_mapped); | ||
| 1105 | EXPECT(msaa && single && down && scratch); | ||
| 1106 | |||
| 1107 | const auto image_binding = [&](VkImage image, std::uint32_t samples) { | ||
| 1108 | ResourceBinding b{}; | ||
| 1109 | b.image = image; | ||
| 1110 | b.format = VK_FORMAT_D32_SFLOAT; | ||
| 1111 | b.aspects = VK_IMAGE_ASPECT_DEPTH_BIT; | ||
| 1112 | b.width = width; | ||
| 1113 | b.height = height; | ||
| 1114 | b.samples = samples; | ||
| 1115 | return b; | ||
| 1116 | }; | ||
| 1117 | constexpr ResourceId msaa_id = 50, single_id = 51; | ||
| 1118 | const std::map<ResourceId, ResourceBinding> bindings{ | ||
| 1119 | {msaa_id, image_binding(msaa, 4)}, | ||
| 1120 | {single_id, image_binding(single, 1)}, | ||
| 1121 | }; | ||
| 1122 | const auto resolve = [&](ResourceId id) -> const ResourceBinding* { | ||
| 1123 | const auto it = bindings.find(id); | ||
| 1124 | return it == bindings.end() ? nullptr : &it->second; | ||
| 1125 | }; | ||
| 1126 | LayoutTracker tracker(true); | ||
| 1127 | for (const ResourceId id : {msaa_id, single_id}) tracker.track(id, bindings.at(id), VK_IMAGE_LAYOUT_UNDEFINED); | ||
| 1128 | |||
| 1129 | Views views{d, {}}; | ||
| 1130 | ReplayFunctions vk{}; | ||
| 1131 | vk.pipeline_barrier = d.fn<PFN_vkCmdPipelineBarrier>("vkCmdPipelineBarrier"); | ||
| 1132 | vk.clear_depth_stencil_image = d.fn<PFN_vkCmdClearDepthStencilImage>("vkCmdClearDepthStencilImage"); | ||
| 1133 | vk.copy_buffer_to_image = d.fn<PFN_vkCmdCopyBufferToImage>("vkCmdCopyBufferToImage"); | ||
| 1134 | vk.copy_image_to_buffer = d.fn<PFN_vkCmdCopyImageToBuffer>("vkCmdCopyImageToBuffer"); | ||
| 1135 | vk.resolve_image = d.fn<PFN_vkCmdResolveImage>("vkCmdResolveImage"); | ||
| 1136 | vk.begin_rendering = d.fn<PFN_vkCmdBeginRendering>("vkCmdBeginRendering"); | ||
| 1137 | vk.end_rendering = d.fn<PFN_vkCmdEndRendering>("vkCmdEndRendering"); | ||
| 1138 | vk.image_view = [&](const ResourceBinding& r, const VkImageSubresourceRange& range) { return views(r, range); }; | ||
| 1139 | vk.separate_depth_stencil_layouts = true; // matches LayoutTracker | ||
| 1140 | const ReplayFunctions without_scratch = vk; | ||
| 1141 | VkDeviceSize scratch_used = 0; | ||
| 1142 | vk.copy_scratch = [&](VkDeviceSize n, VkBuffer& buffer, VkDeviceSize& offset) { | ||
| 1143 | if (n > scratch_bytes - scratch_used) return false; | ||
| 1144 | buffer = scratch; | ||
| 1145 | offset = scratch_used; | ||
| 1146 | scratch_used += (n + 255) / 256 * 256; | ||
| 1147 | return true; | ||
| 1148 | }; | ||
| 1149 | |||
| 1150 | CommandList list; | ||
| 1151 | EXPECT(list.clear_depth_stencil({msaa_id, 0, 0, 1}, clear_depth, 0.75f, 0)); | ||
| 1152 | EXPECT(list.clear_depth_stencil({single_id, 0, 0, 1}, clear_depth, 0.25f, 0)); | ||
| 1153 | EXPECT(list.transition({msaa_id, 0, s::depth_write, s::resolve_source})); | ||
| 1154 | EXPECT(list.transition({single_id, 0, s::depth_write, s::resolve_dest})); | ||
| 1155 | EXPECT(list.resolve_subresource( | ||
| 1156 | {single_id, 0, msaa_id, 0, ResolveRegion{2, 1, rect, d3d12_resolve_mode::minimum}})); | ||
| 1157 | EXPECT(list.transition({single_id, 0, s::resolve_dest, s::copy_source})); | ||
| 1158 | EXPECT(list.close()); | ||
| 1159 | |||
| 1160 | const unsigned errors_before = d.validation_errors(); | ||
| 1161 | const bool ok = d.run([&](VkCommandBuffer cb) { | ||
| 1162 | if (replay(list, cb, resolve, without_scratch, &tracker) != ReplayResult::unsupported) return false; | ||
| 1163 | if (replay(list, cb, resolve, vk, &tracker) != ReplayResult::success) return false; | ||
| 1164 | d.read_back(cb, single, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_ASPECT_DEPTH_BIT, width, height, 1, down, | ||
| 1165 | 0); | ||
| 1166 | return true; | ||
| 1167 | }); | ||
| 1168 | EXPECT(ok); | ||
| 1169 | EXPECT(scratch_used > 0); | ||
| 1170 | const auto* read = static_cast<const std::uint8_t*>(down_mapped); | ||
| 1171 | unsigned outside_written = 0; | ||
| 1172 | for (std::uint32_t i = 0; i < texels; ++i) { | ||
| 1173 | const auto x = std::int32_t(i % width), y = std::int32_t(i / width); | ||
| 1174 | const bool inside = x >= rect.left && x < rect.right && y >= rect.top && y < rect.bottom; | ||
| 1175 | float got = 0; | ||
| 1176 | std::memcpy(&got, read + i * 4, 4); | ||
| 1177 | if (!inside && got != 0.25f) ++outside_written; | ||
| 1178 | expect_depth("I resolve", read, i, inside ? 0.75f : 0.25f); | ||
| 1179 | } | ||
| 1180 | if (outside_written) { | ||
| 1181 | std::fprintf(stderr, "I: %u texels outside the rectangle written\n", outside_written); | ||
| 1182 | return 0; // counted in g_mismatches | ||
| 1183 | } | ||
| 1184 | const unsigned errors = d.validation_errors() - errors_before; | ||
| 1185 | std::printf("I: a 4x D32 MIN resolve of a %dx%d rectangle, %u texels around it kept%s\n", rect.right - rect.left, | ||
| 1186 | rect.bottom - rect.top, texels - unsigned((rect.right - rect.left) * (rect.bottom - rect.top)), | ||
| 1187 | d.validation ? (errors ? ": validation errors" : ", no validation errors") : " (validation off)"); | ||
| 1188 | return 0; | ||
| 1189 | } | ||
| 1190 | |||
| 1082 | } // namespace | 1191 | } // namespace |
| 1083 | 1192 | ||
| 1084 | int main() { | 1193 | int main() { |
| 1085 | vktest::Device device; | 1194 | vktest::Device device; |
| 1086 | EXPECT(device.create()); | 1195 | EXPECT(device.create()); |
| 1087 | if (part_a(device) || part_b(device) || part_c(device) || part_d(device) || part_e(device) || part_f(device) || | 1196 | if (part_a(device) || part_b(device) || part_c(device) || part_d(device) || part_e(device) || part_f(device) || |
| 1088 | part_g(device) || part_h(device)) | 1197 | part_g(device) || part_h(device) || part_i(device)) |
| 1089 | return 1; | 1198 | return 1; |
| 1090 | if (g_mismatches) return 1; | 1199 | if (g_mismatches) return 1; |
| 1091 | if (device.validation_errors()) { | 1200 | if (device.validation_errors()) { |