simplify shader compile cmake
This commit is contained in:
+34
-27
@@ -2,54 +2,61 @@
|
|||||||
function(compile_glsl_aux shader_stage shader_name glsl_filename output)
|
function(compile_glsl_aux shader_stage shader_name glsl_filename output)
|
||||||
|
|
||||||
string(TOUPPER ${shader_stage} shader_stage_upper)
|
string(TOUPPER ${shader_stage} shader_stage_upper)
|
||||||
|
cmake_path(REPLACE_FILENAME output "${shader_name}.spv" OUTPUT_VARIABLE SPV_FILE)
|
||||||
|
|
||||||
|
file(APPEND ${output} "\
|
||||||
|
{ \"${shader_name}\", {
|
||||||
|
#include \"${shader_name}.spv\"
|
||||||
|
}},
|
||||||
|
")
|
||||||
|
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
OUTPUT ${output}
|
OUTPUT "${SPV_FILE}"
|
||||||
COMMAND echo "{ \"${shader_name}\", {" >> ${output}
|
COMMAND Vulkan::glslangValidator -V -S ${shader_stage} -D${shader_stage_upper}_SHADER ${in_file} -x -o "${SPV_FILE}"
|
||||||
COMMAND echo "#include \"${shader_name}.spv\"" >> ${output}
|
DEPENDS "${glsl_filename}"
|
||||||
COMMAND echo "}}," >> ${output}
|
VERBATIM
|
||||||
|
)
|
||||||
|
|
||||||
COMMAND Vulkan::glslangValidator -V -S ${shader_stage} -D${shader_stage_upper}_SHADER ${in_file} -x -o ${shader_name}.spv
|
get_source_file_property(deps "${output}" OBJECT_DEPENDS)
|
||||||
DEPENDS ${glsl_filename}
|
if (deps)
|
||||||
VERBATIM
|
set(deps "${deps};")
|
||||||
APPEND
|
else()
|
||||||
)
|
set(deps "")
|
||||||
|
endif()
|
||||||
|
set_source_files_properties(${output} OBJECT_DEPENDS "${deps}${SPV_FILE}")
|
||||||
|
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function(wivrn_compile_glsl target_name)
|
function(wivrn_compile_glsl target)
|
||||||
|
|
||||||
add_custom_command(
|
cmake_path(APPEND OUTPUT "${CMAKE_CURRENT_BINARY_DIR}" "${target}_shaders.cpp")
|
||||||
OUTPUT ${target_name}_shaders.cpp
|
|
||||||
COMMAND echo "#include <cstdint>" > ${target_name}_shaders.cpp
|
file(WRITE ${OUTPUT} "\
|
||||||
COMMAND echo "#include <map>" >> ${target_name}_shaders.cpp
|
#include <cstdint>
|
||||||
COMMAND echo "#include <vector>" >> ${target_name}_shaders.cpp
|
#include <map>
|
||||||
COMMAND echo "#include <string>" >> ${target_name}_shaders.cpp
|
#include <vector>
|
||||||
COMMAND echo "extern const std::map<std::string, std::vector<uint32_t>> shaders = {" >> ${target_name}_shaders.cpp
|
#include <string>
|
||||||
VERBATIM)
|
extern const std::map<std::string, std::vector<uint32_t>> shaders = {
|
||||||
|
")
|
||||||
|
|
||||||
foreach(in_file IN LISTS ARGN)
|
foreach(in_file IN LISTS ARGN)
|
||||||
if (in_file MATCHES "\.\(vert|frag|tesc|tese|geom|comp\)\.glsl$")
|
if (in_file MATCHES "\.\(vert|frag|tesc|tese|geom|comp\)\.glsl$")
|
||||||
set(shader_stage ${CMAKE_MATCH_1})
|
set(shader_stage ${CMAKE_MATCH_1})
|
||||||
cmake_path(GET in_file STEM LAST_ONLY shader_name)
|
cmake_path(GET in_file STEM LAST_ONLY shader_name)
|
||||||
compile_glsl_aux(${shader_stage} ${shader_name} ${in_file} ${target_name}_shaders.cpp)
|
compile_glsl_aux(${shader_stage} ${shader_name} ${in_file} ${OUTPUT})
|
||||||
else()
|
else()
|
||||||
cmake_path(GET in_file STEM LAST_ONLY shader_name)
|
cmake_path(GET in_file STEM LAST_ONLY shader_name)
|
||||||
compile_glsl_aux(vert ${shader_name}.vert ${in_file} ${target_name}_shaders.cpp)
|
compile_glsl_aux(vert ${shader_name}.vert ${in_file} ${OUTPUT})
|
||||||
compile_glsl_aux(frag ${shader_name}.frag ${in_file} ${target_name}_shaders.cpp)
|
compile_glsl_aux(frag ${shader_name}.frag ${in_file} ${OUTPUT})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|
||||||
endforeach()
|
endforeach()
|
||||||
|
|
||||||
add_custom_command(
|
file(APPEND ${OUTPUT} "};")
|
||||||
OUTPUT ${target_name}_shaders.cpp
|
|
||||||
COMMAND echo "};" >> ${target_name}_shaders.cpp
|
|
||||||
APPEND
|
|
||||||
VERBATIM)
|
|
||||||
|
|
||||||
target_sources(${target_name} PRIVATE ${target_name}_shaders.cpp)
|
target_sources(${target} PRIVATE ${OUTPUT})
|
||||||
|
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|||||||
Reference in New Issue
Block a user