Toast Engine
Loading...
Searching...
No Matches
async_compute.hpp
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <array>
8#include <cstdint>
9#include <functional>
10#include <limits>
11#include <mutex>
12#include <string_view>
13#include <toast/export.hpp>
14#include <vector>
15#include <vulkan/vulkan_raii.hpp>
16
17namespace renderer {
18
19class TOAST_API AsyncCompute {
20public:
22 using Ticket = uint64_t;
23
24 static constexpr Ticket k_no_ticket = 0;
25
27 AsyncCompute(const vk::raii::Device& device, uint32_t queue_family_index, vk::Queue queue, std::mutex* queue_mutex);
28
30
31 AsyncCompute(const AsyncCompute&) = delete;
32 auto operator=(const AsyncCompute&) -> AsyncCompute& = delete;
33 AsyncCompute(AsyncCompute&&) = delete;
34 auto operator=(AsyncCompute&&) -> AsyncCompute& = delete;
35
38 auto submit(const std::function<void(vk::CommandBuffer)>& record, std::string_view debug_name = {}) -> Ticket;
39
40 [[nodiscard]]
41 auto isComplete(Ticket ticket) const -> bool;
42
44 auto wait(Ticket ticket, uint64_t timeout_ns = std::numeric_limits<uint64_t>::max()) const -> bool;
45
46 [[nodiscard]]
47 auto completedTicket() const -> Ticket;
48
49 [[nodiscard]]
50 auto lastSubmittedTicket() const -> Ticket;
51
52 [[nodiscard]]
53 auto queueFamilyIndex() const noexcept -> uint32_t {
54 return m_queue_family_index;
55 }
56
58 [[nodiscard]]
59 auto timeline() const noexcept -> vk::Semaphore {
60 return *m_timeline;
61 }
62
65 std::array<uint32_t, 2> indices {};
66 uint32_t count = 0;
67 };
68
69 [[nodiscard]]
70 auto sharedFamilies(uint32_t graphics_family) const noexcept -> SharedFamilies;
71
72private:
73 struct Slot {
74 vk::raii::CommandPool pool = nullptr;
75 vk::raii::CommandBuffer cmd = nullptr;
76 Ticket ticket = k_no_ticket;
77 };
78
80 void reclaimLocked();
81 auto acquireSlot() -> Slot;
82
83 const vk::raii::Device* m_device = nullptr;
84 uint32_t m_queue_family_index = 0;
85 vk::Queue m_queue = nullptr;
86 std::mutex* m_queue_mutex = nullptr;
87
88 vk::raii::Semaphore m_timeline = nullptr;
89
90 mutable std::mutex m_mutex;
91 Ticket m_last_submitted = k_no_ticket;
92 std::vector<Slot> m_in_flight;
93 std::vector<Slot> m_free;
94};
95
96}
AsyncCompute(const vk::raii::Device &device, uint32_t queue_family_index, vk::Queue queue, std::mutex *queue_mutex)
Definition async_compute.cpp:16
auto timeline() const noexcept -> vk::Semaphore
Never wait on it from a frame submit or the frame couples to async work again.
Definition async_compute.hpp:59
uint64_t Ticket
Timeline value so completing one completes every earlier one.
Definition async_compute.hpp:22
auto submit(const std::function< void(vk::CommandBuffer)> &record, std::string_view debug_name={}) -> Ticket
Definition async_compute.cpp:74
For eConcurrent create infos with count entries since one family repeats.
Definition async_compute.hpp:64