cycles: Add an nvrtc based cubin cli compiler.
[blender.git] / intern / cycles / CMakeLists.txt
1 # Standalone or with Blender
2 if(NOT WITH_BLENDER AND WITH_CYCLES_STANDALONE)
3         set(CYCLES_INSTALL_PATH "")
4 else()
5         set(WITH_CYCLES_BLENDER ON)
6         set(CYCLES_INSTALL_PATH "scripts/addons/cycles")
7 endif()
8
9 # External Libraries
10
11 include(cmake/external_libs.cmake)
12
13 # Build Flags
14 # todo: this code could be refactored a bit to avoid duplication
15 # note: CXX_HAS_SSE is needed in case passing SSE flags fails altogether (gcc-arm)
16
17 if(WITH_CYCLES_NATIVE_ONLY)
18         set(CXX_HAS_SSE FALSE)
19         set(CXX_HAS_AVX FALSE)
20         set(CXX_HAS_AVX2 FALSE)
21         add_definitions(
22                 -DWITH_KERNEL_NATIVE
23         )
24
25         if(NOT MSVC)
26                 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
27                 set(CYCLES_KERNEL_FLAGS "-march=native")
28         endif()
29 elseif(NOT WITH_CPU_SSE)
30         set(CXX_HAS_SSE FALSE)
31         set(CXX_HAS_AVX FALSE)
32         set(CXX_HAS_AVX2 FALSE)
33 elseif(WIN32 AND MSVC)
34         set(CXX_HAS_SSE TRUE)
35         set(CXX_HAS_AVX TRUE)
36         set(CXX_HAS_AVX2 TRUE)
37
38         # /arch:AVX for VC2012 and above
39         if(NOT MSVC_VERSION LESS 1700)
40                 set(CYCLES_AVX_ARCH_FLAGS "/arch:AVX")
41                 set(CYCLES_AVX2_ARCH_FLAGS "/arch:AVX /arch:AVX2")
42         elseif(NOT CMAKE_CL_64)
43                 set(CYCLES_AVX_ARCH_FLAGS "/arch:SSE2")
44                 set(CYCLES_AVX2_ARCH_FLAGS "/arch:SSE2")
45         endif()
46
47         # Unlike GCC/clang we still use fast math, because there is no fine
48         # grained control and the speedup we get here is too big to ignore.
49         set(CYCLES_KERNEL_FLAGS "/fp:fast -D_CRT_SECURE_NO_WARNINGS /GS-")
50
51         # there is no /arch:SSE3, but intrinsics are available anyway
52         if(CMAKE_CL_64)
53                 set(CYCLES_SSE2_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS}")
54                 set(CYCLES_SSE3_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS}")
55                 set(CYCLES_SSE41_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS}")
56                 set(CYCLES_AVX_KERNEL_FLAGS "${CYCLES_AVX_ARCH_FLAGS} ${CYCLES_KERNEL_FLAGS}")
57                 set(CYCLES_AVX2_KERNEL_FLAGS "${CYCLES_AVX2_ARCH_FLAGS} ${CYCLES_KERNEL_FLAGS}")
58         else()
59                 set(CYCLES_SSE2_KERNEL_FLAGS "/arch:SSE2 ${CYCLES_KERNEL_FLAGS}")
60                 set(CYCLES_SSE3_KERNEL_FLAGS "/arch:SSE2 ${CYCLES_KERNEL_FLAGS}")
61                 set(CYCLES_SSE41_KERNEL_FLAGS "/arch:SSE2 ${CYCLES_KERNEL_FLAGS}")
62                 set(CYCLES_AVX_KERNEL_FLAGS "${CYCLES_AVX_ARCH_FLAGS} ${CYCLES_KERNEL_FLAGS}")
63                 set(CYCLES_AVX2_KERNEL_FLAGS "${CYCLES_AVX2_ARCH_FLAGS} ${CYCLES_KERNEL_FLAGS}")
64         endif()
65
66         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CYCLES_KERNEL_FLAGS}")
67         set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Ox")
68         set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /Ox")
69         set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /Ox")
70 elseif(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
71         check_cxx_compiler_flag(-msse CXX_HAS_SSE)
72         check_cxx_compiler_flag(-mavx CXX_HAS_AVX)
73         check_cxx_compiler_flag(-mavx2 CXX_HAS_AVX2)
74
75         # Assume no signal trapping for better code generation.
76         set(CYCLES_KERNEL_FLAGS "-fno-trapping-math")
77         # Avoid overhead of setting errno for NaNs.
78         set(CYCLES_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -fno-math-errno")
79         # Let compiler optimize 0.0 - x without worrying about signed zeros.
80         set(CYCLES_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -fno-signed-zeros")
81
82         if(CMAKE_COMPILER_IS_GNUCC)
83                 # Assume no signal trapping for better code generation.
84                 set(CYCLES_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -fno-signaling-nans")
85                 # Assume a fixed rounding mode for better constant folding.
86                 set(CYCLES_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -fno-rounding-math")
87         endif()
88
89         if(CXX_HAS_SSE)
90                 if(CMAKE_COMPILER_IS_GNUCC)
91                         set(CYCLES_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -mfpmath=sse")
92                 endif()
93
94                 set(CYCLES_SSE2_KERNEL_FLAGS "${CYCLES_KERNEL_FLAGS} -msse -msse2")
95                 set(CYCLES_SSE3_KERNEL_FLAGS "${CYCLES_SSE2_KERNEL_FLAGS} -msse3 -mssse3")
96                 set(CYCLES_SSE41_KERNEL_FLAGS "${CYCLES_SSE3_KERNEL_FLAGS} -msse4.1")
97                 if(CXX_HAS_AVX)
98                         set(CYCLES_AVX_KERNEL_FLAGS "${CYCLES_SSE41_KERNEL_FLAGS} -mavx")
99                 endif()
100                 if(CXX_HAS_AVX2)
101                         set(CYCLES_AVX2_KERNEL_FLAGS "${CYCLES_SSE41_KERNEL_FLAGS} -mavx -mavx2 -mfma -mlzcnt -mbmi -mbmi2 -mf16c")
102                 endif()
103         endif()
104
105         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CYCLES_KERNEL_FLAGS}")
106 endif()
107
108 if(CXX_HAS_SSE)
109         add_definitions(
110                 -DWITH_KERNEL_SSE2
111                 -DWITH_KERNEL_SSE3
112                 -DWITH_KERNEL_SSE41
113         )
114 endif()
115
116 if(CXX_HAS_AVX)
117         add_definitions(-DWITH_KERNEL_AVX)
118 endif()
119
120 if(CXX_HAS_AVX2)
121         add_definitions(-DWITH_KERNEL_AVX2)
122 endif()
123
124 if(WITH_CYCLES_OSL)
125         if(WIN32 AND MSVC)
126                 set(RTTI_DISABLE_FLAGS "/GR- -DBOOST_NO_RTTI -DBOOST_NO_TYPEID")
127         elseif(CMAKE_COMPILER_IS_GNUCC OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
128                 set(RTTI_DISABLE_FLAGS "-fno-rtti -DBOOST_NO_RTTI -DBOOST_NO_TYPEID")
129         endif()
130 endif()
131
132 # Definitions and Includes
133
134 add_definitions(
135         ${BOOST_DEFINITIONS}
136         ${OPENIMAGEIO_DEFINITIONS}
137 )
138
139 add_definitions(
140         -DCCL_NAMESPACE_BEGIN=namespace\ ccl\ {
141         -DCCL_NAMESPACE_END=}
142 )
143
144 if(WITH_CYCLES_STANDALONE_GUI)
145         add_definitions(-DWITH_CYCLES_STANDALONE_GUI)
146 endif()
147
148 if(WITH_CYCLES_PTEX)
149         add_definitions(-DWITH_PTEX)
150 endif()
151
152 if(WITH_CYCLES_OSL)
153         add_definitions(-DWITH_OSL)
154         add_definitions(-DOSL_STATIC_LIBRARY)
155         include_directories(
156                 SYSTEM
157                 ${OSL_INCLUDE_DIR}
158         )
159 endif()
160
161 if(WITH_CYCLES_OPENSUBDIV)
162         add_definitions(-DWITH_OPENSUBDIV)
163         include_directories(
164                 SYSTEM
165                 ${OPENSUBDIV_INCLUDE_DIR}
166         )
167 endif()
168
169 set(WITH_CYCLES_DEVICE_OPENCL TRUE)
170 set(WITH_CYCLES_DEVICE_CUDA TRUE)
171 set(WITH_CYCLES_DEVICE_MULTI TRUE)
172
173 if(CYCLES_STANDALONE_REPOSITORY)
174         TEST_UNORDERED_MAP_SUPPORT()
175 endif()
176 if(WITH_CXX11)
177         add_definitions(-DCYCLES_STD_UNORDERED_MAP)
178 elseif(HAVE_STD_UNORDERED_MAP_HEADER)
179         if(HAVE_UNORDERED_MAP_IN_STD_NAMESPACE)
180                 add_definitions(-DCYCLES_STD_UNORDERED_MAP)
181         else()
182                 if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
183                         add_definitions(-DCYCLES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE)
184                 else()
185                         add_definitions(-DCYCLES_NO_UNORDERED_MAP)
186                         message(STATUS "Replacing unordered_map/set with map/set (warning: slower!)")
187                 endif()
188         endif()
189 else()
190         if(HAVE_UNORDERED_MAP_IN_TR1_NAMESPACE)
191                 add_definitions(-DCYCLES_TR1_UNORDERED_MAP)
192         else()
193                 add_definitions(-DCYCLES_NO_UNORDERED_MAP)
194                 message(STATUS "Replacing unordered_map/set with map/set (warning: slower!)")
195         endif()
196 endif()
197
198 # Logging capabilities using GLog library.
199 if(WITH_CYCLES_LOGGING)
200         add_definitions(-DWITH_CYCLES_LOGGING)
201         add_definitions(${GLOG_DEFINES})
202         add_definitions(-DCYCLES_GFLAGS_NAMESPACE=${GFLAGS_NAMESPACE})
203         include_directories(
204                 SYSTEM
205                 ${GLOG_INCLUDE_DIRS}
206                 ${GFLAGS_INCLUDE_DIRS}
207         )
208 endif()
209
210 # Debugging capabilities (debug passes etc).
211 if(WITH_CYCLES_DEBUG)
212         add_definitions(-DWITH_CYCLES_DEBUG)
213 endif()
214
215 if(NOT OPENIMAGEIO_PUGIXML_FOUND)
216         add_definitions(-DWITH_SYSTEM_PUGIXML)
217 endif()
218
219 include_directories(
220         SYSTEM
221         ${BOOST_INCLUDE_DIR}
222         ${OPENIMAGEIO_INCLUDE_DIRS}
223         ${OPENIMAGEIO_INCLUDE_DIRS}/OpenImageIO
224         ${OPENEXR_INCLUDE_DIR}
225         ${OPENEXR_INCLUDE_DIRS}
226         ${PUGIXML_INCLUDE_DIR}
227 )
228
229 if(CYCLES_STANDALONE_REPOSITORY)
230         include_directories(../third_party/atomic)
231 else()
232         include_directories(../atomic)
233 endif()
234
235 # Warnings
236 if(CMAKE_COMPILER_IS_GNUCXX)
237         ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS _has_cxxflag_float_conversion "-Werror=float-conversion")
238         ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS _has_cxxflag_double_promotion "-Werror=double-promotion")
239         ADD_CHECK_CXX_COMPILER_FLAG(CMAKE_CXX_FLAGS _has_no_error_unused_macros "-Wno-error=unused-macros")
240         unset(_has_cxxflag_float_conversion)
241         unset(_has_cxxflag_double_promotion)
242         unset(_has_no_error_unused_macros)
243 endif()
244
245 if(WITH_CYCLES_CUDA_BINARIES AND (NOT WITH_CYCLES_CUBIN_COMPILER))
246         if(MSVC)
247                 set(MAX_MSVC 1800)
248                 if(${CUDA_VERSION} EQUAL "8.0")
249                         set(MAX_MSVC 1900)
250                 elseif(${CUDA_VERSION} EQUAL "9.0")
251                         set(MAX_MSVC 1910)
252                 elseif(${CUDA_VERSION} EQUAL "9.1")
253                         set(MAX_MSVC 1911)
254                 endif()
255                 if (NOT MSVC_VERSION LESS ${MAX_MSVC})
256                         message(STATUS "nvcc not supported for this compiler version, using cycles_cubin_cc instead.")
257                         set(WITH_CYCLES_CUBIN_COMPILER ON)
258                 endif()
259                 unset(MAX_MSVC)
260         endif()
261 endif()
262
263
264 # Subdirectories
265
266 if(WITH_CYCLES_BLENDER)
267         add_definitions(-DWITH_BLENDER_GUARDEDALLOC)
268         add_subdirectory(blender)
269 endif()
270
271 if(WITH_CYCLES_NETWORK)
272         add_definitions(-DWITH_NETWORK)
273 endif()
274
275 if(WITH_CYCLES_STANDALONE OR WITH_CYCLES_NETWORK OR WITH_CYCLES_CUBIN_COMPILER)
276         add_subdirectory(app)
277 endif()
278
279 add_subdirectory(bvh)
280 add_subdirectory(device)
281 add_subdirectory(doc)
282 add_subdirectory(graph)
283 add_subdirectory(kernel)
284 add_subdirectory(render)
285 add_subdirectory(subd)
286 add_subdirectory(util)
287
288 # TODO(sergey): Make this to work with standalone repository.
289 if(WITH_GTESTS)
290         add_subdirectory(test)
291 endif()
292
293 if(NOT WITH_BLENDER AND WITH_CYCLES_STANDALONE)
294         delayed_do_install(${CMAKE_BINARY_DIR}/bin)
295 endif()
296