Fixed Race-Condition in SamplerStateManager

This commit is contained in:
Simon Lübeß
2023-08-01 22:50:09 +02:00
parent 60725c3588
commit d2600c7517
+17 -1
View File
@@ -3,6 +3,7 @@ using GlitchyEngine.Core;
using GlitchyEngine.Math;
using System.Collections;
using Bon;
using System.Threading;
namespace GlitchyEngine.Renderer
{
@@ -170,6 +171,7 @@ namespace GlitchyEngine.Renderer
public static class SamplerStateManager
{
static Dictionary<SamplerStateDescription, SamplerState> _samplers;
static Monitor _samplersMonitor = new .() ~ delete _;
public static SamplerState PointClamp;
public static SamplerState PointWrap;
@@ -257,9 +259,12 @@ namespace GlitchyEngine.Renderer
AnisotropicClamp.ReleaseRef();
AnisotropicWrap.ReleaseRef();
using (_samplersMonitor.Enter())
{
delete _samplers;
_samplers = null;
}
}
/**
* Returns a Sampler State that has the specified settings.
@@ -273,13 +278,18 @@ namespace GlitchyEngine.Renderer
Log.EngineLogger.AssertDebug(_samplers != null, "SamplerStateManager was not initialized.");
SamplerState newState;
using (_samplersMonitor.Enter())
{
if(_samplers.TryGetValue(desc, let sampler))
{
return sampler..AddRef();
}
SamplerState newState = new SamplerState(desc);
newState = new SamplerState(desc);
ManageSampler(newState);
}
return newState;
}
@@ -288,8 +298,11 @@ namespace GlitchyEngine.Renderer
{
Log.EngineLogger.AssertDebug(_samplers != null, "SamplerStateManager was not initialized.");
using (_samplersMonitor.Enter())
{
_samplers.Add(samplerState.Description, samplerState);
}
}
/**
* Removes the given SamplerState from the manager.
@@ -299,9 +312,12 @@ namespace GlitchyEngine.Renderer
{
Log.EngineLogger.AssertDebug(samplerState.RefCount == 0, "Tried to delete sampler with nonzero reference count.");
using (_samplersMonitor.Enter())
{
_samplers?.Remove(samplerState.Description);
}
}
}
public class SamplerState : RefCounter
{