From 89f911b22c2971e7cd5b56a0463b89de88584f78 Mon Sep 17 00:00:00 2001 From: WebFreak001 Date: Sun, 19 Mar 2023 05:43:26 +0100 Subject: [PATCH 1/5] upgrade framework, upgrade shaders to new spir-v --- .../Visual/BackgroundGridTestScene.cs | 4 +- ...tScene.cs => CustomizedShaderTestScene.cs} | 2 +- .../Visual/Shaders/TestSceneOutlineShader.cs | 2 +- .../Visual/Shaders/TestScenePixelShader.cs | 2 +- .../Visual/Shaders/TestSceneRainbowShader.cs | 2 +- .../TestSceneRepeatMovingBackgroundShader.cs | 2 +- .../Visual/Shaders/TestSceneShadowShader.cs | 2 +- .../Sprites/TestSceneKaraokeSpriteText.cs | 4 +- .../Extensions/ShaderManagerExtensions.cs | 14 +-- .../CustomizedShaderBufferedDrawNode.cs | 25 ++-- .../Graphics/Shaders/CustomizedShader.cs | 21 +++- .../Graphics/Shaders/ICustomizedShader.cs | 25 ++-- .../Graphics/Shaders/IHasCurrentTime.cs | 1 + .../Shaders/IHasInflationPercentage.cs | 1 + .../Graphics/Shaders/IHasTextureSize.cs | 3 + .../Graphics/Shaders/InternalShader.cs | 9 -- .../Graphics/Shaders/OutlineShader.cs | 36 ++++-- .../Graphics/Shaders/PixelShader.cs | 28 ++++- .../Graphics/Shaders/RainbowShader.cs | 50 +++++--- .../Shaders/RepeatMovingBackgroundShader.cs | 55 +++++---- .../Graphics/Shaders/ShadowShader.cs | 30 ++++- .../Graphics/Shaders/StepShader.cs | 13 +- .../Resources/Shaders/sh_Outline.fs | 113 ++++++++++-------- .../Resources/Shaders/sh_Pixel.fs | 17 ++- .../Resources/Shaders/sh_Rainbow.fs | 39 +++--- .../Shaders/sh_RepeatMovingBackground.fs | 56 +++++---- .../Resources/Shaders/sh_Shadow.fs | 28 +++-- osu.Framework.Font/osu.Framework.Font.csproj | 2 +- 28 files changed, 363 insertions(+), 223 deletions(-) rename osu.Framework.Font.Tests/Visual/Shaders/{InternalShaderTestScene.cs => CustomizedShaderTestScene.cs} (74%) delete mode 100644 osu.Framework.Font/Graphics/Shaders/InternalShader.cs diff --git a/osu.Framework.Font.Tests/Visual/BackgroundGridTestScene.cs b/osu.Framework.Font.Tests/Visual/BackgroundGridTestScene.cs index 70c375f..1c0b5fa 100644 --- a/osu.Framework.Font.Tests/Visual/BackgroundGridTestScene.cs +++ b/osu.Framework.Font.Tests/Visual/BackgroundGridTestScene.cs @@ -83,8 +83,8 @@ protected BackgroundGridTestScene() }); } - protected T GetShaderByType() where T : InternalShader, new() - => shaderManager.LocalInternalShader(); + protected T GetShaderByType() where T : ICustomizedShader + => shaderManager.LocalCustomizedShader(); protected partial class DraggableCircle : Circle { diff --git a/osu.Framework.Font.Tests/Visual/Shaders/InternalShaderTestScene.cs b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs similarity index 74% rename from osu.Framework.Font.Tests/Visual/Shaders/InternalShaderTestScene.cs rename to osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs index 15a5145..cbf9bc3 100644 --- a/osu.Framework.Font.Tests/Visual/Shaders/InternalShaderTestScene.cs +++ b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs @@ -3,6 +3,6 @@ namespace osu.Framework.Font.Tests.Visual.Shaders; -public abstract partial class InternalShaderTestScene : ShaderTestScene +public abstract partial class CustomizedShaderTestScene : ShaderTestScene { } diff --git a/osu.Framework.Font.Tests/Visual/Shaders/TestSceneOutlineShader.cs b/osu.Framework.Font.Tests/Visual/Shaders/TestSceneOutlineShader.cs index f0c6034..91fccfc 100644 --- a/osu.Framework.Font.Tests/Visual/Shaders/TestSceneOutlineShader.cs +++ b/osu.Framework.Font.Tests/Visual/Shaders/TestSceneOutlineShader.cs @@ -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 diff --git a/osu.Framework.Font.Tests/Visual/Shaders/TestScenePixelShader.cs b/osu.Framework.Font.Tests/Visual/Shaders/TestScenePixelShader.cs index 72d0de7..599b4ab 100644 --- a/osu.Framework.Font.Tests/Visual/Shaders/TestScenePixelShader.cs +++ b/osu.Framework.Font.Tests/Visual/Shaders/TestScenePixelShader.cs @@ -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)] diff --git a/osu.Framework.Font.Tests/Visual/Shaders/TestSceneRainbowShader.cs b/osu.Framework.Font.Tests/Visual/Shaders/TestSceneRainbowShader.cs index 2588aa2..2e27787 100644 --- a/osu.Framework.Font.Tests/Visual/Shaders/TestSceneRainbowShader.cs +++ b/osu.Framework.Font.Tests/Visual/Shaders/TestSceneRainbowShader.cs @@ -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? diff --git a/osu.Framework.Font.Tests/Visual/Shaders/TestSceneRepeatMovingBackgroundShader.cs b/osu.Framework.Font.Tests/Visual/Shaders/TestSceneRepeatMovingBackgroundShader.cs index 60cd814..5a7322c 100644 --- a/osu.Framework.Font.Tests/Visual/Shaders/TestSceneRepeatMovingBackgroundShader.cs +++ b/osu.Framework.Font.Tests/Visual/Shaders/TestSceneRepeatMovingBackgroundShader.cs @@ -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; } diff --git a/osu.Framework.Font.Tests/Visual/Shaders/TestSceneShadowShader.cs b/osu.Framework.Font.Tests/Visual/Shaders/TestSceneShadowShader.cs index cf9ef29..937af32 100644 --- a/osu.Framework.Font.Tests/Visual/Shaders/TestSceneShadowShader.cs +++ b/osu.Framework.Font.Tests/Visual/Shaders/TestSceneShadowShader.cs @@ -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)")] diff --git a/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteText.cs b/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteText.cs index 9d07fd9..3197a27 100644 --- a/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteText.cs +++ b/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteText.cs @@ -198,7 +198,7 @@ public void TestLyricShaders() { karaokeSpriteText.LeftLyricTextShaders = new[] { - shaderManager.LocalInternalShader().With(s => + shaderManager.LocalCustomizedShader().With(s => { s.Radius = 3; s.Colour = Color4Extensions.FromHex("#FFDD77"); @@ -207,7 +207,7 @@ public void TestLyricShaders() }; karaokeSpriteText.RightLyricTextShaders = new[] { - shaderManager.LocalInternalShader().With(s => + shaderManager.LocalCustomizedShader().With(s => { s.Radius = 3; s.Colour = Color4Extensions.FromHex("#AA88FF"); diff --git a/osu.Framework.Font/Extensions/ShaderManagerExtensions.cs b/osu.Framework.Font/Extensions/ShaderManagerExtensions.cs index ddf0d8b..532183c 100644 --- a/osu.Framework.Font/Extensions/ShaderManagerExtensions.cs +++ b/osu.Framework.Font/Extensions/ShaderManagerExtensions.cs @@ -7,16 +7,16 @@ namespace osu.Framework.Extensions; public static class ShaderManagerExtensions { - public static T LocalInternalShader(this ShaderManager shaderManager) where T : InternalShader, new() + public static T LocalCustomizedShader(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(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); } } diff --git a/osu.Framework.Font/Graphics/CustomizedShaderBufferedDrawNode.cs b/osu.Framework.Font/Graphics/CustomizedShaderBufferedDrawNode.cs index 65f0bf7..b609c9f 100644 --- a/osu.Framework.Font/Graphics/CustomizedShaderBufferedDrawNode.cs +++ b/osu.Framework.Font/Graphics/CustomizedShaderBufferedDrawNode.cs @@ -50,30 +50,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(@"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(@"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("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(); } diff --git a/osu.Framework.Font/Graphics/Shaders/CustomizedShader.cs b/osu.Framework.Font/Graphics/Shaders/CustomizedShader.cs index 0872208..67b7314 100644 --- a/osu.Framework.Font/Graphics/Shaders/CustomizedShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/CustomizedShader.cs @@ -3,15 +3,31 @@ using System; +using osu.Framework.Graphics.Rendering; + namespace osu.Framework.Graphics.Shaders; /// /// Shader with customized property /// -public abstract class CustomizedShader : ICustomizedShader +public abstract class CustomizedShader : ICustomizedShader + where TUniform : unmanaged, IEquatable { private IShader shader = null!; + public abstract string ShaderName { get; } + + private IUniformBuffer? uniformBuffer; + + protected IUniformBuffer UniformBuffer => uniformBuffer!; + + protected IShader OriginShader => shader; + + public void PrepareUniforms(IRenderer renderer) + { + uniformBuffer ??= renderer.CreateUniformBuffer(); + } + public void AttachOriginShader(IShader originShader) { shader = originShader ?? throw new ArgumentNullException(nameof(originShader)); @@ -21,9 +37,6 @@ public void AttachOriginShader(IShader originShader) public void Unbind() => shader.Unbind(); - public Uniform GetUniform(string name) where T : unmanaged, IEquatable - => shader.GetUniform(name); - public bool IsLoaded => shader.IsLoaded; public bool IsBound { get; private set; } diff --git a/osu.Framework.Font/Graphics/Shaders/ICustomizedShader.cs b/osu.Framework.Font/Graphics/Shaders/ICustomizedShader.cs index e8ef34d..cc580f9 100644 --- a/osu.Framework.Font/Graphics/Shaders/ICustomizedShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/ICustomizedShader.cs @@ -3,12 +3,29 @@ using System; +using osu.Framework.Graphics.Rendering; + namespace osu.Framework.Graphics.Shaders; public interface ICustomizedShader { + /// + /// Fragment shader name to load (`sh_` prefix is implicitly added) + /// + string ShaderName { get; } + void ApplyValue(); + /// + /// Make sure shader uniforms are ready for initialization and upload. + /// + void PrepareUniforms(IRenderer renderer); + + /// + /// Sets the original shader that is going to be uniform-tweaked. + /// + void AttachOriginShader(IShader originShader); + /// /// Binds this shader to be used for rendering. /// @@ -28,12 +45,4 @@ public interface ICustomizedShader /// Whether this shader is currently bound. /// bool IsBound { get; } - - /// - /// Retrieves a uniform from the shader. - /// - /// The name of the uniform. - /// The retrieved uniform. - Uniform GetUniform(string name) - where T : unmanaged, IEquatable; } diff --git a/osu.Framework.Font/Graphics/Shaders/IHasCurrentTime.cs b/osu.Framework.Font/Graphics/Shaders/IHasCurrentTime.cs index 5c50b55..9596ee1 100644 --- a/osu.Framework.Font/Graphics/Shaders/IHasCurrentTime.cs +++ b/osu.Framework.Font/Graphics/Shaders/IHasCurrentTime.cs @@ -5,4 +5,5 @@ namespace osu.Framework.Graphics.Shaders; public interface IHasCurrentTime { + float CurrentTime { set; } } diff --git a/osu.Framework.Font/Graphics/Shaders/IHasInflationPercentage.cs b/osu.Framework.Font/Graphics/Shaders/IHasInflationPercentage.cs index 1fd26cd..e31aae4 100644 --- a/osu.Framework.Font/Graphics/Shaders/IHasInflationPercentage.cs +++ b/osu.Framework.Font/Graphics/Shaders/IHasInflationPercentage.cs @@ -5,4 +5,5 @@ namespace osu.Framework.Graphics.Shaders; public interface IHasInflationPercentage { + float InflationPercentage { set; } } diff --git a/osu.Framework.Font/Graphics/Shaders/IHasTextureSize.cs b/osu.Framework.Font/Graphics/Shaders/IHasTextureSize.cs index e79b0eb..e00a073 100644 --- a/osu.Framework.Font/Graphics/Shaders/IHasTextureSize.cs +++ b/osu.Framework.Font/Graphics/Shaders/IHasTextureSize.cs @@ -1,8 +1,11 @@ // Copyright (c) karaoke.dev . 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; } } diff --git a/osu.Framework.Font/Graphics/Shaders/InternalShader.cs b/osu.Framework.Font/Graphics/Shaders/InternalShader.cs deleted file mode 100644 index d273993..0000000 --- a/osu.Framework.Font/Graphics/Shaders/InternalShader.cs +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright (c) karaoke.dev . Licensed under the MIT Licence. -// See the LICENCE file in the repository root for full licence text. - -namespace osu.Framework.Graphics.Shaders; - -public abstract class InternalShader : CustomizedShader -{ - public abstract string ShaderName { get; } -} diff --git a/osu.Framework.Font/Graphics/Shaders/OutlineShader.cs b/osu.Framework.Font/Graphics/Shaders/OutlineShader.cs index 50bac34..2d51957 100644 --- a/osu.Framework.Font/Graphics/Shaders/OutlineShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/OutlineShader.cs @@ -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, 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(@"g_Colour").UpdateValue(ref colourMatrix); - - float radius = Radius; - GetUniform(@"g_Radius").UpdateValue(ref radius); - - var outlineColourMatrix = new Vector4(OutlineColour.R, OutlineColour.G, OutlineColour.B, OutlineColour.A); - GetUniform(@"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) diff --git a/osu.Framework.Font/Graphics/Shaders/PixelShader.cs b/osu.Framework.Font/Graphics/Shaders/PixelShader.cs index 784eeb3..592a87f 100644 --- a/osu.Framework.Font/Graphics/Shaders/PixelShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/PixelShader.cs @@ -1,19 +1,41 @@ // Copyright (c) karaoke.dev . Licensed under the MIT Licence. // 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; namespace osu.Framework.Graphics.Shaders; -public class PixelShader : InternalShader, IHasTextureSize, IHasInflationPercentage +[StructLayout(LayoutKind.Sequential, Pack = 1)] +public record struct PixelShaderParameters +{ + public UniformVector2 TexSize; + public UniformVector2 Size; + public UniformFloat InflationPercentage; +} + +public class PixelShader : CustomizedShader, IHasTextureSize, IHasInflationPercentage { public override string ShaderName => "Pixel"; + public Vector2 TextureSize { get; set; } + public Vector2 Size { get; set; } = new(5); + public float InflationPercentage { get; set; } + public override void ApplyValue() { - var size = Size; - GetUniform(@"g_Size").UpdateValue(ref size); + UniformBuffer.Data = new PixelShaderParameters + { + TexSize = TextureSize, + Size = Size, + InflationPercentage = InflationPercentage + }; + + OriginShader.BindUniformBlock("m_PixelParameters", UniformBuffer); } } diff --git a/osu.Framework.Font/Graphics/Shaders/RainbowShader.cs b/osu.Framework.Font/Graphics/Shaders/RainbowShader.cs index 8512bac..afaf447 100644 --- a/osu.Framework.Font/Graphics/Shaders/RainbowShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/RainbowShader.cs @@ -3,9 +3,26 @@ using osuTK; +using System; +using System.Runtime.InteropServices; +using osu.Framework.Graphics.Primitives; +using osu.Framework.Graphics.Shaders.Types; + namespace osu.Framework.Graphics.Shaders; -public class RainbowShader : InternalShader, IHasCurrentTime +[StructLayout(LayoutKind.Sequential, Pack = 1)] +public record struct RainbowParameters +{ + public UniformVector2 Uv; + public UniformFloat Speed; + public UniformFloat Time; + public UniformFloat Saturation; + public UniformFloat Brightness; + public UniformFloat Section; + public UniformFloat Mix; +} + +public class RainbowShader : CustomizedShader, IHasCurrentTime { public override string ShaderName => "Rainbow"; @@ -13,6 +30,8 @@ public class RainbowShader : InternalShader, IHasCurrentTime public float Speed { get; set; } = 1; + public float CurrentTime { get; set; } + public float Saturation { get; set; } = 0.5f; public float Brightness { get; set; } = 1f; @@ -24,22 +43,17 @@ public class RainbowShader : InternalShader, IHasCurrentTime public override void ApplyValue() { - var uv = Uv; - GetUniform(@"g_Uv").UpdateValue(ref uv); - - var speed = Speed; - GetUniform(@"g_Speed").UpdateValue(ref speed); - - var saturation = Saturation; - GetUniform(@"g_Saturation").UpdateValue(ref saturation); - - var brightness = Brightness; - GetUniform(@"g_Brightness").UpdateValue(ref brightness); - - var section = Section; - GetUniform(@"g_Section").UpdateValue(ref section); - - var mix = Mix; - GetUniform(@"g_Mix").UpdateValue(ref mix); + UniformBuffer.Data = new RainbowParameters + { + Uv = Uv, + Speed = Speed, + Time = CurrentTime, + Saturation = Saturation, + Brightness = Brightness, + Section = Section, + Mix = Mix + }; + + OriginShader.BindUniformBlock("m_RainbowParameters", UniformBuffer); } } diff --git a/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs index 83f8072..acb8478 100644 --- a/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs @@ -2,24 +2,43 @@ // See the LICENCE file in the repository root for full licence text. using System; +using System.Runtime.InteropServices; using osu.Framework.Graphics.Textures; +using osu.Framework.Graphics.Shaders.Types; using osuTK; using osuTK.Graphics.ES30; namespace osu.Framework.Graphics.Shaders; -public class RepeatMovingBackgroundShader : InternalShader, IHasCurrentTime, IHasTextureSize +[StructLayout(LayoutKind.Sequential, Pack = 1)] +public record struct RepeatMovingBackgroundParameters +{ + public UniformVector2 TexSize; + public UniformVector2 RepeatSampleCoord; + public UniformVector2 RepeatSampleSize; + public UniformVector2 DisplaySize; + public UniformVector2 DisplayBorder; + public UniformVector2 Speed; + public UniformFloat Time; + public UniformFloat Mix; +} + +public class RepeatMovingBackgroundShader : CustomizedShader, IHasCurrentTime, IHasTextureSize { public override string ShaderName => "RepeatMovingBackground"; public Texture? Texture { get; set; } + public Vector2 TextureSize { get; set; } + public Vector2 TextureDisplaySize { get; set; } = new(10); public Vector2 TextureDisplayBorder { get; set; } public Vector2 Speed { get; set; } + public float CurrentTime { get; set; } + public float Mix { get; set; } = 1f; public override void ApplyValue() @@ -27,27 +46,21 @@ public override void ApplyValue() if (Texture == null) return; + // where this 1 is from? I have no clue. Texture.Bind(1); - var unitId = TextureUnit.Texture1 - TextureUnit.Texture0; - GetUniform(@"g_RepeatSample").UpdateValue(ref unitId); - - var textureCoord = Texture.GetTextureRect().TopLeft; - GetUniform(@"g_RepeatSampleCoord").UpdateValue(ref textureCoord); - - var textureSize = Texture.GetTextureRect().Size; - GetUniform(@"g_RepeatSampleSize").UpdateValue(ref textureSize); - - var textureDisplaySize = TextureDisplaySize; - GetUniform("g_DisplaySize").UpdateValue(ref textureDisplaySize); - - var textureDisplayBorder = TextureDisplayBorder; - GetUniform("g_DisplayBorder").UpdateValue(ref textureDisplayBorder); - - var speed = Speed; - GetUniform("g_Speed").UpdateValue(ref speed); - - var mix = Math.Clamp(Mix, 0, 1); - GetUniform(@"g_Mix").UpdateValue(ref mix); + UniformBuffer.Data = new RepeatMovingBackgroundParameters + { + TexSize = TextureSize, + RepeatSampleCoord = Texture.GetTextureRect().TopLeft, + RepeatSampleSize = Texture.GetTextureRect().Size, + DisplaySize = TextureDisplaySize, + DisplayBorder = TextureDisplayBorder, + Speed = Speed, + Time = CurrentTime, + Mix = Math.Clamp(Mix, 0, 1), + }; + + OriginShader.BindUniformBlock("m_RepeatMovingBackgroundParameters", UniformBuffer); } } diff --git a/osu.Framework.Font/Graphics/Shaders/ShadowShader.cs b/osu.Framework.Font/Graphics/Shaders/ShadowShader.cs index a04ae66..949115f 100644 --- a/osu.Framework.Font/Graphics/Shaders/ShadowShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/ShadowShader.cs @@ -2,27 +2,47 @@ // 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 ShadowShader : InternalShader, IApplicableToDrawRectangle, IHasTextureSize, IHasInflationPercentage +[StructLayout(LayoutKind.Sequential, Pack = 1)] +public record struct ShadowParameters +{ + public UniformVector4 Colour; + public UniformVector2 TexSize; + public UniformVector2 Offset; + public UniformFloat InflationPercentage; + private readonly UniformPadding12 pad1; +} + +public class ShadowShader : CustomizedShader, IApplicableToDrawRectangle, IHasTextureSize, IHasInflationPercentage { public override string ShaderName => "Shadow"; + public Vector2 TextureSize { get; set; } + public Color4 ShadowColour { get; set; } public Vector2 ShadowOffset { get; set; } + public float InflationPercentage { get; set; } + public override void ApplyValue() { - var shadowColour = new Vector4(ShadowColour.R, ShadowColour.G, ShadowColour.B, ShadowColour.A); - GetUniform(@"g_Colour").UpdateValue(ref shadowColour); + UniformBuffer.Data = new ShadowParameters + { + TexSize = TextureSize, + Colour = new Vector4(ShadowColour.R, ShadowColour.G, ShadowColour.B, ShadowColour.A), + Offset = ShadowOffset, + InflationPercentage = InflationPercentage + }; - var shadowOffset = new Vector2(-ShadowOffset.X, ShadowOffset.Y); - GetUniform(@"g_Offset").UpdateValue(ref shadowOffset); + OriginShader.BindUniformBlock("m_ShadowParameters", UniformBuffer); } public RectangleF ComputeDrawRectangle(RectangleF originDrawRectangle) diff --git a/osu.Framework.Font/Graphics/Shaders/StepShader.cs b/osu.Framework.Font/Graphics/Shaders/StepShader.cs index 4f7493f..980877d 100644 --- a/osu.Framework.Font/Graphics/Shaders/StepShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/StepShader.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using osu.Framework.Graphics.Primitives; +using osu.Framework.Graphics.Rendering; namespace osu.Framework.Graphics.Shaders; @@ -12,6 +13,9 @@ public class StepShader : IStepShader, IApplicableToCharacterSize, IApplicableTo { public string Name { get; set; } = null!; + // TODO: unused, remove this variable (IStepShader should probably not inherit from ICustomizedShader) + public string ShaderName => throw new NotSupportedException(); + public ICustomizedShader? FromShader { get; set; } private readonly List shaders = new(); @@ -31,13 +35,13 @@ public IReadOnlyList StepShaders public bool Draw { get; set; } = true; - public void Bind() + public void PrepareUniforms(IRenderer renderer) => throw new NotSupportedException(); - public void Unbind() + public void Bind() => throw new NotSupportedException(); - public Uniform GetUniform(string name) where T : unmanaged, IEquatable + public void Unbind() => throw new NotSupportedException(); public bool IsLoaded @@ -45,6 +49,9 @@ public bool IsLoaded public bool IsBound { get; private set; } + public void AttachOriginShader(IShader renderer) + => throw new NotSupportedException(); + public void ApplyValue() => throw new NotSupportedException(); diff --git a/osu.Framework.Font/Resources/Shaders/sh_Outline.fs b/osu.Framework.Font/Resources/Shaders/sh_Outline.fs index efab411..4228911 100644 --- a/osu.Framework.Font/Resources/Shaders/sh_Outline.fs +++ b/osu.Framework.Font/Resources/Shaders/sh_Outline.fs @@ -1,66 +1,77 @@ #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 = 0, binding = 0) uniform m_OutlineParameters +{ + mediump vec4 g_Colour; + mediump vec4 g_OutlineColour; + mediump vec2 g_TexSize; + mediump float g_Radius; + mediump float g_InflationPercentage; +}; + +layout(set = 1, binding = 0) uniform lowp texture2D m_Texture; +layout(set = 1, binding = 1) uniform lowp sampler m_Sampler; + +layout(location = 0) out vec4 o_Colour; -uniform mediump vec2 g_TexSize; -uniform vec4 g_Colour; -uniform float g_Radius; -uniform vec4 g_OutlineColour; -uniform float g_InflationPercentage; +mediump vec4 tex(in mediump vec2 texCoord) +{ + return texture(sampler2D(m_Texture, m_Sampler), texCoord); +} -lowp float outlineAlpha(sampler2D tex, float radius, mediump vec2 texCoord, mediump vec2 texSize) +lowp float outlineAlpha(in float radius, in mediump vec2 texCoord, in mediump vec2 texSize) { - mediump vec2 offset = mediump vec2(radius) / texSize; - lowp float alpha = 0.0; + mediump vec2 offset = vec2(radius) / texSize; + lowp float alpha = 0.0; - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(0.20, 0.98) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(0.38, 0.92) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(0.56, 0.83) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(0.71, 0.71) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(0.83, 0.56) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(0.92, 0.38) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(0.98, 0.20) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(1.00, -0.00) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(0.98, -0.20) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(0.92, -0.38) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(0.83, -0.56) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(0.71, -0.71) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(0.56, -0.83) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(0.38, -0.92) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(0.20, -0.98) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-0.00, -1.00) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-0.20, -0.98) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-0.38, -0.92) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-0.56, -0.83) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-0.71, -0.71) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-0.83, -0.56) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-0.92, -0.38) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-0.98, -0.20) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-1.00, 0.00) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-0.98, 0.20) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-0.92, 0.38) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-0.83, 0.56) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-0.71, 0.71) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-0.56, 0.83) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-0.38, 0.92) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(-0.20, 0.98) * offset).a); - alpha = max(alpha, texture2D(tex, texCoord - lowp vec2(0.00, 1.00) * offset).a); - return alpha; + alpha = max(alpha, tex(texCoord - vec2(0.20, 0.98) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(0.38, 0.92) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(0.56, 0.83) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(0.71, 0.71) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(0.83, 0.56) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(0.92, 0.38) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(0.98, 0.20) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(1.00, -0.00) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(0.98, -0.20) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(0.92, -0.38) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(0.83, -0.56) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(0.71, -0.71) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(0.56, -0.83) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(0.38, -0.92) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(0.20, -0.98) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-0.00, -1.00) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-0.20, -0.98) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-0.38, -0.92) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-0.56, -0.83) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-0.71, -0.71) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-0.83, -0.56) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-0.92, -0.38) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-0.98, -0.20) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-1.00, 0.00) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-0.98, 0.20) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-0.92, 0.38) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-0.83, 0.56) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-0.71, 0.71) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-0.56, 0.83) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-0.38, 0.92) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(-0.20, 0.98) * offset).a); + alpha = max(alpha, tex(texCoord - vec2(0.00, 1.00) * offset).a); + return alpha; } -lowp vec4 outline(sampler2D tex, float radius, mediump vec2 texCoord, mediump vec2 texSize, mediump vec4 colour) +lowp vec4 outline(float radius, mediump vec2 texCoord, mediump vec2 texSize, mediump vec4 colour) { - lowp float outlineAlpha = max(outlineAlpha(tex, radius, texCoord, texSize), outlineAlpha(tex, radius / 2.0, texCoord, texSize)); - return mix(vec4(0.0), colour, outlineAlpha); + lowp float res = max(outlineAlpha(radius, texCoord, texSize), outlineAlpha(radius / 2.0, texCoord, texSize)); + return mix(vec4(0.0), colour, res); } void main(void) { - lowp vec4 sample = toSRGB(texture2D(m_Sampler, v_TexCoord)); - lowp vec4 originColur = vec4(mix(sample.rgb, g_Colour.rgb, g_Colour.a), sample.a); - lowp vec4 outlineColour = outline(m_Sampler, g_Radius * g_InflationPercentage, v_TexCoord, g_TexSize, g_OutlineColour); + lowp vec4 texColour = toSRGB(texture(sampler2D(m_Texture, m_Sampler), v_TexCoord)); + lowp vec4 originColour = vec4(mix(texColour.rgb, g_Colour.rgb, g_Colour.a), texColour.a); + lowp vec4 outlineColour = outline(g_Radius * g_InflationPercentage, v_TexCoord, g_TexSize, g_OutlineColour); - gl_FragColor = mix(outlineColour, originColur, originColur.a); -} \ No newline at end of file + o_Colour = mix(outlineColour, originColour, originColour.a); +} diff --git a/osu.Framework.Font/Resources/Shaders/sh_Pixel.fs b/osu.Framework.Font/Resources/Shaders/sh_Pixel.fs index 5ca9f70..ada9f32 100644 --- a/osu.Framework.Font/Resources/Shaders/sh_Pixel.fs +++ b/osu.Framework.Font/Resources/Shaders/sh_Pixel.fs @@ -1,14 +1,19 @@ // see the demo: https://www.geeks3d.com/20101029/shader-library-pixelation-post-processing-effect-glsl/ #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 = 0, binding = 0) uniform m_PixelParameters +{ + mediump vec2 g_TexSize; + mediump vec2 g_Size; + mediump float g_InflationPercentage; +}; -uniform mediump vec2 g_TexSize; -uniform mediump vec2 g_Size; -uniform float g_InflationPercentage; +layout(set = 1, binding = 0) uniform lowp texture2D m_Texture; +layout(set = 1, binding = 1) uniform lowp sampler m_Sampler; +layout(location = 0) out vec4 o_Colour; void main(void) { @@ -17,5 +22,5 @@ void main(void) uv = uv * separaorParts; uv = floor(uv); uv = uv / separaorParts; - gl_FragColor = toSRGB(texture2D(m_Sampler, uv)); + o_Colour = toSRGB(texture(sampler2D(m_Texture, m_Sampler), uv)); } \ No newline at end of file diff --git a/osu.Framework.Font/Resources/Shaders/sh_Rainbow.fs b/osu.Framework.Font/Resources/Shaders/sh_Rainbow.fs index 909c25f..8e639c7 100644 --- a/osu.Framework.Font/Resources/Shaders/sh_Rainbow.fs +++ b/osu.Framework.Font/Resources/Shaders/sh_Rainbow.fs @@ -1,26 +1,31 @@ // see the demo: https://developer.amazon.com/es/blogs/appstore/post/acefafad-29ba-4f31-8dae-00805fda3f58/intro-to-shaders-and-surfaces-with-gamemaker-studio-2 #include "sh_Utils.h" -varying vec2 v_TexCoord; -varying vec4 v_Colour; +layout(location = 2) in highp vec2 v_TexCoord; -uniform lowp sampler2D m_Sampler; +layout(std140, set = 0, binding = 0) uniform m_RainbowParameters +{ + highp vec2 g_Uv; + mediump float g_Speed; + mediump float g_Time; + mediump float g_Saturation; + mediump float g_Brightness; + mediump float g_Section; + mediump float g_Mix; +}; + +layout(set = 1, binding = 0) uniform lowp texture2D m_Texture; +layout(set = 1, binding = 1) uniform lowp sampler m_Sampler; -uniform vec2 g_Uv; -uniform float g_Speed; -uniform float g_Time; -uniform float g_Saturation; -uniform float g_Brightness; -uniform float g_Section; -uniform float g_Mix; +layout(location = 0) out vec4 o_Colour; void main(void) { - float pos = (v_TexCoord.y - g_Uv[0]) / (g_Uv[1] - g_Uv[0]); - vec4 texColor = toSRGB(texture2D(m_Sampler, v_TexCoord)); - - vec4 col = vec4(g_Section * ((g_Time * g_Speed) + pos), g_Saturation, g_Brightness, 1); - vec4 finalCol = mix(texColor, vec4(hsv2rgb(col).xyz, texColor.a), g_Mix); - - gl_FragColor = finalCol; + float pos = (v_TexCoord.y - g_Uv[0]) / (g_Uv[1] - g_Uv[0]); + vec4 texColor = toSRGB(texture(sampler2D(m_Texture, m_Sampler), v_TexCoord)); + + vec4 col = vec4(g_Section * ((g_Time * g_Speed) + pos), g_Saturation, g_Brightness, 1); + vec4 finalCol = mix(texColor, vec4(hsv2rgb(col).xyz, texColor.a), g_Mix); + + o_Colour = finalCol; } \ No newline at end of file diff --git a/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs b/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs index f651335..968fa35 100644 --- a/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs +++ b/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs @@ -1,40 +1,48 @@ // see : https://github.com/kiwipxl/GLSL-shaders/blob/master/repeat.glsl #include "sh_Utils.h" -varying vec2 v_TexCoord; -varying vec4 v_Colour; -varying mediump vec4 v_TexRect; - -uniform lowp sampler2D m_Sampler; - -uniform mediump vec2 g_TexSize; -uniform lowp sampler2D g_RepeatSample; -uniform vec2 g_RepeatSampleCoord; -uniform vec2 g_RepeatSampleSize; -uniform vec2 g_DisplaySize; -uniform vec2 g_DisplayBorder; -uniform vec2 g_Speed; -uniform float g_Time; -uniform float g_Mix; - -float mod(float a, int b) { - return a - (float(b) * floor(a/float(b))); -} +layout(location = 1) in lowp vec4 v_Colour; +layout(location = 2) in highp vec2 v_TexCoord; +layout(location = 3) in highp vec4 v_TexRect; + +layout(std140, set = 0, binding = 0) uniform m_RepeatMovingBackgroundParameters +{ + mediump vec2 g_TexSize; + mediump vec2 g_RepeatSampleCoord; + mediump vec2 g_RepeatSampleSize; + mediump vec2 g_DisplaySize; + mediump vec2 g_DisplayBorder; + mediump vec2 g_Speed; + mediump float g_Time; + mediump float g_Mix; +}; + +layout(set = 1, binding = 0) uniform lowp texture2D m_Texture; +layout(set = 1, binding = 1) uniform lowp sampler m_Sampler; + +layout(set = 2, binding = 0) uniform lowp texture2D m_RepeatTexture; +layout(set = 2, binding = 1) uniform lowp sampler m_RepeatSampler; + +layout(location = 0) out vec4 o_Colour; + +// highp float mod(highp float a, highp float b) { +// return a - (b * floor(a/b)); +// } void main(void) { // calculate how many times texture should be repeated. vec2 repeat = g_TexSize / (g_DisplaySize + g_DisplayBorder); // get the repeat texture coordinate. - float repeatTexCoordX = mod(v_TexCoord.x * repeat.x + g_Speed.x * g_Time, 1); - float repeatTexCoordY = mod(v_TexCoord.y * repeat.y + g_Speed.y * g_Time, 1); + float repeatTexCoordX = mod(v_TexCoord.x * repeat.x + g_Speed.x * g_Time, 1.0); + float repeatTexCoordY = mod(v_TexCoord.y * repeat.y + g_Speed.y * g_Time, 1.0); vec2 repeatTexCoord = vec2(repeatTexCoordX, repeatTexCoordY) / g_DisplaySize * (g_DisplaySize + g_DisplayBorder); // because repeat texture will be the size of 1024*1024, so should make a conversion to get the target area of the texture. vec2 fixedTexCoord = repeatTexCoord * g_RepeatSampleSize + g_RepeatSampleCoord; // get point colour from sample. - vec4 texColor = texture2D(m_Sampler, v_TexCoord); - vec4 repeatSampleColor = v_Colour * vec4(texture2D(g_RepeatSample, fixedTexCoord).xyz, texColor.a); - gl_FragColor = toSRGB(mix(texColor, repeatSampleColor, g_Mix)); + vec4 texColor = texture(sampler2D(m_Texture, m_Sampler), v_TexCoord); + vec4 repeatSampleColor = v_Colour * vec4(texture(sampler2D(m_RepeatTexture, m_RepeatSampler), fixedTexCoord).xyz, texColor.a); + o_Colour = toSRGB(mix(texColor, repeatSampleColor, g_Mix)); } diff --git a/osu.Framework.Font/Resources/Shaders/sh_Shadow.fs b/osu.Framework.Font/Resources/Shaders/sh_Shadow.fs index a82b882..0475dca 100644 --- a/osu.Framework.Font/Resources/Shaders/sh_Shadow.fs +++ b/osu.Framework.Font/Resources/Shaders/sh_Shadow.fs @@ -1,22 +1,28 @@ #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 = 0, binding = 0) uniform m_ShadowParameters +{ + mediump vec4 g_Colour; + mediump vec2 g_TexSize; + mediump vec2 g_Offset; + mediump float g_InflationPercentage; +}; + +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); } \ No newline at end of file diff --git a/osu.Framework.Font/osu.Framework.Font.csproj b/osu.Framework.Font/osu.Framework.Font.csproj index ee181f1..f2b231c 100644 --- a/osu.Framework.Font/osu.Framework.Font.csproj +++ b/osu.Framework.Font/osu.Framework.Font.csproj @@ -29,7 +29,7 @@ en - + From 3de4b625156d94b3c2fa791da204ed9ba6bc147e Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 19 Mar 2023 13:15:09 +0800 Subject: [PATCH 2/5] Remove un-need namespace using. --- .../Graphics/CustomizedShaderBufferedDrawNode.cs | 1 - osu.Framework.Font/Graphics/Shaders/ICustomizedShader.cs | 2 -- osu.Framework.Font/Graphics/Shaders/PixelShader.cs | 2 -- osu.Framework.Font/Graphics/Shaders/RainbowShader.cs | 3 --- .../Graphics/Shaders/RepeatMovingBackgroundShader.cs | 1 - 5 files changed, 9 deletions(-) diff --git a/osu.Framework.Font/Graphics/CustomizedShaderBufferedDrawNode.cs b/osu.Framework.Font/Graphics/CustomizedShaderBufferedDrawNode.cs index b609c9f..3158d68 100644 --- a/osu.Framework.Font/Graphics/CustomizedShaderBufferedDrawNode.cs +++ b/osu.Framework.Font/Graphics/CustomizedShaderBufferedDrawNode.cs @@ -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; diff --git a/osu.Framework.Font/Graphics/Shaders/ICustomizedShader.cs b/osu.Framework.Font/Graphics/Shaders/ICustomizedShader.cs index cc580f9..66872d9 100644 --- a/osu.Framework.Font/Graphics/Shaders/ICustomizedShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/ICustomizedShader.cs @@ -1,8 +1,6 @@ // Copyright (c) karaoke.dev . 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; diff --git a/osu.Framework.Font/Graphics/Shaders/PixelShader.cs b/osu.Framework.Font/Graphics/Shaders/PixelShader.cs index 592a87f..dc3511f 100644 --- a/osu.Framework.Font/Graphics/Shaders/PixelShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/PixelShader.cs @@ -1,9 +1,7 @@ // Copyright (c) karaoke.dev . Licensed under the MIT Licence. // 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; diff --git a/osu.Framework.Font/Graphics/Shaders/RainbowShader.cs b/osu.Framework.Font/Graphics/Shaders/RainbowShader.cs index afaf447..c99b5cc 100644 --- a/osu.Framework.Font/Graphics/Shaders/RainbowShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/RainbowShader.cs @@ -2,10 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using osuTK; - -using System; using System.Runtime.InteropServices; -using osu.Framework.Graphics.Primitives; using osu.Framework.Graphics.Shaders.Types; namespace osu.Framework.Graphics.Shaders; diff --git a/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs index acb8478..cec8de3 100644 --- a/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs @@ -6,7 +6,6 @@ using osu.Framework.Graphics.Textures; using osu.Framework.Graphics.Shaders.Types; using osuTK; -using osuTK.Graphics.ES30; namespace osu.Framework.Graphics.Shaders; From b5cbe8b4d15cfb47f03b2a731a81e9e91ba2e517 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 19 Mar 2023 13:18:41 +0800 Subject: [PATCH 3/5] Fix some format issue. --- osu.Framework.Font/Graphics/Shaders/CustomizedShader.cs | 1 - osu.Framework.Font/Graphics/Shaders/IHasCurrentTime.cs | 2 +- osu.Framework.Font/Graphics/Shaders/IHasInflationPercentage.cs | 2 +- osu.Framework.Font/Graphics/Shaders/IHasTextureSize.cs | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/osu.Framework.Font/Graphics/Shaders/CustomizedShader.cs b/osu.Framework.Font/Graphics/Shaders/CustomizedShader.cs index 67b7314..12eb117 100644 --- a/osu.Framework.Font/Graphics/Shaders/CustomizedShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/CustomizedShader.cs @@ -2,7 +2,6 @@ // See the LICENCE file in the repository root for full licence text. using System; - using osu.Framework.Graphics.Rendering; namespace osu.Framework.Graphics.Shaders; diff --git a/osu.Framework.Font/Graphics/Shaders/IHasCurrentTime.cs b/osu.Framework.Font/Graphics/Shaders/IHasCurrentTime.cs index 9596ee1..07c152f 100644 --- a/osu.Framework.Font/Graphics/Shaders/IHasCurrentTime.cs +++ b/osu.Framework.Font/Graphics/Shaders/IHasCurrentTime.cs @@ -5,5 +5,5 @@ namespace osu.Framework.Graphics.Shaders; public interface IHasCurrentTime { - float CurrentTime { set; } + float CurrentTime { set; } } diff --git a/osu.Framework.Font/Graphics/Shaders/IHasInflationPercentage.cs b/osu.Framework.Font/Graphics/Shaders/IHasInflationPercentage.cs index e31aae4..be24c6e 100644 --- a/osu.Framework.Font/Graphics/Shaders/IHasInflationPercentage.cs +++ b/osu.Framework.Font/Graphics/Shaders/IHasInflationPercentage.cs @@ -5,5 +5,5 @@ namespace osu.Framework.Graphics.Shaders; public interface IHasInflationPercentage { - float InflationPercentage { set; } + float InflationPercentage { set; } } diff --git a/osu.Framework.Font/Graphics/Shaders/IHasTextureSize.cs b/osu.Framework.Font/Graphics/Shaders/IHasTextureSize.cs index e00a073..40714a7 100644 --- a/osu.Framework.Font/Graphics/Shaders/IHasTextureSize.cs +++ b/osu.Framework.Font/Graphics/Shaders/IHasTextureSize.cs @@ -7,5 +7,5 @@ namespace osu.Framework.Graphics.Shaders; public interface IHasTextureSize { - Vector2 TextureSize { set; } + Vector2 TextureSize { set; } } From 1c3556de0b6aeedbcd846d090319dcdc3aec26a3 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 19 Mar 2023 13:18:49 +0800 Subject: [PATCH 4/5] Fix the test case brokwn. --- osu.Framework.Font.Tests/Visual/BackgroundGridTestScene.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Framework.Font.Tests/Visual/BackgroundGridTestScene.cs b/osu.Framework.Font.Tests/Visual/BackgroundGridTestScene.cs index 1c0b5fa..168c41d 100644 --- a/osu.Framework.Font.Tests/Visual/BackgroundGridTestScene.cs +++ b/osu.Framework.Font.Tests/Visual/BackgroundGridTestScene.cs @@ -83,7 +83,7 @@ protected BackgroundGridTestScene() }); } - protected T GetShaderByType() where T : ICustomizedShader + protected T GetShaderByType() where T : ICustomizedShader, new() => shaderManager.LocalCustomizedShader(); protected partial class DraggableCircle : Circle From 3a81d22b49093afca42770c0ef0df123976c9f6a Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 19 Mar 2023 13:23:32 +0800 Subject: [PATCH 5/5] Drawable class should be partial. --- .../Visual/Sprites/TestSceneKaraokeSpriteText.cs | 2 +- .../Visual/Sprites/TestSceneKaraokeSpriteTextTransforms.cs | 2 +- .../Visual/Sprites/TestSceneKaraokeSpriteTextWithShader.cs | 2 +- .../Visual/Sprites/TestSceneLyricSpriteText.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteText.cs b/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteText.cs index 3197a27..a3aa245 100644 --- a/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteText.cs +++ b/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteText.cs @@ -223,7 +223,7 @@ public void TestLyricShaders() }); } - private class TestKaraokeSpriteText : KaraokeSpriteText + private partial class TestKaraokeSpriteText : KaraokeSpriteText { public Action? TransformAction; diff --git a/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteTextTransforms.cs b/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteTextTransforms.cs index 10d00a8..d0b1e33 100644 --- a/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteTextTransforms.cs +++ b/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteTextTransforms.cs @@ -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; } diff --git a/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteTextWithShader.cs b/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteTextWithShader.cs index 541629e..65a18db 100644 --- a/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteTextWithShader.cs +++ b/osu.Framework.Font.Tests/Visual/Sprites/TestSceneKaraokeSpriteTextWithShader.cs @@ -107,7 +107,7 @@ public void ApplyRightLyricTextShader() }); } - private class TestKaraokeSpriteText : KaraokeSpriteText + private partial class TestKaraokeSpriteText : KaraokeSpriteText { public override bool RemoveCompletedTransforms => false; } diff --git a/osu.Framework.Font.Tests/Visual/Sprites/TestSceneLyricSpriteText.cs b/osu.Framework.Font.Tests/Visual/Sprites/TestSceneLyricSpriteText.cs index 7101288..e393875 100644 --- a/osu.Framework.Font.Tests/Visual/Sprites/TestSceneLyricSpriteText.cs +++ b/osu.Framework.Font.Tests/Visual/Sprites/TestSceneLyricSpriteText.cs @@ -230,7 +230,7 @@ private void setContents(Func 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) {