From 7bd7cae47c9ba8d0b5938212bff7cb61d842784d Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Thu, 27 Jul 2023 03:07:59 +0900 Subject: [PATCH] Move global UBO binding to Renderer implementations --- .../Shaders/TestSceneShaderDisposal.cs | 8 +++--- .../Shaders/TestShaderLoading.cs | 8 +++--- .../Graphics/OpenGL/Buffers/GLVertexBuffer.cs | 4 +-- osu.Framework/Graphics/OpenGL/GLRenderer.cs | 13 +++++++-- .../Graphics/OpenGL/Shaders/GLShader.cs | 6 +---- osu.Framework/Graphics/Rendering/Renderer.cs | 27 +++++++++---------- .../Graphics/Veldrid/Shaders/VeldridShader.cs | 6 +---- .../Graphics/Veldrid/VeldridRenderer.cs | 6 +++-- 8 files changed, 39 insertions(+), 39 deletions(-) diff --git a/osu.Framework.Tests/Shaders/TestSceneShaderDisposal.cs b/osu.Framework.Tests/Shaders/TestSceneShaderDisposal.cs index 78ecb5e99..e5f74e7a9 100644 --- a/osu.Framework.Tests/Shaders/TestSceneShaderDisposal.cs +++ b/osu.Framework.Tests/Shaders/TestSceneShaderDisposal.cs @@ -60,13 +60,13 @@ namespace osu.Framework.Tests.Shaders private class TestGLRenderer : GLRenderer { - protected override IShader CreateShader(string name, IShaderPart[] parts, IUniformBuffer globalUniformBuffer, ShaderCompilationStore compilationStore) - => new TestGLShader(this, name, parts.Cast().ToArray(), globalUniformBuffer, compilationStore); + protected override IShader CreateShader(string name, IShaderPart[] parts, ShaderCompilationStore compilationStore) + => new TestGLShader(this, name, parts.Cast().ToArray(), compilationStore); private class TestGLShader : GLShader { - internal TestGLShader(GLRenderer renderer, string name, GLShaderPart[] parts, IUniformBuffer globalUniformBuffer, ShaderCompilationStore compilationStore) - : base(renderer, name, parts, globalUniformBuffer, compilationStore) + internal TestGLShader(GLRenderer renderer, string name, GLShaderPart[] parts, ShaderCompilationStore compilationStore) + : base(renderer, name, parts, compilationStore) { } diff --git a/osu.Framework.Tests/Shaders/TestShaderLoading.cs b/osu.Framework.Tests/Shaders/TestShaderLoading.cs index e8a95c08f..f01cb1b13 100644 --- a/osu.Framework.Tests/Shaders/TestShaderLoading.cs +++ b/osu.Framework.Tests/Shaders/TestShaderLoading.cs @@ -40,13 +40,13 @@ namespace osu.Framework.Tests.Shaders private class TestGLRenderer : GLRenderer { - protected override IShader CreateShader(string name, IShaderPart[] parts, IUniformBuffer globalUniformBuffer, ShaderCompilationStore compilationStore) - => new TestGLShader(this, name, parts.Cast().ToArray(), globalUniformBuffer, compilationStore); + protected override IShader CreateShader(string name, IShaderPart[] parts, ShaderCompilationStore compilationStore) + => new TestGLShader(this, name, parts.Cast().ToArray(), compilationStore); private class TestGLShader : GLShader { - internal TestGLShader(GLRenderer renderer, string name, GLShaderPart[] parts, IUniformBuffer globalUniformBuffer, ShaderCompilationStore compilationStore) - : base(renderer, name, parts, globalUniformBuffer, compilationStore) + internal TestGLShader(GLRenderer renderer, string name, GLShaderPart[] parts, ShaderCompilationStore compilationStore) + : base(renderer, name, parts, compilationStore) { } diff --git a/osu.Framework/Graphics/OpenGL/Buffers/GLVertexBuffer.cs b/osu.Framework/Graphics/OpenGL/Buffers/GLVertexBuffer.cs index 054996910..5f5c7e15b 100644 --- a/osu.Framework/Graphics/OpenGL/Buffers/GLVertexBuffer.cs +++ b/osu.Framework/Graphics/OpenGL/Buffers/GLVertexBuffer.cs @@ -130,9 +130,7 @@ namespace osu.Framework.Graphics.OpenGL.Buffers public void DrawRange(int startIndex, int endIndex) { Bind(true); - - int countVertices = endIndex - startIndex; - GL.DrawElements(Type, ToElements(countVertices), DrawElementsType.UnsignedShort, (IntPtr)(ToElementIndex(startIndex) * sizeof(ushort))); + Renderer.DrawVertices(Type, ToElementIndex(startIndex), ToElements(endIndex - startIndex)); } public void Update() diff --git a/osu.Framework/Graphics/OpenGL/GLRenderer.cs b/osu.Framework/Graphics/OpenGL/GLRenderer.cs index 68f965de4..41878f229 100644 --- a/osu.Framework/Graphics/OpenGL/GLRenderer.cs +++ b/osu.Framework/Graphics/OpenGL/GLRenderer.cs @@ -273,6 +273,15 @@ namespace osu.Framework.Graphics.OpenGL GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit); } + public void DrawVertices(PrimitiveType type, int vertexStart, int verticesCount) + { + var glShader = (GLShader)Shader!; + + glShader.BindUniformBlock("g_GlobalUniforms", GlobalUniformBuffer!); + + GL.DrawElements(type, verticesCount, DrawElementsType.UnsignedShort, (IntPtr)(vertexStart * sizeof(ushort))); + } + protected override void SetScissorStateImplementation(bool enabled) { if (enabled) @@ -376,8 +385,8 @@ namespace osu.Framework.Graphics.OpenGL return new GLShaderPart(this, name, rawData, glType, store); } - protected override IShader CreateShader(string name, IShaderPart[] parts, IUniformBuffer globalUniformBuffer, ShaderCompilationStore compilationStore) - => new GLShader(this, name, parts.Cast().ToArray(), globalUniformBuffer, compilationStore); + protected override IShader CreateShader(string name, IShaderPart[] parts, ShaderCompilationStore compilationStore) + => new GLShader(this, name, parts.Cast().ToArray(), compilationStore); public override IFrameBuffer CreateFrameBuffer(RenderBufferFormat[]? renderBufferFormats = null, TextureFilteringMode filteringMode = TextureFilteringMode.Linear) { diff --git a/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs b/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs index 73e6bc29a..db0025619 100644 --- a/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs +++ b/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs @@ -20,7 +20,6 @@ namespace osu.Framework.Graphics.OpenGL.Shaders { private readonly GLRenderer renderer; private readonly string name; - private readonly IUniformBuffer globalUniformBuffer; private readonly GLShaderPart[] parts; private readonly ScheduledDelegate shaderCompileDelegate; @@ -42,11 +41,10 @@ namespace osu.Framework.Graphics.OpenGL.Shaders private readonly GLShaderPart fragmentPart; private readonly VertexFragmentShaderCompilation compilation; - internal GLShader(GLRenderer renderer, string name, GLShaderPart[] parts, IUniformBuffer globalUniformBuffer, ShaderCompilationStore compilationStore) + internal GLShader(GLRenderer renderer, string name, GLShaderPart[] parts, ShaderCompilationStore compilationStore) { this.renderer = renderer; this.name = name; - this.globalUniformBuffer = globalUniformBuffer; this.parts = parts; vertexPart = parts.Single(p => p.Type == ShaderType.VertexShader); @@ -88,8 +86,6 @@ namespace osu.Framework.Graphics.OpenGL.Shaders throw new ProgramLinkingFailedException(name, GetProgramLog()); IsLoaded = true; - - BindUniformBlock("g_GlobalUniforms", globalUniformBuffer); } internal void EnsureShaderCompiled() diff --git a/osu.Framework/Graphics/Rendering/Renderer.cs b/osu.Framework/Graphics/Rendering/Renderer.cs index 3ec64e5ed..a552f18cf 100644 --- a/osu.Framework/Graphics/Rendering/Renderer.cs +++ b/osu.Framework/Graphics/Rendering/Renderer.cs @@ -9,7 +9,6 @@ using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using osu.Framework.Development; -using osu.Framework.Extensions.ObjectExtensions; using osu.Framework.Extensions.TypeExtensions; using osu.Framework.Graphics.Primitives; using osu.Framework.Graphics.Rendering.Vertices; @@ -130,7 +129,7 @@ namespace osu.Framework.Graphics.Rendering private readonly Lazy whitePixel; private readonly LockedWeakList allTextures = new LockedWeakList(); - private IUniformBuffer? globalUniformBuffer; + protected IUniformBuffer? GlobalUniformBuffer { get; private set; } private IVertexBatch? defaultQuadBatch; private IVertexBatch? currentActiveBatch; private MaskingInfo currentMaskingInfo; @@ -189,8 +188,8 @@ namespace osu.Framework.Graphics.Rendering foreach (var source in flush_source_statistics) source.Value = 0; - globalUniformBuffer ??= ((IRenderer)this).CreateUniformBuffer(); - globalUniformBuffer.Data = globalUniformBuffer.Data with + GlobalUniformBuffer ??= ((IRenderer)this).CreateUniformBuffer(); + GlobalUniformBuffer.Data = GlobalUniformBuffer.Data with { IsDepthRangeZeroToOne = IsDepthRangeZeroToOne, IsClipSpaceYInverted = IsClipSpaceYInverted, @@ -606,7 +605,7 @@ namespace osu.Framework.Graphics.Rendering FlushCurrentBatch(FlushBatchSource.SetProjection); - globalUniformBuffer!.Data = globalUniformBuffer.Data with { ProjMatrix = matrix }; + GlobalUniformBuffer!.Data = GlobalUniformBuffer.Data with { ProjMatrix = matrix }; ProjectionMatrix = matrix; } @@ -635,7 +634,7 @@ namespace osu.Framework.Graphics.Rendering FlushCurrentBatch(FlushBatchSource.SetMasking); - globalUniformBuffer!.Data = globalUniformBuffer.Data with + GlobalUniformBuffer!.Data = GlobalUniformBuffer.Data with { IsMasking = IsMaskingActive, MaskingRect = new Vector4( @@ -669,14 +668,14 @@ namespace osu.Framework.Graphics.Rendering maskingInfo.BorderColour.BottomRight.SRGB.G, maskingInfo.BorderColour.BottomRight.SRGB.B, maskingInfo.BorderColour.BottomRight.SRGB.A) - : globalUniformBuffer.Data.BorderColour, + : GlobalUniformBuffer.Data.BorderColour, MaskingBlendRange = maskingInfo.BlendRange, AlphaExponent = maskingInfo.AlphaExponent, EdgeOffset = maskingInfo.EdgeOffset, DiscardInner = maskingInfo.Hollow, InnerCornerRadius = maskingInfo.Hollow ? maskingInfo.HollowCornerRadius - : globalUniformBuffer.Data.InnerCornerRadius + : GlobalUniformBuffer.Data.InnerCornerRadius }; if (isPushing) @@ -872,14 +871,14 @@ namespace osu.Framework.Graphics.Rendering if (wrapModeS != CurrentWrapModeS) { // Will flush the current batch internally. - globalUniformBuffer!.Data = globalUniformBuffer.Data with { WrapModeS = (int)wrapModeS }; + GlobalUniformBuffer!.Data = GlobalUniformBuffer.Data with { WrapModeS = (int)wrapModeS }; CurrentWrapModeS = wrapModeS; } if (wrapModeT != CurrentWrapModeT) { // Will flush the current batch internally. - globalUniformBuffer!.Data = globalUniformBuffer.Data with { WrapModeT = (int)wrapModeT }; + GlobalUniformBuffer!.Data = GlobalUniformBuffer.Data with { WrapModeT = (int)wrapModeT }; CurrentWrapModeT = wrapModeT; } @@ -957,7 +956,7 @@ namespace osu.Framework.Graphics.Rendering SetFrameBufferImplementation(frameBuffer); - globalUniformBuffer!.Data = globalUniformBuffer.Data with { BackbufferDraw = UsingBackbuffer }; + GlobalUniformBuffer!.Data = GlobalUniformBuffer.Data with { BackbufferDraw = UsingBackbuffer }; FrameBuffer = frameBuffer; } @@ -1042,7 +1041,7 @@ namespace osu.Framework.Graphics.Rendering protected abstract IShaderPart CreateShaderPart(IShaderStore store, string name, byte[]? rawData, ShaderPartType partType); /// - protected abstract IShader CreateShader(string name, IShaderPart[] parts, IUniformBuffer globalUniformBuffer, ShaderCompilationStore compilationStore); + protected abstract IShader CreateShader(string name, IShaderPart[] parts, ShaderCompilationStore compilationStore); private IShader? mipmapShader; @@ -1062,7 +1061,7 @@ namespace osu.Framework.Graphics.Rendering { CreateShaderPart(store, "mipmap.vs", store.GetRawData("sh_mipmap.vs"), ShaderPartType.Vertex), CreateShaderPart(store, "mipmap.fs", store.GetRawData("sh_mipmap.fs"), ShaderPartType.Fragment), - }, globalUniformBuffer.AsNonNull(), shaderCompilationStore); + }, shaderCompilationStore); return mipmapShader; } @@ -1154,7 +1153,7 @@ namespace osu.Framework.Graphics.Rendering void IRenderer.PopQuadBatch() => PopQuadBatch(); Image IRenderer.TakeScreenshot() => TakeScreenshot(); IShaderPart IRenderer.CreateShaderPart(IShaderStore store, string name, byte[]? rawData, ShaderPartType partType) => CreateShaderPart(store, name, rawData, partType); - IShader IRenderer.CreateShader(string name, IShaderPart[] parts) => CreateShader(name, parts, globalUniformBuffer!, shaderCompilationStore); + IShader IRenderer.CreateShader(string name, IShaderPart[] parts) => CreateShader(name, parts, shaderCompilationStore); IVertexBatch IRenderer.CreateLinearBatch(int size, int maxBuffers, PrimitiveTopology topology) { diff --git a/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs b/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs index a62747a3d..bf0ab101c 100644 --- a/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs +++ b/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs @@ -22,7 +22,6 @@ namespace osu.Framework.Graphics.Veldrid.Shaders { private readonly string name; private readonly VeldridShaderPart[] parts; - private readonly IUniformBuffer globalUniformBuffer; private readonly ShaderCompilationStore compilationStore; private readonly VeldridRenderer renderer; @@ -43,11 +42,10 @@ namespace osu.Framework.Graphics.Veldrid.Shaders private readonly Dictionary uniformLayouts = new Dictionary(); private readonly List textureLayouts = new List(); - public VeldridShader(VeldridRenderer renderer, string name, VeldridShaderPart[] parts, IUniformBuffer globalUniformBuffer, ShaderCompilationStore compilationStore) + public VeldridShader(VeldridRenderer renderer, string name, VeldridShaderPart[] parts, ShaderCompilationStore compilationStore) { this.name = name; this.parts = parts; - this.globalUniformBuffer = globalUniformBuffer; this.compilationStore = compilationStore; this.renderer = renderer; @@ -210,8 +208,6 @@ namespace osu.Framework.Graphics.Veldrid.Shaders renderer.Factory.CreateShader(vertexShaderDescription), renderer.Factory.CreateShader(fragmentShaderDescription) }; - - BindUniformBlock("g_GlobalUniforms", globalUniformBuffer); } private bool isDisposed; diff --git a/osu.Framework/Graphics/Veldrid/VeldridRenderer.cs b/osu.Framework/Graphics/Veldrid/VeldridRenderer.cs index 33662cc10..14b339376 100644 --- a/osu.Framework/Graphics/Veldrid/VeldridRenderer.cs +++ b/osu.Framework/Graphics/Veldrid/VeldridRenderer.cs @@ -539,6 +539,8 @@ namespace osu.Framework.Graphics.Veldrid var veldridShader = (VeldridShader)Shader!; + veldridShader.BindUniformBlock("g_GlobalUniforms", GlobalUniformBuffer!); + pipeline.PrimitiveTopology = type; Array.Resize(ref pipeline.ResourceLayouts, veldridShader.LayoutCount); @@ -730,8 +732,8 @@ namespace osu.Framework.Graphics.Veldrid protected override IShaderPart CreateShaderPart(IShaderStore store, string name, byte[]? rawData, ShaderPartType partType) => new VeldridShaderPart(rawData, partType, store); - protected override IShader CreateShader(string name, IShaderPart[] parts, IUniformBuffer globalUniformBuffer, ShaderCompilationStore compilationStore) - => new VeldridShader(this, name, parts.Cast().ToArray(), globalUniformBuffer, compilationStore); + protected override IShader CreateShader(string name, IShaderPart[] parts, ShaderCompilationStore compilationStore) + => new VeldridShader(this, name, parts.Cast().ToArray(), compilationStore); public override IFrameBuffer CreateFrameBuffer(RenderBufferFormat[]? renderBufferFormats = null, TextureFilteringMode filteringMode = TextureFilteringMode.Linear) => new VeldridFrameBuffer(this, renderBufferFormats?.ToPixelFormats(), filteringMode.ToSamplerFilter());