mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-08-04 00:13:08 +00:00
host_shaders: support for includes
This commit is contained in:
parent
0bdd21b4e4
commit
e9ad724343
@ -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}")
|
||||||
|
|
||||||
# Build the shader string, wrapping each line in double quotes.
|
|
||||||
foreach(line IN LISTS line_contents)
|
|
||||||
string(CONCAT CONTENTS "${CONTENTS}" \"${line}\\n\"\n)
|
|
||||||
endforeach ()
|
endforeach ()
|
||||||
|
set(${output_content} "${parsed_content}" PARENT_SCOPE)
|
||||||
|
endfunction()
|
||||||
|
|
||||||
# Revert the original semicolons in the source.
|
parse_includes("${SOURCE_FILE}" CONTENTS)
|
||||||
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)
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user