Skip to content

Commit 94b9a65

Browse files
authored
Move requesting gpu features from VulkanSample::prepare into the vkb::core::Device constructor. (#1428)
1 parent 5bfa6ae commit 94b9a65

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

framework/core/device.h

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ class Device
8282
* @param surface The surface
8383
* @param debug_utils The debug utils to be associated to this device
8484
* @param requested_extensions (Optional) List of required device extensions and whether support is optional or not
85+
* @param request_gpu_features (Optional) A function that will be called to request specific gpu features before device creation
8586
*/
86-
Device(PhysicalDevice<bindingType> &gpu,
87-
SurfaceType surface,
88-
std::unique_ptr<DebugUtilsType> &&debug_utils,
89-
std::unordered_map<const char *, bool> const &requested_extensions = {});
87+
Device(PhysicalDevice<bindingType> &gpu,
88+
SurfaceType surface,
89+
std::unique_ptr<DebugUtilsType> &&debug_utils,
90+
std::unordered_map<const char *, bool> const &requested_extensions = {},
91+
std::function<void(vkb::core::PhysicalDevice<bindingType> &)> request_gpu_features = {});
9092

9193
/**
9294
* @brief Device constructor
@@ -134,7 +136,7 @@ class Device
134136
void flush_command_buffer_impl(
135137
vk::Device device, vk::CommandBuffer command_buffer, vk::Queue queue, bool free = true, vk::Semaphore signal_semaphore = nullptr) const;
136138
vkb::core::HPPQueue const &get_queue_by_flags_impl(vk::QueueFlags queue_flags, uint32_t queue_index) const;
137-
void init(std::unordered_map<const char *, bool> const &requested_extensions);
139+
void init(std::unordered_map<const char *, bool> const &requested_extensions, std::function<void(vkb::core::PhysicalDevice<bindingType> &)> request_gpu_features);
138140

139141
private:
140142
std::unique_ptr<vkb::core::CommandPoolCpp> command_pool;
@@ -161,23 +163,25 @@ namespace core
161163
{
162164

163165
template <>
164-
inline Device<vkb::BindingType::Cpp>::Device(vkb::core::PhysicalDeviceCpp &gpu,
165-
vk::SurfaceKHR surface,
166-
std::unique_ptr<vkb::core::HPPDebugUtils> &&debug_utils,
167-
std::unordered_map<const char *, bool> const &requested_extensions) :
166+
inline Device<vkb::BindingType::Cpp>::Device(vkb::core::PhysicalDeviceCpp &gpu,
167+
vk::SurfaceKHR surface,
168+
std::unique_ptr<vkb::core::HPPDebugUtils> &&debug_utils,
169+
std::unordered_map<const char *, bool> const &requested_extensions,
170+
std::function<void(vkb::core::PhysicalDeviceCpp &)> request_gpu_features) :
168171
vkb::core::VulkanResourceCpp<vk::Device>{nullptr, this}, debug_utils{std::move(debug_utils)}, gpu{gpu}, resource_cache{*this}, surface(surface)
169172
{
170-
init(requested_extensions);
173+
init(requested_extensions, request_gpu_features);
171174
}
172175

173176
template <>
174-
inline Device<vkb::BindingType::C>::Device(vkb::core::PhysicalDeviceC &gpu,
175-
VkSurfaceKHR surface,
176-
std::unique_ptr<vkb::DebugUtils> &&debug_utils,
177-
std::unordered_map<const char *, bool> const &requested_extensions) :
177+
inline Device<vkb::BindingType::C>::Device(vkb::core::PhysicalDeviceC &gpu,
178+
VkSurfaceKHR surface,
179+
std::unique_ptr<vkb::DebugUtils> &&debug_utils,
180+
std::unordered_map<const char *, bool> const &requested_extensions,
181+
std::function<void(vkb::core::PhysicalDeviceC &)> request_gpu_features) :
178182
vkb::core::VulkanResourceC<VkDevice>{VK_NULL_HANDLE, this}, debug_utils{reinterpret_cast<vkb::core::HPPDebugUtils *>(debug_utils.release())}, gpu{reinterpret_cast<vkb::core::PhysicalDeviceCpp &>(gpu)}, resource_cache{*reinterpret_cast<vkb::core::DeviceCpp *>(this)}, surface(static_cast<vk::SurfaceKHR>(surface))
179183
{
180-
init(requested_extensions);
184+
init(requested_extensions, request_gpu_features);
181185
}
182186

183187
template <>
@@ -615,7 +619,7 @@ vkb::core::HPPQueue const &Device<bindingType>::get_queue_by_flags_impl(vk::Queu
615619
}
616620

617621
template <vkb::BindingType bindingType>
618-
inline void Device<bindingType>::init(std::unordered_map<const char *, bool> const &requested_extensions)
622+
inline void Device<bindingType>::init(std::unordered_map<const char *, bool> const &requested_extensions, std::function<void(vkb::core::PhysicalDevice<bindingType> &)> request_gpu_features)
619623
{
620624
LOGI("Selected GPU: {}", *gpu.get_properties().deviceName);
621625

@@ -719,6 +723,15 @@ inline void Device<bindingType>::init(std::unordered_map<const char *, bool> con
719723
}
720724
}
721725

726+
if constexpr (bindingType == vkb::BindingType::Cpp)
727+
{
728+
request_gpu_features(gpu);
729+
}
730+
else
731+
{
732+
request_gpu_features(reinterpret_cast<vkb::core::PhysicalDeviceC &>(gpu));
733+
}
734+
722735
// Latest requested feature will have the pNext's all set up for device creation.
723736
vk::DeviceCreateInfo create_info{.pNext = gpu.get_extension_feature_chain(),
724737
.queueCreateInfoCount = static_cast<uint32_t>(queue_create_infos.size()),

framework/vulkan_sample.h

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -501,18 +501,21 @@ inline void VulkanSample<bindingType>::add_layer_setting(LayerSettingType const
501501
}
502502

503503
template <vkb::BindingType bindingType>
504-
inline std::unique_ptr<typename vkb::core::Device<bindingType>> VulkanSample<bindingType>::create_device(vkb::core::PhysicalDevice<bindingType> &gpu)
504+
inline std::unique_ptr<typename vkb::core::Device<bindingType>>
505+
VulkanSample<bindingType>::create_device(vkb::core::PhysicalDevice<bindingType> &gpu)
505506
{
506507
if constexpr (bindingType == BindingType::Cpp)
507508
{
508-
return std::make_unique<vkb::core::DeviceCpp>(gpu, surface, std::move(debug_utils), get_device_extensions());
509+
return std::make_unique<vkb::core::DeviceCpp>(
510+
gpu, surface, std::move(debug_utils), get_device_extensions(), [this](vkb::core::PhysicalDeviceCpp &gpu) { request_gpu_features(gpu); });
509511
}
510512
else
511513
{
512514
return std::make_unique<vkb::core::DeviceC>(gpu,
513515
static_cast<VkSurfaceKHR>(surface),
514516
std::unique_ptr<vkb::DebugUtils>(reinterpret_cast<vkb::DebugUtils *>(debug_utils.release())),
515-
get_device_extensions());
517+
get_device_extensions(),
518+
[this](vkb::core::PhysicalDeviceC &gpu) { request_gpu_features(gpu); });
516519
}
517520
}
518521

@@ -1074,9 +1077,6 @@ inline bool VulkanSample<bindingType>::prepare(const ApplicationOptions &options
10741077
instance.reset(reinterpret_cast<vkb::core::InstanceCpp *>(create_instance().release()));
10751078
}
10761079

1077-
// initialize C++-Bindings default dispatcher, second step
1078-
VULKAN_HPP_DEFAULT_DISPATCHER.init(instance->get_handle());
1079-
10801080
// Getting a valid vulkan surface from the platform
10811081
surface = static_cast<vk::SurfaceKHR>(window->create_surface(reinterpret_cast<vkb::core::InstanceC &>(*instance)));
10821082
if (!surface)
@@ -1093,16 +1093,6 @@ inline bool VulkanSample<bindingType>::prepare(const ApplicationOptions &options
10931093
gpu.get_mutable_requested_features().textureCompressionASTC_LDR = true;
10941094
}
10951095

1096-
// Request sample required GPU features
1097-
if constexpr (bindingType == BindingType::Cpp)
1098-
{
1099-
request_gpu_features(gpu);
1100-
}
1101-
else
1102-
{
1103-
request_gpu_features(reinterpret_cast<vkb::core::PhysicalDeviceC &>(gpu));
1104-
}
1105-
11061096
// Creating vulkan device, specifying the swapchain extension always
11071097
// If using VK_EXT_headless_surface, we still create and use a swap-chain
11081098
{

0 commit comments

Comments
 (0)