logo
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},
    Version, VulkanLibrary,
};

let library = VulkanLibrary::new().unwrap();
let _instance = Instance::new(
    library,
    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 VulkanLibrary::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},
    Version, VulkanLibrary,
};

let library = VulkanLibrary::new()
    .unwrap_or_else(|err| panic!("Couldn't load Vulkan library: {:?}", err));

let extensions = InstanceExtensions {
    khr_surface: true,
    khr_android_surface: true,
    .. InstanceExtensions::empty()
};

let instance = Instance::new(
    library,
    InstanceCreateInfo {
        enabled_extensions: extensions,
        ..Default::default()
    },
)
.unwrap_or_else(|err| panic!("Couldn't create 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 layer_properties method of VulkanLibrary.

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

let library = VulkanLibrary::new()?;

// For the sake of the example, we activate all the layers that
// contain the word "foo" in their description.
let layers: Vec<_> = library.layer_properties()?
    .filter(|l| l.description().contains("foo"))
    .collect();

let instance = Instance::new(
    library,
    InstanceCreateInfo {
        enabled_layers: layers.iter().map(|l| l.name().to_owned()).collect(),
        ..Default::default()
    },
)?;

Implementations

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 least V1_0.

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 or message_type members of any element of debug_utils_messengers are empty.
Safety
  • The user_callback of each element of debug_utils_messengers must not make any calls to the Vulkan API.

Returns the Vulkan library used to create this instance.

Returns the Vulkan version supported by the instance.

This is the lower of the driver’s supported version and max_api_version.

Returns the maximum Vulkan version that was specified when creating the instance.

Returns pointers to the raw Vulkan functions of the instance.

Returns the extensions that have been enabled on the instance.

Returns the layers that have been enabled on the instance.

Returns an iterator that enumerates the physical devices available.

Example

for physical_device in instance.enumerate_physical_devices().unwrap() {
    println!("Available device: {}", physical_device.properties().device_name);
}

Trait Implementations

Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
The type of the object.
Returns a reference to the object.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.