Skip to content

Commit a29f6ee

Browse files
committed
use an interface
1 parent 647c946 commit a29f6ee

File tree

3 files changed

+88
-29
lines changed

3 files changed

+88
-29
lines changed

include/dxc/Support/dxcapi.extval.h

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
#include <string>
33

44
namespace dxc {
5-
class DxcDllExtValidationSupport {
6-
// DxcompilerSupport manages the
5+
class DxcDllExtValidationSupport : public IDllSupport {
6+
// DxCompilerSupport manages the
77
// lifetime of dxcompiler.dll, while DxilExtValSupport
88
// manages the lifetime of dxil.dll
9-
dxc::DxcDllSupport DxcompilerSupport;
9+
dxc::DxcDllSupport DxCompilerSupport;
1010
dxc::DxcDllSupport DxilExtValSupport;
1111

12+
DxcCreateInstanceProc m_createFn;
13+
DxcCreateInstance2Proc m_createFn2;
14+
1215
std::string DxilDllPath;
1316
HRESULT InitializeInternal(LPCSTR fnName);
1417

@@ -18,23 +21,32 @@ class DxcDllExtValidationSupport {
1821
return !DxilDllPath.empty() && !DxilExtValSupport.IsEnabled();
1922
}
2023

24+
HRESULT CreateInstance(REFCLSID clsid, REFIID riid, IUnknown **pResult);
25+
HRESULT CreateInstance2(IMalloc *pMalloc, REFCLSID clsid, REFIID riid,
26+
IUnknown **pResult);
27+
28+
HRESULT Initialize() { return InitializeInternal("DxcCreateInstance"); }
29+
HRESULT InitializeForDll(LPCSTR dll, LPCSTR entryPoint) {
30+
return InitializeInternal(dll);
31+
}
32+
33+
bool HasCreateWithMalloc() const { return m_createFn2 != nullptr; }
34+
35+
bool IsEnabled() const { return DxCompilerSupport.IsEnabled(); }
36+
37+
bool DxcDllExtValidationSupport::GetCreateInstanceProcs(
38+
DxcCreateInstanceProc *pCreateFn,
39+
DxcCreateInstance2Proc *pCreateFn2) const;
40+
2141
void Cleanup() {
2242
DxilExtValSupport.Cleanup();
23-
DxcompilerSupport.Cleanup();
43+
DxCompilerSupport.Cleanup();
2444
}
2545

2646
HMODULE Detach() {
2747
// Can't Detach and return a handle for DxilSupport. Cleanup() instead.
2848
DxilExtValSupport.Cleanup();
29-
return DxcompilerSupport.Detach();
49+
return DxCompilerSupport.Detach();
3050
}
31-
32-
HRESULT CreateInstance(REFCLSID clsid, REFIID riid, IUnknown **pResult);
33-
HRESULT CreateInstance2(IMalloc *pMalloc, REFCLSID clsid, REFIID riid,
34-
IUnknown **pResult);
35-
36-
HRESULT Initialize() { return InitializeInternal("DxcCreateInstance"); }
37-
38-
bool IsEnabled() const { return DxcompilerSupport.IsEnabled(); }
3951
};
4052
} // namespace dxc

include/dxc/Support/dxcapi.use.h

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,51 @@ namespace dxc {
1919
extern const char *kDxCompilerLib;
2020
extern const char *kDxilLib;
2121

22+
// Interface for common dll operations
23+
class IDllSupport {
24+
public:
25+
IDllSupport() = default;
26+
27+
IDllSupport(IDllSupport &&other) = default;
28+
29+
virtual ~IDllSupport() = default;
30+
31+
virtual HRESULT Initialize() = 0;
32+
33+
virtual HRESULT InitializeForDll(LPCSTR dll, LPCSTR entryPoint) = 0;
34+
35+
template <typename TInterface>
36+
HRESULT CreateInstance(REFCLSID clsid, TInterface **pResult) {
37+
return CreateInstance(clsid, __uuidof(TInterface), (IUnknown **)pResult);
38+
}
39+
40+
virtual HRESULT CreateInstance(REFCLSID clsid, REFIID riid,
41+
IUnknown **pResult) = 0;
42+
43+
template <typename TInterface>
44+
HRESULT CreateInstance2(IMalloc *pMalloc, REFCLSID clsid,
45+
TInterface **pResult) {
46+
return CreateInstance2(pMalloc, clsid, __uuidof(TInterface),
47+
(IUnknown **)pResult);
48+
}
49+
50+
virtual HRESULT CreateInstance2(IMalloc *pMalloc, REFCLSID clsid, REFIID riid,
51+
IUnknown **pResult) = 0;
52+
53+
virtual bool HasCreateWithMalloc() const = 0;
54+
55+
virtual bool IsEnabled() const = 0;
56+
57+
virtual bool
58+
GetCreateInstanceProcs(DxcCreateInstanceProc *pCreateFn,
59+
DxcCreateInstance2Proc *pCreateFn2) const = 0;
60+
61+
virtual void Cleanup() = 0;
62+
virtual HMODULE Detach() = 0;
63+
};
64+
2265
// Helper class to dynamically load the dxcompiler or a compatible libraries.
23-
class DxcDllSupport {
66+
class DxcDllSupport : public IDllSupport {
2467

2568
HMODULE m_dll;
2669
DxcCreateInstanceProc m_createFn;
@@ -95,11 +138,9 @@ class DxcDllSupport {
95138
return InitializeInternal(dll, entryPoint);
96139
}
97140

98-
template <typename TInterface>
99-
HRESULT CreateInstance(REFCLSID clsid, TInterface **pResult) {
100-
return CreateInstance(clsid, __uuidof(TInterface), (IUnknown **)pResult);
101-
}
102-
141+
// Also bring visibility into the interface definition of this function
142+
// which takes 2 args
143+
using IDllSupport::CreateInstance;
103144
HRESULT CreateInstance(REFCLSID clsid, REFIID riid, IUnknown **pResult) {
104145
if (pResult == nullptr)
105146
return E_POINTER;
@@ -109,13 +150,9 @@ class DxcDllSupport {
109150
return hr;
110151
}
111152

112-
template <typename TInterface>
113-
HRESULT CreateInstance2(IMalloc *pMalloc, REFCLSID clsid,
114-
TInterface **pResult) {
115-
return CreateInstance2(pMalloc, clsid, __uuidof(TInterface),
116-
(IUnknown **)pResult);
117-
}
118-
153+
// Also bring visibility into the interface definition of this function
154+
// which takes 3 args
155+
using IDllSupport::CreateInstance2;
119156
HRESULT CreateInstance2(IMalloc *pMalloc, REFCLSID clsid, REFIID riid,
120157
IUnknown **pResult) {
121158
if (pResult == nullptr)

lib/DxcSupport/dxcapi.extval.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ HRESULT DxcDllExtValidationSupport::CreateInstance(REFCLSID clsid, REFIID riid,
1111
if (DxilExtValSupport.IsEnabled() && clsid == CLSID_DxcValidator)
1212
return DxilExtValSupport.CreateInstance(clsid, riid, pResult);
1313

14-
return DxcompilerSupport.CreateInstance(clsid, riid, pResult);
14+
return DxCompilerSupport.CreateInstance(clsid, riid, pResult);
1515
}
1616

1717
HRESULT DxcDllExtValidationSupport::CreateInstance2(IMalloc *pMalloc,
@@ -20,12 +20,12 @@ HRESULT DxcDllExtValidationSupport::CreateInstance2(IMalloc *pMalloc,
2020
if (DxilExtValSupport.IsEnabled() && clsid == CLSID_DxcValidator)
2121
return DxilExtValSupport.CreateInstance2(pMalloc, clsid, riid, pResult);
2222

23-
return DxcompilerSupport.CreateInstance2(pMalloc, clsid, riid, pResult);
23+
return DxCompilerSupport.CreateInstance2(pMalloc, clsid, riid, pResult);
2424
}
2525

2626
HRESULT DxcDllExtValidationSupport::InitializeInternal(LPCSTR fnName) {
2727
// Load dxcompiler.dll
28-
HRESULT Result = DxcompilerSupport.InitializeForDll(kDxCompilerLib, fnName);
28+
HRESULT Result = DxCompilerSupport.InitializeForDll(kDxCompilerLib, fnName);
2929
// if dxcompiler.dll fails to load, return the failed HRESULT
3030
if (DXC_FAILED(Result)) {
3131
return Result;
@@ -48,4 +48,14 @@ HRESULT DxcDllExtValidationSupport::InitializeInternal(LPCSTR fnName) {
4848
return DxilExtValSupport.InitializeForDll(DxilDllPath.c_str(), fnName);
4949
}
5050

51+
bool DxcDllExtValidationSupport::GetCreateInstanceProcs(
52+
DxcCreateInstanceProc *pCreateFn,
53+
DxcCreateInstance2Proc *pCreateFn2) const {
54+
if (pCreateFn == nullptr || pCreateFn2 == nullptr || m_createFn == nullptr)
55+
return false;
56+
*pCreateFn = m_createFn;
57+
*pCreateFn2 = m_createFn2;
58+
return true;
59+
}
60+
5161
} // namespace dxc

0 commit comments

Comments
 (0)