example-glue.cpp 7.8 KB

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