The Vulkan API offers us various functions to enumerate properties, for example vkEnumerateInstanceLayerProperties which returns global layer properties. According to the official documentation the result of available layers may vary from one call to the next. Quote:
The list of available layers may change at any time due to actions outside of the Vulkan implementation, so two calls to vkEnumerateInstanceLayerProperties with the same parameters may return different results, or retrieve different pPropertyCount values or pProperties contents. Once an instance has been created, the layers enabled for that instance will continue to be enabled and valid for the lifetime of that instance, even if some of them become unavailable for future instances.
As I understand it, we first request the property count, we then allocate memory to retrieve the properties in, and we then request the properties with given count and pointer to allocated memory. In case the amount of properties changed, I take it we should redo the entire process, correct?
I looked at a lot of example code all over the Internet, yet nowhere do I see any change checking and looping performed. Should we indeed loop or just assume nothing changes, eventhough the documentation explicitly warns about it? If we should be looping, should we then also include a loop delay for like 10ms orso to give the system time to get things in order? If we should be looping, is there an advised number of attempts or a certain time constraint?
To clarify my thought process, here’s one of my plain-C helper functions (to keep it short, return results have been simplified):
int vkuGetInstanceLayerProperties(uint32_t* pPropertyCount, VkLayerProperties** pProperties)
{
VkLayerProperties* props;
VkResult err;
uint32_t count1;
uint32_t count2;
uint32_t retries = 2; /* Advised number of attempts? Should we even loop? */
while (1)
{
err = vkEnumerateInstanceLayerProperties(&count1, NULL);
if (VK_SUCCESS != err)
{
return -1;
}
if (!(props = calloc((size_t)count1, sizeof(VkLayerProperties))))
{
return -1;
}
count2 = count1;
err = vkEnumerateInstanceLayerProperties(&count2, props);
if (VK_INCOMPLETE == err || (VK_SUCCESS == err && count1 > count2))
{
free(props);
if (retries--)
{
/* Should we delay code execution here, like Sleep(20)? If so, any advised duration? */
continue;
}
return -1;
}
else if (VK_SUCCESS != err)
{
return -1;
}
break;
};
*pPropertyCount = count1;
*pProperties = props;
return 0;
}