Class Instance¶
Defined in File instance.hpp
Class Documentation¶
-
class Instance¶
RAII wrapper class for
VkInstance.Note
Design decision: For 2 reasons, it was deliberately decided to avoid checking inside of the
Instanceconstructor which of the requested instance layers and instance extensions are available and only to enable those which are available. Reason 1: This would require a class interface in which the programmer would have to check which instance layer or instance extension is enabled after theInstanceconstructor has been called. This would make using this wrapper more complex, and the programmer could even forget to do this and attempt to use instance layer or instance extensions which are not available. It is not the responsibility of this class to do this! In external code, you must check what is supported before calling this constructor and it is your responsibility to store this data according to the needs of the application. Reason 2: If you pass any instance layer or instance extension which is not supported by the system, an exception is thrown. We believe this follows one of the most important design rules from Scott Meyers: Make APIs easy to use correctly and as hard to use incorrectly.Public Functions
-
Instance(const std::span<const char*> instance_layers = {}, const std::span<const char*> instance_extensions = {})¶
Default constructor.
Note
Design decision: You might be surprised to find out that we decided to check if
vkEnumerateInstanceVersionandvkCreateInstanceare valid function pointers. We do this because we load Vulkan dynamically with volk metaloader. The programmer might forget to callvolkInitializein external code before calling the constructor of theInstanceclass. If this was the case, bothvkEnumerateInstanceVersionandvkCreateInstancewould still benullptr. The reason why this class is not callingvolkInitializeinternally is becausevolkInitializewill init those functions which are required for checking for available instance layers and instance extensions, which need to be checked before the constructor of theInstanceclass is called! So the order is: 1) CallvolkInitializein external code, 2) check for available instance layers and instance extensions, 3) create an instance of theInstanceclass. In this constructor, aftervkCreateInstanceand subsequentlyvolkLoadInstanceOnlyhave been called, we even check ifvkDestroyInstanceis a valid function pointer. We do this just to be sure, and it is strictly not necessary. IfvkDestroyInstancewould not be available aftervolkLoadInstanceOnly, there is either a bug in volk library or something is fundamentally wrong with the installed Vulkan runtime.Warning
Make sure to understand: It is the responsibility of the external code to ensure that all instance layers and all instance extensions which are passed into the constructor of the
Instanceclass are actually available on the system! Useis_layer_supportedandis_extension_supportedto check which instance layers and instance extensions are available. This constructor will not check this again because if a single instance layer is not present, the creation withvkCreateInstancewill fail and an exception will be thrown (VK_ERROR_LAYER_NOT_PRESENT). The same also applies if a single instance extension is not present, which will also result in an exception (VK_ERROR_EXTENSION_NOT_PRESENT). Furthermore, also make sure that any instance layer you pass really is an instance layer, not an instance extension or a device extension! Because all of these are just strings, they are easy to mix up. This also applies to instance extensions (or device extensions in theDevicewrapper). In case you do mix them up here, theInstancewrapper will throw an exception (VK_ERROR_LAYER_NOT_PRESENTorVK_ERROR_EXTENSION_NOT_PRESENT, depending on what you mixed up).- Parameters:
instance_layers – The required instance layers (can be empty).
instance_extensions – The required instance extensions (can be empty).
- Throws:
InexorException – if function pointer
vkEnumerateInstanceVersionis not available.InexorException – if function pointer
vkCreateInstanceis not available.VulkanException – if
vkEnumerateInstanceVersiondoes not returnVK_SUCCESS.VulkanException – if
vkCreateInstancedoes not returnVK_SUCCESS.InexorException – if function pointer
vkDestroyInstanceis not available aftervolkLoadInstanceOnly.
-
~Instance()¶
-
inline VkInstance instance() const¶
Note
Design decision: Initially we thought we could make access to
m_instancefully private and restrict access to it to some friend classes to avoid public access. However, we can’t make access to theVkInstanceprivate because several public functions require this.
Public Static Attributes
-
static constexpr std::uint32_t REQUIRED_VK_API_VERSION = {VK_API_VERSION_1_2}¶
This is the version of Vulkan API that we use in the entire engine.
Private Members
-
VkInstance m_instance = {VK_NULL_HANDLE}¶
-
Instance(const std::span<const char*> instance_layers = {}, const std::span<const char*> instance_extensions = {})¶