SpiecsEngine
 
Loading...
Searching...
No Matches
VulkanCommandBuffer.h
Go to the documentation of this file.
1/**
2* @file VulkanCommandBuffer.h.
3* @brief The VulkanCommandPool Class and VulkanCommandBuffer Class Definitions.
4* @author Spices.
5*/
6
7#pragma once
8#include "Core/Core.h"
9#include "VulkanUtils.h"
11
12namespace Spices {
13
14 /**
15 * @brief VulkanCommandPool Class.
16 * This class defines the VulkanCommandPool behaves.
17 * This class is just a wrapper of VkCommandPool.
18 */
20 {
21 public:
22
23 /**
24 * @brief Constructor Function.
25 * Create VkCommandPool.
26 * @param[in] vulkanState The global VulkanState.
27 */
28 VulkanCommandPool(VulkanState& vulkanState);
29
30 /**
31 * @brief Destructor Function.
32 */
33 virtual ~VulkanCommandPool() override;
34
35 /**
36 * @brief Get Thread Graphic VkCommandPool by thread id.
37 * @return Returns thread VkCommandPool.
38 */
40
41 /**
42 * @brief Get Thread Compute VkCommandPool by thread id.
43 * @return Returns thread VkCommandPool.
44 */
46
47 /**
48 * @brief Thread Graphic VkCommandPool map.
49 */
51
52 /**
53 * @brief Thread Compute VkCommandPool map.
54 */
56
57 /**
58 * @brief True if this Pool is actived.
59 */
60 static bool m_IsPoolActive;
61
62 /**
63 * @brief Mutex for GraphicCommandPool.
64 */
65 static std::mutex m_GraphicCommandPoolMutex;
66
67 /**
68 * @brief Mutex for GraphicCommandPool.
69 */
70 static std::mutex m_ComputeCommandPoolMutex;
71 };
72
73 /**
74 * @brief Wrapper of Instance/Delete VkCommandPool in thread.
75 */
77 {
78 public:
79
80 /**
81 * @brief Constructor Function.
82 */
86 {}
87
88 /**
89 * @brief Destructor Function.
90 */
92
93 /**
94 * @brief Get this instance.
95 * @return Return this instance.
96 */
98
99 public:
100
101 /**
102 * @brief Thread Unique Graphic ThreadId.
103 */
105
106 /**
107 * @brief Thread Unique Compute ThreadId.
108 */
110 };
111
112 /**
113 * @brief VulkanCommandBuffer Class.
114 * This class defines the VulkanCommandBuffer behaves.
115 * This class is just a wrapper of VkCommandBuffer.
116 */
118 {
119 public:
120
121 /**
122 * @brief Constructor Function.
123 * Create VkCommandBuffer.
124 * @param[in] vulkanState The global VulkanState.
125 */
126 VulkanCommandBuffer(VulkanState& vulkanState);
127
128 /**
129 * @brief Destructor Function.
130 * VkCommandBuffer is created by VkCommandPool, we do not need destroy it here manually.
131 */
132 virtual ~VulkanCommandBuffer() override = default;
133
134 /**
135 * @brief Create a new command buffer and record custom cmd, submit to graphic queue, execute it immediately.
136 * @param[in] func The function pointer of what cmd need to execute.
137 * @param[in] vulkanState The global VulkanState.
138 */
139 template<typename T>
140 static void CustomGraphicCmd(VulkanState& vulkanState, T func);
141 };
142
143 template<typename T>
144 inline void VulkanCommandBuffer::CustomGraphicCmd(VulkanState& vulkanState, T func)
145 {
147
148 /**
149 * @brief Instanced a VkCommandBufferAllocateInfo with default value.
150 */
151 VkCommandBufferAllocateInfo allocInfo{};
152 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
153 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
154 allocInfo.commandPool = VulkanCommandPool::GetThreadGraphicCommandPool();
155 allocInfo.commandBufferCount = 1;
156
157 /**
158 * @brief Allocate s new CommandBuffer from CommandPool.
159 */
160 VkCommandBuffer commandBuffer;
161 VK_CHECK(vkAllocateCommandBuffers(vulkanState.m_Device, &allocInfo, &commandBuffer));
162 DEBUGUTILS_SETOBJECTNAME(VK_OBJECT_TYPE_COMMAND_BUFFER, reinterpret_cast<uint64_t>(commandBuffer), vulkanState.m_Device, "CustomCmd Command Buffer")
163
164 /**
165 * @brief Instanced a VkCommandBufferBeginInfo with default value.
166 */
167 VkCommandBufferBeginInfo beginInfo{};
168 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
169 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
170
171 /**
172 * @brief Begin a CommandBuffer
173 */
174 VK_CHECK(vkBeginCommandBuffer(commandBuffer, &beginInfo));
175
176 SPICES_PROFILE_VK_ZONE(commandBuffer, "CustomCmd Command Buffer")
177
178 /**
179 * @brief Execute function pointer.
180 */
181 func(commandBuffer);
182
183 /**
184 * @brief End a CommandBuffer
185 */
186 VK_CHECK(vkEndCommandBuffer(commandBuffer));
187
188 /**
189 * @brief Fetch valid Graphic Queue.
190 */
192
193 /**
194 * @brief Submit commandBuffer in queue.
195 */
196 queue->Submit(commandBuffer);
197
198 /**
199 * @brief Wait queue finished.
200 */
201 queue->Wait();
202
203 /**
204 * @brief Push back to Graphic queue.
205 */
206 VulkanThreadQueue::PushToGraphic(queue);
207
208 /**
209 * @brief Free the CommandBuffer that created.
210 */
211 vkFreeCommandBuffers(vulkanState.m_Device, allocInfo.commandPool, 1, &commandBuffer);
212 }
213}
#define EVENT_CLASS_TYPE(type)
Defines Event type.
Definition Event.h:74
#define EVENT_CLASS_CATEGORY(category)
Defines Event category.
Definition Event.h:82
#define SPICES_PROFILE_ZONEN(...)
#define SPICES_PROFILE_ZONE
#define SPICES_PROFILE_VK_ZONE(cmdbuf, name)
#define SPICES_PROFILE_FRAME
#define VK_CHECK(expr)
Vulkan Check macro. Verify Vulkan API Effectiveness.
Definition VulkanUtils.h:68
virtual ~Application()
Destructor Function.
Application()
Constructor Function.
static void Run()
Run Our World.
Application Class. Our Engine Start here.
Definition Application.h:20
This Class is the basic Event Class. Inherit from it and create specific event class.
Definition Event.h:96
uint32_t m_PickMaterial
Identify of selected material.
Definition FrameInfo.h:89
FrameInfo(const FrameInfo &)=delete
Copy Constructor Function.
uint32_t m_FrameIndex
FrameIndex, varying during 0 - (MaxFrameInFlight - 1). Used almost anywhere.
Definition FrameInfo.h:69
FrameInfo & operator=(const FrameInfo &)=delete
Copy Assignment Operation.
virtual ~FrameInfo()=default
Destructor Function.
static FrameInfo & Get()
Get FrameInfo.
Definition FrameInfo.cpp:14
std::shared_ptr< World > m_World
The shared pointer of specific world.
Definition FrameInfo.h:94
scl::linked_unordered_map< int, std::string > m_PickEntityID
Definition FrameInfo.h:82
RendererType m_RendererType
The renderer type of current world.
Definition FrameInfo.h:99
FrameInfo()=default
Constructor Function.
uint32_t m_ImageIndex
ImageIndex, varying during 0 - (MaxFrameInFlight - 1). Used in swapchain index and framebuffer index.
Definition FrameInfo.h:75
FrameInfo Class. This class defines the FrameInfo data.
Definition FrameInfo.h:32
static void Init()
Init Log.
Definition Log.cpp:24
static void ShutDown()
Shutdown Log.
Definition Log.cpp:82
virtual std::string ToString() const override
Serialize this Event Class to string.
Definition WorldEvent.h:60
MeshAddedWorldEvent()=default
Constructor Function.
virtual ~MeshAddedWorldEvent() override=default
Destructor Function.
This Class is inherited from Event Class.
Definition WorldEvent.h:43
NativeScriptSystem Class. This class defines the specific behaves of NativeScriptSystem.
ResourceSystem Class. This class defines the specific behaves of RenderSystem.
RendererResourcePool Class. This class is a pool of all framebuffer's attachment.
ResourceSystem Class. Handles resource load/unload event.
This Class is inherited from Event Class. Called by Viewport Resize.
Definition SlateEvent.h:18
SlateSystem Class. This class defines the specific behaves of SlateSystem.
Definition SlateSystem.h:21
SystemManager()
Constructor Function.
static SystemManager & Get()
Get Static SystemManager.
SystemManager & PopSystem(const std::string &systemName)
Push a system to this manager.
SystemManager Class. This class defines the behave of SystemManager.
void Flush()
Refresh time in each engine loop.
Definition TimeStep.cpp:26
const uint64_t & fs() const
Get frames count.
Definition TimeStep.h:63
This Class handles our engine time step during frames. Global Unique.
Definition TimeStep.h:22
virtual ~VulkanCommandBuffer() override=default
Destructor Function. VkCommandBuffer is created by VkCommandPool, we do not need destroy it here manu...
VulkanCommandBuffer(VulkanState &vulkanState)
Constructor Function. Create VkCommandBuffer.
static void CustomGraphicCmd(VulkanState &vulkanState, T func)
Create a new command buffer and record custom cmd, submit to graphic queue, execute it immediately.
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.
VulkanCommandPoolThreadWrapper()
Constructor Function.
Wrapper of Instance/Delete VkCommandPool in thread.
static std::vector< VkCommandPool > m_ThreadGraphicCommandPool
Thread Graphic VkCommandPool map.
static VkCommandPool & GetThreadComputeCommandPool()
Get Thread Compute VkCommandPool by thread id.
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 VkCommandPool & GetThreadGraphicCommandPool()
Get Thread Graphic VkCommandPool by thread id.
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...
VulkanDescriptorPool Class. This class is the wrapper of VkDescriptorPool.
VkSampleCountFlagBits GetMaxUsableSampleCount() const
Get device's max usable sample count.
bool IsQueueMeetDemand(const VkPhysicalDevice &device, const VkSurfaceKHR &surface)
Check all Queue we need meet;.
static SwapChainSupportDetails QuerySwapChainSupport(const VkPhysicalDevice &device, const VkSurfaceKHR &surface, GLFWwindow *window)
Query physical device's SwapChainSupport.
const SwapChainSupportDetails & GetSwapChainSupport()
Get SwapChain Utils.
static VkPhysicalDeviceDeviceGeneratedCommandsPropertiesNV & GetDGCProperties()
Get DeviceGeneratedCommandsPropertiesNV.
QueueHelper m_QueueHelper
QueueHelper.
static VkPhysicalDeviceFeatures m_DeviceFeatures
Selected Physical Device Features.
static VkPhysicalDeviceProperties & GetDeviceProperties()
Get VkPhysicalDeviceProperties.
virtual ~VulkanDevice() override
Destructor Function.
static VkPhysicalDeviceRayTracingPipelinePropertiesKHR m_RayTracingProperties
Device RayTracing Properties.
static VkPhysicalDeviceFeatures & GetDeviceFeatures()
Get VkPhysicalDeviceFeatures.
bool IsPropertyMeetDemand(const VkPhysicalDevice &device)
Check all Property we need meet.
static VkPhysicalDeviceRayTracingPipelinePropertiesKHR & GetRTPipelineProperties()
Get RayTracingPipelineProperties.
bool IsFeatureMeetDemand(const VkPhysicalDevice &device)
Check all Feature we need meet.
void GetExtensionRequirements()
Get all physical device extension requirements our engine needed. Source 1 : user Setting.
static VkPhysicalDeviceProperties m_DeviceProperties
Selected Physical Device Properties.
static VkPhysicalDeviceDeviceGeneratedCommandsPropertiesNV m_DGCProperties
Device DGC Properties.
VulkanDevice(VulkanState &vulkanState)
Constructor Function. Create vkDevice.
bool SelectPhysicalDevice(const VkInstance &instance, const VkSurfaceKHR &surface, GLFWwindow *window)
select a suitable physical device.
SwapChainSupportDetails m_SwapChainSupportDetails
SwapChainSupportDetails.
bool IsExtensionMeetDemand(const VkPhysicalDevice &device)
Check all Extension we need meet;.
std::vector< const char * > m_ExtensionProperties
Device Extension Properties.
void RequerySwapChainSupport()
Requery device's SwapChainSupportDetails. Mainly VkExtent2D.
const QueueHelper & GetQueueHelper() const
Get QueueHelper variable.
VulkanInstance Class. This class defines the VulkanDevice behave. This class is just a wrapper of vkd...
void SetVulkanDebugCallbackFuncPointer()
Set Vulkan's debug message callback function pointer. Working with DEBUG mode.
virtual ~VulkanInstance() override
Destructor Function.
VkDebugUtilsMessengerCreateInfoEXT m_DebugMessengerCreateInfo
Debug Utils Messages used to execute message callback function. Also debug vkInstance create.
void CreateVulkanSurface() const
Create a Surface Object.
std::vector< const char * > m_LayerProperties
Instance Layer Properties.
VulkanInstance(VulkanState &vulkanState, const std::string &name, const std::string &engineName)
Constructor Function. Create vkInstance and vkSurface.
std::vector< const char * > m_ExtensionProperties
Instance Extension Properties.
bool CheckExtensionRequirementsSatisfied()
Iter all our extensions, check whether all satisfied or not.
VkDebugUtilsMessengerEXT m_DebugMessenger
Parameter for Create/Destroy DebugUtilsMessengerEXT.
void FillDebugMessengerCreateInfo()
Set m_DebugMessengerCreateInfo variable.
void GetExtensionRequirements()
Get all instance extension requirements our engine needed. Source 1 : glfw requirements....
bool ChecklayerRequirementsSatisfied()
Iter all our layers, check whether all satisfied or not.
void GetLayerRequirements()
Get all instance layer requirements our engine needed. Source 1 : user Setting.
VulkanInstance Class. This class defines the VulkanInstance behaves. This class is just a wrapper of ...
virtual ~VulkanMemoryAllocator() override
Destructor Function.
VulkanMemoryAllocator(VulkanState &vulkanState)
Constructor Function. Create Specific ThreadPool.
VulkanObject Class. This class defines the basic behaves of VulkanObject. When we create an new Vulka...
void EndFrame(FrameInfo &frameInfo)
End record the frame with vulkan render backend.
void RecreateSwapChain()
Called OnSlateResize.
std::shared_ptr< VulkanMemoryAllocator > m_VmaAllocator
VulkanMemoryAllocator.
std::unique_ptr< VulkanSwapChain > m_VulkanSwapChain
VulkanSwapChain.
virtual ~VulkanRenderBackend()
Destructor Function.
std::unique_ptr< VulkanCommandPool > m_VulkanCommandPool
VulkanCommandPool.
VulkanRenderBackend()
Constructor Function.
static std::shared_ptr< VulkanDescriptorPool > GetDescriptorPool()
Get DescriptorPool in use.
static VulkanState m_VulkanState
The VulkanState in use.
std::unique_ptr< VulkanCommandBuffer > m_VulkanCommandBuffer
VulkanCommandBuffer.
bool isWindowClosed()
Determine whether window is closed.
static std::shared_ptr< VulkanDescriptorPool > m_VulkanDescriptorPool
The VulkanDescriptorPool in use.
static std::shared_ptr< RendererResourcePool > m_RendererResourcePool
The RendererResourcePool in use.
static VulkanState & GetState()
Get VulkanState in use.
std::unique_ptr< VulkanWindows > m_VulkanWindows
VulkanWindows.
bool OnMeshAddedWorldEvent(WorldEvent &event)
This function is called on world mark query tick.
void RenderFrame(TimeStep &ts, FrameInfo &frameInfo)
Draw World.
void OnEvent(Event &event)
This function is called on global event function is called.
static std::shared_ptr< RendererResourcePool > GetRendererResourcePool()
Get RendererResourcePool in use.
void BeginFrame(FrameInfo &frameInfo)
Start record a new frame with vulkan render backend.
VulkanRenderBackend & operator=(const VulkanRenderBackend &)=delete
Copy Assignment Operation.
bool OnWindowResizeOver(WindowResizeOverEvent &event)
This function is called on window is resized over.
std::shared_ptr< VulkanDevice > m_VulkanDevice
VulkanDevice.
std::unique_ptr< VulkanInstance > m_VulkanInstance
VulkanInstance.
bool OnSlateResize(SlateResizeEvent &event)
This function is called on viewport is resized.
VulkanRenderBackend(const VulkanRenderBackend &)=delete
Copy Constructor Function.
This class defines the render backend behaves of Vulkan.
static std::shared_ptr< VulkanThreadQueue > FetchGraphicQueue()
Fetch valid Graphic Queue.
VulkanThreadQueue Class. This class is a wrapper of Thread VkQueue.
VulkanWindows(VulkanState &vulkanState, const WindowInfo &initInfo)
Constructor Function. Create Windows.
bool m_WindowsResized
True if viewPort is resized.
virtual ~VulkanWindows() override
Destructor Function.
void SetInternalCallBack() const
Set all needed GLFW events call back.
void SetResized(bool isResized)
Set m_WindowsResized variable.
WindowInfo m_WindowInfo
Window's info. not viewport's info.
bool IsResized() const
Get m_WindowsResized variable.
VulkanWindows Class. This class defines the windows behaves.
virtual ~WorldEvent() override=default
Destructor Function.
WorldEvent()=default
Constructor Function.
This Class is inherited from Event Class. Inherit from it and create specific KeyEvent class....
Definition WorldEvent.h:20
World Class. This class defines the basic behaves of World. When we create an new world,...
Definition World.h:41
The container combines hashmap and list together. Used in the case that we want iter a hashmap in ord...
VMAMemoryPropertyFlagExtendBits
Map to VmaAllocationCreateFlagBits while use VMA for memory create.
@ VMA_MEMORY_PROPERTY_DEDICATED_MEMORY_BIT
VkMemoryPropertyFlagBits.
@ EventCategoryWorld
Definition Event.h:68
@ MeshAdded
World Event.
RendererType
Definition FrameInfo.h:22
std::optional< uint32_t > graphicqueuefamily
The graphic queue's in used identify.
std::optional< uint32_t > presentqueuefamily
The present queue's in used identify.
std::optional< uint32_t > transferqueuefamily
The transfer queue's in used identify.
std::optional< uint32_t > computequeuefamily
The compute queue's in used identify.
bool isComplete() const
Whether all queues that we need is valid.
This struct contains all queues's identify we need.
VkExtent2D surfaceSize
The VkSurface Size.
std::vector< VkSurfaceFormatKHR > formats
All supported VkSurfaceFormatKHR.
VkSurfaceFormatKHR format
The selected VkSurfaceFormatKHR.
VkSurfaceCapabilitiesKHR capabilities
VkSurfaceCapabilitiesKHR.
VkPresentModeKHR presentMode
The selected VkPresentModeKHR.
VkExtent2D viewPortSize
The ViewPort Size. Init value. Reset by resize event.
std::vector< VkPresentModeKHR > presentModes
All supported VkPresentModeKHR.
SwapChain Utils. Queried from device.
This struct contains all Vulkan object in used global.
Definition VulkanUtils.h:74
GLFWimage image
GLFWimage.
WindowIcon(const std::string &iconPath)
Constructor Function. Load the icon file immediately.
virtual ~WindowIcon()
Destructor Function.
This struct helps load the icon of window.
std::shared_ptr< WindowIcon > icon
Window's icon.
std::string name
Window's name.
int width
Window's width.
int height
Window's height.
This struct defines the basic information of window.