Skip to content

Commit

Permalink
should save shaders into shared data instead because frame buffer is …
Browse files Browse the repository at this point in the history
…highly related to shader.

it might cause not found issue if changing frame buffer dictionary and drawing at the same time.
  • Loading branch information
andy840119 authored and andy840119 committed Oct 11, 2021
1 parent caa3433 commit f45d0b5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
17 changes: 3 additions & 14 deletions osu.Framework.Font/Graphics/MultiShaderBufferedDrawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public class MultiShaderBufferedDrawNode : BufferedDrawNode

protected new MultiShaderBufferedDrawNodeSharedData SharedData => (MultiShaderBufferedDrawNodeSharedData)base.SharedData;

private IShader[] shaders = { };

private readonly double loadTime;

public MultiShaderBufferedDrawNode(IBufferedDrawable source, DrawNode child, MultiShaderBufferedDrawNodeSharedData sharedData)
Expand All @@ -33,19 +31,13 @@ public MultiShaderBufferedDrawNode(IBufferedDrawable source, DrawNode child, Mul
public override void ApplyState()
{
base.ApplyState();

if (shaders.SequenceEqual(Source.Shaders))
return;

// should clear buffer if property changed because might be shader amount changed.
shaders = Source.Shaders.ToArray();
SharedData.CreateDefaultFrameBuffers(shaders);
SharedData.UpdateFrameBuffers(Source.Shaders.ToArray());
}

protected override long GetDrawVersion()
{
// if contains shader that need to apply time, then need to force run populate contents in each frame.
if (ContainTimePropertyShader(shaders))
if (ContainTimePropertyShader(SharedData.Shaders))
{
ResetDrawVersion();
}
Expand Down Expand Up @@ -91,10 +83,6 @@ 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 @@ -107,6 +95,7 @@ protected override void DrawContents()

private void drawFrameBuffer()
{
var shaders = SharedData.Shaders;
if (!shaders.Any())
return;

Expand Down
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.OpenGL;
Expand All @@ -17,6 +16,8 @@ public class MultiShaderBufferedDrawNodeSharedData : BufferedDrawNodeSharedData
{
private readonly Dictionary<IShader, FrameBuffer> shaderBuffers = new Dictionary<IShader, FrameBuffer>();

public IShader[] Shaders => shaderBuffers.Keys.ToArray();

private readonly RenderbufferInternalFormat[] formats;

public MultiShaderBufferedDrawNodeSharedData(RenderbufferInternalFormat[] formats = null, bool pixelSnapping = false)
Expand All @@ -25,11 +26,12 @@ public MultiShaderBufferedDrawNodeSharedData(RenderbufferInternalFormat[] format
this.formats = formats;
}

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

// collect all frame buffer that needs to be disposed.
var disposedFrameBuffer = shaderBuffers.Values.ToArray();
shaderBuffers.Clear();

Expand Down Expand Up @@ -66,15 +68,15 @@ public FrameBuffer GetSourceFrameBuffer(IShader shader)
return CurrentEffectBuffer;

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

return shaderBuffers[fromShader];
}

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

return shaderBuffers[shader];
}
Expand All @@ -90,7 +92,12 @@ public void UpdateBuffer(IShader shader, FrameBuffer frameBuffer)
public FrameBuffer[] GetDrawFrameBuffers()
=> shaderBuffers.Where(x =>
{
var shader = x.Key;
var (shader, frameBuffer) = x;

// should not render disposed or not created frame buffer.
if (frameBuffer.Texture == null)
return false;

if (shader is IStepShader stepShader)
return stepShader.StepShaders.Any() && stepShader.Draw;

Expand Down

0 comments on commit f45d0b5

Please sign in to comment.