Skip to content

Commit

Permalink
Vulkan impl: changed some functions signature (use a struct rather th…
Browse files Browse the repository at this point in the history
…an many parameters).
  • Loading branch information
SuperRonan committed Oct 28, 2024
1 parent eabe7b1 commit 148060f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 19 deletions.
64 changes: 46 additions & 18 deletions backends/imgui_impl_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -857,10 +857,21 @@ static void ImGui_ImplVulkan_CreateShaderModules(VkDevice device, const VkAlloca
}
}

static void ImGui_ImplVulkan_CreatePipeline(VkDevice device, const VkAllocationCallbacks* allocator, VkPipelineCache pipelineCache, VkRenderPass renderPass, VkSampleCountFlagBits MSAASamples, VkPipeline* pipeline, uint32_t subpass, const ImGui_ImplVulkan_PipelineRenderingInfo * p_rendering_info = nullptr)
struct ImGui_ImplVulkan_PipelineCreateInfo
{
VkDevice Device = VK_NULL_HANDLE;
const VkAllocationCallbacks* Allocator = nullptr;
VkPipelineCache PipelineCache = VK_NULL_HANDLE;
VkRenderPass RenderPass = VK_NULL_HANDLE;
uint32_t Subpass = 0;
VkSampleCountFlagBits MSAASamples = {};
const ImGui_ImplVulkan_PipelineRenderingInfo* pRenderingInfo = nullptr;
};

static VkPipeline ImGui_ImplVulkan_CreatePipeline(ImGui_ImplVulkan_PipelineCreateInfo const& pci)
{
ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
ImGui_ImplVulkan_CreateShaderModules(device, allocator);
ImGui_ImplVulkan_CreateShaderModules(pci.Device, pci.Allocator);

VkPipelineShaderStageCreateInfo stage[2] = {};
stage[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Expand Down Expand Up @@ -915,7 +926,7 @@ static void ImGui_ImplVulkan_CreatePipeline(VkDevice device, const VkAllocationC

VkPipelineMultisampleStateCreateInfo ms_info = {};
ms_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
ms_info.rasterizationSamples = (MSAASamples != 0) ? MSAASamples : VK_SAMPLE_COUNT_1_BIT;
ms_info.rasterizationSamples = (pci.MSAASamples != 0) ? pci.MSAASamples : VK_SAMPLE_COUNT_1_BIT;

VkPipelineColorBlendAttachmentState color_attachment[1] = {};
color_attachment[0].blendEnable = VK_TRUE;
Expand Down Expand Up @@ -955,21 +966,22 @@ static void ImGui_ImplVulkan_CreatePipeline(VkDevice device, const VkAllocationC
info.pColorBlendState = &blend_info;
info.pDynamicState = &dynamic_state;
info.layout = bd->PipelineLayout;
info.renderPass = renderPass;
info.subpass = subpass;
info.renderPass = pci.RenderPass;
info.subpass = pci.Subpass;

#ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
if (bd->VulkanInitInfo.UseDynamicRendering)
{
info.renderPass = VK_NULL_HANDLE; // Just make sure it's actually nullptr.
IM_ASSERT(p_rendering_info->sType == VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR && "PipelineRenderingCreateInfo::sType must be VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR");
IM_ASSERT(p_rendering_info->pNext == nullptr && "PipelineRenderingCreateInfo::pNext must be NULL");
info.pNext = p_rendering_info;
IM_ASSERT(pci.pRenderingInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR && "PipelineRenderingCreateInfo::sType must be VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR");
IM_ASSERT(pci.pRenderingInfo->pNext == nullptr && "PipelineRenderingCreateInfo::pNext must be NULL");
info.pNext = pci.pRenderingInfo;
}
#endif

VkResult err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &info, allocator, pipeline);
VkPipeline res;
VkResult err = vkCreateGraphicsPipelines(pci.Device, pci.PipelineCache, 1, &info, pci.Allocator, &res);
check_vk_result(err);
return res;
}

bool ImGui_ImplVulkan_CreateDeviceObjects()
Expand Down Expand Up @@ -1031,7 +1043,7 @@ bool ImGui_ImplVulkan_CreateDeviceObjects()
return true;
}

void ImGui_ImplVulkan_ReCreateMainPipeline(VkRenderPass render_pass, uint32_t subpass, VkSampleCountFlagBits MSSA_samples, const ImGui_ImplVulkan_PipelineRenderingInfo * p_dynamic_rendering)
void ImGui_ImplVulkan_ReCreateMainPipeline(ImGui_ImplVulkan_MainPipelineCreateInfo const& info)
{
ImGui_ImplVulkan_Data* bd = ImGui_ImplVulkan_GetBackendData();
ImGui_ImplVulkan_InitInfo* v = &bd->VulkanInitInfo;
Expand All @@ -1040,19 +1052,29 @@ void ImGui_ImplVulkan_ReCreateMainPipeline(VkRenderPass render_pass, uint32_t su
vkDestroyPipeline(v->Device, bd->Pipeline, v->Allocator);
bd->Pipeline = VK_NULL_HANDLE;
}
v->RenderPass = render_pass;
v->MSAASamples = MSSA_samples;
v->Subpass = subpass;
v->RenderPass = info.RenderPass;
v->MSAASamples = info.MSAASamples;
v->Subpass = info.Subpass;

#ifdef IMGUI_IMPL_VULKAN_HAS_DYNAMIC_RENDERING
if (p_dynamic_rendering)
if (info.pDynamicRendering)
{
v->PipelineRenderingCreateInfo = *p_dynamic_rendering;
v->PipelineRenderingCreateInfo = *info.pDynamicRendering;
}
#else
IM_ASSERT(p_dynamic_rendering == nullptr);
#endif
ImGui_ImplVulkan_CreatePipeline(v->Device, v->Allocator, v->PipelineCache, v->RenderPass, v->MSAASamples, &bd->Pipeline, v->Subpass, p_dynamic_rendering);

ImGui_ImplVulkan_PipelineCreateInfo pci;
pci.Device = v->Device;
pci.Allocator = v->Allocator;
pci.PipelineCache = v->PipelineCache;
pci.RenderPass = v->RenderPass;
pci.Subpass = v->Subpass;
pci.MSAASamples = v->MSAASamples;
pci.pRenderingInfo = info.pDynamicRendering;

bd->Pipeline = ImGui_ImplVulkan_CreatePipeline(pci);
}

void ImGui_ImplVulkan_DestroyDeviceObjects()
Expand Down Expand Up @@ -1163,7 +1185,13 @@ bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info)
}
if (create_pipeline)
{
ImGui_ImplVulkan_ReCreateMainPipeline(v->RenderPass, v->Subpass, v->MSAASamples, p_dynamic_rendering);
ImGui_ImplVulkan_MainPipelineCreateInfo mp_info = {};
mp_info.RenderPass = v->RenderPass;
mp_info.Subpass = v->Subpass;
mp_info.MSAASamples = info->MSAASamples;
mp_info.pDynamicRendering = p_dynamic_rendering;

ImGui_ImplVulkan_ReCreateMainPipeline(mp_info);
}
}

Expand Down
10 changes: 9 additions & 1 deletion backends/imgui_impl_vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,15 @@ struct ImGui_ImplVulkan_InitInfo

// Follow "Getting Started" link and check examples/ folder to learn about using backends!
IMGUI_IMPL_API bool ImGui_ImplVulkan_Init(ImGui_ImplVulkan_InitInfo* info); // The main pipeline will be created if possible (RenderPass xor (UseDynamicRendering && PipelineRenderingCreateInfo->sType is correct))
IMGUI_IMPL_API void ImGui_ImplVulkan_ReCreateMainPipeline(VkRenderPass render_pass, uint32_t subpass, VkSampleCountFlagBits MSAA_samples, const ImGui_ImplVulkan_PipelineRenderingInfo * p_dynamic_rendering = nullptr); // (render_pass xor (p_dynamic_rendering && p_dynamic_rendering is correct (sType and pNext))
#define IMGUI_IMPL_VULKAN_HAS_MAIN_PIPELINE_RE_CREATION 1
struct ImGui_ImplVulkan_MainPipelineCreateInfo
{
VkRenderPass RenderPass = VK_NULL_HANDLE;
uint32_t Subpass = 0;
VkSampleCountFlagBits MSAASamples = {};
const ImGui_ImplVulkan_PipelineRenderingInfo* pDynamicRendering = nullptr;
};
IMGUI_IMPL_API void ImGui_ImplVulkan_ReCreateMainPipeline(ImGui_ImplVulkan_MainPipelineCreateInfo const& info); // (render_pass xor (p_dynamic_rendering && p_dynamic_rendering is correct (sType and pNext))
IMGUI_IMPL_API void ImGui_ImplVulkan_Shutdown();
IMGUI_IMPL_API void ImGui_ImplVulkan_NewFrame();
IMGUI_IMPL_API void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer command_buffer, VkPipeline pipeline = VK_NULL_HANDLE);
Expand Down

0 comments on commit 148060f

Please sign in to comment.