upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 13:12:26 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 13:12:26 -0500
commit60cc47cb79f5362f39e9179e0797c3eb5d8aa643 (patch)
tree36bd1d1c5e942163746fe42338546ff4c962ca9d /runtime/src
parentec8ec4c9a46c6623f1a9c125afd3566da97f1e0c (diff)
parent1ace3ac642605c16136547377de650fb22699f8d (diff)
Merge pull request #306 from IAmNotSiob
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/queue_families.h48
-rw-r--r--runtime/src/vulkan_backend.h20
-rw-r--r--runtime/src/vulkan_replay.h47
3 files changed, 109 insertions, 6 deletions
diff --git a/runtime/src/queue_families.h b/runtime/src/queue_families.h
new file mode 100644
index 000000000..32ef1eb1d
--- /dev/null
+++ b/runtime/src/queue_families.h
@@ -0,0 +1,48 @@
1#pragma once
2
3// Picking Vulkan queue families for D3D12's direct, compute and copy queues. Header-only and free of the rest of
4// the backend, so test harnesses can use it too.
5
6#include <vulkan/vulkan.h>
7
8#include <cstdint>
9#include <span>
10
11namespace leonida {
12
13// Queue families for D3D12's three queue types. Direct needs graphics. Compute prefers a family with compute and no
14// graphics (async compute), copy one with transfer and neither (a DMA engine); each falls back to a more capable
15// family, ending at the graphics one. A dedicated copy family is only used when its image transfer granularity is
16// 1x1x1, because D3D12 copies any box.
17struct QueueFamilies {
18 std::uint32_t direct = 0;
19 std::uint32_t compute = 0;
20 std::uint32_t copy = 0;
21};
22
23inline QueueFamilies pick_queue_families(std::span<const VkQueueFamilyProperties> families, std::uint32_t graphics) {
24 QueueFamilies out{graphics, graphics, graphics};
25 constexpr VkQueueFlags g = VK_QUEUE_GRAPHICS_BIT, c = VK_QUEUE_COMPUTE_BIT, t = VK_QUEUE_TRANSFER_BIT;
26 const auto fine_grained = [](const VkQueueFamilyProperties& f) {
27 return f.minImageTransferGranularity.width == 1 && f.minImageTransferGranularity.height == 1 &&
28 f.minImageTransferGranularity.depth == 1;
29 };
30 bool have_compute = false, have_copy = false;
31 for (std::uint32_t i = 0; i < families.size(); ++i) {
32 const auto& f = families[i];
33 if (!f.queueCount) continue;
34 if (!have_compute && (f.queueFlags & c) && !(f.queueFlags & g)) {
35 out.compute = i;
36 have_compute = true;
37 }
38 if (!have_copy && (f.queueFlags & t) && !(f.queueFlags & (g | c)) && fine_grained(f)) {
39 out.copy = i;
40 have_copy = true;
41 }
42 }
43 // No DMA family: copies go to the async-compute family, which can transfer too (compute implies transfer).
44 if (!have_copy && have_compute && fine_grained(families[out.compute])) out.copy = out.compute;
45 return out;
46}
47
48} // namespace leonida
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index 140d7234b..086b6772a 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -17,6 +17,7 @@
17#include "vulkan_depth24.h" 17#include "vulkan_depth24.h"
18#include "vulkan_scratch.h" 18#include "vulkan_scratch.h"
19#include "vulkan_replay.h" 19#include "vulkan_replay.h"
20#include "queue_families.h"
20#include "resource_tiling.h" 21#include "resource_tiling.h"
21#include "xdxr_build.h" 22#include "xdxr_build.h"
22 23
@@ -1115,6 +1116,8 @@ public:
1115 // `prepare` is called at the start of the command buffer of every ExecuteCommandLists, before the lists are 1116 // `prepare` is called at the start of the command buffer of every ExecuteCommandLists, before the lists are
1116 // written: the device brings the bindless set 0 of the translated shaders up to date there (vulkan_bindless.h) 1117 // written: the device brings the bindless set 0 of the translated shaders up to date there (vulkan_bindless.h)
1117 // and records what the set needs first. When it fails, nothing of that call is executed. 1118 // and records what the set needs first. When it fails, nothing of that call is executed.
1119 void set_transfer_only_family(std::uint32_t family) { transfer_only_family_ = family; }
1120
1118 void set_bindless_prepare(std::function<bool(VkCommandBuffer)> prepare) { 1121 void set_bindless_prepare(std::function<bool(VkCommandBuffer)> prepare) {
1119 std::unique_lock lock(resource_mutex_); 1122 std::unique_lock lock(resource_mutex_);
1120 bindless_prepare_ = std::move(prepare); 1123 bindless_prepare_ = std::move(prepare);
@@ -1294,6 +1297,7 @@ private:
1294 std::unique_ptr<PredicateReducer> predicate_reducer_; 1297 std::unique_ptr<PredicateReducer> predicate_reducer_;
1295 std::unique_ptr<PredicatedBufferCopier> predicated_copier_; 1298 std::unique_ptr<PredicatedBufferCopier> predicated_copier_;
1296 std::uint32_t predicate_family_ = UINT32_MAX; 1299 std::uint32_t predicate_family_ = UINT32_MAX;
1300 std::uint32_t transfer_only_family_ = UINT32_MAX;
1297 DebugLabelFunctions labels_; 1301 DebugLabelFunctions labels_;
1298}; 1302};
1299#ifdef _MSC_VER 1303#ifdef _MSC_VER
@@ -1474,6 +1478,12 @@ public:
1474 // the layout tracker as it was. Pending Wait()s are attached to this submission. 1478 // the layout tracker as it was. Pending Wait()s are attached to this submission.
1475 ReplayResult execute(std::span<const CommandList* const> lists) { 1479 ReplayResult execute(std::span<const CommandList* const> lists) {
1476 if (context_.is_device_lost()) return ReplayResult::unsupported; 1480 if (context_.is_device_lost()) return ReplayResult::unsupported;
1481 if (kind_ != QueueKind::direct)
1482 for (const auto* list : lists)
1483 for (const auto& command : list->commands())
1484 if (std::holds_alternative<ClearRenderTarget>(command) ||
1485 std::holds_alternative<ClearDepthStencil>(command))
1486 return ReplayResult::unsupported;
1477 VkCommandBuffer command_buffer = VK_NULL_HANDLE; 1487 VkCommandBuffer command_buffer = VK_NULL_HANDLE;
1478 std::function<void(bool)> bindless_commit; 1488 std::function<void(bool)> bindless_commit;
1479 // Lock hierarchy: resource_mutex_ -> tracker_mutex_ -> submit_mutex_. 1489 // Lock hierarchy: resource_mutex_ -> tracker_mutex_ -> submit_mutex_.
@@ -1500,12 +1510,16 @@ public:
1500 if (active && scratch) scratch->retire(progress, 0); 1510 if (active && scratch) scratch->retire(progress, 0);
1501 } 1511 }
1502 } scratch_guard{context_.scratch_.get(), progress_, false}; 1512 } scratch_guard{context_.scratch_.get(), progress_, false};
1503 bindless_commit = context_.bindless_commit_; 1513 const bool transfer_only = family_ == context_.transfer_only_family_;
1514 if (!transfer_only) bindless_commit = context_.bindless_commit_;
1504 // Kept between executions: prepare refills each list's entries in the storage the last execution left, where 1515 // Kept between executions: prepare refills each list's entries in the storage the last execution left, where
1505 // making them fresh for every command was about half the layer's CPU cost per command (replay_cpu_bench). 1516 // making them fresh for every command was about half the layer's CPU cost per command (replay_cpu_bench).
1506 auto& prepared = prepared_; 1517 auto& prepared = prepared_;
1507 prepared.resize(lists.size()); 1518 prepared.resize(lists.size());
1508 ReplayFunctions replay = context_.replay_; 1519 ReplayFunctions replay = context_.replay_;
1520 replay.queue_stages = kind_ == QueueKind::copy ? detail::queue_stages_copy
1521 : kind_ == QueueKind::compute ? detail::queue_stages_compute
1522 : 0;
1509 std::uint64_t captures = 0; 1523 std::uint64_t captures = 0;
1510 for (const auto* list : lists) 1524 for (const auto* list : lists)
1511 for (const auto& command : list->commands()) 1525 for (const auto& command : list->commands())
@@ -1549,7 +1563,7 @@ public:
1549 return context_.scratch_->get(bytes, buffer, offset); 1563 return context_.scratch_->get(bytes, buffer, offset);
1550 }; 1564 };
1551 } 1565 }
1552 if (context_.depth24_) { 1566 if (context_.depth24_ && !transfer_only) {
1553 replay.depth24_prepare = [this, &tracker_lock, &depth24_guard](Depth24Job& job, 1567 replay.depth24_prepare = [this, &tracker_lock, &depth24_guard](Depth24Job& job,
1554 std::vector<Depth24Job>& parts) { 1568 std::vector<Depth24Job>& parts) {
1555 if (!tracker_lock.owns_lock()) tracker_lock.lock(); 1569 if (!tracker_lock.owns_lock()) tracker_lock.lock();
@@ -1644,7 +1658,7 @@ public:
1644 return ReplayResult::unsupported; 1658 return ReplayResult::unsupported;
1645 } 1659 }
1646 if (!tracker_lock.owns_lock()) tracker_lock.lock(); 1660 if (!tracker_lock.owns_lock()) tracker_lock.lock();
1647 if (context_.bindless_prepare_ && !context_.bindless_prepare_(command_buffer)) { 1661 if (!transfer_only && context_.bindless_prepare_ && !context_.bindless_prepare_(command_buffer)) {
1648 const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS; 1662 const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS;
1649 resource_lock.unlock(); 1663 resource_lock.unlock();
1650 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); 1664 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0);
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h
index fd69ab91b..0756191f4 100644
--- a/runtime/src/vulkan_replay.h
+++ b/runtime/src/vulkan_replay.h
@@ -773,6 +773,7 @@ struct ReplayFunctions {
773 PFN_vkCmdClearDepthStencilImage clear_depth_stencil_image = nullptr; 773 PFN_vkCmdClearDepthStencilImage clear_depth_stencil_image = nullptr;
774 // Optional; a list that needs a missing one is rejected with ReplayResult::unsupported. 774 // Optional; a list that needs a missing one is rejected with ReplayResult::unsupported.
775 PFN_vkCmdPipelineBarrier2 pipeline_barrier2 = nullptr; // enhanced barriers 775 PFN_vkCmdPipelineBarrier2 pipeline_barrier2 = nullptr; // enhanced barriers
776 VkPipelineStageFlags2 queue_stages = 0;
776 PFN_vkCmdBeginRendering begin_rendering = nullptr; // rectangle clears 777 PFN_vkCmdBeginRendering begin_rendering = nullptr; // rectangle clears
777 PFN_vkCmdEndRendering end_rendering = nullptr; 778 PFN_vkCmdEndRendering end_rendering = nullptr;
778 PFN_vkCmdClearAttachments clear_attachments = nullptr; 779 PFN_vkCmdClearAttachments clear_attachments = nullptr;
@@ -1101,6 +1102,36 @@ inline void clip(std::span<const Rect> rects, VkExtent2D extent, std::uint32_t l
1101 } 1102 }
1102} 1103}
1103 1104
1105inline constexpr VkPipelineStageFlags2 queue_stages_copy =
1106 VK_PIPELINE_STAGE_2_TOP_OF_PIPE_BIT | VK_PIPELINE_STAGE_2_BOTTOM_OF_PIPE_BIT | VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT |
1107 VK_PIPELINE_STAGE_2_HOST_BIT | VK_PIPELINE_STAGE_2_TRANSFER_BIT | VK_PIPELINE_STAGE_2_COPY_BIT;
1108inline constexpr VkPipelineStageFlags2 queue_stages_compute =
1109 queue_stages_copy | VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT | VK_PIPELINE_STAGE_2_DRAW_INDIRECT_BIT |
1110 VK_PIPELINE_STAGE_2_CLEAR_BIT;
1111
1112inline void restrict_to_queue(const ReplayFunctions& vk, VkPipelineStageFlags2& stages, VkAccessFlags2& access) {
1113 if (!vk.queue_stages) return;
1114 VkAccessFlags2 allowed = VK_ACCESS_2_TRANSFER_READ_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT |
1115 VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT | VK_ACCESS_2_HOST_READ_BIT |
1116 VK_ACCESS_2_HOST_WRITE_BIT;
1117 if (vk.queue_stages & VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT)
1118 allowed |= VK_ACCESS_2_SHADER_READ_BIT | VK_ACCESS_2_SHADER_WRITE_BIT | VK_ACCESS_2_UNIFORM_READ_BIT |
1119 VK_ACCESS_2_SHADER_SAMPLED_READ_BIT | VK_ACCESS_2_SHADER_STORAGE_READ_BIT |
1120 VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT | VK_ACCESS_2_INDIRECT_COMMAND_READ_BIT;
1121 stages &= vk.queue_stages;
1122 if (!stages) stages = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
1123 access &= allowed;
1124}
1125
1126inline void restrict_to_queue(const ReplayFunctions& vk, VkPipelineStageFlags& stages, VkAccessFlags& access) {
1127 if (!vk.queue_stages) return;
1128 VkPipelineStageFlags2 s2 = stages;
1129 VkAccessFlags2 a2 = access;
1130 restrict_to_queue(vk, s2, a2);
1131 stages = static_cast<VkPipelineStageFlags>(s2 & 0xFFFFFFFFull);
1132 access = static_cast<VkAccessFlags>(a2 & 0xFFFFFFFFull);
1133}
1134
1104// A D3D12 barrier on a volume mip covers every W slice, including on devices with maintenance9. 1135// A D3D12 barrier on a volume mip covers every W slice, including on devices with maintenance9.
1105inline VkImageSubresourceRange layout_range(const ResourceBinding& resource, VkImageSubresourceRange range) { 1136inline VkImageSubresourceRange layout_range(const ResourceBinding& resource, VkImageSubresourceRange range) {
1106 if (resource.volume) range.layerCount = VK_REMAINING_ARRAY_LAYERS; 1137 if (resource.volume) range.layerCount = VK_REMAINING_ARRAY_LAYERS;
@@ -1111,6 +1142,8 @@ inline void image_barrier(const ReplayFunctions& vk, VkCommandBuffer command_buf
1111 const VkImageSubresourceRange& range, VkImageLayout old_layout, VkImageLayout new_layout, 1142 const VkImageSubresourceRange& range, VkImageLayout old_layout, VkImageLayout new_layout,
1112 VkAccessFlags src_access, VkPipelineStageFlags src_stages, VkAccessFlags dst_access, 1143 VkAccessFlags src_access, VkPipelineStageFlags src_stages, VkAccessFlags dst_access,
1113 VkPipelineStageFlags dst_stages) { 1144 VkPipelineStageFlags dst_stages) {
1145 restrict_to_queue(vk, src_stages, src_access);
1146 restrict_to_queue(vk, dst_stages, dst_access);
1114 VkImageMemoryBarrier barrier{}; 1147 VkImageMemoryBarrier barrier{};
1115 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; 1148 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1116 barrier.srcAccessMask = src_access; 1149 barrier.srcAccessMask = src_access;
@@ -1126,6 +1159,8 @@ inline void image_barrier(const ReplayFunctions& vk, VkCommandBuffer command_buf
1126 1159
1127inline void memory_barrier(const ReplayFunctions& vk, VkCommandBuffer command_buffer, VkAccessFlags src_access, 1160inline void memory_barrier(const ReplayFunctions& vk, VkCommandBuffer command_buffer, VkAccessFlags src_access,
1128 VkPipelineStageFlags src_stages, VkAccessFlags dst_access, VkPipelineStageFlags dst_stages) { 1161 VkPipelineStageFlags src_stages, VkAccessFlags dst_access, VkPipelineStageFlags dst_stages) {
1162 restrict_to_queue(vk, src_stages, src_access);
1163 restrict_to_queue(vk, dst_stages, dst_access);
1129 VkMemoryBarrier barrier{}; 1164 VkMemoryBarrier barrier{};
1130 barrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER; 1165 barrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
1131 barrier.srcAccessMask = src_access; 1166 barrier.srcAccessMask = src_access;
@@ -1152,7 +1187,7 @@ inline bool zero_compressed_image(const ReplayFunctions& vk, VkCommandBuffer com
1152 const ResourceBinding& resource, const VkImageSubresourceRange& range, 1187 const ResourceBinding& resource, const VkImageSubresourceRange& range,
1153 VkImageLayout dst_layout) { 1188 VkImageLayout dst_layout) {
1154 const BlockFormat fmt = block_format(resource.format, VK_IMAGE_ASPECT_COLOR_BIT); 1189 const BlockFormat fmt = block_format(resource.format, VK_IMAGE_ASPECT_COLOR_BIT);
1155 if (fmt.block <= 1 || !fmt.bytes || !vk.copy_buffer_to_image) return false; 1190 if (fmt.block < 1 || !fmt.bytes || !vk.copy_buffer_to_image) return false;
1156 const std::uint32_t mip_begin = range.baseMipLevel; 1191 const std::uint32_t mip_begin = range.baseMipLevel;
1157 const std::uint32_t mip_end = 1192 const std::uint32_t mip_end =
1158 range.levelCount == VK_REMAINING_MIP_LEVELS ? resource.mip_levels 1193 range.levelCount == VK_REMAINING_MIP_LEVELS ? resource.mip_levels
@@ -1296,8 +1331,10 @@ inline void initialize(const ReplayFunctions& vk, VkCommandBuffer command_buffer
1296 for (auto& piece : pieces) { 1331 for (auto& piece : pieces) {
1297 if (piece.layout != VK_IMAGE_LAYOUT_UNDEFINED) continue; 1332 if (piece.layout != VK_IMAGE_LAYOUT_UNDEFINED) continue;
1298 const BlockFormat fmt = block_format(resource.format, VK_IMAGE_ASPECT_COLOR_BIT); 1333 const BlockFormat fmt = block_format(resource.format, VK_IMAGE_ASPECT_COLOR_BIT);
1299 const bool compressed = fmt.block > 1; 1334 const bool color = piece.range.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT;
1300 if (resource.not_zeroed) { 1335 const bool can_clear = !vk.queue_stages || ((vk.queue_stages & VK_PIPELINE_STAGE_2_CLEAR_BIT) && color);
1336 const bool compressed = fmt.block > 1 || (!can_clear && color);
1337 if (resource.not_zeroed || (!can_clear && !color)) {
1301 image_barrier(vk, command_buffer, resource, piece.range, VK_IMAGE_LAYOUT_UNDEFINED, layout, 0, 1338 image_barrier(vk, command_buffer, resource, piece.range, VK_IMAGE_LAYOUT_UNDEFINED, layout, 0,
1302 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, any_memory, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); 1339 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, any_memory, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
1303 } else if (compressed) { 1340 } else if (compressed) {
@@ -2925,6 +2962,8 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
2925 barrier.dstStageMask = dst.stages; 2962 barrier.dstStageMask = dst.stages;
2926 barrier.srcAccessMask = src.access; 2963 barrier.srcAccessMask = src.access;
2927 barrier.dstAccessMask = dst.access; 2964 barrier.dstAccessMask = dst.access;
2965 if (barrier.srcStageMask) detail::restrict_to_queue(vk, barrier.srcStageMask, barrier.srcAccessMask);
2966 if (barrier.dstStageMask) detail::restrict_to_queue(vk, barrier.dstStageMask, barrier.dstAccessMask);
2928 VkDependencyInfo dependency{VK_STRUCTURE_TYPE_DEPENDENCY_INFO}; 2967 VkDependencyInfo dependency{VK_STRUCTURE_TYPE_DEPENDENCY_INFO};
2929 dependency.memoryBarrierCount = 1; 2968 dependency.memoryBarrierCount = 1;
2930 dependency.pMemoryBarriers = &barrier; 2969 dependency.pMemoryBarriers = &barrier;
@@ -2959,6 +2998,8 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
2959 b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; 2998 b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
2960 b.image = p.binding.image; 2999 b.image = p.binding.image;
2961 b.subresourceRange = detail::layout_range(p.binding, piece.range); 3000 b.subresourceRange = detail::layout_range(p.binding, piece.range);
3001 if (b.srcStageMask) detail::restrict_to_queue(vk, b.srcStageMask, b.srcAccessMask);
3002 if (b.dstStageMask) detail::restrict_to_queue(vk, b.dstStageMask, b.dstAccessMask);
2962 barriers.push_back(b); 3003 barriers.push_back(b);
2963 } 3004 }
2964 VkDependencyInfo dependency{VK_STRUCTURE_TYPE_DEPENDENCY_INFO}; 3005 VkDependencyInfo dependency{VK_STRUCTURE_TYPE_DEPENDENCY_INFO};