Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions include/vsg/vk/DeviceFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* <editor-fold desc="MIT License">

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:

Expand All @@ -12,7 +12,8 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI

</editor-fold> */

#include <map>
#include <algorithm>
#include <list>
#include <vsg/io/Logger.h>
#include <vsg/vk/PhysicalDevice.h>

Expand All @@ -37,15 +38,21 @@ namespace vsg
template<typename FeatureStruct, VkStructureType type>
FeatureStruct& get()
{
if (auto itr = _features.find(type); itr != _features.end()) return *reinterpret_cast<FeatureStruct*>(itr->second.first);

FeatureStruct* feature = new FeatureStruct{};

feature->sType = type;
feature->pNext = nullptr;

_features[type].first = reinterpret_cast<FeatureHeader*>(feature);
_features[type].second = [](FeatureHeader* ptr) { delete reinterpret_cast<FeatureStruct*>(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<FeatureStruct*>(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<VkPhysicalDeviceFeatures2, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2>();
it = _features.end();
}

FeatureStruct* feature = new FeatureStruct{type, nullptr};
_features.emplace(it, (FeatureHeader*)feature, [](FeatureHeader* ptr) { delete reinterpret_cast<FeatureStruct*>(ptr); });

return *feature;
}
Expand Down Expand Up @@ -74,7 +81,7 @@ namespace vsg

using DeleteHandler = void (*)(FeatureHeader* ptr);

std::map<VkStructureType, std::pair<FeatureHeader*, DeleteHandler>> _features;
std::list<std::pair<FeatureHeader*, DeleteHandler>> _features;
};
VSG_type_name(vsg::DeviceFeatures);

Expand Down
20 changes: 9 additions & 11 deletions src/vsg/vk/DeviceFeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -32,7 +30,7 @@ void DeviceFeatures::clear()
{
for (auto& feature : _features)
{
feature.second.second(feature.second.first);
feature.second(feature.first);
}

_features.clear();
Expand All @@ -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<void*>(reinterpret_cast<const void*>(_features.begin()->second.first));
return const_cast<void*>(reinterpret_cast<const void*>(first));
}
Loading