feat: deprecate swapto and reimplement basic api surface of managedrendertarget with target pools
This commit is contained in:
1 parent
74bba98039
commit
690bd2b72c
4 files changed
+117
-144
No files matched your search
+109
-95
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Wrapper class for <see cref="RenderTarget2D"/> that safely handles resizing, unloading and auto-disposal if not currently in use to save
|
||||
/// on GPU memory.
|
||||
/// </summary>
|
||||
[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<ManagedRenderTarget> 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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The actual <see cref="RenderTarget2D"/> instance that the wrapper contains.
|
||||
/// </summary>
|
||||
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);
|
||||
|
||||
/// <summary>
|
||||
/// How many frames since this target has been gotten. Used to dispose of unused targets for the sake of performance.
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the current render target to the provided one.
|
||||
/// </summary>
|
||||
/// <param name="target">The render target to swap to</param>
|
||||
/// <param name="flushColor">The color to clear the screen with. Transparent by default</param>
|
||||
public void SwapTo(Color? flushColor = null) => Target.SwapTo(flushColor);
|
||||
|
||||
/// <summary>
|
||||
/// Automatically called by <see cref="RenderTargetManager"/>, do not call!
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (IsDisposed)
|
||||
return;
|
||||
|
||||
IsDisposed = true;
|
||||
target?.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Automatically called by <see cref="RenderTargetManager"/>, do not call!
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The actual <see cref="RenderTarget2D"/> instance that the wrapper contains.
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the current render target to the provided one.
|
||||
/// </summary>
|
||||
/// <param name="target">The render target to swap to</param>
|
||||
/// <param name="flushColor">The color to clear the screen with. Transparent by default</param>
|
||||
[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;
|
||||
}
|
||||
@@ -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
|
||||
/// <summary>
|
||||
/// Manager class that handles all created instances of <see cref="ManagedRenderTarget"/>
|
||||
/// </summary>
|
||||
[Obsolete("ManagedRenderTarget is obsolete; developers should use RenderTarget2Ds and RenderTargetLeases")]
|
||||
public class RenderTargetManager : ModSystem
|
||||
{
|
||||
internal static List<ManagedRenderTarget> ManagedTargets { get; } = [];
|
||||
|
||||
public delegate void RenderTargetUpdateDelegate();
|
||||
|
||||
/// <summary>
|
||||
@@ -24,64 +24,19 @@ namespace CalamityMod.Graphics
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -818,6 +818,7 @@ namespace CalamityMod
|
||||
/// </summary>
|
||||
/// <param name="target">The render target to swap to</param>
|
||||
/// <param name="flushColor">The color to clear the screen with. Transparent by default</param>
|
||||
[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.
|
||||
|
||||
Reference in new issue
Block a user