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