SpiecsEngine
 
Loading...
Searching...
No Matches
ThreadQueue.h
Go to the documentation of this file.
1/**
2* @file ThreadQueue.h.
3* @brief The thread_queue Class Definitions.
4* @author Spices.
5*/
6
7#pragma once
8#include "Core/Core.h"
9
10namespace scl {
11
12 /**
13 * @brief Thread safe Queue.
14 */
15 template<typename T>
17 {
18 public:
19
20 /**
21 * @brief Constructor Function.
22 */
24
25 /**
26 * @brief Destructor Function.
27 */
28 virtual ~thread_queue() = default;
29
30 /**
31 * @brief Push a item to this queue.
32 * @param[in] item Queue item.
33 */
34 void Push(T&& item);
35
36 /**
37 * @brief Pop a item from this queue.
38 * @return Returns item.
39 */
40 T Pop();
41
42 /**
43 * @brief Is this queue is empty.
44 * @reutrn Returns true if empty.
45 */
46 bool IsEmpty() const { return m_Count.load() == 0; }
47
48 /**
49 * @brief Clear this queue.
50 */
51 void Clear();
52
53 private:
54
55 /**
56 * @brief Mutex of this queue.
57 */
58 std::mutex m_Mutex;
59
60 /**
61 * @brief Not empty condition.
62 */
63 std::condition_variable m_NotEmpty;
64
65 /**
66 * @brief Count of tasks.
67 */
68 std::atomic_int m_Count;
69
70 /**
71 * @brief This wrapped queue.
72 */
74 };
75
76 template<typename T>
77 void thread_queue<T>::Push(T&& item)
78 {
79 std::unique_lock<std::mutex> lock(m_Mutex);
80
81 m_Queue.push(item);
82 ++m_Count;
83
84 m_NotEmpty.notify_all();
85 }
86
87 template<typename T>
88 inline T thread_queue<T>::Pop()
89 {
90 std::unique_lock<std::mutex> lock(m_Mutex);
91
92 if (IsEmpty())
93 m_NotEmpty.wait(lock, [&](){ return !IsEmpty(); });
94
95 auto ptr = m_Queue.front();
96 m_Queue.pop();
97 --m_Count;
98
99 return std::move(ptr);
100 }
101
102 template<typename T>
103 inline void thread_queue<T>::Clear()
104 {
105 std::unique_lock<std::mutex> lock(m_Mutex);
106
107 std::queue<T>().swap(m_Queue);
108 m_Count = 0;
109 }
110}
#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 void Destroy()
Destroy all ThreadQueue.
static scl::thread_queue< std::shared_ptr< VulkanThreadQueue > > m_ComputeQueues
Compute VulkanThreadQueue.
static void CreateGraphic(VulkanState &vulkanState, VkQueue queue)
Create Graphic VulkanThreadQueue.
VkQueue m_Queue
This Thread VkQueue.
void Submit(VkCommandBuffer commandBuffer) const
Submit the CommandBuffer in this Queue.
void Wait() const
Wait for queue execute.
static void PushToGraphic(std::shared_ptr< VulkanThreadQueue > &queue)
Push queue to Graphic ThreadQueue.
static std::shared_ptr< VulkanThreadQueue > FetchGraphicQueue()
Fetch valid Graphic Queue.
static std::shared_ptr< VulkanThreadQueue > FetchComputeQueue()
Fetch valid Compute Queue.
static void CreateCompute(VulkanState &vulkanState, VkQueue queue)
Create Compute VulkanThreadQueue.
virtual ~VulkanThreadQueue() override
Destructor Function.
static void PushToCompute(std::shared_ptr< VulkanThreadQueue > &queue)
Push queue to Compute ThreadQueue.
static scl::thread_queue< std::shared_ptr< VulkanThreadQueue > > m_GraphicQueues
Graphic VulkanThreadQueue.
VulkanThreadQueue(VulkanState &vulkanState, VkQueue queue)
Constructor Function.
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...
void Push(T &&item)
Push a item to this queue.
Definition ThreadQueue.h:77
std::mutex m_Mutex
Mutex of this queue.
Definition ThreadQueue.h:58
thread_queue()
Constructor Function.
Definition ThreadQueue.h:23
T Pop()
Pop a item from this queue.
Definition ThreadQueue.h:88
void Clear()
Clear this queue.
std::atomic_int m_Count
Count of tasks.
Definition ThreadQueue.h:68
std::condition_variable m_NotEmpty
Not empty condition.
Definition ThreadQueue.h:63
bool IsEmpty() const
Is this queue is empty. @reutrn Returns true if empty.
Definition ThreadQueue.h:46
virtual ~thread_queue()=default
Destructor Function.
std::queue< T > m_Queue
This wrapped queue.
Definition ThreadQueue.h:73
Thread safe Queue.
Definition ThreadQueue.h:17
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.
static constexpr int NThreadQueue
Thread Queue Count.
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.