From 8c50b1041cb4bf918ec52ccb965612d0938396a9 Mon Sep 17 00:00:00 2001 From: assiduous Date: Fri, 20 Oct 2023 13:23:43 -0700 Subject: [PATCH] CMake: reworked D3D11/D3D12 support and Windows SDK version detection --- .../CMake/BuildUtils.cmake | 0 BuildTools/CMake/CheckATL.cpp | 6 +++ BuildTools/CMake/CheckD3D11.cpp | 6 +++ BuildTools/CMake/CheckD3D12.cpp | 6 +++ CMakeLists.txt | 47 ++++++++++++------- Graphics/GraphicsEngineD3DBase/CMakeLists.txt | 4 +- 6 files changed, 50 insertions(+), 19 deletions(-) rename BuildUtils.cmake => BuildTools/CMake/BuildUtils.cmake (100%) create mode 100644 BuildTools/CMake/CheckATL.cpp create mode 100644 BuildTools/CMake/CheckD3D11.cpp create mode 100644 BuildTools/CMake/CheckD3D12.cpp diff --git a/BuildUtils.cmake b/BuildTools/CMake/BuildUtils.cmake similarity index 100% rename from BuildUtils.cmake rename to BuildTools/CMake/BuildUtils.cmake diff --git a/BuildTools/CMake/CheckATL.cpp b/BuildTools/CMake/CheckATL.cpp new file mode 100644 index 0000000000..39b6a18e2e --- /dev/null +++ b/BuildTools/CMake/CheckATL.cpp @@ -0,0 +1,6 @@ +#include + +int main() +{ + return 0; +} diff --git a/BuildTools/CMake/CheckD3D11.cpp b/BuildTools/CMake/CheckD3D11.cpp new file mode 100644 index 0000000000..0eba52bebd --- /dev/null +++ b/BuildTools/CMake/CheckD3D11.cpp @@ -0,0 +1,6 @@ +#include + +int main() +{ + return 0; +} diff --git a/BuildTools/CMake/CheckD3D12.cpp b/BuildTools/CMake/CheckD3D12.cpp new file mode 100644 index 0000000000..374a50ef55 --- /dev/null +++ b/BuildTools/CMake/CheckD3D12.cpp @@ -0,0 +1,6 @@ +#include + +int main() +{ + return 0; +} diff --git a/CMakeLists.txt b/CMakeLists.txt index 49033651a4..9ed050730f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,12 +99,24 @@ endif() message("CMake generator: " ${CMAKE_GENERATOR}) if(WIN32) + if (CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION) + set(WINDOWS_SDK_VERSION ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION} CACHE INTERNAL "Windows SDK version") + elseif(DEFINED ENV{WindowsSDKVersion}) + set(WINDOWS_SDK_VERSION $ENV{WindowsSDKVersion}) + # For unbeknown reason, the environment variable ends with a backslash, so we need to remove it + string(REPLACE "\\" "" WINDOWS_SDK_VERSION ${WINDOWS_SDK_VERSION}) + set(WINDOWS_SDK_VERSION ${WINDOWS_SDK_VERSION} CACHE INTERNAL "Windows SDK version") + else() + set(WINDOWS_SDK_VERSION "0.0" CACHE INTERNAL "Windows SDK version") + message(WARNING "Unable to determine Windows SDK version: niether CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION CMake variable nor WindowsSDKVersion environment variable is set") + endif() + if(${CMAKE_SYSTEM_NAME} STREQUAL "WindowsStore") set(PLATFORM_UNIVERSAL_WINDOWS TRUE CACHE INTERNAL "Target platform: Windows Store") - message("Target platform: Universal Windows " ${ARCH} ". SDK Version: " ${CMAKE_SYSTEM_VERSION}) + message("Target platform: Universal Windows " ${ARCH} ". Windows SDK Version: " ${WINDOWS_SDK_VERSION}) else() set(PLATFORM_WIN32 TRUE CACHE INTERNAL "Target platform: Win32") #WIN32 is a variable, so we cannot use string "WIN32" - message("Target platform: Win32 " ${ARCH} ". SDK Version: " ${CMAKE_SYSTEM_VERSION}) + message("Target platform: Win32 " ${ARCH} ". Windows SDK Version: " ${WINDOWS_SDK_VERSION}) endif() else() if(${CMAKE_SYSTEM_NAME} STREQUAL "Android") @@ -141,28 +153,29 @@ endif() add_library(Diligent-PublicBuildSettings INTERFACE) -if(PLATFORM_WIN32) - if(MSVC) - set(D3D11_SUPPORTED TRUE CACHE INTERNAL "D3D11 is supported on Win32 platform") - if(CMAKE_SYSTEM_VERSION VERSION_GREATER_EQUAL "10.0") - set(D3D12_SUPPORTED TRUE CACHE INTERNAL "D3D12 is supported on Win32 platform") - endif() +if(PLATFORM_WIN32 OR PLATFORM_UNIVERSAL_WINDOWS) + try_compile(HAS_D3D11 "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/BuildTools/CMake/CheckD3D11.cpp") + try_compile(HAS_D3D12 "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/BuildTools/CMake/CheckD3D12.cpp") + try_compile(HAS_ATL "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/BuildTools/CMake/CheckATL.cpp") + + if (HAS_D3D11 AND HAS_ATL) + set(D3D11_SUPPORTED TRUE CACHE INTERNAL "D3D11 is supported") else() - message("Building with MinGW") - set(MINGW_BUILD TRUE CACHE INTERNAL "Building with MinGW") - set(D3D11_SUPPORTED FALSE CACHE INTERNAL "D3D11 requires compiling with MSVC") - set(D3D12_SUPPORTED FALSE CACHE INTERNAL "D3D12 requires compiling with MSVC") + set(D3D11_SUPPORTED FALSE CACHE INTERNAL "D3D11 is not supported") endif() + if (HAS_D3D12 AND HAS_ATL) + set(D3D12_SUPPORTED TRUE CACHE INTERNAL "D3D12 is supported") + else() + set(D3D12_SUPPORTED FALSE CACHE INTERNAL "D3D12 is not supported") + endif() +endif() +if(PLATFORM_WIN32) set(GL_SUPPORTED TRUE CACHE INTERNAL "OpenGL is supported on Win32 platform") set(VULKAN_SUPPORTED TRUE CACHE INTERNAL "Vulkan is supported on Win32 platform") set(ARCHIVER_SUPPORTED TRUE CACHE INTERNAL "Archiver is supported on Win32 platform") target_compile_definitions(Diligent-PublicBuildSettings INTERFACE PLATFORM_WIN32=1) elseif(PLATFORM_UNIVERSAL_WINDOWS) - set(D3D11_SUPPORTED TRUE CACHE INTERNAL "D3D11 is supported on Universal Windows platform") - if(CMAKE_SYSTEM_VERSION VERSION_GREATER_EQUAL "10.0") - set(D3D12_SUPPORTED TRUE CACHE INTERNAL "D3D12 is supported on Universal Windows platform") - endif() set(ARCHIVER_SUPPORTED TRUE CACHE INTERNAL "Archiver is supported on Universal Windows platform") target_compile_definitions(Diligent-PublicBuildSettings INTERFACE PLATFORM_UNIVERSAL_WINDOWS=1) elseif(PLATFORM_ANDROID) @@ -496,7 +509,7 @@ elseif (NOT IS_ABSOLUTE ${CMAKE_INSTALL_PREFIX}) message("Transformed CMAKE_INSTALL_PREFIX into absolute path: " ${CMAKE_INSTALL_PREFIX}) endif() -include(BuildUtils.cmake) +include(BuildTools/CMake/BuildUtils.cmake) if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") # Find available .NET SDKs diff --git a/Graphics/GraphicsEngineD3DBase/CMakeLists.txt b/Graphics/GraphicsEngineD3DBase/CMakeLists.txt index 8641e99ef5..61befc0485 100644 --- a/Graphics/GraphicsEngineD3DBase/CMakeLists.txt +++ b/Graphics/GraphicsEngineD3DBase/CMakeLists.txt @@ -80,7 +80,7 @@ if(MSVC) # Note that CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION is stated to be defined when targeting Windows 10 # and above, however it is also defined when targeting 8.1 and Visual Studio 2019 (but not VS2017) - if(CMAKE_SYSTEM_VERSION VERSION_GREATER_EQUAL "10.0") + if("${WINDOWS_SDK_VERSION}" VERSION_GREATER_EQUAL "10.0") if (DEFINED CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION) # Note that VS_WINDOWS_SDK_BIN_DIR as well as all derived paths can only be used in Visual Studio # commands and are not valid paths during CMake configuration @@ -94,7 +94,7 @@ if(MSVC) set(VS_DXIL_SIGNER_PATH "\"${VS_WINDOWS_SDK_BIN_DIR}\\dxil.dll\"" CACHE INTERNAL "dxil.dll path") endif() endif() - elseif(CMAKE_SYSTEM_VERSION VERSION_EQUAL "8.1") + elseif("${WINDOWS_SDK_VERSION}" VERSION_EQUAL "8.1") # D3Dcompiler_47.dll from Win8.1 SDK is ancient (from 2013) and fails to # compile a number of test shaders. Use the compiler from Visual Studio # executable path instead