From 5be665447241bc78ed149a7b00e48f9adc96adb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aki=20J=C3=A4ntti?= Date: Wed, 18 May 2016 18:10:24 +0100 Subject: [PATCH 1/2] When creating the device, use the one with the most video memory. --- Common/d3dApp.cpp | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/Common/d3dApp.cpp b/Common/d3dApp.cpp index 87257b0..c12d555 100644 --- a/Common/d3dApp.cpp +++ b/Common/d3dApp.cpp @@ -411,24 +411,44 @@ bool D3DApp::InitDirect3D() } #endif + Microsoft::WRL::ComPtr pDevice; + Microsoft::WRL::ComPtr pAdapter; + + SIZE_T biggestSize = 0; + Microsoft::WRL::ComPtr pBiggestAdapter; + + DXGI_ADAPTER_DESC1 desc; + ThrowIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(&mdxgiFactory))); - // Try to create hardware device. - HRESULT hardwareResult = D3D12CreateDevice( - nullptr, // default adapter - D3D_FEATURE_LEVEL_11_0, - IID_PPV_ARGS(&md3dDevice)); + // Find the adapter with largest video memory in case we have multiple different adapters, like on some laptops. + for (uint32_t Idx = 0; DXGI_ERROR_NOT_FOUND != mdxgiFactory->EnumAdapters1(Idx, &pAdapter); ++Idx) + { + pAdapter->GetDesc1(&desc); + // We don't want software adapters. + if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) + { + continue; + } + if (desc.DedicatedVideoMemory > biggestSize) + { + pBiggestAdapter = pAdapter; + biggestSize = desc.DedicatedVideoMemory; + } + } - // Fallback to WARP device. - if(FAILED(hardwareResult)) + // Try to create the device, require DirectX 12 support. + if (SUCCEEDED(D3D12CreateDevice(pBiggestAdapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&pDevice)))) { - ComPtr pWarpAdapter; - ThrowIfFailed(mdxgiFactory->EnumWarpAdapter(IID_PPV_ARGS(&pWarpAdapter))); + md3dDevice = pDevice.Detach(); + } - ThrowIfFailed(D3D12CreateDevice( - pWarpAdapter.Get(), - D3D_FEATURE_LEVEL_11_0, - IID_PPV_ARGS(&md3dDevice))); + // Fallback to WARP device. + if (md3dDevice == nullptr) + { + ThrowIfFailed(mdxgiFactory->EnumWarpAdapter(IID_PPV_ARGS(&pAdapter))); + ThrowIfFailed(D3D12CreateDevice(pAdapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&pDevice))); + md3dDevice = pDevice.Detach(); } ThrowIfFailed(md3dDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, From 944686c21728a7d2b05018d4e1b4479981bf2eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aki=20J=C3=A4ntti?= Date: Thu, 19 May 2016 14:12:14 +0100 Subject: [PATCH 2/2] Remove .Detach() from ComPtr copying. In the MS example I checked, the global device variable was a raw pointer, but in these samples it's actually a ComPtr, so .Detach() is unnecessary. --- Common/d3dApp.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Common/d3dApp.cpp b/Common/d3dApp.cpp index c12d555..acfe518 100644 --- a/Common/d3dApp.cpp +++ b/Common/d3dApp.cpp @@ -440,15 +440,14 @@ bool D3DApp::InitDirect3D() // Try to create the device, require DirectX 12 support. if (SUCCEEDED(D3D12CreateDevice(pBiggestAdapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&pDevice)))) { - md3dDevice = pDevice.Detach(); + md3dDevice = pDevice; } // Fallback to WARP device. if (md3dDevice == nullptr) { ThrowIfFailed(mdxgiFactory->EnumWarpAdapter(IID_PPV_ARGS(&pAdapter))); - ThrowIfFailed(D3D12CreateDevice(pAdapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&pDevice))); - md3dDevice = pDevice.Detach(); + ThrowIfFailed(D3D12CreateDevice(pAdapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&md3dDevice))); } ThrowIfFailed(md3dDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE,