Toast Engine
Loading...
Searching...
No Matches
gpu_sync.hpp
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <cstdint>
8#include <span>
9#include <toast/export.hpp>
10#include <vector>
11#include <vulkan/vulkan_raii.hpp>
12
13namespace renderer::sync {
14
15enum class Usage : uint8_t {
17 none,
18
25
26 fragment_sampled,
27 compute_sampled,
30
31 compute_storage_read,
32 compute_storage_write,
37 fragment_storage_read,
42 indirect_read,
43
44 transfer_src,
45 transfer_dst,
46 host_read,
47 host_write,
48
51
56
59};
60
61struct UsageInfo {
62 vk::PipelineStageFlags2 stages;
63 vk::AccessFlags2 access;
64 vk::ImageLayout layout = vk::ImageLayout::eUndefined;
65};
66
67[[nodiscard]]
68TOAST_API auto info(Usage usage) -> UsageInfo;
69
70[[nodiscard]]
71TOAST_API auto writes(Usage usage) -> bool;
72
74struct ImageState {
75 vk::Image image = VK_NULL_HANDLE;
76 vk::ImageSubresourceRange range {vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1};
77 Usage usage = Usage::none;
78
80 void reset(vk::Image new_image, const vk::ImageSubresourceRange& new_range) {
81 image = new_image;
82 range = new_range;
83 usage = Usage::none;
84 }
85
86 [[nodiscard]]
87 explicit operator bool() const {
88 return image != VK_NULL_HANDLE;
89 }
90};
91
93 uint32_t from = VK_QUEUE_FAMILY_IGNORED;
94 uint32_t to = VK_QUEUE_FAMILY_IGNORED;
96 bool exclusive = true;
97
98 [[nodiscard]]
99 auto crossesQueues() const noexcept -> bool {
100 return from != to;
101 }
102};
103
105class TOAST_API BarrierBatch {
106public:
108 void image(ImageState& state, Usage next);
109
110 void image(vk::Image image, const vk::ImageSubresourceRange& range, Usage previous, Usage next);
111
112 void buffer(vk::Buffer buffer, Usage previous, Usage next, vk::DeviceSize offset = 0, vk::DeviceSize size = VK_WHOLE_SIZE);
113
114 void memory(Usage previous, Usage next);
115
118 void release(vk::Image image, const vk::ImageSubresourceRange& range, Usage previous, Usage next, const QueueHandoff& handoff);
119
122 void acquire(vk::Image image, const vk::ImageSubresourceRange& range, Usage previous, Usage next, const QueueHandoff& handoff);
123
124 void release(
125 vk::Buffer buffer, Usage previous, Usage next, const QueueHandoff& handoff, vk::DeviceSize offset = 0,
126 vk::DeviceSize size = VK_WHOLE_SIZE
127 );
128
129 void acquire(
130 vk::Buffer buffer, Usage next, const QueueHandoff& handoff, vk::DeviceSize offset = 0, vk::DeviceSize size = VK_WHOLE_SIZE
131 );
132
133 void flush(vk::CommandBuffer cmd);
134
135 [[nodiscard]]
136 auto empty() const -> bool {
137 return m_images.empty() && m_buffers.empty() && m_memory.empty();
138 }
139
140 [[nodiscard]]
141 auto pendingImages() const -> std::span<const vk::ImageMemoryBarrier2> {
142 return m_images;
143 }
144
145 [[nodiscard]]
146 auto pendingBuffers() const -> std::span<const vk::BufferMemoryBarrier2> {
147 return m_buffers;
148 }
149
150 [[nodiscard]]
151 auto pendingMemory() const -> std::span<const vk::MemoryBarrier2> {
152 return m_memory;
153 }
154
155private:
156 std::vector<vk::ImageMemoryBarrier2> m_images;
157 std::vector<vk::BufferMemoryBarrier2> m_buffers;
158 std::vector<vk::MemoryBarrier2> m_memory;
159};
160
161TOAST_API void
162 transition(vk::CommandBuffer cmd, vk::Image image, const vk::ImageSubresourceRange& range, Usage previous, Usage next);
163
164TOAST_API void transition(vk::CommandBuffer cmd, ImageState& state, Usage next);
165
167 vk::Semaphore semaphore;
168 vk::PipelineStageFlags2 stages;
170 uint64_t value = 0;
171};
172
174 vk::Semaphore semaphore;
176 uint64_t value = 0;
177};
178
180 std::span<const vk::CommandBuffer> commands;
181 std::span<const SemaphoreWait> waits;
182 std::span<const SemaphoreSignal> signals;
183 vk::Fence fence;
185 const void* next = nullptr;
186};
187
189TOAST_API void submit(vk::Queue queue, const SubmitDesc& desc);
190
191}
Skips a barrier only when neither side writes and the layout and next read stages are already covered...
Definition gpu_sync.hpp:105
void release(vk::Image image, const vk::ImageSubresourceRange &range, Usage previous, Usage next, const QueueHandoff &handoff)
Definition gpu_sync.cpp:189
void acquire(vk::Image image, const vk::ImageSubresourceRange &range, Usage previous, Usage next, const QueueHandoff &handoff)
Definition gpu_sync.cpp:210
void image(ImageState &state, Usage next)
No op for an image that does not exist. The widest usage until the next write avoids read to read bar...
Definition gpu_sync.cpp:139
void submit(vk::Queue queue, const SubmitDesc &desc)
The caller holds any lock the queue needs.
Definition gpu_sync.cpp:307
Usage
Definition gpu_sync.hpp:15
@ depth_attachment
eDepthAttachmentOptimal including load and store ops
Definition gpu_sync.hpp:22
@ shader_sampled
Vertex fragment and compute.
Definition gpu_sync.hpp:29
@ none
Contents discarded and the layout undefined.
Definition gpu_sync.hpp:17
@ vertex_input
Vertex and index buffers.
Definition gpu_sync.hpp:41
@ present
Colour attachment output stage so the next use chains onto the acquire semaphore wait.
Definition gpu_sync.hpp:50
@ depth_sampled
eDepthReadOnlyOptimal sampled by fragment and compute shaders
Definition gpu_sync.hpp:24
@ color_attachment
Includes load and store ops.
Definition gpu_sync.hpp:20
@ vertex_storage_read
Vertex pulling.
Definition gpu_sync.hpp:34
@ acceleration_trace_fragment
Fragment shader ray queries.
Definition gpu_sync.hpp:55
@ general
Every stage and access in eGeneral so correct anywhere and slow everywhere.
Definition gpu_sync.hpp:58
@ acceleration_build
Build input output and scratch.
Definition gpu_sync.hpp:53
@ uniform_read
Any shader stage.
Definition gpu_sync.hpp:39
@ mesh_storage_read
Task and mesh shaders on devices with VK_EXT_mesh_shader only.
Definition gpu_sync.hpp:36
Owned next to the image it describes.
Definition gpu_sync.hpp:74
void reset(vk::Image new_image, const vk::ImageSubresourceRange &new_range)
On every image creation since its contents start undefined.
Definition gpu_sync.hpp:80
Definition gpu_sync.hpp:92
bool exclusive
False for eConcurrent resources which need visibility on the consumer but no ownership transfer.
Definition gpu_sync.hpp:96
Definition gpu_sync.hpp:173
uint64_t value
Timeline value and ignored for binary semaphores.
Definition gpu_sync.hpp:176
Definition gpu_sync.hpp:166
uint64_t value
Timeline value and ignored for binary semaphores.
Definition gpu_sync.hpp:170
Definition gpu_sync.hpp:179
const void * next
Chained to SubmitInfo2 such as a FrameBoundaryEXT.
Definition gpu_sync.hpp:185
Definition gpu_sync.hpp:61