graphics resource tracking optimization

pull/47/head
cosmonaut 2023-03-06 11:14:05 -08:00
parent 0781a99273
commit e93f4f09bb
2 changed files with 9 additions and 10 deletions

View File

@ -18,7 +18,7 @@ namespace MoonWorks.Graphics
public bool IsDisposed { get; private set; }
private readonly List<WeakReference<GraphicsResource>> resources = new List<WeakReference<GraphicsResource>>();
private readonly HashSet<WeakReference<GraphicsResource>> resources = new HashSet<WeakReference<GraphicsResource>>();
public GraphicsDevice(
Backend preferredBackend,
@ -237,10 +237,9 @@ namespace MoonWorks.Graphics
{
lock (resources)
{
for (var i = resources.Count - 1; i >= 0; i--)
foreach (var weakReference in resources)
{
var resource = resources[i];
if (resource.TryGetTarget(out var target))
if (weakReference.TryGetTarget(out var target))
{
target.Dispose();
}

View File

@ -10,7 +10,7 @@ namespace MoonWorks.Graphics
public bool IsDisposed { get; private set; }
protected abstract Action<IntPtr, IntPtr> QueueDestroyFunction { get; }
private WeakReference<GraphicsResource> selfReference;
internal WeakReference<GraphicsResource> weakReference;
public GraphicsResource(GraphicsDevice device, bool trackResource = true)
{
@ -18,8 +18,8 @@ namespace MoonWorks.Graphics
if (trackResource)
{
selfReference = new WeakReference<GraphicsResource>(this);
Device.AddResourceReference(selfReference);
weakReference = new WeakReference<GraphicsResource>(this);
Device.AddResourceReference(weakReference);
}
}
@ -27,11 +27,11 @@ namespace MoonWorks.Graphics
{
if (!IsDisposed)
{
if (selfReference != null)
if (weakReference != null)
{
QueueDestroyFunction(Device.Handle, Handle);
Device.RemoveResourceReference(selfReference);
selfReference = null;
Device.RemoveResourceReference(weakReference);
weakReference = null;
}
IsDisposed = true;