Skip to content

Commit

Permalink
Add DirectXShaderCompiler git submodule (#49)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: cosmonaut <[email protected]>
  • Loading branch information
madebr and thatcosmonaut authored Nov 7, 2024
1 parent 3385fb5 commit 933e058
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 134 deletions.
47 changes: 33 additions & 14 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ jobs:
fail-fast: false
matrix:
platform:
- { name: Windows (MSVC), os: windows-2019, shell: sh, msvc: true, artifact: 'SDL3_gpu_shadercross-VC-x64' }
- { name: Windows (mingw64), os: windows-latest, shell: 'msys2 {0}', msystem: mingw64, msys-env: mingw-w64-x86_64, artifact: 'SDL3_gpu_shadercross-mingw64' }
- { name: Ubuntu 20.04, os: ubuntu-20.04, shell: sh, artifact: 'SDL3_gpu_shadercross-linux-x64' }
- { name: Steam Linux Runtime (Sniper), os: ubuntu-latest, shell: sh, artifact: 'SDL3_gpu_shadercross-slrsniper', container: 'registry.gitlab.steamos.cloud/steamrt/sniper/sdk:beta' }
- { name: macOS, os: macos-latest, shell: sh, artifact: 'SDL3_gpu_shadercross-macos-x64' }

- { name: Windows (MSVC), os: windows-2019, build-spirv-cross: true, vendored: false, shell: sh, msvc: true, artifact: 'SDL3_gpu_shadercross-VC-x64' }
- { name: Windows (mingw64), os: windows-latest, build-spirv-cross: false, vendored: false, shell: 'msys2 {0}', msystem: mingw64, msys-env: mingw-w64-x86_64, artifact: 'SDL3_gpu_shadercross-mingw64' }
- { name: Ubuntu 22.04, os: ubuntu-22.04, build-spirv-cross: true, vendored: false, shell: sh, artifact: 'SDL3_gpu_shadercross-linux-x64' }
- { name: Steam Linux Runtime (Sniper), os: ubuntu-latest, build-spirv-cross: false, vendored: true, shell: sh, artifact: 'SDL3_gpu_shadercross-slrsniper', container: 'registry.gitlab.steamos.cloud/steamrt/sniper/sdk:beta' }
- { name: macOS, os: macos-latest, build-spirv-cross: true, vendored: true, shell: sh, artifact: 'SDL3_gpu_shadercross-macos-arm64' }

steps:
- uses: actions/checkout@v4
Expand All @@ -43,6 +42,7 @@ jobs:
${{ matrix.platform.msys-env }}-cmake
${{ matrix.platform.msys-env }}-gcc
${{ matrix.platform.msys-env }}-ninja
${{ (!matrix.platform.vendored && format('{0}-spirv-cross', matrix.platform.msys-env)) || '' }}
- name: Set up SDL
id: sdl
uses: libsdl-org/setup-sdl@main
Expand All @@ -58,7 +58,7 @@ jobs:
# patchelf is needed to fix the runpath of libSDL3.so.0
wget https://github.com/NixOS/patchelf/releases/download/0.18.0/patchelf-0.18.0-x86_64.tar.gz
tar xf patchelf-0.18.0-x86_64.tar.gz ./bin/patchelf
echo "$PWD/bin" >>$GITHUB_PATH
echo "${PWD}/bin" >>$GITHUB_PATH
# libxrandr-dev is needed by vulkan-loader
sudo apt-get update -y
Expand All @@ -69,31 +69,52 @@ jobs:
# bison is needed by vkd3d
brew install bison
echo "/opt/homebrew/opt/bison/bin" >>$GITHUB_PATH
- name: Download DirectXShaderCompiler binaries
if: ${{ !matrix.platform.vendored && runner.os != 'macOS' }}
run: |
cmake -P build-scripts/download-prebuilt-DirectXShaderCompiler.cmake
echo "DirectXShaderCompiler_ROOT=${PWD}/external/DirectXShaderCompiler-binaries" >>${GITHUB_ENV}
- name: Build SPIRV-Cross
if: ${{ matrix.platform.build-spirv-cross }}
run: |
cmake -S external/SPIRV-Cross -B spirv_cross_build -GNinja \
-DCMAKE_BUILD_TYPE=Release \
-DSPIRV_CROSS_SHARED=ON \
-DSPIRV_CROSS_STATIC=ON \
-DCMAKE_INSTALL_PREFIX=${PWD}/spirv_cross_prefix
cmake --build spirv_cross_build
cmake --install spirv_cross_build
spirv_cross_prefix=${PWD}/spirv_cross_prefix
if test "x${{ (matrix.platform.msvc && 'msvc') || '' }}" = "xmsvc"; then
spirv_cross_prefix=$(cygpath -w "${spirv_cross_prefix}")
fi
joiner="${{ (runner.os == 'Windows' && ';') || ':' }}"
echo "spirv_cross_c_shared_ROOT=${spirv_cross_prefix}${joiner}${spirv_cross_prefix}/share/spirv_cross_c_shared" >>${GITHUB_ENV}
- name: Configure (CMake)
run: |
cmake -S . -B build -GNinja \
-DCMAKE_BUILD_TYPE=Release \
-DSDLGPUSHADERCROSS_SHARED=ON \
-DSDLGPUSHADERCROSS_STATIC=ON \
-DSDLGPUSHADERCROSS_VENDORED=ON \
-DSDLGPUSHADERCROSS_VENDORED=${{ matrix.platform.vendored }} \
-DSDLGPUSHADERCROSS_CLI=ON \
-DSDLGPUSHADERCROSS_WERROR=ON \
-DSDLGPUSHADERCROSS_INSTALL=ON \
-DSDLGPUSHADERCROSS_INSTALL_DEPS=ON \
-DSDLGPUSHADERCROSS_INSTALL_RUNTIME=ON \
-DSDLGPUSHADERCROSS_INSTALL_CPACK=ON \
-DCMAKE_INSTALL_PREFIX="${PWD}/prefix"
- name: Build (CMake)
id: build
run: |
cmake --build build --config Release --parallel --verbose
- name: Install (CMake)
id: install
if: ${{ always() && steps.build.outcome == 'success' }}
run: |
cmake --install build/ --config Release
echo "SDL3_gpu_shadercross_ROOT=${PWD}/prefix" >>${GITHUB_ENV}
- name: Package (CPack)
id: package
if: ${{ always() && steps.build.outcome == 'success' }}
Expand All @@ -103,10 +124,8 @@ jobs:
- name: Verify CMake configuration files
if: ${{ always() && steps.install.outcome == 'success' }}
run: |
cmake -S cmake/test -B cmake_config_build \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH="${PWD}/prefix" \
-GNinja
cmake -S cmake/test -B cmake_config_build -GNinja \
-DCMAKE_BUILD_TYPE=Release
cmake --build cmake_config_build --verbose
- uses: actions/upload-artifact@v4
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build/
/external/DirectXShaderCompiler-binaries
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
[submodule "spirv-cross"]
path = external/SPIRV-Cross
url = https://github.com/KhronosGroup/SPIRV-Cross.git
[submodule "spirv-headers"]
path = external/SPIRV-Headers
url = https://github.com/KhronosGroup/SPIRV-Headers.git
[submodule "spirv-tools"]
path = external/SPIRV-Tools
url = https://github.com/KhronosGroup/SPIRV-Tools.git
[submodule "DirectXShaderCompiler"]
path = external/DirectXShaderCompiler
url = https://github.com/microsoft/DirectXShaderCompiler.git
148 changes: 80 additions & 68 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)

project(SDL3_gpu_shadercross LANGUAGES C VERSION "${MAJOR_VERSION}.${MINOR_VERSION}.${MICRO_VERSION}")

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

include(CMakeDependentOption)
include("${CMAKE_CURRENT_LIST_DIR}/cmake/PrivateSdlFunctions.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/cmake/sdlcpu.cmake")
Expand All @@ -33,19 +35,23 @@ endif()
# Options
option(SDLGPUSHADERCROSS_SHARED "Build shared SDL_gpu_shadercross library" ${SDLGPUSHADERCROSS_SHARED_DEFAULT})
option(SDLGPUSHADERCROSS_STATIC "Build static SDL_gpu_shadercross library" ${SDLGPUSHADERCROSS_STATIC_DEFAULT})
option(SDLGPUSHADERCROSS_LINK_STATIC "Link to static library variants of dependencies" OFF)
option(SDLGPUSHADERCROSS_SPIRVCROSS_SHARED "Link to shared library variants of dependencies" ON)
option(SDLGPUSHADERCROSS_VENDORED "Use vendored dependencies" OFF)
option(SDLGPUSHADERCROSS_CLI "Build command line executable" ON)
cmake_dependent_option(SDLGPUSHADERCROSS_CLI_STATIC "Link CLI with static libraries" OFF "SDLGPUSHADERCROSS_CLI;SDLGPUSHADERCROSS_STATIC;TARGET SDL3::SDL3-static" OFF)
option(SDLGPUSHADERCROSS_WERROR "Enable Werror" OFF)
option(SDLGPUSHADERCROSS_INSTALL "Enable installation" OFF)
cmake_dependent_option(SDLGPUSHADERCROSS_INSTALL_CPACK "Enable CPack installation" OFF "SDLGPUSHADERCROSS_INSTALL" OFF)
cmake_dependent_option(SDLGPUSHADERCROSS_INSTALL_DEPS "Download, build and install optional dependencies" OFF "SDLGPUSHADERCROSS_INSTALL" OFF)
cmake_dependent_option(SDLGPUSHADERCROSS_INSTALL_RUNTIME "Download, build and install runtime dependencies" OFF "SDLGPUSHADERCROSS_INSTALL" OFF)

sdl_calculate_derived_version_variables(${MAJOR_VERSION} ${MINOR_VERSION} ${MICRO_VERSION})
SDL_DetectTargetCPUArchitectures(SDL_CPU_NAMES)
SDL_DetectCMakePlatform()

if(SDLGPUSHADERCROSS_INSTALL)
include(GNUInstallDirs)
endif()

if(NOT SDLGPUSHADERCROSS_SHARED AND NOT SDLGPUSHADERCROSS_STATIC)
message(FATAL_ERROR "SDLGPUSHADERCROSS_SHARED and SDLGPUSHADERCROSS_STATIC cannot be both disabled")
endif()
Expand All @@ -60,55 +66,79 @@ if(NOT MSVC)
add_compile_options(-pedantic) # -Wno-strict-aliasing
endif()

set(spirv_static_targets spirv-cross-c spirv-cross-glsl spirv-cross-hlsl spirv-cross-msl spirv-cross-cpp spirv-cross-reflect spirv-cross-core)

set(pc_requires )
set(install_extra_targets )
if(SDLGPUSHADERCROSS_VENDORED)
set(SPIRV_CROSS_SKIP_INSTALL ON)
set(SPIRV_CROSS_CLI OFF)
set(SPIRV_CROSS_ENABLE_TESTS OFF)
if(SDLGPUSHADERCROSS_LINK_STATIC)
set(SPIRV_CROSS_SHARED OFF)
set(SPIRV_CROSS_STATIC ON)
else()
if(SDLGPUSHADERCROSS_SPIRVCROSS_SHARED)
set(SPIRV_CROSS_SHARED ON)
set(SPIRV_CROSS_STATIC OFF)
else()
set(SPIRV_CROSS_SHARED OFF)
set(SPIRV_CROSS_STATIC ON)
endif()

sdl_check_project_in_subfolder(external/SPIRV-Cross SPIRV-Cross SDLGPUSHADERCROSS_VENDORED)
set(SPIRV_CROSS_ENABLE_TESTS ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_subdirectory(external/SPIRV-Cross EXCLUDE_FROM_ALL)

if(SDLGPUSHADERCROSS_LINK_STATIC)
if(SDLGPUSHADERCROSS_SPIRVCROSS_SHARED)
list(APPEND install_extra_targets spirv-cross-c-shared)
list(APPEND pc_requires "spirv-cross-c-shared")
else()
enable_language(CXX)
if(SDLGPUSHADERCROSS_STATIC)
list(APPEND install_extra_targets spirv-cross-c)
list(APPEND pc_requires "spirv-cross-c")
set(spirv_extra_targets spirv-cross-glsl spirv-cross-hlsl spirv-cross-msl spirv-cross-cpp spirv-cross-reflect spirv-cross-core)
foreach(extra IN LISTS spirv_extra_targets)
foreach(extra IN LISTS spirv_static_targets)
if(TARGET ${extra})
list(APPEND install_extra_targets ${extra})
list(APPEND pc_requires "${extra}")
endif()
endforeach()
endif()
else()
list(APPEND install_extra_targets spirv-cross-c-shared)
list(APPEND pc_requires "spirv-cross-c-shared")
endif()

sdl_check_project_in_subfolder(external/SPIRV-Headers SPIRV-Headers SDLGPUSHADERCROSS_VENDORED)
add_subdirectory(external/SPIRV-Headers EXCLUDE_FROM_ALL)
sdl_check_project_in_subfolder(external/SPIRV-Tools SPIRV-Tools SDLGPUSHADERCROSS_VENDORED)
add_subdirectory(external/SPIRV-Tools EXCLUDE_FROM_ALL)

sdl_check_project_in_subfolder(external/DirectXShaderCompiler DirectXShaderCompiler SDLGPUSHADERCROSS_VENDORED)
if(MINGW)
set(CMAKE_C_COMPILER_ARCHITECTURE_ID "${SDL_CPU_NAMES}")
endif()
set(DXC_COVERAGE OFF)
set(HLSL_INCLUDE_TESTS OFF)
set(LLVM_INCLUDE_TESTS OFF)
include(external/DirectXShaderCompiler/cmake/caches/PredefinedParams.cmake)
add_subdirectory(external/DirectXShaderCompiler EXCLUDE_FROM_ALL)
add_library(DirectXShaderCompiler::dxcompiler ALIAS dxcompiler)
add_library(DirectXShaderCompiler::dxil ALIAS dxildll)
list(APPEND install_extra_targets dxcompiler dxildll)
add_dependencies(dxcompiler dxildll)
else()
if(SDLGPUSHADERCROSS_LINK_STATIC)
enable_language(CXX)
find_package(spirv_cross_core QUIET)
find_package(spirv_cross_glsl QUIET)
find_package(spirv_cross_hlsl QUIET)
find_package(spirv_cross_msl QUIET)
find_package(spirv_cross_cpp QUIET)
find_package(spirv_cross_reflect QUIET)
find_package(spirv_cross_c REQUIRED)
if(SDLGPUSHADERCROSS_SPIRVCROSS_SHARED)
if(NOT TARGET spirv_cross_c_shared)
find_package(spirv_cross_c_shared REQUIRED)
endif()
else()
find_package(spirv_cross_c_shared REQUIRED)
enable_language(CXX)
if(NOT TARGET spirv_cross_c)
find_package(spirv_cross_core QUIET)
find_package(spirv_cross_glsl QUIET)
find_package(spirv_cross_hlsl QUIET)
find_package(spirv_cross_msl QUIET)
find_package(spirv_cross_cpp QUIET)
find_package(spirv_cross_reflect QUIET)
find_package(spirv_cross_c REQUIRED)
endif()
endif()

find_package(DirectXShaderCompiler REQUIRED)
endif()

# Source lists
Expand Down Expand Up @@ -178,12 +208,13 @@ foreach(target IN LISTS SDL3_gpu_shadercross_targets)
sdl_add_warning_options(${target} WARNING_AS_ERROR ${SDLGPUSHADERCROSS_WERROR})
target_compile_features(${target} PRIVATE c_std_99)

if(SDLGPUSHADERCROSS_LINK_STATIC)
target_link_libraries(${target} PRIVATE spirv-cross-c)
else()
if(SDLGPUSHADERCROSS_SPIRVCROSS_SHARED)
target_link_libraries(${target} PRIVATE spirv-cross-c-shared)
else()
target_link_libraries(${target} PRIVATE spirv-cross-c)
endif()
if(SDLGPUSHADERCROSS_LINK_STATIC)
target_link_libraries(${target} PRIVATE DirectXShaderCompiler::dxcompiler)
if(NOT SDLGPUSHADERCROSS_SPIRVCROSS_SHARED)
# spirv-cross uses C++
set_property(TARGET ${target} PROPERTY LINKER_LANGUAGE CXX)
endif()
Expand Down Expand Up @@ -212,7 +243,6 @@ if(SDLGPUSHADERCROSS_CLI)
endif()

if(SDLGPUSHADERCROSS_INSTALL)
include(GNUInstallDirs)
if(WIN32 AND NOT MINGW)
set(INSTALL_CMAKEDIR_ROOT_DEFAULT "cmake")
else()
Expand Down Expand Up @@ -288,18 +318,27 @@ if(SDLGPUSHADERCROSS_INSTALL)
DESTINATION "${SDLGPUSHADERCROSS_INSTALL_CMAKEDIR}"
COMPONENT devel
)
if(NOT SDLGPUSHADERCROSS_VENDORED)
install(
FILES
"cmake/sdlcpu.cmake"
"cmake/FindDirectXShaderCompiler.cmake"
DESTINATION "${SDLGPUSHADERCROSS_INSTALL_CMAKEDIR}"
COMPONENT devel
)
endif()

file(RELATIVE_PATH SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG "${CMAKE_INSTALL_PREFIX}/${SDLGPUSHADERCROSS_PKGCONFIG_INSTALLDIR}" "${CMAKE_INSTALL_PREFIX}")
string(REGEX REPLACE "[/]+$" "" SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG "${SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG}")
set(SDL_PKGCONFIG_PREFIX "\${pcfiledir}/${SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG}")
if(NOT SDLGPUSHADERCROSS_VENDORED)
if(SDLGPUSHADERCROSS_LINK_STATIC)
set(PC_REQUIRES "spirv-cross-c")
else()
if(SDLGPUSHADERCROSS_SPIRVCROSS_SHARED)
set(PC_REQUIRES "spirv-cross-c-shared")
else()
set(PC_REQUIRES "spirv-cross-c")
endif()
endif()
set(PC_LIBS "")
set(PC_LIBS "-ldxcompiler")
configure_file(cmake/sdl3-gpu-shadercross.pc.in sdl3-gpu-shadercross.pc @ONLY)

# Always install sdl3-gpu-shadercross.pc file: libraries might be different between config modes
Expand Down Expand Up @@ -330,14 +369,21 @@ set(DXC_LINUX_X64_HASH "SHA256=b58725ac191df0686994fb9d54d27ee8dd3f180b023d56273
set(DXC_WINDOWS_X86_X64_ARM64_URL "https://github.com/microsoft/DirectXShaderCompiler/releases/download/v1.8.2407/dxc_2024_07_31.zip")
set(DXC_WINDOWS_X86_X64_ARM64_HASH "SHA256=e2627f004f0f9424d8c71ea1314d04f38c5a5096884ae9217f1f18bd320267b5")

if(SDLGPUSHADERCROSS_INSTALL_DEPS)
if(SDLGPUSHADERCROSS_INSTALL_RUNTIME)
set(chmod_0755 OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
if(APPLE)
set(rpath_origin "@executable_path")
else()
set(rpath_origin "\$ORIGIN")
endif()

if(NOT SDLGPUSHADERCROSS_VENDORED)
install(IMPORTED_RUNTIME_ARTIFACTS DirectXShaderCompiler::dxcompiler DirectXShaderCompiler::dxil RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
if(SDLGPUSHADERCROSS_SPIRVCROSS_SHARED)
install(IMPORTED_RUNTIME_ARTIFACTS spirv-cross-c-shared RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
endif()
endif()

if(TARGET shadercross)
file(RELATIVE_PATH bin_to_lib "${CMAKE_INSTALL_FULL_BINDIR}" "${CMAKE_INSTALL_FULL_LIBDIR}")
set_property(TARGET shadercross PROPERTY INSTALL_RPATH "${rpath_origin}/${bin_to_lib}")
Expand All @@ -347,41 +393,7 @@ if(SDLGPUSHADERCROSS_INSTALL_DEPS)
if(SDLGPUSHADERCROSS_CLI AND NOT SDLGPUSHADERCROSS_CLI_STATIC)
install(IMPORTED_RUNTIME_ARTIFACTS SDL3::SDL3-shared RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
endif()

# Download and install prebuilt dxil and dxcompiler shared libraries
include(ExternalProject)
if(LINUX AND SDL_CPU_X64)
ExternalProject_Add(dxc
URL "${DXC_LINUX_X64_URL}"
URL_HASH "${DXC_LINUX_X64_HASH}"
DOWNLOAD_EXTRACT_TIMESTAMP "1"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
)
ExternalProject_Get_property(dxc SOURCE_DIR)
install(FILES "${SOURCE_DIR}/lib/libdxcompiler.so" "${SOURCE_DIR}/lib/libdxil.so" DESTINATION "${CMAKE_INSTALL_LIBDIR}" PERMISSIONS ${chmod_0755})
install(FILES "${SOURCE_DIR}/LICENSE-LLVM.txt" "${SOURCE_DIR}/LICENSE-MS.txt" DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/licenses/dxc")
elseif(WINDOWS AND (SDL_CPU_ARM64 OR SDL_CPU_X86 OR SDL_CPU_X64))
ExternalProject_Add(dxc
URL "${DXC_WINDOWS_X86_X64_ARM64_URL}"
URL_HASH "${DXC_WINDOWS_X86_X64_ARM64_HASH}"
DOWNLOAD_EXTRACT_TIMESTAMP "1"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
)
if(SDL_CPU_ARM64)
set(arch_dir "arm64")
elseif(SDL_CPU_X86)
set(arch_dir "x86")
elseif(SDL_CPU_X64)
set(arch_dir "x64")
endif()
ExternalProject_Get_property(dxc SOURCE_DIR)
install(FILES "${SOURCE_DIR}/bin/${arch_dir}/dxcompiler.dll" "${SOURCE_DIR}/bin/${arch_dir}/dxil.dll" DESTINATION "${CMAKE_INSTALL_BINDIR}" PERMISSIONS ${chmod_0755})
install(FILES "${SOURCE_DIR}/LICENSE-LLVM.txt" "${SOURCE_DIR}/LICENSE-MS.txt" "${SOURCE_DIR}/LICENSE-MIT.txt" DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/licenses/dxc")
endif()

if(NOT WIN32)
ExternalProject_Add(spirv_headers
Expand Down
Loading

0 comments on commit 933e058

Please sign in to comment.