example-glue.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright 2011-2017 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. static bool bar(float _width, float _maxWidth, float _height, const ImVec4& _color)
  12. {
  13. const ImGuiStyle& style = ImGui::GetStyle();
  14. ImVec4 hoveredColor(
  15. _color.x + _color.x*0.1f
  16. , _color.y + _color.y*0.1f
  17. , _color.z + _color.z*0.1f
  18. , _color.w + _color.w*0.1f
  19. );
  20. ImGui::PushStyleColor(ImGuiCol_Button, _color);
  21. ImGui::PushStyleColor(ImGuiCol_ButtonHovered, hoveredColor);
  22. ImGui::PushStyleColor(ImGuiCol_ButtonActive, _color);
  23. ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 0.0f);
  24. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, style.ItemSpacing.y) );
  25. bool itemHovered = false;
  26. ImGui::Button("", ImVec2(_width, _height) );
  27. itemHovered |= ImGui::IsItemHovered();
  28. ImGui::SameLine();
  29. ImGui::InvisibleButton("", ImVec2(_maxWidth-_width, _height) );
  30. itemHovered |= ImGui::IsItemHovered();
  31. ImGui::PopStyleVar(2);
  32. ImGui::PopStyleColor(3);
  33. return itemHovered;
  34. }
  35. void showExampleDialog(entry::AppI* _app, const char* _errorText)
  36. {
  37. char temp[1024];
  38. bx::snprintf(temp, BX_COUNTOF(temp), "Example: %s", _app->getName() );
  39. ImGui::SetNextWindowPos(
  40. ImVec2(10.0f, 50.0f)
  41. , ImGuiSetCond_FirstUseEver
  42. );
  43. ImGui::Begin(temp
  44. , NULL
  45. , ImVec2(256.0f, 200.0f)
  46. );
  47. ImGui::TextWrapped("%s", _app->getDescription() );
  48. ImGui::Separator();
  49. if (NULL != _errorText)
  50. {
  51. const int64_t now = bx::getHPCounter();
  52. const int64_t freq = bx::getHPFrequency();
  53. const float time = float(now%freq)/float(freq);
  54. bool blink = time > 0.5f;
  55. ImGui::PushStyleColor(ImGuiCol_Text
  56. , blink
  57. ? ImVec4(1.0, 0.0, 0.0, 1.0)
  58. : ImVec4(1.0, 1.0, 1.0, 1.0)
  59. );
  60. ImGui::TextWrapped("%s", _errorText);
  61. ImGui::Separator();
  62. ImGui::PopStyleColor();
  63. }
  64. {
  65. uint32_t num = entry::getNumApps();
  66. const char** items = (const char**)alloca(num*sizeof(void*) );
  67. uint32_t ii = 0;
  68. int32_t current = 0;
  69. for (entry::AppI* app = entry::getFirstApp(); NULL != app; app = app->getNext() )
  70. {
  71. if (app == _app)
  72. {
  73. current = ii;
  74. }
  75. items[ii++] = app->getName();
  76. }
  77. if (1 < num
  78. && ImGui::Combo("Example", &current, items, num) )
  79. {
  80. char command[1024];
  81. bx::snprintf(command, BX_COUNTOF(command), "app restart %s", items[current]);
  82. cmdExec(command);
  83. }
  84. const bgfx::Caps* caps = bgfx::getCaps();
  85. if (0 != (caps->supported & BGFX_CAPS_GRAPHICS_DEBUGGER) )
  86. {
  87. ImGui::SameLine();
  88. ImGui::Text(ICON_FA_SNOWFLAKE_O);
  89. }
  90. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(3.0f, 3.0f) );
  91. if (ImGui::Button(ICON_FA_REPEAT " Restart" ) )
  92. {
  93. cmdExec("app restart");
  94. }
  95. if (1 < entry::getNumApps() )
  96. {
  97. ImGui::SameLine();
  98. if (ImGui::Button(ICON_KI_PREVIOUS " Prev") )
  99. {
  100. cmdExec("app restart prev");
  101. }
  102. ImGui::SameLine();
  103. if (ImGui::Button(ICON_KI_NEXT " Next") )
  104. {
  105. cmdExec("app restart next");
  106. }
  107. }
  108. ImGui::SameLine();
  109. if (ImGui::Button(ICON_KI_EXIT " Exit") )
  110. {
  111. cmdExec("exit");
  112. }
  113. ImGui::PopStyleVar();
  114. }
  115. #if 0
  116. {
  117. bgfx::RendererType::Enum supportedRenderers[bgfx::RendererType::Count];
  118. uint8_t num = bgfx::getSupportedRenderers(BX_COUNTOF(supportedRenderers), supportedRenderers);
  119. const bgfx::Caps* caps = bgfx::getCaps();
  120. const char* items[bgfx::RendererType::Count];
  121. int32_t current = 0;
  122. for (uint8_t ii = 0; ii < num; ++ii)
  123. {
  124. items[ii] = bgfx::getRendererName(supportedRenderers[ii]);
  125. if (supportedRenderers[ii] == caps->rendererType)
  126. {
  127. current = ii;
  128. }
  129. }
  130. if (ImGui::Combo("Renderer", &current, items, num) )
  131. {
  132. cmdExec("app restart");
  133. }
  134. num = caps->numGPUs;
  135. if (0 != num)
  136. {
  137. current = 0;
  138. for (uint8_t ii = 0; ii < num; ++ii)
  139. {
  140. const bgfx::Caps::GPU& gpu = caps->gpu[ii];
  141. items[ii] = gpu.vendorId == BGFX_PCI_ID_AMD ? "AMD"
  142. : gpu.vendorId == BGFX_PCI_ID_INTEL ? "Intel"
  143. : gpu.vendorId == BGFX_PCI_ID_NVIDIA ? "nVidia"
  144. : "Unknown?"
  145. ;
  146. if (caps->vendorId == gpu.vendorId
  147. && caps->deviceId == gpu.deviceId)
  148. {
  149. current = ii;
  150. }
  151. }
  152. if (ImGui::Combo("GPU", &current, items, num) )
  153. {
  154. cmdExec("app restart");
  155. }
  156. }
  157. }
  158. #endif // 0
  159. const bgfx::Stats* stats = bgfx::getStats();
  160. const double toMsCpu = 1000.0/stats->cpuTimerFreq;
  161. const double toMsGpu = 1000.0/stats->gpuTimerFreq;
  162. ImGui::Text("Frame %0.3f"
  163. , double(stats->cpuTimeFrame)*toMsCpu
  164. );
  165. ImGui::Text("Submit CPU %0.3f, GPU %0.3f (L: %d)"
  166. , double(stats->cpuTimeEnd - stats->cpuTimeBegin)*toMsCpu
  167. , double(stats->gpuTimeEnd - stats->gpuTimeBegin)*toMsGpu
  168. , stats->maxGpuLatency
  169. );
  170. if (-INT64_MAX != stats->gpuMemoryUsed)
  171. {
  172. char tmp0[64];
  173. bx::prettify(tmp0, BX_COUNTOF(tmp0), stats->gpuMemoryUsed);
  174. char tmp1[64];
  175. bx::prettify(tmp1, BX_COUNTOF(tmp1), stats->gpuMemoryMax);
  176. ImGui::Text("GPU mem: %s / %s", tmp0, tmp1);
  177. }
  178. if (0 != stats->numViews)
  179. {
  180. if (ImGui::CollapsingHeader(ICON_FA_CLOCK_O " Profiler") )
  181. {
  182. if (ImGui::BeginChild("##view_profiler", ImVec2(0.0f, 0.0f) ) )
  183. {
  184. ImGui::PushFont(ImGui::Font::Mono);
  185. ImVec4 cpuColor(0.5f, 1.0f, 0.5f, 1.0f);
  186. ImVec4 gpuColor(0.5f, 0.5f, 1.0f, 1.0f);
  187. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  188. const float itemHeightWithSpacing = ImGui::GetItemsLineHeightWithSpacing();
  189. const double toCpuMs = 1000.0/double(stats->cpuTimerFreq);
  190. const double toGpuMs = 1000.0/double(stats->gpuTimerFreq);
  191. const float scale = 3.0f;
  192. if (ImGui::ListBoxHeader("Encoders", ImVec2(ImGui::GetWindowWidth(), stats->numEncoders*itemHeightWithSpacing) ) )
  193. {
  194. ImGuiListClipper clipper(stats->numEncoders, itemHeight);
  195. while (clipper.Step() )
  196. {
  197. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  198. {
  199. const bgfx::EncoderStats& encoderStats = stats->encoderStats[pos];
  200. ImGui::Text("%3d", pos);
  201. ImGui::SameLine(64.0f);
  202. if (encoderStats.cpuTimeEnd-encoderStats.cpuTimeBegin < 0)
  203. {
  204. DBG("x");
  205. }
  206. const float maxWidth = 30.0f*scale;
  207. const float cpuMs = float( (encoderStats.cpuTimeEnd-encoderStats.cpuTimeBegin)*toCpuMs);
  208. const float cpuWidth = bx::fclamp(cpuMs*scale, 1.0f, maxWidth);
  209. if (bar(cpuWidth, maxWidth, itemHeight, cpuColor) )
  210. {
  211. ImGui::SetTooltip("Encoder %d, CPU: %f [ms]"
  212. , pos
  213. , cpuMs
  214. );
  215. }
  216. }
  217. }
  218. ImGui::ListBoxFooter();
  219. }
  220. ImGui::Separator();
  221. if (ImGui::ListBoxHeader("Views", ImVec2(ImGui::GetWindowWidth(), stats->numViews*itemHeightWithSpacing) ) )
  222. {
  223. ImGuiListClipper clipper(stats->numViews, itemHeight);
  224. while (clipper.Step() )
  225. {
  226. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  227. {
  228. const bgfx::ViewStats& viewStats = stats->viewStats[pos];
  229. ImGui::Text("%3d %3d %s", pos, viewStats.view, viewStats.name);
  230. const float maxWidth = 30.0f*scale;
  231. const float cpuWidth = bx::fclamp(float(viewStats.cpuTimeElapsed*toCpuMs)*scale, 1.0f, maxWidth);
  232. const float gpuWidth = bx::fclamp(float(viewStats.gpuTimeElapsed*toGpuMs)*scale, 1.0f, maxWidth);
  233. ImGui::SameLine(64.0f);
  234. if (bar(cpuWidth, maxWidth, itemHeight, cpuColor) )
  235. {
  236. ImGui::SetTooltip("View %d \"%s\", CPU: %f [ms]"
  237. , pos
  238. , viewStats.name
  239. , viewStats.cpuTimeElapsed*toCpuMs
  240. );
  241. }
  242. ImGui::SameLine();
  243. if (bar(gpuWidth, maxWidth, itemHeight, gpuColor) )
  244. {
  245. ImGui::SetTooltip("View: %d \"%s\", GPU: %f [ms]"
  246. , pos
  247. , viewStats.name
  248. , viewStats.gpuTimeElapsed*toGpuMs
  249. );
  250. }
  251. }
  252. }
  253. ImGui::ListBoxFooter();
  254. }
  255. ImGui::PopFont();
  256. }
  257. ImGui::EndChild();
  258. }
  259. }
  260. ImGui::End();
  261. }