App.cpp 17 KB

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