DeviceResources.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 once
  22. #if defined(_WIN32)
  23. #define VK_USE_PLATFORM_WIN32_KHR
  24. #endif // _WIN32
  25. #define GLFW_INCLUDE_VULKAN
  26. #include <GLFW/glfw3.h>
  27. #include <imgui_impl_vulkan.h>
  28. #include <stdio.h>
  29. #include <cstdint>
  30. #include <cassert>
  31. #include <vector>
  32. #define IMGUI_VK_HELPERS
  33. #define VK_OK(exp) \
  34. { \
  35. const auto _result = (exp); \
  36. if (_result != VK_SUCCESS) { \
  37. fprintf(stderr, "%s(%d): %d=%s\n", __FUNCTION__, __LINE__, _result, #exp); \
  38. assert(0); \
  39. } \
  40. }
  41. #define VK_DIE(reason) \
  42. fprintf(stderr, "%s(%d): %s\n", __FUNCTION__, __LINE__, (reason)); \
  43. assert(0);
  44. struct PerFrameResources
  45. {
  46. VkCommandPool commandPool = VK_NULL_HANDLE;
  47. VkCommandBuffer commandBuffer = VK_NULL_HANDLE;
  48. VkFence fence = VK_NULL_HANDLE;
  49. VkImage backbuffer = VK_NULL_HANDLE;
  50. VkImageView backbufferView = VK_NULL_HANDLE;
  51. VkFramebuffer framebuffer = VK_NULL_HANDLE;
  52. };
  53. struct FrameSync
  54. {
  55. VkSemaphore imageAcquired = VK_NULL_HANDLE;
  56. VkSemaphore renderComplete = VK_NULL_HANDLE;
  57. };
  58. class DeviceResources
  59. {
  60. public:
  61. static const VkFormat SwapchainFormat;
  62. static const VkColorSpaceKHR SwapchainColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
  63. static const uint32_t NumQueryValues = 2;
  64. void create(GLFWwindow* hWnd);
  65. void cleanUp();
  66. void update();
  67. void beginRender();
  68. void resizeRenderTarget(uint32_t Width, uint32_t Height);
  69. void clearRenderTargetView(const float color[4]) {}
  70. void present(uint32_t SyncInterval, uint32_t Flags);
  71. VkInstance instance() { return m_instance; }
  72. VkPhysicalDevice physicalDevice() { return m_physicalDevice; }
  73. VkDevice logicalDevice() { return m_device; }
  74. VkQueue queue() { return m_queue; }
  75. VkDescriptorPool descriptorPool() { return m_descriptorPool; }
  76. VkSampler* sampler() { return &m_sampler; }
  77. VkRenderPass UIrenderPass() { return m_renderPass; }
  78. VkFramebuffer UIframeBuffer() { return currentFrame()->framebuffer; }
  79. uint32_t numSwapchainImages() { return static_cast<uint32_t>(m_frames.size()); }
  80. uint32_t swapchainIndex() { return m_frameIndex; }
  81. VkCommandBuffer commandBuffer() { return currentFrame()->commandBuffer; }
  82. VkCommandPool commandPool() { return currentFrame()->commandPool; }
  83. VkImage backBuffer() { return currentFrame()->backbuffer; }
  84. VkImageView backBufferView() { return currentFrame()->backbufferView; }
  85. VkSwapchainKHR swapchain() { return m_swapchain; }
  86. VkQueryPool queryPool() { return m_queryPool; }
  87. bool initialized() const { return m_initialized; }
  88. void requestSwapChainRebuild() { m_swapChainRebuild = true; }
  89. VkCommandBuffer beginOneTimeSubmitCmd();
  90. void endOneTimeSubmitCmd();
  91. void createTexture2D(int w, int h, VkFormat format, const void* data, uint32_t rowPitch, uint32_t imageSize, VkImage* outImage, VkDeviceMemory* outDeviceMemory);
  92. void createTexture2D(int w, int h, VkFormat format, VkImage* outImage, VkDeviceMemory* outDeviceMemory);
  93. void createSRV(VkImage inputImage, VkFormat format, VkImageView* outSrv);
  94. void createConstBuffer(void* initialData, uint32_t size, VkBuffer* outBuffer, VkDeviceMemory* outBuffMem, VkDeviceSize* outOffset);
  95. void createBuffer(VkDeviceSize size, VkBufferUsageFlags buffUsage, VkMemoryPropertyFlags memProps, VkBuffer* outBuffer, VkDeviceMemory* outBuffMem);
  96. uint32_t minImageCount() const { return m_minImageCount; }
  97. float timestampPeriod() const { return m_physicalDeviceProperties.limits.timestampPeriod; }
  98. uint32_t width() const { return m_width; }
  99. uint32_t height() const { return m_height; }
  100. private:
  101. void createSurfaceResources();
  102. void destroySurfaceResources();
  103. void selectPhysicalDeviceAndQueueFamily();
  104. uint32_t findMemoryTypeIndex(uint32_t memoryTypeBits, VkMemoryPropertyFlags memPropFlags);
  105. PerFrameResources* currentFrame() { return &m_frames[m_frameIndex]; }
  106. VkSemaphore imageAcquired() { return m_semaphores[m_semaphoreIndex].imageAcquired; }
  107. VkSemaphore renderComplete() { return m_semaphores[m_semaphoreIndex].renderComplete; }
  108. GLFWwindow* m_window = nullptr;
  109. const VkAllocationCallbacks* m_allocator = nullptr;
  110. VkInstance m_instance = VK_NULL_HANDLE;
  111. VkPhysicalDevice m_physicalDevice = VK_NULL_HANDLE;
  112. VkPhysicalDeviceProperties m_physicalDeviceProperties;
  113. VkDevice m_device = VK_NULL_HANDLE;
  114. VkQueue m_queue = VK_NULL_HANDLE;
  115. VkSurfaceKHR m_surface = VK_NULL_HANDLE;
  116. int m_queueFamilyIndex = -1;
  117. VkSurfaceFormatKHR m_surfaceFormat;
  118. VkDescriptorPool m_descriptorPool = VK_NULL_HANDLE;
  119. VkSampler m_sampler = VK_NULL_HANDLE;
  120. VkQueryPool m_queryPool = VK_NULL_HANDLE;
  121. VkSwapchainKHR m_swapchain = VK_NULL_HANDLE;
  122. VkRenderPass m_renderPass = VK_NULL_HANDLE;
  123. std::vector<PerFrameResources> m_frames;
  124. std::vector<FrameSync> m_semaphores;
  125. uint32_t m_frameIndex = 0; // Index into m_frames
  126. uint32_t m_semaphoreIndex = 0; // Index into m_semaphores
  127. uint32_t m_minImageCount = 2;
  128. uint32_t m_width;
  129. uint32_t m_height;
  130. bool m_initialized = false;
  131. bool m_swapChainRebuild = false;
  132. };