forked from VESA-OSS/AdaptiveSyncTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeviceResources.h
221 lines (201 loc) · 7.19 KB
/
DeviceResources.h
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#pragma once
//#define FAVT // Turn on Fixed Average V-Total support
namespace DX
{
// Provides an interface for an application that owns DeviceResources to be notified of the device being lost or created.
interface IDeviceNotify
{
virtual void OnDeviceLost() = 0;
virtual void OnDeviceRestored() = 0;
};
// Controls all the DirectX device resources.
class DeviceResources
{
public:
DeviceResources(
DXGI_FORMAT backBufferFormat = DXGI_FORMAT_R16G16B16A16_FLOAT,
// DXGI_FORMAT backBufferFormat = DXGI_FORMAT_B8G8R8A8_UNORM,
DXGI_FORMAT depthBufferFormat = DXGI_FORMAT_D24_UNORM_S8_UINT,
UINT backBufferCount = 2,
D3D_FEATURE_LEVEL minFeatureLevel = D3D_FEATURE_LEVEL_11_0);
void CreateDeviceIndependentResources();
void CreateDeviceResources();
void CreateWindowSizeDependentResources();
void SetWindow(HWND window, int width, int height);
bool WindowSizeChanged(int width, int height);
void SetDpi(float dpi);
void HandleDeviceLost();
void RegisterDeviceNotify(IDeviceNotify* deviceNotify)
{
m_deviceNotify = deviceNotify;
}
HRESULT Present(UINT syncInterval, UINT flags);
void ChangeBackBufferFormat(DXGI_FORMAT fmt);
void SetVTotalMode(BOOL vTotalMode)
{
m_vTotalMode = vTotalMode;
} // Tell DevResources object when we are requesting v-total Fixed.
// The size of the render target, in pixels.
RECT GetOutputSize() const
{
return m_outputSize;
}
// The size of the render target, in dips.
D2D1_RECT_F GetLogicalSize() const
{
return m_logicalSize;
}
float GetDpi() const
{
return m_dpi;
}
// Direct3D Accessors.
ID3D11Device3* GetD3DDevice() const
{
return m_d3dDevice.Get();
}
ID3D11DeviceContext3* GetD3DDeviceContext() const
{
return m_d3dContext.Get();
}
IDXGISwapChain4* GetSwapChain() const
{
return m_swapChain.Get();
}
HANDLE GetFrameLatencyHandle() const
{
return m_frameLatencyHandle;
}
D3D_FEATURE_LEVEL GetDeviceFeatureLevel() const
{
return m_d3dFeatureLevel;
}
ID3D11Texture2D* GetRenderTarget() const
{
return m_renderTarget.Get();
}
ID3D11Texture2D* GetDepthStencil() const
{
return m_depthStencil.Get();
}
ID3D11RenderTargetView* GetRenderTargetView() const
{
return m_d3dRenderTargetView.Get();
}
ID3D11DepthStencilView* GetDepthStencilView() const
{
return m_d3dDepthStencilView.Get();
}
DXGI_FORMAT GetBackBufferFormat() const
{
return m_backBufferFormat;
}
DXGI_FORMAT GetDepthBufferFormat() const
{
return m_depthBufferFormat;
}
D3D11_VIEWPORT GetScreenViewport() const
{
return m_screenViewport;
}
UINT GetBackBufferCount() const
{
return m_backBufferCount;
}
// D2D Accessors.
ID2D1Factory3* GetD2DFactory() const
{
return m_d2dFactory.Get();
}
ID2D1Device2* GetD2DDevice() const
{
return m_d2dDevice.Get();
}
ID2D1DeviceContext2* GetD2DDeviceContext() const
{
return m_d2dContext.Get();
}
ID2D1Bitmap1* GetD2DTargetBitmap() const
{
return m_d2dTargetBitmap.Get();
}
IDWriteFactory3* GetDWriteFactory() const
{
return m_dwriteFactory.Get();
}
IWICImagingFactory2* GetWicImagingFactory() const
{
return m_wicFactory.Get();
}
// Performance events
void PIXBeginEvent(_In_z_ const wchar_t* name)
{
if (m_d3dAnnotation)
{
m_d3dAnnotation->BeginEvent(name);
}
}
void PIXEndEvent()
{
if (m_d3dAnnotation)
{
m_d3dAnnotation->EndEvent();
}
}
void PIXSetMarker(_In_z_ const wchar_t* name)
{
if (m_d3dAnnotation)
{
m_d3dAnnotation->SetMarker(name);
}
}
private:
void GetHardwareAdapter(IDXGIAdapter1** ppAdapter);
void GetPreferredAdapter(DXGI_GPU_PREFERENCE pref, IDXGIAdapter1** ppAdapter);
void UpdateLogicalSize(RECT outputSize, float dpi);
// Direct3D objects.
Microsoft::WRL::ComPtr<ID3D11Device3> m_d3dDevice;
Microsoft::WRL::ComPtr<ID3D11DeviceContext3> m_d3dContext;
Microsoft::WRL::ComPtr<IDXGISwapChain4> m_swapChain;
Microsoft::WRL::ComPtr<IDXGISwapChainMedia> m_mediaSwap;
Microsoft::WRL::ComPtr<ID3DUserDefinedAnnotation> m_d3dAnnotation;
HANDLE m_frameLatencyHandle;
BOOL m_vTotalMode; // True for a FIXED v-total average swapchain
// Direct3D rendering objects. Required for 3D.
Microsoft::WRL::ComPtr<ID3D11Texture2D> m_renderTarget;
Microsoft::WRL::ComPtr<ID3D11Texture2D> m_depthStencil;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> m_d3dRenderTargetView;
Microsoft::WRL::ComPtr<ID3D11DepthStencilView> m_d3dDepthStencilView;
D3D11_VIEWPORT m_screenViewport;
// Direct2D drawing components.
Microsoft::WRL::ComPtr<ID2D1Factory3> m_d2dFactory;
Microsoft::WRL::ComPtr<ID2D1Device2> m_d2dDevice;
Microsoft::WRL::ComPtr<ID2D1DeviceContext2> m_d2dContext;
Microsoft::WRL::ComPtr<ID2D1Bitmap1> m_d2dTargetBitmap;
// DirectWrite drawing components.
Microsoft::WRL::ComPtr<IDWriteFactory3> m_dwriteFactory;
Microsoft::WRL::ComPtr<IWICImagingFactory2> m_wicFactory;
// Direct3D properties.
DXGI_FORMAT m_backBufferFormat;
DXGI_FORMAT m_depthBufferFormat;
UINT m_backBufferCount;
// Cached device properties.
HWND m_window;
D3D_FEATURE_LEVEL m_d3dFeatureLevel;
RECT m_outputSize;
D2D1_RECT_F m_logicalSize;
float m_dpi;
// The IDeviceNotify can be held directly as it owns the DeviceResources.
IDeviceNotify* m_deviceNotify;
};
} // namespace DX