From 173718d91b1e6d0e02ea7a4e63759472028aa9a9 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 19 Mar 2023 11:07:07 +0800 Subject: [PATCH] Fix the rainbow shader. Co-authored-by: WebFreak001 --- .../Graphics/Shaders/RainbowShader.cs | 44 ++++++++++++------- .../Resources/Shaders/sh_Rainbow.fs | 39 +++++++++------- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/osu.Framework.Font/Graphics/Shaders/RainbowShader.cs b/osu.Framework.Font/Graphics/Shaders/RainbowShader.cs index 66460be..2d7e123 100644 --- a/osu.Framework.Font/Graphics/Shaders/RainbowShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/RainbowShader.cs @@ -1,7 +1,9 @@ // Copyright (c) karaoke.dev . 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; @@ -23,24 +25,34 @@ public class RainbowShader : InternalShader, IHasCurrentTime public float Mix { get; set; } = 0.5f; + private IUniformBuffer? rainbowParametersBuffer; + public override void ApplyValue(IRenderer renderer) { - 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); + rainbowParametersBuffer ??= renderer.CreateUniformBuffer(); + + rainbowParametersBuffer.Data = new RainbowParameters + { + Uv = Uv, + Speed = Speed, + Saturation = Saturation, + Brightness = Brightness, + Section = Section, + Mix = Mix, + }; + + BindUniformBlock("m_RainbowParameters", rainbowParametersBuffer); + } - var mix = Mix; - GetUniform(@"g_Mix").UpdateValue(ref mix); + [StructLayout(LayoutKind.Sequential, Pack = 1)] + private record struct RainbowParameters + { + public UniformVector2 Uv; + public UniformFloat Speed; + public UniformFloat Saturation; + public UniformFloat Brightness; + public UniformFloat Section; + public UniformFloat Mix; + private readonly UniformPadding4 pad1; } } diff --git a/osu.Framework.Font/Resources/Shaders/sh_Rainbow.fs b/osu.Framework.Font/Resources/Shaders/sh_Rainbow.fs index 909c25f..892abea 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_CustomizedShaderGlobalUniforms.h" #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 = 2, binding = 0) uniform m_RainbowParameters +{ + highp vec2 g_Uv; + mediump float g_Speed; + 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