UraniumCompute 0.1.0
A GPU accelerated parallel task scheduler
VulkanComputeDevice.h
1#pragma once
2#include <UnCompute/Backend/IComputeDevice.h>
3#include <UnCompute/Memory/Ptr.h>
4#include <UnCompute/VulkanBackend/VulkanInclude.h>
5
6namespace UN
7{
9 {
10 UInt32 FamilyIndex;
11 UInt32 QueueCount;
12 HardwareQueueKindFlags KindFlags;
13 VkCommandPool CmdPool = VK_NULL_HANDLE;
14
15 inline VulkanQueueFamily(UInt32 familyIndex, UInt32 queueCount, HardwareQueueKindFlags kindFlags)
16 : FamilyIndex(familyIndex)
17 , QueueCount(queueCount)
18 , KindFlags(kindFlags)
19 {
20 }
21 };
22
25
26 class VulkanComputeDevice : public Object<IComputeDevice>
27 {
28 Ptr<VulkanDeviceFactory> m_pFactory;
29
30 std::vector<VulkanQueueFamily> m_QueueFamilies;
31
32 VkDevice m_NativeDevice = VK_NULL_HANDLE;
33 VkPhysicalDevice m_NativeAdapter = VK_NULL_HANDLE;
34
35 Ptr<VulkanDescriptorAllocator> m_pDescriptorAllocator;
36
37 void ResetInternal();
38 void FindQueueFamilies();
39
40 public:
42
43 explicit VulkanComputeDevice(VulkanDeviceFactory* pInstance);
44 ~VulkanComputeDevice() override;
45
46 ResultCode Init(const DescriptorType& desc) override;
47 void Reset() override;
48
49 inline VkCommandPool GetCommandPool(UInt32 queueFamilyIndex)
50 {
51 for (auto& queue : m_QueueFamilies)
52 {
53 if (queue.FamilyIndex == queueFamilyIndex)
54 {
55 return queue.CmdPool;
56 }
57 }
58
59 UN_Verify(false, "Couldn't find command pool");
60 return VK_NULL_HANDLE;
61 }
62
63 inline UInt32 GetQueueFamilyIndex(HardwareQueueKindFlags flags)
64 {
65 for (auto& queue : m_QueueFamilies)
66 {
67 if (AllFlagsActive(queue.KindFlags, flags))
68 {
69 return queue.FamilyIndex;
70 }
71 }
72
73 UN_Verify(false, "Couldn't find queue family");
74 return std::numeric_limits<UInt32>::max();
75 }
76
77 inline VkQueue GetDeviceQueue(UInt32 queueFamilyIndex, UInt32 queueIndex)
78 {
79 VkQueue queue;
80 vkGetDeviceQueue(m_NativeDevice, queueFamilyIndex, queueIndex, &queue);
81 return queue;
82 }
83
84 inline VkQueue GetDeviceQueue(HardwareQueueKindFlags flags)
85 {
86 VkQueue queue;
87 vkGetDeviceQueue(m_NativeDevice, GetQueueFamilyIndex(flags), 0, &queue);
88 return queue;
89 }
90
91 inline VulkanDescriptorAllocator* GetDescriptorAllocator()
92 {
93 return m_pDescriptorAllocator.Get();
94 }
95
96 ResultCode FindMemoryType(UInt32 typeBits, VkMemoryPropertyFlags properties, UInt32& memoryType);
97
98 [[nodiscard]] inline VkDevice GetNativeDevice() const
99 {
100 return m_NativeDevice;
101 }
102
103 ResultCode CreateBuffer(IBuffer** ppBuffer) override;
104 ResultCode CreateMemory(IDeviceMemory** ppMemory) override;
105 ResultCode CreateFence(IFence** ppFence) override;
106 ResultCode CreateCommandList(ICommandList** ppCommandList) override;
107 ResultCode CreateResourceBinding(IResourceBinding** ppResourceBinding) override;
108 ResultCode CreateKernel(IKernel** ppKernel) override;
109
110 static ResultCode Create(VulkanDeviceFactory* pInstance, VulkanComputeDevice** ppDevice);
111 };
112} // namespace UN
An interface for backend-specific buffers that store the data on the device.
Definition: IBuffer.h:26
An interface for command lists that record commands to be executed by the backend.
Definition: ICommandList.h:180
This class holds a handle to backend-specific memory.
Definition: IDeviceMemory.h:31
An interface for fences - synchronization primitives that can be either signaled or reset.
Definition: IFence.h:31
An interface for compute kernel - a program running on the device.
Definition: IKernel.h:29
Resource binding object used to bind resources to a compute kernel.
Definition: IResourceBinding.h:51
Base class for dynamic reference counted objects.
Definition: Object.h:51
Shared smart pointer implementation that uses reference counting.
Definition: Ptr.h:44
T * Get() const
Get underlying raw pointer.
Definition: Ptr.h:219
Definition: VulkanComputeDevice.h:27
ResultCode Init(const DescriptorType &desc) override
Initializes the compute device with the provided descriptor.
Definition: VulkanComputeDevice.cpp:80
void Reset() override
Reset the object to uninitialized state.
Definition: VulkanComputeDevice.cpp:177
Definition: VulkanDescriptorAllocator.h:25
This class holds a Vulkan API instance.
Definition: VulkanDeviceFactory.h:13
Compute device descriptor.
Definition: IComputeDevice.h:9
Definition: VulkanComputeDevice.h:9