-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathShaderOverride.cpp
348 lines (304 loc) · 9.77 KB
/
ShaderOverride.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2023 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "visa/iga/IGALibrary/api/igad.h"
#include <iostream>
#include <fstream>
#include <iostream>
#include "Compiler/CodeGenPublic.h"
#include "visaBuilder_interface.h"
#include "common/secure_mem.h"
#include "common/secure_string.h"
#include "Probe/Assertion.h"
#if defined(WIN32)
#include "WinDef.h"
#include "Windows.h"
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
__inline HINSTANCE GetModuleHINSTANCE() { return (HINSTANCE)&__ImageBase; }
__inline HMODULE
LoadDependency(const TCHAR *dependencyFileName)
{
TCHAR dllPath[MAX_PATH];
GetModuleFileName(GetModuleHINSTANCE(), dllPath, MAX_PATH);
std::basic_string<TCHAR> dllStringPath = std::basic_string<TCHAR>(dllPath);
auto lastPostion = dllStringPath.find_last_of('\\');
dllStringPath = dllStringPath.substr(0, lastPostion + 1);
dllStringPath.append(std::basic_string<TCHAR>(dependencyFileName));
return LoadLibraryEx(dllStringPath.c_str(), NULL, 0);
}
#else
#include <dlfcn.h> // dlopen, dlsym, dlclose
#define HMODULE void *
static void * GetProcAddress(
void * hModule,
const char * pProcName)
{
return dlsym(hModule, pProcName);
}
#endif
#if defined(WIN32)
#define CDECLATTRIBUTE __cdecl
#elif __GNUC__
#if defined(__x86_64__) || defined(__ARM_ARCH)
#define CDECLATTRIBUTE
#else
#define CDECLATTRIBUTE __attribute__((__cdecl__))
#endif
#endif
static void* loadBinFile(
const std::string& fileName,
int& binSize)
{
FILE* fp;
fp = fopen(fileName.c_str(), "rb");
if(fp == nullptr)
{
return nullptr;
}
fseek(fp, 0, SEEK_END);
binSize = int_cast<int>(ftell(fp));
if(binSize <= 0)
{
fclose(fp);
return nullptr;
}
rewind(fp);
void* buf = malloc(sizeof(char)* binSize);
if(buf == nullptr)
{
fclose(fp);
return nullptr;
}
size_t nr = fread(buf, 1, binSize, fp);
if(nr != binSize)
{
fclose(fp);
free(buf);
return nullptr;
}
fclose(fp);
return buf;
}
iga_gen_t GetIGAPlatform(PLATFORM const & platform)
{
auto & ProductFamily = platform.eProductFamily;
switch(platform.eRenderCoreFamily)
{
case IGFX_GEN8_CORE:
if(ProductFamily == IGFX_CHERRYVIEW)
{
return IGA_GEN8lp;
}
else
{
return IGA_GEN8;
}
case IGFX_GEN9_CORE:
case IGFX_GENNEXT_CORE:
if(ProductFamily == IGFX_BROXTON ||
ProductFamily == IGFX_GEMINILAKE)
{
return IGA_GEN9lp;
}
else
{
return IGA_GEN9;
}
case IGFX_GEN10_CORE:
return IGA_GEN10;
case IGFX_GEN11_CORE:
return IGA_GEN11;
case IGFX_GEN12_CORE:
case IGFX_GEN12LP_CORE:
case IGFX_XE_HP_CORE:
case IGFX_XE_HPG_CORE:
case IGFX_XE_HPC_CORE:
if ( ProductFamily == IGFX_TIGERLAKE_LP
|| ProductFamily == IGFX_DG1
|| ProductFamily == IGFX_ROCKETLAKE
|| ProductFamily == IGFX_ALDERLAKE_S
|| ProductFamily == IGFX_ALDERLAKE_P
|| ProductFamily == IGFX_ALDERLAKE_N
)
{
return IGA_GEN12p1;
}
else if (ProductFamily == IGFX_XE_HP_SDV)
{
return IGA_XE_HP;
}
else if (ProductFamily == IGFX_DG2
|| ProductFamily == IGFX_METEORLAKE)
{
return IGA_XE_HPG;
}
else if (ProductFamily == IGFX_ARROWLAKE)
{
return IGA_XE_HPG;
}
else if (ProductFamily == IGFX_PVC)
{
return IGA_XE_HPC;
}
IGC_ASSERT_MESSAGE(0, "unsupported platform");
break;
case IGFX_XE2_HPG_CORE:
if (ProductFamily == IGFX_BMG ||
ProductFamily == IGFX_LUNARLAKE)
{
return IGA_XE2;
}
IGC_ASSERT_MESSAGE(0, "unsupported platform");
break;
case IGFX_XE3_CORE:
return IGA_XE3;
default:
IGC_ASSERT_MESSAGE(0, "unsupported platform");
break;
}
return IGA_GEN9;
}
void appendToShaderOverrideLogFile(std::string const &binFileName, const char * message)
{
auto overridePath = std::string(IGC::Debug::GetShaderOverridePath());
overridePath.append("OverrideLog.txt");
printf("\n%s %s\n", message, binFileName.c_str());
std::ofstream os(overridePath.c_str(), std::ios::app);
if(os.is_open())
{
auto now = std::chrono::system_clock::now();
auto now_time = std::chrono::system_clock::to_time_t(now);
auto c_time = ctime(&now_time);
c_time[strlen(c_time) - 1] = '\t';
os << c_time << message << binFileName.c_str() << std::endl;
}
os.close();
}
void overrideShaderIGA(PLATFORM const & platform, void *& genxbin, int & binSize, std::string const &binFileName, bool &binOverride)
{
std::ifstream is(binFileName.c_str(), std::ios::in);
std::string asmContext;
void * overrideBinary = nullptr;
uint32_t overrideBinarySize = 0;
if(is.is_open())
{
asmContext.append(std::istreambuf_iterator<char>(is), std::istreambuf_iterator<char>());
is.close();
}
else
{
binOverride = false;
return;
}
HMODULE hModule = NULL;
pIGACreateContext fCreateContext;
pIGAGetErrors fIGAGetErrors;
pIGAGetWarnings fIGAGetWarnings;
pIGADiagnosticGetMessage fIGADiagnosticGetMessage;
pIGAAssemble fIGAAssemble;
pIGAReleaseContext fIGAReleaseContext;
#if defined(WIN32)
TCHAR *igaName = nullptr;
#ifdef _WIN64
char igaName64[] = "iga64.dll";
igaName = (TCHAR*)igaName64;
#else
char igaName32[] = "iga32.dll";
igaName = (TCHAR*)igaName32;
#endif //_WIN64
hModule = LoadDependency(igaName);
#else
// linux
#ifdef __x86_64__
char igaName[] = "libiga64.so";
#else
char igaName[] = "libiga32.so";
#endif
hModule = dlopen(igaName, RTLD_LAZY);
#endif
if (hModule == nullptr)
{
binOverride = false;
appendToShaderOverrideLogFile(binFileName, "OVERRIDE FAILED DUE TO MISSING IGA LIBRARY: ");
return;
}
fCreateContext = (pIGACreateContext)GetProcAddress(hModule, IGA_CREATE_CONTEXT_STR);
fIGAGetErrors = (pIGAGetErrors)GetProcAddress(hModule, IGA_GET_ERRORS_STR);
fIGAGetWarnings = (pIGAGetWarnings)GetProcAddress(hModule, IGA_GET_WARNINGS_STR);
fIGADiagnosticGetMessage = (pIGADiagnosticGetMessage)GetProcAddress(hModule, IGA_DIAGNOSTIC_GET_MESSAGE_STR);
fIGAAssemble = (pIGAAssemble)GetProcAddress(hModule, IGA_ASSEMBLE_STR);
fIGAReleaseContext = (pIGAReleaseContext)GetProcAddress(hModule, IGA_RELEASE_CONTEXT_STR);
if (fCreateContext == nullptr ||
fIGAGetErrors == nullptr ||
fIGAGetWarnings == nullptr ||
fIGADiagnosticGetMessage == nullptr ||
fIGAAssemble == nullptr ||
fIGAReleaseContext == nullptr)
{
binOverride = false;
appendToShaderOverrideLogFile(binFileName, "OVERRIDE FAILED DUE TO IGA LOAD ERRORS: ");
return;
}
iga_context_t ctx;
iga_context_options_t ctxOpts = IGA_CONTEXT_OPTIONS_INIT(GetIGAPlatform(platform));
if(fCreateContext(&ctxOpts, &ctx) != IGA_SUCCESS) {
binOverride = false;
appendToShaderOverrideLogFile(binFileName, "OVERRIDE FAILED DUE TO IGA CONTEXT CREATION FAILURE: ");
return;
}
iga_assemble_options_t asmOpts = IGA_ASSEMBLE_OPTIONS_INIT();
iga_status_t assembleRes = fIGAAssemble(ctx, &asmOpts, asmContext.c_str(), &overrideBinary, &overrideBinarySize);
if (assembleRes != IGA_SUCCESS) {
binOverride = false;
std::stringstream msgss;
msgss << "OVERRIDE FAILED DUE TO SHADER ASSEMBLY FAILURE (" << assembleRes << ") : ";
appendToShaderOverrideLogFile(binFileName, msgss.str().c_str());
const iga_diagnostic_t * errors;
uint32_t count;
if (fIGAGetErrors(ctx, &errors, &count) == IGA_SUCCESS)
{
for (uint32_t i = 0; i < count; i++)
{
std::stringstream errorss;
errorss << "error (" << i+1 << "/"<< count << "): Line " << errors[i].line <<": Col: " << errors[i].column << " " << errors[i].message << " ";
appendToShaderOverrideLogFile(binFileName, errorss.str().c_str());
}
}
if (fIGAGetWarnings(ctx, &errors, &count) == IGA_SUCCESS)
{
for (uint32_t i = 0; i < count; i++)
{
std::stringstream errorss;
errorss << "warning (" << i + 1 << "/" << count << "): Line " << errors[i].line << ": Col: " << errors[i].column << " " << errors[i].message << " ";
appendToShaderOverrideLogFile(binFileName, errorss.str().c_str());
}
}
return;
}
freeBlock(genxbin);
//when we releaseContext memory returned is freed
genxbin = malloc(overrideBinarySize);
if(genxbin)
{
memcpy_s(genxbin, overrideBinarySize, overrideBinary, overrideBinarySize);
binSize = overrideBinarySize;
binOverride = true;
appendToShaderOverrideLogFile(binFileName, "OVERRIDEN: ");
}
fIGAReleaseContext(ctx);
}
void overrideShaderBinary(void *& genxbin, int & binSize, std::string const &binFileName, bool &binOverride)
{
void* loadBin = nullptr;
int loadBinSize = 0;
loadBin = loadBinFile(binFileName, loadBinSize);
if(loadBin != nullptr)
{
freeBlock(genxbin);
genxbin = loadBin;
binSize = loadBinSize;
binOverride = true;
appendToShaderOverrideLogFile(binFileName, "OVERRIDEN: ");
}
}