MayaFlux 0.1.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
49{
50 vk::CommandBufferAllocateInfo alloc_info {};
51 alloc_info.commandPool = m_command_pool;
52 alloc_info.level = vk::CommandBufferLevel::ePrimary;
53 alloc_info.commandBufferCount = 1;
54
55 try {
56 auto buffers = m_device.allocateCommandBuffers(alloc_info);
57 m_allocated_buffers.push_back(buffers[0]);
58 return buffers[0];
59 } catch (const vk::SystemError& e) {
61 "Failed to allocate command buffer: {}", e.what());
62 return nullptr;
63 }
64}
65
66void VKCommandManager::free_command_buffer(vk::CommandBuffer command_buffer)
67{
68 if (!command_buffer) {
69 return;
70 }
71
72 auto it = std::ranges::find(m_allocated_buffers, command_buffer);
73 if (it != m_allocated_buffers.end()) {
74 m_device.freeCommandBuffers(m_command_pool, 1, &command_buffer);
75 m_allocated_buffers.erase(it);
76 }
77}
78
80{
81 vk::CommandBuffer command_buffer = allocate_command_buffer();
82
83 vk::CommandBufferBeginInfo begin_info {};
84 begin_info.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit;
85
86 command_buffer.begin(begin_info);
87
88 return command_buffer;
89}
90
91void VKCommandManager::end_single_time_commands(vk::CommandBuffer command_buffer, vk::Queue queue)
92{
93 command_buffer.end();
94
95 vk::SubmitInfo submit_info {};
96 submit_info.commandBufferCount = 1;
97 submit_info.pCommandBuffers = &command_buffer;
98
99 if (auto result = queue.submit(1, &submit_info, nullptr); result != vk::Result::eSuccess) {
101 "Failed to submit single time command buffer: {}", vk::to_string(result));
102 }
103 queue.waitIdle();
104
105 free_command_buffer(command_buffer);
106}
107
109{
110 m_device.resetCommandPool(m_command_pool, vk::CommandPoolResetFlags {});
111}
112
113} // namespace MayaFlux::Core
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(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 allocate_command_buffer()
Allocate a command buffer from the pool.
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.
@ GraphicsBackend
Graphics/visual rendering backend (Vulkan, OpenGL)
@ Core
Core engine, backend, subsystems.