example-glue.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  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::kFloatMax;
  32. float max = -bx::kFloatMax;
  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. char tmp[1024];
  121. bx::snprintf(tmp, BX_COUNTOF(tmp), "Documentation: %.*s", url.getLength(), url.getPtr() );
  122. ImGui::SetTooltip(tmp);
  123. }
  124. }
  125. ImGui::Separator();
  126. if (NULL != _errorText)
  127. {
  128. const int64_t now = bx::getHPCounter();
  129. const int64_t freq = bx::getHPFrequency();
  130. const float time = float(now%freq)/float(freq);
  131. bool blink = time > 0.5f;
  132. ImGui::PushStyleColor(ImGuiCol_Text
  133. , blink
  134. ? ImVec4(1.0, 0.0, 0.0, 1.0)
  135. : ImVec4(1.0, 1.0, 1.0, 1.0)
  136. );
  137. ImGui::TextWrapped("%s", _errorText);
  138. ImGui::Separator();
  139. ImGui::PopStyleColor();
  140. }
  141. {
  142. uint32_t num = entry::getNumApps();
  143. const char** items = (const char**)alloca(num*sizeof(void*) );
  144. uint32_t ii = 0;
  145. int32_t current = 0;
  146. for (entry::AppI* app = entry::getFirstApp(); NULL != app; app = app->getNext() )
  147. {
  148. if (app == _app)
  149. {
  150. current = ii;
  151. }
  152. items[ii++] = app->getName();
  153. }
  154. if (1 < num
  155. && ImGui::Combo("Example", &current, items, num) )
  156. {
  157. char command[1024];
  158. bx::snprintf(command, BX_COUNTOF(command), "app restart %s", items[current]);
  159. cmdExec(command);
  160. }
  161. const bgfx::Caps* caps = bgfx::getCaps();
  162. if (0 != (caps->supported & BGFX_CAPS_GRAPHICS_DEBUGGER) )
  163. {
  164. ImGui::SameLine();
  165. ImGui::Text(ICON_FA_SNOWFLAKE_O);
  166. }
  167. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(3.0f, 3.0f) );
  168. if (ImGui::Button(ICON_FA_REPEAT " Restart" ) )
  169. {
  170. cmdExec("app restart");
  171. }
  172. if (1 < entry::getNumApps() )
  173. {
  174. ImGui::SameLine();
  175. if (ImGui::Button(ICON_KI_PREVIOUS " Prev") )
  176. {
  177. cmdExec("app restart prev");
  178. }
  179. ImGui::SameLine();
  180. if (ImGui::Button(ICON_KI_NEXT " Next") )
  181. {
  182. cmdExec("app restart next");
  183. }
  184. }
  185. ImGui::SameLine();
  186. if (ImGui::Button(ICON_KI_EXIT " Exit") )
  187. {
  188. cmdExec("exit");
  189. }
  190. ImGui::SameLine();
  191. s_showStats ^= ImGui::Button(ICON_FA_BAR_CHART);
  192. ImGui::PopStyleVar();
  193. }
  194. #if 0
  195. {
  196. bgfx::RendererType::Enum supportedRenderers[bgfx::RendererType::Count];
  197. uint8_t num = bgfx::getSupportedRenderers(BX_COUNTOF(supportedRenderers), supportedRenderers);
  198. const bgfx::Caps* caps = bgfx::getCaps();
  199. const char* items[bgfx::RendererType::Count];
  200. int32_t current = 0;
  201. for (uint8_t ii = 0; ii < num; ++ii)
  202. {
  203. items[ii] = bgfx::getRendererName(supportedRenderers[ii]);
  204. if (supportedRenderers[ii] == caps->rendererType)
  205. {
  206. current = ii;
  207. }
  208. }
  209. if (ImGui::Combo("Renderer", &current, items, num) )
  210. {
  211. cmdExec("app restart");
  212. }
  213. num = caps->numGPUs;
  214. if (0 != num)
  215. {
  216. current = 0;
  217. for (uint8_t ii = 0; ii < num; ++ii)
  218. {
  219. const bgfx::Caps::GPU& gpu = caps->gpu[ii];
  220. items[ii] = gpu.vendorId == BGFX_PCI_ID_AMD ? "AMD"
  221. : gpu.vendorId == BGFX_PCI_ID_INTEL ? "Intel"
  222. : gpu.vendorId == BGFX_PCI_ID_NVIDIA ? "nVidia"
  223. : "Unknown?"
  224. ;
  225. if (caps->vendorId == gpu.vendorId
  226. && caps->deviceId == gpu.deviceId)
  227. {
  228. current = ii;
  229. }
  230. }
  231. if (ImGui::Combo("GPU", &current, items, num) )
  232. {
  233. cmdExec("app restart");
  234. }
  235. }
  236. }
  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::ListBoxHeader("Encoders", ImVec2(ImGui::GetWindowWidth(), stats->numEncoders*itemHeightWithSpacing) ) )
  323. {
  324. ImGuiListClipper clipper(stats->numEncoders, itemHeight);
  325. while (clipper.Step() )
  326. {
  327. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  328. {
  329. const bgfx::EncoderStats& encoderStats = stats->encoderStats[pos];
  330. ImGui::Text("%3d", pos);
  331. ImGui::SameLine(64.0f);
  332. const float maxWidth = 30.0f*scale;
  333. const float cpuMs = float( (encoderStats.cpuTimeEnd-encoderStats.cpuTimeBegin)*toCpuMs);
  334. const float cpuWidth = bx::clamp(cpuMs*scale, 1.0f, maxWidth);
  335. if (bar(cpuWidth, maxWidth, itemHeight, cpuColor) )
  336. {
  337. ImGui::SetTooltip("Encoder %d, CPU: %f [ms]"
  338. , pos
  339. , cpuMs
  340. );
  341. }
  342. }
  343. }
  344. ImGui::ListBoxFooter();
  345. }
  346. ImGui::Separator();
  347. if (ImGui::ListBoxHeader("Views", ImVec2(ImGui::GetWindowWidth(), stats->numViews*itemHeightWithSpacing) ) )
  348. {
  349. ImGuiListClipper clipper(stats->numViews, itemHeight);
  350. while (clipper.Step() )
  351. {
  352. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  353. {
  354. const bgfx::ViewStats& viewStats = stats->viewStats[pos];
  355. ImGui::Text("%3d %3d %s", pos, viewStats.view, viewStats.name);
  356. const float maxWidth = 30.0f*scale;
  357. const float cpuWidth = bx::clamp(float(viewStats.cpuTimeElapsed*toCpuMs)*scale, 1.0f, maxWidth);
  358. const float gpuWidth = bx::clamp(float(viewStats.gpuTimeElapsed*toGpuMs)*scale, 1.0f, maxWidth);
  359. ImGui::SameLine(64.0f);
  360. if (bar(cpuWidth, maxWidth, itemHeight, cpuColor) )
  361. {
  362. ImGui::SetTooltip("View %d \"%s\", CPU: %f [ms]"
  363. , pos
  364. , viewStats.name
  365. , viewStats.cpuTimeElapsed*toCpuMs
  366. );
  367. }
  368. ImGui::SameLine();
  369. if (bar(gpuWidth, maxWidth, itemHeight, gpuColor) )
  370. {
  371. ImGui::SetTooltip("View: %d \"%s\", GPU: %f [ms]"
  372. , pos
  373. , viewStats.name
  374. , viewStats.gpuTimeElapsed*toGpuMs
  375. );
  376. }
  377. }
  378. }
  379. ImGui::ListBoxFooter();
  380. }
  381. ImGui::PopFont();
  382. }
  383. ImGui::EndChild();
  384. }
  385. }
  386. }
  387. ImGui::End();
  388. }
  389. ImGui::End();
  390. }