-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
352 lines (322 loc) · 11.8 KB
/
CMakeLists.txt
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
cmake_minimum_required(VERSION 3.23)
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR} CACHE INTERNAL "")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIGURATION>)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIGURATION>)
if(MSVC)
add_definitions(/MP)
endif()
project(RGL-Samples)
if (CMAKE_SYSTEM_NAME MATCHES "Emscripten")
set(CMAKE_EXECUTABLE_SUFFIX ".html" CACHE INTERNAL "")
# required for higher memory, atomics, and threads
add_definitions(-fexceptions)
add_definitions(-pthread)
add_definitions(-sUSE_SDL=2)
set(EM_LINK
"-s USE_WEBGPU"
"--preload-file \"${CMAKE_BINARY_DIR}/shaders/\"@shaders"
"-fexceptions"
"-s USE_PTHREADS"
"-s PROXY_TO_PTHREAD"
"-s LLD_REPORT_UNDEFINED"
"-s NO_DISABLE_EXCEPTION_CATCHING"
"-s NO_DISABLE_EXCEPTION_THROWING"
"-s PTHREAD_POOL_SIZE=4"
"-s ASSERTIONS=1"
"-s USE_SDL=2"
"-s WASM_BIGINT"
"-s ASYNCIFY" # required for WebGPU!!
"-s TOTAL_MEMORY=150MB"
)
else()
set(SDL_STATIC ON CACHE INTERNAL "" FORCE)
set(SDL_SHARED OFF CACHE INTERNAL "" FORCE)
set(SDL_LIBC ON CACHE BOOL "" FORCE)
set(SDL_RENDER_ENABLED_BY_DEFAULT OFF CACHE INTERNAL "")
set(SDL_AUDIO_ENABLED_BY_DEFAULT OFF CACHE INTERNAL "")
set(SDL_FILE_ENABLED_BY_DEFAULT OFF CACHE INTERNAL "")
set(SDL_CPUINFO_ENABLED_BY_DEFAULT OFF CACHE INTERNAL "")
set(SDL_FILESYSTEM_ENABLED_BY_DEFAULT OFF CACHE INTERNAL "")
set(SDL_SENSOR_ENABLED_BY_DEFAULT OFF CACHE INTERNAL "")
set(SDL_LOCALE_ENABLED_BY_DEFAULT OFF CACHE INTERNAL "")
set(SDL_MISC_ENABLED_BY_DEFAULT OFF CACHE INTERNAL "")
set(SDL_ATOMIC_ENABLED_BY_DEFAULT OFF CACHE INTERNAL "")
set(SDL_TIMERS_ENABLED_BY_DEFAULT OFF CACHE INTERNAL "")
set(SDL_POWER_ENABLED_BY_DEFAULT OFF CACHE INTERNAL "")
set(SDL_HAPTIC_ENABLED_BY_DEFAULT OFF CACHE INTERNAL "")
set(SDL_JOYSTIC_ENABLED_BY_DEFAULT OFF CACHE INTERNAL "")
if(CMAKE_SYSTEM_NAME MATCHES "WindowsStore")
set(SDL_RENDER_ENABLED_BY_DEFAULT ON CACHE INTERNAL "")
endif()
add_subdirectory(deps/SDL2 EXCLUDE_FROM_ALL)
endif()
add_subdirectory(deps/glm EXCLUDE_FROM_ALL)
add_subdirectory(deps/fmt EXCLUDE_FROM_ALL)
add_subdirectory(deps/stb_image EXCLUDE_FROM_ALL)
add_subdirectory(deps/imgui-cmake EXCLUDE_FROM_ALL)
add_subdirectory(deps/tinyobjloader EXCLUDE_FROM_ALL)
if (NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten")
add_subdirectory(deps/OpenXR-SDK EXCLUDE_FROM_ALL)
endif()
if(NOT (CMAKE_VS_PLATFORM_NAME STREQUAL ""))
if(CMAKE_VS_PLATFORM_NAME_DEFAULT STREQUAL CMAKE_VS_PLATFORM_NAME)
set(VS_CROSSCOMP OFF CACHE INTERNAL "")
else()
set(VS_CROSSCOMP ON CACHE INTERNAL "")
endif()
else()
set(VS_CROSSCOMP OFF CACHE INTERNAL "")
endif()
if(CMAKE_CROSSCOMPILING OR (VS_CROSSCOMP AND WIN32))
set(RGL_ENABLE_RGLC OFF CACHE INTERNAL "")
set(TOOLS_DIR "${CMAKE_BINARY_DIR}/host-tools" CACHE INTERNAL "")
file(MAKE_DIRECTORY ${TOOLS_DIR})
set(CC_GENERATOR "-G \"${CMAKE_GENERATOR}\"")
if (CMAKE_SYSTEM_NAME MATCHES "Emscripten")
if (CMAKE_HOST_WIN32)
set(CC_GENERATOR "") # otherwise it'll try to use ninja/make which may not exist in that context
endif()
set(HT_ENABLE_WGSL "-DRGL_ENABLE_WEBGPU=ON") # otherwise the separate compiler exe will not know that WGSL output should be enabled
endif()
add_custom_command(
PRE_BUILD
WORKING_DIRECTORY ${TOOLS_DIR}
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/deps/RGL/CMakeLists.txt"
COMMAND ${CMAKE_COMMAND} ${CC_GENERATOR} -DCMAKE_BUILD_TYPE=Release ${HT_ENABLE_WGSL} "${CMAKE_CURRENT_SOURCE_DIR}/deps/RGL"
OUTPUT "${TOOLS_DIR}/CMakeCache.txt"
)
if(MSVC)
set(rglc_ext ".exe")
endif()
set(rglc_path "${TOOLS_DIR}/Release/rglc${rglc_ext}")
add_custom_command(
PRE_BUILD
DEPENDS "${TOOLS_DIR}/CMakeCache.txt"
OUTPUT "${rglc_path}"
COMMAND ${CMAKE_COMMAND} --build . --config Release --target rglc
WORKING_DIRECTORY "${TOOLS_DIR}"
VERBATIM
)
add_custom_target("rglc" DEPENDS "${rglc_path}")
endif()
set(RGL_DISABLE_DX OFF CACHE INTERNAL "")
set(RGL_DISABLE_VK OFF CACHE INTERNAL "")
set(RGL_RUNTIME_COMPILATION ON CACHE INTERNAL "")
add_subdirectory(deps/RGL EXCLUDE_FROM_ALL)
# App framework using SDL2
add_library(${PROJECT_NAME}-Common "Common/App.cpp" "Common/App.hpp" "Common/ExampleFramework.hpp" "Common/ExampleFramework.cpp" "Common/AppleFns.hpp" "Common/AppleFns.mm")
target_compile_features(${PROJECT_NAME}-Common PRIVATE cxx_std_20)
target_include_directories(${PROJECT_NAME}-Common PUBLIC "./" PRIVATE "deps/SDL2/include")
if (APPLE)
set_source_files_properties("Common/AppleFns.mm" PROPERTIES
COMPILE_FLAGS "-x objective-c++"
)
endif()
if (CMAKE_SYSTEM_NAME MATCHES WindowsStore)
add_definitions(-D_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING)
add_definitions(-D_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS)
endif()
if(NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten")
set(SDLLIB "SDL2-static")
set(SDL_HEADERS "sdl_headers_copy")
endif()
if(NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten" AND NOT CMAKE_SYSTEM_NAME MATCHES "WindowsStore")
set(SDLMAIN_LIB "SDL2main")
endif()
target_link_libraries(${PROJECT_NAME}-Common
PUBLIC
${SDLLIB} ${SDLMAIN_LIB}
RGL
glm
fmt
stb_image
imgui
)
macro(add_sample dir)
set(dir "${CMAKE_CURRENT_LIST_DIR}/${dir}")
file(GLOB_RECURSE SOURCES "${dir}/*.cpp" "${dir}/*.hpp" "${dir}/*.h")
file(GLOB_RECURSE RESOURCES "${dir}/*.png" "${dir}/*.jpg" "${dir}/*.obj")
if (APPLE)
set(ADDITIONALS ${RESOURCES})
endif()
add_executable(${dir} ${SOURCES} ${ADDITIONALS} "iosLaunchScreen.storyboard")
if (NOT APPLE)
foreach(FILE ${RESOURCES})
get_filename_component(output_name "${FILE}" NAME)
set(outname "${CMAKE_BINARY_DIR}/$<CONFIGURATION>/${output_name}")
add_custom_command(PRE_BUILD
OUTPUT "${outname}"
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${FILE} "${outname}"
DEPENDS ${FILE}
)
list(APPEND target_assets "${outname}")
endforeach()
endif()
target_compile_features(${dir} PRIVATE cxx_std_20)
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT "${dir}")
set_target_properties(${dir} PROPERTIES
MACOSX_BUNDLE TRUE
XCODE_GENERATE_SCHEME ON
RESOURCE "${RESOURCES}"
VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>"
XCODE_ATTRIBUTE_BUNDLE_IDENTIFIER "com.ravbug.${APPNAME}" # with templated plist we can set this
XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "com.ravbug.${dir}"
XCODE_ATTRIBUTE_CURRENTYEAR "${CURRENTYEAR}"
XCODE_ATTRIBUTE_MTL_ENABLE_DEBUG_INFO "INCLUDE_SOURCE"
XCODE_ATTRIBUTE_MTL_LANGUAGE_REVISION "Metal24"
MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/Info.plist.in"
)
target_link_libraries(${dir} PRIVATE ${PROJECT_NAME}-Common ${EM_LINK})
macro(shader_compile infile stage api extension binary)
get_filename_component(name_only ${infile} NAME)
set(outname "${CMAKE_CURRENT_BINARY_DIR}/shaders/${api}/${name_only}.${extension}")
if (${api} MATCHES "Metal")
STRING(REPLACE "." "_" name_fixed ${name_only} )
set(entrypoint "--entrypoint" "${name_fixed}")
endif()
list(APPEND all_shaders "${outname}")
add_custom_command(
PRE_BUILD
OUTPUT "${outname}"
DEPENDS ${infile}
COMMAND ${rglc_path} -f "${infile}" -o "${outname}" --api ${api} --stage ${stage} --debug ${binary} ${entrypoint}
)
endmacro()
file(GLOB all_shaders "${dir}/*.vert" "${dir}/*.frag" "${dir}/*.comp")
target_sources(${dir} PUBLIC ${all_shaders})
set_source_files_properties(${all_shaders} PROPERTIES
HEADER_FILE_ONLY ON
XCODE_EXPLICIT_FILE_TYPE "sourcecode.glsl"
)
source_group(Shaders FILES ${all_shaders})
file(GLOB all_verts "${dir}/*.vert")
foreach(FILE ${all_verts})
if(RGL_VK_AVAILABLE)
shader_compile("${FILE}" "vertex" "Vulkan" "spv" "")
endif()
if(RGL_DX12_AVAILABLE)
shader_compile("${FILE}" "vertex" "Direct3D12" "hlsl" "")
target_sources(${dir} PUBLIC ${outname})
set_source_files_properties(${outname} PROPERTIES
GENERATED TRUE
VS_SHADER_MODEL "6.4"
VS_SHADER_TYPE "Vertex"
VS_SHADER_ENABLE_DEBUG ON
VS_SHADER_DISABLE_OPTIMIZATIONS ON
)
endif()
if(RGL_MTL_AVAILABLE)
shader_compile("${FILE}" "vertex" "Metal" "metal" "")
target_sources(${dir} PUBLIC ${outname})
set_source_files_properties(${outname} PROPERTIES
GENERATED TRUE
HEADER_FILE_ONLY OFF
LANGUAGE METAL
XCODE_EXPLICIT_FILE_TYPE "sourcecode.metal"
COMPILE_FLAGS "-gline-tables-only -frecord-sources" # enable shader source debugging
)
endif()
if(RGL_WEBGPU_AVAILABLE)
shader_compile("${FILE}" "vertex" "WebGPU" "wgsl" "")
endif()
endforeach()
file(GLOB all_frags "${dir}/*.frag")
foreach(FILE ${all_frags})
if(RGL_VK_AVAILABLE)
shader_compile("${FILE}" "fragment" "Vulkan" "spv" "")
endif()
if(RGL_DX12_AVAILABLE)
shader_compile("${FILE}" "fragment" "Direct3D12" "hlsl" "")
target_sources(${dir} PUBLIC ${outname})
set_source_files_properties(${outname} PROPERTIES
GENERATED TRUE
VS_SHADER_MODEL "6.4"
VS_SHADER_TYPE "Pixel"
VS_SHADER_ENABLE_DEBUG ON
VS_SHADER_DISABLE_OPTIMIZATIONS ON
)
endif()
if(RGL_MTL_AVAILABLE)
shader_compile("${FILE}" "fragment" "Metal" "metal" "")
target_sources(${dir} PUBLIC ${outname})
set_source_files_properties(${outname} PROPERTIES
GENERATED TRUE
HEADER_FILE_ONLY OFF
LANGUAGE METAL
XCODE_EXPLICIT_FILE_TYPE "sourcecode.metal"
COMPILE_FLAGS "-gline-tables-only -frecord-sources" # enable shader source debugging
)
endif()
if(RGL_WEBGPU_AVAILABLE)
shader_compile("${FILE}" "fragment" "WebGPU" "wgsl" "")
endif()
endforeach()
file(GLOB all_computes "${dir}/*.comp")
foreach(FILE ${all_computes})
if(RGL_VK_AVAILABLE)
shader_compile("${FILE}" "compute" "Vulkan" "spv" "")
endif()
if(RGL_DX12_AVAILABLE)
shader_compile("${FILE}" "compute" "Direct3D12" "hlsl" "")
target_sources(${dir} PUBLIC ${outname})
set_source_files_properties(${outname} PROPERTIES
GENERATED TRUE
VS_SHADER_MODEL "6.4"
VS_SHADER_TYPE "Compute"
VS_SHADER_ENABLE_DEBUG ON
VS_SHADER_DISABLE_OPTIMIZATIONS ON
)
endif()
if(RGL_MTL_AVAILABLE)
shader_compile("${FILE}" "compute" "Metal" "metal" "")
target_sources(${dir} PUBLIC ${outname})
set_source_files_properties(${outname} PROPERTIES
GENERATED TRUE
HEADER_FILE_ONLY OFF
LANGUAGE METAL
XCODE_EXPLICIT_FILE_TYPE "sourcecode.metal"
COMPILE_FLAGS "-gline-tables-only -frecord-sources" # enable shader source debugging
)
endif()
endforeach()
add_custom_target(${dir}_CompileShaders
DEPENDS ${all_shaders} ${target_assets}
)
add_dependencies(${dir} ${dir}_CompileShaders)
add_dependencies(${dir}_CompileShaders rglc)
endmacro()
add_sample(01-HelloTriangle)
add_sample(02-Cubes)
add_sample(03-Imgui)
add_sample(04-Deferred)
if (CMAKE_SYSTEM_NAME STREQUAL "Windows") # OpenXR only on windows fornow
add_sample(05-XR)
target_link_libraries(05-XR
PUBLIC
openxr_loader
)
endif()
add_sample(06-Asteroids)
add_sample(07-Mipmap)
add_sample(08-Cubemap)
add_sample(09-BindlessTexturing)
target_link_libraries(06-Asteroids
PUBLIC
tinyobjloader
)
if (NOT CMAKE_SYSTEM_NAME MATCHES "Emscripten")
set(EM_PROJECTS "xr_global_generated_files" "generate_openxr_header" "openxr_loader")
endif()
set_target_properties(
${SDLMAIN_LIB}
${SDLLIB}
${SDL_HEADERS}
"RGL-Samples-Common"
"fmt"
"stb_image"
"imgui"
"tinyobjloader"
${EM_PROJECTS}
PROPERTIES
FOLDER "RGL Samples Dependencies"
)