Sample.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #pragma comment(lib, "user32")
  22. #pragma comment(lib, "d3d12")
  23. #pragma comment(lib, "dxgi")
  24. #pragma comment(lib, "windowscodecs")
  25. #include <filesystem>
  26. #include <iostream>
  27. #include <tchar.h>
  28. #include <wincodec.h>
  29. #include "AppRenderer.h"
  30. #include "DeviceResources.h"
  31. #include "UIRenderer.h"
  32. #include "Utilities.h"
  33. DeviceResources deviceResources;
  34. UIData uiData;
  35. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  36. int main(int argc, char* argv[])
  37. {
  38. // Resources
  39. std::string mediaFolder = "media/images/";
  40. std::string shadersFolder = "NIS/";
  41. if (!std::filesystem::exists(mediaFolder))
  42. mediaFolder = "media/images/";
  43. if (!std::filesystem::exists(mediaFolder))
  44. mediaFolder = "../../media/images/";
  45. if (!std::filesystem::exists(mediaFolder))
  46. return -1;
  47. // UI settings
  48. uiData.Files = getFiles(mediaFolder);
  49. if (uiData.Files.size() == 0)
  50. throw std::runtime_error("No media files");
  51. uiData.FileName = uiData.Files[0].filename().string();
  52. uiData.FilePath = uiData.Files[0];
  53. // Create Window
  54. WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"NVIDIA Image Scaling Demo", nullptr };
  55. ::RegisterClassEx(&wc);
  56. RECT wr = { 0, 0, 1280, 1080 };
  57. AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
  58. HWND hwnd = ::CreateWindow(wc.lpszClassName, L"NVIDIA Image Scaling DX12 Demo", WS_OVERLAPPEDWINDOW, 0, 0, wr.right - wr.left, wr.bottom - wr.top, nullptr, nullptr, wc.hInstance, nullptr);
  59. ::SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SIZEBOX);
  60. SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
  61. // Initialize DX12
  62. deviceResources.create(hwnd, 0);
  63. // Renderers
  64. UIRenderer uiRenderer(hwnd, deviceResources, uiData);
  65. std::vector<std::string> shaderPaths{ "NIS/", "../../../NIS/", "../../DX12/src/" };
  66. AppRenderer appRenderer(deviceResources, uiData, shaderPaths);
  67. // Show the window
  68. ::ShowWindow(hwnd, SW_SHOWDEFAULT);
  69. ::UpdateWindow(hwnd);
  70. FPS m_fps;
  71. MSG msg{};
  72. while (msg.message != WM_QUIT)
  73. {
  74. if (::PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE))
  75. {
  76. // press 's' to dump png file
  77. if (msg.message == WM_KEYDOWN && msg.wParam == 'S')
  78. {
  79. appRenderer.saveOutput("dump.png");
  80. }
  81. ::TranslateMessage(&msg);
  82. ::DispatchMessage(&msg);
  83. }
  84. m_fps.update();
  85. bool update = false;
  86. if (appRenderer.updateSize())
  87. {
  88. deviceResources.resizeRenderTarget(appRenderer.width(), appRenderer.height());
  89. }
  90. deviceResources.PopulateCommandList();
  91. uiRenderer.update(m_fps.fps());
  92. appRenderer.update();
  93. appRenderer.render();
  94. uiRenderer.render();
  95. deviceResources.Present(uiData.EnableVsync, 0);
  96. }
  97. deviceResources.WaitForGPU();
  98. uiRenderer.cleanUp();
  99. ::DestroyWindow(hwnd);
  100. ::UnregisterClass(wc.lpszClassName, wc.hInstance);
  101. return 0;
  102. }
  103. // Forward declare message handler from imgui_impl_win32.cpp
  104. extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  105. // Win32 message handler
  106. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  107. {
  108. if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  109. return true;
  110. switch (msg)
  111. {
  112. case WM_SYSCOMMAND:
  113. if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
  114. return 0;
  115. break;
  116. case WM_KEYUP:
  117. case WM_SYSKEYUP:
  118. if (wParam == VK_F1)
  119. uiData.ShowSettings = !uiData.ShowSettings;
  120. break;
  121. case WM_CLOSE:
  122. case WM_DESTROY:
  123. ::PostQuitMessage(0);
  124. return 0;
  125. }
  126. return ::DefWindowProc(hWnd, msg, wParam, lParam);
  127. }