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