Search code examples
cmakeshaderdirectxdirectx-11hlsl

How to compile HLSL Shaders during build with Cmake?


I'm working on a d3d application and i would like to compile my .hlsl shaders during to build using cmake. I have no idea where to start.

this is my current CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(Direct3DTut)

set(CMAKE_CXX_STANDARD 17)

add_executable(${PROJECT_NAME} WIN32
                WinMain.cpp Window.cpp MessageHandler.cpp Graphics.cpp
                Keyboard.cpp Mouse.cpp
                Utils.cpp)

set(LIBS d3d11 D3DCompiler)
target_link_libraries(${PROJECT_NAME} ${LIBS})

Solution

  • I use this pattern for CMake shaders that works with both Ninja and the MSVC generators.

    # Build HLSL shaders
    add_custom_target(shaders)
    
    set(HLSL_SHADER_FILES VertexShader.hlsl PixelShader.hlsl)
    
    set_source_files_properties(VertexShader.hlsl PROPERTIES ShaderType "vs")
    set_source_files_properties(PixelShader.hlsl PROPERTIES ShaderType "ps")
    set_source_files_properties(${HLSL_SHADER_FILES} PROPERTIES ShaderModel "4_0")
    
    foreach(FILE ${HLSL_SHADER_FILES})
      get_filename_component(FILE_WE ${FILE} NAME_WE)
      get_source_file_property(shadertype ${FILE} ShaderType)
      get_source_file_property(shadermodel ${FILE} ShaderModel)
      add_custom_command(TARGET shaders
                         COMMAND fxc.exe /nologo /Emain /T${shadertype}_${shadermodel} $<IF:$<CONFIG:DEBUG>,/Od,/O1> /Zi /Fo ${CMAKE_BINARY_DIR}/${FILE_WE}.cso /Fd ${CMAKE_BINARY_DIR}/${FILE_WE}.pdb ${FILE}
                         MAIN_DEPENDENCY ${FILE}
                         COMMENT "HLSL ${FILE}"
                         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                         VERBATIM)
    endforeach(FILE)
    

    ...

    add_dependencies(${PROJECT_NAME} shaders)
    

    I use this in my DirectX Tool Kit tutorials. You can also see the rest of the CMake on directx-vs-templates.