Skip to content

Commit 0d01cbb

Browse files
AnastaZIukdeprilula28
authored andcommitted
add NBL_DXC_CT_ADD_INPUT_FILE function with _NBL_INPUT_REL_FILEPATH_ _NBL_PROFILE_ _NBL_ENTRY_POINT_ _NBL_GENERATE_SPIRV_ arguments. Generate one custom command for each NBL_DXC_CT_ADD_INPUT_FILE invocation
1 parent ecb189c commit 0d01cbb

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

3rdparty/dxc/CT/CMakeLists.txt

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
############################################################################
2-
# list files you want to compile with DXC in _NBL_ENABLE_DXC_COMPILE_TESTS_
3-
# once we have guarantee all of shaders in Nabla can be compiled with DXC,
4-
# we can simply grep all of those files/use existing vars holding paths to
5-
# shaders and remove manual adding the files bellow
6-
###################################################
7-
8-
set(_NBL_DXC_CT_INCLUDE_ROOT_ "${NBL_ROOT_PATH}/include") # Nabla include root path for shaders
1+
set(_NBL_DXC_CT_INCLUDE_ROOT_ "${NBL_ROOT_PATH}/include") # Nabla include root path
92
set(_NBL_DXC_CT_BINARY_BIN_ "${CMAKE_CURRENT_BINARY_DIR}") # compile-test directory
103

114
if(_NBL_DXC_COMPILE_TESTS_ENABLE_CMAKE_LOG_)
125
message("${_NBL_DXC_CT_PREFIX_CMAKE_LOG_DEBUG_} _NBL_DXC_CT_INCLUDE_ROOT_ = '${_NBL_DXC_CT_INCLUDE_ROOT_}'")
136
message("${_NBL_DXC_CT_PREFIX_CMAKE_LOG_DEBUG_} _NBL_DXC_CT_BINARY_BIN_ = '${_NBL_DXC_CT_BINARY_BIN_}'")
147
endif()
158

16-
# this is how you add file for compilation test
17-
set(_NBL_DXC_CT_SHADER_FILES_
18-
"${_NBL_DXC_CT_INCLUDE_ROOT_}/nbl/builtin/hlsl/barycentric/utils.hlsl"
19-
)
9+
####################################
10+
# NBL_DXC_CT_ADD_INPUT_FILE function
11+
# @_NBL_INPUT_REL_FILEPATH_@ is path relative to _NBL_DXC_CT_INCLUDE_ROOT_, the file must be nested in _NBL_DXC_CT_INCLUDE_ROOT_ as well to follow Nabla include rules
12+
# @_NBL_PROFILE_@ is DXC target profile, e.g "lib_6_7"
13+
# @_NBL_ENTRY_POINT_@ is entry point name for both HLSL and SPIRV if enabled. Leave it as empty "" string to not specify the entry point explicitly
14+
# @_NBL_GENERATE_SPIRV_@ is a bool option for enabling SPIRV code generation
15+
#############################################################################
2016

21-
foreach(_SHADER_FILE_PATH_ IN LISTS _NBL_DXC_CT_SHADER_FILES_)
17+
function(NBL_DXC_CT_ADD_INPUT_FILE _NBL_INPUT_REL_FILEPATH_ _NBL_PROFILE_ _NBL_ENTRY_POINT_ _NBL_GENERATE_SPIRV_)
18+
set(_SHADER_FILE_PATH_ "${_NBL_DXC_CT_INCLUDE_ROOT_}/${_NBL_INPUT_REL_FILEPATH_}")
19+
2220
if(_NBL_DXC_COMPILE_TESTS_ENABLE_CMAKE_LOG_)
2321
message("${_NBL_DXC_CT_PREFIX_CMAKE_LOG_DEBUG_} adding _SHADER_FILE_PATH_ = '${_SHADER_FILE_PATH_}' as INPUT of DXC CT target")
2422
endif()
@@ -30,13 +28,31 @@ foreach(_SHADER_FILE_PATH_ IN LISTS _NBL_DXC_CT_SHADER_FILES_)
3028
set(_NBL_DXC_CT_CURRENT_OUTPUT_BIN_DIR_ "${_NBL_DXC_CT_BINARY_BIN_}/${_SHADER_NEW_RELATIVE_OUTPUT_DIR_}/bin")
3129
set(_NBL_DXC_CT_CURRENT_OUTPUT_FILEPATH_ "${_NBL_DXC_CT_CURRENT_OUTPUT_BIN_DIR_}/${_SHADER_NEW_STEAM_}.bin")
3230

31+
string(FIND "${_NBL_PROFILE_}" "lib" _TMP_FOUND_LIB_PROFILE_)
32+
if("${_TMP_FOUND_LIB_PROFILE_}" STREQUAL "-1")
33+
if(_NBL_GENERATE_SPIRV_)
34+
cmake_path(REPLACE_EXTENSION _NBL_DXC_CT_CURRENT_OUTPUT_FILEPATH_ "spv")
35+
endif()
36+
else()
37+
cmake_path(REPLACE_EXTENSION _NBL_DXC_CT_CURRENT_OUTPUT_FILEPATH_ "dxil")
38+
endif()
39+
unset(_TMP_FOUND_LIB_PROFILE_)
40+
3341
# MSVC and other generators have "SetEnv" task - we need to make sure our enviroment is clean while invoking DXC compilation
3442
string(APPEND _TMP_CLEAN_ENV_ "set PATH=$ENV{PATH}\nset CAExcludePath=\nset LIB=\nset LIBPATH=\nset INCLUDE=\nset EXTERNAL_INCLUDE=\n")
3543

44+
if(NOT "${_NBL_ENTRY_POINT_}" STREQUAL "")
45+
string(APPEND _TMP_ENTRY_POINT_ARG_ "-E ${_NBL_ENTRY_POINT_}")
46+
endif()
47+
48+
if(_NBL_GENERATE_SPIRV_)
49+
string(APPEND _TMP_ENABLE_SPIRV_GEN_ARG_ "-spirv")
50+
endif()
51+
3652
# include-dependency input file chain
3753
set(_NBL_DXC_CT_CURRENT_DEPENDS_FILEPATH_ "${_NBL_DXC_CT_CURRENT_OUTPUT_BIN_DIR_}/depends.txt")
3854
set(_NBL_DXC_CT_CURRENT_IDC_FILEPATH_ "${_NBL_DXC_CT_CURRENT_OUTPUT_BIN_DIR_}/depends.cmd")
39-
string(APPEND _NBL_DXC_CT_WRAPPER_IDC_COMMAND_ "${_TMP_CLEAN_ENV_}\"${DXC_EXECUTABLE}\" -HV 2021 -T lib_6_7 -I\"${_NBL_DXC_CT_INCLUDE_ROOT_}\" -Zi -MF \"${_NBL_DXC_CT_CURRENT_DEPENDS_FILEPATH_}\" \"${_SHADER_FILE_PATH_}\"")
55+
string(APPEND _NBL_DXC_CT_WRAPPER_IDC_COMMAND_ "${_TMP_CLEAN_ENV_}\"${DXC_EXECUTABLE}\" -HV 2021 -T ${_NBL_PROFILE_} ${_TMP_ENTRY_POINT_ARG_} ${_TMP_ENABLE_SPIRV_GEN_ARG_} -I\"${_NBL_DXC_CT_INCLUDE_ROOT_}\" -Zi -MF \"${_NBL_DXC_CT_CURRENT_DEPENDS_FILEPATH_}\" \"${_SHADER_FILE_PATH_}\"")
4056
file(WRITE "${_NBL_DXC_CT_CURRENT_IDC_FILEPATH_}" "${_NBL_DXC_CT_WRAPPER_IDC_COMMAND_}")
4157

4258
# create a file contating paths to all input file's dependencies scanned recursively
@@ -68,18 +84,23 @@ foreach(_SHADER_FILE_PATH_ IN LISTS _NBL_DXC_CT_SHADER_FILES_)
6884
unset(_NBL_DXC_CT_DEPENDENCIES_L_)
6985

7086
# compile command
71-
string(APPEND _NBL_DXC_CT_WRAPPER_COMPILE_COMMAND_ "${_TMP_CLEAN_ENV_}\"${DXC_EXECUTABLE}\" -HV 2021 -T lib_6_7 -I\"${_NBL_DXC_CT_INCLUDE_ROOT_}\" -Zi -Fo \"${_NBL_DXC_CT_CURRENT_OUTPUT_FILEPATH_}\" \"${_SHADER_FILE_PATH_}\"")
87+
string(APPEND _NBL_DXC_CT_WRAPPER_COMPILE_COMMAND_ "${_TMP_CLEAN_ENV_}\"${DXC_EXECUTABLE}\" -HV 2021 -T ${_NBL_PROFILE_} ${_TMP_ENTRY_POINT_ARG_} ${_TMP_ENABLE_SPIRV_GEN_ARG_} -I\"${_NBL_DXC_CT_INCLUDE_ROOT_}\" -Zi -Fo \"${_NBL_DXC_CT_CURRENT_OUTPUT_FILEPATH_}\" \"${_SHADER_FILE_PATH_}\"")
7288
set(_NBL_DXC_CT_CURRENT_SHADER_COMPILE_FILEPATH_ "${_NBL_DXC_CT_CURRENT_OUTPUT_BIN_DIR_}/compile.cmd")
7389
file(WRITE "${_NBL_DXC_CT_CURRENT_SHADER_COMPILE_FILEPATH_}" "${_NBL_DXC_CT_WRAPPER_COMPILE_COMMAND_}")
7490

7591
unset(_NBL_DXC_CT_WRAPPER_COMPILE_COMMAND_)
7692
unset(_TMP_CLEAN_ENV_)
93+
unset(_TMP_ENTRY_POINT_ARG_)
94+
unset(_TMP_ENABLE_SPIRV_GEN_ARG_)
7795

7896
if(_NBL_DXC_COMPILE_TESTS_ENABLE_CMAKE_LOG_)
7997
message("${_NBL_DXC_CT_PREFIX_CMAKE_LOG_DEBUG_} adding _NBL_DXC_CT_CURRENT_OUTPUT_FILEPATH_ = '${_NBL_DXC_CT_CURRENT_OUTPUT_FILEPATH_}' as OUTPUT of DXC CT target")
8098
endif()
8199

82-
list(APPEND _NBL_DXC_CT_BIN_OUTPUTS_ "${_NBL_DXC_CT_CURRENT_OUTPUT_FILEPATH_}")
100+
set(_NBL_DXC_CT_BIN_OUTPUTS_
101+
${_NBL_DXC_CT_BIN_OUTPUTS_}
102+
"${_NBL_DXC_CT_CURRENT_OUTPUT_FILEPATH_}"
103+
PARENT_SCOPE)
83104

84105
# create custom command per input file with it's all dependencies listed as DEPENDS
85106
add_custom_command(
@@ -91,10 +112,17 @@ foreach(_SHADER_FILE_PATH_ IN LISTS _NBL_DXC_CT_SHADER_FILES_)
91112
VERBATIM
92113
)
93114

115+
unset(_SHADER_FILE_PATH_)
94116
unset(_NBL_DXC_CT_CURRENT_OUTPUT_FILEPATH_)
95117
unset(_NBL_DXC_CT_CURRENT_SHADER_COMPILE_FILEPATH_)
96118
unset(_NBL_DXC_CT_CURRENT_DEPS_LIST_)
97-
endforeach()
119+
endfunction()
120+
121+
###############################################################################
122+
# list files you would like to compile with DXC using NBL_DXC_CT_ADD_INPUT_FILE
123+
###############################################################################
124+
125+
NBL_DXC_CT_ADD_INPUT_FILE("nbl/builtin/hlsl/barycentric/utils.hlsl" "lib_6_7" "" OFF)
98126

99127
# create custom targets for which DEPENDS are all of input files for DXC compile test
100128
add_custom_target(HLSL_NABLA_COMPILE_TEST DEPENDS ${_NBL_DXC_CT_BIN_OUTPUTS_}
@@ -105,5 +133,4 @@ add_dependencies(HLSL_NABLA_COMPILE_TEST dxc)
105133

106134
unset(_NBL_DXC_CT_BIN_OUTPUTS_)
107135
unset(_NBL_DXC_CT_BINARY_BIN_)
108-
unset(_NBL_DXC_CT_SHADER_FILES_)
109136
unset(_NBL_DXC_CT_INCLUDE_ROOT_)

0 commit comments

Comments
 (0)