example-glue.cpp 13 KB

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