-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
244 lines (216 loc) · 7.78 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
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
project(VulkanRenderer)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_CXX_EXTENSIONS FALSE)
include(CheckIPOSupported)
check_ipo_supported(RESULT IPO_SUPPORTED)
if(IPO_SUPPORTED)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_DEBUG FALSE)
endif()
message(STATUS "IPO supported: ${IPO_SUPPORTED}")
if(MSVC)
string(REGEX REPLACE "[-/]W[1-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(RENDERER_REQUIRED_WARN_FLAGS /W4 /permissive-)
else()
set(RENDERER_REQUIRED_WARN_FLAGS -Wall -Wextra -Wpedantic -Wnon-virtual-dtor)
endif()
option(RENDERER_TREAT_WARN_AS_ERROR "Treat compiler warnings as errors" FALSE)
if(RENDERER_TREAT_WARN_AS_ERROR)
if(MSVC)
list(APPEND RENDERER_REQUIRED_WARN_FLAGS /Wx)
else()
list(APPEND RENDERER_REQUIRED_WARN_FLAGS -Werror -pedantic-errors)
endif()
endif()
# See: https://github.com/cpp-best-practices/cppbestpractices.
option(RENDERER_USE_EXTENSIVE_WARNINGS "Enable warnings recommended by C++ Best Practices" TRUE)
if(RENDERER_USE_EXTENSIVE_WARNINGS)
if(MSVC)
set(RENDERER_EXTRA_WARN_FLAGS
/w14242 # possible loss of data: conversion from `type1` to `type2`
/w14254 # possible loss of data: conversion from `type1:field_bits`
# to `type2:field_bits`
/w14263 # member function does not override any base class virtual
# member function
/w14265 # class has virtual functions, but destructor is not
# virtual; instances of this class may not be destructed
# correctly
/w14287 # unsigned/negative constant mismatch
/we4289 # loop control variable declared in the for-loop is used
# outside the for-loop scope
/w14296 # expression is always [true | false]
/w14311 # pointer truncation from `type1` to `type2`
/w14545 # expression before comma evaluates to a function which is
# missing an argument list
/w14546 # function call before comma missing argument list
/w14547 # operator before comma has no effect; expected operator
# with side-effect
/w14549 # operator before comma has no effect; did you intend
# 'operator'?
/w14555 # expression has no effect; expected expression with
# side-effect
/w14619 # pragma warning: there is no warning number `number`
/w14640 # thread un-safe static member initialization
/w14826 # conversion from 'type1' to 'type_2' is sign-extended which
# may cause unexpected runtime behavior
/w14928 # illegal copy-initialization; more than one user-defined
# conversion has been implicitly applied
)
else()
set(RENDERER_EXTRA_WARN_FLAGS
-Wcast-align
-Wunused
-Woverloaded-virtual
-Wconversion
-Wsign-conversion
-Wnull-dereference
-Wmisleading-indentation
-Wformat=2
-Wfloat-equal
)
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
list(APPEND RENDERER_EXTRA_WARN_FLAGS
-Wduplicated-cond
-Wduplicated-branches
-Wuseless-cast
-Wlogical-op
)
elseif(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
list(APPEND RENDERER_EXTRA_WARN_FLAGS
-Wimplicit-fallthrough
-Wshorten-64-to-32
)
endif()
endif()
find_package(glfw3 CONFIG REQUIRED)
find_package(glm CONFIG REQUIRED)
find_package(spdlog CONFIG REQUIRED)
find_package(tinyobjloader CONFIG REQUIRED)
find_package(Vulkan REQUIRED)
set(RESOURCES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Resources)
set(SHADER_SOURCES
Source/Shaders/Triangle.frag
Source/Shaders/Triangle.vert
Source/Shaders/Texture.frag
Source/Shaders/Texture.vert
Source/Shaders/Skybox.frag
Source/Shaders/Skybox.vert
)
foreach(RELATIVE_SOURCE_PATH ${SHADER_SOURCES})
if(NOT RELATIVE_SOURCE_PATH MATCHES "^Source/")
message(WARNING "${RELATIVE_SOURCE_PATH} is not in the Source directory.")
endif()
get_filename_component(FILE_NAME_WITH_EXT ${RELATIVE_SOURCE_PATH} NAME)
get_filename_component(FILE_DIR ${RELATIVE_SOURCE_PATH} DIRECTORY)
string(REGEX
REPLACE "^Source/" ""
RESOURCES_RELATIVE_FILE_DIR
${FILE_DIR}
)
set(RESOURCES_RELATIVE_FILE_DIR "${RESOURCES_RELATIVE_FILE_DIR}/$<CONFIG>")
set(SOURCE_PATH "${PROJECT_SOURCE_DIR}/${RELATIVE_SOURCE_PATH}")
set(OUTPUT_DIR "${RESOURCES_DIR}/${RESOURCES_RELATIVE_FILE_DIR}")
set(OUTPUT_FILE_WITH_EXT "${FILE_NAME_WITH_EXT}.spv")
set(OUTPUT_FILE_PATH "${OUTPUT_DIR}/${OUTPUT_FILE_WITH_EXT}")
add_custom_command(
OUTPUT ${OUTPUT_FILE_PATH}
COMMAND ${CMAKE_COMMAND} -E make_directory ${OUTPUT_DIR}
COMMAND ${Vulkan_GLSLC_EXECUTABLE} ${SOURCE_PATH} -g -o ${OUTPUT_FILE_PATH}
DEPENDS ${SOURCE_PATH}
VERBATIM
)
list(APPEND SPIRV_BYTECODE ${OUTPUT_FILE_PATH})
string(MAKE_C_IDENTIFIER ${FILE_NAME_WITH_EXT} FILE_NAME_ESCAPED)
string(TOUPPER ${FILE_NAME_ESCAPED} FILE_NAME_UPPER)
list(APPEND RENDERER_SHADER_BYTECODE_PATH_DEFINES
"${FILE_NAME_UPPER}_SHADER_RELATIVE_PATH=\"${RESOURCES_RELATIVE_FILE_DIR}/${OUTPUT_FILE_WITH_EXT}\""
)
endforeach()
add_custom_target(RendererShaders
DEPENDS ${SPIRV_BYTECODE}
)
# Workaround for older versions of GLM
if(NOT TARGET glm::glm)
get_target_property(GLM_INCLUDE_DIR glm INTERFACE_INCLUDE_DIRECTORIES)
add_library(glm::glm INTERFACE IMPORTED)
set_target_properties(glm::glm PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GLM_INCLUDE_DIR}
)
endif()
add_executable(Renderer
Source/Main.cpp
Source/Camera.cpp
Source/Device.cpp
Source/Model.cpp
Source/Tga.cpp
Source/TinyObjLoader.cpp
)
target_precompile_headers(Renderer
PRIVATE
Source/pch.hpp
)
target_include_directories(Renderer
PRIVATE
include
)
add_dependencies(Renderer
RendererShaders
)
target_compile_definitions(Renderer
PRIVATE
GLFW_INCLUDE_VULKAN
GLM_FORCE_DEPTH_ZERO_TO_ONE
GLM_FORCE_RADIANS
VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1
${RENDERER_SHADER_BYTECODE_PATH_DEFINES}
)
target_link_libraries(Renderer
PRIVATE
${CMAKE_DL_LIBS}
glfw
glm::glm
spdlog::spdlog # TODO: Figure out why spdlog_header_only causes errors.
tinyobjloader::tinyobjloader
Vulkan::Vulkan
)
target_compile_options(Renderer
PRIVATE
${RENDERER_REQUIRED_WARN_FLAGS}
${RENDERER_EXTRA_WARN_FLAGS}
)
# CMake flags for Release builds are suboptimal.
# See: https://gitlab.kitware.com/cmake/cmake/-/issues/20812.
# See: https://stackoverflow.com/questions/28178978/how-to-generate-pdb-files-for-release-build-with-cmake-flags.
if(MSVC)
target_compile_options(Renderer
PRIVATE
$<$<CONFIG:Release>:/Zc:inline>
$<$<CONFIG:Release>:/Zi>
$<$<CONFIG:Release>:/Gy>
)
target_link_options(Renderer
PRIVATE
$<$<CONFIG:Release>:/DEBUG>
$<$<CONFIG:Release>:/INCREMENTAL:NO>
$<$<CONFIG:Release>:/OPT:REF>
$<$<CONFIG:Release>:/OPT:ICF>
)
elseif((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang"))
target_compile_options(Renderer
PRIVATE
$<$<CONFIG:Release>:-g>
)
target_link_options(Renderer
PRIVATE
$<$<CONFIG:Release>:-g>
)
if(WIN32)
target_compile_options(Renderer
PRIVATE
$<$<CONFIG:Release>:-gcodeview>
)
endif()
endif()