Remove memset, fix mismatched size comparison

This commit is contained in:
Charles Giessen 2021-04-18 14:22:30 -06:00 committed by Charles Giessen
parent 7271cac30c
commit c607a65755
2 changed files with 100 additions and 103 deletions

View File

@ -690,7 +690,7 @@ detail::Result<Instance> InstanceBuilder::build() const {
VkInstanceCreateInfo instance_create_info = {}; VkInstanceCreateInfo instance_create_info = {};
instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
detail::setup_pNext_chain(instance_create_info, pNext_chain); detail::setup_pNext_chain(instance_create_info, pNext_chain);
for(auto& node : pNext_chain) { for (auto& node : pNext_chain) {
assert(node->sType != VK_STRUCTURE_TYPE_APPLICATION_INFO); assert(node->sType != VK_STRUCTURE_TYPE_APPLICATION_INFO);
} }
instance_create_info.flags = info.flags; instance_create_info.flags = info.flags;
@ -907,7 +907,7 @@ bool supports_features(VkPhysicalDeviceFeatures supported,
if (requested.variableMultisampleRate && !supported.variableMultisampleRate) return false; if (requested.variableMultisampleRate && !supported.variableMultisampleRate) return false;
if (requested.inheritedQueries && !supported.inheritedQueries) return false; if (requested.inheritedQueries && !supported.inheritedQueries) return false;
for(auto i = 0; i < extension_requested.size(); ++i) { for(size_t i = 0; i < extension_requested.size(); ++i) {
auto res = GenericFeaturesPNextNode::match(extension_requested[i], extension_supported[i]); auto res = GenericFeaturesPNextNode::match(extension_requested[i], extension_supported[i]);
if(!res) return false; if(!res) return false;
} }
@ -993,8 +993,8 @@ uint32_t get_present_queue_index(VkPhysicalDevice const phys_device,
} // namespace detail } // namespace detail
PhysicalDeviceSelector::PhysicalDeviceDesc PhysicalDeviceSelector::populate_device_details( PhysicalDeviceSelector::PhysicalDeviceDesc PhysicalDeviceSelector::populate_device_details(uint32_t instance_version,
uint32_t instance_version, VkPhysicalDevice phys_device, VkPhysicalDevice phys_device,
std::vector<detail::GenericFeaturesPNextNode> const& src_extended_features_chain) const { std::vector<detail::GenericFeaturesPNextNode> const& src_extended_features_chain) const {
PhysicalDeviceSelector::PhysicalDeviceDesc desc{}; PhysicalDeviceSelector::PhysicalDeviceDesc desc{};
desc.phys_device = phys_device; desc.phys_device = phys_device;
@ -1011,7 +1011,7 @@ PhysicalDeviceSelector::PhysicalDeviceDesc PhysicalDeviceSelector::populate_devi
auto fill_chain = src_extended_features_chain; auto fill_chain = src_extended_features_chain;
if(!fill_chain.empty() && instance_version >= VK_API_VERSION_1_1) { if (!fill_chain.empty() && instance_version >= VK_API_VERSION_1_1) {
detail::GenericFeaturesPNextNode* prev = nullptr; detail::GenericFeaturesPNextNode* prev = nullptr;
for (auto& extension : fill_chain) { for (auto& extension : fill_chain) {
@ -1026,7 +1026,6 @@ PhysicalDeviceSelector::PhysicalDeviceDesc PhysicalDeviceSelector::populate_devi
local_features.pNext = &fill_chain.front(); local_features.pNext = &fill_chain.front();
detail::vulkan_functions().fp_vkGetPhysicalDeviceFeatures2(phys_device, &local_features); detail::vulkan_functions().fp_vkGetPhysicalDeviceFeatures2(phys_device, &local_features);
} }
desc.extended_features_chain = fill_chain; desc.extended_features_chain = fill_chain;
@ -1099,8 +1098,8 @@ PhysicalDeviceSelector::Suitable PhysicalDeviceSelector::is_device_suitable(Phys
return Suitable::no; return Suitable::no;
} }
bool required_features_supported = detail::supports_features(pd.device_features, criteria.required_features, bool required_features_supported = detail::supports_features(
pd.extended_features_chain, criteria.extended_features_chain); pd.device_features, criteria.required_features, pd.extended_features_chain, criteria.extended_features_chain);
if (!required_features_supported) return Suitable::no; if (!required_features_supported) return Suitable::no;
bool has_required_memory = false; bool has_required_memory = false;
@ -1151,9 +1150,8 @@ detail::Result<PhysicalDevice> PhysicalDeviceSelector::select() const {
std::vector<PhysicalDeviceDesc> phys_device_descriptions; std::vector<PhysicalDeviceDesc> phys_device_descriptions;
for (auto& phys_device : physical_devices) { for (auto& phys_device : physical_devices) {
phys_device_descriptions.push_back(populate_device_details(instance_info.version, phys_device_descriptions.push_back(populate_device_details(
phys_device, instance_info.version, phys_device, criteria.extended_features_chain));
criteria.extended_features_chain));
} }
PhysicalDeviceDesc selected_device{}; PhysicalDeviceDesc selected_device{};
@ -1419,8 +1417,8 @@ detail::Result<Device> DeviceBuilder::build() const {
VkDeviceCreateInfo device_create_info = {}; VkDeviceCreateInfo device_create_info = {};
#if defined(VK_API_VERSION_1_1) #if defined(VK_API_VERSION_1_1)
for(auto& pnext : info.pNext_chain) { for (auto& pnext : info.pNext_chain) {
if(pnext->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) { if (pnext->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) {
user_defined_phys_dev_features_2 = true; user_defined_phys_dev_features_2 = true;
break; break;
} }
@ -1430,7 +1428,7 @@ detail::Result<Device> DeviceBuilder::build() const {
VkPhysicalDeviceFeatures2 local_features2{}; VkPhysicalDeviceFeatures2 local_features2{};
local_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; local_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
if(!user_defined_phys_dev_features_2) { if (!user_defined_phys_dev_features_2) {
if (physical_device.instance_version >= VK_MAKE_VERSION(1, 1, 0)) { if (physical_device.instance_version >= VK_MAKE_VERSION(1, 1, 0)) {
local_features2.features = physical_device.features; local_features2.features = physical_device.features;
final_pnext_chain.push_back(reinterpret_cast<VkBaseOutStructure*>(&local_features2)); final_pnext_chain.push_back(reinterpret_cast<VkBaseOutStructure*>(&local_features2));
@ -1440,20 +1438,21 @@ detail::Result<Device> DeviceBuilder::build() const {
} }
} }
} else { } else {
printf("User provided VkPhysicalDeviceFeatures2 instance found in pNext chain. All requirements added via 'add_required_extension_features' will be ignored."); printf("User provided VkPhysicalDeviceFeatures2 instance found in pNext chain. All "
"requirements added via 'add_required_extension_features' will be ignored.");
} }
if(!user_defined_phys_dev_features_2 && !has_phys_dev_features_2) { if (!user_defined_phys_dev_features_2 && !has_phys_dev_features_2) {
device_create_info.pEnabledFeatures = &physical_device.features; device_create_info.pEnabledFeatures = &physical_device.features;
} }
#endif #endif
for(auto& pnext : info.pNext_chain) { for (auto& pnext : info.pNext_chain) {
final_pnext_chain.push_back(pnext); final_pnext_chain.push_back(pnext);
} }
detail::setup_pNext_chain(device_create_info, final_pnext_chain); detail::setup_pNext_chain(device_create_info, final_pnext_chain);
for(auto& node : final_pnext_chain) { for (auto& node : final_pnext_chain) {
assert(node->sType != VK_STRUCTURE_TYPE_APPLICATION_INFO); assert(node->sType != VK_STRUCTURE_TYPE_APPLICATION_INFO);
} }
@ -1698,7 +1697,7 @@ detail::Result<Swapchain> SwapchainBuilder::build() const {
VkSwapchainCreateInfoKHR swapchain_create_info = {}; VkSwapchainCreateInfoKHR swapchain_create_info = {};
swapchain_create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; swapchain_create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
detail::setup_pNext_chain(swapchain_create_info, info.pNext_chain); detail::setup_pNext_chain(swapchain_create_info, info.pNext_chain);
for(auto& node : info.pNext_chain) { for (auto& node : info.pNext_chain) {
assert(node->sType != VK_STRUCTURE_TYPE_APPLICATION_INFO); assert(node->sType != VK_STRUCTURE_TYPE_APPLICATION_INFO);
} }
swapchain_create_info.flags = info.create_flags; swapchain_create_info.flags = info.create_flags;

View File

@ -117,9 +117,10 @@ template <typename T> class Result {
struct GenericFeaturesPNextNode { struct GenericFeaturesPNextNode {
GenericFeaturesPNextNode() : sType(static_cast<VkStructureType>(0)), GenericFeaturesPNextNode() : sType(static_cast<VkStructureType>(0)), pNext(nullptr) {
pNext(nullptr) { for (auto& field : fields) {
memset(fields, 0, sizeof(fields)); field = 0;
}
} }
VkStructureType sType = static_cast<VkStructureType>(0); VkStructureType sType = static_cast<VkStructureType>(0);
@ -127,20 +128,15 @@ struct GenericFeaturesPNextNode {
static const uint32_t field_capacity = 256; static const uint32_t field_capacity = 256;
VkBool32 fields[field_capacity]; VkBool32 fields[field_capacity];
template <typename T> template <typename T> void set(T const& features) { *reinterpret_cast<T*>(this) = features; }
void set(T const& features) {
*reinterpret_cast<T*>(this) = features;
}
static bool match(GenericFeaturesPNextNode const& requested, GenericFeaturesPNextNode const& supported) { static bool match(GenericFeaturesPNextNode const& requested, GenericFeaturesPNextNode const& supported) {
assert(requested.sType == supported.sType && assert(requested.sType == supported.sType && "Non-matching sTypes in features nodes!");
"Non-matching sTypes in features nodes!");
for (uint32_t i = 0; i < field_capacity; i++) { for (uint32_t i = 0; i < field_capacity; i++) {
if (requested.fields[i] && !supported.fields[i]) return false; if (requested.fields[i] && !supported.fields[i]) return false;
} }
return true; return true;
} }
}; };
} // namespace detail } // namespace detail
@ -449,10 +445,12 @@ class PhysicalDeviceSelector {
#if defined(VK_API_VERSION_1_1) #if defined(VK_API_VERSION_1_1)
template <typename T> template <typename T>
PhysicalDeviceSelector& add_required_extension_features(T const& features) { PhysicalDeviceSelector& add_required_extension_features(T const& features) {
assert(features.sType != 0 && assert(features.sType != 0 && "Features struct sType must be filled with the struct's "
"Features struct sType must be filled with the struct's corresponding VkStructureType enum"); "corresponding VkStructureType enum");
assert(features.sType != VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2 && assert(
"Do not pass VkPhysicalDeviceFeatures2 as a required extension feature structure. An instance of this is managed internally for selection criteria and device creation."); features.sType != VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2 &&
"Do not pass VkPhysicalDeviceFeatures2 as a required extension feature structure. An "
"instance of this is managed internally for selection criteria and device creation.");
detail::GenericFeaturesPNextNode node; detail::GenericFeaturesPNextNode node;
node.set(features); node.set(features);
criteria.extended_features_chain.push_back(node); criteria.extended_features_chain.push_back(node);
@ -499,8 +497,8 @@ class PhysicalDeviceSelector {
#endif #endif
}; };
// We copy the extension features stored in the selector criteria under the prose of a "template" to // We copy the extension features stored in the selector criteria under the prose of a
// ensure that after fetching everything is compared 1:1 during a match. // "template" to ensure that after fetching everything is compared 1:1 during a match.
PhysicalDeviceDesc populate_device_details(uint32_t instance_version, PhysicalDeviceDesc populate_device_details(uint32_t instance_version,
VkPhysicalDevice phys_device, VkPhysicalDevice phys_device,