forked from amkovkov/GranuSightSoftware2
feat: CameraView + CameraService fences
This commit is contained in:
@@ -30,6 +30,7 @@ class VulkanExportedImage
|
||||
{
|
||||
public required int ImageExportedSemaphoreFd { get; init; }
|
||||
public required int ImageRenderedSemaphoreFd { get; init; }
|
||||
public required int? FenceFd { get; init; }
|
||||
public required int OrignalFd { get; init; }
|
||||
public required int ImageFd { get; init; }
|
||||
public required int ImageWidth { get; init; }
|
||||
@@ -91,18 +92,18 @@ class VulkanRenderer
|
||||
};
|
||||
_context.KhrExternalSemaphoreFd.GetSemaphoreF(_context.Device, in imageRenderedSemaphoreGetFdInfoKHR, out var imageRenderedSemaphoreFd).ThrowOnError();
|
||||
|
||||
// Silk.NET.Vulkan.Fence imageCapturedFence = new Silk.NET.Vulkan.Fence(null);
|
||||
// if (fenceFd is not null)
|
||||
// {
|
||||
// var importSemaphoreFdInfoKHR = new ImportFenceFdInfoKHR()
|
||||
// {
|
||||
// SType = StructureType.ImportSemaphoreFDInfoKhr,
|
||||
// Fd = fenceFd.Value,
|
||||
// Flags = FenceImportFlags.TemporaryBit,
|
||||
// HandleType = ExternalFenceHandleTypeFlags.SyncFDBit
|
||||
// };
|
||||
// _context.KhrExternalFenceFd.ImportFenceF(_context.Device, &importSemaphoreFdInfoKHR);
|
||||
// }
|
||||
Silk.NET.Vulkan.Fence imageCapturedFence = new Silk.NET.Vulkan.Fence(null);
|
||||
if (fenceFd is not null)
|
||||
{
|
||||
var importSemaphoreFdInfoKHR = new ImportFenceFdInfoKHR()
|
||||
{
|
||||
SType = StructureType.ImportSemaphoreFDInfoKhr,
|
||||
Fd = fenceFd.Value,
|
||||
Flags = FenceImportFlags.TemporaryBit,
|
||||
HandleType = ExternalFenceHandleTypeFlags.OpaqueFDBit
|
||||
};
|
||||
_context.KhrExternalFenceFd.ImportFenceF(_context.Device, &importSemaphoreFdInfoKHR);
|
||||
}
|
||||
|
||||
// Console.WriteLine("Import 9");
|
||||
var (importImage, importMemoryReqirements, importMemory) = ImportImage(imageFdDuplicate, width, height, stride);
|
||||
@@ -160,67 +161,71 @@ class VulkanRenderer
|
||||
_context.KhrExternalMemoryFd.GetMemoryF(_context.Device, &memoryGetFdInfoKHR, out var vulkanImageFd).ThrowOnError();
|
||||
|
||||
// Console.WriteLine("Import 17");
|
||||
// var waitSemaphoreIndex = 0;
|
||||
// Silk.NET.Vulkan.Semaphore[] waitSemaphoresArray = new Silk.NET.Vulkan.Semaphore[
|
||||
// !initializationFrame ? 1 : 0
|
||||
// ];
|
||||
// PipelineStageFlags[] pipelineStageFlagsArray = new PipelineStageFlags[waitSemaphoresArray.Length];
|
||||
// if (!initializationFrame)
|
||||
// {
|
||||
// waitSemaphoresArray[waitSemaphoreIndex] = imageRenderedSemaphore;
|
||||
// pipelineStageFlagsArray[waitSemaphoreIndex++] = PipelineStageFlags.AllGraphicsBit;
|
||||
// }
|
||||
// var waitSemaphoresSpan = new ReadOnlySpan<Silk.NET.Vulkan.Semaphore>(waitSemaphoresArray);
|
||||
// var pipelineStageFlagsSpan = new ReadOnlySpan<PipelineStageFlags>(pipelineStageFlagsArray);
|
||||
|
||||
// fixed (PipelineStageFlags* pPipelineStageFlags = pipelineStageFlagsSpan)
|
||||
// {
|
||||
// fixed (Silk.NET.Vulkan.Semaphore* pWaitSemaphoresSpan = waitSemaphoresSpan)
|
||||
// {
|
||||
// var submitInfo = new SubmitInfo
|
||||
// {
|
||||
// SType = StructureType.SubmitInfo,
|
||||
// CommandBufferCount = 1,
|
||||
// PCommandBuffers = &commandBuffer,
|
||||
// PSignalSemaphores = &imageExportedSemaphore,
|
||||
// SignalSemaphoreCount = 1,
|
||||
// PWaitSemaphores = pWaitSemaphoresSpan,
|
||||
// PWaitDstStageMask = pPipelineStageFlags,
|
||||
// WaitSemaphoreCount = checked((uint)waitSemaphoresArray.Length)
|
||||
// };
|
||||
// // Console.WriteLine("Import 18");
|
||||
// _context.Api.QueueSubmit(_context.Queue, 1, &submitInfo, imageCapturedFence).ThrowOnError();
|
||||
// }
|
||||
// }
|
||||
|
||||
ReadOnlySpan<PipelineStageFlags> pipelineStageFlags = [PipelineStageFlags.AllGraphicsBit];
|
||||
fixed (PipelineStageFlags* pPipelineStageFlags = pipelineStageFlags)
|
||||
var waitSemaphoreIndex = 0;
|
||||
Silk.NET.Vulkan.Semaphore[] waitSemaphoresArray = new Silk.NET.Vulkan.Semaphore[
|
||||
!initializationFrame ? 1 : 0
|
||||
];
|
||||
PipelineStageFlags[] pipelineStageFlagsArray = new PipelineStageFlags[waitSemaphoresArray.Length];
|
||||
if (!initializationFrame)
|
||||
{
|
||||
ReadOnlySpan<Silk.NET.Vulkan.Semaphore> waitSemaphores =
|
||||
[
|
||||
imageRenderedSemaphore
|
||||
];
|
||||
var submitInfo = new SubmitInfo
|
||||
{
|
||||
SType = StructureType.SubmitInfo,
|
||||
CommandBufferCount = 1,
|
||||
PCommandBuffers = &commandBuffer,
|
||||
PSignalSemaphores = &imageExportedSemaphore,
|
||||
SignalSemaphoreCount = 1,
|
||||
PWaitSemaphores = initializationFrame ? null : &imageRenderedSemaphore,
|
||||
WaitSemaphoreCount = initializationFrame ? 0u : 1u,
|
||||
PWaitDstStageMask = initializationFrame ? null : pPipelineStageFlags
|
||||
};
|
||||
|
||||
// Console.WriteLine("Import 18");
|
||||
_context.Api.QueueSubmit(_context.Queue, 1, &submitInfo, new Silk.NET.Vulkan.Fence(UIntPtr.Zero)).ThrowOnError();
|
||||
waitSemaphoresArray[waitSemaphoreIndex] = imageRenderedSemaphore;
|
||||
pipelineStageFlagsArray[waitSemaphoreIndex++] = PipelineStageFlags.AllGraphicsBit;
|
||||
}
|
||||
var waitSemaphoresSpan = new ReadOnlySpan<Silk.NET.Vulkan.Semaphore>(waitSemaphoresArray);
|
||||
var pipelineStageFlagsSpan = new ReadOnlySpan<PipelineStageFlags>(pipelineStageFlagsArray);
|
||||
|
||||
_context.Api.Semaphore
|
||||
|
||||
fixed (PipelineStageFlags* pPipelineStageFlagsSpan = pipelineStageFlagsSpan)
|
||||
{
|
||||
fixed (Silk.NET.Vulkan.Semaphore* pWaitSemaphoresSpan = waitSemaphoresSpan)
|
||||
{
|
||||
var submitInfo = new SubmitInfo
|
||||
{
|
||||
SType = StructureType.SubmitInfo,
|
||||
CommandBufferCount = 1,
|
||||
PCommandBuffers = &commandBuffer,
|
||||
PSignalSemaphores = &imageExportedSemaphore,
|
||||
SignalSemaphoreCount = 1,
|
||||
PWaitSemaphores = waitSemaphoresArray.Length == 0 ? null : pWaitSemaphoresSpan,
|
||||
PWaitDstStageMask = waitSemaphoresArray.Length == 0 ? null : pPipelineStageFlagsSpan,
|
||||
WaitSemaphoreCount = checked((uint)waitSemaphoresArray.Length)
|
||||
};
|
||||
// Console.WriteLine("Import 18");
|
||||
Console.WriteLine(imageCapturedFence.Handle);
|
||||
_context.Api.QueueSubmit(_context.Queue, 1, &submitInfo, imageCapturedFence).ThrowOnError();
|
||||
}
|
||||
}
|
||||
|
||||
// ReadOnlySpan<PipelineStageFlags> pipelineStageFlags = [PipelineStageFlags.AllGraphicsBit];
|
||||
// fixed (PipelineStageFlags* pPipelineStageFlags = pipelineStageFlags)
|
||||
// {
|
||||
// ReadOnlySpan<Silk.NET.Vulkan.Semaphore> waitSemaphores =
|
||||
// [
|
||||
// imageRenderedSemaphore
|
||||
// ];
|
||||
// var submitInfo = new SubmitInfo
|
||||
// {
|
||||
// SType = StructureType.SubmitInfo,
|
||||
// CommandBufferCount = 1,
|
||||
// PCommandBuffers = &commandBuffer,
|
||||
// PSignalSemaphores = &imageExportedSemaphore,
|
||||
// SignalSemaphoreCount = 1,
|
||||
// PWaitSemaphores = initializationFrame ? null : &imageRenderedSemaphore,
|
||||
// WaitSemaphoreCount = initializationFrame ? 0u : 1u,
|
||||
// PWaitDstStageMask = initializationFrame ? null : pPipelineStageFlags
|
||||
// };
|
||||
|
||||
// // Console.WriteLine("Import 18");
|
||||
// _context.Api.QueueSubmit(_context.Queue, 1, &submitInfo, new Silk.NET.Vulkan.Fence(UIntPtr.Zero)).ThrowOnError();
|
||||
// }
|
||||
|
||||
// Console.WriteLine("Import 19");
|
||||
return new VulkanExportedImage
|
||||
{
|
||||
ImageExportedSemaphoreFd = imageExportedSemaphoreFd,
|
||||
ImageRenderedSemaphoreFd = imageRenderedSemaphoreFd,
|
||||
FenceFd = fenceFd,
|
||||
OrignalFd = imageFdDuplicate,
|
||||
ImageWidth = width,
|
||||
ImageHeight = height,
|
||||
@@ -251,6 +256,8 @@ class VulkanRenderer
|
||||
_context.Api.FreeMemory(_context.Device, image.Resources.ImportMemory, null);
|
||||
_context.Api.DestroyImage(_context.Device, image.Resources.ExportImage, null);
|
||||
_context.Api.FreeMemory(_context.Device, image.Resources.ExportMemory, null);
|
||||
if (image.FenceFd is not null)
|
||||
close(image.FenceFd.Value);
|
||||
close(image.OrignalFd);
|
||||
close(image.ImageFd);
|
||||
close(image.ImageExportedSemaphoreFd);
|
||||
@@ -455,9 +462,9 @@ public partial class CameraView : Control
|
||||
if (!_initialized)
|
||||
return;
|
||||
int i = 0;
|
||||
_updateTimer = new System.Timers.Timer(100)
|
||||
_updateTimer = new System.Timers.Timer(105)
|
||||
{
|
||||
AutoReset = true
|
||||
AutoReset = false
|
||||
};
|
||||
_updateTimer.Elapsed += async (_, _) =>
|
||||
{
|
||||
@@ -471,8 +478,16 @@ public partial class CameraView : Control
|
||||
if (result is not null)
|
||||
{
|
||||
var (frameBuffer, streamConfiguration) = result.Value;
|
||||
if (frameBuffer.Metadata.Status == FrameMetadata.StatusEnum.FrameSuccess)
|
||||
Update(frameBuffer, streamConfiguration);
|
||||
// if (frameBuffer.Metadata.Status == FrameMetadata.StatusEnum.FrameSuccess)
|
||||
Update(frameBuffer, streamConfiguration);
|
||||
_updateTimer.Start();
|
||||
// else
|
||||
// {
|
||||
// var fenceFd = frameBuffer.ReleaseFence()?.Fd?.Release();
|
||||
// Console.WriteLine($"Buffer returned not success result, closing fd {fenceFd}");
|
||||
// if (fenceFd is not null)
|
||||
// GSS2.Core.Hardware.CameraService.LibC.close(fenceFd.Value);
|
||||
// }
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -591,13 +606,14 @@ public partial class CameraView : Control
|
||||
_renerer is not null)
|
||||
{
|
||||
// Console.WriteLine("Update 2");
|
||||
// var fenceFd = frameBuffer.ReleaseFence()?.Fd?.Release();
|
||||
var fenceFd = frameBuffer.ReleaseFence()?.Fd?.Release();
|
||||
Console.WriteLine($"Released fd {fenceFd}");
|
||||
var image = _renerer.Import(
|
||||
frameBuffer.Planes.First().Fd.Get(),
|
||||
checked((int)streamConfiguration.Size.Width),
|
||||
checked((int)streamConfiguration.Size.Height),
|
||||
checked((int)streamConfiguration.Stride),
|
||||
null, // fenceFd,
|
||||
fenceFd,
|
||||
_initializationFrame);
|
||||
// _renerer.ClearResource(image);
|
||||
// Console.WriteLine("Update 3");
|
||||
|
||||
Reference in New Issue
Block a user