Added const to builder functions and many other functions

This commit is contained in:
Charles Giessen 2020-03-07 22:21:27 -07:00
parent 5ae586b559
commit 10d9c2ee1b
2 changed files with 64 additions and 47 deletions

View File

@ -253,11 +253,11 @@ SystemInfo::SystemInfo () {
if (strcmp (layer.layerName, detail::validation_layer_name) == 0) if (strcmp (layer.layerName, detail::validation_layer_name) == 0)
validation_layers_available = true; validation_layers_available = true;
} }
bool SystemInfo::is_extension_available (const char* extension_name) { bool SystemInfo::is_extension_available (const char* extension_name) const {
if (!extension_name) return false; if (!extension_name) return false;
return detail::check_extension_supported (available_extensions, extension_name); return detail::check_extension_supported (available_extensions, extension_name);
} }
bool SystemInfo::is_layer_available (const char* layer_name) { bool SystemInfo::is_layer_available (const char* layer_name) const {
if (!layer_name) return false; if (!layer_name) return false;
return detail::check_layer_supported (available_layers, layer_name); return detail::check_layer_supported (available_layers, layer_name);
} }
@ -270,9 +270,9 @@ void destroy_instance (Instance instance) {
} }
} }
SystemInfo InstanceBuilder::get_system_info () { return system; } SystemInfo InstanceBuilder::get_system_info () const { return system; }
detail::Expected<Instance, detail::Error<InstanceError>> InstanceBuilder::build () { detail::Expected<Instance, detail::Error<InstanceError>> InstanceBuilder::build () const {
VkApplicationInfo app_info = {}; VkApplicationInfo app_info = {};
app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
@ -323,6 +323,8 @@ detail::Expected<Instance, detail::Error<InstanceError>> InstanceBuilder::build
return detail::Error<InstanceError>{ InstanceError::requested_layers_not_present }; return detail::Error<InstanceError>{ InstanceError::requested_layers_not_present };
} }
std::vector<VkBaseOutStructure*> pNext_chain;
VkDebugUtilsMessengerCreateInfoEXT messengerCreateInfo = {}; VkDebugUtilsMessengerCreateInfoEXT messengerCreateInfo = {};
if (info.use_debug_messenger) { if (info.use_debug_messenger) {
messengerCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; messengerCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
@ -330,7 +332,7 @@ detail::Expected<Instance, detail::Error<InstanceError>> InstanceBuilder::build
messengerCreateInfo.messageSeverity = info.debug_message_severity; messengerCreateInfo.messageSeverity = info.debug_message_severity;
messengerCreateInfo.messageType = info.debug_message_type; messengerCreateInfo.messageType = info.debug_message_type;
messengerCreateInfo.pfnUserCallback = info.debug_callback; messengerCreateInfo.pfnUserCallback = info.debug_callback;
info.pNext_elements.push_back (reinterpret_cast<VkBaseOutStructure*> (&messengerCreateInfo)); pNext_chain.push_back (reinterpret_cast<VkBaseOutStructure*> (&messengerCreateInfo));
} }
VkValidationFeaturesEXT features{}; VkValidationFeaturesEXT features{};
@ -341,7 +343,7 @@ detail::Expected<Instance, detail::Error<InstanceError>> InstanceBuilder::build
features.pEnabledValidationFeatures = info.enabled_validation_features.data (); features.pEnabledValidationFeatures = info.enabled_validation_features.data ();
features.disabledValidationFeatureCount = info.disabled_validation_features.size (); features.disabledValidationFeatureCount = info.disabled_validation_features.size ();
features.pDisabledValidationFeatures = info.disabled_validation_features.data (); features.pDisabledValidationFeatures = info.disabled_validation_features.data ();
info.pNext_elements.push_back (reinterpret_cast<VkBaseOutStructure*> (&features)); pNext_chain.push_back (reinterpret_cast<VkBaseOutStructure*> (&features));
} }
VkValidationFlagsEXT checks{}; VkValidationFlagsEXT checks{};
@ -350,12 +352,12 @@ detail::Expected<Instance, detail::Error<InstanceError>> InstanceBuilder::build
checks.pNext = nullptr; checks.pNext = nullptr;
checks.disabledValidationCheckCount = info.disabled_validation_checks.size (); checks.disabledValidationCheckCount = info.disabled_validation_checks.size ();
checks.pDisabledValidationChecks = info.disabled_validation_checks.data (); checks.pDisabledValidationChecks = info.disabled_validation_checks.data ();
info.pNext_elements.push_back (reinterpret_cast<VkBaseOutStructure*> (&checks)); pNext_chain.push_back (reinterpret_cast<VkBaseOutStructure*> (&checks));
} }
VkInstanceCreateInfo instance_create_info = {}; VkInstanceCreateInfo instance_create_info = {};
instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
detail::setup_pNext_chain (instance_create_info, info.pNext_elements); detail::setup_pNext_chain (instance_create_info, pNext_chain);
instance_create_info.flags = info.flags; instance_create_info.flags = info.flags;
instance_create_info.pApplicationInfo = &app_info; instance_create_info.pApplicationInfo = &app_info;
instance_create_info.enabledExtensionCount = static_cast<uint32_t> (extensions.size ()); instance_create_info.enabledExtensionCount = static_cast<uint32_t> (extensions.size ());
@ -669,7 +671,7 @@ int get_present_queue_index (VkPhysicalDevice const phys_device,
PhysicalDeviceSelector::PhysicalDeviceDesc PhysicalDeviceSelector::populate_device_details ( PhysicalDeviceSelector::PhysicalDeviceDesc PhysicalDeviceSelector::populate_device_details (
VkPhysicalDevice phys_device) { VkPhysicalDevice phys_device) const {
PhysicalDeviceSelector::PhysicalDeviceDesc desc{}; PhysicalDeviceSelector::PhysicalDeviceDesc desc{};
desc.phys_device = phys_device; desc.phys_device = phys_device;
auto queue_families = detail::get_vector_noerror<VkQueueFamilyProperties> ( auto queue_families = detail::get_vector_noerror<VkQueueFamilyProperties> (
@ -682,7 +684,7 @@ PhysicalDeviceSelector::PhysicalDeviceDesc PhysicalDeviceSelector::populate_devi
return desc; return desc;
} }
PhysicalDeviceSelector::Suitable PhysicalDeviceSelector::is_device_suitable (PhysicalDeviceDesc pd) { PhysicalDeviceSelector::Suitable PhysicalDeviceSelector::is_device_suitable (PhysicalDeviceDesc pd) const {
Suitable suitable = Suitable::yes; Suitable suitable = Suitable::yes;
bool dedicated_compute = detail::get_dedicated_compute_queue_index (pd.queue_families) >= 0; bool dedicated_compute = detail::get_dedicated_compute_queue_index (pd.queue_families) >= 0;
@ -760,7 +762,7 @@ PhysicalDeviceSelector::PhysicalDeviceSelector (Instance const& instance) {
criteria.require_present = !instance.headless; criteria.require_present = !instance.headless;
} }
detail::Expected<PhysicalDevice, detail::Error<PhysicalDeviceError>> PhysicalDeviceSelector::select () { detail::Expected<PhysicalDevice, detail::Error<PhysicalDeviceError>> PhysicalDeviceSelector::select () const {
auto physical_devices = auto physical_devices =
detail::get_vector<VkPhysicalDevice> (vkEnumeratePhysicalDevices, system_info.instance); detail::get_vector<VkPhysicalDevice> (vkEnumeratePhysicalDevices, system_info.instance);
if (!physical_devices.has_value ()) { if (!physical_devices.has_value ()) {
@ -888,16 +890,16 @@ PhysicalDeviceSelector& PhysicalDeviceSelector::select_first_device_unconditiona
return *this; return *this;
} }
bool PhysicalDevice::has_dedicated_compute_queue () { bool PhysicalDevice::has_dedicated_compute_queue () const {
return detail::get_dedicated_compute_queue_index (queue_families) >= 0; return detail::get_dedicated_compute_queue_index (queue_families) >= 0;
} }
bool PhysicalDevice::has_separate_compute_queue () { bool PhysicalDevice::has_separate_compute_queue () const {
return detail::get_separate_compute_queue_index (queue_families) >= 0; return detail::get_separate_compute_queue_index (queue_families) >= 0;
} }
bool PhysicalDevice::has_dedicated_transfer_queue () { bool PhysicalDevice::has_dedicated_transfer_queue () const {
return detail::get_dedicated_transfer_queue_index (queue_families) >= 0; return detail::get_dedicated_transfer_queue_index (queue_families) >= 0;
} }
bool PhysicalDevice::has_separate_transfer_queue () { bool PhysicalDevice::has_separate_transfer_queue () const {
return detail::get_separate_transfer_queue_index (queue_families) >= 0; return detail::get_separate_transfer_queue_index (queue_families) >= 0;
} }
@ -926,7 +928,7 @@ detail::Expected<int32_t, detail::Error<QueueError>> Device::get_queue_index (Qu
return detail::Error<QueueError>{ QueueError::invalid_queue_family_index }; return detail::Error<QueueError>{ QueueError::invalid_queue_family_index };
} }
return index; return index;
} // namespace vkb }
detail::Expected<int32_t, detail::Error<QueueError>> Device::get_dedicated_queue_index (QueueType type) const { detail::Expected<int32_t, detail::Error<QueueError>> Device::get_dedicated_queue_index (QueueType type) const {
int index = -1; int index = -1;
switch (type) { switch (type) {
@ -975,7 +977,7 @@ DeviceBuilder::DeviceBuilder (PhysicalDevice phys_device) {
info.extensions_to_enable = phys_device.extensions_to_enable; info.extensions_to_enable = phys_device.extensions_to_enable;
} }
detail::Expected<Device, detail::Error<DeviceError>> DeviceBuilder::build () { detail::Expected<Device, detail::Error<DeviceError>> DeviceBuilder::build () const {
std::vector<CustomQueueDescription> queue_descriptions; std::vector<CustomQueueDescription> queue_descriptions;
queue_descriptions.insert ( queue_descriptions.insert (
@ -1108,19 +1110,24 @@ SwapchainBuilder::SwapchainBuilder (VkPhysicalDevice const physical_device,
info.graphics_queue_index = graphics_queue_index; info.graphics_queue_index = graphics_queue_index;
info.present_queue_index = present_queue_index; info.present_queue_index = present_queue_index;
} }
detail::Expected<Swapchain, detail::Error<SwapchainError>> SwapchainBuilder::build () const {
detail::Expected<Swapchain, detail::Error<SwapchainError>> SwapchainBuilder::build () { return build (VK_NULL_HANDLE);
if (info.desired_formats.size () == 0) use_default_format_selection (); }
if (info.desired_present_modes.size () == 0) use_default_present_mode_selection (); detail::Expected<Swapchain, detail::Error<SwapchainError>> SwapchainBuilder::build (
VkSwapchainKHR old_swapchain) const {
auto desired_formats = info.desired_formats;
if (desired_formats.size () == 0) add_desired_formats (desired_formats);
auto desired_present_modes = info.desired_present_modes;
if (desired_present_modes.size () == 0) add_desired_present_modes (desired_present_modes);
auto surface_support = detail::query_surface_support_details (info.physical_device, info.surface); auto surface_support = detail::query_surface_support_details (info.physical_device, info.surface);
if (!surface_support.has_value ()) if (!surface_support.has_value ())
return detail::Error<SwapchainError>{ SwapchainError::failed_query_surface_support_details, return detail::Error<SwapchainError>{ SwapchainError::failed_query_surface_support_details,
surface_support.error ().vk_result }; surface_support.error ().vk_result };
VkSurfaceFormatKHR surface_format = VkSurfaceFormatKHR surface_format =
detail::find_surface_format (surface_support.value ().formats, info.desired_formats); detail::find_surface_format (surface_support.value ().formats, desired_formats);
VkPresentModeKHR present_mode = VkPresentModeKHR present_mode =
detail::find_present_mode (surface_support.value ().present_modes, info.desired_present_modes); detail::find_present_mode (surface_support.value ().present_modes, desired_present_modes);
VkExtent2D extent = detail::find_extent ( VkExtent2D extent = detail::find_extent (
surface_support.value ().capabilities, info.desired_width, info.desired_height); surface_support.value ().capabilities, info.desired_width, info.desired_height);
@ -1155,7 +1162,7 @@ detail::Expected<Swapchain, detail::Error<SwapchainError>> SwapchainBuilder::bui
swapchain_create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; swapchain_create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
swapchain_create_info.presentMode = present_mode; swapchain_create_info.presentMode = present_mode;
swapchain_create_info.clipped = VK_TRUE; swapchain_create_info.clipped = VK_TRUE;
swapchain_create_info.oldSwapchain = info.old_swapchain; swapchain_create_info.oldSwapchain = old_swapchain;
Swapchain swapchain{}; Swapchain swapchain{};
VkResult res = vkCreateSwapchainKHR ( VkResult res = vkCreateSwapchainKHR (
info.device, &swapchain_create_info, info.allocation_callbacks, &swapchain.swapchain); info.device, &swapchain_create_info, info.allocation_callbacks, &swapchain.swapchain);
@ -1169,10 +1176,10 @@ detail::Expected<Swapchain, detail::Error<SwapchainError>> SwapchainBuilder::bui
swapchain.image_count = images.value ().size (); swapchain.image_count = images.value ().size ();
swapchain.allocation_callbacks = info.allocation_callbacks; swapchain.allocation_callbacks = info.allocation_callbacks;
return swapchain; return swapchain;
} // namespace vkb }
detail::Expected<Swapchain, detail::Error<SwapchainError>> SwapchainBuilder::recreate (Swapchain const& swapchain) { detail::Expected<Swapchain, detail::Error<SwapchainError>> SwapchainBuilder::recreate (
info.old_swapchain = swapchain.swapchain; Swapchain const& swapchain) const {
return build (); return build (swapchain.swapchain);
} }
detail::Expected<std::vector<VkImage>, detail::Error<SwapchainError>> get_swapchain_images ( detail::Expected<std::vector<VkImage>, detail::Error<SwapchainError>> get_swapchain_images (
Swapchain const& swapchain) { Swapchain const& swapchain) {
@ -1227,8 +1234,7 @@ SwapchainBuilder& SwapchainBuilder::add_fallback_format (VkSurfaceFormatKHR form
return *this; return *this;
} }
SwapchainBuilder& SwapchainBuilder::use_default_format_selection () { SwapchainBuilder& SwapchainBuilder::use_default_format_selection () {
info.desired_formats.push_back ({ VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR }); add_desired_formats (info.desired_formats);
info.desired_formats.push_back ({ VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR });
return *this; return *this;
} }
@ -1241,13 +1247,19 @@ SwapchainBuilder& SwapchainBuilder::add_fallback_present_mode (VkPresentModeKHR
return *this; return *this;
} }
SwapchainBuilder& SwapchainBuilder::use_default_present_mode_selection () { SwapchainBuilder& SwapchainBuilder::use_default_present_mode_selection () {
info.desired_present_modes.push_back (VK_PRESENT_MODE_MAILBOX_KHR); add_desired_present_modes (info.desired_present_modes);
info.desired_present_modes.push_back (VK_PRESENT_MODE_FIFO_KHR);
return *this; return *this;
} }
SwapchainBuilder& SwapchainBuilder::set_allocation_callbacks (VkAllocationCallbacks* callbacks) { SwapchainBuilder& SwapchainBuilder::set_allocation_callbacks (VkAllocationCallbacks* callbacks) {
info.allocation_callbacks = callbacks; info.allocation_callbacks = callbacks;
return *this; return *this;
} }
void SwapchainBuilder::add_desired_formats (std::vector<VkSurfaceFormatKHR>& formats) const {
formats.push_back ({ VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR });
formats.push_back ({ VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR });
}
void SwapchainBuilder::add_desired_present_modes (std::vector<VkPresentModeKHR>& modes) const {
modes.push_back (VK_PRESENT_MODE_MAILBOX_KHR);
modes.push_back (VK_PRESENT_MODE_FIFO_KHR);
}
} // namespace vkb } // namespace vkb

View File

@ -138,9 +138,9 @@ const char* to_string (SwapchainError err);
struct SystemInfo { struct SystemInfo {
SystemInfo (); SystemInfo ();
// Returns true if a layer is available // Returns true if a layer is available
bool is_layer_available (const char* layer_name); bool is_layer_available (const char* layer_name) const;
// Returns true if an extension is available // Returns true if an extension is available
bool is_extension_available (const char* extension_name); bool is_extension_available (const char* extension_name) const;
std::vector<VkLayerProperties> available_layers; std::vector<VkLayerProperties> available_layers;
std::vector<VkExtensionProperties> available_extensions; std::vector<VkExtensionProperties> available_extensions;
@ -168,10 +168,10 @@ void destroy_instance (Instance instance); // release instance resources
class InstanceBuilder { class InstanceBuilder {
public: public:
// contains useful information about the available vulkan capabilities, like layers and instance extensions. // contains useful information about the available vulkan capabilities, like layers and instance extensions.
SystemInfo get_system_info (); SystemInfo get_system_info () const;
// Create a VkInstance. Return an error if it failed. // Create a VkInstance. Return an error if it failed.
detail::Expected<Instance, detail::Error<InstanceError>> build (); detail::Expected<Instance, detail::Error<InstanceError>> build () const;
// Sets the name of the application. Defaults to "" if none is provided. // Sets the name of the application. Defaults to "" if none is provided.
InstanceBuilder& set_app_name (const char* app_name); InstanceBuilder& set_app_name (const char* app_name);
@ -289,14 +289,14 @@ struct PhysicalDevice {
VkSurfaceKHR surface = VK_NULL_HANDLE; VkSurfaceKHR surface = VK_NULL_HANDLE;
// Has a queue family that supports compute operations but not graphics nor transfer. // Has a queue family that supports compute operations but not graphics nor transfer.
bool has_dedicated_compute_queue (); bool has_dedicated_compute_queue () const;
// Has a queue family that supports transfer operations but not graphics nor compute. // Has a queue family that supports transfer operations but not graphics nor compute.
bool has_dedicated_transfer_queue (); bool has_dedicated_transfer_queue () const;
// Has a queue family that supports transfer operations but not graphics. // Has a queue family that supports transfer operations but not graphics.
bool has_separate_compute_queue (); bool has_separate_compute_queue () const;
// Has a queue family that supports transfer operations but not graphics. // Has a queue family that supports transfer operations but not graphics.
bool has_separate_transfer_queue (); bool has_separate_transfer_queue () const;
private: private:
VkPhysicalDeviceFeatures features{}; VkPhysicalDeviceFeatures features{};
@ -319,7 +319,7 @@ class PhysicalDeviceSelector {
// Requires a vkb::Instance to construct, needed to pass instance creation info. // Requires a vkb::Instance to construct, needed to pass instance creation info.
PhysicalDeviceSelector (Instance const& instance); PhysicalDeviceSelector (Instance const& instance);
detail::Expected<PhysicalDevice, detail::Error<PhysicalDeviceError>> select (); detail::Expected<PhysicalDevice, detail::Error<PhysicalDeviceError>> select () const;
// Set the surface in which the physical device should render to. // Set the surface in which the physical device should render to.
PhysicalDeviceSelector& set_surface (VkSurfaceKHR instance); PhysicalDeviceSelector& set_surface (VkSurfaceKHR instance);
@ -383,7 +383,7 @@ class PhysicalDeviceSelector {
VkPhysicalDeviceProperties device_properties; VkPhysicalDeviceProperties device_properties;
VkPhysicalDeviceMemoryProperties mem_properties; VkPhysicalDeviceMemoryProperties mem_properties;
}; };
PhysicalDeviceDesc populate_device_details (VkPhysicalDevice phys_device); PhysicalDeviceDesc populate_device_details (VkPhysicalDevice phys_device) const;
struct SelectionCriteria { struct SelectionCriteria {
PreferredDeviceType preferred_type = PreferredDeviceType::discrete; PreferredDeviceType preferred_type = PreferredDeviceType::discrete;
@ -409,7 +409,7 @@ class PhysicalDeviceSelector {
enum class Suitable { yes, partial, no }; enum class Suitable { yes, partial, no };
Suitable is_device_suitable (PhysicalDeviceDesc phys_device); Suitable is_device_suitable (PhysicalDeviceDesc phys_device) const;
}; };
// ---- Queue ---- // // ---- Queue ---- //
@ -448,7 +448,7 @@ class DeviceBuilder {
// Any features and extensions that are requested/required in PhysicalDeviceSelector are automatically enabled. // Any features and extensions that are requested/required in PhysicalDeviceSelector are automatically enabled.
DeviceBuilder (PhysicalDevice physical_device); DeviceBuilder (PhysicalDevice physical_device);
detail::Expected<Device, detail::Error<DeviceError>> build (); detail::Expected<Device, detail::Error<DeviceError>> build () const;
// For Advanced Users: specify the exact list of VkDeviceQueueCreateInfo's needed for the application. // For Advanced Users: specify the exact list of VkDeviceQueueCreateInfo's needed for the application.
// If a custom queue setup is provided, getting the queues and queue indexes is up to the application. // If a custom queue setup is provided, getting the queues and queue indexes is up to the application.
@ -501,8 +501,8 @@ class SwapchainBuilder {
uint32_t graphics_queue_index, uint32_t graphics_queue_index,
uint32_t present_queue_index); uint32_t present_queue_index);
detail::Expected<Swapchain, detail::Error<SwapchainError>> build (); detail::Expected<Swapchain, detail::Error<SwapchainError>> build () const;
detail::Expected<Swapchain, detail::Error<SwapchainError>> recreate (Swapchain const& swapchain); detail::Expected<Swapchain, detail::Error<SwapchainError>> recreate (Swapchain const& swapchain) const;
SwapchainBuilder& set_desired_format (VkSurfaceFormatKHR format); SwapchainBuilder& set_desired_format (VkSurfaceFormatKHR format);
SwapchainBuilder& add_fallback_format (VkSurfaceFormatKHR format); SwapchainBuilder& add_fallback_format (VkSurfaceFormatKHR format);
@ -516,6 +516,11 @@ class SwapchainBuilder {
SwapchainBuilder& set_allocation_callbacks (VkAllocationCallbacks* callbacks); SwapchainBuilder& set_allocation_callbacks (VkAllocationCallbacks* callbacks);
private: private:
void add_desired_formats (std::vector<VkSurfaceFormatKHR>& formats) const;
void add_desired_present_modes (std::vector<VkPresentModeKHR>& modes) const;
// for use in swapchain recreation
detail::Expected<Swapchain, detail::Error<SwapchainError>> build (VkSwapchainKHR old_swapchain) const;
struct SwapchainInfo { struct SwapchainInfo {
VkPhysicalDevice physical_device = VK_NULL_HANDLE; VkPhysicalDevice physical_device = VK_NULL_HANDLE;
VkDevice device = VK_NULL_HANDLE; VkDevice device = VK_NULL_HANDLE;