From e208ea02ce66a97805463086ee41f025d9f4bc77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Thu, 6 Jun 2024 18:37:23 +0100 Subject: [PATCH] Implemented VulkanDevice.Dispose. --- src/Avalonia.Vulkan/Interop/VulkanDevice.Create.cs | 2 +- src/Avalonia.Vulkan/Interop/VulkanDevice.cs | 14 ++++++++++---- .../UnmanagedInterop/VulkanInstanceApi.cs | 3 +++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Vulkan/Interop/VulkanDevice.Create.cs b/src/Avalonia.Vulkan/Interop/VulkanDevice.Create.cs index dcc48f86347..08d1f637abb 100644 --- a/src/Avalonia.Vulkan/Interop/VulkanDevice.Create.cs +++ b/src/Avalonia.Vulkan/Interop/VulkanDevice.Create.cs @@ -87,7 +87,7 @@ public static IVulkanDevice Create(IVulkanInstance instance, api.GetDeviceQueue(createdDevice, dev.QueueFamilyIndex, 0, out var createdQueue); - return new VulkanDevice(api.Instance, createdDevice, dev.PhysicalDevice, createdQueue, + return new VulkanDevice(api, createdDevice, dev.PhysicalDevice, createdQueue, dev.QueueFamilyIndex, enabledExtensions); } diff --git a/src/Avalonia.Vulkan/Interop/VulkanDevice.cs b/src/Avalonia.Vulkan/Interop/VulkanDevice.cs index 24d02687bc1..a67a629ce2b 100644 --- a/src/Avalonia.Vulkan/Interop/VulkanDevice.cs +++ b/src/Avalonia.Vulkan/Interop/VulkanDevice.cs @@ -9,7 +9,8 @@ namespace Avalonia.Vulkan.Interop; internal partial class VulkanDevice : IVulkanDevice { - private readonly VkDevice _handle; + private readonly VulkanInstanceApi _instanceApi; + private VkDevice _handle; private readonly VkPhysicalDevice _physicalDeviceHandle; private readonly VkQueue _mainQueue; private readonly uint _graphicsQueueIndex; @@ -17,14 +18,15 @@ internal partial class VulkanDevice : IVulkanDevice private Thread? _lockedByThread; private int _lockCount; - private VulkanDevice(IVulkanInstance instance, VkDevice handle, VkPhysicalDevice physicalDeviceHandle, + private VulkanDevice(VulkanInstanceApi instanceApi, VkDevice handle, VkPhysicalDevice physicalDeviceHandle, VkQueue mainQueue, uint graphicsQueueIndex, string[] enabledExtensions) { + _instanceApi = instanceApi; _handle = handle; _physicalDeviceHandle = physicalDeviceHandle; _mainQueue = mainQueue; _graphicsQueueIndex = graphicsQueueIndex; - Instance = instance; + Instance = _instanceApi.Instance; EnabledExtensions = enabledExtensions; } @@ -59,7 +61,11 @@ public IDisposable Lock() public IVulkanInstance Instance { get; } public void Dispose() { - // TODO + if (_handle.Handle != IntPtr.Zero) + { + _instanceApi.DestroyDevice(_handle, IntPtr.Zero); + _handle = default; + } } public object? TryGetFeature(Type featureType) => null; diff --git a/src/Avalonia.Vulkan/UnmanagedInterop/VulkanInstanceApi.cs b/src/Avalonia.Vulkan/UnmanagedInterop/VulkanInstanceApi.cs index fd8c1d40cdf..9a64319b00f 100644 --- a/src/Avalonia.Vulkan/UnmanagedInterop/VulkanInstanceApi.cs +++ b/src/Avalonia.Vulkan/UnmanagedInterop/VulkanInstanceApi.cs @@ -46,6 +46,9 @@ public partial void GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice phys public partial VkResult CreateDevice(VkPhysicalDevice physicalDevice, ref VkDeviceCreateInfo pCreateInfo, IntPtr pAllocator, out VkDevice pDevice); + [GetProcAddress("vkDestroyDevice")] + public partial VkResult DestroyDevice(VkDevice device, IntPtr pAllocator); + [GetProcAddress("vkGetDeviceQueue")] public partial void GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, out VkQueue pQueue);