Skip to content

Commit

Permalink
vkconfig3: Fix new configuration and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christophe-lunarg committed Oct 3, 2024
1 parent 9d65bdd commit cd8dd2e
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 22 deletions.
10 changes: 6 additions & 4 deletions vkconfig_core/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ static void AddApplicationEnabledParameters(std::vector<Parameter>& parameters)
}
}

Configuration Configuration::Create(const LayerManager& layers, const std::string& key) {
Configuration Configuration::Create(const LayerManager& layers, const std::string& configuration_key) {
Configuration result;

result.key = key;
result.key = configuration_key;
result.GatherParameters(layers);

AddApplicationEnabledParameters(result.parameters);
Expand Down Expand Up @@ -409,7 +409,7 @@ void Configuration::SwitchLayerVersion(const LayerManager& layers, const std::st

const Layer* new_layer = layers.Find(layer_key, version);

parameter->api_version = version;
parameter->api_version = new_layer->api_version == version ? version : Version::LATEST;
CollectDefaultSettingData(new_layer->settings, parameter->settings);
}

Expand Down Expand Up @@ -455,7 +455,9 @@ void Configuration::Reorder(const std::vector<std::string>& layer_names) {

for (std::size_t i = 0, n = layer_names.size(); i < n; ++i) {
Parameter* parameter = this->Find(layer_names[i]);
assert(parameter != nullptr);
if (parameter == nullptr) {
continue;
}
ordered_parameters.push_back(*parameter);
}

Expand Down
2 changes: 1 addition & 1 deletion vkconfig_core/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LayerManager;
class Configuration {
public:
static Configuration CreateDisabled(const LayerManager& layers);
static Configuration Create(const LayerManager& layers, const std::string& layer_key);
static Configuration Create(const LayerManager& layers, const std::string& configuration_key);

bool Load(const Path& full_path, const LayerManager& layers);
bool Save(const Path& full_path, bool exporter = false) const;
Expand Down
39 changes: 26 additions & 13 deletions vkconfig_core/configuration_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,27 +255,40 @@ bool ConfigurationManager::HasActiveConfiguration() const {
return false;
}

Configuration &ConfigurationManager::CreateConfiguration(const LayerManager &layers, const std::string &configuration_name,
bool duplicate) {
Configuration *duplicate_configuration = this->FindConfiguration(configuration_name);
Configuration &ConfigurationManager::CreateConfiguration(const LayerManager &layers, const std::string &configuration_name) {
std::string configuration_key = MakeConfigurationName(available_configurations, configuration_name);

Configuration new_configuration = duplicate_configuration != nullptr && duplicate ? *duplicate_configuration : Configuration();
new_configuration.key = MakeConfigurationName(available_configurations, configuration_name);
Configuration new_configuration = Configuration::Create(layers, configuration_key);

const Path &path(MakeConfigurationPath(new_configuration.key));
new_configuration.Save(path);
this->available_configurations.push_back(new_configuration);
this->SortConfigurations();

this->GetActiveConfigurationInfo()->name = new_configuration.key;

return *this->FindConfiguration(new_configuration.key);
}

Configuration &ConfigurationManager::DuplicateConfiguration(const LayerManager &layers, const std::string &configuration_name) {
Configuration *source_configuration = this->FindConfiguration(configuration_name);
assert(source_configuration != nullptr);

Configuration duplicated_configuration = *source_configuration;
duplicated_configuration.key = MakeConfigurationName(available_configurations, configuration_name);

const Path &path = MakeConfigurationPath(duplicated_configuration.key);
duplicated_configuration.Save(path);

// Reload from file to workaround the lack of SettingSet copy support
Configuration configuration;
const bool result = configuration.Load(path, layers);
Configuration reloaded_configuration;
const bool result = reloaded_configuration.Load(path, layers);
assert(result);

this->available_configurations.push_back(configuration);
this->SortConfigurations();
this->available_configurations.push_back(reloaded_configuration);
this->SortConfigurations(); // invalidated all pointers to configuration object

this->GetActiveConfigurationInfo()->name = configuration.key;
this->GetActiveConfigurationInfo()->name = reloaded_configuration.key;

return *this->FindConfiguration(configuration.key);
return *this->FindConfiguration(reloaded_configuration.key);
}

bool ConfigurationManager::HasFile(const Configuration &configuration) const {
Expand Down
3 changes: 2 additions & 1 deletion vkconfig_core/configuration_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class ConfigurationManager : public Serialize {
const std::map<std::string, ConfigurationInfo>& GetConfigurationInfos() const;
bool HasActiveConfiguration() const;

Configuration& CreateConfiguration(const LayerManager& layers, const std::string& configuration_name, bool duplicate = false);
Configuration& CreateConfiguration(const LayerManager& layers, const std::string& configuration_name);
Configuration& DuplicateConfiguration(const LayerManager& layers, const std::string& configuration_name);
void RemoveConfiguration(const std::string& configuration_name);
int GetConfigurationIndex(const std::string& configuration_name) const;

Expand Down
191 changes: 189 additions & 2 deletions vkconfig_core/test/test_configuration.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2020-2021 Valve Corporation
* Copyright (c) 2020-2021 LunarG, Inc.
* Copyright (c) 2020-2024 Valve Corporation
* Copyright (c) 2020-2024 LunarG, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
*/

#include "../configuration.h"
#include "../layer_manager.h"
#include "../util.h"

#include <array>
Expand Down Expand Up @@ -195,3 +196,189 @@ TEST(test_configuration, make_duplicate_tagged_name_mix) {
EXPECT_STREQ("Tag Configuration (tag) Bla", new_name.c_str());
EXPECT_TRUE(!new_name.empty());
}

TEST(test_configuration, create) {
LayerManager layer_manager;
layer_manager.LoadLayersFromPath(":/layers");

Configuration configuration = Configuration::Create(layer_manager, "New Configuration");

bool has_app_api = false;
bool has_app_env = false;

for (std::size_t i = 0, n = configuration.parameters.size(); i < n; ++i) {
if (configuration.parameters[i].control == LAYER_CONTROL_APPLICATIONS_API) {
has_app_api = true;
continue;
}

if (configuration.parameters[i].control == LAYER_CONTROL_APPLICATIONS_ENV) {
has_app_env = true;
continue;
}

EXPECT_EQ(configuration.parameters[i].control, LAYER_CONTROL_AUTO);
}

EXPECT_EQ(has_app_api, true);
EXPECT_EQ(has_app_env, true);
}

TEST(test_configuration, create_disabled) {
LayerManager layer_manager;
layer_manager.LoadLayersFromPath(":/layers");

Configuration configuration = Configuration::CreateDisabled(layer_manager);

bool has_app_api = false;
bool has_app_env = false;

for (std::size_t i = 0, n = configuration.parameters.size(); i < n; ++i) {
if (configuration.parameters[i].control == LAYER_CONTROL_APPLICATIONS_API) {
has_app_api = true;
continue;
}

if (configuration.parameters[i].control == LAYER_CONTROL_APPLICATIONS_ENV) {
has_app_env = true;
continue;
}

EXPECT_EQ(configuration.parameters[i].control, LAYER_CONTROL_OFF);
}

EXPECT_EQ(has_app_api, true);
EXPECT_EQ(has_app_env, true);
}

TEST(test_configuration, SwitchLayerVersion) {
LayerManager layer_manager;
layer_manager.LoadLayersFromPath(":/layers");

Configuration configuration = Configuration::Create(layer_manager, "New Configuration");
for (std::size_t i = 0, n = configuration.parameters.size(); i < n; ++i) {
EXPECT_EQ(configuration.parameters[i].api_version, Version::LATEST);
}
Parameter* parameter_latest = configuration.Find("VK_LAYER_LUNARG_version");
EXPECT_EQ(parameter_latest->api_version, Version::LATEST);

configuration.SwitchLayerVersion(layer_manager, "VK_LAYER_LUNARG_version", Version(1, 3, 204));
Parameter* parameter_204 = configuration.Find("VK_LAYER_LUNARG_version");
EXPECT_EQ(parameter_204->api_version, Version(1, 3, 204));

configuration.SwitchLayerVersion(layer_manager, "VK_LAYER_LUNARG_version", Version::LATEST);
Parameter* parameter_restore = configuration.Find("VK_LAYER_LUNARG_version");
EXPECT_EQ(parameter_restore->api_version, Version::LATEST);

configuration.SwitchLayerVersion(layer_manager, "VK_LAYER_LUNARG_version", Version(1, 3, 205));
Parameter* parameter_version_not_found = configuration.Find("VK_LAYER_LUNARG_version");
EXPECT_EQ(parameter_version_not_found->api_version, Version::LATEST);
}

TEST(test_configuration, Reorder_full) {
LayerManager layer_manager;
layer_manager.LoadLayersFromPath(":/layers");

Configuration configuration = Configuration::Create(layer_manager, "New Configuration");

std::vector<std::string> layer_names;
layer_names.push_back("VK_LAYER_LUNARG_test_04");
layer_names.push_back("VK_LAYER_LUNARG_test_01");
layer_names.push_back("VK_LAYER_LUNARG_test_02");
layer_names.push_back("Vulkan Layers from the Application Vulkan API");
layer_names.push_back("Vulkan Layers from Application Environment Variables");
layer_names.push_back("VK_LAYER_LUNARG_version");
layer_names.push_back("VK_LAYER_LUNARG_reference_1_2_1");
layer_names.push_back("VK_LAYER_LUNARG_reference_1_2_0");
layer_names.push_back("VK_LAYER_LUNARG_reference_1_1_0");
layer_names.push_back("VK_LAYER_LUNARG_test_00");
layer_names.push_back("VK_LAYER_LUNARG_test_05");
layer_names.push_back("VK_LAYER_LUNARG_test_06");
layer_names.push_back("VK_LAYER_LUNARG_test_03");

configuration.Reorder(layer_names);

EXPECT_STREQ(configuration.parameters[0].key.c_str(), "VK_LAYER_LUNARG_test_04");
EXPECT_STREQ(configuration.parameters[1].key.c_str(), "VK_LAYER_LUNARG_test_01");
EXPECT_STREQ(configuration.parameters[2].key.c_str(), "VK_LAYER_LUNARG_test_02");
EXPECT_STREQ(configuration.parameters[3].key.c_str(), "Vulkan Layers from the Application Vulkan API");
EXPECT_STREQ(configuration.parameters[4].key.c_str(), "Vulkan Layers from Application Environment Variables");
EXPECT_STREQ(configuration.parameters[5].key.c_str(), "VK_LAYER_LUNARG_version");
EXPECT_STREQ(configuration.parameters[6].key.c_str(), "VK_LAYER_LUNARG_reference_1_2_1");
EXPECT_STREQ(configuration.parameters[7].key.c_str(), "VK_LAYER_LUNARG_reference_1_2_0");
EXPECT_STREQ(configuration.parameters[8].key.c_str(), "VK_LAYER_LUNARG_reference_1_1_0");
EXPECT_STREQ(configuration.parameters[9].key.c_str(), "VK_LAYER_LUNARG_test_00");
EXPECT_STREQ(configuration.parameters[10].key.c_str(), "VK_LAYER_LUNARG_test_05");
EXPECT_STREQ(configuration.parameters[11].key.c_str(), "VK_LAYER_LUNARG_test_06");
EXPECT_STREQ(configuration.parameters[12].key.c_str(), "VK_LAYER_LUNARG_test_03");
}

TEST(test_configuration, Reorder_partial) {
LayerManager layer_manager;
layer_manager.LoadLayersFromPath(":/layers");

Configuration configuration = Configuration::Create(layer_manager, "New Configuration");

std::vector<std::string> layer_names;
layer_names.push_back("VK_LAYER_LUNARG_test_04");
layer_names.push_back("Vulkan Layers from the Application Vulkan API");
layer_names.push_back("VK_LAYER_LUNARG_version");
layer_names.push_back("VK_LAYER_LUNARG_reference_1_2_1");
layer_names.push_back("VK_LAYER_LUNARG_reference_1_2_0");
layer_names.push_back("VK_LAYER_LUNARG_reference_1_1_0");
layer_names.push_back("VK_LAYER_LUNARG_test_06");
layer_names.push_back("VK_LAYER_LUNARG_test_03");

configuration.Reorder(layer_names);

EXPECT_STREQ(configuration.parameters[0].key.c_str(), "VK_LAYER_LUNARG_test_04");
EXPECT_STREQ(configuration.parameters[1].key.c_str(), "Vulkan Layers from the Application Vulkan API");
EXPECT_STREQ(configuration.parameters[2].key.c_str(), "VK_LAYER_LUNARG_version");
EXPECT_STREQ(configuration.parameters[3].key.c_str(), "VK_LAYER_LUNARG_reference_1_2_1");
EXPECT_STREQ(configuration.parameters[4].key.c_str(), "VK_LAYER_LUNARG_reference_1_2_0");
EXPECT_STREQ(configuration.parameters[5].key.c_str(), "VK_LAYER_LUNARG_reference_1_1_0");
EXPECT_STREQ(configuration.parameters[6].key.c_str(), "VK_LAYER_LUNARG_test_06");
EXPECT_STREQ(configuration.parameters[7].key.c_str(), "VK_LAYER_LUNARG_test_03");
}

TEST(test_configuration, Reorder_missing) {
LayerManager layer_manager;
layer_manager.LoadLayersFromPath(":/layers");

Configuration configuration = Configuration::Create(layer_manager, "New Configuration");

std::vector<std::string> layer_names;
layer_names.push_back("VK_LAYER_LUNARG_test_04");
layer_names.push_back("Vulkan Layers from the Application Vulkan API");
layer_names.push_back("VK_LAYER_LUNARG_missing_0");
layer_names.push_back("VK_LAYER_LUNARG_version");
layer_names.push_back("VK_LAYER_LUNARG_reference_1_2_1");
layer_names.push_back("VK_LAYER_LUNARG_reference_1_2_0");
layer_names.push_back("VK_LAYER_LUNARG_reference_1_1_0");
layer_names.push_back("VK_LAYER_LUNARG_test_06");
layer_names.push_back("VK_LAYER_LUNARG_test_03");
layer_names.push_back("VK_LAYER_LUNARG_missing_1");

configuration.Reorder(layer_names);

EXPECT_STREQ(configuration.parameters[0].key.c_str(), "VK_LAYER_LUNARG_test_04");
EXPECT_STREQ(configuration.parameters[1].key.c_str(), "Vulkan Layers from the Application Vulkan API");
EXPECT_STREQ(configuration.parameters[2].key.c_str(), "VK_LAYER_LUNARG_version");
EXPECT_STREQ(configuration.parameters[3].key.c_str(), "VK_LAYER_LUNARG_reference_1_2_1");
EXPECT_STREQ(configuration.parameters[4].key.c_str(), "VK_LAYER_LUNARG_reference_1_2_0");
EXPECT_STREQ(configuration.parameters[5].key.c_str(), "VK_LAYER_LUNARG_reference_1_1_0");
EXPECT_STREQ(configuration.parameters[6].key.c_str(), "VK_LAYER_LUNARG_test_06");
EXPECT_STREQ(configuration.parameters[7].key.c_str(), "VK_LAYER_LUNARG_test_03");
}

TEST(test_configuration, Reorder_empty) {
LayerManager layer_manager;
layer_manager.LoadLayersFromPath(":/layers");

Configuration configuration = Configuration::Create(layer_manager, "New Configuration");

std::vector<std::string> layer_names;
configuration.Reorder(layer_names);

EXPECT_TRUE(configuration.parameters.empty());
}
5 changes: 4 additions & 1 deletion vkconfig_gui/tab_configurations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,9 @@ void TabConfigurations::OnContextMenuNewClicked(ConfigurationListItem *item) {
configurator.Override(OVERRIDE_AREA_ALL);

this->UpdateUI_Configurations(UPDATE_REBUILD_UI);
this->UpdateUI_LoaderMessages();
this->UpdateUI_Layers(UPDATE_REBUILD_UI);
this->UpdateUI_Settings(UPDATE_REBUILD_UI);
}

void TabConfigurations::OnContextMenuImportClicked(ConfigurationListItem *item) {
Expand Down Expand Up @@ -629,7 +632,7 @@ void TabConfigurations::OnContextMenuDuplicateClicked(ConfigurationListItem *ite

Configurator &configurator = Configurator::Get();
const Configuration &duplicated_configuration =
configurator.configurations.CreateConfiguration(configurator.layers, item->configuration_name, true);
configurator.configurations.DuplicateConfiguration(configurator.layers, item->configuration_name);

item->configuration_name = duplicated_configuration.key;

Expand Down

0 comments on commit cd8dd2e

Please sign in to comment.