From 3002e6d8789b7a764db6f85fbbfa3725819d2a7b Mon Sep 17 00:00:00 2001 From: ollie Date: Mon, 29 Jul 2024 22:25:16 +0100 Subject: [PATCH] vk start swapchains --- src/grabs.Graphics.Vulkan/VkDevice.cs | 17 +++++++-- src/grabs.Graphics.Vulkan/VkInstance.cs | 4 +-- src/grabs.Graphics.Vulkan/VkSurface.cs | 2 +- src/grabs.Graphics.Vulkan/VkSwapchain.cs | 46 ++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 src/grabs.Graphics.Vulkan/VkSwapchain.cs diff --git a/src/grabs.Graphics.Vulkan/VkDevice.cs b/src/grabs.Graphics.Vulkan/VkDevice.cs index a736f6f..1c85233 100644 --- a/src/grabs.Graphics.Vulkan/VkDevice.cs +++ b/src/grabs.Graphics.Vulkan/VkDevice.cs @@ -10,7 +10,7 @@ namespace grabs.Graphics.Vulkan; -public unsafe class VkDevice : Device +public sealed unsafe class VkDevice : Device { private readonly Vk _vk; @@ -19,7 +19,12 @@ public unsafe class VkDevice : Device public readonly VulkanDevice Device; - public VkDevice(Vk vk, PhysicalDevice pDevice, VkSurface surface) + public readonly KhrSwapchain KhrSwapchain; + + public readonly Queue GraphicsQueue; + public readonly Queue PresentQueue; + + public VkDevice(Vk vk, VulkanInstance instance, PhysicalDevice pDevice, VkSurface surface) { _vk = vk; @@ -90,6 +95,12 @@ public VkDevice(Vk vk, PhysicalDevice pDevice, VkSurface surface) }; CheckResult(_vk.CreateDevice(pDevice, &deviceCreateInfo, null, out Device), "Create device"); + + if (!_vk.TryGetDeviceExtension(instance, Device, out KhrSwapchain)) + throw new Exception("Failed to get VK_KHR_swapchain device extension."); + + _vk.GetDeviceQueue(Device, _graphicsQueueIndex, 0, out GraphicsQueue); + _vk.GetDeviceQueue(Device, _presentQueueIndex, 0, out PresentQueue); } public override Swapchain CreateSwapchain(in SwapchainDescription description) @@ -175,6 +186,8 @@ public override void ExecuteCommandList(CommandList list) public override void Dispose() { + CheckResult(_vk.DeviceWaitIdle(Device)); + KhrSwapchain.Dispose(); _vk.DestroyDevice(Device, null); } } \ No newline at end of file diff --git a/src/grabs.Graphics.Vulkan/VkInstance.cs b/src/grabs.Graphics.Vulkan/VkInstance.cs index cd18d19..2c88326 100644 --- a/src/grabs.Graphics.Vulkan/VkInstance.cs +++ b/src/grabs.Graphics.Vulkan/VkInstance.cs @@ -10,7 +10,7 @@ namespace grabs.Graphics.Vulkan; -public unsafe class VkInstance : Instance +public sealed unsafe class VkInstance : Instance { public readonly Vk Vk; @@ -106,7 +106,7 @@ public override Device CreateDevice(Surface surface, Adapter? adapter = null) else device = pDevices[0]; - return new VkDevice(Vk, device, (VkSurface) surface); + return new VkDevice(Vk, Instance, device, (VkSurface) surface); } public override Adapter[] EnumerateAdapters() diff --git a/src/grabs.Graphics.Vulkan/VkSurface.cs b/src/grabs.Graphics.Vulkan/VkSurface.cs index c28fc29..c0ce9bf 100644 --- a/src/grabs.Graphics.Vulkan/VkSurface.cs +++ b/src/grabs.Graphics.Vulkan/VkSurface.cs @@ -4,7 +4,7 @@ namespace grabs.Graphics.Vulkan; -public class VkSurface : Surface +public sealed class VkSurface : Surface { public readonly VkInstance Instance; public readonly SurfaceKHR Surface; diff --git a/src/grabs.Graphics.Vulkan/VkSwapchain.cs b/src/grabs.Graphics.Vulkan/VkSwapchain.cs new file mode 100644 index 0000000..7fdbc61 --- /dev/null +++ b/src/grabs.Graphics.Vulkan/VkSwapchain.cs @@ -0,0 +1,46 @@ +using Silk.NET.Vulkan; +using Silk.NET.Vulkan.Extensions.KHR; + +namespace grabs.Graphics.Vulkan; + +public sealed class VkSwapchain : Swapchain +{ + private readonly Vk _vk; + private readonly KhrSwapchain _khrSwapchain; + + public override PresentMode PresentMode { get; set; } + + public VkSwapchain(Vk vk, KhrSwapchain khrSwapchain, SurfaceKHR surface, in SwapchainDescription description) + { + _vk = vk; + _khrSwapchain = khrSwapchain; + + Extent2D swapchainSize = new Extent2D(description.Width, description.Width); + + SwapchainCreateInfoKHR swapchainCreateInfo = new SwapchainCreateInfoKHR() + { + SType = StructureType.SwapchainCreateInfoKhr, + + } + } + + public override Texture GetSwapchainTexture() + { + throw new System.NotImplementedException(); + } + + public override void Resize(uint width, uint height) + { + throw new System.NotImplementedException(); + } + + public override void Present() + { + throw new System.NotImplementedException(); + } + + public override void Dispose() + { + throw new System.NotImplementedException(); + } +} \ No newline at end of file