1919#pragma once
2020
2121#include " common/helpers.h"
22- #include " common/vk_common.h"
23- #include " core/hpp_debug.h"
24- #include < ranges>
25- #include < unordered_set>
2622#include < vulkan/vulkan.hpp>
2723
28- #if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
29- # define USE_VALIDATION_LAYERS
30- #endif
31-
32- #if defined(USE_VALIDATION_LAYERS) && \
33- (defined (VKB_VALIDATION_LAYERS_GPU_ASSISTED) || defined (VKB_VALIDATION_LAYERS_BEST_PRACTICES) || defined (VKB_VALIDATION_LAYERS_SYNCHRONIZATION))
34- # define USE_VALIDATION_LAYER_FEATURES
35- #endif
36-
3724namespace vkb
3825{
3926namespace core
4027{
28+ namespace
29+ {
4130template <vkb::BindingType bindingType>
42- class PhysicalDevice ;
43- using PhysicalDeviceC = PhysicalDevice<vkb::BindingType::C>;
44- using PhysicalDeviceCpp = PhysicalDevice<vkb::BindingType::Cpp>;
31+ typename std::conditional<bindingType == vkb::BindingType::Cpp, vk::InstanceCreateFlags, VkInstanceCreateFlags>::type get_default_create_flags (std::vector<std::string> const &)
32+ {
33+ if constexpr (bindingType == vkb::BindingType::Cpp)
34+ {
35+ return vk::InstanceCreateFlags{};
36+ }
37+ else
38+ {
39+ return 0 ;
40+ }
41+ }
42+
43+ void const *get_default_pNext (std::vector<std::string> const &, std::vector<std::string> const &)
44+ {
45+ return nullptr ;
46+ }
47+ } // namespace
4548
4649/* *
4750 * @brief A wrapper class for InstanceType
4851 *
49- * This class is responsible for initializing the dispatcher, enumerating over all available extensions and validation layers
50- * enabling them if they exist, setting up debug messaging and querying all the physical devices existing on the machine .
52+ * This class is responsible for checking the API version, checking for required and optional extensions and layers, creating the Vulkan
53+ * instance and initializing the default dispatcher .
5154 */
5255template <vkb::BindingType bindingType>
5356class Instance
5457{
5558 public:
5659 using InstanceCreateFlagsType = typename std::conditional<bindingType == vkb::BindingType::Cpp, vk::InstanceCreateFlags, VkInstanceCreateFlags>::type;
5760 using InstanceType = typename std::conditional<bindingType == vkb::BindingType::Cpp, vk::Instance, VkInstance>::type;
58- using LayerSettingType = typename std::conditional<bindingType == vkb::BindingType::Cpp, vk::LayerSettingEXT, VkLayerSettingEXT>::type;
59- using SurfaceType = typename std::conditional<bindingType == vkb::BindingType::Cpp, vk::SurfaceKHR, VkSurfaceKHR>::type;
6061
6162 public:
6263 /* *
@@ -70,32 +71,15 @@ class Instance
7071 * @throws runtime_error if a required layer or extension is not available
7172 */
7273 Instance (
73- std::string const &application_name,
74- uint32_t api_version = VK_API_VERSION_1_1,
75- std::unordered_map<std::string, vkb::RequestMode> const &requested_layers = {},
76- std::unordered_map<std::string, vkb::RequestMode> const &requested_extensions = {},
77- std::function<void *(std::vector<std::string> const &, std::vector<std::string> const &)> const &get_pNext =
78- [](std::vector<std::string> const &, std::vector<std::string> const &) { return nullptr ; },
79- std::function<InstanceCreateFlagsType(std::vector<std::string> const &)> const &get_create_flags =
80- [](std::vector<std::string> const &) {
81- if constexpr (bindingType == vkb::BindingType::Cpp)
82- {
83- return vk::InstanceCreateFlags{};
84- }
85- else
86- {
87- return 0 ;
88- }
89- });
74+ std::string const &application_name,
75+ uint32_t api_version = VK_API_VERSION_1_1,
76+ std::unordered_map<std::string, vkb::RequestMode> const &requested_layers = {},
77+ std::unordered_map<std::string, vkb::RequestMode> const &requested_extensions = {},
78+ std::function<void const *(std::vector<std::string> const &, std::vector<std::string> const &)> const &get_pNext = get_default_pNext,
79+ std::function<InstanceCreateFlagsType(std::vector<std::string> const &)> const &get_create_flags = get_default_create_flags);
9080
91- /* *
92- * @brief Queries the GPUs of a InstanceType that is already created
93- * @param instance A valid InstanceType
94- * @param externally_enabled_extensions List of extensions that have been enabled, used for following checks e.g. during device creation
95- * @param needsToInitializeDispatcher If the sample uses the C-bindings and some "non-standard" initialization, the dispatcher needs to be initialized
96- */
97- Instance (vk::Instance instance, const std::vector<const char *> &externally_enabled_extensions = {}, bool needsToInitializeDispatcher = false );
98- Instance (VkInstance instance, const std::vector<const char *> &externally_enabled_extensions = {});
81+ Instance (vk::Instance instance, std::vector<char const *> const &externally_enabled_extensions = {}, bool needsToInitializeDispatcher = false );
82+ Instance (VkInstance instance, std::vector<char const *> const &externally_enabled_extensions = {});
9983
10084 Instance (Instance const &) = delete ;
10185 Instance (Instance &&) = delete ;
@@ -105,37 +89,24 @@ class Instance
10589 Instance &operator =(Instance const &) = delete ;
10690 Instance &operator =(Instance &&) = delete ;
10791
108- const std::vector<std::string> & get_extensions ();
92+ std::vector<std::string> const & get_enabled_extensions ();
10993
11094 InstanceType get_handle () const ;
11195
11296 /* *
11397 * @brief Checks if the given extension is enabled in the InstanceType
11498 * @param extension An extension to check
11599 */
116- bool is_enabled (char const *extension) const ;
100+ bool is_extension_enabled (char const *extension) const ;
117101
118102 private:
119103 std::vector<std::string> enabled_extensions; // The enabled extensions
120104 vk::Instance handle; // The Vulkan instance
121-
122- #if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
123- vk::DebugReportCallbackEXT debug_report_callback; // The debug report callback
124- vk::DebugUtilsMessengerEXT debug_utils_messenger; // Debug utils messenger callback for VK_EXT_Debug_Utils
125- #endif
126105};
127106
128107using InstanceC = Instance<vkb::BindingType::C>;
129108using InstanceCpp = Instance<vkb::BindingType::Cpp>;
130- } // namespace core
131- } // namespace vkb
132-
133- #include " core/physical_device.h"
134109
135- namespace vkb
136- {
137- namespace core
138- {
139110namespace
140111{
141112inline bool enable_extension (std::string const &requested_extension,
@@ -184,39 +155,15 @@ inline bool
184155
185156 return is_available;
186157}
187-
188- inline bool validate_layers (std::vector<char const *> const &required, std::vector<vk::LayerProperties> const &available)
189- {
190- for (auto layer : required)
191- {
192- bool found = false ;
193- for (auto &available_layer : available)
194- {
195- if (strcmp (available_layer.layerName , layer) == 0 )
196- {
197- found = true ;
198- break ;
199- }
200- }
201-
202- if (!found)
203- {
204- LOGE (" Validation Layer {} not found" , layer);
205- return false ;
206- }
207- }
208-
209- return true ;
210- }
211158} // namespace
212159
213160template <vkb::BindingType bindingType>
214- inline Instance<bindingType>::Instance(std::string const &application_name,
215- uint32_t api_version,
216- std::unordered_map<std::string, vkb::RequestMode> const &requested_layers,
217- std::unordered_map<std::string, vkb::RequestMode> const &requested_extensions,
218- std::function<void *(std::vector<std::string> const &, std::vector<std::string> const &)> const &get_pNext,
219- std::function<InstanceCreateFlagsType(std::vector<std::string> const &)> const &get_create_flags)
161+ inline Instance<bindingType>::Instance(std::string const &application_name,
162+ uint32_t api_version,
163+ std::unordered_map<std::string, vkb::RequestMode> const &requested_layers,
164+ std::unordered_map<std::string, vkb::RequestMode> const &requested_extensions,
165+ std::function<void const *(std::vector<std::string> const &, std::vector<std::string> const &)> const &get_pNext,
166+ std::function<InstanceCreateFlagsType(std::vector<std::string> const &)> const &get_create_flags)
220167{
221168 // check API version
222169 LOGI (" Requesting Vulkan API version {}.{}" , VK_VERSION_MAJOR (api_version), VK_VERSION_MINOR (api_version));
@@ -266,7 +213,7 @@ inline Instance<bindingType>::Instance(std::string const
266213
267214 if (contains (enabled_layers, " VK_LAYER_KHRONOS_validation" ))
268215 {
269- const std::string validation_layer_name = " VK_LAYER_KHRONOS_validation" ;
216+ std::string const validation_layer_name = " VK_LAYER_KHRONOS_validation" ;
270217 std::vector<vk::ExtensionProperties> available_layer_instance_extensions = vk::enumerateInstanceExtensionProperties (validation_layer_name);
271218 available_extensions.insert (available_extensions.end (),
272219 available_layer_instance_extensions.begin (),
@@ -288,7 +235,7 @@ inline Instance<bindingType>::Instance(std::string const
288235 }
289236 }
290237 }
291- std::vector<const char *> enabled_extensions_cstr;
238+ std::vector<char const *> enabled_extensions_cstr;
292239 for (auto &extension : enabled_extensions)
293240 {
294241 enabled_extensions_cstr.push_back (extension.c_str ());
@@ -312,21 +259,10 @@ inline Instance<bindingType>::Instance(std::string const
312259
313260 // Need to load volk for all the not-yet Vulkan-Hpp calls
314261 volkLoadInstance (handle);
315-
316- #if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
317- if (contains (enabled_extensions, VK_EXT_DEBUG_UTILS_EXTENSION_NAME))
318- {
319- debug_utils_messenger = handle.createDebugUtilsMessengerEXT (vkb::core::getDefaultDebugUtilsMessengerCreateInfoEXT ());
320- }
321- else if (contains (enabled_extensions, VK_EXT_DEBUG_REPORT_EXTENSION_NAME))
322- {
323- debug_report_callback = handle.createDebugReportCallbackEXT (vkb::core::getDefaultDebugReportCallbackCreateInfoEXT ());
324- }
325- #endif
326262}
327263
328264template <vkb::BindingType bindingType>
329- inline Instance<bindingType>::Instance(vk::Instance instance, const std::vector<const char *> &externally_enabled_extensions, bool needsToInitializeDispatcher) :
265+ inline Instance<bindingType>::Instance(vk::Instance instance, std::vector<char const *> const &externally_enabled_extensions, bool needsToInitializeDispatcher) :
330266 handle{instance}
331267{
332268 if (needsToInitializeDispatcher)
@@ -350,38 +286,19 @@ inline Instance<bindingType>::Instance(vk::Instance instance, const std::vector<
350286}
351287
352288template <vkb::BindingType bindingType>
353- inline Instance<bindingType>::Instance(VkInstance instance, const std::vector<const char *> &externally_enabled_extensions) :
289+ inline Instance<bindingType>::Instance(VkInstance instance, std::vector<char const *> const &externally_enabled_extensions) :
354290 Instance<bindingType>(static_cast <vk::Instance>(instance), externally_enabled_extensions, true )
355291{}
356292
357293template <vkb::BindingType bindingType>
358294inline Instance<bindingType>::~Instance ()
359295{
360- #if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
361- if (debug_utils_messenger)
362- {
363- handle.destroyDebugUtilsMessengerEXT (debug_utils_messenger);
364- }
365- if (debug_report_callback)
366- {
367- handle.destroyDebugReportCallbackEXT (debug_report_callback);
368- }
369- #endif
370-
371296 if (handle)
372297 {
373298 handle.destroy ();
374299 }
375300}
376301
377- #if defined(USE_VALIDATION_LAYERS)
378- # undef USE_VALIDATION_LAYERS
379- #endif
380-
381- #if defined(USE_VALIDATION_LAYER_FEATURES)
382- # undef USE_VALIDATION_LAYER_FEATURES
383- #endif
384-
385302template <vkb::BindingType bindingType>
386303inline typename Instance<bindingType>::InstanceType Instance<bindingType>::get_handle() const
387304{
@@ -393,18 +310,16 @@ inline typename Instance<bindingType>::InstanceType Instance<bindingType>::get_h
393310 {
394311 return static_cast <VkInstance>(handle);
395312 }
396- return handle;
397313}
398314
399315template <vkb::BindingType bindingType>
400- inline bool Instance<bindingType>::is_enabled (char const *extension) const
316+ inline bool Instance<bindingType>::is_extension_enabled (char const *extension) const
401317{
402- return std::ranges::find_if (enabled_extensions, [extension](std::string const &enabled_extension) { return enabled_extension == extension; }) !=
403- enabled_extensions.end ();
318+ return std::ranges::any_of (enabled_extensions, [extension](std::string const &enabled_extension) { return enabled_extension == extension; });
404319}
405320
406321template <vkb::BindingType bindingType>
407- inline std::vector<std::string> const &Instance<bindingType>::get_extensions ()
322+ inline std::vector<std::string> const &Instance<bindingType>::get_enabled_extensions ()
408323{
409324 return enabled_extensions;
410325}
0 commit comments