本节讲解启用features的具体流程。
Category of Features
Vulkan 中的所有 feature 可归为 / 查自以下 3 类:
Core 1.0 Features
这些是 Vulkan 1.0 初始版本就提供的特性集合。特性列表可在VkPhysicalDeviceFeatures中找到。
Future Core Version Features
从 Vulkan 1.1 开始,core 版本中加入了一些新特性。为保持VkPhysicalDeviceFeatures的大小与向后兼容,新增了专门的结构体来存放这些特性:
VkPhysicalDeviceVulkan11FeaturesVkPhysicalDeviceVulkan12Features
Extension Features
有些 extension 内部包含需要显式启用的 feature。它们很容易识别,命名格式均为:VkPhysicalDevice[ExtensionName]Features
How to Enable the Features
所有 feature 都必须在创建VkDevice时,通过VkDeviceCreateInfo结构体启用。
注意:不要忘记先用vkGetPhysicalDeviceFeatures或vkGetPhysicalDeviceFeatures2查询是否支持。
对于 Core 1.0 Features
只需将需要开启的特性填入VkDeviceCreateInfo::pEnabledFeatures即可。
VkPhysicalDeviceFeatures features = {}; vkGetPhysicalDeviceFeatures(physical_device, &features); // 如果特性不支持的处理逻辑 if (features.robustBufferAccess == VK_FALSE) { } VkDeviceCreateInfo info = {}; info.pEnabledFeatures = &features;对于所有特性(包括 Core 1.0 Features)
推荐使用VkPhysicalDeviceFeatures2,并通过VkDeviceCreateInfo.pNext传入。
VkPhysicalDeviceShaderDrawParametersFeatures ext_feature = {}; ext_feature.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETERS_FEATURES; VkPhysicalDeviceFeatures2 physical_features2 = {}; physical_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; physical_features2.pNext = &ext_feature; vkGetPhysicalDeviceFeatures2(physical_device, &physical_features2); // 如果特性不支持的处理逻辑 if (ext_feature.shaderDrawParameters == VK_FALSE) { } VkDeviceCreateInfo info = {}; info.pNext = &physical_features2;同样的方法也适用于 “Future Core Version Features”。
VkPhysicalDeviceVulkan11Features features11 = {}; features11.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES; VkPhysicalDeviceFeatures2 physical_features2 = {}; physical_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; physical_features2.pNext = &features11; vkGetPhysicalDeviceFeatures2(physical_device, &physical_features2); // 如果特性不支持的处理逻辑 if (features11.shaderDrawParameters == VK_FALSE) { } VkDeviceCreateInfo info = {}; info.pNext = &physical_features2;