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