example-glue.cpp 12 KB

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