Repository navigation
Expand file tree
/
Copy pathdx12_utils.cc
More file actions
183 lines (151 loc) · 5.92 KB
/
Copy pathdx12_utils.cc
File metadata and controls
183 lines (151 loc) · 5.92 KB
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
//==============================================================================
// Copyright (c) 2015-2020 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools Team
/// \file
/// \brief DX12 utility function implementations
//==============================================================================
#include "dx12_utils.h"
#include "d3d12.h"
#include "logging.h"
GPA_Status DX12Utils::DX12GetAdapterDesc(IUnknown* pDevice, DXGI_ADAPTER_DESC& adapterDesc)
{
GPA_Status status = GPA_STATUS_OK;
if (nullptr == pDevice)
{
GPA_LogError("Parameter 'pDevice' is NULL.");
status = GPA_STATUS_ERROR_NULL_POINTER;
}
else
{
ID3D12Device* dx12Device = static_cast<ID3D12Device*>(pDevice);
LUID adapterLuid = dx12Device->GetAdapterLuid();
IDXGIFactory* pDXGIFactory = nullptr;
HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&pDXGIFactory));
if (FAILED(hr) || (nullptr == pDXGIFactory))
{
GPA_LogError("Unable to get IDXGIFactory interface from ID3D12Device.");
status = GPA_STATUS_ERROR_FAILED;
}
else
{
IDXGIAdapter* pAdapter = nullptr;
for (UINT index = 0; pDXGIFactory->EnumAdapters(index, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++index)
{
memset(&adapterDesc, 0, sizeof(adapterDesc));
hr = pAdapter->GetDesc(&adapterDesc);
if (FAILED(hr))
{
GPA_LogError("Could not get adapter description, hardware cannot be supported.");
status = GPA_STATUS_ERROR_FAILED;
}
else
{
if (adapterDesc.AdapterLuid.HighPart == adapterLuid.HighPart && adapterDesc.AdapterLuid.LowPart == adapterLuid.LowPart)
{
// We found the matching adapter luid, so we're done.
break;
}
}
}
}
pDXGIFactory->Release();
}
return status;
}
bool DX12Utils::IsD3D12CommandList(void* pCmd, D3D12_COMMAND_LIST_TYPE* cmdType)
{
IUnknown* pUnknown = static_cast<IUnknown*>(pCmd);
ID3D12GraphicsCommandList* pD3DCommandList;
HRESULT hr = pUnknown->QueryInterface(__uuidof(ID3D12GraphicsCommandList), reinterpret_cast<void**>(&pD3DCommandList));
bool result = SUCCEEDED(hr);
if (result && nullptr != cmdType)
{
*cmdType = pD3DCommandList->GetType();
pD3DCommandList->Release(); // We need to release this as QueryInterface will call AddRef
}
return result;
}
bool DX12Utils::IsSameCmdType(const D3D12_COMMAND_LIST_TYPE* pDX12CmdType, const GPA_Command_List_Type* pGpaCmdType)
{
if (nullptr != pDX12CmdType && nullptr != pGpaCmdType)
{
if (D3D12_COMMAND_LIST_TYPE_BUNDLE == *pDX12CmdType && GPA_COMMAND_LIST_SECONDARY == *pGpaCmdType)
{
return true;
}
if (D3D12_COMMAND_LIST_TYPE_BUNDLE != *pDX12CmdType && GPA_COMMAND_LIST_PRIMARY == *pGpaCmdType)
{
return true;
}
}
return false;
}
bool DX12Utils::GetD3D12Device(IUnknown* pInterfacePointer, ID3D12Device** ppD3D12Device)
{
bool success = false;
*ppD3D12Device = nullptr;
// Check to see if it's an ID3D12Device
HRESULT hr = pInterfacePointer->QueryInterface(__uuidof(ID3D12Device), reinterpret_cast<void**>(ppD3D12Device));
if (hr == E_NOINTERFACE)
{
// It's not an ID3D12device, so see if it is an ID3D12DeviceChild-derived interface (like a ID3D12CommandList)
ID3D12DeviceChild* pDeviceChild = nullptr;
hr = pInterfacePointer->QueryInterface(__uuidof(ID3D12DeviceChild), reinterpret_cast<void**>(&pDeviceChild));
if (SUCCEEDED(hr))
{
// Note: GetDevice increments a ref on out parameter
hr = pDeviceChild->GetDevice(__uuidof(ID3D12Device), reinterpret_cast<void**>(ppD3D12Device));
pDeviceChild->Release();
}
}
success = SUCCEEDED(hr);
if (success)
{
(*ppD3D12Device)->Release();
}
return success;
}
bool DX12Utils::IsFeatureLevelSupported(ID3D12Device* pD3D12Device)
{
D3D12_FEATURE_DATA_FEATURE_LEVELS featureLevels = {};
const D3D_FEATURE_LEVEL requestedFeatureLevels[] = {
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_12_0,
D3D_FEATURE_LEVEL_12_1,
};
featureLevels.NumFeatureLevels = _countof(requestedFeatureLevels);
featureLevels.pFeatureLevelsRequested = requestedFeatureLevels;
HRESULT hr = pD3D12Device->CheckFeatureSupport(D3D12_FEATURE_FEATURE_LEVELS, &featureLevels, sizeof(featureLevels));
return SUCCEEDED(hr);
}
bool DX12Utils::GetTimestampFrequency(ID3D12Device* pD3D12Device, UINT64& timestampFrequency)
{
bool isSucceeded = false;
ID3D12CommandQueue* pQueue = nullptr;
D3D12_COMMAND_QUEUE_DESC queueDesc;
ZeroMemory(&queueDesc, sizeof(queueDesc));
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
HRESULT hr = pD3D12Device->CreateCommandQueue(&queueDesc, __uuidof(ID3D12CommandQueue), reinterpret_cast<PVOID*>(&pQueue));
if (SUCCEEDED(hr) || nullptr != pQueue)
{
UINT64 frequency = 0;
hr = pQueue->GetTimestampFrequency(&frequency);
if (SUCCEEDED(hr))
{
isSucceeded = true;
timestampFrequency = frequency;
}
else
{
GPA_LogError("Calling GetTimestampFrequency on Direct Command Queue failed");
}
pQueue->Release();
}
else
{
GPA_LogError("GetTimestampFrequency Create Command Queue failed");
}
return isSucceeded;
}