Skip to content

Commit 7991b5d

Browse files
committed
Explicit adapter enumeration for D3D12CreateDevice. Issue #14
1 parent 36a55d8 commit 7991b5d

39 files changed

Lines changed: 468 additions & 26 deletions

Samples/D3D1211On12/src/D3D1211On12.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,11 @@ void D3D1211on12::LoadPipeline()
7373
}
7474
else
7575
{
76+
ComPtr<IDXGIAdapter1> hardwareAdapter;
77+
GetHardwareAdapter(factory.Get(), &hardwareAdapter);
78+
7679
ThrowIfFailed(D3D12CreateDevice(
77-
nullptr,
80+
hardwareAdapter.Get(),
7881
D3D_FEATURE_LEVEL_11_0,
7982
IID_PPV_ARGS(&m_d3d12Device)
8083
));

Samples/D3D1211On12/src/DXSample.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "DXSample.h"
1414
#include <shellapi.h>
1515

16-
DXSample::DXSample(UINT width, UINT height, std::wstring name):
16+
DXSample::DXSample(UINT width, UINT height, std::wstring name) :
1717
m_width(width),
1818
m_height(height),
1919
m_useWarpDevice(false)
@@ -100,6 +100,36 @@ std::wstring DXSample::GetAssetFullPath(LPCWSTR assetName)
100100
return m_assetsPath + assetName;
101101
}
102102

103+
// Helper function for acquiring the first available hardware adapter that supports Direct3D 12.
104+
// If no such adapter can be found, *ppAdapter will be set to nullptr.
105+
void DXSample::GetHardwareAdapter(_In_ IDXGIFactory4* pFactory, _Outptr_result_maybenull_ IDXGIAdapter1** ppAdapter)
106+
{
107+
IDXGIAdapter1* pAdapter = nullptr;
108+
*ppAdapter = nullptr;
109+
110+
for (UINT adapterIndex = 0; DXGI_ERROR_NOT_FOUND != pFactory->EnumAdapters1(adapterIndex, &pAdapter); ++adapterIndex)
111+
{
112+
DXGI_ADAPTER_DESC1 desc;
113+
pAdapter->GetDesc1(&desc);
114+
115+
if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
116+
{
117+
// Don't select the Basic Render Driver adapter.
118+
// If you want a software adapter, pass in "/warp" on the command line.
119+
continue;
120+
}
121+
122+
// Check to see if the adapter supports Direct3D 12, but don't create the
123+
// actual device yet.
124+
if (SUCCEEDED(D3D12CreateDevice(pAdapter, D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device), nullptr)))
125+
{
126+
break;
127+
}
128+
}
129+
130+
*ppAdapter = pAdapter;
131+
}
132+
103133
// Helper function for setting the window's title text.
104134
void DXSample::SetCustomWindowText(LPCWSTR text)
105135
{

Samples/D3D1211On12/src/DXSample.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class DXSample
3030
virtual bool OnEvent(MSG msg) = 0;
3131

3232
std::wstring GetAssetFullPath(LPCWSTR assetName);
33+
void GetHardwareAdapter(_In_ IDXGIFactory4* pFactory, _Outptr_result_maybenull_ IDXGIAdapter1** ppAdapter);
3334

3435
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
3536

Samples/D3D12Bundles/src/D3D12Bundles.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ void D3D12Bundles::LoadPipeline()
7070
}
7171
else
7272
{
73+
ComPtr<IDXGIAdapter1> hardwareAdapter;
74+
GetHardwareAdapter(factory.Get(), &hardwareAdapter);
75+
7376
ThrowIfFailed(D3D12CreateDevice(
74-
nullptr,
77+
hardwareAdapter.Get(),
7578
D3D_FEATURE_LEVEL_11_0,
7679
IID_PPV_ARGS(&m_device)
7780
));

Samples/D3D12Bundles/src/DXSample.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "DXSample.h"
1414
#include <shellapi.h>
1515

16-
DXSample::DXSample(UINT width, UINT height, std::wstring name):
16+
DXSample::DXSample(UINT width, UINT height, std::wstring name) :
1717
m_width(width),
1818
m_height(height),
1919
m_useWarpDevice(false)
@@ -100,6 +100,36 @@ std::wstring DXSample::GetAssetFullPath(LPCWSTR assetName)
100100
return m_assetsPath + assetName;
101101
}
102102

103+
// Helper function for acquiring the first available hardware adapter that supports Direct3D 12.
104+
// If no such adapter can be found, *ppAdapter will be set to nullptr.
105+
void DXSample::GetHardwareAdapter(_In_ IDXGIFactory4* pFactory, _Outptr_result_maybenull_ IDXGIAdapter1** ppAdapter)
106+
{
107+
IDXGIAdapter1* pAdapter = nullptr;
108+
*ppAdapter = nullptr;
109+
110+
for (UINT adapterIndex = 0; DXGI_ERROR_NOT_FOUND != pFactory->EnumAdapters1(adapterIndex, &pAdapter); ++adapterIndex)
111+
{
112+
DXGI_ADAPTER_DESC1 desc;
113+
pAdapter->GetDesc1(&desc);
114+
115+
if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
116+
{
117+
// Don't select the Basic Render Driver adapter.
118+
// If you want a software adapter, pass in "/warp" on the command line.
119+
continue;
120+
}
121+
122+
// Check to see if the adapter supports Direct3D 12, but don't create the
123+
// actual device yet.
124+
if (SUCCEEDED(D3D12CreateDevice(pAdapter, D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device), nullptr)))
125+
{
126+
break;
127+
}
128+
}
129+
130+
*ppAdapter = pAdapter;
131+
}
132+
103133
// Helper function for setting the window's title text.
104134
void DXSample::SetCustomWindowText(LPCWSTR text)
105135
{

Samples/D3D12Bundles/src/DXSample.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class DXSample
3030
virtual bool OnEvent(MSG msg) = 0;
3131

3232
std::wstring GetAssetFullPath(LPCWSTR assetName);
33+
void GetHardwareAdapter(_In_ IDXGIFactory4* pFactory, _Outptr_result_maybenull_ IDXGIAdapter1** ppAdapter);
3334

3435
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
3536

Samples/D3D12DynamicIndexing/src/D3D12DynamicIndexing.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ void D3D12DynamicIndexing::LoadPipeline()
7474
}
7575
else
7676
{
77+
ComPtr<IDXGIAdapter1> hardwareAdapter;
78+
GetHardwareAdapter(factory.Get(), &hardwareAdapter);
79+
7780
ThrowIfFailed(D3D12CreateDevice(
78-
nullptr,
81+
hardwareAdapter.Get(),
7982
D3D_FEATURE_LEVEL_11_0,
8083
IID_PPV_ARGS(&m_device)
8184
));

Samples/D3D12DynamicIndexing/src/DXSample.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "DXSample.h"
1414
#include <shellapi.h>
1515

16-
DXSample::DXSample(UINT width, UINT height, std::wstring name):
16+
DXSample::DXSample(UINT width, UINT height, std::wstring name) :
1717
m_width(width),
1818
m_height(height),
1919
m_useWarpDevice(false)
@@ -100,6 +100,36 @@ std::wstring DXSample::GetAssetFullPath(LPCWSTR assetName)
100100
return m_assetsPath + assetName;
101101
}
102102

103+
// Helper function for acquiring the first available hardware adapter that supports Direct3D 12.
104+
// If no such adapter can be found, *ppAdapter will be set to nullptr.
105+
void DXSample::GetHardwareAdapter(_In_ IDXGIFactory4* pFactory, _Outptr_result_maybenull_ IDXGIAdapter1** ppAdapter)
106+
{
107+
IDXGIAdapter1* pAdapter = nullptr;
108+
*ppAdapter = nullptr;
109+
110+
for (UINT adapterIndex = 0; DXGI_ERROR_NOT_FOUND != pFactory->EnumAdapters1(adapterIndex, &pAdapter); ++adapterIndex)
111+
{
112+
DXGI_ADAPTER_DESC1 desc;
113+
pAdapter->GetDesc1(&desc);
114+
115+
if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
116+
{
117+
// Don't select the Basic Render Driver adapter.
118+
// If you want a software adapter, pass in "/warp" on the command line.
119+
continue;
120+
}
121+
122+
// Check to see if the adapter supports Direct3D 12, but don't create the
123+
// actual device yet.
124+
if (SUCCEEDED(D3D12CreateDevice(pAdapter, D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device), nullptr)))
125+
{
126+
break;
127+
}
128+
}
129+
130+
*ppAdapter = pAdapter;
131+
}
132+
103133
// Helper function for setting the window's title text.
104134
void DXSample::SetCustomWindowText(LPCWSTR text)
105135
{

Samples/D3D12DynamicIndexing/src/DXSample.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class DXSample
3030
virtual bool OnEvent(MSG msg) = 0;
3131

3232
std::wstring GetAssetFullPath(LPCWSTR assetName);
33+
void GetHardwareAdapter(_In_ IDXGIFactory4* pFactory, _Outptr_result_maybenull_ IDXGIAdapter1** ppAdapter);
3334

3435
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
3536

Samples/D3D12ExecuteIndirect/src/D3D12ExecuteIndirect.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,11 @@ void D3D12ExecuteIndirect::LoadPipeline()
8585
}
8686
else
8787
{
88+
ComPtr<IDXGIAdapter1> hardwareAdapter;
89+
GetHardwareAdapter(factory.Get(), &hardwareAdapter);
90+
8891
ThrowIfFailed(D3D12CreateDevice(
89-
nullptr,
92+
hardwareAdapter.Get(),
9093
D3D_FEATURE_LEVEL_11_0,
9194
IID_PPV_ARGS(&m_device)
9295
));

0 commit comments

Comments
 (0)