example-glue.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. , 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 (0 != stats->numViews)
  148. {
  149. if (ImGui::CollapsingHeader(ICON_FA_CLOCK_O " Profiler") )
  150. {
  151. if (ImGui::BeginChild("##view_profiler", ImVec2(0.0f, 0.0f) ) )
  152. {
  153. ImGui::PushFont(ImGui::Font::Mono);
  154. ImVec4 cpuColor(0.5f, 1.0f, 0.5f, 1.0f);
  155. ImVec4 gpuColor(0.5f, 0.5f, 1.0f, 1.0f);
  156. const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
  157. if (ImGui::ListBoxHeader("##empty", ImVec2(ImGui::GetWindowWidth(), stats->numViews*itemHeight) ) )
  158. {
  159. ImGuiListClipper clipper(stats->numViews, itemHeight);
  160. const double toCpuMs = 1000.0/double(stats->cpuTimerFreq);
  161. const double toGpuMs = 1000.0/double(stats->gpuTimerFreq);
  162. const float scale = 3.0f;
  163. while (clipper.Step() )
  164. {
  165. for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
  166. {
  167. const bgfx::ViewStats& viewStats = stats->viewStats[pos];
  168. ImGui::Text("%3d %3d %s", pos, viewStats.view, viewStats.name);
  169. ImGui::SameLine(64.0f);
  170. ImGui::PushStyleColor(ImGuiCol_Button, cpuColor);
  171. ImGui::PushStyleColor(ImGuiCol_ButtonHovered, cpuColor);
  172. ImGui::PushStyleColor(ImGuiCol_ButtonActive, cpuColor);
  173. const float maxWidth = 30.0f*scale;
  174. const float cpuWidth = bx::fclamp(float(viewStats.cpuTimeElapsed*toCpuMs)*scale, 1.0f, maxWidth);
  175. const float gpuWidth = bx::fclamp(float(viewStats.gpuTimeElapsed*toGpuMs)*scale, 1.0f, maxWidth);
  176. ImGui::Button("", ImVec2(cpuWidth, itemHeight) );
  177. if (ImGui::IsItemHovered() )
  178. {
  179. ImGui::SetTooltip("CPU: %f [ms]", viewStats.cpuTimeElapsed*toCpuMs);
  180. }
  181. ImGui::PopStyleColor(3);
  182. ImGui::SameLine();
  183. ImGui::InvisibleButton("", ImVec2(maxWidth-cpuWidth, itemHeight) );
  184. ImGui::SameLine();
  185. ImGui::PushStyleColor(ImGuiCol_Button, gpuColor);
  186. ImGui::PushStyleColor(ImGuiCol_ButtonHovered, gpuColor);
  187. ImGui::PushStyleColor(ImGuiCol_ButtonActive, gpuColor);
  188. ImGui::Button("", ImVec2(gpuWidth, itemHeight) );
  189. if (ImGui::IsItemHovered() )
  190. {
  191. ImGui::SetTooltip("GPU: %f [ms]", viewStats.gpuTimeElapsed*toGpuMs);
  192. }
  193. ImGui::PopStyleColor(3);
  194. }
  195. }
  196. ImGui::ListBoxFooter();
  197. }
  198. ImGui::PopFont();
  199. }
  200. ImGui::EndChild();
  201. }
  202. }
  203. ImGui::End();
  204. }