Blender V5.3
creator.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10#include <cstring>
11
12#ifdef WIN32
13# ifdef WIN32_LEAN_AND_MEAN
14# undef WIN32_LEAN_AND_MEAN
15# endif
16# include "utfconv.hh"
17# include <windows.h>
18# ifdef WITH_CPU_CHECK
19# pragma comment(linker, "/include:cpu_check_win32")
20# endif
21#endif
22
23#if defined(WITH_TBB_MALLOC) && defined(_MSC_VER) && defined(NDEBUG)
24# pragma comment(lib, "tbbmalloc_proxy.lib")
25# pragma comment(linker, "/include:__TBB_malloc_proxy")
26#endif
27
28#include "MEM_guardedalloc.h"
29
30#include "CLG_log.h"
31
32#include "DNA_genfile.h"
33
34#include "BLI_endian_defines.hh"
35#include "BLI_fftw.hh"
36#include "BLI_path_utils.hh"
37#include "BLI_string.hh"
38#include "BLI_system.hh"
39#include "BLI_task_c.hh"
40#include "BLI_threads.hh"
41#include "BLI_utildefines.hh"
42
43/* Mostly initialization functions. */
44#include "BKE_appdir.hh"
45#include "BKE_blender.hh"
46#include "BKE_brush.hh"
47#include "BKE_callbacks.hh"
48#include "BKE_context.hh"
49#include "BKE_cpp_types.hh"
50#include "BKE_global.hh"
51#include "BKE_idtype.hh"
52#include "BKE_material.hh"
53#include "BKE_modifier.hh"
54#include "BKE_node.hh"
55#include "BKE_particle.h"
56#include "BKE_shader_fx.hh"
57#include "BKE_sound.hh"
58#include "BKE_vfont.hh"
59#include "BKE_volume.hh"
60
61#ifndef WITH_PYTHON_MODULE
62# include "BLI_args.hh"
63#endif
64
65#include "DEG_depsgraph.hh"
66
67#include "IMB_imbuf.hh" /* For #IMB_init. */
68
69#include "MOV_util.hh"
70
71#include "RE_engine.h"
72#include "RE_texture.h"
73
74#include "ED_datafiles.h"
75
76#include "SEQ_modifier.hh"
77
78#include "COM_init.hh"
79
80#include "WM_api.hh"
81
82#include "RNA_define.hh"
83
84#include "FN_init.hh"
85
86#ifdef WITH_OPENGL_BACKEND
88#endif
89
90#ifdef WITH_FREESTYLE
91# include "FRS_freestyle.h"
92#endif
93
94#include <csignal>
95
96#ifdef __FreeBSD__
97# include <floatingpoint.h>
98#endif
99
100#ifdef WITH_BINRELOC
101# include "binreloc.h"
102#endif
103
104#ifdef WITH_LIBMV
105# include "libmv-capi.h"
106#endif
107
108#ifdef WITH_CYCLES
109# include "CCL_api.h"
110#endif
111
112#if defined(WITH_PYTHON_MODULE) && defined(__APPLE__)
113/* Environment is not available in macOS shared libraries. */
114# include <crt_externs.h>
115char **environ = nullptr;
116#endif
117
118#if defined(WITH_TBB_MALLOC) && defined(__linux__)
119# include <tbb/scalable_allocator.h>
120#endif
121
122#include "creator_intern.h" /* Own include. */
123
124BLI_STATIC_ASSERT(ENDIAN_ORDER == L_ENDIAN, "Blender only builds on little endian systems")
125
126/* -------------------------------------------------------------------- */
129
130#if (defined(WITH_TBB_MALLOC) && defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_GMP)) || \
131 defined(DOXYGEN)
132# include "gmp.h"
133# include "tbb/scalable_allocator.h"
134
135void *gmp_alloc(size_t size)
136{
137 return scalable_malloc(size);
138}
139void *gmp_realloc(void *ptr, size_t /*old_size*/, size_t new_size)
140{
141 return scalable_realloc(ptr, new_size);
142}
143
144void gmp_free(void *ptr, size_t /*size*/)
145{
146 scalable_free(ptr);
147}
148
155{
156 mp_set_memory_functions(gmp_alloc, gmp_realloc, gmp_free);
157}
158#endif
159
161
162/* -------------------------------------------------------------------- */
165
166/* When building as a Python module, don't use special argument handling
167 * so the module loading logic can control the `argv` & `argc`. */
168#if defined(WIN32) && !defined(WITH_PYTHON_MODULE)
169# define USE_WIN32_UNICODE_ARGS
170#endif
171
173
174namespace blender {
175
176/* -------------------------------------------------------------------- */
179
180/* Written to by `creator_args.cc`. */
183 app_state.signal.use_crash_handler = true;
184 app_state.signal.use_console_crash_handler = false;
185 app_state.signal.use_abort_handler = true;
186 app_state.exit_code_on_error.python = 0;
188 return app_state;
189}();
190
192
193/* -------------------------------------------------------------------- */
198
199static void callback_mem_error(const char *errorStr)
200{
201 fputs(errorStr, stderr);
202 fflush(stderr);
203}
204
206{
207 /* Error output from the guarded allocation routines. */
209}
210
215
218#ifndef WITH_PYTHON_MODULE
220#endif
221
222#ifdef USE_WIN32_UNICODE_ARGS
223 char **argv;
224 int argv_num;
225#endif
226
232};
233
234static void callback_main_atexit(void *user_data)
235{
236 CreatorAtExitData *app_init_data = static_cast<CreatorAtExitData *>(user_data);
237
238#ifndef WITH_PYTHON_MODULE
239 if (app_init_data->ba) {
240 BLI_args_destroy(app_init_data->ba);
241 app_init_data->ba = nullptr;
242 }
243#endif
244
245#ifdef USE_WIN32_UNICODE_ARGS
246 if (app_init_data->argv) {
247 while (app_init_data->argv_num) {
248 free((void *)app_init_data->argv[--app_init_data->argv_num]);
249 }
250 free((void *)app_init_data->argv);
251 app_init_data->argv = nullptr;
252 }
253#endif
254
255 if (CreatorAtExitData_EarlyExit *early_exit = app_init_data->early_exit) {
256 CTX_free(early_exit->C);
257
259
262
263 CLG_exit();
264 }
265}
266
267static void callback_clg_fatal(void *fp)
268{
269 BLI_system_backtrace(static_cast<FILE *>(fp));
270}
271
273
274/* -------------------------------------------------------------------- */
277
279{
280 /* LD_PRELOAD may have been modified on startup for Blender. However
281 * we don't want it for other executables launched from Blender. */
282 const char *restore_ld_preload = BLI_getenv("BLENDER_RESTORE_LD_PRELOAD");
283 if (restore_ld_preload) {
284 BLI_setenv("LD_PRELOAD", restore_ld_preload);
285 }
286}
287
289
290/* -------------------------------------------------------------------- */
296
297#ifdef WITH_PYTHON_MODULE
298static void *main_python_evil_C = nullptr;
299
300/* Called in `bpy_interface.cc` when building as a Python module. */
301int main_python_enter(int argc, const char **argv);
302
303void main_python_exit()
304{
305 WM_exit_ex((bContext *)main_python_evil_C, true, false);
306 main_python_evil_C = nullptr;
307}
308
309/* Rename the `main(..)` function, allowing Python initialization to call it. */
310# define main blender::main_python_enter
311#endif /* WITH_PYTHON_MODULE */
312
314
315} // namespace blender
316
317/* -------------------------------------------------------------------- */
320
321#if defined(__APPLE__)
322extern "C" int GHOST_HACK_getFirstFile(char buf[]);
323#endif
324
332int main(int argc,
333#ifdef USE_WIN32_UNICODE_ARGS
334 const char ** /*argv_c*/
335#else
336 const char **argv
337#endif
338)
339{
340 using namespace blender;
341
342 bContext *C;
343#ifndef WITH_PYTHON_MODULE
344 bArgs *ba;
345#endif
346
347 /* Ensure we free data on early-exit. */
348 CreatorAtExitData app_init_data = {nullptr};
350
351 CreatorAtExitData_EarlyExit app_init_data_early_exit = {nullptr};
352 app_init_data.early_exit = &app_init_data_early_exit;
353
354/* Un-buffered `stdout` makes `stdout` and `stderr` better synchronized, and helps
355 * when stepping through code in a debugger (prints are immediately
356 * visible). However disabling buffering causes lock contention on windows
357 * see #76767 for details, since this is a debugging aid, we do not enable
358 * the un-buffered behavior for release builds. */
359#ifndef NDEBUG
360 setvbuf(stdout, nullptr, _IONBF, 0);
361#endif
362
364
365 /* Use the v2 Level Zero adapter of the SYCL unified runtime. As a fix for #159584, the v1 Level
366 * Zero adapter is not included. While the Cycles oneAPI device sets this as well, we also need
367 * the environment variable for the use of Open Image Denoise in the compositor. */
368 BLI_setenv_if_new("SYCL_UR_USE_LEVEL_ZERO_V2", "1");
369
370#ifdef WIN32
371# ifdef USE_WIN32_UNICODE_ARGS
372 /* Win32 Unicode Arguments. */
373 {
374 /* NOTE: Can't use `guardedalloc` allocation here, as it's not yet initialized
375 * (it depends on the arguments passed in, which is what we're getting here!). */
376 wchar_t **argv_16 = CommandLineToArgvW(GetCommandLineW(), &argc);
377 app_init_data.argv = static_cast<char **>(malloc(argc * sizeof(char *)));
378 for (int i = 0; i < argc; i++) {
379 app_init_data.argv[i] = alloc_utf_8_from_16(argv_16[i], 0);
380 }
381 LocalFree(argv_16);
382
383 /* Free on early-exit. */
384 app_init_data.argv_num = argc;
385 }
386 const char **argv = const_cast<const char **>(app_init_data.argv);
387# endif /* USE_WIN32_UNICODE_ARGS */
388#endif /* WIN32 */
389
390#if defined(WITH_OPENGL_BACKEND) && BLI_SUBPROCESS_SUPPORT
391 if (STREQ(argv[0], "--compilation-subprocess")) {
392 BLI_assert(argc == 2);
393 GPU_compilation_subprocess_run(argv[1]);
394 return 0;
395 }
396#endif
397
398#if defined(WITH_TBB_MALLOC) && defined(__linux__)
399 /* Enable huge pages for performance. */
400 scalable_allocation_mode(TBBMALLOC_USE_HUGE_PAGES, 1);
401#endif
402
403 /* NOTE: Special exception for guarded allocator type switch:
404 * we need to perform switch from lock-free to fully
405 * guarded allocator before any allocation happened.
406 */
407 {
408 int i;
409 for (i = 0; i < argc; i++) {
410 if (STR_ELEM(argv[i], "-d", "--debug", "--debug-memory", "--debug-all")) {
411 printf("Switching to fully guarded memory allocator.\n");
413 break;
414 }
415 if (STR_ELEM(argv[i], "--", "-c", "--command")) {
416 break;
417 }
418 }
420 }
421
422#ifdef BUILD_DATE
423 {
424 const time_t temp_time = build_commit_timestamp;
425 const tm *tm = gmtime(&temp_time);
426 if (tm) [[likely]] {
427 strftime(build_commit_date, sizeof(build_commit_date), "%Y-%m-%d", tm);
428 strftime(build_commit_time, sizeof(build_commit_time), "%H:%M", tm);
429 }
430 else {
431 const char *unknown = "date-unknown";
432 STRNCPY(build_commit_date, unknown);
433 STRNCPY(build_commit_time, unknown);
434 }
435 }
436#endif
437
438 /* Initialize logging. */
439 CLG_init();
445
446 C = CTX_create();
447
448 app_init_data_early_exit.C = C;
449
450#ifdef WITH_PYTHON_MODULE
451# ifdef __APPLE__
452 environ = *_NSGetEnviron();
453# endif
454
455# undef main
456 main_python_evil_C = C;
457#endif
458
459#ifdef WITH_BINRELOC
460 br_init(nullptr);
461#endif
462
463#ifdef WITH_LIBMV
464 libmv_initLogging(argv[0]);
465#endif
466
467#if defined(WITH_TBB_MALLOC) && defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_GMP)
469#endif
470
472
473#if defined(__APPLE__) && !defined(WITH_PYTHON_MODULE) && !defined(WITH_HEADLESS)
474 /* Patch to ignore argument finder gives us (PID?). */
475 if (argc == 2 && STRPREFIX(argv[1], "-psn_")) {
476 static char firstfilebuf[512];
477
478 argc = 1;
479
480 if (GHOST_HACK_getFirstFile(firstfilebuf)) {
481 argc = 2;
482 argv[1] = firstfilebuf;
483 }
484 }
485#endif
486
487#ifdef __FreeBSD__
488 fpsetmask(0);
489#endif
490
491 /* Initialize path to executable. */
493
495
496 BKE_blender_globals_init(); /* `blender.cc` */
497
507
509
510/* First test for background-mode (#Global.background). */
511#ifndef WITH_PYTHON_MODULE
512 ba = BLI_args_create(argc, argv); /* Skip binary path. */
513
514 /* Ensure we free on early exit. */
515 app_init_data.ba = ba;
516
517 main_args_setup(C, ba, false);
518
519 /* Parse environment handling arguments. */
520 BLI_args_parse(ba, ARG_PASS_ENVIRONMENT, nullptr, nullptr);
521
522#else
523 /* Using preferences or user startup makes no sense for #WITH_PYTHON_MODULE. */
524 G.factory_startup = true;
525#endif
526
527 /* After parsing #ARG_PASS_ENVIRONMENT such as `--env-*`,
528 * since they impact `BKE_appdir` behavior. */
530
531 /* After parsing number of threads argument.
532 *
533 * Denormal handling is not enabled for the Python module because just writing `import bpy`
534 * should not change the result of unrelated computations. */
535#ifdef WITH_PYTHON_MODULE
536 const bool use_flush_denormals_to_zero = false;
537#else
538 const bool use_flush_denormals_to_zero = true;
539#endif
540 BLI_task_scheduler_init(use_flush_denormals_to_zero);
541
542 /* Initialize FFTW threading support. */
544
545#ifndef WITH_PYTHON_MODULE
546 /* The settings pass includes:
547 * - Background-mode assignment (#Global.background), checked by other subsystems
548 * which may be skipped in background mode.
549 * - The animation player may be launched which takes over argument passing,
550 * initializes the sub-systems it needs which have not yet been started.
551 * The animation player will call `exit(..)` too, so code after this call
552 * never runs when it's invoked.
553 * - All the `--debug-*` flags.
554 */
555 BLI_args_parse(ba, ARG_PASS_SETTINGS, nullptr, nullptr);
556
558#endif
559
560 /* Continue with regular initialization, no need to use "early" exit. */
561 app_init_data.early_exit = nullptr;
562
563#ifdef WITH_CYCLES
564 CCL_log_init();
566#endif
567
568 /* Set max open files to better handle production files that may use many
569 * open geometry or texture cache file handles. After logging since it's used .*/
571
572 /* Must be initialized after #BKE_appdir_init to account for color-management paths. */
573 IMB_init();
574 /* Keep after #ARG_PASS_SETTINGS since debug flags are checked. */
575 MOV_init();
576
577 /* After #ARG_PASS_SETTINGS arguments, this is so #WM_main_playanim skips #RNA_init. */
578 RNA_init();
579
583
586 /* End second initialization. */
587
588#if defined(WITH_PYTHON_MODULE) || defined(WITH_HEADLESS)
589 /* Python module mode ALWAYS runs in background-mode (for now). */
590 G.background = true;
591 /* Manually using `--background` also forces the audio device. */
593#else
594 if (G.background) {
596 }
597#endif
598
599 /* Background render uses this font too. */
601
602 /* Initialize FFMPEG if built in, also needed for background-mode if videos are
603 * rendered via FFMPEG. */
605
607
608#ifndef WITH_PYTHON_MODULE
609 if (G.background == 0) {
610 BLI_args_parse(ba, ARG_PASS_SETTINGS_GUI, nullptr, nullptr);
611 }
612 BLI_args_parse(ba, ARG_PASS_SETTINGS_FORCE, nullptr, nullptr);
613#endif
614
615 WM_init(C, argc, argv);
616
617#ifndef WITH_PYTHON
618 fprintf(stderr,
619 "\n"
620 "WARNING: Blender compiled without Python!\n"
621 "This is not intended for typical usage.\n"
622 "\n");
623#endif
624
625#ifdef WITH_FREESTYLE
626 /* Initialize Freestyle. */
627 FRS_init();
629#endif
630
631/* OK we are ready for it. */
632#ifndef WITH_PYTHON_MODULE
633 /* Handles #ARG_PASS_FINAL. */
635#endif
636
637 /* Explicitly free data allocated for argument parsing:
638 * - `ba`
639 * - `argv` on WIN32.
640 */
641 callback_main_atexit(&app_init_data);
643
644/* Paranoid, avoid accidental re-use. */
645#ifndef WITH_PYTHON_MODULE
646 ba = nullptr;
647 (void)ba;
648#endif
649
650#ifdef USE_WIN32_UNICODE_ARGS
651 argv = nullptr;
652 (void)argv;
653#endif
654
655#ifndef WITH_PYTHON_MODULE
656 if (G.background) {
657 int exit_code;
658 if (app_state.main_arg_deferred != nullptr) {
659 exit_code = main_arg_deferred_handle();
661 }
662 else {
663 exit_code = G.is_break ? EXIT_FAILURE : EXIT_SUCCESS;
664 }
665 /* Using window-manager API in background-mode is a bit odd, but works fine. */
666 WM_exit(C, exit_code);
667 }
668 else {
669 /* Not supported, although it could be made to work if needed. */
671
672 /* Shows the splash as needed. */
674
675 WM_main(C);
676 }
677 /* Neither #WM_exit, #WM_main return, this quiets CLANG's `unreachable-code-return` warning. */
679
680#endif /* !WITH_PYTHON_MODULE */
681
682 return 0;
683
684} /* End of `int main(...)` function. */
685
Blender util stuff.
General operations, lookup, etc. for materials.
Volume data-block.
A general argument parsing module.
#define L_ENDIAN
#define ENDIAN_ORDER
#define STR_ELEM(...)
void CLG_output_use_basename_set(int value)
Definition clog.cc:892
void CLG_exit()
Definition clog.cc:877
void CLG_output_use_memory_set(int value)
Definition clog.cc:902
void CLG_output_use_source_set(int value)
Definition clog.cc:887
void CLG_fatal_fn_set(void(*fatal_fn)(void *file_handle))
Definition clog.cc:912
void CLG_output_use_timestamp_set(int value)
Definition clog.cc:897
void CLG_init()
Definition clog.cc:870
blenloader genfile private function prototypes
const char datatoc_bfont_pfb[]
int datatoc_bfont_pfb_size
int GHOST_HACK_getFirstFile(char buf[FIRSTFILEBUFLG])
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define STRPREFIX(a, b)
#define BLI_assert_unreachable()
#define BLI_STATIC_ASSERT(a, msg)
#define BLI_assert(a)
#define STREQ(a, b)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
char build_commit_date[]
Definition buildinfo.c:36
ulong build_commit_timestamp
Definition buildinfo.c:35
char build_commit_time[]
Definition buildinfo.c:37
void * gmp_realloc(void *ptr, size_t, size_t new_size)
Definition creator.cc:139
void * gmp_alloc(size_t size)
Definition creator.cc:135
void gmp_free(void *ptr, size_t)
Definition creator.cc:144
void gmp_blender_init_allocator()
Definition creator.cc:154
#define printf
#define main()
void MEM_init_memleak_detection()
void libmv_initLogging(const char *argv0)
Definition logging.cc:21
void MEM_use_guarded_allocator()
Definition mallocn.cc:200
#define G(x, y, z)
void free(Subdiv *subdiv)
Definition subdiv.cc:192
void node_system_init()
Definition node.cc:6178
void initialize_float()
Definition fftw.cc:88
BMesh Iterators.
void BKE_sound_force_device(const char *device)
void BLI_args_destroy(struct bArgs *ba)
Definition BLI_args.cc:147
void DEG_free_node_types()
int main_arg_deferred_handle()
char * STRNCPY(char(&dst)[N], const char *src)
void BKE_brush_system_init()
Definition brush.cc:625
void BKE_blender_globals_init()
Definition blender.cc:194
void RE_texture_rng_init()
void MOV_init()
void WM_main(bContext *C)
Definition wm.cc:627
void WM_exit_ex(bContext *C, const bool do_python_exit, const bool do_user_exit_actions)
void WM_exit(bContext *C, const int exit_code)
void main_signal_setup(void)
void BKE_blender_atexit_unregister(void(*func)(void *user_data), const void *user_data)
Definition blender.cc:493
void WM_init(bContext *C, int argc, const char **argv)
void BKE_appdir_init()
Definition appdir.cc:95
void WM_init_splash_on_startup(bContext *C)
int main_args_handle_load_file(int, const char **argv, void *data)
void BLI_system_max_open_files_ensure()
Definition system.cc:225
void BLI_setenv(const char *env, const char *val) ATTR_NONNULL(1)
void RNA_init()
Definition rna_access.cc:88
void BLI_threadapi_init()
Definition threads.cc:116
bContext * CTX_create()
void BKE_shaderfx_init()
Definition shader_fx.cc:50
static void callback_main_atexit(void *user_data)
Definition creator.cc:234
ApplicationState app_state
Definition creator.cc:181
void FRS_set_context(struct bContext *C)
void BKE_idtype_init()
Definition idtype.cc:207
void main_signal_setup_background(void)
static void callback_clg_fatal(void *fp)
Definition creator.cc:267
static void main_callback_setup()
Definition creator.cc:205
static void restore_ld_preload()
Definition creator.cc:278
void FRS_init(void)
void main_arg_deferred_free()
void RE_engines_init()
void IMB_init()
Definition module.cc:26
void CTX_free(bContext *C)
static void callback_mem_error(const char *errorStr)
Definition creator.cc:199
void main_args_setup(bContext *C, bArgs *ba, bool all)
void(*) MEM_set_error_callback(void(*func)(const char *))
void BLI_setenv_if_new(const char *env, const char *val) ATTR_NONNULL(1)
void BKE_appdir_program_path_init(const char *argv0) ATTR_NONNULL(1)
Definition appdir.cc:978
void BLI_system_backtrace(FILE *fp)
Definition system.cc:112
void BKE_vfont_builtin_register(const void *mem, int size)
Definition vfont.cc:283
void BKE_blender_globals_clear()
Definition blender.cc:215
void DEG_register_node_types()
void BLI_task_scheduler_init(bool use_flush_denormals_to_zero=true)
void BKE_particle_init_rng()
Definition particle.cc:449
void BKE_appdir_exit()
Definition appdir.cc:103
struct bArgs * BLI_args_create(int argc, const char **argv)
Definition BLI_args.cc:127
void BKE_callback_global_init()
Definition callbacks.cc:115
const char * BLI_getenv(const char *env) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
@ ARG_PASS_ENVIRONMENT
@ ARG_PASS_SETTINGS
@ ARG_PASS_SETTINGS_GUI
@ ARG_PASS_SETTINGS_FORCE
void CCL_log_init()
Definition logging.cpp:14
void BKE_blender_atexit_register(void(*func)(void *user_data), void *user_data)
Definition blender.cc:484
void BLI_args_parse(struct bArgs *ba, int pass, BA_ArgCallback default_cb, void *default_data)
Definition BLI_args.cc:339
struct BA_ArgCallback_Deferred * main_arg_deferred
struct blender::ApplicationState::@204252132227047363156276311014221143207211224327 exit_code_on_error
struct blender::ApplicationState::@015013240256342036154333030232065173037361052222 signal
CreatorAtExitData_EarlyExit * early_exit
Definition creator.cc:231
char * alloc_utf_8_from_16(const wchar_t *in16, size_t add)
Definition utfconv.cc:282