-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmach_dxc.cpp
More file actions
250 lines (198 loc) · 8.08 KB
/
mach_dxc.cpp
File metadata and controls
250 lines (198 loc) · 8.08 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
// TODO: investigate if we can eliminate this for Windows builds
#ifdef _WIN32
#ifdef _MSC_VER
#define __C89_NAMELESS
#define __C89_NAMELESSUNIONNAME
#include <atlbase.h>
#include <locale.h>
#include <windows.h>
#else // _MSC_VER
#include <windows.h>
#include <wrl/client.h>
#endif // _MSC_VER
#endif // _WIN32
// Avoid __declspec(dllimport) since dxcompiler is static.
#define DXC_API_IMPORT
#include <dxcapi.h>
#include <cassert>
#include <stddef.h>
#include <string>
#include "mach_dxc.h"
#include "dxc/Support/FileIOHelper.h"
#ifdef __cplusplus
extern "C" {
#endif
// Dynamic allocation for multibyte string conversion.
char* wcstombsAlloc(const wchar_t* inval)
{
size_t size = std::wcslen(inval);
size_t outsz = (size + 1) * MB_CUR_MAX;
auto buf = (char*)std::malloc(outsz);
std::memset(buf, 0, outsz);
setlocale(LC_CTYPE,"");
size = std::wcstombs(buf, inval, size * sizeof(wchar_t));
if (size == (size_t)(-1)) {
std::free(buf);
buf = nullptr;
} else {
buf = (char*)std::realloc(buf, size + 1);
}
return buf;
}
// Provides a way for C applications to override file inclusion by offloading it to a function pointer
class MachDxcIncludeHandler : public IDxcIncludeHandler
{
public:
ULONG STDMETHODCALLTYPE AddRef() override { return 0; }
ULONG STDMETHODCALLTYPE Release() override { return 0; }
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject) override {
if (riid == __uuidof(IDxcIncludeHandler) || riid == __uuidof(IUnknown)) {
*ppvObject = this;
return S_OK;
}
*ppvObject = nullptr;
return E_NOINTERFACE;
}
MachDxcIncludeCallbacks* callbacks;
IDxcUtils* utils;
MachDxcIncludeHandler(MachDxcIncludeCallbacks* callbacks_ptr, IDxcUtils* util_ptr) {
callbacks = callbacks_ptr;
utils = util_ptr;
}
HRESULT STDMETHODCALLTYPE LoadSource(LPCWSTR filename, IDxcBlob **ppIncludeSource) override {
if (callbacks->include_func == nullptr || callbacks->free_func == nullptr)
return E_POINTER;
char* filename_utf8 = wcstombsAlloc(filename);
if (filename_utf8 == nullptr)
filename_utf8 = _strdup(u8"");
MachDxcIncludeResult* include_result = callbacks->include_func(callbacks->include_ctx, filename_utf8);
std::free(filename_utf8);
const char* include_text = include_result != nullptr && include_result->header_data != nullptr ? include_result->header_data : u8"";
size_t include_len = include_result != nullptr ? include_result->header_length : 0;
CComPtr<IDxcBlobEncoding> text_blob;
HRESULT result = utils->CreateBlob(include_text, include_len, CP_UTF8, &text_blob);
if (SUCCEEDED(result))
*ppIncludeSource = text_blob.Detach();
callbacks->free_func(callbacks->include_ctx, include_result);
return S_OK;
}
};
// Mach change start: static dxcompiler/dxil
BOOL MachDxcompilerInvokeDllMain();
void MachDxcompilerInvokeDllShutdown();
//----------------
// MachDxcCompiler
//----------------
MACH_EXPORT MachDxcCompiler machDxcInit() {
MachDxcompilerInvokeDllMain();
CComPtr<IDxcCompiler3> dxcInstance;
HRESULT hr = DxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&dxcInstance));
assert(SUCCEEDED(hr));
return reinterpret_cast<MachDxcCompiler>(dxcInstance.Detach());
}
MACH_EXPORT void machDxcDeinit(MachDxcCompiler compiler) {
CComPtr<IDxcCompiler3> dxcInstance = CComPtr<IDxcCompiler3>(reinterpret_cast<IDxcCompiler3*>(compiler));
dxcInstance.Release();
MachDxcompilerInvokeDllShutdown();
}
//---------------------
// MachDxcCompileResult
//---------------------
MACH_EXPORT MachDxcCompileResult machDxcCompile(
MachDxcCompiler compiler,
MachDxcCompileOptions* options
) {
CComPtr<IDxcCompiler3> dxcInstance = CComPtr<IDxcCompiler3>(reinterpret_cast<IDxcCompiler3*>(compiler));
CComPtr<IDxcUtils> pUtils;
DxcCreateInstance(CLSID_DxcUtils, IID_PPV_ARGS(&pUtils));
CComPtr<IDxcBlobEncoding> pSource;
pUtils->CreateBlob(options->code, options->code_len, CP_UTF8, &pSource);
DxcBuffer sourceBuffer;
sourceBuffer.Ptr = pSource->GetBufferPointer();
sourceBuffer.Size = pSource->GetBufferSize();
sourceBuffer.Encoding = 0;
// We have args in char form, but dxcInstance->Compile expects wchar_t form.
LPCWSTR* arguments = (LPCWSTR*)malloc(sizeof(LPCWSTR) * options->args_len);
wchar_t* wtext_buf = (wchar_t*)malloc(4096);
wchar_t* wtext_cursor = wtext_buf;
assert(arguments);
assert(wtext_buf);
for (int i=0; i < options->args_len; i++) {
size_t available = 4096 / sizeof(wchar_t) - (wtext_cursor - wtext_buf);
size_t written = std::mbstowcs(wtext_cursor, options->args[i], available);
arguments[i] = wtext_cursor;
wtext_cursor += written + 1;
}
MachDxcIncludeHandler* handler = nullptr;
if (options->include_callbacks != nullptr) // Leave include handler as default (nullptr) unless there's available callbacks
handler = new MachDxcIncludeHandler(options->include_callbacks, pUtils);
CComPtr<IDxcResult> pCompileResult;
HRESULT hr = dxcInstance->Compile(
&sourceBuffer,
arguments,
(uint32_t)options->args_len,
handler,
IID_PPV_ARGS(&pCompileResult)
);
if (handler != nullptr)
delete handler;
assert(SUCCEEDED(hr));
free(arguments);
free(wtext_buf);
return reinterpret_cast<MachDxcCompileResult>(pCompileResult.Detach());
}
MACH_EXPORT MachDxcCompileError machDxcCompileResultGetError(MachDxcCompileResult err) {
CComPtr<IDxcResult> pCompileResult = CComPtr<IDxcResult>(reinterpret_cast<IDxcResult*>(err));
CComPtr<IDxcBlobEncoding> pErrors = nullptr;
HRESULT hr = pCompileResult->GetErrorBuffer(&pErrors);
if (hr == S_OK && !hlsl::IsBlobNullOrEmpty(pErrors)) {
return reinterpret_cast<MachDxcCompileError>(pErrors.Detach());
}
return nullptr;
}
MACH_EXPORT MachDxcCompileObject machDxcCompileResultGetObject(MachDxcCompileResult err) {
CComPtr<IDxcResult> pCompileResult = CComPtr<IDxcResult>(reinterpret_cast<IDxcResult*>(err));
CComPtr<IDxcBlob> pObject = nullptr;
HRESULT hr = pCompileResult->GetResult(&pObject);
if (hr == S_OK && !hlsl::IsBlobNullOrEmpty(pObject)) {
return reinterpret_cast<MachDxcCompileObject>(pObject.Detach());
}
return nullptr;
}
MACH_EXPORT void machDxcCompileResultDeinit(MachDxcCompileResult err) {
CComPtr<IDxcResult> pCompileResult = CComPtr<IDxcResult>(reinterpret_cast<IDxcResult*>(err));
pCompileResult.Release();
}
//---------------------
// MachDxcCompileObject
//---------------------
MACH_EXPORT char const* machDxcCompileObjectGetBytes(MachDxcCompileObject err) {
CComPtr<IDxcBlob> pObject = CComPtr<IDxcBlob>(reinterpret_cast<IDxcBlob*>(err));
return (char const*)(pObject->GetBufferPointer());
}
MACH_EXPORT size_t machDxcCompileObjectGetBytesLength(MachDxcCompileObject err) {
CComPtr<IDxcBlob> pObject = CComPtr<IDxcBlob>(reinterpret_cast<IDxcBlob*>(err));
return pObject->GetBufferSize();
}
MACH_EXPORT void machDxcCompileObjectDeinit(MachDxcCompileObject err) {
CComPtr<IDxcBlob> pObject = CComPtr<IDxcBlob>(reinterpret_cast<IDxcBlob*>(err));
pObject.Release();
}
//--------------------
// MachDxcCompileError
//--------------------
MACH_EXPORT char const* machDxcCompileErrorGetString(MachDxcCompileError err) {
CComPtr<IDxcBlobUtf8> pErrors = CComPtr<IDxcBlobUtf8>(reinterpret_cast<IDxcBlobUtf8*>(err));
return (char const*)(pErrors->GetBufferPointer());
}
MACH_EXPORT size_t machDxcCompileErrorGetStringLength(MachDxcCompileError err) {
CComPtr<IDxcBlobUtf8> pErrors = CComPtr<IDxcBlobUtf8>(reinterpret_cast<IDxcBlobUtf8*>(err));
return pErrors->GetStringLength();
}
MACH_EXPORT void machDxcCompileErrorDeinit(MachDxcCompileError err) {
CComPtr<IDxcBlobUtf8> pErrors = CComPtr<IDxcBlobUtf8>(reinterpret_cast<IDxcBlobUtf8*>(err));
pErrors.Release();
}
#ifdef __cplusplus
} // extern "C"
#endif