example-glue.cpp 12 KB

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