diff --git a/Graphics/ManagedRenderTarget.cs b/Graphics/ManagedRenderTarget.cs
index 1c00036be6..6f144ab40d 100644
--- a/Graphics/ManagedRenderTarget.cs
+++ b/Graphics/ManagedRenderTarget.cs
@@ -1,115 +1,129 @@
using System;
+using System.Collections.Generic;
+using CalamityMod.Utilities.Daybreak.Buffers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
-namespace CalamityMod.Graphics
+namespace CalamityMod.Graphics;
+
+internal sealed class ManagedRenderTargetPool : RenderTargetPool
{
- ///
- /// Wrapper class for that safely handles resizing, unloading and auto-disposal if not currently in use to save
- /// on GPU memory.
- ///
- [Obsolete("ManagedRenderTarget is obsolete; developers should use RenderTarget2Ds and RenderTargetLeases")]
- public class ManagedRenderTarget : IDisposable
+ public new static ManagedRenderTargetPool Shared { get; } = new();
+
+ private static readonly List managedTargets = [];
+
+ public override RenderTargetLease Rent(GraphicsDevice device, int width, int height, RenderTargetDescriptor descriptor)
{
- private RenderTarget2D target;
+ throw new InvalidOperationException("ManagedRenderTargetPool does not support directly leasing targets");
+ }
- public readonly RenderTargetCreationCondition CreationCondition;
+ public RenderTargetLease Register(ManagedRenderTarget managedTarget)
+ {
+ managedTargets.Add(managedTarget);
+ return new RenderTargetLease(managedTarget.CreationCondition(Main.screenWidth, Main.screenHeight), this);
+ }
- public readonly bool ShouldResetUponScreenResize;
+ public override void Return(RenderTargetLease lease)
+ {
+ lease.Target.Dispose();
+ }
- public readonly bool ShouldAutoDispose;
+ public void Return(ManagedRenderTarget managedRt)
+ {
+ _ = managedTargets.Remove(managedRt);
+ Return(managedRt.lease);
+ }
- public bool WaitingForFirstInitialization
+ public override void Dispose()
+ {
+ foreach (var target in managedTargets)
{
- get;
- private set;
- } = true;
-
- public bool IsUninitialized => target is null || target.IsDisposed;
-
- public bool IsDisposed
- {
- get;
- private set;
+ target.Dispose();
}
- ///
- /// The actual instance that the wrapper contains.
- ///
- public RenderTarget2D Target
+ managedTargets.Clear();
+ }
+
+ internal void ResizeTargets()
+ {
+ foreach (var target in managedTargets)
{
- get
+ if (target.ShouldResetUponScreenResize)
{
- if (IsUninitialized)
- {
- target = CreationCondition(Main.screenWidth, Main.screenHeight);
- WaitingForFirstInitialization = false;
- IsDisposed = false;
- }
-
- TimeSinceLastAccessed = 0;
- return target;
+ target.lease.Target.Dispose();
+ target.lease.Target = target.CreationCondition(Main.screenWidth, Main.screenHeight);
}
- private set => target = value;
}
-
- public int Width => Target.Width;
-
- public int Height => Target.Height;
-
- public Vector2 Size => new(Width, Height);
-
- ///
- /// How many frames since this target has been gotten. Used to dispose of unused targets for the sake of performance.
- ///
- public int TimeSinceLastAccessed;
-
- public delegate RenderTarget2D RenderTargetCreationCondition(int screenWidth, int screenHeight);
-
- // For some reason, adding more arguments to this constructor causes really low end pcs to crash in FNA. I cannot figure out why, so do not use them.
- public static RenderTarget2D CreateScreenSizedTarget(int screenWidth, int screenHeight) => new(Main.instance.GraphicsDevice, screenWidth, screenHeight);
-
- public ManagedRenderTarget(bool shouldResetUponScreenResize, RenderTargetCreationCondition creationCondition, bool shouldAutoDispose = true)
- {
- ShouldResetUponScreenResize = shouldResetUponScreenResize;
- CreationCondition = creationCondition;
- ShouldAutoDispose = shouldAutoDispose;
- RenderTargetManager.ManagedTargets.Add(this);
- }
-
- ///
- /// Sets the current render target to the provided one.
- ///
- /// The render target to swap to
- /// The color to clear the screen with. Transparent by default
- public void SwapTo(Color? flushColor = null) => Target.SwapTo(flushColor);
-
- ///
- /// Automatically called by , do not call!
- ///
- public void Dispose()
- {
- if (IsDisposed)
- return;
-
- IsDisposed = true;
- target?.Dispose();
- GC.SuppressFinalize(this);
- }
-
- ///
- /// Automatically called by , do not call!
- ///
- public void Recreate(int screenWidth, int screenHeight)
- {
- Dispose();
- IsDisposed = false;
-
- target = CreationCondition(screenWidth, screenHeight);
- TimeSinceLastAccessed = 0;
- }
-
- public static implicit operator RenderTarget2D(ManagedRenderTarget target) => target.Target;
}
}
+
+[Obsolete("ManagedRenderTarget is obsolete; developers should use RenderTarget2Ds and RenderTargetLeases")]
+public class ManagedRenderTarget : IDisposable
+{
+ internal RenderTargetLease lease;
+
+ public readonly RenderTargetCreationCondition CreationCondition;
+
+ public readonly bool ShouldResetUponScreenResize;
+
+ public readonly bool ShouldAutoDispose;
+
+ public bool WaitingForFirstInitialization => false;
+
+ public bool IsUninitialized => false;
+
+ public bool IsDisposed
+ {
+ get;
+ private set;
+ }
+
+ ///
+ /// The actual instance that the wrapper contains.
+ ///
+ public RenderTarget2D Target => lease.Target;
+
+ public int Width => Target.Width;
+
+ public int Height => Target.Height;
+
+ public Vector2 Size => new(Width, Height);
+
+ public int TimeSinceLastAccessed;
+
+ public delegate RenderTarget2D RenderTargetCreationCondition(int screenWidth, int screenHeight);
+
+ [Obsolete("use ScreenspaceTargetPool")]
+ public static RenderTarget2D CreateScreenSizedTarget(int screenWidth, int screenHeight) => new(Main.instance.GraphicsDevice, screenWidth, screenHeight);
+
+ public ManagedRenderTarget(bool shouldResetUponScreenResize, RenderTargetCreationCondition creationCondition, bool shouldAutoDispose = true)
+ {
+ ShouldResetUponScreenResize = shouldResetUponScreenResize;
+ CreationCondition = creationCondition;
+ ShouldAutoDispose = shouldAutoDispose;
+ lease = ManagedRenderTargetPool.Shared.Register(this);
+ }
+
+ ///
+ /// Sets the current render target to the provided one.
+ ///
+ /// The render target to swap to
+ /// The color to clear the screen with. Transparent by default
+ [Obsolete("Use RenderTargetScope")]
+ public void SwapTo(Color? flushColor = null) => Target.SwapTo(flushColor);
+
+ public void Dispose()
+ {
+ if (IsDisposed)
+ return;
+
+ IsDisposed = true;
+ ManagedRenderTargetPool.Shared.Return(this);
+ }
+
+ // Does nothing
+ public void Recreate(int screenWidth, int screenHeight) { }
+
+ public static implicit operator RenderTarget2D(ManagedRenderTarget target) => target.Target;
+}
diff --git a/Graphics/RenderTargetManager.cs b/Graphics/RenderTargetManager.cs
index 56b673c968..52dd844c8c 100644
--- a/Graphics/RenderTargetManager.cs
+++ b/Graphics/RenderTargetManager.cs
@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ModLoader;
@@ -8,10 +9,9 @@ namespace CalamityMod.Graphics
///
/// Manager class that handles all created instances of
///
+ [Obsolete("ManagedRenderTarget is obsolete; developers should use RenderTarget2Ds and RenderTargetLeases")]
public class RenderTargetManager : ModSystem
{
- internal static List ManagedTargets { get; } = [];
-
public delegate void RenderTargetUpdateDelegate();
///
@@ -24,64 +24,19 @@ namespace CalamityMod.Graphics
///
public const int TimeBeforeAutoDispose = 600;
- internal static void ResetTargetSizes(Vector2 screenSize)
- {
- foreach (ManagedRenderTarget target in ManagedTargets)
- {
- // Don't attempt to recreate targets that are already initialized or shouldn't be recreated.
- if (target is null || target.IsDisposed || target.WaitingForFirstInitialization)
- continue;
-
- Main.QueueMainThreadAction(() =>
- {
- target.Recreate((int)screenSize.X, (int)screenSize.Y);
- });
- }
- }
-
- internal static void DisposeOfTargets()
- {
- Main.QueueMainThreadAction(() =>
- {
- foreach (ManagedRenderTarget target in ManagedTargets)
- target?.Dispose();
- ManagedTargets.Clear();
- });
- }
-
public override void OnModLoad()
{
Main.OnPreDraw += HandleTargetUpdateLoop;
- Main.OnResolutionChanged += ResetTargetSizes;
}
public override void OnModUnload()
{
- DisposeOfTargets();
Main.OnPreDraw -= HandleTargetUpdateLoop;
- Main.OnResolutionChanged -= ResetTargetSizes;
-
- if (RenderTargetUpdateLoopEvent != null)
- {
- foreach (var subscription in RenderTargetUpdateLoopEvent.GetInvocationList())
- RenderTargetUpdateLoopEvent -= (RenderTargetUpdateDelegate)subscription;
- }
+ RenderTargetUpdateLoopEvent = null;
}
private void HandleTargetUpdateLoop(GameTime obj)
{
- // Auto-disposal of targets that havent been used in a while, to stop them hogging GPU memory.
- foreach (ManagedRenderTarget target in ManagedTargets)
- {
- if (target == null || target.IsDisposed || !target.ShouldAutoDispose)
- continue;
-
- if (target.TimeSinceLastAccessed >= TimeBeforeAutoDispose)
- target.Dispose();
- else
- target.TimeSinceLastAccessed++;
- }
-
RenderTargetUpdateLoopEvent?.Invoke();
}
}
diff --git a/Utilities/Daybreak/Buffers/BuffersLoadingSystem.cs b/Utilities/Daybreak/Buffers/BuffersLoadingSystem.cs
index 4d4abc310e..3e3b4292f4 100644
--- a/Utilities/Daybreak/Buffers/BuffersLoadingSystem.cs
+++ b/Utilities/Daybreak/Buffers/BuffersLoadingSystem.cs
@@ -1,3 +1,4 @@
+using CalamityMod.Graphics;
using Terraria;
using Terraria.ModLoader;
@@ -44,6 +45,7 @@ public class BuffersLoadingSystem : ModSystem
// Dispose shared pools.
RenderTargetPool.Shared.Dispose();
ScreenspaceTargetPool.Shared.Dispose();
+ ManagedRenderTargetPool.Shared.Dispose();
}
);
}
@@ -62,5 +64,6 @@ public class BuffersLoadingSystem : ModSystem
orig(self);
ScreenspaceTargetPool.Shared.ResizeCachedTargets(self.GraphicsDevice);
+ ManagedRenderTarget.Shared.ResizeTargets();
}
}
diff --git a/Utilities/DrawingUtils.cs b/Utilities/DrawingUtils.cs
index 362d3fc71e..6941342c23 100644
--- a/Utilities/DrawingUtils.cs
+++ b/Utilities/DrawingUtils.cs
@@ -818,6 +818,7 @@ namespace CalamityMod
///
/// The render target to swap to
/// The color to clear the screen with. Transparent by default
+ [Obsolete("Use RenderTargetScope")]
public static void SwapTo(this RenderTarget2D target, Color? flushColor = null)
{
// If we are in the menu, a server, or any of these are null, return.