Skip to content

Commit

Permalink
Auto free finished vulkan command buffers in gpu interop feature impl…
Browse files Browse the repository at this point in the history
…ementation (#17782)
  • Loading branch information
kekekeks authored and maxkatz6 committed Dec 19, 2024
1 parent 04231a4 commit 9ad2ad6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/Avalonia.Vulkan/Interop/VulkanCommandBufferPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ namespace Avalonia.Vulkan.Interop;
internal class VulkanCommandBufferPool : IDisposable
{
private readonly IVulkanPlatformGraphicsContext _context;
private readonly bool _autoFree;
private readonly Queue<VulkanCommandBuffer> _commandBuffers = new();
private VkCommandPool _handle;
public VkCommandPool Handle => _handle;

public VulkanCommandBufferPool(IVulkanPlatformGraphicsContext context)
public VulkanCommandBufferPool(IVulkanPlatformGraphicsContext context, bool autoFree = false)
{
_context = context;
_autoFree = autoFree;
var createInfo = new VkCommandPoolCreateInfo
{
sType = VkStructureType.VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
Expand Down Expand Up @@ -53,6 +55,9 @@ public void Dispose()

public unsafe VulkanCommandBuffer CreateCommandBuffer()
{
if (_autoFree)
FreeFinishedCommandBuffers();

var commandBufferAllocateInfo = new VkCommandBufferAllocateInfo
{
sType = VkStructureType.VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
Expand Down
4 changes: 1 addition & 3 deletions src/Avalonia.Vulkan/VulkanExternalObjectsFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public VulkanExternalObjectsFeature(VulkanContext context)
var uuid = new Span<byte>(physicalDeviceIDProperties.deviceUUID, 16).ToArray();
if (uuid.Any(b => b != 0))
DeviceUuid = uuid;
_pool = new VulkanCommandBufferPool(_context);
_pool = new VulkanCommandBufferPool(_context, true);
}

public IReadOnlyList<string> SupportedImageHandleTypes { get; }
Expand All @@ -111,10 +111,8 @@ public CompositionGpuImportedImageSynchronizationCapabilities GetSynchronization

public IVulkanExternalImage ImportImage(IPlatformHandle handle, PlatformGraphicsExternalImageProperties properties)
{
_pool.FreeFinishedCommandBuffers();
if (!SupportedImageHandleTypes.Contains(handle.HandleDescriptor))
throw new NotSupportedException();


return new ImportedImage(_context, _pool, handle, properties);
}
Expand Down

0 comments on commit 9ad2ad6

Please sign in to comment.