Skip to content

Commit

Permalink
Re-land #1 "[vulkan] Refactor vkEnumerate methods (#6423)" (#6426)
Browse files Browse the repository at this point in the history
* Re-land #1 "[vulkan] Refactor vkEnumerate methods (#6423)"

This reverts commit 6f7d203.
The above commit (6f7d20) is the revert of #6423. Commit
6f7d20 is PR #6425.

"[vulkan] Refactor vkEnumerate methods (#6423)" failed [here] due to
a missing function attribute (VKAPI_ATTR in bluevk) for ARM64
compilers.

[here]: https://github.com/google/filament/actions/runs/3841550984
  • Loading branch information
poweifeng authored Jan 5, 2023
1 parent c7c184d commit 378fb92
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 39 deletions.
43 changes: 16 additions & 27 deletions filament/backend/src/vulkan/VulkanContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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 << ", "
Expand Down
20 changes: 8 additions & 12 deletions filament/backend/src/vulkan/VulkanDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
44 changes: 44 additions & 0 deletions filament/backend/src/vulkan/VulkanUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <backend/DriverEnums.h>

#include <utils/FixedCapacityVector.h>

#include <bluevk/BlueVK.h>

namespace filament::backend {
Expand Down Expand Up @@ -66,6 +68,48 @@ 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(VKAPI_ATTR VkResult (*func)(uint32_t*, OutType*)) {
EXPAND_ENUM_NO_ARGS();
}

template <typename InType, typename OutType>
utils::FixedCapacityVector<OutType> enumerate(
VKAPI_ATTR VkResult (*func)(InType, uint32_t*, OutType*), InType inData) {
EXPAND_ENUM_ARGS(inData);
}

template <typename InTypeA, typename InTypeB, typename OutType>
utils::FixedCapacityVector<OutType> enumerate(
VKAPI_ATTR 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);
Expand Down

0 comments on commit 378fb92

Please sign in to comment.