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  /// Returns true if the Extra Extensions used during instance creation
107  /// match the currently set one, i.e. if any have been added since the
108  /// instance was created
109  bool extraExtensionsChanged() const;
110 
111  /// add callback to be called when current rendering is finished.
112  /// Typed as a UT_Function instead of function ptr , so
113  /// capturing lambdas can be used as callbacks
115  void addPostRenderCB(const RenderCallback &callback);
116 
117  /// notification from active RV_Render that current rendering tasks
118  /// are about to flushed
119  void finishRendering(RV_Render* r);
120 
121  /// Create the global vulkan instance. Used only at startup.
122  static RV_Instance* create();
123 
124  ~RV_Instance();
125 
126  /// Set up Vulkan once the instance is created
127  bool initializeDevice();
128 
129  /// Get the raw vulkan device assocated with this instance.
130  VkDevice getDevice();
131  /// Get the raw vulkan physical device assocated with this instance.
132  VkPhysicalDevice getPhysicalDevice();
133  /// Get the raw vulkan instance.
134  VkInstance getVkInst();
135 
136  /// Raw Vulkan memory properties of the physical device
137  const VkPhysicalDeviceMemoryProperties* getMemoryProps() const;
138 
139  /// Raw Vulkan acceleration structure properties of the physical device
140  const VkPhysicalDeviceAccelerationStructurePropertiesKHR* getAccelStructProps() const;
141 
142  /// Raw Vulkan acceleration structure properties of the physical device
143  const VkPhysicalDeviceLimits* getVulkanDeviceLimits() const;
144 
145  /// Vulkan device UUID, used to match devices for cross-API interop
146  void getDeviceUUID(uint8 (&uuid)[16]) const;
147 
148  /// Main Queue supporting graphics, compute and transfer
149  RV_VKQueue& getGraphicsQueue() { return *myGraphicsQueue; }
150 
151  /// Raw Vulkan handle for Main Queue supporting graphics, compute and transfer
152  VkQueue getVkGraphicsQueue();
153 
154  /// Our Vulkan memory allocator implementation
155  RV_VKMemAllocator& getMemAllocator() { return *myAllocator; }
156  /// Our Vulkan descriptor set allocator
157  RV_DescriptorAllocator& getDescAllocator(){return *myDescriptorAllocator;}
158 
159  /// Get text information about the current driver and device
160  void fetchDriverInfo(UT_WorkBuffer &info);
161 
162  static uint32_t getInstanceVersion();
163 
164  /// The Vulkan version as implemented by the device driver
165  uint32_t getDeviceVersion();
166  /// The vendor of the device (AMD, Intel, NVIDIA, MoltenVK)
167  RV_GraphicsDevice getDeviceVendor() const;
168 
169  const UT_StringHolder &deviceName() const { return myDeviceName; }
170 
171  /// True if the debug validation layers are active
172  static bool usingDebugValidation();
173 
174  /// True if Vulkan multithreading env var is set to true
175  static bool usingVulkanMultithreading();
176 
177  /// Maximum supported number of color samples in a multisample framebuffer
178  int getMaxColorSamples() const;
179 
180  /// Maximum supported number of depth samples in a multisample framebuffer
181  int getMaxDepthSamples() const;
182 
183  /// Maximum size of 2D texture
184  int getMaxTextureSize2D() const;
185 
186  /// Maximum size of 3D texture
187  int getMaxTextureSize3D() const;
188 
189  /// Granularity of Line width setting
190  float getLineWidthGranularity() const;
191 
192  /// Range of Line width setting
193  UT_Vector2F getLineWidthRange() const;
194 
195  /// Granularity of Point Size setting
196  float getPointSizeGranularity() const;
197 
198  /// Range of Point Size setting
199  UT_Vector2F getPointSizeRange() const;
200 
201  /// Object containing function pointers to Vulkan extensions
202  RV_VKExt* getExt() { return myExtensions.get(); }
203 
204  /// Query if the given Vulkan extension is enabled in the device
205  bool extEnabled( const char* vk_ext_name ) const;
206 
207  /// @brief Query the features enabled on the RV_Instance's VkDevice.
208  /// Copies the feature flags used in device creation into `out_feature_struct`
209  /// and returns true, if a feature struct with the matching `sType` was used.
210  /// If a matching struct wasn't used, returns false
211  bool getFeatureStruct(
212  VkBaseOutStructure* out_feature_struct,
213  exint feature_struct_size_bytes) const;
214 
215  /// Query restriction for GL to Vulkan interop texture. If true, must use linear textures
216  bool useLinearTilingForGLInterop() const;
217 
218  /// Query if custom sample locations can be used for the given MSAA level
219  bool supportsSampleLocations(int aa) const;
220 
221  /// Block until the device has finished all commands (Call with care!)
222  void waitDeviceIdle();
223 
224  /// Returns the physical device features that have been enabled on our
225  /// device
226  const VE_PhysicalDeviceFeatures &getPhysicalDeviceFeatures() const;
227 
228  // WIP: Resource tracking
229  // functions used in VK Resource classes to register their existance
230  // with the Instance and track details about them:
231  /// `register` - called on creation, to get a Unique ID for the resource
232  /// `clear` - called on destruction
233  /// `validate` - can be called to check if the ID's resource is still valid
234  /// `setDeletePending` - flags the resource as being queued for deletion by
235  // `RVdestroyVKPtr`. They will be treated as invalid since
236  // they will be deleted once their Command Buffer is executed
237  // Shouldn't be manually called
238  RV_ResourceID registerImage(RV_VKImage* img);
239  void clearImage(RV_VKImage* img);
240  bool validateImage(RV_ResourceID id, bool allow_pending_delete = false) const;
241  void setDeletePending(RV_VKImage* img);
242 
243  RV_ResourceID registerBuffer(RV_VKBuffer* buf);
244  void clearBuffer(RV_VKBuffer* buf);
245  bool validateBuffer(RV_ResourceID id, bool allow_pending_delete = false) const;
246  void setDeletePending(RV_VKBuffer* buf);
247 
248  RV_ResourceID registerAccelStruct(RV_VKAccelerationStructure* accel_struct);
249  void clearAccelStruct(RV_VKAccelerationStructure* accel_struct);
250  bool validateAccelStruct(RV_ResourceID id, bool allow_pending_delete = false) const;
251  void setDeletePending(RV_VKAccelerationStructure* accel_struct);
252 
253  RV_ResourceID registerSet(RV_VKDescriptorSet* set);
254  void clearSet(RV_VKDescriptorSet* set);
255  bool validateSet(RV_ResourceID id, bool allow_pending_delete = false) const;
256  void setDeletePending(RV_VKDescriptorSet* set);
257 
258  // DEBUG: Prints the list of active resource IDs
259  void printIDs() const;
260 
261 private:
262  RV_Instance();
263 
264 
267  UT_UniquePtr<RV_DescriptorAllocator> myDescriptorAllocator;
268  UT_UniquePtr<RV_VKQueue> myGraphicsQueue;
269 
270  UT_UniquePtr<RV_VKExt> myExtensions;
271 
272  VE_PhysicalDeviceFeatures myEnabledDeviceFeatures;
273  UT_Array<const char*> myEnabledDevExtensions;
274  UT_Array<const char*> myEnabledInstExtensions;
275  UT_StringHolder myDeviceName;
276 };
277 
278 #endif
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
RV_VKMemAllocator & getMemAllocator()
Our Vulkan memory allocator implementation.
Definition: RV_Instance.h:155
RV_VKQueue & getGraphicsQueue()
Main Queue supporting graphics, compute and transfer.
Definition: RV_Instance.h:149
RV_DescriptorAllocator & getDescAllocator()
Our Vulkan descriptor set allocator.
Definition: RV_Instance.h:157
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:169
#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:114
RV_VKExt * getExt()
Object containing function pointers to Vulkan extensions.
Definition: RV_Instance.h:202
GLboolean r
Definition: glcorearb.h:1222
A vulkan buffer object.
Definition: RV_VKBuffer.h:81