Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

layer: Add string to int #53

Merged
merged 1 commit into from
Aug 4, 2023
Merged
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
40 changes: 40 additions & 0 deletions src/layer/layer_settings_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,46 @@ std::string ToUpper(const std::string &s) {
return result;
}

uint32_t ToUint32(const std::string &token) {
uint32_t int_id = 0;
if ((token.find("0x") == 0) || token.find("0X") == 0) { // Handle hex format
int_id = static_cast<uint32_t>(std::strtoul(token.c_str(), nullptr, 16));
} else {
int_id = static_cast<uint32_t>(std::strtoul(token.c_str(), nullptr, 10)); // Decimal format
}
return int_id;
}

uint64_t ToUint64(const std::string &token) {
uint64_t int_id = 0;
if ((token.find("0x") == 0) || token.find("0X") == 0) { // Handle hex format
int_id = static_cast<uint64_t>(std::strtoull(token.c_str(), nullptr, 16));
} else {
int_id = static_cast<uint64_t>(std::strtoull(token.c_str(), nullptr, 10)); // Decimal format
}
return int_id;
}

int32_t ToInt32(const std::string &token) {
int32_t int_id = 0;
if (token.find("0x") == 0 || token.find("0X") == 0 || token.find("-0x") == 0 || token.find("-0X") == 0) { // Handle hex format
int_id = static_cast<int32_t>(std::strtol(token.c_str(), nullptr, 16));
} else {
int_id = static_cast<int32_t>(std::strtol(token.c_str(), nullptr, 10)); // Decimal format
}
return int_id;
}

int64_t ToInt64(const std::string &token) {
int64_t int_id = 0;
if (token.find("0x") == 0 || token.find("0X") == 0 || token.find("-0x") == 0 || token.find("-0X") == 0) { // Handle hex format
int_id = static_cast<uint64_t>(std::strtoll(token.c_str(), nullptr, 16));
} else {
int_id = static_cast<uint64_t>(std::strtoll(token.c_str(), nullptr, 10)); // Decimal format
}
return int_id;
}

VkFrameset ToFrameSet(const std::string &s) {
assert(IsFrameSets(s));

Expand Down
8 changes: 8 additions & 0 deletions src/layer/layer_settings_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ namespace vl {

std::string ToUpper(const std::string &s);

uint32_t ToUint32(const std::string &token);

uint64_t ToUint64(const std::string &token);

int32_t ToInt32(const std::string &token);

int64_t ToInt64(const std::string &token);

bool IsFrameSets(const std::string &s);

VkFrameset ToFrameSet(const std::string &s);
Expand Down
109 changes: 79 additions & 30 deletions src/layer/vk_layer_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,45 +462,94 @@ VkResult vlGetLayerSettingValues(VlLayerSettingSet layerSettingSet, const char *
return result;
}
case VK_LAYER_SETTING_TYPE_STRING_EXT: {
std::vector<const char*> values;
VkResult result = VK_SUCCESS;
VkResult result = VK_SUCCESS;

if (!settings.empty()) { // From env variable or setting file
std::vector<std::string> &settings_cache = layer_setting_set->GetSettingCache(pSettingName);
settings_cache = settings;
std::vector<std::string> &settings_cache = layer_setting_set->GetSettingCache(pSettingName);

if (copy_values) {
if (static_cast<std::size_t>(*pValueCount) < settings_cache.size()) {
result = VK_INCOMPLETE;
}
values.resize(std::min(static_cast<std::size_t>(*pValueCount), settings_cache.size()));
if (!settings.empty()) { // From env variable or setting file
settings_cache = settings;

for (std::size_t i = 0, n = values.size(); i < n; ++i) {
values[i] = settings_cache[i].c_str();
}
} else {
*pValueCount = static_cast<std::uint32_t>(settings_cache.size());
}
} else if (api_setting != nullptr) { // From Vulkan Layer Setting API
if (api_setting->type != type) {
result = VK_ERROR_FORMAT_NOT_SUPPORTED;
} else if (copy_values) {
if (*pValueCount < api_setting->count) {
result = VK_INCOMPLETE;
}
const std::uint32_t size = std::min(*pValueCount, api_setting->count);
values.assign(api_setting->asString, api_setting->asString + size);
} else {
*pValueCount = api_setting->count;
if (copy_values) {
if (static_cast<std::size_t>(*pValueCount) < settings_cache.size()) {
result = VK_INCOMPLETE;
}
} else {
*pValueCount = static_cast<std::uint32_t>(settings_cache.size());
}

} else if (api_setting != nullptr) { // From Vulkan Layer Setting API
if (copy_values) {
std::copy(values.begin(), values.end(), reinterpret_cast<const char **>(pValues));
if (*pValueCount < api_setting->count) {
result = VK_INCOMPLETE;
}
const std::uint32_t size = std::min(*pValueCount, api_setting->count);
settings_cache.resize(size);

switch (api_setting->type) {
case VK_LAYER_SETTING_TYPE_STRING_EXT:
for (std::size_t i = 0, n = settings_cache.size(); i < n; ++i) {
settings_cache[i] = api_setting->asString[i];
}
break;
case VK_LAYER_SETTING_TYPE_BOOL_EXT:
for (std::size_t i = 0, n = settings_cache.size(); i < n; ++i) {
settings_cache[i] = api_setting->asBool32[i] == VK_TRUE ? "true" : "false";
}
break;
case VK_LAYER_SETTING_TYPE_INT32_EXT:
for (std::size_t i = 0, n = settings_cache.size(); i < n; ++i) {
settings_cache[i] = vl::Format("%d", api_setting->asInt32[i]);
}
break;
case VK_LAYER_SETTING_TYPE_INT64_EXT:
for (std::size_t i = 0, n = settings_cache.size(); i < n; ++i) {
settings_cache[i] = vl::Format("%d", api_setting->asInt64[i]);
}
break;
case VK_LAYER_SETTING_TYPE_UINT32_EXT:
for (std::size_t i = 0, n = settings_cache.size(); i < n; ++i) {
settings_cache[i] = vl::Format("%u", api_setting->asUint32[i]);
}
break;
case VK_LAYER_SETTING_TYPE_UINT64_EXT:
for (std::size_t i = 0, n = settings_cache.size(); i < n; ++i) {
settings_cache[i] = vl::Format("%u", api_setting->asUint64[i]);
}
break;
case VK_LAYER_SETTING_TYPE_FLOAT_EXT:
for (std::size_t i = 0, n = settings_cache.size(); i < n; ++i) {
settings_cache[i] = vl::Format("%f", api_setting->asFloat[i]);
}
break;
case VK_LAYER_SETTING_TYPE_DOUBLE_EXT:
for (std::size_t i = 0, n = settings_cache.size(); i < n; ++i) {
settings_cache[i] = vl::Format("%f", api_setting->asDouble[i]);
}
break;
case VK_LAYER_SETTING_TYPE_FRAMESET_EXT:
for (std::size_t i = 0, n = settings_cache.size(); i < n; ++i) {
settings_cache[i] = vl::Format("%d-%d-%d",
api_setting->asFrameset[i].first,
api_setting->asFrameset[i].count,
api_setting->asFrameset[i].step);
}
break;
default:
result = VK_ERROR_FORMAT_NOT_SUPPORTED;
break;
}
} else {
*pValueCount = api_setting->count;
}
}

return result;
if (copy_values) {
for (std::size_t i = 0, n = std::min(static_cast<std::size_t>(*pValueCount), settings_cache.size()); i < n; ++i) {
reinterpret_cast<const char **>(pValues)[i] = settings_cache[i].c_str();
}
}

return result;
}
}

return VK_ERROR_UNKNOWN;
Expand Down
19 changes: 19 additions & 0 deletions tests/layer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,22 @@ target_link_libraries(test_layer_setting_file PRIVATE

gtest_discover_tests(test_layer_setting_file)

# test_layer_setting_cast
add_executable(test_layer_setting_cast)

target_include_directories(test_layer_setting_cast PRIVATE
${CMAKE_SOURCE_DIR}/src/layer
)

target_sources(test_layer_setting_cast PRIVATE
test_setting_cast.cpp
)

target_link_libraries(test_layer_setting_cast PRIVATE
GTest::gtest
GTest::gtest_main
Vulkan::Headers
Vulkan::LayerSettings
)

gtest_discover_tests(test_layer_setting_cast)
Loading