Browse Source

vulkan: fix validation errors related to scissors when drawing.

Sasha Szpakowski 1 year ago
parent
commit
a894c2f6a4
2 changed files with 31 additions and 32 deletions
  1. 30 32
      src/modules/graphics/vulkan/Graphics.cpp
  2. 1 0
      src/modules/graphics/vulkan/Graphics.h

+ 30 - 32
src/modules/graphics/vulkan/Graphics.cpp

@@ -972,29 +972,42 @@ void Graphics::setColor(Colorf c)
 	states.back().color = c;
 }
 
-void Graphics::setScissor(const Rect &rect)
+void Graphics::applyScissor()
 {
-	flushBatchedDraws();
+	VkRect2D scissor{};
 
-	states.back().scissor = true;
-	states.back().scissorRect = rect;
+	if (renderPassState.isWindow)
+		scissor.extent = swapChainExtent;
+	else
+	{
+		scissor.extent.width = renderPassState.width;
+		scissor.extent.height = renderPassState.height;
+	}
 
-	if (renderPassState.active)
+	if (states.back().scissor)
 	{
+		const Rect &rect = states.back().scissorRect;
 		double dpiScale = getCurrentDPIScale();
 
-		double x = static_cast<double>(rect.x) * dpiScale;
-		double y = static_cast<double>(rect.y) * dpiScale;
-		double w = static_cast<double>(rect.w) * dpiScale;
-		double h = static_cast<double>(rect.h) * dpiScale;
+		// 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);
+	}
 
-		VkRect2D scissor = {
-			{static_cast<int32_t>(x), static_cast<int32_t>(y)},
-			{static_cast<uint32_t>(w), static_cast<uint32_t>(h)}
-		};
+	vkCmdSetScissor(commandBuffers.at(currentFrame), 0, 1, &scissor);
+}
 
-		vkCmdSetScissor(commandBuffers.at(currentFrame), 0, 1, &scissor);
-	}
+void Graphics::setScissor(const Rect &rect)
+{
+	flushBatchedDraws();
+
+	states.back().scissor = true;
+	states.back().scissorRect = rect;
+
+	if (renderPassState.active)
+		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()

+ 1 - 0
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();