UIRenderer.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. IMGUI_CHECKVERSION();
  27. ImGui::CreateContext();
  28. ImGuiIO& io = ImGui::GetIO(); (void)io;
  29. ImGui::StyleColorsDark();
  30. ImGui::GetStyle().WindowRounding = 6;
  31. ImGui::GetStyle().FrameBorderSize = 1;
  32. ImGui_ImplWin32_Init(hwnd);
  33. ImGui_ImplDX12_Init(m_deviceResources.device(), 3,
  34. DXGI_FORMAT_R8G8B8A8_UNORM, m_deviceResources.SRVDescHeap(),
  35. m_deviceResources.SRVDescHeap()->GetCPUDescriptorHandleForHeapStart(),
  36. m_deviceResources.SRVDescHeap()->GetGPUDescriptorHandleForHeapStart());
  37. }
  38. void UIRenderer::cleanUp()
  39. {
  40. // Cleanup
  41. ImGui_ImplDX12_Shutdown();
  42. ImGui_ImplWin32_Shutdown();
  43. ImGui::DestroyContext();
  44. }
  45. void UIRenderer::update(double fps)
  46. {
  47. m_elapsedTimer.start();
  48. ImGui_ImplDX12_NewFrame();
  49. ImGui_ImplWin32_NewFrame();
  50. ImGui::NewFrame();
  51. if (m_ui.ShowSettings)
  52. {
  53. ImGui::Begin("Settings", 0, ImGuiWindowFlags_AlwaysAutoResize);
  54. if (ImGui::CollapsingHeader("GPU Info", ImGuiTreeNodeFlags_DefaultOpen))
  55. {
  56. Adapter adapter = m_deviceResources.adapter();
  57. ImGui::Text("Description: %s", adapter.Description.c_str());
  58. ImGui::Text("VendorID: 0x%x DeviceID: 0x%x", adapter.VendorId, adapter.DeviceId);
  59. ImGui::Text("DedicatedSystemMemory: %d [MB]", adapter.DedicatedVideoMemory / 1024 / 1024);
  60. ImGui::Text("SharedSystemMemory : %d [MB]", adapter.SharedSystemMemory / 1024 / 1024);
  61. }
  62. if (ImGui::CollapsingHeader("Images", ImGuiTreeNodeFlags_DefaultOpen))
  63. {
  64. if (ImGui::BeginCombo("Filename", m_ui.FileName.c_str()))
  65. {
  66. for (auto& e : m_ui.Files)
  67. {
  68. bool is_selected = (m_ui.FileName == e.filename().string());
  69. if (ImGui::Selectable(e.filename().string().c_str(), is_selected))
  70. {
  71. m_ui.FileName = e.filename().string();
  72. m_ui.FilePath = e;
  73. }
  74. if (is_selected)
  75. {
  76. ImGui::SetItemDefaultFocus();
  77. }
  78. }
  79. ImGui::EndCombo();
  80. }
  81. }
  82. if (ImGui::CollapsingHeader("Filter", ImGuiTreeNodeFlags_DefaultOpen))
  83. {
  84. ImGui::RadioButton("NVScaler", &m_ui.FilterMode, 0); ImGui::SameLine();
  85. ImGui::RadioButton("Bilinear", &m_ui.FilterMode, 2); ImGui::SameLine();
  86. ImGui::RadioButton("NVSharpen", &m_ui.FilterMode, 1);
  87. ;
  88. ImGui::Separator();
  89. if (m_ui.FilterMode == 0 || m_ui.FilterMode == 1)
  90. {
  91. m_ui.EnableNVScaler = true;
  92. ImGui::SliderFloat("Sharpness (0% - 100%)", &m_ui.Sharpness, 0, 100, "%2.1f%%");
  93. }
  94. else
  95. {
  96. m_ui.EnableNVScaler = false;
  97. }
  98. if (m_ui.FilterMode == 0 || m_ui.FilterMode == 2)
  99. {
  100. ImGui::Separator();
  101. std::vector<const char*> outputSizes = { "Variable", "1920x1080", "2560x1440", "3840x2160" };
  102. ImGui::Combo("Height Size", (int*)&m_ui.OutputMode, outputSizes.data(), int(outputSizes.size()));
  103. float fixScaleSize = 0;
  104. switch (m_ui.OutputMode) {
  105. case OutputSizeMode::VARIABLE:
  106. ImGui::SliderFloat("Scale (50% - 100%)", &m_ui.Scale, 50, 100, "%2.1f%%");
  107. break;
  108. case OutputSizeMode::P1080:
  109. fixScaleSize = 1080.f;
  110. break;
  111. case OutputSizeMode::P1440:
  112. fixScaleSize = 1440.f;
  113. break;
  114. case OutputSizeMode::P2160:
  115. fixScaleSize = 2160.f;
  116. break;
  117. }
  118. if (fixScaleSize > 0)
  119. {
  120. m_ui.Scale = std::min<float>(100.f, std::max<float>(50.f, m_ui.InputHeight / fixScaleSize * 100.f));
  121. ImGui::Text("Fix Scale : %2.1f%%", m_ui.Scale);
  122. }
  123. }
  124. else
  125. {
  126. m_ui.Scale = 100;
  127. }
  128. ImGui::Separator();
  129. if (m_ui.Scale == 100)
  130. {
  131. if (m_ui.EnableNVScaler)
  132. {
  133. ImGui::Text("Using NVSharpen shader:");
  134. ImGui::Text("Scale 100 %% performs only sharpening");
  135. }
  136. else
  137. {
  138. ImGui::Text("Using CopyResource");
  139. }
  140. }
  141. else
  142. {
  143. if (m_ui.EnableNVScaler)
  144. {
  145. ImGui::Text("Using NVScaler shader:");
  146. ImGui::Text("Performs scaling and sharpening");
  147. }
  148. else
  149. {
  150. ImGui::Text("Using bilinear upscale shader");
  151. }
  152. }
  153. ImGui::Separator();
  154. ImGui::Text("Input Size : %d x %d", m_ui.InputWidth, m_ui.InputHeight);
  155. ImGui::Text("Output Size : %d x %d", m_ui.OutputWidth, m_ui.OutputHeight);
  156. }
  157. if (ImGui::CollapsingHeader("Display", ImGuiTreeNodeFlags_DefaultOpen))
  158. {
  159. ImGui::Checkbox("VSync", &m_ui.EnableVsync);
  160. }
  161. if (ImGui::CollapsingHeader("Profiling", ImGuiTreeNodeFlags_DefaultOpen))
  162. {
  163. ImGui::RadioButton("microseconds", &m_ui.UnitMicroseconds, 1); ImGui::SameLine();
  164. ImGui::RadioButton("milliseconds", &m_ui.UnitMicroseconds, 0);
  165. ImGui::Separator();
  166. double unitConst = 1E6;
  167. std::string unitStr = "us";
  168. if (!m_ui.UnitMicroseconds)
  169. {
  170. unitConst = 1E3;
  171. unitStr = "ms";
  172. }
  173. double filterTime = m_deviceResources.GetTime_us() / 1E6 * unitConst;
  174. double totalTime = 1. / fps * unitConst;
  175. double uiTime = m_elapsedTimer.averageTime_us() / 1E6 * unitConst;
  176. ImGui::Text("FPS : %9.2f", fps);
  177. ImGui::Text("Filter Time : %9.2f %s", filterTime, unitStr.c_str());
  178. ImGui::Text("UI Time : %9.2f %s", uiTime, unitStr.c_str());
  179. ImGui::Text("Presnt Time : %9.2f %s", totalTime - filterTime - uiTime, unitStr.c_str());
  180. ImGui::Text("Total Time : %9.2f %s", totalTime, unitStr.c_str());
  181. }
  182. ImGui::End();
  183. }
  184. ImGui::Render();
  185. m_elapsedTimer.end();
  186. }
  187. void UIRenderer::render()
  188. {
  189. ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), m_deviceResources.commandList());
  190. }