postprocessing_shader.cpp (3594B)
1 // SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com> 2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) 3 4 #include "postprocessing_shader.h" 5 6 #include "common/file_system.h" 7 #include "common/log.h" 8 #include "common/string_util.h" 9 10 #include <cctype> 11 #include <cstring> 12 #include <sstream> 13 14 Log_SetChannel(PostProcessing); 15 16 void PostProcessing::Shader::ParseKeyValue(std::string_view line, std::string_view* key, std::string_view* value) 17 { 18 size_t key_start = 0; 19 while (key_start < line.size() && std::isspace(line[key_start])) 20 key_start++; 21 22 size_t key_end = key_start; 23 while (key_end < line.size() && (!std::isspace(line[key_end]) && line[key_end] != '=')) 24 key_end++; 25 26 if (key_start == key_end || key_end == line.size()) 27 return; 28 29 size_t value_start = key_end; 30 while (value_start < line.size() && std::isspace(line[value_start])) 31 value_start++; 32 33 if (value_start == line.size() || line[value_start] != '=') 34 return; 35 36 value_start++; 37 while (value_start < line.size() && std::isspace(line[value_start])) 38 value_start++; 39 40 size_t value_end = line.size(); 41 while (value_end > value_start && std::isspace(line[value_end - 1])) 42 value_end--; 43 44 if (value_start == value_end) 45 return; 46 47 *key = line.substr(key_start, key_end - key_start); 48 *value = line.substr(value_start, value_end - value_start); 49 } 50 51 PostProcessing::Shader::Shader() = default; 52 53 PostProcessing::Shader::Shader(std::string name) : m_name(std::move(name)) 54 { 55 } 56 57 PostProcessing::Shader::~Shader() = default; 58 59 bool PostProcessing::Shader::IsValid() const 60 { 61 return false; 62 } 63 64 std::vector<PostProcessing::ShaderOption> PostProcessing::Shader::TakeOptions() 65 { 66 return std::move(m_options); 67 } 68 69 void PostProcessing::Shader::LoadOptions(const SettingsInterface& si, const char* section) 70 { 71 for (ShaderOption& option : m_options) 72 { 73 if (option.type == ShaderOption::Type::Bool) 74 { 75 const bool new_value = si.GetBoolValue(section, option.name.c_str(), option.default_value[0].int_value != 0); 76 if ((option.value[0].int_value != 0) != new_value) 77 { 78 option.value[0].int_value = new_value ? 1 : 0; 79 OnOptionChanged(option); 80 } 81 } 82 else 83 { 84 ShaderOption::ValueVector value = option.default_value; 85 86 std::string config_value; 87 if (si.GetStringValue(section, option.name.c_str(), &config_value)) 88 { 89 const u32 value_vector_size = (option.type == ShaderOption::Type::Int) ? 90 ShaderOption::ParseIntVector(config_value, &value) : 91 ShaderOption::ParseFloatVector(config_value, &value); 92 if (value_vector_size != option.vector_size) 93 { 94 WARNING_LOG("Only got {} of {} elements for '{}' in config section {}.", value_vector_size, 95 option.vector_size, option.name, section); 96 } 97 } 98 99 if (std::memcmp(&option.value, &value, sizeof(value)) != 0) 100 { 101 option.value = value; 102 OnOptionChanged(option); 103 } 104 } 105 } 106 } 107 108 const PostProcessing::ShaderOption* PostProcessing::Shader::GetOptionByName(std::string_view name) const 109 { 110 for (const ShaderOption& option : m_options) 111 { 112 if (option.name == name) 113 return &option; 114 } 115 116 return nullptr; 117 } 118 119 PostProcessing::ShaderOption* PostProcessing::Shader::GetOptionByName(std::string_view name) 120 { 121 for (ShaderOption& option : m_options) 122 { 123 if (option.name == name) 124 return &option; 125 } 126 127 return nullptr; 128 } 129 130 void PostProcessing::Shader::OnOptionChanged(const ShaderOption& option) 131 { 132 }