rendering_device_driver_vulkan.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /**************************************************************************/
  2. /* rendering_device_driver_vulkan.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/templates/hash_map.h"
  32. #include "core/templates/paged_allocator.h"
  33. #include "drivers/vulkan/rendering_context_driver_vulkan.h"
  34. #include "drivers/vulkan/rendering_shader_container_vulkan.h"
  35. #include "servers/rendering/rendering_device_driver.h"
  36. #ifdef DEBUG_ENABLED
  37. #ifndef _MSC_VER
  38. #define _DEBUG
  39. #endif
  40. #endif
  41. #include "thirdparty/re-spirv/re-spirv.h"
  42. #include "thirdparty/vulkan/vk_mem_alloc.h"
  43. #include "drivers/vulkan/godot_vulkan.h"
  44. // Design principles:
  45. // - Vulkan structs are zero-initialized and fields not requiring a non-zero value are omitted (except in cases where expresivity reasons apply).
  46. class RenderingDeviceDriverVulkan : public RenderingDeviceDriver {
  47. /*****************/
  48. /**** GENERIC ****/
  49. /*****************/
  50. struct CommandQueue;
  51. struct SwapChain;
  52. struct CommandBufferInfo;
  53. struct RenderPassInfo;
  54. struct Framebuffer;
  55. struct Queue {
  56. VkQueue queue = VK_NULL_HANDLE;
  57. uint32_t virtual_count = 0;
  58. BinaryMutex submit_mutex;
  59. };
  60. struct SubgroupCapabilities {
  61. uint32_t size = 0;
  62. uint32_t min_size = 0;
  63. uint32_t max_size = 0;
  64. VkShaderStageFlags supported_stages = 0;
  65. VkSubgroupFeatureFlags supported_operations = 0;
  66. VkBool32 quad_operations_in_all_stages = false;
  67. bool size_control_is_supported = false;
  68. uint32_t supported_stages_flags_rd() const;
  69. String supported_stages_desc() const;
  70. uint32_t supported_operations_flags_rd() const;
  71. String supported_operations_desc() const;
  72. };
  73. struct ShaderCapabilities {
  74. bool shader_float16_is_supported = false;
  75. bool shader_int8_is_supported = false;
  76. };
  77. struct StorageBufferCapabilities {
  78. bool storage_buffer_16_bit_access_is_supported = false;
  79. bool uniform_and_storage_buffer_16_bit_access_is_supported = false;
  80. bool storage_push_constant_16_is_supported = false;
  81. bool storage_input_output_16 = false;
  82. };
  83. struct AccelerationStructureCapabilities {
  84. bool acceleration_structure_support = false;
  85. uint32_t min_acceleration_structure_scratch_offset_alignment = 0;
  86. };
  87. struct RaytracingCapabilities {
  88. bool raytracing_pipeline_support = false;
  89. uint32_t shader_group_handle_size = 0;
  90. uint32_t shader_group_handle_alignment = 0;
  91. uint32_t shader_group_handle_size_aligned = 0;
  92. uint32_t shader_group_base_alignment = 0;
  93. bool validation = false;
  94. };
  95. struct DeviceFunctions {
  96. PFN_vkCreateSwapchainKHR CreateSwapchainKHR = nullptr;
  97. PFN_vkDestroySwapchainKHR DestroySwapchainKHR = nullptr;
  98. PFN_vkGetSwapchainImagesKHR GetSwapchainImagesKHR = nullptr;
  99. PFN_vkAcquireNextImageKHR AcquireNextImageKHR = nullptr;
  100. PFN_vkQueuePresentKHR QueuePresentKHR = nullptr;
  101. PFN_vkCreateRenderPass2KHR CreateRenderPass2KHR = nullptr;
  102. PFN_vkCmdEndRenderPass2KHR EndRenderPass2KHR = nullptr;
  103. // Debug marker extensions.
  104. PFN_vkCmdDebugMarkerBeginEXT CmdDebugMarkerBeginEXT = nullptr;
  105. PFN_vkCmdDebugMarkerEndEXT CmdDebugMarkerEndEXT = nullptr;
  106. PFN_vkCmdDebugMarkerInsertEXT CmdDebugMarkerInsertEXT = nullptr;
  107. PFN_vkDebugMarkerSetObjectNameEXT DebugMarkerSetObjectNameEXT = nullptr;
  108. // Debug device fault.
  109. PFN_vkGetDeviceFaultInfoEXT GetDeviceFaultInfoEXT = nullptr;
  110. // Raytracing extensions.
  111. PFN_vkCreateAccelerationStructureKHR CreateAccelerationStructureKHR = nullptr;
  112. PFN_vkCreateRayTracingPipelinesKHR CreateRaytracingPipelinesKHR = nullptr;
  113. };
  114. // Debug marker extensions.
  115. VkDebugReportObjectTypeEXT _convert_to_debug_report_objectType(VkObjectType p_object_type);
  116. VkDevice vk_device = VK_NULL_HANDLE;
  117. RenderingContextDriverVulkan *context_driver = nullptr;
  118. RenderingContextDriver::Device context_device = {};
  119. uint32_t frame_count = 1;
  120. VkPhysicalDevice physical_device = VK_NULL_HANDLE;
  121. VkPhysicalDeviceProperties physical_device_properties = {};
  122. VkPhysicalDeviceFeatures physical_device_features = {};
  123. VkPhysicalDeviceFeatures requested_device_features = {};
  124. HashMap<CharString, bool> requested_device_extensions;
  125. HashSet<CharString> enabled_device_extension_names;
  126. TightLocalVector<TightLocalVector<Queue>> queue_families;
  127. TightLocalVector<VkQueueFamilyProperties> queue_family_properties;
  128. RDD::Capabilities device_capabilities;
  129. SubgroupCapabilities subgroup_capabilities;
  130. MultiviewCapabilities multiview_capabilities;
  131. FragmentShadingRateCapabilities fsr_capabilities;
  132. FragmentDensityMapCapabilities fdm_capabilities;
  133. ShaderCapabilities shader_capabilities;
  134. StorageBufferCapabilities storage_buffer_capabilities;
  135. RenderingShaderContainerFormatVulkan shader_container_format;
  136. bool buffer_device_address_support = false;
  137. bool vulkan_memory_model_support = false;
  138. bool vulkan_memory_model_device_scope_support = false;
  139. AccelerationStructureCapabilities acceleration_structure_capabilities;
  140. bool ray_query_support = false;
  141. RaytracingCapabilities raytracing_capabilities;
  142. bool pipeline_cache_control_support = false;
  143. bool device_fault_support = false;
  144. bool framebuffer_depth_resolve = false;
  145. #if defined(VK_TRACK_DEVICE_MEMORY)
  146. bool device_memory_report_support = false;
  147. #endif
  148. #if defined(SWAPPY_FRAME_PACING_ENABLED)
  149. // Swappy frame pacer for Android.
  150. bool swappy_frame_pacer_enable = false;
  151. uint8_t swappy_mode = 2; // See default value for display/window/frame_pacing/android/swappy_mode.
  152. #endif
  153. DeviceFunctions device_functions;
  154. struct PendingFlushes {
  155. LocalVector<VmaAllocation> allocations;
  156. LocalVector<VkDeviceSize> offsets;
  157. LocalVector<VkDeviceSize> sizes;
  158. };
  159. PendingFlushes pending_flushes;
  160. struct PipelineStatistics {
  161. Ref<FileAccess> file_access;
  162. Mutex file_access_mutex;
  163. };
  164. PipelineStatistics pipeline_statistics;
  165. void _register_requested_device_extension(const CharString &p_extension_name, bool p_required);
  166. Error _initialize_device_extensions();
  167. Error _check_device_features();
  168. Error _check_device_capabilities();
  169. void _choose_vrs_capabilities();
  170. Error _add_queue_create_info(LocalVector<VkDeviceQueueCreateInfo> &r_queue_create_info);
  171. Error _initialize_device(const LocalVector<VkDeviceQueueCreateInfo> &p_queue_create_info);
  172. Error _initialize_allocator();
  173. Error _initialize_pipeline_cache();
  174. VkResult _create_render_pass(VkDevice p_device, const VkRenderPassCreateInfo2 *p_create_info, const VkAllocationCallbacks *p_allocator, VkRenderPass *p_render_pass);
  175. bool _release_image_semaphore(CommandQueue *p_command_queue, uint32_t p_semaphore_index, bool p_release_on_swap_chain);
  176. bool _recreate_image_semaphore(CommandQueue *p_command_queue, uint32_t p_semaphore_index, bool p_release_on_swap_chain);
  177. void _set_object_name(VkObjectType p_object_type, uint64_t p_object_handle, String p_object_name);
  178. public:
  179. Error initialize(uint32_t p_device_index, uint32_t p_frame_count) override final;
  180. private:
  181. /****************/
  182. /**** MEMORY ****/
  183. /****************/
  184. VmaAllocator allocator = nullptr;
  185. HashMap<uint32_t, VmaPool> small_allocs_pools;
  186. VmaPool _find_or_create_small_allocs_pool(uint32_t p_mem_type_index);
  187. private:
  188. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  189. // It's a circular buffer.
  190. BufferID breadcrumb_buffer;
  191. uint32_t breadcrumb_offset = 0u;
  192. uint32_t breadcrumb_id = 0u;
  193. #endif
  194. public:
  195. /*****************/
  196. /**** BUFFERS ****/
  197. /*****************/
  198. struct BufferInfo {
  199. VkBuffer vk_buffer = VK_NULL_HANDLE;
  200. struct {
  201. VmaAllocation handle = nullptr;
  202. uint64_t size = UINT64_MAX;
  203. } allocation;
  204. uint64_t size = 0;
  205. VkBufferView vk_view = VK_NULL_HANDLE; // For texel buffers.
  206. // If dynamic buffer, then its range is [0; RenderingDeviceDriverVulkan::frame_count)
  207. // else it's UINT32_MAX.
  208. uint32_t frame_idx = UINT32_MAX;
  209. bool is_dynamic() const { return frame_idx != UINT32_MAX; }
  210. };
  211. struct BufferDynamicInfo : BufferInfo {
  212. uint8_t *persistent_ptr = nullptr;
  213. #ifdef DEBUG_ENABLED
  214. // For tracking that a persistent buffer isn't mapped twice in the same frame.
  215. uint64_t last_frame_mapped = 0;
  216. #endif
  217. };
  218. virtual BufferID buffer_create(uint64_t p_size, BitField<BufferUsageBits> p_usage, MemoryAllocationType p_allocation_type, uint64_t p_frames_drawn) override final;
  219. virtual bool buffer_set_texel_format(BufferID p_buffer, DataFormat p_format) override final;
  220. virtual void buffer_free(BufferID p_buffer) override final;
  221. virtual uint64_t buffer_get_allocation_size(BufferID p_buffer) override final;
  222. virtual uint8_t *buffer_map(BufferID p_buffer) override final;
  223. virtual void buffer_unmap(BufferID p_buffer) override final;
  224. virtual uint8_t *buffer_persistent_map_advance(BufferID p_buffer, uint64_t p_frames_drawn) override final;
  225. virtual uint64_t buffer_get_dynamic_offsets(Span<BufferID> p_buffers) override final;
  226. virtual void buffer_flush(BufferID p_buffer) override final;
  227. virtual uint64_t buffer_get_device_address(BufferID p_buffer) override final;
  228. /*****************/
  229. /**** TEXTURE ****/
  230. /*****************/
  231. struct TextureInfo {
  232. VkImage vk_image = VK_NULL_HANDLE;
  233. VkImageView vk_view = VK_NULL_HANDLE;
  234. DataFormat rd_format = DATA_FORMAT_MAX;
  235. VkImageCreateInfo vk_create_info = {};
  236. VkImageViewCreateInfo vk_view_create_info = {};
  237. struct {
  238. VmaAllocation handle = nullptr;
  239. VmaAllocationInfo info = {};
  240. } allocation; // All 0/null if just a view.
  241. #ifdef DEBUG_ENABLED
  242. bool created_from_extension = false;
  243. bool transient = false;
  244. #endif
  245. };
  246. VkSampleCountFlagBits _ensure_supported_sample_count(TextureSamples p_requested_sample_count);
  247. public:
  248. virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) override final;
  249. virtual TextureID texture_create_from_extension(uint64_t p_native_texture, TextureType p_type, DataFormat p_format, uint32_t p_array_layers, bool p_depth_stencil, uint32_t p_mipmaps) override final;
  250. virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) override final;
  251. virtual TextureID texture_create_shared_from_slice(TextureID p_original_texture, const TextureView &p_view, TextureSliceType p_slice_type, uint32_t p_layer, uint32_t p_layers, uint32_t p_mipmap, uint32_t p_mipmaps) override final;
  252. virtual void texture_free(TextureID p_texture) override final;
  253. virtual uint64_t texture_get_allocation_size(TextureID p_texture) override final;
  254. virtual void texture_get_copyable_layout(TextureID p_texture, const TextureSubresource &p_subresource, TextureCopyableLayout *r_layout) override final;
  255. virtual Vector<uint8_t> texture_get_data(TextureID p_texture, uint32_t p_layer) override final;
  256. virtual BitField<TextureUsageBits> texture_get_usages_supported_by_format(DataFormat p_format, bool p_cpu_readable) override final;
  257. virtual bool texture_can_make_shared_with_format(TextureID p_texture, DataFormat p_format, bool &r_raw_reinterpretation) override final;
  258. /*****************/
  259. /**** SAMPLER ****/
  260. /*****************/
  261. public:
  262. virtual SamplerID sampler_create(const SamplerState &p_state) final override;
  263. virtual void sampler_free(SamplerID p_sampler) final override;
  264. virtual bool sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_filter) override final;
  265. /**********************/
  266. /**** VERTEX ARRAY ****/
  267. /**********************/
  268. private:
  269. struct VertexFormatInfo {
  270. TightLocalVector<VkVertexInputBindingDescription> vk_bindings;
  271. TightLocalVector<VkVertexInputAttributeDescription> vk_attributes;
  272. VkPipelineVertexInputStateCreateInfo vk_create_info = {};
  273. };
  274. public:
  275. virtual VertexFormatID vertex_format_create(Span<VertexAttribute> p_vertex_attribs, const VertexAttributeBindingsMap &p_vertex_bindings) override final;
  276. virtual void vertex_format_free(VertexFormatID p_vertex_format) override final;
  277. /******************/
  278. /**** BARRIERS ****/
  279. /******************/
  280. virtual void command_pipeline_barrier(
  281. CommandBufferID p_cmd_buffer,
  282. BitField<PipelineStageBits> p_src_stages,
  283. BitField<PipelineStageBits> p_dst_stages,
  284. VectorView<MemoryAccessBarrier> p_memory_barriers,
  285. VectorView<BufferBarrier> p_buffer_barriers,
  286. VectorView<TextureBarrier> p_texture_barriers,
  287. VectorView<AccelerationStructureBarrier> p_acceleration_structure_barriers) override final;
  288. /****************/
  289. /**** FENCES ****/
  290. /****************/
  291. private:
  292. struct Fence {
  293. VkFence vk_fence = VK_NULL_HANDLE;
  294. CommandQueue *queue_signaled_from = nullptr;
  295. };
  296. public:
  297. virtual FenceID fence_create() override final;
  298. virtual Error fence_wait(FenceID p_fence) override final;
  299. virtual void fence_free(FenceID p_fence) override final;
  300. /********************/
  301. /**** SEMAPHORES ****/
  302. /********************/
  303. virtual SemaphoreID semaphore_create() override final;
  304. virtual void semaphore_free(SemaphoreID p_semaphore) override final;
  305. /******************/
  306. /**** COMMANDS ****/
  307. /******************/
  308. // ----- QUEUE FAMILY -----
  309. virtual CommandQueueFamilyID command_queue_family_get(BitField<CommandQueueFamilyBits> p_cmd_queue_family_bits, RenderingContextDriver::SurfaceID p_surface = 0) override final;
  310. // ----- QUEUE -----
  311. private:
  312. struct CommandQueue {
  313. LocalVector<VkSemaphore> image_semaphores;
  314. LocalVector<SwapChain *> image_semaphores_swap_chains;
  315. LocalVector<uint32_t> pending_semaphores_for_execute;
  316. LocalVector<uint32_t> pending_semaphores_for_fence;
  317. LocalVector<uint32_t> free_image_semaphores;
  318. LocalVector<Pair<Fence *, uint32_t>> image_semaphores_for_fences;
  319. uint32_t queue_family = 0;
  320. uint32_t queue_index = 0;
  321. };
  322. public:
  323. virtual CommandQueueID command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue = false) override final;
  324. virtual Error command_queue_execute_and_present(CommandQueueID p_cmd_queue, VectorView<SemaphoreID> p_wait_semaphores, VectorView<CommandBufferID> p_cmd_buffers, VectorView<SemaphoreID> p_cmd_semaphores, FenceID p_cmd_fence, VectorView<SwapChainID> p_swap_chains) override final;
  325. virtual void command_queue_free(CommandQueueID p_cmd_queue) override final;
  326. private:
  327. // ----- POOL -----
  328. struct CommandPool {
  329. VkCommandPool vk_command_pool = VK_NULL_HANDLE;
  330. CommandBufferType buffer_type = COMMAND_BUFFER_TYPE_PRIMARY;
  331. LocalVector<CommandBufferInfo *> command_buffers_created;
  332. };
  333. public:
  334. virtual CommandPoolID command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) override final;
  335. virtual bool command_pool_reset(CommandPoolID p_cmd_pool) override final;
  336. virtual void command_pool_free(CommandPoolID p_cmd_pool) override final;
  337. private:
  338. // ----- BUFFER -----
  339. struct CommandBufferInfo {
  340. VkCommandBuffer vk_command_buffer = VK_NULL_HANDLE;
  341. Framebuffer *active_framebuffer = nullptr;
  342. RenderPassInfo *active_render_pass = nullptr;
  343. };
  344. public:
  345. virtual CommandBufferID command_buffer_create(CommandPoolID p_cmd_pool) override final;
  346. virtual bool command_buffer_begin(CommandBufferID p_cmd_buffer) override final;
  347. virtual bool command_buffer_begin_secondary(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, uint32_t p_subpass, FramebufferID p_framebuffer) override final;
  348. virtual void command_buffer_end(CommandBufferID p_cmd_buffer) override final;
  349. virtual void command_buffer_execute_secondary(CommandBufferID p_cmd_buffer, VectorView<CommandBufferID> p_secondary_cmd_buffers) override final;
  350. /********************/
  351. /**** SWAP CHAIN ****/
  352. /********************/
  353. private:
  354. struct SwapChain {
  355. VkSwapchainKHR vk_swapchain = VK_NULL_HANDLE;
  356. RenderingContextDriver::SurfaceID surface = RenderingContextDriver::SurfaceID();
  357. VkFormat format = VK_FORMAT_UNDEFINED;
  358. VkColorSpaceKHR color_space = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
  359. TightLocalVector<VkImage> images;
  360. TightLocalVector<VkImageView> image_views;
  361. TightLocalVector<VkSemaphore> present_semaphores;
  362. TightLocalVector<FramebufferID> framebuffers;
  363. LocalVector<CommandQueue *> command_queues_acquired;
  364. LocalVector<uint32_t> command_queues_acquired_semaphores;
  365. RenderPassID render_pass;
  366. int pre_transform_rotation_degrees = 0;
  367. uint32_t image_index = 0;
  368. #ifdef ANDROID_ENABLED
  369. uint64_t refresh_duration = 0;
  370. #endif
  371. };
  372. void _swap_chain_release(SwapChain *p_swap_chain);
  373. public:
  374. virtual SwapChainID swap_chain_create(RenderingContextDriver::SurfaceID p_surface) override final;
  375. virtual Error swap_chain_resize(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, uint32_t p_desired_framebuffer_count) override final;
  376. virtual FramebufferID swap_chain_acquire_framebuffer(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, bool &r_resize_required) override final;
  377. virtual RenderPassID swap_chain_get_render_pass(SwapChainID p_swap_chain) override final;
  378. virtual int swap_chain_get_pre_rotation_degrees(SwapChainID p_swap_chain) override final;
  379. virtual DataFormat swap_chain_get_format(SwapChainID p_swap_chain) override final;
  380. virtual void swap_chain_set_max_fps(SwapChainID p_swap_chain, int p_max_fps) override final;
  381. virtual void swap_chain_free(SwapChainID p_swap_chain) override final;
  382. private:
  383. /*********************/
  384. /**** FRAMEBUFFER ****/
  385. /*********************/
  386. struct Framebuffer {
  387. VkFramebuffer vk_framebuffer = VK_NULL_HANDLE;
  388. // Only filled in if the framebuffer uses a fragment density map with offsets. Unused otherwise.
  389. uint32_t fragment_density_map_offsets_layers = 0;
  390. // Only filled in by a framebuffer created by a swap chain. Unused otherwise.
  391. VkImage swap_chain_image = VK_NULL_HANDLE;
  392. VkImageSubresourceRange swap_chain_image_subresource_range = {};
  393. bool swap_chain_acquired = false;
  394. };
  395. public:
  396. virtual FramebufferID framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height) override final;
  397. virtual void framebuffer_free(FramebufferID p_framebuffer) override final;
  398. /****************/
  399. /**** SHADER ****/
  400. /****************/
  401. private:
  402. struct RaytracingShaderRegionCount {
  403. uint32_t raygen_count = 0;
  404. uint32_t hit_count = 0;
  405. uint32_t miss_count = 0;
  406. uint32_t group_count = 0;
  407. };
  408. struct ShaderInfo {
  409. String name;
  410. VkShaderStageFlags vk_push_constant_stages = 0;
  411. TightLocalVector<VkPipelineShaderStageCreateInfo> vk_stages_create_info;
  412. TightLocalVector<VkRayTracingShaderGroupCreateInfoKHR> vk_groups_create_info;
  413. TightLocalVector<VkDescriptorSetLayout> vk_descriptor_set_layouts;
  414. TightLocalVector<respv::Shader> respv_stage_shaders;
  415. TightLocalVector<Vector<uint8_t>> spirv_stage_bytes;
  416. TightLocalVector<uint64_t> original_stage_size;
  417. VkPipelineLayout vk_pipeline_layout = VK_NULL_HANDLE;
  418. // Used to update the shader binding table buffer.
  419. RaytracingShaderRegionCount region_count;
  420. };
  421. public:
  422. virtual ShaderID shader_create_from_container(const Ref<RenderingShaderContainer> &p_shader_container, const Vector<ImmutableSampler> &p_immutable_samplers) override final;
  423. virtual void shader_free(ShaderID p_shader) override final;
  424. virtual void shader_destroy_modules(ShaderID p_shader) override final;
  425. /*********************/
  426. /**** UNIFORM SET ****/
  427. /*********************/
  428. // Descriptor sets require allocation from a pool.
  429. // The documentation on how to use pools properly
  430. // is scarce, and the documentation is strange.
  431. //
  432. // Basically, you can mix and match pools as you
  433. // like, but you'll run into fragmentation issues.
  434. // Because of this, the recommended approach is to
  435. // create a pool for every descriptor set type, as
  436. // this prevents fragmentation.
  437. //
  438. // This is implemented here as a having a list of
  439. // pools (each can contain up to 64 sets) for each
  440. // set layout. The amount of sets for each type
  441. // is used as the key.
  442. private:
  443. static const uint32_t MAX_UNIFORM_POOL_ELEMENT = 65535;
  444. struct DescriptorSetPoolKey {
  445. uint16_t uniform_type[UNIFORM_TYPE_MAX] = {};
  446. bool operator<(const DescriptorSetPoolKey &p_other) const {
  447. return memcmp(uniform_type, p_other.uniform_type, sizeof(uniform_type)) < 0;
  448. }
  449. };
  450. using DescriptorSetPools = RBMap<DescriptorSetPoolKey, HashMap<VkDescriptorPool, uint32_t>>;
  451. DescriptorSetPools descriptor_set_pools;
  452. uint32_t max_descriptor_sets_per_pool = 0;
  453. HashMap<int, DescriptorSetPools> linear_descriptor_set_pools;
  454. bool linear_descriptor_pools_enabled = true;
  455. VkDescriptorPool _descriptor_set_pool_create(const DescriptorSetPoolKey &p_key, bool p_linear_pool);
  456. void _descriptor_set_pool_unreference(DescriptorSetPools::Iterator p_pool_sets_it, VkDescriptorPool p_vk_descriptor_pool, int p_linear_pool_index);
  457. // Global flag to toggle usage of immutable sampler when creating pipeline layouts.
  458. // It cannot change after creating the PSOs, since we need to skipping samplers when creating uniform sets.
  459. bool immutable_samplers_enabled = true;
  460. struct UniformSetInfo {
  461. VkDescriptorSet vk_descriptor_set = VK_NULL_HANDLE;
  462. VkDescriptorPool vk_descriptor_pool = VK_NULL_HANDLE;
  463. VkDescriptorPool vk_linear_descriptor_pool = VK_NULL_HANDLE;
  464. DescriptorSetPools::Iterator pool_sets_it;
  465. TightLocalVector<BufferInfo const *, uint32_t> dynamic_buffers;
  466. };
  467. bool adreno_5xx_empty_descriptor_set_layout_workaround = false;
  468. public:
  469. virtual UniformSetID uniform_set_create(VectorView<BoundUniform> p_uniforms, ShaderID p_shader, uint32_t p_set_index, int p_linear_pool_index) override final;
  470. virtual void linear_uniform_set_pools_reset(int p_linear_pool_index) override final;
  471. virtual void uniform_set_free(UniformSetID p_uniform_set) override final;
  472. virtual bool uniform_sets_have_linear_pools() const override final;
  473. virtual uint32_t uniform_sets_get_dynamic_offsets(VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count) const override final;
  474. // ----- COMMANDS -----
  475. virtual void command_uniform_set_prepare_for_use(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) override final;
  476. /******************/
  477. /**** TRANSFER ****/
  478. /******************/
  479. virtual void command_clear_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, uint64_t p_offset, uint64_t p_size) override final;
  480. virtual void command_copy_buffer(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, BufferID p_dst_buffer, VectorView<BufferCopyRegion> p_regions) override final;
  481. virtual void command_copy_texture(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, VectorView<TextureCopyRegion> p_regions) override final;
  482. virtual void command_resolve_texture(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, uint32_t p_src_layer, uint32_t p_src_mipmap, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, uint32_t p_dst_layer, uint32_t p_dst_mipmap) override final;
  483. virtual void command_clear_color_texture(CommandBufferID p_cmd_buffer, TextureID p_texture, TextureLayout p_texture_layout, const Color &p_color, const TextureSubresourceRange &p_subresources) override final;
  484. virtual void command_clear_depth_stencil_texture(CommandBufferID p_cmd_buffer, TextureID p_texture, TextureLayout p_texture_layout, float p_depth, uint8_t p_stencil, const TextureSubresourceRange &p_subresources) override final;
  485. virtual void command_copy_buffer_to_texture(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, VectorView<BufferTextureCopyRegion> p_regions) override final;
  486. virtual void command_copy_texture_to_buffer(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, BufferID p_dst_buffer, VectorView<BufferTextureCopyRegion> p_regions) override final;
  487. /******************/
  488. /**** PIPELINE ****/
  489. /******************/
  490. private:
  491. struct PipelineCacheHeader {
  492. uint32_t magic = 0;
  493. uint32_t data_size = 0;
  494. uint64_t data_hash = 0;
  495. uint32_t vendor_id = 0;
  496. uint32_t device_id = 0;
  497. uint32_t driver_version = 0;
  498. uint8_t uuid[VK_UUID_SIZE] = {};
  499. uint8_t driver_abi = 0;
  500. };
  501. struct PipelineCache {
  502. String file_path;
  503. size_t current_size = 0;
  504. Vector<uint8_t> buffer; // Header then data.
  505. VkPipelineCache vk_cache = VK_NULL_HANDLE;
  506. };
  507. static int caching_instance_count;
  508. PipelineCache pipelines_cache;
  509. String pipeline_cache_id;
  510. HashMap<uint64_t, bool> has_comp_alpha;
  511. public:
  512. virtual void pipeline_free(PipelineID p_pipeline) override final;
  513. // ----- BINDING -----
  514. virtual void command_bind_push_constants(CommandBufferID p_cmd_buffer, ShaderID p_shader, uint32_t p_first_index, VectorView<uint32_t> p_data) override final;
  515. // ----- CACHE -----
  516. virtual bool pipeline_cache_create(const Vector<uint8_t> &p_data) override final;
  517. virtual void pipeline_cache_free() override final;
  518. virtual size_t pipeline_cache_query_size() override final;
  519. virtual Vector<uint8_t> pipeline_cache_serialize() override final;
  520. /*******************/
  521. /**** RENDERING ****/
  522. /*******************/
  523. private:
  524. // ----- SUBPASS -----
  525. struct RenderPassInfo {
  526. VkRenderPass vk_render_pass = VK_NULL_HANDLE;
  527. bool uses_fragment_density_map = false;
  528. };
  529. public:
  530. virtual RenderPassID render_pass_create(VectorView<Attachment> p_attachments, VectorView<Subpass> p_subpasses, VectorView<SubpassDependency> p_subpass_dependencies, uint32_t p_view_count, AttachmentReference p_fragment_density_map_attachment) override final;
  531. virtual void render_pass_free(RenderPassID p_render_pass) override final;
  532. // ----- COMMANDS -----
  533. virtual void command_begin_render_pass(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, FramebufferID p_framebuffer, CommandBufferType p_cmd_buffer_type, const Rect2i &p_rect, VectorView<RenderPassClearValue> p_clear_values) override final;
  534. virtual void command_end_render_pass(CommandBufferID p_cmd_buffer) override final;
  535. virtual void command_next_render_subpass(CommandBufferID p_cmd_buffer, CommandBufferType p_cmd_buffer_type) override final;
  536. virtual void command_render_set_viewport(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_viewports) override final;
  537. virtual void command_render_set_scissor(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_scissors) override final;
  538. virtual void command_render_clear_attachments(CommandBufferID p_cmd_buffer, VectorView<AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) override final;
  539. // Binding.
  540. virtual void command_bind_render_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;
  541. virtual void command_bind_render_uniform_sets(CommandBufferID p_cmd_buffer, VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count, uint32_t p_dynamic_offsets) override final;
  542. // Drawing.
  543. virtual void command_render_draw(CommandBufferID p_cmd_buffer, uint32_t p_vertex_count, uint32_t p_instance_count, uint32_t p_base_vertex, uint32_t p_first_instance) override final;
  544. virtual void command_render_draw_indexed(CommandBufferID p_cmd_buffer, uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index, int32_t p_vertex_offset, uint32_t p_first_instance) override final;
  545. virtual void command_render_draw_indexed_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride) override final;
  546. virtual void command_render_draw_indexed_indirect_count(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride) override final;
  547. virtual void command_render_draw_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride) override final;
  548. virtual void command_render_draw_indirect_count(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride) override final;
  549. // Buffer binding.
  550. virtual void command_render_bind_vertex_buffers(CommandBufferID p_cmd_buffer, uint32_t p_binding_count, const BufferID *p_buffers, const uint64_t *p_offsets, uint64_t p_dynamic_offsets) override final;
  551. virtual void command_render_bind_index_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, IndexBufferFormat p_format, uint64_t p_offset) override final;
  552. // Dynamic state.
  553. virtual void command_render_set_blend_constants(CommandBufferID p_cmd_buffer, const Color &p_constants) override final;
  554. virtual void command_render_set_line_width(CommandBufferID p_cmd_buffer, float p_width) override final;
  555. // ----- PIPELINE -----
  556. virtual PipelineID render_pipeline_create(
  557. ShaderID p_shader,
  558. VertexFormatID p_vertex_format,
  559. RenderPrimitive p_render_primitive,
  560. PipelineRasterizationState p_rasterization_state,
  561. PipelineMultisampleState p_multisample_state,
  562. PipelineDepthStencilState p_depth_stencil_state,
  563. PipelineColorBlendState p_blend_state,
  564. VectorView<int32_t> p_color_attachments,
  565. BitField<PipelineDynamicStateFlags> p_dynamic_state,
  566. RenderPassID p_render_pass,
  567. uint32_t p_render_subpass,
  568. VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
  569. /*****************/
  570. /**** COMPUTE ****/
  571. /*****************/
  572. // ----- COMMANDS -----
  573. // Binding.
  574. virtual void command_bind_compute_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) override final;
  575. virtual void command_bind_compute_uniform_sets(CommandBufferID p_cmd_buffer, VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count, uint32_t p_dynamic_offsets) override final;
  576. // Dispatching.
  577. virtual void command_compute_dispatch(CommandBufferID p_cmd_buffer, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) override final;
  578. virtual void command_compute_dispatch_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset) override final;
  579. // ----- PIPELINE -----
  580. virtual PipelineID compute_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
  581. /********************/
  582. /**** RAYTRACING ****/
  583. /********************/
  584. // ----- ACCELERATION STRUCTURE -----
  585. struct AccelerationStructureInfo {
  586. VkAccelerationStructureKHR vk_acceleration_structure = VK_NULL_HANDLE;
  587. // Buffer used for the structure
  588. RDD::BufferID buffer;
  589. // Alignment of the scratch buffer for building the structure
  590. uint32_t scratch_alignment;
  591. // Size of the scratch buffer for building the structure
  592. uint32_t scratch_size;
  593. // Buffer used for instances in a TLAS
  594. RDD::BufferID instances_buffer;
  595. // Required for building
  596. VkAccelerationStructureGeometryKHR geometry;
  597. LocalVector<VkAccelerationStructureInstanceKHR> instances;
  598. VkAccelerationStructureBuildGeometryInfoKHR build_info;
  599. VkAccelerationStructureBuildRangeInfoKHR range_info;
  600. };
  601. virtual AccelerationStructureID blas_create(BufferID p_vertex_buffer, uint64_t p_vertex_offset, VertexFormatID p_vertex_format, uint32_t p_vertex_count, uint32_t p_position_attribute_location, BufferID p_index_buffer, IndexBufferFormat p_index_format, uint64_t p_index_offset_bytes, uint32_t p_index_count, BitField<AccelerationStructureGeometryBits> p_geometry_bits) override final;
  602. virtual uint32_t tlas_instances_buffer_get_size_bytes(uint32_t p_instance_count) override final;
  603. virtual void tlas_instances_buffer_fill(BufferID p_instances_buffer, VectorView<AccelerationStructureID> p_blases, VectorView<Transform3D> p_transforms) override final;
  604. virtual AccelerationStructureID tlas_create(BufferID p_instances_buffer) override final;
  605. virtual void acceleration_structure_free(AccelerationStructureID p_acceleration_structure) override final;
  606. virtual uint32_t acceleration_structure_get_scratch_size_bytes(AccelerationStructureID p_acceleration_structure) override final;
  607. private:
  608. void _acceleration_structure_create(VkAccelerationStructureTypeKHR p_type, VkAccelerationStructureBuildSizesInfoKHR p_size_info, AccelerationStructureInfo *r_accel_info);
  609. public:
  610. // ----- COMMANDS -----
  611. virtual void command_build_acceleration_structure(CommandBufferID p_cmd_buffer, AccelerationStructureID p_acceleration_structure, BufferID p_scratch_buffer) override final;
  612. virtual void command_bind_raytracing_pipeline(CommandBufferID p_cmd_buffer, RaytracingPipelineID p_pipeline) override final;
  613. virtual void command_bind_raytracing_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) override final;
  614. virtual void command_trace_rays(CommandBufferID p_cmd_buffer, uint32_t p_width, uint32_t p_height) override final;
  615. private:
  616. RaytracingPipelineID bound_raytracing_pipeline_id;
  617. // ----- PIPELINE -----
  618. struct RaytracingShaderRegions {
  619. VkStridedDeviceAddressRegionKHR raygen;
  620. VkStridedDeviceAddressRegionKHR hit;
  621. VkStridedDeviceAddressRegionKHR miss;
  622. VkStridedDeviceAddressRegionKHR call;
  623. };
  624. struct RaytracingPipelineInfo {
  625. VkPipeline vk_pipeline = VK_NULL_HANDLE;
  626. ShaderID shader;
  627. // Used vkCmdTraceRaysKHR.
  628. RaytracingShaderRegions regions;
  629. // Shader binding table.
  630. BufferID sbt_buffer;
  631. };
  632. public:
  633. virtual RaytracingPipelineID raytracing_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) override final;
  634. VkResult _raytracing_pipeline_stb_create(RaytracingPipelineID p_pipeline, ShaderID p_shader);
  635. virtual void raytracing_pipeline_free(RaytracingPipelineID p_pipeline) override final;
  636. /*****************/
  637. /**** QUERIES ****/
  638. /*****************/
  639. // ----- TIMESTAMP -----
  640. // Basic.
  641. virtual QueryPoolID timestamp_query_pool_create(uint32_t p_query_count) override final;
  642. virtual void timestamp_query_pool_free(QueryPoolID p_pool_id) override final;
  643. virtual void timestamp_query_pool_get_results(QueryPoolID p_pool_id, uint32_t p_query_count, uint64_t *r_results) override final;
  644. virtual uint64_t timestamp_query_result_to_time(uint64_t p_result) override final;
  645. // Commands.
  646. virtual void command_timestamp_query_pool_reset(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_query_count) override final;
  647. virtual void command_timestamp_write(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_index) override final;
  648. /****************/
  649. /**** LABELS ****/
  650. /****************/
  651. virtual void command_begin_label(CommandBufferID p_cmd_buffer, const char *p_label_name, const Color &p_color) override final;
  652. virtual void command_end_label(CommandBufferID p_cmd_buffer) override final;
  653. /****************/
  654. /**** DEBUG *****/
  655. /****************/
  656. virtual void command_insert_breadcrumb(CommandBufferID p_cmd_buffer, uint32_t p_data) override final;
  657. void print_lost_device_info();
  658. void on_device_lost() const;
  659. static String get_vulkan_result(VkResult err);
  660. /********************/
  661. /**** SUBMISSION ****/
  662. /********************/
  663. virtual void begin_segment(uint32_t p_frame_index, uint32_t p_frames_drawn) override final;
  664. virtual void end_segment() override final;
  665. /**************/
  666. /**** MISC ****/
  667. /**************/
  668. virtual void set_object_name(ObjectType p_type, ID p_driver_id, const String &p_name) override final;
  669. virtual uint64_t get_resource_native_handle(DriverResource p_type, ID p_driver_id) override final;
  670. virtual uint64_t get_total_memory_used() override final;
  671. virtual uint64_t get_lazily_memory_used() override final;
  672. virtual uint64_t limit_get(Limit p_limit) override final;
  673. virtual uint64_t api_trait_get(ApiTrait p_trait) override final;
  674. virtual bool has_feature(Features p_feature) override final;
  675. virtual const MultiviewCapabilities &get_multiview_capabilities() override final;
  676. virtual const FragmentShadingRateCapabilities &get_fragment_shading_rate_capabilities() override final;
  677. virtual const FragmentDensityMapCapabilities &get_fragment_density_map_capabilities() override final;
  678. virtual String get_api_name() const override final;
  679. virtual String get_api_version() const override final;
  680. virtual String get_pipeline_cache_uuid() const override final;
  681. virtual const Capabilities &get_capabilities() const override final;
  682. virtual const RenderingShaderContainerFormat &get_shader_container_format() const override final;
  683. virtual bool is_composite_alpha_supported(CommandQueueID p_queue) const override final;
  684. private:
  685. /*********************/
  686. /**** BOOKKEEPING ****/
  687. /*********************/
  688. using VersatileResource = VersatileResourceTemplate<
  689. BufferInfo,
  690. TextureInfo,
  691. VertexFormatInfo,
  692. ShaderInfo,
  693. UniformSetInfo,
  694. RenderPassInfo,
  695. CommandBufferInfo>;
  696. PagedAllocator<VersatileResource, true> resources_allocator;
  697. /******************/
  698. public:
  699. RenderingDeviceDriverVulkan(RenderingContextDriverVulkan *p_context_driver);
  700. virtual ~RenderingDeviceDriverVulkan();
  701. };
  702. using VKC = RenderingContextDriverVulkan;