[Fix] (MG_Backend/DirectVulkan): Correct viewport setting.

This commit is contained in:
BZLZHH
2026-02-16 18:29:03 +08:00
parent 03d3fcdc66
commit e63eebf425
6 changed files with 41 additions and 20 deletions
@@ -30,6 +30,7 @@ namespace MobileGL::MG_Backend::DirectVulkan::VkManager {
VkExtent2D ChooseExtent(const VkSurfaceCapabilitiesKHR& caps, ANativeWindow* window) {
if (caps.currentExtent.width != UINT32_MAX) return caps.currentExtent;
// try to get frontend viewport size
VkExtent2D extent{640, 480};
#ifdef VK_USE_PLATFORM_ANDROID_KHR
if (window) {
@@ -45,16 +46,22 @@ namespace MobileGL::MG_Backend::DirectVulkan::VkManager {
}
} // namespace
SwapchainManager::~SwapchainManager() { DestroySwapchain(); }
SwapchainManager::~SwapchainManager() {
DestroySwapchain();
}
void SwapchainManager::Initialize() { CreateSwapchain(VK_NULL_HANDLE); }
void SwapchainManager::Initialize() {
CreateSwapchain(VK_NULL_HANDLE);
}
void SwapchainManager::Recreate() {
DestroySwapchain();
CreateSwapchain(VK_NULL_HANDLE);
}
void SwapchainManager::SetFramebuffers(Vector<VkFramebuffer>&& framebuffers) { m_framebuffers = Move(framebuffers); }
void SwapchainManager::SetFramebuffers(Vector<VkFramebuffer>&& framebuffers) {
m_framebuffers = Move(framebuffers);
}
void SwapchainManager::DestroySwapchain() {
auto device = m_ctx.GetDevice();
@@ -102,7 +109,12 @@ namespace MobileGL::MG_Backend::DirectVulkan::VkManager {
VkSurfaceFormatKHR surfaceFormat = ChooseSurfaceFormat(formats);
VkPresentModeKHR presentMode = ChoosePresentMode(modes);
VkExtent2D extent = ChooseExtent(caps, m_ctx.GetWindow());
VkExtent2D extent;
if (m_viewportSize.x() == 0 || m_viewportSize.y() == 0) {
extent = ChooseExtent(caps, m_ctx.GetWindow());
} else {
extent = {m_viewportSize.x(), m_viewportSize.y()};
}
Uint32 imageCount = caps.minImageCount + 1;
if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) imageCount = caps.maxImageCount;
@@ -8,6 +8,7 @@
#pragma once
#include <Includes.h>
#include "MG_Util/Math/VectorTypes.h"
#include "VulkanContext.h"
#include "VkCommon.h"
@@ -22,6 +23,7 @@ namespace MobileGL::MG_Backend::DirectVulkan::VkManager {
void Initialize();
void Recreate();
void SetViewportSize(const UintVec2& size) { m_viewportSize = size; }
VkSwapchainKHR GetSwapchain() const { return m_swapchain; }
VkFormat GetFormat() const { return m_format; }
@@ -31,6 +33,7 @@ namespace MobileGL::MG_Backend::DirectVulkan::VkManager {
const Vector<VkFramebuffer>& GetFramebuffers() const { return m_framebuffers; }
void SetFramebuffers(Vector<VkFramebuffer>&& framebuffers);
Vector<VkFence>& GetImagesInFlight() { return m_imagesInFlight; }
const UintVec2& GetViewportSize() const { return m_viewportSize; }
private:
void CreateSwapchain(VkSwapchainKHR oldSwapchain);
@@ -44,5 +47,6 @@ namespace MobileGL::MG_Backend::DirectVulkan::VkManager {
Vector<VkImageView> m_imageViews;
Vector<VkFramebuffer> m_framebuffers;
Vector<VkFence> m_imagesInFlight;
UintVec2 m_viewportSize{0, 0};
};
} // namespace MobileGL::MG_Backend::DirectVulkan::VkManager
@@ -126,6 +126,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
m_swapchain->SetFramebuffers(Move(fbs));
}
void VulkanRenderer::SetSwapchainViewportSize(const UintVec2& size) {
if (m_swapchain) m_swapchain->SetViewportSize(size);
}
void VulkanRenderer::CreateFrameResources() {
DestroyFrameResources();
Uint32 imageCount = static_cast<Uint32>(m_swapchain->GetImageViews().size());
@@ -242,8 +246,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
void VulkanRenderer::RecordAndSubmit(Uint32 imageIndex) {
auto& frame = *m_frames[m_currentFrame];
VK_VERIFY(vkWaitForFences(m_ctx.GetDevice(), 1, &frame.InFlightFence, VK_TRUE, UINT64_MAX),
"vkWaitForFences");
VK_VERIFY(vkWaitForFences(m_ctx.GetDevice(), 1, &frame.InFlightFence, VK_TRUE, UINT64_MAX), "vkWaitForFences");
VK_VERIFY(vkResetFences(m_ctx.GetDevice(), 1, &frame.InFlightFence), "vkResetFences");
VK_VERIFY(vkResetCommandBuffer(frame.CommandBuffer, 0), "vkResetCommandBuffer");
@@ -351,7 +354,11 @@ namespace MobileGL::MG_Backend::DirectVulkan {
DestroyPipelines();
}
void VulkanRenderer::RenderFrame() { DrawAndPresent(); }
void VulkanRenderer::RenderFrame() {
DrawAndPresent();
}
void VulkanRenderer::Present() { DrawAndPresent(); }
void VulkanRenderer::Present() {
DrawAndPresent();
}
} // namespace MobileGL::MG_Backend::DirectVulkan
@@ -27,6 +27,7 @@ namespace MobileGL::MG_Backend::DirectVulkan {
void Initialize();
void RenderFrame();
void Present();
void SetSwapchainViewportSize(const UintVec2& size);
VkPipeline CreateGraphicsPipelineFromSpv(const String& name, const Vector<Uint>& vertexSpv,
const Vector<Uint>& fragmentSpv);
+8 -11
View File
@@ -8,6 +8,7 @@
#include "TmpImpl.h"
#include "MG_Backend/DirectVulkan/DirectVulkan.h"
#include "MG_Util/Types.h"
#include "Renderer/VkCommon.h"
#include "Renderer/VulkanContext.h"
#include "Renderer/SwapchainManager.h"
@@ -190,6 +191,7 @@ namespace MobileGL::MG_Backend::DirectVulkan::TmpImpl {
Uint32 currentFrame = 0;
Uint32 maxFramesInFlight = 2;
Bool initialized = false;
UintVec2 viewportSize{0, 0};
VkImage depthImage = VK_NULL_HANDLE;
VkDeviceMemory depthMemory = VK_NULL_HANDLE;
@@ -2983,6 +2985,7 @@ namespace MobileGL::MG_Backend::DirectVulkan::TmpImpl {
DestroyRenderPass();
DestroyDepthResources();
g.swapchain->SetViewportSize(g.viewportSize);
g.swapchain->Recreate();
CreateDepthResources();
CreateRenderPass();
@@ -3152,20 +3155,14 @@ namespace MobileGL::MG_Backend::DirectVulkan::TmpImpl {
}
VkViewport vp{};
if (rs->Viewport.z() > 0 && rs->Viewport.w() > 0) {
vp.x = static_cast<Float>(rs->Viewport.x());
vp.y = static_cast<Float>(rs->Viewport.y());
vp.width = static_cast<Float>(rs->Viewport.z());
vp.height = static_cast<Float>(rs->Viewport.w());
} else {
vp.x = 0.0f;
vp.y = 0.0f;
vp.width = static_cast<Float>(g.activeExtent.width);
vp.height = static_cast<Float>(g.activeExtent.height);
}
vp.x = 0.0f;
vp.y = 0.0f;
vp.width = static_cast<Float>(g.activeExtent.width);
vp.height = static_cast<Float>(g.activeExtent.height);
vp.minDepth = 0.0f;
vp.maxDepth = 1.0f;
vkCmdSetViewport(frame->CommandBuffer, 0, 1, &vp);
g.viewportSize = {(Uint)rs->Viewport.z(), (Uint)rs->Viewport.w()};
VkRect2D scissor{};
if (rs->ScissorTestEnabled) {
@@ -94,7 +94,7 @@ namespace MobileGL {
MG_Backend::DirectVulkan::pVulkanRenderer = MakeUnique<MG_Backend::DirectVulkan::VulkanRenderer>(window);
MG_Backend::DirectVulkan::pVulkanRenderer->Initialize();
PrepareDemoRes(); // for demo use
// PrepareDemoRes(); // for demo use
}
EGLSurface CreateWindowSurface(EGLDisplay dpy, EGLConfig config, NativeWindowType window,