Skip to content

Commit 0f2e3e4

Browse files
Lab: Fix Vulkan validation layer errors when minimizing SDL window (#493)
* Fix missing API checks in SdlView When using Vulkan an exception will trigger when initializing the view if these API checks aren't present. * Fix Vulkan errors when minimizing SDL window When minimizing an SDL window an Invalid swap chain is produced. This guards against that. Co-authored-by: EmileGr <[email protected]>
1 parent ce8e330 commit 0f2e3e4

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/Lab/Experiments/VulkanTriangle/HelloTriangleApplication.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,18 @@ private void InitWindow()
7979
{
8080
var opts = WindowOptions.DefaultVulkan;
8181
opts.IsEventDriven = EventBasedRendering;
82+
83+
// Uncomment the line below to use SDL
84+
// Window.PrioritizeSdl();
85+
8286
_window = Window.Create(opts);
87+
_window.Initialize(); // For safety the window should be initialized before querying the VkSurface
88+
8389
if (_window?.VkSurface is null)
8490
{
8591
throw new NotSupportedException("Windowing platform doesn't support Vulkan.");
8692
}
8793

88-
_window.Initialize();
8994
_window.FramebufferResize += OnFramebufferResize;
9095
}
9196

@@ -592,14 +597,19 @@ private unsafe void CreateLogicalDevice()
592597
Console.WriteLine($"{_vk.CurrentInstance?.Handle} {_vk.CurrentDevice?.Handle}");
593598
}
594599

595-
private unsafe void CreateSwapChain()
600+
private unsafe bool CreateSwapChain()
596601
{
597602
var swapChainSupport = QuerySwapChainSupport(_physicalDevice);
598603

599604
var surfaceFormat = ChooseSwapSurfaceFormat(swapChainSupport.Formats);
600605
var presentMode = ChooseSwapPresentMode(swapChainSupport.PresentModes);
601606
var extent = ChooseSwapExtent(swapChainSupport.Capabilities);
602607

608+
// TODO: On SDL minimizing the window does not affect the frameBufferSize.
609+
// This check can be removed if it does
610+
if (extent.Width == 0 || extent.Height == 0)
611+
return false;
612+
603613
var imageCount = swapChainSupport.Capabilities.MinImageCount + 1;
604614
if (swapChainSupport.Capabilities.MaxImageCount > 0 &&
605615
imageCount > swapChainSupport.Capabilities.MaxImageCount)
@@ -665,6 +675,8 @@ private unsafe void CreateSwapChain()
665675

666676
_swapchainImageFormat = surfaceFormat.Format;
667677
_swapchainExtent = extent;
678+
679+
return true;
668680
}
669681

670682
private unsafe void RecreateSwapChain()
@@ -681,7 +693,13 @@ private unsafe void RecreateSwapChain()
681693

682694
CleanupSwapchain();
683695

684-
CreateSwapChain();
696+
// TODO: On SDL it is possible to get an invalid swap chain when the window is minimized.
697+
// This check can be removed when the above frameBufferSize check catches it.
698+
while (!CreateSwapChain())
699+
{
700+
_window.DoEvents();
701+
}
702+
685703
CreateImageViews();
686704
CreateRenderPass();
687705
CreateGraphicsPipeline();

0 commit comments

Comments
 (0)