example-glue.cpp 12 KB

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