Skip to content

Commit

Permalink
Revert "Revert "Added generic feature polling/matching/enabling.""
Browse files Browse the repository at this point in the history
This reverts commit 523e761.
  • Loading branch information
charles-lunarg committed Apr 18, 2021
1 parent fefa3f7 commit 98a4ae7
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 186 deletions.
212 changes: 59 additions & 153 deletions src/VkBootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,10 @@ std::vector<const char*> check_device_extension_support(
}

// clang-format off
bool supports_features(VkPhysicalDeviceFeatures supported, VkPhysicalDeviceFeatures requested) {
bool supports_features(VkPhysicalDeviceFeatures supported,
VkPhysicalDeviceFeatures requested,
const std::vector<ExtensionFeatures>& extension_supported,
const std::vector<ExtensionFeatures>& extension_requested) {
if (requested.robustBufferAccess && !supported.robustBufferAccess) return false;
if (requested.fullDrawIndexUint32 && !supported.fullDrawIndexUint32) return false;
if (requested.imageCubeArray && !supported.imageCubeArray) return false;
Expand Down Expand Up @@ -900,75 +903,14 @@ bool supports_features(VkPhysicalDeviceFeatures supported, VkPhysicalDeviceFeatu
if (requested.sparseResidencyAliased && !supported.sparseResidencyAliased) return false;
if (requested.variableMultisampleRate && !supported.variableMultisampleRate) return false;
if (requested.inheritedQueries && !supported.inheritedQueries) return false;
return true;
}
#if defined(VK_API_VERSION_1_2)
bool supports_features_11(VkPhysicalDeviceVulkan11Features supported, VkPhysicalDeviceVulkan11Features requested){
if (requested.storageBuffer16BitAccess && !supported.storageBuffer16BitAccess) return false;
if (requested.uniformAndStorageBuffer16BitAccess && !supported.uniformAndStorageBuffer16BitAccess) return false;
if (requested.storagePushConstant16 && !supported.storagePushConstant16) return false;
if (requested.storageInputOutput16 && !supported.storageInputOutput16) return false;
if (requested.multiview && !supported.multiview) return false;
if (requested.multiviewGeometryShader && !supported.multiviewGeometryShader) return false;
if (requested.multiviewTessellationShader && !supported.multiviewTessellationShader) return false;
if (requested.variablePointersStorageBuffer && !supported.variablePointersStorageBuffer) return false;
if (requested.variablePointers && !supported.variablePointers) return false;
if (requested.protectedMemory && !supported.protectedMemory) return false;
if (requested.samplerYcbcrConversion && !supported.samplerYcbcrConversion) return false;
if (requested.shaderDrawParameters && !supported.shaderDrawParameters) return false;
return true;
}
bool supports_features_12(VkPhysicalDeviceVulkan12Features supported, VkPhysicalDeviceVulkan12Features requested){
if(requested.samplerMirrorClampToEdge && !supported.samplerMirrorClampToEdge) return false;
if(requested.drawIndirectCount && !supported.drawIndirectCount) return false;
if(requested.storageBuffer8BitAccess && !supported.storageBuffer8BitAccess) return false;
if(requested.uniformAndStorageBuffer8BitAccess && !supported.uniformAndStorageBuffer8BitAccess) return false;
if(requested.storagePushConstant8 && !supported.storagePushConstant8) return false;
if(requested.shaderBufferInt64Atomics && !supported.shaderBufferInt64Atomics) return false;
if(requested.shaderSharedInt64Atomics && !supported.shaderSharedInt64Atomics) return false;
if(requested.shaderFloat16 && !supported.shaderFloat16) return false;
if(requested.shaderInt8 && !supported.shaderInt8) return false;
if(requested.descriptorIndexing && !supported.descriptorIndexing) return false;
if(requested.shaderInputAttachmentArrayDynamicIndexing && !supported.shaderInputAttachmentArrayDynamicIndexing) return false;
if(requested.shaderUniformTexelBufferArrayDynamicIndexing && !supported.shaderUniformTexelBufferArrayDynamicIndexing) return false;
if(requested.shaderStorageTexelBufferArrayDynamicIndexing && !supported.shaderStorageTexelBufferArrayDynamicIndexing) return false;
if(requested.shaderUniformBufferArrayNonUniformIndexing && !supported.shaderUniformBufferArrayNonUniformIndexing) return false;
if(requested.shaderSampledImageArrayNonUniformIndexing && !supported.shaderSampledImageArrayNonUniformIndexing) return false;
if(requested.shaderStorageBufferArrayNonUniformIndexing && !supported.shaderStorageBufferArrayNonUniformIndexing) return false;
if(requested.shaderStorageImageArrayNonUniformIndexing && !supported.shaderStorageImageArrayNonUniformIndexing) return false;
if(requested.shaderInputAttachmentArrayNonUniformIndexing && !supported.shaderInputAttachmentArrayNonUniformIndexing) return false;
if(requested.shaderUniformTexelBufferArrayNonUniformIndexing && !supported.shaderUniformTexelBufferArrayNonUniformIndexing) return false;
if(requested.shaderStorageTexelBufferArrayNonUniformIndexing && !supported.shaderStorageTexelBufferArrayNonUniformIndexing) return false;
if(requested.descriptorBindingUniformBufferUpdateAfterBind && !supported.descriptorBindingUniformBufferUpdateAfterBind) return false;
if(requested.descriptorBindingSampledImageUpdateAfterBind && !supported.descriptorBindingSampledImageUpdateAfterBind) return false;
if(requested.descriptorBindingStorageImageUpdateAfterBind && !supported.descriptorBindingStorageImageUpdateAfterBind) return false;
if(requested.descriptorBindingStorageBufferUpdateAfterBind && !supported.descriptorBindingStorageBufferUpdateAfterBind) return false;
if(requested.descriptorBindingUniformTexelBufferUpdateAfterBind && !supported.descriptorBindingUniformTexelBufferUpdateAfterBind) return false;
if(requested.descriptorBindingStorageTexelBufferUpdateAfterBind && !supported.descriptorBindingStorageTexelBufferUpdateAfterBind) return false;
if(requested.descriptorBindingUpdateUnusedWhilePending && !supported.descriptorBindingUpdateUnusedWhilePending) return false;
if(requested.descriptorBindingPartiallyBound && !supported.descriptorBindingPartiallyBound) return false;
if(requested.descriptorBindingVariableDescriptorCount && !supported.descriptorBindingVariableDescriptorCount) return false;
if(requested.runtimeDescriptorArray && !supported.runtimeDescriptorArray) return false;
if(requested.samplerFilterMinmax && !supported.samplerFilterMinmax) return false;
if(requested.scalarBlockLayout && !supported.scalarBlockLayout) return false;
if(requested.imagelessFramebuffer && !supported.imagelessFramebuffer) return false;
if(requested.uniformBufferStandardLayout && !supported.uniformBufferStandardLayout) return false;
if(requested.shaderSubgroupExtendedTypes && !supported.shaderSubgroupExtendedTypes) return false;
if(requested.separateDepthStencilLayouts && !supported.separateDepthStencilLayouts) return false;
if(requested.hostQueryReset && !supported.hostQueryReset) return false;
if(requested.timelineSemaphore && !supported.timelineSemaphore) return false;
if(requested.bufferDeviceAddress && !supported.bufferDeviceAddress) return false;
if(requested.bufferDeviceAddressCaptureReplay && !supported.bufferDeviceAddressCaptureReplay) return false;
if(requested.bufferDeviceAddressMultiDevice && !supported.bufferDeviceAddressMultiDevice) return false;
if(requested.vulkanMemoryModel && !supported.vulkanMemoryModel) return false;
if(requested.vulkanMemoryModelDeviceScope && !supported.vulkanMemoryModelDeviceScope) return false;
if(requested.vulkanMemoryModelAvailabilityVisibilityChains && !supported.vulkanMemoryModelAvailabilityVisibilityChains) return false;
if(requested.shaderOutputViewportIndex && !supported.shaderOutputViewportIndex) return false;
if(requested.shaderOutputLayer && !supported.shaderOutputLayer) return false;
if(requested.subgroupBroadcastDynamicId && !supported.subgroupBroadcastDynamicId) return false;

for(auto i = 0; i < extension_requested.size(); ++i) {
auto res = extension_requested[i].match(extension_supported[i]);
if(!res) return false;
}

return true;
}
#endif
// clang-format on
// finds the first queue which supports graphics operations. returns QUEUE_INDEX_MAX_VALUE if none is found
uint32_t get_graphics_queue_index(std::vector<VkQueueFamilyProperties> const& families) {
Expand Down Expand Up @@ -1049,7 +991,8 @@ uint32_t get_present_queue_index(VkPhysicalDevice const phys_device,


PhysicalDeviceSelector::PhysicalDeviceDesc PhysicalDeviceSelector::populate_device_details(
uint32_t instance_version, VkPhysicalDevice phys_device) const {
uint32_t instance_version, VkPhysicalDevice phys_device,
std::vector<ExtensionFeatures> extension_features_as_template) const {
PhysicalDeviceSelector::PhysicalDeviceDesc desc{};
desc.phys_device = phys_device;
auto queue_families = detail::get_vector_noerror<VkQueueFamilyProperties>(
Expand All @@ -1062,16 +1005,20 @@ PhysicalDeviceSelector::PhysicalDeviceDesc PhysicalDeviceSelector::populate_devi

#if defined(VK_API_VERSION_1_1)
desc.device_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;

#if defined(VK_API_VERSION_1_2)
desc.device_features_11.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
desc.device_features_12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
desc.device_features2.pNext = &desc.device_features_11;
desc.device_features_11.pNext = &desc.device_features_12;
#endif
if (instance_version >= VK_API_VERSION_1_1) {
detail::vulkan_functions().fp_vkGetPhysicalDeviceFeatures2(phys_device, &desc.device_features2);
}
desc.extension_features = extension_features_as_template;
if (instance_version >= VK_API_VERSION_1_1) {
ExtensionFeatures* prev = nullptr;
for(auto& extension : desc.extension_features) {
if(prev != nullptr) {
prev->structure->pNext = extension.structure;
}
prev = &extension;
}
if(desc.extension_features.size() > 0) {
desc.device_features2.pNext = &desc.extension_features[0].structure;
}
detail::vulkan_functions().fp_vkGetPhysicalDeviceFeatures2(phys_device, &desc.device_features2);
}
#endif
return desc;
}
Expand Down Expand Up @@ -1112,7 +1059,6 @@ PhysicalDeviceSelector::Suitable PhysicalDeviceSelector::is_device_suitable(Phys
if (desired_extensions_supported.size() != criteria.desired_extensions.size())
suitable = Suitable::partial;


bool swapChainAdequate = false;
if (criteria.defer_surface_initialization) {
swapChainAdequate = true;
Expand Down Expand Up @@ -1142,21 +1088,10 @@ PhysicalDeviceSelector::Suitable PhysicalDeviceSelector::is_device_suitable(Phys
return Suitable::no;
}

bool required_features_supported = detail::supports_features(pd.device_features, criteria.required_features);
bool required_features_supported = detail::supports_features(pd.device_features, criteria.required_features,
pd.extension_features, criteria.extension_features);
if (!required_features_supported) return Suitable::no;

#if defined(VK_API_VERSION_1_2)
if (instance_info.version >= VK_API_VERSION_1_2) {
bool required_features_11_supported =
detail::supports_features_11(pd.device_features_11, criteria.required_features_11);
if (!required_features_11_supported) return Suitable::no;

bool required_features_12_supported =
detail::supports_features_12(pd.device_features_12, criteria.required_features_12);
if (!required_features_12_supported) return Suitable::no;
}
#endif

bool has_required_memory = false;
bool has_preferred_memory = false;
for (uint32_t i = 0; i < pd.mem_properties.memoryHeapCount; i++) {
Expand Down Expand Up @@ -1205,7 +1140,9 @@ detail::Result<PhysicalDevice> PhysicalDeviceSelector::select() const {

std::vector<PhysicalDeviceDesc> phys_device_descriptions;
for (auto& phys_device : physical_devices) {
phys_device_descriptions.push_back(populate_device_details(instance_info.version, phys_device));
phys_device_descriptions.push_back(populate_device_details(instance_info.version,
phys_device,
criteria.extension_features));
}

PhysicalDeviceDesc selected_device{};
Expand All @@ -1231,12 +1168,7 @@ detail::Result<PhysicalDevice> PhysicalDeviceSelector::select() const {
out_device.physical_device = selected_device.phys_device;
out_device.surface = instance_info.surface;
out_device.features = criteria.required_features;
#if defined(VK_API_VERSION_1_2)
out_device.features_11 = criteria.required_features_11;
out_device.features_11.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES;
out_device.features_12 = criteria.required_features_12;
out_device.features_12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
#endif
out_device.extension_features = criteria.extension_features;
out_device.properties = selected_device.device_properties;
out_device.memory_properties = selected_device.mem_properties;
out_device.queue_families = selected_device.queue_families;
Expand Down Expand Up @@ -1321,20 +1253,17 @@ PhysicalDeviceSelector& PhysicalDeviceSelector::set_desired_version(uint32_t maj
criteria.desired_version = VK_MAKE_VERSION(major, minor, 0);
return *this;
}
PhysicalDeviceSelector& PhysicalDeviceSelector::set_required_features(VkPhysicalDeviceFeatures const& features) {
criteria.required_features = features;
return *this;
}
#if defined(VK_API_VERSION_1_2)
// Just calls add_required_features
PhysicalDeviceSelector& PhysicalDeviceSelector::set_required_features_11(
VkPhysicalDeviceVulkan11Features const& features_11) {
criteria.required_features_11 = features_11;
return *this;
add_required_features(features_11);
return *this;
}
PhysicalDeviceSelector& PhysicalDeviceSelector::set_required_features_12(
VkPhysicalDeviceVulkan12Features const& features_12) {
criteria.required_features_12 = features_12;
return *this;
add_required_features(features_12);
return *this;
}
#endif
PhysicalDeviceSelector& PhysicalDeviceSelector::defer_surface_initialization() {
Expand Down Expand Up @@ -1467,63 +1396,40 @@ detail::Result<Device> DeviceBuilder::build() const {
if (physical_device.surface != VK_NULL_HANDLE || physical_device.defer_surface_initialization)
extensions.push_back({ VK_KHR_SWAPCHAIN_EXTENSION_NAME });

std::vector<VkBaseOutStructure*> pNext_chain = info.pNext_chain;

// check if certain structs were added in the pNext chain by the user
bool has_phys_dev_features_2 = false;
bool has_phys_dev_vulkan_features_11 = false;
bool has_phys_dev_vulkan_features_12 = false;
for (auto& pNext_struct : pNext_chain) {
if (pNext_struct->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) {
has_phys_dev_features_2 = true;
}
if (pNext_struct->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES) {
has_phys_dev_vulkan_features_11 = true;
}
if (pNext_struct->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES) {
has_phys_dev_vulkan_features_12 = true;
}
}
bool has_phys_dev_features_2 = false;

// Add the correct structs to the pNext chain if api is 1.1/1.2 and aren't already present
// This is to guard against users who were already doing that
// Setup the pNexts of all the extension features
#if defined(VK_API_VERSION_1_1)
VkPhysicalDeviceFeatures2 local_features2{};
if (physical_device.instance_version >= VK_MAKE_VERSION(1, 1, 0) && !has_phys_dev_features_2) {
local_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
local_features2.features = physical_device.features;
pNext_chain.push_back(reinterpret_cast<VkBaseOutStructure*>(&local_features2));
has_phys_dev_features_2 = true;
}
#if defined(VK_API_VERSION_1_2)
VkPhysicalDeviceVulkan11Features local_features_11 = physical_device.features_11;
VkPhysicalDeviceVulkan12Features local_features_12 = physical_device.features_12;
if (physical_device.instance_version >= VK_MAKE_VERSION(1, 2, 0)) {
if (!has_phys_dev_vulkan_features_11) {
pNext_chain.push_back(reinterpret_cast<VkBaseOutStructure*>(&local_features_11));
has_phys_dev_vulkan_features_11 = true;
}
if (!has_phys_dev_vulkan_features_12) {
pNext_chain.push_back(reinterpret_cast<VkBaseOutStructure*>(&local_features_12));
has_phys_dev_vulkan_features_12 = true;
}
}
#endif
VkPhysicalDeviceFeatures2 local_features2{};
if (physical_device.instance_version >= VK_MAKE_VERSION(1, 1, 0) &&
physical_device.extension_features.size() > 0) {
ExtensionFeatures* prev = nullptr;
for(auto& extension : physical_device.extension_features) {
if(prev != nullptr) {
prev->structure->pNext = extension.structure;
}
prev = &extension;
}
local_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
local_features2.features = physical_device.features;
local_features2.pNext = physical_device.extension_features[0].structure;
has_phys_dev_features_2 = true;
}
#endif

VkDeviceCreateInfo device_create_info = {};
device_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
detail::setup_pNext_chain(device_create_info, pNext_chain);
device_create_info.flags = info.flags;
device_create_info.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
device_create_info.pQueueCreateInfos = queueCreateInfos.data();
device_create_info.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
device_create_info.ppEnabledExtensionNames = extensions.data();
// VUID-VkDeviceCreateInfo-pNext-00373 - don't add pEnabledFeatures if the phys_dev_features_2 is present
if (!has_phys_dev_features_2) {
device_create_info.pEnabledFeatures = &physical_device.features;
}

if (!has_phys_dev_features_2) {
device_create_info.pEnabledFeatures = &physical_device.features;
} else {
device_create_info.pNext = &local_features2;
}

Device device;
VkResult res = detail::vulkan_functions().fp_vkCreateDevice(
Expand Down
Loading

0 comments on commit 98a4ae7

Please sign in to comment.