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