aorii/cmake/compile_shaders.cmake
2025-02-04 19:25:09 +08:00

89 lines
3.7 KiB
CMake
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
set(extra_args ${ARGN})
# 如果是debug模式添加调试标志
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
list(APPEND extra_args "-g")
endif()
# 为每个后台创建自定义命令
if(LLGL_BUILD_RENDERER_OPENGL OR LLGL_BUILD_RENDERER_OPENGL_ES3 OR LLGL_BUILD_RENDERER_WEBGL)
set(output_file ${output_dir}/${filename}_${entry_point}.glsl)
add_custom_command(
OUTPUT ${output_file}
COMMAND slangc -target glsl -entry ${entry_point} -stage ${stage} ${extra_args} -o ${output_file} ${input_file} -DUSE_OPENGL=1 -DUV_FLIP=1
DEPENDS ${input_file}
COMMENT "Compiling ${input_file} to GLSL (${stage}, ${entry_point})"
)
list(APPEND output_files ${output_file})
endif()
if(LLGL_BUILD_RENDERER_DIRECT3D11 OR LLGL_BUILD_RENDERER_DIRECT3D12)
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} ${extra_args} -o ${output_file} ${input_file} -DUSE_DX=1
DEPENDS ${input_file}
COMMENT "Compiling ${input_file} to DXBC (${stage}, ${entry_point}) with profile ${profile}"
)
list(APPEND output_files ${output_file})
endif()
if(LLGL_BUILD_RENDERER_VULKAN)
set(output_file ${output_dir}/${filename}_${entry_point}.spirv)
add_custom_command(
OUTPUT ${output_file}
COMMAND slangc -target spirv -entry ${entry_point} -stage ${stage} ${extra_args} -o ${output_file} ${input_file} -DUSE_VULKAN=1 -DUV_FLIP=1
DEPENDS ${input_file}
COMMENT "Compiling ${input_file} to SPIR-V (${stage}, ${entry_point})"
)
list(APPEND output_files ${output_file})
endif()
if(LLGL_BUILD_RENDERER_METAL)
set(output_file ${output_dir}/${filename}_${entry_point}.metal)
add_custom_command(
OUTPUT ${output_file}
COMMAND slangc -target metal -entry ${entry_point} -stage ${stage} ${extra_args} -o ${output_file} ${input_file} -DUSE_METAL=1
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()