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 
146  /// Raw Vulkan handle for Main Queue supporting graphics, compute and transfer
147  VkQueue getVkGraphicsQueue();
148 
149  /// Our Vulkan memory allocator implementation
150  RV_VKMemAllocator& getMemAllocator() { return *myAllocator; }
151  /// Our Vulkan descriptor set allocator
152  RV_DescriptorAllocator& getDescAllocator(){return *myDescriptorAllocator;}
153 
154  /// Get text information about the current driver and device
155  void fetchDriverInfo(UT_WorkBuffer &info);
156 
157  static uint32_t getInstanceVersion();
158 
159  /// The Vulkan version as implemented by the device driver
160  uint32_t getDeviceVersion();
161  /// The vendor of the device (AMD, Intel, NVIDIA, MoltenVK)
162  RV_GraphicsDevice getDeviceVendor() const;
163 
164  const UT_StringHolder &deviceName() const { return myDeviceName; }
165 
166  /// True if the debug validation layers are active
167  static bool usingDebugValidation();
168 
169  /// True if Vulkan multithreading env var is set to true
170  static bool usingVulkanMultithreading();
171 
172  /// Maximum supported number of color samples in a multisample framebuffer
173  int getMaxColorSamples() const;
174 
175  /// Maximum supported number of depth samples in a multisample framebuffer
176  int getMaxDepthSamples() const;
177 
178  /// Maximum size of 2D texture
179  int getMaxTextureSize2D() const;
180 
181  /// Maximum size of 3D texture
182  int getMaxTextureSize3D() const;
183 
184  /// Granularity of Line width setting
185  float getLineWidthGranularity() const;
186 
187  /// Range of Line width setting
188  UT_Vector2F getLineWidthRange() const;
189 
190  /// Granularity of Point Size setting
191  float getPointSizeGranularity() const;
192 
193  /// Range of Point Size setting
194  UT_Vector2F getPointSizeRange() const;
195 
196  /// Object containing function pointers to Vulkan extensions
197  RV_VKExt* getExt() { return myExtensions.get(); }
198 
199  /// Query if the given Vulkan extension is enabled in the device
200  bool extEnabled( const char* vk_ext_name ) const;
201 
202  /// @brief Query the features enabled on the RV_Instance's VkDevice.
203  /// Copies the feature flags used in device creation into `out_feature_struct`
204  /// and returns true, if a feature struct with the matching `sType` was used.
205  /// If a matching struct wasn't used, returns false
206  bool getFeatureStruct(
207  VkBaseOutStructure* out_feature_struct,
208  exint feature_struct_size_bytes) const;
209 
210  /// Query restriction for GL to Vulkan interop texture. If true, must use linear textures
211  bool useLinearTilingForGLInterop() const;
212 
213  /// Query if custom sample locations can be used for the given MSAA level
214  bool supportsSampleLocations(int aa) const;
215 
216  /// Block until the device has finished all commands (Call with care!)
217  void waitDeviceIdle();
218 
219  /// Returns the physical device features that have been enabled on our
220  /// device
221  const VE_PhysicalDeviceFeatures &getPhysicalDeviceFeatures() const;
222 
223  // WIP: Resource tracking
224  // functions used in VK Resource classes to register their existance
225  // with the Instance and track details about them:
226  /// `register` - called on creation, to get a Unique ID for the resource
227  /// `clear` - called on destruction
228  /// `validate` - can be called to check if the ID's resource is still valid
229  /// `setDeletePending` - flags the resource as being queued for deletion by
230  // `RVdestroyVKPtr`. They will be treated as invalid since
231  // they will be deleted once their Command Buffer is executed
232  // Shouldn't be manually called
233  RV_ResourceID registerImage(RV_VKImage* img);
234  void clearImage(RV_VKImage* img);
235  bool validateImage(RV_ResourceID id, bool allow_pending_delete = false) const;
236  void setDeletePending(RV_VKImage* img);
237 
238  RV_ResourceID registerBuffer(RV_VKBuffer* buf);
239  void clearBuffer(RV_VKBuffer* buf);
240  bool validateBuffer(RV_ResourceID id, bool allow_pending_delete = false) const;
241  void setDeletePending(RV_VKBuffer* buf);
242 
243  RV_ResourceID registerAccelStruct(RV_VKAccelerationStructure* accel_struct);
244  void clearAccelStruct(RV_VKAccelerationStructure* accel_struct);
245  bool validateAccelStruct(RV_ResourceID id, bool allow_pending_delete = false) const;
246  void setDeletePending(RV_VKAccelerationStructure* accel_struct);
247 
248  RV_ResourceID registerSet(RV_VKDescriptorSet* set);
249  void clearSet(RV_VKDescriptorSet* set);
250  bool validateSet(RV_ResourceID id, bool allow_pending_delete = false) const;
251  void setDeletePending(RV_VKDescriptorSet* set);
252 
253  // DEBUG: Prints the list of active resource IDs
254  void printIDs() const;
255 
256 private:
257  RV_Instance();
258 
259 
262  UT_UniquePtr<RV_DescriptorAllocator> myDescriptorAllocator;
263  UT_UniquePtr<RV_VKQueue> myGraphicsQueue;
264 
265  UT_UniquePtr<RV_VKExt> myExtensions;
266 
267  VE_PhysicalDeviceFeatures myEnabledDeviceFeatures;
268  UT_Array<const char*> myEnabledDevExtensions;
269  UT_Array<const char*> myEnabledInstExtensions;
270  UT_StringHolder myDeviceName;
271 };
272 
273 #endif
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
RV_VKMemAllocator & getMemAllocator()
Our Vulkan memory allocator implementation.
Definition: RV_Instance.h:150
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:152
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:164
#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:197
GLboolean r
Definition: glcorearb.h:1222
A vulkan buffer object.
Definition: RV_VKBuffer.h:81