Skip to content

Commit

Permalink
Flip input to blur textures once, but not in subsequent passes.
Browse files Browse the repository at this point in the history
  • Loading branch information
kblaschke committed Nov 14, 2023
1 parent 9a73a88 commit f9bc83d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
21 changes: 10 additions & 11 deletions src/libprojectM/MilkdropPreset/BlurTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(m_blurLevel) * 2;
auto const blur1EdgeDarken = static_cast<float>(*perFrameContext.blur1_edge_darken);
Expand Down Expand Up @@ -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<float>((pass == 0) ? sourceTexture->Width() : m_blurTextures[pass - 1]->Width());
float srcHeight = static_cast<float>((pass == 0) ? sourceTexture->Height() : m_blurTextures[pass - 1]->Height());
float srcWidth = static_cast<float>((pass == 0) ? sourceTexture.Width() : m_blurTextures[pass - 1]->Width());
float srcHeight = static_cast<float>((pass == 0) ? sourceTexture.Height() : m_blurTextures[pass - 1]->Height());

float scaleNow = scale[pass / 2];
float biasNow = bias[pass / 2];
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/libprojectM/MilkdropPreset/BlurTexture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion src/libprojectM/MilkdropPreset/MilkdropPreset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit f9bc83d

Please sign in to comment.