App.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Core/App.h>
  6. #include <AnKi/Core/ConfigSet.h>
  7. #include <AnKi/Util/Logger.h>
  8. #include <AnKi/Util/File.h>
  9. #include <AnKi/Util/Filesystem.h>
  10. #include <AnKi/Util/System.h>
  11. #include <AnKi/Util/ThreadHive.h>
  12. #include <AnKi/Util/Tracer.h>
  13. #include <AnKi/Util/HighRezTimer.h>
  14. #include <AnKi/Core/CoreTracer.h>
  15. #include <AnKi/Core/DeveloperConsole.h>
  16. #include <AnKi/Core/StatsUi.h>
  17. #include <AnKi/Core/NativeWindow.h>
  18. #include <AnKi/Core/MaliHwCounters.h>
  19. #include <AnKi/Input/Input.h>
  20. #include <AnKi/Scene/SceneGraph.h>
  21. #include <AnKi/Renderer/RenderQueue.h>
  22. #include <AnKi/Resource/ResourceManager.h>
  23. #include <AnKi/Physics/PhysicsWorld.h>
  24. #include <AnKi/Renderer/MainRenderer.h>
  25. #include <AnKi/Script/ScriptManager.h>
  26. #include <AnKi/Resource/ResourceFilesystem.h>
  27. #include <AnKi/Resource/AsyncLoader.h>
  28. #include <AnKi/Core/GpuMemoryPools.h>
  29. #include <AnKi/Ui/UiManager.h>
  30. #include <AnKi/Ui/Canvas.h>
  31. #include <csignal>
  32. #if ANKI_OS_ANDROID
  33. # include <android_native_app_glue.h>
  34. #endif
  35. namespace anki {
  36. #if ANKI_OS_ANDROID
  37. /// The one and only android hack
  38. android_app* g_androidApp = nullptr;
  39. #endif
  40. void* App::MemStats::allocCallback(void* userData, void* ptr, PtrSize size, PtrSize alignment)
  41. {
  42. ANKI_ASSERT(userData);
  43. static const PtrSize MAX_ALIGNMENT = 64;
  44. struct alignas(MAX_ALIGNMENT) Header
  45. {
  46. PtrSize m_allocatedSize;
  47. Array<U8, MAX_ALIGNMENT - sizeof(PtrSize)> _m_padding;
  48. };
  49. static_assert(sizeof(Header) == MAX_ALIGNMENT, "See file");
  50. static_assert(alignof(Header) == MAX_ALIGNMENT, "See file");
  51. void* out = nullptr;
  52. if(ptr == nullptr)
  53. {
  54. // Need to allocate
  55. ANKI_ASSERT(size > 0);
  56. ANKI_ASSERT(alignment > 0 && alignment <= MAX_ALIGNMENT);
  57. const PtrSize newAlignment = MAX_ALIGNMENT;
  58. const PtrSize newSize = sizeof(Header) + size;
  59. // Allocate
  60. MemStats* self = static_cast<MemStats*>(userData);
  61. Header* allocation = static_cast<Header*>(
  62. self->m_originalAllocCallback(self->m_originalUserData, nullptr, newSize, newAlignment));
  63. allocation->m_allocatedSize = size;
  64. ++allocation;
  65. out = static_cast<void*>(allocation);
  66. // Update stats
  67. self->m_allocatedMem.fetchAdd(size);
  68. self->m_allocCount.fetchAdd(1);
  69. }
  70. else
  71. {
  72. // Need to free
  73. MemStats* self = static_cast<MemStats*>(userData);
  74. Header* allocation = static_cast<Header*>(ptr);
  75. --allocation;
  76. ANKI_ASSERT(allocation->m_allocatedSize > 0);
  77. // Update stats
  78. self->m_freeCount.fetchAdd(1);
  79. self->m_allocatedMem.fetchSub(allocation->m_allocatedSize);
  80. // Free
  81. self->m_originalAllocCallback(self->m_originalUserData, allocation, 0, 0);
  82. }
  83. return out;
  84. }
  85. App::App()
  86. {
  87. }
  88. App::~App()
  89. {
  90. ANKI_CORE_LOGI("Destroying application");
  91. cleanup();
  92. }
  93. void App::cleanup()
  94. {
  95. m_statsUi.reset(nullptr);
  96. m_console.reset(nullptr);
  97. m_heapAlloc.deleteInstance(m_scene);
  98. m_scene = nullptr;
  99. m_heapAlloc.deleteInstance(m_script);
  100. m_script = nullptr;
  101. m_heapAlloc.deleteInstance(m_renderer);
  102. m_renderer = nullptr;
  103. m_heapAlloc.deleteInstance(m_ui);
  104. m_ui = nullptr;
  105. m_heapAlloc.deleteInstance(m_resources);
  106. m_resources = nullptr;
  107. m_heapAlloc.deleteInstance(m_resourceFs);
  108. m_resourceFs = nullptr;
  109. m_heapAlloc.deleteInstance(m_physics);
  110. m_physics = nullptr;
  111. m_heapAlloc.deleteInstance(m_stagingMem);
  112. m_stagingMem = nullptr;
  113. m_heapAlloc.deleteInstance(m_vertexMem);
  114. m_vertexMem = nullptr;
  115. m_heapAlloc.deleteInstance(m_threadHive);
  116. m_threadHive = nullptr;
  117. m_heapAlloc.deleteInstance(m_maliHwCounters);
  118. m_maliHwCounters = nullptr;
  119. GrManager::deleteInstance(m_gr);
  120. m_gr = nullptr;
  121. Input::deleteInstance(m_input);
  122. m_input = nullptr;
  123. NativeWindow::deleteInstance(m_window);
  124. m_window = nullptr;
  125. #if ANKI_ENABLE_TRACE
  126. m_heapAlloc.deleteInstance(m_coreTracer);
  127. m_coreTracer = nullptr;
  128. #endif
  129. m_settingsDir.destroy(m_heapAlloc);
  130. m_cacheDir.destroy(m_heapAlloc);
  131. }
  132. Error App::init(ConfigSet* config, CString executableFilename, AllocAlignedCallback allocCb, void* allocCbUserData)
  133. {
  134. ANKI_ASSERT(config);
  135. m_config = config;
  136. const Error err = initInternal(executableFilename, allocCb, allocCbUserData);
  137. if(err)
  138. {
  139. ANKI_CORE_LOGE("App initialization failed. Shutting down");
  140. cleanup();
  141. }
  142. return err;
  143. }
  144. Error App::initInternal(CString executableFilename, AllocAlignedCallback allocCb, void* allocCbUserData)
  145. {
  146. LoggerSingleton::get().enableVerbosity(m_config->getCoreVerboseLog());
  147. setSignalHandlers();
  148. Thread::setNameOfCurrentThread("AnKiMain");
  149. initMemoryCallbacks(allocCb, allocCbUserData);
  150. m_heapAlloc = HeapAllocator<U8>(m_allocCb, m_allocCbData, "Core");
  151. ANKI_CHECK(initDirs());
  152. // Print a message
  153. const char* buildType =
  154. #if ANKI_OPTIMIZE
  155. "optimized, "
  156. #else
  157. "NOT optimized, "
  158. #endif
  159. #if ANKI_DEBUG_SYMBOLS
  160. "dbg symbols, "
  161. #else
  162. "NO dbg symbols, "
  163. #endif
  164. #if ANKI_EXTRA_CHECKS
  165. "extra checks, "
  166. #else
  167. "NO extra checks, "
  168. #endif
  169. #if ANKI_ENABLE_TRACE
  170. "built with tracing";
  171. #else
  172. "NOT built with tracing";
  173. #endif
  174. ANKI_CORE_LOGI("Initializing application ("
  175. "version %u.%u, "
  176. "%s, "
  177. "compiler %s, "
  178. "build date %s, "
  179. "commit %s)",
  180. ANKI_VERSION_MAJOR, ANKI_VERSION_MINOR, buildType, ANKI_COMPILER_STR, __DATE__, ANKI_REVISION);
  181. // Check SIMD support
  182. #if ANKI_SIMD_SSE && ANKI_COMPILER_GCC_COMPATIBLE
  183. if(!__builtin_cpu_supports("sse4.2"))
  184. {
  185. ANKI_CORE_LOGF(
  186. "AnKi is built with sse4.2 support but your CPU doesn't support it. Try bulding without SSE support");
  187. }
  188. #endif
  189. ANKI_CORE_LOGI("Number of job threads: %u", m_config->getCoreJobThreadCount());
  190. //
  191. // Core tracer
  192. //
  193. #if ANKI_ENABLE_TRACE
  194. m_coreTracer = m_heapAlloc.newInstance<CoreTracer>();
  195. ANKI_CHECK(m_coreTracer->init(m_heapAlloc, m_settingsDir));
  196. #endif
  197. //
  198. // Window
  199. //
  200. NativeWindowInitInfo nwinit;
  201. nwinit.m_allocCallback = m_allocCb;
  202. nwinit.m_allocCallbackUserData = m_allocCbData;
  203. nwinit.m_width = m_config->getWidth();
  204. nwinit.m_height = m_config->getHeight();
  205. nwinit.m_depthBits = 0;
  206. nwinit.m_stencilBits = 0;
  207. nwinit.m_fullscreenDesktopRez = m_config->getWindowFullscreen();
  208. ANKI_CHECK(NativeWindow::newInstance(nwinit, m_window));
  209. //
  210. // Input
  211. //
  212. ANKI_CHECK(Input::newInstance(m_allocCb, m_allocCbData, m_window, m_input));
  213. //
  214. // ThreadPool
  215. //
  216. m_threadHive = m_heapAlloc.newInstance<ThreadHive>(m_config->getCoreJobThreadCount(), m_heapAlloc, true);
  217. //
  218. // Graphics API
  219. //
  220. GrManagerInitInfo grInit;
  221. grInit.m_allocCallback = m_allocCb;
  222. grInit.m_allocCallbackUserData = m_allocCbData;
  223. grInit.m_cacheDirectory = m_cacheDir.toCString();
  224. grInit.m_config = m_config;
  225. grInit.m_window = m_window;
  226. ANKI_CHECK(GrManager::newInstance(grInit, m_gr));
  227. //
  228. // Mali HW counters
  229. //
  230. if(m_gr->getDeviceCapabilities().m_gpuVendor == GpuVendor::ARM && m_config->getCoreMaliHwCounters())
  231. {
  232. m_maliHwCounters = m_heapAlloc.newInstance<MaliHwCounters>(m_heapAlloc);
  233. }
  234. //
  235. // GPU mem
  236. //
  237. m_vertexMem = m_heapAlloc.newInstance<VertexGpuMemoryPool>();
  238. ANKI_CHECK(m_vertexMem->init(m_heapAlloc, m_gr, *m_config));
  239. m_stagingMem = m_heapAlloc.newInstance<StagingGpuMemoryPool>();
  240. ANKI_CHECK(m_stagingMem->init(m_gr, *m_config));
  241. //
  242. // Physics
  243. //
  244. m_physics = m_heapAlloc.newInstance<PhysicsWorld>();
  245. ANKI_CHECK(m_physics->init(m_allocCb, m_allocCbData));
  246. //
  247. // Resource FS
  248. //
  249. #if !ANKI_OS_ANDROID
  250. // Add the location of the executable where the shaders are supposed to be
  251. StringAuto shadersPath(m_heapAlloc);
  252. getParentFilepath(executableFilename, shadersPath);
  253. shadersPath.append(":");
  254. shadersPath.append(m_config->getRsrcDataPaths());
  255. m_config->setRsrcDataPaths(shadersPath);
  256. #endif
  257. m_resourceFs = m_heapAlloc.newInstance<ResourceFilesystem>(m_heapAlloc);
  258. ANKI_CHECK(m_resourceFs->init(*m_config, m_cacheDir.toCString()));
  259. //
  260. // Resources
  261. //
  262. ResourceManagerInitInfo rinit;
  263. rinit.m_gr = m_gr;
  264. rinit.m_physics = m_physics;
  265. rinit.m_resourceFs = m_resourceFs;
  266. rinit.m_vertexMemory = m_vertexMem;
  267. rinit.m_config = m_config;
  268. rinit.m_allocCallback = m_allocCb;
  269. rinit.m_allocCallbackData = m_allocCbData;
  270. m_resources = m_heapAlloc.newInstance<ResourceManager>();
  271. ANKI_CHECK(m_resources->init(rinit));
  272. //
  273. // UI
  274. //
  275. m_ui = m_heapAlloc.newInstance<UiManager>();
  276. ANKI_CHECK(m_ui->init(m_allocCb, m_allocCbData, m_resources, m_gr, m_stagingMem, m_input));
  277. //
  278. // Renderer
  279. //
  280. MainRendererInitInfo renderInit;
  281. renderInit.m_swapchainSize = UVec2(m_window->getWidth(), m_window->getHeight());
  282. renderInit.m_allocCallback = m_allocCb;
  283. renderInit.m_allocCallbackUserData = m_allocCbData;
  284. renderInit.m_threadHive = m_threadHive;
  285. renderInit.m_resourceManager = m_resources;
  286. renderInit.m_gr = m_gr;
  287. renderInit.m_stagingMemory = m_stagingMem;
  288. renderInit.m_ui = m_ui;
  289. renderInit.m_config = m_config;
  290. renderInit.m_globTimestamp = &m_globalTimestamp;
  291. m_renderer = m_heapAlloc.newInstance<MainRenderer>();
  292. ANKI_CHECK(m_renderer->init(renderInit));
  293. //
  294. // Script
  295. //
  296. m_script = m_heapAlloc.newInstance<ScriptManager>();
  297. ANKI_CHECK(m_script->init(m_allocCb, m_allocCbData));
  298. //
  299. // Scene
  300. //
  301. m_scene = m_heapAlloc.newInstance<SceneGraph>();
  302. ANKI_CHECK(m_scene->init(m_allocCb, m_allocCbData, m_threadHive, m_resources, m_input, m_script, m_ui, m_config,
  303. &m_globalTimestamp));
  304. // Inform the script engine about some subsystems
  305. m_script->setRenderer(m_renderer);
  306. m_script->setSceneGraph(m_scene);
  307. //
  308. // Misc
  309. //
  310. ANKI_CHECK(m_ui->newInstance<StatsUi>(m_statsUi));
  311. ANKI_CHECK(m_ui->newInstance<DeveloperConsole>(m_console, m_allocCb, m_allocCbData, m_script));
  312. ANKI_CORE_LOGI("Application initialized");
  313. return Error::NONE;
  314. }
  315. Error App::initDirs()
  316. {
  317. // Settings path
  318. #if !ANKI_OS_ANDROID
  319. StringAuto home(m_heapAlloc);
  320. ANKI_CHECK(getHomeDirectory(home));
  321. m_settingsDir.sprintf(m_heapAlloc, "%s/.anki", &home[0]);
  322. #else
  323. m_settingsDir.sprintf(m_heapAlloc, "%s/.anki", g_androidApp->activity->internalDataPath);
  324. #endif
  325. if(!directoryExists(m_settingsDir.toCString()))
  326. {
  327. ANKI_CORE_LOGI("Creating settings dir \"%s\"", &m_settingsDir[0]);
  328. ANKI_CHECK(createDirectory(m_settingsDir.toCString()));
  329. }
  330. else
  331. {
  332. ANKI_CORE_LOGI("Using settings dir \"%s\"", &m_settingsDir[0]);
  333. }
  334. // Cache
  335. m_cacheDir.sprintf(m_heapAlloc, "%s/cache", &m_settingsDir[0]);
  336. const Bool cacheDirExists = directoryExists(m_cacheDir.toCString());
  337. if(m_config->getCoreClearCaches() && cacheDirExists)
  338. {
  339. ANKI_CORE_LOGI("Will delete the cache dir and start fresh: %s", &m_cacheDir[0]);
  340. ANKI_CHECK(removeDirectory(m_cacheDir.toCString(), m_heapAlloc));
  341. ANKI_CHECK(createDirectory(m_cacheDir.toCString()));
  342. }
  343. else if(!cacheDirExists)
  344. {
  345. ANKI_CORE_LOGI("Will create cache dir: %s", &m_cacheDir[0]);
  346. ANKI_CHECK(createDirectory(m_cacheDir.toCString()));
  347. }
  348. return Error::NONE;
  349. }
  350. Error App::mainLoop()
  351. {
  352. ANKI_CORE_LOGI("Entering main loop");
  353. Bool quit = false;
  354. Second prevUpdateTime = HighRezTimer::getCurrentTime();
  355. Second crntTime = prevUpdateTime;
  356. while(!quit)
  357. {
  358. {
  359. ANKI_TRACE_SCOPED_EVENT(FRAME);
  360. const Second startTime = HighRezTimer::getCurrentTime();
  361. prevUpdateTime = crntTime;
  362. crntTime = HighRezTimer::getCurrentTime();
  363. // Update
  364. ANKI_CHECK(m_input->handleEvents());
  365. // User update
  366. ANKI_CHECK(userMainLoop(quit, crntTime - prevUpdateTime));
  367. ANKI_CHECK(m_scene->update(prevUpdateTime, crntTime));
  368. RenderQueue rqueue;
  369. m_scene->doVisibilityTests(rqueue);
  370. // Inject stats UI
  371. DynamicArrayAuto<UiQueueElement> newUiElementArr(m_heapAlloc);
  372. injectUiElements(newUiElementArr, rqueue);
  373. // Render
  374. TexturePtr presentableTex = m_gr->acquireNextPresentableTexture();
  375. m_renderer->setStatsEnabled(m_config->getCoreDisplayStats()
  376. #if ANKI_ENABLE_TRACE
  377. || TracerSingleton::get().getEnabled()
  378. #endif
  379. );
  380. ANKI_CHECK(m_renderer->render(rqueue, presentableTex));
  381. // Pause and sync async loader. That will force all tasks before the pause to finish in this frame.
  382. m_resources->getAsyncLoader().pause();
  383. m_gr->swapBuffers();
  384. m_stagingMem->endFrame();
  385. // Update the trace info with some async loader stats
  386. U64 asyncTaskCount = m_resources->getAsyncLoader().getCompletedTaskCount();
  387. ANKI_TRACE_INC_COUNTER(RESOURCE_ASYNC_TASKS, asyncTaskCount - m_resourceCompletedAsyncTaskCount);
  388. m_resourceCompletedAsyncTaskCount = asyncTaskCount;
  389. // Now resume the loader
  390. m_resources->getAsyncLoader().resume();
  391. // Sleep
  392. const Second endTime = HighRezTimer::getCurrentTime();
  393. const Second frameTime = endTime - startTime;
  394. const Second timerTick = 1.0 / Second(m_config->getCoreTargetFps());
  395. if(frameTime < timerTick)
  396. {
  397. ANKI_TRACE_SCOPED_EVENT(TIMER_TICK_SLEEP);
  398. HighRezTimer::sleep(timerTick - frameTime);
  399. }
  400. // Stats
  401. if(m_config->getCoreDisplayStats())
  402. {
  403. StatsUi& statsUi = *static_cast<StatsUi*>(m_statsUi.get());
  404. statsUi.setFrameTime(frameTime);
  405. statsUi.setRenderTime(m_renderer->getStats().m_renderingCpuTime);
  406. statsUi.setSceneUpdateTime(m_scene->getStats().m_updateTime);
  407. statsUi.setVisibilityTestsTime(m_scene->getStats().m_visibilityTestsTime);
  408. statsUi.setPhysicsTime(m_scene->getStats().m_physicsUpdate);
  409. statsUi.setGpuTime(m_renderer->getStats().m_renderingGpuTime);
  410. if(m_maliHwCounters)
  411. {
  412. MaliHwCountersOut out;
  413. m_maliHwCounters->sample(out);
  414. statsUi.setGpuActiveCycles(out.m_gpuActive);
  415. statsUi.setGpuReadBandwidth(out.m_readBandwidth);
  416. statsUi.setGpuWriteBandwidth(out.m_writeBandwidth);
  417. }
  418. statsUi.setAllocatedCpuMemory(m_memStats.m_allocatedMem.load());
  419. statsUi.setCpuAllocationCount(m_memStats.m_allocCount.load());
  420. statsUi.setCpuFreeCount(m_memStats.m_freeCount.load());
  421. statsUi.setGrStats(m_gr->getStats());
  422. BuddyAllocatorBuilderStats vertMemStats;
  423. m_vertexMem->getMemoryStats(vertMemStats);
  424. statsUi.setGlobalVertexMemoryPoolStats(vertMemStats);
  425. statsUi.setDrawableCount(rqueue.countAllRenderables());
  426. }
  427. #if ANKI_ENABLE_TRACE
  428. if(m_renderer->getStats().m_renderingGpuTime >= 0.0)
  429. {
  430. ANKI_TRACE_CUSTOM_EVENT(GPU_TIME, m_renderer->getStats().m_renderingGpuSubmitTimestamp,
  431. m_renderer->getStats().m_renderingGpuTime);
  432. }
  433. #endif
  434. ++m_globalTimestamp;
  435. }
  436. #if ANKI_ENABLE_TRACE
  437. static U64 frame = 1;
  438. m_coreTracer->flushFrame(frame++);
  439. #endif
  440. }
  441. return Error::NONE;
  442. }
  443. void App::injectUiElements(DynamicArrayAuto<UiQueueElement>& newUiElementArr, RenderQueue& rqueue)
  444. {
  445. const U32 originalCount = rqueue.m_uis.getSize();
  446. if(m_config->getCoreDisplayStats() || m_consoleEnabled)
  447. {
  448. const U32 extraElements = m_config->getCoreDisplayStats() + (m_consoleEnabled != 0);
  449. newUiElementArr.create(originalCount + extraElements);
  450. if(originalCount > 0)
  451. {
  452. memcpy(&newUiElementArr[0], &rqueue.m_uis[0], rqueue.m_uis.getSizeInBytes());
  453. }
  454. rqueue.m_uis = WeakArray<UiQueueElement>(newUiElementArr);
  455. }
  456. U32 count = originalCount;
  457. if(m_config->getCoreDisplayStats())
  458. {
  459. newUiElementArr[count].m_userData = m_statsUi.get();
  460. newUiElementArr[count].m_drawCallback = [](CanvasPtr& canvas, void* userData) -> void {
  461. static_cast<StatsUi*>(userData)->build(canvas);
  462. };
  463. ++count;
  464. }
  465. if(m_consoleEnabled)
  466. {
  467. newUiElementArr[count].m_userData = m_console.get();
  468. newUiElementArr[count].m_drawCallback = [](CanvasPtr& canvas, void* userData) -> void {
  469. static_cast<DeveloperConsole*>(userData)->build(canvas);
  470. };
  471. ++count;
  472. }
  473. }
  474. void App::initMemoryCallbacks(AllocAlignedCallback allocCb, void* allocCbUserData)
  475. {
  476. if(m_config->getCoreDisplayStats())
  477. {
  478. m_memStats.m_originalAllocCallback = allocCb;
  479. m_memStats.m_originalUserData = allocCbUserData;
  480. m_allocCb = MemStats::allocCallback;
  481. m_allocCbData = &m_memStats;
  482. }
  483. else
  484. {
  485. m_allocCb = allocCb;
  486. m_allocCbData = allocCbUserData;
  487. }
  488. }
  489. void App::setSignalHandlers()
  490. {
  491. auto handler = [](int signum) -> void {
  492. const char* name = nullptr;
  493. switch(signum)
  494. {
  495. case SIGABRT:
  496. name = "SIGABRT";
  497. break;
  498. case SIGSEGV:
  499. name = "SIGSEGV";
  500. break;
  501. #if ANKI_POSIX
  502. case SIGBUS:
  503. name = "SIGBUS";
  504. break;
  505. #endif
  506. case SIGILL:
  507. name = "SIGILL";
  508. break;
  509. case SIGFPE:
  510. name = "SIGFPE";
  511. break;
  512. }
  513. if(name)
  514. printf("Caught signal %d (%s)\n", signum, name);
  515. else
  516. printf("Caught signal %d\n", signum);
  517. U32 count = 0;
  518. printf("Backtrace:\n");
  519. backtrace(HeapAllocator<U8>(allocAligned, nullptr), [&count](CString symbol) {
  520. printf("%.2u: %s\n", count++, symbol.cstr());
  521. });
  522. ANKI_DEBUG_BREAK();
  523. };
  524. signal(SIGSEGV, handler);
  525. signal(SIGILL, handler);
  526. signal(SIGFPE, handler);
  527. #if ANKI_POSIX
  528. signal(SIGBUS, handler);
  529. #endif
  530. // Ignore for now: signal(SIGABRT, handler);
  531. }
  532. } // end namespace anki