forked from GPUOpen-Tools/gpu_performance_api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DX12GPAContext.cpp
286 lines (236 loc) · 8.35 KB
/
DX12GPAContext.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
//==============================================================================
// Copyright (c) 2017-2018 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools Team
/// \file
/// \brief GPA DX12 Context implementation
//==============================================================================
#include "DX12GPAContext.h"
#include "GPAUniqueObject.h"
#include "DX12Utils.h"
#include "DeviceInfoUtils.h"
#include "DX12GPASession.h"
#include "GPAHardwareCounters.h"
#include "GPACounterGeneratorBase.h"
DX12GPAContext::DX12GPAContext(ID3D12Device* pD3D12Device,
GPA_HWInfo& hwInfo,
GPA_OpenContextFlags flags)
: GPAContext(hwInfo, flags)
{
m_pD3D12Device = pD3D12Device;
m_pD3D12Device->AddRef();
m_pAmdExtD3DFactoryObject = nullptr;
m_pGpaInterface = nullptr;
m_clockMode = AmdExtDeviceClockMode::Default;
}
DX12GPAContext::~DX12GPAContext()
{
CleanUp();
}
bool DX12GPAContext::Initialize()
{
bool isSucceeded = false;
std::lock_guard<std::mutex> lockInitialization(m_dx12GpaContextMutex);
if (nullptr != m_pD3D12Device)
{
if (InitializeAMDExtension() && OpenCounters())
{
isSucceeded = true;
}
}
SetAsOpened(isSucceeded);
return isSucceeded;
}
GPA_SessionId DX12GPAContext::CreateSession(GPA_Session_Sample_Type sampleType)
{
GPA_SessionId pRetSessionId = nullptr;
if (nullptr != m_pGpaInterface)
{
DX12GPASession* pNewGpaDx12GpaSession = new(std::nothrow) DX12GPASession(this, sampleType, m_pGpaInterface);
if (nullptr != pNewGpaDx12GpaSession)
{
AddGpaSession(pNewGpaDx12GpaSession);
pRetSessionId = reinterpret_cast<GPA_SessionId>(GPAUniqueObjectManager::Instance()->CreateObject(pNewGpaDx12GpaSession));
}
}
return pRetSessionId;
}
bool DX12GPAContext::DeleteSession(GPA_SessionId sessionId)
{
bool success = false;
if (GPAUniqueObjectManager::Instance()->DoesExist(sessionId))
{
DX12GPASession* pDx12Session = reinterpret_cast<DX12GPASession*>(sessionId->Object());
unsigned int index;
if (GetIndex(pDx12Session, &index))
{
RemoveGpaSession(pDx12Session);
GPAUniqueObjectManager::Instance()->DeleteObject(sessionId);
delete pDx12Session;
success = true;
}
}
return success;
}
gpa_uint32 DX12GPAContext::GetMaxGPASessions() const
{
return GPA_SESSION_NO_LIMIT;
}
GPA_API_Type DX12GPAContext::GetAPIType() const
{
return GPA_API_DIRECTX_12;
}
ID3D12Device* DX12GPAContext::GetD3D12Device() const
{
return m_pD3D12Device;
}
gpa_uint32 DX12GPAContext::GetInstanceCount(AmdExtGpuBlock block) const
{
gpa_uint32 instanceCount = 0;
if (block < AmdExtGpuBlock::Count)
{
instanceCount = static_cast<gpa_uint32>(m_amdDeviceProps.blocks[static_cast<size_t>(block)].instanceCount);
}
return instanceCount;
}
gpa_uint32 DX12GPAContext::GetMaxEventIdCount(AmdExtGpuBlock block) const
{
gpa_uint32 maxEventId = 0;
if (block < AmdExtGpuBlock::Count)
{
maxEventId = static_cast<gpa_uint32>(m_amdDeviceProps.blocks[static_cast<size_t>(block)].maxEventId);
}
return maxEventId;
}
bool DX12GPAContext::InitializeAMDExtension()
{
GPA_Status result = GPA_STATUS_OK;
if (nullptr != m_pD3D12Device)
{
if (nullptr == m_pGpaInterface && IsAMDDevice())
{
result = GPA_STATUS_ERROR_DRIVER_NOT_SUPPORTED;
HMODULE hDll = nullptr;
#ifdef X64
hDll = GetModuleHandle("amdxc64.dll");
#else
hDll = GetModuleHandle("amdxc32.dll");
#endif
if (nullptr == hDll)
{
GPA_LogError("Unable to get driver module handle.");
}
else
{
PFNAmdExtD3DCreateInterface pAmdExtD3dCreateFunc =
reinterpret_cast<PFNAmdExtD3DCreateInterface>(GetProcAddress(hDll, "AmdExtD3DCreateInterface"));
if (nullptr == pAmdExtD3dCreateFunc)
{
GPA_LogError("Unable to get driver extension entry point.");
}
else
{
HRESULT hr = pAmdExtD3dCreateFunc(m_pD3D12Device,
__uuidof(IAmdExtD3DFactory),
reinterpret_cast<void**>(&m_pAmdExtD3DFactoryObject));
if (FAILED(hr))
{
GPA_LogError("Unable to get driver extension interface.");
}
else
{
hr = m_pAmdExtD3DFactoryObject->CreateInterface(m_pD3D12Device,
__uuidof(IAmdExtGpaInterface),
reinterpret_cast<void**>(&m_pGpaInterface));
if (FAILED(hr))
{
GPA_LogError("Unable to get driver GPA extension interface.");
}
else
{
hr = m_pGpaInterface->GetPerfExperimentProperties(&m_amdDeviceProps);
if (FAILED(hr))
{
GPA_LogError("Unable to get current hardware perf experiment properties.");
}
else
{
if (0 == m_amdDeviceProps.features.counters)
{
GPA_LogError("Active GPU hardware does not support performance counters.");
result = GPA_STATUS_ERROR_HARDWARE_NOT_SUPPORTED;
}
else
{
SetStableClocks(true);
result = GPA_STATUS_OK;
}
}
}
}
}
}
}
}
return result == GPA_STATUS_OK;
}
void DX12GPAContext::CleanUp()
{
std::lock_guard<std::mutex> lockContextResources(m_dx12GpaContextMutex);
SetStableClocks(false);
// Release Device
if (nullptr != m_pD3D12Device)
{
m_pD3D12Device->Release();
m_pD3D12Device = nullptr;
}
if (nullptr != m_pGpaInterface)
{
IterateGpaSessionList([](IGPASession* pGpaSession)->bool {delete pGpaSession; return true; });
ClearSessionList();
m_pGpaInterface->Release();
m_pGpaInterface = nullptr;
}
// Release AMD D3D Factory
if (nullptr != m_pAmdExtD3DFactoryObject)
{
m_pAmdExtD3DFactoryObject->Release();
m_pAmdExtD3DFactoryObject = nullptr;
}
}
void DX12GPAContext::SetStableClocks(bool useProfilingClocks)
{
if (nullptr != m_pGpaInterface)
{
AmdExtDeviceClockMode amdClockMode = AmdExtDeviceClockMode::Default;
if (useProfilingClocks)
{
DeviceClockMode deviceClockMode = GetDeviceClockMode();
switch (deviceClockMode)
{
case DeviceClockMode::Default:
amdClockMode = AmdExtDeviceClockMode::Default;
break;
case DeviceClockMode::MinimumEngine:
amdClockMode = AmdExtDeviceClockMode::MinimumEngine;
break;
case DeviceClockMode::MinimumMemory:
amdClockMode = AmdExtDeviceClockMode::MinimumMemory;
break;
case DeviceClockMode::Peak:
amdClockMode = AmdExtDeviceClockMode::Peak;
break;
case DeviceClockMode::Profiling:
amdClockMode = AmdExtDeviceClockMode::Profiling;
break;
default:
amdClockMode = AmdExtDeviceClockMode::Profiling;
break;
}
}
if (amdClockMode != m_clockMode)
{
m_clockMode = amdClockMode;
m_pGpaInterface->SetClockMode(m_clockMode, nullptr);
}
}
}