84 lines
3.3 KiB
CMake
84 lines
3.3 KiB
CMake
function(compile_slang_shaders input_file stage entry_point)
|
|
# 保证输出目录存在
|
|
if (NOT DEFINED SHADER_OUTPUT_DIR)
|
|
message(FATAL_ERROR "SHADER_OUTPUT_DIR not defined")
|
|
endif ()
|
|
set(output_dir ${SHADER_OUTPUT_DIR})
|
|
|
|
# 获取输出文件的基本名称
|
|
get_filename_component(filename ${input_file} NAME_WE)
|
|
|
|
# 定义根据阶段确定的配置
|
|
if(stage STREQUAL "vertex")
|
|
set(profile "vs_5_0")
|
|
elseif(stage STREQUAL "pixel")
|
|
set(profile "ps_5_0")
|
|
elseif (stage STREQUAL "comp")
|
|
set(profile "cs_5_0")
|
|
else()
|
|
message(FATAL_ERROR "Unsupported shader stage: ${stage}")
|
|
endif()
|
|
|
|
# 初始化一个列表来存储输出文件
|
|
set(output_files)
|
|
|
|
# 为每个后台创建自定义命令
|
|
if(GL_BACKEND)
|
|
set(output_file ${output_dir}/${filename}_${entry_point}.glsl)
|
|
add_custom_command(
|
|
OUTPUT ${output_file}
|
|
COMMAND slangc -target glsl -entry ${entry_point} -stage ${stage} ${ARGN} -o ${output_file} ${input_file}
|
|
DEPENDS ${input_file}
|
|
COMMENT "Compiling ${input_file} to GLSL (${stage}, ${entry_point})"
|
|
)
|
|
list(APPEND output_files ${output_file})
|
|
endif()
|
|
|
|
if(DX_BACKEND)
|
|
set(output_file ${output_dir}/${filename}_${entry_point}.dxbc)
|
|
add_custom_command(
|
|
OUTPUT ${output_file}
|
|
COMMAND slangc -target dxbc -profile ${profile} -entry ${entry_point} -stage ${stage} ${ARGN} -o ${output_file} ${input_file}
|
|
DEPENDS ${input_file}
|
|
COMMENT "Compiling ${input_file} to DXBC (${stage}, ${entry_point}) with profile ${profile}"
|
|
)
|
|
list(APPEND output_files ${output_file})
|
|
endif()
|
|
|
|
if(VK_BACKEND)
|
|
set(output_file ${output_dir}/${filename}_${entry_point}.spirv)
|
|
add_custom_command(
|
|
OUTPUT ${output_file}
|
|
COMMAND slangc -target spirv -entry ${entry_point} -stage ${stage} ${ARGN} -o ${output_file} ${input_file}
|
|
DEPENDS ${input_file}
|
|
COMMENT "Compiling ${input_file} to SPIR-V (${stage}, ${entry_point})"
|
|
)
|
|
list(APPEND output_files ${output_file})
|
|
endif()
|
|
|
|
if(METAL_BACKEND)
|
|
set(output_file ${output_dir}/${filename}_${entry_point}.metal)
|
|
add_custom_command(
|
|
OUTPUT ${output_file}
|
|
COMMAND slangc -target msl -entry ${entry_point} -stage ${stage} ${ARGN} -o ${output_file} ${input_file}
|
|
DEPENDS ${input_file}
|
|
COMMENT "Compiling ${input_file} to Metal Shading Language (${stage}, ${entry_point})"
|
|
)
|
|
list(APPEND output_files ${output_file})
|
|
endif()
|
|
|
|
# 将输出文件添加到全局列表中
|
|
set_property(GLOBAL APPEND PROPERTY ALL_SHADER_OUTPUTS ${output_files})
|
|
endfunction()
|
|
|
|
function(add_compile_shaders_target target_name before_target)
|
|
file(MAKE_DIRECTORY ${SHADER_OUTPUT_DIR})
|
|
|
|
get_property(ALL_SHADER_OUTPUTS GLOBAL PROPERTY ALL_SHADER_OUTPUTS)
|
|
add_custom_target(${target_name} ALL DEPENDS ${ALL_SHADER_OUTPUTS})
|
|
# 将编译着色器的目标添加到指定目标之前
|
|
add_dependencies(${before_target} ${target_name})
|
|
# 将ALL_SHADER_OUTPUTS清空
|
|
set_property(GLOBAL PROPERTY ALL_SHADER_OUTPUTS "")
|
|
endfunction()
|