example-glue.cpp 7.4 KB

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