Skip to content

Commit

Permalink
Merge pull request #5681 from frenzibyte/fix-ubo-not-bound
Browse files Browse the repository at this point in the history
Fix local uniform buffers not always bound on draw
  • Loading branch information
peppy authored Mar 22, 2023
2 parents b1fe5da + 7157211 commit 840aec2
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 33 deletions.
6 changes: 2 additions & 4 deletions osu.Framework/Graphics/BufferedDrawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,12 @@ public sealed override void Draw(IRenderer renderer)
SharedData.DrawVersion = GetDrawVersion();
}

var shader = TextureShader;

shader.Bind();
BindTextureShader(renderer);

base.Draw(renderer);
DrawContents(renderer);

shader.Unbind();
UnbindTextureShader(renderer);
}

/// <summary>
Expand Down
6 changes: 2 additions & 4 deletions osu.Framework/Graphics/Sprites/BufferedContainerView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ public override void Draw(IRenderer renderer)
if (shared?.MainBuffer?.Texture.Available != true || shared.DrawVersion == -1)
return;

var shader = TextureShader;

shader.Bind();
BindTextureShader(renderer);

if (sourceEffectPlacement == EffectPlacement.InFront)
drawMainBuffer(renderer);
Expand All @@ -151,7 +149,7 @@ public override void Draw(IRenderer renderer)
if (sourceEffectPlacement == EffectPlacement.Behind)
drawMainBuffer(renderer);

shader.Unbind();
UnbindTextureShader(renderer);
}

private void drawMainBuffer(IRenderer renderer)
Expand Down
10 changes: 4 additions & 6 deletions osu.Framework/Graphics/Sprites/SpriteDrawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ public override void Draw(IRenderer renderer)
if (Texture?.Available != true)
return;

var shader = TextureShader;

shader.Bind();
BindTextureShader(renderer);

Blit(renderer);

shader.Unbind();
UnbindTextureShader(renderer);
}

protected override void DrawOpaqueInterior(IRenderer renderer)
Expand All @@ -99,11 +97,11 @@ protected override void DrawOpaqueInterior(IRenderer renderer)
if (Texture?.Available != true)
return;

TextureShader.Bind();
BindTextureShader(renderer);

BlitOpaqueInterior(renderer);

TextureShader.Unbind();
UnbindTextureShader(renderer);
}

protected internal override bool CanDrawOpaqueInterior => Texture?.Available == true && Texture.Opacity == Opacity.Opaque && hasOpaqueInterior;
Expand Down
6 changes: 2 additions & 4 deletions osu.Framework/Graphics/Sprites/SpriteText_DrawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ public override void Draw(IRenderer renderer)
{
base.Draw(renderer);

var shader = TextureShader;

shader.Bind();
BindTextureShader(renderer);

var avgColour = (Color4)DrawColourInfo.Colour.AverageColour;
float shadowAlpha = MathF.Pow(Math.Max(Math.Max(avgColour.R, avgColour.G), avgColour.B), 2);
Expand Down Expand Up @@ -80,7 +78,7 @@ public override void Draw(IRenderer renderer)
renderer.DrawQuad(parts[i].Texture, parts[i].DrawQuad, DrawColourInfo.Colour, inflationPercentage: parts[i].InflationPercentage);
}

shader.Unbind();
UnbindTextureShader(renderer);
}
}

Expand Down
27 changes: 25 additions & 2 deletions osu.Framework/Graphics/TexturedShaderDrawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

#nullable disable

using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Shaders;

namespace osu.Framework.Graphics
{
public abstract class TexturedShaderDrawNode : DrawNode
{
protected IShader TextureShader { get; private set; }
private IShader textureShader;

protected new ITexturedShaderDrawable Source => (ITexturedShaderDrawable)base.Source;

Expand All @@ -22,7 +23,29 @@ public override void ApplyState()
{
base.ApplyState();

TextureShader = Source.TextureShader;
textureShader = Source.TextureShader;
}

/// <summary>
/// Binds the <see cref="IShader"/> used for rendering the texture.
/// </summary>
/// <param name="renderer">The renderer to use for setting up uniform resources.</param>
protected void BindTextureShader(IRenderer renderer)
{
textureShader.Bind();

BindUniformResources(textureShader, renderer);
}

protected void UnbindTextureShader(IRenderer renderer) => textureShader.Unbind();

/// <summary>
/// Binds uniform resources against the provided shader.
/// </summary>
/// <param name="shader">The shader to bind uniform resources against.</param>
/// <param name="renderer">The renderer to use for setting up uniform resources.</param>
protected virtual void BindUniformResources(IShader shader, IRenderer renderer)
{
}
}
}
11 changes: 8 additions & 3 deletions osu.Framework/Graphics/UserInterface/CircularBlob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ protected override void Blit(IRenderer renderer)
if (innerRadius == 0)
return;

base.Blit(renderer);
}

protected override void BindUniformResources(IShader shader, IRenderer renderer)
{
base.BindUniformResources(shader, renderer);

parametersBuffer ??= renderer.CreateUniformBuffer<CircularBlobParameters>();
parametersBuffer.Data = new CircularBlobParameters
{
Expand All @@ -139,9 +146,7 @@ protected override void Blit(IRenderer renderer)
NoisePosition = noisePosition,
};

TextureShader.BindUniformBlock("m_CircularBlobParameters", parametersBuffer);

base.Blit(renderer);
shader.BindUniformBlock("m_CircularBlobParameters", parametersBuffer);
}

protected internal override bool CanDrawOpaqueInterior => false;
Expand Down
11 changes: 8 additions & 3 deletions osu.Framework/Graphics/UserInterface/CircularProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ protected override void Blit(IRenderer renderer)
if (innerRadius == 0 || (!roundedCaps && progress == 0))
return;

base.Blit(renderer);
}

protected override void BindUniformResources(IShader shader, IRenderer renderer)
{
base.BindUniformResources(shader, renderer);

parametersBuffer ??= renderer.CreateUniformBuffer<CircularProgressParameters>();
parametersBuffer.Data = new CircularProgressParameters
{
Expand All @@ -131,9 +138,7 @@ protected override void Blit(IRenderer renderer)
RoundedCaps = roundedCaps,
};

TextureShader.BindUniformBlock("m_CircularProgressParameters", parametersBuffer);

base.Blit(renderer);
shader.BindUniformBlock("m_CircularProgressParameters", parametersBuffer);
}

protected internal override bool CanDrawOpaqueInterior => false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,14 @@ public override void ApplyState()

private IUniformBuffer<HueData> hueDataBuffer;

protected override void Blit(IRenderer renderer)
protected override void BindUniformResources(IShader shader, IRenderer renderer)
{
base.BindUniformResources(shader, renderer);

hueDataBuffer ??= renderer.CreateUniformBuffer<HueData>();
hueDataBuffer.Data = hueDataBuffer.Data with { Hue = hue };
TextureShader.BindUniformBlock("m_HueData", hueDataBuffer);
base.Blit(renderer);

shader.BindUniformBlock("m_HueData", hueDataBuffer);
}

protected override void Dispose(bool isDisposing)
Expand Down
8 changes: 4 additions & 4 deletions osu.Framework/Graphics/Video/VideoSpriteDrawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Runtime.InteropServices;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Shaders;
using osu.Framework.Graphics.Shaders.Types;

namespace osu.Framework.Graphics.Video
Expand All @@ -22,15 +23,14 @@ public VideoSpriteDrawNode(Video source)

private IUniformBuffer<YuvData> yuvDataBuffer;

public override void Draw(IRenderer renderer)
protected override void BindUniformResources(IShader shader, IRenderer renderer)
{
base.BindUniformResources(shader, renderer);

yuvDataBuffer ??= renderer.CreateUniformBuffer<YuvData>();
yuvDataBuffer.Data = new YuvData { YuvCoeff = video.ConversionMatrix };

var shader = TextureShader;
shader.BindUniformBlock("m_yuvData", yuvDataBuffer);

base.Draw(renderer);
}

protected override void Dispose(bool isDisposing)
Expand Down

0 comments on commit 840aec2

Please sign in to comment.