Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding Direct3D 12 using CMake

Tags:

c++

cmake

So I've been trying to learn CMake and use it with C++. I'd like to have a go at creating a portable game engine which uses Direct3D 12 on Windows.

Currently, I have the following CMakeLists.txt for my project:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

project(PsychoEngineCore)

set(SRCS_CXX ${CMAKE_CURRENT_LIST_DIR})

include(${CMAKE_CURRENT_SOURCE_DIR}/src/dir_src.cmake)

set(LIB_TYPE "STATIC" CACHE STRING "Static or Dynamic Linking")

if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU OR
${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
    set(warnings "-Wall -Wextra -Werror")
elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
    set(warnings "/W4 /WX /EHsc")
endif()

if(NOT CONFIGURED_ONCE)
    set(CMAKE_CXX_FLAGS "${warnings}"
        CACHE STRING "Flags used by the compiler during all build types." FORCE)
endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

# Do we want static libraries?
# When STATIC_LINKING is TRUE, then cmake looks for binaries ending in ".a".
# THIS IS FOR LINUX ONLY!
if(LIB_TYPE EQUAL STATIC)
    if (UNIX AND NOT APPLE)
        set(CMAKE_FIND_LIBRARY_SUFFIXES(".a"))
    endif(UNIX AND NOT APPLE)

    set(CMAKE_EXE_LINKER_FLAGS "-static")
    set_target_properties(surface PROPERTIES LINK_SEARCH_END_STATIC 1)
endif(LIB_TYPE EQUAL STATIC)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include(GenerateExportHeader)
add_library(${PROJECT_NAME} ${LIB_TYPE} ${SRCS_CXX})
GENERATE_EXPORT_HEADER(
    ${PROJECT_NAME}
    EXPORT_MACRO_NAME PE_API
    EXPORT_FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR}/include/Engine/API/${PROJECT_NAME}Export.hpp
    DEPRECATED_MACRO_NAME PE_API_DEP
    STATIC_DEFINE PE_STATIC
)

Currently I have my d3d12.lib file in the following location:

C:\Program Files (x86)\Windows Kits\10\Libs\10.0.16299.0\um\x64

Is there a way to keep the path dynamic and preferably "update" if a newer version is available on said system?

Thanks!

like image 609
JamieRhys Avatar asked Oct 17 '25 17:10

JamieRhys


1 Answers

Turning my comment into an answer

Generally speaking, just add a

target_link_libraries((${PROJECT_NAME} d3d12.lib) 

The Windows SDK (where the Direct3D SDK is now part of) is in the standard search paths e.g. of the linker for libraries. So it's found automatically by the MSVC compiler and linker.

And I don't think that upgrading to a newer (yet unknown) API version automatically is a good idea. You're writing your program for a specific API version.

Working Example

Here is a minimal working example (Tested VS2017 15.5.5, CMake 3.9.0):

cmake_minimum_required(VERSION 3.0)

project(Direct3DExample)

find_package(Git REQUIRED)

set(_path "${CMAKE_BINARY_DIR}/DirectX-Graphics-Samples/Samples/Desktop/D3D12HelloWorld/src/HelloTriangle")
if (NOT EXISTS "${_path}")
    execute_process(
        COMMAND "${GIT_EXECUTABLE}" clone https://github.com/Microsoft/DirectX-Graphics-Samples.git
    )
endif()

file(GLOB _files "${_path}/*")
list(APPEND _shader ${_files})
list(FILTER _files EXCLUDE REGEX "\\.vcxproj|\\.hlsl")
list(FILTER _shader INCLUDE REGEX "\\.hlsl")

get_filename_component(_name "${_path}" NAME)

add_executable(${_name} WIN32 ${_files})
target_compile_definitions(${_name} PRIVATE "UNICODE" "_UNICODE")
target_link_libraries(${_name} PRIVATE "d3d12.lib" "dxgi.lib" "d3dcompiler.lib")

add_custom_command(
    TARGET ${_name}
    POST_BUILD
    COMMAND "${CMAKE_COMMAND}" -E copy ${_shader} $<TARGET_FILE_DIR:${_name}>
)

If you need more (compilers/linkers that are not finding the Windows SDK automatically), you may want to look the following find_package() config code:

  • https://github.com/Microsoft/DirectXShaderCompiler/blob/master/cmake/modules/FindD3D12.cmake
like image 72
Florian Avatar answered Oct 19 '25 09:10

Florian