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