forked from notaz/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdxil_validator.cpp
362 lines (299 loc) · 9.51 KB
/
dxil_validator.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "dxil_validator.h"
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif
#include <windows.h>
#include <unknwn.h>
#include "util/ralloc.h"
#include "util/u_debug.h"
#include "util/compiler.h"
#include "dxcapi.h"
#include <wrl/client.h>
using Microsoft::WRL::ComPtr;
struct dxil_validator {
HMODULE dxil_mod;
HMODULE dxcompiler_mod;
IDxcValidator *dxc_validator;
IDxcLibrary *dxc_library;
IDxcCompiler *dxc_compiler;
enum dxil_validator_version version;
};
extern "C" {
extern IMAGE_DOS_HEADER __ImageBase;
}
static HMODULE
load_dxil_mod()
{
/* First, try to load DXIL.dll from the default search-path */
HMODULE mod = LoadLibraryA("DXIL.dll");
if (mod)
return mod;
/* If that fails, try to load it next to the current module, so we can
* ship DXIL.dll next to the GLon12 DLL.
*/
char self_path[MAX_PATH];
uint32_t path_size = GetModuleFileNameA((HINSTANCE)&__ImageBase,
self_path, sizeof(self_path));
if (!path_size || path_size == sizeof(self_path)) {
debug_printf("DXIL: Unable to get path to self");
return NULL;
}
auto last_slash = strrchr(self_path, '\\');
if (!last_slash) {
debug_printf("DXIL: Unable to get path to self");
return NULL;
}
*(last_slash + 1) = '\0';
if (strcat_s(self_path, "DXIL.dll") != 0) {
debug_printf("DXIL: Unable to get path to DXIL.dll next to self");
return NULL;
}
return LoadLibraryA(self_path);
}
static IDxcValidator *
create_dxc_validator(HMODULE dxil_mod)
{
DxcCreateInstanceProc dxil_create_func =
(DxcCreateInstanceProc)GetProcAddress(dxil_mod, "DxcCreateInstance");
if (!dxil_create_func) {
debug_printf("DXIL: Failed to load DxcCreateInstance from DXIL.dll\n");
return NULL;
}
IDxcValidator *dxc_validator;
HRESULT hr = dxil_create_func(CLSID_DxcValidator,
IID_PPV_ARGS(&dxc_validator));
if (FAILED(hr)) {
debug_printf("DXIL: Failed to create validator\n");
return NULL;
}
return dxc_validator;
}
static enum dxil_validator_version
get_validator_version(IDxcValidator *val)
{
ComPtr<IDxcVersionInfo> version_info;
if (FAILED(val->QueryInterface(version_info.ReleaseAndGetAddressOf())))
return NO_DXIL_VALIDATION;
UINT32 major, minor;
if (FAILED(version_info->GetVersion(&major, &minor)))
return NO_DXIL_VALIDATION;
if (major == 1)
return (enum dxil_validator_version)(DXIL_VALIDATOR_1_0 + MIN2(minor, 7));
if (major > 1)
return DXIL_VALIDATOR_1_7;
return NO_DXIL_VALIDATION;
}
static uint64_t
get_dll_version(HMODULE mod)
{
WCHAR filename[MAX_PATH];
DWORD filename_length = GetModuleFileNameW(mod, filename, ARRAY_SIZE(filename));
if (filename_length == 0 || filename_length == ARRAY_SIZE(filename))
return 0;
DWORD version_handle = 0;
DWORD version_size = GetFileVersionInfoSizeW(filename, &version_handle);
if (version_size == 0)
return 0;
void *version_data = malloc(version_size);
if (!version_data)
return 0;
if (!GetFileVersionInfoW(filename, version_handle, version_size, version_data)) {
free(version_data);
return 0;
}
UINT value_size = 0;
VS_FIXEDFILEINFO *version_info = nullptr;
if (!VerQueryValueW(version_data, L"\\", reinterpret_cast<void **>(&version_info), &value_size) ||
!value_size ||
version_info->dwSignature != VS_FFI_SIGNATURE) {
free(version_data);
return 0;
}
uint64_t ret =
((uint64_t)version_info->dwFileVersionMS << 32ull) |
(uint64_t)version_info->dwFileVersionLS;
free(version_data);
return ret;
}
static enum dxil_validator_version
get_filtered_validator_version(HMODULE mod, enum dxil_validator_version raw)
{
switch (raw) {
case DXIL_VALIDATOR_1_6: {
uint64_t dxil_version = get_dll_version(mod);
static constexpr uint64_t known_bad_version =
// 101.5.2005.60
(101ull << 48ull) | (5ull << 32ull) | (2005ull << 16ull) | 60ull;
if (dxil_version == known_bad_version)
return DXIL_VALIDATOR_1_5;
FALLTHROUGH;
}
default:
return raw;
}
}
struct dxil_validator *
dxil_create_validator(const void *ctx)
{
struct dxil_validator *val = rzalloc(ctx, struct dxil_validator);
if (!val)
return NULL;
/* Load DXIL.dll. This is a hard requirement on Windows, so we error
* out if this fails.
*/
val->dxil_mod = load_dxil_mod();
if (!val->dxil_mod) {
debug_printf("DXIL: Failed to load DXIL.dll\n");
goto fail;
}
/* Create IDxcValidator. This is a hard requirement on Windows, so we
* error out if this fails.
*/
val->dxc_validator = create_dxc_validator(val->dxil_mod);
if (!val->dxc_validator)
goto fail;
val->version = get_filtered_validator_version(
val->dxil_mod,
get_validator_version(val->dxc_validator));
/* Try to load dxcompiler.dll. This is just used for diagnostics, and
* will fail on most end-users install. So we do not error out if this
* fails.
*/
val->dxcompiler_mod = LoadLibraryA("dxcompiler.dll");
if (val->dxcompiler_mod) {
/* If we managed to load dxcompiler.dll, but either don't find
* DxcCreateInstance, or fail to create IDxcLibrary or
* IDxcCompiler, this is a good indication that the user wants
* diagnostics, but something went wrong. Print warnings to help
* figuring out what's wrong, but do not treat it as an error.
*/
DxcCreateInstanceProc compiler_create_func =
(DxcCreateInstanceProc)GetProcAddress(val->dxcompiler_mod,
"DxcCreateInstance");
if (!compiler_create_func) {
debug_printf("DXIL: Failed to load DxcCreateInstance from "
"dxcompiler.dll\n");
} else {
if (FAILED(compiler_create_func(CLSID_DxcLibrary,
IID_PPV_ARGS(&val->dxc_library))))
debug_printf("DXIL: Unable to create IDxcLibrary instance\n");
if (FAILED(compiler_create_func(CLSID_DxcCompiler,
IID_PPV_ARGS(&val->dxc_compiler))))
debug_printf("DXIL: Unable to create IDxcCompiler instance\n");
}
}
return val;
fail:
if (val->dxil_mod)
FreeLibrary(val->dxil_mod);
ralloc_free(val);
return NULL;
}
void
dxil_destroy_validator(struct dxil_validator *val)
{
/* if we have a validator, we have these */
val->dxc_validator->Release();
FreeLibrary(val->dxil_mod);
if (val->dxcompiler_mod) {
if (val->dxc_library)
val->dxc_library->Release();
if (val->dxc_compiler)
val->dxc_compiler->Release();
FreeLibrary(val->dxcompiler_mod);
}
ralloc_free(val);
}
class ShaderBlob : public IDxcBlob {
public:
ShaderBlob(void *data, size_t size) :
m_data(data),
m_size(size)
{
}
LPVOID STDMETHODCALLTYPE
GetBufferPointer(void) override
{
return m_data;
}
SIZE_T STDMETHODCALLTYPE
GetBufferSize() override
{
return m_size;
}
HRESULT STDMETHODCALLTYPE
QueryInterface(REFIID, void **) override
{
return E_NOINTERFACE;
}
ULONG STDMETHODCALLTYPE
AddRef() override
{
return 1;
}
ULONG STDMETHODCALLTYPE
Release() override
{
return 0;
}
void *m_data;
size_t m_size;
};
bool
dxil_validate_module(struct dxil_validator *val, void *data, size_t size, char **error)
{
ShaderBlob source(data, size);
ComPtr<IDxcOperationResult> result;
val->dxc_validator->Validate(&source, DxcValidatorFlags_InPlaceEdit,
&result);
HRESULT hr;
result->GetStatus(&hr);
if (FAILED(hr) && error) {
/* try to resolve error message */
*error = NULL;
if (!val->dxc_library) {
debug_printf("DXIL: validation failed, but lacking IDxcLibrary"
"from dxcompiler.dll for proper diagnostics.\n");
return false;
}
ComPtr<IDxcBlobEncoding> blob, blob_utf8;
if (FAILED(result->GetErrorBuffer(&blob)))
fprintf(stderr, "DXIL: IDxcOperationResult::GetErrorBuffer() failed\n");
else if (FAILED(val->dxc_library->GetBlobAsUtf8(blob.Get(),
blob_utf8.GetAddressOf())))
fprintf(stderr, "DXIL: IDxcLibrary::GetBlobAsUtf8() failed\n");
else {
char *str = reinterpret_cast<char *>(blob_utf8->GetBufferPointer());
str[blob_utf8->GetBufferSize() - 1] = 0;
*error = ralloc_strdup(val, str);
}
}
return SUCCEEDED(hr);
}
char *
dxil_disasm_module(struct dxil_validator *val, void *data, size_t size)
{
if (!val->dxc_compiler || !val->dxc_library) {
fprintf(stderr, "DXIL: disassembly requires IDxcLibrary and "
"IDxcCompiler from dxcompiler.dll\n");
return NULL;
}
ShaderBlob source(data, size);
ComPtr<IDxcBlobEncoding> blob, blob_utf8;
if (FAILED(val->dxc_compiler->Disassemble(&source, &blob))) {
fprintf(stderr, "DXIL: IDxcCompiler::Disassemble() failed\n");
return NULL;
}
if (FAILED(val->dxc_library->GetBlobAsUtf8(blob.Get(), blob_utf8.GetAddressOf()))) {
fprintf(stderr, "DXIL: IDxcLibrary::GetBlobAsUtf8() failed\n");
return NULL;
}
char *str = reinterpret_cast<char*>(blob_utf8->GetBufferPointer());
str[blob_utf8->GetBufferSize() - 1] = 0;
return ralloc_strdup(val, str);
}
enum dxil_validator_version
dxil_get_validator_version(struct dxil_validator *val)
{
return val->version;
}