-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathDeviceResources.cpp
439 lines (361 loc) · 15.8 KB
/
DeviceResources.cpp
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//
// DeviceResources.cpp - A wrapper for the Direct3D 12.X device and swapchain
//
#include "pch.h"
#include "DeviceResources.h"
#include <XSystem.h>
using namespace DirectX;
using namespace DX;
#ifdef __clang__
#pragma clang diagnostic ignored "-Wcovered-switch-default"
#pragma clang diagnostic ignored "-Wswitch-enum"
#endif
#pragma warning(disable : 4061)
using Microsoft::WRL::ComPtr;
// Constructor for DeviceResources.
DeviceResources::DeviceResources(
DXGI_FORMAT backBufferFormat,
DXGI_FORMAT depthBufferFormat,
UINT backBufferCount,
unsigned int flags) noexcept(false) :
m_backBufferIndex(0),
m_fenceValue(0),
m_framePipelineToken{},
m_rtvDescriptorSize(0),
m_screenViewport{},
m_scissorRect{},
m_backBufferFormat(backBufferFormat),
m_depthBufferFormat(depthBufferFormat),
m_backBufferCount(backBufferCount),
m_clearColor{},
m_window(nullptr),
m_d3dFeatureLevel(D3D_FEATURE_LEVEL_12_0),
m_outputSize{0, 0, 1920, 1080},
m_options(flags)
{
if (backBufferCount < 2 || backBufferCount > MAX_BACK_BUFFER_COUNT)
{
throw std::out_of_range("invalid backBufferCount");
}
}
// Destructor for DeviceResources.
DeviceResources::~DeviceResources()
{
// Ensure that the GPU is no longer referencing resources that are about to be destroyed.
WaitForGpu();
// Ensure we present a blank screen before cleaning up resources.
if (m_commandQueue)
{
std::ignore = m_commandQueue->PresentX(0, nullptr, nullptr);
}
}
// Configures the Direct3D device, and stores handles to it and the device context.
void DeviceResources::CreateDeviceResources()
{
// Create the DX12 API device object.
D3D12XBOX_CREATE_DEVICE_PARAMETERS params = {};
params.Version = D3D12_SDK_VERSION;
#if defined(_DEBUG)
// Enable the debug layer.
params.ProcessDebugFlags = D3D12_PROCESS_DEBUG_FLAG_DEBUG_LAYER_ENABLED;
#elif defined(PROFILE)
// Enable the instrumented driver.
params.ProcessDebugFlags = D3D12XBOX_PROCESS_DEBUG_FLAG_INSTRUMENTED;
#endif
params.GraphicsCommandQueueRingSizeBytes = static_cast<UINT>(D3D12XBOX_DEFAULT_SIZE_BYTES);
params.DisableGeometryShaderAllocations = TRUE;
params.DisableTessellationShaderAllocations = TRUE;
#ifdef _GAMING_XBOX_SCARLETT
params.DisableDXR = TRUE;
params.CreateDeviceFlags = D3D12XBOX_CREATE_DEVICE_FLAG_NONE;
#endif
HRESULT hr = D3D12XboxCreateDevice(
nullptr,
¶ms,
IID_GRAPHICS_PPV_ARGS(m_d3dDevice.ReleaseAndGetAddressOf()));
#ifdef _DEBUG
if (hr == D3D12_ERROR_DRIVER_VERSION_MISMATCH)
{
#ifdef _GAMING_XBOX_SCARLETT
OutputDebugStringA("ERROR: Running a d3d12_xs.lib (Xbox Series X|S) linked binary on an Xbox One is not supported\n");
#else
OutputDebugStringA("ERROR: Running a d3d12_x.lib (Xbox One) linked binary on a Xbox Series X|S in 'Scarlett' mode is not supported\n");
#endif
}
#endif
ThrowIfFailed(hr);
m_d3dDevice->SetName(L"DeviceResources");
// Create the command queue.
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
ThrowIfFailed(m_d3dDevice->CreateCommandQueue(&queueDesc, IID_GRAPHICS_PPV_ARGS(m_commandQueue.ReleaseAndGetAddressOf())));
m_commandQueue->SetName(L"DeviceResources");
// Create descriptor heaps for render target views and depth stencil views.
D3D12_DESCRIPTOR_HEAP_DESC rtvDescriptorHeapDesc = {};
rtvDescriptorHeapDesc.NumDescriptors = m_backBufferCount;
rtvDescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
ThrowIfFailed(m_d3dDevice->CreateDescriptorHeap(&rtvDescriptorHeapDesc, IID_GRAPHICS_PPV_ARGS(m_rtvDescriptorHeap.ReleaseAndGetAddressOf())));
m_rtvDescriptorHeap->SetName(L"DeviceResources");
m_rtvDescriptorSize = m_d3dDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
if (m_depthBufferFormat != DXGI_FORMAT_UNKNOWN)
{
D3D12_DESCRIPTOR_HEAP_DESC dsvDescriptorHeapDesc = {};
dsvDescriptorHeapDesc.NumDescriptors = 1;
dsvDescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
ThrowIfFailed(m_d3dDevice->CreateDescriptorHeap(&dsvDescriptorHeapDesc, IID_GRAPHICS_PPV_ARGS(m_dsvDescriptorHeap.ReleaseAndGetAddressOf())));
m_dsvDescriptorHeap->SetName(L"DeviceResources");
}
// Create a command allocator for each back buffer that will be rendered to.
for (UINT n = 0; n < m_backBufferCount; n++)
{
ThrowIfFailed(m_d3dDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_GRAPHICS_PPV_ARGS(m_commandAllocators[n].ReleaseAndGetAddressOf())));
wchar_t name[25] = {};
swprintf_s(name, L"Render target %u", n);
m_commandAllocators[n]->SetName(name);
}
// Create a command list for recording graphics commands.
ThrowIfFailed(m_d3dDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_commandAllocators[0].Get(), nullptr, IID_GRAPHICS_PPV_ARGS(m_commandList.ReleaseAndGetAddressOf())));
ThrowIfFailed(m_commandList->Close());
m_commandList->SetName(L"DeviceResources");
// Create a fence for tracking GPU execution progress.
ThrowIfFailed(m_d3dDevice->CreateFence(m_fenceValue, D3D12_FENCE_FLAG_NONE, IID_GRAPHICS_PPV_ARGS(m_fence.ReleaseAndGetAddressOf())));
m_fenceValue++;
m_fence->SetName(L"DeviceResources");
m_fenceEvent.Attach(CreateEventEx(nullptr, nullptr, 0, EVENT_MODIFY_STATE | SYNCHRONIZE));
if (!m_fenceEvent.IsValid())
{
throw std::system_error(std::error_code(static_cast<int>(GetLastError()), std::system_category()), "CreateEventEx");
}
if (m_options & (c_Enable4K_UHD | c_EnableQHD))
{
switch (XSystemGetDeviceType())
{
case XSystemDeviceType::XboxOne:
case XSystemDeviceType::XboxOneS:
m_options &= ~(c_Enable4K_UHD | c_EnableQHD);
break;
case XSystemDeviceType::XboxScarlettLockhart /* Xbox Series S */:
m_options &= ~c_Enable4K_UHD;
if (m_options & c_EnableQHD)
{
m_outputSize = { 0, 0, 2560, 1440 };
}
break;
case XSystemDeviceType::XboxScarlettAnaconda /* Xbox Series X */:
case XSystemDeviceType::XboxOneXDevkit:
case XSystemDeviceType::XboxScarlettDevkit:
default:
m_outputSize = (m_options & c_Enable4K_UHD) ? RECT{ 0, 0, 3840, 2160 } : RECT{ 0, 0, 2560, 1440 };
break;
}
}
#ifdef _DEBUG
const char* info = nullptr;
switch (m_outputSize.bottom)
{
case 2160: info = "INFO: Swapchain using 4k (3840 x 2160)\n"; break;
case 1440: info = "INFO: Swapchain using 1440p (2560 x 1440)\n"; break;
default: info = "INFO: Swapchain using 1080p (1920 x 1080)\n"; break;
}
OutputDebugStringA(info);
#endif
RegisterFrameEvents();
}
// These resources need to be recreated every time the window size is changed.
void DeviceResources::CreateWindowSizeDependentResources()
{
if (!m_window)
{
throw std::logic_error("Call SetWindow with a valid Win32 window handle");
}
// Wait until all previous GPU work is complete.
WaitForGpu();
// Ensure we present a blank screen before cleaning up resources.
ThrowIfFailed(m_commandQueue->PresentX(0, nullptr, nullptr));
// Release resources that are tied to the swap chain and update fence values.
for (UINT n = 0; n < m_backBufferCount; n++)
{
m_renderTargets[n].Reset();
}
// Determine the render target size in pixels.
const UINT backBufferWidth = std::max<UINT>(static_cast<UINT>(m_outputSize.right - m_outputSize.left), 1u);
const UINT backBufferHeight = std::max<UINT>(static_cast<UINT>(m_outputSize.bottom - m_outputSize.top), 1u);
// Obtain the back buffers for this window which will be the final render targets
// and create render target views for each of them.
const CD3DX12_HEAP_PROPERTIES swapChainHeapProperties(D3D12_HEAP_TYPE_DEFAULT);
D3D12_RESOURCE_DESC swapChainBufferDesc = CD3DX12_RESOURCE_DESC::Tex2D(
m_backBufferFormat,
backBufferWidth,
backBufferHeight,
1, // This resource has only one texture.
1 // Use a single mipmap level.
);
swapChainBufferDesc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
const CD3DX12_CLEAR_VALUE swapChainOptimizedClearValue(m_backBufferFormat, m_clearColor);
for (UINT n = 0; n < m_backBufferCount; n++)
{
ThrowIfFailed(m_d3dDevice->CreateCommittedResource(
&swapChainHeapProperties,
D3D12_HEAP_FLAG_ALLOW_DISPLAY,
&swapChainBufferDesc,
D3D12_RESOURCE_STATE_PRESENT,
&swapChainOptimizedClearValue,
IID_GRAPHICS_PPV_ARGS(m_renderTargets[n].GetAddressOf())));
wchar_t name[25] = {};
swprintf_s(name, L"Render target %u", n);
m_renderTargets[n]->SetName(name);
D3D12_RENDER_TARGET_VIEW_DESC rtvDesc = {};
rtvDesc.Format = m_backBufferFormat;
rtvDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
const CD3DX12_CPU_DESCRIPTOR_HANDLE rtvDescriptor(
m_rtvDescriptorHeap->GetCPUDescriptorHandleForHeapStart(),
static_cast<INT>(n), m_rtvDescriptorSize);
m_d3dDevice->CreateRenderTargetView(m_renderTargets[n].Get(), &rtvDesc, rtvDescriptor);
}
// Reset the index to the current back buffer.
m_backBufferIndex = 0;
if (m_depthBufferFormat != DXGI_FORMAT_UNKNOWN)
{
// Allocate a 2-D surface as the depth/stencil buffer and create a depth/stencil view
// on this surface.
const CD3DX12_HEAP_PROPERTIES depthHeapProperties(D3D12_HEAP_TYPE_DEFAULT);
D3D12_RESOURCE_DESC depthStencilDesc = CD3DX12_RESOURCE_DESC::Tex2D(
m_depthBufferFormat,
backBufferWidth,
backBufferHeight,
1, // Use a single array entry.
1 // Use a single mipmap level.
);
depthStencilDesc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
const CD3DX12_CLEAR_VALUE depthOptimizedClearValue(m_depthBufferFormat, (m_options & c_ReverseDepth) ? 0.0f : 1.0f, 0u);
ThrowIfFailed(m_d3dDevice->CreateCommittedResource(
&depthHeapProperties,
D3D12_HEAP_FLAG_NONE,
&depthStencilDesc,
D3D12_RESOURCE_STATE_DEPTH_WRITE,
&depthOptimizedClearValue,
IID_GRAPHICS_PPV_ARGS(m_depthStencil.ReleaseAndGetAddressOf())
));
m_depthStencil->SetName(L"Depth stencil");
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = {};
dsvDesc.Format = m_depthBufferFormat;
dsvDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
m_d3dDevice->CreateDepthStencilView(m_depthStencil.Get(), &dsvDesc, m_dsvDescriptorHeap->GetCPUDescriptorHandleForHeapStart());
}
// Set the 3D rendering viewport and scissor rectangle to target the entire window.
m_screenViewport.TopLeftX = m_screenViewport.TopLeftY = 0.f;
m_screenViewport.Width = static_cast<float>(backBufferWidth);
m_screenViewport.Height = static_cast<float>(backBufferHeight);
m_screenViewport.MinDepth = D3D12_MIN_DEPTH;
m_screenViewport.MaxDepth = D3D12_MAX_DEPTH;
m_scissorRect.left = m_scissorRect.top = 0;
m_scissorRect.right = static_cast<LONG>(backBufferWidth);
m_scissorRect.bottom = static_cast<LONG>(backBufferHeight);
}
// Prepare the command list and render target for rendering.
void DeviceResources::Prepare(D3D12_RESOURCE_STATES beforeState, D3D12_RESOURCE_STATES afterState)
{
// Reset command list and allocator.
ThrowIfFailed(m_commandAllocators[m_backBufferIndex]->Reset());
ThrowIfFailed(m_commandList->Reset(m_commandAllocators[m_backBufferIndex].Get(), nullptr));
if (beforeState != afterState)
{
// Transition the render target into the correct state to allow for drawing into it.
const D3D12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(
m_renderTargets[m_backBufferIndex].Get(),
beforeState, afterState);
m_commandList->ResourceBarrier(1, &barrier);
}
}
// Present the contents of the swap chain to the screen.
void DeviceResources::Present(D3D12_RESOURCE_STATES beforeState, _In_opt_ const D3D12XBOX_PRESENT_PARAMETERS* params)
{
if (beforeState != D3D12_RESOURCE_STATE_PRESENT)
{
// Transition the render target to the state that allows it to be presented to the display.
const D3D12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(
m_renderTargets[m_backBufferIndex].Get(),
beforeState, D3D12_RESOURCE_STATE_PRESENT);
m_commandList->ResourceBarrier(1, &barrier);
}
// Send the command list off to the GPU for processing.
ThrowIfFailed(m_commandList->Close());
m_commandQueue->ExecuteCommandLists(1, CommandListCast(m_commandList.GetAddressOf()));
// Present the backbuffer using the PresentX API.
D3D12XBOX_PRESENT_PLANE_PARAMETERS planeParameters = {};
planeParameters.Token = m_framePipelineToken;
planeParameters.ResourceCount = 1;
planeParameters.ppResources = m_renderTargets[m_backBufferIndex].GetAddressOf();
ThrowIfFailed(
m_commandQueue->PresentX(1, &planeParameters, params)
);
// Xbox apps do not need to handle DXGI_ERROR_DEVICE_REMOVED or DXGI_ERROR_DEVICE_RESET.
// Update the back buffer index.
m_backBufferIndex = (m_backBufferIndex + 1) % m_backBufferCount;
}
// Handle GPU suspend/resume
void DeviceResources::Suspend()
{
m_commandQueue->SuspendX(0);
}
void DeviceResources::Resume()
{
m_commandQueue->ResumeX();
RegisterFrameEvents();
}
// Wait for pending GPU work to complete.
void DeviceResources::WaitForGpu() noexcept
{
if (m_commandQueue && m_fence && m_fenceEvent.IsValid())
{
// Schedule a Signal command in the GPU queue.
const UINT64 fenceValue = m_fenceValue;
if (SUCCEEDED(m_commandQueue->Signal(m_fence.Get(), fenceValue)))
{
// Wait until the Signal has been processed.
if (SUCCEEDED(m_fence->SetEventOnCompletion(fenceValue, m_fenceEvent.Get())))
{
std::ignore = WaitForSingleObjectEx(m_fenceEvent.Get(), INFINITE, FALSE);
// Increment the fence value for the current frame.
m_fenceValue++;
}
}
}
}
// For PresentX rendering, we should wait for the origin event just before processing input.
void DeviceResources::WaitForOrigin()
{
// Wait until frame start is signaled
m_framePipelineToken = D3D12XBOX_FRAME_PIPELINE_TOKEN_NULL;
ThrowIfFailed(m_d3dDevice->WaitFrameEventX(
D3D12XBOX_FRAME_EVENT_ORIGIN,
INFINITE,
nullptr,
D3D12XBOX_WAIT_FRAME_EVENT_FLAG_NONE,
&m_framePipelineToken));
}
// Set frame interval and register for frame events
void DeviceResources::RegisterFrameEvents()
{
// First, retrieve the underlying DXGI device from the D3D device.
ComPtr<IDXGIDevice1> dxgiDevice;
ThrowIfFailed(m_d3dDevice.As(&dxgiDevice));
// Identify the physical adapter (GPU or card) this device is running on.
ComPtr<IDXGIAdapter> dxgiAdapter;
ThrowIfFailed(dxgiDevice->GetAdapter(dxgiAdapter.GetAddressOf()));
// Retrieve the outputs for the adapter.
ComPtr<IDXGIOutput> dxgiOutput;
ThrowIfFailed(dxgiAdapter->EnumOutputs(0, dxgiOutput.GetAddressOf()));
// Set frame interval and register for frame events
ThrowIfFailed(m_d3dDevice->SetFrameIntervalX(
dxgiOutput.Get(),
D3D12XBOX_FRAME_INTERVAL_60_HZ,
m_backBufferCount - 1u /* Allow n-1 frames of latency */,
D3D12XBOX_FRAME_INTERVAL_FLAG_NONE));
ThrowIfFailed(m_d3dDevice->ScheduleFrameEventX(
D3D12XBOX_FRAME_EVENT_ORIGIN,
0U,
nullptr,
D3D12XBOX_SCHEDULE_FRAME_EVENT_FLAG_NONE));
}