Sample.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <filesystem>
  22. #include <iostream>
  23. #include "AppRenderer.h"
  24. #include "DeviceResources.h"
  25. #include "UIRenderer.h"
  26. static void glfw_error_callback(int error, const char* description)
  27. {
  28. fprintf(stderr, "glfw error: %d %s\n", error, description);
  29. assert(0);
  30. }
  31. int main(int argc, char* argv[])
  32. {
  33. // Resources
  34. std::string mediaFolder = "media/images/";
  35. if (!std::filesystem::exists(mediaFolder))
  36. mediaFolder = "media/images/";
  37. if (!std::filesystem::exists(mediaFolder))
  38. mediaFolder = "../../media/images/";
  39. glfwSetErrorCallback(glfw_error_callback);
  40. if (glfwInit() == GLFW_FALSE)
  41. {
  42. fprintf(stderr, "glfwInit() failed\n");
  43. exit(EXIT_FAILURE);
  44. }
  45. glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
  46. // Don't need OpenGL context
  47. glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
  48. auto app_name = "NVIDIA Image Scaling Vulkan Demo";
  49. GLFWwindow* hwnd = glfwCreateWindow(256, 256, app_name, nullptr, nullptr);
  50. if (!hwnd)
  51. {
  52. glfwTerminate();
  53. exit(EXIT_FAILURE);
  54. }
  55. DeviceResources deviceResources;
  56. UIData uiData;
  57. deviceResources.create(hwnd);
  58. // Show window and force resize similiar to ::UpdateWindow() in DX11 sample
  59. glfwShowWindow(hwnd);
  60. glfwFocusWindow(hwnd);
  61. glfwSetWindowSize(hwnd, 1280, 1080);
  62. // UI settings
  63. uiData.Files = getFiles(mediaFolder);
  64. if (uiData.Files.size() == 0)
  65. throw std::runtime_error("No media files");
  66. uiData.FileName = uiData.Files[0].filename().string();
  67. uiData.FilePath = uiData.Files[0];
  68. std::vector<std::string> shaderPaths{ "NIS/", "../../../NIS/", "." };
  69. bool useGlsl = false;
  70. AppRenderer appRenderer(deviceResources, uiData, shaderPaths, useGlsl);
  71. UIRenderer uiRenderer(hwnd, deviceResources, uiData);
  72. FPS m_fps;
  73. while (!glfwWindowShouldClose(hwnd))
  74. {
  75. glfwPollEvents();
  76. m_fps.update();
  77. deviceResources.update();
  78. uiRenderer.update(m_fps.fps());
  79. if (appRenderer.update())
  80. {
  81. glfwSetWindowSize(hwnd, appRenderer.width(), appRenderer.height());
  82. }
  83. deviceResources.beginRender();
  84. appRenderer.render();
  85. uiRenderer.render();
  86. deviceResources.present(uiData.EnableVsync /*ignored*/, 0);
  87. appRenderer.present();
  88. }
  89. uiRenderer.cleanUp();
  90. appRenderer.cleanUp();
  91. deviceResources.cleanUp();
  92. glfwDestroyWindow(hwnd);
  93. glfwTerminate();
  94. return EXIT_SUCCESS;
  95. }