example-glue.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. #include <string>
  12. #define SAMPLE_COUNT 100
  13. struct SampleData
  14. {
  15. void pushSample(float value)
  16. {
  17. values[offset] = value;
  18. offset = (offset+1) % SAMPLE_COUNT;
  19. }
  20. float getMin() const
  21. {
  22. float minValue = FLT_MAX;
  23. for(size_t i = 0; i < SAMPLE_COUNT; ++i)
  24. {
  25. minValue = bx::min(minValue, values[i]);
  26. }
  27. return minValue;
  28. }
  29. float getMax() const
  30. {
  31. float maxValue = -FLT_MAX;
  32. for(size_t i = 0; i < SAMPLE_COUNT; ++i)
  33. {
  34. maxValue = bx::max(maxValue, values[i]);
  35. }
  36. return maxValue;
  37. }
  38. float getAvg() const
  39. {
  40. float avgValue = 0;
  41. for(size_t i = 0; i < SAMPLE_COUNT; ++i)
  42. {
  43. avgValue += values[i];
  44. }
  45. return avgValue / SAMPLE_COUNT;
  46. }
  47. int offset = 0;
  48. float values[SAMPLE_COUNT] = {0};
  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(_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, 220.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. const float frameMsAvg = s_frameTime.getAvg();
  228. std::string frameTextOverlay;
  229. bx::stringPrintf(frameTextOverlay, "%s%.3fms, %s%.3fms\nAvg: %.3fms, %.1f FPS"
  230. , ICON_FA_ARROW_UP, s_frameTime.getMax()
  231. , ICON_FA_ARROW_DOWN, s_frameTime.getMin()
  232. , frameMsAvg, 1000.0/frameMsAvg
  233. );
  234. ImGui::PushStyleColor(ImGuiCol_PlotHistogram, ImColor(0.0f,0.5f,0.15f,1.0f).Value);
  235. ImGui::PlotHistogram("Frame", s_frameTime.values
  236. , SAMPLE_COUNT, s_frameTime.offset
  237. , frameTextOverlay.c_str()
  238. , 0.0f, 60.0f, ImVec2(0, 45)
  239. );
  240. ImGui::PopStyleColor();
  241. ImGui::Text("Submit CPU %0.3f, GPU %0.3f (L: %d)"
  242. , double(stats->cpuTimeEnd - stats->cpuTimeBegin)*toMsCpu
  243. , double(stats->gpuTimeEnd - stats->gpuTimeBegin)*toMsGpu
  244. , stats->maxGpuLatency
  245. );
  246. if (-INT64_MAX != stats->gpuMemoryUsed)
  247. {
  248. char tmp0[64];
  249. bx::prettify(tmp0, BX_COUNTOF(tmp0), stats->gpuMemoryUsed);
  250. char tmp1[64];
  251. bx::prettify(tmp1, BX_COUNTOF(tmp1), stats->gpuMemoryMax);
  252. ImGui::Text("GPU mem: %s / %s", tmp0, tmp1);
  253. }
  254. if (s_showStats)
  255. {
  256. ImGui::SetNextWindowSize(
  257. ImVec2(300.0f, 500.0f)
  258. , ImGuiCond_FirstUseEver
  259. );
  260. if (ImGui::Begin(ICON_FA_BAR_CHART " Stats", &s_showStats) )
  261. {
  262. if (ImGui::CollapsingHeader(ICON_FA_PUZZLE_PIECE " Resources") )
  263. {
  264. const bgfx::Caps* caps = bgfx::getCaps();
  265. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  266. const float maxWidth = 90.0f;
  267. ImGui::PushFont(ImGui::Font::Mono);
  268. ImGui::Text("Res: Num / Max");
  269. resourceBar("DIB", "Dynamic index buffers", stats->numDynamicIndexBuffers, caps->limits.maxDynamicIndexBuffers, maxWidth, itemHeight);
  270. resourceBar("DVB", "Dynamic vertex buffers", stats->numDynamicVertexBuffers, caps->limits.maxDynamicVertexBuffers, maxWidth, itemHeight);
  271. resourceBar(" FB", "Frame buffers", stats->numFrameBuffers, caps->limits.maxFrameBuffers, maxWidth, itemHeight);
  272. resourceBar(" IB", "Index buffers", stats->numIndexBuffers, caps->limits.maxIndexBuffers, maxWidth, itemHeight);
  273. resourceBar(" OQ", "Occlusion queries", stats->numOcclusionQueries, caps->limits.maxOcclusionQueries, maxWidth, itemHeight);
  274. resourceBar(" P", "Programs", stats->numPrograms, caps->limits.maxPrograms, maxWidth, itemHeight);
  275. resourceBar(" S", "Shaders", stats->numShaders, caps->limits.maxShaders, maxWidth, itemHeight);
  276. resourceBar(" T", "Textures", stats->numTextures, caps->limits.maxTextures, maxWidth, itemHeight);
  277. resourceBar(" U", "Uniforms", stats->numUniforms, caps->limits.maxUniforms, maxWidth, itemHeight);
  278. resourceBar(" VB", "Vertex buffers", stats->numVertexBuffers, caps->limits.maxVertexBuffers, maxWidth, itemHeight);
  279. resourceBar(" VD", "Vertex declarations", stats->numVertexDecls, caps->limits.maxVertexDecls, maxWidth, itemHeight);
  280. ImGui::PopFont();
  281. }
  282. if (ImGui::CollapsingHeader(ICON_FA_CLOCK_O " Profiler") )
  283. {
  284. if (0 == stats->numViews)
  285. {
  286. ImGui::Text("Profiler is not enabled.");
  287. }
  288. else
  289. {
  290. if (ImGui::BeginChild("##view_profiler", ImVec2(0.0f, 0.0f) ) )
  291. {
  292. ImGui::PushFont(ImGui::Font::Mono);
  293. ImVec4 cpuColor(0.5f, 1.0f, 0.5f, 1.0f);
  294. ImVec4 gpuColor(0.5f, 0.5f, 1.0f, 1.0f);
  295. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  296. const float itemHeightWithSpacing = ImGui::GetFrameHeightWithSpacing();
  297. const double toCpuMs = 1000.0/double(stats->cpuTimerFreq);
  298. const double toGpuMs = 1000.0/double(stats->gpuTimerFreq);
  299. const float scale = 3.0f;
  300. if (ImGui::ListBoxHeader("Encoders", ImVec2(ImGui::GetWindowWidth(), stats->numEncoders*itemHeightWithSpacing) ) )
  301. {
  302. ImGuiListClipper clipper(stats->numEncoders, itemHeight);
  303. while (clipper.Step() )
  304. {
  305. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  306. {
  307. const bgfx::EncoderStats& encoderStats = stats->encoderStats[pos];
  308. ImGui::Text("%3d", pos);
  309. ImGui::SameLine(64.0f);
  310. const float maxWidth = 30.0f*scale;
  311. const float cpuMs = float( (encoderStats.cpuTimeEnd-encoderStats.cpuTimeBegin)*toCpuMs);
  312. const float cpuWidth = bx::clamp(cpuMs*scale, 1.0f, maxWidth);
  313. if (bar(cpuWidth, maxWidth, itemHeight, cpuColor) )
  314. {
  315. ImGui::SetTooltip("Encoder %d, CPU: %f [ms]"
  316. , pos
  317. , cpuMs
  318. );
  319. }
  320. }
  321. }
  322. ImGui::ListBoxFooter();
  323. }
  324. ImGui::Separator();
  325. if (ImGui::ListBoxHeader("Views", ImVec2(ImGui::GetWindowWidth(), stats->numViews*itemHeightWithSpacing) ) )
  326. {
  327. ImGuiListClipper clipper(stats->numViews, itemHeight);
  328. while (clipper.Step() )
  329. {
  330. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  331. {
  332. const bgfx::ViewStats& viewStats = stats->viewStats[pos];
  333. ImGui::Text("%3d %3d %s", pos, viewStats.view, viewStats.name);
  334. const float maxWidth = 30.0f*scale;
  335. const float cpuWidth = bx::clamp(float(viewStats.cpuTimeElapsed*toCpuMs)*scale, 1.0f, maxWidth);
  336. const float gpuWidth = bx::clamp(float(viewStats.gpuTimeElapsed*toGpuMs)*scale, 1.0f, maxWidth);
  337. ImGui::SameLine(64.0f);
  338. if (bar(cpuWidth, maxWidth, itemHeight, cpuColor) )
  339. {
  340. ImGui::SetTooltip("View %d \"%s\", CPU: %f [ms]"
  341. , pos
  342. , viewStats.name
  343. , viewStats.cpuTimeElapsed*toCpuMs
  344. );
  345. }
  346. ImGui::SameLine();
  347. if (bar(gpuWidth, maxWidth, itemHeight, gpuColor) )
  348. {
  349. ImGui::SetTooltip("View: %d \"%s\", GPU: %f [ms]"
  350. , pos
  351. , viewStats.name
  352. , viewStats.gpuTimeElapsed*toGpuMs
  353. );
  354. }
  355. }
  356. }
  357. ImGui::ListBoxFooter();
  358. }
  359. ImGui::PopFont();
  360. }
  361. ImGui::EndChild();
  362. }
  363. }
  364. }
  365. ImGui::End();
  366. }
  367. ImGui::End();
  368. }