Skip to content

Commit

Permalink
Fix blurring under D3D
Browse files Browse the repository at this point in the history
  • Loading branch information
smoogipoo committed Mar 6, 2023
1 parent bb45f88 commit 53272d8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion osu.Framework/Graphics/Containers/BufferedContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public BufferedContainer(RenderBufferFormat[] formats = null, bool pixelSnapping
private void load(ShaderManager shaders)
{
TextureShader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE);
blurShader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.BLUR);
blurShader = shaders.Load(VertexShaderDescriptor.BLUR, FragmentShaderDescriptor.BLUR);
}

protected override DrawNode CreateDrawNode() => new BufferedContainerDrawNode(this, sharedData);
Expand Down
34 changes: 32 additions & 2 deletions osu.Framework/Graphics/Containers/BufferedContainer_DrawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
using System.Runtime.InteropServices;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Rendering.Vertices;
using osu.Framework.Graphics.Shaders.Types;
using osu.Framework.Utils;
using osuTK.Graphics.ES30;

namespace osu.Framework.Graphics.Containers
{
Expand All @@ -25,6 +27,8 @@ private class BufferedContainerDrawNode : BufferedDrawNode, ICompositeDrawNode

protected new CompositeDrawableDrawNode Child => (CompositeDrawableDrawNode)base.Child;

private readonly Action<TexturedVertex2D> addVertexAction;

private bool drawOriginal;
private ColourInfo effectColour;
private BlendingParameters effectBlending;
Expand All @@ -35,12 +39,19 @@ private class BufferedContainerDrawNode : BufferedDrawNode, ICompositeDrawNode
private float blurRotation;

private long updateVersion;

private IShader blurShader;

public BufferedContainerDrawNode(BufferedContainer<T> source, BufferedContainerDrawNodeSharedData sharedData)
: base(source, new CompositeDrawableDrawNode(source), sharedData)
{
addVertexAction = v =>
{
blurQuadBatch!.Add(new BlurVertex
{
Position = v.Position,
TexturePosition = v.TexturePosition
});
};
}

public override void ApplyState()
Expand Down Expand Up @@ -95,10 +106,12 @@ protected override void DrawContents(IRenderer renderer)
}

private IUniformBuffer<BlurParameters> blurParametersBuffer;
private IVertexBatch<BlurVertex> blurQuadBatch;

private void drawBlurredFrameBuffer(IRenderer renderer, int kernelRadius, float sigma, float blurRotation)
{
blurParametersBuffer ??= renderer.CreateUniformBuffer<BlurParameters>();
blurQuadBatch ??= renderer.CreateQuadBatch<BlurVertex>(1, 1);

IFrameBuffer current = SharedData.CurrentEffectBuffer;
IFrameBuffer target = SharedData.GetNextEffectBuffer();
Expand All @@ -119,7 +132,9 @@ private void drawBlurredFrameBuffer(IRenderer renderer, int kernelRadius, float

blurShader.BindUniformBlock("m_BlurParameters", blurParametersBuffer);
blurShader.Bind();
renderer.DrawFrameBuffer(current, new RectangleF(0, 0, current.Texture.Width, current.Texture.Height), ColourInfo.SingleColour(Color4.White));

renderer.DrawFrameBuffer(current, new RectangleF(0, 0, current.Texture.Width, current.Texture.Height), ColourInfo.SingleColour(Color4.White), addVertexAction);

blurShader.Unbind();
}
}
Expand All @@ -136,6 +151,7 @@ protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
blurParametersBuffer?.Dispose();
blurQuadBatch?.Dispose();
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
Expand All @@ -147,6 +163,20 @@ private record struct BlurParameters
public UniformVector2 Direction;
private readonly UniformPadding8 pad1;
}

[StructLayout(LayoutKind.Sequential)]
public struct BlurVertex : IEquatable<BlurVertex>, IVertex
{
[VertexMember(2, VertexAttribPointerType.Float)]
public Vector2 Position;

[VertexMember(2, VertexAttribPointerType.Float)]
public Vector2 TexturePosition;

public readonly bool Equals(BlurVertex other) =>
Position.Equals(other.Position)
&& TexturePosition.Equals(other.TexturePosition);
}
}

private class BufferedContainerDrawNodeSharedData : BufferedDrawNodeSharedData
Expand Down
1 change: 1 addition & 0 deletions osu.Framework/Graphics/Shaders/ShaderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public static class VertexShaderDescriptor
public const string TEXTURE_2 = "Texture2D";
public const string TEXTURE_3 = "Texture3D";
public const string POSITION = "Position";
public const string BLUR = "Blur";
}

public static class FragmentShaderDescriptor
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Resources/Shaders/sh_Blur.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define INV_SQRT_2PI 0.39894

layout(location = 2) in mediump vec2 v_TexCoord;
layout(location = 0) in mediump vec2 v_TexCoord;

layout(std140, set = 0, binding = 0) uniform m_BlurParameters
{
Expand Down
12 changes: 12 additions & 0 deletions osu.Framework/Resources/Shaders/sh_Blur.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "sh_Utils.h"

layout(location = 0) in highp vec2 m_Position;
layout(location = 1) in highp vec2 m_TexCoord;

layout(location = 0) out highp vec2 v_TexCoord;

void main(void)
{
v_TexCoord = m_TexCoord;
gl_Position = g_ProjMatrix * vec4(m_Position, 1.0, 1.0);
}

0 comments on commit 53272d8

Please sign in to comment.