MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SlotMachine.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "GraphicsUtils.hpp"
4
5#include "vulkan/vulkan.hpp"
6
7namespace MayaFlux::Buffers {
8class VKBuffer;
9}
10
12
13/**
14 * @class SlotLease
15 * @brief A claim on one slot of a SlotArena for as long as the claim exists
16 *
17 * Copies and moves share the claim; the slot is in use until every copy is
18 * gone. A command buffer that pushed the slot's range holds one until its
19 * submission completes, so the slot cannot be reused while the GPU may read it.
20 */
21class MAYAFLUX_API SlotLease {
22public:
23 SlotLease() = default;
24
25 explicit SlotLease(std::shared_ptr<std::atomic<uint32_t>> uses)
26 : m_uses(std::move(uses))
27 {
28 if (m_uses) {
29 m_uses->fetch_add(1, std::memory_order_relaxed);
30 }
31 }
32
33 SlotLease(const SlotLease& other)
34 : m_uses(other.m_uses)
35 {
36 if (m_uses) {
37 m_uses->fetch_add(1, std::memory_order_relaxed);
38 }
39 }
40
41 SlotLease(SlotLease&& other) noexcept = default;
42
44 {
45 if (this != &other) {
46 release();
47 m_uses = other.m_uses;
48 if (m_uses) {
49 m_uses->fetch_add(1, std::memory_order_relaxed);
50 }
51 }
52 return *this;
53 }
54
55 SlotLease& operator=(SlotLease&& other) noexcept
56 {
57 if (this != &other) {
58 release();
59 m_uses = std::move(other.m_uses);
60 }
61 return *this;
62 }
63
65
66 [[nodiscard]] explicit operator bool() const noexcept { return m_uses != nullptr; }
67
68private:
69 void release() noexcept
70 {
71 if (m_uses) {
72 m_uses->fetch_sub(1, std::memory_order_release);
73 m_uses.reset();
74 }
75 }
76
77 std::shared_ptr<std::atomic<uint32_t>> m_uses;
78};
79
80/**
81 * @class SlotArena
82 * @brief One host-visible buffer split into equal slots, with the slot chosen where the descriptor is pushed
83 *
84 * For data the host rewrites while the GPU may still be reading an earlier
85 * version of it. The owner writes a slot nothing holds a lease on and pushes
86 * that slot's range. Whoever records the push takes a lease on the slot and
87 * the command buffer keeps it until its submission completes. Nothing waits:
88 * acquire() returns a slot with no lease, and grows the arena when every
89 * slot is held.
90 *
91 * Slots are aligned to 256 bytes, the largest uniform buffer offset alignment
92 * Vulkan permits, so a slot's range is valid for uniform and storage bindings
93 * on every device.
94 */
95class MAYAFLUX_API SlotArena {
96public:
97 SlotArena() = default;
98 ~SlotArena() = default;
99
100 SlotArena(const SlotArena&) = delete;
101 SlotArena& operator=(const SlotArena&) = delete;
102 SlotArena(SlotArena&&) = delete;
104
105 /**
106 * @brief Allocate the arena, releasing any previous buffer
107 * @param slot_bytes Bytes each slot holds
108 * @param usage Buffer usage, which must be host visible
109 * @param slots Initial slot count
110 * @return True if the buffer is allocated and mapped
111 */
112 bool initialize(size_t slot_bytes, BufferUsageHint usage, size_t slots = 2);
113
114 [[nodiscard]] bool is_initialized() const noexcept { return m_buffer != nullptr; }
115
116 /**
117 * @brief A slot nothing holds a lease on, growing the arena if none is free
118 */
119 [[nodiscard]] size_t acquire();
120
121 /**
122 * @brief Copy bytes into a slot and flush them
123 * @param slot Slot from acquire()
124 * @param data Source bytes
125 * @param size Byte count, at most the slot size
126 */
127 void write(size_t slot, const void* data, size_t size);
128
129 /**
130 * @brief Claim a slot until the returned lease and its copies are gone
131 */
132 [[nodiscard]] SlotLease lease(size_t slot) const;
133
134 /**
135 * @brief Buffer, offset and range of a slot, ready for a descriptor write
136 */
137 [[nodiscard]] vk::DescriptorBufferInfo info(size_t slot) const;
138
139 [[nodiscard]] size_t slot_count() const noexcept { return m_uses.size(); }
140
141 [[nodiscard]] size_t slot_bytes() const noexcept { return m_slot_bytes; }
142
143 /**
144 * @brief The backing buffer, which changes when the arena grows
145 */
146 [[nodiscard]] const std::shared_ptr<Buffers::VKBuffer>& buffer() const noexcept { return m_buffer; }
147
148 /**
149 * @brief Hand the buffer to the caller, leaving the arena empty
150 *
151 * For an owner that keeps the buffer alive until its readers finish.
152 */
153 [[nodiscard]] std::shared_ptr<Buffers::VKBuffer> detach();
154
155 /**
156 * @brief Release the buffer once the work submitted so far completes
157 */
158 void release();
159
160private:
161 bool grow();
162
163 std::shared_ptr<Buffers::VKBuffer> m_buffer;
164 std::vector<std::shared_ptr<std::atomic<uint32_t>>> m_uses;
165 size_t m_slot_bytes {};
166 size_t m_stride {};
168};
169
170} // namespace MayaFlux::Portal::Graphics
Vulkan-backed buffer wrapper used in processing chains.
Definition VKBuffer.hpp:76
SlotArena & operator=(SlotArena &&)=delete
std::vector< std::shared_ptr< std::atomic< uint32_t > > > m_uses
SlotArena(const SlotArena &)=delete
const std::shared_ptr< Buffers::VKBuffer > & buffer() const noexcept
The backing buffer, which changes when the arena grows.
SlotArena & operator=(const SlotArena &)=delete
std::shared_ptr< Buffers::VKBuffer > m_buffer
SlotLease(std::shared_ptr< std::atomic< uint32_t > > uses)
SlotLease(SlotLease &&other) noexcept=default
SlotLease & operator=(const SlotLease &other)
std::shared_ptr< std::atomic< uint32_t > > m_uses
SlotLease(const SlotLease &other)
SlotLease & operator=(SlotLease &&other) noexcept
A claim on one slot of a SlotArena for as long as the claim exists.
void initialize()
Definition main.cpp:10
BufferUsageHint
Semantic usage hint for buffer allocation and memory properties.