pub struct Instance { /* private fields */ }
Expand description
An instance of a Vulkan context. This is the main object that should be created by an application before everything else.
Application and engine info
When you create an instance, you have the possibility to set information about your application and its engine.
Providing this information allows for example the driver to let the user configure the driver’s behavior for your application alone through a control panel.
use vulkano::instance::{Instance, InstanceCreateInfo, InstanceExtensions};
use vulkano::Version;
let _instance = Instance::new(InstanceCreateInfo::application_from_cargo_toml()).unwrap();
API versions
Both an Instance
and a Device
have a highest version of the Vulkan
API that they support. This places a limit on what Vulkan functions and features are available
to use when used on a particular instance or device. It is possible for the instance and the
device to support different versions. The supported version for an instance can be queried
before creation with
FunctionPointers::api_version
,
while for a device it can be retrieved with
PhysicalDevice::api_version
.
When creating an Instance
, you have to specify a maximum API version that you will use.
This restricts the API version that is available for the instance and any devices created from
it. For example, if both instance and device potentially support Vulkan 1.2, but you specify
1.1 as the maximum API version when creating the Instance
, then you can only use Vulkan 1.1
functions, even though they could theoretically support a higher version. You can think of it
as a promise never to use any functionality from a higher version.
The maximum API version is not a minimum, so it is possible to set it to a higher version than
what the instance or device inherently support. The final API version that you are able to use
on an instance or device is the lower of the supported API version and the chosen maximum API
version of the Instance
.
Due to a quirk in how the Vulkan 1.0 specification was written, if the instance only
supports Vulkan 1.0, then it is not possible to specify a maximum API version higher than 1.0.
Trying to create an Instance
will return an IncompatibleDriver
error. Consequently, it is
not possible to use a higher device API version with an instance that only supports 1.0.
Extensions
When creating an Instance
, you must provide a list of extensions that must be enabled on the
newly-created instance. Trying to enable an extension that is not supported by the system will
result in an error.
Contrary to OpenGL, it is not possible to use the features of an extension if it was not explicitly enabled.
Extensions are especially important to take into account if you want to render images on the
screen, as the only way to do so is to use the VK_KHR_surface
extension. More information
about this in the swapchain
module.
For example, here is how we create an instance with the VK_KHR_surface
and
VK_KHR_android_surface
extensions enabled, which will allow us to render images to an
Android screen. You can compile and run this code on any system, but it is highly unlikely to
succeed on anything else than an Android-running device.
use vulkano::instance::{Instance, InstanceCreateInfo, InstanceExtensions};
use vulkano::Version;
let extensions = InstanceExtensions {
khr_surface: true,
khr_android_surface: true,
.. InstanceExtensions::none()
};
let instance = match Instance::new(InstanceCreateInfo {
enabled_extensions: extensions,
..Default::default()
}) {
Ok(i) => i,
Err(err) => panic!("Couldn't build instance: {:?}", err)
};
Layers
When creating an Instance
, you have the possibility to pass a list of layers that will
be activated on the newly-created instance. The list of available layers can be retrieved by
calling the layers_list
function.
A layer is a component that will hook and potentially modify the Vulkan function calls. For example, activating a layer could add a frames-per-second counter on the screen, or it could send information to a debugger that will debug your application.
Note: From an application’s point of view, layers “just exist”. In practice, on Windows and Linux, layers can be installed by third party installers or by package managers and can also be activated by setting the value of the
VK_INSTANCE_LAYERS
environment variable before starting the program. See the documentation of the official Vulkan loader for these platforms.
Note: In practice, the most common use of layers right now is for debugging purposes. To do so, you are encouraged to set the
VK_INSTANCE_LAYERS
environment variable on Windows or Linux instead of modifying the source code of your program. For example:export VK_INSTANCE_LAYERS=VK_LAYER_LUNARG_api_dump
on Linux if you installed the Vulkan SDK will print the list of raw Vulkan function calls.
Example
// For the sake of the example, we activate all the layers that
// contain the word "foo" in their description.
let layers: Vec<_> = instance::layers_list()?
.filter(|l| l.description().contains("foo"))
.collect();
let instance = Instance::new(InstanceCreateInfo {
enabled_layers: layers.iter().map(|l| l.name().to_owned()).collect(),
..Default::default()
})?;
Implementations
sourceimpl Instance
impl Instance
sourcepub fn new(
create_info: InstanceCreateInfo
) -> Result<Arc<Instance>, InstanceCreationError>
pub fn new(
create_info: InstanceCreateInfo
) -> Result<Arc<Instance>, InstanceCreationError>
Creates a new Instance
.
Panics
- Panics if any version numbers in
create_info
contain a field too large to be converted into a Vulkan version number. - Panics if
create_info.max_api_version
is not at leastV1_0
.
sourcepub unsafe fn with_debug_utils_messengers(
create_info: InstanceCreateInfo,
debug_utils_messengers: impl IntoIterator<Item = DebugUtilsMessengerCreateInfo>
) -> Result<Arc<Instance>, InstanceCreationError>
pub unsafe fn with_debug_utils_messengers(
create_info: InstanceCreateInfo,
debug_utils_messengers: impl IntoIterator<Item = DebugUtilsMessengerCreateInfo>
) -> Result<Arc<Instance>, InstanceCreationError>
Creates a new Instance
with debug messengers to use during the creation and destruction
of the instance.
The debug messengers are not used at any other time,
DebugUtilsMessenger
should be used for
that.
If debug_utils_messengers
is not empty, the ext_debug_utils
extension must be set in
enabled_extensions
.
Panics
- Panics if the
message_severity
ormessage_type
members of any element ofdebug_utils_messengers
are empty.
Safety
- The
user_callback
of each element ofdebug_utils_messengers
must not make any calls to the Vulkan API.
sourcepub fn api_version(&self) -> Version
pub fn api_version(&self) -> Version
Returns the Vulkan version supported by the instance.
This is the lower of the
driver’s supported version and
max_api_version
.
sourcepub fn max_api_version(&self) -> Version
pub fn max_api_version(&self) -> Version
Returns the maximum Vulkan version that was specified when creating the instance.
sourcepub fn fns(&self) -> &InstanceFunctions
pub fn fns(&self) -> &InstanceFunctions
Grants access to the Vulkan functions of the instance.
sourcepub fn enabled_extensions(&self) -> &InstanceExtensions
pub fn enabled_extensions(&self) -> &InstanceExtensions
Returns the extensions that have been enabled on the instance.
sourcepub fn enabled_layers(&self) -> &[String]
pub fn enabled_layers(&self) -> &[String]
Returns the layers that have been enabled on the instance.
Trait Implementations
sourceimpl VulkanObject for Instance
impl VulkanObject for Instance
sourcefn internal_object(&self) -> Instance
fn internal_object(&self) -> Instance
Returns a reference to the object.
impl Eq for Instance
impl RefUnwindSafe for Instance
impl UnwindSafe for Instance
Auto Trait Implementations
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more