MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
VKCommandManager.cpp
Go to the documentation of this file.
2
4
5namespace MayaFlux::Core {
6
11
12bool VKCommandManager::initialize(vk::Device device, uint32_t graphics_queue_family)
13{
14 m_device = device;
15 m_graphics_queue_family = graphics_queue_family;
16
17 vk::CommandPoolCreateInfo pool_info {};
18 pool_info.queueFamilyIndex = graphics_queue_family;
19 pool_info.flags = vk::CommandPoolCreateFlagBits::eResetCommandBuffer;
20
21 try {
22 m_command_pool = device.createCommandPool(pool_info);
23 } catch (const vk::SystemError& e) {
25 "Failed to create command pool: {}", e.what());
26 return false;
27 }
28
30 "Command manager initialized");
31
32 return true;
33}
34
36{
37 if (m_command_pool) {
38 if (!m_allocated_buffers.empty()) {
39 m_device.freeCommandBuffers(m_command_pool, m_allocated_buffers);
40 m_allocated_buffers.clear();
41 }
42
43 m_device.destroyCommandPool(m_command_pool);
44 m_command_pool = nullptr;
45 }
46}
47
48vk::CommandBuffer VKCommandManager::allocate_command_buffer(vk::CommandBufferLevel level)
49{
50 vk::CommandBufferAllocateInfo alloc_info {};
51 alloc_info.commandPool = m_command_pool;
52 alloc_info.level = level;
53 alloc_info.commandBufferCount = 1;
54
55 vk::CommandBuffer cmd_buffer;
56 try {
57 auto result = m_device.allocateCommandBuffers(&alloc_info, &cmd_buffer);
58 if (result != vk::Result::eSuccess) {
60 "Failed to allocate command buffer");
61 return nullptr;
62 }
63 m_allocated_buffers.push_back(cmd_buffer);
64 } catch (const vk::SystemError& e) {
66 "Failed to allocate command buffer: {}", e.what());
67 return nullptr;
68 }
69
71 "Allocated {} command buffer",
72 level == vk::CommandBufferLevel::ePrimary ? "primary" : "secondary");
73
74 return cmd_buffer;
75}
76
77void VKCommandManager::free_command_buffer(vk::CommandBuffer command_buffer)
78{
79 if (!command_buffer) {
80 return;
81 }
82
83 auto it = std::ranges::find(m_allocated_buffers, command_buffer);
84 if (it != m_allocated_buffers.end()) {
85 m_device.freeCommandBuffers(m_command_pool, 1, &command_buffer);
86 m_allocated_buffers.erase(it);
87 }
88}
89
91{
92 vk::CommandBuffer command_buffer = allocate_command_buffer();
93
94 vk::CommandBufferBeginInfo begin_info {};
95 begin_info.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit;
96
97 command_buffer.begin(begin_info);
98
99 return command_buffer;
100}
101
102void VKCommandManager::end_single_time_commands(vk::CommandBuffer command_buffer, vk::Queue queue)
103{
104 command_buffer.end();
105
106 vk::SubmitInfo submit_info {};
107 submit_info.commandBufferCount = 1;
108 submit_info.pCommandBuffers = &command_buffer;
109
110 if (auto result = queue.submit(1, &submit_info, nullptr); result != vk::Result::eSuccess) {
112 "Failed to submit single time command buffer: {}", vk::to_string(result));
113 }
114 queue.waitIdle();
115
116 free_command_buffer(command_buffer);
117}
118
120{
121 m_device.resetCommandPool(m_command_pool, vk::CommandPoolResetFlags {});
122}
123
124} // namespace MayaFlux::Core
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_RT_TRACE(comp, ctx,...)
void reset_pool()
Reset command pool (invalidates all allocated buffers)
void end_single_time_commands(vk::CommandBuffer command_buffer, vk::Queue queue)
End and submit single-time command.
void free_command_buffer(vk::CommandBuffer command_buffer)
Free a command buffer back to the pool.
bool initialize(vk::Device device, uint32_t graphics_queue_family)
Initialize command manager.
vk::CommandBuffer begin_single_time_commands()
Begin single-time command (for transfers, etc.)
std::vector< vk::CommandBuffer > m_allocated_buffers
void cleanup()
Cleanup all command pools and buffers.
vk::CommandBuffer allocate_command_buffer(vk::CommandBufferLevel level=vk::CommandBufferLevel::ePrimary)
Allocate a command buffer with specified level.
@ GraphicsBackend
Graphics/visual rendering backend (Vulkan, OpenGL)
@ Core
Core engine, backend, subsystems.