HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VE_Device.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: VE_Device.h ( VE Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __VE_DEVICE_H__
12 #define __VE_DEVICE_H__
13 
14 #include "VE_API.h"
15 #include "VE_Ext.h"
16 #include "VE_Instance.h"
17 #include "VE_Memory.h"
18 #include "VE_PhysicalDevice.h"
19 #include "VE_Result.h"
20 
21 #include <SYS/SYS_Handle.h>
22 
27 };
28 
30 {
31  VkBuffer buffer;
32  VkDeviceMemory memory;
34 };
35 
36 using VE_QueueFlags = uint32_t;
37 
38 /// This class contains a VkDevice handle along with the objects that are
39 /// involved with the creation of it. Upon instantiation, it creates the
40 /// VkInstance, finds a suitable physical device, and creates the logical
41 /// VkDevice from that along queue objects. It
42 /// cleans up everything upon calling destroy() on it.
43 ///
44 /// It therefore forms a single convenient point interface into the Vulkan API,
45 /// and can form the bedrock of a higher level abstraction.
46 ///
47 /// Keep in mind the size of the object. Heap allocation is suggested.
49 {
50  VE_PhysicalDevice myPhysicalDevice;
51  VE_Instance myInstance;
52  VE_Ext myExt;
53  VkDevice myHandle = VK_NULL_HANDLE;
54 
55  static constexpr int UNIVERSAL_QUEUE_BITS = (VE_QUEUE_GRAPHICS_BIT | VE_QUEUE_COMPUTE_BIT | VE_QUEUE_TRANSFER_BIT);
56  static constexpr int MAX_QUEUE_TYPES = UNIVERSAL_QUEUE_BITS + 1;
57 
58  /// From the spec, describing VkQueueFlagBits:
59  ///
60  /// " If an implementation exposes any queue family that supports graphics
61  /// operations, at least one queue family of at least one physical device
62  /// exposed by the implementation must support both graphics and compute
63  /// operations. Furthermore, if the protected memory physical device feature is
64  /// supported, then at least one queue family of at least one physical device
65  /// exposed by the implementation must support graphics operations, compute
66  /// operations, and protected memory operations.
67  ///
68  /// Note: All commands that are allowed on a queue that supports
69  /// transfer operations are also allowed on a queue that supports either
70  /// graphics or compute operations. Thus, if the capabilities of a queue family
71  /// include VK_QUEUE_GRAPHICS_BIT or VK_QUEUE_COMPUTE_BIT, then reporting the
72  /// VK_QUEUE_TRANSFER_BIT capability separately for that queue family is
73  /// optional. "
74  /// - Vulkan Spec. Page 108 | Chapter 5. Devices and Queues
75 
76  struct QueueFamilies {
77  // Use a VE_QueueBits to index into this to retrieve the queue family index.
78  // A -1 means there is no queue family satifying those bit flags
79  int indices[MAX_QUEUE_TYPES];
80  } myQueueFamilies;
81 
82  VE_Device(
83  VE_Instance inst,
84  VE_PhysicalDevice phy_dev,
85  VkDevice device,
86  QueueFamilies queue_families,
87  uint32_t enabled_extension_count,
88  const char *const *enabled_extensions)
89  : myPhysicalDevice(std::move(phy_dev))
90  , myInstance(std::move(inst))
91  , myHandle(device)
92  , myQueueFamilies(queue_families)
93  {
94  myExt.loadInstanceExtensionFunctions(
95  myInstance.handle(), myInstance.enabledExtensions());
96  myExt.loadDeviceExtensionFunctions(
97  device, enabled_extension_count, enabled_extensions);
98  }
99 
100 public:
101  VE_Device() = default;
102 
103  static VE_Result<VE_Device>
104  create(
105  const VE_Instance &instance,
106  const VE_PhysicalDevice &physical_device,
107  VE_PhysicalDeviceFeatures device_features,
108  uint32_t device_extension_count,
109  const VE_DeviceExtension device_extensions[]);
110 
111  void destroy();
112 
113  VkDevice handle() const { return myHandle; }
114 
115  const VE_Ext &ext() const { return myExt; }
116 
117  /// TODO make sure this is available
118  /// Return a queue that supports graphics, compute, and transfer operations.
119  VkQueue universalQueue() const;
120  uint32_t universalQueueFamilyIndex() const;
121 
122  /// TODO make sure this is available
123  /// Return a queue that supports graphics operations.
124  VkQueue graphicsQueue() const;
125  uint32_t graphicsQueueFamilyIndex() const;
126 
127  const VE_PhysicalDevice &physicalDevice() const { return myPhysicalDevice; }
128 
129  const VE_Instance &instance() const { return myInstance; }
130 
131  VE_Result<VkDeviceMemory> allocateMemory(
132  uint64_t size,
133  uint32_t memory_type_bits,
134  VkMemoryPropertyFlags mem_props,
135  bool external);
136 
137  VE_Result<VE_BufferAllocation> createAndAllocateBuffer(
140  VkMemoryPropertyFlags mem_props,
141  bool external);
142 
143  VE_Result<SYS_Handle> createExternalMemoryHandle(VkDeviceMemory memory);
144 
145  VE_Result<VkDebugUtilsMessengerEXT> setDebugCallback(
146  VkDebugUtilsMessageSeverityFlagsEXT msg_severity_flags,
147  VkDebugUtilsMessageTypeFlagsEXT msg_type_flags,
148  VkBool32 (*callback)(
151  const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
152  void *pUserData),
153  void *pUserData);
154 };
155 
156 /// NOTE: These are low-level calls and it is recommended to use a higher-level
157 /// interface to Vulkan if possible.
158 ///
159 /// Create a Vulkan logical device object. It grants access to a specific device,
160 /// which in many cases will be a dedicated GPU. Some high-level uses of the
161 /// device are listed below.
162 ///
163 /// • Creation of command queues.
164 /// • Creation and tracking of various synchronization constructs.
165 /// • Allocating, freeing, and managing memory.
166 /// • Creation and destruction of command buffers and command buffer pools.
167 /// • Creation, destruction, and management of graphics state.
168 ///
169 /// It is the caller's responsibily to pass the returned VkDevice to
170 /// ve_destroyVulkanDevice once it is no longer needed.
171 VE_API
173 /// This call assigns equal priorities to all queues.
174 VE_API
176  VkPhysicalDevice,
177  const VE_PhysicalDeviceFeatures &enabled_features,
178  uint32_t enabled_extension_count,
179  const char *const *enabled_extensions,
180  const uint32_t queue_family_count,
181  const uint32_t queue_family_indices[],
182  const uint32_t queue_counts[]);
183 /// Create a device with access to only one family of queues.
184 VE_API
186  VkPhysicalDevice,
187  const VE_PhysicalDeviceFeatures &enabled_features,
188  uint32_t enabled_extension_count,
189  const char *const *enabled_extensions,
190  uint32_t queue_family_index,
191  uint32_t queue_count);
192 /// Create a device with a single queue statisfying the given queue_flags.
193 VE_API
195  const VE_PhysicalDevice &,
196  const VE_PhysicalDeviceFeatures &enabled_features,
197  uint32_t enabled_extension_count,
198  const char *const *enabled_extensions,
199  VkQueueFlags queue_flags);
200 
201 /// Clean up a VkDevice. This likely will simply call vkDestroyDevice.
202 VE_API
203 void VEdestroyVulkanDevice(VkDevice);
204 
205 /// Will create the system specific external memory handle
206 /// for the memory passed in.
207 /// pfn should be a function pointer with signature matching either
208 /// PFN_vkGetMemoryFdKHR or PFN_vkGetMemoryWin32HandleKHR, depending on the
209 /// platrom.
210 /// memory must have been created with the
213  void *pfn,
214  VkDevice device,
215  VkDeviceMemory memory);
216 
217 #endif
GLsizei GLenum GLsizei GLsizei GLuint memory
Definition: RE_OGL.h:202
VkDeviceSize size
Definition: VE_Device.h:33
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
uint32_t VkBool32
Definition: vulkan_core.h:93
#define VK_NULL_HANDLE
Definition: vulkan_core.h:45
VkFlags VkBufferUsageFlags
Definition: vulkan_core.h:2515
VkFlags VkDebugUtilsMessageTypeFlagsEXT
const VE_Ext & ext() const
Definition: VE_Device.h:115
const VE_PhysicalDevice & physicalDevice() const
Definition: VE_Device.h:127
VE_API VE_Result< SYS_Handle > VEcreateExternalMemoryHandle(void *pfn, VkDevice device, VkDeviceMemory memory)
Definition: VE_Ext.h:24
VE_QueueFlagBits
Definition: VE_Device.h:23
VkDebugUtilsMessageSeverityFlagBitsEXT
VE_DeviceExtension
#define VE_API
Definition: VE_API.h:20
VkDevice handle() const
Definition: VE_Device.h:113
VkFlags VkDebugUtilsMessageSeverityFlagsEXT
GLsizeiptr size
Definition: glcorearb.h:664
GLsizeiptr const void GLenum usage
Definition: glcorearb.h:664
uint32_t VE_QueueFlags
Definition: VE_Device.h:36
uint64_t VkDeviceSize
Definition: vulkan_core.h:95
VkDeviceMemory memory
Definition: VE_Device.h:32
VkFlags VkQueueFlags
Definition: vulkan_core.h:2356
VE_API void VEdestroyVulkanDevice(VkDevice)
Clean up a VkDevice. This likely will simply call vkDestroyDevice.
VkFlags VkMemoryPropertyFlags
Definition: vulkan_core.h:2339
VE_API VE_Result< VkDevice > VEcreateVulkanDevice(VkPhysicalDevice, VkDeviceCreateInfo)
const VE_Instance & instance() const
Definition: VE_Device.h:129