Skip to content

Commit

Permalink
Fix the shadow shader.
Browse files Browse the repository at this point in the history
Co-authored-by: WebFreak001 <[email protected]>
  • Loading branch information
andy840119 and WebFreak001 committed Mar 19, 2023
1 parent d5d0c3c commit 069aa20
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
24 changes: 21 additions & 3 deletions osu.Framework.Font/Graphics/Shaders/ShadowShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Runtime.InteropServices;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Shaders.Types;
using osuTK;
using osuTK.Graphics;

Expand All @@ -17,13 +19,21 @@ public class ShadowShader : InternalShader, IApplicableToDrawRectangle, IHasText

public Vector2 ShadowOffset { get; set; }

private IUniformBuffer<ShadowParameters>? shadowParametersBuffer;

public override void ApplyValue(IRenderer renderer)
{
var shadowColour = new Vector4(ShadowColour.R, ShadowColour.G, ShadowColour.B, ShadowColour.A);
GetUniform<Vector4>(@"g_Colour").UpdateValue(ref shadowColour);
shadowParametersBuffer ??= renderer.CreateUniformBuffer<ShadowParameters>();

var shadowColour = new Vector4(ShadowColour.R, ShadowColour.G, ShadowColour.B, ShadowColour.A);
var shadowOffset = new Vector2(-ShadowOffset.X, ShadowOffset.Y);
GetUniform<Vector2>(@"g_Offset").UpdateValue(ref shadowOffset);
shadowParametersBuffer.Data = new ShadowParameters
{
Colour = shadowColour,
Offset = shadowOffset,
};

BindUniformBlock("m_ShadowParameters", shadowParametersBuffer);
}

public RectangleF ComputeDrawRectangle(RectangleF originDrawRectangle)
Expand All @@ -34,4 +44,12 @@ public RectangleF ComputeDrawRectangle(RectangleF originDrawRectangle)
Top = Math.Max(-ShadowOffset.Y, 0),
Bottom = Math.Max(ShadowOffset.Y, 0),
});

[StructLayout(LayoutKind.Sequential, Pack = 1)]
private record struct ShadowParameters
{
public UniformVector4 Colour;
public UniformVector2 Offset;
private UniformPadding8 pad1;
}
}
27 changes: 16 additions & 11 deletions osu.Framework.Font/Resources/Shaders/sh_Shadow.fs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
#include "sh_CustomizedShaderGlobalUniforms.h"
#include "sh_Utils.h"

varying mediump vec2 v_TexCoord;
layout(location = 2) in highp vec2 v_TexCoord;

uniform lowp sampler2D m_Sampler;
layout(std140, set = 2, binding = 0) uniform m_ShadowParameters
{
mediump vec4 g_Colour;
mediump vec2 g_Offset;
};

layout(set = 1, binding = 0) uniform lowp texture2D m_Texture;
layout(set = 1, binding = 1) uniform lowp sampler m_Sampler;

uniform mediump vec2 g_TexSize;
uniform vec4 g_Colour;
uniform vec2 g_Offset;
uniform float g_InflationPercentage;
layout(location = 0) out vec4 o_Colour;

lowp vec4 shadow(sampler2D tex, mediump vec2 texCoord, mediump vec2 texSize, mediump vec4 colour, mediump vec2 offset)
lowp vec4 shadow(texture2D tex, mediump vec2 texCoord, mediump vec2 texSize, mediump vec4 colour, mediump vec2 offset)
{
return texture2D(tex, texCoord + offset / texSize).a * colour;
return texture(sampler2D(tex, m_Sampler), texCoord + offset / texSize).a * colour;
}

void main(void)
{
lowp vec4 texture = toSRGB(texture2D(m_Sampler, v_TexCoord));
lowp vec4 shadow = shadow(m_Sampler, v_TexCoord, g_TexSize, g_Colour, g_Offset * g_InflationPercentage);
gl_FragColor = mix(shadow, texture, texture.a);
lowp vec4 texture = toSRGB(texture(sampler2D(m_Texture, m_Sampler), v_TexCoord));
lowp vec4 shadow = shadow(m_Texture, v_TexCoord, g_TexSize, g_Colour, g_Offset * g_InflationPercentage);
o_Colour = mix(shadow, texture, texture.a);
}

0 comments on commit 069aa20

Please sign in to comment.