Flip input to blur textures once, but not in subsequent passes.

This commit is contained in:
Kai Blaschke
2023-11-14 16:11:13 +01:00
parent 9a73a883df
commit f9bc83d636
4 changed files with 25 additions and 14 deletions

View File

@ -91,23 +91,20 @@ auto BlurTexture::GetDescriptorsForBlurLevel(BlurTexture::BlurLevel blurLevel) c
return descriptors; 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) if (m_blurLevel == BlurLevel::None)
{ {
return; return;
} }
auto sourceTexture = presetState.mainTexture.lock(); if (sourceTexture.Width() == 0 ||
sourceTexture.Height() == 0)
if (!sourceTexture ||
sourceTexture->Width() == 0 ||
sourceTexture->Height() == 0)
{ {
return; return;
} }
AllocateTextures(*sourceTexture); AllocateTextures(sourceTexture);
unsigned int const passes = static_cast<int>(m_blurLevel) * 2; unsigned int const passes = static_cast<int>(m_blurLevel) * 2;
auto const blur1EdgeDarken = static_cast<float>(*perFrameContext.blur1_edge_darken); auto const blur1EdgeDarken = static_cast<float>(*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 // hook up correct source texture - assume there is only one, at stage 0
if (pass == 0) if (pass == 0)
{ {
sourceTexture->Bind(0); sourceTexture.Bind(0);
blurShader->SetUniformInt("flipVertical", 1);
} }
else else
{ {
m_blurTextures[pass - 1]->Bind(0); m_blurTextures[pass - 1]->Bind(0);
blurShader->SetUniformInt("flipVertical", 0);
} }
m_blurSampler->Bind(0); m_blurSampler->Bind(0);
float srcWidth = static_cast<float>((pass == 0) ? sourceTexture->Width() : m_blurTextures[pass - 1]->Width()); 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 srcHeight = static_cast<float>((pass == 0) ? sourceTexture.Height() : m_blurTextures[pass - 1]->Height());
float scaleNow = scale[pass / 2]; float scaleNow = scale[pass / 2];
float biasNow = bias[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 // Bind previous framebuffer and reset viewport size
glBindFramebuffer(GL_READ_FRAMEBUFFER, origReadFramebuffer); glBindFramebuffer(GL_READ_FRAMEBUFFER, origReadFramebuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, origDrawFramebuffer); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, origDrawFramebuffer);
glViewport(0, 0, sourceTexture->Width(), sourceTexture->Height()); glViewport(0, 0, sourceTexture.Width(), sourceTexture.Height());
Shader::Unbind(); Shader::Unbind();
} }

View File

@ -62,10 +62,10 @@ public:
/** /**
* @brief Renders the required blur passes on the given texture. * @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. * @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. * @brief Binds the user-readable blur textures to the texture slots starting with the given index.

View File

@ -116,7 +116,11 @@ void MilkdropPreset::RenderFrame(const libprojectM::Audio::FrameAudioData& audio
m_framebuffer.RemoveColorAttachment(m_currentFrameBuffer, 1); m_framebuffer.RemoveColorAttachment(m_currentFrameBuffer, 1);
// Update blur textures // 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 // Draw audio-data-related stuff
for (auto& shape : m_customShapes) for (auto& shape : m_customShapes)

View File

@ -3,9 +3,17 @@ precision mediump float;
layout(location = 0) in vec2 vertex_position; layout(location = 0) in vec2 vertex_position;
layout(location = 1) in vec2 vertex_texture; layout(location = 1) in vec2 vertex_texture;
uniform int flipVertical;
out vec2 fragment_texture; out vec2 fragment_texture;
void main(){ void main(){
gl_Position = vec4(vertex_position, 0.0, 1.0); gl_Position = vec4(vertex_position, 0.0, 1.0);
fragment_texture = vertex_texture; 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;
}
} }