跳到主要内容

启用扩展

扩展类型

实例级扩展设备级扩展这两种扩展。简单地说,实例级扩展与整个VkInstance相关联,而设备级扩展仅与单个VkDevice关联。

在每个扩展参考页面的“扩展类型”部分会说明是那种扩展,如:

enabling_extensions_instance_extension.png

检查扩展的支持情况

应用可以先查询物理设备是否支持扩展,查询接口:vkEnumerateInstanceExtensionPropertiesvkEnumerateDeviceExtensionProperties

// Simple example
uint32_t count = 0;
vkEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &count, nullptr);
std::vector<VkExtensionProperties> extensions(count);
vkEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &count, extensions.data());

// Checking for support of VK_KHR_bind_memory2
for (uint32_t i = 0; i < count; i++) {
if (strcmp(VK_KHR_BIND_MEMORY_2_EXTENSION_NAME, extensions[i].extensionName) == 0) {
break; // VK_KHR_bind_memory2 is supported
}
}

启用扩展

即使物理设备支持扩展,如果在创建VkInstanceVkDevice未启用它,直接使用该扩展的功能将是未定义行为

VK_KHR_driver_properties扩展为例,下面是启用它所需要的条件:

enabling_extensions_driver_properties.png

// VK_KHR_get_physical_device_properties2 is required to use VK_KHR_driver_properties
// since it's an instance extension it needs to be enabled before at VkInstance creation time
std::vector<const char*> instance_extensions;
instance_extensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);

VkInstanceCreateInfo instance_create_info = {};
instance_create_info.enabledExtensionCount = static_cast<uint32_t>(instance_extensions.size());
instance_create_info.ppEnabledExtensionNames = instance_extensions.data();
vkCreateInstance(&instance_create_info, nullptr, &myInstance);

// ...

std::vector<const char*> device_extensions;
device_extensions.push_back(VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME);

VkDeviceCreateInfo device_create_info = {};
device_create_info.enabledExtensionCount = static_cast<uint32_t>(device_extensions.size());
device_create_info.ppEnabledExtensionNames = device_extensions.data();
vkCreateDevice(physicalDevice, &device_create_info, nullptr, &myDevice);

检查特征位

扩展会向 Vulkan spec添加函数,但支持了扩展不代表扩展的所有功能都可用。例如,VK_KHR_8bit_storage扩展有 3 个功能在VkPhysicalDevice8BitStorageFeatures中控制开关:

enabling_extensions_8bit.png

在启用该扩展后,应用仍需查询和启用扩展所需的功能。

扩展升级为核心

Vulkan 发布新规范版本时,某些得到广泛支持的扩展功能可能会提升为到核心功能。

已升级到核心功能的扩展列表可参阅spec版本附录