Sample.cpp 5.4 KB

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