Skip to content

Commit 3a14715

Browse files
committed
Allow diligent to link a static DirectXShaderCompiler
1 parent 1c3ac22 commit 3a14715

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

Graphics/ShaderTools/CMakeLists.txt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ set(SOURCE
1818
src/HLSLTokenizer.cpp
1919
)
2020

21+
option(DILIGENT_USE_STATIC_DXC "Link DXC statically and bypass dlopen" OFF)
22+
2123
set(DXC_SUPPORTED FALSE)
2224
if((PLATFORM_WIN32 AND NOT MINGW_BUILD) OR PLATFORM_UNIVERSAL_WINDOWS OR PLATFORM_LINUX)
2325
set(DXC_SUPPORTED TRUE)
@@ -143,8 +145,15 @@ if (DXC_SUPPORTED)
143145
endif()
144146

145147
if (NOT PLATFORM_WIN32)
146-
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR
147-
CMAKE_CXX_COMPILER_ID MATCHES "GNU")
148+
if (DILIGENT_USE_STATIC_DXC)
149+
target_compile_definitions(Diligent-ShaderTools PRIVATE DILIGENT_USE_STATIC_DXC=1)
150+
target_link_libraries(Diligent-ShaderTools
151+
PUBLIC
152+
LLVMSupport
153+
dxcompiler
154+
)
155+
endif()
156+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
148157
target_link_libraries(Diligent-ShaderTools PRIVATE dl)
149158
endif()
150159
endif()
@@ -176,6 +185,13 @@ if(ENABLE_SPIRV)
176185
endif()
177186

178187
if (${USE_SPIRV_TOOLS})
188+
if (NOT PLATFORM_WIN32 AND DILIGENT_USE_STATIC_DXC)
189+
cmake_policy(SET CMP0079 NEW)
190+
target_link_libraries(SPIRV-Tools-opt
191+
PUBLIC
192+
LLVMSupport
193+
)
194+
endif()
179195
target_link_libraries(Diligent-ShaderTools
180196
PRIVATE
181197
SPIRV-Tools-opt

Graphics/ShaderTools/src/DXCompilerLibraryLinux.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,34 @@ namespace Diligent
3333

3434
void DXCompilerLibrary::Load()
3535
{
36-
if (!m_LibName.empty())
37-
m_Library = dlopen(m_LibName.c_str(), RTLD_LOCAL | RTLD_LAZY);
36+
#if defined(DILIGENT_USE_STATIC_DXC)
37+
m_Library = reinterpret_cast<void*>(0x1);
38+
m_DxcCreateInstance = &DxcCreateInstance;
39+
#else
40+
if (!m_LibName.empty())
41+
m_Library = dlopen(m_LibName.c_str(), RTLD_LOCAL | RTLD_LAZY);
3842

39-
if (m_Library == nullptr)
40-
m_Library = dlopen("libdxcompiler.so", RTLD_LOCAL | RTLD_LAZY);
43+
if (m_Library == nullptr)
44+
m_Library = dlopen("libdxcompiler.so", RTLD_LOCAL | RTLD_LAZY);
4145

42-
// try to load from default path
43-
if (m_Library == nullptr)
44-
m_Library = dlopen("/usr/lib/dxc/libdxcompiler.so", RTLD_LOCAL | RTLD_LAZY);
46+
if (m_Library == nullptr)
47+
m_Library = dlopen("/usr/lib/dxc/libdxcompiler.so", RTLD_LOCAL | RTLD_LAZY);
4548

46-
m_DxcCreateInstance = m_Library != nullptr ? reinterpret_cast<DxcCreateInstanceProc>(dlsym(m_Library, "DxcCreateInstance")) : nullptr;
49+
m_DxcCreateInstance = m_Library != nullptr
50+
? reinterpret_cast<DxcCreateInstanceProc>(dlsym(m_Library, "DxcCreateInstance"))
51+
: nullptr;
52+
#endif
4753
}
4854

4955
void DXCompilerLibrary::Unload()
5056
{
51-
if (m_Library != nullptr)
52-
{
53-
dlclose(m_Library);
54-
m_Library = nullptr;
55-
}
57+
#if !defined(DILIGENT_USE_STATIC_DXC)
58+
if (m_Library != nullptr)
59+
{
60+
dlclose(m_Library);
61+
m_Library = nullptr;
62+
}
63+
#endif
5664
}
5765

5866
} // namespace Diligent

0 commit comments

Comments
 (0)