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 as soon as instance is created.
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  /// add callback to be called when current rendering is finished.
68  /// Typed as a UT_Function instead of function ptr , so
69  /// capturing lambdas can be used as callbacks
71  void addPostRenderCB(const RenderCallback &callback);
72 
73  /// notification from active RV_Render that current rendering tasks
74  /// are about to flushed
75  void finishRendering(RV_Render* r);
76 
77  /// Create the global vulkan instance. Used only at startup.
78  static RV_Instance* create();
79 
80  ~RV_Instance();
81 
82  /// Set up Vulkan once the instance is created
83  bool initializeDevice();
84 
85  /// Get the raw vulkan device assocated with this instance.
86  VkDevice getDevice();
87  /// Get the raw vulkan physical device assocated with this instance.
88  VkPhysicalDevice getPhysicalDevice();
89  /// Get the raw vulkan instance.
90  VkInstance getVkInst();
91 
92  /// Raw Vulkan memory properties of the physical device
93  const VkPhysicalDeviceMemoryProperties* getMemoryProps() const;
94 
95  /// Raw Vulkan acceleration structure properties of the physical device
96  const VkPhysicalDeviceAccelerationStructurePropertiesKHR* getAccelStructProps() const;
97 
98  /// Raw Vulkan acceleration structure properties of the physical device
99  const VkPhysicalDeviceLimits* getVulkanDeviceLimits() const;
100 
101  /// Vulkan device UUID, used to match devices for cross-API interop
102  void getDeviceUUID(uint8 (&uuid)[16]) const;
103 
104  /// Main Queue supporting graphics, compute and transfer
105  RV_VKQueue& getGraphicsQueue() { return *myGraphicsQueue; }
106  /// Our Vulkan memory allocator implementation
107  RV_VKMemAllocator& getMemAllocator() { return *myAllocator; }
108  /// Our Vulkan descriptor set allocator
109  RV_DescriptorAllocator& getDescAllocator(){return *myDescriptorAllocator;}
110 
111  /// Get text information about the current driver and device
112  void fetchDriverInfo(UT_WorkBuffer &info);
113 
114  static uint32_t getInstanceVersion();
115 
116  /// The Vulkan version as implemented by the device driver
117  uint32_t getDeviceVersion();
118  /// The vendor of the device (AMD, Intel, NVIDIA, MoltenVK)
119  RV_GraphicsDevice getDeviceVendor() const;
120 
121  const UT_StringHolder &deviceName() const { return myDeviceName; }
122 
123  /// True if the debug validation layers are active
124  static bool usingDebugValidation();
125 
126  /// True if Vulkan multithreading env var is set to true
127  static bool usingVulkanMultithreading();
128 
129  /// Maximum supported number of color samples in a multisample framebuffer
130  int getMaxColorSamples() const;
131 
132  /// Maximum supported number of depth samples in a multisample framebuffer
133  int getMaxDepthSamples() const;
134 
135  /// Maximum size of 2D texture
136  int getMaxTextureSize2D() const;
137 
138  /// Maximum size of 3D texture
139  int getMaxTextureSize3D() const;
140 
141  /// Granularity of Line width setting
142  float getLineWidthGranularity() const;
143 
144  /// Range of Line width setting
145  UT_Vector2F getLineWidthRange() const;
146 
147  /// Granularity of Point Size setting
148  float getPointSizeGranularity() const;
149 
150  /// Range of Point Size setting
151  UT_Vector2F getPointSizeRange() const;
152 
153  /// Object containing function pointers to Vulkan extensions
154  RV_VKExt* getExt() { return myExtensions.get(); }
155 
156  /// Query if the given Vulkan extension is enabled in the device
157  bool extEnabled( const char* vk_ext_name ) const;
158 
159  /// Query restriction for GL to Vulkan interop texture. If true, must use linear textures
160  bool useLinearTilingForGLInterop() const;
161 
162  /// Block until the device has finished all commands (Call with care!)
163  void waitDeviceIdle();
164 
165  /// Returns the physical device features that have been enabled on our
166  /// device
167  const VE_PhysicalDeviceFeatures &getPhysicalDeviceFeatures() const;
168 
169 
170  // WIP: Resource tracking
171  // functions used in VK Resource classes to register their existance
172  // with the Instance and track details about them:
173  /// `register` - called on creation, to get a Unique ID for the resource
174  /// `clear` - called on destruction
175  /// `validate` - can be called to check if the ID's resource is still valid
176  /// `setDeletePending` - flags the resource as being queued for deletion by
177  // `RVdestroyVKPtr`. They will be treated as invalid since
178  // they will be deleted once their Command Buffer is executed
179  // Shouldn't be manually called
180  RV_ResourceID registerImage(RV_VKImage* img);
181  void clearImage(RV_VKImage* img);
182  bool validateImage(RV_ResourceID id, bool allow_pending_delete = false) const;
183  void setDeletePending(RV_VKImage* img);
184 
185  RV_ResourceID registerBuffer(RV_VKBuffer* buf);
186  void clearBuffer(RV_VKBuffer* buf);
187  bool validateBuffer(RV_ResourceID id, bool allow_pending_delete = false) const;
188  void setDeletePending(RV_VKBuffer* buf);
189 
190  RV_ResourceID registerAccelStruct(RV_VKAccelerationStructure* accel_struct);
191  void clearAccelStruct(RV_VKAccelerationStructure* accel_struct);
192  bool validateAccelStruct(RV_ResourceID id, bool allow_pending_delete = false) const;
193  void setDeletePending(RV_VKAccelerationStructure* accel_struct);
194 
195  RV_ResourceID registerSet(RV_VKDescriptorSet* set);
196  void clearSet(RV_VKDescriptorSet* set);
197  bool validateSet(RV_ResourceID id, bool allow_pending_delete = false) const;
198  void setDeletePending(RV_VKDescriptorSet* set);
199 
200  // DEBUG: Prints the list of active resource IDs
201  void printIDs() const;
202 
203 private:
204  RV_Instance();
205 
206 
209  UT_UniquePtr<RV_DescriptorAllocator> myDescriptorAllocator;
210  UT_UniquePtr<RV_VKQueue> myGraphicsQueue;
211 
212  UT_UniquePtr<RV_VKExt> myExtensions;
213 
214  VE_PhysicalDeviceFeatures myEnabledDeviceFeatures;
215  UT_Array<const char*> myEnabledDevExtensions;
216  UT_Array<const char*> myEnabledInstExtensions;
217  UT_StringHolder myDeviceName;
218 };
219 
220 #endif
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
RV_VKMemAllocator & getMemAllocator()
Our Vulkan memory allocator implementation.
Definition: RV_Instance.h:107
RV_VKQueue & getGraphicsQueue()
Main Queue supporting graphics, compute and transfer.
Definition: RV_Instance.h:105
RV_DescriptorAllocator & getDescAllocator()
Our Vulkan descriptor set allocator.
Definition: RV_Instance.h:109
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:121
#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:70
RV_VKExt * getExt()
Object containing function pointers to Vulkan extensions.
Definition: RV_Instance.h:154
GLboolean r
Definition: glcorearb.h:1222
A vulkan buffer object.
Definition: RV_VKBuffer.h:81