vsg  1.1.0
VulkanSceneGraph library
Instance.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 Robert Osfield
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 </editor-fold> */
14 
15 #include <vsg/core/ref_ptr.h>
16 #include <vsg/vk/AllocationCallbacks.h>
17 #include <vsg/vk/InstanceExtensions.h>
18 
19 #include <vector>
20 
21 namespace vsg
22 {
23  // forward declare
24  class PhysicalDevice;
25  class Surface;
26 
27  using Names = std::vector<const char*>;
28  using PhysicalDeviceTypes = std::vector<VkPhysicalDeviceType>;
29  using InstanceLayerProperties = std::vector<VkLayerProperties>;
30  using InstanceExtensionProperties = std::vector<VkExtensionProperties>;
31 
33  extern VSG_DECLSPEC InstanceExtensionProperties enumerateInstanceExtensionProperties(const char* pLayerName = nullptr);
34 
36  extern VSG_DECLSPEC InstanceLayerProperties enumerateInstanceLayerProperties();
37 
39  extern VSG_DECLSPEC Names validateInstancelayerNames(const Names& names);
40 
42  class VSG_DECLSPEC Instance : public Inherit<Object, Instance>
43  {
44  public:
45  Instance(Names instanceExtensions, Names layers, uint32_t vulkanApiVersion = VK_API_VERSION_1_0, AllocationCallbacks* allocator = nullptr);
46 
48  const uint32_t apiVersion = VK_API_VERSION_1_0;
49 
50  operator VkInstance() const { return _instance; }
51  VkInstance vk() const { return _instance; }
52 
53  AllocationCallbacks* getAllocationCallbacks() { return _allocator.get(); }
54  const AllocationCallbacks* getAllocationCallbacks() const { return _allocator.get(); }
55 
56  using PhysicalDevices = std::vector<ref_ptr<PhysicalDevice>>;
57  PhysicalDevices& getPhysicalDevices() { return _physicalDevices; }
58  const PhysicalDevices& getPhysicalDevices() const { return _physicalDevices; }
59 
61  ref_ptr<PhysicalDevice> getPhysicalDevice(VkQueueFlags queueFlags, const PhysicalDeviceTypes& deviceTypePreferences = {}) const;
62 
64  ref_ptr<PhysicalDevice> getPhysicalDevice(VkQueueFlags queueFlags, Surface* surface, const PhysicalDeviceTypes& deviceTypePreferences = {}) const;
65 
67  std::pair<ref_ptr<PhysicalDevice>, int> getPhysicalDeviceAndQueueFamily(VkQueueFlags queueFlags, const PhysicalDeviceTypes& deviceTypePreferences = {}) const;
68 
70  std::tuple<ref_ptr<PhysicalDevice>, int, int> getPhysicalDeviceAndQueueFamily(VkQueueFlags queueFlags, Surface* surface, const PhysicalDeviceTypes& deviceTypePreferences = {}) const;
71 
73  const InstanceExtensions* getExtensions() const { return _extensions.get(); }
74 
76  template<typename T>
77  bool getProcAddr(T& procAddress, const char* pName, const char* pNameFallback = nullptr) const
78  {
79  procAddress = reinterpret_cast<T>(vkGetInstanceProcAddr(_instance, pName));
80  if (procAddress) return true;
81 
82  if (pNameFallback) procAddress = reinterpret_cast<T>(vkGetInstanceProcAddr(_instance, pNameFallback));
83  return (procAddress);
84  }
85 
86  protected:
87  virtual ~Instance();
88 
89  VkInstance _instance;
91 
92  PhysicalDevices _physicalDevices;
93 
94  ref_ptr<InstanceExtensions> _extensions;
95  };
96  VSG_type_name(vsg::Instance);
97 
98 } // namespace vsg
Adapter class that provides a means of managing the lifetime of VkAllocationCallbacks.
Definition: AllocationCallbacks.h:24
Definition: Inherit.h:28
Definition: InstanceExtensions.h:24
Instance encapsulates the VkInstance.
Definition: Instance.h:43
bool getProcAddr(T &procAddress, const char *pName, const char *pNameFallback=nullptr) const
get the address of specified function using vkGetInstanceProcAddr.
Definition: Instance.h:77
std::tuple< ref_ptr< PhysicalDevice >, int, int > getPhysicalDeviceAndQueueFamily(VkQueueFlags queueFlags, Surface *surface, const PhysicalDeviceTypes &deviceTypePreferences={}) const
get a PhysicalDevice and queue family index that supports the specified queueFlags,...
const InstanceExtensions * getExtensions() const
get the extensions structure that holds a range of function pointers to vkInstance extensions
Definition: Instance.h:73
std::pair< ref_ptr< PhysicalDevice >, int > getPhysicalDeviceAndQueueFamily(VkQueueFlags queueFlags, const PhysicalDeviceTypes &deviceTypePreferences={}) const
get a PhysicalDevice and queue family index that supports the specified queueFlags,...
ref_ptr< PhysicalDevice > getPhysicalDevice(VkQueueFlags queueFlags, Surface *surface, const PhysicalDeviceTypes &deviceTypePreferences={}) const
get a PhysicalDevice that supports the specified queueFlags, and presentation of specified surface if...
ref_ptr< PhysicalDevice > getPhysicalDevice(VkQueueFlags queueFlags, const PhysicalDeviceTypes &deviceTypePreferences={}) const
get a PhysicalDevice that supports the specified queueFlags, and presentation of specified surface if...
Surface encapsulates VkSurfaceKHR.
Definition: Surface.h:22
Definition: ref_ptr.h:22