This repository was archived by the owner on Apr 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGPUPerfAPILoader.cpp
More file actions
executable file
·198 lines (154 loc) · 5.2 KB
/
GPUPerfAPILoader.cpp
File metadata and controls
executable file
·198 lines (154 loc) · 5.2 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//==============================================================================
// Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools Team
/// \file
/// \brief class to load the GPA at run-time
//==============================================================================
#include "GPUPerfAPILoader.h"
GPUPerfAPILoader::GPUPerfAPILoader()
{
#ifdef _WIN32
m_hMod = NULL;
#else
pHandle = NULL;
#endif
// set all GPA public function pointers to zero
#define GPA_FUNCTION_PREFIX( f ) \
f = NULL;
#include "GPAFunctions.h"
#undef GPA_FUNCTION_PREFIX
}
GPUPerfAPILoader::~GPUPerfAPILoader()
{
Unload();
}
void GPUPerfAPILoader::Unload()
{
#ifdef _WIN32
if (m_hMod)
{
FreeLibrary(m_hMod);
m_hMod = NULL;
}
#else
if (pHandle != NULL)
{
dlclose(pHandle);
pHandle = NULL;
}
#endif
}
bool GPUPerfAPILoader::Loaded()
{
#ifdef _WIN32
if (m_hMod)
{
return true;
}
#else
if (pHandle != NULL)
{
return true;
}
#endif
return false;
}
#ifdef _WIN32
bool GPUPerfAPILoader::Load(const char* pDllPath, GPA_API_Type api, const char** ppErrorMessage)
{
std::string dllFullPath = GetGPADllName(std::string(pDllPath), api);
m_hMod = LoadLibraryA(dllFullPath.c_str());
if (m_hMod == NULL)
{
*ppErrorMessage = "LoadLibrary failed, dll not found.\n";
return false;
}
// Execute GetProcAddress on every public function. If fail assert and return error
#define GPA_FUNCTION_PREFIX( func ) \
func = ( func##PtrType ) GetProcAddress( m_hMod, #func ); \
assert( func != NULL ); \
if ( func == NULL ) \
{ \
*ppErrorMessage = #func" not located in the dll. Incorrect or out of date dll.\n"; \
Unload(); \
return false; \
}
#include "GPAFunctions.h"
#undef GPA_FUNCTION_PREFIX
return true;
}
#else
bool GPUPerfAPILoader::Load(const char* pDllPath, GPA_API_Type api, const char** ppErrorMessage)
{
std::string dllFullPath = GetGPADllName(std::string(pDllPath), api);
pHandle = dlopen(dllFullPath.c_str(), RTLD_LAZY);
if (pHandle == NULL)
{
*ppErrorMessage = dlerror();//"dlopen failed, shared library not found.\n";
return false;
}
// Execute GetProcAddress on every public function. If fail assert and return error
#define GPA_FUNCTION_PREFIX( func ) \
func = ( func##PtrType ) dlsym( pHandle, #func ); \
assert( func != NULL ); \
if ( func == NULL ) \
{ \
*ppErrorMessage = #func" not located in the shared library. Incorrect or out of date shared library.\n"; \
Unload(); \
return false; \
}
#include "GPAFunctions.h"
#undef GPA_FUNCTION_PREFIX
return true;
}
#endif
#ifdef _WIN32
#define LIB_PREFIX ""
#define LIB_SUFFIX ".dll"
#else
#define LIB_PREFIX "lib"
#define LIB_SUFFIX ".so"
#endif
#ifndef AMDT_PLATFORM_SUFFIX
#ifdef PLATFORM_SUFFIX
#define AMDT_PLATFORM_SUFFIX PLATFORM_SUFFIX
#endif
#endif
#ifdef USE_DEBUG_GPA
#define LIB_DEBUG_SUFFIX "-d"
#else
#define LIB_DEBUG_SUFFIX ""
#endif
std::string GPUPerfAPILoader::GetGPADllName(const std::string& dllPath, GPA_API_Type api)
{
std::string dllFullPath = dllPath;
dllFullPath.append(LIB_PREFIX);
switch (api)
{
case GPA_API_DIRECTX_11:
dllFullPath.append("GPUPerfAPIDX11");
break;
case GPA_API_DIRECTX_12:
dllFullPath.append("GPUPerfAPIDX12");
break;
case GPA_API_OPENGL:
dllFullPath.append("GPUPerfAPIGL");
break;
case GPA_API_OPENGLES:
dllFullPath.append("GPUPerfAPIGLES");
break;
case GPA_API_OPENCL:
dllFullPath.append("GPUPerfAPICL");
break;
case GPA_API_HSA:
dllFullPath.append("GPUPerfAPIHSA");
break;
default:
assert("unknown API type");
}
dllFullPath.append(AMDT_PLATFORM_SUFFIX);
dllFullPath.append(LIB_DEBUG_SUFFIX);
dllFullPath.append(AMDT_BUILD_SUFFIX);
dllFullPath.append(LIB_SUFFIX);
return dllFullPath;
}