-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathblock.hpp
More file actions
260 lines (214 loc) · 5.93 KB
/
Copy pathblock.hpp
File metadata and controls
260 lines (214 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* @file liblava/block/block.hpp
* @brief Command buffer model
* @authors Lava Block OÜ and contributors
* @copyright Copyright (c) 2018-present, MIT License
*/
#pragma once
#include "liblava/base/device.hpp"
namespace lava {
/**
* @brief Block command
*/
struct command : entity {
/// Shared pointer to command
using s_ptr = std::shared_ptr<command>;
/// Const pointer to command
using c_ptr = command const*;
/// Map of commands
using s_map = std::map<id, s_ptr>;
/// List of commands
using c_list = std::vector<c_ptr>;
/// List of command buffers
VkCommandBuffers buffers = {};
/// Command process function
using process_func = std::function<void(VkCommandBuffer)>;
/// Called on command process
process_func on_process;
/// Active state
bool active = true;
/**
* @brief Make a new command
* @return s_ptr Shared pointer to command
*/
static s_ptr make() {
return std::make_shared<command>();
}
/**
* @brief Create a new command
* @param device Vulkan device
* @param frame_count Number of frames
* @param command_pools List of command pools
* @return Create was successful or failed
*/
bool create(device::ptr device,
index frame_count,
VkCommandPools command_pools);
/**
* @brief Destroy the command
* @param device Vulkan device
* @param command_pools List of command pools
*/
void destroy(device::ptr device,
VkCommandPools command_pools);
};
/**
* @brief Block of commands
*/
struct block : entity {
/// Pointer to block
using ptr = block*;
/// Shared pointer to block
using s_ptr = std::shared_ptr<block>;
/// Const pointer to block
using c_ptr = block const*;
/// Map of blocks
using s_map = std::map<id, s_ptr>;
/// List of blocks
using c_list = std::vector<c_ptr>;
/**
* @brief Make a new block
* @return s_ptr Shared pointer to block
*/
static s_ptr make() {
return std::make_shared<block>();
}
/**
* @brief Destroy the block
*/
~block() {
destroy();
}
/**
* @brief Create a new block
* @param device Vulkan device
* @param frame_count Number of frames
* @param queue_family Queue family index
* @return Create was successful or failed
*/
bool create(device::ptr device,
index frame_count,
index queue_family);
/**
* @brief Destroy the block
*/
void destroy();
/**
* @brief Get the frame count
* @return index Number of frames
*/
index get_frame_count() const {
return to_index(m_cmd_pools.size());
}
/**
* @see add_command
*/
id add_cmd(command::process_func func, bool active = true);
/**
* @brief Add a command
* @param func Command function
* @param active Active state
* @return id Command id
*/
id add_command(command::process_func func, bool active = true) {
return add_cmd(func, active);
}
/**
* @see remove_command
*/
void remove_cmd(id::ref cmd_id);
/**
* @brief Remove the command
* @param cmd_id Command id
*/
void remove_command(id::ref cmd_id) {
remove_cmd(cmd_id);
}
/**
* @brief Process the block
* @param frame Frame index
* @return Process was successful or aborted
*/
bool process(index frame);
/**
* @brief Get the current frame
* @return index Current frame
*/
index get_current_frame() const {
return m_current_frame;
}
/**
* @brief Get the command buffer
* @param cmd_id Command id
* @return VkCommandBuffer Vulkan command buffer
*/
VkCommandBuffer get_command_buffer(id::ref cmd_id) const {
return m_commands.at(cmd_id)->buffers.at(m_current_frame);
}
/**
* @brief Get the command buffer
* @param cmd_id Command id
* @param frame Frame index
* @return VkCommandBuffer Vulkan command buffer
*/
VkCommandBuffer get_command_buffer(id::ref cmd_id, index frame) const {
return m_commands.at(cmd_id)->buffers.at(frame);
}
/**
* @brief Collect the buffers
* @return VkCommandBuffers List of Vulkan command buffers
*/
VkCommandBuffers collect_buffers() {
VkCommandBuffers result;
for (auto& cmd : m_cmd_order)
if (cmd->active)
result.push_back(cmd->buffers.at(m_current_frame));
return result;
}
/**
* @brief Get the commands
* @return command::s_map const& Map of commands
*/
command::s_map const& get_commands() const {
return m_commands;
}
/**
* @brief Get the cmd order
* @return command::c_list const& List of commands
*/
command::c_list const& get_cmd_order() const {
return m_cmd_order;
}
/**
* @brief Check if command is activated
* @param cmd_id Command id
* @return Command is active or not
*/
bool activated(id::ref cmd_id);
/**
* @brief Set the command active
* @param cmd_id Command id
* @param active Active state
* @return Set was successful or failed
*/
bool set_active(id::ref cmd_id, bool active = true);
/**
* @brief Get the device
* @return device::ptr Vulkan device
*/
device::ptr get_device() {
return m_device;
}
private:
/// Vulkan device
device::ptr m_device = nullptr;
/// Current frame index
index m_current_frame = 0;
/// Command pools
VkCommandPools m_cmd_pools = {};
/// Map of commands
command::s_map m_commands;
/// Ordered list of commands
command::c_list m_cmd_order;
};
} // namespace lava