fix: CameraView memory leakage

This commit is contained in:
2026-01-28 15:03:46 +03:00
parent 8ff52f7d1e
commit 3283d73b3b
10 changed files with 363 additions and 266 deletions
+56 -22
View File
@@ -19,8 +19,13 @@ unsafe public class VulkanContext : IDisposable
public Device Device { get; init; }
public uint QueueFamilyIndex { get; init; }
public Queue Queue { get; init; }
public Semaphore ImageAvailableSemaphore { get; init; }
public Semaphore RenderFinishedSemaphore { get; init; }
public Semaphore ImageExportedSemaphore { get; init; }
public Semaphore ImageRenderedSemaphore { get; init; }
public CommandPool CommandPool { get; init; }
public CommandBuffer CommandBuffer { get; init; }
public KhrExternalMemoryFd KhrExternalMemoryFd { get; init; }
public KhrExternalSemaphoreFd KhrExternalSemaphoreFd { get; init; }
public KhrExternalFenceFd KhrExternalFenceFd { get; init; }
public VulkanContext(string applicationName, string engineName)
{
@@ -30,42 +35,42 @@ unsafe public class VulkanContext : IDisposable
[
"VK_KHR_get_physical_device_properties2",
"VK_KHR_external_memory_capabilities",
"VK_KHR_external_fence_capabilities",
"VK_KHR_external_semaphore_capabilities",
"VK_EXT_debug_utils"
];
List<string> instanceLayers =
[
];
List<string> deviceExtensions =
[
"VK_KHR_external_memory",
"VK_KHR_external_memory_fd",
"VK_KHR_external_semaphore",
"VK_KHR_external_semaphore_fd"
"VK_KHR_external_semaphore_fd",
"VK_KHR_external_fence",
"VK_KHR_external_fence_fd"
];
Instance = CreateInstance(Api, applicationName, engineName, instanceExtensions, instanceLayers);
PhysicalDevice = FindPhysicalDevice(Api, Instance, deviceExtensions);
(Device, QueueFamilyIndex) = CreateDevice(Api, Instance, PhysicalDevice, deviceExtensions);
Queue = CreateQueue(Api, Device, QueueFamilyIndex);
ImageAvailableSemaphore = CreateSemaphore(Api, Device);
RenderFinishedSemaphore = CreateSemaphore(Api, Device);
}
ImageExportedSemaphore = CreateSemaphore(Api, Device);
ImageRenderedSemaphore = CreateSemaphore(Api, Device);
CommandPool = CreateCommandPool(Api, Device, QueueFamilyIndex);
CommandBuffer = CreateCommandBuffer(Api, Device, CommandPool);
public int ExportSemaphoreFd(Semaphore semaphore)
{
if (!Api.TryGetDeviceExtension<KhrExternalSemaphoreFd>(Instance, Device, out var extension))
throw new InvalidOperationException($"No {KhrExternalSemaphoreFd.ExtensionName} device extension");
var info = new SemaphoreGetFdInfoKHR()
{
SType = StructureType.SemaphoreGetFDInfoKhr,
Semaphore = semaphore,
HandleType = ExternalSemaphoreHandleTypeFlags.OpaqueFDBit
};
extension.GetSemaphoreF(Device, in info, out var fd).ThrowOnError();
return fd;
if (!Api.TryGetDeviceExtension<KhrExternalMemoryFd>(Instance, Device, out var khrExternalMemoryFd))
throw new Exception("KhrExternalMemoryFd import error");
KhrExternalMemoryFd = khrExternalMemoryFd;
if (!Api.TryGetDeviceExtension<KhrExternalSemaphoreFd>(Instance, Device, out var khrExternalSemaphoreFd))
throw new Exception("KhrExternalMemoryFd import error");
KhrExternalSemaphoreFd = khrExternalSemaphoreFd;
if (!Api.TryGetDeviceExtension<KhrExternalFenceFd>(Instance, Device, out var khrExternalFenceFd))
throw new Exception("KhrExternalFenceFd import error");
KhrExternalFenceFd = khrExternalFenceFd;
}
private static Instance CreateInstance(Vk api, string applicationName, string engineName, List<string> extentions, List<string> layers)
@@ -186,6 +191,32 @@ unsafe public class VulkanContext : IDisposable
api.CreateSemaphore(device, in semaphoreCreateInfo, null, out var semaphore).ThrowOnError();
return semaphore;
}
private static CommandPool CreateCommandPool(Vk api, Device device, uint queueFamilyIndex)
{
var commandPoolCreateInfo = new CommandPoolCreateInfo
{
SType = StructureType.CommandPoolCreateInfo,
Flags = CommandPoolCreateFlags.ResetCommandBufferBit,
QueueFamilyIndex = queueFamilyIndex
};
api.CreateCommandPool(device, &commandPoolCreateInfo, null, out var commandPool).ThrowOnError();
return commandPool;
}
private static CommandBuffer CreateCommandBuffer(Vk api, Device device, CommandPool commandPool)
{
var commandBufferAllocateInfo = new CommandBufferAllocateInfo
{
SType = StructureType.CommandBufferAllocateInfo,
CommandPool = commandPool,
Level = CommandBufferLevel.Primary,
CommandBufferCount = 1
};
api.AllocateCommandBuffers(device, &commandBufferAllocateInfo, out var commandBuffer).ThrowOnError();
return commandBuffer;
}
protected virtual void Dispose(bool disposing)
{
@@ -195,8 +226,11 @@ unsafe public class VulkanContext : IDisposable
{
if (Api is null)
return;
Api.DestroySemaphore(Device, ImageAvailableSemaphore, null);
Api.DestroySemaphore(Device, RenderFinishedSemaphore, null);
Api.DestroySemaphore(Device, ImageExportedSemaphore, null);
Api.DestroySemaphore(Device, ImageRenderedSemaphore, null);
Api.FreeCommandBuffers(Device, CommandPool, [CommandBuffer]);
Api.DestroyCommandPool(Device, CommandPool, null);
Api.DestroyDevice(Device, null);
}
_disposedValue = true;
}