如何在使用 Cmake 构建期间编译 HLSL 着色器?
How to compile HLSL Shaders during build with Cmake?
我正在开发一个 d3d 应用程序,我想在使用 cmake 构建期间编译我的 .hlsl 着色器。
我不知道从哪里开始。
这是我现在的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})
我将此模式用于可与 Ninja 和 MSVC 生成器一起使用的 CMake 着色器。
# 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.
我正在开发一个 d3d 应用程序,我想在使用 cmake 构建期间编译我的 .hlsl 着色器。 我不知道从哪里开始。
这是我现在的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})
我将此模式用于可与 Ninja 和 MSVC 生成器一起使用的 CMake 着色器。
# 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.