forked from NVIDIAGameWorks/donut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compileshaders.cmake
256 lines (222 loc) · 11.4 KB
/
compileshaders.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#
# Copyright (c) 2014-2020, NVIDIA CORPORATION. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
set (NVRHI_DEFAULT_VK_REGISTER_OFFSETS
--tRegShift 0
--sRegShift 128
--bRegShift 256
--uRegShift 384)
# Generates a build target that will compile shaders for a given config file
# Usage:
#
# donut_compile_shaders(TARGET <generated build target name>
# CONFIG <shader-config-file>
# SOURCES <list>
# [FOLDER <folder-in-visual-studio-solution>]
# [OUTPUT_FORMAT (HEADER|BINARY)]
# [DXIL <dxil-output-path>]
# [DXBC <dxbc-output-path>]
# [SPIRV_DXC <spirv-output-path>]
# [COMPILER_OPTIONS_DXBC <string>] -- arguments passed to ShaderMake
# [COMPILER_OPTIONS_DXIL <string>]
# [COMPILER_OPTIONS_SPIRV <string>]
# [BYPRODUCTS_DXBC <list>] -- list of generated files without paths,
# [BYPRODUCTS_DXIL <list>] needed to get correct incremental builds when
# [BYPRODUCTS_SPIRV <list>]) using static shaders with Ninja generator
function(donut_compile_shaders)
set(options "")
set(oneValueArgs TARGET CONFIG FOLDER OUTPUT_FORMAT DXIL DXBC SPIRV_DXC
COMPILER_OPTIONS_DXBC COMPILER_OPTIONS_DXIL COMPILER_OPTIONS_SPIRV)
set(multiValueArgs SOURCES BYPRODUCTS_DXBC BYPRODUCTS_DXIL BYPRODUCTS_SPIRV)
cmake_parse_arguments(params "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if (NOT params_TARGET)
message(FATAL_ERROR "donut_compile_shaders: TARGET argument missing")
endif()
if (NOT params_CONFIG)
message(FATAL_ERROR "donut_compile_shaders: CONFIG argument missing")
endif()
# just add the source files to the project as documents, they are built by the script
set_source_files_properties(${params_SOURCES} PROPERTIES VS_TOOL_OVERRIDE "None")
add_custom_target(${params_TARGET}
DEPENDS ShaderMake
SOURCES ${params_SOURCES})
if (WIN32)
set(use_api_arg --useAPI)
else()
set(use_api_arg "")
endif()
if ("${params_OUTPUT_FORMAT}" STREQUAL "HEADER")
set(output_format_arg --headerBlob)
elseif(("${params_OUTPUT_FORMAT}" STREQUAL "BINARY") OR ("${params_OUTPUT_FORMAT}" STREQUAL ""))
set(output_format_arg --binaryBlob --outputExt .bin)
else()
message(FATAL_ERROR "donut_compile_shaders: unsupported OUTPUT_FORMAT = '${params_OUTPUT_FORMAT}'")
endif()
if (params_DXIL AND DONUT_WITH_DX12)
if (NOT DXC_PATH)
message(FATAL_ERROR "donut_compile_shaders: DXC not found --- please set DXC_PATH to the full path to the DXC binary")
endif()
set(compilerCommand ShaderMake
--config ${params_CONFIG}
--out ${params_DXIL}
--platform DXIL
${output_format_arg}
-I ${DONUT_SHADER_INCLUDE_DIR}
--compiler "${DXC_PATH}"
--shaderModel 6_5
${use_api_arg})
separate_arguments(params_COMPILER_OPTIONS_DXIL NATIVE_COMMAND "${params_COMPILER_OPTIONS_DXIL}")
list(APPEND compilerCommand ${params_COMPILER_OPTIONS_DXIL})
if ("${params_BYPRODUCTS_DXIL}" STREQUAL "")
add_custom_command(TARGET ${params_TARGET} PRE_BUILD COMMAND ${compilerCommand})
else()
set(byproducts_with_paths "")
foreach(relative_path IN LISTS params_BYPRODUCTS_DXIL)
list(APPEND byproducts_with_paths "${pasams_DXIL}/${relative_path}")
endforeach()
add_custom_command(TARGET ${params_TARGET} PRE_BUILD COMMAND ${compilerCommand} BYPRODUCTS "${byproducts_with_paths}")
endif()
endif()
if (params_DXBC AND DONUT_WITH_DX11)
if (NOT FXC_PATH)
message(FATAL_ERROR "donut_compile_shaders: FXC not found --- please set FXC_PATH to the full path to the FXC binary")
endif()
set(compilerCommand ShaderMake
--config ${params_CONFIG}
--out ${params_DXBC}
--platform DXBC
${output_format_arg}
-I ${DONUT_SHADER_INCLUDE_DIR}
--compiler "${FXC_PATH}"
${use_api_arg})
separate_arguments(params_COMPILER_OPTIONS_DXBC NATIVE_COMMAND "${params_COMPILER_OPTIONS_DXBC}")
list(APPEND compilerCommand ${params_COMPILER_OPTIONS_DXBC})
if ("${params_BYPRODUCTS_DXBC}" STREQUAL "")
add_custom_command(TARGET ${params_TARGET} PRE_BUILD COMMAND ${compilerCommand})
else()
set(byproducts_with_paths "")
foreach(relative_path IN LISTS params_BYPRODUCTS_DXBC)
list(APPEND byproducts_with_paths "${pasams_DXBC}/${relative_path}")
endforeach()
add_custom_command(TARGET ${params_TARGET} PRE_BUILD COMMAND ${compilerCommand} BYPRODUCTS "${byproducts_with_paths}")
endif()
endif()
if (params_SPIRV_DXC AND DONUT_WITH_VULKAN)
if (NOT DXC_SPIRV_PATH)
message(FATAL_ERROR "donut_compile_shaders: DXC for SPIR-V not found --- please set DXC_SPIRV_PATH to the full path to the DXC binary")
endif()
set(compilerCommand ShaderMake
--config ${params_CONFIG}
--out ${params_SPIRV_DXC}
--platform SPIRV
${output_format_arg}
-I ${DONUT_SHADER_INCLUDE_DIR}
-D SPIRV
--compiler "${DXC_SPIRV_PATH}"
${NVRHI_DEFAULT_VK_REGISTER_OFFSETS}
--vulkanVersion 1.2
${use_api_arg})
separate_arguments(params_COMPILER_OPTIONS_SPIRV NATIVE_COMMAND "${params_COMPILER_OPTIONS_SPIRV}")
list(APPEND compilerCommand ${params_COMPILER_OPTIONS_SPIRV})
if ("${params_BYPRODUCTS_SPIRV}" STREQUAL "")
add_custom_command(TARGET ${params_TARGET} PRE_BUILD COMMAND ${compilerCommand})
else()
set(byproducts_with_paths "")
foreach(relative_path IN LISTS params_BYPRODUCTS_SPIRV)
list(APPEND byproducts_with_paths "${params_SPIRV_DXC}/${relative_path}")
endforeach()
add_custom_command(TARGET ${params_TARGET} PRE_BUILD COMMAND ${compilerCommand} BYPRODUCTS "${byproducts_with_paths}")
endif()
endif()
if(params_FOLDER)
set_target_properties(${params_TARGET} PROPERTIES FOLDER ${params_FOLDER})
endif()
endfunction()
# Generates a build target that will compile shaders for a given config file for all enabled Donut platforms.
#
# When OUTPUT_FORMAT is BINARY or is unspecified, the shaders will be placed into subdirectories of
# ${OUTPUT_BASE}, with names compatible with the FindDirectoryWithShaderBin framework function.
# When OUTPUT_FORMAT is HEADER, the shaders for all platforms will be placed into OUTPUT_BASE directly,
# with platform-specific extensions: .dxbc.h, .dxil.h, .spirv.h.
#
# The BYPRODUCTS_NO_EXT argument lists all generated files without extensions and without base paths.
# Similar to donut_compile_shaders, the list of byproducts is needed to get correct incremental builds
# when using static (.h) shaders with Ninja build system.
#
# Usage:
#
# donut_compile_shaders_all_platforms(TARGET <generated build target name>
# CONFIG <shader-config-file>
# SOURCES <list>
# [FOLDER <folder-in-visual-studio-solution>]
# [OUTPUT_FORMAT (HEADER|BINARY)]
# [COMPILER_OPTIONS_DXBC <string>] -- arguments passed to ShaderMake
# [COMPILER_OPTIONS_DXIL <string>]
# [COMPILER_OPTIONS_SPIRV <string>]
# [BYPRODUCTS_NO_EXT <list>])
function(donut_compile_shaders_all_platforms)
set(options "")
set(oneValueArgs TARGET CONFIG FOLDER OUTPUT_BASE OUTPUT_FORMAT COMPILER_OPTIONS_DXIL COMPILER_OPTIONS_DXBC COMPILER_OPTIONS_SPIRV)
set(multiValueArgs SOURCES BYPRODUCTS_NO_EXT)
cmake_parse_arguments(params "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if (NOT params_TARGET)
message(FATAL_ERROR "donut_compile_shaders_all_platforms: TARGET argument missing")
endif()
if (NOT params_CONFIG)
message(FATAL_ERROR "donut_compile_shaders_all_platforms: CONFIG argument missing")
endif()
if (NOT params_OUTPUT_BASE)
message(FATAL_ERROR "donut_compile_shaders_all_platforms: OUTPUT_BASE argument missing")
endif()
if ("${params_OUTPUT_FORMAT}" STREQUAL "HEADER")
# Header/static compilation puts everything into one location and differentiates between platforms
# using the .dxbc.h, .dxil.h, or .spirv.h extensions native to ShaderMake
set(output_dxbc ${params_OUTPUT_BASE})
set(output_dxil ${params_OUTPUT_BASE})
set(output_spirv ${params_OUTPUT_BASE})
else()
# Binary compilation puts shaders into per-platform folders - legacy mode compatible with various apps
set(output_dxbc ${params_OUTPUT_BASE}/dxbc)
set(output_dxil ${params_OUTPUT_BASE}/dxil)
set(output_spirv ${params_OUTPUT_BASE}/spirv)
endif()
set(byproducts_dxbc "")
set(byproducts_dxil "")
set(byproducts_spirv "")
foreach(byproduct IN LISTS params_BYPRODUCTS_NO_EXT)
list(APPEND byproducts_dxbc "${byproduct}.dxbc.h")
list(APPEND byproducts_dxil "${byproduct}.dxil.h")
list(APPEND byproducts_spirv "${byproduct}.spirv.h")
endforeach()
donut_compile_shaders(TARGET ${params_TARGET}
CONFIG ${params_CONFIG}
FOLDER ${params_FOLDER}
DXBC ${output_dxbc}
DXIL ${output_dxil}
SPIRV_DXC ${output_spirv}
OUTPUT_FORMAT ${params_OUTPUT_FORMAT}
COMPILER_OPTIONS_DXIL ${params_COMPILER_OPTIONS_DXIL}
COMPILER_OPTIONS_DXBC ${params_COMPILER_OPTIONS_DXBC}
COMPILER_OPTIONS_SPIRV ${params_COMPILER_OPTIONS_SPIRV}
SOURCES ${params_SOURCES}
BYPRODUCTS_DXBC ${byproducts_dxbc}
BYPRODUCTS_DXIL ${byproducts_dxil}
BYPRODUCTS_SPIRV ${byproducts_spirv})
endfunction()