forked from Rampastring/Rampastring.XNAUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderTargetPool.cs
115 lines (101 loc) · 3.85 KB
/
RenderTargetPool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using Microsoft.Xna.Framework.Graphics;
using Rampastring.Tools;
using System.Collections.Generic;
namespace Rampastring.XNAUI;
/// <summary>
/// A pool for render targets. Safe for multithreaded use.
/// </summary>
public class RenderTargetPool
{
private readonly GraphicsDevice graphicsDevice;
private static readonly object locker = new object();
private List<RenderTarget2D> renderTargets = new List<RenderTarget2D>();
public RenderTargetPool(GraphicsDevice graphicsDevice)
{
this.graphicsDevice = graphicsDevice;
}
/// <summary>
/// Adds a render target to the pool.
/// </summary>
/// <param name="renderTarget">The render target to add.</param>
public void Add(RenderTarget2D renderTarget)
{
lock (locker)
{
renderTargets.Add(renderTarget);
}
}
/// <summary>
/// Creates and adds a new render target to the pool.
/// Returns the created render target.
/// </summary>
/// <param name="width">The width of the render target.</param>
/// <param name="height">The height of the render target.</param>
public RenderTarget2D Create(int width, int height)
{
lock (locker)
{
var renderTarget = new RenderTarget2D(graphicsDevice, width, height, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
renderTargets.Add(renderTarget);
return renderTarget;
}
}
/// <summary>
/// Gets the smallest render target from the pool that is at least as large as the given size.
/// If a suitable render target is not found, then creates and returns
/// a new render target. The returned render target can be larger
/// than requested if no render targets of the exact requested size are available.
/// Removes the returned render target from the pool.
/// </summary>
/// <param name="width">The width of the render target.</param>
/// <param name="height">The height of the render target.</param>
public RenderTarget2D Get(int width, int height)
{
lock (locker)
{
RenderTarget2D bestRenderTarget = null;
int bestRenderTargetIndex = -1;
for (int i = 0; i < renderTargets.Count; i++)
{
var renderTarget = renderTargets[i];
if (renderTarget.Width < width || renderTarget.Height < height)
continue;
if (bestRenderTarget != null)
{
if (renderTarget.Width * renderTarget.Height > bestRenderTarget.Width * bestRenderTarget.Height)
continue;
}
bestRenderTarget = renderTarget;
bestRenderTargetIndex = i;
}
if (bestRenderTarget == null)
{
Logger.Log($"RenderTargetPool.Get: Creating new render target of size {width}x{height}");
bestRenderTarget = new RenderTarget2D(graphicsDevice, width, height,
false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
}
else
{
renderTargets.RemoveAt(bestRenderTargetIndex);
}
return bestRenderTarget;
}
}
/// <summary>
/// Removes a render target from the pool.
/// Returns a value that determines whether
/// the given render target was found and
/// removed from the pool.
/// Does NOT call Dispose on the render target,
/// so do it after calling this if you're not using the render
/// target afterwards.
/// </summary>
/// <param name="renderTarget">The render target to remove.</param>
public bool Remove(RenderTarget2D renderTarget)
{
lock (locker)
{
return renderTargets.Remove(renderTarget);
}
}
}