-
Notifications
You must be signed in to change notification settings - Fork 10
/
DeviceResources.cpp
603 lines (512 loc) · 22.5 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//*********************************************************
//
// 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.
//
//*********************************************************
#include "pch.h"
#include "DeviceResources.h"
using namespace DirectX;
using Microsoft::WRL::ComPtr;
namespace
{
#if defined(_DEBUG)
// Check for SDK Layer support.
inline bool SdkLayersAvailable()
{
HRESULT hr = D3D11CreateDevice(nullptr,
D3D_DRIVER_TYPE_NULL, // There is no need to create a real hardware device.
0,
D3D11_CREATE_DEVICE_DEBUG, // Check for the SDK layers.
nullptr, // Any feature level will do.
0,
D3D11_SDK_VERSION,
nullptr, // No need to keep the D3D device reference.
nullptr, // No need to know the feature level.
nullptr // No need to keep the D3D device context reference.
);
return SUCCEEDED(hr);
}
#endif
}; // namespace
// Constructor for DeviceResources.
DX::DeviceResources::DeviceResources(DXGI_FORMAT backBufferFormat,
DXGI_FORMAT depthBufferFormat,
UINT backBufferCount,
D3D_FEATURE_LEVEL minFeatureLevel)
: m_screenViewport{}
, m_backBufferFormat(backBufferFormat)
, m_depthBufferFormat(depthBufferFormat)
, m_backBufferCount(backBufferCount)
, m_window(0)
, m_d3dFeatureLevel(D3D_FEATURE_LEVEL_9_1)
, m_outputSize{0, 0, 1, 1}
, m_deviceNotify(nullptr)
, m_dpi(-1.0f)
, // Require DPI to be explicitly set.
m_vTotalMode(false)
{
UNREFERENCED_PARAMETER(minFeatureLevel);
CreateDeviceIndependentResources();
}
// Configures resources that don't depend on the Direct3D device.
void DX::DeviceResources::CreateDeviceIndependentResources()
{
// Initialize Direct2D resources.
D2D1_FACTORY_OPTIONS options;
ZeroMemory(&options, sizeof(D2D1_FACTORY_OPTIONS));
#if defined(_DEBUG)
// If the project is in a debug build, enable Direct2D debugging via SDK Layers.
options.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;
#endif
// Initialize the Direct2D Factory.
DX::ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory5), &options, &m_d2dFactory));
// Initialize the DirectWrite Factory.
DX::ThrowIfFailed(DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory3), &m_dwriteFactory));
// Initialize the Windows Imaging Component (WIC) Factory.
DX::ThrowIfFailed(CoCreateInstance(CLSID_WICImagingFactory2, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&m_wicFactory)));
}
// Configures the Direct3D device, and stores handles to it and the device context.
void DX::DeviceResources::CreateDeviceResources()
{
UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#if defined(_DEBUG)
if (SdkLayersAvailable())
{
// If the project is in a debug build, enable debugging via SDK Layers with this flag.
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
}
else
{
OutputDebugStringA("WARNING: Direct3D Debug Device is not available\n");
}
#endif
// Determine DirectX hardware feature levels this app will support.
static const D3D_FEATURE_LEVEL s_featureLevels[] = {
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1,
};
ComPtr<IDXGIAdapter1> adapter;
GetHardwareAdapter(adapter.GetAddressOf());
// GetPreferredAdapter( DXGI_GPU_PREFERENCE_MINIMUM_POWER, adapter.GetAddressOf()); // Can't find an output6/display off this device
// GetPreferredAdapter( DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, adapter.GetAddressOf());
// GetPreferredAdapter( DXGI_GPU_PREFERENCE_UNSPECIFIED, adapter.GetAddressOf());
// Create the Direct3D 11 API device object and a corresponding context.
ComPtr<ID3D11Device> device;
ComPtr<ID3D11DeviceContext> context;
HRESULT hr = E_FAIL;
if (adapter)
{
hr = D3D11CreateDevice(adapter.Get(),
D3D_DRIVER_TYPE_UNKNOWN,
0,
creationFlags,
s_featureLevels,
ARRAYSIZE(s_featureLevels),
D3D11_SDK_VERSION,
&device, // Returns the Direct3D device created.
&m_d3dFeatureLevel, // Returns feature level of device created.
&context // Returns the device immediate context.
);
}
#if defined(NDEBUG)
else
{
throw std::exception("No Direct3D hardware device found");
}
#else
if (FAILED(hr))
{
// If the initialization fails, fall back to the WARP device.
// For more information on WARP, see:
// http://go.microsoft.com/fwlink/?LinkId=286690
DX::ThrowIfFailed(D3D11CreateDevice(nullptr,
D3D_DRIVER_TYPE_WARP, // Create a WARP device instead of a hardware device.
0,
creationFlags,
s_featureLevels,
ARRAYSIZE(s_featureLevels),
D3D11_SDK_VERSION,
&device,
&m_d3dFeatureLevel,
&context));
if (SUCCEEDED(hr))
{
OutputDebugStringA("Direct3D Adapter - WARP\n");
}
}
#endif
// Store pointers to the Direct3D 11.3 API device and immediate context.
DX::ThrowIfFailed(device.As(&m_d3dDevice));
DX::ThrowIfFailed(context.As(&m_d3dContext));
#ifndef NDEBUG
ComPtr<ID3D11Debug> d3dDebug;
if (SUCCEEDED(m_d3dDevice.As(&d3dDebug)))
{
ComPtr<ID3D11InfoQueue> d3dInfoQueue;
if (SUCCEEDED(d3dDebug.As(&d3dInfoQueue)))
{
#ifdef _DEBUG
d3dInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, true);
d3dInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, true);
#endif
D3D11_MESSAGE_ID hide[] = {
D3D11_MESSAGE_ID_SETPRIVATEDATA_CHANGINGPARAMS,
};
D3D11_INFO_QUEUE_FILTER filter = {};
filter.DenyList.NumIDs = _countof(hide);
filter.DenyList.pIDList = hide;
d3dInfoQueue->AddStorageFilterEntries(&filter);
}
}
#endif
// Create the Direct2D device object and a corresponding context.
ComPtr<IDXGIDevice3> dxgiDevice;
DX::ThrowIfFailed(m_d3dDevice.As(&dxgiDevice));
DX::ThrowIfFailed(m_d2dFactory->CreateDevice(dxgiDevice.Get(), &m_d2dDevice));
DX::ThrowIfFailed(m_d2dDevice->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &m_d2dContext));
}
// These resources need to be recreated every time the window size is changed.
void DX::DeviceResources::CreateWindowSizeDependentResources()
{
if (!m_window)
{
throw std::exception("Call SetWindow with a valid Win32 window handle");
}
// Clear the previous window size specific context.
ID3D11RenderTargetView* nullViews[] = {nullptr};
m_d3dContext->OMSetRenderTargets(_countof(nullViews), nullViews, nullptr);
m_d3dRenderTargetView.Reset();
m_d3dDepthStencilView.Reset();
m_renderTarget.Reset();
m_d2dContext->SetTarget(nullptr);
m_d2dTargetBitmap.Reset();
m_depthStencil.Reset();
m_d3dContext->Flush(); // Why can't I call Flush1?
// Determine the render target size in pixels.
UINT backBufferWidth = std::max<UINT>(m_outputSize.right - m_outputSize.left, 1);
UINT backBufferHeight = std::max<UINT>(m_outputSize.bottom - m_outputSize.top, 1);
UINT swapFlags = 0; // Present Flags for swapchain resize call
if (m_swapChain)
{
if (m_vTotalMode) // if user is requesting V-TotalAverage to be FIXED
{
swapFlags = DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT;
}
else
{
swapFlags = 0; // default is vsync on, not tearing
// swapFlags = DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING; // It is recommended to always use the tearing flag when it is available.
// swapChainDesc.Flags = m_tearingSupport ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0; TODO make this not hard-coded -then older systems may work
}
// If the swap chain already exists, resize it.
HRESULT hr = m_swapChain->ResizeBuffers(m_backBufferCount, backBufferWidth, backBufferHeight, m_backBufferFormat, swapFlags);
if (hr == DXGI_ERROR_DEVICE_REMOVED || hr == DXGI_ERROR_DEVICE_RESET)
{
#ifdef _DEBUG
char buff[64] = {};
sprintf_s(buff,
"Device Lost on ResizeBuffers: Reason code 0x%08X\n",
(hr == DXGI_ERROR_DEVICE_REMOVED) ? m_d3dDevice->GetDeviceRemovedReason() : hr);
OutputDebugStringA(buff);
#endif
// If the device was removed for any reason, a new device and swap chain will need to be created.
HandleDeviceLost();
// Everything is set up now. Do not continue execution of this method. HandleDeviceLost will reenter this method
// and correctly set up the new device.
return;
}
else
{
DX::ThrowIfFailed(hr);
}
}
else
{
// Otherwise, create a new one using the same adapter as the existing Direct3D device.
// This sequence obtains the DXGI factory that was used to create the Direct3D device above.
ComPtr<IDXGIDevice3> dxgiDevice;
DX::ThrowIfFailed(m_d3dDevice.As(&dxgiDevice));
ComPtr<IDXGIAdapter> dxgiAdapter;
DX::ThrowIfFailed(dxgiDevice->GetAdapter(&dxgiAdapter));
ComPtr<IDXGIFactory4> dxgiFactory;
DX::ThrowIfFailed(dxgiAdapter->GetParent(IID_PPV_ARGS(&dxgiFactory)));
// Requires DirectX 11.1 or later
DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
swapChainDesc.Width = backBufferWidth;
swapChainDesc.Height = backBufferHeight;
swapChainDesc.Format = m_backBufferFormat;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = m_backBufferCount;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.Scaling = DXGI_SCALING_STRETCH;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
// swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
swapChainDesc.AlphaMode = DXGI_ALPHA_MODE_IGNORE;
if (m_vTotalMode) // if user is requesting V-TotalAverage to be FIXED
{
swapFlags = DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT;
}
else
{
swapFlags = 0; // default is vsync enabled, not tearing
// swapFlags = DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING; // It is recommended to always use the tearing flag when it is available.
// swapChainDesc.Flags = m_tearingSupport ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0; TODO make this not hard-coded -then older systems may work
}
swapChainDesc.Flags = swapFlags;
// Create a SwapChain from a Win32 window.
ComPtr<IDXGISwapChain1> swapChain1;
DX::ThrowIfFailed(
dxgiFactory->CreateSwapChainForHwnd(m_d3dDevice.Get(), m_window, &swapChainDesc, nullptr, nullptr, swapChain1.ReleaseAndGetAddressOf()));
DX::ThrowIfFailed(swapChain1.As(&m_swapChain));
// This class does not support exclusive full-screen mode and prevents DXGI from responding to the ALT+ENTER shortcut
DX::ThrowIfFailed(dxgiFactory->MakeWindowAssociation(m_window, DXGI_MWA_NO_ALT_ENTER));
}
if (m_vTotalMode) // if user is requesting V-TotalAverage to be FIXED
{
// swapchain is now created or resized
m_swapChain->SetMaximumFrameLatency(1); // only valid in PresentDuration mode
// need to update latency handle when it changes like on swapchain create or resize.
m_frameLatencyHandle = m_swapChain->GetFrameLatencyWaitableObject();
}
// Create a render target view of the swap chain back buffer.
DX::ThrowIfFailed(m_swapChain->GetBuffer(0, IID_PPV_ARGS(m_renderTarget.ReleaseAndGetAddressOf())));
DX::ThrowIfFailed(m_d3dDevice->CreateRenderTargetView(m_renderTarget.Get(), nullptr, m_d3dRenderTargetView.ReleaseAndGetAddressOf()));
if (m_depthBufferFormat != DXGI_FORMAT_UNKNOWN)
{
// Create a depth stencil view for use with 3D rendering if needed.
CD3D11_TEXTURE2D_DESC depthStencilDesc(m_depthBufferFormat,
backBufferWidth,
backBufferHeight,
1, // This depth stencil view has only one texture.
1, // Use a single mipmap level.
D3D11_BIND_DEPTH_STENCIL);
DX::ThrowIfFailed(m_d3dDevice->CreateTexture2D(&depthStencilDesc, nullptr, m_depthStencil.ReleaseAndGetAddressOf()));
CD3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc(D3D11_DSV_DIMENSION_TEXTURE2D);
DX::ThrowIfFailed(
m_d3dDevice->CreateDepthStencilView(m_depthStencil.Get(), &depthStencilViewDesc, m_d3dDepthStencilView.ReleaseAndGetAddressOf()));
}
// Set the 3D rendering viewport to target the entire window.
m_screenViewport = CD3D11_VIEWPORT(0.0f, 0.0f, static_cast<float>(backBufferWidth), static_cast<float>(backBufferHeight));
m_d3dContext->RSSetViewports(1, &m_screenViewport);
// Create a Direct2D target bitmap from the swapchain.
CD3D11_TEXTURE2D_DESC d2dTargetDesc(m_backBufferFormat,
backBufferWidth,
backBufferHeight,
1, // Only one texture.
1, // Use a single mipmap level.
D3D11_BIND_RENDER_TARGET);
D2D1_BITMAP_PROPERTIES1 bitmapProperties = D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
D2D1::PixelFormat(m_backBufferFormat, D2D1_ALPHA_MODE_PREMULTIPLIED),
m_dpi,
m_dpi);
ComPtr<IDXGISurface> targetDxgiSurface;
DX::ThrowIfFailed(m_swapChain->GetBuffer(0, IID_PPV_ARGS(&targetDxgiSurface)));
DX::ThrowIfFailed(m_d2dContext->CreateBitmapFromDxgiSurface(targetDxgiSurface.Get(), &bitmapProperties, &m_d2dTargetBitmap));
m_d2dContext->SetTarget(m_d2dTargetBitmap.Get());
m_d2dContext->SetDpi(m_dpi, m_dpi);
}
// This method is called in the event handler for the DpiChanged event.
void DX::DeviceResources::SetDpi(float dpi)
{
if (dpi != m_dpi)
{
m_dpi = dpi;
// When the display DPI changes, the logical size of the window (measured in Dips) also changes
// and needs to be updated.
UpdateLogicalSize(m_outputSize, m_dpi);
m_d2dContext->SetDpi(m_dpi, m_dpi);
//CreateWindowSizeDependentResources();
}
}
// This method is called when the Win32 window is created (or re-created).
void DX::DeviceResources::SetWindow(HWND window, int width, int height)
{
m_window = window;
m_outputSize.left = m_outputSize.top = 0;
m_outputSize.right = width;
m_outputSize.bottom = height;
}
// This method is called when the Win32 window changes size
bool DX::DeviceResources::WindowSizeChanged(int width, int height)
{
RECT newRc;
newRc.left = newRc.top = 0;
newRc.right = width;
newRc.bottom = height;
if (newRc == m_outputSize)
{
return false;
}
m_outputSize = newRc;
UpdateLogicalSize(m_outputSize, m_dpi);
CreateWindowSizeDependentResources();
return true;
}
// Recreate all device resources and set them back to the current state.
void DX::DeviceResources::HandleDeviceLost()
{
if (m_deviceNotify)
{
m_deviceNotify->OnDeviceLost();
}
m_d3dContext.Reset();
m_swapChain.Reset();
m_d3dAnnotation.Reset();
m_renderTarget.Reset();
m_depthStencil.Reset();
m_d3dRenderTargetView.Reset();
m_d3dDepthStencilView.Reset();
m_d2dDevice.Reset();
m_d2dContext.Reset();
m_d2dTargetBitmap.Reset();
#ifdef _DEBUG
{
ComPtr<ID3D11Debug> d3dDebug;
if (SUCCEEDED(m_d3dDevice.As(&d3dDebug)))
{
d3dDebug->ReportLiveDeviceObjects(D3D11_RLDO_SUMMARY);
}
}
#endif
m_d3dDevice.Reset();
CreateDeviceResources();
CreateWindowSizeDependentResources();
if (m_deviceNotify)
{
m_deviceNotify->OnDeviceRestored();
}
}
// Present the contents of the swap chain to the screen.
HRESULT DX::DeviceResources::Present(UINT syncInterval, UINT flags)
{
// The first argument instructs DXGI to block until VSync, putting the application
// to sleep until the next VSync. This ensures we don't waste any cycles rendering
// frames that will never be displayed to the screen.
// When using sync interval 0, it is recommended to always pass the tearing
// flag when it is supported, even when presenting in windowed mode.
// However, this flag cannot be used if the app is in fullscreen mode as a
// result of calling SetFullscreenState.
// UINT presentFlags = (m_tearingSupport && m_windowedMode) ? DXGI_PRESENT_ALLOW_TEARING : 0;
HRESULT hr = m_swapChain->Present(syncInterval, flags);
if (m_d3dContext)
{
// Discard the contents of the render target.
// This is a valid operation only when the existing contents will be entirely
// overwritten. If dirty or scroll rects are used, this call should be removed.
m_d3dContext->DiscardView(m_d3dRenderTargetView.Get());
if (m_d3dDepthStencilView)
{
// Discard the contents of the depth stencil.
m_d3dContext->DiscardView(m_d3dDepthStencilView.Get());
}
}
// If the device was removed either by a disconnection or a driver upgrade, we
// must recreate all device resources.
if (hr == DXGI_ERROR_DEVICE_REMOVED || hr == DXGI_ERROR_DEVICE_RESET)
{
#ifdef _DEBUG
char buff[64] = {};
sprintf_s(
buff, "Device Lost on Present: Reason code 0x%08X\n", (hr == DXGI_ERROR_DEVICE_REMOVED) ? m_d3dDevice->GetDeviceRemovedReason() : hr);
OutputDebugStringA(buff);
#endif
HandleDeviceLost();
}
else
{
DX::ThrowIfFailed(hr);
}
return hr;
}
// Sets a new swapchain/render target format and recreates all device resources.
void DX::DeviceResources::ChangeBackBufferFormat(DXGI_FORMAT fmt)
{
if (fmt == m_backBufferFormat)
{
return;
}
switch (fmt)
{
// Only support formats that Direct2D can use as a target.
case DXGI_FORMAT_R8G8B8A8_UNORM:
case DXGI_FORMAT_B8G8R8A8_UNORM:
case DXGI_FORMAT_R16G16B16A16_UNORM:
case DXGI_FORMAT_R16G16B16A16_FLOAT:
m_backBufferFormat = fmt;
break;
default:
DX::ThrowIfFailed(E_INVALIDARG);
break;
}
HandleDeviceLost();
}
// This method acquires the first available hardware adapter.
// If no such adapter can be found, *ppAdapter will be set to nullptr.
void DX::DeviceResources::GetHardwareAdapter(IDXGIAdapter1** ppAdapter)
{
*ppAdapter = nullptr;
ComPtr<IDXGIFactory1> dxgiFactory;
DX::ThrowIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(dxgiFactory.GetAddressOf())));
ComPtr<IDXGIAdapter1> adapter;
for (UINT adapterIndex = 0; DXGI_ERROR_NOT_FOUND != dxgiFactory->EnumAdapters1(adapterIndex, adapter.ReleaseAndGetAddressOf()); adapterIndex++)
{
DXGI_ADAPTER_DESC1 desc;
adapter->GetDesc1(&desc);
if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
{
// Don't select the Basic Render Driver adapter.
continue;
}
#ifdef _DEBUG
wchar_t buff[256] = {};
swprintf_s(buff, L"Direct3D Adapter (%u): VID:%04X, PID:%04X - %ls\n", adapterIndex, desc.VendorId, desc.DeviceId, desc.Description);
OutputDebugStringW(buff);
#endif
break;
}
*ppAdapter = adapter.Detach();
}
// This method acquires the first available hardware adapter.
// If no such adapter can be found, *ppAdapter will be set to nullptr.
void DX::DeviceResources::GetPreferredAdapter(DXGI_GPU_PREFERENCE pref, IDXGIAdapter1** ppAdapter)
{
*ppAdapter = nullptr;
ComPtr<IDXGIFactory6> dxgiFactory;
DX::ThrowIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(dxgiFactory.GetAddressOf())));
ComPtr<IDXGIAdapter1> adapter;
for (UINT adapterIndex = 0;
DXGI_ERROR_NOT_FOUND != dxgiFactory->EnumAdapterByGpuPreference(adapterIndex, pref, IID_PPV_ARGS(adapter.ReleaseAndGetAddressOf()));
adapterIndex++)
{
DXGI_ADAPTER_DESC1 desc;
adapter->GetDesc1(&desc);
if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
{
// Don't select the Basic Render Driver adapter.
continue;
}
#ifdef _DEBUG
wchar_t buff[256] = {};
swprintf_s(buff, L"Direct3D Adapter (%u): VID:%04X, PID:%04X - %ls\n", adapterIndex, desc.VendorId, desc.DeviceId, desc.Description);
OutputDebugStringW(buff);
#endif
break;
}
*ppAdapter = adapter.Detach();
}
// Updates the (primarily Direct2D) DPI-dependent measurements.
void DX::DeviceResources::UpdateLogicalSize(RECT outputSize, float dpi)
{
m_logicalSize = {0.0f, 0.0f, outputSize.right * 96.0f / dpi, outputSize.bottom * 96.0f / dpi};
}