From 303a2b56249a06b74b281f9c8aa55dc66ec8af74 Mon Sep 17 00:00:00 2001 From: Almamu Date: Fri, 15 Aug 2025 07:20:15 +0200 Subject: [PATCH] chore: DynamicValues now keep track of current type, colors should be converted to vec4/ivec4 upon reading so alpha is correct, keep combo overrides too --- .../Core/DynamicValues/CDynamicValue.cpp | 13 +++++++ .../Core/DynamicValues/CDynamicValue.h | 19 +++++++++- .../Data/Parsers/ObjectParser.cpp | 19 +++++++--- .../Render/Objects/Effects/CPass.cpp | 4 +- .../Render/Shaders/CShader.cpp | 9 +++-- src/WallpaperEngine/Render/Shaders/CShader.h | 8 +++- .../Render/Shaders/CShaderUnit.cpp | 37 ++++++++++++++++--- .../Render/Shaders/CShaderUnit.h | 6 ++- 8 files changed, 93 insertions(+), 22 deletions(-) diff --git a/src/WallpaperEngine/Core/DynamicValues/CDynamicValue.cpp b/src/WallpaperEngine/Core/DynamicValues/CDynamicValue.cpp index 2353c1e..f7c7750 100644 --- a/src/WallpaperEngine/Core/DynamicValues/CDynamicValue.cpp +++ b/src/WallpaperEngine/Core/DynamicValues/CDynamicValue.cpp @@ -19,6 +19,7 @@ void CDynamicValue::update(float newValue) { this->m_float = newValue; this->m_int = static_cast (newValue); this->m_bool = static_cast (newValue) != 0; + this->m_type = UnderlyingType::Float; this->propagate (); } @@ -33,6 +34,7 @@ void CDynamicValue::update(int newValue) { this->m_float = static_cast (newValue); this->m_int = newValue; this->m_bool = newValue != 0; + this->m_type = UnderlyingType::Int; this->propagate (); } @@ -47,6 +49,7 @@ void CDynamicValue::update(bool newValue) { this->m_float = newValue; this->m_int = newValue; this->m_bool = newValue; + this->m_type = UnderlyingType::Boolean; this->propagate (); } @@ -61,6 +64,7 @@ void CDynamicValue::update(const glm::vec2& newValue) { this->m_float = newValue.x; this->m_int = static_cast (newValue.x); this->m_bool = newValue.x != 0.0f; + this->m_type = UnderlyingType::Vec2; this->propagate (); } @@ -75,6 +79,7 @@ void CDynamicValue::update(const glm::vec3& newValue) { this->m_float = newValue.x; this->m_int = static_cast (newValue.x); this->m_bool = newValue.x != 0.0f; + this->m_type = UnderlyingType::Vec3; this->propagate (); } @@ -89,6 +94,7 @@ void CDynamicValue::update(const glm::vec4& newValue) { this->m_float = newValue.x; this->m_int = static_cast (newValue.x); this->m_bool = newValue.x != 0.0f; + this->m_type = UnderlyingType::Vec4; this->propagate (); } @@ -103,6 +109,7 @@ void CDynamicValue::update(const glm::ivec2& newValue) { this->m_float = static_cast (newValue.x); this->m_int = static_cast (newValue.x); this->m_bool = newValue.x != 0; + this->m_type = UnderlyingType::IVec2; this->propagate (); } @@ -117,6 +124,7 @@ void CDynamicValue::update(const glm::ivec3& newValue) { this->m_float = static_cast (newValue.x); this->m_int = static_cast (newValue.x); this->m_bool = newValue.x != 0; + this->m_type = UnderlyingType::IVec3; this->propagate (); } @@ -131,6 +139,7 @@ void CDynamicValue::update(const glm::ivec4& newValue) { this->m_float = static_cast (newValue.x); this->m_int = static_cast (newValue.x); this->m_bool = newValue.x != 0; + this->m_type = UnderlyingType::IVec4; this->propagate (); } @@ -202,4 +211,8 @@ const int& CDynamicValue::getInt () const { const bool& CDynamicValue::getBool () const { return this->m_bool; +} + +CDynamicValue::UnderlyingType CDynamicValue::getType () const { + return this->m_type; } \ No newline at end of file diff --git a/src/WallpaperEngine/Core/DynamicValues/CDynamicValue.h b/src/WallpaperEngine/Core/DynamicValues/CDynamicValue.h index 177cb97..7347da8 100644 --- a/src/WallpaperEngine/Core/DynamicValues/CDynamicValue.h +++ b/src/WallpaperEngine/Core/DynamicValues/CDynamicValue.h @@ -19,6 +19,19 @@ class CDynamicValue { friend class WallpaperEngine::Data::Parsers::UserSettingParser; friend class WallpaperEngine::Data::Builders::UserSettingBuilder; public: + enum UnderlyingType { + Unknown = -1, + IVec4 = 0, + IVec3 = 1, + IVec2 = 2, + Vec4 = 3, + Vec3 = 4, + Vec2 = 5, + Float = 6, + Int = 7, + Boolean = 8 + }; + virtual ~CDynamicValue (); [[nodiscard]] const glm::ivec4& getIVec4 () const; @@ -30,6 +43,7 @@ class CDynamicValue { [[nodiscard]] const float& getFloat () const; [[nodiscard]] const int& getInt () const; [[nodiscard]] const bool& getBool () const; + [[nodiscard]] UnderlyingType getType () const; /** * Connects the current instance to the given instance, updating it's values @@ -38,8 +52,6 @@ class CDynamicValue { * @param value */ void connectOutgoing (CDynamicValue* value) const; - - protected: void update (float newValue); void update (int newValue); void update (bool newValue); @@ -49,6 +61,8 @@ class CDynamicValue { void update (const glm::ivec2& newValue); void update (const glm::ivec3& newValue); void update (const glm::ivec4& newValue); + + protected: /** * Registers an incoming connection (another CDynamicValue affecting the current instance's value) * Useful mainly for destroying the connection on delete @@ -65,6 +79,7 @@ class CDynamicValue { private: mutable std::vector m_outgoingConnections = {}; mutable std::vector m_incomingConnections = {}; + UnderlyingType m_type = UnderlyingType::Unknown; // different values that we will be casted to automagically glm::ivec4 m_ivec4 = {}; glm::ivec3 m_ivec3 = {}; diff --git a/src/WallpaperEngine/Data/Parsers/ObjectParser.cpp b/src/WallpaperEngine/Data/Parsers/ObjectParser.cpp index 8e5ba6c..8cb84d7 100644 --- a/src/WallpaperEngine/Data/Parsers/ObjectParser.cpp +++ b/src/WallpaperEngine/Data/Parsers/ObjectParser.cpp @@ -78,7 +78,7 @@ ImageUniquePtr ObjectParser::parseImage ( const auto& properties = project.properties; const auto& effects = it.optional ("effects"); - return std::make_unique ( + auto result = std::make_unique ( std::move (base), ImageData { .origin = it.user ("origin", properties, glm::vec3 (0.0f)), @@ -86,7 +86,7 @@ ImageUniquePtr ObjectParser::parseImage ( .angles = it.user ("angles", properties, glm::vec3 (0.0)), .visible = it.user ("visible", properties, true), .alpha = it.user ("alpha", properties, 1.0f), - .color = it.user ("color", properties, glm::vec3 (1.0f)), + .color = it.user ("color", properties, glm::vec4 (1.0f)), .alignment = it.optional ("alignment", std::string ("center")), .size = it.optional ("size", glm::vec2 (0.0f)), .parallaxDepth = it.optional ("parallaxDepth", glm::vec2 (0.0f)), @@ -96,6 +96,15 @@ ImageUniquePtr ObjectParser::parseImage ( .effects = effects.has_value () ? parseEffects (*effects, project) : std::vector {}, } ); + + // color should be a vec4 for alpha, but it's read as vec3 + if (result->color->value->getType () == DynamicValue::UnderlyingType::Vec3) { + result->color->value->update (glm::vec4 (result->color->value->getVec3 (), 1.0f)); + } else if (result->color->value->getType () == DynamicValue::UnderlyingType::IVec3) { + result->color->value->update (glm::vec4 (result->color->value->getIVec3 (), 255)); + } + + return result; } std::vector ObjectParser::parseEffects (const JSON& it, Project& project) { @@ -163,10 +172,10 @@ TextureMap ObjectParser::parseTextureMap (const JSON& it) { textureIndex ++; if (cur.is_null ()) { - continue; + result.emplace (textureIndex, ""); + } else { + result.emplace (textureIndex, cur); } - - result.emplace (textureIndex, cur); } return result; diff --git a/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp b/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp index ac6c254..0dc01f3 100644 --- a/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp +++ b/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp @@ -425,7 +425,7 @@ void CPass::setupShaders () { // TODO: USED TO BUILD THE TEXTURES LATER // use the combos copied from the pass so it includes the texture format this->m_shader = new Render::Shaders::CShader ( - this->m_image.getContainer (), this->m_pass.shader, this->m_combos, + this->m_image.getContainer (), this->m_pass.shader, this->m_combos, this->m_override.combos, this->m_pass.textures, this->m_override.textures, this->m_override.constants ); @@ -608,7 +608,7 @@ void CPass::setupUniforms () { this->addUniform ("g_UserAlpha", image.alpha->value->getFloat ()); this->addUniform ("g_Alpha", image.alpha->value->getFloat ()); this->addUniform ("g_Color", image.color->value->getVec3 ()); - this->addUniform ("g_Color4", image.color->value->getVec4()); + this->addUniform ("g_Color4", image.color->value->getVec4 ()); // TODO: VALIDATE THAT G_COMPOSITECOLOR REALLY COMES FROM THIS ONE this->addUniform ("g_CompositeColor", image.color->value->getVec3 ()); // add some external variables diff --git a/src/WallpaperEngine/Render/Shaders/CShader.cpp b/src/WallpaperEngine/Render/Shaders/CShader.cpp index d2413ed..6b5d424 100644 --- a/src/WallpaperEngine/Render/Shaders/CShader.cpp +++ b/src/WallpaperEngine/Render/Shaders/CShader.cpp @@ -19,20 +19,21 @@ using namespace WallpaperEngine::Assets; namespace WallpaperEngine::Render::Shaders { CShader::CShader ( const CContainer& container, std::string filename, - const ComboMap& combos, const TextureMap& textures, - const TextureMap& overrideTextures, + const ComboMap& combos, const ComboMap& overrideCombos, + const TextureMap& textures, const TextureMap& overrideTextures, const ShaderConstantMap& constants ) : m_file (std::move (filename)), m_combos (combos), + m_overrideCombos (overrideCombos), m_passTextures (textures), m_overrideTextures (overrideTextures), m_vertex ( CGLSLContext::UnitType_Vertex, filename, container.readVertexShader (filename), - container, constants, textures, overrideTextures, combos), + container, constants, textures, overrideTextures, combos, overrideCombos), m_fragment ( CGLSLContext::UnitType_Fragment, filename, container.readFragmentShader (filename), - container, constants, textures, overrideTextures, combos) { + container, constants, textures, overrideTextures, combos, overrideCombos) { // link shaders between them this->m_vertex.linkToUnit (&this->m_fragment); this->m_fragment.linkToUnit (&this->m_vertex); diff --git a/src/WallpaperEngine/Render/Shaders/CShader.h b/src/WallpaperEngine/Render/Shaders/CShader.h index 0638f9b..5bbe810 100644 --- a/src/WallpaperEngine/Render/Shaders/CShader.h +++ b/src/WallpaperEngine/Render/Shaders/CShader.h @@ -46,8 +46,8 @@ class CShader { */ CShader ( const CContainer& container, std::string filename, - const ComboMap& combos, const TextureMap& textures, - const TextureMap& overrideTextures, + const ComboMap& combos, const ComboMap& overrideCombos, + const TextureMap& textures, const TextureMap& overrideTextures, const ShaderConstantMap& constants); /** * @return The vertex's shader coude for OpenGL to use @@ -98,6 +98,10 @@ class CShader { * The combos the shader should be generated with */ const ComboMap& m_combos; + /** + * The overriden combos + */ + const ComboMap& m_overrideCombos; /** * The list of textures the pass knows about */ diff --git a/src/WallpaperEngine/Render/Shaders/CShaderUnit.cpp b/src/WallpaperEngine/Render/Shaders/CShaderUnit.cpp index 4c00202..915510f 100644 --- a/src/WallpaperEngine/Render/Shaders/CShaderUnit.cpp +++ b/src/WallpaperEngine/Render/Shaders/CShaderUnit.cpp @@ -58,7 +58,7 @@ using namespace WallpaperEngine::Render::Shaders; CShaderUnit::CShaderUnit ( CGLSLContext::UnitType type, std::string file, std::string content, const CContainer& container, const ShaderConstantMap& constants, const TextureMap& passTextures, const TextureMap& overrideTextures, - const ComboMap& combos + const ComboMap& combos, const ComboMap& overrideCombos ) : m_type (type), m_link (nullptr), @@ -69,6 +69,7 @@ CShaderUnit::CShaderUnit ( m_passTextures (passTextures), m_overrideTextures (overrideTextures), m_combos (combos), + m_overrideCombos (overrideCombos), m_discoveredCombos (), m_usedCombos () { // pre-process the shader so the units are clear @@ -335,14 +336,14 @@ void CShaderUnit::parseComboConfiguration (const std::string& content, int defau // check the combos const auto entry = this->m_combos.find (combo->get ()); + const auto entryOverride = this->m_overrideCombos.find (combo->get ()); // add the combo to the found list this->m_usedCombos.emplace (*combo, true); // if the combo was not found in the predefined values this means that the default value in the JSON data can be // used so only define the ones that are not already defined - if (entry == this->m_combos.end ()) { - + if (entry == this->m_combos.end () && entryOverride == this->m_overrideCombos.end ()) { // if no combo is defined just load the default settings if (defvalue == data.end ()) { // TODO: PROPERLY SUPPORT EMPTY COMBOS @@ -406,6 +407,8 @@ void CShaderUnit::parseParameterConfiguration ( // samplers can have special requirements, check what sampler we're working with and create definitions // if needed const auto textureName = data.find ("default"); + // TODO: CREATE TEXTURE WITH THE GIVEN COLOR + const auto paintDefaultColor = data.find ("paintdefaultcolor"); // extract the texture number from the name const char value = name.at (std::string ("g_Texture").length ()); const auto requireany = data.find ("requireany"); @@ -414,6 +417,10 @@ void CShaderUnit::parseParameterConfiguration ( // TODO: BETTER CONVERSION HERE size_t index = value - '0'; + if (combo != data.end () && paintDefaultColor != data.end () && combo->get () == "MASK" && this->m_file.find ("water")) { + sLog.debug("mask defaultcolor!"); + } + if (combo != data.end ()) { // TODO: CLEANUP HOW THIS IS DETERMINED FIRST // if the texture exists (and is not null), add to the combo @@ -432,9 +439,10 @@ void CShaderUnit::parseParameterConfiguration ( for (const auto& item : require->items ()) { const std::string& macro = item.key (); const auto it = this->m_combos.find (macro); + const auto itOverride = this->m_overrideCombos.find (macro); // if any of the values matched, this option is required - if (it == this->m_combos.end () || it->second != item.value ()) { + if (it == this->m_combos.end () || itOverride != this->m_overrideCombos.end () || it->second != item.value ()) { isRequired = true; break; } @@ -446,9 +454,10 @@ void CShaderUnit::parseParameterConfiguration ( for (const auto& item : require->items ()) { const std::string& macro = item.key (); const auto it = this->m_combos.find (macro); + const auto itOverride = this->m_overrideCombos.find (macro); // these can not exist and that'd be fine, we just care about the values - if (it != this->m_combos.end () && it->second == item.value ()) { + if ((it != this->m_combos.end () || itOverride != this->m_overrideCombos.end ()) && it->second == item.value ()) { isRequired = false; break; } @@ -463,9 +472,10 @@ void CShaderUnit::parseParameterConfiguration ( // is the combo registered already? // if not, add it with the default value const auto combo_it = this->m_combos.find (*combo); + const auto overridencombo_it = this->m_overrideCombos.find (*combo); // there's already a combo providing this value, so it doesn't need to be added - if (combo_it != this->m_combos.end ()) { + if (combo_it != this->m_combos.end () || overridencombo_it != this->m_overrideCombos.end ()) { isRequired = false; // otherwise a default value must be used } else if (defvalue->is_string ()) { @@ -535,27 +545,42 @@ const std::string& CShaderUnit::compile () { std::map addedCombos; + for (const auto& combo : this->m_overrideCombos) { + if (addedCombos.find (combo.first) == addedCombos.end ()) { + this->m_final += DEFINE_COMBO (combo.first, combo.second); + } + + addedCombos.emplace (combo.first, true); + } // now add all the combos to the source for (const auto& combo : this->m_combos) { if (addedCombos.find (combo.first) == addedCombos.end ()) { this->m_final += DEFINE_COMBO (combo.first, combo.second); } + + addedCombos.emplace (combo.first, true); } for (const auto& combo : this->m_discoveredCombos) { if (addedCombos.find (combo.first) == addedCombos.end ()) { this->m_final += DEFINE_COMBO (combo.first, combo.second); } + + addedCombos.emplace (combo.first, true); } if (this->m_link != nullptr) { for (const auto& combo : this->m_link->getCombos ()) { if (addedCombos.find (combo.first) == addedCombos.end ()) { this->m_final += DEFINE_COMBO (combo.first, combo.second); } + + addedCombos.emplace (combo.first, true); } for (const auto& combo : this->m_link->getDiscoveredCombos ()) { if (addedCombos.find (combo.first) == addedCombos.end ()) { this->m_final += DEFINE_COMBO (combo.first, combo.second); } + + addedCombos.emplace (combo.first, true); } } diff --git a/src/WallpaperEngine/Render/Shaders/CShaderUnit.h b/src/WallpaperEngine/Render/Shaders/CShaderUnit.h index df365a3..19dc0c5 100644 --- a/src/WallpaperEngine/Render/Shaders/CShaderUnit.h +++ b/src/WallpaperEngine/Render/Shaders/CShaderUnit.h @@ -27,7 +27,7 @@ class CShaderUnit { CShaderUnit ( CGLSLContext::UnitType type, std::string file, std::string content, const CContainer& container, const ShaderConstantMap& constants, const TextureMap& passTextures, - const TextureMap& overrideTextures, const ComboMap& combos); + const TextureMap& overrideTextures, const ComboMap& combos, const ComboMap& overrideCombos); ~CShaderUnit () = default; /** @@ -132,6 +132,10 @@ class CShaderUnit { * Pre-defined values for the combos */ const ComboMap& m_combos; + /** + * Pre-defined overriden values for the combos + */ + const ComboMap& m_overrideCombos; /** * The combos discovered in the pre-processing step that were not in the combos list */