The first thing, which is necessary to use Vulkan, is to create its instance. The instance is a representation of Vulkan itself. It is the main object, which allows us to interact with Vulkan and do something useful. The minimal example of creating an instance is shown below. There is only one custom param: app title Vulkan APP in line 13th. Most of the time, the default parameters from Open-Source Vulkan C++ API are used. There is one exception, which is the API version (set in line 14th). The default value is 0, which on my machine resulted in an error of missing compatible device. After setting the API version to VK_MAKE_VERSION(1, 0, 0), Vulkan instance is successfully created and destroyed.
#include <iostream>
#include <string>
// Use more c++ friendly version of Vulkan cpp
#define VKCPP_ENHANCED_MODE
#include <vulkan/vk_cpp.h>
using namespace std;
int main(int argc, char* argv[]) {
try {
auto app_info = vk::ApplicationInfo()
.pApplicationName("Vulkan APP")
.apiVersion(VK_MAKE_VERSION(1, 0, 0));
auto instance_info = vk::InstanceCreateInfo().pApplicationInfo(&app_info);
auto instance = vk::createInstance(instance_info, nullptr);
cout << "Vulkan instance created" << endl;
instance.destroy(nullptr);
cout << "Vulkan instance destroyed" << endl;
}
catch(const std::system_error& err) {
cerr << "[ERROR] " << err.what() << endl;
return 1;
}
}
Unfortunately, vk::Instance is not an RAII wrapper, and the instance has to be freed manually. After deciding on memory allocation policy (nullptr parameters for vk::createInstance and vk::Instance::destroy), one should create RAII wrapper around vk::Instance.
No comments:
Post a Comment