example-glue.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "imgui/imgui.h"
  6. #include "entry/entry.h"
  7. #include "entry/cmd.h"
  8. #include "entry/dialog.h"
  9. #include <bx/string.h>
  10. #include <bx/timer.h>
  11. #include <bx/math.h>
  12. struct SampleData
  13. {
  14. static constexpr uint32_t kNumSamples = 100;
  15. SampleData()
  16. {
  17. reset();
  18. }
  19. void reset()
  20. {
  21. m_offset = 0;
  22. bx::memSet(m_values, 0, sizeof(m_values) );
  23. m_min = 0.0f;
  24. m_max = 0.0f;
  25. m_avg = 0.0f;
  26. }
  27. void pushSample(float value)
  28. {
  29. m_values[m_offset] = value;
  30. m_offset = (m_offset+1) % kNumSamples;
  31. float min = bx::max<float>();
  32. float max = bx::min<float>();
  33. float avg = 0.0f;
  34. for (uint32_t ii = 0; ii < kNumSamples; ++ii)
  35. {
  36. const float val = m_values[ii];
  37. min = bx::min(min, val);
  38. max = bx::max(max, val);
  39. avg += val;
  40. }
  41. m_min = min;
  42. m_max = max;
  43. m_avg = avg / kNumSamples;
  44. }
  45. int32_t m_offset;
  46. float m_values[kNumSamples];
  47. float m_min;
  48. float m_max;
  49. float m_avg;
  50. };
  51. static SampleData s_frameTime;
  52. static bool bar(const char* _name, float _width, float _maxWidth, float _height, const ImVec4& _color)
  53. {
  54. ImGui::PushID(_name);
  55. const ImGuiStyle& style = ImGui::GetStyle();
  56. ImVec4 hoveredColor(
  57. _color.x + _color.x*0.1f
  58. , _color.y + _color.y*0.1f
  59. , _color.z + _color.z*0.1f
  60. , _color.w + _color.w*0.1f
  61. );
  62. ImGui::PushStyleColor(ImGuiCol_Button, _color);
  63. ImGui::PushStyleColor(ImGuiCol_ButtonHovered, hoveredColor);
  64. ImGui::PushStyleColor(ImGuiCol_ButtonActive, _color);
  65. ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 0.0f);
  66. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, style.ItemSpacing.y) );
  67. bool itemHovered = false;
  68. ImGui::Button("##button", ImVec2(_width, _height) );
  69. itemHovered |= ImGui::IsItemHovered();
  70. ImGui::SameLine();
  71. ImGui::InvisibleButton("##invisible_button", ImVec2(bx::max(1.0f, _maxWidth-_width), _height) );
  72. itemHovered |= ImGui::IsItemHovered();
  73. ImGui::PopStyleVar(2);
  74. ImGui::PopStyleColor(3);
  75. ImGui::PopID();
  76. return itemHovered;
  77. }
  78. static constexpr ImVec4 kResourceColor(0.5f, 0.5f, 0.5f, 1.0f);
  79. static void resourceBar(const char* _name, const char* _tooltip, uint32_t _num, uint32_t _max, float _maxWidth, float _height)
  80. {
  81. ImGui::PushID(_name);
  82. bool itemHovered = false;
  83. ImGui::Text("%s: %4d / %4d", _name, _num, _max);
  84. itemHovered |= ImGui::IsItemHovered();
  85. ImGui::SameLine();
  86. const float percentage = float(_num)/float(_max);
  87. itemHovered |= bar("Resource", bx::max(1.0f, percentage * _maxWidth), _maxWidth, _height, kResourceColor);
  88. ImGui::SameLine();
  89. ImGui::Text("%5.2f%%", percentage*100.0f);
  90. if (itemHovered)
  91. {
  92. ImGui::SetTooltip("%s %5.2f%%"
  93. , _tooltip
  94. , percentage*100.0f
  95. );
  96. }
  97. ImGui::PopID();
  98. }
  99. static bool s_showStats = false;
  100. void showExampleDialog(entry::AppI* _app, const char* _errorText)
  101. {
  102. char temp[1024];
  103. bx::snprintf(temp, BX_COUNTOF(temp), "Example: %s", _app->getName() );
  104. ImGui::SetNextWindowPos(
  105. ImVec2(10.0f, 50.0f)
  106. , ImGuiCond_FirstUseEver
  107. );
  108. ImGui::SetNextWindowSize(
  109. ImVec2(300.0f, 210.0f)
  110. , ImGuiCond_FirstUseEver
  111. );
  112. ImGui::Begin(temp);
  113. ImGui::TextWrapped("%s", _app->getDescription() );
  114. bx::StringView url = _app->getUrl();
  115. if (!url.isEmpty() )
  116. {
  117. ImGui::SameLine();
  118. if (ImGui::SmallButton(ICON_FA_LINK) )
  119. {
  120. openUrl(url);
  121. }
  122. else if (ImGui::IsItemHovered() )
  123. {
  124. ImGui::SetTooltip("Documentation: %.*s", url.getLength(), url.getPtr() );
  125. }
  126. }
  127. ImGui::Separator();
  128. if (NULL != _errorText)
  129. {
  130. const int64_t now = bx::getHPCounter();
  131. const int64_t freq = bx::getHPFrequency();
  132. const float time = float(now%freq)/float(freq);
  133. bool blink = time > 0.5f;
  134. ImGui::PushStyleColor(ImGuiCol_Text
  135. , blink
  136. ? ImVec4(1.0, 0.0, 0.0, 1.0)
  137. : ImVec4(1.0, 1.0, 1.0, 1.0)
  138. );
  139. ImGui::TextWrapped("%s", _errorText);
  140. ImGui::Separator();
  141. ImGui::PopStyleColor();
  142. }
  143. {
  144. uint32_t num = entry::getNumApps();
  145. const char** items = (const char**)BX_STACK_ALLOC(num*sizeof(void*) );
  146. uint32_t ii = 0;
  147. int32_t current = 0;
  148. for (entry::AppI* app = entry::getFirstApp(); NULL != app; app = app->getNext() )
  149. {
  150. if (app == _app)
  151. {
  152. current = ii;
  153. }
  154. items[ii++] = app->getName();
  155. }
  156. if (1 < num
  157. && ImGui::Combo("Example", &current, items, num) )
  158. {
  159. char command[1024];
  160. bx::snprintf(command, BX_COUNTOF(command), "app restart %s", items[current]);
  161. cmdExec(command);
  162. }
  163. const bgfx::Caps* caps = bgfx::getCaps();
  164. if (0 != (caps->supported & BGFX_CAPS_GRAPHICS_DEBUGGER) )
  165. {
  166. ImGui::SameLine();
  167. ImGui::Text(ICON_FA_SNOWFLAKE_O);
  168. }
  169. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(3.0f, 3.0f) );
  170. if (ImGui::Button(ICON_FA_REPEAT " Restart" ) )
  171. {
  172. cmdExec("app restart");
  173. }
  174. if (1 < entry::getNumApps() )
  175. {
  176. ImGui::SameLine();
  177. if (ImGui::Button(ICON_KI_PREVIOUS " Prev") )
  178. {
  179. cmdExec("app restart prev");
  180. }
  181. ImGui::SameLine();
  182. if (ImGui::Button(ICON_KI_NEXT " Next") )
  183. {
  184. cmdExec("app restart next");
  185. }
  186. }
  187. ImGui::SameLine();
  188. if (ImGui::Button(ICON_KI_EXIT " Exit") )
  189. {
  190. cmdExec("exit");
  191. }
  192. ImGui::SameLine();
  193. s_showStats ^= ImGui::Button(ICON_FA_BAR_CHART);
  194. ImGui::PopStyleVar();
  195. }
  196. #if 0
  197. {
  198. bgfx::RendererType::Enum supportedRenderers[bgfx::RendererType::Count];
  199. uint8_t num = bgfx::getSupportedRenderers(BX_COUNTOF(supportedRenderers), supportedRenderers);
  200. const bgfx::Caps* caps = bgfx::getCaps();
  201. const char* items[bgfx::RendererType::Count];
  202. int32_t current = 0;
  203. for (uint8_t ii = 0; ii < num; ++ii)
  204. {
  205. items[ii] = bgfx::getRendererName(supportedRenderers[ii]);
  206. if (supportedRenderers[ii] == caps->rendererType)
  207. {
  208. current = ii;
  209. }
  210. }
  211. if (ImGui::Combo("Renderer", &current, items, num) )
  212. {
  213. cmdExec("app restart");
  214. }
  215. num = caps->numGPUs;
  216. if (0 != num)
  217. {
  218. current = 0;
  219. for (uint8_t ii = 0; ii < num; ++ii)
  220. {
  221. const bgfx::Caps::GPU& gpu = caps->gpu[ii];
  222. items[ii] = gpu.vendorId == BGFX_PCI_ID_AMD ? "AMD"
  223. : gpu.vendorId == BGFX_PCI_ID_INTEL ? "Intel"
  224. : gpu.vendorId == BGFX_PCI_ID_NVIDIA ? "nVidia"
  225. : "Unknown?"
  226. ;
  227. if (caps->vendorId == gpu.vendorId
  228. && caps->deviceId == gpu.deviceId)
  229. {
  230. current = ii;
  231. }
  232. }
  233. if (ImGui::Combo("GPU", &current, items, num) )
  234. {
  235. cmdExec("app restart");
  236. }
  237. }
  238. }
  239. #else
  240. ImGui::Text("Renderer: %s", bgfx::getRendererName(bgfx::getRendererType()));
  241. #endif // 0
  242. const bgfx::Stats* stats = bgfx::getStats();
  243. const double toMsCpu = 1000.0/stats->cpuTimerFreq;
  244. const double toMsGpu = 1000.0/stats->gpuTimerFreq;
  245. const double frameMs = double(stats->cpuTimeFrame)*toMsCpu;
  246. s_frameTime.pushSample(float(frameMs) );
  247. char frameTextOverlay[256];
  248. bx::snprintf(frameTextOverlay, BX_COUNTOF(frameTextOverlay), "%s%.3fms, %s%.3fms\nAvg: %.3fms, %.1f FPS"
  249. , ICON_FA_ARROW_DOWN
  250. , s_frameTime.m_min
  251. , ICON_FA_ARROW_UP
  252. , s_frameTime.m_max
  253. , s_frameTime.m_avg
  254. , 1000.0f/s_frameTime.m_avg
  255. );
  256. ImGui::PushStyleColor(ImGuiCol_PlotHistogram, ImColor(0.0f, 0.5f, 0.15f, 1.0f).Value);
  257. ImGui::PlotHistogram("Frame"
  258. , s_frameTime.m_values
  259. , SampleData::kNumSamples
  260. , s_frameTime.m_offset
  261. , frameTextOverlay
  262. , 0.0f
  263. , 60.0f
  264. , ImVec2(0.0f, 45.0f)
  265. );
  266. ImGui::PopStyleColor();
  267. ImGui::Text("Submit CPU %0.3f, GPU %0.3f (L: %d)"
  268. , double(stats->cpuTimeEnd - stats->cpuTimeBegin)*toMsCpu
  269. , double(stats->gpuTimeEnd - stats->gpuTimeBegin)*toMsGpu
  270. , stats->maxGpuLatency
  271. );
  272. if (-INT64_MAX != stats->gpuMemoryUsed)
  273. {
  274. char tmp0[64];
  275. bx::prettify(tmp0, BX_COUNTOF(tmp0), stats->gpuMemoryUsed);
  276. char tmp1[64];
  277. bx::prettify(tmp1, BX_COUNTOF(tmp1), stats->gpuMemoryMax);
  278. ImGui::Text("GPU mem: %s / %s", tmp0, tmp1);
  279. }
  280. if (s_showStats)
  281. {
  282. ImGui::SetNextWindowSize(
  283. ImVec2(300.0f, 500.0f)
  284. , ImGuiCond_FirstUseEver
  285. );
  286. if (ImGui::Begin(ICON_FA_BAR_CHART " Stats", &s_showStats) )
  287. {
  288. if (ImGui::CollapsingHeader(ICON_FA_PUZZLE_PIECE " Resources") )
  289. {
  290. const bgfx::Caps* caps = bgfx::getCaps();
  291. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  292. const float maxWidth = 90.0f;
  293. ImGui::PushFont(ImGui::Font::Mono);
  294. ImGui::Text("Res: Num / Max");
  295. resourceBar("DIB", "Dynamic index buffers", stats->numDynamicIndexBuffers, caps->limits.maxDynamicIndexBuffers, maxWidth, itemHeight);
  296. resourceBar("DVB", "Dynamic vertex buffers", stats->numDynamicVertexBuffers, caps->limits.maxDynamicVertexBuffers, maxWidth, itemHeight);
  297. resourceBar(" FB", "Frame buffers", stats->numFrameBuffers, caps->limits.maxFrameBuffers, maxWidth, itemHeight);
  298. resourceBar(" IB", "Index buffers", stats->numIndexBuffers, caps->limits.maxIndexBuffers, maxWidth, itemHeight);
  299. resourceBar(" OQ", "Occlusion queries", stats->numOcclusionQueries, caps->limits.maxOcclusionQueries, maxWidth, itemHeight);
  300. resourceBar(" P", "Programs", stats->numPrograms, caps->limits.maxPrograms, maxWidth, itemHeight);
  301. resourceBar(" S", "Shaders", stats->numShaders, caps->limits.maxShaders, maxWidth, itemHeight);
  302. resourceBar(" T", "Textures", stats->numTextures, caps->limits.maxTextures, maxWidth, itemHeight);
  303. resourceBar(" U", "Uniforms", stats->numUniforms, caps->limits.maxUniforms, maxWidth, itemHeight);
  304. resourceBar(" VB", "Vertex buffers", stats->numVertexBuffers, caps->limits.maxVertexBuffers, maxWidth, itemHeight);
  305. resourceBar(" VL", "Vertex layouts", stats->numVertexLayouts, caps->limits.maxVertexLayouts, maxWidth, itemHeight);
  306. ImGui::PopFont();
  307. }
  308. if (ImGui::CollapsingHeader(ICON_FA_CLOCK_O " Profiler") )
  309. {
  310. if (0 == stats->numViews)
  311. {
  312. ImGui::Text("Profiler is not enabled.");
  313. }
  314. else
  315. {
  316. if (ImGui::BeginChild("##view_profiler", ImVec2(0.0f, 0.0f) ) )
  317. {
  318. ImGui::PushFont(ImGui::Font::Mono);
  319. ImVec4 cpuColor(0.5f, 1.0f, 0.5f, 1.0f);
  320. ImVec4 gpuColor(0.5f, 0.5f, 1.0f, 1.0f);
  321. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  322. const float itemHeightWithSpacing = ImGui::GetFrameHeightWithSpacing();
  323. const double toCpuMs = 1000.0/double(stats->cpuTimerFreq);
  324. const double toGpuMs = 1000.0/double(stats->gpuTimerFreq);
  325. const float scale = 3.0f;
  326. if (ImGui::BeginListBox("Encoders", ImVec2(ImGui::GetWindowWidth(), stats->numEncoders*itemHeightWithSpacing) ) )
  327. {
  328. ImGuiListClipper clipper;
  329. clipper.Begin(stats->numEncoders, itemHeight);
  330. while (clipper.Step() )
  331. {
  332. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  333. {
  334. const bgfx::EncoderStats& encoderStats = stats->encoderStats[pos];
  335. ImGui::Text("%3d", pos);
  336. ImGui::SameLine(64.0f);
  337. const float maxWidth = 30.0f*scale;
  338. const float cpuMs = float( (encoderStats.cpuTimeEnd-encoderStats.cpuTimeBegin)*toCpuMs);
  339. const float cpuWidth = bx::clamp(cpuMs*scale, 1.0f, maxWidth);
  340. if (bar("CPU", cpuWidth, maxWidth, itemHeight, cpuColor))
  341. {
  342. ImGui::SetTooltip("Encoder %d, CPU: %f [ms]"
  343. , pos
  344. , cpuMs
  345. );
  346. }
  347. }
  348. }
  349. ImGui::EndListBox();
  350. }
  351. ImGui::Separator();
  352. if (ImGui::BeginListBox("Views", ImVec2(ImGui::GetWindowWidth(), stats->numViews*itemHeightWithSpacing) ) )
  353. {
  354. ImGuiListClipper clipper;
  355. clipper.Begin(stats->numViews, itemHeight);
  356. while (clipper.Step() )
  357. {
  358. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  359. {
  360. const bgfx::ViewStats& viewStats = stats->viewStats[pos];
  361. ImGui::Text("%3d %3d %s", pos, viewStats.view, viewStats.name);
  362. const float maxWidth = 30.0f*scale;
  363. const float cpuTimeElapsed = float((viewStats.cpuTimeEnd - viewStats.cpuTimeBegin) * toCpuMs);
  364. const float gpuTimeElapsed = float((viewStats.gpuTimeEnd - viewStats.gpuTimeBegin) * toGpuMs);
  365. const float cpuWidth = bx::clamp(cpuTimeElapsed*scale, 1.0f, maxWidth);
  366. const float gpuWidth = bx::clamp(gpuTimeElapsed*scale, 1.0f, maxWidth);
  367. ImGui::SameLine(64.0f);
  368. ImGui::PushID(viewStats.name);
  369. if (bar("CPU", cpuWidth, maxWidth, itemHeight, cpuColor))
  370. {
  371. ImGui::SetTooltip("View %d \"%s\", CPU: %f [ms]"
  372. , pos
  373. , viewStats.name
  374. , cpuTimeElapsed
  375. );
  376. }
  377. ImGui::SameLine();
  378. if (bar("GPU", gpuWidth, maxWidth, itemHeight, gpuColor) )
  379. {
  380. ImGui::SetTooltip("View: %d \"%s\", GPU: %f [ms]"
  381. , pos
  382. , viewStats.name
  383. , gpuTimeElapsed
  384. );
  385. }
  386. ImGui::PopID();
  387. }
  388. }
  389. ImGui::EndListBox();
  390. }
  391. ImGui::PopFont();
  392. }
  393. ImGui::EndChild();
  394. }
  395. }
  396. }
  397. ImGui::End();
  398. }
  399. ImGui::End();
  400. }