Skip to content

Commit

Permalink
Merge pull request #5949 from smoogipoo/refactor-uniform-binding
Browse files Browse the repository at this point in the history
Refactor global UBO binding
  • Loading branch information
peppy authored Jul 28, 2023
2 parents 74fdbb5 + 8a4d45c commit e40d447
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 39 deletions.
8 changes: 4 additions & 4 deletions osu.Framework.Tests/Shaders/TestSceneShaderDisposal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ public void TestShadersLoseReferencesOnManagerDisposal()

private class TestGLRenderer : GLRenderer
{
protected override IShader CreateShader(string name, IShaderPart[] parts, IUniformBuffer<GlobalUniformData> globalUniformBuffer, ShaderCompilationStore compilationStore)
=> new TestGLShader(this, name, parts.Cast<GLShaderPart>().ToArray(), globalUniformBuffer, compilationStore);
protected override IShader CreateShader(string name, IShaderPart[] parts, ShaderCompilationStore compilationStore)
=> new TestGLShader(this, name, parts.Cast<GLShaderPart>().ToArray(), compilationStore);

private class TestGLShader : GLShader
{
internal TestGLShader(GLRenderer renderer, string name, GLShaderPart[] parts, IUniformBuffer<GlobalUniformData> globalUniformBuffer, ShaderCompilationStore compilationStore)
: base(renderer, name, parts, globalUniformBuffer, compilationStore)
internal TestGLShader(GLRenderer renderer, string name, GLShaderPart[] parts, ShaderCompilationStore compilationStore)
: base(renderer, name, parts, compilationStore)
{
}

Expand Down
8 changes: 4 additions & 4 deletions osu.Framework.Tests/Shaders/TestShaderLoading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public void TestFetchExistentShader()

private class TestGLRenderer : GLRenderer
{
protected override IShader CreateShader(string name, IShaderPart[] parts, IUniformBuffer<GlobalUniformData> globalUniformBuffer, ShaderCompilationStore compilationStore)
=> new TestGLShader(this, name, parts.Cast<GLShaderPart>().ToArray(), globalUniformBuffer, compilationStore);
protected override IShader CreateShader(string name, IShaderPart[] parts, ShaderCompilationStore compilationStore)
=> new TestGLShader(this, name, parts.Cast<GLShaderPart>().ToArray(), compilationStore);

private class TestGLShader : GLShader
{
internal TestGLShader(GLRenderer renderer, string name, GLShaderPart[] parts, IUniformBuffer<GlobalUniformData> globalUniformBuffer, ShaderCompilationStore compilationStore)
: base(renderer, name, parts, globalUniformBuffer, compilationStore)
internal TestGLShader(GLRenderer renderer, string name, GLShaderPart[] parts, ShaderCompilationStore compilationStore)
: base(renderer, name, parts, compilationStore)
{
}

Expand Down
4 changes: 1 addition & 3 deletions osu.Framework/Graphics/OpenGL/Buffers/GLVertexBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ public void Draw()
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()
Expand Down
13 changes: 11 additions & 2 deletions osu.Framework/Graphics/OpenGL/GLRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@ protected override void ClearImplementation(ClearInfo clearInfo)
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)
Expand Down Expand Up @@ -376,8 +385,8 @@ protected override IShaderPart CreateShaderPart(IShaderStore store, string name,
return new GLShaderPart(this, name, rawData, glType, store);
}

protected override IShader CreateShader(string name, IShaderPart[] parts, IUniformBuffer<GlobalUniformData> globalUniformBuffer, ShaderCompilationStore compilationStore)
=> new GLShader(this, name, parts.Cast<GLShaderPart>().ToArray(), globalUniformBuffer, compilationStore);
protected override IShader CreateShader(string name, IShaderPart[] parts, ShaderCompilationStore compilationStore)
=> new GLShader(this, name, parts.Cast<GLShaderPart>().ToArray(), compilationStore);

public override IFrameBuffer CreateFrameBuffer(RenderBufferFormat[]? renderBufferFormats = null, TextureFilteringMode filteringMode = TextureFilteringMode.Linear)
{
Expand Down
6 changes: 1 addition & 5 deletions osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ internal class GLShader : IShader
{
private readonly GLRenderer renderer;
private readonly string name;
private readonly IUniformBuffer<GlobalUniformData> globalUniformBuffer;
private readonly GLShaderPart[] parts;

private readonly ScheduledDelegate shaderCompileDelegate;
Expand All @@ -42,11 +41,10 @@ internal class GLShader : IShader
private readonly GLShaderPart fragmentPart;
private readonly VertexFragmentShaderCompilation compilation;

internal GLShader(GLRenderer renderer, string name, GLShaderPart[] parts, IUniformBuffer<GlobalUniformData> 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);
Expand Down Expand Up @@ -88,8 +86,6 @@ private void compile()
throw new ProgramLinkingFailedException(name, GetProgramLog());

IsLoaded = true;

BindUniformBlock("g_GlobalUniforms", globalUniformBuffer);
}

internal void EnsureShaderCompiled()
Expand Down
27 changes: 13 additions & 14 deletions osu.Framework/Graphics/Rendering/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
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;
Expand Down Expand Up @@ -130,7 +129,7 @@ protected internal Storage? CacheStorage
private readonly Lazy<TextureWhitePixel> whitePixel;
private readonly LockedWeakList<Texture> allTextures = new LockedWeakList<Texture>();

private IUniformBuffer<GlobalUniformData>? globalUniformBuffer;
protected IUniformBuffer<GlobalUniformData>? GlobalUniformBuffer { get; private set; }
private IVertexBatch<TexturedVertex2D>? defaultQuadBatch;
private IVertexBatch? currentActiveBatch;
private MaskingInfo currentMaskingInfo;
Expand Down Expand Up @@ -189,8 +188,8 @@ protected internal virtual void BeginFrame(Vector2 windowSize)
foreach (var source in flush_source_statistics)
source.Value = 0;

globalUniformBuffer ??= ((IRenderer)this).CreateUniformBuffer<GlobalUniformData>();
globalUniformBuffer.Data = globalUniformBuffer.Data with
GlobalUniformBuffer ??= ((IRenderer)this).CreateUniformBuffer<GlobalUniformData>();
GlobalUniformBuffer.Data = GlobalUniformBuffer.Data with
{
IsDepthRangeZeroToOne = IsDepthRangeZeroToOne,
IsClipSpaceYInverted = IsClipSpaceYInverted,
Expand Down Expand Up @@ -606,7 +605,7 @@ private void setProjectionMatrix(Matrix4 matrix)

FlushCurrentBatch(FlushBatchSource.SetProjection);

globalUniformBuffer!.Data = globalUniformBuffer.Data with { ProjMatrix = matrix };
GlobalUniformBuffer!.Data = GlobalUniformBuffer.Data with { ProjMatrix = matrix };
ProjectionMatrix = matrix;
}

Expand Down Expand Up @@ -635,7 +634,7 @@ private void setMaskingInfo(MaskingInfo maskingInfo, bool isPushing, bool overwr

FlushCurrentBatch(FlushBatchSource.SetMasking);

globalUniformBuffer!.Data = globalUniformBuffer.Data with
GlobalUniformBuffer!.Data = GlobalUniformBuffer.Data with
{
IsMasking = IsMaskingActive,
MaskingRect = new Vector4(
Expand Down Expand Up @@ -669,14 +668,14 @@ private void setMaskingInfo(MaskingInfo maskingInfo, bool isPushing, bool overwr
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)
Expand Down Expand Up @@ -872,14 +871,14 @@ public bool BindTexture(INativeTexture texture, int unit = 0, WrapMode wrapModeS
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;
}

Expand Down Expand Up @@ -957,7 +956,7 @@ private void setFrameBuffer(IFrameBuffer? frameBuffer, bool force = false)

SetFrameBufferImplementation(frameBuffer);

globalUniformBuffer!.Data = globalUniformBuffer.Data with { BackbufferDraw = UsingBackbuffer };
GlobalUniformBuffer!.Data = GlobalUniformBuffer.Data with { BackbufferDraw = UsingBackbuffer };

FrameBuffer = frameBuffer;
}
Expand Down Expand Up @@ -1042,7 +1041,7 @@ internal void SetUniform<T>(IUniformWithValue<T> uniform)
protected abstract IShaderPart CreateShaderPart(IShaderStore store, string name, byte[]? rawData, ShaderPartType partType);

/// <inheritdoc cref="IRenderer.CreateShader"/>
protected abstract IShader CreateShader(string name, IShaderPart[] parts, IUniformBuffer<GlobalUniformData> globalUniformBuffer, ShaderCompilationStore compilationStore);
protected abstract IShader CreateShader(string name, IShaderPart[] parts, ShaderCompilationStore compilationStore);

private IShader? mipmapShader;

Expand All @@ -1062,7 +1061,7 @@ internal IShader GetMipmapShader()
{
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;
}
Expand Down Expand Up @@ -1154,7 +1153,7 @@ bool IRenderer.AllowTearing
void IRenderer.PopQuadBatch() => PopQuadBatch();
Image<Rgba32> 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<TVertex> IRenderer.CreateLinearBatch<TVertex>(int size, int maxBuffers, PrimitiveTopology topology)
{
Expand Down
6 changes: 1 addition & 5 deletions osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ internal class VeldridShader : IShader
{
private readonly string name;
private readonly VeldridShaderPart[] parts;
private readonly IUniformBuffer<GlobalUniformData> globalUniformBuffer;
private readonly ShaderCompilationStore compilationStore;
private readonly VeldridRenderer renderer;

Expand All @@ -43,11 +42,10 @@ internal class VeldridShader : IShader
private readonly Dictionary<string, VeldridUniformLayout> uniformLayouts = new Dictionary<string, VeldridUniformLayout>();
private readonly List<VeldridUniformLayout> textureLayouts = new List<VeldridUniformLayout>();

public VeldridShader(VeldridRenderer renderer, string name, VeldridShaderPart[] parts, IUniformBuffer<GlobalUniformData> 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;

Expand Down Expand Up @@ -210,8 +208,6 @@ private void loadToGpu()
renderer.Factory.CreateShader(vertexShaderDescription),
renderer.Factory.CreateShader(fragmentShaderDescription)
};

BindUniformBlock("g_GlobalUniforms", globalUniformBuffer);
}

private bool isDisposed;
Expand Down
6 changes: 4 additions & 2 deletions osu.Framework/Graphics/Veldrid/VeldridRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ public void DrawVertices(PrimitiveTopology type, int vertexStart, int verticesCo

var veldridShader = (VeldridShader)Shader!;

veldridShader.BindUniformBlock("g_GlobalUniforms", GlobalUniformBuffer!);

pipeline.PrimitiveTopology = type;
Array.Resize(ref pipeline.ResourceLayouts, veldridShader.LayoutCount);

Expand Down Expand Up @@ -730,8 +732,8 @@ private bool waitForFence(Fence fence, int millisecondsTimeout)
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<GlobalUniformData> globalUniformBuffer, ShaderCompilationStore compilationStore)
=> new VeldridShader(this, name, parts.Cast<VeldridShaderPart>().ToArray(), globalUniformBuffer, compilationStore);
protected override IShader CreateShader(string name, IShaderPart[] parts, ShaderCompilationStore compilationStore)
=> new VeldridShader(this, name, parts.Cast<VeldridShaderPart>().ToArray(), compilationStore);

public override IFrameBuffer CreateFrameBuffer(RenderBufferFormat[]? renderBufferFormats = null, TextureFilteringMode filteringMode = TextureFilteringMode.Linear)
=> new VeldridFrameBuffer(this, renderBufferFormats?.ToPixelFormats(), filteringMode.ToSamplerFilter());
Expand Down

0 comments on commit e40d447

Please sign in to comment.