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:
authorRyx <118130241+Ryx69@users.noreply.github.com>2026-11-21 18:58:48 +0100
committerRyx <118130241+Ryx69@users.noreply.github.com>2026-11-21 18:58:48 +0100
commit7f6e8f059dbd8c3202e185c972832841d6a8a9d2 (patch)
tree0b64927bd2b6d206611e2f66561a3f3ab8e7a119 /runtime/src
parent793b06f12765571ed5e126d1d8944cec6a4dc805 (diff)
Runtime: D3D12 barriers and depth-stencil clears in the command-list IR
The IR (command_list.h, was clear_commands.h) now records transition, UAV and aliasing barriers (split barriers record their end half) and full-view ClearDepthStencilView next to ClearRenderTargetView. vulkan_replay.h (was vulkan_clear.h) maps D3D12 resource states to Vulkan layout/access/stage (section 4.3 table), decodes subresource indices including depth/stencil planes, validates the whole list before emitting anything, and wraps clears in a layout round trip so images stay in their D3D12 state. The caller no longer manages layouts by hand. Tests: command_list_test (recording rules), d3d12_state_map_test (state table, subresource decoding, constants checked against d3d12.h; needs Vulkan headers only) and vk_command_replay_test (GPU readback of colour, depth and stencil; LEONIDA_VK_VALIDATION=1 runs it under the Khronos validation layer).
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/clear_commands.h56
-rw-r--r--runtime/src/command_list.h150
-rw-r--r--runtime/src/vulkan_clear.h48
-rw-r--r--runtime/src/vulkan_replay.h283
4 files changed, 433 insertions, 104 deletions
diff --git a/runtime/src/clear_commands.h b/runtime/src/clear_commands.h
deleted file mode 100644
index c517d0211..000000000
--- a/runtime/src/clear_commands.h
+++ /dev/null
@@ -1,56 +0,0 @@
1#pragma once
2
3#include <array>
4#include <cstdint>
5#include <span>
6#include <vector>
7
8namespace leonida {
9
10using ClearResourceId = std::uintptr_t;
11using ClearColor = std::array<float, 4>;
12
13struct ClearTargetView {
14 ClearResourceId resource;
15 std::uint32_t mip;
16 std::uint32_t first_layer;
17 std::uint32_t layer_count;
18};
19
20struct ClearRenderTarget {
21 ClearTargetView target;
22 ClearColor color;
23};
24
25// The full-view ClearRenderTargetView subset of the command-list IR.
26class ClearCommandList {
27public:
28 // The caller reads the CPU descriptor during recording. Store its view by value.
29 bool clear_render_target(const ClearTargetView& target, const ClearColor& color) {
30 if (closed_ || !target.resource || !target.layer_count) return false;
31 commands_.push_back({target, color});
32 return true;
33 }
34
35 bool close() {
36 if (closed_) return false;
37 closed_ = true;
38 return true;
39 }
40
41 bool reset() {
42 if (!closed_) return false;
43 commands_.clear();
44 closed_ = false;
45 return true;
46 }
47
48 bool closed() const { return closed_; }
49 std::span<const ClearRenderTarget> commands() const { return commands_; }
50
51private:
52 std::vector<ClearRenderTarget> commands_;
53 bool closed_ = false;
54};
55
56} // namespace leonida
diff --git a/runtime/src/command_list.h b/runtime/src/command_list.h
new file mode 100644
index 000000000..f27ce4265
--- /dev/null
+++ b/runtime/src/command_list.h
@@ -0,0 +1,150 @@
1#pragma once
2
3// The command-list IR (docs/d3d12_vulkan.md section 3.2): what a D3D12 command list records, kept until
4// ExecuteCommandLists replays it on Vulkan (vulkan_replay.h). This slice covers resource barriers and full-view
5// render-target and depth-stencil clears. It has no Windows or Vulkan dependency.
6
7#include <array>
8#include <cstdint>
9#include <span>
10#include <variant>
11#include <vector>
12
13namespace leonida {
14
15using ResourceId = std::uintptr_t;
16using ClearColor = std::array<float, 4>;
17
18// D3D12_RESOURCE_STATES values from the public d3d12.h, so the IR builds without the Windows SDK.
19// d3d12_state_map_test checks them against d3d12.h on Windows.
20namespace d3d12_state {
21inline constexpr std::uint32_t common = 0;
22inline constexpr std::uint32_t vertex_and_constant_buffer = 0x1;
23inline constexpr std::uint32_t index_buffer = 0x2;
24inline constexpr std::uint32_t render_target = 0x4;
25inline constexpr std::uint32_t unordered_access = 0x8;
26inline constexpr std::uint32_t depth_write = 0x10;
27inline constexpr std::uint32_t depth_read = 0x20;
28inline constexpr std::uint32_t non_pixel_shader_resource = 0x40;
29inline constexpr std::uint32_t pixel_shader_resource = 0x80;
30inline constexpr std::uint32_t stream_out = 0x100;
31inline constexpr std::uint32_t indirect_argument = 0x200;
32inline constexpr std::uint32_t copy_dest = 0x400;
33inline constexpr std::uint32_t copy_source = 0x800;
34inline constexpr std::uint32_t resolve_dest = 0x1000;
35inline constexpr std::uint32_t resolve_source = 0x2000;
36inline constexpr std::uint32_t raytracing_acceleration_structure = 0x400000;
37inline constexpr std::uint32_t shading_rate_source = 0x1000000;
38inline constexpr std::uint32_t generic_read = 0xac3;
39inline constexpr std::uint32_t present = 0;
40} // namespace d3d12_state
41
42// D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, D3D12_RESOURCE_BARRIER_FLAGS and D3D12_CLEAR_FLAGS.
43inline constexpr std::uint32_t all_subresources = 0xffffffff;
44inline constexpr std::uint32_t barrier_begin_only = 0x1;
45inline constexpr std::uint32_t barrier_end_only = 0x2;
46inline constexpr std::uint32_t clear_depth = 0x1;
47inline constexpr std::uint32_t clear_stencil = 0x2;
48
49// A view captured by value while recording: the CPU descriptor slot may be rewritten after the call.
50struct TargetView {
51 ResourceId resource;
52 std::uint32_t mip;
53 std::uint32_t first_layer;
54 std::uint32_t layer_count;
55};
56
57// D3D12 subresource index: mip + layer * mips + plane * mips * layers, or all_subresources.
58struct TransitionBarrier {
59 ResourceId resource;
60 std::uint32_t subresource;
61 std::uint32_t before;
62 std::uint32_t after;
63};
64
65// resource 0 means every UAV access, as a null pResource does in D3D12.
66struct UavBarrier {
67 ResourceId resource;
68};
69
70// Either resource may be 0 ("any placed or reserved resource").
71struct AliasingBarrier {
72 ResourceId before;
73 ResourceId after;
74};
75
76struct ClearRenderTarget {
77 TargetView target;
78 ClearColor color;
79};
80
81struct ClearDepthStencil {
82 TargetView target;
83 std::uint32_t flags; // clear_depth | clear_stencil
84 float depth;
85 std::uint8_t stencil;
86};
87
88using Command = std::variant<TransitionBarrier, UavBarrier, AliasingBarrier, ClearRenderTarget, ClearDepthStencil>;
89
90class CommandList {
91public:
92 // D3D12 split barriers: the begin half only marks the resource unusable until the end half, so the transition
93 // is recorded once, at the end. A full barrier (flags 0) is recorded where it is issued.
94 bool transition(const TransitionBarrier& barrier, std::uint32_t flags = 0) {
95 if (closed_ || !barrier.resource) return false;
96 if (flags & ~(barrier_begin_only | barrier_end_only)) return false;
97 if (flags == (barrier_begin_only | barrier_end_only)) return false;
98 if (flags & barrier_begin_only) return true;
99 commands_.push_back(barrier);
100 return true;
101 }
102
103 bool uav_barrier(const UavBarrier& barrier) { return append(barrier); }
104 bool aliasing_barrier(const AliasingBarrier& barrier) { return append(barrier); }
105
106 // ClearRenderTargetView with NumRects == 0; the resource must be in render_target state at execution.
107 bool clear_render_target(const TargetView& target, const ClearColor& color) {
108 if (!valid(target)) return false;
109 return append(ClearRenderTarget{target, color});
110 }
111
112 // ClearDepthStencilView with NumRects == 0; the resource must be in depth_write state at execution.
113 // D3D12 requires depth in [0, 1] and at least one clear flag.
114 bool clear_depth_stencil(const TargetView& target, std::uint32_t flags, float depth, std::uint8_t stencil) {
115 if (!valid(target)) return false;
116 if (!flags || (flags & ~(clear_depth | clear_stencil))) return false;
117 if ((flags & clear_depth) && !(depth >= 0.0f && depth <= 1.0f)) return false;
118 return append(ClearDepthStencil{target, flags, depth, stencil});
119 }
120
121 bool close() {
122 if (closed_) return false;
123 closed_ = true;
124 return true;
125 }
126
127 bool reset() {
128 if (!closed_) return false;
129 commands_.clear();
130 closed_ = false;
131 return true;
132 }
133
134 bool closed() const { return closed_; }
135 std::span<const Command> commands() const { return commands_; }
136
137private:
138 static bool valid(const TargetView& target) { return target.resource && target.layer_count; }
139
140 bool append(const Command& command) {
141 if (closed_) return false;
142 commands_.push_back(command);
143 return true;
144 }
145
146 std::vector<Command> commands_;
147 bool closed_ = false;
148};
149
150} // namespace leonida
diff --git a/runtime/src/vulkan_clear.h b/runtime/src/vulkan_clear.h
deleted file mode 100644
index 0efb841e3..000000000
--- a/runtime/src/vulkan_clear.h
+++ /dev/null
@@ -1,48 +0,0 @@
1#pragma once
2
3#include "clear_commands.h"
4
5#include <vulkan/vulkan.h>
6
7#include <algorithm>
8
9namespace leonida {
10
11enum class ClearReplayResult { success, list_open, unresolved_target };
12
13// The caller transitions the target subresources to TRANSFER_DST_OPTIMAL and keeps them alive
14// until execution completes. The resolver maps stable resource IDs to transfer-destination
15// images whose formats match the float or normalized views captured at recording time.
16template<typename Resolve>
17ClearReplayResult replay_clears(const ClearCommandList& list, VkCommandBuffer command_buffer,
18 Resolve resolve, PFN_vkCmdPipelineBarrier barrier,
19 PFN_vkCmdClearColorImage clear_color_image) {
20 if (!list.closed()) return ClearReplayResult::list_open;
21 const auto commands = list.commands();
22 std::vector<VkImage> images;
23 images.reserve(commands.size());
24 for (const auto& command : commands) {
25 const VkImage image = resolve(command.target.resource);
26 if (image == VK_NULL_HANDLE) return ClearReplayResult::unresolved_target;
27 images.push_back(image);
28 }
29
30 VkMemoryBarrier clear_order{};
31 clear_order.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
32 clear_order.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
33 clear_order.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
34 for (std::size_t i = 0; i < commands.size(); ++i) {
35 // Preserve write order, including when two descriptors alias the same subresource.
36 barrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
37 0, 1, &clear_order, 0, nullptr, 0, nullptr);
38 VkClearColorValue color{};
39 std::copy(commands[i].color.begin(), commands[i].color.end(), color.float32);
40 const auto& target = commands[i].target;
41 const VkImageSubresourceRange range{VK_IMAGE_ASPECT_COLOR_BIT, target.mip, 1,
42 target.first_layer, target.layer_count};
43 clear_color_image(command_buffer, images[i], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &color, 1, &range);
44 }
45 return ClearReplayResult::success;
46}
47
48} // namespace leonida
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h
new file mode 100644
index 000000000..f983bdf80
--- /dev/null
+++ b/runtime/src/vulkan_replay.h
@@ -0,0 +1,283 @@
1#pragma once
2
3// Replays a closed CommandList (command_list.h) into a Vulkan command buffer.
4//
5// Resource states follow D3D12: a transition's `before` is trusted as the state the subresource is in when the
6// list executes, as the D3D12 runtime does, and becomes the Vulkan oldLayout. Clears assume the states D3D12
7// requires for them (render_target, depth_write) and wrap the transfer clear in a layout round trip, so the
8// image is back in its D3D12 state afterwards.
9//
10// Requirements on the device and images:
11// - a queue with graphics support (the stage masks use ALL_GRAPHICS);
12// - images that can be cleared are created with TRANSFER_DST usage;
13// - transitions of a single plane of a combined depth-stencil image need separateDepthStencilLayouts (Vulkan 1.2).
14// Clears move both planes, so a depth-only clear while the stencil plane is in another state is not supported.
15// Implicit state promotion and decay (D3D12 "common" state rules) are not modelled.
16
17#include "command_list.h"
18
19#include <vulkan/vulkan.h>
20
21#include <algorithm>
22#include <cstdint>
23#include <vector>
24
25namespace leonida {
26
27// What the resolver knows about a resource. image == VK_NULL_HANDLE means a buffer in the shared GPU address
28// space: its transitions become global memory barriers.
29struct ResourceBinding {
30 VkImage image = VK_NULL_HANDLE;
31 VkImageAspectFlags aspects = VK_IMAGE_ASPECT_COLOR_BIT;
32 std::uint32_t mip_levels = 1;
33 std::uint32_t array_layers = 1;
34 bool presentable = false; // a swapchain image: common/present maps to PRESENT_SRC_KHR
35};
36
37struct VulkanAccess {
38 VkImageLayout layout;
39 VkAccessFlags access;
40 VkPipelineStageFlags stages;
41};
42
43inline constexpr VkPipelineStageFlags shader_stages =
44 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
45
46// Maps a D3D12_RESOURCE_STATES combination to the Vulkan layout, accesses and stages that cover it.
47// Read-only combinations share one layout when every bit agrees on it (depth_read with shader reads stays in
48// DEPTH_STENCIL_READ_ONLY_OPTIMAL, which can be sampled); otherwise they fall back to GENERAL.
49// Buffer-only and extension states map to GENERAL with conservative memory access.
50inline VulkanAccess vulkan_access(std::uint32_t states, bool presentable) {
51 namespace s = d3d12_state;
52 constexpr VkAccessFlags any_memory = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
53 if (states == s::common) {
54 if (presentable) return {VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, 0, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT};
55 return {VK_IMAGE_LAYOUT_GENERAL, any_memory, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT};
56 }
57
58 struct Bit {
59 std::uint32_t state;
60 VkImageLayout layout;
61 VkAccessFlags access;
62 VkPipelineStageFlags stages;
63 };
64 constexpr VkPipelineStageFlags fragment_tests =
65 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
66 static constexpr Bit bits[] = {
67 {s::vertex_and_constant_buffer, VK_IMAGE_LAYOUT_GENERAL,
68 VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT | VK_ACCESS_UNIFORM_READ_BIT,
69 VK_PIPELINE_STAGE_VERTEX_INPUT_BIT | shader_stages},
70 {s::index_buffer, VK_IMAGE_LAYOUT_GENERAL, VK_ACCESS_INDEX_READ_BIT, VK_PIPELINE_STAGE_VERTEX_INPUT_BIT},
71 {s::render_target, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
72 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
73 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT},
74 {s::unordered_access, VK_IMAGE_LAYOUT_GENERAL, VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT,
75 shader_stages},
76 {s::depth_write, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
77 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, fragment_tests},
78 {s::depth_read, VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT,
79 fragment_tests},
80 {s::non_pixel_shader_resource, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
81 shader_stages},
82 {s::pixel_shader_resource, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_ACCESS_SHADER_READ_BIT,
83 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT},
84 {s::stream_out, VK_IMAGE_LAYOUT_GENERAL, any_memory, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT},
85 {s::indirect_argument, VK_IMAGE_LAYOUT_GENERAL, VK_ACCESS_INDIRECT_COMMAND_READ_BIT,
86 VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT},
87 {s::copy_dest, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
88 VK_PIPELINE_STAGE_TRANSFER_BIT},
89 {s::copy_source, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_ACCESS_TRANSFER_READ_BIT,
90 VK_PIPELINE_STAGE_TRANSFER_BIT},
91 {s::resolve_dest, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
92 VK_PIPELINE_STAGE_TRANSFER_BIT},
93 {s::resolve_source, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_ACCESS_TRANSFER_READ_BIT,
94 VK_PIPELINE_STAGE_TRANSFER_BIT},
95 {s::raytracing_acceleration_structure, VK_IMAGE_LAYOUT_GENERAL, any_memory,
96 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT},
97 {s::shading_rate_source, VK_IMAGE_LAYOUT_GENERAL, VK_ACCESS_MEMORY_READ_BIT,
98 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT},
99 };
100
101 const auto merge = [](VkImageLayout a, VkImageLayout b) {
102 if (a == VK_IMAGE_LAYOUT_UNDEFINED || a == b) return b;
103 const auto pair = [&](VkImageLayout x, VkImageLayout y) { return (a == x && b == y) || (a == y && b == x); };
104 if (pair(VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL))
105 return VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
106 return VK_IMAGE_LAYOUT_GENERAL;
107 };
108
109 VulkanAccess out{VK_IMAGE_LAYOUT_UNDEFINED, 0, 0};
110 std::uint32_t known = 0;
111 for (const auto& bit : bits) {
112 if (!(states & bit.state)) continue;
113 known |= bit.state;
114 out.layout = merge(out.layout, bit.layout);
115 out.access |= bit.access;
116 out.stages |= bit.stages;
117 }
118 if (states & ~known) {
119 out.layout = VK_IMAGE_LAYOUT_GENERAL;
120 out.access |= any_memory;
121 out.stages |= VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
122 }
123 return out;
124}
125
126// Decodes a D3D12 subresource index. Combined depth-stencil images have two planes (0 depth, 1 stencil);
127// every other image has one.
128inline bool image_range(const ResourceBinding& resource, std::uint32_t subresource, VkImageSubresourceRange& out) {
129 if (!resource.mip_levels || !resource.array_layers) return false;
130 if (subresource == all_subresources) {
131 out = {resource.aspects, 0, resource.mip_levels, 0, resource.array_layers};
132 return true;
133 }
134 constexpr VkImageAspectFlags depth_stencil = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
135 const bool two_planes = (resource.aspects & depth_stencil) == depth_stencil;
136 const std::uint64_t per_plane = std::uint64_t(resource.mip_levels) * resource.array_layers;
137 const std::uint64_t plane = subresource / per_plane;
138 if (plane >= (two_planes ? 2u : 1u)) return false;
139 const auto within = static_cast<std::uint32_t>(subresource % per_plane);
140 const VkImageAspectFlags aspect =
141 two_planes ? (plane ? VK_IMAGE_ASPECT_STENCIL_BIT : VK_IMAGE_ASPECT_DEPTH_BIT) : resource.aspects;
142 out = {aspect, within % resource.mip_levels, 1, within / resource.mip_levels, 1};
143 return true;
144}
145
146struct ReplayFunctions {
147 PFN_vkCmdPipelineBarrier pipeline_barrier;
148 PFN_vkCmdClearColorImage clear_color_image;
149 PFN_vkCmdClearDepthStencilImage clear_depth_stencil_image;
150};
151
152enum class ReplayResult { success, list_open, unresolved_resource, invalid_subresource, invalid_clear };
153
154namespace detail {
155
156struct Prepared {
157 ResourceBinding binding;
158 VkImageSubresourceRange range; // the transition range, or the cleared view
159};
160
161inline bool view_range(const ResourceBinding& resource, const TargetView& view, VkImageAspectFlags aspects,
162 VkImageSubresourceRange& out) {
163 if (view.mip >= resource.mip_levels) return false;
164 if (view.first_layer >= resource.array_layers || view.layer_count > resource.array_layers - view.first_layer)
165 return false;
166 out = {aspects, view.mip, 1, view.first_layer, view.layer_count};
167 return true;
168}
169
170inline void image_barrier(const ReplayFunctions& vk, VkCommandBuffer command_buffer, VkImage image,
171 const VkImageSubresourceRange& range, const VulkanAccess& from, const VulkanAccess& to) {
172 VkImageMemoryBarrier barrier{};
173 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
174 barrier.srcAccessMask = from.access;
175 barrier.dstAccessMask = to.access;
176 barrier.oldLayout = from.layout;
177 barrier.newLayout = to.layout;
178 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
179 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
180 barrier.image = image;
181 barrier.subresourceRange = range;
182 vk.pipeline_barrier(command_buffer, from.stages, to.stages, 0, 0, nullptr, 0, nullptr, 1, &barrier);
183}
184
185inline void memory_barrier(const ReplayFunctions& vk, VkCommandBuffer command_buffer, const VulkanAccess& from,
186 const VulkanAccess& to) {
187 VkMemoryBarrier barrier{};
188 barrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
189 barrier.srcAccessMask = from.access;
190 barrier.dstAccessMask = to.access;
191 vk.pipeline_barrier(command_buffer, from.stages, to.stages, 0, 1, &barrier, 0, nullptr, 0, nullptr);
192}
193
194} // namespace detail
195
196// Resolve maps a ResourceId to a const ResourceBinding* (nullptr: unknown). Every command is resolved and checked
197// before anything is written, so a failed replay leaves the command buffer untouched.
198template<typename Resolve>
199ReplayResult replay(const CommandList& list, VkCommandBuffer command_buffer, Resolve resolve,
200 const ReplayFunctions& vk) {
201 if (!list.closed()) return ReplayResult::list_open;
202 const auto commands = list.commands();
203 std::vector<detail::Prepared> prepared(commands.size());
204 constexpr VkImageAspectFlags depth_stencil = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
205
206 for (std::size_t i = 0; i < commands.size(); ++i) {
207 const auto& command = commands[i];
208 auto& out = prepared[i];
209 const auto bind = [&](ResourceId id) {
210 const ResourceBinding* binding = resolve(id);
211 if (binding) out.binding = *binding;
212 return binding != nullptr;
213 };
214 if (const auto* barrier = std::get_if<TransitionBarrier>(&command)) {
215 if (!bind(barrier->resource)) return ReplayResult::unresolved_resource;
216 if (out.binding.image == VK_NULL_HANDLE) {
217 if (barrier->subresource != 0 && barrier->subresource != all_subresources)
218 return ReplayResult::invalid_subresource;
219 } else if (!image_range(out.binding, barrier->subresource, out.range)) {
220 return ReplayResult::invalid_subresource;
221 }
222 } else if (const auto* clear = std::get_if<ClearRenderTarget>(&command)) {
223 if (!bind(clear->target.resource)) return ReplayResult::unresolved_resource;
224 if (!out.binding.image || out.binding.aspects != VK_IMAGE_ASPECT_COLOR_BIT)
225 return ReplayResult::invalid_clear;
226 if (!detail::view_range(out.binding, clear->target, VK_IMAGE_ASPECT_COLOR_BIT, out.range))
227 return ReplayResult::invalid_subresource;
228 } else if (const auto* clear = std::get_if<ClearDepthStencil>(&command)) {
229 if (!bind(clear->target.resource)) return ReplayResult::unresolved_resource;
230 VkImageAspectFlags aspects = 0;
231 if (clear->flags & clear_depth) aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
232 if (clear->flags & clear_stencil) aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
233 const VkImageAspectFlags available = out.binding.aspects & depth_stencil;
234 if (!out.binding.image || !available || (aspects & ~available) ||
235 (out.binding.aspects & ~depth_stencil))
236 return ReplayResult::invalid_clear;
237 if (!detail::view_range(out.binding, clear->target, aspects, out.range))
238 return ReplayResult::invalid_subresource;
239 }
240 }
241
242 const VulkanAccess transfer_dst = vulkan_access(d3d12_state::copy_dest, false);
243 for (std::size_t i = 0; i < commands.size(); ++i) {
244 const auto& command = commands[i];
245 const auto& p = prepared[i];
246 if (const auto* barrier = std::get_if<TransitionBarrier>(&command)) {
247 const VulkanAccess from = vulkan_access(barrier->before, p.binding.presentable);
248 const VulkanAccess to = vulkan_access(barrier->after, p.binding.presentable);
249 if (p.binding.image)
250 detail::image_barrier(vk, command_buffer, p.binding.image, p.range, from, to);
251 else
252 detail::memory_barrier(vk, command_buffer, from, to);
253 } else if (std::holds_alternative<UavBarrier>(command)) {
254 const VulkanAccess uav = vulkan_access(d3d12_state::unordered_access, false);
255 detail::memory_barrier(vk, command_buffer, {uav.layout, VK_ACCESS_SHADER_WRITE_BIT, uav.stages}, uav);
256 } else if (std::holds_alternative<AliasingBarrier>(command)) {
257 const VulkanAccess any = vulkan_access(~0u, false);
258 detail::memory_barrier(vk, command_buffer, {any.layout, VK_ACCESS_MEMORY_WRITE_BIT, any.stages}, any);
259 } else if (const auto* clear = std::get_if<ClearRenderTarget>(&command)) {
260 const VulkanAccess target = vulkan_access(d3d12_state::render_target, false);
261 detail::image_barrier(vk, command_buffer, p.binding.image, p.range, target, transfer_dst);
262 VkClearColorValue color{};
263 std::copy(clear->color.begin(), clear->color.end(), color.float32);
264 vk.clear_color_image(command_buffer, p.binding.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &color, 1,
265 &p.range);
266 detail::image_barrier(vk, command_buffer, p.binding.image, p.range, transfer_dst, target);
267 } else if (const auto* clear = std::get_if<ClearDepthStencil>(&command)) {
268 // The layout is shared by both planes unless separateDepthStencilLayouts is used, so the round trip
269 // moves every plane the image has; only the requested aspects are cleared.
270 VkImageSubresourceRange planes = p.range;
271 planes.aspectMask = p.binding.aspects;
272 const VulkanAccess target = vulkan_access(d3d12_state::depth_write, false);
273 detail::image_barrier(vk, command_buffer, p.binding.image, planes, target, transfer_dst);
274 const VkClearDepthStencilValue value{clear->depth, clear->stencil};
275 vk.clear_depth_stencil_image(command_buffer, p.binding.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
276 &value, 1, &p.range);
277 detail::image_barrier(vk, command_buffer, p.binding.image, planes, transfer_dst, target);
278 }
279 }
280 return ReplayResult::success;
281}
282
283} // namespace leonida