HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RV_Instance.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: RV_Instance.h ( RV Library, C++)
7  *
8  * COMMENTS:
9  * Main interface to Vulkan
10  */
11 #ifndef RV_Instance_h
12 #define RV_Instance_h
13 
14 #include "RV_API.h"
15 
16 #include "RV_Type.h"
17 
18 #include <VE/VE_VK.h>
19 
20 #include <UT/UT_Array.h>
21 #include <UT/UT_UniquePtr.h>
22 #include <UT/UT_Function.h>
23 
25 
26 class rv_InstanceData;
27 
29 class RV_Render;
30 class RV_VKQueue;
31 class RV_VKCommandPool;
32 class RV_VKCommandBuffer;
33 class RV_VKCommandBufferAllocator;
34 class RV_VKExt;
35 class RV_VKMemAllocator;
36 class UT_WorkBuffer;
37 
38 class RV_VKBuffer;
39 class RV_VKImage;
41 class RV_VKDescriptorSet;
42 
43 /// Handle to the main interface of Vulkan
45 {
46 public:
47  /// The global vulkan instance, creating it if it hasn't been created yet.
48  static RV_Instance* getInstance();
49  /// Returns whether the global vulkan instance exists
50  static bool hasInstance();
51  /// Destroy the global instance. For system exit.
52  static void destroyInstance();
53 
54  /// The callback that's called on app exit to destroy the global instance
55  static void exitCallback(void*);
56 
57  /// add callbacks to be called as soon as instance is created
58  static void addPostCreateCB(bool (*cb)(RV_Instance*));
59  static bool hasPostCreateCBs();
60 
61  /// add callbacks to be called before the instance is destroyed.
62  /// the list is cleared after the instance is destroyed, so they
63  /// must be re-added for a new instance
64  static void addPreDestroyCB(bool (*cb)(RV_Instance*));
65  static bool hasPreDestroyCBs();
66 
67  /// @brief Request extensions or features to be enabled on the Vulkan device
68  /// Can be called before the RV_Instance is created to add extra device
69  /// extensions to be enabled, if supported on the selected device.
70  /// Can pass in a feature struct with the extension, to enable
71  /// extension-specific features
72  ///
73  /// Core features can be also requested by passing the appropriate struct
74  /// (e.g. VkPhysicalDeviceFeatures2, VkPhysicalDeviceVulkan11Features, etc.)
75  /// with `nullptr` as the name. Features from any extensions promoted to
76  /// Core in Vulkan 1.1, 1.2, or 1.3 should be requested using the Core
77  /// Vulkan features struct, NOT the extension specific struct.
78  ///
79  /// After the instance is created, it should be queried wtih `extEnabled()`
80  /// and 'getFeatureStruct()' to confirm whether the requested extensions and
81  /// features were supported
82  ///
83  /// @param ext_name the name of the extension
84  /// @param feature_struct The feature struct for the extension, with
85  /// desired features flags enabled. It can be
86  /// 'nullptr' if the extension doesn't have
87  /// features
88  /// @param struct_size_bytes the size in bytes of the feature struct
89  static void addExtraDeviceExtension(
90  const char* ext_name,
91  VkBaseOutStructure* feature_struct = nullptr,
92  exint struct_size_bytes = 0);
93 
94  /// Query if an extension has been added to the Extra Extension list. If the
95  /// appropriate feature struct is supplied, it will be filled with requested
96  /// features. Parameters same as 'addExtraDeviceExtension()'
97  static bool hasExtraDeviceExtension(
98  const char* ext_name,
99  VkBaseOutStructure* feature_struct = nullptr,
100  exint feature_struct_size_bytes = 0);
101 
102  /// Can be called before the RV_Instance is created to add extra instance
103  /// extensions to be enabled (if supported) on the selected device
104  static void addExtraInstanceExtension(const char* ext_name);
105 
106  /// add callback to be called when current rendering is finished.
107  /// Typed as a UT_Function instead of function ptr , so
108  /// capturing lambdas can be used as callbacks
110  void addPostRenderCB(const RenderCallback &callback);
111 
112  /// notification from active RV_Render that current rendering tasks
113  /// are about to flushed
114  void finishRendering(RV_Render* r);
115 
116  /// Create the global vulkan instance. Used only at startup.
117  static RV_Instance* create();
118 
119  ~RV_Instance();
120 
121  /// Set up Vulkan once the instance is created
122  bool initializeDevice();
123 
124  /// Get the raw vulkan device assocated with this instance.
125  VkDevice getDevice();
126  /// Get the raw vulkan physical device assocated with this instance.
127  VkPhysicalDevice getPhysicalDevice();
128  /// Get the raw vulkan instance.
129  VkInstance getVkInst();
130 
131  /// Raw Vulkan memory properties of the physical device
132  const VkPhysicalDeviceMemoryProperties* getMemoryProps() const;
133 
134  /// Raw Vulkan acceleration structure properties of the physical device
135  const VkPhysicalDeviceAccelerationStructurePropertiesKHR* getAccelStructProps() const;
136 
137  /// Raw Vulkan acceleration structure properties of the physical device
138  const VkPhysicalDeviceLimits* getVulkanDeviceLimits() const;
139 
140  /// Vulkan device UUID, used to match devices for cross-API interop
141  void getDeviceUUID(uint8 (&uuid)[16]) const;
142 
143  /// Main Queue supporting graphics, compute and transfer
144  RV_VKQueue& getGraphicsQueue() { return *myGraphicsQueue; }
145  /// Our Vulkan memory allocator implementation
146  RV_VKMemAllocator& getMemAllocator() { return *myAllocator; }
147  /// Our Vulkan descriptor set allocator
148  RV_DescriptorAllocator& getDescAllocator(){return *myDescriptorAllocator;}
149 
150  /// Get text information about the current driver and device
151  void fetchDriverInfo(UT_WorkBuffer &info);
152 
153  static uint32_t getInstanceVersion();
154 
155  /// The Vulkan version as implemented by the device driver
156  uint32_t getDeviceVersion();
157  /// The vendor of the device (AMD, Intel, NVIDIA, MoltenVK)
158  RV_GraphicsDevice getDeviceVendor() const;
159 
160  const UT_StringHolder &deviceName() const { return myDeviceName; }
161 
162  /// True if the debug validation layers are active
163  static bool usingDebugValidation();
164 
165  /// True if Vulkan multithreading env var is set to true
166  static bool usingVulkanMultithreading();
167 
168  /// Maximum supported number of color samples in a multisample framebuffer
169  int getMaxColorSamples() const;
170 
171  /// Maximum supported number of depth samples in a multisample framebuffer
172  int getMaxDepthSamples() const;
173 
174  /// Maximum size of 2D texture
175  int getMaxTextureSize2D() const;
176 
177  /// Maximum size of 3D texture
178  int getMaxTextureSize3D() const;
179 
180  /// Granularity of Line width setting
181  float getLineWidthGranularity() const;
182 
183  /// Range of Line width setting
184  UT_Vector2F getLineWidthRange() const;
185 
186  /// Granularity of Point Size setting
187  float getPointSizeGranularity() const;
188 
189  /// Range of Point Size setting
190  UT_Vector2F getPointSizeRange() const;
191 
192  /// Object containing function pointers to Vulkan extensions
193  RV_VKExt* getExt() { return myExtensions.get(); }
194 
195  /// Query if the given Vulkan extension is enabled in the device
196  bool extEnabled( const char* vk_ext_name ) const;
197 
198  /// @brief Query the features enabled on the RV_Instance's VkDevice.
199  /// Copies the feature flags used in device creation into `out_feature_struct`
200  /// and returns true, if a feature struct with the matching `sType` was used.
201  /// If a matching struct wasn't used, returns false
202  bool getFeatureStruct(
203  VkBaseOutStructure* out_feature_struct,
204  exint feature_struct_size_bytes) const;
205 
206  /// Query restriction for GL to Vulkan interop texture. If true, must use linear textures
207  bool useLinearTilingForGLInterop() const;
208 
209  /// Query if custom sample locations can be used for the given MSAA level
210  bool supportsSampleLocations(int aa) const;
211 
212  /// Block until the device has finished all commands (Call with care!)
213  void waitDeviceIdle();
214 
215  /// Returns the physical device features that have been enabled on our
216  /// device
217  const VE_PhysicalDeviceFeatures &getPhysicalDeviceFeatures() const;
218 
219  // WIP: Resource tracking
220  // functions used in VK Resource classes to register their existance
221  // with the Instance and track details about them:
222  /// `register` - called on creation, to get a Unique ID for the resource
223  /// `clear` - called on destruction
224  /// `validate` - can be called to check if the ID's resource is still valid
225  /// `setDeletePending` - flags the resource as being queued for deletion by
226  // `RVdestroyVKPtr`. They will be treated as invalid since
227  // they will be deleted once their Command Buffer is executed
228  // Shouldn't be manually called
229  RV_ResourceID registerImage(RV_VKImage* img);
230  void clearImage(RV_VKImage* img);
231  bool validateImage(RV_ResourceID id, bool allow_pending_delete = false) const;
232  void setDeletePending(RV_VKImage* img);
233 
234  RV_ResourceID registerBuffer(RV_VKBuffer* buf);
235  void clearBuffer(RV_VKBuffer* buf);
236  bool validateBuffer(RV_ResourceID id, bool allow_pending_delete = false) const;
237  void setDeletePending(RV_VKBuffer* buf);
238 
239  RV_ResourceID registerAccelStruct(RV_VKAccelerationStructure* accel_struct);
240  void clearAccelStruct(RV_VKAccelerationStructure* accel_struct);
241  bool validateAccelStruct(RV_ResourceID id, bool allow_pending_delete = false) const;
242  void setDeletePending(RV_VKAccelerationStructure* accel_struct);
243 
244  RV_ResourceID registerSet(RV_VKDescriptorSet* set);
245  void clearSet(RV_VKDescriptorSet* set);
246  bool validateSet(RV_ResourceID id, bool allow_pending_delete = false) const;
247  void setDeletePending(RV_VKDescriptorSet* set);
248 
249  // DEBUG: Prints the list of active resource IDs
250  void printIDs() const;
251 
252 private:
253  RV_Instance();
254 
255 
258  UT_UniquePtr<RV_DescriptorAllocator> myDescriptorAllocator;
259  UT_UniquePtr<RV_VKQueue> myGraphicsQueue;
260 
261  UT_UniquePtr<RV_VKExt> myExtensions;
262 
263  VE_PhysicalDeviceFeatures myEnabledDeviceFeatures;
264  UT_Array<const char*> myEnabledDevExtensions;
265  UT_Array<const char*> myEnabledInstExtensions;
266  UT_StringHolder myDeviceName;
267 };
268 
269 #endif
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
RV_VKMemAllocator & getMemAllocator()
Our Vulkan memory allocator implementation.
Definition: RV_Instance.h:146
RV_VKQueue & getGraphicsQueue()
Main Queue supporting graphics, compute and transfer.
Definition: RV_Instance.h:144
RV_DescriptorAllocator & getDescAllocator()
Our Vulkan descriptor set allocator.
Definition: RV_Instance.h:148
int64 exint
Definition: SYS_Types.h:125
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
unsigned char uint8
Definition: SYS_Types.h:36
RV_GraphicsDevice
Definition: RV_Type.h:79
constexpr auto set(type rhs) -> int
Definition: core.h:610
const UT_StringHolder & deviceName() const
Definition: RV_Instance.h:160
#define RV_API
Definition: RV_API.h:10
std::function< T > UT_Function
Definition: UT_Function.h:37
Handle to the main interface of Vulkan.
Definition: RV_Instance.h:44
GLint void * img
Definition: glcorearb.h:556
UT_Function< void(RV_Instance *)> RenderCallback
Definition: RV_Instance.h:109
RV_VKExt * getExt()
Object containing function pointers to Vulkan extensions.
Definition: RV_Instance.h:193
GLboolean r
Definition: glcorearb.h:1222
A vulkan buffer object.
Definition: RV_VKBuffer.h:81