Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve outline shader performance #78

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions osu.Framework.Font.Tests/Visual/Shaders/TestSceneOutlineShader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using NUnit.Framework;
using osu.Framework.Extensions;
using osu.Framework.Extensions.Color4Extensions;
Expand Down Expand Up @@ -28,5 +29,25 @@ public void TestProperty(int radius, string colour)
};
});
}

[TestCase(128)]
public void CreateAnglePositionParams(int samples)
{
// it's a script on creating const params in shader.

const float pi = 3.14159265359f;
double angle = 0.0f;

Console.WriteLine($"const lowp vec2 angelPosition[{samples}] = lowp vec2[](");

for (int i = 0; i < samples; i++)
{
var last = i == samples - 1;
angle += 1.0 / (samples / 2.0) * pi;
Console.WriteLine($" lowp vec2({Math.Sin(angle):N2}, {Math.Cos(angle):N2})" + (last ? "" : ","));
}

Console.WriteLine(");");
}
}
}
143 changes: 137 additions & 6 deletions osu.Framework.Font/Resources/Shaders/sh_Outline.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,154 @@ uniform mediump vec2 g_TexSize;
uniform int g_Radius;
uniform vec4 g_Colour;

const lowp vec2 angelPosition[SAMPLES] = lowp vec2[](
lowp vec2(0.05, 1.00),
lowp vec2(0.10, 1.00),
lowp vec2(0.15, 0.99),
lowp vec2(0.20, 0.98),
lowp vec2(0.24, 0.97),
lowp vec2(0.29, 0.96),
lowp vec2(0.34, 0.94),
lowp vec2(0.38, 0.92),
lowp vec2(0.43, 0.90),
lowp vec2(0.47, 0.88),
lowp vec2(0.51, 0.86),
lowp vec2(0.56, 0.83),
lowp vec2(0.60, 0.80),
lowp vec2(0.63, 0.77),
lowp vec2(0.67, 0.74),
lowp vec2(0.71, 0.71),
lowp vec2(0.74, 0.67),
lowp vec2(0.77, 0.63),
lowp vec2(0.80, 0.60),
lowp vec2(0.83, 0.56),
lowp vec2(0.86, 0.51),
lowp vec2(0.88, 0.47),
lowp vec2(0.90, 0.43),
lowp vec2(0.92, 0.38),
lowp vec2(0.94, 0.34),
lowp vec2(0.96, 0.29),
lowp vec2(0.97, 0.24),
lowp vec2(0.98, 0.20),
lowp vec2(0.99, 0.15),
lowp vec2(1.00, 0.10),
lowp vec2(1.00, 0.05),
lowp vec2(1.00, -0.00),
lowp vec2(1.00, -0.05),
lowp vec2(1.00, -0.10),
lowp vec2(0.99, -0.15),
lowp vec2(0.98, -0.20),
lowp vec2(0.97, -0.24),
lowp vec2(0.96, -0.29),
lowp vec2(0.94, -0.34),
lowp vec2(0.92, -0.38),
lowp vec2(0.90, -0.43),
lowp vec2(0.88, -0.47),
lowp vec2(0.86, -0.51),
lowp vec2(0.83, -0.56),
lowp vec2(0.80, -0.60),
lowp vec2(0.77, -0.63),
lowp vec2(0.74, -0.67),
lowp vec2(0.71, -0.71),
lowp vec2(0.67, -0.74),
lowp vec2(0.63, -0.77),
lowp vec2(0.60, -0.80),
lowp vec2(0.56, -0.83),
lowp vec2(0.51, -0.86),
lowp vec2(0.47, -0.88),
lowp vec2(0.43, -0.90),
lowp vec2(0.38, -0.92),
lowp vec2(0.34, -0.94),
lowp vec2(0.29, -0.96),
lowp vec2(0.24, -0.97),
lowp vec2(0.20, -0.98),
lowp vec2(0.15, -0.99),
lowp vec2(0.10, -1.00),
lowp vec2(0.05, -1.00),
lowp vec2(-0.00, -1.00),
lowp vec2(-0.05, -1.00),
lowp vec2(-0.10, -1.00),
lowp vec2(-0.15, -0.99),
lowp vec2(-0.20, -0.98),
lowp vec2(-0.24, -0.97),
lowp vec2(-0.29, -0.96),
lowp vec2(-0.34, -0.94),
lowp vec2(-0.38, -0.92),
lowp vec2(-0.43, -0.90),
lowp vec2(-0.47, -0.88),
lowp vec2(-0.51, -0.86),
lowp vec2(-0.56, -0.83),
lowp vec2(-0.60, -0.80),
lowp vec2(-0.63, -0.77),
lowp vec2(-0.67, -0.74),
lowp vec2(-0.71, -0.71),
lowp vec2(-0.74, -0.67),
lowp vec2(-0.77, -0.63),
lowp vec2(-0.80, -0.60),
lowp vec2(-0.83, -0.56),
lowp vec2(-0.86, -0.51),
lowp vec2(-0.88, -0.47),
lowp vec2(-0.90, -0.43),
lowp vec2(-0.92, -0.38),
lowp vec2(-0.94, -0.34),
lowp vec2(-0.96, -0.29),
lowp vec2(-0.97, -0.24),
lowp vec2(-0.98, -0.20),
lowp vec2(-0.99, -0.15),
lowp vec2(-1.00, -0.10),
lowp vec2(-1.00, -0.05),
lowp vec2(-1.00, 0.00),
lowp vec2(-1.00, 0.05),
lowp vec2(-1.00, 0.10),
lowp vec2(-0.99, 0.15),
lowp vec2(-0.98, 0.20),
lowp vec2(-0.97, 0.24),
lowp vec2(-0.96, 0.29),
lowp vec2(-0.94, 0.34),
lowp vec2(-0.92, 0.38),
lowp vec2(-0.90, 0.43),
lowp vec2(-0.88, 0.47),
lowp vec2(-0.86, 0.51),
lowp vec2(-0.83, 0.56),
lowp vec2(-0.80, 0.60),
lowp vec2(-0.77, 0.63),
lowp vec2(-0.74, 0.67),
lowp vec2(-0.71, 0.71),
lowp vec2(-0.67, 0.74),
lowp vec2(-0.63, 0.77),
lowp vec2(-0.60, 0.80),
lowp vec2(-0.56, 0.83),
lowp vec2(-0.51, 0.86),
lowp vec2(-0.47, 0.88),
lowp vec2(-0.43, 0.90),
lowp vec2(-0.38, 0.92),
lowp vec2(-0.34, 0.94),
lowp vec2(-0.29, 0.96),
lowp vec2(-0.24, 0.97),
lowp vec2(-0.20, 0.98),
lowp vec2(-0.15, 0.99),
lowp vec2(-0.10, 1.00),
lowp vec2(-0.05, 1.00),
lowp vec2(0.00, 1.00)
);

lowp vec4 outline(sampler2D tex, int radius, mediump vec2 texCoord, mediump vec2 texSize, mediump vec4 colour)
{
float angle = 0.0;
float outlineAlpha = 0.0;

for (int i = 0; i < SAMPLES; i++)
{
angle += 1.0 / (float(SAMPLES) / 2.0) * PI;

// todo: might need to adjust step samples amount to fill the inner side.
// but it will cause lots of performance issue if make step samples larger.
// so should find a better algorithm to fill inner colour.
for (int j = 1; j <= STEP_SAMPLES; j++)
{
vec2 testPoint = texCoord - vec2(sin(angle), cos(angle)) * (float(radius) * (1.0 / j)) / texSize;
float sampledAlpha = texture2D(tex, testPoint).a;
outlineAlpha = max(outlineAlpha, sampledAlpha);
if(outlineAlpha < 0.99)
{
vec2 testPoint = texCoord - angelPosition[i] * (float(radius) * (1.0 / j)) / texSize;
float sampledAlpha = texture2D(tex, testPoint).a;
outlineAlpha = max(outlineAlpha, sampledAlpha);
}
}
}

Expand Down