'using vkEnumerateInstanceVersion to get exact Vulkan API version
I am using vkEnumerateInstanceVersion
to get the vulkan api version and pass it to VkApplicationInfo
struct. I can easily differentiate between VK_VERSION_1_0
and VK_VERSION_1_1
//query the api version in order to use the correct vulkan functionality
PFN_vkEnumerateInstanceVersion FN_vkEnumerateInstanceVersion =
PFN_vkEnumerateInstanceVersion(vkGetInstanceProcAddr(nullptr, "vkEnumerateInstanceVersion"));
uint32_t *instanceVersion = (uint32_t*)malloc(sizeof(uint32_t));
VkResult result = FN_vkEnumerateInstanceVersion(instanceVersion);
//check what is returned
if(result == VK_SUCCESS){
std::cout<<"RESULT(vkEnumerateInstanceVersion) : Intance version enumeration successful"<<std::endl;
if(instanceVersion!=nullptr){
std::cout<<"API_VERSION : VK_API_VERSION_1_1"<<std::endl;
appInfo.apiVersion = VK_API_VERSION_1_1;
}
else{
std::cout<<"API_VERSION : VK_API_VERSION_1_0"<<std::endl;
}
std::cout<<"Version number returned : "<<*instanceVersion<<std::endl;
}else if(result == VK_ERROR_OUT_OF_HOST_MEMORY){
std::cerr<<"RESULT(vkEnumerateInstanceVersion) : VK_ERROR_HOST_OUT_OF_MEMORY"<<std::endl;
}else{
std::cerr<<"RESULT(vkEnumerateInstanceVersion) : Something else returned while enumerating instance version"<<std::endl;
}
I am using the above code to get the api version. The vulkan specification states :
To query the version of instance-level functionality supported by the implementation, call:
// Provided by VK_VERSION_1_1 VkResult vkEnumerateInstanceVersion( uint32_t* pApiVersion);
The comment in the above block means that this is provided by VK_VERSION_1_1
and not by VK_VERSION_1_0
.
Now, when I open my terminal and type :
vulkaninfo | head -n 5
I get the following :
ERROR: [Loader Message] Code 0 : /usr/lib32/libvulkan_intel.so: wrong ELF class: ELFCLASS32
ERROR: [Loader Message] Code 0 : /usr/lib32/libvulkan_radeon.so: wrong ELF class: ELFCLASS32
==========
VULKANINFO
==========
Vulkan Instance Version: 1.2.159
Which means I have VK_API_VERSION_1_2. What I want to do is get the exact api version. On executing the first block of code I get :
RESULT(vkEnumerateInstanceVersion) : Intance version enumeration successful
API_VERSION : VK_API_VERSION_1_1
Version number returned : 4202655
Is there any way to determine VK_VERSION_1_1 or VK_VERSION_1_2 in program. Also I am checking the vulkan reference guide for version 1.2.165
Solution 1:[1]
I am using
vkEnumerateInstanceVersion
to get the vulkan api version and pass it toVkApplicationInfo
struct.
That's not what vkEnumerateInstanceVersion
is for.
VkApplicationInfo::apiVersion
specifies the version of Vulkan against which your code is written. This version is meant to be a fixed value, because your code cannot be written against versions of Vulkan that do not yet exist. That is, if you're writing an application that works against 1.1, then you specify VK_VERSION_1_1
.
You may still get a 1.2 version, but that's OK because Vulkan major versions are all backwards compatible with lower minor version numbers. So if you write your code against Vulkan 1.1, it will work on any 1.2 implementation.
You also are misusing the instance version. The instance version is the version of the Vulkan instance machinery, not of the Vulkan implementation as a whole. You might be connected to 1.2 instance machinery, but the Vulkan device implementation might only give you 1.1. That's fine as far as Vulkan is concerned.
As for the particulars of your code:
This code does not in fact differentiate between Vulkan 1.0 and Vulkan 1.1 correctly. Indeed, it contains numerous bugs and confusing bits.
The first potential issue unaddressed is the possibility that the instance implementation is too old to support vkEnumerateInstanceVersion
. As this function is not provided by Vulkan 1.0 instance implementations, you should first make sure that the function actually exists. That is, you should check the value of FN_vkEnumerateInstanceVersion
before calling it. If the function doesn't exist, then the instance version must be 1.0.
Next, you call the function. But the function cannot fail; it can only return the error code VK_SUCCESS
. So there's no point in checking its result code.
Once you go to check the actual value in question, you then do something odd. You test instanceVersion!=nullptr
. But there's no point in doing that because it will always point to valid memory. So concluding from this that the instance implementation version is 1.1 is simply wrong.
The meaning of the value of version numbers in Vulkan is clearly specified in the specification. In your case, if you're just looking for the major and minor version numbers, you can chop off the patch version numbers.
So the correct code to get the instance version would be:
auto FN_vkEnumerateInstanceVersion = PFN_vkEnumerateInstanceVersion(vkGetInstanceProcAddr(nullptr, "vkEnumerateInstanceVersion"));
if(FN_vkEnumerateInstanceVersion == nullptr)
return VK_API_VERSION_1_0;
else
{
uint32_t instanceVersion;
auto result = FN_vkEnumerateInstanceVersion(&instanceVersion);
return instanceVersion & 0xFFFFF000; //Remove the patch version.
}
Solution 2:[2]
You can get the Vulkan version information from the instance version returned by vkEnumerateInstanceVersion with 3 macros that are defined in the vulkan header.
Here is a snippet that does the trick:
uint32 instanceVersion = VK_API_VERSION_1_0;
auto FN_vkEnumerateInstanceVersion = PFN_vkEnumerateInstanceVersion(vkGetInstanceProcAddr(nullptr, "vkEnumerateInstanceVersion"));
if(vkEnumerateInstanceVersion){
vkEnumerateInstanceVersion(&instanceVersion );
}
// 3 macros to extract version info
uint32_t major = VK_VERSION_MAJOR(instanceVersion);
uint32_t minor = VK_VERSION_MINOR(instanceVersion);
uint32_t patch = VK_VERSION_PATCH(instanceVersion);
cout << "Vulkan Version:" << major << "." << minor << "." << patch << endl;
That will print out something along the lines of:
Vulkan Version:1.1.121
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | pmw1234 |