RmlUi_Renderer_VK.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #ifndef RMLUI_BACKENDS_RENDERER_VK_H
  29. #define RMLUI_BACKENDS_RENDERER_VK_H
  30. #include <RmlUi/Core/RenderInterface.h>
  31. #ifdef RMLUI_PLATFORM_WIN32
  32. #include "RmlUi_Include_Windows.h"
  33. #define VK_USE_PLATFORM_WIN32_KHR
  34. #endif
  35. #include "RmlUi_Include_Vulkan.h"
  36. #ifdef RMLUI_DEBUG
  37. #define RMLUI_VK_ASSERTMSG(statement, msg) RMLUI_ASSERTMSG(statement, msg)
  38. // Uncomment the following line to enable additional Vulkan debugging.
  39. // #define RMLUI_VK_DEBUG
  40. #else
  41. #define RMLUI_VK_ASSERTMSG(statement, msg) static_cast<void>(statement)
  42. #endif
  43. // your specified api version, but in future it will be dynamic ^_^
  44. #define RMLUI_VK_API_VERSION VK_API_VERSION_1_0
  45. /**
  46. * Vulkan render interface for RmlUi
  47. *
  48. * My aim is to create compact, but easy to use class
  49. * I understand that it isn't good architectural choice to keep all things in one class
  50. * But I follow to RMLUI design and for implementing one GAPI backend it just needs one class
  51. * For user looks cool, but for programmer...
  52. *
  53. * It's better to try operate with very clean 'one-class' architecture rather than create own library for Vulkan
  54. * With many different classes, with not trivial signatures and etc
  55. * And as a result we should document that library so it's just a headache for all of us
  56. *
  57. * Reminder to users: If you want to implement your Vulkan renderer check previous commits of this work, because current system works only with new
  58. * and delete operations every frame (CPU side), on GPU we implemented the pre-allocated buffer with virtual allocs (Vma) so there's no problems and
  59. * all fine. I wrote all ideas and implementation for that.
  60. *
  61. * @author wh1t3lord (https://github.com/wh1t3lord)
  62. */
  63. class RenderInterface_VK : public Rml::RenderInterface {
  64. public:
  65. static constexpr uint32_t kSwapchainBackBufferCount = 3;
  66. static constexpr VkDeviceSize kVideoMemoryForAllocation = 4 * 1024 * 1024; // [bytes]
  67. RenderInterface_VK();
  68. ~RenderInterface_VK();
  69. using CreateSurfaceCallback = bool (*)(VkInstance instance, VkSurfaceKHR* out_surface);
  70. bool Initialize(Rml::Vector<const char*> required_extensions, CreateSurfaceCallback create_surface_callback);
  71. void Shutdown();
  72. void BeginFrame();
  73. void EndFrame();
  74. void SetViewport(int width, int height);
  75. bool IsSwapchainValid();
  76. void RecreateSwapchain();
  77. // -- Inherited from Rml::RenderInterface --
  78. /// Called by RmlUi when it wants to compile geometry it believes will be static for the forseeable future.
  79. Rml::CompiledGeometryHandle CompileGeometry(Rml::Span<const Rml::Vertex> vertices, Rml::Span<const int> indices) override;
  80. /// Called by RmlUi when it wants to render application-compiled geometry.
  81. void RenderGeometry(Rml::CompiledGeometryHandle handle, Rml::Vector2f translation, Rml::TextureHandle texture) override;
  82. /// Called by RmlUi when it wants to release application-compiled geometry.
  83. void ReleaseGeometry(Rml::CompiledGeometryHandle geometry) override;
  84. /// Called by RmlUi when a texture is required by the library.
  85. Rml::TextureHandle LoadTexture(Rml::Vector2i& texture_dimensions, const Rml::String& source) override;
  86. /// Called by RmlUi when a texture is required to be built from an internally-generated sequence of pixels.
  87. Rml::TextureHandle GenerateTexture(Rml::Span<const Rml::byte> source_data, Rml::Vector2i source_dimensions) override;
  88. /// Called by RmlUi when a loaded texture is no longer required.
  89. void ReleaseTexture(Rml::TextureHandle texture_handle) override;
  90. /// Called by RmlUi when it wants to enable or disable scissoring to clip content.
  91. void EnableScissorRegion(bool enable) override;
  92. /// Called by RmlUi when it wants to change the scissor region.
  93. void SetScissorRegion(Rml::Rectanglei region) override;
  94. /// Called by RmlUi when it wants to set the current transform matrix to a new matrix.
  95. void SetTransform(const Rml::Matrix4f* transform) override;
  96. private:
  97. enum class shader_type_t : int { Vertex, Fragment, Unknown = -1 };
  98. enum class shader_id_t : int { Vertex, Fragment_WithoutTextures, Fragment_WithTextures };
  99. struct shader_vertex_user_data_t {
  100. // Member objects are order-sensitive to match shader.
  101. Rml::Matrix4f m_transform;
  102. Rml::Vector2f m_translate;
  103. };
  104. struct texture_data_t {
  105. VkImage m_p_vk_image;
  106. VkImageView m_p_vk_image_view;
  107. VkSampler m_p_vk_sampler;
  108. VkDescriptorSet m_p_vk_descriptor_set;
  109. VmaAllocation m_p_vma_allocation;
  110. };
  111. struct geometry_handle_t {
  112. int m_num_indices;
  113. VkDescriptorBufferInfo m_p_vertex;
  114. VkDescriptorBufferInfo m_p_index;
  115. VkDescriptorBufferInfo m_p_shader;
  116. // @ this is for freeing our logical blocks for VMA
  117. // see https://gpuopen-librariesandsdks.github.io/VulkanMemoryAllocator/html/virtual_allocator.html
  118. VmaVirtualAllocation m_p_vertex_allocation;
  119. VmaVirtualAllocation m_p_index_allocation;
  120. VmaVirtualAllocation m_p_shader_allocation;
  121. };
  122. struct buffer_data_t {
  123. VkBuffer m_p_vk_buffer;
  124. VmaAllocation m_p_vma_allocation;
  125. };
  126. class UploadResourceManager {
  127. public:
  128. UploadResourceManager() : m_p_device{}, m_p_fence{}, m_p_command_buffer{}, m_p_command_pool{}, m_p_graphics_queue{} {}
  129. ~UploadResourceManager() {}
  130. void Initialize(VkDevice p_device, VkQueue p_queue, uint32_t queue_family_index)
  131. {
  132. RMLUI_VK_ASSERTMSG(p_queue, "you have to pass a valid VkQueue");
  133. RMLUI_VK_ASSERTMSG(p_device, "you have to pass a valid VkDevice for creation resources");
  134. m_p_device = p_device;
  135. m_p_graphics_queue = p_queue;
  136. Create_All(queue_family_index);
  137. }
  138. void Shutdown()
  139. {
  140. vkDestroyFence(m_p_device, m_p_fence, nullptr);
  141. vkDestroyCommandPool(m_p_device, m_p_command_pool, nullptr);
  142. }
  143. template <typename Func>
  144. void UploadToGPU(Func&& p_user_commands) noexcept
  145. {
  146. RMLUI_VK_ASSERTMSG(m_p_command_buffer, "you didn't initialize VkCommandBuffer");
  147. VkCommandBufferBeginInfo info_command = {};
  148. info_command.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
  149. info_command.pNext = nullptr;
  150. info_command.pInheritanceInfo = nullptr;
  151. info_command.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
  152. VkResult status = vkBeginCommandBuffer(m_p_command_buffer, &info_command);
  153. RMLUI_VK_ASSERTMSG(status == VkResult::VK_SUCCESS, "failed to vkBeginCommandBuffer");
  154. p_user_commands(m_p_command_buffer);
  155. status = vkEndCommandBuffer(m_p_command_buffer);
  156. RMLUI_VK_ASSERTMSG(status == VkResult::VK_SUCCESS, "faield to vkEndCommandBuffer");
  157. Submit();
  158. Wait();
  159. }
  160. private:
  161. void Create_Fence() noexcept
  162. {
  163. VkFenceCreateInfo info = {};
  164. info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
  165. info.pNext = nullptr;
  166. info.flags = 0;
  167. VkResult status = vkCreateFence(m_p_device, &info, nullptr, &m_p_fence);
  168. RMLUI_VK_ASSERTMSG(status == VkResult::VK_SUCCESS, "failed to vkCreateFence");
  169. }
  170. void Create_CommandBuffer() noexcept
  171. {
  172. RMLUI_VK_ASSERTMSG(m_p_command_pool, "you have to initialize VkCommandPool before calling this method!");
  173. VkCommandBufferAllocateInfo info = {};
  174. info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  175. info.pNext = nullptr;
  176. info.commandPool = m_p_command_pool;
  177. info.commandBufferCount = 1;
  178. info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
  179. VkResult status = vkAllocateCommandBuffers(m_p_device, &info, &m_p_command_buffer);
  180. RMLUI_VK_ASSERTMSG(status == VkResult::VK_SUCCESS, "failed to vkAllocateCommandBuffers");
  181. }
  182. void Create_CommandPool(uint32_t queue_family_index) noexcept
  183. {
  184. VkCommandPoolCreateInfo info = {};
  185. info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  186. info.pNext = nullptr;
  187. info.queueFamilyIndex = queue_family_index;
  188. info.flags = 0;
  189. VkResult status = vkCreateCommandPool(m_p_device, &info, nullptr, &m_p_command_pool);
  190. RMLUI_VK_ASSERTMSG(status == VkResult::VK_SUCCESS, "failed to vkCreateCommandPool");
  191. }
  192. void Create_All(uint32_t queue_family_index) noexcept
  193. {
  194. Create_Fence();
  195. Create_CommandPool(queue_family_index);
  196. Create_CommandBuffer();
  197. }
  198. void Wait() noexcept
  199. {
  200. RMLUI_VK_ASSERTMSG(m_p_fence, "you must initialize your VkFence");
  201. vkWaitForFences(m_p_device, 1, &m_p_fence, VK_TRUE, UINT64_MAX);
  202. vkResetFences(m_p_device, 1, &m_p_fence);
  203. vkResetCommandPool(m_p_device, m_p_command_pool, 0);
  204. }
  205. void Submit() noexcept
  206. {
  207. VkSubmitInfo info = {};
  208. info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  209. info.pNext = nullptr;
  210. info.waitSemaphoreCount = 0;
  211. info.signalSemaphoreCount = 0;
  212. info.pSignalSemaphores = nullptr;
  213. info.pWaitSemaphores = nullptr;
  214. info.pWaitDstStageMask = nullptr;
  215. info.pCommandBuffers = &m_p_command_buffer;
  216. info.commandBufferCount = 1;
  217. auto status = vkQueueSubmit(m_p_graphics_queue, 1, &info, m_p_fence);
  218. RMLUI_VK_ASSERTMSG(status == VkResult::VK_SUCCESS, "failed to vkQueueSubmit");
  219. }
  220. private:
  221. VkDevice m_p_device;
  222. VkFence m_p_fence;
  223. VkCommandBuffer m_p_command_buffer;
  224. VkCommandPool m_p_command_pool;
  225. VkQueue m_p_graphics_queue;
  226. };
  227. // @ main manager for "allocating" vertex, index, uniform stuff
  228. class MemoryPool {
  229. public:
  230. MemoryPool();
  231. ~MemoryPool();
  232. void Initialize(VkDeviceSize byte_size, VkDeviceSize device_min_uniform_alignment, VmaAllocator p_allocator, VkDevice p_device) noexcept;
  233. void Shutdown() noexcept;
  234. bool Alloc_GeneralBuffer(VkDeviceSize size, void** p_data, VkDescriptorBufferInfo* p_out, VmaVirtualAllocation* p_alloc) noexcept;
  235. bool Alloc_VertexBuffer(uint32_t number_of_elements, uint32_t stride_in_bytes, void** p_data, VkDescriptorBufferInfo* p_out,
  236. VmaVirtualAllocation* p_alloc) noexcept;
  237. bool Alloc_IndexBuffer(uint32_t number_of_elements, uint32_t stride_in_bytes, void** p_data, VkDescriptorBufferInfo* p_out,
  238. VmaVirtualAllocation* p_alloc) noexcept;
  239. void SetDescriptorSet(uint32_t binding_index, uint32_t size, VkDescriptorType descriptor_type, VkDescriptorSet p_set) noexcept;
  240. void SetDescriptorSet(uint32_t binding_index, VkDescriptorBufferInfo* p_info, VkDescriptorType descriptor_type,
  241. VkDescriptorSet p_set) noexcept;
  242. void SetDescriptorSet(uint32_t binding_index, VkSampler p_sampler, VkImageLayout layout, VkImageView p_view, VkDescriptorType descriptor_type,
  243. VkDescriptorSet p_set) noexcept;
  244. void Free_GeometryHandle(geometry_handle_t* p_valid_geometry_handle) noexcept;
  245. void Free_GeometryHandle_ShaderDataOnly(geometry_handle_t* p_valid_geometry_handle) noexcept;
  246. private:
  247. VkDeviceSize m_memory_total_size;
  248. VkDeviceSize m_device_min_uniform_alignment;
  249. char* m_p_data;
  250. VkBuffer m_p_buffer;
  251. VmaAllocation m_p_buffer_alloc;
  252. VkDevice m_p_device;
  253. VmaAllocator m_p_vk_allocator;
  254. VmaVirtualBlock m_p_block;
  255. };
  256. // If we need additional command buffers, we can add them to this list and retrieve them from the ring.
  257. enum class CommandBufferName { Primary, Count };
  258. // The command buffer ring stores a unique set of named command buffers for each bufferd frame.
  259. // Explanation of how to use Vulkan efficiently: https://vkguide.dev/docs/chapter-4/double_buffering/
  260. class CommandBufferRing {
  261. public:
  262. static constexpr uint32_t kNumFramesToBuffer = kSwapchainBackBufferCount;
  263. static constexpr uint32_t kNumCommandBuffersPerFrame = static_cast<uint32_t>(CommandBufferName::Count);
  264. CommandBufferRing();
  265. void Initialize(VkDevice p_device, uint32_t queue_index_graphics) noexcept;
  266. void Shutdown();
  267. void OnBeginFrame();
  268. VkCommandBuffer GetCommandBufferForActiveFrame(CommandBufferName named_command_buffer);
  269. private:
  270. struct CommandBuffersPerFrame {
  271. Rml::Array<VkCommandPool, kNumCommandBuffersPerFrame> m_command_pools;
  272. Rml::Array<VkCommandBuffer, kNumCommandBuffersPerFrame> m_command_buffers;
  273. };
  274. VkDevice m_p_device;
  275. uint32_t m_frame_index;
  276. CommandBuffersPerFrame* m_p_current_frame;
  277. Rml::Array<CommandBuffersPerFrame, kNumFramesToBuffer> m_frames;
  278. };
  279. class DescriptorPoolManager {
  280. public:
  281. DescriptorPoolManager() : m_allocated_descriptor_count{}, m_p_descriptor_pool{} {}
  282. ~DescriptorPoolManager()
  283. {
  284. RMLUI_VK_ASSERTMSG(m_allocated_descriptor_count <= 0, "something is wrong. You didn't free some VkDescriptorSet");
  285. }
  286. void Initialize(VkDevice p_device, uint32_t count_uniform_buffer, uint32_t count_image_sampler, uint32_t count_sampler,
  287. uint32_t count_storage_buffer) noexcept
  288. {
  289. RMLUI_VK_ASSERTMSG(p_device, "you can't pass an invalid VkDevice here");
  290. Rml::Array<VkDescriptorPoolSize, 5> sizes;
  291. sizes[0] = {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, count_uniform_buffer};
  292. sizes[1] = {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, count_uniform_buffer};
  293. sizes[2] = {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count_image_sampler};
  294. sizes[3] = {VK_DESCRIPTOR_TYPE_SAMPLER, count_sampler};
  295. sizes[4] = {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, count_storage_buffer};
  296. VkDescriptorPoolCreateInfo info = {};
  297. info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
  298. info.pNext = nullptr;
  299. info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
  300. info.maxSets = 1000;
  301. info.poolSizeCount = static_cast<uint32_t>(sizes.size());
  302. info.pPoolSizes = sizes.data();
  303. auto status = vkCreateDescriptorPool(p_device, &info, nullptr, &m_p_descriptor_pool);
  304. RMLUI_VK_ASSERTMSG(status == VkResult::VK_SUCCESS, "failed to vkCreateDescriptorPool");
  305. }
  306. void Shutdown(VkDevice p_device)
  307. {
  308. RMLUI_VK_ASSERTMSG(p_device, "you can't pass an invalid VkDevice here");
  309. vkDestroyDescriptorPool(p_device, m_p_descriptor_pool, nullptr);
  310. }
  311. uint32_t Get_AllocatedDescriptorCount() const noexcept { return m_allocated_descriptor_count; }
  312. bool Alloc_Descriptor(VkDevice p_device, VkDescriptorSetLayout* p_layouts, VkDescriptorSet* p_sets,
  313. uint32_t descriptor_count_for_creation = 1) noexcept
  314. {
  315. RMLUI_VK_ASSERTMSG(p_layouts, "you have to pass a valid and initialized VkDescriptorSetLayout (probably you must create it)");
  316. RMLUI_VK_ASSERTMSG(p_device, "you must pass a valid VkDevice here");
  317. VkDescriptorSetAllocateInfo info = {};
  318. info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  319. info.pNext = nullptr;
  320. info.descriptorPool = m_p_descriptor_pool;
  321. info.descriptorSetCount = descriptor_count_for_creation;
  322. info.pSetLayouts = p_layouts;
  323. auto status = vkAllocateDescriptorSets(p_device, &info, p_sets);
  324. RMLUI_VK_ASSERTMSG(status == VkResult::VK_SUCCESS, "failed to vkAllocateDescriptorSets");
  325. m_allocated_descriptor_count += descriptor_count_for_creation;
  326. return status == VkResult::VK_SUCCESS;
  327. }
  328. void Free_Descriptors(VkDevice p_device, VkDescriptorSet* p_sets, uint32_t descriptor_count = 1) noexcept
  329. {
  330. RMLUI_VK_ASSERTMSG(p_device, "you must pass a valid VkDevice here");
  331. if (p_sets)
  332. {
  333. m_allocated_descriptor_count -= descriptor_count;
  334. vkFreeDescriptorSets(p_device, m_p_descriptor_pool, descriptor_count, p_sets);
  335. }
  336. }
  337. private:
  338. int m_allocated_descriptor_count;
  339. VkDescriptorPool m_p_descriptor_pool;
  340. };
  341. struct PhysicalDeviceWrapper {
  342. VkPhysicalDevice m_p_physical_device;
  343. VkPhysicalDeviceProperties m_physical_device_properties;
  344. };
  345. using PhysicalDeviceWrapperList = Rml::Vector<PhysicalDeviceWrapper>;
  346. using LayerPropertiesList = Rml::Vector<VkLayerProperties>;
  347. using ExtensionPropertiesList = Rml::Vector<VkExtensionProperties>;
  348. private:
  349. Rml::TextureHandle CreateTexture(Rml::Span<const Rml::byte> source, Rml::Vector2i dimensions, const Rml::String& name);
  350. void Initialize_Instance(Rml::Vector<const char*> required_extensions) noexcept;
  351. void Initialize_Device() noexcept;
  352. void Initialize_PhysicalDevice(VkPhysicalDeviceProperties& out_physical_device_properties) noexcept;
  353. void Initialize_Swapchain(VkExtent2D window_extent) noexcept;
  354. void Initialize_Surface(CreateSurfaceCallback create_surface_callback) noexcept;
  355. void Initialize_QueueIndecies() noexcept;
  356. void Initialize_Queues() noexcept;
  357. void Initialize_SyncPrimitives() noexcept;
  358. void Initialize_Resources(const VkPhysicalDeviceProperties& physical_device_properties) noexcept;
  359. void Initialize_Allocator() noexcept;
  360. void Destroy_Instance() noexcept;
  361. void Destroy_Device() noexcept;
  362. void Destroy_Swapchain() noexcept;
  363. void Destroy_Surface() noexcept;
  364. void Destroy_SyncPrimitives() noexcept;
  365. void Destroy_Resources() noexcept;
  366. void Destroy_Allocator() noexcept;
  367. void QueryInstanceLayers(LayerPropertiesList& result) noexcept;
  368. void QueryInstanceExtensions(ExtensionPropertiesList& result, const LayerPropertiesList& instance_layer_properties) noexcept;
  369. bool AddLayerToInstance(Rml::Vector<const char*>& result, const LayerPropertiesList& instance_layer_properties,
  370. const char* p_instance_layer_name) noexcept;
  371. bool AddExtensionToInstance(Rml::Vector<const char*>& result, const ExtensionPropertiesList& instance_extension_properties,
  372. const char* p_instance_extension_name) noexcept;
  373. void CreatePropertiesFor_Instance(Rml::Vector<const char*>& instance_layer_names, Rml::Vector<const char*>& instance_extension_names) noexcept;
  374. bool IsLayerPresent(const LayerPropertiesList& properties, const char* p_layer_name) noexcept;
  375. bool IsExtensionPresent(const ExtensionPropertiesList& properties, const char* p_extension_name) noexcept;
  376. bool AddExtensionToDevice(Rml::Vector<const char*>& result, const ExtensionPropertiesList& device_extension_properties,
  377. const char* p_device_extension_name) noexcept;
  378. void CreatePropertiesFor_Device(ExtensionPropertiesList& result) noexcept;
  379. void CreateReportDebugCallback() noexcept;
  380. void Destroy_ReportDebugCallback() noexcept;
  381. uint32_t GetUserAPIVersion() const noexcept;
  382. uint32_t GetRequiredVersionAndValidateMachine() noexcept;
  383. void CollectPhysicalDevices(PhysicalDeviceWrapperList& out_physical_devices) noexcept;
  384. const PhysicalDeviceWrapper* ChoosePhysicalDevice(const PhysicalDeviceWrapperList& physical_devices, VkPhysicalDeviceType device_type) noexcept;
  385. VkSurfaceFormatKHR ChooseSwapchainFormat() noexcept;
  386. VkSurfaceTransformFlagBitsKHR CreatePretransformSwapchain() noexcept;
  387. VkCompositeAlphaFlagBitsKHR ChooseSwapchainCompositeAlpha() noexcept;
  388. int Choose_SwapchainImageCount(uint32_t user_swapchain_count_for_creation = kSwapchainBackBufferCount, bool if_failed_choose_min = true) noexcept;
  389. VkPresentModeKHR GetPresentMode(VkPresentModeKHR type = VkPresentModeKHR::VK_PRESENT_MODE_FIFO_KHR) noexcept;
  390. VkSurfaceCapabilitiesKHR GetSurfaceCapabilities() noexcept;
  391. VkExtent2D GetValidSurfaceExtent() noexcept;
  392. void CreateShaders() noexcept;
  393. void CreateDescriptorSetLayout() noexcept;
  394. void CreatePipelineLayout() noexcept;
  395. void CreateDescriptorSets() noexcept;
  396. void CreateSamplers() noexcept;
  397. void Create_Pipelines() noexcept;
  398. void CreateRenderPass() noexcept;
  399. void CreateSwapchainFrameBuffers(const VkExtent2D& real_render_image_size) noexcept;
  400. // This method is called in Views, so don't call it manually
  401. void CreateSwapchainImages() noexcept;
  402. void CreateSwapchainImageViews() noexcept;
  403. void Create_DepthStencilImage() noexcept;
  404. void Create_DepthStencilImageViews() noexcept;
  405. void CreateResourcesDependentOnSize(const VkExtent2D& real_render_image_size) noexcept;
  406. buffer_data_t CreateResource_StagingBuffer(VkDeviceSize size, VkBufferUsageFlags flags) noexcept;
  407. void DestroyResource_StagingBuffer(const buffer_data_t& data) noexcept;
  408. void Destroy_Textures() noexcept;
  409. void Destroy_Geometries() noexcept;
  410. void Destroy_Texture(const texture_data_t& p_texture) noexcept;
  411. void DestroyResourcesDependentOnSize() noexcept;
  412. void DestroySwapchainImageViews() noexcept;
  413. void DestroySwapchainFrameBuffers() noexcept;
  414. void DestroyRenderPass() noexcept;
  415. void Destroy_Pipelines() noexcept;
  416. void DestroyDescriptorSets() noexcept;
  417. void DestroyPipelineLayout() noexcept;
  418. void DestroySamplers() noexcept;
  419. void Wait() noexcept;
  420. void Update_PendingForDeletion_Textures_By_Frames() noexcept;
  421. void Update_PendingForDeletion_Geometries() noexcept;
  422. void Submit() noexcept;
  423. void Present() noexcept;
  424. VkFormat Get_SupportedDepthFormat();
  425. private:
  426. bool m_is_transform_enabled;
  427. bool m_is_apply_to_regular_geometry_stencil;
  428. bool m_is_use_scissor_specified;
  429. bool m_is_use_stencil_pipeline;
  430. int m_width;
  431. int m_height;
  432. uint32_t m_queue_index_present;
  433. uint32_t m_queue_index_graphics;
  434. uint32_t m_queue_index_compute;
  435. uint32_t m_semaphore_index;
  436. uint32_t m_semaphore_index_previous;
  437. uint32_t m_image_index;
  438. VkInstance m_p_instance;
  439. VkDevice m_p_device;
  440. VkPhysicalDevice m_p_physical_device;
  441. VkSurfaceKHR m_p_surface;
  442. VkSwapchainKHR m_p_swapchain;
  443. VmaAllocator m_p_allocator;
  444. // @ obtained from command list see PrepareRenderBuffer method
  445. VkCommandBuffer m_p_current_command_buffer;
  446. VkDescriptorSetLayout m_p_descriptor_set_layout_vertex_transform;
  447. VkDescriptorSetLayout m_p_descriptor_set_layout_texture;
  448. VkPipelineLayout m_p_pipeline_layout;
  449. VkPipeline m_p_pipeline_with_textures;
  450. VkPipeline m_p_pipeline_without_textures;
  451. VkPipeline m_p_pipeline_stencil_for_region_where_geometry_will_be_drawn;
  452. VkPipeline m_p_pipeline_stencil_for_regular_geometry_that_applied_to_region_with_textures;
  453. VkPipeline m_p_pipeline_stencil_for_regular_geometry_that_applied_to_region_without_textures;
  454. VkDescriptorSet m_p_descriptor_set;
  455. VkRenderPass m_p_render_pass;
  456. VkSampler m_p_sampler_linear;
  457. VkRect2D m_scissor;
  458. // @ means it captures the window size full width and full height, offset equals both x and y to 0
  459. VkRect2D m_scissor_original;
  460. VkViewport m_viewport;
  461. VkQueue m_p_queue_present;
  462. VkQueue m_p_queue_graphics;
  463. VkQueue m_p_queue_compute;
  464. #ifdef RMLUI_VK_DEBUG
  465. VkDebugUtilsMessengerEXT m_debug_messenger;
  466. #endif
  467. VkSurfaceFormatKHR m_swapchain_format;
  468. shader_vertex_user_data_t m_user_data_for_vertex_shader;
  469. texture_data_t m_texture_depthstencil;
  470. Rml::Matrix4f m_projection;
  471. Rml::Vector<VkFence> m_executed_fences;
  472. Rml::Vector<VkSemaphore> m_semaphores_image_available;
  473. Rml::Vector<VkSemaphore> m_semaphores_finished_render;
  474. Rml::Vector<VkFramebuffer> m_swapchain_frame_buffers;
  475. Rml::Vector<VkImage> m_swapchain_images;
  476. Rml::Vector<VkImageView> m_swapchain_image_views;
  477. Rml::Vector<VkShaderModule> m_shaders;
  478. Rml::Array<Rml::Vector<texture_data_t*>, kSwapchainBackBufferCount> m_pending_for_deletion_textures_by_frames;
  479. // vma handles that thing, so there's no need for frame splitting
  480. Rml::Vector<geometry_handle_t*> m_pending_for_deletion_geometries;
  481. CommandBufferRing m_command_buffer_ring;
  482. MemoryPool m_memory_pool;
  483. UploadResourceManager m_upload_manager;
  484. DescriptorPoolManager m_manager_descriptors;
  485. };
  486. #endif