-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
CMakeLists.txt
386 lines (325 loc) · 13.9 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# Copyright (c) 2021-2022, NVIDIA CORPORATION. All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
cmake_minimum_required(VERSION 3.18)
project(instant-ngp
VERSION 1.0
DESCRIPTION "Instant Neural Graphics Primitives"
LANGUAGES C CXX CUDA
)
set(NGP_VERSION "${CMAKE_PROJECT_VERSION}")
if (NOT NGP_DEPLOY)
set(NGP_VERSION "${NGP_VERSION}dev")
endif()
option(NGP_BUILD_EXECUTABLE "Build instant-ngp.exe?" ON)
option(NGP_BUILD_WITH_GUI "Build with GUI support (requires GLFW and GLEW)?" ON)
option(NGP_BUILD_WITH_OPTIX "Build with OptiX to enable hardware ray tracing?" ON)
option(NGP_BUILD_WITH_PYTHON_BINDINGS "Build bindings that allow instrumenting instant-ngp with Python?" ON)
option(NGP_BUILD_WITH_VULKAN "Build with Vulkan to enable DLSS support?" ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
###############################################################################
# Build type and C++ compiler setup
###############################################################################
# Set a default configuration if none was specified
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "No release type specified. Setting to 'Release'.")
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "RelWithDebInfo")
endif()
if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/dependencies/glfw/CMakeLists.txt")
message(FATAL_ERROR
"Some instant-ngp dependencies are missing. "
"If you forgot the \"--recursive\" flag when cloning this project, "
"this can be fixed by calling \"git submodule update --init --recursive\"."
)
endif()
if (APPLE)
set(CMAKE_MACOSX_RPATH ON)
endif()
if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_CRT_SECURE_NO_WARNINGS")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP24")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
endif()
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_EXTENSIONS OFF)
###############################################################################
# CUDA compiler setup
###############################################################################
set(CMAKE_CUDA_STANDARD 14)
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
set(CMAKE_CUDA_EXTENSIONS OFF)
set(CUDA_LINK_LIBRARIES_KEYWORD PUBLIC)
set(CMAKE_CUDA_RUNTIME_LIBRARY Shared)
if (MSVC)
list(APPEND CUDA_NVCC_FLAGS "-Xcompiler=/bigobj")
else()
list(APPEND CUDA_NVCC_FLAGS "-Xcompiler=-Wno-float-conversion")
list(APPEND CUDA_NVCC_FLAGS "-Xcompiler=-fno-strict-aliasing")
list(APPEND CUDA_NVCC_FLAGS "-Xcompiler=-fPIC")
endif()
list(APPEND CUDA_NVCC_FLAGS "--extended-lambda")
list(APPEND CUDA_NVCC_FLAGS "--expt-relaxed-constexpr")
list(APPEND CUDA_NVCC_FLAGS "--use_fast_math")
###############################################################################
# Dependencies
###############################################################################
set(TCNN_BUILD_BENCHMARK OFF)
set(TCNN_BUILD_EXAMPLES OFF)
set(TCNN_ALLOW_CUBLAS_CUSOLVER OFF)
add_subdirectory(dependencies/tiny-cuda-nn)
set(CMAKE_CUDA_ARCHITECTURES ${TCNN_CUDA_ARCHITECTURES})
if (NGP_BUILD_WITH_GUI)
find_package(Vulkan)
if (Vulkan_FOUND AND NGP_BUILD_WITH_VULKAN)
set(NGP_VULKAN ON)
list(APPEND NGP_DEFINITIONS -DNGP_VULKAN -DGLFW_INCLUDE_VULKAN)
list(APPEND NGP_INCLUDE_DIRECTORIES "${Vulkan_INCLUDE_DIRS}")
list(APPEND NGP_LIBRARIES ${Vulkan_LIBRARIES})
list(APPEND GUI_SOURCES src/dlss.cu)
# DLSS depends on vulkan, so appears here
list(APPEND NGP_INCLUDE_DIRECTORIES "dependencies/dlss/include")
if (MSVC)
list(APPEND NGP_LINK_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/dependencies/dlss/lib/Windows_x86_64/x86_64")
list(APPEND NGP_LIBRARIES "$<IF:$<CONFIG:Debug>,nvsdk_ngx_d_dbg,nvsdk_ngx_d>")
else()
list(APPEND NGP_LINK_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/dependencies/dlss/lib/Linux_x86_64")
list(APPEND NGP_LIBRARIES nvsdk_ngx)
endif()
else()
set(NGP_VULKAN OFF)
if (NGP_BUILD_WITH_VULKAN)
message(WARNING
"Vulkan was not found. Instant neural graphics primitives will still "
"compile and run correctly, but DLSS will not be supported."
)
endif()
endif()
# OpenXR
if (WIN32)
list(APPEND NGP_DEFINITIONS -DXR_USE_PLATFORM_WIN32 -DGLFW_EXPOSE_NATIVE_WGL)
elseif (UNIX AND NOT APPLE)
list(APPEND NGP_DEFINITIONS -DGLFW_EXPOSE_NATIVE_GLX)
if (JK_USE_WAYLAND)
set(PRESENTATION_BACKEND wayland CACHE STRING " " FORCE)
set(BUILD_WITH_XLIB_HEADERS OFF CACHE BOOL " " FORCE)
set(BUILD_WITH_XCB_HEADERS OFF CACHE BOOL " " FORCE)
set(BUILD_WITH_WAYLAND_HEADERS ON CACHE BOOL " " FORCE)
list(APPEND NGP_DEFINITIONS -DGLFW_EXPOSE_NATIVE_WAYLAND -DXR_USE_PLATFORM_WAYLAND)
else()
set(PRESENTATION_BACKEND xlib CACHE STRING " " FORCE)
set(BUILD_WITH_XLIB_HEADERS ON CACHE BOOL " " FORCE)
set(BUILD_WITH_XCB_HEADERS OFF CACHE BOOL " " FORCE)
set(BUILD_WITH_WAYLAND_HEADERS OFF CACHE BOOL " " FORCE)
list(APPEND NGP_DEFINITIONS -DGLFW_EXPOSE_NATIVE_X11 -DXR_USE_PLATFORM_XLIB)
endif()
else()
message(FATAL_ERROR "No OpenXR platform set for this OS")
endif()
add_subdirectory(dependencies/OpenXR-SDK)
list(APPEND NGP_INCLUDE_DIRECTORIES "dependencies/OpenXR-SDK/include" "dependencies/OpenXR-SDK/src/common")
list(APPEND NGP_LIBRARIES openxr_loader)
list(APPEND GUI_SOURCES src/openxr_hmd.cu)
# OpenGL
find_package(OpenGL REQUIRED)
# GLFW
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL " " FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL " " FORCE)
set(GLFW_BUILD_DOCS OFF CACHE BOOL " " FORCE)
set(GLFW_BUILD_INSTALL OFF CACHE BOOL " " FORCE)
set(GLFW_INSTALL OFF CACHE BOOL " " FORCE)
set(GLFW_USE_CHDIR OFF CACHE BOOL " " FORCE)
set(GLFW_VULKAN_STATIC OFF CACHE BOOL " " FORCE)
set(BUILD_SHARED_LIBS ON CACHE BOOL " " FORCE)
add_subdirectory(dependencies/glfw)
set_target_properties(glfw PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1)
mark_as_advanced(
GLFW_BUILD_DOCS GLFW_BUILD_EXAMPLES GLFW_BUILD_INSTALL GLFW_BUILD_TESTS
GLFW_DOCUMENT_INTERNALS GLFW_INSTALL GLFW_USE_CHDIR GLFW_USE_MENUBAR
GLFW_USE_OSMESA GLFW_VULKAN_STATIC GLFW_USE_RETINA GLFW_USE_MIR
BUILD_SHARED_LIBS USE_MSVC_RUNTIME_LIBRARY_DLL
)
list(APPEND NGP_INCLUDE_DIRECTORIES "dependencies/glfw/include" "dependencies/imgui")
if (MSVC)
list(APPEND NGP_INCLUDE_DIRECTORIES "dependencies/gl3w")
list(APPEND GUI_SOURCES "dependencies/gl3w/GL/gl3w.c")
list(APPEND NGP_LIBRARIES opengl32 $<TARGET_OBJECTS:glfw_objects>)
else()
find_package(GLEW REQUIRED)
list(APPEND NGP_INCLUDE_DIRECTORIES ${GLEW_INCLUDE_DIRS})
list(APPEND NGP_LIBRARIES GL ${GLEW_LIBRARIES} $<TARGET_OBJECTS:glfw_objects>)
endif()
list(APPEND GUI_SOURCES
dependencies/imguizmo/ImGuizmo.cpp
dependencies/imgui/imgui.cpp
dependencies/imgui/backends/imgui_impl_glfw.cpp
dependencies/imgui/backends/imgui_impl_opengl3.cpp
dependencies/imgui/imgui_draw.cpp
dependencies/imgui/imgui_tables.cpp
dependencies/imgui/imgui_widgets.cpp
)
list(APPEND NGP_DEFINITIONS -DNGP_GUI)
endif(NGP_BUILD_WITH_GUI)
list(APPEND NGP_INCLUDE_DIRECTORIES
"dependencies"
"dependencies/filesystem"
"dependencies/nanovdb"
"dependencies/NaturalSort"
"dependencies/tinylogger"
)
find_package(OpenMP)
if (OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()
if (NGP_BUILD_WITH_OPTIX)
set(NGP_OPTIX ON)
list(APPEND NGP_INCLUDE_DIRECTORIES "dependencies/optix")
list(APPEND NGP_DEFINITIONS -DNGP_OPTIX)
endif()
if (NGP_BUILD_WITH_PYTHON_BINDINGS)
find_package(Python 3.7 COMPONENTS Interpreter Development)
if (Python_FOUND)
add_subdirectory("dependencies/pybind11")
endif()
endif()
# Compile zlib (only on Windows)
if (WIN32)
set(ZLIB_USE_STATIC_LIBS ON CACHE BOOL " " FORCE)
set(ZLIB_BUILD_STATIC_LIBS ON CACHE BOOL " " FORCE)
set(ZLIB_BUILD_SHARED_LIBS OFF CACHE BOOL " " FORCE)
set(SKIP_INSTALL_ALL ON CACHE BOOL " " FORCE)
add_subdirectory("dependencies/zlib")
set_property(TARGET zlibstatic PROPERTY FOLDER "dependencies")
set(ZLIB_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/dependencies/zlib" CACHE PATH " " FORCE)
set(ZLIB_LIBRARY zlibstatic)
include_directories(${ZLIB_INCLUDE_DIR} "${CMAKE_CURRENT_BINARY_DIR}/dependencies/zlib")
list(APPEND NGP_LIBRARIES zlibstatic)
endif()
add_subdirectory("dependencies/zstr")
list(APPEND NGP_LIBRARIES zstr::zstr)
###############################################################################
# Program
###############################################################################
list(APPEND NGP_DEFINITIONS -DNGP_VERSION="${NGP_VERSION}")
list(APPEND NGP_INCLUDE_DIRECTORIES "include")
if (NOT MSVC)
list(APPEND NGP_LIBRARIES ${CMAKE_DL_LIBS})
endif()
list(APPEND NGP_SOURCES
${GUI_SOURCES}
src/camera_path.cu
src/common_host.cu
src/marching_cubes.cu
src/nerf_loader.cu
src/render_buffer.cu
src/testbed.cu
src/testbed_image.cu
src/testbed_nerf.cu
src/testbed_sdf.cu
src/testbed_volume.cu
src/thread_pool.cpp
src/tinyexr_wrapper.cu
src/tinyobj_loader_wrapper.cu
src/triangle_bvh.cu
)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR})
get_filename_component(CUDA_COMPILER_BIN "${CMAKE_CUDA_COMPILER}" DIRECTORY)
get_filename_component(CUDA_DIR "${CUDA_COMPILER_BIN}" DIRECTORY)
set(CUDA_INCLUDE "${CUDA_DIR}/include")
if (NGP_OPTIX)
add_library(optix_program OBJECT
src/optix/pathescape.cu
src/optix/raystab.cu
src/optix/raytrace.cu
)
set_target_properties(optix_program PROPERTIES CUDA_PTX_COMPILATION ON CUDA_ARCHITECTURES OFF)
target_compile_definitions(optix_program PUBLIC ${NGP_DEFINITIONS} -DTCNN_MIN_GPU_ARCH=0)
target_compile_options(optix_program PUBLIC "--expt-relaxed-constexpr")
get_target_property(TCNN_INCLUDE_DIRECTORIES tiny-cuda-nn INCLUDE_DIRECTORIES)
target_include_directories(optix_program PUBLIC ${NGP_INCLUDE_DIRECTORIES} ${TCNN_INCLUDE_DIRECTORIES})
# OptiX programs will be compiled as PTX and packaged
# as headers to be included from the binary dir.
list(APPEND NGP_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}")
set(OPTIX_PTX_HEADER ${CMAKE_CURRENT_BINARY_DIR}/optix_ptx.h)
find_program(bin_to_c NAMES bin2c PATHS ${CUDA_COMPILER_BIN})
if (NOT bin_to_c)
message(FATAL_ERROR
"bin2c not found:\n"
" CMAKE_CUDA_COMPILER='${CMAKE_CUDA_COMPILER}'\n"
" CUDA_COMPILER_BIN='${CUDA_COMPILER_BIN}'\n"
)
endif()
add_custom_command(
OUTPUT "${OPTIX_PTX_HEADER}"
COMMAND ${CMAKE_COMMAND}
"-DBIN_TO_C_COMMAND=${bin_to_c}"
"-DOBJECTS=$<TARGET_OBJECTS:optix_program>"
"-DOUTPUT=${OPTIX_PTX_HEADER}"
-P ${PROJECT_SOURCE_DIR}/cmake/bin2c_wrapper.cmake
VERBATIM
DEPENDS optix_program
COMMENT "Converting PTX files to a C header"
)
list(APPEND NGP_SOURCES ${OPTIX_PTX_HEADER})
endif()
add_library(ngp STATIC ${NGP_SOURCES})
set_target_properties(ngp PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON CUDA_SEPARABLE_COMPILATION ON)
target_compile_definitions(ngp PUBLIC ${NGP_DEFINITIONS})
target_compile_options(ngp PUBLIC $<$<COMPILE_LANGUAGE:CUDA>:${CUDA_NVCC_FLAGS}>)
target_include_directories(ngp PUBLIC ${NGP_INCLUDE_DIRECTORIES})
target_link_directories(ngp PUBLIC ${NGP_LINK_DIRECTORIES})
target_link_libraries(ngp PUBLIC ${NGP_LIBRARIES} tiny-cuda-nn)
# Copy shared libraries to the binary directory as needed
if (NGP_VULKAN)
set(NGX_BUILD_DIR "$<IF:$<CONFIG:Debug>,dev,rel>")
if (MSVC)
set(NGX_SHARED_LIB "${CMAKE_CURRENT_SOURCE_DIR}/dependencies/dlss/lib/Windows_x86_64/${NGX_BUILD_DIR}/nvngx_dlss.dll")
else()
set(NGX_SHARED_LIB "${CMAKE_CURRENT_SOURCE_DIR}/dependencies/dlss/lib/Linux_x86_64/${NGX_BUILD_DIR}/libnvidia-ngx-dlss.so.*")
endif()
add_custom_command(TARGET ngp POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${NGX_SHARED_LIB}" "${CMAKE_CURRENT_BINARY_DIR}" COMMAND_EXPAND_LISTS)
endif()
if (MSVC)
file(GLOB CUDA_DLLS "${CUDA_COMPILER_BIN}/cudart64*.dll")
if (CUDA_DLLS)
add_custom_command(TARGET ngp POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CUDA_DLLS} "${CMAKE_CURRENT_BINARY_DIR}" COMMAND_EXPAND_LISTS)
endif()
endif()
if (NGP_BUILD_EXECUTABLE)
add_executable(instant-ngp src/main.cu)
target_link_libraries(instant-ngp PRIVATE ngp)
# Link the executable to the project directory and copy over DLLs such that instant-ngp can be invoked without going into the build folder.
set(NGP_BINARY_FILE "\"${CMAKE_CURRENT_SOURCE_DIR}/$<TARGET_FILE_NAME:instant-ngp>\"")
if (MSVC)
add_custom_command(TARGET instant-ngp POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:instant-ngp> ${CMAKE_CURRENT_SOURCE_DIR})
file(GLOB NGP_REQUIRED_DLLS "${CUDA_COMPILER_BIN}/cudart64*.dll")
if (NGP_VULKAN)
list(APPEND NGP_REQUIRED_DLLS "${NGX_SHARED_LIB}")
endif()
if (NGP_REQUIRED_DLLS)
add_custom_command(TARGET instant-ngp POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${NGP_REQUIRED_DLLS} ${CMAKE_CURRENT_SOURCE_DIR} COMMAND_EXPAND_LISTS)
endif()
else()
add_custom_command(TARGET instant-ngp POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $<TARGET_FILE:instant-ngp> "${NGP_BINARY_FILE}")
if (NGP_VULKAN)
add_custom_command(TARGET instant-ngp POST_BUILD COMMAND "ln" -s -f "${NGX_SHARED_LIB}" "${CMAKE_CURRENT_SOURCE_DIR}/")
endif()
endif()
endif(NGP_BUILD_EXECUTABLE)
if (Python_FOUND)
add_library(pyngp SHARED src/python_api.cu)
set_target_properties(pyngp PROPERTIES CXX_VISIBILITY_PRESET "hidden" CUDA_VISIBILITY_PRESET "hidden")
target_link_libraries(pyngp PRIVATE ngp PUBLIC ${PYTHON_LIBRARIES} pybind11::module)
target_compile_definitions(pyngp PUBLIC -DNGP_PYTHON)
pybind11_extension(pyngp)
endif()