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
|
// dxc-support.cpp
#include "compiler.h"
// This file implements support for invoking the `dxcompiler`
// library to translate HLSL to DXIL.
#if defined(_WIN32)
# if !defined(SLANG_ENABLE_DXIL_SUPPORT)
# define SLANG_ENABLE_DXIL_SUPPORT 1
# endif
#endif
#if !defined(SLANG_ENABLE_DXIL_SUPPORT)
# define SLANG_ENABLE_DXIL_SUPPORT 0
#endif
#if SLANG_ENABLE_DXIL_SUPPORT
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <Unknwn.h>
#include "../../external/dxc/dxcapi.h"
#undef WIN32_LEAN_AND_MEAN
#undef NOMINMAX
#include "../core/platform.h"
namespace Slang
{
char const* GetHLSLProfileName(Profile profile);
String emitHLSLForEntryPoint(
EntryPointRequest* entryPoint,
TargetRequest* targetReq);
SharedLibrary loadDXCSharedLibrary(CompileRequest* request)
{
// TODO(tfoley): Let user specify name/path of library to use.
char const* libraryName = "dxcompiler";
SharedLibrary library = SharedLibrary::load(libraryName);
if (!library)
{
request->mSink.diagnose(SourceLoc(), Diagnostics::failedToLoadDynamicLibrary, libraryName);
}
return library;
}
SharedLibrary getDXCSharedLibrary(CompileRequest* request)
{
static SharedLibrary library = loadDXCSharedLibrary(request);
return library;
}
int emitDXILForEntryPointUsingDXC(
EntryPointRequest* entryPoint,
TargetRequest* targetReq,
List<uint8_t>& outCode)
{
auto compileRequest = entryPoint->compileRequest;
// First deal with all the rigamarole of loading
// the `dxcompiler` library, and creating the
// top-level COM objects that will be used to
// compile things.
static DxcCreateInstanceProc dxcCreateInstance = nullptr;
if (!dxcCreateInstance)
{
auto dxcSharedLibrary = getDXCSharedLibrary(compileRequest);
if (!dxcSharedLibrary)
return 1;
dxcCreateInstance = (DxcCreateInstanceProc) dxcSharedLibrary.findFuncByName("DxcCreateInstance");
if (!dxcCreateInstance)
return 1;
}
IDxcCompiler* dxcCompiler = nullptr;
if (FAILED(dxcCreateInstance(
CLSID_DxcCompiler,
__uuidof(dxcCompiler),
(LPVOID*) &dxcCompiler)))
{
return 1;
}
IDxcLibrary* dxcLibrary = nullptr;
if (FAILED(dxcCreateInstance(
CLSID_DxcLibrary,
__uuidof(dxcLibrary),
(LPVOID*) &dxcLibrary)))
{
return 1;
}
// Now let's go ahead and generate HLSL for the entry
// point, since we'll need that to feed into dxc.
auto hlslCode = emitHLSLForEntryPoint(entryPoint, targetReq);
maybeDumpIntermediate(entryPoint->compileRequest, hlslCode.Buffer(), CodeGenTarget::HLSL);
// Wrap the
// Create blob from the string
IDxcBlobEncoding* dxcSourceBlob = nullptr;
if (FAILED(dxcLibrary->CreateBlobWithEncodingFromPinned(
(LPBYTE)hlslCode.Buffer(),
(UINT32)hlslCode.Length(),
0,
&dxcSourceBlob)))
{
return 1;
}
WCHAR const* args[16];
UINT32 argCount = 0;
// TODO: deal with
bool treatWarningsAsErrors = false;
if (treatWarningsAsErrors)
{
args[argCount++] = L"-WX";
}
String entryPointName = getText(entryPoint->name);
OSString wideEntryPointName = entryPointName.ToWString();
String profileName = GetHLSLProfileName(entryPoint->profile);
OSString wideProfileName = profileName.ToWString();
IDxcOperationResult* dxcResult = nullptr;
if (FAILED(dxcCompiler->Compile(dxcSourceBlob,
L"slang",
wideEntryPointName.begin(),
wideProfileName.begin(),
args,
argCount,
nullptr, // `#define`s
0, // `#define` count
nullptr, // `#include` handler
&dxcResult)))
{
return 1;
}
// Retrieve result.
HRESULT resultCode = S_OK;
if (FAILED(dxcResult->GetStatus(&resultCode)))
{
// This indicates that we failed to retrieve the reuslt...
return 1;
}
// Note: it seems like the dxcompiler interface
// doesn't support querying diagnostic output
// *unless* the compile failed (no way to get
// warnings out!?).
// Verify compile result
if (FAILED(resultCode))
{
// Compilation failed.
// Try to read any diagnostic output.
IDxcBlobEncoding* dxcErrorBlob = nullptr;
if (!FAILED(dxcResult->GetErrorBuffer(&dxcErrorBlob)))
{
compileRequest->mSink.diagnoseRaw(
FAILED(resultCode) ? Severity::Error : Severity::Warning,
(char const*)dxcErrorBlob->GetBufferPointer());
dxcErrorBlob->Release();
}
return 1;
}
// Okay, the compile supposedly succeeded, so we
// just need to grab the buffer with the output DXIL.
IDxcBlob* dxcResultBlob = nullptr;
if (FAILED(dxcResult->GetResult(&dxcResultBlob)))
{
return 1;
}
outCode.AddRange(
(uint8_t const*)dxcResultBlob->GetBufferPointer(),
(int) dxcResultBlob->GetBufferSize());
// Clean up after ourselves.
if(dxcResultBlob) dxcResultBlob ->Release();
if(dxcResult) dxcResult ->Release();
if(dxcLibrary) dxcLibrary ->Release();
if(dxcCompiler) dxcCompiler ->Release();
return 0;
}
String dissassembleDXILUsingDXC(
CompileRequest* compileRequest,
void const* data,
size_t size)
{
// First deal with all the rigamarole of loading
// the `dxcompiler` library, and creating the
// top-level COM objects that will be used to
// compile things.
static DxcCreateInstanceProc dxcCreateInstance = nullptr;
if (!dxcCreateInstance)
{
auto dxcSharedLibrary = getDXCSharedLibrary(compileRequest);
if (!dxcSharedLibrary)
return 1;
dxcCreateInstance = (DxcCreateInstanceProc) dxcSharedLibrary.findFuncByName("DxcCreateInstance");
if (!dxcCreateInstance)
return 1;
}
IDxcCompiler* dxcCompiler = nullptr;
if (FAILED(dxcCreateInstance(
CLSID_DxcCompiler,
__uuidof(dxcCompiler),
(LPVOID*) &dxcCompiler)))
{
return 1;
}
IDxcLibrary* dxcLibrary = nullptr;
if (FAILED(dxcCreateInstance(
CLSID_DxcLibrary,
__uuidof(dxcLibrary),
(LPVOID*) &dxcLibrary)))
{
return 1;
}
// Create blob from the input data
IDxcBlobEncoding* dxcSourceBlob = nullptr;
if (FAILED(dxcLibrary->CreateBlobWithEncodingFromPinned(
(LPBYTE) data,
(UINT32) size,
0,
&dxcSourceBlob)))
{
return 1;
}
IDxcBlobEncoding* dxcResultBlob = nullptr;
if(FAILED(dxcCompiler->Disassemble(
dxcSourceBlob,
&dxcResultBlob)))
{
return 1;
}
String result;
char const* codeBegin = (char const*)dxcResultBlob->GetBufferPointer();
char const* codeEnd = codeBegin + dxcResultBlob->GetBufferSize() - 1;
result.append(codeBegin, codeEnd);
if(dxcResultBlob) dxcResultBlob ->Release();
if(dxcLibrary) dxcLibrary ->Release();
if(dxcCompiler) dxcCompiler ->Release();
return result;
}
} // namespace Slang
#endif
|