HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VE_PhysicalDevice.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_PhysicalDevice.h ( VE Library, C++)
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __VE_PHYSICAL_DEVICE_H__
13 #define __VE_PHYSICAL_DEVICE_H__
14 
15 #include "VE_VK.h"
16 
17 #include "VE_API.h"
18 #include "VE_Result.h"
20 
21 #include <UT/UT_Array.h>
22 #include <UT/UT_ArrayStringSet.h>
23 #include <UT/UT_Optional.h>
24 
25 constexpr uint32_t VE_SWIFT_SHADER_DEVICE_ID = 0xC0DE;
26 
27 /// A enum of common device extensions. These can be used in lieu of passing
28 /// the names themselves into functions, making it a bit hard
29 /// to make typos and a bit easier to remember what extensions exist.
30 ///
31 /// To map from extension name to enum member, just remove the capitalized
32 /// prefix from the the extension name.
33 /// For example
34 /// VK_KHR_external_fence_capabilities -> external_fence_capabilities
35 enum class VE_DeviceExtension {
46  ray_query,
48 #ifdef VULKAN_PRESENTATION
49  swapchain,
50 #endif
52 
53  count, // used to mark the size of this enum
54 };
55 
56 /// This class maintains the actual Vulkan API structures used to inspect and
57 /// enable various possible device features.
59 {
60 public:
62 
66  const UT_ArrayStringSet &deviceextensions);
67  VE_PhysicalDeviceFeatureChain(VkPhysicalDevice dev,
68  const UT_ArrayStringSet &deviceextensions);
69 
71 
72  /// Returns a linked list of Vulkan structures, chained through pNext
73  /// members, that can be passed into various API calls.
74  /// This pointer must not outlive the VE_PhysicalDeviceFeatureChain instance
75  /// it was returned from.
76  const VkPhysicalDeviceFeatures2 *pFeatures() const { return &my0; }
77 
78  // Returns a filled `VE_PhysicalDeviceFeatures` struct, with the features
79  // enabled in this linked feature chain
80  VE_PhysicalDeviceFeatures featuresStruct() const;
81 
82  bool containsAll(
83  const VE_PhysicalDeviceFeatures &features,
84  UT_WorkBuffer *feature_list = nullptr) const;
85 
87  { return my2.bufferDeviceAddress; }
88 
90  { return my1.multiviewTessellationShader; }
91 
93  { return my6.accelerationStructure; }
94 
95  bool hasRayQuery() const
96  { return my4.rayQuery; }
97 
99 
100 private:
101  void linkMembers();
102  void linkMembers(const UT_ArrayStringSet &deviceextensions);
103  void linkMembers(const VE_PhysicalDeviceFeatureChain &other);
104 
112 };
113 
114 /// A wrapper around a VkPhysicalDevice object that also contains useful
115 /// information about that physical device.
116 /// Due to caching information from the driver, this object is rather hefty.
117 /// Consider allocating it on the heap.
119 {
120 public:
121  static VE_Result<VE_PhysicalDevice> create(VkPhysicalDevice);
122 
123  static VE_Result<UT_Array<VE_PhysicalDevice>> findAll(VkInstance instance);
124 
125  VkPhysicalDevice getVkPhysicalDevice() const { return myHandle; }
126 
128  {
129  return myProperties.properties;
130  }
131 
133  {
134  return myIdProperties;
135  }
136 
138  {
139  UT_ASSERT(VK_API_VERSION_MINOR(properties().apiVersion) >= 2);
140  return myProperties11;
141  }
142 
144  {
145  UT_ASSERT(VK_API_VERSION_MINOR(properties().apiVersion) >= 2);
146  return myProperties12;
147  }
148 
150  {
151  return myAccelStructProperties;
152  }
153 
154  const VE_PhysicalDeviceFeatureChain &features() const { return myFeatures; }
155 
156  bool extensionAvailable(const char *name) const;
157  bool extensionAvailable(VE_DeviceExtension ext) const;
158 
160  {
161  return myQueueFamilyProperties;
162  }
163 
165  {
166  return myMemoryProperties;
167  }
168 
169  // Print out device information. Mainly for debugging.
170  void printInfo() const;
171 
172  /// Methods for retrieving a memory type index.
173  ///
174  /// Vulkan resources can be created before any memory is actually allocated for
175  /// them. This memory-less resource handle acts as a kind of meta-data object. One
176  /// of the reasons for this is so the memory requirements for the resource can be
177  /// queried before the resource is actually allocated, allowing us to merge the
178  /// resources requirements with the applications needs and preferences.
179  ///
180  /// The resource's requirements are expressed as memory type bits. The
181  /// applications requirements or preferences are expressed as
182  /// VkMemoryPropertyFlags. You can pass both of these to these new methods to
183  /// retrieve the actually memory type index, which is all you need to actually
184  /// allocate the memory.
185  ///
186  /// Usage:
187  ///
188  /// VkMemoryRequirements reqs;
189  ///
190  /// vkGetBufferMemoryRequirements(device, buffer, &reqs);
191  ///
192  /// uint32_t memory_type_index;
193  ///
194  /// auto r = physical_device.findMemoryTypeIndex(reqs.memoryTypeBits,
195  /// preferred_flags | required_flags);
196  /// if (!r)
197  /// r = physical_device.findMemoryTypeIndex(reqs.memoryTypeBits,
198  /// required_flags);
199  ///
200  /// memory_type_index = *r;
201  ///
202  UT_Optional<uint32_t> findMemoryTypeIndex(uint32_t memory_type_bits, VkMemoryPropertyFlags required_properties) const;
203 
204  /// Returns the memory type index satisfying the given flags, or none. This
205  /// is equivalent to the above call with memory_type_bits == 0xFFFFFFFF
206  UT_Optional<uint32_t> findMemoryTypeIndex(VkMemoryPropertyFlags flags) const;
207 
208  // TODO extensions do not get enabled on the physical device but the logical
209  // one. these methods should there for be on the logical device object
211  {
212  return myAvailableExtensions;
213  }
214 
216  findQueueFamilyIndex(VkQueueFlags flags) const;
217 
218  VkFormatProperties formatProperties(const VkFormat format) const;
219 
220  VE_Result<VkImageFormatProperties2> imageFormatProperties(const VkImageCreateInfo &info) const;
221 
222  VE_PhysicalDevice() = default;
223 
224 private:
225  VkPhysicalDevice myHandle;
226  VkPhysicalDeviceProperties2 myProperties;
227  VkPhysicalDeviceIDProperties myIdProperties;
228  VkPhysicalDeviceVulkan11Properties myProperties11;
229  VkPhysicalDeviceVulkan12Properties myProperties12;
231  VkPhysicalDeviceMemoryProperties myMemoryProperties;
233  UT_Array<VkQueueFamilyProperties> myQueueFamilyProperties;
234 
235  UT_ArrayStringSet myAvailableExtensions;
236 
237  VE_PhysicalDevice(VkPhysicalDevice);
238 };
239 
241 VEextensionNamesToStrings(uint32_t count, const VE_DeviceExtension extensions[]);
242 
243 uint32_t VE_API VEgetPhysicalDeviceVersion(VkPhysicalDevice phy_dev);
244 
245 #endif
GLbitfield flags
Definition: glcorearb.h:1596
const VkPhysicalDeviceVulkan12Properties & vulkan12Properties() const
bool hasMultiviewTessellationShader() const
const VkPhysicalDeviceMemoryProperties & memoryProperties() const
std::optional< T > UT_Optional
Definition: UT_Optional.h:26
VkPhysicalDeviceFeatures2 * pFeatures()
constexpr uint32_t VE_SWIFT_SHADER_DEVICE_ID
#define VK_API_VERSION_MINOR(version)
Definition: vulkan_core.h:91
VE_DeviceExtension
const VkPhysicalDeviceAccelerationStructurePropertiesKHR & accelStructProperties() const
#define VE_API
Definition: VE_API.h:20
const VkPhysicalDeviceVulkan11Properties & vulkan11Properties() const
const VkPhysicalDeviceIDProperties & idProperties() const
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
GLuint const GLchar * name
Definition: glcorearb.h:786
const UT_ArrayStringSet & availableExtensions() const
VkPhysicalDevice getVkPhysicalDevice() const
const VE_PhysicalDeviceFeatureChain & features() const
const UT_Array< VkQueueFamilyProperties > & queueFamiltyProperties() const
LeafData & operator=(const LeafData &)=delete
const VkPhysicalDeviceFeatures2 * pFeatures() const
UT_Array< const char * > VE_API VEextensionNamesToStrings(uint32_t count, const VE_DeviceExtension extensions[])
VkFormat
Definition: vulkan_core.h:1386
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
VkFlags VkQueueFlags
Definition: vulkan_core.h:2356
VkFlags VkMemoryPropertyFlags
Definition: vulkan_core.h:2339
const VkPhysicalDeviceProperties & properties() const
GLint GLsizei count
Definition: glcorearb.h:405
uint32_t VE_API VEgetPhysicalDeviceVersion(VkPhysicalDevice phy_dev)