mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Fixed Race-Condition in SamplerStateManager
This commit is contained in:
@@ -3,6 +3,7 @@ using GlitchyEngine.Core;
|
|||||||
using GlitchyEngine.Math;
|
using GlitchyEngine.Math;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using Bon;
|
using Bon;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace GlitchyEngine.Renderer
|
namespace GlitchyEngine.Renderer
|
||||||
{
|
{
|
||||||
@@ -170,6 +171,7 @@ namespace GlitchyEngine.Renderer
|
|||||||
public static class SamplerStateManager
|
public static class SamplerStateManager
|
||||||
{
|
{
|
||||||
static Dictionary<SamplerStateDescription, SamplerState> _samplers;
|
static Dictionary<SamplerStateDescription, SamplerState> _samplers;
|
||||||
|
static Monitor _samplersMonitor = new .() ~ delete _;
|
||||||
|
|
||||||
public static SamplerState PointClamp;
|
public static SamplerState PointClamp;
|
||||||
public static SamplerState PointWrap;
|
public static SamplerState PointWrap;
|
||||||
@@ -257,8 +259,11 @@ namespace GlitchyEngine.Renderer
|
|||||||
AnisotropicClamp.ReleaseRef();
|
AnisotropicClamp.ReleaseRef();
|
||||||
AnisotropicWrap.ReleaseRef();
|
AnisotropicWrap.ReleaseRef();
|
||||||
|
|
||||||
delete _samplers;
|
using (_samplersMonitor.Enter())
|
||||||
_samplers = null;
|
{
|
||||||
|
delete _samplers;
|
||||||
|
_samplers = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -273,13 +278,18 @@ namespace GlitchyEngine.Renderer
|
|||||||
|
|
||||||
Log.EngineLogger.AssertDebug(_samplers != null, "SamplerStateManager was not initialized.");
|
Log.EngineLogger.AssertDebug(_samplers != null, "SamplerStateManager was not initialized.");
|
||||||
|
|
||||||
if(_samplers.TryGetValue(desc, let sampler))
|
SamplerState newState;
|
||||||
{
|
|
||||||
return sampler..AddRef();
|
|
||||||
}
|
|
||||||
|
|
||||||
SamplerState newState = new SamplerState(desc);
|
using (_samplersMonitor.Enter())
|
||||||
ManageSampler(newState);
|
{
|
||||||
|
if(_samplers.TryGetValue(desc, let sampler))
|
||||||
|
{
|
||||||
|
return sampler..AddRef();
|
||||||
|
}
|
||||||
|
|
||||||
|
newState = new SamplerState(desc);
|
||||||
|
ManageSampler(newState);
|
||||||
|
}
|
||||||
|
|
||||||
return newState;
|
return newState;
|
||||||
}
|
}
|
||||||
@@ -288,7 +298,10 @@ namespace GlitchyEngine.Renderer
|
|||||||
{
|
{
|
||||||
Log.EngineLogger.AssertDebug(_samplers != null, "SamplerStateManager was not initialized.");
|
Log.EngineLogger.AssertDebug(_samplers != null, "SamplerStateManager was not initialized.");
|
||||||
|
|
||||||
_samplers.Add(samplerState.Description, samplerState);
|
using (_samplersMonitor.Enter())
|
||||||
|
{
|
||||||
|
_samplers.Add(samplerState.Description, samplerState);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -299,7 +312,10 @@ namespace GlitchyEngine.Renderer
|
|||||||
{
|
{
|
||||||
Log.EngineLogger.AssertDebug(samplerState.RefCount == 0, "Tried to delete sampler with nonzero reference count.");
|
Log.EngineLogger.AssertDebug(samplerState.RefCount == 0, "Tried to delete sampler with nonzero reference count.");
|
||||||
|
|
||||||
_samplers?.Remove(samplerState.Description);
|
using (_samplersMonitor.Enter())
|
||||||
|
{
|
||||||
|
_samplers?.Remove(samplerState.Description);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user