Repository navigation
Expand file tree
/
Copy pathdevice.hpp
More file actions
409 lines (344 loc) · 10.2 KB
/
Copy pathdevice.hpp
File metadata and controls
409 lines (344 loc) · 10.2 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**
* @file liblava/base/device.hpp
* @brief Vulkan device
* @authors Lava Block OÜ and contributors
* @copyright Copyright (c) 2018-present, MIT License
*/
#pragma once
#include "liblava/base/device_table.hpp"
#include "liblava/base/queue.hpp"
#include "liblava/core/data.hpp"
#include "liblava/core/id.hpp"
#include "liblava/fwd.hpp"
namespace lava {
/**
* @brief Vulkan device
*/
struct device : device_table, entity {
/// Pointer to device
using ptr = device*;
/// Const pointer to device
using c_ptr = device const*;
/// Shared pointer to a device
using s_ptr = std::shared_ptr<device>;
/// List of devices
using s_list = std::vector<s_ptr>;
/// Const pointer to a physical device
using physical_device_c_ptr = physical_device const*;
/**
* @brief Device create parameters
*/
struct create_param {
/// Reference to device create parameters
using ref = create_param const&;
/// Physical device
physical_device_c_ptr physical_device = nullptr;
/// VMA flags
VmaAllocatorCreateFlags vma_flags = 0;
/// List of extensions to enable
names extensions;
/// List of physical device features to enable
VkPhysicalDeviceFeatures features{};
/// Must be true if .next points to a VkPhysicalDevice2 instance
bool has_features_2 = false;
/// Create parameter next pointer (pNext)
void const* next = nullptr;
/// List of queue famiy infos
queue_family_info::list queue_family_infos;
/**
* @brief Add swapchain extension
*/
void add_swapchain_extension() {
extensions.push_back("VK_KHR_swapchain");
}
/**
* @brief Add portability subset extension
*/
void add_portability_subset_extension() {
extensions.push_back("VK_KHR_portability_subset");
}
/**
* @brief Set the default queues
*/
void set_default_queues() {
lava::set_default_queues(queue_family_infos);
}
/**
* @brief Set the all queues
*/
void set_all_queues();
/**
* @brief Add queue
* @param flags Queue flags
* @param priority Priority for queue
* @return Add was successful or failed
*/
bool add_queue(VkQueueFlags flags,
r32 priority = 1.f) {
return add_queues(flags, 1, priority);
}
/**
* @brief Add queues
* @param flags Queue flags
* @param count Number of queues
* @param priority Priority for queues
* @return Add was successful or failed
*/
bool add_queues(VkQueueFlags flags,
ui32 count,
r32 priority = 1.f);
/**
* @brief Add all dedicated queues
* @param priority Priority for queues
* @return Add was successful or failed
*/
bool add_dedicated_queues(r32 priority = 1.f);
/**
* @brief Verify queues
* @return verify_queues_result Verification result
*/
verify_queues_result verify_queues() const;
};
/**
* @brief Make a new device
* @return s_ptr Shared pointer to device
*/
static s_ptr make() {
return std::make_shared<device>();
}
/**
* @brief Destroy the device
*/
~device() {
destroy();
}
/**
* @brief Create a new device
* @param param Create parameters
* @return Create was successful or failed
*/
bool create(create_param::ref param);
/**
* @brief Destroy the device
*/
void destroy();
/**
* @brief Get a graphics queue by index
* @param index Index of queue
* @return queue::ref Graphics queue
*/
queue::ref get_graphics_queue(index index = 0) const {
return get_graphics_queues().at(index);
}
/**
* @see get_graphics_queue
*/
queue::ref graphics_queue(index index = 0) const {
return get_graphics_queue(index);
}
/**
* @brief Get a compute queue by index
* @param index Index of queue
* @return queue::ref Compute queue
*/
queue::ref get_compute_queue(index index = 0) const {
return get_compute_queues().at(index);
}
/**
* @see get_compute_queue
*/
queue::ref compute_queue(index index = 0) const {
return get_compute_queue(index);
}
/**
* @brief Get a transfer queue by index
* @param index Index of queue
* @return queue::ref Transfer queue
*/
queue::ref get_transfer_queue(index index = 0) const {
return get_transfer_queues().at(index);
}
/**
* @see get_transfer_queue
*/
queue::ref transfer_queue(index index = 0) const {
return get_transfer_queue(index);
}
/**
* @brief Get the list of graphics queues
* @return queue::list const& Graphics queues
*/
queue::list const& get_graphics_queues() const {
return m_graphics_queue_list;
}
/**
* @see get_graphics_queues
*/
queue::list const& graphics_queues() const {
return get_graphics_queues();
}
/**
* @brief Get the list of compute queues
* @return queue::list const& Compute queues
*/
queue::list const& get_compute_queues() const {
return m_compute_queue_list;
}
/**
* @see get_compute_queues
*/
queue::list const& compute_queues() const {
return get_compute_queues();
}
/**
* @brief Get the list of transfer queues
* @return queue::list const& Transfer queues
*/
queue::list const& get_transfer_queues() const {
return m_transfer_queue_list;
}
/**
* @see get_transfer_queues
*/
queue::list const& transfer_queues() const {
return get_transfer_queues();
}
/**
* @brief Get all queues
* @return queue::list const& List of all queues
*/
queue::list const& get_queues() const {
return m_queue_list;
}
/**
* @see get_queues
*/
queue::list const& queues() const {
return get_queues();
}
/**
* @brief Get the Vulkan device
* @return VkDevice Vulkan device
*/
VkDevice get() const {
return vk_device;
}
/**
* @brief Get the Volk device table
* @return VolkDeviceTable const& Volk device table
*/
VolkDeviceTable const& call() const {
return table;
}
/**
* @brief Wait for idle
* @return Wait was successful or failed
*/
bool wait_for_idle() const {
return check(call().vkDeviceWaitIdle(vk_device));
}
/**
* @brief Get the physical device
* @return physical_device_c_ptr Physical device
*/
physical_device_c_ptr get_physical_device() const {
return m_physical_device;
}
/**
* @brief Get the Vulkan physical device
* @return VkPhysicalDevice Vulkan physical device
*/
VkPhysicalDevice get_vk_physical_device() const;
/**
* @brief Get the physical device features
* @return VkPhysicalDeviceFeatures const& Features
*/
VkPhysicalDeviceFeatures const& get_features() const;
/**
* @brief Get the physical device properties
* @return VkPhysicalDeviceProperties const& Properties
*/
VkPhysicalDeviceProperties const& get_properties() const;
/**
* @brief Check if surface is supported by this device
* @param surface Surface to check
* @return Surface is supported or not
*/
bool surface_supported(VkSurfaceKHR surface) const;
/**
* @brief Set the allocator for this device
* @param value Allocator
*/
void set_allocator(allocator::s_ptr value) {
m_mem_allocator = value;
}
/**
* @brief Get the allocator of this device
* @return allocator::s_ptr Allocator
*/
allocator::s_ptr get_allocator() {
return m_mem_allocator;
}
/**
* @brief Get the VMA allocator
* @return VmaAllocator VMA allocator
*/
VmaAllocator alloc() const {
return m_mem_allocator != nullptr
? m_mem_allocator->get()
: nullptr;
}
private:
/// Physical device
physical_device_c_ptr m_physical_device = nullptr;
/// List of qraphics queues
queue::list m_graphics_queue_list;
/// List of compute queues
queue::list m_compute_queue_list;
/// List of transfer queues
queue::list m_transfer_queue_list;
/// List of all queues
queue::list m_queue_list;
/// Device features
VkPhysicalDeviceFeatures m_features{};
/// Device allocator
allocator::s_ptr m_mem_allocator;
};
/**
* @brief Create a shader module
* @param device Vulkan device
* @param data Shader data
* @return VkShaderModule Shader module
*/
VkShaderModule create_shader_module(device::ptr device,
c_data::ref data);
/// One time command function
using one_time_command_func = std::function<void(VkCommandBuffer)>;
/**
* @brief Submit one time command function with pool
* @param device Vulkan device
* @param pool Command pool (VK_NULL_HANDLE: managed)
* @param queue Target queue
* @param callback Function to be called
* @return Submit was successful or failed
*/
bool one_time_submit_pool(device::ptr device,
VkCommandPool pool,
queue::ref queue,
one_time_command_func callback);
/**
* @brief Submit one time command function
* @param device Vulkan device
* @param queue Target queue
* @param callback Function to be called
* @return Submit was successful or failed
*/
inline bool one_time_submit(device::ptr device,
queue::ref queue,
one_time_command_func callback) {
return one_time_submit_pool(device,
VK_NULL_HANDLE,
queue,
callback);
}
} // namespace lava