Skip to content

Commit d7d1936

Browse files
committed
implement stage 2
1 parent adffd31 commit d7d1936

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed

include/dxc/Support/dxcapi.extval.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "dxc/Support/dxcapi.use.h"
2+
#include "dxc/dxcapi.h"
3+
4+
class DxcDllExtValidationSupport : public dxc::DxcDllSupport {
5+
// DxcDllExtValidationSupport manages the
6+
// lifetime of dxcompiler.dll, while the member, m_DxilSupport,
7+
// manages the lifetime of dxil.dll
8+
dxc::DxcDllSupport m_DxilSupport;
9+
10+
std::string DxilDllPath;
11+
// override DxcDllSupport's implementation of InitializeInternal,
12+
// adding the environment variable value check for a path to a dxil.dll
13+
14+
std::string GetDxilDLLPathExt() { return DxilDllPath; }
15+
bool DxilDllSuccessfullyLoaded() {
16+
return !DxilDllPath.empty() && !m_DxilSupport.IsEnabled();
17+
}
18+
19+
HRESULT InitializeInternal(LPCSTR dllName, LPCSTR fnName);
20+
};

include/dxc/Support/dxcapi.use.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#define __DXCAPI_USE_H__
1414

1515
#include "dxc/dxcapi.h"
16+
#include <cstdlib> // for getenv
17+
#include <string>
18+
#include <filesystem> // C++17 and later
19+
#include <dxc/Support/Global.h> // for hresult handling with DXC_FAILED
1620

1721
namespace dxc {
1822

@@ -85,13 +89,13 @@ class DxcDllSupport {
8589
other.m_createFn2 = nullptr;
8690
}
8791

88-
~DxcDllSupport() { Cleanup(); }
92+
virtual ~DxcDllSupport() { Cleanup(); }
8993

90-
HRESULT Initialize() {
94+
HRESULT virtual Initialize() {
9195
return InitializeInternal(kDxCompilerLib, "DxcCreateInstance");
9296
}
9397

94-
HRESULT InitializeForDll(LPCSTR dll, LPCSTR entryPoint) {
98+
HRESULT virtual InitializeForDll(LPCSTR dll, LPCSTR entryPoint) {
9599
return InitializeInternal(dll, entryPoint);
96100
}
97101

@@ -100,7 +104,8 @@ class DxcDllSupport {
100104
return CreateInstance(clsid, __uuidof(TInterface), (IUnknown **)pResult);
101105
}
102106

103-
HRESULT CreateInstance(REFCLSID clsid, REFIID riid, IUnknown **pResult) {
107+
HRESULT virtual CreateInstance(REFCLSID clsid, REFIID riid,
108+
IUnknown **pResult) {
104109
if (pResult == nullptr)
105110
return E_POINTER;
106111
if (m_dll == nullptr)
@@ -116,7 +121,7 @@ class DxcDllSupport {
116121
(IUnknown **)pResult);
117122
}
118123

119-
HRESULT CreateInstance2(IMalloc *pMalloc, REFCLSID clsid, REFIID riid,
124+
HRESULT virtual CreateInstance2(IMalloc *pMalloc, REFCLSID clsid, REFIID riid,
120125
IUnknown **pResult) {
121126
if (pResult == nullptr)
122127
return E_POINTER;
@@ -128,11 +133,11 @@ class DxcDllSupport {
128133
return hr;
129134
}
130135

131-
bool HasCreateWithMalloc() const { return m_createFn2 != nullptr; }
136+
bool virtual HasCreateWithMalloc() const { return m_createFn2 != nullptr; }
132137

133138
bool IsEnabled() const { return m_dll != nullptr; }
134139

135-
void Cleanup() {
140+
void virtual Cleanup() {
136141
if (m_dll != nullptr) {
137142
m_createFn = nullptr;
138143
m_createFn2 = nullptr;
@@ -145,7 +150,7 @@ class DxcDllSupport {
145150
}
146151
}
147152

148-
HMODULE Detach() {
153+
HMODULE virtual Detach() {
149154
HMODULE hModule = m_dll;
150155
m_dll = nullptr;
151156
return hModule;

lib/DxcSupport/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_llvm_library(LLVMDxcSupport
1010
WinAdapter.cpp
1111
WinIncludes.cpp
1212
WinFunctions.cpp
13+
dxcapi.extval.cpp
1314
)
1415

1516
#generate header with platform-specific library name

lib/DxcSupport/dxcapi.extval.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "dxc/Support/WinIncludes.h"
2+
3+
#include "dxc/Support/dxcapi.use.h"
4+
5+
#include "dxc/Support/FileIOHelper.h"
6+
#include "dxc/Support/Global.h"
7+
#include "dxc/Support/SharedLibAffix.h" // header generated during DXC build
8+
#include "dxc/Support/Unicode.h"
9+
#include "dxc/Support/WinFunctions.h"
10+
11+
#include "dxc/Support/dxcapi.extval.h"
12+
13+
HRESULT DxcDllExtValidationSupport::InitializeInternal(LPCSTR dllName,
14+
LPCSTR fnName) {
15+
// Load dxcompiler.dll
16+
HRESULT result = InitializeForDll(dllName, fnName);
17+
// if dxcompiler.dll fails to load, return the failed HRESULT
18+
if (DXC_FAILED(result)) {
19+
return result;
20+
}
21+
22+
// now handle external dxil.dll
23+
const char *envVal = std::getenv("DXC_DXIL_DLL_PATH");
24+
if (!envVal || std::string(envVal).empty()) {
25+
return S_OK;
26+
}
27+
28+
std::string DllPathStr(envVal);
29+
DxilDllPath = DllPathStr;
30+
std::filesystem::path DllPath(DllPathStr);
31+
32+
// Check if path is absolute and exists
33+
if (!DllPath.is_absolute() || !std::filesystem::exists(DllPath)) {
34+
return S_OK;
35+
}
36+
// code that calls this function is responsible for checking
37+
// to see if dxil.dll is successfully loaded.
38+
// the CheckDxilDLLLoaded function can determine whether there were any
39+
// problems loading dxil.dll or not
40+
m_DxilSupport.InitializeForDll(DllPathStr.data(), fnName);
41+
42+
return S_OK;
43+
}

0 commit comments

Comments
 (0)