36 lines
1.7 KiB
CMake
36 lines
1.7 KiB
CMake
function(add_shader TARGET SHADER)
|
|
find_package(Vulkan REQUIRED COMPONENTS dxc)
|
|
|
|
get_filename_component(shader-ext ${SHADER} LAST_EXT)
|
|
get_filename_component(shader-inner ${SHADER} NAME_WLE)
|
|
get_filename_component(shader-type ${shader-inner} LAST_EXT)
|
|
string(REPLACE "." "" shader-type ${shader-type})
|
|
|
|
set(current-shader-path ${CMAKE_CURRENT_SOURCE_DIR}/${SHADER})
|
|
set(current-output-path ${CMAKE_CURRENT_BINARY_DIR}/${SHADER}.spv)
|
|
|
|
get_filename_component(current-output-dir ${current-output-path} DIRECTORY)
|
|
file(MAKE_DIRECTORY ${current-output-dir})
|
|
|
|
if (Vulkan_dxc_exe_FOUND AND ${shader-ext} STREQUAL ".hlsl")
|
|
message("Marked as hlsl file. ${current-output-path}")
|
|
add_custom_command(
|
|
OUTPUT ${current-output-path}
|
|
COMMAND Vulkan::dxc_exe ${DXC_SHADER_FLAGS} -spirv -T "${shader-type}_6_0" -E main ${current-shader-path} -Fo ${current-output-path}
|
|
DEPENDS ${current-shader-path}
|
|
IMPLICIT_DEPENDS CXX ${current-shader-path}
|
|
VERBATIM)
|
|
elseif (Vulkan_glslc_FOUND AND ${shader-ext} STREQUAL ".glsl")
|
|
message("Marked as glsl file. ${current-output-path}")
|
|
add_custom_command(
|
|
OUTPUT ${current-output-path}
|
|
COMMAND Vulkan::glslc ${GLSLC_SHADER_FLAGS} -o ${current-output-path} ${current-shader-path}
|
|
DEPENDS ${current-shader-path}
|
|
IMPLICIT_DEPENDS CXX ${current-shader-path}
|
|
VERBATIM)
|
|
endif ()
|
|
|
|
# Make sure our build depends on this output.
|
|
set_source_files_properties(${current-output-path} PROPERTIES GENERATED TRUE)
|
|
target_sources(${TARGET} PRIVATE ${current-output-path})
|
|
endfunction(add_shader) |