-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
174f25e
commit 1b660a2
Showing
24 changed files
with
210 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,134 @@ | ||
if (${Vulkan_glslc_FOUND}) | ||
message(STATUS "Using Vulkan glslc for shader compilation.") | ||
elseif (${Vulkan_glslangValidator_FOUND}) | ||
message(WARNING "Vulkan glslc not found, using glslangValidator for shader compilation instead. Modifying indirectly included files will NOT trigger recompilation.") | ||
else() | ||
message(FATAL_ERROR "No shader compiler found.") | ||
endif () | ||
|
||
function(target_link_shaders TARGET) | ||
if (${Vulkan_glslc_FOUND}) | ||
message(STATUS "Using Vulkan glslc for shader compilation.") | ||
elseif (${Vulkan_glslangValidator_FOUND}) | ||
message(WARNING "Vulkan glslc not found, using glslangValidator for shader compilation instead. Modifying indirectly included files will NOT trigger recompilation.") | ||
else() | ||
message(FATAL_ERROR "No shader compiler found.") | ||
endif () | ||
# Make target identifier. | ||
string(MAKE_C_IDENTIFIER ${TARGET} target_identifier) | ||
|
||
set(outputs "") | ||
foreach (source IN LISTS ARGN) | ||
# Get filename from source. | ||
cmake_path(GET source FILENAME filename) | ||
|
||
# Make source path absolute. | ||
cmake_path(ABSOLUTE_PATH source OUTPUT_VARIABLE source) | ||
|
||
set(output "shader/${filename}.spv") | ||
# Make shader identifier. | ||
string(MAKE_C_IDENTIFIER ${filename} shader_identifier) | ||
|
||
if (${Vulkan_glslc_FOUND}) | ||
set(depfile "shader_depfile/${filename}.d") | ||
add_custom_command( | ||
OUTPUT "${output}" | ||
COMMAND ${Vulkan_GLSLC_EXECUTABLE} -MD -MF "${depfile}" $<$<CONFIG:Release>:-O> --target-env=vulkan1.2 "${source}" -o "${output}" | ||
DEPENDS "${source}" | ||
BYPRODUCTS "${depfile}" | ||
COMMENT "Compiling SPIRV: ${source} -> ${output}" | ||
DEPFILE "${depfile}" | ||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/shader/${filename}.cppm | ||
COMMAND ${Vulkan_GLSLC_EXECUTABLE} -MD -MF shader_depfile/${filename}.d $<$<CONFIG:Release>:-O> --target-env=vulkan1.2 -mfmt=num ${source} -o "shader/${filename}.h" | ||
&& ${CMAKE_COMMAND} -E echo "export module ${target_identifier}:shader.${shader_identifier}\;" > shader/${filename}.cppm | ||
&& ${CMAKE_COMMAND} -E echo "namespace ${target_identifier}::shader { export constexpr unsigned int ${shader_identifier}[] = {" >> shader/${filename}.cppm | ||
&& ${CMAKE_COMMAND} -E cat shader/${filename}.h >> shader/${filename}.cppm | ||
&& ${CMAKE_COMMAND} -E rm shader/${filename}.h | ||
&& ${CMAKE_COMMAND} -E echo "}\;}" >> shader/${filename}.cppm | ||
DEPENDS ${source} | ||
BYPRODUCTS shader_depfile/${filename}.d | ||
COMMENT "Compiling SPIR-V: ${source} -> ${filename}.cppm" | ||
DEPFILE shader_depfile/${filename}.d | ||
VERBATIM | ||
COMMAND_EXPAND_LISTS | ||
) | ||
elseif (${Vulkan_glslangValidator_FOUND}) | ||
add_custom_command( | ||
OUTPUT "${output}" | ||
COMMAND ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE} -V $<$<CONFIG:Debug>:-Od> --target-env vulkan1.2 "${source}" -o "${output}" | ||
DEPENDS "${source}" | ||
COMMENT "Compiling SPIRV: ${source} -> ${output}" | ||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/shader/${filename}.cppm | ||
COMMAND ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE} -V $<$<CONFIG:Debug>:-Od> --target-env vulkan1.2 -x ${source} -o "shader/${filename}.h" | ||
&& ${CMAKE_COMMAND} -E echo "export module ${target_identifier}:shader.${shader_identifier}\;" > shader/${filename}.cppm | ||
&& ${CMAKE_COMMAND} -E echo "namespace ${target_identifier}::shader { export constexpr unsigned int ${shader_identifier}[] = {" >> shader/${filename}.cppm | ||
&& ${CMAKE_COMMAND} -E cat shader/${filename}.h >> shader/${filename}.cppm | ||
&& ${CMAKE_COMMAND} -E rm shader/${filename}.h | ||
&& ${CMAKE_COMMAND} -E echo "}\;}" >> shader/${filename}.cppm | ||
DEPENDS ${source} | ||
COMMENT "Compiling SPIR-V: ${source}" | ||
VERBATIM | ||
COMMAND_EXPAND_LISTS | ||
) | ||
endif () | ||
|
||
# Make target depends on output shader file. | ||
string(CONFIGURE "${TARGET}_${filename}" shader_target) | ||
add_custom_target("${shader_target}" DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${output}") | ||
add_dependencies("${TARGET}" "${shader_target}") | ||
list(APPEND outputs ${CMAKE_CURRENT_BINARY_DIR}/shader/${filename}.cppm) | ||
endforeach () | ||
|
||
target_compile_definitions(${TARGET} PRIVATE COMPILED_SHADER_DIR="shader") | ||
target_sources(${TARGET} PRIVATE FILE_SET CXX_MODULES FILES ${outputs}) | ||
endfunction() | ||
|
||
function(target_link_shaders_variant TARGET MACRO_NAME MACRO_VALUES) | ||
if (${Vulkan_glslc_FOUND}) | ||
message(STATUS "Using Vulkan glslc for shader compilation.") | ||
elseif (${Vulkan_glslangValidator_FOUND}) | ||
message(WARNING "Vulkan glslc not found, using glslangValidator for shader compilation instead. Modifying indirectly included files will NOT trigger recompilation.") | ||
else() | ||
message(FATAL_ERROR "No shader compiler found.") | ||
endif () | ||
# Make target identifier. | ||
string(MAKE_C_IDENTIFIER ${TARGET} target_identifier) | ||
|
||
set(outputs "") | ||
foreach (source IN LISTS ARGN) | ||
# Get filename from source. | ||
cmake_path(GET source FILENAME filename) | ||
|
||
# Make shader identifier. | ||
string(MAKE_C_IDENTIFIER ${filename} shader_identifier) | ||
|
||
# Make source path absolute. | ||
cmake_path(ABSOLUTE_PATH source OUTPUT_VARIABLE source) | ||
|
||
add_custom_command( | ||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/shader/${filename}.cppm | ||
COMMAND ${CMAKE_COMMAND} -E echo "export module ${target_identifier}:shader.${shader_identifier}\;" > shader/${filename}.cppm | ||
COMMAND ${CMAKE_COMMAND} -E echo "namespace ${target_identifier}::shader {" >> shader/${filename}.cppm | ||
COMMENT "Compiling SPIR-V: ${source} (${MACRO_NAME}=${MACRO_VALUES})" | ||
VERBATIM | ||
COMMAND_EXPAND_LISTS | ||
) | ||
|
||
foreach (macro_value IN LISTS MACRO_VALUES) | ||
set(output "shader/${filename}_${MACRO_NAME}_${macro_value}.spv") | ||
# Make variant identifier. | ||
string(MAKE_C_IDENTIFIER "${MACRO_NAME}_${macro_value}" variant_identifier) | ||
|
||
if (${Vulkan_glslc_FOUND}) | ||
set(depfile "shader_depfile/${filename}_${MACRO_NAME}_${macro_value}.d") | ||
add_custom_command( | ||
OUTPUT "${output}" | ||
COMMAND ${Vulkan_GLSLC_EXECUTABLE} -MD -MF "${depfile}" $<$<CONFIG:Release>:-O> --target-env=vulkan1.2 -D${MACRO_NAME}=${macro_value} "${source}" -o "${output}" | ||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/shader/${filename}.cppm | ||
COMMAND ${Vulkan_GLSLC_EXECUTABLE} -MD -MF shader_depfile/${filename}_${variant_identifier}.d $<$<CONFIG:Release>:-O> --target-env=vulkan1.2 -mfmt=num -D${MACRO_NAME}=${macro_value} "${source}" -o shader/${filename}_${variant_identifier}_body.h | ||
&& ${CMAKE_COMMAND} -E echo "export constexpr unsigned int ${shader_identifier}_${variant_identifier}[] = {" >> shader/${filename}.cppm | ||
&& ${CMAKE_COMMAND} -E cat shader/${filename}_${variant_identifier}_body.h >> shader/${filename}.cppm | ||
&& ${CMAKE_COMMAND} -E rm shader/${filename}_${variant_identifier}_body.h | ||
&& ${CMAKE_COMMAND} -E echo "}\;" >> shader/${filename}.cppm | ||
DEPENDS "${source}" | ||
BYPRODUCTS "${depfile}" | ||
COMMENT "Compiling SPIRV: ${source} -> ${output}" | ||
DEPFILE "${depfile}" | ||
BYPRODUCTS shader_depfile/${filename}_${variant_identifier}.d | ||
DEPFILE shader_depfile/${filename}_${variant_identifier}.d | ||
VERBATIM | ||
COMMAND_EXPAND_LISTS | ||
APPEND | ||
) | ||
elseif (${Vulkan_glslangValidator_FOUND}) | ||
add_custom_command( | ||
OUTPUT "${output}" | ||
COMMAND ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE} -V $<$<CONFIG:Debug>:-Od> --target-env vulkan1.2 "${source}" -D${MACRO_NAME}=${macro_value} -o "${output}" | ||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/shader/${filename}.cppm | ||
COMMAND ${Vulkan_GLSLANG_VALIDATOR_EXECUTABLE} -V $<$<CONFIG:Debug>:-Od> --target-env vulkan1.2 -x ${source} -D${MACRO_NAME}=${macro_value} -o shader/${filename}_${variant_identifier}_body.h | ||
&& ${CMAKE_COMMAND} -E echo "export constexpr unsigned int ${shader_identifier}_${variant_identifier}[] = {" >> shader/${filename}.cppm | ||
&& ${CMAKE_COMMAND} -E cat shader/${filename}_${variant_identifier}_body.h >> shader/${filename}.cppm | ||
&& ${CMAKE_COMMAND} -E rm shader/${filename}_${variant_identifier}_body.h | ||
&& ${CMAKE_COMMAND} -E echo "}\;" >> shader/${filename}.cppm | ||
DEPENDS "${source}" | ||
COMMENT "Compiling SPIRV: ${source} -> ${output}" | ||
VERBATIM | ||
COMMAND_EXPAND_LISTS | ||
APPEND | ||
) | ||
endif () | ||
|
||
# Make target depends on output shader file. | ||
string(CONFIGURE "${TARGET}_${filename}_${MACRO_NAME}_${macro_value}" shader_target) | ||
add_custom_target("${shader_target}" DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${output}") | ||
add_dependencies("${TARGET}" "${shader_target}") | ||
endforeach () | ||
|
||
add_custom_command( | ||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/shader/${filename}.cppm | ||
COMMAND ${CMAKE_COMMAND} -E echo "}" >> shader/${filename}.cppm | ||
APPEND | ||
VERBATIM | ||
COMMAND_EXPAND_LISTS | ||
) | ||
|
||
list(APPEND outputs ${CMAKE_CURRENT_BINARY_DIR}/shader/${filename}.cppm) | ||
endforeach () | ||
|
||
target_compile_definitions(${TARGET} PRIVATE COMPILED_SHADER_DIR="shader") | ||
target_sources(${TARGET} PRIVATE FILE_SET CXX_MODULES FILES ${outputs}) | ||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.