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

Upgrade package to the latest. #322

Merged
10 changes: 5 additions & 5 deletions osu.Framework.Font.Tests/Shaders/OutlineShaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ public void CreateGetOutlineMethod(int samples)
const float pi = 3.14159265359f;
double angle = 0.0f;

Console.WriteLine("lowp float outlineAlpha(sampler2D tex, float radius, mediump vec2 texCoord, mediump vec2 texSize)");
Console.WriteLine("lowp float outlineAlpha(in float radius, in mediump vec2 texCoord, in mediump vec2 texSize)");
Console.WriteLine("{");
Console.WriteLine(" mediump vec2 offset = mediump vec2(radius) / texSize;");
Console.WriteLine(" lowp float alpha = 0.0;");
Console.WriteLine(" mediump vec2 offset = vec2(radius) / texSize;");
Console.WriteLine(" lowp float alpha = 0.0;");
Console.WriteLine();

for (int i = 0; i < samples; i++)
{
angle += 1.0 / (samples / 2.0) * pi;
Console.WriteLine($" alpha = max(alpha, texture2D(tex, texCoord - lowp vec2({Math.Sin(angle):N2}, {Math.Cos(angle):N2}) * offset).a);");
Console.WriteLine($" alpha = max(alpha, tex(texCoord - vec2({Math.Sin(angle):N2}, {Math.Cos(angle):N2}) * offset).a);");
}

Console.WriteLine(" return alpha;");
Console.WriteLine(" return alpha;");
Console.WriteLine("}");
}
}
3 changes: 1 addition & 2 deletions osu.Framework.Font.Tests/Visual/BackgroundGridTestScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
using osu.Framework.Graphics.Shaders;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Framework.Testing;
using osuTK;
using osuTK.Graphics;

namespace osu.Framework.Font.Tests.Visual;

public abstract partial class BackgroundGridTestScene : TestScene
public abstract partial class BackgroundGridTestScene : FrameworkTestScene
{
protected const int GRID_SIZE = 30;

Expand Down
18 changes: 18 additions & 0 deletions osu.Framework.Font.Tests/Visual/FrameworkTestScene.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) karaoke.dev <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Testing;

namespace osu.Framework.Font.Tests.Visual;

/// <summary>
/// All the test scene with resource should inherit this class.
/// </summary>
public abstract partial class FrameworkTestScene : TestScene
{
/// <summary>
/// This runner will be created only if run the unit-test.
/// </summary>
/// <returns></returns>
protected override ITestSceneTestRunner CreateRunner() => new FrameworkTestSceneTestRunner();
}
18 changes: 18 additions & 0 deletions osu.Framework.Font.Tests/Visual/FrameworkTestSceneTestRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) karaoke.dev <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Allocation;
using osu.Framework.IO.Stores;
using osu.Framework.Testing;

namespace osu.Framework.Font.Tests.Visual;

public partial class FrameworkTestSceneTestRunner : TestSceneTestRunner
{
[BackgroundDependencyLoader]
private void load()
{
Resources.AddStore(new NamespacedResourceStore<byte[]>(new DllResourceStore(typeof(FrameworkTestSceneTestRunner).Assembly), "Resources"));
Resources.AddStore(new NamespacedResourceStore<byte[]>(new ShaderResourceStore(), "Resources"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shaders;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Testing;
using osuTK;
using osuTK.Graphics;

namespace osu.Framework.Font.Tests.Visual.Sprites;

public partial class TestSceneKaraokeSpriteText : TestScene
public partial class TestSceneKaraokeSpriteText : FrameworkTestScene
{
private readonly TestKaraokeSpriteText karaokeSpriteText;
private readonly SpriteText transformAmountSpriteText;
Expand Down Expand Up @@ -223,7 +222,7 @@ public void TestLyricShaders()
});
}

private class TestKaraokeSpriteText : KaraokeSpriteText
private partial class TestKaraokeSpriteText : KaraokeSpriteText
{
public Action? TransformAction;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void TestApplyOutlineAndShadowShader(bool differentSizing)
private static string getApplyDescription(bool applyDifferentSizing)
=> applyDifferentSizing ? "Apply shader with different sizing" : "Apply shader";

private class TestKaraokeSpriteText : KaraokeSpriteText
private partial class TestKaraokeSpriteText : KaraokeSpriteText
{
public override bool RemoveCompletedTransforms => false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void ApplyRightLyricTextShader()
});
}

private class TestKaraokeSpriteText : KaraokeSpriteText
private partial class TestKaraokeSpriteText : KaraokeSpriteText
{
public override bool RemoveCompletedTransforms => false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
using osu.Framework.Font.Tests.Helper;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Testing;
using osuTK;

namespace osu.Framework.Font.Tests.Visual.Sprites;

public partial class TestSceneLyricSpriteText : TestScene
public partial class TestSceneLyricSpriteText : FrameworkTestScene
{
[TestCase("karaoke", null, null)]
[TestCase("カラオケ", new[] { "[0,1]:か", "[1,2]:ら", "[2,3]:お", "[3,4]:け" }, null)]
Expand Down Expand Up @@ -230,11 +229,12 @@ private void setContents(Func<LyricSpriteText> creationFunction)
Child = creationFunction().With(x => x.Scale = new Vector2(2));
}

internal class DefaultLyricSpriteText : LyricSpriteText
internal partial class DefaultLyricSpriteText : LyricSpriteText
{
public DefaultLyricSpriteText(bool ruby = true, bool romaji = true)
{
Text = "カラオケ";
Font = FontUsage.Default;

if (ruby)
{
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework.Font.Tests/osu.Framework.Font.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="ppy.osu.Game.Resources" Version="2023.228.0" />
<PackageReference Include="ppy.osu.Game.Resources" Version="2023.314.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\osu.Framework.Font\osu.Framework.Font.csproj" />
Expand Down
33 changes: 28 additions & 5 deletions osu.Framework.Font/Graphics/CustomizedShaderBufferedDrawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Shaders;
using osuTK;
using osu.Framework.Graphics.Shaders.Types;
using osuTK.Graphics;

namespace osu.Framework.Graphics;
Expand Down Expand Up @@ -46,36 +47,58 @@ protected void ResetDrawVersion()
prop.SetValue(SharedData, -1);
}

private IUniformBuffer<SharedParameters>? sharedParametersBuffer;

protected void RenderShader(IRenderer renderer, ICustomizedShader shader, IFrameBuffer current, IFrameBuffer target)
{
sharedParametersBuffer ??= renderer.CreateUniformBuffer<SharedParameters>();

renderer.SetBlend(BlendingParameters.None);

using (BindFrameBuffer(target))
{
if (shader is IHasTextureSize)
{
var size = current.Size;
shader.GetUniform<Vector2>(@"g_TexSize").UpdateValue(ref size);
sharedParametersBuffer.Data = sharedParametersBuffer.Data with
{
TexSize = size
};
}

if (shader is IHasInflationPercentage)
{
var localInflationAmount = DrawInfo.Matrix.ExtractScale().X;
shader.GetUniform<float>(@"g_InflationPercentage").UpdateValue(ref localInflationAmount);
sharedParametersBuffer.Data = sharedParametersBuffer.Data with
{
InflationPercentage = localInflationAmount
};
}

if (shader is IHasCurrentTime)
{
var currentTime = (float)(Source.Clock.CurrentTime - loadTime) / 1000;
shader.GetUniform<float>("g_Time").UpdateValue(ref currentTime);
sharedParametersBuffer.Data = sharedParametersBuffer.Data with
{
Time = currentTime
};
}

if (shader is ICustomizedShader customizedShader)
customizedShader.ApplyValue();
customizedShader.ApplyValue(renderer);

shader.Bind();
shader.BindUniformBlock("m_SharedParameters", sharedParametersBuffer);
renderer.DrawFrameBuffer(current, new RectangleF(0, 0, current.Texture.Width, current.Texture.Height), ColourInfo.SingleColour(Color4.White));
shader.Unbind();
}
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
private record struct SharedParameters
{
public UniformVector2 TexSize;
public UniformFloat InflationPercentage;
public UniformFloat Time;
}
}
6 changes: 5 additions & 1 deletion osu.Framework.Font/Graphics/Shaders/CustomizedShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using osu.Framework.Graphics.Rendering;

namespace osu.Framework.Graphics.Shaders;

Expand All @@ -24,11 +25,14 @@ public void AttachOriginShader(IShader originShader)
public Uniform<T> GetUniform<T>(string name) where T : unmanaged, IEquatable<T>
=> shader.GetUniform<T>(name);

public void BindUniformBlock(string blockName, IUniformBuffer buffer)
=> shader.BindUniformBlock(blockName, buffer);

public bool IsLoaded => shader.IsLoaded;

public bool IsBound { get; private set; }

public abstract void ApplyValue();
public abstract void ApplyValue(IRenderer renderer);

public void Dispose()
{
Expand Down
10 changes: 9 additions & 1 deletion osu.Framework.Font/Graphics/Shaders/ICustomizedShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using osu.Framework.Graphics.Rendering;

namespace osu.Framework.Graphics.Shaders;

public interface ICustomizedShader
{
void ApplyValue();
void ApplyValue(IRenderer renderer);

/// <summary>
/// Binds this shader to be used for rendering.
Expand Down Expand Up @@ -36,4 +37,11 @@ public interface ICustomizedShader
/// <returns>The retrieved uniform.</returns>
Uniform<T> GetUniform<T>(string name)
where T : unmanaged, IEquatable<T>;

/// <summary>
/// Binds an <see cref="IUniformBuffer"/> to a uniform block of the given name.
/// </summary>
/// <param name="blockName">The uniform block name.</param>
/// <param name="buffer">The buffer to bind to the block.</param>
void BindUniformBlock(string blockName, IUniformBuffer buffer);
}
32 changes: 24 additions & 8 deletions osu.Framework.Font/Graphics/Shaders/OutlineShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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 @@ -14,25 +17,38 @@ public class OutlineShader : InternalShader, IApplicableToCharacterSize, IApplic

public Color4 Colour { get; set; }

public Color4 OutlineColour { get; set; }

public float Radius { get; set; }

public Color4 OutlineColour { get; set; }
private IUniformBuffer<OutlineParameters>? outlineParametersBuffer;

public override void ApplyValue()
public override void ApplyValue(IRenderer renderer)
{
var colourMatrix = new Vector4(Colour.R, Colour.G, Colour.B, Colour.A);
GetUniform<Vector4>(@"g_Colour").UpdateValue(ref colourMatrix);
outlineParametersBuffer ??= renderer.CreateUniformBuffer<OutlineParameters>();

float radius = Radius;
GetUniform<float>(@"g_Radius").UpdateValue(ref radius);
outlineParametersBuffer.Data = new OutlineParameters
{
Colour = new Vector4(Colour.R, Colour.G, Colour.B, Colour.A),
OutlineColour = new Vector4(OutlineColour.R, OutlineColour.G, OutlineColour.B, OutlineColour.A),
Radius = Radius,
};

var outlineColourMatrix = new Vector4(OutlineColour.R, OutlineColour.G, OutlineColour.B, OutlineColour.A);
GetUniform<Vector4>(@"g_OutlineColour").UpdateValue(ref outlineColourMatrix);
BindUniformBlock("m_OutlineParameters", outlineParametersBuffer);
}

public RectangleF ComputeCharacterDrawRectangle(RectangleF originalCharacterDrawRectangle)
=> originalCharacterDrawRectangle.Inflate(Math.Max(Radius, 0));

public RectangleF ComputeDrawRectangle(RectangleF originDrawRectangle)
=> ComputeCharacterDrawRectangle(originDrawRectangle);

[StructLayout(LayoutKind.Sequential, Pack = 1)]
private record struct OutlineParameters
{
public UniformVector4 Colour;
public UniformVector4 OutlineColour;
public UniformFloat Radius;
private readonly UniformPadding12 pad1;
}
}
21 changes: 18 additions & 3 deletions osu.Framework.Font/Graphics/Shaders/PixelShader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (c) karaoke.dev <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

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

namespace osu.Framework.Graphics.Shaders;
Expand All @@ -11,9 +14,21 @@ public class PixelShader : InternalShader, IHasTextureSize, IHasInflationPercent

public Vector2 Size { get; set; } = new(5);

public override void ApplyValue()
private IUniformBuffer<PixelParameters>? pixelParametersBuffer;

public override void ApplyValue(IRenderer renderer)
{
pixelParametersBuffer ??= renderer.CreateUniformBuffer<PixelParameters>();

pixelParametersBuffer.Data = new PixelParameters { Size = Size };

BindUniformBlock("m_PixelParameters", pixelParametersBuffer);
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
private record struct PixelParameters
{
var size = Size;
GetUniform<Vector2>(@"g_Size").UpdateValue(ref size);
public UniformVector2 Size;
private readonly UniformPadding8 pad1;
}
}
Loading