Changeset 6493e43 in opengl-game for IMGUI/imgui_impl_vulkan.cpp


Ignore:
Timestamp:
Jan 3, 2021, 6:18:28 PM (4 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl
Children:
6a39266
Parents:
3b7d497
Message:

In sdl-game, add support for using separate graphics and present queues, and move the example ImGui code for (re)creating the swapchain into my own files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • IMGUI/imgui_impl_vulkan.cpp

    r3b7d497 r6493e43  
    103103void ImGui_ImplVulkanH_DestroyFrameRenderBuffers(VkDevice device, ImGui_ImplVulkanH_FrameRenderBuffers* buffers, const VkAllocationCallbacks* allocator);
    104104void ImGui_ImplVulkanH_DestroyWindowRenderBuffers(VkDevice device, ImGui_ImplVulkanH_WindowRenderBuffers* buffers, const VkAllocationCallbacks* allocator);
    105 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);
    106105void ImGui_ImplVulkanH_CreateWindowCommandBuffers(VkPhysicalDevice physical_device, VkDevice device, ImGui_ImplVulkanH_Window* wd, uint32_t queue_family, const VkAllocationCallbacks* allocator);
    107106
     
    895894    IM_ASSERT(info->PhysicalDevice != VK_NULL_HANDLE);
    896895    IM_ASSERT(info->Device != VK_NULL_HANDLE);
    897     IM_ASSERT(info->Queue != VK_NULL_HANDLE);
    898896    IM_ASSERT(info->DescriptorPool != VK_NULL_HANDLE);
    899897    IM_ASSERT(info->MinImageCount >= 2);
     
    10721070}
    10731071
    1074 // Also destroy old swap chain and in-flight frames data, if any.
    1075 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)
    1076 {
    1077     VkResult err;
    1078     VkSwapchainKHR old_swapchain = wd->Swapchain;
    1079     wd->Swapchain = NULL;
    1080     err = vkDeviceWaitIdle(device);
    1081     check_vk_result(err);
    1082 
    1083     // We don't use ImGui_ImplVulkanH_DestroyWindow() because we want to preserve the old swapchain to create the new one.
    1084     // Destroy old Framebuffer
    1085     for (uint32_t i = 0; i < wd->ImageCount; i++)
    1086     {
    1087         ImGui_ImplVulkanH_DestroyFrame(device, &wd->Frames[i], allocator);
    1088         ImGui_ImplVulkanH_DestroyFrameSemaphores(device, &wd->FrameSemaphores[i], allocator);
    1089     }
    1090     IM_FREE(wd->Frames);
    1091     IM_FREE(wd->FrameSemaphores);
    1092     wd->Frames = NULL;
    1093     wd->FrameSemaphores = NULL;
    1094     wd->ImageCount = 0;
    1095     if (wd->RenderPass)
    1096         vkDestroyRenderPass(device, wd->RenderPass, allocator);
    1097     if (wd->Pipeline)
    1098         vkDestroyPipeline(device, wd->Pipeline, allocator);
    1099 
    1100     // If min image count was not specified, request different count of images dependent on selected present mode
    1101     if (min_image_count == 0)
    1102         min_image_count = ImGui_ImplVulkanH_GetMinImageCountFromPresentMode(wd->PresentMode);
    1103 
    1104     // Create Swapchain
    1105     {
    1106         VkSwapchainCreateInfoKHR info = {};
    1107         info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
    1108         info.surface = wd->Surface;
    1109         info.minImageCount = min_image_count;
    1110         info.imageFormat = wd->SurfaceFormat.format;
    1111         info.imageColorSpace = wd->SurfaceFormat.colorSpace;
    1112         info.imageArrayLayers = 1;
    1113         info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
    1114         info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;           // Assume that graphics family == present family
    1115         info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
    1116         info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
    1117         info.presentMode = wd->PresentMode;
    1118         info.clipped = VK_TRUE;
    1119         info.oldSwapchain = old_swapchain;
    1120         VkSurfaceCapabilitiesKHR cap;
    1121         err = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, wd->Surface, &cap);
    1122         check_vk_result(err);
    1123         if (info.minImageCount < cap.minImageCount)
    1124             info.minImageCount = cap.minImageCount;
    1125         else if (cap.maxImageCount != 0 && info.minImageCount > cap.maxImageCount)
    1126             info.minImageCount = cap.maxImageCount;
    1127 
    1128         if (cap.currentExtent.width == 0xffffffff)
    1129         {
    1130             info.imageExtent.width = wd->Width = w;
    1131             info.imageExtent.height = wd->Height = h;
    1132         }
    1133         else
    1134         {
    1135             info.imageExtent.width = wd->Width = cap.currentExtent.width;
    1136             info.imageExtent.height = wd->Height = cap.currentExtent.height;
    1137         }
    1138         err = vkCreateSwapchainKHR(device, &info, allocator, &wd->Swapchain);
    1139         check_vk_result(err);
    1140         err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->ImageCount, NULL);
    1141         check_vk_result(err);
    1142         VkImage backbuffers[16] = {};
    1143         IM_ASSERT(wd->ImageCount >= min_image_count);
    1144         IM_ASSERT(wd->ImageCount < IM_ARRAYSIZE(backbuffers));
    1145         err = vkGetSwapchainImagesKHR(device, wd->Swapchain, &wd->ImageCount, backbuffers);
    1146         check_vk_result(err);
    1147 
    1148         IM_ASSERT(wd->Frames == NULL);
    1149         wd->Frames = (ImGui_ImplVulkanH_Frame*)IM_ALLOC(sizeof(ImGui_ImplVulkanH_Frame) * wd->ImageCount);
    1150         wd->FrameSemaphores = (ImGui_ImplVulkanH_FrameSemaphores*)IM_ALLOC(sizeof(ImGui_ImplVulkanH_FrameSemaphores) * wd->ImageCount);
    1151         memset(wd->Frames, 0, sizeof(wd->Frames[0]) * wd->ImageCount);
    1152         memset(wd->FrameSemaphores, 0, sizeof(wd->FrameSemaphores[0]) * wd->ImageCount);
    1153         for (uint32_t i = 0; i < wd->ImageCount; i++)
    1154             wd->Frames[i].Backbuffer = backbuffers[i];
    1155     }
    1156     if (old_swapchain)
    1157         vkDestroySwapchainKHR(device, old_swapchain, allocator);
    1158 
    1159     // Create the Render Pass
    1160     {
    1161         VkAttachmentDescription attachment = {};
    1162         attachment.format = wd->SurfaceFormat.format;
    1163         attachment.samples = VK_SAMPLE_COUNT_1_BIT;
    1164         attachment.loadOp = wd->ClearEnable ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_DONT_CARE;
    1165         attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
    1166         attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
    1167         attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
    1168         attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
    1169         attachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
    1170         VkAttachmentReference color_attachment = {};
    1171         color_attachment.attachment = 0;
    1172         color_attachment.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
    1173         VkSubpassDescription subpass = {};
    1174         subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
    1175         subpass.colorAttachmentCount = 1;
    1176         subpass.pColorAttachments = &color_attachment;
    1177         VkSubpassDependency dependency = {};
    1178         dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
    1179         dependency.dstSubpass = 0;
    1180         dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
    1181         dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
    1182         dependency.srcAccessMask = 0;
    1183         dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
    1184         VkRenderPassCreateInfo info = {};
    1185         info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
    1186         info.attachmentCount = 1;
    1187         info.pAttachments = &attachment;
    1188         info.subpassCount = 1;
    1189         info.pSubpasses = &subpass;
    1190         info.dependencyCount = 1;
    1191         info.pDependencies = &dependency;
    1192         err = vkCreateRenderPass(device, &info, allocator, &wd->RenderPass);
    1193         check_vk_result(err);
    1194 
    1195         // We do not create a pipeline by default as this is also used by examples' main.cpp,
    1196         // but secondary viewport in multi-viewport mode may want to create one with:
    1197         //ImGui_ImplVulkan_CreatePipeline(device, allocator, VK_NULL_HANDLE, wd->RenderPass, VK_SAMPLE_COUNT_1_BIT, &wd->Pipeline);
    1198     }
    1199 
    1200     // Create The Image Views
    1201     {
    1202         VkImageViewCreateInfo info = {};
    1203         info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
    1204         info.viewType = VK_IMAGE_VIEW_TYPE_2D;
    1205         info.format = wd->SurfaceFormat.format;
    1206         info.components.r = VK_COMPONENT_SWIZZLE_R;
    1207         info.components.g = VK_COMPONENT_SWIZZLE_G;
    1208         info.components.b = VK_COMPONENT_SWIZZLE_B;
    1209         info.components.a = VK_COMPONENT_SWIZZLE_A;
    1210         VkImageSubresourceRange image_range = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
    1211         info.subresourceRange = image_range;
    1212         for (uint32_t i = 0; i < wd->ImageCount; i++)
    1213         {
    1214             ImGui_ImplVulkanH_Frame* fd = &wd->Frames[i];
    1215             info.image = fd->Backbuffer;
    1216             err = vkCreateImageView(device, &info, allocator, &fd->BackbufferView);
    1217             check_vk_result(err);
    1218         }
    1219     }
    1220 
    1221     // Create Framebuffer
    1222     {
    1223         VkImageView attachment[1];
    1224         VkFramebufferCreateInfo info = {};
    1225         info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
    1226         info.renderPass = wd->RenderPass;
    1227         info.attachmentCount = 1;
    1228         info.pAttachments = attachment;
    1229         info.width = wd->Width;
    1230         info.height = wd->Height;
    1231         info.layers = 1;
    1232         for (uint32_t i = 0; i < wd->ImageCount; i++)
    1233         {
    1234             ImGui_ImplVulkanH_Frame* fd = &wd->Frames[i];
    1235             attachment[0] = fd->BackbufferView;
    1236             err = vkCreateFramebuffer(device, &info, allocator, &fd->Framebuffer);
    1237             check_vk_result(err);
    1238         }
    1239     }
    1240 }
    1241 
    1242 // Create or resize window
    1243 void ImGui_ImplVulkanH_CreateOrResizeWindow(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)
    1244 {
    1245     (void)instance;
    1246     ImGui_ImplVulkanH_CreateWindowSwapChain(physical_device, device, wd, allocator, width, height, min_image_count);
    1247     ImGui_ImplVulkanH_CreateWindowCommandBuffers(physical_device, device, wd, queue_family, allocator);
    1248 }
    1249 
    12501072void ImGui_ImplVulkanH_DestroyWindow(VkInstance instance, VkDevice device, ImGui_ImplVulkanH_Window* wd, const VkAllocationCallbacks* allocator)
    12511073{
Note: See TracChangeset for help on using the changeset viewer.