Skip to content

Commit 5d269a7

Browse files
ShaderTools: Add DXCompiler library loader for macOS
1 parent 6e785a6 commit 5d269a7

File tree

6 files changed

+85
-5
lines changed

6 files changed

+85
-5
lines changed

Graphics/GraphicsEngine/interface/GraphicsTypes.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4262,6 +4262,12 @@ struct EngineMtlCreateInfo DILIGENT_DERIVE(EngineCreateInfo)
42624262
#endif
42634263
;
42644264

4265+
/// Path to DirectX Shader Compiler, which is required to use Shader Model 6.0+
4266+
/// features when compiling shaders from HLSL (e.g. mesh shaders).
4267+
4268+
/// By default, the engine will search for "libdxcompiler.dylib".
4269+
const Char* pDxCompilerPath DEFAULT_INITIALIZER(nullptr);
4270+
42654271
#if DILIGENT_CPP_INTERFACE
42664272
EngineMtlCreateInfo() noexcept :
42674273
EngineMtlCreateInfo{EngineCreateInfo{}}

Graphics/GraphicsEngineVulkan/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,16 @@ if(PLATFORM_LINUX)
243243
set(DXC_SPIRV_PATH "/usr/lib/dxc/libdxcompiler.so")
244244
endif()
245245

246+
if(PLATFORM_MACOS)
247+
if(VULKAN_SDK)
248+
if(EXISTS "${VULKAN_SDK}/lib/libdxcompiler.dylib")
249+
set(DXC_SPIRV_PATH "${VULKAN_SDK}/lib/libdxcompiler.dylib")
250+
elseif(EXISTS "${VULKAN_SDK}/macOS/lib/libdxcompiler.dylib")
251+
set(DXC_SPIRV_PATH "${VULKAN_SDK}/macOS/lib/libdxcompiler.dylib")
252+
endif()
253+
endif()
254+
endif()
255+
246256
if(EXISTS ${DXC_SPIRV_PATH})
247257
set(DILIGENT_DXCOMPILER_FOR_SPIRV_PATH "${DXC_SPIRV_PATH}" CACHE INTERNAL "" FORCE)
248258
message(STATUS "Found DXCompiler for Vulkan: " ${DILIGENT_DXCOMPILER_FOR_SPIRV_PATH})

Graphics/ShaderTools/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ set(SOURCE
1919
)
2020

2121
set(DXC_SUPPORTED FALSE)
22-
if((PLATFORM_WIN32 AND NOT MINGW_BUILD) OR PLATFORM_UNIVERSAL_WINDOWS OR PLATFORM_LINUX)
22+
if((PLATFORM_WIN32 AND NOT MINGW_BUILD) OR PLATFORM_UNIVERSAL_WINDOWS OR PLATFORM_LINUX OR PLATFORM_MACOS)
2323
set(DXC_SUPPORTED TRUE)
2424
endif()
2525

@@ -73,11 +73,13 @@ if (DXC_SUPPORTED)
7373
list(APPEND SOURCE src/DXCompilerLibraryUWP.cpp)
7474
elseif(PLATFORM_LINUX)
7575
list(APPEND SOURCE src/DXCompilerLibraryLinux.cpp)
76+
elseif(PLATFORM_MACOS)
77+
list(APPEND SOURCE src/DXCompilerLibraryMacOS.cpp)
7678
else()
7779
message(FATAL_ERROR "Unexpected platform")
7880
endif()
7981

80-
if(PLATFORM_LINUX)
82+
if(PLATFORM_LINUX OR PLATFORM_MACOS)
8183
list(APPEND INCLUDE
8284
../../ThirdParty/DirectXShaderCompiler/dxc/dxcapi.h
8385
../../ThirdParty/DirectXShaderCompiler/dxc/WinAdapter.h

Graphics/ShaderTools/include/DXCompilerLibrary.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024-2025 Diligent Graphics LLC
2+
* Copyright 2024-2026 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@
3737
# include <Unknwn.h>
3838
# include "WinHPostface.h"
3939

40-
#elif PLATFORM_LINUX
40+
#elif PLATFORM_LINUX || PLATFORM_MACOS
4141

4242
#else
4343
# error Unsupported platform
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2026 Diligent Graphics LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* In no event and under no legal theory, whether in tort (including negligence),
17+
* contract, or otherwise, unless required by applicable law (such as deliberate
18+
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
19+
* liable for any damages, including any direct, indirect, special, incidental,
20+
* or consequential damages of any character arising as a result of this License or
21+
* out of the use or inability to use the software (including but not limited to damages
22+
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
23+
* all other commercial damages or losses), even if such Contributor has been advised
24+
* of the possibility of such damages.
25+
*/
26+
27+
#include "DXCompilerLibrary.hpp"
28+
29+
#include <dlfcn.h>
30+
31+
namespace Diligent
32+
{
33+
34+
void DXCompilerLibrary::Load()
35+
{
36+
if (!m_LibName.empty())
37+
m_Library = dlopen(m_LibName.c_str(), RTLD_LOCAL | RTLD_LAZY);
38+
39+
if (m_Library == nullptr)
40+
m_Library = dlopen("libdxcompiler.dylib", RTLD_LOCAL | RTLD_LAZY);
41+
42+
// try to load from Homebrew path
43+
if (m_Library == nullptr)
44+
m_Library = dlopen("/usr/local/lib/libdxcompiler.dylib", RTLD_LOCAL | RTLD_LAZY);
45+
46+
// try to load from Homebrew on Apple Silicon
47+
if (m_Library == nullptr)
48+
m_Library = dlopen("/opt/homebrew/lib/libdxcompiler.dylib", RTLD_LOCAL | RTLD_LAZY);
49+
50+
m_DxcCreateInstance = m_Library != nullptr ? reinterpret_cast<DxcCreateInstanceProc>(dlsym(m_Library, "DxcCreateInstance")) : nullptr;
51+
}
52+
53+
void DXCompilerLibrary::Unload()
54+
{
55+
if (m_Library != nullptr)
56+
{
57+
dlclose(m_Library);
58+
m_Library = nullptr;
59+
}
60+
}
61+
62+
} // namespace Diligent

Graphics/ShaderTools/src/SPIRVShaderResources.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator,
902902

903903
VERIFY(ResourceNamesPool.GetRemainingSize() == 0, "Names pool must be empty");
904904

905-
if (m_ShaderType == SHADER_TYPE_COMPUTE)
905+
if (m_ShaderType == SHADER_TYPE_COMPUTE || m_ShaderType == SHADER_TYPE_MESH || m_ShaderType == SHADER_TYPE_AMPLIFICATION)
906906
{
907907
for (uint32_t i = 0; i < m_ComputeGroupSize.size(); ++i)
908908
m_ComputeGroupSize[i] = Compiler.get_execution_mode_argument(spv::ExecutionModeLocalSize, i);

0 commit comments

Comments
 (0)