diff options
| author | lloyd <nichlloyd@gmail.com> | 2026-11-22 12:10:35 +0200 |
|---|---|---|
| committer | lloyd <nichlloyd@gmail.com> | 2026-11-22 13:33:50 +0200 |
| commit | e1b3fca96d5d004e8b20de06a5a148b7da1b4363 (patch) | |
| tree | 9e0855d9e983ea745c3304a529584b05fb0760d1 /runtime/src | |
| parent | bb2e18421dac527374eb837c88b5063c5d96f81f (diff) | |
Runtime: depth <-> colour copies through a scratch buffer
CopyResource and CopyTextureRegion between D32_FLOAT and R32_FLOAT/UINT/SINT,
and between D16_UNORM and R16_FLOAT/UNORM, were refused at execution because
vkCmdCopyImage cannot mix a depth and a colour aspect. Host D3D12 and WARP
copy the bits, and #1134 listed these as not covered.
The replay now stages each such region image -> scratch buffer -> image.
The scratch comes from a new ScratchBuffers pool (vulkan_scratch.h):
device-local chunks handed out while a list is prepared and retired with
the submission's timeline value, like the depth24 converter's scratch, but
enabled on every GPU. Without it the copy is refused as unsupported before
anything is written.
Still refused: depth with stencil (two planes), D24 stored as D32, and
multisampled textures.
Tests: d3d12_copy_xfmt gains five depth <-> colour cases (R16 -> D16 both
ways, D32 -> R32_UINT, three mips, and a D32 -> R16 negative control), and a
depth <-> colour mismatch now fails it. vk_command_replay_test part H runs a
D32 -> R32 -> D32 round trip through the replay, and checks that a list is
refused with no scratch.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 38 | ||||
| -rw-r--r-- | runtime/src/vulkan_replay.h | 92 | ||||
| -rw-r--r-- | runtime/src/vulkan_scratch.h | 179 |
3 files changed, 304 insertions, 5 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 0bb6e54f2..b69b4d698 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -11,6 +11,7 @@ | |||
| 11 | #include "binary_occlusion_spirv.h" | 11 | #include "binary_occlusion_spirv.h" |
| 12 | #include "occlusion_sum_spirv.h" | 12 | #include "occlusion_sum_spirv.h" |
| 13 | #include "vulkan_depth24.h" | 13 | #include "vulkan_depth24.h" |
| 14 | #include "vulkan_scratch.h" | ||
| 14 | #include "vulkan_replay.h" | 15 | #include "vulkan_replay.h" |
| 15 | 16 | ||
| 16 | #include <algorithm> | 17 | #include <algorithm> |
| @@ -827,6 +828,14 @@ public: | |||
| 827 | }; | 828 | }; |
| 828 | } | 829 | } |
| 829 | 830 | ||
| 831 | // Copies the replay stages through a buffer (depth <-> colour, vulkan_replay.h) take device-local scratch from | ||
| 832 | // `allocate`. Without this such a copy is refused as unsupported. | ||
| 833 | void enable_copy_scratch(PFN_vkGetDeviceProcAddr get, ScratchBuffers::Allocate allocate) { | ||
| 834 | std::unique_lock lock(resource_mutex_); | ||
| 835 | scratch_ = std::make_unique<ScratchBuffers>(get, device_, std::move(allocate)); | ||
| 836 | if (!scratch_->loaded()) scratch_.reset(); | ||
| 837 | } | ||
| 838 | |||
| 830 | // Called after that command buffer's submit: true when it was accepted, false when vkQueueSubmit failed. The | 839 | // Called after that command buffer's submit: true when it was accepted, false when vkQueueSubmit failed. The |
| 831 | // set keeps its upload pending in the second case, because the copy never reached the GPU. | 840 | // set keeps its upload pending in the second case, because the copy never reached the GPU. |
| 832 | void set_bindless_commit(std::function<void(bool)> commit) { | 841 | void set_bindless_commit(std::function<void(bool)> commit) { |
| @@ -939,6 +948,7 @@ private: | |||
| 939 | std::map<std::tuple<VkImage, VkImageAspectFlags, std::uint32_t, std::uint32_t, std::uint32_t, VkFormat>, VkImageView> views_; | 948 | std::map<std::tuple<VkImage, VkImageAspectFlags, std::uint32_t, std::uint32_t, std::uint32_t, VkFormat>, VkImageView> views_; |
| 940 | std::function<bool(VkCommandBuffer)> bindless_prepare_; | 949 | std::function<bool(VkCommandBuffer)> bindless_prepare_; |
| 941 | std::unique_ptr<Depth24Converter> depth24_; | 950 | std::unique_ptr<Depth24Converter> depth24_; |
| 951 | std::unique_ptr<ScratchBuffers> scratch_; // copies staged through a buffer (depth <-> colour) | ||
| 942 | std::function<void(bool)> bindless_commit_; | 952 | std::function<void(bool)> bindless_commit_; |
| 943 | std::unordered_map<QueryHeapId, QueryPool> query_pools_; // element pointers stay valid across inserts | 953 | std::unordered_map<QueryHeapId, QueryPool> query_pools_; // element pointers stay valid across inserts |
| 944 | double timestamp_period_ = 0; | 954 | double timestamp_period_ = 0; |
| @@ -1045,6 +1055,7 @@ public: | |||
| 1045 | if (progress_) { | 1055 | if (progress_) { |
| 1046 | std::lock_guard lock(context_.tracker_mutex_); | 1056 | std::lock_guard lock(context_.tracker_mutex_); |
| 1047 | if (context_.depth24_) context_.depth24_->forget(progress_); // its scratch is idle now | 1057 | if (context_.depth24_) context_.depth24_->forget(progress_); // its scratch is idle now |
| 1058 | if (context_.scratch_) context_.scratch_->forget(progress_); | ||
| 1048 | } | 1059 | } |
| 1049 | if (pool_) vk.vkDestroyCommandPool(context_.device(), pool_, nullptr); | 1060 | if (pool_) vk.vkDestroyCommandPool(context_.device(), pool_, nullptr); |
| 1050 | if (progress_) vk.vkDestroySemaphore(context_.device(), progress_, nullptr); | 1061 | if (progress_) vk.vkDestroySemaphore(context_.device(), progress_, nullptr); |
| @@ -1077,6 +1088,14 @@ public: | |||
| 1077 | if (active && d24) d24->retire(progress, 0); | 1088 | if (active && d24) d24->retire(progress, 0); |
| 1078 | } | 1089 | } |
| 1079 | } depth24_guard{context_.depth24_.get(), progress_, false}; | 1090 | } depth24_guard{context_.depth24_.get(), progress_, false}; |
| 1091 | struct ScratchGuard { // the same for the copy scratch | ||
| 1092 | ScratchBuffers* scratch = nullptr; | ||
| 1093 | VkSemaphore progress = VK_NULL_HANDLE; | ||
| 1094 | bool active = false; | ||
| 1095 | ~ScratchGuard() { | ||
| 1096 | if (active && scratch) scratch->retire(progress, 0); | ||
| 1097 | } | ||
| 1098 | } scratch_guard{context_.scratch_.get(), progress_, false}; | ||
| 1080 | bindless_commit = context_.bindless_commit_; | 1099 | bindless_commit = context_.bindless_commit_; |
| 1081 | std::vector<PreparedList> prepared(lists.size()); | 1100 | std::vector<PreparedList> prepared(lists.size()); |
| 1082 | ReplayFunctions replay = context_.replay_; | 1101 | ReplayFunctions replay = context_.replay_; |
| @@ -1112,6 +1131,17 @@ public: | |||
| 1112 | }; | 1131 | }; |
| 1113 | } | 1132 | } |
| 1114 | } | 1133 | } |
| 1134 | if (context_.scratch_) { | ||
| 1135 | replay.copy_scratch = [this, &tracker_lock, &scratch_guard](VkDeviceSize bytes, VkBuffer& buffer, | ||
| 1136 | VkDeviceSize& offset) { | ||
| 1137 | if (!tracker_lock.owns_lock()) tracker_lock.lock(); | ||
| 1138 | if (!scratch_guard.active) { | ||
| 1139 | context_.scratch_->collect(); | ||
| 1140 | scratch_guard.active = true; | ||
| 1141 | } | ||
| 1142 | return context_.scratch_->get(bytes, buffer, offset); | ||
| 1143 | }; | ||
| 1144 | } | ||
| 1115 | if (context_.depth24_) { | 1145 | if (context_.depth24_) { |
| 1116 | replay.depth24_prepare = [this, &tracker_lock, &depth24_guard](Depth24Job& job, | 1146 | replay.depth24_prepare = [this, &tracker_lock, &depth24_guard](Depth24Job& job, |
| 1117 | std::vector<Depth24Job>& parts) { | 1147 | std::vector<Depth24Job>& parts) { |
| @@ -1195,6 +1225,10 @@ public: | |||
| 1195 | context_.depth24_->retire(progress_, 0); | 1225 | context_.depth24_->retire(progress_, 0); |
| 1196 | depth24_guard.active = false; | 1226 | depth24_guard.active = false; |
| 1197 | } | 1227 | } |
| 1228 | if (scratch_guard.active) { | ||
| 1229 | context_.scratch_->retire(progress_, 0); | ||
| 1230 | scratch_guard.active = false; | ||
| 1231 | } | ||
| 1198 | tracker_lock.unlock(); | 1232 | tracker_lock.unlock(); |
| 1199 | if (bindless_commit) bindless_commit(submitted); | 1233 | if (bindless_commit) bindless_commit(submitted); |
| 1200 | return ReplayResult::unsupported; | 1234 | return ReplayResult::unsupported; |
| @@ -1255,6 +1289,10 @@ public: | |||
| 1255 | context_.depth24_->retire(progress_, submitted ? submitted_ : 0); | 1289 | context_.depth24_->retire(progress_, submitted ? submitted_ : 0); |
| 1256 | depth24_guard.active = false; | 1290 | depth24_guard.active = false; |
| 1257 | } | 1291 | } |
| 1292 | if (scratch_guard.active) { | ||
| 1293 | context_.scratch_->retire(progress_, submitted ? submitted_ : 0); | ||
| 1294 | scratch_guard.active = false; | ||
| 1295 | } | ||
| 1258 | tracker_lock.unlock(); | 1296 | tracker_lock.unlock(); |
| 1259 | if (bindless_commit) bindless_commit(submitted); | 1297 | if (bindless_commit) bindless_commit(submitted); |
| 1260 | return submitted ? ReplayResult::success : ReplayResult::unsupported; | 1298 | return submitted ? ReplayResult::success : ReplayResult::unsupported; |
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 5fd1c8fff..7a9dab564 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h | |||
| @@ -474,6 +474,34 @@ inline bool two_planes(const ResourceBinding& resource) { | |||
| 474 | return (resource.aspects & depth_stencil_aspects) == depth_stencil_aspects; | 474 | return (resource.aspects & depth_stencil_aspects) == depth_stencil_aspects; |
| 475 | } | 475 | } |
| 476 | 476 | ||
| 477 | // D3D12 copies between a depth format and a colour format of the same typeless family, bit for bit: D32_FLOAT with | ||
| 478 | // R32_FLOAT/UINT/SINT (R32_TYPELESS) and D16_UNORM with R16_FLOAT/UNORM (R16_TYPELESS; the other R16 formats have no | ||
| 479 | // copy layout here yet). Single-sampled, single-plane depth only: D24 and D32 with stencil have two planes, and a D24 | ||
| 480 | // stored as D32 would need the depth24 conversion. Vulkan's vkCmdCopyImage refuses mismatched aspects, so the replay | ||
| 481 | // stages these through a buffer. | ||
| 482 | inline bool depth_colour_pair(const ResourceBinding& a, const ResourceBinding& b) { | ||
| 483 | const auto family = [](VkFormat format) -> int { | ||
| 484 | switch (format) { | ||
| 485 | case VK_FORMAT_D32_SFLOAT: | ||
| 486 | case VK_FORMAT_R32_SFLOAT: | ||
| 487 | case VK_FORMAT_R32_UINT: | ||
| 488 | case VK_FORMAT_R32_SINT: return 32; | ||
| 489 | case VK_FORMAT_D16_UNORM: | ||
| 490 | case VK_FORMAT_R16_SFLOAT: | ||
| 491 | case VK_FORMAT_R16_UNORM: return 16; | ||
| 492 | default: return 0; | ||
| 493 | } | ||
| 494 | }; | ||
| 495 | const auto depth_only = [](const ResourceBinding& r) { | ||
| 496 | return r.aspects == VK_IMAGE_ASPECT_DEPTH_BIT && !r.depth24_as_float; | ||
| 497 | }; | ||
| 498 | const ResourceBinding *depth = &a, *colour = &b; | ||
| 499 | if (!depth_only(*depth)) std::swap(depth, colour); | ||
| 500 | return depth_only(*depth) && colour->aspects == VK_IMAGE_ASPECT_COLOR_BIT && a.image && b.image && | ||
| 501 | a.samples == 1 && b.samples == 1 && !a.volume && !b.volume && family(a.format) && | ||
| 502 | family(a.format) == family(b.format); | ||
| 503 | } | ||
| 504 | |||
| 477 | // Decodes a D3D12 subresource index. Combined depth-stencil images have two planes (0 depth, 1 stencil); | 505 | // Decodes a D3D12 subresource index. Combined depth-stencil images have two planes (0 depth, 1 stencil); |
| 478 | // every other image has one. | 506 | // every other image has one. |
| 479 | inline bool image_range(const ResourceBinding& resource, std::uint32_t subresource, VkImageSubresourceRange& out) { | 507 | inline bool image_range(const ResourceBinding& resource, std::uint32_t subresource, VkImageSubresourceRange& out) { |
| @@ -702,6 +730,9 @@ struct ReplayFunctions { | |||
| 702 | // for a job while the list is checked, `depth24_run` records its dispatch. Without them such a copy is unsupported. | 730 | // for a job while the list is checked, `depth24_run` records its dispatch. Without them such a copy is unsupported. |
| 703 | std::function<bool(Depth24Job&, std::vector<Depth24Job>&)> depth24_prepare; // whole copy in, dispatches out | 731 | std::function<bool(Depth24Job&, std::vector<Depth24Job>&)> depth24_prepare; // whole copy in, dispatches out |
| 704 | std::function<void(VkCommandBuffer, const Depth24Job&)> depth24_run; | 732 | std::function<void(VkCommandBuffer, const Depth24Job&)> depth24_run; |
| 733 | // `bytes` of transfer scratch that stays alive until the submission completes (vulkan_scratch.h): depth <-> colour | ||
| 734 | // copies go through it, image to buffer to image. Without it such a copy is unsupported. | ||
| 735 | std::function<bool(VkDeviceSize bytes, VkBuffer&, VkDeviceSize&)> copy_scratch; | ||
| 705 | // Returns the query heap with that ID, or nullptr; it stays valid while the lists that use it run. | 736 | // Returns the query heap with that ID, or nullptr; it stays valid while the lists that use it run. |
| 706 | std::function<QueryPool*(QueryHeapId)> query_pool; | 737 | std::function<QueryPool*(QueryHeapId)> query_pool; |
| 707 | bool separate_depth_stencil_layouts = false; // must match the LayoutTracker, if one is used | 738 | bool separate_depth_stencil_layouts = false; // must match the LayoutTracker, if one is used |
| @@ -766,6 +797,9 @@ struct Prepared { | |||
| 766 | std::uint32_t rgb96_slice_words = 0; | 797 | std::uint32_t rgb96_slice_words = 0; |
| 767 | Depth24Job depth24; // the whole copy: where its scratch floats are | 798 | Depth24Job depth24; // the whole copy: where its scratch floats are |
| 768 | std::vector<Depth24Job> depth24_parts; // the dispatches that convert it | 799 | std::vector<Depth24Job> depth24_parts; // the dispatches that convert it |
| 800 | // A depth <-> colour copy: the source into `staged` (image to buffer), then `staged` into the destination. | ||
| 801 | VkBuffer staged = VK_NULL_HANDLE; | ||
| 802 | std::vector<VkBufferImageCopy> staged_reads, staged_writes; | ||
| 769 | std::vector<VkImageSubresourceRange> discards; // DiscardResource: the subresources to discard | 803 | std::vector<VkImageSubresourceRange> discards; // DiscardResource: the subresources to discard |
| 770 | VkBuffer write_buffer = VK_NULL_HANDLE; // WriteBufferImmediate: where the address lands | 804 | VkBuffer write_buffer = VK_NULL_HANDLE; // WriteBufferImmediate: where the address lands |
| 771 | VkDeviceSize write_offset = 0; | 805 | VkDeviceSize write_offset = 0; |
| @@ -1071,8 +1105,9 @@ inline ReplayResult prepare_texture_copy(const CopyTexture& copy, Prepared& p) { | |||
| 1071 | if (!copy_subresource(p.binding, copy.dst.subresource, dst) || | 1105 | if (!copy_subresource(p.binding, copy.dst.subresource, dst) || |
| 1072 | !copy_subresource(p.source, copy.src.subresource, src)) | 1106 | !copy_subresource(p.source, copy.src.subresource, src)) |
| 1073 | return ReplayResult::invalid_subresource; | 1107 | return ReplayResult::invalid_subresource; |
| 1074 | if (dst.aspectMask != src.aspectMask || p.binding.volume != p.source.volume || | 1108 | if ((dst.aspectMask != src.aspectMask && !depth_colour_pair(p.binding, p.source)) || |
| 1075 | p.binding.samples != p.source.samples || p.binding.rgb96 != p.source.rgb96) | 1109 | p.binding.volume != p.source.volume || p.binding.samples != p.source.samples || |
| 1110 | p.binding.rgb96 != p.source.rgb96) | ||
| 1076 | return ReplayResult::invalid_copy; | 1111 | return ReplayResult::invalid_copy; |
| 1077 | const VkExtent2D from = mip_extent(p.source, src.baseMipLevel), to = mip_extent(p.binding, dst.baseMipLevel); | 1112 | const VkExtent2D from = mip_extent(p.source, src.baseMipLevel), to = mip_extent(p.binding, dst.baseMipLevel); |
| 1078 | const auto from_depth = std::max(1u, p.source.depth >> src.baseMipLevel); | 1113 | const auto from_depth = std::max(1u, p.source.depth >> src.baseMipLevel); |
| @@ -1243,12 +1278,15 @@ inline ReplayResult prepare_resource_copy(Prepared& p) { | |||
| 1243 | blocks(dst.height, db) == blocks(src.height, sb); | 1278 | blocks(dst.height, db) == blocks(src.height, sb); |
| 1244 | if (!dst.image || !src.image || !same_shape || | 1279 | if (!dst.image || !src.image || !same_shape || |
| 1245 | dst.depth != src.depth || dst.volume != src.volume || dst.mip_levels != src.mip_levels || | 1280 | dst.depth != src.depth || dst.volume != src.volume || dst.mip_levels != src.mip_levels || |
| 1246 | dst.array_layers != src.array_layers || dst.aspects != src.aspects || dst.samples != src.samples || | 1281 | dst.array_layers != src.array_layers || (dst.aspects != src.aspects && !depth_colour_pair(dst, src)) || |
| 1247 | dst.rgb96 != src.rgb96 || | 1282 | dst.samples != src.samples || dst.rgb96 != src.rgb96 || |
| 1248 | block_format(dst.format, VK_IMAGE_ASPECT_COLOR_BIT).bytes != block_format(src.format, VK_IMAGE_ASPECT_COLOR_BIT).bytes) | 1283 | block_format(dst.format, VK_IMAGE_ASPECT_COLOR_BIT).bytes != block_format(src.format, VK_IMAGE_ASPECT_COLOR_BIT).bytes) |
| 1249 | return ReplayResult::invalid_copy; | 1284 | return ReplayResult::invalid_copy; |
| 1285 | // A depth <-> colour pair has one aspect on each side; the copies name each side's own (staged by the caller). | ||
| 1286 | const bool cross = dst.aspects != src.aspects; | ||
| 1250 | for (const VkImageAspectFlags aspect : {VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_ASPECT_DEPTH_BIT, VK_IMAGE_ASPECT_STENCIL_BIT}) { | 1287 | for (const VkImageAspectFlags aspect : {VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_ASPECT_DEPTH_BIT, VK_IMAGE_ASPECT_STENCIL_BIT}) { |
| 1251 | if (!(dst.aspects & aspect)) continue; | 1288 | if (!(dst.aspects & aspect)) continue; |
| 1289 | const VkImageAspectFlags src_aspect = cross ? src.aspects : aspect; | ||
| 1252 | for (std::uint32_t mip = 0; mip < dst.mip_levels; ++mip) { | 1290 | for (std::uint32_t mip = 0; mip < dst.mip_levels; ++mip) { |
| 1253 | const VkExtent2D extent = mip_extent(src, mip), to = mip_extent(dst, mip); | 1291 | const VkExtent2D extent = mip_extent(src, mip), to = mip_extent(dst, mip); |
| 1254 | // Lower mips can differ in blocks (BC 12x8 mip 1 is 2x1 blocks, R32G32 3x2 mip 1 is 1x1): more source | 1292 | // Lower mips can differ in blocks (BC 12x8 mip 1 is 2x1 blocks, R32G32 3x2 mip 1 is 1x1): more source |
| @@ -1257,7 +1295,7 @@ inline ReplayResult prepare_resource_copy(Prepared& p) { | |||
| 1257 | // a copy is staged through a buffer. | 1295 | // a copy is staged through a buffer. |
| 1258 | if (sb != db && (blocks(extent.width, sb) * db > to.width || blocks(extent.height, sb) * db > to.height)) | 1296 | if (sb != db && (blocks(extent.width, sb) * db > to.width || blocks(extent.height, sb) * db > to.height)) |
| 1259 | return ReplayResult::invalid_copy; | 1297 | return ReplayResult::invalid_copy; |
| 1260 | p.image_copies.push_back({{aspect, mip, 0, dst.array_layers}, {0, 0, 0}, {aspect, mip, 0, dst.array_layers}, | 1298 | p.image_copies.push_back({{src_aspect, mip, 0, dst.array_layers}, {0, 0, 0}, {aspect, mip, 0, dst.array_layers}, |
| 1261 | {0, 0, 0}, {extent.width, extent.height, std::max(1u, src.depth >> mip)}}); | 1299 | {0, 0, 0}, {extent.width, extent.height, std::max(1u, src.depth >> mip)}}); |
| 1262 | } | 1300 | } |
| 1263 | } | 1301 | } |
| @@ -1708,6 +1746,36 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 1708 | } else { | 1746 | } else { |
| 1709 | result = detail::prepare_resource_copy(p); | 1747 | result = detail::prepare_resource_copy(p); |
| 1710 | } | 1748 | } |
| 1749 | if (result == ReplayResult::success && !p.image_copies.empty() && | ||
| 1750 | p.image_copies[0].srcSubresource.aspectMask != p.image_copies[0].dstSubresource.aspectMask) { | ||
| 1751 | // Depth <-> colour: vkCmdCopyImage cannot mix aspects, so each region goes image -> scratch -> image, | ||
| 1752 | // tightly packed. Depth aspects want 4-byte buffer offsets; 16 covers every texel size here. | ||
| 1753 | const std::uint32_t texel = | ||
| 1754 | block_format(p.source.format, p.image_copies[0].srcSubresource.aspectMask).bytes; | ||
| 1755 | VkDeviceSize total = 0; | ||
| 1756 | std::vector<VkDeviceSize> offsets; | ||
| 1757 | for (const VkImageCopy& c : p.image_copies) { | ||
| 1758 | offsets.push_back(total); | ||
| 1759 | total += (VkDeviceSize(c.extent.width) * c.extent.height * c.extent.depth * | ||
| 1760 | c.srcSubresource.layerCount * texel + 15) / 16 * 16; | ||
| 1761 | } | ||
| 1762 | VkDeviceSize base = 0; | ||
| 1763 | if (!texel || !vk.copy_scratch || !vk.copy_scratch(total, p.staged, base)) | ||
| 1764 | return ReplayResult::unsupported; | ||
| 1765 | for (std::size_t r = 0; r < p.image_copies.size(); ++r) { | ||
| 1766 | const VkImageCopy& c = p.image_copies[r]; | ||
| 1767 | VkBufferImageCopy region{}; | ||
| 1768 | region.bufferOffset = base + offsets[r]; | ||
| 1769 | region.imageSubresource = c.srcSubresource; | ||
| 1770 | region.imageOffset = c.srcOffset; | ||
| 1771 | region.imageExtent = c.extent; | ||
| 1772 | p.staged_reads.push_back(region); | ||
| 1773 | region.imageSubresource = c.dstSubresource; | ||
| 1774 | region.imageOffset = c.dstOffset; | ||
| 1775 | p.staged_writes.push_back(region); | ||
| 1776 | } | ||
| 1777 | p.image_copies.clear(); | ||
| 1778 | } | ||
| 1711 | if (result != ReplayResult::success) return result; | 1779 | if (result != ReplayResult::success) return result; |
| 1712 | } else if (const auto* resolve_command = std::get_if<ResolveSubresource>(&command)) { | 1780 | } else if (const auto* resolve_command = std::get_if<ResolveSubresource>(&command)) { |
| 1713 | if (!vk.resolve_image) return ReplayResult::unsupported; | 1781 | if (!vk.resolve_image) return ReplayResult::unsupported; |
| @@ -2290,6 +2358,20 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 2290 | } else if (!p.buffer_copies.empty()) { | 2358 | } else if (!p.buffer_copies.empty()) { |
| 2291 | vk.copy_buffer(command_buffer, p.source.buffer, p.binding.buffer, std::uint32_t(p.buffer_copies.size()), | 2359 | vk.copy_buffer(command_buffer, p.source.buffer, p.binding.buffer, std::uint32_t(p.buffer_copies.size()), |
| 2292 | p.buffer_copies.data()); | 2360 | p.buffer_copies.data()); |
| 2361 | } else if (p.staged) { | ||
| 2362 | // Depth <-> colour: the source into the scratch, then the scratch into the destination. | ||
| 2363 | const VkImageLayout src_layout = detail::copy_layout( | ||
| 2364 | vk, command_buffer, p.source, src_id, detail::planes_for_layout(p.source, p.source_range, separate), | ||
| 2365 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_ACCESS_TRANSFER_READ_BIT, tracker); | ||
| 2366 | vk.copy_image_to_buffer(command_buffer, p.source.image, src_layout, p.staged, | ||
| 2367 | std::uint32_t(p.staged_reads.size()), p.staged_reads.data()); | ||
| 2368 | detail::memory_barrier(vk, command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, | ||
| 2369 | VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); | ||
| 2370 | const VkImageLayout dst_layout = detail::copy_layout( | ||
| 2371 | vk, command_buffer, p.binding, dst_id, detail::planes_for_layout(p.binding, p.range, separate), | ||
| 2372 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT, tracker); | ||
| 2373 | vk.copy_buffer_to_image(command_buffer, p.staged, p.binding.image, dst_layout, | ||
| 2374 | std::uint32_t(p.staged_writes.size()), p.staged_writes.data()); | ||
| 2293 | } else if (!p.image_copies.empty()) { | 2375 | } else if (!p.image_copies.empty()) { |
| 2294 | const VkImageLayout src_layout = detail::copy_layout( | 2376 | const VkImageLayout src_layout = detail::copy_layout( |
| 2295 | vk, command_buffer, p.source, src_id, detail::planes_for_layout(p.source, p.source_range, separate), | 2377 | vk, command_buffer, p.source, src_id, detail::planes_for_layout(p.source, p.source_range, separate), |
diff --git a/runtime/src/vulkan_scratch.h b/runtime/src/vulkan_scratch.h new file mode 100644 index 000000000..fe29d1a5c --- /dev/null +++ b/runtime/src/vulkan_scratch.h | |||
| @@ -0,0 +1,179 @@ | |||
| 1 | // Scratch buffers that live until the submission that used them has completed: device-local chunks handed out | ||
| 2 | // while a list is prepared, retired with the submission's timeline value, and reused (or freed) once the GPU is past | ||
| 3 | // it. The replay stages copies Vulkan cannot do in one command through them (vulkan_replay.h, depth <-> colour | ||
| 4 | // copies). Single-threaded: the caller holds the context's lock. The same life cycle as the depth24 converter's own | ||
| 5 | // scratch (vulkan_depth24.h), which also needs descriptor pools and exists only on GPUs without D24. | ||
| 6 | #pragma once | ||
| 7 | |||
| 8 | #ifndef VK_NO_PROTOTYPES | ||
| 9 | #define VK_NO_PROTOTYPES | ||
| 10 | #endif | ||
| 11 | #include <vulkan/vulkan.h> | ||
| 12 | |||
| 13 | #include <algorithm> | ||
| 14 | #include <cstdint> | ||
| 15 | #include <functional> | ||
| 16 | #include <utility> | ||
| 17 | #include <vector> | ||
| 18 | |||
| 19 | namespace leonida { | ||
| 20 | |||
| 21 | class ScratchBuffers { | ||
| 22 | public: | ||
| 23 | // A device-local allocation for a chunk; null on failure. | ||
| 24 | using Allocate = std::function<VkDeviceMemory(const VkMemoryRequirements&)>; | ||
| 25 | |||
| 26 | ScratchBuffers(PFN_vkGetDeviceProcAddr get, VkDevice device, Allocate allocate) | ||
| 27 | : device_(device), allocate_(std::move(allocate)) { | ||
| 28 | loaded_ = vk_.load(get, device); | ||
| 29 | } | ||
| 30 | |||
| 31 | ~ScratchBuffers() { | ||
| 32 | if (!loaded_) return; | ||
| 33 | vk_.vkDeviceWaitIdle(device_); | ||
| 34 | release(current_); | ||
| 35 | for (auto& batch : retired_) release(batch); | ||
| 36 | for (auto& chunk : spare_) destroy(chunk); | ||
| 37 | } | ||
| 38 | |||
| 39 | ScratchBuffers(const ScratchBuffers&) = delete; | ||
| 40 | ScratchBuffers& operator=(const ScratchBuffers&) = delete; | ||
| 41 | |||
| 42 | bool loaded() const { return loaded_; } | ||
| 43 | |||
| 44 | // `bytes` of transfer scratch for the next submission, at a 256-byte aligned offset (more than any copy needs). | ||
| 45 | bool get(std::uint64_t bytes, VkBuffer& buffer, VkDeviceSize& offset) { | ||
| 46 | constexpr VkDeviceSize alignment = 256, minimum = 4u << 20; | ||
| 47 | if (!loaded_ || !bytes) return false; | ||
| 48 | for (auto& chunk : current_.chunks) { | ||
| 49 | const VkDeviceSize start = (chunk.used + alignment - 1) / alignment * alignment; | ||
| 50 | if (start <= chunk.size && bytes <= chunk.size - start) { | ||
| 51 | chunk.used = start + bytes; | ||
| 52 | buffer = chunk.buffer; | ||
| 53 | offset = start; | ||
| 54 | return true; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | Chunk chunk; | ||
| 58 | for (std::size_t i = 0; i < spare_.size(); ++i) | ||
| 59 | if (spare_[i].size >= bytes) { | ||
| 60 | chunk = spare_[i]; | ||
| 61 | spare_.erase(spare_.begin() + std::ptrdiff_t(i)); | ||
| 62 | break; | ||
| 63 | } | ||
| 64 | if (!chunk.buffer && !make_chunk(std::max<std::uint64_t>(bytes, minimum), chunk)) return false; | ||
| 65 | chunk.used = bytes; | ||
| 66 | buffer = chunk.buffer; | ||
| 67 | offset = 0; | ||
| 68 | current_.chunks.push_back(chunk); | ||
| 69 | return true; | ||
| 70 | } | ||
| 71 | |||
| 72 | // The chunks handed out since the last call belong to a submission that `timeline` reaches `ticket` for | ||
| 73 | // (ticket 0: nothing was submitted, they are free once `collect` runs). | ||
| 74 | void retire(VkSemaphore timeline, std::uint64_t ticket) { | ||
| 75 | if (current_.chunks.empty()) return; | ||
| 76 | current_.timeline = timeline; | ||
| 77 | current_.ticket = ticket; | ||
| 78 | retired_.push_back(std::move(current_)); | ||
| 79 | current_ = {}; | ||
| 80 | } | ||
| 81 | |||
| 82 | // The submissions on `timeline` are over and the semaphore is about to go (its queue is idle). | ||
| 83 | void forget(VkSemaphore timeline) { | ||
| 84 | for (std::size_t i = 0; i < retired_.size();) { | ||
| 85 | if (retired_[i].timeline != timeline) { | ||
| 86 | ++i; | ||
| 87 | continue; | ||
| 88 | } | ||
| 89 | release(retired_[i]); | ||
| 90 | retired_.erase(retired_.begin() + std::ptrdiff_t(i)); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | // Keeps for reuse (or frees) the chunks of finished submissions. | ||
| 95 | void collect() { | ||
| 96 | for (std::size_t i = 0; i < retired_.size();) { | ||
| 97 | std::uint64_t done = 0; | ||
| 98 | if (retired_[i].timeline) vk_.vkGetSemaphoreCounterValue(device_, retired_[i].timeline, &done); | ||
| 99 | if (done < retired_[i].ticket) { | ||
| 100 | ++i; | ||
| 101 | continue; | ||
| 102 | } | ||
| 103 | for (auto& chunk : retired_[i].chunks) { | ||
| 104 | chunk.used = 0; | ||
| 105 | if (spare_.size() < 4) spare_.push_back(chunk); | ||
| 106 | else destroy(chunk); | ||
| 107 | } | ||
| 108 | retired_[i].chunks.clear(); | ||
| 109 | retired_.erase(retired_.begin() + std::ptrdiff_t(i)); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | private: | ||
| 114 | struct Chunk { | ||
| 115 | VkBuffer buffer = VK_NULL_HANDLE; | ||
| 116 | VkDeviceMemory memory = VK_NULL_HANDLE; | ||
| 117 | VkDeviceSize size = 0, used = 0; | ||
| 118 | }; | ||
| 119 | struct Batch { | ||
| 120 | std::vector<Chunk> chunks; | ||
| 121 | VkSemaphore timeline = VK_NULL_HANDLE; | ||
| 122 | std::uint64_t ticket = 0; | ||
| 123 | }; | ||
| 124 | |||
| 125 | struct Functions { | ||
| 126 | #define LEONIDA_SCRATCH_FUNCTIONS(X) \ | ||
| 127 | X(vkCreateBuffer) X(vkDestroyBuffer) X(vkGetBufferMemoryRequirements) X(vkBindBufferMemory) X(vkFreeMemory) \ | ||
| 128 | X(vkGetSemaphoreCounterValue) X(vkDeviceWaitIdle) | ||
| 129 | #define LEONIDA_SCRATCH_MEMBER(name) PFN_##name name = nullptr; | ||
| 130 | LEONIDA_SCRATCH_FUNCTIONS(LEONIDA_SCRATCH_MEMBER) | ||
| 131 | #undef LEONIDA_SCRATCH_MEMBER | ||
| 132 | |||
| 133 | bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { | ||
| 134 | bool all = true; | ||
| 135 | #define LEONIDA_SCRATCH_LOAD(name) \ | ||
| 136 | name = reinterpret_cast<PFN_##name>(get(device, #name)); \ | ||
| 137 | all = all && name != nullptr; | ||
| 138 | LEONIDA_SCRATCH_FUNCTIONS(LEONIDA_SCRATCH_LOAD) | ||
| 139 | #undef LEONIDA_SCRATCH_LOAD | ||
| 140 | #undef LEONIDA_SCRATCH_FUNCTIONS | ||
| 141 | return all; | ||
| 142 | } | ||
| 143 | }; | ||
| 144 | |||
| 145 | bool make_chunk(std::uint64_t size, Chunk& out) { | ||
| 146 | VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; | ||
| 147 | info.size = size; | ||
| 148 | info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; | ||
| 149 | if (vk_.vkCreateBuffer(device_, &info, nullptr, &out.buffer) != VK_SUCCESS) return false; | ||
| 150 | VkMemoryRequirements requirements{}; | ||
| 151 | vk_.vkGetBufferMemoryRequirements(device_, out.buffer, &requirements); | ||
| 152 | out.memory = allocate_ ? allocate_(requirements) : VK_NULL_HANDLE; | ||
| 153 | if (!out.memory || vk_.vkBindBufferMemory(device_, out.buffer, out.memory, 0) != VK_SUCCESS) { | ||
| 154 | destroy(out); | ||
| 155 | return false; | ||
| 156 | } | ||
| 157 | out.size = size; | ||
| 158 | return true; | ||
| 159 | } | ||
| 160 | void destroy(Chunk& chunk) { | ||
| 161 | if (chunk.buffer) vk_.vkDestroyBuffer(device_, chunk.buffer, nullptr); | ||
| 162 | if (chunk.memory) vk_.vkFreeMemory(device_, chunk.memory, nullptr); | ||
| 163 | chunk = {}; | ||
| 164 | } | ||
| 165 | void release(Batch& batch) { | ||
| 166 | for (auto& chunk : batch.chunks) destroy(chunk); | ||
| 167 | batch.chunks.clear(); | ||
| 168 | } | ||
| 169 | |||
| 170 | VkDevice device_; | ||
| 171 | Allocate allocate_; | ||
| 172 | Functions vk_; | ||
| 173 | bool loaded_ = false; | ||
| 174 | Batch current_; // handed out, not yet submitted | ||
| 175 | std::vector<Batch> retired_; // submitted, waiting for the GPU | ||
| 176 | std::vector<Chunk> spare_; // finished chunks kept for reuse | ||
| 177 | }; | ||
| 178 | |||
| 179 | } // namespace leonida | ||