-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmdPix3.h
226 lines (187 loc) · 7.54 KB
/
AmdPix3.h
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
//==============================================================================
// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools Team
/// \file
/// \brief This file contains three macro definitions to wrap existing PIX3
/// functions with AMD RGP marker support.
///
/// This requires AMD driver with a device level marker extension
/// support (driver 17.30.1081 or newer).
///
/// This requires a WinPixEventRuntime version of at least
/// 1.0.200127001.
///
/// To use: Update the PIXEvents.h file to #include this header file.
/// Then prefix the existing calls to PIXBeginEventOnContextCpu,
/// PIXEndEventOnContextCpu and PIXSetMarkerOnContextCpu within
/// that file to add an "Rgp" prefix (so the calls become
/// RgpPIXBeginEventOnContextCpu, RgpPIXEndEventOnContextCpu and
/// RgpPIXSetMarkerOnContextCpu). Also check the RGP user
/// documentation for a complete user guide.
//==============================================================================
#ifndef _AMD_PIX3_H_
#define _AMD_PIX3_H_
#include <stdarg.h>
#include <stdio.h>
#include "AmdExtD3D.h"
#include "AmdExtD3DDeviceApi.h"
#define MAX_MARKER_STRING_LENGTH 1024
// per thread amd ext device object using TLS
static __declspec(thread) IAmdExtD3DDevice1* tls_pAmdExtDeviceObject = nullptr;
static __declspec(thread) bool tls_checkAmdDriver = true;
// this function will initialize the tls_pAmdExtDeviceObject per thread
// tls_pAmdExtDeviceObject contains the marker API
inline void InitializeAmdExtDeviceObject(ID3D12GraphicsCommandList* pCommandList)
{
// return immediately if the device extension object has been created for the thread
if (nullptr != tls_pAmdExtDeviceObject)
{
return;
}
// return immediately if on non-AMD system
if (!tls_checkAmdDriver)
{
return;
}
HMODULE hpAmdD3dDl2 = nullptr;
BOOL loaded = FALSE;
if (tls_checkAmdDriver)
{
loaded = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, L"amdxc64.dll", &hpAmdD3dDl2);
}
if (FALSE != loaded && nullptr != hpAmdD3dDl2)
{
PFNAmdExtD3DCreateInterface pAmdExtD3dCreateFunc = (PFNAmdExtD3DCreateInterface)GetProcAddress(hpAmdD3dDl2, "AmdExtD3DCreateInterface");
if (nullptr != pAmdExtD3dCreateFunc)
{
ID3D12Device* pDevice = nullptr;
if (nullptr != pCommandList)
{
pCommandList->GetDevice(__uuidof(ID3D12Device), reinterpret_cast<void**>(&pDevice));
}
if (nullptr != pDevice)
{
// create the extension object factory
IAmdExtD3DFactory* pAmdExtObject = nullptr;
pAmdExtD3dCreateFunc(pDevice, __uuidof(IAmdExtD3DFactory), reinterpret_cast<void**>(&pAmdExtObject));
if (nullptr != pAmdExtObject)
{
// use the extension factory object to create a device extension object that contains the marker API
pAmdExtObject->CreateInterface(pDevice, __uuidof(IAmdExtD3DDevice1), reinterpret_cast<void**>(&tls_pAmdExtDeviceObject));
}
}
}
}
else
{
// running on non-amd hardware or missing amd driver install
tls_checkAmdDriver = false;
}
}
inline void RgpSetMarker(ID3D12GraphicsCommandList* pCommandList, PCWSTR formatString, ...)
{
InitializeAmdExtDeviceObject(pCommandList);
if (nullptr != tls_pAmdExtDeviceObject)
{
// create a new marker string that includes all the variadic args
wchar_t markerString[MAX_MARKER_STRING_LENGTH];
va_list args;
va_start(args, formatString);
vswprintf_s(markerString, formatString, args);
va_end(args);
// convert from wchar_t to char string
char markerStringInChar[MAX_MARKER_STRING_LENGTH];
size_t retValue = 0;
wcstombs_s(&retValue, markerStringInChar, MAX_MARKER_STRING_LENGTH, markerString, MAX_MARKER_STRING_LENGTH);
// set the rgp marker
tls_pAmdExtDeviceObject->SetMarker(pCommandList, markerStringInChar);
}
}
inline void RgpSetMarker(ID3D12GraphicsCommandList* pCommandList, PCSTR formatString, ...)
{
InitializeAmdExtDeviceObject(pCommandList);
if (nullptr != tls_pAmdExtDeviceObject)
{
// create a new marker string that includes all the variadic args
char markerString[MAX_MARKER_STRING_LENGTH];
va_list args;
va_start(args, formatString);
vsprintf_s(markerString, formatString, args);
va_end(args);
// set the rgp marker
tls_pAmdExtDeviceObject->SetMarker(pCommandList, markerString);
}
}
inline void RgpPushMarker(ID3D12GraphicsCommandList* pCommandList, PCWSTR formatString, ...)
{
InitializeAmdExtDeviceObject(pCommandList);
if (nullptr != tls_pAmdExtDeviceObject)
{
// create a new marker string that includes all the variadic args
wchar_t markerString[MAX_MARKER_STRING_LENGTH];
va_list args;
va_start(args, formatString);
vswprintf_s(markerString, formatString, args);
va_end(args);
// convert from wchar_t to char string
char markerStringInChar[MAX_MARKER_STRING_LENGTH];
size_t retValue = 0;
wcstombs_s(&retValue, markerStringInChar, MAX_MARKER_STRING_LENGTH, markerString, MAX_MARKER_STRING_LENGTH);
// push the rgp marker
tls_pAmdExtDeviceObject->PushMarker(pCommandList, markerStringInChar);
}
}
inline void RgpPushMarker(ID3D12GraphicsCommandList* pCommandList, PCSTR formatString, ...)
{
InitializeAmdExtDeviceObject(pCommandList);
if (nullptr != tls_pAmdExtDeviceObject)
{
// create a new marker string that includes all the variadic args
char markerString[MAX_MARKER_STRING_LENGTH];
va_list args;
va_start(args, formatString);
vsprintf_s(markerString, formatString, args);
va_end(args);
// push the rgp marker
tls_pAmdExtDeviceObject->PushMarker(pCommandList, markerString);
}
}
inline void RgpPopMarker(ID3D12GraphicsCommandList* pCommandList)
{
InitializeAmdExtDeviceObject(pCommandList);
if (nullptr != tls_pAmdExtDeviceObject)
{
tls_pAmdExtDeviceObject->PopMarker(pCommandList);
}
}
inline void RgpSetMarker(ID3D12CommandQueue*, PCWSTR, ...)
{
// there is no queue-based marker yet
}
inline void RgpSetMarker(ID3D12CommandQueue*, PCSTR, ...)
{
// there is no queue-based marker yet
}
inline void RgpPushMarker(ID3D12CommandQueue*, PCWSTR, ...)
{
// there is no queue-based marker yet
}
inline void RgpPushMarker(ID3D12CommandQueue*, PCSTR, ...)
{
// there is no queue-based marker yet
}
inline void RgpPopMarker(ID3D12CommandQueue*)
{
// there is no queue-based marker yet
}
// define three macros to wrap existing PIX3 functions
#define RgpPIXBeginEventOnContextCpu(context, color, formatString, ...) \
RgpPushMarker(context, formatString, args...); \
PIXBeginEventOnContextCpu(context, color, formatString, args...);
#define RgpPIXEndEventOnContextCpu(context) \
RgpPopMarker(context); \
PIXEndEventOnContextCpu(context);
#define RgpPIXSetMarkerOnContextCpu(context, color, formatString, ...) \
RgpSetMarker(context, formatString, args...); \
PIXSetMarkerOnContextCpu(context, color, formatString, args...);
#endif //_AMD_PIX3_H_