host_shaders: support for includes

This commit is contained in:
Vinicius Rangel 2025-03-03 10:28:42 -03:00
parent 0bdd21b4e4
commit e9ad724343
No known key found for this signature in database
GPG Key ID: A5B154D904B761D9
3 changed files with 24 additions and 21 deletions

View File

@ -9,28 +9,31 @@ get_filename_component(CONTENTS_NAME ${SOURCE_FILE} NAME)
string(REPLACE "." "_" CONTENTS_NAME ${CONTENTS_NAME}) string(REPLACE "." "_" CONTENTS_NAME ${CONTENTS_NAME})
string(TOUPPER ${CONTENTS_NAME} CONTENTS_NAME) string(TOUPPER ${CONTENTS_NAME} CONTENTS_NAME)
FILE(READ ${SOURCE_FILE} line_contents) # Function to recursively parse #include directives and replace them with file contents
function(parse_includes file_path output_content)
file(READ ${file_path} file_content)
# This regex includes \n at the begin to (hackish) avoid including comments
string(REGEX MATCHALL "\n#include +\"[^\"]+\"" includes "${file_content}")
# Replace double quotes with single quotes, set(parsed_content "${file_content}")
# as double quotes will be used to wrap the lines foreach (include_match ${includes})
STRING(REGEX REPLACE "\"" "'" line_contents "${line_contents}") string(REGEX MATCH "\"([^\"]+)\"" _ "${include_match}")
set(include_file ${CMAKE_MATCH_1})
get_filename_component(include_full_path "${file_path}" DIRECTORY)
set(include_full_path "${include_full_path}/${include_file}")
# CMake separates list elements with semicolons, but semicolons if (NOT EXISTS "${include_full_path}")
# are used extensively in the shader code. message(FATAL_ERROR "Included file not found: ${include_full_path} from ${file_path}")
# Replace with a temporary marker, to be reverted later. endif ()
STRING(REGEX REPLACE ";" "{{SEMICOLON}}" line_contents "${line_contents}")
# Make every line an individual element in the CMake list. parse_includes("${include_full_path}" sub_content)
STRING(REGEX REPLACE "\n" ";" line_contents "${line_contents}") string(REPLACE "${include_match}" "\n${sub_content}" parsed_content "${parsed_content}")
endforeach ()
set(${output_content} "${parsed_content}" PARENT_SCOPE)
endfunction()
# Build the shader string, wrapping each line in double quotes. parse_includes("${SOURCE_FILE}" CONTENTS)
foreach(line IN LISTS line_contents)
string(CONCAT CONTENTS "${CONTENTS}" \"${line}\\n\"\n)
endforeach()
# Revert the original semicolons in the source.
STRING(REGEX REPLACE "{{SEMICOLON}}" ";" CONTENTS "${CONTENTS}")
get_filename_component(OUTPUT_DIR ${HEADER_FILE} DIRECTORY) get_filename_component(OUTPUT_DIR ${HEADER_FILE} DIRECTORY)
make_directory(${OUTPUT_DIR}) file(MAKE_DIRECTORY ${OUTPUT_DIR})
configure_file(${INPUT_FILE} ${HEADER_FILE} @ONLY) configure_file(${INPUT_FILE} ${HEADER_FILE} @ONLY)

View File

@ -8,7 +8,7 @@ layout (location = 0) out vec4 color;
layout (binding = 0) uniform sampler2D texSampler; layout (binding = 0) uniform sampler2D texSampler;
layout(push_constant) uniform settings { layout (push_constant) uniform settings {
float gamma; float gamma;
bool hdr; bool hdr;
} pp; } pp;

View File

@ -7,8 +7,8 @@
namespace HostShaders { namespace HostShaders {
constexpr std::string_view @CONTENTS_NAME@ = { constexpr std::string_view @CONTENTS_NAME@ = R"shader_src(
@CONTENTS@ @CONTENTS@
}; )shader_src";
} // namespace HostShaders } // namespace HostShaders