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