example-glue.cpp 6.4 KB

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