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