[vulkan] Refactor vkEnumerate methods (#6423)
* [vulkan] Refactor vkEnumerate methods Use templates to help clean up the enumeration a bit.
This commit is contained in:
@@ -56,16 +56,10 @@ VkImageView VulkanAttachment::getImageView(VkImageAspectFlags aspect) const {
|
||||
}
|
||||
|
||||
void VulkanContext::selectPhysicalDevice() {
|
||||
uint32_t physicalDeviceCount = 0;
|
||||
VkResult result = vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, nullptr);
|
||||
ASSERT_POSTCONDITION(result == VK_SUCCESS && physicalDeviceCount > 0,
|
||||
"vkEnumeratePhysicalDevices count error.");
|
||||
FixedCapacityVector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
|
||||
result = vkEnumeratePhysicalDevices(instance, &physicalDeviceCount,
|
||||
physicalDevices.data());
|
||||
ASSERT_POSTCONDITION(result == VK_SUCCESS, "vkEnumeratePhysicalDevices error.");
|
||||
for (uint32_t i = 0; i < physicalDeviceCount; ++i) {
|
||||
physicalDevice = physicalDevices[i];
|
||||
const FixedCapacityVector<VkPhysicalDevice> physicalDevices = enumerate(
|
||||
vkEnumeratePhysicalDevices, instance);
|
||||
for (const auto& candidateDevice : physicalDevices) {
|
||||
physicalDevice = candidateDevice;
|
||||
vkGetPhysicalDeviceProperties(physicalDevice, &physicalDeviceProperties);
|
||||
|
||||
const int major = VK_VERSION_MAJOR(physicalDeviceProperties.apiVersion);
|
||||
@@ -103,28 +97,23 @@ void VulkanContext::selectPhysicalDevice() {
|
||||
if (graphicsQueueFamilyIndex == 0xffff) continue;
|
||||
|
||||
// Does the device support the VK_KHR_swapchain extension?
|
||||
uint32_t extensionCount;
|
||||
result = vkEnumerateDeviceExtensionProperties(physicalDevice, /*pLayerName = */ nullptr,
|
||||
&extensionCount, nullptr);
|
||||
ASSERT_POSTCONDITION(result == VK_SUCCESS,
|
||||
"vkEnumerateDeviceExtensionProperties count error.");
|
||||
FixedCapacityVector<VkExtensionProperties> extensions(extensionCount);
|
||||
result = vkEnumerateDeviceExtensionProperties(physicalDevice, /*pLayerName = */ nullptr,
|
||||
&extensionCount, extensions.data());
|
||||
ASSERT_POSTCONDITION(result == VK_SUCCESS, "vkEnumerateDeviceExtensionProperties error.");
|
||||
const FixedCapacityVector<VkExtensionProperties> extensions = enumerate(
|
||||
vkEnumerateDeviceExtensionProperties, physicalDevice,
|
||||
static_cast<const char*>(nullptr) /* pLayerName */);
|
||||
|
||||
bool supportsSwapchain = false;
|
||||
for (uint32_t k = 0; k < extensionCount; ++k) {
|
||||
if (!strcmp(extensions[k].extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
|
||||
for (const auto& extension : extensions) {
|
||||
if (!strcmp(extension.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
|
||||
supportsSwapchain = true;
|
||||
} else if (!strcmp(extensions[k].extensionName, VK_EXT_DEBUG_MARKER_EXTENSION_NAME)) {
|
||||
} else if (!strcmp(extension.extensionName, VK_EXT_DEBUG_MARKER_EXTENSION_NAME)) {
|
||||
debugMarkersSupported = true;
|
||||
} else if (!strcmp(extensions[k].extensionName, VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME)) {
|
||||
} else if (!strcmp(extension.extensionName, VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME)) {
|
||||
portabilitySubsetSupported = true;
|
||||
} else if (!strcmp(extensions[k].extensionName, VK_KHR_MAINTENANCE1_EXTENSION_NAME)) {
|
||||
} else if (!strcmp(extension.extensionName, VK_KHR_MAINTENANCE1_EXTENSION_NAME)) {
|
||||
maintenanceSupported[0] = true;
|
||||
} else if (!strcmp(extensions[k].extensionName, VK_KHR_MAINTENANCE2_EXTENSION_NAME)) {
|
||||
} else if (!strcmp(extension.extensionName, VK_KHR_MAINTENANCE2_EXTENSION_NAME)) {
|
||||
maintenanceSupported[1] = true;
|
||||
} else if (!strcmp(extensions[k].extensionName, VK_KHR_MAINTENANCE3_EXTENSION_NAME)) {
|
||||
} else if (!strcmp(extension.extensionName, VK_KHR_MAINTENANCE3_EXTENSION_NAME)) {
|
||||
maintenanceSupported[2] = true;
|
||||
}
|
||||
}
|
||||
@@ -171,7 +160,7 @@ void VulkanContext::selectPhysicalDevice() {
|
||||
const uint32_t deviceID = physicalDeviceProperties.deviceID;
|
||||
utils::slog.i << "Selected physical device '"
|
||||
<< physicalDeviceProperties.deviceName
|
||||
<< "' from " << physicalDeviceCount << " physical devices. "
|
||||
<< "' from " << physicalDevices.size() << " physical devices. "
|
||||
<< "(vendor " << utils::io::hex << vendorID << ", "
|
||||
<< "device " << deviceID << ", "
|
||||
<< "driver " << driverVersion << ", "
|
||||
|
||||
@@ -109,10 +109,9 @@ VulkanDriver::VulkanDriver(VulkanPlatform* platform,
|
||||
|
||||
// Determine if the VK_EXT_debug_utils instance extension is available.
|
||||
mContext.debugUtilsSupported = false;
|
||||
uint32_t availableExtsCount = 0;
|
||||
vkEnumerateInstanceExtensionProperties(nullptr, &availableExtsCount, nullptr);
|
||||
utils::FixedCapacityVector<VkExtensionProperties> availableExts(availableExtsCount);
|
||||
vkEnumerateInstanceExtensionProperties(nullptr, &availableExtsCount, availableExts.data());
|
||||
const FixedCapacityVector<VkExtensionProperties> availableExts = enumerate(
|
||||
vkEnumerateInstanceExtensionProperties,
|
||||
static_cast<const char*>(nullptr) /* pLayerName */);
|
||||
for (const auto& extProps : availableExts) {
|
||||
if (!strcmp(extProps.extensionName, VK_EXT_DEBUG_UTILS_EXTENSION_NAME)) {
|
||||
mContext.debugUtilsSupported = true;
|
||||
@@ -138,10 +137,9 @@ VulkanDriver::VulkanDriver(VulkanPlatform* platform,
|
||||
|
||||
constexpr size_t kMaxEnabledLayersCount = sizeof(DESIRED_LAYERS) / sizeof(DESIRED_LAYERS[0]);
|
||||
|
||||
uint32_t layerCount;
|
||||
vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
|
||||
FixedCapacityVector<VkLayerProperties> availableLayers(layerCount);
|
||||
vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
|
||||
const FixedCapacityVector<VkLayerProperties> availableLayers = enumerate(
|
||||
vkEnumerateInstanceLayerProperties);
|
||||
|
||||
auto enabledLayers = FixedCapacityVector<const char*>::with_capacity(kMaxEnabledLayersCount);
|
||||
for (const auto& desired : DESIRED_LAYERS) {
|
||||
for (const VkLayerProperties& layer : availableLayers) {
|
||||
@@ -158,10 +156,8 @@ VulkanDriver::VulkanDriver(VulkanPlatform* platform,
|
||||
instanceCreateInfo.ppEnabledLayerNames = enabledLayers.data();
|
||||
|
||||
// Check if VK_EXT_validation_features is supported.
|
||||
uint32_t availableExtsCount = 0;
|
||||
vkEnumerateInstanceExtensionProperties("VK_LAYER_KHRONOS_validation", &availableExtsCount, nullptr);
|
||||
utils::FixedCapacityVector<VkExtensionProperties> availableExts(availableExtsCount);
|
||||
vkEnumerateInstanceExtensionProperties("VK_LAYER_KHRONOS_validation", &availableExtsCount, availableExts.data());
|
||||
const FixedCapacityVector<VkExtensionProperties> availableExts = enumerate(
|
||||
vkEnumerateInstanceExtensionProperties, "VK_LAYER_KHRONOS_validation");
|
||||
for (const auto& extProps : availableExts) {
|
||||
if (!strcmp(extProps.extensionName, VK_EXT_VALIDATION_FEATURES_EXTENSION_NAME)) {
|
||||
validationFeaturesSupported = true;
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
|
||||
#include <backend/DriverEnums.h>
|
||||
|
||||
#include <utils/FixedCapacityVector.h>
|
||||
|
||||
#include <bluevk/BlueVK.h>
|
||||
|
||||
namespace filament::backend {
|
||||
@@ -66,6 +68,46 @@ bool equivalent(const VkExtent2D& a, const VkExtent2D& b);
|
||||
bool isDepthFormat(VkFormat format);
|
||||
uint8_t reduceSampleCount(uint8_t sampleCount, VkSampleCountFlags mask);
|
||||
|
||||
|
||||
// Helper function for the vkEnumerateX methods. These methods have the format of
|
||||
// VkResult vkEnumerateX(InputType1 arg1, InputTyp2 arg2, ..., uint32_t* size,
|
||||
// OutputType* output_arg)
|
||||
// Instead of macros and explicitly listing the template params, Variadic Template was also
|
||||
// considered, but because the "variadic" part of the vk methods (i.e. the inputs) are before the
|
||||
// non-variadic parts, this breaks the template type matching logic. Hence, we use a macro approach
|
||||
// here.
|
||||
#define EXPAND_ENUM(...)\
|
||||
uint32_t size = 0;\
|
||||
VkResult result = func(__VA_ARGS__, nullptr);\
|
||||
ASSERT_POSTCONDITION(result == VK_SUCCESS && size > 0, "enumerate size error");\
|
||||
utils::FixedCapacityVector<OutType> ret(size);\
|
||||
result = func(__VA_ARGS__, ret.data());\
|
||||
ASSERT_POSTCONDITION(result == VK_SUCCESS, "enumerate error");\
|
||||
return std::move(ret);
|
||||
|
||||
#define EXPAND_ENUM_NO_ARGS() EXPAND_ENUM(&size)
|
||||
#define EXPAND_ENUM_ARGS(...) EXPAND_ENUM(__VA_ARGS__, &size)
|
||||
|
||||
template <typename OutType>
|
||||
utils::FixedCapacityVector<OutType> enumerate(VkResult (*func)(uint32_t*, OutType*)) {
|
||||
EXPAND_ENUM_NO_ARGS();
|
||||
}
|
||||
|
||||
template <typename InType, typename OutType>
|
||||
utils::FixedCapacityVector<OutType> enumerate(VkResult (*func)(InType, uint32_t*, OutType*), InType inData) {
|
||||
EXPAND_ENUM_ARGS(inData);
|
||||
}
|
||||
|
||||
template <typename InTypeA, typename InTypeB, typename OutType>
|
||||
utils::FixedCapacityVector<OutType> enumerate(
|
||||
VkResult (*func)(InTypeA, InTypeB, uint32_t*, OutType*), InTypeA inDataA, InTypeB inDataB) {
|
||||
EXPAND_ENUM_ARGS(inDataA, inDataB);
|
||||
}
|
||||
|
||||
#undef EXPAND_ENUM
|
||||
#undef EXPAND_ENUM_NO_ARGS
|
||||
#undef EXPAND_ENUM_ARGS
|
||||
|
||||
} // namespace filament::backend
|
||||
|
||||
bool operator<(const VkImageSubresourceRange& a, const VkImageSubresourceRange& b);
|
||||
|
||||
Reference in New Issue
Block a user