From ae66deffb6fdcfc9f92bfe9179c36b3af1c06739 Mon Sep 17 00:00:00 2001 From: Almamu Date: Fri, 11 Apr 2025 01:40:13 +0200 Subject: [PATCH] chore: some readability improvements on code --- .../Application/CWallpaperApplication.cpp | 16 +- .../Application/CWallpaperApplication.h | 12 + src/WallpaperEngine/Audio/CAudioStream.cpp | 1 - .../Render/Objects/Effects/CPass.cpp | 245 ++++++++++-------- .../Render/Objects/Effects/CPass.h | 9 + .../Render/Shaders/CCompiler.cpp | 10 +- src/WallpaperEngine/Render/Wallpapers/CWeb.h | 1 - 7 files changed, 174 insertions(+), 120 deletions(-) diff --git a/src/WallpaperEngine/Application/CWallpaperApplication.cpp b/src/WallpaperEngine/Application/CWallpaperApplication.cpp index a666445..0a1e8fe 100644 --- a/src/WallpaperEngine/Application/CWallpaperApplication.cpp +++ b/src/WallpaperEngine/Application/CWallpaperApplication.cpp @@ -290,7 +290,7 @@ void CWallpaperApplication::takeScreenshot (const std::filesystem::path& filenam delete [] bitmap; } -void CWallpaperApplication::show () { +void CWallpaperApplication::setupOutput () { const char* XDG_SESSION_TYPE = getenv ("XDG_SESSION_TYPE"); if (!XDG_SESSION_TYPE) { @@ -381,7 +381,11 @@ void CWallpaperApplication::show () { } else { this->m_audioRecorder = new WallpaperEngine::Audio::Drivers::Recorders::CPlaybackRecorder (); } + // initialize render context + m_renderContext = new WallpaperEngine::Render::CRenderContext (*m_videoDriver, *m_inputContext, *this); +} +void CWallpaperApplication::setupAudio () { // audio playing detector WallpaperEngine::Audio::Drivers::Detectors::CPulseAudioPlayingDetector audioDetector (this->m_context, *this->m_fullScreenDetector); @@ -390,9 +394,9 @@ void CWallpaperApplication::show () { new WallpaperEngine::Audio::Drivers::CSDLAudioDriver (this->m_context, audioDetector, *this->m_audioRecorder); // initialize audio context m_audioContext = new WallpaperEngine::Audio::CAudioContext (*m_audioDriver); - // initialize render context - m_renderContext = new WallpaperEngine::Render::CRenderContext (*m_videoDriver, *m_inputContext, *this); +} +void CWallpaperApplication::prepareOutputs () { // create a new background for each screen // set all the specific wallpapers required @@ -402,6 +406,12 @@ void CWallpaperApplication::show () { info->getWallpaper (), *m_renderContext, *m_audioContext, *m_browserContext, this->m_context.settings.general.screenScalings [background])); } +} + +void CWallpaperApplication::show () { + this->setupOutput (); + this->setupAudio (); + this->prepareOutputs (); static time_t seconds; static struct tm* timeinfo; diff --git a/src/WallpaperEngine/Application/CWallpaperApplication.h b/src/WallpaperEngine/Application/CWallpaperApplication.h index 913879b..a0fd074 100644 --- a/src/WallpaperEngine/Application/CWallpaperApplication.h +++ b/src/WallpaperEngine/Application/CWallpaperApplication.h @@ -105,6 +105,18 @@ class CWallpaperApplication { * Prepares CEF browser to be used */ void setupBrowser (); + /** + * Prepares desktop environment-related things (like render, window, fullscreen detector, etc) + */ + void setupOutput (); + /** + * Prepares all audio-related things (like detector, output, etc) + */ + void setupAudio (); + /** + * Prepares the render-context of all the backgrounds so they can be displayed on the screen + */ + void prepareOutputs (); /** * Takes an screenshot of the background and saves it to the specified path * diff --git a/src/WallpaperEngine/Audio/CAudioStream.cpp b/src/WallpaperEngine/Audio/CAudioStream.cpp index c9f7f95..a6d4535 100644 --- a/src/WallpaperEngine/Audio/CAudioStream.cpp +++ b/src/WallpaperEngine/Audio/CAudioStream.cpp @@ -287,7 +287,6 @@ void CAudioStream::dequeuePacket (AVPacket* output) { SDL_LockMutex (this->m_queue->mutex); while (this->m_audioContext.getApplicationContext ().state.general.keepRunning) { - #if FF_API_FIFO_OLD_API int ret = -1; diff --git a/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp b/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp index 20a84df..effc3b9 100644 --- a/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp +++ b/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp @@ -60,7 +60,7 @@ const ITexture* CPass::resolveTexture (const ITexture* expected, int index, cons return fbo; } -void CPass::render () { +void CPass::setupRenderFramebuffer () { // set the framebuffer we're drawing to glBindFramebuffer (GL_FRAMEBUFFER, this->m_drawTo->getFramebuffer ()); @@ -99,7 +99,9 @@ void CPass::render () { } else { glDepthMask (true); } +} +void CPass::setupRenderTexture () { // use the shader we have registered glUseProgram (this->m_programID); @@ -148,6 +150,44 @@ void CPass::render () { } } + // used in animations when one of the frames is vertical instead of horizontal + // rotation with translation = origin and end of the image to display + if (this->g_Texture0Rotation != -1) + glUniform4f (this->g_Texture0Rotation, rotation.x, rotation.y, rotation.z, rotation.w); + // this actually picks the origin point of the image from the atlast + if (this->g_Texture0Translation != -1) + glUniform2f (this->g_Texture0Translation, translation.x, translation.y); +} + +void CPass::setupRenderReferenceUniforms () { + // add reference uniforms + for (const auto& [name, value] : this->m_referenceUniforms) { + switch (value->type) { + case Double: glUniform1d (value->id, *static_cast (*value->value)); break; + case Float: glUniform1f (value->id, *static_cast (*value->value)); break; + case Integer: glUniform1i (value->id, *static_cast (*value->value)); break; + case Vector4: + glUniform4fv (value->id, 1, glm::value_ptr (*static_cast (*value->value))); + break; + case Vector3: + glUniform3fv (value->id, 1, glm::value_ptr (*static_cast (*value->value))); + break; + case Vector2: + glUniform2fv (value->id, 1, glm::value_ptr (*static_cast (*value->value))); + break; + case Matrix4: + glUniformMatrix4fv (value->id, 1, GL_FALSE, + glm::value_ptr (*static_cast (*value->value))); + break; + case Matrix3: + glUniformMatrix3fv (value->id, 1, GL_FALSE, + glm::value_ptr (*static_cast (*value->value))); + break; + } + } +} + +void CPass::setupRenderUniforms () { // add uniforms for (const auto& [name, value] : this->m_uniforms) { switch (value->type) { @@ -174,40 +214,9 @@ void CPass::render () { break; } } - // add reference uniforms - for (const auto& [name, value] : this->m_referenceUniforms) { - switch (value->type) { - case Double: glUniform1d (value->id, *static_cast (*value->value)); break; - case Float: glUniform1f (value->id, *static_cast (*value->value)); break; - case Integer: glUniform1i (value->id, *static_cast (*value->value)); break; - case Vector4: - glUniform4fv (value->id, 1, glm::value_ptr (*static_cast (*value->value))); - break; - case Vector3: - glUniform3fv (value->id, 1, glm::value_ptr (*static_cast (*value->value))); - break; - case Vector2: - glUniform2fv (value->id, 1, glm::value_ptr (*static_cast (*value->value))); - break; - case Matrix4: - glUniformMatrix4fv (value->id, 1, GL_FALSE, - glm::value_ptr (*static_cast (*value->value))); - break; - case Matrix3: - glUniformMatrix3fv (value->id, 1, GL_FALSE, - glm::value_ptr (*static_cast (*value->value))); - break; - } - } - - // used in animations when one of the frames is vertical instead of horizontal - // rotation with translation = origin and end of the image to display - if (this->g_Texture0Rotation != -1) - glUniform4f (this->g_Texture0Rotation, rotation.x, rotation.y, rotation.z, rotation.w); - // this actually picks the origin point of the image from the atlast - if (this->g_Texture0Translation != -1) - glUniform2f (this->g_Texture0Translation, translation.x, translation.y); +} +void CPass::setupRenderAttributes () { for (const auto& cur : this->m_attribs) { glEnableVertexAttribArray (cur->id); glBindBuffer (GL_ARRAY_BUFFER, *cur->value); @@ -220,11 +229,15 @@ void CPass::render () { .c_str ()); #endif /* DEBUG */ } +} +void CPass::renderGeometry () const { // start actual rendering now glBindBuffer (GL_ARRAY_BUFFER, this->a_Position); glDrawArrays (GL_TRIANGLES, 0, 6); +} +void CPass::cleanupRenderSetup () { // disable vertex attribs array and textures for (const auto& cur : this->m_attribs) glDisableVertexAttribArray (cur->id); @@ -242,6 +255,16 @@ void CPass::render () { } } +void CPass::render () { + this->setupRenderFramebuffer (); + this->setupRenderTexture (); + this->setupRenderUniforms (); + this->setupRenderReferenceUniforms (); + this->setupRenderAttributes (); + this->renderGeometry (); + this->cleanupRenderSetup (); +} + const CMaterial* CPass::getMaterial () const { return this->m_material; } @@ -405,7 +428,7 @@ void CPass::setupAttributes () { this->addAttribute ("a_Position", GL_FLOAT, 3, &this->a_Position); } -void CPass::setupUniforms () { +void CPass::setupTextureUniforms () { // resolve the main texture const ITexture* texture = this->resolveTexture (this->m_material->getImage ()->getTexture (), 0); // register all the texture uniforms with correct values @@ -420,96 +443,98 @@ void CPass::setupUniforms () { this->addUniform ("g_Texture0Resolution", texture->getResolution ()); // do the real, final texture setup for the whole process - { - auto cur = this->m_textures.begin (); - const auto end = this->m_textures.end (); - auto fragCur = this->m_fragShader->getTextures ().begin (); - const auto fragEnd = this->m_fragShader->getTextures ().end (); - auto vertCur = this->m_vertShader->getTextures ().begin (); - const auto vertEnd = this->m_vertShader->getTextures ().end (); - auto bindCur = this->m_material->getMaterial ()->getTextureBinds ().begin (); - const auto bindEnd = this->m_material->getMaterial ()->getTextureBinds ().end (); + auto cur = this->m_textures.begin (); + const auto end = this->m_textures.end (); + auto fragCur = this->m_fragShader->getTextures ().begin (); + const auto fragEnd = this->m_fragShader->getTextures ().end (); + auto vertCur = this->m_vertShader->getTextures ().begin (); + const auto vertEnd = this->m_vertShader->getTextures ().end (); + auto bindCur = this->m_material->getMaterial ()->getTextureBinds ().begin (); + const auto bindEnd = this->m_material->getMaterial ()->getTextureBinds ().end (); - int index = 1; + int index = 1; - // technically m_textures should have the right amount of textures - // but better be safe than sorry - while (bindCur != bindEnd || cur != end || fragCur != fragEnd || vertCur != vertEnd) { - if (bindCur != bindEnd) { - this->m_finalTextures [bindCur->first] = nullptr; - ++bindCur; + // technically m_textures should have the right amount of textures + // but better be safe than sorry + while (bindCur != bindEnd || cur != end || fragCur != fragEnd || vertCur != vertEnd) { + if (bindCur != bindEnd) { + this->m_finalTextures [bindCur->first] = nullptr; + ++bindCur; + } + + if (cur != end) { + if ((*cur) != nullptr) + this->m_finalTextures [index] = *cur; + + index++; + ++cur; + } + + if (fragCur != fragEnd) { + std::string textureName = fragCur->second; + + try { + // resolve the texture first + const ITexture* textureRef; + + if (textureName.find ("_rt_") == 0 || textureName.find ("_alias_") == 0) { + textureRef = this->getMaterial ()->getEffect ()->findFBO (textureName); + + if (textureRef == nullptr) + textureRef = this->getMaterial ()->getImage ()->getScene ()->findFBO (textureName); + } else + textureRef = this->getContext ().resolveTexture (textureName); + + // ensure there's no texture in that slot already, shader textures are defaults in case nothing is + // there + if (this->m_finalTextures.find (fragCur->first) == this->m_finalTextures.end ()) + this->m_finalTextures [fragCur->first] = textureRef; + } catch (std::runtime_error& ex) { + sLog.error ("Cannot resolve texture ", textureName, " for fragment shader ", ex.what ()); } - if (cur != end) { - if ((*cur) != nullptr) - this->m_finalTextures [index] = *cur; + ++fragCur; + } - index++; - ++cur; + if (vertCur != vertEnd) { + std::string textureName = vertCur->second; + + try { + // resolve the texture first + const ITexture* textureRef; + + if (textureName.find ("_rt_") == 0) { + textureRef = this->getMaterial ()->getEffect ()->findFBO (textureName); + + if (textureRef == nullptr) + textureRef = this->getMaterial ()->getImage ()->getScene ()->findFBO (textureName); + } else + textureRef = this->getContext ().resolveTexture (textureName); + + // ensure there's no texture in that slot already, shader textures are defaults in case nothing is + // there + if (this->m_finalTextures.find (vertCur->first) == this->m_finalTextures.end ()) + this->m_finalTextures [vertCur->first] = textureRef; + } catch (std::runtime_error& ex) { + sLog.error ("Cannot resolve texture ", textureName, " for vertex shader ", ex.what ()); } - if (fragCur != fragEnd) { - std::string textureName = fragCur->second; - - try { - // resolve the texture first - const ITexture* textureRef; - - if (textureName.find ("_rt_") == 0 || textureName.find ("_alias_") == 0) { - textureRef = this->getMaterial ()->getEffect ()->findFBO (textureName); - - if (textureRef == nullptr) - textureRef = this->getMaterial ()->getImage ()->getScene ()->findFBO (textureName); - } else - textureRef = this->getContext ().resolveTexture (textureName); - - // ensure there's no texture in that slot already, shader textures are defaults in case nothing is - // there - if (this->m_finalTextures.find (fragCur->first) == this->m_finalTextures.end ()) - this->m_finalTextures [fragCur->first] = textureRef; - } catch (std::runtime_error& ex) { - sLog.error ("Cannot resolve texture ", textureName, " for fragment shader ", ex.what ()); - } - - ++fragCur; - } - - if (vertCur != vertEnd) { - std::string textureName = vertCur->second; - - try { - // resolve the texture first - const ITexture* textureRef; - - if (textureName.find ("_rt_") == 0) { - textureRef = this->getMaterial ()->getEffect ()->findFBO (textureName); - - if (textureRef == nullptr) - textureRef = this->getMaterial ()->getImage ()->getScene ()->findFBO (textureName); - } else - textureRef = this->getContext ().resolveTexture (textureName); - - // ensure there's no texture in that slot already, shader textures are defaults in case nothing is - // there - if (this->m_finalTextures.find (vertCur->first) == this->m_finalTextures.end ()) - this->m_finalTextures [vertCur->first] = textureRef; - } catch (std::runtime_error& ex) { - sLog.error ("Cannot resolve texture ", textureName, " for vertex shader ", ex.what ()); - } - - ++vertCur; - } + ++vertCur; } } - for (const auto& [index, expectedTexture] : this->m_finalTextures) { + for (const auto& [textureIndex, expectedTexture] : this->m_finalTextures) { std::ostringstream namestream; - namestream << "g_Texture" << index << "Resolution"; + namestream << "g_Texture" << textureIndex << "Resolution"; - texture = this->resolveTexture (expectedTexture, index, texture); + texture = this->resolveTexture (expectedTexture, textureIndex, texture); this->addUniform (namestream.str (), texture->getResolution ()); } +} + +void CPass::setupUniforms () { + this->setupTextureUniforms (); const auto projection = this->getMaterial ()->getImage ()->getScene ()->getScene ()->getOrthogonalProjection (); diff --git a/src/WallpaperEngine/Render/Objects/Effects/CPass.h b/src/WallpaperEngine/Render/Objects/Effects/CPass.h index 76783fc..a2c6008 100644 --- a/src/WallpaperEngine/Render/Objects/Effects/CPass.h +++ b/src/WallpaperEngine/Render/Objects/Effects/CPass.h @@ -100,6 +100,7 @@ class CPass final : public Helpers::CContextAware { void setupShaders (); void setupShaderVariables (); void setupUniforms (); + void setupTextureUniforms (); void setupAttributes (); void addAttribute (const std::string& name, GLint type, GLint elements, const GLuint* value); void addUniform (CShaderVariable* value); @@ -132,6 +133,14 @@ class CPass final : public Helpers::CContextAware { template void addUniform (const std::string& name, UniformType type, T* value, int count = 1); template void addUniform (const std::string& name, UniformType type, T** value); + void setupRenderFramebuffer (); + void setupRenderTexture (); + void setupRenderUniforms (); + void setupRenderReferenceUniforms (); + void setupRenderAttributes (); + void renderGeometry () const; + void cleanupRenderSetup (); + const ITexture* resolveTexture (const ITexture* expected, int index, const ITexture* previous = nullptr); CMaterial* m_material; diff --git a/src/WallpaperEngine/Render/Shaders/CCompiler.cpp b/src/WallpaperEngine/Render/Shaders/CCompiler.cpp index 08bc9f0..675e6fb 100644 --- a/src/WallpaperEngine/Render/Shaders/CCompiler.cpp +++ b/src/WallpaperEngine/Render/Shaders/CCompiler.cpp @@ -260,7 +260,7 @@ void CCompiler::parseComboConfiguration (const std::string& content, int default const auto entry = this->m_combos->find (combo->get ()); // add the combo to the found list - this->m_foundCombos->insert (std::make_pair (*combo, true)); + this->m_foundCombos->insert (std::make_pair (*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 @@ -271,11 +271,11 @@ void CCompiler::parseComboConfiguration (const std::string& content, int default // if no combo is defined just load the default settings if (defvalue == data.end ()) { // TODO: PROPERLY SUPPORT EMPTY COMBOS - this->m_combos->insert (std::make_pair (*combo, (int) defaultValue)); + this->m_combos->insert (std::make_pair (*combo, (int) defaultValue)); } else if (defvalue->is_number_float ()) { sLog.exception ("float combos are not supported in shader ", this->m_file, ". ", *combo); } else if (defvalue->is_number_integer ()) { - this->m_combos->insert (std::make_pair (*combo, defvalue->get ())); + this->m_combos->insert (std::make_pair (*combo, defvalue->get ())); } else if (defvalue->is_string ()) { sLog.exception ("string combos are not supported in shader ", this->m_file, ". ", *combo); } else { @@ -352,11 +352,11 @@ void CCompiler::parseParameterConfiguration (const std::string& type, const std: if (this->m_passTextures.size () > index && (!this->m_passTextures.at (index).empty () || textureName != data.end ())) { // add the new combo to the list - this->m_combos->insert (std::make_pair (*combo, 1)); + this->m_combos->insert (std::make_pair (*combo, 1)); // textures linked to combos need to be tracked too if (this->m_foundCombos->find (*combo) == this->m_foundCombos->end ()) - this->m_foundCombos->insert (std::make_pair (*combo, true)); + this->m_foundCombos->insert (std::make_pair (*combo, true)); } } diff --git a/src/WallpaperEngine/Render/Wallpapers/CWeb.h b/src/WallpaperEngine/Render/Wallpapers/CWeb.h index 945ca1d..78a5d85 100644 --- a/src/WallpaperEngine/Render/Wallpapers/CWeb.h +++ b/src/WallpaperEngine/Render/Wallpapers/CWeb.h @@ -45,7 +45,6 @@ namespace WallpaperEngine::Render::Wallpapers { static const std::string Type; private: - WallpaperEngine::WebBrowser::CWebBrowserContext& m_browserContext; CefRefPtr m_browser; CefRefPtr m_client;