-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
260 lines (240 loc) · 9.57 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
cmake_minimum_required(VERSION 3.20)
# set the project name and version
project(Vulf)
# specify the C++ standard and making it mandatory
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "_CMakePredefinedTargets")
# Set the startup project - HelloTriangle
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT HelloTriangle)
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
set(CMAKE_CXX_FLAGS_DEBUG "/ZI")
set(CMAKE_SHARED_LINKER_FLAGS "/SAFESEH:NO")
set(CMAKE_EXE_LINKER_FLAGS "/SAFESEH:NO")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /INCREMENTAL:YES" )
endif()
#===============================================================================
# TODO: Download the necesary assets here
#===============================================================================
# COMPILE SHADERS
# TODO: Design decision on whether shaders are distributed with Vulf library or with examples, this will also influence how theya re grouped into folders and compiled
# Set the path to glslc compiler based on the OS
if(APPLE)
set(glslc_executable $ENV{VULKAN_SDK}/macOS/bin/glslc)
endif()
if(WIN32)
set(glslc_executable $ENV{VULKAN_SDK}/Bin/glslc)
endif()
# Set the directory for loading the compiled spir-v shaders and source direcotry for glsl shaders
set(SHADER_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/shaders/glsl)
set(SHADER_BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/shaders/spir-v)
# Add the glsl shader files
file(GLOB_RECURSE SHADERS
${SHADER_SOURCE_DIR}/*.vert
${SHADER_SOURCE_DIR}/*.frag
${SHADER_SOURCE_DIR}/*.comp
${SHADER_SOURCE_DIR}/*.geom
${SHADER_SOURCE_DIR}/*.tesc
${SHADER_SOURCE_DIR}/*.tese
${SHADER_SOURCE_DIR}/*.mesh
${SHADER_SOURCE_DIR}/*.task
${SHADER_SOURCE_DIR}/*.rgen
${SHADER_SOURCE_DIR}/*.rchit
${SHADER_SOURCE_DIR}/*.rmiss)
# Creating the spir-v source directory
add_custom_command(
COMMAND
${CMAKE_COMMAND} -E make_directory ${SHADER_BINARY_DIR}
OUTPUT ${SHADER_BINARY_DIR}
COMMENT "Creating ${SHADER_BINARY_DIR}"
)
source_group(shaders "Shaders" FILES ${SHADERS})
# Custom rule for compiling the shader files
foreach(source IN LISTS SHADERS)
get_filename_component(FILENAME ${source} NAME)
add_custom_command(
COMMAND
${glslc_executable}
# -MD -MF ${SHADER_BINARY_DIR}/${FILENAME}.d
-o ${SHADER_BINARY_DIR}/${FILENAME}.spv
${source}
OUTPUT ${SHADER_BINARY_DIR}/${FILENAME}.spv
DEPENDS ${source} ${SHADER_BINARY_DIR}
COMMENT "Compiling ${FILENAME}"
)
list(APPEND SPV_SHADERS ${SHADER_BINARY_DIR}/${FILENAME}.spv)
endforeach()
# add dependency for shader files
add_custom_target(shaders ALL DEPENDS ${SPV_SHADERS} SOURCES ${SHADERS})
#===============================================================================
# INCLUDE AND LINK DIRECTORIES
# MACOS
if (APPLE)
# set the include directories for the vendor dependencies
# TODO: Make this cross-platform for now it'll be MacOS specific for the Vulkan headers
include_directories(
# GLFW headers
"${CMAKE_SOURCE_DIR}/vendor/glfw/include"
# GLM headers
"${CMAKE_SOURCE_DIR}/vendor/glm"
# ImGui Headers
"${CMAKE_SOURCE_DIR}/vendor/ImGui"
# The entire vendor folder
"${CMAKE_SOURCE_DIR}/vendor"
"${CMAKE_SOURCE_DIR}/vendor/stb"
# Tracy headers
"${CMAKE_SOURCE_DIR}/vendor/TracyProfiler"
#SPIRV-Reflect
"${CMAKE_SOURCE_DIR}/vendor/SPIRVReflect"
# Optick
#"${CMAKE_SOURCE_DIR}/vendor/optick/src"
# Vulf
"${CMAKE_SOURCE_DIR}/src"
"${CMAKE_SOURCE_DIR}/src/Vulkan"
# KTX
"${CMAKE_SOURCE_DIR}/vendor/ktx/include"
"${CMAKE_SOURCE_DIR}/vendor/tinygltf"
"${CMAKE_SOURCE_DIR}/vendor/ktx/other_include/"
# taskflow
"${CMAKE_SOURCE_DIR}/vendor/taskflow/"
# Vulkan MacOS headers
$ENV{VULKAN_SDK}/macOS/include
)
link_directories(
# glfw library path
"${CMAKE_SOURCE_DIR}/vendor/glfw/build/src"
# MoltenVk libraries
$ENV{VULKAN_SDK}/macOS/lib
)
endif()
# WINDOWS
if(WIN32)
include_directories(
# GLFW headers
"${CMAKE_SOURCE_DIR}/vendor/glfw/include"
# GLM headers
"${CMAKE_SOURCE_DIR}/vendor/glm"
# ImGui Headers
"${CMAKE_SOURCE_DIR}/vendor/ImGui"
# The entire vendor folder
"${CMAKE_SOURCE_DIR}/vendor"
"${CMAKE_SOURCE_DIR}/vendor/stb"
# Tracy headers
"${CMAKE_SOURCE_DIR}/vendor/TracyProfiler"
#SPIRV-Reflect
"${CMAKE_SOURCE_DIR}/vendor/SPIRVReflect"
# Optick
#"${CMAKE_SOURCE_DIR}/vendor/optick/src"
# Vulf
"${CMAKE_SOURCE_DIR}/src"
"${CMAKE_SOURCE_DIR}/src/Vulkan"
# KTX
"${CMAKE_SOURCE_DIR}/vendor/ktx/include"
"${CMAKE_SOURCE_DIR}/vendor/tinygltf"
"${CMAKE_SOURCE_DIR}/vendor/ktx/other_include/"
# taskflow
"${CMAKE_SOURCE_DIR}/vendor/taskflow/"
# Platform specific vulkan headers
$ENV{VULKAN_SDK}/include
)
link_directories(
# GLFW library path
"${CMAKE_SOURCE_DIR}/vendor/glfw/libs"
# Vulkan libraries
$ENV{VULKAN_SDK}/Lib
)
endif()
#===============================================================================
# Add source files
file(GLOB_RECURSE SOURCE_FILES
${CMAKE_SOURCE_DIR}/src/*.c
${CMAKE_SOURCE_DIR}/src/*.cpp
)
# Add source header files
file(GLOB_RECURSE HEADER_FILES
${CMAKE_SOURCE_DIR}/src/*.h
${CMAKE_SOURCE_DIR}/src/*.hpp
)
# vendor source files that need to compiled to build into Vulf
file(GLOB_RECURSE VENDOR_SOURCE_FILES
${CMAKE_SOURCE_DIR}/vendor/imgui/*.c
${CMAKE_SOURCE_DIR}/vendor/imgui/*.cpp
${CMAKE_SOURCE_DIR}/vendor/imgui/*.c
${CMAKE_SOURCE_DIR}/vendor/imgui/*.cpp
${CMAKE_SOURCE_DIR}/vendor/ImGuizmo/ImCurveEdit.cpp
${CMAKE_SOURCE_DIR}/vendor/ImGuizmo/ImGradient.cpp
${CMAKE_SOURCE_DIR}/vendor/ImGuizmo/ImGuizmo.cpp
${CMAKE_SOURCE_DIR}/vendor/ImGuizmo/ImSequencer.cpp
${CMAKE_SOURCE_DIR}/vendor/TracyProfiler/TracyClient.cpp
#${CMAKE_SOURCE_DIR}/vendor/optick/*.h
#${CMAKE_SOURCE_DIR}/vendor/optick/*.cpp
${CMAKE_SOURCE_DIR}/vendor/SPIRVReflect/spirv_reflect.c
${CMAKE_SOURCE_DIR}/vendor/SPIRVReflect/common/output_stream.cpp
${CMAKE_SOURCE_DIR}/vendor/ktx/*.c
${CMAKE_SOURCE_DIR}/vendor/ktx/other_include/KHR/*.h
${CMAKE_SOURCE_DIR}/vendor/ktx/*.h
)
# remove unnecessary files
list(REMOVE_ITEM SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/application.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/application.h")
#list(REMOVE_ITEM SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/application.h")
#===============================================================================
#source_group("src" FILES ${SOURCE_FILES})
# file(GLOB_RECURSE VENDOR_HEADER_FILES
# ${CMAKE_SOURCE_DIR}/vendor/imgui/*.h
# ${CMAKE_SOURCE_DIR}/vendor/imgui/*.hpp
# )
# source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}"
# PREFIX "Source"
# FILES ${SOURCE_FILES})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCE_FILES})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${HEADER_FILES})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${VENDOR_SOURCE_FILES})
#===============================================================================
# Compile the source files into a static library
add_library(Vulf STATIC ${HEADER_FILES} ${SOURCE_FILES} ${VENDOR_SOURCE_FILES})
add_dependencies(Vulf shaders)
#===============================================================================
# Global pre-processor defines
add_compile_definitions(TRACY_ENABLE)
add_compile_definitions(_DEBUG)
add_compile_definitions("SHADER_BINARY_DIR=\"${SHADER_BINARY_DIR}\"")
add_compile_definitions("SRC_DIR=\"${CMAKE_SOURCE_DIR}\"")
add_compile_definitions("ASSETS_DIR=\"${CMAKE_SOURCE_DIR}/data\"")
#===============================================================================
# Link with necessary external libraries
if (APPLE)
target_link_libraries(Vulf glfw3 vulkan.1 "-framework CoreFoundation -framework CoreAudio -framework OpenAL -framework Cocoa -framework IOKit -framework CoreGraphics")
endif (APPLE)
if(WIN32)
target_link_libraries(Vulf glfw3 vulkan-1)
endif()
#===============================================================================
# now build the examples
# HACK: Just try building hello triangle then make it dynamics to build all exaples with source dependency
file(GLOB_RECURSE EXAMPLE_SOURCE_FILES
${CMAKE_SOURCE_DIR}/examples/cpp/*.cpp
)
foreach(source IN LISTS EXAMPLE_SOURCE_FILES)
get_filename_component(FILENAME ${source} NAME_WE)
message("Examples being built : ${FILENAME}")
# TODO: Improve source group folder hierarchy for examples
# source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${source})
# Build the examples as executables (run them using make examplefilename.run command)
## add the source files in each directory of the example instead of by *.cpp + add a text file or something to load shaders from common pool or the folder itseld
add_executable(${FILENAME} ${source})
target_link_libraries(${FILENAME} Vulf)
add_dependencies(${FILENAME} Vulf)
if(APPLE) # Used only with makefiles cause VS can handle this
# Run the binary built using 'make run' command
add_custom_target("${FILENAME}.run"
COMMAND ${FILENAME}
DEPENDS ${FILENAME} Vulf
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} # here we set the executable direcotry to the CMake home dir
)
endif(APPLE)
# Set the output directory for the examples build separately
set_target_properties(${FILENAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/examples)
set_target_properties(${FILENAME} PROPERTIES FOLDER "examples")
endforeach()
#===============================================================================