-
Notifications
You must be signed in to change notification settings - Fork 45
/
SwapChainGraphicsPresenter.cs
79 lines (61 loc) · 2.55 KB
/
SwapChainGraphicsPresenter.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
using System.Numerics;
using Vortice.DirectX;
using Vortice.Direct3D12;
using Vortice.DXGI;
namespace DirectX12GameEngine.Graphics
{
public class SwapChainGraphicsPresenter : GraphicsPresenter
{
protected const int BufferCount = 2;
private readonly Texture[] renderTargets = new Texture[BufferCount];
public SwapChainGraphicsPresenter(GraphicsDevice device, PresentationParameters presentationParameters, IDXGISwapChain3 swapChain)
: base(device, presentationParameters)
{
SwapChain = swapChain;
if (GraphicsDevice.RenderTargetViewAllocator.DescriptorHeap.Description.DescriptorCount != BufferCount)
{
GraphicsDevice.RenderTargetViewAllocator.Dispose();
GraphicsDevice.RenderTargetViewAllocator = new DescriptorAllocator(GraphicsDevice, DescriptorHeapType.RenderTargetView, BufferCount);
}
CreateRenderTargets();
}
public override Texture BackBuffer => renderTargets[SwapChain.GetCurrentBackBufferIndex()];
public Matrix3x2 MatrixTransform { get => SwapChain.MatrixTransform; set => SwapChain.MatrixTransform = value; }
public override object NativePresenter => SwapChain;
protected IDXGISwapChain3 SwapChain { get; }
public override void Dispose()
{
SwapChain.Dispose();
foreach (Texture renderTarget in renderTargets)
{
renderTarget.Dispose();
}
base.Dispose();
}
public override void Present()
{
SwapChain.Present(PresentationParameters.SyncInterval, PresentFlags.None, PresentationParameters.PresentParameters);
}
protected override void ResizeBackBuffer(int width, int height)
{
for (int i = 0; i < BufferCount; i++)
{
renderTargets[i].Dispose();
}
SwapChain.ResizeBuffers(BufferCount, width, height, (Format)PresentationParameters.BackBufferFormat, SwapChainFlags.None);
CreateRenderTargets();
}
protected override void ResizeDepthStencilBuffer(int width, int height)
{
DepthStencilBuffer.Dispose();
DepthStencilBuffer = CreateDepthStencilBuffer();
}
private void CreateRenderTargets()
{
for (int i = 0; i < BufferCount; i++)
{
renderTargets[i] = new Texture(GraphicsDevice).InitializeFrom(SwapChain.GetBuffer<ID3D12Resource>(i));
}
}
}
}