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; } 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( public GraphicsDevice(
Backend preferredBackend, Backend preferredBackend,
@ -237,10 +237,9 @@ namespace MoonWorks.Graphics
{ {
lock (resources) lock (resources)
{ {
for (var i = resources.Count - 1; i >= 0; i--) foreach (var weakReference in resources)
{ {
var resource = resources[i]; if (weakReference.TryGetTarget(out var target))
if (resource.TryGetTarget(out var target))
{ {
target.Dispose(); target.Dispose();
} }

View File

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