imgui_impl_vulkan.cpp 60 KB

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