UIRenderer.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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(void* hwnd, DeviceResources& deviceResources, UIData& ui)
  23. : m_deviceResources(deviceResources)
  24. , m_ui(ui)
  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. const bool installInputCallbacks = true;
  34. ImGui_ImplGlfw_InitForVulkan((GLFWwindow*)hwnd, installInputCallbacks);
  35. ImGui_ImplVulkan_InitInfo initInfo{};
  36. initInfo.Instance = deviceResources.instance();
  37. initInfo.PhysicalDevice = deviceResources.physicalDevice();
  38. initInfo.Device = deviceResources.logicalDevice();
  39. initInfo.Queue = deviceResources.queue();
  40. initInfo.DescriptorPool = deviceResources.descriptorPool();
  41. initInfo.MinImageCount = deviceResources.minImageCount();
  42. initInfo.ImageCount = deviceResources.numSwapchainImages();
  43. ImGui_ImplVulkan_Init(&initInfo, deviceResources.UIrenderPass());
  44. // Upload Fonts
  45. {
  46. auto cmdBuff = deviceResources.beginOneTimeSubmitCmd();
  47. ImGui_ImplVulkan_CreateFontsTexture(cmdBuff);
  48. deviceResources.endOneTimeSubmitCmd();
  49. ImGui_ImplVulkan_DestroyFontUploadObjects();
  50. }
  51. }
  52. void UIRenderer::cleanUp()
  53. {
  54. ImGui_ImplVulkan_Shutdown();
  55. ImGui_ImplGlfw_Shutdown();
  56. ImGui::DestroyContext();
  57. }
  58. void UIRenderer::update(double fps)
  59. {
  60. m_elapsedTimer.start();
  61. ImGui_ImplVulkan_NewFrame();
  62. ImGui_ImplGlfw_NewFrame();
  63. ImGui::NewFrame();
  64. if (m_ui.ShowSettings)
  65. {
  66. ImGui::Begin("Settings", 0, ImGuiWindowFlags_AlwaysAutoResize);
  67. if (ImGui::CollapsingHeader("Images", ImGuiTreeNodeFlags_DefaultOpen))
  68. {
  69. if (ImGui::BeginCombo("Filename", m_ui.FileName.c_str()))
  70. {
  71. for (auto& e : m_ui.Files)
  72. {
  73. bool is_selected = (m_ui.FileName == e.filename().string());
  74. if (ImGui::Selectable(e.filename().string().c_str(), is_selected))
  75. {
  76. m_ui.FileName = e.filename().string();
  77. m_ui.FilePath = e;
  78. }
  79. if (is_selected)
  80. {
  81. ImGui::SetItemDefaultFocus();
  82. }
  83. }
  84. ImGui::EndCombo();
  85. }
  86. }
  87. if (ImGui::CollapsingHeader("Filter", ImGuiTreeNodeFlags_DefaultOpen))
  88. {
  89. ImGui::RadioButton("NVScaler", &m_ui.FilterMode, 0); ImGui::SameLine();
  90. ImGui::RadioButton("Bilinear", &m_ui.FilterMode, 2); ImGui::SameLine();
  91. ImGui::RadioButton("NVSharpen", &m_ui.FilterMode, 1);
  92. ;
  93. ImGui::Separator();
  94. if (m_ui.FilterMode == 0 || m_ui.FilterMode == 1)
  95. {
  96. m_ui.EnableNVScaler = true;
  97. ImGui::SliderFloat("Sharpness (0% - 100%)", &m_ui.Sharpness, 0, 100, "%2.1f%%");
  98. }
  99. else
  100. {
  101. m_ui.EnableNVScaler = false;
  102. }
  103. if (m_ui.FilterMode == 0 || m_ui.FilterMode == 2)
  104. {
  105. ImGui::Separator();
  106. std::vector<const char*> outputSizes = { "Variable", "1920x1080", "2560x1440", "3840x2160" };
  107. ImGui::Combo("Height Size", (int*)&m_ui.OutputMode, outputSizes.data(), int(outputSizes.size()));
  108. float fixScaleSize = 0;
  109. switch (m_ui.OutputMode) {
  110. case OutputSizeMode::VARIABLE:
  111. ImGui::SliderFloat("Scale (50% - 100%)", &m_ui.Scale, 50, 100, "%2.1f%%");
  112. break;
  113. case OutputSizeMode::P1080:
  114. fixScaleSize = 1080.f;
  115. break;
  116. case OutputSizeMode::P1440:
  117. fixScaleSize = 1440.f;
  118. break;
  119. case OutputSizeMode::P2160:
  120. fixScaleSize = 2160.f;
  121. break;
  122. }
  123. if (fixScaleSize > 0)
  124. {
  125. m_ui.Scale = std::min<float>(100.f, std::max<float>(50.f, m_ui.InputHeight / fixScaleSize * 100.f));
  126. ImGui::Text("Fix Scale : %2.1f%%", m_ui.Scale);
  127. }
  128. }
  129. else
  130. {
  131. m_ui.Scale = 100;
  132. }
  133. ImGui::Separator();
  134. if (m_ui.Scale == 100)
  135. {
  136. if (m_ui.EnableNVScaler)
  137. {
  138. ImGui::Text("Using NVSharpen shader:");
  139. ImGui::Text("Scale 100 %% performs only sharpening");
  140. }
  141. else
  142. {
  143. ImGui::Text("Using CopyResource");
  144. }
  145. }
  146. else
  147. {
  148. if (m_ui.EnableNVScaler)
  149. {
  150. ImGui::Text("Using NVScaler shader:");
  151. ImGui::Text("Performs scaling and sharpening");
  152. }
  153. else
  154. {
  155. ImGui::Text("Using bilinear upscale shader");
  156. }
  157. }
  158. ImGui::Separator();
  159. ImGui::Text("Input Size : %d x %d", m_ui.InputWidth, m_ui.InputHeight);
  160. ImGui::Text("Output Size : %d x %d", m_ui.OutputWidth, m_ui.OutputHeight);
  161. }
  162. if (ImGui::CollapsingHeader("Profiling", ImGuiTreeNodeFlags_DefaultOpen))
  163. {
  164. ImGui::RadioButton("microseconds", &m_ui.UnitMicroseconds, 1); ImGui::SameLine();
  165. ImGui::RadioButton("milliseconds", &m_ui.UnitMicroseconds, 0);
  166. ImGui::Separator();
  167. double unitConst = 1E6;
  168. std::string unitStr = "us";
  169. if (!m_ui.UnitMicroseconds)
  170. {
  171. unitConst = 1E3;
  172. unitStr = "ms";
  173. }
  174. double filterTime = m_ui.FilterTime / 1E6 * unitConst;
  175. double totalTime = 1. / fps * unitConst;
  176. double uiTime = m_elapsedTimer.averageTime_us() / 1E6 * unitConst;
  177. ImGui::Text("FPS : %9.2f", fps);
  178. ImGui::Text("Filter Time : %9.2f %s", filterTime, unitStr.c_str());
  179. ImGui::Text("UI Time : %9.2f %s", uiTime, unitStr.c_str());
  180. ImGui::Text("Presnt Time : %9.2f %s", totalTime - filterTime - uiTime, unitStr.c_str());
  181. ImGui::Text("Total Time : %9.2f %s", totalTime, unitStr.c_str());
  182. }
  183. ImGui::End();
  184. }
  185. ImGui::Render();
  186. m_elapsedTimer.end();
  187. }
  188. void UIRenderer::render()
  189. {
  190. auto cmdBuff = m_deviceResources.commandBuffer();
  191. {
  192. VkRenderPassBeginInfo info{};
  193. info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
  194. info.renderPass = m_deviceResources.UIrenderPass();
  195. info.framebuffer = m_deviceResources.UIframeBuffer();
  196. info.renderArea.extent.width = m_deviceResources.width();
  197. info.renderArea.extent.height = m_deviceResources.height();
  198. vkCmdBeginRenderPass(cmdBuff, &info, VK_SUBPASS_CONTENTS_INLINE);
  199. }
  200. // Record dear imgui primitives into command buffer
  201. ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), cmdBuff);
  202. vkCmdEndRenderPass(cmdBuff);
  203. }