imgui_impl_vulkan.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. // dear imgui: Renderer for Vulkan
  2. // This needs to be used along with a Platform Binding (e.g. GLFW, SDL, Win32, custom..)
  3. // Missing features:
  4. // [ ] Renderer: User texture binding. Changes of ImTextureID aren't supported by this binding! See https://github.com/ocornut/imgui/pull/914
  5. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  6. // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
  7. // https://github.com/ocornut/imgui
  8. // The aim of imgui_impl_vulkan.h/.cpp is to be usable in your engine without any modification.
  9. // IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/
  10. // Important note to the reader who wish to integrate imgui_impl_vulkan.cpp/.h in their own engine/app.
  11. // - Common ImGui_ImplVulkan_XXX functions and structures are used to interface with imgui_impl_vulkan.cpp/.h.
  12. // You will use those if you want to use this rendering back-end in your engine/app.
  13. // - Helper ImGui_ImplVulkanH_XXX functions and structures are only used by this example (main.cpp) and by
  14. // the back-end itself (imgui_impl_vulkan.cpp), but should PROBABLY NOT be used by your own engine/app code.
  15. // Read comments in imgui_impl_vulkan.h.
  16. // CHANGELOG
  17. // (minor and older changes stripped away, please see git history for details)
  18. // 2019-XX-XX: *BREAKING CHANGE*: Vulkan: Added ImageCount/MinImageCount fields in ImGui_ImplVulkan_InitInfo, required for initialization (was previously a hard #define IMGUI_VK_QUEUED_FRAMES 2). Added ImGui_ImplVulkan_SetMinImageCount().
  19. // 2019-XX-XX: Vulkan: Added VkInstance argument to ImGui_ImplVulkanH_CreateWindow() optional helper.
  20. // 2019-04-04: Vulkan: Avoid passing negative coordinates to vkCmdSetScissor, which debug validation layers do not like.
  21. // 2019-04-01: Vulkan: Support for 32-bit index buffer (#define ImDrawIdx unsigned int).
  22. // 2019-02-16: Vulkan: Viewport and clipping rectangles correctly using draw_data->FramebufferScale to allow retina display.
  23. // 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
  24. // 2018-08-25: Vulkan: Fixed mishandled VkSurfaceCapabilitiesKHR::maxImageCount=0 case.
  25. // 2018-06-22: Inverted the parameters to ImGui_ImplVulkan_RenderDrawData() to be consistent with other bindings.
  26. // 2018-06-08: Misc: Extracted imgui_impl_vulkan.cpp/.h away from the old combined GLFW+Vulkan example.
  27. // 2018-06-08: Vulkan: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
  28. // 2018-03-03: Vulkan: Various refactor, created a couple of ImGui_ImplVulkanH_XXX helper that the example can use and that viewport support will use.
  29. // 2018-03-01: Vulkan: Renamed ImGui_ImplVulkan_Init_Info to ImGui_ImplVulkan_InitInfo and fields to match more closely Vulkan terminology.
  30. // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback, ImGui_ImplVulkan_Render() calls ImGui_ImplVulkan_RenderDrawData() itself.
  31. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  32. // 2017-05-15: Vulkan: Fix scissor offset being negative. Fix new Vulkan validation warnings. Set required depth member for buffer image copy.
  33. // 2016-11-13: Vulkan: Fix validation layer warnings and errors and redeclare gl_PerVertex.
  34. // 2016-10-18: Vulkan: Add location decorators & change to use structs as in/out in glsl, update embedded spv (produced with glslangValidator -x). Null the released resources.
  35. // 2016-08-27: Vulkan: Fix Vulkan example for use when a depth buffer is active.
  36. #include "imgui.h"
  37. #include "imgui_impl_vulkan.h"
  38. #include <stdio.h>
  39. // Reusable buffers used for rendering 1 current in-flight frame, for ImGui_ImplVulkan_RenderDrawData()
  40. // [Please zero-clear before use!]
  41. struct ImGui_ImplVulkanH_FrameRenderBuffers
  42. {
  43. VkDeviceMemory VertexBufferMemory;
  44. VkDeviceMemory IndexBufferMemory;
  45. VkDeviceSize VertexBufferSize;
  46. VkDeviceSize IndexBufferSize;
  47. VkBuffer VertexBuffer;
  48. VkBuffer IndexBuffer;
  49. };
  50. // Each viewport will hold 1 ImGui_ImplVulkanH_WindowRenderBuffers
  51. // [Please zero-clear before use!]
  52. struct ImGui_ImplVulkanH_WindowRenderBuffers
  53. {
  54. uint32_t Index;
  55. uint32_t Count;
  56. ImGui_ImplVulkanH_FrameRenderBuffers* FrameRenderBuffers;
  57. };
  58. // Vulkan data
  59. static ImGui_ImplVulkan_InitInfo g_VulkanInitInfo = {};
  60. static VkRenderPass g_RenderPass = VK_NULL_HANDLE;
  61. static VkDeviceSize g_BufferMemoryAlignment = 256;
  62. static VkPipelineCreateFlags g_PipelineCreateFlags = 0x00;
  63. static VkDescriptorSetLayout g_DescriptorSetLayout = VK_NULL_HANDLE;
  64. static VkPipelineLayout g_PipelineLayout = VK_NULL_HANDLE;
  65. static VkDescriptorSet g_DescriptorSet = VK_NULL_HANDLE;
  66. static VkPipeline g_Pipeline = VK_NULL_HANDLE;
  67. // Font data
  68. static VkSampler g_FontSampler = VK_NULL_HANDLE;
  69. static VkDeviceMemory g_FontMemory = VK_NULL_HANDLE;
  70. static VkImage g_FontImage = VK_NULL_HANDLE;
  71. static VkImageView g_FontView = VK_NULL_HANDLE;
  72. static VkDeviceMemory g_UploadBufferMemory = VK_NULL_HANDLE;
  73. static VkBuffer g_UploadBuffer = VK_NULL_HANDLE;
  74. // Render buffers
  75. static ImGui_ImplVulkanH_WindowRenderBuffers g_MainWindowRenderBuffers;
  76. // Forward Declarations
  77. bool ImGui_ImplVulkan_CreateDeviceObjects();
  78. void ImGui_ImplVulkan_DestroyDeviceObjects();
  79. void ImGui_ImplVulkanH_DestroyFrame(VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator);
  80. void ImGui_ImplVulkanH_DestroyFrameSemaphores(VkDevice device, ImGui_ImplVulkanH_FrameSemaphores* fsd, const VkAllocationCallbacks* allocator);
  81. void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkDevice device, ImGui_ImplVulkanH_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator);
  82. void ImGui_ImplVulkanH_DestroyWindowRenderBuffers(VkDevice device, ImGui_ImplVulkanH_WindowRenderBuffers* buffers, const VkAllocationCallbacks* allocator);
  83. void ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count);
  84. void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator);
  85. //-----------------------------------------------------------------------------
  86. // SHADERS
  87. //-----------------------------------------------------------------------------
  88. // glsl_shader.vert, compiled with:
  89. // # glslangValidator -V -x -o glsl_shader.vert.u32 glsl_shader.vert
  90. /*
  91. #version 450 core
  92. layout(location = 0) in vec2 aPos;
  93. layout(location = 1) in vec2 aUV;
  94. layout(location = 2) in vec4 aColor;
  95. layout(push_constant) uniform uPushConstant { vec2 uScale; vec2 uTranslate; } pc;
  96. out gl_PerVertex { vec4 gl_Position; };
  97. layout(location = 0) out struct { vec4 Color; vec2 UV; } Out;
  98. void main()
  99. {
  100. Out.Color = aColor;
  101. Out.UV = aUV;
  102. gl_Position = vec4(aPos * pc.uScale + pc.uTranslate, 0, 1);
  103. }
  104. */
  105. static uint32_t __glsl_shader_vert_spv[] =
  106. {
  107. 0x07230203,0x00010000,0x00080001,0x0000002e,0x00000000,0x00020011,0x00000001,0x0006000b,
  108. 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
  109. 0x000a000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000b,0x0000000f,0x00000015,
  110. 0x0000001b,0x0000001c,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
  111. 0x00000000,0x00030005,0x00000009,0x00000000,0x00050006,0x00000009,0x00000000,0x6f6c6f43,
  112. 0x00000072,0x00040006,0x00000009,0x00000001,0x00005655,0x00030005,0x0000000b,0x0074754f,
  113. 0x00040005,0x0000000f,0x6c6f4361,0x0000726f,0x00030005,0x00000015,0x00565561,0x00060005,
  114. 0x00000019,0x505f6c67,0x65567265,0x78657472,0x00000000,0x00060006,0x00000019,0x00000000,
  115. 0x505f6c67,0x7469736f,0x006e6f69,0x00030005,0x0000001b,0x00000000,0x00040005,0x0000001c,
  116. 0x736f5061,0x00000000,0x00060005,0x0000001e,0x73755075,0x6e6f4368,0x6e617473,0x00000074,
  117. 0x00050006,0x0000001e,0x00000000,0x61635375,0x0000656c,0x00060006,0x0000001e,0x00000001,
  118. 0x61725475,0x616c736e,0x00006574,0x00030005,0x00000020,0x00006370,0x00040047,0x0000000b,
  119. 0x0000001e,0x00000000,0x00040047,0x0000000f,0x0000001e,0x00000002,0x00040047,0x00000015,
  120. 0x0000001e,0x00000001,0x00050048,0x00000019,0x00000000,0x0000000b,0x00000000,0x00030047,
  121. 0x00000019,0x00000002,0x00040047,0x0000001c,0x0000001e,0x00000000,0x00050048,0x0000001e,
  122. 0x00000000,0x00000023,0x00000000,0x00050048,0x0000001e,0x00000001,0x00000023,0x00000008,
  123. 0x00030047,0x0000001e,0x00000002,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,
  124. 0x00030016,0x00000006,0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040017,
  125. 0x00000008,0x00000006,0x00000002,0x0004001e,0x00000009,0x00000007,0x00000008,0x00040020,
  126. 0x0000000a,0x00000003,0x00000009,0x0004003b,0x0000000a,0x0000000b,0x00000003,0x00040015,
  127. 0x0000000c,0x00000020,0x00000001,0x0004002b,0x0000000c,0x0000000d,0x00000000,0x00040020,
  128. 0x0000000e,0x00000001,0x00000007,0x0004003b,0x0000000e,0x0000000f,0x00000001,0x00040020,
  129. 0x00000011,0x00000003,0x00000007,0x0004002b,0x0000000c,0x00000013,0x00000001,0x00040020,
  130. 0x00000014,0x00000001,0x00000008,0x0004003b,0x00000014,0x00000015,0x00000001,0x00040020,
  131. 0x00000017,0x00000003,0x00000008,0x0003001e,0x00000019,0x00000007,0x00040020,0x0000001a,
  132. 0x00000003,0x00000019,0x0004003b,0x0000001a,0x0000001b,0x00000003,0x0004003b,0x00000014,
  133. 0x0000001c,0x00000001,0x0004001e,0x0000001e,0x00000008,0x00000008,0x00040020,0x0000001f,
  134. 0x00000009,0x0000001e,0x0004003b,0x0000001f,0x00000020,0x00000009,0x00040020,0x00000021,
  135. 0x00000009,0x00000008,0x0004002b,0x00000006,0x00000028,0x00000000,0x0004002b,0x00000006,
  136. 0x00000029,0x3f800000,0x00050036,0x00000002,0x00000004,0x00000000,0x00000003,0x000200f8,
  137. 0x00000005,0x0004003d,0x00000007,0x00000010,0x0000000f,0x00050041,0x00000011,0x00000012,
  138. 0x0000000b,0x0000000d,0x0003003e,0x00000012,0x00000010,0x0004003d,0x00000008,0x00000016,
  139. 0x00000015,0x00050041,0x00000017,0x00000018,0x0000000b,0x00000013,0x0003003e,0x00000018,
  140. 0x00000016,0x0004003d,0x00000008,0x0000001d,0x0000001c,0x00050041,0x00000021,0x00000022,
  141. 0x00000020,0x0000000d,0x0004003d,0x00000008,0x00000023,0x00000022,0x00050085,0x00000008,
  142. 0x00000024,0x0000001d,0x00000023,0x00050041,0x00000021,0x00000025,0x00000020,0x00000013,
  143. 0x0004003d,0x00000008,0x00000026,0x00000025,0x00050081,0x00000008,0x00000027,0x00000024,
  144. 0x00000026,0x00050051,0x00000006,0x0000002a,0x00000027,0x00000000,0x00050051,0x00000006,
  145. 0x0000002b,0x00000027,0x00000001,0x00070050,0x00000007,0x0000002c,0x0000002a,0x0000002b,
  146. 0x00000028,0x00000029,0x00050041,0x00000011,0x0000002d,0x0000001b,0x0000000d,0x0003003e,
  147. 0x0000002d,0x0000002c,0x000100fd,0x00010038
  148. };
  149. // glsl_shader.frag, compiled with:
  150. // # glslangValidator -V -x -o glsl_shader.frag.u32 glsl_shader.frag
  151. /*
  152. #version 450 core
  153. layout(location = 0) out vec4 fColor;
  154. layout(set=0, binding=0) uniform sampler2D sTexture;
  155. layout(location = 0) in struct { vec4 Color; vec2 UV; } In;
  156. void main()
  157. {
  158. fColor = In.Color * texture(sTexture, In.UV.st);
  159. }
  160. */
  161. static uint32_t __glsl_shader_frag_spv[] =
  162. {
  163. 0x07230203,0x00010000,0x00080001,0x0000001e,0x00000000,0x00020011,0x00000001,0x0006000b,
  164. 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
  165. 0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000d,0x00030010,
  166. 0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00040005,0x00000004,0x6e69616d,
  167. 0x00000000,0x00040005,0x00000009,0x6c6f4366,0x0000726f,0x00030005,0x0000000b,0x00000000,
  168. 0x00050006,0x0000000b,0x00000000,0x6f6c6f43,0x00000072,0x00040006,0x0000000b,0x00000001,
  169. 0x00005655,0x00030005,0x0000000d,0x00006e49,0x00050005,0x00000016,0x78655473,0x65727574,
  170. 0x00000000,0x00040047,0x00000009,0x0000001e,0x00000000,0x00040047,0x0000000d,0x0000001e,
  171. 0x00000000,0x00040047,0x00000016,0x00000022,0x00000000,0x00040047,0x00000016,0x00000021,
  172. 0x00000000,0x00020013,0x00000002,0x00030021,0x00000003,0x00000002,0x00030016,0x00000006,
  173. 0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,0x00040020,0x00000008,0x00000003,
  174. 0x00000007,0x0004003b,0x00000008,0x00000009,0x00000003,0x00040017,0x0000000a,0x00000006,
  175. 0x00000002,0x0004001e,0x0000000b,0x00000007,0x0000000a,0x00040020,0x0000000c,0x00000001,
  176. 0x0000000b,0x0004003b,0x0000000c,0x0000000d,0x00000001,0x00040015,0x0000000e,0x00000020,
  177. 0x00000001,0x0004002b,0x0000000e,0x0000000f,0x00000000,0x00040020,0x00000010,0x00000001,
  178. 0x00000007,0x00090019,0x00000013,0x00000006,0x00000001,0x00000000,0x00000000,0x00000000,
  179. 0x00000001,0x00000000,0x0003001b,0x00000014,0x00000013,0x00040020,0x00000015,0x00000000,
  180. 0x00000014,0x0004003b,0x00000015,0x00000016,0x00000000,0x0004002b,0x0000000e,0x00000018,
  181. 0x00000001,0x00040020,0x00000019,0x00000001,0x0000000a,0x00050036,0x00000002,0x00000004,
  182. 0x00000000,0x00000003,0x000200f8,0x00000005,0x00050041,0x00000010,0x00000011,0x0000000d,
  183. 0x0000000f,0x0004003d,0x00000007,0x00000012,0x00000011,0x0004003d,0x00000014,0x00000017,
  184. 0x00000016,0x00050041,0x00000019,0x0000001a,0x0000000d,0x00000018,0x0004003d,0x0000000a,
  185. 0x0000001b,0x0000001a,0x00050057,0x00000007,0x0000001c,0x00000017,0x0000001b,0x00050085,
  186. 0x00000007,0x0000001d,0x00000012,0x0000001c,0x0003003e,0x00000009,0x0000001d,0x000100fd,
  187. 0x00010038
  188. };
  189. //-----------------------------------------------------------------------------
  190. // FUNCTIONS
  191. //-----------------------------------------------------------------------------
  192. static uint32_t ImGui_ImplVulkan_MemoryType(VkMemoryPropertyFlags properties, uint32_t type_bits)
  193. {
  194. ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo;
  195. VkPhysicalDeviceMemoryProperties prop;
  196. vkGetPhysicalDeviceMemoryProperties(v->PhysicalDevice, &prop);
  197. for (uint32_t i = 0; i < prop.memoryTypeCount; i++)
  198. if ((prop.memoryTypes[i].propertyFlags & properties) == properties && type_bits & (1<<i))
  199. return i;
  200. return 0xFFFFFFFF; // Unable to find memoryType
  201. }
  202. static void check_vk_result(VkResult err)
  203. {
  204. ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo;
  205. if (v->CheckVkResultFn)
  206. v->CheckVkResultFn(err);
  207. }
  208. static void CreateOrResizeBuffer(VkBuffer& buffer, VkDeviceMemory& buffer_memory, VkDeviceSize& p_buffer_size, size_t new_size, VkBufferUsageFlagBits usage)
  209. {
  210. ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo;
  211. VkResult err;
  212. if (buffer != VK_NULL_HANDLE)
  213. vkDestroyBuffer(v->Device, buffer, v->Allocator);
  214. if (buffer_memory != VK_NULL_HANDLE)
  215. vkFreeMemory(v->Device, buffer_memory, v->Allocator);
  216. VkDeviceSize vertex_buffer_size_aligned = ((new_size - 1) / g_BufferMemoryAlignment + 1) * g_BufferMemoryAlignment;
  217. VkBufferCreateInfo buffer_info = {};
  218. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  219. buffer_info.size = vertex_buffer_size_aligned;
  220. buffer_info.usage = usage;
  221. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  222. err = vkCreateBuffer(v->Device, &buffer_info, v->Allocator, &buffer);
  223. check_vk_result(err);
  224. VkMemoryRequirements req;
  225. vkGetBufferMemoryRequirements(v->Device, buffer, &req);
  226. g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
  227. VkMemoryAllocateInfo alloc_info = {};
  228. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  229. alloc_info.allocationSize = req.size;
  230. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
  231. err = vkAllocateMemory(v->Device, &alloc_info, v->Allocator, &buffer_memory);
  232. check_vk_result(err);
  233. err = vkBindBufferMemory(v->Device, buffer, buffer_memory, 0);
  234. check_vk_result(err);
  235. p_buffer_size = new_size;
  236. }
  237. static void ImGui_ImplVulkan_SetupRenderState(ImDrawData* draw_data, VkCommandBuffer command_buffer, ImGui_ImplVulkanH_FrameRenderBuffers* rb, int fb_width, int fb_height)
  238. {
  239. // Bind pipeline and descriptor sets:
  240. {
  241. vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_Pipeline);
  242. VkDescriptorSet desc_set[1] = { g_DescriptorSet };
  243. vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_PipelineLayout, 0, 1, desc_set, 0, NULL);
  244. }
  245. // Bind Vertex And Index Buffer:
  246. {
  247. VkBuffer vertex_buffers[1] = { rb->VertexBuffer };
  248. VkDeviceSize vertex_offset[1] = { 0 };
  249. vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffers, vertex_offset);
  250. vkCmdBindIndexBuffer(command_buffer, rb->IndexBuffer, 0, sizeof(ImDrawIdx) == 2 ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32);
  251. }
  252. // Setup viewport:
  253. {
  254. VkViewport viewport;
  255. viewport.x = 0;
  256. viewport.y = 0;
  257. viewport.width = (float)fb_width;
  258. viewport.height = (float)fb_height;
  259. viewport.minDepth = 0.0f;
  260. viewport.maxDepth = 1.0f;
  261. vkCmdSetViewport(command_buffer, 0, 1, &viewport);
  262. }
  263. // Setup scale and translation:
  264. // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayMin is typically (0,0) for single viewport apps.
  265. {
  266. float scale[2];
  267. scale[0] = 2.0f / draw_data->DisplaySize.x;
  268. scale[1] = 2.0f / draw_data->DisplaySize.y;
  269. float translate[2];
  270. translate[0] = -1.0f - draw_data->DisplayPos.x * scale[0];
  271. translate[1] = -1.0f - draw_data->DisplayPos.y * scale[1];
  272. vkCmdPushConstants(command_buffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 0, sizeof(float) * 2, scale);
  273. vkCmdPushConstants(command_buffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 2, sizeof(float) * 2, translate);
  274. }
  275. }
  276. // Render function
  277. // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
  278. void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer)
  279. {
  280. // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
  281. int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
  282. int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
  283. if (fb_width <= 0 || fb_height <= 0 || draw_data->TotalVtxCount == 0)
  284. return;
  285. ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo;
  286. // Allocate array to store enough vertex/index buffers
  287. ImGui_ImplVulkanH_WindowRenderBuffers* wrb = &g_MainWindowRenderBuffers;
  288. if (wrb->FrameRenderBuffers == NULL)
  289. {
  290. wrb->Index = 0;
  291. wrb->Count = v->ImageCount;
  292. wrb->FrameRenderBuffers = (ImGui_ImplVulkanH_FrameRenderBuffers*)IM_ALLOC(sizeof(ImGui_ImplVulkanH_FrameRenderBuffers) * wrb->Count);
  293. memset(wrb->FrameRenderBuffers, 0, sizeof(ImGui_ImplVulkanH_FrameRenderBuffers) * wrb->Count);
  294. }
  295. IM_ASSERT(wrb->Count == v->ImageCount);
  296. wrb->Index = (wrb->Index + 1) % wrb->Count;
  297. ImGui_ImplVulkanH_FrameRenderBuffers* rb = &wrb->FrameRenderBuffers[wrb->Index];
  298. VkResult err;
  299. // Create or resize the vertex/index buffers
  300. size_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert);
  301. size_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
  302. if (rb->VertexBuffer == VK_NULL_HANDLE || rb->VertexBufferSize < vertex_size)
  303. CreateOrResizeBuffer(rb->VertexBuffer, rb->VertexBufferMemory, rb->VertexBufferSize, vertex_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
  304. if (rb->IndexBuffer == VK_NULL_HANDLE || rb->IndexBufferSize < index_size)
  305. CreateOrResizeBuffer(rb->IndexBuffer, rb->IndexBufferMemory, rb->IndexBufferSize, index_size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT);
  306. // Upload vertex/index data into a single contiguous GPU buffer
  307. {
  308. ImDrawVert* vtx_dst = NULL;
  309. ImDrawIdx* idx_dst = NULL;
  310. err = vkMapMemory(v->Device, rb->VertexBufferMemory, 0, vertex_size, 0, (void**)(&vtx_dst));
  311. check_vk_result(err);
  312. err = vkMapMemory(v->Device, rb->IndexBufferMemory, 0, index_size, 0, (void**)(&idx_dst));
  313. check_vk_result(err);
  314. for (int n = 0; n < draw_data->CmdListsCount; n++)
  315. {
  316. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  317. memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
  318. memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
  319. vtx_dst += cmd_list->VtxBuffer.Size;
  320. idx_dst += cmd_list->IdxBuffer.Size;
  321. }
  322. VkMappedMemoryRange range[2] = {};
  323. range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  324. range[0].memory = rb->VertexBufferMemory;
  325. range[0].size = VK_WHOLE_SIZE;
  326. range[1].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  327. range[1].memory = rb->IndexBufferMemory;
  328. range[1].size = VK_WHOLE_SIZE;
  329. err = vkFlushMappedMemoryRanges(v->Device, 2, range);
  330. check_vk_result(err);
  331. vkUnmapMemory(v->Device, rb->VertexBufferMemory);
  332. vkUnmapMemory(v->Device, rb->IndexBufferMemory);
  333. }
  334. // Setup desired Vulkan state
  335. ImGui_ImplVulkan_SetupRenderState(draw_data, command_buffer, rb, fb_width, fb_height);
  336. // Will project scissor/clipping rectangles into framebuffer space
  337. ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
  338. ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
  339. // Render command lists
  340. int vtx_offset = 0;
  341. int idx_offset = 0;
  342. for (int n = 0; n < draw_data->CmdListsCount; n++)
  343. {
  344. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  345. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
  346. {
  347. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  348. if (pcmd->UserCallback != NULL)
  349. {
  350. // User callback, registered via ImDrawList::AddCallback()
  351. // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
  352. if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
  353. ImGui_ImplVulkan_SetupRenderState(draw_data, command_buffer, rb, fb_width, fb_height);
  354. else
  355. pcmd->UserCallback(cmd_list, pcmd);
  356. }
  357. else
  358. {
  359. // Project scissor/clipping rectangles into framebuffer space
  360. ImVec4 clip_rect;
  361. clip_rect.x = (pcmd->ClipRect.x - clip_off.x) * clip_scale.x;
  362. clip_rect.y = (pcmd->ClipRect.y - clip_off.y) * clip_scale.y;
  363. clip_rect.z = (pcmd->ClipRect.z - clip_off.x) * clip_scale.x;
  364. clip_rect.w = (pcmd->ClipRect.w - clip_off.y) * clip_scale.y;
  365. if (clip_rect.x < fb_width && clip_rect.y < fb_height && clip_rect.z >= 0.0f && clip_rect.w >= 0.0f)
  366. {
  367. // Negative offsets are illegal for vkCmdSetScissor
  368. if (clip_rect.x < 0.0f)
  369. clip_rect.x = 0.0f;
  370. if (clip_rect.y < 0.0f)
  371. clip_rect.y = 0.0f;
  372. // Apply scissor/clipping rectangle
  373. VkRect2D scissor;
  374. scissor.offset.x = (int32_t)(clip_rect.x);
  375. scissor.offset.y = (int32_t)(clip_rect.y);
  376. scissor.extent.width = (uint32_t)(clip_rect.z - clip_rect.x);
  377. scissor.extent.height = (uint32_t)(clip_rect.w - clip_rect.y);
  378. vkCmdSetScissor(command_buffer, 0, 1, &scissor);
  379. // Draw
  380. vkCmdDrawIndexed(command_buffer, pcmd->ElemCount, 1, idx_offset, vtx_offset, 0);
  381. }
  382. }
  383. idx_offset += pcmd->ElemCount;
  384. }
  385. vtx_offset += cmd_list->VtxBuffer.Size;
  386. }
  387. }
  388. bool ImGui_ImplVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
  389. {
  390. ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo;
  391. ImGuiIO& io = ImGui::GetIO();
  392. unsigned char* pixels;
  393. int width, height;
  394. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  395. size_t upload_size = width*height*4*sizeof(char);
  396. VkResult err;
  397. // Create the Image:
  398. {
  399. VkImageCreateInfo info = {};
  400. info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
  401. info.imageType = VK_IMAGE_TYPE_2D;
  402. info.format = VK_FORMAT_R8G8B8A8_UNORM;
  403. info.extent.width = width;
  404. info.extent.height = height;
  405. info.extent.depth = 1;
  406. info.mipLevels = 1;
  407. info.arrayLayers = 1;
  408. info.samples = VK_SAMPLE_COUNT_1_BIT;
  409. info.tiling = VK_IMAGE_TILING_OPTIMAL;
  410. info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
  411. info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  412. info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  413. err = vkCreateImage(v->Device, &info, v->Allocator, &g_FontImage);
  414. check_vk_result(err);
  415. VkMemoryRequirements req;
  416. vkGetImageMemoryRequirements(v->Device, g_FontImage, &req);
  417. VkMemoryAllocateInfo alloc_info = {};
  418. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  419. alloc_info.allocationSize = req.size;
  420. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, req.memoryTypeBits);
  421. err = vkAllocateMemory(v->Device, &alloc_info, v->Allocator, &g_FontMemory);
  422. check_vk_result(err);
  423. err = vkBindImageMemory(v->Device, g_FontImage, g_FontMemory, 0);
  424. check_vk_result(err);
  425. }
  426. // Create the Image View:
  427. {
  428. VkImageViewCreateInfo info = {};
  429. info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  430. info.image = g_FontImage;
  431. info.viewType = VK_IMAGE_VIEW_TYPE_2D;
  432. info.format = VK_FORMAT_R8G8B8A8_UNORM;
  433. info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  434. info.subresourceRange.levelCount = 1;
  435. info.subresourceRange.layerCount = 1;
  436. err = vkCreateImageView(v->Device, &info, v->Allocator, &g_FontView);
  437. check_vk_result(err);
  438. }
  439. // Update the Descriptor Set:
  440. {
  441. VkDescriptorImageInfo desc_image[1] = {};
  442. desc_image[0].sampler = g_FontSampler;
  443. desc_image[0].imageView = g_FontView;
  444. desc_image[0].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  445. VkWriteDescriptorSet write_desc[1] = {};
  446. write_desc[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  447. write_desc[0].dstSet = g_DescriptorSet;
  448. write_desc[0].descriptorCount = 1;
  449. write_desc[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  450. write_desc[0].pImageInfo = desc_image;
  451. vkUpdateDescriptorSets(v->Device, 1, write_desc, 0, NULL);
  452. }
  453. // Create the Upload Buffer:
  454. {
  455. VkBufferCreateInfo buffer_info = {};
  456. buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  457. buffer_info.size = upload_size;
  458. buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
  459. buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  460. err = vkCreateBuffer(v->Device, &buffer_info, v->Allocator, &g_UploadBuffer);
  461. check_vk_result(err);
  462. VkMemoryRequirements req;
  463. vkGetBufferMemoryRequirements(v->Device, g_UploadBuffer, &req);
  464. g_BufferMemoryAlignment = (g_BufferMemoryAlignment > req.alignment) ? g_BufferMemoryAlignment : req.alignment;
  465. VkMemoryAllocateInfo alloc_info = {};
  466. alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
  467. alloc_info.allocationSize = req.size;
  468. alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
  469. err = vkAllocateMemory(v->Device, &alloc_info, v->Allocator, &g_UploadBufferMemory);
  470. check_vk_result(err);
  471. err = vkBindBufferMemory(v->Device, g_UploadBuffer, g_UploadBufferMemory, 0);
  472. check_vk_result(err);
  473. }
  474. // Upload to Buffer:
  475. {
  476. char* map = NULL;
  477. err = vkMapMemory(v->Device, g_UploadBufferMemory, 0, upload_size, 0, (void**)(&map));
  478. check_vk_result(err);
  479. memcpy(map, pixels, upload_size);
  480. VkMappedMemoryRange range[1] = {};
  481. range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
  482. range[0].memory = g_UploadBufferMemory;
  483. range[0].size = upload_size;
  484. err = vkFlushMappedMemoryRanges(v->Device, 1, range);
  485. check_vk_result(err);
  486. vkUnmapMemory(v->Device, g_UploadBufferMemory);
  487. }
  488. // Copy to Image:
  489. {
  490. VkImageMemoryBarrier copy_barrier[1] = {};
  491. copy_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  492. copy_barrier[0].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  493. copy_barrier[0].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  494. copy_barrier[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  495. copy_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  496. copy_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  497. copy_barrier[0].image = g_FontImage;
  498. copy_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  499. copy_barrier[0].subresourceRange.levelCount = 1;
  500. copy_barrier[0].subresourceRange.layerCount = 1;
  501. vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, copy_barrier);
  502. VkBufferImageCopy region = {};
  503. region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  504. region.imageSubresource.layerCount = 1;
  505. region.imageExtent.width = width;
  506. region.imageExtent.height = height;
  507. region.imageExtent.depth = 1;
  508. vkCmdCopyBufferToImage(command_buffer, g_UploadBuffer, g_FontImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
  509. VkImageMemoryBarrier use_barrier[1] = {};
  510. use_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  511. use_barrier[0].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
  512. use_barrier[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
  513. use_barrier[0].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
  514. use_barrier[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  515. use_barrier[0].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  516. use_barrier[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  517. use_barrier[0].image = g_FontImage;
  518. use_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  519. use_barrier[0].subresourceRange.levelCount = 1;
  520. use_barrier[0].subresourceRange.layerCount = 1;
  521. vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, use_barrier);
  522. }
  523. // Store our identifier
  524. io.Fonts->TexID = (ImTextureID)(intptr_t)g_FontImage;
  525. return true;
  526. }
  527. bool ImGui_ImplVulkan_CreateDeviceObjects()
  528. {
  529. ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo;
  530. VkResult err;
  531. VkShaderModule vert_module;
  532. VkShaderModule frag_module;
  533. // Create The Shader Modules:
  534. {
  535. VkShaderModuleCreateInfo vert_info = {};
  536. vert_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  537. vert_info.codeSize = sizeof(__glsl_shader_vert_spv);
  538. vert_info.pCode = (uint32_t*)__glsl_shader_vert_spv;
  539. err = vkCreateShaderModule(v->Device, &vert_info, v->Allocator, &vert_module);
  540. check_vk_result(err);
  541. VkShaderModuleCreateInfo frag_info = {};
  542. frag_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
  543. frag_info.codeSize = sizeof(__glsl_shader_frag_spv);
  544. frag_info.pCode = (uint32_t*)__glsl_shader_frag_spv;
  545. err = vkCreateShaderModule(v->Device, &frag_info, v->Allocator, &frag_module);
  546. check_vk_result(err);
  547. }
  548. if (!g_FontSampler)
  549. {
  550. VkSamplerCreateInfo info = {};
  551. info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
  552. info.magFilter = VK_FILTER_LINEAR;
  553. info.minFilter = VK_FILTER_LINEAR;
  554. info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
  555. info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  556. info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  557. info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
  558. info.minLod = -1000;
  559. info.maxLod = 1000;
  560. info.maxAnisotropy = 1.0f;
  561. err = vkCreateSampler(v->Device, &info, v->Allocator, &g_FontSampler);
  562. check_vk_result(err);
  563. }
  564. if (!g_DescriptorSetLayout)
  565. {
  566. VkSampler sampler[1] = {g_FontSampler};
  567. VkDescriptorSetLayoutBinding binding[1] = {};
  568. binding[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
  569. binding[0].descriptorCount = 1;
  570. binding[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
  571. binding[0].pImmutableSamplers = sampler;
  572. VkDescriptorSetLayoutCreateInfo info = {};
  573. info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
  574. info.bindingCount = 1;
  575. info.pBindings = binding;
  576. err = vkCreateDescriptorSetLayout(v->Device, &info, v->Allocator, &g_DescriptorSetLayout);
  577. check_vk_result(err);
  578. }
  579. // Create Descriptor Set:
  580. {
  581. VkDescriptorSetAllocateInfo alloc_info = {};
  582. alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  583. alloc_info.descriptorPool = v->DescriptorPool;
  584. alloc_info.descriptorSetCount = 1;
  585. alloc_info.pSetLayouts = &g_DescriptorSetLayout;
  586. err = vkAllocateDescriptorSets(v->Device, &alloc_info, &g_DescriptorSet);
  587. check_vk_result(err);
  588. }
  589. if (!g_PipelineLayout)
  590. {
  591. // Constants: we are using 'vec2 offset' and 'vec2 scale' instead of a full 3d projection matrix
  592. VkPushConstantRange push_constants[1] = {};
  593. push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
  594. push_constants[0].offset = sizeof(float) * 0;
  595. push_constants[0].size = sizeof(float) * 4;
  596. VkDescriptorSetLayout set_layout[1] = { g_DescriptorSetLayout };
  597. VkPipelineLayoutCreateInfo layout_info = {};
  598. layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  599. layout_info.setLayoutCount = 1;
  600. layout_info.pSetLayouts = set_layout;
  601. layout_info.pushConstantRangeCount = 1;
  602. layout_info.pPushConstantRanges = push_constants;
  603. err = vkCreatePipelineLayout(v->Device, &layout_info, v->Allocator, &g_PipelineLayout);
  604. check_vk_result(err);
  605. }
  606. VkPipelineShaderStageCreateInfo stage[2] = {};
  607. stage[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  608. stage[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
  609. stage[0].module = vert_module;
  610. stage[0].pName = "main";
  611. stage[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
  612. stage[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
  613. stage[1].module = frag_module;
  614. stage[1].pName = "main";
  615. VkVertexInputBindingDescription binding_desc[1] = {};
  616. binding_desc[0].stride = sizeof(ImDrawVert);
  617. binding_desc[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
  618. VkVertexInputAttributeDescription attribute_desc[3] = {};
  619. attribute_desc[0].location = 0;
  620. attribute_desc[0].binding = binding_desc[0].binding;
  621. attribute_desc[0].format = VK_FORMAT_R32G32_SFLOAT;
  622. attribute_desc[0].offset = IM_OFFSETOF(ImDrawVert, pos);
  623. attribute_desc[1].location = 1;
  624. attribute_desc[1].binding = binding_desc[0].binding;
  625. attribute_desc[1].format = VK_FORMAT_R32G32_SFLOAT;
  626. attribute_desc[1].offset = IM_OFFSETOF(ImDrawVert, uv);
  627. attribute_desc[2].location = 2;
  628. attribute_desc[2].binding = binding_desc[0].binding;
  629. attribute_desc[2].format = VK_FORMAT_R8G8B8A8_UNORM;
  630. attribute_desc[2].offset = IM_OFFSETOF(ImDrawVert, col);
  631. VkPipelineVertexInputStateCreateInfo vertex_info = {};
  632. vertex_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  633. vertex_info.vertexBindingDescriptionCount = 1;
  634. vertex_info.pVertexBindingDescriptions = binding_desc;
  635. vertex_info.vertexAttributeDescriptionCount = 3;
  636. vertex_info.pVertexAttributeDescriptions = attribute_desc;
  637. VkPipelineInputAssemblyStateCreateInfo ia_info = {};
  638. ia_info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  639. ia_info.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
  640. VkPipelineViewportStateCreateInfo viewport_info = {};
  641. viewport_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  642. viewport_info.viewportCount = 1;
  643. viewport_info.scissorCount = 1;
  644. VkPipelineRasterizationStateCreateInfo raster_info = {};
  645. raster_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  646. raster_info.polygonMode = VK_POLYGON_MODE_FILL;
  647. raster_info.cullMode = VK_CULL_MODE_NONE;
  648. raster_info.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
  649. raster_info.lineWidth = 1.0f;
  650. VkPipelineMultisampleStateCreateInfo ms_info = {};
  651. ms_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  652. ms_info.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
  653. VkPipelineColorBlendAttachmentState color_attachment[1] = {};
  654. color_attachment[0].blendEnable = VK_TRUE;
  655. color_attachment[0].srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
  656. color_attachment[0].dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  657. color_attachment[0].colorBlendOp = VK_BLEND_OP_ADD;
  658. color_attachment[0].srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
  659. color_attachment[0].dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
  660. color_attachment[0].alphaBlendOp = VK_BLEND_OP_ADD;
  661. color_attachment[0].colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
  662. VkPipelineDepthStencilStateCreateInfo depth_info = {};
  663. depth_info.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  664. VkPipelineColorBlendStateCreateInfo blend_info = {};
  665. blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  666. blend_info.attachmentCount = 1;
  667. blend_info.pAttachments = color_attachment;
  668. VkDynamicState dynamic_states[2] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
  669. VkPipelineDynamicStateCreateInfo dynamic_state = {};
  670. dynamic_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
  671. dynamic_state.dynamicStateCount = (uint32_t)IM_ARRAYSIZE(dynamic_states);
  672. dynamic_state.pDynamicStates = dynamic_states;
  673. VkGraphicsPipelineCreateInfo info = {};
  674. info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  675. info.flags = g_PipelineCreateFlags;
  676. info.stageCount = 2;
  677. info.pStages = stage;
  678. info.pVertexInputState = &vertex_info;
  679. info.pInputAssemblyState = &ia_info;
  680. info.pViewportState = &viewport_info;
  681. info.pRasterizationState = &raster_info;
  682. info.pMultisampleState = &ms_info;
  683. info.pDepthStencilState = &depth_info;
  684. info.pColorBlendState = &blend_info;
  685. info.pDynamicState = &dynamic_state;
  686. info.layout = g_PipelineLayout;
  687. info.renderPass = g_RenderPass;
  688. err = vkCreateGraphicsPipelines(v->Device, v->PipelineCache, 1, &info, v->Allocator, &g_Pipeline);
  689. check_vk_result(err);
  690. vkDestroyShaderModule(v->Device, vert_module, v->Allocator);
  691. vkDestroyShaderModule(v->Device, frag_module, v->Allocator);
  692. return true;
  693. }
  694. void ImGui_ImplVulkan_DestroyFontUploadObjects()
  695. {
  696. ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo;
  697. if (g_UploadBuffer)
  698. {
  699. vkDestroyBuffer(v->Device, g_UploadBuffer, v->Allocator);
  700. g_UploadBuffer = VK_NULL_HANDLE;
  701. }
  702. if (g_UploadBufferMemory)
  703. {
  704. vkFreeMemory(v->Device, g_UploadBufferMemory, v->Allocator);
  705. g_UploadBufferMemory = VK_NULL_HANDLE;
  706. }
  707. }
  708. void ImGui_ImplVulkan_DestroyDeviceObjects()
  709. {
  710. ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo;
  711. ImGui_ImplVulkanH_DestroyWindowRenderBuffers(v->Device, &g_MainWindowRenderBuffers, v->Allocator);
  712. ImGui_ImplVulkan_DestroyFontUploadObjects();
  713. if (g_FontView) { vkDestroyImageView(v->Device, g_FontView, v->Allocator); g_FontView = VK_NULL_HANDLE; }
  714. if (g_FontImage) { vkDestroyImage(v->Device, g_FontImage, v->Allocator); g_FontImage = VK_NULL_HANDLE; }
  715. if (g_FontMemory) { vkFreeMemory(v->Device, g_FontMemory, v->Allocator); g_FontMemory = VK_NULL_HANDLE; }
  716. if (g_FontSampler) { vkDestroySampler(v->Device, g_FontSampler, v->Allocator); g_FontSampler = VK_NULL_HANDLE; }
  717. if (g_DescriptorSetLayout) { vkDestroyDescriptorSetLayout(v->Device, g_DescriptorSetLayout, v->Allocator); g_DescriptorSetLayout = VK_NULL_HANDLE; }
  718. if (g_PipelineLayout) { vkDestroyPipelineLayout(v->Device, g_PipelineLayout, v->Allocator); g_PipelineLayout = VK_NULL_HANDLE; }
  719. if (g_Pipeline) { vkDestroyPipeline(v->Device, g_Pipeline, v->Allocator); g_Pipeline = VK_NULL_HANDLE; }
  720. }
  721. bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info, VkRenderPass render_pass)
  722. {
  723. ImGuiIO& io = ImGui::GetIO();
  724. io.BackendRendererName = "imgui_impl_vulkan";
  725. IM_ASSERT(info->Instance != VK_NULL_HANDLE);
  726. IM_ASSERT(info->PhysicalDevice != VK_NULL_HANDLE);
  727. IM_ASSERT(info->Device != VK_NULL_HANDLE);
  728. IM_ASSERT(info->Queue != VK_NULL_HANDLE);
  729. IM_ASSERT(info->DescriptorPool != VK_NULL_HANDLE);
  730. IM_ASSERT(info->MinImageCount >= 2);
  731. IM_ASSERT(info->ImageCount >= info->MinImageCount);
  732. IM_ASSERT(render_pass != VK_NULL_HANDLE);
  733. g_VulkanInitInfo = *info;
  734. g_RenderPass = render_pass;
  735. ImGui_ImplVulkan_CreateDeviceObjects();
  736. return true;
  737. }
  738. void ImGui_ImplVulkan_Shutdown()
  739. {
  740. ImGui_ImplVulkan_DestroyDeviceObjects();
  741. }
  742. void ImGui_ImplVulkan_NewFrame()
  743. {
  744. }
  745. void ImGui_ImplVulkan_SetMinImageCount(uint32_t min_image_count)
  746. {
  747. IM_ASSERT(min_image_count >= 2);
  748. if (g_VulkanInitInfo.MinImageCount == min_image_count)
  749. return;
  750. ImGui_ImplVulkan_InitInfo* v = &g_VulkanInitInfo;
  751. VkResult err = vkDeviceWaitIdle(v->Device);
  752. check_vk_result(err);
  753. ImGui_ImplVulkanH_DestroyWindowRenderBuffers(v->Device, &g_MainWindowRenderBuffers, v->Allocator);
  754. g_VulkanInitInfo.MinImageCount = min_image_count;
  755. }
  756. //-------------------------------------------------------------------------
  757. // Internal / Miscellaneous Vulkan Helpers
  758. // (Used by example's main.cpp. Used by multi-viewport features. PROBABLY NOT used by your own app.)
  759. //-------------------------------------------------------------------------
  760. // You probably do NOT need to use or care about those functions.
  761. // Those functions only exist because:
  762. // 1) they facilitate the readability and maintenance of the multiple main.cpp examples files.
  763. // 2) the upcoming multi-viewport feature will need them internally.
  764. // Generally we avoid exposing any kind of superfluous high-level helpers in the bindings,
  765. // but it is too much code to duplicate everywhere so we exceptionally expose them.
  766. //
  767. // Your engine/app will likely _already_ have code to setup all that stuff (swap chain, render pass, frame buffers, etc.).
  768. // You may read this code to learn about Vulkan, but it is recommended you use you own custom tailored code to do equivalent work.
  769. // (The ImGui_ImplVulkanH_XXX functions do not interact with any of the state used by the regular ImGui_ImplVulkan_XXX functions)
  770. //-------------------------------------------------------------------------
  771. VkSurfaceFormatKHR ImGui_ImplVulkanH_SelectSurfaceFormat(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkFormat* request_formats, int request_formats_count, VkColorSpaceKHR request_color_space)
  772. {
  773. IM_ASSERT(request_formats != NULL);
  774. IM_ASSERT(request_formats_count > 0);
  775. // Per Spec Format and View Format are expected to be the same unless VK_IMAGE_CREATE_MUTABLE_BIT was set at image creation
  776. // Assuming that the default behavior is without setting this bit, there is no need for separate Swapchain image and image view format
  777. // Additionally several new color spaces were introduced with Vulkan Spec v1.0.40,
  778. // hence we must make sure that a format with the mostly available color space, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, is found and used.
  779. uint32_t avail_count;
  780. vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &avail_count, NULL);
  781. ImVector<VkSurfaceFormatKHR> avail_format;
  782. avail_format.resize((int)avail_count);
  783. vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &avail_count, avail_format.Data);
  784. // First check if only one format, VK_FORMAT_UNDEFINED, is available, which would imply that any format is available
  785. if (avail_count == 1)
  786. {
  787. if (avail_format[0].format == VK_FORMAT_UNDEFINED)
  788. {
  789. VkSurfaceFormatKHR ret;
  790. ret.format = request_formats[0];
  791. ret.colorSpace = request_color_space;
  792. return ret;
  793. }
  794. else
  795. {
  796. // No point in searching another format
  797. return avail_format[0];
  798. }
  799. }
  800. else
  801. {
  802. // Request several formats, the first found will be used
  803. for (int request_i = 0; request_i < request_formats_count; request_i++)
  804. for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
  805. if (avail_format[avail_i].format == request_formats[request_i] && avail_format[avail_i].colorSpace == request_color_space)
  806. return avail_format[avail_i];
  807. // If none of the requested image formats could be found, use the first available
  808. return avail_format[0];
  809. }
  810. }
  811. VkPresentModeKHR ImGui_ImplVulkanH_SelectPresentMode(VkPhysicalDevice physical_device, VkSurfaceKHR surface, const VkPresentModeKHR* request_modes, int request_modes_count)
  812. {
  813. IM_ASSERT(request_modes != NULL);
  814. IM_ASSERT(request_modes_count > 0);
  815. // Request a certain mode and confirm that it is available. If not use VK_PRESENT_MODE_FIFO_KHR which is mandatory
  816. uint32_t avail_count = 0;
  817. vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &avail_count, NULL);
  818. ImVector<VkPresentModeKHR> avail_modes;
  819. avail_modes.resize((int)avail_count);
  820. vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &avail_count, avail_modes.Data);
  821. //for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
  822. // printf("[vulkan] avail_modes[%d] = %d\n", avail_i, avail_modes[avail_i]);
  823. for (int request_i = 0; request_i < request_modes_count; request_i++)
  824. for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
  825. if (request_modes[request_i] == avail_modes[avail_i])
  826. return request_modes[request_i];
  827. return VK_PRESENT_MODE_FIFO_KHR; // Always available
  828. }
  829. void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator)
  830. {
  831. IM_ASSERT(physical_device != VK_NULL_HANDLE && device != VK_NULL_HANDLE);
  832. (void)physical_device;
  833. (void)allocator;
  834. // Create Command Buffers
  835. VkResult err;
  836. for (uint32_t i = 0; i < wd->ImageCount; i++)
  837. {
  838. ImGui_ImplVulkanH_Frame* fd = &wd->Frames[i];
  839. ImGui_ImplVulkanH_FrameSemaphores* fsd = &wd->FrameSemaphores[i];
  840. {
  841. VkCommandPoolCreateInfo info = {};
  842. info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  843. info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
  844. info.queueFamilyIndex = queue_family;
  845. err = vkCreateCommandPool(device, &info, allocator, &fd->CommandPool);
  846. check_vk_result(err);
  847. }
  848. {
  849. VkCommandBufferAllocateInfo info = {};
  850. info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  851. info.commandPool = fd->CommandPool;
  852. info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
  853. info.commandBufferCount = 1;
  854. err = vkAllocateCommandBuffers(device, &info, &fd->CommandBuffer);
  855. check_vk_result(err);
  856. }
  857. {
  858. VkFenceCreateInfo info = {};
  859. info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
  860. info.flags = VK_FENCE_CREATE_SIGNALED_BIT;
  861. err = vkCreateFence(device, &info, allocator, &fd->Fence);
  862. check_vk_result(err);
  863. }
  864. {
  865. VkSemaphoreCreateInfo info = {};
  866. info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
  867. err = vkCreateSemaphore(device, &info, allocator, &fsd->ImageAcquiredSemaphore);
  868. check_vk_result(err);
  869. err = vkCreateSemaphore(device, &info, allocator, &fsd->RenderCompleteSemaphore);
  870. check_vk_result(err);
  871. }
  872. }
  873. }
  874. int ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(VkPresentModeKHR present_mode)
  875. {
  876. if (present_mode == VK_PRESENT_MODE_MAILBOX_KHR)
  877. return 3;
  878. if (present_mode == VK_PRESENT_MODE_FIFO_KHR || present_mode == VK_PRESENT_MODE_FIFO_RELAXED_KHR)
  879. return 2;
  880. if (present_mode == VK_PRESENT_MODE_IMMEDIATE_KHR)
  881. return 1;
  882. IM_ASSERT(0);
  883. return 1;
  884. }
  885. // Also destroy old swap chain and in-flight frames data, if any.
  886. void ImGui_ImplVulkanH_CreateWindowSwapChain(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator, int w, int h, uint32_t min_image_count)
  887. {
  888. VkResult err;
  889. VkSwapchainKHR old_swapchain = wd->Swapchain;
  890. err = vkDeviceWaitIdle(device);
  891. check_vk_result(err);
  892. // We don't use ImGui_ImplVulkanH_DestroyWindow() because we want to preserve the old swapchain to create the new one.
  893. // Destroy old Framebuffer
  894. for (uint32_t i = 0; i < wd->ImageCount; i++)
  895. {
  896. ImGui_ImplVulkanH_DestroyFrame(device, &wd->Frames[i], allocator);
  897. ImGui_ImplVulkanH_DestroyFrameSemaphores(device, &wd->FrameSemaphores[i], allocator);
  898. }
  899. IM_FREE(wd->Frames);
  900. IM_FREE(wd->FrameSemaphores);
  901. wd->Frames = NULL;
  902. wd->FrameSemaphores = NULL;
  903. wd->ImageCount = 0;
  904. if (wd->RenderPass)
  905. vkDestroyRenderPass(device, wd->RenderPass, allocator);
  906. // If min image count was not specified, request different count of images dependent on selected present mode
  907. if (min_image_count == 0)
  908. min_image_count = ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(wd->PresentMode);
  909. // Create Swapchain
  910. {
  911. VkSwapchainCreateInfoKHR info = {};
  912. info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
  913. info.surface = wd->Surface;
  914. info.minImageCount = min_image_count;
  915. info.imageFormat = wd->SurfaceFormat.format;
  916. info.imageColorSpace = wd->SurfaceFormat.colorSpace;
  917. info.imageArrayLayers = 1;
  918. info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
  919. info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; // Assume that graphics family == present family
  920. info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
  921. info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
  922. info.presentMode = wd->PresentMode;
  923. info.clipped = VK_TRUE;
  924. info.oldSwapchain = old_swapchain;
  925. VkSurfaceCapabilitiesKHR cap;
  926. err = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, wd->Surface, &cap);
  927. check_vk_result(err);
  928. if (info.minImageCount < cap.minImageCount)
  929. info.minImageCount = cap.minImageCount;
  930. else if (cap.maxImageCount != 0 && info.minImageCount > cap.maxImageCount)
  931. info.minImageCount = cap.maxImageCount;
  932. if (cap.currentExtent.width == 0xffffffff)
  933. {
  934. info.imageExtent.width = wd->Width = w;
  935. info.imageExtent.height = wd->Height = h;
  936. }
  937. else
  938. {
  939. info.imageExtent.width = wd->Width = cap.currentExtent.width;
  940. info.imageExtent.height = wd->Height = cap.currentExtent.height;
  941. }
  942. err = vkCreateSwapchainKHR(device, &info, allocator, &wd->Swapchain);
  943. check_vk_result(err);
  944. err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->ImageCount, NULL);
  945. check_vk_result(err);
  946. VkImage backbuffers[16] = {};
  947. IM_ASSERT(wd->ImageCount >= min_image_count);
  948. IM_ASSERT(wd->ImageCount < IM_ARRAYSIZE(backbuffers));
  949. err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->ImageCount, backbuffers);
  950. check_vk_result(err);
  951. IM_ASSERT(wd->Frames == NULL);
  952. wd->Frames = (ImGui_ImplVulkanH_Frame*)IM_ALLOC(sizeof(ImGui_ImplVulkanH_Frame) * wd->ImageCount);
  953. wd->FrameSemaphores = (ImGui_ImplVulkanH_FrameSemaphores*)IM_ALLOC(sizeof(ImGui_ImplVulkanH_FrameSemaphores) * wd->ImageCount);
  954. memset(wd->Frames, 0, sizeof(wd->Frames[0]) * wd->ImageCount);
  955. memset(wd->FrameSemaphores, 0, sizeof(wd->FrameSemaphores[0]) * wd->ImageCount);
  956. for (uint32_t i = 0; i < wd->ImageCount; i++)
  957. wd->Frames[i].Backbuffer = backbuffers[i];
  958. }
  959. if (old_swapchain)
  960. vkDestroySwapchainKHR(device, old_swapchain, allocator);
  961. // Create the Render Pass
  962. {
  963. VkAttachmentDescription attachment = {};
  964. attachment.format = wd->SurfaceFormat.format;
  965. attachment.samples = VK_SAMPLE_COUNT_1_BIT;
  966. attachment.loadOp = wd->ClearEnable ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE;
  967. attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
  968. attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
  969. attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
  970. attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  971. attachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
  972. VkAttachmentReference color_attachment = {};
  973. color_attachment.attachment = 0;
  974. color_attachment.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  975. VkSubpassDescription subpass = {};
  976. subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
  977. subpass.colorAttachmentCount = 1;
  978. subpass.pColorAttachments = &color_attachment;
  979. VkSubpassDependency dependency = {};
  980. dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
  981. dependency.dstSubpass = 0;
  982. dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  983. dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  984. dependency.srcAccessMask = 0;
  985. dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
  986. VkRenderPassCreateInfo info = {};
  987. info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
  988. info.attachmentCount = 1;
  989. info.pAttachments = &attachment;
  990. info.subpassCount = 1;
  991. info.pSubpasses = &subpass;
  992. info.dependencyCount = 1;
  993. info.pDependencies = &dependency;
  994. err = vkCreateRenderPass(device, &info, allocator, &wd->RenderPass);
  995. check_vk_result(err);
  996. }
  997. // Create The Image Views
  998. {
  999. VkImageViewCreateInfo info = {};
  1000. info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  1001. info.viewType = VK_IMAGE_VIEW_TYPE_2D;
  1002. info.format = wd->SurfaceFormat.format;
  1003. info.components.r = VK_COMPONENT_SWIZZLE_R;
  1004. info.components.g = VK_COMPONENT_SWIZZLE_G;
  1005. info.components.b = VK_COMPONENT_SWIZZLE_B;
  1006. info.components.a = VK_COMPONENT_SWIZZLE_A;
  1007. VkImageSubresourceRange image_range = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
  1008. info.subresourceRange = image_range;
  1009. for (uint32_t i = 0; i < wd->ImageCount; i++)
  1010. {
  1011. ImGui_ImplVulkanH_Frame* fd = &wd->Frames[i];
  1012. info.image = fd->Backbuffer;
  1013. err = vkCreateImageView(device, &info, allocator, &fd->BackbufferView);
  1014. check_vk_result(err);
  1015. }
  1016. }
  1017. // Create Framebuffer
  1018. {
  1019. VkImageView attachment[1];
  1020. VkFramebufferCreateInfo info = {};
  1021. info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
  1022. info.renderPass = wd->RenderPass;
  1023. info.attachmentCount = 1;
  1024. info.pAttachments = attachment;
  1025. info.width = wd->Width;
  1026. info.height = wd->Height;
  1027. info.layers = 1;
  1028. for (uint32_t i = 0; i < wd->ImageCount; i++)
  1029. {
  1030. ImGui_ImplVulkanH_Frame* fd = &wd->Frames[i];
  1031. attachment[0] = fd->BackbufferView;
  1032. err = vkCreateFramebuffer(device, &info, allocator, &fd->Framebuffer);
  1033. check_vk_result(err);
  1034. }
  1035. }
  1036. }
  1037. void ImGui_ImplVulkanH_CreateWindow(VkInstance instance, VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator, int width, int height, uint32_t min_image_count)
  1038. {
  1039. (void)instance;
  1040. ImGui_ImplVulkanH_CreateWindowSwapChain(physical_device, device, wd, allocator, width, height, min_image_count);
  1041. ImGui_ImplVulkanH_CreateWindowCommandBuffers(physical_device, device, wd, queue_family, allocator);
  1042. }
  1043. void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator)
  1044. {
  1045. vkDeviceWaitIdle(device); // FIXME: We could wait on the Queue if we had the queue in wd-> (otherwise VulkanH functions can't use globals)
  1046. //vkQueueWaitIdle(g_Queue);
  1047. for (uint32_t i = 0; i < wd->ImageCount; i++)
  1048. {
  1049. ImGui_ImplVulkanH_DestroyFrame(device, &wd->Frames[i], allocator);
  1050. ImGui_ImplVulkanH_DestroyFrameSemaphores(device, &wd->FrameSemaphores[i], allocator);
  1051. }
  1052. IM_FREE(wd->Frames);
  1053. IM_FREE(wd->FrameSemaphores);
  1054. wd->Frames = NULL;
  1055. wd->FrameSemaphores = NULL;
  1056. vkDestroyRenderPass(device, wd->RenderPass, allocator);
  1057. vkDestroySwapchainKHR(device, wd->Swapchain, allocator);
  1058. vkDestroySurfaceKHR(instance, wd->Surface, allocator);
  1059. *wd = ImGui_ImplVulkanH_Window();
  1060. }
  1061. void ImGui_ImplVulkanH_DestroyFrame(VkDevice device, ImGui_ImplVulkanH_Frame* fd, const VkAllocationCallbacks* allocator)
  1062. {
  1063. vkDestroyFence(device, fd->Fence, allocator);
  1064. vkFreeCommandBuffers(device, fd->CommandPool, 1, &fd->CommandBuffer);
  1065. vkDestroyCommandPool(device, fd->CommandPool, allocator);
  1066. fd->Fence = VK_NULL_HANDLE;
  1067. fd->CommandBuffer = VK_NULL_HANDLE;
  1068. fd->CommandPool = VK_NULL_HANDLE;
  1069. vkDestroyImageView(device, fd->BackbufferView, allocator);
  1070. vkDestroyFramebuffer(device, fd->Framebuffer, allocator);
  1071. }
  1072. void ImGui_ImplVulkanH_DestroyFrameSemaphores(VkDevice device, ImGui_ImplVulkanH_FrameSemaphores* fsd, const VkAllocationCallbacks* allocator)
  1073. {
  1074. vkDestroySemaphore(device, fsd->ImageAcquiredSemaphore, allocator);
  1075. vkDestroySemaphore(device, fsd->RenderCompleteSemaphore, allocator);
  1076. fsd->ImageAcquiredSemaphore = fsd->RenderCompleteSemaphore = VK_NULL_HANDLE;
  1077. }
  1078. void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkDevice device, ImGui_ImplVulkanH_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator)
  1079. {
  1080. if (buffers->VertexBuffer) { vkDestroyBuffer(device, buffers->VertexBuffer, allocator); buffers->VertexBuffer = VK_NULL_HANDLE; }
  1081. if (buffers->VertexBufferMemory) { vkFreeMemory(device, buffers->VertexBufferMemory, allocator); buffers->VertexBufferMemory = VK_NULL_HANDLE; }
  1082. if (buffers->IndexBuffer) { vkDestroyBuffer(device, buffers->IndexBuffer, allocator); buffers->IndexBuffer = VK_NULL_HANDLE; }
  1083. if (buffers->IndexBufferMemory) { vkFreeMemory(device, buffers->IndexBufferMemory, allocator); buffers->IndexBufferMemory = VK_NULL_HANDLE; }
  1084. buffers->VertexBufferSize = 0;
  1085. buffers->IndexBufferSize = 0;
  1086. }
  1087. void ImGui_ImplVulkanH_DestroyWindowRenderBuffers(VkDevice device, ImGui_ImplVulkanH_WindowRenderBuffers* buffers, const VkAllocationCallbacks* allocator)
  1088. {
  1089. for (uint32_t n = 0; n < buffers->Count; n++)
  1090. ImGui_ImplVulkanH_DestroyFrameRenderBuffers(device, &buffers->FrameRenderBuffers[n], allocator);
  1091. IM_FREE(buffers->FrameRenderBuffers);
  1092. buffers->FrameRenderBuffers = NULL;
  1093. buffers->Index = 0;
  1094. buffers->Count = 0;
  1095. }