Skip to content

Commit

Permalink
make list of frame buffer being readonly.
Browse files Browse the repository at this point in the history
also prevent duplicated destroy and create if shaders are the same.
  • Loading branch information
andy840119 authored and andy840119 committed Oct 11, 2021
1 parent a57139d commit caa3433
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
25 changes: 6 additions & 19 deletions osu.Framework.Font/Graphics/MultiShaderBufferedDrawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using osu.Framework.Graphics.Colour;
Expand Down Expand Up @@ -92,6 +91,10 @@ protected override void DrawContents()
{
foreach (var frameBuffer in drawFrameBuffers)
{
// got no idea why happened.
if (frameBuffer.Texture == null)
break;

DrawFrameBuffer(frameBuffer, DrawRectangle, Color4.White);
}
}
Expand All @@ -111,8 +114,8 @@ private void drawFrameBuffer()

foreach (var shader in shaders)
{
var current = getSourceFrameBuffer(shader);
var target = SharedData.ShaderBuffers[shader];
var current = SharedData.GetSourceFrameBuffer(shader);
var target = SharedData.GetTargetFrameBuffer(shader);

if (shader is IStepShader stepShader)
{
Expand Down Expand Up @@ -149,22 +152,6 @@ void renderShader(IShader shader, FrameBuffer current, FrameBuffer target)
shader.Unbind();
}
}

FrameBuffer getSourceFrameBuffer(IShader targetShader)
{
if (!(targetShader is IStepShader stepShader))
return SharedData.CurrentEffectBuffer;

var fromShader = stepShader.FromShader;
if (fromShader == null)
return SharedData.CurrentEffectBuffer;

var shaderBuffers = SharedData.ShaderBuffers;
if (!shaderBuffers.ContainsKey(fromShader))
throw new DirectoryNotFoundException("Frame buffer does not found in target shader.");

return shaderBuffers[fromShader];
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using osu.Framework.Graphics.OpenGL;
using osu.Framework.Graphics.OpenGL.Buffers;
using osu.Framework.Graphics.Shaders;
using osuTK.Graphics.ES30;
Expand All @@ -14,8 +17,6 @@ public class MultiShaderBufferedDrawNodeSharedData : BufferedDrawNodeSharedData
{
private readonly Dictionary<IShader, FrameBuffer> shaderBuffers = new Dictionary<IShader, FrameBuffer>();

public IReadOnlyDictionary<IShader, FrameBuffer> ShaderBuffers => shaderBuffers;

private readonly RenderbufferInternalFormat[] formats;

public MultiShaderBufferedDrawNodeSharedData(RenderbufferInternalFormat[] formats = null, bool pixelSnapping = false)
Expand All @@ -26,14 +27,56 @@ public MultiShaderBufferedDrawNodeSharedData(RenderbufferInternalFormat[] format

public void CreateDefaultFrameBuffers(IShader[] shaders)
{
clearBuffers();
if (shaderBuffers.Keys.SequenceEqual(shaders))
return;

var filterMode = PixelSnapping ? All.Nearest : All.Linear;
var disposedFrameBuffer = shaderBuffers.Values.ToArray();
shaderBuffers.Clear();

foreach (var shader in shaders)
{
var filterMode = PixelSnapping ? All.Nearest : All.Linear;
shaderBuffers.Add(shader, new FrameBuffer(formats, filterMode));
}

clearBuffersWithSchedule(() =>
{
clearBuffers(disposedFrameBuffer);
});

static void clearBuffersWithSchedule(Action action)
{
// should call GLWrapper.ScheduleDisposal(() => Dispose(true));
Delegate act = new Action(() => action?.Invoke());
var prop = typeof(GLWrapper).GetRuntimeMethods().FirstOrDefault(x => x.Name == "ScheduleDisposal");
if (prop == null)
throw new NullReferenceException();

prop.Invoke(prop, new object[] { act });
}
}

public FrameBuffer GetSourceFrameBuffer(IShader shader)
{
if (!(shader is IStepShader stepShader))
return CurrentEffectBuffer;

var fromShader = stepShader.FromShader;
if (fromShader == null)
return CurrentEffectBuffer;

if (!shaderBuffers.ContainsKey(fromShader))
throw new DirectoryNotFoundException();

return shaderBuffers[fromShader];
}

public FrameBuffer GetTargetFrameBuffer(IShader shader)
{
if (!shaderBuffers.ContainsKey(shader))
throw new DirectoryNotFoundException();

return shaderBuffers[shader];
}

public void UpdateBuffer(IShader shader, FrameBuffer frameBuffer)
Expand All @@ -45,7 +88,7 @@ public void UpdateBuffer(IShader shader, FrameBuffer frameBuffer)
}

public FrameBuffer[] GetDrawFrameBuffers()
=> ShaderBuffers.Where(x =>
=> shaderBuffers.Where(x =>
{
var shader = x.Key;
if (shader is IStepShader stepShader)
Expand All @@ -57,18 +100,16 @@ public FrameBuffer[] GetDrawFrameBuffers()
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
clearBuffers();
clearBuffers(shaderBuffers.Values.ToArray());
}

private void clearBuffers()
private void clearBuffers(FrameBuffer[] effectBuffers)
{
// clear all frame in the dictionary.
foreach (var shaderBuffer in shaderBuffers)
// dispose all frame buffer in array.
foreach (var shaderBuffer in effectBuffers)
{
shaderBuffer.Value.Dispose();
shaderBuffer.Dispose();
}

shaderBuffers.Clear();
}
}
}

0 comments on commit caa3433

Please sign in to comment.