forked from microsoft/DirectXShaderCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdxcapi.use.h
More file actions
183 lines (154 loc) · 5.65 KB
/
dxcapi.use.h
File metadata and controls
183 lines (154 loc) · 5.65 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
//////////////////////////////////////////////////////////////////////////////
// //
// dxcapi.use.h //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// Provides support for DXC API users. //
// //
///////////////////////////////////////////////////////////////////////////////
#ifndef __DXCAPI_USE_H__
#define __DXCAPI_USE_H__
#include "dxc/dxcapi.h"
namespace dxc {
extern const char *kDxCompilerLib;
extern const char *kDxilLib;
// Helper class to dynamically load the dxcompiler or a compatible libraries.
class DxcDllSupport {
protected:
HMODULE m_dll;
DxcCreateInstanceProc m_createFn;
DxcCreateInstance2Proc m_createFn2;
HRESULT InitializeInternal(LPCSTR dllName, LPCSTR fnName) {
if (m_dll != nullptr)
return S_OK;
#ifdef _WIN32
m_dll = LoadLibraryA(dllName);
if (m_dll == nullptr)
return HRESULT_FROM_WIN32(GetLastError());
m_createFn = reinterpret_cast<DxcCreateInstanceProc>(reinterpret_cast<void *>(GetProcAddress(m_dll, fnName)));
if (m_createFn == nullptr) {
HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
FreeLibrary(m_dll);
m_dll = nullptr;
return hr;
}
#else
m_dll = ::dlopen(dllName, RTLD_LAZY);
if (m_dll == nullptr)
return E_FAIL;
m_createFn = (DxcCreateInstanceProc)::dlsym(m_dll, fnName);
if (m_createFn == nullptr) {
::dlclose(m_dll);
m_dll = nullptr;
return E_FAIL;
}
#endif
// Only basic functions used to avoid requiring additional headers.
m_createFn2 = nullptr;
char fnName2[128];
size_t s = strlen(fnName);
if (s < sizeof(fnName2) - 2) {
memcpy(fnName2, fnName, s);
fnName2[s] = '2';
fnName2[s + 1] = '\0';
#ifdef _WIN32
m_createFn2 = reinterpret_cast<DxcCreateInstance2Proc>(reinterpret_cast<void *>(GetProcAddress(m_dll, fnName2)));
#else
m_createFn2 = (DxcCreateInstance2Proc)::dlsym(m_dll, fnName2);
#endif
}
return S_OK;
}
public:
DxcDllSupport() : m_dll(nullptr), m_createFn(nullptr), m_createFn2(nullptr) {}
DxcDllSupport(DxcDllSupport &&other) {
m_dll = other.m_dll;
other.m_dll = nullptr;
m_createFn = other.m_createFn;
other.m_createFn = nullptr;
m_createFn2 = other.m_createFn2;
other.m_createFn2 = nullptr;
}
~DxcDllSupport() { Cleanup(); }
HRESULT Initialize() {
return InitializeInternal(kDxCompilerLib, "DxcCreateInstance");
}
HRESULT InitializeForDll(LPCSTR dll, LPCSTR entryPoint) {
return InitializeInternal(dll, entryPoint);
}
template <typename TInterface>
HRESULT CreateInstance(REFCLSID clsid, TInterface **pResult) {
return CreateInstance(clsid, __uuidof(TInterface), (IUnknown **)pResult);
}
HRESULT CreateInstance(REFCLSID clsid, REFIID riid, IUnknown **pResult) {
if (pResult == nullptr)
return E_POINTER;
if (m_dll == nullptr)
return E_FAIL;
HRESULT hr = m_createFn(clsid, riid, (LPVOID *)pResult);
return hr;
}
template <typename TInterface>
HRESULT CreateInstance2(IMalloc *pMalloc, REFCLSID clsid,
TInterface **pResult) {
return CreateInstance2(pMalloc, clsid, __uuidof(TInterface),
(IUnknown **)pResult);
}
HRESULT CreateInstance2(IMalloc *pMalloc, REFCLSID clsid, REFIID riid,
IUnknown **pResult) {
if (pResult == nullptr)
return E_POINTER;
if (m_dll == nullptr)
return E_FAIL;
if (m_createFn2 == nullptr)
return E_FAIL;
HRESULT hr = m_createFn2(pMalloc, clsid, riid, (LPVOID *)pResult);
return hr;
}
bool HasCreateWithMalloc() const { return m_createFn2 != nullptr; }
bool IsEnabled() const { return m_dll != nullptr; }
void Cleanup() {
if (m_dll != nullptr) {
m_createFn = nullptr;
m_createFn2 = nullptr;
#ifdef _WIN32
FreeLibrary(m_dll);
#else
::dlclose(m_dll);
#endif
m_dll = nullptr;
}
}
HMODULE Detach() {
HMODULE hModule = m_dll;
m_dll = nullptr;
return hModule;
}
};
inline DxcDefine GetDefine(LPCWSTR name, LPCWSTR value) {
DxcDefine result;
result.Name = name;
result.Value = value;
return result;
}
// Checks an HRESULT and formats an error message with the appended data.
void IFT_Data(HRESULT hr, LPCWSTR data);
void EnsureEnabled(DxcDllSupport &dxcSupport);
void ReadFileIntoBlob(DxcDllSupport &dxcSupport, LPCWSTR pFileName,
IDxcBlobEncoding **ppBlobEncoding);
void WriteBlobToConsole(IDxcBlob *pBlob, DWORD streamType = STD_OUTPUT_HANDLE);
void WriteBlobToFile(IDxcBlob *pBlob, LPCWSTR pFileName, UINT32 textCodePage);
void WriteBlobToHandle(IDxcBlob *pBlob, HANDLE hFile, LPCWSTR pFileName,
UINT32 textCodePage);
void WriteUtf8ToConsole(const char *pText, int charCount,
DWORD streamType = STD_OUTPUT_HANDLE);
void WriteUtf8ToConsoleSizeT(const char *pText, size_t charCount,
DWORD streamType = STD_OUTPUT_HANDLE);
void WriteOperationErrorsToConsole(IDxcOperationResult *pResult,
bool outputWarnings);
void WriteOperationResultToConsole(IDxcOperationResult *pRewriteResult,
bool outputWarnings);
} // namespace dxc
#endif