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 framework, upgrade shaders to new spir-v #321

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 2 additions & 2 deletions osu.Framework.Font.Tests/Visual/BackgroundGridTestScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ protected BackgroundGridTestScene()
});
}

protected T GetShaderByType<T>() where T : InternalShader, new()
=> shaderManager.LocalInternalShader<T>();
protected T GetShaderByType<T>() where T : ICustomizedShader, new()
=> shaderManager.LocalCustomizedShader<T>();

protected partial class DraggableCircle : Circle
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

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

public abstract partial class InternalShaderTestScene : ShaderTestScene
public abstract partial class CustomizedShaderTestScene : ShaderTestScene
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

public partial class TestSceneOutlineShader : InternalShaderTestScene
public partial class TestSceneOutlineShader : CustomizedShaderTestScene
{
[TestCase(RED)] // will make the inner texture red
[TestCase(GREEN)] // will make the inner texture green
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

public partial class TestScenePixelShader : InternalShaderTestScene
public partial class TestScenePixelShader : CustomizedShaderTestScene
{
[TestCase(5, 5)]
[TestCase(5, 20)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

public partial class TestSceneRainbowShader : InternalShaderTestScene
public partial class TestSceneRainbowShader : CustomizedShaderTestScene
{
[TestCase("(0,1)", 1, 1, 1, 1, 1)] // normal state.
[TestCase("(0.5,0.7)", 1, 1, 1, 1, 1)] // restrict color range?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

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

public partial class TestSceneRepeatMovingBackgroundShader : InternalShaderTestScene
public partial class TestSceneRepeatMovingBackgroundShader : CustomizedShaderTestScene
{
[Resolved, AllowNull]
private TextureStore textures { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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

public partial class TestSceneShadowShader : InternalShaderTestScene
public partial class TestSceneShadowShader : CustomizedShaderTestScene
{
[TestCase(RED, "(3,3)")]
[TestCase(GREEN, "(-4,-4)")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void TestLyricShaders()
{
karaokeSpriteText.LeftLyricTextShaders = new[]
{
shaderManager.LocalInternalShader<OutlineShader>().With(s =>
shaderManager.LocalCustomizedShader<OutlineShader>().With(s =>
{
s.Radius = 3;
s.Colour = Color4Extensions.FromHex("#FFDD77");
Expand All @@ -207,7 +207,7 @@ public void TestLyricShaders()
};
karaokeSpriteText.RightLyricTextShaders = new[]
{
shaderManager.LocalInternalShader<OutlineShader>().With(s =>
shaderManager.LocalCustomizedShader<OutlineShader>().With(s =>
{
s.Radius = 3;
s.Colour = Color4Extensions.FromHex("#AA88FF");
Expand All @@ -223,7 +223,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 @@ -230,7 +230,7 @@ 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)
{
Expand Down
14 changes: 7 additions & 7 deletions osu.Framework.Font/Extensions/ShaderManagerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ namespace osu.Framework.Extensions;

public static class ShaderManagerExtensions
{
public static T LocalInternalShader<T>(this ShaderManager shaderManager) where T : InternalShader, new()
public static T LocalCustomizedShader<T>(this ShaderManager shaderManager) where T : ICustomizedShader, new()
{
var internalShader = new T();
shaderManager.AttachShader(internalShader);
return internalShader;
var customizedShader = new T();
AttachShader(shaderManager, customizedShader);
return customizedShader;
}

public static void AttachShader<T>(this ShaderManager shaderManager, T internalShader) where T : InternalShader
public static void AttachShader(this ShaderManager shaderManager, ICustomizedShader customizedShader)
{
var shader = shaderManager.Load(VertexShaderDescriptor.TEXTURE_2, internalShader.ShaderName);
internalShader.AttachOriginShader(shader);
var shader = shaderManager.Load(VertexShaderDescriptor.TEXTURE_2, customizedShader.ShaderName);
customizedShader.AttachOriginShader(shader);
}
}
26 changes: 9 additions & 17 deletions osu.Framework.Font/Graphics/CustomizedShaderBufferedDrawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Shaders;
using osuTK;
using osuTK.Graphics;

namespace osu.Framework.Graphics;
Expand Down Expand Up @@ -50,30 +49,23 @@ protected void RenderShader(IRenderer renderer, ICustomizedShader shader, IFrame
{
renderer.SetBlend(BlendingParameters.None);

shader.PrepareUniforms(renderer);

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

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

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

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

shader.Bind();
renderer.DrawFrameBuffer(current, new RectangleF(0, 0, current.Texture.Width, current.Texture.Height), ColourInfo.SingleColour(Color4.White));
shader.Unbind();
}
Expand Down
20 changes: 16 additions & 4 deletions osu.Framework.Font/Graphics/Shaders/CustomizedShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,31 @@
// See the LICENCE file in the repository root for full licence text.

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

namespace osu.Framework.Graphics.Shaders;

/// <summary>
/// Shader with customized property
/// </summary>
public abstract class CustomizedShader : ICustomizedShader
public abstract class CustomizedShader<TUniform> : ICustomizedShader
where TUniform : unmanaged, IEquatable<TUniform>
{
private IShader shader = null!;

public abstract string ShaderName { get; }

private IUniformBuffer<TUniform>? uniformBuffer;

protected IUniformBuffer<TUniform> UniformBuffer => uniformBuffer!;

protected IShader OriginShader => shader;

public void PrepareUniforms(IRenderer renderer)
{
uniformBuffer ??= renderer.CreateUniformBuffer<TUniform>();
}

public void AttachOriginShader(IShader originShader)
{
shader = originShader ?? throw new ArgumentNullException(nameof(originShader));
Expand All @@ -21,9 +36,6 @@ public void AttachOriginShader(IShader originShader)

public void Unbind() => shader.Unbind();

public Uniform<T> GetUniform<T>(string name) where T : unmanaged, IEquatable<T>
=> shader.GetUniform<T>(name);

public bool IsLoaded => shader.IsLoaded;

public bool IsBound { get; private set; }
Expand Down
25 changes: 16 additions & 9 deletions osu.Framework.Font/Graphics/Shaders/ICustomizedShader.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
// 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;
using osu.Framework.Graphics.Rendering;

namespace osu.Framework.Graphics.Shaders;

public interface ICustomizedShader
{
/// <summary>
/// Fragment shader name to load (`sh_` prefix is implicitly added)
/// </summary>
string ShaderName { get; }

void ApplyValue();

/// <summary>
/// Make sure shader uniforms are ready for initialization and upload.
/// </summary>
void PrepareUniforms(IRenderer renderer);

/// <summary>
/// Sets the original shader that is going to be uniform-tweaked.
/// </summary>
void AttachOriginShader(IShader originShader);

/// <summary>
/// Binds this shader to be used for rendering.
/// </summary>
Expand All @@ -28,12 +43,4 @@ public interface ICustomizedShader
/// Whether this shader is currently bound.
/// </summary>
bool IsBound { get; }

/// <summary>
/// Retrieves a uniform from the shader.
/// </summary>
/// <param name="name">The name of the uniform.</param>
/// <returns>The retrieved uniform.</returns>
Uniform<T> GetUniform<T>(string name)
where T : unmanaged, IEquatable<T>;
}
1 change: 1 addition & 0 deletions osu.Framework.Font/Graphics/Shaders/IHasCurrentTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ namespace osu.Framework.Graphics.Shaders;

public interface IHasCurrentTime
{
float CurrentTime { set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ namespace osu.Framework.Graphics.Shaders;

public interface IHasInflationPercentage
{
float InflationPercentage { set; }
}
3 changes: 3 additions & 0 deletions osu.Framework.Font/Graphics/Shaders/IHasTextureSize.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Copyright (c) karaoke.dev <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using osuTK;

namespace osu.Framework.Graphics.Shaders;

public interface IHasTextureSize
{
Vector2 TextureSize { set; }
}
9 changes: 0 additions & 9 deletions osu.Framework.Font/Graphics/Shaders/InternalShader.cs

This file was deleted.

36 changes: 27 additions & 9 deletions osu.Framework.Font/Graphics/Shaders/OutlineShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,50 @@
// 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.Shaders.Types;
using osuTK;
using osuTK.Graphics;

namespace osu.Framework.Graphics.Shaders;

public class OutlineShader : InternalShader, IApplicableToCharacterSize, IApplicableToDrawRectangle, IHasTextureSize, IHasInflationPercentage
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public record struct OutlineParameters
{
public UniformVector4 Colour;
public UniformVector4 OutlineColour;
public UniformVector2 TexSize;
public UniformFloat Radius;
public UniformFloat InflationPercentage;
}

public class OutlineShader : CustomizedShader<OutlineParameters>, IApplicableToCharacterSize, IApplicableToDrawRectangle, IHasTextureSize, IHasInflationPercentage
{
public override string ShaderName => "Outline";

public Vector2 TextureSize { get; set; }

public Color4 Colour { get; set; }

public float Radius { get; set; }

public Color4 OutlineColour { get; set; }

public float InflationPercentage { get; set; }

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

float radius = Radius;
GetUniform<float>(@"g_Radius").UpdateValue(ref radius);

var outlineColourMatrix = new Vector4(OutlineColour.R, OutlineColour.G, OutlineColour.B, OutlineColour.A);
GetUniform<Vector4>(@"g_OutlineColour").UpdateValue(ref outlineColourMatrix);
UniformBuffer.Data = new OutlineParameters
{
TexSize = TextureSize,
Colour = new Vector4(Colour.R, Colour.G, Colour.B, Colour.A),
Radius = Radius,
OutlineColour = new Vector4(OutlineColour.R, OutlineColour.G, OutlineColour.B, OutlineColour.A),
InflationPercentage = InflationPercentage
};

OriginShader.BindUniformBlock("m_OutlineParameters", UniformBuffer);
}

public RectangleF ComputeCharacterDrawRectangle(RectangleF originalCharacterDrawRectangle)
Expand Down
Loading