forked from microsoft/DirectXShaderCompiler
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdxcapi.extval.cpp
More file actions
445 lines (373 loc) · 16 KB
/
dxcapi.extval.cpp
File metadata and controls
445 lines (373 loc) · 16 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#ifndef _WIN32
#include "dxc/WinAdapter.h"
#endif
#include "dxc/Support/Global.h" // for hresult handling
#include "dxc/Support/HLSLOptions.h"
#include "dxc/Support/WinIncludes.h"
#include <filesystem> // C++17 and later
#include <sstream>
// WinIncludes must come before dxcapi.extval.h
#include "dxc/Support/FileIOHelper.h"
#include "dxc/Support/dxcapi.extval.h"
#include "dxc/Support/dxcapi.impl.h"
#include "dxc/Support/microcom.h"
#include "dxc/dxcapi.internal.h"
#include <iostream>
#include "llvm/ADT/SmallVector.h"
namespace {
// The ExtValidationArgHelper class helps manage arguments for external
// validation, as well as perform the validation step if needed.
class ExtValidationArgHelper {
public:
ExtValidationArgHelper() = default;
ExtValidationArgHelper(const ExtValidationArgHelper &) = delete;
HRESULT initialize(IDxcValidator *NewValidator, LPCWSTR **Arguments,
UINT32 *ArgCount, LPCWSTR TargetProfile = nullptr) {
IFR(setValidator(NewValidator));
IFR(processArgs(Arguments, ArgCount, TargetProfile));
return S_OK;
}
HRESULT doValidation(IDxcOperationResult *CompileResult, REFIID Riid,
void **ValResult) {
if (!CompileResult)
return E_INVALIDARG;
// No validation needed; just set the result and return.
if (!NeedsValidation)
return CompileResult->QueryInterface(Riid, ValResult);
// Get the compiled shader.
CComPtr<IDxcBlob> CompiledBlob;
IFR(CompileResult->GetResult(&CompiledBlob));
// If no compiled blob; just place the compile result
// into ValResult and return.
if (!CompiledBlob)
return CompileResult->QueryInterface(Riid, ValResult);
// Validate the compiled shader.
CComPtr<IDxcOperationResult> TempValidationResult;
UINT32 DxcValidatorFlags =
DxcValidatorFlags_InPlaceEdit |
(RootSignatureOnly ? DxcValidatorFlags_RootSignatureOnly : 0);
IFR(Validator->Validate(CompiledBlob, DxcValidatorFlags,
&TempValidationResult));
// Return the validation result if it failed.
HRESULT HR;
IFR(TempValidationResult->GetStatus(&HR));
if (FAILED(HR)) {
// prefix the stderr output
fprintf(stderr, "error: validation errors\r\n");
return TempValidationResult->QueryInterface(Riid, ValResult);
}
// Validation succeeded. Return the original compile result.
return CompileResult->QueryInterface(Riid, ValResult);
}
private:
std::vector<std::wstring> ArgStorage;
llvm::SmallVector<LPCWSTR, 16> NewArgs;
hlsl::options::DxcOpts Opts;
CComPtr<IDxcValidator> Validator;
UINT32 ValidatorVersionMajor = 0;
UINT32 ValidatorVersionMinor = 0;
bool NeedsValidation = false;
bool RootSignatureOnly = false;
/// Add argument to ArgStorage to be referenced by NewArgs.
void addArgument(const std::wstring &Arg) { ArgStorage.push_back(Arg); }
HRESULT setValidator(IDxcValidator *NewValidator) {
DXASSERT(NewValidator, "Invalid Validator argument");
Validator = NewValidator;
// 1.0 had no version interface.
ValidatorVersionMajor = 1;
ValidatorVersionMinor = 0;
CComPtr<IDxcVersionInfo> ValidatorVersionInfo;
if (SUCCEEDED(
Validator->QueryInterface(IID_PPV_ARGS(&ValidatorVersionInfo)))) {
if (FAILED(ValidatorVersionInfo->GetVersion(&ValidatorVersionMajor,
&ValidatorVersionMinor))) {
DXASSERT(false, "Failed to get validator version");
return E_FAIL;
}
}
return S_OK;
}
/// Process arguments, adding validator version and disable validation if
/// needed. If TargetProfile provided, use this instead of -T argument.
HRESULT processArgs(LPCWSTR **Arguments, UINT32 *ArgCount,
LPCWSTR TargetProfile = nullptr) {
DXASSERT((Arguments && ArgCount) && (*Arguments || *ArgCount == 0) &&
*ArgCount < INT_MAX - 3,
"Invalid Arguments and ArgCount arguments");
NeedsValidation = false;
// Must not call twice.
DXASSERT(NewArgs.empty(), "Must not call twice");
// Parse compiler arguments to Opts.
std::string Errors;
llvm::raw_string_ostream OS(Errors);
hlsl::options::MainArgs MainArgs(static_cast<int>(*ArgCount), *Arguments,
0);
if (hlsl::options::ReadDxcOpts(hlsl::options::getHlslOptTable(),
hlsl::options::CompilerFlags, MainArgs, Opts,
OS))
return E_FAIL;
// Determine important conditions from arguments.
const bool ProduceDxModule = Opts.ProduceDxModule();
NeedsValidation = Opts.NeedsValidation();
RootSignatureOnly = Opts.IsRootSignatureProfile();
// Add extra arguments as needed
if (ProduceDxModule) {
if (Opts.ValVerMajor == UINT_MAX) {
// If not supplied, add validator version arguments.
addArgument(L"-validator-version");
std::wstring VerStr = std::to_wstring(ValidatorVersionMajor) + L"." +
std::to_wstring(ValidatorVersionMinor);
addArgument(VerStr);
}
// Add argument to disable validation, so we can call it ourselves
// later.
if (NeedsValidation)
addArgument(L"-Vd");
}
if (ArgStorage.empty())
return S_OK;
// Reference added arguments from storage.
NewArgs.reserve(*ArgCount + ArgStorage.size());
// Copied arguments refer to original strings.
for (UINT32 I = 0; I < *ArgCount; ++I)
NewArgs.push_back((*Arguments)[I]);
for (const auto &Arg : ArgStorage)
NewArgs.push_back(Arg.c_str());
*Arguments = NewArgs.data();
*ArgCount = (UINT32)NewArgs.size();
return S_OK;
}
};
// ExternalValidationCompiler wraps a DxCompiler to use an external validator
// instead of the internal one. It disables internal validation by adding
// '-Vd' and sets the default validator version with '-validator-version'.
// After a successful compilation, it uses the provided IDxcValidator to
// perform validation when it would normally be performed.
class ExternalValidationCompiler : public IDxcCompiler2, public IDxcCompiler3 {
public:
ExternalValidationCompiler(IMalloc *Malloc, IDxcValidator *OtherValidator,
IUnknown *OtherCompiler)
: Validator(OtherValidator), Compiler(OtherCompiler), m_pMalloc(Malloc) {}
// IUnknown implementation
DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL()
DXC_MICROCOM_TM_ALLOC(ExternalValidationCompiler)
// QueryInterface creates a new wrapper for the correct compiler interface
// retrieved by calling QueryInterface on the compiler. The new wrapper
// keeps the IID and returned compiler interface pointer for use in
// associated interface methods. Those methods can then use
// castCompilerUnsafe to upcast the pointer to the appropriate interface
// for the method.
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID Iid,
void **ResultObject) override {
if (!ResultObject)
return E_POINTER;
*ResultObject = nullptr;
// Get pointer for an interface the compiler object supports.
CComPtr<IUnknown> TempCompiler;
IFR(Compiler->QueryInterface(Iid, (void **)&TempCompiler));
try {
// This can throw; do not leak C++ exceptions through COM method
// calls.
CComPtr<ExternalValidationCompiler> NewWrapper(
Alloc(m_pMalloc, Validator, TempCompiler));
return DoBasicQueryInterface<IDxcCompiler, IDxcCompiler2, IDxcCompiler3>(
NewWrapper.p, Iid, ResultObject);
} catch (...) {
return E_FAIL;
}
}
// IDxcCompiler implementation
HRESULT STDMETHODCALLTYPE
Compile(IDxcBlob *Source, LPCWSTR SourceName, LPCWSTR EntryPoint,
LPCWSTR TargetProfile, LPCWSTR *Arguments, UINT32 ArgCount,
const DxcDefine *Defines, UINT32 DefineCount,
IDxcIncludeHandler *IncludeHandler,
IDxcOperationResult **ResultObject) override {
if (ResultObject == nullptr)
return E_INVALIDARG;
DxcThreadMalloc TM(m_pMalloc);
// initialize will update Arguments and ArgCount if needed.
ExtValidationArgHelper Helper;
IFR(Helper.initialize(Validator, &Arguments, &ArgCount, TargetProfile));
CComPtr<IDxcOperationResult> CompileResult;
IFR(cast<IDxcCompiler>()->Compile(
Source, SourceName, EntryPoint, TargetProfile, Arguments, ArgCount,
Defines, DefineCount, IncludeHandler, &CompileResult));
HRESULT CompileHR;
CompileResult->GetStatus(&CompileHR);
if (SUCCEEDED(CompileHR))
return Helper.doValidation(CompileResult, IID_PPV_ARGS(ResultObject));
CompileResult->QueryInterface(ResultObject);
return S_OK;
}
HRESULT STDMETHODCALLTYPE
Preprocess(IDxcBlob *Source, LPCWSTR SourceName, LPCWSTR *Arguments,
UINT32 ArgCount, const DxcDefine *Defines, UINT32 DefineCount,
IDxcIncludeHandler *IncludeHandler,
IDxcOperationResult **ResultObject) override {
return cast<IDxcCompiler>()->Preprocess(Source, SourceName, Arguments,
ArgCount, Defines, DefineCount,
IncludeHandler, ResultObject);
}
HRESULT STDMETHODCALLTYPE
Disassemble(IDxcBlob *Source, IDxcBlobEncoding **Disassembly) override {
return cast<IDxcCompiler>()->Disassemble(Source, Disassembly);
}
// IDxcCompiler2 implementation
HRESULT STDMETHODCALLTYPE CompileWithDebug(
IDxcBlob *Source, LPCWSTR SourceName, LPCWSTR EntryPoint,
LPCWSTR TargetProfile, LPCWSTR *Arguments, UINT32 ArgCount,
const DxcDefine *pDefines, UINT32 DefineCount,
IDxcIncludeHandler *IncludeHandler, IDxcOperationResult **ResultObject,
LPWSTR *DebugBlobName, IDxcBlob **DebugBlob) override {
if (ResultObject == nullptr)
return E_INVALIDARG;
DxcThreadMalloc TM(m_pMalloc);
// ProcessArgs will update Arguments and ArgCount if needed.
ExtValidationArgHelper Helper;
IFR(Helper.initialize(Validator, &Arguments, &ArgCount, TargetProfile));
CComPtr<IDxcOperationResult> CompileResult;
IFR(cast<IDxcCompiler2>()->CompileWithDebug(
Source, SourceName, EntryPoint, TargetProfile, Arguments, ArgCount,
pDefines, DefineCount, IncludeHandler, &CompileResult, DebugBlobName,
DebugBlob));
return Helper.doValidation(CompileResult, IID_PPV_ARGS(ResultObject));
}
// IDxcCompiler3 implementation
HRESULT STDMETHODCALLTYPE Compile(const DxcBuffer *Source, LPCWSTR *Arguments,
UINT32 ArgCount,
IDxcIncludeHandler *IncludeHandler,
REFIID Riid,
LPVOID *ResultObject) override {
if (ResultObject == nullptr)
return E_INVALIDARG;
DxcThreadMalloc TM(m_pMalloc);
// ProcessArgs will update Arguments and ArgCount if needed.
ExtValidationArgHelper Helper;
Helper.initialize(Validator, &Arguments, &ArgCount);
CComPtr<IDxcResult> CompileResult;
IFR(cast<IDxcCompiler3>()->Compile(Source, Arguments, ArgCount,
IncludeHandler,
IID_PPV_ARGS(&CompileResult)));
return Helper.doValidation(CompileResult, Riid, ResultObject);
}
HRESULT STDMETHODCALLTYPE Disassemble(const DxcBuffer *Object, REFIID Riid,
LPVOID *ResultObject) override {
return cast<IDxcCompiler3>()->Disassemble(Object, Riid, ResultObject);
}
private:
CComPtr<IDxcValidator> Validator;
// This wrapper wraps one particular compiler interface.
// When QueryInterface is called, we create a new wrapper
// for the requested interface, which wraps the result of QueryInterface
// on the compiler object. Compiler pointer is held as IUnknown and must
// be upcast to the appropriate interface on use.
CComPtr<IUnknown> Compiler;
DXC_MICROCOM_TM_REF_FIELDS()
template <typename T> CComPtr<T> cast() const {
CComPtr<T> Result;
if (Compiler)
Compiler.QueryInterface(&Result);
assert(Result);
return Result;
}
};
} // namespace
namespace dxc {
static HRESULT createCompilerWrapper(IMalloc *Malloc,
SpecificDllLoader &DxCompilerSupport,
SpecificDllLoader &DxilExtValSupport,
REFIID Riid, IUnknown **ResultObject) {
DXASSERT(ResultObject, "Invalid ResultObject");
*ResultObject = nullptr;
CComPtr<IUnknown> Compiler;
CComPtr<IDxcValidator> Validator;
// Create compiler and validator
if (Malloc) {
IFR(DxCompilerSupport.CreateInstance2(Malloc, CLSID_DxcCompiler, Riid,
&Compiler));
IFR(DxilExtValSupport.CreateInstance2<IDxcValidator>(
Malloc, CLSID_DxcValidator, &Validator));
} else {
IFR(DxCompilerSupport.CreateInstance(CLSID_DxcCompiler, Riid, &Compiler));
IFR(DxilExtValSupport.CreateInstance<IDxcValidator>(CLSID_DxcValidator,
&Validator));
}
// Wrap compiler
CComPtr<ExternalValidationCompiler> CompilerWrapper =
ExternalValidationCompiler::Alloc(
Malloc ? Malloc : DxcGetThreadMallocNoRef(), Validator, Compiler);
return CompilerWrapper->QueryInterface(Riid, (void **)ResultObject);
}
HRESULT DxcDllExtValidationLoader::CreateInstanceImpl(REFCLSID Clsid,
REFIID Riid,
IUnknown **ResultObject) {
if (!ResultObject)
return E_POINTER;
*ResultObject = nullptr;
// If there is intent to use an external dxil.dll
if (!DxilDllPath.empty() && !dxilDllFailedToLoad()) {
if (Clsid == CLSID_DxcValidator) {
return DxilExtValSupport.CreateInstance(Clsid, Riid, ResultObject);
}
if (Clsid == CLSID_DxcCompiler) {
return createCompilerWrapper(nullptr, DxCompilerSupport,
DxilExtValSupport, Riid, ResultObject);
}
}
// Fallback: let DxCompiler handle it
return DxCompilerSupport.CreateInstance(Clsid, Riid, ResultObject);
}
HRESULT DxcDllExtValidationLoader::CreateInstance2Impl(
IMalloc *Malloc, REFCLSID Clsid, REFIID Riid, IUnknown **ResultObject) {
if (!ResultObject)
return E_POINTER;
*ResultObject = nullptr;
// If there is intent to use an external dxil.dll
if (!DxilDllPath.empty() && !dxilDllFailedToLoad()) {
if (Clsid == CLSID_DxcValidator) {
return DxilExtValSupport.CreateInstance2(Malloc, Clsid, Riid,
ResultObject);
}
if (Clsid == CLSID_DxcCompiler) {
return createCompilerWrapper(Malloc, DxCompilerSupport, DxilExtValSupport,
Riid, ResultObject);
}
}
// Fallback: let DxCompiler handle it
return DxCompilerSupport.CreateInstance2(Malloc, Clsid, Riid, ResultObject);
}
HRESULT
DxcDllExtValidationLoader::InitializeForDll(LPCSTR dllName, LPCSTR fnName) {
// Load dxcompiler.dll
HRESULT Result = DxCompilerSupport.InitializeForDll(dllName, fnName);
// if dxcompiler.dll fails to load, return the failed HRESULT
if (FAILED(Result)) {
FailureReason = FailedCompilerLoad;
return Result;
}
// now handle external dxil.dll
const char *EnvVarVal = std::getenv("DXC_DXIL_DLL_PATH");
if (!EnvVarVal || std::string(EnvVarVal).empty()) {
// no need to emit anything if external validation isn't wanted
return S_OK;
}
DxilDllPath = std::string(EnvVarVal);
std::filesystem::path DllPath(DxilDllPath);
// Check if path is absolute and exists
if (!DllPath.is_absolute() || !std::filesystem::exists(DllPath)) {
FailureReason = FailedDxilPath;
return E_INVALIDARG;
}
Result = DxilExtValSupport.InitializeForDll(DxilDllPath.c_str(),
"DxcCreateInstance");
if (FAILED(Result)) {
FailureReason = FailedDxilLoad;
return Result;
}
return Result;
}
HRESULT DxcDllExtValidationLoader::initialize() {
return InitializeForDll(kDxCompilerLib, "DxcCreateInstance");
}
} // namespace dxc