GrManagerImpl.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Gr/GrManager.h>
  7. #include <AnKi/Gr/Vulkan/Common.h>
  8. #include <AnKi/Gr/Vulkan/GpuMemoryManager.h>
  9. #include <AnKi/Gr/Vulkan/SemaphoreFactory.h>
  10. #include <AnKi/Gr/Vulkan/DeferredBarrierFactory.h>
  11. #include <AnKi/Gr/Vulkan/FenceFactory.h>
  12. #include <AnKi/Gr/Vulkan/SamplerFactory.h>
  13. #include <AnKi/Gr/Vulkan/QueryFactory.h>
  14. #include <AnKi/Gr/Vulkan/DescriptorSet.h>
  15. #include <AnKi/Gr/Vulkan/CommandBufferFactory.h>
  16. #include <AnKi/Gr/Vulkan/SwapchainFactory.h>
  17. #include <AnKi/Gr/Vulkan/PipelineLayout.h>
  18. #include <AnKi/Gr/Vulkan/PipelineCache.h>
  19. #include <AnKi/Gr/Vulkan/DescriptorSet.h>
  20. #include <AnKi/Util/HashMap.h>
  21. #include <AnKi/Util/File.h>
  22. namespace anki
  23. {
  24. /// @note Disable that because it crashes Intel drivers
  25. #define ANKI_GR_MANAGER_DEBUG_MEMMORY ANKI_EXTRA_CHECKS && 0
  26. // Forward
  27. class TextureFallbackUploader;
  28. class ConfigSet;
  29. /// @addtogroup vulkan
  30. /// @{
  31. /// Vulkan implementation of GrManager.
  32. class GrManagerImpl : public GrManager
  33. {
  34. public:
  35. GrManagerImpl()
  36. {
  37. }
  38. ~GrManagerImpl();
  39. ANKI_USE_RESULT Error init(const GrManagerInitInfo& cfg);
  40. const Array<U32, U(QueueType::COUNT)>& getQueueFamilies() const
  41. {
  42. return m_queueFamilyIndices;
  43. }
  44. const VkPhysicalDeviceProperties& getPhysicalDeviceProperties() const
  45. {
  46. return m_devProps.properties;
  47. }
  48. const VkPhysicalDeviceRayTracingPipelinePropertiesKHR& getPhysicalDeviceRayTracingProperties() const
  49. {
  50. return m_rtPipelineProps;
  51. }
  52. TexturePtr acquireNextPresentableTexture();
  53. void endFrame();
  54. void finish();
  55. VkDevice getDevice() const
  56. {
  57. ANKI_ASSERT(m_device);
  58. return m_device;
  59. }
  60. VkPhysicalDevice getPhysicalDevice() const
  61. {
  62. ANKI_ASSERT(m_physicalDevice);
  63. return m_physicalDevice;
  64. }
  65. /// @name object_creation
  66. /// @{
  67. CommandBufferFactory& getCommandBufferFactory()
  68. {
  69. return m_cmdbFactory;
  70. }
  71. const CommandBufferFactory& getCommandBufferFactory() const
  72. {
  73. return m_cmdbFactory;
  74. }
  75. SamplerFactory& getSamplerFactory()
  76. {
  77. return m_samplerFactory;
  78. }
  79. /// @}
  80. void flushCommandBuffer(MicroCommandBufferPtr cmdb, Bool cmdbRenderedToSwapchain,
  81. WeakArray<MicroSemaphorePtr> waitSemaphores, MicroSemaphorePtr* signalSemaphore,
  82. Bool wait = false);
  83. /// @name Memory
  84. /// @{
  85. GpuMemoryManager& getGpuMemoryManager()
  86. {
  87. return m_gpuMemManager;
  88. }
  89. const GpuMemoryManager& getGpuMemoryManager() const
  90. {
  91. return m_gpuMemManager;
  92. }
  93. const VkPhysicalDeviceMemoryProperties& getMemoryProperties() const
  94. {
  95. return m_memoryProperties;
  96. }
  97. /// @}
  98. QueryFactory& getOcclusionQueryFactory()
  99. {
  100. return m_occlusionQueryFactory;
  101. }
  102. QueryFactory& getTimestampQueryFactory()
  103. {
  104. return m_timestampQueryFactory;
  105. }
  106. Bool getR8g8b8ImagesSupported() const
  107. {
  108. return m_r8g8b8ImagesSupported;
  109. }
  110. Bool getS8ImagesSupported() const
  111. {
  112. return m_s8ImagesSupported;
  113. }
  114. Bool getD24S8ImagesSupported() const
  115. {
  116. return m_d24S8ImagesSupported;
  117. }
  118. DescriptorSetFactory& getDescriptorSetFactory()
  119. {
  120. return m_descrFactory;
  121. }
  122. VkPipelineCache getPipelineCache() const
  123. {
  124. ANKI_ASSERT(m_pplineCache.m_cacheHandle);
  125. return m_pplineCache.m_cacheHandle;
  126. }
  127. PipelineLayoutFactory& getPipelineLayoutFactory()
  128. {
  129. return m_pplineLayoutFactory;
  130. }
  131. VulkanExtensions getExtensions() const
  132. {
  133. return m_extensions;
  134. }
  135. MicroSwapchainPtr getSwapchain() const
  136. {
  137. return m_crntSwapchain;
  138. }
  139. VkSurfaceKHR getSurface() const
  140. {
  141. ANKI_ASSERT(m_surface);
  142. return m_surface;
  143. }
  144. /// @name Debug report
  145. /// @{
  146. void beginMarker(VkCommandBuffer cmdb, CString name) const
  147. {
  148. ANKI_ASSERT(cmdb);
  149. if(m_pfnCmdDebugMarkerBeginEXT)
  150. {
  151. VkDebugMarkerMarkerInfoEXT markerInfo = {};
  152. markerInfo.sType = VK_STRUCTURE_TYPE_DEBUG_MARKER_MARKER_INFO_EXT;
  153. markerInfo.color[0] = 1.0f;
  154. markerInfo.pMarkerName = (!name.isEmpty() && name.getLength() > 0) ? name.cstr() : "Unnamed";
  155. m_pfnCmdDebugMarkerBeginEXT(cmdb, &markerInfo);
  156. }
  157. }
  158. void endMarker(VkCommandBuffer cmdb) const
  159. {
  160. ANKI_ASSERT(cmdb);
  161. if(m_pfnCmdDebugMarkerEndEXT)
  162. {
  163. m_pfnCmdDebugMarkerEndEXT(cmdb);
  164. }
  165. }
  166. void trySetVulkanHandleName(CString name, VkDebugReportObjectTypeEXT type, U64 handle) const;
  167. void trySetVulkanHandleName(CString name, VkDebugReportObjectTypeEXT type, void* handle) const
  168. {
  169. trySetVulkanHandleName(name, type, U64(ptrToNumber(handle)));
  170. }
  171. StringAuto tryGetVulkanHandleName(U64 handle) const;
  172. StringAuto tryGetVulkanHandleName(void* handle) const
  173. {
  174. return tryGetVulkanHandleName(U64(ptrToNumber(handle)));
  175. }
  176. /// @}
  177. void printPipelineShaderInfo(VkPipeline ppline, CString name, ShaderTypeBit stages, U64 hash = 0) const;
  178. private:
  179. U64 m_frame = 0;
  180. #if ANKI_GR_MANAGER_DEBUG_MEMMORY
  181. VkAllocationCallbacks m_debugAllocCbs;
  182. static const U32 MAX_ALLOC_ALIGNMENT = 64;
  183. static const PtrSize ALLOC_SIG = 0xF00B00;
  184. struct alignas(MAX_ALLOC_ALIGNMENT) AllocHeader
  185. {
  186. PtrSize m_sig;
  187. PtrSize m_size;
  188. };
  189. #endif
  190. VkInstance m_instance = VK_NULL_HANDLE;
  191. VkDebugReportCallbackEXT m_debugCallback = VK_NULL_HANDLE;
  192. VkPhysicalDevice m_physicalDevice = VK_NULL_HANDLE;
  193. VulkanExtensions m_extensions = VulkanExtensions::NONE;
  194. VkDevice m_device = VK_NULL_HANDLE;
  195. Array<U32, U32(QueueType::COUNT)> m_queueFamilyIndices = {MAX_U32, MAX_U32};
  196. Array<VkQueue, U32(QueueType::COUNT)> m_queues = {};
  197. Mutex m_globalMtx;
  198. VkPhysicalDeviceProperties2 m_devProps = {};
  199. VkPhysicalDeviceAccelerationStructurePropertiesKHR m_accelerationStructureProps = {};
  200. VkPhysicalDeviceRayTracingPipelinePropertiesKHR m_rtPipelineProps = {};
  201. VkPhysicalDeviceFeatures m_devFeatures = {};
  202. VkPhysicalDeviceAccelerationStructureFeaturesKHR m_accelerationStructureFeatures = {};
  203. VkPhysicalDeviceRayTracingPipelineFeaturesKHR m_rtPipelineFeatures = {};
  204. VkPhysicalDeviceRayQueryFeaturesKHR m_rayQueryFeatures = {};
  205. VkPhysicalDeviceVulkan11Features m_11Features = {};
  206. VkPhysicalDeviceVulkan12Features m_12Features = {};
  207. VkPhysicalDevicePipelineExecutablePropertiesFeaturesKHR m_pplineExecutablePropertiesFeatures = {};
  208. PFN_vkDebugMarkerSetObjectNameEXT m_pfnDebugMarkerSetObjectNameEXT = nullptr;
  209. PFN_vkCmdDebugMarkerBeginEXT m_pfnCmdDebugMarkerBeginEXT = nullptr;
  210. PFN_vkCmdDebugMarkerEndEXT m_pfnCmdDebugMarkerEndEXT = nullptr;
  211. PFN_vkGetShaderInfoAMD m_pfnGetShaderInfoAMD = nullptr;
  212. mutable File m_shaderStatsFile;
  213. mutable SpinLock m_shaderStatsFileMtx;
  214. /// @name Surface_related
  215. /// @{
  216. class PerFrame
  217. {
  218. public:
  219. MicroFencePtr m_presentFence;
  220. MicroSemaphorePtr m_acquireSemaphore;
  221. /// Signaled by the submit that renders to the default FB. Present waits for it.
  222. MicroSemaphorePtr m_renderSemaphore;
  223. QueueType m_queueWroteToSwapchainImage = QueueType::COUNT;
  224. };
  225. VkSurfaceKHR m_surface = VK_NULL_HANDLE;
  226. MicroSwapchainPtr m_crntSwapchain;
  227. U8 m_acquiredImageIdx = MAX_U8;
  228. Array<PerFrame, MAX_FRAMES_IN_FLIGHT> m_perFrame;
  229. /// @}
  230. /// @name Memory
  231. /// @{
  232. VkPhysicalDeviceMemoryProperties m_memoryProperties;
  233. /// The main allocator.
  234. GpuMemoryManager m_gpuMemManager;
  235. /// @}
  236. CommandBufferFactory m_cmdbFactory;
  237. FenceFactory m_fenceFactory;
  238. SemaphoreFactory m_semaphoreFactory;
  239. DeferredBarrierFactory m_barrierFactory;
  240. SamplerFactory m_samplerFactory;
  241. /// @}
  242. SwapchainFactory m_swapchainFactory;
  243. PipelineLayoutFactory m_pplineLayoutFactory;
  244. DescriptorSetFactory m_descrFactory;
  245. QueryFactory m_occlusionQueryFactory;
  246. QueryFactory m_timestampQueryFactory;
  247. PipelineCache m_pplineCache;
  248. Bool m_r8g8b8ImagesSupported = false;
  249. Bool m_s8ImagesSupported = false;
  250. Bool m_d24S8ImagesSupported = false;
  251. mutable HashMap<U64, StringAuto> m_vkHandleToName;
  252. mutable SpinLock m_vkHandleToNameLock;
  253. ANKI_USE_RESULT Error initInternal(const GrManagerInitInfo& init);
  254. ANKI_USE_RESULT Error initInstance(const GrManagerInitInfo& init);
  255. ANKI_USE_RESULT Error initSurface(const GrManagerInitInfo& init);
  256. ANKI_USE_RESULT Error initDevice(const GrManagerInitInfo& init);
  257. ANKI_USE_RESULT Error initFramebuffers(const GrManagerInitInfo& init);
  258. ANKI_USE_RESULT Error initMemory(const ConfigSet& cfg);
  259. #if ANKI_GR_MANAGER_DEBUG_MEMMORY
  260. static void* allocateCallback(void* userData, size_t size, size_t alignment,
  261. VkSystemAllocationScope allocationScope);
  262. static void* reallocateCallback(void* userData, void* original, size_t size, size_t alignment,
  263. VkSystemAllocationScope allocationScope);
  264. static void freeCallback(void* userData, void* ptr);
  265. #endif
  266. void resetFrame(PerFrame& frame);
  267. static VkBool32 debugReportCallbackEXT(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objectType,
  268. uint64_t object, size_t location, int32_t messageCode,
  269. const char* pLayerPrefix, const char* pMessage, void* pUserData);
  270. ANKI_USE_RESULT Error printPipelineShaderInfoInternal(VkPipeline ppline, CString name, ShaderTypeBit stages,
  271. U64 hash) const;
  272. };
  273. /// @}
  274. } // end namespace anki