App.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. // Copyright (C) 2009-2021, 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/NativeWindow.h>
  17. #include <anki/input/Input.h>
  18. #include <anki/scene/SceneGraph.h>
  19. #include <anki/renderer/RenderQueue.h>
  20. #include <anki/resource/ResourceManager.h>
  21. #include <anki/physics/PhysicsWorld.h>
  22. #include <anki/renderer/MainRenderer.h>
  23. #include <anki/script/ScriptManager.h>
  24. #include <anki/resource/ResourceFilesystem.h>
  25. #include <anki/resource/AsyncLoader.h>
  26. #include <anki/core/StagingGpuMemoryManager.h>
  27. #include <anki/ui/UiManager.h>
  28. #include <anki/ui/Canvas.h>
  29. #if ANKI_OS_ANDROID
  30. # include <android_native_app_glue.h>
  31. #endif
  32. namespace anki
  33. {
  34. #if ANKI_OS_ANDROID
  35. /// The one and only android hack
  36. android_app* gAndroidApp = nullptr;
  37. #endif
  38. class App::StatsUi : public UiImmediateModeBuilder
  39. {
  40. public:
  41. template<typename T>
  42. class BufferedValue
  43. {
  44. public:
  45. void set(T x)
  46. {
  47. m_total += x;
  48. ++m_count;
  49. }
  50. F64 get(Bool flush)
  51. {
  52. if(flush)
  53. {
  54. m_avg = F64(m_total) / m_count;
  55. m_count = 0;
  56. m_total = 0.0;
  57. }
  58. return m_avg;
  59. }
  60. private:
  61. T m_total = T(0);
  62. F64 m_avg = 0.0;
  63. U32 m_count = 0;
  64. };
  65. BufferedValue<Second> m_frameTime;
  66. BufferedValue<Second> m_renderTime;
  67. BufferedValue<Second> m_lightBinTime;
  68. BufferedValue<Second> m_sceneUpdateTime;
  69. BufferedValue<Second> m_visTestsTime;
  70. BufferedValue<Second> m_physicsTime;
  71. BufferedValue<Second> m_gpuTime;
  72. PtrSize m_allocatedCpuMem = 0;
  73. U64 m_allocCount = 0;
  74. U64 m_freeCount = 0;
  75. U64 m_vkCpuMem = 0;
  76. U64 m_vkGpuMem = 0;
  77. U32 m_vkCmdbCount = 0;
  78. PtrSize m_drawableCount = 0;
  79. static const U32 BUFFERED_FRAMES = 16;
  80. U32 m_bufferedFrames = 0;
  81. StatsUi(UiManager* ui)
  82. : UiImmediateModeBuilder(ui)
  83. {
  84. }
  85. void build(CanvasPtr canvas) override
  86. {
  87. // Misc
  88. ++m_bufferedFrames;
  89. Bool flush = false;
  90. if(m_bufferedFrames == BUFFERED_FRAMES)
  91. {
  92. flush = true;
  93. m_bufferedFrames = 0;
  94. }
  95. // Start drawing the UI
  96. canvas->pushFont(canvas->getDefaultFont(), 16);
  97. const Vec4 oldWindowColor = ImGui::GetStyle().Colors[ImGuiCol_WindowBg];
  98. ImGui::GetStyle().Colors[ImGuiCol_WindowBg].w = 0.3f;
  99. if(ImGui::Begin("Stats", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize))
  100. {
  101. ImGui::SetWindowPos(Vec2(5.0f, 5.0f));
  102. ImGui::SetWindowSize(Vec2(230.0f, 450.0f));
  103. ImGui::Text("CPU Time:");
  104. labelTime(m_frameTime.get(flush), "Total frame");
  105. labelTime(m_renderTime.get(flush) - m_lightBinTime.get(flush), "Renderer");
  106. labelTime(m_lightBinTime.get(false), "Light bin");
  107. labelTime(m_sceneUpdateTime.get(flush), "Scene update");
  108. labelTime(m_visTestsTime.get(flush), "Visibility");
  109. labelTime(m_physicsTime.get(flush), "Physics");
  110. ImGui::Text("----");
  111. ImGui::Text("GPU Time:");
  112. labelTime(m_gpuTime.get(flush), "Total frame");
  113. ImGui::Text("----");
  114. ImGui::Text("Memory:");
  115. labelBytes(m_allocatedCpuMem, "Total CPU");
  116. labelUint(m_allocCount, "Total allocations");
  117. labelUint(m_freeCount, "Total frees");
  118. labelBytes(m_vkCpuMem, "Vulkan CPU");
  119. labelBytes(m_vkGpuMem, "Vulkan GPU");
  120. ImGui::Text("----");
  121. ImGui::Text("Vulkan:");
  122. labelUint(m_vkCmdbCount, "Cmd buffers");
  123. ImGui::Text("----");
  124. ImGui::Text("Other:");
  125. labelUint(m_drawableCount, "Drawbles");
  126. }
  127. ImGui::End();
  128. ImGui::GetStyle().Colors[ImGuiCol_WindowBg] = oldWindowColor;
  129. canvas->popFont();
  130. }
  131. void labelTime(Second val, CString name)
  132. {
  133. ImGui::Text("%s: %fms", name.cstr(), val * 1000.0);
  134. }
  135. void labelBytes(PtrSize val, CString name)
  136. {
  137. U gb, mb, kb, b;
  138. gb = val / 1_GB;
  139. val -= gb * 1_GB;
  140. mb = val / 1_MB;
  141. val -= mb * 1_MB;
  142. kb = val / 1_KB;
  143. val -= kb * 1_KB;
  144. b = val;
  145. StringAuto timestamp(getAllocator());
  146. if(gb)
  147. {
  148. timestamp.sprintf("%s: %4u,%04u,%04u,%04u", name.cstr(), gb, mb, kb, b);
  149. }
  150. else if(mb)
  151. {
  152. timestamp.sprintf("%s: %4u,%04u,%04u", name.cstr(), mb, kb, b);
  153. }
  154. else if(kb)
  155. {
  156. timestamp.sprintf("%s: %4u,%04u", name.cstr(), kb, b);
  157. }
  158. else
  159. {
  160. timestamp.sprintf("%s: %4u", name.cstr(), b);
  161. }
  162. ImGui::TextUnformatted(timestamp.cstr());
  163. }
  164. void labelUint(U64 val, CString name)
  165. {
  166. ImGui::Text("%s: %lu", name.cstr(), val);
  167. }
  168. };
  169. void* App::MemStats::allocCallback(void* userData, void* ptr, PtrSize size, PtrSize alignment)
  170. {
  171. ANKI_ASSERT(userData);
  172. static const PtrSize MAX_ALIGNMENT = 64;
  173. struct alignas(MAX_ALIGNMENT) Header
  174. {
  175. PtrSize m_allocatedSize;
  176. Array<U8, MAX_ALIGNMENT - sizeof(PtrSize)> _m_padding;
  177. };
  178. static_assert(sizeof(Header) == MAX_ALIGNMENT, "See file");
  179. static_assert(alignof(Header) == MAX_ALIGNMENT, "See file");
  180. void* out = nullptr;
  181. if(ptr == nullptr)
  182. {
  183. // Need to allocate
  184. ANKI_ASSERT(size > 0);
  185. ANKI_ASSERT(alignment > 0 && alignment <= MAX_ALIGNMENT);
  186. const PtrSize newAlignment = MAX_ALIGNMENT;
  187. const PtrSize newSize = sizeof(Header) + size;
  188. // Allocate
  189. MemStats* self = static_cast<MemStats*>(userData);
  190. Header* allocation = static_cast<Header*>(
  191. self->m_originalAllocCallback(self->m_originalUserData, nullptr, newSize, newAlignment));
  192. allocation->m_allocatedSize = size;
  193. ++allocation;
  194. out = static_cast<void*>(allocation);
  195. // Update stats
  196. self->m_allocatedMem.fetchAdd(size);
  197. self->m_allocCount.fetchAdd(1);
  198. }
  199. else
  200. {
  201. // Need to free
  202. MemStats* self = static_cast<MemStats*>(userData);
  203. Header* allocation = static_cast<Header*>(ptr);
  204. --allocation;
  205. ANKI_ASSERT(allocation->m_allocatedSize > 0);
  206. // Update stats
  207. self->m_freeCount.fetchAdd(1);
  208. self->m_allocatedMem.fetchSub(allocation->m_allocatedSize);
  209. // Free
  210. self->m_originalAllocCallback(self->m_originalUserData, allocation, 0, 0);
  211. }
  212. return out;
  213. }
  214. App::App()
  215. {
  216. }
  217. App::~App()
  218. {
  219. cleanup();
  220. }
  221. void App::cleanup()
  222. {
  223. m_statsUi.reset(nullptr);
  224. m_console.reset(nullptr);
  225. m_heapAlloc.deleteInstance(m_scene);
  226. m_heapAlloc.deleteInstance(m_script);
  227. m_heapAlloc.deleteInstance(m_renderer);
  228. m_heapAlloc.deleteInstance(m_ui);
  229. m_heapAlloc.deleteInstance(m_resources);
  230. m_heapAlloc.deleteInstance(m_resourceFs);
  231. m_heapAlloc.deleteInstance(m_physics);
  232. m_heapAlloc.deleteInstance(m_stagingMem);
  233. m_heapAlloc.deleteInstance(m_threadHive);
  234. GrManager::deleteInstance(m_gr);
  235. m_heapAlloc.deleteInstance(m_input);
  236. m_heapAlloc.deleteInstance(m_window);
  237. #if ANKI_ENABLE_TRACE
  238. m_heapAlloc.deleteInstance(m_coreTracer);
  239. #endif
  240. m_settingsDir.destroy(m_heapAlloc);
  241. m_cacheDir.destroy(m_heapAlloc);
  242. }
  243. Error App::init(const ConfigSet& config, AllocAlignedCallback allocCb, void* allocCbUserData)
  244. {
  245. const Error err = initInternal(config, allocCb, allocCbUserData);
  246. if(err)
  247. {
  248. ANKI_CORE_LOGE("App initialization failed. Shutting down");
  249. cleanup();
  250. }
  251. return err;
  252. }
  253. Error App::initInternal(const ConfigSet& config_, AllocAlignedCallback allocCb, void* allocCbUserData)
  254. {
  255. ConfigSet config = config_;
  256. m_displayStats = config.getNumberU32("core_displayStats");
  257. initMemoryCallbacks(allocCb, allocCbUserData);
  258. m_heapAlloc = HeapAllocator<U8>(m_allocCb, m_allocCbData);
  259. ANKI_CHECK(initDirs(config));
  260. // Print a message
  261. const char* buildType =
  262. #if ANKI_OPTIMIZE
  263. "optimized, "
  264. #else
  265. "NOT optimized, "
  266. #endif
  267. #if ANKI_DEBUG_SYMBOLS
  268. "dbg symbols, "
  269. #else
  270. "NO dbg symbols, "
  271. #endif
  272. #if ANKI_EXTRA_CHECKS
  273. "extra checks";
  274. #else
  275. "NO extra checks";
  276. #endif
  277. ANKI_CORE_LOGI("Initializing application ("
  278. "version %u.%u, "
  279. "%s, "
  280. "compiler %s, "
  281. "build date %s, "
  282. "commit %s)",
  283. ANKI_VERSION_MAJOR, ANKI_VERSION_MINOR, buildType, ANKI_COMPILER_STR, __DATE__, ANKI_REVISION);
  284. m_timerTick = 1.0 / F32(config.getNumberU32("core_targetFps")); // in sec. 1.0 / period
  285. // Check SIMD support
  286. #if ANKI_SIMD_SSE && ANKI_COMPILER_GCC_COMPATIBLE
  287. if(!__builtin_cpu_supports("sse4.2"))
  288. {
  289. ANKI_CORE_LOGF(
  290. "AnKi is built with sse4.2 support but your CPU doesn't support it. Try bulding without SSE support");
  291. }
  292. #endif
  293. ANKI_CORE_LOGI("Number of main threads: %u", config.getNumberU32("core_mainThreadCount"));
  294. //
  295. // Core tracer
  296. //
  297. #if ANKI_ENABLE_TRACE
  298. m_coreTracer = m_heapAlloc.newInstance<CoreTracer>();
  299. ANKI_CHECK(m_coreTracer->init(m_heapAlloc, m_settingsDir));
  300. #endif
  301. //
  302. // Window
  303. //
  304. NativeWindowInitInfo nwinit;
  305. nwinit.m_width = config.getNumberU32("width");
  306. nwinit.m_height = config.getNumberU32("height");
  307. nwinit.m_depthBits = 0;
  308. nwinit.m_stencilBits = 0;
  309. nwinit.m_fullscreenDesktopRez = config.getBool("window_fullscreen");
  310. m_window = m_heapAlloc.newInstance<NativeWindow>();
  311. ANKI_CHECK(m_window->init(nwinit, m_heapAlloc));
  312. //
  313. // Input
  314. //
  315. m_input = m_heapAlloc.newInstance<Input>();
  316. ANKI_CHECK(m_input->init(m_window));
  317. //
  318. // ThreadPool
  319. //
  320. m_threadHive = m_heapAlloc.newInstance<ThreadHive>(config.getNumberU32("core_mainThreadCount"), m_heapAlloc, true);
  321. //
  322. // Graphics API
  323. //
  324. GrManagerInitInfo grInit;
  325. grInit.m_allocCallback = m_allocCb;
  326. grInit.m_allocCallbackUserData = m_allocCbData;
  327. grInit.m_cacheDirectory = m_cacheDir.toCString();
  328. grInit.m_config = &config;
  329. grInit.m_window = m_window;
  330. ANKI_CHECK(GrManager::newInstance(grInit, m_gr));
  331. //
  332. // Staging mem
  333. //
  334. m_stagingMem = m_heapAlloc.newInstance<StagingGpuMemoryManager>();
  335. ANKI_CHECK(m_stagingMem->init(m_gr, config));
  336. //
  337. // Physics
  338. //
  339. m_physics = m_heapAlloc.newInstance<PhysicsWorld>();
  340. ANKI_CHECK(m_physics->init(m_allocCb, m_allocCbData));
  341. //
  342. // Resource FS
  343. //
  344. m_resourceFs = m_heapAlloc.newInstance<ResourceFilesystem>(m_heapAlloc);
  345. ANKI_CHECK(m_resourceFs->init(config, m_cacheDir.toCString()));
  346. //
  347. // Resources
  348. //
  349. ResourceManagerInitInfo rinit;
  350. rinit.m_gr = m_gr;
  351. rinit.m_physics = m_physics;
  352. rinit.m_resourceFs = m_resourceFs;
  353. rinit.m_config = &config;
  354. rinit.m_cacheDir = m_cacheDir.toCString();
  355. rinit.m_allocCallback = m_allocCb;
  356. rinit.m_allocCallbackData = m_allocCbData;
  357. m_resources = m_heapAlloc.newInstance<ResourceManager>();
  358. ANKI_CHECK(m_resources->init(rinit));
  359. //
  360. // UI
  361. //
  362. m_ui = m_heapAlloc.newInstance<UiManager>();
  363. ANKI_CHECK(m_ui->init(m_allocCb, m_allocCbData, m_resources, m_gr, m_stagingMem, m_input));
  364. //
  365. // Renderer
  366. //
  367. if(nwinit.m_fullscreenDesktopRez)
  368. {
  369. config.set("width", m_window->getWidth());
  370. config.set("height", m_window->getHeight());
  371. }
  372. m_renderer = m_heapAlloc.newInstance<MainRenderer>();
  373. ANKI_CHECK(m_renderer->init(m_threadHive, m_resources, m_gr, m_stagingMem, m_ui, m_allocCb, m_allocCbData, config,
  374. &m_globalTimestamp));
  375. //
  376. // Script
  377. //
  378. m_script = m_heapAlloc.newInstance<ScriptManager>();
  379. ANKI_CHECK(m_script->init(m_allocCb, m_allocCbData));
  380. //
  381. // Scene
  382. //
  383. m_scene = m_heapAlloc.newInstance<SceneGraph>();
  384. ANKI_CHECK(m_scene->init(m_allocCb, m_allocCbData, m_threadHive, m_resources, m_input, m_script, &m_globalTimestamp,
  385. config));
  386. // Inform the script engine about some subsystems
  387. m_script->setRenderer(m_renderer);
  388. m_script->setSceneGraph(m_scene);
  389. //
  390. // Misc
  391. //
  392. ANKI_CHECK(m_ui->newInstance<StatsUi>(m_statsUi));
  393. ANKI_CHECK(m_ui->newInstance<DeveloperConsole>(m_console, m_allocCb, m_allocCbData, m_script));
  394. ANKI_CORE_LOGI("Application initialized");
  395. return Error::NONE;
  396. }
  397. Error App::initDirs(const ConfigSet& cfg)
  398. {
  399. #if !ANKI_OS_ANDROID
  400. // Settings path
  401. StringAuto home(m_heapAlloc);
  402. ANKI_CHECK(getHomeDirectory(home));
  403. m_settingsDir.sprintf(m_heapAlloc, "%s/.anki", &home[0]);
  404. if(!directoryExists(m_settingsDir.toCString()))
  405. {
  406. ANKI_CORE_LOGI("Creating settings dir \"%s\"", &m_settingsDir[0]);
  407. ANKI_CHECK(createDirectory(m_settingsDir.toCString()));
  408. }
  409. else
  410. {
  411. ANKI_CORE_LOGI("Using settings dir \"%s\"", &m_settingsDir[0]);
  412. }
  413. // Cache
  414. m_cacheDir.sprintf(m_heapAlloc, "%s/cache", &m_settingsDir[0]);
  415. const Bool cacheDirExists = directoryExists(m_cacheDir.toCString());
  416. if(cfg.getBool("core_clearCaches") && cacheDirExists)
  417. {
  418. ANKI_CORE_LOGI("Will delete the cache dir and start fresh: %s", &m_cacheDir[0]);
  419. ANKI_CHECK(removeDirectory(m_cacheDir.toCString(), m_heapAlloc));
  420. ANKI_CHECK(createDirectory(m_cacheDir.toCString()));
  421. }
  422. else if(!cacheDirExists)
  423. {
  424. ANKI_CORE_LOGI("Will create cache dir: %s", &m_cacheDir[0]);
  425. ANKI_CHECK(createDirectory(m_cacheDir.toCString()));
  426. }
  427. #else
  428. // ANKI_ASSERT(gAndroidApp);
  429. // ANativeActivity* activity = gAndroidApp->activity;
  430. // Settings path
  431. // settingsDir = String(activity->internalDataDir, alloc);
  432. settingsDir = String("/sdcard/.anki/");
  433. if(!directoryExists(settingsDir.c_str()))
  434. {
  435. createDirectory(settingsDir.c_str());
  436. }
  437. // Cache
  438. cacheDir = settingsDir + "/cache";
  439. if(directoryExists(cacheDir.c_str()))
  440. {
  441. removeDirectory(cacheDir.c_str());
  442. }
  443. createDirectory(cacheDir.c_str());
  444. #endif
  445. return Error::NONE;
  446. }
  447. Error App::mainLoop()
  448. {
  449. ANKI_CORE_LOGI("Entering main loop");
  450. Bool quit = false;
  451. Second prevUpdateTime = HighRezTimer::getCurrentTime();
  452. Second crntTime = prevUpdateTime;
  453. while(!quit)
  454. {
  455. {
  456. ANKI_TRACE_SCOPED_EVENT(FRAME);
  457. const Second startTime = HighRezTimer::getCurrentTime();
  458. prevUpdateTime = crntTime;
  459. crntTime = HighRezTimer::getCurrentTime();
  460. // Update
  461. ANKI_CHECK(m_input->handleEvents());
  462. // User update
  463. ANKI_CHECK(userMainLoop(quit, crntTime - prevUpdateTime));
  464. ANKI_CHECK(m_scene->update(prevUpdateTime, crntTime));
  465. RenderQueue rqueue;
  466. m_scene->doVisibilityTests(rqueue);
  467. // Inject stats UI
  468. DynamicArrayAuto<UiQueueElement> newUiElementArr(m_heapAlloc);
  469. injectUiElements(newUiElementArr, rqueue);
  470. // Render
  471. TexturePtr presentableTex = m_gr->acquireNextPresentableTexture();
  472. m_renderer->setStatsEnabled(m_displayStats
  473. #if ANKI_ENABLE_TRACE
  474. || TracerSingleton::get().getEnabled()
  475. #endif
  476. );
  477. ANKI_CHECK(m_renderer->render(rqueue, presentableTex));
  478. // Pause and sync async loader. That will force all tasks before the pause to finish in this frame.
  479. m_resources->getAsyncLoader().pause();
  480. m_gr->swapBuffers();
  481. m_stagingMem->endFrame();
  482. // Update the trace info with some async loader stats
  483. U64 asyncTaskCount = m_resources->getAsyncLoader().getCompletedTaskCount();
  484. ANKI_TRACE_INC_COUNTER(RESOURCE_ASYNC_TASKS, asyncTaskCount - m_resourceCompletedAsyncTaskCount);
  485. m_resourceCompletedAsyncTaskCount = asyncTaskCount;
  486. // Now resume the loader
  487. m_resources->getAsyncLoader().resume();
  488. // Sleep
  489. const Second endTime = HighRezTimer::getCurrentTime();
  490. const Second frameTime = endTime - startTime;
  491. if(frameTime < m_timerTick)
  492. {
  493. ANKI_TRACE_SCOPED_EVENT(TIMER_TICK_SLEEP);
  494. HighRezTimer::sleep(m_timerTick - frameTime);
  495. }
  496. // Stats
  497. if(m_displayStats)
  498. {
  499. StatsUi& statsUi = static_cast<StatsUi&>(*m_statsUi);
  500. statsUi.m_frameTime.set(frameTime);
  501. statsUi.m_renderTime.set(m_renderer->getStats().m_renderingCpuTime);
  502. statsUi.m_lightBinTime.set(m_renderer->getStats().m_lightBinTime);
  503. statsUi.m_sceneUpdateTime.set(m_scene->getStats().m_updateTime);
  504. statsUi.m_visTestsTime.set(m_scene->getStats().m_visibilityTestsTime);
  505. statsUi.m_physicsTime.set(m_scene->getStats().m_physicsUpdate);
  506. statsUi.m_gpuTime.set(m_renderer->getStats().m_renderingGpuTime);
  507. statsUi.m_allocatedCpuMem = m_memStats.m_allocatedMem.load();
  508. statsUi.m_allocCount = m_memStats.m_allocCount.load();
  509. statsUi.m_freeCount = m_memStats.m_freeCount.load();
  510. GrManagerStats grStats = m_gr->getStats();
  511. statsUi.m_vkCpuMem = grStats.m_cpuMemory;
  512. statsUi.m_vkGpuMem = grStats.m_gpuMemory;
  513. statsUi.m_vkCmdbCount = grStats.m_commandBufferCount;
  514. statsUi.m_drawableCount = rqueue.countAllRenderables();
  515. }
  516. #if ANKI_ENABLE_TRACE
  517. if(m_renderer->getStats().m_renderingGpuTime >= 0.0)
  518. {
  519. ANKI_TRACE_CUSTOM_EVENT(GPU_TIME, m_renderer->getStats().m_renderingGpuSubmitTimestamp,
  520. m_renderer->getStats().m_renderingGpuTime);
  521. }
  522. #endif
  523. ++m_globalTimestamp;
  524. }
  525. #if ANKI_ENABLE_TRACE
  526. static U64 frame = 1;
  527. m_coreTracer->flushFrame(frame++);
  528. #endif
  529. }
  530. return Error::NONE;
  531. }
  532. void App::injectUiElements(DynamicArrayAuto<UiQueueElement>& newUiElementArr, RenderQueue& rqueue)
  533. {
  534. const U32 originalCount = rqueue.m_uis.getSize();
  535. if(m_displayStats || m_consoleEnabled)
  536. {
  537. const U32 extraElements = (m_displayStats != 0) + (m_consoleEnabled != 0);
  538. newUiElementArr.create(originalCount + extraElements);
  539. if(originalCount > 0)
  540. {
  541. memcpy(&newUiElementArr[0], &rqueue.m_uis[0], rqueue.m_uis.getSizeInBytes());
  542. }
  543. rqueue.m_uis = WeakArray<UiQueueElement>(newUiElementArr);
  544. }
  545. U32 count = originalCount;
  546. if(m_displayStats)
  547. {
  548. newUiElementArr[count].m_userData = m_statsUi.get();
  549. newUiElementArr[count].m_drawCallback = [](CanvasPtr& canvas, void* userData) -> void {
  550. static_cast<StatsUi*>(userData)->build(canvas);
  551. };
  552. ++count;
  553. }
  554. if(m_consoleEnabled)
  555. {
  556. newUiElementArr[count].m_userData = m_console.get();
  557. newUiElementArr[count].m_drawCallback = [](CanvasPtr& canvas, void* userData) -> void {
  558. static_cast<DeveloperConsole*>(userData)->build(canvas);
  559. };
  560. ++count;
  561. }
  562. }
  563. void App::initMemoryCallbacks(AllocAlignedCallback allocCb, void* allocCbUserData)
  564. {
  565. if(m_displayStats)
  566. {
  567. m_memStats.m_originalAllocCallback = allocCb;
  568. m_memStats.m_originalUserData = allocCbUserData;
  569. m_allocCb = MemStats::allocCallback;
  570. m_allocCbData = &m_memStats;
  571. }
  572. else
  573. {
  574. m_allocCb = allocCb;
  575. m_allocCbData = allocCbUserData;
  576. }
  577. }
  578. } // end namespace anki