forked from microsoft/DirectML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectMLSuperResolution.h
329 lines (274 loc) · 12.2 KB
/
DirectMLSuperResolution.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//--------------------------------------------------------------------------------------
// DirectMLSuperResolution.h
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. Copyright (C) NVIDIA Corporation. All rights reserved.
// Licensed under the MIT License.
//--------------------------------------------------------------------------------------
#pragma once
#include "DeviceResources.h"
#include "StepTimer.h"
#include "LoadWeights.h"
#include "MediaEnginePlayer.h"
// Force the default NCHW (batch/channels/height/width) tensor format, instead of determining
// this based on the GPU vendor. Setting this may help run on older Nvidia hardware.
#define FORCE_NCHW 0
// Use video frames as input to the DirectML model, instead of a static texture.
#define USE_VIDEO 1
// Let DirectML manage the data in the weight tensors. This can be faster on some hardware.
#define DML_MANAGED_WEIGHTS 1
class SmoothedFPS
{
public:
SmoothedFPS(float secondsInterval = 1.f)
{
Initialize(secondsInterval);
}
void Initialize(float secondsInterval = 1.f)
{
m_secondsInterval = secondsInterval;
m_timeAccumulator = 0.0f;
m_frameAccumulator = 0;
m_smoothedFPS = 0.0f;
}
void Tick(float DeltaTime)
{
m_timeAccumulator += DeltaTime;
++m_frameAccumulator;
if (m_timeAccumulator >= m_secondsInterval)
{
m_smoothedFPS = (float)m_frameAccumulator / m_timeAccumulator;
m_timeAccumulator = 0.0f;
m_frameAccumulator = 0;
}
}
float GetFPS() const { return m_smoothedFPS; }
private:
float m_smoothedFPS;
float m_timeAccumulator;
uint32_t m_frameAccumulator;
float m_secondsInterval;
};
enum class TensorLayout
{
Default,
NHWC
};
// A basic sample implementation that creates a D3D12 device and
// provides a render loop.
class Sample final : public DX::IDeviceNotify
{
public:
Sample() noexcept(false);
~Sample();
// Initialization and management
void Initialize(HWND window, int width, int height);
// Basic render loop
void Tick();
// IDeviceNotify
virtual void OnDeviceLost() override;
virtual void OnDeviceRestored() override;
// Messages
void OnActivated();
void OnDeactivated();
void OnSuspending();
void OnResuming();
void OnWindowMoved();
void OnWindowSizeChanged(int width, int height);
// Properties
void GetDefaultSize( int& width, int& height ) const;
private:
void Update(DX::StepTimer const& timer);
void Render();
void UpdateZoomVertexBuffer();
void Clear();
void CreateDeviceDependentResources();
void CreateTextureResources();
void CreateDirectMLResources();
void InitializeDirectMLResources();
void CreateUIResources();
void GetStrides(
_In_reads_(4) const uint32_t* sizes,
TensorLayout layout,
_Out_writes_(4) uint32_t* stridesOut
);
void CreateUpsampleLayer(
_In_reads_(4) const uint32_t* inputSizes,
_Inout_updates_(1) uint64_t* inputBufferRequiredSize,
_Inout_updates_(1) uint64_t* outputBufferRequiredSize,
_Out_writes_(4) uint32_t* outputSizesOut,
_Out_writes_(1) IDMLCompiledOperator** compiledOpOut);
void CreateConvolutionLayer(
_In_reads_(4) const uint32_t* inputSizes,
_In_reads_(4) const uint32_t* filterSizes,
bool useBiasAndActivation,
_Inout_updates_(1) uint64_t* inputBufferRequiredSize,
_Inout_updates_(1) uint64_t* outputBufferRequiredSize,
_Out_writes_(4) uint32_t* outputSizesOut,
_Out_writes_(1) IDMLCompiledOperator** compiledOpOut);
void CreateWeightTensors(
WeightMapType& weights,
const char* convLayerName,
const char* scaleLayerName,
const char* shiftLayerName,
dml::Span<const uint32_t> filterSizes,
DirectX::ResourceUploadBatch& uploadBatch,
_Out_writes_(1) ID3D12Resource** filterWeightResourceOut,
_Out_writes_opt_(1) ID3D12Resource** biasWeightResourceOut);
void CreateWeightResource(
_In_reads_(4) const uint32_t* tensorSizes,
_Out_writes_(1) ID3D12Resource** d3dResourceOut);
void CreateWindowSizeDependentResources();
void BindTempResourceIfNeeded(
DML_BINDING_PROPERTIES& bindingProps,
_In_reads_(1) IDMLBindingTable* initBindingTable,
_Out_writes_opt_(1) ID3D12Resource** tempResource);
// DirectML method for setting up Tensors and creating operators
#if !(USE_DMLX)
void CreateAdditionLayer(
_In_reads_(4) const uint32_t* inputSizes,
_Out_writes_(1) IDMLCompiledOperator** compiledOpOut);
#endif
// Device resources
std::unique_ptr<DX::DeviceResources> m_deviceResources;
// Rendering loop timer
DX::StepTimer m_timer;
// Input devices
std::unique_ptr<DirectX::GamePad> m_gamePad;
std::unique_ptr<DirectX::Keyboard> m_keyboard;
DirectX::GamePad::ButtonStateTracker m_gamePadButtons;
DirectX::Keyboard::KeyboardStateTracker m_keyboardButtons;
bool m_ctrlConnected;
// DirectXTK objects
std::unique_ptr<DirectX::GraphicsMemory> m_graphicsMemory;
std::unique_ptr<DirectX::DescriptorHeap> m_SRVDescriptorHeap;
std::unique_ptr<DirectX::DescriptorHeap> m_RTVDescriptorHeap;
// UI
SmoothedFPS m_fps;
std::unique_ptr<DirectX::BasicEffect> m_lineEffect;
std::unique_ptr<DirectX::PrimitiveBatch<DirectX::VertexPositionColor>> m_lineBatch;
std::unique_ptr<DirectX::DescriptorHeap> m_fontDescriptorHeap;
std::unique_ptr<DirectX::SpriteBatch> m_spriteBatch;
std::unique_ptr<DirectX::SpriteFont> m_labelFont;
std::unique_ptr<DirectX::SpriteFont> m_labelFontBold;
std::unique_ptr<DirectX::SpriteFont> m_legendFont;
std::unique_ptr<DirectX::SpriteFont> m_ctrlFont;
// Video player
std::unique_ptr<MediaEnginePlayer> m_player;
HANDLE m_sharedVideoTexture;
// Direct3D 12 objects for rendering texture to screen
Microsoft::WRL::ComPtr<ID3D12RootSignature> m_texRootSignatureNN; // Nearest-neighbor texture upscale
Microsoft::WRL::ComPtr<ID3D12PipelineState> m_texPipelineStateNN;
Microsoft::WRL::ComPtr<ID3D12RootSignature> m_texRootSignatureLinear; // Bilinear texture upscale
Microsoft::WRL::ComPtr<ID3D12PipelineState> m_texPipelineStateLinear;
Microsoft::WRL::ComPtr<ID3D12RootSignature> m_tensorRenderRootSignature; // Render from DML tensor format to texture
Microsoft::WRL::ComPtr<ID3D12PipelineState> m_tensorRenderPipelineState;
Microsoft::WRL::ComPtr<ID3D12Resource> m_texture; // Static input texture to render, if USE_VIDEO == 0
Microsoft::WRL::ComPtr<ID3D12Resource> m_videoTexture; // Input video frame to render, if USE_VIDEO == 1
Microsoft::WRL::ComPtr<ID3D12Resource> m_finalResultTexture; // Upscaled 4K texture output
uint32_t m_origTextureHeight;
uint32_t m_origTextureWidth;
Microsoft::WRL::ComPtr<ID3D12Resource> m_vertexBuffer;
Microsoft::WRL::ComPtr<ID3D12Resource> m_indexBuffer;
D3D12_VERTEX_BUFFER_VIEW m_vertexBufferView;
D3D12_INDEX_BUFFER_VIEW m_indexBufferView;
// Additional D3D12 objects for rendering zoomed picture-in-picture window
DirectX::SharedGraphicsResource m_zoomedVertexHeap;
D3D12_VERTEX_BUFFER_VIEW m_zoomedVertexBufferView;
// Compute objects for converting texture to DML tensor format
Microsoft::WRL::ComPtr<ID3D12PipelineState> m_computePSO;
Microsoft::WRL::ComPtr<ID3D12RootSignature> m_computeRootSignature;
// DirectML objects
Microsoft::WRL::ComPtr<IDMLDevice> m_dmlDevice;
Microsoft::WRL::ComPtr<IDMLCommandRecorder> m_dmlCommandRecorder;
TensorLayout m_tensorLayout;
// Model layer sizes and indices
static const size_t c_numUpsampleLayers = 2;
static const size_t c_numConvLayers = 7;
static const size_t c_numIntermediateBuffers = 2;
enum OpTypes : uint32_t
{
e_opUpsample,
e_opConv,
e_opAdd,
e_opCount
};
// Shared Resources
std::unique_ptr<DirectX::DescriptorHeap> m_dmlDescriptorHeap;
Microsoft::WRL::ComPtr<ID3D12Resource> m_modelInput;
Microsoft::WRL::ComPtr<ID3D12Resource> m_modelOutput;
// DirectML Model Resources
#if !(USE_DMLX)
Microsoft::WRL::ComPtr<ID3D12Resource> m_modelIntermediateResult[c_numIntermediateBuffers];
Microsoft::WRL::ComPtr<ID3D12Resource> m_modelUpsamplePersistentResources[c_numUpsampleLayers];
Microsoft::WRL::ComPtr<ID3D12Resource> m_modelConvPersistentResources[c_numConvLayers];
Microsoft::WRL::ComPtr<ID3D12Resource> m_modelAddPersistentResource;
Microsoft::WRL::ComPtr<ID3D12Resource> m_modelInitTemporaryResources[e_opCount];
Microsoft::WRL::ComPtr<ID3D12Resource> m_modelUpsampleTemporaryResources[c_numUpsampleLayers];
Microsoft::WRL::ComPtr<ID3D12Resource> m_modelConvTemporaryResources[c_numConvLayers];
Microsoft::WRL::ComPtr<ID3D12Resource> m_modelAddTemporaryResource;
#endif
// DirectMLX Model Resources
Microsoft::WRL::ComPtr<ID3D12Resource> m_modelConvFilterWeights[c_numConvLayers];
Microsoft::WRL::ComPtr<ID3D12Resource> m_modelConvBiasWeights[c_numConvLayers];
Microsoft::WRL::ComPtr<ID3D12Resource> m_modelPersistentResource;
Microsoft::WRL::ComPtr<ID3D12Resource> m_modelTemporaryResource;
// DirectML operations
#if !(USE_DMLX)
Microsoft::WRL::ComPtr<IDMLCompiledOperator> m_dmlUpsampleOps[c_numUpsampleLayers];
Microsoft::WRL::ComPtr<IDMLBindingTable> m_dmlUpsampleBindings[c_numUpsampleLayers];
Microsoft::WRL::ComPtr<IDMLCompiledOperator> m_dmlConvOps[c_numConvLayers];
Microsoft::WRL::ComPtr<IDMLBindingTable> m_dmlConvBindings[c_numConvLayers];
Microsoft::WRL::ComPtr<IDMLCompiledOperator> m_dmlAddResidualOp;
Microsoft::WRL::ComPtr<IDMLBindingTable> m_dmlAddResidualBinding;
Microsoft::WRL::ComPtr<IDMLOperatorInitializer> m_dmlOpInitializers[e_opCount];
#endif
// DirectMLX operations
Microsoft::WRL::ComPtr<IDMLCompiledOperator> m_dmlGraph;
Microsoft::WRL::ComPtr<IDMLBindingTable> m_dmlBindingTable;
Microsoft::WRL::ComPtr<IDMLOperatorInitializer> m_dmlOpInitializer;
// Application state
bool m_useDml;
bool m_showPip;
float m_zoomX;
float m_zoomY;
float m_zoomWindowSize;
bool m_zoomUpdated;
const float c_minZoom = 0.005f;
const float c_maxZoom = 0.05f;
// DirectX index enums
enum SrvDescriptors : uint32_t
{
e_descTexture,
e_descModelInput,
e_descModelOutput,
e_descFinalResultTextureSrv,
e_srvDescCount
};
enum RtvDescriptors : uint32_t
{
e_descFinalResultTextureRtv,
e_rtvDescCount
};
enum FontDescriptors : uint32_t
{
e_descLabelFont,
e_descLabelFontBold,
e_descLegendFont,
e_descCtrlFont,
e_fontDescCount,
};
enum ComputeRootParameters : uint32_t
{
e_crpIdxCB = 0,
e_crpIdxSRV,
e_crpIdxUAV,
e_crpIdxCount
};
enum TensorRenderRootParameters : uint32_t
{
e_rrpIdxCB = 0,
e_rrpIdxSRV,
e_rrpIdxCount
};
};