forked from AerysBat/XNALara
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRenderToTexture.cs
80 lines (70 loc) · 3.19 KB
/
RenderToTexture.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
using Microsoft.Xna.Framework.Graphics;
namespace XNALara
{
public class RenderToTexture
{
private GraphicsDevice graphicsDevice;
private RenderTarget2D renderTarget;
private DepthStencilBuffer depthBuffer;
private DepthStencilBuffer savedDepthBuffer;
private int renderTargetWidth;
private int renderTargetHeight;
public RenderToTexture(GraphicsDevice graphicsDevice) {
this.graphicsDevice = graphicsDevice;
renderTarget = null;
}
public void Begin() {
PresentationParameters parameters = graphicsDevice.PresentationParameters;
Begin(parameters.BackBufferWidth, parameters.BackBufferHeight);
}
public void Begin(int backBufferWidth, int backBufferHeight) {
Begin(backBufferWidth, backBufferHeight, graphicsDevice.DisplayMode.Format,
graphicsDevice.PresentationParameters.MultiSampleType);
}
public void Begin(int backBufferWidth, int backBufferHeight, SurfaceFormat bufferFormat) {
Begin(backBufferWidth, backBufferHeight, bufferFormat,
graphicsDevice.PresentationParameters.MultiSampleType);
}
public void Begin(int backBufferWidth, int backBufferHeight, MultiSampleType multiSampleType) {
Begin(backBufferWidth, backBufferHeight, graphicsDevice.DisplayMode.Format, multiSampleType);
}
public void Begin(int backBufferWidth, int backBufferHeight, SurfaceFormat bufferFormat, MultiSampleType multiSampleType) {
if (renderTarget == null ||
renderTargetWidth != backBufferWidth ||
renderTargetHeight != backBufferHeight) {
if (renderTarget != null) {
renderTarget.Dispose();
depthBuffer.Dispose();
}
renderTarget =
new RenderTarget2D(graphicsDevice,
backBufferWidth, backBufferHeight,
1, bufferFormat,
multiSampleType, 0);
depthBuffer =
new DepthStencilBuffer(graphicsDevice,
backBufferWidth, backBufferHeight,
graphicsDevice.DepthStencilBuffer.Format,
multiSampleType, 0);
renderTargetWidth = backBufferWidth;
renderTargetHeight = backBufferHeight;
}
graphicsDevice.SetRenderTarget(0, renderTarget);
savedDepthBuffer = graphicsDevice.DepthStencilBuffer;
graphicsDevice.DepthStencilBuffer = depthBuffer;
}
public Texture2D End() {
return End(false);
}
public Texture2D End(bool dispose) {
graphicsDevice.SetRenderTarget(0, null);
graphicsDevice.DepthStencilBuffer = savedDepthBuffer;
Texture2D texture = renderTarget.GetTexture();
if (dispose) {
renderTarget.Dispose();
renderTarget = null;
}
return texture;
}
}
}