From a894c2f6a46ceaad0fd4bbc36e49a4429ca653a3 Mon Sep 17 00:00:00 2001 From: Sasha Szpakowski Date: Fri, 3 May 2024 20:24:25 -0300 Subject: [PATCH] vulkan: fix validation errors related to scissors when drawing. --- src/modules/graphics/vulkan/Graphics.cpp | 62 ++++++++++++------------ src/modules/graphics/vulkan/Graphics.h | 1 + 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/modules/graphics/vulkan/Graphics.cpp b/src/modules/graphics/vulkan/Graphics.cpp index 4d8a852e0..0284e922a 100644 --- a/src/modules/graphics/vulkan/Graphics.cpp +++ b/src/modules/graphics/vulkan/Graphics.cpp @@ -972,6 +972,33 @@ void Graphics::setColor(Colorf c) states.back().color = c; } +void Graphics::applyScissor() +{ + VkRect2D scissor{}; + + if (renderPassState.isWindow) + scissor.extent = swapChainExtent; + else + { + scissor.extent.width = renderPassState.width; + scissor.extent.height = renderPassState.height; + } + + if (states.back().scissor) + { + const Rect &rect = states.back().scissorRect; + double dpiScale = getCurrentDPIScale(); + + // TODO: clamp this to the above viewport size. + scissor.offset.x = (int)(rect.x * dpiScale); + scissor.offset.y = (int)(rect.y * dpiScale); + scissor.extent.width = (uint32)(rect.w * dpiScale); + scissor.extent.height = (uint32)(rect.h * dpiScale); + } + + vkCmdSetScissor(commandBuffers.at(currentFrame), 0, 1, &scissor); +} + void Graphics::setScissor(const Rect &rect) { flushBatchedDraws(); @@ -980,21 +1007,7 @@ void Graphics::setScissor(const Rect &rect) states.back().scissorRect = rect; if (renderPassState.active) - { - double dpiScale = getCurrentDPIScale(); - - double x = static_cast(rect.x) * dpiScale; - double y = static_cast(rect.y) * dpiScale; - double w = static_cast(rect.w) * dpiScale; - double h = static_cast(rect.h) * dpiScale; - - VkRect2D scissor = { - {static_cast(x), static_cast(y)}, - {static_cast(w), static_cast(h)} - }; - - vkCmdSetScissor(commandBuffers.at(currentFrame), 0, 1, &scissor); - } + applyScissor(); } void Graphics::setScissor() @@ -1004,19 +1017,7 @@ void Graphics::setScissor() states.back().scissor = false; if (renderPassState.active) - { - VkRect2D scissor{}; - scissor.offset = { 0, 0 }; - if (renderPassState.isWindow) - scissor.extent = swapChainExtent; - else - { - scissor.extent.width = renderPassState.width; - scissor.extent.height = renderPassState.height; - } - - vkCmdSetScissor(commandBuffers.at(currentFrame), 0, 1, &scissor); - } + applyScissor(); } void Graphics::setStencilState(const StencilState &s) @@ -2550,10 +2551,7 @@ void Graphics::startRenderPass() vkCmdBeginRenderPass(commandBuffers.at(currentFrame), &renderPassState.beginInfo, VK_SUBPASS_CONTENTS_INLINE); - if (states.back().scissor) - setScissor(states.back().scissorRect); - else - setScissor(); + applyScissor(); } void Graphics::endRenderPass() diff --git a/src/modules/graphics/vulkan/Graphics.h b/src/modules/graphics/vulkan/Graphics.h index 7c20d0f63..1fb624d9c 100644 --- a/src/modules/graphics/vulkan/Graphics.h +++ b/src/modules/graphics/vulkan/Graphics.h @@ -378,6 +378,7 @@ private: void setDefaultRenderPass(); void startRenderPass(); void endRenderPass(); + void applyScissor(); VkSampler createSampler(const SamplerState &sampler); void cleanupUnusedObjects(); void requestSwapchainRecreation();