UIRenderer.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // The MIT License(MIT)
  2. //
  3. // Copyright(c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. // this software and associated documentation files(the "Software"), to deal in
  7. // the Software without restriction, including without limitation the rights to
  8. // use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of
  9. // the Software, and to permit persons to whom the Software is furnished to do so,
  10. // subject to the following conditions :
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
  18. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #include <UIRenderer.h>
  22. UIRenderer::UIRenderer(HWND hwnd, DeviceResources& deviceResources, UIData& ui)
  23. : m_ui(ui)
  24. , m_deviceResources(deviceResources)
  25. {
  26. // Setup Dear ImGui context
  27. IMGUI_CHECKVERSION();
  28. ImGui::CreateContext();
  29. ImGuiIO& io = ImGui::GetIO(); (void)io;
  30. ImGui::StyleColorsDark();
  31. ImGui::GetStyle().WindowRounding = 6;
  32. ImGui::GetStyle().FrameBorderSize = 1;
  33. ImGui_ImplWin32_Init(hwnd);
  34. ImGui_ImplDX11_Init(m_deviceResources.device(), m_deviceResources.context());
  35. }
  36. void UIRenderer::cleanUp()
  37. {
  38. // Cleanup
  39. ImGui_ImplDX11_Shutdown();
  40. ImGui_ImplWin32_Shutdown();
  41. ImGui::DestroyContext();
  42. }
  43. void UIRenderer::update(double fps)
  44. {
  45. m_elapsedTimer.start();
  46. ImGui_ImplDX11_NewFrame();
  47. ImGui_ImplWin32_NewFrame();
  48. ImGui::NewFrame();
  49. if (m_ui.ShowSettings)
  50. {
  51. ImGui::Begin("Settings", 0, ImGuiWindowFlags_AlwaysAutoResize);
  52. if (ImGui::CollapsingHeader("GPU Info", ImGuiTreeNodeFlags_DefaultOpen))
  53. {
  54. Adapter adapter = m_deviceResources.getAdapter();
  55. ImGui::Text("Description: %s", adapter.Description.c_str());
  56. ImGui::Text("VendorID: 0x%x DeviceID: 0x%x", adapter.VendorId, adapter.DeviceId);
  57. ImGui::Text("DedicatedSystemMemory: %d [MB]", adapter.DedicatedVideoMemory / 1024 / 1024);
  58. ImGui::Text("SharedSystemMemory : %d [MB]", adapter.SharedSystemMemory / 1024 / 1024);
  59. }
  60. if (ImGui::CollapsingHeader("Images", ImGuiTreeNodeFlags_DefaultOpen))
  61. {
  62. if (ImGui::BeginCombo("Filename", m_ui.FileName.c_str()))
  63. {
  64. for (auto& e : m_ui.Files)
  65. {
  66. bool is_selected = (m_ui.FileName == e.filename().string());
  67. if (ImGui::Selectable(e.filename().string().c_str(), is_selected))
  68. {
  69. m_ui.FileName = e.filename().string();
  70. m_ui.FilePath = e;
  71. }
  72. if (is_selected)
  73. {
  74. ImGui::SetItemDefaultFocus();
  75. }
  76. }
  77. ImGui::EndCombo();
  78. }
  79. }
  80. if (ImGui::CollapsingHeader("Filter", ImGuiTreeNodeFlags_DefaultOpen))
  81. {
  82. ImGui::RadioButton("NVScaler", &m_ui.FilterMode, 0); ImGui::SameLine();
  83. ImGui::RadioButton("Bilinear", &m_ui.FilterMode, 2); ImGui::SameLine();
  84. ImGui::RadioButton("NVSharpen", &m_ui.FilterMode, 1);
  85. ;
  86. ImGui::Separator();
  87. if (m_ui.FilterMode == 0 || m_ui.FilterMode == 1)
  88. {
  89. m_ui.EnableNVScaler = true;
  90. ImGui::SliderFloat("Sharpness (0% - 100%)", &m_ui.Sharpness, 0, 100, "%2.1f%%");
  91. }
  92. else
  93. {
  94. m_ui.EnableNVScaler = false;
  95. }
  96. if (m_ui.FilterMode == 0 || m_ui.FilterMode == 2)
  97. {
  98. ImGui::Separator();
  99. std::vector<const char*> outputSizes = { "Variable", "1920x1080", "2560x1440", "3840x2160" };
  100. ImGui::Combo("Height Size", (int*)&m_ui.OutputMode, outputSizes.data(), int(outputSizes.size()));
  101. float fixScaleSize = 0;
  102. switch (m_ui.OutputMode) {
  103. case OutputSizeMode::VARIABLE:
  104. ImGui::SliderFloat("Scale (50% - 100%)", &m_ui.Scale, 50, 100, "%2.1f%%");
  105. break;
  106. case OutputSizeMode::P1080:
  107. fixScaleSize = 1080.f;
  108. break;
  109. case OutputSizeMode::P1440:
  110. fixScaleSize = 1440.f;
  111. break;
  112. case OutputSizeMode::P2160:
  113. fixScaleSize = 2160.f;
  114. break;
  115. }
  116. if (fixScaleSize > 0)
  117. {
  118. m_ui.Scale = std::min<float>(100.f, std::max<float>(50.f, m_ui.InputHeight / fixScaleSize * 100.f));
  119. ImGui::Text("Fix Scale : %2.1f%%", m_ui.Scale);
  120. }
  121. }
  122. else
  123. {
  124. m_ui.Scale = 100;
  125. }
  126. ImGui::Separator();
  127. if (m_ui.Scale == 100)
  128. {
  129. if (m_ui.EnableNVScaler)
  130. {
  131. ImGui::Text("Using NVSharpen shader:");
  132. ImGui::Text("Scale 100 %% performs only sharpening");
  133. }
  134. else
  135. {
  136. ImGui::Text("Using CopyResource");
  137. }
  138. }
  139. else
  140. {
  141. if (m_ui.EnableNVScaler)
  142. {
  143. ImGui::Text("Using NVScaler shader:");
  144. ImGui::Text("Performs scaling and sharpening");
  145. }
  146. else
  147. {
  148. ImGui::Text("Using bilinear upscale shader");
  149. }
  150. }
  151. ImGui::Separator();
  152. ImGui::Text("Input Size : %d x %d", m_ui.InputWidth, m_ui.InputHeight);
  153. ImGui::Text("Output Size : %d x %d", m_ui.OutputWidth, m_ui.OutputHeight);
  154. }
  155. if (ImGui::CollapsingHeader("Display", ImGuiTreeNodeFlags_DefaultOpen))
  156. {
  157. ImGui::Checkbox("VSync", &m_ui.EnableVsync);
  158. }
  159. if (ImGui::CollapsingHeader("Profiling", ImGuiTreeNodeFlags_DefaultOpen))
  160. {
  161. ImGui::RadioButton("microseconds", &m_ui.UnitMicroseconds, 1); ImGui::SameLine();
  162. ImGui::RadioButton("milliseconds", &m_ui.UnitMicroseconds, 0);
  163. ImGui::Separator();
  164. double unitConst = 1E6;
  165. std::string unitStr = "us";
  166. if (!m_ui.UnitMicroseconds)
  167. {
  168. unitConst = 1E3;
  169. unitStr = "ms";
  170. }
  171. double filterTime = m_ui.FilterTime / 1E6 * unitConst;
  172. double totalTime = 1. / fps * unitConst;
  173. double uiTime = m_elapsedTimer.averageTime_us() / 1E6 * unitConst;
  174. ImGui::Text("FPS : %9.2f", fps);
  175. ImGui::Text("Filter Time : %9.2f %s", filterTime, unitStr.c_str());
  176. ImGui::Text("UI Time : %9.2f %s", uiTime, unitStr.c_str());
  177. ImGui::Text("Presnt Time : %9.2f %s", totalTime - filterTime - uiTime, unitStr.c_str());
  178. ImGui::Text("Total Time : %9.2f %s", totalTime, unitStr.c_str());
  179. }
  180. ImGui::End();
  181. }
  182. ImGui::Render();
  183. m_elapsedTimer.end();
  184. }
  185. void UIRenderer::render()
  186. {
  187. ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
  188. }