SpiecsEngine
 
Loading...
Searching...
No Matches
VulkanCommandBuffer.cpp
Go to the documentation of this file.
1/**
2* @file VulkanCommandBuffer.cpp.
3* @brief The VulkanCommandPool Class and VulkanCommandBuffer Class Implementation.
4* @author Spices.
5*/
6
7#include "Pchheader.h"
10
11namespace Spices {
12
18
20 : VulkanObject(vulkanState)
21 {
23
24 m_IsPoolActive = true;
25
26 /**
27 * @brief Instanced a VkCommandPoolCreateInfo with default value.
28 */
29 VkCommandPoolCreateInfo poolInfo{};
30 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
31 poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
32 poolInfo.queueFamilyIndex = vulkanState.m_GraphicQueueFamily;
33
34 /**
35 * @brief Create commandpool and set it global.
36 */
37 VK_CHECK(vkCreateCommandPool(vulkanState.m_Device, &poolInfo, nullptr, &vulkanState.m_GraphicCommandPool))
38 DEBUGUTILS_SETOBJECTNAME(VK_OBJECT_TYPE_COMMAND_POOL, reinterpret_cast<uint64_t>(vulkanState.m_GraphicCommandPool), vulkanState.m_Device, "GraphicCommandPool")
39
40 VulkanCommandPoolThreadWrapper::GetInst().m_GraphicThreadId = 0;
41 m_ThreadGraphicCommandPool.push_back(vulkanState.m_GraphicCommandPool);
42
43 poolInfo.queueFamilyIndex = vulkanState.m_ComputeQueueFamily;
44
45 /**
46 * @brief Create commandpool and set it global.
47 */
48 VK_CHECK(vkCreateCommandPool(vulkanState.m_Device, &poolInfo, nullptr, &vulkanState.m_ComputeCommandPool))
49 DEBUGUTILS_SETOBJECTNAME(VK_OBJECT_TYPE_COMMAND_POOL, reinterpret_cast<uint64_t>(vulkanState.m_ComputeCommandPool), vulkanState.m_Device, "ComputeCommandPool")
50
51 VulkanCommandPoolThreadWrapper::GetInst().m_ComputeThreadId = 0;
52 m_ThreadComputeCommandPool.push_back(vulkanState.m_ComputeCommandPool);
53 }
54
56 {
58
59 m_IsPoolActive = false;
60
61 /**
62 * @brief Destroy the Vulkan CommandPool Object.
63 */
64 for(auto& pool : m_ThreadGraphicCommandPool)
65 {
66 if (pool)
67 {
68 vkDestroyCommandPool(m_VulkanState.m_Device, pool, nullptr);
69 pool = nullptr;
70 }
71 }
72
73 for(auto& pool : m_ThreadComputeCommandPool)
74 {
75 if (pool)
76 {
77 vkDestroyCommandPool(m_VulkanState.m_Device, pool, nullptr);
78 pool = nullptr;
79 }
80 }
81 }
82
83 VkCommandPool& VulkanCommandPool::GetThreadGraphicCommandPool()
84 {
86
87 std::unique_lock<std::mutex> lock(m_GraphicCommandPoolMutex);
88
89 if (!m_IsPoolActive)
90 {
91 SPICES_CORE_ERROR("CommandPool is not active.")
92 }
93
95 {
96 for (int i = 0; i < m_ThreadGraphicCommandPool.size(); i++)
97 {
98 if (!m_ThreadGraphicCommandPool[i]) VulkanCommandPoolThreadWrapper::GetInst().m_GraphicThreadId = i;
99 }
100
102 {
103 VulkanCommandPoolThreadWrapper::GetInst().m_GraphicThreadId = m_ThreadGraphicCommandPool.size();
104 m_ThreadGraphicCommandPool.push_back(nullptr);
105 }
106 }
107
108 if(!m_ThreadGraphicCommandPool[VulkanCommandPoolThreadWrapper::GetInst().m_GraphicThreadId])
109 {
110 /**
111 * @brief Instanced a VkCommandPoolCreateInfo with default value.
112 */
113 VkCommandPoolCreateInfo poolInfo{};
114 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
115 poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
116 poolInfo.queueFamilyIndex = VulkanRenderBackend::GetState().m_GraphicQueueFamily;
117
118 /**
119 * @brief Create commandpool and set it global.
120 */
121 VkCommandPool pool;
122 VK_CHECK(vkCreateCommandPool(VulkanRenderBackend::GetState().m_Device, &poolInfo, nullptr, &pool))
123 DEBUGUTILS_SETOBJECTNAME(VK_OBJECT_TYPE_COMMAND_POOL, reinterpret_cast<uint64_t>(pool), VulkanRenderBackend::GetState().m_Device, "ThreadGraphicCommandPool")
124 m_ThreadGraphicCommandPool[VulkanCommandPoolThreadWrapper::GetInst().m_GraphicThreadId] = std::move(pool);
125 }
126
127 return m_ThreadGraphicCommandPool[VulkanCommandPoolThreadWrapper::GetInst().m_GraphicThreadId];
128 }
129
130 VkCommandPool& VulkanCommandPool::GetThreadComputeCommandPool()
131 {
133
134 std::unique_lock<std::mutex> lock(m_ComputeCommandPoolMutex);
135
136 if (!m_IsPoolActive)
137 {
138 SPICES_CORE_ERROR("CommandPool is not active.")
139 }
140
142 {
143 for (int i = 0; i < m_ThreadComputeCommandPool.size(); i++)
144 {
145 if (!m_ThreadComputeCommandPool[i]) VulkanCommandPoolThreadWrapper::GetInst().m_ComputeThreadId = i;
146 }
147
149 {
150 VulkanCommandPoolThreadWrapper::GetInst().m_ComputeThreadId = m_ThreadComputeCommandPool.size();
151 m_ThreadComputeCommandPool.push_back(nullptr);
152 }
153 }
154
155 if(m_ThreadComputeCommandPool[VulkanCommandPoolThreadWrapper::GetInst().m_ComputeThreadId])
156 {
157 /**
158 * @brief Instanced a VkCommandPoolCreateInfo with default value.
159 */
160 VkCommandPoolCreateInfo poolInfo{};
161 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
162 poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
163 poolInfo.queueFamilyIndex = VulkanRenderBackend::GetState().m_ComputeQueueFamily;
164
165 /**
166 * @brief Create commandpool and set it global.
167 */
168 VkCommandPool pool;
169 VK_CHECK(vkCreateCommandPool(VulkanRenderBackend::GetState().m_Device, &poolInfo, nullptr, &pool))
170 DEBUGUTILS_SETOBJECTNAME(VK_OBJECT_TYPE_COMMAND_POOL, reinterpret_cast<uint64_t>(pool), VulkanRenderBackend::GetState().m_Device, "ThreadComputeCommandPool")
171 m_ThreadComputeCommandPool[VulkanCommandPoolThreadWrapper::GetInst().m_ComputeThreadId] = std::move(pool);
172 }
173
174 return m_ThreadComputeCommandPool[VulkanCommandPoolThreadWrapper::GetInst().m_ComputeThreadId];
175 }
176
178 : VulkanObject(vulkanState)
179 {
181
182 /**
183 * @brief Create VkCommandBufferAllocateInfo struct.
184 */
185 VkCommandBufferAllocateInfo allocInfo{};
186 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
187 allocInfo.commandPool = vulkanState.m_GraphicCommandPool;
188 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
189 allocInfo.commandBufferCount = MaxFrameInFlight;
190
191 /**
192 * @brief Create commandbuffer and set it global.
193 */
194 VK_CHECK(vkAllocateCommandBuffers(vulkanState.m_Device, &allocInfo, vulkanState.m_GraphicCommandBuffer.data()))
195
196 allocInfo.commandPool = vulkanState.m_ComputeCommandPool;
197 VK_CHECK(vkAllocateCommandBuffers(vulkanState.m_Device, &allocInfo, vulkanState.m_ComputeCommandBuffer.data()))
198
199 for (int i = 0; i < MaxFrameInFlight; i++)
200 {
201 DEBUGUTILS_SETOBJECTNAME(VK_OBJECT_TYPE_COMMAND_BUFFER, reinterpret_cast<uint64_t>(vulkanState.m_GraphicCommandBuffer[i]), vulkanState.m_Device, "GraphicCommandBuffer")
202 DEBUGUTILS_SETOBJECTNAME(VK_OBJECT_TYPE_COMMAND_BUFFER, reinterpret_cast<uint64_t>(vulkanState.m_ComputeCommandBuffer[i]), vulkanState.m_Device, "ComputeCommandBuffer")
203
204 SPICES_PROFILE_VK_COLLECT(vulkanState.m_GraphicCommandBuffer[i])
205 SPICES_PROFILE_VK_COLLECT(vulkanState.m_ComputeCommandBuffer[i])
206 }
207 }
208
210 {
212
214
215 if (m_GraphicThreadId >= 0)
216 {
217 auto& pool = VulkanCommandPool::GetThreadGraphicCommandPool();
218
219 if (pool)
220 {
221 vkDestroyCommandPool(VulkanRenderBackend::GetState().m_Device, pool, nullptr);
222 pool = nullptr;
223 }
224 }
225
226 if (m_ComputeThreadId >= 0)
227 {
228 auto& pool = VulkanCommandPool::GetThreadComputeCommandPool();
229
230 if (pool)
231 {
232 vkDestroyCommandPool(VulkanRenderBackend::GetState().m_Device, pool, nullptr);
233 pool = nullptr;
234 }
235 }
236 }
237
239 {
240 /**
241 * @brief Thread Unique VulkanCommandPoolThreadWrapper.
242 */
243 static _declspec(thread) VulkanCommandPoolThreadWrapper pTLSVulkanCommandPool;
244
245 return pTLSVulkanCommandPool;
246 }
247}
#define SPICES_PROFILE_ZONE
#define SPICES_PROFILE_VK_COLLECT(cmdbuf)
#define VK_CHECK(expr)
Vulkan Check macro. Verify Vulkan API Effectiveness.
Definition VulkanUtils.h:68
VulkanCommandBuffer(VulkanState &vulkanState)
Constructor Function. Create VkCommandBuffer.
VulkanCommandBuffer Class. This class defines the VulkanCommandBuffer behaves. This class is just a w...
int m_GraphicThreadId
Thread Unique Graphic ThreadId.
int m_ComputeThreadId
Thread Unique Compute ThreadId.
static VulkanCommandPoolThreadWrapper & GetInst()
Get this instance.
virtual ~VulkanCommandPoolThreadWrapper()
Destructor Function.
Wrapper of Instance/Delete VkCommandPool in thread.
static std::vector< VkCommandPool > m_ThreadGraphicCommandPool
Thread Graphic VkCommandPool map.
static bool m_IsPoolActive
True if this Pool is actived.
VulkanCommandPool(VulkanState &vulkanState)
Constructor Function. Create VkCommandPool.
virtual ~VulkanCommandPool() override
Destructor Function.
static std::mutex m_ComputeCommandPoolMutex
Mutex for GraphicCommandPool.
static std::mutex m_GraphicCommandPoolMutex
Mutex for GraphicCommandPool.
static std::vector< VkCommandPool > m_ThreadComputeCommandPool
Thread Compute VkCommandPool map.
VulkanCommandPool Class. This class defines the VulkanCommandPool behaves. This class is just a wrapp...
VulkanObject(VulkanState &vulkanState)
Constructor Function. Init member variables.
VulkanObject Class. This class defines the basic behaves of VulkanObject. When we create an new Vulka...
constexpr uint32_t MaxFrameInFlight
Max In Flight Frame. 2 buffers are enough in this program.
Definition VulkanUtils.h:22
This struct contains all Vulkan object in used global.
Definition VulkanUtils.h:74