Skip to content

Commit

Permalink
vulkan修复更改surface大小时的变化
Browse files Browse the repository at this point in the history
  • Loading branch information
enenH committed Feb 18, 2024
1 parent b2d16de commit c623c74
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/VulkanGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "VulkanGraphics.h"
#include "imgui_impl_vulkan.h"
#include <vulkan/vulkan_android.h>
#include <android/native_window.h>

#ifndef NDEBUG

Expand Down Expand Up @@ -267,6 +268,25 @@ void VulkanGraphics::Setup() {
}

void VulkanGraphics::PrepareFrame(bool resize) {
if (m_SwapChainRebuild || resize) {
if (m_LastWidth == 0 || m_LastHeight == 0) {
m_LastWidth = (int) m_Width;
m_LastHeight = (int) m_Height;
}
int width = ANativeWindow_getWidth(m_Window);
int height = ANativeWindow_getHeight(m_Window);
if (width > 0 && height > 0) {
if (width != m_LastWidth || height != m_LastHeight) {
m_LastWidth = width;
m_LastHeight = height;
ImGui_ImplVulkan_SetMinImageCount(m_MinImageCount);
ImGui_ImplVulkanH_CreateOrResizeWindow(m_Instance, m_PhysicalDevice, m_Device, wd.get(),
m_QueueFamily, m_Allocator, width, height, m_MinImageCount);
wd->FrameIndex = 0;
m_SwapChainRebuild = false;
}
}
}
ImGui_ImplVulkan_NewFrame();
}

Expand All @@ -278,7 +298,7 @@ void VulkanGraphics::Render(ImDrawData *drawData) {
err = vkAcquireNextImageKHR(m_Device, wd->Swapchain, UINT64_MAX, image_acquired_semaphore, VK_NULL_HANDLE,
&wd->FrameIndex);
if (err == VK_ERROR_OUT_OF_DATE_KHR /*|| err == VK_SUBOPTIMAL_KHR*/) {
//g_SwapChainRebuild = true;
m_SwapChainRebuild = true;
return;
}
//check_vk_result(err);
Expand Down Expand Up @@ -351,7 +371,7 @@ void VulkanGraphics::Render(ImDrawData *drawData) {
info.pImageIndices = &wd->FrameIndex;
VkResult err = vkQueuePresentKHR(m_Queue, &info);
if (err == VK_ERROR_OUT_OF_DATE_KHR /*|| err == VK_SUBOPTIMAL_KHR*/) {
//g_SwapChainRebuild = true;
m_SwapChainRebuild = true;
return;
}
//check_vk_result(err);
Expand Down
2 changes: 2 additions & 0 deletions src/VulkanGraphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class VulkanGraphics : public AndroidImgui {
int m_MinImageCount = 2;
bool m_SwapChainRebuild = false;

int m_LastWidth = 0;
int m_LastHeight = 0;
public:
bool Create() override;

Expand Down

0 comments on commit c623c74

Please sign in to comment.