diff --git a/include/vsg/vk/DeviceFeatures.h b/include/vsg/vk/DeviceFeatures.h index c9aa381fc..b0bc60602 100644 --- a/include/vsg/vk/DeviceFeatures.h +++ b/include/vsg/vk/DeviceFeatures.h @@ -2,7 +2,7 @@ /* -Copyright(c) 2021 Robert Osfield +Copyright(c) 2021 Robert Osfield, 2026 Stefan Kaps Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: @@ -12,7 +12,8 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI */ -#include +#include +#include #include #include @@ -37,15 +38,21 @@ namespace vsg template FeatureStruct& get() { - if (auto itr = _features.find(type); itr != _features.end()) return *reinterpret_cast(itr->second.first); - - FeatureStruct* feature = new FeatureStruct{}; - - feature->sType = type; - feature->pNext = nullptr; - - _features[type].first = reinterpret_cast(feature); - _features[type].second = [](FeatureHeader* ptr) { delete reinterpret_cast(ptr); }; + auto it = std::find_if(_features.begin(), _features.end(), + [](const auto& p) { return p.first->sType == type; }); + if (it != _features.end()) return *reinterpret_cast(it->first); + + if constexpr (type == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) + it = _features.begin(); + else + { // as soon as we WILL create another type, we also need the base + if (_features.front().first->sType != VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) + get(); + it = _features.end(); + } + + FeatureStruct* feature = new FeatureStruct{type, nullptr}; + _features.emplace(it, (FeatureHeader*)feature, [](FeatureHeader* ptr) { delete reinterpret_cast(ptr); }); return *feature; } @@ -74,7 +81,7 @@ namespace vsg using DeleteHandler = void (*)(FeatureHeader* ptr); - std::map> _features; + std::list> _features; }; VSG_type_name(vsg::DeviceFeatures); diff --git a/src/vsg/vk/DeviceFeatures.cpp b/src/vsg/vk/DeviceFeatures.cpp index 2ed841044..a0ed5a059 100644 --- a/src/vsg/vk/DeviceFeatures.cpp +++ b/src/vsg/vk/DeviceFeatures.cpp @@ -14,9 +14,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI using namespace vsg; -DeviceFeatures::DeviceFeatures() -{ -} +DeviceFeatures::DeviceFeatures() {} DeviceFeatures::~DeviceFeatures() { @@ -32,7 +30,7 @@ void DeviceFeatures::clear() { for (auto& feature : _features) { - feature.second.second(feature.second.first); + feature.second(feature.first); } _features.clear(); @@ -43,13 +41,13 @@ void* DeviceFeatures::data() const if (_features.empty()) return nullptr; // chain the Feature pNext pointers together - FeatureHeader* previous = nullptr; - for (auto itr = _features.rbegin(); itr != _features.rend(); ++itr) - { - itr->second.first->pNext = previous; - previous = itr->second.first; - } + FeatureHeader *first{nullptr}, *prev{nullptr}; + for (auto it : _features) + if (first) + prev->pNext = it.first, prev = it.first; + else + first = prev = it.first; // return head of the chain - return const_cast(reinterpret_cast(_features.begin()->second.first)); + return const_cast(reinterpret_cast(first)); }