From f9bc83d636e35ae23ddf085b3047e271d838657e Mon Sep 17 00:00:00 2001 From: Kai Blaschke Date: Tue, 14 Nov 2023 16:11:13 +0100 Subject: [PATCH] Flip input to blur textures once, but not in subsequent passes. --- .../MilkdropPreset/BlurTexture.cpp | 21 +++++++++---------- .../MilkdropPreset/BlurTexture.hpp | 4 ++-- .../MilkdropPreset/MilkdropPreset.cpp | 6 +++++- .../Shaders/BlurVertexShaderGlsl330.vert | 8 +++++++ 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/libprojectM/MilkdropPreset/BlurTexture.cpp b/src/libprojectM/MilkdropPreset/BlurTexture.cpp index b66a55fe78..5f8bd2e513 100644 --- a/src/libprojectM/MilkdropPreset/BlurTexture.cpp +++ b/src/libprojectM/MilkdropPreset/BlurTexture.cpp @@ -91,23 +91,20 @@ auto BlurTexture::GetDescriptorsForBlurLevel(BlurTexture::BlurLevel blurLevel) c return descriptors; } -void BlurTexture::Update(const PresetState& presetState, const PerFrameContext& perFrameContext) +void BlurTexture::Update(const Texture& sourceTexture, const PerFrameContext& perFrameContext) { if (m_blurLevel == BlurLevel::None) { return; } - auto sourceTexture = presetState.mainTexture.lock(); - - if (!sourceTexture || - sourceTexture->Width() == 0 || - sourceTexture->Height() == 0) + if (sourceTexture.Width() == 0 || + sourceTexture.Height() == 0) { return; } - AllocateTextures(*sourceTexture); + AllocateTextures(sourceTexture); unsigned int const passes = static_cast(m_blurLevel) * 2; auto const blur1EdgeDarken = static_cast(*perFrameContext.blur1_edge_darken); @@ -170,16 +167,18 @@ void BlurTexture::Update(const PresetState& presetState, const PerFrameContext& // hook up correct source texture - assume there is only one, at stage 0 if (pass == 0) { - sourceTexture->Bind(0); + sourceTexture.Bind(0); + blurShader->SetUniformInt("flipVertical", 1); } else { m_blurTextures[pass - 1]->Bind(0); + blurShader->SetUniformInt("flipVertical", 0); } m_blurSampler->Bind(0); - float srcWidth = static_cast((pass == 0) ? sourceTexture->Width() : m_blurTextures[pass - 1]->Width()); - float srcHeight = static_cast((pass == 0) ? sourceTexture->Height() : m_blurTextures[pass - 1]->Height()); + float srcWidth = static_cast((pass == 0) ? sourceTexture.Width() : m_blurTextures[pass - 1]->Width()); + float srcHeight = static_cast((pass == 0) ? sourceTexture.Height() : m_blurTextures[pass - 1]->Height()); float scaleNow = scale[pass / 2]; float biasNow = bias[pass / 2]; @@ -254,7 +253,7 @@ void BlurTexture::Update(const PresetState& presetState, const PerFrameContext& // Bind previous framebuffer and reset viewport size glBindFramebuffer(GL_READ_FRAMEBUFFER, origReadFramebuffer); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, origDrawFramebuffer); - glViewport(0, 0, sourceTexture->Width(), sourceTexture->Height()); + glViewport(0, 0, sourceTexture.Width(), sourceTexture.Height()); Shader::Unbind(); } diff --git a/src/libprojectM/MilkdropPreset/BlurTexture.hpp b/src/libprojectM/MilkdropPreset/BlurTexture.hpp index 060ee8f0e3..8ac566b7e4 100644 --- a/src/libprojectM/MilkdropPreset/BlurTexture.hpp +++ b/src/libprojectM/MilkdropPreset/BlurTexture.hpp @@ -62,10 +62,10 @@ class BlurTexture /** * @brief Renders the required blur passes on the given texture. - * @param presetState The preset state with initial values and the main texture. + * @param sourceTexture The texture to create the blur levels from. * @param perFrameContext The per-frame variables. */ - void Update(const PresetState& presetState, const PerFrameContext& perFrameContext); + void Update(const Texture& sourceTexture, const PerFrameContext& perFrameContext); /** * @brief Binds the user-readable blur textures to the texture slots starting with the given index. diff --git a/src/libprojectM/MilkdropPreset/MilkdropPreset.cpp b/src/libprojectM/MilkdropPreset/MilkdropPreset.cpp index 8819e3617b..6c730271b7 100755 --- a/src/libprojectM/MilkdropPreset/MilkdropPreset.cpp +++ b/src/libprojectM/MilkdropPreset/MilkdropPreset.cpp @@ -116,7 +116,11 @@ void MilkdropPreset::RenderFrame(const libprojectM::Audio::FrameAudioData& audio m_framebuffer.RemoveColorAttachment(m_currentFrameBuffer, 1); // Update blur textures - m_state.blurTexture.Update(m_state, m_perFrameContext); + { + const auto warpedImage = m_framebuffer.GetColorAttachmentTexture(m_currentFrameBuffer, 0); + assert(warpedImage.get()); + m_state.blurTexture.Update(*warpedImage, m_perFrameContext); + } // Draw audio-data-related stuff for (auto& shape : m_customShapes) diff --git a/src/libprojectM/MilkdropPreset/Shaders/BlurVertexShaderGlsl330.vert b/src/libprojectM/MilkdropPreset/Shaders/BlurVertexShaderGlsl330.vert index 7b3fdf54ca..38bdeacc20 100644 --- a/src/libprojectM/MilkdropPreset/Shaders/BlurVertexShaderGlsl330.vert +++ b/src/libprojectM/MilkdropPreset/Shaders/BlurVertexShaderGlsl330.vert @@ -3,9 +3,17 @@ precision mediump float; layout(location = 0) in vec2 vertex_position; layout(location = 1) in vec2 vertex_texture; +uniform int flipVertical; + out vec2 fragment_texture; void main(){ gl_Position = vec4(vertex_position, 0.0, 1.0); fragment_texture = vertex_texture; + + // Vertically flip main texture, but not the already blurred ones. + if (flipVertical == 1) + { + fragment_texture.y = 1.0 - fragment_texture.y; + } }